Skip to content

feat(core): make Code Mode agent() durable and llm() idempotent - #67628

Open
jlongster wants to merge 3 commits into
devfrom
feat/codemode-agents-durable
Open

jlongster wants to merge 3 commits into
devfrom
feat/codemode-agents-durable

Conversation

@jlongster

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #67621: makes Code Mode's llm() global idempotent and agent() global durable using Host.fn and Host.durableFn.

Changes

  • packages/core/src/codemode/agents.ts
    • Mark llm() with { type: "async", idempotent: true } so a suspend detaches it without firing extension.after and replays it on resume with the same <runId>:<callId> key.
    • Convert agent() to Host.durableFn({ start, wait, cancel }) with AgentData = { childSessionID: string }.
    • Re-parse call.args (including options.schema) in wait so a resumed wait in a fresh runtime reconstructs its schema codec without closure state from start.
  • packages/core/src/tool/plugin/codemode-agents.ts
    • Split agent into startAgent (creates child with codemodeAgent marker, prompts/wakes it, returns { childSessionID }), waitAgent (stateless reconnect via sessions.resume(childID), reading results and retry counts from durable history), and cancelAgent (sessions.interrupt(childID) on real abort only, never on suspend).
  • Tests (packages/core/test/codemode/agents.test.ts, codemode-agents-e2e.test.ts)
    • Added suspend/resume tests for llm() (same key, extension.after skipped on detach), agent() (plain text & schema output in a fresh runtime without re-calling start), real cancellation calling cancel once, SuspendError during start, and history-backed retries.

Verification

  • packages/core: bunx tsc -b tsconfig.json tsconfig.tests.json — clean.
  • bun test test/codemode/ test/codemode-agents-e2e.test.ts test/tool-execute.test.ts — 123 pass, 0 fail.
  • Prettier & oxlint clean on all 4 changed files.

@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 durable agent() and idempotent llm() work as described. I checked this with the codemode unit and e2e tests (123 pass) and tsc -b on packages/core (clean), and by reading the Host.durableFn contract in packages/codemode. The suspend, resume and cancel paths match it: the child is woken in start, wait only reconnects through sessions.resume, and cancel only runs when the run is really aborted.

Public API: unchanged. CodeModeAgents.Implementation is internal to packages/core, and the only implementation is the built-in plugin.

One concern, in an inline comment: the retry accounting in waitAgent uses two separate counters. It can loop forever when resume fails without recording a new assistant message, and it can go over the retries budget when schema misses and errors mix.


// A failed attempt retries the task in the same child while retries remain, counting attempts already recorded
// in its history so a resume never starts the count over.
const failed = (error: CodeModeAgents.CallError): Effect.Effect<unknown, unknown> =>

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.

Errors and schema misses use different retry budgets. Errors count error assistant messages (failures()); schema misses count task prompts (taskPrompts()). This causes two problems:

  • Possible endless loop: attempt() reads the last assistant message even when sessions.resume failed. Say the turn fails before it records an assistant message (e.g. a run error at admission). After an earlier failed attempt, the last message is still that old error. failed() then sees the same failures() count, sends the task again and calls attempt(). Nothing in that cycle increases the count, so it never ends while retries >= 1.
  • Budget overrun: with retries: 1, a schema miss followed by a provider error gets a third attempt. The error check doesn't count the earlier task prompt.

Using one count on both paths fixes both. For example, check (yield* taskPrompts()) > retries here too, since every attempt sends exactly one task prompt and that count always goes up.

@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 new commit fixes the earlier retry concern. failed() now checks the task-prompt count, which goes up with every retry, so the endless loop on a stale error is gone and errors and schema misses share one retries budget. I checked with the codemode unit and e2e tests (124 pass) and tsc -b on packages/core (clean). Public API: unchanged. Nothing new to flag.

@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 earlier retry-budget point stays fixed. The new commit correctly skips sending the schema reminder again when a resumed wait finds it is already the latest user message. An error in the reminder's answer is still caught first, and a retry still gets its own reminder. I checked with the codemode unit and e2e tests (125 pass, including the new reminder test) and tsc -b on packages/core (clean). Public API: unchanged. Nothing new to flag.

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