diff --git a/packages/shared/src/issue-write-denial.test.ts b/packages/shared/src/issue-write-denial.test.ts index e21b93aa79e..1d57188ce2a 100644 --- a/packages/shared/src/issue-write-denial.test.ts +++ b/packages/shared/src/issue-write-denial.test.ts @@ -83,6 +83,19 @@ describe("describeIssueWriteDenial", () => { const copy = describeIssueWriteDenial("cross_issue_influence_run_context_required"); expect(copy.sanctionedPath).toContain("X-Paperclip-Run-Id"); expect(copy.sanctionedPath).toContain("PAPERCLIP_RUN_ID"); + expect(copy.sanctionedPath).toContain("/api/heartbeat-runs/$PAPERCLIP_RUN_ID"); + }); + + it("routes an unverifiable run to the canonical run endpoint", () => { + const copy = describeIssueWriteDenial("issue_write_run_context_unavailable"); + expect(copy.status).toBe(403); + expect(copy.sanctionedPath).toContain("/api/heartbeat-runs/$PAPERCLIP_RUN_ID"); + expect(copy.sanctionedPath).toContain("Stop affected issue writes"); + expect(copy.sanctionedPath).toContain("registered in this company"); + expect(copy.sanctionedPath).toContain("owned by"); + expect(copy.sanctionedPath).toContain("403 or 404"); + expect(copy.sanctionedPath).toContain("does not prove missing registration"); + expect(copy.title).not.toContain("Cross-issue"); }); it("tells a spoof attempt that the write itself was fine", () => { diff --git a/packages/shared/src/issue-write-denial.ts b/packages/shared/src/issue-write-denial.ts index e6b4d72a83f..5946161957a 100644 --- a/packages/shared/src/issue-write-denial.ts +++ b/packages/shared/src/issue-write-denial.ts @@ -33,6 +33,7 @@ export const ISSUE_WRITE_DENIAL_CODES = [ "issue_status_change_live_run", "cross_issue_influence_cap_exceeded", "cross_issue_influence_run_context_required", + "issue_write_run_context_unavailable", "issue_write_attribution_spoof_rejected", ] as const; @@ -271,7 +272,7 @@ export function describeIssueWriteDenial( status: 403, tone: "boundary", boundary: "Heartbeat run context", - title: "Cross-issue writes need a run to attribute them to", + title: "Issue writes need a run to attribute them to", description: `Every agent comment and task update is attributed to a heartbeat run so the ` + `cross-issue cap can be counted and the audit trail can name who acted for whom. ` + @@ -279,10 +280,29 @@ export function describeIssueWriteDenial( whoCanAct: `${actor}, once the request carries its own run id.`, sanctionedPath: `Send the \`X-Paperclip-Run-Id\` header with your current run (\`$PAPERCLIP_RUN_ID\`) ` + - `and retry.`, + `and retry once. If the header was already present, check \`GET /api/heartbeat-runs/$PAPERCLIP_RUN_ID\` and report the denial.`, }; + case "issue_write_run_context_unavailable": + return { + code, + status: 403, + tone: "boundary", + boundary: "Heartbeat run attribution", + title: "The issue write could not be attributed to this run", + description: + `The supplied run id did not identify a registered heartbeat run owned by ${actor} ` + + `in this company. This issue write cannot proceed without that attribution.`, + whoCanAct: `${actor}, after its heartbeat run is available to the control plane.`, + sanctionedPath: + `Check \`GET /api/heartbeat-runs/$PAPERCLIP_RUN_ID\` with the current run credential. ` + + `Stop affected issue writes and report this denial and the lookup result. The supplied run ` + + `must be registered in this company and owned by ${actor}; a 403 or 404 from the lookup ` + + `alone does not prove missing registration. Do not create a different run id or retry ` + + `this write under another identity.`, + }; + case "issue_write_attribution_spoof_rejected": return { code, diff --git a/server/src/__tests__/cross-issue-influence-limit.test.ts b/server/src/__tests__/cross-issue-influence-limit.test.ts index c4fff2fd10a..151db09f9ff 100644 --- a/server/src/__tests__/cross-issue-influence-limit.test.ts +++ b/server/src/__tests__/cross-issue-influence-limit.test.ts @@ -177,7 +177,7 @@ describe("cross-issue influence limit rollout", () => { kind: "comment", })).rejects.toMatchObject({ status: 403, - details: { code: "cross_issue_influence_run_context_required" }, + details: { code: "issue_write_run_context_unavailable" }, }); expect(fake.inserted).toEqual([]); }); diff --git a/server/src/services/cross-issue-influence-limit.ts b/server/src/services/cross-issue-influence-limit.ts index e1ec2c00244..5ac3cde6de0 100644 --- a/server/src/services/cross-issue-influence-limit.ts +++ b/server/src/services/cross-issue-influence-limit.ts @@ -34,6 +34,11 @@ export function crossIssueInfluenceRunContextError() { return forbidden(body.error, body.details); } +function issueWriteRunContextUnavailableError() { + const { body } = issueWriteDenialResponse("issue_write_run_context_unavailable"); + return forbidden(body.error, body.details); +} + function readRunSourceIssueId(contextSnapshot: unknown) { if (!contextSnapshot || typeof contextSnapshot !== "object" || Array.isArray(contextSnapshot)) return null; const context = contextSnapshot as Record; @@ -106,7 +111,7 @@ export async function observeCrossIssueInfluence( run.companyId !== input.companyId || run.agentId !== input.agentId ) { - throw crossIssueInfluenceRunContextError(); + throw issueWriteRunContextUnavailableError(); } // An unscoped run (timer heartbeat with no checked-out issue) has no source