Skip to content

fix(ts): run process-wide default hooks once per Router.predict (#435 parity) - #519

Merged
NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
Divit-aggarwal:fix/ts-router-default-hooks-once
Sep 27, 2026
Merged

NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
Divit-aggarwal:fix/ts-router-default-hooks-once

Conversation

@Divit-aggarwal

Copy link
Copy Markdown
Contributor

What

A process-wide default hook now runs once per Router.predict request in laya-ts, not twice. This is
the TypeScript side of #433, which #435 fixed in Python only.

Why

Router.predict composes the default hooks for its own call, then calls agent.systemOne, whose hook
composition adds them again. A default onPredictEnd therefore fired for the Router and again for the
Agent on every request:

setDefaultHooks({
  onPredictStart: (ctx) => events.push(`start:${ctx.router ? "router" : "agent"}`),
  onPredictEnd: (ctx) => events.push(`end:${ctx.router ? "router" : "agent"}`),
});
const router = new Router();
router.attach("english", agent);
await router.predict("hi", questions, { model: "english" });
// before: ["start:router", "start:agent", "end:agent", "end:router"]
// after:  ["start:router", "end:router"]

Python fixed this with a ContextVar (#435). JavaScript has no browser-safe equivalent (AsyncLocalStorage
is Node-only, and laya-ts also targets the browser), and a module-level flag would wrongly skip a
direct Agent call running concurrently.

So the Router marks the identity of the per-call options object it hands to the Agent in a WeakSet, and
the 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 in hooks.ts and are not exported from the
package 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 Agent used directly still runs the defaults.

Tests

Two tests in hooks.test.ts, both failing on main and passing with this change:

  • defaults fire once per Router.predict, on the Router and not again in the Agent
  • a direct Agent call made while a Router request is in flight still gets its defaults (this is what
    a global flag would break)

How it was verified

cd laya-ts
npx tsc -p tsconfig.json --noEmit   # exit 0
npm test -- --run                   # 22 files, 372 tests passed
npm run test:package                # 1 package test passed
cd ..
ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120   # All checks passed
python -m compileall -q laya/ tests/                                   # exit 0

The two Python commands are the CI gates; no Python was changed.

Scope

  • TypeScript only: three source files (+20/-2) and the tests
  • No public API change, no dependencies, no Python changes
  • No accuracy or performance claim is made

Checklist

  • Focused on one change
  • Rebased on the latest main
  • Tests pass locally
  • No public API change

Related: #433 (closed) and #435 (the Python fix). #329 adds Router.route_batch and predict_batch, which
will need the same marker on their Agent calls; #340, #411 and #336 also touch router.ts or agent.ts,
so small textual conflicts are possible.

Written with Claude Code; I've reviewed the change and re-run the verification myself.

…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.
@NandhaKishorM
NandhaKishorM merged commit 18d1ec1 into NandhaKishorM:main Sep 27, 2026
20 checks passed
@NandhaKishorM

Copy link
Copy Markdown
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.

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.

2 participants