Repository navigation
fix: isolate lifecycle hooks so telemetry cannot replay requests - #120
Merged
Merged
Conversation
The factory called the caller's raw hooks object inside the dispatch try. A throwing onRequestEnd after a successful vendor call was treated as a provider failure, so the request fell back to a second provider and was billed twice. composeHooks already guarded handlers, but only for callers who used it. Every factory hook now goes through guardHooks: sync throws and async rejections are swallowed, handlers keep their `this`, and handlers are resolved at call time. composeHooks shares the same safe call. Part of #116. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review of #120: the tests failed without the fix for incidental reasons (onRequestError throwing, leaked rejections), directly constructed adapters still let async onSchemaDrift rejections escape, and composeHooks semantics changed silently. - BaseProvider guards config.hooks, so direct adapters contain sync throws and async rejections; guardHooks is idempotent so factory adapters are not double wrapped. - guardHooks is a pass-through Proxy: only caller-defined methods are present, resolved at call time; the factory keeps its presence checks. - composeHooks captures handlers at compose time again; `this` binding and rejection containment are documented as changes. - Compile-time check that HOOK_METHODS covers ObservabilityHooks. - Each failing hook method is logged once via the configured logger. - Tests isolate a lone throwing onRequestEnd and check unhandled rejections locally; each fails when its fix is reverted. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Round-2 review of #120: proxying the caller's hooks object violated the Proxy invariant for non-writable, non-configurable properties, so a frozen hooks object threw on every hook read inside dispatch and replayed the request on the fallback provider. - guardHooks proxies a private empty target and reads through to the caller's object; only hook-method names are wrapped, and wrappers are cached per handler so identity is stable. - Adapter updateConfig({ hooks }) re-applies the guard. - Adapter log source resolves this.name lazily instead of the (minifiable) constructor name. - Tests: frozen and defineProperty hooks through the factory, class instance `this` through the factory guard, updateConfig re-guard. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…dlers correctly Round-3 review of #120 (approved, low findings): a function shared by several hooks was reported under the first method name, and the guarded view dropped writes/deletes, had no prototype, and broke if frozen. - Wrapper cache is keyed by method name. - set/deleteProperty/defineProperty and getPrototypeOf forward to the caller's object; preventExtensions is refused so later reads keep working. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merged
stackbilt-admin
added a commit
that referenced
this pull request
Oct 15, 2026
Minor release for the reliability sweep (#119) and hook isolation (#120): new public APIs for managed streaming, client-scoped resilience state, explicit request normalization, client routing preflight and local quota denial, plus the streaming, caching, breaker and hook fixes. The CHANGELOG entry was reviewed against the merged code, including the behaviour changes callers may notice. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Oct 15, 2026
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.
Part of #116 (the hook-replay defect; the rest of the issue stays open).
Problem
LLMProviderFactorycalled the caller's rawhooksobject inside the dispatchtry. WhenonRequestEndthrew after a successful vendor call, the error was classified as a provider failure: the request fell back to the next provider, which billed the logical request twice. Async hooks that rejected raised unhandled rejections, including from directly constructed adapters (onSchemaDrift).Fix
guardHooks()(internal, not exported) is a Proxy over a private empty target that reads through to the caller's hooks. Frozen or read-only hooks objects therefore work. Only methods the caller defined are present, resolved at call time, with a stable wrapper per method. Writes, deletes and the prototype forward to the caller's object; the view refuses to be frozen. Sync throws and async rejections are contained, and handlers keep the hooks object asthis. It is idempotent.BaseProvider(direct adapters, includingupdateConfig({ hooks })) both guard hooks. Each failing hook method is logged once through the configuredlogger.composeHooks()still captures handlers at compose time. It now contains async rejections and calls handlers with their implementation asthis. Class-method handlers used to fail silently and now run; this is in the CHANGELOG.ObservabilityHooks.Tests (
src/__tests__/hook-isolation.test.ts, 20)Each fix was reverted in turn to confirm its test fails:
onRequestEndreplaying on Groq, and rejecting hooks raising unhandled rejections (now checked inside that test rather than leaking into another).OpenAIProviderrejectingonSchemaDrifttest fails.thisbinding reverted: the factory class-instance test and thecomposeHooksclass test fail.guardHooksdefining every method: the presence test fails.definePropertytests fail. Removing theupdateConfigre-guard: its test fails.tsc.Behaviour notes
Adapters built by the factory (e.g. from
getProvider()) and adapters givenhooksnow hold a guarded view rather than the caller's object. Method presence is unchanged, but object identity differs.Not in this PR (remaining #116 scope)
Producers for
onRetry,onCircuitStateChangeandonBudgetThreshold(documented as not yet emitted); request signal and absolute deadline; retry-sleep aborts; Retry-After parsing; vendor error fixtures; fault matrix; workerd test; coverage gates.Validation (Windows, Node 24.19.0)
npm test: 637 passed across 28 files.npm run typecheck: passed.npm run test:worker-import: passed.🤖 Generated with Claude Code