hive-tasks 1.0.0 → 1.1.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 CHANGED
@@ -4,6 +4,29 @@ All notable changes to this project are documented here. The format follows
4
4
  [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and the project uses
5
5
  [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [1.1.0] - 2026-12-03
8
+
9
+ ### Added
10
+
11
+ - Claim expiry: every claim records claimedUntil. A claimed card past that time is
12
+ reclaimable, so a crashed or starved agent is recovered without an operator.
13
+ - renew <id> [--ttl]: extend a claim without releasing it.
14
+ - done --verify: run the card acceptance command before finishing; the default stays
15
+ off, because a card is repository content and executing it is a code-execution path.
16
+ - resultCommit: done records the commit the work landed on (--sha or HEAD).
17
+ - tags, plus freeze semantics: while hive mode is freeze, ready returns only fix-tagged
18
+ cards, so the hotfix can run while the rest waits.
19
+ - plan --file: create a whole dependency tree in one tree write (one transaction).
20
+
21
+ ### Changed
22
+
23
+ - claim now sets claimedUntil alongside owner and attempt.
24
+
25
+ ### Tests
26
+
27
+ - 14 node:test scenarios: freeze filtering, claim expiry and reclaim, renew, acceptance
28
+ pass/fail, plan DAG creation, plus the earlier claim race and state machine cases.
29
+
7
30
  ## [1.0.0] - 2026-12-03
8
31
 
9
32
  ### Added
@@ -22,4 +45,5 @@ All notable changes to this project are documented here. The format follows
22
45
  - SPEC.md and RFC-0001 (why one tree, why the queue is derived).
23
46
  - 9 node:test scenarios, including two concurrent claims.
24
47
 
48
+ [1.1.0]: https://github.com/d86e/hive-tasks/releases/tag/v1.1.0
25
49
  [1.0.0]: https://github.com/d86e/hive-tasks/releases/tag/v1.0.0
package/README.md CHANGED
@@ -69,7 +69,9 @@ token-tests-ba66 pending tester token tests
69
69
  | ready [--limit N] [--role R] [--json] | pending cards whose dependencies are done, ordered by priority then age |
70
70
  | next [--role R] | the single best candidate |
71
71
  | claim <id> --agent A [--ttl 5m] | card CAS, then hive leases on the scope |
72
- | done <id> [--sha S] [--verdict pass\|fail] | finish, release leases, optionally record a verdict note |
72
+ | renew <id> [--ttl 5m] | extend a claim so a long run is not stolen |
73
+ | done <id> [--sha S] [--verdict pass\|fail] [--verify] | finish, release leases, record resultCommit; --verify runs the acceptance command first |
74
+ | plan --file plan.json | create a whole dependency tree in one transaction |
73
75
  | fail <id> --reason R | failure is data: attempt stays |
74
76
  | retry <id> | failed or blocked back to pending |
75
77
  | block <id> --reason R / unblock <id> | park a card without losing it |
@@ -84,6 +86,35 @@ Exit codes: 0 done, 1 internal error, 2 refused (already claimed, wrong state, d
84
86
 
85
87
  Reverting is another CAS we own, so a lost race never strands a lease. The card is the authority; the leases are the courtesy that keeps two agents out of the same file.
86
88
 
89
+ ## When an agent dies
90
+
91
+ Every claim carries claimedUntil (the --ttl, 5 minutes by default). Once it passes, the
92
+ card is **reclaimable**: the next agent that claims it takes over as if the card were
93
+ pending, incrementing attempt. No operator, no stale lock:
94
+
95
+ ~~~bash
96
+ $ hive-tasks claim slow-task-1a2b --agent agent-A --ttl 1s
97
+ $ # agent A dies here
98
+ $ sleep 2
99
+ $ hive-tasks claim slow-task-1a2b --agent agent-B # attempt 2, owner agent-B
100
+ ~~~
101
+
102
+ A long but healthy run calls renew to keep its claim.
103
+
104
+ ## Acceptance and freeze
105
+
106
+ A card may carry an acceptance command. It is **not** executed by default, because a card
107
+ is repository content and executing it would mean running code someone else pushed:
108
+
109
+ ~~~bash
110
+ $ hive-tasks add --title "auth takes an object" --acceptance "npm test -- auth"
111
+ $ hive-tasks done auth-takes-an-object-dea2 --verify # runs it, records resultCommit
112
+ $ hive-tasks done auth-takes-an-object-dea2 # records resultCommit only
113
+ ~~~
114
+
115
+ When hive mode is freeze, ready returns only cards tagged fix, so the hotfix can run while
116
+ the pipeline is red. Everything else waits.
117
+
87
118
  ## Where it fits
88
119
 
89
120
  | Project | Question |
package/README.zh-CN.md CHANGED
@@ -69,7 +69,9 @@ token-tests-ba66 pending tester token 测试
69
69
  | ready [--limit N] [--role R] [--json] | 依赖已完成的 pending 卡,按优先级、再按时间排序 |
70
70
  | next [--role R] | 只给最优的那一张 |
71
71
  | claim <id> --agent A [--ttl 5m] | 先卡片 CAS,再对 scope 拿 hive 租约 |
72
- | done <id> [--sha S] [--verdict pass\|fail] | 完成、释放租约、可选写判定 note |
72
+ | renew <id> [--ttl 5m] | 续期认领,长任务不会被抢 |
73
+ | done <id> [--sha S] [--verdict pass\|fail] [--verify] | 完成、释放租约、记录 resultCommit;--verify 先跑验收命令 |
74
+ | plan --file plan.json | 一次事务创建整棵依赖树 |
73
75
  | fail <id> --reason R | 失败是数据:attempt 保留 |
74
76
  | retry <id> | failed 或 blocked 回到 pending |
75
77
  | block <id> --reason R / unblock <id> | 挂起而不丢卡 |
@@ -84,6 +86,31 @@ token-tests-ba66 pending tester token 测试
84
86
 
85
87
  回滚是我们自己持有的另一次 CAS,所以输掉竞争不会泄漏租约。**卡是权威,租约是礼貌**——它只是让两个 agent 不去碰同一个文件。
86
88
 
89
+ ## agent 中途死掉怎么办
90
+
91
+ 每次认领都带 claimedUntil(--ttl,默认 5 分钟)。时间一过,这张卡就是**可回收的**:下一个来认领的 agent 直接接管,attempt +1,不需要人工介入:
92
+
93
+ ~~~bash
94
+ $ hive-tasks claim slow-task-1a2b --agent agent-A --ttl 1s
95
+ $ # agent A 在这里崩溃
96
+ $ sleep 2
97
+ $ hive-tasks claim slow-task-1a2b --agent agent-B # attempt 2,owner agent-B
98
+ ~~~
99
+
100
+ 跑得久的健康任务用 renew 续期。
101
+
102
+ ## 验收与冻结
103
+
104
+ 卡片可以带一条验收命令。**默认不执行**——卡片是仓库内容,默认执行等于让别人 push 一段代码就能在你机器上跑:
105
+
106
+ ~~~bash
107
+ $ hive-tasks add --title "auth 改成对象入参" --acceptance "npm test -- auth"
108
+ $ hive-tasks done auth-takes-an-object-dea2 --verify # 执行验收,记录 resultCommit
109
+ $ hive-tasks done auth-takes-an-object-dea2 # 只记录 resultCommit
110
+ ~~~
111
+
112
+ hive 处于 freeze 模式时,ready 只返回带 fix 标签的卡,让热修能跑、其余等待。
113
+
87
114
  ## 和另外两块的关系
88
115
 
89
116
  | 项目 | 回答的问题 |
package/SPEC.md CHANGED
@@ -62,6 +62,10 @@ git update-ref refs/hive/v1/tasks <new-tree> <old-tree>
62
62
  "owner": null,
63
63
  "attempt": 0,
64
64
  "priority": 0,
65
+ "tags": [],
66
+ "acceptance": "",
67
+ "claimedUntil": null,
68
+ "resultCommit": null,
65
69
  "verdict": null,
66
70
  "createdBy": "queen",
67
71
  "createdAt": "2026-12-03T15:00:00.000Z",
@@ -79,6 +83,10 @@ git update-ref refs/hive/v1/tasks <new-tree> <old-tree>
79
83
  | owner | agent id while claimed |
80
84
  | attempt | incremented on every claim; used to stop retry loops |
81
85
  | verdict | pass or fail, copied from the hive verdict when done |
86
+ | tags | free labels; "fix" is special (see mode) |
87
+ | acceptance | optional shell command; only run by done --verify |
88
+ | claimedUntil | expiry of the current claim; a claimed card past it is reclaimable |
89
+ | resultCommit | the commit recorded when the card was done |
82
90
 
83
91
  ## 4. State machine
84
92
 
@@ -101,6 +109,10 @@ git update-ref refs/hive/v1/tasks <new-tree> <old-tree>
101
109
 
102
110
  Stored states are pending, claimed, done, failed, blocked. ready is always computed.
103
111
 
112
+ A claimed card carries claimedUntil. Once that time passes the card is **reclaimable**:
113
+ claim may take it over exactly as if it were pending, incrementing attempt. This is how
114
+ a crashed or starved agent is recovered without an operator.
115
+
104
116
  Every transition is a compare-and-swap on the task ref: read the blob, verify the
105
117
  current state, write the new blob with the ref's current sha as the expected value.
106
118
  Two claimants therefore cannot both win.
@@ -111,7 +123,8 @@ A task is ready when:
111
123
 
112
124
  1. state is pending;
113
125
  2. every id in after exists and is done;
114
- 3. hive mode is free (or the dispatcher was started with --force).
126
+ 3. hive mode is free, or the card is tagged "fix" (the fix must be able to run while the
127
+ pipeline is frozen).
115
128
 
116
129
  A missing dependency id is a data error: report it, do not treat it as satisfied.
117
130
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hive-tasks",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Task cards for agent teams: one git tree behind one ref, with a state machine, dependencies and hive leases.",
5
5
  "type": "module",
6
6
  "bin": {
package/src/cli.js CHANGED
@@ -1,9 +1,10 @@
1
+ import { spawnSync } from "node:child_process";
1
2
  import fs from "node:fs";
2
3
  import path from "node:path";
3
4
  import { git, out } from "./git.js";
4
5
  import { ensureState, repoInfo } from "./repo.js";
5
6
  import { listRaw, mutate, readTask } from "./store.js";
6
- import { create, isReady, order, transition } from "./task.js";
7
+ import { create, isReady, isReclaimable, order, transition } from "./task.js";
7
8
  import { addVerdict, claimLeases, parseTtl, readMode, releaseLeases, toKey } from "./lease.js";
8
9
  import { append } from "./events.js";
9
10
 
@@ -14,13 +15,16 @@ const USAGE = [
14
15
  "",
15
16
  "USAGE",
16
17
  " hive-tasks init create .hive/ and the AGENTS.md section",
17
- " hive-tasks add --title T [--role R] [--scope P]... [--after ID]... [--goal G] [--priority N]",
18
+ " hive-tasks add --title T [--role R] [--scope P]... [--after ID]... [--tag T] [--priority N]",
19
+ " [--goal G] [--acceptance CMD]",
18
20
  " hive-tasks list [--json] [--state S] [--role R]",
19
21
  " hive-tasks show <id> [--json]",
20
22
  " hive-tasks ready [--json] [--limit N] [--role R]",
21
23
  " hive-tasks next [--role R] [--json] the single best candidate",
22
24
  " hive-tasks claim <id> --agent A [--ttl 5m] card CAS, then hive leases",
23
- " hive-tasks done <id> [--sha S] [--verdict pass|fail]",
25
+ " hive-tasks renew <id> [--ttl 5m] extend a claim",
26
+ " hive-tasks plan --file plan.json create a whole DAG in one transaction",
27
+ " hive-tasks done <id> [--sha S] [--verdict pass|fail] [--verify]",
24
28
  " hive-tasks fail <id> --reason R",
25
29
  " hive-tasks retry <id>",
26
30
  " hive-tasks block <id> --reason R | unblock <id>",
@@ -41,13 +45,17 @@ function value(argv, index, flag) {
41
45
  }
42
46
 
43
47
  function parse(argv) {
44
- const out = { args: [], scopes: [], after: [], 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 };
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 };
45
49
  for (let i = 0; i < argv.length; i++) {
46
50
  const arg = argv[i];
47
51
  if (arg === "--json") out.json = true;
48
52
  else if (arg === "--force") out.force = true;
49
53
  else if (arg === "--scope") out.scopes.push(value(argv, i++, arg));
50
54
  else if (arg === "--after") out.after.push(value(argv, i++, arg));
55
+ else if (arg === "--tag") out.tags.push(value(argv, i++, arg));
56
+ else if (arg === "--acceptance") out.acceptance = value(argv, i++, arg);
57
+ else if (arg === "--verify") out.verify = true;
58
+ else if (arg === "--file") out.file = value(argv, i++, arg);
51
59
  else if (arg === "--reason") out.reason = value(argv, i++, arg);
52
60
  else if (arg === "--agent") out.agent = value(argv, i++, arg);
53
61
  else if (arg === "--ttl") out.ttl = value(argv, i++, arg);
@@ -125,6 +133,61 @@ function run(argv) {
125
133
  return;
126
134
  }
127
135
 
136
+ if (command === "plan") {
137
+ const raw = options.file ? fs.readFileSync(options.file, "utf8") : fs.readFileSync(0, "utf8");
138
+ let plan;
139
+ try { plan = JSON.parse(raw); } catch { fail("plan is not valid JSON", 2); }
140
+ if (!Array.isArray(plan) || plan.length === 0) fail("plan must be a non-empty JSON array", 2);
141
+
142
+ const keys = new Map();
143
+ const drafts = plan.map((entry) => {
144
+ if (!entry || !entry.title) fail("every plan entry needs a title", 2);
145
+ const task = create({
146
+ title: entry.title,
147
+ goal: entry.goal,
148
+ role: entry.role,
149
+ scope: entry.scope || [],
150
+ after: [],
151
+ priority: entry.priority,
152
+ tags: entry.tags || [],
153
+ acceptance: entry.acceptance || "",
154
+ createdBy: options.agent || process.env.HIVE_AGENT || "planner",
155
+ });
156
+ if (entry.key) keys.set(entry.key, task.id);
157
+ return { entry, task };
158
+ });
159
+ for (const draft of drafts) {
160
+ draft.task.after = (draft.entry.after || []).map((dep) => keys.get(dep) || dep);
161
+ }
162
+
163
+ const result = mutate(repo, (tasks) => {
164
+ const known = new Set([...tasks.map((task) => task.id), ...drafts.map((draft) => draft.task.id)]);
165
+ for (const draft of drafts) {
166
+ for (const dep of draft.task.after) if (!known.has(dep)) throw new Error("unknown dependency: " + dep);
167
+ }
168
+ return [...tasks, ...drafts.map((draft) => draft.task)];
169
+ });
170
+ if (!result.ok) fail("could not write the task board");
171
+ append(repo, { actor: "planner", kind: "task_plan", count: drafts.length });
172
+ for (const draft of drafts) process.stdout.write(draft.task.id + " " + draft.task.title + "\n");
173
+ return;
174
+ }
175
+
176
+ if (command === "renew") {
177
+ const id = options.args[0];
178
+ if (!id) fail("renew needs a task id", 2);
179
+ const agent = identity(repo, options.agent);
180
+ const ttlMs = parseTtl(options.ttl);
181
+ const current = readTask(repo, id);
182
+ if (!current) fail("no such task: " + id, 2);
183
+ if (current.state !== "claimed" || current.owner !== agent) fail("task " + id + " is not claimed by " + agent, 2);
184
+ const result = mutate(repo, (tasks) => tasks.map((entry) => (entry.id === id ? { ...entry, claimedUntil: new Date(Date.now() + ttlMs).toISOString(), updatedAt: new Date().toISOString() } : entry)));
185
+ if (!result.ok) fail("could not renew " + id);
186
+ append(repo, { actor: agent, kind: "task_renew", task: id });
187
+ process.stdout.write(id + " renewed for " + Math.round(ttlMs / 1000) + "s\n");
188
+ return;
189
+ }
190
+
128
191
  if (command === "add") {
129
192
  if (!options.title) fail("--title is required", 2);
130
193
  const task = create({
@@ -134,6 +197,8 @@ function run(argv) {
134
197
  scope: options.scopes,
135
198
  after: options.after,
136
199
  priority: options.priority,
200
+ tags: options.tags,
201
+ acceptance: options.acceptance || "",
137
202
  createdBy: options.agent || process.env.HIVE_AGENT || "unknown",
138
203
  });
139
204
  const result = mutate(repo, (tasks) => {
@@ -182,11 +247,15 @@ function run(argv) {
182
247
  const id = options.args[0];
183
248
  if (!id) fail("claim needs a task id", 2);
184
249
  const agent = identity(repo, options.agent);
185
- if (readMode(repo) === "freeze" && !options.force) fail("mode is freeze: no new work (use --force to override)", 2);
250
+ const ttlMs = parseTtl(options.ttl);
186
251
 
187
252
  const preview = readTask(repo, id);
188
253
  if (!preview) fail("no such task: " + id, 2);
189
- if (preview.state !== "pending") fail("task " + id + " is " + preview.state, 2);
254
+ if (readMode(repo) === "freeze" && !options.force && !(preview.tags || []).includes("fix")) {
255
+ fail("mode is freeze: only fix-tagged cards may be claimed (use --force to override)", 2);
256
+ }
257
+ const reclaim = isReclaimable(preview);
258
+ if (preview.state !== "pending" && !reclaim) fail("task " + id + " is " + preview.state, 2);
190
259
  if (!isReady(preview, byId(board(repo)), { modeFree: true })) fail("task " + id + " is not ready (dependencies or mode)", 2);
191
260
 
192
261
  let claimed = null;
@@ -195,9 +264,14 @@ function run(argv) {
195
264
  const map = byId(tasks);
196
265
  const task = map.get(id);
197
266
  if (!task) throw new Error("no such task: " + id);
198
- if (task.state !== "pending") throw new Error("task " + id + " is " + task.state);
267
+ if (task.state !== "pending" && !isReclaimable(task)) throw new Error("task " + id + " is " + task.state);
199
268
  if (!isReady(task, map, { modeFree: true })) throw new Error("task " + id + " is not ready (dependencies or mode)");
200
- claimed = transition(task, "claimed", { owner: agent, attempt: (task.attempt || 0) + 1, reason: null });
269
+ claimed = transition(task, "claimed", {
270
+ owner: agent,
271
+ attempt: (task.attempt || 0) + 1,
272
+ reason: null,
273
+ claimedUntil: new Date(Date.now() + ttlMs).toISOString(),
274
+ });
201
275
  return tasks.map((entry) => (entry.id === id ? claimed : entry));
202
276
  });
203
277
  } catch (error) {
@@ -206,7 +280,6 @@ function run(argv) {
206
280
 
207
281
  if (!claimed) fail("could not claim " + id, 2);
208
282
 
209
- const ttlMs = parseTtl(options.ttl);
210
283
  const leases = claimLeases(repo, { paths: claimed.scope, agent, ttlMs, taskId: claimed.id });
211
284
  if (leases.granted === false) {
212
285
  const holder = leases.denied[0];
@@ -231,11 +304,22 @@ function run(argv) {
231
304
  if (command === "retry" && current.state !== "failed" && current.state !== "blocked") fail("task " + id + " is " + current.state + ", not failed or blocked", 2);
232
305
  if (command === "unblock" && current.state !== "blocked") fail("task " + id + " is " + current.state + ", not blocked", 2);
233
306
 
307
+ if (command === "done" && options.verify) {
308
+ if (!current.acceptance) fail("task " + id + " has no acceptance command", 2);
309
+ const run = spawnSync("sh", ["-c", current.acceptance], { cwd: repo.top, encoding: "utf8" });
310
+ if (run.stdout) process.stdout.write(run.stdout);
311
+ if (run.stderr) process.stderr.write(run.stderr);
312
+ if (run.status !== 0) fail("acceptance failed for " + id, 1);
313
+ }
314
+
315
+ const head = git(["rev-parse", "--verify", "--quiet", "HEAD"], { cwd: repo.top, allowFail: true }).stdout.trim();
234
316
  const targetState = command === "done" ? "done" : command === "fail" ? "failed" : command === "block" ? "blocked" : "pending";
235
317
  const patch = {
236
318
  owner: null,
237
319
  verdict: command === "done" ? (options.verdict || null) : current.verdict,
238
320
  reason: options.reason || null,
321
+ resultCommit: command === "done" ? (options.sha || head || null) : current.resultCommit,
322
+ claimedUntil: null,
239
323
  };
240
324
 
241
325
  const result = mutate(repo, (tasks) => tasks.map((entry) => (entry.id === id ? transition(entry, targetState, patch) : entry)));
package/src/task.js CHANGED
@@ -20,7 +20,7 @@ export function now() {
20
20
  return new Date().toISOString();
21
21
  }
22
22
 
23
- export function create({ title, goal, role, scope, after, priority, createdBy }) {
23
+ export function create({ title, goal, role, scope, after, priority, createdBy, tags, acceptance }) {
24
24
  if (!title || !title.trim()) throw new Error("title is required");
25
25
  const time = now();
26
26
  return {
@@ -35,6 +35,10 @@ export function create({ title, goal, role, scope, after, priority, createdBy })
35
35
  owner: null,
36
36
  attempt: 0,
37
37
  priority: Number.isFinite(priority) ? priority : 0,
38
+ tags: tags || [],
39
+ acceptance: acceptance || "",
40
+ claimedUntil: null,
41
+ resultCommit: null,
38
42
  verdict: null,
39
43
  reason: null,
40
44
  createdBy: createdBy || "unknown",
@@ -43,13 +47,29 @@ export function create({ title, goal, role, scope, after, priority, createdBy })
43
47
  };
44
48
  }
45
49
 
46
- /** A task is ready when it is pending, unlocked by mode, and its deps are done. */
47
- export function isReady(task, byId, { modeFree = true } = {}) {
48
- if (task.state !== "pending") return false;
49
- if (!modeFree) return false;
50
+ /** A claimed card whose claim expired can be taken over by the next agent. */
51
+ export function isReclaimable(task, now = Date.now()) {
52
+ if (task.state !== "claimed") return false;
53
+ if (!task.claimedUntil) return false;
54
+ return Date.parse(task.claimedUntil) <= now;
55
+ }
56
+
57
+ function depsDone(task, byId) {
50
58
  return (task.after || []).every((id) => byId.get(id) && byId.get(id).state === "done");
51
59
  }
52
60
 
61
+ /**
62
+ * Ready = pending (or a reclaimable claim), dependencies done, and allowed by mode:
63
+ * in freeze only tags include "fix", because the fix itself must be able to run.
64
+ */
65
+ export function isReady(task, byId, { modeFree = true, now = Date.now() } = {}) {
66
+ const claimable = task.state === "pending" || isReclaimable(task, now);
67
+ if (!claimable) return false;
68
+ if (!depsDone(task, byId)) return false;
69
+ if (modeFree) return true;
70
+ return Array.isArray(task.tags) && task.tags.includes("fix");
71
+ }
72
+
53
73
  /** Queue order: higher priority first, then oldest first. */
54
74
  export function order(a, b) {
55
75
  if ((b.priority || 0) !== (a.priority || 0)) return (b.priority || 0) - (a.priority || 0);