Skip to content

Commit 8af595a

Browse files
committed
fix: add 30s TTL to device name resolver cache
The module-level device name cache was never invalidated, causing stale device names in long-running server processes. Now expires after 30 seconds so it refreshes across tool invocations while avoiding repeated devicectl shell-outs within a single invocation.
1 parent 5412fcf commit 8af595a

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

src/utils/device-name-resolver.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { readFileSync, unlinkSync } from 'node:fs';
33
import { tmpdir } from 'node:os';
44
import { join } from 'node:path';
55

6+
const CACHE_TTL_MS = 30_000;
7+
68
let cachedDevices: Map<string, string> | null = null;
9+
let cacheTimestamp = 0;
710

811
interface DeviceCtlEntry {
912
identifier: string;
@@ -12,7 +15,9 @@ interface DeviceCtlEntry {
1215
}
1316

1417
function loadDeviceNames(): Map<string, string> {
15-
if (cachedDevices) return cachedDevices;
18+
if (cachedDevices && Date.now() - cacheTimestamp < CACHE_TTL_MS) {
19+
return cachedDevices;
20+
}
1621

1722
const map = new Map<string, string>();
1823
const tmpFile = join(tmpdir(), `devicectl-list-${process.pid}.json`);
@@ -36,7 +41,7 @@ function loadDeviceNames(): Map<string, string> {
3641
}
3742
}
3843
} catch {
39-
// Device list unavailable return empty map, will fall back to UUID only
44+
// Device list unavailable -- return empty map, will fall back to UUID only
4045
} finally {
4146
try {
4247
unlinkSync(tmpFile);
@@ -46,6 +51,7 @@ function loadDeviceNames(): Map<string, string> {
4651
}
4752

4853
cachedDevices = map;
54+
cacheTimestamp = Date.now();
4955
return map;
5056
}
5157

0 commit comments

Comments
 (0)