@leiyi-works/dsh-session-tmpdir 0.5.6 → 0.6.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 +22 -1
- package/cordis.patch.yml +7 -0
- package/index.js +413 -10
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Task Temp Folder(session-task-tmpdir)v0.
|
|
1
|
+
# Task Temp Folder(session-task-tmpdir)v0.6.0
|
|
2
2
|
|
|
3
3
|
给每个 DSH 会话一个**任务临时目录**,并在**会话被删除 7 天后**自动清掉对应目录:
|
|
4
4
|
|
|
@@ -42,6 +42,10 @@
|
|
|
42
42
|
| `allowRootPatterns` | `['dsh-*']` | 无需审批的根目录名(glob,`*`/`?`) |
|
|
43
43
|
| `allowRootFiles` | `['.dsh-standing-rules.md']` | 无需审批的工作区根文件 |
|
|
44
44
|
| `commandGuard` | `deny` | pwsh/bash 命令守卫:`deny` / `warn`(只记日志)/ `off` |
|
|
45
|
+
| `attachmentCleanup` | `true` | v0.6 附件回收总开关(会话删除后回收其附件) |
|
|
46
|
+
| `attachmentCleanupDays` | 同 `orphanCleanupDays` | 会话删除后多少天回收其附件;`0` = 只扫地孤儿 |
|
|
47
|
+
| `attachmentSweepOrphans` | `true` | 是否同时清扫"无人引用且够旧"的历史孤儿附件 |
|
|
48
|
+
| `attachmentsDir` | `<DSH_HOME>/attachments` | 附件仓库根目录(一般不用改) |
|
|
45
49
|
|
|
46
50
|
## 长期文件审批流程
|
|
47
51
|
|
|
@@ -69,6 +73,19 @@
|
|
|
69
73
|
- 启动后约 60 秒跑第一次,之后按 `cleanupIntervalHours` 周期跑;`orphanCleanupDays: 0` 关闭清理。
|
|
70
74
|
- 删除是**整目录递归删除**,不备份;想先观察就把 `cleanupDryRun: true` 开着跑一轮。
|
|
71
75
|
|
|
76
|
+
## 附件回收规则(v0.6)
|
|
77
|
+
|
|
78
|
+
Host 的附件仓库(`~/.dsh/attachments`)按内容 hash 存储、**自身没有任何回收机制**:删掉会话只删日志,图片会永远留在磁盘上。本插件把附件也接进同一条 7 天清理链路:
|
|
79
|
+
|
|
80
|
+
1. 每轮清理时,通过 `sessionPersistence` 读取**每个已存会话**的事件,收集 `attachmentId`(`sha256:<hash>`),写进台账 `~/.dsh/data/session-tmpdir-attachments.json`;
|
|
81
|
+
2. 台账按 `revision` 缓存——会话没变就不重读,稳态开销几乎为零;
|
|
82
|
+
3. 会话消失 → 台账记 `orphanedAt`;满 `attachmentCleanupDays` 天后,删除**只被该会话引用**的 blob;
|
|
83
|
+
4. 任何时候只要还有别的存活会话引用同一个 hash,就跳过(同一张图被多个会话用过的安全阀);
|
|
84
|
+
5. `attachmentSweepOrphans` 开启时,额外清扫「无任何会话引用 + 文件本身够旧」的历史孤儿(覆盖本功能上线前就残留的那批);
|
|
85
|
+
6. 最近 1 小时内写入的 blob 一律不碰;每次删除追加到 `~/.dsh/data/session-tmpdir-attachments-gc.jsonl`(hash / 大小 / 时间 / 原因),可审计。
|
|
86
|
+
|
|
87
|
+
`tmp_cleanup` 的返回里多一个 `attachments` 字段(删除数、释放 MB、待回收会话数、跳过数);`dryRun: true` 只统计不动手。
|
|
88
|
+
|
|
72
89
|
## 开发
|
|
73
90
|
|
|
74
91
|
```powershell
|
|
@@ -87,3 +104,7 @@ v0.5.4 变更(2026-11-12):按实际弹窗效果精简选项——删掉 C
|
|
|
87
104
|
v0.5.5 变更(2026-11-12):选项标签统一写短(A 只写"不长期保存",路径等说明移到选项 `description` 小字),并要求不许把路径细节塞进标签;避免长标签把 GUI 里的描述小字顶到下一行造成排版不一致。规则与 guard 未改。
|
|
88
105
|
|
|
89
106
|
v0.5.6 变更(2026-11-12):选 B(工作区根目录保存)时不再要用户确认目录名——模型自行起一个清楚的目录名、`longterm_approve` 登记后直接写,跟 DSH 平时自己命名落盘一样;B 的小字改为"目录名自动起"。规则与 guard 未改。
|
|
107
|
+
|
|
108
|
+
v0.5.7 变更(2026-11-12):微调选项小字——A 的说明改为"随会话删除后 N 天清理"(N 取 `orphanCleanupDays`,默认 7,不再写死"随会话删除清理");B 的说明删掉"目录名自动起"字样,只留"长期保存到会话工作区根目录下"(自动起名行为本身不变)。规则与 guard 未改。
|
|
109
|
+
|
|
110
|
+
v0.6.0 变更(2026-11-13):新增**附件回收**——7 天清理会话目录时,一并回收该会话的附件 blob(台账 + 引用二次校验 + 扫描历史孤儿 + 审计日志),解决 `~/.dsh/attachments` 只增不减的问题。通过 `sessionPersistence` 读事件,不依赖日志格式、不自带 zstd 解码;`tmp_cleanup` 增加 `attachments` 参数与 `attachments` 输出字段。临时目录规则与 guard 未改。
|
package/cordis.patch.yml
CHANGED
|
@@ -20,3 +20,10 @@
|
|
|
20
20
|
- .dsh-standing-rules.md
|
|
21
21
|
# pwsh/bash 命令守卫:deny(拦截)| warn(只记日志)| off(关闭)
|
|
22
22
|
commandGuard: deny
|
|
23
|
+
# v0.6:附件回收——会话删除满 N 天后,回收它独占的附件 blob
|
|
24
|
+
# (台账 + 引用二次校验 + 审计日志,见 README「附件回收规则」)
|
|
25
|
+
attachmentCleanup: true
|
|
26
|
+
# 默认跟随 orphanCleanupDays;单独指定时用下面的键
|
|
27
|
+
# attachmentCleanupDays: 7
|
|
28
|
+
# 是否顺带清扫"无任何会话引用且文件够旧"的历史孤儿附件
|
|
29
|
+
attachmentSweepOrphans: true
|
package/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { existsSync, statSync } from 'node:fs'
|
|
2
|
-
import { mkdir, readdir, readFile, rename, rm, stat, writeFile } from 'node:fs/promises'
|
|
2
|
+
import { appendFile, mkdir, readdir, readFile, rename, rm, stat, writeFile } from 'node:fs/promises'
|
|
3
3
|
import { homedir } from 'node:os'
|
|
4
4
|
import { basename, dirname, isAbsolute, join, resolve } from 'node:path'
|
|
5
5
|
|
|
@@ -29,6 +29,19 @@ import { basename, dirname, isAbsolute, join, resolve } from 'node:path'
|
|
|
29
29
|
* - a folder whose session is gone is stamped `orphanedAt` on first sight;
|
|
30
30
|
* - once `orphanedAt` is older than `orphanCleanupDays`, the folder is deleted.
|
|
31
31
|
* Folders without our marker are never touched.
|
|
32
|
+
*
|
|
33
|
+
* Attachment garbage collection (v0.6), same schedule and grace period:
|
|
34
|
+
* - the Host attachment store (`<DSH_HOME>/attachments`) is content-addressed and never
|
|
35
|
+
* garbage collected, so deleting a conversation used to leave its blobs on disk forever;
|
|
36
|
+
* - every tick the ledger `<DSH_HOME>/data/session-tmpdir-attachments.json` records which
|
|
37
|
+
* attachment ids each stored session references (read through `sessionPersistence`, so
|
|
38
|
+
* no log format is assumed); only a changed revision is re-read;
|
|
39
|
+
* - a session that disappears is stamped `orphanedAt`; after `attachmentCleanupDays` the
|
|
40
|
+
* blobs it alone referenced are deleted (any blob still referenced is always kept);
|
|
41
|
+
* - blobs nobody references and older than the grace period are swept as well, which
|
|
42
|
+
* reclaims the historical leftovers of sessions deleted before this feature existed;
|
|
43
|
+
* - every deletion is appended to `session-tmpdir-attachments-gc.jsonl`, and a blob
|
|
44
|
+
* written within the last hour is never touched.
|
|
32
45
|
*/
|
|
33
46
|
|
|
34
47
|
const MARKER_FILE = '.dsh-task.json'
|
|
@@ -39,6 +52,21 @@ const DEFAULT_CLEANUP_HOURS = 24
|
|
|
39
52
|
const STARTUP_CLEANUP_DELAY_MS = 60_000
|
|
40
53
|
const FALLBACK_PREFIX = 'session-'
|
|
41
54
|
const DAY_MS = 86_400_000
|
|
55
|
+
|
|
56
|
+
// ── attachment garbage collection (v0.6) ────────────────────────────────────
|
|
57
|
+
/** Ledger file (under `<DSH_HOME>/data/`) recording which attachments each session used. */
|
|
58
|
+
const ATTACHMENT_LEDGER_FILE = 'session-tmpdir-attachments.json'
|
|
59
|
+
/** Append-only audit log of every attachment the collector deleted. */
|
|
60
|
+
const ATTACHMENT_GC_LOG_FILE = 'session-tmpdir-attachments-gc.jsonl'
|
|
61
|
+
const ATTACHMENT_LEDGER_VERSION = 1
|
|
62
|
+
/** `attachmentId` values look like `sha256:<64 lowercase hex>`. */
|
|
63
|
+
const ATTACHMENT_ID_PATTERN = /^sha256:([0-9a-f]{64})$/i
|
|
64
|
+
/** Never delete a blob written this recently — it may belong to a message not yet persisted. */
|
|
65
|
+
const ATTACHMENT_MIN_AGE_MS = 3_600_000
|
|
66
|
+
/** Upper bound on read pages per session, so one corrupt log cannot hang the sweep. */
|
|
67
|
+
const ATTACHMENT_READ_PAGE_LIMIT = 200
|
|
68
|
+
/** Events per read page: keeps peak memory bounded on very long sessions. */
|
|
69
|
+
const ATTACHMENT_READ_PAGE_SIZE = 500
|
|
42
70
|
/** Tool names whose `file_path` argument creates or overwrites a file. */
|
|
43
71
|
const WRITE_TOOL_NAMES = new Set(['write', 'edit'])
|
|
44
72
|
/** Tool names that run a shell command (their write targets must be parsed from text). */
|
|
@@ -84,6 +112,12 @@ function readConfig(config) {
|
|
|
84
112
|
const orphanCleanupDays = Number.isFinite(c.orphanCleanupDays) && c.orphanCleanupDays >= 0 ? c.orphanCleanupDays : DEFAULT_ORPHAN_DAYS
|
|
85
113
|
const cleanupIntervalHours = Number.isFinite(c.cleanupIntervalHours) && c.cleanupIntervalHours >= 1 ? c.cleanupIntervalHours : DEFAULT_CLEANUP_HOURS
|
|
86
114
|
const cleanupDryRun = c.cleanupDryRun === true
|
|
115
|
+
const attachmentCleanup = c.attachmentCleanup !== false
|
|
116
|
+
const attachmentCleanupDays =
|
|
117
|
+
Number.isFinite(c.attachmentCleanupDays) && c.attachmentCleanupDays >= 0 ? c.attachmentCleanupDays : orphanCleanupDays
|
|
118
|
+
const attachmentSweepOrphans = c.attachmentSweepOrphans !== false
|
|
119
|
+
const attachmentsDir =
|
|
120
|
+
typeof c.attachmentsDir === 'string' && c.attachmentsDir.trim() ? c.attachmentsDir.trim() : join(resolveDshHome(), 'attachments')
|
|
87
121
|
const guardWorkspaceRoot = c.guardWorkspaceRoot !== false
|
|
88
122
|
const requireApprovalOutsideTmp = c.requireApprovalOutsideTmp !== false
|
|
89
123
|
const allowRootPatterns = toStringList(c.allowRootPatterns, DEFAULT_ALLOW_ROOT_PATTERNS)
|
|
@@ -101,6 +135,10 @@ function readConfig(config) {
|
|
|
101
135
|
allowRootPatterns,
|
|
102
136
|
allowRootFiles,
|
|
103
137
|
commandGuard,
|
|
138
|
+
attachmentCleanup,
|
|
139
|
+
attachmentCleanupDays,
|
|
140
|
+
attachmentSweepOrphans,
|
|
141
|
+
attachmentsDir,
|
|
104
142
|
}
|
|
105
143
|
}
|
|
106
144
|
|
|
@@ -439,10 +477,16 @@ export async function cleanupOrphanedTempDirs({
|
|
|
439
477
|
return report
|
|
440
478
|
}
|
|
441
479
|
|
|
480
|
+
/** Resolve the DSH home directory (`DSH_HOME` override, else `~/.dsh`). */
|
|
481
|
+
export function resolveDshHome() {
|
|
482
|
+
const fromEnv = process.env.DSH_HOME && process.env.DSH_HOME.trim() ? process.env.DSH_HOME.trim() : ''
|
|
483
|
+
return fromEnv || join(homedir(), '.dsh')
|
|
484
|
+
}
|
|
485
|
+
|
|
442
486
|
/** Best-effort fallback: collect session ids from the persisted session tree. */
|
|
443
487
|
async function scanPersistedSessionIds() {
|
|
444
488
|
const ids = new Set()
|
|
445
|
-
const home =
|
|
489
|
+
const home = resolveDshHome()
|
|
446
490
|
const roots = [join(home, 'sessions'), join(home, '_archived-sessions')]
|
|
447
491
|
const idPattern = /^(?:session-)?[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
|
448
492
|
for (const root of roots) {
|
|
@@ -475,6 +519,103 @@ async function scanPersistedSessionIds() {
|
|
|
475
519
|
return ids
|
|
476
520
|
}
|
|
477
521
|
|
|
522
|
+
/**
|
|
523
|
+
* Collect `sha256:` attachment ids from an arbitrary session-event payload.
|
|
524
|
+
*
|
|
525
|
+
* Attachment references are stored as `{ attachmentId: 'sha256:<hex>', ... }` inside
|
|
526
|
+
* `image` / `file` content blocks; walking the parsed event keeps this independent of
|
|
527
|
+
* where a block lives (user message, tool result, spliced inbox, …).
|
|
528
|
+
*
|
|
529
|
+
* @param {unknown} value parsed session event (or any nested JSON value)
|
|
530
|
+
* @param {Set<string>} [out] accumulator of hex hashes
|
|
531
|
+
* @param {number} [depth] recursion guard
|
|
532
|
+
* @returns {Set<string>}
|
|
533
|
+
*/
|
|
534
|
+
export function collectAttachmentIds(value, out = new Set(), depth = 0) {
|
|
535
|
+
if (!value || depth > 24) return out
|
|
536
|
+
if (Array.isArray(value)) {
|
|
537
|
+
for (const item of value) collectAttachmentIds(item, out, depth + 1)
|
|
538
|
+
return out
|
|
539
|
+
}
|
|
540
|
+
if (typeof value !== 'object') return out
|
|
541
|
+
for (const [key, item] of Object.entries(value)) {
|
|
542
|
+
if (key === 'attachmentId' && typeof item === 'string') {
|
|
543
|
+
const match = ATTACHMENT_ID_PATTERN.exec(item)
|
|
544
|
+
if (match) out.add(match[1].toLowerCase())
|
|
545
|
+
continue
|
|
546
|
+
}
|
|
547
|
+
collectAttachmentIds(item, out, depth + 1)
|
|
548
|
+
}
|
|
549
|
+
return out
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/** Read the attachment ledger; a missing or unreadable file yields an empty ledger. */
|
|
553
|
+
async function readAttachmentLedger(path) {
|
|
554
|
+
try {
|
|
555
|
+
const parsed = JSON.parse(await readFile(path, 'utf8'))
|
|
556
|
+
if (parsed && typeof parsed === 'object' && parsed.sessions && typeof parsed.sessions === 'object') {
|
|
557
|
+
return {
|
|
558
|
+
version: ATTACHMENT_LEDGER_VERSION,
|
|
559
|
+
sessions: parsed.sessions,
|
|
560
|
+
lastSweepAt: Number(parsed.lastSweepAt) || 0,
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
} catch {
|
|
564
|
+
// missing or corrupt ledger: start from scratch
|
|
565
|
+
}
|
|
566
|
+
return { version: ATTACHMENT_LEDGER_VERSION, sessions: {}, lastSweepAt: 0 }
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/** Write the ledger atomically (temp file + rename), best-effort. */
|
|
570
|
+
async function writeAttachmentLedger(path, ledger) {
|
|
571
|
+
const temp = `${path}.tmp`
|
|
572
|
+
try {
|
|
573
|
+
await mkdir(dirname(path), { recursive: true })
|
|
574
|
+
await writeFile(temp, JSON.stringify(ledger), 'utf8')
|
|
575
|
+
await rename(temp, path)
|
|
576
|
+
} catch {
|
|
577
|
+
// ledger persistence is an optimization; a failed write only costs a rescan
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/**
|
|
582
|
+
* List every stored attachment blob.
|
|
583
|
+
* @param {string} attachmentsRoot attachment service root (default `<DSH_HOME>/attachments`)
|
|
584
|
+
* @returns {Promise<Array<{ hash: string, path: string, bytes: number, mtime: number }>>}
|
|
585
|
+
*/
|
|
586
|
+
async function listAttachmentBlobs(attachmentsRoot) {
|
|
587
|
+
const objectsDir = join(attachmentsRoot, 'v1', 'objects')
|
|
588
|
+
/** @type {Array<{ hash: string, path: string, bytes: number, mtime: number }>} */
|
|
589
|
+
const blobs = []
|
|
590
|
+
let buckets
|
|
591
|
+
try {
|
|
592
|
+
buckets = await readdir(objectsDir, { withFileTypes: true })
|
|
593
|
+
} catch {
|
|
594
|
+
return blobs
|
|
595
|
+
}
|
|
596
|
+
for (const bucket of buckets) {
|
|
597
|
+
if (!bucket.isDirectory()) continue
|
|
598
|
+
const bucketDir = join(objectsDir, bucket.name)
|
|
599
|
+
let files
|
|
600
|
+
try {
|
|
601
|
+
files = await readdir(bucketDir, { withFileTypes: true })
|
|
602
|
+
} catch {
|
|
603
|
+
continue
|
|
604
|
+
}
|
|
605
|
+
for (const file of files) {
|
|
606
|
+
if (!file.isFile() || !/^[0-9a-f]{64}$/i.test(file.name)) continue
|
|
607
|
+
const full = join(bucketDir, file.name)
|
|
608
|
+
try {
|
|
609
|
+
const info = await stat(full)
|
|
610
|
+
blobs.push({ hash: file.name.toLowerCase(), path: full, bytes: info.size, mtime: info.mtimeMs })
|
|
611
|
+
} catch {
|
|
612
|
+
/* raced with a delete */
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
return blobs
|
|
617
|
+
}
|
|
618
|
+
|
|
478
619
|
export function apply(ctx, config) {
|
|
479
620
|
const {
|
|
480
621
|
rootDirName,
|
|
@@ -488,6 +629,10 @@ export function apply(ctx, config) {
|
|
|
488
629
|
allowRootPatterns,
|
|
489
630
|
allowRootFiles,
|
|
490
631
|
commandGuard,
|
|
632
|
+
attachmentCleanup,
|
|
633
|
+
attachmentCleanupDays,
|
|
634
|
+
attachmentSweepOrphans,
|
|
635
|
+
attachmentsDir,
|
|
491
636
|
} = readConfig(config)
|
|
492
637
|
const queues = new Map()
|
|
493
638
|
/** sessionId → approved long-term prefixes ({ prefix, reason, basis, approvedAt }[]). */
|
|
@@ -718,19 +863,246 @@ export function apply(ctx, config) {
|
|
|
718
863
|
// ── orphan cleanup ─────────────────────────────────────────────────────────
|
|
719
864
|
let cleaning = false
|
|
720
865
|
|
|
721
|
-
async function
|
|
866
|
+
async function loadPersistenceSnapshots() {
|
|
722
867
|
if (sessionPersistence && typeof sessionPersistence.list === 'function') {
|
|
723
868
|
try {
|
|
724
|
-
|
|
725
|
-
return snapshots.map((snapshot) => String(snapshot?.header?.id ?? '')).filter(Boolean)
|
|
869
|
+
return await sessionPersistence.list()
|
|
726
870
|
} catch {
|
|
727
871
|
// fall through to the directory scan
|
|
728
872
|
}
|
|
729
873
|
}
|
|
874
|
+
return undefined
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
async function collectPersistedSessionIds() {
|
|
878
|
+
const snapshots = await loadPersistenceSnapshots()
|
|
879
|
+
if (snapshots) return snapshots.map((snapshot) => String(snapshot?.header?.id ?? '')).filter(Boolean)
|
|
730
880
|
return [...(await scanPersistedSessionIds())]
|
|
731
881
|
}
|
|
732
882
|
|
|
733
|
-
|
|
883
|
+
// ── attachment cleanup (v0.6) ───────────────────────────────────────────────
|
|
884
|
+
// The attachment store is content-addressed and never garbage collected by the Host,
|
|
885
|
+
// so deleting a conversation leaves its blobs behind forever. Every cleanup tick we
|
|
886
|
+
// record the attachment ids each *stored* session references (ledger), stamp the
|
|
887
|
+
// moment a session disappears, and only once that stamp is older than
|
|
888
|
+
// `attachmentCleanupDays` delete the blobs no surviving session references.
|
|
889
|
+
const attachmentsRoot = attachmentsDir
|
|
890
|
+
const attachmentLedgerPath = join(resolveDshHome(), 'data', ATTACHMENT_LEDGER_FILE)
|
|
891
|
+
const attachmentGcLogPath = join(resolveDshHome(), 'data', ATTACHMENT_GC_LOG_FILE)
|
|
892
|
+
|
|
893
|
+
/** Read one stored session through the persistence service and collect its attachment ids. */
|
|
894
|
+
async function readSessionAttachmentIds(sessionId) {
|
|
895
|
+
if (!sessionPersistence || typeof sessionPersistence.open !== 'function') return undefined
|
|
896
|
+
let handle
|
|
897
|
+
try {
|
|
898
|
+
handle = await sessionPersistence.open(sessionId, 'read')
|
|
899
|
+
} catch {
|
|
900
|
+
return undefined
|
|
901
|
+
}
|
|
902
|
+
const ids = new Set()
|
|
903
|
+
try {
|
|
904
|
+
let offset = 0
|
|
905
|
+
for (let page = 0; page < ATTACHMENT_READ_PAGE_LIMIT; page += 1) {
|
|
906
|
+
const result = await handle.read(offset, ATTACHMENT_READ_PAGE_SIZE)
|
|
907
|
+
const events = Array.isArray(result?.events) ? result.events : []
|
|
908
|
+
if (events.length === 0) break
|
|
909
|
+
for (const event of events) collectAttachmentIds(event, ids)
|
|
910
|
+
const lastSeq = Number(events[events.length - 1]?.seq)
|
|
911
|
+
if (!Number.isFinite(lastSeq) || lastSeq < offset) break
|
|
912
|
+
offset = lastSeq + 1
|
|
913
|
+
if (events.length < ATTACHMENT_READ_PAGE_SIZE) break
|
|
914
|
+
}
|
|
915
|
+
} catch {
|
|
916
|
+
// torn or unreadable tail: keep whatever was collected so far
|
|
917
|
+
} finally {
|
|
918
|
+
try {
|
|
919
|
+
await handle.close()
|
|
920
|
+
} catch {
|
|
921
|
+
/* already closed */
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
return ids
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
/**
|
|
928
|
+
* Delete attachment blobs whose owning session has been gone for `attachmentCleanupDays`,
|
|
929
|
+
* plus (optionally) blobs no stored session references and that are at least that old.
|
|
930
|
+
*/
|
|
931
|
+
async function runAttachmentCleanup(trigger, { dryRun = cleanupDryRun } = {}) {
|
|
932
|
+
if (!attachmentCleanup) return { skipped: 'disabled' }
|
|
933
|
+
const snapshots = await loadPersistenceSnapshots()
|
|
934
|
+
if (!snapshots) return { skipped: 'sessionPersistence-unavailable' }
|
|
935
|
+
if (snapshots.length === 0) return { skipped: 'no-stored-sessions' }
|
|
936
|
+
const now = Date.now()
|
|
937
|
+
const liveSessionIds = new Set()
|
|
938
|
+
try {
|
|
939
|
+
for (const session of ctx.sessions.list()) liveSessionIds.add(String(session.id))
|
|
940
|
+
} catch {
|
|
941
|
+
/* sessions unavailable this tick */
|
|
942
|
+
}
|
|
943
|
+
const ledger = await readAttachmentLedger(attachmentLedgerPath)
|
|
944
|
+
const alive = new Set()
|
|
945
|
+
const referenced = new Set()
|
|
946
|
+
let rescanned = 0
|
|
947
|
+
let scannedFailures = 0
|
|
948
|
+
for (const snapshot of snapshots) {
|
|
949
|
+
const id = String(snapshot?.header?.id ?? '')
|
|
950
|
+
if (!id) continue
|
|
951
|
+
alive.add(id)
|
|
952
|
+
const revision = String(snapshot?.revision ?? '')
|
|
953
|
+
const entry = ledger.sessions[id]
|
|
954
|
+
let hashes
|
|
955
|
+
if (entry && entry.revision === revision && Array.isArray(entry.hashes)) {
|
|
956
|
+
hashes = entry.hashes
|
|
957
|
+
} else {
|
|
958
|
+
const scanned = await readSessionAttachmentIds(id)
|
|
959
|
+
if (!scanned) {
|
|
960
|
+
scannedFailures += 1
|
|
961
|
+
if (entry && Array.isArray(entry.hashes)) hashes = entry.hashes
|
|
962
|
+
else continue
|
|
963
|
+
} else {
|
|
964
|
+
hashes = [...scanned]
|
|
965
|
+
ledger.sessions[id] = { revision, hashes, scannedAt: now }
|
|
966
|
+
rescanned += 1
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
for (const hash of hashes) referenced.add(hash)
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
// ── safety interlocks (portability) ───────────────────────────────────────
|
|
973
|
+
// The layout and the attachment-id vocabulary are Host-owned. On another platform
|
|
974
|
+
// (HarmonyOS / mobile / a future store version) either may differ; sweeping then
|
|
975
|
+
// must fail closed instead of treating "nothing referenced" as "everything stale".
|
|
976
|
+
const blobs = await listAttachmentBlobs(attachmentsRoot)
|
|
977
|
+
if (blobs.length === 0) {
|
|
978
|
+
return { skipped: 'no-attachment-objects', attachmentsRoot }
|
|
979
|
+
}
|
|
980
|
+
if (referenced.size === 0) {
|
|
981
|
+
return {
|
|
982
|
+
skipped: 'no-attachment-references',
|
|
983
|
+
objects: blobs.length,
|
|
984
|
+
storedSessions: alive.size,
|
|
985
|
+
rescanned,
|
|
986
|
+
scanFailures: scannedFailures,
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
// Stamp sessions that vanished since the previous tick.
|
|
991
|
+
let markedOrphaned = 0
|
|
992
|
+
for (const [id, entry] of Object.entries(ledger.sessions)) {
|
|
993
|
+
if (alive.has(id) || liveSessionIds.has(id)) continue
|
|
994
|
+
if (!entry) continue
|
|
995
|
+
if (!(Number(entry.orphanedAt) > 0)) {
|
|
996
|
+
entry.orphanedAt = now
|
|
997
|
+
markedOrphaned += 1
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
const graceDays = attachmentCleanupDays
|
|
1002
|
+
const due = new Map()
|
|
1003
|
+
for (const [id, entry] of Object.entries(ledger.sessions)) {
|
|
1004
|
+
if (!entry || alive.has(id) || liveSessionIds.has(id)) continue
|
|
1005
|
+
const orphanedAt = Number(entry.orphanedAt) || 0
|
|
1006
|
+
if (!(orphanedAt > 0)) continue
|
|
1007
|
+
if (now - orphanedAt < graceDays * DAY_MS) continue
|
|
1008
|
+
due.set(id, entry)
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
const deleted = []
|
|
1012
|
+
let freedBytes = 0
|
|
1013
|
+
let skippedRecent = 0
|
|
1014
|
+
let skippedReferenced = 0
|
|
1015
|
+
const drop = async (hash, reason) => {
|
|
1016
|
+
const blobPath = join(attachmentsRoot, 'v1', 'objects', hash.slice(0, 2), hash)
|
|
1017
|
+
let info
|
|
1018
|
+
try {
|
|
1019
|
+
info = await stat(blobPath)
|
|
1020
|
+
} catch {
|
|
1021
|
+
return false
|
|
1022
|
+
}
|
|
1023
|
+
if (now - info.mtimeMs < ATTACHMENT_MIN_AGE_MS) {
|
|
1024
|
+
skippedRecent += 1
|
|
1025
|
+
return false
|
|
1026
|
+
}
|
|
1027
|
+
if (!dryRun) {
|
|
1028
|
+
try {
|
|
1029
|
+
await rm(blobPath, { force: true })
|
|
1030
|
+
} catch {
|
|
1031
|
+
return false
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
deleted.push({ hash, bytes: info.size, reason })
|
|
1035
|
+
freedBytes += info.size
|
|
1036
|
+
return true
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
for (const entry of due.values()) {
|
|
1040
|
+
for (const hash of entry.hashes ?? []) {
|
|
1041
|
+
if (referenced.has(hash)) {
|
|
1042
|
+
skippedReferenced += 1
|
|
1043
|
+
continue
|
|
1044
|
+
}
|
|
1045
|
+
await drop(hash, 'session-deleted')
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
const known = new Set()
|
|
1050
|
+
for (const entry of Object.values(ledger.sessions)) {
|
|
1051
|
+
for (const hash of entry?.hashes ?? []) known.add(hash)
|
|
1052
|
+
}
|
|
1053
|
+
let sweptOrphans = 0
|
|
1054
|
+
if (attachmentSweepOrphans) {
|
|
1055
|
+
for (const blob of blobs) {
|
|
1056
|
+
if (referenced.has(blob.hash) || known.has(blob.hash)) continue
|
|
1057
|
+
if (now - blob.mtime < graceDays * DAY_MS) continue
|
|
1058
|
+
if (await drop(blob.hash, 'unreferenced')) sweptOrphans += 1
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
if (!dryRun) {
|
|
1063
|
+
for (const id of due.keys()) delete ledger.sessions[id]
|
|
1064
|
+
ledger.lastSweepAt = now
|
|
1065
|
+
await writeAttachmentLedger(attachmentLedgerPath, ledger)
|
|
1066
|
+
if (deleted.length > 0) {
|
|
1067
|
+
try {
|
|
1068
|
+
await appendFile(
|
|
1069
|
+
attachmentGcLogPath,
|
|
1070
|
+
`${JSON.stringify({ at: new Date(now).toISOString(), trigger, deleted })}\n`,
|
|
1071
|
+
'utf8',
|
|
1072
|
+
)
|
|
1073
|
+
} catch {
|
|
1074
|
+
/* audit log is best-effort */
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
const report = {
|
|
1080
|
+
dryRun,
|
|
1081
|
+
storedSessions: alive.size,
|
|
1082
|
+
objects: blobs.length,
|
|
1083
|
+
rescanned,
|
|
1084
|
+
scanFailures: scannedFailures,
|
|
1085
|
+
referencedBlobs: referenced.size,
|
|
1086
|
+
markedOrphaned,
|
|
1087
|
+
dueSessions: due.size,
|
|
1088
|
+
deleted: deleted.length,
|
|
1089
|
+
freedBytes,
|
|
1090
|
+
sweptOrphans,
|
|
1091
|
+
skippedRecent,
|
|
1092
|
+
skippedReferenced,
|
|
1093
|
+
}
|
|
1094
|
+
if (report.deleted > 0 || report.markedOrphaned > 0) {
|
|
1095
|
+
console.info(
|
|
1096
|
+
`[session-task-tmpdir] attachments(${trigger})${dryRun ? ' dry-run' : ''}: ` +
|
|
1097
|
+
`deleted ${report.deleted} (${(freedBytes / 1048576).toFixed(1)} MB), ` +
|
|
1098
|
+
`marked ${report.markedOrphaned}, due sessions ${report.dueSessions}, ` +
|
|
1099
|
+
`referenced ${report.referencedBlobs}, rescanned ${report.rescanned}`,
|
|
1100
|
+
)
|
|
1101
|
+
}
|
|
1102
|
+
return report
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
async function runCleanup(trigger, { dryRun = cleanupDryRun, withAttachments = true } = {}) {
|
|
734
1106
|
if (cleaning || !(orphanCleanupDays > 0)) return undefined
|
|
735
1107
|
cleaning = true
|
|
736
1108
|
try {
|
|
@@ -780,6 +1152,15 @@ export function apply(ctx, config) {
|
|
|
780
1152
|
`unmarked ${report.kept.unmarked} / errors ${report.kept.error}`,
|
|
781
1153
|
)
|
|
782
1154
|
}
|
|
1155
|
+
// Attachment store sweep (v0.6): same tick, same 7-day semantics.
|
|
1156
|
+
if (withAttachments) {
|
|
1157
|
+
try {
|
|
1158
|
+
report.attachments = await runAttachmentCleanup(trigger, { dryRun })
|
|
1159
|
+
} catch (error) {
|
|
1160
|
+
console.warn('[session-task-tmpdir] attachment cleanup failed:', error?.message ?? error)
|
|
1161
|
+
report.attachments = { error: String(error?.message ?? error) }
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
783
1164
|
return report
|
|
784
1165
|
} catch (error) {
|
|
785
1166
|
console.warn('[session-task-tmpdir] cleanup failed:', error?.message ?? error)
|
|
@@ -823,7 +1204,7 @@ export function apply(ctx, config) {
|
|
|
823
1204
|
`Before writing a long-term file **outside** \`${rootDirName}/\`, you MUST ask the user with \`ask_user_question\` (multiple choice; the free-text answer box lets the user type any other location), for example:`,
|
|
824
1205
|
'- A. 不长期保存',
|
|
825
1206
|
'- B. 工作区根目录保存',
|
|
826
|
-
`Keep the option labels short exactly like the two above; put the explanation in each option's \`description\` instead (A: 放 ${rootDirName}
|
|
1207
|
+
`Keep the option labels short exactly like the two above; put the explanation in each option's \`description\` instead (A: 放 ${rootDirName}/<会话标题>/,临时目录,随会话删除后 ${orphanCleanupDays} 天清理;B: 长期保存到会话工作区根目录下). Never stuff the path details into the label.`,
|
|
827
1208
|
'Do not write anything until the user answers.',
|
|
828
1209
|
'Picking B: keep it under the workspace root; pick a clear folder name yourself, call `longterm_approve` on it, then write — no need to ask the user to confirm the folder name (same as how DSH normally names things).',
|
|
829
1210
|
'Any answer other than A/B (e.g. a path the user typed in the free-text box) is a designated location: if it is missing or ambiguous, ask the user to confirm it before calling `longterm_approve` — never guess.',
|
|
@@ -941,11 +1322,12 @@ export function apply(ctx, config) {
|
|
|
941
1322
|
// Manual cleanup trigger (also handy for verifying the scheduled pass).
|
|
942
1323
|
ctx.effect(() =>
|
|
943
1324
|
ctx.tools.register({
|
|
944
|
-
name: 'tmp_cleanup', description: `Run the task temp folder cleanup now: marker-owned folders whose owning session has been deleted for more than ${orphanCleanupDays} days are removed; everything else is reported as kept. Pass dryRun to only report.`,
|
|
1325
|
+
name: 'tmp_cleanup', description: `Run the task temp folder cleanup now: marker-owned folders whose owning session has been deleted for more than ${orphanCleanupDays} days are removed, and (v0.6) the attachment store reclaims the blobs of sessions deleted more than ${attachmentCleanupDays} days ago; everything else is reported as kept. Pass dryRun to only report.`,
|
|
945
1326
|
parameters: {
|
|
946
1327
|
type: 'object',
|
|
947
1328
|
properties: {
|
|
948
1329
|
dryRun: { type: 'boolean', description: 'Only report what would be marked or deleted.' },
|
|
1330
|
+
attachments: { type: 'boolean', description: 'Also sweep the attachment store (default true).' },
|
|
949
1331
|
},
|
|
950
1332
|
additionalProperties: false,
|
|
951
1333
|
},
|
|
@@ -958,14 +1340,16 @@ export function apply(ctx, config) {
|
|
|
958
1340
|
deleted: { type: 'number' },
|
|
959
1341
|
markedOrphaned: { type: 'number' },
|
|
960
1342
|
kept: { type: 'object' },
|
|
1343
|
+
attachments: { type: 'object' },
|
|
961
1344
|
},
|
|
962
1345
|
},
|
|
963
1346
|
render: (_args, value) => [{ type: 'text', text: JSON.stringify(value) }],
|
|
964
1347
|
},
|
|
965
1348
|
async execute(args) {
|
|
966
|
-
const
|
|
1349
|
+
const withAttachments = args?.attachments !== false
|
|
1350
|
+
const report = await runCleanup('manual', { dryRun: args?.dryRun === true, withAttachments })
|
|
967
1351
|
if (!report) return { disabled: true, dryRun: false, deleted: 0, markedOrphaned: 0, kept: {} }
|
|
968
|
-
|
|
1352
|
+
const value = {
|
|
969
1353
|
disabled: false,
|
|
970
1354
|
dryRun: report.dryRun,
|
|
971
1355
|
deleted: report.deleted.length,
|
|
@@ -973,6 +1357,25 @@ export function apply(ctx, config) {
|
|
|
973
1357
|
kept: report.kept,
|
|
974
1358
|
paths: report.deleted.map((entry) => entry.path),
|
|
975
1359
|
}
|
|
1360
|
+
if (report.attachments) {
|
|
1361
|
+
const a = report.attachments
|
|
1362
|
+
value.attachments = a.skipped || a.error
|
|
1363
|
+
? a
|
|
1364
|
+
: {
|
|
1365
|
+
dryRun: Boolean(a.dryRun),
|
|
1366
|
+
deleted: a.deleted ?? 0,
|
|
1367
|
+
freedMb: Number(((a.freedBytes ?? 0) / 1048576).toFixed(1)),
|
|
1368
|
+
dueSessions: a.dueSessions ?? 0,
|
|
1369
|
+
storedSessions: a.storedSessions ?? 0,
|
|
1370
|
+
objects: a.objects ?? 0,
|
|
1371
|
+
referencedBlobs: a.referencedBlobs ?? 0,
|
|
1372
|
+
markedOrphaned: a.markedOrphaned ?? 0,
|
|
1373
|
+
sweptOrphans: a.sweptOrphans ?? 0,
|
|
1374
|
+
skippedRecent: a.skippedRecent ?? 0,
|
|
1375
|
+
skippedReferenced: a.skippedReferenced ?? 0,
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1378
|
+
return value
|
|
976
1379
|
},
|
|
977
1380
|
}),
|
|
978
1381
|
)
|
package/locale/en.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"meta": {
|
|
3
3
|
"title": "Task Temp Folder",
|
|
4
|
-
"description": "Gives every session a task temp folder _tmp/<session title>/ in its working directory, renames it whenever the session title changes, and deletes it 7 days after the session is deleted."
|
|
4
|
+
"description": "Gives every session a task temp folder _tmp/<session title>/ in its working directory, renames it whenever the session title changes, and deletes it 7 days after the session is deleted; the same pass also reclaims that session's attachments."
|
|
5
5
|
}
|
|
6
6
|
}
|
package/locale/zh.json
CHANGED
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leiyi-works/dsh-session-tmpdir",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "Per-session task temp folder: _tmp/<session title>/ inside the session working directory, auto-created and auto-renamed when the session title changes; folders are deleted 7 days after their session is deleted, with a manual cleanup tool. Also guards the workspace root: scratch files must live under _tmp/, and any long-term artifact outside _tmp/ requires the user's approval (longterm_approve) before write/edit or shell commands may create it — otherwise the call is denied and the model is told to ask the user first, then fall back to _tmp.",
|
|
5
|
+
"description": "Per-session task temp folder: _tmp/<session title>/ inside the session working directory, auto-created and auto-renamed when the session title changes; folders are deleted 7 days after their session is deleted, with a manual cleanup tool. The same tick also reclaims the attachment store: <DSH_HOME>/attachments is content-addressed and never garbage collected by the Host, so the plugin keeps a per-session attachment ledger, deletes the blobs of sessions deleted N days ago after re-checking that no surviving session references them, sweeps historical orphans, and appends every deletion to an audit log. Also guards the workspace root: scratch files must live under _tmp/, and any long-term artifact outside _tmp/ requires the user's approval (longterm_approve) before write/edit or shell commands may create it — otherwise the call is denied and the model is told to ask the user first, then fall back to _tmp.",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": "./index.js",
|
|
8
8
|
"./locale/*.json": "./locale/*.json",
|