feat(maestro): nest sessions by live working directory, drop labels

Replace the freeform `metadata.label` session grouping with nesting derived
from each session's working directory. The daemon's activity watcher already
polls each tmux pane every tick, so it now also captures
`#{pane_current_path}` and surfaces it as a new `Session.current_cwd`
(serde-default, back-compat); `cwd` stays the spawn dir for worktree
inference, spawn-similar, and recents.

Web catalog builds the tree from `current_cwd ?? cwd`, collapses the home
dir to `~`, and folds single-child folder chains so `~/git/maestro` reads as
one node. CLI swaps its LABEL column for an abbreviated DIR; the spawn forms
(web + TUI) and `--label` flag are removed.

The pane format stays space-delimited (parser splits the first three tokens
and keeps the remainder as the path) so a cwd containing spaces survives.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-21 21:11:23 +00:00
co-authored by Claude Opus 4.8
parent 5602cf5fc8
commit 458c81094b
23 changed files with 280 additions and 167 deletions
+47 -13
View File
@@ -2,7 +2,7 @@ import { killSession } from "./api.js";
import { HUD_TOGGLE_EVENT, isHudEnabled, toggleHud } from "./debug.js";
import { CatalogStore } from "./events.js";
import type { DaemonInfo, Session } from "./types.js";
import { sessionLabel, sessionName } from "./types.js";
import { abbreviateCwd, sessionCwd, sessionName } from "./types.js";
import { openSpawnModal } from "./spawn.js";
import { createThemeToggle } from "./theme.js";
@@ -29,21 +29,26 @@ function compareNodes(a: TreeNode, b: TreeNode): number {
return an.localeCompare(bn);
}
function buildTree(sessions: Session[], filter: string): TreeNode {
function buildTree(
sessions: Session[],
daemons: Map<string, DaemonInfo>,
filter: string,
): TreeNode {
const root: TreeFolder = { kind: "folder", name: "", path: "", children: [] };
const lc = filter.trim().toLowerCase();
for (const s of sessions) {
const home = daemons.get(s.daemon_id)?.home_dir;
const cwd = sessionCwd(s);
const dir = cwd ? abbreviateCwd(cwd, home) : "";
if (lc) {
const haystack =
`${s.daemon_id} ${sessionName(s)} ${sessionLabel(s) ?? ""}`.toLowerCase();
const haystack = `${s.daemon_id} ${sessionName(s)} ${dir}`.toLowerCase();
if (!haystack.includes(lc)) continue;
}
const label = sessionLabel(s);
// Host is the first level of nesting; the label path nests beneath it, so
// the daemon no longer needs to ride along on each leaf.
const labelParts = label ? label.split("/").filter(Boolean) : [];
const parts = [s.daemon_id, ...labelParts];
// Host is the first level of nesting; the working directory nests beneath
// it, so the daemon no longer needs to ride along on each leaf.
const dirParts = dir ? dir.split("/").filter(Boolean) : [];
const parts = [s.daemon_id, ...dirParts];
let cursor = root;
for (const part of parts) {
const path = cursor.path ? `${cursor.path}/${part}` : part;
@@ -64,9 +69,39 @@ function buildTree(sessions: Session[], filter: string): TreeNode {
for (const c of n.children) if (c.kind === "folder") sortRec(c);
};
sortRec(root);
// Collapse linear directory chains *within* each daemon (but never the daemon
// node itself) so `~/git.naps.pt/naps62/maestro` reads as one compact folder
// rather than four nested ones.
for (const daemon of root.children) {
if (daemon.kind === "folder") {
daemon.children = daemon.children.map((c) =>
c.kind === "folder" ? mergeLinear(c) : c,
);
}
}
return root;
}
/** Fold a folder with a single folder-child into one node (`a` + `b` → `a/b`),
* repeating down the chain, then recurse into the surviving branches. The merged
* node keeps the deepest path so collapse/expand state stays stable as it grows. */
function mergeLinear(folder: TreeFolder): TreeFolder {
let cur = folder;
while (cur.children.length === 1 && cur.children[0].kind === "folder") {
const child = cur.children[0];
cur = {
kind: "folder",
name: `${cur.name}/${child.name}`,
path: child.path,
children: child.children,
};
}
cur.children = cur.children.map((c) =>
c.kind === "folder" ? mergeLinear(c) : c,
);
return cur;
}
function renderNode(node: TreeNode, depth: number): HTMLElement {
if (node.kind === "folder") {
const wrap = document.createElement("div");
@@ -406,7 +441,7 @@ export function renderSidebar(store: CatalogStore): SidebarHandle {
const refresh = () => {
const state = store.snapshot();
const sessions = [...state.sessions.values()];
const tree = buildTree(sessions, filter);
const tree = buildTree(sessions, state.daemons, filter);
treeHost.replaceChildren();
if (sessions.length === 0) {
treeHost.appendChild(
@@ -427,8 +462,8 @@ export function renderSidebar(store: CatalogStore): SidebarHandle {
document.addEventListener("catalog-rerender", rerenderHandler);
// Spawn similar: reopen the spawn modal pre-seeded from a session's host, dir,
// command (matched back to a preset by the form), and label — but with a
// freshly generated name rather than the original's, since it's a new session.
// and command (matched back to a preset by the form) — but with a freshly
// generated name rather than the original's, since it's a new session.
const spawnSimilarHandler = (ev: Event) => {
const s = (ev as CustomEvent<Session>).detail;
const snap = store.snapshot();
@@ -438,7 +473,6 @@ export function renderSidebar(store: CatalogStore): SidebarHandle {
daemon_id: s.daemon_id,
cwd: s.cwd,
command: s.command,
label: sessionLabel(s) ?? "",
},
snap.sessions,
);