@dadado/agent-kit-cli 6.18.0 → 6.19.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.
- package/dashboard/dashboard-data.mjs +8 -3
- package/dashboard/dashboard.html +140 -0
- package/dashboard/lib/guards.d.mts +20 -0
- package/dashboard/lib/guards.mjs +307 -0
- package/dashboard/lib/semantic-model.mjs +228 -1
- package/dist/{add-JZKFUH2O.js → add-MRI5BXHH.js} +3 -3
- package/dist/{chunk-OKIFM3IH.js → chunk-2TX6NJEG.js} +1 -1
- package/dist/{chunk-VRSBZKTA.js → chunk-3HNTNYPR.js} +1 -1
- package/dist/{chunk-H5D3HF2N.js → chunk-5EBT3V7G.js} +1 -1
- package/dist/{chunk-B4MUKUVS.js → chunk-677W4QO3.js} +58 -1
- package/dist/{chunk-SKC6QZW4.js → chunk-6WUKLS4D.js} +1 -1
- package/dist/{chunk-CRH2Q6KY.js → chunk-6XJZLH53.js} +149 -18
- package/dist/{chunk-UEW42NIF.js → chunk-7MUTZHWP.js} +3 -3
- package/dist/{chunk-L7BL4246.js → chunk-BGL5DRK2.js} +2 -2
- package/dist/{chunk-44SDKJYY.js → chunk-C4LCOOL4.js} +3 -1
- package/dist/chunk-DTSL2RH7.js +433 -0
- package/dist/{chunk-XHANIDPK.js → chunk-EBDQYFA4.js} +1 -1
- package/dist/{chunk-YP5OCR4I.js → chunk-EOQMAU2J.js} +1 -1
- package/dist/{chunk-WJRNI5FJ.js → chunk-G2Z37XFM.js} +3 -3
- package/dist/{chunk-4KV3TXKC.js → chunk-GTP224FI.js} +1 -1
- package/dist/{chunk-357Y422W.js → chunk-HJWSOZXS.js} +52 -12
- package/dist/{chunk-42AA2VFW.js → chunk-IQQQDEQ4.js} +2 -2
- package/dist/{chunk-ZFJIJUZ4.js → chunk-MPCNPZJZ.js} +1 -1
- package/dist/{hook-AZ5LSJBJ.js → chunk-NL3I2BWA.js} +104 -113
- package/dist/{chunk-R4VLVX7B.js → chunk-OKQ37A7E.js} +9 -9
- package/dist/{chunk-WRCXRV2U.js → chunk-P7732A25.js} +10 -0
- package/dist/{chunk-BDIWC3WB.js → chunk-QBYJNGCD.js} +2 -2
- package/dist/chunk-QWTBYXHN.js +751 -0
- package/dist/{chunk-SC2DBY2R.js → chunk-R4DUO5DA.js} +1 -0
- package/dist/{chunk-MOJRD6HF.js → chunk-SGTTC2JP.js} +4 -3
- package/dist/{chunk-CTVVGKYB.js → chunk-U23SLQGX.js} +5 -5
- package/dist/chunk-UATQQDVR.js +297 -0
- package/dist/{chunk-2CBKKCVU.js → chunk-UXQH2CNC.js} +2 -1
- package/dist/{chunk-VY7AFID2.js → chunk-VJWWJA36.js} +3 -3
- package/dist/{chunk-Y7BH6KIE.js → chunk-W3ZJVBNY.js} +6 -3
- package/dist/{chunk-XDCCYBCJ.js → chunk-ZMYV6PRE.js} +13 -0
- package/dist/{commands-5P2E5LL4.js → commands-NL6UOEP7.js} +50 -16
- package/dist/{conflicts-A4B422EL.js → conflicts-XMN4PBCZ.js} +7 -7
- package/dist/{contribute-GAHG3J4Q.js → contribute-QDTEJ42W.js} +4 -4
- package/dist/{countdown-C6NY2TSG.js → countdown-7ONIVAG6.js} +39 -11
- package/dist/{dashboard-YNCFISNO.js → dashboard-P65XHYCU.js} +1 -1
- package/dist/{dashboard-broadcast-NK7PU72L.js → dashboard-broadcast-GYK34QYL.js} +1 -1
- package/dist/{diff-EYAGGEU4.js → diff-RLB7R2NE.js} +5 -5
- package/dist/{doctor-GOMPRU6F.js → doctor-OXMDSOXL.js} +10 -10
- package/dist/{engine-info-R6GXZ2VO.js → engine-info-BBI5QO5Y.js} +3 -3
- package/dist/{ground-control-MJGF5QRE.js → ground-control-IDCXN5FK.js} +4 -4
- package/dist/hook-B4VUQOQF.js +108 -0
- package/dist/index.js +27 -27
- package/dist/{init-WW5DR3DX.js → init-BYONEUCW.js} +13 -13
- package/dist/{install-RN2DRLK6.js → install-HOR6VR44.js} +13 -13
- package/dist/{mission-control-JIF6GFSV.js → mission-control-QJBSZVNI.js} +2 -2
- package/dist/{queue-7EBQGPPW.js → queue-OJHKFKK3.js} +54 -10
- package/dist/{run-GOMAHNV2.js → run-PFANZJ5Y.js} +174 -26
- package/dist/{run-plan-EUQ5WWF6.js → run-plan-7EB4ERSQ.js} +45 -15
- package/dist/{scan-BWA64R7I.js → scan-K3T5RQLC.js} +1 -1
- package/dist/session-start-OSFSF4OZ.js +74 -0
- package/dist/sessions-WQU5DONU.js +102 -0
- package/dist/{setup-global-DJWURJS3.js → setup-global-O34KFL2C.js} +6 -6
- package/dist/{status-2WYPLWTN.js → status-QB2FFYUP.js} +8 -8
- package/dist/{update-5VFQNBY5.js → update-N2EKKSE7.js} +10 -10
- package/package.json +1 -1
- package/dist/sessions-O7MYEMCT.js +0 -343
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
whichBinary
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-W3ZJVBNY.js";
|
|
4
4
|
|
|
5
5
|
// src/plan-loop/detect.ts
|
|
6
6
|
var DETECT_ORDER = ["cursor-agent", "claude"];
|
|
@@ -64,6 +64,8 @@ async function detectAgentBackend(requested, whichFn = whichBinary) {
|
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
export {
|
|
67
|
+
isBackendId,
|
|
68
|
+
isRequestedBackend,
|
|
67
69
|
listDetectBackendIds,
|
|
68
70
|
detectAgentBackend
|
|
69
71
|
};
|
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
import {
|
|
2
|
+
HANDOFF_REL
|
|
3
|
+
} from "./chunk-C4Q77GGJ.js";
|
|
4
|
+
|
|
5
|
+
// src/plan-loop/queue-lease.ts
|
|
6
|
+
import { randomUUID } from "crypto";
|
|
7
|
+
import { linkSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "fs";
|
|
8
|
+
import { hostname } from "os";
|
|
9
|
+
import path from "path";
|
|
10
|
+
var QUEUE_LEASE_RELPATH = ".cursor/context/run-plan-all.lock";
|
|
11
|
+
var QUEUE_LEASE_HEARTBEAT_MS = 3e4;
|
|
12
|
+
var QUEUE_LEASE_TTL_MS = 12e4;
|
|
13
|
+
var QUEUE_LEASE_INTERRUPT_WAIT_MS = 2e3;
|
|
14
|
+
var QUEUE_LEASE_SESSION_ENV = "AGENT_KIT_QUEUE_LEASE_SESSION";
|
|
15
|
+
function decideQueueLease(lease, probe) {
|
|
16
|
+
if (lease === null) return { kind: "free" };
|
|
17
|
+
if (lease === "corrupt") return { kind: "stale", lease: null, reason: "unreadable lease file" };
|
|
18
|
+
if (probe.session && probe.session === lease.session) return { kind: "own", lease };
|
|
19
|
+
const ttl = probe.ttlMs ?? QUEUE_LEASE_TTL_MS;
|
|
20
|
+
const beat = Date.parse(lease.heartbeatAt);
|
|
21
|
+
const fresh = Number.isFinite(beat) && probe.now - beat <= ttl;
|
|
22
|
+
if (lease.host !== probe.host) {
|
|
23
|
+
return fresh ? {
|
|
24
|
+
kind: "held",
|
|
25
|
+
lease,
|
|
26
|
+
orphan: false,
|
|
27
|
+
reason: `held by host ${lease.host} (fresh heartbeat)`
|
|
28
|
+
} : { kind: "stale", lease, reason: `heartbeat expired (host ${lease.host})` };
|
|
29
|
+
}
|
|
30
|
+
const dispatcherAlive = probe.alive(lease.dispatcher);
|
|
31
|
+
const agentAlive = lease.agent !== null && probe.alive(lease.agent);
|
|
32
|
+
if (agentAlive && !dispatcherAlive) {
|
|
33
|
+
return {
|
|
34
|
+
kind: "held",
|
|
35
|
+
lease,
|
|
36
|
+
orphan: true,
|
|
37
|
+
reason: `orphaned agent pid ${lease.agent?.pid} is still running (dispatcher pid ${lease.dispatcher.pid} is gone)`
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
if (agentAlive && !fresh) {
|
|
41
|
+
return {
|
|
42
|
+
kind: "held",
|
|
43
|
+
lease,
|
|
44
|
+
orphan: true,
|
|
45
|
+
reason: `agent pid ${lease.agent?.pid} is still running under an unresponsive dispatcher (pid ${lease.dispatcher.pid}, heartbeat expired)`
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
if (dispatcherAlive && fresh) {
|
|
49
|
+
return {
|
|
50
|
+
kind: "held",
|
|
51
|
+
lease,
|
|
52
|
+
orphan: false,
|
|
53
|
+
reason: `held by dispatcher pid ${lease.dispatcher.pid}`
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
if (!dispatcherAlive) {
|
|
57
|
+
return { kind: "stale", lease, reason: `dispatcher pid ${lease.dispatcher.pid} is gone` };
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
kind: "stale",
|
|
61
|
+
lease,
|
|
62
|
+
reason: `heartbeat expired (dispatcher pid ${lease.dispatcher.pid})`
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function formatQueueLeaseRefusal(v) {
|
|
66
|
+
const l = v.lease;
|
|
67
|
+
const who = [
|
|
68
|
+
`dispatcher pid ${l.dispatcher.pid}`,
|
|
69
|
+
l.agent ? `agent pid ${l.agent.pid}` : "no agent yet",
|
|
70
|
+
`session ${l.session}`,
|
|
71
|
+
`host ${l.host}`,
|
|
72
|
+
`started ${l.startedAt}`,
|
|
73
|
+
`last heartbeat ${l.heartbeatAt}`
|
|
74
|
+
].join(", ");
|
|
75
|
+
const next = v.orphan ? `It may still be writing this repository. Check it (ps -o pid,etime,args -p ${l.agent?.pid}), stop it, then start /run-plan-all again.` : "Wait for that session to end (or stop it from the window that started it), then start /run-plan-all again.";
|
|
76
|
+
return `Queue lease held: another /run-plan-all owns this queue (${v.reason}; ${who}). ${next}`;
|
|
77
|
+
}
|
|
78
|
+
function procStart(pid) {
|
|
79
|
+
if (process.platform !== "linux") return null;
|
|
80
|
+
const stat = procStat(pid);
|
|
81
|
+
return stat?.start ?? null;
|
|
82
|
+
}
|
|
83
|
+
function procStat(pid) {
|
|
84
|
+
try {
|
|
85
|
+
const text = readFileSync(`/proc/${pid}/stat`, "utf8");
|
|
86
|
+
const fields = text.slice(text.lastIndexOf(")") + 2).split(" ");
|
|
87
|
+
const state = fields[0];
|
|
88
|
+
const ppid = Number(fields[1]);
|
|
89
|
+
const start = fields[19];
|
|
90
|
+
return state && start ? { state, ppid, start } : null;
|
|
91
|
+
} catch {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function procParent(pid) {
|
|
96
|
+
if (process.platform !== "linux") return null;
|
|
97
|
+
return procStat(pid)?.ppid ?? null;
|
|
98
|
+
}
|
|
99
|
+
function processAlive(proc) {
|
|
100
|
+
if (!Number.isInteger(proc.pid) || proc.pid <= 1) return false;
|
|
101
|
+
try {
|
|
102
|
+
process.kill(proc.pid, 0);
|
|
103
|
+
} catch (err) {
|
|
104
|
+
if (err.code !== "EPERM") return false;
|
|
105
|
+
}
|
|
106
|
+
if (process.platform !== "linux") return true;
|
|
107
|
+
const stat = procStat(proc.pid);
|
|
108
|
+
if (!stat) return false;
|
|
109
|
+
if (stat.state === "Z" || stat.state === "X") return false;
|
|
110
|
+
return proc.start === null || proc.start === stat.start;
|
|
111
|
+
}
|
|
112
|
+
function queueLeasePath(root) {
|
|
113
|
+
return path.join(root, QUEUE_LEASE_RELPATH);
|
|
114
|
+
}
|
|
115
|
+
function readQueueLease(root) {
|
|
116
|
+
let text;
|
|
117
|
+
try {
|
|
118
|
+
text = readFileSync(queueLeasePath(root), "utf8");
|
|
119
|
+
} catch (err) {
|
|
120
|
+
if (err.code === "ENOENT") return null;
|
|
121
|
+
return "corrupt";
|
|
122
|
+
}
|
|
123
|
+
try {
|
|
124
|
+
const v = JSON.parse(text);
|
|
125
|
+
const okProc = (p) => !!p && typeof p.pid === "number";
|
|
126
|
+
if (v.version !== 1 || typeof v.session !== "string" || typeof v.host !== "string" || !okProc(v.dispatcher) || v.agent !== null && !okProc(v.agent) || typeof v.startedAt !== "string" || typeof v.heartbeatAt !== "string") {
|
|
127
|
+
return "corrupt";
|
|
128
|
+
}
|
|
129
|
+
return v;
|
|
130
|
+
} catch {
|
|
131
|
+
return "corrupt";
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function leaseTmp(root, lease) {
|
|
135
|
+
const tmp = `${queueLeasePath(root)}.${process.pid}.${randomUUID().slice(0, 8)}.tmp`;
|
|
136
|
+
writeFileSync(tmp, `${JSON.stringify(lease, null, 2)}
|
|
137
|
+
`, "utf8");
|
|
138
|
+
return tmp;
|
|
139
|
+
}
|
|
140
|
+
function writeLeaseAtomic(root, lease) {
|
|
141
|
+
renameSync(leaseTmp(root, lease), queueLeasePath(root));
|
|
142
|
+
}
|
|
143
|
+
function createLeaseExclusive(root, lease) {
|
|
144
|
+
const tmp = leaseTmp(root, lease);
|
|
145
|
+
try {
|
|
146
|
+
linkSync(tmp, queueLeasePath(root));
|
|
147
|
+
return true;
|
|
148
|
+
} catch (err) {
|
|
149
|
+
if (err.code === "EEXIST") return false;
|
|
150
|
+
throw err;
|
|
151
|
+
} finally {
|
|
152
|
+
try {
|
|
153
|
+
unlinkSync(tmp);
|
|
154
|
+
} catch {
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function pauseMs(ms) {
|
|
159
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
160
|
+
}
|
|
161
|
+
var QUEUE_LEASE_TAKEOVER_SETTLE_MS = 150;
|
|
162
|
+
function inspectQueueLease(root, env = {}) {
|
|
163
|
+
return decideQueueLease(readQueueLease(root), {
|
|
164
|
+
host: env.host ?? hostname(),
|
|
165
|
+
now: (env.now ?? Date.now)(),
|
|
166
|
+
alive: env.alive ?? processAlive,
|
|
167
|
+
session: env.callerSession ?? process.env[QUEUE_LEASE_SESSION_ENV],
|
|
168
|
+
ttlMs: env.ttlMs
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
function acquireQueueLease(root, env = {}) {
|
|
172
|
+
const now = env.now ?? Date.now;
|
|
173
|
+
const host = env.host ?? hostname();
|
|
174
|
+
const pid = env.pid ?? process.pid;
|
|
175
|
+
const startOf = env.procStart ?? procStart;
|
|
176
|
+
const alive = env.alive ?? processAlive;
|
|
177
|
+
const session = env.session ?? randomUUID();
|
|
178
|
+
const iso = () => new Date(now()).toISOString();
|
|
179
|
+
const file = queueLeasePath(root);
|
|
180
|
+
mkdirSync(path.dirname(file), { recursive: true });
|
|
181
|
+
let lease = {
|
|
182
|
+
version: 1,
|
|
183
|
+
session,
|
|
184
|
+
host,
|
|
185
|
+
dispatcher: { pid, start: startOf(pid) },
|
|
186
|
+
agent: null,
|
|
187
|
+
startedAt: iso(),
|
|
188
|
+
heartbeatAt: iso()
|
|
189
|
+
};
|
|
190
|
+
let takeover = null;
|
|
191
|
+
if (!createLeaseExclusive(root, lease)) {
|
|
192
|
+
const verdict = decideQueueLease(readQueueLease(root), {
|
|
193
|
+
host,
|
|
194
|
+
now: now(),
|
|
195
|
+
alive,
|
|
196
|
+
ttlMs: env.ttlMs
|
|
197
|
+
});
|
|
198
|
+
if (verdict.kind === "held") {
|
|
199
|
+
return { ok: false, verdict, message: formatQueueLeaseRefusal(verdict) };
|
|
200
|
+
}
|
|
201
|
+
writeLeaseAtomic(root, lease);
|
|
202
|
+
pauseMs(env.settleMs ?? QUEUE_LEASE_TAKEOVER_SETTLE_MS);
|
|
203
|
+
const back = readQueueLease(root);
|
|
204
|
+
if (back === null || back === "corrupt" || back.session !== session) {
|
|
205
|
+
const held = {
|
|
206
|
+
kind: "held",
|
|
207
|
+
lease: back !== null && back !== "corrupt" ? back : lease,
|
|
208
|
+
orphan: false,
|
|
209
|
+
reason: "another dispatcher took over the stale lease first"
|
|
210
|
+
};
|
|
211
|
+
return { ok: false, verdict: held, message: formatQueueLeaseRefusal(held) };
|
|
212
|
+
}
|
|
213
|
+
takeover = verdict.kind === "stale" ? verdict : { kind: "stale", lease: null, reason: "lease file vanished" };
|
|
214
|
+
logQueueLeaseTakeover(root, takeover, { at: iso(), pid, session, holder: env.holder });
|
|
215
|
+
}
|
|
216
|
+
let released = false;
|
|
217
|
+
let lost = false;
|
|
218
|
+
const mine = () => {
|
|
219
|
+
const cur = readQueueLease(root);
|
|
220
|
+
if (cur !== null && cur !== "corrupt" && cur.session === session) return true;
|
|
221
|
+
if (!lost) {
|
|
222
|
+
lost = true;
|
|
223
|
+
env.onLost?.(cur !== null && cur !== "corrupt" ? cur : null);
|
|
224
|
+
}
|
|
225
|
+
return false;
|
|
226
|
+
};
|
|
227
|
+
const write = (next) => {
|
|
228
|
+
if (released || lost || !mine()) return;
|
|
229
|
+
lease = next;
|
|
230
|
+
writeLeaseAtomic(root, lease);
|
|
231
|
+
};
|
|
232
|
+
const timer = setInterval(() => {
|
|
233
|
+
try {
|
|
234
|
+
write({ ...lease, heartbeatAt: iso() });
|
|
235
|
+
} catch {
|
|
236
|
+
}
|
|
237
|
+
}, env.heartbeatMs ?? QUEUE_LEASE_HEARTBEAT_MS);
|
|
238
|
+
timer.unref();
|
|
239
|
+
let agent = null;
|
|
240
|
+
const release = () => {
|
|
241
|
+
if (released) return;
|
|
242
|
+
released = true;
|
|
243
|
+
clearInterval(timer);
|
|
244
|
+
try {
|
|
245
|
+
const cur = readQueueLease(root);
|
|
246
|
+
if (cur !== null && cur !== "corrupt" && cur.session === session) unlinkSync(file);
|
|
247
|
+
} catch {
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
const handle = {
|
|
251
|
+
session,
|
|
252
|
+
takeover,
|
|
253
|
+
setAgent: (agentPid) => {
|
|
254
|
+
if (agentPid === void 0) return;
|
|
255
|
+
agent = { pid: agentPid, start: startOf(agentPid) };
|
|
256
|
+
write({ ...lease, agent, heartbeatAt: iso() });
|
|
257
|
+
},
|
|
258
|
+
agent: () => agent,
|
|
259
|
+
beat: () => write({ ...lease, heartbeatAt: iso() }),
|
|
260
|
+
held: () => !released && !lost && mine(),
|
|
261
|
+
release,
|
|
262
|
+
releaseAfterAgent: (waitMs) => {
|
|
263
|
+
const deadline = now() + waitMs;
|
|
264
|
+
while (agent !== null && alive(agent) && now() < deadline) pauseMs(50);
|
|
265
|
+
if (agent !== null && alive(agent)) {
|
|
266
|
+
clearInterval(timer);
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
release();
|
|
270
|
+
return true;
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
return { ok: true, handle };
|
|
274
|
+
}
|
|
275
|
+
function logQueueLeaseTakeover(root, verdict, by) {
|
|
276
|
+
const file = path.join(root, HANDOFF_REL);
|
|
277
|
+
let text;
|
|
278
|
+
try {
|
|
279
|
+
text = readFileSync(file, "utf8");
|
|
280
|
+
} catch {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
const old = verdict.lease;
|
|
284
|
+
const from = old ? `dispatcher pid ${old.dispatcher.pid}${old.agent ? `, agent pid ${old.agent.pid}` : ""}, session ${old.session}, host ${old.host}, started ${old.startedAt}, last heartbeat ${old.heartbeatAt}` : "no readable owner";
|
|
285
|
+
const line = `- **Queue lease takeover ${by.at} (${by.holder ?? "agent-kit run-plan-all"}, pid ${by.pid}, session ${by.session}):** took over a stale queue lease (${verdict.reason}; ${from}). Check the old owner's worktrees before resuming its rows.`;
|
|
286
|
+
const nl = text.indexOf("\n");
|
|
287
|
+
const next = nl === -1 ? `${text}
|
|
288
|
+
${line}
|
|
289
|
+
` : `${text.slice(0, nl + 1)}${line}
|
|
290
|
+
${text.slice(nl + 1)}`;
|
|
291
|
+
writeFileSync(file, next, "utf8");
|
|
292
|
+
return line;
|
|
293
|
+
}
|
|
294
|
+
var LEASE_FREE_SLASHES = /* @__PURE__ */ new Set([
|
|
295
|
+
"summary",
|
|
296
|
+
"context-status",
|
|
297
|
+
"tips",
|
|
298
|
+
"review-pack",
|
|
299
|
+
"resolve-conflicts"
|
|
300
|
+
]);
|
|
301
|
+
function nestedUnderLease(root, probe = {}) {
|
|
302
|
+
const session = probe.session ?? process.env[QUEUE_LEASE_SESSION_ENV];
|
|
303
|
+
if (!session) return null;
|
|
304
|
+
const lease = readQueueLease(root);
|
|
305
|
+
if (lease === null || lease === "corrupt" || lease.session !== session) return null;
|
|
306
|
+
const verdict = decideQueueLease(lease, {
|
|
307
|
+
host: probe.host ?? hostname(),
|
|
308
|
+
now: probe.now ?? Date.now(),
|
|
309
|
+
alive: probe.alive ?? processAlive,
|
|
310
|
+
ttlMs: probe.ttlMs
|
|
311
|
+
});
|
|
312
|
+
if (verdict.kind !== "held") return null;
|
|
313
|
+
const canCheck = probe.canCheckAncestry ?? process.platform === "linux";
|
|
314
|
+
if (!canCheck) return lease;
|
|
315
|
+
const parentOf = probe.parentOf ?? procParent;
|
|
316
|
+
const holders = /* @__PURE__ */ new Set([lease.dispatcher.pid, ...lease.agent ? [lease.agent.pid] : []]);
|
|
317
|
+
let p = probe.pid ?? process.pid;
|
|
318
|
+
for (let guard = 0; p !== null && p > 1 && guard < 128; guard++) {
|
|
319
|
+
if (holders.has(p)) return lease;
|
|
320
|
+
p = parentOf(p);
|
|
321
|
+
}
|
|
322
|
+
return null;
|
|
323
|
+
}
|
|
324
|
+
function formatPlanWriterRefusal(v, command) {
|
|
325
|
+
const l = v.lease;
|
|
326
|
+
const who = [
|
|
327
|
+
`dispatcher pid ${l.dispatcher.pid}`,
|
|
328
|
+
l.agent ? `agent pid ${l.agent.pid}` : "no agent yet",
|
|
329
|
+
`session ${l.session}`,
|
|
330
|
+
`host ${l.host}`,
|
|
331
|
+
`started ${l.startedAt}`
|
|
332
|
+
].join(", ");
|
|
333
|
+
const next = v.orphan ? `It may still be writing this repository. Check it (ps -o pid,etime,args -p ${l.agent?.pid}), stop it, then start ${command} again.` : `Wait for that session to end (agent-kit sessions --live shows it), then start ${command} again.`;
|
|
334
|
+
return `Queue lease held: another live session writes this workspace's plans (${v.reason}; ${who}). ${command} did not start. ${next}`;
|
|
335
|
+
}
|
|
336
|
+
function gateCheckIn(root, session, env = {}) {
|
|
337
|
+
return () => {
|
|
338
|
+
try {
|
|
339
|
+
const v = inspectQueueLease(root, { ...env, callerSession: session() ?? void 0 });
|
|
340
|
+
if (v.kind !== "held") return null;
|
|
341
|
+
return `Queue lease held: another live session writes this workspace's plans (${v.reason}; session ${v.lease.session}); check with agent-kit sessions --live`;
|
|
342
|
+
} catch {
|
|
343
|
+
return null;
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
function acquirePlanWriterLease(root, command, env = {}) {
|
|
348
|
+
const nested = nestedUnderLease(root, {
|
|
349
|
+
host: env.host,
|
|
350
|
+
now: env.now?.(),
|
|
351
|
+
alive: env.alive,
|
|
352
|
+
ttlMs: env.ttlMs,
|
|
353
|
+
pid: env.pid,
|
|
354
|
+
...env.nested
|
|
355
|
+
});
|
|
356
|
+
if (nested) return { kind: "nested", lease: nested };
|
|
357
|
+
const acquired = acquireQueueLease(root, { ...env, holder: env.holder ?? command });
|
|
358
|
+
if (acquired.ok) return { kind: "acquired", handle: acquired.handle };
|
|
359
|
+
return { kind: "refused", message: formatPlanWriterRefusal(acquired.verdict, command) };
|
|
360
|
+
}
|
|
361
|
+
var QUEUE_STATUS_LINE = /^- \*\*Queue status:\*\*[ \t]*(.*)$/m;
|
|
362
|
+
function holdQueueStatus(root, by) {
|
|
363
|
+
const file = path.join(root, HANDOFF_REL);
|
|
364
|
+
let text;
|
|
365
|
+
try {
|
|
366
|
+
text = readFileSync(file, "utf8");
|
|
367
|
+
} catch {
|
|
368
|
+
return null;
|
|
369
|
+
}
|
|
370
|
+
const m = QUEUE_STATUS_LINE.exec(text);
|
|
371
|
+
if (!m) return null;
|
|
372
|
+
const previous = (m[1] ?? "").trim();
|
|
373
|
+
if (!/^(running|paused)\b/i.test(previous)) return null;
|
|
374
|
+
const original = m[0];
|
|
375
|
+
const held = `- **Queue status:** held (by ${by.plan}, ${by.who}, ${by.at}; was ${previous})`;
|
|
376
|
+
writeFileSync(
|
|
377
|
+
file,
|
|
378
|
+
text.replace(original, () => held),
|
|
379
|
+
"utf8"
|
|
380
|
+
);
|
|
381
|
+
return () => {
|
|
382
|
+
try {
|
|
383
|
+
const now = readFileSync(file, "utf8");
|
|
384
|
+
const cur = QUEUE_STATUS_LINE.exec(now);
|
|
385
|
+
if (!cur || cur[0] !== held) return false;
|
|
386
|
+
writeFileSync(
|
|
387
|
+
file,
|
|
388
|
+
now.replace(held, () => original),
|
|
389
|
+
"utf8"
|
|
390
|
+
);
|
|
391
|
+
return true;
|
|
392
|
+
} catch {
|
|
393
|
+
return false;
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
function forwardTermination(agent, onExit, opts = {}) {
|
|
398
|
+
const kill = opts.kill ?? ((p, s) => {
|
|
399
|
+
process.kill(p, s);
|
|
400
|
+
});
|
|
401
|
+
const isAlive = opts.alive ?? processAlive;
|
|
402
|
+
const handler = (signal) => {
|
|
403
|
+
const proc = agent();
|
|
404
|
+
if (proc && proc.pid > 1 && isAlive(proc)) {
|
|
405
|
+
try {
|
|
406
|
+
kill(proc.pid, "SIGTERM");
|
|
407
|
+
} catch {
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
onExit(signal);
|
|
411
|
+
};
|
|
412
|
+
const signals = opts.signals ?? ["SIGTERM", "SIGHUP"];
|
|
413
|
+
const listeners = signals.map((sig) => [sig, () => handler(sig)]);
|
|
414
|
+
for (const [sig, fn] of listeners) process.on(sig, fn);
|
|
415
|
+
return () => {
|
|
416
|
+
for (const [sig, fn] of listeners) process.off(sig, fn);
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
export {
|
|
421
|
+
QUEUE_LEASE_RELPATH,
|
|
422
|
+
QUEUE_LEASE_INTERRUPT_WAIT_MS,
|
|
423
|
+
QUEUE_LEASE_SESSION_ENV,
|
|
424
|
+
formatQueueLeaseRefusal,
|
|
425
|
+
procStart,
|
|
426
|
+
inspectQueueLease,
|
|
427
|
+
acquireQueueLease,
|
|
428
|
+
LEASE_FREE_SLASHES,
|
|
429
|
+
gateCheckIn,
|
|
430
|
+
acquirePlanWriterLease,
|
|
431
|
+
holdQueueStatus,
|
|
432
|
+
forwardTermination
|
|
433
|
+
};
|
|
@@ -2,7 +2,7 @@ import {
|
|
|
2
2
|
compareSemver,
|
|
3
3
|
normalizeSemver,
|
|
4
4
|
readLocalKitVersion
|
|
5
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-IQQQDEQ4.js";
|
|
6
6
|
import {
|
|
7
7
|
L0_ARTIFACTS
|
|
8
8
|
} from "./chunk-WS6OX2IG.js";
|
|
@@ -15,11 +15,11 @@ import {
|
|
|
15
15
|
mergeStats,
|
|
16
16
|
recordOutcome,
|
|
17
17
|
resolveProtectedGlobs
|
|
18
|
-
} from "./chunk-
|
|
18
|
+
} from "./chunk-3HNTNYPR.js";
|
|
19
19
|
import {
|
|
20
20
|
loadManagedHashLedger,
|
|
21
21
|
saveManagedHashLedger
|
|
22
|
-
} from "./chunk-
|
|
22
|
+
} from "./chunk-677W4QO3.js";
|
|
23
23
|
import {
|
|
24
24
|
KIT_PACKAGE_SPEC,
|
|
25
25
|
KIT_VERSION
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
readRunQueueState
|
|
3
|
-
} from "./chunk-7ABY6DYH.js";
|
|
4
1
|
import {
|
|
5
2
|
DRIVER_PROTOCOL_VERSION,
|
|
6
3
|
DriverAgentSink,
|
|
@@ -8,24 +5,34 @@ import {
|
|
|
8
5
|
createDriverEmitter,
|
|
9
6
|
driverReplies,
|
|
10
7
|
driverStop
|
|
11
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-6WUKLS4D.js";
|
|
9
|
+
import {
|
|
10
|
+
QUEUE_LEASE_INTERRUPT_WAIT_MS,
|
|
11
|
+
QUEUE_LEASE_SESSION_ENV,
|
|
12
|
+
acquireQueueLease,
|
|
13
|
+
forwardTermination,
|
|
14
|
+
gateCheckIn
|
|
15
|
+
} from "./chunk-DTSL2RH7.js";
|
|
16
|
+
import {
|
|
17
|
+
readRunQueueState
|
|
18
|
+
} from "./chunk-7ABY6DYH.js";
|
|
12
19
|
import {
|
|
13
20
|
detectAgentBackend
|
|
14
|
-
} from "./chunk-
|
|
21
|
+
} from "./chunk-C4LCOOL4.js";
|
|
15
22
|
import {
|
|
16
23
|
classifyGateExit,
|
|
17
24
|
landGateExit
|
|
18
|
-
} from "./chunk-
|
|
25
|
+
} from "./chunk-BGL5DRK2.js";
|
|
19
26
|
import {
|
|
20
27
|
buildDispatchPrompt,
|
|
21
28
|
ensureDispatchLogPath,
|
|
22
29
|
readCommandFile,
|
|
23
30
|
runHeadlessDispatch
|
|
24
|
-
} from "./chunk-
|
|
31
|
+
} from "./chunk-SGTTC2JP.js";
|
|
25
32
|
import {
|
|
26
33
|
formatHitlSummary,
|
|
27
34
|
hitlExitCode
|
|
28
|
-
} from "./chunk-
|
|
35
|
+
} from "./chunk-P7732A25.js";
|
|
29
36
|
import {
|
|
30
37
|
HANDOFF_REL
|
|
31
38
|
} from "./chunk-C4Q77GGJ.js";
|
|
@@ -210,20 +217,46 @@ async function runDispatchDriver(opts) {
|
|
|
210
217
|
}
|
|
211
218
|
const logPath = await ensureDispatchLogPath(root);
|
|
212
219
|
say(`Log: ${path.relative(root, logPath)}`);
|
|
213
|
-
const
|
|
220
|
+
const acquired = acquireQueueLease(root, {
|
|
221
|
+
...opts.lease,
|
|
222
|
+
onLost: (by) => say(
|
|
223
|
+
`Queue lease lost: ${by ? `session ${by.session} (pid ${by.dispatcher.pid})` : "the lease file"} replaced this session's lease. Another /run-plan-all may be writing this queue.`,
|
|
224
|
+
"warn"
|
|
225
|
+
)
|
|
226
|
+
});
|
|
227
|
+
if (!acquired.ok) {
|
|
228
|
+
say(acquired.message, "error");
|
|
229
|
+
return finish(1, acquired.message);
|
|
230
|
+
}
|
|
231
|
+
const lease = acquired.handle;
|
|
232
|
+
if (lease.takeover) {
|
|
233
|
+
say(
|
|
234
|
+
`Queue lease: took over a stale lease (${lease.takeover.reason}); the takeover is logged in HANDOFF.`,
|
|
235
|
+
"warn"
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
const previousSession = process.env[QUEUE_LEASE_SESSION_ENV];
|
|
239
|
+
process.env[QUEUE_LEASE_SESSION_ENV] = lease.session;
|
|
240
|
+
const interrupted = (reason) => {
|
|
214
241
|
queue?.stop();
|
|
242
|
+
lease.releaseAfterAgent(QUEUE_LEASE_INTERRUPT_WAIT_MS);
|
|
215
243
|
emitter.emit({
|
|
216
244
|
type: "run_end",
|
|
217
245
|
exitCode: 130,
|
|
218
246
|
ticks,
|
|
219
247
|
pending: null,
|
|
220
|
-
reason
|
|
248
|
+
reason,
|
|
221
249
|
planExhausted: false,
|
|
222
250
|
usage: null
|
|
223
251
|
});
|
|
224
252
|
process.exit(130);
|
|
225
253
|
};
|
|
254
|
+
const onSigInt = () => interrupted("interrupted (SIGINT)");
|
|
226
255
|
process.on("SIGINT", onSigInt);
|
|
256
|
+
const offTermination = forwardTermination(
|
|
257
|
+
() => lease.agent(),
|
|
258
|
+
(signal) => interrupted(`interrupted (${signal})`)
|
|
259
|
+
);
|
|
227
260
|
try {
|
|
228
261
|
ticks = DISPATCH_DRIVER_TICK;
|
|
229
262
|
let result;
|
|
@@ -235,6 +268,7 @@ async function runDispatchDriver(opts) {
|
|
|
235
268
|
prompt,
|
|
236
269
|
model: opts.model,
|
|
237
270
|
logPath,
|
|
271
|
+
...opts.env ? { env: opts.env } : {},
|
|
238
272
|
spawnFn: opts.spawnFn,
|
|
239
273
|
versionFn: opts.versionFn,
|
|
240
274
|
log: (line) => say(line),
|
|
@@ -242,8 +276,10 @@ async function runDispatchDriver(opts) {
|
|
|
242
276
|
hitl: {
|
|
243
277
|
policy: opts.noHitl ? "off" : "driver",
|
|
244
278
|
readAnswer: reader.read,
|
|
245
|
-
write: writeErr
|
|
246
|
-
|
|
279
|
+
write: writeErr,
|
|
280
|
+
checkIn: gateCheckIn(root, () => lease.session, opts.lease)
|
|
281
|
+
},
|
|
282
|
+
onSpawn: (info) => lease.setAgent(info.pid)
|
|
247
283
|
});
|
|
248
284
|
} catch (err) {
|
|
249
285
|
say(String(err), "error");
|
|
@@ -267,6 +303,10 @@ async function runDispatchDriver(opts) {
|
|
|
267
303
|
return finish(exitCode, reason, result);
|
|
268
304
|
} finally {
|
|
269
305
|
process.off("SIGINT", onSigInt);
|
|
306
|
+
offTermination();
|
|
307
|
+
lease.release();
|
|
308
|
+
if (previousSession === void 0) delete process.env[QUEUE_LEASE_SESSION_ENV];
|
|
309
|
+
else process.env[QUEUE_LEASE_SESSION_ENV] = previousSession;
|
|
270
310
|
}
|
|
271
311
|
}
|
|
272
312
|
|
|
@@ -6,13 +6,13 @@ import {
|
|
|
6
6
|
import {
|
|
7
7
|
diffAgainstRegistry,
|
|
8
8
|
summarizeDiff
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-VJWWJA36.js";
|
|
10
10
|
import {
|
|
11
11
|
DEFAULT_REGISTRY_URL,
|
|
12
12
|
assertSafeRegistrySource,
|
|
13
13
|
loadAgentKitManifest,
|
|
14
14
|
resolveRegistryRoot
|
|
15
|
-
} from "./chunk-
|
|
15
|
+
} from "./chunk-3HNTNYPR.js";
|
|
16
16
|
import {
|
|
17
17
|
KIT_PACKAGE_SPEC,
|
|
18
18
|
KIT_VERSION
|