@twinsearth/ai-prison-dsh-plugin 1.4.4 → 1.4.5
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 +26 -0
- package/lib/budget-child.mjs +27 -2
- package/lib/budget-probe.js +2 -1
- package/lib/tamper-monitor.js +16 -9
- package/lib/watchdog-runtime.js +22 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,32 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to `@twinsearth/ai-prison-dsh-plugin` are recorded here.
|
|
4
4
|
|
|
5
|
+
## [1.4.5] — 2026-11-18
|
|
6
|
+
|
|
7
|
+
### Fixed — tamper monitor false positives (important)
|
|
8
|
+
|
|
9
|
+
- The first tamper monitor attributed writes at **poll time**, not at write time, so
|
|
10
|
+
a legitimate plugin-manager write (install/enable rewrites package.json and
|
|
11
|
+
pnpm-lock.yaml) was classified as tampering 30 s later. That closed and jailed
|
|
12
|
+
plugins after every install.
|
|
13
|
+
- Operation windows are now recorded on `started`/`settled` and matched against
|
|
14
|
+
each changed file's **mtime** (±5 s), so a manager write stays legitimate even
|
|
15
|
+
when the poll happens after the window closed.
|
|
16
|
+
- One-time migration: tamper quarantine persisted by 1.4.3/1.4.4 is cleared with
|
|
17
|
+
an audited `migration` incident instead of re-closing those plugins on boot.
|
|
18
|
+
|
|
19
|
+
### Fixed — budget probe could not import plugins that use harness peers
|
|
20
|
+
|
|
21
|
+
- `budget-child.mjs` now resolves `@deepseek-ai/*` and `cordis` from the DSH
|
|
22
|
+
installation via `module.registerHooks`, so plugins like `dsh-context` that
|
|
23
|
+
import `@deepseek-ai/dsh-session` at module scope can be measured.
|
|
24
|
+
- Also fixed a wrong `pathToFileURL` import that broke the probe child entirely.
|
|
25
|
+
|
|
26
|
+
### Tests
|
|
27
|
+
- mtime operation-window suppression, legacy tamper-quarantine migration, and the
|
|
28
|
+
probe child import path are covered by the test suite (70 tests total).
|
|
29
|
+
|
|
30
|
+
## [1.4.4] — 2026-11-18
|
|
5
31
|
## [1.4.4] — 2026-11-18
|
|
6
32
|
|
|
7
33
|
### Fixed — per-plugin budget probe actually works end to end
|
package/lib/budget-child.mjs
CHANGED
|
@@ -1,14 +1,39 @@
|
|
|
1
|
+
import * as nodeModule from 'node:module'
|
|
1
2
|
import { createRequire } from 'node:module'
|
|
2
3
|
import { pathToFileURL } from 'node:url'
|
|
4
|
+
import path from 'node:path'
|
|
3
5
|
|
|
4
6
|
// Child side of the AI Prison per-plugin budget probe (see budget-probe.js).
|
|
5
|
-
// argv: [node, --expose-gc, thisFile, profileDir, packageName]
|
|
6
|
-
const [, , profileDir, packageName] = process.argv
|
|
7
|
+
// argv: [node, --expose-gc, thisFile, profileDir, packageName, installDir]
|
|
8
|
+
const [, , profileDir, packageName, installDir] = process.argv
|
|
7
9
|
|
|
8
10
|
function heapMB() {
|
|
9
11
|
return process.memoryUsage().heapUsed / (1024 * 1024)
|
|
10
12
|
}
|
|
11
13
|
|
|
14
|
+
// Plugins import peer packages such as @deepseek-ai/dsh-session that live in the
|
|
15
|
+
// DSH installation, not in the profile. Resolve those from the install root so
|
|
16
|
+
// the probe can measure the real entry module.
|
|
17
|
+
if (installDir && typeof nodeModule.registerHooks === 'function') {
|
|
18
|
+
try {
|
|
19
|
+
const requireFromInstall = createRequire(path.join(installDir, 'package.json'))
|
|
20
|
+
nodeModule.registerHooks({
|
|
21
|
+
resolve(specifier, context, nextResolve) {
|
|
22
|
+
if (specifier.startsWith('@deepseek-ai/') || specifier === 'cordis') {
|
|
23
|
+
try {
|
|
24
|
+
return { url: pathToFileURL(requireFromInstall.resolve(specifier)).href, shortCircuit: true }
|
|
25
|
+
} catch {
|
|
26
|
+
// fall through to the default resolver
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return nextResolve(specifier, context)
|
|
30
|
+
},
|
|
31
|
+
})
|
|
32
|
+
} catch {
|
|
33
|
+
// hooks are best effort; the probe still measures what resolves normally
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
12
37
|
async function main() {
|
|
13
38
|
if (!profileDir || !packageName) {
|
|
14
39
|
console.log(JSON.stringify({ ok: false, error: 'missing-args' }))
|
package/lib/budget-probe.js
CHANGED
|
@@ -12,10 +12,11 @@ const CHILD = fileURLToPath(new URL('./budget-child.mjs', import.meta.url))
|
|
|
12
12
|
export function probeBudget({ profileDir, packageName, timeoutMs = 25_000 } = {}) {
|
|
13
13
|
return new Promise((resolve) => {
|
|
14
14
|
const nodeExe = process.env.DSH_PROBE_NODE || process.execPath
|
|
15
|
+
const installDir = process.env.DSH_INSTALL_DIR || (typeof process.argv[2] === 'string' && process.argv[2].endsWith('package.json') === false ? process.argv[2] : '')
|
|
15
16
|
const env = { ...process.env }
|
|
16
17
|
if (process.versions.electron) env.ELECTRON_RUN_AS_NODE = '1'
|
|
17
18
|
delete env.NODE_OPTIONS
|
|
18
|
-
const child = spawn(nodeExe, ['--expose-gc', CHILD, profileDir, packageName], {
|
|
19
|
+
const child = spawn(nodeExe, ['--expose-gc', CHILD, profileDir, packageName, installDir || ''], {
|
|
19
20
|
env,
|
|
20
21
|
windowsHide: true,
|
|
21
22
|
stdio: ['ignore', 'pipe', 'pipe'],
|
package/lib/tamper-monitor.js
CHANGED
|
@@ -33,7 +33,9 @@ export function snapshotCriticalFiles(profileDir) {
|
|
|
33
33
|
const file = path.join(profileDir, rel)
|
|
34
34
|
const bytes = readIfExists(file)
|
|
35
35
|
contents.set(rel, bytes)
|
|
36
|
-
|
|
36
|
+
let mtimeMs = 0
|
|
37
|
+
try { mtimeMs = fs.statSync(file).mtimeMs } catch { mtimeMs = 0 }
|
|
38
|
+
files.push({ file: rel, exists: bytes !== null, hash: bytes === null ? null : sha256(bytes), size: bytes === null ? 0 : bytes.length, mtimeMs })
|
|
37
39
|
}
|
|
38
40
|
return { files, contents }
|
|
39
41
|
}
|
|
@@ -45,9 +47,9 @@ export function diffSnapshots(previous, next) {
|
|
|
45
47
|
for (const entry of next || []) {
|
|
46
48
|
const old = before.get(entry.file)
|
|
47
49
|
if (!old) continue
|
|
48
|
-
if (!old.exists && entry.exists) diffs.push({ file: entry.file, kind: 'created' })
|
|
49
|
-
else if (old.exists && !entry.exists) diffs.push({ file: entry.file, kind: 'deleted' })
|
|
50
|
-
else if (old.exists && entry.exists && old.hash !== entry.hash) diffs.push({ file: entry.file, kind: 'modified' })
|
|
50
|
+
if (!old.exists && entry.exists) diffs.push({ file: entry.file, kind: 'created', mtimeMs: entry.mtimeMs })
|
|
51
|
+
else if (old.exists && !entry.exists) diffs.push({ file: entry.file, kind: 'deleted', mtimeMs: entry.mtimeMs })
|
|
52
|
+
else if (old.exists && entry.exists && old.hash !== entry.hash) diffs.push({ file: entry.file, kind: 'modified', mtimeMs: entry.mtimeMs })
|
|
51
53
|
}
|
|
52
54
|
return diffs
|
|
53
55
|
}
|
|
@@ -108,6 +110,7 @@ export function createTamperMonitor({
|
|
|
108
110
|
graceMs = STARTUP_GRACE_MS,
|
|
109
111
|
intervalMs = DEFAULT_INTERVAL_MS,
|
|
110
112
|
shouldIgnore,
|
|
113
|
+
isWithinOperationWindow,
|
|
111
114
|
onTamper,
|
|
112
115
|
log,
|
|
113
116
|
} = {}) {
|
|
@@ -138,9 +141,13 @@ export function createTamperMonitor({
|
|
|
138
141
|
status.skipped = null
|
|
139
142
|
return { ok: true, diffs: [], restored: [] }
|
|
140
143
|
}
|
|
144
|
+
// A changed file written inside a plugin-manager operation window is a
|
|
145
|
+
// legitimate manager write even when this poll runs after the window closed.
|
|
146
|
+
const withinWindow = (mtimeMs) => typeof isWithinOperationWindow === 'function' && isWithinOperationWindow(mtimeMs)
|
|
147
|
+
const suspicious = force ? diffs : diffs.filter((diff) => !withinWindow(diff.mtimeMs))
|
|
141
148
|
const startup = !force && Date.now() - baselineAt < graceMs
|
|
142
149
|
const ignored = !force && typeof shouldIgnore === 'function' && shouldIgnore()
|
|
143
|
-
if (startup || ignored) {
|
|
150
|
+
if (startup || ignored || suspicious.length === 0) {
|
|
144
151
|
baseline = next
|
|
145
152
|
baselineAt = Date.now()
|
|
146
153
|
status.skipped = startup ? 'startup' : 'operation-window'
|
|
@@ -148,16 +155,16 @@ export function createTamperMonitor({
|
|
|
148
155
|
if (typeof log === 'function') log(`tamper monitor: ${diffs.map((d) => d.file).join(', ')} ${status.skipped}(视为正常写入)`)
|
|
149
156
|
return { ok: true, diffs: [], restored: [], skipped: status.skipped }
|
|
150
157
|
}
|
|
151
|
-
const backup = backupTampered(dataDir, profileDir,
|
|
152
|
-
const restored = policy === 'restore' ? restoreBaseline(profileDir, baseline.contents,
|
|
153
|
-
status.lastDiff =
|
|
158
|
+
const backup = backupTampered(dataDir, profileDir, suspicious)
|
|
159
|
+
const restored = policy === 'restore' ? restoreBaseline(profileDir, baseline.contents, suspicious) : []
|
|
160
|
+
status.lastDiff = suspicious
|
|
154
161
|
status.restored = restored
|
|
155
162
|
status.incidents += 1
|
|
156
163
|
// Re-baseline on the post-restore state so one drift yields one alert.
|
|
157
164
|
baseline = snapshotCriticalFiles(profileDir)
|
|
158
165
|
baselineAt = Date.now()
|
|
159
166
|
if (backup.length > 0 && typeof log === 'function') log(`tamper monitor: 备份到 ${backup.length} 个文件`)
|
|
160
|
-
if (typeof onTamper === 'function') onTamper({ diffs, restored, policy, backup })
|
|
167
|
+
if (typeof onTamper === 'function') onTamper({ diffs: suspicious, restored, policy, backup })
|
|
161
168
|
return { ok: true, diffs, restored, backup }
|
|
162
169
|
}
|
|
163
170
|
|
package/lib/watchdog-runtime.js
CHANGED
|
@@ -137,6 +137,8 @@ export function createWatchdog(ctx, config = {}, logger = {}) {
|
|
|
137
137
|
|
|
138
138
|
let lastFlush = 0
|
|
139
139
|
let operationWindowUntil = 0
|
|
140
|
+
const operationWindows = []
|
|
141
|
+
let tamperMigrationDone = false
|
|
140
142
|
let tamper = null
|
|
141
143
|
restoreState()
|
|
142
144
|
readCrashLogs()
|
|
@@ -155,6 +157,10 @@ export function createWatchdog(ctx, config = {}, logger = {}) {
|
|
|
155
157
|
graceMs: Number(config.tamperGraceMs) >= 0 ? Number(config.tamperGraceMs) : 15_000,
|
|
156
158
|
intervalMs: Number(config.tamperIntervalMs) > 0 ? Number(config.tamperIntervalMs) : 30_000,
|
|
157
159
|
shouldIgnore: () => Date.now() < operationWindowUntil,
|
|
160
|
+
isWithinOperationWindow: (mtimeMs) => {
|
|
161
|
+
const now = Date.now()
|
|
162
|
+
return operationWindows.some((window) => mtimeMs >= window.startedAt - 5000 && mtimeMs <= (window.settledAt || now) + 5000)
|
|
163
|
+
},
|
|
158
164
|
onTamper: handleTamper,
|
|
159
165
|
log: (message) => log.info('[ai-prison-watchdog] ' + message),
|
|
160
166
|
})
|
|
@@ -212,6 +218,7 @@ export function createWatchdog(ctx, config = {}, logger = {}) {
|
|
|
212
218
|
baseline: watchdogState.baseline,
|
|
213
219
|
budgets: watchdogState.budgets,
|
|
214
220
|
quarantine: [...watchdogState.quarantine].map(([target, entry]) => ({ target, ...entry })),
|
|
221
|
+
migratedTamperQuarantine: tamperMigrationDone,
|
|
215
222
|
}
|
|
216
223
|
try {
|
|
217
224
|
fs.writeFileSync(stateFile, `${JSON.stringify(payload, null, 2)}\n`)
|
|
@@ -228,10 +235,20 @@ export function createWatchdog(ctx, config = {}, logger = {}) {
|
|
|
228
235
|
if (parsed.escalation && Array.isArray(parsed.escalation.crashes)) watchdogState.escalation = { crashes: parsed.escalation.crashes.slice(-50), level: parsed.escalation.level || 'L0' }
|
|
229
236
|
if (parsed.baseline && Array.isArray(parsed.baseline.bundles)) watchdogState.baseline = parsed.baseline
|
|
230
237
|
if (parsed.budgets && typeof parsed.budgets === 'object') watchdogState.budgets = parsed.budgets
|
|
238
|
+
tamperMigrationDone = parsed.migratedTamperQuarantine === true
|
|
231
239
|
if (Array.isArray(parsed.quarantine)) {
|
|
240
|
+
let dropped = 0
|
|
232
241
|
for (const entry of parsed.quarantine) {
|
|
242
|
+
if (!tamperMigrationDone && entry && entry.reason === 'tamper') {
|
|
243
|
+
dropped += 1
|
|
244
|
+
continue
|
|
245
|
+
}
|
|
233
246
|
if (entry && typeof entry.target === 'string') watchdogState.quarantine.set(entry.target, { reason: entry.reason || 'crash', level: entry.level || 'L1', at: entry.at || new Date().toISOString(), release: entry.release || 'manual', evidence: entry.evidence || '' })
|
|
234
247
|
}
|
|
248
|
+
if (dropped > 0) {
|
|
249
|
+
watchdogState.incidents.push({ at: new Date().toISOString(), mode: watchdogState.mode, kind: 'migration', detail: '清理 ' + dropped + ' 条 1.4.3/1.4.4 误判的 tamper 隔离(操作窗口归因已修复)' })
|
|
250
|
+
}
|
|
251
|
+
tamperMigrationDone = true
|
|
235
252
|
}
|
|
236
253
|
} catch {
|
|
237
254
|
// first run or unreadable state: start clean
|
|
@@ -376,6 +393,8 @@ export function createWatchdog(ctx, config = {}, logger = {}) {
|
|
|
376
393
|
const entry = watchdogState.operations.get(target) || { count: 0, estimateMB: 0, lastAt: null, version: operation.version || null, lastApplication: null }
|
|
377
394
|
if (operation.phase === 'started') {
|
|
378
395
|
operationWindowUntil = Date.now() + 4000
|
|
396
|
+
operationWindows.push({ startedAt: Date.now(), settledAt: null })
|
|
397
|
+
if (operationWindows.length > 20) operationWindows.shift()
|
|
379
398
|
inflight.set(operation.id, { target, heapBefore: watchdogState.latest ? watchdogState.latest.heapUsed : 0 })
|
|
380
399
|
entry.lastAt = operation.time || entry.lastAt
|
|
381
400
|
watchdogState.operations.set(target, entry)
|
|
@@ -383,6 +402,9 @@ export function createWatchdog(ctx, config = {}, logger = {}) {
|
|
|
383
402
|
}
|
|
384
403
|
if (operation.phase !== 'settled') return
|
|
385
404
|
operationWindowUntil = Date.now() + 4000
|
|
405
|
+
for (let i = operationWindows.length - 1; i >= 0; i -= 1) {
|
|
406
|
+
if (operationWindows[i].settledAt === null) { operationWindows[i].settledAt = Date.now(); break }
|
|
407
|
+
}
|
|
386
408
|
const weight = operationWeight(operation)
|
|
387
409
|
if (weight > 0 && operation.application !== 'failed' && operation.application !== 'cancelled') {
|
|
388
410
|
entry.count = Math.round((Number(entry.count) + weight) * 10) / 10
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twinsearth/ai-prison-dsh-plugin",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.5",
|
|
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",
|