Skip to content

feat(projects): delete a project's sessions - #27

Merged
dreadster3 merged 8 commits into
mainfrom
feat/delete-projects
Oct 5, 2026
Merged

dreadster3 merged 8 commits into
mainfrom
feat/delete-projects

Conversation

@dreadster3

Copy link
Copy Markdown
Owner

Summary

Adds destructive project deletion to pi web: a "Delete project…" action in the sidebar's project dropdown that removes all of pi's session data for a project — every session transcript, nested pi-subagent transcripts and artifacts, across all of the project's per-cwd session directories (including linked git-worktree cwds), and then the now-empty directories themselves — behind a type-to-confirm dialog.

Your actual code directory is never touched, and neither is anything outside ~/.pi/agent/sessions/ (see "What is deleted" below). Nothing merges, tags, or publishes on your behalf in this PR — the action only affects pi's own session store.

What "a project" means here

pi-web has no stored project entity: a project is the derived grouping of sessions whose cwd/projectRoot resolves to the same key (lib/project-groups.ts). A git repo with linked worktrees collapses into one project (its projectRoot is the main checkout), and its sessions may live in several encoded directories under ~/.pi/agent/sessions/. Deletion therefore enumerates the project's directories from the real session paths — it does not encode anything from the request.

What is deleted / explicitly not

Deleted: every .jsonl of the project's sessions in its per-cwd directories, each session's pi-subagents child tree, its subagent-artifacts/, and the per-cwd project directory itself once empty.

Never touched: the user's code directory, <project>/.pi/** (project-owned config), trust.json decisions, and the hash-keyed global stores (~/.pi/agent/{missions,todos,pi-subagents} — their project-hash key is not exported by the agent SDK, so they are out of scope by design).

Safety model

  • POST /api/projects/delete { projectKey } — request guards in the established order: same-host/origin (403 request-denied), JSON content type (415 content-type), body shape (400), and allowed-roots ancestry for every project cwd.
  • The posted projectKey is only ever compared against workspaceKeyOf(session); it is never joined into a filesystem path. Every removed path derives from enumerated session files and is lstat + realpath containment-verified inside the sessions root before anything is removed — so symlinked project dirs are refused whole (409 symlink), and no traversal through the request can move the deletion set.
  • If any session of the project has a live pi-web wrapper (running or idle-open, detected via isAlive()), the route refuses with 409 session-busy and names the sessions; nothing is deleted.
  • Best-effort cascade with per-item reporting: unexpected non-session files keep their directory (recursive rm refuses it), and any leftover paths come back in failedPaths so a partial result is never shown as a full success.
  • Cache invalidation mirrors per-session delete (path/manager/session-list/project caches), plus the client's clearLastOpen and refresh.

Dialog UX

Red warning listing exactly what is removed — session/dir counts (including nested subagents and worktree cwds) and the project's full path (so two checkouts both named api are distinguishable) — an input that must exactly match the project's display name before Delete enables, Esc to cancel, a specific message when a live session blocks deletion, and a partial-deletion report that stays on screen when any path survived.

Notable decisions

  • POST /{api}/projects/delete rather than DELETE /api/projects: a project isn't a stored entity (the body is a derived-grouping key), and the mutation keeps the guard chain and error shapes of the other mutating routes. Note app/api/worktrees/route.ts does already carry a JSON body on DELETE; the shape here simply follows the project-trust precedent and the key/body semantics.
  • deletedSessions counts nested pi-subagent children (they are genuinely removed with their carrying tree), and only counts a child once its tree's removal actually succeeded.
  • Live wrappers are refused, never killed, differing from per-session delete (which shuts down its wrapper first): for a whole-project wipe, refusing is the conservative choice.
  • Session-file symlinks are unlinked as links (the link is removed, its target file survives), matching per-session delete.
  • No undo/trash — consistent with per-session delete in pi web today.

Testing

  • 16 targeted tests (route.test.mjs: guard order, 404 unknown/traversal keys, busy refusal naming titles incl. an idle-open wrapper, symlink refusal of a project dir + symlinked session file whose target survives, symlinked agent directory (regression-pinned), stray-file/partial reporting, read-only failure isolation, cache invalidation; ProjectDeleteDialog.test.mjs: counts, exact-match rule, path line, refusal copy, partial focus, Esc/portal wiring).
  • Full suite 2428 tests pass; tsc --noEmit clean for app and demo; lint clean for both; i18n parity (same keys + placeholders across en, zh-CN, zh-TW); demo build passes.
  • Demo parity: route/store mirror in demo/mock/, byte-parity locale copies, dialog CSS mirrored, and demo/lib/stacked-dialog.ts added byte-identical to the app's.
  • Verified live against a scratch dev server + temp agent directory: full delete across two cwds with a nested child tree and artifacts, partial-delete reporting, busy refusal rendering, symlink refusal, and dialog focus behavior.

Residual notes

  • A non-pi-web process (e.g. a pi TUI) writing the same transcript concurrently remains undetectable — the pre-existing two-writer caveat of the sessions file format, now also applying to bulk unlink.
  • The busy refusal is conservative: an idle open chat in any of the project's cwds (including worktrees) blocks the delete and names it in the message.
  • Hash-keyed sibling stores are intentionally untouched (see above).

worker added 8 commits October 4, 2026 16:46
Adds POST /api/projects/delete plus the sidebar's type-to-confirm
dialog. Scope: sessions data only.
F1: refuse any alive wrapper, not only a running one.
F2: group against the scanner's non-realpath sessions dir; realpath
both sides of the containment check.
F10: count a nested child only once its carrying tree is gone.
F3: name leftovers and keep the dialog open; the partial key lives.
F5: full project path in the dialog.
F6: onSessionDeleted for every deleted session.
F7: synchronous guard against a double submit.
F8: symlink refusal gets its own copy.
F4: mirror the CSS block into the demo. F12: demo 405.
Capture the project when the dialog opens: the success refresh drops
it from the list, which unmounted the dialog mid-report. Hide the
counts once the delete has run, and freeze the field.
R1: 409 copy says the session is still open (or running).
R2: focus the Close button when the partial report appears.
R3: note why the captured session ids are frozen.
R4: annotate nestedChildren as SessionInfo[].
@dreadster3
dreadster3 force-pushed the main branch 4 times, most recently from 613668d to 5149664 Compare October 5, 2026 02:02
@dreadster3
dreadster3 merged commit 0c03988 into main Oct 5, 2026
4 checks passed
@dreadster3
dreadster3 deleted the feat/delete-projects branch October 5, 2026 03:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant