Repository navigation
fix(ts): run process-wide default hooks once per Router.predict (#435 parity) - #519
Merged
NandhaKishorM merged 1 commit intoSep 27, 2026
Conversation
…haKishorM#435 parity) Router.predict composed the default hooks for its own call and then called agent.systemOne, whose hook composition added them again, so a default hook fired twice per request. Python fixed this in NandhaKishorM#435 with a context variable. JavaScript has no browser-safe equivalent, so the Router marks the identity of the per-call options object it passes to the Agent in a WeakSet, and the Agent skips the defaults for a marked call. The marker is per call, so a concurrent direct Agent call still gets its defaults, and the options the Agent receives keep their shape. No public API changes. Add tests for once-per-request firing and for the concurrent direct call.
4 tasks done
Owner
|
Thanks @Divit-aggarwal, merged. It sits alongside #554's async hooks: defaults still run once per Router.predict, and hooks are awaited. It ships in 0.3.21. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
A process-wide default hook now runs once per
Router.predictrequest inlaya-ts, not twice. This isthe TypeScript side of #433, which #435 fixed in Python only.
Why
Router.predictcomposes the default hooks for its own call, then callsagent.systemOne, whose hookcomposition adds them again. A default
onPredictEndtherefore fired for the Router and again for theAgent on every request:
Python fixed this with a
ContextVar(#435). JavaScript has no browser-safe equivalent (AsyncLocalStorageis Node-only, and
laya-tsalso targets the browser), and a module-level flag would wrongly skip adirect
Agentcall running concurrently.So the Router marks the identity of the per-call options object it hands to the Agent in a
WeakSet, andthe Agent leaves the defaults out of a marked call. The marker is per call and garbage-collected with the
call. The options the Agent receives keep the same shape, so the tests that pin the Router-to-Agent call
(
lang-temperature.test.ts) pass unmodified. Both helpers live inhooks.tsand are not exported from thepackage index.
Behaviour change
For a request that goes through the Router, default hooks now see the Router context only (with
ctx.decision), and no longer also see an Agent context. That matches the Python behaviour after #435.Installed and per-call hooks are unchanged, and
Agentused directly still runs the defaults.Tests
Two tests in
hooks.test.ts, both failing onmainand passing with this change:Router.predict, on the Router and not again in the AgentAgentcall made while a Router request is in flight still gets its defaults (this is whata global flag would break)
How it was verified
The two Python commands are the CI gates; no Python was changed.
Scope
Checklist
mainRelated: #433 (closed) and #435 (the Python fix). #329 adds
Router.route_batchandpredict_batch, whichwill need the same marker on their Agent calls; #340, #411 and #336 also touch
router.tsoragent.ts,so small textual conflicts are possible.
Written with Claude Code; I've reviewed the change and re-run the verification myself.