Skip to content

Clean Up After Recipe Runs

Recipe steps run in the named checkout and its task or PR worktree. The runner does not create or release a private execution repository or review launch folder. Interrupted runs retain uncommitted and unpushed work, branches, and worktree registration. Runtime files and recoverable repository data are separate; see execution and retention.

Legacy private execution/review directories may contain recoverable work. Removal of their ownership and release machinery does not authorize sweeping those directories. Inspect and preserve their contents before any operator cleanup.

Use this guide to find what a recipe run leaves on disk, to choose where it goes, and to remove or archive it with amplihack cleanup.

Every amplihack recipe run writes into one work root. At the end of each run the runner removes the run's temporary folder and sweeps locally eligible run-owned items. Use amplihack cleanup --apply to remove eligible worktrees that need a pull-request lookup. The command can also archive old logs and remove temporary folders left by killed runs.

Cleanup keeps a folder that holds unsaved Git work it can detect. A folder with a Git work tree that has uncommitted changes, or commits that are not on its upstream, is kept, and cleanup says so once per pass on stderr. The check has limits: it does not see a stash, other branches or ignored files. See Folders with unsaved work are kept and What the check does not cover.

Contents

Quick start

See what cleanup would do. A dry run writes nothing, not even the work root:

amplihack cleanup

Remove what is eligible and archive logs older than 30 days:

amplihack cleanup --apply

Remove only stale temporary folders, without touching logs:

amplihack cleanup --apply --only run-tmp --only tmp-leftover

No configuration is needed for the end-of-run cleanup. It runs after every amplihack recipe run unless AMPLIHACK_CLEANUP_ON_EXIT=0 is set. A recipe run --dry-run still removes its temporary amplihack-dry-* folder (RunWorkspace::dry_runtime) but never runs the sweep. A dry run creates no runs/ folder, so there is nothing else to remove.

A folder that holds unsaved Git work is left in place by every one of these commands, with one line on stderr naming it:

amplihack: leaving "/home/you/.amplihack/work/r/3f0c9a526d1e" in place: work tree "/home/you/.amplihack/work/r/3f0c9a526d1e/tmp/amplihack-rs": has unpushed commits

To clear such a folder, save or discard the work, then run cleanup again. See Free a kept folder.

Settings

Each setting can be given as a command-line flag (where one exists), an environment variable, or left at its default. A flag overrides the variable, and the variable overrides the default. There are no amplihack config keys for these settings.

Variable Flag Default Meaning
AMPLIHACK_WORK_ROOT none $HOME/.amplihack/work Folder that holds run folders, targets, clones, the archive and, when set explicitly, worktrees.
AMPLIHACK_CLEANUP_KEEP_HOURS --keep-hours N 24 How long run-owned worktrees and targets are kept after a run's PR closes or the run ends, and how old untagged targets and system temporary leftovers must be. Does not apply to run-tmp. Maximum 87600.
AMPLIHACK_LOG_ARCHIVE_DAYS --log-days N 30 Recipe logs and session logs whose modification time is older than this are archived. Maximum 36500.
AMPLIHACK_LOG_ARCHIVE_DIR none <work root>/archive Where log archives and MANIFEST.jsonl are written.
AMPLIHACK_CLEANUP_ON_EXIT none 1 Set to 0 to skip the end-of-run sweep. The run's runtime/ folder is still removed.
AMPLIHACK_CLEANUP_PRIVATE_TARGET_DIR none unset Set to exactly 1 by the launcher to state that the folder named by CARGO_TARGET_DIR is private to this run, so the run removes it when it ends. Any other value, including an empty one, means the folder is not private. The setting carries no path. See Remove a run's own build output.
AMPLIHACK_TARGET_LINK_ROOTS none unset Extra colon-separated absolute folders where linked targets may point. Not needed for links made by the host cargo wrapper. See Linked targets.

Recipe logs stay in recipe.log_dir (default ~/.amplihack/recipe-logs). The work root does not change where they are written.

The work root must be an absolute path. It is refused if it is /, your home folder, an ancestor of your home folder, a system folder such as /usr or /etc, the system temporary folder, or the root of a git checkout. If the folder already exists it must be owned by you and must not be writable by group or others. New folders under the work root are created with mode 0700.

Where a run writes

Each amplihack recipe run invocation has a run id, a UUID created once when the command starts. The run folder is named after it, and steps see it as AMPLIHACK_RUN_ID.

The run id is not the same as the attempt id. The runner retries an attempt that fails with a transient fault, such as a 529 response or a reset connection, and gives each attempt its own UUID. The attempt id is what AMPLIHACK_RECIPE_RUN_ID holds and what run_id in the JSON result of amplihack recipe run --format json reports, as in earlier releases. A run with no retry has one attempt; run.json lists every attempt id of the run under attempts[].correlation_id.

The runner creates one folder per run:

~/.amplihack/work/
├── runs/
│   └── 3f0c9a52-6d1e-4b8a-9f57-2c41d0e7a6b3/
│       ├── run.json              # the run report (kept)
│       ├── owned-paths           # worktrees and targets this run created
│       ├── cleanup-record.json   # written only when cleanup removed something
│       ├── recovery/             # private completions and required artifact copies
│       ├── procs/                # one record per running attempt, for recipe stop (Linux)
│       └── runtime/              # removed when the run ends
│           ├── tmp/              # TMPDIR, TMP and TEMP for every step
│           ├── context/          # 0700; holds the run's 0600 context file
│           └── artifacts/        # AMPLIHACK_WORKFLOW_ARTIFACT_DIR
├── targets/                      # one cargo target per checkout, from git-hook-cargo.sh
├── clones/                       # repositories cloned by the launcher inside a run (never swept)
├── worktrees/                    # only when AMPLIHACK_WORK_ROOT is set
├── archive/                      # log archives and MANIFEST.jsonl
└── .cleanup.lock

Every step of the run sees these variables:

Variable Value
AMPLIHACK_WORK_ROOT The resolved work root.
AMPLIHACK_WORK_ROOT_EXPLICIT 1 or 0; see below. The worktree helpers read this, so a work root that the runner exported by default does not move worktrees.
AMPLIHACK_RUN_ID The run id.
AMPLIHACK_RECIPE_RUN_ID The id of the current attempt (unchanged from earlier releases).
AMPLIHACK_RUN_DIR <work root>/runs/<run id>
AMPLIHACK_WORKFLOW_RUNTIME_DIR <run dir>/runtime
AMPLIHACK_RUNTIME_ROOT <run dir>/runtime
AMPLIHACK_WORKFLOW_ARTIFACT_DIR <run dir>/runtime/artifacts
TMPDIR, TMP, TEMP <run dir>/runtime/tmp

AMPLIHACK_WORK_ROOT_EXPLICIT is set as follows:

  • If it is already set in the environment that starts the run, the runner passes it through unchanged. A nested run therefore keeps its parent's 0, even though the parent exported AMPLIHACK_WORK_ROOT.
  • Otherwise it is 1 when AMPLIHACK_WORK_ROOT is set in that environment, and 0 when it is not.

A sub-recipe started by a step gets its own run folder, its own run id and its own temporary folder.

Trusted delegated executions also attach to the enclosing recovery tree. Their completion records are published through the root runner's writer; their own temporary folders are not recovery authority.

If <run dir>/runtime/tmp would be longer than 80 bytes, which can happen with a long home path, the runtime folder is placed at <work root>/r/<first 12 hex digits of the run id> instead. This leaves room under the 108-byte limit on Unix socket paths for tools that create sockets in TMPDIR.

The run report

run.json is written when the run starts and finalised when it ends. It is mode 0600. It holds only status data, never environment variables, prompts or step output:

{
  "schema_version": 1,
  "run_id": "3f0c9a52-6d1e-4b8a-9f57-2c41d0e7a6b3",
  "recipe": "default-workflow",
  "pid": 418822,
  "pid_start_ticks": 91833412,
  "cwd": "/home/you/src/amplihack-rs",
  "started_at": "2026-11-01T09:12:44Z",
  "ended_at": "2026-11-01T10:03:19Z",
  "outcome": "success",
  "attempts": [
    { "correlation_id": "b81d47e0-2a9c-4f13-8e65-7c0f19d3a2e4" },
    { "correlation_id": "e4a2c6f1-58b7-4d0e-9a31-6f2b8c7d0e95" }
  ],
  "steps": [{ "id": "step-01-prepare-workspace", "status": "completed" }],
  "log_path": "/home/you/.amplihack/recipe-logs/e4a2c6f1-58b7-4d0e-9a31-6f2b8c7d0e95.log"
}

This run had one transient retry, so it lists two attempt ids; each is an independent UUID. log_path is the log of the last attempt.

A build that delivered nothing also has reason, one line saying why it failed, and undelivered, its branch, worktree, last turn, unpushed commits and staged, unstaged and untracked files. A build that found a pull request whose state could not be read has both fields as well, with its outcome unchanged and undelivered.delivered null. Read the fields, not the words of reason. Other runs have neither field. See A build that delivered nothing.

outcome is success, failure or interrupted. A run that was killed before it could finalise the report keeps ended_at empty and reads as interrupted. For cleanup, such a run is dated by the modification time of run.json, which the runner updates at the start of each attempt and each step.

To find the run folder from the JSON result of amplihack recipe run, which reports the attempt id, search the run reports for that attempt:

ATTEMPT_ID="$(jq -r '.run_id' result.json)"
WORK_ROOT="${AMPLIHACK_WORK_ROOT:-$HOME/.amplihack/work}"
grep -l "\"$ATTEMPT_ID\"" "$WORK_ROOT"/runs/*/run.json | xargs -r dirname
# /home/you/.amplihack/work/runs/3f0c9a52-6d1e-4b8a-9f57-2c41d0e7a6b3

Run folders themselves are never removed. After cleanup a folder retains run.json, cleanup-record.json and, while some recorded path remains, owned-paths. These summary files take a few kilobytes. The recovery feature will retain additional recovery/ contents, which can be much larger. Pruning old run folders is not part of this feature.

Recording owned paths

Cleanup removes a worktree or target only if the run that created it recorded it in owned-paths. The bundled helpers do this for you:

  • workflow_worktree_root.sh and the recipes that create worktrees through it (workflow-worktree.yaml, consensus-issue-worktree.yaml and autodrive_pr_worktree.sh) call amplihack_run_owned.sh record after a new git worktree add.
  • When the same helpers reuse an existing worktree, they call amplihack_run_owned.sh adopt. It records the worktree for the current run only if an earlier run already recorded it. The worktree is then listed by every run that used it, and cleanup keeps it while any of those runs is active. A worktree you made by hand is never recorded, even when a run reuses it.
  • scripts/git-hook-cargo.sh records the target it uses, whether it built it first (created) or found it already there (reused), together with the checkout it builds.

A worktree line has three tab-separated fields, kind, path and branch. A target line written by the current hook has five: kind, path, an empty branch, the origin (created or reused) and the checkout path:

worktree    /home/you/.amplihack/work/worktrees/amplihack-rs-1a2b3c4d/feat/issue-2039   feat/issue-2039
target  /home/you/.amplihack/work/targets/amplihack-precommit-target-5e6f7a8b9c0d       created /home/you/.amplihack/work/worktrees/amplihack-rs-1a2b3c4d/feat/issue-2039

Cleanup also accepts the older forms: a line with two fields, or three with an empty branch. Such a legacy target line comes from a hook older than

2039, names a target with a -run-<run id> suffix, and is handled by the

rules in When a run is eligible for cleanup.

Any other line is dropped when cleanup reads the file: four fields, six or more, an origin other than created or reused, a relative checkout path, a five-field line whose kind is not target or whose branch is not empty, and any line with a control character. Cleanup never repairs a line. It keeps valid lines byte for byte when it rewrites the file.

The five-field format and the conditions under which the hook writes nothing are listed in Recording the target for a run.

When a worktree has no recorded branch, cleanup uses its checked-out branch. A detached worktree with no recorded branch follows the no-PR rule in When a run is eligible for cleanup.

A custom step that creates a worktree can record it the same way:

"$AMPLIHACK_HOME/amplifier-bundle/tools/amplihack_run_owned.sh" record \
  worktree "$WORKTREE_PATH" "$BRANCH" \
  || echo "warning: could not record $WORKTREE_PATH for cleanup" >&2

A worktree created without recording it is never removed by cleanup. That is the safe outcome; remove it yourself with git worktree remove when you no longer need it.

Worktree location

By default, worktrees stay where they were: <main checkout>/worktrees/<branch>.

When AMPLIHACK_WORK_ROOT is set in the environment that starts the run, worktrees go to <work root>/worktrees/<repository name>-<8-character hash of the main checkout path>/<branch> instead. The hash keeps two checkouts with the same name apart. Inside a run the helpers decide this from AMPLIHACK_WORK_ROOT_EXPLICIT, not from whether AMPLIHACK_WORK_ROOT is set. To print the base folder that the helpers will use:

"$AMPLIHACK_HOME/amplifier-bundle/tools/workflow_worktree_root.sh" base
# with AMPLIHACK_WORK_ROOT_EXPLICIT=1:
# /home/you/.amplihack/work/worktrees/amplihack-rs-1a2b3c4d
# with AMPLIHACK_WORK_ROOT_EXPLICIT=0 or unset:
# /home/you/src/amplihack-rs/worktrees

workflow_worktree_root.sh base is the only source of this folder. These helpers use it:

  • workflow-worktree.yaml (used by default-workflow);
  • autodrive_pr_worktree.sh;
  • consensus-issue-worktree.yaml, unless the caller passes worktree_dir, which still wins;
  • workflow_branch_name.sh, so that a re-run of a task that names its branch recognises its own worktree under the work root and reuses it.

None of them adds a fallback of its own. When the work root is set explicitly but cannot be used, for example because AMPLIHACK_WORK_ROOT is a relative path, base prints the reason and the step stops with cannot work out where worktrees go. It does not create the worktree under <main checkout>/worktrees/, the place you asked to avoid.

One exception remains. When the branch a run continues is checked out in the caller's own checkout, default-workflow step 4 creates an alias worktree, <branch>--wt-<n>, and that always goes under <main checkout>/worktrees/, because the #1762 planner and workflow_alias_reuse.sh look for it there. Alias worktrees are not recorded for the run, and cleanup does not remove them. Remove them with git worktree remove when you no longer need them.

The default work root ($HOME/.amplihack/work) applies to run folders, targets, clones and the archive whether or not the variable is set. Only worktrees need the variable set explicitly.

Preserve or retire recovery state

Durable recovery storage and --fresh are supported. Recovery retirement remains a separate operation; existing cleanup categories do not retire recovery history or its latest-attempt markers.

Keep <run dir>/recovery/ when you need a future launch to recover after a usage limit. The identity index under the work root selects only the latest attempt. Keep that metadata as well; moving a run directory alone does not establish a matching recovery identity.

The run-tmp category and normal end-of-run runtime removal exclude durable recovery state. Required temporary artifacts are copied into private recovery storage before completion is published, so runtime cleanup does not erase replayable outputs. An output that refers to an unavailable, uncopied artifact is invalidated on relaunch.

Use --fresh on the next recipe run to bypass history without deleting it. Do not delete a latest marker to reach an older usage-limit run. Retention must preserve a latest-attempt tombstone when detailed history is retired, so a later failure or success continues to exclude older history. Deleting the latest attempt's detail means future recovery falls back fresh if that detail is needed.

Retirement tooling is unresolved. Existing amplihack cleanup categories cover runtime files, worktrees, targets, and logs; they do not provide a coordinated recovery-retirement operation. No supported command or manual deletion procedure is specified for retiring sensitive recovery records. Deleting directories or markers manually cannot meet the concurrency contract. The planned --fresh flag only bypasses replay; it does not remove records.

A supported retirement interface must coordinate with admission and imports, exclude active owners and in-progress imports, and durably preserve the latest-attempt sequence/tombstone before deleting detailed records. Interrupted retirement must not expose older history as eligible. Its selection/reporting contract must account for sensitive inherited copies in later attempts: retiring an ancestor does not erase those copies. Recovery records never authorize deleting external dependencies or worktrees. The command, locking protocol, and handling of interrupted retirement must be specified and implemented before publishing operational retirement instructions.

Recovery copies can retain allowed prompts, typed context, outputs, and artifact contents after their runtime originals disappear. They are private sensitive data: directories are 0700, regular files 0600. They are not included in a summary-only run.json, and log archiving does not retire them. Retention policy must cover these durable copies as well as logs, but enforcing that policy requires the unresolved retirement interface. Private permissions do not provide encryption.

For launch behavior, see Resume a recipe after a usage limit. For publication and retention invariants, see private recovery storage.

What happens when a run ends

When amplihack recipe run finishes, whether the run succeeded, failed or panicked, the runner does these steps in order. Each step runs even when an earlier one failed, and the final error names every failure.

  1. Finishes the recovery admission with an outcome judged from the run.
  2. Finishes the workspace (finish_workspace):
  3. Removes <run dir>/runtime/, including everything steps wrote to TMPDIR and artifacts/, unless a running process still uses it, running processes cannot be seen on this system, or it holds unsaved Git work. Durable recovery/ state and copied artifacts remain outside that disposable folder. See A runtime folder still in use is kept, Where running processes cannot be seen, Locked folders inside the runtime folder and Folders with unsaved work are kept. A folder that was replaced by another one is kept and the run fails; see A replaced runtime folder is refused.
  4. Finalises run.json with the end time, outcome and step summary.
  5. Removes the run's own build output: the private CARGO_TARGET_DIR and target/ in each linked worktree the run owns or was started in. This happens after the report is written, after a success or a failure, and does not depend on AMPLIHACK_CLEANUP_ON_EXIT. See Remove a run's own build output.
  6. Unless AMPLIHACK_CLEANUP_ON_EXIT=0 is set, sweeps the whole work root for run-owned items that are now eligible (the categories run-tmp, run-worktree and run-target), and removes them. This includes items from earlier runs, and from the current run if it is already eligible. The sweep makes no GitHub calls. It keeps worktrees and legacy targets that need a pull-request answer for amplihack cleanup --apply. When the sweep removes a worktree, that worktree's target goes with it in the same sweep, whether the run recorded it as created or reused; see When a target is removed.

There is no execution-copy release step. The selected checkout and retained task or PR worktree remain available for recovery; legacy execution and review directories are not swept by this finalization.

AMPLIHACK_CLEANUP_ON_EXIT=0 skips only the sweep in step 2.4. Everything before it always runs, including the removal of build output.

A run that fails before its first step, for example on an unborn HEAD, still finishes its recovery admission as Failure, so the next launch does not try to resume it.

The sweep is best effort. If another cleanup holds the lock, the sweep is skipped. It stops after 120 seconds. Any problem is printed as a warning and never changes the run's exit status. Untagged targets, log archiving and scans of the system temporary folder are left to amplihack cleanup, so a run never removes the pre-commit hook caches of your other checkouts.

When the sweep removes something from a run, it adds an entry to that run's cleanup-record.json:

{
  "run_id": "3f0c9a52-6d1e-4b8a-9f57-2c41d0e7a6b3",
  "sweeps": [
    {
      "cleaned_at": "2026-11-02T11:20:05Z",
      "removed": [
        {
          "kind": "run-worktree",
          "path": "/home/you/.amplihack/work/worktrees/amplihack-rs-1a2b3c4d/feat/issue-1808",
          "size_bytes": 1843200
        },
        {
          "kind": "run-target",
          "path": "/home/you/.amplihack/work/targets/amplihack-precommit-target-5e6f7a8b9c0d",
          "size_bytes": 4294967296
        }
      ],
      "skipped": []
    }
  ]
}

If nothing was removed, no record is written.

A real run starts its workspace with RunWorkspace::begin_durable. If the work root cannot be used, for example because it is group-writable, the run fails before any step starts, with an error that names the work root. Fix its permissions (chmod g-w,o-w) or point AMPLIHACK_WORK_ROOT at a private absolute directory. A real run never falls back to a temporary folder.

The error names the work root and the operation that failed, for example opening the work root or creating the run's runtime folder. The run opens every component of the work root without following symbolic links, so a work root that passes through a link is unusable, as it has always been. When a link is on the path, the error also names the first component that is a symbolic link, but not the place it points to. Name the folder by its canonical path (realpath "$AMPLIHACK_WORK_ROOT") to avoid the link. On macOS, /var and /tmp are links to /private/var and /private/tmp, so /var/tmp is reached through one; a path under any of them goes through a link there. A --dry-run uses RunWorkspace::dry_runtime: a temporary amplihack-dry-* folder that is removed on exit, with no run folder and no checkout.

A runtime folder still in use is kept

A step can start a process that outlives the run, such as a build server, a language server or a background job a test left behind. If the runner removed runtime/ under such a process, the process, and any agent that inherited its TMPDIR, would be left pointing at a folder that no longer exists.

So before step 2.1 removes runtime/, the runner checks the process table once. The folder is in use if any running process other than the runner itself:

  • has its working folder inside it;
  • holds a file open inside it; or
  • has TMPDIR, TMP or TEMP set to a path inside it.

These are the same checks the sweep and amplihack cleanup make for run-tmp. If the folder is in use, the runner leaves it in place, does not change its permissions or move it, and prints one line on stderr:

amplihack: runtime folder "~/.amplihack/work/r/<id>" is still in use by a running process; leaving it in place — the end-of-run cleanup sweep will remove it later

The real message shows the full absolute path of the runtime folder in double quotes: <work root>/runs/<run id>/runtime, or <work root>/r/<short id> when that path would be too long. The run's exit status does not change. The message names only the folder, never the process or its environment.

The kept folder is an ordinary run-tmp candidate. Once the run has ended it is no longer active, so the next sweep, from a later run or from amplihack cleanup --apply, removes it after checking again that no process uses it. A process that runs for a long time keeps the folder until a sweep runs after that process exits. To remove it now, stop the process and run:

amplihack cleanup --apply --only run-tmp

Where running processes cannot be seen

The check needs /proc. A system without /proc, as macOS is, gives the runner no view of running processes, so it cannot tell whether runtime/ is still in use. It then keeps the folder, changes nothing in it, and prints one line on stderr:

amplihack: running processes cannot be seen on this system, so whether runtime folder "~/.amplihack/work/r/<id>" is still in use is not known; leaving it in place. Cleanup will not remove it either: delete it by hand once nothing started by the run is still running

The run's exit status does not change. Before issue #2935 the runner removed the folder in this case.

The sweep and amplihack cleanup --apply have the same limit: they keep every folder they cannot prove unused, so on such a system neither removes a kept runtime/ folder later. These folders stay until they are deleted by hand. See the troubleshooting entry for how to check that nothing still uses a folder before deleting it.

What this costs, and how to list the kept folders

On such a system every run leaves its runtime/ folder behind, and nothing removes it until the system gets a view of running processes (issue #2936). The folder holds what the run's steps wrote to TMPDIR and to artifacts/, so its size is whatever they left there; there is no upper limit.

Sizes measured on one macOS host on 22 November 2026, over the 14 runtime folders it held, each read with du -sk while its run was in progress:

Runtime folders Size of each
8 of the 14 under 1 MiB (0 to 660 KiB)
5, each an auto-drive run, whose launcher copies its recipe bundle into TMPDIR 12.4 MiB
1, the largest 42.2 MiB

So a run that writes little to TMPDIR leaves well under 1 MiB, and an auto-drive run about 12 MiB. A step that builds in TMPDIR can leave far more than any figure above. At 12.4 MiB a run, a host that makes 50 auto-drive runs a day keeps about 620 MiB a day.

The dry run lists them. It removes nothing:

amplihack cleanup --only run-tmp
CATEGORY        ACTION                             SIZE  PATH
run-tmp         skipped                        12.4 MiB  /Users/you/.amplihack/work/r/3f0c9a526d1e
                reason: running processes cannot be seen on this system
run-tmp         skipped                               -  /Users/you/.amplihack/work/r/7b1e44c0a2d9
                reason: run is active

A folder with the reason running processes cannot be seen on this system belongs to a run that has ended, or was killed, and is waiting to be deleted by hand. Its size is shown in the dry run only: --apply, and the sweep at the end of each run, report the same reason without reading the folder. A folder with run is active belongs to a run that is still going; leave it alone. The reason in use by a running process appears only where processes can be seen, so it is never printed for these folders.

To see only the kept folders, or their total size:

amplihack cleanup --only run-tmp | grep -B1 'cannot be seen'
amplihack cleanup --only run-tmp --format json \
  | jq '[.entries[] | select(.reason | contains("cannot be seen")) | .size_bytes] | add'

The folder of a recipe run --dry-run is not checked: it lives in the system temporary folder, no sweep ever looks at it, and keeping it would leave it there for good. It is removed at the end of the dry run.

Locked folders inside the runtime folder

A step can leave a folder under TMPDIR that its owner cannot write to or search, for example a test fixture left at mode 000 or 0500. Removing the contents of such a folder fails with Permission denied (os error 13).

So once the runner has found that no process uses runtime/, and before it removes it, the runner walks the folder from its root and gives the owner back read, write and search access (u+rwx) on every folder the run owns. It adds only those three owner bits: group and other bits stay as they were, and no bit is ever removed. File modes are never changed, because removing a file depends on the mode of the folder that holds it, not on the file's own mode.

The walk does not follow symbolic links. A link inside runtime/ is removed as a link, and the folder it points to keeps its mode and its contents.

The walk leaves alone, and does not enter, any entry that:

  • belongs to another user; or
  • is on another filesystem than the runtime folder, such as a bind mount.

It names each such entry in a warning on stderr and leaves its permissions unchanged:

amplihack: warning: leaving permissions unchanged on "/home/you/.amplihack/work/r/3f0c9a526d1e/tmp/fixture" in the runtime folder: not owned by this user or on another filesystem

At most 20 entries are named on stderr; any further ones are counted in one last line, amplihack: warning: … and 37 more entries left unchanged. Every path is quoted, so a name with a newline or control characters in it is shown escaped. The full list is written to the debug log only, which is off by default. To see every path, run with RUST_LOG=amplihack_cli=debug (or RUST_LOG=debug); the entries then appear on stderr.

These warnings never change the run's exit status. The runner then checks runtime/ for unsaved work, after the access was restored so that a locked folder cannot hide a work tree from the check, and removes it as usual if it holds none. A folder kept for unsaved work keeps the owner access that was just restored; no bit is taken away again.

If an entry the walk left alone stops the removal, the folder stays in place and the run fails with release owned runtime directory and the underlying error. A later sweep tries again, but it fails for as long as the entry still belongs to another user or a folder in it is still locked, so fix the folder by hand as shown below.

This applies to the runner's own removal of runtime/ at the end of a run. The sweep and amplihack cleanup do not change permissions. When the run-tmp folder itself, a folder directly inside it, or a folder two levels inside it is locked, the check for unsaved work cannot open it, so the folder is kept as skipped with cannot check for unpushed commits: … and amplihack cleanup still exits 0. A locked folder three or more levels inside the run-tmp folder is not looked at by that check; it still stops the removal, the run-tmp folder is reported as failed and kept, and amplihack cleanup exits with a non-zero status. Give the owner access back on the folders only, then run the sweep again. Point find at the runtime folder itself, which is <work root>/runs/<run id>/runtime or, when that path is too long, <work root>/r/<short id>:

RUNTIME="$HOME/.amplihack/work/r/3f0c9a526d1e"   # or .../runs/<run id>/runtime
find "$RUNTIME" -type d -exec chmod u+rwx {} +
amplihack cleanup --apply --only run-tmp

The -type d test leaves file modes unchanged, as the runner does. Avoid chmod -R u+rwx, which also makes every file executable. Entries owned by another user need that user, or root, to change or remove them.

Unverifiable historical records instead cause a logged fresh execution after secure admission of the new attempt.

A replaced runtime folder is refused

While a run lives, the runner holds its runtime folder open. When the run starts, it opens the folder with O_DIRECTORY | O_NOFOLLOW and records the device and inode number of the open folder. A symbolic link or a file at that path fails to open, and the run does not start. If the folder at the path changes between the check and the open, the run also does not start.

Because the folder stays open, the filesystem cannot free its inode number and give it to another folder. On ext4, a new folder made at the same path would otherwise often get the number that was just freed and look like the original.

Before each step, the runner compares the folder at the path with the one it holds:

Folder at the path What the runner does
The same folder Launches the step.
Missing Creates it again with mode 0700, opens and records the new folder, then launches the step.
A different folder, a symbolic link or a file Refuses the step and fails the run.
A new folder that cannot be opened Refuses the step. The old record stays in place, so at the end of the run the new folder is kept and the run fails.

A refused step prints:

amplihack: this run's runtime folder cannot be used: "/home/you/.amplihack/work/r/3f0c9a526d1e" is no longer this run's runtime folder; refusing to launch the step

When the run ends, the runner checks the folder again in the same way while it still holds it open. It removes only the folder it holds open. If the runner created the folder again during the run, that new folder is the one it holds and the one it removes. Any other folder at the path is kept, and the run fails with:

runtime resource identity changed; replacement retained at /home/you/.amplihack/work/r/3f0c9a526d1e

See step 2.1 in What happens when a run ends.

The open folder does not count as "in use" when the run ends, because the runner skips its own process. Step processes do not inherit it, because it is opened close-on-exec. Another amplihack process's sweep sees the open folder and keeps it, which is correct while the run lives.

Remove a run's own build output

Build output holds no unsaved work, so a run removes the build output it made for itself when it ends, whether it succeeded or failed. This step runs after run.json is written and before the sweep, and does not depend on AMPLIHACK_CLEANUP_ON_EXIT. It removes:

  • the folder named by CARGO_TARGET_DIR, when the launcher states that the folder is private to the run;
  • target/ at the root of each worktree listed in the run's owned-paths;
  • target/ at the root of the folder the run was started in, when that folder is a linked worktree and the run is not nested inside another run.
  • each Cargo target folder inside the run's own folder (<work root>/runs/<id>/), such as the build goal's build-goal/build.*/cargo-target. Only a folder that holds Cargo's CACHEDIR.TAG counts. The search goes at most four levels down, follows no link, stays on one device, and does not enter a tagged folder, a folder holding .git or the run's runtime/. The rest of the run folder, such as the build goal's identity files, turns and logs, stays.

A main checkout is never touched, and only the target folder at a worktree root is considered, not those of nested crates.

A linked worktree's target/ is removed at the end of every run, and there is no setting that turns this off. If you run recipes from a linked worktree, the next build there starts from scratch. Removing a large target delays the end of the run until it is done. The removal has no deadline and no retry. A partial removal is harmless, because build output can be rebuilt.

Say that a target folder is private

The launcher states privacy; amplihack never decides it from a path. Set AMPLIHACK_CLEANUP_PRIVATE_TARGET_DIR=1 next to CARGO_TARGET_DIR for the run:

CARGO_TARGET_DIR="$AMPLIHACK_WORK_ROOT/targets/issue-2886" \
AMPLIHACK_CLEANUP_PRIVATE_TARGET_DIR=1 \
  amplihack recipe run default-workflow -c task_description="Fix the parser"

Only the value 1 (spaces around it are ignored) counts. The setting claims that the whole folder belongs to this run alone. It is an assertion made by the launcher and is not checked, so do not set it for a folder that several runs or checkouts share. The runner clears the setting in the environment it gives to steps, in a normal run and in a --dry-run, so a recipe started from a step never inherits the claim. With --detach the launcher's setting is passed to the detached pane, because the claim has to survive into it.

What a run prints

Each removal is one line on stderr naming the folder and the space freed. When two or more folders were removed, a last line gives the total. Standard output is unchanged, so --format json stays parseable.

amplihack: removed this run's build output "<work root>/targets/issue-2886" (12.4 GiB)
amplihack: removed this run's build output "<work root>/worktrees/<repository>-<hash>/<branch>/target" (6.1 GiB)
amplihack: end-of-run cleanup removed 2 build output folders, freeing 18.5 GiB in total

When the size of a folder cannot be measured, its line says size unknown in place of the size and the folder is still removed. The total line then counts only the known sizes and reads at least, for example amplihack: end-of-run cleanup removed 2 build output folders, freeing at least 12.4 GiB in total (size unknown for one or more of them).

A folder named twice, for example as the private CARGO_TARGET_DIR and as a worktree's target/, is removed and reported once. Paths in these lines are quoted, and a reason that contains a control character is printed with the character escaped, so one problem is always one line.

A worktree that holds unsaved work is kept. The build-output step prints one amplihack: leaving line for it. The sweep that follows prints no second line for the same worktree only while the run that just ended is still inside the keep period. With AMPLIHACK_CLEANUP_KEEP_HOURS=0 the sweep may print a second line. See Folders with unsaved work are kept. Its target/ is still removed. The worktree itself is never removed by this step.

A folder that is skipped or cannot be removed

A removal that is refused or fails prints one warning and never changes the run's result or exit status:

amplihack: warning: end-of-run cleanup could not remove "<work root>/targets/issue-2886": in use: .cargo-lock is held

The reason after the colon is the wording of whichever check applied, so it varies. in use: .cargo-lock is held, not a cargo target and owned by another user are the same words the sweep uses for the same findings.

Every folder passes the same checks, whichever setting or file named it. It is removed only when all of these hold. Otherwise it is skipped with a reason:

  • the path is absolute and resolves to a real folder, and the folder itself is not a symbolic link;
  • you own it, and its parent is not writable by other users unless the parent has the sticky bit;
  • it has a parent folder, so / is refused, and it is neither the folder HOME names nor an ancestor of it, and neither the run's folder, its work root nor its launch folder nor an ancestor of any of them. The home folder is read from HOME alone; when HOME is unset or empty there is no home folder to protect and the other checks still apply;
  • it is on the same device as its parent, and so are its immediate subfolders, so a mount point at the folder or directly inside it is refused;
  • it has no .git at its root;
  • it is empty (no entries at all) or carries the Cargo marker (CACHEDIR.TAG or .rustc_info.json), so a folder holding only unrelated files is rejected. A run that never builds with Cargo, for example in a repository with no Rust code, leaves its claimed folder empty, and that folder is removed;
  • an empty folder is removed only while it is still empty, never recursively. When an entry appears in it after the checks, the removal is declined with one warning and the folder and the new entry are kept;
  • no build holds the .cargo-lock of the folder or of a folder directly inside it. Each lock is tried once, with no waiting and no retry;
  • it is still the same folder (same device and inode) immediately before the removal, and links inside it are never followed.

These checks have limits. A volume mounted more than one level inside the folder is not detected. A folder that carries a Cargo marker next to unrelated files, and has no .git, is still removed. An active build is protected only by the single .cargo-lock attempt, and only where Cargo has written one. This is why the claim must be accurate.

A CARGO_TARGET_DIR that is unset or does not exist is skipped silently.

An empty claimed folder that was kept

An empty claimed folder has been removed at the end of a run since v1.2.194, the first release tag that contains #2906. Releases up to v1.2.193 do not contain build_output.rs, so they have no end-of-run removal of a claimed folder at all. To check which case applies to a host, run amplihack --version with the binary the launcher used.

If an empty claimed folder is still there after a run on v1.2.194 or later, and no warning names it, the end-of-run step did not consider it. The code allows three ways for that to happen. They are possible causes, not established ones: the empty folders reported in #3263 did not reproduce, and their cause is still open in #3274.

  • The run was nested. Launch::from_env in build_output.rs treats a run as nested when AMPLIHACK_RUN_ID is set and not empty, and a nested run ignores the claim (see Nested runs).
  • AMPLIHACK_CLEANUP_PRIVATE_TARGET_DIR was not 1 in the environment of the amplihack recipe run process. private_target_dir_claimed in cleanup/mod.rs trims surrounding whitespace and then requires exactly 1.
  • The run never reached its end. The step is called from run_dir.rs only after the run's report is written, so a run killed with SIGKILL, or one whose process died before then, skips it.

A claimed folder that the step did consider and rejected is always named in a warning, with the reason.

Entries in owned-paths are checked

The owned-paths file lives in the run's folder, which steps can write, so its entries are not trusted. An entry is used only when it is an absolute path to a linked worktree: a folder whose .git is a regular file starting with gitdir:. A main checkout, a path that does not resolve and an entry that fails the checks above are skipped, with a warning where the entry looked like a worktree. Only the target/ at the worktree root is ever removed.

Nested runs

A recipe started from a step of another run is a nested run. A run is nested when AMPLIHACK_RUN_ID is set and not empty in its environment. It removes target/ only from worktrees in its own owned-paths, and leaves the inherited CARGO_TARGET_DIR and the launch folder's target/ alone.

This protection has a limit. A worktree that a nested run reuses is recorded in that run's owned-paths as well, so when a sub-recipe reuses its parent's worktree, the sub-recipe removes that worktree's target/ when it ends, while the parent may still be building there. The parent's next build starts from scratch. A run started with amplihack recipe run --detach is treated as its own outermost run, because the detached pane starts without AMPLIHACK_RUN_ID. The launcher's AMPLIHACK_CLEANUP_PRIVATE_TARGET_DIR setting is kept for the pane, and the pane's own runner then clears it for its steps.

When a run is eligible for cleanup

Cleanup never touches an active run. A run is active when the process id recorded in run.json is alive and its start time matches the recorded pid_start_ticks. The start time check stops a reused process id from making a dead run look alive. If /proc cannot be read, the run is treated as active.

Cleanup keeps the worktrees of a run that ended within the keep period (AMPLIHACK_CLEANUP_KEEP_HOURS, default 24) without a GitHub lookup. For older or interrupted runs, a worktree is eligible in either of these cases:

  • It has closed or merged pull requests and none are open. Cleanup uses each pull request's closing time, falling back to its merge time, and keeps the worktree until the keep period has passed since the latest of those times.
  • It has no pull request or is detached with no recorded branch. At least the keep period must have passed since the later of ended_at and the modification time of run.json. If ended_at is missing or unparseable, cleanup uses the modification time alone.

In report and apply mode, amplihack cleanup matches pull requests by exact head branch name. It reads GH_REPO before the checkout's gh repo set-default selection. Use GH_REPO=github.com/owner/repo or set GH_HOST=github.com alongside a bare GH_REPO=owner/repo. Without either selection, all remotes that could supply a PR repository must identify the same public GitHub repository. An unrecognized fetch URL, such as a GitHub Enterprise upstream, leaves the PR repository unresolved. SSH and SCP-style fetch URLs also leave it unresolved because SSH configuration can redirect the literal host. A local fetch URL can also be ambiguous if the remote has a nonlocal push URL. A saved selection for a dotted remote name is also ambiguous because GitHub CLI reads the part before the first dot as the remote name. Checkouts of the same repository share REST evidence.

Cleanup reads all PR states in ascending creation order from GET /repos/{owner}/{repo}/pulls. Closing or reopening a PR leaves its position in this listing unchanged. Cleanup requests pages of 100, with a limit of 40 requests per repository and a 30-second timeout per request. If the listing advertises a history beyond this limit and there is room for two requests, cleanup requires a complete single page of open PRs followed by one page of all PR states ordered by most recent update. That second page must reach the keep-period cutoff or finish the listing. A close or reopen between the requests appears in the most recent updates. When sufficient evidence covers open PRs and recent closures, old run activity can authorize removal. A recently touched run.json still requires a matching older closure for removal. If that closure falls outside the fetched pages, cleanup keeps the worktree and reports that the bounded listing found no matching closure. A complete history would make that worktree eligible.

Cleanup keeps dependent items and reports why if the repository is ambiguous or the required pages are incomplete. A continuation must contain results and link back to the previous page of the same listing. Cleanup keeps dependent items if a continuation lacks that link, even when the returned page is short.

These rules decide worktrees, and targets recorded in the legacy format. A run's legacy targets are removed only when every worktree the run still has is eligible. If a recorded worktree could not be checked, the targets are kept. When none of the run's worktrees is left, the targets follow the no-PR rule above. Targets recorded in the five-field format follow When a target is removed instead, and the keep period does not apply to them.

A run is skipped, with a reason in the output, when:

  • the keep period has not yet passed;
  • its pull request is still open;
  • a needed pull-request listing is incomplete or unusable;
  • run.json has a schema_version newer than this release understands;
  • its owned-paths file exists but cannot be read (reported as owned-paths unreadable: ...). Untagged targets are then also skipped, since that run may own them.

With the default keep period, manual cleanup keeps an older run's worktree if its PR closed one hour ago. If the PR closed 25 hours ago, the worktree is eligible for removal.

Folders with unsaved work are kept

A step can leave a repository copy in its temporary folder, with a commit it never pushed or a file it never committed. Before cleanup removes a folder, it judges whether the folder holds a Git work tree with work that exists nowhere else. If it does, cleanup keeps the folder, says so once on stderr, and carries on. If it does not, the folder is removed as described in the rest of this guide.

Keeping a folder is a skip, never a failure. It does not change the exit status of amplihack cleanup, and it never ends or fails a run. There is no flag and no environment variable that turns the check off.

What counts as unsaved work

The check reports the first of these that applies to a work tree, in this order:

Reason Meaning
has uncommitted changes git status lists a modified, staged or untracked file. Ignored files are not listed. A git status that fails or times out is reported as cannot check for unpushed commits instead, so this reason always means changes Git actually saw.
has unpushed commits The checked-out branch has an upstream, and HEAD has commits that are not on it.
has no upstream and commits on no remote branch The checked-out branch has no upstream, and HEAD has commits that no remote-tracking branch contains.
cannot check for unpushed commits: <detail> Cleanup could not tell. Not knowing is a reason to keep, never to delete.

These are the reasons the run-worktree category gives, with one difference: run-worktree has only "remove it or keep it" to settle, so it counts a git status it could not run as uncommitted changes, whereas this check says a line and so names what it met. Either way the folder is kept. The check reads only local state. It runs no fetch and contacts no remote, and it runs Git with the same hardening as the rest of cleanup.

Which removals are checked

Removal When the folder is judged
run-tmp When cleanup plans it, and again just before removal.
The runner's own removal of runtime/ at the end of a run After the process check and the owner-access restore, just before removal.
run-target recorded in the legacy format as a plain folder When cleanup plans it, and again just before removal.
untagged-target Just before removal.
tmp-leftover Just before removal.

These are not checked:

  • run-worktree. It already requires no uncommitted changes and no unpushed commits; see Safety rules.
  • Targets recorded in the five-field format, and linked targets. They hold build output, not user work, and keep their .cargo-lock checks.
  • recipe-log and session-log. They are archived, not deleted.
  • Clones under <work root>/clones/. They are never swept.

A dry run judges what it plans, so amplihack cleanup lists a run-tmp that holds unsaved work as skipped. It judges untagged-target and tmp-leftover folders only at removal, so a dry run can list one as would-remove and amplihack cleanup --apply then keeps it. Trust --apply for the outcome.

How cleanup finds a work tree

Cleanup looks for a .git entry, a folder or a file, in the folder itself, in a folder directly inside it, and in a folder two levels inside it. That finds a repository copy a step left under the run's runtime folder, such as <run dir>/runtime/tmp/amplihack-rs/.git. A repository three or more levels down is not found.

  • No .git there. The folder is removed as described in the rest of this guide. No Git command is run.
  • Symbolic links. Cleanup never follows one, and a candidate folder that is itself a link is not judged, because the removal refuses links anyway.
  • Each .git must belong to its own folder. Cleanup runs git rev-parse --show-toplevel in the folder that holds the .git and requires the answer to be that same folder, compared byte for byte. A folder that only sits inside some enclosing repository, with no .git of its own, is not judged by that repository and is removed as usual. Without this rule, a run folder under a work root inside a repository would never be removed.
  • Several work trees in one folder. Cleanup judges each one and stops at the first that holds unsaved work. It reports that one.
  • A .git that is not a repository. A .git folder with no HEAD in it is ignored, whatever else it holds, and the folder is removed as usual. Nothing inside it is read beyond the presence of HEAD.

Anything cleanup cannot settle is kept, with cannot check for unpushed commits: …:

  • Git cannot be run, or does not answer within 60 seconds.
  • A .git that is a file or a link, or a folder with a HEAD, which Git refuses or places somewhere else: for example a .git file that says gitdir: /nonexistent, a corrupt repository, or a repository Git calls unsafe because of its owner.
  • The folder or its .git belongs to another user. Cleanup does not run Git in it.
  • The folder itself, a folder directly inside it, or a folder two levels inside it cannot be opened, for example because it is mode 000.
  • A path cannot be resolved.
  • One folder holds more than eight work trees. The detail is more work trees than can be checked. Only the .git entries described above count: an ignored .git folder with no HEAD and a candidate that is a symbolic link do not. The cap is reached while the work trees are being found, before any of them is judged, so more than eight keeps the folder without judging any of them.

A folder kept for one of these reasons stays until a person fixes the repository or removes the folder by hand. Cleanup does not retry, wait for an age to pass, or offer an override.

What cleanup prints

When cleanup keeps a folder it prints one amplihack: leaving line on stderr. The line names the folder and the reason. It names the work tree as well when the work tree is not the folder itself:

amplihack: leaving "/home/you/.amplihack/work/r/3f0c9a526d1e/tmp/fixture" in place: has uncommitted changes
amplihack: leaving "/home/you/.amplihack/work/r/3f0c9a526d1e" in place: work tree "/home/you/.amplihack/work/r/3f0c9a526d1e/tmp/amplihack-rs": has unpushed commits

The line goes to stderr whatever RUST_LOG is set to, so it is in any log that captures the run's stderr. When RUST_LOG is set to warn or a more verbose level, cleanup also writes a tracing warning record for the same event on stderr. That record is for diagnosis; its layout is not a stable interface, so match on the amplihack: leaving line. Standard output is untouched, so --format json stays parseable.

In the report, the item is skipped and its reason is the text after the colon:

amplihack cleanup --apply --only run-tmp --format json 2>/dev/null
{
  "dry_run": false,
  "work_root": "/home/you/.amplihack/work",
  "entries": [
    {
      "category": "run-tmp",
      "path": "/home/you/.amplihack/work/r/3f0c9a526d1e",
      "size_bytes": 222298112,
      "reason": "work tree \"/home/you/.amplihack/work/r/3f0c9a526d1e/tmp/amplihack-rs\": has unpushed commits",
      "action": "skipped",
      "run_id": "3f0c9a52-6d1e-4b8a-9f57-2c41d0e7a6b3"
    }
  ]
}

Rules for the line:

  • Once per pass. One folder gets one amplihack: leaving line in one cleanup pass. At the end of a run, two passes can reach the same runtime/ folder: the runner's own removal, and then the sweep, which finds the folder once run.json is finalised. The sweep does not run when AMPLIHACK_CLEANUP_ON_EXIT=0 is set or another cleanup holds the lock. A later amplihack cleanup is another pass. A folder that still holds unsaved work is reported by each pass that reaches it.
  • Paths are quoted and escaped. A folder name with a newline, a terminal escape or a right-to-left override in it cannot start a second line, change your terminal, or reverse the line it appears in. The same escaping is applied to Git's words in the detail.
  • Only paths and the reason. The line never shows a remote URL, a commit id, a branch name or any other Git output. The detail after cannot check for unpushed commits: has credentials in URLs removed and is shortened.
  • The runner's line. When the runner's own removal of runtime/ keeps the folder, the line is the same and the run goes on. The run's exit status does not change.

Free a kept folder

Set WT to the work tree from the message. When the message names no work tree, it is the folder itself.

WT="$HOME/.amplihack/work/r/3f0c9a526d1e/tmp/amplihack-rs"
git -C "$WT" status --short
git -C "$WT" log --oneline '@{upstream}..HEAD'

For has no upstream and commits on no remote branch, list the commits with:

git -C "$WT" log --oneline HEAD --not --remotes

Then save the work or decide to drop it:

# Keep it: commit anything uncommitted, then push.
git -C "$WT" add -A
git -C "$WT" commit -m "Save work from the killed run"
git -C "$WT" push -u origin HEAD

# Or drop it: delete the folder yourself.
rm -rf "$HOME/.amplihack/work/r/3f0c9a526d1e"

After a push, run cleanup again. The folder is judged afresh, and is removed if it holds nothing unsaved and is otherwise eligible:

amplihack cleanup --apply --only run-tmp

For cannot check for unpushed commits, read the detail after the colon. Fix what it names, such as the owner of the folder, a broken .git file or a locked folder, or inspect the folder and remove it by hand.

What the check does not cover

The check is deliberately narrow. It asks the same questions as the run-worktree category, but what a wrong answer costs is not the same. A run-worktree removal takes away a linked worktree, and the branches and the stash stay behind in the main repository. Removing a folder that holds a clone takes that clone's branches and stash with it, and nothing is left behind.

  • It looks at HEAD and the working tree. It does not look at refs/stash, at local branches other than the checked-out one, or at ignored files. A clone whose checked-out branch is clean and pushed is therefore removed even when another local branch holds a commit that is on no remote, or the stash holds changes.
  • A remote-tracking branch that is out of date can make a commit look pushed. Cleanup never fetches to find out.
  • An upstream can itself be a local branch, as git checkout -b feature --track master sets up. A commit that has reached that local branch then counts as pushed, even when the local branch has never been pushed anywhere.
  • A bare repository has no work tree, so it is not judged.
  • A repository three or more levels inside the folder is not found, so the folder can be removed with it.
  • The check and the removal are two steps. Work added to a folder between them is not seen.

When a target is removed

A target recorded in the five-field format is removed only when one of three events happens. There is no age rule: --keep-hours and AMPLIHACK_CLEANUP_KEEP_HOURS do not delay or cause the removal, and a target is never removed because it is old.

Event Which targets Reason in the report
(a) Run end. The sweep removes a worktree that this run recorded. Targets this run recorded as created for that worktree's path. worktree removed by run cleanup
(b) Checkout gone. The checkout path in the line no longer exists. Any recorded target for that checkout, created or reused. checkout gone
(c) Worktree teardown. A worktree is removed by amplihack cleanup (including the run-end sweep) or by a bundled recipe helper. The target of that worktree path, unless rule (a) already covers it. This includes a reused target of the run's own worktree. See Remove the target of a removed worktree. worktree removed

In every other case the target is kept and the entry says why:

Case Reason
The run's worktree for that checkout is kept, for example because its PR is open. worktree kept
The sweep does not include the run-worktree category (--only run-target), so no worktree is removed and rule (a) cannot apply. worktree kept: run-worktree not selected
The checkout is not a worktree this run recorded, such as the main checkout or a worktree the run adopted. checkout not owned by this run
The origin is reused and the checkout still exists, for example the main checkout. reused target; checkout still exists
The checkout path could not be checked, for example EACCES. checkout state unknown: <error>
Another active run records the same target. the existing recorded by active run <id> reason

So the target of your main checkout is never removed at the end of a run, even when a run built in it first, because the main checkout is never a worktree the sweep removes. It goes only if the main checkout folder itself is deleted (event b).

Rule (a) needs the worktree to be gone. Cleanup removes worktrees before targets, and just before removing a target under rule (a) it checks the checkout path again. Anything other than "not found", including a worktree that git worktree remove refused, keeps the target with the reason worktree kept.

Every removal, whatever the event, also needs the checks in Safety rules, including a free .cargo-lock.

Issue #2039 asked that a run's target be gone when the run ends. Cleanup instead removes it when the run's worktree is removed (events a and c). While the worktree is kept, for example an auto-drive PR worktree waiting for review, its target stays, because the next commit in that worktree would otherwise rebuild it from nothing (2-5 GB of build output). The target goes in the same sweep that removes the worktree, or when the checkout folder is deleted by any other means (event b).

Example: a run builds in a new worktree for feat/issue-2039, commits, and its PR merges. At the next sweep the worktree passes its checks and is removed, and its target goes with it in the same sweep:

CATEGORY        ACTION                         SIZE     PATH
run-worktree    removed                        1.8 MiB  /home/you/.amplihack/work/worktrees/amplihack-rs-1a2b3c4d/feat/issue-2039
                reason: PR #2071 merged 2026-11-08T14:02:11Z
run-target      removed                        4.0 GiB  /home/you/.amplihack/work/targets/amplihack-precommit-target-5e6f7a8b9c0d
                reason: worktree removed by run cleanup

The report lists entries in the order they were processed, so a worktree always comes before the target that went with it.

The same run also committed once in the main checkout and recorded that target as reused. That target stays:

run-target      skipped                        6.2 GiB  /home/you/.amplihack/work/targets/amplihack-precommit-target-0a1b2c3d4e5f
                reason: reused target; checkout still exists

Linked targets

A target in <work root>/targets or the hook's cache folder can be a link to a folder on another disk. Cleanup removes a linked target, first the destination folder and then the link, only when every one of these holds, both when the plan is made and again just before removal:

  • the link is owned by you;
  • the link points in one step at a real folder: not dangling, not at another link, with no link anywhere in the destination's path;
  • the destination is a direct child of an allowed root (see below), not the root itself, or the link has the host cargo wrapper's layout (see Links made by the host cargo wrapper);
  • the destination is owned by you;
  • the destination looks like a Cargo target: a CACHEDIR.TAG with Cargo's signature, a .rustc_info.json, or <profile>/.fingerprint or <profile>/.cargo-lock; links inside it are not followed;
  • at removal, the link still points at the same destination, and the target has the same shape (folder, link or dangling link) it had when planned.

The allowed roots are <work root>/targets, the hook's XDG cache folder, and the folders in AMPLIHACK_TARGET_LINK_ROOTS:

export AMPLIHACK_TARGET_LINK_ROOTS=/mnt/build/cargo-targets
amplihack cleanup --only run-target

A host cargo wrapper, such as the one on host-a, moves a new target off the root disk and leaves a link in its place:

~/.cache/amplihack/amplihack-precommit-target-0e047858ba2f
  -> /data/you/cargo-targets/amplihack-amplihack-precommit-target-0e047858ba2f-f5e95e09b016

Cleanup follows such a link with no setting. The destination's name must be exactly <name of the link's folder>-<target name>-<12 lowercase hex>, so it carries the checkout's own hash, and the folder that holds the destination must pass the same checks as an entry of AMPLIHACK_TARGET_LINK_ROOTS (below). Every other rule in this list still applies. On host-a, therefore, AMPLIHACK_TARGET_LINK_ROOTS does not need to be set; it is only for links with some other layout.

Use a folder that holds only build output for each entry of AMPLIHACK_TARGET_LINK_ROOTS, such as /mnt/build/cargo-targets. An entry such as ~/src is accepted, because it is not your home folder or one of its parents, but a link you own that points at a project's own target/ folder under it would then let cleanup remove that project's build output.

Entries of AMPLIHACK_TARGET_LINK_ROOTS, and the folder derived from a wrapper link, that are /, your home folder or one of its parents, relative, missing, owned by someone else, or writable by others without the sticky bit are ignored with a warning. The full rules are in Linked targets and AMPLIHACK_TARGET_LINK_ROOTS.

When a check fails, the link and its destination are left alone and the entry is skipped with one of these reasons:

Reason Meaning
link not followed: outside allowed roots The destination is not a direct child of an allowed root, and its name is not the host cargo wrapper's layout, or its folder failed the root checks. Set AMPLIHACK_TARGET_LINK_ROOTS.
link not followed: not a cargo target The destination does not look like a Cargo target.
link not followed: link chain The link points at another link, or the destination's path contains one.
link not followed: foreign owner The link or the destination belongs to another user.
changed since plan The link now points elsewhere, or the target changed shape between plan and removal.

A dangling link is not checked against these rules, since there is nothing behind it to remove. When rule (a) or (b) applies to a recorded target that is a dangling link, or --worktree-target finds one at the target path, the link alone is removed with remove_file, after the ownership check. The report gives the reason of the rule that applied, for example checkout gone. A link that resolved when planned and is dangling at removal is skipped as changed since plan.

Untagged targets (the untagged-target category) still skip every link, with the reason symbolic link; not followed.

Remove the target of a removed worktree

When amplihack cleanup removes a worktree, including during the run-end sweep, it then removes that worktree's target by itself. The one exception is a target for which the same sweep has already planned a rule (a) removal as its own entry; the follow-on step skips that one to avoid handling it twice. A created record alone is not enough: under --only run-worktree, or when the rule (a) entry was refused at plan time (for example because its .cargo-lock was held), there is no such entry, and this step checks the target again after the worktree is gone. A target recorded as reused for the run's own worktree, or not recorded at all, is also removed by this step in the same sweep, with the reason worktree removed; it does not wait for rule (b) at a later sweep.

Whatever happens to the target, it is listed in the report. A target this step keeps is shown as skipped with its reason, in both text and JSON output, and a warning is written to the log. A target is listed once: when this step reports it, a plan-time entry for the same target is dropped. The result never changes the worktree's own entry.

The bundled recipe helpers do the same after their own worktree removals, in workflow-worktree.yaml, workflow_worktree_sweep.sh and workflow_runtime_artifacts.sh. Each one calls:

command -v amplihack >/dev/null 2>&1 \
  && amplihack cleanup --apply --worktree-target "$wt" >/dev/null || true

The report on stdout is discarded, but errors and refusals still go to stderr, so they appear in the step log. No helper deletes a target with rm.

When a helper runs inside an active run, that run usually records the target itself, so the call skips it as recorded by another live run. This is expected: the target is removed under rule (b) once the run has ended, because its checkout is gone by then. If amplihack is not installed, the target stays and is removed later under rule (b), if a run recorded it, or by the hook's stale-cache reclaim.

You can call the option yourself after removing a worktree by hand:

git worktree remove ~/.amplihack/work/worktrees/amplihack-rs-1a2b3c4d/feat/issue-2039
amplihack cleanup --worktree-target ~/.amplihack/work/worktrees/amplihack-rs-1a2b3c4d/feat/issue-2039
CATEGORY        ACTION                         SIZE     PATH
run-target      would-remove                   4.0 GiB  /home/you/.amplihack/work/targets/amplihack-precommit-target-5e6f7a8b9c0d
                reason: worktree removed

Add --apply to remove it. The option computes the target name from the checkout path, looks for it under <work root>/targets and the hook's cache folder, and applies the same checks as the sweep: owned by you, Cargo-shaped, lock free, the linked target rules, and not recorded by any active run. A dangling link at the target path is removed.

It refuses, writes one line to stderr and exits with code 1, when the checkout path:

Case Message
is relative amplihack cleanup: --worktree-target needs an absolute path: <path>
still exists, including the main checkout or a dangling link at that path amplihack cleanup: --worktree-target: checkout still exists: <path>
has a parent folder that cannot be resolved amplihack cleanup: --worktree-target: cannot resolve the parent of <path>
cannot be examined, for example because a parent folder denies access amplihack cleanup: --worktree-target: cannot examine checkout <path>: <error>

Only "not found" counts as removed. Any other error leaves the checkout's state unknown, and the target is kept.

The target name is computed from the checkout's parent folder, so that folder must still exist. When a helper removes the folder that held the worktree as well as the worktree, the call exits with 1 and the message cannot resolve the parent of <path>, and the target stays. The bundled helpers end the call in || true, so this does not fail the step; the message is in the step log. The target is then removed under rule (b), if a run recorded it, or by the hook's stale-cache reclaim.

It does not take the cleanup lock, so it can run while a sweep is in progress. It exits with 0 when nothing was found or the target was skipped, and with 1 only if a removal failed.

git worktree remove run directly does not call it. Such a target stays until a sweep finds its checkout gone, if a run recorded it, or until the hook's TTL reclaim removes it.

Preview and apply cleanup

A dry run lists every candidate and what would happen to it:

amplihack cleanup
CATEGORY        ACTION                         SIZE     PATH
run-target      would-remove                   4.0 GiB  /home/you/.amplihack/work/targets/amplihack-precommit-target-5e6f7a8b9c0d
                reason: PR #1820 merged 2026-10-31T08:02:11Z
run-worktree    would-remove                   1.8 MiB  /home/you/.amplihack/work/worktrees/amplihack-rs-1a2b3c4d/feat/issue-1808
                reason: PR #1820 merged 2026-10-31T08:02:11Z
run-worktree    skipped                        612 KiB  /home/you/.amplihack/work/worktrees/amplihack-rs-1a2b3c4d/feat/issue-1829
                reason: PR #1831 is open
recipe-log      would-archive                  88 KiB   /home/you/.amplihack/recipe-logs/0d4e2c1a-7f33-4b5c-9e8d-1a2b3c4d5e6f.log
                reason: modified 41 days ago
session-log     skipped                        2.1 MiB  /home/you/.claude/projects/-home-you-src-amplihack-rs/5c1f....jsonl
                reason: archived by `amplihack sessions archive`

The dry run reads only. It does not create the work root, the archive folder or the lock file.

Apply the plan:

amplihack cleanup --apply

--apply takes an exclusive lock on <work root>/.cleanup.lock and checks each item again just before removing it. Actions in the output change to removed and archived. When an item is skipped, the action is always skipped and the explanation is in reason.

Get machine-readable output:

amplihack cleanup --format json
{
  "dry_run": true,
  "work_root": "/home/you/.amplihack/work",
  "entries": [
    {
      "category": "run-target",
      "path": "/home/you/.amplihack/work/targets/amplihack-precommit-target-5e6f7a8b9c0d",
      "size_bytes": 4294967296,
      "reason": "PR #1820 merged 2026-10-31T08:02:11Z",
      "action": "would-remove",
      "run_id": "3f0c9a52-6d1e-4b8a-9f57-2c41d0e7a6b3"
    }
  ]
}

Each entry has the same six keys: category, path, size_bytes (an integer, or null when the size could not be counted), reason, action and run_id (the run id of the run folder the item belongs to, or null). Entries are sorted by category, then by path, so two runs over the same state give the same output.

Running --apply a second time reports no candidates and changes nothing on disk.

Other examples:

# Keep run output for a week instead of a day
amplihack cleanup --apply --keep-hours 168

# Archive logs older than 7 days and do nothing else
amplihack cleanup --apply --only recipe-log --only session-log --log-days 7

# Remove run output but leave every log alone
amplihack cleanup --apply --skip-logs

Cleanup categories

Category What it collects Action
run-tmp <work root>/runs/<id>/runtime (or <work root>/r/<short id>) left by a run that is no longer active, such as one that was killed. The keep period does not apply: the folder is removed as soon as the run is inactive and no running process (such as a child the killed run left behind) uses it. remove
run-worktree Worktrees recorded in a run's owned-paths, under <work root>/worktrees/ or <main checkout>/worktrees/, once the run is eligible. git worktree remove; the branch is kept
run-target Targets recorded in a run's owned-paths: on the events in When a target is removed, or for legacy lines once the run is eligible. Also the target removed by --worktree-target. A linked target loses both its destination and the link. Also each Cargo target folder (one holding CACHEDIR.TAG) inside an ended run's own folder, such as the build goal's build-goal/build.*/cargo-target, once the run is eligible; an active run's folder is skipped. remove
untagged-target Folders directly under <work root>/targets/, and amplihack-precommit-target-* folders in the hook cache base, that no run recorded and that have not been modified within the keep period. remove
tmp-leftover Top-level replay-*, release-* and amplihack-workflow-* folders in the system temporary folder that you own, that are older than the keep period, that hold no pid marker naming a live process, and that no running process has as its working folder, holds a file open in, or has as its TMPDIR, TMP or TEMP. remove
recipe-log Files in recipe.log_dir older than --log-days. archive
session-log Files in ${CODEX_HOME:-~/.codex}/sessions, including its nested date folders, older than --log-days. Claude sessions under ~/.claude/projects are listed but skipped with archived by `amplihack sessions archive`: that command archives each whole session as one restorable unit, never touches a session a running claude still uses, and runs once a day from the login rotator. See the sessions command. archive

amplihack-workflow-* folders were the per-run temporary folders of earlier releases. Runs now use <run dir>/runtime instead, so new ones no longer appear. replay-* and release-* come from tools outside amplihack; cleanup only collects them.

The tmp-leftover category is skipped entirely when cleanup runs as root, and when the system temporary folder is neither owned by you nor sticky.

In the run-tmp, untagged-target and tmp-leftover categories, and for run-target folders recorded in the legacy format, a folder that holds a Git work tree with unsaved work is skipped instead of removed. See Folders with unsaved work are kept.

Clones under <work root>/clones/ belong to no category. They are never recorded in owned-paths and never swept; remove them by hand when you no longer need them.

An auto-drive loop killed part way through a round can likewise leave a pre-review.* baseline directory in its state directory, mode 0700 with 0600 contents. autodrive_loop.sh removes the baseline once the round returns and the loop goes on, so one survives an interrupted round, and also a round that ended the run on an orphaned driver, a policy refusal, a usage limit or a recorded step-recovery handoff; the next run's first round reclaims it. No cleanup category covers that path, and the directory holds the contents of the worktree's untracked files, so remove it by hand once no recovery needs it. See pre-review evidence.

Build output: CACHEDIR.TAG and target names

Every target folder that amplihack creates contains a CACHEDIR.TAG file, so backup tools that honour the Cache Directory Tagging Specification skip it:

Signature: 8a477f597d28d172789f06886806bc55
# This file is a cache directory tag created by amplihack.
# For information about cache directory tags see https://bford.info/cachedir/

The tag is written by:

  • scripts/git-hook-cargo.sh, for the pre-commit and pre-push target. It follows the #1771 rules: it tags a target that already exists without a tag, leaves a missing target for cargo to create (cargo writes its own tag, with "created by cargo" on the second line), and never writes through a CACHEDIR.TAG that is a link, dangling or not;
  • scripts/install-pinned-recipe-runner.sh and scripts/dev-space.sh, when they create a target folder;
  • the Rust helper create_tagged_target_dir.

git-hook-cargo.sh chooses its folder like this, inside or outside a run:

Condition Folder
AMPLIHACK_WORK_ROOT usable $AMPLIHACK_WORK_ROOT/targets/amplihack-precommit-target-<hash>
AMPLIHACK_WORK_ROOT not set, or not usable ${XDG_CACHE_HOME:-$HOME/.cache}/amplihack/amplihack-precommit-target-<hash>

<hash> is the first 12 hex digits of the SHA-256 of the checkout path.

The hook uses the work root only when it already exists as a real folder (not a symbolic link) owned by you, is not group- or world-writable, and is neither / nor your home folder. It never creates the work root, and it creates targets/ with mode 0700. If it cannot record a target in owned-paths, it prints amplihack: warning: could not record cache ownership for <path> and the build goes ahead.

Each checkout has one target. Every run that commits in a checkout reuses its target, so a second run in the same worktree does not build from scratch. Inside a run, the script records the target in owned-paths as created or reused, and cleanup removes it on the events in When a target is removed. Its stale-cache reclaim recognises both the current names and the older -run-<run id> names, and considers only folders you own.

The runner does not set CARGO_TARGET_DIR. A worktree's own target/ is removed when the run ends (see Remove a run's own build output), and any that remains is removed with the worktree.

To list the targets a run recorded, with their origin and checkout:

grep '^target' ~/.amplihack/work/runs/3f0c9a52-6d1e-4b8a-9f57-2c41d0e7a6b3/owned-paths
# target    /home/you/.amplihack/work/targets/amplihack-precommit-target-5e6f7a8b9c0d       created /home/you/.amplihack/work/worktrees/amplihack-rs-1a2b3c4d/feat/issue-2039

Archive and restore logs

amplihack cleanup --apply archives each recipe log and Codex session log older than --log-days into its own .tar.gz file under AMPLIHACK_LOG_ARCHIVE_DIR, then removes the original. Archives keep the log's path relative to its log folder:

Log Archive path
<recipe.log_dir>/<name> <archive dir>/recipe-logs/<name>.tar.gz
${CODEX_HOME:-~/.codex}/sessions/<relative path> <archive dir>/codex-sessions/<relative path>.tar.gz

For example, ~/.codex/sessions/2026/09/12/rollout-2026-09-12T08-14-03-7c2e.jsonl is archived as ~/.amplihack/work/archive/codex-sessions/2026/09/12/rollout-2026-09-12T08-14-03-7c2e.jsonl.tar.gz.

For each file, cleanup:

  1. computes the SHA-256 digest;
  2. writes the archive to a .partial file with mode 0600 and flushes it to disk;
  3. reads the .partial file back, decompresses it and compares the bytes with the original, then renames it to its final name;
  4. appends a line to MANIFEST.jsonl;
  5. checks that the original's size and modification time have not changed;
  6. removes the original.

If any step fails, the original is kept and the entry is reported as failed. No log is deleted unless its archive has been verified. An archive that fails the read-back check is removed, so a retry does not leave extra files.

A manifest line looks like this:

{"original_path":"/home/you/.amplihack/recipe-logs/0d4e2c1a-7f33-4b5c-9e8d-1a2b3c4d5e6f.log","archive_path":"/home/you/.amplihack/work/archive/recipe-logs/0d4e2c1a-7f33-4b5c-9e8d-1a2b3c4d5e6f.log.tar.gz","size_bytes":90112,"digest":"sha256:4f1c...","archived_at":"2026-11-01T10:05:40Z"}

The archive folder has no CACHEDIR.TAG, so backup tools still include it. It must not be inside a log folder, and no log folder may be inside it.

Restore a log

Restore one archive to its original path:

amplihack cleanup restore \
  ~/.amplihack/work/archive/recipe-logs/0d4e2c1a-7f33-4b5c-9e8d-1a2b3c4d5e6f.log.tar.gz

Restore every file listed in a manifest:

amplihack cleanup restore ~/.amplihack/work/archive/MANIFEST.jsonl

Restore into another folder instead of the original paths:

amplihack cleanup restore ~/.amplihack/work/archive/MANIFEST.jsonl \
  --dest ~/restored-logs

The destination path always comes from a manifest line, never from the archive. When given an archive, restore reads MANIFEST.jsonl in the archive folder (AMPLIHACK_LOG_ARCHIVE_DIR, the root that holds recipe-logs/ and codex-sessions/) and uses the line whose archive_path is that archive. If no line matches, the archive is not restored; the command reports no manifest entry for <archive> and exits with code 1.

Restore never overwrites a file. It writes to a temporary file in the destination folder and moves it into place with an operation that fails if the destination exists, so a file created at the same moment is not replaced either. If the destination already exists, the conflict is reported, the other files are still restored, and the command exits with code 1. The destination must lie under a log folder or under --dest. Restored files are checked against the recorded digest and written with mode 0600.

Restore refuses, and writes nothing, when:

  • a folder between the log folder (or --dest) and the destination is a symbolic link, or resolves outside it;
  • --dest or an archive path in the manifest contains . or ..;
  • the archive holds anything other than one regular file, or more bytes than the manifest records.

Move the work root to another disk

To put run folders, targets, clones and worktrees on a data disk:

install -d -m 700 /data/amplihack-work
export AMPLIHACK_WORK_ROOT=/data/amplihack-work

amplihack recipe run default-workflow \
  -c task_description="Fix flaky login tests" \
  -c repo_path=.

With the variable set, nothing new is created under ~/.amplihack/work. Existing items under the old root are not moved; run AMPLIHACK_WORK_ROOT=$HOME/.amplihack/work amplihack cleanup --apply to clear them as they become eligible.

Inside a run, the launcher clones repositories into <work root>/clones/<repository name>-<8-character hash>. Outside a run, clones go to ./<repository name> as before. Inside a run, the launcher prints amplihack: <url> is checked out at <folder>, because the clone is no longer in the current folder. Cleanup does not remove clones.

The folder name depends only on the URL, so a later run that checks out the same repository reuses the clone. It fetches and moves the clone's branch to the fetched tip, so the run never works on an old commit. The launcher adds /worktrees/ to the clone's .git/info/exclude, so worktrees that runs put under <clone>/worktrees/ do not count as changes. The launch is refused, with a message naming the folder and changing nothing, when:

  • the folder is not a git checkout of that URL;
  • the clone has uncommitted changes, is not on a branch with an upstream, tracks a branch of a remote other than origin, or has commits its upstream does not have;
  • the work root or clones/ is a symbolic link, belongs to another user, or is group- or world-writable, or the clone belongs to another user.

Clones and fetches inside a run use only https and ssh; plain git:// is refused.

Safety rules

Cleanup deletes only items inside the folders it manages, at the depth it expects:

  • <work root>/runs/<id>/runtime and <work root>/r/<short id>;
  • recorded worktrees under <work root>/worktrees/ or <main checkout>/worktrees/;
  • folders directly under <work root>/targets/, and amplihack-precommit-target-* folders in the hook cache base;
  • for a linked target, the destination folder directly inside an allowed root, and then the link;
  • the private CARGO_TARGET_DIR a run was told to remove, and target/ at the root of a linked worktree it owns or started in, as described in Remove a run's own build output;
  • the tmp-leftover folders described above;
  • log originals, after their archive is verified.

The owned-paths rules below apply to runtime cleanup and recorded legitimate worktrees/targets. There is no execution-copy ownership manifest or end-of-run release of a private repository. The retired run-exec category is not swept; legacy execution and review directories remain for recovery.

An entry in owned-paths is a request, not a permission. A recorded path outside these folders is reported as skipped.

A worktree is removed only if all of these hold:

  • it is a linked worktree, never the main checkout;
  • it has no uncommitted changes and no unpushed commits (if its upstream branch is gone, for example deleted after the merge, every commit must be reachable from some remote-tracking branch);
  • no running process has its working folder, an open file, or its TMPDIR, TMP or TEMP inside it;
  • the run that recorded it is not active, and no other active run has recorded it (a run whose run.json cannot be read counts as active);
  • git worktree remove succeeds without --force.

The other folders cleanup removes get a narrower version of the same protection. Before it removes a run-tmp, a legacy run-target folder, an untagged-target, a tmp-leftover or the run's own runtime/ folder, it looks for Git work trees in the folder and keeps the folder if one has uncommitted changes or commits that are not on its upstream, or if it cannot tell. See Folders with unsaved work are kept.

Git commands against a candidate run with hooks and fsmonitor disabled and with the system git configuration ignored. A repository's own .git/config can still name clean and smudge filters, and those can run during git status and git worktree remove, and inside gh. The unsaved-work check widens where that applies: it runs Git in any repository copy up to two levels inside a candidate folder, which can be one a step downloaded or extracted rather than one a run recorded. Cleanup runs as you, and only in a folder whose .git belongs to you, so such a filter has no more rights than whoever could already write into that folder.

Before each removal, cleanup resolves the path again and requires it to resolve to the same place it did when the item was collected, and still to lie inside the folders above. It then removes that resolved path, never the path a step wrote. It also refuses symbolic links and repeats the liveness, Cargo lock and cleanliness checks; if the run's run.json can no longer be read, the item is skipped. An item that fails one of these checks is reported as skipped with the reason. Symbolic links are not followed when counting sizes. A run target that is a link is checked by the linked target rules instead of being refused outright.

Every target removal, whether by the sweep, after a worktree removal or by --worktree-target, goes through one routine that:

  1. takes an exclusive, non-blocking lock on <target>/.cargo-lock and on the .cargo-lock of each real subfolder, such as debug/ and release/. Lock files are opened without following links and are never created. A .cargo-lock that is itself a link counts as held, because Cargo never makes one;
  2. if any lock is held, skips the target with in use: .cargo-lock is held and deletes nothing. If a lock or a subfolder cannot be checked, it skips the target with a reason that names the problem and the path, and deletes nothing:
Reason Meaning
in use: .cargo-lock is held A build holds the lock.
linked lock: <path> The .cargo-lock is a link. Cargo never makes one.
permission denied: <path> The lock file or folder cannot be opened.
lock check failed: <path>: <error> Any other error while opening or locking.
  1. keeps every lock until the removal is finished, so a build cannot start in the target while it is being deleted;
  2. removes the resolved target folder without following links inside it, then, for a linked target, the link;
  3. logs each removal at info level with the path and shape, and a second line with the event (a, b or c from When a target is removed), the reason and the run id.

The locks are kept while remove_dir_all runs, so the .cargo-lock files are deleted while cleanup still holds them. A build that opened one of them just before that point finds its target folder gone and fails; nothing else is removed. The next build in that checkout starts from an empty target.

A held lock never makes cleanup wait or retry. The target is skipped and looked at again by the next sweep. A running process with a file open in the target also keeps it.

Locks are checked one folder level deep only. A cross-compiled profile keeps its lock at <target>/<triple>/<profile>/.cargo-lock, which is not locked; such a build is protected by the running-process check instead.

An error of any kind while checking a target, such as a permission error, a path in an unknown state, or a folder that cannot be listed, is a reason to keep the target, never to remove it.

If a run's cleanup-record.json cannot be parsed, it is renamed to cleanup-record.json.corrupt-<time>, a warning is printed, and a new record is started. When every path in owned-paths has been removed, the file is removed too.

Cleanup never touches other files in the work root, such as driver scripts or task files you keep there, clones under <work root>/clones/, your home folder, other runs' folders, or the main checkout. It never runs git worktree prune, which would also drop the entries of other runs' worktrees on missing or unmounted paths.

Command reference

amplihack cleanup [--apply] [--format text|json] [--keep-hours N]
                  [--log-days N] [--only <category>]... [--skip-logs]
amplihack cleanup [--apply] [--format text|json] --worktree-target <checkout path>
amplihack cleanup restore <manifest-or-archive-path> [--dest <dir>]
Option Meaning
--apply Remove and archive. Without it the command is a dry run.
--format text\|json Output format. Default text.
--keep-hours N Overrides AMPLIHACK_CLEANUP_KEEP_HOURS.
--log-days N Overrides AMPLIHACK_LOG_ARCHIVE_DAYS.
--only <category> Limit to one category. Repeat for several. Cannot be combined with --skip-logs.
--skip-logs Leave recipe-log and session-log alone.
--worktree-target <path> Hidden. Remove only the target of the removed worktree at <path>. Cannot be combined with --only, --skip-logs, --keep-hours or --log-days. See Remove the target of a removed worktree.

Actions in the output are would-remove, removed, would-archive, archived, skipped and failed. The explanation for each action is in reason.

Exit code Meaning
0 Success, including when items were skipped, such as folders kept for unsaved work.
1 At least one apply or restore action failed, or a fatal error occurred. The remaining items are still processed. Errors are printed as amplihack cleanup: <message>.
2 Bad usage, such as an unknown category or --only with --skip-logs.

amplihack hygiene cleanup is unchanged. Its 48-hour minimum, categories and output stay as described in Hygiene Cleanup.

Troubleshooting

git worktree list shows a prunable entry under runs/. A run was killed with SIGKILL, so it could not remove its own checkout. The end-of-run sweep deleted the folder later, but the worktree record is still in the repository's .git/worktrees/. It does no harm. amplihack cleanup never prunes records, so remove it from the repository yourself:

git -C ~/src/myapp worktree prune --dry-run --verbose
git -C ~/src/myapp worktree prune

Check the dry-run list first: prune also drops records of worktrees on a disk that is not mounted.

A run printed runtime folder ... is still in use by a running process. A process started during the run still has its working folder, an open file or its TMPDIR in the run's runtime folder. Nothing needs to be done: a later sweep removes the folder once that process exits. To find processes that hold it, set DIR to the path from the message:

DIR="$HOME/.amplihack/work/r/<id>"   # the quoted path from the message
for p in /proc/[0-9]*; do
  { readlink "$p/cwd"; ls -l "$p/fd";
    tr '\0' '\n' < "$p/environ" | grep -E '^(TMPDIR|TMP|TEMP)='; } 2>/dev/null \
    | grep -qE "$DIR(/|$)" && echo "${p#/proc/} $(tr '\0' ' ' < "$p/cmdline")"
done

The loop reads every process, so it can take a minute or more on a busy host.

See A runtime folder still in use is kept.

A run printed running processes cannot be seen on this system. The system has no /proc (macOS, for example), so the runner could not check whether the run's runtime folder is still in use, and kept it. No later sweep and no amplihack cleanup --apply removes it, for the same reason. Once nothing that the run started is still running, check the folder and delete it. Set DIR to the quoted path from the message:

DIR="$HOME/.amplihack/work/r/<id>"   # the quoted path from the message
lsof +D "$DIR"                       # lists processes with a file or working folder inside it
rm -rf -- "$DIR"                     # only when lsof printed nothing

amplihack cleanup --only run-tmp lists every folder kept this way, with its size; see What this costs, and how to list the kept folders.

lsof +D does not show a process whose only link to the folder is a TMPDIR value, so also make sure that no build server, language server or background job from that run is still alive. If the folder holds a Git work tree, look for uncommitted or unpushed work in it first: deleting by hand skips the check described in the next entry.

See Where running processes cannot be seen.

A folder stays, and stderr says leaving "<folder>" in place. The folder holds a Git work tree with uncommitted changes or unpushed commits, or cleanup could not check it. The message names the work tree and the reason. Nothing is wrong with cleanup, and the run's exit status did not change. Commit and push the work, or remove the folder by hand if you do not need it, as shown in Free a kept folder.

A folder is kept with cannot check for unpushed commits: …. Git could not answer for a repository inside the folder. Read the detail after the colon: not owned by the current user, a .git file whose gitdir: points nowhere, a repository Git calls unsafe, a locked folder, a timeout, or more than eight work trees in one folder. Cleanup keeps such a folder until you fix the cause or remove the folder yourself. A folder that cleanup tried to check and could not is kept; a repository the check never looks at is not covered, as listed in What the check does not cover.

A folder that sits inside a repository was removed. Cleanup judges only a .git in the folder itself, in a folder directly inside it, or in a folder two levels inside it, and only when that .git belongs to the folder it sits in. The enclosing repository is not judged, and a repository three or more levels inside the folder is not found. Keep work you care about in a checkout or worktree outside the folders cleanup manages.

A dry run fails with inherited TMPDIR ... does not exist (or is not a directory) or is not an absolute directory. recipe run --dry-run creates its folder inside TMPDIR, and the TMPDIR it inherited is missing, is a file, or is a relative path. This happens most often in a shell that was started inside an earlier run, whose runtime folder has since been removed. The runner does not create the folder for you, because that path may belong to another run. Point TMPDIR at a folder that exists:

export TMPDIR=/tmp
amplihack recipe run default-workflow --dry-run -c task_description="Check plan"

work root ... is writable by group or others. An existing ~/.amplihack/work created with a permissive umask is refused. Fix it with:

chmod 700 ~/.amplihack/work

Until then, amplihack recipe run stops with this error before any step runs.

A worktree is always skipped with not recorded in owned-paths. The worktree was created outside the bundled helpers, or by a release before this feature. Record it with amplihack_run_owned.sh record, or remove it yourself.

Skipped with cannot list pull requests. Read the reported detail and check gh auth status for github.com. Cleanup keeps dependent items when the required PR evidence is unavailable or exceeds the request limit.

Skipped with cannot identify repository. If you set GH_REPO, include the github.com host or set GH_HOST=github.com. Otherwise, check the worktree's remote URLs with git remote -v. For a checkout with a fork and an upstream remote, use gh repo set-default to select its PR repository.

Skipped with cleanup lock held. Another amplihack cleanup --apply or an end-of-run sweep is running. Wait for it to finish and run the command again.

A linked target is skipped with link not followed: outside allowed roots. The link points at a disk that is not in AMPLIHACK_TARGET_LINK_ROOTS, and the destination's name is not the host cargo wrapper's layout. Add the folder that holds the destinations, for example export AMPLIHACK_TARGET_LINK_ROOTS=/mnt/build/cargo-targets, and run cleanup again. If cleanup prints a warning that it ignored the entry, check that the folder exists, is owned by you, and is not writable by others. A wrapper link with this reason means the folder holding its destination failed those same checks.

A target is skipped with in use: .cargo-lock is held. A Cargo build is using it. The next sweep looks at it again; nothing needs to be done.

A target is skipped with linked lock, permission denied or lock check failed. Cleanup could not check a .cargo-lock at the path shown. Fix the permissions, or remove the link if you made it, and run cleanup again.

A target stays after its worktree was removed. Check how the worktree was removed. git worktree remove run by hand does not remove the target; call amplihack cleanup --apply --worktree-target <path> with the same path.

A tool cannot create a socket in TMPDIR. Check the length of $TMPDIR. Runs switch to the shorter <work root>/r/<short id> layout when the default path is over 80 bytes; a very long AMPLIHACK_WORK_ROOT can still exceed the socket limit, so choose a shorter one.

amplihack cleanup lists a folder as would-remove, but --apply keeps it. A dry run reports a kept folder only for the categories it judges while planning, run-tmp and legacy run-target. For an untagged-target or a tmp-leftover, the check for unsaved work runs just before removal, so only --apply shows it. Look inside such a folder yourself before you apply.

An older run left private execution or review directories. Those resources are no longer allocated, retired, or released by recipe runs. They are not collected by a replacement cleanup category. They can contain uncommitted files, staged content, unpushed commits, or repositories on which linked worktrees depend. Preserve them and inspect their Git state before deciding whether any data can be removed. Do not infer that a directory is disposable from an old ownership record or an inactive run.