feat: comment-triggered wakeups, coalescing improvements, and failed run badges

Enhance heartbeat wakeup to propagate wakeCommentId, queue follow-up runs
for comment wakes on already-running agents, and merge coalesced context
snapshots. Add failed run count to sidebar badges and expose usage/result
JSON in activity service.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Forgotten
2026-02-20 10:32:17 -06:00
parent 2c3c2cf724
commit b327687c92
3 changed files with 91 additions and 9 deletions

View File

@@ -1,9 +1,10 @@
import { and, eq, inArray, sql } from "drizzle-orm";
import { and, desc, eq, inArray, not, sql } from "drizzle-orm";
import type { Db } from "@paperclip/db";
import { approvals } from "@paperclip/db";
import { agents, approvals, heartbeatRuns } from "@paperclip/db";
import type { SidebarBadges } from "@paperclip/shared";
const ACTIONABLE_APPROVAL_STATUSES = ["pending", "revision_requested"];
const FAILED_HEARTBEAT_STATUSES = ["failed", "timed_out"];
export function sidebarBadgeService(db: Db) {
return {
@@ -19,10 +20,29 @@ export function sidebarBadgeService(db: Db) {
)
.then((rows) => Number(rows[0]?.count ?? 0));
const latestRunByAgent = await db
.selectDistinctOn([heartbeatRuns.agentId], {
runStatus: heartbeatRuns.status,
})
.from(heartbeatRuns)
.innerJoin(agents, eq(heartbeatRuns.agentId, agents.id))
.where(
and(
eq(heartbeatRuns.companyId, companyId),
eq(agents.companyId, companyId),
not(eq(agents.status, "terminated")),
),
)
.orderBy(heartbeatRuns.agentId, desc(heartbeatRuns.createdAt));
const failedRuns = latestRunByAgent.filter((row) =>
FAILED_HEARTBEAT_STATUSES.includes(row.runStatus),
).length;
return {
// Inbox currently mirrors actionable approvals; expand as inbox categories grow.
inbox: actionableApprovals,
inbox: actionableApprovals + failedRuns,
approvals: actionableApprovals,
failedRuns,
};
},
};