Skip to content

feat(ctx_read): serve explicit re-reads as deltas when delta_explicit is enabled - #463

Merged
yvgude merged 1 commit into
yvgude:mainfrom
parker-brown-family:feat/ctx-read-delta-explicit
Jun 20, 2026
Merged

feat(ctx_read): serve explicit re-reads as deltas when delta_explicit is enabled#463
yvgude merged 1 commit into
yvgude:mainfrom
parker-brown-family:feat/ctx-read-delta-explicit

Conversation

@parker-brown-family

@parker-brown-family parker-brown-family commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Hi, and thanks for lean-ctx 👋

Daily user here. This adds an opt-in I wanted for myself: delta-aware explicit
re-reads in ctx_read.

For context — a lot of my sessions are tight iterative loops over a handful of
files: read, edit, re-read to confirm, edit again. When I (or the agent)
explicitly re-read a file already seen this session, getting the whole thing
back is mostly tokens I'm already holding — the change is the only new
information.

The pain

lean-ctx already collapses auto re-reads to deltas. But an explicit full
or lines:N-M re-read of a session-cached file re-emits content the model still
holds, even when only a few lines moved.

What this adds

delta_explicit (config field + LCTX_DELTA_EXPLICIT env, default off): when
enabled, an explicit re-read of a session-cached file returns mode=diff if it
changed on disk, or collapses to the existing full-mode stub if unchanged. First
reads are untouched; fresh=true always bypasses.

Tradeoff / Tension

Opt-in by design — some workflows want an explicit full to always mean full.
Default off keeps it purely additive: those who want the token savings on explicit
re-reads turn it on. Workflow-specific, like a lot of token-economy choices.

Notes

  • Staleness uses the verified mtime+md5 check (is_cache_entry_stale_verified),
    so a same-second write can't produce a misleading empty diff.
  • Output stays byte-stable / deterministic (no timestamps in the note).
  • The diff base is always the full cached source, never a compressed view.

Tests

Config round-trip + env override, and ctx_read behavior: unchanged → stub,
changed + enabled → diff, disabled → full (today's behavior), fresh=true
bypasses, first read untouched.

CI / back-compat

cargo fmt and clippy -D warnings clean on the changed files; ctx_read and
config lib tests green. One additive config field; default off.

@github-actions

github-actions Bot commented Jun 19, 2026

Copy link
Copy Markdown

All contributors have signed the CLA. ✅
Posted by the CLA Assistant Lite bot.

@parker-brown-family
parker-brown-family marked this pull request as ready for review June 19, 2026 15:31
@parker-brown-family
parker-brown-family force-pushed the feat/ctx-read-delta-explicit branch 2 times, most recently from 11c7e06 to 29e5409 Compare June 19, 2026 20:02
@parker-brown-family

Copy link
Copy Markdown
Contributor Author

recheck

… is enabled

When an agent re-requests `full` or `lines:N-M` for a file it already read
this session and the file changed on disk, return `mode=diff` instead of
re-emitting content the model already holds; when unchanged, a `lines:`
re-read of a fully-delivered file collapses to the full-mode `[unchanged]`
stub. This enforces, on explicit re-reads, the RE-READ discipline lean-ctx
already recommends.

Opt-in and default OFF: config field `delta_explicit` plus the
`LCTX_DELTA_EXPLICIT=1/0` env override (env wins in both directions,
mirroring `no_degrade_effective`). First reads are unaffected and
`fresh=true` always bypasses.

- Staleness uses the verified mtime+md5 `is_cache_entry_stale_verified`, not
  mtime alone, so a same-second write on a coarse-granularity filesystem
  cannot be mistaken for "unchanged" and yield a misleading empty diff.
- The diff base is the full cached source (via `handle_diff`), never a
  compressed map/signatures view.
- The `[delta-explicit]` advisory and the diff carry no timestamps/counters,
  so identical inputs stay byte-stable (yvgude#498).

The decision is factored into the pure, testable
`ctx_read::resolve_explicit_delta_mode`; the MCP dispatcher calls it under a
cache read-lock before the `lines:`->fresh guard so a changed-file `lines:`
re-read can still be diverted to a diff.

Tests: config round-trip + env-override (both directions); ctx_read behavior
matrix (changed->diff, unchanged `lines:`->stub, OFF preserves current
behavior, fresh bypasses, first read unaffected, auto-mode never diverted,
diff base is full source, decision byte-stable). Schema + feature-catalog get
a concise opt-in note.
@parker-brown-family
parker-brown-family force-pushed the feat/ctx-read-delta-explicit branch from 29e5409 to 3521329 Compare June 19, 2026 20:12

@yvgude yvgude left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Reviewed line-by-line: the core is a pure resolve_explicit_delta_mode(cache, path, mode, …) with no wall-clock/counter/randomness, opt-in (default off = zero behavior change), and #498-determinism-aware — it uses the verified staleness check so a same-second write can't be misread as unchanged, and ships a byte-stability test plus a guard that the diff base is the full cached source (not a compressed view). Thorough test matrix (changed→diff, unchanged-lines→stub, fresh bypass, first-read, off-preserves). CI green. LGTM.

@yvgude
yvgude merged commit 536fcbb into yvgude:main Jun 20, 2026
24 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 20, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants