Files
PaperClipAI/packages
Nicky LeachandPaperclip eb31b926a1 fix(runner): keep the OpenCode session event stream open across turns (#14582)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Paperclip Runner keeps provider sessions and their event streams for
agent runs
> - The OpenCode driver closed its event queue after each terminal turn
> - A second turn on the same session then lost its response and
completion events
> - This pull request keeps the queue open between turns and rejects
late events for sealed turns
> - The benefit is reliable multi-turn OpenCode sessions with visible
diagnostics for late provider events

## Linked Issues or Issue Description

**What happened?**

The OpenCode driver closed its event queue when a turn completed, was
cancelled, or failed. A second turn on the same session then lost its
response and completion events.

**Expected behavior**

The session must keep its event stream open between turns. Each turn
must deliver its response and one terminal event. The session must close
the stream only during session shutdown or an unrecoverable pump error.

**Steps to reproduce**

1. Start one OpenCode session.
2. Run one turn and wait for its terminal event.
3. Run a second turn on the same session.
4. Confirm that the second turn delivers its response and terminal
event.

**Paperclip version or commit**

`c0e1d87ddc181329471fa80a2061b2c538bb6618`

**Deployment mode**

Built from source with the Paperclip Runner package test suite.

## What Changed

- Keep the OpenCode event queue open across completed, cancelled, and
failed turns.
- Track sealed turn ids and reject later events for those turns with a
diagnostic event.
- Preserve queue shutdown on session close and unrecoverable pump
errors.
- Give each simulated fixture turn unique provider event ids.
- Add regression tests for completed, cancelled, failed, and
closed-session paths.

## Verification

- Type check: `cd packages/paperclip-runner && node
./node_modules/typescript/bin/tsc -p tsconfig.json --noEmit` passed.
- Driver tests: `cd packages/paperclip-runner && npx vitest run
src/drivers/opencode/opencode-server-driver.test.ts` passed except for
the known pre-existing flaky test described below.
- Consumer tests: `cd packages/paperclip-runner && npx vitest run
src/native-session-runtime.test.ts
src/backends/harness-driver-backend.test.ts
src/cli/opencode-app-server-proxy.test.ts
src/conformance/harness-driver.test.ts` passed.
- The known flaky test reproduced on unmodified `master` because fixture
event order depends on a local MCP HTTP round-trip.
- CI must run the full pull request suite.

## Risks

- The queue now retains sealed turn ids for the session lifetime.
OpenCode does not reuse turn ids, so this set grows with the session.
- A late provider event cannot reach a later turn. The driver emits a
diagnostic event so the rejection remains visible.
- The change does not alter session shutdown or unrecoverable pump error
handling.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. This pull request fixes an
OpenCode Runner bug and does not add a roadmap feature.

## Model Used

OpenAI Codex, GPT-5, tool use and code execution; Anthropic Claude
Sonnet 5 also assisted with the implementation. The runtime did not
provide a context window size.

## 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
- [x] All Paperclip CI gates are green
- [x] 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>
2026-09-29 22:05:41 -07:00
..