Skip to content

Commit c643ca1

Browse files
test: de-flake transform_decisions retention cap via injectable cap
The retention-cap test wrote TRANSFORM_DECISIONS_RETENTION+5 (2005) rows, and each writeRow opens a fresh DB connection (the off-hot-path telemetry writer's design). Under CI load that loop exceeded the 5s test timeout and flaked the plugin check intermittently (unrelated to the change under test). The prune SQL is cap-agnostic (LIMIT ?), so the behavior is identical at any cap: add a test-only retention override and exercise the prune with a cap of 10 instead of 2000. Runtime drops from ~10s (timeout) to ~0.25s. A guard test pins the real TRANSFORM_DECISIONS_RETENTION constant so the production cap can't silently drift. Co-authored-by: Alfonso [Magic Context] <288211368+alfonso-magic-context@users.noreply.github.com>
1 parent aa93c58 commit c643ca1

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

‎packages/plugin/src/features/magic-context/transform-decision-log.test.ts‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,23 @@ function rowCount(): number {
5555
}
5656

5757
describe("transform_decisions retention cap", () => {
58-
it("prunes to the newest TRANSFORM_DECISIONS_RETENTION rows per (session,harness)", () => {
58+
// The prune SQL is cap-agnostic (`LIMIT ?`), so we inject a tiny cap to
59+
// exercise it with a handful of rows. Writing the real 2000+ cap opened
60+
// that many fresh DB connections in a loop and timed out under CI load.
61+
// The override removes the flake without weakening the assertion.
62+
const TEST_CAP = 10;
63+
64+
beforeEach(() => {
65+
__test.setRetentionForTests(TEST_CAP);
66+
});
67+
68+
it("prunes to the newest cap rows per (session,harness)", () => {
5969
// Write cap + 5 rows with strictly increasing ts and distinct message ids.
60-
const total = TRANSFORM_DECISIONS_RETENTION + 5;
70+
const total = TEST_CAP + 5;
6171
for (let i = 0; i < total; i++) {
6272
__test.writeRow(dbPath, baseRow(`msg-${i}`, 1000 + i));
6373
}
64-
expect(rowCount()).toBe(TRANSFORM_DECISIONS_RETENTION);
74+
expect(rowCount()).toBe(TEST_CAP);
6575

6676
// The oldest (smallest ts) must be gone; the newest must remain.
6777
const oldest = db
@@ -75,10 +85,15 @@ describe("transform_decisions retention cap", () => {
7585
});
7686

7787
it("does not prune below the cap", () => {
78-
for (let i = 0; i < 10; i++) {
88+
for (let i = 0; i < TEST_CAP - 1; i++) {
7989
__test.writeRow(dbPath, baseRow(`m-${i}`, 1000 + i));
8090
}
81-
expect(rowCount()).toBe(10);
91+
expect(rowCount()).toBe(TEST_CAP - 1);
92+
});
93+
94+
it("keeps the production retention constant sane", () => {
95+
// Guard against accidental drift of the real cap.
96+
expect(TRANSFORM_DECISIONS_RETENTION).toBe(2000);
8297
});
8398
});
8499

‎packages/plugin/src/features/magic-context/transform-decision-log.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ const scheduledWriteTokensBySession = new Map<string, Set<symbol>>();
8282

8383
let writerOverrideForTests: TransformDecisionWriter | null = null;
8484

85+
// Tests override the retention cap so the prune can be exercised with a handful
86+
// of rows instead of writing TRANSFORM_DECISIONS_RETENTION+ rows (each opening a
87+
// fresh DB connection), which timed out under CI load. The prune SQL is
88+
// cap-agnostic (LIMIT ?), so a small cap verifies identical behavior.
89+
let retentionOverrideForTests: number | null = null;
90+
8591
export function normalizeMaterializeReason(
8692
harness: TransformDecisionHarness,
8793
reason: string | null | undefined,
@@ -353,7 +359,7 @@ function writeTransformDecisionRow(dbPath: string, row: TransformDecisionRow): v
353359
row.harness,
354360
row.sessionId,
355361
row.harness,
356-
TRANSFORM_DECISIONS_RETENTION,
362+
retentionOverrideForTests ?? TRANSFORM_DECISIONS_RETENTION,
357363
);
358364
} finally {
359365
closeQuietly(db);
@@ -373,10 +379,14 @@ export const __test = {
373379
lastBoundMessageIdBySession.clear();
374380
scheduledWriteTokensBySession.clear();
375381
writerOverrideForTests = null;
382+
retentionOverrideForTests = null;
376383
},
377384
setWriterForTests(writer: TransformDecisionWriter | null): void {
378385
writerOverrideForTests = writer;
379386
},
387+
setRetentionForTests(cap: number | null): void {
388+
retentionOverrideForTests = cap;
389+
},
380390
writeRow(dbPath: string, row: TransformDecisionRow): void {
381391
writeTransformDecisionRow(dbPath, row);
382392
},

0 commit comments

Comments
 (0)