Skip to content

Commit 16315e4

Browse files
author
Tehan
committed
fix(scheduler): keep cacheRemainingMs JSON-safe and restore invalid-TTL diagnostics
- rpc-handlers: the cacheNeverExpires branch assigned Infinity to cacheRemainingMs; JSON.stringify converts Infinity to null over RPC, violating the numeric StatusDetail contract. Use 0 and let the cacheNeverExpires flag carry the semantics (the TUI keys on it first). - computeHardCacheExpired: the extraction dropped the invalid-cache-ttl-fallback pass outcome and session log on parse failure. Add an onInvalid callback; the transform call site restores the exact pre-extraction record + log. - test: seed last_response_time in the never-TTL status test — the guarded branch only runs when lastResponseTime > 0, so the assertion was vacuous without it (verified red: Infinity revert now fails it).
1 parent e8fb5f4 commit 16315e4

4 files changed

Lines changed: 61 additions & 6 deletions

File tree

‎packages/plugin/src/hooks/magic-context/transform-context-state.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,39 @@ describe("computeHardCacheExpired", () => {
163163
const sixMinutesAgo = now - 6 * 60 * 1000;
164164
expect(computeHardCacheExpired("garbage", sixMinutesAgo, now)).toBe(true);
165165
});
166+
167+
it("invokes onInvalid callback for invalid TTL but not for valid ones", () => {
168+
const now = Date.now();
169+
const tenDaysAgo = now - 10 * 24 * 60 * 60 * 1000;
170+
const invalidErrors: unknown[] = [];
171+
172+
// Invalid TTL: callback fires + 5m fallback applied
173+
const result = computeHardCacheExpired("bad-format", tenDaysAgo, now, (error) => {
174+
invalidErrors.push(error);
175+
});
176+
expect(invalidErrors.length).toBe(1);
177+
expect(invalidErrors[0]).toBeInstanceOf(Error);
178+
expect(result).toBe(true); // 5m fallback, 10-day-old → expired
179+
180+
// Valid TTLs: callback NOT invoked
181+
const validErrors: unknown[] = [];
182+
computeHardCacheExpired("never", tenDaysAgo, now, (error) => {
183+
validErrors.push(error);
184+
});
185+
expect(validErrors.length).toBe(0);
186+
187+
computeHardCacheExpired("5m", tenDaysAgo, now, (error) => {
188+
validErrors.push(error);
189+
});
190+
expect(validErrors.length).toBe(0);
191+
});
192+
193+
it("omitting onInvalid is harmless (5m fallback still applies)", () => {
194+
const now = Date.now();
195+
const tenDaysAgo = now - 10 * 24 * 60 * 60 * 1000;
196+
// No callback — should not throw, should still fall back to 5m
197+
expect(computeHardCacheExpired("bad-format", tenDaysAgo, now)).toBe(true);
198+
// "never" with no callback still works
199+
expect(computeHardCacheExpired("never", tenDaysAgo, now)).toBe(false);
200+
});
166201
});

‎packages/plugin/src/hooks/magic-context/transform.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,16 +246,21 @@ export function __getMessageTokensCacheForTest(
246246
*
247247
* Returns false when cacheTtl is "never" (Infinity) because any finite
248248
* elapsed time is < Infinity.
249+
*
250+
* @param onInvalid Optional callback invoked when cacheTtl fails to parse;
251+
* the 5m fallback is applied AFTER the callback returns.
249252
*/
250253
export function computeHardCacheExpired(
251254
cacheTtl: string,
252255
lastResponseTime: number,
253256
now: number,
257+
onInvalid?: (error: unknown) => void,
254258
): boolean {
255259
let ttlMs: number;
256260
try {
257261
ttlMs = parseCacheTtl(cacheTtl);
258-
} catch {
262+
} catch (error) {
263+
onInvalid?.(error);
259264
ttlMs = 5 * 60 * 1000;
260265
}
261266
return lastResponseTime > 0 && now - lastResponseTime >= ttlMs;
@@ -2049,6 +2054,10 @@ export function createTransform(deps: TransformDeps) {
20492054
sessionMeta.cacheTtl,
20502055
sessionMeta.lastResponseTime,
20512056
Date.now(),
2057+
(error) => {
2058+
passOutcome.record("invalid-cache-ttl-fallback");
2059+
sessionLog(sessionId, "invalid cache_ttl; using the 5m default:", error);
2060+
},
20522061
);
20532062
const m0HardSignals = {
20542063
systemHash: hardSystemHash,

‎packages/plugin/src/plugin/rpc-handlers.test.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,15 +474,23 @@ describe("buildStatusDetail — cacheNeverExpires with 'never' TTL", () => {
474474

475475
// Force-create the session meta row so the UPDATE lands on an existing row.
476476
db.prepare(`INSERT INTO session_meta (session_id) VALUES (?)`).run(sessionId);
477-
db.prepare("UPDATE session_meta SET cache_ttl = ? WHERE session_id = ?").run(
478-
"never",
479-
sessionId,
480-
);
477+
// Seed last_response_time: the cacheNeverExpires branch only runs
478+
// inside `if (lastResponseTime > 0)` — without this the test would
479+
// pass even if Infinity leaked into cacheRemainingMs.
480+
db.prepare(
481+
"UPDATE session_meta SET cache_ttl = ?, last_response_time = ? WHERE session_id = ?",
482+
).run("never", Date.now() - 60_000, sessionId);
481483

482484
const detail = buildStatusDetail(db, sessionId, directory);
483485

484486
expect(detail.cacheNeverExpires).toBe(true);
485487
expect(detail.cacheExpired).toBe(false);
488+
// Infinity must NOT leak into the numeric RPC field — JSON.stringify
489+
// converts Infinity to null, violating the StatusDetail contract.
490+
expect(detail.cacheRemainingMs).toBe(0);
491+
const roundTripped = JSON.parse(JSON.stringify(detail));
492+
expect(roundTripped.cacheRemainingMs).toBe(0);
493+
expect(roundTripped.cacheRemainingMs).not.toBeNull();
486494
} finally {
487495
closeQuietly(db);
488496
}

‎packages/plugin/src/plugin/rpc-handlers.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,10 @@ export function buildStatusDetail(
742742
if (detail.lastResponseTime > 0) {
743743
const elapsed = Date.now() - detail.lastResponseTime;
744744
if (detail.cacheNeverExpires) {
745-
detail.cacheRemainingMs = Number.POSITIVE_INFINITY;
745+
// Infinity does not survive JSON-RPC; cacheNeverExpires is the
746+
// authoritative flag — the TUI keys on it first.
747+
detail.cacheRemainingMs = 0;
748+
detail.cacheExpired = false;
746749
} else {
747750
detail.cacheRemainingMs = Math.max(0, detail.cacheTtlMs - elapsed);
748751
detail.cacheExpired = detail.cacheRemainingMs === 0;

0 commit comments

Comments
 (0)