hive-tasks 1.9.0 → 1.10.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 +28 -2
- package/README.md +36 -2
- package/README.zh-CN.md +32 -2
- package/SPEC.md +25 -1
- package/package.json +1 -1
- package/src/cli.js +69 -35
- package/src/git.js +19 -0
- package/src/store.js +225 -21
package/CHANGELOG.md
CHANGED
|
@@ -4,7 +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
|
-
##
|
|
7
|
+
## [1.10.0] - 2026-12-06
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- `HIVE_EVENT_ACTOR` decouples event `actor` from the task owner/claim identity; the
|
|
12
|
+
runner sets it to `worker:<role>` for worker CLI calls while the claim owner stays
|
|
13
|
+
the runner identity.
|
|
14
|
+
- `list --json --state S --role R --limit N --cursor C` returns one server-side page,
|
|
15
|
+
`{ items, total, cursor, hasMore }`, ordered by stateChangedAt descending (ties by
|
|
16
|
+
id); only the page's card blobs are read and cursor is the numeric offset returned
|
|
17
|
+
by the previous page.
|
|
18
|
+
- `counts --json` returns `{ total, byState, superseded, oldestPending }` from a
|
|
19
|
+
lightweight index; `stats` is index-backed too and keeps its shape.
|
|
20
|
+
- `show <id> --json` reads exactly one card blob.
|
|
21
|
+
- Derived `refs/hive/v1/tasks-index` cache, refreshed after every board write and
|
|
22
|
+
rebuilt once when missing or stale.
|
|
23
|
+
|
|
24
|
+
### Tests
|
|
25
|
+
|
|
26
|
+
- 68 node:test scenarios: the 62 from 1.9.0 unchanged, plus six in
|
|
27
|
+
test/pagination.test.mjs covering page walking against the filtered full list,
|
|
28
|
+
boundaries, counts versus stats, index rebuild after a missing/stale index, pages and
|
|
29
|
+
counts being index-only while show reads one blob, and a 100k-entry mock index.
|
|
8
30
|
|
|
9
31
|
### Changed
|
|
10
32
|
|
|
@@ -55,6 +77,10 @@ All notable changes to this project are documented here. The format follows
|
|
|
55
77
|
|
|
56
78
|
- Not published to npm in this change.
|
|
57
79
|
|
|
80
|
+
### Published
|
|
81
|
+
|
|
82
|
+
- npm: `hive-tasks@1.9.0` on 2026-12-04.
|
|
83
|
+
|
|
58
84
|
## [1.8.1] - 2026-12-04
|
|
59
85
|
|
|
60
86
|
### Added
|
|
@@ -316,4 +342,4 @@ All notable changes to this project are documented here. The format follows
|
|
|
316
342
|
[1.4.0]: https://github.com/d86e/hive-tasks/releases/tag/v1.4.0
|
|
317
343
|
[1.3.0]: https://github.com/d86e/hive-tasks/releases/tag/v1.3.0
|
|
318
344
|
[1.1.0]: https://github.com/d86e/hive-tasks/releases/tag/v1.1.0
|
|
319
|
-
[1.0.0]: https://github.com/d86e/hive-tasks/releases/tag/v1.0.0
|
|
345
|
+
[1.0.0]: https://github.com/d86e/hive-tasks/releases/tag/v1.0.0
|
package/README.md
CHANGED
|
@@ -78,7 +78,7 @@ token-tests-ba66 pending tester token tests
|
|
|
78
78
|
| edit <id> [--title T] [--scope-add P] [--scope-remove P] [--acceptance CMD] [--priority N] [--after-set ID,ID] [--tag +T/-T] [--role R] [--superseded\|--no-superseded] | change only the fields you pass, on a pending or failed task; an empty --acceptance is refused and --acceptance - clears it on a draft; a scope edit syncs the task's announcements |
|
|
79
79
|
| split <id> --into '<json array>' [--keep] [--reason R] [--json] | replace a task with finer cards; the parent is marked superseded unless --keep keeps it and gates the cards on it |
|
|
80
80
|
| supersede <id> --reason R [--replaced-by ID] | retire a task without done/failed; it leaves ready, next and claim |
|
|
81
|
-
| list [--state S] [--role R] [--json] | every task,
|
|
81
|
+
| list [--state S] [--role R] [--json] [--limit N] [--cursor C] | every task, or one server-side page (`{ items, total, cursor, hasMore }`) newest stateChangedAt first |
|
|
82
82
|
| show <id> [--json] | one task, its stage chain, the draft and superseded flags, and the resolved role source |
|
|
83
83
|
| ready [--limit N] [--role R] [--json] | pending, non-draft, non-superseded tasks whose dependencies are done, ordered by priority then age |
|
|
84
84
|
| next [--role R] [--json] | the single best candidate |
|
|
@@ -90,7 +90,8 @@ token-tests-ba66 pending tester token tests
|
|
|
90
90
|
| retry <id> [--stage KEY] | failed back to pending (or reset a chain to KEY) |
|
|
91
91
|
| reopen <id> [--reason R] [--agent A] [--stage KEY] | every state back to pending; clears owner, claim, result and verdict, attempt + 1; releases the claim's announcements; a staged task resets to KEY (default the first stage) |
|
|
92
92
|
| rm <id> [--force] [--json] | delete a pending/failed/done task; a claimed task is refused (exit 2) unless --force, which releases its announcements first |
|
|
93
|
-
|
|
|
93
|
+
| counts [--json] | per-state counts, the superseded count and the oldest pending task, from the lightweight index |
|
|
94
|
+
| stats [--json] | counts by state and role, plus the oldest pending task (index-backed) |
|
|
94
95
|
|
|
95
96
|
Exit codes: 0 success, 1 internal error, 2 refused (already claimed, wrong state, unknown
|
|
96
97
|
task, dependency missing, mode freeze, invalid stage chain, missing --acceptance, draft
|
|
@@ -131,6 +132,39 @@ Counts by state and role, plus the oldest pending task. Superseded tasks keep th
|
|
|
131
132
|
lane but are counted separately: `superseded` in the JSON and its own line in the text
|
|
132
133
|
output; they are not part of byState/byRole or the oldest pending task.
|
|
133
134
|
|
|
135
|
+
## Pagination and counts
|
|
136
|
+
|
|
137
|
+
Large boards are served a page at a time. `--limit` and/or `--cursor` switch
|
|
138
|
+
`list` into server-side pagination:
|
|
139
|
+
|
|
140
|
+
~~~bash
|
|
141
|
+
$ hive-tasks list --json --state pending --limit 50 --cursor 0
|
|
142
|
+
{ "items": [ ... ], "total": 1234, "cursor": 50, "hasMore": true }
|
|
143
|
+
$ hive-tasks list --json --state pending --limit 50 --cursor 50 # next page
|
|
144
|
+
~~~
|
|
145
|
+
|
|
146
|
+
- rows are ordered by `stateChangedAt` descending, ties broken by id so a walk is
|
|
147
|
+
deterministic;
|
|
148
|
+
- `cursor` is the numeric offset printed by the previous page; `--limit` must be a
|
|
149
|
+
positive integer (default 50 when only `--cursor` is given), `--cursor` a
|
|
150
|
+
non-negative integer;
|
|
151
|
+
- `total` is the filtered count, so the caller knows how many pages remain;
|
|
152
|
+
- only the page's cards are read from git; filtering, ordering and the total come from
|
|
153
|
+
a lightweight index (`refs/hive/v1/tasks-index`), which is written with every board
|
|
154
|
+
write and rebuilt once when missing or stale.
|
|
155
|
+
|
|
156
|
+
A `list` without `--limit`/`--cursor` keeps returning the full array. `counts --json`
|
|
157
|
+
returns the per-state numbers from that index without parsing any card:
|
|
158
|
+
|
|
159
|
+
~~~bash
|
|
160
|
+
$ hive-tasks counts --json
|
|
161
|
+
{ "total": 1234, "byState": { "pending": 1200, "claimed": 3, "done": 30, "failed": 1 },
|
|
162
|
+
"superseded": 2, "oldestPending": { "id": "auth-token-a1b2", "createdAt": "..." } }
|
|
163
|
+
~~~
|
|
164
|
+
|
|
165
|
+
`show <id> --json` reads exactly that one card blob. `stats` keeps its older shape
|
|
166
|
+
(byState, byRole, superseded, oldestPending) and is index-backed too.
|
|
167
|
+
|
|
134
168
|
## How a claim works
|
|
135
169
|
|
|
136
170
|
1. the task CAS moves pending -> claimed in the task tree (one winner per task);
|
package/README.zh-CN.md
CHANGED
|
@@ -77,7 +77,7 @@ token-tests-ba66 pending tester token tests
|
|
|
77
77
|
| edit <id> [--title T] [--scope-add P] [--scope-remove P] [--acceptance CMD] [--priority N] [--after-set ID,ID] [--tag +T/-T] [--role R] [--superseded\|--no-superseded] | 只改传入的字段,且只对 pending/failed 任务生效;空 --acceptance 被拒,草稿可用 --acceptance - 清空;scope 改动会同步任务自己的公告 |
|
|
78
78
|
| split <id> --into '<json array>' [--keep] [--reason R] [--json] | 用更细的卡替换一张任务;父任务默认标 superseded 并指向第一张新卡,--keep 保留父任务并让新卡以它为前置 |
|
|
79
79
|
| supersede <id> --reason R [--replaced-by ID] | 退休一张任务(不是 done/failed),不再进入 ready、next 和 claim |
|
|
80
|
-
| list [--state S] [--role R] [--json] |
|
|
80
|
+
| list [--state S] [--role R] [--json] [--limit N] [--cursor C] | 所有任务,或一页服务端分页结果(`{ items, total, cursor, hasMore }`),stateChangedAt 倒序 |
|
|
81
81
|
| show <id> [--json] | 单个任务、它的阶段链、draft/superseded 标记,以及解析到的角色来源 |
|
|
82
82
|
| ready [--limit N] [--role R] [--json] | 依赖已完成的 pending 非草稿、非 superseded 任务,按优先级、再按时间排序 |
|
|
83
83
|
| next [--role R] [--json] | 只给最优的那一个 |
|
|
@@ -89,7 +89,8 @@ token-tests-ba66 pending tester token tests
|
|
|
89
89
|
| retry <id> [--stage KEY] | failed 回到 pending(或把链复位到 KEY) |
|
|
90
90
|
| reopen <id> [--reason R] [--agent A] [--stage KEY] | 任何状态回到 pending;清 owner、认领、结果与判定,attempt + 1;释放认领时的公告;带阶段的任务复位到 KEY(默认第一阶段) |
|
|
91
91
|
| rm <id> [--force] [--json] | 删除 pending/failed/done 的任务;claimed 的任务默认拒绝(退出码 2),加 --force 会先释放公告再删 |
|
|
92
|
-
|
|
|
92
|
+
| counts [--json] | 各状态计数、superseded 计数和最老 pending 任务,来自轻量索引 |
|
|
93
|
+
| stats [--json] | 按状态和角色计数,外加最老的 pending 任务;superseded 单独计数(同样走索引) |
|
|
93
94
|
|
|
94
95
|
退出码:0 成功,1 内部错误,2 被拒(已被认领、状态不对、任务不存在、依赖缺失、
|
|
95
96
|
模式 freeze、阶段链非法、缺 --acceptance、认领草稿任务)。
|
|
@@ -125,6 +126,35 @@ hive-tasks stats --json
|
|
|
125
126
|
按状态和角色计数,外加最老的 pending 任务。superseded 任务保留原 lane,但单独计数:
|
|
126
127
|
JSON 里是 `superseded`,文本输出单独一行;它们不计入 byState/byRole,也不参与最老 pending。
|
|
127
128
|
|
|
129
|
+
## 分页与计数
|
|
130
|
+
|
|
131
|
+
大看板按页读取。`--limit` 和/或 `--cursor` 会把 `list` 切成服务端分页:
|
|
132
|
+
|
|
133
|
+
~~~bash
|
|
134
|
+
$ hive-tasks list --json --state pending --limit 50 --cursor 0
|
|
135
|
+
{ "items": [ ... ], "total": 1234, "cursor": 50, "hasMore": true }
|
|
136
|
+
$ hive-tasks list --json --state pending --limit 50 --cursor 50 # 下一页
|
|
137
|
+
~~~
|
|
138
|
+
|
|
139
|
+
- 行按 `stateChangedAt` 倒序,时间相同按 id 升序,保证翻页顺序稳定;
|
|
140
|
+
- `cursor` 是上一页返回的数字偏移;`--limit` 必须是正整数(只给 `--cursor` 时默认
|
|
141
|
+
50),`--cursor` 必须是非负整数;
|
|
142
|
+
- `total` 是过滤后的总数,调用方能算出还剩几页;
|
|
143
|
+
- 只读取当前页的卡片 blob;过滤、排序和总数来自轻量索引
|
|
144
|
+
(`refs/hive/v1/tasks-index`),它随每次写板更新,缺失或过期时自动重建一次。
|
|
145
|
+
|
|
146
|
+
不带 `--limit`/`--cursor` 的 `list` 仍返回完整数组。`counts --json` 直接从索引返回
|
|
147
|
+
各状态计数,不解析任何卡片:
|
|
148
|
+
|
|
149
|
+
~~~bash
|
|
150
|
+
$ hive-tasks counts --json
|
|
151
|
+
{ "total": 1234, "byState": { "pending": 1200, "claimed": 3, "done": 30, "failed": 1 },
|
|
152
|
+
"superseded": 2, "oldestPending": { "id": "auth-token-a1b2", "createdAt": "..." } }
|
|
153
|
+
~~~
|
|
154
|
+
|
|
155
|
+
`show <id> --json` 只读取那一张卡的 blob。`stats` 保持原有输出形状
|
|
156
|
+
(byState、byRole、superseded、oldestPending),同样由索引支撑。
|
|
157
|
+
|
|
128
158
|
## claim 怎么走
|
|
129
159
|
|
|
130
160
|
1. 任务 CAS 把 pending 推到 claimed(一个任务唯一赢家);
|
package/SPEC.md
CHANGED
|
@@ -47,6 +47,16 @@ Implementation sketch (plumbing, no working tree involved):
|
|
|
47
47
|
GIT_INDEX_FILE=tmp git write-tree # new tree
|
|
48
48
|
git update-ref refs/hive/v1/tasks <new-tree> <old-tree>
|
|
49
49
|
|
|
50
|
+
A derived index lives next to the tree and is never the source of truth:
|
|
51
|
+
|
|
52
|
+
refs/hive/v1/tasks-index -> blob { v: 1, tasks: <tasks tree sha>, entries: [...] }
|
|
53
|
+
|
|
54
|
+
Each entry is [id, state, role, sortTime(ms), createdAt, superseded]. The blob pins the
|
|
55
|
+
task tree sha it describes: a reader that finds it missing or pinned to another tree
|
|
56
|
+
rebuilds it once from the task tree. A board write refreshes it after the task ref moves,
|
|
57
|
+
so a crash between the two updates only costs one rebuild. `list` pagination, `counts`
|
|
58
|
+
and `stats` read this index instead of parsing every card blob.
|
|
59
|
+
|
|
50
60
|
## 3. Task blob
|
|
51
61
|
|
|
52
62
|
```json
|
|
@@ -343,7 +353,8 @@ see the same order; the claim CAS still gives exactly one winner per task.
|
|
|
343
353
|
[--after ID]... [--goal G] [--review-of ID] [--tag T]...
|
|
344
354
|
[--priority N] [--stage ROLE[:ACCEPTANCE]]... [--stages FILE]
|
|
345
355
|
hive-tasks plan [--file plan.json] # no --file reads the plan from stdin
|
|
346
|
-
hive-tasks list [--json] [--state S] [--role R]
|
|
356
|
+
hive-tasks list [--json] [--state S] [--role R] [--limit N] [--cursor C]
|
|
357
|
+
hive-tasks counts [--json]
|
|
347
358
|
hive-tasks ready [--json] [--limit N] [--role R]
|
|
348
359
|
hive-tasks next [--role R] [--json] # the single best candidate, or null
|
|
349
360
|
hive-tasks show <id> [--json] # task plus resolved role source
|
|
@@ -381,6 +392,19 @@ per-stage acceptance in one shot. The two flags are mutually exclusive.
|
|
|
381
392
|
Exit codes: 0 success; 1 internal error; 2 refused (already claimed, wrong state,
|
|
382
393
|
dependency missing, mode freeze, invalid stage chain, missing --acceptance, draft claim).
|
|
383
394
|
|
|
395
|
+
`list --json --state S --role R --limit N --cursor C` returns one server-side page:
|
|
396
|
+
`{ items, total, cursor, hasMore }`. Rows are ordered by stateChangedAt descending
|
|
397
|
+
(ties by id ascending); `cursor` is the numeric offset printed by the previous page (a
|
|
398
|
+
cursor at or past the total yields an empty page); --limit must be a positive integer
|
|
399
|
+
(default 50 when only --cursor is passed) and --cursor a non-negative integer. `total`
|
|
400
|
+
is the filtered count, and only the page's card blobs are read. A list without
|
|
401
|
+
--limit/--cursor keeps returning the array of all matching tasks.
|
|
402
|
+
|
|
403
|
+
`counts --json` returns `{ total, byState, superseded, oldestPending }` from the
|
|
404
|
+
lightweight index without parsing card blobs; superseded tasks are counted separately and
|
|
405
|
+
do not appear inside byState. `stats` keeps its shape (byState, byRole, superseded,
|
|
406
|
+
oldestPending) and is index-backed too. `show <id> --json` reads exactly one card blob.
|
|
407
|
+
|
|
384
408
|
claim couples the task with the hive-consensus announcement board, task first:
|
|
385
409
|
|
|
386
410
|
1. CAS the task from pending to claimed (this is what picks the single winner);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hive-tasks",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.0",
|
|
4
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": {
|
package/src/cli.js
CHANGED
|
@@ -3,7 +3,7 @@ import fs from "node:fs";
|
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { git, out } from "./git.js";
|
|
5
5
|
import { ensureState, repoInfo } from "./repo.js";
|
|
6
|
-
import { listRaw, mutate, readTask } from "./store.js";
|
|
6
|
+
import { DEFAULT_PAGE_LIMIT, listRaw, mutate, readTask, readTaskPage, summarizeIndex } from "./store.js";
|
|
7
7
|
import { create, isDraft, isReady, isReclaimable, isSuperseded, now, order, reopenPatch, transition } from "./task.js";
|
|
8
8
|
import {
|
|
9
9
|
advanceStage,
|
|
@@ -41,7 +41,9 @@ const USAGE = [
|
|
|
41
41
|
" task is superseded and points at the first subtask",
|
|
42
42
|
" hive-tasks supersede <id> --reason R [--replaced-by ID]",
|
|
43
43
|
" retire a task without done/failed; never dispatched",
|
|
44
|
-
" hive-tasks list [--json] [--state S] [--role R]",
|
|
44
|
+
" hive-tasks list [--json] [--state S] [--role R] [--limit N] [--cursor C]",
|
|
45
|
+
" a page of tasks newest stateChangedAt first",
|
|
46
|
+
" hive-tasks counts [--json] per-state counts + the oldest pending task",
|
|
45
47
|
" hive-tasks show <id> [--json] card plus the resolved role source",
|
|
46
48
|
" hive-tasks ready [--json] [--limit N] [--role R]",
|
|
47
49
|
" hive-tasks next [--role R] [--json] the single best candidate",
|
|
@@ -81,7 +83,7 @@ function value(argv, index, flag) {
|
|
|
81
83
|
}
|
|
82
84
|
|
|
83
85
|
function parse(argv) {
|
|
84
|
-
const out = { args: [], scopes: [], scopeAdd: [], scopeRemove: [], after: [], afterSet: null, tags: [], stageArgs: [], into: [], json: false, keep: false, reason: null, agent: null, ttl: null, verdict: null, sha: null, state: null, role: null, limit: null, title: null, goal: null, priority: null, force: false, acceptance: null, verify: false, file: null, stagesFile: null, reviewOf: null, replacedBy: null, supersedeFlag: null, draft: false };
|
|
86
|
+
const out = { args: [], scopes: [], scopeAdd: [], scopeRemove: [], after: [], afterSet: null, tags: [], stageArgs: [], into: [], json: false, keep: false, reason: null, agent: null, ttl: null, verdict: null, sha: null, state: null, role: null, limit: null, cursor: null, title: null, goal: null, priority: null, force: false, acceptance: null, verify: false, file: null, stagesFile: null, reviewOf: null, replacedBy: null, supersedeFlag: null, draft: false };
|
|
85
87
|
for (let i = 0; i < argv.length; i++) {
|
|
86
88
|
const arg = argv[i];
|
|
87
89
|
if (arg === "--json") out.json = true;
|
|
@@ -112,6 +114,7 @@ function parse(argv) {
|
|
|
112
114
|
else if (arg === "--state") out.state = value(argv, i++, arg);
|
|
113
115
|
else if (arg === "--role") out.role = value(argv, i++, arg);
|
|
114
116
|
else if (arg === "--limit") out.limit = Number(value(argv, i++, arg));
|
|
117
|
+
else if (arg === "--cursor") out.cursor = Number(value(argv, i++, arg));
|
|
115
118
|
else if (arg === "--title") out.title = value(argv, i++, arg);
|
|
116
119
|
else if (arg === "--goal") out.goal = value(argv, i++, arg);
|
|
117
120
|
else if (arg === "--priority") out.priority = Number(value(argv, i++, arg));
|
|
@@ -121,6 +124,11 @@ function parse(argv) {
|
|
|
121
124
|
return out;
|
|
122
125
|
}
|
|
123
126
|
|
|
127
|
+
function eventActor(fallback) {
|
|
128
|
+
const value = (process.env.HIVE_EVENT_ACTOR || '').trim();
|
|
129
|
+
return value !== '' ? value : (fallback || 'unknown');
|
|
130
|
+
}
|
|
131
|
+
|
|
124
132
|
function identity(repo, explicit) {
|
|
125
133
|
const name = explicit || process.env.HIVE_AGENT || git(["config", "--get", "hive.agent"], { cwd: repo.top, allowFail: true }).stdout.trim();
|
|
126
134
|
if (!name) fail("an identity is required (--agent, HIVE_AGENT, or git config hive.agent)", 2);
|
|
@@ -491,7 +499,7 @@ function run(argv) {
|
|
|
491
499
|
return [...tasks, ...drafts.map((draft) => draft.task)];
|
|
492
500
|
});
|
|
493
501
|
if (!result.ok) fail("could not write the task board");
|
|
494
|
-
append(repo, { actor: "planner", kind: "task_plan", count: drafts.length });
|
|
502
|
+
append(repo, { actor: eventActor("planner"), kind: "task_plan", count: drafts.length });
|
|
495
503
|
for (const draft of drafts) process.stdout.write(draft.task.id + " " + draft.task.title + "\n");
|
|
496
504
|
return;
|
|
497
505
|
}
|
|
@@ -506,7 +514,7 @@ function run(argv) {
|
|
|
506
514
|
if (current.state !== "claimed" || current.owner !== agent) fail("task " + id + " is not claimed by " + agent, 2);
|
|
507
515
|
const result = mutate(repo, (tasks) => tasks.map((entry) => (entry.id === id ? { ...entry, claimedUntil: new Date(Date.now() + ttlMs).toISOString(), updatedAt: new Date().toISOString() } : entry)));
|
|
508
516
|
if (!result.ok) fail("could not renew " + id);
|
|
509
|
-
append(repo, { actor: agent, kind: "task_renew", task: id });
|
|
517
|
+
append(repo, { actor: eventActor(agent), kind: "task_renew", task: id });
|
|
510
518
|
process.stdout.write(id + " renewed for " + Math.round(ttlMs / 1000) + "s\n");
|
|
511
519
|
return;
|
|
512
520
|
}
|
|
@@ -525,7 +533,7 @@ function run(argv) {
|
|
|
525
533
|
// Legacy cards keep the idempotent no-op. A staged card always resets to
|
|
526
534
|
// the requested stage (default the first), so a chain can be re-run.
|
|
527
535
|
if (!isStaged(current) && current.state === "pending") {
|
|
528
|
-
append(repo, { actor: agent, kind: "reopen", task: id, reason, noop: true });
|
|
536
|
+
append(repo, { actor: eventActor(agent), kind: "reopen", task: id, reason, noop: true });
|
|
529
537
|
return printTask(current, options.json);
|
|
530
538
|
}
|
|
531
539
|
|
|
@@ -549,13 +557,13 @@ function run(argv) {
|
|
|
549
557
|
if (!result.ok) fail("could not reopen " + id);
|
|
550
558
|
|
|
551
559
|
if (!reopened) {
|
|
552
|
-
append(repo, { actor: agent, kind: "reopen", task: id, reason, noop: true });
|
|
560
|
+
append(repo, { actor: eventActor(agent), kind: "reopen", task: id, reason, noop: true });
|
|
553
561
|
return printTask(readTask(repo, id), options.json);
|
|
554
562
|
}
|
|
555
563
|
|
|
556
564
|
// A claimed card may still hold announcements; release exactly the claim we reset.
|
|
557
565
|
if (previous.state === "claimed") releaseLeases(repo, { paths: previous.scope, agent: previous.owner || agent });
|
|
558
|
-
append(repo, { actor: agent, kind: "reopen", task: id, from: previous.state, reason, attempt: reopened.attempt, stage: isStaged(reopened) ? reopened.stage : undefined });
|
|
566
|
+
append(repo, { actor: eventActor(agent), kind: "reopen", task: id, from: previous.state, reason, attempt: reopened.attempt, stage: isStaged(reopened) ? reopened.stage : undefined });
|
|
559
567
|
return printTask(reopened, options.json);
|
|
560
568
|
}
|
|
561
569
|
|
|
@@ -591,7 +599,7 @@ function run(argv) {
|
|
|
591
599
|
// --force deletes a claimed card only after releasing exactly the announcements
|
|
592
600
|
// that claim holds, so a forced delete cannot strand them.
|
|
593
601
|
if (deleted.state === "claimed") releaseLeases(repo, { paths: deleted.scope, agent: deleted.owner || agent });
|
|
594
|
-
append(repo, { actor: agent, kind: "task_delete", task: id, id, state: deleted.state });
|
|
602
|
+
append(repo, { actor: eventActor(agent), kind: "task_delete", task: id, id, state: deleted.state });
|
|
595
603
|
if (options.json) return process.stdout.write(JSON.stringify({ ...viewTask(deleted), deleted: true }, null, 2) + "\n");
|
|
596
604
|
return process.stdout.write(id + " deleted [" + deleted.state + "] " + deleted.title + "\n");
|
|
597
605
|
}
|
|
@@ -660,7 +668,7 @@ function run(argv) {
|
|
|
660
668
|
if (fields.length > 0) {
|
|
661
669
|
const changed = {};
|
|
662
670
|
for (const name of fields) changed[name] = true;
|
|
663
|
-
append(repo, { actor, kind: "task_edit", task: id, fields: changed });
|
|
671
|
+
append(repo, { actor: eventActor(actor), kind: "task_edit", task: id, fields: changed });
|
|
664
672
|
}
|
|
665
673
|
return printTask(updated, options.json);
|
|
666
674
|
}
|
|
@@ -721,7 +729,7 @@ function run(argv) {
|
|
|
721
729
|
fail(error.message, 2);
|
|
722
730
|
}
|
|
723
731
|
|
|
724
|
-
const event = { actor, kind: "task_split", task: id, cards: cards.map((card) => card.id), kept: Boolean(options.keep) };
|
|
732
|
+
const event = { actor: eventActor(actor), kind: "task_split", task: id, cards: cards.map((card) => card.id), kept: Boolean(options.keep) };
|
|
725
733
|
if (!options.keep) event.replacedBy = cards[0].id;
|
|
726
734
|
append(repo, event);
|
|
727
735
|
if (options.json) {
|
|
@@ -762,7 +770,7 @@ function run(argv) {
|
|
|
762
770
|
} catch (error) {
|
|
763
771
|
fail(error.message, 2);
|
|
764
772
|
}
|
|
765
|
-
const event = { actor, kind: "task_supersede", task: id, reason };
|
|
773
|
+
const event = { actor: eventActor(actor), kind: "task_supersede", task: id, reason };
|
|
766
774
|
if (updated.replacedBy) event.replacedBy = updated.replacedBy;
|
|
767
775
|
append(repo, event);
|
|
768
776
|
return printTask(updated, options.json);
|
|
@@ -806,11 +814,37 @@ function run(argv) {
|
|
|
806
814
|
return [...tasks, task];
|
|
807
815
|
});
|
|
808
816
|
if (!result.ok) fail("could not write the task board (too much contention)");
|
|
809
|
-
append(repo, { actor: task.createdBy, kind: "task_add", task: task.id, stages: isStaged(task) ? task.stages.length : undefined });
|
|
817
|
+
append(repo, { actor: eventActor(task.createdBy), kind: "task_add", task: task.id, stages: isStaged(task) ? task.stages.length : undefined });
|
|
810
818
|
return printTask(task, options.json);
|
|
811
819
|
}
|
|
812
820
|
|
|
813
821
|
if (command === "list" || command === "ready") {
|
|
822
|
+
// Paginated list: the index filters/orders/counts, and only the page's
|
|
823
|
+
// cards are parsed, so a page costs the same on a 10-task board and a
|
|
824
|
+
// 100k-task board.
|
|
825
|
+
if (command === "list" && (options.limit !== null || options.cursor !== null)) {
|
|
826
|
+
const limit = options.limit === null ? DEFAULT_PAGE_LIMIT : options.limit;
|
|
827
|
+
if (!Number.isInteger(limit) || limit <= 0) fail("list: --limit must be a positive integer", 2);
|
|
828
|
+
const cursor = options.cursor === null ? 0 : options.cursor;
|
|
829
|
+
if (!Number.isInteger(cursor) || cursor < 0) fail("list: --cursor must be a non-negative integer", 2);
|
|
830
|
+
const page = readTaskPage(repo, { state: options.state, role: options.role, limit, cursor });
|
|
831
|
+
if (options.json) {
|
|
832
|
+
const payload = {
|
|
833
|
+
items: page.items.map((task) => viewTask(task)),
|
|
834
|
+
total: page.total,
|
|
835
|
+
cursor: page.cursor,
|
|
836
|
+
hasMore: page.hasMore,
|
|
837
|
+
};
|
|
838
|
+
return process.stdout.write(JSON.stringify(payload, null, 2) + "\n");
|
|
839
|
+
}
|
|
840
|
+
if (page.items.length === 0) return process.stdout.write("No tasks.\n");
|
|
841
|
+
for (const task of page.items) {
|
|
842
|
+
const tags = (task.draft ? " [draft]" : "") + (task.superseded ? " [superseded]" : "");
|
|
843
|
+
process.stdout.write(task.id.padEnd(34) + task.state.padEnd(9) + stageRole(task).padEnd(11) + task.title + tags + "\n");
|
|
844
|
+
}
|
|
845
|
+
process.stdout.write("\n" + page.items.length + " of " + page.total + " tasks" + (page.hasMore ? "; next cursor: " + page.cursor : "") + "\n");
|
|
846
|
+
return;
|
|
847
|
+
}
|
|
814
848
|
const tasks = board(repo);
|
|
815
849
|
const selected = command === "ready"
|
|
816
850
|
? readyList(repo, tasks, { role: options.role })
|
|
@@ -833,6 +867,17 @@ function run(argv) {
|
|
|
833
867
|
return;
|
|
834
868
|
}
|
|
835
869
|
|
|
870
|
+
if (command === "counts") {
|
|
871
|
+
const summary = summarizeIndex(repo);
|
|
872
|
+
const payload = { total: summary.total, byState: summary.byState, superseded: summary.superseded, oldestPending: summary.oldestPending };
|
|
873
|
+
if (options.json) return process.stdout.write(JSON.stringify(payload, null, 2) + "\n");
|
|
874
|
+
process.stdout.write("cards: " + summary.total + "\n");
|
|
875
|
+
for (const [state, count] of Object.entries(summary.byState)) process.stdout.write(" " + state.padEnd(8) + count + "\n");
|
|
876
|
+
if (summary.superseded > 0) process.stdout.write(" " + "superseded".padEnd(8) + summary.superseded + "\n");
|
|
877
|
+
if (summary.oldestPending) process.stdout.write("oldest pending: " + summary.oldestPending.id + " (" + summary.oldestPending.createdAt + ")\n");
|
|
878
|
+
return;
|
|
879
|
+
}
|
|
880
|
+
|
|
836
881
|
if (command === "next") {
|
|
837
882
|
const [first] = readyList(repo, board(repo), { role: options.role });
|
|
838
883
|
if (!first) {
|
|
@@ -911,7 +956,7 @@ function run(argv) {
|
|
|
911
956
|
const announcements = claimLeases(repo, { paths: claimed.scope, agent, ttlMs, taskId: claimed.id });
|
|
912
957
|
publishPresence(repo, { agent, task: claimed, state: "working", files: claimed.scope });
|
|
913
958
|
append(repo, {
|
|
914
|
-
actor: agent,
|
|
959
|
+
actor: eventActor(agent),
|
|
915
960
|
kind: "task_claim",
|
|
916
961
|
task: claimed.id,
|
|
917
962
|
attempt: claimed.attempt,
|
|
@@ -1032,13 +1077,13 @@ function run(argv) {
|
|
|
1032
1077
|
addVerdict(repo, { sha: options.sha, result: options.verdict });
|
|
1033
1078
|
}
|
|
1034
1079
|
publishPresence(repo, { agent, task: current, state: outcome.targetState === "done" ? "idle" : "working", files: [] });
|
|
1035
|
-
append(repo, { actor: agent, kind: "task_" + command, task: id, reason: options.reason || undefined, stage: isStaged(current) ? current.stage : undefined });
|
|
1036
|
-
for (const event of outcome.events) append(repo, { actor: agent, task: id, ...event });
|
|
1080
|
+
append(repo, { actor: eventActor(agent), kind: "task_" + command, task: id, reason: options.reason || undefined, stage: isStaged(current) ? current.stage : undefined });
|
|
1081
|
+
for (const event of outcome.events) append(repo, { actor: eventActor(agent), task: id, ...event });
|
|
1037
1082
|
if (command === "fail" && current.reviewOf && outcome.targetState === "failed") {
|
|
1038
1083
|
if (upstreamReopened) {
|
|
1039
|
-
append(repo, { actor: agent, kind: "reopen", task: upstreamReopened.id, from: "done", reason: upstreamReason, attempt: upstreamReopened.attempt, via: "review_failed" });
|
|
1084
|
+
append(repo, { actor: eventActor(agent), kind: "reopen", task: upstreamReopened.id, from: "done", reason: upstreamReason, attempt: upstreamReopened.attempt, via: "review_failed" });
|
|
1040
1085
|
}
|
|
1041
|
-
append(repo, { actor: agent, kind: "review_failed", task: id, reviewOf: current.reviewOf, reopened: Boolean(upstreamReopened), reason: options.reason || undefined });
|
|
1086
|
+
append(repo, { actor: eventActor(agent), kind: "review_failed", task: id, reviewOf: current.reviewOf, reopened: Boolean(upstreamReopened), reason: options.reason || undefined });
|
|
1042
1087
|
}
|
|
1043
1088
|
|
|
1044
1089
|
const finalCard = readTask(repo, id);
|
|
@@ -1051,25 +1096,14 @@ function run(argv) {
|
|
|
1051
1096
|
}
|
|
1052
1097
|
|
|
1053
1098
|
if (command === "stats") {
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
const
|
|
1057
|
-
let superseded = 0;
|
|
1058
|
-
let oldest = null;
|
|
1059
|
-
for (const task of items) {
|
|
1060
|
-
// Superseded tasks keep their lane but are counted separately, never as a
|
|
1061
|
-
// state or role of the live board.
|
|
1062
|
-
if (isSuperseded(task)) { superseded += 1; continue; }
|
|
1063
|
-
byState[task.state] = (byState[task.state] || 0) + 1;
|
|
1064
|
-
byRole[task.role] = (byRole[task.role] || 0) + 1;
|
|
1065
|
-
if (task.state === "pending" && (!oldest || task.createdAt < oldest.createdAt)) oldest = task;
|
|
1066
|
-
}
|
|
1067
|
-
const summary = { total: items.length, byState, byRole, superseded, oldestPending: oldest ? { id: oldest.id, createdAt: oldest.createdAt } : null };
|
|
1099
|
+
// Same shape as before, but from the lightweight index: no card blobs are
|
|
1100
|
+
// parsed for a board-wide summary.
|
|
1101
|
+
const summary = summarizeIndex(repo);
|
|
1068
1102
|
if (options.json) return process.stdout.write(JSON.stringify(summary, null, 2) + "\n");
|
|
1069
1103
|
process.stdout.write("cards: " + summary.total + "\n");
|
|
1070
|
-
for (const [state, count] of Object.entries(byState)) process.stdout.write(" " + state.padEnd(8) + count + "\n");
|
|
1071
|
-
if (superseded > 0) process.stdout.write(" " + "superseded".padEnd(8) + superseded + "\n");
|
|
1072
|
-
if (
|
|
1104
|
+
for (const [state, count] of Object.entries(summary.byState)) process.stdout.write(" " + state.padEnd(8) + count + "\n");
|
|
1105
|
+
if (summary.superseded > 0) process.stdout.write(" " + "superseded".padEnd(8) + summary.superseded + "\n");
|
|
1106
|
+
if (summary.oldestPending) process.stdout.write("oldest pending: " + summary.oldestPending.id + " (" + summary.oldestPending.createdAt + ")\n");
|
|
1073
1107
|
return;
|
|
1074
1108
|
}
|
|
1075
1109
|
|
package/src/git.js
CHANGED
|
@@ -21,6 +21,25 @@ export function out(args, opts) {
|
|
|
21
21
|
return git(args, opts).stdout.trim();
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Run git and return stdout as a Buffer, for binary-framed output such as
|
|
26
|
+
* `cat-file --batch`. Input may be a string or Buffer.
|
|
27
|
+
*/
|
|
28
|
+
export function gitBuffer(args, { cwd, env, input, allowFail = false } = {}) {
|
|
29
|
+
const result = spawnSync("git", args, {
|
|
30
|
+
cwd,
|
|
31
|
+
env: env ? { ...process.env, ...env } : process.env,
|
|
32
|
+
input,
|
|
33
|
+
maxBuffer: 512 * 1024 * 1024,
|
|
34
|
+
});
|
|
35
|
+
if (result.error) throw result.error;
|
|
36
|
+
if (!allowFail && result.status !== 0) {
|
|
37
|
+
const detail = String(result.stderr || "").trim();
|
|
38
|
+
throw new Error("git " + args.join(" ") + " failed (exit " + result.status + ")" + (detail ? ": " + detail : ""));
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
|
|
24
43
|
export function ok(args, opts) {
|
|
25
44
|
return git(args, { ...opts, allowFail: true }).status === 0;
|
|
26
45
|
}
|
package/src/store.js
CHANGED
|
@@ -1,30 +1,236 @@
|
|
|
1
1
|
import crypto from "node:crypto";
|
|
2
2
|
import fs from "node:fs";
|
|
3
3
|
import path from "node:path";
|
|
4
|
-
import { git, out } from "./git.js";
|
|
4
|
+
import { git, gitBuffer, out } from "./git.js";
|
|
5
5
|
|
|
6
6
|
export const TASKS_REF = "refs/hive/v1/tasks";
|
|
7
|
+
/**
|
|
8
|
+
* A lightweight board index next to the task tree: one blob holding
|
|
9
|
+
* [id, state, role, sortTime(ms), createdAt, superseded] per task. It is
|
|
10
|
+
* written with every board write, so list/counts/stats never have to parse
|
|
11
|
+
* every card blob; a reader that finds the index missing or pinned to an older
|
|
12
|
+
* tree rebuilds it once from the task tree (a single `cat-file --batch`).
|
|
13
|
+
*/
|
|
14
|
+
export const TASKS_INDEX_REF = "refs/hive/v1/tasks-index";
|
|
15
|
+
export const INDEX_VERSION = 1;
|
|
16
|
+
export const DEFAULT_PAGE_LIMIT = 50;
|
|
7
17
|
|
|
8
|
-
|
|
9
|
-
const result = git(["rev-parse", "--verify", "--quiet",
|
|
18
|
+
function readRefSha(repo, ref) {
|
|
19
|
+
const result = git(["rev-parse", "--verify", "--quiet", ref], { cwd: repo.top, allowFail: true });
|
|
10
20
|
return result.status === 0 ? result.stdout.trim() : null;
|
|
11
21
|
}
|
|
12
22
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
23
|
+
export function refSha(repo) {
|
|
24
|
+
return readRefSha(repo, TASKS_REF);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** [{ id, blobSha }] for one tree, or [] when the tree is missing. */
|
|
28
|
+
function readTree(repo, sha) {
|
|
16
29
|
if (!sha) return [];
|
|
17
|
-
const
|
|
30
|
+
const result = git(["ls-tree", "-z", sha], { cwd: repo.top, allowFail: true });
|
|
31
|
+
if (result.status !== 0) return [];
|
|
32
|
+
const raw = result.stdout;
|
|
18
33
|
if (!raw) return [];
|
|
19
|
-
|
|
34
|
+
const entries = [];
|
|
35
|
+
for (const line of raw.split("\u0000")) {
|
|
36
|
+
if (line === "") continue;
|
|
20
37
|
const tab = line.indexOf("\t");
|
|
38
|
+
if (tab < 0) continue;
|
|
21
39
|
const meta = line.slice(0, tab).split(/\s+/);
|
|
22
|
-
|
|
23
|
-
|
|
40
|
+
entries.push({ id: line.slice(tab + 1), blobSha: meta[2] });
|
|
41
|
+
}
|
|
42
|
+
return entries;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Read blobs by object name in one process: [{ spec }] -> string[].
|
|
47
|
+
* A missing object yields null in its slot. Framing follows git's
|
|
48
|
+
* `<oid> <type> <size>\n<payload>\n` (or `<spec> missing\n`).
|
|
49
|
+
*/
|
|
50
|
+
function readBlobs(repo, specs) {
|
|
51
|
+
if (specs.length === 0) return [];
|
|
52
|
+
const result = gitBuffer(["cat-file", "--batch"], { cwd: repo.top, input: specs.join("\n") + "\n", allowFail: true });
|
|
53
|
+
if (result.status !== 0) {
|
|
54
|
+
const detail = String(result.stderr || "").trim();
|
|
55
|
+
throw new Error("git cat-file --batch failed" + (detail ? ": " + detail : ""));
|
|
56
|
+
}
|
|
57
|
+
const buffer = result.stdout;
|
|
58
|
+
const values = [];
|
|
59
|
+
let offset = 0;
|
|
60
|
+
for (let index = 0; index < specs.length; index += 1) {
|
|
61
|
+
const headerEnd = buffer.indexOf(0x0a, offset);
|
|
62
|
+
if (headerEnd < 0) { values.push(null); continue; }
|
|
63
|
+
const header = buffer.toString("utf8", offset, headerEnd);
|
|
64
|
+
offset = headerEnd + 1;
|
|
65
|
+
const parts = header.split(" ");
|
|
66
|
+
if (parts.length < 3) { values.push(null); continue; }
|
|
67
|
+
const size = Number(parts[2]);
|
|
68
|
+
if (!Number.isInteger(size) || size < 0) { values.push(null); continue; }
|
|
69
|
+
values.push(buffer.subarray(offset, offset + size).toString("utf8"));
|
|
70
|
+
offset += size + 1;
|
|
71
|
+
}
|
|
72
|
+
return values;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** [{ id, value }] from the task tree. An absent ref is an empty board. */
|
|
76
|
+
export function listRaw(repo) {
|
|
77
|
+
const sha = refSha(repo);
|
|
78
|
+
if (!sha) return [];
|
|
79
|
+
const entries = readTree(repo, sha);
|
|
80
|
+
if (entries.length === 0) return [];
|
|
81
|
+
const blobs = readBlobs(repo, entries.map((entry) => entry.blobSha));
|
|
82
|
+
const rows = [];
|
|
83
|
+
for (let index = 0; index < entries.length; index += 1) {
|
|
84
|
+
const text = blobs[index];
|
|
85
|
+
if (text === null) continue;
|
|
24
86
|
let value = null;
|
|
25
|
-
try { value = JSON.parse(
|
|
26
|
-
|
|
27
|
-
}
|
|
87
|
+
try { value = JSON.parse(text); } catch { value = null; }
|
|
88
|
+
if (value) rows.push({ id: entries[index].id, value });
|
|
89
|
+
}
|
|
90
|
+
return rows;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** One card by id: a single blob read, never the whole tree. */
|
|
94
|
+
export function readTask(repo, id) {
|
|
95
|
+
const sha = refSha(repo);
|
|
96
|
+
if (!sha) return null;
|
|
97
|
+
const result = git(["cat-file", "blob", sha + ":" + id], { cwd: repo.top, allowFail: true });
|
|
98
|
+
if (result.status !== 0) return null;
|
|
99
|
+
try { return JSON.parse(result.stdout); } catch { return null; }
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Full cards for a known page of ids, in one batch call. */
|
|
103
|
+
export function readTasksByIds(repo, ids) {
|
|
104
|
+
if (!Array.isArray(ids) || ids.length === 0) return [];
|
|
105
|
+
const sha = refSha(repo);
|
|
106
|
+
if (!sha) return [];
|
|
107
|
+
const blobs = readBlobs(repo, ids.map((id) => sha + ":" + id));
|
|
108
|
+
const tasks = [];
|
|
109
|
+
for (const text of blobs) {
|
|
110
|
+
if (text === null) continue;
|
|
111
|
+
try {
|
|
112
|
+
const value = JSON.parse(text);
|
|
113
|
+
if (value) tasks.push(value);
|
|
114
|
+
} catch {
|
|
115
|
+
// A malformed card is skipped, like listRaw does.
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return tasks;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Sort key shared by the index writer and readers: newest lane change first. */
|
|
122
|
+
export function taskSortTime(task) {
|
|
123
|
+
const value = task && (task.stateChangedAt || task.updatedAt || task.createdAt);
|
|
124
|
+
const time = Date.parse(value || "");
|
|
125
|
+
return Number.isFinite(time) ? time : 0;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function buildIndexEntries(tasks) {
|
|
129
|
+
return tasks.map((task) => [
|
|
130
|
+
task.id,
|
|
131
|
+
task.state,
|
|
132
|
+
task.role || "",
|
|
133
|
+
taskSortTime(task),
|
|
134
|
+
task.createdAt || "",
|
|
135
|
+
task.superseded === true ? 1 : 0,
|
|
136
|
+
]);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function readIndexBlob(repo) {
|
|
140
|
+
const sha = readRefSha(repo, TASKS_INDEX_REF);
|
|
141
|
+
if (!sha) return null;
|
|
142
|
+
const result = git(["cat-file", "blob", sha], { cwd: repo.top, allowFail: true });
|
|
143
|
+
if (result.status !== 0) return null;
|
|
144
|
+
try { return JSON.parse(result.stdout); } catch { return null; }
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function writeIndex(repo, tasksSha, entries) {
|
|
148
|
+
const blob = out(["hash-object", "-w", "--stdin"], {
|
|
149
|
+
cwd: repo.top,
|
|
150
|
+
input: JSON.stringify({ v: INDEX_VERSION, tasks: tasksSha, entries }),
|
|
151
|
+
});
|
|
152
|
+
git(["update-ref", TASKS_INDEX_REF, blob], { cwd: repo.top, allowFail: true });
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Lightweight rows for the whole board: [{ id, state, role, time, createdAt,
|
|
157
|
+
* superseded }]. Uses the index when it is pinned to the current task tree;
|
|
158
|
+
* otherwise rebuilds and stores the index once.
|
|
159
|
+
*/
|
|
160
|
+
export function readIndex(repo) {
|
|
161
|
+
const tasksSha = refSha(repo);
|
|
162
|
+
if (!tasksSha) return { tasksSha: null, entries: [] };
|
|
163
|
+
const cached = readIndexBlob(repo);
|
|
164
|
+
if (cached && cached.v === INDEX_VERSION && cached.tasks === tasksSha && Array.isArray(cached.entries)) {
|
|
165
|
+
return { tasksSha, entries: cached.entries };
|
|
166
|
+
}
|
|
167
|
+
const tasks = listRaw(repo).map((entry) => entry.value);
|
|
168
|
+
const entries = buildIndexEntries(tasks);
|
|
169
|
+
writeIndex(repo, tasksSha, entries);
|
|
170
|
+
return { tasksSha, entries };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Newest stateChangedAt first; ids break ties so paging is deterministic. */
|
|
174
|
+
export function compareIndexRows(left, right) {
|
|
175
|
+
if (left[3] !== right[3]) return right[3] - left[3];
|
|
176
|
+
return String(left[0]).localeCompare(String(right[0]));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Pure page computation over index rows: filter, sort newest-first, cut one
|
|
181
|
+
* page. Exported so the 100k-scale behaviour can be tested without git.
|
|
182
|
+
*/
|
|
183
|
+
export function paginateIndexEntries(entries, { state = null, role = null, limit = DEFAULT_PAGE_LIMIT, cursor = 0 } = {}) {
|
|
184
|
+
let rows = entries;
|
|
185
|
+
if (state) rows = rows.filter((entry) => entry[1] === state);
|
|
186
|
+
if (role) rows = rows.filter((entry) => entry[2] === role);
|
|
187
|
+
rows = rows.slice().sort(compareIndexRows);
|
|
188
|
+
const total = rows.length;
|
|
189
|
+
const start = Math.min(Math.max(0, cursor), total);
|
|
190
|
+
const page = rows.slice(start, start + limit);
|
|
191
|
+
const next = start + page.length;
|
|
192
|
+
return { page, total, cursor: next, hasMore: next < total };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* One server-side page of tasks, newest stateChangedAt first. Only the page's
|
|
197
|
+
* cards are read from git; filtering, ordering and the total come from the
|
|
198
|
+
* lightweight index.
|
|
199
|
+
*/
|
|
200
|
+
export function readTaskPage(repo, options = {}) {
|
|
201
|
+
const { entries } = readIndex(repo);
|
|
202
|
+
const page = paginateIndexEntries(entries, options);
|
|
203
|
+
const tasks = readTasksByIds(repo, page.page.map((entry) => entry[0]));
|
|
204
|
+
const byId = new Map(tasks.map((task) => [task.id, task]));
|
|
205
|
+
const items = page.page.map((entry) => byId.get(entry[0])).filter(Boolean);
|
|
206
|
+
return { items, total: page.total, cursor: page.cursor, hasMore: page.hasMore };
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Pure counts over index rows: per-state and per-role counts, the superseded
|
|
211
|
+
* count and the oldest pending task. Superseded tasks keep their lane but are
|
|
212
|
+
* counted separately, never inside byState/byRole. Exported for the 100k mock.
|
|
213
|
+
*/
|
|
214
|
+
export function summarizeEntries(entries) {
|
|
215
|
+
const byState = {};
|
|
216
|
+
const byRole = {};
|
|
217
|
+
let superseded = 0;
|
|
218
|
+
let oldest = null;
|
|
219
|
+
for (const [id, state, role, , createdAt, isSuperseded] of entries) {
|
|
220
|
+
if (isSuperseded) { superseded += 1; continue; }
|
|
221
|
+
byState[state] = (byState[state] || 0) + 1;
|
|
222
|
+
byRole[role] = (byRole[role] || 0) + 1;
|
|
223
|
+
if (state === "pending" && (oldest === null || String(createdAt) < String(oldest.createdAt))) {
|
|
224
|
+
oldest = { id, createdAt: String(createdAt) };
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return { total: entries.length, byState, byRole, superseded, oldestPending: oldest };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** Counts for the whole board from the lightweight index. */
|
|
231
|
+
export function summarizeIndex(repo) {
|
|
232
|
+
const { entries } = readIndex(repo);
|
|
233
|
+
return summarizeEntries(entries);
|
|
28
234
|
}
|
|
29
235
|
|
|
30
236
|
function hashBlob(repo, value) {
|
|
@@ -33,7 +239,8 @@ function hashBlob(repo, value) {
|
|
|
33
239
|
|
|
34
240
|
/**
|
|
35
241
|
* Replace the whole board with the given tasks, compare-and-swap on the ref.
|
|
36
|
-
*
|
|
242
|
+
* The lightweight index is written after the task ref moves. Returns false when
|
|
243
|
+
* the ref moved underneath us (caller retries).
|
|
37
244
|
*/
|
|
38
245
|
function write(repo, tasks, expectedSha) {
|
|
39
246
|
const indexFile = path.join(repo.stateDir, "index-tasks-" + process.pid + "-" + crypto.randomBytes(4).toString("hex"));
|
|
@@ -44,7 +251,7 @@ function write(repo, tasks, expectedSha) {
|
|
|
44
251
|
else git(["read-tree", "--empty"], { cwd: repo.top, env });
|
|
45
252
|
|
|
46
253
|
const keep = new Set(tasks.map((task) => task.id));
|
|
47
|
-
for (const entry of
|
|
254
|
+
for (const entry of readTree(repo, expectedSha)) {
|
|
48
255
|
if (!keep.has(entry.id)) git(["update-index", "--force-remove", entry.id], { cwd: repo.top, env });
|
|
49
256
|
}
|
|
50
257
|
for (const task of tasks) {
|
|
@@ -56,7 +263,9 @@ function write(repo, tasks, expectedSha) {
|
|
|
56
263
|
cwd: repo.top,
|
|
57
264
|
allowFail: true,
|
|
58
265
|
});
|
|
59
|
-
|
|
266
|
+
if (update.status !== 0) return false;
|
|
267
|
+
writeIndex(repo, tree, buildIndexEntries(tasks));
|
|
268
|
+
return true;
|
|
60
269
|
} finally {
|
|
61
270
|
fs.rmSync(indexFile, { force: true });
|
|
62
271
|
}
|
|
@@ -73,8 +282,3 @@ export function mutate(repo, fn) {
|
|
|
73
282
|
}
|
|
74
283
|
return { ok: false, tasks: [] };
|
|
75
284
|
}
|
|
76
|
-
|
|
77
|
-
export function readTask(repo, id) {
|
|
78
|
-
const found = listRaw(repo).find((entry) => entry.id === id);
|
|
79
|
-
return found ? found.value : null;
|
|
80
|
-
}
|