hive-tasks 1.7.0 → 1.9.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 +110 -0
- package/README.md +181 -70
- package/README.zh-CN.md +173 -74
- package/SPEC.md +252 -117
- package/package.json +1 -1
- package/src/cli.js +446 -12
- package/src/lease.js +12 -0
- package/src/task.js +32 -2
package/src/cli.js
CHANGED
|
@@ -4,7 +4,7 @@ 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, reopenPatch, transition } from "./task.js";
|
|
7
|
+
import { create, isDraft, isReady, isReclaimable, isSuperseded, now, order, reopenPatch, transition } from "./task.js";
|
|
8
8
|
import {
|
|
9
9
|
advanceStage,
|
|
10
10
|
currentStage,
|
|
@@ -20,7 +20,7 @@ import {
|
|
|
20
20
|
stagesFromFlags,
|
|
21
21
|
} from "./stage.js";
|
|
22
22
|
import { resolveRole } from "./role.js";
|
|
23
|
-
import { addVerdict, claimLeases, parseTtl, readMode, releaseLeases, toKey } from "./lease.js";
|
|
23
|
+
import { addVerdict, claimLeases, listLeases, parseTtl, readMode, releaseLeases, toKey } from "./lease.js";
|
|
24
24
|
import { append } from "./events.js";
|
|
25
25
|
|
|
26
26
|
const VERSION = JSON.parse(fs.readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
|
|
@@ -30,16 +30,25 @@ const USAGE = [
|
|
|
30
30
|
"",
|
|
31
31
|
"USAGE",
|
|
32
32
|
" hive-tasks init create .hive/ and the AGENTS.md section",
|
|
33
|
-
" hive-tasks add --title T [--role R] [--scope P]...
|
|
34
|
-
" [--tag T] [--priority N] [--goal G]
|
|
33
|
+
" hive-tasks add --title T (--acceptance CMD | --draft) [--role R] [--scope P]...",
|
|
34
|
+
" [--after ID]... [--review-of ID] [--tag T] [--priority N] [--goal G]",
|
|
35
35
|
" [--stage ROLE[:ACCEPTANCE]]... [--stages FILE]",
|
|
36
|
+
" hive-tasks edit <id> [--title T] [--scope-add P]... [--scope-remove P]...",
|
|
37
|
+
" [--acceptance CMD] [--priority N] [--after-set ID,ID] [--tag +T|-T]... [--role R]",
|
|
38
|
+
" change only the fields you pass",
|
|
39
|
+
" hive-tasks split <id> --into '<json array>' [--keep] [--reason R] [--json]",
|
|
40
|
+
" replace a task with finer entries; the old",
|
|
41
|
+
" task is superseded and points at the first subtask",
|
|
42
|
+
" hive-tasks supersede <id> --reason R [--replaced-by ID]",
|
|
43
|
+
" retire a task without done/failed; never dispatched",
|
|
36
44
|
" hive-tasks list [--json] [--state S] [--role R]",
|
|
37
45
|
" hive-tasks show <id> [--json] card plus the resolved role source",
|
|
38
46
|
" hive-tasks ready [--json] [--limit N] [--role R]",
|
|
39
47
|
" hive-tasks next [--role R] [--json] the single best candidate",
|
|
40
48
|
" hive-tasks claim <id> --agent A [--ttl 5m] card CAS, then announce scope paths",
|
|
41
49
|
" hive-tasks renew <id> [--ttl 5m] extend a claim",
|
|
42
|
-
" hive-tasks plan --file plan.json create a whole DAG in one transaction",
|
|
50
|
+
" hive-tasks plan --file plan.json create a whole DAG in one transaction;",
|
|
51
|
+
" every entry needs acceptance unless draft: true",
|
|
43
52
|
" hive-tasks done <id> [--sha S] [--verdict pass|fail] [--verify]",
|
|
44
53
|
" hive-tasks fail <id> --reason R",
|
|
45
54
|
" hive-tasks retry <id> [--stage KEY] failed back to pending",
|
|
@@ -48,6 +57,14 @@ const USAGE = [
|
|
|
48
57
|
" resets to KEY (default the first stage)",
|
|
49
58
|
" hive-tasks rm <id> [--force] [--json] delete a card; a claimed card needs --force",
|
|
50
59
|
"",
|
|
60
|
+
"CARD RULES",
|
|
61
|
+
" add requires --acceptance unless --draft is passed; done --verify runs the command.",
|
|
62
|
+
" edit refuses an empty --acceptance; pass --acceptance - to clear it on a draft card.",
|
|
63
|
+
" --draft stores draft: true: the card stays on the board but is never ready, next or",
|
|
64
|
+
" claimable. Draft is a flag, not a state: pending/claimed/done/failed are unchanged.",
|
|
65
|
+
" superseded is a flag too: edit --superseded/--no-superseded on a pending or failed",
|
|
66
|
+
" task, or supersede in any state. A superseded task is never ready or claimable.",
|
|
67
|
+
"",
|
|
51
68
|
"ENVIRONMENT",
|
|
52
69
|
" HIVE_AGENT=name identity used for claims",
|
|
53
70
|
].join("\n");
|
|
@@ -64,16 +81,25 @@ function value(argv, index, flag) {
|
|
|
64
81
|
}
|
|
65
82
|
|
|
66
83
|
function parse(argv) {
|
|
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:
|
|
84
|
+
const out = { args: [], scopes: [], scopeAdd: [], scopeRemove: [], after: [], afterSet: null, tags: [], stageArgs: [], into: [], json: false, keep: false, reason: null, agent: null, ttl: null, verdict: null, sha: null, state: null, role: null, limit: null, title: null, goal: null, priority: null, force: false, acceptance: null, verify: false, file: null, stagesFile: null, reviewOf: null, replacedBy: null, supersedeFlag: null, draft: false };
|
|
68
85
|
for (let i = 0; i < argv.length; i++) {
|
|
69
86
|
const arg = argv[i];
|
|
70
87
|
if (arg === "--json") out.json = true;
|
|
71
88
|
else if (arg === "--force") out.force = true;
|
|
72
89
|
else if (arg === "--scope") out.scopes.push(value(argv, i++, arg));
|
|
90
|
+
else if (arg === "--scope-add") out.scopeAdd.push(value(argv, i++, arg));
|
|
91
|
+
else if (arg === "--scope-remove") out.scopeRemove.push(value(argv, i++, arg));
|
|
73
92
|
else if (arg === "--after") out.after.push(value(argv, i++, arg));
|
|
93
|
+
else if (arg === "--after-set") out.afterSet = value(argv, i++, arg);
|
|
94
|
+
else if (arg === "--into") out.into.push(value(argv, i++, arg));
|
|
95
|
+
else if (arg === "--keep") out.keep = true;
|
|
96
|
+
else if (arg === "--replaced-by") out.replacedBy = value(argv, i++, arg);
|
|
97
|
+
else if (arg === "--superseded") out.supersedeFlag = true;
|
|
98
|
+
else if (arg === "--no-superseded") out.supersedeFlag = false;
|
|
74
99
|
else if (arg === "--review-of") out.reviewOf = value(argv, i++, arg);
|
|
75
100
|
else if (arg === "--tag") out.tags.push(value(argv, i++, arg));
|
|
76
101
|
else if (arg === "--acceptance") out.acceptance = value(argv, i++, arg);
|
|
102
|
+
else if (arg === "--draft") out.draft = true;
|
|
77
103
|
else if (arg === "--verify") out.verify = true;
|
|
78
104
|
else if (arg === "--file") out.file = value(argv, i++, arg);
|
|
79
105
|
else if (arg === "--stages") out.stagesFile = value(argv, i++, arg);
|
|
@@ -114,6 +140,17 @@ function reviewFailureReason(reason) {
|
|
|
114
140
|
return detail ? "test failed: " + detail : "test failed";
|
|
115
141
|
}
|
|
116
142
|
|
|
143
|
+
/** One message for both the preview and the CAS: a draft is never dispatchable. */
|
|
144
|
+
function draftClaimMessage(id) {
|
|
145
|
+
return "task " + id + " is a draft and cannot be claimed (rm it and re-add with --acceptance)";
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** A superseded task keeps its lane but is never dispatched again. */
|
|
149
|
+
function supersededClaimMessage(task) {
|
|
150
|
+
const replaced = task.replacedBy ? " (replaced by " + task.replacedBy + ")" : "";
|
|
151
|
+
return "task " + task.id + " is superseded" + replaced + " and cannot be claimed";
|
|
152
|
+
}
|
|
153
|
+
|
|
117
154
|
function board(repo) {
|
|
118
155
|
return listRaw(repo).map((entry) => entry.value);
|
|
119
156
|
}
|
|
@@ -122,6 +159,193 @@ function byId(tasks) {
|
|
|
122
159
|
return new Map(tasks.map((task) => [task.id, task]));
|
|
123
160
|
}
|
|
124
161
|
|
|
162
|
+
/** Parse --after-set: a comma list of dependency ids; an empty value clears it. */
|
|
163
|
+
function parseAfterSet(text) {
|
|
164
|
+
const ids = [];
|
|
165
|
+
for (const part of String(text).split(",")) {
|
|
166
|
+
const id = part.trim();
|
|
167
|
+
if (id !== "" && !ids.includes(id)) ids.push(id);
|
|
168
|
+
}
|
|
169
|
+
return ids;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** True when following `afterIds` from the edited task can reach the task itself. */
|
|
173
|
+
function dependencyCycle(map, taskId, afterIds) {
|
|
174
|
+
const seen = new Set();
|
|
175
|
+
const stack = [...afterIds];
|
|
176
|
+
while (stack.length > 0) {
|
|
177
|
+
const id = stack.pop();
|
|
178
|
+
if (id === taskId) return true;
|
|
179
|
+
if (seen.has(id)) continue;
|
|
180
|
+
seen.add(id);
|
|
181
|
+
const task = map.get(id);
|
|
182
|
+
if (!task) continue;
|
|
183
|
+
stack.push(...(task.after || []));
|
|
184
|
+
}
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Compute the patch and the changed field names for `edit`. Only fields the
|
|
190
|
+
* caller passed can change; every validation throws and the caller turns the
|
|
191
|
+
* message into exit 2 inside the CAS. Values are never stored in the event.
|
|
192
|
+
*/
|
|
193
|
+
function planEdit(task, options, map) {
|
|
194
|
+
const patch = {};
|
|
195
|
+
const fields = [];
|
|
196
|
+
|
|
197
|
+
if (options.title !== null) {
|
|
198
|
+
const title = String(options.title).trim();
|
|
199
|
+
if (title === "") throw new Error("edit: --title must not be empty");
|
|
200
|
+
if (title !== task.title) { patch.title = title; fields.push("title"); }
|
|
201
|
+
}
|
|
202
|
+
if (options.role !== null) {
|
|
203
|
+
const role = String(options.role).trim();
|
|
204
|
+
if (role === "") throw new Error("edit: --role must not be empty");
|
|
205
|
+
if (role !== task.role) { patch.role = role; fields.push("role"); }
|
|
206
|
+
}
|
|
207
|
+
if (options.priority !== null) {
|
|
208
|
+
if (!Number.isInteger(options.priority)) throw new Error("edit: --priority must be an integer");
|
|
209
|
+
if (options.priority !== task.priority) { patch.priority = options.priority; fields.push("priority"); }
|
|
210
|
+
}
|
|
211
|
+
if (options.acceptance !== null) {
|
|
212
|
+
const text = String(options.acceptance).trim();
|
|
213
|
+
if (text === "") throw new Error("edit: --acceptance must not be empty (use --acceptance - to clear it)");
|
|
214
|
+
if (text === "-") {
|
|
215
|
+
if (!isDraft(task)) throw new Error("edit: only a draft card can clear its acceptance (--acceptance -)");
|
|
216
|
+
if (task.acceptance !== "") { patch.acceptance = ""; fields.push("acceptance"); }
|
|
217
|
+
} else if (text !== task.acceptance) {
|
|
218
|
+
patch.acceptance = text;
|
|
219
|
+
fields.push("acceptance");
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
if (options.scopeAdd.length > 0 || options.scopeRemove.length > 0) {
|
|
223
|
+
const add = [];
|
|
224
|
+
for (const raw of options.scopeAdd) {
|
|
225
|
+
const path = String(raw).trim();
|
|
226
|
+
if (path === "") throw new Error("edit: --scope-add needs a non-empty path");
|
|
227
|
+
if (!add.includes(path)) add.push(path);
|
|
228
|
+
}
|
|
229
|
+
const remove = [];
|
|
230
|
+
for (const raw of options.scopeRemove) {
|
|
231
|
+
const path = String(raw).trim();
|
|
232
|
+
if (path === "") throw new Error("edit: --scope-remove needs a non-empty path");
|
|
233
|
+
if (!remove.includes(path)) remove.push(path);
|
|
234
|
+
}
|
|
235
|
+
for (const path of add) {
|
|
236
|
+
if (remove.includes(path)) throw new Error("edit: scope path " + path + " is both added and removed");
|
|
237
|
+
}
|
|
238
|
+
const missing = remove.filter((path) => !task.scope.includes(path));
|
|
239
|
+
if (missing.length > 0) throw new Error("edit: --scope-remove: not in scope: " + missing.join(", "));
|
|
240
|
+
const scope = task.scope.filter((path) => !remove.includes(path));
|
|
241
|
+
for (const path of add) if (!scope.includes(path)) scope.push(path);
|
|
242
|
+
const same = scope.length === task.scope.length && scope.every((path, index) => path === task.scope[index]);
|
|
243
|
+
if (!same) { patch.scope = scope; fields.push("scope"); }
|
|
244
|
+
}
|
|
245
|
+
if (options.afterSet !== null) {
|
|
246
|
+
const ids = parseAfterSet(options.afterSet);
|
|
247
|
+
for (const dep of ids) {
|
|
248
|
+
if (dep === task.id) throw new Error("edit: a card cannot depend on itself");
|
|
249
|
+
if (!map.has(dep)) throw new Error("edit: --after-set: no such task: " + dep);
|
|
250
|
+
}
|
|
251
|
+
if (task.reviewOf && !ids.includes(task.reviewOf)) {
|
|
252
|
+
throw new Error("edit: --after-set must keep the review-of card " + task.reviewOf);
|
|
253
|
+
}
|
|
254
|
+
const same = ids.length === task.after.length
|
|
255
|
+
&& [...ids].sort().join("\u0000") === [...task.after].sort().join("\u0000");
|
|
256
|
+
if (!same) {
|
|
257
|
+
if (dependencyCycle(map, task.id, ids)) {
|
|
258
|
+
throw new Error("edit: --after-set would create a dependency cycle through " + task.id);
|
|
259
|
+
}
|
|
260
|
+
patch.after = ids;
|
|
261
|
+
fields.push("after");
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
if (options.tags.length > 0) {
|
|
265
|
+
const add = [];
|
|
266
|
+
const remove = [];
|
|
267
|
+
for (const raw of options.tags) {
|
|
268
|
+
const text = String(raw).trim();
|
|
269
|
+
if (text.length < 2 || (text[0] !== "+" && text[0] !== "-")) {
|
|
270
|
+
throw new Error("edit: --tag must look like +name or -name");
|
|
271
|
+
}
|
|
272
|
+
const name = text.slice(1).trim();
|
|
273
|
+
if (name === "") throw new Error("edit: --tag must look like +name or -name");
|
|
274
|
+
if (text[0] === "+") { if (!add.includes(name)) add.push(name); }
|
|
275
|
+
else if (!remove.includes(name)) remove.push(name);
|
|
276
|
+
}
|
|
277
|
+
for (const name of add) {
|
|
278
|
+
if (remove.includes(name)) throw new Error("edit: tag " + name + " is both added and removed");
|
|
279
|
+
}
|
|
280
|
+
const tags = task.tags.filter((tag) => !remove.includes(tag));
|
|
281
|
+
for (const name of add) if (!tags.includes(name)) tags.push(name);
|
|
282
|
+
const same = tags.length === task.tags.length
|
|
283
|
+
&& [...tags].sort().join("\u0000") === [...task.tags].sort().join("\u0000");
|
|
284
|
+
if (!same) { patch.tags = tags; fields.push("tags"); }
|
|
285
|
+
}
|
|
286
|
+
const clear = [];
|
|
287
|
+
if (options.supersedeFlag !== null) {
|
|
288
|
+
if (options.supersedeFlag === true) {
|
|
289
|
+
if (task.superseded !== true) {
|
|
290
|
+
patch.superseded = true;
|
|
291
|
+
patch.supersededReason = options.reason !== null ? String(options.reason).trim() : "superseded by edit";
|
|
292
|
+
fields.push("superseded");
|
|
293
|
+
}
|
|
294
|
+
} else if (task.superseded === true) {
|
|
295
|
+
clear.push("superseded", "supersededReason", "replacedBy");
|
|
296
|
+
fields.push("superseded");
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
return { patch, fields, clear };
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** A JSON array of strings, per split entry field. */
|
|
303
|
+
function splitStringList(value, where) {
|
|
304
|
+
if (!Array.isArray(value)) throw new Error(where + " must be a JSON array of strings");
|
|
305
|
+
const list = [];
|
|
306
|
+
for (const entry of value) {
|
|
307
|
+
if (typeof entry !== "string" || entry.trim() === "") throw new Error(where + " must contain non-empty strings");
|
|
308
|
+
if (!list.includes(entry.trim())) list.push(entry.trim());
|
|
309
|
+
}
|
|
310
|
+
return list;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** One --into entry: { title, role?, scope?, acceptance?, after? }. */
|
|
314
|
+
function splitItem(entry, index) {
|
|
315
|
+
const where = "split: --into[" + index + "]";
|
|
316
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry)) throw new Error(where + " must be an object");
|
|
317
|
+
const title = typeof entry.title === "string" ? entry.title.trim() : "";
|
|
318
|
+
if (title === "") throw new Error(where + " needs a non-empty title");
|
|
319
|
+
const item = { title };
|
|
320
|
+
if (entry.role !== undefined) {
|
|
321
|
+
if (typeof entry.role !== "string" || entry.role.trim() === "") throw new Error(where + ".role must be a non-empty string");
|
|
322
|
+
item.role = entry.role.trim();
|
|
323
|
+
}
|
|
324
|
+
if (entry.scope !== undefined) item.scope = splitStringList(entry.scope, where + ".scope");
|
|
325
|
+
if (entry.acceptance !== undefined) {
|
|
326
|
+
if (typeof entry.acceptance !== "string" || entry.acceptance.trim() === "") throw new Error(where + ".acceptance must be a non-empty string");
|
|
327
|
+
item.acceptance = entry.acceptance.trim();
|
|
328
|
+
}
|
|
329
|
+
if (entry.after !== undefined) item.after = splitStringList(entry.after, where + ".after");
|
|
330
|
+
return item;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** --into takes one JSON array with at least two entries. */
|
|
334
|
+
function splitItems(values) {
|
|
335
|
+
const raw = values.map((value) => String(value).trim()).filter((value) => value !== "");
|
|
336
|
+
const text = raw.length === 1 ? raw[0] : raw.join(" ");
|
|
337
|
+
if (text === "") throw new Error("split needs --into '<json array>'");
|
|
338
|
+
if (!text.startsWith("[")) throw new Error("split: --into must be a JSON array");
|
|
339
|
+
let parsed;
|
|
340
|
+
try {
|
|
341
|
+
parsed = JSON.parse(text);
|
|
342
|
+
} catch {
|
|
343
|
+
throw new Error("split: --into is not valid JSON");
|
|
344
|
+
}
|
|
345
|
+
if (!Array.isArray(parsed) || parsed.length < 2) throw new Error("split: --into JSON array needs at least two entries");
|
|
346
|
+
return parsed.map((entry, index) => splitItem(entry, index));
|
|
347
|
+
}
|
|
348
|
+
|
|
125
349
|
/** The target key of a retry/reopen --stage flag (the last one wins). */
|
|
126
350
|
function stageTarget(options) {
|
|
127
351
|
return options.stageArgs.length ? options.stageArgs[options.stageArgs.length - 1] : null;
|
|
@@ -135,9 +359,16 @@ function readyList(repo, tasks, { role } = {}) {
|
|
|
135
359
|
.sort(order);
|
|
136
360
|
}
|
|
137
361
|
|
|
138
|
-
/**
|
|
362
|
+
/**
|
|
363
|
+
* JSON/print view of a card: stages, stage and stageState are always present.
|
|
364
|
+
* Cards written before 1.8.1 have no stateChangedAt; they read as updatedAt.
|
|
365
|
+
*/
|
|
139
366
|
function viewTask(task) {
|
|
140
|
-
return {
|
|
367
|
+
return {
|
|
368
|
+
...task,
|
|
369
|
+
...stageView(task),
|
|
370
|
+
stateChangedAt: task.stateChangedAt || task.updatedAt,
|
|
371
|
+
};
|
|
141
372
|
}
|
|
142
373
|
|
|
143
374
|
function printTask(task, json, extra = {}, extraLines = []) {
|
|
@@ -154,6 +385,13 @@ function printTask(task, json, extra = {}, extraLines = []) {
|
|
|
154
385
|
process.stdout.write(indent + "chain: " + task.stages.map((entry) => entry.key + ":" + entry.state).join(" -> ") + "\n");
|
|
155
386
|
}
|
|
156
387
|
}
|
|
388
|
+
if (task.draft) process.stdout.write(indent + "draft: true (never ready, not claimable)\n");
|
|
389
|
+
if (task.superseded) {
|
|
390
|
+
const parts = ["superseded: true"];
|
|
391
|
+
if (task.supersededReason) parts.push("reason: " + task.supersededReason);
|
|
392
|
+
if (task.replacedBy) parts.push("replaced by: " + task.replacedBy);
|
|
393
|
+
process.stdout.write(indent + parts.join(" ") + "\n");
|
|
394
|
+
}
|
|
157
395
|
if (task.scope.length) process.stdout.write(indent + "scope: " + task.scope.join(", ") + "\n");
|
|
158
396
|
if (task.after.length) process.stdout.write(indent + "after: " + task.after.join(", ") + "\n");
|
|
159
397
|
if (task.reviewOf) process.stdout.write(indent + "review-of: " + task.reviewOf + "\n");
|
|
@@ -213,8 +451,13 @@ function run(argv) {
|
|
|
213
451
|
if (!Array.isArray(plan) || plan.length === 0) fail("plan must be a non-empty JSON array", 2);
|
|
214
452
|
|
|
215
453
|
const keys = new Map();
|
|
216
|
-
const drafts = plan.map((entry) => {
|
|
217
|
-
|
|
454
|
+
const drafts = plan.map((entry, index) => {
|
|
455
|
+
const where = "plan[" + index + "]";
|
|
456
|
+
if (!entry || !entry.title) fail(where + " needs a title", 2);
|
|
457
|
+
const isDraftEntry = entry.draft === true;
|
|
458
|
+
if (!entry.acceptance && !isDraftEntry) {
|
|
459
|
+
fail(where + " (\"" + entry.title + "\"): acceptance is required (or set \"draft\": true)", 2);
|
|
460
|
+
}
|
|
218
461
|
let task;
|
|
219
462
|
try {
|
|
220
463
|
task = create({
|
|
@@ -226,6 +469,7 @@ function run(argv) {
|
|
|
226
469
|
priority: entry.priority,
|
|
227
470
|
tags: entry.tags || [],
|
|
228
471
|
acceptance: entry.acceptance || "",
|
|
472
|
+
draft: isDraftEntry,
|
|
229
473
|
createdBy: options.agent || process.env.HIVE_AGENT || "planner",
|
|
230
474
|
stages: entry.stages,
|
|
231
475
|
});
|
|
@@ -352,8 +596,185 @@ function run(argv) {
|
|
|
352
596
|
return process.stdout.write(id + " deleted [" + deleted.state + "] " + deleted.title + "\n");
|
|
353
597
|
}
|
|
354
598
|
|
|
599
|
+
if (command === "edit") {
|
|
600
|
+
const id = options.args[0];
|
|
601
|
+
if (!id) fail("edit needs a task id", 2);
|
|
602
|
+
const actor = options.agent || process.env.HIVE_AGENT || "unknown";
|
|
603
|
+
if (!readTask(repo, id)) fail("no such task: " + id, 2);
|
|
604
|
+
const hasEdit = options.title !== null
|
|
605
|
+
|| options.role !== null
|
|
606
|
+
|| options.priority !== null
|
|
607
|
+
|| options.acceptance !== null
|
|
608
|
+
|| options.scopeAdd.length > 0
|
|
609
|
+
|| options.scopeRemove.length > 0
|
|
610
|
+
|| options.afterSet !== null
|
|
611
|
+
|| options.tags.length > 0
|
|
612
|
+
|| options.supersedeFlag !== null;
|
|
613
|
+
if (!hasEdit) {
|
|
614
|
+
fail("edit needs at least one field: --title, --scope-add, --scope-remove, --acceptance, --priority, --after-set, --tag, --role, --superseded", 2);
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
// Metadata only: the lane (state/stage/stageState) never changes here, so
|
|
618
|
+
// stateChangedAt is left alone and only updatedAt moves.
|
|
619
|
+
let previous = null;
|
|
620
|
+
let updated = null;
|
|
621
|
+
let fields = [];
|
|
622
|
+
try {
|
|
623
|
+
const result = mutate(repo, (tasks) => {
|
|
624
|
+
const map = byId(tasks);
|
|
625
|
+
const task = map.get(id);
|
|
626
|
+
if (!task) throw new Error("no such task: " + id);
|
|
627
|
+
if (task.state !== "pending" && task.state !== "failed") {
|
|
628
|
+
const hint = task.state === "claimed" ? "; done or fail it first" : task.state === "done" ? "; reopen it first" : "";
|
|
629
|
+
throw new Error("edit: task " + id + " is " + task.state + hint + " (edit only works on pending or failed tasks)");
|
|
630
|
+
}
|
|
631
|
+
const plan = planEdit(task, options, map);
|
|
632
|
+
if (plan.fields.length === 0) { updated = task; fields = []; return null; }
|
|
633
|
+
previous = task;
|
|
634
|
+
fields = plan.fields;
|
|
635
|
+
updated = { ...task, ...plan.patch, updatedAt: now() };
|
|
636
|
+
for (const key of plan.clear) delete updated[key];
|
|
637
|
+
return tasks.map((entry) => (entry.id === id ? updated : entry));
|
|
638
|
+
});
|
|
639
|
+
if (!result.ok) fail("could not edit " + id + " (too much contention)");
|
|
640
|
+
} catch (error) {
|
|
641
|
+
fail(error.message, 2);
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
// Keep the announcement board in sync with the task scope: added paths are
|
|
645
|
+
// announced under the task announcer (or the editor) and removed paths a
|
|
646
|
+
// claim of this task wrote are released.
|
|
647
|
+
if (fields.includes("scope")) {
|
|
648
|
+
const added = updated.scope.filter((path) => !previous.scope.includes(path));
|
|
649
|
+
const removed = previous.scope.filter((path) => !updated.scope.includes(path));
|
|
650
|
+
const announced = listLeases(repo).filter((entry) => (entry.value.task || "") === id);
|
|
651
|
+
const ttlMs = parseTtl(options.ttl);
|
|
652
|
+
const announcer = announced.length > 0 ? announced[0].value.agent : actor;
|
|
653
|
+
if (added.length > 0) claimLeases(repo, { paths: added, agent: announcer, ttlMs, taskId: id });
|
|
654
|
+
if (removed.length > 0) {
|
|
655
|
+
for (const agent of new Set(announced.map((entry) => entry.value.agent))) {
|
|
656
|
+
releaseLeases(repo, { paths: removed, agent });
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
if (fields.length > 0) {
|
|
661
|
+
const changed = {};
|
|
662
|
+
for (const name of fields) changed[name] = true;
|
|
663
|
+
append(repo, { actor, kind: "task_edit", task: id, fields: changed });
|
|
664
|
+
}
|
|
665
|
+
return printTask(updated, options.json);
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
if (command === "split") {
|
|
669
|
+
const id = options.args[0];
|
|
670
|
+
if (!id) fail("split needs a task id", 2);
|
|
671
|
+
const actor = options.agent || process.env.HIVE_AGENT || "unknown";
|
|
672
|
+
if (!readTask(repo, id)) fail("no such task: " + id, 2);
|
|
673
|
+
if (options.into.length === 0) fail("split needs --into '<json array>'", 2);
|
|
674
|
+
|
|
675
|
+
let parent = null;
|
|
676
|
+
let cards = [];
|
|
677
|
+
try {
|
|
678
|
+
const result = mutate(repo, (tasks) => {
|
|
679
|
+
const map = byId(tasks);
|
|
680
|
+
const task = map.get(id);
|
|
681
|
+
if (!task) throw new Error("no such task: " + id);
|
|
682
|
+
if (task.state !== "pending" && task.state !== "failed") {
|
|
683
|
+
const hint = task.state === "claimed" ? "; done or fail it first" : task.state === "done" ? "; reopen it first" : "";
|
|
684
|
+
throw new Error("split: task " + id + " is " + task.state + hint + " (split only works on pending or failed tasks)");
|
|
685
|
+
}
|
|
686
|
+
if (isSuperseded(task)) throw new Error("split: task " + id + " is already superseded");
|
|
687
|
+
const items = splitItems(options.into);
|
|
688
|
+
const parentDeps = Array.isArray(task.after) ? task.after.filter((dep) => dep !== id) : [];
|
|
689
|
+
cards = items.map((item, index) => {
|
|
690
|
+
const acceptance = item.acceptance !== undefined ? item.acceptance : task.acceptance;
|
|
691
|
+
if (!String(acceptance || "").trim()) {
|
|
692
|
+
throw new Error("split: --into[" + index + "] needs an acceptance command (the parent has none to inherit)");
|
|
693
|
+
}
|
|
694
|
+
const after = item.after !== undefined ? [...item.after] : [...parentDeps];
|
|
695
|
+
for (const dep of after) {
|
|
696
|
+
if (dep === id) throw new Error("split: --into[" + index + "].after cannot depend on the task being split");
|
|
697
|
+
if (!map.has(dep)) throw new Error("split: --into[" + index + "].after: no such task: " + dep);
|
|
698
|
+
}
|
|
699
|
+
const childAfter = options.keep && !after.includes(id) ? [...after, id] : after;
|
|
700
|
+
return create({
|
|
701
|
+
title: item.title,
|
|
702
|
+
role: item.role !== undefined ? item.role : task.role,
|
|
703
|
+
scope: item.scope !== undefined ? [...item.scope] : [...(task.scope || [])],
|
|
704
|
+
after: childAfter,
|
|
705
|
+
priority: task.priority,
|
|
706
|
+
tags: [...(task.tags || [])],
|
|
707
|
+
acceptance,
|
|
708
|
+
createdBy: actor,
|
|
709
|
+
});
|
|
710
|
+
});
|
|
711
|
+
if (options.keep) {
|
|
712
|
+
parent = task;
|
|
713
|
+
return [...tasks, ...cards];
|
|
714
|
+
}
|
|
715
|
+
const reason = options.reason !== null ? String(options.reason).trim() : "split into " + cards.length;
|
|
716
|
+
parent = { ...task, superseded: true, supersededReason: reason, replacedBy: cards[0].id, updatedAt: now() };
|
|
717
|
+
return [...tasks.map((entry) => (entry.id === id ? parent : entry)), ...cards];
|
|
718
|
+
});
|
|
719
|
+
if (!result.ok) fail("could not split " + id + " (too much contention)");
|
|
720
|
+
} catch (error) {
|
|
721
|
+
fail(error.message, 2);
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
const event = { actor, kind: "task_split", task: id, cards: cards.map((card) => card.id), kept: Boolean(options.keep) };
|
|
725
|
+
if (!options.keep) event.replacedBy = cards[0].id;
|
|
726
|
+
append(repo, event);
|
|
727
|
+
if (options.json) {
|
|
728
|
+
const payload = { task: viewTask(parent), cards: cards.map((card) => viewTask(card)) };
|
|
729
|
+
return process.stdout.write(JSON.stringify(payload, null, 2) + "\n");
|
|
730
|
+
}
|
|
731
|
+
printTask(parent, false);
|
|
732
|
+
for (const card of cards) process.stdout.write(" " + card.id + " " + card.title + "\n");
|
|
733
|
+
return;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
if (command === "supersede") {
|
|
737
|
+
const id = options.args[0];
|
|
738
|
+
if (!id) fail("supersede needs a task id", 2);
|
|
739
|
+
const reason = options.reason === null ? "" : String(options.reason).trim();
|
|
740
|
+
if (reason === "") fail("supersede needs --reason R", 2);
|
|
741
|
+
const actor = options.agent || process.env.HIVE_AGENT || "unknown";
|
|
742
|
+
if (!readTask(repo, id)) fail("no such task: " + id, 2);
|
|
743
|
+
let updated = null;
|
|
744
|
+
try {
|
|
745
|
+
const result = mutate(repo, (tasks) => {
|
|
746
|
+
const map = byId(tasks);
|
|
747
|
+
const task = map.get(id);
|
|
748
|
+
if (!task) throw new Error("no such task: " + id);
|
|
749
|
+
if (isSuperseded(task)) throw new Error("task " + id + " is already superseded");
|
|
750
|
+
const patch = { superseded: true, supersededReason: reason, updatedAt: now() };
|
|
751
|
+
if (options.replacedBy !== null) {
|
|
752
|
+
const replacement = String(options.replacedBy).trim();
|
|
753
|
+
if (replacement === "") throw new Error("supersede: --replaced-by must not be empty");
|
|
754
|
+
if (replacement === id) throw new Error("supersede: a task cannot replace itself");
|
|
755
|
+
if (!map.has(replacement)) throw new Error("supersede: --replaced-by: no such task: " + replacement);
|
|
756
|
+
patch.replacedBy = replacement;
|
|
757
|
+
}
|
|
758
|
+
updated = { ...task, ...patch };
|
|
759
|
+
return tasks.map((entry) => (entry.id === id ? updated : entry));
|
|
760
|
+
});
|
|
761
|
+
if (!result.ok) fail("could not supersede " + id + " (too much contention)");
|
|
762
|
+
} catch (error) {
|
|
763
|
+
fail(error.message, 2);
|
|
764
|
+
}
|
|
765
|
+
const event = { actor, kind: "task_supersede", task: id, reason };
|
|
766
|
+
if (updated.replacedBy) event.replacedBy = updated.replacedBy;
|
|
767
|
+
append(repo, event);
|
|
768
|
+
return printTask(updated, options.json);
|
|
769
|
+
}
|
|
770
|
+
|
|
355
771
|
if (command === "add") {
|
|
356
772
|
if (!options.title) fail("--title is required", 2);
|
|
773
|
+
// Mechanical enforcement of the acceptance rule: a card that can be dispatched
|
|
774
|
+
// must say how it will be judged, unless it is deliberately parked as a draft.
|
|
775
|
+
if (!options.acceptance && !options.draft) {
|
|
776
|
+
fail("add: --acceptance is required (or pass --draft for a draft card)", 2);
|
|
777
|
+
}
|
|
357
778
|
if (options.reviewOf && !readTask(repo, options.reviewOf)) fail("no such task: " + options.reviewOf, 2);
|
|
358
779
|
if (options.stageArgs.length && options.stagesFile) fail("use --stage or --stages, not both", 2);
|
|
359
780
|
let stages = null;
|
|
@@ -375,6 +796,7 @@ function run(argv) {
|
|
|
375
796
|
tags: options.tags,
|
|
376
797
|
acceptance: options.acceptance || "",
|
|
377
798
|
reviewOf: options.reviewOf,
|
|
799
|
+
draft: options.draft,
|
|
378
800
|
createdBy: options.agent || process.env.HIVE_AGENT || "unknown",
|
|
379
801
|
stages,
|
|
380
802
|
});
|
|
@@ -404,7 +826,10 @@ function run(argv) {
|
|
|
404
826
|
return process.stdout.write(JSON.stringify(rows, null, 2) + "\n");
|
|
405
827
|
}
|
|
406
828
|
if (limited.length === 0) return process.stdout.write(command === "ready" ? "Nothing ready.\n" : "No tasks.\n");
|
|
407
|
-
for (const task of limited)
|
|
829
|
+
for (const task of limited) {
|
|
830
|
+
const tags = (task.draft ? " [draft]" : "") + (task.superseded ? " [superseded]" : "");
|
|
831
|
+
process.stdout.write(task.id.padEnd(34) + task.state.padEnd(9) + stageRole(task).padEnd(11) + task.title + tags + "\n");
|
|
832
|
+
}
|
|
408
833
|
return;
|
|
409
834
|
}
|
|
410
835
|
|
|
@@ -438,6 +863,8 @@ function run(argv) {
|
|
|
438
863
|
|
|
439
864
|
const preview = readTask(repo, id);
|
|
440
865
|
if (!preview) fail("no such task: " + id, 2);
|
|
866
|
+
if (isDraft(preview)) fail(draftClaimMessage(id), 2);
|
|
867
|
+
if (isSuperseded(preview)) fail(supersededClaimMessage(preview), 2);
|
|
441
868
|
if (readMode(repo) === "freeze" && !options.force && !(preview.tags || []).includes("fix")) {
|
|
442
869
|
fail("mode is freeze: only fix-tagged cards may be claimed (use --force to override)", 2);
|
|
443
870
|
}
|
|
@@ -454,6 +881,8 @@ function run(argv) {
|
|
|
454
881
|
const map = byId(tasks);
|
|
455
882
|
const task = map.get(id);
|
|
456
883
|
if (!task) throw new Error("no such task: " + id);
|
|
884
|
+
if (isDraft(task)) throw new Error(draftClaimMessage(id));
|
|
885
|
+
if (isSuperseded(task)) throw new Error(supersededClaimMessage(task));
|
|
457
886
|
const canReclaim = isReclaimable(task);
|
|
458
887
|
if (task.state !== "pending" && !canReclaim) throw new Error("task " + id + " is " + task.state + stageOf(task));
|
|
459
888
|
if (isStaged(task) && task.stageState !== "pending" && !(task.stageState === "claimed" && canReclaim)) {
|
|
@@ -625,16 +1054,21 @@ function run(argv) {
|
|
|
625
1054
|
const items = board(repo);
|
|
626
1055
|
const byState = {};
|
|
627
1056
|
const byRole = {};
|
|
1057
|
+
let superseded = 0;
|
|
628
1058
|
let oldest = null;
|
|
629
1059
|
for (const task of items) {
|
|
1060
|
+
// Superseded tasks keep their lane but are counted separately, never as a
|
|
1061
|
+
// state or role of the live board.
|
|
1062
|
+
if (isSuperseded(task)) { superseded += 1; continue; }
|
|
630
1063
|
byState[task.state] = (byState[task.state] || 0) + 1;
|
|
631
1064
|
byRole[task.role] = (byRole[task.role] || 0) + 1;
|
|
632
1065
|
if (task.state === "pending" && (!oldest || task.createdAt < oldest.createdAt)) oldest = task;
|
|
633
1066
|
}
|
|
634
|
-
const summary = { total: items.length, byState, byRole, oldestPending: oldest ? { id: oldest.id, createdAt: oldest.createdAt } : null };
|
|
1067
|
+
const summary = { total: items.length, byState, byRole, superseded, oldestPending: oldest ? { id: oldest.id, createdAt: oldest.createdAt } : null };
|
|
635
1068
|
if (options.json) return process.stdout.write(JSON.stringify(summary, null, 2) + "\n");
|
|
636
1069
|
process.stdout.write("cards: " + summary.total + "\n");
|
|
637
1070
|
for (const [state, count] of Object.entries(byState)) process.stdout.write(" " + state.padEnd(8) + count + "\n");
|
|
1071
|
+
if (superseded > 0) process.stdout.write(" " + "superseded".padEnd(8) + superseded + "\n");
|
|
638
1072
|
if (oldest) process.stdout.write("oldest pending: " + oldest.id + " (" + oldest.createdAt + ")\n");
|
|
639
1073
|
return;
|
|
640
1074
|
}
|
package/src/lease.js
CHANGED
|
@@ -128,6 +128,18 @@ export function claimLeases(repo, { paths, agent, ttlMs, taskId }) {
|
|
|
128
128
|
return { granted: true, keys, overlaps };
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
+
/** Every announcement currently on the board: [{ key, value }]. */
|
|
132
|
+
export function listLeases(repo) {
|
|
133
|
+
const raw = git(["for-each-ref", "refs/hive/v1/lease", "--format=%(refname)"], { cwd: repo.top }).stdout.trim();
|
|
134
|
+
if (!raw) return [];
|
|
135
|
+
const prefix = NS + "/lease/";
|
|
136
|
+
return raw
|
|
137
|
+
.split("\n")
|
|
138
|
+
.filter((ref) => ref.endsWith("/" + KEY_LEAF))
|
|
139
|
+
.map((ref) => ({ key: ref.slice(prefix.length, -(KEY_LEAF.length + 1)), value: readBlob(repo, ref) }))
|
|
140
|
+
.filter((entry) => entry.value);
|
|
141
|
+
}
|
|
142
|
+
|
|
131
143
|
export function releaseLeases(repo, { paths, agent }) {
|
|
132
144
|
const released = [];
|
|
133
145
|
for (const key of paths.map(toKey)) {
|