hive-tasks 1.1.0 → 1.4.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/CHANGELOG.md +81 -0
- package/README.md +96 -6
- package/README.zh-CN.md +88 -6
- package/SPEC.md +243 -51
- package/package.json +4 -3
- package/src/cli.js +346 -60
- package/src/role.js +240 -0
- package/src/stage.js +240 -0
- package/src/task.js +28 -3
package/src/cli.js
CHANGED
|
@@ -4,7 +4,22 @@ import path from "node:path";
|
|
|
4
4
|
import { git, out } from "./git.js";
|
|
5
5
|
import { ensureState, repoInfo } from "./repo.js";
|
|
6
6
|
import { listRaw, mutate, readTask } from "./store.js";
|
|
7
|
-
import { create, isReady, isReclaimable, order, transition } from "./task.js";
|
|
7
|
+
import { create, isReady, isReclaimable, order, reopenPatch, transition } from "./task.js";
|
|
8
|
+
import {
|
|
9
|
+
advanceStage,
|
|
10
|
+
currentStage,
|
|
11
|
+
failStage,
|
|
12
|
+
isStaged,
|
|
13
|
+
matchesRole,
|
|
14
|
+
normalizeStages,
|
|
15
|
+
resetStage,
|
|
16
|
+
stageIndex,
|
|
17
|
+
stageRole,
|
|
18
|
+
stageStatePatch,
|
|
19
|
+
stageView,
|
|
20
|
+
stagesFromFlags,
|
|
21
|
+
} from "./stage.js";
|
|
22
|
+
import { resolveRole } from "./role.js";
|
|
8
23
|
import { addVerdict, claimLeases, parseTtl, readMode, releaseLeases, toKey } from "./lease.js";
|
|
9
24
|
import { append } from "./events.js";
|
|
10
25
|
|
|
@@ -15,10 +30,11 @@ const USAGE = [
|
|
|
15
30
|
"",
|
|
16
31
|
"USAGE",
|
|
17
32
|
" hive-tasks init create .hive/ and the AGENTS.md section",
|
|
18
|
-
" hive-tasks add --title T [--role R] [--scope P]... [--after ID]... [--
|
|
19
|
-
" [--goal G] [--acceptance CMD]",
|
|
33
|
+
" hive-tasks add --title T [--role R] [--scope P]... [--after ID]... [--review-of ID]",
|
|
34
|
+
" [--tag T] [--priority N] [--goal G] [--acceptance CMD]",
|
|
35
|
+
" [--stage ROLE[:ACCEPTANCE]]... [--stages FILE]",
|
|
20
36
|
" hive-tasks list [--json] [--state S] [--role R]",
|
|
21
|
-
" hive-tasks show <id> [--json]",
|
|
37
|
+
" hive-tasks show <id> [--json] card plus the resolved role source",
|
|
22
38
|
" hive-tasks ready [--json] [--limit N] [--role R]",
|
|
23
39
|
" hive-tasks next [--role R] [--json] the single best candidate",
|
|
24
40
|
" hive-tasks claim <id> --agent A [--ttl 5m] card CAS, then hive leases",
|
|
@@ -26,8 +42,11 @@ const USAGE = [
|
|
|
26
42
|
" hive-tasks plan --file plan.json create a whole DAG in one transaction",
|
|
27
43
|
" hive-tasks done <id> [--sha S] [--verdict pass|fail] [--verify]",
|
|
28
44
|
" hive-tasks fail <id> --reason R",
|
|
29
|
-
" hive-tasks retry <id>",
|
|
45
|
+
" hive-tasks retry <id> [--stage KEY]",
|
|
30
46
|
" hive-tasks block <id> --reason R | unblock <id>",
|
|
47
|
+
" hive-tasks reopen <id> [--reason R] [--agent A] [--stage KEY]",
|
|
48
|
+
" any state back to pending, attempt+1; a staged card",
|
|
49
|
+
" resets to KEY (default the first stage)",
|
|
31
50
|
"",
|
|
32
51
|
"ENVIRONMENT",
|
|
33
52
|
" HIVE_AGENT=name identity used for claims",
|
|
@@ -45,17 +64,20 @@ function value(argv, index, flag) {
|
|
|
45
64
|
}
|
|
46
65
|
|
|
47
66
|
function parse(argv) {
|
|
48
|
-
const out = { args: [], scopes: [], after: [], tags: [], json: false, reason: null, agent: null, ttl: null, verdict: null, sha: null, state: null, role: null, limit: null, title: null, goal: null, priority: 0, force: false, acceptance: null, verify: false, file: null };
|
|
67
|
+
const out = { args: [], scopes: [], after: [], tags: [], stageArgs: [], json: false, reason: null, agent: null, ttl: null, verdict: null, sha: null, state: null, role: null, limit: null, title: null, goal: null, priority: 0, force: false, acceptance: null, verify: false, file: null, stagesFile: null, reviewOf: null };
|
|
49
68
|
for (let i = 0; i < argv.length; i++) {
|
|
50
69
|
const arg = argv[i];
|
|
51
70
|
if (arg === "--json") out.json = true;
|
|
52
71
|
else if (arg === "--force") out.force = true;
|
|
53
72
|
else if (arg === "--scope") out.scopes.push(value(argv, i++, arg));
|
|
54
73
|
else if (arg === "--after") out.after.push(value(argv, i++, arg));
|
|
74
|
+
else if (arg === "--review-of") out.reviewOf = value(argv, i++, arg);
|
|
55
75
|
else if (arg === "--tag") out.tags.push(value(argv, i++, arg));
|
|
56
76
|
else if (arg === "--acceptance") out.acceptance = value(argv, i++, arg);
|
|
57
77
|
else if (arg === "--verify") out.verify = true;
|
|
58
78
|
else if (arg === "--file") out.file = value(argv, i++, arg);
|
|
79
|
+
else if (arg === "--stages") out.stagesFile = value(argv, i++, arg);
|
|
80
|
+
else if (arg === "--stage") out.stageArgs.push(value(argv, i++, arg));
|
|
59
81
|
else if (arg === "--reason") out.reason = value(argv, i++, arg);
|
|
60
82
|
else if (arg === "--agent") out.agent = value(argv, i++, arg);
|
|
61
83
|
else if (arg === "--ttl") out.ttl = value(argv, i++, arg);
|
|
@@ -79,6 +101,19 @@ function identity(repo, explicit) {
|
|
|
79
101
|
return name;
|
|
80
102
|
}
|
|
81
103
|
|
|
104
|
+
const SUMMARY_MAX = 120;
|
|
105
|
+
|
|
106
|
+
/** One-line reason summary stored on the card that is reopened by a failed review. */
|
|
107
|
+
function summarize(text) {
|
|
108
|
+
const flat = String(text || "").replace(/\s+/g, " ").trim();
|
|
109
|
+
return flat.length > SUMMARY_MAX ? flat.slice(0, SUMMARY_MAX - 1) + "…" : flat;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function reviewFailureReason(reason) {
|
|
113
|
+
const detail = summarize(reason);
|
|
114
|
+
return detail ? "test failed: " + detail : "test failed";
|
|
115
|
+
}
|
|
116
|
+
|
|
82
117
|
function board(repo) {
|
|
83
118
|
return listRaw(repo).map((entry) => entry.value);
|
|
84
119
|
}
|
|
@@ -87,41 +122,79 @@ function byId(tasks) {
|
|
|
87
122
|
return new Map(tasks.map((task) => [task.id, task]));
|
|
88
123
|
}
|
|
89
124
|
|
|
125
|
+
/** The target key of a retry/reopen --stage flag (the last one wins). */
|
|
126
|
+
function stageTarget(options) {
|
|
127
|
+
return options.stageArgs.length ? options.stageArgs[options.stageArgs.length - 1] : null;
|
|
128
|
+
}
|
|
129
|
+
|
|
90
130
|
function readyList(repo, tasks, { role } = {}) {
|
|
91
131
|
const free = readMode(repo) === "free";
|
|
92
132
|
const map = byId(tasks);
|
|
93
133
|
return tasks
|
|
94
|
-
.filter((task) => (
|
|
134
|
+
.filter((task) => matchesRole(task, role) && isReady(task, map, { modeFree: free }))
|
|
95
135
|
.sort(order);
|
|
96
136
|
}
|
|
97
137
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
const value = { v: 1, agent, task: task ? task.id : "", state, files: files || [], updatedAt: new Date().toISOString(), host: "hive-tasks", pid: process.pid };
|
|
102
|
-
const blob = out(["hash-object", "-w", "--stdin"], { cwd: repo.top, input: JSON.stringify(value) });
|
|
103
|
-
const current = git(["rev-parse", "--verify", "--quiet", ref], { cwd: repo.top, allowFail: true }).stdout.trim();
|
|
104
|
-
git(["update-ref", ref, blob, current || "0".repeat(blob.length)], { cwd: repo.top, allowFail: true });
|
|
138
|
+
/** JSON/print view of a card: stages, stage and stageState are always present. */
|
|
139
|
+
function viewTask(task) {
|
|
140
|
+
return { ...task, ...stageView(task) };
|
|
105
141
|
}
|
|
106
142
|
|
|
107
|
-
function printTask(task, json) {
|
|
108
|
-
if (json) return process.stdout.write(JSON.stringify(task, null, 2) + "\n");
|
|
143
|
+
function printTask(task, json, extra = {}, extraLines = []) {
|
|
144
|
+
if (json) return process.stdout.write(JSON.stringify({ ...viewTask(task), ...extra }, null, 2) + "\n");
|
|
109
145
|
const indent = " ";
|
|
110
146
|
process.stdout.write(task.id + " [" + task.state + "] " + task.title + "\n");
|
|
111
147
|
process.stdout.write(indent + "role: " + task.role + " owner: " + (task.owner || "-") + " attempt: " + task.attempt + "\n");
|
|
148
|
+
if (isStaged(task)) {
|
|
149
|
+
const stage = currentStage(task);
|
|
150
|
+
const parts = ["stage: " + task.stage + " (" + task.stageState + ") stage-role: " + (stage ? stage.role : "-")];
|
|
151
|
+
if (stage && stage.acceptance) parts.push("acceptance: " + stage.acceptance);
|
|
152
|
+
process.stdout.write(indent + parts.join(" ") + "\n");
|
|
153
|
+
if (task.stages.length > 1) {
|
|
154
|
+
process.stdout.write(indent + "chain: " + task.stages.map((entry) => entry.key + ":" + entry.state).join(" -> ") + "\n");
|
|
155
|
+
}
|
|
156
|
+
}
|
|
112
157
|
if (task.scope.length) process.stdout.write(indent + "scope: " + task.scope.join(", ") + "\n");
|
|
113
158
|
if (task.after.length) process.stdout.write(indent + "after: " + task.after.join(", ") + "\n");
|
|
159
|
+
if (task.reviewOf) process.stdout.write(indent + "review-of: " + task.reviewOf + "\n");
|
|
114
160
|
if (task.goal) process.stdout.write(indent + "goal: " + task.goal + "\n");
|
|
161
|
+
for (const line of extraLines) process.stdout.write(indent + line + "\n");
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function readStagesFile(file) {
|
|
165
|
+
let raw;
|
|
166
|
+
try {
|
|
167
|
+
raw = fs.readFileSync(file, "utf8");
|
|
168
|
+
} catch {
|
|
169
|
+
throw new Error("could not read stages file: " + file);
|
|
170
|
+
}
|
|
171
|
+
let parsed;
|
|
172
|
+
try {
|
|
173
|
+
parsed = JSON.parse(raw);
|
|
174
|
+
} catch {
|
|
175
|
+
throw new Error("stages file is not valid JSON: " + file);
|
|
176
|
+
}
|
|
177
|
+
if (!Array.isArray(parsed)) throw new Error("stages file must be a JSON array");
|
|
178
|
+
return parsed;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function publishPresence(repo, { agent, task, state, files }) {
|
|
182
|
+
const key = toKey(agent);
|
|
183
|
+
const ref = "refs/hive/v1/board/" + key + "/__key__";
|
|
184
|
+
const value = { v: 1, agent, task: task ? task.id : "", state, files: files || [], updatedAt: new Date().toISOString(), host: "hive-tasks", pid: process.pid };
|
|
185
|
+
const blob = out(["hash-object", "-w", "--stdin"], { cwd: repo.top, input: JSON.stringify(value) });
|
|
186
|
+
const current = git(["rev-parse", "--verify", "--quiet", ref], { cwd: repo.top, allowFail: true }).stdout.trim();
|
|
187
|
+
git(["update-ref", ref, blob, current || "0".repeat(blob.length)], { cwd: repo.top, allowFail: true });
|
|
115
188
|
}
|
|
116
189
|
|
|
117
190
|
function run(argv) {
|
|
118
191
|
const command = argv[0];
|
|
119
192
|
const options = parse(argv.slice(1));
|
|
120
|
-
const repo = repoInfo(process.cwd());
|
|
121
|
-
|
|
122
193
|
if (!command || command === "-h" || command === "--help" || command === "help") return process.stdout.write(USAGE + "\n");
|
|
123
194
|
if (command === "-V" || command === "--version" || command === "version") return process.stdout.write(VERSION + "\n");
|
|
124
195
|
|
|
196
|
+
const repo = repoInfo(process.cwd());
|
|
197
|
+
|
|
125
198
|
if (command === "init") {
|
|
126
199
|
ensureState(repo);
|
|
127
200
|
const file = path.join(repo.top, "AGENTS.md");
|
|
@@ -142,17 +215,23 @@ function run(argv) {
|
|
|
142
215
|
const keys = new Map();
|
|
143
216
|
const drafts = plan.map((entry) => {
|
|
144
217
|
if (!entry || !entry.title) fail("every plan entry needs a title", 2);
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
218
|
+
let task;
|
|
219
|
+
try {
|
|
220
|
+
task = create({
|
|
221
|
+
title: entry.title,
|
|
222
|
+
goal: entry.goal,
|
|
223
|
+
role: entry.role,
|
|
224
|
+
scope: entry.scope || [],
|
|
225
|
+
after: [],
|
|
226
|
+
priority: entry.priority,
|
|
227
|
+
tags: entry.tags || [],
|
|
228
|
+
acceptance: entry.acceptance || "",
|
|
229
|
+
createdBy: options.agent || process.env.HIVE_AGENT || "planner",
|
|
230
|
+
stages: entry.stages,
|
|
231
|
+
});
|
|
232
|
+
} catch (error) {
|
|
233
|
+
fail(error.message, 2);
|
|
234
|
+
}
|
|
156
235
|
if (entry.key) keys.set(entry.key, task.id);
|
|
157
236
|
return { entry, task };
|
|
158
237
|
});
|
|
@@ -188,18 +267,79 @@ function run(argv) {
|
|
|
188
267
|
return;
|
|
189
268
|
}
|
|
190
269
|
|
|
270
|
+
if (command === "reopen") {
|
|
271
|
+
const id = options.args[0];
|
|
272
|
+
if (!id) fail("reopen needs a task id", 2);
|
|
273
|
+
const agent = options.agent || process.env.HIVE_AGENT || "unknown";
|
|
274
|
+
const reason = options.reason || "reopened";
|
|
275
|
+
const target = stageTarget(options);
|
|
276
|
+
const current = readTask(repo, id);
|
|
277
|
+
if (!current) fail("no such task: " + id, 2);
|
|
278
|
+
if (target && !isStaged(current)) fail("--stage only applies to a card with stages", 2);
|
|
279
|
+
if (target && isStaged(current) && stageIndex(current, target) < 0) fail("no such stage: " + target, 2);
|
|
280
|
+
|
|
281
|
+
// Legacy cards keep the idempotent no-op. A staged card always resets to
|
|
282
|
+
// the requested stage (default the first), so a chain can be re-run.
|
|
283
|
+
if (!isStaged(current) && current.state === "pending") {
|
|
284
|
+
append(repo, { actor: agent, kind: "reopen", task: id, reason, noop: true });
|
|
285
|
+
return printTask(current, options.json);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
let previous = null;
|
|
289
|
+
let reopened = null;
|
|
290
|
+
const result = mutate(repo, (tasks) => {
|
|
291
|
+
previous = null;
|
|
292
|
+
reopened = null;
|
|
293
|
+
const task = byId(tasks).get(id);
|
|
294
|
+
if (!task) throw new Error("no such task: " + id);
|
|
295
|
+
if (!isStaged(task) && task.state === "pending") return null;
|
|
296
|
+
previous = task;
|
|
297
|
+
if (isStaged(task)) {
|
|
298
|
+
const reset = resetStage(task, target);
|
|
299
|
+
reopened = transition(task, "pending", { ...reopenPatch(task, reason), ...reset.patch });
|
|
300
|
+
} else {
|
|
301
|
+
reopened = transition(task, "pending", reopenPatch(task, reason));
|
|
302
|
+
}
|
|
303
|
+
return tasks.map((entry) => (entry.id === id ? reopened : entry));
|
|
304
|
+
});
|
|
305
|
+
if (!result.ok) fail("could not reopen " + id);
|
|
306
|
+
|
|
307
|
+
if (!reopened) {
|
|
308
|
+
append(repo, { actor: agent, kind: "reopen", task: id, reason, noop: true });
|
|
309
|
+
return printTask(readTask(repo, id), options.json);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// A claimed card may still hold hive leases; release exactly the claim we reset.
|
|
313
|
+
if (previous.state === "claimed") releaseLeases(repo, { paths: previous.scope, agent: previous.owner || agent });
|
|
314
|
+
append(repo, { actor: agent, kind: "reopen", task: id, from: previous.state, reason, attempt: reopened.attempt, stage: isStaged(reopened) ? reopened.stage : undefined });
|
|
315
|
+
return printTask(reopened, options.json);
|
|
316
|
+
}
|
|
317
|
+
|
|
191
318
|
if (command === "add") {
|
|
192
319
|
if (!options.title) fail("--title is required", 2);
|
|
320
|
+
if (options.reviewOf && !readTask(repo, options.reviewOf)) fail("no such task: " + options.reviewOf, 2);
|
|
321
|
+
if (options.stageArgs.length && options.stagesFile) fail("use --stage or --stages, not both", 2);
|
|
322
|
+
let stages = null;
|
|
323
|
+
try {
|
|
324
|
+
if (options.stagesFile) stages = normalizeStages(readStagesFile(options.stagesFile));
|
|
325
|
+
else if (options.stageArgs.length) stages = stagesFromFlags(options.stageArgs);
|
|
326
|
+
} catch (error) {
|
|
327
|
+
fail(error.message, 2);
|
|
328
|
+
}
|
|
329
|
+
const after = [...options.after];
|
|
330
|
+
if (options.reviewOf && !after.includes(options.reviewOf)) after.push(options.reviewOf);
|
|
193
331
|
const task = create({
|
|
194
332
|
title: options.title,
|
|
195
333
|
goal: options.goal,
|
|
196
334
|
role: options.role,
|
|
197
335
|
scope: options.scopes,
|
|
198
|
-
after
|
|
336
|
+
after,
|
|
199
337
|
priority: options.priority,
|
|
200
338
|
tags: options.tags,
|
|
201
339
|
acceptance: options.acceptance || "",
|
|
340
|
+
reviewOf: options.reviewOf,
|
|
202
341
|
createdBy: options.agent || process.env.HIVE_AGENT || "unknown",
|
|
342
|
+
stages,
|
|
203
343
|
});
|
|
204
344
|
const result = mutate(repo, (tasks) => {
|
|
205
345
|
const ids = byId(tasks);
|
|
@@ -207,7 +347,7 @@ function run(argv) {
|
|
|
207
347
|
return [...tasks, task];
|
|
208
348
|
});
|
|
209
349
|
if (!result.ok) fail("could not write the task board (too much contention)");
|
|
210
|
-
append(repo, { actor: task.createdBy, kind: "task_add", task: task.id });
|
|
350
|
+
append(repo, { actor: task.createdBy, kind: "task_add", task: task.id, stages: isStaged(task) ? task.stages.length : undefined });
|
|
211
351
|
return printTask(task, options.json);
|
|
212
352
|
}
|
|
213
353
|
|
|
@@ -216,12 +356,18 @@ function run(argv) {
|
|
|
216
356
|
const selected = command === "ready"
|
|
217
357
|
? readyList(repo, tasks, { role: options.role })
|
|
218
358
|
: tasks
|
|
219
|
-
.filter((task) => (!options.state || task.state === options.state) && (
|
|
359
|
+
.filter((task) => (!options.state || task.state === options.state) && matchesRole(task, options.role))
|
|
220
360
|
.sort(order);
|
|
221
361
|
const limited = options.limit ? selected.slice(0, options.limit) : selected;
|
|
222
|
-
if (options.json)
|
|
362
|
+
if (options.json) {
|
|
363
|
+
const rows = limited.map((task) => {
|
|
364
|
+
const view = viewTask(task);
|
|
365
|
+
return command === "ready" ? { ...view, readyRole: stageRole(task) } : view;
|
|
366
|
+
});
|
|
367
|
+
return process.stdout.write(JSON.stringify(rows, null, 2) + "\n");
|
|
368
|
+
}
|
|
223
369
|
if (limited.length === 0) return process.stdout.write(command === "ready" ? "Nothing ready.\n" : "No tasks.\n");
|
|
224
|
-
for (const task of limited) process.stdout.write(task.id.padEnd(34) + task.state.padEnd(9) + task.
|
|
370
|
+
for (const task of limited) process.stdout.write(task.id.padEnd(34) + task.state.padEnd(9) + stageRole(task).padEnd(11) + task.title + "\n");
|
|
225
371
|
return;
|
|
226
372
|
}
|
|
227
373
|
|
|
@@ -232,7 +378,7 @@ function run(argv) {
|
|
|
232
378
|
else process.stdout.write("Nothing ready.\n");
|
|
233
379
|
return;
|
|
234
380
|
}
|
|
235
|
-
return printTask(first, options.json);
|
|
381
|
+
return printTask(first, options.json, { readyRole: stageRole(first) });
|
|
236
382
|
}
|
|
237
383
|
|
|
238
384
|
if (command === "show") {
|
|
@@ -240,7 +386,10 @@ function run(argv) {
|
|
|
240
386
|
if (!id) fail("show needs a task id", 2);
|
|
241
387
|
const task = readTask(repo, id);
|
|
242
388
|
if (!task) fail("no such task: " + id, 2);
|
|
243
|
-
|
|
389
|
+
const active = stageRole(task);
|
|
390
|
+
const resolved = resolveRole(repo, active, isStaged(task) ? task.stage : {});
|
|
391
|
+
const lines = ["role-source: " + resolved.source + " (adapter " + resolved.adapter + ", model " + (resolved.model || "-") + ")"];
|
|
392
|
+
return printTask(task, options.json, { roleSource: resolved.source, roleResolved: resolved }, lines);
|
|
244
393
|
}
|
|
245
394
|
|
|
246
395
|
if (command === "claim") {
|
|
@@ -248,6 +397,7 @@ function run(argv) {
|
|
|
248
397
|
if (!id) fail("claim needs a task id", 2);
|
|
249
398
|
const agent = identity(repo, options.agent);
|
|
250
399
|
const ttlMs = parseTtl(options.ttl);
|
|
400
|
+
const stageOf = (task) => (isStaged(task) ? " at stage " + task.stage : "");
|
|
251
401
|
|
|
252
402
|
const preview = readTask(repo, id);
|
|
253
403
|
if (!preview) fail("no such task: " + id, 2);
|
|
@@ -255,7 +405,10 @@ function run(argv) {
|
|
|
255
405
|
fail("mode is freeze: only fix-tagged cards may be claimed (use --force to override)", 2);
|
|
256
406
|
}
|
|
257
407
|
const reclaim = isReclaimable(preview);
|
|
258
|
-
if (preview.state !== "pending" && !reclaim) fail("task " + id + " is " + preview.state, 2);
|
|
408
|
+
if (preview.state !== "pending" && !reclaim) fail("task " + id + " is " + preview.state + stageOf(preview), 2);
|
|
409
|
+
if (isStaged(preview) && preview.stageState !== "pending" && !(preview.stageState === "claimed" && reclaim)) {
|
|
410
|
+
fail("task " + id + " stage " + preview.stage + " is " + preview.stageState, 2);
|
|
411
|
+
}
|
|
259
412
|
if (!isReady(preview, byId(board(repo)), { modeFree: true })) fail("task " + id + " is not ready (dependencies or mode)", 2);
|
|
260
413
|
|
|
261
414
|
let claimed = null;
|
|
@@ -264,14 +417,20 @@ function run(argv) {
|
|
|
264
417
|
const map = byId(tasks);
|
|
265
418
|
const task = map.get(id);
|
|
266
419
|
if (!task) throw new Error("no such task: " + id);
|
|
267
|
-
|
|
420
|
+
const canReclaim = isReclaimable(task);
|
|
421
|
+
if (task.state !== "pending" && !canReclaim) throw new Error("task " + id + " is " + task.state + stageOf(task));
|
|
422
|
+
if (isStaged(task) && task.stageState !== "pending" && !(task.stageState === "claimed" && canReclaim)) {
|
|
423
|
+
throw new Error("task " + id + " stage " + task.stage + " is " + task.stageState);
|
|
424
|
+
}
|
|
268
425
|
if (!isReady(task, map, { modeFree: true })) throw new Error("task " + id + " is not ready (dependencies or mode)");
|
|
269
|
-
|
|
426
|
+
const patch = {
|
|
270
427
|
owner: agent,
|
|
271
428
|
attempt: (task.attempt || 0) + 1,
|
|
272
429
|
reason: null,
|
|
273
430
|
claimedUntil: new Date(Date.now() + ttlMs).toISOString(),
|
|
274
|
-
|
|
431
|
+
...stageStatePatch(task, "claimed"),
|
|
432
|
+
};
|
|
433
|
+
claimed = transition(task, "claimed", patch);
|
|
275
434
|
return tasks.map((entry) => (entry.id === id ? claimed : entry));
|
|
276
435
|
});
|
|
277
436
|
} catch (error) {
|
|
@@ -283,13 +442,16 @@ function run(argv) {
|
|
|
283
442
|
const leases = claimLeases(repo, { paths: claimed.scope, agent, ttlMs, taskId: claimed.id });
|
|
284
443
|
if (leases.granted === false) {
|
|
285
444
|
const holder = leases.denied[0];
|
|
286
|
-
mutate(repo, (tasks) => tasks.map((entry) =>
|
|
445
|
+
mutate(repo, (tasks) => tasks.map((entry) => {
|
|
446
|
+
if (entry.id !== id) return entry;
|
|
447
|
+
return transition(entry, "pending", { owner: null, claimedUntil: null, reason: "lease held by " + (holder ? holder.agent : "another agent"), ...stageStatePatch(entry, "pending") });
|
|
448
|
+
}));
|
|
287
449
|
const detail = holder ? " (" + holder.agent + " holds " + holder.key + ")" : "";
|
|
288
450
|
fail("could not claim " + id + ": path busy" + detail, 2);
|
|
289
451
|
}
|
|
290
452
|
|
|
291
453
|
publishPresence(repo, { agent, task: claimed, state: "working", files: claimed.scope });
|
|
292
|
-
append(repo, { actor: agent, kind: "task_claim", task: claimed.id, attempt: claimed.attempt });
|
|
454
|
+
append(repo, { actor: agent, kind: "task_claim", task: claimed.id, attempt: claimed.attempt, stage: isStaged(claimed) ? claimed.stage : undefined });
|
|
293
455
|
return printTask(claimed, options.json);
|
|
294
456
|
}
|
|
295
457
|
|
|
@@ -299,39 +461,163 @@ function run(argv) {
|
|
|
299
461
|
const agent = options.agent || process.env.HIVE_AGENT || "unknown";
|
|
300
462
|
const current = readTask(repo, id);
|
|
301
463
|
if (!current) fail("no such task: " + id, 2);
|
|
464
|
+
const target = stageTarget(options);
|
|
465
|
+
if (target && command !== "retry") fail("--stage only applies to retry and reopen", 2);
|
|
466
|
+
if (target && !isStaged(current)) fail("--stage only applies to a card with stages", 2);
|
|
467
|
+
if (target && isStaged(current) && stageIndex(current, target) < 0) fail("no such stage: " + target, 2);
|
|
302
468
|
|
|
303
469
|
if ((command === "done" || command === "fail") && current.state !== "claimed") fail("task " + id + " is " + current.state + ", not claimed", 2);
|
|
470
|
+
if ((command === "done" || command === "fail") && isStaged(current) && current.stageState !== "claimed") fail("task " + id + " stage " + current.stage + " is " + current.stageState + ", not claimed", 2);
|
|
304
471
|
if (command === "retry" && current.state !== "failed" && current.state !== "blocked") fail("task " + id + " is " + current.state + ", not failed or blocked", 2);
|
|
305
472
|
if (command === "unblock" && current.state !== "blocked") fail("task " + id + " is " + current.state + ", not blocked", 2);
|
|
306
473
|
|
|
307
474
|
if (command === "done" && options.verify) {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
if (
|
|
312
|
-
if (
|
|
475
|
+
const acceptance = isStaged(current) ? ((currentStage(current) || {}).acceptance || current.acceptance) : current.acceptance;
|
|
476
|
+
if (!acceptance) fail("task " + id + " has no acceptance command", 2);
|
|
477
|
+
const runVerify = spawnSync("sh", ["-c", acceptance], { cwd: repo.top, encoding: "utf8" });
|
|
478
|
+
if (runVerify.stdout) process.stdout.write(runVerify.stdout);
|
|
479
|
+
if (runVerify.stderr) process.stderr.write(runVerify.stderr);
|
|
480
|
+
if (runVerify.status !== 0) fail("acceptance failed for " + id, 1);
|
|
313
481
|
}
|
|
314
482
|
|
|
315
483
|
const head = git(["rev-parse", "--verify", "--quiet", "HEAD"], { cwd: repo.top, allowFail: true }).stdout.trim();
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
484
|
+
|
|
485
|
+
// Build the transition from the card as it is inside the CAS, so two
|
|
486
|
+
// writers cannot advance the same stage twice.
|
|
487
|
+
function planCommand(task) {
|
|
488
|
+
if (command === "done") {
|
|
489
|
+
if (isStaged(task)) {
|
|
490
|
+
if (task.stageState !== "claimed") throw new Error("task " + id + " stage " + task.stage + " is " + task.stageState + ", not claimed");
|
|
491
|
+
const advanced = advanceStage(task);
|
|
492
|
+
const events = [{ kind: "stage_done", stage: task.stage }];
|
|
493
|
+
if (advanced.nextStage) events.push({ kind: "stage_advanced", from: task.stage, stage: advanced.nextStage });
|
|
494
|
+
return {
|
|
495
|
+
targetState: advanced.finished ? "done" : "pending",
|
|
496
|
+
nextStage: advanced.nextStage,
|
|
497
|
+
patch: {
|
|
498
|
+
owner: null,
|
|
499
|
+
claimedUntil: null,
|
|
500
|
+
reason: null,
|
|
501
|
+
resultCommit: options.sha || head || null,
|
|
502
|
+
verdict: advanced.finished ? (options.verdict || null) : task.verdict,
|
|
503
|
+
...advanced.patch,
|
|
504
|
+
},
|
|
505
|
+
events,
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
return {
|
|
509
|
+
targetState: "done",
|
|
510
|
+
nextStage: null,
|
|
511
|
+
patch: { owner: null, claimedUntil: null, reason: options.reason || null, verdict: options.verdict || null, resultCommit: options.sha || head || null },
|
|
512
|
+
events: [],
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
if (command === "fail") {
|
|
516
|
+
if (isStaged(task)) {
|
|
517
|
+
const failed = failStage(task, "failed");
|
|
518
|
+
const events = [{ kind: "stage_failed", stage: failed.from, state: "failed" }];
|
|
519
|
+
if (failed.rewound) events.push({ kind: "stage_rewound", from: failed.from, stage: failed.to, attempt: (task.attempt || 0) + 1 });
|
|
520
|
+
return {
|
|
521
|
+
targetState: failed.rewound ? "pending" : "failed",
|
|
522
|
+
nextStage: null,
|
|
523
|
+
patch: { owner: null, claimedUntil: null, reason: options.reason || null, ...failed.patch },
|
|
524
|
+
events,
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
return { targetState: "failed", nextStage: null, patch: { owner: null, claimedUntil: null, reason: options.reason || null }, events: [] };
|
|
528
|
+
}
|
|
529
|
+
if (command === "block") {
|
|
530
|
+
if (isStaged(task)) {
|
|
531
|
+
const blocked = failStage(task, "blocked");
|
|
532
|
+
const events = blocked.rewound ? [{ kind: "stage_rewound", from: blocked.from, stage: blocked.to, attempt: (task.attempt || 0) + 1 }] : [];
|
|
533
|
+
return {
|
|
534
|
+
targetState: blocked.rewound ? "pending" : "blocked",
|
|
535
|
+
nextStage: null,
|
|
536
|
+
patch: { owner: null, claimedUntil: null, reason: options.reason || null, ...blocked.patch },
|
|
537
|
+
events,
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
return { targetState: "blocked", nextStage: null, patch: { owner: null, claimedUntil: null, reason: options.reason || null }, events: [] };
|
|
541
|
+
}
|
|
542
|
+
if (command === "retry") {
|
|
543
|
+
if (isStaged(task)) {
|
|
544
|
+
const reset = resetStage(task, target || task.stage);
|
|
545
|
+
return { targetState: "pending", nextStage: null, patch: { owner: null, claimedUntil: null, reason: options.reason || null, ...reset.patch }, events: [] };
|
|
546
|
+
}
|
|
547
|
+
return { targetState: "pending", nextStage: null, patch: { owner: null, claimedUntil: null, reason: options.reason || null }, events: [] };
|
|
548
|
+
}
|
|
549
|
+
// unblock
|
|
550
|
+
if (isStaged(task)) {
|
|
551
|
+
return { targetState: "pending", nextStage: null, patch: { owner: null, claimedUntil: null, reason: options.reason || null, ...stageStatePatch(task, "pending") }, events: [] };
|
|
552
|
+
}
|
|
553
|
+
return { targetState: "pending", nextStage: null, patch: { owner: null, claimedUntil: null, reason: options.reason || null }, events: [] };
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
// A review card that fails sends its upstream card back to pending, so a
|
|
557
|
+
// rejected implementation becomes claimable again with a fresh attempt.
|
|
558
|
+
const upstreamReason = reviewFailureReason(options.reason);
|
|
559
|
+
let upstreamReopened = null;
|
|
560
|
+
let outcome = null;
|
|
561
|
+
const result = mutate(repo, (tasks) => {
|
|
562
|
+
const map = byId(tasks);
|
|
563
|
+
const task = map.get(id);
|
|
564
|
+
if (!task) throw new Error("no such task: " + id);
|
|
565
|
+
outcome = planCommand(task);
|
|
566
|
+
const next = transition(task, outcome.targetState, outcome.patch);
|
|
567
|
+
upstreamReopened = null;
|
|
568
|
+
if (command === "fail" && outcome.targetState === "failed" && task.reviewOf) {
|
|
569
|
+
const upstream = map.get(task.reviewOf);
|
|
570
|
+
if (upstream && upstream.state === "done") {
|
|
571
|
+
upstreamReopened = transition(upstream, "pending", reopenPatch(upstream, upstreamReason));
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
return tasks.map((entry) => {
|
|
575
|
+
if (entry.id === id) return next;
|
|
576
|
+
if (upstreamReopened && entry.id === upstreamReopened.id) return upstreamReopened;
|
|
577
|
+
return entry;
|
|
578
|
+
});
|
|
579
|
+
});
|
|
326
580
|
if (!result.ok) fail("could not update " + id);
|
|
327
581
|
|
|
328
582
|
if (current.state === "claimed") releaseLeases(repo, { paths: current.scope, agent: current.owner || agent });
|
|
329
583
|
if (command === "done" && options.verdict && options.sha) {
|
|
330
584
|
addVerdict(repo, { sha: options.sha, result: options.verdict });
|
|
331
585
|
}
|
|
332
|
-
publishPresence(repo, { agent, task: current, state: targetState === "done" ? "idle" : "working", files: [] });
|
|
333
|
-
append(repo, { actor: agent, kind: "task_" + command, task: id, reason: options.reason || undefined });
|
|
334
|
-
|
|
586
|
+
publishPresence(repo, { agent, task: current, state: outcome.targetState === "done" ? "idle" : "working", files: [] });
|
|
587
|
+
append(repo, { actor: agent, kind: "task_" + command, task: id, reason: options.reason || undefined, stage: isStaged(current) ? current.stage : undefined });
|
|
588
|
+
for (const event of outcome.events) append(repo, { actor: agent, task: id, ...event });
|
|
589
|
+
if (command === "fail" && current.reviewOf && outcome.targetState === "failed") {
|
|
590
|
+
if (upstreamReopened) {
|
|
591
|
+
append(repo, { actor: agent, kind: "reopen", task: upstreamReopened.id, from: "done", reason: upstreamReason, attempt: upstreamReopened.attempt, via: "review_failed" });
|
|
592
|
+
}
|
|
593
|
+
append(repo, { actor: agent, kind: "review_failed", task: id, reviewOf: current.reviewOf, reopened: Boolean(upstreamReopened), reason: options.reason || undefined });
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
const finalCard = readTask(repo, id);
|
|
597
|
+
const lines = [];
|
|
598
|
+
if (command === "done" && isStaged(current)) {
|
|
599
|
+
lines.push(outcome.nextStage ? "next stage: " + outcome.nextStage + " (card stays pending)" : "stage chain complete");
|
|
600
|
+
}
|
|
601
|
+
const extra = command === "done" ? { nextStage: outcome.nextStage } : {};
|
|
602
|
+
return printTask(finalCard, options.json, extra, lines);
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
if (command === "stats") {
|
|
606
|
+
const items = board(repo);
|
|
607
|
+
const byState = {};
|
|
608
|
+
const byRole = {};
|
|
609
|
+
let oldest = null;
|
|
610
|
+
for (const task of items) {
|
|
611
|
+
byState[task.state] = (byState[task.state] || 0) + 1;
|
|
612
|
+
byRole[task.role] = (byRole[task.role] || 0) + 1;
|
|
613
|
+
if (task.state === "pending" && (!oldest || task.createdAt < oldest.createdAt)) oldest = task;
|
|
614
|
+
}
|
|
615
|
+
const summary = { total: items.length, byState, byRole, oldestPending: oldest ? { id: oldest.id, createdAt: oldest.createdAt } : null };
|
|
616
|
+
if (options.json) return process.stdout.write(JSON.stringify(summary, null, 2) + "\n");
|
|
617
|
+
process.stdout.write("cards: " + summary.total + "\n");
|
|
618
|
+
for (const [state, count] of Object.entries(byState)) process.stdout.write(" " + state.padEnd(8) + count + "\n");
|
|
619
|
+
if (oldest) process.stdout.write("oldest pending: " + oldest.id + " (" + oldest.createdAt + ")\n");
|
|
620
|
+
return;
|
|
335
621
|
}
|
|
336
622
|
|
|
337
623
|
fail("unknown command: " + command, 2);
|