Repository navigation
Tools.MemoryFetch: auto-dedup repeat fetches of the same URL - #192
Conversation
The original memory_fetch always re-ran the HTTP and overwrote
the cached file even when the same URL had been fetched
seconds ago. Wasted a round trip and a tool-call budget on
something the model could have read straight off disk.
Two helpers + one gate in Tool_MemoryFetch:
ReadCachedHeader parses the YAML-ish provenance block
BuildBodyWithHeader writes ('source:' and
'fetched_at:' on consecutive lines after the
marker comment). Scans the first 10 lines and
bails -- never reads the body. Returns False
on any parse failure so the caller refetches.
FormatAgeHours short m/h/d age string for the cache-hit
confirmation. Same shape as
PasClaw.Providers.Models.HumanAge.
The gate, between FullPath computation and the HTTP call, only
trips when (a) the destination exists, (b) the cached file's
source URL matches the request, and (c) AgeHours falls inside
MEMORY_FETCH_DEDUP_HOURS (24h). All three conditions are
necessary -- a name-collision from a DIFFERENT URL still
overwrites correctly, and a stale entry still refetches.
When the cache wins, the tool returns:
memory_fetch: already indexed (cached 3h ago) as
fetched-rfc7230.md. Run memory_search to query.
No HTTP, no rewrite, no rebuilt FTS5 row (SyncDir's mtime check
already short-circuits when the file is unchanged). Concept
borrowed from chopratejas/headroom's cross-agent memory dedup.
Cross-compiler TDateTime UTC conversion uses the same
LocalTimeToUniversal / TTimeZone.Local.ToUniversalTime split
PasClaw.Utils.NowIsoUtc already uses.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0c9e79765c
ℹ️ 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".
| which is a legitimate overwrite. } | ||
| if FileExists(FullPath) and | ||
| ReadCachedHeader(FullPath, CachedURL, CachedAt) and | ||
| SameText(CachedURL, URL) then |
There was a problem hiding this comment.
Compare cached URLs case-sensitively
When two requests differ only by case in the path or query and therefore map to the same lower-cased fetched-<name>.md filename, SameText treats the cached source: as a match and skips the HTTP fetch. HTTP paths and query values are commonly case-sensitive, so fetching https://example.com/API and then https://example.com/api can return the first cached document instead of overwriting it with the second resource. Compare the URL exactly, or normalize only the scheme/host portions.
Useful? React with 👍 / 👎.
SameText over the whole URL was wrong: HTTP scheme and host are case-insensitive per RFC 3986 but path and query are commonly case-sensitive. /API and /api routinely point at distinct resources -- the previous gate would have served the first cached body when the model asked for the second URL. New CanonicalizeURLForDedup lowercases the scheme + host portion (everything before the first '/' after '://') and leaves path/query verbatim. Apply to both the requested URL and the cached source URL before comparing for exact equality. Behaviour matrix: https://Example.com/foo vs https://example.com/foo dedup HTTPS://example.com/foo vs https://example.com/foo dedup https://example.com/API vs https://example.com/api refetch https://example.com/?A=1 vs https://example.com/?a=1 refetch Falls through to the URL verbatim when the structure isn't recognisable (no '://') -- a malformed URL never gets a false cache hit.
Summary
The original
memory_fetchalways re-ran the HTTP and overwrote the cached file even when the same URL had been fetched seconds ago — wasted a round trip + a tool-call budget for something the model could have read straight off disk. This PR closes that hole by checking the existing cached file's provenance header against the request before any network call.Concept borrowed from chopratejas/headroom's "cross-agent memory with auto-dedup".
Design
Two helpers + one gate in
Tool_MemoryFetch:ReadCachedHeaderparses the YAML-ish provenance blockBuildBodyWithHeaderalready writes —source:andfetched_at:on consecutive lines after the<!-- pasclaw memory_fetch -->marker. Scans the first 10 lines and bails; never reads the body. ReturnsFalseon any parse failure so the caller refetches.FormatAgeHours— shortm/h/dage string for the cache-hit confirmation. Same shape asPasClaw.Providers.Models.HumanAge.The gate, between
FullPathcomputation and the HTTP call, only trips when all three conditions hold:source:URL matches the request (case-insensitive viaSameText).Now - cached fetched_at) is non-negative and insideMEMORY_FETCH_DEDUP_HOURS(24h).If any condition fails, the original fetch path runs unchanged. A name-collision from a different URL still overwrites correctly (operator passed
name=foofor two different URLs). A stale entry still refetches. A first fetch still hits HTTP.When the cache wins, the tool returns:
No HTTP, no rewrite, no rebuilt FTS5 row (
SyncDir's mtime check already short-circuits when the file is unchanged).Cross-compiler
UTC conversion uses the same FPC/Delphi split
PasClaw.Utils.NowIsoUtcalready uses:Test plan
makerebuilds cleanlymake test— all 15 suites green (no new tests added; the helper-level testing would need an HTTP mock that's out of scope)pasclaw agent, ask the model tomemory_fetchan RFC twice in a row — confirm the second call returns "already indexed (cached Nm ago)" without an HTTP round-tripname=foo, then fetch URL B withname=foo— confirm the second call refetches and overwrites (name-collision path)fetched_at:to 48h ago, refetch — confirm refetch triggers (stale path)Generated by Claude Code