Skip to content

ENG-15749: fix(agent): preserve per-turn provider session headers - #141

Merged
inassjunus merged 8 commits into
boonfrom
inas/eng-15749-smoke-refund-openclaw
Sep 3, 2026
Merged

inassjunus merged 8 commits into
boonfrom
inas/eng-15749-smoke-refund-openclaw

Conversation

@inassjunus

@inassjunus inassjunus commented Aug 21, 2026 •

Copy link
Copy Markdown
Collaborator

What Problem This Solves

Fixes an issue where a provisioned smoke turn could have its signed per-turn session identity overwritten by ordinary request attribution. The Gateway then rejected the otherwise valid smoke capability before the model request could run.

Why This Change Was Made

Canonicalize request header names case-insensitively across model resolution and Anthropic transport. Provider-authenticated per-turn headers take precedence over ordinary attribution only when both target the same header, preserving normal behavior for all other requests.

User Impact

Provisioning validation now reaches the tenant model route with the intended signed session context. Customer chat requests retain their normal attribution behavior.

Evidence

  • Focused SDK and transport regressions cover duplicate header casing and session precedence.
  • Typecheck and changed-file lint pass.
  • Local HTTPS end-to-end validation completed two Gateway model requests with 104 tokens fully refunded, no customer-visible usage or balance change, and settled refund logs hidden from usage.

Related: ENG-15749


Summary by cubic

Preserves signed provisioning smoke session headers so smoke turns reach the Gateway with the intended session and refund attribution intact. Previously, ordinary attribution could overwrite a smoke turn's signed X-Boon-Session-ID (e.g. via x-boon-session-id), causing Gateway rejection.

  • Merges request headers case-insensitively across SDK, model registry, and Anthropic transports; duplicate keys that differ only by case collapse to the later source.
  • Keeps normal precedence unchanged: attribution → auth/registry → runtime options. Static model headers don't override those for ordinary sessions; only signed smoke headers are reasserted over runtime attribution.
  • Restores X-Boon-Session-ID, X-Boon-Provisioning-Smoke-Run-ID, and X-Boon-Provisioning-Smoke-Capability together so refund attribution isn't lost.
  • Preserves provider-authenticated headers when casing differs, and diagnostic Boon usage headers no longer overwrite existing headers.
  • Keeps dynamic bearer Authorization ahead of any static model Authorization in the registry.
  • Threads explicit per-turn headers via GetReplyOptions.modelRequestHeaders through the embedded runner and applies them after cached model headers.
  • No migration for normal chat. Provisioning smoke tests set X-Boon-Session-ID to a provisioning-smoke-... value via modelRequestHeaders to reach the Gateway under the intended session, supporting ENG-15749.

Written for commit b53a072. Summary will update on new commits.

Review in cubic

@claude

claude Bot commented Aug 21, 2026

Copy link
Copy Markdown

Claude Code is working…

I'll analyze this and get back to you.

View job run

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 17 files

Heads up: you’re close to your included review allowance. Set a flex budget so reviews don’t pause.

Re-trigger cubic

Comment thread src/agents/sessions/sdk.ts Resolved
Comment thread src/llm/providers/anthropic.ts Resolved
Comment thread src/llm/providers/anthropic.ts Resolved
Comment thread src/agents/sessions/model-registry.ts Resolved
@inassjunus inassjunus changed the title fix(agent): preserve per-turn provider session headers ENG-15749: fix(agent): preserve per-turn provider session headers Aug 21, 2026

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 12 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="src/agents/sessions/sdk.ts">

<violation number="1" location="src/agents/sessions/sdk.ts:216">
P1: When a model carries any non-session model or per-turn header, this merge drops it from the outbound request; the smoke injector, for example, loses `X-Boon-Provisioning-Smoke-Run-ID`. Merge `modelHeaders` into the base headers, keeping runtime headers ahead of ordinary model headers, then reassert the signed smoke session.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Comment thread src/agents/sessions/sdk.ts Resolved
inassjunus and others added 5 commits August 26, 2026 21:38
The header-preservation fix for provisioning smoke turns also moved
model.headers to the end of the merge order in three places
(anthropic.ts's 5 createClient branches, model-registry.ts, sdk.ts),
letting a static model header win over dynamicHeaders/optionsHeaders/
resolved auth for every session, not just smoke ones. Restore the
pre-existing merge order everywhere; the narrow
preserveProvisioningSmokeSessionHeader reassertion still does the actual
job of protecting a smoke session's headers.

Also broadens that reassertion itself: it only ever restored
X-Boon-Session-ID, but the Gateway needs X-Boon-Provisioning-Smoke-Run-ID
and X-Boon-Provisioning-Smoke-Capability to travel with it too. A
stale/incomplete upstream header set could silently drop those two while
the session id alone survived, breaking refund attribution while looking
intact.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

@gandalfboon gandalfboon Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e2e review across ENG-15749 (this PR + boon#14260, boon-llm-gateway#85, anychat-boon-web#81).

The narrow fix does exactly what it says.

  • preserveProvisioningSmokeSessionHeader restores X-Boon-Session-ID — AND, correctly broadened this pass, X-Boon-Provisioning-Smoke-Run-ID + X-Boon-Provisioning-Smoke-Capability — from modelHeaders (Gateway/AnyChat-injected) when downstream attribution would otherwise overwrite them.
  • Reason this matters: the Gateway middleware requires all three headers together (session validated via signed capability), and boon-core's session-id refs a provisioning-smoke-… value. Ordinary attribution overwriting even one of the three breaks refund attribution silently while looking intact. The reassertion covers the whole triple.

The critical correctness point in the last commit. ENG-15749: restore per-session header precedence, keep smoke reassertion reverts a subtle regression that the earlier commits introduced: an intermediate revision moved model.headers to the END of the merge order in anthropic-transport-stream.ts (5 branches), model-registry.ts, and sdk.ts — which would have made a static model header win over dynamicHeaders/optionsHeaders/resolved auth for every session, not just smoke. Restoring the pre-existing merge order everywhere and letting the narrow reassertion do the actual smoke job is the right shape — ordinary sessions retain their runtime-attribution-wins precedence, only smoke sessions get their signed triple protected.

Model-registry change.

  • mergeTransportHeaders(model.headers, providerHeaders, modelHeaders) then preserveProvisioningSmokeSessionHeader — clean.
  • Bearer Authorization is now correctly merged last (mergeTransportHeaders(headers, { Authorization: Bearer ${apiKey} })) so a dynamic bearer beats a static model Authorization — regression test in place.
  • The subtle spec commentary in model-registry.test.ts about why the model.headers-not-registered variant tests the source-order regression is worth its weight; that's the exact class of bug this PR was created to prevent.

Per-turn plumbing. applyModelRequestHeaders threads params.modelRequestHeaders from RunEmbeddedAgentParams → runEmbeddedAttemptWithBackend → params.model clone at the top of runEmbeddedAttempt. No mutation of the passed-in model object ({ ...model, headers: mergeTransportHeaders(model.headers, headers) }), and the identity short-circuit (if (!headers) return model) avoids unnecessary allocations.

Boon usage headers guard. withBoonUsageHeaders now skips any header whose lowercase name already exists — no accidental overwrite of an explicitly-set X-Boon-Session-ID from the smoke path.

Regression surface.

  • Only case-insensitive merge behaviour changes, and the smoke-triple reassertion is opt-in (empty modelHeaders → identity). Every existing precedence test is preserved.
  • Anthropic transport: 4 branches now use mergeAnthropicRequestHeaders, all with the same shape.
  • sdk.ts and anthropic-transport-stream.ts tests explicitly cover: "preserves an explicit smoke session over runtime attribution" + "keeps ordinary runtime attribution ahead of an ordinary model session" — the exact two regressions this could have introduced.

Tests. 118 checks green, 24 skipped, 2 neutral — no failures. test: cover model request header propagation adds the end-to-end propagation test.

Secrets clean. Only test fixtures.

Ship it. 🧙

@inassjunus
inassjunus merged commit c3ead27 into boon Sep 3, 2026
144 checks passed
@inassjunus
inassjunus deleted the inas/eng-15749-smoke-refund-openclaw branch September 3, 2026 12:26
insomnius added a commit that referenced this pull request Sep 3, 2026
…191)

Carries #190 (ENG-19115) — forward run.oboToken into the embedded runner
params so x-boon-gateway-obo-token is actually emitted and the tier-1
signed-OBO meter-skip fires — and #141 (ENG-15749) preserve per-turn
provider session headers. Fork gateway + @openclaw/slack + @openclaw/msteams
+ @openclaw/diagnostics-prometheus bumped in lockstep via release:prep;
CHANGELOG.md gains the 2026.6.11-boon.35 section the prepack requires.
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.

1 participant