Skip to content

fix(core): replace agent script polling, tighten fetch response tracking, and stop waking dead workers - #68401

Open
jlongster wants to merge 2 commits into
devfrom
codemode-round2-runtime-fixes
Open

jlongster wants to merge 2 commits into
devfrom
codemode-round2-runtime-fixes

Conversation

@jlongster

@jlongster jlongster commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Issue for this PR

Related to #66478

Type of change

  • Bug fix
  • Refactor / code improvement

What does this PR do?

This is PR 2 of 3, stacked on #68400 (codemode-fix-tear-bugs): its base is dev, so it includes PR 1's commits until that PR merges. Every change is behind full_codemode; with the flag off, Code Mode never enters full mode and flag-off behavior is unchanged.

  1. Agent script waits no longer poll SQLite. waitAgent now waits on CodeModeJob.waitBackground, where each job delivery registers a Deferred before its forkIn({ startImmediately: true }), so a delivery that completes synchronously cannot leave a stale entry that spins at 100% CPU. The 10ms poll loop and its full-table pendingBackground scans are gone. Only a codemode-sourced synthetic notice resumes an idle child, and an active child is waited on with sessions.wait, so a resume can no longer force an empty LLM step.
  2. Fetch response tracking is one collection. liveResponses, fullyRead and targets collapse into Map<string, Set<Tracked>>. A recursive retain proxy reads stream getters with the raw receiver, binds methods, and wraps getReader()/values() results so holding only a reader still keeps the tracked entry alive across GC. Body methods record read in .finally only when the body was unlocked before the call and was actually used, so invalid JSON counts as consumed while text() on a locked body does not clear the live body. The single guarded Bun.gc(true) stays in CodeModeTool.suspendAll.
  3. Dead workers are no longer woken. Dead pending jobs and dead-worker cleanup discard the saved run and fail the running part without admitting a notice, so a dead worker is not revived for a turn nobody waits on. recoverCodeModeCalls resumes only when the session is not a worker. The duplicate shell-settle loops are one pass over the union, the redundant recovery sets are removed, and the dead/orphaned session loops merge with guards so only a dead session releases its claim and only an orphaned worker gets the closed marker.
  4. Small dedupes: hoisted REMINDER_PROMPT, schemaMissError, and the unshadowed assistantCallError in codemode-agents; AgentOptions now spreads StartOptions and AskOptions.

How did you verify your code works?

  • packages/core: nice -n 19 bunx tsc -b tsconfig.json tsconfig.tests.json (pass)
  • Focused tests from packages/core (173 passed): test/codemode/execute-durable.test.ts, test/session-execution.test.ts, test/durable-tool-restart.test.ts, test/codemode-agents-e2e.test.ts, test/codemode/agents.test.ts
  • New fetch tests hold only body.getReader() across Bun.gc(true) and still block suspendAll, verify invalid res.json() lets a later suspend save, and verify res.text() on a reader-locked body keeps the body live.
  • New restart test seeds a dead worker with a running Code Mode call, a running job, and a snapshot: boot discards the snapshot, fails the part, admits no synthetic message, and never drains the worker.
  • bunx prettier --check and bunx oxlint on every changed file (pass)

Screenshots / recordings

N/A (no UI change)

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Related: #66478

…elled job cleanup

Refuse a background execute on its call during a planned handoff, so no job row is written before its script would start. Clear the handoff signal before discarding saved runs, so a failed handoff settles them instead of leaving them running. Settle a released saved background job as failed, and deliver and complete the outbox row of a caller-cancelled job.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks sound. I ran the focused core tests (183 passed), the server handoff test, and type-checked core and server with tsc -b (both clean). I couldn't reproduce the agent() polling spin or the empty-step resume end to end: they need full_codemode with a real agent run, which the simulated model can't drive.

Public API: unchanged. This is internal Code Mode behaviour behind full_codemode.

Two of the new tests pass without the fix, so they don't guard what they describe (see the inline comments). With the previous code swapped in, only the locked-body text() fetch test fails. The reader-only GC test, the invalid-JSON test and the dead-worker restart test all still pass. I found nothing blocking in the code itself.

Related: #66478

}
})

test("a body reader the script still holds blocks a suspend after a collection", async () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test also passes with the base fetch.ts swapped in, so it doesn't show that the new retain wrapping of getReader() is what keeps the run live. Bun.gc(true) probably isn't collecting the response here, or something else is still blocking the suspend. Could you make it fail on the old code, or drop it? The invalid-JSON test below also passes on base, so it doesn't guard a change either.


// A dead worker's script is discarded and its running part failed, but no notice is admitted: one would wake the
// worker for a turn nobody waits on.
it.live("does not wake a dead worker for its interrupted Code Mode call", () =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This passes with the parent commit's restart.ts/job.ts too, and still passes when I remove closed: true from the marker. So it doesn't exercise the new behaviour (fail the part, admit no notice). The old path apparently never admitted a visible synthetic message or drained the worker in this setup either. Could you seed a case where the old code did wake the worker, or drop the test?

@jlongster
jlongster force-pushed the codemode-round2-runtime-fixes branch from 50028f5 to 486209f Compare December 5, 2026 00:40

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant