@twinsearth/ai-prison-dsh-plugin 1.4.5 → 1.4.7

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
@@ -2,6 +2,52 @@
2
2
 
3
3
  All notable changes to `@twinsearth/ai-prison-dsh-plugin` are recorded here.
4
4
 
5
+ ## [1.4.7] — 2026-11-18
6
+
7
+ ### Added — panel/remote operations roll back while paused or frozen
8
+
9
+ - The 5 GB freeze (and the 3.9 GB pause without a confirmation) now also covers
10
+ operations started from the panel or the web UI, not just the agent tool:
11
+ after an additive operation settles, the watchdog immediately disables that
12
+ bundle and records a `freeze-rollback` / `pause-rollback` incident. Release
13
+ operations (disable/remove) and a confirmed pause grace window stay allowed.
14
+ - The panel's "Measure plugin budgets now" button is disabled while frozen,
15
+ because each probe spawns a child process.
16
+
17
+ ### Fixed — tamper monitor no longer flags the manager's own writes
18
+
19
+ - The operation-window suppression was ±5 s around settle, but pnpm may write
20
+ package.json / pnpm-lock.yaml later than that, so installs of 1.4.5/1.4.6 were
21
+ repeatedly "restored" by the tamper monitor and some installs failed.
22
+ - Any drift within 120 s after the last plugin operation is now treated as a
23
+ manager write; the mtime window still applies to older operations. Real
24
+ out-of-window edits (manual scripts) are still detected and restored.
25
+
26
+ ### Tests
27
+ - New `test/watchdog-freeze.test.mjs`: freeze rollback, pause rollback with and
28
+ without grace, and release-class operations never rolled back.
29
+
30
+ ## [1.4.6] — 2026-11-18
31
+ ## [1.4.6] — 2026-11-18
32
+
33
+ ### Added — structured reason / impact / steps / release text
34
+
35
+ - New `lib/remediation.js` explains every enforcement action in four fixed
36
+ sections: why it happened, what it affects, what to do (numbered steps) and how
37
+ the plugin leaves the prison.
38
+ - Freeze, pause and every quarantine reason (hot-ops, crash, budget, tamper) now
39
+ carry a `remediation` object in the incident, the quarantine entry and the
40
+ crash/tamper plan.
41
+ - The panel "Plugin prison" renders it under each jailed plugin (title, why,
42
+ impact, numbered steps, release path); the agent tool returns the same object.
43
+ - Tests cover every remediation reason and the quarantine payload.
44
+
45
+ ### Notes
46
+ - This completes the three follow-ups requested for the probe work: the probe
47
+ publish fix and the full background probe queue shipped in 1.4.3/1.4.4; this
48
+ release adds the operator-facing explanation.
49
+
50
+ ## [1.4.5] — 2026-11-18
5
51
  ## [1.4.5] — 2026-11-18
6
52
 
7
53
  ### Fixed — tamper monitor false positives (important)
package/client.js CHANGED
@@ -443,9 +443,18 @@ window.__ModuleLoader__.load({
443
443
  h('div', { style: { ...C.key, marginBottom: 4 } }, t('watchdog.quarantine')),
444
444
  quarantine.length === 0
445
445
  ? h('p', { style: { ...C.muted, fontSize: 12, margin: '0 0 10px' } }, t('watchdog.noQuarantine'))
446
- : h('div', { style: { display: 'flex', flexDirection: 'column', gap: 6, marginBottom: 10 } },
447
- quarantine.map((entry) => h('div', { key: entry.target, style: { display: 'flex', alignItems: 'center', gap: 8, flexWrap: 'wrap' } },
448
- h('span', { style: { ...C.mono, fontSize: 12, wordBreak: 'break-all' } }, entry.target + ' · ' + entry.reason + ' · ' + entry.level),
446
+ : h('div', { style: { display: 'flex', flexDirection: 'column', gap: 8, marginBottom: 10 } },
447
+ quarantine.map((entry) => h('div', { key: entry.target, style: { display: 'flex', alignItems: 'flex-start', gap: 8, flexWrap: 'wrap' } },
448
+ h('div', { style: { display: 'flex', flexDirection: 'column', gap: 2, flex: '1 1 260px', minWidth: 0 } },
449
+ h('span', { style: { ...C.mono, fontSize: 12, wordBreak: 'break-all' } }, entry.target + ' · ' + entry.reason + ' · ' + entry.level),
450
+ entry.remediation ? h('div', { style: { fontSize: 11.5, color: 'var(--dsw-alias-label-secondary, #61666b)', lineHeight: 1.5 } },
451
+ h('div', { style: { fontWeight: 600 } }, entry.remediation.title),
452
+ h('div', null, entry.remediation.why),
453
+ h('div', null, entry.remediation.impact),
454
+ entry.remediation.steps.map((step, index) => h('div', { key: index }, (index + 1) + '. ' + step)),
455
+ h('div', null, '解除:' + entry.remediation.release),
456
+ ) : null,
457
+ ),
449
458
  h('button', { style: button, onClick: () => act('release', entry.target, '假释 ' + entry.target + ' ?') }, t('watchdog.release')),
450
459
  ))),
451
460
  topFive.length === 0 ? null : h('div', null,
@@ -466,7 +475,7 @@ window.__ModuleLoader__.load({
466
475
  watchdog.baseline ? h('p', { style: { ...C.muted, fontSize: 11.5, margin: '10px 0 0' } }, t('watchdog.baseline', { at: watchdog.baseline.at })) : null,
467
476
  watchdog.tamper ? h('p', { style: { ...C.muted, fontSize: 11.5, margin: '6px 0 0' } }, t('watchdog.tamper', { n: (watchdog.tamper.files || []).length, at: watchdog.tamper.lastCheckAt || '—' })) : null,
468
477
  h('div', { style: { marginTop: 10, display: 'flex', gap: 8, alignItems: 'center', flexWrap: 'wrap' } },
469
- h('button', { style: { ...button, padding: '5px 10px' }, onClick: () => act('probe') }, t('watchdog.measure')),
478
+ h('button', { style: { ...button, padding: '5px 10px', opacity: frozen ? 0.5 : 1 }, disabled: frozen, title: frozen ? t('watchdog.measureBlocked') : undefined, onClick: () => { if (!frozen) act('probe') } }, t('watchdog.measure')),
470
479
  watchdog.probeProgress ? h('span', { style: { ...C.mono, fontSize: 11.5 } }, `${watchdog.probeProgress.done || 0}/${watchdog.probeProgress.total || 0}`) : null,
471
480
  ),
472
481
  )
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Human-readable remediation for every AI Prison enforcement action.
3
+ *
4
+ * Each entry answers four questions in fixed order so the panel and the agent
5
+ * say the same thing: why it happened, what it affects, what to do, and how the
6
+ * plugin gets out of prison.
7
+ */
8
+ const REMEDIATION = {
9
+ 'hot-ops': {
10
+ title: '插件热操作过频',
11
+ why: '同一插件在本次运行中被反复安装、启用或热切换(超过 5 次),插件图被反复重组,是内存暴涨与崩溃的高风险来源。',
12
+ impact: '该插件已被自动关闭并隔离;本次运行内不再加载。',
13
+ steps: [
14
+ '确认你确实需要反复开关它;安装/升级完成一次即可,不要连续点击。',
15
+ '重启 DSH(或在本面板点"假释")后重新启用一次。',
16
+ '如果它每次启动都反复启用自己,向插件作者反馈"启动期重复 enable"。',
17
+ ],
18
+ release: '重启 DSH 会自动释放;也可以在面板"插件监狱"里点"假释"手动释放。',
19
+ },
20
+ crash: {
21
+ title: '该插件被归因为崩溃来源',
22
+ why: 'DSH 宿主进程崩溃(如爆内存),崩溃日志里的 hostRunId 与本插件最后一次安装/启用记录匹配。',
23
+ impact: '该插件已被隔离并关闭,隔离在重启后仍生效,避免崩溃循环。',
24
+ steps: [
25
+ '先重启 DSH,确认在没有该插件时环境稳定。',
26
+ '检查插件版本是否有修复(升级到最新版),或查看其 README 的已知崩溃问题。',
27
+ '确需继续使用时,在面板"插件监狱"点"假释",一次只放行一个插件并观察内存。',
28
+ ],
29
+ release: '仅手动释放(面板"假释");重启不会自动解除,防止崩溃循环。',
30
+ },
31
+ budget: {
32
+ title: '启动内存超过硬阈值',
33
+ why: '隔离探针实测该插件模块加载的 retained 内存超过 500 MB 硬阈值(或该项目历史上留下超限记录)。',
34
+ impact: '该插件已被关闭并隔离,且写入 pending-boot.json,下次启动前会被外部 guard 直接剔除。',
35
+ steps: [
36
+ '升级到该插件的修复版本;若仍超限,联系作者或换用轻量替代。',
37
+ '如果确认是误报(插件版本已变化),在面板"插件监狱"点"假释"后重新测量预算。',
38
+ '临时需要使用时,只放行一次并在面板观察内存 Top5。',
39
+ ],
40
+ release: '手动释放(面板"假释");释放后再测一次预算确认。',
41
+ },
42
+ tamper: {
43
+ title: '关键配置文件被非插件管理器修改',
44
+ why: 'profile 的 package.json / cordis.patch.yml / pnpm-workspace.yaml / pnpm-lock.yaml 在插件管理器操作窗口之外发生了变化(被修改或删除)。',
45
+ impact: '已把被改动内容备份到 .ai-prison-watchdog/tampered/,默认恢复原文件;归因到的最后安装/启用插件已被隔离。',
46
+ steps: [
47
+ '检查 .ai-prison-watchdog/tampered/ 下的备份,确认改动来自哪里(手工编辑、脚本、其他工具)。',
48
+ '不要在 DSH 运行中手工改 profile 配置;确需修改时先退出 DSH。',
49
+ '确认无插件行为异常后,在面板"插件监狱"点"假释"恢复该插件。',
50
+ ],
51
+ release: '手动释放(面板"假释");恢复前先核对 tampered/ 备份。',
52
+ },
53
+ freeze: {
54
+ title: '内存水位达到冻结线',
55
+ why: '宿主堆内存超过 5 GB 冻结阈值(6 GB host;4 GB host 按比例折算),继续加载/重组插件会直接导致 OOM 崩溃。',
56
+ impact: '所有增内存的插件操作被禁止(安装、启用、热切换、重载、卸载等),Agent 的 plugin_manager 也已被拒绝。',
57
+ steps: [
58
+ '在面板"内存看护 → Top5"里立即关闭占用最大的插件。',
59
+ '关闭后等待 1 分钟让堆回落,或直接重启 DSH。',
60
+ '排查是谁在持续吃内存:优先怀疑最近开启/升级的插件。',
61
+ ],
62
+ release: '堆回落到冻结线以下并持续 60 秒会自动解除;或重启 DSH。',
63
+ },
64
+ pause: {
65
+ title: '内存水位达到暂停线,需要二次确认',
66
+ why: '宿主堆内存超过 3.9 GB 暂停阈值,插件操作已暂停,防止继续推高水位。',
67
+ impact: '所有增内存的插件操作被临时拦截;本次操作需要你明确确认。',
68
+ steps: [
69
+ '先在"内存看护 → Top5"里关闭一个不需要的插件。',
70
+ '确认要继续时,点面板的"我已知晓:允许插件操作 10 分钟"。',
71
+ '宽限期内若水位继续上涨到 5 GB,会自动升级为冻结。',
72
+ ],
73
+ release: '点"二次确认"给 10 分钟宽限;堆回落到 3.6 GB 以下会自动解除。',
74
+ },
75
+ }
76
+
77
+ /** Look up the remediation for one enforcement reason. */
78
+ export function remediationFor(reason, extra = {}) {
79
+ const code = REMEDIATION[reason] ? reason : 'hot-ops'
80
+ const base = REMEDIATION[code]
81
+ return {
82
+ code,
83
+ title: base.title,
84
+ why: base.why,
85
+ impact: base.impact,
86
+ steps: [...base.steps],
87
+ release: base.release,
88
+ ...(extra.count !== undefined ? { detail: `本次运行计数 ${extra.count}` } : {}),
89
+ }
90
+ }
91
+
92
+ /** Reasons this module knows about (used by tests and the panel). */
93
+ export const REMEDIATION_REASONS = Object.keys(REMEDIATION)
@@ -6,6 +6,7 @@ import crypto from 'node:crypto'
6
6
  import { watchdogState, MAX_SAMPLES, MAX_INCIDENTS } from './watchdog-state.js'
7
7
  import { probeBudget } from './budget-probe.js'
8
8
  import { createTamperMonitor } from './tamper-monitor.js'
9
+ import { remediationFor } from './remediation.js'
9
10
  import {
10
11
  attributeCrash,
11
12
  classifyWaterline,
@@ -137,6 +138,7 @@ export function createWatchdog(ctx, config = {}, logger = {}) {
137
138
 
138
139
  let lastFlush = 0
139
140
  let operationWindowUntil = 0
141
+ let lastOperationAt = 0
140
142
  const operationWindows = []
141
143
  let tamperMigrationDone = false
142
144
  let tamper = null
@@ -156,7 +158,7 @@ export function createWatchdog(ctx, config = {}, logger = {}) {
156
158
  policy: config.tamperPolicy === 'alert' ? 'alert' : 'restore',
157
159
  graceMs: Number(config.tamperGraceMs) >= 0 ? Number(config.tamperGraceMs) : 15_000,
158
160
  intervalMs: Number(config.tamperIntervalMs) > 0 ? Number(config.tamperIntervalMs) : 30_000,
159
- shouldIgnore: () => Date.now() < operationWindowUntil,
161
+ shouldIgnore: () => Date.now() < operationWindowUntil || Date.now() - lastOperationAt < 120000,
160
162
  isWithinOperationWindow: (mtimeMs) => {
161
163
  const now = Date.now()
162
164
  return operationWindows.some((window) => mtimeMs >= window.startedAt - 5000 && mtimeMs <= (window.settledAt || now) + 5000)
@@ -194,11 +196,11 @@ export function createWatchdog(ctx, config = {}, logger = {}) {
194
196
  target = null
195
197
  }
196
198
  const applied = Boolean(watchdogState.enforce && target)
197
- const plan = { at: new Date().toISOString(), level: 'L1', crash: null, reason: 'tamper', target, inFlight: false, evidence: { files: result.diffs, candidates: evidence }, applied }
199
+ const plan = { at: new Date().toISOString(), level: 'L1', crash: null, reason: 'tamper', remediation: remediationFor('tamper'), target, inFlight: false, evidence: { files: result.diffs, candidates: evidence }, applied }
198
200
  watchdogState.plans.push(plan)
199
201
  if (watchdogState.plans.length > 20) watchdogState.plans = watchdogState.plans.slice(-20)
200
202
  if (applied) {
201
- watchdogState.quarantine.set(target, { reason: 'tamper', level: 'L1', at: plan.at, release: 'manual', evidence: '配置漂移:' + files.join(', ') })
203
+ watchdogState.quarantine.set(target, { reason: 'tamper', level: 'L1', at: plan.at, release: 'manual', evidence: '配置漂移:' + files.join(', '), remediation: remediationFor('tamper') })
202
204
  void disableTarget(target, 'tamper')
203
205
  writePendingBoot({ level: 'L1', reason: 'tamper', disable: [target], keep: [] })
204
206
  appendIncident({ kind: 'tamper-enforced', target, files, detail: '配置漂移后已隔离 ' + target })
@@ -310,6 +312,7 @@ export function createWatchdog(ctx, config = {}, logger = {}) {
310
312
  heapUsed,
311
313
  heapLimit,
312
314
  detail: `heap ${formatBytes(heapUsed)} ≥ block ${formatBytes(thresholds.totalBlock)}:冻结插件操作`,
315
+ remediation: remediationFor('freeze'),
313
316
  })
314
317
  log.warn(`[ai-prison-watchdog] ${incident.detail}`)
315
318
  persist(true)
@@ -321,6 +324,7 @@ export function createWatchdog(ctx, config = {}, logger = {}) {
321
324
  heapUsed,
322
325
  heapLimit,
323
326
  detail: `heap ${formatBytes(heapUsed)} ≥ pause ${formatBytes(thresholds.totalPause)}:插件操作需二次确认`,
327
+ remediation: remediationFor('pause'),
324
328
  })
325
329
  persist(true)
326
330
  } else if (waterline === 'warn' && previous !== 'warn' && previous === 'ok') {
@@ -393,6 +397,7 @@ export function createWatchdog(ctx, config = {}, logger = {}) {
393
397
  const entry = watchdogState.operations.get(target) || { count: 0, estimateMB: 0, lastAt: null, version: operation.version || null, lastApplication: null }
394
398
  if (operation.phase === 'started') {
395
399
  operationWindowUntil = Date.now() + 4000
400
+ lastOperationAt = Date.now()
396
401
  operationWindows.push({ startedAt: Date.now(), settledAt: null })
397
402
  if (operationWindows.length > 20) operationWindows.shift()
398
403
  inflight.set(operation.id, { target, heapBefore: watchdogState.latest ? watchdogState.latest.heapUsed : 0 })
@@ -402,6 +407,7 @@ export function createWatchdog(ctx, config = {}, logger = {}) {
402
407
  }
403
408
  if (operation.phase !== 'settled') return
404
409
  operationWindowUntil = Date.now() + 4000
410
+ lastOperationAt = Date.now()
405
411
  for (let i = operationWindows.length - 1; i >= 0; i -= 1) {
406
412
  if (operationWindows[i].settledAt === null) { operationWindows[i].settledAt = Date.now(); break }
407
413
  }
@@ -419,6 +425,24 @@ export function createWatchdog(ctx, config = {}, logger = {}) {
419
425
  entry.version = operation.version || entry.version
420
426
  entry.lastApplication = operation.application || null
421
427
  watchdogState.operations.set(target, entry)
428
+ const additive = operation.stage === 'install' || (operation.stage === 'enable' && operation.enabled !== false)
429
+ if (
430
+ watchdogState.enforce &&
431
+ additive &&
432
+ target &&
433
+ !EXEMPT_TARGETS.has(target) &&
434
+ (watchdogState.waterline === 'frost' || watchdogState.waterline === 'pause') &&
435
+ !(Number.isFinite(watchdogState.graceUntil) && Date.now() < watchdogState.graceUntil)
436
+ ) {
437
+ const level = watchdogState.waterline
438
+ appendIncident({
439
+ kind: level === 'frost' ? 'freeze-rollback' : 'pause-rollback',
440
+ target,
441
+ level,
442
+ detail: (level === 'frost' ? '冻结' : '暂停') + '期间检测到面板/网页发起的增内存操作,已立即回滚关闭 ' + target,
443
+ })
444
+ void disableTarget(target, level + '-rollback')
445
+ }
422
446
  if (operation.reason === 'remove' && operation.application === 'applied') {
423
447
  watchdogState.operations.delete(target)
424
448
  return
@@ -430,6 +454,7 @@ export function createWatchdog(ctx, config = {}, logger = {}) {
430
454
  at: new Date().toISOString(),
431
455
  release: 'restart',
432
456
  evidence: `本次运行内热操作计数 ${entry.count} > 5(安装/启用/热切换)`,
457
+ remediation: remediationFor('hot-ops', { count: entry.count }),
433
458
  })
434
459
  appendIncident({ kind: 'quarantine', target, level: 'L2', reason: 'hot-ops', detail: `热操作 ${entry.count} 次 > 5,自动关闭` })
435
460
  persist(true)
@@ -490,7 +515,7 @@ export function createWatchdog(ctx, config = {}, logger = {}) {
490
515
  const unique = [...new Set(targets)].filter((target) => target && !EXEMPT_TARGETS.has(target) && !watchdogState.quarantine.has(target))
491
516
  const disabled = []
492
517
  for (const target of unique) {
493
- watchdogState.quarantine.set(target, { reason, level: 'crash', at: new Date().toISOString(), release: 'manual', evidence: reason })
518
+ watchdogState.quarantine.set(target, { reason, level: 'crash', at: new Date().toISOString(), release: 'manual', evidence: reason, remediation: remediationFor(reason) })
494
519
  if (await disableTarget(target, reason)) disabled.push(target)
495
520
  await new Promise((resolve) => setTimeout(resolve, 300))
496
521
  }
@@ -500,7 +525,7 @@ export function createWatchdog(ctx, config = {}, logger = {}) {
500
525
  async function enforceCrashPlan(plan) {
501
526
  if (!plan || !plan.target || watchdogState.crashFallback !== 'enforce') return
502
527
  try {
503
- watchdogState.quarantine.set(plan.target, { reason: 'crash', level: plan.level, at: new Date().toISOString(), release: 'manual', evidence: plan.reason })
528
+ watchdogState.quarantine.set(plan.target, { reason: 'crash', level: plan.level, at: new Date().toISOString(), release: 'manual', evidence: plan.reason, remediation: remediationFor('crash') })
504
529
  const disabled = [plan.target].filter((target) => !EXEMPT_TARGETS.has(target))
505
530
  await disableTarget(plan.target, 'crash-' + plan.level)
506
531
  let targets = [plan.target]
@@ -584,7 +609,7 @@ export function createWatchdog(ctx, config = {}, logger = {}) {
584
609
  if (thresholds && mb >= Math.round(thresholds.singleBlock / (1024 * 1024))) {
585
610
  appendIncident({ kind: 'plugin-budget', target: name, level: 'block', detail: '启动实测 ' + mb + ' MB > 500 MB,禁止该插件' })
586
611
  if (watchdogState.enforce) {
587
- watchdogState.quarantine.set(name, { reason: 'budget', level: 'L1', at: new Date().toISOString(), release: 'manual', evidence: mb + ' MB > 500 MB' })
612
+ watchdogState.quarantine.set(name, { reason: 'budget', level: 'L1', at: new Date().toISOString(), release: 'manual', evidence: mb + ' MB > 500 MB', remediation: remediationFor('budget') })
588
613
  void disableTarget(name, 'budget-over-500MB')
589
614
  writePendingBoot({ level: 'L1', reason: 'budget', disable: [name], keep: [] })
590
615
  }
@@ -674,6 +699,7 @@ export function createWatchdog(ctx, config = {}, logger = {}) {
674
699
  const plan = {
675
700
  at: new Date().toISOString(),
676
701
  level,
702
+ remediation: remediationFor('crash'),
677
703
  crash: { file: crash.file, time: crash.parsed.time, phase: crash.parsed.phase, oom: crash.parsed.oom, hostExitCode: crash.parsed.hostExitCode },
678
704
  hostRunId: crash.parsed.hostRunId,
679
705
  target: attribution.picked ? attribution.picked.target : null,
package/locale/en.json CHANGED
@@ -18,5 +18,6 @@
18
18
  "watchdog.noPlans": "No crash attribution plans.",
19
19
  "watchdog.baseline": "Clean baseline: {at}",
20
20
  "watchdog.tamper": "Profile watch: {n} critical file(s), last check {at}",
21
- "watchdog.measure": "Measure plugin budgets now"
21
+ "watchdog.measure": "Measure plugin budgets now",
22
+ "watchdog.measureBlocked": "Heap is frozen: measuring spawns child processes, so it stays disabled until the heap recovers."
22
23
  }
package/locale/zh.json CHANGED
@@ -18,5 +18,6 @@
18
18
  "watchdog.noPlans": "暂无崩溃归因计划。",
19
19
  "watchdog.baseline": "干净基线:{at}",
20
20
  "watchdog.tamper": "配置监控:{n} 个关键文件,最近检查 {at}",
21
- "watchdog.measure": "立即测量插件预算"
21
+ "watchdog.measure": "立即测量插件预算",
22
+ "watchdog.measureBlocked": "堆已冻结:测量会启动子进程,恢复前暂停测量。"
22
23
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twinsearth/ai-prison-dsh-plugin",
3
- "version": "1.4.5",
3
+ "version": "1.4.7",
4
4
  "description": "AI Prison for DSH: six governance tools wrapping the aiprison CLI (levels, demo, drill, inspect, verify, repair-tail) plus a panel for the L0–L5 matrix and the audit-chain state. The CLI ships bundled for Windows, macOS and Linux; every capability-removal backend stays simulated.",
5
5
  "type": "module",
6
6
  "main": "index.js",