Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ Tier boundaries are hardcoded to keep behavior predictable and prevent cache-bus

**Always compressed from the original.** The pristine pre-caveman text is persisted in `source_contents` per tag. When a tag shifts deeper (lite → full → ultra), caveman compresses the ORIGINAL text at the new target depth rather than the already-cavemaned intermediate, so repeated tier shifts converge to exactly the same output as direct compression at the final depth.

**Cache safety.** Runs only on execute-threshold heuristic passes (same gate as automatic tool drops), so the single cache-busting pass materializes both tool drops and caveman compression together. Defer passes don't run caveman, and tier assignments are persisted in `tags.caveman_depth` so the next pass re-compresses only the tags that have shifted tiers.
**Cache safety.** Runs only on execute-threshold heuristic passes, so a cache-busting pass can materialize queued tool drops and caveman compression together. Defer passes don't run caveman, and tier assignments are persisted in `tags.caveman_depth` so the next pass re-compresses only the tags that have shifted tiers.

**Relationship to `ctx_reduce`.** Caveman compression is independent of the agent-driven `ctx_reduce` tool. `ctx_reduce` is still best for tool outputs the agent knows are spent; caveman targets old prose only, and dropped tags always win over caveman rewriting. To hide the reduce surface for a particular agent, remove or deny `ctx_reduce` in that agent's tool allow-list; Magic Context then omits `§N§` prefixes, nudges, and reduce guidance for that session.

Expand All @@ -597,7 +597,7 @@ Tier boundaries are hardcoded to keep behavior predictable and prevent cache-bus
|-----|------|---------|
| `smart_drops` | `boolean` | `false` |

**Experimental, opt-in.** Content-aware reclaim of tool output that a later call has made obsolete, layered on top of the normal age-based auto-drop. The age-based drop reclaims the *oldest* tool outputs first; smart-drops additionally reclaims outputs that are dead by *supersession*, regardless of age:
**Experimental, opt-in.** Content-aware reclaim of tool output that a later call has made obsolete. This is separate from agent-controlled `ctx_reduce`: smart-drops targets outputs that are dead by *supersession*, never merely because they are old:

| Class | Behavior |
|---|---|
Expand All @@ -606,7 +606,7 @@ Tier boundaries are hardcoded to keep behavior predictable and prevent cache-bus
| Zero-value meta (`bash_status`, `bash_kill`, `ctx_note` read/dismiss) | Drop all (worthless once the call ran; `ctx_note` write/update carry intent and are never dropped). |
| Superseded edits | When a file is edited more than once, the newest edit stays in full and each older edit is compressed to a marker that keeps its `filePath` and a short region hint of the diff, so the agent still sees which file and region it edited. This is the largest source of reclaimable bytes. |

**Cache safety.** Selection is age-independent, but smart-drops only *acts* during a transform pass that is already rewriting the message array (the same execute-threshold gate the age-based drop uses), so it never causes a prompt-cache miss on its own. Every drop resolves to the same deterministic placeholder as the normal drops, so defer passes replay byte-for-byte identically. **When `smart_drops` is off, the messages sent to the model are byte-identical to the age-based-only behavior** — the entire feature is inert.
**Cache safety.** Selection is age-independent, but smart-drops only *acts* during a transform pass that is already rewriting the message array, so it never causes a prompt-cache miss on its own. Every drop resolves to the same deterministic placeholder as an explicit drop, so defer passes replay byte-for-byte identically. **When `smart_drops` is off, the entire supersession feature is inert:** routine tool outputs are reclaimed only when the agent explicitly queues them with `ctx_reduce`, or by the emergency safety valve.

**Cross-version note.** Once you enable `smart_drops`, all binaries that share your Magic Context database (e.g. multiple OpenCode instances, or OpenCode + Pi) should be on the release that introduced it. If a stale older binary co-runs with the feature on, the worst case is a one-time cache bust (the older binary fully drops what the newer one compressed); it never corrupts data.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ Magic Context is **1 of the 3 plugins you'll ever need.** It remembers; AFT perc

- **Historian compartmentalization**: a background historian compresses old raw history into **tiered compartments**, chronological summaries that stand in for older messages. Each carries an importance score, so the live window stays small without losing the thread. Summarizing doesn't need your primary's coding muscle, so you can run the historian on a cheap or even fully local model while your main agent stays top-tier.
- **Decay rendering**: compartments render at the right fidelity for the moment, by a deterministic, no-LLM rule that self-tunes to the model's context window. Old history fades gracefully instead of dropping off a cliff, and because it is deterministic, the same history always renders the same way.
- **The agent hints what to drop, or it doesn't**: with agent-driven reduction on, the agent calls `ctx_reduce` to mark stale tool outputs or long messages for removal. Drops are **queued and cache-aware**, applied only at cache-safe moments so reduction never thrashes the cache. Switch it off and the agent stays out of context management entirely: stale output is shed automatically by age, with optional caveman compression of the oldest text.
- **Agent-controlled tool reduction**: the agent calls `ctx_reduce` to mark stale tool outputs or long messages for removal. Drops are **queued and cache-aware**, applied only at cache-safe moments so reduction never thrashes the cache. Magic Context does not silently drop tool output by age; when agent-driven reduction is unavailable, routine tool output stays whole until the emergency safety valve, while optional caveman compression can reduce the oldest text.
- **Cache-stable layout**: all of this is structured so background work never invalidates the cached prefix of your prompt. Your cache survives the whole session.

The result: one session runs for months, with no compaction pauses and low cost on cache-priced providers. You can watch it happen in OpenCode's TUI, where a live sidebar shows the context breakdown by source, historian status, and memory counts, updating after every message.
Expand Down
2 changes: 1 addition & 1 deletion assets/magic-context.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,7 @@
},
"smart_drops": {
"default": false,
"description": "Content-aware reclaim of provably-superseded tool output, layered on the existing execute-pass auto-drop. When on: superseded todowrite (keep newest 1), spent ctx_reduce (keep newest 5), and zero-value meta (bash_status, bash_kill, ctx_note read/dismiss) outputs are dropped; older edits to a file are compressed to a filePath-preserving marker while the newest edit per file stays full. Only acts on passes already busting the cache, so it never originates a cache bust. Honors the protected-tag reserve. Experimental: opt-in, default off until cache stability is proven; when off the wire is byte-identical to the positional-only reclaim. Requires a restart.",
"description": "Content-aware reclaim of provably-superseded tool output. When on: superseded todowrite (keep newest 1), spent ctx_reduce (keep newest 5), and zero-value meta (bash_status, bash_kill, ctx_note read/dismiss) outputs are dropped; older edits to a file are compressed to a filePath-preserving marker while the newest edit per file stays full. Age alone never selects a tool. Only acts on passes already busting the cache, so it never originates a cache bust. Honors the protected-tag reserve. Experimental: opt-in, default off; when off the entire supersession feature is inert and routine tool output requires explicit ctx_reduce or the emergency safety valve. Requires a restart.",
"type": "boolean"
},
"caveman_text_compression": {
Expand Down
35 changes: 3 additions & 32 deletions crates/mc-module/gen/gen-selection-golden.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Generate the differential SELECTION golden for the Rust mc-module port.
*
* Drives the REAL OpenCode TS selectors (supersession, edit-supersession, two-pass,
* Drives the REAL OpenCode TS selectors (supersession, edit-supersession,
* emergency) over tag fixtures, extracts the DECISION set (which tool → drop vs
* edit_marker), and emits (a) the equivalent flat typed-CK tail (SelItem[]) and (b)
* the expected ARC-LEVEL decisions. The Rust `selection_golden` test runs
* `select_reductions` over the same tail, projects its per-block output back to
* arc-level, and asserts equality — proving the SELECTOR logic (keep-N, tier
* ordering, reserve, headroom, watermark, file-supersession, ctx_note actions) is
* ordering, reserve, headroom, file-supersession, ctx_note actions) is
* bit-faithful to TS.
*
* SCOPE: the golden is DECISION-LEVEL (which arcs, what intent) — that is the
Expand Down Expand Up @@ -35,7 +35,6 @@ const resolve = (m: string) => Bun.resolveSync(m, pluginDir);

const storage = await import(resolve("./src/features/magic-context/storage"));
const supersession = await import(resolve("./src/hooks/magic-context/supersession-reclaim"));
const toolReclaim = await import(resolve("./src/hooks/magic-context/tool-reclaim"));
const emergency = await import(resolve("./src/hooks/magic-context/emergency-drop"));

const { openDatabase, closeDatabase, insertTag } = storage as {
Expand Down Expand Up @@ -64,7 +63,7 @@ interface TagFixture {
providerExecuted?: boolean;
}

type SelectorKind = "supersession" | "edit" | "two_pass" | "emergency";
type SelectorKind = "supersession" | "edit" | "emergency";

interface CaseSpec {
label: string;
Expand All @@ -73,8 +72,6 @@ interface CaseSpec {
/** pass class for the Rust ctx. */
passClass: "Execute" | "EmergencyForce";
smartDrops: boolean;
/** two_pass: the watermark ordinal. */
lastExecuteOrdinal?: number;
/** emergency inputs. */
emergency?: {
currentTotalInputTokens: number;
Expand Down Expand Up @@ -276,17 +273,6 @@ function runTsSelector(spec: CaseSpec): Record<string, string> {
const arc = tagNumberToArc.get(op.tagId);
if (arc) expected[arc] = "edit_marker";
}
} else if (spec.selector === "two_pass") {
const ops = toolReclaim.buildSyntheticToolReclaimOps({
db,
sessionId: SES,
targets,
watermark: spec.lastExecuteOrdinal ?? 0,
});
for (const op of ops) {
const arc = tagNumberToArc.get(op.tagId);
if (arc) expected[arc] = "drop";
}
}
} finally {
closeDatabase();
Expand All @@ -303,7 +289,6 @@ function buildCtx(spec: CaseSpec): Record<string, unknown> {
ceiling_tokens: em?.ceilingTokens ?? 0,
// protected tail cutoff = maxTag − protectedTags (ordinal space == tag space).
protected_cutoff_ordinal: em ? Math.max(maxN - em.protectedTags, 0) : 0,
last_execute_ordinal: spec.lastExecuteOrdinal ?? 0,
prior_input_sample: em?.priorInputSample ?? 0,
has_prior_drop: em?.hasPriorDrop ?? false,
agent_drop_ids: [],
Expand Down Expand Up @@ -379,20 +364,6 @@ const cases: CaseSpec[] = [
{ id: "c2", toolName: "edit", n: 2, byteSize: 500, input: { oldString: "p", newString: "q" } },
],
},
{
label: "two_pass: drop tools at/under watermark",
selector: "two_pass",
smartDrops: false,
passClass: "Execute",
lastExecuteOrdinal: 3,
tags: [
{ id: "c1", toolName: "bash", n: 1, byteSize: 200 },
{ id: "c2", toolName: "read", n: 2, byteSize: 200 },
{ id: "c3", toolName: "grep", n: 3, byteSize: 200 },
{ id: "c4", toolName: "bash", n: 4, byteSize: 200 },
{ id: "c5", toolName: "edit", n: 5, byteSize: 200 },
],
},
{
label: "emergency: tier order T3→T2→T1 to headroom",
selector: "emergency",
Expand Down
Loading
Loading