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
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions docker/bin/t3-expose
Original file line number Diff line number Diff line change
Expand Up @@ -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' 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 ;;
Expand Down
4 changes: 1 addition & 3 deletions docker/bin/t3-pair
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +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.
response="$("$T3_INFRA_LAUNCHER" "${args[@]}" 2>/dev/null | sed -n '/^{/,$p')"

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
Expand Down
10 changes: 7 additions & 3 deletions docker/harness/manager.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down
49 changes: 33 additions & 16 deletions docker/harness/retention.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,22 @@ 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)))];

/** 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;
for (const [key, child] of Object.entries(value)) {
Expand Down Expand Up @@ -71,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 = record.previousVersion || versions.filter((version) => version !== record.version).at(-1);
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 = record.previousVersion || versions.filter((v) => v !== record.version).at(-1);
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;
Expand All @@ -102,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.
Expand Down
9 changes: 7 additions & 2 deletions docker/harness/state.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
2 changes: 1 addition & 1 deletion docker/setup/connect.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = linked ? "on" : !desired ? "off" : revoked || !authenticated ? "signin" : "pending";
const relayClient = json.relayClient && typeof json.relayClient === "object" ? json.relayClient : {};
return {
state,
Expand Down
1 change: 1 addition & 0 deletions docker/setup/files/zip-format.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions docker/setup/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
})
Expand Down Expand Up @@ -1635,7 +1637,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.");
}
Expand Down
26 changes: 21 additions & 5 deletions tests/connect-api.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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),
Expand Down Expand Up @@ -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 });
Expand Down
2 changes: 2 additions & 0 deletions tests/connect.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ 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");
});

test("anything but that JSON is no state, and a malformed relay URL is no host", () => {
Expand Down
6 changes: 6 additions & 0 deletions tests/files-archives.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 23 additions & 0 deletions tests/harness-manager.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,29 @@ 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");
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 () => {
const fs = new MemoryFs();
const world = createWorld(fs);
Expand Down
Loading
Loading