feat(client): forward APIMUX_CALLER_CTX_* env vars as X-Apimux-Caller-Ctx-* headers#16
Merged
Merged
Conversation
…-Ctx-* headers apimux-service collects any X-Apimux-Caller-Ctx-* header into the per-call caller_context jsonb that drives the billing-webhook fan-out. The CLI must participate transparently so subprocess callers (kamay-agent, future partners) can inject attribution context without depending on CLI version upgrades when their schema evolves. Scan the process environment for the APIMUX_CALLER_CTX_ prefix and copy each non-empty value to the matching header (trimmed; whitespace-only treated as absent). The prefix → header mapping is intentionally generic — this CLI has no compile-time knowledge of which keys downstreams use.
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.
Summary
apimux's billing-webhook fan-out (kamay/apimux!69) only fires when the request carries at least one
X-Apimux-Caller-Ctx-*header. The CLI must participate transparently so subprocess callers (kamay-agent's Claude Bash tool, future partners) can inject attribution context without depending on CLI version upgrades when their schema evolves.Scan the process environment for the
APIMUX_CALLER_CTX_prefix and copy each non-empty value to the matching header. The prefix → header mapping is intentionally generic — this CLI has no compile-time knowledge of which keys downstreams use; apimux groups them into a single opaquecaller_contextjsonb on its side.Wire shape
Whitespace-only values are dropped (treated as "header absent"); apimux records SQL NULL when no headers are sent.
Test plan
go test ./internal/client/...— five new tests covering full set / empty set / partial / whitespace trim / arbitrary unknown keyapimux reddit search --query xwithAPIMUX_CALLER_CTX_USER_ID=usr_via_cli APIMUX_CALLER_CTX_RUN_TAG=cli-smoke-1, verifiedapi_call_logs.caller_context = {"user_id":"usr_via_cli","run_tag":"cli-smoke-1",...}and the webhook receiver got the matching nestedcaller_contextobjectRollout coupling
This PR is not consumed in isolation:
APIMUX_CALLER_CTX_*env vars when spawning the Claude SDK (their MR coming separately)Each can ship out of order safely: missing headers → apimux just records SQL NULL → behavior identical to no-webhook today.