Files
PaperClipAI/server/src/services/native-runtime
92271da899 fix(native): keep a chat turn's message batch when a fresh-session retry continues it (#22413)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - External chat turns (GitHub, Slack, Discord) wake an agent on a task
with an admitted message batch.
> - When a task is in review, dispatch checks that the run is bound to
that reviewed chat batch. A run without a batch fails with
`reviewed_chat_execution_binding_not_authorized` (`context_mismatch`).
> - Automatic fresh-session retries (tool-receipt limit, provider
capacity, environment blocker, agent blocker, and safe replacement)
remove all consumed wake input from the failed run's context. This also
removes the chat message IDs, but the context keeps `source:
chat:<provider>`.
> - Thus every automatic retry of a reviewed chat turn fails at setup.
The one-shot review-path recovery that follows copies the same empty
context and fails the same way.
> - This pull request keeps the admitted chat message batch on those
retries and removes the failed run's dispatch proofs, so dispatch checks
ownership and current access again.
> - The benefit is that a reviewed chat turn can continue after a
transient failure, and access checks still apply.

## Linked Issues or Issue Description

Refs #13818 (the same approach for review-path recovery). Related:
#20689, #20697, #20842 (other causes of the same denial).

**What happened?**
A GitHub PR review turn on a task in review stopped on a transient
failure (tool-receipt limit or sandbox environment blocker). Paperclip
scheduled an automatic fresh-session retry. The retry failed at setup in
less than one second with
`reviewed_chat_execution_binding_not_authorized` and denial reason
`context_mismatch`. The review-path recovery wake then failed with the
same error. On staging this occurred 5 times on 3 tasks after #20689,
#20697 and #20842 were deployed.

**Expected behavior**
An automatic retry of a chat turn keeps the admitted message batch.
Dispatch then checks the retry against current task ownership and
current endpoint, conversation and principal access. If access is still
valid, the retry runs. If access was revoked, dispatch refuses the
retry.

**Steps to reproduce**
1. Put a task in review with a run-bound external chat turn (for example
`source: chat:github` with `wakeCommentIds`).
2. Fail that native run with a fresh-session retry lane (for example
`native_environment_blocker`).
3. Let dispatch claim the scheduled retry. The retry context has
`source: chat:github` but no `wakeCommentIds`.
4. Dispatch refuses the retry with `context_mismatch`.

**Paperclip version or commit**
`master` at 414abc2dbf.

## What Changed

- Add
`server/src/services/native-runtime/fresh-session-retry-context.ts`. It
holds the one list of consumed wake input keys and builds the retry
context. For a run-bound external chat turn, it keeps the admitted
message IDs (`wakeCommentIds`, `wakeCommentId`, `commentId`). It removes
the dispatch proofs that the failed run got
(`paperclipHarnessCheckedOut`, `paperclipExternalChatExecutionBound`).
Other runs keep the existing behavior.
- Use the helper in `materializeNativeFreshSessionRetry` (all its lanes)
and in `reconcileSafeNativeReplacements`. Before, each producer had its
own copy of the key list.
- Add a unit test for the helper.
- Add a regression test in
`heartbeat-reviewed-chat-binding.integration.test.ts` for Slack and
Discord. It checks that the retry passes the reviewed chat check, that
revoked access still refuses the retry, and that review-path recovery
keeps the batch.

## Verification

- `cd server && npx vitest run
src/__tests__/heartbeat-reviewed-chat-binding.integration.test.ts
src/services/native-runtime/fresh-session-retry-context.test.ts
src/services/native-runtime/native-safe-replacement.test.ts
src/services/native-runtime/native-agent-blocker.integration.test.ts
src/services/native-runtime/native-tool-receipt-limit.integration.test.ts`
passes: 5 files, 127 tests.
- Red check: with the `master` versions of
`native-provider-capacity-retry.ts` and `native-safe-replacement.ts`,
the new regression test fails (2 failed, 34 passed). The retry is
refused with `context_mismatch`.
- CI runs the full typecheck. The local full server typecheck stopped
because the sandbox ran out of memory.

## Risks

- Low risk. The change applies only to automatic fresh-session retries
whose context has a run-bound external chat source and admitted message
IDs.
- The retry does not get the failed run's authority. Dispatch must check
ownership and access again, and the regression test shows that a revoked
principal is still refused.
- Other retries keep the same context as before, because both producers
now use the same key list.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with tool use and
code execution.

## 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

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: paperclipbugbot <356415228+paperclipbugbot@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 06:47:34 -08:00
..
…