@furongjun1999/dsh-memory 0.9.7 → 0.9.9
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 +150 -662
- package/codebuddy/CODEBUDDY.md +337 -204
- package/docs/README.md +21 -0
- package/docs/discipline/harnesses.yaml +11 -3
- package/docs/discipline/templates/compact.txt.tmpl +20 -18
- package/docs/discipline/templates/full.md.tmpl +63 -61
- package/docs/discipline/templates/rules.mdc.tmpl +5 -3
- package/docs/discipline/templates/skill.md.tmpl +25 -23
- package/docs/discipline/templates/spec.txt.tmpl +1 -1
- package/docs/discipline/templates/zcode-user.md.tmpl +1 -1
- package/docs//350/247/204/350/214/203/AI/347/274/226/347/250/213/351/200/232/347/224/250/350/247/204/350/214/203.md +4 -0
- package/dsh/cordis.yml.example +2 -2
- package/lib/hooks.d.ts +11 -1
- package/lib/hooks.js +59 -4
- package/lib/index.d.ts +1 -1
- package/lib/index.js +11 -5
- package/lib/init.js +28 -4
- package/lib/lib/hook_audit.d.ts +3 -0
- package/lib/lib/hook_audit.js +63 -0
- package/md_cg/autonomy.py +1 -1
- package/md_cg/config_registry.py +329 -329
- package/md_cg/config_registry_bulk.py +483 -482
- package/md_cg/config_render.py +27 -0
- package/md_cg/config_validate.py +456 -455
- package/md_cg/conflict_surface.py +127 -0
- package/md_cg/mcp_server.py +5324 -5227
- package/md_cg/mdcg.py +12 -1
- package/md_cg/mdcos.py +7159 -7108
- package/md_cg/pooling.py +487 -485
- package/md_cg/qmd_llm.py +334 -334
- package/md_cg/reach.py +10 -4
- package/md_cg/sleep.py +2 -2
- package/md_cg/sustain.py +13 -4
- package/md_cg/test_auto_defaults.py +463 -463
- package/md_cg/test_autonomy_emotion.py +328 -328
- package/md_cg/test_bm25_default.py +56 -0
- package/md_cg/test_c3_transient_read_negative.py +7 -3
- package/md_cg/test_conflict_surface.py +75 -0
- package/md_cg/test_conflict_value_untrusted.py +63 -0
- package/md_cg/test_exact_text_cache.py +194 -191
- package/md_cg/test_heartbeat_ledger.py +563 -557
- package/md_cg/test_hotcache_tool.py +121 -0
- package/md_cg/test_issue274_read_node_id_none.py +364 -341
- package/md_cg/test_issue561_pending_review.py +89 -5
- package/md_cg/test_issue578_service_info_gate.py +264 -0
- package/md_cg/test_n184_keys_concurrent_provision.py +1 -1
- package/md_cg/test_p3_legacy_closure.py +441 -438
- package/md_cg/test_p43_pooling.py +419 -412
- package/md_cg/test_reach.py +5 -1
- package/md_cg/test_reach_exits.py +54 -0
- package/md_cg/test_reach_single_char.py +74 -0
- package/md_cg/test_read_scope_b27.py +2 -2
- package/md_cg/test_search_bm25_default.py +60 -0
- package/md_cg/test_security_audit_v21.py +249 -249
- package/md_cg/test_sleep_p1.py +1032 -1032
- package/md_cg/test_sustain_bad_stamp.py +178 -0
- package/md_cg/test_untrusted_audit.py +55 -0
- package/md_cg/test_untrusted_classifier.py +42 -0
- package/md_cg/test_untrusted_enforcement.py +66 -0
- package/md_cg/untrusted.py +45 -0
- package/md_cg/untrusted_audit.py +87 -0
- package/md_cg/value_conflict.py +71 -0
- package/package.json +7 -7
- package/src/hooks.ts +986 -932
- package/src/index.ts +20 -14
- package/src/init.ts +21 -4
- package/src/lib/hook_audit.ts +334 -282
- package/zcode/AGENTS.md +337 -204
package/lib/index.js
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
*
|
|
14
14
|
* 用法(cordis.yml):
|
|
15
15
|
* ```yaml
|
|
16
|
-
* - id:
|
|
16
|
+
* - id: furongjun1999-dsh-memory
|
|
17
17
|
* name: '@furongjun1999/dsh-memory'
|
|
18
18
|
* config:
|
|
19
19
|
* mdcg: # 记忆真源:认知图(md 文档)
|
|
@@ -379,7 +379,7 @@ export async function apply(ctx, config) {
|
|
|
379
379
|
clearInterval(toolsPoll); });
|
|
380
380
|
}
|
|
381
381
|
// 自动记忆:沉淀进 md_cg 认知图(记忆唯一真源)。mdcg=null 时整体停用。
|
|
382
|
-
installMemoryHooks(ctx, mdcg, config.memory);
|
|
382
|
+
disposers.push(installMemoryHooks(ctx, mdcg, config.memory));
|
|
383
383
|
// 角色扮演网页(同源挂载 /roleplay):生成能力走可选「身体」后端(capability),
|
|
384
384
|
// 转录/历史/翻译/落图走大脑(mdcg)。capability=null 时生成接口 fail-closed。
|
|
385
385
|
await installRoleplayWeb(ctx, capability, config, disposers, mdcg);
|
|
@@ -463,11 +463,17 @@ export async function apply(ctx, config) {
|
|
|
463
463
|
// 含子串「同意」,颠倒顺序会把不同意误判为同意(P1 修复,勿回退)。
|
|
464
464
|
// #563③:先查「带否定的同意」(无法同意/不能同意/难以同意/不太同意…)——
|
|
465
465
|
// 这些串里含子串「同意」,只看『不同意』会把否定压成肯定(实测可致 pass)。
|
|
466
|
-
|
|
467
|
-
|
|
466
|
+
// #563③补:白名单枚举不全——「不予同意」「没法同意」均不在原表,且都含子串「同意」
|
|
467
|
+
// ⇒ 否定被压成肯定(实测可致 pass)。改为「否定前缀 + 同意类动词」的组合判据,
|
|
468
|
+
// 覆盖 不/没/未/无法/难以/未能/别/勿/甭 与 同意/认同/赞同/赞成 的笛卡尔积。
|
|
469
|
+
const NEG_AGREE_RE = /(?:不|没|未|无法|难以|未能|别|勿|甭)\s*(?:太|予|能|会|想|可|曾|有|要|够|法)?\s*(?:同意|认同|赞同|赞成)/;
|
|
470
|
+
const NEG_PHRASES = ['不同意', '不赞同', '不通过', '反对', '拒绝'];
|
|
471
|
+
// 肯定判据同步扩到 认同/赞同/赞成——原先只认「同意」子串,「赞同」会被落到 else 判成不同意。
|
|
472
|
+
const POS_AGREE_RE = /同意|认同|赞同|赞成/;
|
|
473
|
+
const conclusion = (NEG_AGREE_RE.test(text) || NEG_PHRASES.some((k) => text.includes(k)))
|
|
468
474
|
? '不同意'
|
|
469
475
|
: text.includes('质疑') ? '质疑'
|
|
470
|
-
:
|
|
476
|
+
: POS_AGREE_RE.test(text) ? '同意' : '不同意';
|
|
471
477
|
return { conclusion, reason: text.slice(0, 120) };
|
|
472
478
|
},
|
|
473
479
|
});
|
package/lib/init.js
CHANGED
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
* 零运行时依赖:只用 Node 内置模块(node:readline/promises / node:fs /
|
|
26
26
|
* node:path)——与全仓「CLI 不加第三方依赖」纪律一致。
|
|
27
27
|
*/
|
|
28
|
-
import { writeFileSync } from 'node:fs';
|
|
28
|
+
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
|
|
29
29
|
import { homedir } from 'node:os';
|
|
30
30
|
import { createInterface } from 'node:readline/promises';
|
|
31
31
|
import { join, resolve } from 'node:path';
|
|
@@ -241,7 +241,31 @@ export async function runInit(argv, opts = {}) {
|
|
|
241
241
|
const snippet = buildSnippet(endSpec, rootAbs, pythonName);
|
|
242
242
|
const snippetJson = `${JSON.stringify(snippet, null, 2)}\n`;
|
|
243
243
|
const snippetPath = join(cwd, SNIPPET_FILENAME);
|
|
244
|
-
|
|
244
|
+
// 覆盖前先判断:用户可能已在本目录手写过同名文件(甚至已把真实 MDCG_TOKEN 填进去)。
|
|
245
|
+
// 无条件 writeFileSync 会把它们连同令牌一起冲掉,而 init 的文案只承诺「重跑输出一致」。
|
|
246
|
+
// 已有且内容不同时:不覆盖,改写旁路文件名,并在输出里说明——宁可多一个文件,不可丢数据。
|
|
247
|
+
// 旁路名**固定不含时间戳**:反复跑 init 只覆盖同一个 -new.json,不随时间线性堆积;
|
|
248
|
+
// 内容与 -new.json 相同时也跳过写入,做到真正幂等(issue #1193)。
|
|
249
|
+
let writtenPath = snippetPath;
|
|
250
|
+
if (existsSync(snippetPath)) {
|
|
251
|
+
let prev = '';
|
|
252
|
+
try {
|
|
253
|
+
prev = readFileSync(snippetPath, 'utf8');
|
|
254
|
+
}
|
|
255
|
+
catch { /* 读不出当作不同,走旁路 */ }
|
|
256
|
+
if (prev !== snippetJson) {
|
|
257
|
+
const alt = join(cwd, SNIPPET_FILENAME.replace(/\.json$/, '-new.json'));
|
|
258
|
+
writtenPath = alt;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
// 幂等:目标文件已是这份内容就不再落盘(避免重复写同一字节串,也保住 mtime)
|
|
262
|
+
let needWrite = true;
|
|
263
|
+
try {
|
|
264
|
+
needWrite = readFileSync(writtenPath, 'utf8') !== snippetJson;
|
|
265
|
+
}
|
|
266
|
+
catch { /* 不存在则写 */ }
|
|
267
|
+
if (needWrite)
|
|
268
|
+
writeFileSync(writtenPath, snippetJson, 'utf8');
|
|
245
269
|
say();
|
|
246
270
|
say(`灵枢(Lingshu)一键配置 · 端=${endSpec.label}`);
|
|
247
271
|
say(` 记忆根目录 MDCG_ROOT:${rootAbs}`);
|
|
@@ -255,7 +279,7 @@ export async function runInit(argv, opts = {}) {
|
|
|
255
279
|
for (const line of tokenCommand(pythonName, endSpec.tokenActor, ''))
|
|
256
280
|
say(line);
|
|
257
281
|
say();
|
|
258
|
-
say(`片段已写入:${
|
|
282
|
+
say(`片段已写入:${writtenPath}(mcpServers 键直接复制即可)`);
|
|
259
283
|
say('提示:PYTHONPATH 当前指向本命令所在包——若是 npx 临时包,建议换成 git clone 的常驻仓库绝对路径(免疫 npx 缓存清理)。');
|
|
260
284
|
say('init 只生成配置:未创建目录、未启动服务、未写库;重跑输出一致。');
|
|
261
285
|
return {
|
|
@@ -263,7 +287,7 @@ export async function runInit(argv, opts = {}) {
|
|
|
263
287
|
endLabel: endSpec.label,
|
|
264
288
|
root: rootAbs,
|
|
265
289
|
python: pythonName,
|
|
266
|
-
snippetPath,
|
|
290
|
+
snippetPath: writtenPath,
|
|
267
291
|
snippet,
|
|
268
292
|
output: collected,
|
|
269
293
|
snippetFile: snippetJson,
|
package/lib/lib/hook_audit.d.ts
CHANGED
|
@@ -43,6 +43,9 @@ export declare class HookAuditRecorder {
|
|
|
43
43
|
/** 实际生效的审计文件路径(构造时定死,供日志/测试核对)。 */
|
|
44
44
|
readonly path: string;
|
|
45
45
|
constructor(path?: string);
|
|
46
|
+
/** #606:收尾时只读最新盘面,返回一行累计计数;缺失/损坏/不可读均静默。
|
|
47
|
+
* written 沿用“成功发起”口径,不能当成成功落库数;不读取 last/kinds 等文本进日志。 */
|
|
48
|
+
summary(): string | null;
|
|
46
49
|
/** 一条**被设计滤除**的消息(三处过滤分支各调一次)。 */
|
|
47
50
|
filtered(reason: HookFilterReason, kind?: string): void;
|
|
48
51
|
/** `source.kind` 分布(每条**进入消息级处理**的事件调一次,含被滤与放行)。 */
|
package/lib/lib/hook_audit.js
CHANGED
|
@@ -85,6 +85,21 @@ function emptyState() {
|
|
|
85
85
|
function countOf(v) {
|
|
86
86
|
return typeof v === 'number' && Number.isFinite(v) && v >= 0 ? Math.floor(v) : 0;
|
|
87
87
|
}
|
|
88
|
+
/** 摘要只接受完整的非负安全整数桶;坏盘面不伪装成零计数。 */
|
|
89
|
+
function sumCounts(bucket, keys) {
|
|
90
|
+
if (!bucket || typeof bucket !== 'object' || Array.isArray(bucket))
|
|
91
|
+
return null;
|
|
92
|
+
let total = 0;
|
|
93
|
+
for (const key of keys) {
|
|
94
|
+
const value = bucket[key];
|
|
95
|
+
if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < 0)
|
|
96
|
+
return null;
|
|
97
|
+
total += value;
|
|
98
|
+
if (!Number.isSafeInteger(total))
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
return total;
|
|
102
|
+
}
|
|
88
103
|
/** 盘面内容 → 状态:逐字段归并,形态不符的字段一律丢弃(不信任盘面内容)。 */
|
|
89
104
|
function mergeState(raw) {
|
|
90
105
|
const s = emptyState();
|
|
@@ -138,9 +153,33 @@ export class HookAuditRecorder {
|
|
|
138
153
|
path;
|
|
139
154
|
/** 进程内状态缓存(首次记录时从盘面载入,此后只增量改内存 + 全量重写)。 */
|
|
140
155
|
#state = null;
|
|
156
|
+
/** 本进程持有的锁 token;null 表示当前未持锁。 */
|
|
157
|
+
#lockToken = null;
|
|
141
158
|
constructor(path) {
|
|
142
159
|
this.path = typeof path === 'string' && path.trim() ? path.trim() : DEFAULT_HOOK_AUDIT_PATH;
|
|
143
160
|
}
|
|
161
|
+
/** #606:收尾时只读最新盘面,返回一行累计计数;缺失/损坏/不可读均静默。
|
|
162
|
+
* written 沿用“成功发起”口径,不能当成成功落库数;不读取 last/kinds 等文本进日志。 */
|
|
163
|
+
summary() {
|
|
164
|
+
try {
|
|
165
|
+
const raw = JSON.parse(readFileSync(this.path, 'utf8'));
|
|
166
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw))
|
|
167
|
+
return null;
|
|
168
|
+
const counts = raw.counts;
|
|
169
|
+
if (!counts || typeof counts !== 'object' || Array.isArray(counts))
|
|
170
|
+
return null;
|
|
171
|
+
const buckets = counts;
|
|
172
|
+
const written = sumCounts(buckets.written, WRITE_ROLES);
|
|
173
|
+
const filtered = sumCounts(buckets.filtered, FILTER_REASONS);
|
|
174
|
+
const skipped = sumCounts(buckets.skipped, SKIP_REASONS);
|
|
175
|
+
if (written === null || filtered === null || skipped === null)
|
|
176
|
+
return null;
|
|
177
|
+
return `dsh-memory: 自动记忆审计(累计)written=${written} filtered=${filtered} skipped=${skipped}`;
|
|
178
|
+
}
|
|
179
|
+
catch {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
144
183
|
/** 一条**被设计滤除**的消息(三处过滤分支各调一次)。 */
|
|
145
184
|
filtered(reason, kind) {
|
|
146
185
|
this.#mutate((s) => {
|
|
@@ -202,10 +241,26 @@ export class HookAuditRecorder {
|
|
|
202
241
|
/** 跨进程排他锁:独占创建 `${path}.lock`;陈旧锁(>5s)清掉;超时返回 false(静默降级)。 */
|
|
203
242
|
#acquireLock(timeoutMs = 800) {
|
|
204
243
|
const lock = `${this.path}.lock`;
|
|
244
|
+
const token = `${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
|
|
205
245
|
const t0 = Date.now();
|
|
206
246
|
for (;;) {
|
|
207
247
|
try {
|
|
208
248
|
closeSync(openSync(lock, 'wx'));
|
|
249
|
+
// #563④补(ABA):把归属 token 写进锁文件,释放时据此判断锁是否仍是自己的。
|
|
250
|
+
// 不加 token 时:A 的锁被 B 判陈旧清掉 → B 建新锁 → A 在 finally 无条件 unlink,
|
|
251
|
+
// 把 B 的锁删掉,互斥窗口重新打开(回到本函数要修的丢计数问题)。
|
|
252
|
+
try {
|
|
253
|
+
writeFileSync(lock, token);
|
|
254
|
+
}
|
|
255
|
+
catch {
|
|
256
|
+
// 写不进归属 token ⇒ 不持有无法验证归属的锁,立即归还(否则 releaseLock 比对失败会漏删)
|
|
257
|
+
try {
|
|
258
|
+
unlinkSync(lock);
|
|
259
|
+
}
|
|
260
|
+
catch { /* 已被他人清理 */ }
|
|
261
|
+
return false;
|
|
262
|
+
}
|
|
263
|
+
this.#lockToken = token;
|
|
209
264
|
return true;
|
|
210
265
|
}
|
|
211
266
|
catch {
|
|
@@ -222,7 +277,15 @@ export class HookAuditRecorder {
|
|
|
222
277
|
}
|
|
223
278
|
}
|
|
224
279
|
#releaseLock() {
|
|
280
|
+
// #563④补(ABA):只删自己的锁。锁可能已被他人判陈旧清掉并重建——
|
|
281
|
+
// 无条件 unlink 会删掉别人的锁,互斥窗口重新打开。
|
|
282
|
+
const token = this.#lockToken;
|
|
283
|
+
this.#lockToken = null;
|
|
284
|
+
if (!token)
|
|
285
|
+
return;
|
|
225
286
|
try {
|
|
287
|
+
if (readFileSync(`${this.path}.lock`, 'utf8') !== token)
|
|
288
|
+
return;
|
|
226
289
|
unlinkSync(`${this.path}.lock`);
|
|
227
290
|
}
|
|
228
291
|
catch { /* 已清理 */ }
|
package/md_cg/autonomy.py
CHANGED
|
@@ -267,7 +267,7 @@ def proposals(cg, window: int = 200, limit: int = 3,
|
|
|
267
267
|
mv = d_meta.compute(cg, window=window) # 每轮一次、循环内复用(禁 N× 扫描)
|
|
268
268
|
bonus = (round(W_DMETA * d_meta.pressure(mv, DMETA_PROXY), 4)
|
|
269
269
|
if mv.get("enabled") else 0.0)
|
|
270
|
-
emotion_on = os.environ.get(EMOTION_ENV) == "1" #
|
|
270
|
+
emotion_on = os.environ.get(EMOTION_ENV) == "1" # 默认开(2026-10-21 裁定);显式设 0/false ⇒ bonus 恒 0
|
|
271
271
|
eps = metacognition.D2_EMOTION # 情绪加分硬上限(复用既有常量)
|
|
272
272
|
for a in agg.values():
|
|
273
273
|
score = (W_D2 * a["d2_abs"] + W_BLINDSPOT * a["blindspot"]
|