Files
PaperClipAI/server
DottaandClaude Opus 5.5 496efd201f fix(native-runtime): recover external-chat task replies skipped by copy-back failure (#22807)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - A native agent run ends with an accepted final reply. Paperclip
posts that reply on the task.
> - The live completion path posts the reply. If a post-result step
fails (for example, workspace copy-back), a background presentation
worker recovers and posts the accepted reply later.
> - The recovery path did not accept a task whose run was woken by an
external chat message, such as a GitHub review comment. The worker found
the run, recovery refused it, and after 6 attempts the reply was marked
failed and lost.
> - The live path posts these replies normally, so the recovery rule was
stricter than the live rule for no reason.
> - This pull request lets recovery post a `done` or `needs_review`
reply for a non-conversation task that an external chat woke, with the
same authorization and source rules as the live path.
> - The benefit is that the agent's real answer reaches the task even
when copy-back fails after the result is accepted.

## Linked Issues or Issue Description

Refs #22798

**What happened?**
On a hosted instance, some native runs finished `succeeded` with a
committed result, but their final reply never appeared on the task. The
run's `responsePublication` ended with `state: "failed"` and
`lastReason: "publication_refused"` after 6 attempts. Every affected run
had `source: "chat:github"`, a `done` result, and a
`native.post_result_failure` event with `retryReasonCode:
"workspace_sync_out_failed"`. Runs on the same task that did not hit the
copy-back failure posted their reply on the live path.

**Expected behavior**
When the live path is skipped after the result is accepted, recovery
posts the accepted final reply once, as the live path would.

**Steps to reproduce**
1. Wake a native agent on an ordinary (non-conversation) task from an
external chat message, so the run context has `source: "chat:..."`.
2. Let the agent report `done` with a final agent message.
3. Make workspace copy-back fail after the result is accepted, so the
live completion path does not post the reply.
4. The presentation worker finds the run, but
`recoverCommittedNativeChatResponse` returns `false`. The worker records
`publication_refused` until it gives up.

**Additional context**
In `recoverCommittedNativeChatResponseInScope`, `ordinaryTaskReply`
required `!isExternalChatPresentationContext(...)`. An external-chat
`done` reply then fell through to the `response_wake` branch, which
returns `false` for anything other than `yielded`. The candidate query
in `native-response-presentation-query.ts` has no such exclusion, so the
worker kept selecting a run that recovery always refused.

## What Changed

- `native-run-finalizer.ts`: recovery now treats a `done` or
`needs_review` reply on a non-conversation task that an external chat
woke as an ordinary task reply.
- That reply uses the authorization that
`resolveChatRunPresentationAuthorizationReason` resolves, as the live
path does. GitHub-origin replies resolve to `internal_agent_write`, so
they stay a local task comment. Other chat providers keep their normal
provider-visible reply.
- That reply prefers the final agent message over an existing run
comment, as the live path does for external chat
(`preferFinalResponseOverExistingComment`).
- Conversation tasks keep their current rules. Blocked replies, governed
review replies, and `response_wake` replies are unchanged.
- `agent-chat-response-recovery.test.ts`: new red/green regression cases
for `chat:github` + `done`, `chat:github` + `needs_review`, and
`externalChatContinuation: true` + `done`. Each case checks that the
reply posts once, the run records `committed_task_response_recovered`,
and a second recovery does not post again.

## Verification

- Red: before the fix, all 3 new cases failed. `work.check` returned a
retry entry (the `publication_refused` path), the same outcome as on the
hosted instance.
- Green: `npx vitest run
src/services/native-runtime/agent-chat-response-recovery.test.ts` passes
67/67.
- `npx tsc --noEmit -p server` is clean.
- `pnpm verify` selected 2374 of 2737 test files (the finalizer is
widely imported) and did not finish in 25 minutes locally. The 2
failures it reached are unrelated load-timing tests. Required CI
supplies the broad coverage.
- After deploy: confirm on staging that a new external-chat task run
with a copy-back failure posts its reply, and that no new
`issue.response_publication_failed` activity appears for this pattern.

## Risks

- Low risk. The change only widens recovery for non-conversation tasks
woken by external chat, and only for `done` and `needs_review` replies
that the candidate query already selects.
- For a non-GitHub chat provider, the recovered reply goes to the
provider, as it does on the live path. Recovery runs only when the run
has no recorded presentation decision or comment, so it does not post a
second reply.
- Runs that already ended as `responsePublication.state: "failed"`
before this change stay failed. This change does not backfill them.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), via Claude Code with tool use
(shell, file edits, browser reads of the staging API).

## 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: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 14:21:17 -06:00
..