hive-tasks 1.1.0 → 1.4.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,107 @@ 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, ...) |
80
- | scope | paths the task is expected to touch; used to claim hive leases |
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 |
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
+ | From | Command | To | Effect |
155
+ | --- | --- | --- | --- |
156
+ | pending | claim | claimed | CAS winner; attempt + 1 |
157
+ | claimed | done | done | releases leases; records resultCommit and verdict |
158
+ | claimed | fail | failed | releases leases; attempt kept |
159
+ | claimed | block | blocked | releases leases |
160
+ | claimed | reopen | pending | releases leases; attempt + 1 |
161
+ | done | reopen | pending | attempt + 1; clears resultCommit and verdict |
162
+ | failed | retry | pending | attempt kept |
163
+ | failed | reopen | pending | attempt + 1 |
164
+ | blocked | unblock | pending | attempt kept |
165
+ | blocked | retry | pending | attempt kept |
166
+ | blocked | reopen | pending | attempt + 1 |
167
+ | pending | reopen | pending | no-op (exit 0) |
168
+ | any stored state | block | blocked | block accepts any state |
169
+
103
170
  - pending: exists, dependencies not satisfied (or no capacity to run it).
104
171
  - ready: pending with every id in after in state done. Computed, never stored.
105
172
  - claimed: a dispatcher claimed it; owner and attempt are set.
@@ -117,14 +184,84 @@ Every transition is a compare-and-swap on the task ref: read the blob, verify th
117
184
  current state, write the new blob with the ref's current sha as the expected value.
118
185
  Two claimants therefore cannot both win.
119
186
 
187
+ ### 4.1 Review cards (review-of)
188
+
189
+ A review card names the card it tests:
190
+
191
+ hive-tasks add --title "review auth" --role reviewer --review-of <upstream-id>
192
+
193
+ The CLI stores reviewOf and adds the upstream id to after when it is not there yet, so
194
+ the review card becomes ready only once the upstream is done.
195
+
196
+ When the review card fails:
197
+
198
+ 1. if the upstream exists and is done, it is reopened (state pending, attempt + 1,
199
+ reason "test failed: <reason summary>", claimedUntil/resultCommit/verdict cleared) in
200
+ the same tree write that moves the review card to failed;
201
+ 2. the review card itself becomes failed and can be retried;
202
+ 3. the events record task_fail, reopen (via review_failed) and review_failed.
203
+
204
+ When the review card is done the upstream is not touched: passing the review settles it.
205
+ If the upstream is missing or not done, the review still fails and no upstream is
206
+ reopened; review_failed records reopened: false. If a staged review stage rewinds through
207
+ onFail the card does not fail, so the upstream is not reopened.
208
+
209
+ ### 4.2 Stage chains
210
+
211
+ A stage chain turns one card into an ordered pipeline, each stage owned by a role. This
212
+ is how a single card moves coder -> tester -> reviewer without a new card per role.
213
+
214
+ Rules:
215
+
216
+ 1. **Current stage only.** claim accepts the card only when its current stage is pending
217
+ (or when a claim expired, in which case the stage is reclaimed like any claim).
218
+ done and fail accept only a claimed current stage.
219
+ 2. **Advance, do not release.** done marks the current stage done, opens the next stage
220
+ (pending, owner cleared, leases released) and keeps the card pending. The last stage
221
+ finishes the card (state done, stageState done). done returns `nextStage` (null when
222
+ the chain is complete).
223
+ 3. **Fail stops or rewinds.** fail/block mark the current stage failed/blocked. If the
224
+ stage has onFail and that target stage is done, the chain rewinds: the target and all
225
+ later stages become pending, the current stage stays failed/blocked as history, the
226
+ card becomes pending and ready again, and attempt is incremented. Without a usable
227
+ onFail target the whole card becomes failed/blocked, exactly as before.
228
+ 4. **Retry and reopen reset stages.** retry resets the failed/blocked stage (or the
229
+ --stage target) to pending and keeps earlier done stages; reopen resets to the target
230
+ (default the first stage) and increments attempt.
231
+ 5. **Acceptance is per stage.** done --verify runs the current stage acceptance, falling
232
+ back to the card acceptance when the stage has none.
233
+ 6. **Legacy cards are unchanged.** Without stages there is a single implicit stage and
234
+ every rule in section 4 applies verbatim.
235
+
236
+ | Card / stageState | Command | Result | Effect |
237
+ | --- | --- | --- | --- |
238
+ | pending / pending | claim | claimed / claimed | owner, leases, attempt + 1; stage state claimed |
239
+ | claimed / claimed | done (more stages) | pending / pending at next | stage done, next pending, owner cleared, leases released; nextStage returned |
240
+ | claimed / claimed | done (last stage) | done / done | stage done; card done; resultCommit/verdict recorded |
241
+ | claimed / claimed | fail, onFail target done | pending / pending at target | stage failed; target..end pending; attempt + 1; card ready again |
242
+ | claimed / claimed | fail, no usable onFail | failed / failed | whole card failed; stage failed |
243
+ | claimed / claimed | block, onFail target done | pending / pending at target | stage blocked; target..end pending; attempt + 1 |
244
+ | claimed / claimed | block, no usable onFail | blocked / blocked | whole card blocked |
245
+ | failed / failed | retry [--stage KEY] | pending / pending at KEY | KEY..end reset to pending, attempt kept |
246
+ | blocked / blocked | unblock | pending / pending at current | current stage pending, attempt kept |
247
+ | blocked / blocked | retry [--stage KEY] | pending / pending at KEY | KEY..end reset to pending, attempt kept |
248
+ | any / any | reopen [--stage KEY] | pending / pending at KEY | KEY..end reset (default first), attempt + 1, leases released |
249
+ | any / any | block | blocked / blocked or rewind | block accepts any state |
250
+
120
251
  ## 5. Ready computation
121
252
 
122
253
  A task is ready when:
123
254
 
124
- 1. state is pending;
255
+ 1. state is pending (or a claim expired and is reclaimable);
125
256
  2. every id in after exists and is done;
126
257
  3. hive mode is free, or the card is tagged "fix" (the fix must be able to run while the
127
- pipeline is frozen).
258
+ pipeline is frozen);
259
+ 4. for a staged card: stageState is pending, or claimed with an expired claim so it can
260
+ be recovered;
261
+ 5. when a role filter is given, the role that works the card now matches it: the current
262
+ stage role for a staged card, the card role otherwise. Without a role filter every
263
+ ready card is visible; ready/list JSON adds `readyRole` and the text output prints it,
264
+ so a dispatcher can route without guessing.
128
265
 
129
266
  A missing dependency id is a data error: report it, do not treat it as satisfied.
130
267
 
@@ -135,22 +272,27 @@ see the same order; the claim CAS still gives exactly one winner per card.
135
272
 
136
273
  ## 6. CLI
137
274
 
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
- ```
275
+ hive-tasks add --title T --role coder --scope src/auth.ts [--scope ...] [--after ID]... [--goal G] [--review-of ID]
276
+ [--stage ROLE[:ACCEPTANCE]]... [--stages FILE]
277
+ hive-tasks list [--json] [--state S] [--role R]
278
+ hive-tasks ready [--json] [--limit N] [--role R]
279
+ hive-tasks next [--role R] [--json] # the single best candidate, or null
280
+ hive-tasks show <id> [--json] # card plus resolved role source
281
+ hive-tasks claim <id> --agent A [--ttl 5m]
282
+ hive-tasks done <id> [--sha S] [--verdict pass|fail] [--verify]
283
+ hive-tasks fail <id> --reason R
284
+ hive-tasks retry <id> [--stage KEY]
285
+ hive-tasks block <id> --reason R
286
+ hive-tasks unblock <id>
287
+ hive-tasks reopen <id> [--reason R] [--stage KEY]
288
+
289
+ `--stage ROLE[:ACCEPTANCE]` is repeatable: each flag adds one stage in order, and the
290
+ stage key is derived from the role (duplicates become role-2, role-3...). `--stages
291
+ FILE` takes a JSON array of stage objects and is the way to set explicit keys, onFail and
292
+ per-stage acceptance in one shot. The two flags are mutually exclusive.
151
293
 
152
294
  Exit codes: 0 success; 1 internal error; 2 refused (already claimed, wrong state,
153
- dependency missing, mode freeze).
295
+ dependency missing, mode freeze, invalid stage chain).
154
296
 
155
297
  claim couples the card with the hive leases, card first:
156
298
 
@@ -160,13 +302,51 @@ claim couples the card with the hive leases, card first:
160
302
 
161
303
  Card-first is deliberate: a lost card race costs nothing, while lease-first would leave
162
304
  held leases behind when the card CAS loses. Reverting the card is another CAS we own, so
163
- no state leaks.
305
+ no state leaks. A staged card reverts its stageState to pending together with the card.
164
306
 
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.
307
+ done releases the leases and records the verdict (or opens the next stage). fail releases
308
+ the leases and keeps the attempt counter. retry accepts failed or blocked and returns the
309
+ card to pending (or resets the chain to a stage). block accepts any state; unblock
310
+ accepts blocked. reopen accepts every stored state, returns the card to pending and
311
+ releases a claimed card's leases (section 4). add --review-of links a review card to its
312
+ upstream (section 4.1).
168
313
 
169
- ## 7. Events
314
+ ## 7. Role resolution (dispatchers)
315
+
316
+ A dispatcher needs an adapter, a model and a prompt for the role that works a card.
317
+ Roles are free strings, so resolving one must never fail. `resolveRole(repo, name,
318
+ options)` in src/role.js returns a plain object with a stable shape:
319
+
320
+ ```js
321
+ {
322
+ name: "coder", // the resolved role name ("" becomes "worker")
323
+ source: "repo", // "repo" | "default" | "synthetic"
324
+ adapter: "dsh", // default "dsh"
325
+ model: null, // default null
326
+ prompt: "…", // role contract text, "" when there is none
327
+ args: [], // extra adapter arguments
328
+ stage: null // optional stage key, used by synthetic prompts
329
+ }
330
+ ```
331
+
332
+ Resolution is a three-tier fallback:
333
+
334
+ 1. **repo** - `.hive/roles.yaml` (the same file hive-runner reads) has an entry for the
335
+ name. `adapter`, `model`, `args` and a `prompt` path are taken from it; the prompt
336
+ file is read to text (a missing file falls back to the default prompt path).
337
+ 2. **default** - the name is one of the built-in roles (coder, tester, reviewer,
338
+ researcher, planner, worker, queen): adapter `dsh`, model `null`, prompt from
339
+ `.hive/roles/<name>.md` when that file exists, otherwise empty.
340
+ 3. **synthetic** - any other name gets a generated generic contract that mentions the
341
+ name and, when the dispatcher passes one, the stage key. Nothing is refused.
342
+
343
+ The function never throws: a missing or malformed roles.yaml, an unreadable prompt file,
344
+ a relative path outside the repository or an unknown name all fall through to the next
345
+ tier. It only reads files under the repository top and does not mutate the input.
346
+ `hive-tasks show` prints the resolved source and, with --json, the whole object as
347
+ `roleResolved` (plus `roleSource`).
348
+
349
+ ## 8. Events
170
350
 
171
351
  Each command appends one line to .hive/events.jsonl:
172
352
 
@@ -174,14 +354,26 @@ Each command appends one line to .hive/events.jsonl:
174
354
  { "ts": "...", "actor": "worker-3", "kind": "task_claim", "task": "auth-token-a1b2", "attempt": 1 }
175
355
  ```
176
356
 
177
- kinds: task_add, task_claim, task_done, task_fail, task_retry, task_block, task_unblock.
357
+ kinds: task_add, task_claim, task_done, task_fail, task_retry, task_block,
358
+ task_unblock, task_renew, task_plan, reopen, review_failed, stage_done, stage_failed,
359
+ stage_advanced, stage_rewound.
360
+
361
+ A review card that fails appends two extra events: reopen for the upstream card it sends
362
+ back to pending, and review_failed for the review itself (with reviewOf and reopened).
363
+
364
+ Stage events are additional lines next to the task event:
365
+
366
+ - stage_done: a stage finished (task done or advanced); carries stage.
367
+ - stage_advanced: the card opened the next stage; carries from and stage.
368
+ - stage_failed: the current stage failed; carries stage and state.
369
+ - stage_rewound: an onFail target took the card back; carries from and stage (the target).
178
370
 
179
- ## 8. Requirements
371
+ ## 9. Requirements
180
372
 
181
373
  - hive-consensus >= 1.2.0 (leases, board, mode, verdict, events).
182
374
  - git >= 2.40.
183
375
  - No network, no database, no daemon.
184
376
 
185
- ## 9. Open questions
377
+ ## 10. Open questions
186
378
 
187
379
  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.1.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.4.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
+ }