ComputerAgent icon indicating copy to clipboard operation
ComputerAgent copied to clipboard

SDK: multi-turn agent.chat() replays first-turn events instead of advancing

Open shreyas-lyzr opened this issue 6 months ago • 1 comments

Symptom

Calling `agent.chat()` more than once on the same `ComputerAgent` instance returns the first turn's response on every subsequent call, even though each call has a different prompt.

Reproduced live against `github.com/shreyas-lyzr/marketing-agent` with `LocalSubstrate` + `claude-haiku-4-5-20251001`. Three sequential `agent.chat()` calls with three distinct prompts all produced the same answer (the response to prompt #1).

Root cause (suspected)

`packages/sdk/src/computer-agent.ts:194` — `openEventStream` opens a fresh SSE connection to `GET /v1/sessions/:id/events` on every `chat()` call but does not pass `Last-Event-ID`. The harness's per-session replay buffer therefore replays from event 0. Combined with the consumer breaking on the first `SDKResultMessage` it sees (or the harness emitting `ca_session_ended` after each turn and closing the stream), the iterator captures only the first turn's `result`.

Either:

  • The harness should not close the SSE stream on `ca_session_ended` for in-progress sessions; OR
  • The SDK should track `lastEventId` across `chat()` calls and pass it on subsequent `/events` opens; OR
  • The SDK should not call `/events` on subsequent `chat()` calls and instead reuse the open stream.

Workaround for users today

Use `runTask({...})` per turn — each call is a fresh session, so multi-turn memory is lost, but each call returns its own correct response. Pass `sessionStore: { kind: "file", options: { root: ... } }` + the same `sessionId` across calls to maintain conversation memory at the engine level.

Definition of done

  • A failing test in `packages/sdk/` that exercises two-turn `agent.chat()` with distinct prompts and asserts both responses are distinct.
  • Fix in `computer-agent.ts` (`openEventStream` and/or session-end handling).
  • Test green.
  • Update the `marketing-agent.ts` example back to using `agent.chat()` for the multi-turn flow.

Repro

```ts import { ComputerAgent, LocalSubstrate } from "computeragent";

await using agent = new ComputerAgent({ source: { type: "git", url: "github.com/shreyas-lyzr/marketing-agent" }, harness: "claude-agent-sdk", runtime: new LocalSubstrate(), envs: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY! }, options: { permissionMode: "bypassPermissions", model: "claude-haiku-4-5-20251001" }, });

const r1 = await agent.chat("Say 'one'"); const r2 = await agent.chat("Say 'two'"); // Expected: r2.result contains 'two' // Actual: r2.result contains 'one' (replayed from turn 1) ```

Severity

High for the SDK story — multi-turn is a headline feature and the docs/examples assume it works. Workaround is documented above so users aren't blocked, but the example codebase should not ship in this state long-term.

shreyas-lyzr avatar May 15 '26 01:05 shreyas-lyzr

Fixed in b31b91c.

Root cause was as suspected — openEventStream opened a fresh SSE per chat() with no Last-Event-ID, so the replay buffer re-emitted turn 1 forever. Also discovered a second factor: in non-streaming-input mode the harness calls endUserMessages() after enqueuing the createSession body's messages, which terminates the engine after turn 1 entirely (no second turn possible regardless of resume).

Fix:

  • SDK always creates sessions in streaming-input mode internally
  • Always pushes via /messages (never createSession body)
  • Tracks lastSeenEventId, sends as Last-Event-ID on every /events open
  • Synthesizes ca_session_ended per turn (on the SDK's terminal result) so ChatHandle.drain() resolves while the underlying server session stays alive for the next chat()
  • dispose() POSTs /end-input so the engine drains cleanly

Regression test in packages/sdk/src/computer-agent.test.ts asserts two sequential chats with distinct prompts produce distinct results. Verified end-to-end against marketing-agent — 4 turns, 4 distinct deliverables, agent demonstrates memory across turns.

shreyas-lyzr avatar May 15 '26 01:05 shreyas-lyzr