dsh-progress-union 0.8.4 → 0.9.1

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.
@@ -1,78 +1,78 @@
1
- /**
2
- * v0.7.0 R2 验收:从工具输出里抽真实进度(parseProgress)。
3
- * 硬口径:**无命中返回 null**——绝不拿「165/165」「0:00:06」这类计数臆造百分比/字节。
4
- * 样本取自 curl/wget/pip/uv/git clone/npm·pnpm 的真实打印形态(含 \r 覆写前的单行)。
5
- */
6
- import assert from 'node:assert/strict'
7
- import fs from 'node:fs'
8
- import os from 'node:os'
9
- import path from 'node:path'
10
- import test from 'node:test'
11
-
12
- process.env.DSH_HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'union-prog-test-'))
13
- const { parseProgress } = await import('../index.js')
14
-
15
- test('parseProgress:wget 进度行(百分比 + 字节对)', () => {
16
- const hit = parseProgress('bash', '--2026-12-04 10:00:00-- https://cdn/x.zip\nLength: 29150432 (27.8M)\n 45% [=========> ] 12.3MB/27.8MB 4.5MB/s eta 4s')
17
- assert.equal(hit.percent, 45)
18
- assert.equal(hit.bytesDone, 12300000)
19
- assert.equal(hit.bytesTotal, 27800000)
20
- })
21
-
22
- test('parseProgress:curl 双单写法与 MiB', () => {
23
- const hit = parseProgress('bash', 'curl: 62.5% (344 MiB / 550 MiB) downloaded')
24
- assert.equal(hit.percent, 62.5)
25
- assert.equal(hit.bytesDone, 344 * 1048576)
26
- assert.equal(hit.bytesTotal, 550 * 1048576)
27
- })
28
-
29
- test('parseProgress:curl 的 --:--:-- 进度列不算真值(无 % 无字节对 → null)', () => {
30
- assert.equal(parseProgress('bash', ' 0 0 100 27.8M 0 0 4.5M 0:00:06'), null)
31
- })
32
-
33
- test('parseProgress:pip/uv 的单尾单位与 Downloading 行', () => {
34
- const bar = parseProgress('bash', 'Collecting torch\n ━━━━━━━━━━━━━━ 75.2/128.6 MB 4.1 MB/s eta 0:00:14')
35
- assert.equal(bar.bytesDone, 75200000)
36
- assert.equal(bar.bytesTotal, 128600000)
37
- assert.equal(bar.percent, 58.5)
38
- const only = parseProgress('bash', 'Downloading numpy (12.6 MB)')
39
- assert.equal(only.percent, null)
40
- assert.equal(only.bytesDone, null)
41
- assert.equal(only.bytesTotal, 12600000)
42
- })
43
-
44
- test('parseProgress:git clone 的阶段百分比', () => {
45
- const hit = parseProgress('bash', 'Receiving objects: 62% (155/250), 4.12 MiB | 2.30 MiB/s, done.')
46
- assert.equal(hit.percent, 62)
47
- })
48
-
49
- test('parseProgress:pnpm/npm 步数比(仅下载语境采信)', () => {
50
- assert.equal(parseProgress('bash', '[3/12] Installing lodash@4.17.21').percent, 25)
51
- assert.equal(parseProgress('bash', '[3/12] Running postinstall script'), null, '非下载/安装语境不采信步数比')
52
- })
53
-
54
- test('parseProgress:无进度一律 null(不把计数/时间当字节)', () => {
55
- for (const text of [
56
- '', 'added 142 packages in 3s', '165/165 tests passed', '0:00:06 --:--:--',
57
- 'On branch main\nnothing to commit, working tree clean',
58
- 'Successfully installed requests-2.31.0', 'Committing finished transaction',
59
- 'v1.2.3 released to npm', '12 files changed, 340 insertions(+)',
60
- ]) {
61
- assert.equal(parseProgress('bash', text), null, `不该产出数值:${JSON.stringify(text)}`)
62
- }
63
- })
64
-
65
- test('parseProgress:越界与畸形数值一律不收(不臆造 0..100 之外的百分比)', () => {
66
- assert.equal(parseProgress('bash', 'progress 999% done'), null)
67
- assert.equal(parseProgress('bash', '150% CPU'), null)
68
- assert.equal(parseProgress('bash', ' -12% '), null, '负百分比不是进度')
69
- assert.equal(parseProgress('bash', 'zip45%'), null, '粘连在词尾的数字不当百分比')
70
- assert.equal(parseProgress(null, null), null)
71
- assert.equal(parseProgress('bash', 42), null)
72
- })
73
-
74
- test('parseProgress:只取末次命中(进度是递增打印的)', () => {
75
- const hit = parseProgress('bash', ' 10% [> ] 2.0MB/20.0MB\n 40% [==> ] 8.0MB/20.0MB\n 90% [=====> ] 18.0MB/20.0MB')
76
- assert.equal(hit.percent, 90)
77
- assert.equal(hit.bytesDone, 18000000)
78
- })
1
+ /**
2
+ * v0.7.0 R2 验收:从工具输出里抽真实进度(parseProgress)。
3
+ * 硬口径:**无命中返回 null**——绝不拿「165/165」「0:00:06」这类计数臆造百分比/字节。
4
+ * 样本取自 curl/wget/pip/uv/git clone/npm·pnpm 的真实打印形态(含 \r 覆写前的单行)。
5
+ */
6
+ import assert from 'node:assert/strict'
7
+ import fs from 'node:fs'
8
+ import os from 'node:os'
9
+ import path from 'node:path'
10
+ import test from 'node:test'
11
+
12
+ process.env.DSH_HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'union-prog-test-'))
13
+ const { parseProgress } = await import('../index.js')
14
+
15
+ test('parseProgress:wget 进度行(百分比 + 字节对)', () => {
16
+ const hit = parseProgress('bash', '--2026-12-04 10:00:00-- https://cdn/x.zip\nLength: 29150432 (27.8M)\n 45% [=========> ] 12.3MB/27.8MB 4.5MB/s eta 4s')
17
+ assert.equal(hit.percent, 45)
18
+ assert.equal(hit.bytesDone, 12300000)
19
+ assert.equal(hit.bytesTotal, 27800000)
20
+ })
21
+
22
+ test('parseProgress:curl 双单写法与 MiB', () => {
23
+ const hit = parseProgress('bash', 'curl: 62.5% (344 MiB / 550 MiB) downloaded')
24
+ assert.equal(hit.percent, 62.5)
25
+ assert.equal(hit.bytesDone, 344 * 1048576)
26
+ assert.equal(hit.bytesTotal, 550 * 1048576)
27
+ })
28
+
29
+ test('parseProgress:curl 的 --:--:-- 进度列不算真值(无 % 无字节对 → null)', () => {
30
+ assert.equal(parseProgress('bash', ' 0 0 100 27.8M 0 0 4.5M 0:00:06'), null)
31
+ })
32
+
33
+ test('parseProgress:pip/uv 的单尾单位与 Downloading 行', () => {
34
+ const bar = parseProgress('bash', 'Collecting torch\n ━━━━━━━━━━━━━━ 75.2/128.6 MB 4.1 MB/s eta 0:00:14')
35
+ assert.equal(bar.bytesDone, 75200000)
36
+ assert.equal(bar.bytesTotal, 128600000)
37
+ assert.equal(bar.percent, 58.5)
38
+ const only = parseProgress('bash', 'Downloading numpy (12.6 MB)')
39
+ assert.equal(only.percent, null)
40
+ assert.equal(only.bytesDone, null)
41
+ assert.equal(only.bytesTotal, 12600000)
42
+ })
43
+
44
+ test('parseProgress:git clone 的阶段百分比', () => {
45
+ const hit = parseProgress('bash', 'Receiving objects: 62% (155/250), 4.12 MiB | 2.30 MiB/s, done.')
46
+ assert.equal(hit.percent, 62)
47
+ })
48
+
49
+ test('parseProgress:pnpm/npm 步数比(仅下载语境采信)', () => {
50
+ assert.equal(parseProgress('bash', '[3/12] Installing lodash@4.17.21').percent, 25)
51
+ assert.equal(parseProgress('bash', '[3/12] Running postinstall script'), null, '非下载/安装语境不采信步数比')
52
+ })
53
+
54
+ test('parseProgress:无进度一律 null(不把计数/时间当字节)', () => {
55
+ for (const text of [
56
+ '', 'added 142 packages in 3s', '165/165 tests passed', '0:00:06 --:--:--',
57
+ 'On branch main\nnothing to commit, working tree clean',
58
+ 'Successfully installed requests-2.31.0', 'Committing finished transaction',
59
+ 'v1.2.3 released to npm', '12 files changed, 340 insertions(+)',
60
+ ]) {
61
+ assert.equal(parseProgress('bash', text), null, `不该产出数值:${JSON.stringify(text)}`)
62
+ }
63
+ })
64
+
65
+ test('parseProgress:越界与畸形数值一律不收(不臆造 0..100 之外的百分比)', () => {
66
+ assert.equal(parseProgress('bash', 'progress 999% done'), null)
67
+ assert.equal(parseProgress('bash', '150% CPU'), null)
68
+ assert.equal(parseProgress('bash', ' -12% '), null, '负百分比不是进度')
69
+ assert.equal(parseProgress('bash', 'zip45%'), null, '粘连在词尾的数字不当百分比')
70
+ assert.equal(parseProgress(null, null), null)
71
+ assert.equal(parseProgress('bash', 42), null)
72
+ })
73
+
74
+ test('parseProgress:只取末次命中(进度是递增打印的)', () => {
75
+ const hit = parseProgress('bash', ' 10% [> ] 2.0MB/20.0MB\n 40% [==> ] 8.0MB/20.0MB\n 90% [=====> ] 18.0MB/20.0MB')
76
+ assert.equal(hit.percent, 90)
77
+ assert.equal(hit.bytesDone, 18000000)
78
+ })
@@ -1,185 +1,185 @@
1
- /**
2
- * v0.6.2 验收(R1/R2):冷启动从会话归档回灌板。
3
- * 全部用 archive.js 写盘 + harness 的空 seed / 老 seed 模拟「KV 丢失」「KV 落后于归档」,
4
- * 不起真宿主;每条都可独立复现。
5
- */
6
- import assert from 'node:assert/strict'
7
- import fs from 'node:fs'
8
- import os from 'node:os'
9
- import path from 'node:path'
10
- import test from 'node:test'
11
-
12
- // index.js 模块求值时即读 DSH_HOME,必须先钉死再动态引入。
13
- process.env.DSH_HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'union-hydrate-home-'))
14
- const { archiveDir, writeArchiveVersion, readIndex, archiveVersionPath } = await import('../archive.js')
15
- const { SESSION, createHarness, callRoute, jsonReq, flush } = await import('./harness.mjs')
16
-
17
- const TMP = fs.mkdtempSync(path.join(os.tmpdir(), 'union-hydrate-root-'))
18
- const KEY = Buffer.from(SESSION, 'utf8').toString('base64url')
19
- let rootSeq = 0
20
- const root = () => path.join(TMP, 'case-' + ++rootSeq)
21
-
22
- function mkItem(i, over = {}) {
23
- return {
24
- id: 'p_' + i, title: '任务' + i, status: 'todo', percent: 0, current: '', next: '',
25
- approach: '', note: '', order: i, unit: '', targetTurns: 2, closedAt: 0, startedTurn: 0, ...over,
26
- }
27
- }
28
-
29
- function makeEntry(rev, items) {
30
- const n = items.length
31
- const done = items.filter((i) => i.status === 'done').length
32
- return {
33
- version: 1, sessionId: SESSION, seq: 0, revision: rev, at: Date.now(), reason: 'turn',
34
- task: { title: '回灌测试', goal: { objective: '回灌目标', phase: 'active', roundsStarted: 0 } },
35
- board: { enabled: true, summary: { total: n, done, doing: 0, todo: n - done, blocked: 0, percent: 0 }, items },
36
- handoff: { objective: '回灌目标', doneSoFar: [], inFlight: [], nextItems: [], filesTouched: [], openUnits: [], lastTurn: { turn: rev, startSeq: 0, endSeq: 0, summary: '' }, generatedBy: 'rules' },
37
- truncated: false,
38
- }
39
- }
40
-
41
- function seedVersions(dirRoot, revs, items = [mkItem(0), mkItem(1), mkItem(2)]) {
42
- const d = archiveDir(dirRoot, SESSION)
43
- fs.rmSync(d, { recursive: true, force: true })
44
- const seqs = revs.map((rev) => writeArchiveVersion(d, makeEntry(rev, items.map((i) => ({ ...i })))).seq)
45
- return { dir: d, seqs }
46
- }
47
-
48
- function seedRecord(rev, items) {
49
- return {
50
- [KEY]: {
51
- record: {
52
- version: 1, sessionId: SESSION, revision: rev, userRevision: 0, agentSeenRevision: 0,
53
- enabled: true, createdAt: 1, updatedAt: 1, items, userChanges: [], archiveSeq: 0, archiveError: '',
54
- },
55
- },
56
- }
57
- }
58
-
59
- async function boot(config = {}) {
60
- const harness = createHarness({ config })
61
- await harness.run()
62
- return harness
63
- }
64
-
65
- function snapshot(harness) {
66
- return callRoute(harness, '/progress-union/snapshot', {
67
- method: 'GET', url: '/progress-union/snapshot?sessionId=' + SESSION, headers: {},
68
- }).then((res) => res.json())
69
- }
70
-
71
- const execUpdate = (harness, args) => harness.toolMap.get('progress_update').definition.execute(args, { agent: harness.agent })
72
-
73
- test('KV 缺失 + 归档 rev 7(3 项)→ 快照回灌到 rev 7 / 3 项 / restoredFrom.seq 是 head', async () => {
74
- const r = root()
75
- const { seqs } = seedVersions(r, [5, 6, 7])
76
- const harness = await boot({ archiveRoot: r })
77
- const snap = await snapshot(harness)
78
- assert.equal(snap.revision, 7, '版本号从归档 rev 继续,不回到 0')
79
- assert.equal(snap.items.length, 3)
80
- assert.equal(snap.items[0].title, '任务0')
81
- assert.equal(snap.restoredFrom.seq, seqs.at(-1))
82
- assert.equal(snap.restoredFrom.revision, 7)
83
- assert.deepEqual(Object.keys(snap.restoredFrom).sort(), ['at', 'degraded', 'items', 'reason', 'revision', 'seq'])
84
- assert.equal(snap.restoredFrom.items, 3)
85
- assert.equal(snap.restoredFrom.degraded, false)
86
- // 回灌必须立刻落盘:下一次重启直接从 KV 拿到接续板
87
- await flush()
88
- assert.equal(harness.records[KEY].revision, 7)
89
- assert.equal(harness.records[KEY].items.length, 3)
90
- assert.equal(harness.records[KEY].archiveSeq, seqs.at(-1))
91
- })
92
-
93
- test('回灌后再写一次板 → revision 8(不从 0 重开)', async () => {
94
- const r = root()
95
- seedVersions(r, [7])
96
- const harness = await boot({ archiveRoot: r })
97
- assert.equal((await snapshot(harness)).revision, 7)
98
- await execUpdate(harness, { changes: [{ title: '对账后的新任务', status: 'doing', current: '继续做' }] })
99
- const snap = await snapshot(harness)
100
- assert.equal(snap.revision, 8)
101
- assert.equal(snap.items.length, 4)
102
- })
103
-
104
- test('seed rev 3(2 项)+ 归档 rev 9 → 采用归档、revision 9', async () => {
105
- const r = root()
106
- seedVersions(r, [9])
107
- const harness = createHarness({ config: { archiveRoot: r }, seed: seedRecord(3, [mkItem(0), mkItem(1)]) })
108
- await harness.run()
109
- const snap = await snapshot(harness)
110
- assert.equal(snap.revision, 9)
111
- assert.equal(snap.items.length, 3)
112
- assert.equal(snap.restoredFrom.revision, 9)
113
- })
114
-
115
- test('seed rev 5(2 项)+ 归档 rev 5 → 不回灌,items 保持 seed 内容', async () => {
116
- const r = root()
117
- seedVersions(r, [5])
118
- const items = [mkItem(0, { title: 'seed甲' }), mkItem(1, { title: 'seed乙' })]
119
- const harness = createHarness({ config: { archiveRoot: r }, seed: seedRecord(5, items) })
120
- await harness.run()
121
- const snap = await snapshot(harness)
122
- assert.equal(snap.revision, 5)
123
- assert.equal(snap.items.length, 2)
124
- assert.equal(snap.items[0].title, 'seed甲')
125
- assert.equal(snap.restoredFrom, null, 'KV 不落后时不得回灌')
126
- })
127
-
128
- test('archiveEnabled=false + 归档存在 → 不回灌、不写盘', async () => {
129
- const r = root()
130
- seedVersions(r, [7])
131
- const harness = await boot({ archiveRoot: r, archiveEnabled: false })
132
- const snap = await snapshot(harness)
133
- assert.equal(snap.revision, 0)
134
- assert.equal(snap.items.length, 0)
135
- assert.equal(snap.restoredFrom, null)
136
- await flush()
137
- assert.equal(harness.records[KEY], undefined, '关档时不得写盘')
138
- })
139
-
140
- test('head 版 checksum 坏 + head-1 可读 → 回灌 head-1 且 degraded=true', async () => {
141
- const r = root()
142
- const { dir, seqs } = seedVersions(r, [11, 12])
143
- // 破坏 head 的 checksum:改字段但保留旧 checksum
144
- const headFile = archiveVersionPath(dir, seqs.at(-1))
145
- const head = JSON.parse(fs.readFileSync(headFile, 'utf8'))
146
- head.revision = 99
147
- fs.writeFileSync(headFile, JSON.stringify(head))
148
- const harness = await boot({ archiveRoot: r })
149
- const snap = await snapshot(harness)
150
- assert.equal(snap.revision, 11, 'head 坏则用 head-1 的 revision')
151
- assert.equal(snap.restoredFrom.seq, seqs.at(-2))
152
- assert.equal(snap.restoredFrom.degraded, true)
153
- })
154
-
155
- test('kickoff:恢复态含「已从上一版会话归档接续」且不含「当前为空」,同进程只催一次', async () => {
156
- const r = root()
157
- seedVersions(r, [7])
158
- const harness = await boot({ archiveRoot: r })
159
- const kickoff = harness.contexts.get('progress-union-kickoff')
160
- const first = kickoff()
161
- assert.match(first, /已从上一版会话归档接续/)
162
- assert.match(first, /不要从 0 重建计划/)
163
- assert.doesNotMatch(first, /当前为空/)
164
- assert.equal(kickoff(), '', '同进程不重复催办')
165
- })
166
-
167
- test('captureArchive 之后 KV 的 archiveSeq === index.headSeq', async () => {
168
- const r = root()
169
- const harness = createHarness({ config: { archiveRoot: r }, seed: seedRecord(2, [mkItem(0)]) })
170
- await harness.run()
171
- harness.emit('turn/end', {})
172
- await flush()
173
- const index = readIndex(archiveDir(r, SESSION))
174
- assert.ok(index && index.headSeq >= 1, '归档已写入')
175
- await flush()
176
- assert.equal(harness.records[KEY].archiveSeq, index.headSeq, 'archiveSeq 必须落盘(0.6.1 只写内存)')
177
- })
178
-
179
- test('老记录无 restoredFrom 字段:归一不报错,快照回 null', async () => {
180
- const harness = createHarness({ seed: seedRecord(2, [mkItem(0)]) })
181
- await harness.run()
182
- const snap = await snapshot(harness)
183
- assert.equal(snap.restoredFrom, null)
184
- assert.equal(snap.revision, 2)
185
- })
1
+ /**
2
+ * v0.6.2 验收(R1/R2):冷启动从会话归档回灌板。
3
+ * 全部用 archive.js 写盘 + harness 的空 seed / 老 seed 模拟「KV 丢失」「KV 落后于归档」,
4
+ * 不起真宿主;每条都可独立复现。
5
+ */
6
+ import assert from 'node:assert/strict'
7
+ import fs from 'node:fs'
8
+ import os from 'node:os'
9
+ import path from 'node:path'
10
+ import test from 'node:test'
11
+
12
+ // index.js 模块求值时即读 DSH_HOME,必须先钉死再动态引入。
13
+ process.env.DSH_HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'union-hydrate-home-'))
14
+ const { archiveDir, writeArchiveVersion, readIndex, archiveVersionPath } = await import('../archive.js')
15
+ const { SESSION, createHarness, callRoute, jsonReq, flush } = await import('./harness.mjs')
16
+
17
+ const TMP = fs.mkdtempSync(path.join(os.tmpdir(), 'union-hydrate-root-'))
18
+ const KEY = Buffer.from(SESSION, 'utf8').toString('base64url')
19
+ let rootSeq = 0
20
+ const root = () => path.join(TMP, 'case-' + ++rootSeq)
21
+
22
+ function mkItem(i, over = {}) {
23
+ return {
24
+ id: 'p_' + i, title: '任务' + i, status: 'todo', percent: 0, current: '', next: '',
25
+ approach: '', note: '', order: i, unit: '', targetTurns: 2, closedAt: 0, startedTurn: 0, ...over,
26
+ }
27
+ }
28
+
29
+ function makeEntry(rev, items) {
30
+ const n = items.length
31
+ const done = items.filter((i) => i.status === 'done').length
32
+ return {
33
+ version: 1, sessionId: SESSION, seq: 0, revision: rev, at: Date.now(), reason: 'turn',
34
+ task: { title: '回灌测试', goal: { objective: '回灌目标', phase: 'active', roundsStarted: 0 } },
35
+ board: { enabled: true, summary: { total: n, done, doing: 0, todo: n - done, blocked: 0, percent: 0 }, items },
36
+ handoff: { objective: '回灌目标', doneSoFar: [], inFlight: [], nextItems: [], filesTouched: [], openUnits: [], lastTurn: { turn: rev, startSeq: 0, endSeq: 0, summary: '' }, generatedBy: 'rules' },
37
+ truncated: false,
38
+ }
39
+ }
40
+
41
+ function seedVersions(dirRoot, revs, items = [mkItem(0), mkItem(1), mkItem(2)]) {
42
+ const d = archiveDir(dirRoot, SESSION)
43
+ fs.rmSync(d, { recursive: true, force: true })
44
+ const seqs = revs.map((rev) => writeArchiveVersion(d, makeEntry(rev, items.map((i) => ({ ...i })))).seq)
45
+ return { dir: d, seqs }
46
+ }
47
+
48
+ function seedRecord(rev, items) {
49
+ return {
50
+ [KEY]: {
51
+ record: {
52
+ version: 1, sessionId: SESSION, revision: rev, userRevision: 0, agentSeenRevision: 0,
53
+ enabled: true, createdAt: 1, updatedAt: 1, items, userChanges: [], archiveSeq: 0, archiveError: '',
54
+ },
55
+ },
56
+ }
57
+ }
58
+
59
+ async function boot(config = {}) {
60
+ const harness = createHarness({ config })
61
+ await harness.run()
62
+ return harness
63
+ }
64
+
65
+ function snapshot(harness) {
66
+ return callRoute(harness, '/progress-union/snapshot', {
67
+ method: 'GET', url: '/progress-union/snapshot?sessionId=' + SESSION, headers: {},
68
+ }).then((res) => res.json())
69
+ }
70
+
71
+ const execUpdate = (harness, args) => harness.toolMap.get('progress_update').definition.execute(args, { agent: harness.agent })
72
+
73
+ test('KV 缺失 + 归档 rev 7(3 项)→ 快照回灌到 rev 7 / 3 项 / restoredFrom.seq 是 head', async () => {
74
+ const r = root()
75
+ const { seqs } = seedVersions(r, [5, 6, 7])
76
+ const harness = await boot({ archiveRoot: r })
77
+ const snap = await snapshot(harness)
78
+ assert.equal(snap.revision, 7, '版本号从归档 rev 继续,不回到 0')
79
+ assert.equal(snap.items.length, 3)
80
+ assert.equal(snap.items[0].title, '任务0')
81
+ assert.equal(snap.restoredFrom.seq, seqs.at(-1))
82
+ assert.equal(snap.restoredFrom.revision, 7)
83
+ assert.deepEqual(Object.keys(snap.restoredFrom).sort(), ['at', 'degraded', 'items', 'reason', 'revision', 'seq'])
84
+ assert.equal(snap.restoredFrom.items, 3)
85
+ assert.equal(snap.restoredFrom.degraded, false)
86
+ // 回灌必须立刻落盘:下一次重启直接从 KV 拿到接续板
87
+ await flush()
88
+ assert.equal(harness.records[KEY].revision, 7)
89
+ assert.equal(harness.records[KEY].items.length, 3)
90
+ assert.equal(harness.records[KEY].archiveSeq, seqs.at(-1))
91
+ })
92
+
93
+ test('回灌后再写一次板 → revision 8(不从 0 重开)', async () => {
94
+ const r = root()
95
+ seedVersions(r, [7])
96
+ const harness = await boot({ archiveRoot: r })
97
+ assert.equal((await snapshot(harness)).revision, 7)
98
+ await execUpdate(harness, { changes: [{ title: '对账后的新任务', status: 'doing', current: '继续做' }] })
99
+ const snap = await snapshot(harness)
100
+ assert.equal(snap.revision, 8)
101
+ assert.equal(snap.items.length, 4)
102
+ })
103
+
104
+ test('seed rev 3(2 项)+ 归档 rev 9 → 采用归档、revision 9', async () => {
105
+ const r = root()
106
+ seedVersions(r, [9])
107
+ const harness = createHarness({ config: { archiveRoot: r }, seed: seedRecord(3, [mkItem(0), mkItem(1)]) })
108
+ await harness.run()
109
+ const snap = await snapshot(harness)
110
+ assert.equal(snap.revision, 9)
111
+ assert.equal(snap.items.length, 3)
112
+ assert.equal(snap.restoredFrom.revision, 9)
113
+ })
114
+
115
+ test('seed rev 5(2 项)+ 归档 rev 5 → 不回灌,items 保持 seed 内容', async () => {
116
+ const r = root()
117
+ seedVersions(r, [5])
118
+ const items = [mkItem(0, { title: 'seed甲' }), mkItem(1, { title: 'seed乙' })]
119
+ const harness = createHarness({ config: { archiveRoot: r }, seed: seedRecord(5, items) })
120
+ await harness.run()
121
+ const snap = await snapshot(harness)
122
+ assert.equal(snap.revision, 5)
123
+ assert.equal(snap.items.length, 2)
124
+ assert.equal(snap.items[0].title, 'seed甲')
125
+ assert.equal(snap.restoredFrom, null, 'KV 不落后时不得回灌')
126
+ })
127
+
128
+ test('archiveEnabled=false + 归档存在 → 不回灌、不写盘', async () => {
129
+ const r = root()
130
+ seedVersions(r, [7])
131
+ const harness = await boot({ archiveRoot: r, archiveEnabled: false })
132
+ const snap = await snapshot(harness)
133
+ assert.equal(snap.revision, 0)
134
+ assert.equal(snap.items.length, 0)
135
+ assert.equal(snap.restoredFrom, null)
136
+ await flush()
137
+ assert.equal(harness.records[KEY], undefined, '关档时不得写盘')
138
+ })
139
+
140
+ test('head 版 checksum 坏 + head-1 可读 → 回灌 head-1 且 degraded=true', async () => {
141
+ const r = root()
142
+ const { dir, seqs } = seedVersions(r, [11, 12])
143
+ // 破坏 head 的 checksum:改字段但保留旧 checksum
144
+ const headFile = archiveVersionPath(dir, seqs.at(-1))
145
+ const head = JSON.parse(fs.readFileSync(headFile, 'utf8'))
146
+ head.revision = 99
147
+ fs.writeFileSync(headFile, JSON.stringify(head))
148
+ const harness = await boot({ archiveRoot: r })
149
+ const snap = await snapshot(harness)
150
+ assert.equal(snap.revision, 11, 'head 坏则用 head-1 的 revision')
151
+ assert.equal(snap.restoredFrom.seq, seqs.at(-2))
152
+ assert.equal(snap.restoredFrom.degraded, true)
153
+ })
154
+
155
+ test('kickoff:恢复态含「已从上一版会话归档接续」且不含「当前为空」,同进程只催一次', async () => {
156
+ const r = root()
157
+ seedVersions(r, [7])
158
+ const harness = await boot({ archiveRoot: r })
159
+ const kickoff = harness.contexts.get('progress-union-kickoff')
160
+ const first = kickoff()
161
+ assert.match(first, /已从上一版会话归档接续/)
162
+ assert.match(first, /不要从 0 重建计划/)
163
+ assert.doesNotMatch(first, /当前为空/)
164
+ assert.equal(kickoff(), '', '同进程不重复催办')
165
+ })
166
+
167
+ test('captureArchive 之后 KV 的 archiveSeq === index.headSeq', async () => {
168
+ const r = root()
169
+ const harness = createHarness({ config: { archiveRoot: r }, seed: seedRecord(2, [mkItem(0)]) })
170
+ await harness.run()
171
+ harness.emit('turn/end', {})
172
+ await flush()
173
+ const index = readIndex(archiveDir(r, SESSION))
174
+ assert.ok(index && index.headSeq >= 1, '归档已写入')
175
+ await flush()
176
+ assert.equal(harness.records[KEY].archiveSeq, index.headSeq, 'archiveSeq 必须落盘(0.6.1 只写内存)')
177
+ })
178
+
179
+ test('老记录无 restoredFrom 字段:归一不报错,快照回 null', async () => {
180
+ const harness = createHarness({ seed: seedRecord(2, [mkItem(0)]) })
181
+ await harness.run()
182
+ const snap = await snapshot(harness)
183
+ assert.equal(snap.restoredFrom, null)
184
+ assert.equal(snap.revision, 2)
185
+ })