dsh-context-toolkit 0.3.1 → 0.3.2

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 CHANGED
@@ -115,7 +115,7 @@ Tool switches: `tools.execute`, `tools.executeFile`, `tools.batchExecute`, `tool
115
115
  The offline suites need no DSH runtime:
116
116
 
117
117
  ```bash
118
- node smoke.mjs # 82 checks: contract, runtime discovery, tools, gate (segment matching, host-aligned path base, script_path and host-fetcher coverage), output slimming and reversible retrieval, resource disposal, knowledge base, observability, version reporting
118
+ node smoke.mjs # 85 checks: contract, runtime discovery, tools, gate (segment matching, host-aligned path base, script_path and host-fetcher coverage), output slimming with reversible retrieval and retention, resource disposal, knowledge base, observability, version reporting
119
119
  node test-client.mjs # 27 checks: browser-half contract and bilingual dictionary parity
120
120
  node test-package.mjs # 20 checks: publish manifest and the zero-dependency constraint
121
121
  node release-check.mjs # 30 checks: release readiness (manifest, deps, tool count, gate matching, version, SHA coverage, no hardcoded paths)
@@ -139,6 +139,7 @@ At `tools/post-execute`, the strategy follows the shape:
139
139
  **Two gates**: a candidate must be at most 72% of the original bytes *and* have a strictly lower token estimate, or it is dropped — no saving means no claim, and the ledger never overstates.
140
140
 
141
141
  **Reversibility is the red line**: every lossy path stores the exact original first and injects a `[ctk_retrieve id=…]` marker, read back with `ctk_retrieve`. The originals live in the **same database as the FTS5 index** — one file, two jobs: FTS5 finds sections, blobs restore them.
142
+ - **Retention**: `slimOriginalTtlMinutes` (default 1440 = one day, 0 = forever) is swept once on every load; when a marker cannot be delivered the blob is rolled back by id, so an unrelated original is never taken with it.
142
143
 
143
144
  ## Known limits
144
145
 
package/README.zh.md CHANGED
@@ -116,7 +116,7 @@ await ctk_search({ queries: ['rate limit', 'auth'] }); // 只取相关片段
116
116
  离线套件不依赖 DSH 运行时:
117
117
 
118
118
  ```bash
119
- node smoke.mjs # 82 项:契约、运行时探测、工具、门禁(分段匹配 + 路径基准对齐 + script_path 与取网页工具覆盖)、输出瘦身与可逆取回、资源释放、知识库、观测、版本自述
119
+ node smoke.mjs # 85 项:契约、运行时探测、工具、门禁(分段匹配 + 路径基准对齐 + script_path 与取网页工具覆盖)、输出瘦身与可逆取回及保留期、资源释放、知识库、观测、版本自述
120
120
  node test-client.mjs # 27 项:浏览器半体契约与双语字典一致性
121
121
  node test-package.mjs # 20 项:发布清单与零依赖约束
122
122
  node release-check.mjs # 30 项:发布就绪(清单/依赖/工具数/门禁匹配/版本/SHA 覆盖/无硬编码路径)
@@ -140,6 +140,7 @@ node release-check.mjs # 30 项:发布就绪(清单/依赖/工具数/门禁
140
140
  **双重门**:候选必须同时满足「字节 ≤ 72%」且「估算 token 严格下降」,否则放弃——压不动就不压,台账绝不虚报节省。
141
141
 
142
142
  **可逆红线**:任何有损路径先把原文**逐字节**存好并注入 `[ctk_retrieve id=…]` 标记,用 `ctk_retrieve` 取回。原文与 FTS5 索引同在**同一个数据库**(一个文件两个职责:FTS5 找段落,blob 逐字节还原)。
143
+ - **原文保留期**:`slimOriginalTtlMinutes`(默认 1440 = 一天,0 = 永久)由插件在**每次加载时**执行一次清理;标记投递失败时按 id 精确回滚,不会牵连其他原文。
143
144
 
144
145
  ## 已知边界
145
146
 
package/index.js CHANGED
@@ -284,6 +284,19 @@ export function apply(ctx, rawConfig) {
284
284
  log.warn('knowledge base unavailable: ' + String((error && error.message) || error))
285
285
  }
286
286
 
287
+ // Retention is enforced here, once per load, because the alternative was a setting
288
+ // the user could see and the code never read: slimOriginalTtlMinutes was exposed in
289
+ // the settings page while nothing consumed it, so stored originals accumulated
290
+ // forever and the promise the panel made was not kept.
291
+ if (store) {
292
+ try {
293
+ const swept = store.purgeBlobsOlderThan(Number(config.slimOriginalTtlMinutes))
294
+ if (swept.removed > 0) log.info('retention sweep removed ' + swept.removed + ' stored original(s)')
295
+ } catch (error) {
296
+ log.warn('retention sweep failed: ' + String((error && error.message) || error))
297
+ }
298
+ }
299
+
287
300
  registerRoutingGate(ctx, config, stats)
288
301
  registerOutputSlimmer(ctx, config, stats, store, log)
289
302
  registerTools(ctx, config, stats, storageDir, log)
@@ -559,7 +572,10 @@ function registerOutputSlimmer(ctx, config, stats, store, log) {
559
572
  if (Buffer.byteLength(slimmed, 'utf8') >= outcome.originalBytes) {
560
573
  stats.slimSkipped += 1
561
574
  rewritten.push(block)
562
- if (marker) { try { store.purgeBlobs({ all: false }) } catch { /* blob stays, harmless */ } }
575
+ // The blob was written but the marker could not be delivered, so nothing will
576
+ // ever reference it. Removing this specific id is the point: the previous call
577
+ // passed { all: false } with no age, which purgeBlobs treats as "nothing to do".
578
+ if (marker) { try { store.purgeBlobs({ ids: [id] }) } catch { /* blob stays, harmless */ } }
563
579
  continue
564
580
  }
565
581
 
@@ -1438,6 +1454,7 @@ function registerObservabilityTools(ctx, config, stats, store, storageDir, log)
1438
1454
  '- 片段 chunks: ' + kb.chunks,
1439
1455
  '- 原文字节 bytes: ' + kb.bytes,
1440
1456
  '- 可逆原文 blobs: ' + (kb.blobs || 0) + '(' + (kb.blobBytes || 0) + ' 字节,可用 ctk_retrieve 逐字节取回)',
1457
+ '- 原文保留期 retention: ' + (Number(config.slimOriginalTtlMinutes) > 0 ? Number(config.slimOriginalTtlMinutes) + ' 分钟(每次加载时清理过期项)' : '永久保留'),
1441
1458
  '- 位置: ' + storageDir,
1442
1459
  '',
1443
1460
  '### 本插件注册的工具',
package/lib/store.js CHANGED
@@ -165,13 +165,27 @@ function makeApi(db, dbPath, storageDir) {
165
165
  return { id: String(id), text: Buffer.from(row.body).toString('utf8'), bytes: Number(row.bytes) || 0, createdAtMs: Number(row.created_at_ms) || 0 }
166
166
  },
167
167
 
168
- /** Drop stored originals older than the given age, or all of them. */
169
- purgeBlobs({ olderThanMs = 0, all = false } = {}) {
168
+ /**
169
+ * Drop stored originals: specific ids, everything older than an age, or all.
170
+ *
171
+ * The three modes answer different questions. A caller whose marker never reached
172
+ * the model removes the one id it just wrote; a retention policy removes by age; a
173
+ * reset removes all. Removing by id first means a rollback cannot take an unrelated
174
+ * original down with it.
175
+ */
176
+ purgeBlobs({ ids = [], olderThanMs = 0, all = false } = {}) {
170
177
  if (all) {
171
178
  const n = db.prepare('SELECT COUNT(*) AS n FROM blobs').get().n
172
179
  db.exec('DELETE FROM blobs;')
173
180
  return { removed: Number(n) || 0 }
174
181
  }
182
+ const list = (Array.isArray(ids) ? ids : [ids]).map((v) => String(v || '')).filter(Boolean)
183
+ if (list.length) {
184
+ const del = db.prepare('DELETE FROM blobs WHERE blob_id = ?')
185
+ let removed = 0
186
+ for (const id of list) removed += Number(del.run(id).changes) || 0
187
+ return { removed }
188
+ }
175
189
  if (!(olderThanMs > 0)) return { removed: 0 }
176
190
  const cutoff = Date.now() - olderThanMs
177
191
  const n = db.prepare('SELECT COUNT(*) AS n FROM blobs WHERE created_at_ms < ?').get(cutoff).n
@@ -179,6 +193,15 @@ function makeApi(db, dbPath, storageDir) {
179
193
  return { removed: Number(n) || 0 }
180
194
  },
181
195
 
196
+ /** Drop originals older than a retention in minutes; 0 means keep forever. */
197
+ purgeBlobsOlderThan(minutes) {
198
+ const n = Number(minutes)
199
+ // An explicit 0 is a policy ("keep forever"), not an oversight, so it must not be
200
+ // read as "expire immediately".
201
+ if (!(n > 0)) return { removed: 0 }
202
+ return this.purgeBlobs({ olderThanMs: n * 60000 })
203
+ },
204
+
182
205
  /** File one document, replacing any earlier version of the same path. */
183
206
  indexDocument({ path, title, text, mtimeMs }) {
184
207
  const abs = normalizeTarget(path)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-context-toolkit",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "DeepSeek Harness bundle: native ctk_* sandbox tools, a routing gate that keeps bulk output out of the transcript, and a local full-text knowledge base. No third-party runtime dependencies.",
5
5
  "repository": {
6
6
  "type": "git",