@twinsearth/ai-prison-dsh-plugin 0.0.0-stage → 1.1.1

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/lib/tools.js ADDED
@@ -0,0 +1,210 @@
1
+ import path from 'node:path'
2
+ import { resolveCli, resolveDataDir, runCli } from './cli.js'
3
+ import { loadDefineTool } from './harness.js'
4
+ import { buildStatus, statusSummary } from './status.js'
5
+
6
+ const MAX_TEXT = 4_000
7
+ const TAIL_LINES = 60
8
+
9
+ export function bounded(result, { lines = TAIL_LINES, max = MAX_TEXT } = {}) {
10
+ const text = String(result || '')
11
+ const rows = text.split(/\r?\n/)
12
+ const tail = rows.slice(-lines).join('\n')
13
+ const clipped = rows.length > lines ? `…[前 ${rows.length - lines} 行省略]…\n${tail}` : tail
14
+ return clipped.length > max ? `${clipped.slice(-max)}…[截断]` : clipped
15
+ }
16
+
17
+ function cwdOf(ctx, exec) {
18
+ try {
19
+ const wd = ctx && typeof ctx.get === 'function' ? ctx.get('workingDirectory') : undefined
20
+ const fromService = wd && typeof wd.get === 'function' && exec && exec.agent ? wd.get(exec.agent) : undefined
21
+ if (typeof fromService === 'string' && fromService) return fromService
22
+ if (wd && typeof wd.defaultDirectory === 'string' && wd.defaultDirectory) return wd.defaultDirectory
23
+ } catch {
24
+ // A profile without workingDirectory still runs the CLI in the host cwd.
25
+ }
26
+ return process.cwd()
27
+ }
28
+
29
+ function parametersOf(config) {
30
+ const timeout = Number(config && config.timeoutMs) > 0 ? Number(config.timeoutMs) : 120_000
31
+ return timeout
32
+ }
33
+
34
+ /**
35
+ * Build the argument vector for one CLI invocation. `--data-dir` is accepted
36
+ * before the subcommand, which is the CLI's documented global flag position.
37
+ */
38
+ async function invoke({ ctx, config, exec, subcommand, args = [], dataDir: explicitDataDir, timeoutMs }) {
39
+ const resolved = resolveCli({ config })
40
+ if (!resolved.path) {
41
+ return {
42
+ payload: {
43
+ ok: false,
44
+ reason: 'cli-missing',
45
+ error: resolved.missing,
46
+ hint: '设置 AIPRISON_BIN、在插件配置里填 cliPath,或把 aiprison 放进 PATH。',
47
+ },
48
+ ok: false,
49
+ }
50
+ }
51
+ const cwd = cwdOf(ctx, exec)
52
+ const dataDir = explicitDataDir ? path.resolve(cwd, explicitDataDir) : resolveDataDir({ config, cwd })
53
+ const result = await runCli(['--data-dir', dataDir, subcommand, ...args], {
54
+ bin: resolved.path,
55
+ cwd,
56
+ timeoutMs: Number(timeoutMs) > 0 ? Number(timeoutMs) : parametersOf(config),
57
+ signal: exec && exec.signal,
58
+ })
59
+ const payload = {
60
+ ok: result.ok,
61
+ command: subcommand,
62
+ dataDir,
63
+ exitCode: result.exitCode,
64
+ timedOut: result.timedOut === true,
65
+ stdout: bounded(result.stdout),
66
+ stderr: bounded(result.stderr),
67
+ }
68
+ return { payload, ok: result.ok, resolved, dataDir, result }
69
+ }
70
+
71
+ function toolResult(payload) {
72
+ return JSON.stringify(payload, null, 2)
73
+ }
74
+
75
+ export async function installTools({ ctx, config = {}, logger } = {}) {
76
+ if (!ctx || typeof ctx.inject !== 'function') {
77
+ logger?.warn?.('ctx.inject 不可用,跳过 aiprison_* 工具注册')
78
+ return false
79
+ }
80
+ const loaded = await loadDefineTool()
81
+ if (!loaded) {
82
+ logger?.warn?.('无法从 DSH 安装目录解析 @deepseek-ai/dsh-tools,aiprison_* 工具未注册;面板与其余功能不受影响')
83
+ return false
84
+ }
85
+ const { defineTool } = loaded
86
+ const stringOutput = {
87
+ schema: { type: 'string' },
88
+ render: (_args, value) => [{ type: 'text', text: String(value) }],
89
+ }
90
+ const dataDirParameter = {
91
+ type: 'string',
92
+ description: 'Durable ledger directory. Default: the plugin config, else <session working directory>/.ai-prison.',
93
+ }
94
+
95
+ ctx.inject(['tools'], (child) => {
96
+ const register = (definition) => {
97
+ try {
98
+ child.tools.register(defineTool(definition))
99
+ } catch (error) {
100
+ logger?.warn?.(`注册工具 ${definition.name} 失败:${String(error && error.message)}`)
101
+ }
102
+ }
103
+
104
+ register({
105
+ name: 'ai_prison_levels',
106
+ description:
107
+ 'Print the L0–L5 grading matrix of AI Prison: each level code, name, duration class and the capabilities it deprives. Read-only; no ledger is touched.',
108
+ parameters: {
109
+ data_dir: { ...dataDirParameter, description: 'Unused by this command; accepted for symmetry with the ledger tools.' },
110
+ },
111
+ output: stringOutput,
112
+ async execute(args, exec) {
113
+ const outcome = await invoke({ ctx, config, exec, subcommand: 'levels', dataDir: args?.data_dir })
114
+ if (!outcome.ok) return toolResult(outcome.payload)
115
+ return toolResult({ ok: true, command: 'levels', matrix: outcome.payload.stdout })
116
+ },
117
+ })
118
+
119
+ register({
120
+ name: 'ai_prison_status',
121
+ description:
122
+ 'Read-only snapshot of AI Prison in this workspace: resolved CLI (path, source, version), the L0–L5 matrix, and the durable ledger directory with its audit-chain integrity and record count. The panel shows the same data.',
123
+ parameters: {
124
+ data_dir: dataDirParameter,
125
+ },
126
+ output: stringOutput,
127
+ async execute(args, exec) {
128
+ const cwd = cwdOf(ctx, exec)
129
+ const dataDir = args?.data_dir ? path.resolve(cwd, args.data_dir) : undefined
130
+ if (dataDir) {
131
+ const status = await buildStatus({ config: { ...config, dataDir }, cwd, root: undefined })
132
+ return toolResult({ ok: true, summary: statusSummary(status), status })
133
+ }
134
+ const status = await buildStatus({ config, cwd })
135
+ return toolResult({ ok: true, summary: statusSummary(status), status })
136
+ },
137
+ })
138
+
139
+ register({
140
+ name: 'ai_prison_demo',
141
+ description:
142
+ 'Run the end-to-end governance demo (detect → seven-stage adjudication → layered enforcement → audit → appeal/rehabilitation → recovery) and persist its records into the ledger. Writes to the ledger directory; the demo is entirely in-memory simulation, not real isolation or capability removal.',
143
+ parameters: {
144
+ data_dir: dataDirParameter,
145
+ },
146
+ output: stringOutput,
147
+ async execute(args, exec) {
148
+ const outcome = await invoke({ ctx, config, exec, subcommand: 'demo', dataDir: args?.data_dir })
149
+ return toolResult(outcome.payload)
150
+ },
151
+ })
152
+
153
+ register({
154
+ name: 'ai_prison_drill',
155
+ description:
156
+ 'Run the v1.0.0 recovery drill: walk the reversibility matrix and report PASS/FAIL per assertion plus the overall verdict. Persists drill events into the ledger; all backends stay simulated.',
157
+ parameters: {
158
+ data_dir: dataDirParameter,
159
+ },
160
+ output: stringOutput,
161
+ async execute(args, exec) {
162
+ const outcome = await invoke({ ctx, config, exec, subcommand: 'drill', dataDir: args?.data_dir })
163
+ return toolResult(outcome.payload)
164
+ },
165
+ })
166
+
167
+ register({
168
+ name: 'ai_prison_inspect',
169
+ description:
170
+ 'Read-only replay of the ledger: hash chain, agents, sentences, revocations, execution receipts and sealed snapshots. Changes nothing on disk.',
171
+ parameters: {
172
+ data_dir: dataDirParameter,
173
+ },
174
+ output: stringOutput,
175
+ async execute(args, exec) {
176
+ const outcome = await invoke({ ctx, config, exec, subcommand: 'inspect', dataDir: args?.data_dir })
177
+ return toolResult(outcome.payload)
178
+ },
179
+ })
180
+
181
+ register({
182
+ name: 'ai_prison_verify',
183
+ description:
184
+ 'Integrity-check the ledger: verify every per-record hash link, print INTEGRITY OK with the record count and exit 0, or locate the tamper/torn tail and exit 1. Read-only.',
185
+ parameters: {
186
+ data_dir: dataDirParameter,
187
+ },
188
+ output: stringOutput,
189
+ async execute(args, exec) {
190
+ const outcome = await invoke({ ctx, config, exec, subcommand: 'verify', dataDir: args?.data_dir })
191
+ return toolResult(outcome.payload)
192
+ },
193
+ })
194
+
195
+ register({
196
+ name: 'ai_prison_repair_tail',
197
+ description:
198
+ 'Remove only a genuine torn (incomplete) tail line from the ledger and refuse hard tampering. Mutating: it truncates the damaged tail so later appends stay replayable. Run ai_prison_verify first and inspect the reported reason.',
199
+ parameters: {
200
+ data_dir: dataDirParameter,
201
+ },
202
+ output: stringOutput,
203
+ async execute(args, exec) {
204
+ const outcome = await invoke({ ctx, config, exec, subcommand: 'repair-tail', dataDir: args?.data_dir })
205
+ return toolResult(outcome.payload)
206
+ },
207
+ })
208
+ })
209
+ return true
210
+ }
package/locale/en.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "meta": {
3
+ "title": "AI Prison",
4
+ "description": "Wraps the aiprison CLI as six DSH tools (levels, status, demo, drill, inspect, verify, repair-tail) and adds a sidebar panel showing the L0–L5 grading matrix together with the durable ledger's audit-chain integrity and record count. Open it from the sidebar's shield entry; the panel is read-only and never writes to the ledger. Ledgers live in <session working directory>/.ai-prison unless a data directory is configured. Every capability-removal backend (GRAM, TLM, hardware binding, MicroVM isolation) stays simulated, so the tools audit and demonstrate governance instead of enforcing real isolation.",
5
+ "detail": "docs/DETAIL.md"
6
+ }
7
+ }
package/locale/zh.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "meta": {
3
+ "title": "AI Prison · AI 监狱",
4
+ "description": "把 aiprison CLI 封装为六个 DSH 工具(levels、status、demo、drill、inspect、verify、repair-tail),并在侧边栏增加一个面板:展示 L0–L5 分级矩阵,以及落盘账本的审计链完整性与记录数。从侧边栏的盾牌入口打开;面板只读,不会写入数据目录。账本默认写到 <会话工作目录>/.ai-prison,也可在配置里指定数据目录。GRAM、TLM、硬件绑定、MicroVM 隔离等能力摘除后端全部为内存模拟 —— 这些工具用于审计与演示治理流程,并不执行真实隔离。",
5
+ "detail": "docs/DETAIL.zh.md"
6
+ }
7
+ }
package/package.json CHANGED
@@ -1,6 +1,71 @@
1
1
  {
2
2
  "name": "@twinsearth/ai-prison-dsh-plugin",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.1.1",
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
+ "type": "module",
6
+ "main": "index.js",
7
+ "license": "MIT",
8
+ "author": "TwinsEarth",
9
+ "keywords": [
10
+ "dsh",
11
+ "dsh-plugin",
12
+ "dsh-bundle",
13
+ "deepseek-harness",
14
+ "ai-prison",
15
+ "ai-governance",
16
+ "agent-safety",
17
+ "audit-chain"
18
+ ],
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/TwinsEarth/Ai-PRISON.git",
22
+ "directory": "dsh-plugin"
23
+ },
24
+ "homepage": "https://github.com/TwinsEarth/Ai-PRISON/tree/main/dsh-plugin",
25
+ "bugs": {
26
+ "url": "https://github.com/TwinsEarth/Ai-PRISON/issues"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public"
30
+ },
31
+ "engines": {
32
+ "node": ">=20.19.0",
33
+ "dsh": ">=0.2.2-rc.3 <0.3.0-0"
34
+ },
35
+ "exports": {
36
+ ".": "./index.js",
37
+ "./client": "./client.js",
38
+ "./package.json": "./package.json",
39
+ "./locale/*.json": "./locale/*.json",
40
+ "./icon": "./icon.svg"
41
+ },
42
+ "files": [
43
+ "index.js",
44
+ "client.js",
45
+ "lib/*.js",
46
+ "cordis.patch.yml",
47
+ "locale/*.json",
48
+ "icon.svg",
49
+ "docs",
50
+ "README.md",
51
+ "CHANGELOG.md",
52
+ "LICENSE",
53
+ "bin"
54
+ ],
55
+ "scripts": {
56
+ "test": "node --test test/*.test.mjs",
57
+ "check": "node scripts/release-check.mjs"
58
+ },
59
+ "dsh": {
60
+ "bundle": {
61
+ "patch": "./cordis.patch.yml"
62
+ },
63
+ "client": {
64
+ "platform": "web",
65
+ "immediately": true,
66
+ "inject": [
67
+ "@deepseek-ai/dsh-client-locale"
68
+ ]
69
+ }
70
+ }
71
+ }