From 199520beb7ff1ed3276ff3694c707ed01fda9471 Mon Sep 17 00:00:00 2001 From: "developerz-ai[bot]" <286432749+developerz-ai[bot]@users.noreply.github.com> Date: Wed, 11 Nov 2026 01:41:55 +0000 Subject: [PATCH] fix: report live runs after config workspace drift Dz-Task-Id: tsk_0dae0ad1c95257675f4446f97986992f --- src/cli/control.test.ts | 55 +++++++++++++++++++++++++++++++++++++++++ src/cli/control.ts | 26 +++++++++++-------- 2 files changed, 71 insertions(+), 10 deletions(-) diff --git a/src/cli/control.test.ts b/src/cli/control.test.ts index 26e4978..3e16793 100644 --- a/src/cli/control.test.ts +++ b/src/cli/control.test.ts @@ -275,6 +275,61 @@ test('stop signals the live pid and marks the LIVE workspace stopped after a con expect(await readState(drifted.stateJson)).toBeNull(); // nothing invented in the new workspace }); +test('status follows a live run when the effective config moves from .dz to root', async () => { + const { live, drifted } = await startThenEditConfig(); + const dzConfig = join(cwd, '.dz', 'ui-debugger', 'ui-debugger-mcp.json'); + await writeFile( + join(cwd, '.ui-debugger-mcp.json'), + JSON.stringify({ + workspace: './ws-b', + targets: { web: { adapter: 'browser', url: 'http://x' } }, + }), + 'utf8', + ); + await rm(dzConfig); + + await runStatus(cwd); + + const out = logs.join('\n'); + expect(out).toContain('live-1'); + expect(out).toContain('target: web'); + expect(out).toContain('goal: drifted run'); + expect(out).toContain(`pid ${process.pid} — running`); + expect(out).toContain('status: running'); + expect(out).toContain('findings: no findings yet'); + expect(out).toContain(live.root); + expect(out).toContain(drifted.root); +}); + +test('stop follows a live run when the effective config moves from .dz to root', async () => { + const { live, drifted } = await startThenEditConfig(); + const dzConfig = join(cwd, '.dz', 'ui-debugger', 'ui-debugger-mcp.json'); + await writeFile( + join(cwd, '.ui-debugger-mcp.json'), + JSON.stringify({ + workspace: './ws-b', + targets: { web: { adapter: 'browser', url: 'http://x' } }, + }), + 'utf8', + ); + await rm(dzConfig); + const kills: number[] = []; + + await runStop(cwd, (pid) => kills.push(pid)); + + expect(kills).toEqual([process.pid]); + expect((await readState(live.stateJson))?.status).toBe('stopped'); + expect(await readState(drifted.stateJson)).toBeNull(); +}); + +test('status and stop reject an unreadable recorded live workspace as config drift', async () => { + const { live } = await startThenEditConfig(); + await rm(live.stateJson); + + await expect(runStatus(cwd)).rejects.toThrow(/Config drift.*no readable state breadcrumb/); + await expect(runStop(cwd)).rejects.toThrow(/Config drift.*no readable state breadcrumb/); +}); + test('record stamps workspaceRoot and a no-drift status prints no drift line', async () => { const { live } = await startThenEditConfig(); expect((await readState(live.stateJson))?.workspaceRoot).toBe(live.root); diff --git a/src/cli/control.ts b/src/cli/control.ts index 72717c5..17e938c 100644 --- a/src/cli/control.ts +++ b/src/cli/control.ts @@ -52,9 +52,11 @@ function configWorkspace(cwd: string) { * * The server's workspace was fixed from the config at boot; the config may have * been edited since. The live pointer names the root the server really owns, so - * it wins — even over an invalid config. No usable pointer (legacy server, no run - * yet) → the config-resolved workspace, whose ConfigError then surfaces loud. - * `drift` is set when the live root and the current config disagree. + * it wins — even over an invalid config. A recorded pointer without its matching + * breadcrumb is config drift, not evidence that no run exists: fail loud rather + * than falling back to the current config's possibly unrelated workspace. No + * pointer (legacy server, no run yet) → the config-resolved workspace, whose + * ConfigError then surfaces loud. `drift` is set when live and config roots differ. */ async function resolveState( cwd: string, @@ -64,14 +66,18 @@ async function resolveState( const liveRoot = await readLivePointer(cwd); if (liveRoot !== null) { const stateJson = join(liveRoot, 'state.json'); - if ((await readState(stateJson)) !== null) { - if (config !== null && resolve(config.root) === resolve(liveRoot)) { - return { project, stateJson }; - } - const now = config === null ? 'is unreadable or invalid' : `resolves to ${config.root}`; - const drift = `the live run's workspace is ${liveRoot}, but the config now ${now} — config edited since the server started`; - return { project, stateJson, drift }; + if ((await readState(stateJson)) === null) { + throw new ConfigError( + `Config drift: recorded live workspace ${liveRoot} has no readable state breadcrumb at ${stateJson}. ` + + 'The config may have changed since the server started; restore the live workspace or restart the server.', + ); + } + if (config !== null && resolve(config.root) === resolve(liveRoot)) { + return { project, stateJson }; } + const now = config === null ? 'is unreadable or invalid' : `resolves to ${config.root}`; + const drift = `the live run's workspace is ${liveRoot}, but the config now ${now} — config edited since the server started`; + return { project, stateJson, drift }; } if (config === null) loadWorkspaceDir(cwd); // rethrows the ConfigError — fail loud return { project, stateJson: (config ?? workspacePaths(cwd)).stateJson };