Skip to content

fix: isolate lifecycle hooks so telemetry cannot replay requests - #120

Merged
stackbilt-admin merged 4 commits into
mainfrom
auto/bugfix/issue-116
Oct 15, 2026
Merged

stackbilt-admin merged 4 commits into
mainfrom
auto/bugfix/issue-116

Conversation

@stackbilt-admin

@stackbilt-admin stackbilt-admin commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Part of #116 (the hook-replay defect; the rest of the issue stays open).

Problem

LLMProviderFactory called the caller's raw hooks object inside the dispatch try. When onRequestEnd threw 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 as this. It is idempotent.
  • The factory and BaseProvider (direct adapters, including updateConfig({ hooks })) both guard hooks. Each failing hook method is logged once through the configured logger.
  • composeHooks() still captures handlers at compose time. It now contains async rejections and calls handlers with their implementation as this. Class-method handlers used to fail silently and now run; this is in the CHANGELOG.
  • A compile-time check ensures the hook-method list covers ObservabilityHooks.

Tests (src/__tests__/hook-isolation.test.ts, 20)

Each fix was reverted in turn to confirm its test fails:

  • Factory guard reverted: 6 fail. This includes a lone throwing onRequestEnd replaying on Groq, and rejecting hooks raising unhandled rejections (now checked inside that test rather than leaking into another).
  • Adapter guard reverted: the direct OpenAIProvider rejecting onSchemaDrift test fails.
  • Guard this binding reverted: the factory class-instance test and the composeHooks class test fail. guardHooks defining every method: the presence test fails.
  • Proxying the caller's object directly: the frozen and defineProperty tests fail. Removing the updateConfig re-guard: its test fails.
  • Removing a hook name from the list fails tsc.

Behaviour notes

Adapters built by the factory (e.g. from getProvider()) and adapters given hooks now 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, onCircuitStateChange and onBudgetThreshold (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

kovermier and others added 4 commits October 15, 2026 11:08
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]>
@stackbilt-admin
stackbilt-admin merged commit 59ce0bc into main Oct 15, 2026
2 checks passed
@stackbilt-admin
stackbilt-admin deleted the auto/bugfix/issue-116 branch October 15, 2026 17:34
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]>
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