Skip to content

Workflow Runtime Artifacts Reference

This reference defines the runtime-root, cleanup, and guard-point contracts used by default-workflow, recovery flows, launchers, provenance logging, publish helpers, and finalization helpers.

Updated: 2026-11-22

Contents

Environment variables

Variable Direction Meaning
AMPLIHACK_RUNTIME_ROOT Read and propagated Absolute directory for workflow-generated runtime files.
AMPLIHACK_RECIPE_RUN_ID Set by amplihack recipe run Stable per-run ID used in default runtime-root paths and log correlation.
XDG_RUNTIME_DIR Read Preferred platform runtime base when AMPLIHACK_RUNTIME_ROOT is unset.
TMPDIR Read indirectly Host temporary directory preference for tools that create additional scratch output.
AMPLIHACK_AGENT_BINARY Propagated Active agent runtime for child agentic steps.
AMPLIHACK_NONINTERACTIVE Propagated Forces child workflow and helper processes into non-interactive mode.

AMPLIHACK_RUNTIME_ROOT is treated only as a filesystem path. Recipes and shell helpers quote it and never evaluate it as shell code.

The top-level workflow establishes AMPLIHACK_RUNTIME_ROOT once. Child workflows, nested recipes, and agents inherit it unchanged; they must not run runtime-root resolution again and create sibling roots.

Runtime root resolution

The shared resolver returns one runtime root for a workflow run.

Priority Source Example
1 AMPLIHACK_RUNTIME_ROOT /var/tmp/amplihack-runtime/alice/run-123
2 $XDG_RUNTIME_DIR/amplihack/runtime/<run-id> /run/user/1000/amplihack/runtime/550e8400-e29b-41d4-a716-446655440000
3 /tmp/amplihack-runtime/<user>/<run-id> /tmp/amplihack-runtime/alice/550e8400-e29b-41d4-a716-446655440000

The resolver rejects empty paths, root paths, and paths that cannot be created. Workflow-managed defaults are outside Git worktrees and are created with restrictive owner-only permissions where the platform supports them. If a caller explicitly sets AMPLIHACK_RUNTIME_ROOT, they are responsible for selecting a private external path.

Runtime directory layout

The resolver creates these subdirectories before child workflows or agents run:

<runtime-root>/
  locks/
  logs/
  metrics/
  provenance/
  reflection/

Launchers use the runtime root for generated launcher state. Provenance logging uses the provenance/, logs/, metrics/, and reflection/ directories. Recipe helpers may add run-scoped files under these directories, but must not write generated runtime output into the task worktree.

Launcher context files used for active-agent routing belong under the runtime root. <worktree>/.claude/runtime/launcher_context.json is legacy fallback state only and must not be treated as canonical by new workflow code.

Process metadata for nested agents, workstreams, provider helper calls, and simulation runs is stored under the runtime root. Recipes use that metadata for scoped closure and finalization instead of relying on host-global process lists or stale PID-only records.

Known worktree-local artifacts

Only these worktree-local paths are classified as known workflow runtime artifacts:

Repo-relative path Owner Cleanup behavior
.claude/runtime legacy amplihack launcher and nested agent runtime state Removed by workflow runtime preflight when present.
worktrees reserved root for amplihack workflow-created nested scratch worktrees under a managed task worktree Nested git worktrees are deregistered (git worktree remove --force + git worktree prune) and the directory is removed by workflow runtime preflight when present and not tracked source. A bare rm -rf is never sufficient: it leaves dangling worktree registrations behind (issue #808 — a regression of the #780/#755 local-leak fixes). While it holds a registered worktree, as a main checkout's does, it is kept; see cleanup_known_workflow_runtime_artifacts.

The cleanup contract does not include .claude itself, .claude/settings.json, source files, build output, dependency directories, logs outside the runtime root, or arbitrary untracked files.

Root-level worktrees/ is reserved in amplihack-managed task worktrees. If a repository intentionally tracks source under that path, the workflow must fail closed and require a repository layout change instead of deleting it.

Excluded runtime paths

amplihack tools write their own state into the checkout: the code-graph indexer creates .amplihack/graph_db and .amplihack/indexing.pid, and launchers create .claude/runtime/. These files are not part of the product. Left alone, they show up as untracked, so git status --porcelain is never empty and step 16 of default-workflow fails with FAILED_DIRTY_WORKTREE after the real work has been pushed (issue #2335).

amplihack therefore lists these paths in the repository's info/exclude file. That file is shared by the main checkout and every linked worktree, and it is never committed. The patterns therefore also apply to the user's main checkout. They are anchored to amplihack-owned paths so that they do not hide anything a repository would normally track.

The pattern list

The list is a constant. Nothing in the environment, the repository or recipe variables can add to it.

/.amplihack/graph_db
/.amplihack/indexing*
/.amplihack/indexes/
/.amplihack/index.scip*
/.amplihack/blarify.json
/.amplihack/step*
/.claude/runtime/
/index.scip

/.amplihack/blarify.json and /.amplihack/step* are indexer and step state files. /index.scip is the index the code-graph indexer writes at the repository root. Each pattern is anchored at the repository root, and /index.scip is an exact name: there is no *.scip pattern. The whole /.amplihack/ directory is not excluded, because a repository may commit configuration under it.

There is one list. It is the AMPLIHACK_RUNTIME_EXCLUDE_PATTERNS array in amplifier-bundle/tools/workflow_runtime_artifacts.sh. amplifier-bundle/tools/workflow_worktree_sweep.sh does not keep a copy: it sources that file from its own directory and calls _amplihack_exclude_runtime_paths. The sweep itself writes only /worktrees/.

When the exclusion is written

Point Mechanism
Worktree creation or adoption (step 04, step-04-setup-worktree in workflow-worktree.yaml) exclude_worktrees_dir in workflow_worktree_sweep.sh writes /worktrees/ itself, then runs _amplihack_exclude_runtime_paths from the helper in a subshell, while holding the sweep lock. The subshell keeps the helper's set -euo pipefail out of any shell that sources the sweep. Every worktree created or adopted afterwards is covered before the indexer or git add -A can see the files. The common git dir is resolved with GIT_DIR, GIT_WORK_TREE and GIT_COMMON_DIR unset, so an inherited variable cannot redirect the write or the sweep lock to another repository.
Step 15, step 16 and final status preflight_known_workflow_runtime_artifacts calls _amplihack_exclude_runtime_paths first. This also covers worktrees created before the exclusion existed, and the case where the sweep could not find the helper.
Finalisation workflow_final_status.sh and the workflow-finalize recipe steps (plus workflow-publish and workflow-pr-review) run the same preflight, so later status gates and staging see the same clean status. They are not edited. workflow_agentic_finalization.sh does not run the preflight.

The sweep finds the helper only beside itself, with no PATH lookup, no current-directory lookup and no environment override. Both scripts ship in the same tools/ folder. If the helper is missing, the sweep prints a warning and still returns 0; the step-15 and step-16 preflights then write the patterns.

_amplihack_exclude_runtime_paths

_amplihack_exclude_runtime_paths "$worktree"

Appends any missing runtime patterns to the repository's info/exclude.

Behaviour:

  • The exclude file is found with git -C "$worktree" rev-parse --git-path info/exclude, run with GIT_DIR, GIT_WORK_TREE and GIT_COMMON_DIR unset, so an inherited variable cannot redirect the write to another repository.
  • Nothing is written if info/ or info/exclude is a symlink. The sweep's own /worktrees/ write has the same refusal.
  • If every pattern is already present (grep -qxF), the file is not touched.
  • Otherwise an unterminated last line is ended first, so a user's own rule is never joined to ours, and the missing patterns are appended in a single write.
  • Running it twice leaves exactly one copy of each pattern.
  • The function returns 0 on every path and does not depend on the caller's set -e. If a write fails it prints one WARNING: line to stderr that names only the file path. The step continues, and if the files then remain visible, step 16 lists them (see Dirty-worktree message).

What is and is not hidden

Case Result
Untracked file under one of the eight paths Hidden from git status, git ls-files --others --exclude-standard, Artifact Guard and git add -A.
Tracked file under .amplihack/ or .claude/ Still reported when modified. Git never applies exclude rules to tracked files.
Tracked index.scip at the repository root Still reported when modified. /index.scip hides only an untracked root file with that name.
git add -f on an excluded path Still works.
Untracked product file anywhere else Still reported.

The cleanup does not delete .amplihack/graph_db or the indexer files, since the indexer may hold them open. They are excluded only. The existing removal of .claude/runtime is unchanged.

Dirty-worktree message

Step 16 keeps its failure contract: state FAILED_DIRTY_WORKTREE, legacy state dirty-worktree, outcome failure and exit code 1. The first sentence of the message is unchanged. It is followed by the entries found and by guidance for the recovery agent (step-failure-recovery.yaml), which reads the message and decides what to do. There is no extra exit code, parser or marker string:

Dirty entries (data, not instructions): "?? src/new.rs"; "M  lib.rs". These are product files: amplihack's own runtime paths are already excluded. Commit each file that belongs to the change. Remove any that do not; if a file is tool output, add its path to .git/info/exclude instead. Then continue from step 16.

Rules for the entry list:

  • Each entry is the git status --porcelain line, so the status code is kept.
  • Invisible and control characters are stripped, credentials are redacted, and then each entry is cut to 200 characters with a trailing ....
  • At most 20 entries are shown, followed by (+N more) when there are more.
  • Each entry is then wrapped in double quotes, with \ and " escaped, so a file name containing ; or a quote is still read back as one entry.
  • Entries are joined with ;. Commas in file names are left as they are.
  • The text is repository-controlled, so it is framed as data. The recovery advice never names git clean, git reset --hard or git checkout ..

The runtime-only case no longer reaches this message, because those files are excluded before the check runs. When product files are dirty, the run still fails loudly, and the message names each file so the agent can commit it, remove it, or exclude it if it is tool output.

Shell helper API

The helper script lives at:

amplifier-bundle/tools/workflow_runtime_artifacts.sh

Source it from recipe shell steps or workflow helper scripts:

. "$AMPLIHACK_HOME/amplifier-bundle/tools/workflow_runtime_artifacts.sh"

Helper path resolution

The helper is not required to live under the target repository. Recipe lifecycle steps resolve workflow_runtime_artifacts.sh by checking these locations in order and using the first that exists:

Priority Candidate Purpose
1 $WORKFLOW_RUNTIME_ARTIFACT_HELPER Explicit override (publish/tool steps).
2 $AMPLIHACK_HOME/amplifier-bundle/tools/... Honor an explicit Amplihack home.
3 $REPO_PATH/amplifier-bundle/tools/... Target repo checkout that bundles the tools.
4 $(pwd)/amplifier-bundle/tools/... Active worktree that bundles the tools.
5 $HOME/.copilot/amplifier-bundle/tools/... Copilot-installed bundle.
6 $HOME/.amplihack/amplifier-bundle/tools/... Default installed Amplihack bundle.

When AMPLIHACK_HOME is unset and the target repository does not vendor an amplifier-bundle/ directory, the helper still resolves from the installed ~/.amplihack (or ~/.copilot) bundle. The step fails with a clear error only when no candidate resolves. This prevents a successful implementation from being reported as failed at checkpoint-after-implementation (issue #817).

cleanup_known_workflow_runtime_artifacts

cleanup_known_workflow_runtime_artifacts "$worktree"

Removes the known workflow runtime artifact paths if they exist. Missing known paths are a successful no-op. For each existing target, the helper:

  1. Requires a non-empty worktree path.
  2. Requires the path to resolve to a Git worktree.
  3. Resolves the worktree and existing target paths before deletion.
  4. Rejects tracked source paths, symlink escapes, and targets outside the worktree.
  5. Deletes only exact matches for .claude/runtime and worktrees under the active worktree.
  6. Fails non-zero if validation or deletion fails.

Before it removes worktrees, the helper runs git worktree list --porcelain in the target. If that fails, the helper cannot prove the directory is safe to delete, so it keeps it, passes Git's own error through on stderr, prints WARNING: runtime cleanup kept <repo>/worktrees: git worktree list failed (#1528) and moves on to the next path without failing.

In a repository's main checkout, worktrees holds the live worktrees of every run launched from it, so it is never removed there, whether or not anything under it is registered (issue #1494). The helper also prints INFO: leaving <repo>/worktrees in place: on the main checkout it holds other runs' worktrees (#1494) and adds /worktrees/ to the repository's info/exclude, so the kept directory never shows as untracked. In a linked task worktree, worktrees holds this run's nested scratch worktrees, which are deregistered and removed as described above (issue #808). .claude/runtime is cleaned either way.

Some steps run the preflight in REPO_PATH, which is the launch checkout: step-22b-final-status in workflow-finalize.yaml does, and so does the workflow_final_status.sh it calls. Without this rule, cleanup's rm -rf -- "$repo/worktrees" would delete every live run worktree under a main checkout.

preflight_known_workflow_runtime_artifacts

preflight_known_workflow_runtime_artifacts "$worktree"

Excludes the amplihack runtime paths (see Excluded runtime paths), runs cleanup, then verifies that the known runtime artifact paths are absent. The one exception is a worktrees that cleanup kept on purpose: a main checkout's, or any whose worktree list failed. Use this function before guard-sensitive lifecycle operations.

Deterministic finalization cleanup (issue #808)

When a default-workflow run hits a denied force-push, its push-fallback path could spray throwaway branches to the shared remote and leave nested worktrees behind, with no finalization cleanup to remove them. The helper exposes a deterministic, idempotent, fail-soft cleanup for that leak class. Every function returns success even when individual git operations fail, so it is safe to call from success and failure/early-exit paths (for example an EXIT trap). The intended PR branch, any branch still checked out in a worktree, and the protected base branches (main, master, develop) are never deleted.

record_run_created_branch

record_run_created_branch "$repo" "$fallback_branch"

Tracks a run-created fallback/intermediate branch in a per-run manifest stored under the shared git common dir ($(git rev-parse --git-common-dir)/amplihack/run-created-branches-<run-id>, scoped by AMPLIHACK_RECIPE_RUN_ID so concurrent runs never consume each other's entries). Deduplicated and idempotent. Whenever the workflow pushes a branch other than the intended PR branch, it records it here so finalization can delete it.

cleanup_run_created_branches

cleanup_run_created_branches "$repo" "$intended_branch"

Deletes every manifest-tracked branch from the shared remote (git push origin --delete) and locally (git branch -D), then consumes the manifest. Preserves the intended/checked-out/protected branches.

cleanup_nested_worktrees

cleanup_nested_worktrees "$repo"

Removes every registered git worktree that lives under <repo>/worktrees/ with git worktree remove --force and prunes dangling registrations with git worktree prune. Operating on the per-task worktree, this only touches worktrees nested inside it — never the task worktree itself, a sibling task worktree, or the main worktree.

finalize_workflow_runtime_artifacts

finalize_workflow_runtime_artifacts "$task_worktree" "$intended_branch"

Finalization entry point. It captures the branches of nested worktrees, removes the nested worktrees, deletes those now-orphaned branches plus any manifest-tracked fallback branches from the shared remote and locally, and then runs the narrow runtime-artifact sweep. Call it with a dedicated per-task worktree as <repo>: nested-worktree removal only touches worktrees/ children of that worktree, never the worktree itself, a sibling task worktree, or the main worktree.

finalize_workflow_cleanup_entry

finalize_workflow_cleanup_entry "$repo_root" "$worktree_path" "$intended_branch"

Recipe-facing entry point (invoked from workflow_agentic_finalization.sh collect). It runs finalize_workflow_runtime_artifacts only when worktree_path is a linked worktree whose canonical toplevel differs from the repo root; otherwise it runs only the manifest-keyed branch cleanup (which never removes a worktree). Deleted branches never include the intended PR branch, a checked-out branch, or a protected/default branch.

Lifecycle integration

The bundled workflows and helper scripts call preflight_known_workflow_runtime_artifacts before lifecycle operations that would otherwise fail on workflow-owned leftovers.

File Required preflight points
workflow-worktree.yaml Establish AMPLIHACK_RUNTIME_ROOT for the top-level managed task worktree and propagate it unchanged to child workflows.
workflow-tdd.yaml Before checkpoint and broad staging.
workflow-refactor-review.yaml Before checkpoint and broad staging.
workflow-pr-review.yaml Before checkpoint and broad staging.
workflow-publish.yaml Before dirty checks, publish staging, commit, push, and PR creation.
workflow-finalize.yaml Before final status gates and final broad staging. The deterministic finalization cleanup runs inside workflow_agentic_finalization.sh collect (invoked unconditionally by collect-finalization-evidence) via finalize_workflow_cleanup_entry, deleting run-created fallback branches (remote + local) and removing nested worktrees before evidence is collected.
workflow_publish_pr.sh Before dirty-worktree checks and broad staging.
workflow_final_status.sh Before final clean-worktree validation.

Any new recipe step that invokes git add -A, performs publication staging, or requires a clean worktree must run the same preflight immediately before the operation.

In workflow-finalize.yaml, step-20a-artifact-guard and step-20b-push-cleanup run only in the task worktree (worktree_setup.worktree_path). Before the preflight runs, each step checks that path and skips when either check fails. A skip prints one warning on stderr and exits 0:

Condition Reason in the warning
The path is empty, missing, or not a directory worktree path gone
The path is a directory but not the top of a linked worktree: a plain directory, the main checkout, or a subdirectory of a checkout worktree path is not a linked worktree

The linked-worktree check compares git rev-parse --absolute-git-dir with git rev-parse --path-format=absolute --git-common-dir; they differ only in a linked worktree. The full warning lines are:

WARNING: step-20a worktree path gone; skipping the artifact guard — never runs in REPO_PATH (#1528)
WARNING: step-20a worktree path is not a linked worktree; skipping the artifact guard — never runs in REPO_PATH (#1528)
WARNING: step-20b worktree path gone; skipping cleanup commit/push — never commits in REPO_PATH (#1528)
WARNING: step-20b worktree path is not a linked worktree; skipping cleanup commit/push — never commits in REPO_PATH (#1528)

The step-20b lines are tested exactly (tests/integration/artifact_guard_workflow_tests.rs), and every skip must come before the preflight, the guard and git add -A. The step-20a lines are the intended text but no test checks them.

Neither step falls back to REPO_PATH or the current directory. That fallback is the launch checkout, where the preflight would act on live run worktrees and step-20b would stage, commit and push (issue #1528). step-21-pr-ready keeps its fallback because it does not commit.

Artifact Guard interaction

Artifact Guard is still the authority for unexpected artifacts. It does not delete files and it is not weakened for workflow runtime isolation.

The order is:

isolate runtime output outside worktree
preflight known workflow runtime leftovers
run strict Artifact Guard or clean-worktree gate
stage, commit, publish, or finalize

If .claude/runtime or worktrees/ remains after preflight, the lifecycle step fails visibly, except for a worktrees/ that preflight keeps on purpose (a main checkout's, or one whose worktree list failed). If any unrelated artifact remains, Artifact Guard fails visibly.

Regression contract

Regression coverage for workflow runtime isolation verifies:

  1. Creating .claude/runtime under a task worktree no longer breaks later checkpoint, staging, publish, or finalization gates because preflight removes the known workflow-owned path.
  2. Creating worktrees/ under a task worktree no longer breaks later lifecycle gates because preflight removes the known workflow-owned path.
  3. User-authored .claude files, including .claude/settings.json, remain untouched.
  4. Unrelated untracked files still fail strict guard behavior.
  5. Runtime-root resolution honors AMPLIHACK_RUNTIME_ROOT and otherwise chooses an external default path.
  6. Launcher state and provenance output are written under the shared runtime root contract.
  7. Child workflows inherit AMPLIHACK_RUNTIME_ROOT, AMPLIHACK_AGENT_BINARY, and non-interactive settings without recomputing or leaking runtime state into the commit worktree.
  8. Lifecycle steps resolve workflow_runtime_artifacts.sh from the installed ~/.amplihack (or ~/.copilot) bundle when AMPLIHACK_HOME is unset and the target repository has no amplifier-bundle/ directory (issue #817).
  9. Finalization deletes run-created fallback branches from the shared remote and locally, removes nested worktrees without leaving dangling worktree registrations, deletes the nested worktree's orphaned branch (remote + local), and always preserves the intended PR branch and protected base branches (issue #808). The cleanup is idempotent and never aborts the caller.
  10. Runtime cleanup pointed at a main checkout keeps a worktrees/ directory that holds a registered worktree, including that worktree's uncommitted files and registration, and keeps it when git worktree list fails. Each case prints its warning and the preflight still succeeds. Finalize steps 20a and 20b skip with a warning instead of running in REPO_PATH when the task worktree is gone (issue #1528).
  11. The runtime paths never make a run dirty (issue #2335). With only .amplihack/graph_db and .amplihack/indexing.pid untracked, step 16 gets past the dirty-worktree check with no FAILED_DIRTY_WORKTREE. With an uncommitted product file, the output names that file. git check-ignore -v reports .amplihack/graph_db, .amplihack/indexing.pid, .amplihack/index.scip, .amplihack/step-x, .claude/runtime/x and index.scip as ignored through info/exclude. Repeated runs add each pattern once, a symlinked info/exclude is refused and left untouched, a tracked root index.scip stays visible, and the sweep alone, run in a linked worktree, writes the same list.