Files
PaperClipAI/packages
d939c64a6b fix(opencode): keep the text-first answer when only the completion call is written as text (#23806)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The Paperclip runner drives OpenCode models and asks again when a
turn ends without a final answer
> - The second request asks for a plain-text answer first, then the
`paperclip_finish` call
> - DeepSeek v4.1 flash sometimes sends that answer and then writes
`paperclip_finish` as DSML text, not as a real tool call
> - The runner discards the full reply when it finds tool-call text, so
it also discards the answer it asked for, and the turn fails with
`opencode_empty_response`
> - This pull request keeps that answer when the only text call after it
is the completion call
> - The benefit is that a turn that already did its work ends with an
answer, not a failed run and a full retry

## Linked Issues or Issue Description

Refs #21361
Refs #21761

**What happened?**
After real tool work, a reviewer agent on DeepSeek v4.1 flash
(OpenRouter) ended its turn with a tool call written as text. The runner
sent two corrections. For the second correction, the model wrote a
correct plain-text answer, a blank line, and then `paperclip_finish` as
DSML text. The runner discarded the full reply and failed the turn with
`opencode_empty_response` ("after two corrections"). The work was
already done. In one run, the model had published a PR review. In a
different run, it had posted a GitHub reply. The automatic retry then
ran the full session again. This occurred three times on staging in
three weeks, and two of the three were on the same day.

**Expected behavior**
The second correction asks for exactly this shape: an answer, then the
completion call. When the completion call is the only text call, the
runner should keep the answer. A completion call starts no work, so the
answer cannot describe an action that did not occur.

**Steps to reproduce**
1. Start an OpenCode turn that does tool work and then sends an empty
reply two times.
2. For the text-first correction, send `Here is the actual final
answer.` followed by a DSML `paperclip_paperclip_finish` call as text.
3. Before this change, the turn fails with `opencode_empty_response`.
The new `tool-work-text-finish-markup` case in `empty-response.test.ts`
does these steps.

**Paperclip version or commit**
`master` at bf49fce70f.

## What Changed

- `tool-call-markup.ts`: add `toolCallMarkupNames()`. It finds each call
that the text opens: a DSML invoke, a `<function_calls>` invoke, a
`<tool_call>` object, or a DeepSeek `tool▁call▁begin` token. It returns
the name of each call. When it cannot read the name of one call, it
returns `null`. Markup that opens no call (for example, closing tags
only) gives an empty list.
- `opencode-server-driver.ts`: the runner keeps the prose before the
markup as the final answer only when all of these are true:
  - the reply is for the second (text-first) correction of the same turn
  - there is prose before the markup
- the runner can read the name of each call in the markup, and each name
is `paperclip_finish` or `paperclip_block` after the usual OpenCode MCP
prefix is removed

The kept answer stops at the first model control token or markup. If no
prose remains, the runner does not keep the reply.
  
The runner records a new `opencode_textual_completion_call_discarded`
diagnostic. It does not use the text call. In all other cases, the reply
is discarded as before.
- `tool-call-markup.test.ts`: add unit tests for
`toolCallMarkupNames()`. The tests include readable forms, closing tags
only, and forms with an unreadable call. One form has a DeepSeek call
token before a DSML finish call.
- `empty-response.test.ts`: add five cases:
- `tool-work-text-finish-markup`: the turn completes and the final
answer is the prose.
- `tool-work-text-work-markup`: the same reply shape with a text
`paperclip_call_assigned_tool` call. The turn still fails after two
corrections.
- `finish-markup-first-reply`: prose and a text finish call in an
ordinary reply. The runner still discards the reply, and the prose is
not used as the answer.
- `tool-work-text-mixed-markup`: prose, an unreadable DeepSeek
call-token work call, and then a DSML finish call. The turn still fails
after two corrections.
- `tool-work-text-finish-markup-control`: prose, `<|end▁of▁sentence|>`,
and then a DSML finish call. The final answer is the prose without the
token.

## Verification

- `npx vitest run src/drivers/opencode/empty-response.test.ts
src/drivers/opencode/tool-call-markup.test.ts` (in
`packages/paperclip-runner`): 83/83 pass. With the driver change
removed, `tool-work-text-finish-markup` fails with `turn.failed`. This
is the same failure as on staging. With the first commit's
implementation, the review-finding cases (`tool-work-text-mixed-markup`,
`tool-work-text-finish-markup-control`, and the four `null` unit cases)
fail.
- `npx vitest run src/drivers/opencode/`: the result is the same as on
unchanged `master` in this sandbox. Nine tests fail in
`opencode-server-driver.test.ts` and `output-custody.test.ts`, both with
and without this change. All other tests pass.
- `tsc -p tsconfig.json --noEmit`: no errors in the changed files. The
only errors come from an unbuilt eval-kernel workspace package in this
sandbox.

## Risks

- Low risk. The change applies only to the last correction, which fails
the turn today. It accepts only prose followed by completion calls. A
text work call, prose in an ordinary reply, and markup with no readable
names keep the current discard behavior.
- The text completion call is not run. The turn ends with a final answer
and no completion report. The runner already handles this path for
models that answer without `paperclip_finish`.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used as a coding agent
with tool use and shell 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-12-04 17:38:02 -08:00
..