Prompt-cache audit scripts
Feature docs index · Repository README
Purpose
Maintainer scripts under scripts/ check prompt-cache readiness without changing provider runtime code. The static audit is offline by default. The live checker requires explicit opt-in for networked cold/warm experiments.
Static audit: scripts/audit_prompt_cache.py
Use this script before and after prompt-cache provider work:
python3 scripts/audit_prompt_cache.py --repo .
python3 scripts/audit_prompt_cache.py --repo . --json > /tmp/magi-code-prompt-cache-audit.json
python3 scripts/audit_prompt_cache.py --repo . --strict
python3 scripts/audit_prompt_cache.py --self-test
Checks cover:
- Stable prompt prefix ordering in agent conversation assembly.
- Obvious ephemeral system-prompt inputs such as timestamps, random ids, session ids, auth/account ids, or cwd-specific runtime state.
- Provider request cache-scope plumbing.
- Custom Responses
prompt_cache_keysupport. - OpenAI Codex cache-scope evidence.
- Chat Completions streaming usage telemetry request.
- Cached-token and cache-write usage parsing.
- Local
ContextCacheseparation from provider prompt cache (N/Awhen no automatic recorder is found in agent source). - Custom provider classification via
use_responses_endpoint.
Status meanings:
| Status | Meaning |
|---|---|
PASS | Required static evidence exists. |
FAIL | Required implementation evidence is missing or contradicted. |
WARN | Evidence is incomplete, provider-specific, or optional outside strict mode. |
N/A | Check does not apply to the detected provider shape or local recorder. |
Exit codes:
| Exit | Meaning |
|---|---|
0 | Audit completed with no FAIL findings. WARN allowed unless --strict is set. |
1 | Audit completed with FAIL findings, or --strict promoted WARN to failing exit semantics. |
2 | Usage error, missing/unreadable source file, invalid fixture/body data, or self-test failure. |
--json emits one object with schema_version, repo, generated_at, overall_status, strict, and findings. Each finding includes stable fields such as id, status, severity, category, provider, file, symbol, evidence, expected, impact, suggested_fix, and references.
Use the audit output for findings on the checked revision rather than a fixed result count. context_cache.separation is N/A when no automatic disk snapshot recorder is found; retained local cache readers are not provider prompt-cache evidence. A codex.cache_scope failure means the audit did not find its recognized cache-scope field, not that live Codex caching fails.
Implemented static evidence includes:
request.cache_scope:ProviderRequestcarries a crate-private stable prompt cache key.responses.prompt_cache_key: custom Responses bodies emitprompt_cache_keywhen request scope exists.codex.cache_scope: the audit does not find its recognized cache-scope field in the Codex request body or headers; backend acceptance and scope remain unverified.usage.cached_tokens.chat: Chat Completionsprompt_tokens_details.cached_tokensmaps toUsage.cache_read.chat.stream_usage: streaming Chat Completions requests includestream_options.include_usageso providers can return usage telemetry.- Agent session runs derive bounded cache scope as
magi-code-session-{first_32_hex_sha256(session.id())}. Raw session ids, cwd, account data, timestamps, random values, and prompt text are not included.
Chat Completions bodies intentionally do not emit prompt_cache_key; only Responses-compatible request shapes receive that field.
Do not interpret PASS findings as proof of live provider cache hits. The static audit proves source evidence only; Codex cache scope remains unresolved.
Live checker: scripts/check_prompt_cache_live.py
Use this script only when you intentionally want a networked provider check:
python3 scripts/check_prompt_cache_live.py --help
python3 scripts/check_prompt_cache_live.py \
--url https://example.invalid \
--shape responses \
--body-file scripts/fixtures/prompt_cache_audit/live_offline_body.json
python3 scripts/check_prompt_cache_live.py \
--allow-network \
--url "$URL" \
--shape responses \
--body-file /tmp/prompt-cache-live-body.json \
--bearer-env OPENAI_API_KEY \
--repeat 2 \
--expect-warm-cache
Safety behavior:
- Network is denied unless
--allow-networkis present. - Without
--allow-network, send attempts exit2before reading secrets. - Secrets are read only from named environment variables such as
--bearer-envand--account-id-env. - Script never reads
~/.magi-code/auth.jsonor provider settings automatically. - Secret-bearing
--headernames such asauthorization,api-key, cookies, andchatgpt-account-idare rejected; use env flags instead. - Output redacts bearer tokens, API keys, cookies, tokens, and account ids.
- Request body is read once after the network gate, kept as bytes, and reused for each repeat. Output reports
body_sha256andbody_bytes. - JSON and SSE
data:responses are parsed for cached-token fields. Raw request headers are not logged.
Supported shapes:
| Shape | Usage fields checked |
|---|---|
responses | usage.input_tokens_details.cached_tokens, response.usage.input_tokens_details.cached_tokens, root and nested cache-write locations |
chat-completions | usage.prompt_tokens_details.cached_tokens, usage.prompt_tokens, usage.completion_tokens, usage.total_tokens |
codex | Responses-style SSE/JSON fields plus Codex headers supplied through safe env flags; cache-key backend contract remains unverified. |
Exit codes:
| Exit | Meaning |
|---|---|
0 | Invocation and expectations succeeded. |
2 | Usage, offline gate, body, header, or self-test error. |
3 | Network/auth/provider response failure, or --expect-warm-cache found no positive cached-token field on warm attempt. |
Verification
Credential-free checks:
python3 scripts/audit_prompt_cache.py --help
python3 scripts/check_prompt_cache_live.py --help
python3 scripts/audit_prompt_cache.py --self-test
python3 scripts/check_prompt_cache_live.py --self-test
python3 scripts/audit_prompt_cache.py --repo .
python3 scripts/audit_prompt_cache.py --repo . --strict
python3 scripts/audit_prompt_cache.py --repo . --json
Any FAIL finding makes both normal and strict audits exit 1. With no FAIL findings, WARN exits 0 normally and 1 under --strict. Static findings do not establish live provider cache behavior; missing recognized cache-scope evidence is not proof that live Codex caching fails.