feat(web): explain a failed open, keep the dialog, and follow instance_state

Opening a project showed a toast on success and a bare code (`E_FORBIDDEN`) on
failure: no reason, no retry, and a workspace card that kept claiming "Instance
stopped" because `instance_state` frames were dropped by the socket layer.

- The dialog validates a non-absolute path inline, sends `alias` (the field the
  daemon persists), maps the daemon's status/code/message to a specific sentence
  (`~` expansions, missing directory, roots allowlist, permissions), and keeps
  itself open on a spawn failure with the daemon's reason plus a retry that
  re-sends only `instance.ensure`.
- `lib/ws` handles `instance_state` by invalidating `workspaces`/`health`
  (coalesced at 250 ms across sockets) so the grid reflects reality.
- A never-started workspace renders as "not started · opens on demand" instead
  of "stopped", and a degraded/stopped instance shows `instance.reason`.
This commit is contained in:
yuez committed 2026-10-28 17:32:53 +08:00
1 parent b95ff6055b
commit d5d205a7eb
11 files changed
+673 -21

No files matched your search

+48 -5
View File
@@ -21,22 +21,61 @@ function extractWorkspaceId(result: CommandResult): string | undefined {
);
}
/** Trailing slashes are noise; the daemon stores the canonical path either way. */
function samePath(a: string, b: string): boolean {
const normalize = (value: string) => value.trim().replace(/\/+$/, "");
return normalize(a) === normalize(b);
}
function matchesName(workspace: WorkspaceInfo, name: string): boolean {
return (workspace.name?.trim() || workspace.alias?.trim()) === name;
}
function findCreated(list: WorkspaceInfo[], cwd: string, name: string): WorkspaceInfo | undefined {
const exact = list.find((item) => item.cwd === cwd && (name ? item.name === name : true));
if (exact) return exact;
return list.find((item) => item.cwd === cwd);
// `~` paths never match on cwd (the daemon expands them), so fall back to the
// display name the caller just typed.
const byPathAndName = list.find((item) => samePath(item.cwd, cwd) && (name ? matchesName(item, name) : true));
if (byPathAndName) return byPathAndName;
const byPath = list.find((item) => samePath(item.cwd, cwd));
if (byPath) return byPath;
return name ? list.find((item) => matchesName(item, name)) : undefined;
}
/**
* `workspace.add` succeeded but its instance did not start. The workspace id is
* already known, so the dialog can retry `instance.ensure` without adding twice.
*/
export class InstanceStartError extends Error {
readonly workspaceId: string;
/** Whatever `instance.ensure` threw (usually an `ApiError`). */
readonly detail: unknown;
constructor(workspaceId: string, detail: unknown) {
super(detail instanceof Error ? detail.message : String(detail));
this.name = "InstanceStartError";
this.workspaceId = workspaceId;
this.detail = detail;
}
}
/** `instance.ensure` is idempotent (docs/protocol.md §3), so retrying it is safe. */
export async function ensureInstance(workspaceId: string): Promise<void> {
await api.command({ method: "instance.ensure", params: { workspaceId } });
}
/**
* `workspace.add` → resolve the new workspace id (the command response is not
* required to carry it, so fall back to a fresh GET) → `instance.ensure`.
*
* The daemon owns path expansion: `~/…` and `~` are sent verbatim and resolved
* there, and the display name travels as `alias` (the field it persists).
*/
export async function openWorkspace(input: { cwd: string; name?: string }): Promise<string> {
const cwd = input.cwd.trim();
const name = input.name?.trim() ?? "";
const added = (await api.command({
method: "workspace.add",
params: { cwd, ...(name ? { name } : {}) },
params: { cwd, ...(name ? { alias: name } : {}) },
})) as unknown as CommandResult;
let workspaceId = extractWorkspaceId(added);
@@ -48,7 +87,11 @@ export async function openWorkspace(input: { cwd: string; name?: string }): Prom
throw new Error("workspace.add returned no workspace id");
}
await api.command({ method: "instance.ensure", params: { workspaceId } });
try {
await ensureInstance(workspaceId);
} catch (error) {
throw new InstanceStartError(workspaceId, error);
}
return workspaceId;
}