Skip to content

prompt_stamp: make the injected UserPromptSubmit line the user's choice - #304

Merged
fdaviddpt merged 1 commit into
mainfrom
fix/301-cache-stable-prompt-stamp
Aug 4, 2026
Merged

fdaviddpt merged 1 commit into
mainfrom
fix/301-cache-stable-prompt-stamp

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #301.

user-prompt-hook.sh prints [HH:MM TZ — user — 45%] to stdout on every UserPromptSubmit, and Claude Code injects that into the model's context. The reporter asked for a way to stop emitting bytes that change between turns for reasons unrelated to what was typed.

What ships

A prompt_stamp config key, three values:

value emits note
full [14:30 CEST — jack — 45%] default — byte-for-byte what every previous release printed
stable [jack] plus the >= 95 context warning
off nothing warning included

An unrecognised value reads as full: a typo must not silently delete the clock.

Resolved in log.sh beside REMEMBER_TZ, off the already-built config table, carried on the env cache and replayed by the hook — the #227 design, so the hook still resolves nothing itself.

Why not just drop the clock

The issue proposes keeping [$(whoami)] and the context percentage, on the grounds that both are byte-stable between turns. The username is; the percentage is not. CTX_PCT climbs on every prompt for anyone running the status line — which the hook's own comment at line 141 records as the common case, not the rare one. Dropping only the clock would ship the same defect under a shorter name. stable drops both.

Hour granularity (the issue's option 3) was declined for the same reason — still volatile, just slower — and because it would introduce a third timestamp format that has to stay byte-identical across bash 3.2's date and 4.2's printf '%(...)T', a cost #227 already paid once.

Why stable keeps the warning and off does not

That is the entire difference between the two modes. The >= 95 warning is threshold-gated, so it changes bytes only when it changes behaviour, and it is the only line here anybody acts on. But off has to mean off: a user who asked for silence and got a surprise line near the context limit is a user back to filtering our stdout, which is what this option exists to retire.

On the reported mechanism

The measurement in the issue is the reporter's, from a self-hosted gateway, and this PR does not rest on it. UserPromptSubmit output lands at the tail of the prompt and is frozen into history once emitted; whether that defeats a given gateway's prefix cache is a property of that gateway. The argument for shipping is the interface one: a hook should not force operators to regex-rewrite its stdout in flight.

Cost

No fork added; two removed. Measured under /bin/bash 3.2.57 with PATH-shim spawn counting, warm ×5: full and default = 1 (date, unchanged), stable and off = 0. Cold 10 → 9. The extra config read costs nothing measurable — the cold count for full is identical to the untouched default.

Cache versioning

An env cache written before this key existed is refused, not defaulted. Invalidation is by config mtime and never by plugin version, so reading the absence of the key as "the default" would serve a resolution that never considered the option. Cost is one slow prompt, once, at upgrade.

Tests

TDD. RED, against zero implementation:

10 failed, 4 passed in 7.26s

The 4 green are deliberate — three default-preservation assertions and the spawn budget, which can only ever fail by this change moving something it promised not to move. Everything asserting new behaviour was red.

GREEN: 14 passed in 5.32s. Full suite, run twice: 1445 passed, 43 skipped, coverage 94.26%.

The tests drive the real script through a subprocess and assert its stdout bytes, including a test that the default is byte-identical and one that stable renders the same under different context percentages.

Docs

README.md config table, config.example.json (value + annotation), CHANGELOG.md.

…hoice (#301)

user-prompt-hook.sh has always printed [14:30 CEST — jack — 45%] into the
model's context on every UserPromptSubmit, with no way to turn it down. The
reporter's workaround is rewriting this hook's stdout in flight with a regex.

Three modes, default byte-for-byte unchanged:
  full   (default) [14:30 CEST — jack — 45%]
  stable           [jack] + the >=95 context warning
  off              nothing, warning included
An unrecognised value reads as full.

The issue's option 2 (drop the clock, keep user + percentage) does not fix
the reported problem: the percentage climbs on every prompt for anyone
running the status line, which the hook's own comment records as the common
case. stable drops both volatile fields. Hour granularity is declined —
still volatile, plus a third timestamp format to hold byte-identical across
bash 3.2's date and 4.2's printf '%(...)T'.

No process added. Resolved by log.sh off the already-built config table and
replayed by lib-env-cache.sh, the #227 design. Measured on /bin/bash 3.2.57,
warm run x5: full 1 spawn (unchanged), stable and off 0.

A cache written before the key existed is refused rather than defaulted —
the env cache is invalidated by config mtime, never by plugin version.

The reporter's prefix-cache measurement is theirs and is not claimed here.

Co-Authored-By: Max <noreply>
@fdaviddpt
fdaviddpt merged commit 427bb70 into main Aug 4, 2026
12 checks passed
@fdaviddpt
fdaviddpt deleted the fix/301-cache-stable-prompt-stamp branch August 4, 2026 11:31
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.

user-prompt-hook's per-turn timestamp defeats prompt prefix caching — add a toggle or drop the clock

1 participant