Skip to content

Tools.MemoryFetch: auto-dedup repeat fetches of the same URL - #192

Merged
FMXExpress merged 2 commits into
mainfrom
claude/memory-fetch-dedup
Jun 9, 2026
Merged

FMXExpress merged 2 commits into
mainfrom
claude/memory-fetch-dedup

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

Summary

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 + 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:

  • ReadCachedHeader parses the YAML-ish provenance block BuildBodyWithHeader already writes — source: and fetched_at: on consecutive lines after the <!-- pasclaw memory_fetch --> marker. 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 all three conditions hold:

  1. The destination file exists.
  2. The cached file's source: URL matches the request (case-insensitive via SameText).
  3. The age (Now - cached fetched_at) is non-negative and inside MEMORY_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=foo for two different URLs). A stale entry still refetches. A first fetch still hits HTTP.

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).

Cross-compiler

UTC conversion uses the same FPC/Delphi split PasClaw.Utils.NowIsoUtc already uses:

{$IFDEF FPC}
NowUtc := LocalTimeToUniversal(Now);
{$ELSE}
NowUtc := TTimeZone.Local.ToUniversalTime(Now);
{$ENDIF}

Test plan

  • make rebuilds cleanly
  • make test — all 15 suites green (no new tests added; the helper-level testing would need an HTTP mock that's out of scope)
  • Manual: pasclaw agent, ask the model to memory_fetch an RFC twice in a row — confirm the second call returns "already indexed (cached Nm ago)" without an HTTP round-trip
  • Manual: rm the cached file then re-fetch — confirm refetch works
  • Manual: fetch URL A with name=foo, then fetch URL B with name=foo — confirm the second call refetches and overwrites (name-collision path)
  • Manual: edit a cached file's fetched_at: to 48h ago, refetch — confirm refetch triggers (stale path)

Generated by Claude Code

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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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.
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.

2 participants