Repository navigation
Inject the current date/time per turn, after the cache breakpoint - #543
Conversation
PasClaw never told the model what time it is. Dates leaked in only
incidentally and conditionally -- as a memory-file heading when that day's
file happened to exist, and as an example filename in the memory rule -- so
a stock run left the model falling back to its training cutoff. That
silently corrupts date arithmetic, "is this still current" judgements,
relative-time reasoning, and anything schedule-shaped.
WHERE it goes is the part with a cost attached. The system block carries
the cache_control breakpoint (PasClaw.Providers.Anthropic.BuildRequest),
so a clock in the system prompt would invalidate the provider prefix cache
on every single call -- the biggest single-breakpoint win in the request.
The progress ledger already states that constraint for itself in as many
words: "NO counters, no timestamps". The messages array sits after both
the system and last-tool breakpoints, so a value that changes every turn
costs nothing there.
Two traps in the message array itself, both avoided by extending the
trailing user message rather than adding one:
- a trailing mrSystem message is HOISTED into the system prompt by the
Anthropic provider, which scans Messages for the first mrSystem when
Options.SystemPrompt is empty -- straight back into the cached prefix;
- a fresh mrUser message appended after tool results would sit between a
tool_use and its tool_result.
The caller's history is never mutated, so persisted transcripts do not
accumulate a stamp per turn. The clock is therefore fresh once per turn
rather than per iteration: within a turn it moves by seconds to minutes
while the model works, which is below the resolution any of this is used
at, and it keeps tool iterations byte-stable for providers that cache
further in.
NowStampWithZone gives local wall clock, the UTC offset, and the UTC
equivalent, e.g. "Fri 2026-08-14 03:38:39 UTC+00:00 (2026-08-14T03:38:39Z)".
Local alone is ambiguous; UTC alone makes the model do arithmetic to answer
"what time is it here". Weekday is spelled with fixed English names rather
than FormatDateTime's locale-dependent 'ddd', because "next Monday" is a
question models actually get asked.
Tests assert the placement, not just the presence: a trailing tool result
is left alone, a system message is never stamped, no message is ever added,
and the input history comes back unmodified.
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LGQKz579j1ZnDRwmr6h1V6
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d490d30307
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| Result := Copy(Hist, 0, Length(Hist)); { true copy: element-wise } | ||
| Last := High(Result); | ||
| if Last < 0 then Exit; | ||
| if Result[Last].Role <> mrUser then Exit; |
There was a problem hiding this comment.
Preserve the clock across tool iterations
After the first response dispatches a tool, Hist ends with one or more mrTool messages, so this guard returns without stamping anything. Because the stamped copy from the first request was never persisted, the next stateless provider request contains neither that stamp nor any other current-time context; therefore any turn involving a tool loses the clock before producing its final answer. Search backward for the latest non-empty mrUser message and stamp that copy while leaving the trailing tool-result sequence unchanged.
Useful? React with 👍 / 👎.
| Resp := ChatWithEmptyRetry(Cfg.Provider, HistWithTurnClock(Hist), | ||
| Tools, Cfg.Model, | ||
| LiveOptions, Cfg.StreamReliability); |
There was a problem hiding this comment.
Pass the stamped history to fallback providers
When the primary provider returns a retryable 408/429/5xx or network failure, only this primary call receives HistWithTurnClock(Hist); the fallback path at line 1747 still calls Chat(Hist, ...). A successful fallback consequently answers the turn without the newly introduced date/time context, precisely during the outages and rate limits that activate configured fallbacks. Compute the stamped history once for the iteration and use it for both primary and fallback calls.
Useful? React with 👍 / 👎.
Both findings are legit and the P1 is worse than it reads. P1 -- the clock vanished the moment a turn used a tool. The guard only looked at the FINAL message, and once the first response dispatches a tool the history ends in mrTool results, so nothing was stamped from iteration 2 onward. Since the stamp is never persisted, those later stateless requests carried no time at all -- and they are the ones that produce the final answer. Most turns use a tool, so in practice the clock was reaching the model only on turns where it mattered least. The PR description called this "fresh once per turn", which papered over it. The search now runs BACKWARD to the latest non-empty user message and stamps that copy, leaving the trailing tool-result run untouched so tool_use/tool_result pairing is unaffected. P2 -- the fallback path still sent the unstamped Hist, so a fallback answered without the time during exactly the outages and rate limits that engage fallbacks. The stamped history is now built once per iteration and used by the primary call and every fallback. One thing neither finding raised, and the reason the stamp is passed in rather than read inside: now that an EARLY message carries it, re-reading the clock per iteration would change that message's bytes on every call. There is no message-level cache_control in the Anthropic provider, but several providers cache message prefixes implicitly, and a value ticking mid-turn would defeat that for nothing -- the model does not need second-accurate time while it works. So the reading is taken once per turn (beside the Identity assignment, which is per-loop for the same reason) and every iteration shows the same one. Tests cover the tool-loop case directly: a history ending in two tool results still stamps the user turn, leaves both results and the assistant turn alone, adds no message, and does not touch the caller's copy. Also pinned that only the LATEST user turn is stamped in a multi-turn history. Confirmed the new case fails against the old last-message-only guard. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LGQKz579j1ZnDRwmr6h1V6
#544) All four findings are legit, and the first one is a census failure on my part -- the whole premise of the doc was "survey first", and I missed bench/agentloop entirely. 1. bench/agentloop already exists with `make bench-agentloop` and NINE scenarios. It plays a scripted model over the relay provider, asserts on what the loop did, and already emits request-byte metrics. Its repeatread scenario benches the C3 dedup, fatread the output cap, histelide compaction, build-site the ledger fold plus iteration-1 pristine. Roughly 60% of the P1 I proposed was already implemented, and a second `make bench-loop` target would have collided with it. P1 is now "extend bench/agentloop", with only the genuinely-new scenarios listed, and "a second harness or target name" is an explicit non-goal. 2. The prefix-stability budget was wrong by design: FormatLedgerBlock adds sticky nudge text once TotalCalls reaches LedgerNudgeAfter (8), so "iterations 2..50 byte-identical" fails even when caching behaves correctly. The budget now models the transition -- identical WITHIN a ledger-state-stable window, with the one permitted change occurring exactly at 8 calls and nowhere else. 3. The supersession scenario was backwards. StubSupersededReads rewrites reads that came BEFORE a write; my script was write -> read, which leaves the new read untouched and would have tested nothing. Now read -> write, asserting the earlier body was stubbed. 4. HistWithTurnClock is not on main -- it lives on the open PR #543. It was listed as an exported seam benchable today; it is now marked a prerequisite, and the turn-clock scenario is explicitly blocked on that merge. Also recorded while verifying: bench/agentloop/README.md links ../swe, which is on the orphaned #313 branch, so main ships a dangling reference -- extra evidence for the P2 mining step. The revision note is kept in the document on purpose. "We already had one" is the most useful thing this plan can tell the next person. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LGQKz579j1ZnDRwmr6h1V6
The week's agent-optimization work (batch rule FMXExpress#537, SerialOnly FMXExpress#538, compact results, disclosure, turn clock FMXExpress#543) all shipped on mechanistic arguments with no measured effect. The plan closes that by wiring together what already exists rather than building a bench from scratch. Census findings the plan is grounded in: - a scripted ILLMProvider (TScripted) already drives RunToolLoop in the progress-ledger tests -- the in-process bench substrate exists - TToolLoopResult already carries the metrics (iterations, usage with cache read/created tokens, truncations, dispatched calls) - context hygiene already exists and is untested for effect: per-turn hash-aware read dedup (C3) and stale-read stubbing after writes -- which corrects the earlier Bullet-eval claim that PasClaw has no re-read guard - PR FMXExpress#313 holds a real 13-fixture SWE-shaped bench with oracles and an ablation grid, but its branch has NO merge base with main -- the plan mines it onto main and closes the PR, same treatment for FMXExpress#308 Phases: P0 bench record (NDJSON per turn incl. prefix byte-stability), P1 in-process scenario bench with committed budgets as a failing CI gate, P2 extract bench/swe from the orphaned branch, P3 opt-in live-model A/B of the shipped levers (needs a key; refuses without --spend), P4 the backlog each item lands only with its bench delta attached. Honest boundary stated up front: harness properties are fully measurable in-sandbox with the scripted provider; model-dependent effects (does rule 7 change batching) need the live tier. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LGQKz579j1ZnDRwmr6h1V6
PasClaw never told the model what time it is. Dates leaked in only incidentally and conditionally — as a memory-file heading when that day's file happened to exist, and as an example filename in the memory rule — so a stock run left the model falling back to its training cutoff. That silently corrupts date arithmetic, "is this still current" judgements, relative-time reasoning, and anything schedule-shaped.
Option B as chosen: fresh per turn, in the message stream, with timezone.
Where it goes is the part with a cost attached
The system block carries the
cache_controlbreakpoint (PasClaw.Providers.Anthropic.BuildRequest), so a clock in the system prompt would invalidate the provider prefix cache on every single call — the biggest single-breakpoint win in the request. The progress ledger already states that constraint for itself in as many words: "NO counters, no timestamps."The messages array sits after both the system and last-tool breakpoints (verified — there is no message-level
cache_control), so a value that changes every turn costs nothing there.Two traps inside the message array
Both avoided by extending the trailing user message rather than adding one:
mrSystemmessage is hoisted into the system prompt by the Anthropic provider, which scansMessagesfor the firstmrSystemwhenOptions.SystemPromptis empty — straight back into the cached prefixmrUsermessage appended after tool results would sit between atool_useand itstool_resultThe caller's history is never mutated, so persisted transcripts don't accumulate a stamp per turn.
Consequence worth stating: the clock is fresh once per turn, not per iteration. Within a turn it drifts by seconds to minutes while the model works — below the resolution any of this is used at — and it keeps tool iterations byte-stable for providers that cache further in.
The stamp
Local wall clock, the UTC offset, and the UTC equivalent. Local alone is ambiguous; UTC alone makes the model do arithmetic to answer "what time is it here". The weekday uses fixed English names rather than
FormatDateTime's locale-dependent'ddd', because "next Monday" is a question models actually get asked.Tests
test-turn-clockasserts the placement, not just the presence: a trailing tool result is left alone, a system message is never stamped, no message is ever added, and the input history comes back unmodified.Green:
test-turn-clock,test-prompt-batching,test-progress-ledger,test-stream-reliability,test-max-iter-notice,test-plan-build-mode,test-condense-json,test-toolview,test-tool-rpc,make smoke, clean full rebuild.🤖 Generated with Claude Code
https://claude.ai/code/session_01LGQKz579j1ZnDRwmr6h1V6
Generated by Claude Code