cccc icon indicating copy to clipboard operation
cccc copied to clipboard

Opt-in reaping of finished Actor workspaces, with a workspace registry and a task `worktree` field

Open chriscoveries opened this issue 1 month ago • 2 comments

Problem

Long-lived Actors do not stay in the directory CCCC launched them in. They, or the runtime they wrap, create a git worktree per task, review or experiment, and when the work ends nothing removes it: stopping an Actor ends its process but does not guarantee any runtime's cleanup. Each worktree is a full checkout, usually with its own build output, so over weeks of unattended operation they fill the disk of whoever runs the daemon.

Cleaning up by hand is safe only after checks that are tedious to repeat and easy to skip under disk pressure: does any Actor still use the tree, is it clean (ignored files too, which git worktree remove deletes without --force), are its commits on a server and not only in a local tracking ref, is a git operation in flight. #139 and #140 documented those steps. This proposes letting the daemon run them: opt-in, dry run first, and only for directories it has a record of.

In #139 we said we were not proposing worktree management, and the answer was that CCCC cannot safely assume ownership of agent-created worktrees. This is narrower than management (CCCC creates nothing, and acts only on a workspace an Actor, a task or a user has explicitly registered), but it is a step past documentation, so we are asking before building anything.

What cccc does today

Line references are to commit a212cb04.

  • An Actor is launched in the attached Scope's root and nowhere else: working_directory resolves default_scope_key or active_scope_key to the Scope url (crates/cccc-daemon/src/ops/actor_runtime.rs:279-305; Scope is {scope_key, url, label, git_remote}, crates/cccc-core/src/group.rs:21-29). The guide says CCCC does not manage worktree cleanup (docs/guide/runtimes.md:692, docs/guide/faq.md:290-292), and v0.4.43 chose guidance "without adding an automatic Git cleanup service" (docs/release/v0.4.43_release_notes.md:222-223).
  • Nothing records the other directories an Actor works in. The nearest record is the per-Actor runtime-session file, which stores only the launch directory as workspace_path, for resume (crates/cccc-daemon/src/ops/runtime_session.rs:97-132, stored under runtime_sessions/<actor>.json, :265-280).
  • A task cannot name its checkout. task.update writes a fixed allow-list (crates/cccc-core/src/context/task_apply.rs:73-90), the MCP cccc_task surface repeats it (crates/cccc-mcp/src/mapping.rs:216-232, :242-258), and the spec lists no such field (docs/standards/CCCC_CONTEXT_OPS_V1.md:149-201).
  • Every Actor process starts with CCCC_HOME, CCCC_GROUP_ID and CCCC_ACTOR_ID (crates/cccc-daemon/src/ops/actor_runtime/environment.rs:26-31). The only code that uses them to find processes is the Group-retirement sweep, which reads /proc/<pid>/environ and calls itself "a one-shot sweep at retirement, not a background reaper" (crates/cccc-daemon/src/ops/actor_runtime/attributed_reap.rs:26, :359-379). A tool outside the daemon that wants to know which Actor owns a directory has to repeat such a scan host-wide: it sees only what its user may read, and only on Linux.
  • The hooks a reaper would plug into exist: a per-Group maintenance tick (maintain_group, tick_group, crates/cccc-daemon/src/ops/automation_runtime.rs:33-99), Group settings with an audit event (group_settings_update, crates/cccc-daemon/src/ops/settings.rs:52-115, event at :103-109), and event kinds that clients must tolerate when unknown (docs/standards/CCCS_V1.md:27).

Proposal

Four parts, each useful alone.

1. Workspace registry. Per Group, beside runtime_sessions/: one record per registered workspace, {path, repo, actor_id, task_id?, source, registered_at, last_seen_at}. Entries come from the Actor's launch directory (recorded, never reapable); a new workspace.register {path, task_id?} op (CLI and MCP) callable by the owning Actor, the foreman or the user; and the task field below. The daemon accepts a path only if it is a linked worktree of a repo whose Scope is attached, so it cannot be pointed at arbitrary directories. A worktree of an attached repo that nobody registered is listed by the dry run as unregistered and never removed; cccc workspace register <path> --actor <id> adopts it. Ownership comes from this record, and "is the owner using it" from daemon state, not from other processes' environments.

2. Task worktree field. A string on task.create, task.update and MCP, with the same permissions as the other fields; setting it registers the workspace for that task. When the task is done or archived the workspace becomes eligible on the next pass without a commit-age wait, since closing the task is the owner saying the work is over. Every safety rule below still applies, and restoring the task holds it again.

3. Settings (Group settings; nothing runs unless set):

key default meaning
workspace_reap off off, dry_run or on
workspace_reap_interval_seconds 900 time between passes
workspace_reap_min_commit_age_hours 24 for a workspace with no task: HEAD's commit date (not mtime) must be older
workspace_reap_emergency_disk_pct, _recover_disk_pct, _emergency_min_commit_age_hours 90, 85, 6 at or above the first (filesystem holding the repo) the interval gap is skipped and the age floor drops to the third; back to normal below the second, so a borderline disk does not flap. No other rule is relaxed
workspace_reap_push_remote origin the git remote that must hold a workspace's commits
workspace_reap_protect [] paths; a path protects itself, what is inside it and what contains it

A .cccc-keep file at a workspace root also protects it and wins over everything, including a closed task.

dry_run plans exactly what on would do and only appends events. The first pass after switching to on is itself a dry run, so there is always a reviewable list before anything is deleted. A pass runs on its own worker with a hard deadline and a cap on removals per pass, and any failure is a hold, never an error that touches delivery. cccc workspace reap [--apply] runs the same planner by hand (dry by default), for cron users and for review.

4. Event workspace.reap. Appended for each workspace removed or, in dry_run, that would be removed: {path, repo, action, reason, actor_id, task_id, bytes, emergency, dry_run}, plus one summary per pass that did anything. Held workspaces and their reasons are returned by the CLI and not ledgered, so a pass over many held workspaces cannot flood the ledger.

Safety rules. A workspace is removed only if all of these hold, each re-checked from scratch immediately before removal. Any doubt is a hold.

  1. It is registered; it is not the Scope root or a main working tree, not bare, and its .git is a file.
  2. It has no .cccc-keep and is not protected.
  3. Its task is closed, or it has no task and is older than the age floor. An open task always holds it.
  4. Its owner is not using it: the owning Actor has no active task, and no process attributed to this Group (the retirement sweep's attribution) has its cwd or an open file under it. The process check is Linux-only; elsewhere the pass also holds while the owner is running.
  5. It is clean: nothing staged, modified or untracked, and ignored entries only from a short regenerable list (build output, dependency directories, caches). Other ignored files, such as local env files, hold it.
  6. Its commits survive: HEAD is contained in a ref of the push remote, confirmed against the live remote right before removal and not only against local tracking refs, which can outlive a deleted remote. Unreachable or unparseable is a hold. A repo with no remotes at all requires HEAD on a surviving local branch or tag.
  7. It is not git worktree locked, has no lock files in its git dir, and holds no other worktree or nested .git (removal would take them along).
  8. Removal is git worktree remove <path> without --force, then git worktree prune, through a fixed allow-list of git subcommands; never a recursive delete. It runs as the directory's owner. A directory the daemon's user does not own is skipped and retried at most daily.

Alternatives considered. Documentation only (#139/#140): correct, but every operator repeats the checks and nothing runs unattended. Runtime exit hooks: Claude Code's sweep skips worktrees made with git worktree add, and stopping an Actor is a signal, not an exit (docs/guide/runtimes.md:694); Actors also outlive their tasks. CCCC creating and owning worktrees: ruled out in #32, and this keeps creation with Actors and users. mtime-only reaping: running services keep touching trees, so mtime never ages. Inferring the owner from directory or branch names: guesswork. An external timer (what we run today): works, but needs the owner and task, which only the daemon has, and is rewritten per deployment.

Out of scope. Creating worktrees; non-git workspaces; build caches outside a registered workspace; rescuing or pushing unpushed work (the rules only refuse); deleting remote branches; any UI beyond the event and the CLI listing.

Questions. (1) If this does not belong in the daemon, would the registry, the task field and the CLI planner be acceptable on their own, with interval-driven passes left to cron? (2) Is "the owner has no active task" the right liveness line for a long-lived Actor, or should a running owner always hold?

We run this today in our own tooling on top of cccc, and would send it as separate PRs in that order (registry and task field; planner and CLI; interval pass and emergency tier) if the direction suits.

chriscoveries avatar Oct 22 '26 11:10 chriscoveries

Thanks for raising this before building it. The explicit registration, dry-run mode, and conservative safety checks are carefully considered.

We’re keeping the existing boundary: CCCC will not automatically remove agent-created worktrees. Registration establishes an association, but not exclusive ownership. A completed task and Group-attributed process checks cannot establish that another Group, editor, or external process no longer needs the directory.

Please keep the cleanup registry, planner, and scheduling policy in the external tool that manages these workspaces. Moving only the timer to cron would still leave CCCC responsible for the deletion decision, so that split does not resolve the ownership concern.

We also don’t want to introduce a registry or task field solely as groundwork for a future reaper. If your external tool lacks specific read-only CCCC information, please identify that gap separately before implementing anything.

I’d suggest closing this proposal for now. Thank you for documenting the operational problem and considering the risks carefully.

ChesterRa avatar Oct 23 '26 07:10 ChesterRa

Thanks, agreed. Workspace cleanup stays in our own tooling, which owns those workspaces, so I'm closing this. If that tool needs specific read-only information from CCCC, we'll raise that gap separately.

chriscoveries avatar Oct 23 '26 12:10 chriscoveries