-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathproxy-microcompact-stability.test.mjs
More file actions
848 lines (785 loc) · 30.2 KB
/
Copy pathproxy-microcompact-stability.test.mjs
File metadata and controls
848 lines (785 loc) · 30.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
import { test } from "node:test";
import assert from "node:assert/strict";
import { readFile, stat, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { existsSync, rmSync } from "node:fs";
import { spawnSync } from "node:child_process";
import { join } from "node:path";
import { scratchDir } from "./scratch-registry.mjs";
import ext, {
matchesSentinelPattern,
walkToolResultsForSentinels,
normalizeToolResultContent,
buildDiagnosticRecord,
runMicrocompactStability,
} from "../proxy/extensions/microcompact-stability.mjs";
// Registration lives in scratch-registry.mjs, which gates removal on the FILE's
// outcome: a green run cleans up, a red one keeps the dirs and names them. A
// test body that throws never reaches its own cleanup, which is the whole point.
const SCRATCH_PREFIX = "mc-";
const mcTemp = () => scratchDir(SCRATCH_PREFIX);
// --- Fixture helpers ---
const SENTINEL_BARE = "[Old tool result content cleared]";
const SENTINEL_TS = "[Old tool result content cleared at 2026-04-30T13:42:11Z]";
const SENTINEL_TS_MS = "[Old tool result content cleared at 2026-04-30T13:42:11.123Z]";
const SENTINEL_TS_NOT_ISO = "[Old tool result content cleared at not-a-real-timestamp]";
const SENTINEL_PARTIAL_TRAILING = "[Old tool result content cleared] (with extra notes)";
function trBlockString(toolUseId, content) {
return { type: "tool_result", tool_use_id: toolUseId, content };
}
function trBlockArray(toolUseId, items) {
return { type: "tool_result", tool_use_id: toolUseId, content: items };
}
function userMsg(content) {
return { role: "user", content };
}
function assistantMsg(toolUseId, name = "Bash", input = {}) {
return {
role: "assistant",
content: [{ type: "tool_use", id: toolUseId, name, input }],
};
}
function makeBody(messages) {
return { model: "claude-opus-4-7-20260101", messages };
}
async function runExt(body, { meta, headers } = {}) {
const ctx = { body, meta: meta || {}, headers: headers || {} };
await ext.onRequest(ctx);
return ctx;
}
async function withEnv(overrides, fn) {
const saved = {};
for (const k of Object.keys(overrides)) {
saved[k] = process.env[k];
if (overrides[k] === undefined) delete process.env[k];
else process.env[k] = overrides[k];
}
try {
return await fn();
} finally {
for (const k of Object.keys(saved)) {
if (saved[k] === undefined) delete process.env[k];
else process.env[k] = saved[k];
}
}
}
function silenceStderr(fn) {
const orig = process.stderr.write.bind(process.stderr);
const captured = [];
process.stderr.write = (chunk) => {
captured.push(typeof chunk === "string" ? chunk : chunk.toString());
return true;
};
try {
return [fn(), captured];
} finally {
process.stderr.write = orig;
}
}
async function silenceStderrAsync(fn) {
const orig = process.stderr.write.bind(process.stderr);
const captured = [];
process.stderr.write = (chunk) => {
captured.push(typeof chunk === "string" ? chunk : chunk.toString());
return true;
};
try {
const r = await fn();
return [r, captured];
} finally {
process.stderr.write = orig;
}
}
// --- Mode A ---
test("1. exact bare sentinel → Mode A match in exact_matches", () => {
const messages = [
assistantMsg("tool_1"),
userMsg([trBlockString("tool_1", SENTINEL_BARE)]),
];
const { exact_matches, partial_matches, total_tool_results } = walkToolResultsForSentinels(messages);
assert.equal(exact_matches.length, 1);
assert.equal(partial_matches.length, 0);
assert.equal(total_tool_results, 1);
assert.equal(exact_matches[0].text, SENTINEL_BARE);
assert.match(exact_matches[0].matched_pattern, /Old tool result content cleared\\\]/);
});
test("2. exact ISO-8601 timestamp variant → Mode A match", () => {
const { exact_matches, partial_matches } = walkToolResultsForSentinels([
assistantMsg("t1"),
userMsg([trBlockString("t1", SENTINEL_TS)]),
]);
assert.equal(exact_matches.length, 1);
assert.equal(partial_matches.length, 0);
assert.match(exact_matches[0].matched_pattern, /\\d\{4\}-\\d\{2\}-\\d\{2\}T/);
});
test("2a. ISO-8601 with milliseconds variant → Mode A match", () => {
const { exact_matches } = walkToolResultsForSentinels([
assistantMsg("t1"),
userMsg([trBlockString("t1", SENTINEL_TS_MS)]),
]);
assert.equal(exact_matches.length, 1);
});
test("3. malformed timestamp → does NOT match Mode A; falls through to Mode B", () => {
const { exact_matches, partial_matches } = walkToolResultsForSentinels([
assistantMsg("t1"),
userMsg([trBlockString("t1", SENTINEL_TS_NOT_ISO)]),
]);
assert.equal(exact_matches.length, 0);
assert.equal(partial_matches.length, 1);
});
test("4. unrelated truncation message → no match in either mode (rejected from candidates)", () => {
const { exact_matches, partial_matches } = walkToolResultsForSentinels([
assistantMsg("t1"),
userMsg([trBlockString("t1", "[Tool result truncated by user]")]),
]);
assert.equal(exact_matches.length, 0);
assert.equal(partial_matches.length, 0);
});
// --- Mode B ---
test("4a. sentinel + trailing text → Mode B match, body NOT mutated, prefix_64 only in dump", async () => {
const dir = await mcTemp();
const dumpPath = join(dir, "dump.jsonl");
const body = makeBody([
assistantMsg("t1"),
userMsg([trBlockString("t1", SENTINEL_PARTIAL_TRAILING)]),
]);
const before = JSON.stringify(body);
await silenceStderrAsync(async () => {
await withEnv(
{
CACHE_FIX_DUMP_MICROCOMPACT: dumpPath,
CACHE_FIX_NORMALIZE_MICROCOMPACT: "1",
},
async () => {
await runExt(body);
},
);
});
// Body must NOT mutate — Mode B is never normalized even with normalize on.
assert.equal(JSON.stringify(body), before);
const raw = await readFile(dumpPath, "utf8");
const rec = JSON.parse(raw.trim());
assert.equal(rec.exact_matches.length, 0);
assert.equal(rec.partial_matches.length, 1);
// Full text NEVER in dump.
assert.equal(rec.partial_matches[0].sentinel_text, undefined);
assert.ok(rec.partial_matches[0].prefix_64.length <= 64);
assert.ok(rec.partial_matches[0].prefix_64.startsWith("[Old tool result content cleared"));
});
test("4b. long trailing → prefix_64 captures only first 64 chars, byte_length reports full size", async () => {
const long = SENTINEL_TS + " " + "x".repeat(200);
const dir = await mcTemp();
const dumpPath = join(dir, "dump.jsonl");
const body = makeBody([
assistantMsg("t1"),
userMsg([trBlockString("t1", long)]),
]);
await silenceStderrAsync(async () => {
await withEnv({ CACHE_FIX_DUMP_MICROCOMPACT: dumpPath }, async () => {
await runExt(body);
});
});
const rec = JSON.parse((await readFile(dumpPath, "utf8")).trim());
assert.equal(rec.partial_matches.length, 1);
assert.equal(rec.partial_matches[0].prefix_64.length, 64);
assert.equal(rec.partial_matches[0].byte_length, Buffer.byteLength(long, "utf8"));
});
test("4c. CACHE_FIX_MICROCOMPACT_REDACT_LEN=0 → prefix_64 absent", async () => {
const dir = await mcTemp();
const dumpPath = join(dir, "dump.jsonl");
const body = makeBody([
assistantMsg("t1"),
userMsg([trBlockString("t1", SENTINEL_PARTIAL_TRAILING)]),
]);
await silenceStderrAsync(async () => {
await withEnv(
{
CACHE_FIX_DUMP_MICROCOMPACT: dumpPath,
CACHE_FIX_MICROCOMPACT_REDACT_LEN: "0",
},
async () => {
await runExt(body);
},
);
});
const rec = JSON.parse((await readFile(dumpPath, "utf8")).trim());
assert.equal(rec.partial_matches.length, 1);
assert.equal(rec.partial_matches[0].prefix_64, undefined);
assert.equal(typeof rec.partial_matches[0].byte_length, "number");
});
// --- Custom patterns ---
test("5a. CACHE_FIX_MICROCOMPACT_SENTINEL_PATTERN_1 adds custom Mode A pattern", () => {
withEnv({ CACHE_FIX_MICROCOMPACT_SENTINEL_PATTERN_1: "^\\[CC microcompact rev2\\]$" }, () => {
const matched = matchesSentinelPattern("[CC microcompact rev2]", [
{ source: "^\\[CC microcompact rev2\\]$", re: /^\[CC microcompact rev2\]$/ },
]);
assert.equal(matched, "^\\[CC microcompact rev2\\]$");
});
});
test("5b. custom Mode A regex + custom Mode B prefix → exact match goes to exact_matches", async () => {
const dir = await mcTemp();
const dumpPath = join(dir, "dump.jsonl");
// Exact match against a custom regex from a sentinel family that does NOT
// share the built-in prefix. Verifies exact-match path for custom families.
const body = makeBody([
assistantMsg("t1"),
userMsg([trBlockString("t1", "[CC microcompact rev2]")]),
]);
await silenceStderrAsync(async () => {
await withEnv(
{
CACHE_FIX_DUMP_MICROCOMPACT: dumpPath,
CACHE_FIX_MICROCOMPACT_SENTINEL_PATTERN_1: "^\\[CC microcompact rev2\\]\\s*$",
CACHE_FIX_MICROCOMPACT_SENTINEL_PREFIX_1: "[CC microcompact",
},
async () => {
await runExt(body);
},
);
});
const rec = JSON.parse((await readFile(dumpPath, "utf8")).trim());
assert.equal(rec.exact_matches.length, 1);
assert.equal(rec.partial_matches.length, 0);
assert.equal(rec.exact_matches[0].sentinel_text, "[CC microcompact rev2]");
});
test("5c. custom Mode B prefix → variant-of-custom-family captured redacted in partial_matches", async () => {
// The blocker Codex flagged: a custom sentinel family that matches
// EXACTLY normalizes, but its prefix-only variant must also be captured
// in Mode B (redacted) — not silently dropped just because the variant
// doesn't share the built-in `[Old tool result content cleared` prefix.
const dir = await mcTemp();
const dumpPath = join(dir, "dump.jsonl");
const text = "[CC microcompact rev2 at 2026-04-30T17:00:00Z] (with trailing content)";
const body = makeBody([
assistantMsg("t1"),
userMsg([trBlockString("t1", text)]),
]);
const before = JSON.stringify(body);
await silenceStderrAsync(async () => {
await withEnv(
{
CACHE_FIX_DUMP_MICROCOMPACT: dumpPath,
CACHE_FIX_NORMALIZE_MICROCOMPACT: "1",
// Mode A regex won't match because of the trailing content.
CACHE_FIX_MICROCOMPACT_SENTINEL_PATTERN_1: "^\\[CC microcompact rev2\\]\\s*$",
// Mode B prefix is what should fire here.
CACHE_FIX_MICROCOMPACT_SENTINEL_PREFIX_1: "[CC microcompact",
},
async () => {
await runExt(body);
},
);
});
// Body must not mutate — Mode B is never normalized.
assert.equal(JSON.stringify(body), before);
const rec = JSON.parse((await readFile(dumpPath, "utf8")).trim());
assert.equal(rec.exact_matches.length, 0);
assert.equal(rec.partial_matches.length, 1);
assert.equal(rec.partial_matches[0].sentinel_text, undefined); // never full text
assert.ok(rec.partial_matches[0].prefix_64.startsWith("[CC microcompact"));
});
// --- Tool_result content shapes ---
test("6. content as a string containing the sentinel → matched and normalizable at string level", async () => {
const body = makeBody([
assistantMsg("t1"),
userMsg([trBlockString("t1", SENTINEL_TS)]),
]);
await silenceStderrAsync(async () => {
await withEnv({ CACHE_FIX_NORMALIZE_MICROCOMPACT: "1" }, async () => {
await runExt(body);
});
});
assert.equal(body.messages[1].content[0].content, SENTINEL_BARE);
});
test("7. content as array [{type:text, text:<sentinel>}] → matched and normalized at item level", async () => {
const body = makeBody([
assistantMsg("t1"),
userMsg([trBlockArray("t1", [{ type: "text", text: SENTINEL_TS }])]),
]);
await silenceStderrAsync(async () => {
await withEnv({ CACHE_FIX_NORMALIZE_MICROCOMPACT: "1" }, async () => {
await runExt(body);
});
});
assert.equal(body.messages[1].content[0].content[0].text, SENTINEL_BARE);
});
test("8. mixed array (text + image) — only the text matches → image untouched", async () => {
const image = {
type: "image",
source: { type: "base64", media_type: "image/png", data: "iVBORw0KGgo=" },
};
const body = makeBody([
assistantMsg("t1"),
userMsg([trBlockArray("t1", [{ type: "text", text: SENTINEL_TS }, image])]),
]);
const beforeImage = JSON.stringify(body.messages[1].content[0].content[1]);
await silenceStderrAsync(async () => {
await withEnv({ CACHE_FIX_NORMALIZE_MICROCOMPACT: "1" }, async () => {
await runExt(body);
});
});
assert.equal(body.messages[1].content[0].content[0].text, SENTINEL_BARE);
assert.equal(JSON.stringify(body.messages[1].content[0].content[1]), beforeImage);
});
// --- Diagnostic dump ---
test("9. dump set + sentinel match → JSONL line; session_id is hashed (no plaintext)", async () => {
const dir = await mcTemp();
const dumpPath = join(dir, "dump.jsonl");
const body = makeBody([
assistantMsg("t1"),
userMsg([trBlockString("t1", SENTINEL_TS)]),
]);
await silenceStderrAsync(async () => {
await withEnv({ CACHE_FIX_DUMP_MICROCOMPACT: dumpPath }, async () => {
await runExt(body, { meta: { session_id: "secret-session-12345" } });
});
});
const rec = JSON.parse((await readFile(dumpPath, "utf8")).trim());
assert.equal(rec.exact_matches.length, 1);
assert.equal(typeof rec.session_id_hash, "string");
assert.equal(rec.session_id_hash.length, 8);
assert.ok(!JSON.stringify(rec).includes("secret-session-12345"));
assert.equal(rec.model, "claude-opus-4-7-20260101");
});
test("10. dump unset → no fs activity", async () => {
const dir = await mcTemp();
const dumpPath = join(dir, "dump.jsonl");
const body = makeBody([
assistantMsg("t1"),
userMsg([trBlockString("t1", SENTINEL_BARE)]),
]);
await silenceStderrAsync(async () => {
await withEnv(
{
CACHE_FIX_DUMP_MICROCOMPACT: undefined,
CACHE_FIX_NORMALIZE_MICROCOMPACT: undefined,
},
async () => {
await runExt(body);
},
);
});
await assert.rejects(() => stat(dumpPath), /ENOENT/);
});
test("11. multiple matches in one request → ONE JSONL line with arrays split A/B", async () => {
const dir = await mcTemp();
const dumpPath = join(dir, "dump.jsonl");
const body = makeBody([
assistantMsg("t1"),
userMsg([
trBlockString("t1", SENTINEL_BARE), // exact
trBlockString("t1b", SENTINEL_TS), // exact
trBlockString("t1c", SENTINEL_PARTIAL_TRAILING), // partial
]),
]);
await silenceStderrAsync(async () => {
await withEnv({ CACHE_FIX_DUMP_MICROCOMPACT: dumpPath }, async () => {
await runExt(body);
});
});
const lines = (await readFile(dumpPath, "utf8")).trim().split("\n");
assert.equal(lines.length, 1);
const rec = JSON.parse(lines[0]);
assert.equal(rec.exact_matches.length, 2);
assert.equal(rec.partial_matches.length, 1);
});
// --- Normalization ---
test("12. normalize on, default canonical → matched sentinel becomes bare; other fields preserved", async () => {
const body = makeBody([
assistantMsg("t1"),
userMsg([trBlockString("t1", SENTINEL_TS)]),
]);
await silenceStderrAsync(async () => {
await withEnv({ CACHE_FIX_NORMALIZE_MICROCOMPACT: "1" }, async () => {
await runExt(body);
});
});
const block = body.messages[1].content[0];
assert.equal(block.type, "tool_result");
assert.equal(block.tool_use_id, "t1");
assert.equal(block.content, SENTINEL_BARE);
});
test("13. normalize on + custom canonical → matched sentinel becomes the custom text", async () => {
const body = makeBody([
assistantMsg("t1"),
userMsg([trBlockString("t1", SENTINEL_TS)]),
]);
await silenceStderrAsync(async () => {
await withEnv(
{
CACHE_FIX_NORMALIZE_MICROCOMPACT: "1",
CACHE_FIX_MICROCOMPACT_NORMALIZED: "[CLEARED]",
},
async () => {
await runExt(body);
},
);
});
assert.equal(body.messages[1].content[0].content, "[CLEARED]");
});
test("14. normalize disabled, dump enabled → matches recorded in dump but body NOT mutated", async () => {
const dir = await mcTemp();
const dumpPath = join(dir, "dump.jsonl");
const body = makeBody([
assistantMsg("t1"),
userMsg([trBlockString("t1", SENTINEL_TS)]),
]);
const before = JSON.stringify(body);
await silenceStderrAsync(async () => {
await withEnv(
{
CACHE_FIX_DUMP_MICROCOMPACT: dumpPath,
CACHE_FIX_NORMALIZE_MICROCOMPACT: undefined,
},
async () => {
await runExt(body);
},
);
});
assert.equal(JSON.stringify(body), before);
const rec = JSON.parse((await readFile(dumpPath, "utf8")).trim());
assert.equal(rec.exact_matches.length, 1);
});
test("15. two requests with different timestamps → byte-identical bodies after normalization", async () => {
const a = makeBody([
assistantMsg("t1"),
userMsg([trBlockString("t1", "[Old tool result content cleared at 2026-04-30T13:42:11Z]")]),
]);
const b = makeBody([
assistantMsg("t1"),
userMsg([trBlockString("t1", "[Old tool result content cleared at 2026-04-30T17:55:33.001Z]")]),
]);
await silenceStderrAsync(async () => {
await withEnv({ CACHE_FIX_NORMALIZE_MICROCOMPACT: "1" }, async () => {
await runExt(a);
await runExt(b);
});
});
assert.equal(JSON.stringify(a), JSON.stringify(b));
});
// --- Activation ---
test("16. both gates unset → extension fires but exits early; no telemetry, no mutation, no fs", async () => {
const dir = await mcTemp();
const dumpPath = join(dir, "dump.jsonl");
const body = makeBody([
assistantMsg("t1"),
userMsg([trBlockString("t1", SENTINEL_BARE)]),
]);
const before = JSON.stringify(body);
let ctx;
await silenceStderrAsync(async () => {
await withEnv(
{
CACHE_FIX_DUMP_MICROCOMPACT: undefined,
CACHE_FIX_NORMALIZE_MICROCOMPACT: undefined,
},
async () => {
ctx = await runExt(body);
},
);
});
assert.equal(JSON.stringify(body), before);
assert.equal(ctx.meta.microcompactStats, undefined);
await assert.rejects(() => stat(dumpPath), /ENOENT/);
});
test("17. only diagnostic enabled → telemetry present, JSONL written, no mutation", async () => {
const dir = await mcTemp();
const dumpPath = join(dir, "dump.jsonl");
const body = makeBody([
assistantMsg("t1"),
userMsg([trBlockString("t1", SENTINEL_TS)]),
]);
const before = JSON.stringify(body);
let ctx;
await silenceStderrAsync(async () => {
await withEnv({ CACHE_FIX_DUMP_MICROCOMPACT: dumpPath }, async () => {
ctx = await runExt(body);
});
});
assert.equal(JSON.stringify(body), before);
assert.ok(ctx.meta.microcompactStats);
assert.equal(ctx.meta.microcompactStats.diagnostic_enabled, true);
assert.equal(ctx.meta.microcompactStats.normalization_enabled, false);
assert.equal(ctx.meta.microcompactStats.diagnostic_records_written, 1);
});
test("18. only normalize enabled → telemetry present, mutation happens, no JSONL", async () => {
const dir = await mcTemp();
const dumpPath = join(dir, "dump.jsonl");
const body = makeBody([
assistantMsg("t1"),
userMsg([trBlockString("t1", SENTINEL_TS)]),
]);
let ctx;
await silenceStderrAsync(async () => {
await withEnv({ CACHE_FIX_NORMALIZE_MICROCOMPACT: "1" }, async () => {
ctx = await runExt(body);
});
});
assert.equal(body.messages[1].content[0].content, SENTINEL_BARE);
assert.equal(ctx.meta.microcompactStats.normalization_enabled, true);
assert.equal(ctx.meta.microcompactStats.sentinels_normalized, 1);
await assert.rejects(() => stat(dumpPath), /ENOENT/);
});
test("19. both enabled → telemetry, mutation, AND JSONL all happen; raw text captured pre-normalization", async () => {
const dir = await mcTemp();
const dumpPath = join(dir, "dump.jsonl");
const body = makeBody([
assistantMsg("t1"),
userMsg([trBlockString("t1", SENTINEL_TS)]),
]);
let ctx;
await silenceStderrAsync(async () => {
await withEnv(
{
CACHE_FIX_DUMP_MICROCOMPACT: dumpPath,
CACHE_FIX_NORMALIZE_MICROCOMPACT: "1",
},
async () => {
ctx = await runExt(body);
},
);
});
// Body mutated.
assert.equal(body.messages[1].content[0].content, SENTINEL_BARE);
// Dump records the RAW pre-normalization text.
const rec = JSON.parse((await readFile(dumpPath, "utf8")).trim());
assert.equal(rec.exact_matches[0].sentinel_text, SENTINEL_TS);
assert.equal(rec.exact_matches[0].normalized_text, undefined);
// Telemetry attached.
assert.equal(ctx.meta.microcompactStats.sentinels_normalized, 1);
});
test("19a. CACHE_FIX_DUMP_MICROCOMPACT_INCLUDE_NORMALIZED=1 → adds normalized_text alongside raw sentinel_text", async () => {
const dir = await mcTemp();
const dumpPath = join(dir, "dump.jsonl");
const body = makeBody([
assistantMsg("t1"),
userMsg([trBlockString("t1", SENTINEL_TS)]),
]);
await silenceStderrAsync(async () => {
await withEnv(
{
CACHE_FIX_DUMP_MICROCOMPACT: dumpPath,
CACHE_FIX_NORMALIZE_MICROCOMPACT: "1",
CACHE_FIX_DUMP_MICROCOMPACT_INCLUDE_NORMALIZED: "1",
},
async () => {
await runExt(body);
},
);
});
const rec = JSON.parse((await readFile(dumpPath, "utf8")).trim());
assert.equal(rec.exact_matches[0].sentinel_text, SENTINEL_TS);
assert.equal(rec.exact_matches[0].normalized_text, SENTINEL_BARE);
});
// --- Telemetry shape ---
test("20. ctx.meta.microcompactStats contains every documented field after a sentinel-bearing request", async () => {
const body = makeBody([
assistantMsg("t1"),
userMsg([trBlockString("t1", SENTINEL_TS)]),
]);
let ctx;
await silenceStderrAsync(async () => {
await withEnv({ CACHE_FIX_NORMALIZE_MICROCOMPACT: "1" }, async () => {
ctx = await runExt(body);
});
});
const s = ctx.meta.microcompactStats;
for (const key of [
"diagnostic_enabled",
"normalization_enabled",
"sentinel_pattern_used",
"total_tool_results_scanned",
"exact_matches_count",
"partial_matches_count",
"sentinels_matched",
"sentinels_normalized",
"bytes_original",
"bytes_normalized",
"bytes_saved",
"diagnostic_records_written",
]) {
assert.ok(key in s, `missing telemetry field: ${key}`);
}
assert.equal(s.exact_matches_count, 1);
assert.equal(s.sentinels_matched, 1);
assert.equal(s.sentinels_normalized, 1);
assert.ok(s.bytes_saved > 0); // timestamp stripped
});
test("21. stderr summary line emitted with the spec format on enabled invocations", async () => {
const body = makeBody([
assistantMsg("t1"),
userMsg([trBlockString("t1", SENTINEL_TS)]),
]);
const [, captured] = await silenceStderrAsync(async () => {
await withEnv({ CACHE_FIX_NORMALIZE_MICROCOMPACT: "1" }, async () => {
await runExt(body);
});
});
const line = captured.find((l) => l.startsWith("[microcompact]"));
assert.ok(line, `no [microcompact] line, got: ${JSON.stringify(captured)}`);
assert.match(line, /matched=1 normalized=1 bytes=\d+->\d+/);
});
// --- Direct unit tests on pure functions ---
test("matchesSentinelPattern returns null on non-string", () => {
assert.equal(matchesSentinelPattern(undefined), null);
assert.equal(matchesSentinelPattern(null), null);
assert.equal(matchesSentinelPattern(42), null);
});
test("normalizeToolResultContent handles string and array shapes; returns false on missing target", () => {
const messages = [
{
role: "user",
content: [
{ type: "tool_result", tool_use_id: "t1", content: SENTINEL_TS },
{ type: "tool_result", tool_use_id: "t2", content: [{ type: "text", text: SENTINEL_TS }] },
],
},
];
// String shape
assert.equal(
normalizeToolResultContent(messages, { msg_idx: 0, block_idx: 0, content_kind: "string" }, SENTINEL_BARE),
true,
);
assert.equal(messages[0].content[0].content, SENTINEL_BARE);
// Array shape
assert.equal(
normalizeToolResultContent(
messages,
{ msg_idx: 0, block_idx: 1, content_kind: "array_item", item_idx: 0 },
SENTINEL_BARE,
),
true,
);
assert.equal(messages[0].content[1].content[0].text, SENTINEL_BARE);
// Missing target
assert.equal(
normalizeToolResultContent(messages, { msg_idx: 99, block_idx: 0, content_kind: "string" }, SENTINEL_BARE),
false,
);
});
test("buildDiagnosticRecord captures total_messages and total_tool_results", () => {
const reqCtx = {
body: { model: "claude-opus-4-7", messages: [{ role: "user", content: [] }] },
meta: { session_id: "abc" },
};
const rec = buildDiagnosticRecord(reqCtx, [], [], 0, { ts: "2026-04-30T00:00:00Z" });
assert.equal(rec.ts, "2026-04-30T00:00:00Z");
assert.equal(rec.total_messages, 1);
assert.equal(rec.total_tool_results, 0);
assert.equal(rec.model, "claude-opus-4-7");
assert.equal(rec.exact_matches.length, 0);
assert.equal(rec.partial_matches.length, 0);
});
// --- session_id fallback chain (#12-#15 from per-session quota-status directive) ---
import { createHash as _createHash } from "node:crypto";
function _hash8(s) {
return _createHash("sha256").update(String(s)).digest("hex").slice(0, 8);
}
test("12. hashSessionId reads x-claude-code-session-id (the canonical CC header)", () => {
const sid = "b16c607d-d484-4935-840e-e3f7ee78eb08";
const reqCtx = {
body: { messages: [] },
headers: { "x-claude-code-session-id": sid },
};
const rec = buildDiagnosticRecord(reqCtx, [], [], 0, { ts: "t" });
assert.equal(rec.session_id_hash, _hash8(sid));
});
test("13. precedence: meta.session_id wins over canonical header", () => {
const reqCtx = {
body: { messages: [] },
meta: { session_id: "from-meta" },
headers: { "x-claude-code-session-id": "from-canonical-header" },
};
const rec = buildDiagnosticRecord(reqCtx, [], [], 0, { ts: "t" });
assert.equal(rec.session_id_hash, _hash8("from-meta"));
});
test("14. precedence: canonical header wins over legacy x-session-id", () => {
const reqCtx = {
body: { messages: [] },
headers: {
"x-claude-code-session-id": "canonical",
"x-session-id": "legacy",
},
};
const rec = buildDiagnosticRecord(reqCtx, [], [], 0, { ts: "t" });
assert.equal(rec.session_id_hash, _hash8("canonical"));
});
test("15. all sources missing → null", () => {
const reqCtx = { body: { messages: [] } };
const rec = buildDiagnosticRecord(reqCtx, [], [], 0, { ts: "t" });
assert.equal(rec.session_id_hash, null);
});
// A dir minted outside mcTemp() is unregistered, so `after()` cannot remove it
// and a throwing case strands it, with the suite green either way.
// The lifecycle cannot be asserted in-process: the decision is taken on the way
// out, after every hook this file could run. So it is measured through a real
// child, both ways -- the passing arm is the control, without which "the dir is
// there" would also be what a registry that never cleans anything looks like.
test("scratch: a failing run keeps its scratch, a passing one does not", async () => {
const registry = new URL("./scratch-registry.mjs", import.meta.url).href;
const run = async (outcome) => {
const dir = await mcTemp();
const file = join(dir, `probe-${outcome}.test.mjs`);
await writeFile(file, `
import { test } from "node:test";
import assert from "node:assert/strict";
import { scratchDir } from ${JSON.stringify(registry)};
test("probe", async () => {
const d = await scratchDir("mcprobe-");
console.error("PROBE_DIR=" + d);
${outcome === "fail" ? 'assert.fail("deliberate");' : "assert.ok(true);"}
});
`);
// NODE_TEST_CONTEXT is inherited, and the runner refuses to run files when it
// sees it ("run() is being called recursively"), so the child would report
// nothing and the case would fail on its own plumbing.
const env = { ...process.env };
delete env.NODE_TEST_CONTEXT;
const r = spawnSync(process.execPath, ["--test", file],
{ env, encoding: "utf8", timeout: 60_000 });
// STDOUT, not stderr: node:test captures a case's output and re-emits it on
// its own stream, so a child's `process.stderr.write` arrives here.
const out = `${r.stdout}${r.stderr}`;
const probe = /PROBE_DIR=(.*)/.exec(out)?.[1]?.trim();
assert.ok(probe, `the probe never reported its dir: ${out}`);
return { probe, status: r.status, out };
};
const failed = await run("fail");
assert.notEqual(failed.status, 0, "premise: the failing probe exited 0");
assert.ok(existsSync(failed.probe),
"a failing run deleted the scratch that is the only record of what it was looking at");
assert.match(failed.out, /\[scratch kept\]/,
"the dir was kept and never named — the caller cannot find it");
rmSync(failed.probe, { recursive: true, force: true });
const passed = await run("pass");
assert.equal(passed.status, 0, "premise: the passing probe did not pass");
assert.ok(!existsSync(passed.probe),
"a passing run left its scratch behind — the registry cleans nothing");
});
test("scratch: no test body mints an unregistered temp dir", async () => {
const src = await readFile(new URL(import.meta.url), "utf8");
// Matches any quoted prefix, which the registrar (passing a const) does not
// have. Line comments are skipped and this line's own escaping keeps it from
// matching itself, so neither the prose above nor the assertion self-flags.
// Only "//": nothing can follow it on the line, while a block comment closes
// mid-line, so skipping "/*" and "*" would hide a real mint after the close
// and behind any generator method. Ceiling: a single-line textual match on
// one exact call form, so any other spelling passes.
const call = /mkdtemp(?:Sync)?\(join\(tmpdir\(\),\s*["'`][^"'`]+["'`]\s*\)\)/;
const raw = src.split("\n")
.map((line, i) => [i + 1, line])
.filter(([, l]) => !l.trim().startsWith("//"))
.filter(([, l]) => call.test(l))
.map(([n]) => n);
assert.deepEqual(raw, [],
`every temp dir must go through mcTemp(); raw mkdtemp at line(s): ${raw.join(", ")}`);
// POSITIVE CONTROL. Without it the pattern can be neutered -- a typo, a
// rename, anything -- and this case stays green over a file it no longer
// matches. Assembled from pieces so the sample itself is not a mint on this
// line for the pattern above to find.
const sample = ["mkdtemp", "(join(tmpdir(), ", '"x-"))'].join("");
assert.ok(call.test(sample), "the pattern no longer matches a raw mint");
// The premise is a SOURCE count, not a runtime array: the registration moved
// out of this file, so a runtime one would now be empty here and read green.
assert.ok(src.split("mcTemp(").length - 1 > 1, "premise: this file does mint temp dirs");
});