Skip to content
Merged
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
55 changes: 55 additions & 0 deletions src/cli/control.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 16 additions & 10 deletions src/cli/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 };
Expand Down