hive-tasks 1.0.0 → 1.2.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 +32 -0
- package/README.md +55 -2
- package/README.zh-CN.md +49 -2
- package/SPEC.md +15 -2
- package/package.json +1 -1
- package/src/cli.js +111 -9
- package/src/task.js +25 -5
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,37 @@ 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.2.0] - 2026-12-04
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- hive-tasks stats [--json]: counts by state and role, plus the oldest pending card.
|
|
12
|
+
- Resource keys: scope entries may be any stable string (resource:port:3000,
|
|
13
|
+
resource:db:dev), so non-file shared resources are leased exactly like files.
|
|
14
|
+
|
|
15
|
+
## [1.1.0] - 2026-12-03
|
|
16
|
+
|
|
17
|
+
### Added
|
|
18
|
+
|
|
19
|
+
- Claim expiry: every claim records claimedUntil. A claimed card past that time is
|
|
20
|
+
reclaimable, so a crashed or starved agent is recovered without an operator.
|
|
21
|
+
- renew <id> [--ttl]: extend a claim without releasing it.
|
|
22
|
+
- done --verify: run the card acceptance command before finishing; the default stays
|
|
23
|
+
off, because a card is repository content and executing it is a code-execution path.
|
|
24
|
+
- resultCommit: done records the commit the work landed on (--sha or HEAD).
|
|
25
|
+
- tags, plus freeze semantics: while hive mode is freeze, ready returns only fix-tagged
|
|
26
|
+
cards, so the hotfix can run while the rest waits.
|
|
27
|
+
- plan --file: create a whole dependency tree in one tree write (one transaction).
|
|
28
|
+
|
|
29
|
+
### Changed
|
|
30
|
+
|
|
31
|
+
- claim now sets claimedUntil alongside owner and attempt.
|
|
32
|
+
|
|
33
|
+
### Tests
|
|
34
|
+
|
|
35
|
+
- 14 node:test scenarios: freeze filtering, claim expiry and reclaim, renew, acceptance
|
|
36
|
+
pass/fail, plan DAG creation, plus the earlier claim race and state machine cases.
|
|
37
|
+
|
|
7
38
|
## [1.0.0] - 2026-12-03
|
|
8
39
|
|
|
9
40
|
### Added
|
|
@@ -22,4 +53,5 @@ All notable changes to this project are documented here. The format follows
|
|
|
22
53
|
- SPEC.md and RFC-0001 (why one tree, why the queue is derived).
|
|
23
54
|
- 9 node:test scenarios, including two concurrent claims.
|
|
24
55
|
|
|
56
|
+
[1.1.0]: https://github.com/d86e/hive-tasks/releases/tag/v1.1.0
|
|
25
57
|
[1.0.0]: https://github.com/d86e/hive-tasks/releases/tag/v1.0.0
|
package/README.md
CHANGED
|
@@ -69,13 +69,37 @@ 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
|
-
|
|
|
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 |
|
|
76
78
|
|
|
77
79
|
Exit codes: 0 done, 1 internal error, 2 refused (already claimed, wrong state, dependency missing, mode freeze, path busy).
|
|
78
80
|
|
|
81
|
+
## Resource keys
|
|
82
|
+
|
|
83
|
+
scope entries are usually paths, but the lease layer does not care: any stable string
|
|
84
|
+
works. Use a prefix for shared things that are not files:
|
|
85
|
+
|
|
86
|
+
~~~bash
|
|
87
|
+
hive-tasks add --title "run the dev server" --scope src/web --scope resource:port:3000
|
|
88
|
+
hive-tasks add --title "migrate" --scope resource:db:dev
|
|
89
|
+
~~~
|
|
90
|
+
|
|
91
|
+
Two cards that name the same resource cannot be claimed at the same time, which is the
|
|
92
|
+
same guarantee files get. Nothing else is required: no daemon watches ports.
|
|
93
|
+
|
|
94
|
+
## Stats
|
|
95
|
+
|
|
96
|
+
~~~bash
|
|
97
|
+
hive-tasks stats
|
|
98
|
+
hive-tasks stats --json
|
|
99
|
+
~~~
|
|
100
|
+
|
|
101
|
+
Counts by state and role, plus the oldest pending card.
|
|
102
|
+
|
|
79
103
|
## How a claim works
|
|
80
104
|
|
|
81
105
|
1. the card CAS moves pending -> claimed in the task tree (one winner);
|
|
@@ -84,7 +108,36 @@ Exit codes: 0 done, 1 internal error, 2 refused (already claimed, wrong state, d
|
|
|
84
108
|
|
|
85
109
|
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
110
|
|
|
87
|
-
##
|
|
111
|
+
## When an agent dies
|
|
112
|
+
|
|
113
|
+
Every claim carries claimedUntil (the --ttl, 5 minutes by default). Once it passes, the
|
|
114
|
+
card is **reclaimable**: the next agent that claims it takes over as if the card were
|
|
115
|
+
pending, incrementing attempt. No operator, no stale lock:
|
|
116
|
+
|
|
117
|
+
~~~bash
|
|
118
|
+
$ hive-tasks claim slow-task-1a2b --agent agent-A --ttl 1s
|
|
119
|
+
$ # agent A dies here
|
|
120
|
+
$ sleep 2
|
|
121
|
+
$ hive-tasks claim slow-task-1a2b --agent agent-B # attempt 2, owner agent-B
|
|
122
|
+
~~~
|
|
123
|
+
|
|
124
|
+
A long but healthy run calls renew to keep its claim.
|
|
125
|
+
|
|
126
|
+
## Acceptance and freeze
|
|
127
|
+
|
|
128
|
+
A card may carry an acceptance command. It is **not** executed by default, because a card
|
|
129
|
+
is repository content and executing it would mean running code someone else pushed:
|
|
130
|
+
|
|
131
|
+
~~~bash
|
|
132
|
+
$ hive-tasks add --title "auth takes an object" --acceptance "npm test -- auth"
|
|
133
|
+
$ hive-tasks done auth-takes-an-object-dea2 --verify # runs it, records resultCommit
|
|
134
|
+
$ hive-tasks done auth-takes-an-object-dea2 # records resultCommit only
|
|
135
|
+
~~~
|
|
136
|
+
|
|
137
|
+
When hive mode is freeze, ready returns only cards tagged fix, so the hotfix can run while
|
|
138
|
+
the pipeline is red. Everything else waits.
|
|
139
|
+
|
|
140
|
+
## Works with
|
|
88
141
|
|
|
89
142
|
| Project | Question |
|
|
90
143
|
| --- | --- |
|
package/README.zh-CN.md
CHANGED
|
@@ -69,13 +69,35 @@ 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
|
-
|
|
|
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> | 挂起而不丢卡 |
|
|
76
78
|
|
|
77
79
|
退出码:0 成功,1 内部错误,2 被拒(已被认领、状态不对、依赖缺失、模式 freeze、路径忙)。
|
|
78
80
|
|
|
81
|
+
## 资源 key
|
|
82
|
+
|
|
83
|
+
scope 里通常放路径,但租约层并不关心内容:任何稳定字符串都行。共享资源用前缀表达:
|
|
84
|
+
|
|
85
|
+
~~~bash
|
|
86
|
+
hive-tasks add --title "起 dev server" --scope src/web --scope resource:port:3000
|
|
87
|
+
hive-tasks add --title "跑迁移" --scope resource:db:dev
|
|
88
|
+
~~~
|
|
89
|
+
|
|
90
|
+
两张卡写了同一个资源,就不可能同时被认领——和文件拿到的是同一种保证。不需要额外的东西:没有守护进程去盯端口。
|
|
91
|
+
|
|
92
|
+
## 统计
|
|
93
|
+
|
|
94
|
+
~~~bash
|
|
95
|
+
hive-tasks stats
|
|
96
|
+
hive-tasks stats --json
|
|
97
|
+
~~~
|
|
98
|
+
|
|
99
|
+
按状态和角色计数,外加最老的 pending 卡。
|
|
100
|
+
|
|
79
101
|
## claim 怎么走
|
|
80
102
|
|
|
81
103
|
1. 卡片 CAS 把 pending 推到 claimed(唯一赢家);
|
|
@@ -84,7 +106,32 @@ token-tests-ba66 pending tester token 测试
|
|
|
84
106
|
|
|
85
107
|
回滚是我们自己持有的另一次 CAS,所以输掉竞争不会泄漏租约。**卡是权威,租约是礼貌**——它只是让两个 agent 不去碰同一个文件。
|
|
86
108
|
|
|
87
|
-
##
|
|
109
|
+
## agent 中途死掉怎么办
|
|
110
|
+
|
|
111
|
+
每次认领都带 claimedUntil(--ttl,默认 5 分钟)。时间一过,这张卡就是**可回收的**:下一个来认领的 agent 直接接管,attempt +1,不需要人工介入:
|
|
112
|
+
|
|
113
|
+
~~~bash
|
|
114
|
+
$ hive-tasks claim slow-task-1a2b --agent agent-A --ttl 1s
|
|
115
|
+
$ # agent A 在这里崩溃
|
|
116
|
+
$ sleep 2
|
|
117
|
+
$ hive-tasks claim slow-task-1a2b --agent agent-B # attempt 2,owner agent-B
|
|
118
|
+
~~~
|
|
119
|
+
|
|
120
|
+
跑得久的健康任务用 renew 续期。
|
|
121
|
+
|
|
122
|
+
## 验收与冻结
|
|
123
|
+
|
|
124
|
+
卡片可以带一条验收命令。**默认不执行**——卡片是仓库内容,默认执行等于让别人 push 一段代码就能在你机器上跑:
|
|
125
|
+
|
|
126
|
+
~~~bash
|
|
127
|
+
$ hive-tasks add --title "auth 改成对象入参" --acceptance "npm test -- auth"
|
|
128
|
+
$ hive-tasks done auth-takes-an-object-dea2 --verify # 执行验收,记录 resultCommit
|
|
129
|
+
$ hive-tasks done auth-takes-an-object-dea2 # 只记录 resultCommit
|
|
130
|
+
~~~
|
|
131
|
+
|
|
132
|
+
hive 处于 freeze 模式时,ready 只返回带 fix 标签的卡,让热修能跑、其余等待。
|
|
133
|
+
|
|
134
|
+
## 和谁配合
|
|
88
135
|
|
|
89
136
|
| 项目 | 回答的问题 |
|
|
90
137
|
| --- | --- |
|
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",
|
|
@@ -73,12 +77,16 @@ git update-ref refs/hive/v1/tasks <new-tree> <old-tree>
|
|
|
73
77
|
| --- | --- |
|
|
74
78
|
| goal | what the worker should achieve; free text, no format |
|
|
75
79
|
| role | who may claim: a role name the team agrees on (coder, tester, reviewer, ...) |
|
|
76
|
-
| scope | paths the task
|
|
80
|
+
| scope | paths or resource keys the task expects to touch; used for hive leases. A resource key is any stable string such as resource:port:3000 |
|
|
77
81
|
| after | ids that must be done before this becomes ready |
|
|
78
82
|
| state | see below |
|
|
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
|
|
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
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]... [--
|
|
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
|
|
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
|
-
|
|
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 (
|
|
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", {
|
|
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)));
|
|
@@ -250,6 +334,24 @@ function run(argv) {
|
|
|
250
334
|
return printTask(readTask(repo, id), options.json);
|
|
251
335
|
}
|
|
252
336
|
|
|
337
|
+
if (command === "stats") {
|
|
338
|
+
const items = board(repo);
|
|
339
|
+
const byState = {};
|
|
340
|
+
const byRole = {};
|
|
341
|
+
let oldest = null;
|
|
342
|
+
for (const task of items) {
|
|
343
|
+
byState[task.state] = (byState[task.state] || 0) + 1;
|
|
344
|
+
byRole[task.role] = (byRole[task.role] || 0) + 1;
|
|
345
|
+
if (task.state === "pending" && (!oldest || task.createdAt < oldest.createdAt)) oldest = task;
|
|
346
|
+
}
|
|
347
|
+
const summary = { total: items.length, byState, byRole, oldestPending: oldest ? { id: oldest.id, createdAt: oldest.createdAt } : null };
|
|
348
|
+
if (options.json) return process.stdout.write(JSON.stringify(summary, null, 2) + "\n");
|
|
349
|
+
process.stdout.write("cards: " + summary.total + "\n");
|
|
350
|
+
for (const [state, count] of Object.entries(byState)) process.stdout.write(" " + state.padEnd(8) + count + "\n");
|
|
351
|
+
if (oldest) process.stdout.write("oldest pending: " + oldest.id + " (" + oldest.createdAt + ")\n");
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
|
|
253
355
|
fail("unknown command: " + command, 2);
|
|
254
356
|
}
|
|
255
357
|
|
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
|
|
47
|
-
export function
|
|
48
|
-
if (task.state !== "
|
|
49
|
-
if (!
|
|
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);
|