Compare commits

...
@@ -1,3 +1,6 @@
import { channel } from "node:diagnostics_channel";
import type { ChildProcess } from "node:child_process";
import { startGitCommandMetrics, getGitCommandMetrics } from "../utils/run-git-command.js";
import { AgentDirectory } from "./agent/directory.js";
import { buildStoredAgentPayload } from "./agent/agent-projections.js";
import {
@@ -18,11 +21,12 @@ import {
rmSync,
statSync,
writeFileSync,
writeSync,
} from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { setImmediate as waitForImmediate } from "node:timers/promises";
import { afterEach, expect, test, vi } from "vitest";
import { afterEach, expect, onTestFinished, test, vi } from "vitest";
import { z } from "zod";
import { CLIENT_CAPS } from "@getpaseo/protocol/client-capabilities";
@@ -1101,21 +1105,98 @@ test("create_agent_request keeps requested child cwd when grouped under an exist
});
test("create_agent_request launches from an exact subdirectory in a created worktree", async () => {
// Temporary diagnostic: preserve the first invocation while separating process exit
// from stream closure. Metrics alone count exit, not the Git promise settling.
const started = performance.now();
const phases: Array<{ name: string; atMs: number }> = [];
const children: Array<{
id: number;
pid?: number;
operation?: string;
events: Array<{ name: string; atMs: number }>;
}> = [];
let observing = true;
const elapsed = () => Math.round(performance.now() - started);
const mark = (name: string) => {
if (observing) phases.push({ name, atMs: elapsed() });
};
const spawned = channel("child_process");
const observe = (message: unknown) => {
const child = (message as { process: ChildProcess }).process;
const entry: (typeof children)[number] = { id: children.length + 1, events: [] };
children.push(entry);
const record = (name: string) => {
if (observing) entry.events.push({ name, atMs: elapsed() });
};
record("created");
child.once("spawn", () => {
entry.pid = child.pid;
entry.operation =
child.spawnargs.find((arg) =>
["rev-parse", "check-ref-format", "show-ref", "for-each-ref", "worktree"].includes(arg),
) ?? "other";
record("spawn");
child.stdout?.once("end", () => record("stdout end"));
child.stdout?.once("close", () => record("stdout close"));
child.stderr?.once("end", () => record("stderr end"));
child.stderr?.once("close", () => record("stderr close"));
});
child.once("exit", () => record("exit"));
child.once("close", () => record("close"));
child.once("error", () => record("error"));
};
const report = (reason: string) => {
const metrics = getGitCommandMetrics();
writeSync(
2,
JSON.stringify({
test: "worktree subdirectory",
reason,
elapsedMs: elapsed(),
phases,
children,
submitted: metrics.submitted,
completed: metrics.completed,
active: metrics.active,
pending: metrics.pending,
operations: metrics.submissions.map(({ args }) => args.slice(0, 2).join(" ")),
}) + "\n",
);
};
spawned.subscribe(observe);
startGitCommandMetrics();
const watchdog = setTimeout(() => report("watchdog"), 25000);
watchdog.unref();
onTestFinished(() => {
clearTimeout(watchdog);
spawned.unsubscribe(observe);
mark("observer detached");
observing = false;
report("test finished");
});
const fixtureGit = (args: string[], options: { cwd: string; stdio: "pipe" }) => {
mark(`fixture git ${args[0]} start`);
const output = execFileSync("git", args, options);
mark(`fixture git ${args[0]} end`);
return output;
};
mark("repository setup");
const workdir = mkdtempSync(path.join(tmpdir(), "paseo-create-agent-worktree-cwd-"));
try {
const parent = path.join(workdir, "parent");
const child = path.join(parent, "packages", "app");
mkdirSync(child, { recursive: true });
execFileSync("git", ["init", "-b", "main"], { cwd: parent, stdio: "pipe" });
execFileSync("git", ["config", "user.email", "test@getpaseo.local"], {
fixtureGit(["init", "-b", "main"], { cwd: parent, stdio: "pipe" });
fixtureGit(["config", "user.email", "test@getpaseo.local"], {
cwd: parent,
stdio: "pipe",
});
execFileSync("git", ["config", "user.name", "Paseo Test"], { cwd: parent, stdio: "pipe" });
fixtureGit(["config", "user.name", "Paseo Test"], { cwd: parent, stdio: "pipe" });
writeFileSync(path.join(child, "README.md"), "app\n");
execFileSync("git", ["add", "."], { cwd: parent, stdio: "pipe" });
execFileSync("git", ["commit", "-m", "initial"], { cwd: parent, stdio: "pipe" });
fixtureGit(["add", "."], { cwd: parent, stdio: "pipe" });
fixtureGit(["commit", "-m", "initial"], { cwd: parent, stdio: "pipe" });
mark("session setup");
const logger = {
child: () => logger,
trace: vi.fn(),
@@ -1152,6 +1233,7 @@ test("create_agent_request launches from an exact subdirectory in a created work
resolveRepoRoot: async () => parent,
resolveDefaultBranch: async () => "main",
});
mark("project registry write");
await projectRegistry.upsert(
createPersistedProjectRecord({
projectId: "proj-parent",
@@ -1162,6 +1244,7 @@ test("create_agent_request launches from an exact subdirectory in a created work
updatedAt: "2026-05-07T00:00:00.000Z",
}),
);
mark("workspace registry write");
await workspaceRegistry.upsert(
createPersistedWorkspaceRecord({
workspaceId: "ws-parent",
@@ -1174,6 +1257,7 @@ test("create_agent_request launches from an exact subdirectory in a created work
}),
);
mark("session construction");
const emitted: SessionOutboundMessage[] = [];
const session = new Session({
workspaceRuntimeStore: createWorkspaceRuntimeStore(),
@@ -1231,6 +1315,7 @@ test("create_agent_request launches from an exact subdirectory in a created work
terminalManager: null,
});
mark("create agent request");
await session.handleMessage({
type: "create_agent_request",
requestId: "req-create-worktree-child",
@@ -1239,8 +1324,9 @@ test("create_agent_request launches from an exact subdirectory in a created work
worktree: { mode: "branch-off", newBranch: "feature/created-worktree" },
});
mark("create agent returned");
const [createdAgent] = agentManager.listAgents();
const createdWorktreeRoot = execFileSync("git", ["rev-parse", "--show-toplevel"], {
const createdWorktreeRoot = fixtureGit(["rev-parse", "--show-toplevel"], {
cwd: createdAgent!.cwd,
stdio: "pipe",
})
@@ -1256,7 +1342,9 @@ test("create_agent_request launches from an exact subdirectory in a created work
agent: { cwd: createdAgent?.cwd },
});
} finally {
mark("cleanup start");
rmSync(workdir, { recursive: true, force: true });
mark("cleanup end");
}
});