dsh-limit-break 1.2.0 → 1.3.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/CHANGELOG.md CHANGED
@@ -5,6 +5,35 @@ All notable changes to `dsh-limit-break`. Format follows
5
5
 
6
6
  ## Unreleased
7
7
 
8
+ ## 1.3.0 — 2026-10-22
9
+
10
+ ### Added
11
+ - **`memory_prune`: the manual cleanup, made repeatable.** The 2026-10-21 session pruned a
12
+ 27-fact store to 17 by hand — merging benchmark facts into a suite, deleting session state,
13
+ dropping derivative copies. That judgment is now a tool with deterministic rules:
14
+ `session-state` (a fact that reads as conversational state) and `exact-duplicate` (same
15
+ statement; the richer fact survives). It reports a plan first, applies only with
16
+ `apply: true`, copies the store to `facts.json.bak-<timestamp>`, and moves removed facts to
17
+ `facts.archive.json`. Nothing is ever deleted. Softer suspicions — two facts citing the same
18
+ measurements, two facts claiming opposite things on one topic — are returned as read-only
19
+ warnings, since they need a human call.
20
+ - `tools/prune-selftest.mjs` covers the dry run (must not touch the store), the apply path
21
+ (archive + backup), the richer-duplicate rule, and idempotence (a second run must not grow
22
+ the archive).
23
+
24
+ ## 1.2.1 — 2026-10-21
25
+
26
+ ### Fixed
27
+ - **The log could not tell you which copy was live.** `User-Agent` and every
28
+ `plugin-load.log` record now carry the version read from the installed `package.json`
29
+ instead of a hard-coded string, and mount records add `moduleDir`. A rolled-back or
30
+ hand-copied install is now visible in one line instead of a guess.
31
+ - **A doubly-activated plugin looked like a broken switch.** When a profile patch keeps a
32
+ second `insert` row for the same id, the loader mounts the plugin twice, the bundle switch
33
+ has nothing left to turn off, and the row switch is locked as *unaddressable*. Mounts are
34
+ now counted per store and a second live mount writes a `duplicate-apply` record with a
35
+ hint, so the misconfiguration is diagnosable from the log instead of from the symptom.
36
+
8
37
  ## 1.2.0 — 2026-10-20
9
38
 
10
39
  ### Fixed
package/MAINTAIN.md CHANGED
@@ -14,7 +14,7 @@ cd D:/deepseek-harness/default-workspace/capability-demo/plugin/dsh-limit-break
14
14
  PY="C:/Users/lenovo/.dsh/dsh-runtimes/dsh-primary-runtime/dependencies/python/python.exe"
15
15
  $PY tools/maintain.py status # 我发布的版本在哪、PR 合没合、装的是不是 latest
16
16
  $PY tools/maintain.py probe # 重新测一遍环境,和上次的 baseline 做 diff
17
- $PY tools/maintain.py verify # 契约测试:4 个工具是否仍然注册成功
17
+ $PY tools/maintain.py verify # 契约测试:5 个工具是否仍然注册成功
18
18
  ```
19
19
 
20
20
  先读 `facts.json` 里 `topic` 为 `maintenance` / `release` 的条目,再看 `probe` 的
@@ -108,25 +108,21 @@ git add -A && git commit -m "..." && git push
108
108
 
109
109
  ---
110
110
 
111
- ## Backlog(按 2026-10-20 结束时的真实状态)
111
+ ## Backlog(按 2026-10-20 20:06 的真实状态)
112
112
 
113
- - [ ] **1.2.0 待上线**:commit `69b726f` 只在本地。`git push origin main` 连续 3 次失败
114
- (github.com:443 连接重置 / 连不上;api.github.com 仍 200,所以是 SNI 级封锁)。
115
- `npm publish` 需要用户的 OTP,本轮未执行。恢复网络后跑:
116
- `git push origin main && npm publish`(仓库与 `profiles/desktop/node_modules/dsh-limit-break`
117
- 两份副本已同步且 `verify` PASS)。
118
- - [ ] 旧定时任务 `schedule-803cdbdc`(会话 5133bee1)跨会话删不掉(`schedule_not_found`),
119
- 已从 `storages/schedule.json` 摘行;运行中的 hub 会从内存回写,**重启后才彻底消失**,
120
- 或由用户在 5133bee1 的定时任务面板手动删。INDEX.md 里留了自删提示。
113
+ - [x] **1.2.0 已上线**(2026-10-20T12:02:52Z):npm `latest` = `1.2.0`,GitHub `origin/main` =
114
+ `fd844a4`,`verify` PASS,`test_plugin.mjs` 6/6 PASS。用户 20:04 完成了 OTP 发布和推送。
115
+ `env.node_npm` 与 `release.dsh-limit-break` 两条事实已回写。
116
+ - [x] 旧定时任务 `schedule-803cdbdc`(会话 5133bee1)已由用户在面板删除;`storages/schedule.json`
117
+ 里只剩 `schedule-0b6c60ea`(hello world)和新的 `schedule-b3675d28`。无需再处理。
121
118
  - [ ] 市场条目 PR `awesome-dsh-plugin/awesome-dsh-plugin#8296` 仍是 open;合并前 `status` 会一直报
122
119
  `market listing not live yet`。找合并节奏,不要重提。
123
- - [ ] npm 上还挂着占位包 `0.0.0-stage`(`stub: true`)。可以考虑 `npm deprecate dsh-limit-break@0.0.0-stage "placeholder"`。
124
- - [ ] `1.0.0` 是无元数据的旧包体(author/repository 都不在)。可以 deprecate 掉,减少噪音。
120
+ - [ ] npm 上还挂着占位包 `0.0.0-stage`(`stub: true`)。`npm deprecate` 也要 OTP(本轮 EOTP 失败);
121
+ 用户一条命令即可:`npm deprecate dsh-limit-break@0.0.0-stage "placeholder - use 1.2.0"`。
122
+ - [ ] `1.0.0` 是无元数据的旧包体(author/repository 都不在)。同样一条 `npm deprecate` 可清掉噪音(需 OTP)。
125
123
  - [ ] `facts.json` 里 `env.gpu` 仍写着"no GPU compute benchmark was run"——已过时(GPU 实测 54.3×)。
126
- - [ ] 插件工具对**已经打开的会话**不可见:插件在 `19:07:45` mount 成功(4 个工具都在),
127
- 但那个时间点之前启动的会话,工具集里没有 `memory_*`。新会话才有。
128
- 值得查清:是工具清单在会话创建时快照,还是 hub 与 agent 进程的工具注册面不同。
129
- 查清后要么修(如果能在插件侧解决),要么写进 README 作为已知限制。
124
+ - [x] 插件工具对**已经打开的会话**不可见:已在 README「Known limits」第 1 条写明
125
+ (tool list 是 per-session 快照,插件侧修不了)。事实 `harness.plugin-tool-visibility` 有实测。
130
126
  - [ ] `resilient_fetch` 目前按"主机名别名"降级,可以继续加:`--resolve` 到已知 IP、
131
127
  IPv6 优先、以及把成功路径写回记忆库(现在是只读阶梯)。
132
128
  - [ ] 没有任何自动化测试覆盖 `memory_save` 的并发写。单进程够用,多进程未验证。
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # dsh-limit-break
2
2
 
3
- Four small tools for [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness) that came out of a
3
+ Five small tools for [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness) that came out of a
4
4
  self-benchmark: two things an agent is normally told it cannot do, made cheap enough to just do.
5
5
 
6
6
  | Tool | What it does |
@@ -9,17 +9,32 @@ self-benchmark: two things an agent is normally told it cannot do, made cheap en
9
9
  | `memory_save` | Write or overwrite one fact: `{ id, statement, value?, unit?, details?, tags?, source? }`. |
10
10
  | `resilient_fetch` | Fetch with a four-rung ladder and a report of which rung answered. |
11
11
  | `memory_stats` | Where the store is, how many facts, how big. |
12
+ | `memory_prune` | Finds redundant facts (session state, exact duplicates) and archives them. Dry run unless `apply: true`; nothing is ever deleted. |
12
13
 
13
- Zero dependencies. No import-time side effects. Everything fails soft — a broken store or a dead
14
+ Zero npm dependencies. No import-time side effects. Everything fails soft — a broken store or a dead
14
15
  network returns an error object, never an exception that could take the host down.
15
16
 
16
- ## Why these four
17
+ **Runtime requirement: `curl`.** Rung 1 of the fetch ladder is plain `fetch` and needs nothing; rungs
18
+ 2–4 (IPv6 → IPv4 → sibling hostname) shell out to `curl`, so it has to be on `PATH`. Windows 10+,
19
+ macOS and mainstream Linux all ship it. Without it the ladder degrades to rung 1 and says so in the
20
+ report instead of pretending it climbed.
21
+
22
+ ## Why these five
17
23
 
18
24
  **Memory.** A session starts from zero. A store on disk plus a read at the start is enough to beat
19
25
  that. Measured with two brand-new agents, zero context, the same five questions: **5/5 with the
20
26
  store, 0/5 without** — and the control did not hallucinate, it answered "I don't have that" five
21
27
  times.
22
28
 
29
+ **Redundancy.** A store's slow failure mode is drift: two facts restating one measurement, or a
30
+ fact that is really session state, until a recall returns two slightly different answers. Cleaning
31
+ it by hand works once; `memory_prune` makes it repeatable. The two safe rules — *session state* and
32
+ *exact duplicates* — are deterministic, reported as a plan first, and applied only on
33
+ `apply: true`, with the pre-prune store copied to `facts.json.bak-<timestamp>` and the dropped
34
+ facts moved to `facts.archive.json`. Nothing is deleted. Soft suspicions (facts citing the same
35
+ measurements, facts that contradict each other on one topic) come back as read-only warnings,
36
+ because those need a human decision.
37
+
23
38
  **Network.** The reflex "this host is unreachable" is wrong more often than it is right. Measured on
24
39
  one network, same edge IP, 75 seconds apart: `api.wikimedia.org` → 200 in 0.43 s,
25
40
  `en.wikipedia.org` → connection reset in 1.21 s, `api.wikimedia.org` again → 200 in 0.39 s. The
@@ -64,6 +79,9 @@ package the profile does not declare is silently ignored.
64
79
  Plain JSON on purpose: anything that can read a file can use it, with or without this plugin.
65
80
  Writes are tmp-file + rename.
66
81
 
82
+ `memory_prune` keeps the same contract: a plan first, a `facts.json.bak-<timestamp>` copy before
83
+ any write, and the removed facts kept in `facts.archive.json` next to the store.
84
+
67
85
  ## Disable / remove
68
86
 
69
87
  * Hot: set `disabled: true` on the entry in `cordis.patch.yml` — Cordis recomposes in about a second.
package/lib/index.js CHANGED
@@ -21,6 +21,7 @@ import { appendFileSync, mkdirSync, readFileSync, statSync, writeFileSync } from
21
21
  import { mkdir, readFile, rename, stat, writeFile } from 'node:fs/promises'
22
22
  import { homedir } from 'node:os'
23
23
  import { dirname, isAbsolute, join, resolve } from 'node:path'
24
+ import { fileURLToPath } from 'node:url'
24
25
  import { promisify } from 'node:util'
25
26
 
26
27
  const execFileAsync = promisify(execFile)
@@ -28,7 +29,26 @@ const execFileAsync = promisify(execFile)
28
29
  export const name = 'limit-break'
29
30
  export const inject = ['tools']
30
31
 
31
- const UA = 'dsh-limit-break/1.2 (+local)'
32
+ /**
33
+ * Our own version, read from the package we were loaded out of.
34
+ *
35
+ * Hard-coding it means the value in `User-Agent` and in `plugin-load.log` can silently
36
+ * disagree with the tarball that is actually installed — which is exactly how a rolled-back
37
+ * 1.0.1 install kept looking like 1.2.0 during debugging.
38
+ */
39
+ let cachedVersion
40
+ function version() {
41
+ if (cachedVersion) return cachedVersion
42
+ try {
43
+ const pkg = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'))
44
+ cachedVersion = String(pkg.version || '0.0.0-unknown')
45
+ } catch {
46
+ cachedVersion = '0.0.0-unknown'
47
+ }
48
+ return cachedVersion
49
+ }
50
+
51
+ const UA = `dsh-limit-break/${version()} (+local)`
32
52
  const MAX_TEXT = 20000
33
53
  const MAX_FACTS_RETURNED = 12
34
54
  const MAX_BODY_BYTES = 2 * 1024 * 1024
@@ -55,6 +75,32 @@ function resolveStoreDir(config) {
55
75
  return configured ? configured : join(resolveHome(), 'memory')
56
76
  }
57
77
 
78
+ /**
79
+ * Count live mounts of this plugin per store, in this process.
80
+ *
81
+ * The Plugins page can switch a plugin off, but a profile patch may still contain a second
82
+ * `insert` row for the same id — the loader happily mounts both, and then *neither* switch
83
+ * appears to work. That failure is invisible from the outside, so record it here: two live
84
+ * mounts pointing at one store is only ever a duplicate activation.
85
+ */
86
+ const LIVE_MOUNTS = Symbol.for('dsh-limit-break.live-mounts')
87
+
88
+ function registerMount(storeDir) {
89
+ const box = globalThis
90
+ if (!(box[LIVE_MOUNTS] instanceof Map)) box[LIVE_MOUNTS] = new Map()
91
+ const map = box[LIVE_MOUNTS]
92
+ const count = (map.get(storeDir) || 0) + 1
93
+ map.set(storeDir, count)
94
+ return {
95
+ count,
96
+ release() {
97
+ const left = (map.get(storeDir) || 1) - 1
98
+ if (left <= 0) map.delete(storeDir)
99
+ else map.set(storeDir, left)
100
+ }
101
+ }
102
+ }
103
+
58
104
  /**
59
105
  * Append one JSON line per load attempt to <store>/plugin-load.log.
60
106
  *
@@ -79,7 +125,7 @@ function diag(config, stage, extra) {
79
125
  } catch {
80
126
  /* first write: no file yet */
81
127
  }
82
- const line = JSON.stringify({ at: new Date().toISOString(), pid: process.pid, stage, ...extra })
128
+ const line = JSON.stringify({ at: new Date().toISOString(), pid: process.pid, version: version(), stage, ...extra })
83
129
  appendFileSync(logFile, `${line}\n`)
84
130
  } catch {
85
131
  /* diagnostics must never break the host */
@@ -175,6 +221,246 @@ function shapeFact(fact) {
175
221
  }
176
222
  }
177
223
 
224
+ /* ------------------------------------------------------------------ prune */
225
+
226
+ /**
227
+ * Redundancy is the slow failure mode of a memory store: two facts that restate one
228
+ * measurement, a fact that is really session state, a stale claim superseded by a better
229
+ * one. They still answer recall fine — until they disagree with each other.
230
+ *
231
+ * The rules below are deliberately deterministic: a plan you can read, never a guess. Only
232
+ * two of them are safe to act on without a human, and even then nothing is deleted — the
233
+ * facts move to `facts.archive.json` and the store is backed up first.
234
+ */
235
+ const SESSION_STATE = /(in this session|this conversation|current session|the approval policy (?:changed|is|was)\b)/i
236
+ const ANTONYM_PAIRS = [
237
+ ['allowed', 'denied'],
238
+ ['succeeds', 'fails'],
239
+ ['succeeded', 'failed'],
240
+ ['worked', 'failed'],
241
+ ['passes', 'fails']
242
+ ]
243
+ const STOPWORDS = new Set([
244
+ 'that', 'this', 'these', 'those', 'their', 'there', 'which', 'while', 'where', 'when', 'with',
245
+ 'without', 'about', 'after', 'before', 'would', 'could', 'should', 'being', 'under', 'over',
246
+ 'from', 'into', 'than', 'then', 'also', 'been', 'have', 'having', 'will', 'were', 'what'
247
+ ])
248
+
249
+ function normalizeStatement(text) {
250
+ return String(text || '')
251
+ .toLowerCase()
252
+ .replace(/[\s\u3000]+/g, ' ')
253
+ .replace(/[.,;:!?'"`()[\]{}]+/g, '')
254
+ .trim()
255
+ }
256
+
257
+ /** Years and other 4-digit calendar noise are not measurements. */
258
+ function isCalendarNoise(token) {
259
+ return /^\d{4}$/.test(token) && Number(token) >= 1990 && Number(token) <= 2100
260
+ }
261
+
262
+ function numbersIn(fact) {
263
+ const hay = [fact.statement, fact.value, fact.details ? JSON.stringify(fact.details) : ''].join(' ')
264
+ const found = new Set()
265
+ for (const match of String(hay).matchAll(/-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/g)) {
266
+ const canonical = match[0].replace(/^[-+]/, '')
267
+ if (isCalendarNoise(canonical)) continue
268
+ if (canonical.replace(/[^0-9]/g, '').length >= 3) found.add(canonical)
269
+ }
270
+ return found
271
+ }
272
+
273
+ function keywordsIn(fact) {
274
+ const words = String(fact.statement || '')
275
+ .toLowerCase()
276
+ .split(/[^\p{L}\p{N}_]+/u)
277
+ .filter((word) => word.length >= 4 && !STOPWORDS.has(word) && !/^\d{4}$/.test(word))
278
+ return new Set(words)
279
+ }
280
+
281
+ /** Ordered by how much a fact carries, so a merge keeps the informative one. */
282
+ function richness(fact) { let score = 0
283
+ if (fact.value !== undefined && fact.value !== null) score += 2
284
+ if (fact.details) score += 2
285
+ if (fact.source) score += 1
286
+ if (Array.isArray(fact.tags) && fact.tags.length) score += 1
287
+ score += String(fact.statement || '').length / 1000
288
+ if (fact.timestamp) score += (Date.parse(fact.timestamp) || 0) / 1e15
289
+ return score
290
+ }
291
+
292
+ /**
293
+ * A contradiction must share a *subject*, not just a topic tag: two facts about the network
294
+ * that both mention "host" are not enemies. This only fires on a real antonym pair, either
295
+ * from the list above or from the general un-prefix shape (reachable / unreachable).
296
+ */
297
+ function antonymConflict(a, b) {
298
+ const ta = String(a.statement || '').toLowerCase()
299
+ const tb = String(b.statement || '').toLowerCase()
300
+ for (const [x, y] of ANTONYM_PAIRS) {
301
+ const rx = new RegExp(`\\b${x}\\b`)
302
+ const ry = new RegExp(`\\b${y}\\b`)
303
+ if (rx.test(ta) && ry.test(tb)) return [x, y]
304
+ if (ry.test(ta) && rx.test(tb)) return [y, x]
305
+ }
306
+ for (const [left, right] of [[ta, tb], [tb, ta]]) {
307
+ for (const raw of left.match(/\bun(\p{L}{4,})\b/gu) || []) {
308
+ const stem = raw.slice(2)
309
+ if (new RegExp(`\\b${stem}\\b`).test(right)) return [stem, raw]
310
+ }
311
+ }
312
+ return null
313
+ }
314
+
315
+ function planPrune(facts) {
316
+ const living = facts.filter(Boolean)
317
+ const candidates = []
318
+ const warnings = []
319
+ const flagged = new Set()
320
+
321
+ const flag = (id, entry) => {
322
+ if (!id || flagged.has(id)) return
323
+ flagged.add(id)
324
+ candidates.push(entry)
325
+ }
326
+
327
+ for (const fact of living) {
328
+ if (SESSION_STATE.test(String(fact.statement || ''))) {
329
+ flag(fact.id, {
330
+ id: fact.id,
331
+ rule: 'session-state',
332
+ statement: fact.statement,
333
+ reason: 'reads as conversational/session state, which a durable store must not hold'
334
+ })
335
+ }
336
+ }
337
+
338
+ const byStatement = new Map()
339
+ for (const fact of living) {
340
+ const key = normalizeStatement(fact.statement)
341
+ if (!key) continue
342
+ if (!byStatement.has(key)) byStatement.set(key, [])
343
+ byStatement.get(key).push(fact)
344
+ }
345
+ for (const group of byStatement.values()) {
346
+ if (group.length < 2) continue
347
+ const [keep, ...rest] = [...group].sort((a, b) => richness(b) - richness(a))
348
+ for (const duplicate of rest) {
349
+ flag(duplicate.id, {
350
+ id: duplicate.id,
351
+ rule: 'exact-duplicate',
352
+ keep: keep.id,
353
+ statement: duplicate.statement,
354
+ reason: `statement is identical to ${keep.id}`
355
+ })
356
+ }
357
+ }
358
+
359
+ for (let i = 0; i < living.length; i += 1) {
360
+ for (let j = i + 1; j < living.length; j += 1) {
361
+ const a = living[i]
362
+ const b = living[j]
363
+ if (a.id === b.id || flagged.has(a.id) || flagged.has(b.id)) continue
364
+ const shared = [...numbersIn(a)].filter((n) => numbersIn(b).has(n))
365
+ const overlap = [...keywordsIn(a)].filter((k) => keywordsIn(b).has(k))
366
+ // Two facts citing the same tiny number can be unrelated (a year, a version); require
367
+ // either several numbers or one number plus overlapping prose.
368
+ if (shared.length >= 2 || (shared.length >= 1 && overlap.length >= 2)) {
369
+ warnings.push({
370
+ rule: 'shared-numbers',
371
+ ids: [a.id, b.id],
372
+ shared,
373
+ reason: 'both facts cite the same measurements: one is probably a subset of the other'
374
+ })
375
+ }
376
+ const sameTopic = (a.topic || '') && a.topic === b.topic
377
+ if (sameTopic && overlap.length >= 1) {
378
+ const conflict = antonymConflict(a, b)
379
+ if (conflict) {
380
+ warnings.push({
381
+ rule: 'contradiction',
382
+ ids: [a.id, b.id],
383
+ terms: conflict,
384
+ shared: overlap.slice(0, 4),
385
+ reason: 'same topic, opposite claims: one of them is stale by definition'
386
+ })
387
+ }
388
+ }
389
+ }
390
+ }
391
+ return { candidates, warnings }
392
+ }
393
+
394
+ /** Dry run by default. Nothing leaves the store without a backup and an archive copy. */
395
+ async function pruneStore(dir, options) {
396
+ const apply = Boolean(options && options.apply)
397
+ const store = await loadStore(dir)
398
+ const archiveFile = join(dir, 'facts.archive.json')
399
+ if (!store.existed) {
400
+ return { ok: false, code: 'memory.empty', store: store.file, total: 0, archived: 0, candidates: [], warnings: [] }
401
+ }
402
+ const living = store.facts.filter(Boolean)
403
+ const plan = planPrune(living)
404
+ const total = living.length
405
+ const kept = total - plan.candidates.length
406
+
407
+ if (!apply || plan.candidates.length === 0) {
408
+ return {
409
+ ok: true,
410
+ mode: apply ? 'applied' : 'plan',
411
+ store: store.file,
412
+ archiveFile,
413
+ total,
414
+ kept,
415
+ archived: plan.candidates.length,
416
+ candidates: plan.candidates,
417
+ warnings: plan.warnings,
418
+ changed: false
419
+ }
420
+ }
421
+
422
+ const entries = []
423
+ try {
424
+ const existing = JSON.parse(await readFile(archiveFile, 'utf8'))
425
+ if (Array.isArray(existing)) entries.push(...existing)
426
+ } catch {
427
+ entries.length = 0
428
+ }
429
+ const byId = new Map(living.map((fact) => [fact.id, fact]))
430
+ const archivedAt = new Date().toISOString()
431
+ for (const candidate of plan.candidates) {
432
+ entries.push({ fact: byId.get(candidate.id), archivedAt, rule: candidate.rule, reason: candidate.reason })
433
+ }
434
+
435
+ const backupFile = `${store.file}.bak-${archivedAt.replace(/[:.]/g, '-')}`
436
+ await writeFile(backupFile, await readFile(store.file, 'utf8'), 'utf8')
437
+ await writeJsonAtomic(archiveFile, entries)
438
+ const dropped = new Set(plan.candidates.map((candidate) => candidate.id))
439
+ store.facts = living.filter((fact) => !dropped.has(fact.id))
440
+ await saveStore(dir, store)
441
+
442
+ return {
443
+ ok: true,
444
+ mode: 'applied',
445
+ store: store.file,
446
+ archiveFile,
447
+ backupFile,
448
+ total,
449
+ kept: store.facts.length,
450
+ archived: plan.candidates.length,
451
+ candidates: plan.candidates,
452
+ warnings: plan.warnings,
453
+ changed: true
454
+ }
455
+ }
456
+
457
+ async function writeJsonAtomic(file, value) {
458
+ await mkdir(dirname(file), { recursive: true })
459
+ const tmp = `${file}.${process.pid}.tmp`
460
+ await writeFile(tmp, JSON.stringify(value, null, 2), 'utf8')
461
+ await rename(tmp, file)
462
+ }
463
+
178
464
  /* ------------------------------------------------------------------ fetch */
179
465
 
180
466
  /**
@@ -299,9 +585,21 @@ export function apply(ctx, config) {
299
585
  diag(config, 'no-tools-service', { ctxKeys: ctx ? Object.keys(ctx).slice(0, 24) : [] })
300
586
  return
301
587
  }
302
- diag(config, 'mount', { storeDir: resolveStoreDir(config) })
303
-
304
588
  const storeDir = resolveStoreDir(config)
589
+ const mount = registerMount(storeDir)
590
+ diag(config, 'mount', {
591
+ storeDir,
592
+ moduleDir: dirname(fileURLToPath(import.meta.url)),
593
+ liveMounts: mount.count
594
+ })
595
+ if (mount.count > 1) {
596
+ diag(config, 'duplicate-apply', {
597
+ storeDir,
598
+ liveMounts: mount.count,
599
+ hint: 'the same plugin is mounted more than once (e.g. a bundle plus a manual insert row); switches will look broken'
600
+ })
601
+ }
602
+
305
603
  const timeoutMs = Number(config && config.requestTimeoutMs) || 12000
306
604
  const shellFallback = !(config && config.shellFallback === false)
307
605
 
@@ -310,6 +608,8 @@ export function apply(ctx, config) {
310
608
  return fn()
311
609
  }
312
610
 
611
+ effect(() => () => mount.release(), 'limit-break:lifetime')
612
+
313
613
  /* ---- memory_recall ------------------------------------------------ */
314
614
  effect(
315
615
  () =>
@@ -570,8 +870,54 @@ export function apply(ctx, config) {
570
870
  'limit-break: memory_stats tool'
571
871
  )
572
872
 
573
- diag(config, 'mounted', { tools: ['memory_recall', 'memory_save', 'resilient_fetch', 'memory_stats'] })
873
+ /* ---- memory_prune ------------------------------------------------- */
874
+ effect(
875
+ () =>
876
+ ctx.tools.register({
877
+ name: 'memory_prune',
878
+ description:
879
+ 'Find redundant facts in the durable memory store and archive them. Never deletes: the plan is ' +
880
+ 'returned first (apply defaults to false), and applying writes facts.archive.json plus a ' +
881
+ 'facts.json.bak-<timestamp> backup. Candidates are deterministic (session state, exact duplicates); ' +
882
+ 'softer suspicions (shared measurements, contradictory claims) come back as warnings only.',
883
+ parameters: {
884
+ type: 'object',
885
+ properties: {
886
+ apply: {
887
+ type: 'boolean',
888
+ description: 'Archive the safe candidates. Defaults to false: report the plan and change nothing.'
889
+ }
890
+ },
891
+ additionalProperties: false
892
+ },
893
+ output: {
894
+ schema: { type: 'object' },
895
+ render: (_args, value) => {
896
+ if (!value || !value.ok) return [{ type: 'text', text: 'Memory store is empty or unreadable.' }]
897
+ const head = value.mode === 'applied'
898
+ ? `Archived ${value.archived} of ${value.total} facts -> ${value.archiveFile}`
899
+ : `Plan: ${value.archived} of ${value.total} facts are safe to archive (nothing changed yet)`
900
+ const lines = value.candidates.map((c) => `- ${c.id} [${c.rule}]${c.keep ? ` keep ${c.keep}` : ''}`)
901
+ const warns = value.warnings.map((w) => `- ~ ${w.ids.join(' vs ')} [${w.rule}]`)
902
+ return [{ type: 'text', text: [head, ...lines, ...warns].join('\n') }]
903
+ }
904
+ },
905
+ presentCall: (args) => ({
906
+ card: 'generic',
907
+ title: 'Memory prune',
908
+ kind: args && args.apply ? 'write' : 'read'
909
+ }),
910
+ async execute(args) {
911
+ return pruneStore(storeDir, { apply: Boolean(args && args.apply) })
912
+ }
913
+ }),
914
+ 'limit-break: memory_prune tool'
915
+ )
916
+
917
+ diag(config, 'mounted', {
918
+ tools: ['memory_recall', 'memory_save', 'resilient_fetch', 'memory_stats', 'memory_prune']
919
+ })
574
920
  }
575
921
 
576
922
  export default { name, inject, apply }
577
- export { resolveStoreDir, loadStore, saveStore, alternatesFor, clip }
923
+ export { resolveStoreDir, loadStore, saveStore, alternatesFor, clip, planPrune, pruneStore }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-limit-break",
3
- "version": "1.2.0",
4
- "description": "Durable cross-session memory and a fetch ladder that survives hostname-keyed blocking — four small tools for DeepSeek Harness.",
3
+ "version": "1.3.0",
4
+ "description": "Durable cross-session memory, a fetch ladder that survives hostname-keyed blocking, and a redundant-fact pruner — five small tools for DeepSeek Harness.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
7
7
  "exports": {
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Self-test for memory_prune. Run: node tools/prune-selftest.mjs
3
+ *
4
+ * It builds a store with one of each redundancy shape, checks that the dry run changes
5
+ * nothing, applies, and checks that a second run is a no-op. Exit code 1 on any mismatch.
6
+ */
7
+ import assert from 'node:assert/strict'
8
+ import { mkdtemp, readFile, writeFile, readdir } from 'node:fs/promises'
9
+ import { createHash } from 'node:crypto'
10
+ import { tmpdir } from 'node:os'
11
+ import { join } from 'node:path'
12
+ import { pruneStore } from '../lib/index.js'
13
+
14
+ const dir = await mkdtemp(join(tmpdir(), 'limit-break-prune-'))
15
+ const facts = [
16
+ { id: 'bench.latency_a', statement: 'Wikipedia via SNI api.wikimedia.org answers in 0.43 s', value: 0.43, unit: 's', topic: 'network' },
17
+ { id: 'bench.latency_b', statement: 'Wikipedia via SNI api.wikimedia.org answered in 0.43 s', value: 0.43, unit: 's', topic: 'network' },
18
+ { id: 'env.approval', statement: 'The approval policy changed in this session to never', topic: 'env' },
19
+ { id: 'dup.a', statement: 'GIL means threads do not help CPU-bound Python', tags: ['python'] },
20
+ { id: 'dup.b', statement: 'GIL means threads do not help CPU-bound Python', value: 1, details: { who: 'dynamodb' }, source: 'bench' },
21
+ { id: 'net.pos', statement: 'Wikipedia is reachable from this host via SNI api.wikimedia.org', topic: 'network' },
22
+ { id: 'net.neg', statement: 'Wikipedia is unreachable from this host', topic: 'network' },
23
+ { id: 'stable', statement: 'crypto dyn32_b pow-32 measures 0.051 ms/sign', value: 0.051, unit: 'ms/sign', topic: 'crypto' }
24
+ ]
25
+
26
+ await writeFile(
27
+ join(dir, 'facts.json'),
28
+ JSON.stringify({ schema_version: 1, kind: 'agent-memory-store', facts }, null, 2),
29
+ 'utf8'
30
+ )
31
+ const storeFile = join(dir, 'facts.json')
32
+ const hash = async () => createHash('sha256').update(await readFile(storeFile)).digest('hex')
33
+
34
+ const before = await hash()
35
+ const plan = await pruneStore(dir, {})
36
+ assert.equal(plan.ok, true)
37
+ assert.equal(plan.mode, 'plan')
38
+ assert.equal(plan.changed, false)
39
+ assert.deepEqual(plan.candidates.map((c) => c.id).sort(), ['dup.a', 'env.approval'])
40
+ assert.equal(plan.warnings.length >= 2, true)
41
+ assert.equal(plan.warnings.some((w) => w.rule === 'shared-numbers'), true)
42
+ assert.equal(plan.warnings.some((w) => w.rule === 'contradiction'), true)
43
+ assert.equal(await hash(), before, 'dry run must not touch facts.json')
44
+
45
+ const applied = await pruneStore(dir, { apply: true })
46
+ assert.equal(applied.mode, 'applied')
47
+ assert.equal(applied.changed, true)
48
+ assert.equal(applied.archived, 2)
49
+ assert.equal(applied.kept, 6)
50
+ assert.equal(applied.backupFile.includes('facts.json.bak-'), true)
51
+
52
+ const kept = JSON.parse(await readFile(storeFile, 'utf8'))
53
+ assert.equal(kept.facts.length, 6)
54
+ assert.equal(kept.facts.some((f) => f.id === 'env.approval' || f.id === 'dup.a'), false)
55
+ assert.equal(kept.facts.some((f) => f.id === 'dup.b'), true, 'the richer duplicate must survive')
56
+
57
+ const archive = JSON.parse(await readFile(applied.archiveFile, 'utf8'))
58
+ assert.equal(archive.length, 2)
59
+ assert.equal(archive.every((e) => e.archivedAt && e.rule && e.fact), true)
60
+
61
+ const again = await pruneStore(dir, { apply: true })
62
+ assert.equal(again.archived, 0)
63
+ assert.equal(again.changed, false)
64
+ const archiveAgain = JSON.parse(await readFile(applied.archiveFile, 'utf8'))
65
+ assert.equal(archiveAgain.length, 2, 'second run must not grow the archive')
66
+
67
+ const leftovers = (await readdir(dir)).filter((name) => name.endsWith('.tmp'))
68
+ assert.deepEqual(leftovers, [], 'no temp files left behind')
69
+
70
+ console.log('prune selftest: ok')