Skip to content

feat: add supersedeReads strategy to prune stale read outputs#451

Open
sudorest wants to merge 1 commit intoOpencode-DCP:masterfrom
sudorest:feat/supersede-reads
Open

feat: add supersedeReads strategy to prune stale read outputs#451
sudorest wants to merge 1 commit intoOpencode-DCP:masterfrom
sudorest:feat/supersede-reads

Conversation

@sudorest
Copy link

@sudorest sudorest commented Mar 21, 2026

Summary

Adds a new automatic pruning strategy — supersedeReads — that prunes stale read tool outputs when the same file has been subsequently written or edited successfully. This is the natural complement to the existing supersedeWrites strategy.

Problem

In a typical coding session, the model frequently reads a file, then edits or writes to it. After a successful write/edit, the original read output is stale — the file contents have changed. These outdated read outputs waste context tokens and can cause the model to reference old file contents, leading to confusion and hallucinations.

Read outputs are often large (entire file contents), so pruning them yields significant token savings.

Solution

The new supersedeReads strategy:

  • Tracks all read tool calls and all successful write/edit tool calls by file path
  • Prunes read outputs where a subsequent successful write/edit targets the same file
  • Ignores errored writes — if a write failed, the file wasn't actually changed, so the read is still valid
  • Enabled by default (consistent with supersedeWrites)
  • Respects all existing guards: protectedFilePatterns, manualMode, automaticStrategies, turn protection

Changes

File Change
lib/strategies/supersede-reads.ts New strategy implementation
lib/strategies/index.ts Export the new strategy
lib/config.ts Add SupersedeReads interface, config keys, validation, defaults, merge logic
lib/tools/compress.ts Wire strategy into compress tool execution
dcp.schema.json Add supersedeReads to JSON schema for IDE autocomplete
tests/supersede-reads.test.ts 14 comprehensive tests
tests/compress.test.ts Add supersedeReads to test config fixture

Configuration

{
    "strategies": {
        "supersedeReads": {
            "enabled": true  // default
        }
    }
}

Testing

  • npm run typecheck — passes
  • npm test — 30/30 tests pass (14 new + 16 existing)
  • npm run format:check — passes

Test coverage

Test What it verifies
Prunes read then write (same file) Core happy path
Prunes read then edit (same file) Edit also supersedes
Write before read Chronological order matters
Different files No cross-file pruning
Errored write Failed writes don't supersede
Multiple reads All stale reads pruned
Read after write Only pre-write reads pruned
Already-pruned reads No double-counting
Disabled config Respects enabled: false
Manual mode Respects automaticStrategies: false
Protected file patterns Glob patterns honored
Multiple files independently Per-file tracking
Empty tool list Graceful no-op
Non-file tools Only file tools affected

When a file is read and then subsequently written/edited successfully, the original read output becomes stale. This new strategy automatically prunes those outdated read outputs, saving context tokens.

- New strategy: supersedeReads (enabled by default)

- Only prunes reads followed by successful writes/edits (errored writes are ignored)

- Respects protectedFilePatterns, manual mode, and automaticStrategies

- Full config support with JSON schema, validation, and merge logic

- 14 comprehensive tests covering all edge cases

- Wired into compress tool alongside existing strategies
@sudorest sudorest force-pushed the feat/supersede-reads branch from 985bd3e to dc58183 Compare March 21, 2026 21:31
@Tarquinen
Copy link
Collaborator

Why should editing a file prune the read from context? that means you can't edit it again without first reading it again. You're right pruning the read when it has been overwritten by a write tool makes sense though. Also supersede writes is on its way out, that strategy doesn't work for other reasons.

I like the idea! Will review if you address the first issue.

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