Skip to content
Open
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
228 changes: 228 additions & 0 deletions docs/state-machines/p5-p6-tier-a-reconciliation.md

Large diffs are not rendered by default.

131 changes: 122 additions & 9 deletions docs/state-machines/physical-teardown.md

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions main/services/bot-runtime/bot-session-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1667,3 +1667,27 @@ test("metadata commits dedupe concurrent issue effects and cannot append after d
await assert.rejects(service.appendEntry("bot:a", kind, data), /no longer exists/u);
} finally { await service.shutdown(); }
});

test("shutdown interrupts in-flight awaitReply for Resume and rejects late appendEntry commits", async () => {
const fauxModels = createFauxModels([slowAnswer()], { tokensPerSecond: 40 });
const service = await serviceFor(tempProfile(), fauxModels);
try {
const sent = await service.send("bot:a", { text: "Long work", requestId: "req-shutdown-race" });
await waitFor(
async () => (await service.state("bot:a")).kind === "running" && fauxModels.calls() === 1,
{ what: "turn start" },
);
const replyPromise = service.awaitReply("bot:a", sent.submissionId, new AbortController().signal);
// Let awaitReply attach to submission.wait before host shutdown begins.
await new Promise((resolve) => setTimeout(resolve, 40));
await service.shutdown();
const outcome = await replyPromise;
assert.deepEqual(outcome, { kind: "interrupted" });
await assert.rejects(
service.appendEntry("bot:a", BOT_ISSUE_STATUS_ENTRY_KIND, { effectId: "after-shutdown" }),
/shut down|shutting down|closed/u,
);
} finally {
await service.shutdown();
}
});
32 changes: 32 additions & 0 deletions main/services/browser/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,35 @@ test("same browser page compares normalized URLs", () => {
assert.equal(sameBrowserPage("https://example.com/#x", "https://example.com/#y"), false);
assert.equal(sameBrowserPage("about:blank", "nope"), false);
});
test("human input while a user action runs keeps epoch stable and cancels only queued agent work", async () => {
const queue = new BrowserActionQueue();
const initialEpoch = queue.epoch;
let userStarted!: () => void;
let releaseUser!: () => void;
const started = new Promise<void>((resolve) => { userStarted = resolve; });
const userRun = queue.run(async (check) => {
userStarted();
await new Promise<void>((resolve) => { releaseUser = resolve; });
check();
return "user-ok";
}, undefined, "user");
let queuedAgentRan = false;
const queuedAgent = queue.run(async () => {
queuedAgentRan = true;
return "agent-ran";
}, undefined, "agent");
await started;

queue.interruptForHumanInput();
assert.equal(queue.epoch, initialEpoch);
assert.equal(queue.signal?.aborted, false);
releaseUser();

assert.equal(await userRun, "user-ok");
await assert.rejects(queuedAgent, /interrupted/);
assert.equal(queuedAgentRan, false);

queue.interrupt();
assert.equal(queue.epoch, initialEpoch + 1);
assert.equal(await queue.run(async () => "post-interrupt"), "post-interrupt");
});
93 changes: 93 additions & 0 deletions main/services/follow-through/background-jobs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -882,3 +882,96 @@ test("a shell's background job that cannot be confirmed gone is not reported sto
assert.equal(result.timedOut, false);
assert.equal(result.backgroundStopped, false);
});

test("an adoption racing with shutdown never registers a job or writes it after close", { skip }, async (t) => {
const dir = await tempDir();
const id = newBackgroundJobId();
let enteredLookup!: () => void;
let releaseLookup!: () => void;
const lookupEntered = new Promise<void>((resolve) => { enteredLookup = resolve; });
const lookupHeld = new Promise<void>((resolve) => { releaseLookup = resolve; });
const { jobs } = harness(dir, {
adoptSettleMs: 0,
processGroupMembers: async (pgid) => {
enteredLookup();
await lookupHeld;
return defaultProcessGroupMembers(pgid);
},
});
const child = spawn("sleep 30", {
cwd: dir,
detached: true,
shell: true,
stdio: "ignore",
env: { ...process.env, AIDEN_BACKGROUND_JOB: id },
});
t.after(async () => {
try { if (child.pid) process.kill(-child.pid, "SIGKILL"); } catch { /* Already gone. */ }
await cleanUp(jobs, dir);
});
assert.ok(child.pid);
const adopting = jobs.adopt({
id,
chatId: "chat-1",
workspaceId: "ws-1",
cwd: dir,
command: "sleep 30 &",
processGroup: child.pid,
});
await lookupEntered;
const shuttingDown = jobs.shutdown();
releaseLookup();
assert.equal(await adopting, undefined);
await shuttingDown;
assert.deepEqual(jobs.listAll(), []);
});

test("an unconfirmed adopted stop blocks workspace teardown and stays in running.json across shutdown", { skip }, async (t) => {
const dir = await tempDir();
const id = newBackgroundJobId();
let failLookup = false;
const { jobs } = harness(dir, {
adoptSettleMs: 0,
processHasEnvironment: async (pid, entry) => {
if (failLookup) throw new Error("proc unreadable");
return pid === child.pid && entry === `AIDEN_BACKGROUND_JOB=${id}`;
},
processGroupMembers: async (pgid) => {
if (failLookup) throw new Error("ps unavailable");
return pgid === child.pid ? [child.pid] : [];
},
});
const child = spawn("sleep 30", {
cwd: dir,
detached: true,
shell: true,
stdio: "ignore",
env: { ...process.env, AIDEN_BACKGROUND_JOB: id },
});
t.after(async () => {
failLookup = false;
try { if (child.pid) process.kill(-child.pid, "SIGKILL"); } catch { /* Already gone. */ }
await fs.rm(dir, { recursive: true, force: true });
});
assert.ok(child.pid);
const adopted = await jobs.adopt({
id,
chatId: "chat-1",
workspaceId: "ws-1",
cwd: dir,
command: "sleep 30 &",
processGroup: child.pid,
});
assert.equal(adopted?.state, "running");

failLookup = true;
await assert.rejects(jobs.stopWorkspace("ws-1", "access-changed"), /confirm.*background jobs stopped/u);
assert.equal(jobs.get("chat-1", id)?.state, "running");

await jobs.shutdown();
assert.equal(jobs.get("chat-1", id)?.state, "running");
const ledger = JSON.parse(await fs.readFile(path.join(dir, "logs", "running.json"), "utf8")) as {
jobs: Array<{ id: string; pid: number }>;
};
assert.ok(ledger.jobs.some((entry) => entry.id === id && entry.pid === child.pid));
});
191 changes: 191 additions & 0 deletions main/services/git-executable.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
import test from "node:test";
import { promisify } from "node:util";
import { GitService } from "./git.js";
import { createGitExecutableResolver, resolveGitExecutable } from "./git-executable.js";

const execFileAsync = promisify(execFile);

function fixture(paths: Record<string, string>, developerDir?: string) {
let probes = 0;
return {
Expand Down Expand Up @@ -233,3 +241,186 @@ test("memoized resolver on Linux skips a workspace-relative PATH entry", async (
assert.equal(await resolve("git", "/w", { PATH: ".:/usr/bin" }), "/usr/bin/git");
assert.ok(!f.inspected.includes("/w/git"));
});

async function createTempRepoWithWorktree(t: test.TestContext, gitBinary?: string) {
const root = await fs.mkdtemp(path.join(os.tmpdir(), "aiden-git-proof-"));
t.after(() => fs.rm(root, { recursive: true, force: true }));
const repo = path.join(root, "repo");
const worktreesRoot = path.join(root, "worktrees");
await fs.mkdir(repo, { recursive: true });
await fs.mkdir(worktreesRoot, { recursive: true });
const runGit = (args: string[]) =>
execFileAsync("git", args, {
cwd: repo,
env: {
...process.env,
GIT_AUTHOR_NAME: "Test",
GIT_AUTHOR_EMAIL: "test@example.com",
GIT_COMMITTER_NAME: "Test",
GIT_COMMITTER_EMAIL: "test@example.com",
},
});
await runGit(["init", "-b", "main"]);
await fs.writeFile(path.join(repo, "README.md"), "hello\n", "utf8");
await runGit(["add", "README.md"]);
await runGit(["commit", "-m", "init"]);
const service = new GitService(gitBinary ? { gitBinary } : {});
const created = await service.createWorktree(repo, worktreesRoot, "proof/test-branch");
return { root, repo, service, created };
}

test(
"VerifiedRepoIdentityProof refuses to cache pure-FS admission when repo config uses [include]",
{ skip: process.platform === "win32" },
async (t) => {
const { root, repo, service, created } = await createTempRepoWithWorktree(t);
const check = () =>
service.managedWorktreeUsable(
repo,
created.path,
created.branch,
created.worktreeGitDir,
created.ownershipToken,
created.worktreeDevice,
created.worktreeInode,
);
// First call verifies through Git and caches the VerifiedRepoIdentityProof;
// second call hits the pure-FS fast path while signatures match.
assert.equal(await check(), true);
assert.equal(await check(), true);

// Add a valid [include] directive to repo/.git/config and re-check.
// Because repo/.git/config now has an [include], pureFsAdmissionProof must
// refuse to cache a VerifiedRepoIdentityProof for subsequent calls.
const extraConfig = path.join(root, "extra.gitconfig");
await fs.writeFile(extraConfig, "# included\n", "utf8");
const repoConfig = path.join(repo, ".git", "config");
const before = await fs.readFile(repoConfig, "utf8");
await fs.writeFile(repoConfig, `${before}\n[include]\n\tpath = ${extraConfig}\n`, "utf8");
assert.equal(await check(), true);

// Now break ONLY the included file without modifying repo/.git/config.
// A stale pure-FS cache would have returned true; failing closed forces
// Git discovery, which rejects the broken included config.
await fs.writeFile(extraConfig, "[core\n", "utf8");
await assert.rejects(check(), /Git repository|bad config|not_a_repo/u);
},
);

test(
"VerifiedRepoIdentityProof refuses to cache pure-FS admission for a shebang wrapper git binary",
{ skip: process.platform === "win32" },
async (t) => {
const wrapperDir = await fs.mkdtemp(path.join(os.tmpdir(), "aiden-git-wrap-"));
t.after(() => fs.rm(wrapperDir, { recursive: true, force: true }));
const wrapperPath = path.join(wrapperDir, "git-wrapper");
const countFile = path.join(wrapperDir, "calls.log");
const realGit = await resolveGitExecutable("git", wrapperDir, process.env);
await fs.writeFile(
wrapperPath,
`#!/bin/sh\necho call >> "${countFile}"\nexec "${realGit}" "$@"\n`,
{ mode: 0o755 },
);

const { repo, service, created } = await createTempRepoWithWorktree(t, wrapperPath);
const check = () =>
service.managedWorktreeUsable(
repo,
created.path,
created.branch,
created.worktreeGitDir,
created.ownershipToken,
created.worktreeDevice,
created.worktreeInode,
);
assert.equal(await check(), true);
const callsAfterFirst = (await fs.readFile(countFile, "utf8")).trim().split("\n").length;
assert.equal(await check(), true);
const callsAfterSecond = (await fs.readFile(countFile, "utf8")).trim().split("\n").length;
assert.ok(
callsAfterSecond > callsAfterFirst,
"wrapper binary must never satisfy the 0-spawn pure-FS admission proof",
);
},
);

test(
"VerifiedRepoIdentityProof fails closed out of pure-FS fast path when GIT_EXEC_PATH is set",
{ skip: process.platform === "win32" },
async (t) => {
const previousExecPath = process.env.GIT_EXEC_PATH;
const previousTrace = process.env.GIT_TRACE;
const previousGlobalConfig = process.env.GIT_CONFIG_GLOBAL;
const previousNoSystem = process.env.GIT_CONFIG_NOSYSTEM;
const { root, repo, service, created } = await createTempRepoWithWorktree(t, "/usr/bin/git");
const fakeExecDir = path.join(root, "fake-exec");
const traceFile = path.join(root, "git-trace.log");
await fs.mkdir(fakeExecDir, { recursive: true });
t.after(() => {
if (previousExecPath === undefined) delete process.env.GIT_EXEC_PATH;
else process.env.GIT_EXEC_PATH = previousExecPath;
if (previousTrace === undefined) delete process.env.GIT_TRACE;
else process.env.GIT_TRACE = previousTrace;
if (previousGlobalConfig === undefined) delete process.env.GIT_CONFIG_GLOBAL;
else process.env.GIT_CONFIG_GLOBAL = previousGlobalConfig;
if (previousNoSystem === undefined) delete process.env.GIT_CONFIG_NOSYSTEM;
else process.env.GIT_CONFIG_NOSYSTEM = previousNoSystem;
});

delete process.env.GIT_EXEC_PATH;
process.env.GIT_CONFIG_GLOBAL = "/dev/null";
process.env.GIT_CONFIG_NOSYSTEM = "1";
process.env.GIT_TRACE = traceFile;

const readTrace = () => fs.readFile(traceFile, "utf8").catch(() => "");
const check = () =>
service.managedWorktreeUsable(
repo,
created.path,
created.branch,
created.worktreeGitDir,
created.ownershipToken,
created.worktreeDevice,
created.worktreeInode,
);

// First call runs Git and caches the VerifiedRepoIdentityProof.
assert.equal(await check(), true);
const traceAfterFirst = await readTrace();
assert.ok(traceAfterFirst.length > 0, "initial check must invoke Git to mint the proof");

// Second call with identical environment and untouched signed paths must
// hit the 0-spawn pure-FS fast path (no new Git trace entries).
assert.equal(await check(), true);
const traceAfterFastPath = await readTrace();
assert.equal(
traceAfterFastPath,
traceAfterFirst,
"unchanged check without GIT_EXEC_PATH must use the 0-spawn pure-FS fast path",
);

// Set GIT_EXEC_PATH while leaving all signed repo and config paths intact.
// The environment fence must invalidate the cached proof and invoke Git.
process.env.GIT_EXEC_PATH = fakeExecDir;
assert.equal(await check(), true);
const traceAfterExecPath = await readTrace();
assert.ok(
traceAfterExecPath.length > traceAfterFastPath.length,
"setting GIT_EXEC_PATH must bypass the cached proof and invoke Git",
);
assert.match(
traceAfterExecPath.slice(traceAfterFastPath.length),
/rev-parse/u,
"Git discovery must run when GIT_EXEC_PATH invalidates the cached proof",
);

// A subsequent call while GIT_EXEC_PATH remains set must still invoke Git
// (no new proof may be cached under GIT_EXEC_PATH).
assert.equal(await check(), true);
const traceAfterRepeatExecPath = await readTrace();
assert.ok(
traceAfterRepeatExecPath.length > traceAfterExecPath.length,
"pure-FS proof must not be re-cached while GIT_EXEC_PATH is set",
);
},
);
Loading
Loading