dsh-vscode-mode 0.36.0 → 0.38.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/README.md +32 -4
- package/lib/client.js +68 -65
- package/lib/client.js.map +1 -1
- package/lib/index.js +1961 -222
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
- package/skills/dsh-vscodemode-html-plan/SKILL.md +1 -1
- package/src/archivePurge.ts +411 -0
- package/src/client/addToConversation.ts +77 -18
- package/src/client/htmlPlan/HtmlPlanPanel.ts +9 -13
- package/src/client/htmlPlan/planActions.ts +97 -23
- package/src/client/htmlPlan/planPage.ts +2 -2
- package/src/client/reviewBatch.ts +48 -1
- package/src/client/state/diffSummary.ts +2 -1
- package/src/client/styles/editor.css +3 -1
- package/src/client/ui/EditorView.ts +8 -9
- package/src/client/ui/SvnCommitDialog.ts +256 -31
- package/src/client/ui/svnCommitModel.ts +63 -0
- package/src/index.ts +13 -0
- package/src/pending/capture.ts +201 -34
- package/src/pending/decide.ts +33 -1
- package/src/pending/externalCapture.ts +171 -8
- package/src/pending/kernel.ts +13 -2
- package/src/pending/noteTool.ts +96 -0
- package/src/pending/notes.ts +83 -0
- package/src/pending/persist.ts +301 -51
- package/src/pending/provenance.ts +45 -11
- package/src/pending/receipt.ts +177 -0
- package/src/pending/store.ts +161 -69
- package/src/pending/vcs.ts +25 -7
- package/src/pending/verdict.ts +170 -0
- package/src/pendingCompat.ts +14 -1
- package/src/pendingRpc.ts +19 -1
- package/src/reviewDb.ts +63 -2
- package/src/reviewRpc.ts +47 -1
- package/src/rpc.ts +36 -4
- package/src/shared/pendingReview.ts +20 -2
- package/src/shared/review.ts +17 -0
- package/src/shared/reviewRegions.ts +48 -3
- package/src/shared/rpc.ts +7 -1
- package/src/slowLog.ts +74 -0
- package/src/textEncoding.probe.ts +1 -0
package/src/pending/persist.ts
CHANGED
|
@@ -3,8 +3,10 @@
|
|
|
3
3
|
* @author ddj 2026年10月29号
|
|
4
4
|
*/
|
|
5
5
|
import type { DatabaseSync } from 'node:sqlite'
|
|
6
|
+
import { createHash } from 'node:crypto'
|
|
6
7
|
import { activeReady, dbFor } from '../archiveDb.js'
|
|
7
|
-
import
|
|
8
|
+
import { log } from '../log.js'
|
|
9
|
+
import type { PendingBaseKind, PendingEvidence, PendingLineage, PendingOrigin, PendingSource, PendingVerdict } from '../shared/pendingReview.js'
|
|
8
10
|
import type { PendingEntry } from './kernel.js'
|
|
9
11
|
|
|
10
12
|
/** 数据库行。 */
|
|
@@ -24,8 +26,8 @@ export interface StoredEntry {
|
|
|
24
26
|
flags: PendingFlags
|
|
25
27
|
}
|
|
26
28
|
|
|
27
|
-
/** 待审库版本号,写进 metadata 便于日后迁移。
|
|
28
|
-
const SCHEMA_VERSION = '
|
|
29
|
+
/** 待审库版本号,写进 metadata 便于日后迁移。v6 把撤销栈改成「一条路径一行」。 */
|
|
30
|
+
const SCHEMA_VERSION = '6'
|
|
29
31
|
/** 迁移前的备份后缀。 */
|
|
30
32
|
const BACKUP_SUFFIX = '.before-pending.bak'
|
|
31
33
|
/** 工作区待审版本号:条目一落库就自增,供客户端轮询判断是否需要重读列表。 */
|
|
@@ -79,7 +81,9 @@ export function pendingDb(cwd: string): DatabaseSync {
|
|
|
79
81
|
try {
|
|
80
82
|
db.exec(DDL)
|
|
81
83
|
migrateHistory(db)
|
|
84
|
+
migrateHistoryMerge(db)
|
|
82
85
|
migrateProvenance(db)
|
|
86
|
+
migrateEvidence(db)
|
|
83
87
|
db.prepare("INSERT INTO metadata(name,value) VALUES('pending_schema',?) ON CONFLICT(name) DO UPDATE SET value=excluded.value")
|
|
84
88
|
.run(SCHEMA_VERSION)
|
|
85
89
|
db.exec('COMMIT')
|
|
@@ -100,7 +104,8 @@ CREATE TABLE IF NOT EXISTS pending_entries (
|
|
|
100
104
|
revision INTEGER NOT NULL DEFAULT 0, session_id TEXT NOT NULL,
|
|
101
105
|
session_ids TEXT NOT NULL, lineage TEXT,
|
|
102
106
|
source TEXT NOT NULL DEFAULT 'tool', origin TEXT NOT NULL DEFAULT 'agent',
|
|
103
|
-
base_kind TEXT NOT NULL DEFAULT 'known'
|
|
107
|
+
base_kind TEXT NOT NULL DEFAULT 'known',
|
|
108
|
+
evidence TEXT NOT NULL DEFAULT 'snapshot', verdict TEXT NOT NULL DEFAULT 'changed');
|
|
104
109
|
CREATE TABLE IF NOT EXISTS pending_blocks (
|
|
105
110
|
entry_id TEXT NOT NULL, block_id TEXT NOT NULL, decision TEXT NOT NULL,
|
|
106
111
|
PRIMARY KEY(entry_id,block_id));
|
|
@@ -108,9 +113,10 @@ CREATE TABLE IF NOT EXISTS pending_ops (
|
|
|
108
113
|
operation_id TEXT PRIMARY KEY, request TEXT NOT NULL, state TEXT NOT NULL,
|
|
109
114
|
result TEXT, undo TEXT);
|
|
110
115
|
CREATE TABLE IF NOT EXISTS pending_history (
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
116
|
+
entry_id TEXT PRIMARY KEY, path TEXT NOT NULL, kind TEXT NOT NULL,
|
|
117
|
+
earlier TEXT NOT NULL DEFAULT 'file', baseline TEXT NOT NULL, content TEXT NOT NULL,
|
|
118
|
+
digest TEXT NOT NULL, wrote INTEGER NOT NULL DEFAULT 0,
|
|
119
|
+
state TEXT NOT NULL DEFAULT 'applied', merged_at INTEGER NOT NULL);
|
|
114
120
|
`
|
|
115
121
|
|
|
116
122
|
/**
|
|
@@ -128,6 +134,20 @@ function migrateProvenance(db: DatabaseSync): void {
|
|
|
128
134
|
if (!names.has('base_kind')) db.exec("ALTER TABLE pending_entries ADD COLUMN base_kind TEXT NOT NULL DEFAULT 'known'")
|
|
129
135
|
}
|
|
130
136
|
|
|
137
|
+
/**
|
|
138
|
+
* @author ddj 2026年11月25号
|
|
139
|
+
* @description: v4 → v5:给条目补证据强度与结局两列。旧行按默认值读成「快照 / 真实改动」,
|
|
140
|
+
* 与 v4 的语义一致,不需要回填。
|
|
141
|
+
* @param db 已打开的工作区库
|
|
142
|
+
* @returns 无
|
|
143
|
+
*/
|
|
144
|
+
function migrateEvidence(db: DatabaseSync): void {
|
|
145
|
+
const columns = db.prepare('PRAGMA table_info(pending_entries)').all() as Row[]
|
|
146
|
+
const names = new Set(columns.map((column) => String(column.name)))
|
|
147
|
+
if (!names.has('evidence')) db.exec("ALTER TABLE pending_entries ADD COLUMN evidence TEXT NOT NULL DEFAULT 'snapshot'")
|
|
148
|
+
if (!names.has('verdict')) db.exec("ALTER TABLE pending_entries ADD COLUMN verdict TEXT NOT NULL DEFAULT 'changed'")
|
|
149
|
+
}
|
|
150
|
+
|
|
131
151
|
/**
|
|
132
152
|
* @author ddj 2026年10月29号
|
|
133
153
|
* @description: v1 历史表补 state 列(rebuild 成本高于一次 ALTER)。
|
|
@@ -140,6 +160,81 @@ function migrateHistory(db: DatabaseSync): void {
|
|
|
140
160
|
db.exec("ALTER TABLE pending_history ADD COLUMN state TEXT NOT NULL DEFAULT 'applied'")
|
|
141
161
|
}
|
|
142
162
|
|
|
163
|
+
/**
|
|
164
|
+
* @author ddj 2026年11月25号
|
|
165
|
+
* @description: v5 → v6:把「每次决定一行」的撤销栈折成「每条路径一行」。
|
|
166
|
+
* 同一路径只保留最近一次判定,用被替换掉的那一侧全文作撤销依据;其余旧行直接丢弃,
|
|
167
|
+
* 这是合并语义:一段代码改三次仍然只有一份差异,不再堆三次快照。
|
|
168
|
+
* @param db 已打开的工作区库
|
|
169
|
+
* @returns 无
|
|
170
|
+
*/
|
|
171
|
+
function migrateHistoryMerge(db: DatabaseSync): void {
|
|
172
|
+
const columns = db.prepare('PRAGMA table_info(pending_history)').all() as Row[]
|
|
173
|
+
if (!columns.some((column) => column.name === 'snapshot')) return
|
|
174
|
+
const rows = db.prepare(`SELECT h.entry_id, h.snapshot, h.created_at, h.state, e.path AS entry_path
|
|
175
|
+
FROM pending_history h LEFT JOIN pending_entries e ON e.id = h.entry_id
|
|
176
|
+
WHERE h.id IN (SELECT MAX(id) FROM pending_history GROUP BY entry_id)`).all() as Row[]
|
|
177
|
+
db.exec(`CREATE TABLE IF NOT EXISTS pending_history_v6 (
|
|
178
|
+
entry_id TEXT PRIMARY KEY, path TEXT NOT NULL, kind TEXT NOT NULL,
|
|
179
|
+
earlier TEXT NOT NULL DEFAULT 'file', baseline TEXT NOT NULL, content TEXT NOT NULL,
|
|
180
|
+
digest TEXT NOT NULL, wrote INTEGER NOT NULL DEFAULT 0,
|
|
181
|
+
state TEXT NOT NULL DEFAULT 'applied', merged_at INTEGER NOT NULL)`)
|
|
182
|
+
const insert = db.prepare(`INSERT OR REPLACE INTO pending_history_v6
|
|
183
|
+
(entry_id,path,kind,earlier,baseline,content,digest,wrote,state,merged_at) VALUES(?,?,?,?,?,?,?,?,?,?)`)
|
|
184
|
+
for (const row of rows) {
|
|
185
|
+
const folded = foldLegacySnapshot(row)
|
|
186
|
+
if (!folded) continue
|
|
187
|
+
insert.run(String(row.entry_id), String(row.entry_path ?? folded.path), folded.kind, folded.earlier,
|
|
188
|
+
folded.baseline, folded.content, digestOf(folded.content), folded.wrote ? 1 : 0,
|
|
189
|
+
row.state === 'undone' ? 'undone' : 'applied', Number(row.created_at) || Date.now())
|
|
190
|
+
}
|
|
191
|
+
db.exec('DROP TABLE pending_history')
|
|
192
|
+
db.exec('ALTER TABLE pending_history_v6 RENAME TO pending_history')
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* @private
|
|
197
|
+
* @author ddj 2026年11月25号
|
|
198
|
+
* @description: 把旧的「before/after 条目快照」还原成一条合并撤销行;解析不了就丢弃。
|
|
199
|
+
* @param row 旧历史行
|
|
200
|
+
* @returns 折叠结果,无法解析时为 null
|
|
201
|
+
*/
|
|
202
|
+
function foldLegacySnapshot(row: Row): { kind: 'keep' | 'revert'; earlier: string; baseline: string; content: string; wrote: boolean; path: string } | null {
|
|
203
|
+
try {
|
|
204
|
+
const snapshot = JSON.parse(String(row.snapshot)) as {
|
|
205
|
+
before?: { entry?: { oldText?: unknown; newText?: unknown; earlierVersion?: unknown; path?: unknown } }
|
|
206
|
+
after?: { entry?: { oldText?: unknown; newText?: unknown } }
|
|
207
|
+
wrote?: unknown
|
|
208
|
+
}
|
|
209
|
+
const before = snapshot.before?.entry
|
|
210
|
+
const after = snapshot.after?.entry
|
|
211
|
+
if (!before || !after) return null
|
|
212
|
+
const baseline = String(before.oldText ?? '')
|
|
213
|
+
const content = String(before.newText ?? '')
|
|
214
|
+
return {
|
|
215
|
+
kind: String(after.oldText ?? '') !== baseline ? 'keep' : 'revert',
|
|
216
|
+
earlier: String(before.earlierVersion ?? 'file') === 'none' ? 'none' : 'file',
|
|
217
|
+
baseline,
|
|
218
|
+
content,
|
|
219
|
+
wrote: snapshot.wrote === true,
|
|
220
|
+
path: String(before.path ?? ''),
|
|
221
|
+
}
|
|
222
|
+
} catch {
|
|
223
|
+
return null
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* @private
|
|
229
|
+
* @author ddj 2026年11月25号
|
|
230
|
+
* @description: 撤销行内容指纹,用于重试幂等与诊断,不参与判定。
|
|
231
|
+
* @param text 被替换掉的那一侧全文
|
|
232
|
+
* @returns 十六进制摘要
|
|
233
|
+
*/
|
|
234
|
+
function digestOf(text: string): string {
|
|
235
|
+
return createHash('sha256').update(text).digest('hex').slice(0, 32)
|
|
236
|
+
}
|
|
237
|
+
|
|
143
238
|
/**
|
|
144
239
|
* @author ddj 2026年10月29号
|
|
145
240
|
* @description: 读出全部待审条目,按更新时间升序。
|
|
@@ -151,6 +246,21 @@ export function loadEntries(cwd: string): StoredEntry[] {
|
|
|
151
246
|
return rows.map((row) => ({ entry: entryOfRow(row), flags: flagsOfRow(row) }))
|
|
152
247
|
}
|
|
153
248
|
|
|
249
|
+
/**
|
|
250
|
+
* @author ddj 2026年11月25号
|
|
251
|
+
* @description: 按身份读单条条目:watch 批次快照可能早于工具落库,重读一条比整表便宜。
|
|
252
|
+
* @param cwd 工作区
|
|
253
|
+
* @param id 条目身份(后端进程路径)
|
|
254
|
+
* @returns 条目与标记,缺失时 undefined
|
|
255
|
+
*/
|
|
256
|
+
export function loadEntry(cwd: string, id: string): StoredEntry | undefined {
|
|
257
|
+
const db = pendingDb(cwd)
|
|
258
|
+
// Windows 上盘符与路径大小写不敏感:精确命中失败再按 NOCASE 找一次。
|
|
259
|
+
const row = (db.prepare('SELECT * FROM pending_entries WHERE id=?').get(id)
|
|
260
|
+
?? db.prepare('SELECT * FROM pending_entries WHERE id=? COLLATE NOCASE').get(id)) as Row | undefined
|
|
261
|
+
return row ? { entry: entryOfRow(row), flags: flagsOfRow(row) } : undefined
|
|
262
|
+
}
|
|
263
|
+
|
|
154
264
|
/**
|
|
155
265
|
* @author ddj 2026年10月29号
|
|
156
266
|
* @description: 写入或更新一条条目(含标记),单条原子完成。
|
|
@@ -174,17 +284,19 @@ function upsertEntry(db: DatabaseSync, cwd: string, stored: StoredEntry): void {
|
|
|
174
284
|
const { entry, flags } = stored
|
|
175
285
|
db.prepare(`INSERT INTO pending_entries
|
|
176
286
|
(id,path,earlier,old_text,new_text,updated_at,unseen,missing,diverged,oversized,revision,session_id,session_ids,lineage,
|
|
177
|
-
source,origin,base_kind)
|
|
178
|
-
VALUES(
|
|
287
|
+
source,origin,base_kind,evidence,verdict)
|
|
288
|
+
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
|
|
179
289
|
ON CONFLICT(id) DO UPDATE SET path=excluded.path,earlier=excluded.earlier,old_text=excluded.old_text,
|
|
180
290
|
new_text=excluded.new_text,updated_at=excluded.updated_at,unseen=excluded.unseen,missing=excluded.missing,
|
|
181
291
|
diverged=excluded.diverged,oversized=excluded.oversized,revision=excluded.revision,
|
|
182
292
|
session_id=excluded.session_id,session_ids=excluded.session_ids,lineage=excluded.lineage,
|
|
183
|
-
source=excluded.source,origin=excluded.origin,base_kind=excluded.base_kind
|
|
293
|
+
source=excluded.source,origin=excluded.origin,base_kind=excluded.base_kind,
|
|
294
|
+
evidence=excluded.evidence,verdict=excluded.verdict`)
|
|
184
295
|
.run(entry.id, entry.path, entry.earlierVersion, entry.oldText, entry.newText, entry.updatedAt,
|
|
185
296
|
entry.unseen === true ? 1 : 0, flags.missing ? 1 : 0, flags.diverged ? 1 : 0, flags.oversized ? 1 : 0,
|
|
186
297
|
flags.revision, entry.sessionId, JSON.stringify(entry.sessionIds), entry.lineage ? JSON.stringify(entry.lineage) : null,
|
|
187
|
-
entry.source ?? 'tool', entry.origin ?? 'agent', entry.baseKind ?? 'known'
|
|
298
|
+
entry.source ?? 'tool', entry.origin ?? 'agent', entry.baseKind ?? 'known',
|
|
299
|
+
entry.evidence ?? 'snapshot', entry.verdict ?? 'changed')
|
|
188
300
|
bumpVersion(cwd)
|
|
189
301
|
}
|
|
190
302
|
|
|
@@ -295,90 +407,106 @@ export function writeOp(
|
|
|
295
407
|
}
|
|
296
408
|
|
|
297
409
|
/**
|
|
298
|
-
* @author ddj 2026年10月29号
|
|
299
|
-
* @description:
|
|
410
|
+
* @author ddj 2026年10月29号 / 2026年11月25号
|
|
411
|
+
* @description: 写一条撤销行(同一路径覆盖写)。
|
|
300
412
|
* @param cwd 工作区
|
|
301
|
-
* @param
|
|
302
|
-
* @param entryId 涉及的条目
|
|
303
|
-
* @param snapshot 可重放快照
|
|
413
|
+
* @param record 撤销行内容
|
|
304
414
|
* @param now 时间
|
|
305
415
|
* @returns 无
|
|
306
416
|
*/
|
|
307
|
-
export function pushHistory(cwd: string,
|
|
308
|
-
insertHistory(pendingDb(cwd),
|
|
417
|
+
export function pushHistory(cwd: string, record: HistoryRecord, now: number): void {
|
|
418
|
+
insertHistory(pendingDb(cwd), record, now)
|
|
309
419
|
}
|
|
310
420
|
|
|
311
421
|
/**
|
|
312
422
|
* @private
|
|
313
|
-
* @author ddj 2026年11月03号
|
|
314
|
-
* @description:
|
|
423
|
+
* @author ddj 2026年11月03号 / 2026年11月25号
|
|
424
|
+
* @description: 写一条合并撤销行:同一路径永远只有一行,新判定覆盖写,不再堆叠。
|
|
315
425
|
* @param db 已迁移连接(可能已在事务中)
|
|
316
|
-
* @param
|
|
317
|
-
* @param entryId 涉及的条目
|
|
318
|
-
* @param snapshot 可重放快照
|
|
426
|
+
* @param record 撤销行内容
|
|
319
427
|
* @param now 时间
|
|
320
428
|
* @returns 无
|
|
321
429
|
*/
|
|
322
|
-
function insertHistory(db: DatabaseSync,
|
|
323
|
-
db.prepare(
|
|
324
|
-
|
|
325
|
-
.
|
|
430
|
+
function insertHistory(db: DatabaseSync, record: HistoryRecord, now: number): void {
|
|
431
|
+
db.prepare(`INSERT INTO pending_history(entry_id,path,kind,earlier,baseline,content,digest,wrote,state,merged_at)
|
|
432
|
+
VALUES(?,?,?,?,?,?,?,?,'applied',?)
|
|
433
|
+
ON CONFLICT(entry_id) DO UPDATE SET path=excluded.path,kind=excluded.kind,earlier=excluded.earlier,
|
|
434
|
+
baseline=excluded.baseline,content=excluded.content,digest=excluded.digest,wrote=excluded.wrote,
|
|
435
|
+
state='applied',merged_at=excluded.merged_at`)
|
|
436
|
+
.run(record.entryId, record.path, record.kind, record.earlier, record.baseline, record.content,
|
|
437
|
+
digestOf(record.content), record.wrote ? 1 : 0, now)
|
|
326
438
|
}
|
|
327
439
|
|
|
328
440
|
/**
|
|
329
|
-
* @author ddj 2026年10月30号
|
|
330
|
-
* @description:
|
|
441
|
+
* @author ddj 2026年10月30号 / 2026年11月25号
|
|
442
|
+
* @description: 取最近一条仍生效的撤销行,撤销就作用在它上面。
|
|
331
443
|
* @param cwd 工作区
|
|
332
444
|
* @returns 记录或 undefined
|
|
333
445
|
*/
|
|
334
446
|
export function latestApplied(cwd: string): HistoryRow | undefined {
|
|
335
447
|
const row = pendingDb(cwd).prepare(
|
|
336
|
-
"SELECT
|
|
448
|
+
"SELECT * FROM pending_history WHERE state='applied' ORDER BY merged_at DESC LIMIT 1",
|
|
337
449
|
).get() as Row | undefined
|
|
338
450
|
return row ? rowOfHistory(row) : undefined
|
|
339
451
|
}
|
|
340
452
|
|
|
341
453
|
/**
|
|
342
|
-
* @author ddj 2026年10月30号
|
|
343
|
-
* @description:
|
|
454
|
+
* @author ddj 2026年10月30号 / 2026年11月25号
|
|
455
|
+
* @description: 取最早一条被撤销的记录,重做按「撤销的逆序」重新生效。
|
|
344
456
|
* @param cwd 工作区
|
|
345
457
|
* @returns 记录或 undefined
|
|
346
458
|
*/
|
|
347
459
|
export function earliestUndone(cwd: string): HistoryRow | undefined {
|
|
348
460
|
const row = pendingDb(cwd).prepare(
|
|
349
|
-
"SELECT
|
|
461
|
+
"SELECT * FROM pending_history WHERE state='undone' ORDER BY merged_at ASC LIMIT 1",
|
|
350
462
|
).get() as Row | undefined
|
|
351
463
|
return row ? rowOfHistory(row) : undefined
|
|
352
464
|
}
|
|
353
465
|
|
|
354
466
|
/**
|
|
355
|
-
* @author ddj 2026年10月30号
|
|
356
|
-
* @description:
|
|
467
|
+
* @author ddj 2026年10月30号 / 2026年11月25号
|
|
468
|
+
* @description: 翻转一条撤销行的生效状态。
|
|
357
469
|
* @param cwd 工作区
|
|
358
|
-
* @param
|
|
470
|
+
* @param entryId 条目身份(一条路径一行)
|
|
359
471
|
* @param state applied / undone
|
|
360
472
|
* @returns 无
|
|
361
473
|
*/
|
|
362
|
-
export function setHistoryState(cwd: string,
|
|
363
|
-
pendingDb(cwd).prepare('UPDATE pending_history SET state=? WHERE
|
|
474
|
+
export function setHistoryState(cwd: string, entryId: string, state: 'applied' | 'undone'): void {
|
|
475
|
+
pendingDb(cwd).prepare('UPDATE pending_history SET state=? WHERE entry_id=?').run(state, entryId)
|
|
364
476
|
}
|
|
365
477
|
|
|
366
478
|
/**
|
|
367
|
-
* @author ddj 2026年10月30号
|
|
368
|
-
* @description:
|
|
479
|
+
* @author ddj 2026年10月30号 / 2026年11月25号
|
|
480
|
+
* @description: 把数据库行还原成撤销记录。
|
|
369
481
|
* @param row 数据库行
|
|
370
482
|
* @returns 记录
|
|
371
483
|
*/
|
|
372
484
|
function rowOfHistory(row: Row): HistoryRow {
|
|
373
|
-
return {
|
|
485
|
+
return {
|
|
486
|
+
entryId: String(row.entry_id), path: String(row.path),
|
|
487
|
+
kind: String(row.kind) === 'revert' ? 'revert' : 'keep',
|
|
488
|
+
earlier: String(row.earlier ?? 'file') === 'none' ? 'none' : 'file',
|
|
489
|
+
baseline: String(row.baseline), content: String(row.content),
|
|
490
|
+
wrote: Number(row.wrote) === 1, state: row.state === 'undone' ? 'undone' : 'applied',
|
|
491
|
+
mergedAt: Number(row.merged_at) || 0,
|
|
492
|
+
}
|
|
374
493
|
}
|
|
375
494
|
|
|
376
|
-
/**
|
|
377
|
-
export interface
|
|
378
|
-
id: number
|
|
379
|
-
kind: string
|
|
495
|
+
/** 一条撤销行的内容:被这次判定替换掉的两侧全文,用于撤销与重做。 */
|
|
496
|
+
export interface HistoryRecord {
|
|
380
497
|
entryId: string
|
|
381
|
-
|
|
498
|
+
path: string
|
|
499
|
+
kind: 'keep' | 'revert'
|
|
500
|
+
earlier: 'file' | 'none'
|
|
501
|
+
baseline: string
|
|
502
|
+
content: string
|
|
503
|
+
wrote: boolean
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/** 撤销行形态(一条路径一行)。 */
|
|
507
|
+
export interface HistoryRow extends HistoryRecord {
|
|
508
|
+
state: 'applied' | 'undone'
|
|
509
|
+
mergedAt: number
|
|
382
510
|
}
|
|
383
511
|
|
|
384
512
|
/**
|
|
@@ -407,6 +535,8 @@ function entryOfRow(row: Row): PendingEntry {
|
|
|
407
535
|
source: sourceOfRow(row),
|
|
408
536
|
origin: originOfRow(row),
|
|
409
537
|
baseKind: baseKindOfRow(row),
|
|
538
|
+
evidence: evidenceOfRow(row),
|
|
539
|
+
verdict: verdictOfRow(row),
|
|
410
540
|
}
|
|
411
541
|
if (Number(row.unseen) === 1) entry.unseen = true
|
|
412
542
|
if (lineage !== undefined) entry.lineage = lineage
|
|
@@ -447,7 +577,31 @@ function originOfRow(row: Row): PendingOrigin {
|
|
|
447
577
|
*/
|
|
448
578
|
function baseKindOfRow(row: Row): PendingBaseKind {
|
|
449
579
|
const value = String(row.base_kind ?? 'known')
|
|
450
|
-
return value === 'absent' || value === 'vcs' || value === 'unknown' ? value : 'known'
|
|
580
|
+
return value === 'absent' || value === 'vcs' || value === 'normalized' || value === 'unknown' ? value : 'known'
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* @private
|
|
585
|
+
* @author ddj 2026年11月25号
|
|
586
|
+
* @description: 读证据强度列;v4 旧库升级后该列有默认值,异常值按 `snapshot` 处理。
|
|
587
|
+
* @param row 数据库行
|
|
588
|
+
* @returns 证据强度
|
|
589
|
+
*/
|
|
590
|
+
function evidenceOfRow(row: Row): PendingEvidence {
|
|
591
|
+
const value = String(row.evidence ?? 'snapshot')
|
|
592
|
+
return value === 'authoritative' || value === 'receipt' || value === 'none' ? value : 'snapshot'
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* @private
|
|
597
|
+
* @author ddj 2026年11月25号
|
|
598
|
+
* @description: 读调用结局列;异常值按 `changed` 处理(旧条目的语义就是真实改动)。
|
|
599
|
+
* @param row 数据库行
|
|
600
|
+
* @returns 调用结局
|
|
601
|
+
*/
|
|
602
|
+
function verdictOfRow(row: Row): PendingVerdict {
|
|
603
|
+
const value = String(row.verdict ?? 'changed')
|
|
604
|
+
return value === 'noop' || value === 'failed' || value === 'unverified' ? value : 'changed'
|
|
451
605
|
}
|
|
452
606
|
|
|
453
607
|
/**
|
|
@@ -465,13 +619,12 @@ function flagsOfRow(row: Row): PendingFlags {
|
|
|
465
619
|
}
|
|
466
620
|
}
|
|
467
621
|
|
|
468
|
-
/** 批量 Keep 的一条落库记录:stored 提供 CAS
|
|
622
|
+
/** 批量 Keep 的一条落库记录:stored 提供 CAS 依据与撤销依据,next 是推进后的条目。 */
|
|
469
623
|
export interface KeepRecord {
|
|
470
624
|
entryId: string
|
|
471
625
|
stored: StoredEntry
|
|
472
626
|
next: StoredEntry
|
|
473
627
|
blocks: { id: string; decision: string }[]
|
|
474
|
-
snapshot: string
|
|
475
628
|
}
|
|
476
629
|
|
|
477
630
|
/**
|
|
@@ -488,7 +641,8 @@ export function saveDecisions(cwd: string, records: KeepRecord[]): { id: string;
|
|
|
488
641
|
const db = pendingDb(cwd)
|
|
489
642
|
const skipped: { id: string; reason: string }[] = []
|
|
490
643
|
const advance = db.prepare(`UPDATE pending_entries SET path=?,earlier=?,old_text=?,new_text=?,updated_at=?,unseen=?,
|
|
491
|
-
missing=?,diverged=?,oversized=?,revision=?,session_id=?,session_ids=?,lineage=?,source=?,origin=?,base_kind
|
|
644
|
+
missing=?,diverged=?,oversized=?,revision=?,session_id=?,session_ids=?,lineage=?,source=?,origin=?,base_kind=?,
|
|
645
|
+
evidence=?,verdict=?
|
|
492
646
|
WHERE id=? AND revision=?`)
|
|
493
647
|
|
|
494
648
|
db.exec('BEGIN IMMEDIATE')
|
|
@@ -501,6 +655,7 @@ export function saveDecisions(cwd: string, records: KeepRecord[]): { id: string;
|
|
|
501
655
|
flags.revision, entry.sessionId, JSON.stringify(entry.sessionIds),
|
|
502
656
|
entry.lineage ? JSON.stringify(entry.lineage) : null,
|
|
503
657
|
entry.source ?? 'tool', entry.origin ?? 'agent', entry.baseKind ?? 'known',
|
|
658
|
+
entry.evidence ?? 'snapshot', entry.verdict ?? 'changed',
|
|
504
659
|
record.entryId, expected)
|
|
505
660
|
// 读条目到落库之间被别的决定推进过:只放弃这一条
|
|
506
661
|
if (changed.changes !== 1) {
|
|
@@ -508,7 +663,16 @@ export function saveDecisions(cwd: string, records: KeepRecord[]): { id: string;
|
|
|
508
663
|
continue
|
|
509
664
|
}
|
|
510
665
|
writeBlocks(db, record.entryId, record.blocks)
|
|
511
|
-
|
|
666
|
+
// 撤销行每条路径只留一行:批量 Keep 也按同一口径覆盖写,不再追加
|
|
667
|
+
insertHistory(db, {
|
|
668
|
+
entryId: record.entryId,
|
|
669
|
+
path: record.stored.entry.path,
|
|
670
|
+
kind: 'keep',
|
|
671
|
+
earlier: record.stored.entry.earlierVersion,
|
|
672
|
+
baseline: record.stored.entry.oldText,
|
|
673
|
+
content: record.stored.entry.newText,
|
|
674
|
+
wrote: false,
|
|
675
|
+
}, Date.now())
|
|
512
676
|
}
|
|
513
677
|
db.exec('COMMIT')
|
|
514
678
|
if (records.length) bumpVersion(cwd)
|
|
@@ -518,3 +682,89 @@ export function saveDecisions(cwd: string, records: KeepRecord[]): { id: string;
|
|
|
518
682
|
throw error
|
|
519
683
|
}
|
|
520
684
|
}
|
|
685
|
+
|
|
686
|
+
/** 清理筛选条件:只按「采集通道 + 基线可信度 + 时间窗」命中,不猜内容。 */
|
|
687
|
+
export interface PurgeFilter {
|
|
688
|
+
/** 采集通道(缺省 watch)。 */
|
|
689
|
+
sources?: string[]
|
|
690
|
+
/** 基线可信度(缺省 absent)。 */
|
|
691
|
+
baseKinds?: string[]
|
|
692
|
+
/** 只清这个时间之前更新的条目(毫秒时间戳)。 */
|
|
693
|
+
before?: number
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/** 一次清理的结果。 */
|
|
697
|
+
export interface PurgeOutcome {
|
|
698
|
+
/** 命中多少条(dryRun 也算)。 */
|
|
699
|
+
matched: number
|
|
700
|
+
/** 真正删掉多少条;dryRun 恒为 0。 */
|
|
701
|
+
removed: number
|
|
702
|
+
/** 命中但因为有判定历史而保留的条数。 */
|
|
703
|
+
kept: number
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* @public
|
|
708
|
+
* @author ddj 2026-11-24
|
|
709
|
+
* @description: 按条件清理待审条目(清幽灵记录用)。命中判定与删除在同一份读结果上做:
|
|
710
|
+
* 有 pending_history 的条目(已经 Keep/Revert 过)一律保留,绝不按内容猜。
|
|
711
|
+
* @param cwd 工作区
|
|
712
|
+
* @param filter 筛选条件
|
|
713
|
+
* @param dryRun 只数不删
|
|
714
|
+
* @returns 命中、删除与保留条数
|
|
715
|
+
* @throws 事务级异常整体回滚后抛出
|
|
716
|
+
*/
|
|
717
|
+
export function purgeEntries(cwd: string, filter: PurgeFilter = {}, dryRun = false): PurgeOutcome {
|
|
718
|
+
const db = pendingDb(cwd)
|
|
719
|
+
const sources = filter.sources && filter.sources.length ? new Set(filter.sources) : null
|
|
720
|
+
const baseKinds = filter.baseKinds && filter.baseKinds.length ? new Set(filter.baseKinds) : null
|
|
721
|
+
const before = typeof filter.before === 'number' ? filter.before : null
|
|
722
|
+
// 只读三列,不把两侧全文拉进内存(大库上单条全文可达百 KB)
|
|
723
|
+
const rows = db.prepare(`SELECT id, source, base_kind, updated_at,
|
|
724
|
+
EXISTS(SELECT 1 FROM pending_history h WHERE h.entry_id = pending_entries.id) AS decided
|
|
725
|
+
FROM pending_entries`).all() as Row[]
|
|
726
|
+
|
|
727
|
+
const hits: string[] = []
|
|
728
|
+
let kept = 0
|
|
729
|
+
for (const row of rows) {
|
|
730
|
+
if (sources && !sources.has(String(row.source))) continue
|
|
731
|
+
if (baseKinds && !baseKinds.has(String(row.base_kind))) continue
|
|
732
|
+
if (before !== null && Number(row.updated_at) >= before) continue
|
|
733
|
+
if (Number(row.decided) === 1) kept += 1
|
|
734
|
+
else hits.push(String(row.id))
|
|
735
|
+
}
|
|
736
|
+
if (dryRun || !hits.length) return { matched: hits.length, removed: 0, kept }
|
|
737
|
+
|
|
738
|
+
db.exec('BEGIN IMMEDIATE')
|
|
739
|
+
try {
|
|
740
|
+
const dropEntryRow = db.prepare('DELETE FROM pending_entries WHERE id=?')
|
|
741
|
+
const dropBlockRows = db.prepare('DELETE FROM pending_blocks WHERE entry_id=?')
|
|
742
|
+
for (const id of hits) {
|
|
743
|
+
dropEntryRow.run(id)
|
|
744
|
+
dropBlockRows.run(id)
|
|
745
|
+
}
|
|
746
|
+
db.exec('COMMIT')
|
|
747
|
+
} catch (error) {
|
|
748
|
+
if (db.isTransaction) db.exec('ROLLBACK')
|
|
749
|
+
throw error
|
|
750
|
+
}
|
|
751
|
+
bumpVersion(cwd)
|
|
752
|
+
return { matched: hits.length, removed: hits.length, kept }
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
/**
|
|
756
|
+
* @public
|
|
757
|
+
* @author ddj 2026-11-24
|
|
758
|
+
* @description: 回收已删行占用的页;失败只记日志,不影响已完成的删除。
|
|
759
|
+
* @param cwd 工作区
|
|
760
|
+
* @returns 耗时毫秒
|
|
761
|
+
*/
|
|
762
|
+
export function vacuumEntries(cwd: string): number {
|
|
763
|
+
const started = Date.now()
|
|
764
|
+
try {
|
|
765
|
+
pendingDb(cwd).exec('VACUUM')
|
|
766
|
+
} catch (error) {
|
|
767
|
+
log.warn('待审库 VACUUM 失败:' + String(error))
|
|
768
|
+
}
|
|
769
|
+
return Date.now() - started
|
|
770
|
+
}
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* 作者 ddj 2026-11-24
|
|
12
12
|
*/
|
|
13
13
|
import { createHash } from 'node:crypto'
|
|
14
|
-
import type { PendingBaseKind, PendingOrigin, PendingSource } from '../shared/pendingReview.js'
|
|
14
|
+
import type { PendingBaseKind, PendingEvidence, PendingOrigin, PendingSource } from '../shared/pendingReview.js'
|
|
15
15
|
import { pathKeyFor } from '../shared/pathKey.js'
|
|
16
16
|
|
|
17
17
|
// --region 常量
|
|
@@ -49,6 +49,11 @@ const SOURCE_RANK: Record<PendingSource, number> = {
|
|
|
49
49
|
edit: 6, write: 6, remote_ssh_write: 5, tool: 4, watch: 3, scan: 2, vcs: 1,
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
/** 证据强度:回执说过的话比自己的快照可信,快照又胜过没有任何依据。 */
|
|
53
|
+
const EVIDENCE_RANK: Record<PendingEvidence, number> = {
|
|
54
|
+
authoritative: 4, receipt: 3, snapshot: 2, none: 1,
|
|
55
|
+
}
|
|
56
|
+
|
|
52
57
|
// --endregion
|
|
53
58
|
|
|
54
59
|
// --region 纯函数
|
|
@@ -87,6 +92,32 @@ export function strongerSource(earlier: PendingSource | undefined, later: Pendin
|
|
|
87
92
|
return (SOURCE_RANK[left] >= SOURCE_RANK[right] ? left : right)
|
|
88
93
|
}
|
|
89
94
|
|
|
95
|
+
/**
|
|
96
|
+
* @author ddj 2026年11月25号
|
|
97
|
+
* @description: 合并证据强度:回执胜过快照,同级保留首次值。
|
|
98
|
+
* @param earlier 已落库证据(可为空)
|
|
99
|
+
* @param later 本次证据
|
|
100
|
+
* @returns 应当落库的证据强度
|
|
101
|
+
*/
|
|
102
|
+
export function strongerEvidence(earlier: PendingEvidence | undefined, later: PendingEvidence | undefined): PendingEvidence {
|
|
103
|
+
const left = earlier ?? 'snapshot'
|
|
104
|
+
const right = later ?? 'snapshot'
|
|
105
|
+
return (EVIDENCE_RANK[left] >= EVIDENCE_RANK[right] ? left : right)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* @author ddj 2026年11月25号
|
|
110
|
+
* @description: 证据徽标文案;旧条目没有该字段时按快照处理。
|
|
111
|
+
* @param evidence 证据强度
|
|
112
|
+
* @returns 展示文案
|
|
113
|
+
*/
|
|
114
|
+
export function evidenceLabelOf(evidence?: PendingEvidence): string {
|
|
115
|
+
if (evidence === 'authoritative') return '回执已核'
|
|
116
|
+
if (evidence === 'receipt') return '回执对账'
|
|
117
|
+
if (evidence === 'none') return '无回执'
|
|
118
|
+
return '快照推断'
|
|
119
|
+
}
|
|
120
|
+
|
|
90
121
|
/**
|
|
91
122
|
* @author ddj 2026-11-24
|
|
92
123
|
* @description: 去重键:路径比较键 + 内容 sha256。只按内容判同一变化,不看时间。
|
|
@@ -99,8 +130,8 @@ export function dedupeKey(pathKey: string, content: string | null): string {
|
|
|
99
130
|
}
|
|
100
131
|
|
|
101
132
|
/**
|
|
102
|
-
* @author ddj 2026-11-24
|
|
103
|
-
* @description: 撤销白名单:只有 agent
|
|
133
|
+
* @author ddj 2026-11-24 / 2026年11月25号
|
|
134
|
+
* @description: 撤销白名单:只有 agent 来源且基线可信时才可以直接撤销;归一基线要确认。
|
|
104
135
|
* @param origin 归属
|
|
105
136
|
* @param baseKind 基线与可信度
|
|
106
137
|
* @returns 可直接撤销 / 需确认 / 禁止
|
|
@@ -109,23 +140,26 @@ export function revertPolicyOf(
|
|
|
109
140
|
origin: PendingOrigin | undefined,
|
|
110
141
|
baseKind: PendingBaseKind | undefined,
|
|
111
142
|
): 'direct' | 'confirm' | 'blocked' {
|
|
112
|
-
|
|
143
|
+
const base = baseKind ?? 'known'
|
|
144
|
+
if (base === 'unknown') return 'blocked'
|
|
145
|
+
if (base === 'normalized') return 'confirm'
|
|
113
146
|
return (origin ?? 'agent') === 'agent' ? 'direct' : 'confirm'
|
|
114
147
|
}
|
|
115
148
|
|
|
116
149
|
/**
|
|
117
|
-
* @author ddj 2026-11-24
|
|
118
|
-
* @description:
|
|
150
|
+
* @author ddj 2026-11-24 / 2026年11月25号
|
|
151
|
+
* @description: 条目来源徽标文案;官方行没有徽标,待审条目在来源后附证据强度。
|
|
119
152
|
* @param entry 待审条目的来源字段
|
|
120
153
|
* @returns 展示文案
|
|
121
154
|
*/
|
|
122
|
-
export function sourceLabelOf(entry: { source?: PendingSource; origin?: PendingOrigin }): string {
|
|
155
|
+
export function sourceLabelOf(entry: { source?: PendingSource; origin?: PendingOrigin; evidence?: PendingEvidence }): string {
|
|
123
156
|
const source = entry.source ?? 'tool'
|
|
124
157
|
const origin = entry.origin ?? 'agent'
|
|
125
|
-
|
|
126
|
-
if (origin === '
|
|
127
|
-
if (origin === '
|
|
128
|
-
|
|
158
|
+
const badge = ' · ' + evidenceLabelOf(entry.evidence)
|
|
159
|
+
if (origin === 'agent') return source + ' · agent' + badge
|
|
160
|
+
if (origin === 'editor') return '编辑器保存' + badge
|
|
161
|
+
if (origin === 'vcs') return '版本库同步' + badge
|
|
162
|
+
return source + ' · 外部' + badge
|
|
129
163
|
}
|
|
130
164
|
|
|
131
165
|
// --endregion
|