dsh-limit-break 1.2.1 → 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,22 @@ 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
+
8
24
  ## 1.2.1 — 2026-10-21
9
25
 
10
26
  ### 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` 的
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
@@ -221,6 +221,246 @@ function shapeFact(fact) {
221
221
  }
222
222
  }
223
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
+
224
464
  /* ------------------------------------------------------------------ fetch */
225
465
 
226
466
  /**
@@ -630,8 +870,54 @@ export function apply(ctx, config) {
630
870
  'limit-break: memory_stats tool'
631
871
  )
632
872
 
633
- 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
+ })
634
920
  }
635
921
 
636
922
  export default { name, inject, apply }
637
- 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.1",
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')