hive-tasks 1.2.0 → 1.5.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 +98 -0
- package/README.md +92 -5
- package/README.zh-CN.md +85 -5
- package/SPEC.md +258 -50
- package/package.json +4 -3
- package/src/cli.js +366 -60
- package/src/role.js +240 -0
- package/src/stage.js +240 -0
- package/src/task.js +28 -3
package/src/role.js
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Role resolution for dispatchers: a role always resolves, it never throws.
|
|
6
|
+
*
|
|
7
|
+
* Three tiers, in order:
|
|
8
|
+
* 1. repo .hive/roles.yaml has an entry for the role
|
|
9
|
+
* 2. default the name is one of the built-in roles (adapter dsh, model null,
|
|
10
|
+
* prompt from .hive/roles/<name>.md when that file exists)
|
|
11
|
+
* 3. synthetic a generated generic contract for any other name, so a card is
|
|
12
|
+
* never blocked because the team has no role config for it
|
|
13
|
+
*
|
|
14
|
+
* The returned object shape is stable and is what the runner/supervisor should
|
|
15
|
+
* consume:
|
|
16
|
+
*
|
|
17
|
+
* { name, source, adapter, model, prompt, args, stage }
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
export const DEFAULT_ADAPTER = "dsh";
|
|
21
|
+
export const DEFAULT_ROLES = ["coder", "tester", "reviewer", "researcher", "planner", "worker", "queen"];
|
|
22
|
+
|
|
23
|
+
const SAFE_ROLE_FILE = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A deliberately small YAML subset: nested maps, scalars, quoted strings,
|
|
27
|
+
* inline lists and comments. Enough for .hive/roles.yaml, nothing more.
|
|
28
|
+
*/
|
|
29
|
+
export function parseYaml(text) {
|
|
30
|
+
const root = {};
|
|
31
|
+
const stack = [{ indent: -1, node: root }];
|
|
32
|
+
for (const raw of String(text == null ? "" : text).split("\n")) {
|
|
33
|
+
if (!raw.trim() || raw.trim().startsWith("#")) continue;
|
|
34
|
+
const line = raw.replace(/\s+#.*$/, "");
|
|
35
|
+
if (!line.trim()) continue;
|
|
36
|
+
const indent = line.match(/^\s*/)[0].length;
|
|
37
|
+
const body = line.trim();
|
|
38
|
+
if (body.startsWith("-")) continue;
|
|
39
|
+
const at = body.indexOf(":");
|
|
40
|
+
if (at < 0) continue;
|
|
41
|
+
const key = body.slice(0, at).trim();
|
|
42
|
+
if (!key) continue;
|
|
43
|
+
while (stack.length > 1 && indent <= stack[stack.length - 1].indent) stack.pop();
|
|
44
|
+
const parent = stack[stack.length - 1].node;
|
|
45
|
+
const rest = body.slice(at + 1).trim();
|
|
46
|
+
if (rest === "") {
|
|
47
|
+
const child = {};
|
|
48
|
+
parent[key] = child;
|
|
49
|
+
stack.push({ indent, node: child });
|
|
50
|
+
} else {
|
|
51
|
+
parent[key] = parseScalar(rest);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return root;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function parseScalar(text) {
|
|
58
|
+
const value = String(text).trim();
|
|
59
|
+
if (value.length >= 2 && value.startsWith('"') && value.endsWith('"')) {
|
|
60
|
+
return value.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, "\\");
|
|
61
|
+
}
|
|
62
|
+
if (value.length >= 2 && value.startsWith("'") && value.endsWith("'")) {
|
|
63
|
+
return value.slice(1, -1).replace(/''/g, "'");
|
|
64
|
+
}
|
|
65
|
+
if (value === "true") return true;
|
|
66
|
+
if (value === "false") return false;
|
|
67
|
+
if (value === "null" || value === "~") return null;
|
|
68
|
+
if (/^-?\d+$/.test(value)) return Number(value);
|
|
69
|
+
if (value.startsWith("[") && value.endsWith("]")) {
|
|
70
|
+
return value.slice(1, -1).split(",").map((part) => parseScalar(part)).filter((part) => part !== "");
|
|
71
|
+
}
|
|
72
|
+
if (value.startsWith("{") && value.endsWith("}")) return parseFlowMap(value);
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function parseFlowMap(text) {
|
|
77
|
+
const out = {};
|
|
78
|
+
for (const part of splitTop(text.slice(1, -1))) {
|
|
79
|
+
const at = part.indexOf(":");
|
|
80
|
+
if (at < 0) continue;
|
|
81
|
+
out[part.slice(0, at).trim()] = parseScalar(part.slice(at + 1));
|
|
82
|
+
}
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function splitTop(text) {
|
|
87
|
+
const parts = [];
|
|
88
|
+
let current = "";
|
|
89
|
+
let quote = null;
|
|
90
|
+
let depth = 0;
|
|
91
|
+
for (const char of text) {
|
|
92
|
+
if (quote) {
|
|
93
|
+
current += char;
|
|
94
|
+
if (char === quote) quote = null;
|
|
95
|
+
} else if (char === '"' || char === "'") {
|
|
96
|
+
quote = char;
|
|
97
|
+
current += char;
|
|
98
|
+
} else if (char === "[" || char === "{") {
|
|
99
|
+
depth += 1;
|
|
100
|
+
current += char;
|
|
101
|
+
} else if (char === "]" || char === "}") {
|
|
102
|
+
depth -= 1;
|
|
103
|
+
current += char;
|
|
104
|
+
} else if (char === "," && depth === 0) {
|
|
105
|
+
parts.push(current);
|
|
106
|
+
current = "";
|
|
107
|
+
} else {
|
|
108
|
+
current += char;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (current.trim()) parts.push(current);
|
|
112
|
+
return parts;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function repoTop(repo) {
|
|
116
|
+
if (!repo) return null;
|
|
117
|
+
if (typeof repo === "string") return repo;
|
|
118
|
+
return repo.top || repo.repoTop || repo.cwd || null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function readText(file) {
|
|
122
|
+
try {
|
|
123
|
+
if (!file) return null;
|
|
124
|
+
return fs.readFileSync(file, "utf8").trim();
|
|
125
|
+
} catch {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function insideRepo(top, file) {
|
|
131
|
+
if (!top) return false;
|
|
132
|
+
const resolved = path.resolve(top, file);
|
|
133
|
+
const root = path.resolve(top);
|
|
134
|
+
return resolved === root || resolved.startsWith(root + path.sep);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function rolePrompt(top, name, explicit) {
|
|
138
|
+
const safeName = SAFE_ROLE_FILE.test(name) ? name : null;
|
|
139
|
+
const candidates = [];
|
|
140
|
+
if (explicit && top && insideRepo(top, explicit)) candidates.push(path.resolve(top, explicit));
|
|
141
|
+
if (safeName && top) candidates.push(path.join(top, ".hive", "roles", safeName + ".md"));
|
|
142
|
+
for (const file of candidates) {
|
|
143
|
+
const text = readText(file);
|
|
144
|
+
if (text !== null) return text;
|
|
145
|
+
}
|
|
146
|
+
return "";
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function rolesMap(top) {
|
|
150
|
+
if (!top) return {};
|
|
151
|
+
try {
|
|
152
|
+
const file = path.join(top, ".hive", "roles.yaml");
|
|
153
|
+
if (!fs.existsSync(file)) return {};
|
|
154
|
+
const parsed = parseYaml(fs.readFileSync(file, "utf8"));
|
|
155
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
|
|
156
|
+
} catch {
|
|
157
|
+
return {};
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function asEntry(value) {
|
|
162
|
+
if (typeof value === "string" && value.trim()) return { adapter: value.trim() };
|
|
163
|
+
if (value && typeof value === "object" && !Array.isArray(value)) return value;
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function textOrNull(value) {
|
|
168
|
+
if (value == null || value === "") return null;
|
|
169
|
+
return String(value);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function textOr(value, fallback) {
|
|
173
|
+
if (value == null || value === "") return fallback;
|
|
174
|
+
return String(value);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function normalizeArgs(value) {
|
|
178
|
+
if (value == null) return [];
|
|
179
|
+
if (Array.isArray(value)) return value.filter((part) => part != null && part !== "").map(String);
|
|
180
|
+
if (value === "") return [];
|
|
181
|
+
return [String(value)];
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function syntheticPrompt(name, stage) {
|
|
185
|
+
const lines = [
|
|
186
|
+
'You are acting as "' + name + '" for this repository.',
|
|
187
|
+
"Work only on the task card you were given: read its goal, scope and acceptance, then do the work and report the result.",
|
|
188
|
+
];
|
|
189
|
+
if (stage) lines.push("Current stage: " + stage + ".");
|
|
190
|
+
lines.push("Edit only the card scope. Report a failure with a concrete reason instead of guessing.");
|
|
191
|
+
return lines.join("\n");
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Resolve a role name against a repository. The first argument is a repoInfo
|
|
196
|
+
* object ({ top }) or a path string. The third argument is either a stage key
|
|
197
|
+
* string or { stage }; the stage is only used by synthetic prompts.
|
|
198
|
+
*
|
|
199
|
+
* Never throws: a missing or malformed roles.yaml, an unreadable prompt file
|
|
200
|
+
* or an unknown role name all resolve to the next tier, ending in synthetic.
|
|
201
|
+
*/
|
|
202
|
+
export function resolveRole(repo, name, options = {}) {
|
|
203
|
+
const top = repoTop(repo);
|
|
204
|
+
const requested = name == null ? "" : String(name).trim();
|
|
205
|
+
const roleName = requested || "worker";
|
|
206
|
+
let stage = null;
|
|
207
|
+
if (typeof options === "string") stage = options || null;
|
|
208
|
+
else if (options && typeof options === "object" && options.stage != null) stage = String(options.stage);
|
|
209
|
+
|
|
210
|
+
const base = {
|
|
211
|
+
name: roleName,
|
|
212
|
+
source: "default",
|
|
213
|
+
adapter: DEFAULT_ADAPTER,
|
|
214
|
+
model: null,
|
|
215
|
+
prompt: "",
|
|
216
|
+
args: [],
|
|
217
|
+
stage: stage || null,
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
try {
|
|
221
|
+
const roles = rolesMap(top);
|
|
222
|
+
const entry = Object.prototype.hasOwnProperty.call(roles, roleName) ? asEntry(roles[roleName]) : null;
|
|
223
|
+
if (entry) {
|
|
224
|
+
return {
|
|
225
|
+
...base,
|
|
226
|
+
source: "repo",
|
|
227
|
+
adapter: textOr(entry.adapter, DEFAULT_ADAPTER),
|
|
228
|
+
model: textOrNull(entry.model),
|
|
229
|
+
prompt: rolePrompt(top, roleName, typeof entry.prompt === "string" ? entry.prompt : null),
|
|
230
|
+
args: normalizeArgs(entry.args),
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
if (DEFAULT_ROLES.includes(roleName)) {
|
|
234
|
+
return { ...base, source: "default", prompt: rolePrompt(top, roleName, null) };
|
|
235
|
+
}
|
|
236
|
+
return { ...base, source: "synthetic", prompt: syntheticPrompt(roleName, base.stage) };
|
|
237
|
+
} catch {
|
|
238
|
+
return { ...base, source: "synthetic", prompt: syntheticPrompt(roleName, base.stage) };
|
|
239
|
+
}
|
|
240
|
+
}
|
package/src/stage.js
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stage chains: an optional ordered list of stages on one card.
|
|
3
|
+
*
|
|
4
|
+
* A card without stages is a single implicit stage: stage and stageState stay
|
|
5
|
+
* null, and every legacy path behaves exactly as it did in 1.3.0.
|
|
6
|
+
*
|
|
7
|
+
* A stage stores its configuration (key, role, acceptance, onFail) plus its
|
|
8
|
+
* runtime state (pending, claimed, done, failed, blocked). The card records
|
|
9
|
+
* the current stage and its state in stage / stageState.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export const STAGE_STATES = ["pending", "claimed", "done", "failed", "blocked"];
|
|
13
|
+
export const STAGE_KEY_RE = /^[a-z][a-z0-9-]{0,31}$/;
|
|
14
|
+
|
|
15
|
+
export function isStaged(task) {
|
|
16
|
+
return Boolean(task && Array.isArray(task.stages) && task.stages.length > 0);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function stageIndex(task, key) {
|
|
20
|
+
if (!isStaged(task)) return -1;
|
|
21
|
+
return task.stages.findIndex((stage) => stage.key === key);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The stage the card is on, or null for a card without stages. */
|
|
25
|
+
export function currentStage(task) {
|
|
26
|
+
if (!isStaged(task)) return null;
|
|
27
|
+
const index = stageIndex(task, task.stage);
|
|
28
|
+
return index >= 0 ? task.stages[index] : task.stages[0] || null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The role that works the card right now: the stage role, or the card role. */
|
|
32
|
+
export function stageRole(task) {
|
|
33
|
+
const stage = currentStage(task);
|
|
34
|
+
return stage ? stage.role : task.role;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** An expired claim on a non-pending card can be taken over. */
|
|
38
|
+
export function claimExpired(task, now = Date.now()) {
|
|
39
|
+
if (task.state !== "claimed" || !task.claimedUntil) return false;
|
|
40
|
+
return Date.parse(task.claimedUntil) <= now;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A card is stage-ready when its current stage is pending, or when an expired
|
|
45
|
+
* claim left the stage claimed (the same recovery rule cards already use).
|
|
46
|
+
*/
|
|
47
|
+
export function isStageReady(task, now = Date.now()) {
|
|
48
|
+
if (!isStaged(task)) return true;
|
|
49
|
+
if (task.stageState === "pending") return true;
|
|
50
|
+
return task.stageState === "claimed" && claimExpired(task, now);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Role filter for ready/list: a card without stages matches on its own role. */
|
|
54
|
+
export function matchesRole(task, role) {
|
|
55
|
+
if (!role) return true;
|
|
56
|
+
return stageRole(task) === role;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function keyBase(role) {
|
|
60
|
+
let base = String(role || "stage")
|
|
61
|
+
.toLowerCase()
|
|
62
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
63
|
+
.replace(/^-+|-+$/g, "");
|
|
64
|
+
if (!base) base = "stage";
|
|
65
|
+
if (!/^[a-z]/.test(base)) base = "stage-" + base;
|
|
66
|
+
return base.slice(0, 28) || "stage";
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Derive a valid, unique stage key from a role name. */
|
|
70
|
+
export function stageKeyFor(role, used = new Set()) {
|
|
71
|
+
const base = keyBase(role);
|
|
72
|
+
let key = base;
|
|
73
|
+
let n = 2;
|
|
74
|
+
while (used.has(key)) {
|
|
75
|
+
const suffix = "-" + n++;
|
|
76
|
+
key = base.slice(0, 32 - suffix.length) + suffix;
|
|
77
|
+
}
|
|
78
|
+
used.add(key);
|
|
79
|
+
return key;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function normalizeStage(stage, index) {
|
|
83
|
+
const where = "stage " + (index + 1);
|
|
84
|
+
if (!stage || typeof stage !== "object" || Array.isArray(stage)) {
|
|
85
|
+
throw new Error(where + " must be an object with key and role");
|
|
86
|
+
}
|
|
87
|
+
const key = String(stage.key || "").trim();
|
|
88
|
+
if (!STAGE_KEY_RE.test(key)) {
|
|
89
|
+
throw new Error(where + " needs a key matching " + STAGE_KEY_RE + " (got " + JSON.stringify(stage.key) + ")");
|
|
90
|
+
}
|
|
91
|
+
const role = String(stage.role == null ? "" : stage.role).trim();
|
|
92
|
+
if (!role) throw new Error(where + " (" + key + ") needs a role");
|
|
93
|
+
const out = { key, role, state: "pending" };
|
|
94
|
+
const acceptance = stage.acceptance == null ? "" : String(stage.acceptance).trim();
|
|
95
|
+
if (acceptance) out.acceptance = acceptance;
|
|
96
|
+
const onFail = stage.onFail == null ? "" : String(stage.onFail).trim();
|
|
97
|
+
if (onFail) out.onFail = onFail;
|
|
98
|
+
return out;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Validate a stage chain; returns a fresh normalized copy with state pending. */
|
|
102
|
+
export function normalizeStages(stages) {
|
|
103
|
+
if (!Array.isArray(stages) || stages.length === 0) {
|
|
104
|
+
throw new Error("stages must be a non-empty array");
|
|
105
|
+
}
|
|
106
|
+
const out = stages.map((stage, index) => normalizeStage(stage, index));
|
|
107
|
+
const keys = new Set();
|
|
108
|
+
for (const stage of out) {
|
|
109
|
+
if (keys.has(stage.key)) throw new Error("duplicate stage key: " + stage.key);
|
|
110
|
+
keys.add(stage.key);
|
|
111
|
+
}
|
|
112
|
+
for (const stage of out) {
|
|
113
|
+
if (stage.onFail && !keys.has(stage.onFail)) {
|
|
114
|
+
throw new Error("stage " + stage.key + " onFail target is not in this chain: " + stage.onFail);
|
|
115
|
+
}
|
|
116
|
+
if (stage.onFail === stage.key) {
|
|
117
|
+
throw new Error("stage " + stage.key + " cannot onFail back to itself");
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return out;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Parse repeatable --stage role[:acceptance] flags into a chain, keys derived from roles. */
|
|
124
|
+
export function stagesFromFlags(values) {
|
|
125
|
+
const used = new Set();
|
|
126
|
+
const drafts = values.map((raw) => {
|
|
127
|
+
const text = String(raw);
|
|
128
|
+
const at = text.indexOf(":");
|
|
129
|
+
const role = (at < 0 ? text : text.slice(0, at)).trim();
|
|
130
|
+
const acceptance = at < 0 ? "" : text.slice(at + 1).trim();
|
|
131
|
+
if (!role) throw new Error("every --stage needs a role before the optional :acceptance");
|
|
132
|
+
return { key: stageKeyFor(role, used), role, acceptance: acceptance || undefined };
|
|
133
|
+
});
|
|
134
|
+
return normalizeStages(drafts);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Stored fields for a brand new staged card. */
|
|
138
|
+
export function initialStageFields(stages) {
|
|
139
|
+
const list = normalizeStages(stages);
|
|
140
|
+
return { stages: list, stage: list[0].key, stageState: "pending" };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Copy a card's stage view for JSON output; a legacy card reads as empty/null. */
|
|
144
|
+
export function stageView(task) {
|
|
145
|
+
return {
|
|
146
|
+
stages: isStaged(task) ? task.stages.map((stage) => ({ ...stage })) : [],
|
|
147
|
+
stage: task.stage == null ? null : task.stage,
|
|
148
|
+
stageState: task.stageState == null ? null : task.stageState,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Set the current stage's runtime state (claim, lease revert, unblock). */
|
|
153
|
+
export function stageStatePatch(task, state) {
|
|
154
|
+
if (!isStaged(task)) return {};
|
|
155
|
+
const index = stageIndex(task, task.stage);
|
|
156
|
+
const stages = task.stages.map((entry, i) => (i === index ? { ...entry, state } : { ...entry }));
|
|
157
|
+
return { stages, stageState: state };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Complete the current stage. A non-final stage opens the next one and keeps
|
|
162
|
+
* the card pending; the final stage finishes the whole card.
|
|
163
|
+
*/
|
|
164
|
+
export function advanceStage(task) {
|
|
165
|
+
const index = stageIndex(task, task.stage);
|
|
166
|
+
if (index < 0) throw new Error("card has no current stage: " + JSON.stringify(task.stage));
|
|
167
|
+
const stages = task.stages.map((entry, i) => ({ ...entry }));
|
|
168
|
+
stages[index] = { ...stages[index], state: "done" };
|
|
169
|
+
const next = index + 1 < stages.length ? stages[index + 1] : null;
|
|
170
|
+
if (next) {
|
|
171
|
+
stages[index + 1] = { ...next, state: "pending" };
|
|
172
|
+
return {
|
|
173
|
+
finished: false,
|
|
174
|
+
nextStage: next.key,
|
|
175
|
+
patch: { stages, stage: next.key, stageState: "pending", state: "pending", owner: null, claimedUntil: null },
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
return {
|
|
179
|
+
finished: true,
|
|
180
|
+
nextStage: null,
|
|
181
|
+
patch: { stages, stage: stages[index].key, stageState: "done", state: "done", owner: null, claimedUntil: null },
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Fail or block the current stage. When the stage names an onFail target that
|
|
187
|
+
* is already done, the chain rewinds to it (target pending, later stages
|
|
188
|
+
* pending, attempt + 1) and the card stays runnable. Otherwise the whole card
|
|
189
|
+
* takes the failure state.
|
|
190
|
+
*/
|
|
191
|
+
export function failStage(task, nextState) {
|
|
192
|
+
if (!STAGE_STATES.includes(nextState)) throw new Error("unknown stage state: " + nextState);
|
|
193
|
+
const index = stageIndex(task, task.stage);
|
|
194
|
+
if (index < 0) throw new Error("card has no current stage: " + JSON.stringify(task.stage));
|
|
195
|
+
const stage = task.stages[index];
|
|
196
|
+
const target = stage.onFail ? stageIndex(task, stage.onFail) : -1;
|
|
197
|
+
|
|
198
|
+
if (target >= 0 && task.stages[target].state === "done") {
|
|
199
|
+
const stages = task.stages.map((entry, i) => {
|
|
200
|
+
if (i === index) return { ...entry, state: nextState };
|
|
201
|
+
if (i >= target) return { ...entry, state: "pending" };
|
|
202
|
+
return { ...entry };
|
|
203
|
+
});
|
|
204
|
+
stages[target] = { ...stages[target], state: "pending" };
|
|
205
|
+
return {
|
|
206
|
+
rewound: true,
|
|
207
|
+
from: stage.key,
|
|
208
|
+
to: stages[target].key,
|
|
209
|
+
patch: {
|
|
210
|
+
stages,
|
|
211
|
+
stage: stages[target].key,
|
|
212
|
+
stageState: "pending",
|
|
213
|
+
state: "pending",
|
|
214
|
+
owner: null,
|
|
215
|
+
claimedUntil: null,
|
|
216
|
+
attempt: (task.attempt || 0) + 1,
|
|
217
|
+
},
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const stages = task.stages.map((entry, i) => (i === index ? { ...entry, state: nextState } : { ...entry }));
|
|
222
|
+
return {
|
|
223
|
+
rewound: false,
|
|
224
|
+
from: stage.key,
|
|
225
|
+
to: null,
|
|
226
|
+
patch: { stages, stageState: nextState, state: nextState, owner: null, claimedUntil: null },
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** Reset a chain to a stage (default the first): that stage and later ones become pending. */
|
|
231
|
+
export function resetStage(task, key) {
|
|
232
|
+
if (!isStaged(task)) throw new Error("card has no stages");
|
|
233
|
+
const index = key == null || key === "" ? 0 : stageIndex(task, key);
|
|
234
|
+
if (index < 0) throw new Error("no such stage: " + key);
|
|
235
|
+
const stages = task.stages.map((entry, i) => (i >= index ? { ...entry, state: "pending" } : { ...entry }));
|
|
236
|
+
return {
|
|
237
|
+
stage: stages[index].key,
|
|
238
|
+
patch: { stages, stage: stages[index].key, stageState: "pending", state: "pending" },
|
|
239
|
+
};
|
|
240
|
+
}
|
package/src/task.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import crypto from "node:crypto";
|
|
2
|
+
import { initialStageFields, isStageReady } from "./stage.js";
|
|
2
3
|
|
|
3
4
|
export const STATES = ["pending", "claimed", "done", "failed", "blocked"];
|
|
4
5
|
export const ROLES = ["coder", "tester", "reviewer", "researcher", "planner", "worker"];
|
|
@@ -20,15 +21,16 @@ export function now() {
|
|
|
20
21
|
return new Date().toISOString();
|
|
21
22
|
}
|
|
22
23
|
|
|
23
|
-
export function create({ title, goal, role, scope, after, priority, createdBy, tags, acceptance }) {
|
|
24
|
+
export function create({ title, goal, role, scope, after, priority, createdBy, tags, acceptance, reviewOf, stages }) {
|
|
24
25
|
if (!title || !title.trim()) throw new Error("title is required");
|
|
25
26
|
const time = now();
|
|
26
|
-
|
|
27
|
+
const firstRole = Array.isArray(stages) && stages[0] ? stages[0].role : null;
|
|
28
|
+
const task = {
|
|
27
29
|
v: 1,
|
|
28
30
|
id: newId(title),
|
|
29
31
|
title: title.trim(),
|
|
30
32
|
goal: goal || "",
|
|
31
|
-
role: role || "worker",
|
|
33
|
+
role: role || firstRole || "worker",
|
|
32
34
|
scope: scope || [],
|
|
33
35
|
after: after || [],
|
|
34
36
|
state: "pending",
|
|
@@ -41,10 +43,15 @@ export function create({ title, goal, role, scope, after, priority, createdBy, t
|
|
|
41
43
|
resultCommit: null,
|
|
42
44
|
verdict: null,
|
|
43
45
|
reason: null,
|
|
46
|
+
reviewOf: reviewOf || null,
|
|
44
47
|
createdBy: createdBy || "unknown",
|
|
45
48
|
createdAt: time,
|
|
46
49
|
updatedAt: time,
|
|
47
50
|
};
|
|
51
|
+
// A card without stages is untouched: no stage fields are stored at all, so
|
|
52
|
+
// 1.3.0 blobs and 1.4.0 legacy blobs are identical.
|
|
53
|
+
if (stages && stages.length > 0) Object.assign(task, initialStageFields(stages));
|
|
54
|
+
return task;
|
|
48
55
|
}
|
|
49
56
|
|
|
50
57
|
/** A claimed card whose claim expired can be taken over by the next agent. */
|
|
@@ -61,11 +68,14 @@ function depsDone(task, byId) {
|
|
|
61
68
|
/**
|
|
62
69
|
* Ready = pending (or a reclaimable claim), dependencies done, and allowed by mode:
|
|
63
70
|
* in freeze only tags include "fix", because the fix itself must be able to run.
|
|
71
|
+
* A staged card is additionally ready only when its current stage is open
|
|
72
|
+
* (pending, or claimed and expired so it can be recovered).
|
|
64
73
|
*/
|
|
65
74
|
export function isReady(task, byId, { modeFree = true, now = Date.now() } = {}) {
|
|
66
75
|
const claimable = task.state === "pending" || isReclaimable(task, now);
|
|
67
76
|
if (!claimable) return false;
|
|
68
77
|
if (!depsDone(task, byId)) return false;
|
|
78
|
+
if (!isStageReady(task, now)) return false;
|
|
69
79
|
if (modeFree) return true;
|
|
70
80
|
return Array.isArray(task.tags) && task.tags.includes("fix");
|
|
71
81
|
}
|
|
@@ -80,3 +90,18 @@ export function transition(task, next, patch = {}) {
|
|
|
80
90
|
if (!STATES.includes(next)) throw new Error("unknown state: " + next);
|
|
81
91
|
return { ...task, ...patch, state: next, updatedAt: now() };
|
|
82
92
|
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Every stored state can be reopened back to pending, so no card is a one-way
|
|
96
|
+
* door. The old attempt is kept and counted up: the next claim is a new try.
|
|
97
|
+
*/
|
|
98
|
+
export function reopenPatch(task, reason = "reopened") {
|
|
99
|
+
return {
|
|
100
|
+
owner: null,
|
|
101
|
+
claimedUntil: null,
|
|
102
|
+
resultCommit: null,
|
|
103
|
+
verdict: null,
|
|
104
|
+
attempt: (task.attempt || 0) + 1,
|
|
105
|
+
reason,
|
|
106
|
+
};
|
|
107
|
+
}
|