Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions docker/harness/manager.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
Expand Down
16 changes: 13 additions & 3 deletions docker/harness/preinstall.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 */ }
}
}

/**
Expand Down
28 changes: 26 additions & 2 deletions docker/setup/auto-update.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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"));
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
34 changes: 33 additions & 1 deletion docker/setup/cache.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -213,6 +216,8 @@ export function createHarnessCache({
refreshIntervalMs = 15_000,
liveTtlMs = 30_000,
cheapReserveMs = 1000,
freshLocal = false,
onLocalChange,
now = Date.now,
} = {}) {
let warm = null; // { harnesses, degraded, at, generation }
Expand Down Expand Up @@ -279,7 +284,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));
Expand Down Expand Up @@ -330,6 +335,33 @@ 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;
onLocalChange?.(row.id);
}
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) {
Expand Down
83 changes: 51 additions & 32 deletions docker/setup/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand All @@ -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.
Expand Down Expand Up @@ -668,7 +675,14 @@ 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. 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 };
},
cheap: async () => {
Expand All @@ -677,6 +691,11 @@ 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,
freshLocal: true,
onLocalChange: forgetSignInState,
});

// Who each source control CLI is signed in as, the same way: a check asks the
Expand Down
33 changes: 33 additions & 0 deletions tests/codex-cli-sign-in.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
Loading
Loading