mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-29 04:25:52 +01:00
176 lines
5.4 KiB
TypeScript
176 lines
5.4 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.",
|
|
});
|
|
expect(decision.contextSnapshot).toMatchObject({
|
|
issueId,
|
|
wakeReason: RUN_LIVENESS_CONTINUATION_REASON,
|
|
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("enqueues advanced terminal runs so progress is not mistaken for a live path", () => {
|
|
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.kind).toBe("enqueue");
|
|
if (decision.kind !== "enqueue") return;
|
|
expect(decision.payload).toMatchObject({
|
|
issueId,
|
|
sourceRunId: runId,
|
|
livenessState: "advanced",
|
|
instruction: "Resume the implementation from the remaining acceptance criteria.",
|
|
});
|
|
});
|
|
|
|
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: "needs_followup" 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");
|
|
}
|
|
});
|
|
});
|