/** * Tab leases: one file per leased tab, exclusive-create claims, opaque tokens. * * WHY A FILE AND NOT MODULE STATE. Two Claude Code sessions each run their own * MCP server process against the same Chrome on port 9222 (see backend.ts's * header), so in-process state would protect two agents inside one process and * miss the case that actually collides. Intermittent protection is worse than * none, because a caller learns to distrust a refusal it only sometimes gets. * * WHY ONE FILE PER TAB AND NOT ONE REGISTRY. A shared registry file would need * its own lock to make "check if free, then claim" atomic, which relocates the * race one level up. Filesystem exclusive-create (the "wx" flag below) gives * that atomicity per tab for free. This is the first use of "wx" in the repo. * * WHY THE TOKEN EMBEDS A NONCE. Reclaiming a stale lease mints a fresh nonce, * so the superseded token stops matching by construction rather than by a check * someone could forget to write. See assertLeaseOk. * * THE ONE WRITE THAT COULD BREAK THAT GUARANTEE is touchLease, because it is * the only write here that is not an exclusive create. Everything else either * creates with "wx" or unlinks, so a record can never be modified in place and * a nonce can never survive a reclamation. touchLease has to rewrite an * existing file, so it re-reads and re-checks the nonce immediately before * writing and writes only what it just read. Anyone adding a second * non-exclusive write to this module owes the same guard, or the whole feature * loses the property it rests on. */ import { AsyncLocalStorage } from "node:async_hooks"; import { randomBytes } from "node:crypto"; import { mkdir, readFile, readdir, unlink, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { currentEndpoint, leaseNamespace, type Endpoint } from "./endpoints.ts"; /** Opaque ownership token. Hold it, pass it back, never parse or build one. */ export type LeaseToken = string; /** Ownership is keyed by backend plus id: a CDP targetId and a BiDi context id * are not guaranteed disjoint, so a bare id could collide across browsers. * * A NAMED CDP ENDPOINT (src/endpoints.ts) is its own browser, so it is its own * backend key: `chrome@`. The default endpoint stays plain "chrome", so * lease and origin filenames, and tokens, are byte-identical to every older * process sharing the directory. Because the endpoint rides IN the key, every * existing "same backend" comparison (liveBackend scoping, reap, list_pages * annotation, token checks) scopes by endpoint with no extra code: a target * list read from one port can never mark another port's lease stale. */ export type LeaseBackend = "chrome" | "firefox" | `chrome@${string}`; /** The Chrome lease key for an endpoint (by default the current dispatch scope's). */ export function chromeLeaseBackend(ep: Endpoint = currentEndpoint()): LeaseBackend { const ns = leaseNamespace(ep); return ns === undefined ? "chrome" : `chrome@${ns}`; } /** Regex source for a backend key as it appears in filenames and tokens. Endpoint * names are [A-Za-z0-9_]+, so a key never contains '-' or ':'. Shared with origins.ts. */ export const BACKEND_KEY_SOURCE = "chrome@[A-Za-z0-9_]+|chrome|firefox"; const BACKEND_KEY_RE = new RegExp(`^(?:${BACKEND_KEY_SOURCE})$`); export function isLeaseBackend(value: string): value is LeaseBackend { return BACKEND_KEY_RE.test(value); } export interface LeaseRecord { backend: LeaseBackend; targetId: string; /** The value embedded in the token, compared on every call. */ nonce: string; /** Owning MCP server process. */ pid: number; /** Caller-supplied agent label, surfaced in conflicts and list_leases. */ label: string; createdAt: number; lastUsedAt: number; ttlMs: number; /** * How this lease was taken, and therefore who may use it without a token. * true - the gate acquired it implicitly; passes for any call from `pid`. * false - claim_page or new_page{claim:true}; the token is required, even * from the owning process. * Absent on records written by <=1.4.0 and read as false, so an upgrade never * downgrades a held lease from token-required to pid-only. */ auto?: boolean; } /** 15 minutes. Refresh-on-use means an active agent never expires, so this only * bites on abandonment and can afford to be generous. */ export const DEFAULT_LEASE_TTL_MS = 900_000; /** Read per call, not at module load, so a test can redirect the directory. */ export function leaseDir(): string { return process.env.CDP_ARTIFACT_DIR ?? "/tmp/cdp-toolkit"; } export function leaseTtlMs(): number { const raw = Number(process.env.CDP_LEASE_TTL_MS); return Number.isFinite(raw) && raw > 0 ? raw : DEFAULT_LEASE_TTL_MS; } /** * Whether THIS process is the long-lived MCP server rather than a one-shot CLI * invocation. Module state, which this file's header otherwise forbids: that * prohibition is about state describing OTHER processes' ownership, which must * live on disk because two MCP servers drive one browser. This describes this * process's own role, which no other process can observe or contend for, so a * module flag is the correct scope rather than a violation of the rule. */ let longLived = false; /** Called once by mcp.ts at startup. cli.ts never calls it. The parameter * exists so a test can put the module back into CLI state. */ export function markLongLivedProcess(value = true): void { longLived = value; } /** Whether markLongLivedProcess ran: true in the MCP server, false under the CLI. Hidden tabs * (cdp/hiddenTabs.ts) gate on it for the same reason requireLease does. */ export function isLongLivedProcess(): boolean { return longLived; } /** * Strict mode: every tool call acquires a lease for the tab it resolves, and * stale agent tabs are reaped on read. Off by default, so 1.4.0 consumers are * unaffected. * * ALWAYS FALSE UNDER THE CLI, regardless of the env var, and that is not a * convenience. cli.ts already refuses claim_page because a CLI invocation is * one process per call, so its lease is reclaimable by the dead-pid rule the * moment the process exits, and "a lease that is reclaimable on arrival is * worse than no lease". Auto-acquire would mint exactly such a lease on every * call. The sharper danger is reap: those dead-pid records would read as * orphaned agent tabs, so a CLI user running list_pages twice could watch the * second run close the tabs the first run leased. Read per call, like * leaseDir(), so a test can flip it. */ export function requireLease(): boolean { if (!longLived) return false; const raw = (process.env.CDP_REQUIRE_LEASE ?? "").trim().toLowerCase(); return raw === "1" || raw === "true" || raw === "yes" || raw === "on"; } /** The label recorded when a caller supplies none. ONE definition, because it * is now written by three call sites (claim_page, new_page, and the origin * ledger) and a second rule would make the same tab describe itself two * different ways depending on which of them wrote first. */ export function defaultLabel(): string { return `pid-${process.pid}`; } function safeId(id: string): string { return id.replace(/[^A-Za-z0-9._-]/g, "_"); } /** Sibling of recorder.ts's rec-.jsonl, in the same ARTIFACT_DIR. */ export function leaseFile(backend: LeaseBackend, targetId: string): string { return join(leaseDir(), `lease-${backend}-${safeId(targetId)}.json`); } export function mintToken(backend: LeaseBackend, targetId: string, nonce: string): LeaseToken { return `${backend}:${targetId}:${nonce}`; } /** Split on the FIRST and LAST colon so an id containing a colon still parses. */ export function tokenParts(token: LeaseToken): { backend: LeaseBackend; targetId: string; nonce: string } | undefined { if (typeof token !== "string") return undefined; const first = token.indexOf(":"); const last = token.lastIndexOf(":"); if (first <= 0 || last <= first) return undefined; const backend = token.slice(0, first); if (!isLeaseBackend(backend)) return undefined; const targetId = token.slice(first + 1, last); const nonce = token.slice(last + 1); if (!targetId.length || !/^[0-9a-f]{24}$/.test(nonce)) return undefined; return { backend, targetId, nonce }; } export class LeaseConflictError extends Error { constructor( message: string, readonly targetId: string, readonly holder?: string, ) { super(message); this.name = "LeaseConflictError"; } } /** * Read a lease record, or undefined if the tab is genuinely unleased. * * "UNREADABLE" IS NOT "ABSENT", AND CONFLATING THEM IS WRONG IN BOTH DIRECTIONS * AT ONCE. Enforcement reads `undefined` as "free to take". If an EACCES, EIO or * EMFILE on a live, healthy lease file also produced `undefined`, a stranger * would be ADMITTED to a tab someone owns, while the true owner would be * REFUSED with "not leased any more" (its token names a tab that now reads as * unleased). Fd exhaustion on a long-lived MCP server is the most plausible * trigger. So every errno other than ENOENT is rethrown and the call fails. * * FAILING CLOSED HERE CANNOT REGRESS THE UPGRADE PATH, and that is what makes * it safe: a user who never calls claim_page and never passes claim:true has NO * lease files at all, so every read here is a plain ENOENT and this throw is * unreachable for them. Only a tab that already has a lease file can reach it. * * A record that reads fine but does not PARSE is treated as absent instead, * deliberately: a corrupt record can never become readable, so throwing would * brick that tab permanently with no in-product recovery, whereas an errno is * transient. A parse failure need not mean tampering; touchLease rewrites the * file by truncating it first, so a crash or ENOSPC mid-heartbeat can leave a * genuinely partial file with no tampering involved. listLeases reports such * a file so an operator can see it and delete it. */ export async function readLease(backend: LeaseBackend, targetId: string): Promise { let raw: string; try { raw = await readFile(leaseFile(backend, targetId), "utf8"); } catch (err) { if ((err as NodeJS.ErrnoException).code === "ENOENT") return undefined; // genuinely unleased throw err; // unreadable is not absent: refuse rather than hand out a held tab } try { const rec = JSON.parse(raw) as LeaseRecord; if (typeof rec?.nonce === "string" && typeof rec?.pid === "number") return rec; } catch { /* unparseable: fall through to undefined, see the doc comment above */ } return undefined; } export interface ClaimOptions { label: string; ttlMs?: number; now?: number; /** Live target ids from the browser, enabling the "target-gone" reclamation. */ liveIds?: readonly string[]; /** Marks the lease gate-acquired. See LeaseRecord.auto. Defaults to false: * an unmarked claim is an explicit one, which is the stricter reading. */ auto?: boolean; } /** Why an existing lease may be taken over. All three are sufficient alone. */ export type LeaseStaleReason = "dead-pid" | "expired" | "target-gone"; /** Signal-0 liveness probe. EPERM means the pid exists but belongs to another * user, which still counts as alive. */ export function isPidAlive(pid: number): boolean { if (!Number.isInteger(pid) || pid <= 0) return false; try { process.kill(pid, 0); return true; } catch (err) { return (err as NodeJS.ErrnoException).code === "EPERM"; } } /** * Classify a lease. `liveIds` is passed in by the caller rather than looked up * here on purpose: it is the only browser-derived input this module needs, and * taking it as data keeps leases.ts free of any CDP or BiDi import, which is * what makes the negative tests cheap to run. */ export function staleReason( rec: LeaseRecord, opts: { now: number; liveIds?: readonly string[] }, ): LeaseStaleReason | false { if (!isPidAlive(rec.pid)) return "dead-pid"; if (opts.now - rec.lastUsedAt > rec.ttlMs) return "expired"; if (opts.liveIds && !opts.liveIds.includes(rec.targetId)) return "target-gone"; return false; } /** Take exclusive ownership. Throws LeaseConflictError if the tab is held and * not stale. A stale lease is reclaimed: the old file is unlinked and a fresh * record with a fresh nonce is created, never overwritten in place, so the * superseded token stops matching by construction. */ export async function claimLease( backend: LeaseBackend, targetId: string, opts: ClaimOptions, ): Promise<{ token: LeaseToken; record: LeaseRecord }> { const now = opts.now ?? Date.now(); await mkdir(leaseDir(), { recursive: true }); for (let attempt = 0; attempt < 3; attempt++) { const record: LeaseRecord = { backend, targetId, // A fresh nonce on EVERY attempt, so a reclamation cannot reuse the // nonce it displaced. This is what invalidates the old owner's token. nonce: randomBytes(12).toString("hex"), pid: process.pid, label: opts.label, createdAt: now, lastUsedAt: now, ttlMs: opts.ttlMs ?? leaseTtlMs(), auto: opts.auto === true, }; try { await writeFile(leaseFile(backend, targetId), JSON.stringify(record), { flag: "wx" }); return { token: mintToken(backend, targetId, record.nonce), record }; } catch (err) { if ((err as NodeJS.ErrnoException).code !== "EEXIST") throw err; } const existing = await readLease(backend, targetId); if (!existing) continue; // released between our create and our read: retry const stale = staleReason(existing, { now, liveIds: opts.liveIds }); if (!stale) { throw new LeaseConflictError( `target '${targetId}' is already leased by '${existing.label}' (pid ${existing.pid}); call release_page with that lease's token, or list_leases to inspect it`, targetId, existing.label, ); } await unlink(leaseFile(backend, targetId)).catch(() => undefined); } throw new LeaseConflictError( `target '${targetId}' could not be claimed after 3 attempts: another process is claiming and releasing it concurrently`, targetId, ); } /** Give up a lease. Idempotent: releasing an already-gone lease is not an error. */ export async function releaseLease(token: LeaseToken): Promise<{ released: boolean }> { const parts = tokenParts(token); if (!parts) return { released: false }; const rec = await readLease(parts.backend, parts.targetId); if (!rec || rec.nonce !== parts.nonce) return { released: false }; await unlink(leaseFile(parts.backend, parts.targetId)).catch(() => undefined); return { released: true }; } /** Drop whatever lease a tab has, token or not. Only for close_page, which is * already authorized against the lease by the time it calls this. */ export async function releaseLeaseFor(backend: LeaseBackend, targetId: string): Promise<{ released: boolean }> { if (!(await readLease(backend, targetId))) return { released: false }; await unlink(leaseFile(backend, targetId)).catch(() => undefined); return { released: true }; } /* -------------------------------- the gate -------------------------------- */ /** * The lease token for the tool call currently being dispatched. * * This exists so enforcement can stay at ONE point per resolution path while * the token still reaches it. The token arrives as a key on a tool's args * object, many frames above resolveTarget, and threading a parameter through * all 45 tools would put the burden on every future contributor to remember * the check exists: one missed tool would silently defeat the feature for that * tool. mcp.ts and cli.ts open this scope once per dispatch instead, so a tool * added tomorrow is covered with no action from whoever writes it. */ const leaseScope = new AsyncLocalStorage(); export function withLeaseScope(lease: LeaseToken | undefined, fn: () => Promise): Promise { return leaseScope.run(lease, fn); } export function currentLease(): LeaseToken | undefined { return leaseScope.getStore(); } /** Pull the token off a tool's loosely-typed args object at the dispatch site. * Kept here rather than duplicated in mcp.ts and cli.ts so the two entry points * can never disagree about what counts as a token. An empty string is treated * as absent: a caller that sends lease:"" means "no lease", and letting it * through would trip the malformed-token branch with a confusing message. */ export function leaseFromArgs(args: unknown): LeaseToken | undefined { if (!args || typeof args !== "object") return undefined; const lease = (args as { lease?: unknown }).lease; return typeof lease === "string" && lease.length > 0 ? lease : undefined; } /** * Refresh lastUsedAt. Every checked call already touches this file, so keeping * an active lease alive needs no separate heartbeat. * * THIS IS THE ONE WRITE IN THIS MODULE THAT IS NOT AN EXCLUSIVE CREATE, so it * is the one write that could violate the nonce guarantee. `rec` was read some * time ago by the caller; between that read and this write another process can * reclaim the tab, which unlinks the file and creates a new one with a fresh * nonce. A blind `writeFile` of `rec` would then clobber the new owner's record * with the old nonce: the new owner's token would start failing and the * superseded one would start passing. That is the two-owner failure inverted. * * So this re-reads and compares the nonce immediately before writing, and * writes the record it just read rather than the caller's older copy. A lease * that was reclaimed or released underneath us is left alone. * * Errors are swallowed DELIBERATELY: this is a heartbeat, and a tool call that * did real work must not fail because a lastUsedAt refresh could not be * persisted. The cost of that choice is real and worth stating plainly: an * EACCES or ENOSPC here means the holder's lease quietly stops being refreshed * and expires under it after ttlMs, with no signal anywhere. If lease * expiry-under-load ever gets reported, this silence is the first place to look. */ export async function touchLease(rec: LeaseRecord, now: number = Date.now()): Promise { // readLease now THROWS on an unreadable file rather than reporting absence. // That throw is load-bearing at the gate and must not leak out of a // heartbeat, so it is caught here and treated as "leave the record alone", // which is the same thing this function does for any record it does not own. const current = await readLease(rec.backend, rec.targetId).catch(() => undefined); if (!current || current.nonce !== rec.nonce) return; // reclaimed or released: not ours to write await writeFile(leaseFile(current.backend, current.targetId), JSON.stringify({ ...current, lastUsedAt: now }), "utf8").catch( () => undefined, ); } export interface LeaseCheckContext { /** Explicit token. Falls back to the ambient dispatch scope when absent. */ lease?: LeaseToken; url?: string; title?: string; /** Live ids from the resolution the caller just did, for "target-gone". */ liveIds?: readonly string[]; now?: number; } function describeTarget(targetId: string, ctx: LeaseCheckContext): string { const url = ctx.url ? ` (url ${ctx.url})` : ctx.title ? ` (title ${JSON.stringify(ctx.title)})` : ""; return `target '${targetId}'${url}`; } /** * The single enforcement point, called from every target-resolution path. * * NOT an ADR-001 capability gap. ADR-001 says a capability a backend can never * do is ABSENT from tools/list rather than present and throwing, because that * gap is static and known before the first call. A lease conflict is the * opposite: the same tool against the same tab succeeds or fails depending on * who holds it at that instant. There is nothing to remove from tools/list, * because the tool is not unsupported, it is supported and currently spoken * for. So this throws, loudly, naming the holder. Do not "fix" the * inconsistency with ADR-001 by hiding leased tools from tools/list: the two * situations differ in kind, not degree. */ export async function assertLeaseOk( backend: LeaseBackend, targetId: string, ctx: LeaseCheckContext = {}, ): Promise { const token = ctx.lease ?? currentLease(); const now = ctx.now ?? Date.now(); // NOT wrapped in a catch. If the lease file exists but cannot be read, // readLease throws and that error propagates out of the gate, failing the // call. Swallowing it here would put us back where "unreadable" means // "unleased", which admits a stranger to a held tab. See readLease. const rec = await readLease(backend, targetId); const held = rec && !staleReason(rec, { now, liveIds: ctx.liveIds }) ? rec : undefined; const where = describeTarget(targetId, ctx); if (token === undefined) { if (!held) { // STRICT MODE: acquire rather than wave through. This is the change that // makes "you cannot drive a tab you do not hold" true, and it is done // here rather than by refusing because refusing would force every caller // to learn a claim/release protocol to do what used to just work. // Marked auto:true, which is what lets a later call from this same // process pass with no token. A LeaseConflictError from claimLease (two // processes racing to acquire the same tab, resolved by the "wx" // exclusive create) propagates: the loser is genuinely refused. if (requireLease()) { await claimLease(backend, targetId, { label: defaultLabel(), ttlMs: leaseTtlMs(), auto: true, now }); } return; // unleased, or leased-but-reclaimable: today's behavior } // An auto lease is owned by a PROCESS, not by a token, so any call from the // owning pid passes. NOT gated on requireLease(): an auto:true record can // only exist because strict mode created it, and gating this too would mean // turning the flag off locks this process out of leases it still holds. It // cannot regress 1.4.0, where no auto:true record can exist at all. if (held.auto === true && held.pid === process.pid) { await touchLease(held, now); return; } throw new LeaseConflictError( `${where} is leased by '${held.label}' (pid ${held.pid}). Pass that lease's token as the 'lease' argument, or call release_page to free it. list_leases shows every active lease.`, targetId, held.label, ); } const parts = tokenParts(token); if (!parts) { throw new LeaseConflictError( `malformed lease token for ${where}. Pass back the token claim_page returned, never a constructed one.`, targetId, ); } // The "is this token even about this tab" question comes FIRST, before the // "is this tab leased" question. Order matters: holding a token for TAB-A // must not change what happens when you touch an unrelated TAB-C. If the // mismatch check ran second, a token for another tab would fall into the // !held branch below and refuse every unleased tab with a message about a // lease that was "released or reclaimed", which is both wrong and confusing. // Under the ambient dispatch scope that is the COMMON case, not an edge one: // any agent holding one tab that touches a second unleased tab in the same // call arrives here, select_page via activatePage included. if (parts.backend !== backend || parts.targetId !== targetId) { // The token is about a different tab, so it says nothing about this one. // Under strict mode this tab still has to be acquired: an agent holding // tab A that reaches into unleased tab C is exactly the case the feature // exists to cover, and it is the COMMON case under the ambient dispatch // scope, not an edge one. if (!held) { if (requireLease()) { await claimLease(backend, targetId, { label: defaultLabel(), ttlMs: leaseTtlMs(), auto: true, now }); } return; } throw new LeaseConflictError( `the lease token you passed is for ${parts.backend} target '${parts.targetId}', not ${where}, which is leased by '${held.label}'. Resolve the tab you actually hold, or claim this one.`, targetId, held.label, ); } // From here the token names THIS tab, so the caller believes it holds this // tab. Being wrong about that is an error, never a pass-through. if (!held) { throw new LeaseConflictError( `${where} is not leased any more: the token you passed belongs to a lease that was released or reclaimed. Call claim_page again to take the tab.`, targetId, ); } // The nonce match is what makes reclamation safe. Without it a stalled agent // that comes back after its lease was reclaimed would sail through with a // token whose backend and targetId still line up, and two owners would be // driving one tab. claimLease mints a fresh nonce on every create, so the // superseded token can never match here. if (parts.nonce !== held.nonce) { throw new LeaseConflictError( `${where} was reclaimed and is now leased by '${held.label}' (pid ${held.pid}). Your token was invalidated by that reclamation. Call claim_page again if you still need this tab.`, targetId, held.label, ); } await touchLease(held, now); } /* ------------------------------- enumeration ------------------------------- */ export interface LeaseSummary { backend: LeaseBackend; targetId: string; label: string; pid: number; createdAt: number; lastUsedAt: number; ttlMs: number; pidAlive: boolean; stale: LeaseStaleReason | false; /** * Present ONLY on a row this function could not read: the errno of the failed * read, or "unparseable" for a file whose contents are not a lease record. * The row still appears, because the whole point of this tool is to show an * operator what is on disk, and a silently skipped file is the one thing that * makes an unreadable lease impossible to diagnose. Every other field on such * a row is a placeholder derived from the FILENAME and nothing else: backend * and targetId are real (the filename carries both), the rest are zeroed. It * reports stale:false so the row never reads as "free to take". */ unreadable?: string; } const LEASE_FILE_RE = new RegExp(`^lease-(${BACKEND_KEY_SOURCE})-(.+)\\.json$`); /** * Enumerate every lease on disk. Diagnosis only, so it needs no token. * * `liveIds` comes from ONE browser, but this enumeration spans both backends, * so `liveBackend` names which backend those ids belong to and the target-gone * test is applied only to records of that backend. Without that scoping, a * caller listing from Chrome would see every Firefox lease reported as * target-gone (its context ids are simply not in Chrome's target list), which * reads as "free to take" for a lease that is very much held. * * Residual: for records of the OTHER backend, the target-gone test is never * evaluated, so a cross-backend lease whose tab really did close reports * stale:false until its owning pid dies or its ttlMs elapses. That errs * toward "held", which is the safe direction, and a Firefox lease fails the * dead-pid test as soon as its per-invocation process exits. */ export async function listLeases( opts: { now?: number; liveIds?: readonly string[]; liveBackend?: LeaseBackend } = {}, ): Promise { const now = opts.now ?? Date.now(); let names: string[] = []; try { names = await readdir(leaseDir()); } catch (err) { // An absent directory means nobody has ever claimed anything, which is the // normal state for a user who never opts in and is genuinely "no leases". // Any OTHER errno means leases may exist and we cannot see them, and // answering "none" to that question is exactly the hidden failure this // function is supposed to expose. Fail loudly instead. if ((err as NodeJS.ErrnoException).code === "ENOENT") return []; throw err; } const out: LeaseSummary[] = []; for (const name of names) { const parts = LEASE_FILE_RE.exec(name); if (!parts) continue; // Everything the filename alone can tell us, used only to describe a row // whose contents we could not read. safeId() may have substituted // characters in the id, so this is an identifying label, not a key. const fromName = { backend: parts[1] as LeaseBackend, targetId: parts[2] as string, label: "", pid: 0, createdAt: 0, lastUsedAt: 0, ttlMs: 0, pidAlive: false, // Never "reclaimable": we do not know, and this errs toward held. stale: false as const, }; let rec: LeaseRecord; try { rec = JSON.parse(await readFile(join(leaseDir(), name), "utf8")) as LeaseRecord; } catch (err) { const code = (err as NodeJS.ErrnoException).code; // A file that vanished between readdir and readFile is not a problem; it // was released. Anything else is reported rather than skipped. if (code === "ENOENT") continue; out.push({ ...fromName, unreadable: code ?? "unparseable" }); continue; } if (typeof rec?.nonce !== "string" || typeof rec?.pid !== "number") { out.push({ ...fromName, unreadable: "unparseable" }); continue; } out.push({ backend: rec.backend, targetId: rec.targetId, label: rec.label, pid: rec.pid, createdAt: rec.createdAt, lastUsedAt: rec.lastUsedAt, ttlMs: rec.ttlMs, pidAlive: isPidAlive(rec.pid), // The nonce never leaves this function: it is not in LeaseSummary, so a // diagnosis read can never be turned into a forged token. stale: staleReason(rec, { now, liveIds: opts.liveBackend === undefined || opts.liveBackend === rec.backend ? opts.liveIds : undefined, }), }); } return out; }