Skip to content

fix: freeze memory metadata timestamps in system prompt head - #20

Open
Ghilteras wants to merge 1 commit into
joshuadavidthomas:mainfrom
Ghilteras:fix/freeze-metadata-timestamp
Open

fix: freeze memory metadata timestamps in system prompt head#20
Ghilteras wants to merge 1 commit into
joshuadavidthomas:mainfrom
Ghilteras:fix/freeze-metadata-timestamp

Conversation

@Ghilteras

Copy link
Copy Markdown

Problem

renderMemoryMetadata() emits new Date().toISOString() (millisecond precision) on every request. The system.transform hook splices it at index 1 of the system prompt. Provider prompt caching is prefix-based: a changing byte near the top means the cache never matches past it — the entire conversation re-sends as fresh input every turn.

Measured impact

In production (opencode + DeepSeek): cache hit ratio collapsed from ~94% to ~15%, daily cost ~13x. Fingerprint: cache_read pinned to the static base prompt size for every message.

Fix

Freeze the metadata block — no dynamic timestamps. opencode already injects the current date in the env block, so this loses no information. Memory blocks themselves still appear in the prompt and change when written (memory_set/replace), which is the intended cache-busting event; the per-request timestamp was not.

Test

Replaced the timestamp-asserting test with a stability regression test (two render calls → identical output). All tests + typecheck pass.

Fixes #8. Related: #14.

renderMemoryMetadata() emitted new Date().toISOString() on every request,
spliced at index 1 of the system prompt. Provider prompt caching requires
a byte-identical prefix: the millisecond timestamp made the cache never
match past the injection point, so the full conversation was re-sent as
fresh input on every turn (measured: cache hit 94% -> 15%, ~10x cost).

Fixes #8, closes the issue raised in #14.
@Ghilteras
Ghilteras force-pushed the fix/freeze-metadata-timestamp branch from 90ebe80 to 8faac95 Compare August 3, 2026 06:14
@Ghilteras

Copy link
Copy Markdown
Author

@joshuadavidthomas can we unblock this

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

System prompt injection busts Anthropic prompt cache on every turn

1 participant