@furongjun1999/dsh-memory 0.2.9 → 0.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/README.md +430 -359
- package/docs/guardrail-charter.md +2 -2
- package/lib/bridge.d.ts +1 -1
- package/lib/bridge.js +19 -4
- package/lib/hooks.d.ts +14 -0
- package/lib/hooks.js +72 -3
- package/lib/index.d.ts +4 -1
- package/lib/index.js +72 -31
- package/lib/mutual.d.ts +6 -3
- package/lib/mutual.js +131 -44
- package/lib/roleplay_web.js +569 -432
- package/lib/tools.d.ts +10 -1
- package/lib/tools.js +51 -6
- package/package.json +5 -3
- package/src/bridge.ts +296 -0
- package/src/hooks.ts +138 -0
- package/src/index.ts +237 -0
- package/src/mutual.ts +401 -0
- package/src/roleplay_web.ts +851 -0
- package/src/tools.ts +226 -0
package/lib/mutual.js
CHANGED
|
@@ -75,30 +75,79 @@ function execP(cmd) {
|
|
|
75
75
|
});
|
|
76
76
|
});
|
|
77
77
|
}
|
|
78
|
-
/** 检测 harness
|
|
78
|
+
/** 检测 harness 进程是否存在
|
|
79
|
+
* P1 完善(GPT 审查·wmic 兼容):wmic 在 Win11+ 已被移除——失败时
|
|
80
|
+
* fallback PowerShell Get-CimInstance(Win);非 Windows 用 ps 查询。 */
|
|
79
81
|
export async function harnessRunning() {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
+
if (process.platform === 'win32') {
|
|
83
|
+
// ① wmic(旧 Win10 可用)
|
|
84
|
+
const out = await execP(`wmic process where "name='python.exe'" get commandline 2>nul`);
|
|
85
|
+
if (out.includes('harness.guardian') || out.includes('harness.main'))
|
|
86
|
+
return true;
|
|
87
|
+
// ② wmic 不可用(Win11 移除)→ PowerShell
|
|
88
|
+
const psOut = await execP(`powershell -NoProfile -Command "Get-CimInstance Win32_Process -Filter \\"name='python.exe'\\" | Select-Object -ExpandProperty CommandLine"`);
|
|
89
|
+
return psOut.includes('harness.guardian') || psOut.includes('harness.main');
|
|
90
|
+
}
|
|
91
|
+
// 非 Windows(Linux/macOS):ps 查询
|
|
92
|
+
const psOut = await execP(`ps -eo command | grep -E 'harness\\.(guardian|main)' | grep -v grep`);
|
|
93
|
+
return !!psOut;
|
|
82
94
|
}
|
|
83
95
|
/** detached 拉起 A 侧 harness.guardian(幂等:先确认不存在) */
|
|
84
96
|
export async function ensureHarness(python = 'python', opts = DEFAULTS) {
|
|
85
97
|
const running = await harnessRunning();
|
|
86
98
|
if (running)
|
|
87
99
|
return 'already';
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
100
|
+
// P1 修复(GPT 审查):spawn 找不到可执行文件是异步 error 事件,无监听
|
|
101
|
+
// 会直接崩 Node(Unhandled 'error' event)。加监听 + 超时判定拉起失败。
|
|
102
|
+
return new Promise((resolve) => {
|
|
103
|
+
let child = null;
|
|
104
|
+
let failTimer = null;
|
|
105
|
+
let okTimer = null;
|
|
106
|
+
let settled = false;
|
|
107
|
+
const finish = (r) => {
|
|
108
|
+
if (settled)
|
|
109
|
+
return;
|
|
110
|
+
settled = true;
|
|
111
|
+
if (failTimer)
|
|
112
|
+
clearTimeout(failTimer);
|
|
113
|
+
if (okTimer)
|
|
114
|
+
clearTimeout(okTimer);
|
|
115
|
+
resolve(r);
|
|
116
|
+
};
|
|
117
|
+
try {
|
|
118
|
+
child = spawn(python, ['-m', 'harness.guardian'], {
|
|
119
|
+
windowsHide: true,
|
|
120
|
+
detached: true,
|
|
121
|
+
stdio: 'ignore',
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
catch (e) {
|
|
125
|
+
log(opts, `守护失败:${String(e)}`);
|
|
126
|
+
finish('failed');
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
// 拉起失败(python 不存在等)→ 记录 + failed,不崩 Node。
|
|
130
|
+
// spawn 的 error 事件是异步的:不能立即判 started——等 50ms error 窗口,
|
|
131
|
+
// 无 error 才算拉起成功(GPT 审查:此前立即返回 started,错误路径也报成功)。
|
|
132
|
+
child.on('error', (err) => {
|
|
133
|
+
log(opts, `守护拉起错误:${err.message}`);
|
|
134
|
+
finish('failed');
|
|
93
135
|
});
|
|
136
|
+
// 拉起成功但立即退出(harness.guardian 不存在)→ failed
|
|
137
|
+
child.on('exit', (code) => {
|
|
138
|
+
if (code !== null && code !== 0) {
|
|
139
|
+
log(opts, `守护进程退出 code=${code}(guardian 可能不可用)`);
|
|
140
|
+
finish('failed');
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
okTimer = setTimeout(() => finish('started'), 50);
|
|
144
|
+
failTimer = setTimeout(() => {
|
|
145
|
+
log(opts, `守护拉起超时(10s 无健康确认)`);
|
|
146
|
+
finish('failed');
|
|
147
|
+
}, 10_000);
|
|
94
148
|
child.unref();
|
|
95
149
|
log(opts, `守护动作:拉起 harness.guardian(pid=${child.pid ?? '?'})`);
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
catch (e) {
|
|
99
|
-
log(opts, `守护失败:${String(e)}`);
|
|
100
|
-
return 'failed';
|
|
101
|
-
}
|
|
150
|
+
});
|
|
102
151
|
}
|
|
103
152
|
/** 扫描互维目录里 A→B 的 pending 任务 */
|
|
104
153
|
export function scanTasks(opts = DEFAULTS) {
|
|
@@ -139,6 +188,11 @@ export async function llmReview(claim, whitebox, reviewFn) {
|
|
|
139
188
|
/** 综合 verdict(白箱优先) */
|
|
140
189
|
export function combineVerdict(w, l) {
|
|
141
190
|
const c = l.conclusion || '';
|
|
191
|
+
// P1 修复(GPT 审查):白箱通道异常 → fail-closed(不因复核「同意」而 pass)
|
|
192
|
+
if (w.judgment.startsWith('whitebox_error') || w.judgment.startsWith('llm_error')
|
|
193
|
+
|| w.d_norm < 0) {
|
|
194
|
+
return 'needs_revision';
|
|
195
|
+
}
|
|
142
196
|
if (w.judgment.startsWith('采纳')) {
|
|
143
197
|
// 白箱采纳:复核质疑/不同意→needs_revision(白箱优先,记录分歧);否则 pass
|
|
144
198
|
return c.includes('质疑') || c.includes('不同意') || c.includes('不通过')
|
|
@@ -149,43 +203,73 @@ export function combineVerdict(w, l) {
|
|
|
149
203
|
// 证据不足/无法判断 → 白箱不确定,交给复核决定(「不同意」须排除「同意」子串)
|
|
150
204
|
if (c.includes('不同意') || c.includes('不通过'))
|
|
151
205
|
return 'needs_revision';
|
|
152
|
-
if (c.includes('通过') || c.includes('同意')
|
|
206
|
+
if (c.includes('通过') || c.includes('同意'))
|
|
153
207
|
return 'pass';
|
|
208
|
+
// P1 修复(GPT 审查):「质疑」是分歧信号——白箱不确定 + 复核质疑 → 需要修订,
|
|
209
|
+
// 此前把「质疑」当 pass 与白箱采纳分支语义矛盾。
|
|
210
|
+
if (c.includes('质疑'))
|
|
211
|
+
return 'needs_revision';
|
|
154
212
|
return 'needs_revision';
|
|
155
213
|
}
|
|
214
|
+
/** 任务 ID 安全校验(P1 修复:防路径穿越——task.id 可含 ../../ 写目录外) */
|
|
215
|
+
const TASK_ID_RE = /^[A-Za-z0-9._-]{1,64}$/;
|
|
216
|
+
export function safeTaskId(id) {
|
|
217
|
+
return TASK_ID_RE.test(id) && !id.includes('..');
|
|
218
|
+
}
|
|
156
219
|
/** 处理单个任务:双通道验证 → 写回 result */
|
|
157
220
|
export async function processTask(task, verifyFn, reviewFn, opts = DEFAULTS) {
|
|
158
221
|
const dir = path.join(netDir(opts), 'tasks');
|
|
222
|
+
// P1 修复(GPT 审查):task.id 路径穿越——非法 id 拒绝处理,不写任何文件
|
|
223
|
+
if (!safeTaskId(task.id)) {
|
|
224
|
+
log(opts, `任务 ${JSON.stringify(task.id)} 非法(路径穿越风险),拒绝处理`);
|
|
225
|
+
throw new Error(`非法任务 id: ${task.id}`);
|
|
226
|
+
}
|
|
159
227
|
const taskPath = path.join(dir, `task-${task.id}.json`);
|
|
160
|
-
//
|
|
228
|
+
// P1 完善(GPT 审查·任务竞态):原子 claim——rename task→processing。
|
|
229
|
+
// 多实例同时 scanTasks 读到同一 pending 时,rename 只有一方成功;
|
|
230
|
+
// 失败(文件已被 rename/不存在)= 其他实例正在处理 → 跳过(返回 null)。
|
|
231
|
+
// 此前无锁:多实例都写 processing/result/done,结果互相覆盖。
|
|
232
|
+
const claimPath = path.join(dir, `processing-${task.id}.json`);
|
|
161
233
|
try {
|
|
162
|
-
fs.
|
|
234
|
+
fs.renameSync(taskPath, claimPath);
|
|
235
|
+
}
|
|
236
|
+
catch {
|
|
237
|
+
return null; // 已被其他实例 claim,或文件不存在
|
|
163
238
|
}
|
|
164
|
-
catch { /* 非阻塞 */ }
|
|
165
|
-
const w = await whiteboxVerify(task.payload.claim, verifyFn);
|
|
166
|
-
const l = await llmReview(task.payload.claim, w, reviewFn);
|
|
167
|
-
const verdict = combineVerdict(w, l);
|
|
168
|
-
const result = {
|
|
169
|
-
task_id: task.id,
|
|
170
|
-
verdict,
|
|
171
|
-
whitebox: w,
|
|
172
|
-
llm_review: l,
|
|
173
|
-
reasons: [
|
|
174
|
-
`白箱判定:${w.judgment}(best=${w.best},d_norm=${w.d_norm})`,
|
|
175
|
-
`复核结论:${l.conclusion}`,
|
|
176
|
-
],
|
|
177
|
-
evidence: [task.payload.source_ref || ''].filter(Boolean),
|
|
178
|
-
verifier: 'B',
|
|
179
|
-
at: Date.now() / 1000,
|
|
180
|
-
};
|
|
181
|
-
fs.writeFileSync(path.join(dir, `result-${task.id}.json`), JSON.stringify(result, null, 2), 'utf-8');
|
|
182
|
-
// 标记 done
|
|
183
239
|
try {
|
|
184
|
-
|
|
240
|
+
const w = await whiteboxVerify(task.payload.claim, verifyFn);
|
|
241
|
+
const l = await llmReview(task.payload.claim, w, reviewFn);
|
|
242
|
+
const verdict = combineVerdict(w, l);
|
|
243
|
+
const result = {
|
|
244
|
+
task_id: task.id,
|
|
245
|
+
verdict,
|
|
246
|
+
whitebox: w,
|
|
247
|
+
llm_review: l,
|
|
248
|
+
reasons: [
|
|
249
|
+
`白箱判定:${w.judgment}(best=${w.best},d_norm=${w.d_norm})`,
|
|
250
|
+
`复核结论:${l.conclusion}`,
|
|
251
|
+
],
|
|
252
|
+
evidence: [task.payload.source_ref || ''].filter(Boolean),
|
|
253
|
+
verifier: 'B',
|
|
254
|
+
at: Date.now() / 1000,
|
|
255
|
+
};
|
|
256
|
+
// 结果先写 tmp 再 rename(原子,防半写文件)
|
|
257
|
+
const resultTmp = path.join(dir, `result-${task.id}.json.tmp`);
|
|
258
|
+
fs.writeFileSync(resultTmp, JSON.stringify(result, null, 2), 'utf-8');
|
|
259
|
+
fs.renameSync(resultTmp, path.join(dir, `result-${task.id}.json`));
|
|
260
|
+
// 完成:清理 claim 文件(rename 回 done 标记供审计)
|
|
261
|
+
fs.renameSync(claimPath, path.join(dir, `done-${task.id}.json`));
|
|
262
|
+
log(opts, `任务 ${task.id} 验证完成:${verdict}`);
|
|
263
|
+
return result;
|
|
264
|
+
}
|
|
265
|
+
catch (e) {
|
|
266
|
+
// 处理失败:释放 claim(rename 回 task 供重试)
|
|
267
|
+
try {
|
|
268
|
+
fs.renameSync(claimPath, taskPath);
|
|
269
|
+
}
|
|
270
|
+
catch { /* 文件可能已不在 */ }
|
|
271
|
+
throw e;
|
|
185
272
|
}
|
|
186
|
-
catch { /* 非阻塞 */ }
|
|
187
|
-
log(opts, `任务 ${task.id} 验证完成:${verdict}`);
|
|
188
|
-
return result;
|
|
189
273
|
}
|
|
190
274
|
// ---------------------------------------------------------------------------
|
|
191
275
|
// 日志 / last_contact
|
|
@@ -209,7 +293,10 @@ export function writeLastContact(opts = DEFAULTS) {
|
|
|
209
293
|
export function installMutualMaintenance(ctx, config = {}, hooks = {}) {
|
|
210
294
|
const opts = { ...DEFAULTS, ...config };
|
|
211
295
|
// 1. 心跳写戳 + 守护 A(10min 周期)
|
|
212
|
-
|
|
296
|
+
// P1 修复(GPT 审查):cordis-plugin-timer 签名是 interval(callback, delay)
|
|
297
|
+
// ——此前 (ms, fn) 写反:数字被当 callback、函数被当 delay,回调永不执行,
|
|
298
|
+
// 互维心跳/任务扫描实际不工作。
|
|
299
|
+
const heartbeatDispose = ctx.interval(() => {
|
|
213
300
|
try {
|
|
214
301
|
writeHeartbeat(opts);
|
|
215
302
|
// 守护 A:harness 不在 → 拉起
|
|
@@ -227,9 +314,9 @@ export function installMutualMaintenance(ctx, config = {}, hooks = {}) {
|
|
|
227
314
|
writeLastContact(opts);
|
|
228
315
|
}
|
|
229
316
|
catch (e) { /* 单次失败不中断 */ }
|
|
230
|
-
});
|
|
317
|
+
}, opts.heartbeatMs);
|
|
231
318
|
// 2. 任务轮询(30s 周期,检查 A→B 任务)
|
|
232
|
-
const taskDispose = ctx.interval(
|
|
319
|
+
const taskDispose = ctx.interval(() => {
|
|
233
320
|
try {
|
|
234
321
|
const tasks = scanTasks(opts);
|
|
235
322
|
for (const t of tasks) {
|
|
@@ -241,7 +328,7 @@ export function installMutualMaintenance(ctx, config = {}, hooks = {}) {
|
|
|
241
328
|
}
|
|
242
329
|
}
|
|
243
330
|
catch (e) { /* 单次失败不中断 */ }
|
|
244
|
-
});
|
|
331
|
+
}, 30_000);
|
|
245
332
|
// 3. effect 作用域清理
|
|
246
333
|
ctx.effect(() => {
|
|
247
334
|
return () => {
|