Skip to content

Commit d841132

Browse files
committed
feat(agents): add managed git worktree lifecycle (create/provision/snapshot/restore/GC)
Centralized managed worktrees under <state-dir>/worktrees/<repo-fingerprint>/<name> with branch-per-task (openclaw/<name>), .worktreeinclude provisioning, an optional .openclaw/worktree-setup.sh repo hook, and a SQLite registry in the shared state DB. Removal always snapshots the tree (untracked included, gitignored excluded) to refs/openclaw/snapshots/<id>; restore rebuilds the branch at the original commit with the snapshot content as uncommitted state. Lossless run-end cleanup, 7-day idle GC for run-owned worktrees (manual exempt), orphan reconciliation, 30-day snapshot retention. Surfaces: worktrees.* gateway RPC (operator.admin mutations), openclaw worktrees CLI, Control UI page, plugin-SDK facade + Workboard kind:"worktree" materialization. E2E-verified on Testbox: full create->work->remove->restore->gc lifecycle.
1 parent d6f0747 commit d841132

129 files changed

Lines changed: 4026 additions & 279 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,7 @@ public struct AgentParams: Codable, Sendable {
776776
public let timeout: Int?
777777
public let besteffortdeliver: Bool?
778778
public let lane: String?
779+
public let cwd: String?
779780
public let cleanupbundlemcponrunend: Bool?
780781
public let modelrun: Bool?
781782
public let promptmode: AnyCodable?
@@ -818,6 +819,7 @@ public struct AgentParams: Codable, Sendable {
818819
timeout: Int?,
819820
besteffortdeliver: Bool?,
820821
lane: String?,
822+
cwd: String?,
821823
cleanupbundlemcponrunend: Bool?,
822824
modelrun: Bool?,
823825
promptmode: AnyCodable?,
@@ -859,6 +861,7 @@ public struct AgentParams: Codable, Sendable {
859861
self.timeout = timeout
860862
self.besteffortdeliver = besteffortdeliver
861863
self.lane = lane
864+
self.cwd = cwd
862865
self.cleanupbundlemcponrunend = cleanupbundlemcponrunend
863866
self.modelrun = modelrun
864867
self.promptmode = promptmode
@@ -902,6 +905,7 @@ public struct AgentParams: Codable, Sendable {
902905
case timeout
903906
case besteffortdeliver = "bestEffortDeliver"
904907
case lane
908+
case cwd
905909
case cleanupbundlemcponrunend = "cleanupBundleMcpOnRunEnd"
906910
case modelrun = "modelRun"
907911
case promptmode = "promptMode"
@@ -1023,6 +1027,180 @@ public struct WakeParams: Codable, Sendable {
10231027
}
10241028
}
10251029

1030+
public struct WorktreeRecord: Codable, Sendable {
1031+
public let id: String
1032+
public let name: String
1033+
public let repofingerprint: String
1034+
public let reporoot: String
1035+
public let path: String
1036+
public let branch: String
1037+
public let baseref: String
1038+
public let ownerkind: String
1039+
public let ownerid: String?
1040+
public let snapshotref: String?
1041+
public let createdat: Int
1042+
public let lastactiveat: Int
1043+
public let removedat: Int?
1044+
1045+
public init(
1046+
id: String,
1047+
name: String,
1048+
repofingerprint: String,
1049+
reporoot: String,
1050+
path: String,
1051+
branch: String,
1052+
baseref: String,
1053+
ownerkind: String,
1054+
ownerid: String?,
1055+
snapshotref: String?,
1056+
createdat: Int,
1057+
lastactiveat: Int,
1058+
removedat: Int?)
1059+
{
1060+
self.id = id
1061+
self.name = name
1062+
self.repofingerprint = repofingerprint
1063+
self.reporoot = reporoot
1064+
self.path = path
1065+
self.branch = branch
1066+
self.baseref = baseref
1067+
self.ownerkind = ownerkind
1068+
self.ownerid = ownerid
1069+
self.snapshotref = snapshotref
1070+
self.createdat = createdat
1071+
self.lastactiveat = lastactiveat
1072+
self.removedat = removedat
1073+
}
1074+
1075+
private enum CodingKeys: String, CodingKey {
1076+
case id
1077+
case name
1078+
case repofingerprint = "repoFingerprint"
1079+
case reporoot = "repoRoot"
1080+
case path
1081+
case branch
1082+
case baseref = "baseRef"
1083+
case ownerkind = "ownerKind"
1084+
case ownerid = "ownerId"
1085+
case snapshotref = "snapshotRef"
1086+
case createdat = "createdAt"
1087+
case lastactiveat = "lastActiveAt"
1088+
case removedat = "removedAt"
1089+
}
1090+
}
1091+
1092+
public struct WorktreesListParams: Codable, Sendable {}
1093+
1094+
public struct WorktreesListResult: Codable, Sendable {
1095+
public let worktrees: [WorktreeRecord]
1096+
1097+
public init(
1098+
worktrees: [WorktreeRecord])
1099+
{
1100+
self.worktrees = worktrees
1101+
}
1102+
1103+
private enum CodingKeys: String, CodingKey {
1104+
case worktrees
1105+
}
1106+
}
1107+
1108+
public struct WorktreesCreateParams: Codable, Sendable {
1109+
public let reporoot: String
1110+
public let name: String?
1111+
public let baseref: String?
1112+
1113+
public init(
1114+
reporoot: String,
1115+
name: String?,
1116+
baseref: String?)
1117+
{
1118+
self.reporoot = reporoot
1119+
self.name = name
1120+
self.baseref = baseref
1121+
}
1122+
1123+
private enum CodingKeys: String, CodingKey {
1124+
case reporoot = "repoRoot"
1125+
case name
1126+
case baseref = "baseRef"
1127+
}
1128+
}
1129+
1130+
public struct WorktreesRemoveParams: Codable, Sendable {
1131+
public let id: String
1132+
public let force: Bool?
1133+
1134+
public init(
1135+
id: String,
1136+
force: Bool?)
1137+
{
1138+
self.id = id
1139+
self.force = force
1140+
}
1141+
1142+
private enum CodingKeys: String, CodingKey {
1143+
case id
1144+
case force
1145+
}
1146+
}
1147+
1148+
public struct WorktreesRemoveResult: Codable, Sendable {
1149+
public let removed: Bool
1150+
public let snapshotref: String?
1151+
1152+
public init(
1153+
removed: Bool,
1154+
snapshotref: String?)
1155+
{
1156+
self.removed = removed
1157+
self.snapshotref = snapshotref
1158+
}
1159+
1160+
private enum CodingKeys: String, CodingKey {
1161+
case removed
1162+
case snapshotref = "snapshotRef"
1163+
}
1164+
}
1165+
1166+
public struct WorktreesRestoreParams: Codable, Sendable {
1167+
public let id: String
1168+
1169+
public init(
1170+
id: String)
1171+
{
1172+
self.id = id
1173+
}
1174+
1175+
private enum CodingKeys: String, CodingKey {
1176+
case id
1177+
}
1178+
}
1179+
1180+
public struct WorktreesGcParams: Codable, Sendable {}
1181+
1182+
public struct WorktreesGcResult: Codable, Sendable {
1183+
public let removed: [String]
1184+
public let orphansdeleted: Int
1185+
public let snapshotspruned: Int
1186+
1187+
public init(
1188+
removed: [String],
1189+
orphansdeleted: Int,
1190+
snapshotspruned: Int)
1191+
{
1192+
self.removed = removed
1193+
self.orphansdeleted = orphansdeleted
1194+
self.snapshotspruned = snapshotspruned
1195+
}
1196+
1197+
private enum CodingKeys: String, CodingKey {
1198+
case removed
1199+
case orphansdeleted = "orphansDeleted"
1200+
case snapshotspruned = "snapshotsPruned"
1201+
}
1202+
}
1203+
10261204
public struct NodePairRequestParams: Codable, Sendable {
10271205
public let nodeid: String
10281206
public let displayname: String?

docs/concepts/managed-worktrees.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
summary: "Run agent tasks in isolated git checkouts with automatic snapshots and cleanup"
3+
read_when:
4+
- You want an isolated branch and checkout for an agent task
5+
- You are configuring Workboard cards with worktree workspaces
6+
- You need to restore or clean up an OpenClaw-managed worktree
7+
title: "Managed worktrees"
8+
---
9+
10+
Managed worktrees give an agent task its own git branch and checkout without placing temporary directories inside the source repository. OpenClaw creates them under its state directory, records them in the shared state database, and snapshots their tracked and non-ignored untracked contents before removal.
11+
12+
## Layout and names
13+
14+
Each worktree lives at:
15+
16+
```text
17+
<openclaw-state-dir>/worktrees/<repo-fingerprint>/<name>
18+
```
19+
20+
The repository fingerprint is the first 16 hexadecimal characters of a SHA-256 hash over the canonical git common directory and origin URL. A supplied name must match `[a-z0-9][a-z0-9-]{0,63}`. Without a name, OpenClaw generates `wt-` followed by eight random hexadecimal characters.
21+
22+
OpenClaw creates branch `openclaw/<name>` at the requested base ref. Without a base ref, it fetches `origin`, uses the remote default branch when available, and falls back to local `HEAD` when the repository is offline or has no usable remote.
23+
24+
## Provision ignored files
25+
26+
Add `.worktreeinclude` at the source repository root to copy selected ignored, untracked files into a new worktree. The file uses gitignore-pattern syntax, one pattern per line, with `#` comments:
27+
28+
```gitignore
29+
.env.local
30+
fixtures/generated/**
31+
```
32+
33+
Only files reported by git as both ignored and untracked are eligible. Tracked files are already present through git and are never copied by this step. OpenClaw does not overwrite destination files or follow symlinked directories, and it preserves copied file modes.
34+
35+
## Run repository setup
36+
37+
If `.openclaw/worktree-setup.sh` exists in the source repository and is executable, OpenClaw runs it with the new worktree as its current directory. The script receives:
38+
39+
```text
40+
OPENCLAW_SOURCE_TREE_PATH=<source checkout>
41+
OPENCLAW_WORKTREE_PATH=<managed worktree>
42+
```
43+
44+
A nonzero exit aborts creation and removes the new worktree and branch. This is a repository-local contract; there is no OpenClaw config key for it.
45+
46+
## Snapshots, cleanup, and restore
47+
48+
Removal first creates a synthetic commit containing tracked and non-ignored untracked files, and pins it at `refs/openclaw/snapshots/<id>`. Gitignored files are excluded from the repository object database; files selected by `.worktreeinclude` are copied again during restore. If snapshot creation fails, removal stops. An explicit force delete can continue without a snapshot.
49+
50+
OpenClaw applies these cleanup rules:
51+
52+
- At run end, it removes a worktree only when `git status --porcelain` is empty and `git log HEAD --not --remotes --oneline` finds no unpushed commits. Otherwise it only releases the activity lock.
53+
- Hourly cleanup snapshots and removes unlocked Workboard- and session-owned worktrees idle for more than 7 days, even when dirty. Manual worktrees are never automatically removed.
54+
- Snapshot records remain restorable for 30 days. Cleanup then deletes the snapshot ref and registry row.
55+
- A live OpenClaw process lock and any foreign or unrecognized git worktree lock protect a worktree from garbage collection.
56+
57+
Restore recreates `openclaw/<name>` at the original pre-snapshot commit, then rebuilds the snapshot differences as unstaged modifications and untracked files. This keeps the synthetic snapshot commit out of branch history. The snapshot ref remains recorded as provenance.
58+
59+
## CLI
60+
61+
```bash
62+
openclaw worktrees list [--json]
63+
openclaw worktrees create <repo-root> [--name <name>] [--base-ref <ref>] [--json]
64+
openclaw worktrees remove <id> [--force] [--json]
65+
openclaw worktrees restore <id> [--json]
66+
openclaw worktrees gc [--json]
67+
```
68+
69+
The Control UI **Worktrees** page provides the same list, delete, restore, and cleanup actions.
70+
71+
## Gateway methods
72+
73+
| Method | Purpose |
74+
| ------------------- | --------------------------------------------- |
75+
| `worktrees.list` | List active and restorable worktree records. |
76+
| `worktrees.create` | Create or reuse a named managed worktree. |
77+
| `worktrees.remove` | Snapshot and remove a worktree. |
78+
| `worktrees.restore` | Restore a removed worktree from its snapshot. |
79+
| `worktrees.gc` | Run idle, orphan, and retention cleanup now. |
80+
81+
`worktrees.list` requires `operator.read`. Mutating methods require `operator.admin`.
82+
83+
## Workboard workspaces
84+
85+
The bundled [Workboard plugin](/plugins/workboard) can materialize a card workspace as a managed worktree:
86+
87+
```json
88+
{
89+
"kind": "worktree",
90+
"path": "/absolute/path/to/source-checkout",
91+
"branch": "main"
92+
}
93+
```
94+
95+
`path` identifies the source git checkout. `branch` is optional and becomes the base ref. When dispatch starts the card's worker, Workboard creates or reuses `wb-<card-id>`, runs the subagent with the managed checkout as its working directory, and writes the resolved path and branch back to the card. Gateway-triggered materialization requires `operator.admin`. On run end, Workboard removes the checkout only when it is provably lossless; dirty work or unpushed commits remain available.
96+
97+
Sandboxed embedded agents currently reject a task working directory outside their configured agent workspace. Use an unsandboxed target agent for Workboard managed-worktree cards until the sandbox runtime supports an additive checkout mount.

docs/docs.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,7 @@
11941194
"concepts/context",
11951195
"concepts/context-engine",
11961196
"concepts/agent-workspace",
1197+
"concepts/managed-worktrees",
11971198
"concepts/soul",
11981199
"concepts/oauth",
11991200
"start/bootstrapping",

extensions/workboard/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { definePluginEntry } from "./api.js";
33
import { registerWorkboardGatewayMethods } from "./runtime-api.js";
44
import { registerWorkboardCommand } from "./src/command.js";
5+
import { cleanupWorkboardRunWorktree } from "./src/dispatcher.js";
56
import { WorkboardStore } from "./src/store.js";
67
import { createWorkboardTools } from "./src/tools.js";
78

@@ -13,6 +14,15 @@ export default definePluginEntry({
1314
const store = WorkboardStore.openSqlite();
1415
registerWorkboardGatewayMethods({ api, store });
1516
registerWorkboardCommand({ api, store });
17+
api.on("subagent_ended", async (event) => {
18+
if (event.runId) {
19+
await cleanupWorkboardRunWorktree({
20+
store,
21+
worktrees: api.runtime.worktrees,
22+
runId: event.runId,
23+
});
24+
}
25+
});
1626
api.registerCli(
1727
async ({ program }) => {
1828
const { registerWorkboardCli } = await import("./src/cli.js");

0 commit comments

Comments
 (0)