@klok-integration/cli 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,116 @@
1
+ ---
2
+ name: klok-worktree
3
+ description: Create, use, hand over and clean up the per task Git worktree and branch that every change to a Klok repository is made in (<workspace>/worktrees/<tenant>/<group>/.../<repo>/<task-id>, branch agent/<task-id>). Use before the first edit of any task that changes repository files, when resuming another session's work, and when finishing or cleaning up.
4
+ ---
5
+
6
+ # Working in a task worktree
7
+
8
+ The rule is in `KLOK-AGENTS.md` (*One task, one worktree, one branch*): every task that changes repository files runs
9
+ in its own Git worktree on its own branch, and the canonical checkout is left exactly as found. This skill is the
10
+ procedure. The principle behind it, *Git is the truth*, is described in
11
+ https://docs.klok.dev/klok-integration-strategy-and-tactics/llm.txt.
12
+
13
+ ## Directory contract
14
+
15
+ | Item | Value |
16
+ |---|---|
17
+ | Workspace root | the directory `klok sync` mirrors into: config key `workspace`, `KLOK_GIT_LOCAL_WORKSPACE`, default `~/devklok` |
18
+ | Canonical checkout | `<root>/<tenant>/<group>/<subgroups...>/<repo>` |
19
+ | Task worktree | `<root>/worktrees/<tenant>/<group>/<subgroups...>/<repo>/<task-id>` |
20
+ | Task branch | `agent/<task-id>` |
21
+ | Task id | `YYYYMMDD-<short-topic>-<8 random hex>`, lowercase letters, digits and hyphens |
22
+
23
+ The task directory is the worktree root; its parent only groups that repository's tasks and is never a place to
24
+ run repository commands. Expand `~` to the real home directory. Never flatten the repository path to its
25
+ basename, never put the worktree inside `.claude`, inside the canonical checkout or in a tool specific directory.
26
+
27
+ ## Before the first edit
28
+
29
+ 1. **Find the repository.** From the directory the task points at, use Git metadata rather than folder names:
30
+ `git rev-parse --show-toplevel` gives the checkout, `git rev-parse --git-common-dir` tells whether you are in
31
+ the canonical checkout (`.git` inside it) or already in a worktree (a path into another checkout's `.git`),
32
+ `git remote -v` gives the remote. The workspace relative path of the canonical checkout is `<repo_rel>`.
33
+ 2. **Load the instructions deliberately.** Read `<root>/KLOK-AGENTS.md`, `<root>/<tenant>/KLOK-AGENTS.md` and
34
+ every `KLOK-AGENTS.md`, `AGENTS.md` or `CLAUDE.md` along the canonical path, plus the ones inside the repository revision you will
35
+ work on. A worktree under `<root>/worktrees` does not sit below those directories, so no tool loads them for
36
+ you. Keep the existing project rules; this skill does not replace them.
37
+ 3. **Inspect, do not touch.** `git -C <canonical> status --short`, `git -C <canonical> worktree list --porcelain`
38
+ and the canonical branch. Never `stash`, `reset`, `clean`, `checkout` or `switch` there. Uncommitted changes in
39
+ the canonical checkout are not a blocker; the worktree starts from committed history. If the task depends on
40
+ those changes, say so and ask for a handoff instead of moving them.
41
+ 4. **Choose the base.** Fetch the remote and resolve its default branch from the remote HEAD
42
+ (`git symbolic-ref refs/remotes/origin/HEAD`, or `git remote show origin`); do not assume `main`. Use the task's
43
+ named base when it gives one. Offline, say how old the local base is and continue when that is reasonable.
44
+ 5. **Create the worktree.** A new id per writing session; if the branch or directory exists, pick a new id rather
45
+ than deleting or force reusing anything.
46
+ 6. **Move into it.** Every edit, build, test and Git command uses the worktree as working directory (`cd` or
47
+ `git -C`). Verify `git rev-parse --show-toplevel`, `git branch --show-current` and the shared common dir before
48
+ the first write. Open this exact folder in the editor.
49
+ 7. **Say where you are.** State repository, worktree path and branch, then continue with the task.
50
+
51
+ ```bash
52
+ root="${KLOK_GIT_LOCAL_WORKSPACE:-$HOME/devklok}"
53
+ repo_rel="klok/integration/klok-api-konvoj" # full path, every subgroup kept
54
+ task_id="$(date +%Y%m%d)-auth-$(openssl rand -hex 4)"
55
+ canonical="$root/$repo_rel"
56
+ task_tree="$root/worktrees/$repo_rel/$task_id"
57
+ task_branch="agent/$task_id"
58
+
59
+ git -C "$canonical" status --short
60
+ git -C "$canonical" worktree list --porcelain
61
+ git -C "$canonical" fetch origin
62
+ base_commit="$(git -C "$canonical" rev-parse --verify 'origin/main^{commit}')" # after verifying the default branch
63
+ mkdir -p "$(dirname "$task_tree")"
64
+ git -C "$canonical" worktree add -b "$task_branch" "$task_tree" "$base_commit"
65
+ cd "$task_tree" && git rev-parse --show-toplevel && git branch --show-current
66
+ ```
67
+
68
+ Run the steps one at a time and stop at the first failure; this is a procedure, not an installer. Validate any
69
+ path that comes from the task: no absolute repository relative paths, no `..`, no symlink out of the workspace.
70
+ Never use force flags to get past a worktree ownership check.
71
+
72
+ A harness with its own worktree feature (for example Claude Code's `EnterWorktree`, which defaults to
73
+ `.claude/worktrees/`) is used only if it can be pointed at the contract path; otherwise create the worktree with
74
+ the commands above and open that directory. If the tool cannot read or write the worktree, report that
75
+ limitation instead of editing the canonical checkout.
76
+
77
+ ## During the task
78
+
79
+ * Keep source edits, formatting, generated source, builds and tests inside the worktree. Recreate dependencies
80
+ there with the project's documented setup (`npm ci`, `./gradlew build`); do not copy or symlink `.env`,
81
+ credentials, `node_modules`, build directories or other ignored files from another checkout.
82
+ * Git objects, refs, remotes and configuration are shared with the canonical checkout. Do not change shared
83
+ settings, another task's branch, or force check out a branch attached to another worktree.
84
+ * A worktree is not a sandbox: use isolated ports, containers, databases and output directories where parallel
85
+ tasks would collide, and never write through absolute paths or symlinks into another checkout.
86
+ * Sibling repository dependencies (`includeBuild`, relative paths) point at canonical checkouts by default. Give
87
+ them explicit paths to the task worktrees when the task changes several repositories, and never let writable
88
+ build output land in a canonical checkout.
89
+ * A task that spans repositories uses the same task id under each repository's path and records repository, base
90
+ commit, branch and worktree for each.
91
+
92
+ ## Resume and handoff
93
+
94
+ Resume an existing task worktree only when the user or the handoff names it and no other writer is active.
95
+ Verify its path, branch, shared repository (`git rev-parse --git-common-dir`), status and unpushed commits. A
96
+ matching directory or branch name alone does not prove ownership; when unclear, create a new worktree. Legacy
97
+ worktrees (for example under `.claude/worktrees/`) are reported with their paths and left for an explicit
98
+ cleanup task, never moved or deleted on the way.
99
+
100
+ ## Finish
101
+
102
+ 1. Validate the requested behaviour, inspect the final diff, and commit to the task branch: stage specific paths,
103
+ check for secrets and unrelated files. Respect an explicit request to leave changes uncommitted.
104
+ 2. Push and open a merge request ready for review when the task or the established workflow allows it (see the
105
+ `klok-release` skill for what to check afterwards). Do not invent approval requirements and do not assume a
106
+ permission that was not given.
107
+ 3. Report: canonical repository path, worktree path, branch, commits, tests run, push and merge request status,
108
+ remaining changes and the next action. Unfinished work gets a clear handoff and keeps its worktree.
109
+
110
+ ## Cleanup
111
+
112
+ Keep the worktree until the branch is merged or cleanup is asked for; a clean `git status` does not mean the
113
+ commits are pushed or merged. Before removing, verify that no process or session uses it, look at tracked,
114
+ untracked and ignored files, and confirm the commits are merged or preserved as agreed. Then
115
+ `git -C <canonical> worktree remove <exact path>` without force. Never `rm -rf` a worktree as routine cleanup,
116
+ and never delete a branch merely because a session ended.