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/SPEC.md CHANGED
@@ -1,6 +1,6 @@
1
- # hive-tasks protocol (draft)
1
+ # hive-tasks protocol
2
2
 
3
- Status: draft, not implemented. Target: 1.0.0.
3
+ Status: implemented, 1.4.0. Protocol version v1 (card blobs keep `v: 1`).
4
4
 
5
5
  ## 1. Scope
6
6
 
@@ -10,18 +10,19 @@ defines:
10
10
  - where a task lives (a git ref, same namespace family as hive);
11
11
  - the task blob and its state machine;
12
12
  - how a task is claimed, finished and failed, with compare-and-swap;
13
+ - optional stage chains, so one card can move across roles without being released;
13
14
  - what "ready" means, and the CLI surface a dispatcher consumes.
14
15
 
15
- It does not start agents, split goals, run tests, or store patches.
16
+ It does not start agents, split goals, run tests, or store patches. It never starts a
17
+ process and never calls a model: role resolution returns a description a dispatcher can
18
+ use.
16
19
 
17
20
  ## 2. Namespace
18
21
 
19
22
  The whole board is one git tree, behind one ref:
20
23
 
21
- ```
22
- refs/hive/v1/tasks -> tree
23
- <id> -> blob (the task JSON)
24
- ```
24
+ refs/hive/v1/tasks -> tree
25
+ <id> -> blob (the task JSON)
25
26
 
26
27
  <id> is the tree entry name: a short slug, lowercase letters, digits and dashes, 4 to
27
28
  40 characters, generated from the title and a random suffix (for example
@@ -40,12 +41,10 @@ wins and the other retries. For a work queue that is the behavior we want.
40
41
 
41
42
  Implementation sketch (plumbing, no working tree involved):
42
43
 
43
- ```bash
44
- GIT_INDEX_FILE=tmp git read-tree refs/hive/v1/tasks # or an empty tree at first
45
- GIT_INDEX_FILE=tmp git update-index --add --cacheinfo 100644,<blob>,<id>
46
- GIT_INDEX_FILE=tmp git write-tree # new tree
47
- git update-ref refs/hive/v1/tasks <new-tree> <old-tree>
48
- ```
44
+ GIT_INDEX_FILE=tmp git read-tree refs/hive/v1/tasks # or an empty tree at first
45
+ GIT_INDEX_FILE=tmp git update-index --add --cacheinfo 100644,<blob>,<id>
46
+ GIT_INDEX_FILE=tmp git write-tree # new tree
47
+ git update-ref refs/hive/v1/tasks <new-tree> <old-tree>
49
48
 
50
49
  ## 3. Task blob
51
50
 
@@ -67,39 +66,117 @@ git update-ref refs/hive/v1/tasks <new-tree> <old-tree>
67
66
  "claimedUntil": null,
68
67
  "resultCommit": null,
69
68
  "verdict": null,
69
+ "reason": null,
70
+ "reviewOf": null,
70
71
  "createdBy": "queen",
71
72
  "createdAt": "2026-12-03T15:00:00.000Z",
72
- "updatedAt": "2026-12-03T15:00:00.000Z"
73
+ "updatedAt": "2026-12-03T15:00:00.000Z",
74
+
75
+ "stages": [
76
+ { "key": "code", "role": "coder", "acceptance": "npm test", "state": "done" },
77
+ { "key": "review", "role": "reviewer", "onFail": "code", "state": "pending" }
78
+ ],
79
+ "stage": "review",
80
+ "stageState": "pending"
73
81
  }
74
82
  ```
75
83
 
76
84
  | Field | Meaning |
77
85
  | --- | --- |
78
86
  | goal | what the worker should achieve; free text, no format |
79
- | role | who may claim: a role name the team agrees on (coder, tester, reviewer, ...) |
87
+ | role | who may claim: a role name the team agrees on (coder, tester, reviewer, ...). For a staged card it is the fallback/display role; routing uses the stage role |
80
88
  | 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 |
81
89
  | after | ids that must be done before this becomes ready |
82
90
  | state | see below |
83
91
  | owner | agent id while claimed |
84
- | attempt | incremented on every claim; used to stop retry loops |
85
- | verdict | pass or fail, copied from the hive verdict when done |
92
+ | attempt | incremented on every claim and every reopen, and on an onFail rewind; used to stop retry loops |
93
+ | verdict | pass or fail, copied from the hive verdict when the card itself is done |
94
+ | reason | why the card is in its current state: the text passed to fail, block or reopen, or "test failed: ..." after a review reflow |
95
+ | reviewOf | id of the card this review card tests; the id is also added to after, so the review is ready only when its upstream is done |
86
96
  | tags | free labels; "fix" is special (see mode) |
87
- | acceptance | optional shell command; only run by done --verify |
97
+ | acceptance | optional shell command; used by done --verify when the card has no stages |
88
98
  | claimedUntil | expiry of the current claim; a claimed card past it is reclaimable |
89
- | resultCommit | the commit recorded when the card was done |
99
+ | resultCommit | the commit recorded when the card or its current stage was finished |
100
+ | stages | optional ordered stage chain. Absent or [] means a card without stages |
101
+ | stage | current stage key, or null for a card without stages |
102
+ | stageState | state of the current stage: pending, claimed, done, failed, blocked; null for a card without stages |
103
+
104
+ Fields at the end are only stored when the card has stages. A card created without
105
+ `--stage`/`--stages` has exactly the 1.3.0 blob: `stages`, `stage` and
106
+ `stageState` are not written, and the JSON view reports `stages: []`, `stage: null`,
107
+ `stageState: null`. That is the "single implicit stage": every command behaves as before.
108
+
109
+ ### 3.1 Stage object
110
+
111
+ ```yaml
112
+ - key: code # unique inside the card, ^[a-z][a-z0-9-]{0,31}$
113
+ role: coder # any string, not enumerated
114
+ acceptance: "npm test" # optional, used by done --verify at this stage
115
+ onFail: code # optional, the key to rewind to when this stage fails
116
+ state: pending # runtime: pending | claimed | done | failed | blocked
117
+ ```
118
+
119
+ | Field | Meaning |
120
+ | --- | --- |
121
+ | key | unique and stable inside the card; the stage identifier |
122
+ | role | free-form role name; resolved to an adapter/model/prompt by the dispatcher (section 7) |
123
+ | acceptance | optional command; done --verify runs the current stage's command before it advances |
124
+ | onFail | optional key of an earlier stage. When this stage fails or is blocked and that target is done, the chain rewinds to it instead of failing the card |
125
+ | state | runtime stage state, maintained by every transition |
126
+
127
+ Stage state and card state move together for the current stage: a claimed stage means
128
+ card state claimed, a failed stage without a rewind means card state failed, and so on.
129
+ Once a non-final stage is done the card returns to pending while the next stage opens.
90
130
 
91
131
  ## 4. State machine
92
132
 
93
133
  ```
94
- (dependencies done)
95
- pending ------------------------> ready
96
- ^ | claim
97
- | retry v
98
- failed <--------- claimed ----> done
99
- ^ |
100
- +--- blocked <---+ (timeout, conflict, manual)
134
+ claim
135
+ pending ─────────────────────▶ claimed
136
+ ▲ │ │ │
137
+ │ │ │ └── block ─▶ blocked
138
+ │ │ └──── fail ──▶ failed
139
+ │ └───── done ───▶ done
140
+ │
141
+ ├── retry (failed, blocked)
142
+ ├── unblock (blocked)
143
+ └── reopen (every stored state)
101
144
  ```
102
145
 
146
+ `reopen` is the universal escape hatch: no stored state is a one-way door. It moves the
147
+ card back to pending, clears owner, claimedUntil, resultCommit and verdict, keeps attempt
148
+ and increments it, records reason (default "reopened") and writes a reopen event.
149
+ Reopening a pending card is a no-op (exit 0) and does not change attempt. Reopening a
150
+ claimed card releases its hive leases, so no orphan lease is left behind. On a staged
151
+ card reopen always resets the chain (default first stage, --stage KEY selects another),
152
+ including a card that is currently pending.
153
+
154
+ `rm` is the only destructive command: it removes the card's blob from the task tree in
155
+ one CAS. pending, failed, blocked and done cards are deletable. A claimed card is refused
156
+ (exit 2, with a hint to release or reopen it) unless --force is given; --force releases
157
+ exactly that claim's hive leases and then deletes the card, so a forced delete cannot
158
+ strand a lease. Deleting a card is not idempotent: an unknown id is exit 2, and a second
159
+ `rm` of the same id reports "no such task". Cards that named the deleted id in `after`
160
+ now have a missing dependency, which the ready computation reports as a data error.
161
+
162
+ | From | Command | To | Effect |
163
+ | --- | --- | --- | --- |
164
+ | pending | claim | claimed | CAS winner; attempt + 1 |
165
+ | claimed | done | done | releases leases; records resultCommit and verdict |
166
+ | claimed | fail | failed | releases leases; attempt kept |
167
+ | claimed | block | blocked | releases leases |
168
+ | claimed | reopen | pending | releases leases; attempt + 1 |
169
+ | done | reopen | pending | attempt + 1; clears resultCommit and verdict |
170
+ | failed | retry | pending | attempt kept |
171
+ | failed | reopen | pending | attempt + 1 |
172
+ | blocked | unblock | pending | attempt kept |
173
+ | blocked | retry | pending | attempt kept |
174
+ | blocked | reopen | pending | attempt + 1 |
175
+ | pending | reopen | pending | no-op (exit 0) |
176
+ | any stored state | block | blocked | block accepts any state |
177
+ | pending/failed/blocked/done | rm | deleted | removes the blob in one CAS |
178
+ | claimed | rm --force | deleted | releases the claim's leases, then removes the blob |
179
+
103
180
  - pending: exists, dependencies not satisfied (or no capacity to run it).
104
181
  - ready: pending with every id in after in state done. Computed, never stored.
105
182
  - claimed: a dispatcher claimed it; owner and attempt are set.
@@ -117,14 +194,84 @@ Every transition is a compare-and-swap on the task ref: read the blob, verify th
117
194
  current state, write the new blob with the ref's current sha as the expected value.
118
195
  Two claimants therefore cannot both win.
119
196
 
197
+ ### 4.1 Review cards (review-of)
198
+
199
+ A review card names the card it tests:
200
+
201
+ hive-tasks add --title "review auth" --role reviewer --review-of <upstream-id>
202
+
203
+ The CLI stores reviewOf and adds the upstream id to after when it is not there yet, so
204
+ the review card becomes ready only once the upstream is done.
205
+
206
+ When the review card fails:
207
+
208
+ 1. if the upstream exists and is done, it is reopened (state pending, attempt + 1,
209
+ reason "test failed: <reason summary>", claimedUntil/resultCommit/verdict cleared) in
210
+ the same tree write that moves the review card to failed;
211
+ 2. the review card itself becomes failed and can be retried;
212
+ 3. the events record task_fail, reopen (via review_failed) and review_failed.
213
+
214
+ When the review card is done the upstream is not touched: passing the review settles it.
215
+ If the upstream is missing or not done, the review still fails and no upstream is
216
+ reopened; review_failed records reopened: false. If a staged review stage rewinds through
217
+ onFail the card does not fail, so the upstream is not reopened.
218
+
219
+ ### 4.2 Stage chains
220
+
221
+ A stage chain turns one card into an ordered pipeline, each stage owned by a role. This
222
+ is how a single card moves coder -> tester -> reviewer without a new card per role.
223
+
224
+ Rules:
225
+
226
+ 1. **Current stage only.** claim accepts the card only when its current stage is pending
227
+ (or when a claim expired, in which case the stage is reclaimed like any claim).
228
+ done and fail accept only a claimed current stage.
229
+ 2. **Advance, do not release.** done marks the current stage done, opens the next stage
230
+ (pending, owner cleared, leases released) and keeps the card pending. The last stage
231
+ finishes the card (state done, stageState done). done returns `nextStage` (null when
232
+ the chain is complete).
233
+ 3. **Fail stops or rewinds.** fail/block mark the current stage failed/blocked. If the
234
+ stage has onFail and that target stage is done, the chain rewinds: the target and all
235
+ later stages become pending, the current stage stays failed/blocked as history, the
236
+ card becomes pending and ready again, and attempt is incremented. Without a usable
237
+ onFail target the whole card becomes failed/blocked, exactly as before.
238
+ 4. **Retry and reopen reset stages.** retry resets the failed/blocked stage (or the
239
+ --stage target) to pending and keeps earlier done stages; reopen resets to the target
240
+ (default the first stage) and increments attempt.
241
+ 5. **Acceptance is per stage.** done --verify runs the current stage acceptance, falling
242
+ back to the card acceptance when the stage has none.
243
+ 6. **Legacy cards are unchanged.** Without stages there is a single implicit stage and
244
+ every rule in section 4 applies verbatim.
245
+
246
+ | Card / stageState | Command | Result | Effect |
247
+ | --- | --- | --- | --- |
248
+ | pending / pending | claim | claimed / claimed | owner, leases, attempt + 1; stage state claimed |
249
+ | claimed / claimed | done (more stages) | pending / pending at next | stage done, next pending, owner cleared, leases released; nextStage returned |
250
+ | claimed / claimed | done (last stage) | done / done | stage done; card done; resultCommit/verdict recorded |
251
+ | claimed / claimed | fail, onFail target done | pending / pending at target | stage failed; target..end pending; attempt + 1; card ready again |
252
+ | claimed / claimed | fail, no usable onFail | failed / failed | whole card failed; stage failed |
253
+ | claimed / claimed | block, onFail target done | pending / pending at target | stage blocked; target..end pending; attempt + 1 |
254
+ | claimed / claimed | block, no usable onFail | blocked / blocked | whole card blocked |
255
+ | failed / failed | retry [--stage KEY] | pending / pending at KEY | KEY..end reset to pending, attempt kept |
256
+ | blocked / blocked | unblock | pending / pending at current | current stage pending, attempt kept |
257
+ | blocked / blocked | retry [--stage KEY] | pending / pending at KEY | KEY..end reset to pending, attempt kept |
258
+ | any / any | reopen [--stage KEY] | pending / pending at KEY | KEY..end reset (default first), attempt + 1, leases released |
259
+ | any / any | block | blocked / blocked or rewind | block accepts any state |
260
+
120
261
  ## 5. Ready computation
121
262
 
122
263
  A task is ready when:
123
264
 
124
- 1. state is pending;
265
+ 1. state is pending (or a claim expired and is reclaimable);
125
266
  2. every id in after exists and is done;
126
267
  3. hive mode is free, or the card is tagged "fix" (the fix must be able to run while the
127
- pipeline is frozen).
268
+ pipeline is frozen);
269
+ 4. for a staged card: stageState is pending, or claimed with an expired claim so it can
270
+ be recovered;
271
+ 5. when a role filter is given, the role that works the card now matches it: the current
272
+ stage role for a staged card, the card role otherwise. Without a role filter every
273
+ ready card is visible; ready/list JSON adds `readyRole` and the text output prints it,
274
+ so a dispatcher can route without guessing.
128
275
 
129
276
  A missing dependency id is a data error: report it, do not treat it as satisfied.
130
277
 
@@ -135,22 +282,28 @@ see the same order; the claim CAS still gives exactly one winner per card.
135
282
 
136
283
  ## 6. CLI
137
284
 
138
- ```
139
- hive-tasks add --title T --role coder --scope src/auth.ts [--scope ...] [--after ID]... [--goal G]
140
- hive-tasks list [--json] [--state S] [--role R]
141
- hive-tasks ready [--json] [--limit N] [--role R]
142
- hive-tasks next [--role R] [--json] # the single best candidate, or null
143
- hive-tasks show <id> [--json]
144
- hive-tasks claim <id> --agent A [--ttl 5m]
145
- hive-tasks done <id> [--sha S] [--verdict pass|fail]
146
- hive-tasks fail <id> --reason R
147
- hive-tasks retry <id>
148
- hive-tasks block <id> --reason R
149
- hive-tasks unblock <id>
150
- ```
285
+ hive-tasks add --title T --role coder --scope src/auth.ts [--scope ...] [--after ID]... [--goal G] [--review-of ID]
286
+ [--stage ROLE[:ACCEPTANCE]]... [--stages FILE]
287
+ hive-tasks list [--json] [--state S] [--role R]
288
+ hive-tasks ready [--json] [--limit N] [--role R]
289
+ hive-tasks next [--role R] [--json] # the single best candidate, or null
290
+ hive-tasks show <id> [--json] # card plus resolved role source
291
+ hive-tasks claim <id> --agent A [--ttl 5m]
292
+ hive-tasks done <id> [--sha S] [--verdict pass|fail] [--verify]
293
+ hive-tasks fail <id> --reason R
294
+ hive-tasks retry <id> [--stage KEY]
295
+ hive-tasks block <id> --reason R
296
+ hive-tasks unblock <id>
297
+ hive-tasks reopen <id> [--reason R] [--stage KEY]
298
+ hive-tasks rm <id> [--force] [--json]
299
+
300
+ `--stage ROLE[:ACCEPTANCE]` is repeatable: each flag adds one stage in order, and the
301
+ stage key is derived from the role (duplicates become role-2, role-3...). `--stages
302
+ FILE` takes a JSON array of stage objects and is the way to set explicit keys, onFail and
303
+ per-stage acceptance in one shot. The two flags are mutually exclusive.
151
304
 
152
305
  Exit codes: 0 success; 1 internal error; 2 refused (already claimed, wrong state,
153
- dependency missing, mode freeze).
306
+ dependency missing, mode freeze, invalid stage chain).
154
307
 
155
308
  claim couples the card with the hive leases, card first:
156
309
 
@@ -160,13 +313,53 @@ claim couples the card with the hive leases, card first:
160
313
 
161
314
  Card-first is deliberate: a lost card race costs nothing, while lease-first would leave
162
315
  held leases behind when the card CAS loses. Reverting the card is another CAS we own, so
163
- no state leaks.
316
+ no state leaks. A staged card reverts its stageState to pending together with the card.
317
+
318
+ done releases the leases and records the verdict (or opens the next stage). fail releases
319
+ the leases and keeps the attempt counter. retry accepts failed or blocked and returns the
320
+ card to pending (or resets the chain to a stage). block accepts any state; unblock
321
+ accepts blocked. reopen accepts every stored state, returns the card to pending and
322
+ releases a claimed card's leases (section 4). add --review-of links a review card to its
323
+ upstream (section 4.1). rm deletes pending, failed, blocked and done cards, and refuses a
324
+ claimed card (exit 2) unless --force, which releases the claim's leases first; it prints
325
+ the deleted card (with `deleted: true`) under --json.
164
326
 
165
- done releases the leases and records the verdict. fail releases the leases and keeps the
166
- attempt counter. retry accepts failed or blocked and returns the card to pending. block
167
- accepts any state; unblock accepts blocked.
327
+ ## 7. Role resolution (dispatchers)
168
328
 
169
- ## 7. Events
329
+ A dispatcher needs an adapter, a model and a prompt for the role that works a card.
330
+ Roles are free strings, so resolving one must never fail. `resolveRole(repo, name,
331
+ options)` in src/role.js returns a plain object with a stable shape:
332
+
333
+ ```js
334
+ {
335
+ name: "coder", // the resolved role name ("" becomes "worker")
336
+ source: "repo", // "repo" | "default" | "synthetic"
337
+ adapter: "dsh", // default "dsh"
338
+ model: null, // default null
339
+ prompt: "…", // role contract text, "" when there is none
340
+ args: [], // extra adapter arguments
341
+ stage: null // optional stage key, used by synthetic prompts
342
+ }
343
+ ```
344
+
345
+ Resolution is a three-tier fallback:
346
+
347
+ 1. **repo** - `.hive/roles.yaml` (the same file hive-runner reads) has an entry for the
348
+ name. `adapter`, `model`, `args` and a `prompt` path are taken from it; the prompt
349
+ file is read to text (a missing file falls back to the default prompt path).
350
+ 2. **default** - the name is one of the built-in roles (coder, tester, reviewer,
351
+ researcher, planner, worker, queen): adapter `dsh`, model `null`, prompt from
352
+ `.hive/roles/<name>.md` when that file exists, otherwise empty.
353
+ 3. **synthetic** - any other name gets a generated generic contract that mentions the
354
+ name and, when the dispatcher passes one, the stage key. Nothing is refused.
355
+
356
+ The function never throws: a missing or malformed roles.yaml, an unreadable prompt file,
357
+ a relative path outside the repository or an unknown name all fall through to the next
358
+ tier. It only reads files under the repository top and does not mutate the input.
359
+ `hive-tasks show` prints the resolved source and, with --json, the whole object as
360
+ `roleResolved` (plus `roleSource`).
361
+
362
+ ## 8. Events
170
363
 
171
364
  Each command appends one line to .hive/events.jsonl:
172
365
 
@@ -174,14 +367,29 @@ Each command appends one line to .hive/events.jsonl:
174
367
  { "ts": "...", "actor": "worker-3", "kind": "task_claim", "task": "auth-token-a1b2", "attempt": 1 }
175
368
  ```
176
369
 
177
- kinds: task_add, task_claim, task_done, task_fail, task_retry, task_block, task_unblock.
370
+ kinds: task_add, task_claim, task_done, task_fail, task_retry, task_block,
371
+ task_unblock, task_renew, task_plan, task_delete, reopen, review_failed, stage_done,
372
+ stage_failed, stage_advanced, stage_rewound.
373
+
374
+ `task_delete` records the removed card: it carries both `task` and `id` (the card id),
375
+ `state` (the state the card had when it was deleted) and `actor`.
376
+
377
+ A review card that fails appends two extra events: reopen for the upstream card it sends
378
+ back to pending, and review_failed for the review itself (with reviewOf and reopened).
379
+
380
+ Stage events are additional lines next to the task event:
381
+
382
+ - stage_done: a stage finished (task done or advanced); carries stage.
383
+ - stage_advanced: the card opened the next stage; carries from and stage.
384
+ - stage_failed: the current stage failed; carries stage and state.
385
+ - stage_rewound: an onFail target took the card back; carries from and stage (the target).
178
386
 
179
- ## 8. Requirements
387
+ ## 9. Requirements
180
388
 
181
389
  - hive-consensus >= 1.2.0 (leases, board, mode, verdict, events).
182
390
  - git >= 2.40.
183
391
  - No network, no database, no daemon.
184
392
 
185
- ## 9. Open questions
393
+ ## 10. Open questions
186
394
 
187
395
  See docs/RFC-0001-task-cards.md section "Open questions".
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "hive-tasks",
3
- "version": "1.2.0",
4
- "description": "Task cards for agent teams: one git tree behind one ref, with a state machine, dependencies and hive leases.",
3
+ "version": "1.5.0",
4
+ "description": "Task cards for agent teams: one git tree behind one ref, with a state machine, optional stage chains, role resolution, dependencies and hive leases.",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "hive-tasks": "bin/hive-tasks.js"
@@ -27,6 +27,7 @@
27
27
  "agents",
28
28
  "tasks",
29
29
  "queue",
30
+ "stages",
30
31
  "coordination",
31
32
  "multi-agent",
32
33
  "ai-agents",
@@ -42,4 +43,4 @@
42
43
  "url": "https://github.com/d86e/hive-tasks/issues"
43
44
  },
44
45
  "homepage": "https://github.com/d86e/hive-tasks#readme"
45
- }
46
+ }