From 16c4ba47a386c4e02547cd151ecaf4b726bc8564 Mon Sep 17 00:00:00 2001 From: 0libote <79215282+0libote@users.noreply.github.com> Date: Sat, 14 Nov 2026 11:08:45 +0000 Subject: [PATCH 1/3] fix(setup): atomic policy writes, unique preinstall temps, cheap status polls Policy and update-state writes now go through a unique temp file plus rename, so a power loss or concurrent POSTs from two tabs can never leave a torn file that the catch-all reader turns into defaults (silently disabling schedules). Preinstall records get the same random-suffix plus wx treatment, so overlapping writers no longer interleave JSON and trigger redundant reinstalls. The harness status() read defaults to cheap (no CLI probes); authenticated verdicts refresh in the background on a 15-minute TTL and after mutations. --- docker/harness/manager.mjs | 12 +- docker/harness/preinstall.mjs | 16 +- docker/setup/auto-update.mjs | 28 ++- docker/setup/server.mjs | 11 +- tests/setup-harness-robustness.test.mjs | 279 ++++++++++++++++++++++++ 5 files changed, 337 insertions(+), 9 deletions(-) create mode 100644 tests/setup-harness-robustness.test.mjs diff --git a/docker/harness/manager.mjs b/docker/harness/manager.mjs index cc7a2879..6416bb88 100644 --- a/docker/harness/manager.mjs +++ b/docker/harness/manager.mjs @@ -1080,11 +1080,17 @@ export function createHarnessManager(options = {}) { } } - /** Read-only facts for every catalogue entry. */ - async function status(options = {}) { + /** + * Read-only facts for every catalogue entry. Cheap by default: one + * `mise ls` plus filesystem checks, no CLI subprocesses. Auth verdicts + * come only with `authenticate: true` (one bounded probe per installed + * harness), which the setup service refreshes in the background on a + * 15-minute TTL and after mutations - never on every 15-second poll. + */ + async function status({ authenticate = false } = {}) { const snap = await snapshot(); const harnesses = await Promise.all( - CATALOGUE.map((entry) => factsFor(entry, snap, options)), + CATALOGUE.map((entry) => factsFor(entry, snap, { authenticate })), ); return { harnesses, degraded: snap.degraded }; } diff --git a/docker/harness/preinstall.mjs b/docker/harness/preinstall.mjs index c80ef5fe..da3ec70b 100644 --- a/docker/harness/preinstall.mjs +++ b/docker/harness/preinstall.mjs @@ -28,6 +28,7 @@ // server talks to cannot be guessed, so not even `all` includes them. import path from "node:path"; import { pathToFileURL } from "node:url"; +import { randomBytes } from "node:crypto"; import { CATALOGUE, SOURCE_CONTROL, TOOLCHAINS } from "./catalogue.mjs"; import { createFs, processAlive, processStartTime } from "./io.mjs"; @@ -100,9 +101,18 @@ async function readRecord(fs, file) { async function writeRecord(fs, file, record) { await fs.mkdir(path.dirname(file), { recursive: true }); - const tmp = `${file}.tmp.${process.pid}`; - await fs.writeFile(tmp, `${JSON.stringify(record, null, 2)}\n`, { encoding: "utf8", mode: 0o600 }); - await fs.rename(tmp, file); + // Unique per write, never pid-only: two overlapping writers (a rerun + // while a previous run still sleep-retries in its busy loop, or pid + // reuse after a restart) must not share one temp path and interleave + // JSON into a torn record. `wx` refuses a surprising collision instead + // of silently mixing, and the temp is removed either way. + const tmp = `${file}.tmp.${process.pid}.${randomBytes(6).toString("hex")}`; + try { + await fs.writeFile(tmp, `${JSON.stringify(record, null, 2)}\n`, { encoding: "utf8", mode: 0o600, flag: "wx" }); + await fs.rename(tmp, file); + } finally { + try { await fs.rm?.(tmp, { force: true }); } catch { /* renamed, or best effort */ } + } } /** diff --git a/docker/setup/auto-update.mjs b/docker/setup/auto-update.mjs index 29b70f9b..101beb5e 100644 --- a/docker/setup/auto-update.mjs +++ b/docker/setup/auto-update.mjs @@ -10,6 +10,7 @@ // first-start install running. Agent turns already running inside T3 Code // are not directly visible to this service, so idle is a client-activity // signal, not proof that no turn is running. +import { randomBytes } from "node:crypto"; export const POLICY_VERSION = 2; export const DEFAULT_POLICY = Object.freeze({ @@ -91,6 +92,29 @@ export function normalizePolicy(input) { export const policyPath = (stateDir) => `${stateDir}/setup/auto-update.json`; export const statePath = (stateDir) => `${stateDir}/setup/auto-update-state.json`; +/** + * Replace durably: a power loss or a concurrent POST from another tab must + * never leave a torn file behind. The readers treat any unreadable file as + * defaults (everything disabled), so a half-written policy would silently + * turn schedules off. Write to a unique temp file and rename over the + * target, so the path holds either the old or the new document, never a + * mix. `fs` is the injected subset the server passes in; older callers + * without `rename` keep the direct write. + */ +async function writeFileAtomic(fs, file, text) { + if (typeof fs.rename !== "function") { + await fs.writeFile(file, text); + return; + } + const tmp = `${file}.tmp.${process.pid}.${randomBytes(6).toString("hex")}`; + try { + await fs.writeFile(tmp, text, { encoding: "utf8", flag: "wx" }); + await fs.rename(tmp, file); + } finally { + try { await fs.rm?.(tmp, { force: true }); } catch { /* renamed, or best effort */ } + } +} + export async function readPolicy(fs, stateDir) { try { const raw = JSON.parse(await fs.readFile(policyPath(stateDir), "utf8")); @@ -103,7 +127,7 @@ export async function readPolicy(fs, stateDir) { export async function writePolicy(fs, stateDir, input) { const policy = normalizePolicy(input); await fs.mkdir(`${stateDir}/setup`, { recursive: true }); - await fs.writeFile(policyPath(stateDir), `${JSON.stringify(policy, null, 2)}\n`); + await writeFileAtomic(fs, policyPath(stateDir), `${JSON.stringify(policy, null, 2)}\n`); return policy; } @@ -139,7 +163,7 @@ export async function writeUpdateState(fs, stateDir, patch) { const current = await readUpdateState(fs, stateDir); const next = { ...current, ...patch }; await fs.mkdir(`${stateDir}/setup`, { recursive: true }); - await fs.writeFile(statePath(stateDir), `${JSON.stringify(next, null, 2)}\n`); + await writeFileAtomic(fs, statePath(stateDir), `${JSON.stringify(next, null, 2)}\n`); return next; } diff --git a/docker/setup/server.mjs b/docker/setup/server.mjs index 8dcd3c0f..72f36622 100644 --- a/docker/setup/server.mjs +++ b/docker/setup/server.mjs @@ -668,7 +668,13 @@ const withT3SignIn = async (harness, harnesses) => { const harnessCache = createHarnessCache({ full: async () => { const harness = await loadHarness(); - const { harnesses, degraded } = await harness.status({}); + // The only authenticated read on the poll path: one bounded probe per + // installed harness. Every poll in between serves the last verdicts + // from `warm` instead of forking (~11 CLI execs per 15s poll, each with + // a 20s timeout, mostly discarded when the 4s budget loses). Mutations + // invalidate the cache, so a sign-in, install or uninstall still shows + // at once; nothing else needs fresh auth state. + const { harnesses, degraded } = await harness.status({ authenticate: true }); return { harnesses: await withT3SignIn(harness, harnesses), degraded }; }, cheap: async () => { @@ -677,6 +683,9 @@ const harnessCache = createHarnessCache({ return { harnesses: await withT3SignIn(harness, harnesses), degraded }; }, budgetMs: HARNESS_BUDGET_MS, + // Auth verdicts refresh here at most every 15 minutes; the manager also + // caches each verdict, and every mutation invalidates this cache outright. + refreshIntervalMs: 15 * 60 * 1000, }); // Who each source control CLI is signed in as, the same way: a check asks the diff --git a/tests/setup-harness-robustness.test.mjs b/tests/setup-harness-robustness.test.mjs new file mode 100644 index 00000000..6912dc69 --- /dev/null +++ b/tests/setup-harness-robustness.test.mjs @@ -0,0 +1,279 @@ +// Regression tests for setup/harness robustness: atomic policy writes, +// unique preinstall temp files, and cheap-by-default status polls. +// +// node --test tests/setup-harness-robustness.test.mjs +import assert from "node:assert/strict"; +import path from "node:path"; +import test from "node:test"; + +import { + DEFAULT_POLICY, + normalizePolicy, + readPolicy, + readUpdateState, + writePolicy, + writeUpdateState, +} from "../docker/setup/auto-update.mjs"; +import { createHarnessCache } from "../docker/setup/cache.mjs"; +import { createHarnessManager } from "../docker/harness/index.mjs"; +import { runPreinstall } from "../docker/harness/preinstall.mjs"; + +const STATE_DIR = "/home/t3/.local/state/mise"; + +/** In-memory fs with tmp+rename semantics and wx support. */ +function memoryFs({ failRename = false } = {}) { + const files = new Map(); + const writtenTemps = []; + const fs = { + files, + writtenTemps, + async readFile(file) { + if (!files.has(file)) throw Object.assign(new Error(`ENOENT: ${file}`), { code: "ENOENT" }); + return files.get(file); + }, + async writeFile(file, data, options = {}) { + const flag = typeof options === "object" ? options.flag : undefined; + if (flag === "wx" && files.has(file)) throw Object.assign(new Error("EEXIST"), { code: "EEXIST" }); + if (file.includes(".tmp.")) writtenTemps.push(file); + files.set(file, String(data)); + }, + async mkdir() {}, + async rename(from, to) { + if (failRename) throw Object.assign(new Error("EIO: power loss"), { code: "EIO" }); + if (!files.has(from)) throw Object.assign(new Error(`ENOENT: ${from}`), { code: "ENOENT" }); + files.set(to, files.get(from)); + files.delete(from); + }, + async rm(file, options = {}) { + if (!files.has(file) && options.force) return; + files.delete(file); + }, + }; + return fs; +} + +test("a crash between temp write and rename keeps the prior policy", async () => { + // Seed a complete policy with a working fs, then fail renames to simulate + // power loss mid-commit. + const seed = memoryFs(); + const enabled = normalizePolicy({ providers: { enabled: true, checkMinutes: 60, idleMinutes: 10 } }); + await writePolicy(seed, STATE_DIR, { providers: { enabled: true, checkMinutes: 60, idleMinutes: 10 } }); + const committed = seed.files.get(`${STATE_DIR}/setup/auto-update.json`); + assert.ok(committed.includes('"enabled": true')); + + const crashing = memoryFs({ failRename: true }); + crashing.files.set(`${STATE_DIR}/setup/auto-update.json`, committed); + await assert.rejects(() => writePolicy(crashing, STATE_DIR, { providers: { enabled: false } })); + // The target still holds the whole prior document: readers never see a mix. + assert.equal(crashing.files.get(`${STATE_DIR}/setup/auto-update.json`), committed); + assert.deepEqual(await readPolicy(crashing, STATE_DIR), enabled); +}); + +test("concurrent policy writes land whole, never torn", async () => { + const fs = memoryFs(); + const a = { providers: { enabled: true, checkMinutes: 60, idleMinutes: 10 } }; + const b = { toolchains: { enabled: true, checkMinutes: 120, idleMinutes: 20 } }; + await Promise.all([writePolicy(fs, STATE_DIR, a), writePolicy(fs, STATE_DIR, b)]); + const raw = fs.files.get(`${STATE_DIR}/setup/auto-update.json`); + const parsed = JSON.parse(raw); + assert.ok( + (parsed.providers.enabled === true && parsed.toolchains.enabled === false) || + (parsed.providers.enabled === false && parsed.toolchains.enabled === true), + `expected one whole write to win, got ${raw}`, + ); + // Unique temp names per write: no shared pid-only path to interleave on. + assert.ok(fs.writtenTemps.length >= 2, `expected temp files, got ${fs.writtenTemps}`); + assert.equal(new Set(fs.writtenTemps).size, fs.writtenTemps.length, "temp names collided"); + assert.ok(fs.writtenTemps.every((tmp) => !tmp.endsWith(`.tmp.${process.pid}`)), "pid-only temp name used"); +}); + +test("a torn policy file recovers to defaults (schedules stay off, never half-on)", async () => { + const fs = memoryFs(); + fs.files.set(`${STATE_DIR}/setup/auto-update.json`, '{"version":2,"providers":{"enabled":tru'); + assert.deepEqual(await readPolicy(fs, STATE_DIR), JSON.parse(JSON.stringify(DEFAULT_POLICY))); + const stateFs = memoryFs(); + stateFs.files.set(`${STATE_DIR}/setup/auto-update-state.json`, '{"lastProviderCheckAt":12'); + const state = await readUpdateState(stateFs, STATE_DIR); + assert.equal(state.lastProviderCheckAt, null); + assert.equal(state.lastProviderRunAt, null); +}); + +test("a crash between temp write and rename keeps the prior update state", async () => { + const seed = memoryFs(); + await writeUpdateState(seed, STATE_DIR, { lastResult: "before" }); + const committed = seed.files.get(`${STATE_DIR}/setup/auto-update-state.json`); + const crashing = memoryFs({ failRename: true }); + crashing.files.set(`${STATE_DIR}/setup/auto-update-state.json`, committed); + await assert.rejects(() => writeUpdateState(crashing, STATE_DIR, { lastResult: "after" })); + assert.equal(crashing.files.get(`${STATE_DIR}/setup/auto-update-state.json`), committed); + assert.equal((await readUpdateState(crashing, STATE_DIR)).lastResult, "before"); +}); + +/** Fake manager with the preinstall surface: nothing installed, installs stick. */ +function fakeManager() { + const present = new Set(); + const side = () => ({ + async resolve(id) { + return { id, installed: present.has(id), installedVersion: present.has(id) ? "9.9.9" : null, + version: present.has(id) ? "9.9.9" : null, providedBy: null }; + }, + async install(id) { + present.add(id); + return { ok: true, code: "ok", harness: { installed: true, installedVersion: "9.9.9" } }; + }, + }); + const manager = side("agent"); + manager.toolchains = side("toolchain"); + manager.sourceControl = side("source-control"); + manager.refreshLinks = async () => {}; + return manager; +} + +const preinstallRun = (fs, env, extra = {}) => runPreinstall({ + manager: fakeManager(), + fs, + env, + stateDir: STATE_DIR, + sleep: async () => {}, + now: () => 1_700_000_000_000, + pid: 4242, + startTimeOf: () => "100", + log: () => {}, + ...extra, +}); + +test("concurrent preinstall writes use unique temp names, never a pid-only clobber", async () => { + const fs = memoryFs(); + await Promise.all([ + preinstallRun(fs, { T3_PREINSTALL: "claude" }), + preinstallRun(fs, { T3_PREINSTALL: "codex" }), + ]); + assert.ok(fs.writtenTemps.length >= 2, `expected temp files, got ${fs.writtenTemps}`); + assert.equal(new Set(fs.writtenTemps).size, fs.writtenTemps.length, "temp names collided"); + assert.ok( + fs.writtenTemps.every((tmp) => !tmp.endsWith(`.tmp.${process.pid}`) && !tmp.endsWith(".tmp.4242")), + `pid-only temp name used: ${fs.writtenTemps}`, + ); + // Whatever won, the record is whole JSON, not interleaved fragments. + const record = JSON.parse(fs.files.get(`${STATE_DIR}/t3-preinstall.json`)); + assert.equal(record.schema, 1); + assert.ok(record.items && typeof record.items === "object"); +}); + +test("a stale pid-only temp from a crashed run is left alone and never read", async () => { + const fs = memoryFs(); + // The exact pid-only name the old code wrote: a crashed run may have left + // it behind with a torn document in it. + const stale = `${STATE_DIR}/t3-preinstall.json.tmp.${process.pid}`; + fs.files.set(stale, '{"torn":'); + await preinstallRun(fs, { T3_PREINSTALL: "claude" }); + assert.equal(fs.files.get(stale), '{"torn":', "stale temp was touched"); + const record = JSON.parse(fs.files.get(`${STATE_DIR}/t3-preinstall.json`)); + assert.equal(record.schema, 1); + assert.equal(record.items["agent:claude"]?.state, "done"); +}); + +// --- cheap-by-default status polls ------------------------------------------- + +const HOME = "/home/t3"; +const INSTALL = path.join(HOME, ".local/share/mise/installs/claude/2.1.0"); +const EXECUTABLE = path.join(INSTALL, "claude"); + +/** Manager world with claude installed; counts non-mise CLI probe subprocesses. */ +function probeWorld() { + const files = new Map(); + const probes = []; + const miseListing = { + claude: [{ + version: "2.1.0", + requested_version: "2.1.0", + install_path: INSTALL, + installed: true, + active: true, + source: { type: "mise.toml", path: path.join(HOME, ".config/mise/config.toml") }, + }], + }; + const fs = { + async readFile(file) { + if (!files.has(file)) throw Object.assign(new Error(`ENOENT: ${file}`), { code: "ENOENT" }); + return files.get(file); + }, + async writeFile(file, data) { files.set(file, String(data)); }, + async mkdir() {}, + async rename(from, to) { files.set(to, files.get(from)); files.delete(from); }, + async stat(target) { + if (target === EXECUTABLE) return { isFile: () => true, mode: 0o755, mtimeMs: 1, size: 8 }; + throw Object.assign(new Error(`ENOENT: ${target}`), { code: "ENOENT" }); + }, + async exists() { return false; }, + async readdir() { return []; }, + }; + const run = async (argv) => { + if (argv[0] === "mise") return { code: 0, stdout: JSON.stringify(miseListing), stderr: "", error: null }; + probes.push(argv); + if (argv[0] === EXECUTABLE) return { code: 0, stdout: '{"loggedIn":true}', stderr: "", error: null }; + return { code: 0, stdout: "", stderr: "", error: null }; + }; + const manager = createHarnessManager({ + env: {}, + home: HOME, + fs, + run, + now: () => 1_700_000_000_000, + pid: 4242, + arch: "x64", + }); + return { manager, probes }; +} + +test("status() defaults to cheap: no CLI probes spawned", async () => { + const { manager, probes } = probeWorld(); + const { harnesses } = await manager.status(); + const claude = harnesses.find((row) => row.id === "claude"); + assert.equal(claude.installed, true); + assert.equal(claude.authenticated, null, "cheap polls report no verdict, not a fresh probe"); + assert.deepEqual(probes, [], "default status() must not fork CLI probes"); +}); + +test("status({ authenticate: true }) still probes for a fresh verdict", async () => { + const { manager, probes } = probeWorld(); + const { harnesses } = await manager.status({ authenticate: true }); + const claude = harnesses.find((row) => row.id === "claude"); + assert.equal(claude.authenticated, true); + assert.ok(probes.some((argv) => argv[0] === EXECUTABLE), "expected a CLI probe"); +}); + +test("resolve() still probes by default, for callers that need fresh auth", async () => { + const { manager, probes } = probeWorld(); + const facts = await manager.resolve("claude"); + assert.equal(facts.authenticated, true); + assert.ok(probes.some((argv) => argv[0] === EXECUTABLE), "expected a CLI probe"); +}); + +test("auth refresh happens only on TTL expiry or mutation", async () => { + let fullCalls = 0; + let clock = 1_000_000; + const cache = createHarnessCache({ + full: async () => { + fullCalls += 1; + return { harnesses: [{ id: "claude", signedIn: true }], degraded: [] }; + }, + cheap: async () => ({ harnesses: [{ id: "claude", signedIn: false }], degraded: [] }), + budgetMs: 1000, + refreshIntervalMs: 15 * 60 * 1000, + now: () => clock, + }); + await cache.snapshot(); + assert.equal(fullCalls, 1); + // Polls inside the 15-minute window reuse the warm verdicts, no refresh. + for (let poll = 0; poll < 5; poll += 1) { + clock += 15_000; + const snap = await cache.snapshot(); + assert.equal(snap.source, "cache"); + assert.deepEqual(snap.harnesses, [{ id: "claude", signedIn: true }]); + } + assert.equal(fullCalls, 1, "polls must not re-probe inside the TTL"); + // A mutation invalidates: the next poll refreshes immediately. + await cache.invalidate(); + assert.equal(fullCalls, 2); +}); From c24bc25154860e3027fa5271982b3f7ded11ee3b Mon Sep 17 00:00:00 2001 From: Ziyang Zeng Date: Sat, 14 Nov 2026 15:06:00 -0800 Subject: [PATCH 2/3] fix: keep local agent facts fresh between authentication checks --- docker/setup/cache.mjs | 30 +++++++++- docker/setup/server.mjs | 73 ++++++++++++++----------- tests/codex-cli-sign-in.test.mjs | 33 +++++++++++ tests/setup-harness-robustness.test.mjs | 48 ++++++++++++++++ 4 files changed, 151 insertions(+), 33 deletions(-) diff --git a/docker/setup/cache.mjs b/docker/setup/cache.mjs index d2f51ae0..b99950a0 100644 --- a/docker/setup/cache.mjs +++ b/docker/setup/cache.mjs @@ -201,6 +201,9 @@ export function createProviderCache({ * * `snapshotCheap()` answers the explicit `authenticate=false` polls from a * bounded cheap read without starting an auth refresh. + * With `freshLocal`, normal snapshots also read current local facts within + * the same budget. Only a matching installation's sign-in verdict is reused; + * versions, lifecycle state and native connection settings stay current. * * `full` and `cheap` must already return the public card shape * (`{ harnesses, degraded }`); this never throws, so the endpoint always has @@ -213,6 +216,7 @@ export function createHarnessCache({ refreshIntervalMs = 15_000, liveTtlMs = 30_000, cheapReserveMs = 1000, + freshLocal = false, now = Date.now, } = {}) { let warm = null; // { harnesses, degraded, at, generation } @@ -279,7 +283,7 @@ export function createHarnessCache({ // One poll answers within `budgetMs` overall: the refresh race keeps // `cheapReserveMs` back, and the cheap fallback gets only what is left, // rather than each stage spending the whole budget in turn. - const snapshot = async () => { + const cachedSnapshot = async () => { const started = now(); const refreshBudget = Math.max(250, budgetMs - cheapReserveMs); const remaining = () => Math.max(250, budgetMs - (now() - started)); @@ -330,6 +334,30 @@ export function createHarnessCache({ return cheapAnswer(remaining()); }; + const installation = row => JSON.stringify([row.id, row.installed, row.runnable, + row.executable, row.installedVersion ?? row.version, row.credentialsPresent, row.authMethod]); + const snapshot = async () => { + const started = now(); + const answer = await cachedSnapshot(); + if (!freshLocal || ["cheap", "unavailable"].includes(answer.source)) return answer; + const local = await cheapAnswer(Math.max(250, budgetMs - (now() - started))); + if (local.source === "unavailable") { + const degraded = [...new Map([...answer.degraded, ...local.degraded].map(row => [JSON.stringify(row), row])).values()]; + return { ...answer, stale: true, degraded }; + } + const cached = new Map(answer.harnesses.map(row => [row.id, row])); + let changed = cached.size !== local.harnesses.length; + const harnesses = local.harnesses.map(row => { + const prior = cached.get(row.id), same = prior && installation(prior) === installation(row); + if (!same) changed = true; + return { ...row, signedIn: same ? prior.signedIn : null }; + }); + // A CLI install or credential removal outside Setup must not wait out the + // long auth TTL, nor inherit the previous executable's sign-in verdict. + if (changed) lastRefreshStart = 0; + return { ...answer, harnesses, degraded: local.degraded }; + }; + const snapshotCheap = async () => { const raced = await withTimeout(Promise.resolve().then(cheap), budgetMs); if (raced.ok) { diff --git a/docker/setup/server.mjs b/docker/setup/server.mjs index 72f36622..fb7c670b 100644 --- a/docker/setup/server.mjs +++ b/docker/setup/server.mjs @@ -378,12 +378,14 @@ const useCliSignIn = async (id) => { // about anyone's credentials, so it must not turn a known "Not signed in" // into "not readable". const lastKnown = new Map(); -const stableAuth = (id, value) => { +const stableAuth = (id, value, identity) => { // Muse's null is a real state after credentials are saved, rather than a // missed probe. Retaining its earlier false would keep it signed out forever. if (id === "muse") return value; - if (value === null) return lastKnown.has(id) ? lastKnown.get(id) : null; - lastKnown.set(id, value); + const prior = lastKnown.get(id); + if (prior && prior.identity !== identity) lastKnown.delete(id); + if (typeof value !== "boolean") return lastKnown.get(id)?.value ?? null; + lastKnown.set(id, { identity, value }); return value; }; @@ -392,34 +394,39 @@ const stableAuth = (id, value) => { // the card actually renders: exact versions, runnable state, the operation in // flight, and the failure that stopped the last one. Auth comes from the manager's bounded probe of // the managed executable - never from a PATH search. -const toPublicHarness = (facts) => ({ - id: facts.id, - name: facts.name, - installed: facts.installed, - runnable: facts.runnable, - signedIn: stableAuth(facts.id, facts.authenticated), - version: facts.installedVersion ?? facts.recordedVersion ?? null, - installedVersion: facts.installedVersion, - recordedVersion: facts.recordedVersion, - verifiedVersion: facts.verifiedVersion ?? null, - configuredVersion: facts.configuredVersion ?? null, - executable: facts.executable, - configured: facts.configured, - minimumVersion: facts.minimumVersion ?? null, - minimumSatisfied: facts.minimumSatisfied ?? null, - supported: facts.supported, - failed: facts.failed, - failure: facts.failure, - operation: facts.operation, - operationState: facts.operationState, - inProgress: facts.inProgress, - managedVersions: facts.managedVersions ?? [], - credentialsPresent: facts.credentials?.present ?? false, - canSignIn: Boolean(AGENTS[facts.id]?.signin), - canSetKey: Boolean(AGENTS[facts.id]?.apiKey), - keyKind: AGENTS[facts.id]?.apiKey?.kind ?? null, - authMethod: authMethodOf(facts), -}); +const toPublicHarness = (facts) => { + const authMethod = authMethodOf(facts); + const identity = JSON.stringify([facts.installed, facts.executable, + facts.installedVersion ?? facts.recordedVersion, facts.credentials?.present, authMethod]); + return { + id: facts.id, + name: facts.name, + installed: facts.installed, + runnable: facts.runnable, + signedIn: stableAuth(facts.id, facts.authenticated, identity), + version: facts.installedVersion ?? facts.recordedVersion ?? null, + installedVersion: facts.installedVersion, + recordedVersion: facts.recordedVersion, + verifiedVersion: facts.verifiedVersion ?? null, + configuredVersion: facts.configuredVersion ?? null, + executable: facts.executable, + configured: facts.configured, + minimumVersion: facts.minimumVersion ?? null, + minimumSatisfied: facts.minimumSatisfied ?? null, + supported: facts.supported, + failed: facts.failed, + failure: facts.failure, + operation: facts.operation, + operationState: facts.operationState, + inProgress: facts.inProgress, + managedVersions: facts.managedVersions ?? [], + credentialsPresent: facts.credentials?.present ?? false, + canSignIn: Boolean(AGENTS[facts.id]?.signin), + canSetKey: Boolean(AGENTS[facts.id]?.apiKey), + keyKind: AGENTS[facts.id]?.apiKey?.kind ?? null, + authMethod, + }; +}; // How an agent is signed in, where that can be told without asking it: a key // in the environment, or Codex's stored API key against its browser sign-in. @@ -673,7 +680,8 @@ const harnessCache = createHarnessCache({ // from `warm` instead of forking (~11 CLI execs per 15s poll, each with // a 20s timeout, mostly discarded when the 4s budget loses). Mutations // invalidate the cache, so a sign-in, install or uninstall still shows - // at once; nothing else needs fresh auth state. + // at once. Local settings and installation facts are read separately on + // every poll; only authentication verdicts use the longer cache. const { harnesses, degraded } = await harness.status({ authenticate: true }); return { harnesses: await withT3SignIn(harness, harnesses), degraded }; }, @@ -686,6 +694,7 @@ const harnessCache = createHarnessCache({ // Auth verdicts refresh here at most every 15 minutes; the manager also // caches each verdict, and every mutation invalidates this cache outright. refreshIntervalMs: 15 * 60 * 1000, + freshLocal: true, }); // Who each source control CLI is signed in as, the same way: a check asks the diff --git a/tests/codex-cli-sign-in.test.mjs b/tests/codex-cli-sign-in.test.mjs index f581053a..b5032ed0 100644 --- a/tests/codex-cli-sign-in.test.mjs +++ b/tests/codex-cli-sign-in.test.mjs @@ -18,6 +18,39 @@ const codexFacts = (f, authenticated) => ({ codex: { } }); const codexRow = async (f) => (await (await f.request('/harnesses')).json()).harnesses.find((row) => row.id === 'codex'); +test('native connection changes are visible on a warm Setup cache without probing every agent', { timeout: 30000 }, async t => { + const f=await authFixture();t.after(f.close); + await fs.mkdir(path.dirname(settingsFile(f)),{recursive:true}); + const existing=structuredClone(managed);existing.providerInstances.codex.config.setupMode='existing'; + await fs.writeFile(settingsFile(f),JSON.stringify(existing)); + await f.facts(codexFacts(f,true)); + const warm=await f.request('/auth/use-cli',{agent:'codex'}); + assert.equal(warm.status,200); + assert.equal((await warm.json()).changed,false); + assert.equal((await codexRow(f)).t3OwnSignIn,false,'warm the cache while T3 uses the CLI'); + // T3 Code or a terminal can change this file without calling Setup's APIs. + await fs.writeFile(settingsFile(f),JSON.stringify(managed)); + assert.equal((await codexRow(f)).t3OwnSignIn,true,'the next poll must see T3 managing its own connection'); + const switched=await f.request('/auth/use-cli',{agent:'codex'}); + assert.equal(switched.status,200); + assert.equal((await switched.json()).changed,true); + assert.equal((await codexRow(f)).t3OwnSignIn,false); +}); + +test('an unreadable sign-in for a new executable cannot reuse the old executable verdict', { timeout: 30000 }, async t => { + const f=await authFixture();t.after(f.close); + await fs.mkdir(path.dirname(settingsFile(f)),{recursive:true}); + const existing=structuredClone(managed);existing.providerInstances.codex.config.setupMode='existing'; + await fs.writeFile(settingsFile(f),JSON.stringify(existing)); + await f.facts(codexFacts(f,true)); + assert.equal((await f.request('/auth/use-cli',{agent:'codex'})).status,200); + assert.equal((await codexRow(f)).signedIn,true); + const changed=codexFacts(f,null);changed.codex.installedVersion='0.167.0';changed.codex.executable=f.executable+'.new'; + await f.facts(changed); + assert.equal((await codexRow(f)).signedIn,null,'the changed installation has no verdict yet'); + assert.equal((await codexRow(f)).signedIn,null,'an inconclusive refresh cannot carry a verdict across executables'); +}); + test('Setup points T3 Code at the CLI sign-in only when asked, and only with one to use', { timeout: 30000 }, async (t) => { const f = await authFixture(); t.after(f.close); await fs.mkdir(path.dirname(settingsFile(f)), { recursive: true }); diff --git a/tests/setup-harness-robustness.test.mjs b/tests/setup-harness-robustness.test.mjs index 6912dc69..cf51433f 100644 --- a/tests/setup-harness-robustness.test.mjs +++ b/tests/setup-harness-robustness.test.mjs @@ -277,3 +277,51 @@ test("auth refresh happens only on TTL expiry or mutation", async () => { await cache.invalidate(); assert.equal(fullCalls, 2); }); + +test('fresh local facts retain cached auth without retaining old connection modes or versions',async()=>{ + let calls=0,clock=1_000_000; + let row={id:'codex',executable:'/managed/codex/1/bin',version:'1',installed:true,runnable:true,credentialsPresent:true,t3OwnSignIn:false}; + const cache=createHarnessCache({freshLocal:true,now:()=>clock,refreshIntervalMs:15*60*1000, + full:async()=>{calls++;return {harnesses:[{...row,signedIn:true}],degraded:[]};}, + cheap:async()=>({harnesses:[{...row,signedIn:null}],degraded:[]})}); + assert.equal((await cache.snapshot()).harnesses[0].signedIn,true); + row={...row,t3OwnSignIn:true,operation:'external-update'};clock+=15_000; + const mode=await cache.snapshot(); + assert.equal(mode.harnesses[0].t3OwnSignIn,true); + assert.equal(mode.harnesses[0].operation,'external-update'); + assert.equal(mode.harnesses[0].signedIn,true); + assert.equal(calls,1,'native mode changes need no new authentication probes'); + row={...row,version:'2',executable:'/managed/codex/2/bin'};clock+=15_000; + const updated=await cache.snapshot(); + assert.equal(updated.harnesses[0].version,'2'); + assert.equal(updated.harnesses[0].signedIn,null,'a new executable cannot inherit the old sign-in verdict'); + await cache.snapshot(); + assert.equal(calls,2,'an installation change makes the next authentication refresh due'); +}); + +test('fresh local inventories remove missing agents and report local read failures honestly',async()=>{ + let rows=[{id:'codex',installed:true,executable:'/managed/codex',version:'1',runnable:true}],fail=false; + const cache=createHarnessCache({freshLocal:true, + full:async()=>({harnesses:rows.map(row=>({...row,signedIn:true})),degraded:[]}), + cheap:async()=>{if(fail)throw new Error('local inventory unavailable');return {harnesses:rows.map(row=>({...row,signedIn:null})),degraded:[]};}}); + await cache.snapshot();rows=[]; + assert.deepEqual((await cache.snapshot()).harnesses,[],'a cached row cannot resurrect an agent removed locally'); + fail=true; + const unavailable=await cache.snapshot(); + assert.equal(unavailable.stale,true); + assert.ok(unavailable.degraded.some(row=>String(row.error).includes('local inventory unavailable'))); +}); + +test('fresh local reads share the status budget and cannot hang a warm response',async()=>{ + let stalled=false; + const row={id:'codex',installed:true,runnable:true,executable:'/managed/codex',version:'1'}; + const cache=createHarnessCache({freshLocal:true,budgetMs:400, + full:async()=>({harnesses:[{...row,signedIn:true}],degraded:[]}), + cheap:async()=>{if(stalled)return new Promise(()=>{});return {harnesses:[{...row,signedIn:null}],degraded:[]};}}); + await cache.snapshot();stalled=true; + const started=Date.now(),answer=await cache.snapshot(); + assert.ok(Date.now()-started<550,'one local read must not add a second request budget'); + assert.equal(answer.harnesses[0].signedIn,true); + assert.equal(answer.stale,true); + assert.ok(answer.degraded.some(row=>row.what==='harness probes')); +}); From 9fc66500599f8dfd39b99ed7fae0c46efb3dfd08 Mon Sep 17 00:00:00 2001 From: Ziyang Zeng Date: Sat, 14 Nov 2026 15:10:17 -0800 Subject: [PATCH 3/3] fix: discard probe verdicts after local installation changes --- docker/setup/cache.mjs | 6 +++++- docker/setup/server.mjs | 1 + tests/setup-harness-robustness.test.mjs | 5 ++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/docker/setup/cache.mjs b/docker/setup/cache.mjs index b99950a0..c63a8aa1 100644 --- a/docker/setup/cache.mjs +++ b/docker/setup/cache.mjs @@ -217,6 +217,7 @@ export function createHarnessCache({ liveTtlMs = 30_000, cheapReserveMs = 1000, freshLocal = false, + onLocalChange, now = Date.now, } = {}) { let warm = null; // { harnesses, degraded, at, generation } @@ -349,7 +350,10 @@ export function createHarnessCache({ let changed = cached.size !== local.harnesses.length; const harnesses = local.harnesses.map(row => { const prior = cached.get(row.id), same = prior && installation(prior) === installation(row); - if (!same) changed = true; + if (!same) { + changed = true; + onLocalChange?.(row.id); + } return { ...row, signedIn: same ? prior.signedIn : null }; }); // A CLI install or credential removal outside Setup must not wait out the diff --git a/docker/setup/server.mjs b/docker/setup/server.mjs index fb7c670b..cac931aa 100644 --- a/docker/setup/server.mjs +++ b/docker/setup/server.mjs @@ -695,6 +695,7 @@ const harnessCache = createHarnessCache({ // caches each verdict, and every mutation invalidates this cache outright. refreshIntervalMs: 15 * 60 * 1000, freshLocal: true, + onLocalChange: forgetSignInState, }); // Who each source control CLI is signed in as, the same way: a check asks the diff --git a/tests/setup-harness-robustness.test.mjs b/tests/setup-harness-robustness.test.mjs index cf51433f..aa1a4462 100644 --- a/tests/setup-harness-robustness.test.mjs +++ b/tests/setup-harness-robustness.test.mjs @@ -280,8 +280,9 @@ test("auth refresh happens only on TTL expiry or mutation", async () => { test('fresh local facts retain cached auth without retaining old connection modes or versions',async()=>{ let calls=0,clock=1_000_000; + const cleared=[]; let row={id:'codex',executable:'/managed/codex/1/bin',version:'1',installed:true,runnable:true,credentialsPresent:true,t3OwnSignIn:false}; - const cache=createHarnessCache({freshLocal:true,now:()=>clock,refreshIntervalMs:15*60*1000, + const cache=createHarnessCache({freshLocal:true,onLocalChange:id=>cleared.push(id),now:()=>clock,refreshIntervalMs:15*60*1000, full:async()=>{calls++;return {harnesses:[{...row,signedIn:true}],degraded:[]};}, cheap:async()=>({harnesses:[{...row,signedIn:null}],degraded:[]})}); assert.equal((await cache.snapshot()).harnesses[0].signedIn,true); @@ -291,10 +292,12 @@ test('fresh local facts retain cached auth without retaining old connection mode assert.equal(mode.harnesses[0].operation,'external-update'); assert.equal(mode.harnesses[0].signedIn,true); assert.equal(calls,1,'native mode changes need no new authentication probes'); + assert.deepEqual(cleared,[],'connection mode changes leave the matching CLI verdict cached'); row={...row,version:'2',executable:'/managed/codex/2/bin'};clock+=15_000; const updated=await cache.snapshot(); assert.equal(updated.harnesses[0].version,'2'); assert.equal(updated.harnesses[0].signedIn,null,'a new executable cannot inherit the old sign-in verdict'); + assert.deepEqual(cleared,['codex'],'clear the manager verdict before the next full read can reuse it'); await cache.snapshot(); assert.equal(calls,2,'an installation change makes the next authentication refresh due'); });