Skip to content

Commit 410c1bd

Browse files
committed
fix(rpc): -1 never-expires sentinel for cacheTtlMs/cacheRemainingMs
Post-merge review on #245 (cubic): collapsing the never-lane to 0 made a warm lane numerically indistinguishable from a fresh/expired one for any consumer that reads the numeric fields without the cacheNeverExpires flag. -1 discriminates by value alone (falsy-value contract: -1 never / 0 expired-or-unset / N live), documented on the wire type. Pi's dialog is in-process (no JSON boundary) and legitimately keeps Infinity.
1 parent 8e496b8 commit 410c1bd

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,13 @@ describe("buildStatusDetail — cacheNeverExpires with 'never' TTL", () => {
487487
expect(detail.cacheExpired).toBe(false);
488488
// Infinity must NOT leak into the numeric RPC field — JSON.stringify
489489
// converts Infinity to null, violating the StatusDetail contract.
490-
expect(detail.cacheRemainingMs).toBe(0);
490+
// -1 is the never-expires sentinel: distinguishable from 0 (expired)
491+
// by the value alone, so a consumer that never learned the flag cannot
492+
// misread a warm lane as expired.
493+
expect(detail.cacheRemainingMs).toBe(-1);
494+
expect(detail.cacheTtlMs).toBe(-1);
491495
const roundTripped = JSON.parse(JSON.stringify(detail));
492-
expect(roundTripped.cacheRemainingMs).toBe(0);
496+
expect(roundTripped.cacheRemainingMs).toBe(-1);
493497
expect(roundTripped.cacheRemainingMs).not.toBeNull();
494498
} finally {
495499
closeQuietly(db);

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -736,15 +736,19 @@ export function buildStatusDetail(
736736
}
737737
detail.cacheTtlMs = safeParseTtl(detail.cacheTtl);
738738
if (detail.cacheTtlMs === Number.POSITIVE_INFINITY) {
739+
// Infinity does not survive JSON-RPC (JSON.stringify emits null), and
740+
// 0 would be indistinguishable from a fresh/expired lane to a consumer
741+
// that never learned the cacheNeverExpires convention. -1 is the
742+
// never-expires sentinel: the VALUES discriminate on their own
743+
// (-1 never / 0 expired-or-unset / N live), and the flag stays as a
744+
// convenience for consumers that prefer it.
739745
detail.cacheNeverExpires = true;
740-
detail.cacheTtlMs = 0;
746+
detail.cacheTtlMs = -1;
741747
}
742748
if (detail.lastResponseTime > 0) {
743749
const elapsed = Date.now() - detail.lastResponseTime;
744750
if (detail.cacheNeverExpires) {
745-
// Infinity does not survive JSON-RPC; cacheNeverExpires is the
746-
// authoritative flag — the TUI keys on it first.
747-
detail.cacheRemainingMs = 0;
751+
detail.cacheRemainingMs = -1;
748752
detail.cacheExpired = false;
749753
} else {
750754
detail.cacheRemainingMs = Math.max(0, detail.cacheTtlMs - elapsed);

‎packages/plugin/src/shared/rpc-types.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,19 @@ export interface StatusDetail extends SidebarSnapshot {
126126
isSubagent: boolean;
127127
pendingOps: Array<{ tagId: number; operation: string }>;
128128
contextLimit: number;
129+
/**
130+
* Parsed cache TTL in ms. -1 = never expires (cacheTtl "never"; Infinity
131+
* cannot ride JSON-RPC and 0 would be indistinguishable from unset). The
132+
* values discriminate without the cacheNeverExpires flag: -1 never /
133+
* N live. Falsy-value contract: see storage_versions for the precedent.
134+
*/
129135
cacheTtlMs: number;
136+
/** Remaining ms in the idle-TTL window. -1 = never expires; 0 = expired
137+
* (only meaningful when lastResponseTime > 0); N = live countdown. */
130138
cacheRemainingMs: number;
131139
cacheExpired: boolean;
132-
/** True when cacheTtl is "never" — the idle-TTL heuristic is disabled on this lane. */
140+
/** True when cacheTtl is "never" — the idle-TTL heuristic is disabled on
141+
* this lane. Redundant with cacheTtlMs === -1; kept as the readable form. */
133142
cacheNeverExpires?: boolean;
134143
executeThreshold: number;
135144
/**

0 commit comments

Comments
 (0)