A local-first multi-agent app development platform, built on PenguinHarness
feat(core): a bill's vocabulary, for the cleanup a person reviews
lmlihengf107e56907e1
diff --git a/packages/core/src/state/paths.ts b/packages/core/src/state/paths.tsindex db949f0c..83fad2f4 100644--- a/packages/core/src/state/paths.ts+++ b/packages/core/src/state/paths.ts@@ -111,6 +111,42 @@ export function workspacesDir(root: string, projectId: string, agentId: string):return path.join(agentDir(root, projectId, agentId), "workspaces");}+/**+ * `<root>/storage` — where the cleanup machinery keeps its own records: the plans a person+ * reviews (`storage/plans/<planId>.json`). One directory for the whole machine rather than one+ * per Project, because the data root, its disk and its free space are machine-wide facts and a+ * plan that named candidates from two Projects would otherwise be two plans. Created on the+ * first scan; absent on an install nobody has ever scanned.+ */+export function storageDir(root: string): string {+ return path.join(root, "storage");+}++/** `<root>/storage/plans`, one JSON file per scan a person may yet review. */+export function storagePlansDir(root: string): string {+ return path.join(storageDir(root), "plans");+}++/**+ * `<root>/.trash` — where a cleanup moves things instead of deleting them: one directory per+ * run (`<yyyymmdd-HHMMSS>/`), each holding a `manifest.json` and the moved trees under their+ * original relative paths. Dot-prefixed so it reads as machinery rather than as a Project+ * directory, and inside the data root so a restore is a rename on the same filesystem.+ */+export function storageTrashDir(root: string): string {+ return path.join(root, ".trash");+}++/** `<root>/logs` — the append-only records of what the machinery did (see `storageLogFile`). */+export function logsDir(root: string): string {+ return path.join(root, "logs");+}++/** `<root>/logs/storage-gc.jsonl`, one JSON line per cleanup action, appended and never rewritten. */+export function storageLogFile(root: string): string {+ return path.join(logsDir(root), "storage-gc.jsonl");+}+/*** `<agentDir>/scratchpad/<sessionId>`, one Session's private scratchpad directory. The single* Session-scoped storage root shared by every by-product bound to that Session: input imagesdiff --git a/packages/core/src/state/storage.ts b/packages/core/src/state/storage.tsindex b2617e05..4c90a961 100644--- a/packages/core/src/state/storage.ts+++ b/packages/core/src/state/storage.ts@@ -39,6 +39,7 @@*/import fs from "node:fs/promises";import type { Dirent } from "node:fs";+import { createHash } from "node:crypto";import path from "node:path";/** The classes the ledger accounts for. Every byte under the root lands in exactly one. */@@ -629,6 +630,272 @@ export async function scanStorage(};}+/** Whether `target` is `root` itself or something below it. Lexical: both sides are absolute. */+export function isInsideRoot(root: string, target: string): boolean {+ const resolved = path.resolve(target);+ const base = path.resolve(root);+ return resolved === base || resolved.startsWith(base + path.sep);+}++/**+ * Where a planned entry actually is, or null when it may not be touched. Four refusals, in+ * the order the design's guardrails ask for them: a path with `..` or an absolute form is+ * refused lexically before any filesystem call; the target must be a real directory rather+ * than a symlink; it must resolve inside the root; and its realpath must be the path itself,+ * so no directory above it is a link either.+ *+ * The last one is stricter than containment needs and is the version that is easy to state:+ * a tree is moved only from exactly where the bill recorded it. A link anywhere in the chain+ * means the recorded path and the real one disagree, and a person reading the bill was looking+ * at the wrong sentence — a case worth refusing rather than interpreting. A root reached+ * through a symlinked parent (a home on macOS reaches its data through `/var`) is unaffected:+ * both sides of the comparison are under the root's own realpath.+ *+ * Resolution is per call and never cached: what it answers is exactly the thing that can have+ * changed since the scan, and a stale answer here is a move of the wrong thing.+ */+export async function resolveStorageEntry(+ root: string,+ relativePath: string,+): Promise<string | null> {+ if (relativePath === "" || relativePath.startsWith("/") || relativePath.includes("\\")) {+ return null;+ }+ if (relativePath.split("/").some((segment) => segment === "" || segment === "..")) return null;+ const realRoot = await realPathOrResolve(root);+ const target = path.join(realRoot, ...relativePath.split("/"));+ if (!isInsideRoot(realRoot, target)) return null;+ let st;+ try {+ st = await fs.lstat(target);+ } catch {+ return null;+ }+ if (st.isSymbolicLink() || !st.isDirectory()) return null;+ if ((await realPathOrResolve(target)) !== target) return null;+ return target;+}++/**+ * Measures one entry again, the same way the scan measured it, so an approval can be checked+ * against the disk as it is now rather than as the scan found it. Null when the entry is+ * gone — which is drift too, not a silently empty tree.+ */+export async function measureStorageEntry(+ target: string,+): Promise<{ bytes: number; files: number; newestMtimeMs: number } | null> {+ try {+ await fs.lstat(target);+ } catch {+ return null;+ }+ const measured = await measure(target, []);+ return { bytes: measured.bytes, files: measured.files, newestMtimeMs: measured.newestMtimeMs };+}++// ---------------------------------------------------------------------------+// From candidate to executed: the plan a person approves+// ---------------------------------------------------------------------------+/**+ * The classes a cleanup may actually move in this version. One class at a time, on purpose:+ * a temporary Workspace is the entry whose whole premise is that nobody points at it, so a+ * move can be undone by a rename and nothing in a running Session notices. Drafts, Traces and+ * environments each carry a consequence a person must weigh (an image URL in an old message+ * stops resolving, a replay loses its shard, a toolchain has to be installed again), and every+ * one of them deserves its own review rather than riding along with the first switch that+ * happens to exist. Classes outside this list are reported and planned, never applied.+ */+export const EXECUTABLE_STORAGE_CLASSES: readonly StorageClass[] = ["tmp_workspaces"];++export function isExecutableStorageClass(classKey: StorageClass): boolean {+ return EXECUTABLE_STORAGE_CLASSES.includes(classKey);+}++/** Short hex digest, for the fingerprints a plan records. Not a security boundary. */+function hash(input: string): string {+ return createHash("sha256").update(input).digest("hex");+}++/**+ * A temporary Workspace's recorded path: `<project>/agents/<agent>/workspaces/tmp-<suffix>`.+ * Anchored, one path segment per level, so a plan file edited by hand (or written by an older+ * version) cannot name `../..`, a nested path inside a Workspace, or a Workspace that is not+ * temporary.+ */+const TEMP_WORKSPACE_PATH = /^[^/]+\/agents\/[^/]+\/workspaces\/tmp-[^/]+$/;++/**+ * Whether a recorded path is one this class may act on at all — the prefix whitelist of the+ * design's safety rules, applied to the `/`-separated relative paths a plan records. Purely+ * lexical: whether the path escapes the root through a symlink is a filesystem question the+ * server answers with `realpath`, and it asks this first so a plan that was tampered with is+ * refused before anything is stat'd.+ */+export function isPlannablePath(relativePath: string, classKey: StorageClass): boolean {+ if (relativePath === "" || relativePath.startsWith("/") || relativePath.includes("\\")) {+ return false;+ }+ if (relativePath.split("/").some((segment) => segment === "" || segment === "..")) return false;+ switch (classKey) {+ case "tmp_workspaces":+ return TEMP_WORKSPACE_PATH.test(relativePath);+ default:+ // Nothing else is executable yet, so nothing else has a prefix to be checked against —+ // and answering `true` here would be a whitelist that whitelists everything.+ return false;+ }+}++/**+ * How long an approved-but-unexecuted plan stays valid. A day is long enough to review a bill+ * over lunch and short enough that a plan stopped describing the disk a week ago by the time+ * somebody presses the button; the design asks for exactly this bound, after which the answer+ * is a fresh scan rather than a guess.+ */+export const STORAGE_PLAN_TTL_MS = 24 * 60 * 60 * 1000;++/** One line of a bill: a candidate, and everything needed to prove it has not changed since. */+export interface StoragePlanEntry {+ /** Path relative to the root, `/`-separated — what was measured and what a move would name. */+ path: string;+ class: StorageClass;+ bytes: number;+ files: number;+ /** Newest modification inside the entry at scan time, epoch ms (0 = nothing could be stat'd). */+ newestMtimeMs: number;+ rules: CandidateRule[];+ /**+ * The entry as it was measured, as one short hash. Re-measured before a move: a mismatch+ * means the tree grew, shrank or was touched after the person read the bill, and the answer+ * is a new scan — never a partial cleanup of something nobody reviewed.+ */+ fingerprint: string;+}++/**+ * A scan's output, as a file a person reviews: what could go, why, and how big it is. Written+ * by the server, read by the page and the CLI, and **used once** — `appliedAtMs` is the whole+ * one-shot rule, so an approval cannot be replayed against a disk that has moved on.+ */+export interface StoragePlan {+ id: string;+ /** The real path of the root the scan walked. */+ root: string;+ createdAtMs: number;+ expiresAtMs: number;+ /** Hash over the entries, in order — what an approval quotes back to prove it read this one. */+ fingerprint: string;+ /** Sum of `entries`, i.e. what this bill accounts for (not the root's size). */+ totalBytes: number;+ entries: StoragePlanEntry[];+ /** Paths a pin kept out of the bill — why a candidate the report shows is missing from it. */+ excluded: string[];+ /** When an apply ran; a plan is valid until then. */+ appliedAtMs?: number;+ /** The paths that apply actually moved. */+ appliedPaths?: string[];+}++/** The local-time parts of an instant, for the names a person reads in a directory listing. */+function localStamp(nowMs: number): {+ date: string;+ time: string;+ compact: string;+} {+ const at = new Date(nowMs);+ const pad = (n: number): string => String(n).padStart(2, "0");+ const date = `${at.getFullYear()}-${pad(at.getMonth() + 1)}-${pad(at.getDate())}`;+ const time = `${pad(at.getHours())}-${pad(at.getMinutes())}-${pad(at.getSeconds())}`;+ return { date, time, compact: `${date.replace(/-/g, "")}-${time.replace(/-/g, "")}` };+}++/**+ * A plan id: `2026-10-10-13-05-22-4f2a` — the moment of the scan plus a suffix that keeps two+ * apart, in the machine's own local time because this name is read by a person looking at the+ * directory (or at a bill's header) rather than parsed. Ids of the same shape sort by time as+ * strings, which is all the plan list needs.+ */+export function storagePlanId(nowMs: number, suffix: string): string {+ const stamp = localStamp(nowMs);+ return `${stamp.date}-${stamp.time}-${suffix}`;+}++/**+ * A trash entry's id: `20261010-130522`, the moment of the run. Folders, not files, so the+ * compact form reads better in a listing; a collision (two runs inside one second, which the+ * single-writer rule should prevent anyway) is resolved by the caller appending a counter.+ */+export function storageTrashId(nowMs: number): string {+ return localStamp(nowMs).compact;+}++/**+ * One entry's fingerprint: what the bill said about this tree, in one short hash. Size and+ * newest mtime rather than a walk of every file — a plan names hundreds of trees and hashing+ * their contents would cost more than the cleanup saves — and both change when a tree is+ * written to, which is the drift an approval has to notice.+ */+export function planEntryFingerprint(entry: {+ path: string;+ bytes: number;+ newestMtimeMs: number;+}): string {+ return hash(`${entry.path}\n${entry.bytes}\n${Math.round(entry.newestMtimeMs)}`).slice(0, 32);+}++/** The whole bill's fingerprint, over its entries in order. */+export function planFingerprint(entries: readonly StoragePlanEntry[]): string {+ return hash(entries.map((entry) => entry.fingerprint).join("\n")).slice(0, 32);+}++/** The plan's entries this version may act on — what "全选本类" means, said once, in core. */+export function planDefaultSelection(plan: StoragePlan): string[] {+ return plan.entries.filter((entry) => isExecutableStorageClass(entry.class)).map((e) => e.path);+}++/**+ * Turns a ledger's candidates into a bill. Pure: the pins are an argument, the id is an+ * argument, and the clock is an argument — the file it becomes is the server's business.+ *+ * Pinned paths are left out entirely (the design's "pin 过的东西不再出现在任何账单里"), which is+ * why the pin list is applied here rather than filtered out again at each call site: one place+ * decides what a person may be asked about.+ */+export function buildStoragePlan(+ ledger: StorageLedger,+ options: { id: string; nowMs: number; pins?: ReadonlySet<string>; ttlMs?: number },+): StoragePlan {+ const pins = options.pins ?? new Set<string>();+ const excluded: string[] = [];+ const entries: StoragePlanEntry[] = [];+ for (const candidate of ledger.candidates) {+ if (candidate.rules.length === 0) continue;+ if (pins.has(candidate.path)) {+ excluded.push(candidate.path);+ continue;+ }+ entries.push({+ path: candidate.path,+ class: candidate.class,+ bytes: candidate.bytes,+ files: candidate.files,+ newestMtimeMs: candidate.newestMtimeMs,+ rules: [...candidate.rules],+ fingerprint: planEntryFingerprint(candidate),+ });+ }+ return {+ id: options.id,+ root: ledger.root,+ createdAtMs: options.nowMs,+ expiresAtMs: options.nowMs + (options.ttlMs ?? STORAGE_PLAN_TTL_MS),+ fingerprint: planFingerprint(entries),+ totalBytes: entries.reduce((sum, entry) => sum + entry.bytes, 0),+ entries,+ excluded,+ };+}+/** The volume's free/total bytes, or null where the platform cannot answer. */async function diskFacts(target: string,diff --git a/packages/core/test/storage.test.ts b/packages/core/test/storage.test.tsindex 2dfe54a3..78dcc7ac 100644--- a/packages/core/test/storage.test.ts+++ b/packages/core/test/storage.test.ts@@ -20,9 +20,21 @@ import os from "node:os";import path from "node:path";import { afterEach, describe, expect, it } from "vitest";import {+ buildStoragePlan,DEFAULT_STORAGE_POLICY,emptyLiveSet,+ EXECUTABLE_STORAGE_CLASSES,+ isExecutableStorageClass,+ isPlannablePath,+ measureStorageEntry,+ planDefaultSelection,+ planEntryFingerprint,+ planFingerprint,+ resolveStorageEntry,scanStorage,+ storagePlanId,+ storageTrashId,+ STORAGE_PLAN_TTL_MS,type StorageLiveSet,type StoragePolicy,} from "../src/state/storage.js";@@ -374,3 +386,154 @@ describe("scanStorage", () => {expect(ledger.root).toBe(await fs.realpath(root));});});++/**+ * The bill a person reviews, and the two filesystem questions an apply asks before a move:+ * whether a recorded path may be touched at all, and whether it still measures as recorded.+ *+ * These are the properties the design's safety rules are made of, and all of them are pure+ * enough to pin here: the whitelist is a regular expression over the recorded path, the+ * containment rule is `realpath` against the root, and a fingerprint that ignores content is+ * exactly as strong as the drift it has to notice.+ */+describe("storage plan", () => {+ it("keeps the classes it may act on to the one whose move is reversible", () => {+ // Pinning the list, not describing it: widening it is a decision about consequences a+ // person did not get to read (an image URL that stops resolving, a replay with a hole).+ expect([...EXECUTABLE_STORAGE_CLASSES]).toEqual(["tmp_workspaces"]);+ expect(isExecutableStorageClass("tmp_workspaces")).toBe(true);+ expect(isExecutableStorageClass("session_drafts")).toBe(false);+ expect(isExecutableStorageClass("traces")).toBe(false);+ expect(isExecutableStorageClass("shared_env")).toBe(false);+ expect(isExecutableStorageClass("protected")).toBe(false);+ });++ it("only admits the recorded paths of an executable class", () => {+ const ok = "proj/agents/agent/workspaces/tmp-8e8a3ace";+ expect(isPlannablePath(ok, "tmp_workspaces")).toBe(true);+ // A Workspace that is not temporary is user data, whatever else is true of it.+ expect(isPlannablePath("proj/agents/agent/workspaces/mine", "tmp_workspaces")).toBe(false);+ // Nested below one, above one, or outside the layout: none of them is a temporary Workspace.+ expect(isPlannablePath(`${ok}/inner`, "tmp_workspaces")).toBe(false);+ expect(isPlannablePath("proj/agents/agent/workspaces", "tmp_workspaces")).toBe(false);+ expect(isPlannablePath("proj/agents/agent/agent_state", "tmp_workspaces")).toBe(false);+ // Escapes and host-specific forms are refused before any filesystem call.+ expect(isPlannablePath("../outside", "tmp_workspaces")).toBe(false);+ expect(isPlannablePath("proj/agents/agent/workspaces/tmp-x/../tmp-y", "tmp_workspaces")).toBe(+ false,+ );+ expect(isPlannablePath("proj//agents/agent/workspaces/tmp-x", "tmp_workspaces")).toBe(false);+ expect(isPlannablePath("/proj/agents/agent/workspaces/tmp-x", "tmp_workspaces")).toBe(false);+ expect(isPlannablePath("proj\\agents\\agent\\workspaces\\tmp-x", "tmp_workspaces")).toBe(false);+ // Nothing else has a prefix whitelist yet, so nothing else may be planned.+ expect(isPlannablePath("proj/agents/agent/scratchpad/s", "session_drafts")).toBe(false);+ expect(isPlannablePath("web.db", "database")).toBe(false);+ });++ it("resolves a planned path only inside the root, and only to a real directory", async () => {+ const root = await scaffold();+ const workspace = path.join(root, "proj", "agents", "agent", "workspaces", "tmp-one");+ await writeFile(path.join(workspace, "f.md"), "x");+ expect(await resolveStorageEntry(root, "proj/agents/agent/workspaces/tmp-one")).toBe(workspace);++ // A file, a missing path and a symlink are all refused: the first two have nothing to move,+ // and a link is the one way a move could take something from outside the root with it.+ expect(await resolveStorageEntry(root, "web.db")).toBeNull();+ expect(await resolveStorageEntry(root, "proj/agents/agent/workspaces/tmp-gone")).toBeNull();+ await fs.symlink(path.join(root, "proj", "agents"), path.join(root, "link-to-agents"));+ expect(await resolveStorageEntry(root, "link-to-agents/agent/workspaces/tmp-one")).toBeNull();+ // Lexical escapes never reach the filesystem.+ expect(await resolveStorageEntry(root, "../outside")).toBeNull();+ expect(await resolveStorageEntry(root, "/etc")).toBeNull();+ });++ it("follows the root's own realpath but refuses a link out of it", async () => {+ const outside = await tempRoot();+ await writeFile(+ path.join(outside, "proj", "agents", "agent", "workspaces", "tmp-out", "f"),+ "x",+ );+ const root = await scaffold();+ // A directory inside the root that is really somewhere else: `resolveStorageEntry` asks the+ // parent's realpath, so a linked Workspace directory cannot be planned.+ await fs.mkdir(path.join(root, "proj", "agents", "agent", "workspaces"), { recursive: true });+ await fs.symlink(+ path.join(outside, "proj", "agents", "agent", "workspaces", "tmp-out"),+ path.join(root, "proj", "agents", "agent", "workspaces", "tmp-out"),+ );+ expect(await resolveStorageEntry(root, "proj/agents/agent/workspaces/tmp-out")).toBeNull();+ });++ it("builds a bill from the candidates, leaving pinned paths out of it entirely", async () => {+ const root = await scaffold();+ await writeFile(path.join(root, "proj", "agents", "agent", "workspaces", "tmp-a", "f"), "x");+ await writeFile(path.join(root, "proj", "agents", "agent", "scratchpad", "s", "f"), "x");+ const ledger = await scanStorage(root, emptyLiveSet());+ const pinned = "proj/agents/agent/workspaces/tmp-a";++ const plan = buildStoragePlan(ledger, {+ id: "2026-01-01-00-00-00-aaaaaa",+ nowMs: 1_767_225_600_000,+ pins: new Set([pinned]),+ });++ // The draft directory stays on the bill (report-only, but it is what a person should see);+ // the pinned Workspace does not — that is the whole meaning of a pin.+ expect(plan.entries.map((e) => e.path)).toEqual(["proj/agents/agent/scratchpad/s"]);+ expect(plan.excluded).toEqual([pinned]);+ expect(plan.totalBytes).toBe(plan.entries.reduce((sum, entry) => sum + entry.bytes, 0));+ expect(plan.expiresAtMs - plan.createdAtMs).toBe(STORAGE_PLAN_TTL_MS);+ // Only the executable class is selected by "all of them", which is what the button means.+ expect(planDefaultSelection(plan)).toEqual([]);++ // The same ledger without the pin: both candidates are on the bill, and "all of them"+ // selects exactly the temporary Workspace — the draft list stays visible but unselectable.+ const open = buildStoragePlan(ledger, { id: "2026-01-01-00-00-00-bbbbbb", nowMs: 0 });+ expect(open.entries.map((e) => e.path)).toEqual([+ "proj/agents/agent/workspaces/tmp-a",+ "proj/agents/agent/scratchpad/s",+ ]);+ expect(open.excluded).toEqual([]);+ expect(planDefaultSelection(open)).toEqual(["proj/agents/agent/workspaces/tmp-a"]);+ });++ it("fingerprints what a bill said, and notices a tree that changed or vanished", async () => {+ const root = await scaffold();+ const workspace = path.join(root, "proj", "agents", "agent", "workspaces", "tmp-drift");+ await writeFile(path.join(workspace, "f.md"), "one");+ const ledger = await scanStorage(root, emptyLiveSet());+ const plan = buildStoragePlan(ledger, { id: "p", nowMs: Date.now() });+ const entry = plan.entries[0];+ expect(entry).toBeDefined();+ if (entry === undefined) return;++ const fingerprint = planEntryFingerprint(entry);+ expect(fingerprint).toBe(entry.fingerprint);+ expect(plan.fingerprint).toBe(planFingerprint(plan.entries));++ const measured = async (): Promise<string> => {+ const now = await measureStorageEntry(workspace);+ return planEntryFingerprint({+ path: entry.path,+ bytes: now?.bytes ?? 0,+ newestMtimeMs: now?.newestMtimeMs ?? 0,+ });+ };+ expect(await measured()).toBe(entry.fingerprint);++ // A write inside the tree — the drift an approval must not slide past.+ await writeFile(path.join(workspace, "second.md"), "two");+ expect(await measured()).not.toBe(entry.fingerprint);+ // And a tree that is gone measures as nothing at all, rather than as an empty one.+ await fs.rm(workspace, { recursive: true, force: true });+ expect(await measureStorageEntry(workspace)).toBeNull();+ });++ it("names a plan id after the moment of the scan, so ids sort by time", () => {+ const id = storagePlanId(Date.UTC(2026, 9, 10, 13, 5, 22), "4f2a");+ // Local time, so the minutes and seconds are what a clock in this time zone showed; the+ // date part is what makes a directory listing read chronologically.+ expect(id).toMatch(/^2026-10-\d\d-\d\d-\d\d-22-4f2a$/);+ expect(storageTrashId(Date.UTC(2026, 9, 10, 13, 5, 22))).toMatch(/^2026\d{4}-\d{6}$/);+ });+});