Files
paperclip/server/src/__tests__/run-continuations.test.ts
T
Dotta 454edfe81e Add recovery handoff system notices (#5289)
## Thinking Path

> - Paperclip orchestrates AI agents for zero-human companies.
> - Agent runs can end productively while the source issue still lacks a
durable final disposition.
> - That leaves the control plane unsure whether to resume, escalate, or
close the work.
> - Issue comments also need a presentation contract so system-authored
recovery notices can render as first-class thread messages without
overloading normal comments.
> - This pull request adds successful-run handoff recovery, comment
presentation metadata, and system notice rendering.
> - The benefit is stricter task liveness with clearer operator-facing
recovery state.

## What Changed

- Added successful-run handoff decisions, wake payloads, escalation
behavior, and recovery tests.
- Added issue comment presentation metadata with migration
`0078_white_darwin.sql` and shared/server/company portability support.
- Rendered recovery/system notices in issue chat with dedicated UI
components, fixtures, tests, and storybook/lab coverage.
- Included the current recovery model-profile hint patch so automatic
recovery follow-ups use the cheap profile.

## Verification

- `pnpm install --frozen-lockfile`
- `pnpm exec vitest run
server/src/services/recovery/successful-run-handoff.test.ts
ui/src/components/SystemNotice.test.tsx
ui/src/lib/system-notice-comment.test.ts
ui/src/components/IssueChatThreadSystemNotice.test.tsx`

## Risks

- Migration-bearing PR: merge this before any other branch that might
later add a migration.
- The branch touches both recovery services and issue-thread rendering,
so review should pay attention to recovery wake idempotency and comment
metadata compatibility.

## Model Used

- OpenAI GPT-5 Codex via Paperclip `codex_local` adapter, with
shell/git/GitHub CLI tool use.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] If this change affects the UI, I have included before/after
screenshots
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-05-06 06:05:58 -05:00

174 lines
5.3 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
DEFAULT_MAX_LIVENESS_CONTINUATION_ATTEMPTS,
RUN_LIVENESS_CONTINUATION_REASON,
buildRunLivenessContinuationIdempotencyKey,
decideRunLivenessContinuation,
} from "../services/run-continuations.ts";
const companyId = "company-1";
const agentId = "agent-1";
const issueId = "issue-1";
const runId = "run-1";
function run(overrides: Record<string, unknown> = {}) {
return {
id: runId,
companyId,
agentId,
continuationAttempt: 0,
...overrides,
} as never;
}
function issue(overrides: Record<string, unknown> = {}) {
return {
id: issueId,
companyId,
identifier: "PAP-1577",
title: "Add bounded liveness continuation wakes",
status: "in_progress",
assigneeAgentId: agentId,
executionState: null,
projectId: null,
...overrides,
} as never;
}
function agent(overrides: Record<string, unknown> = {}) {
return {
id: agentId,
companyId,
status: "idle",
...overrides,
} as never;
}
describe("run liveness continuations", () => {
it("enqueues the first plan_only continuation for the same issue and assignee", () => {
const decision = decideRunLivenessContinuation({
run: run(),
issue: issue(),
agent: agent(),
livenessState: "plan_only",
livenessReason: "Planned without acting",
nextAction: "Take the first concrete action now.",
budgetBlocked: false,
idempotentWakeExists: false,
});
expect(decision.kind).toBe("enqueue");
if (decision.kind !== "enqueue") return;
expect(decision.nextAttempt).toBe(1);
expect(decision.idempotencyKey).toBe(
buildRunLivenessContinuationIdempotencyKey({
issueId,
sourceRunId: runId,
livenessState: "plan_only",
nextAttempt: 1,
}),
);
expect(decision.payload).toMatchObject({
issueId,
sourceRunId: runId,
livenessState: "plan_only",
livenessReason: "Planned without acting",
continuationAttempt: 1,
maxContinuationAttempts: DEFAULT_MAX_LIVENESS_CONTINUATION_ATTEMPTS,
instruction: "Take the first concrete action now.",
modelProfile: "cheap",
});
expect(decision.contextSnapshot).toMatchObject({
issueId,
wakeReason: RUN_LIVENESS_CONTINUATION_REASON,
modelProfile: "cheap",
livenessContinuationAttempt: 1,
livenessContinuationMaxAttempts: DEFAULT_MAX_LIVENESS_CONTINUATION_ATTEMPTS,
livenessContinuationSourceRunId: runId,
livenessContinuationState: "plan_only",
livenessContinuationReason: "Planned without acting",
livenessContinuationInstruction: "Take the first concrete action now.",
});
});
it("enqueues the second empty_response continuation", () => {
const decision = decideRunLivenessContinuation({
run: run({ continuationAttempt: 1 }),
issue: issue(),
agent: agent(),
livenessState: "empty_response",
livenessReason: "No useful output",
nextAction: null,
budgetBlocked: false,
idempotentWakeExists: false,
});
expect(decision.kind).toBe("enqueue");
if (decision.kind !== "enqueue") return;
expect(decision.nextAttempt).toBe(2);
});
it("leaves advanced terminal runs to stranded issue recovery instead of bounded liveness continuation", () => {
const decision = decideRunLivenessContinuation({
run: run(),
issue: issue(),
agent: agent(),
livenessState: "advanced",
livenessReason: "Run produced concrete action evidence: created an issue comment",
nextAction: "Resume the implementation from the remaining acceptance criteria.",
budgetBlocked: false,
idempotentWakeExists: false,
});
expect(decision).toEqual({
kind: "skip",
reason: "liveness state is not actionable for continuation",
});
});
it("does not enqueue a third continuation and returns an exhaustion comment", () => {
const decision = decideRunLivenessContinuation({
run: run({ continuationAttempt: 2 }),
issue: issue(),
agent: agent(),
livenessState: "plan_only",
livenessReason: "Still planning",
nextAction: null,
budgetBlocked: false,
idempotentWakeExists: false,
});
expect(decision.kind).toBe("exhausted");
if (decision.kind !== "exhausted") return;
expect(decision.comment).toContain("Bounded liveness continuation exhausted");
expect(decision.comment).toContain("Attempts used: 2/2");
});
it("skips non-actionable and guarded issues", () => {
const guardedCases = [
{ livenessState: "advanced" as const },
{ issue: issue({ status: "done" }) },
{ issue: issue({ assigneeAgentId: "other-agent" }) },
{ issue: issue({ executionState: { status: "pending" } }) },
{ agent: agent({ status: "paused" }) },
{ budgetBlocked: true },
{ idempotentWakeExists: true },
];
for (const guarded of guardedCases) {
const decision = decideRunLivenessContinuation({
run: run(),
issue: guarded.issue ?? issue(),
agent: guarded.agent ?? agent(),
livenessState: guarded.livenessState ?? "plan_only",
livenessReason: "No progress",
nextAction: null,
budgetBlocked: guarded.budgetBlocked ?? false,
idempotentWakeExists: guarded.idempotentWakeExists ?? false,
});
expect(decision.kind).toBe("skip");
}
});
});