perf(database): replace listSnapshot+JS filter with scoped SQL in getInbox
Replace 17-table snapshot fetch + 6 JS filter passes with 5 scoped
SELECTs targeting only inbox-relevant rows:
- approvals where state = 'pending'
- runs where status in ('failed', 'timed_out') and created_at > 24h
- agents where budget breach > 80%
- tasks where status in ('blocked', 'in_review')
- agent_messages with __board__ read-receipt left join filtered to
company/incident channel or urgent priority
Agent name lookup is a single id->name map built from one scoped agents
read. Return shape unchanged.
This commit is contained in:
1 parent
19c575933d
commit
94a9e457ce
1 file changed
+117
-71
@@ -172,56 +172,84 @@ export function buildCompanyCrudMethods(
|
|||||||
|
|
||||||
getInbox(companyId: string): InboxItem[] {
|
getInbox(companyId: string): InboxItem[] {
|
||||||
const items: InboxItem[] = [];
|
const items: InboxItem[] = [];
|
||||||
const snapshot = deps.listSnapshot({ companyId });
|
const oneDayAgo = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString();
|
||||||
const agents = snapshot.agents;
|
|
||||||
const agentMap = new Map(agents.map((a) => [a.id, a]));
|
|
||||||
|
|
||||||
// Pending approvals
|
const agentRows = readAll<{ id: string; name: string }>(
|
||||||
for (const approval of snapshot.approvals.filter(
|
"select id, name from agents where company_id = ?",
|
||||||
(a) => a.companyId === companyId && a.state === "pending",
|
companyId,
|
||||||
)) {
|
);
|
||||||
const requester = agentMap.get(approval.requestedByAgentId ?? "");
|
const agentNameMap = new Map(agentRows.map((a) => [a.id, a.name]));
|
||||||
|
|
||||||
|
const pendingApprovals = readAll<{
|
||||||
|
id: string; type: string; payloadSummary: string;
|
||||||
|
requestedByAgentId: string | null; createdAt: string;
|
||||||
|
}>(
|
||||||
|
`select id, type, payload_summary as payloadSummary,
|
||||||
|
requested_by_agent_id as requestedByAgentId, created_at as createdAt
|
||||||
|
from approvals where company_id = ? and state = 'pending'
|
||||||
|
order by created_at desc`,
|
||||||
|
companyId,
|
||||||
|
);
|
||||||
|
for (const approval of pendingApprovals) {
|
||||||
|
const requesterName = approval.requestedByAgentId
|
||||||
|
? agentNameMap.get(approval.requestedByAgentId) ?? null
|
||||||
|
: null;
|
||||||
items.push({
|
items.push({
|
||||||
id: `approval-${approval.id}`,
|
id: `approval-${approval.id}`,
|
||||||
type: "pending_approval",
|
type: "pending_approval",
|
||||||
title: approval.payloadSummary,
|
title: approval.payloadSummary,
|
||||||
subtitle: `${approval.type.replaceAll("_", " ")} — requested by ${requester?.name ?? "Unknown"}`,
|
subtitle: `${approval.type.replaceAll("_", " ")} — requested by ${requesterName ?? "Unknown"}`,
|
||||||
severity: approval.type === "dangerous_command" ? "critical" : "high",
|
severity: approval.type === "dangerous_command" ? "critical" : "high",
|
||||||
entityType: "approval",
|
entityType: "approval",
|
||||||
entityId: approval.id,
|
entityId: approval.id,
|
||||||
agentId: approval.requestedByAgentId,
|
agentId: approval.requestedByAgentId,
|
||||||
agentName: requester?.name ?? null,
|
agentName: requesterName,
|
||||||
createdAt: approval.createdAt,
|
createdAt: approval.createdAt,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Failed runs (last 24h)
|
const failedRuns = readAll<{
|
||||||
const oneDayAgo = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString();
|
id: string; agentId: string; summary: string;
|
||||||
for (const run of snapshot.runs.filter(
|
errorMessage: string | null; createdAt: string;
|
||||||
(r) =>
|
}>(
|
||||||
r.companyId === companyId &&
|
`select id, agent_id as agentId, summary, error_message as errorMessage,
|
||||||
(r.status === "failed" || r.status === "timed_out") &&
|
created_at as createdAt
|
||||||
r.createdAt > oneDayAgo,
|
from runs
|
||||||
)) {
|
where company_id = ?
|
||||||
const agent = agentMap.get(run.agentId);
|
and status in ('failed', 'timed_out')
|
||||||
|
and created_at > ?
|
||||||
|
order by created_at desc`,
|
||||||
|
companyId,
|
||||||
|
oneDayAgo,
|
||||||
|
);
|
||||||
|
for (const run of failedRuns) {
|
||||||
|
const agentName = agentNameMap.get(run.agentId) ?? null;
|
||||||
items.push({
|
items.push({
|
||||||
id: `run-${run.id}`,
|
id: `run-${run.id}`,
|
||||||
type: "failed_run",
|
type: "failed_run",
|
||||||
title: run.summary || "Run failed",
|
title: run.summary || "Run failed",
|
||||||
subtitle: `${agent?.name ?? "Unknown agent"} — ${run.errorMessage?.slice(0, 100) ?? "Unknown error"}`,
|
subtitle: `${agentName ?? "Unknown agent"} — ${run.errorMessage?.slice(0, 100) ?? "Unknown error"}`,
|
||||||
severity: "high",
|
severity: "high",
|
||||||
entityType: "run",
|
entityType: "run",
|
||||||
entityId: run.id,
|
entityId: run.id,
|
||||||
agentId: run.agentId,
|
agentId: run.agentId,
|
||||||
agentName: agent?.name ?? null,
|
agentName,
|
||||||
createdAt: run.createdAt,
|
createdAt: run.createdAt,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Budget warnings (agents at >80% budget)
|
const budgetAlertRows = readAll<{
|
||||||
for (const agent of agents.filter(
|
id: string; name: string; budgetMonthlyUsd: number; spentMonthlyUsd: number; updatedAt: string;
|
||||||
(a) => a.budgetMonthlyUsd > 0 && a.spentMonthlyUsd / a.budgetMonthlyUsd > 0.8,
|
}>(
|
||||||
)) {
|
`select id, name, budget_monthly_usd as budgetMonthlyUsd,
|
||||||
|
spent_monthly_usd as spentMonthlyUsd, updated_at as updatedAt
|
||||||
|
from agents
|
||||||
|
where company_id = ?
|
||||||
|
and budget_monthly_usd > 0
|
||||||
|
and (spent_monthly_usd * 1.0 / budget_monthly_usd) > 0.8`,
|
||||||
|
companyId,
|
||||||
|
);
|
||||||
|
for (const agent of budgetAlertRows) {
|
||||||
const pct = Math.round((agent.spentMonthlyUsd / agent.budgetMonthlyUsd) * 100);
|
const pct = Math.round((agent.spentMonthlyUsd / agent.budgetMonthlyUsd) * 100);
|
||||||
items.push({
|
items.push({
|
||||||
id: `budget-${agent.id}`,
|
id: `budget-${agent.id}`,
|
||||||
@@ -237,62 +265,80 @@ export function buildCompanyCrudMethods(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Blocked tasks
|
const taskAlertRows = readAll<{
|
||||||
for (const task of snapshot.tasks.filter(
|
id: string; title: string; status: string; priority: string;
|
||||||
(t) => t.companyId === companyId && t.status === "blocked",
|
assigneeAgentId: string | null; updatedAt: string;
|
||||||
)) {
|
}>(
|
||||||
const assignee = agentMap.get(task.assigneeAgentId ?? "");
|
`select id, title, status, priority,
|
||||||
items.push({
|
assignee_agent_id as assigneeAgentId, updated_at as updatedAt
|
||||||
id: `blocked-${task.id}`,
|
from tasks
|
||||||
type: "blocked_task",
|
where company_id = ? and status in ('blocked', 'in_review')
|
||||||
title: task.title,
|
order by updated_at desc`,
|
||||||
subtitle: `Assigned to ${assignee?.name ?? "Unassigned"} — blocked`,
|
companyId,
|
||||||
severity: task.priority === "critical" ? "critical" : "medium",
|
);
|
||||||
entityType: "task",
|
for (const task of taskAlertRows) {
|
||||||
entityId: task.id,
|
const assigneeName = task.assigneeAgentId ? agentNameMap.get(task.assigneeAgentId) ?? null : null;
|
||||||
agentId: task.assigneeAgentId,
|
if (task.status === "blocked") {
|
||||||
agentName: assignee?.name ?? null,
|
items.push({
|
||||||
createdAt: task.updatedAt,
|
id: `blocked-${task.id}`,
|
||||||
});
|
type: "blocked_task",
|
||||||
|
title: task.title,
|
||||||
|
subtitle: `Assigned to ${assigneeName ?? "Unassigned"} — blocked`,
|
||||||
|
severity: task.priority === "critical" ? "critical" : "medium",
|
||||||
|
entityType: "task",
|
||||||
|
entityId: task.id,
|
||||||
|
agentId: task.assigneeAgentId,
|
||||||
|
agentName: assigneeName,
|
||||||
|
createdAt: task.updatedAt,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
items.push({
|
||||||
|
id: `review-${task.id}`,
|
||||||
|
type: "review_needed",
|
||||||
|
title: task.title,
|
||||||
|
subtitle: `Submitted by ${assigneeName ?? "Unknown"} — awaiting review`,
|
||||||
|
severity: task.priority === "critical" ? "high" : "medium",
|
||||||
|
entityType: "task",
|
||||||
|
entityId: task.id,
|
||||||
|
agentId: task.assigneeAgentId,
|
||||||
|
agentName: assigneeName,
|
||||||
|
createdAt: task.updatedAt,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tasks needing review
|
const messageRows = readAll<{
|
||||||
for (const task of snapshot.tasks.filter(
|
id: string; fromAgentId: string; subject: string; channel: string;
|
||||||
(t) => t.companyId === companyId && t.status === "in_review",
|
priority: string; readAt: string | null; createdAt: string;
|
||||||
)) {
|
}>(
|
||||||
const assignee = agentMap.get(task.assigneeAgentId ?? "");
|
`select messages.id as id,
|
||||||
items.push({
|
messages.from_agent_id as fromAgentId,
|
||||||
id: `review-${task.id}`,
|
messages.subject as subject,
|
||||||
type: "review_needed",
|
messages.channel as channel,
|
||||||
title: task.title,
|
messages.priority as priority,
|
||||||
subtitle: `Submitted by ${assignee?.name ?? "Unknown"} — awaiting review`,
|
reads.read_at as readAt,
|
||||||
severity: task.priority === "critical" ? "high" : "medium",
|
messages.created_at as createdAt
|
||||||
entityType: "task",
|
from agent_messages messages
|
||||||
entityId: task.id,
|
left join agent_message_reads reads
|
||||||
agentId: task.assigneeAgentId,
|
on reads.message_id = messages.id and reads.agent_id = '__board__'
|
||||||
agentName: assignee?.name ?? null,
|
where messages.company_id = ?
|
||||||
createdAt: task.updatedAt,
|
and reads.read_at is null
|
||||||
});
|
and (messages.channel = 'company' or messages.channel = 'incident' or messages.priority = 'urgent')
|
||||||
}
|
order by messages.created_at desc`,
|
||||||
|
companyId,
|
||||||
// Unread urgent agent messages (company-wide or incident channels that the Board should see)
|
);
|
||||||
for (const msg of snapshot.agentMessages.filter(
|
for (const msg of messageRows) {
|
||||||
(m) =>
|
const senderName = agentNameMap.get(msg.fromAgentId) ?? null;
|
||||||
m.companyId === companyId &&
|
|
||||||
!m.readAt &&
|
|
||||||
(m.channel === "company" || m.channel === "incident" || m.priority === "urgent"),
|
|
||||||
)) {
|
|
||||||
const sender = agentMap.get(msg.fromAgentId);
|
|
||||||
items.push({
|
items.push({
|
||||||
id: `message-${msg.id}`,
|
id: `message-${msg.id}`,
|
||||||
type: "agent_message",
|
type: "agent_message",
|
||||||
title: msg.subject,
|
title: msg.subject,
|
||||||
subtitle: `${sender?.name ?? "Unknown"} — ${msg.channel === "incident" ? "INCIDENT" : msg.channel} ${msg.priority === "urgent" ? "(urgent)" : ""}`,
|
subtitle: `${senderName ?? "Unknown"} — ${msg.channel === "incident" ? "INCIDENT" : msg.channel} ${msg.priority === "urgent" ? "(urgent)" : ""}`,
|
||||||
severity: msg.priority === "urgent" || msg.channel === "incident" ? "critical" : "medium",
|
severity: msg.priority === "urgent" || msg.channel === "incident" ? "critical" : "medium",
|
||||||
entityType: "message",
|
entityType: "message",
|
||||||
entityId: msg.id,
|
entityId: msg.id,
|
||||||
agentId: msg.fromAgentId,
|
agentId: msg.fromAgentId,
|
||||||
agentName: sender?.name ?? null,
|
agentName: senderName,
|
||||||
createdAt: msg.createdAt,
|
createdAt: msg.createdAt,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user