dsh-vscode-mode 0.36.0 → 0.37.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 +16 -4
- package/lib/client.js +59 -59
- package/lib/client.js.map +1 -1
- package/lib/index.js +507 -10
- 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 +319 -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/index.ts +4 -0
- package/src/pending/externalCapture.ts +100 -4
- package/src/pending/persist.ts +87 -0
- package/src/pending/store.ts +37 -2
- package/src/pending/vcs.ts +25 -7
- package/src/pendingRpc.ts +19 -1
- package/src/rpc.ts +23 -0
- package/src/shared/rpc.ts +4 -0
package/lib/index.js
CHANGED
|
@@ -3829,6 +3829,75 @@ function saveDecisions(cwd, records) {
|
|
|
3829
3829
|
throw error;
|
|
3830
3830
|
}
|
|
3831
3831
|
}
|
|
3832
|
+
/**
|
|
3833
|
+
* @public
|
|
3834
|
+
* @author ddj 2026-11-24
|
|
3835
|
+
* @description: 按条件清理待审条目(清幽灵记录用)。命中判定与删除在同一份读结果上做:
|
|
3836
|
+
* 有 pending_history 的条目(已经 Keep/Revert 过)一律保留,绝不按内容猜。
|
|
3837
|
+
* @param cwd 工作区
|
|
3838
|
+
* @param filter 筛选条件
|
|
3839
|
+
* @param dryRun 只数不删
|
|
3840
|
+
* @returns 命中、删除与保留条数
|
|
3841
|
+
* @throws 事务级异常整体回滚后抛出
|
|
3842
|
+
*/
|
|
3843
|
+
function purgeEntries(cwd, filter = {}, dryRun = false) {
|
|
3844
|
+
const db = pendingDb(cwd);
|
|
3845
|
+
const sources = filter.sources && filter.sources.length ? new Set(filter.sources) : null;
|
|
3846
|
+
const baseKinds = filter.baseKinds && filter.baseKinds.length ? new Set(filter.baseKinds) : null;
|
|
3847
|
+
const before = typeof filter.before === "number" ? filter.before : null;
|
|
3848
|
+
const rows = db.prepare(`SELECT id, source, base_kind, updated_at,
|
|
3849
|
+
EXISTS(SELECT 1 FROM pending_history h WHERE h.entry_id = pending_entries.id) AS decided
|
|
3850
|
+
FROM pending_entries`).all();
|
|
3851
|
+
const hits = [];
|
|
3852
|
+
let kept = 0;
|
|
3853
|
+
for (const row of rows) {
|
|
3854
|
+
if (sources && !sources.has(String(row.source))) continue;
|
|
3855
|
+
if (baseKinds && !baseKinds.has(String(row.base_kind))) continue;
|
|
3856
|
+
if (before !== null && Number(row.updated_at) >= before) continue;
|
|
3857
|
+
if (Number(row.decided) === 1) kept += 1;
|
|
3858
|
+
else hits.push(String(row.id));
|
|
3859
|
+
}
|
|
3860
|
+
if (dryRun || !hits.length) return {
|
|
3861
|
+
matched: hits.length,
|
|
3862
|
+
removed: 0,
|
|
3863
|
+
kept
|
|
3864
|
+
};
|
|
3865
|
+
db.exec("BEGIN IMMEDIATE");
|
|
3866
|
+
try {
|
|
3867
|
+
const dropEntryRow = db.prepare("DELETE FROM pending_entries WHERE id=?");
|
|
3868
|
+
const dropBlockRows = db.prepare("DELETE FROM pending_blocks WHERE entry_id=?");
|
|
3869
|
+
for (const id of hits) {
|
|
3870
|
+
dropEntryRow.run(id);
|
|
3871
|
+
dropBlockRows.run(id);
|
|
3872
|
+
}
|
|
3873
|
+
db.exec("COMMIT");
|
|
3874
|
+
} catch (error) {
|
|
3875
|
+
if (db.isTransaction) db.exec("ROLLBACK");
|
|
3876
|
+
throw error;
|
|
3877
|
+
}
|
|
3878
|
+
bumpVersion(cwd);
|
|
3879
|
+
return {
|
|
3880
|
+
matched: hits.length,
|
|
3881
|
+
removed: hits.length,
|
|
3882
|
+
kept
|
|
3883
|
+
};
|
|
3884
|
+
}
|
|
3885
|
+
/**
|
|
3886
|
+
* @public
|
|
3887
|
+
* @author ddj 2026-11-24
|
|
3888
|
+
* @description: 回收已删行占用的页;失败只记日志,不影响已完成的删除。
|
|
3889
|
+
* @param cwd 工作区
|
|
3890
|
+
* @returns 耗时毫秒
|
|
3891
|
+
*/
|
|
3892
|
+
function vacuumEntries(cwd) {
|
|
3893
|
+
const started = Date.now();
|
|
3894
|
+
try {
|
|
3895
|
+
pendingDb(cwd).exec("VACUUM");
|
|
3896
|
+
} catch (error) {
|
|
3897
|
+
log.warn("待审库 VACUUM 失败:" + String(error));
|
|
3898
|
+
}
|
|
3899
|
+
return Date.now() - started;
|
|
3900
|
+
}
|
|
3832
3901
|
//#endregion
|
|
3833
3902
|
//#region src/store.ts
|
|
3834
3903
|
const READ_CAP$1 = 8388608;
|
|
@@ -12411,12 +12480,20 @@ async function runCommand(shell, command, workdir, stdoutMaxBytes = STDOUT_BUDGE
|
|
|
12411
12480
|
* 非 git/svn(含 Perforce 与无版本库)一律返回 null,由调用方降级为「只对齐已记录条目」。
|
|
12412
12481
|
* @param shell shell 能力
|
|
12413
12482
|
* @param cwd 工作区
|
|
12483
|
+
* @param onFailure 失败原因回调(可选):调用方用它把「为什么没有状态」写进采集缺口
|
|
12414
12484
|
* @returns 状态集合或 null
|
|
12415
12485
|
*/
|
|
12416
|
-
async function vcsStatus(shell, cwd) {
|
|
12486
|
+
async function vcsStatus(shell, cwd, onFailure) {
|
|
12417
12487
|
const detected = await detectVcsRoot(cwd);
|
|
12418
|
-
if (!detected
|
|
12419
|
-
|
|
12488
|
+
if (!detected) {
|
|
12489
|
+
onFailure?.("不是 git/svn 工作副本");
|
|
12490
|
+
return null;
|
|
12491
|
+
}
|
|
12492
|
+
if (detected.kind === "p4") {
|
|
12493
|
+
onFailure?.("Perforce 工作副本不支持状态探测");
|
|
12494
|
+
return null;
|
|
12495
|
+
}
|
|
12496
|
+
return detected.kind === "git" ? gitStatus(shell, detected.root, onFailure) : svnStatus(shell, detected.root, onFailure);
|
|
12420
12497
|
}
|
|
12421
12498
|
/**
|
|
12422
12499
|
* @private
|
|
@@ -12424,11 +12501,15 @@ async function vcsStatus(shell, cwd) {
|
|
|
12424
12501
|
* @description: git 状态:一条 status 命令解析出改动集合。
|
|
12425
12502
|
* @param shell shell 能力
|
|
12426
12503
|
* @param root 仓库根
|
|
12504
|
+
* @param onFailure 失败原因回调(可选)
|
|
12427
12505
|
* @returns 状态集合或 null(命令失败)
|
|
12428
12506
|
*/
|
|
12429
|
-
async function gitStatus(shell, root) {
|
|
12507
|
+
async function gitStatus(shell, root, onFailure) {
|
|
12430
12508
|
const status = await runCommand(shell, "git -c status.renames=false status --porcelain=v1 -z --untracked-files=all", root);
|
|
12431
|
-
if (!status.ok)
|
|
12509
|
+
if (!status.ok) {
|
|
12510
|
+
onFailure?.("git status 失败:" + status.stderr.slice(0, 200));
|
|
12511
|
+
return null;
|
|
12512
|
+
}
|
|
12432
12513
|
const entries = /* @__PURE__ */ new Map();
|
|
12433
12514
|
for (const entry of parseGitStatus(status.stdout)) {
|
|
12434
12515
|
const absolute = join(root, ...entry.path.split(GIT_SEPARATOR));
|
|
@@ -12450,11 +12531,15 @@ async function gitStatus(shell, root) {
|
|
|
12450
12531
|
* @description: svn 状态:一条 status --xml 解析出改动集合。
|
|
12451
12532
|
* @param shell shell 能力
|
|
12452
12533
|
* @param root 工作副本根
|
|
12534
|
+
* @param onFailure 失败原因回调(可选)
|
|
12453
12535
|
* @returns 状态集合或 null(命令失败)
|
|
12454
12536
|
*/
|
|
12455
|
-
async function svnStatus(shell, root) {
|
|
12537
|
+
async function svnStatus(shell, root, onFailure) {
|
|
12456
12538
|
const status = await runCommand(shell, "svn status --xml", root);
|
|
12457
|
-
if (!status.ok)
|
|
12539
|
+
if (!status.ok) {
|
|
12540
|
+
onFailure?.("svn status 失败:" + status.stderr.slice(0, 200));
|
|
12541
|
+
return null;
|
|
12542
|
+
}
|
|
12458
12543
|
const entries = /* @__PURE__ */ new Map();
|
|
12459
12544
|
for (const entry of parseSvnStatus(status.stdout)) {
|
|
12460
12545
|
const absolute = resolve(root, entry.path);
|
|
@@ -12866,7 +12951,11 @@ const VCS_WRITE_TOOLS = /* @__PURE__ */ new Set([
|
|
|
12866
12951
|
"svn_changelist",
|
|
12867
12952
|
"remote_ssh_write"
|
|
12868
12953
|
]);
|
|
12869
|
-
/**
|
|
12954
|
+
/**
|
|
12955
|
+
* 额外忽略的目录名(在搜索排除项之外,按原样大小写敏感匹配,口径与 EXCLUDES 一致)。
|
|
12956
|
+
* 只收版本库元数据、Unity 缓存与构建产物:第三方与代码生成目录不在忽略之列,
|
|
12957
|
+
* 它们的改动靠「mtime 证据 + 版本库状态」两道闸门拦,而不是按目录名一刀切。
|
|
12958
|
+
*/
|
|
12870
12959
|
const EXTRA_IGNORE_SEGMENTS = [
|
|
12871
12960
|
".svn",
|
|
12872
12961
|
".hg",
|
|
@@ -12941,7 +13030,10 @@ function installExternalCapture(deps) {
|
|
|
12941
13030
|
status: void 0,
|
|
12942
13031
|
alignedAt: 0,
|
|
12943
13032
|
alignTimer: null,
|
|
12944
|
-
requestTimer: null
|
|
13033
|
+
requestTimer: null,
|
|
13034
|
+
startedAt: 0,
|
|
13035
|
+
gapKeys: /* @__PURE__ */ new Set(),
|
|
13036
|
+
statusNoteAt: 0
|
|
12945
13037
|
};
|
|
12946
13038
|
spaces.set(key, created);
|
|
12947
13039
|
return created;
|
|
@@ -12978,6 +13070,7 @@ function installExternalCapture(deps) {
|
|
|
12978
13070
|
*/
|
|
12979
13071
|
function startWatch(space) {
|
|
12980
13072
|
if (space.watchers.length || stopped) return;
|
|
13073
|
+
space.startedAt = now();
|
|
12981
13074
|
try {
|
|
12982
13075
|
const watcher = watch(space.cwd, {
|
|
12983
13076
|
recursive: true,
|
|
@@ -13062,11 +13155,78 @@ function installExternalCapture(deps) {
|
|
|
13062
13155
|
return space.status;
|
|
13063
13156
|
}
|
|
13064
13157
|
const scope = vcsScopeOf(deps.ctx, space.cwd, space.sessionId);
|
|
13065
|
-
|
|
13158
|
+
if (!scope) {
|
|
13159
|
+
noteStatusOnce(space, "没有 shell 能力,版本库状态不可用");
|
|
13160
|
+
space.status = null;
|
|
13161
|
+
return space.status;
|
|
13162
|
+
}
|
|
13163
|
+
space.status = await vcsStatus(scope.shell, space.cwd, (reason) => noteStatusOnce(space, reason));
|
|
13066
13164
|
return space.status;
|
|
13067
13165
|
}
|
|
13068
13166
|
/**
|
|
13069
13167
|
* @private
|
|
13168
|
+
* @author ddj 2026-11-24
|
|
13169
|
+
* @description: 读文件修改时间(测试可注入);读不到返回 undefined。
|
|
13170
|
+
* @param space 工作区状态
|
|
13171
|
+
* @param absolute 绝对路径
|
|
13172
|
+
* @returns 毫秒时间戳或 undefined
|
|
13173
|
+
*/
|
|
13174
|
+
function mtimeOf(space, absolute) {
|
|
13175
|
+
if (deps.mtime) return deps.mtime(space.cwd, absolute);
|
|
13176
|
+
try {
|
|
13177
|
+
return statSync(absolute).mtimeMs;
|
|
13178
|
+
} catch {
|
|
13179
|
+
return;
|
|
13180
|
+
}
|
|
13181
|
+
}
|
|
13182
|
+
/**
|
|
13183
|
+
* @private
|
|
13184
|
+
* @author ddj 2026-11-24
|
|
13185
|
+
* @description: 判断文件是否在本次监听开始之后被写过:这是「拿不到版本库状态」时唯一的外部证据。
|
|
13186
|
+
* @param space 工作区状态
|
|
13187
|
+
* @param absolute 绝对路径
|
|
13188
|
+
* @returns 刚被写过返回 true
|
|
13189
|
+
*/
|
|
13190
|
+
function recentlyWritten(space, absolute) {
|
|
13191
|
+
if (!space.startedAt) return false;
|
|
13192
|
+
const mtime = mtimeOf(space, absolute);
|
|
13193
|
+
return mtime !== void 0 && mtime >= space.startedAt - 2e3;
|
|
13194
|
+
}
|
|
13195
|
+
/**
|
|
13196
|
+
* @private
|
|
13197
|
+
* @author ddj 2026-11-24
|
|
13198
|
+
* @description: 同一条缺口只上报一次;去重表满了就丢最早的键,保证内存有界。
|
|
13199
|
+
* @param space 工作区状态
|
|
13200
|
+
* @param key 去重键
|
|
13201
|
+
* @param absolute 出问题的路径
|
|
13202
|
+
* @param message 原因
|
|
13203
|
+
* @returns 无
|
|
13204
|
+
*/
|
|
13205
|
+
function noteGapOnce(space, key, absolute, message) {
|
|
13206
|
+
if (space.gapKeys.has(key)) return;
|
|
13207
|
+
if (space.gapKeys.size >= 500) {
|
|
13208
|
+
const oldest = space.gapKeys.values().next().value;
|
|
13209
|
+
if (oldest !== void 0) space.gapKeys.delete(oldest);
|
|
13210
|
+
}
|
|
13211
|
+
space.gapKeys.add(key);
|
|
13212
|
+
noteCaptureGap(space.cwd, space.sessionId, absolute, message);
|
|
13213
|
+
}
|
|
13214
|
+
/**
|
|
13215
|
+
* @private
|
|
13216
|
+
* @author ddj 2026-11-24
|
|
13217
|
+
* @description: 「版本库状态不可用」按工作区节流上报,让面板能说清为什么没拦住噪声。
|
|
13218
|
+
* @param space 工作区状态
|
|
13219
|
+
* @param reason 失败原因(命令回执头 200 字)
|
|
13220
|
+
* @returns 无
|
|
13221
|
+
*/
|
|
13222
|
+
function noteStatusOnce(space, reason) {
|
|
13223
|
+
const at = now();
|
|
13224
|
+
if (at - space.statusNoteAt < 3e5) return;
|
|
13225
|
+
space.statusNoteAt = at;
|
|
13226
|
+
noteCaptureGap(space.cwd, space.sessionId, space.cwd, "版本库状态不可用:" + reason.slice(0, 200));
|
|
13227
|
+
}
|
|
13228
|
+
/**
|
|
13229
|
+
* @private
|
|
13070
13230
|
* @description: 处理一个路径:判定链见文件头注释。
|
|
13071
13231
|
* @param space 工作区状态
|
|
13072
13232
|
* @param absolute 绝对路径
|
|
@@ -13102,6 +13262,11 @@ function installExternalCapture(deps) {
|
|
|
13102
13262
|
deps.record(writeOf(space, absolute, stored.entry.newText, content, at, "vcs", "vcs", "known"));
|
|
13103
13263
|
return true;
|
|
13104
13264
|
}
|
|
13265
|
+
if (!status && !stored && !space.seen.has(key) && !recentlyWritten(space, absolute)) {
|
|
13266
|
+
noteGapOnce(space, "absent:" + key, absolute, "无版本库状态且文件未改动,未建档");
|
|
13267
|
+
space.seen.set(key, content);
|
|
13268
|
+
return false;
|
|
13269
|
+
}
|
|
13105
13270
|
const before = await beforeOf(space, key, absolute, stored, status, content);
|
|
13106
13271
|
if (before.text === null && content === null) {
|
|
13107
13272
|
space.seen.set(key, null);
|
|
@@ -14871,6 +15036,32 @@ function removeEntry(cwd, id, revision) {
|
|
|
14871
15036
|
dropEntry(cwd, id);
|
|
14872
15037
|
return true;
|
|
14873
15038
|
}
|
|
15039
|
+
/** 清库默认口径:只清 watch 通道、空基线的条目——幽灵记录就长这样。 */
|
|
15040
|
+
const PURGE_DEFAULT_SOURCES = ["watch"];
|
|
15041
|
+
/** 清库默认基线口径:空基线(改前内容缺失)。 */
|
|
15042
|
+
const PURGE_DEFAULT_BASE_KINDS = ["absent"];
|
|
15043
|
+
/**
|
|
15044
|
+
* @public
|
|
15045
|
+
* @author ddj 2026-11-24
|
|
15046
|
+
* @description: 清掉指定通道的待审条目(幽灵记录治理入口)。dryRun 只报数;
|
|
15047
|
+
* 真删之后按需 VACUUM,把已删行占的页还给磁盘。
|
|
15048
|
+
* @param cwd 工作区
|
|
15049
|
+
* @param filter 筛选条件(缺省 watch + absent)
|
|
15050
|
+
* @param options dryRun 只数不删;vacuum 删除后是否回收
|
|
15051
|
+
* @returns 命中、删除、保留条数与 VACUUM 耗时
|
|
15052
|
+
*/
|
|
15053
|
+
function purgePending(cwd, filter = {}, options = {}) {
|
|
15054
|
+
const outcome = purgeEntries(cwd, {
|
|
15055
|
+
sources: filter.sources && filter.sources.length ? filter.sources : PURGE_DEFAULT_SOURCES,
|
|
15056
|
+
baseKinds: filter.baseKinds && filter.baseKinds.length ? filter.baseKinds : PURGE_DEFAULT_BASE_KINDS,
|
|
15057
|
+
before: filter.before
|
|
15058
|
+
}, options.dryRun === true);
|
|
15059
|
+
if (options.dryRun || !outcome.removed || !options.vacuum) return outcome;
|
|
15060
|
+
return {
|
|
15061
|
+
...outcome,
|
|
15062
|
+
vacuumMs: vacuumEntries(cwd)
|
|
15063
|
+
};
|
|
15064
|
+
}
|
|
14874
15065
|
/**
|
|
14875
15066
|
* @author ddj 2026年10月29号
|
|
14876
15067
|
* @description: 取错误码用于拒绝原因,没有则回落到消息文本。
|
|
@@ -14881,6 +15072,253 @@ function reasonOf(error) {
|
|
|
14881
15072
|
const code = error.code;
|
|
14882
15073
|
return typeof code === "string" ? code : String(error);
|
|
14883
15074
|
}
|
|
15075
|
+
/** 真删前备份文件名后缀。 */
|
|
15076
|
+
const BACKUP_SUFFIX = ".before-purge.bak";
|
|
15077
|
+
/**
|
|
15078
|
+
* @author ddj 2026-11-24
|
|
15079
|
+
* @description: 挑出要删的批次:先按保留期,再按每路径上限,未判完的整批跳过。
|
|
15080
|
+
* @param db 工作区归档库连接
|
|
15081
|
+
* @param options 清理选项
|
|
15082
|
+
* @returns 候选批次 id 与两个统计口径
|
|
15083
|
+
*/
|
|
15084
|
+
function pickCandidates(db, options) {
|
|
15085
|
+
const retentionDays = options.retentionDays ?? 30;
|
|
15086
|
+
const keepBatches = options.keepBatches ?? 20;
|
|
15087
|
+
const cutoff = Date.now() - retentionDays * 24 * 60 * 60 * 1e3;
|
|
15088
|
+
const rows = db.prepare(`SELECT b.id, b.path, b.at, b.pending,
|
|
15089
|
+
(SELECT COUNT(*) FROM records r WHERE r.batch_id = b.id AND r.pending > 0) AS open_records
|
|
15090
|
+
FROM batches b ORDER BY b.path, b.at DESC, b.id DESC`).all();
|
|
15091
|
+
const seen = /* @__PURE__ */ new Map();
|
|
15092
|
+
const picked = /* @__PURE__ */ new Set();
|
|
15093
|
+
let expired = 0;
|
|
15094
|
+
let overCap = 0;
|
|
15095
|
+
let keptPending = 0;
|
|
15096
|
+
for (const row of rows) {
|
|
15097
|
+
const id = Number(row.id);
|
|
15098
|
+
const path = String(row.path);
|
|
15099
|
+
const order = (seen.get(path) ?? 0) + 1;
|
|
15100
|
+
seen.set(path, order);
|
|
15101
|
+
const at = Date.parse(String(row.at));
|
|
15102
|
+
const isExpired = Number.isFinite(at) && at < cutoff;
|
|
15103
|
+
const isOverCap = order > keepBatches;
|
|
15104
|
+
if (!isExpired && !isOverCap) continue;
|
|
15105
|
+
if (isExpired) expired += 1;
|
|
15106
|
+
if (isOverCap) overCap += 1;
|
|
15107
|
+
if (Number(row.pending) > 0 || Number(row.open_records) > 0) {
|
|
15108
|
+
keptPending += 1;
|
|
15109
|
+
continue;
|
|
15110
|
+
}
|
|
15111
|
+
picked.add(id);
|
|
15112
|
+
}
|
|
15113
|
+
return {
|
|
15114
|
+
ids: [...picked],
|
|
15115
|
+
expired,
|
|
15116
|
+
overCap,
|
|
15117
|
+
keptPending
|
|
15118
|
+
};
|
|
15119
|
+
}
|
|
15120
|
+
/**
|
|
15121
|
+
* @author ddj 2026-11-24
|
|
15122
|
+
* @description: 截断撤销栈与幂等表:每个条目只留最近若干步,避免这两张表跟着天长日久。
|
|
15123
|
+
* @param db 工作区归档库连接
|
|
15124
|
+
* @param keepHistory 每条目保留步数
|
|
15125
|
+
* @returns 删除的历史行数
|
|
15126
|
+
*/
|
|
15127
|
+
function trimPendingTables(db, keepHistory) {
|
|
15128
|
+
const trimmed = db.prepare(`DELETE FROM pending_history WHERE id NOT IN (
|
|
15129
|
+
SELECT id FROM (
|
|
15130
|
+
SELECT id, ROW_NUMBER() OVER (PARTITION BY entry_id ORDER BY id DESC) AS rn FROM pending_history
|
|
15131
|
+
) WHERE rn <= ?)`).run(keepHistory);
|
|
15132
|
+
db.prepare(`DELETE FROM pending_ops WHERE rowid NOT IN (
|
|
15133
|
+
SELECT rowid FROM pending_ops ORDER BY rowid DESC LIMIT ?)`).run(500);
|
|
15134
|
+
return Number(trimmed.changes);
|
|
15135
|
+
}
|
|
15136
|
+
/**
|
|
15137
|
+
* @author ddj 2026-11-24
|
|
15138
|
+
* @description: 真删:单事务内按「撤销意图 → 记录 → 批次」顺序清,备份在事务之前做。
|
|
15139
|
+
* @param db 工作区归档库连接
|
|
15140
|
+
* @param ids 候选批次 id
|
|
15141
|
+
* @returns 删除的记录数与批次数
|
|
15142
|
+
*/
|
|
15143
|
+
function removeBatches(db, ids) {
|
|
15144
|
+
const callIds = db.prepare("SELECT call_id FROM records WHERE batch_id = ?");
|
|
15145
|
+
const dropIntent = db.prepare("DELETE FROM revert_intents WHERE call_id = ?");
|
|
15146
|
+
const dropRecords = db.prepare("DELETE FROM records WHERE batch_id = ?");
|
|
15147
|
+
const dropBatch = db.prepare("DELETE FROM batches WHERE id = ?");
|
|
15148
|
+
let records = 0;
|
|
15149
|
+
let batches = 0;
|
|
15150
|
+
db.exec("BEGIN IMMEDIATE");
|
|
15151
|
+
try {
|
|
15152
|
+
for (const id of ids) {
|
|
15153
|
+
for (const row of callIds.all(id)) dropIntent.run(String(row.call_id));
|
|
15154
|
+
records += Number(dropRecords.run(id).changes);
|
|
15155
|
+
batches += Number(dropBatch.run(id).changes);
|
|
15156
|
+
}
|
|
15157
|
+
db.exec("COMMIT");
|
|
15158
|
+
} catch (error) {
|
|
15159
|
+
if (db.isTransaction) db.exec("ROLLBACK");
|
|
15160
|
+
throw error;
|
|
15161
|
+
}
|
|
15162
|
+
return {
|
|
15163
|
+
records,
|
|
15164
|
+
batches
|
|
15165
|
+
};
|
|
15166
|
+
}
|
|
15167
|
+
/**
|
|
15168
|
+
* @public
|
|
15169
|
+
* @author ddj 2026-11-24
|
|
15170
|
+
* @description: 归档库清理入口:保留期 + 每文件上限 + 撤销栈截断,可选 VACUUM。
|
|
15171
|
+
* @param cwd 工作区
|
|
15172
|
+
* @param options 清理选项
|
|
15173
|
+
* @returns 三个统计口径与实际删除行数
|
|
15174
|
+
* @throws 归档库不可用或事务失败
|
|
15175
|
+
*/
|
|
15176
|
+
function purgeArchiveDb(cwd, options = {}) {
|
|
15177
|
+
const db = dbFor(cwd);
|
|
15178
|
+
const { ids, expired, overCap, keptPending } = pickCandidates(db, options);
|
|
15179
|
+
const dryRun = options.dryRun !== false;
|
|
15180
|
+
const keepHistory = options.keepHistory ?? 10;
|
|
15181
|
+
if (dryRun) return {
|
|
15182
|
+
expired,
|
|
15183
|
+
overCap,
|
|
15184
|
+
keptPending,
|
|
15185
|
+
removed: {
|
|
15186
|
+
records: 0,
|
|
15187
|
+
batches: 0,
|
|
15188
|
+
history: 0
|
|
15189
|
+
}
|
|
15190
|
+
};
|
|
15191
|
+
if (ids.length) backupBeforePurge(db, cwd);
|
|
15192
|
+
const removedBatches = removeBatches(db, ids);
|
|
15193
|
+
const history = activeReady(cwd) ? trimPendingTables(db, keepHistory) : 0;
|
|
15194
|
+
const outcome = {
|
|
15195
|
+
expired,
|
|
15196
|
+
overCap,
|
|
15197
|
+
keptPending,
|
|
15198
|
+
removed: {
|
|
15199
|
+
...removedBatches,
|
|
15200
|
+
history
|
|
15201
|
+
}
|
|
15202
|
+
};
|
|
15203
|
+
if (options.vacuum !== false && (removedBatches.batches || history)) outcome.vacuumMs = vacuumOf(db);
|
|
15204
|
+
return outcome;
|
|
15205
|
+
}
|
|
15206
|
+
/**
|
|
15207
|
+
* @private
|
|
15208
|
+
* @author ddj 2026-11-24
|
|
15209
|
+
* @description: 回收已删行占用的页;失败只记日志,不影响已完成的删除。
|
|
15210
|
+
* @param db 工作区归档库连接
|
|
15211
|
+
* @returns 耗时毫秒
|
|
15212
|
+
*/
|
|
15213
|
+
function vacuumOf(db) {
|
|
15214
|
+
const started = Date.now();
|
|
15215
|
+
try {
|
|
15216
|
+
db.exec("VACUUM");
|
|
15217
|
+
} catch (error) {
|
|
15218
|
+
log.warn("归档库 VACUUM 失败:" + String(error));
|
|
15219
|
+
}
|
|
15220
|
+
return Date.now() - started;
|
|
15221
|
+
}
|
|
15222
|
+
/**
|
|
15223
|
+
* @private
|
|
15224
|
+
* @author ddj 2026-11-24
|
|
15225
|
+
* @description: 真删前留一份备份;同名覆盖,每个工作区只占一份。
|
|
15226
|
+
* @param db 工作区归档库连接
|
|
15227
|
+
* @param cwd 工作区
|
|
15228
|
+
* @returns 无
|
|
15229
|
+
*/
|
|
15230
|
+
function backupBeforePurge(db, cwd) {
|
|
15231
|
+
const target = archiveDbFile(cwd) + BACKUP_SUFFIX;
|
|
15232
|
+
try {
|
|
15233
|
+
rmSync(target, { force: true });
|
|
15234
|
+
db.exec(`VACUUM INTO '${target.replace(/'/g, "''")}'`);
|
|
15235
|
+
} catch (error) {
|
|
15236
|
+
log.warn("归档库清理前备份失败(继续执行):" + String(error));
|
|
15237
|
+
}
|
|
15238
|
+
}
|
|
15239
|
+
/**
|
|
15240
|
+
* @public
|
|
15241
|
+
* @author ddj 2026-11-24
|
|
15242
|
+
* @description: 列出本机所有归档库对应的工作区:读每个库里的 metadata.cwd,
|
|
15243
|
+
* 不依赖有没有活跃会话。读不出来的库跳过并记日志。
|
|
15244
|
+
* @param home DSH home
|
|
15245
|
+
* @returns 工作区绝对路径清单
|
|
15246
|
+
*/
|
|
15247
|
+
function listArchiveWorkspaces(home = dshHome()) {
|
|
15248
|
+
let names = [];
|
|
15249
|
+
try {
|
|
15250
|
+
names = readdirSync(join(home, PLUGIN_ID, "data", "archive")).filter((name) => name.endsWith(".sqlite"));
|
|
15251
|
+
} catch {
|
|
15252
|
+
return [];
|
|
15253
|
+
}
|
|
15254
|
+
const found = [];
|
|
15255
|
+
for (const name of names) {
|
|
15256
|
+
const file = join(home, PLUGIN_ID, "data", "archive", name);
|
|
15257
|
+
let db;
|
|
15258
|
+
try {
|
|
15259
|
+
db = new DatabaseSync(file, { readOnly: true });
|
|
15260
|
+
const row = db.prepare("SELECT value FROM metadata WHERE name='cwd'").get();
|
|
15261
|
+
if (row?.value) found.push(String(row.value));
|
|
15262
|
+
} catch (error) {
|
|
15263
|
+
log.debug("跳过不可读归档库:" + file + " " + String(error));
|
|
15264
|
+
} finally {
|
|
15265
|
+
try {
|
|
15266
|
+
db?.close();
|
|
15267
|
+
} catch {}
|
|
15268
|
+
}
|
|
15269
|
+
}
|
|
15270
|
+
return found;
|
|
15271
|
+
}
|
|
15272
|
+
/**
|
|
15273
|
+
* @public
|
|
15274
|
+
* @author ddj 2026-11-24
|
|
15275
|
+
* @description: 对所有工作区归档库跑一遍清理;单个库失败只记日志,不影响其他库。
|
|
15276
|
+
* @param options 扫描选项
|
|
15277
|
+
* @returns 扫描到的库数与删除的行数合计
|
|
15278
|
+
*/
|
|
15279
|
+
function sweepArchives(options = {}) {
|
|
15280
|
+
const run = options.run ?? purgeArchiveDb;
|
|
15281
|
+
const workspaces = listArchiveWorkspaces(options.home);
|
|
15282
|
+
let removed = 0;
|
|
15283
|
+
for (const cwd of workspaces) try {
|
|
15284
|
+
const outcome = run(cwd, { dryRun: false });
|
|
15285
|
+
removed += outcome.removed.batches + outcome.removed.history;
|
|
15286
|
+
if (outcome.removed.batches || outcome.removed.history) log.debug(`归档库清理:${cwd} 批次 ${outcome.removed.batches},历史 ${outcome.removed.history},VACUUM ${outcome.vacuumMs ?? 0}ms`);
|
|
15287
|
+
} catch (error) {
|
|
15288
|
+
log.debug("归档库清理跳过:" + cwd + " " + String(error));
|
|
15289
|
+
}
|
|
15290
|
+
return {
|
|
15291
|
+
workspaces: workspaces.length,
|
|
15292
|
+
removed
|
|
15293
|
+
};
|
|
15294
|
+
}
|
|
15295
|
+
/**
|
|
15296
|
+
* @public
|
|
15297
|
+
* @author ddj 2026-11-24
|
|
15298
|
+
* @description: 每日自动清理:启动后延迟跑一次,之后每 24 小时一次;返回可释放的句柄。
|
|
15299
|
+
* @param options 扫描选项
|
|
15300
|
+
* @returns 可释放句柄
|
|
15301
|
+
*/
|
|
15302
|
+
function startArchiveSweep(options = {}) {
|
|
15303
|
+
let stopped = false;
|
|
15304
|
+
const run = () => {
|
|
15305
|
+
if (stopped) return;
|
|
15306
|
+
try {
|
|
15307
|
+
sweepArchives(options);
|
|
15308
|
+
} catch (error) {
|
|
15309
|
+
log.warn("归档库每日清理失败:" + String(error));
|
|
15310
|
+
}
|
|
15311
|
+
};
|
|
15312
|
+
const first = setTimeout(run, options.delayMs ?? 9e4);
|
|
15313
|
+
const timer = setInterval(run, options.intervalMs ?? 864e5);
|
|
15314
|
+
if (typeof first.unref === "function") first.unref();
|
|
15315
|
+
if (typeof timer.unref === "function") timer.unref();
|
|
15316
|
+
return { dispose() {
|
|
15317
|
+
stopped = true;
|
|
15318
|
+
clearTimeout(first);
|
|
15319
|
+
clearInterval(timer);
|
|
15320
|
+
} };
|
|
15321
|
+
}
|
|
14884
15322
|
//#endregion
|
|
14885
15323
|
//#region src/revert.ts
|
|
14886
15324
|
/**
|
|
@@ -20322,6 +20760,33 @@ function refreshHandlers(kit) {
|
|
|
20322
20760
|
changed: outcome.changed,
|
|
20323
20761
|
gaps: outcome.gaps
|
|
20324
20762
|
};
|
|
20763
|
+
},
|
|
20764
|
+
/**
|
|
20765
|
+
* @author ddj 2026-11-24
|
|
20766
|
+
* @description: 清掉指定通道的幽灵记录。dryRun 只报数;真删后按需 VACUUM,
|
|
20767
|
+
* 并重建「待审接管」集合,避免旧路径继续顶掉官方行。
|
|
20768
|
+
* @param args 清理请求
|
|
20769
|
+
* @returns 命中、删除、保留条数与 VACUUM 耗时
|
|
20770
|
+
*/
|
|
20771
|
+
"edrv.pending.purge": async (args) => {
|
|
20772
|
+
const cwd = await kit.write(args.sessionId);
|
|
20773
|
+
const outcome = purgePending(cwd, {
|
|
20774
|
+
sources: args.sources,
|
|
20775
|
+
baseKinds: args.baseKinds,
|
|
20776
|
+
before: args.before
|
|
20777
|
+
}, {
|
|
20778
|
+
dryRun: args.dryRun === true,
|
|
20779
|
+
vacuum: true
|
|
20780
|
+
});
|
|
20781
|
+
const page = loadPage(cwd, args.sessionId);
|
|
20782
|
+
setPendingCovers(cwd, args.sessionId, page.items.map((item) => item.path));
|
|
20783
|
+
return {
|
|
20784
|
+
ok: true,
|
|
20785
|
+
matched: outcome.matched,
|
|
20786
|
+
removed: outcome.removed,
|
|
20787
|
+
kept: outcome.kept,
|
|
20788
|
+
vacuumMs: outcome.vacuumMs
|
|
20789
|
+
};
|
|
20325
20790
|
}
|
|
20326
20791
|
};
|
|
20327
20792
|
}
|
|
@@ -22970,6 +23435,36 @@ function buildHandlers(ctx, registry, searcher = newSearcher(ctx), contentSearch
|
|
|
22970
23435
|
};
|
|
22971
23436
|
}
|
|
22972
23437
|
},
|
|
23438
|
+
/**
|
|
23439
|
+
* @author ddj 2026-11-24
|
|
23440
|
+
* @description: 归档库清理:保留期 + 每文件上限 + 撤销栈截断。缺省 dryRun,显式传 false 才删。
|
|
23441
|
+
* @param args 清理参数
|
|
23442
|
+
* @returns 三个统计口径与删除行数
|
|
23443
|
+
*/
|
|
23444
|
+
"edrv.archive.purge": async (args) => {
|
|
23445
|
+
const sc = await resolveScope(ctx, args.sessionId);
|
|
23446
|
+
if ("err" in sc) return {
|
|
23447
|
+
ok: false,
|
|
23448
|
+
error: sc.err
|
|
23449
|
+
};
|
|
23450
|
+
try {
|
|
23451
|
+
return {
|
|
23452
|
+
ok: true,
|
|
23453
|
+
...purgeArchiveDb(sc.cwd, {
|
|
23454
|
+
dryRun: args.dryRun !== false,
|
|
23455
|
+
retentionDays: args.retentionDays,
|
|
23456
|
+
keepBatches: args.keepBatches,
|
|
23457
|
+
keepHistory: args.keepHistory,
|
|
23458
|
+
vacuum: args.vacuum
|
|
23459
|
+
})
|
|
23460
|
+
};
|
|
23461
|
+
} catch (error) {
|
|
23462
|
+
return {
|
|
23463
|
+
ok: false,
|
|
23464
|
+
error: "归档清理失败:" + String(error)
|
|
23465
|
+
};
|
|
23466
|
+
}
|
|
23467
|
+
},
|
|
22973
23468
|
"edrv.archivePage": async (args) => {
|
|
22974
23469
|
const sc = await resolveScope(ctx, args.sessionId);
|
|
22975
23470
|
if ("err" in sc) return {
|
|
@@ -37267,6 +37762,7 @@ function attachRuntime(ctx, runtime) {
|
|
|
37267
37762
|
settings: () => runtime.settings.external(),
|
|
37268
37763
|
record: recordPendingWrite
|
|
37269
37764
|
});
|
|
37765
|
+
const sweep = startArchiveSweep();
|
|
37270
37766
|
/**
|
|
37271
37767
|
* @author ddj 2026年11月24号
|
|
37272
37768
|
* @description: 任一工具调用期间都是 agent 写入窗口:bash / python 写盘落在窗口内时归属 agent。
|
|
@@ -37312,6 +37808,7 @@ function attachRuntime(ctx, runtime) {
|
|
|
37312
37808
|
evidence.dispose();
|
|
37313
37809
|
pending.dispose();
|
|
37314
37810
|
external.dispose();
|
|
37811
|
+
sweep.dispose();
|
|
37315
37812
|
windows.dispose();
|
|
37316
37813
|
runtime.fileVersions.dispose();
|
|
37317
37814
|
closeArchiveDbs();
|