Repository navigation
feat(harness): opt-in SH_PROMPT_CACHE / SH_GATEWAY_CACHE switches to turn caching off (e.g. for benchmarking and performance analysis) - #846
Conversation
…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]>
clawgenti
left a comment
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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. onPayloadruns afterbuildParamsin 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_TIERSpattern. - 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 | |
There was a problem hiding this comment.
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.
clawgenti
left a comment
There was a problem hiding this comment.
Review record for this cycle.
Generated by RepoMan github-pr-review@235a7f7 (https://github.com/rossoctl/agent-skills/blob/235a7f7620139ee3e4ab0872b8439591fe52e8c1/skills/github-pr-review/SKILL.md).
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]>
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
cache_control, andPI_CACHE_RETENTIONonly knowslong.SH_PROMPT_CACHE=offstrips Anthropiccache_controlmarkers from the agent's model requests: the system blocks, the tools, and every message's content blocks, one level into atool_result. A tool'sinput_schemais never walked, so a schema property namedcache_controlsurvives. It covers the OpenAI-compatible provider's Anthropic-style markers too.SH_GATEWAY_CACHE=offaddscache: {"no-cache": true}to every such request body, so a caching gateway skips its response-cache lookup. An existingcacheobject 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.before_provider_requesthandler, 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.off,onand 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 setOFFand 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 beforeready. 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.harness/src/prompt-cache-extension.tsis a harness-internal extension factory, liketoolChoiceExtension, not an extension file a user could load or drop. It is on theexecuteTurnstack (sync, streamed and detached/turn, and prompt leaves) and on the solve and verdict leaves' own stacks inharness/src/run-leaf.ts. MI1 R4's server-modenoExtensionsturns off discovered extension files only, so the switches apply in server mode too; the tests run in server mode on purpose.MOCA_TENANCY=multithey apply to every tenant's turns alike, and nothing a client sends can set or clear them.docs/reference/configuration.md(Worker: turns, model and budget), with a paragraph on the purpose, scope, server mode and tenancy.docs/api/CHANGELOG.mdis 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,disabledrefused for each switch); inert with both unset, blank oron; 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=offalone leaves the markers; both together;bypassGatewayCachekeeps other keys, overridesno-cache: false, replaces non-object values.harness/test/run-turn-cache-switches.test.ts(4), the wiring throughexecuteTurnin server mode: the fake loader recordsextensionFactories, every factory runs against a fakepi, and the collectedbefore_provider_requesthandlers are applied in order to a provider payload, as pi'sonPayloaddoes. Cases: both unset, payload JSON identical;SH_PROMPT_CACHE=off, nocache_controland nothing else changed;SH_GATEWAY_CACHE=off,cache.no-cacheadded 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 beforereadyonSH_PROMPT_CACHE=OFFand onSH_GATEWAY_CACHE=false, and boots toreadywith bothoff.Without the wiring (the new module kept, the changes to
run-turn.ts,run-leaf.tsandworker.tsreverted), the switch tests fail and the default-unchanged tests pass, as they should:7 failed | 3 passed(the turn'sSH_PROMPT_CACHE=off,SH_GATEWAY_CACHE=offand refused-value cases; the solve and verdict leaves' two switch cases each);2 failed | 1 passed(both boot refusals).With the change:
72 passed | 2 skippedfiles,904 passed | 5 skippedtests;tsc --noEmitclean;@moca/serversuite:33 passedfiles,585 passedtests;tsc --noEmitclean;python3 scripts/check-doc-links.py: 212 files, 0 broken links;Out of scope
streamFnwithout the agent loop'sonPayload, so it does not reach thebefore_provider_requesthook; it keeps its markers and carries nono-cache. Covering it would need a change in pi.prompt_cache_key, and that provider-side caching is not something these switches change, soSH_PROMPT_CACHEcoverscache_controlmarkers only.Every commit is signed off (DCO).
Assisted-By: Claude Code