Clarify unavailable run context for issue writes (#19788)

## Thinking Path

> - Paperclip manages AI agents and their work.
> - The issue API attributes agent writes to a heartbeat run.
> - A valid run with no source issue and an unavailable run are
different denial cases.
> - The old response used one code and could tell an agent to resend a
header that was already present.
> - This pull request gives an unavailable or mismatched run its own
denial code.
> - The response tells the agent to inspect the run and stop affected
writes.
> - The benefit is a clear audit boundary and a useful recovery
instruction.

## Linked Issues or Issue Description

Refs #18450. Related PRs #18743 and #18275 address run context copy and
checkout binding.

**What happened?**

An agent issue write with a supplied run ID can fail when the run row is
absent, in another company, or owned by another agent. The API returned
the same denial as a run without a source issue. The response could tell
the agent to resend a header it already sent.

**Expected behavior**

The API returns a distinct 403 denial for an unavailable or mismatched
run. The response tells the agent to check the run lookup, stop affected
writes, and report the result. A run with no source issue keeps its
separate denial.

**Steps to reproduce**

1. Send an agent issue write with a run ID that has no matching run row
for that agent and company.
2. Observe the denial code and response text before and after this
change.
3. Send a write from a registered run with no source issue and confirm
that it keeps the separate run-context-required code.

**Paperclip version or commit**

Base behavior: `a708a133363a73bd6ad0491cf38dbddfd54c180e`. This PR is
based on a newer `master` commit with unchanged base blobs in the four
affected files.

**Deployment mode**

Agent issue writes in local and self-hosted deployments. No live
installation was tested.

## What Changed

- Add `issue_write_run_context_unavailable` to the shared denial
contract.
- Return that code when the supplied run is absent, belongs to another
company, or belongs to another agent.
- Clarify the separate denial text for a run that has no source issue.
- Add focused shared and server regression tests.

## Verification

- The reviewed four-file patch has SHA256
`d90722aef44d211d57a728e40d435ea4d2b5b15fa9271d9aceab6f254a18e4c7` as a
binary Git diff against its original base.
- Each changed blob in this PR matches the reviewed local commit byte
for byte. The newer base has the same four pre-change blobs.
- `pnpm --filter @paperclipai/shared exec vitest run
src/issue-write-denial.test.ts` passed locally.
- `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/cross-issue-influence-limit.test.ts` passed locally.
- `pnpm --filter @paperclipai/shared typecheck` passed locally.
- `git diff --check` passed. Node 22.23.1 emitted an engine warning
because the repository requests Node 24.11.0 or newer.
- Full CI and Greptile review are pending. No live runtime verification
was performed.

## Risks

- Clients that branch on denial codes will see a new code for
unavailable or mismatched runs.
- The status remains 403. This change does not grant issue write scope
or alter the cross-issue limit.
- A run lookup may return 403 or 404 for reasons other than missing
registration. The response does not treat that result as proof that the
run is absent.
- Rollback: revert this commit. No database migration or configuration
change is required.

## Model Used

- OpenAI GPT-6 Codex assisted with source acceptance and PR preparation.
The exact serving model ID and context window were not exposed in this
run. Reasoning, shell, and GitHub tools were used.
- The reviewed source candidate was AI assisted. Its original model ID
was not recorded with the review 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
- [ ] All Paperclip CI gates are green
- [ ] 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>
This commit is contained in:
brendanmatthewjones-cmykandPaperclip authored and GitHub committed 2026-12-05 03:45:03 -08:00
1 parent eb7546d913
commit f78519ebb8
4 files changed
+42 -4

No files matched your search

@@ -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", () => {
+22 -2
View File
@@ -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,
@@ -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([]);
});
@@ -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<string, unknown>;
@@ -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