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

+73
View File
@@ -0,0 +1,73 @@
/**
* Workspace-noise policy, browser side.
*
* The daemon classifies every row (`visibility: "default" | "secondary"` +
* `visibilityReason`) but never filters the list, so `/workspace/<id>` deep
* links keep resolving. This module only partitions the served rows for
* list/picker surfaces: `primary` is what the user normally wants, `secondary`
* is noise (explicitly hidden, ghosts, temp dirs, …) behind a toggle.
*/
import type { WorkspaceInfo } from "@/lib/protocol";
/** Persisted toggle: "show N hidden" on the workspaces page. */
export const SHOW_ALL_STORAGE_KEY = "pi2-workspaces-show-all";
export interface WorkspacePartition {
primary: WorkspaceInfo[];
secondary: WorkspaceInfo[];
}
/** Legacy daemons omit `visibility`; `hidden` alone still means secondary. */
export function isSecondary(workspace: WorkspaceInfo): boolean {
if (workspace.visibility === "secondary") return true;
return workspace.hidden === true;
}
/** Reason for the badge; `hidden` is inferred for pre-classification daemons. */
export function visibilityReason(workspace: WorkspaceInfo): string | null {
if (typeof workspace.visibilityReason === "string" && workspace.visibilityReason.length > 0) {
return workspace.visibilityReason;
}
return workspace.hidden === true ? "hidden" : null;
}
/** i18n key for the reason badge (or `null` for a default row). */
export function reasonLabelKey(workspace: WorkspaceInfo): string | null {
const reason = visibilityReason(workspace);
return reason === null ? null : `workspaces.reason.${reason}`;
}
/** Split served rows into the normal list and the collapsed noise. */
export function partitionWorkspaces(list: readonly WorkspaceInfo[], _showAll = false): WorkspacePartition {
const primary: WorkspaceInfo[] = [];
const secondary: WorkspaceInfo[] = [];
for (const workspace of list) {
if (isSecondary(workspace)) secondary.push(workspace);
else primary.push(workspace);
}
return { primary, secondary };
}
/** How many rows the "show N hidden" toggle would reveal (0 hides the toggle). */
export function countSecondary(list: readonly WorkspaceInfo[]): number {
return partitionWorkspaces(list).secondary.length;
}
/** `localStorage` read; default off and any failure means off. */
export function readShowAll(): boolean {
try {
return localStorage.getItem(SHOW_ALL_STORAGE_KEY) === "1";
} catch {
return false;
}
}
/** `localStorage` write; storage failures must never break the page. */
export function writeShowAll(showAll: boolean): void {
try {
localStorage.setItem(SHOW_ALL_STORAGE_KEY, showAll ? "1" : "0");
} catch {
// private mode / quota: the in-memory toggle still works
}
}
+18 -1
View File
@@ -10,6 +10,7 @@ import { plural } from "@/lib/i18n";
import type { WorkspaceInfo } from "@/lib/protocol";
import { workspaceName } from "./commands";
import { isSecondary, reasonLabelKey } from "./visibility";
function serverDot(status: ServerIdentity["status"]): DotStatus {
if (status === "online") return "ok";
@@ -78,9 +79,20 @@ export function WorkspaceRow({ workspace }: WorkspaceRowProps) {
const { t } = useTranslation();
const dot = instanceDot(workspace);
const count = Math.max(workspace.liveCount || 0, workspace.sessionCount || 0);
// Secondary rows are dimmed and carry the policy reason; the page decides
// *whether* they render (see features/workspaces/visibility.ts).
const secondary = isSecondary(workspace);
const reasonKey = secondary ? reasonLabelKey(workspace) : null;
return (
<div className="list-row" data-workspace-id={workspace.workspaceId} data-testid="workspace-row">
<div
className={secondary ? "list-row list-row--secondary" : "list-row"}
style={secondary ? { opacity: 0.55 } : undefined}
data-workspace-id={workspace.workspaceId}
data-testid="workspace-row"
data-visibility={secondary ? "secondary" : "default"}
data-visibility-reason={reasonKey ? workspace.visibilityReason ?? "hidden" : undefined}
>
<span className="ico list-row__icon">
<Icon name="folder" />
</span>
@@ -88,6 +100,11 @@ export function WorkspaceRow({ workspace }: WorkspaceRowProps) {
<div className="list-row__name">{workspaceName(workspace, t("workspaces.unnamed"))}</div>
<div className="list-row__cwd mono muted truncate">{workspace.cwd}</div>
</div>
{reasonKey ? (
<Badge className="list-row__count" data-testid="workspace-reason">
{t(reasonKey)}
</Badge>
) : null}
<Badge tone="info" className="list-row__count">
{plural("workspaces.sessionCount", count)}
</Badge>