Skip to content

fix(sdk): custom agent loop parity for continuations, steering, and subtasks#3936

Open
ericallam wants to merge 3 commits into
mainfrom
fix/custom-agent-loop-fixes
Open

fix(sdk): custom agent loop parity for continuations, steering, and subtasks#3936
ericallam wants to merge 3 commits into
mainfrom
fix/custom-agent-loop-fixes

Conversation

@ericallam

Copy link
Copy Markdown
Member

Summary

Three fixes that bring custom agent loops (chat.customAgent hand-rolled loops and chat.createSession) up to the behavior chat.agent users already get, and that the docs already promise:

  • Continuation runs no longer replay already-answered messages. A chat continuing after a cancel, crash, or upgrade re-delivered every prior user message into the loop's first wait, so the model re-answered an old message while the real new one had to arrive via steering. The .in resume cursor is now seeded before any listener attaches, using the same boot logic as chat.agent.
  • Mid-stream steering no longer wipes the in-flight response. chat.pipeAndCapture (also backing turn.complete()) streamed without a server-generated message id, so a prepareStep injection regenerated the assistant id mid-stream and the frontend replaced the partial message, discarding everything streamed before the injection.
  • Task-backed tools now work from custom agent loops. A child task triggered via ai.toolExecute failed with "chat.agent session handle is not initialized" because the parent's chatId only threaded from the per-turn context that hand-rolled loops never set. It now falls back to the session handle the chat.customAgent wrapper binds at run boot, so children can stream progress into the chat with chat.stream.writer({ target: "root" }) (the documented sub-agent pattern).

Root cause on the replay fix

Attaching any .in listener (chat.createStopSignal, chat.messages.on, the first wait) opens the SSE tail with Last-Event-ID taken from the seq cursor at attach time. Custom loops attached before any cursor existed, so S2 replayed from seq 0. The fix resolves the cursor from the latest turn-complete header and seeds both manager cursors (setLastSeqNum drives the SSE resume point, setLastDispatchedSeqNum gates waiter dispatch) before attach; chat.createSession now creates its stop signal lazily on the first iteration, after the seed. Seeding only the first cursor after attach does not work, which is why the earlier attempt at this was reverted.

All three were reproduced red-green against the references ai-chat project: the replay repro showed the continuation wait consuming a stale message in 403ms with the real message arriving via steering injection; post-fix the wait consumes the real message directly with no injection. Steering now preserves the full in-flight response, and the deepResearch sub-agent streams its progress parts into a raw-loop parent. Existing behavior verified unchanged: full SDK unit suite, chat.agent steering, and stop-then-continue on chat.createSession.

@changeset-bot

changeset-bot Bot commented Jun 12, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 09f8e7a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 25 packages
Name Type
@trigger.dev/sdk Patch
@trigger.dev/python Patch
@internal/sdk-compat-tests Patch
@trigger.dev/build Patch
@trigger.dev/core Patch
@trigger.dev/plugins Patch
@trigger.dev/react-hooks Patch
@trigger.dev/redis-worker Patch
@trigger.dev/rsc Patch
@trigger.dev/schema-to-json Patch
@trigger.dev/database Patch
@trigger.dev/otlp-importer Patch
@trigger.dev/rbac Patch
trigger.dev Patch
@internal/cache Patch
@internal/clickhouse Patch
@internal/llm-model-catalog Patch
@internal/redis Patch
@internal/replication Patch
@internal/run-engine Patch
@internal/schedule-engine Patch
@internal/testcontainers Patch
@internal/tracing Patch
@internal/tsql Patch
@internal/zod-worker Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 9969ac20-9e88-4408-9f0f-273577020c3c

📥 Commits

Reviewing files that changed from the base of the PR and between 174ba12 and 09f8e7a.

📒 Files selected for processing (1)
  • packages/trigger-sdk/src/v3/ai.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/trigger-sdk/src/v3/ai.ts
📜 Recent review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (33)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (2, 10)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (6, 10)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (5, 10)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (9, 10)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (8, 10)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (4, 10)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (10, 10)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (7, 10)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (1, 10)
  • GitHub Check: internal / 🧪 Unit Tests: Internal (2, 12)
  • GitHub Check: internal / 🧪 Unit Tests: Internal (6, 12)
  • GitHub Check: internal / 🧪 Unit Tests: Internal (7, 12)
  • GitHub Check: internal / 🧪 Unit Tests: Internal (11, 12)
  • GitHub Check: internal / 🧪 Unit Tests: Internal (4, 12)
  • GitHub Check: internal / 🧪 Unit Tests: Internal (8, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (3, 10)
  • GitHub Check: internal / 🧪 Unit Tests: Internal (10, 12)
  • GitHub Check: internal / 🧪 Unit Tests: Internal (9, 12)
  • GitHub Check: internal / 🧪 Unit Tests: Internal (3, 12)
  • GitHub Check: internal / 🧪 Unit Tests: Internal (12, 12)
  • GitHub Check: internal / 🧪 Unit Tests: Internal (1, 12)
  • GitHub Check: internal / 🧪 Unit Tests: Internal (5, 12)
  • GitHub Check: sdk-compat / Bun Runtime
  • GitHub Check: sdk-compat / Node.js 20.20 (ubuntu-latest)
  • GitHub Check: sdk-compat / Deno Runtime
  • GitHub Check: sdk-compat / Node.js 22.12 (ubuntu-latest)
  • GitHub Check: typecheck / typecheck
  • GitHub Check: sdk-compat / Cloudflare Workers
  • GitHub Check: packages / 🧪 Unit Tests: Packages (1, 3)
  • GitHub Check: packages / 🧪 Unit Tests: Packages (3, 3)
  • GitHub Check: e2e-webapp / 🧪 E2E Tests: Webapp
  • GitHub Check: packages / 🧪 Unit Tests: Packages (2, 3)
  • GitHub Check: Build and publish previews

Walkthrough

This PR addresses three behavioral issues in custom agent loops and chat session handling. The primary fix introduces resume cursor seeding that scans session history before user code attaches listeners, preventing replay of already-answered messages on continuation. Tool subtask execution now threads parent session context so task-backed tools can stream progress to the root chat. Chat capture streaming receives explicit message ID generation to avoid text loss during mid-stream steering. The raw chat session iterator is reordered to seed cursors before stop-signal creation, with cleanup made safe against early termination. A changelog entry documents all three fixes.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 71.43% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title directly and concisely describes the main changes: fixes to custom agent loops for three specific behaviors (continuations, steering, and subtasks).
Description check ✅ Passed The description provides comprehensive context on the three fixes, root causes, and verification steps, though it deviates from the template structure by omitting the checklist and explicit sections like Testing and Screenshots.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/custom-agent-loop-fixes

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install timed out. The project may have too many dependencies for the sandbox.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@ericallam ericallam marked this pull request as ready for review June 12, 2026 17:51
… steering, and subtasks

Three fixes for chat.customAgent raw loops and chat.createSession:

Continuation boots replayed already-answered user messages into the
first wait: the .in SSE tail attached (via createStopSignal or any
listener) before a resume cursor existed, so S2 replayed from seq 0.
The custom-agent wrapper and createChatSession's first next() now seed
both manager cursors from the latest turn-complete header before
anything attaches, the same boot logic chat.agent uses. Seeding only
setLastSeqNum after attach (the reverted earlier attempt) does not
work because dispatch is gated on the other cursor.

Steering a hand-rolled loop mid-stream wiped the in-flight assistant
text: pipeChatAndCapture called toUIMessageStream without
generateMessageId, so a prepareStep injection starting a new step
regenerated the assistant id and the frontend replaced the partial
message. It now stamps the server-generated id like chat.agent's pipe.

Task-backed tools (ai.toolExecute) failed from custom agent loops with
"session handle is not initialized" on the child run: the chatId only
threaded from the per-turn context that raw loops never set. It now
falls back to the session handle the customAgent wrapper binds at boot,
so child tasks can stream into the parent's chat with
chat.stream.writer({ target: "root" }).

@devin-ai-integration devin-ai-integration 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.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

coderabbitai[bot]

This comment was marked as resolved.

The wire can omit the continuation flag on a run that still has prior
turns. The cursor scan doubles as the prior-state probe (a fresh
session has no turn-complete on .out and seeds nothing), so run it on
every custom-loop boot instead of gating on continuation or attempt
number, mirroring the snapshot-exists arm of chat.agent's boot check.
@ericallam ericallam force-pushed the fix/custom-agent-loop-fixes branch from ce2b2d8 to 174ba12 Compare June 12, 2026 20:50
@pkg-pr-new

pkg-pr-new Bot commented Jun 12, 2026

Copy link
Copy Markdown

Open in StackBlitz

@trigger.dev/build

npm i https://pkg.pr.new/@trigger.dev/build@09f8e7a

trigger.dev

npm i https://pkg.pr.new/trigger.dev@09f8e7a

@trigger.dev/core

npm i https://pkg.pr.new/@trigger.dev/core@09f8e7a

@trigger.dev/python

npm i https://pkg.pr.new/@trigger.dev/python@09f8e7a

@trigger.dev/react-hooks

npm i https://pkg.pr.new/@trigger.dev/react-hooks@09f8e7a

@trigger.dev/redis-worker

npm i https://pkg.pr.new/@trigger.dev/redis-worker@09f8e7a

@trigger.dev/rsc

npm i https://pkg.pr.new/@trigger.dev/rsc@09f8e7a

@trigger.dev/schema-to-json

npm i https://pkg.pr.new/@trigger.dev/schema-to-json@09f8e7a

@trigger.dev/sdk

npm i https://pkg.pr.new/@trigger.dev/sdk@09f8e7a

commit: 09f8e7a

coderabbitai[bot]

This comment was marked as resolved.

…itly

Custom-loop subtask tool metadata read the chatId off the Session
handle id. That value already equals the external chatId (the handle
is opened on payload.chatId), but reading it from a dedicated locals
slot set at run boot matches the documented ToolCallExecutionOptions
contract directly and decouples from the handle id semantics.

@devin-ai-integration devin-ai-integration 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.

Devin Review found 1 new potential issue.

Open in Devin Review

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.

🚩 createChatSession documented for plain task() but requires chatSessionHandleKey

The docstring example for createChatSession (line 9013) shows usage in a plain task({}), but createStopSignal() internally calls stopInput.on()getChatSession() which requires chatSessionHandleKey to be set in locals. This works because in practice createChatSession is intended to be called inside a chatCustomAgent wrapper (which sets the handle at line 5160) or in code that manually sets up the session. The example is slightly misleading but this is pre-existing — the PR doesn't change this contract.

(Refers to lines 9030-9033)

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant