Repository navigation
feat(plugins): let tools deliver their finished reply without a second model turn - #164558
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
|
Codex review: needs maintainer review before merge. Reviewed October 4, 2026, 4:59 AM ET / 08:59 UTC (Revision 11). ClawSweeper reviewWhat this changesAdds an optional plugin-tool capability that delivers a finished reply through OpenClaw’s conversation delivery pipeline without another model turn. Merge readiness✅ Ready for maintainer review The work remains useful: current main and the latest release lack this capability. The latest head resolves the prior Codex sibling-completion blocker, and no remaining actionable introduced defect was identified. Priority: P2 Review scores
Verification
How this fits togetherThe agent runtime processes tool results before deciding whether to request another model response. Admitted tool-authored replies pass through the existing conversation delivery owner, which sends them and records the assistant reply after successful delivery. flowchart TD
A[Model tool calls] --> B[Tools and result middleware]
B --> C{Direct capable call with final reply?}
C -->|No| D[Continue model response]
C -->|Yes| E[Settle sibling calls and finish turn]
E --> F[Conversation delivery]
F --> G[Send reply and mirror transcript]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +346, Tests +930, Docs +51, Other +1. Total +1328 across 44 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Keep one opt-in reply-admission contract across runtimes, with ordinary tools continuing normally and the existing delivery owner retaining send and transcript authority. Do we have a high-confidence way to reproduce the issue? Not applicable as a feature request; the captured live comparison demonstrates the extra model-turn cost and the embedded runtime’s improved result. Is this the best way to solve the issue? Yes. The patch extends the existing source-reply delivery owner and adds a narrow capability rather than introducing another send or transcript-writing pipeline. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against bced15851945. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (10 earlier review cycles; latest 8 shown)
|
e447b7e to
f5acf38
Compare
f8f7853 to
b47ed19
Compare
…odel turn Tools can declare canDeliverSourceReply and return the finished reply in details.sourceReply. The embedded runtime and the Codex bridge record it as a tool-authored source reply payload, persist it as the assistant turn, deliver it to the current conversation like assistant text, and terminate the tool batch when the reply is final, so a tool that already wrote the answer no longer costs a post-tool model turn. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Collapse the four new harness SDK exports into captureToolAuthoredSourceReply, used by both the embedded runtime and the Codex bridge, and record the +1 public export in the pinned SDK surface budget. Cover the failed transcript write path: delivery still proceeds and the failure is logged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Move the message-tool source-reply facts, tool-authored reply capture and turn termination into dynamic-tool-source-reply.ts so dynamic-tools.ts does not grow past its line cap. Replace the plugin tool type cast with a property check, and cover the terminal hook installation in the session prepare test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Address review findings: - Codex reads the reply from the result after middleware and extensions, so middleware can rewrite or withdraw it. - Drop the early transcript write; tool-authored replies take the existing source-reply path and delivery writes the transcript row after the send, with its session, lifecycle and writer checks. - Replies are final only; final: false is ignored and the model continues. - Admission requires text or media, matching final payload delivery. - Calls nested inside a Code Mode program are never admitted as replies. - Treat a captured tool-authored reply as a complete turn: the delivery state still reads missing until the host sends it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
b47ed19 to
4a7c373
Compare
CHANGELOG/** is generated at release time; the PR description carries the note. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tch test Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ored replies A per-call terminate hint cannot see the rest of its batch: a second capable tool without a final reply, or a call rejected before execution, kept the turn open and let the model restate the captured reply. Add one internal, OpenClaw-only agent-loop hook that runs once per assistant message after all its tool batches settle and may end the turn. The tool-authored hook admits exactly what the completion handler captures (a direct, capable, non-error result with a deliverable final reply), which also fixes an ok:false result ending the turn with nothing captured. The batch combining step moves to tool-batch-completion.ts, shrinking agent-loop.ts. Tests drive the real agent loop with explicit gates instead of timers and assert the completion order. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…n-completion test Co-Authored-By: Claude Opus 5.5 <[email protected]>
Admission counted blank mediaUrl/mediaUrls entries that source-reply delivery drops, so a reply with only blank media could end the turn and deliver nothing. Admission and delivery now resolve media through one shared function. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…dinary siblings The Codex harness releases a turn after a terminal dynamic tool only when no ordinary sibling result arrived in the batch, so a captured final reply plus an ordinary sibling let Codex run another model step in either completion order. A captured tool-authored final reply now marks its batch complete: siblings still settle but cannot reopen the turn. Other terminal tools keep the existing behavior. The per-result classification moves from the server-request handler into the lifecycle controller, where both completion orders are tested. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Merged via squash.
|
…d model turn (openclaw#164558) * feat(agents): deliver tool-authored source replies without a second model turn Tools can declare canDeliverSourceReply and return the finished reply in details.sourceReply. The embedded runtime and the Codex bridge record it as a tool-authored source reply payload, persist it as the assistant turn, deliver it to the current conversation like assistant text, and terminate the tool batch when the reply is final, so a tool that already wrote the answer no longer costs a post-tool model turn. * fix(plugins): expose one SDK helper for tool-authored source replies Collapse the four new harness SDK exports into captureToolAuthoredSourceReply, used by both the embedded runtime and the Codex bridge, and record the +1 public export in the pinned SDK surface budget. Cover the failed transcript write path: delivery still proceeds and the failure is logged. * fix(codex): extract dynamic tool source-reply decisions Move the message-tool source-reply facts, tool-authored reply capture and turn termination into dynamic-tool-source-reply.ts so dynamic-tools.ts does not grow past its line cap. Replace the plugin tool type cast with a property check, and cover the terminal hook installation in the session prepare test. * test(agents): keep real exports in tool-authored reply mocks * fix(agents): deliver tool-authored replies through the source-reply path Address review findings: - Codex reads the reply from the result after middleware and extensions, so middleware can rewrite or withdraw it. - Drop the early transcript write; tool-authored replies take the existing source-reply path and delivery writes the transcript row after the send, with its session, lifecycle and writer checks. - Replies are final only; final: false is ignored and the model continues. - Admission requires text or media, matching final payload delivery. - Calls nested inside a Code Mode program are never admitted as replies. - Treat a captured tool-authored reply as a complete turn: the delivery state still reads missing until the host sends it. * chore: drop the release-owned changelog entry CHANGELOG/** is generated at release time; the PR description carries the note. * fix(agents): match source-reply capable tools by normalized name The completion handler compares policy-normalized tool names, so the capable set and the terminal hook must too; otherwise a tool registered with capitals would end the turn without its reply being captured. * fix(agents): complete mixed batches and reject Codex program calls for tool-authored replies - Other calls in a batch with a capable call carry the terminal hint, so a captured final reply ends the turn after the whole batch settles. - Codex only captures tool-authored replies from the model-only openclaw_direct namespace, which Code Mode programs cannot reach. - Prove the transcript mirror writes nothing after session writer replacement. * test(agents): avoid returning from a promise executor in the mixed-batch test * test(agents): type the fake provider stop reason in the mixed-batch test * fix(agents): end the turn after the whole batch settles for tool-authored replies A per-call terminate hint cannot see the rest of its batch: a second capable tool without a final reply, or a call rejected before execution, kept the turn open and let the model restate the captured reply. Add one internal, OpenClaw-only agent-loop hook that runs once per assistant message after all its tool batches settle and may end the turn. The tool-authored hook admits exactly what the completion handler captures (a direct, capable, non-error result with a deliverable final reply), which also fixes an ok:false result ending the turn with nothing captured. The batch combining step moves to tool-batch-completion.ts, shrinking agent-loop.ts. Tests drive the real agent loop with explicit gates instead of timers and assert the completion order. * test(agents): resolve tool gates without optional promises in the turn-completion test * fix(agents): reject blank media before admitting a tool-authored reply Admission counted blank mediaUrl/mediaUrls entries that source-reply delivery drops, so a reply with only blank media could end the turn and deliver nothing. Admission and delivery now resolve media through one shared function. * fix(codex): keep a tool-authored final reply's batch released past ordinary siblings The Codex harness releases a turn after a terminal dynamic tool only when no ordinary sibling result arrived in the batch, so a captured final reply plus an ordinary sibling let Codex run another model step in either completion order. A captured tool-authored final reply now marks its batch complete: siblings still settle but cannot reopen the turn. Other terminal tools keep the existing behavior. The per-result classification moves from the server-request handler into the lifecycle controller, where both completion orders are tested.
Release-line-only harness fixes that 2026.10.1-2026.10.4 carried but main (and so release/2026.10.5) never received: - cron-scheduled-authority: seed the cron authority specimen through the published baseline's own SQLite writer (seedPublishedCronJobs) instead of a raw cron/jobs.json the candidate now refuses as a retired pre-July store; drop owner fields for pre-2026.7.1 baselines (usesCronOwnerAtBaseline). Cron-seed parts of a9fd56d and 47ecf4d only; the dispatcher passes the package root to seedState. - bundled plugin runtime smoke: join deferred read-only update discovery via update.status before the package-manager child watchdog (from 756a515). - codex npm plugin live: detect the explicit source-reply finality contract anywhere under extensions/codex/src/app-server/ since #164558 moved it to dynamic-tool-source-reply.ts (new; no 10.4 equivalent, main also affected). Test/harness files only.
What Problem This Solves
A plugin tool that already produces the finished user reply (a report, a confirmation, a formatted answer) still costs a second model turn: OpenClaw feeds the tool result back to the model, which restates the same text before anything reaches the channel. On a Telegram sales bot with such tools that extra turn is about 6 seconds of every reply.
User Impact
User impact: a plugin tool can declare
canDeliverSourceReplyand return its finished reply indetails.sourceReply. OpenClaw delivers that reply to the conversation the turn came from, ends the tool batch, and records the reply as the assistant turn after the send. Nothing opts in by default; existing tools and themessagetool are unchanged.Why This Change Was Made
The runtime already has a source-reply path for the built-in
messagetool: such replies are delivered even when automatic replies are suppressed, and mirrored into the transcript by delivery, with its session, lifecycle-revision and writer-run checks. This PR lets an author-declared tool capability feed that same path, instead of adding a parallel one.sourceReply.final: falseare never admitted; the model continues as usual. Admission and delivery resolve media through one shared function, so a reply whose media entries are all blank cannot end a turn that delivery would then drop. On the embedded runtime, nested calls carry a parent call id and are rejected at completion. On Codex, only calls in the model-onlyopenclaw_directnamespace qualify; Codex never exposes that namespace to Code Mode programs, so a program call cannot end the turn or reach the conversation.tool-batch-completion.ts, which keepsagent-loop.tsunder its line cap. Incomplete-turn resolution treats the captured reply as a complete turn: the delivery state still reads "missing" until the host sends it.recordDynamicToolResult), where both orders are tested.The SDK gains one helper,
captureToolAuthoredSourceReply, used by the Codex harness; the pinned surface budget is raised by one with a reason comment. The Codex bridge's source-reply decisions moved intodynamic-tool-source-reply.ts, sodynamic-tools.tsdoes not grow past its line cap.Evidence
Live on Telegram, an agent on the embedded runtime (gpt-5.6-terra) with a test plugin exposing two identical tools, one with the capability and one without, after the review-driven rework:
The reply text arrived verbatim, with no warning and no post-tool model turn.
Session writer replacement. The captured reply takes the same final-reply delivery path as the model's own final answer. Delivery mirrors it into the transcript only after a successful send, under the dispatching run's session, lifecycle-revision and writer-run fence. A new test drives the payload built from a tool-authored capture through that mirror against a real session store: with the admitted writer it writes exactly one assistant row (a repeat is deduped), and after the session writer is replaced it writes none. The channel send itself is fenced exactly as for any ordinary final answer: this PR adds no new send path and does not widen that boundary.
The turn-completion tests drive the real agent loop with parallel tools held on explicit gates (no timers), assert the completion order, and count provider requests. Five of them fail on the previous head, including one more defect it had: a capable tool reporting
ok: falseended the turn although the completion handler never captured its reply. The Codex namespace tests also fail on the previous head.Test cost (local Vitest, per new or changed test file):
embedded-agent-messaging-extraction.tool-authored.test.tsmessage-tool-terminal.tool-authored.test.ts(real agent loop)payloads.tool-authored.test.tsembedded-agent-subscribe.handlers.tools.source-reply.test.tsdynamic-tools.tool-authored-source-reply.test.ts(Codex)dispatch-from-config.transcript.tool-authored.test.ts(real session store)The transcript-mirror file spends its time opening a real SQLite session store on the first test; the second test takes about 140 ms.
The live runs also caught two real defects, both fixed with regression tests that fail without the fix:
afterToolCallwith{ isError }; an early per-call hook misread that partial override and never ended the turn. Turn completion now reads the finalized tool results after every hook has applied.Tests cover extraction (text, media, blank media, attachments-only,
final: false), turn completion through the real agent loop (completion orders, two capable tools, pre-execution rejection, errors,ok: false, afterToolCall withdrawal, name normalization), the transcript mirror under writer replacement, the completion handler (capability gate, errors, nested calls, non-final replies), the payload builder (delivery despite suppression, transcript mirror without an owner, in both reply modes), incomplete-turn resolution, client tool-set collection, the session prepare hook installation, the Codex bridge (terminate, middleware rewrite and withdrawal, non-final, capability gate, program-reachable namespaces), and Codex batch release in both sibling orders, including an ordinary terminal tool whose batch must stay open.Checks on this branch, rebased on current
main:pnpm tsgo:core,tsgo:extensions,tsgo:core:test,tsgo:extensions:test; 1,700+ tests across the affected suites;pnpm format:check,format:docs:check; the plugin SDK surface check; and the line-cap, assertion-safety and mock-export ratchets.🤖 Generated with Claude Code