@phren/cli 0.3.67 → 0.3.69
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.
- package/dist/bridge/agent-hook-callback.js +11 -2
- package/dist/bridge/agent-hooks.d.ts +11 -1
- package/dist/bridge/agent-hooks.js +259 -40
- package/dist/bridge/changes.d.ts +2 -2
- package/dist/bridge/changes.js +11 -9
- package/dist/bridge/checkpoints.d.ts +24 -0
- package/dist/bridge/checkpoints.js +423 -0
- package/dist/bridge/claude-input-state.d.ts +14 -0
- package/dist/bridge/claude-input-state.js +58 -0
- package/dist/bridge/claude-queue.js +5 -2
- package/dist/bridge/claude-suggestion.d.ts +3 -0
- package/dist/bridge/claude-suggestion.js +10 -0
- package/dist/bridge/codex-threads.d.ts +7 -1
- package/dist/bridge/codex-threads.js +185 -56
- package/dist/bridge/conversation-search.d.ts +20 -0
- package/dist/bridge/conversation-search.js +928 -0
- package/dist/bridge/git-watch.d.ts +46 -0
- package/dist/bridge/git-watch.js +351 -0
- package/dist/bridge/git-worktree-remove.d.ts +44 -0
- package/dist/bridge/git-worktree-remove.js +140 -0
- package/dist/bridge/git-worktrees.d.ts +14 -0
- package/dist/bridge/git-worktrees.js +7 -3
- package/dist/bridge/git-write-policy.js +1 -1
- package/dist/bridge/gitboy-webhooks.d.ts +3 -1
- package/dist/bridge/gitboy-webhooks.js +26 -2
- package/dist/bridge/herdr.d.ts +6 -1
- package/dist/bridge/herdr.js +56 -10
- package/dist/bridge/hook-fast.js +13 -1
- package/dist/bridge/input-classification.d.ts +33 -0
- package/dist/bridge/input-classification.js +84 -0
- package/dist/bridge/pane-status.d.ts +3 -3
- package/dist/bridge/pane-status.js +16 -8
- package/dist/bridge/prompt-deliveries.d.ts +30 -4
- package/dist/bridge/prompt-deliveries.js +365 -13
- package/dist/bridge/prompt-identity.d.ts +8 -0
- package/dist/bridge/prompt-identity.js +15 -0
- package/dist/bridge/request-origin.js +2 -1
- package/dist/bridge/schedule-events.d.ts +17 -0
- package/dist/bridge/schedule-events.js +53 -0
- package/dist/bridge/schedule-format.d.ts +22 -2
- package/dist/bridge/schedule-format.js +40 -5
- package/dist/bridge/schedule-update.js +2 -1
- package/dist/bridge/schedule-watch.js +15 -5
- package/dist/bridge/schedules.d.ts +9 -0
- package/dist/bridge/schedules.js +120 -7
- package/dist/bridge/server-pane-routes.d.ts +7 -8
- package/dist/bridge/server-pane-routes.js +186 -82
- package/dist/bridge/server-routes.d.ts +26 -2
- package/dist/bridge/server-routes.js +180 -56
- package/dist/bridge/server-stream.js +66 -15
- package/dist/bridge/server.js +25 -7
- package/dist/bridge/session-activity.d.ts +49 -4
- package/dist/bridge/session-activity.js +47 -0
- package/dist/bridge/session-evidence.d.ts +18 -0
- package/dist/bridge/session-evidence.js +72 -0
- package/dist/bridge/session-seen.d.ts +23 -0
- package/dist/bridge/session-seen.js +69 -0
- package/dist/bridge/tab-activity.d.ts +26 -1
- package/dist/bridge/tab-activity.js +64 -9
- package/dist/bridge/terminal-resolve.d.ts +20 -0
- package/dist/bridge/terminal-resolve.js +145 -0
- package/dist/bridge/terminal-tmux.d.ts +8 -3
- package/dist/bridge/terminal-tmux.js +59 -7
- package/dist/bridge/transcript-claude.d.ts +12 -2
- package/dist/bridge/transcript-claude.js +91 -13
- package/dist/bridge/transcript-index.d.ts +4 -2
- package/dist/bridge/transcript-index.js +6 -5
- package/dist/bridge/transcript-preview.d.ts +16 -1
- package/dist/bridge/transcript-preview.js +171 -27
- package/dist/bridge/transcripts.d.ts +41 -1
- package/dist/bridge/transcripts.js +181 -23
- package/dist/bridge/turn-queue-pane.d.ts +6 -0
- package/dist/bridge/turn-queue-pane.js +30 -0
- package/dist/bridge/turn-queue.d.ts +86 -0
- package/dist/bridge/turn-queue.js +280 -0
- package/dist/bridge/turn-records.d.ts +18 -0
- package/dist/bridge/turn-records.js +77 -5
- package/dist/bridge-hook.mjs +7074 -3173
- package/dist/cli/config-shared.js +2 -1
- package/dist/cli/schedules.js +6 -1
- package/dist/content/secrets.js +1 -1
- package/dist/modules/registry.js +12 -11
- package/package.json +1 -1
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export declare const GIT_WATCH_DEBOUNCE_MS = 1000;
|
|
2
|
+
export declare const GIT_WATCH_PING_MS = 20000;
|
|
3
|
+
export declare const GIT_WATCH_POLL_MS = 3000;
|
|
4
|
+
export declare const GIT_WATCH_GRACE_MS = 30000;
|
|
5
|
+
export declare const GIT_WATCH_MAX = 16;
|
|
6
|
+
export interface GitWatchClient {
|
|
7
|
+
readonly readyState: number;
|
|
8
|
+
send(data: string): void;
|
|
9
|
+
close(code?: number, reason?: string): void;
|
|
10
|
+
once(event: "close" | "error", listener: () => void): unknown;
|
|
11
|
+
}
|
|
12
|
+
export interface GitWatchOptions {
|
|
13
|
+
platform?: NodeJS.Platform;
|
|
14
|
+
pollMs?: number;
|
|
15
|
+
debounceMs?: number;
|
|
16
|
+
pingMs?: number;
|
|
17
|
+
graceMs?: number;
|
|
18
|
+
maxWatchers?: number;
|
|
19
|
+
now?: () => number;
|
|
20
|
+
}
|
|
21
|
+
export declare class GitWatch {
|
|
22
|
+
private readonly watchers;
|
|
23
|
+
private closed;
|
|
24
|
+
private readonly platform;
|
|
25
|
+
private readonly pollMs;
|
|
26
|
+
private readonly debounceMs;
|
|
27
|
+
private readonly pingMs;
|
|
28
|
+
private readonly graceMs;
|
|
29
|
+
private readonly maxWatchers;
|
|
30
|
+
private readonly now;
|
|
31
|
+
constructor(options?: GitWatchOptions);
|
|
32
|
+
stream(client: GitWatchClient, url: URL): Promise<void>;
|
|
33
|
+
watch(client: GitWatchClient, cwd: string): Promise<void>;
|
|
34
|
+
close(): void;
|
|
35
|
+
private send;
|
|
36
|
+
private fail;
|
|
37
|
+
private failWatcher;
|
|
38
|
+
private attach;
|
|
39
|
+
private detach;
|
|
40
|
+
private idle;
|
|
41
|
+
private ensure;
|
|
42
|
+
private closedError;
|
|
43
|
+
private create;
|
|
44
|
+
private changed;
|
|
45
|
+
private stop;
|
|
46
|
+
}
|
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readFileSync, watch as watchFs } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { WebSocket } from "ws";
|
|
5
|
+
import { logger } from "../logger.js";
|
|
6
|
+
import { validateTarget } from "./herdr.js";
|
|
7
|
+
import { git, gitRoot } from "./projects.js";
|
|
8
|
+
import { BridgeError, errorCode, targetFromURL } from "./protocol.js";
|
|
9
|
+
import { gitRepository } from "./server-pane-routes.js";
|
|
10
|
+
export const GIT_WATCH_DEBOUNCE_MS = 1_000;
|
|
11
|
+
export const GIT_WATCH_PING_MS = 20_000;
|
|
12
|
+
export const GIT_WATCH_POLL_MS = 3_000;
|
|
13
|
+
export const GIT_WATCH_GRACE_MS = 30_000;
|
|
14
|
+
export const GIT_WATCH_MAX = 16;
|
|
15
|
+
function gitDirectory(root) {
|
|
16
|
+
const dotGit = path.join(root, ".git");
|
|
17
|
+
try {
|
|
18
|
+
const link = /^gitdir:\s*(.+)$/m.exec(readFileSync(dotGit, "utf8"));
|
|
19
|
+
if (link)
|
|
20
|
+
return path.resolve(root, link[1].trim());
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return dotGit;
|
|
24
|
+
}
|
|
25
|
+
return dotGit;
|
|
26
|
+
}
|
|
27
|
+
function isNoise(name) {
|
|
28
|
+
return name === ".git/objects" || name.startsWith(".git/objects/")
|
|
29
|
+
|| name === ".git/logs" || name.startsWith(".git/logs/");
|
|
30
|
+
}
|
|
31
|
+
function startNative(root, onChange, onError) {
|
|
32
|
+
const handles = [];
|
|
33
|
+
const rootWatch = watchFs(root, { recursive: true }, (_event, filename) => {
|
|
34
|
+
const name = filename;
|
|
35
|
+
if (name && isNoise(name))
|
|
36
|
+
return;
|
|
37
|
+
onChange();
|
|
38
|
+
});
|
|
39
|
+
rootWatch.on("error", onError);
|
|
40
|
+
handles.push(rootWatch);
|
|
41
|
+
const gitDir = gitDirectory(root);
|
|
42
|
+
const externalGitDir = gitDir !== path.join(root, ".git");
|
|
43
|
+
try {
|
|
44
|
+
const indexWatch = watchFs(gitDir, (_event, filename) => {
|
|
45
|
+
const name = filename;
|
|
46
|
+
if (name === "index" || name === "HEAD")
|
|
47
|
+
onChange();
|
|
48
|
+
});
|
|
49
|
+
indexWatch.on("error", onError);
|
|
50
|
+
handles.push(indexWatch);
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
if (externalGitDir) {
|
|
54
|
+
for (const handle of handles)
|
|
55
|
+
handle.close();
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return { ready: Promise.resolve(), close: () => { for (const handle of handles)
|
|
60
|
+
handle.close(); } };
|
|
61
|
+
}
|
|
62
|
+
function startPolling(root, onChange, onError, pollMs) {
|
|
63
|
+
let stopped = false, timer, current;
|
|
64
|
+
const read = async () => createHash("sha256")
|
|
65
|
+
.update(await git(root, "--no-optional-locks", "status", "--porcelain=v2", "-z"))
|
|
66
|
+
.digest("hex");
|
|
67
|
+
const tick = async () => {
|
|
68
|
+
if (stopped)
|
|
69
|
+
return;
|
|
70
|
+
try {
|
|
71
|
+
const next = await read();
|
|
72
|
+
if (current !== undefined && next !== current)
|
|
73
|
+
onChange();
|
|
74
|
+
current = next;
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
stopped = true;
|
|
78
|
+
onError(error);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (!stopped)
|
|
82
|
+
timer = setTimeout(() => { void tick(); }, pollMs);
|
|
83
|
+
};
|
|
84
|
+
const ready = read().then(value => {
|
|
85
|
+
current = value;
|
|
86
|
+
if (!stopped)
|
|
87
|
+
timer = setTimeout(() => { void tick(); }, pollMs);
|
|
88
|
+
}, error => {
|
|
89
|
+
stopped = true;
|
|
90
|
+
throw error;
|
|
91
|
+
});
|
|
92
|
+
return {
|
|
93
|
+
ready,
|
|
94
|
+
close: () => {
|
|
95
|
+
stopped = true;
|
|
96
|
+
if (timer)
|
|
97
|
+
clearTimeout(timer);
|
|
98
|
+
timer = undefined;
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
export class GitWatch {
|
|
103
|
+
watchers = new Map();
|
|
104
|
+
closed = false;
|
|
105
|
+
platform;
|
|
106
|
+
pollMs;
|
|
107
|
+
debounceMs;
|
|
108
|
+
pingMs;
|
|
109
|
+
graceMs;
|
|
110
|
+
maxWatchers;
|
|
111
|
+
now;
|
|
112
|
+
constructor(options = {}) {
|
|
113
|
+
this.platform = options.platform ?? process.platform;
|
|
114
|
+
this.pollMs = options.pollMs ?? GIT_WATCH_POLL_MS;
|
|
115
|
+
this.debounceMs = options.debounceMs ?? GIT_WATCH_DEBOUNCE_MS;
|
|
116
|
+
this.pingMs = options.pingMs ?? GIT_WATCH_PING_MS;
|
|
117
|
+
this.graceMs = options.graceMs ?? GIT_WATCH_GRACE_MS;
|
|
118
|
+
this.maxWatchers = options.maxWatchers ?? GIT_WATCH_MAX;
|
|
119
|
+
this.now = options.now ?? Date.now;
|
|
120
|
+
}
|
|
121
|
+
async stream(client, url) {
|
|
122
|
+
try {
|
|
123
|
+
if (this.closed)
|
|
124
|
+
return;
|
|
125
|
+
const target = targetFromURL(url);
|
|
126
|
+
const pane = await validateTarget(target);
|
|
127
|
+
if (this.closed)
|
|
128
|
+
return;
|
|
129
|
+
const cwd = await gitRepository(pane, target, url.searchParams.get("child") ?? undefined, url.searchParams.get("worktree") ?? undefined);
|
|
130
|
+
if (this.closed)
|
|
131
|
+
return;
|
|
132
|
+
await this.watch(client, cwd);
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
if (!this.closed)
|
|
136
|
+
this.fail(client, error);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
async watch(client, cwd) {
|
|
140
|
+
try {
|
|
141
|
+
if (this.closed)
|
|
142
|
+
return;
|
|
143
|
+
const root = await gitRoot(cwd);
|
|
144
|
+
if (this.closed)
|
|
145
|
+
return;
|
|
146
|
+
if (!root)
|
|
147
|
+
throw new BridgeError(409, "This pane is not in a Git repository.", { code: "git-not-repository" });
|
|
148
|
+
await this.attach(client, root);
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
if (!this.closed)
|
|
152
|
+
this.fail(client, error);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
close() {
|
|
156
|
+
if (this.closed)
|
|
157
|
+
return;
|
|
158
|
+
this.closed = true;
|
|
159
|
+
for (const watcher of [...this.watchers.values()]) {
|
|
160
|
+
for (const subscriber of watcher.subscribers)
|
|
161
|
+
if (subscriber.ping)
|
|
162
|
+
clearInterval(subscriber.ping);
|
|
163
|
+
watcher.subscribers.clear();
|
|
164
|
+
this.stop(watcher);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
send(client, frame) {
|
|
168
|
+
if (client.readyState !== WebSocket.OPEN)
|
|
169
|
+
return false;
|
|
170
|
+
try {
|
|
171
|
+
client.send(JSON.stringify(frame));
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
fail(client, error) {
|
|
179
|
+
const code = errorCode(error) ?? "git-watch-failed";
|
|
180
|
+
const message = error instanceof Error ? error.message : "This checkout cannot be watched.";
|
|
181
|
+
this.send(client, { type: "error", code, message });
|
|
182
|
+
client.close(1011);
|
|
183
|
+
}
|
|
184
|
+
failWatcher(watcher, error) {
|
|
185
|
+
if (watcher.closed)
|
|
186
|
+
return;
|
|
187
|
+
watcher.failure = error;
|
|
188
|
+
const code = errorCode(error) ?? "git-watch-failed";
|
|
189
|
+
const message = error instanceof Error ? error.message : "This checkout cannot be watched.";
|
|
190
|
+
logger.warn("git-watch", `watch failed for ${watcher.root}: ${message}`);
|
|
191
|
+
const subscribers = [...watcher.subscribers];
|
|
192
|
+
watcher.subscribers.clear();
|
|
193
|
+
for (const subscriber of subscribers) {
|
|
194
|
+
if (subscriber.ping)
|
|
195
|
+
clearInterval(subscriber.ping);
|
|
196
|
+
subscriber.ping = undefined;
|
|
197
|
+
this.send(subscriber.client, { type: "error", code, message });
|
|
198
|
+
try {
|
|
199
|
+
subscriber.client.close(1011);
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
this.stop(watcher);
|
|
206
|
+
}
|
|
207
|
+
async attach(client, root) {
|
|
208
|
+
if (this.closed || client.readyState !== WebSocket.OPEN)
|
|
209
|
+
return;
|
|
210
|
+
const watcher = await this.ensure(root);
|
|
211
|
+
if (this.closed || watcher.closed) {
|
|
212
|
+
this.stop(watcher);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
if (client.readyState !== WebSocket.OPEN) {
|
|
216
|
+
this.idle(watcher);
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
const subscriber = { client };
|
|
220
|
+
watcher.subscribers.add(subscriber);
|
|
221
|
+
watcher.idleSince = undefined;
|
|
222
|
+
if (watcher.stopTimer) {
|
|
223
|
+
clearTimeout(watcher.stopTimer);
|
|
224
|
+
watcher.stopTimer = undefined;
|
|
225
|
+
}
|
|
226
|
+
subscriber.ping = setInterval(() => {
|
|
227
|
+
if (!this.send(client, { type: "ping" }))
|
|
228
|
+
this.detach(watcher, subscriber);
|
|
229
|
+
}, this.pingMs);
|
|
230
|
+
const leave = () => this.detach(watcher, subscriber);
|
|
231
|
+
client.once("close", leave);
|
|
232
|
+
client.once("error", leave);
|
|
233
|
+
}
|
|
234
|
+
detach(watcher, subscriber) {
|
|
235
|
+
if (!watcher.subscribers.delete(subscriber))
|
|
236
|
+
return;
|
|
237
|
+
if (subscriber.ping)
|
|
238
|
+
clearInterval(subscriber.ping);
|
|
239
|
+
subscriber.ping = undefined;
|
|
240
|
+
this.idle(watcher);
|
|
241
|
+
}
|
|
242
|
+
idle(watcher) {
|
|
243
|
+
if (watcher.subscribers.size || watcher.stopTimer || watcher.closed)
|
|
244
|
+
return;
|
|
245
|
+
watcher.idleSince = this.now();
|
|
246
|
+
watcher.stopTimer = setTimeout(() => {
|
|
247
|
+
watcher.stopTimer = undefined;
|
|
248
|
+
if (!watcher.subscribers.size)
|
|
249
|
+
this.stop(watcher);
|
|
250
|
+
}, this.graceMs);
|
|
251
|
+
}
|
|
252
|
+
async ensure(root) {
|
|
253
|
+
if (this.closed)
|
|
254
|
+
throw new BridgeError(503, "The Hook is shutting down.", { code: "git-watch-closed" });
|
|
255
|
+
const existing = this.watchers.get(root);
|
|
256
|
+
if (existing) {
|
|
257
|
+
await existing.ready;
|
|
258
|
+
if (this.closed)
|
|
259
|
+
throw new BridgeError(503, "The Hook is shutting down.", { code: "git-watch-closed" });
|
|
260
|
+
if (existing.closed)
|
|
261
|
+
throw this.closedError(existing);
|
|
262
|
+
return existing;
|
|
263
|
+
}
|
|
264
|
+
if (this.watchers.size >= this.maxWatchers) {
|
|
265
|
+
const idle = [...this.watchers.values()]
|
|
266
|
+
.filter(watcher => watcher.subscribers.size === 0 && watcher.idleSince !== undefined)
|
|
267
|
+
.sort((a, b) => (a.idleSince ?? 0) - (b.idleSince ?? 0))[0];
|
|
268
|
+
if (!idle)
|
|
269
|
+
throw new BridgeError(503, `This Hook is already watching ${this.maxWatchers} checkouts.`, { code: "too-many-watchers" });
|
|
270
|
+
this.stop(idle);
|
|
271
|
+
}
|
|
272
|
+
if (this.closed)
|
|
273
|
+
throw new BridgeError(503, "The Hook is shutting down.", { code: "git-watch-closed" });
|
|
274
|
+
const watcher = this.create(root);
|
|
275
|
+
this.watchers.set(root, watcher);
|
|
276
|
+
try {
|
|
277
|
+
await watcher.ready;
|
|
278
|
+
}
|
|
279
|
+
catch (error) {
|
|
280
|
+
this.stop(watcher);
|
|
281
|
+
throw error;
|
|
282
|
+
}
|
|
283
|
+
if (this.closed) {
|
|
284
|
+
this.stop(watcher);
|
|
285
|
+
throw new BridgeError(503, "The Hook is shutting down.", { code: "git-watch-closed" });
|
|
286
|
+
}
|
|
287
|
+
if (watcher.closed) {
|
|
288
|
+
this.stop(watcher);
|
|
289
|
+
throw this.closedError(watcher);
|
|
290
|
+
}
|
|
291
|
+
return watcher;
|
|
292
|
+
}
|
|
293
|
+
closedError(watcher) {
|
|
294
|
+
if (watcher.failure instanceof BridgeError)
|
|
295
|
+
return watcher.failure;
|
|
296
|
+
const message = watcher.failure instanceof Error ? watcher.failure.message : "This checkout cannot be watched.";
|
|
297
|
+
return new BridgeError(503, message, { code: "git-watch-failed" });
|
|
298
|
+
}
|
|
299
|
+
create(root) {
|
|
300
|
+
const watcher = {
|
|
301
|
+
root, subscribers: new Set(), idleSince: undefined, stopTimer: undefined,
|
|
302
|
+
debounceTimer: undefined, pending: false, closed: false, failure: undefined,
|
|
303
|
+
ready: Promise.resolve(), close: () => { },
|
|
304
|
+
};
|
|
305
|
+
let started;
|
|
306
|
+
try {
|
|
307
|
+
started = this.platform === "linux"
|
|
308
|
+
? startPolling(root, () => this.changed(watcher), error => this.failWatcher(watcher, error), this.pollMs)
|
|
309
|
+
: startNative(root, () => this.changed(watcher), error => this.failWatcher(watcher, error));
|
|
310
|
+
}
|
|
311
|
+
catch (error) {
|
|
312
|
+
throw new BridgeError(503, "This checkout cannot be watched.", { code: "git-watch-failed", cause: error instanceof Error ? error.message : String(error) });
|
|
313
|
+
}
|
|
314
|
+
watcher.ready = started.ready;
|
|
315
|
+
watcher.close = () => {
|
|
316
|
+
if (watcher.closed)
|
|
317
|
+
return;
|
|
318
|
+
watcher.closed = true;
|
|
319
|
+
started.close();
|
|
320
|
+
if (watcher.debounceTimer)
|
|
321
|
+
clearTimeout(watcher.debounceTimer);
|
|
322
|
+
watcher.debounceTimer = undefined;
|
|
323
|
+
};
|
|
324
|
+
return watcher;
|
|
325
|
+
}
|
|
326
|
+
changed(watcher) {
|
|
327
|
+
if (watcher.closed)
|
|
328
|
+
return;
|
|
329
|
+
watcher.pending = true;
|
|
330
|
+
if (watcher.debounceTimer)
|
|
331
|
+
clearTimeout(watcher.debounceTimer);
|
|
332
|
+
watcher.debounceTimer = setTimeout(() => {
|
|
333
|
+
watcher.debounceTimer = undefined;
|
|
334
|
+
if (!watcher.pending)
|
|
335
|
+
return;
|
|
336
|
+
watcher.pending = false;
|
|
337
|
+
const at = new Date(this.now()).toISOString();
|
|
338
|
+
for (const subscriber of watcher.subscribers)
|
|
339
|
+
this.send(subscriber.client, { type: "changed", at });
|
|
340
|
+
}, this.debounceMs);
|
|
341
|
+
}
|
|
342
|
+
stop(watcher) {
|
|
343
|
+
if (this.watchers.get(watcher.root) === watcher)
|
|
344
|
+
this.watchers.delete(watcher.root);
|
|
345
|
+
if (watcher.stopTimer) {
|
|
346
|
+
clearTimeout(watcher.stopTimer);
|
|
347
|
+
watcher.stopTimer = undefined;
|
|
348
|
+
}
|
|
349
|
+
watcher.close();
|
|
350
|
+
}
|
|
351
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type Json } from "./protocol.js";
|
|
2
|
+
/** Safe removal of a linked worktree from the phone. A preview first describes
|
|
3
|
+
* what removing it would lose; its token is a digest of that exact state, so a
|
|
4
|
+
* later removal either confirms the same state or is refused with the new one.
|
|
5
|
+
* The branch is never deleted, and only worktrees outside the main checkout and
|
|
6
|
+
* the requesting pane's own checkout qualify. */
|
|
7
|
+
export interface WorktreeRemovalSession {
|
|
8
|
+
pane: string;
|
|
9
|
+
title: string;
|
|
10
|
+
}
|
|
11
|
+
export interface WorktreeRemovalSubmodule {
|
|
12
|
+
path: string;
|
|
13
|
+
unpushedCommits: number;
|
|
14
|
+
}
|
|
15
|
+
export interface WorktreeRemovalPreview {
|
|
16
|
+
token: string;
|
|
17
|
+
path: string;
|
|
18
|
+
branch: string | null;
|
|
19
|
+
dirty: string[];
|
|
20
|
+
untracked: number;
|
|
21
|
+
submodules: WorktreeRemovalSubmodule[];
|
|
22
|
+
sessions: WorktreeRemovalSession[];
|
|
23
|
+
removable: boolean;
|
|
24
|
+
reason?: string;
|
|
25
|
+
}
|
|
26
|
+
/** A live pane, with the folder it is running in, considered for blocking. */
|
|
27
|
+
export interface WorktreeRemovalLiveSession {
|
|
28
|
+
pane: string;
|
|
29
|
+
title: string;
|
|
30
|
+
cwd: string;
|
|
31
|
+
}
|
|
32
|
+
export interface WorktreeRemovalContext {
|
|
33
|
+
/** The checkout the request's pane is in; never removable. */
|
|
34
|
+
paneRoot?: string;
|
|
35
|
+
/** Live panes on this server; any running inside the worktree blocks removal. */
|
|
36
|
+
sessions?: WorktreeRemovalLiveSession[];
|
|
37
|
+
}
|
|
38
|
+
/** Preview a removal: what the worktree is, what it would lose, and whether
|
|
39
|
+
* anything live is using it. A main or current checkout is refused outright. */
|
|
40
|
+
export declare function previewWorktreeRemoval(cwd: string, worktree: unknown, context?: WorktreeRemovalContext): Promise<WorktreeRemovalPreview>;
|
|
41
|
+
/** Remove a worktree after its preview token matches the state recomputed now.
|
|
42
|
+
* Dirty or untracked files the preview showed let the removal use `--force`;
|
|
43
|
+
* a token for a clean worktree never does. The branch is left in place. */
|
|
44
|
+
export declare function removeWorktree(cwd: string, worktree: unknown, token: unknown, context?: WorktreeRemovalContext): Promise<Json>;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { realpath, stat } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { git, withGitReadDeadline } from "./projects.js";
|
|
5
|
+
import { BridgeError } from "./protocol.js";
|
|
6
|
+
import { displayPath, resolveWorktreeEntry } from "./git-worktrees.js";
|
|
7
|
+
const MAX_DIRTY = 50;
|
|
8
|
+
/** Working-tree changes, with untracked paths collapsed as Git reports them. */
|
|
9
|
+
async function changes(abs) {
|
|
10
|
+
const tokens = (await git(abs, "status", "--porcelain=v1", "-z", "--untracked-files=normal")).split("\0");
|
|
11
|
+
const dirty = [];
|
|
12
|
+
let untracked = 0;
|
|
13
|
+
for (let index = 0; index < tokens.length; index++) {
|
|
14
|
+
const row = tokens[index];
|
|
15
|
+
if (!row)
|
|
16
|
+
continue;
|
|
17
|
+
const status = row.slice(0, 2), file = row.slice(3);
|
|
18
|
+
if (status === "??") {
|
|
19
|
+
untracked++;
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
// Porcelain -z places the destination before the source of a rename.
|
|
23
|
+
if (/[RC]/.test(status))
|
|
24
|
+
index++;
|
|
25
|
+
if (dirty.length < MAX_DIRTY)
|
|
26
|
+
dirty.push(file);
|
|
27
|
+
}
|
|
28
|
+
return { dirty, untracked };
|
|
29
|
+
}
|
|
30
|
+
/** A submodule's HEAD commit when no remote branch contains it, with the number
|
|
31
|
+
* of its commits that no remote branch reaches, plus whether any submodule is
|
|
32
|
+
* initialized. An uninitialized submodule has no checkout to inspect and does
|
|
33
|
+
* not stop Git from removing the worktree. */
|
|
34
|
+
async function inspectSubmodules(abs) {
|
|
35
|
+
const output = await git(abs, "submodule", "status", "--recursive");
|
|
36
|
+
const unpushed = [];
|
|
37
|
+
let initialized = false;
|
|
38
|
+
for (const line of output.split("\n")) {
|
|
39
|
+
if (!line.trim())
|
|
40
|
+
continue;
|
|
41
|
+
const marker = /^[ +\-U]/.test(line) ? line[0] : "";
|
|
42
|
+
// An uninitialized submodule has no checkout to inspect.
|
|
43
|
+
if (marker === "-")
|
|
44
|
+
continue;
|
|
45
|
+
const rest = marker ? line.slice(1) : line;
|
|
46
|
+
const space = rest.indexOf(" ");
|
|
47
|
+
if (space < 0)
|
|
48
|
+
continue;
|
|
49
|
+
const head = rest.slice(0, space);
|
|
50
|
+
let sub = rest.slice(space + 1);
|
|
51
|
+
// `git submodule status` may append " (describe)" to the path.
|
|
52
|
+
const describe = sub.lastIndexOf(" (");
|
|
53
|
+
if (describe > 0 && sub.endsWith(")"))
|
|
54
|
+
sub = sub.slice(0, describe);
|
|
55
|
+
const checkout = path.join(abs, sub);
|
|
56
|
+
if (!(await stat(checkout).catch(() => undefined))?.isDirectory())
|
|
57
|
+
continue;
|
|
58
|
+
initialized = true;
|
|
59
|
+
const count = Number((await git(checkout, "rev-list", "--count", head, "--not", "--remotes")).trim()) || 0;
|
|
60
|
+
if (count > 0)
|
|
61
|
+
unpushed.push({ path: sub, unpushedCommits: count });
|
|
62
|
+
}
|
|
63
|
+
return { initialized, unpushed: unpushed.sort((a, b) => a.path.localeCompare(b.path)) };
|
|
64
|
+
}
|
|
65
|
+
/** Look through the live panes for sessions whose folder is the worktree or
|
|
66
|
+
* inside it. */
|
|
67
|
+
async function sessionsIn(abs, sessions) {
|
|
68
|
+
const found = [];
|
|
69
|
+
for (const session of sessions) {
|
|
70
|
+
const cwd = await realpath(session.cwd).catch(() => path.resolve(session.cwd));
|
|
71
|
+
if (cwd !== abs && !cwd.startsWith(abs + path.sep))
|
|
72
|
+
continue;
|
|
73
|
+
found.push({ pane: session.pane, title: session.title });
|
|
74
|
+
}
|
|
75
|
+
return found.sort((a, b) => a.pane.localeCompare(b.pane));
|
|
76
|
+
}
|
|
77
|
+
function tokenFor(preview) {
|
|
78
|
+
return createHash("sha256").update(JSON.stringify([
|
|
79
|
+
preview.path, preview.branch, preview.dirty, preview.untracked,
|
|
80
|
+
preview.submodules, preview.sessions, preview.removable, preview.reason ?? null,
|
|
81
|
+
])).digest("hex");
|
|
82
|
+
}
|
|
83
|
+
async function computePreview(cwd, worktree, context) {
|
|
84
|
+
const { abs, branch, main, root, locked } = await resolveWorktreeEntry(cwd, worktree);
|
|
85
|
+
if (abs === main)
|
|
86
|
+
throw new BridgeError(409, "The main checkout cannot be removed as a worktree.", { code: "worktree-not-removable" });
|
|
87
|
+
if (context.paneRoot) {
|
|
88
|
+
const paneRoot = await realpath(context.paneRoot).catch(() => path.resolve(context.paneRoot));
|
|
89
|
+
if (abs === paneRoot)
|
|
90
|
+
throw new BridgeError(409, "The pane's own checkout cannot be removed.", { code: "worktree-not-removable" });
|
|
91
|
+
}
|
|
92
|
+
const { dirty, untracked } = await changes(abs);
|
|
93
|
+
const submodules = await inspectSubmodules(abs);
|
|
94
|
+
const sessions = await sessionsIn(abs, context.sessions ?? []);
|
|
95
|
+
// The removal command may only add --force for changes this preview shows;
|
|
96
|
+
// a clean worktree is never forced and a lock is never overridden.
|
|
97
|
+
const force = dirty.length > 0 || untracked > 0;
|
|
98
|
+
const reason = sessions.length > 0 ? "sessions-open"
|
|
99
|
+
: locked ? "locked"
|
|
100
|
+
: submodules.initialized && !force ? "initialized-submodules"
|
|
101
|
+
: undefined;
|
|
102
|
+
const removable = reason === undefined;
|
|
103
|
+
const state = {
|
|
104
|
+
path: displayPath(abs, [root, ...(main && main !== root ? [main] : [])]),
|
|
105
|
+
branch,
|
|
106
|
+
dirty,
|
|
107
|
+
untracked,
|
|
108
|
+
submodules: submodules.unpushed,
|
|
109
|
+
sessions,
|
|
110
|
+
removable,
|
|
111
|
+
...(reason ? { reason } : {}),
|
|
112
|
+
};
|
|
113
|
+
return { token: tokenFor(state), ...state };
|
|
114
|
+
}
|
|
115
|
+
/** Preview a removal: what the worktree is, what it would lose, and whether
|
|
116
|
+
* anything live is using it. A main or current checkout is refused outright. */
|
|
117
|
+
export function previewWorktreeRemoval(cwd, worktree, context = {}) {
|
|
118
|
+
return withGitReadDeadline(() => computePreview(cwd, worktree, context));
|
|
119
|
+
}
|
|
120
|
+
/** Remove a worktree after its preview token matches the state recomputed now.
|
|
121
|
+
* Dirty or untracked files the preview showed let the removal use `--force`;
|
|
122
|
+
* a token for a clean worktree never does. The branch is left in place. */
|
|
123
|
+
export async function removeWorktree(cwd, worktree, token, context = {}) {
|
|
124
|
+
if (typeof token !== "string" || !/^[a-f0-9]{64}$/.test(token))
|
|
125
|
+
throw new BridgeError(400, "Invalid removal token.");
|
|
126
|
+
const preview = await computePreview(cwd, worktree, context);
|
|
127
|
+
if (preview.token !== token) {
|
|
128
|
+
throw new BridgeError(409, "The worktree changed since the preview.", { code: "worktree-changed", preview });
|
|
129
|
+
}
|
|
130
|
+
if (!preview.removable) {
|
|
131
|
+
const message = preview.reason === "sessions-open" ? "A live session is using this worktree."
|
|
132
|
+
: preview.reason === "locked" ? "This worktree is locked; unlock it before removing it."
|
|
133
|
+
: "This worktree has initialized submodules; a clean worktree is never removed with force.";
|
|
134
|
+
throw new BridgeError(409, message, { code: "worktree-not-removable", reason: preview.reason, preview });
|
|
135
|
+
}
|
|
136
|
+
const { abs, branch, root } = await resolveWorktreeEntry(cwd, worktree);
|
|
137
|
+
const force = preview.dirty.length > 0 || preview.untracked > 0;
|
|
138
|
+
await git(root, "worktree", "remove", ...(force ? ["--force"] : []), "--", abs);
|
|
139
|
+
return { removed: true, branch };
|
|
140
|
+
}
|
|
@@ -12,6 +12,9 @@ export interface WorktreeWorker {
|
|
|
12
12
|
state?: string;
|
|
13
13
|
}
|
|
14
14
|
export declare function worktreeId(abs: string): string;
|
|
15
|
+
/** Relative to the repository when the worktree lives inside it (the usual
|
|
16
|
+
* `.claude/worktrees/agent-*`), otherwise with the home folder shortened. */
|
|
17
|
+
export declare function displayPath(abs: string, roots: string[]): string;
|
|
15
18
|
/** The pane repository's other worktrees with branch, HEAD, commits ahead of
|
|
16
19
|
* and behind this pane's HEAD, uncommitted file count and, when known, the
|
|
17
20
|
* worker editing there. */
|
|
@@ -19,3 +22,14 @@ export declare function gitWorktrees(cwd: string, workers?: WorktreeWorker[]): P
|
|
|
19
22
|
/** The checkout for one listed worktree id of the pane's repository, or a
|
|
20
23
|
* 404; never a path the phone supplied. */
|
|
21
24
|
export declare function resolveWorktree(cwd: string, id: unknown): Promise<string>;
|
|
25
|
+
/** One listed worktree id resolved against the pane's repository: its checkout,
|
|
26
|
+
* branch, the primary checkout, the repository root the id was listed from and
|
|
27
|
+
* whether Git has it locked. */
|
|
28
|
+
export interface ResolvedWorktree {
|
|
29
|
+
abs: string;
|
|
30
|
+
branch: string | null;
|
|
31
|
+
main: string | undefined;
|
|
32
|
+
root: string;
|
|
33
|
+
locked: boolean;
|
|
34
|
+
}
|
|
35
|
+
export declare function resolveWorktreeEntry(cwd: string, id: unknown): Promise<ResolvedWorktree>;
|
|
@@ -54,7 +54,7 @@ async function listed(root) {
|
|
|
54
54
|
}
|
|
55
55
|
/** Relative to the repository when the worktree lives inside it (the usual
|
|
56
56
|
* `.claude/worktrees/agent-*`), otherwise with the home folder shortened. */
|
|
57
|
-
function displayPath(abs, roots) {
|
|
57
|
+
export function displayPath(abs, roots) {
|
|
58
58
|
for (const root of roots) {
|
|
59
59
|
const rel = path.relative(root, abs);
|
|
60
60
|
if (rel && !rel.startsWith("..") && !path.isAbsolute(rel))
|
|
@@ -135,13 +135,17 @@ async function readGitWorktrees(cwd, workers) {
|
|
|
135
135
|
/** The checkout for one listed worktree id of the pane's repository, or a
|
|
136
136
|
* 404; never a path the phone supplied. */
|
|
137
137
|
export async function resolveWorktree(cwd, id) {
|
|
138
|
+
return (await resolveWorktreeEntry(cwd, id)).abs;
|
|
139
|
+
}
|
|
140
|
+
export async function resolveWorktreeEntry(cwd, id) {
|
|
138
141
|
if (typeof id !== "string" || !worktreeIdPattern.test(id))
|
|
139
142
|
throw new BridgeError(400, "Invalid worktree.");
|
|
140
143
|
const root = await gitRoot(cwd);
|
|
141
144
|
if (!root)
|
|
142
145
|
throw new BridgeError(409, "This pane is not in a Git repository.", { code: "git-not-repository" });
|
|
143
|
-
const
|
|
146
|
+
const { main, worktrees } = await listed(root);
|
|
147
|
+
const match = worktrees.find(worktree => worktreeId(worktree.abs) === id);
|
|
144
148
|
if (!match)
|
|
145
149
|
throw new BridgeError(404, "That worktree is not part of this repository.");
|
|
146
|
-
return match.abs;
|
|
150
|
+
return { abs: match.abs, branch: match.branch, main, root, locked: match.locked };
|
|
147
151
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Require this no earlier than the first Hook release on or after 2026-11-15,
|
|
2
2
|
// once the warning counts show no older clients (owner-approved 2026-10-14).
|
|
3
3
|
export const EXPECTED_REPOSITORY_REQUIRED = false;
|
|
4
|
-
const WRITE_ROUTES = new Set(["stage", "unstage", "discard", "commit", "push", "pr", "pr/merge", "host-token", "checkout", "fetch", "pull", "apply"]
|
|
4
|
+
const WRITE_ROUTES = new Set(["checkpoints", "checkpoints/preview", "checkpoints/restore", "stage", "unstage", "discard", "commit", "push", "pr", "pr/merge", "host-token", "checkout", "fetch", "pull", "apply"]
|
|
5
5
|
.map(action => `/v1/git/${action}`));
|
|
6
6
|
export const isGitWriteRoute = (route) => WRITE_ROUTES.has(route);
|
|
7
7
|
/** Request metadata is a diagnostic hint, never an authentication identity.
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type ScheduleEvent } from "./schedule-events.js";
|
|
1
2
|
import type { StorePullIdentity } from "./store-pull.js";
|
|
2
3
|
export interface WebhookStatus {
|
|
3
4
|
configured: boolean;
|
|
@@ -12,13 +13,14 @@ export declare class GitboyWebhooks {
|
|
|
12
13
|
private readonly root;
|
|
13
14
|
private readonly pull;
|
|
14
15
|
private readonly nudge;
|
|
16
|
+
private readonly scheduleEvent?;
|
|
15
17
|
readonly status: WebhookStatus;
|
|
16
18
|
private server?;
|
|
17
19
|
private readonly deliveries;
|
|
18
20
|
private readonly bodies;
|
|
19
21
|
private readonly nudges;
|
|
20
22
|
private closed;
|
|
21
|
-
constructor(root: string, pull: (storeId: string) => string | undefined, nudge?: (identity: StorePullIdentity) => Promise<void>);
|
|
23
|
+
constructor(root: string, pull: (storeId: string) => string | undefined, nudge?: (identity: StorePullIdentity) => Promise<void>, scheduleEvent?: ((event: ScheduleEvent) => Promise<unknown>) | undefined);
|
|
22
24
|
private notify;
|
|
23
25
|
start(): Promise<void>;
|
|
24
26
|
stop(): void;
|