fix(sdk): custom agent loop parity for continuations, steering, and subtasks#3936
fix(sdk): custom agent loop parity for continuations, steering, and subtasks#3936ericallam wants to merge 3 commits into
Conversation
🦋 Changeset detectedLatest commit: 09f8e7a The changes in this PR will be included in the next version bump. This PR includes changesets to release 25 packages
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 |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📜 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)
WalkthroughThis 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)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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
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. Comment |
… 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" }).
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.
ce2b2d8 to
174ba12
Compare
@trigger.dev/build
trigger.dev
@trigger.dev/core
@trigger.dev/python
@trigger.dev/react-hooks
@trigger.dev/redis-worker
@trigger.dev/rsc
@trigger.dev/schema-to-json
@trigger.dev/sdk
commit: |
…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.
There was a problem hiding this comment.
🚩 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)
Was this helpful? React with 👍 or 👎 to provide feedback.
Summary
Three fixes that bring custom agent loops (
chat.customAgenthand-rolled loops andchat.createSession) up to the behaviorchat.agentusers already get, and that the docs already promise:.inresume cursor is now seeded before any listener attaches, using the same boot logic aschat.agent.chat.pipeAndCapture(also backingturn.complete()) streamed without a server-generated message id, so aprepareStepinjection regenerated the assistant id mid-stream and the frontend replaced the partial message, discarding everything streamed before the injection.ai.toolExecutefailed 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 thechat.customAgentwrapper binds at run boot, so children can stream progress into the chat withchat.stream.writer({ target: "root" })(the documented sub-agent pattern).Root cause on the replay fix
Attaching any
.inlistener (chat.createStopSignal,chat.messages.on, the first wait) opens the SSE tail withLast-Event-IDtaken 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 (setLastSeqNumdrives the SSE resume point,setLastDispatchedSeqNumgates waiter dispatch) before attach;chat.createSessionnow 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.agentsteering, and stop-then-continue onchat.createSession.