feat(kvb-git): wrap the pkgmgr worktree and gc commands as skills
kvb-git-branch-gc becomes kvb-git-branch-cleanup and kvb-git-orphans-find becomes kvb-git-worktree-orphans, which drops its own orphans.py for pkgmgr's output. New wrappers: kvb-git-gc, kvb-git-worktree-cleanup and kvb-git-worktree-up. kvb-cleanup, kvb-dream, AGENTS.md and README.md point at the new names. Merges only after the pkgmgr release that ships these commands. 🎯 Fix: 90 % 💥 Break: 3 % 💸 Cost: new ⏱️ Time: new 🛡️ Security: 100 % 🏆 Score: 87 🚦 Gate: cost new, time new: cleared by the operator's commit word for the Skills wrappers (2026-11-07) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 18dade9a48a84761bf0c3a9b35584367ae0cb0e4)
This commit is contained in:
1 parent
fd8fc08603
commit
ebe7886654
17 files changed
+290
-807
No files matched your search
@@ -31,7 +31,8 @@ rules forbidding raw commands.
|
||||
- **Docker**, **worktrees**: read `docker.md` (its `⚠ Volumes` checklist
|
||||
joins Step 4) and `worktrees.md` next to this file when present. Only at
|
||||
watch stage `warn` or worse, or with worktrees among the biggest consumers:
|
||||
`kvb-git-orphans-find --sizes`, its largest orphaned worktrees as candidates.
|
||||
`kvb-git-worktree-orphans --sizes`, its largest orphaned worktrees as
|
||||
candidates.
|
||||
- **Agent dirs**: pip, npm, pre-commit, act, gh, buildx caches; Ansible and
|
||||
build temp dirs.
|
||||
- **Register**: `council.py list --long`; `rasci.py artifacts` (`kvb-org-artefacts`,
|
||||
|
||||
@@ -5,9 +5,8 @@
|
||||
Per worktree: size (`du -sh`), dirty state, whether its branch is pushed and
|
||||
whether its HEAD is in an integration line (the default branch and every
|
||||
branch the project merges features into; ask the operator or the project's
|
||||
memory). With `pkgmgr`, `pkgmgr branch gc --json --sizes --merged-into
|
||||
<line>` (one per line) measures it all; merged-clean items go to the
|
||||
`kvb-git-branch-gc` skill.
|
||||
memory). With `pkgmgr`, `pkgmgr gc --json --sizes --merged-into <line>`
|
||||
(one per line) measures it all; its candidates go to the `kvb-git-gc` skill.
|
||||
|
||||
Clean and in a line: medium, its ignored files (`node_modules`, build output,
|
||||
evidence) are lost. Dirty: a candidate only when every changed, staged and
|
||||
|
||||
@@ -74,8 +74,8 @@ in-scope session's scripts, tests and skills into their repositories.
|
||||
|
||||
The `kvb-cleanup` skill for the disk, `/tmp`, Docker and agent directories the
|
||||
window filled, with the finished tasks' artifacts (`rasci.py artifacts
|
||||
--ended`) as candidates; then `kvb-git-orphans-find` per in-scope
|
||||
repository, which hands merged-clean rows to `kvb-git-branch-gc`. Each keeps
|
||||
--ended`) as candidates; then `kvb-git-worktree-orphans` per in-scope
|
||||
repository, which hands merged-clean rows to `kvb-git-gc`. Each keeps
|
||||
its own confirmations; the dream never writes, deletes or commits for them. Close
|
||||
with `date +%s > ~/.claude/.dream-stamps/"$CLAUDE_CODE_SESSION_ID"`, so the
|
||||
next hint is twelve hours out.
|
||||
|
||||
@@ -1,42 +1,44 @@
|
||||
---
|
||||
name: kvb-git-branch-gc
|
||||
name: kvb-git-branch-cleanup
|
||||
description: >
|
||||
Remove merged branches and worktrees with `pkgmgr branch gc` after a dry
|
||||
run the operator picks from. Trigger on "branches aufräumen",
|
||||
"worktrees aufräumen".
|
||||
Remove merged branches no worktree holds.
|
||||
---
|
||||
|
||||
`pkgmgr branch gc` decides what is merged and does the removing; this skill
|
||||
decides what may go and who has to agree first. The dry run changes nothing,
|
||||
and nothing is removed before the operator's pick in this run.
|
||||
`pkgmgr branch cleanup` decides what is merged and does the removing; this
|
||||
skill decides what may go and who has to agree first. The dry run changes
|
||||
nothing, and nothing is removed before the operator's pick in this run. The
|
||||
`kvb-git-worktree-cleanup` and `kvb-git-gc` skills run these steps with their
|
||||
own command.
|
||||
|
||||
## Step 0: claim and chair
|
||||
|
||||
1. Claim `work/branch-gc-<repo>`, or `work/branch-gc-all` for every
|
||||
1. Claim `work/branch-cleanup-<repo>`, or `work/branch-cleanup-all` for every
|
||||
repository, through the `kvb-org-organize` skill.
|
||||
2. The apply takes stacks down and ends with a host-wide
|
||||
`docker image prune -f` and `docker builder prune -f`, which reach every
|
||||
session's images and build cache. When another session holds the chair,
|
||||
request the grant (what, where, how long) and wait for it; the prune never
|
||||
runs during a peer's build.
|
||||
2. The apply ends with a host-wide `docker image prune -f` and
|
||||
`docker builder prune -f`, which reach every session's images and build
|
||||
cache. When another session holds the chair, request the grant (what,
|
||||
where, how long) and wait for it; the prune never runs during a peer's
|
||||
build.
|
||||
|
||||
## Step 1: dry run
|
||||
|
||||
`pkgmgr branch gc --json --sizes --min-age 24 --merged-into <line>`, one
|
||||
`pkgmgr branch cleanup --json --min-age 24 --merged-into <line>`, one
|
||||
`--merged-into` per integration line the project merges features into
|
||||
(default branch excluded, it always counts), with `--all` only when the
|
||||
operator asked for every repository, output to `/tmp/branch-gc-<N>.json`. A
|
||||
line is protected, and a checkout at its tip stays as fresh work; an item
|
||||
operator asked for every repository, output to `/tmp/branch-cleanup-<N>.json`.
|
||||
A line is protected, and a checkout at its tip stays as fresh work; an item
|
||||
merged into a line names it in `merged_into`. No `git fetch` of your own
|
||||
before it: gc fetches without auto-maintenance, while a plain fetch on git
|
||||
before it: pkgmgr fetches without auto-maintenance, while a plain fetch on git
|
||||
2.50 or later may prune the sandbox's bind-mount stubs. Every repository's
|
||||
`warnings` (a failed fetch) go to the top of the plan. Sort the items:
|
||||
`warnings` (a failed fetch) go to the top of the plan; `forge unavailable`
|
||||
skips every item: hand the operator the dry run for the host. Sort the items:
|
||||
|
||||
- **Candidates**: `state` `merged-clean`, non-empty `actions`, and `skip`
|
||||
null or `lock: liveness unknown`.
|
||||
- **Step 5**: `state` `unmerged` or `dirty`.
|
||||
- **Reported only**: `protected`, every other `skip`, and `stale` as one count
|
||||
per repository.
|
||||
- **Candidates**: non-empty `actions`, and `skip` null or
|
||||
`lock: liveness unknown`.
|
||||
- **Step 5**: `state` `unmerged` or `dirty` without `actions`.
|
||||
- **Reported only**: `protected`, every other `skip` (`checked out in
|
||||
worktree <path>` belongs to `kvb-git-gc`), and `stale` as one count per
|
||||
repository.
|
||||
|
||||
Never override a state: a branch without own commits is `unmerged` even
|
||||
though git sees its tip on the default branch, because it is fresh work.
|
||||
@@ -59,8 +61,8 @@ Then ask with `AskUserQuestion`:
|
||||
|
||||
- **Call 1** (`multiSelect: true`): the candidates, grouped by repository,
|
||||
nothing preselected. Each description names `merged_by` and every action
|
||||
(stack with volumes, the `images`, worktree, local and remote branches), and
|
||||
`lock checked at apply` where liveness was unknown.
|
||||
(hook, stack with volumes, the `images`, worktree, local and remote
|
||||
branches), and `lock checked at apply` where liveness was unknown.
|
||||
- **Call 2** (`multiSelect: false`): `Apply: <n> items (Recommended)`,
|
||||
`Revise the selection`, `Abort`. Revise repeats call 1.
|
||||
|
||||
@@ -5,15 +5,17 @@
|
||||
remote branches, which is a push. Unless this session runs in the host PID
|
||||
namespace and the project lets agents push, hand the operator one command
|
||||
per repository, run from inside it, one `--branch` per picked item (the
|
||||
worktree path for a detached worktree):
|
||||
worktree path for a detached worktree), with the command of the skill that
|
||||
planned it:
|
||||
|
||||
```bash
|
||||
pkgmgr branch gc --apply --json --branch <name> --branch <name> | tee /tmp/branch-gc-apply-<N>.json
|
||||
pkgmgr branch cleanup --apply --json --branch <name> --branch <name> | tee /tmp/branch-cleanup-apply-<N>.json
|
||||
```
|
||||
|
||||
3. Read every item's `result` there: `done`, `done; image kept: <img>` (still
|
||||
in use elsewhere), or `failed: <why>`. Report a failure as it stands; never
|
||||
remove by hand what gc skipped or refused.
|
||||
in use elsewhere), `skipped: <why>` (changed since the plan or during the
|
||||
fini hook), or `failed: <why>`. Report a failure as it stands; never
|
||||
remove by hand what pkgmgr skipped or refused.
|
||||
4. For every `done` item, drop its worktree and branch from the task listing
|
||||
them in the `kvb-org-rasci` register (`rasci.py artifacts` names it): `set
|
||||
<task> --drop-artifact worktree:<path>`, and `--branch ''` where the branch
|
||||
@@ -4,5 +4,6 @@ Never part of the apply. One `AskUserQuestion` question per item
|
||||
(`multiSelect: false`): `Keep (Recommended)` and `Delete`, the description
|
||||
naming what is lost (the item's `reason`: unique commits, dirty paths). For
|
||||
each `Delete`, hand the operator `pkgmgr branch purge --branch <name>`: it
|
||||
asks y/N per item and answers no without a terminal, so it runs only in the
|
||||
operator's shell.
|
||||
refuses outside the host PID namespace, asks y/N per item and answers no
|
||||
without a terminal, so it runs only in the operator's shell. Its `removes:`
|
||||
line names every action, remote branches included.
|
||||
@@ -0,0 +1,27 @@
|
||||
---
|
||||
name: kvb-git-gc
|
||||
description: >
|
||||
Remove merged branches and worktrees. Trigger on "branches aufräumen",
|
||||
"worktrees aufräumen".
|
||||
---
|
||||
|
||||
`pkgmgr gc` runs the worktree cleanup and the branch cleanup in one pass: a
|
||||
merged worktree goes together with its local and remote branches, a pushed
|
||||
but unmerged clean worktree goes while its branch stays, and a merged branch
|
||||
without a worktree goes. This skill adds only the agent discipline: run the
|
||||
steps of the `kvb-git-branch-cleanup` skill
|
||||
(`${CLAUDE_SKILL_DIR}/../kvb-git-branch-cleanup/SKILL.md`, with its
|
||||
`apply.md` and `unmerged.md`) with these changes.
|
||||
|
||||
## Changes
|
||||
|
||||
- **Claim**: `work/gc-<repo>`, or `work/gc-all` for every repository. The
|
||||
apply takes stacks down with their volumes, so the chair's grant comes
|
||||
first.
|
||||
- **Dry run**: `pkgmgr gc --json --sizes --min-age 24 --merged-into <line>`,
|
||||
`--all` only on the operator's word, output to `/tmp/gc-<N>.json`.
|
||||
- **Worktree items**: the hook, sandbox and EBUSY rules of the
|
||||
`kvb-git-worktree-cleanup` skill hold for every item with a worktree.
|
||||
- **Apply**: `pkgmgr gc --apply --json --branch <name or path>`, one
|
||||
`--branch` per picked item, `| tee /tmp/gc-apply-<N>.json`, handed to the
|
||||
operator from an agent sandbox.
|
||||
@@ -1,88 +0,0 @@
|
||||
---
|
||||
name: kvb-git-orphans-find
|
||||
description: >
|
||||
Find merged and abandoned branches and worktrees, propose deleting them.
|
||||
---
|
||||
|
||||
Finds merged branches and worktrees and those with open changes nobody works
|
||||
on any more, then proposes a step for each. The script only reads; nothing
|
||||
goes without the operator's pick in this run.
|
||||
|
||||
## When
|
||||
|
||||
On "verwaiste branches", "alte worktrees", "was kann weg"; from step 8 of
|
||||
`kvb-dream`; from `kvb-cleanup` with `--sizes` when disk runs short.
|
||||
|
||||
## Step 1: find
|
||||
|
||||
Per repository in scope:
|
||||
|
||||
1. Fetch first: `git fetch --no-auto-maintenance origin`, never a plain
|
||||
fetch (the `kvb-git-branch-gc` reason) and no `--prune`, which deletes
|
||||
refs.
|
||||
2. `python3 ${CLAUDE_SKILL_DIR}/scripts/orphans.py --repo <dir> --default
|
||||
<branch> --stale-days 7 --top 5 --full /tmp/orphans-<repo>-<N>.md`:
|
||||
`--default` the branch `origin/HEAD` names, another limit only on the
|
||||
operator's word. `--sizes` adds and orders by each orphan's worktree size;
|
||||
it walks every file, so only when disk matters. `--json` lists every row.
|
||||
|
||||
The summary counts each class and shows its top rows, oldest or largest
|
||||
first; the `--full` file holds all.
|
||||
|
||||
Merged means the tip is on `origin/<default>`, or `git cherry` finds every
|
||||
commit's patch there. That catches squash and rebase merges only partly: a
|
||||
squash of several commits, a rebase that changed a patch or a merge commit
|
||||
on the branch stays unmerged. Say so above the summaries.
|
||||
|
||||
| Class | Means | Proposal |
|
||||
| --- | --- | --- |
|
||||
| `merged-clean` | merged, nothing dirty | `kvb-git-branch-gc` |
|
||||
| `merged-dirty` | merged, dirty files | `ask` |
|
||||
| `stale-open` | unmerged, dirty or unpushed (no upstream counts), idle beyond the limit, no live claim, not busy | `ask` |
|
||||
| `active` | anything else; always the main worktree, the default branch and `archive/` branches | `keep` |
|
||||
|
||||
A merged row with a live claim or a busy process proposes `keep (claimed)`
|
||||
or `keep (busy)`: never offer it. Busy is unknown without the agents-council
|
||||
config, and a sandbox hides other PID namespaces: say so.
|
||||
|
||||
## Step 2: propose
|
||||
|
||||
Print each repository's summary, then:
|
||||
|
||||
- **merged-clean**: the rows go to the `kvb-git-branch-gc` skill; its dry
|
||||
run, claims step and pick decide. A row its dry run does not list stays.
|
||||
- **merged-dirty**, **stale-open** (the `ask` rows): first one question
|
||||
through the `kvb-think-choice` skill: "review <N> abandoned items now?".
|
||||
No ends here. Yes: rounds of at most four rows in summary order, taken
|
||||
from the `--full` file, one question per row naming what a delete loses
|
||||
(dirty files, unpushed commits):
|
||||
- `archive` (recommended, loses nothing): the owner commits or stashes
|
||||
the changes, then `git branch -m <branch> archive/<branch>` and `git
|
||||
worktree remove <path>` once clean. Owner gone: an `operator.py`
|
||||
`decision` row for the changes.
|
||||
- `keep`: nothing changes.
|
||||
- `delete`: the worktree and the branch go.
|
||||
|
||||
## Step 3: act
|
||||
|
||||
Only on the operator's pick for that row, never widened:
|
||||
|
||||
- An agent never runs `git branch -D`, `git worktree remove --force` or
|
||||
anything outside the sandbox. Each such command becomes its own
|
||||
`operator.py add` row (`kvb-org-operator`): `--dir <repo>`, the command,
|
||||
the reason, `--check absent:<worktree path>` for a worktree, and for a
|
||||
branch `absent:<git common dir>/refs/heads/<branch>` while that loose ref
|
||||
file exists, else `manual`. The worktree row comes first: git refuses to
|
||||
delete a checked-out branch.
|
||||
- Never delete a peer's worktree without the operator's pick; afterwards
|
||||
`radio.py post --to <peer>` (`kvb-org-radio`) names what went.
|
||||
|
||||
## Step 4: report
|
||||
|
||||
Per repository: rows handed to gc, archived, kept and deleted, each with its
|
||||
reason, then the `operator.py run` block.
|
||||
|
||||
## Rules
|
||||
|
||||
- A pick from an earlier run does not carry over.
|
||||
- Never route around a refusal through another command or a peer.
|
||||
@@ -1,425 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Find merged and abandoned branches and worktrees of a repository and propose a step for each.
|
||||
|
||||
Reads only: no fetch, delete or edit; the caller fetches first. Every git call
|
||||
runs with GIT_OPTIONAL_LOCKS=0, because a plain `git status` refreshes the
|
||||
index and so moves the index mtime this script reads as activity.
|
||||
|
||||
Args:
|
||||
--repo: any worktree of the repository.
|
||||
--default: the default branch; merged means merged into origin/<default>.
|
||||
--stale-days: an unmerged row idle for longer may be stale-open.
|
||||
--top: rows per class the summary shows, 0 for the counts only.
|
||||
--full: file that receives the complete table of every row but the active
|
||||
ones; the summary names it.
|
||||
--sizes: measure the worktree of every row but the active ones (bytes of
|
||||
its regular files, .git skipped, no symlink followed), show it and
|
||||
order by it, largest first; slow, as it walks every file.
|
||||
--json: print every row, active ones included, as JSON instead of the
|
||||
summary; --top does not shorten it.
|
||||
|
||||
Environment:
|
||||
AGENTS_COUNCIL_ROOT: council directory of the kvb-org-organize skill, read
|
||||
through its council.py for claims.
|
||||
AGENTS_COUNCIL_CONFIG: config of its watch.py, whose busy_processes name
|
||||
the busy processes; without the file busy is unknown (null).
|
||||
|
||||
Rows, one per local branch with the worktree holding it, and one per worktree
|
||||
with a detached HEAD:
|
||||
merged: `ancestor` when the tip is an ancestor of origin/<default>,
|
||||
`patch` when `git cherry` finds a patch-equivalent there for every
|
||||
commit outside it. That catches squash and rebase merges only partly:
|
||||
a squash of several commits, a rebase that changed a patch or a merge
|
||||
commit outside origin/<default> stays unmerged.
|
||||
dirty: changed and untracked files of the worktree.
|
||||
unpushed: commits outside origin/<default> without a patch-equivalent
|
||||
there that the upstream lacks; without an upstream on a remote, those
|
||||
no remote-tracking ref holds. Always 0 when merged.
|
||||
upstream: the remote-tracking upstream, null when none resolves; null
|
||||
counts as unpushed for an unmerged row.
|
||||
last activity: newest of the tip's commit date, the worktree's index mtime
|
||||
and the mtimes of its changed files.
|
||||
claims: council claims whose work names the worktree path or the branch;
|
||||
only a live one protects.
|
||||
busy: a busy_processes process works inside the worktree; processes of
|
||||
another PID namespace stay unseen.
|
||||
size: bytes with --sizes, else null; null for active rows and rows
|
||||
without a worktree.
|
||||
|
||||
Classes, first match wins:
|
||||
active: the main worktree, the default branch and archive/ branches.
|
||||
merged-clean: merged, nothing dirty; proposal kvb-git-branch-gc.
|
||||
merged-dirty: merged with dirty files; proposal ask.
|
||||
stale-open: unmerged, dirty or unpushed, last activity older than
|
||||
--stale-days, no live claim, not busy; proposal ask.
|
||||
active: anything else; proposal keep.
|
||||
A merged row with a live claim or a busy process proposes keep (claimed)
|
||||
or keep (busy) instead.
|
||||
|
||||
Output:
|
||||
A summary: one line per class with its count and proposal, the --full
|
||||
file, claims or busy that could not be checked; then the table `| Class |
|
||||
Branch | Worktree | Dirty | Unpushed | Last activity | Claim | Proposal |`
|
||||
of the top rows per class but active, oldest last activity first, with
|
||||
--sizes largest first and a Size column after Worktree. The --full file
|
||||
holds the same table with every row but the active ones.
|
||||
|
||||
Exit codes:
|
||||
0 done, 2 usage error, a failed git call or origin/<default> missing.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import contextlib
|
||||
import dataclasses
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import stat
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from collections.abc import Callable
|
||||
from pathlib import Path
|
||||
from types import ModuleType
|
||||
from typing import NoReturn
|
||||
|
||||
ORGANIZE = Path(__file__).resolve().parents[2] / "kvb-org-organize" / "scripts"
|
||||
DAY = 86400
|
||||
ORDER = ("merged-clean", "merged-dirty", "stale-open", "active")
|
||||
HEADER = ("Class", "Branch", "Worktree", "Dirty", "Unpushed", "Last activity", "Claim", "Proposal")
|
||||
GC = "kvb-git-branch-gc"
|
||||
PROPOSALS = {"merged-clean": GC, "merged-dirty": "ask", "stale-open": "ask", "active": "keep, listed by --json"}
|
||||
UNITS = ("KiB", "MiB", "GiB", "TiB")
|
||||
DETACHED = "(detached)"
|
||||
ARCHIVE = "refs/heads/archive/"
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Scan:
|
||||
repo: str
|
||||
base: str
|
||||
default: str
|
||||
claims: list[dict]
|
||||
busy: Callable[[str], bool] | None
|
||||
now: float
|
||||
limit: float
|
||||
|
||||
|
||||
def fail(message: str) -> NoReturn:
|
||||
print(f"orphans: {message}", file=sys.stderr)
|
||||
sys.exit(2)
|
||||
|
||||
|
||||
def module(name: str) -> ModuleType | None:
|
||||
path = ORGANIZE / f"{name}.py"
|
||||
if not path.is_file():
|
||||
return None
|
||||
spec = importlib.util.spec_from_file_location(name, path)
|
||||
loaded = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(loaded)
|
||||
return loaded
|
||||
|
||||
|
||||
def git(where: str, *args: str) -> subprocess.CompletedProcess:
|
||||
try:
|
||||
return subprocess.run(
|
||||
["git", *args], cwd=where, stdin=subprocess.DEVNULL, capture_output=True, text=True,
|
||||
env={**os.environ, "GIT_OPTIONAL_LOCKS": "0"}, check=False,
|
||||
)
|
||||
except OSError as error:
|
||||
return subprocess.CompletedProcess(["git", *args], 128, "", str(error))
|
||||
|
||||
|
||||
def git_out(where: str, *args: str) -> str:
|
||||
result = git(where, *args)
|
||||
if result.returncode:
|
||||
fail(f"git {' '.join(args)} in {where}: {(result.stderr or result.stdout).strip()}")
|
||||
return result.stdout
|
||||
|
||||
|
||||
def resolves(where: str, ref: str) -> bool:
|
||||
return git(where, "rev-parse", "--verify", "--quiet", f"{ref}^{{commit}}").returncode == 0
|
||||
|
||||
|
||||
def worktrees(repo: str) -> list[dict]:
|
||||
trees: list[dict] = []
|
||||
for line in git_out(repo, "worktree", "list", "--porcelain").splitlines():
|
||||
key, _, value = line.partition(" ")
|
||||
if key == "worktree":
|
||||
trees.append({"path": value, "head": None, "branch": None, "main": not trees, "bare": False})
|
||||
elif trees and key == "HEAD":
|
||||
trees[-1]["head"] = value
|
||||
elif trees and key == "branch":
|
||||
trees[-1]["branch"] = value
|
||||
elif trees and key == "bare":
|
||||
trees[-1]["bare"] = True
|
||||
return trees
|
||||
|
||||
|
||||
def branches(repo: str) -> list[list[str]]:
|
||||
out = git_out(repo, "branch", "--list", "--format=%(refname)\t%(upstream)\t%(committerdate:unix)")
|
||||
return [line.split("\t") for line in out.splitlines() if line]
|
||||
|
||||
|
||||
def committed(repo: str, rev: str) -> int:
|
||||
return int(git_out(repo, "log", "-1", "--format=%ct", rev).strip())
|
||||
|
||||
|
||||
def measure(scan: Scan, ref: str, upstream: str) -> tuple[str | None, int, str | None]:
|
||||
tracked = upstream if upstream.startswith("refs/remotes/") and resolves(scan.repo, upstream) else None
|
||||
outside = set(git_out(scan.repo, "rev-list", ref, f"^{scan.base}").split())
|
||||
if not outside:
|
||||
return "ancestor", 0, tracked
|
||||
cherry = git_out(scan.repo, "cherry", scan.base, ref).splitlines()
|
||||
equivalent = {line[2:] for line in cherry if line.startswith("- ")}
|
||||
if equivalent == outside:
|
||||
return "patch", 0, tracked
|
||||
held = [f"^{tracked}"] if tracked else ["--not", "--remotes"]
|
||||
local = set(git_out(scan.repo, "rev-list", ref, *held).split())
|
||||
return None, len(local & (outside - equivalent)), tracked
|
||||
|
||||
|
||||
def index_mtime(tree: str) -> float:
|
||||
path = git_out(tree, "rev-parse", "--path-format=absolute", "--git-path", "index").strip()
|
||||
try:
|
||||
return os.stat(path).st_mtime
|
||||
except OSError:
|
||||
return 0.0
|
||||
|
||||
|
||||
def changes(tree: str) -> tuple[int, float]:
|
||||
fields = git_out(tree, "status", "--porcelain=v1", "-z", "--untracked-files=all").split("\0")
|
||||
count, newest, at = 0, 0.0, 0
|
||||
while at < len(fields):
|
||||
entry = fields[at]
|
||||
at += 2 if "R" in entry[:2] or "C" in entry[:2] else 1
|
||||
if not entry:
|
||||
continue
|
||||
count += 1
|
||||
with contextlib.suppress(OSError):
|
||||
newest = max(newest, os.lstat(os.path.join(tree, entry[3:])).st_mtime)
|
||||
return count, newest
|
||||
|
||||
|
||||
def mentions(text: str, name: str) -> bool:
|
||||
return re.search(rf"(?<![\w./-]){re.escape(name)}(?![\w-]|\.\w)", text) is not None
|
||||
|
||||
|
||||
def classify(item: dict, scan: Scan, ref: str) -> str:
|
||||
if item["main"] or item["default"] or ref.startswith(ARCHIVE):
|
||||
return "active"
|
||||
if item["merged"]:
|
||||
return "merged-dirty" if item["dirty"] else "merged-clean"
|
||||
pending = bool(item["dirty"] or item["unpushed"] or not item["upstream"])
|
||||
idle = scan.now - item["last_activity"] > scan.limit
|
||||
guarded = any(claim["live"] for claim in item["claims"]) or item["busy"]
|
||||
return "stale-open" if pending and idle and not guarded else "active"
|
||||
|
||||
|
||||
def proposal(item: dict) -> str:
|
||||
if item["class"] == "active":
|
||||
return "keep"
|
||||
if any(claim["live"] for claim in item["claims"]):
|
||||
return "keep (claimed)"
|
||||
if item["busy"]:
|
||||
return "keep (busy)"
|
||||
return GC if item["class"] == "merged-clean" else "ask"
|
||||
|
||||
|
||||
def inspect(scan: Scan, ref: str, upstream: str, date: int, tree: dict | None) -> dict:
|
||||
name = ref.removeprefix("refs/heads/") if ref.startswith("refs/heads/") else DETACHED
|
||||
merged, unpushed, tracked = measure(scan, ref, upstream)
|
||||
path = tree["path"] if tree else None
|
||||
present = path is not None and os.path.isdir(path)
|
||||
activity, dirty, busy = float(date), None, False
|
||||
if present:
|
||||
activity = max(activity, index_mtime(path))
|
||||
dirty, changed = changes(path)
|
||||
activity = max(activity, changed)
|
||||
busy = scan.busy(path) if scan.busy else None
|
||||
names = {path, os.path.realpath(path)} if path else set()
|
||||
if ref.startswith("refs/heads/"):
|
||||
names.add(name)
|
||||
item = {
|
||||
"branch": name,
|
||||
"worktree": path,
|
||||
"missing": path is not None and not present,
|
||||
"main": bool(tree and tree["main"]),
|
||||
"default": ref == f"refs/heads/{scan.default}",
|
||||
"merged": merged,
|
||||
"dirty": dirty,
|
||||
"unpushed": unpushed,
|
||||
"upstream": tracked,
|
||||
"last_activity": int(activity),
|
||||
"claims": [
|
||||
{key: claim[key] for key in ("office", "holder", "live")}
|
||||
for claim in scan.claims
|
||||
if any(mentions(claim["work"], each) for each in names)
|
||||
],
|
||||
"busy": busy,
|
||||
}
|
||||
item["class"] = classify(item, scan, ref)
|
||||
item["proposal"] = proposal(item)
|
||||
return item
|
||||
|
||||
|
||||
def tree_size(top: str) -> int:
|
||||
total = 0
|
||||
for base, dirs, files in os.walk(top):
|
||||
dirs[:] = [name for name in dirs if name != ".git"]
|
||||
for name in files:
|
||||
if name == ".git":
|
||||
continue
|
||||
with contextlib.suppress(OSError):
|
||||
found = os.lstat(os.path.join(base, name))
|
||||
if stat.S_ISREG(found.st_mode):
|
||||
total += found.st_size
|
||||
return total
|
||||
|
||||
|
||||
def human(size: int | None) -> str:
|
||||
if size is None:
|
||||
return "-"
|
||||
if size < 1024:
|
||||
return f"{size} B"
|
||||
value = float(size)
|
||||
for unit in UNITS:
|
||||
value /= 1024
|
||||
if value < 1024 or unit == UNITS[-1]:
|
||||
break
|
||||
return f"{value:.1f} {unit}"
|
||||
|
||||
|
||||
def cells(item: dict, now: float, sizes: bool) -> list[str]:
|
||||
worktree = (item["worktree"] or "-") + (" (missing)" if item["missing"] else "")
|
||||
unpushed = str(item["unpushed"]) + (" (no upstream)" if not item["upstream"] and not item["merged"] else "")
|
||||
day = time.strftime("%Y-%m-%d", time.localtime(item["last_activity"]))
|
||||
age = int((now - item["last_activity"]) // DAY)
|
||||
claims = [f"{c['office']} ({c['holder']}{'' if c['live'] else ', ended'})" for c in item["claims"]]
|
||||
if item["busy"]:
|
||||
claims.append("busy")
|
||||
values = [
|
||||
item["class"], item["branch"], worktree, *([human(item["size"])] if sizes else []),
|
||||
"-" if item["dirty"] is None else str(item["dirty"]), unpushed, f"{day} ({age} d)",
|
||||
", ".join(claims) or "-", item["proposal"],
|
||||
]
|
||||
return [value.replace("|", "\\|") for value in values]
|
||||
|
||||
|
||||
def table(items: list[dict], now: float, sizes: bool) -> list[str]:
|
||||
header = [*HEADER[:3], *(["Size"] if sizes else []), *HEADER[3:]]
|
||||
lines = ["| " + " | ".join(header) + " |", "| " + " | ".join("---" for _ in header) + " |"]
|
||||
return lines + ["| " + " | ".join(cells(item, now, sizes)) + " |" for item in items]
|
||||
|
||||
|
||||
def rank(item: dict, sizes: bool) -> tuple:
|
||||
largest = -(item["size"] or 0) if sizes else 0
|
||||
return ORDER.index(item["class"]), largest, item["last_activity"], item["branch"], item["worktree"] or ""
|
||||
|
||||
|
||||
def parse() -> argparse.Namespace:
|
||||
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||
parser.add_argument("--repo", required=True)
|
||||
parser.add_argument("--default", required=True)
|
||||
parser.add_argument("--stale-days", type=int, required=True)
|
||||
parser.add_argument("--top", type=int, required=True)
|
||||
parser.add_argument("--full")
|
||||
parser.add_argument("--sizes", action="store_true")
|
||||
parser.add_argument("--json", action="store_true")
|
||||
args = parser.parse_args()
|
||||
if args.stale_days < 0:
|
||||
fail("--stale-days must be 0 or more")
|
||||
if args.top < 0:
|
||||
fail("--top must be 0 or more")
|
||||
return args
|
||||
|
||||
|
||||
def target(args: argparse.Namespace) -> tuple[str, str]:
|
||||
repo = os.path.realpath(args.repo)
|
||||
if not os.path.isdir(repo):
|
||||
fail(f"--repo {args.repo} is no directory")
|
||||
if args.default.startswith("-") or git(repo, "check-ref-format", f"refs/heads/{args.default}").returncode:
|
||||
fail(f"--default {args.default!r} is no valid branch name")
|
||||
base = f"refs/remotes/origin/{args.default}"
|
||||
if not resolves(repo, base):
|
||||
fail(f"origin/{args.default} does not resolve in {repo}; fetch first")
|
||||
return repo, base
|
||||
|
||||
|
||||
def council_claims(council: ModuleType | None) -> list[dict]:
|
||||
if council is None:
|
||||
return []
|
||||
live = council.live_sessions()
|
||||
return [
|
||||
{
|
||||
"office": office,
|
||||
"holder": claim.get("name") or claim.get("session"),
|
||||
"live": not council.is_stale(claim, live),
|
||||
"work": str(claim.get("work", "")),
|
||||
}
|
||||
for office, claim in council.claims()
|
||||
]
|
||||
|
||||
|
||||
def rows(scan: Scan, sizes: bool) -> list[dict]:
|
||||
trees = worktrees(scan.repo)
|
||||
held = {tree["branch"]: tree for tree in trees if tree["branch"]}
|
||||
items = [inspect(scan, ref, upstream, int(date), held.get(ref)) for ref, upstream, date in branches(scan.repo)]
|
||||
items += [
|
||||
inspect(scan, tree["head"], "", committed(scan.repo, tree["head"]), tree)
|
||||
for tree in trees
|
||||
if not tree["bare"] and tree["branch"] is None and tree["head"]
|
||||
]
|
||||
for item in items:
|
||||
measured = sizes and item["class"] != "active" and item["worktree"] and not item["missing"]
|
||||
item["size"] = tree_size(item["worktree"]) if measured else None
|
||||
items.sort(key=lambda item: rank(item, sizes))
|
||||
return items
|
||||
|
||||
|
||||
def main() -> int:
|
||||
args = parse()
|
||||
repo, base = target(args)
|
||||
council, watch = module("council"), module("watch")
|
||||
config = watch.load_config() if watch else None
|
||||
names = config["busy_processes"] if config else None
|
||||
scan = Scan(
|
||||
repo=repo, base=base, default=args.default, claims=council_claims(council),
|
||||
busy=(lambda tree: watch.busy(tree, names)) if names is not None else None,
|
||||
now=time.time(), limit=args.stale_days * DAY,
|
||||
)
|
||||
items = rows(scan, args.sizes)
|
||||
orphans = [item for item in items if item["class"] != "active"]
|
||||
full = os.path.abspath(args.full) if args.full is not None else None
|
||||
if full is not None:
|
||||
try:
|
||||
Path(full).write_text("\n".join(table(orphans, scan.now, args.sizes)) + "\n", encoding="utf-8")
|
||||
except OSError as error:
|
||||
fail(f"--full {args.full}: {error.strerror or error}")
|
||||
if args.json:
|
||||
print(json.dumps({
|
||||
"repo": repo, "default": args.default, "stale_days": args.stale_days,
|
||||
"claims_checked": council is not None, "busy_checked": names is not None, "rows": items,
|
||||
}, indent=1))
|
||||
return 0
|
||||
print(f"orphans in {repo}, merged into origin/{args.default}, stale after {args.stale_days} days:")
|
||||
for name in ORDER:
|
||||
print(f"- {name}: {sum(item['class'] == name for item in items)} ({PROPOSALS[name]})")
|
||||
if full is not None:
|
||||
print(f"- full table: {full}")
|
||||
if council is None:
|
||||
print("- claims unknown: council.py of kvb-org-organize is missing")
|
||||
if names is None:
|
||||
print("- busy unknown: no agents-council config")
|
||||
top = [item for name in ORDER[:-1] for item in [o for o in orphans if o["class"] == name][: args.top]]
|
||||
if top:
|
||||
print(f"\nTop {args.top} per class, {'largest' if args.sizes else 'oldest'} first:\n")
|
||||
print("\n".join(table(top, scan.now, args.sizes)))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,37 @@
|
||||
---
|
||||
name: kvb-git-worktree-cleanup
|
||||
description: >
|
||||
Remove merged or pushed clean worktrees.
|
||||
---
|
||||
|
||||
`pkgmgr worktree cleanup` removes a clean worktree whose commits are merged
|
||||
or pushed: the pinned `worktree-fini` hook, its compose stack with volumes and
|
||||
images, the checkout; the branch stays. This skill adds only the agent
|
||||
discipline: run the steps of the `kvb-git-branch-cleanup` skill
|
||||
(`${CLAUDE_SKILL_DIR}/../kvb-git-branch-cleanup/SKILL.md`, with its
|
||||
`apply.md` and `unmerged.md`) with these changes.
|
||||
|
||||
## Changes
|
||||
|
||||
- **Claim**: `work/worktree-cleanup-<repo>`, or `work/worktree-cleanup-all`.
|
||||
The apply takes stacks down with their volumes, so the chair's grant comes
|
||||
first.
|
||||
- **Dry run**: `pkgmgr worktree cleanup --json --sizes --min-age 24
|
||||
--merged-into <line>`, output to `/tmp/worktree-cleanup-<N>.json`. A
|
||||
candidate may be `unmerged` with `unpushed` 0: its worktree goes, its
|
||||
branch stays; its description says so.
|
||||
- **Hooks**: `hook:worktree-fini` among the `actions` means the operator's
|
||||
pkgmgr config pins it (`worktree_hooks`, keyed
|
||||
`<provider>/<account>/<repository>`). Without it pkgmgr runs no fini hook,
|
||||
even where the worktree's Makefile defines a `worktree-fini` target: name
|
||||
such a target in the item's note, since its teardown then does not run.
|
||||
Pinning is the operator's; never run the target in place of pkgmgr.
|
||||
- **Apply**: `pkgmgr worktree cleanup --apply --json --branch <name or path>`,
|
||||
one `--branch` per picked item, `| tee
|
||||
/tmp/worktree-cleanup-apply-<N>.json`. From an agent sandbox it always goes
|
||||
to the operator: pkgmgr refuses there, and `git worktree remove` fails with
|
||||
EBUSY on the read-only `.git/worktrees/<id>` mount. Never remove a checkout
|
||||
or its metadata by hand. A `done` item drops only `worktree:<path>` from the
|
||||
register.
|
||||
- **Dirty or unpushed worktrees** reach step 5; the `kvb-git-worktree-orphans`
|
||||
skill finds the abandoned ones among them.
|
||||
@@ -0,0 +1,88 @@
|
||||
---
|
||||
name: kvb-git-worktree-orphans
|
||||
description: >
|
||||
Find abandoned branches and worktrees.
|
||||
---
|
||||
|
||||
`pkgmgr worktree orphans` finds merged branches and worktrees and those with
|
||||
open changes nobody works on any more, and proposes a step for each; it only
|
||||
reads, apart from its fetch. Nothing goes without the operator's pick in this
|
||||
run.
|
||||
|
||||
## When
|
||||
|
||||
On "verwaiste branches", "alte worktrees", "was kann weg"; from step 8 of
|
||||
`kvb-dream`; from `kvb-cleanup` with `--sizes` when disk runs short.
|
||||
|
||||
## Step 1: find
|
||||
|
||||
Per repository in scope, from inside it (`--all`: every managed one):
|
||||
|
||||
1. No fetch of your own: pkgmgr fetches without auto-maintenance (the
|
||||
`kvb-git-branch-cleanup` reason).
|
||||
2. `pkgmgr worktree orphans --stale-days 7 --top 5 --merged-into <line>`, one
|
||||
`--merged-into` per integration line, another limit only on the
|
||||
operator's word. `--sizes` orders by worktree size (slow, `du`), only when
|
||||
disk matters. `--json` lists every row, active ones included.
|
||||
|
||||
Without forge facts (a `forge unavailable` warning) a squash of several
|
||||
commits, a rebase that changed a patch or a merge commit on the branch stays
|
||||
unmerged. Say so above the summaries.
|
||||
|
||||
| Class | Means | Proposal |
|
||||
| --- | --- | --- |
|
||||
| `merged-clean` | merged, nothing dirty | `kvb-git-gc` |
|
||||
| `merged-dirty` | merged, dirty files | `ask` |
|
||||
| `stale-open` | unmerged, dirty or unpushed (no remote counts), idle beyond the limit, not busy | `ask` |
|
||||
| `stale` | worktree metadata whose checkout is missing | report only |
|
||||
| `active` | anything else; always the main worktree, protected lines and `archive/` branches | `keep` |
|
||||
|
||||
pkgmgr knows no claims: a row whose branch or worktree a live claim in
|
||||
`council.py list --long` names becomes `keep (claimed)`. Never offer a `keep
|
||||
(process: ...)` or `keep (lock: ...)` row. `host_pid_namespace: false` leaves
|
||||
busy and locks unknown: say so. Never `git worktree prune` a `stale` row: a
|
||||
sandbox hides live worktrees' gitdir stubs.
|
||||
|
||||
## Step 2: propose
|
||||
|
||||
Print each repository's summary, then:
|
||||
|
||||
- **merged-clean**: the rows go to the `kvb-git-gc` skill; its dry
|
||||
run, claims step and pick decide. A row its dry run does not list stays.
|
||||
- **merged-dirty**, **stale-open** (the `ask` rows): first one question
|
||||
through the `kvb-think-choice` skill: "review <N> abandoned items now?".
|
||||
No ends here. Yes: rerun with `--json` into `/tmp/orphans-<repo>-<N>.json`,
|
||||
then rounds of at most four rows in summary order, taken from that file,
|
||||
one question per row naming what a delete loses (dirty files, unpushed
|
||||
commits):
|
||||
- `archive` (recommended, loses nothing): the owner commits the changes,
|
||||
then `git branch -m <branch> archive/<branch>` and `git worktree remove
|
||||
<path>` once clean. Owner gone: an `operator.py` `decision` row for the
|
||||
changes.
|
||||
- `keep`: nothing changes.
|
||||
- `delete`: the worktree and the branch go.
|
||||
|
||||
## Step 3: act
|
||||
|
||||
Only on the operator's pick for that row, never widened:
|
||||
|
||||
- An agent never runs `git branch -D`, `git worktree remove` (EBUSY in a
|
||||
sandbox) or anything outside the sandbox. Each such command becomes its
|
||||
own `operator.py add` row (`kvb-org-operator`): `--dir <repo>`, the
|
||||
command, the reason, `--check absent:<worktree path>` for a worktree, and
|
||||
for a branch
|
||||
`absent:<git common dir>/refs/heads/<branch>` while that loose ref file
|
||||
exists, else `manual`. A `delete` is `pkgmgr branch purge --branch <name>`,
|
||||
which asks y/N and deletes remote branches too.
|
||||
- Never delete a peer's worktree without the operator's pick; afterwards
|
||||
`radio.py post --to <peer>` (`kvb-org-radio`) names what went.
|
||||
|
||||
## Step 4: report
|
||||
|
||||
Per repository: rows handed to gc, archived, kept and deleted, each with its
|
||||
reason, then the `operator.py run` block.
|
||||
|
||||
## Rules
|
||||
|
||||
- A pick from an earlier run does not carry over.
|
||||
- Never route around a refusal through another command or a peer.
|
||||
@@ -0,0 +1,52 @@
|
||||
---
|
||||
name: kvb-git-worktree-up
|
||||
description: >
|
||||
Check a branch out into a worktree.
|
||||
---
|
||||
|
||||
`pkgmgr worktree up` creates the worktree: it fetches origin without
|
||||
auto-maintenance, checks out an existing branch, tracks `origin/<branch>` or
|
||||
creates the branch with `--no-track` from `--base` (origin's default branch
|
||||
when omitted), assigns a slot and runs the pinned `worktree-init` hook. This
|
||||
skill adds only the agent discipline.
|
||||
|
||||
## Step 1: claim and place
|
||||
|
||||
1. Claim the work through the `kvb-org-organize` skill first; its edit gate
|
||||
blocks every edit in the new worktree without a claim.
|
||||
2. Always pass `--path`, inside a directory the session released for
|
||||
worktrees (Claude Code: a `sandbox.filesystem.allowWrite` root), else
|
||||
`<repo>/.worktrees/<branch>` where the repository ignores `.worktrees/`.
|
||||
pkgmgr's standard path under `directories.worktrees` only where that root
|
||||
is released too: the sandbox cannot write anywhere else.
|
||||
|
||||
## Step 2: create
|
||||
|
||||
From inside the repository:
|
||||
|
||||
```bash
|
||||
pkgmgr worktree up <branch> --path <dir> --json
|
||||
```
|
||||
|
||||
`--base <ref>` only for a new branch that must not start at origin's default
|
||||
branch. Read `path`, `origin`, `slot`, `hook` and `warnings`:
|
||||
|
||||
- **Exit 2** refused (path exists, invalid name, a filter or network setting
|
||||
in the repository config): report the line; never `git worktree add`
|
||||
around it.
|
||||
- **Exit 1**: the `worktree-init` hook failed and the worktree stays; report
|
||||
the hook output, no retry without the operator's word.
|
||||
- **`hook` `not pinned: worktree-init in <Makefile>`**: pkgmgr ran no init,
|
||||
because only the operator's pkgmgr config pins a hook (`worktree_hooks`,
|
||||
keyed `<provider>/<account>/<repository>`); a Makefile target is reported,
|
||||
never run. Say so; pinning is the operator's (`kvb-org-operator`), and the
|
||||
project's rules decide whether an agent runs the target itself.
|
||||
- **`warnings`** (`fetch failed`, `fetch skipped`): the base may be stale; say
|
||||
so.
|
||||
|
||||
## Step 3: register
|
||||
|
||||
Record the worktree under the task in the `kvb-org-rasci` register:
|
||||
`rasci.py set <task> --workdir <path> --branch <branch> --artifact
|
||||
worktree:<path>`. It goes later through the `kvb-git-worktree-cleanup` skill,
|
||||
never by hand.
|
||||
@@ -61,7 +61,7 @@ row.
|
||||
Per merged branch and for `unify/<goal>`, the teardown of `kvb-org-swarm`
|
||||
(`teardown.md`: jobs, stacks, container-owned files, worktree, register). A
|
||||
proposal keeps its branch and loses worktree, stacks and temporary files. Then
|
||||
the dry runs of `kvb-git-branch-gc` (`--merged-into <line>`) and the
|
||||
the dry runs of `kvb-git-branch-cleanup` (`--merged-into <line>`) and the
|
||||
inventory of `kvb-cleanup` for the swarm's logs, scratch and Docker leftovers,
|
||||
merged into one table (area, item, size, owner, risk, what breaks if wrong)
|
||||
and one `AskUserQuestion` pick; each skill removes only the picked items, by
|
||||
|
||||
Reference in new issue
Block a user