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:
@@ -70,6 +70,8 @@ export function activityService(db: Db) {
|
|||||||
finishedAt: heartbeatRuns.finishedAt,
|
finishedAt: heartbeatRuns.finishedAt,
|
||||||
createdAt: heartbeatRuns.createdAt,
|
createdAt: heartbeatRuns.createdAt,
|
||||||
invocationSource: heartbeatRuns.invocationSource,
|
invocationSource: heartbeatRuns.invocationSource,
|
||||||
|
usageJson: heartbeatRuns.usageJson,
|
||||||
|
resultJson: heartbeatRuns.resultJson,
|
||||||
})
|
})
|
||||||
.from(activityLog)
|
.from(activityLog)
|
||||||
.innerJoin(heartbeatRuns, eq(activityLog.runId, heartbeatRuns.id))
|
.innerJoin(heartbeatRuns, eq(activityLog.runId, heartbeatRuns.id))
|
||||||
|
|||||||
@@ -55,6 +55,35 @@ function deriveTaskKey(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function deriveCommentId(
|
||||||
|
contextSnapshot: Record<string, unknown> | null | undefined,
|
||||||
|
payload: Record<string, unknown> | null | undefined,
|
||||||
|
) {
|
||||||
|
return (
|
||||||
|
readNonEmptyString(contextSnapshot?.wakeCommentId) ??
|
||||||
|
readNonEmptyString(contextSnapshot?.commentId) ??
|
||||||
|
readNonEmptyString(payload?.commentId) ??
|
||||||
|
null
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function mergeCoalescedContextSnapshot(
|
||||||
|
existingRaw: unknown,
|
||||||
|
incoming: Record<string, unknown>,
|
||||||
|
) {
|
||||||
|
const existing = parseObject(existingRaw);
|
||||||
|
const merged: Record<string, unknown> = {
|
||||||
|
...existing,
|
||||||
|
...incoming,
|
||||||
|
};
|
||||||
|
const commentId = deriveCommentId(incoming, null);
|
||||||
|
if (commentId) {
|
||||||
|
merged.commentId = commentId;
|
||||||
|
merged.wakeCommentId = commentId;
|
||||||
|
}
|
||||||
|
return merged;
|
||||||
|
}
|
||||||
|
|
||||||
function runTaskKey(run: typeof heartbeatRuns.$inferSelect) {
|
function runTaskKey(run: typeof heartbeatRuns.$inferSelect) {
|
||||||
return deriveTaskKey(run.contextSnapshot as Record<string, unknown> | null, null);
|
return deriveTaskKey(run.contextSnapshot as Record<string, unknown> | null, null);
|
||||||
}
|
}
|
||||||
@@ -914,7 +943,9 @@ export function heartbeatService(db: Db) {
|
|||||||
const reason = opts.reason ?? null;
|
const reason = opts.reason ?? null;
|
||||||
const payload = opts.payload ?? null;
|
const payload = opts.payload ?? null;
|
||||||
const issueIdFromPayload = readNonEmptyString(payload?.["issueId"]);
|
const issueIdFromPayload = readNonEmptyString(payload?.["issueId"]);
|
||||||
|
const commentIdFromPayload = readNonEmptyString(payload?.["commentId"]);
|
||||||
const taskKey = deriveTaskKey(contextSnapshot, payload);
|
const taskKey = deriveTaskKey(contextSnapshot, payload);
|
||||||
|
const wakeCommentId = deriveCommentId(contextSnapshot, payload);
|
||||||
|
|
||||||
if (!readNonEmptyString(contextSnapshot["wakeReason"]) && reason) {
|
if (!readNonEmptyString(contextSnapshot["wakeReason"]) && reason) {
|
||||||
contextSnapshot.wakeReason = reason;
|
contextSnapshot.wakeReason = reason;
|
||||||
@@ -928,6 +959,12 @@ export function heartbeatService(db: Db) {
|
|||||||
if (!readNonEmptyString(contextSnapshot["taskKey"]) && taskKey) {
|
if (!readNonEmptyString(contextSnapshot["taskKey"]) && taskKey) {
|
||||||
contextSnapshot.taskKey = taskKey;
|
contextSnapshot.taskKey = taskKey;
|
||||||
}
|
}
|
||||||
|
if (!readNonEmptyString(contextSnapshot["commentId"]) && commentIdFromPayload) {
|
||||||
|
contextSnapshot.commentId = commentIdFromPayload;
|
||||||
|
}
|
||||||
|
if (!readNonEmptyString(contextSnapshot["wakeCommentId"]) && wakeCommentId) {
|
||||||
|
contextSnapshot.wakeCommentId = wakeCommentId;
|
||||||
|
}
|
||||||
if (!readNonEmptyString(contextSnapshot["wakeSource"])) {
|
if (!readNonEmptyString(contextSnapshot["wakeSource"])) {
|
||||||
contextSnapshot.wakeSource = source;
|
contextSnapshot.wakeSource = source;
|
||||||
}
|
}
|
||||||
@@ -978,11 +1015,34 @@ export function heartbeatService(db: Db) {
|
|||||||
.where(and(eq(heartbeatRuns.agentId, agentId), inArray(heartbeatRuns.status, ["queued", "running"])))
|
.where(and(eq(heartbeatRuns.agentId, agentId), inArray(heartbeatRuns.status, ["queued", "running"])))
|
||||||
.orderBy(desc(heartbeatRuns.createdAt));
|
.orderBy(desc(heartbeatRuns.createdAt));
|
||||||
|
|
||||||
const sameScopeRun = activeRuns.find((candidate) =>
|
const sameScopeQueuedRun = activeRuns.find(
|
||||||
isSameTaskScope(runTaskKey(candidate), taskKey),
|
(candidate) => candidate.status === "queued" && isSameTaskScope(runTaskKey(candidate), taskKey),
|
||||||
);
|
);
|
||||||
|
const sameScopeRunningRun = activeRuns.find(
|
||||||
|
(candidate) => candidate.status === "running" && isSameTaskScope(runTaskKey(candidate), taskKey),
|
||||||
|
);
|
||||||
|
const shouldQueueFollowupForCommentWake =
|
||||||
|
Boolean(wakeCommentId) && Boolean(sameScopeRunningRun) && !sameScopeQueuedRun;
|
||||||
|
|
||||||
|
const coalescedTargetRun =
|
||||||
|
sameScopeQueuedRun ??
|
||||||
|
(shouldQueueFollowupForCommentWake ? null : sameScopeRunningRun ?? null);
|
||||||
|
|
||||||
|
if (coalescedTargetRun) {
|
||||||
|
const mergedContextSnapshot = mergeCoalescedContextSnapshot(
|
||||||
|
coalescedTargetRun.contextSnapshot,
|
||||||
|
contextSnapshot,
|
||||||
|
);
|
||||||
|
const mergedRun = await db
|
||||||
|
.update(heartbeatRuns)
|
||||||
|
.set({
|
||||||
|
contextSnapshot: mergedContextSnapshot,
|
||||||
|
updatedAt: new Date(),
|
||||||
|
})
|
||||||
|
.where(eq(heartbeatRuns.id, coalescedTargetRun.id))
|
||||||
|
.returning()
|
||||||
|
.then((rows) => rows[0] ?? coalescedTargetRun);
|
||||||
|
|
||||||
if (sameScopeRun) {
|
|
||||||
await db.insert(agentWakeupRequests).values({
|
await db.insert(agentWakeupRequests).values({
|
||||||
companyId: agent.companyId,
|
companyId: agent.companyId,
|
||||||
agentId,
|
agentId,
|
||||||
@@ -995,10 +1055,10 @@ export function heartbeatService(db: Db) {
|
|||||||
requestedByActorType: opts.requestedByActorType ?? null,
|
requestedByActorType: opts.requestedByActorType ?? null,
|
||||||
requestedByActorId: opts.requestedByActorId ?? null,
|
requestedByActorId: opts.requestedByActorId ?? null,
|
||||||
idempotencyKey: opts.idempotencyKey ?? null,
|
idempotencyKey: opts.idempotencyKey ?? null,
|
||||||
runId: sameScopeRun.id,
|
runId: mergedRun.id,
|
||||||
finishedAt: new Date(),
|
finishedAt: new Date(),
|
||||||
});
|
});
|
||||||
return sameScopeRun;
|
return mergedRun;
|
||||||
}
|
}
|
||||||
|
|
||||||
const wakeupRequest = await db
|
const wakeupRequest = await db
|
||||||
|
|||||||
@@ -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 type { Db } from "@paperclip/db";
|
||||||
import { approvals } from "@paperclip/db";
|
import { agents, approvals, heartbeatRuns } from "@paperclip/db";
|
||||||
import type { SidebarBadges } from "@paperclip/shared";
|
import type { SidebarBadges } from "@paperclip/shared";
|
||||||
|
|
||||||
const ACTIONABLE_APPROVAL_STATUSES = ["pending", "revision_requested"];
|
const ACTIONABLE_APPROVAL_STATUSES = ["pending", "revision_requested"];
|
||||||
|
const FAILED_HEARTBEAT_STATUSES = ["failed", "timed_out"];
|
||||||
|
|
||||||
export function sidebarBadgeService(db: Db) {
|
export function sidebarBadgeService(db: Db) {
|
||||||
return {
|
return {
|
||||||
@@ -19,10 +20,29 @@ export function sidebarBadgeService(db: Db) {
|
|||||||
)
|
)
|
||||||
.then((rows) => Number(rows[0]?.count ?? 0));
|
.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 {
|
return {
|
||||||
// Inbox currently mirrors actionable approvals; expand as inbox categories grow.
|
inbox: actionableApprovals + failedRuns,
|
||||||
inbox: actionableApprovals,
|
|
||||||
approvals: actionableApprovals,
|
approvals: actionableApprovals,
|
||||||
|
failedRuns,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user