fcc7ceb8b6
* fix(web): exclude permission_request from turn-complete safety net The session.turn.completed handler iterated over messages and forced metadata.status="complete" on anything carrying tool_call_id. Permission prompts also carry tool_call_id (for FE pairing with their tool_call), so this loop overwrote the user's "approved" decision on every prompt in the turn — and the FE component re-rendered the Approve/Deny buttons. * fix(backend): protect permission_request rows from tool_call sweeps Three permission_request invariants the prior code violated, mirroring the FE bug fix: - CompletePendingToolCallsForTurn: SQL UPDATE filtered by tool_call_id presence, also catching permission_request rows. Add type filter so the safety net only touches actual tool messages. - GetMessageByToolCallID: returned the oldest row carrying that tool_call_id, which races to the permission_request when timing shifts. Exclude permission_request type so a tool_update can never land on the user's approve/reject row. - applyToolCallMessageUpdate: defensive guard that refuses to overwrite a permission_request, making the invariant explicit at the layer that does the writing. Adds a regression test for CompletePendingToolCallsForTurn covering approved/rejected/pending permission rows. * test(e2e): cover approval-reappear regression with mock-agent scenario Adds a multi-permission mock-agent scenario (three sequential bash tools, each requiring approval) and an E2E test that approves all three and asserts no permission action row remains visible after the agent's turn ends, and that the resolved state survives a reload. Supporting test infrastructure: - data-testid hooks on PermissionActionRow. - permissionActionRows() / permissionApproveButtons() on SessionPage. - backend fixture force-disables AGENTCTL_AUTO_APPROVE_PERMISSIONS=false so a developer's local AUTO_APPROVE=true env doesn't silently bypass the permission UI in tests that exercise the approve/reject flow. * test(backend): cover GetMessageByToolCallID permission_request exclusion Addresses claude-review feedback on the approvals fix: - Adds a regression test that seeds both a permission_request and a tool_call with the same tool_call_id (permission_request created first, so an ORDER BY created_at ASC sweep without the type filter would race to it) and asserts the lookup returns the tool_call row. - Bumps the applyToolCallMessageUpdate invariant guard from Warn to Error: reaching that branch means a caller bug, not a transient condition, and Warn would silently swallow the violation. * fix(e2e): scope auto-approve override to permission-approval test only The previous fixture force-disabled AGENTCTL_AUTO_APPROVE_PERMISSIONS globally, which broke session-isolation.spec.ts and other tests that use scenarios with permission-required tools (e.g. /e2e:read-and-edit) and rely on auto-approve to let the agent finish without UI clicks. Default the env to "true" in the fixture; let permission-approval.spec flip it to "false" via a backend.restart() override in beforeAll, then restore in afterAll. Adds an envOverrides parameter to backend.restart.
350 lines
13 KiB
TypeScript
350 lines
13 KiB
TypeScript
import { test as base } from "@playwright/test";
|
|
import { type ChildProcess, spawn } from "node:child_process";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
|
|
const BACKEND_DIR = path.resolve(__dirname, "../../../../apps/backend");
|
|
const WEB_DIR = path.resolve(__dirname, "../..");
|
|
const KANDEV_BIN = path.join(BACKEND_DIR, "bin", "kandev");
|
|
const STANDALONE_SERVER = path.join(WEB_DIR, ".next/standalone/web/server.js");
|
|
const STANDALONE_STATIC_DIR = path.join(WEB_DIR, ".next/standalone/web/.next/static");
|
|
const SOURCE_STATIC_DIR = path.join(WEB_DIR, ".next/static");
|
|
// Auto-derive from PID if not explicitly set — prevents port clashes between concurrent test runs
|
|
// Modulo 30 keeps agentctl ports under 65535 (30001 + 30*1000 = 60001 max)
|
|
const rawPortOffset = process.env.E2E_PORT_OFFSET;
|
|
const E2E_PORT_OFFSET = rawPortOffset === undefined ? process.pid % 30 : Number(rawPortOffset);
|
|
if (!Number.isInteger(E2E_PORT_OFFSET) || E2E_PORT_OFFSET < 0 || E2E_PORT_OFFSET > 29) {
|
|
throw new Error(`E2E_PORT_OFFSET must be an integer 0-29, got: ${rawPortOffset}`);
|
|
}
|
|
const BACKEND_BASE_PORT = 18080 + E2E_PORT_OFFSET;
|
|
const FRONTEND_BASE_PORT = 13000 + E2E_PORT_OFFSET;
|
|
const HEALTH_TIMEOUT_MS = 30_000;
|
|
const HEALTH_POLL_MS = 250;
|
|
|
|
export type BackendContext = {
|
|
port: number;
|
|
baseUrl: string;
|
|
frontendPort: number;
|
|
frontendUrl: string;
|
|
tmpDir: string;
|
|
/**
|
|
* Kill the backend process and respawn with the same config (DB, ports,
|
|
* tmpDir persist). Optional `envOverrides` mutates the captured env *for the
|
|
* lifetime of the worker*; pass them again on a subsequent restart to
|
|
* revert. Only in-memory execution state (running agents, WS connections)
|
|
* is lost.
|
|
*/
|
|
restart: (envOverrides?: Record<string, string>) => Promise<void>;
|
|
};
|
|
|
|
async function waitForHealth(url: string, timeoutMs: number, proc?: ChildProcess): Promise<void> {
|
|
const deadline = Date.now() + timeoutMs;
|
|
|
|
// Track process exit so we can fail fast instead of polling for the full timeout.
|
|
let processExited = false;
|
|
let processExitCode: number | null = null;
|
|
const onExit = (code: number | null) => {
|
|
processExited = true;
|
|
processExitCode = code;
|
|
};
|
|
proc?.once("exit", onExit);
|
|
|
|
try {
|
|
while (Date.now() < deadline) {
|
|
if (processExited) {
|
|
throw new Error(
|
|
`Backend process exited with code ${processExitCode} while waiting for health at ${url}`,
|
|
);
|
|
}
|
|
try {
|
|
const res = await fetch(url);
|
|
if (res.ok) return;
|
|
} catch {
|
|
// not ready yet
|
|
}
|
|
await new Promise((r) => setTimeout(r, HEALTH_POLL_MS));
|
|
}
|
|
throw new Error(`Service did not become healthy at ${url} within ${timeoutMs}ms`);
|
|
} finally {
|
|
proc?.off("exit", onExit);
|
|
}
|
|
}
|
|
|
|
function killProcess(proc: ChildProcess): Promise<void> {
|
|
return new Promise<void>((resolve) => {
|
|
proc.kill("SIGTERM");
|
|
const timeout = setTimeout(() => {
|
|
proc.kill("SIGKILL");
|
|
resolve();
|
|
}, 5_000);
|
|
proc.on("exit", () => {
|
|
clearTimeout(timeout);
|
|
resolve();
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Kills an entire process group. Used for the backend process which is spawned
|
|
* with `detached: true` so it becomes a process group leader. Sending signals
|
|
* to the negative PID targets all processes in that group (backend + agentctl).
|
|
* The 7s grace period gives agentctl time to cascade cleanup to agent process groups.
|
|
*/
|
|
function killProcessGroup(proc: ChildProcess): Promise<void> {
|
|
return new Promise<void>((resolve) => {
|
|
if (!proc.pid) {
|
|
resolve();
|
|
return;
|
|
}
|
|
|
|
const pid = proc.pid;
|
|
|
|
try {
|
|
process.kill(-pid, "SIGTERM");
|
|
} catch {
|
|
// Process group may already be gone
|
|
resolve();
|
|
return;
|
|
}
|
|
|
|
const timeout = setTimeout(() => {
|
|
try {
|
|
process.kill(-pid, "SIGKILL");
|
|
} catch {
|
|
// Already dead
|
|
}
|
|
resolve();
|
|
}, 7_000);
|
|
|
|
proc.on("exit", () => {
|
|
clearTimeout(timeout);
|
|
resolve();
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Spawn a backend process with the given environment. Returns the child process.
|
|
* The process is spawned with `detached: true` so it becomes a process group leader.
|
|
*/
|
|
function spawnBackendProcess(
|
|
env: Record<string, string>,
|
|
debug: boolean,
|
|
port: number,
|
|
): ChildProcess {
|
|
const proc = spawn(KANDEV_BIN, [], {
|
|
env: env as unknown as NodeJS.ProcessEnv,
|
|
stdio: ["ignore", "pipe", "pipe"],
|
|
detached: true,
|
|
});
|
|
|
|
const logFile = debug ? fs.createWriteStream(`/tmp/e2e-backend-${port}.log`) : null;
|
|
proc.once("exit", () => {
|
|
logFile?.end();
|
|
});
|
|
proc.stderr?.on("data", (chunk: Buffer) => {
|
|
if (debug) {
|
|
process.stderr.write(`[backend:${port}] ${chunk.toString()}`);
|
|
logFile?.write(chunk);
|
|
}
|
|
});
|
|
proc.stdout?.on("data", (chunk: Buffer) => {
|
|
if (debug) {
|
|
process.stderr.write(`[backend-log:${port}] ${chunk.toString()}`);
|
|
logFile?.write(chunk);
|
|
}
|
|
});
|
|
|
|
return proc;
|
|
}
|
|
|
|
/**
|
|
* Worker-scoped fixture that spawns an isolated backend process and
|
|
* a dedicated Next.js frontend. Each Playwright worker gets its own
|
|
* backend on a unique port with an isolated HOME, database, and data
|
|
* directory, plus its own frontend with SSR routed to that backend.
|
|
*/
|
|
export const backendFixture = base.extend<object, { backend: BackendContext }>({
|
|
backend: [
|
|
async ({}, use, workerInfo) => {
|
|
const backendPort = BACKEND_BASE_PORT + workerInfo.workerIndex;
|
|
const frontendPort = FRONTEND_BASE_PORT + workerInfo.workerIndex;
|
|
const tmpDir = fs.mkdtempSync(
|
|
path.join(os.tmpdir(), `kandev-e2e-${workerInfo.workerIndex}-`),
|
|
);
|
|
const homeDir = path.join(tmpDir, ".kandev");
|
|
const dbPath = path.join(tmpDir, "kandev.db");
|
|
const worktreeBase = path.join(tmpDir, "worktrees");
|
|
const repoCloneBase = path.join(tmpDir, "repos");
|
|
|
|
fs.mkdirSync(homeDir, { recursive: true });
|
|
fs.mkdirSync(worktreeBase, { recursive: true });
|
|
fs.mkdirSync(repoCloneBase, { recursive: true });
|
|
|
|
// Write a minimal .gitconfig so git doesn't prompt for identity
|
|
// and disable signing to avoid SSH/GPG key lookups in the isolated HOME.
|
|
fs.writeFileSync(
|
|
path.join(tmpDir, ".gitconfig"),
|
|
"[user]\n name = E2E Test\n email = e2e@test.local\n[commit]\n gpgsign = false\n[tag]\n gpgsign = false\n",
|
|
);
|
|
|
|
// Give each worker its own agentctl port range, offset from the default
|
|
// range (41001-41100) to avoid conflicts with a running dev instance.
|
|
const agentctlPortBase = 30001 + E2E_PORT_OFFSET * 1000 + workerInfo.workerIndex * 50;
|
|
const agentctlPortMax = agentctlPortBase + 49;
|
|
|
|
// Install a `git` shim that can sleep on `fetch`/`pull` before execing
|
|
// the real git binary. Tests that need to simulate slow network git
|
|
// operations write a millisecond value to `${tmpDir}/git-delay-ms`; the
|
|
// shim reads it on every invocation and sleeps the matching duration.
|
|
// When the file is absent the shim is a transparent passthrough, so
|
|
// other tests in the same worker are unaffected.
|
|
const shimDir = path.join(tmpDir, "bin");
|
|
const shimPath = path.join(shimDir, "git");
|
|
const shimDelayFile = path.join(tmpDir, "git-delay-ms");
|
|
const originalPath = process.env.PATH ?? "";
|
|
fs.mkdirSync(shimDir, { recursive: true });
|
|
fs.writeFileSync(
|
|
shimPath,
|
|
`#!/bin/sh
|
|
if [ -f "$KANDEV_E2E_GIT_DELAY_FILE" ] && { [ "$1" = "fetch" ] || [ "$1" = "pull" ]; }; then
|
|
delay_ms=$(cat "$KANDEV_E2E_GIT_DELAY_FILE" 2>/dev/null)
|
|
case "$delay_ms" in
|
|
''|*[!0-9]*) ;;
|
|
*)
|
|
if [ "$delay_ms" -gt 0 ]; then
|
|
sleep_secs=$((delay_ms / 1000))
|
|
[ "$sleep_secs" -lt 1 ] && sleep_secs=1
|
|
sleep "$sleep_secs"
|
|
fi
|
|
;;
|
|
esac
|
|
fi
|
|
export PATH="$KANDEV_E2E_ORIGINAL_PATH"
|
|
exec git "$@"
|
|
`,
|
|
{ mode: 0o755 },
|
|
);
|
|
|
|
const backendEnv = {
|
|
...stripGitHubTokens(process.env as Record<string, string>),
|
|
// Prepend the kandev bin dir so the host utility probe can locate
|
|
// the `mock-agent` binary via PATH. In production that dir is the
|
|
// same as the running kandev binary's dir, but e2e spawns via an
|
|
// absolute path and doesn't inherit that location.
|
|
PATH: `${shimDir}:${path.join(BACKEND_DIR, "bin")}:${originalPath}`,
|
|
KANDEV_E2E_ORIGINAL_PATH: originalPath,
|
|
KANDEV_E2E_GIT_DELAY_FILE: shimDelayFile,
|
|
HOME: tmpDir,
|
|
KANDEV_HOME_DIR: homeDir,
|
|
KANDEV_SERVER_PORT: String(backendPort),
|
|
KANDEV_DATABASE_PATH: dbPath,
|
|
KANDEV_MOCK_AGENT: "only",
|
|
KANDEV_MOCK_GITHUB: "true",
|
|
KANDEV_DOCKER_ENABLED: "false",
|
|
KANDEV_WORKTREE_ENABLED: "true",
|
|
KANDEV_WORKTREE_BASEPATH: worktreeBase,
|
|
KANDEV_REPOCLONE_BASEPATH: repoCloneBase,
|
|
KANDEV_LOG_LEVEL: process.env.KANDEV_LOG_LEVEL ?? "warn",
|
|
AGENTCTL_INSTANCE_PORT_BASE: String(agentctlPortBase),
|
|
AGENTCTL_INSTANCE_PORT_MAX: String(agentctlPortMax),
|
|
// Most E2E tests rely on auto-approve being on so tools that require
|
|
// permission (Edit, Bash) just complete without UI interaction. Tests
|
|
// that exercise the approve/reject flow itself opt in by spawning their
|
|
// own backend with this env var set to "false" — see permission-approval.spec.ts.
|
|
AGENTCTL_AUTO_APPROVE_PERMISSIONS: process.env.AGENTCTL_AUTO_APPROVE_PERMISSIONS ?? "true",
|
|
// Short window makes coalesce-boundary tests practical without wall-clock waits.
|
|
KANDEV_PLAN_COALESCE_WINDOW_MS: process.env.KANDEV_PLAN_COALESCE_WINDOW_MS ?? "2000",
|
|
GIT_AUTHOR_NAME: "E2E Test",
|
|
GIT_AUTHOR_EMAIL: "e2e@test.local",
|
|
GIT_COMMITTER_NAME: "E2E Test",
|
|
GIT_COMMITTER_EMAIL: "e2e@test.local",
|
|
};
|
|
|
|
const debug = !!process.env.E2E_DEBUG;
|
|
const baseUrl = `http://localhost:${backendPort}`;
|
|
|
|
// --- Spawn backend ---
|
|
let backendProc = spawnBackendProcess(backendEnv, debug, backendPort);
|
|
await waitForHealth(`${baseUrl}/health`, HEALTH_TIMEOUT_MS);
|
|
|
|
// Ensure Next.js static assets are available to the standalone server.
|
|
// The CLI start command does this via symlink; replicate it here.
|
|
// Use try/catch to handle concurrent workers racing to create the same symlink.
|
|
if (fs.existsSync(SOURCE_STATIC_DIR) && !fs.existsSync(STANDALONE_STATIC_DIR)) {
|
|
fs.mkdirSync(path.dirname(STANDALONE_STATIC_DIR), { recursive: true });
|
|
try {
|
|
fs.symlinkSync(SOURCE_STATIC_DIR, STANDALONE_STATIC_DIR, "junction");
|
|
} catch (err: unknown) {
|
|
if ((err as NodeJS.ErrnoException).code !== "EEXIST") throw err;
|
|
}
|
|
}
|
|
|
|
// --- Spawn frontend (Next.js standalone server) ---
|
|
const frontendProc: ChildProcess = spawn("node", [STANDALONE_SERVER], {
|
|
cwd: WEB_DIR,
|
|
env: {
|
|
...(process.env as unknown as Record<string, string>),
|
|
KANDEV_API_BASE_URL: baseUrl,
|
|
NEXT_PUBLIC_KANDEV_API_PORT: String(backendPort),
|
|
PORT: String(frontendPort),
|
|
HOSTNAME: "localhost",
|
|
NODE_ENV: "production",
|
|
} as unknown as NodeJS.ProcessEnv,
|
|
stdio: ["ignore", "pipe", "pipe"],
|
|
});
|
|
|
|
frontendProc.stderr?.on("data", (chunk: Buffer) => {
|
|
if (debug) process.stderr.write(`[frontend:${frontendPort}] ${chunk.toString()}`);
|
|
});
|
|
|
|
const frontendUrl = `http://localhost:${frontendPort}`;
|
|
await waitForHealth(frontendUrl, HEALTH_TIMEOUT_MS);
|
|
|
|
/**
|
|
* Kill the backend process group and respawn with the same config.
|
|
* SQLite DB, tmpDir, and all persisted data survive the restart.
|
|
* Only in-memory execution state (running agents, WS connections) is lost.
|
|
*/
|
|
const restart = async (envOverrides?: Record<string, string>) => {
|
|
if (envOverrides) {
|
|
for (const [k, v] of Object.entries(envOverrides)) {
|
|
(backendEnv as Record<string, string>)[k] = v;
|
|
}
|
|
}
|
|
await killProcessGroup(backendProc);
|
|
// Wait for OS to release the TCP port — Linux TIME_WAIT can exceed 500ms under load
|
|
await new Promise((r) => setTimeout(r, 2_000));
|
|
backendProc = spawnBackendProcess(backendEnv, debug, backendPort);
|
|
// Pass the process so waitForHealth fails fast if it exits (e.g. port still in use)
|
|
await waitForHealth(`${baseUrl}/health`, HEALTH_TIMEOUT_MS, backendProc);
|
|
};
|
|
|
|
try {
|
|
await use({ port: backendPort, baseUrl, frontendPort, frontendUrl, tmpDir, restart });
|
|
} finally {
|
|
// Shutdown frontend first (simple process), then backend (process group)
|
|
await killProcess(frontendProc);
|
|
await killProcessGroup(backendProc);
|
|
|
|
// Cleanup temp directory — ignore errors (backend may still hold files briefly)
|
|
try {
|
|
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
} catch {
|
|
// Non-fatal: OS may not have released all file handles yet
|
|
}
|
|
}
|
|
},
|
|
{ scope: "worker", timeout: 60_000 },
|
|
],
|
|
});
|
|
|
|
/** Strip GH_TOKEN / GITHUB_TOKEN so the mock client is used. */
|
|
function stripGitHubTokens(env: Record<string, string>): Record<string, string> {
|
|
const cleaned = { ...env };
|
|
delete cleaned.GH_TOKEN;
|
|
delete cleaned.GITHUB_TOKEN;
|
|
return cleaned;
|
|
}
|