Skip to content

feat(harness): opt-in SH_PROMPT_CACHE / SH_GATEWAY_CACHE switches to turn caching off (e.g. for benchmarking and performance analysis) - #846

Merged
webchang merged 1 commit into
rossoctl:mainfrom
webchang:pr/i754-cache-switches
Nov 17, 2026
Merged

webchang merged 1 commit into
rossoctl:mainfrom
webchang:pr/i754-cache-switches

Conversation

@webchang

Copy link
Copy Markdown
Member

Closes #754

An opt-in feature to turn prompt and gateway caching off, for example for benchmarking and performance analysis: runs that re-send the same prompts to compare configurations need model requests free of caching effects. Default unchanged; both switches are deployment-wide operator settings.

What changed and why

  • Why. When the same prompts are re-run to compare two configurations, cached prefixes move tokens into the cache-read class and change the timings from one run to the next, and a caching gateway can answer a byte-identical request from its response cache (same message id, no model call), so repeated reps stop being independent samples. Neither could be turned off: pi-ai's providers always mark the system prompt, the last tool and the last user message with cache_control, and PI_CACHE_RETENTION only knows long.
  • SH_PROMPT_CACHE=off strips Anthropic cache_control markers from the agent's model requests: the system blocks, the tools, and every message's content blocks, one level into a tool_result. A tool's input_schema is never walked, so a schema property named cache_control survives. It covers the OpenAI-compatible provider's Anthropic-style markers too.
  • SH_GATEWAY_CACHE=off adds cache: {"no-cache": true} to every such request body, so a caching gateway skips its response-cache lookup. An existing cache object keeps its other keys. It is meant for an endpoint that is a gateway reading that field; a provider's own API may reject the unknown field, and the docs say so.
  • Default unchanged. With both unset the extension registers no before_provider_request handler, and pi installs its payload hook only when some extension has one, so requests are byte-for-byte what they were. The two switches are independent.
  • Strict values. off, on and unset (or blank) are accepted; anything else (OFF, false, off) throws, naming the switch, like the other enum switches (SH_ROUTING_POLICY, SH_MODEL_API). A run that set OFF and silently kept caching would compare the wrong thing. The P6 worker checks both at boot next to the tier checks (packages/server/src/worker.ts), so a typo exits 2 before ready. The factory reads them again when a turn's stack is built, outside pi's loader (which records a factory's throw as a load error and carries on), so a turn built outside the worker fails instead of running cached.
  • Where it applies. harness/src/prompt-cache-extension.ts is a harness-internal extension factory, like toolChoiceExtension, not an extension file a user could load or drop. It is on the executeTurn stack (sync, streamed and detached /turn, and prompt leaves) and on the solve and verdict leaves' own stacks in harness/src/run-leaf.ts. MI1 R4's server-mode noExtensions turns off discovered extension files only, so the switches apply in server mode too; the tests run in server mode on purpose.
  • Multi-tenancy. These are deployment-wide operator switches read from the worker's environment, not per-session or per-tenant settings: under MOCA_TENANCY=multi they apply to every tenant's turns alike, and nothing a client sends can set or clear them.
  • Docs. Both variables are in docs/reference/configuration.md (Worker: turns, model and budget), with a paragraph on the purpose, scope, server mode and tenancy. docs/api/CHANGELOG.md is the API changelog and nothing on the wire changes, so it has no entry.

Tests

  • harness/test/prompt-cache-extension.test.ts (16): value parsing (accepted values; OFF, Off, off, false, 0, none, disabled refused for each switch); inert with both unset, blank or on; a refused value throws when the factory is built, not when pi runs it; marker stripping on Anthropic and OpenAI-compatible payloads; schema property kept; marker count; log once; SH_GATEWAY_CACHE=off alone leaves the markers; both together; bypassGatewayCache keeps other keys, overrides no-cache: false, replaces non-object values.
  • harness/test/run-turn-cache-switches.test.ts (4), the wiring through executeTurn in server mode: the fake loader records extensionFactories, every factory runs against a fake pi, and the collected before_provider_request handlers are applied in order to a provider payload, as pi's onPayload does. Cases: both unset, payload JSON identical; SH_PROMPT_CACHE=off, no cache_control and nothing else changed; SH_GATEWAY_CACHE=off, cache.no-cache added and the rest identical; a refused value fails the turn before the loader is built.
  • harness/test/run-leaf.test.ts (+6): the same three cases for the solve leaf and the verdict leaf, in server mode.
  • packages/server/test/worker-boot.test.ts (+3): the forked worker exits 2 before ready on SH_PROMPT_CACHE=OFF and on SH_GATEWAY_CACHE=false, and boots to ready with both off.

Without the wiring (the new module kept, the changes to run-turn.ts, run-leaf.ts and worker.ts reverted), the switch tests fail and the default-unchanged tests pass, as they should:

  • harness: 7 failed | 3 passed (the turn's SH_PROMPT_CACHE=off, SH_GATEWAY_CACHE=off and refused-value cases; the solve and verdict leaves' two switch cases each);
  • server: 2 failed | 1 passed (both boot refusals).

With the change:

  • harness suite: 72 passed | 2 skipped files, 904 passed | 5 skipped tests; tsc --noEmit clean;
  • @moca/server suite: 33 passed files, 585 passed tests; tsc --noEmit clean;
  • python3 scripts/check-doc-links.py: 212 files, 0 broken links;
  • pre-commit (prettier, secrets) passed.

Out of scope

  • A compaction's summary request: pi sends it through streamFn without the agent loop's onPayload, so it does not reach the before_provider_request hook; it keeps its markers and carries no no-cache. Covering it would need a change in pi.
  • OpenAI's own automatic prefix caching: pi's OpenAI-compatible provider may send a prompt_cache_key, and that provider-side caching is not something these switches change, so SH_PROMPT_CACHE covers cache_control markers only.
  • A per-session or per-request control. These are operator switches by design; a per-session form would be a config-bundle or API change to discuss separately.

Every commit is signed off (DCO).

Assisted-By: Claude Code

…turn caching off

An opt-in feature for runs that need model requests free of caching effects, for example
benchmarking and performance analysis: when the same prompts are re-run to compare two
configurations, cached prefixes move tokens into the cache-read class and change the timings, and
a caching gateway can answer a byte-identical request from its response cache without calling the
model. Neither could be turned off: pi-ai always marks the system prompt, the last tool and the
last user message, and PI_CACHE_RETENTION only knows "long".

- SH_PROMPT_CACHE=off strips Anthropic cache_control markers from the agent's model requests
  (system blocks, tools, message content, one level into a tool_result); a tool's input_schema is
  never walked.
- SH_GATEWAY_CACHE=off adds cache: {"no-cache": true} to every such request body, keeping an
  existing cache object's other keys.
- Both unset: no handler is registered, so requests are byte-for-byte unchanged. Each switch works
  on its own. Values other than off, on or unset refuse the worker's boot (and fail a turn built
  outside the worker), like the other enum switches.

promptCacheExtension is a harness-internal extension factory on the executeTurn stack and on the
solve and verdict leaves' own stacks, so it applies in server mode too (noExtensions only turns off
discovered extension files). These are deployment-wide operator switches; the configuration
reference says so. A compaction's summary request is sent by pi outside the payload hook and is
not covered.

Closes rossoctl#754

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
@webchang
webchang requested a review from pdettori as a code owner November 17, 2026 03:08
@pdettori pdettori added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 17, 2026

@clawgenti clawgenti 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.

Summary

Well-structured opt-in feature: the extension registers no before_provider_request handler with both switches unset (so default requests stay byte-for-byte identical), values are validated strictly at worker boot and again at stack build, and the wiring is covered at all three seams (turn stack, solve/verdict leaf stacks, worker boot), including in server mode to prove noExtensions doesn't disable it. The input_schema carve-out in stripCacheControl and the honest out-of-scope notes (compaction summary requests, OpenAI's own prefix caching) show the limits are understood rather than papered over. One suggestion below; otherwise clean.

Author: webchang (MEMBER — maintainer)
Areas reviewed: TypeScript (harness, server), docs
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, all signed-off: yes
CI status: passing

Generated by RepoMan github-pr-review@235a7f7 (https://github.com/rossoctl/agent-skills/blob/235a7f7620139ee3e4ab0872b8439591fe52e8c1/skills/github-pr-review/SKILL.md).

const strip = (block: unknown) => {
if (block && typeof block === 'object' && 'cache_control' in block) {
delete (block as Record<string, unknown>).cache_control;
n += 1;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion — readCacheSwitches is called when the factory is built (once per stack), so a long-running worker keeps the value it booted with; that matches the boot-check design, but it also means SH_PROMPT_CACHE=off set in the worker's ambient environment after boot would apply to CLI-built turns and not worker turns. The docs say "deployment-wide operator switches read from the worker's environment", which is accurate — this is only a note that a per-stack re-read (reading env inside the handler instead of at build time) would make the two paths agree if that ever matters. No change needed now; the current design's fail-fast-on-typo behavior is the better trade-off for benchmarking use.

@ingpaolodettori-dev ingpaolodettori-dev 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.

This adds opt-in, deployment-wide SH_PROMPT_CACHE=off (strips cache_control markers) and SH_GATEWAY_CACHE=off (adds cache: {"no-cache": true}) through a harness-internal before_provider_request extension factory. It is wired into the executeTurn stack and the solve and verdict leaves, and the worker validates both at boot.

I checked it against pi-fork 2c28be5:

  • The strip covers every place pi-ai sets a marker. On Anthropic that is the system blocks, the last tool, and the last block of the final user message, including a tool_result. On OpenAI-compatible it is the system/developer text, the last tool, and the last user/assistant text part.
  • onPayload runs after buildParams in both providers, so the in-place edit plus return takes effect.
  • Factories bypass the server-mode noExtensions.
  • With both switches unset the factory registers nothing, so the default path is unchanged.
  • Validating at boot, and again whenever a turn's stack is built, matches the existing SH_SANDBOX_TIERS pattern.
  • The tests restore the env and cover the wiring as well as the helpers, with the revert evidence in the body.

Not blocking: with SH_PROMPT_CACHE=off, pi still sends x-session-affinity (when the compat flag is set) and prompt_cache_key on api.openai.com, because only cacheRetention: "none" drops those. The body already scopes the switch to cache_control markers. A one-line mention in configuration.md would help anyone benchmarking through an OpenAI-compatible endpoint.

Author: webchang (MEMBER, maintainer)
Areas reviewed: TypeScript (harness, server worker), tests, docs
Agent/IDE config (.claude/.vscode): none
Commits: 1, signed off
CI status: all 17 checks passing

Assisted-By: Claude Code

| `SH_MODEL_MAX_TOKENS` | `8192` | | Custom model max tokens |
| `SH_BUDGET_TOKENS`, `SH_BUDGET_MARGIN` | none | Applied only when a positive number | Token budget and margin |
| `SH_PROMPT_CACHE` | unset (caching on) | Opt-in, for example for benchmarking and performance analysis. `off` or `on`; any other value (`OFF` too) refuses to boot. Deployment-wide (below) | `off` strips Anthropic prompt-caching `cache_control` markers from the agent's model requests |
| `SH_GATEWAY_CACHE` | unset (caching on) | Opt-in, as above; independent of `SH_PROMPT_CACHE`, same values. Only for a caching gateway that reads the field; a provider's own API may reject it | `off` adds `cache: {"no-cache": true}` to the agent's model requests, bypassing the response cache |

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: "a caching gateway that reads the field" doesn't say which gateway. cache: {"no-cache": true} is LiteLLM's per-request cache control; other gateways use different controls, such as Portkey's x-portkey-cache-force-refresh header. Naming LiteLLM here and in the module doc would tell operators whether the switch does anything for their endpoint.

@webchang
webchang merged commit bf6476e into rossoctl:main Nov 17, 2026
17 checks passed

@clawgenti clawgenti 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.

webchang added a commit to webchang/moca that referenced this pull request Nov 17, 2026
Brings rossoctl#845 (vmpool: the deferred-reap gate test's select is
deterministic) and rossoctl#846 (harness: opt-in SH_PROMPT_CACHE /
SH_GATEWAY_CACHE switches turn turn caching off). No conflicts.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
@pdettori pdettori added this to the v0.11.0 milestone Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ai-review Request automated AI code review from clawgenti

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Let a benchmark turn off prompt and gateway caching for repeatable turns (a question)

4 participants