mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-22 16:51:28 +02:00
## Thinking Path > - Paperclip manages AI agents and their work. > - An agent workflow needs persistent facts and a safe way to transfer work. > - Creating a task for each phase splits the objective, discussion, workspace, and review policy. > - A case can retain the facts while one primary task moves between agents. > - Transfers must survive process failure and preserve private access, budgets, and completion gates. > - This pull request adds durable case handoffs, native case tools, and read-only case pages. > - Prompts describe the workflow. Existing task controls decide when work can run and finish. ## Linked Issues or Issue Description **Problem or motivation** Prompt-driven agent teams need a persistent record that can move through triage, implementation, verification, and correction. Ordinary reassignment does not provide a durable instruction, safe release barrier, or recoverable delivery receipt for an agent handing off its own running task. **Proposed solution** Extend standalone cases with one primary task, a descriptive phase, retained access sources, and a handoff ledger. Apply assignments after source execution and workspace settlement. Deliver each transfer through the existing durable wake queue. Provide `create_case`, `read_case`, `update_case`, and `handoff_case` in the native runner. Creation requires kickoff and explicit workflow text. An explicit handoff to `done` closes the case when normal task completion is accepted. **Alternatives considered** Do not add a workflow engine, filesystem queue, configured state graph, or one task per phase. The workflow document combines transition prose with an optional Mermaid `stateDiagram-v2` map. The server does not execute the diagram. The Pipelines experiment removed on master stays removed. **Roadmap alignment** This extends Agent Reviews and Approvals, Enforced Outcomes, Bounded Run Recovery, and Activity Log attribution. Existing approval, review, budget, privacy, workspace, and responsible-user rules remain authoritative. Related work: [ordinary handoff wake recovery](https://github.com/paperclipai/paperclip/pull/15019). This feature adds a separate persisted case-handoff receipt and release barrier. ## What Changed - Add case routing fields, retained access sources, durable handoff history, delivery leases, and replay-safe migrations. - Keep the same primary task across agent phases. Preserve each returning agent's own session and the task's workspace. - Fence outgoing mutations, wait for provider and workspace release, and recover delivery without duplicate task comments or runs. Recheck current assignment permissions as the original caller before applying a delayed transfer. Scope a run Stop to that run’s own handoff intents, so repeated old Stop requests preserve newer work. Cancelling terminal intent without a source run also requires fenced current task ownership, including native retries and preparation with an old checkout. For Cloud callers, revalidate current stack membership with an uncached Cloud RPC and derive owner elevation from the current managed setting. - Keep case reads, documents, assets, Audit, and live events inside retained task/project permissions. - Add native case creation/read/update/handoff tools. Store required workflow text and initial documents on the first task. Page oversized case facts and referenced document bodies through the dedicated tool with bounded UTF-8 JSON, current routing versions, snapshot conflicts, and current source authorization on every page. - Add read-only case lists and detail pages, task-style list controls, compact dotted field tables, linked handoff tasks, and case filters in Audit. - Add native/legacy integration tests, migration/restart tests, browser coverage, and bounded live receipt fixtures. Extend the trusted sandbox condition to cover the new subscription fixture profile. - Hide obsolete paused-agent warnings on completed or cancelled task threads. - Synchronize the company-switch settings test with React effects after a CI failure. Preserve its security assertions and production remount behavior. ## Verification The branch directly includes master `9ef434fe79`. The required `ci / verify` and `ci / e2e` gates pass on head `765fccd67c`. [Current-head CI](https://github.com/paperclipai/paperclip/actions/runs/39464348614) passes typecheck, build, tests, and all eight browser shards. Security checks pass. [Greptile gives this exact head 5/5](https://github.com/paperclipai/paperclip/pull/16959#issuecomment-6291156549), with no unresolved review threads. The optional Paperclip Review check remains queued because its assigned agent is paused. It is not a required branch check. No check result was overridden. Local workspace typecheck and build pass after the master merge, along with database migration checks, token gates, map checks, Storybook catalog checks, and the Copilot profile identity check. Standard builds refresh Runner vendor output. Focused checks pass 2,008 API/paging/context tests and 7 strict case protocol tests. The final commit changes one test fixture only; its ten tests, UI typecheck, and token gates pass. No production source changed in that commit. On current head `765fccd67c`, all **194 critical workflow tests** executed and passed with no skipped tests in those files: - 38 Stop/ownership tests, including board completion intent, native retries, stale Stop, and preparation with an old checkout. - 2 actual legacy process/HTTP handoff tests, including scratch failure, a closed-stdio child that continues writing, and recovery through a fresh worker after the process group stops. - 5 migration tests, including SQL column metadata, preserved null authority context, and all six prior-numbering receipts. - 3 process-settlement and 5 scratch tests. - 88 handoff-service, 18 heartbeat handoff, and 21 native case-tool integration tests. - 7 bounded case/document-read projection tests and 7 strict protocol-contract tests. The current CI checkout was `fed65d49`, merging `765fccd67c` into master `9ef434fe79`. Its new document regressions reconstruct exact Unicode/escaped bodies and revisions through real native authority and authenticated HTTP with generic API tools disabled. They cover case-owned, primary-task, and linked-task documents; routing on every page; stale document snapshots; access loss and unlinking; unrelated/foreign/private sources; system documents; and quarantined content. Active-task document tools retain their existing behavior. These are current-head test results from the first CI attempt; the browser-only retry did not rerun these suites. The full `64fdd3403f` CI failed one separate settings test: after a company switch, it observed an unsettled API-key draft. The production component and test were unchanged from prior green source. Production switches remount both keyed settings layers; the test directly reused an unkeyed view and used a timer tick instead of waiting for React effects. The final commit replaces that fixture synchronization with React `act`. The exact secret-clearing assertions and all timeouts remain unchanged. The original assertion failure remains recorded; no unchanged retry was used. The narrow unchanged baseline and fixed fixture each pass 10 tests. The upstream result-paging merge preserves case capture namespaces and current asset privacy. Its API integration suite executed 39 of 41 tests successfully, with two skipped; the optional assigned-result PRP integration test was skipped. Neither skipped suite is counted in the 194 critical tests or as live provider proof. All 361 upstream migration entries and SQL bodies remain unchanged. Six case migrations follow master as 0363–0368 with identical SQL hashes. Official Drizzle generators produced the combined metadata and five retained snapshots. Generation reports no schema changes, and the snapshot drift test passes. The earlier `ce361ae5d0` CI passed on attempt 1, including its then-current 179 critical tests. Fresh review later identified unusable document references with generic API tools disabled; the bounded document reads and ten additional authenticated HTTP tests address that finding. The full local `pnpm test:run` attempt was cancelled after this Mac exhausted PostgreSQL semaphore capacity; it is **not** counted as a pass. Later database attempts also stopped before assertions or skipped, including one sandbox socket denial. Two original stale-Stop regressions did reproduce the bug against the old source. A REST serialization assertion passed after two preserved database-startup failures. No test timeout was increased. All 194 assertions listed above executed and passed in current-head CI. The complete 385-file, 3,963-test UI shard also passed after the fixture repair. A configured-provider receipt workflow passed 13 backend checks across triage → implementation → verification → correction → done, using one task and four work runs. The saved receipt is revision 2 with total 6 USD; case and task both closed. The original canonical attempt remains failed because its browser assertion confused the terminal destination with completed-task tooltips. The corrected full browser verifier passed on the same retained database and original runs, with no additional provider calls. The last clean-build browser readback is on `6a86c54a53`; later changes affect recovery, authorization, bounded reads, and settlement. Accounted work cost is a $1.010574 rate-card estimate; provider billing and setup-probe usage are unavailable. Copilot profile 49 passed the unchanged seven-fixture private protocol pack (46/46 checks, no retries) and the product file-edit canary after master's completion-replay change. All 60 bound implementation hashes remain unchanged on the current source. Historical profile-47/48 identities and results remain intact. See the [profile-49 compatibility record](https://github.com/paperclipai/paperclip/blob/codex/persistent-case-handoffs/doc/plans/2026-10-20-copilot-completion-recovery-compatibility-v49.json). This is bounded compatibility evidence, not a fresh full matrix, installed-package product test, or Daytona rollout. Preserved CI failures and retries: - On `20662930ac`, Slack managed setup failed a five-second success assertion after reload. Its test/application path was unchanged from a prior green head. One unchanged-source retry passed all 25 tests in that shard; no source or timeout changed. - On `9402b4f17a`, two CI workers received shutdown signals at 15:24:19 UTC. Neither log had an assertion failure before interruption. One unchanged-source retry of each passed all Runner static checks and all 32 tests in browser shard 6; both aggregates passed. The next head, `7b4374edaf`, passed all CI without retries. - On `295f289b5a`, serialized shard 9 timed out during its first device-login test, then the next test found an active login lease. Direct login/test paths were unchanged from a prior green head. Cold import plus a timed-out async body using the next test's mutable actor/store is the supported fixture-race explanation; no request trace proves that sequence. The original failure remains recorded. One unchanged-source retry passed all 294 tests in 19 files, including all 41 device-login tests. No login source or timeout changed. The subsequent `ce361ae5d0` CI passed without retries. - On `64fdd3403f`, one company-switch settings assertion observed an unsettled draft. The final test-only commit uses React `act` without changing assertions, timeouts, or production code. On `765fccd67c`, all ten settings tests and the complete 385-file, 3,963-test UI shard passed. - On `765fccd67c`, browser shard 3 completed the split-handoff assertions, then cleanup received HTTP 500 from run cancellation after a one-second PostgreSQL row-lock timeout. The extra chat run was reporting completed work while teardown began. Cleanup stopped before restoring a global experiment flag, causing a later assertion to fail. Relevant production and test paths are unchanged from an earlier green commit. Stop's lock order is consistent, and no nested self-lock was found; the exact PostgreSQL blocker was not recorded. One unchanged-shard retry passed. The original failure, trace, and downstream flag failure remain recorded. No Stop implementation, cleanup assertion, or timeout changed. The retry supports intermittent cleanup contention; it does not identify the original blocking transaction. ## Risks - Local process transfers require host-owned PID/group absence. Missing or uncertain termination evidence keeps delivery pending, independently of scratch setup. - Handoff admission and task cancellation share execution ownership. New regression tests cover temporary holds, late source writes, and uncertain provider termination. - Retained private sources use an intersection of existing permissions. This intentionally fails closed when a source is missing or an existing task cannot preserve that boundary. - Migrations add tables and indexes. They preserve master history and can replay over prior feature installs. Migration 0368 adds nullable internal authorization provenance with no default or authority backfill. Historical transfers still awaiting release without this context fail closed; an operator must cancel them and request a new handoff. - Delayed Cloud handoffs depend on the current Cloud membership RPC. An unavailable, ambiguous, expired, or mismatched response holds the handoff for retry; it does not reuse cached authority or grant access. - Bounded reads reload and reauthorize the selected projection per page. Underlying work grows with the data size and number of pages. Changed data or routing requires restarting from offset zero without the old snapshot. - Phase names and workflow diagrams remain advisory. They do not satisfy review or approval gates. - This is a large, cross-cutting PR. The maintainer requested one PR for the whole feature; the commits separate data, runtime, UI, and qualification work. ## Model Used OpenAI Codex, GPT-6 family, with tool use, code execution, and delegated code review. The exact deployment model ID and context-window size were not exposed in the session. Live fixture agents used the explicitly configured provider model recorded in their test evidence. ## 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 searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
323 lines
24 KiB
TypeScript
323 lines
24 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { expect } from "@playwright/test";
|
|
import { pollUntil, RunnerApiHttpError } from "./api.js";
|
|
import type { runCaseHandoffsFlow } from "./case-handoffs-flow.js";
|
|
import { caseHandoffRunFailure, hasNativeHandoffReceipt } from "./case-handoffs-scoring.js";
|
|
import {
|
|
CASE_RECEIPT_BUDGET_CENTS, CASE_RECEIPT_MAX_TURNS, CASE_RECEIPT_RUN_TIMEOUT_MS,
|
|
caseReceiptAgentPrompt,
|
|
} from "./case-receipt-cases.js";
|
|
import { gradeCaseReceipt, hasTaskDocumentRead, type CaseReceiptObservation } from "./case-receipt-scoring.js";
|
|
import { codexSubscriptionAgentConfig } from "./codex-subscription-fixture.js";
|
|
import { collectRunEvents } from "./run-observations.js";
|
|
|
|
type Row = Record<string, any>;
|
|
type Input = Parameters<typeof runCaseHandoffsFlow>[0];
|
|
const roles = ["triage", "implementation", "verification"] as const;
|
|
|
|
/** A real-provider smoke with tiny work products and ordinary production APIs. */
|
|
export async function runCaseReceiptFlow(input: Input) {
|
|
const { api, page, fixtures, execution, nonce } = input;
|
|
const companyPath = `/api/companies/${fixtures.company.id}`;
|
|
const agents = { triage: fixtures.agent.id, implementation: "", verification: "" };
|
|
const configuredAgents: Row[] = [];
|
|
const setupObservations: Row[] = [];
|
|
let issue: Row | null = null;
|
|
let caseRecord: Row | null = null;
|
|
let runs: Row[] = [], handoffs: Row[] = [];
|
|
let receipt: Row | null = null, review: Row | null = null, initialBrief: Row | null = null;
|
|
let triageReadBrief = false;
|
|
let receiptRevisions: Row[] = [];
|
|
let kickoffReplayed = false, verifierReadReceipt = false, builderReadReview = false;
|
|
const handoffToolRunIds: string[] = [];
|
|
let budgetEvidence: CaseReceiptObservation["budgets"];
|
|
const utcDay = new Date().toISOString().slice(0, 10);
|
|
const configuredLimits = { maxRuns: CASE_RECEIPT_MAX_TURNS, runTimeoutMs: CASE_RECEIPT_RUN_TIMEOUT_MS,
|
|
attemptTimeoutMs: execution.task.attemptTimeoutMs.local, automaticRetryPolicy: execution.task.automaticRetryPolicy,
|
|
agents: configuredAgents };
|
|
const agentConfig = (role: typeof roles[number], original: Row) => ({
|
|
...original, budgetMonthlyCents: CASE_RECEIPT_BUDGET_CENTS,
|
|
adapterConfig: { ...original.adapterConfig, timeoutSec: CASE_RECEIPT_RUN_TIMEOUT_MS / 1000, modelReasoningEffort: "low" },
|
|
runtimeConfig: { ...original.runtimeConfig, heartbeat: { ...original.runtimeConfig?.heartbeat,
|
|
enabled: false, maxConcurrentRuns: 1, maxDailyRuns: role === "implementation" ? 2 : 1,
|
|
maxDailyCostCents: CASE_RECEIPT_BUDGET_CENTS } },
|
|
});
|
|
async function waitForReady(role: typeof roles[number]) {
|
|
// Production setup performs a small provider probe outside heartbeat usage.
|
|
// Observe the initial attempt; do not restart setup or wait for its 60s retry.
|
|
await pollUntil({ label: `receipt ${role} readiness`, deadlineAt: Math.min(input.deadlineAt, Date.now() + 45_000), intervalMs: 500,
|
|
load: async () => {
|
|
const agent = await api.get<Row>(`/api/agents/${agents[role]}`);
|
|
const observation = { role, agentId: agent.id, lifecycleState: agent.lifecycleState,
|
|
status: agent.status, lifecycleError: agent.lifecycleError, lifecycleHolds: agent.lifecycleHolds,
|
|
observedAt: new Date().toISOString() };
|
|
const previous = setupObservations.at(-1);
|
|
if (!previous || previous.agentId !== agent.id || previous.lifecycleState !== agent.lifecycleState
|
|
|| previous.status !== agent.status || previous.lifecycleError !== agent.lifecycleError) setupObservations.push(observation);
|
|
await input.evidence("receipt-setup.json", { observations: setupObservations,
|
|
usageCoverage: "Production lifecycle hello probes are outside heartbeat usage; their tokens and cost are unavailable." });
|
|
return agent;
|
|
},
|
|
accept: agent => agent.lifecycleState === "ready" && ["idle", "active"].includes(agent.status),
|
|
reject: agent => agent.lifecycleError ? `Agent setup failed: ${agent.lifecycleError}`
|
|
: agent.lifecycleHolds?.length ? `Agent setup held: ${agent.lifecycleHolds.join(", ")}`
|
|
: ["paused", "rejected", "terminated"].includes(agent.lifecycleState) ? `Agent setup stopped: ${agent.lifecycleState}` : undefined,
|
|
});
|
|
}
|
|
async function refresh() {
|
|
if (!issue || !caseRecord) return;
|
|
const [currentIssue, currentCase, listed, history] = await Promise.all([
|
|
api.get<Row>(`/api/issues/${issue.id}`), api.get<Row>(`/api/cases/${caseRecord.id}`),
|
|
api.get<Row[]>(`${companyPath}/heartbeat-runs?limit=20`),
|
|
api.get<Row>(`/api/cases/${caseRecord.id}/handoffs?limit=20`),
|
|
]);
|
|
issue = currentIssue; caseRecord = currentCase;
|
|
runs = await Promise.all(listed.map(run => api.get<Row>(`/api/heartbeat-runs/${run.id}`)));
|
|
runs.sort((a, b) => String(a.createdAt).localeCompare(String(b.createdAt)) || a.id.localeCompare(b.id));
|
|
handoffs = history.items ?? [];
|
|
input.observe(issue, runs, [], caseRecord);
|
|
}
|
|
async function readDocument(key: string): Promise<Row | null> {
|
|
try { const value = await api.get<Row>(`/api/issues/${issue!.id}/documents/${key}`); return value.document ?? value; }
|
|
catch (error) { if (error instanceof RunnerApiHttpError && error.status === 404) return null; throw error; }
|
|
}
|
|
async function save(stage: string, failure?: unknown) {
|
|
if (!issue || !caseRecord) return [];
|
|
const [tasks, records, interactions] = await Promise.all([
|
|
api.get<Row[]>(`${companyPath}/issues?limit=20&includeAgentCreatedTasks=true`),
|
|
api.get<Row[]>(`${companyPath}/cases?limit=20`), api.get<Row[]>(`/api/issues/${issue.id}/interactions`),
|
|
]);
|
|
const observation: CaseReceiptObservation = {
|
|
nonce, companyId: fixtures.company.id, taskId: issue.id, caseId: caseRecord.id, agents,
|
|
issue, caseRecord, tasks, cases: records, handoffs, runs, receipt, receiptRevisions, review,
|
|
handoffToolRunIds, verifierReadReceipt, builderReadReview, kickoffReplayed,
|
|
initialBrief, triageReadBrief, pendingInteractions: interactions.filter(row => row.status === "pending").length,
|
|
configuredLimits, budgets: budgetEvidence,
|
|
};
|
|
const checks = gradeCaseReceipt(observation);
|
|
input.observe(issue, runs, checks, caseRecord);
|
|
const projectDocument = (value: Row | null): Row | null => value ? {
|
|
id: value.id, issueId: value.issueId, companyId: value.companyId, key: value.key, body: value.body ?? value.latestBody,
|
|
latestRevisionId: value.latestRevisionId, latestRevisionNumber: value.latestRevisionNumber,
|
|
createdByAgentId: value.createdByAgentId, updatedByAgentId: value.updatedByAgentId,
|
|
...(value.revision ? { revision: value.revision } : {}),
|
|
} : null;
|
|
const publicObservation: CaseReceiptObservation = {
|
|
nonce, companyId: fixtures.company.id, taskId: issue.id, caseId: caseRecord.id, agents,
|
|
issue: { id: issue.id, companyId: issue.companyId, status: issue.status, assigneeAgentId: issue.assigneeAgentId },
|
|
caseRecord: { id: caseRecord.id, identifier: caseRecord.identifier, companyId: caseRecord.companyId,
|
|
workIssueId: caseRecord.workIssueId, status: caseRecord.status, phase: caseRecord.phase,
|
|
handoffVersion: caseRecord.handoffVersion, completedAt: caseRecord.completedAt, fields: caseRecord.fields,
|
|
pendingHandoff: caseRecord.pendingHandoff ? { id: caseRecord.pendingHandoff.id, state: caseRecord.pendingHandoff.state } : null },
|
|
cases: records.map(row => ({ id: row.id, companyId: row.companyId, workIssueId: row.workIssueId,
|
|
caseType: row.caseType, status: row.status, phase: row.phase, handoffVersion: row.handoffVersion, fields: { nonce: row.fields?.nonce } })),
|
|
tasks: tasks.map(row => ({ id: row.id, status: row.status })),
|
|
handoffs: handoffs.map(row => ({ id: row.id, companyId: row.companyId, caseId: row.caseId,
|
|
issueId: row.issueId, version: row.version, phase: row.phase, state: row.state, destinationKind: row.destinationKind, fromAgentId: row.fromAgentId,
|
|
toAgentId: row.toAgentId, sourceRunId: row.sourceRunId, expectedIssueStatusVersion: row.expectedIssueStatusVersion,
|
|
appliedIssueStatusVersion: row.appliedIssueStatusVersion, assignmentAppliedAt: row.assignmentAppliedAt,
|
|
deliveredAt: row.deliveredAt, completedAt: row.completedAt, lastErrorCode: row.lastErrorCode })),
|
|
runs: runs.map(row => ({ id: row.id, companyId: row.companyId, agentId: row.agentId, status: row.status,
|
|
runtimeMode: row.runtimeMode, issueId: row.issueId, nativeIssueId: row.nativeIssueId,
|
|
startedAt: row.startedAt, finishedAt: row.finishedAt, errorCode: row.errorCode,
|
|
contextSnapshot: { issueId: row.contextSnapshot?.issueId, taskId: row.contextSnapshot?.taskId,
|
|
paperclipCase: row.contextSnapshot?.paperclipCase ? {
|
|
id: row.contextSnapshot.paperclipCase.id, taskId: row.contextSnapshot.paperclipCase.taskId,
|
|
phase: row.contextSnapshot.paperclipCase.phase, handoffVersion: row.contextSnapshot.paperclipCase.handoffVersion,
|
|
issueStatusVersion: row.contextSnapshot.paperclipCase.issueStatusVersion,
|
|
incomingHandoff: row.contextSnapshot.paperclipCase.incomingHandoff,
|
|
} : undefined } })),
|
|
receipt: projectDocument(receipt), receiptRevisions, review: projectDocument(review),
|
|
handoffToolRunIds, verifierReadReceipt, builderReadReview, kickoffReplayed,
|
|
initialBrief: projectDocument(initialBrief), triageReadBrief, pendingInteractions: observation.pendingInteractions, configuredLimits, budgets: budgetEvidence,
|
|
};
|
|
// Persist synthetic work products and allowlisted facts, never provider prompts/sessions.
|
|
await input.evidence("case-receipt.json", {
|
|
schema: "paperclip.e2e.case-receipt.v1", stage, checks, observation: publicObservation,
|
|
...(failure instanceof Error ? { failure: { message: failure.message.slice(0, 2000) } } : {}),
|
|
});
|
|
return checks;
|
|
}
|
|
try {
|
|
// Daily limits must remain valid for the entire short attempt.
|
|
if (Date.now() + execution.task.attemptTimeoutMs.local >= Date.parse(`${utcDay}T23:59:59.000Z`)) {
|
|
throw new Error("Receipt smoke must not cross the UTC daily-run limit reset; no agents started");
|
|
}
|
|
await api.patch("/api/instance/settings/experimental", { enableCases: true });
|
|
await waitForReady("triage");
|
|
for (const role of ["implementation", "verification"] as const) {
|
|
const original = execution.profile.buildAgent({ environmentId: fixtures.environment.id, environmentFixtureId: "local",
|
|
workspacePath: input.workspacePath, secretRefs: fixtures.secretRefs, executionId: `${nonce}-${role}` });
|
|
const configured = agentConfig(role, execution.profile.credential === "CODEX_AUTH_JSON"
|
|
? codexSubscriptionAgentConfig(original, process.env, fixtures.company.id) : original);
|
|
const agent = await api.post<Row>(`${companyPath}/agents`, { ...configured, name: `Receipt ${role} ${nonce}`, role: "engineer" });
|
|
agents[role] = agent.id;
|
|
await waitForReady(role);
|
|
}
|
|
for (const role of roles) {
|
|
const detail = await api.get<Row>(`/api/agents/${agents[role]}/instructions-bundle/file?path=AGENTS.md`);
|
|
if (typeof detail.content !== "string" || !detail.content.trim()) throw new Error("Missing production agent instructions");
|
|
const appendix = caseReceiptAgentPrompt(role, agents, nonce);
|
|
const appendedInstructions = `${detail.content}\n\n## Receipt smoke role\n\n${appendix}\n`;
|
|
await api.saveAgentInstructions(agents[role], appendedInstructions);
|
|
const savedInstructions = await api.get<Row>(`/api/agents/${agents[role]}/instructions-bundle/file?path=AGENTS.md`);
|
|
if (savedInstructions.content !== appendedInstructions) throw new Error("Receipt role instructions were not preserved before provider admission");
|
|
const persisted = await api.get<Row>(`/api/agents/${agents[role]}`);
|
|
configuredAgents.push({ id: persisted.id, maxDailyRuns: persisted.runtimeConfig?.heartbeat?.maxDailyRuns,
|
|
maxConcurrentRuns: persisted.runtimeConfig?.heartbeat?.maxConcurrentRuns, heartbeatEnabled: persisted.runtimeConfig?.heartbeat?.enabled,
|
|
timeoutSec: persisted.adapterConfig?.timeoutSec, modelReasoningEffort: persisted.adapterConfig?.modelReasoningEffort });
|
|
await input.evidence(`receipt-${role}-instructions.json`, { agentId: persisted.id,
|
|
productionInstructionsSha256: createHash("sha256").update(detail.content).digest("hex"), appendixBytes: Buffer.byteLength(appendix) });
|
|
await waitForReady(role);
|
|
}
|
|
const [company, ...persistedAgents] = await Promise.all([
|
|
api.get<Row>(companyPath), ...roles.map(role => api.get<Row>(`/api/agents/${agents[role]}`)),
|
|
]);
|
|
budgetEvidence = { company: { budgetMonthlyCents: company.budgetMonthlyCents },
|
|
agents: persistedAgents.map(agent => ({ id: agent.id, budgetMonthlyCents: agent.budgetMonthlyCents })) };
|
|
if (company.budgetMonthlyCents !== CASE_RECEIPT_BUDGET_CENTS
|
|
|| persistedAgents.some(agent => agent.budgetMonthlyCents !== CASE_RECEIPT_BUDGET_CENTS)) {
|
|
throw new Error("Receipt smoke budget readback failed before provider admission");
|
|
}
|
|
for (const role of roles) {
|
|
const bounds = configuredAgents.find(agent => agent.id === agents[role]);
|
|
if (!bounds || bounds.maxDailyRuns !== (role === "implementation" ? 2 : 1)
|
|
|| bounds.maxConcurrentRuns !== 1 || bounds.heartbeatEnabled !== false
|
|
|| bounds.timeoutSec !== CASE_RECEIPT_RUN_TIMEOUT_MS / 1000 || bounds.modelReasoningEffort !== "low") {
|
|
throw new Error(`Receipt ${role} limits readback failed before provider admission`);
|
|
}
|
|
}
|
|
const project = await api.post<Row>(`${companyPath}/projects`, { name: `Receipts ${nonce}` });
|
|
for (const [index, status] of ["draft", "done"].entries()) await api.post(`${companyPath}/cases`, {
|
|
caseType: "receipt-research", key: `passive-${nonce}-${index}`, title: `Receipt notes ${index + 1} ${nonce}`,
|
|
summary: "Passive filter fixture; no work requested.", status, fields: { nonce },
|
|
});
|
|
const kickoff = { idempotencyKey: `receipt-${nonce}`,
|
|
case: { create: { caseType: "receipt-smoke", title: execution.task.buildTitle(nonce), projectId: project.id,
|
|
summary: "Produce a receipt, have it independently reviewed, apply the requested currency correction, and finish the same task. Keep findings in ordinary primary-task documents.",
|
|
fields: { nonce, items: [{ quantity: 2, unitPrice: 3 }], currency: "USD" } } },
|
|
workflow_document_text: `Receipt workflow ${nonce}. Reuse this primary task throughout.
|
|
Triage (${agents.triage}) reads brief, validates all receipt inputs and saves inputsValidated, then hands off to implementation.
|
|
Implementation (${agents.implementation}) computes the total and writes receipt with total only; omit currency in the first revision so the independent reviewer can detect the missing field. Hand off to verification.
|
|
Verification (${agents.verification}) reads the saved receipt, independently recomputes its total and checks currency. Write review with totalCorrect and missing, then return the same task to implementation in phase revision.
|
|
Revision (${agents.implementation}) reads review and receipt, preserves the verified total, adds currency from case inputs and saves the next receipt revision. Hand off to done and complete the same task normally.
|
|
Use observed case/task versions, persist findings before each handoff, and obey existing completion gates. Exactly four runs; no additional tasks or cases.`,
|
|
initialDocuments: [{ key: "brief", title: "Receipt brief", body: `Receipt ${nonce}: total and currency are required.` }],
|
|
toAgentId: agents.triage, phase: "triage", instructions: `${nonce}: Read the primary task document workflow, inspect the saved receipt inputs and hand this same task to the builder, following your receipt role instructions.` };
|
|
const started = await api.post<Row>(`${companyPath}/case-handoffs`, kickoff);
|
|
// Set observable identities immediately so partial failures still retain their provenance.
|
|
issue = { id: started.taskId }; caseRecord = { id: started.caseId };
|
|
await refresh();
|
|
const replay = await api.post<Row>(`${companyPath}/case-handoffs`, kickoff);
|
|
kickoffReplayed = replay.replayed === true && replay.handoffId === started.handoffId
|
|
&& replay.taskId === started.taskId && replay.caseId === started.caseId;
|
|
if (!kickoffReplayed) throw new Error("Receipt kickoff replay did not return the original handoff/task/case");
|
|
await save("accepted");
|
|
await page.goto(`/${fixtures.company.issuePrefix}/cases/${caseRecord!.identifier}`);
|
|
await expect(page.getByRole("heading", { name: execution.task.buildTitle(nonce), exact: true })).toBeVisible();
|
|
await input.capture("case-kickoff", "Receipt case with one primary task", "case-kickoff.png");
|
|
await pollUntil({ label: "four-turn receipt smoke", deadlineAt: input.deadlineAt, intervalMs: 1000,
|
|
load: async () => { await refresh(); return { issue, runs, handoffs }; },
|
|
accept: state => state.issue?.status === "done" && state.runs.length === CASE_RECEIPT_MAX_TURNS
|
|
&& state.runs.every(run => run.status === "succeeded") && state.handoffs.length === 5
|
|
&& caseRecord?.status === "done"
|
|
&& state.handoffs.every(handoff => handoff.destinationKind === "done" ? handoff.state === "completed" : handoff.state === "delivered"),
|
|
reject: state => state.runs.length > CASE_RECEIPT_MAX_TURNS ? "Receipt smoke provider execution cap exceeded"
|
|
: caseHandoffRunFailure(state.runs) ?? (state.handoffs.some(handoff => handoff.state === "cancelled") ? "Receipt handoff cancelled"
|
|
: state.issue?.status === "blocked" ? "Receipt task became blocked; inspect retained task/run evidence" : undefined),
|
|
});
|
|
[receipt, review, initialBrief] = await Promise.all([readDocument("receipt"), readDocument("review"), readDocument("brief")]);
|
|
const [receiptHistory, reviewHistory] = await Promise.all([
|
|
api.get<Row>(`/api/issues/${issue!.id}/documents/receipt/revisions`),
|
|
api.get<Row>(`/api/issues/${issue!.id}/documents/review/revisions`),
|
|
]);
|
|
receiptRevisions = Array.isArray(receiptHistory) ? receiptHistory : receiptHistory.revisions ?? [];
|
|
if (review) review = { ...review, revision: (Array.isArray(reviewHistory) ? reviewHistory : reviewHistory.revisions)?.[0] };
|
|
for (const run of runs) {
|
|
const events = await collectRunEvents<Row>((afterSeq, limit) => api.get(`/api/heartbeat-runs/${run.id}/events?afterSeq=${afterSeq}&limit=${limit}`));
|
|
if (hasNativeHandoffReceipt(events)) handoffToolRunIds.push(run.id);
|
|
if (run.agentId === agents.triage) triageReadBrief ||= hasTaskDocumentRead(events, issue!.id, "brief");
|
|
if (run.agentId === agents.verification) verifierReadReceipt ||= hasTaskDocumentRead(events, issue!.id, "receipt", true);
|
|
if (run.agentId === agents.implementation && run.contextSnapshot?.paperclipCase?.phase === "revision") {
|
|
builderReadReview ||= hasTaskDocumentRead(events, issue!.id, "review", true);
|
|
}
|
|
}
|
|
const checks = await save("completed");
|
|
const failed = checks.filter(check => !check.passed);
|
|
if (!checks.length || failed.length) throw new Error(`Receipt smoke oracle failed: ${failed.map(check => check.id).join(", ") || "missing evidence"}`);
|
|
await verifyReceiptBrowser(input, caseRecord!, issue!, receipt!);
|
|
await save("browser-verified");
|
|
return { issue: issue!, runs, checks };
|
|
} catch (error) {
|
|
await refresh().catch(() => undefined);
|
|
if (caseRecord?.id) {
|
|
if (!receipt) receipt = await readDocument("receipt").catch(() => null);
|
|
if (!review) review = await readDocument("review").catch(() => null);
|
|
await save("failed", error).catch(() => undefined);
|
|
}
|
|
throw error;
|
|
} finally {
|
|
// Retire only this disposable fixture's agents, including partial runs.
|
|
for (const id of Object.values(agents).filter(Boolean)) await api.post(`/api/agents/${id}/pause`, {}).catch(() => undefined);
|
|
}
|
|
}
|
|
|
|
export async function verifyReceiptBrowser(input: Pick<Input, "page" | "fixtures" | "execution" | "nonce" | "capture">, workflowCase: Row, task: Row, receipt: Row) {
|
|
const { page, fixtures, nonce, execution } = input;
|
|
const prefix = fixtures.company.issuePrefix!;
|
|
await page.goto(`/${prefix}/cases`);
|
|
const cases = () => page.getByRole("region", { name: "Case list" });
|
|
await expect(cases().getByRole("link")).toHaveCount(3);
|
|
await page.getByRole("button", { name: /^Filter cases/ }).click();
|
|
await page.getByLabel("Filter by project", { exact: true }).click();
|
|
await page.getByRole("option", { name: "No project", exact: true }).click();
|
|
await expect(page.getByRole("button", { name: "Filter by project", exact: true })).toHaveText("No project");
|
|
await page.keyboard.press("Escape");
|
|
await expect(cases().getByRole("link")).toHaveCount(2);
|
|
await page.getByRole("button", { name: "Clear filters", exact: true }).click();
|
|
await page.getByRole("button", { name: /^Filter cases/ }).click();
|
|
await page.getByLabel("Filter by type", { exact: true }).click();
|
|
await page.getByRole("option", { name: "receipt-smoke", exact: true }).click();
|
|
await expect(page.getByRole("button", { name: "Filter by type", exact: true })).toHaveText("receipt-smoke");
|
|
await page.keyboard.press("Escape");
|
|
await expect.poll(() => new URL(page.url()).searchParams.get("type")).toBe("receipt-smoke");
|
|
await expect(cases().getByRole("link")).toHaveCount(1);
|
|
await page.getByRole("textbox", { name: "Search cases" }).fill(nonce);
|
|
await expect(cases().getByRole("link")).toHaveCount(1);
|
|
await cases().getByRole("link", { name: new RegExp(execution.task.buildTitle(nonce)) }).click();
|
|
await expect(page.getByRole("heading", { name: execution.task.buildTitle(nonce), exact: true })).toBeVisible();
|
|
const work = page.getByRole("region", { name: "Case work", exact: true });
|
|
await expect(work.getByText("done", { exact: true })).toBeVisible();
|
|
await expect(work.getByRole("link")).toHaveAttribute("href", `/${prefix}/issues/${task.identifier}`);
|
|
const history = page.getByRole("region", { name: "Case handoffs" });
|
|
await expect(history.locator(":scope > ol > li")).toHaveCount(5);
|
|
await expect(history.locator("summary").filter({ hasText: /^Done/ })).toHaveCount(1);
|
|
await history.locator("summary").filter({ has: page.getByText("verification", { exact: true }) }).click();
|
|
await expect(history).toContainText("currency");
|
|
await input.capture("case-final", "Same task after the reviewer requested a correction", "case-final.png");
|
|
const receiptTitle = typeof receipt.title === "string" && receipt.title ? receipt.title : "receipt";
|
|
const receiptButton = page.getByRole("button", { name: receiptTitle, exact: true });
|
|
await receiptButton.click();
|
|
const preview = page.getByRole("dialog", { name: receiptTitle, exact: true });
|
|
await expect(preview).toContainText(/"currency"\s*:\s*"USD"/);
|
|
await preview.getByText("Revision history", { exact: true }).click();
|
|
await expect(preview.getByRole("button", { name: /^rev 1/ })).toBeVisible();
|
|
await expect(preview.getByRole("button", { name: /^rev 2/ })).toBeVisible();
|
|
await preview.getByRole("button", { name: /^rev 1/ }).click();
|
|
await input.capture("case-document", "Real receipt document and its two revisions", "case-document.png");
|
|
await page.keyboard.press("Escape");
|
|
await expect(receiptButton).toBeFocused();
|
|
await expect(page.getByTestId("case-pages").locator("input, textarea, [contenteditable=true]")).toHaveCount(0);
|
|
await page.getByRole("navigation", { name: "breadcrumb" }).getByRole("link", { name: "Cases", exact: true }).click();
|
|
await expect(page.getByRole("textbox", { name: "Search cases" })).toHaveValue(nonce);
|
|
await page.reload();
|
|
await expect(page.getByRole("textbox", { name: "Search cases" })).toHaveValue(nonce);
|
|
await expect(cases().getByRole("link")).toHaveCount(1);
|
|
await page.goto(`/${prefix}/cases/${workflowCase.identifier}`);
|
|
await expect(page.getByRole("region", { name: "Case work", exact: true })).toContainText("done");
|
|
await page.goto(`/${prefix}/issues/${task.identifier}`);
|
|
await expect(page.getByTestId("issue-detail-header").getByRole("button", { name: "Change status (current: Done)", exact: true })).toBeVisible();
|
|
await expect(page.getByTestId("issue-assignee-paused-notice")).toHaveCount(0);
|
|
await input.capture("task-final", "Original task completed by the returning builder", "task-final.png");
|
|
}
|