@modusensus/dsh-mneme 0.9.1 → 0.9.2
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 +7 -6
- package/bin/cli.mjs +61 -7
- package/docs/CONFIGURATION.md +23 -0
- package/lib/api.js +57 -2
- package/lib/client.js +72 -9
- package/lib/config.js +17 -0
- package/lib/dream/sleep.js +35 -12
- package/lib/dream.js +86 -14
- package/lib/embedding.js +52 -22
- package/lib/scope-state.js +277 -0
- package/lib/service.js +80 -3
- package/lib/settings.js +7 -0
- package/lib/store.js +139 -0
- package/lib/tools.js +71 -0
- package/package.json +1 -1
- package/src/api.js +57 -2
- package/src/config.js +17 -0
- package/src/dream/sleep.js +35 -12
- package/src/dream.js +86 -14
- package/src/embedding.js +52 -22
- package/src/scope-state.js +277 -0
- package/src/service.js +80 -3
- package/src/settings.js +7 -0
- package/src/store.js +139 -0
- package/src/tools.js +71 -0
- package/test/api.test.js +113 -1
- package/test/cli-flag-value.test.js +86 -0
- package/test/client.test.js +64 -3
- package/test/distill-tx-embed-gap.test.js +110 -0
- package/test/dream-maxtokens-fallback.test.js +132 -0
- package/test/embed-text-reasons.test.js +112 -0
- package/test/readme-claims.test.js +68 -0
- package/test/scope-state.test.js +481 -0
- package/test/tools-dto-schema-matrix.test.js +12 -3
- package/test/tools.test.js +6 -6
package/src/dream/sleep.js
CHANGED
|
@@ -21,7 +21,7 @@ import { randomUUID, createHash } from "node:crypto";
|
|
|
21
21
|
import { validateDecisions, applyDecisions, ACTIONS } from "./decisions.js";
|
|
22
22
|
import { findPotentialConflicts, cosineSimilarity } from "./clustering.js";
|
|
23
23
|
import { scopeKeyOf } from "../scope.js";
|
|
24
|
-
import { buildReceipt, describeStreamFailure, resolveDreamEffort, withEffortFallback, maintainIndexAfterDream } from "../dream.js";
|
|
24
|
+
import { buildReceipt, describeStreamFailure, resolveDreamEffort, withEffortFallback, withMaxTokensFallback, maintainIndexAfterDream } from "../dream.js";
|
|
25
25
|
import { computeHeat } from "../heat.js";
|
|
26
26
|
|
|
27
27
|
// Issue #126 review(Copilot):默认档(sleepActionSet="conflict")允许的动作 = 全局
|
|
@@ -172,6 +172,22 @@ function makeSummary(m) {
|
|
|
172
172
|
return text.length <= SUMMARY_MAX ? text : `${text.slice(0, SUMMARY_MAX)}…`;
|
|
173
173
|
}
|
|
174
174
|
|
|
175
|
+
// issue #471 政策轴:被按下的作用域也不参与睡眠整理。与 dream.js 的 withoutPaused
|
|
176
|
+
// 同源(那里记着「判定只许有一份实现」的理由),但这里是**单独一处**而不是复用:
|
|
177
|
+
// 两个模块各被独立装配(runSleep / runDream 各自接 service),任何一个被换进只
|
|
178
|
+
// 实现了一半 service 的夹具时都该各自降级,而不是因为对方模块没装而双双失效。
|
|
179
|
+
//
|
|
180
|
+
// 睡眠侧比日间加固侧更要紧:这几个相位不只读——phaseDemotion 会 setArchived 与
|
|
181
|
+
// demoteToSummary(真改归档面),phasePatterns 会 mint 出 type=pattern 的新行
|
|
182
|
+
// (凭空给被按下的作用域生出衍生记忆)。只挡日间那几处读入口的话,闸门开着而每
|
|
183
|
+
// 6 小时一次的睡眠照样动手,那等于「按下」只是让它在白天看不见。
|
|
184
|
+
//
|
|
185
|
+
// service 上取不到 filterPaused 时(老装配、测试夹具)原样返回,与改动前一致。
|
|
186
|
+
function withoutPaused(service, rows) {
|
|
187
|
+
if (typeof service?.filterPaused !== "function") return rows;
|
|
188
|
+
return service.filterPaused(rows);
|
|
189
|
+
}
|
|
190
|
+
|
|
175
191
|
// ---------------------------------------------------------------- phases
|
|
176
192
|
|
|
177
193
|
/**
|
|
@@ -189,7 +205,7 @@ async function phaseConflicts(ctx, service, config, logger, runId, semantic = nu
|
|
|
189
205
|
}
|
|
190
206
|
const strictness = config.sleepConflictStrictness ?? "normal";
|
|
191
207
|
const threshold = CONFLICT_THRESHOLDS[strictness] ?? CONFLICT_THRESHOLDS.normal;
|
|
192
|
-
const memories = service.all().filter((m) => !m.archived && !m.forgotten && m.type !== "summary" && m.type !== "document");
|
|
208
|
+
const memories = withoutPaused(service, service.all().filter((m) => !m.archived && !m.forgotten && m.type !== "summary" && m.type !== "document"));
|
|
193
209
|
if (memories.length < 2) return { status: "skipped", reason: "too few memories" };
|
|
194
210
|
if (signal?.aborted) return { status: "aborted", reason: "user activity" };
|
|
195
211
|
|
|
@@ -313,7 +329,7 @@ async function phaseConflicts(ctx, service, config, logger, runId, semantic = nu
|
|
|
313
329
|
// actionSet 已在候选构造前解析(跨类型候选需要它),这里只按档位选 prompt:
|
|
314
330
|
// 默认档保持只有 conflict/keep 的窄 prompt,"full" 才开放六分支。
|
|
315
331
|
const conflictPrompt = actionSet === "full" ? STR.prompts.conflictFull[language] : STR.prompts.conflict[language];
|
|
316
|
-
const runConflict = (withEffort) => {
|
|
332
|
+
const runConflict = (withEffort, tokens) => {
|
|
317
333
|
conflictStreamFailure = "";
|
|
318
334
|
return auditedSleepLlm(ctx, service, config, logger, {
|
|
319
335
|
triggerSource: "sleep",
|
|
@@ -331,7 +347,7 @@ async function phaseConflicts(ctx, service, config, logger, runId, semantic = nu
|
|
|
331
347
|
provider: route.provider,
|
|
332
348
|
model: route.model,
|
|
333
349
|
purpose: "sleep-conflict",
|
|
334
|
-
maxTokens:
|
|
350
|
+
maxTokens: tokens,
|
|
335
351
|
...(withEffort && sleepEffort ? { reasoningEffort: sleepEffort } : {}),
|
|
336
352
|
messages: [
|
|
337
353
|
{ role: "system", content: [{ type: "text", text: conflictPrompt }], source: { kind: "plugin:dsh-mneme", plugin: "dsh-mneme" } },
|
|
@@ -339,7 +355,9 @@ async function phaseConflicts(ctx, service, config, logger, runId, semantic = nu
|
|
|
339
355
|
]
|
|
340
356
|
}, reportUsage, (reason) => { conflictStreamFailure = describeStreamFailure(reason); }));
|
|
341
357
|
};
|
|
342
|
-
|
|
358
|
+
// Issue #478:睡眠侧同样接 max_tokens 降级——面板 hint 已承诺「超上限同样自动降级」,
|
|
359
|
+
// 只挂 dream 三个点会让 sleepMaxTokens 的 131072 上限继续原样发。
|
|
360
|
+
const text = await withMaxTokensFallback(ctx, config.sleepMaxTokens ?? 2048, (tokens) => withEffortFallback(ctx, sleepEffort, () => runConflict(true, tokens), () => runConflict(false, tokens), () => conflictStreamFailure), () => conflictStreamFailure);
|
|
343
361
|
if (text === undefined) {
|
|
344
362
|
if (conflictStreamFailure) ctx.logger?.warn?.(`dsh-mneme sleep: conflict stream aborted or errored (${conflictStreamFailure})`);
|
|
345
363
|
return { status: "failed", error: "llm failed" };
|
|
@@ -423,7 +441,10 @@ function phaseDemotion(service, config, logger, runId, signal = null) {
|
|
|
423
441
|
const compressCut = Date.now() - compressDays * 86400000;
|
|
424
442
|
const demoted = [];
|
|
425
443
|
const archived = [];
|
|
426
|
-
|
|
444
|
+
// 政策轴在这里尤其要紧:下面两行会真改归档面(setArchived / demoteToSummary)。
|
|
445
|
+
// 把过滤放在循环之外、而不是在循环体里 continue:一旦将来有人在循环体里加
|
|
446
|
+
// 副作用(现已有),漏掉那条 continue 就是静默改到被按下的行。
|
|
447
|
+
for (const m of withoutPaused(service, service.all())) {
|
|
427
448
|
if (signal?.aborted) break;
|
|
428
449
|
if (m.archived || m.forgotten) continue;
|
|
429
450
|
const ref = m.last_accessed_at ?? m.created_at;
|
|
@@ -472,9 +493,11 @@ async function phasePatterns(ctx, service, config, logger, runId, signal = null)
|
|
|
472
493
|
const limit = config.sleepPatternMinMemories ?? 100;
|
|
473
494
|
// #230:document 在查询层排除(excludeTypes)而不是截断后过滤——LIMIT 200
|
|
474
495
|
// 先生效的话,指针行一多就会把普通记忆挤出扫描窗,池子饿到门槛以下。
|
|
475
|
-
|
|
496
|
+
// 政策轴同理必须排在 `.slice(0, limit)` **之前**,否则被按下的行照样占掉
|
|
497
|
+
// sleepPatternMinMemories 的名额,而它们既不会被扫、也不该挤掉别人(#471)。
|
|
498
|
+
const memories = withoutPaused(service, service
|
|
476
499
|
.list({ limit: 200, includeForgotten: false, excludeTypes: ["document"] })
|
|
477
|
-
.filter((m) => !m.archived && m.type !== "summary" && m.type !== "pattern")
|
|
500
|
+
.filter((m) => !m.archived && m.type !== "summary" && m.type !== "pattern"))
|
|
478
501
|
.sort((a, b) => (a.updated_at < b.updated_at ? 1 : -1))
|
|
479
502
|
.slice(0, limit);
|
|
480
503
|
if (memories.length === 0) return { status: "skipped", reason: "no memories to scan" };
|
|
@@ -485,7 +508,7 @@ async function phasePatterns(ctx, service, config, logger, runId, signal = null)
|
|
|
485
508
|
const maxPatterns = config.sleepMaxPatternPerRun ?? 3;
|
|
486
509
|
const sleepEffort = await resolveDreamEffort(ctx, route, config.sleepReasoningEffort, logger);
|
|
487
510
|
let patternStreamFailure = "";
|
|
488
|
-
const runPattern = (withEffort) => {
|
|
511
|
+
const runPattern = (withEffort, tokens) => {
|
|
489
512
|
patternStreamFailure = "";
|
|
490
513
|
return auditedSleepLlm(ctx, service, config, logger, {
|
|
491
514
|
triggerSource: "sleep",
|
|
@@ -500,7 +523,7 @@ async function phasePatterns(ctx, service, config, logger, runId, signal = null)
|
|
|
500
523
|
provider: route.provider,
|
|
501
524
|
model: route.model,
|
|
502
525
|
purpose: "sleep-pattern",
|
|
503
|
-
maxTokens:
|
|
526
|
+
maxTokens: tokens,
|
|
504
527
|
...(withEffort && sleepEffort ? { reasoningEffort: sleepEffort } : {}),
|
|
505
528
|
messages: [
|
|
506
529
|
{ role: "system", content: [{ type: "text", text: STR.prompts.pattern[language].replace("N", String(maxPatterns)) }], source: { kind: "plugin:dsh-mneme", plugin: "dsh-mneme" } },
|
|
@@ -508,7 +531,7 @@ async function phasePatterns(ctx, service, config, logger, runId, signal = null)
|
|
|
508
531
|
]
|
|
509
532
|
}, reportUsage, (reason) => { patternStreamFailure = describeStreamFailure(reason); }));
|
|
510
533
|
};
|
|
511
|
-
const text = await withEffortFallback(ctx, sleepEffort, () => runPattern(true), () => runPattern(false), () => patternStreamFailure);
|
|
534
|
+
const text = await withMaxTokensFallback(ctx, config.sleepMaxTokens ?? 2048, (tokens) => withEffortFallback(ctx, sleepEffort, () => runPattern(true, tokens), () => runPattern(false, tokens), () => patternStreamFailure), () => patternStreamFailure);
|
|
512
535
|
if (text === undefined) {
|
|
513
536
|
if (patternStreamFailure) ctx.logger?.warn?.(`dsh-mneme sleep: pattern stream aborted or errored (${patternStreamFailure})`);
|
|
514
537
|
return { status: "failed", error: "llm failed" };
|
|
@@ -563,7 +586,7 @@ function phaseRelations(service, config, logger, runId, signal = null) {
|
|
|
563
586
|
if (entities.length < 2) return { status: "skipped", reason: "too few entities" };
|
|
564
587
|
const orphans = entities.filter((e) => (service.getRelations(e.id) ?? []).length === 0);
|
|
565
588
|
if (orphans.length === 0) return { status: "skipped", reason: "no orphan entities" };
|
|
566
|
-
const memories = service.all().filter((m) => !m.archived && !m.forgotten);
|
|
589
|
+
const memories = withoutPaused(service, service.all().filter((m) => !m.archived && !m.forgotten));
|
|
567
590
|
const seen = new Set();
|
|
568
591
|
const related = [];
|
|
569
592
|
const MAX_RELATIONS_PER_ORPHAN = 3;
|
package/src/dream.js
CHANGED
|
@@ -10,7 +10,7 @@ import { scopeKeyOf } from "./scope.js";
|
|
|
10
10
|
import { isInPeakWindow, nextOffPeakAt } from "./peak-hours.js";
|
|
11
11
|
import { createHash, randomUUID } from "node:crypto";
|
|
12
12
|
import { STR, langOf } from "./lang.js";
|
|
13
|
-
export { validateDecisions, applyDecisions, withEffortFallback, describeStreamFailure, resolveDreamEffort, resolveRoute };
|
|
13
|
+
export { validateDecisions, applyDecisions, withEffortFallback, withMaxTokensFallback, parseMaxTokensCap, describeStreamFailure, resolveDreamEffort, resolveRoute };
|
|
14
14
|
|
|
15
15
|
// Extract the first JSON array from LLM output, tolerating markdown fences,
|
|
16
16
|
// leading/trailing prose, and common wrapper noise. Returns an array or null.
|
|
@@ -57,10 +57,24 @@ function totalChars(memories) {
|
|
|
57
57
|
// 两处若各写一份过滤条件,重启播种读到的基线就会和运行期判定口径漂移(一个把
|
|
58
58
|
// document 算进去、一个不算),阈值随之失真,且这种漂移只会在重启后才显形。
|
|
59
59
|
function activeStoreSize(service) {
|
|
60
|
-
const memories = service.all().filter((m) => !m.archived && m.type !== "summary" && m.type !== "document");
|
|
60
|
+
const memories = withoutPaused(service, service.all().filter((m) => !m.archived && m.type !== "summary" && m.type !== "document"));
|
|
61
61
|
return { count: memories.length, chars: totalChars(memories) };
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
+
// issue #471 政策轴:被按下的作用域不参与巩固,也就不该再撑着阈值把它自己触发起来。
|
|
65
|
+
// 三个读入口(本函数、generateNarratives 的 inputs、runDream 的 memories)各自的附加
|
|
66
|
+
// 条件不同——有的排 forgotten、有的不排——但「政策轴这一层」对三处是一样的,故收成
|
|
67
|
+
// 一个助手而不是各写一遍过滤,理由与 activeStoreSize 开头记着的口径漂移同源:两处
|
|
68
|
+
// 各写一份过滤条件,漂移只会在重启后才显形。
|
|
69
|
+
//
|
|
70
|
+
// 走 service.filterPaused 而不是自己读状态表:判定只许有一份实现(见
|
|
71
|
+
// src/scope-state.js 的模块注释)。service 上取不到这个函数时(老装配、测试夹具)
|
|
72
|
+
// 原样返回,行为与改动前一致。
|
|
73
|
+
function withoutPaused(service, rows) {
|
|
74
|
+
if (typeof service?.filterPaused !== "function") return rows;
|
|
75
|
+
return service.filterPaused(rows);
|
|
76
|
+
}
|
|
77
|
+
|
|
64
78
|
// 基线的唯一合法形状:两个非负整数;其余(缺字段、负数、非整数、整个对象缺失)
|
|
65
79
|
// 一律视为「没有基线」→ 调用方退回 {0,0}。播种值来自持久化列,可能是老库的 NULL
|
|
66
80
|
// 或手工改坏的库,判定必须比调用点更严格,而不是相信数据库里存的一定合法。
|
|
@@ -399,6 +413,60 @@ async function withEffortFallback(ctx, effort, attempt, fallback, getStreamError
|
|
|
399
413
|
}
|
|
400
414
|
}
|
|
401
415
|
|
|
416
|
+
// Issue #478:max_tokens 超服务商硬上限的拒收判别式与上限提取的单一来源。
|
|
417
|
+
// 实测失败样例(dream_runs 180 连败):"max_tokens 不能超过 65536"——131072 的
|
|
418
|
+
// 默认值原样发出,网关 400 直接拒,推理还没开始。与 EFFORT_REJECT_RE 同款思路:
|
|
419
|
+
// 各处手写会漂移,导出供测试与其它调用点复用。
|
|
420
|
+
export const MAX_TOKENS_REJECT_RE = /max[\s_-]*tokens?/i;
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* 从拒收报错里提取服务商声明的 max_tokens 上限。只认「显式给数」的说法
|
|
424
|
+
* (不能超过 / at most / maximum value is / <= 65536 …),认不出就返回 null
|
|
425
|
+
* ——宁可不降级重试,也不能把报错里那个被拒的请求值(131072)误当上限。
|
|
426
|
+
* @returns {number|null}
|
|
427
|
+
*/
|
|
428
|
+
function parseMaxTokensCap(message) {
|
|
429
|
+
const m = String(message ?? "").match(
|
|
430
|
+
/(?:不能超过|不得超过|超出上限|至多|最多(?:只能|仅)?|at\s+most|no\s+more\s+than|maximum(?:\s+value)?(?:\s+is)?|max(?:\.)?(?:\s+value)?(?:\s+is)?|less\s+than\s+or\s+equal\s+to|<=?)\s*[::]?\s*(\d{2,10})/i
|
|
431
|
+
);
|
|
432
|
+
const cap = m ? Number(m[1]) : null;
|
|
433
|
+
return cap && Number.isFinite(cap) && cap > 0 ? cap : null;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Issue #478:max_tokens 被服务商 400 拒收时,按报错里的上限自动降级重试一次。
|
|
438
|
+
* 与 withEffortFallback 同构:stream 路径的结果折叠成 undefined、真实原因在
|
|
439
|
+
* getStreamError() 里;throw 路径直接看 message。命中拒收且提取到比当前值小的
|
|
440
|
+
* 上限才重试(attempt 接收 tokens 值),否则原样返回/抛出。单次重试,与
|
|
441
|
+
* effort 的先例一致——重试后若仍被拒(同上限再报),cap >= tokens 的守卫让
|
|
442
|
+
* 它自然终止,不会循环。
|
|
443
|
+
*/
|
|
444
|
+
async function withMaxTokensFallback(ctx, maxTokens, attempt, getStreamError) {
|
|
445
|
+
const retry = (tokens, reason) => {
|
|
446
|
+
ctx.logger?.warn?.(`dsh-mneme dream: max_tokens ${maxTokens} rejected by provider (${reason}); retrying with ${tokens}`);
|
|
447
|
+
return attempt(tokens);
|
|
448
|
+
};
|
|
449
|
+
try {
|
|
450
|
+
const result = await attempt(maxTokens);
|
|
451
|
+
if (result === undefined) {
|
|
452
|
+
const reason = String(getStreamError?.() ?? "");
|
|
453
|
+
if (MAX_TOKENS_REJECT_RE.test(reason)) {
|
|
454
|
+
const cap = parseMaxTokensCap(reason);
|
|
455
|
+
if (cap && cap < maxTokens) return retry(cap, reason);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return result;
|
|
459
|
+
} catch (error) {
|
|
460
|
+
// AbortError 直接放行(取消意图绝不能被降级重试吞掉,与 effort 同款守卫)。
|
|
461
|
+
if (error?.name === "AbortError") throw error;
|
|
462
|
+
const message = String(error?.message ?? error);
|
|
463
|
+
if (!MAX_TOKENS_REJECT_RE.test(message)) throw error;
|
|
464
|
+
const cap = parseMaxTokensCap(message);
|
|
465
|
+
if (!cap || cap >= maxTokens) throw error;
|
|
466
|
+
return retry(cap, message);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
402
470
|
/** 向量索引同步(总览与叙述条共用):fail-safe,失败只 warn 不反噬主流程。 */
|
|
403
471
|
async function reEmbedMemory(semantic, memory, logger) {
|
|
404
472
|
if (!semantic?.embedder || !semantic?.vectorIndex || !memory) return;
|
|
@@ -418,7 +486,7 @@ async function reEmbedMemory(semantic, memory, logger) {
|
|
|
418
486
|
* 任何失败降级为 0 条,绝不反噬 dream 主流程。
|
|
419
487
|
*/
|
|
420
488
|
async function generateNarratives({ ctx, service, config, route, language, effort, semantic, logger }) {
|
|
421
|
-
const inputs = service.all().filter((m) => !m.archived && !m.forgotten && m.type !== "summary" && m.type !== "document");
|
|
489
|
+
const inputs = withoutPaused(service, service.all().filter((m) => !m.archived && !m.forgotten && m.type !== "summary" && m.type !== "document"));
|
|
422
490
|
const clusters = clusterByTag(inputs, { minCluster: config.dreamNarrativeMinCluster ?? 3 });
|
|
423
491
|
if (!clusters.length) return 0;
|
|
424
492
|
|
|
@@ -427,7 +495,7 @@ async function generateNarratives({ ctx, service, config, route, language, effor
|
|
|
427
495
|
[`# ${tag}`, ...members.map((m) => `- id=${m.id} | title=${m.title} | ${m.content}`)].join("\n"))
|
|
428
496
|
.join("\n\n");
|
|
429
497
|
let streamFailure = "";
|
|
430
|
-
const runNarratives = (withEffort) => {
|
|
498
|
+
const runNarratives = (withEffort, tokens) => {
|
|
431
499
|
streamFailure = "";
|
|
432
500
|
return runAuditedLlm(ctx, service, config, {
|
|
433
501
|
triggerSource: "autoDream",
|
|
@@ -439,7 +507,7 @@ async function generateNarratives({ ctx, service, config, route, language, effor
|
|
|
439
507
|
provider: route.provider,
|
|
440
508
|
model: route.model,
|
|
441
509
|
purpose: "compaction",
|
|
442
|
-
maxTokens:
|
|
510
|
+
maxTokens: tokens,
|
|
443
511
|
...(withEffort && effort ? { reasoningEffort: effort } : {}),
|
|
444
512
|
messages: [
|
|
445
513
|
{ role: "system", content: [{ type: "text", text: STR.prompts.narrative[language] }], source: { kind: "plugin:dsh-mneme", plugin: "dsh-mneme" } },
|
|
@@ -447,7 +515,7 @@ async function generateNarratives({ ctx, service, config, route, language, effor
|
|
|
447
515
|
]
|
|
448
516
|
}, reportUsage, (reason) => { streamFailure = describeStreamFailure(reason); }));
|
|
449
517
|
};
|
|
450
|
-
const text = await withEffortFallback(ctx, effort, () => runNarratives(true), () => runNarratives(false), () => streamFailure);
|
|
518
|
+
const text = await withMaxTokensFallback(ctx, config.dreamMaxTokens ?? 2048, (tokens) => withEffortFallback(ctx, effort, () => runNarratives(true, tokens), () => runNarratives(false, tokens), () => streamFailure), () => streamFailure);
|
|
451
519
|
|
|
452
520
|
const entries = extractJsonArray(text);
|
|
453
521
|
if (!Array.isArray(entries)) return 0;
|
|
@@ -946,7 +1014,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
946
1014
|
async function runDream(ctx, service, config) {
|
|
947
1015
|
const language = langOf(config);
|
|
948
1016
|
const logger = ctx.logger;
|
|
949
|
-
let memories = service.all().filter((m) => !m.archived && m.type !== "summary" && m.type !== "document");
|
|
1017
|
+
let memories = withoutPaused(service, service.all().filter((m) => !m.archived && m.type !== "summary" && m.type !== "document"));
|
|
950
1018
|
if (memories.length === 0) return { ok: true, applied: 0, skipped: true, summary: false };
|
|
951
1019
|
// Issue #89:开跑时刻既喂给审计行(created_at),也是调度器闸门的时间基准。
|
|
952
1020
|
const dreamStartedAt = Date.now();
|
|
@@ -1127,7 +1195,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
1127
1195
|
const effort = await resolveDreamEffort(ctx, route, config.dreamReasoningEffort, logger);
|
|
1128
1196
|
let decisions = null;
|
|
1129
1197
|
let streamFailure = "";
|
|
1130
|
-
const runConsolidation = (withEffort) => {
|
|
1198
|
+
const runConsolidation = (withEffort, tokens) => {
|
|
1131
1199
|
streamFailure = "";
|
|
1132
1200
|
return runAuditedLlm(ctx, service, config, {
|
|
1133
1201
|
triggerSource: "autoDream",
|
|
@@ -1143,7 +1211,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
1143
1211
|
provider: route.provider,
|
|
1144
1212
|
model: route.model,
|
|
1145
1213
|
purpose: "compaction",
|
|
1146
|
-
maxTokens:
|
|
1214
|
+
maxTokens: tokens,
|
|
1147
1215
|
...(withEffort && effort ? { reasoningEffort: effort } : {}),
|
|
1148
1216
|
messages: [
|
|
1149
1217
|
{ role: "system", content: [{ type: "text", text: consolidationPrompt }], source: { kind: "plugin:dsh-mneme", plugin: "dsh-mneme" } },
|
|
@@ -1155,7 +1223,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
1155
1223
|
// Bug8: the consolidation call is audited (tokens/time/status). A throw
|
|
1156
1224
|
// re-propagates to the catch below; an aborted stream returns undefined
|
|
1157
1225
|
// and is treated as a failed run after the check below.
|
|
1158
|
-
decisionText = await withEffortFallback(ctx, effort, () => runConsolidation(true), () => runConsolidation(false), () => streamFailure);
|
|
1226
|
+
decisionText = await withMaxTokensFallback(ctx, config.dreamMaxTokens ?? 4096, (tokens) => withEffortFallback(ctx, effort, () => runConsolidation(true, tokens), () => runConsolidation(false, tokens), () => streamFailure), () => streamFailure);
|
|
1159
1227
|
} catch (error) {
|
|
1160
1228
|
logger?.warn?.(`dsh-mneme dream: consolidation llm call failed: ${String(error)}`);
|
|
1161
1229
|
return finish({ ok: false, error: "llm failed", summary: false });
|
|
@@ -1368,7 +1436,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
1368
1436
|
// a failed run; summary:false marks a run that produced no summary.
|
|
1369
1437
|
let summaryText;
|
|
1370
1438
|
let summaryStreamFailure = "";
|
|
1371
|
-
const runSummary = (withEffort) => {
|
|
1439
|
+
const runSummary = (withEffort, tokens) => {
|
|
1372
1440
|
summaryStreamFailure = "";
|
|
1373
1441
|
// Issue #258:总览阶段可用 dreamSummaryProvider/dreamSummaryModel 走独立路由
|
|
1374
1442
|
//(默认空 = 沿用 dream 路由);输入侧有 dreamSummaryMaxInputs 硬上限,
|
|
@@ -1386,7 +1454,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
1386
1454
|
provider: summaryRoute.provider,
|
|
1387
1455
|
model: summaryRoute.model,
|
|
1388
1456
|
purpose: "compaction",
|
|
1389
|
-
maxTokens:
|
|
1457
|
+
maxTokens: tokens,
|
|
1390
1458
|
...(withEffort && effort ? { reasoningEffort: effort } : {}),
|
|
1391
1459
|
messages: [
|
|
1392
1460
|
{ role: "system", content: [{ type: "text", text: STR.prompts.dreamSummary[language] }], source: { kind: "plugin:dsh-mneme", plugin: "dsh-mneme" } },
|
|
@@ -1404,7 +1472,11 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
1404
1472
|
// 渐进恶化。倒序排序与 consolidate 窗口同款(updated_at desc, id tiebreak),
|
|
1405
1473
|
// 保留最新的;0 = 关闭上限(回归全库行为,调用方自担 ctx)。
|
|
1406
1474
|
// #230:document 指针行不进总览(与 dream/sleep 五个候选池的同款排除)。
|
|
1407
|
-
|
|
1475
|
+
// issue #471:这一处尤其不能漏——总览落库时是**无作用域**的 summary 行
|
|
1476
|
+
// (saveWithDedupe 不带 agent_scope/workspace_scope),所以被按下作用域的内容
|
|
1477
|
+
// 一旦进了总览,注入侧按作用域过滤就再也拦不住它:内容已经洗成了全局行。
|
|
1478
|
+
// 别的入口漏了只是「那一条漏过去」,这里漏了是「洗一遍之后一直漏」。
|
|
1479
|
+
const summaryAll = withoutPaused(service, service.all().filter((m) => !m.archived && m.type !== "summary" && m.type !== "document"));
|
|
1408
1480
|
const summaryMaxInputs = Number.isInteger(config.dreamSummaryMaxInputs) ? config.dreamSummaryMaxInputs : 0;
|
|
1409
1481
|
const summaryInputs = summaryMaxInputs > 0 && summaryAll.length > summaryMaxInputs
|
|
1410
1482
|
? [...summaryAll]
|
|
@@ -1425,7 +1497,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
1425
1497
|
);
|
|
1426
1498
|
try {
|
|
1427
1499
|
// Bug8: the summary call is audited too (operation dream_summarize).
|
|
1428
|
-
summaryText = await withEffortFallback(ctx, effort, () => runSummary(true), () => runSummary(false), () => summaryStreamFailure);
|
|
1500
|
+
summaryText = await withMaxTokensFallback(ctx, config.dreamMaxTokens ?? 2048, (tokens) => withEffortFallback(ctx, effort, () => runSummary(true, tokens), () => runSummary(false, tokens), () => summaryStreamFailure), () => summaryStreamFailure);
|
|
1429
1501
|
} catch (error) {
|
|
1430
1502
|
logger?.warn?.(`dsh-mneme dream: summary llm call failed: ${String(error)}`);
|
|
1431
1503
|
return finish({ ok: false, error: "llm failed", applied, decisions: auditDecisions, outcome, frozen: frozenCount, summary: false });
|
package/src/embedding.js
CHANGED
|
@@ -31,10 +31,32 @@ function embeddingsUrl(baseUrl) {
|
|
|
31
31
|
* Call the embeddings API for one text. Resolves to a Float64 array, or null
|
|
32
32
|
* when the provider is not configured, the call fails, or the response is
|
|
33
33
|
* unusable. Never throws: failures degrade to keyword search.
|
|
34
|
+
*
|
|
35
|
+
* The plain null return cannot tell the caller WHY — "model not loaded",
|
|
36
|
+
* "endpoint 404", "timeout" and "input rejected" all look identical, so the
|
|
37
|
+
* receipt ledger used to flatten them into one
|
|
38
|
+
* `embedding-request-returned-no-vector`. Use `embedTextDetailed` when the
|
|
39
|
+
* reason matters; this wrapper keeps the old contract for existing callers.
|
|
34
40
|
*/
|
|
35
|
-
export async function embedText(
|
|
41
|
+
export async function embedText(cfg, text) {
|
|
42
|
+
const out = await embedTextDetailed(cfg, text);
|
|
43
|
+
return out.vector;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Same call, but the failure mode is distinguishable: returns
|
|
48
|
+
* { vector: number[]|null, reason: null|string } where reason is one of
|
|
49
|
+
* config-incomplete | request-failed:<err> | http-<status> |
|
|
50
|
+
* response-not-json | response-no-vector | empty-text
|
|
51
|
+
* Never throws. `reason: null` means success.
|
|
52
|
+
*/
|
|
53
|
+
export async function embedTextDetailed({ baseUrl, apiKey, model }, text) {
|
|
36
54
|
const url = embeddingsUrl(baseUrl);
|
|
37
|
-
if (!url || !apiKey || !model || !text)
|
|
55
|
+
if (!url || !apiKey || !model || !text) {
|
|
56
|
+
// 空输入不是端点故障:区分开,否则「text 为空」会被误报成网络问题。
|
|
57
|
+
const missing = !url ? "baseUrl" : !apiKey ? "apiKey" : !model ? "model" : "text";
|
|
58
|
+
return { vector: null, reason: `config-incomplete:${missing}` };
|
|
59
|
+
}
|
|
38
60
|
let res;
|
|
39
61
|
try {
|
|
40
62
|
res = await fetch(url, {
|
|
@@ -46,18 +68,33 @@ export async function embedText({ baseUrl, apiKey, model }, text) {
|
|
|
46
68
|
body: JSON.stringify({ model, input: String(text).slice(0, 8000) }),
|
|
47
69
|
signal: AbortSignal.timeout(DEFAULT_TIMEOUT_MS)
|
|
48
70
|
});
|
|
49
|
-
} catch {
|
|
50
|
-
|
|
71
|
+
} catch (error) {
|
|
72
|
+
// 超时是 AbortSignal 抛的 TimeoutError;网络层错误(DNS/拒连)是 TypeError。
|
|
73
|
+
// 两者都走 request-failed,但带上类型,日志里能分清「慢」和「不通」。
|
|
74
|
+
const kind = error?.name === "TimeoutError" || error?.name === "AbortError" ? "timeout" : "network";
|
|
75
|
+
return { vector: null, reason: `request-failed:${kind}:${String(error?.message ?? error).slice(0, 200)}` };
|
|
76
|
+
}
|
|
77
|
+
if (!res.ok) {
|
|
78
|
+
// 状态码本身就是最有用的诊断:401/403 是钥匙、404 是路径、429 是限流、
|
|
79
|
+
// 5xx 是服务端。文本截断且不含请求体,日志安全。
|
|
80
|
+
let detail = "";
|
|
81
|
+
try {
|
|
82
|
+
const t = await res.text();
|
|
83
|
+
detail = String(t).slice(0, 200);
|
|
84
|
+
} catch { /* body unreadable, status alone is already informative */ }
|
|
85
|
+
return { vector: null, reason: `http-${res.status}${detail ? `:${detail}` : ""}` };
|
|
51
86
|
}
|
|
52
|
-
if (!res.ok) return null;
|
|
53
87
|
let body;
|
|
54
88
|
try {
|
|
55
89
|
body = await res.json();
|
|
56
90
|
} catch {
|
|
57
|
-
return null;
|
|
91
|
+
return { vector: null, reason: "response-not-json" };
|
|
58
92
|
}
|
|
59
93
|
const vec = body?.data?.[0]?.embedding;
|
|
60
|
-
|
|
94
|
+
if (!Array.isArray(vec) || !vec.length) {
|
|
95
|
+
return { vector: null, reason: "response-no-vector" };
|
|
96
|
+
}
|
|
97
|
+
return { vector: Array.from(vec), reason: null };
|
|
61
98
|
}
|
|
62
99
|
|
|
63
100
|
/**
|
|
@@ -145,24 +182,17 @@ export function createEmbedder({ store, settings, logger, vectorIndex }) {
|
|
|
145
182
|
return entry;
|
|
146
183
|
}
|
|
147
184
|
const text = [title, content].filter(Boolean).join("\n");
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
logger?.warn?.(`[dsh-mneme] embed memory ${id} failed: ${String(error)}`);
|
|
155
|
-
return entry;
|
|
156
|
-
}
|
|
157
|
-
if (!vector) {
|
|
158
|
-
// embedText swallows the HTTP detail (404 / timeout / malformed body all
|
|
159
|
-
// resolve to null). The receipt still has to say "no vector", which is
|
|
160
|
-
// exactly the fact the write path never used to report.
|
|
161
|
-
const entry = { status: "failed", id, at, reason: "embedding-request-returned-no-vector" };
|
|
185
|
+
const detailed = await embedTextDetailed(cfg, text);
|
|
186
|
+
if (detailed.reason) {
|
|
187
|
+
// 回执带上可区分的失败模式(http-404 / request-failed:timeout /
|
|
188
|
+
// config-incomplete:model / response-no-vector ...),不再把「模型没配」
|
|
189
|
+
// 「请求挂了」「响应没有向量」压成同一个词。状态页与日志按 reason 分诊。
|
|
190
|
+
const entry = { status: "failed", id, at, reason: detailed.reason };
|
|
162
191
|
ledger.record(entry);
|
|
163
|
-
logger?.warn?.(`[dsh-mneme] embed memory ${id}
|
|
192
|
+
logger?.warn?.(`[dsh-mneme] embed memory ${id} failed: ${detailed.reason}`);
|
|
164
193
|
return entry;
|
|
165
194
|
}
|
|
195
|
+
const vector = detailed.vector;
|
|
166
196
|
const model = modelHashOf(cfg.model);
|
|
167
197
|
store.setEmbedding(id, vector, model);
|
|
168
198
|
_dimension = vector.length;
|