Skip to content

Commit 5b29809

Browse files
fix(memory): suppress cached surfaces before system hash refresh
The prior test relied on a changed system hash, but messages.transform observes that hash one pass late. Replace that claim with the documented immediate memory-off invariant, add the TS/Rust wire fixture, and record hunt #3 evidence. Co-authored-by: Alfonso <[email protected]>
1 parent 495c7bc commit 5b29809

8 files changed

Lines changed: 626 additions & 65 deletions

‎docs/issue-368-memory-gate-report.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ The fix must not rewrite m[0] during a defer pass. The established m[0]/m[1] con
3535

3636
## Regression coverage and mutation proof
3737

38-
The focused OpenCode and Pi tests seed active project memory, active global profile rows, a profile-version advance, and an explicit image-capable mural. Their memory-off cases assert that neither m[0] nor m[1] contains the corresponding project-memory, profile, profile-delta, or mural bytes; their memory-on control compares the default and explicit `memoryEnabled: true` m[0]/m[1] shapes byte-for-byte. A second TS and Pi case starts memory-on, changes the guidance system hash while turning memory off, observes one HARD materialization, then proves the next defer replays the frozen suppressed m[0] without another materialization.
38+
The focused OpenCode and Pi tests seed active project memory, active global profile rows, a profile-version advance, and an explicit image-capable mural. Their memory-off cases assert that neither m[0] nor m[1] contains the corresponding project-memory, profile, profile-delta, or mural bytes; their memory-on control compares the default and explicit `memoryEnabled: true` m[0]/m[1] shapes byte-for-byte. The TypeScript transition case keeps the previous guidance system hash while turning memory off and requires an immediate `render_config` HARD materialization; this prevents one replay of the memory-bearing cache before the later system hook publishes its changed hash. The next defer replays the frozen suppressed m[0] without another materialization. Pi retains its equivalent memory-off transition coverage.
3939

4040
The Rust `m0_compose` and `m1_compose` unit legs seed the module store with project memory, active profile data, a version gap, and a mural. They prove the memory-off compose path omits the profile baseline, profile delta, mural, project-memory bytes, and memory watermarks. The standard module differential/golden suite remains part of the module gate.
4141

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# TS ↔ Rust transform structural parity hunt #3
2+
3+
## Scope and method
4+
5+
The primary evidence remained serialized Anthropic request bodies. The audit did not reconstruct provider messages from transform state. The reproducible configured-lane snapshot was:
6+
7+
```sh
8+
python3 scripts/audit-transform-wire-parity.py \
9+
"$TMPDIR/opencode-anthropic-auth-dumps" \
10+
--date 2026-08-26 --after 2026-08-26T12-30 --per-session 1000 \
11+
--context-db "$HOME/.local/share/cortexkit/magic-context/context.db" \
12+
--store-db "$HOME/.local/share/cortexkit/magic-context/store.db"
13+
```
14+
15+
It contains 227 requests assigned to the three requested Rust projects and 1,366 requests from the other 19 sessions; all 1,593 companion statuses are 200. Telemetry then found that SUBCONSCIOUS was not actually serving through Rust (finding F2). Re-running with only the two observed Rust sessions produced 92 Rust and 1,501 TypeScript bodies. Unlike-session counts are discovery evidence only; byte-template and source/fixture comparisons remove the scale confound.
16+
17+
A second frozen all-session snapshot, bounded at request `2026-08-27T05-38-29-059Z-001994-ses_227ce5788ffeRPA9THoPLOQreO-direct-sticky-yiyi`, covered 2026-08-27 post-deploy traffic: 64 configured-Rust and 337 TypeScript bodies. The adjacent `.meta.json` `id` is the request identifier used below. The audit script now reports project roots, reduced-representation vocabulary, compaction markers, media shapes, exact nudge assembly, caveman state, transform decisions, module activity, and matched input-state predicate bands.
18+
19+
## Findings
20+
21+
### F1 — fixed as a TypeScript bug: memory-off could replay one memory-bearing cache entry
22+
23+
**File evidence and non-vacuity**
24+
25+
`mustMaterialize` receives `memoryEnabled`, but before this change it had no direct memory-on → memory-off predicate (`inject-compartments.ts:1529-1684`). It relied on the system hash. The transform itself documents that `messages.transform` observes the persisted previous system hash because `system.transform` runs later (`transform.ts:2101-2107`). Therefore the first memory-off message pass could replay cached memory-bearing m0/m1 bytes before the changed system hash arrived on the next pass. That violates the documented invariant that `memory.enabled=false` suppresses every memory-derived surface.
26+
27+
The focused transition test now deliberately keeps the old system hash and requires an immediate `render_config` HARD decision. Disabling the new predicate under `NON-VACUITY BREAK` made the test fail with `expected render_config, received null`; the break was restored and the test returned green.
28+
29+
**Adjudication:** **TS-bug** (the allowed exception to TypeScript-as-spec). The invariant is documented in `docs/issue-368-memory-gate-report.md:28-42`; a one-request memory leak is not an acceptable cache-stability trade.
30+
31+
**Disposition:** fixed. `mustMaterialize` performs a self-consuming check: when memory is off and cached m0 or m1 contains `<project-memory>`, `<user-profile>`, `<new-user-profile>`, `<memory-updates>`, or `<memory-mural>`, it folds immediately. The sanitized bytes contain none of those blocks, so subsequent defer passes remain byte-stable without a schema flag.
32+
33+
A new hermetic differential fixture starts memory-off, sends a real TS request, restarts the same data dir into Rust mode, and sends a real Rust request. Both served wires omit all five memory surfaces and the `ctx_memory` tool. The existing seeded TS and Rust compose fixtures supply non-vacuity for profile, delta, mural, and project-memory data; there is still no production memory-off Rust session.
34+
35+
### F2 — operational gap: SUBCONSCIOUS was configured as Rust but served TypeScript
36+
37+
**Evidence**
38+
39+
- Project-root extraction maps `ses_12a4fa38dffe81Fz7Y2AsWb5Cg` to `/Users/ufukaltinok/Work/Projects/CortexKit/subconscious`.
40+
- In the 2026-08-26 snapshot it produced 135 served bodies. Its 395 same-window `transform_decisions` rows contain TypeScript-only hash operands in all 395 rows; decisions are 394 `defer` and one `execute/pressure_refold`.
41+
- `store.db.mc_cache_state.last_activity_at` for that session is `2026-07-22T21:41:50Z`, outside the audit window, and it has no same-window `mc_pass_trace` scheduler history. By contrast ASTRO/ENGRAM rows have null TypeScript-only operands and current module activity.
42+
- The frozen current-day snapshot reproduces it: 30/30 SUBCONSCIOUS rows have TypeScript-only operands and no current module activity.
43+
44+
**Adjudication:** **benign-documented for transform bytes, but an activation/deployment gap**. Treating these bodies as Rust would contaminate every parity denominator. There is no repository transform fix justified by wires that never traversed Rust.
45+
46+
**Fix brief:** inspect SUBCONSCIOUS's effective project `transform_mode`, user-tier subc connection, module status/provenance, and route binding; restart that host after restoring the Rust route. Acceptance is one fresh served request whose decision row has all six TypeScript-only operands null plus current `mc_cache_state`/`mc_pass_trace` activity. Then re-run this audit with all three sessions genuinely observed as Rust.
47+
48+
## Per-axis verdicts
49+
50+
### 1. Fresh de-gauged nudge assembly
51+
52+
**Verdict: pass where observed; TypeScript live-occurrence gap.** The first post-deploy de-gauged wire occurrence is Rust/ASTRO request `2026-08-27T04-33-39-205Z-001881-ses_08df2045bffeBcWcqw60elghER-direct-sticky-ufuk2`, message 440, block 0. It is a suffix inside the textual content of a sole `tool_result` block, begins with exactly two newlines, contains one `<system-reminder>` span, and renders the hint on its own line:
53+
54+
```text
55+
56+
57+
<system-reminder>
58+
Housekeeping backlog: 99 spent tool outputs (~94k tokens) are reclaimable — a ctx_reduce pass is due.
59+
oldest reclaimable: §8642§ bash · §8662§ bash · §8683§ bash · §8689§ bash.
60+
</system-reminder>
61+
```
62+
63+
The same frozen span appears in 14 later accepted bodies. No post-deploy TypeScript body contains a newly emitted de-gauged reminder; 337 TypeScript bodies carry only older frozen gauged spans. That is an honest production evidence gap, not a byte failure. The shared copy remains unit-pinned. The differ now separates gauged/degauged, Channel 1/2, band, full/suffix placement, carrier vector, and hint-line presence so hunt #4 can capture the first TypeScript occurrence.
64+
65+
### 2. Reduced-arc skeleton/edit-marker/drop vocabulary
66+
67+
**Verdict: pass for all live classes; skeleton has no live Rust occurrence.** With observed lanes:
68+
69+
- Rust request `2026-08-26T15-40-02-795Z-000043-ses_0ad83017cffexe0g5N8UG0y3LZ-direct-sticky-ufuk2`, message 135/block 0, carries a Write edit-marker preserving `filePath` and a 40-character `content` region prefix followed by exact `...[truncated]`. The Rust snapshot has 35 such frozen arcs.
70+
- TypeScript request `2026-08-26T15-38-39-385Z-000017-ses_0758f6ce7ffeJ0A9sV8Qvema7d-direct-sticky-ufuk2`, message 695/block 0, has the same 40-character Write vocabulary; the snapshot has 1,372.
71+
- Rust request `2026-08-26T15-39-15-867Z-000025-ses_08df2045bffeBcWcqw60elghER-direct-sticky-ufuk2`, message 14/block 0, and TypeScript request `2026-08-26T15-38-41-882Z-000018-ses_00ed68536ffeah34foNE2loI5i-direct-sticky-yiyi`, message 8/block 0, both render exact `[dropped §N§]`.
72+
- No current body in either lane contains the historical `{reduced,summary}` envelope. Actual Rust traffic did not freeze a skeleton in this window. The TS differential selection golden pins the shared five-character skeleton clamp, array/object vocabulary, edit-marker top-level semantics, and drop ordering in Rust.
73+
74+
Search-hint-bearing drops are now classified separately from exact sentinels instead of being counted as visible text.
75+
76+
### 3. Caveman tiers on aged sessions
77+
78+
**Verdict: pass; same freeze-time age basis.** Live aged state exists in both lanes. ASTRO has depth counts `lite=54/full=53/ultra=54`; ENGRAM has `291/463/708`. TypeScript sessions likewise carry all three depths (for example AVATAR `41/40/41`). Rust stores `caveman_age_basis_tag` at the maximum tag of each genuine bust and reuses it on defer (`transform.rs:4351-4370`). TypeScript computes the maximum tag from the bust snapshot and only replays persisted depth on defer (`caveman-cleanup.ts:104-133,198-265`). Both implement oldest-first eligible ordering and 20/20/20/40 tiers. The cross-language `caveman-golden.json` pins the exact text compressor bytes. Different source prose means live output strings are not falsely compared as rates or byte twins.
79+
80+
### 4. Compaction marker and summary-row shapes
81+
82+
**Verdict: pass.** Rust request `...000025...ASTRO`, message 1/block 0, and TypeScript request `...000018...AVATAR`, message 1/block 0, both carry:
83+
84+
```text
85+
§N§ [Compacted by magic-context — session history is managed by the plugin]
86+
```
87+
88+
Every observed marker is assistant text at block 0 with the same tagged template. Sibling block vectors vary (`[text]`, `[text,tool_use]`, and merged TypeScript text variants) because the native assistant row differs; the marker block itself does not. Both lanes filter OpenCode summary rows before ordinal assignment. No untagged or alternate marker vocabulary appeared.
89+
90+
### 5. Channel-2 delivered shape
91+
92+
**Verdict: hermetic pass; no live Channel-2 delivery.** No body in either snapshot contains the exact `Routine housekeeping:` Channel-2 reminder. TypeScript `maybeDeliverChannel2` delivers a synthetic user text part through `promptAsync`; Rust OpenCode returns the same host directive and the host uses that delivery path. Hermetic tests pin the synthetic-user echo, terminal lease, exact de-gauged copy, and no repeat. The live `tool_result` suffix shapes reported above are Channel 1, not evidence of a Channel-2 alternate encoding.
93+
94+
### 6. Image/attachment stripping
95+
96+
**Verdict: pass where observable; transcript-image Rust evidence gap.** Both lanes render the m0 mural image identically as user message 0/block 1 with keys `{type,source,cache_control}`, source `{type:"base64",media_type:"image/png",data}`, before m1. Example Rust is request `...000025...ASTRO`; the same shape appears 92 times in observed Rust traffic.
97+
98+
TypeScript request `2026-08-26T15-38-28-413Z-000013-ses_06be916fbffezpvuoIO3ac4yMZ-direct-sticky-ufuk2`, message 392, contains transcript images with the same source shape. No Rust request retained a transcript image in this window, so live wires cannot prove an equivalent aged image strip. Seeded DETECT/REPLAY tests pin that both lanes remove processed image blocks and replay the frozen absence without inventing a textual placeholder (`strip-content.test.ts:314-486`; `transform.rs:10488-10841,14208-14252`). No `document`, `file`, or `attachment` provider block appeared.
99+
100+
### 7. Decision parity
101+
102+
**Verdict: no structural divergence in matched states.** The differ compares predicates in matched 50k input bands, not rates across unlike windows. In the shared 250k-350k bands, Rust and TypeScript observations are all `defer` with no materialize reason, emergency, or drop. TypeScript-only higher matched states contain `execute/pressure_refold`; no Rust session reached those states, so a rate comparison would violate the scale-confound rule. Rust `scheduler_history` and `scheduler_interesting_history` agree with its normalized decision rows. No trigger/threshold predicate disagreement is established. F2 is excluded from the Rust distribution because its non-null operands prove it was TypeScript.
103+
104+
### 8. Additional differ output
105+
106+
Apart from F1 and F2, the extended differ found no unexplained provider-shape class: all responses are 200; no bare dropped sentinel, reduced envelope, orphan result, duplicate tool ID, or invalid compaction marker occurred. Historical gauged reminders remain frozen by design and are not mistaken for new post-deploy assembly.
107+
108+
## Verification
109+
110+
The implementation is verified by the commands recorded in the task delivery. In particular, the memory transition guard was red under the explicit non-vacuity mutation, the steady memory-off TS→Rust hermetic fixture passed against production-path plugin bytes, and the full plugin/Pi/Rust three-suite gate was run after the final edits.

‎packages/e2e-tests/mode-manifest.json‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,19 @@
477477
"HISTORIAN-PRODUCER"
478478
]
479479
},
480+
{
481+
"path": "tests/rust-memory-off-parity.test.ts",
482+
"tier": "rust-only",
483+
"invocation": {
484+
"ts": false,
485+
"rust": true
486+
},
487+
"rationale": "one hermetic persistent-data fixture runs memory-on/off transitions through both TS and Rust served-wire lanes",
488+
"contract_refs": [
489+
"RUST-E2E",
490+
"MEMORY-OFF-TRANSFORM-PARITY"
491+
]
492+
},
480493
{
481494
"path": "tests/rust-multi-frame-delta-perf.test.ts",
482495
"tier": "rust-only",

‎packages/e2e-tests/scripts/validate-mode-manifest.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function manifestWith(entries: ModeManifest["entries"]): ModeManifest {
1818

1919
describe("mode manifest validator", () => {
2020
it("covers every live e2e test exactly once", () => {
21-
expect(validation.files.length).toBe(60);
21+
expect(validation.files.length).toBe(61);
2222
expect(validation.manifest.entries).toHaveLength(validation.files.length);
2323
expect(new Set(validation.manifest.entries.map((entry) => entry.path)).size).toBe(
2424
validation.files.length,
@@ -30,7 +30,7 @@ describe("mode manifest validator", () => {
3030
const ts = filesForMode(validation, "ts");
3131
const rust = filesForMode(validation, "rust");
3232
expect(ts).toHaveLength(41);
33-
expect(rust).toHaveLength(31);
33+
expect(rust).toHaveLength(32);
3434
expect(ts.filter((path) => path.startsWith("tests/pi-")).length).toBe(22);
3535
expect(filesForMode(validation, "ts", "opencode")).toHaveLength(19);
3636
expect(filesForMode(validation, "ts", "pi")).toHaveLength(22);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/// <reference types="bun-types" />
2+
3+
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
4+
import { RustTestHarness } from "../src/rust-harness";
5+
import { rustPrereqs } from "../src/rust-scenario-support";
6+
7+
const MEMORY_SURFACES = [
8+
"<project-memory>",
9+
"<user-profile>",
10+
"<new-user-profile>",
11+
"<memory-updates>",
12+
"<memory-mural>",
13+
] as const;
14+
15+
function expectMemorySuppressed(wire: string): void {
16+
for (const surface of MEMORY_SURFACES) expect(wire).not.toContain(surface);
17+
}
18+
19+
function expectMemoryToolSuppressed(body: Record<string, unknown>): void {
20+
const tools = Array.isArray(body.tools) ? body.tools : [];
21+
expect(
22+
tools.some((tool) =>
23+
/ctx_memory/.test(String((tool as { name?: unknown })?.name ?? "")),
24+
),
25+
).toBe(false);
26+
}
27+
28+
describe.skipIf(!rustPrereqs.ok)("TS/Rust parity: memory-off served wire", () => {
29+
let h: RustTestHarness;
30+
31+
beforeAll(async () => {
32+
h = await RustTestHarness.create({
33+
startInTsMode: true,
34+
magicContextConfig: { memory: { enabled: false } },
35+
});
36+
});
37+
38+
afterAll(async () => {
39+
await h?.dispose();
40+
});
41+
42+
it("suppresses every memory-derived surface in both transform lanes", async () => {
43+
const sessionId = await h.createSession();
44+
45+
await h.sendPrompt(sessionId, "TS memory-off assertion");
46+
const tsBody = [...h.requests()]
47+
.reverse()
48+
.find((request) => JSON.stringify(request.body.messages ?? []).includes("TS memory-off assertion"))
49+
?.body;
50+
expect(tsBody).toBeDefined();
51+
if (!tsBody) throw new Error("TS memory-off request was not captured");
52+
expectMemoryToolSuppressed(tsBody);
53+
expectMemorySuppressed(h.lastMainWireSerialized());
54+
55+
await h.restart({ rust: true, magicContextConfig: { memory: { enabled: false } } });
56+
await h.sendPrompt(sessionId, "Rust memory-off assertion");
57+
const rustBody = [...h.requests()]
58+
.reverse()
59+
.find((request) =>
60+
JSON.stringify(request.body.messages ?? []).includes("Rust memory-off assertion"),
61+
)?.body;
62+
expect(rustBody).toBeDefined();
63+
if (!rustBody) throw new Error("Rust memory-off request was not captured");
64+
expectMemoryToolSuppressed(rustBody);
65+
expectMemorySuppressed(h.lastMainWireSerialized());
66+
}, 180_000);
67+
});

‎packages/plugin/src/hooks/magic-context/inject-compartments.test.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,7 +2065,7 @@ describe("m[0]/m[1] materialization", () => {
20652065
expect(m1).not.toContain("profile delta must not leak");
20662066
});
20672067

2068-
it("uses the system-hash HARD path for a memory-on to memory-off transition", () => {
2068+
it("uses an immediate render-config HARD path for a memory-on to memory-off transition", () => {
20692069
db = makeDb();
20702070
const projectDirectory = makeProjectDir();
20712071
createUserMemoryTable();
@@ -2100,8 +2100,12 @@ describe("m[0]/m[1] materialization", () => {
21002100
projectPath: undefined,
21012101
projectDirectory,
21022102
memoryEnabled: false,
2103-
hardSignals: { systemHash: "memory-guidance-off", modelKey: "test/model" },
2103+
// The system hook publishes its changed hash after message transform.
2104+
// Keep the old hash here to prove the memory gate itself prevents a
2105+
// one-request replay of the memory-bearing cache.
2106+
hardSignals: { systemHash: "memory-guidance-on", modelKey: "test/model" },
21042107
});
2108+
expect(transition.decision.reason).toBe("render_config");
21052109
expect(transition.m0RematerializedThisPass).toBe(true);
21062110
expect(renderedText(off[0])).not.toContain("transition profile fact");
21072111
const suppressedBytes = transition.m0Bytes?.toString("utf8");
@@ -2116,7 +2120,7 @@ describe("m[0]/m[1] materialization", () => {
21162120
projectPath: undefined,
21172121
projectDirectory,
21182122
memoryEnabled: false,
2119-
hardSignals: { systemHash: "memory-guidance-off", modelKey: "test/model" },
2123+
hardSignals: { systemHash: "memory-guidance-on", modelKey: "test/model" },
21202124
});
21212125
expect(replay.m0RematerializedThisPass).toBe(false);
21222126
expect(replay.m0Bytes?.toString("utf8")).toBe(suppressedBytes);

0 commit comments

Comments
 (0)