hive-tasks 1.6.0 → 1.8.1
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 +94 -0
- package/README.md +140 -70
- package/README.zh-CN.md +133 -74
- package/SPEC.md +208 -126
- package/package.json +1 -1
- package/src/cli.js +61 -26
- package/src/lease.js +76 -23
- package/src/task.js +27 -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, order, reopenPatch, transition } from "./task.js";
|
|
8
8
|
import {
|
|
9
9
|
advanceStage,
|
|
10
10
|
currentStage,
|
|
@@ -30,16 +30,17 @@ 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
36
|
" hive-tasks list [--json] [--state S] [--role R]",
|
|
37
37
|
" hive-tasks show <id> [--json] card plus the resolved role source",
|
|
38
38
|
" hive-tasks ready [--json] [--limit N] [--role R]",
|
|
39
39
|
" hive-tasks next [--role R] [--json] the single best candidate",
|
|
40
|
-
" hive-tasks claim <id> --agent A [--ttl 5m] card CAS, then
|
|
40
|
+
" hive-tasks claim <id> --agent A [--ttl 5m] card CAS, then announce scope paths",
|
|
41
41
|
" hive-tasks renew <id> [--ttl 5m] extend a claim",
|
|
42
|
-
" hive-tasks plan --file plan.json create a whole DAG in one transaction",
|
|
42
|
+
" hive-tasks plan --file plan.json create a whole DAG in one transaction;",
|
|
43
|
+
" every entry needs acceptance unless draft: true",
|
|
43
44
|
" hive-tasks done <id> [--sha S] [--verdict pass|fail] [--verify]",
|
|
44
45
|
" hive-tasks fail <id> --reason R",
|
|
45
46
|
" hive-tasks retry <id> [--stage KEY] failed back to pending",
|
|
@@ -48,6 +49,11 @@ const USAGE = [
|
|
|
48
49
|
" resets to KEY (default the first stage)",
|
|
49
50
|
" hive-tasks rm <id> [--force] [--json] delete a card; a claimed card needs --force",
|
|
50
51
|
"",
|
|
52
|
+
"CARD RULES",
|
|
53
|
+
" add requires --acceptance unless --draft is passed; done --verify runs the command.",
|
|
54
|
+
" --draft stores draft: true: the card stays on the board but is never ready, next or",
|
|
55
|
+
" claimable. Draft is a flag, not a state: pending/claimed/done/failed are unchanged.",
|
|
56
|
+
"",
|
|
51
57
|
"ENVIRONMENT",
|
|
52
58
|
" HIVE_AGENT=name identity used for claims",
|
|
53
59
|
].join("\n");
|
|
@@ -64,7 +70,7 @@ function value(argv, index, flag) {
|
|
|
64
70
|
}
|
|
65
71
|
|
|
66
72
|
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: 0, force: false, acceptance: null, verify: false, file: null, stagesFile: null, reviewOf: null };
|
|
73
|
+
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, draft: false };
|
|
68
74
|
for (let i = 0; i < argv.length; i++) {
|
|
69
75
|
const arg = argv[i];
|
|
70
76
|
if (arg === "--json") out.json = true;
|
|
@@ -74,6 +80,7 @@ function parse(argv) {
|
|
|
74
80
|
else if (arg === "--review-of") out.reviewOf = value(argv, i++, arg);
|
|
75
81
|
else if (arg === "--tag") out.tags.push(value(argv, i++, arg));
|
|
76
82
|
else if (arg === "--acceptance") out.acceptance = value(argv, i++, arg);
|
|
83
|
+
else if (arg === "--draft") out.draft = true;
|
|
77
84
|
else if (arg === "--verify") out.verify = true;
|
|
78
85
|
else if (arg === "--file") out.file = value(argv, i++, arg);
|
|
79
86
|
else if (arg === "--stages") out.stagesFile = value(argv, i++, arg);
|
|
@@ -114,6 +121,11 @@ function reviewFailureReason(reason) {
|
|
|
114
121
|
return detail ? "test failed: " + detail : "test failed";
|
|
115
122
|
}
|
|
116
123
|
|
|
124
|
+
/** One message for both the preview and the CAS: a draft is never dispatchable. */
|
|
125
|
+
function draftClaimMessage(id) {
|
|
126
|
+
return "task " + id + " is a draft and cannot be claimed (rm it and re-add with --acceptance)";
|
|
127
|
+
}
|
|
128
|
+
|
|
117
129
|
function board(repo) {
|
|
118
130
|
return listRaw(repo).map((entry) => entry.value);
|
|
119
131
|
}
|
|
@@ -135,9 +147,16 @@ function readyList(repo, tasks, { role } = {}) {
|
|
|
135
147
|
.sort(order);
|
|
136
148
|
}
|
|
137
149
|
|
|
138
|
-
/**
|
|
150
|
+
/**
|
|
151
|
+
* JSON/print view of a card: stages, stage and stageState are always present.
|
|
152
|
+
* Cards written before 1.8.1 have no stateChangedAt; they read as updatedAt.
|
|
153
|
+
*/
|
|
139
154
|
function viewTask(task) {
|
|
140
|
-
return {
|
|
155
|
+
return {
|
|
156
|
+
...task,
|
|
157
|
+
...stageView(task),
|
|
158
|
+
stateChangedAt: task.stateChangedAt || task.updatedAt,
|
|
159
|
+
};
|
|
141
160
|
}
|
|
142
161
|
|
|
143
162
|
function printTask(task, json, extra = {}, extraLines = []) {
|
|
@@ -154,6 +173,7 @@ function printTask(task, json, extra = {}, extraLines = []) {
|
|
|
154
173
|
process.stdout.write(indent + "chain: " + task.stages.map((entry) => entry.key + ":" + entry.state).join(" -> ") + "\n");
|
|
155
174
|
}
|
|
156
175
|
}
|
|
176
|
+
if (task.draft) process.stdout.write(indent + "draft: true (never ready, not claimable)\n");
|
|
157
177
|
if (task.scope.length) process.stdout.write(indent + "scope: " + task.scope.join(", ") + "\n");
|
|
158
178
|
if (task.after.length) process.stdout.write(indent + "after: " + task.after.join(", ") + "\n");
|
|
159
179
|
if (task.reviewOf) process.stdout.write(indent + "review-of: " + task.reviewOf + "\n");
|
|
@@ -213,8 +233,13 @@ function run(argv) {
|
|
|
213
233
|
if (!Array.isArray(plan) || plan.length === 0) fail("plan must be a non-empty JSON array", 2);
|
|
214
234
|
|
|
215
235
|
const keys = new Map();
|
|
216
|
-
const drafts = plan.map((entry) => {
|
|
217
|
-
|
|
236
|
+
const drafts = plan.map((entry, index) => {
|
|
237
|
+
const where = "plan[" + index + "]";
|
|
238
|
+
if (!entry || !entry.title) fail(where + " needs a title", 2);
|
|
239
|
+
const isDraftEntry = entry.draft === true;
|
|
240
|
+
if (!entry.acceptance && !isDraftEntry) {
|
|
241
|
+
fail(where + " (\"" + entry.title + "\"): acceptance is required (or set \"draft\": true)", 2);
|
|
242
|
+
}
|
|
218
243
|
let task;
|
|
219
244
|
try {
|
|
220
245
|
task = create({
|
|
@@ -226,6 +251,7 @@ function run(argv) {
|
|
|
226
251
|
priority: entry.priority,
|
|
227
252
|
tags: entry.tags || [],
|
|
228
253
|
acceptance: entry.acceptance || "",
|
|
254
|
+
draft: isDraftEntry,
|
|
229
255
|
createdBy: options.agent || process.env.HIVE_AGENT || "planner",
|
|
230
256
|
stages: entry.stages,
|
|
231
257
|
});
|
|
@@ -309,7 +335,7 @@ function run(argv) {
|
|
|
309
335
|
return printTask(readTask(repo, id), options.json);
|
|
310
336
|
}
|
|
311
337
|
|
|
312
|
-
// A claimed card may still hold
|
|
338
|
+
// A claimed card may still hold announcements; release exactly the claim we reset.
|
|
313
339
|
if (previous.state === "claimed") releaseLeases(repo, { paths: previous.scope, agent: previous.owner || agent });
|
|
314
340
|
append(repo, { actor: agent, kind: "reopen", task: id, from: previous.state, reason, attempt: reopened.attempt, stage: isStaged(reopened) ? reopened.stage : undefined });
|
|
315
341
|
return printTask(reopened, options.json);
|
|
@@ -344,7 +370,7 @@ function run(argv) {
|
|
|
344
370
|
fail(error.message, 2);
|
|
345
371
|
}
|
|
346
372
|
|
|
347
|
-
// --force deletes a claimed card only after releasing exactly the
|
|
373
|
+
// --force deletes a claimed card only after releasing exactly the announcements
|
|
348
374
|
// that claim holds, so a forced delete cannot strand them.
|
|
349
375
|
if (deleted.state === "claimed") releaseLeases(repo, { paths: deleted.scope, agent: deleted.owner || agent });
|
|
350
376
|
append(repo, { actor: agent, kind: "task_delete", task: id, id, state: deleted.state });
|
|
@@ -354,6 +380,11 @@ function run(argv) {
|
|
|
354
380
|
|
|
355
381
|
if (command === "add") {
|
|
356
382
|
if (!options.title) fail("--title is required", 2);
|
|
383
|
+
// Mechanical enforcement of the acceptance rule: a card that can be dispatched
|
|
384
|
+
// must say how it will be judged, unless it is deliberately parked as a draft.
|
|
385
|
+
if (!options.acceptance && !options.draft) {
|
|
386
|
+
fail("add: --acceptance is required (or pass --draft for a draft card)", 2);
|
|
387
|
+
}
|
|
357
388
|
if (options.reviewOf && !readTask(repo, options.reviewOf)) fail("no such task: " + options.reviewOf, 2);
|
|
358
389
|
if (options.stageArgs.length && options.stagesFile) fail("use --stage or --stages, not both", 2);
|
|
359
390
|
let stages = null;
|
|
@@ -375,6 +406,7 @@ function run(argv) {
|
|
|
375
406
|
tags: options.tags,
|
|
376
407
|
acceptance: options.acceptance || "",
|
|
377
408
|
reviewOf: options.reviewOf,
|
|
409
|
+
draft: options.draft,
|
|
378
410
|
createdBy: options.agent || process.env.HIVE_AGENT || "unknown",
|
|
379
411
|
stages,
|
|
380
412
|
});
|
|
@@ -404,7 +436,7 @@ function run(argv) {
|
|
|
404
436
|
return process.stdout.write(JSON.stringify(rows, null, 2) + "\n");
|
|
405
437
|
}
|
|
406
438
|
if (limited.length === 0) return process.stdout.write(command === "ready" ? "Nothing ready.\n" : "No tasks.\n");
|
|
407
|
-
for (const task of limited) process.stdout.write(task.id.padEnd(34) + task.state.padEnd(9) + stageRole(task).padEnd(11) + task.title + "\n");
|
|
439
|
+
for (const task of limited) process.stdout.write(task.id.padEnd(34) + task.state.padEnd(9) + stageRole(task).padEnd(11) + task.title + (task.draft ? " [draft]" : "") + "\n");
|
|
408
440
|
return;
|
|
409
441
|
}
|
|
410
442
|
|
|
@@ -438,6 +470,7 @@ function run(argv) {
|
|
|
438
470
|
|
|
439
471
|
const preview = readTask(repo, id);
|
|
440
472
|
if (!preview) fail("no such task: " + id, 2);
|
|
473
|
+
if (isDraft(preview)) fail(draftClaimMessage(id), 2);
|
|
441
474
|
if (readMode(repo) === "freeze" && !options.force && !(preview.tags || []).includes("fix")) {
|
|
442
475
|
fail("mode is freeze: only fix-tagged cards may be claimed (use --force to override)", 2);
|
|
443
476
|
}
|
|
@@ -454,6 +487,7 @@ function run(argv) {
|
|
|
454
487
|
const map = byId(tasks);
|
|
455
488
|
const task = map.get(id);
|
|
456
489
|
if (!task) throw new Error("no such task: " + id);
|
|
490
|
+
if (isDraft(task)) throw new Error(draftClaimMessage(id));
|
|
457
491
|
const canReclaim = isReclaimable(task);
|
|
458
492
|
if (task.state !== "pending" && !canReclaim) throw new Error("task " + id + " is " + task.state + stageOf(task));
|
|
459
493
|
if (isStaged(task) && task.stageState !== "pending" && !(task.stageState === "claimed" && canReclaim)) {
|
|
@@ -476,20 +510,21 @@ function run(argv) {
|
|
|
476
510
|
|
|
477
511
|
if (!claimed) fail("could not claim " + id, 2);
|
|
478
512
|
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
if (entry.id !== id) return entry;
|
|
484
|
-
return transition(entry, "pending", { owner: null, claimedUntil: null, reason: "lease held by " + (holder ? holder.agent : "another agent"), ...stageStatePatch(entry, "pending") });
|
|
485
|
-
}));
|
|
486
|
-
const detail = holder ? " (" + holder.agent + " holds " + holder.key + ")" : "";
|
|
487
|
-
fail("could not claim " + id + ": path busy" + detail, 2);
|
|
488
|
-
}
|
|
489
|
-
|
|
513
|
+
// Announcements are information, never a gate: the card claim already
|
|
514
|
+
// succeeded, and a live announcement by someone else is reported as an
|
|
515
|
+
// overlap instead of reverting the card.
|
|
516
|
+
const announcements = claimLeases(repo, { paths: claimed.scope, agent, ttlMs, taskId: claimed.id });
|
|
490
517
|
publishPresence(repo, { agent, task: claimed, state: "working", files: claimed.scope });
|
|
491
|
-
append(repo, {
|
|
492
|
-
|
|
518
|
+
append(repo, {
|
|
519
|
+
actor: agent,
|
|
520
|
+
kind: "task_claim",
|
|
521
|
+
task: claimed.id,
|
|
522
|
+
attempt: claimed.attempt,
|
|
523
|
+
stage: isStaged(claimed) ? claimed.stage : undefined,
|
|
524
|
+
...(announcements.overlaps.length > 0 ? { overlaps: announcements.overlaps } : {}),
|
|
525
|
+
});
|
|
526
|
+
const extra = announcements.overlaps.length > 0 ? { overlaps: announcements.overlaps } : {};
|
|
527
|
+
return printTask(claimed, options.json, extra);
|
|
493
528
|
}
|
|
494
529
|
|
|
495
530
|
if (command === "done" || command === "fail" || command === "retry") {
|
package/src/lease.js
CHANGED
|
@@ -39,40 +39,93 @@ export function readMode(repo) {
|
|
|
39
39
|
return (readBlob(repo, MODE_REF) || { mode: "free" }).mode;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
/**
|
|
42
|
+
/** One consistent observation of a lease ref: its sha and parsed blob. */
|
|
43
|
+
function readEntry(repo, ref) {
|
|
44
|
+
const rev = git(["rev-parse", "--verify", "--quiet", ref], { cwd: repo.top, allowFail: true });
|
|
45
|
+
if (rev.status !== 0) return { sha: null, value: null };
|
|
46
|
+
const sha = rev.stdout.trim();
|
|
47
|
+
try {
|
|
48
|
+
return { sha, value: JSON.parse(out(["cat-file", "blob", sha], { cwd: repo.top })) };
|
|
49
|
+
} catch {
|
|
50
|
+
return { sha, value: null };
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Write one announcement with no compare-and-swap: the last announcer wins. */
|
|
55
|
+
function replaceAnnouncement(repo, ref, value) {
|
|
56
|
+
const blob = out(["hash-object", "-w", "--stdin"], { cwd: repo.top, input: JSON.stringify(value) });
|
|
57
|
+
return git(["update-ref", ref, blob], { cwd: repo.top, allowFail: true }).status === 0;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Announce every scope path. Announcements never fail and never block a claim.
|
|
62
|
+
*
|
|
63
|
+
* The consensus layer is a shared announcement board: the latest announcer per
|
|
64
|
+
* key wins, and a live announcement by another agent is reported as an overlap
|
|
65
|
+
* (information) instead of refusing the card. merge-on-commit reconciles two
|
|
66
|
+
* agents writing the same file at commit time.
|
|
67
|
+
*
|
|
68
|
+
* Returns { granted: true, keys, overlaps: [{ key, agent, task, expiresAt }] }.
|
|
69
|
+
*/
|
|
43
70
|
export function claimLeases(repo, { paths, agent, ttlMs, taskId }) {
|
|
44
|
-
const keys =
|
|
45
|
-
|
|
71
|
+
const keys = [];
|
|
72
|
+
for (const path of paths) {
|
|
73
|
+
const key = toKey(path);
|
|
74
|
+
if (!keys.includes(key)) keys.push(key);
|
|
75
|
+
}
|
|
76
|
+
if (keys.length === 0) return { granted: true, keys, overlaps: [] };
|
|
46
77
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
78
|
+
let overlaps = [];
|
|
79
|
+
for (let attempt = 0; attempt < 8; attempt++) {
|
|
80
|
+
const now = Date.now();
|
|
81
|
+
const entries = new Map();
|
|
82
|
+
overlaps = [];
|
|
83
|
+
for (const key of keys) {
|
|
84
|
+
const entry = readEntry(repo, leaseRef(key));
|
|
85
|
+
entries.set(key, entry);
|
|
86
|
+
const current = entry.value;
|
|
87
|
+
if (
|
|
88
|
+
current &&
|
|
89
|
+
Date.parse(current.expiresAt) > now &&
|
|
90
|
+
(current.agent !== agent || (current.task || "") !== (taskId || ""))
|
|
91
|
+
) {
|
|
92
|
+
overlaps.push({ key, agent: current.agent, task: current.task || "", expiresAt: current.expiresAt });
|
|
93
|
+
}
|
|
94
|
+
}
|
|
50
95
|
|
|
51
|
-
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
96
|
+
const lines = ["start"];
|
|
97
|
+
for (const key of keys) {
|
|
98
|
+
const entry = entries.get(key);
|
|
99
|
+
const value = {
|
|
100
|
+
v: 1,
|
|
101
|
+
agent,
|
|
102
|
+
task: taskId || "",
|
|
103
|
+
key,
|
|
104
|
+
startedAt: new Date(now).toISOString(),
|
|
105
|
+
expiresAt: new Date(now + ttlMs).toISOString(),
|
|
106
|
+
};
|
|
107
|
+
const blob = out(["hash-object", "-w", "--stdin"], { cwd: repo.top, input: JSON.stringify(value) });
|
|
108
|
+
lines.push("update " + leaseRef(key) + " " + blob + " " + (entry.sha || "0".repeat(blob.length)));
|
|
57
109
|
}
|
|
58
|
-
|
|
110
|
+
lines.push("prepare", "commit");
|
|
111
|
+
const result = git(["update-ref", "--stdin"], { cwd: repo.top, input: lines.join("\n") + "\n", allowFail: true });
|
|
112
|
+
if (result.status === 0) return { granted: true, keys, overlaps };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Announcements are advisory: after repeated contention the last write wins
|
|
116
|
+
// instead of turning an announcement into a failure.
|
|
117
|
+
const now = Date.now();
|
|
118
|
+
for (const key of keys) {
|
|
119
|
+
replaceAnnouncement(repo, leaseRef(key), {
|
|
59
120
|
v: 1,
|
|
60
121
|
agent,
|
|
61
122
|
task: taskId || "",
|
|
62
123
|
key,
|
|
63
124
|
startedAt: new Date(now).toISOString(),
|
|
64
125
|
expiresAt: new Date(now + ttlMs).toISOString(),
|
|
65
|
-
};
|
|
66
|
-
const blob = out(["hash-object", "-w", "--stdin"], { cwd: repo.top, input: JSON.stringify(value) });
|
|
67
|
-
const currentSha = git(["rev-parse", "--verify", "--quiet", ref], { cwd: repo.top, allowFail: true }).stdout.trim();
|
|
68
|
-
lines.push("update " + ref + " " + blob + " " + (currentSha || "0".repeat(blob.length)));
|
|
126
|
+
});
|
|
69
127
|
}
|
|
70
|
-
|
|
71
|
-
if (denied.length > 0) return { granted: false, denied };
|
|
72
|
-
|
|
73
|
-
lines.push("prepare", "commit");
|
|
74
|
-
const result = git(["update-ref", "--stdin"], { cwd: repo.top, input: lines.join("\n") + "\n", allowFail: true });
|
|
75
|
-
return { granted: result.status === 0, denied: [], stderr: (result.stderr || "").trim() };
|
|
128
|
+
return { granted: true, keys, overlaps };
|
|
76
129
|
}
|
|
77
130
|
|
|
78
131
|
export function releaseLeases(repo, { paths, agent }) {
|
package/src/task.js
CHANGED
|
@@ -21,7 +21,7 @@ export function now() {
|
|
|
21
21
|
return new Date().toISOString();
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
export function create({ title, goal, role, scope, after, priority, createdBy, tags, acceptance, reviewOf, stages }) {
|
|
24
|
+
export function create({ title, goal, role, scope, after, priority, createdBy, tags, acceptance, reviewOf, stages, draft }) {
|
|
25
25
|
if (!title || !title.trim()) throw new Error("title is required");
|
|
26
26
|
const time = now();
|
|
27
27
|
const firstRole = Array.isArray(stages) && stages[0] ? stages[0].role : null;
|
|
@@ -47,13 +47,23 @@ export function create({ title, goal, role, scope, after, priority, createdBy, t
|
|
|
47
47
|
createdBy: createdBy || "unknown",
|
|
48
48
|
createdAt: time,
|
|
49
49
|
updatedAt: time,
|
|
50
|
+
stateChangedAt: time,
|
|
50
51
|
};
|
|
52
|
+
// draft is an optional flag, never a state: a draft card keeps the normal
|
|
53
|
+
// pending/claimed/done/failed machine but is never dispatched. Normal cards
|
|
54
|
+
// omit the key entirely, exactly like the other optional fields.
|
|
55
|
+
if (draft) task.draft = true;
|
|
51
56
|
// A card without stages is untouched: no stage fields are stored at all, so
|
|
52
57
|
// 1.3.0 blobs and 1.4.0 legacy blobs are identical.
|
|
53
58
|
if (stages && stages.length > 0) Object.assign(task, initialStageFields(stages));
|
|
54
59
|
return task;
|
|
55
60
|
}
|
|
56
61
|
|
|
62
|
+
/** A draft is a flag, not a state: it only removes the card from dispatch. */
|
|
63
|
+
export function isDraft(task) {
|
|
64
|
+
return Boolean(task && task.draft === true);
|
|
65
|
+
}
|
|
66
|
+
|
|
57
67
|
/** A claimed card whose claim expired can be taken over by the next agent. */
|
|
58
68
|
export function isReclaimable(task, now = Date.now()) {
|
|
59
69
|
if (task.state !== "claimed") return false;
|
|
@@ -72,6 +82,8 @@ function depsDone(task, byId) {
|
|
|
72
82
|
* (pending, or claimed and expired so it can be recovered).
|
|
73
83
|
*/
|
|
74
84
|
export function isReady(task, byId, { modeFree = true, now = Date.now() } = {}) {
|
|
85
|
+
// A draft never enters a dispatch computation, whatever its state.
|
|
86
|
+
if (isDraft(task)) return false;
|
|
75
87
|
const claimable = task.state === "pending" || isReclaimable(task, now);
|
|
76
88
|
if (!claimable) return false;
|
|
77
89
|
if (!depsDone(task, byId)) return false;
|
|
@@ -86,9 +98,22 @@ export function order(a, b) {
|
|
|
86
98
|
return a.createdAt < b.createdAt ? -1 : 1;
|
|
87
99
|
}
|
|
88
100
|
|
|
101
|
+
/**
|
|
102
|
+
* The lane a card is in: its state plus the current stage and stage state. A
|
|
103
|
+
* stage advance can keep the card pending, so state alone is not enough.
|
|
104
|
+
*/
|
|
105
|
+
function laneKey(task) {
|
|
106
|
+
return [task.state, task.stage == null ? "" : task.stage, task.stageState == null ? "" : task.stageState].join("\u0000");
|
|
107
|
+
}
|
|
108
|
+
|
|
89
109
|
export function transition(task, next, patch = {}) {
|
|
90
110
|
if (!STATES.includes(next)) throw new Error("unknown state: " + next);
|
|
91
|
-
|
|
111
|
+
const updated = { ...task, ...patch, state: next };
|
|
112
|
+
// stateChangedAt tracks real lane/stage moves only; a patch that leaves the
|
|
113
|
+
// lane alone (a reason, a verdict, metadata) must not reset it.
|
|
114
|
+
if (laneKey(updated) !== laneKey(task)) updated.stateChangedAt = now();
|
|
115
|
+
updated.updatedAt = now();
|
|
116
|
+
return updated;
|
|
92
117
|
}
|
|
93
118
|
|
|
94
119
|
/**
|