Repository navigation
fix(ts): await async hooks and route their errors through hooksRaise - #554
Merged
NandhaKishorM merged 1 commit intoSep 27, 2026
Merged
Conversation
dispatch called each hook and discarded what it returned. An async hook, the natural way to write one that ships telemetry or reads a cache, therefore ran detached: the call resolved before it finished, a rewrite of ctx.results or a ctx.skip after its first await was lost, and a rejection was an unhandled rejection even with hooksRaise=false, which ends a Node process by default. Python awaits them (NandhaKishorM#277). Add dispatchAsync, which waits for each hook in order and applies raiseErrors to a rejection the way it does to a throw, and use it for the predict hooks in Agent and Router and for onLoad and onEvict. Router.route is synchronous, so onRoute keeps the synchronous dispatch; it now reports a rejecting hook as a warning instead of leaving it unhandled. The hook types allow a promise return. dispatchAsync is not exported from the package index, and a hook that returns nothing behaves as before. Add tests for awaiting, ordering, rewriting and skipping after an await, both hooksRaise modes, and a re-entrant router.load from an async onLoad hook. Per-hook timeouts (hooks_timeout) are not part of this change.
4 tasks done
Owner
|
Thanks @Divit-aggarwal, merged. Combined with #519: default hooks still run once per Router.predict, and now they 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
laya-tsnow waits for anasynchook and applieshooksRaiseto a rejection, as Python does since #277.dispatchAsyncruns each hook in order, awaiting any promise it returns, andAgentandRouteruse it forthe predict hooks and for
onLoadandonEvict.Why
dispatchcalled each hook and discarded what it returned, so anasynchook ran detached:ctx.resultsor actx.skip(...)after itsfirst
awaitwas losthooksRaise: false, the mode meant to keep telemetry fromfailing a request. Node ends a process on an unhandled rejection by default.
A hook that awaits a metrics backend, which is the natural way to write one, hit both:
Design
dispatchAsyncawaits each hook before the next and treats a rejection like a throw: raised, or warned whenhooksRaiseis false. A hook that returns nothing behaves as before.Router.routeis synchronous, soonRoutekeeps the synchronousdispatch. That function no longer leaves areturned promise unhandled: a rejection is reported as a warning. The README says
onRouteis not awaited.dispatchAsyncis not exported from the package index, and the hook types now allow aPromise<void>return.onLoadandonEvictare awaited, sorouter.loadresolves after an async lifecycle hook finishes._agentsis set before they fire, so a hook that calls
router.loadagain gets the cached agent; a test covers it.Behaviour change
Async hooks that used to run detached are now awaited, so a slow one lengthens the call it belongs to, and
loadwaits for slowonLoadandonEvicthooks. Synchronous hooks are unaffected.Not part of this change: Python's per-hook timeout (
hooks_timeout), whichlaya-tshas no equivalent of.Tests
Six new tests in
hooks.test.ts. Five fail onmainand pass with this change (two of them also raiseUnhandled Rejection errors on
main): an async end hook rewriting results after anawait, an async start hookskipping after an
await, ordering across hooks, bothhooksRaisemodes for a rejecting hook, and Router hookstogether with a rejecting
onRoute. The sixth passes on both: it guards the newawaitinload, checking thatan async
onLoadhook can callrouter.loadfor the model being loaded without deadlocking.How it was verified
The hook, router, agent and language-temperature test files ran 5 times: 5 of 5 passed. The two Python
commands are the CI gates; no Python was changed.
Scope
hooks.ts,agent.ts,router.ts, the hook tests and one README paragraphChecklist
mainRelated: #277 (the Python change). #519 also edits
_predictHookedandRouter.predict, a few lines from thesecall sites, so a small conflict is likely whichever merges second. #329 adds
Router.route_batchandpredict_batchand would usedispatchAsyncfor their hooks.Written with Claude Code; I've reviewed the change and re-run the verification myself.