dsh-archive-management 1.2.0 → 1.2.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.2.1 (2026-11-20)
4
+
5
+ ### 修复(fork P0 深度审查产出)
6
+
7
+ - **软删除级联**:删除父会话时全部子代理会话(多级)跟随进入回收站,可独立还原;任一子代理运行中整体拒绝(409)。
8
+ - **purge-archived 级联**:「全部永久删除」同时清掉已归档会话的未归档子代理,不再留孤儿。
9
+ - **配置原子写**:config.json 改为临时文件 + rename,杜绝写一半损坏。
10
+ - **/debug 收紧鉴权**:GET 也要求自定义头(遥测快照不再对无凭证本机请求可见)。
11
+ - 新增 10 项测试断言(软删除级联/运行子代理拒绝/调试鉴权),宿主套件 63 项全绿。
12
+
3
13
  ## 1.2.0 (2026-11-20)
4
14
 
5
15
  ### 变更
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-archive-management",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "Real session deletion for DeepSeek Harness: settings-page manager with trash & restore + sidebar permanent delete (Codex style) + auto-archive with retention (zcodex style). Fork of xohmai/dsh-session-delete.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
package/src/index.js CHANGED
@@ -598,60 +598,24 @@ export function apply(ctx) {
598
598
  // 删除 / 还原 / 清空
599
599
  // -------------------------------------------------------------------------
600
600
 
601
- async function deleteSession(id) {
602
- if (typeof id !== 'string' || !ID_RE.test(id)) throw httpError(400, 'INVALID_ID', '会话 id 非法')
603
- // 条目名在动手之前算好并校验:id 经转义后若仍越界(极端长 id 转义膨胀),
604
- // 直接拒绝删除——绝不制造「已归档但没删成」的中间态或不可还原的回收站条目。
601
+ /** 单会话移入回收站:定位 → 条目名校验 → rename → meta.json。返回 { entry, sizeBytes }。 */
602
+ async function moveOneToTrash(id, header, title) {
605
603
  const entry = `${entryStamp()}-${encodeEntrySegment(id)}`
606
604
  if (!ENTRY_RE.test(entry)) {
607
605
  throw httpError(400, 'INVALID_ID', '会话 id 转义后条目名过长,无法安全移入回收站')
608
606
  }
609
- const { byId } = await inventory()
610
- const header = byId.get(id)
611
- if (header === undefined) throw httpError(404, 'NOT_FOUND', `会话 ${id} 不存在于磁盘记录(可能已删除)`)
612
-
613
- const agent = agentState(id)
614
- if (agent.running) throw httpError(409, 'RUNNING', '会话正在运行(模型或子代理未结束),请稍后再试')
615
-
616
- // 归档必须在文件移动之前:官方 sessionKnown 检查 live 或仍在持久化清单。
617
- // 已归档是幂等 no-op;此时会话一定 known(上面刚在清单里找到)。
618
- try {
619
- await ctx.workspaceRegistry.archiveSession(id)
620
- } catch (error) {
621
- throw httpError(500, 'ARCHIVE_FAILED', `归档失败:${error?.message ?? error}`)
622
- }
623
-
624
607
  const location = ctx.sessionPersistence.locate(header)
625
608
  if (!location || typeof location.path !== 'string') {
626
609
  throw httpError(500, 'UNSUPPORTED', '当前持久化后端无法定位会话目录')
627
610
  }
628
611
  const dir = dirname(location.path)
629
- if ((await stat(dir).catch(() => undefined)) === undefined) {
630
- // 目录已不存在(例如此前删了一半):归档已完成,直接视为成功。
631
- const keptHidden = archivedSet().has(id)
632
- await audit('delete', { id, note: 'dir-missing', keptHidden })
633
- // 通知所有客户端从会话列表 store 里移除该行(typert emit 广播;
634
- // mock/旧版 ctx 无 emit 时静默跳过)。
635
- ctx.emit?.('api-session/removed', id)
636
- return { id, ok: true, note: 'dir-missing', keptHidden }
637
- }
612
+ if ((await stat(dir).catch(() => undefined)) === undefined) return null
638
613
  const sizeBytes = await dirSize(dir)
639
- // 标题随会话进回收站:缓存优先(清理面板刚读过清单),缺失时单独折叠一次。
640
- // 回收站据此显示会话名而非一串 session-id;旧条目无 title 时客户端回退显示 id。
641
- let title = null
642
- const cachedRec = recordCache.get(id)
643
- if (typeof cachedRec?.title === 'string') title = cachedRec.title
644
- else {
645
- const titles = await titlesFor([id])
646
- if (typeof titles.get(id) === 'string') title = titles.get(id)
647
- }
648
-
649
614
  const root = await trashRoot()
650
615
  const entryDir = join(root, entry)
651
- const dataDir = join(entryDir, 'data')
652
616
  await mkdir(entryDir, { recursive: true })
653
617
  try {
654
- await rename(dir, dataDir)
618
+ await rename(dir, join(entryDir, 'data'))
655
619
  } catch (error) {
656
620
  await rm(entryDir, { recursive: true, force: true }).catch(() => {})
657
621
  throw httpError(500, 'MOVE_FAILED', `移入回收站失败:${error?.message ?? error}`)
@@ -672,19 +636,63 @@ export function apply(ctx) {
672
636
  2,
673
637
  ),
674
638
  )
675
- // 文件已离盘。归档 id 保留不摘(见文件头删除流水线第 5 步):摘除会广播
676
- // archived-sessions-changed,把已连接客户端 list store 里的残留行(冷会话)
677
- // 或宿主内存里的活会话(live)重新显示回侧栏。ghost 待宿主重启时由
678
- // reconcileArchivedGhosts 统一清理。
639
+ return { entry, sizeBytes }
640
+ }
641
+
642
+ async function deleteSession(id) {
643
+ if (typeof id !== 'string' || !ID_RE.test(id)) throw httpError(400, 'INVALID_ID', '会话 id 非法')
644
+ const { byId } = await inventory()
645
+ const header = byId.get(id)
646
+ if (header === undefined) throw httpError(404, 'NOT_FOUND', `会话 ${id} 不存在于磁盘记录(可能已删除)`)
647
+ // fork P0:级联目标 = 全部后代子代理(多级)。任何一个在运行都整体拒绝——绝不半删。
648
+ const children = descendantsOf(byId, id)
649
+ const targets = [id, ...children]
650
+ const runningTarget = targets.find((tid) => agentState(tid).running)
651
+ if (runningTarget !== undefined) {
652
+ throw httpError(
653
+ 409,
654
+ 'RUNNING',
655
+ runningTarget === id
656
+ ? '会话正在运行(模型或子代理未结束),请稍后再试'
657
+ : `子代理会话 ${runningTarget} 正在运行,请等它结束后再删除`,
658
+ )
659
+ }
660
+ // 归档必须在文件移动之前:官方 sessionKnown 检查 live 或仍在持久化清单。
661
+ // 已归档是幂等 no-op;此时会话一定 known(上面刚在清单里找到)。
662
+ for (const tid of targets) {
663
+ try {
664
+ await ctx.workspaceRegistry.archiveSession(tid)
665
+ } catch (error) {
666
+ throw httpError(500, 'ARCHIVE_FAILED', `归档失败:${error?.message ?? error}`)
667
+ }
668
+ }
669
+ // 标题批量解析一次(父 + 全部子代理):缓存优先,缺失合批折叠。
670
+ const folded = await titlesFor(targets)
671
+ const root = await trashRoot()
672
+ const entries = []
673
+ for (const tid of targets) {
674
+ const h = byId.get(tid)
675
+ const dir = (() => {
676
+ try {
677
+ const location = ctx.sessionPersistence.locate(h)
678
+ return location && typeof location.path === 'string' ? dirname(location.path) : null
679
+ } catch {
680
+ return null
681
+ }
682
+ })()
683
+ if (dir === null || (await stat(dir).catch(() => undefined)) === undefined) continue
684
+ const cached = recordCache.get(tid)
685
+ const title = typeof cached?.title === 'string' ? cached.title : typeof folded.get(tid) === 'string' ? folded.get(tid) : null
686
+ entries.push({ id: tid, ...(await moveOneToTrash(tid, h, title)) })
687
+ }
688
+ for (const tid of targets) {
689
+ ctx.emit?.('api-session/removed', tid)
690
+ recordCache.delete(tid)
691
+ }
692
+ // 归档 id 保留不摘(ghost 待宿主重启 reconcile 统一清理,见文件头第 5 步)。
679
693
  const keptHidden = archivedSet().has(id)
680
- await audit('delete', { id, entry, keptHidden })
681
- // 关键修复:显式广播 api-session/removed,让 typert 协议把「会话已删除」
682
- // 推给所有客户端,客户端据此把残留行从 list store 里剪掉——没有这一步,
683
- // 冷会话被删后宿主不发任何帧,行会一直留在侧栏(点开即 session/not-found)。
684
- // mock/旧版 ctx 无 emit 时静默跳过(?. 防御)。
685
- ctx.emit?.('api-session/removed', id)
686
- recordCache.delete(id)
687
- return { id, ok: true, entry, keptHidden }
694
+ await audit('delete', { id, cascade: children.length, entries: entries.map((e) => e.entry), keptHidden })
695
+ return { id, ok: true, entry: (entries.find((e) => e.id === id) ?? {}).entry, cascade: children.length, keptHidden }
688
696
  }
689
697
 
690
698
  async function listTrash() {
@@ -930,7 +938,10 @@ export function apply(ctx) {
930
938
  async function saveConfig(next) {
931
939
  const path = join(await trashRoot(), 'config.json')
932
940
  await mkdir(dirname(path), { recursive: true })
933
- await writeFile(path, `${JSON.stringify(next, null, 2)}\n`, 'utf8')
941
+ // fork P0:原子写——临时文件 + rename,杜绝写一半崩溃导致配置损坏
942
+ const tmp = `${path}.tmp`
943
+ await writeFile(tmp, `${JSON.stringify(next, null, 2)}\n`, 'utf8')
944
+ await rename(tmp, path)
934
945
  }
935
946
 
936
947
  /** 置顶会话集合(registry 内部字段名未成契约,多形态探测;取不到则视为无置顶)。 */
@@ -1047,11 +1058,16 @@ export function apply(ctx) {
1047
1058
  .catch(() => {})
1048
1059
  }
1049
1060
 
1050
- /** 永久删除全部已归档会话(仿 zcodex「全部永久删除」):跳过运行中,逐条容错。 */
1061
+ /** 永久删除全部已归档会话(仿 zcodex「全部永久删除」):跳过运行中,逐条容错。
1062
+ * fork P0:级联目标包含已归档会话的全部后代子代理——不留孤儿。 */
1051
1063
  async function purgeArchivedAll() {
1052
1064
  const { byId } = await inventory()
1053
1065
  const archived = archivedSet()
1054
- const targets = [...byId.keys()].filter((id) => archived.has(id))
1066
+ const targets = new Set()
1067
+ for (const id of [...byId.keys()].filter((x) => archived.has(x))) {
1068
+ targets.add(id)
1069
+ for (const d of descendantsOf(byId, id)) targets.add(d)
1070
+ }
1055
1071
  let purged = 0
1056
1072
  let failed = 0
1057
1073
  let skippedRunning = 0
@@ -1398,18 +1414,28 @@ export function apply(ctx) {
1398
1414
  ctx.webServer.register({
1399
1415
  kind: 'exact',
1400
1416
  path: `${PREFIX}/debug`,
1401
- handler: guard(async (req, res) => {
1402
- if (req.method === 'GET') {
1403
- sendJson(res, 200, { ok: true, debug: menuHookDebug })
1404
- return
1405
- }
1406
- if (req.method !== 'POST') return sendJson(res, 405, { error: { code: 'METHOD', message: 'GET/POST only' } })
1407
- const body = await readBody(req)
1408
- if (body === null || typeof body !== 'object' || body.state === undefined) {
1409
- throw httpError(400, 'INVALID_BODY', '缺少 state')
1417
+ handler: (async (req, res) => {
1418
+ try {
1419
+ // fork P0:遥测可能包含菜单结构快照——所有方法(含 GET)都要求自定义头
1420
+ if (req.headers[PLUGIN_HEADER] !== 'session-delete') {
1421
+ throw httpError(403, 'FORBIDDEN', `缺少 ${PLUGIN_HEADER} 头`)
1422
+ }
1423
+ if (req.method === 'GET') {
1424
+ sendJson(res, 200, { ok: true, debug: menuHookDebug })
1425
+ return
1426
+ }
1427
+ if (req.method !== 'POST') return sendJson(res, 405, { error: { code: 'METHOD', message: 'GET/POST only' } })
1428
+ const body = await readBody(req)
1429
+ if (body === null || typeof body !== 'object' || body.state === undefined) {
1430
+ throw httpError(400, 'INVALID_BODY', '缺少 state')
1431
+ }
1432
+ menuHookDebug = { at: new Date().toISOString(), body }
1433
+ sendJson(res, 200, { ok: true })
1434
+ } catch (error) {
1435
+ sendJson(res, error?.statusCode ?? 500, {
1436
+ error: { code: error?.code ?? 'INTERNAL', message: String(error?.message ?? error) },
1437
+ })
1410
1438
  }
1411
- menuHookDebug = { at: new Date().toISOString(), body }
1412
- sendJson(res, 200, { ok: true })
1413
1439
  }),
1414
1440
  }),
1415
1441
  )