Share worker session setup and teardown between background jobs #198
- LA
Follow-up to [Maggie's review comment on #194](https://github.com/githubnext/chopin/pull/194#issuecomment-5873523279), part of #181.
Stacked on #196 (`feat/harness-6-pi-structured-output`). The workers are unchanged since #194, so this diff contains only the refactor.
## What changes
- **`jobs/worker-session.ts`** adds `openWorkerSession(agent, { token, maxAiCredits, aborted })`. It opens a fresh just-bash sandbox, registers the per-session credential, opens the agent session, and returns `{ session, close }`. - If the worker aborts while the sandbox or session is still opening, that resource is destroyed once it arrives. - If the session fails to open, the helper tears down the sandbox and credential and rethrows the original error. - `close()` destroys the session, then the sandbox, then releases the credential. Each step runs even if an earlier one fails. - **`jobs/document-summary.ts`** and **`jobs/research-workspace.ts`** use the helper in place of their copies. The research stage still closes its MCP `web_search` client after the worker closes.
## Behavior differences
- The description worker now races sandbox opening against abort, as the research worker already did. Before this change, it waited for the sandbox before it built its abort signal. - The research stage now closes the MCP `web_search` client after releasing the session credential, not before. The web tool holds the job credential directly, not the registry entry, so the order doesn't matter.
## Verification
Neither worker's harness path had test coverage; the job tests inject their own engines. `worker-session.test.ts` covers the helper directly, including one case with a real just-bash sandbox. Removing the late-open destroy or the credential release makes those tests fail.
Assistant-model: Claude Opus 5.5 Assistant-workflow: inline Assistant-duration: 5m converged, estimated 15m Assistant-verification: bun test unit passed: worker-session.test.ts covers teardown order, destroy failure, open failure, late session and late sandbox after abort, and a real just-bash sandbox Assistant-verification: mutation check passed: removing late-open destroy or credential release fails the helper tests Assistant-verification: bun test passed: 1611 pass, 2 skip, 0 fail Assistant-verification: bun run types passed: all workspaces and E2E Assistant-verification: bun run ci passed: dprint, oxlint, tokens Co-authored-by: @lavaman131
lavaman131mentionedlast month on GitHub
lavaman131subscribedlast month on GitHub
lavaman131referenced this in Move workers to structured output and add harness deployment modes #194last month on GitHub
lavaman131base-ref-force-pushedlast month on GitHub
lavaman131head-ref-force-pushed d87d6e645dfflast month on GitHub
lavaman131added-to-stacklast month on GitHub
lavaman131merged e19b9324e639last month on GitHub
lavaman131head-ref-deletedlast month on GitHub