diff options
| author | user <user@clank> | 2026-08-12 21:55:12 +0200 |
|---|---|---|
| committer | user <user@clank> | 2026-08-12 21:55:12 +0200 |
| commit | 72441afa75c058e4b1454108d962f2bebaee941b (patch) | |
| tree | d668f1f61d8cbf19127013724db50cf680aa59c9 /cli/src/core/plan.ts | |
| parent | docs: agree the sl0ppi CLI design (diff) | |
| download | sl0ppi-72441afa75c058e4b1454108d962f2bebaee941b.tar.gz sl0ppi-72441afa75c058e4b1454108d962f2bebaee941b.tar.xz sl0ppi-72441afa75c058e4b1454108d962f2bebaee941b.zip | |
cli: the idempotency core (discovery, tab identity, plan diff)
Slice 1 of docs/cli-design.md: the only part with logic worth testing hard.
Pure functions, no subprocesses, so the tests hit them directly.
purpose.ts tab identity as a zellij tab purpose carrying the project's
root-relative path
discover.ts depth-1 scan for .sl0ppi/ markers
plan.ts discovered projects vs list-tabs -> {create, close, keep, foreign}
Identity carries the *path* because `list-panes --json` exposes no cwd (checked
against a live session, not assumed), so there is otherwise no way back from a
tab to a directory on disk. Slashes survive the fork's sanitiser, so nested
paths encode fine.
Two safety properties, both fuzzed over 5000 random names:
- a purpose never exceeds 64 chars and always survives re-sanitising, so what
we generate is what the terminal stores
- projectRelPath() reconstructs the original path exactly or returns null --
never a different path. Wrong answers close the wrong tab; refusing only
leaves a stale one.
The fuzz caught a real bug in the second property. The first implementation
detected lossy encodings by re-encoding and comparing, which silently failed:
`project:<47 a's>.<hash8>` is itself 64 legal characters, so it re-encodes to
itself, the check passed, and a bogus path was "reconstructed" -- which would
have closed a live tab. Lossy purposes now carry a structural marker (a leading
dot, which no discovered path can have) instead of relying on a comparison.
Plus the mount guard from the design: when the ROOT itself is missing, close
nothing. An unmounted filesystem is indistinguishable from mass deletion, and
one of those two readings eats your whole session.
31 unit tests + 2 fuzz properties, 10178 assertions, green.
Diffstat (limited to 'cli/src/core/plan.ts')
| -rw-r--r-- | cli/src/core/plan.ts | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/cli/src/core/plan.ts b/cli/src/core/plan.ts new file mode 100644 index 0000000..7c947d2 --- /dev/null +++ b/cli/src/core/plan.ts @@ -0,0 +1,119 @@ +/** + * The idempotency core. + * + * `sl0ppi up` must be a no-op the second time you run it. Everything else in + * the CLI is plumbing; if this diff is wrong the tool is worse than useless. + * So it is a pure function over data — no filesystem, no subprocesses — and the + * tests hammer it directly. + */ + +import { isProjectPurpose, projectPurpose, projectRelPath } from "./purpose.ts"; + +export interface TabInfo { + /** zellij tab position, which is what `go-to-tab` wants. */ + position: number; + name: string; + purpose?: string; +} + +export interface PlanInput { + /** Absolute path of the root whose session this is. */ + root: string; + /** Whether the root itself is present (see the mount guard below). */ + rootExists: boolean; + /** Discovered projects, as paths relative to the root. */ + projects: string[]; + /** Tabs currently in the session. */ + tabs: TabInfo[]; + /** Injected so the planner stays pure and testable. */ + pathExists: (absPath: string) => boolean; +} + +export interface CreateAction { + relPath: string; + purpose: string; +} + +export interface Plan { + create: CreateAction[]; + close: TabInfo[]; + /** Project tabs that already match a discovered project. */ + keep: TabInfo[]; + /** Tabs sl0ppi does not manage (no `project:` purpose). Never touched. */ + foreign: TabInfo[]; +} + +export function planTabs(input: PlanInput): Plan { + const { root, rootExists, projects, tabs, pathExists } = input; + + // Expected state: one tab per discovered project, keyed by purpose. + const expected = new Map<string, string>(); // purpose -> relPath + for (const relPath of projects) expected.set(projectPurpose(relPath), relPath); + + const create: CreateAction[] = []; + const close: TabInfo[] = []; + const keep: TabInfo[] = []; + const foreign: TabInfo[] = []; + const seen = new Set<string>(); + + for (const tab of tabs) { + if (!isProjectPurpose(tab.purpose)) { + // Someone else's tab (a scratch tab the user opened by hand). Not ours. + foreign.push(tab); + continue; + } + const purpose = tab.purpose as string; + + if (expected.has(purpose)) { + keep.push(tab); + seen.add(purpose); + continue; + } + + // Not a project we discovered. Either its directory is gone, or it is a + // project outside the depth-1 scan that was opened explicitly. Only the + // first justifies closing a tab, and closing is destructive, so every + // uncertain case keeps the tab. + if (!rootExists) { + // The ROOT is missing, not the project. That is an unmounted filesystem, + // not twelve deleted projects: closing here would eat the whole session. + keep.push(tab); + continue; + } + + const relPath = projectRelPath(purpose); + if (relPath === null) { + // Purpose does not reconstruct (truncated/hashed), so we cannot check + // the directory. Refuse to guess. + keep.push(tab); + continue; + } + + if (pathExists(joinPath(root, relPath))) { + // Directory is there; it simply was not discovered (nested, or its + // .sl0ppi/ marker was removed). Leave it alone. + keep.push(tab); + continue; + } + + close.push(tab); + } + + // Alphabetical, per the agreed MVP ordering. + for (const relPath of [...projects].sort()) { + const purpose = projectPurpose(relPath); + if (!seen.has(purpose)) create.push({ relPath, purpose }); + } + + return { create, close, keep, foreign }; +} + +/** Trivial join; avoids pulling node:path into a pure module. */ +function joinPath(root: string, rel: string): string { + return root.endsWith("/") ? `${root}${rel}` : `${root}/${rel}`; +} + +/** True when applying this plan would change nothing. */ +export function isNoOp(plan: Plan): boolean { + return plan.create.length === 0 && plan.close.length === 0; +} |
