@kenz1117/dsh-engram 0.11.2 → 0.11.4
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 +19 -1
- package/lib/client.js +1 -1
- package/lib/client.js.map +1 -1
- package/lib/index.js +339 -138
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -231,6 +231,7 @@ const zh$5 = {
|
|
|
231
231
|
"core.warnHistoryBackfillFailed": "[dsh-engram] 历史回填失败:",
|
|
232
232
|
"core.warnDisposeFinalFailed": "[dsh-engram] 会话结束的收尾处理异常(不影响对话):",
|
|
233
233
|
"core.warnReflectFailed": "[dsh-engram] 会话结束的信念巩固异常({scope},不影响对话):",
|
|
234
|
+
"core.warnReflectBatchDropped": "[dsh-engram] 信念巩固丢弃 {count} 条候选({reason};未被巩固、下次巩固继续取材)",
|
|
234
235
|
"core.warnDecayStartupFailed": "[dsh-engram] 启动衰减调度失败(跳过):",
|
|
235
236
|
"core.warnDecayPeriodicFailed": "[dsh-engram] 周期衰减调度失败(跳过):",
|
|
236
237
|
"core.warnConsolidationFailed": "[dsh-engram] 闭馆整理失败(不影响对话):",
|
|
@@ -287,6 +288,7 @@ const en$5 = {
|
|
|
287
288
|
"core.warnHistoryBackfillFailed": "[dsh-engram] History backfill failed:",
|
|
288
289
|
"core.warnDisposeFinalFailed": "[dsh-engram] Session-end finalisation threw (conversation unaffected):",
|
|
289
290
|
"core.warnReflectFailed": "[dsh-engram] Belief consolidation at session end threw ({scope}, conversation unaffected):",
|
|
291
|
+
"core.warnReflectBatchDropped": "[dsh-engram] Belief consolidation dropped {count} candidates ({reason}; not consolidated, will be picked up next consolidation)",
|
|
290
292
|
"core.warnDecayStartupFailed": "[dsh-engram] Startup decay scheduling failed (skipped):",
|
|
291
293
|
"core.warnDecayPeriodicFailed": "[dsh-engram] Periodic decay scheduling failed (skipped):",
|
|
292
294
|
"core.warnConsolidationFailed": "[dsh-engram] Closing-time consolidation failed (conversation unaffected):",
|
|
@@ -890,6 +892,7 @@ const zh$1 = {
|
|
|
890
892
|
"tool.reflectDone": "信念巩固完成:取材池 {pool} 条,新证据 {candidates} 条、待复核信念 {staleReviewed} 条;新建 {formed},细化 {refined},维持 {confirmed},否定 {refuted},跳过 {skipped}。",
|
|
891
893
|
"tool.reflectNote": "信念是叠加在原始记忆之上的抽象层(证据 id 与证据数已留存,原始记忆未归档)。",
|
|
892
894
|
"tool.reflectConsumed": "同时消费了 {count} 条会话结束时登记的待巩固任务。",
|
|
895
|
+
"tool.reflectDropped": "另有 {count} 条候选因输出预算/解析失败被丢弃(未被巩固,可重跑补做)。",
|
|
893
896
|
"tool.examineDescription": "渐进式披露:按 id 批量拉取记忆完整铭牌(content + 房间 + 状态 + 边关系)。仅在已通过 engram_search/timeline/neighbors 拿到候选 id 后调用,避免一次性吞全文。建议 ≤16 个 id,超出会按入参顺序保留前 N 条。",
|
|
894
897
|
"tool.examineIdsDescription": "记忆 id 列表",
|
|
895
898
|
"tool.examineIdsRequired": "engram_examine: ids 必填且至少含 1 条",
|
|
@@ -1223,6 +1226,7 @@ const en$1 = {
|
|
|
1223
1226
|
"tool.reflectDone": "Belief consolidation complete: a pool of {pool} memories, {candidates} new evidence items, {staleReviewed} beliefs to re-check; {formed} formed, {refined} refined, {confirmed} confirmed, {refuted} refuted, {skipped} skipped.",
|
|
1224
1227
|
"tool.reflectNote": "Beliefs are an abstraction layer stacked on top of the raw memories (evidence ids and counts are kept; the raw memories are not archived).",
|
|
1225
1228
|
"tool.reflectConsumed": "Also consumed {count} consolidation tasks registered at session end.",
|
|
1229
|
+
"tool.reflectDropped": "Additionally, {count} candidates were dropped (output budget/parse failure; not consolidated, rerun to pick up).",
|
|
1226
1230
|
"tool.examineDescription": "Progressive disclosure: fetch the complete nameplates of memories by id in a batch (content + room + status + edges). Call it only after you already have candidate ids from engram_search/timeline/neighbors, so you do not swallow full texts at once. At most 16 ids are advised; extras keep the first N in the order given.",
|
|
1227
1231
|
"tool.examineIdsDescription": "list of memory ids",
|
|
1228
1232
|
"tool.examineIdsRequired": "engram_examine: ids is required and must hold at least 1 id",
|
|
@@ -5838,12 +5842,23 @@ function finishError(finish) {
|
|
|
5838
5842
|
error.code = finish.failure.code;
|
|
5839
5843
|
return error;
|
|
5840
5844
|
}
|
|
5841
|
-
case "max-tokens":
|
|
5845
|
+
case "max-tokens": {
|
|
5846
|
+
const error = new Error(message("llm.finishMaxTokens"));
|
|
5847
|
+
error.code = "LLM_MAX_TOKENS";
|
|
5848
|
+
return error;
|
|
5849
|
+
}
|
|
5842
5850
|
case "tool-calls": return new Error(message("llm.finishUnexpectedToolCalls"));
|
|
5843
5851
|
default: return new Error(message("llm.finishUnsupported", { kind: String(finish.kind) }));
|
|
5844
5852
|
}
|
|
5845
5853
|
}
|
|
5846
5854
|
/**
|
|
5855
|
+
* 判别 max-tokens 终止错误(finishError 打上 LLM_MAX_TOKENS code)。摄取分片重试与
|
|
5856
|
+
* 信念巩固分片重试共用(#42 附带 / #45)。
|
|
5857
|
+
*/
|
|
5858
|
+
function isMaxTokensError(error) {
|
|
5859
|
+
return error?.code === "LLM_MAX_TOKENS";
|
|
5860
|
+
}
|
|
5861
|
+
/**
|
|
5847
5862
|
* 一次辅助 LLM 调用:流式收集文本输出。
|
|
5848
5863
|
* @param ctx - 提供 llm 服务的上下文。
|
|
5849
5864
|
* @param params.route - 模型路由(来自日志解析或显式配置)。
|
|
@@ -6341,6 +6356,47 @@ const MODE_LIMITS = {
|
|
|
6341
6356
|
/** 摄取输出 token 上限。completion 预算含 reasoning_tokens:推理模型可能把预算耗在思考上、
|
|
6342
6357
|
答案为空(实测 600/2000 都会出现,4000 未复现),上限给足余量而非按答案长度估。 */
|
|
6343
6358
|
const INGEST_MAX_TOKENS = 4e3;
|
|
6359
|
+
/** 提取切块的最小文本粒度(字符):切到低于此长度仍超输出预算的块直接放弃(贡献 0 条候选)。
|
|
6360
|
+
正常文本到这个量级输出预算绰绰有余;触发即说明模型输出退化(复读死循环),再切只是空烧。 */
|
|
6361
|
+
const EXTRACT_MIN_CHUNK = 80;
|
|
6362
|
+
/** 单块提取的解析失败(含空答案重试后仍失败)——携带原始输出,交上层按 reason 分类报告。 */
|
|
6363
|
+
var ExtractFailure = class extends Error {
|
|
6364
|
+
raw;
|
|
6365
|
+
constructor(raw) {
|
|
6366
|
+
super("extract-parse-failed");
|
|
6367
|
+
this.raw = raw;
|
|
6368
|
+
this.name = "ExtractFailure";
|
|
6369
|
+
}
|
|
6370
|
+
};
|
|
6371
|
+
/**
|
|
6372
|
+
* 提取候选,输出超预算(max-tokens)时对半切块重试(#42 附带报告:长转写/数百行日志的
|
|
6373
|
+
* 轮次提取输出必然超 4000 输出预算,且同样输入重跑永远失败——回填轮次被永久卡死)。
|
|
6374
|
+
* 切块让这类轮次自愈:多段文本对半分;单段文本按换行边界对半切;切到最小粒度仍超限的
|
|
6375
|
+
* 块放弃(贡献 0 条候选),其余块照常提取,整轮照常收口为 done 而非永久 pending。
|
|
6376
|
+
* 语义边界:只有 max-tokens 触发切块;解析失败(ExtractFailure)与瞬时错误原样上抛,
|
|
6377
|
+
* 轮次保持「可重试」状态,与旧行为一致。
|
|
6378
|
+
*/
|
|
6379
|
+
async function extractChunked(callOnce, texts) {
|
|
6380
|
+
try {
|
|
6381
|
+
return await callOnce(texts);
|
|
6382
|
+
} catch (error) {
|
|
6383
|
+
if (!isMaxTokensError(error)) throw error;
|
|
6384
|
+
if (texts.length > 1) {
|
|
6385
|
+
const mid = Math.ceil(texts.length / 2);
|
|
6386
|
+
const first = await extractChunked(callOnce, texts.slice(0, mid));
|
|
6387
|
+
const second = await extractChunked(callOnce, texts.slice(mid));
|
|
6388
|
+
return [...first, ...second];
|
|
6389
|
+
}
|
|
6390
|
+
const text = texts[0] ?? "";
|
|
6391
|
+
if (text.length < EXTRACT_MIN_CHUNK) return [];
|
|
6392
|
+
const half = Math.floor(text.length / 2);
|
|
6393
|
+
const positions = [text.lastIndexOf("\n", half), text.indexOf("\n", half)].filter((position) => position !== -1);
|
|
6394
|
+
const cut = positions.length === 0 ? half : positions.reduce((best, position) => Math.abs(position - half) < Math.abs(best - half) ? position : best);
|
|
6395
|
+
const first = await extractChunked(callOnce, [text.slice(0, cut)]);
|
|
6396
|
+
const second = await extractChunked(callOnce, [text.slice(cut + 1)]);
|
|
6397
|
+
return [...first, ...second];
|
|
6398
|
+
}
|
|
6399
|
+
}
|
|
6344
6400
|
/** op_log 幂等键 op:该 (sessionId, turn) 已完成摄取。 */
|
|
6345
6401
|
const INGEST_DONE_OP = "ingest-done";
|
|
6346
6402
|
/** op_log pending 键 op:disposed 末轮摄取失败/超时,待下次会话重放补做。 */
|
|
@@ -6706,43 +6762,54 @@ async function ingestPreviousTurn(deps) {
|
|
|
6706
6762
|
};
|
|
6707
6763
|
}
|
|
6708
6764
|
const recallNote = hasRecallToolCalls(scoped) ? message("llm.ingestRecallNote") : "";
|
|
6709
|
-
const
|
|
6710
|
-
|
|
6711
|
-
|
|
6712
|
-
|
|
6713
|
-
|
|
6714
|
-
|
|
6715
|
-
|
|
6716
|
-
|
|
6717
|
-
|
|
6718
|
-
|
|
6719
|
-
system: ingestSystemPrompt(),
|
|
6720
|
-
userText,
|
|
6721
|
-
maxTokens: INGEST_MAX_TOKENS,
|
|
6722
|
-
purpose: "engram-ingest",
|
|
6723
|
-
signal: deps.signal
|
|
6724
|
-
});
|
|
6725
|
-
let parsed = parseJsonArray(raw);
|
|
6726
|
-
if (parsed === void 0 && raw.trim() === "") {
|
|
6727
|
-
raw = await deps.call({
|
|
6765
|
+
const callExtract = async (texts) => {
|
|
6766
|
+
const userText = `${message("llm.ingestUserPrompt")}\n${JSON.stringify(texts)}${recallNote === "" ? "" : `\n${recallNote}`}`;
|
|
6767
|
+
deps.logRequest({
|
|
6768
|
+
route,
|
|
6769
|
+
round,
|
|
6770
|
+
userText,
|
|
6771
|
+
maxTokens: INGEST_MAX_TOKENS,
|
|
6772
|
+
mode: deps.mode
|
|
6773
|
+
});
|
|
6774
|
+
const args = {
|
|
6728
6775
|
route,
|
|
6729
6776
|
system: ingestSystemPrompt(),
|
|
6730
6777
|
userText,
|
|
6731
6778
|
maxTokens: INGEST_MAX_TOKENS,
|
|
6732
6779
|
purpose: "engram-ingest",
|
|
6733
6780
|
signal: deps.signal
|
|
6734
|
-
}
|
|
6735
|
-
|
|
6736
|
-
|
|
6737
|
-
|
|
6738
|
-
|
|
6739
|
-
|
|
6740
|
-
|
|
6741
|
-
|
|
6742
|
-
|
|
6743
|
-
|
|
6744
|
-
|
|
6781
|
+
};
|
|
6782
|
+
let raw = await deps.call(args);
|
|
6783
|
+
let parsed = parseJsonArray(raw);
|
|
6784
|
+
if (parsed === void 0 && raw.trim() === "") {
|
|
6785
|
+
deps.logRequest({
|
|
6786
|
+
route,
|
|
6787
|
+
round,
|
|
6788
|
+
userText,
|
|
6789
|
+
maxTokens: INGEST_MAX_TOKENS,
|
|
6790
|
+
mode: deps.mode
|
|
6791
|
+
});
|
|
6792
|
+
raw = await deps.call(args);
|
|
6793
|
+
parsed = parseJsonArray(raw);
|
|
6794
|
+
}
|
|
6795
|
+
if (parsed === void 0) throw new ExtractFailure(raw);
|
|
6796
|
+
return parsed;
|
|
6745
6797
|
};
|
|
6798
|
+
let parsed;
|
|
6799
|
+
try {
|
|
6800
|
+
parsed = await extractChunked(callExtract, cleaned);
|
|
6801
|
+
} catch (error) {
|
|
6802
|
+
if (!(error instanceof ExtractFailure)) throw error;
|
|
6803
|
+
return {
|
|
6804
|
+
scannedEvents: slice.length,
|
|
6805
|
+
candidates: 0,
|
|
6806
|
+
written: 0,
|
|
6807
|
+
merged: 0,
|
|
6808
|
+
deferred: 0,
|
|
6809
|
+
dropped: 0,
|
|
6810
|
+
skipped: error.raw.trim() === "" ? "empty-answer" : "unparseable-output"
|
|
6811
|
+
};
|
|
6812
|
+
}
|
|
6746
6813
|
const embedder = await deps.embedder;
|
|
6747
6814
|
const judge = deps.resolveJudge?.();
|
|
6748
6815
|
const writtenContents = [];
|
|
@@ -7139,8 +7206,8 @@ async function selectSessions(deps, rules) {
|
|
|
7139
7206
|
const cwd = header.cwd;
|
|
7140
7207
|
if (cwd === void 0 || cwd === "") return {
|
|
7141
7208
|
header,
|
|
7142
|
-
events,
|
|
7143
7209
|
turns,
|
|
7210
|
+
loadEvents: () => loadSession(source, header.id),
|
|
7144
7211
|
openStore: deps.openUserStore,
|
|
7145
7212
|
routing: {
|
|
7146
7213
|
writeScope: "user",
|
|
@@ -7150,8 +7217,8 @@ async function selectSessions(deps, rules) {
|
|
|
7150
7217
|
const projectCwd = cwd;
|
|
7151
7218
|
return {
|
|
7152
7219
|
header,
|
|
7153
|
-
events,
|
|
7154
7220
|
turns,
|
|
7221
|
+
loadEvents: () => loadSession(source, header.id),
|
|
7155
7222
|
openStore: () => deps.resolveStore(projectCwd),
|
|
7156
7223
|
routing: ingestWriteRouting(projectCwd, deps.resolveStore, deps.openUserStore)
|
|
7157
7224
|
};
|
|
@@ -7267,11 +7334,23 @@ async function runHistoryBackfill(deps, defaults, overrides, onProgress, signal)
|
|
|
7267
7334
|
onProgress({ ...progress });
|
|
7268
7335
|
for (const candidate of candidates) {
|
|
7269
7336
|
if (signal.aborted) break;
|
|
7337
|
+
const events = await candidate.loadEvents();
|
|
7338
|
+
if (events === void 0) {
|
|
7339
|
+
progress.turnsSkipped += candidate.turns.length;
|
|
7340
|
+
progress.skipReasons["session-unreadable"] = (progress.skipReasons["session-unreadable"] ?? 0) + candidate.turns.length;
|
|
7341
|
+
progress.turnsDone += candidate.turns.length;
|
|
7342
|
+
progress.sessionsDone += 1;
|
|
7343
|
+
onProgress({
|
|
7344
|
+
...progress,
|
|
7345
|
+
currentSession: candidate.header.id
|
|
7346
|
+
});
|
|
7347
|
+
continue;
|
|
7348
|
+
}
|
|
7270
7349
|
for (const turn of candidate.turns) {
|
|
7271
7350
|
if (signal.aborted) break;
|
|
7272
7351
|
try {
|
|
7273
7352
|
const outcome = await ingestPreviousTurn({
|
|
7274
|
-
events
|
|
7353
|
+
events,
|
|
7275
7354
|
sessionId: candidate.header.id,
|
|
7276
7355
|
turn,
|
|
7277
7356
|
slice: turn,
|
|
@@ -7308,7 +7387,7 @@ async function runHistoryBackfill(deps, defaults, overrides, onProgress, signal)
|
|
|
7308
7387
|
});
|
|
7309
7388
|
}
|
|
7310
7389
|
await ensureSessionSummary({
|
|
7311
|
-
events
|
|
7390
|
+
events,
|
|
7312
7391
|
sessionId: candidate.header.id,
|
|
7313
7392
|
openStore: () => candidate.openStore(),
|
|
7314
7393
|
call: deps.call,
|
|
@@ -7432,17 +7511,18 @@ async function runConsolidation(store, embedder, opts = {}) {
|
|
|
7432
7511
|
const key = record.content.slice(0, 60);
|
|
7433
7512
|
const existing = seen.get(key);
|
|
7434
7513
|
if (existing === void 0) seen.set(key, record);
|
|
7435
|
-
else if (record.importance > existing.importance) {
|
|
7436
|
-
await store.supersedeMany({
|
|
7514
|
+
else if (record.importance > existing.importance) try {
|
|
7515
|
+
const survivor = await store.supersedeMany({
|
|
7437
7516
|
scope,
|
|
7438
7517
|
kind: record.kind,
|
|
7439
7518
|
content: record.content,
|
|
7440
7519
|
importance: record.importance,
|
|
7441
7520
|
confidence: record.confidence
|
|
7442
|
-
}, [existing.id])
|
|
7443
|
-
skipped += 1;
|
|
7444
|
-
});
|
|
7521
|
+
}, [existing.id, record.id]);
|
|
7445
7522
|
merged += 1;
|
|
7523
|
+
seen.set(key, survivor);
|
|
7524
|
+
} catch {
|
|
7525
|
+
skipped += 1;
|
|
7446
7526
|
seen.set(key, record);
|
|
7447
7527
|
}
|
|
7448
7528
|
}
|
|
@@ -7481,21 +7561,23 @@ async function runConsolidation(store, embedder, opts = {}) {
|
|
|
7481
7561
|
if (bVec === void 0) continue;
|
|
7482
7562
|
if (cosine(vectors.get(a.id), bVec) >= options.mergeThreshold) dupes.push(b);
|
|
7483
7563
|
}
|
|
7484
|
-
if (dupes.length
|
|
7485
|
-
|
|
7486
|
-
|
|
7487
|
-
|
|
7488
|
-
|
|
7489
|
-
|
|
7490
|
-
|
|
7491
|
-
|
|
7492
|
-
|
|
7493
|
-
|
|
7494
|
-
|
|
7495
|
-
|
|
7496
|
-
|
|
7497
|
-
|
|
7498
|
-
|
|
7564
|
+
if (dupes.length > 0) {
|
|
7565
|
+
const ids = [...dupes.map((record) => record.id), a.id];
|
|
7566
|
+
const embedding = vectors.get(a.id);
|
|
7567
|
+
try {
|
|
7568
|
+
await store.supersedeMany({
|
|
7569
|
+
scope,
|
|
7570
|
+
kind: a.kind,
|
|
7571
|
+
content: a.content,
|
|
7572
|
+
importance: Math.max(a.importance, ...dupes.map((record) => record.importance)),
|
|
7573
|
+
confidence: a.confidence,
|
|
7574
|
+
...embedding === void 0 ? {} : { embedding }
|
|
7575
|
+
}, ids);
|
|
7576
|
+
for (const id of ids) visited.add(id);
|
|
7577
|
+
merged += 1;
|
|
7578
|
+
} catch {
|
|
7579
|
+
skipped += 1;
|
|
7580
|
+
}
|
|
7499
7581
|
}
|
|
7500
7582
|
}
|
|
7501
7583
|
}
|
|
@@ -7537,14 +7619,21 @@ async function runConsolidation(store, embedder, opts = {}) {
|
|
|
7537
7619
|
const REFLECT_POOL = 300;
|
|
7538
7620
|
/** 单次巩固喂给 LLM 的新证据条数上限(协议内常量:单次辅助调用的可控上下文)。 */
|
|
7539
7621
|
const REFLECT_MAX_INPUT = 30;
|
|
7622
|
+
/** 单次 LLM 调用的候选分片大小(#45:30 条候选一次调用必然超输出预算整批丢弃;分片后单片失败只丢该片)。 */
|
|
7623
|
+
const REFLECT_CHUNK = 10;
|
|
7624
|
+
/** 候选原文截断字符数(#45:与 stale 证据的 REFLECT_EVIDENCE_SLICE 同纪律——候选此前整篇进 prompt)。 */
|
|
7625
|
+
const REFLECT_CANDIDATE_SLICE = 200;
|
|
7540
7626
|
/** 单次喂给 LLM 的待复核 stale 信念上限。 */
|
|
7541
7627
|
const REFLECT_MAX_STALE = 5;
|
|
7542
7628
|
/** 单条 stale 信念附带的证据原文条数上限。 */
|
|
7543
7629
|
const REFLECT_STALE_EVIDENCE_LIMIT = 6;
|
|
7544
7630
|
/** stale 证据原文截断字符数(控制总上下文)。 */
|
|
7545
7631
|
const REFLECT_EVIDENCE_SLICE = 120;
|
|
7546
|
-
/**
|
|
7547
|
-
|
|
7632
|
+
/** 输出预算按批量缩放:底价 + 单动作配额,封顶 8000(#45 实测 10 动作需 ~8000;推理模型的
|
|
7633
|
+
reasoning_tokens 计入 completion 预算,固定 900 对 30 动作必然截断)。 */
|
|
7634
|
+
const REFLECT_TOKENS_BASE = 400;
|
|
7635
|
+
const REFLECT_TOKENS_PER_ACTION = 500;
|
|
7636
|
+
const REFLECT_TOKENS_CAP = 8e3;
|
|
7548
7637
|
/** 信念向量近邻归并阈值:新信念与既有信念余弦达到该值则并入同一条(持续细化)。 */
|
|
7549
7638
|
const REFLECT_MERGE_THRESHOLD = .9;
|
|
7550
7639
|
/** 新鲜度阈值:新证据与既有信念余弦达到该值(低于归并阈值,语义更宽)则把信念标 stale 待复核。 */
|
|
@@ -7561,6 +7650,27 @@ function reflectSystemPrompt() {
|
|
|
7561
7650
|
});
|
|
7562
7651
|
}
|
|
7563
7652
|
/**
|
|
7653
|
+
* 巩固输出预算:底价 + 按本片动作数缩放,封顶 REFLECT_TOKENS_CAP。#45 实测 10 动作需
|
|
7654
|
+
* ~8000 tokens;推理模型的 reasoning_tokens 计入 completion 预算,固定小预算对大批必然
|
|
7655
|
+
* 截断——预算必须随喂入量走,而不是拍一个常数。
|
|
7656
|
+
*/
|
|
7657
|
+
function reflectMaxTokens(actionCount) {
|
|
7658
|
+
return Math.min(REFLECT_TOKENS_CAP, REFLECT_TOKENS_BASE + REFLECT_TOKENS_PER_ACTION * actionCount);
|
|
7659
|
+
}
|
|
7660
|
+
/**
|
|
7661
|
+
* 单片巩固的确定性失败(#45 分片自愈的内部信号):max-tokens 由调用层对半递归重试,
|
|
7662
|
+
* unparseable 由调用层把该片候选计入 dropped。网络等瞬时错误不用它承载、原样上抛
|
|
7663
|
+
* (整批失败仍由 reflectObservations 抛给调用层,由调用层决定是否落 pending)。
|
|
7664
|
+
*/
|
|
7665
|
+
var ReflectBatchFailure = class extends Error {
|
|
7666
|
+
reason;
|
|
7667
|
+
constructor(reason) {
|
|
7668
|
+
super("reflect-batch-failure");
|
|
7669
|
+
this.reason = reason;
|
|
7670
|
+
this.name = "ReflectBatchFailure";
|
|
7671
|
+
}
|
|
7672
|
+
};
|
|
7673
|
+
/**
|
|
7564
7674
|
* 执行一次信念巩固。纯异步:LLM/解析失败抛错(调用层决定是否落 pending),
|
|
7565
7675
|
* 单条动作的写入失败计入 skipped 不中断整批。
|
|
7566
7676
|
*/
|
|
@@ -7573,7 +7683,8 @@ async function reflectObservations(deps) {
|
|
|
7573
7683
|
refined: 0,
|
|
7574
7684
|
confirmed: 0,
|
|
7575
7685
|
refuted: 0,
|
|
7576
|
-
skipped: 0
|
|
7686
|
+
skipped: 0,
|
|
7687
|
+
dropped: 0
|
|
7577
7688
|
};
|
|
7578
7689
|
const pool = await deps.store.topActive(deps.scope, REFLECT_POOL);
|
|
7579
7690
|
outcome.pool = pool.length;
|
|
@@ -7603,86 +7714,127 @@ async function reflectObservations(deps) {
|
|
|
7603
7714
|
outcome.candidates = candidates.length;
|
|
7604
7715
|
outcome.staleReviewed = stale.length;
|
|
7605
7716
|
if (candidates.length === 0 && stale.length === 0) return outcome;
|
|
7606
|
-
const candidateIds = new Set(candidates.map((record) => String(record.id)));
|
|
7607
7717
|
const staleIds = new Set(stale.map((observation) => String(observation.id)));
|
|
7608
|
-
|
|
7609
|
-
|
|
7610
|
-
|
|
7611
|
-
|
|
7612
|
-
|
|
7613
|
-
|
|
7614
|
-
const
|
|
7615
|
-
|
|
7616
|
-
|
|
7617
|
-
|
|
7618
|
-
|
|
7619
|
-
count: String(observation.proofCount),
|
|
7620
|
-
belief: observation.belief
|
|
7621
|
-
})}\n${evidence}`);
|
|
7718
|
+
/**
|
|
7719
|
+
* 单片巩固:组装(候选原文截断)→调用(预算按批量缩放)→解析→逐动作落库。
|
|
7720
|
+
* 输出超预算抛 ReflectBatchFailure('max-tokens') 供上层对半递归;解析失败抛
|
|
7721
|
+
* ReflectBatchFailure('unparseable') 由上层计入 dropped;网络等错误原样上抛。
|
|
7722
|
+
*/
|
|
7723
|
+
const applySlice = async (slice, includeStale) => {
|
|
7724
|
+
const sliceIds = new Set(slice.map((record) => String(record.id)));
|
|
7725
|
+
const sections = [];
|
|
7726
|
+
if (slice.length > 0) {
|
|
7727
|
+
const lines = slice.map((record) => `[${String(record.id)}] (${record.kind}, importance ${record.importance.toFixed(2)}) ${record.content.slice(0, REFLECT_CANDIDATE_SLICE)}`);
|
|
7728
|
+
sections.push(`${message("llm.reflectMarkerNewMemories")}\n${lines.join("\n")}`);
|
|
7622
7729
|
}
|
|
7623
|
-
|
|
7624
|
-
|
|
7625
|
-
|
|
7626
|
-
|
|
7627
|
-
|
|
7628
|
-
|
|
7629
|
-
|
|
7630
|
-
|
|
7631
|
-
|
|
7632
|
-
|
|
7633
|
-
|
|
7634
|
-
|
|
7635
|
-
|
|
7636
|
-
|
|
7637
|
-
|
|
7638
|
-
|
|
7639
|
-
|
|
7640
|
-
|
|
7641
|
-
|
|
7642
|
-
|
|
7643
|
-
|
|
7730
|
+
if (includeStale && stale.length > 0) {
|
|
7731
|
+
const blocks = [];
|
|
7732
|
+
for (const observation of stale) {
|
|
7733
|
+
const evidence = (await deps.store.getMany(observation.sourceIds.slice(0, REFLECT_STALE_EVIDENCE_LIMIT))).map((record) => ` - ${record.content.slice(0, REFLECT_EVIDENCE_SLICE)}`).join("\n");
|
|
7734
|
+
blocks.push(`${message("llm.reflectStaleLine", {
|
|
7735
|
+
id: String(observation.id),
|
|
7736
|
+
count: String(observation.proofCount),
|
|
7737
|
+
belief: observation.belief
|
|
7738
|
+
})}\n${evidence}`);
|
|
7739
|
+
}
|
|
7740
|
+
sections.push(`${message("llm.reflectMarkerBeliefs")}\n${blocks.join("\n")}`);
|
|
7741
|
+
}
|
|
7742
|
+
const userText = sections.join("\n\n");
|
|
7743
|
+
const maxTokens = reflectMaxTokens(slice.length + (includeStale ? stale.length : 0));
|
|
7744
|
+
deps.logRequest({
|
|
7745
|
+
route: deps.route,
|
|
7746
|
+
scope: deps.scope,
|
|
7747
|
+
candidateCount: slice.length,
|
|
7748
|
+
staleCount: includeStale ? stale.length : 0,
|
|
7749
|
+
maxTokens
|
|
7750
|
+
});
|
|
7751
|
+
let raw;
|
|
7644
7752
|
try {
|
|
7645
|
-
|
|
7646
|
-
|
|
7647
|
-
|
|
7648
|
-
|
|
7649
|
-
|
|
7650
|
-
|
|
7651
|
-
|
|
7652
|
-
|
|
7653
|
-
|
|
7654
|
-
|
|
7655
|
-
|
|
7656
|
-
continue;
|
|
7657
|
-
}
|
|
7658
|
-
const sourceIds = requireCandidateIds(action.sourceIds, candidateIds);
|
|
7659
|
-
if (sourceIds === void 0) {
|
|
7660
|
-
outcome.skipped += 1;
|
|
7661
|
-
continue;
|
|
7662
|
-
}
|
|
7663
|
-
await refineWithEmbedding(deps, observationId, action.belief.trim(), sourceIds);
|
|
7664
|
-
outcome.refined += 1;
|
|
7665
|
-
} else if (action.action === "still") {
|
|
7666
|
-
const observationId = requireObservationId(action.observationId, staleIds);
|
|
7667
|
-
if (observationId === void 0) {
|
|
7668
|
-
outcome.skipped += 1;
|
|
7669
|
-
continue;
|
|
7670
|
-
}
|
|
7671
|
-
await deps.store.confirmObservation(observationId);
|
|
7672
|
-
outcome.confirmed += 1;
|
|
7673
|
-
} else if (action.action === "refute") {
|
|
7674
|
-
const observationId = requireObservationId(action.observationId, staleIds);
|
|
7675
|
-
if (observationId === void 0) {
|
|
7676
|
-
outcome.skipped += 1;
|
|
7677
|
-
continue;
|
|
7678
|
-
}
|
|
7679
|
-
await deps.store.refuteObservation(observationId);
|
|
7680
|
-
outcome.refuted += 1;
|
|
7681
|
-
} else outcome.skipped += 1;
|
|
7682
|
-
} catch {
|
|
7683
|
-
outcome.skipped += 1;
|
|
7753
|
+
raw = await deps.call({
|
|
7754
|
+
route: deps.route,
|
|
7755
|
+
system: reflectSystemPrompt(),
|
|
7756
|
+
userText,
|
|
7757
|
+
maxTokens,
|
|
7758
|
+
purpose: "engram-reflect",
|
|
7759
|
+
signal: deps.signal
|
|
7760
|
+
});
|
|
7761
|
+
} catch (error) {
|
|
7762
|
+
if (isMaxTokensError(error)) throw new ReflectBatchFailure("max-tokens");
|
|
7763
|
+
throw error;
|
|
7684
7764
|
}
|
|
7685
|
-
|
|
7765
|
+
const parsed = parseJsonArray(raw);
|
|
7766
|
+
if (parsed === void 0) throw new ReflectBatchFailure("unparseable");
|
|
7767
|
+
for (const item of parsed) {
|
|
7768
|
+
const action = item;
|
|
7769
|
+
try {
|
|
7770
|
+
if (action.action === "form") {
|
|
7771
|
+
if (await applyForm(deps, action, sliceIds) === "refined") outcome.refined += 1;
|
|
7772
|
+
else outcome.formed += 1;
|
|
7773
|
+
} else if (action.action === "refine") {
|
|
7774
|
+
const observationId = requireObservationId(action.observationId, staleIds);
|
|
7775
|
+
if (observationId === void 0) {
|
|
7776
|
+
outcome.skipped += 1;
|
|
7777
|
+
continue;
|
|
7778
|
+
}
|
|
7779
|
+
if (typeof action.belief !== "string" || action.belief.trim() === "") {
|
|
7780
|
+
outcome.skipped += 1;
|
|
7781
|
+
continue;
|
|
7782
|
+
}
|
|
7783
|
+
const sourceIds = requireCandidateIds(action.sourceIds, sliceIds);
|
|
7784
|
+
if (sourceIds === void 0) {
|
|
7785
|
+
outcome.skipped += 1;
|
|
7786
|
+
continue;
|
|
7787
|
+
}
|
|
7788
|
+
await refineWithEmbedding(deps, observationId, action.belief.trim(), sourceIds);
|
|
7789
|
+
outcome.refined += 1;
|
|
7790
|
+
} else if (action.action === "still") {
|
|
7791
|
+
const observationId = requireObservationId(action.observationId, staleIds);
|
|
7792
|
+
if (observationId === void 0) {
|
|
7793
|
+
outcome.skipped += 1;
|
|
7794
|
+
continue;
|
|
7795
|
+
}
|
|
7796
|
+
await deps.store.confirmObservation(observationId);
|
|
7797
|
+
outcome.confirmed += 1;
|
|
7798
|
+
} else if (action.action === "refute") {
|
|
7799
|
+
const observationId = requireObservationId(action.observationId, staleIds);
|
|
7800
|
+
if (observationId === void 0) {
|
|
7801
|
+
outcome.skipped += 1;
|
|
7802
|
+
continue;
|
|
7803
|
+
}
|
|
7804
|
+
await deps.store.refuteObservation(observationId);
|
|
7805
|
+
outcome.refuted += 1;
|
|
7806
|
+
} else outcome.skipped += 1;
|
|
7807
|
+
} catch {
|
|
7808
|
+
outcome.skipped += 1;
|
|
7809
|
+
}
|
|
7810
|
+
}
|
|
7811
|
+
};
|
|
7812
|
+
/**
|
|
7813
|
+
* 单片失败自愈(#45):输出超预算对半递归重试(两半各自独立调用——合回同一请求体积
|
|
7814
|
+
* 几乎不减、必然再超限);切到单片仍超限则该候选计入 dropped(未被巩固、无信念引用,
|
|
7815
|
+
* 下次巩固仍会取材),不再空烧;解析失败直接丢弃该片;其余错误原样上抛。
|
|
7816
|
+
*/
|
|
7817
|
+
const runSlice = async (slice, includeStale) => {
|
|
7818
|
+
try {
|
|
7819
|
+
await applySlice(slice, includeStale);
|
|
7820
|
+
return;
|
|
7821
|
+
} catch (error) {
|
|
7822
|
+
if (!(error instanceof ReflectBatchFailure)) throw error;
|
|
7823
|
+
if (error.reason === "unparseable") {
|
|
7824
|
+
outcome.dropped += slice.length;
|
|
7825
|
+
return;
|
|
7826
|
+
}
|
|
7827
|
+
}
|
|
7828
|
+
if (slice.length <= 1) {
|
|
7829
|
+
outcome.dropped += slice.length;
|
|
7830
|
+
return;
|
|
7831
|
+
}
|
|
7832
|
+
const mid = Math.ceil(slice.length / 2);
|
|
7833
|
+
await runSlice(slice.slice(0, mid), includeStale);
|
|
7834
|
+
await runSlice(slice.slice(mid), false);
|
|
7835
|
+
};
|
|
7836
|
+
if (candidates.length === 0) await runSlice([], true);
|
|
7837
|
+
for (let start = 0; start < candidates.length; start += REFLECT_CHUNK) await runSlice(candidates.slice(start, start + REFLECT_CHUNK), start === 0);
|
|
7686
7838
|
try {
|
|
7687
7839
|
await deps.store.audit("reflect", "AUX", JSON.stringify({
|
|
7688
7840
|
scope: deps.scope,
|
|
@@ -7693,7 +7845,8 @@ async function reflectObservations(deps) {
|
|
|
7693
7845
|
refined: outcome.refined,
|
|
7694
7846
|
confirmed: outcome.confirmed,
|
|
7695
7847
|
refuted: outcome.refuted,
|
|
7696
|
-
skipped: outcome.skipped
|
|
7848
|
+
skipped: outcome.skipped,
|
|
7849
|
+
dropped: outcome.dropped
|
|
7697
7850
|
}));
|
|
7698
7851
|
} catch {}
|
|
7699
7852
|
return outcome;
|
|
@@ -8124,6 +8277,35 @@ async function readJsonBody(req, limit = BODY_LIMIT_BYTES) {
|
|
|
8124
8277
|
//#endregion
|
|
8125
8278
|
//#region src/jev/client.ts
|
|
8126
8279
|
/**
|
|
8280
|
+
* HTTP-клиент модели Jev (System One): задаёт пачке «Noul»-вопросов (да/нет) по
|
|
8281
|
+
* одному тексту состояния и получает вероятности. Формат запроса — JSON вида
|
|
8282
|
+
* state/questions (POST на адрес соединения), он не совместим с OpenAI-chat,
|
|
8283
|
+
* поэтому роутинг через хостовый `ctx.llm` невозможен и клиент остаётся свой.
|
|
8284
|
+
*
|
|
8285
|
+
* Реквизиты соединения (адрес, ключ, модель) клиент не хранит и не читает сам:
|
|
8286
|
+
* их собирает мост `./host.ts` из данных хоста DSH — так провайдер и токен
|
|
8287
|
+
* вводятся ровно в одном месте, у хоста.
|
|
8288
|
+
*
|
|
8289
|
+
* Сетевой сбой, не-2xx, отсутствие полей в ответе — всё бросает ошибку наверх,
|
|
8290
|
+
* а вызывающий по каждой точке молча откатывается к чистым правилам.
|
|
8291
|
+
* @module @kenz1117/dsh-engram/jev/client
|
|
8292
|
+
*/
|
|
8293
|
+
/**
|
|
8294
|
+
* opencode.ai 系端点的稳定会话头(#46):Go 通道对缺该头的 System One 请求一律
|
|
8295
|
+
* 400 MissingSessionID。进程内生成一次、全程稳定(opencode 文档要求「每次对话带
|
|
8296
|
+
* 稳定的 x-opencode-session」;插件即一个客户端)。
|
|
8297
|
+
*/
|
|
8298
|
+
const OPENCODE_SESSION_ID = randomUUID();
|
|
8299
|
+
/** 端点是否属于 opencode.ai(该家要求 x-opencode-session;别家不发多余的头)。 */
|
|
8300
|
+
function isOpencodeEndpoint(endpoint) {
|
|
8301
|
+
try {
|
|
8302
|
+
const host = new URL(endpoint).hostname;
|
|
8303
|
+
return host === "opencode.ai" || host.endsWith(".opencode.ai");
|
|
8304
|
+
} catch {
|
|
8305
|
+
return false;
|
|
8306
|
+
}
|
|
8307
|
+
}
|
|
8308
|
+
/**
|
|
8127
8309
|
* Отказ вызова Jev с разобранной причиной.
|
|
8128
8310
|
*
|
|
8129
8311
|
* Класс, а не строка: панель показывает не только текст ошибки, но и вид отказа и
|
|
@@ -8142,6 +8324,20 @@ var JevCallError = class extends Error {
|
|
|
8142
8324
|
}
|
|
8143
8325
|
};
|
|
8144
8326
|
/**
|
|
8327
|
+
* 从上游错误响应体提取可读摘录(#46):JSON 时取 error.message(兼容 {error:{message}}
|
|
8328
|
+
* 与 {type:'error',error:{...}} 两种形状),非 JSON 取原文截断。空体返回空串。
|
|
8329
|
+
*/
|
|
8330
|
+
function jevUpstreamDetail(body) {
|
|
8331
|
+
if (body.trim() === "") return "";
|
|
8332
|
+
const excerpt = (text) => text.length > 200 ? `${text.slice(0, 200)}…` : text;
|
|
8333
|
+
try {
|
|
8334
|
+
const parsed = JSON.parse(body);
|
|
8335
|
+
return ` — ${excerpt((typeof parsed.error?.message === "string" ? parsed.error.message : body).trim())}`;
|
|
8336
|
+
} catch {
|
|
8337
|
+
return ` — ${excerpt(body.trim())}`;
|
|
8338
|
+
}
|
|
8339
|
+
}
|
|
8340
|
+
/**
|
|
8145
8341
|
* Причина отказа из брошенного значения: тайм-аут отличается от сети по имени ошибки,
|
|
8146
8342
|
* всё остальное — сеть (fetch отверг соединение, DNS, TLS).
|
|
8147
8343
|
* @param error - то, что попало в catch.
|
|
@@ -8169,7 +8365,8 @@ async function askNoul(connection, state, questions, signal = new AbortControlle
|
|
|
8169
8365
|
method: "POST",
|
|
8170
8366
|
headers: {
|
|
8171
8367
|
Authorization: `Bearer ${connection.apiKey}`,
|
|
8172
|
-
"Content-Type": "application/json"
|
|
8368
|
+
"Content-Type": "application/json",
|
|
8369
|
+
...isOpencodeEndpoint(connection.endpoint) ? { "x-opencode-session": OPENCODE_SESSION_ID } : {}
|
|
8173
8370
|
},
|
|
8174
8371
|
body: JSON.stringify({
|
|
8175
8372
|
model: connection.model,
|
|
@@ -8184,7 +8381,10 @@ async function askNoul(connection, state, questions, signal = new AbortControlle
|
|
|
8184
8381
|
} catch (error) {
|
|
8185
8382
|
throw new JevCallError(failureKind(error), error instanceof Error ? error.message : String(error));
|
|
8186
8383
|
}
|
|
8187
|
-
if (!response.ok)
|
|
8384
|
+
if (!response.ok) {
|
|
8385
|
+
const body = await response.text().catch(() => "");
|
|
8386
|
+
throw new JevCallError("http", `jev: HTTP ${response.status} from ${connection.endpoint}${jevUpstreamDetail(body)}`, response.status);
|
|
8387
|
+
}
|
|
8188
8388
|
const payload = await response.json();
|
|
8189
8389
|
if (payload.answers === void 0 || payload.answers === null) throw new JevCallError("shape", "jev: response missing answers");
|
|
8190
8390
|
const result = {};
|
|
@@ -16593,6 +16793,7 @@ function createEngramTools(baseDeps) {
|
|
|
16593
16793
|
skipped: outcome.skipped
|
|
16594
16794
|
}), message("tool.reflectNote")];
|
|
16595
16795
|
if (consumed > 0) lines.push(message("tool.reflectConsumed", { count: consumed }));
|
|
16796
|
+
if (outcome.dropped > 0) lines.push(message("tool.reflectDropped", { count: outcome.dropped }));
|
|
16596
16797
|
return { text: lines.join("\n") };
|
|
16597
16798
|
}
|
|
16598
16799
|
}),
|
|
@@ -17152,7 +17353,7 @@ function estimateTokens(text) {
|
|
|
17152
17353
|
}
|
|
17153
17354
|
//#endregion
|
|
17154
17355
|
//#region package.json
|
|
17155
|
-
var version = "0.11.
|
|
17356
|
+
var version = "0.11.4";
|
|
17156
17357
|
//#endregion
|
|
17157
17358
|
//#region src/index.ts
|
|
17158
17359
|
/**
|