Multi-Agent Collaboration for Team and Friends
TypeScript agent-automationagent-orchestrationagent-workflowai-agentsai-collaborationai-memoryai-workforceautonomous-agentsclaude-codeclaude-code-skillscodexcoding-agentmulti-agentmultiple-agentone-person-businessone-person-companyone-person-teamopencodesolopreneur
fix(e2e): use one native D1 owner for managed UI services (#998)
Generc4ac6b41ae07
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex 0ae6a3bf0..91b2fb4b9 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -103,6 +103,72 @@ Use the body to explain *what and why*, not how. Reference issues with `Closes #Tests run on both Ubuntu and Windows in CI.+### UI browser E2E and formal local QA++Run `pnpm test:e2e-ui` from the repository root. Local runs and CI use the+same built Worker graph: one pinned Wrangler loads Web, WS and the real+queue worker; Blog runs separately with its own storage. The public ingress+is `http://localhost:3000`, with Web on port 3001 and Blog on port 3002. Stop other dev+stacks before this run. The runner does not reuse a stack based on health+responses and does not stop listeners owned by another worktree.++Prepare local Worker variables before running. If the files do not exist,+copy `src/web/.dev.vars.example` to `src/web/.dev.vars` and+`src/queue-worker/.dev.vars.example` to `src/queue-worker/.dev.vars`.+Fill in local `BETTER_AUTH_SECRET` and `ENCRYPTION_KEY`; the queue worker+must use the same encryption key as Web. Keep these files private. The Web+Worker variables must also include:++```dotenv+BETTER_AUTH_URL=http://localhost:3000+NODE_ENV=development+DEV_WS_DO_URL=http://localhost:3000+```++`NODE_ENV` here is a local Worker variable used by authentication through+`env.NODE_ENV`; it is not a build command override or a queue transport+switch. Next compiles the built Web producer's `process.env.NODE_ENV` as+`production`, so this entry uses `TASK_QUEUE.sendBatch`. The auth URL,+browser origin and WS ingress must agree. The browser's token+response can override its compiled WS port, so changing only a test URL is+insufficient. The test process also honors `ALOOK_SERVER_URL` and+`DEV_WS_DO_URL`; both must describe this graph's public ingress. A stale+split WS override such as `http://localhost:8789` is rejected. An override does not reconfigure+the launcher's fixed ports. Machines connect through+`/api/ws/community-machine` at the same ingress.++Both Web and Blog are built before startup. Set `ALOOK_E2E_PREBUILT=1` only+after building both from the exact source under test; setup checks that+both Worker entrypoints exist, but does not prove their freshness. This+entry does not provide Next source hot reload: rebuild for source changes.++With the checkout's local state users stopped, local setup moves existing+Web `.wrangler/state` to its sibling `.wrangler/state.e2e-backup` before+either build. This preserves the original bytes, file types and permission+modes, including under a restrictive umask. Startup then resets the test+database and creates fresh actors without replacing that original backup.+Teardown stops the owned process groups before moving the original state+back; build and startup failures use the same cleanup. An unresolved backup+blocks preparation and remains available for recovery; do not delete it+or start another run over it.+Do not set `CI=1` to select the Worker graph: CI also disables the local+state backup. Formal QA probes must use the same managed setup and teardown.++Verify actual shared D1 reads/writes, browser and machine WS events, and+task execution rather than health alone. In this built Worker graph, Web+publishes to the local `alook-queue` binding and the queue worker's `queue()`+consumer runs the existing task handler. Verify send, consumption, D1 state+and the resulting machine wake; health alone is insufficient. This is local+Queue simulation, not proof of production durability or restart recovery.+The separate Next development mode retains its HTTP task transport. Record+the built artifact and transport that actually ran. Worker count, retries,+timeouts and the CI matrix retain their existing settings.++This entry covers UI Playwright and QA using its lifecycle. The separate+Vitest API/integration `pnpm test:e2e` job, root `pnpm dev`, Email topology+and full-stack hot reload retain their existing entrypoints and remain+outside this change.+## Scripts Reference| Command | What it does |diff --git a/src/web/src/test/e2e-service-definitions.test.ts b/src/web/src/test/e2e-service-definitions.test.tsindex 2eb216182..bae2a6926 100644--- a/src/web/src/test/e2e-service-definitions.test.ts+++ b/src/web/src/test/e2e-service-definitions.test.ts@@ -16,7 +16,7 @@ import {waitForServicesReady,wranglerLogEnvironment,} from "./e2e-ui/_setup/services"-import { resolveMachineWsUrl, resolveWsUrl } from "./e2e-ui/_setup/paths"+import { MACHINE_WS_URL, resolveMachineWsUrl, resolveWsUrl, WEB_URL, WS_URL } from "./e2e-ui/_setup/paths"const temporaryRoots: string[] = []@@ -25,6 +25,13 @@ afterEach(() => {})describe("UI E2E service definitions", () => {+ it("uses the native Worker graph and Web upgrade URLs by default locally", () => {+ expect(serviceDefinitions().map(({ name }) => name)).toEqual(["web-zones"])+ expect(WS_URL).toBe(WEB_URL)+ expect(MACHINE_WS_URL).toBe(`${WEB_URL}/api/ws/community-machine`)+ expect(serviceBuildCommands(undefined, false).map(({ name }) => name)).toEqual(["web worker", "Blog worker"])+ })+it("launches the exact Web workspace Wrangler instead of a PATH-selected CLI", () => {const runtime = resolveE2EWranglerRuntime()const requireFromTest = createRequire(import.meta.url)@@ -57,7 +64,7 @@ describe("UI E2E service definitions", () => {}])})- it("requires the real queue consumer alongside web and ws-do locally", () => {+ it("keeps the explicit split definition as a compatibility input with real worker URLs", () => {expect(serviceDefinitions(false).map(({ name, command }) => ({ name, command }))).toEqual([{ name: "web", command: "pnpm" },{ name: "ws-do", command: "pnpm" },@@ -67,6 +74,8 @@ describe("UI E2E service definitions", () => {name: "queue-worker", command: "pnpm", args: ["--filter", "@alook/queue-worker", "dev"],healthUrl: "http://localhost:8790/health", expectedStatus: 200, expectedBody: { status: "ok" },})+ expect(serviceDefinitions(false).find(({ name }) => name === "ws-do")?.healthUrl)+ .toBe("http://localhost:8789/health")})it("skips both builds only when both prebuilt worker entrypoints exist", () => {@@ -99,14 +108,22 @@ describe("UI E2E service definitions", () => {})).toBe("http://localhost:8789")})- it("always honors an explicit ws-do URL", () => {+ it("honors an explicit WS URL at the native graph's Web ingress", () => {expect(resolveWsUrl({- webUrl: "http://localhost:3000",+ webUrl: "http://localhost:9000",explicitWsUrl: "http://localhost:9000",singleRuntime: true,})).toBe("http://localhost:9000")})+ it("rejects a stale split WS override instead of leaving the native graph", () => {+ expect(() => resolveWsUrl({+ webUrl: "http://localhost:3000",+ explicitWsUrl: "http://localhost:8789",+ singleRuntime: true,+ })).toThrow("UI E2E WS URL must match the Web ingress")+ })+it("routes machine upgrades through the web worker in the single runtime", () => {expect(resolveMachineWsUrl({wsUrl: "http://localhost:3000/",diff --git a/src/web/src/test/e2e-service-startup.test.ts b/src/web/src/test/e2e-service-startup.test.tsindex cc307c01c..7330f9f56 100644--- a/src/web/src/test/e2e-service-startup.test.ts+++ b/src/web/src/test/e2e-service-startup.test.ts@@ -1,5 +1,5 @@import { EventEmitter } from "node:events"-import { existsSync, mkdirSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs"+import { chmodSync, existsSync, lstatSync, mkdirSync, readFileSync, readlinkSync, rmSync, symlinkSync, writeFileSync } from "node:fs"import { createRequire } from "node:module"import { dirname, resolve } from "node:path"import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest"@@ -16,8 +16,10 @@ vi.mock("./e2e-ui/_setup/paths", async () => {REPO_ROOT: transport.root,SERVICE_LOG_DIR: path.resolve(transport.root, "logs"),SERVICE_STATE_PATH: path.resolve(transport.root, "state.json"),+ resolveWsUrl: () => { throw new Error("Native startup must not resolve a split WS health URL") },+ SINGLE_RUNTIME: true,WEB_URL: "http://localhost:3000",- WS_URL: "http://localhost:8789",+ WS_URL: "http://localhost:3000",QUEUE_URL: "http://localhost:8790",}})@@ -65,10 +67,20 @@ function resetCalls() {return transport.spawnSync.mock.calls.filter(([command, args]) => command === "pnpm" && args[1] === "db:reset")}+async function startPreparedServices() {+ runtime.prepareServices()+ return runtime.startServices()+}+beforeEach(async () => {vi.stubEnv("CI", "")vi.stubEnv("ALOOK_E2E_FORCE_FRESH", "")await loadRuntime()+ const modules = resolve(transport.root, "src/web/node_modules")+ mkdirSync(modules, { recursive: true })+ if (!existsSync(resolve(modules, "wrangler"))) {+ symlinkSync(dirname(createRequire(import.meta.url).resolve("wrangler/package.json")), resolve(modules, "wrangler"), "dir")+ }rmSync(stateDir, { recursive: true, force: true })rmSync(backupDir, { recursive: true, force: true })children.length = 0@@ -107,56 +119,173 @@ afterAll(() => {})describe("normal UI service startup", () => {- it("waits for each exact health before launching the next shared-state runtime", async () => {- vi.stubEnv("ALOOK_E2E_FORCE_FRESH", "1")- await loadRuntime()- const web = heldHealth(), ws = heldHealth()+ it("preserves pre-build state bytes, types and modes after a failed build under umask077", async () => {+ priorState()+ const nested = resolve(stateDir, "nested")+ mkdirSync(nested)+ const linked = resolve(nested, "original-link")+ if (process.platform !== "win32") symlinkSync("../prior-state", linked)+ chmodSync(stateDir, 0o751)+ chmodSync(nested, 0o750)+ chmodSync(priorFile, 0o644)+ const originalModes = {+ file: lstatSync(priorFile).mode & 0o777,+ state: lstatSync(stateDir).mode & 0o777,+ nested: lstatSync(nested).mode & 0o777,+ }+ const seen: unknown[] = []+ transport.spawnSync.mockImplementation((command) => {+ if (command !== "pnpm") return { status: 0, stdout: "", stderr: "" }+ seen.push({+ original: readFileSync(resolve(backupDir, "prior-state"), "utf8"),+ mode: lstatSync(resolve(backupDir, "prior-state")).mode & 0o777,+ restorePending: readLifecycleState(statePath)?.restorePending,+ })+ mkdirSync(stateDir, { recursive: true })+ writeFileSync(priorFile, "mutated by failed build")+ return { status: 1, signal: null, stdout: "", stderr: "" }+ })+ const previousUmask = process.umask(0o077)+ try {+ expect(() => runtime.prepareServices()).toThrow("web worker build failed")+ await runtime.stopServicesAndRestore()+ expect(seen).toEqual([{ original: "original local state", mode: originalModes.file, restorePending: true }])+ expect(readFileSync(priorFile, "utf8")).toBe("original local state")+ expect(lstatSync(priorFile).mode & 0o777).toBe(originalModes.file)+ expect(lstatSync(stateDir).mode & 0o777).toBe(originalModes.state)+ expect(lstatSync(nested).mode & 0o777).toBe(originalModes.nested)+ if (process.platform !== "win32") {+ expect(originalModes).toEqual({ file: 0o644, state: 0o751, nested: 0o750 })+ expect(lstatSync(linked).isSymbolicLink()).toBe(true)+ expect(readlinkSync(linked)).toBe("../prior-state")+ }+ expect(existsSync(backupDir)).toBe(false)+ expect(transport.spawn).not.toHaveBeenCalled()+ writeFileSync(priorFile, "after restored build failure")+ await runtime.stopServicesAndRestore()+ expect(readFileSync(priorFile, "utf8")).toBe("after restored build failure")+ } finally {+ process.umask(previousUmask)+ }+ })++ it("retains the pre-build baseline through preparation and native startup", async () => {+ priorState()+ const previousSpawn = transport.spawnSync.getMockImplementation()!+ transport.spawnSync.mockImplementation((command, args, options) => {+ if (command === "pnpm" && args[1] !== "db:reset") {+ mkdirSync(stateDir, { recursive: true })+ writeFileSync(priorFile, "build-created state")+ }+ return previousSpawn(command, args, options)+ })+ coldHealth()+ runtime.prepareServices()+ expect(readFileSync(resolve(backupDir, "prior-state"), "utf8")).toBe("original local state")+ expect(readLifecycleState(statePath)?.restorePending).toBe(true)+ const starting = runtime.startServices()+ await vi.advanceTimersByTimeAsync(1_000)+ await starting+ expect(readFileSync(resolve(backupDir, "prior-state"), "utf8")).toBe("original local state")+ await runtime.stopServicesAndRestore()+ expect(readFileSync(priorFile, "utf8")).toBe("original local state")+ expect(existsSync(initializedFile)).toBe(false)+ })++ it.each(process.platform === "win32" ? ["directory", "file"] : ["directory", "file", "dangling link"])("rejects an unresolved %s backup before a build without replacing either state", (kind) => {+ priorState()+ if (kind === "directory") {+ mkdirSync(backupDir)+ writeFileSync(resolve(backupDir, "prior-state"), "unresolved original")+ } else if (kind === "file") writeFileSync(backupDir, "unresolved original")+ else symlinkSync("missing-original", backupDir)+ expect(() => runtime.prepareServices()).toThrow("unresolved E2E state backup")+ expect(readFileSync(priorFile, "utf8")).toBe("original local state")+ if (kind === "dangling link") expect(readlinkSync(backupDir)).toBe("missing-original")+ else expect(readFileSync(kind === "directory" ? resolve(backupDir, "prior-state") : backupDir, "utf8")).toBe("unresolved original")+ expect(transport.spawnSync).not.toHaveBeenCalled()+ expect(readLifecycleState(statePath)?.restorePending).toBe(false)+ })++ it.each(process.platform === "win32" ? ["file"] : ["file", "dangling link"])("rejects a %s state root before changing it or building", (kind) => {+ mkdirSync(dirname(stateDir), { recursive: true })+ if (kind === "file") writeFileSync(stateDir, "unexpected state file")+ else symlinkSync("missing-state", stateDir)+ expect(() => runtime.prepareServices()).toThrow("E2E local state must be a directory")+ if (kind === "file") expect(readFileSync(stateDir, "utf8")).toBe("unexpected state file")+ else expect(readlinkSync(stateDir)).toBe("missing-state")+ expect(existsSync(backupDir)).toBe(false)+ expect(transport.spawnSync).not.toHaveBeenCalled()+ expect(readLifecycleState(statePath)?.restorePending).toBe(false)+ })++ it("does not back up build-created state when no local state existed before preparation", async () => {+ const previousSpawn = transport.spawnSync.getMockImplementation()!+ transport.spawnSync.mockImplementation((command, args, options) => {+ if (command === "pnpm" && args[1] !== "db:reset") {+ mkdirSync(stateDir, { recursive: true })+ writeFileSync(priorFile, "build-created state")+ }+ return previousSpawn(command, args, options)+ })+ coldHealth()+ expect(existsSync(stateDir)).toBe(false)+ runtime.prepareServices()+ expect(readFileSync(priorFile, "utf8")).toBe("build-created state")+ expect(readLifecycleState(statePath)?.restorePending).toBe(false)+ const starting = runtime.startServices()+ await vi.advanceTimersByTimeAsync(1_000)+ await starting+ expect(existsSync(backupDir)).toBe(false)+ expect(readLifecycleState(statePath)?.restorePending).toBe(false)+ await runtime.stopServicesAndRestore()+ expect(existsSync(priorFile)).toBe(false)+ expect(existsSync(initializedFile)).toBe(true)+ })++ it("waits for the native graph's exact health before warmup and owns its process group", async () => {+ const web = heldHealth()vi.stubGlobal("fetch", vi.fn((url: string) => {if (url === "http://localhost:3000/api/health") return web.promise.then(health)- if (url === "http://localhost:8789/health") return ws.promise.then(health)return Promise.resolve(health())}))- const starting = runtime.startServices()+ const starting = startPreparedServices()await vi.advanceTimersByTimeAsync(1_000)expect(transport.spawn).toHaveBeenCalledTimes(1)- expect(readLifecycleState(resolve(transport.root, "state.json"))?.services.map((service) => service.name)).toEqual(["web"])+ expect(readLifecycleState(resolve(transport.root, "state.json"))?.services.map((service) => service.name)).toEqual(["web-zones"])+ expect(vi.mocked(fetch).mock.calls.some(([url]) => url === "http://localhost:3000/c")).toBe(false)web.release()await vi.advanceTimersByTimeAsync(0)- expect(transport.spawn).toHaveBeenCalledTimes(2)- expect(readLifecycleState(resolve(transport.root, "state.json"))?.services.map((service) => service.name)).toEqual(["web", "ws-do"])-- ws.release()- await vi.advanceTimersByTimeAsync(0)- expect((await starting).map((service) => service.name)).toEqual(["web", "ws-do", "queue-worker"])- expect(transport.spawn).toHaveBeenCalledTimes(3)+ expect((await starting).map((service) => service.name)).toEqual(["web-zones"])+ expect(transport.spawn).toHaveBeenCalledTimes(1)expect(readLifecycleState(resolve(transport.root, "state.json"))?.status).toBe("healthy")- expect(vi.mocked(fetch).mock.calls.some(([url]) => url === "http://localhost:8790/health")).toBe(true)+ expect(vi.mocked(fetch).mock.calls.some(([url]) => url === "http://localhost:3000/c")).toBe(true)+ expect(vi.mocked(fetch).mock.calls.some(([url]) => /:(8789|8790)\//.test(String(url)))).toBe(false)await runtime.stopServicesAndRestore()expect(readLifecycleState(resolve(transport.root, "state.json"))?.status).toBe("stopped")expect(process.kill).toHaveBeenCalledWith(-children[0].pid, "SIGTERM")})- it("rejects an exit during readiness and never launches later services", async () => {- vi.stubEnv("ALOOK_E2E_FORCE_FRESH", "1")- await loadRuntime()- const ws = heldHealth()- vi.stubGlobal("fetch", vi.fn((url: string) => url === "http://localhost:8789/health" ? ws.promise.then(health) : Promise.resolve(health())))+ it("rejects a native graph exit during readiness and never reaches warmup", async () => {+ const web = heldHealth()+ vi.stubGlobal("fetch", vi.fn(() => web.promise.then(health)))- const starting = runtime.startServices()- const failed = expect(starting).rejects.toThrow("ws-do exited before readiness (code 1, signal null)")+ const starting = startPreparedServices()+ const failed = expect(starting).rejects.toThrow("web-zones exited before readiness (code 1, signal null)")await vi.advanceTimersByTimeAsync(1_000)- expect(transport.spawn).toHaveBeenCalledTimes(2)- children[1].exitCode = 1- children[1].emit("close", 1, null)+ expect(transport.spawn).toHaveBeenCalledTimes(1)+ children[0].exitCode = 1+ children[0].emit("close", 1, null)await failed- expect(transport.spawn).toHaveBeenCalledTimes(2)- expect(readLifecycleState(resolve(transport.root, "state.json"))?.failure).toMatchObject({ service: "ws-do", kind: "child-close", code: 1 })+ expect(transport.spawn).toHaveBeenCalledTimes(1)+ expect(vi.mocked(fetch).mock.calls.some(([url]) => url === "http://localhost:3000/c")).toBe(false)+ expect(readLifecycleState(resolve(transport.root, "state.json"))?.failure).toMatchObject({ service: "web-zones", kind: "child-close", code: 1 })await runtime.stopServicesAndRestore()expect(readLifecycleState(resolve(transport.root, "state.json"))?.status).toBe("stopped")- expect(process.kill).toHaveBeenCalledWith(-children[1].pid, "SIGTERM")+ expect(process.kill).toHaveBeenCalledWith(-children[0].pid, "SIGTERM")})it("initializes an absent database before the first spawn under default local cold startup", async () => {@@ -168,7 +297,7 @@ describe("normal UI service startup", () => {return spawnChild()})- const starting = runtime.startServices()+ const starting = startPreparedServices()await vi.advanceTimersByTimeAsync(1_000)await starting@@ -176,7 +305,7 @@ describe("normal UI service startup", () => {expect(resetCalls()[0]).toEqual(["pnpm", ["run", "db:reset"], {cwd: transport.root, encoding: "utf8", stdio: ["ignore", "ignore", "pipe"],}])- expect(initializedAtSpawn).toEqual([true, true, true])+ expect(initializedAtSpawn).toEqual([true])expect(readLifecycleState(statePath)?.restorePending).toBe(false)expect(existsSync(backupDir)).toBe(false)})@@ -197,7 +326,7 @@ describe("normal UI service startup", () => {return { status: 1, signal: null, stdout: "", stderr: "migration failed\n" }})const stderr = vi.spyOn(process.stderr, "write").mockReturnValue(true)- const starting = runtime.startServices()+ const starting = startPreparedServices()const failed = starting.then(() => null, (error: unknown) => error)await vi.advanceTimersByTimeAsync(1_000)expect(await failed).toMatchObject({ message: "db:reset failed (exit 1, signal null)" })@@ -216,46 +345,44 @@ describe("normal UI service startup", () => {expect(resetCalls()).toHaveLength(1)})- it("reuses all exact healthy services once without owning processes or changing prior state", async () => {+ it("starts an owned native graph even if prior services look healthy and restores local state", async () => {priorState()vi.stubGlobal("fetch", vi.fn(() => Promise.resolve(health())))- expect(await runtime.startServices()).toEqual([])- for (const url of ["http://localhost:3000/api/health", "http://localhost:8789/health", "http://localhost:8790/health"]) {- expect(vi.mocked(fetch).mock.calls.filter(([called]) => called === url)).toHaveLength(1)- }- expect(transport.spawn).not.toHaveBeenCalled()- expect(transport.spawnSync).not.toHaveBeenCalled()- expect(existsSync(backupDir)).toBe(false)- expect(readLifecycleState(statePath)).toMatchObject({ status: "healthy", restorePending: false, services: [] })+ const starting = startPreparedServices()+ await vi.advanceTimersByTimeAsync(1_000)+ expect((await starting).map(({ name }) => name)).toEqual(["web-zones"])+ expect(transport.spawn).toHaveBeenCalledTimes(1)+ expect(resetCalls()).toHaveLength(1)+ expect(readFileSync(resolve(backupDir, "prior-state"), "utf8")).toBe("original local state")+ expect(existsSync(priorFile)).toBe(false)+ expect(readLifecycleState(statePath)).toMatchObject({ status: "healthy", restorePending: true, services: [{ name: "web-zones" }] })await runtime.stopServicesAndRestore()- expect(process.kill).not.toHaveBeenCalled()+ expect(process.kill).toHaveBeenCalledWith(-children[0].pid, "SIGTERM")expect(readFileSync(priorFile, "utf8")).toBe("original local state")+ expect(existsSync(backupDir)).toBe(false)+ expect(readLifecycleState(statePath)).toMatchObject({ status: "stopped", restorePending: false })})- it("initializes all services when only part of the required health batch matches", async () => {+ it("does not probe or adopt the old split services before initializing the native graph", async () => {vi.stubGlobal("fetch", vi.fn((url: string) => Promise.resolve(url === "http://localhost:8789/health" && children.length === 0? new Response('{"status":"unavailable"}', { status: 503 }): health(),)))- const starting = runtime.startServices()+ const starting = startPreparedServices()await vi.advanceTimersByTimeAsync(1_000)- expect((await starting).map(({ name }) => name)).toEqual(["web", "ws-do", "queue-worker"])+ expect((await starting).map(({ name }) => name)).toEqual(["web-zones"])+ expect(vi.mocked(fetch).mock.calls.some(([url]) => /:(8789|8790)\//.test(String(url)))).toBe(false)expect(resetCalls()).toHaveLength(1)expect(readFileSync(initializedFile, "utf8")).toBe("initialized")})it.each([- ["ALOOK_E2E_FORCE_FRESH", 3],+ ["ALOOK_E2E_FORCE_FRESH", 1],["CI", 1],] as const)("starts fresh under %s even if existing health would match", async (key, count) => {- if (key === "CI") {- const modules = resolve(transport.root, "src/web/node_modules")- mkdirSync(modules, { recursive: true })- symlinkSync(dirname(createRequire(import.meta.url).resolve("wrangler/package.json")), resolve(modules, "wrangler"), "dir")- }vi.stubEnv(key, "1")await loadRuntime()vi.stubGlobal("fetch", vi.fn(() => Promise.resolve(health())))@@ -266,7 +393,7 @@ describe("normal UI service startup", () => {return spawnChild()})- const starting = runtime.startServices()+ const starting = startPreparedServices()await vi.advanceTimersByTimeAsync(1_000)expect(await starting).toHaveLength(count)expect(initialProbeCounts[0]).toBe(0)@@ -275,22 +402,22 @@ describe("normal UI service startup", () => {expect(readLifecycleState(statePath)?.restorePending).toBe(false)})- it.each([false, true])("keeps a warmup500 fatal with reuse=%s and preserves the correct state owner", async (reuse) => {+ it.each([false, true])("keeps a warmup500 fatal with CI=%s and restores only local prior state", async (ci) => {+ vi.stubEnv("CI", ci ? "1" : "")+ await loadRuntime()priorState()vi.stubGlobal("fetch", vi.fn((url: string) => Promise.resolve(url === "http://localhost:3000/c/channels/warmup"? new Response("original warmup failure", { status: 500 })- : !reuse && url.endsWith("/health") && children.length === 0- ? new Response('{"status":"unavailable"}', { status: 503 })- : health(),+ : health(),)))- const starting = runtime.startServices()+ const starting = startPreparedServices()const failed = starting.then(() => null, (error: unknown) => error)await vi.advanceTimersByTimeAsync(1_000)expect(await failed).toMatchObject({ message: "Route warm-up failed (500 /c/channels/warmup)" })- expect(resetCalls()).toHaveLength(reuse ? 0 : 1)- expect(transport.spawn).toHaveBeenCalledTimes(reuse ? 0 : 3)- expect(readLifecycleState(statePath)?.restorePending).toBe(!reuse)+ expect(resetCalls()).toHaveLength(1)+ expect(transport.spawn).toHaveBeenCalledTimes(1)+ expect(readLifecycleState(statePath)?.restorePending).toBe(!ci)const originalAtTermination: boolean[] = []vi.mocked(process.kill).mockImplementation((_pid, signal) => {@@ -299,8 +426,9 @@ describe("normal UI service startup", () => {return true})await runtime.stopServicesAndRestore()- expect(originalAtTermination).toEqual(reuse ? [] : [false, false, false])- expect(readFileSync(priorFile, "utf8")).toBe("original local state")+ expect(originalAtTermination).toEqual([false])+ if (!ci) expect(readFileSync(priorFile, "utf8")).toBe("original local state")+ else expect(existsSync(priorFile)).toBe(false)expect(existsSync(backupDir)).toBe(false)expect(readLifecycleState(statePath)).toMatchObject({ status: "stopped", restorePending: false })expect(vi.mocked(fetch).mock.calls.filter(([url]) => url === "http://localhost:3000/c/channels/warmup")).toHaveLength(1)diff --git a/src/web/src/test/e2e-ui/_setup/paths.ts b/src/web/src/test/e2e-ui/_setup/paths.tsindex c1855cf60..ea9ba037d 100644--- a/src/web/src/test/e2e-ui/_setup/paths.ts+++ b/src/web/src/test/e2e-ui/_setup/paths.ts@@ -11,6 +11,7 @@ export const SERVICE_LOG_DIR = resolve(REPO_ROOT, "src/web/e2e-service-logs")export const SERVICE_STATE_PATH = resolve(AUTH_DIR, "service-state.json")export const SERVICE_FAILURE_CLAIM_PATH = resolve(AUTH_DIR, "service-failure-claimed")+export const SINGLE_RUNTIME = trueexport const WEB_URL = process.env.ALOOK_SERVER_URL || "http://localhost:3000"export const QUEUE_URL = "http://localhost:8790"@@ -23,7 +24,12 @@ export function resolveWsUrl({explicitWsUrl?: stringsingleRuntime: boolean}): string {- if (explicitWsUrl) return explicitWsUrl+ if (explicitWsUrl) {+ if (singleRuntime && new URL(explicitWsUrl).href.replace(/\/$/, "") !== new URL(webUrl).href.replace(/\/$/, "")) {+ throw new Error("UI E2E WS URL must match the Web ingress; remove the split DEV_WS_DO_URL override")+ }+ return explicitWsUrl+ }return singleRuntime ? webUrl : "http://localhost:8789"}@@ -41,10 +47,10 @@ export function resolveMachineWsUrl({export const WS_URL = resolveWsUrl({webUrl: WEB_URL,explicitWsUrl: process.env.DEV_WS_DO_URL,- singleRuntime: !!process.env.CI,+ singleRuntime: SINGLE_RUNTIME,})export const MACHINE_WS_URL = resolveMachineWsUrl({wsUrl: WS_URL,- singleRuntime: !!process.env.CI,+ singleRuntime: SINGLE_RUNTIME,})diff --git a/src/web/src/test/e2e-ui/_setup/services.ts b/src/web/src/test/e2e-ui/_setup/services.tsindex 09cf94505..8918d450f 100644--- a/src/web/src/test/e2e-ui/_setup/services.ts+++ b/src/web/src/test/e2e-ui/_setup/services.ts@@ -1,8 +1,8 @@import { spawn, spawnSync, type ChildProcess } from "child_process"import { createRequire } from "module"-import { closeSync, cpSync, existsSync, mkdirSync, openSync, rmSync, statSync } from "fs"+import { closeSync, existsSync, lstatSync, mkdirSync, openSync, renameSync, rmSync, statSync } from "fs"import { dirname, resolve } from "path"-import { QUEUE_URL, REPO_ROOT, SERVICE_LOG_DIR, SERVICE_STATE_PATH, WEB_URL, WS_URL } from "./paths"+import { QUEUE_URL, REPO_ROOT, resolveWsUrl, SERVICE_LOG_DIR, SERVICE_STATE_PATH, SINGLE_RUNTIME, WEB_URL } from "./paths"import {addServiceProcess,applyHealthProbe,@@ -82,28 +82,35 @@ export function readinessExitMessage(: `${service} exited before readiness (code ${exitCode}, signal ${signalCode})`}-export const SINGLE_RUNTIME = !!process.env.CI+export { SINGLE_RUNTIME } from "./paths"-// Local D1/DO state that `db:reset` wipes. Backing it up to a sibling path-// (outside `.wrangler/state`, so `rm -rf .wrangler/state` can't touch it)-// lets a local run restore the developer's dev data on teardown. CI has no-// prior state, so backup/restore is a no-op there.const STATE_DIR = resolve(REPO_ROOT, "src/web/.wrangler/state")const STATE_BACKUP_DIR = resolve(REPO_ROOT, "src/web/.wrangler/state.e2e-backup")-// Returns true if a backup was taken (i.e. there was existing state to save).-function backupState(): boolean {- if (process.env.CI || !existsSync(STATE_DIR)) return false- rmSync(STATE_BACKUP_DIR, { recursive: true, force: true })- cpSync(STATE_DIR, STATE_BACKUP_DIR, { recursive: true })- return true+function backupState(): void {+ const state = readLifecycleState(SERVICE_STATE_PATH)+ if (!state) throw new Error("service lifecycle state was not initialized")+ if (process.env.CI) return+ if (lstatSync(STATE_BACKUP_DIR, { throwIfNoEntry: false })) throw new Error(`unresolved E2E state backup: ${STATE_BACKUP_DIR}`)+ if (state.restorePending) throw new Error("E2E state restoration is already pending")+ const original = lstatSync(STATE_DIR, { throwIfNoEntry: false })+ if (!original) return+ if (!original.isDirectory()) throw new Error("E2E local state must be a directory")+ writeLifecycleState(SERVICE_STATE_PATH, { ...state, restorePending: true })+ try {+ renameSync(STATE_DIR, STATE_BACKUP_DIR)+ } catch (error) {+ writeLifecycleState(SERVICE_STATE_PATH, state)+ throw error+ }}export function restoreState(): void {- if (!existsSync(STATE_BACKUP_DIR)) return+ if (!lstatSync(STATE_BACKUP_DIR, { throwIfNoEntry: false })?.isDirectory()) {+ throw new Error("E2E state backup must be a directory during restoration")+ }rmSync(STATE_DIR, { recursive: true, force: true })- cpSync(STATE_BACKUP_DIR, STATE_DIR, { recursive: true })- rmSync(STATE_BACKUP_DIR, { recursive: true, force: true })+ renameSync(STATE_BACKUP_DIR, STATE_DIR)}export async function hasExactHealth(@@ -257,6 +264,7 @@ export function serviceBuildCommands(}export function prepareServices(): void {+ backupState()for (const build of serviceBuildCommands()) {const res = spawnSync("pnpm", build.args, {cwd: REPO_ROOT,@@ -330,10 +338,14 @@ function killPortOrphans(ports: number[]): void {}}-export function serviceDefinitions(singleRuntime: boolean): ServiceDefinition[] {+export function serviceDefinitions(singleRuntime = SINGLE_RUNTIME): ServiceDefinition[] {const webHealth = `${WEB_URL}/api/health`- const wsHealth = `${WS_URL}/health`if (!singleRuntime) {+ const wsHealth = `${resolveWsUrl({+ webUrl: WEB_URL,+ explicitWsUrl: process.env.DEV_WS_DO_URL,+ singleRuntime,+ })}/health`return [{name: "web",@@ -465,14 +477,6 @@ function startHealthSupervisor(services: ManagedService[]): void {healthSupervisor.unref()}-// Starts web (:3000) + ws-do (:8789). Realtime journeys REQUIRE ws-do, so a-// missing ws health check is a hard failure (fail fast), never a silent-// degrade. Returns started services (empty when reusing an existing server).-// `next dev` compiles each route lazily on first request, so a health check-// (which only proves the process is up) doesn't mean `/c/channels/...` is-// compiled. The first spec to hit a route then eats multi-second cold-compile-// time and its `waitForURL` can time out. Pre-hit the hot routes so they're-// warm before any spec runs.async function warmUpRoutes(): Promise<void> {// Include the DYNAMIC route segments the first specs land on — `next dev`// compiles per route *file*, not per id, so a placeholder id triggers the@@ -491,14 +495,6 @@ async function warmUpRoutes(): Promise<void> {export async function startServices(): Promise<ManagedService[]> {const definitions = serviceDefinitions(SINGLE_RUNTIME)- const reuseExisting = !process.env.CI && !process.env.ALOOK_E2E_FORCE_FRESH-- if (reuseExisting && (await Promise.all(definitions.map(({ healthUrl }) => hasExactHealth(healthUrl)))).every((health) => health.ok)) {- const state = readLifecycleState(SERVICE_STATE_PATH)- if (state) writeLifecycleState(SERVICE_STATE_PATH, { ...state, status: "healthy" })- await warmUpRoutes()- return []- }// Starting fresh — clear any orphaned dev servers on our ports first so a// prior crashed run doesn't leave 3000/8789 occupied (or get reused).@@ -511,8 +507,9 @@ export async function startServices(): Promise<ManagedService[]> {await new Promise((r) => setTimeout(r, 1000))const initialState = readLifecycleState(SERVICE_STATE_PATH)if (!initialState) throw new Error("service lifecycle state was not initialized")- const restorePending = backupState()- writeLifecycleState(SERVICE_STATE_PATH, { ...initialState, restorePending })+ if (initialState.restorePending && !lstatSync(STATE_BACKUP_DIR, { throwIfNoEntry: false })?.isDirectory()) {+ throw new Error("E2E state backup is missing before service startup")+ }resetDb()rmSync(SERVICE_LOG_DIR, { recursive: true, force: true })mkdirSync(SERVICE_LOG_DIR, { recursive: true })