feat(daemon): collapse workspace noise into a "show hidden" section

Discovery returns every pi session directory ever seen, so the console opened on
47 rows on this machine: 20 ghosts (cwd deleted) and 21 scratch dirs under /tmp
next to the real projects. Every row now carries a derived, non-persisted
`visibility`/`visibilityReason`; nothing is filtered server-side, so
`/workspace/<id>` deep links and the chat page's id lookup still resolve.

Precedence: explicit `hidden` wins; a live instance, `pinned` or `trusted`
rescues a row; then temp paths (`PI2_WORKSPACE_HIDE_TEMP`, on), ghosts
(`PI2_WORKSPACE_HIDE_GHOSTS`, on), outside roots (`PI2_WORKSPACE_HIDE_OUTSIDE_ROOTS`,
off) and staleness (`PI2_WORKSPACE_STALE_DAYS`, off). Measured on real data:
47 → 26 default rows, 0 openable rows lost.

The browser collapses the secondary rows behind a persisted toggle and dims them
with a reason badge; `workspace.add` now clears `hidden`.
This commit is contained in:
yuez committed 2026-10-23 10:08:15 +08:00
1 parent 58e247644d
commit 64a1a98c1f
12 files changed
+888 -38

No files matched your search

+49
View File
@@ -128,6 +128,55 @@ describe("workspace store", () => {
expect(store.get(bId)?.hidden).toBe(true);
});
it("keeps secondary rows in list() and get() so deep links still resolve", async () => {
// The sandbox lives under $TMPDIR, so temp-hiding is switched off here to
// isolate the hidden/ghost rules (and to exercise the env injection).
const { store } = setup({ env: { PI2_WORKSPACE_HIDE_TEMP: "0" } });
const bId = workspaceIdForCwd(projectB);
await store.update(bId, { hidden: true });
const ghostId = workspaceIdForCwd(projectGhost);
const all = store.list();
expect(all).toHaveLength(3);
expect(all.map((workspace) => workspace.workspaceId).sort()).toEqual(
[workspaceIdForCwd(projectA), bId, ghostId].sort(),
);
expect(store.get(bId)).toMatchObject({ hidden: true, visibility: "secondary", visibilityReason: "hidden" });
expect(store.get(ghostId)).toMatchObject({
exists: false,
visibility: "secondary",
visibilityReason: "ghost",
});
expect(store.get(workspaceIdForCwd(projectA))).toMatchObject({ visibility: "default" });
expect(store.get(workspaceIdForCwd(projectA))).not.toHaveProperty("visibilityReason");
});
it("classifies sandbox paths under $TMPDIR as temp when the policy is on", () => {
const { store } = setup({ env: {} });
expect(store.get(workspaceIdForCwd(projectA))).toMatchObject({
visibility: "secondary",
visibilityReason: "temp",
});
});
it("add() clears hidden on a previously hidden workspace", async () => {
const { store, registry } = setup({ env: { PI2_WORKSPACE_HIDE_TEMP: "0" } });
const info = await store.add({ cwd: projectA });
await store.update(info.workspaceId, { hidden: true });
expect(store.get(info.workspaceId)).toMatchObject({
hidden: true,
visibility: "secondary",
visibilityReason: "hidden",
});
const reopened = await store.add({ cwd: projectA });
expect(reopened).toMatchObject({ hidden: false, visibility: "default" });
expect(reopened).not.toHaveProperty("visibilityReason");
expect(registry.get(info.workspaceId)?.entry.hidden).toBe(false);
expect(registry.list()).toHaveLength(1);
});
it("adds existing, readable, writable directories inside the allowlist", async () => {
const { store, registry } = setup();
const info = await store.add({ cwd: projectA, alias: " Alpha ", trust: true });
+254
View File
@@ -0,0 +1,254 @@
import { mkdirSync } from "node:fs";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import type { InstanceState } from "../../src/protocol/types.js";
import {
classifyWorkspace,
isTempPath,
parseBooleanEnv,
parseNonNegativeInt,
tempRoots,
type ClassifyWorkspaceInput,
} from "../../src/workspaces/index.js";
import { cleanup, makeSandbox } from "./helpers.js";
const DAY = 86_400_000;
const NOW = Date.UTC(2026, 9, 15, 12, 0, 0);
function instanceState(workspaceId: string): InstanceState {
return {
workspaceId,
instanceId: "i-1",
status: "ready",
nvimPid: 4242,
pluginVersion: "1.8.0",
hasUI: true,
attachedClients: 1,
listenAddr: null,
startedAt: NOW,
};
}
describe("classifyWorkspace", () => {
// Synthetic, non-existent roots: the test must not depend on the host's
// $HOME or on the mkdtemp sandbox (which itself lives under /tmp).
const home = "/pi2-test-home";
const other = "/pi2-test-other";
const third = "/pi2-test-third";
let sandbox: string;
let temp: string;
beforeEach(() => {
sandbox = makeSandbox("pi2-daemon-visibility-");
temp = join(sandbox, "temp");
mkdirSync(temp, { recursive: true });
});
afterEach(() => cleanup(sandbox));
/** Hermetic base row: no env leakage from the host, roots pinned to `home`. */
function row(overrides: Partial<ClassifyWorkspaceInput> = {}): ClassifyWorkspaceInput {
return {
cwd: join(home, "code", "alpha"),
exists: true,
lastActiveAt: NOW - DAY,
roots: [home],
env: { TMPDIR: temp },
now: NOW,
...overrides,
};
}
it("defaults to a visible row with no reason", () => {
expect(classifyWorkspace(row())).toEqual({ visibility: "default" });
});
it("precedence 1: hidden wins over every rescue", () => {
const classified = classifyWorkspace(
row({
hidden: true,
pinned: true,
trusted: true,
liveCount: 2,
instance: instanceState("ws-1"),
exists: false,
cwd: join(temp, "scratch"),
}),
);
expect(classified).toEqual({ visibility: "secondary", visibilityReason: "hidden" });
});
it("precedence 2: a live instance, pin or trust rescues temp/ghost/stale rows", () => {
const tempGhostStale = {
cwd: join(temp, "scratch", "deep"),
exists: false,
lastActiveAt: NOW - 365 * DAY,
env: { TMPDIR: temp, PI2_WORKSPACE_STALE_DAYS: "7" },
};
expect(classifyWorkspace(row(tempGhostStale))).toMatchObject({
visibility: "secondary",
visibilityReason: "temp",
});
for (const rescue of [
{ instance: instanceState("ws-1") },
{ liveCount: 1 },
{ pinned: true },
{ trusted: true },
]) {
expect(classifyWorkspace(row({ ...tempGhostStale, ...rescue })), JSON.stringify(rescue)).toEqual({
visibility: "default",
});
}
});
it("precedence 3: temp paths hide by default, including trailing subpaths", () => {
expect(classifyWorkspace(row({ cwd: temp }))).toEqual({ visibility: "secondary", visibilityReason: "temp" });
expect(classifyWorkspace(row({ cwd: join(temp, "a", "b", "c") }))).toEqual({
visibility: "secondary",
visibilityReason: "temp",
});
// Built-in roots work without TMPDIR.
expect(classifyWorkspace(row({ cwd: "/tmp/pi2-not-created", env: {} }))).toEqual({
visibility: "secondary",
visibilityReason: "temp",
});
expect(classifyWorkspace(row({ env: { TMPDIR: temp, PI2_WORKSPACE_HIDE_TEMP: "0" } }))).toEqual({
visibility: "default",
});
});
it("precedence 4: ghosts hide by default and beat outside-roots/stale", () => {
const elsewhere = join(other, "gone");
expect(classifyWorkspace(row({ cwd: elsewhere, exists: false }))).toEqual({
visibility: "secondary",
visibilityReason: "ghost",
});
expect(
classifyWorkspace(
row({
cwd: elsewhere,
exists: false,
lastActiveAt: NOW - 400 * DAY,
env: { TMPDIR: temp, PI2_WORKSPACE_HIDE_OUTSIDE_ROOTS: "1", PI2_WORKSPACE_STALE_DAYS: "30" },
}),
),
).toEqual({ visibility: "secondary", visibilityReason: "ghost" });
expect(
classifyWorkspace(row({ exists: false, env: { TMPDIR: temp, PI2_WORKSPACE_HIDE_GHOSTS: "0" } })),
).toEqual({ visibility: "default" });
});
it("precedence 5: outside-roots is opt-in and parses PI2_WORKSPACE_ROOTS as a list", () => {
const outside = row({ cwd: join(other, "project"), roots: [home], env: { TMPDIR: temp } });
// Default is 0 → still visible.
expect(classifyWorkspace(outside)).toEqual({ visibility: "default" });
expect(classifyWorkspace({ ...outside, env: { TMPDIR: temp, PI2_WORKSPACE_HIDE_OUTSIDE_ROOTS: "1" } })).toEqual(
{ visibility: "secondary", visibilityReason: "outside-roots" },
);
// Both roots from the env list are honoured; the third path is outside.
const roots = `${home}, ${other}`;
const env = { TMPDIR: temp, PI2_WORKSPACE_HIDE_OUTSIDE_ROOTS: "1", PI2_WORKSPACE_ROOTS: roots };
expect(classifyWorkspace(row({ cwd: join(home, "a"), roots: undefined, env }))).toEqual({
visibility: "default",
});
expect(classifyWorkspace(row({ cwd: join(other, "b"), roots: undefined, env }))).toEqual({
visibility: "default",
});
expect(classifyWorkspace(row({ cwd: join(third, "c"), roots: undefined, env }))).toEqual({
visibility: "secondary",
visibilityReason: "outside-roots",
});
});
it("precedence 6: stale is opt-in and never applies to lastActiveAt === null", () => {
const old = row({ lastActiveAt: NOW - 31 * DAY, env: { TMPDIR: temp } });
// Default 0 = disabled.
expect(classifyWorkspace(old)).toEqual({ visibility: "default" });
expect(classifyWorkspace({ ...old, env: { TMPDIR: temp, PI2_WORKSPACE_STALE_DAYS: "30" } })).toEqual({
visibility: "secondary",
visibilityReason: "stale",
});
const fresh = row({ lastActiveAt: NOW - 29 * DAY, env: { TMPDIR: temp, PI2_WORKSPACE_STALE_DAYS: "30" } });
expect(classifyWorkspace(fresh)).toEqual({ visibility: "default" });
expect(
classifyWorkspace(
row({ lastActiveAt: null, env: { TMPDIR: temp, PI2_WORKSPACE_STALE_DAYS: "1" } }),
),
).toEqual({ visibility: "default" });
});
it("never throws on garbage env values", () => {
const garbage = {
PI2_WORKSPACE_HIDE_TEMP: "maybe",
PI2_WORKSPACE_HIDE_GHOSTS: " ",
PI2_WORKSPACE_HIDE_OUTSIDE_ROOTS: "2",
PI2_WORKSPACE_STALE_DAYS: "-3",
};
// Garbage booleans fall back to the defaults (temp+ghost on, roots off);
// garbage stale-days falls back to 0 (off).
expect(classifyWorkspace(row({ cwd: join(temp, "x"), env: garbage }))).toEqual({
visibility: "secondary",
visibilityReason: "temp",
});
expect(classifyWorkspace(row({ exists: false, env: garbage }))).toEqual({
visibility: "secondary",
visibilityReason: "ghost",
});
expect(classifyWorkspace(row({ cwd: join(other, "x"), lastActiveAt: null, env: garbage }))).toEqual({
visibility: "default",
});
// Unknown cwd / missing roots must not throw.
expect(classifyWorkspace({ cwd: "", exists: true, lastActiveAt: null, env: garbage })).toEqual({
visibility: "default",
});
});
it("parseBooleanEnv accepts 1/true/yes/on and 0/false/no/off, else the fallback", () => {
for (const raw of ["1", "true", "TRUE", "yes", "on", " on "]) {
expect(parseBooleanEnv(raw, false), raw).toBe(true);
}
for (const raw of ["0", "false", "False", "no", "off", " off "]) {
expect(parseBooleanEnv(raw, true), raw).toBe(false);
}
for (const raw of [undefined, "", "maybe", "2", "enabled"]) {
expect(parseBooleanEnv(raw, true), String(raw)).toBe(true);
expect(parseBooleanEnv(raw, false), String(raw)).toBe(false);
}
});
it("parseNonNegativeInt keeps positive integers and falls back to 0", () => {
expect(parseNonNegativeInt("30")).toBe(30);
expect(parseNonNegativeInt(" 7 ")).toBe(7);
expect(parseNonNegativeInt("0")).toBe(0);
for (const raw of [undefined, "", "-1", "1.5", "abc", "1e3", "999999999999999999999"]) {
expect(parseNonNegativeInt(raw), String(raw)).toBe(0);
}
});
it("tempRoots includes $TMPDIR plus the platform defaults and isTempPath matches subpaths", () => {
const roots = tempRoots({ TMPDIR: temp });
expect(roots).toContain(temp);
expect(roots).toContain("/tmp");
expect(new Set(roots).size).toBe(roots.length);
expect(isTempPath(join(temp, "x", "y"), { TMPDIR: temp })).toBe(true);
expect(isTempPath(temp, { TMPDIR: temp })).toBe(true);
expect(isTempPath(join(home, "code"), { TMPDIR: temp })).toBe(false);
expect(isTempPath("", { TMPDIR: temp })).toBe(false);
});
it("hides a temp ghost as temp (temp precedes ghost)", () => {
expect(classifyWorkspace(row({ cwd: join(temp, "gone"), exists: false }))).toEqual({
visibility: "secondary",
visibilityReason: "temp",
});
});
it("classifies rows with an unknown workspaceId-shaped input safely", () => {
// Registry-only rows can carry an empty cwd; no path rule may fire.
expect(classifyWorkspace(row({ cwd: "", exists: false, env: { PI2_WORKSPACE_HIDE_OUTSIDE_ROOTS: "1" } }))).toEqual(
{ visibility: "secondary", visibilityReason: "ghost" },
);
});
});