From 3daefa68246348a20c3e15c9709c3c1d6c21439d Mon Sep 17 00:00:00 2001 From: 0libote <79215282+0libote@users.noreply.github.com> Date: Thu, 19 Nov 2026 22:38:48 +0000 Subject: [PATCH 1/2] fix: six silent-failure bugs found in a repo audit - retention: a previousVersion naming an uninstalled version (an upgrade interrupted before its state write) unprotects the installed fallback; fall back to the newest installed version - zip: an entry with an empty symlink target passed every safety check and failed the whole extraction at symlink(''); disclose and skip it - connect: revoked-but-unlinked reported 'signin' instead of 'off' - sign-in: a session that never printed its URL was never killed or settled by the TTL; rescue every non-terminal state like the siblings - manager: guard the failure-record write so a full state volume cannot replace the operation's own error - t3-pair/t3-expose: keep a failed pipeline from aborting under set -e before the script's own error handling runs --- .github/workflows/build.yml | 1 + docker/bin/t3-expose | 6 +++-- docker/bin/t3-pair | 4 +++- docker/harness/manager.mjs | 10 +++++--- docker/harness/retention.mjs | 12 ++++++++-- docker/setup/connect.mjs | 2 +- docker/setup/files/zip-format.mjs | 1 + docker/setup/server.mjs | 2 +- tests/connect.test.mjs | 1 + tests/files-archives.test.mjs | 6 +++++ tests/harness-manager.test.mjs | 21 ++++++++++++++++ tests/harness-retention.test.mjs | 13 ++++++++++ tests/t3-pair.test.sh | 40 +++++++++++++++++++++++++++++++ 13 files changed, 109 insertions(+), 10 deletions(-) create mode 100644 tests/t3-pair.test.sh diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f2a7ccb8..6144cacc 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -54,6 +54,7 @@ jobs: bash tests/run-t3.test.sh bash tests/persist-credentials.test.sh bash tests/setup-key-exposure.test.sh + bash tests/t3-pair.test.sh sh -n docker/setup/credentials/askpass.sh shellcheck -s sh -S warning docker/setup/credentials/askpass.sh for f in docker/setup/app.js docker/setup/client/*.js docker/setup/credentials.mjs docker/setup/credentials/*.mjs docker/setup/plugins.mjs docker/setup/plugins/*.mjs docker/setup/design/ui.js docker/router/*.mjs docker/runtime/*.mjs; do diff --git a/docker/bin/t3-expose b/docker/bin/t3-expose index e5efeafd..636a0211 100755 --- a/docker/bin/t3-expose +++ b/docker/bin/t3-expose @@ -136,9 +136,11 @@ url="" for _ in $(seq 1 40); do sleep 1 printf '.' - line="$(api GET /ports | jq -r --argjson p "$port" ' + # The setup server restarts on its own loop; one refused or 5xx answer is + # a blip, not a verdict, so keep polling instead of dying under `set -e`. + line="$(api GET /ports 2>/dev/null | jq -r --argjson p "$port" ' (.tunnels // []) | map(select(.port == $p)) | first - | if . == null then "|" else "\(.state)|\(.url // "")|\(.error // "")" end')" + | if . == null then "|" else "\(.state)|\(.url // "")|\(.error // "")" end' || true)" case "$line" in open\|*) url="$(printf '%s' "$line" | cut -d'|' -f2)"; break ;; failed\|*) printf '\n'; echo "t3-expose: $(printf '%s' "$line" | cut -d'|' -f3)" >&2; exit 1 ;; diff --git a/docker/bin/t3-pair b/docker/bin/t3-pair index 208a6a53..672724fa 100644 --- a/docker/bin/t3-pair +++ b/docker/bin/t3-pair @@ -73,7 +73,9 @@ args=(auth pairing create --base-url "$base_url" --ttl "$ttl" --json) [ -n "$label" ] && args+=(--label "$label") # `t3 auth` logs migration chatter on stderr; keep stdout clean for the JSON. -response="$("$T3_INFRA_LAUNCHER" "${args[@]}" 2>/dev/null | sed -n '/^{/,$p')" +# `|| true` lets the failed-mint handling below run: under `set -o pipefail` +# the substitution's exit status would otherwise abort the script first. +response="$("$T3_INFRA_LAUNCHER" "${args[@]}" 2>/dev/null | sed -n '/^{/,$p' || true)" if [ -z "$response" ]; then echo "t3-pair: failed to mint a pairing token" >&2 diff --git a/docker/harness/manager.mjs b/docker/harness/manager.mjs index 6416bb88..63e8b51c 100644 --- a/docker/harness/manager.mjs +++ b/docker/harness/manager.mjs @@ -390,9 +390,13 @@ export function createHarnessManager(options = {}) { // it was, without an error to explain. const cancelled = error?.code === "cancelled" || Boolean(signal?.aborted); const message = cancelled ? "cancelled" : String(error?.message ?? error); - await state.updateEntry(ctx, section, id, { - operation: { kind, state: cancelled ? "cancelled" : "failed", startedAt: null, finishedAt: ctx.now(), error: cancelled ? null : message }, - }); + // The failure record itself is best effort: a state volume that just + // filled up must not replace the operation's own error. + try { + await state.updateEntry(ctx, section, id, { + operation: { kind, state: cancelled ? "cancelled" : "failed", startedAt: null, finishedAt: ctx.now(), error: cancelled ? null : message }, + }); + } catch { /* the row keeps its in-progress record; the next start treats that as interrupted */ } outcome = { ok: false, code: cancelled ? "cancelled" : error?.code ?? "failed", error: message }; } finally { try { await held.release(); } diff --git a/docker/harness/retention.mjs b/docker/harness/retention.mjs index 4ad7ef85..655b0a3a 100644 --- a/docker/harness/retention.mjs +++ b/docker/harness/retention.mjs @@ -16,6 +16,14 @@ const sections = ["harnesses", "toolchains", "packages"]; const recordsOf = (saved) => sections.flatMap((section) => Object.entries(saved[section] || {}).map(([id, record]) => ({ section, id, record }))); const versionsOf = (record) => [...new Set((record.managedVersions || []).filter((v) => typeof v === "string" && exact.test(v)))]; +/** The version automatic retention keeps as the rollback fallback: the recorded + * previous version while that one is actually installed, else the newest other + * version. A record interrupted mid-upgrade can name a version that is gone; + * trusting it would leave no installed version protected. */ +const previousVersionOf = (record, versions) => versions.includes(record.previousVersion) + ? record.previousVersion + : versions.filter((version) => version !== record.version).at(-1); + function providerPaths(value, result = []) { if (!value || typeof value !== "object") return result; for (const [key, child] of Object.entries(value)) { @@ -74,7 +82,7 @@ export async function planRetention(ctx, { saved, only, includePrevious = false const records = only ? [only] : recordsOf(saved); const needsReferences = records.some(({ record }) => { const versions = versionsOf(record); - const previous = record.previousVersion || versions.filter((version) => version !== record.version).at(-1); + const previous = previousVersionOf(record, versions); return versions.some((version) => version !== record.version && (includePrevious || version !== previous)); }); let refs = null, warning = preferences.error; @@ -84,7 +92,7 @@ export async function planRetention(ctx, { saved, only, includePrevious = false const items = []; for (const { section, id, record } of records) { const versions = versionsOf(record); - const previous = record.previousVersion || versions.filter((v) => v !== record.version).at(-1); + const previous = previousVersionOf(record, versions); for (const version of versions) { const entry = section === "packages" ? { name: id, miseTool: id } : section === "harnesses" ? getHarness(id) : getManagedTool(id); if (!entry) continue; diff --git a/docker/setup/connect.mjs b/docker/setup/connect.mjs index d223fcc2..b25548e8 100644 --- a/docker/setup/connect.mjs +++ b/docker/setup/connect.mjs @@ -70,7 +70,7 @@ export function connectState(json) { const authenticated = json.authenticated === true; const revoked = json.revoked === true; const linked = json.linked === true && !revoked; - const state = revoked ? "signin" : linked ? "on" : !desired ? "off" : authenticated ? "pending" : "signin"; + const state = !desired ? "off" : revoked ? "signin" : linked ? "on" : authenticated ? "pending" : "signin"; const relayClient = json.relayClient && typeof json.relayClient === "object" ? json.relayClient : {}; return { state, diff --git a/docker/setup/files/zip-format.mjs b/docker/setup/files/zip-format.mjs index ad895c63..8bb586eb 100644 --- a/docker/setup/files/zip-format.mjs +++ b/docker/setup/files/zip-format.mjs @@ -80,6 +80,7 @@ export async function inspectZip(value,{signal}={}) { budget();if((linkBytes+=entry.bytes)>16*1024*1024)fail('too-large','This ZIP has too much link metadata. Extract a smaller archive.'); if(entry.bytes>4096){entry.skip='link target is too long';skipped.push({entry:entry.name,reason:entry.skip});continue;} try{entry.target=utf8.decode(await entryBytes(handle,entry,{signal,maxBytes:4096,deadline}));}catch(error){if(error.code||error.name==='AbortError')throw error;entry.skip='invalid link target';skipped.push({entry:entry.name,reason:entry.skip});continue;} + if(!entry.target){entry.skip='empty link target';skipped.push({entry:entry.name,reason:entry.skip});continue;} if(entry.target.includes('\0')||entry.target.includes('\\')||entry.target.startsWith('/'))entry.skip='link'; const parts=path.posix.dirname(entry.name)==='.'?[]:path.posix.dirname(entry.name).split('/'); for(const part of entry.target.split('/'))if(part==='..'){if(!parts.length){entry.skip='link';break;}parts.pop();}else if(part&&part!=='.')parts.push(part); diff --git a/docker/setup/server.mjs b/docker/setup/server.mjs index fd5444c3..075c4412 100644 --- a/docker/setup/server.mjs +++ b/docker/setup/server.mjs @@ -1635,7 +1635,7 @@ const startSignin = async (agentId) => { sessions.set(id, session); setTimeout(() => { - if (sessions.get(id)?.state?.startsWith("awaiting")) { + if (!TERMINAL_STATES.has(session.state)) { try { session.child.kill(); } catch {} sessionStore.settle(session, "failed", "Timed out waiting for the browser step."); } diff --git a/tests/connect.test.mjs b/tests/connect.test.mjs index 0e077400..f8c42d89 100644 --- a/tests/connect.test.mjs +++ b/tests/connect.test.mjs @@ -67,6 +67,7 @@ test("the saved state reads as off, signed out, waiting on a start, or on", () = assert.equal(on.relayHost, "relay.t3.codes"); assert.equal(on.publishAgentActivity, true); assert.equal(connectState({ ...base, authenticated: true }).state, "off", "signed in but not asked for is off"); + assert.equal(connectState({ ...base, revoked: true, linked: true }).state, "off", "revoked but not asked for stays off"); }); test("anything but that JSON is no state, and a malformed relay URL is no host", () => { diff --git a/tests/files-archives.test.mjs b/tests/files-archives.test.mjs index 59807ac6..b97539e7 100644 --- a/tests/files-archives.test.mjs +++ b/tests/files-archives.test.mjs @@ -56,6 +56,12 @@ test('internal link chains stay contained; dangling, cyclic and destination-depe const info=await inspectZip(archive);assert.deepEqual(info.entries.map(row=>row.name),['file','good','chain']);assert.equal(info.skipped.length,3); await fs.symlink(f.home,path.join(f.workspace,'existing'));await fs.writeFile(path.join(f.workspace,'file'),'existing content');const preview=await f.archives.extractPreview({path:archive});await assert.rejects(f.archives.extract({token:preview.token,into:'here',choices:{file:'keep-both'}}),/new folder/);assert.equal(await fs.readFile(path.join(f.workspace,'file'),'utf8'),'existing content'); }); +test('a link with an empty target is disclosed and skipped, not a failed extraction',async t=>{ + const f=await filesFixture(t),archive=path.join(f.workspace,'empty-link.zip'),stage=path.join(f.workspace,'out');await fs.writeFile(archive,zipFixture([{name:'safe',data:'ok'},{name:'l',mode:0o120777,data:''}])); + const info=await inspectZip(archive);assert.deepEqual(info.entries.map(row=>row.name),['safe']);assert.equal(info.skipped.find(row=>row.entry==='l').reason,'empty link target'); + await extractEntries(archive,info,stage);assert.equal(await fs.readFile(path.join(stage,'safe'),'utf8'),'ok');assert.equal(await fs.lstat(path.join(stage,'l')).then(()=>true,()=>false),false); +}); + test('low disk space preserves an informative preview, refuses publication, and can be reviewed again after freeing space',async t=>{ let available=1024**3+100;const f=await archiveServices(t,{space:async()=>available}),archive=path.join(f.workspace,'capacity.zip');await fs.writeFile(archive,zipFixture([{name:'file',data:Buffer.alloc(200,0x61)}])); const preview=await f.archives.extractPreview({path:archive});assert.equal(preview.canExtract,false);assert.equal(preview.uncompressedBytes,200);assert.deepEqual(preview.topLevelEntries,[{name:'file',kind:'file'}]);assert.equal(preview.freeBytes,available);assert.equal(preview.reserveBytes,1024**3);await assert.rejects(f.archives.extract({token:preview.token,name:'out'}),{code:'no-space'});assert.equal(await fs.stat(path.join(f.workspace,'out')).then(()=>true,()=>false),false); diff --git a/tests/harness-manager.test.mjs b/tests/harness-manager.test.mjs index a91cb075..4589da9d 100644 --- a/tests/harness-manager.test.mjs +++ b/tests/harness-manager.test.mjs @@ -1222,6 +1222,27 @@ test("a failed install leaves the harness not runnable, and a retry recovers", a assert.equal(retried.harness.runnable, true); }); +test("a state volume that cannot record the failure still answers with the operation's own error", async () => { + const fs = new MemoryFs(); + const world = createWorld(fs); + const manager = managerFor(world); + const stateFile = path.join(STATE_DIR, "harness-state.json"); + const rename = fs.rename.bind(fs); + let writes = 0; + // The in-progress record lands; the failure record does not, as with a full + // or read-only state volume at the worst moment. + fs.rename = async (from, to) => { + if (to === stateFile && ++writes === 2) throw Object.assign(new Error("EACCES: read-only"), { code: "EACCES" }); + return rename(from, to); + }; + + world.failUse = "registry unavailable"; + const failed = await manager.install("grok"); + assert.equal(failed.ok, false); + assert.equal(failed.code, "failed"); + assert.equal(failed.error, "registry unavailable"); +}); + test("status never reports runnable while a previous install is still in progress", async () => { const fs = new MemoryFs(); const world = createWorld(fs); diff --git a/tests/harness-retention.test.mjs b/tests/harness-retention.test.mjs index d1252a04..6519ecac 100644 --- a/tests/harness-retention.test.mjs +++ b/tests/harness-retention.test.mjs @@ -57,6 +57,19 @@ test("retention protects current, previous, project pins, provider binaries, and assert.equal((await readState(f.ctx)).toolchains.go.managedVersions.includes("1.23.0"), false); }); +test("a previousVersion naming an uninstalled version keeps the newest installed fallback", async (t) => { + const f = await fixture(t); + // An upgrade interrupted before its state write can leave the recorded + // previous version gone: the fallback must move to what is installed. + await writeState(f.ctx, { ...emptyState(), toolchains: { go: { version: "1.25.0", previousVersion: "1.26.0", managedVersions: ["1.24.0", "1.25.0"] } } }); + f.setPrunable(f.installs.filter((item) => ["1.24.0", "1.25.0"].includes(item.version))); + const plan = await planRetention(f.ctx); + const byVersion = Object.fromEntries(plan.items.map((item) => [item.version, item])); + assert.equal(byVersion["1.24.0"].reason, "Previous version"); + assert.equal(byVersion["1.25.0"].reason, "Current version"); + assert.equal(plan.removableCount, 0); +}); + test("provider PATH, process cwd, and flag paths veto removal", async (t) => { const f = await fixture(t); await writeStorageJson(f.settings, { providerInstances: { custom: { config: { env: { PATH: `${f.installs[0].install_path}/bin:/usr/bin` } } } } }); diff --git a/tests/t3-pair.test.sh b/tests/t3-pair.test.sh new file mode 100644 index 00000000..071324b9 --- /dev/null +++ b/tests/t3-pair.test.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +# t3-pair must explain a failed mint instead of dying silently: `set -e` +# would otherwise abort the script before its own error handling runs. +# +# bash tests/t3-pair.test.sh +set -euo pipefail + +ROOT="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" +T3_PAIR="$ROOT/docker/bin/t3-pair" +# The helper sources the shared step-down from the image path; point it at +# the checkout instead. +export T3_STEP_DOWN_LIB="$ROOT/docker/lib" + +pass=0 +fail=0 +ok() { printf ' PASS %s\n' "$1"; pass=$((pass + 1)); } +no() { printf ' FAIL %s\n' "$1"; fail=$((fail + 1)); } + +WORK="$(mktemp -d)" +trap 'rm -rf "$WORK"' EXIT + +# A launcher that always fails, with the migration chatter a real CLI adds. +printf '#!/usr/bin/env bash\necho "migration chatter" >&2\nexit 1\n' > "$WORK/failing-launcher" +chmod +x "$WORK/failing-launcher" + +output="$(set +e; T3_INFRA_LAUNCHER="$WORK/failing-launcher" \ + T3_INFRA_NODE="$(command -v node)" T3_PUBLIC_URL_RESOLVER=/nonexistent.mjs \ + bash "$T3_PAIR" --no-qr 2>&1; echo "exit=$?")" +status="${output##*exit=}" + +if [ "$status" = 1 ]; then ok "a failed mint exits 1"; else no "a failed mint exits 1 (got $status)"; fi +if printf '%s' "$output" | grep -q 'failed to mint a pairing token'; then + ok "the failure says so" +else + no "the failure says so" +fi + +if [ "$fail" -eq 0 ]; then printf 'all %s checks passed\n' "$pass"; exit 0; fi +printf '%s of %s checks failed\n' "$fail" "$((pass + fail))" +exit 1 From 8fa5a15ab1e0ebead6904d92111bb41a2863a56e Mon Sep 17 00:00:00 2001 From: Ziyang Zeng Date: Thu, 19 Nov 2026 16:06:39 -0800 Subject: [PATCH 2/2] fix: verify command success and preserve installed rollback versions --- docker/bin/t3-expose | 2 +- docker/bin/t3-pair | 6 +--- docker/harness/retention.mjs | 55 ++++++++++++++++------------- docker/harness/state.mjs | 9 +++-- docker/setup/connect.mjs | 2 +- docker/setup/server.mjs | 4 ++- tests/connect-api.test.mjs | 26 +++++++++++--- tests/connect.test.mjs | 1 + tests/harness-manager.test.mjs | 2 ++ tests/harness-retention.test.mjs | 26 +++++++++----- tests/setup-front-door.test.mjs | 10 +++--- tests/t3-expose.test.mjs | 59 +++++++++++++++++++++++++------- tests/t3-pair.test.sh | 32 ++++++++++------- 13 files changed, 156 insertions(+), 78 deletions(-) diff --git a/docker/bin/t3-expose b/docker/bin/t3-expose index 636a0211..c88538bf 100755 --- a/docker/bin/t3-expose +++ b/docker/bin/t3-expose @@ -140,7 +140,7 @@ for _ in $(seq 1 40); do # a blip, not a verdict, so keep polling instead of dying under `set -e`. line="$(api GET /ports 2>/dev/null | jq -r --argjson p "$port" ' (.tunnels // []) | map(select(.port == $p)) | first - | if . == null then "|" else "\(.state)|\(.url // "")|\(.error // "")" end' || true)" + | if . == null then "|" else "\(.state)|\(.url // "")|\(.error // "")" end' 2>/dev/null)" || continue case "$line" in open\|*) url="$(printf '%s' "$line" | cut -d'|' -f2)"; break ;; failed\|*) printf '\n'; echo "t3-expose: $(printf '%s' "$line" | cut -d'|' -f3)" >&2; exit 1 ;; diff --git a/docker/bin/t3-pair b/docker/bin/t3-pair index 672724fa..5d98219b 100644 --- a/docker/bin/t3-pair +++ b/docker/bin/t3-pair @@ -73,11 +73,7 @@ args=(auth pairing create --base-url "$base_url" --ttl "$ttl" --json) [ -n "$label" ] && args+=(--label "$label") # `t3 auth` logs migration chatter on stderr; keep stdout clean for the JSON. -# `|| true` lets the failed-mint handling below run: under `set -o pipefail` -# the substitution's exit status would otherwise abort the script first. -response="$("$T3_INFRA_LAUNCHER" "${args[@]}" 2>/dev/null | sed -n '/^{/,$p' || true)" - -if [ -z "$response" ]; then +if ! response="$("$T3_INFRA_LAUNCHER" "${args[@]}" 2>/dev/null | sed -n '/^{/,$p')" || [ -z "$response" ]; then echo "t3-pair: failed to mint a pairing token" >&2 echo "t3-pair: re-run with: t3 auth pairing create --base-url '$base_url' --json" >&2 exit 1 diff --git a/docker/harness/retention.mjs b/docker/harness/retention.mjs index 655b0a3a..f47b80af 100644 --- a/docker/harness/retention.mjs +++ b/docker/harness/retention.mjs @@ -10,19 +10,21 @@ import * as state from "./state.mjs"; import { policyPath, readPolicy } from "./storage/policy.mjs"; import { inside, processSnapshot } from "./storage/system.mjs"; import { serviceReferences, serviceUsesTool } from "./service-references.mjs"; +import { compareVersions } from "./version.mjs"; const exact = /^\d+\.[0-9A-Za-z._+-]{1,62}$/; const sections = ["harnesses", "toolchains", "packages"]; const recordsOf = (saved) => sections.flatMap((section) => Object.entries(saved[section] || {}).map(([id, record]) => ({ section, id, record }))); const versionsOf = (record) => [...new Set((record.managedVersions || []).filter((v) => typeof v === "string" && exact.test(v)))]; -/** The version automatic retention keeps as the rollback fallback: the recorded - * previous version while that one is actually installed, else the newest other - * version. A record interrupted mid-upgrade can name a version that is gone; - * trusting it would leave no installed version protected. */ -const previousVersionOf = (record, versions) => versions.includes(record.previousVersion) - ? record.previousVersion - : versions.filter((version) => version !== record.version).at(-1); +/** Prefer the previous selection while it is installed, then the newest other + * managed install. mise's prunable list omits installs pinned by projects. */ +function previousVersionOf(record, versions, installed) { + const available = versions.filter(({ version, tool }) => version !== record.version && + installed[tool]?.some((item) => item.installed === true && item.version === version)); + return available.find(({ version }) => version === record.previousVersion)?.version + ?? available.sort((a, b) => compareVersions(b.version, a.version))[0]?.version; +} function providerPaths(value, result = []) { if (!value || typeof value !== "object") return result; @@ -79,25 +81,32 @@ export async function planRetention(ctx, { saved, only, includePrevious = false if (typeof includePrevious !== "boolean") throw new TypeError("includePrevious must be a boolean"); saved ||= await state.readState(ctx); const preferences = await readPolicy(policyPath(ctx.env, ctx.home), ctx.fs); - const records = only ? [only] : recordsOf(saved); - const needsReferences = records.some(({ record }) => { - const versions = versionsOf(record); - const previous = previousVersionOf(record, versions); - return versions.some((version) => version !== record.version && (includePrevious || version !== previous)); + const records = (only ? [only] : recordsOf(saved)).flatMap(({ section, id, record }) => { + const entry = section === "packages" ? { name: id, miseTool: id } : section === "harnesses" ? getHarness(id) : getManagedTool(id); + if (!entry) return []; + const versions = versionsOf(record).map((version) => { + const installation = section === "harnesses" ? harnessInstallationForVersion(entry, version) : entry; + return { version, tool: installation.miseTool }; + }); + return [{ section, id, record, name: entry.name || id, versions }]; }); let refs = null, warning = preferences.error; - if (needsReferences && !warning) { - try { refs = await references(ctx); } catch (error) { warning = error.message; } + const hasOlderVersions = records.some(({ record, versions }) => versions.some(({ version }) => version !== record.version)); + if (hasOlderVersions && !warning) { + try { + if (!includePrevious) { + const { tools } = await mise.listTools(ctx); + if (!tools) throw new Error("Installed versions could not be checked. Older versions were kept."); + for (const row of records) row.previous = previousVersionOf(row.record, row.versions, tools); + } + const needsReferences = records.some(({ record, versions, previous }) => + versions.some(({ version }) => version !== record.version && version !== previous)); + if (needsReferences) refs = await references(ctx); + } catch (error) { warning = error.message; } } const items = []; - for (const { section, id, record } of records) { - const versions = versionsOf(record); - const previous = previousVersionOf(record, versions); - for (const version of versions) { - const entry = section === "packages" ? { name: id, miseTool: id } : section === "harnesses" ? getHarness(id) : getManagedTool(id); - if (!entry) continue; - const installation = section === "harnesses" ? harnessInstallationForVersion(entry, version) : entry; - const tool = installation.miseTool; + for (const { section, id, record, name, versions, previous } of records) { + for (const { version, tool } of versions) { const candidate = refs?.prunable?.[tool]?.find((item) => item.version === version && item.installed); const target = candidate?.install_path || null; let reason = version === record.version ? "Current version" : !includePrevious && version === previous ? "Previous version" : null; @@ -110,7 +119,7 @@ export async function planRetention(ctx, { saved, only, includePrevious = false return path.isAbsolute(value) && inside(target, path.resolve(value)); }))) reason = "Used by a running process"; if (!reason && !(await ordinaryInstall(ctx, target))) reason = "Install location could not be verified"; - items.push({ id, section, name: entry.name || id, tool, version, path: target, eligible: !reason, reason }); + items.push({ id, section, name, tool, version, path: target, eligible: !reason, reason }); } } // Bind consent to the cleanup mode as well as the exact removable set. diff --git a/docker/harness/state.mjs b/docker/harness/state.mjs index 92c64673..e0dd898f 100644 --- a/docker/harness/state.mjs +++ b/docker/harness/state.mjs @@ -40,8 +40,13 @@ export async function readState(ctx) { export async function writeState(ctx, state) { await ctx.fs.mkdir(path.dirname(ctx.statePath), { recursive: true }); const tmp = `${ctx.statePath}.tmp.${process.pid}.${Math.random().toString(36).slice(2, 8)}`; - await ctx.fs.writeFile(tmp, `${JSON.stringify(state, null, 2)}\n`, { encoding: "utf8", mode: 0o600 }); - await ctx.fs.rename(tmp, ctx.statePath); + try { + await ctx.fs.writeFile(tmp, `${JSON.stringify(state, null, 2)}\n`, { encoding: "utf8", mode: 0o600 }); + await ctx.fs.rename(tmp, ctx.statePath); + } catch (error) { + await ctx.fs.unlink(tmp).catch(() => {}); + throw error; + } } /** Merge one entry of a section ("harnesses", "toolchains", "packages") and persist it. */ diff --git a/docker/setup/connect.mjs b/docker/setup/connect.mjs index b25548e8..82920b01 100644 --- a/docker/setup/connect.mjs +++ b/docker/setup/connect.mjs @@ -70,7 +70,7 @@ export function connectState(json) { const authenticated = json.authenticated === true; const revoked = json.revoked === true; const linked = json.linked === true && !revoked; - const state = !desired ? "off" : revoked ? "signin" : linked ? "on" : authenticated ? "pending" : "signin"; + const state = linked ? "on" : !desired ? "off" : revoked || !authenticated ? "signin" : "pending"; const relayClient = json.relayClient && typeof json.relayClient === "object" ? json.relayClient : {}; return { state, diff --git a/docker/setup/server.mjs b/docker/setup/server.mjs index 075c4412..3ae9a922 100644 --- a/docker/setup/server.mjs +++ b/docker/setup/server.mjs @@ -1259,7 +1259,9 @@ const refreshConnect = () => { .then(async ({ stdout }) => { const value = connectState(jsonFrom(stdout)); if (value) value.preferences = value.linked ? await connectPreferences.read().catch(() => null) : null; - if (value?.preferences?.revoked) Object.assign(value, { linked: false, revoked: true, state: "signin" }); + if (value?.preferences?.revoked) { + Object.assign(value, { linked: false, revoked: true, state: value.desired ? "signin" : "off" }); + } if (revision !== connectRevision) return; connectCache = { at: Date.now(), value, refreshing: null }; }) diff --git a/tests/connect-api.test.mjs b/tests/connect-api.test.mjs index 254441a7..aa7c5b91 100644 --- a/tests/connect-api.test.mjs +++ b/tests/connect-api.test.mjs @@ -39,7 +39,8 @@ const root = ${JSON.stringify(root)}; const args = process.argv.slice(2); if (args[0] === 'connect' && args[1] === 'status') { try { await fs.access(root + '/fail-status'); process.exit(1); } catch {} - console.log(JSON.stringify({ desired: true, authenticated: true, linked: true })); + const desired = await fs.access(root + '/disabled-status').then(() => false, () => true); + console.log(JSON.stringify({ desired, authenticated: true, linked: true })); } else if (args[0] === 'connect' && args[1] === 'link') { // The parent waits for this file to appear. Publish only complete JSON. await fs.writeFile(root + '/link-args.tmp', JSON.stringify(args)); @@ -60,11 +61,13 @@ else process.exit(1); T3_STEP_DOWN_LIB: fileURLToPath(new URL('../docker/lib', import.meta.url)), MISE_DATA_DIR: path.join(root, 'mise-data'), MISE_STATE_DIR: path.join(root, 'mise-state'), MISE_CONFIG_DIR: path.join(root, 'mise-config') }; const child = spawn(process.execPath, [fileURLToPath(new URL('../docker/setup/server.mjs', import.meta.url))], { env, stdio: ['ignore', 'pipe', 'pipe'] }); + const closed = once(child, 'close'); let logs = ''; child.stdout.on('data', x => { logs += x; }); child.stderr.on('data', x => { logs += x; }); t.after(async () => { - if (child.exitCode === null) { const ended = once(child, 'exit'); child.kill(); await ended; } + if (child.exitCode === null && child.signalCode === null) child.kill(); + await closed; native.closeAllConnections(); await new Promise(resolve => native.close(resolve)); - await fs.rm(root, { recursive: true, force: true }); + await fs.rm(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 50 }); }); const request = (route, body, headers = { 'x-t3-setup-key': 'fixture' }) => fetch(`http://127.0.0.1:${port}${route}`, { method: body === undefined ? 'GET' : 'POST', headers: { ...headers, 'content-type': 'application/json' }, body: body === undefined ? undefined : JSON.stringify(body), @@ -102,10 +105,23 @@ else process.exit(1); nativeState.linkRevoked = true; assert.equal((await request('/connect/preferences', { publishAgentActivity: false })).status, 409); - const revoked = (await (await request('/status')).json()).connect; + // Status can serve the cache while a fresh CLI status read is in flight. + const readRevoked = async (desired) => { + for (let i = 0; i < 30; i++) { + const { connect } = await (await request('/status')).json(); + if (connect?.revoked && connect.desired === desired) return connect; + await pause(25); + } + assert.fail('Setup did not report the refreshed Connect state'); + }; + const revoked = await readRevoked(true); assert.equal(revoked.state, 'signin', 'live revocation wins over an older CLI link'); - assert.equal(revoked.revoked, true); assert.equal(revoked.linked, false); + await fs.writeFile(path.join(root, 'disabled-status'), 'yes'); + assert.equal((await request('/connect/preferences', { publishAgentActivity: false })).status, 409); + const disabled = await readRevoked(false); + assert.equal(disabled.state, 'off', 'a live revocation respects a disabled Connect setting'); + assert.equal(disabled.linked, false); for (const mode of ['publish_only', 'managed']) { const start = await request('/auth/signin', { agent: 'connect', mode }); diff --git a/tests/connect.test.mjs b/tests/connect.test.mjs index f8c42d89..fd2e5184 100644 --- a/tests/connect.test.mjs +++ b/tests/connect.test.mjs @@ -67,6 +67,7 @@ test("the saved state reads as off, signed out, waiting on a start, or on", () = assert.equal(on.relayHost, "relay.t3.codes"); assert.equal(on.publishAgentActivity, true); assert.equal(connectState({ ...base, authenticated: true }).state, "off", "signed in but not asked for is off"); + assert.equal(connectState({ ...base, linked: true }).state, "on", "an existing live link is shown until it is removed"); assert.equal(connectState({ ...base, revoked: true, linked: true }).state, "off", "revoked but not asked for stays off"); }); diff --git a/tests/harness-manager.test.mjs b/tests/harness-manager.test.mjs index 4589da9d..568a7895 100644 --- a/tests/harness-manager.test.mjs +++ b/tests/harness-manager.test.mjs @@ -1241,6 +1241,8 @@ test("a state volume that cannot record the failure still answers with the opera assert.equal(failed.ok, false); assert.equal(failed.code, "failed"); assert.equal(failed.error, "registry unavailable"); + await assert.rejects(fs.stat(manager.paths.lock), { code: "ENOENT" }); + assert.equal((await fs.readdir(STATE_DIR)).some(name => name.startsWith("harness-state.json.tmp.")), false); }); test("status never reports runnable while a previous install is still in progress", async () => { diff --git a/tests/harness-retention.test.mjs b/tests/harness-retention.test.mjs index 6519ecac..36907c23 100644 --- a/tests/harness-retention.test.mjs +++ b/tests/harness-retention.test.mjs @@ -15,18 +15,19 @@ async function fixture(t) { const versions = ["1.20.0", "1.21.0", "1.22.0", "1.23.0", "1.24.0", "1.25.0"]; const installs = versions.map((version) => ({ version, installed: true, install_path: path.join(dataDir, "installs/go", version) })); for (const item of installs) await fs.mkdir(path.join(item.install_path, "bin"), { recursive: true }); - let prunable = installs, processes = { complete: true, processes: [] }, fail = false; + let installed = installs, prunable = installs, processes = { complete: true, processes: [] }, fail = false; const removed = [], calls = []; const env = { HOME: home, T3CODE_HOME: path.join(home, ".t3"), MISE_DATA_DIR: dataDir, MISE_STATE_DIR: stateDir }; const ctx = { env, home, dataDir, stateDir, statePath: statePath(stateDir), fs: createFs(), now: Date.now, miseBin: "mise", timeouts: { mise: 120000, install: 120000 }, retentionProcesses: async () => processes, run: async (args) => { calls.push(args); - if (args.includes("ls")) return { code: fail ? 1 : 0, stdout: JSON.stringify({ go: prunable }) }; + if (args.includes("ls")) return { code: fail ? 1 : 0, stdout: JSON.stringify({ go: args.includes("--prunable") ? prunable : installed }) }; if (args.includes("uninstall")) { if (fail) return { code: 1, stderr: "uninstall failed" }; const version = args.at(-1).split("@").at(-1); removed.push(version); await fs.rm(path.join(dataDir, "installs/go", version), { recursive: true }); + installed = installed.filter((item) => item.version !== version); prunable = prunable.filter((item) => item.version !== version); return { code: 0 }; } @@ -36,12 +37,13 @@ async function fixture(t) { await writeState(ctx, saved); t.after(() => fs.rm(home, { recursive: true, force: true })); return { ctx, env, home, installs, removed, calls, settings: path.join(home, ".t3/userdata/settings.json"), - setProcesses: (value) => { processes = value; }, setPrunable: (value) => { prunable = value; }, setFail: (value) => { fail = value; } }; + setProcesses: (value) => { processes = value; }, setInstalled: (value) => { installed = value; }, + setPrunable: (value) => { prunable = value; }, setFail: (value) => { fail = value; } }; } test("retention protects current, previous, project pins, provider binaries, and running tools", async (t) => { const f = await fixture(t); - f.setPrunable(f.installs.filter((item) => item.version !== "1.20.0")); + f.setPrunable(f.installs.filter((item) => !["1.20.0", "1.24.0"].includes(item.version))); await writeStorageJson(f.settings, { providerInstances: { custom: { config: { binaryPath: path.join(f.installs[1].install_path, "bin/go") } } } }); f.setProcesses({ complete: true, processes: [{ args: ["node", path.join(f.installs[2].install_path, "bin/tool.js")], cwd: "/tmp" }] }); const plan = await planRetention(f.ctx); @@ -59,15 +61,21 @@ test("retention protects current, previous, project pins, provider binaries, and test("a previousVersion naming an uninstalled version keeps the newest installed fallback", async (t) => { const f = await fixture(t); - // An upgrade interrupted before its state write can leave the recorded - // previous version gone: the fallback must move to what is installed. - await writeState(f.ctx, { ...emptyState(), toolchains: { go: { version: "1.25.0", previousVersion: "1.26.0", managedVersions: ["1.24.0", "1.25.0"] } } }); - f.setPrunable(f.installs.filter((item) => ["1.24.0", "1.25.0"].includes(item.version))); + const saved = emptyState(); + saved.toolchains.go = { + version: "1.25.0", previousVersion: "1.22.0", managedVersions: ["1.24.0", "1.25.0", "1.23.0", "1.22.0"], + }; + await writeState(f.ctx, saved); + const missing = f.installs.find((item) => item.version === "1.22.0"); + await fs.rm(missing.install_path, { recursive: true }); + f.setInstalled(f.installs.map((item) => item === missing ? { ...item, installed: false } : item)); + f.setPrunable(f.installs.filter((item) => item !== missing)); const plan = await planRetention(f.ctx); const byVersion = Object.fromEntries(plan.items.map((item) => [item.version, item])); assert.equal(byVersion["1.24.0"].reason, "Previous version"); assert.equal(byVersion["1.25.0"].reason, "Current version"); - assert.equal(plan.removableCount, 0); + assert.equal(byVersion["1.22.0"].eligible, false); + assert.deepEqual((await applyRetention(f.ctx, plan)).removed.map((item) => item.version), ["1.23.0"]); }); test("provider PATH, process cwd, and flag paths veto removal", async (t) => { diff --git a/tests/setup-front-door.test.mjs b/tests/setup-front-door.test.mjs index 16373886..903961e8 100644 --- a/tests/setup-front-door.test.mjs +++ b/tests/setup-front-door.test.mjs @@ -170,15 +170,13 @@ const launch = async (t) => { stdio: ["ignore", "pipe", "pipe"], }); let logs = ""; + const closed = once(child, "close"); child.stdout.on("data", (x) => { logs += x; }); child.stderr.on("data", (x) => { logs += x; }); t.after(async () => { - if (child.exitCode === null) { - const exit = once(child, "exit"); - child.kill(); - await exit; - } - await fs.rm(root, { recursive: true, force: true }); + if (child.exitCode === null && child.signalCode === null) child.kill(); + await closed; + await fs.rm(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 50 }); }); const base = `http://127.0.0.1:${port}`; for (let i = 0; i < 200; i++) { diff --git a/tests/t3-expose.test.mjs b/tests/t3-expose.test.mjs index 39f54ed2..63ff6a93 100644 --- a/tests/t3-expose.test.mjs +++ b/tests/t3-expose.test.mjs @@ -1,4 +1,4 @@ -// Argument validation for t3-expose (docker/bin/t3-expose). +// Argument validation and polling for t3-expose (docker/bin/t3-expose). // // node --test tests/t3-expose.test.mjs // @@ -7,42 +7,75 @@ // dialling; these run the real script with a throwaway state dir and no // server, so reaching validation (exit 2) also proves nothing was dialled. import assert from "node:assert/strict"; -import { execFileSync } from "node:child_process"; +import { execFile } from "node:child_process"; +import { once } from "node:events"; import { mkdtempSync, rmSync } from "node:fs"; +import { createServer } from "node:http"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { fileURLToPath } from "node:url"; +import { promisify } from "node:util"; const EXPOSE = fileURLToPath(new URL("../docker/bin/t3-expose", import.meta.url)); +const exec = promisify(execFile); -const run = (t, ...args) => { +const run = async (t, args, env = {}) => { const home = mkdtempSync(path.join(tmpdir(), "t3-expose-")); t.after(() => rmSync(home, { recursive: true, force: true })); try { - execFileSync("bash", [EXPOSE, ...args], { - env: { PATH: "/usr/bin:/bin", T3_SETUP_KEY: "fixture-setup-key", T3CODE_HOME: home, T3_STEP_DOWN_LIB: fileURLToPath(new URL("../docker/lib", import.meta.url)) }, - encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], + const { stdout, stderr } = await exec("bash", [EXPOSE, ...args], { + env: { PATH: "/usr/bin:/bin", T3_SETUP_KEY: "fixture-setup-key", T3CODE_HOME: home, T3_STEP_DOWN_LIB: fileURLToPath(new URL("../docker/lib", import.meta.url)), ...env }, + encoding: "utf8", timeout: 15_000, }); - return { status: 0, stdout: "", stderr: "" }; + return { status: 0, stdout, stderr }; } catch (error) { - return { status: error.status, stdout: String(error.stdout ?? ""), stderr: String(error.stderr ?? "") }; + return { status: error.code, stdout: String(error.stdout ?? ""), stderr: String(error.stderr ?? "") }; } }; -test("stop rejects a port that would break out of its JSON body", (t) => { +test("stop rejects a port that would break out of its JSON body", async (t) => { for (const port of ['1,"x":1', '3000;id', '$(id)', '`id`', '3 0 0 0', ' 3000', '3000\n', '0x3000', '+3000']) { - const result = run(t, "stop", port); + const result = await run(t, ["stop", port]); assert.equal(result.status, 2, JSON.stringify(port)); assert.match(result.stderr, /not a port number/, JSON.stringify(port)); } }); -test("stop without a port explains itself, and expose validates the same way", (t) => { - const missing = run(t, "stop"); +test("stop without a port explains itself, and expose validates the same way", async (t) => { + const missing = await run(t, ["stop"]); assert.equal(missing.status, 2); assert.match(missing.stderr, /usage: t3-expose stop /); - const exposed = run(t, '1,"x":1'); + const exposed = await run(t, ['1,"x":1']); assert.equal(exposed.status, 2); assert.match(exposed.stderr, /not a port number/); }); + +test("polling retries a dropped connection and discards tunnel data from an HTTP error", async (t) => { + let polls = 0; + const server = createServer((req, res) => { + if (req.method === "POST" && req.url === "/ports/expose") { + res.end(JSON.stringify({ ok: true })); + } else if (req.method === "GET" && req.url === "/ports") { + polls += 1; + if (polls === 1) { + req.socket.destroy(); + return; + } + res.statusCode = polls === 2 ? 503 : 200; + const url = polls === 2 ? "https://stale.example.test" : "https://live.example.test"; + res.end(JSON.stringify({ tunnels: [{ port: 3000, state: "open", url }] })); + } else { + res.writeHead(404).end(); + } + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + t.after(() => new Promise((resolve) => server.close(resolve))); + + const result = await run(t, ["3000"], { T3_SETUP_PORT: String(server.address().port) }); + assert.equal(result.status, 0, result.stderr); + assert.equal(polls, 3); + assert.match(result.stdout, /Port 3000 is live at: https:\/\/live\.example\.test/); + assert.doesNotMatch(result.stdout, /stale\.example\.test/); +}); diff --git a/tests/t3-pair.test.sh b/tests/t3-pair.test.sh index 071324b9..800d6df9 100644 --- a/tests/t3-pair.test.sh +++ b/tests/t3-pair.test.sh @@ -19,21 +19,29 @@ no() { printf ' FAIL %s\n' "$1"; fail=$((fail + 1)); } WORK="$(mktemp -d)" trap 'rm -rf "$WORK"' EXIT -# A launcher that always fails, with the migration chatter a real CLI adds. -printf '#!/usr/bin/env bash\necho "migration chatter" >&2\nexit 1\n' > "$WORK/failing-launcher" +# A failed launcher can still print a complete JSON response before it exits. +cat > "$WORK/failing-launcher" <<'SH' +#!/usr/bin/env bash +echo "migration chatter" >&2 +if [ -n "$FIXTURE_PAIR_RESPONSE" ]; then printf '%s\n' "$FIXTURE_PAIR_RESPONSE"; fi +exit 1 +SH chmod +x "$WORK/failing-launcher" -output="$(set +e; T3_INFRA_LAUNCHER="$WORK/failing-launcher" \ - T3_INFRA_NODE="$(command -v node)" T3_PUBLIC_URL_RESOLVER=/nonexistent.mjs \ - bash "$T3_PAIR" --no-qr 2>&1; echo "exit=$?")" -status="${output##*exit=}" +for response in '' '{"pairUrl":"https://example.test/pair/fixture"}'; do + status=0 + # The caller supplies the URL, so the resolver is unnecessary here. + output="$(FIXTURE_PAIR_RESPONSE="$response" T3_INFRA_LAUNCHER="$WORK/failing-launcher" \ + T3_INFRA_NODE=/bin/false \ + bash "$T3_PAIR" --base-url https://example.test --no-qr 2>&1)" || status=$? -if [ "$status" = 1 ]; then ok "a failed mint exits 1"; else no "a failed mint exits 1 (got $status)"; fi -if printf '%s' "$output" | grep -q 'failed to mint a pairing token'; then - ok "the failure says so" -else - no "the failure says so" -fi + if [ "$status" = 1 ]; then ok "a failed mint exits 1"; else no "a failed mint exits 1 (got $status)"; fi + if [[ "$output" == *"failed to mint a pairing token"* && "$output" != *"Pairing URL:"* ]]; then + ok "a failed mint reports the error without printing a link" + else + no "a failed mint reports the error without printing a link" + fi +done if [ "$fail" -eq 0 ]; then printf 'all %s checks passed\n' "$pass"; exit 0; fi printf '%s of %s checks failed\n' "$fail" "$((pass + fail))"