1- import { test } from "node:test" ;
1+ import { after , test } from "node:test" ;
22import assert from "node:assert/strict" ;
33import { mkdtemp , readFile , rm , stat } from "node:fs/promises" ;
44import { tmpdir } from "node:os" ;
@@ -12,6 +12,24 @@ import ext, {
1212 runMicrocompactStability ,
1313} from "../proxy/extensions/microcompact-stability.mjs" ;
1414
15+ // EVERY SCRATCH DIR, EVEN THE RUNS THAT FAIL. Fourteen cases each made their own
16+ // and thirteen removed it on the last line of the test body, so an assertion
17+ // that threw skipped the removal — measured, 750 mc-* dirs on one host, and two
18+ // more per run with a single failure injected. Registering here means the
19+ // after() below collects what the happy path already collected and what it did
20+ // not.
21+ const scratch = [ ] ;
22+ async function mcTemp ( ) {
23+ const d = await mkdtemp ( join ( tmpdir ( ) , "mc-" ) ) ;
24+ scratch . push ( d ) ;
25+ return d ;
26+ }
27+ after ( async ( ) => {
28+ for ( const d of scratch ) {
29+ try { await rm ( d , { recursive : true , force : true } ) ; } catch { /* already gone */ }
30+ }
31+ } ) ;
32+
1533// --- Fixture helpers ---
1634
1735const SENTINEL_BARE = "[Old tool result content cleared]" ;
@@ -149,7 +167,7 @@ test("4. unrelated truncation message → no match in either mode (rejected from
149167// --- Mode B ---
150168
151169test ( "4a. sentinel + trailing text → Mode B match, body NOT mutated, prefix_64 only in dump" , async ( ) => {
152- const dir = await mkdtemp ( join ( tmpdir ( ) , "mc-" ) ) ;
170+ const dir = await mcTemp ( ) ;
153171 const dumpPath = join ( dir , "dump.jsonl" ) ;
154172 const body = makeBody ( [
155173 assistantMsg ( "t1" ) ,
@@ -182,7 +200,7 @@ test("4a. sentinel + trailing text → Mode B match, body NOT mutated, prefix_64
182200
183201test ( "4b. long trailing → prefix_64 captures only first 64 chars, byte_length reports full size" , async ( ) => {
184202 const long = SENTINEL_TS + " " + "x" . repeat ( 200 ) ;
185- const dir = await mkdtemp ( join ( tmpdir ( ) , "mc-" ) ) ;
203+ const dir = await mcTemp ( ) ;
186204 const dumpPath = join ( dir , "dump.jsonl" ) ;
187205 const body = makeBody ( [
188206 assistantMsg ( "t1" ) ,
@@ -201,7 +219,7 @@ test("4b. long trailing → prefix_64 captures only first 64 chars, byte_length
201219} ) ;
202220
203221test ( "4c. CACHE_FIX_MICROCOMPACT_REDACT_LEN=0 → prefix_64 absent" , async ( ) => {
204- const dir = await mkdtemp ( join ( tmpdir ( ) , "mc-" ) ) ;
222+ const dir = await mcTemp ( ) ;
205223 const dumpPath = join ( dir , "dump.jsonl" ) ;
206224 const body = makeBody ( [
207225 assistantMsg ( "t1" ) ,
@@ -237,7 +255,7 @@ test("5a. CACHE_FIX_MICROCOMPACT_SENTINEL_PATTERN_1 adds custom Mode A pattern",
237255} ) ;
238256
239257test ( "5b. custom Mode A regex + custom Mode B prefix → exact match goes to exact_matches" , async ( ) => {
240- const dir = await mkdtemp ( join ( tmpdir ( ) , "mc-" ) ) ;
258+ const dir = await mcTemp ( ) ;
241259 const dumpPath = join ( dir , "dump.jsonl" ) ;
242260 // Exact match against a custom regex from a sentinel family that does NOT
243261 // share the built-in prefix. Verifies exact-match path for custom families.
@@ -269,7 +287,7 @@ test("5c. custom Mode B prefix → variant-of-custom-family captured redacted in
269287 // EXACTLY normalizes, but its prefix-only variant must also be captured
270288 // in Mode B (redacted) — not silently dropped just because the variant
271289 // doesn't share the built-in `[Old tool result content cleared` prefix.
272- const dir = await mkdtemp ( join ( tmpdir ( ) , "mc-" ) ) ;
290+ const dir = await mcTemp ( ) ;
273291 const dumpPath = join ( dir , "dump.jsonl" ) ;
274292 const text = "[CC microcompact rev2 at 2026-04-30T17:00:00Z] (with trailing content)" ;
275293 const body = makeBody ( [
@@ -352,7 +370,7 @@ test("8. mixed array (text + image) — only the text matches → image untouche
352370// --- Diagnostic dump ---
353371
354372test ( "9. dump set + sentinel match → JSONL line; session_id is hashed (no plaintext)" , async ( ) => {
355- const dir = await mkdtemp ( join ( tmpdir ( ) , "mc-" ) ) ;
373+ const dir = await mcTemp ( ) ;
356374 const dumpPath = join ( dir , "dump.jsonl" ) ;
357375 const body = makeBody ( [
358376 assistantMsg ( "t1" ) ,
@@ -373,7 +391,7 @@ test("9. dump set + sentinel match → JSONL line; session_id is hashed (no plai
373391} ) ;
374392
375393test ( "10. dump unset → no fs activity" , async ( ) => {
376- const dir = await mkdtemp ( join ( tmpdir ( ) , "mc-" ) ) ;
394+ const dir = await mcTemp ( ) ;
377395 const dumpPath = join ( dir , "dump.jsonl" ) ;
378396 const body = makeBody ( [
379397 assistantMsg ( "t1" ) ,
@@ -395,7 +413,7 @@ test("10. dump unset → no fs activity", async () => {
395413} ) ;
396414
397415test ( "11. multiple matches in one request → ONE JSONL line with arrays split A/B" , async ( ) => {
398- const dir = await mkdtemp ( join ( tmpdir ( ) , "mc-" ) ) ;
416+ const dir = await mcTemp ( ) ;
399417 const dumpPath = join ( dir , "dump.jsonl" ) ;
400418 const body = makeBody ( [
401419 assistantMsg ( "t1" ) ,
@@ -455,7 +473,7 @@ test("13. normalize on + custom canonical → matched sentinel becomes the custo
455473} ) ;
456474
457475test ( "14. normalize disabled, dump enabled → matches recorded in dump but body NOT mutated" , async ( ) => {
458- const dir = await mkdtemp ( join ( tmpdir ( ) , "mc-" ) ) ;
476+ const dir = await mcTemp ( ) ;
459477 const dumpPath = join ( dir , "dump.jsonl" ) ;
460478 const body = makeBody ( [
461479 assistantMsg ( "t1" ) ,
@@ -500,7 +518,7 @@ test("15. two requests with different timestamps → byte-identical bodies after
500518// --- Activation ---
501519
502520test ( "16. both gates unset → extension fires but exits early; no telemetry, no mutation, no fs" , async ( ) => {
503- const dir = await mkdtemp ( join ( tmpdir ( ) , "mc-" ) ) ;
521+ const dir = await mcTemp ( ) ;
504522 const dumpPath = join ( dir , "dump.jsonl" ) ;
505523 const body = makeBody ( [
506524 assistantMsg ( "t1" ) ,
@@ -526,7 +544,7 @@ test("16. both gates unset → extension fires but exits early; no telemetry, no
526544} ) ;
527545
528546test ( "17. only diagnostic enabled → telemetry present, JSONL written, no mutation" , async ( ) => {
529- const dir = await mkdtemp ( join ( tmpdir ( ) , "mc-" ) ) ;
547+ const dir = await mcTemp ( ) ;
530548 const dumpPath = join ( dir , "dump.jsonl" ) ;
531549 const body = makeBody ( [
532550 assistantMsg ( "t1" ) ,
@@ -548,7 +566,7 @@ test("17. only diagnostic enabled → telemetry present, JSONL written, no mutat
548566} ) ;
549567
550568test ( "18. only normalize enabled → telemetry present, mutation happens, no JSONL" , async ( ) => {
551- const dir = await mkdtemp ( join ( tmpdir ( ) , "mc-" ) ) ;
569+ const dir = await mcTemp ( ) ;
552570 const dumpPath = join ( dir , "dump.jsonl" ) ;
553571 const body = makeBody ( [
554572 assistantMsg ( "t1" ) ,
@@ -568,7 +586,7 @@ test("18. only normalize enabled → telemetry present, mutation happens, no JSO
568586} ) ;
569587
570588test ( "19. both enabled → telemetry, mutation, AND JSONL all happen; raw text captured pre-normalization" , async ( ) => {
571- const dir = await mkdtemp ( join ( tmpdir ( ) , "mc-" ) ) ;
589+ const dir = await mcTemp ( ) ;
572590 const dumpPath = join ( dir , "dump.jsonl" ) ;
573591 const body = makeBody ( [
574592 assistantMsg ( "t1" ) ,
@@ -598,7 +616,7 @@ test("19. both enabled → telemetry, mutation, AND JSONL all happen; raw text c
598616} ) ;
599617
600618test ( "19a. CACHE_FIX_DUMP_MICROCOMPACT_INCLUDE_NORMALIZED=1 → adds normalized_text alongside raw sentinel_text" , async ( ) => {
601- const dir = await mkdtemp ( join ( tmpdir ( ) , "mc-" ) ) ;
619+ const dir = await mcTemp ( ) ;
602620 const dumpPath = join ( dir , "dump.jsonl" ) ;
603621 const body = makeBody ( [
604622 assistantMsg ( "t1" ) ,
0 commit comments