fix(recovery): tell the fresh run which follow-ups the cut-off run had already read

The opening note now lists the person's messages the interrupted run had read
(task_history.read run-log events, with the read time) before what that run then
created, including its own final comment, so the agent answers a re-delivered
follow-up once and does not post DONE again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DottaandClaude Opus 5.5 committed 2026-11-12 19:36:33 -06:00
1 parent 8d83b88410
commit d2be6d624d
2 files changed
+43 -4

No files matched your search

@@ -3,7 +3,7 @@ import { and, eq, sql } from "drizzle-orm";
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import {
activityLog, agentWakeupRequests, agents, approvals, authUsers, companies, createDb, environmentLeases, environments, executionWorkspaces, heartbeatRuns, issueApprovals, issueRecoveryActions, issueThreadInteractions,
issueComments, issues, nativeRunFinalizations, toolInvocations,
heartbeatRunEvents, issueComments, issues, nativeRunFinalizations, toolInvocations,
} from "@paperclipai/db";
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "../__tests__/helpers/embedded-postgres.js";
import { deliverReconciledExecutions, settleUnrecoverableExecutions } from "./execution-recovery-resolution.js";
@@ -808,6 +808,24 @@ suite("fresh-continuation fallback", () => {
expect(hold!.evidence.freshContinuation).toMatchObject({ createdObjects: { subtasks: expect.arrayContaining([expect.stringContaining("SM sub 1")]) } });
}, 60_000);
it("names the follow-up the cut-off run had read and its own final comment, so the agent answers once (t003)", async () => {
const f = await unfinishedRun({ runtime: "native", errorCode: "native_restart_recovery_blocked" });
const [followUp] = await db.insert(issueComments).values({ companyId, issueId: f.issueId, authorType: "user",
authorUserId: "board-user", body: "Additional requirement: include the line FOLLOWUP-0d981334" } as typeof issueComments.$inferInsert).returning();
await db.insert(heartbeatRunEvents).values({ companyId, runId: f.runId, agentId, seq: 1, eventType: "task_history.read",
stream: "system", level: "info", payload: { comments: { [followUp!.id]: followUp!.updatedAt.toISOString() } },
} as typeof heartbeatRunEvents.$inferInsert);
await db.insert(issueComments).values({ companyId, issueId: f.issueId, authorAgentId: agentId, createdByRunId: f.runId,
body: "DONE-0d981334" } as typeof issueComments.$inferInsert);
expect(await ensureFreshContinuation(db, { companyId, runId: f.runId, trigger: "recovery_refused" })).toMatchObject({ kind: "continued" });
const [hold] = await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.id, f.actionId!));
const note = (hold!.evidence.executionReconciliation as Record<string, string>).outcomeEvidence;
expect(note).toContain("had already read these messages");
expect(note).toContain("FOLLOWUP-0d981334");
expect(note).toContain('posted comments "DONE-0d981334"');
expect(note.indexOf("had already read")).toBeLessThan(note.indexOf("DONE-0d981334"));
}, 60_000);
it.each([
{ title: "a pending approval", setup: "approval" as const, errorCode: "native_continuation_requires_reconciliation" },
{ title: "a credential the person has not shared", setup: "none" as const, errorCode: "configuration_incomplete" },
@@ -1,6 +1,6 @@
import { and, asc, desc, eq, gt, inArray, isNull, or, sql } from "drizzle-orm";
import {
agentWakeupRequests, agents, approvals, companies, documentRevisions, documents, executionWorkspaces, heartbeatRuns, issueApprovals,
agentWakeupRequests, agents, approvals, companies, documentRevisions, documents, executionWorkspaces, heartbeatRunEvents, heartbeatRuns, issueApprovals,
issueComments, issueRecoveryActions, issueThreadInteractions, issues, nativeRunFinalizations, toolInvocations, type Db,
} from "@paperclipai/db";
import {
@@ -195,7 +195,20 @@ async function unverifiedActions(tx: Db, run: typeof heartbeatRuns.$inferSelect)
return [...names].slice(0, 20);
}
export type FreshContinuationCreatedObjects = { subtasks: string[]; comments: string[]; documents: string[] };
export type FreshContinuationCreatedObjects = { subtasks: string[]; comments: string[]; documents: string[]; readFollowUps?: string[] };
/** Comment ids the run read through get_task_history, with the read time (the
* `task_history.read` run-log event). One function, so the queued-comment
* owner's read helper can replace it in one line. */
async function taskHistoryReadsOfRun(tx: Db, run: Pick<typeof heartbeatRuns.$inferSelect, "id" | "companyId">) {
const events = await tx.select({ payload: heartbeatRunEvents.payload, createdAt: heartbeatRunEvents.createdAt })
.from(heartbeatRunEvents).where(and(eq(heartbeatRunEvents.companyId, run.companyId), eq(heartbeatRunEvents.runId, run.id),
eq(heartbeatRunEvents.eventType, "task_history.read"))).orderBy(asc(heartbeatRunEvents.createdAt)).limit(50);
const firstReadAt = new Map<string, Date>();
for (const event of events) for (const commentId of Object.keys(object(object(event.payload).comments)))
if (!firstReadAt.has(commentId)) firstReadAt.set(commentId, event.createdAt);
return firstReadAt;
}
const quoted = (value: string, max = 80) => `"${value.replace(/\s+/g, " ").trim().slice(0, max)}${value.length > max ? "..." : ""}"`;
@@ -213,7 +226,12 @@ async function createdObjects(tx: Db, run: typeof heartbeatRuns.$inferSelect): P
.innerJoin(documents, eq(documents.id, documentRevisions.documentId))
.where(and(eq(documentRevisions.companyId, run.companyId), eq(documentRevisions.createdByRunId, run.id))).limit(10),
]);
const reads = await taskHistoryReadsOfRun(tx, run);
const readComments = reads.size ? await tx.select({ id: issueComments.id, body: issueComments.body }).from(issueComments).where(and(
eq(issueComments.companyId, run.companyId), inArray(issueComments.id, [...reads.keys()]),
eq(issueComments.authorType, "user"), isNull(issueComments.deletedAt))).limit(10) : [];
return {
readFollowUps: readComments.map(comment => `${quoted(comment.body)} (read at ${reads.get(comment.id)!.toISOString().slice(11, 16)} UTC)`),
subtasks: children.map(child => `${child.identifier ?? "subtask"} ${quoted(child.title)}`),
comments: comments.map(comment => quoted(comment.body)),
documents: revisions.map(revision => quoted(revision.title ?? "untitled document")),
@@ -236,6 +254,9 @@ export function freshContinuationNote(input: { runId: string; unverified: string
+ "If repeating it would be a governed or irreversible action, request it through the normal approval step. "
+ "Nothing about the previous attempt needs to be restored before you continue.";
const created = input.created;
const read = created?.readFollowUps?.length
? ` That run had already read these messages from a person: ${created.readFollowUps.join(", ")}. Answer each once; what it did after reading is below.`
: "";
const existing = created && (created.subtasks.length || created.comments.length || created.documents.length)
? ` That run already did these, and they exist now: ${[
created.subtasks.length ? `created subtasks ${created.subtasks.join(", ")}` : null,
@@ -243,7 +264,7 @@ export function freshContinuationNote(input: { runId: string; unverified: string
created.documents.length ? `saved documents ${created.documents.join(", ")}` : null,
].filter(Boolean).join("; ")}. Do not create or post them again; read them and continue from where they leave off.`
: "";
return `The previous attempt (run ${input.runId}) was cut off before it finished. ${files}${existing} ${actions} ${guidance}`;
return `The previous attempt (run ${input.runId}) was cut off before it finished. ${files}${read}${existing} ${actions} ${guidance}`;
}
const USER_TEXT = "The previous run was cut off. Paperclip continues the task in a fresh run from its last saved work.";