Skip to content

feat(llm): add opt-in provider cache affinity - #750

Open
renaesop wants to merge 1 commit into
BaaRai:mainfrom
renaesop:codex/maas-cache-affinity
Open

renaesop wants to merge 1 commit into
BaaRai:mainfrom
renaesop:codex/maas-cache-affinity

Conversation

@renaesop

@renaesop renaesop commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Known gateways use different cache-routing hints, and retrying an expired TokenHub binding with the same routing ID cannot repair it. This PR adds the provider-layer resolver and bounded recovery mechanism. Callers opt in through agent.Config.Options; existing profile, environment and connection-test paths keep their legacy behavior until the separate configuration integration supplies an explicit policy.

Scope

This is the provider subsystem split requested in the review: 8 files, +938/-10 lines, rebased onto main 2f065a958e4bc84e300ece4af501a5cdd3ed08cd.

  • Resolve verified endpoint defaults from parsed HTTPS host, port, path and wire format, with independent automatic/custom/off options. Unknown endpoints receive no automatic hints.
  • Preserve zero-option callers: an empty session header remains disabled, and existing custom headers retain their original behavior.
  • Add stable, request-scoped prompt_cache_key values for supported Chat/Responses policies.
  • Recover only TokenHub HTTP 410 with structured code 410001, once per logical invocation, including SDK retries. Require a live context, replayable body and eligible generation request.
  • Keep routing bindings bounded and preserve stricter redirect policies. Recovery changes only the routing header; the request body, logical conversation, deadline, cache key and output-token limit remain intact.
  • Document the provider contract and endpoint evidence. Tests stay with this subsystem.

Dependent subsystem drafts

The remaining work is now published for visibility:

  1. #897 — Configuration backend: migration, persistence, API compatibility and activation, with tests and documentation. Its own layer is 10 files.
  2. #898 — Settings UI and preview: controls, authenticated read-only preview, all five locale translations and tests. Its own layer is 13 files.

Both are dependency drafts targeting main because the contributor cannot create upstream branches for a cross-fork stack. Their current PR diffs include their predecessors (17 and 28 cumulative files). After each predecessor merges, rebase the next draft onto main and remove the inherited commits before requesting review; this brings each PR down to its own subsystem's review size.

All three branches have been rebased onto main after its route-permission refactor. The preview uses the new authenticated route registration and has an end-to-end session test.

Validation

On this provider-only commit:

  • go test -race ./agent ./llmpolicy -count=1 with an isolated PostgreSQL database: 584 tests/subtests passed; one existing root-only filesystem test skipped because the local runner is unprivileged.
  • go test -race github.com/Autumn-27/norma/llm -count=1: 91 tests/subtests passed.
  • go build ./..., go vet ./..., and cross-builds for Linux amd64, Windows amd64, macOS amd64 and the native macOS arm64 target passed.
  • Regression coverage includes zero-option compatibility, streaming/non-streaming requests, zero and explicit output caps, Chat max_tokens/max_completion_tokens, Responses max_output_tokens, bounded recovery, ineligible requests, concurrent routing, expiry and redirect policy preservation.
  • No live provider calls or deployment were used for validation.

SDK merge prerequisite

BaaRai/Norma#15 remains open. The provider layer needs its CompletionRequest.PromptCacheKey and Config.ResolvedBaseURL() APIs. The review branch retains the publicly fetchable immutable pin github.com/renaesop/Norma v0.4.14-0.20261201033148-d14bb979d978 to build independently. Replace it with an upstream BaaRai/Norma release containing those APIs before merging.

@renaesop
renaesop force-pushed the codex/maas-cache-affinity branch 5 times, most recently from 4535d59 to 9ea4691 Compare December 2, 2026 14:15
@BaaRai

BaaRai commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Thanks for the work. This PR is 1,672 lines across 27 files, above the review threshold we can apply line by line (2,500 lines or 15 files). I am parking it for the current wave rather than closing it: the PR stays open and the work is kept.

To get it reviewed, please split it by subsystem into PRs under that threshold. For this one, a natural split is: provider detection, config and UI, tests.

If the feature is already on the roadmap, link the tracking issue and we will pick it up there.

@BaaRai BaaRai added the feature label Dec 2, 2026
@renaesop
renaesop force-pushed the codex/maas-cache-affinity branch from 9ea4691 to abf9422 Compare December 3, 2026 00:59
@renaesop renaesop changed the title feat(llm): auto-configure provider cache affinity feat(llm): add opt-in provider cache affinity Dec 3, 2026
@renaesop

renaesop commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Following the request to split by subsystem, the original change is now separated into three review units, in dependency order:

  1. feat(llm): add opt-in provider cache affinity #750 — Provider capability: endpoint-policy resolution, request cache keys and bounded expired-session recovery. Current head: addefea8; 8 files, +938/-10.
  2. feat(llm): persist provider affinity settings (2/3) #897 — Configuration backend: schema migration, persistence, API compatibility and activation of the provider policy. Its own layer is 10 files, +515/-75 (layer-only diff).
  3. feat(web): configure and preview provider affinity (3/3) #898 — Settings UI and preview: controls, a read-only policy preview and translations. Its own layer is 13 files, +547/-21 (layer-only diff).

The key boundary is providing the capability first, then explicitly activating it through configuration. In #750, zero agent.Config.Options preserves the old provider path: an empty header remains disabled, and an existing custom header keeps its original behavior. #897 normalizes persisted/API settings before enabling the new policy. This avoids a partially integrated version changing an older client's explicit “clear header” operation into automatic routing.

Regression tests stay with the subsystem they exercise, so the request behavior, migration semantics and UI-facing API can each be reviewed with their verification. The preview handler belongs with #898 because it serves draft UI inputs without loading or saving a profile. Shared files receive only the changes needed by each layer.

The complete file scope of this PR (#750) is:

File Responsibility
llmpolicy/policy.go Pure endpoint-policy resolution and option validation.
llmpolicy/policy_test.go Endpoint matching, overrides and invalid settings.
agent/provider.go Opt-in integration while preserving zero-option callers.
agent/session_affinity.go Request cache keys, header routing and bounded recovery.
agent/session_affinity_test.go Legacy compatibility, streaming, token caps, recovery guards, concurrency and redirects.
go.mod Pin the SDK APIs required by this provider layer.
go.sum Checksums for that SDK revision.
docs/maas-cache-affinity.md Provider contract, endpoint evidence and integration boundaries.

Draft/base limitation: #897 and #898 currently target main and include their predecessors, so GitHub currently shows 17 and 28 cumulative files, respectively. Their base cannot be a branch in the contributor fork, and I cannot create base branches in the upstream repository. They remain dependency drafts; the cumulative diffs are not yet within the review-size limit. After #750 merges, rebase only the configuration layer onto updated main; after that merges, do the same for the UI layer before requesting review.

All three branches are based on main 2f065a95. Local verification of #750 passed: 584 Agent/policy tests and subtests under -race (one existing root-only test skipped), build/vet and the release-platform cross-builds. The pinned SDK's 91 LLM tests also passed. The tests cover zero and explicit output-token caps, including streaming, without changing the configured limits.

The upstream release containing BaaRai/Norma#15 — feat(llm): add request-scoped prompt cache keys is still a merge prerequisite; the fork pin must be replaced before merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants