@kenz1117/dsh-engram 0.11.6 → 0.12.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/CHANGELOG.md +30 -1
- package/i18n/ru.txt +2 -0
- package/lib/client.js +2 -2
- package/lib/client.js.map +1 -1
- package/lib/index.js +1067 -143
- package/package.json +1 -1
- package/registry/schema.json +5 -1
package/lib/index.js
CHANGED
|
@@ -6,7 +6,7 @@ import { homedir } from "node:os";
|
|
|
6
6
|
import z from "@deepseek-ai/schemastery";
|
|
7
7
|
import { execFileSync } from "node:child_process";
|
|
8
8
|
import { fileURLToPath } from "node:url";
|
|
9
|
-
import {
|
|
9
|
+
import { mkdir, mkdtemp, readFile, readdir, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
10
10
|
import { createGzip, gunzip } from "node:zlib";
|
|
11
11
|
import { promisify } from "node:util";
|
|
12
12
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
@@ -229,6 +229,7 @@ const zh$5 = {
|
|
|
229
229
|
"core.warnIngestPreviousFailed": "[dsh-engram] 本轮自动摄取失败(已跳过,不影响对话):",
|
|
230
230
|
"core.warnHistoryBackfillFailed": "[dsh-engram] 历史回填失败:",
|
|
231
231
|
"core.warnDisposeFinalFailed": "[dsh-engram] 会话结束的收尾处理异常(不影响对话):",
|
|
232
|
+
"core.warnInjectionSnapshotFailed": "[dsh-engram] 注入快照落库失败(不影响对话):",
|
|
232
233
|
"core.warnReflectFailed": "[dsh-engram] 会话结束的信念巩固异常({scope},不影响对话):",
|
|
233
234
|
"core.warnReflectBatchDropped": "[dsh-engram] 信念巩固丢弃 {count} 条候选(输出预算/解析失败;未被巩固、下次巩固继续取材)",
|
|
234
235
|
"core.warnDecayStartupFailed": "[dsh-engram] 启动衰减调度失败(跳过):",
|
|
@@ -286,6 +287,7 @@ const en$5 = {
|
|
|
286
287
|
"core.warnIngestPreviousFailed": "[dsh-engram] Automatic ingest of this turn failed (skipped, conversation unaffected):",
|
|
287
288
|
"core.warnHistoryBackfillFailed": "[dsh-engram] History backfill failed:",
|
|
288
289
|
"core.warnDisposeFinalFailed": "[dsh-engram] Session-end finalisation threw (conversation unaffected):",
|
|
290
|
+
"core.warnInjectionSnapshotFailed": "[dsh-engram] Could not persist the injection snapshot (conversation unaffected):",
|
|
289
291
|
"core.warnReflectFailed": "[dsh-engram] Belief consolidation at session end threw ({scope}, conversation unaffected):",
|
|
290
292
|
"core.warnReflectBatchDropped": "[dsh-engram] Belief consolidation dropped {count} candidates (output budget/parse failure; not consolidated, will be picked up next consolidation)",
|
|
291
293
|
"core.warnDecayStartupFailed": "[dsh-engram] Startup decay scheduling failed (skipped):",
|
|
@@ -435,6 +437,7 @@ const zh$3 = {
|
|
|
435
437
|
"misc.jevProviderUnresolved": "Jev:没有提供商——面板、插件设置和会话路由都没有指定",
|
|
436
438
|
"misc.jevModelNotPicked": "Jev:提供商「{provider}」的 DSH 设置里没有 JEV 模型,请手动指定模型",
|
|
437
439
|
"misc.jevEndpointMustBeHttp": "invalid: jev.endpoint 必须是 http(s) URL",
|
|
440
|
+
"misc.jevEndpointOriginMismatch": "Jev:endpoint 必须与提供商「{provider}」的 baseURL 同源;拒绝把宿主凭据发往其他服务",
|
|
438
441
|
"misc.jevModelMustNotBeEmpty": "invalid: jev.model 不能为空",
|
|
439
442
|
"misc.jevTimeoutMsMustBeInteger": "invalid: jev.timeoutMs 必须是 1000-60000 的整数毫秒",
|
|
440
443
|
"tool.languageGuideDescription": "如何把新语言引入记忆宫殿:返回说明文本(文件 docs/agent-language-guide.ru.md,目前只有俄文草稿)。用户的语言不在 zh/en 之内时使用:先读说明,再与用户一起完成登记语言、词典和切换。",
|
|
@@ -489,6 +492,7 @@ const en$3 = {
|
|
|
489
492
|
"misc.jevProviderUnresolved": "Jev: no provider — neither the panel, nor the plugin settings, nor the session route names one",
|
|
490
493
|
"misc.jevModelNotPicked": "Jev: provider \"{provider}\" has no JEV model in its DSH settings; name the model explicitly",
|
|
491
494
|
"misc.jevEndpointMustBeHttp": "invalid: jev.endpoint must be an http(s) URL",
|
|
495
|
+
"misc.jevEndpointOriginMismatch": "Jev: endpoint must share the origin of provider \"{provider}\" baseURL; refusing to send host credentials to another service",
|
|
492
496
|
"misc.jevModelMustNotBeEmpty": "invalid: jev.model must not be empty",
|
|
493
497
|
"misc.jevTimeoutMsMustBeInteger": "invalid: jev.timeoutMs must be an integer number of milliseconds between 1000 and 60000",
|
|
494
498
|
"misc.warnDictionaryProblems": "[dsh-engram] The translation dictionary skipped {count} problem line(s); those texts stay English: {sample}",
|
|
@@ -2447,6 +2451,34 @@ const en = {
|
|
|
2447
2451
|
corridorIndexRooms: "{n} rooms",
|
|
2448
2452
|
skipEmptyAnswer: "empty output (empty after retry)",
|
|
2449
2453
|
skipAllCandidatesFailed: "every candidate failed to write (kept for replay)",
|
|
2454
|
+
tabReplay: "Context replay",
|
|
2455
|
+
pageHeaderReplaySubtitle: "Replay exact injected packets, selected memories, and their provenance",
|
|
2456
|
+
replayFilterLabel: "Session",
|
|
2457
|
+
replayFilterPlaceholder: "Filter by sessionId (optional)",
|
|
2458
|
+
replayCount: "{n} injections",
|
|
2459
|
+
replayEmpty: "No injection snapshots yet; enable profile injection to record them",
|
|
2460
|
+
replayFailed: "Failed to load injection replay",
|
|
2461
|
+
replayOpen: "View final packet",
|
|
2462
|
+
replayClose: "Hide final packet",
|
|
2463
|
+
replayPacketTitle: "Final packet actually received by the model",
|
|
2464
|
+
replayMetaTitle: "Snapshot metadata",
|
|
2465
|
+
replayExactNote: "This text is byte-for-byte the packet used for the hash, audit trail, and model request.",
|
|
2466
|
+
replaySession: "Session",
|
|
2467
|
+
replayHash: "SHA-256",
|
|
2468
|
+
replayMemories: "Selected memories",
|
|
2469
|
+
replayCurated: "Curated profile",
|
|
2470
|
+
replayCompactions: "Compactions",
|
|
2471
|
+
replaySize: "Packet size",
|
|
2472
|
+
benchInspect: "Inspect placard",
|
|
2473
|
+
governanceTitle: "Contradiction governance",
|
|
2474
|
+
governanceHint: "Only contradictions with both sides active are shown; reclassifying keeps both; closing archives the selected side",
|
|
2475
|
+
governanceCount: "{n} pairs pending",
|
|
2476
|
+
governanceEmpty: "No contradictions pending",
|
|
2477
|
+
governanceFailed: "Failed to load governance queue",
|
|
2478
|
+
governanceDismiss: "Reclassify as related",
|
|
2479
|
+
governanceForget: "Close this memory",
|
|
2480
|
+
governanceActionFailed: "Action failed",
|
|
2481
|
+
governanceStableId: "Stable ID",
|
|
2450
2482
|
tabSpiral: "Memory Spiral",
|
|
2451
2483
|
spiralAriaLabel: "memory spiral view",
|
|
2452
2484
|
spiralCenterLabel: "Now",
|
|
@@ -3023,6 +3055,34 @@ const ru = {
|
|
|
3023
3055
|
corridorIndexRooms: "комнат: {n}",
|
|
3024
3056
|
skipEmptyAnswer: "вывод дистилляции пуст (пуст и после повтора)",
|
|
3025
3057
|
skipAllCandidatesFailed: "все кандидаты не записались (сохранено для повтора)",
|
|
3058
|
+
tabReplay: "Воспроизведение контекста",
|
|
3059
|
+
pageHeaderReplaySubtitle: "Точные пакеты внедрения, выбранные воспоминания и их происхождение",
|
|
3060
|
+
replayFilterLabel: "Сессия",
|
|
3061
|
+
replayFilterPlaceholder: "Фильтр по sessionId (необязательно)",
|
|
3062
|
+
replayCount: "внедрений: {n}",
|
|
3063
|
+
replayEmpty: "Снимков внедрения ещё нет; включите внедрение профиля для записи",
|
|
3064
|
+
replayFailed: "Не удалось загрузить воспроизведение",
|
|
3065
|
+
replayOpen: "Показать итоговый пакет",
|
|
3066
|
+
replayClose: "Скрыть итоговый пакет",
|
|
3067
|
+
replayPacketTitle: "Пакет, фактически полученный моделью",
|
|
3068
|
+
replayMetaTitle: "Метаданные снимка",
|
|
3069
|
+
replayExactNote: "Этот текст дословно совпадает с пакетом для хеша, аудита и запроса модели.",
|
|
3070
|
+
replaySession: "Сессия",
|
|
3071
|
+
replayHash: "Хеш SHA-256",
|
|
3072
|
+
replayMemories: "Выбранные воспоминания",
|
|
3073
|
+
replayCurated: "Курируемый профиль",
|
|
3074
|
+
replayCompactions: "Сжатий",
|
|
3075
|
+
replaySize: "Размер пакета",
|
|
3076
|
+
benchInspect: "Открыть табличку",
|
|
3077
|
+
governanceTitle: "Разбор противоречий",
|
|
3078
|
+
governanceHint: "Показаны только противоречия с двумя активными сторонами; переклассификация сохраняет обе; закрытие архивирует выбранную",
|
|
3079
|
+
governanceCount: "ожидают решения: {n}",
|
|
3080
|
+
governanceEmpty: "Нет противоречий для решения",
|
|
3081
|
+
governanceFailed: "Не удалось загрузить очередь разбора",
|
|
3082
|
+
governanceDismiss: "Переклассифицировать как связанные",
|
|
3083
|
+
governanceForget: "Закрыть это воспоминание",
|
|
3084
|
+
governanceActionFailed: "Не удалось выполнить действие",
|
|
3085
|
+
governanceStableId: "Стабильный ID",
|
|
3026
3086
|
tabSpiral: "Спираль воспоминаний",
|
|
3027
3087
|
spiralAriaLabel: "спираль воспоминаний",
|
|
3028
3088
|
spiralCenterLabel: "сейчас",
|
|
@@ -3597,6 +3657,34 @@ const zh = {
|
|
|
3597
3657
|
corridorIndexRooms: "{n} 间房",
|
|
3598
3658
|
skipEmptyAnswer: "提炼输出为空(重试后仍空)",
|
|
3599
3659
|
skipAllCandidatesFailed: "全部候选落库失败(保留待重放)",
|
|
3660
|
+
tabReplay: "上下文回放",
|
|
3661
|
+
pageHeaderReplaySubtitle: "回看每次实际注入的最终包、入选记忆和逐字事实源",
|
|
3662
|
+
replayFilterLabel: "会话",
|
|
3663
|
+
replayFilterPlaceholder: "按 sessionId 过滤(可留空)",
|
|
3664
|
+
replayCount: "{n} 次注入",
|
|
3665
|
+
replayEmpty: "还没有注入快照;开启画像注入后自动记录",
|
|
3666
|
+
replayFailed: "注入回放加载失败",
|
|
3667
|
+
replayOpen: "查看最终包",
|
|
3668
|
+
replayClose: "收起最终包",
|
|
3669
|
+
replayPacketTitle: "模型实际收到的最终包",
|
|
3670
|
+
replayMetaTitle: "快照元数据",
|
|
3671
|
+
replayExactNote: "此处正文与哈希、审计和当时模型收到的内容逐字一致。",
|
|
3672
|
+
replaySession: "会话",
|
|
3673
|
+
replayHash: "SHA-256",
|
|
3674
|
+
replayMemories: "入选记忆",
|
|
3675
|
+
replayCurated: "人工画像",
|
|
3676
|
+
replayCompactions: "压缩次数",
|
|
3677
|
+
replaySize: "包大小",
|
|
3678
|
+
benchInspect: "查看铭牌",
|
|
3679
|
+
governanceTitle: "矛盾治理",
|
|
3680
|
+
governanceHint: "只显示双侧仍驻留的矛盾边;改判保留两条记忆,闭馆会作废所选一侧",
|
|
3681
|
+
governanceCount: "{n} 组待处置",
|
|
3682
|
+
governanceEmpty: "没有待处置矛盾",
|
|
3683
|
+
governanceFailed: "治理队列加载失败",
|
|
3684
|
+
governanceDismiss: "改判为相关",
|
|
3685
|
+
governanceForget: "闭馆此条",
|
|
3686
|
+
governanceActionFailed: "处置失败",
|
|
3687
|
+
governanceStableId: "稳定身份",
|
|
3600
3688
|
tabSpiral: "回忆螺旋",
|
|
3601
3689
|
spiralAriaLabel: "回忆螺旋视图",
|
|
3602
3690
|
spiralCenterLabel: "现在",
|
|
@@ -6928,22 +7016,28 @@ async function markPendingIngest(store, sessionId, turn) {
|
|
|
6928
7016
|
* pre-step 重放补做),绝不影响对话。
|
|
6929
7017
|
* 本函数不 reject:从取轮次到摄取全程在 try 内,异常一律降级为告警 + pending 键
|
|
6930
7018
|
* (dispose 观察器是 fire-and-forget,逃逸的 rejection 会被宿主的 fail-loud 当作致命错误)。
|
|
6931
|
-
* @returns
|
|
7019
|
+
* @returns 摄取结果;无末轮或异常失败(已尝试落 pending)时返回 null,结果型失败原样返回。
|
|
6932
7020
|
*/
|
|
6933
7021
|
async function ingestFinalTurn(deps) {
|
|
6934
7022
|
/** 末轮轮次号;取不到(无 turn/start 或事件源不可用)时不落 pending——键需要轮次。 */
|
|
6935
7023
|
let round;
|
|
7024
|
+
const persistPending = async () => {
|
|
7025
|
+
if (round === void 0) return;
|
|
7026
|
+
try {
|
|
7027
|
+
await markPendingIngest(await (deps.openAuditStore ?? deps.openStore)(), deps.sessionId, round);
|
|
7028
|
+
} catch {}
|
|
7029
|
+
};
|
|
6936
7030
|
try {
|
|
6937
7031
|
round = lastTurnNumber(deps.events);
|
|
6938
7032
|
if (round === void 0) return null;
|
|
6939
|
-
|
|
7033
|
+
const outcome = await ingestPreviousTurn({
|
|
6940
7034
|
...deps,
|
|
6941
7035
|
slice: "last"
|
|
6942
7036
|
});
|
|
7037
|
+
if (outcome.skipped === "empty-answer" || outcome.skipped === "unparseable-output" || outcome.skipped === "all-candidates-failed") await persistPending();
|
|
7038
|
+
return outcome;
|
|
6943
7039
|
} catch (error) {
|
|
6944
|
-
|
|
6945
|
-
await markPendingIngest(await (deps.openAuditStore ?? deps.openStore)(), deps.sessionId, round);
|
|
6946
|
-
} catch {}
|
|
7040
|
+
await persistPending();
|
|
6947
7041
|
console.warn(message("llm.warnFinalIngestFailed"), error);
|
|
6948
7042
|
return null;
|
|
6949
7043
|
}
|
|
@@ -7893,6 +7987,540 @@ function requireCandidateIds(raw, candidateIds) {
|
|
|
7893
7987
|
}
|
|
7894
7988
|
return ids;
|
|
7895
7989
|
}
|
|
7990
|
+
// src/jobs/queue.ts
|
|
7991
|
+
/**
|
|
7992
|
+
* 持久作业队列原语:基于 node:sqlite 的独立 DB 文件(WAL + busy_timeout),
|
|
7993
|
+
* 供后续 reflect/consolidation 等宿主调度使用——本模块只提供队列原语,不做调度。
|
|
7994
|
+
*
|
|
7995
|
+
* 与主库(store/sqlite)的差别:队列按「多 worker 竞争」设计,写事务一律
|
|
7996
|
+
* BEGIN IMMEDIATE——先拿写锁再动手,撞车交给 busy_timeout 等待,而不是让
|
|
7997
|
+
* 事务中途从读锁升级写锁时撞 SQLITE_BUSY。表名固定常量、全部参数化查询,
|
|
7998
|
+
* 不存在拼接用户输入的 SQL 面。
|
|
7999
|
+
*
|
|
8000
|
+
* 状态机(无独立 retrying 状态,重试 = 回 queued + 延期 runAfter,attempts 保留):
|
|
8001
|
+
* queued --claim--> running --complete--> done
|
|
8002
|
+
* ^ | --fail(attempts<max)--> queued(runAfter 延期)
|
|
8003
|
+
* | └─── --fail(attempts>=max)--> dead
|
|
8004
|
+
* └-------- claim 到期的 queued;claim 租约过期的 running(被新 owner 抢走)
|
|
8005
|
+
*
|
|
8006
|
+
* done/dead 是终态:不可 claim、不可 complete/fail/checkpoint;只能被 prune 按
|
|
8007
|
+
* 保留窗口清除(历史垃圾回收),或因 payload 损坏(毒丸)被 claim 就地 dead-letter。
|
|
8008
|
+
* @module @kenz1117/dsh-engram/jobs/queue
|
|
8009
|
+
*/
|
|
8010
|
+
/** 表名固定常量:所有语句写死引用它,绝不拼接调用方输入。 */
|
|
8011
|
+
const TABLE = "jobs";
|
|
8012
|
+
/** 队列库自身结构版本(PRAGMA user_version);结构性变更必须 +1。 */
|
|
8013
|
+
const QUEUE_SCHEMA_VERSION = 1;
|
|
8014
|
+
/** maxAttempts 缺省值:允许 3 次尝试(首次 + 2 次重试)。 */
|
|
8015
|
+
const DEFAULT_MAX_ATTEMPTS = 3;
|
|
8016
|
+
/** 校验非空字符串(不静默 trim:只拒绝纯空白,原文原样入库)。 */
|
|
8017
|
+
function requireText(value, field) {
|
|
8018
|
+
if (typeof value !== "string" || value.trim().length === 0) throw new EngramError("JOB_INVALID", `jobs: ${field} 必须是非空字符串`);
|
|
8019
|
+
return value;
|
|
8020
|
+
}
|
|
8021
|
+
/** 校验正整数(maxAttempts/leaseMs/limit)。 */
|
|
8022
|
+
function requirePositiveInt(value, field) {
|
|
8023
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value <= 0) throw new EngramError("JOB_INVALID", `jobs: ${field} 必须是正整数`);
|
|
8024
|
+
return value;
|
|
8025
|
+
}
|
|
8026
|
+
/** 校验非负整数(runAfter/retryDelayMs)。 */
|
|
8027
|
+
function requireNonNegativeInt(value, field) {
|
|
8028
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < 0) throw new EngramError("JOB_INVALID", `jobs: ${field} 必须是非负整数`);
|
|
8029
|
+
return value;
|
|
8030
|
+
}
|
|
8031
|
+
/** payload 只收可 JSON 序列化的值:undefined/函数/循环引用一律 JOB_INVALID。 */
|
|
8032
|
+
function serializePayload(payload) {
|
|
8033
|
+
let json;
|
|
8034
|
+
try {
|
|
8035
|
+
json = JSON.stringify(payload);
|
|
8036
|
+
} catch (cause) {
|
|
8037
|
+
throw new EngramError("JOB_INVALID", "jobs: payload 必须可 JSON 序列化", { cause });
|
|
8038
|
+
}
|
|
8039
|
+
if (json === void 0) throw new EngramError("JOB_INVALID", "jobs: payload 必须可 JSON 序列化");
|
|
8040
|
+
return json;
|
|
8041
|
+
}
|
|
8042
|
+
/** complete/fail/checkpoint 共用的守卫:作业存在、正在 running、调用方就是当前租约持有者、且租约未过期。 */
|
|
8043
|
+
function requireOwnedRunning(row, input, at) {
|
|
8044
|
+
if (row.status !== "running") throw new EngramError("JOB_NOT_RUNNING", `jobs: 作业 ${row.id} 不在 running(当前 ${row.status})`);
|
|
8045
|
+
if (row.lease_owner !== input.owner) throw new EngramError("JOB_OWNER_MISMATCH", `jobs: 作业 ${row.id} 的租约持有者是 ${row.lease_owner ?? "无"},不是 ${input.owner}`);
|
|
8046
|
+
if (row.lease_expires_at === null || row.lease_expires_at <= at) throw new EngramError("JOB_LEASE_EXPIRED", `jobs: 作业 ${row.id} 的租约已于 ${row.lease_expires_at} 过期(now ${at}),owner ${input.owner} 须重新 claim`);
|
|
8047
|
+
}
|
|
8048
|
+
/**
|
|
8049
|
+
* 打开(必要时创建)一个独立队列库。
|
|
8050
|
+
* @param path - SQLite 文件路径;目录不存在会自动创建(0o700)。
|
|
8051
|
+
* @param options - now 注入时钟;缺省 Date.now。
|
|
8052
|
+
* @returns 就绪的 JobQueue。
|
|
8053
|
+
* @throws EngramError(code=SCHEMA_INCOMPATIBLE) 当队列库结构版本高于当前实现。
|
|
8054
|
+
* @throws EngramError(code=JOB_INVALID) 当 path 非法或时钟不可用。
|
|
8055
|
+
*/
|
|
8056
|
+
async function openJobQueue(path, options = {}) {
|
|
8057
|
+
if (typeof path !== "string" || path.length === 0) throw new EngramError("JOB_INVALID", "jobs: path 必须是非空字符串");
|
|
8058
|
+
const now = options.now ?? (() => Date.now());
|
|
8059
|
+
const probed = now();
|
|
8060
|
+
if (typeof probed !== "number" || !Number.isFinite(probed)) throw new EngramError("JOB_INVALID", "jobs: 注入的 now() 必须返回有限数字");
|
|
8061
|
+
await mkdir(dirname(path), {
|
|
8062
|
+
recursive: true,
|
|
8063
|
+
mode: 448
|
|
8064
|
+
});
|
|
8065
|
+
const { DatabaseSync } = await import("node:sqlite");
|
|
8066
|
+
const db = new DatabaseSync(path);
|
|
8067
|
+
/** 手工事务(IMMEDIATE:队列是多写者场景,先拿写锁让 busy_timeout 吸收竞争)。 */
|
|
8068
|
+
const withTransaction = (fn) => {
|
|
8069
|
+
db.exec("BEGIN IMMEDIATE");
|
|
8070
|
+
try {
|
|
8071
|
+
fn();
|
|
8072
|
+
db.exec("COMMIT");
|
|
8073
|
+
} catch (error) {
|
|
8074
|
+
db.exec("ROLLBACK");
|
|
8075
|
+
throw error;
|
|
8076
|
+
}
|
|
8077
|
+
};
|
|
8078
|
+
let versionRow;
|
|
8079
|
+
try {
|
|
8080
|
+
db.exec("PRAGMA journal_mode = WAL;");
|
|
8081
|
+
db.exec("PRAGMA busy_timeout = 5000;");
|
|
8082
|
+
db.exec("PRAGMA synchronous = NORMAL;");
|
|
8083
|
+
versionRow = db.prepare("PRAGMA user_version").get();
|
|
8084
|
+
const version = versionRow?.user_version ?? 0;
|
|
8085
|
+
if (version > QUEUE_SCHEMA_VERSION) throw new EngramError("SCHEMA_INCOMPATIBLE", `jobs: 队列库结构版本过新(found ${version} > supported ${QUEUE_SCHEMA_VERSION},path ${path})`);
|
|
8086
|
+
if (version < QUEUE_SCHEMA_VERSION) db.exec(`
|
|
8087
|
+
CREATE TABLE IF NOT EXISTS ${TABLE} (
|
|
8088
|
+
id TEXT PRIMARY KEY,
|
|
8089
|
+
kind TEXT NOT NULL,
|
|
8090
|
+
payload TEXT NOT NULL,
|
|
8091
|
+
status TEXT NOT NULL,
|
|
8092
|
+
attempts INTEGER NOT NULL,
|
|
8093
|
+
max_attempts INTEGER NOT NULL,
|
|
8094
|
+
run_after INTEGER NOT NULL,
|
|
8095
|
+
lease_owner TEXT,
|
|
8096
|
+
lease_expires_at INTEGER,
|
|
8097
|
+
checkpoint TEXT,
|
|
8098
|
+
last_error TEXT,
|
|
8099
|
+
created_at INTEGER NOT NULL,
|
|
8100
|
+
updated_at INTEGER NOT NULL);
|
|
8101
|
+
CREATE INDEX IF NOT EXISTS jobs_due ON ${TABLE} (status, run_after);
|
|
8102
|
+
PRAGMA user_version = ${QUEUE_SCHEMA_VERSION};`);
|
|
8103
|
+
} catch (error) {
|
|
8104
|
+
try {
|
|
8105
|
+
db.close();
|
|
8106
|
+
} catch {}
|
|
8107
|
+
throw error;
|
|
8108
|
+
}
|
|
8109
|
+
const sqlGet = db.prepare(`SELECT * FROM ${TABLE} WHERE id = ?`);
|
|
8110
|
+
const sqlInsert = db.prepare(`INSERT INTO ${TABLE}
|
|
8111
|
+
(id, kind, payload, status, attempts, max_attempts, run_after, lease_owner, lease_expires_at, checkpoint, last_error, created_at, updated_at)
|
|
8112
|
+
VALUES (?, ?, ?, 'queued', 0, ?, ?, NULL, NULL, NULL, NULL, ?, ?)`);
|
|
8113
|
+
const sqlDue = db.prepare(`SELECT * FROM ${TABLE}
|
|
8114
|
+
WHERE ((status = 'queued' AND run_after <= ?)
|
|
8115
|
+
OR (status = 'running' AND lease_expires_at IS NOT NULL AND lease_expires_at <= ?))
|
|
8116
|
+
ORDER BY run_after ASC, id ASC
|
|
8117
|
+
LIMIT ?`);
|
|
8118
|
+
const sqlDueKinds = db.prepare(`SELECT * FROM ${TABLE}
|
|
8119
|
+
WHERE ((status = 'queued' AND run_after <= ?)
|
|
8120
|
+
OR (status = 'running' AND lease_expires_at IS NOT NULL AND lease_expires_at <= ?))
|
|
8121
|
+
AND kind IN (SELECT value FROM json_each(?))
|
|
8122
|
+
ORDER BY run_after ASC, id ASC
|
|
8123
|
+
LIMIT ?`);
|
|
8124
|
+
const sqlTake = db.prepare(`UPDATE ${TABLE}
|
|
8125
|
+
SET status = 'running', attempts = attempts + 1, lease_owner = ?, lease_expires_at = ?, updated_at = ?
|
|
8126
|
+
WHERE id = ?`);
|
|
8127
|
+
const sqlComplete = db.prepare(`UPDATE ${TABLE}
|
|
8128
|
+
SET status = 'done', lease_owner = NULL, lease_expires_at = NULL, updated_at = ?
|
|
8129
|
+
WHERE id = ?`);
|
|
8130
|
+
const sqlRetry = db.prepare(`UPDATE ${TABLE}
|
|
8131
|
+
SET status = 'queued', run_after = ?, lease_owner = NULL, lease_expires_at = NULL, last_error = ?, updated_at = ?
|
|
8132
|
+
WHERE id = ?`);
|
|
8133
|
+
const sqlDead = db.prepare(`UPDATE ${TABLE}
|
|
8134
|
+
SET status = 'dead', lease_owner = NULL, lease_expires_at = NULL, last_error = ?, updated_at = ?
|
|
8135
|
+
WHERE id = ?`);
|
|
8136
|
+
const sqlCheckpoint = db.prepare(`UPDATE ${TABLE} SET checkpoint = ?, updated_at = ? WHERE id = ?`);
|
|
8137
|
+
const sqlPrune = db.prepare(`DELETE FROM ${TABLE} WHERE status IN ('done', 'dead') AND updated_at < ?`);
|
|
8138
|
+
/** 行 → 记录:payload 反序列化;坏行(解析失败)明确报 JOB_CORRUPT 而不是静默吞掉。 */
|
|
8139
|
+
const toRecord = (row) => {
|
|
8140
|
+
let payload;
|
|
8141
|
+
try {
|
|
8142
|
+
payload = JSON.parse(row.payload);
|
|
8143
|
+
} catch (cause) {
|
|
8144
|
+
throw new EngramError("JOB_CORRUPT", `jobs: 作业 ${row.id} 的 payload 不是合法 JSON`, { cause });
|
|
8145
|
+
}
|
|
8146
|
+
return {
|
|
8147
|
+
id: row.id,
|
|
8148
|
+
kind: row.kind,
|
|
8149
|
+
payload,
|
|
8150
|
+
status: row.status,
|
|
8151
|
+
attempts: row.attempts,
|
|
8152
|
+
maxAttempts: row.max_attempts,
|
|
8153
|
+
runAfter: row.run_after,
|
|
8154
|
+
leaseOwner: row.lease_owner,
|
|
8155
|
+
leaseExpiresAt: row.lease_expires_at,
|
|
8156
|
+
checkpoint: row.checkpoint,
|
|
8157
|
+
lastError: row.last_error,
|
|
8158
|
+
createdAt: row.created_at,
|
|
8159
|
+
updatedAt: row.updated_at
|
|
8160
|
+
};
|
|
8161
|
+
};
|
|
8162
|
+
return {
|
|
8163
|
+
enqueue(input) {
|
|
8164
|
+
const id = requireText(input.id, "id");
|
|
8165
|
+
const kind = requireText(input.kind, "kind");
|
|
8166
|
+
const payloadJson = serializePayload(input.payload);
|
|
8167
|
+
const maxAttempts = input.maxAttempts === void 0 ? DEFAULT_MAX_ATTEMPTS : requirePositiveInt(input.maxAttempts, "maxAttempts");
|
|
8168
|
+
const at = now();
|
|
8169
|
+
const runAfter = input.runAfter === void 0 ? at : requireNonNegativeInt(input.runAfter, "runAfter");
|
|
8170
|
+
const existing = sqlGet.get(id);
|
|
8171
|
+
if (existing !== void 0) return {
|
|
8172
|
+
job: toRecord(existing),
|
|
8173
|
+
deduped: true
|
|
8174
|
+
};
|
|
8175
|
+
let deduped = false;
|
|
8176
|
+
withTransaction(() => {
|
|
8177
|
+
if (sqlGet.get(id) !== void 0) {
|
|
8178
|
+
deduped = true;
|
|
8179
|
+
return;
|
|
8180
|
+
}
|
|
8181
|
+
sqlInsert.run(id, kind, payloadJson, maxAttempts, runAfter, at, at);
|
|
8182
|
+
});
|
|
8183
|
+
return {
|
|
8184
|
+
job: toRecord(sqlGet.get(id)),
|
|
8185
|
+
deduped
|
|
8186
|
+
};
|
|
8187
|
+
},
|
|
8188
|
+
claim(input) {
|
|
8189
|
+
const owner = requireText(input.owner, "owner");
|
|
8190
|
+
const leaseMs = requirePositiveInt(input.leaseMs, "leaseMs");
|
|
8191
|
+
const limit = input.limit === void 0 ? 1 : requirePositiveInt(input.limit, "limit");
|
|
8192
|
+
const kinds = input.kinds === void 0 ? void 0 : input.kinds.map((kind) => requireText(kind, "kinds"));
|
|
8193
|
+
const at = now();
|
|
8194
|
+
const expiresAt = at + leaseMs;
|
|
8195
|
+
const taken = [];
|
|
8196
|
+
withTransaction(() => {
|
|
8197
|
+
const candidates = kinds === void 0 ? sqlDue.all(at, at, limit) : sqlDueKinds.all(at, at, JSON.stringify(kinds), limit);
|
|
8198
|
+
for (const row of candidates) {
|
|
8199
|
+
sqlTake.run(owner, expiresAt, at, row.id);
|
|
8200
|
+
try {
|
|
8201
|
+
taken.push(toRecord(sqlGet.get(row.id)));
|
|
8202
|
+
} catch (error) {
|
|
8203
|
+
if (!(error instanceof EngramError && error.code === "JOB_CORRUPT")) throw error;
|
|
8204
|
+
const cause = error.cause;
|
|
8205
|
+
const causeText = cause instanceof Error ? cause.message : cause === void 0 ? "" : String(cause);
|
|
8206
|
+
sqlDead.run(causeText === "" ? error.message : `${error.message}(${causeText})`, at, row.id);
|
|
8207
|
+
}
|
|
8208
|
+
}
|
|
8209
|
+
});
|
|
8210
|
+
return taken;
|
|
8211
|
+
},
|
|
8212
|
+
complete(input) {
|
|
8213
|
+
const id = requireText(input.id, "id");
|
|
8214
|
+
requireText(input.owner, "owner");
|
|
8215
|
+
const at = now();
|
|
8216
|
+
withTransaction(() => {
|
|
8217
|
+
const row = sqlGet.get(id);
|
|
8218
|
+
if (row === void 0) throw new EngramError("JOB_NOT_FOUND", `jobs: 作业 ${id} 不存在`);
|
|
8219
|
+
requireOwnedRunning(row, input, at);
|
|
8220
|
+
sqlComplete.run(at, id);
|
|
8221
|
+
});
|
|
8222
|
+
return toRecord(sqlGet.get(id));
|
|
8223
|
+
},
|
|
8224
|
+
fail(input) {
|
|
8225
|
+
const id = requireText(input.id, "id");
|
|
8226
|
+
requireText(input.owner, "owner");
|
|
8227
|
+
const error = requireText(input.error, "error");
|
|
8228
|
+
const retryDelayMs = input.retryDelayMs === void 0 ? 0 : requireNonNegativeInt(input.retryDelayMs, "retryDelayMs");
|
|
8229
|
+
const at = now();
|
|
8230
|
+
withTransaction(() => {
|
|
8231
|
+
const row = sqlGet.get(id);
|
|
8232
|
+
if (row === void 0) throw new EngramError("JOB_NOT_FOUND", `jobs: 作业 ${id} 不存在`);
|
|
8233
|
+
requireOwnedRunning(row, input, at);
|
|
8234
|
+
if (row.attempts >= row.max_attempts) sqlDead.run(error, at, id);
|
|
8235
|
+
else sqlRetry.run(at + retryDelayMs, error, at, id);
|
|
8236
|
+
});
|
|
8237
|
+
return toRecord(sqlGet.get(id));
|
|
8238
|
+
},
|
|
8239
|
+
checkpoint(input) {
|
|
8240
|
+
const id = requireText(input.id, "id");
|
|
8241
|
+
requireText(input.owner, "owner");
|
|
8242
|
+
const mark = requireText(input.checkpoint, "checkpoint");
|
|
8243
|
+
const at = now();
|
|
8244
|
+
withTransaction(() => {
|
|
8245
|
+
const row = sqlGet.get(id);
|
|
8246
|
+
if (row === void 0) throw new EngramError("JOB_NOT_FOUND", `jobs: 作业 ${id} 不存在`);
|
|
8247
|
+
requireOwnedRunning(row, input, at);
|
|
8248
|
+
sqlCheckpoint.run(mark, at, id);
|
|
8249
|
+
});
|
|
8250
|
+
return toRecord(sqlGet.get(id));
|
|
8251
|
+
},
|
|
8252
|
+
get(id) {
|
|
8253
|
+
const row = sqlGet.get(requireText(id, "id"));
|
|
8254
|
+
return row === void 0 ? void 0 : toRecord(row);
|
|
8255
|
+
},
|
|
8256
|
+
prune(olderThanMs) {
|
|
8257
|
+
const thresholdMs = requireNonNegativeInt(olderThanMs, "olderThanMs");
|
|
8258
|
+
const cutoff = now() - thresholdMs;
|
|
8259
|
+
const result = sqlPrune.run(cutoff);
|
|
8260
|
+
return Number(result.changes);
|
|
8261
|
+
},
|
|
8262
|
+
close() {
|
|
8263
|
+
db.close();
|
|
8264
|
+
}
|
|
8265
|
+
};
|
|
8266
|
+
}
|
|
8267
|
+
// src/jobs/reflect-worker.ts
|
|
8268
|
+
/**
|
|
8269
|
+
* reflect 持久调度器:确定性入队、串行认领、限时执行、失败重试与停止恢复。
|
|
8270
|
+
* queue 的生命周期由宿主管理;本 worker 只拥有定时器与自己的执行取消信号。
|
|
8271
|
+
* @module @kenz1117/dsh-engram/jobs/reflect-worker
|
|
8272
|
+
*/
|
|
8273
|
+
function positive(value, fallback, field) {
|
|
8274
|
+
const result = value ?? fallback;
|
|
8275
|
+
if (!Number.isInteger(result) || result <= 0) throw new Error(`reflect worker: ${field} 必须是正整数`);
|
|
8276
|
+
return result;
|
|
8277
|
+
}
|
|
8278
|
+
function nonNegative(value, fallback, field) {
|
|
8279
|
+
const result = value ?? fallback;
|
|
8280
|
+
if (!Number.isInteger(result) || result < 0) throw new Error(`reflect worker: ${field} 必须是非负整数`);
|
|
8281
|
+
return result;
|
|
8282
|
+
}
|
|
8283
|
+
function text(value) {
|
|
8284
|
+
return typeof value === "string" && value.trim() !== "";
|
|
8285
|
+
}
|
|
8286
|
+
function parsePayload(value) {
|
|
8287
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) throw new Error("reflect job payload 必须是对象");
|
|
8288
|
+
const raw = value;
|
|
8289
|
+
const route = raw.route;
|
|
8290
|
+
if (!text(raw.sessionId) || raw.scope !== "user" && raw.scope !== "project") throw new Error("reflect job payload 缺 sessionId 或 scope 非法");
|
|
8291
|
+
if (raw.cwd !== void 0 && !text(raw.cwd)) throw new Error("reflect job payload.cwd 必须是非空字符串");
|
|
8292
|
+
if (raw.eventSeq !== void 0 && (typeof raw.eventSeq !== "number" || !Number.isInteger(raw.eventSeq) || raw.eventSeq < 0)) throw new Error("reflect job payload.eventSeq 必须是非负整数");
|
|
8293
|
+
if (route === void 0 || !text(route.provider) || !text(route.model)) throw new Error("reflect job payload.route 缺 provider/model");
|
|
8294
|
+
return {
|
|
8295
|
+
sessionId: raw.sessionId,
|
|
8296
|
+
scope: raw.scope,
|
|
8297
|
+
...raw.cwd === void 0 ? {} : { cwd: raw.cwd },
|
|
8298
|
+
...raw.eventSeq === void 0 ? {} : { eventSeq: raw.eventSeq },
|
|
8299
|
+
route: {
|
|
8300
|
+
provider: route.provider,
|
|
8301
|
+
model: route.model
|
|
8302
|
+
}
|
|
8303
|
+
};
|
|
8304
|
+
}
|
|
8305
|
+
function errorText(error) {
|
|
8306
|
+
return error instanceof Error ? error.message : String(error);
|
|
8307
|
+
}
|
|
8308
|
+
var DurableReflectWorker = class {
|
|
8309
|
+
queue;
|
|
8310
|
+
execute;
|
|
8311
|
+
onError;
|
|
8312
|
+
owner;
|
|
8313
|
+
intervalMs;
|
|
8314
|
+
leaseMs;
|
|
8315
|
+
retryDelayMs;
|
|
8316
|
+
timeoutMs;
|
|
8317
|
+
externalSignal;
|
|
8318
|
+
abort = new AbortController();
|
|
8319
|
+
currentAbort;
|
|
8320
|
+
timer;
|
|
8321
|
+
activePump;
|
|
8322
|
+
stopped = false;
|
|
8323
|
+
constructor(options) {
|
|
8324
|
+
this.queue = options.queue;
|
|
8325
|
+
this.execute = options.execute;
|
|
8326
|
+
this.onError = options.onError;
|
|
8327
|
+
this.owner = options.owner ?? `reflect-${randomUUID()}`;
|
|
8328
|
+
this.intervalMs = positive(options.intervalMs, 1e3, "intervalMs");
|
|
8329
|
+
this.leaseMs = positive(options.leaseMs, 18e4, "leaseMs");
|
|
8330
|
+
this.retryDelayMs = nonNegative(options.retryDelayMs, 3e4, "retryDelayMs");
|
|
8331
|
+
this.timeoutMs = positive(options.timeoutMs, 12e4, "timeoutMs");
|
|
8332
|
+
this.externalSignal = options.signal;
|
|
8333
|
+
}
|
|
8334
|
+
enqueue(payload) {
|
|
8335
|
+
const checked = parsePayload(payload);
|
|
8336
|
+
return this.queue.enqueue({
|
|
8337
|
+
id: checked.eventSeq === void 0 ? `reflect:${checked.sessionId}:${checked.scope}` : `reflect:${checked.sessionId}:${checked.scope}:${checked.eventSeq}`,
|
|
8338
|
+
kind: "reflect",
|
|
8339
|
+
payload: checked,
|
|
8340
|
+
maxAttempts: 3
|
|
8341
|
+
});
|
|
8342
|
+
}
|
|
8343
|
+
start() {
|
|
8344
|
+
if (this.stopped) throw new Error("reflect worker: stop 后不可重新 start");
|
|
8345
|
+
if (this.timer !== void 0) return;
|
|
8346
|
+
this.pump();
|
|
8347
|
+
this.timer = setInterval(() => {
|
|
8348
|
+
this.pump();
|
|
8349
|
+
}, this.intervalMs);
|
|
8350
|
+
this.timer.unref();
|
|
8351
|
+
}
|
|
8352
|
+
/** 重入共享同一个 pump;executor 在任意时刻至多一个在跑。 */
|
|
8353
|
+
pump() {
|
|
8354
|
+
if (this.activePump !== void 0) return this.activePump;
|
|
8355
|
+
const active = this.drain();
|
|
8356
|
+
this.activePump = active;
|
|
8357
|
+
const clear = () => {
|
|
8358
|
+
if (this.activePump === active) this.activePump = void 0;
|
|
8359
|
+
};
|
|
8360
|
+
active.then(clear, clear);
|
|
8361
|
+
return active;
|
|
8362
|
+
}
|
|
8363
|
+
get cancelled() {
|
|
8364
|
+
return this.stopped || this.abort.signal.aborted || this.externalSignal?.aborted === true;
|
|
8365
|
+
}
|
|
8366
|
+
report(error, job) {
|
|
8367
|
+
if (this.onError === void 0) return;
|
|
8368
|
+
try {
|
|
8369
|
+
this.onError(error instanceof Error ? error : new Error(String(error)), job);
|
|
8370
|
+
} catch {}
|
|
8371
|
+
}
|
|
8372
|
+
async drain() {
|
|
8373
|
+
try {
|
|
8374
|
+
while (!this.cancelled) {
|
|
8375
|
+
const [job] = this.queue.claim({
|
|
8376
|
+
owner: this.owner,
|
|
8377
|
+
kinds: ["reflect"],
|
|
8378
|
+
leaseMs: this.leaseMs
|
|
8379
|
+
});
|
|
8380
|
+
if (job === void 0) break;
|
|
8381
|
+
const controller = new AbortController();
|
|
8382
|
+
this.currentAbort = controller;
|
|
8383
|
+
const signals = [
|
|
8384
|
+
controller.signal,
|
|
8385
|
+
this.abort.signal,
|
|
8386
|
+
AbortSignal.timeout(this.timeoutMs)
|
|
8387
|
+
];
|
|
8388
|
+
if (this.externalSignal !== void 0) signals.push(this.externalSignal);
|
|
8389
|
+
try {
|
|
8390
|
+
const payload = parsePayload(job.payload);
|
|
8391
|
+
await this.execute(payload, AbortSignal.any(signals));
|
|
8392
|
+
this.queue.complete({
|
|
8393
|
+
id: job.id,
|
|
8394
|
+
owner: this.owner
|
|
8395
|
+
});
|
|
8396
|
+
} catch (error) {
|
|
8397
|
+
let reported = error;
|
|
8398
|
+
try {
|
|
8399
|
+
this.queue.fail({
|
|
8400
|
+
id: job.id,
|
|
8401
|
+
owner: this.owner,
|
|
8402
|
+
error: errorText(error),
|
|
8403
|
+
retryDelayMs: this.retryDelayMs
|
|
8404
|
+
});
|
|
8405
|
+
} catch (queueError) {
|
|
8406
|
+
reported = queueError;
|
|
8407
|
+
}
|
|
8408
|
+
this.report(reported, job);
|
|
8409
|
+
} finally {
|
|
8410
|
+
this.currentAbort = void 0;
|
|
8411
|
+
}
|
|
8412
|
+
}
|
|
8413
|
+
} catch (error) {
|
|
8414
|
+
this.report(error);
|
|
8415
|
+
}
|
|
8416
|
+
}
|
|
8417
|
+
/** 停止认领新作业,取消在途执行并等待 queue 状态收束;queue 保持打开。 */
|
|
8418
|
+
async stop() {
|
|
8419
|
+
if (this.stopped) {
|
|
8420
|
+
await this.activePump;
|
|
8421
|
+
return;
|
|
8422
|
+
}
|
|
8423
|
+
this.stopped = true;
|
|
8424
|
+
if (this.timer !== void 0) clearInterval(this.timer);
|
|
8425
|
+
this.timer = void 0;
|
|
8426
|
+
this.abort.abort();
|
|
8427
|
+
this.currentAbort?.abort();
|
|
8428
|
+
await this.activePump;
|
|
8429
|
+
}
|
|
8430
|
+
};
|
|
8431
|
+
// src/jobs/host-reflect.ts
|
|
8432
|
+
/**
|
|
8433
|
+
* DSH 宿主侧的 reflect 作业工厂:打开持久队列,执行真实巩固,并统一停止/关闭。
|
|
8434
|
+
* @module @kenz1117/dsh-engram/jobs/host-reflect
|
|
8435
|
+
*/
|
|
8436
|
+
/** 终态作业保留窗口:与主库 op_log 同款 366 天,过窗 done/dead 由 prune 清除。 */
|
|
8437
|
+
const PRUNE_RETENTION_MS = 316224e5;
|
|
8438
|
+
/** prune 班车间隔:24 小时一班,启动时先跑一次。 */
|
|
8439
|
+
const PRUNE_INTERVAL_MS = 864e5;
|
|
8440
|
+
/** 启动真实 reflect worker;stop 先取消在途执行,再关闭独立 jobs.db。 */
|
|
8441
|
+
async function startAutoReflectWorker(options) {
|
|
8442
|
+
const queue = await openJobQueue(join(options.dbDir, "jobs.db"));
|
|
8443
|
+
try {
|
|
8444
|
+
queue.prune(PRUNE_RETENTION_MS);
|
|
8445
|
+
} catch (error) {
|
|
8446
|
+
console.warn(message("core.warnReflectFailed", { scope: "prune" }), error);
|
|
8447
|
+
}
|
|
8448
|
+
const pruneTimer = setInterval(() => {
|
|
8449
|
+
try {
|
|
8450
|
+
queue.prune(PRUNE_RETENTION_MS);
|
|
8451
|
+
} catch {}
|
|
8452
|
+
}, PRUNE_INTERVAL_MS);
|
|
8453
|
+
pruneTimer.unref();
|
|
8454
|
+
const worker = new DurableReflectWorker({
|
|
8455
|
+
queue,
|
|
8456
|
+
execute: async (payload, signal) => {
|
|
8457
|
+
const scope = payload.scope;
|
|
8458
|
+
if (scope === "project" && (payload.cwd === void 0 || !existsSync(join(options.dbDir, options.dbNameForCwd(payload.cwd))))) return;
|
|
8459
|
+
const store = scope === "user" ? await options.openStore("user") : await options.openProjectStore(payload.cwd);
|
|
8460
|
+
const outcome = await reflectObservations({
|
|
8461
|
+
store,
|
|
8462
|
+
embedder: await options.embedder(),
|
|
8463
|
+
scope,
|
|
8464
|
+
call: (params) => options.call(payload.sessionId, params),
|
|
8465
|
+
logRequest: (data) => {
|
|
8466
|
+
store.audit("reflect-request", "AUX", JSON.stringify(data)).catch(() => {});
|
|
8467
|
+
},
|
|
8468
|
+
route: payload.route,
|
|
8469
|
+
signal
|
|
8470
|
+
});
|
|
8471
|
+
if (outcome.formed + outcome.refined + outcome.confirmed + outcome.refuted > 0) console.log(message("core.logReflectDone", {
|
|
8472
|
+
scope,
|
|
8473
|
+
formed: outcome.formed,
|
|
8474
|
+
refined: outcome.refined,
|
|
8475
|
+
confirmed: outcome.confirmed,
|
|
8476
|
+
refuted: outcome.refuted
|
|
8477
|
+
}));
|
|
8478
|
+
if (outcome.dropped > 0) console.warn(message("core.warnReflectBatchDropped", { count: outcome.dropped }));
|
|
8479
|
+
},
|
|
8480
|
+
onError: (error, job) => {
|
|
8481
|
+
const scope = (job?.payload)?.scope;
|
|
8482
|
+
console.warn(message("core.warnReflectFailed", { scope: typeof scope === "string" ? scope : "unknown" }), error);
|
|
8483
|
+
}
|
|
8484
|
+
});
|
|
8485
|
+
worker.start();
|
|
8486
|
+
return {
|
|
8487
|
+
enqueue: (payload) => {
|
|
8488
|
+
worker.enqueue(payload);
|
|
8489
|
+
},
|
|
8490
|
+
pump: () => worker.pump(),
|
|
8491
|
+
stop: async () => {
|
|
8492
|
+
clearInterval(pruneTimer);
|
|
8493
|
+
try {
|
|
8494
|
+
await worker.stop();
|
|
8495
|
+
} finally {
|
|
8496
|
+
queue.close();
|
|
8497
|
+
}
|
|
8498
|
+
}
|
|
8499
|
+
};
|
|
8500
|
+
}
|
|
8501
|
+
// src/application/memory-commands.ts
|
|
8502
|
+
/**
|
|
8503
|
+
* 更新一条记忆。不存在返回 undefined,让工具与 HTTP 各自保留既有错误协议。
|
|
8504
|
+
* 内容变化必须产生新向量;embedder 不可用时显式清空旧向量,禁止语义索引停在旧正文。
|
|
8505
|
+
*/
|
|
8506
|
+
async function updateMemory(deps, input) {
|
|
8507
|
+
const content = redactSecrets(sanitizeProtocolText(input.content));
|
|
8508
|
+
if (content.trim() === "") throw new EngramError("EMPTY_CONTENT", message("tool.updateContentEmptyAfterSanitize"));
|
|
8509
|
+
const old = await deps.store.get(input.id) ?? await deps.store.getMemory(input.id);
|
|
8510
|
+
if (old === void 0) return void 0;
|
|
8511
|
+
let embedding = null;
|
|
8512
|
+
const embedder = await deps.embedder;
|
|
8513
|
+
if (embedder !== void 0) embedding = (await embedder.embed([content.trim()]))[0] ?? null;
|
|
8514
|
+
return deps.store.update({
|
|
8515
|
+
id: old.id,
|
|
8516
|
+
scope: input.scope,
|
|
8517
|
+
kind: input.kind ?? old.kind,
|
|
8518
|
+
content,
|
|
8519
|
+
embedding,
|
|
8520
|
+
...input.importance === void 0 ? {} : { importance: Math.min(1, Math.max(0, input.importance)) },
|
|
8521
|
+
...input.imagery === void 0 ? {} : { imagery: input.imagery }
|
|
8522
|
+
});
|
|
8523
|
+
}
|
|
7896
8524
|
// src/store/import.ts
|
|
7897
8525
|
const SCOPES = [
|
|
7898
8526
|
"user",
|
|
@@ -7950,6 +8578,7 @@ const parseRecord = (raw, index) => {
|
|
|
7950
8578
|
if (imagery !== void 0 && imagery !== null && !(isObj(imagery) && isStr(imagery.caption) && Array.isArray(imagery.sensoryTags) && isNum(imagery.emotionalValence) && typeof imagery.provisional === "boolean")) return `records[${index}].imagery 非法`;
|
|
7951
8579
|
return {
|
|
7952
8580
|
id: asMemoryId(id),
|
|
8581
|
+
memoryId: asMemoryId(isStr(raw.memoryId) && raw.memoryId !== "" ? raw.memoryId : id),
|
|
7953
8582
|
scope: raw.scope,
|
|
7954
8583
|
kind: raw.kind,
|
|
7955
8584
|
content,
|
|
@@ -8112,6 +8741,7 @@ function parseMarkdownExport(raw, scope) {
|
|
|
8112
8741
|
if (!Number.isFinite(importance)) continue;
|
|
8113
8742
|
records.push({
|
|
8114
8743
|
id: asMemoryId(id),
|
|
8744
|
+
memoryId: asMemoryId(id),
|
|
8115
8745
|
scope,
|
|
8116
8746
|
kind,
|
|
8117
8747
|
content,
|
|
@@ -8517,6 +9147,19 @@ function joinUrl(base, relative) {
|
|
|
8517
9147
|
function systemOneEndpoint(baseUrl) {
|
|
8518
9148
|
return joinUrl(baseUrl, "systemone");
|
|
8519
9149
|
}
|
|
9150
|
+
/**
|
|
9151
|
+
* 宿主凭据只能发往 provider 配置中已信任的 origin。面板可以覆盖同源路径,
|
|
9152
|
+
* 但不能把存储在 DSH 凭据库里的 Bearer key 指向另一个服务。
|
|
9153
|
+
*/
|
|
9154
|
+
function isTrustedJevEndpoint(baseUrl, endpoint) {
|
|
9155
|
+
try {
|
|
9156
|
+
const trusted = new URL(baseUrl);
|
|
9157
|
+
const candidate = new URL(endpoint);
|
|
9158
|
+
return (candidate.protocol === "http:" || candidate.protocol === "https:") && candidate.username === "" && candidate.password === "" && candidate.origin === trusted.origin;
|
|
9159
|
+
} catch {
|
|
9160
|
+
return false;
|
|
9161
|
+
}
|
|
9162
|
+
}
|
|
8520
9163
|
/** Дескриптор конфигурации хоста по namespace (например `llm-pi-ai`). */
|
|
8521
9164
|
async function readSettingsValue(ctx, ns) {
|
|
8522
9165
|
const settings = ctx.get("settings");
|
|
@@ -8702,7 +9345,9 @@ async function resolveJevConnection(ctx, providerId, model, timeoutMs, endpointO
|
|
|
8702
9345
|
if (typeof rekvisites === "string") return rekvisites;
|
|
8703
9346
|
const chosenModel = model.trim() !== "" ? model.trim() : pickJevModel(rekvisites.modelIds);
|
|
8704
9347
|
if (chosenModel === void 0) return message("misc.jevModelNotPicked", { provider: providerId });
|
|
8705
|
-
const
|
|
9348
|
+
const chosen = endpointOverride !== void 0 && endpointOverride.trim() !== "" ? endpointOverride.trim() : void 0;
|
|
9349
|
+
if (chosen !== void 0 && (rekvisites.baseUrl === void 0 || !isTrustedJevEndpoint(rekvisites.baseUrl, chosen))) return message("misc.jevEndpointOriginMismatch", { provider: providerId });
|
|
9350
|
+
const endpoint = chosen ?? (rekvisites.baseUrl === void 0 ? void 0 : systemOneEndpoint(rekvisites.baseUrl));
|
|
8706
9351
|
if (endpoint === void 0) return message("misc.jevProviderHasNoBaseUrl", { provider: providerId });
|
|
8707
9352
|
if (rekvisites.apiKey === "") return message("misc.jevProviderKeyMissing", {
|
|
8708
9353
|
provider: providerId,
|
|
@@ -9927,6 +10572,31 @@ function registerEngramRoutes(ctx, deps) {
|
|
|
9927
10572
|
}))) });
|
|
9928
10573
|
return;
|
|
9929
10574
|
}
|
|
10575
|
+
if (req.method === "GET" && route === "injections") {
|
|
10576
|
+
const scope = scopeOf$1(url.searchParams.get("scope"), "user");
|
|
10577
|
+
const sessionId = url.searchParams.get("sessionId") ?? void 0;
|
|
10578
|
+
const limit = Math.min(200, Math.max(1, Number(url.searchParams.get("limit") ?? 50) || 50));
|
|
10579
|
+
json(res, 200, { items: await (await storeFor(scope)).listInjections({
|
|
10580
|
+
...sessionId === void 0 ? {} : { sessionId },
|
|
10581
|
+
limit
|
|
10582
|
+
}) });
|
|
10583
|
+
return;
|
|
10584
|
+
}
|
|
10585
|
+
if (req.method === "GET" && route === "injection") {
|
|
10586
|
+
const scope = scopeOf$1(url.searchParams.get("scope"), "user");
|
|
10587
|
+
const id = url.searchParams.get("id");
|
|
10588
|
+
if (id === null || id === "") {
|
|
10589
|
+
json(res, 400, { error: "id required" });
|
|
10590
|
+
return;
|
|
10591
|
+
}
|
|
10592
|
+
const snapshot = await (await storeFor(scope)).getInjection(id);
|
|
10593
|
+
if (snapshot === void 0) {
|
|
10594
|
+
json(res, 404, { error: "injection not found" });
|
|
10595
|
+
return;
|
|
10596
|
+
}
|
|
10597
|
+
json(res, 200, snapshot);
|
|
10598
|
+
return;
|
|
10599
|
+
}
|
|
9930
10600
|
if (req.method === "GET" && route === "list") {
|
|
9931
10601
|
const scope = scopeOf$1(url.searchParams.get("scope"), "user");
|
|
9932
10602
|
const status = url.searchParams.get("status");
|
|
@@ -9974,6 +10644,8 @@ function registerEngramRoutes(ctx, deps) {
|
|
|
9974
10644
|
degraded,
|
|
9975
10645
|
hits: hits.map((hit) => ({
|
|
9976
10646
|
id: hit.record.id,
|
|
10647
|
+
memoryId: hit.record.memoryId,
|
|
10648
|
+
revisionId: hit.record.id,
|
|
9977
10649
|
score: hit.score,
|
|
9978
10650
|
via: hit.via,
|
|
9979
10651
|
...hit.viaEdge === void 0 ? {} : { viaEdge: hit.viaEdge },
|
|
@@ -9986,6 +10658,46 @@ function registerEngramRoutes(ctx, deps) {
|
|
|
9986
10658
|
});
|
|
9987
10659
|
return;
|
|
9988
10660
|
}
|
|
10661
|
+
if (req.method === "GET" && route === "governance") {
|
|
10662
|
+
const scope = scopeOf$1(url.searchParams.get("scope"), "user");
|
|
10663
|
+
const limit = Math.min(100, Math.max(1, Number(url.searchParams.get("limit") ?? 50) || 50));
|
|
10664
|
+
const all = await (await storeFor(scope)).listContradictions(200);
|
|
10665
|
+
json(res, 200, {
|
|
10666
|
+
scope,
|
|
10667
|
+
items: all.slice(0, limit),
|
|
10668
|
+
total: all.length
|
|
10669
|
+
});
|
|
10670
|
+
return;
|
|
10671
|
+
}
|
|
10672
|
+
if (req.method === "POST" && route === "governance") {
|
|
10673
|
+
if (!guardWrite(req, res, deps.panelAccess === "lan")) return;
|
|
10674
|
+
const body = await readBody(req, res, deps.language);
|
|
10675
|
+
if (body === null) return;
|
|
10676
|
+
const scope = scopeOf$1(typeof body.scope === "string" ? body.scope : null, "user");
|
|
10677
|
+
const action = body.action;
|
|
10678
|
+
if (action !== "dismiss" && action !== "forget-loser") {
|
|
10679
|
+
json(res, 400, { error: "action must be dismiss or forget-loser" });
|
|
10680
|
+
return;
|
|
10681
|
+
}
|
|
10682
|
+
if (typeof body.firstId !== "string" || typeof body.secondId !== "string" || body.firstId === "" || body.secondId === "") {
|
|
10683
|
+
json(res, 400, { error: "firstId and secondId required" });
|
|
10684
|
+
return;
|
|
10685
|
+
}
|
|
10686
|
+
if (action === "forget-loser" && (typeof body.loserId !== "string" || body.loserId === "")) {
|
|
10687
|
+
json(res, 400, { error: "loserId required" });
|
|
10688
|
+
return;
|
|
10689
|
+
}
|
|
10690
|
+
json(res, 200, {
|
|
10691
|
+
ok: true,
|
|
10692
|
+
result: await (await storeFor(scope)).resolveContradiction({
|
|
10693
|
+
firstId: body.firstId,
|
|
10694
|
+
secondId: body.secondId,
|
|
10695
|
+
action,
|
|
10696
|
+
...typeof body.loserId === "string" ? { loserId: body.loserId } : {}
|
|
10697
|
+
})
|
|
10698
|
+
});
|
|
10699
|
+
return;
|
|
10700
|
+
}
|
|
9989
10701
|
if (req.method === "GET" && route === "observations") {
|
|
9990
10702
|
const scope = scopeOf$1(url.searchParams.get("scope"), "user");
|
|
9991
10703
|
const status = url.searchParams.get("status");
|
|
@@ -10923,25 +11635,36 @@ function registerEngramRoutes(ctx, deps) {
|
|
|
10923
11635
|
json(res, 400, { error: "content required" });
|
|
10924
11636
|
return;
|
|
10925
11637
|
}
|
|
10926
|
-
|
|
10927
|
-
|
|
10928
|
-
|
|
11638
|
+
try {
|
|
11639
|
+
const record = await updateMemory({
|
|
11640
|
+
store,
|
|
11641
|
+
embedder: deps.embedder
|
|
11642
|
+
}, {
|
|
11643
|
+
id: body.id,
|
|
11644
|
+
scope,
|
|
11645
|
+
content: body.content,
|
|
11646
|
+
...typeof body.kind === "string" && [
|
|
11647
|
+
"fact",
|
|
11648
|
+
"preference",
|
|
11649
|
+
"decision",
|
|
11650
|
+
"episode",
|
|
11651
|
+
"skill"
|
|
11652
|
+
].includes(body.kind) ? { kind: body.kind } : {},
|
|
11653
|
+
...typeof body.importance === "number" && Number.isFinite(body.importance) ? { importance: body.importance } : {}
|
|
11654
|
+
});
|
|
11655
|
+
if (record === void 0) {
|
|
11656
|
+
json(res, 404, { error: messageIn(deps.language, "misc.recordNotFound", { id: body.id }) });
|
|
11657
|
+
return;
|
|
11658
|
+
}
|
|
11659
|
+
json(res, 200, { record });
|
|
10929
11660
|
return;
|
|
11661
|
+
} catch (error) {
|
|
11662
|
+
if (error instanceof EngramError && error.code === "EMPTY_CONTENT") {
|
|
11663
|
+
json(res, 400, { error: error.message });
|
|
11664
|
+
return;
|
|
11665
|
+
}
|
|
11666
|
+
throw error;
|
|
10930
11667
|
}
|
|
10931
|
-
json(res, 200, { record: await store.update({
|
|
10932
|
-
id: body.id,
|
|
10933
|
-
scope,
|
|
10934
|
-
kind: typeof body.kind === "string" && [
|
|
10935
|
-
"fact",
|
|
10936
|
-
"preference",
|
|
10937
|
-
"decision",
|
|
10938
|
-
"episode",
|
|
10939
|
-
"skill"
|
|
10940
|
-
].includes(body.kind) ? body.kind : old.kind,
|
|
10941
|
-
content: body.content,
|
|
10942
|
-
...typeof body.importance === "number" && Number.isFinite(body.importance) ? { importance: Math.min(1, Math.max(0, body.importance)) } : {}
|
|
10943
|
-
}) });
|
|
10944
|
-
return;
|
|
10945
11668
|
}
|
|
10946
11669
|
json(res, 200, { record: route === "forget" ? await store.forget(body.id) : await store.restore(body.id) });
|
|
10947
11670
|
return;
|
|
@@ -12178,7 +12901,7 @@ function placardImprovementHint(score) {
|
|
|
12178
12901
|
*/
|
|
12179
12902
|
/** 当前 schema 版本;结构性变更必须 +1。可空列与伴随表走增量迁移(见 openEngramStore 的迁移段)。
|
|
12180
12903
|
* v12→v13 是纯数据迁移(历史房名归一化),但同样借版本号作为「只跑一次」的开关。 */
|
|
12181
|
-
const SCHEMA_VERSION =
|
|
12904
|
+
const SCHEMA_VERSION = 15;
|
|
12182
12905
|
/** 增量迁移表:key 为起始版本,value 为升到下一版本的 SQL(可多语句)。
|
|
12183
12906
|
* v2 → v3:nodes 补可空列 outcome(使用效果回报)。
|
|
12184
12907
|
* v3 → v4:新增 nodes_revisions 修订表(update 归档旧条目时的内容快照)。
|
|
@@ -12206,7 +12929,10 @@ const SCHEMA_VERSION = 13;
|
|
|
12206
12929
|
* v12 → v13:历史房名归一化——0.8.12 及更早把**显示名**写进 slot_room(「事实厅」),
|
|
12207
12930
|
* 语言中立 id 启用后同一逻辑房会裂成两间(容量 9 变 18、tour_routes 按 room 断开)。
|
|
12208
12931
|
* 升级时一次性改写为 id,含扩建房(「事实厅-2」→「fact-2」);已经是 id 的库零行改动,
|
|
12209
|
-
* SQL 自身幂等。手动脚本 scripts/migrate-room-ids.mjs 保留为兜底。
|
|
12932
|
+
* SQL 自身幂等。手动脚本 scripts/migrate-room-ids.mjs 保留为兜底。
|
|
12933
|
+
* v13 → v14:injection_snapshots 持久化实际注入的最终包;列表只取 head,正文按 id 读取。
|
|
12934
|
+
* v14 → v15:稳定逻辑身份 memory_id——nodes.id 保留为 revision id;存量行 memory_id=id,
|
|
12935
|
+
* update 继承旧 memory_id,修订表补 memory_id 使同一逻辑身份的完整修订链可查。 */
|
|
12210
12936
|
const MIGRATIONS = {
|
|
12211
12937
|
"2": "ALTER TABLE nodes ADD COLUMN outcome TEXT",
|
|
12212
12938
|
"3": `CREATE TABLE IF NOT EXISTS nodes_revisions (
|
|
@@ -12257,7 +12983,19 @@ const MIGRATIONS = {
|
|
|
12257
12983
|
UPDATE nodes SET slot_room = replace(slot_room, '偏好阁', 'preference') WHERE slot_room LIKE '偏好阁%';
|
|
12258
12984
|
UPDATE nodes SET slot_room = replace(slot_room, '决策堂', 'decision') WHERE slot_room LIKE '决策堂%';
|
|
12259
12985
|
UPDATE nodes SET slot_room = replace(slot_room, '往事廊', 'episode') WHERE slot_room LIKE '往事廊%';
|
|
12260
|
-
UPDATE nodes SET slot_room = replace(slot_room, '技法坊', 'skill') WHERE slot_room LIKE '技法坊%'
|
|
12986
|
+
UPDATE nodes SET slot_room = replace(slot_room, '技法坊', 'skill') WHERE slot_room LIKE '技法坊%';`,
|
|
12987
|
+
"14": `ALTER TABLE nodes ADD COLUMN memory_id TEXT;
|
|
12988
|
+
UPDATE nodes SET memory_id = id WHERE memory_id IS NULL;
|
|
12989
|
+
ALTER TABLE nodes_revisions ADD COLUMN memory_id TEXT;
|
|
12990
|
+
UPDATE nodes_revisions SET memory_id = (
|
|
12991
|
+
SELECT nodes.memory_id FROM nodes WHERE nodes.id = nodes_revisions.node_id
|
|
12992
|
+
) WHERE memory_id IS NULL;
|
|
12993
|
+
CREATE INDEX IF NOT EXISTS nodes_memory_created ON nodes (memory_id, created_at);`,
|
|
12994
|
+
"13": `CREATE TABLE IF NOT EXISTS injection_snapshots (
|
|
12995
|
+
id TEXT PRIMARY KEY, session_id TEXT NOT NULL, packet_hash TEXT NOT NULL,
|
|
12996
|
+
packet TEXT NOT NULL, memory_ids_json TEXT NOT NULL, has_curated INTEGER NOT NULL,
|
|
12997
|
+
compaction_count INTEGER NOT NULL, created_at INTEGER NOT NULL);
|
|
12998
|
+
CREATE INDEX IF NOT EXISTS injection_session_created ON injection_snapshots (session_id, created_at);`
|
|
12261
12999
|
};
|
|
12262
13000
|
/** RRF 融合常数:score = Σ 1/(K + rank)。 */
|
|
12263
13001
|
const RRF_K = 60;
|
|
@@ -12269,12 +13007,29 @@ const CONTRADICTION_COSINE = .88;
|
|
|
12269
13007
|
const RANK_POOL = 64;
|
|
12270
13008
|
/** 一跳扩展引入的邻居上限。 */
|
|
12271
13009
|
const EXPANSION_LIMIT = 32;
|
|
12272
|
-
/**
|
|
13010
|
+
/** 置信度增量:真实反馈专用——效果回报 success 与写入期 MERGE 并入强化(检索曝光不算反馈)。 */
|
|
12273
13011
|
const CONFIDENCE_BUMP = .05;
|
|
12274
13012
|
/** 效果回报降权:failure 回报的置信度扣减(success 复用 CONFIDENCE_BUMP)。 */
|
|
12275
13013
|
const OUTCOME_PENALTY = .1;
|
|
12276
13014
|
/** 审计视图返回的操作日志条数上限。 */
|
|
12277
13015
|
const REVIEW_LOG_LIMIT = 20;
|
|
13016
|
+
function rowToInjectionHead(row) {
|
|
13017
|
+
let memoryIds = [];
|
|
13018
|
+
try {
|
|
13019
|
+
const parsed = JSON.parse(row.memory_ids_json);
|
|
13020
|
+
if (Array.isArray(parsed)) memoryIds = parsed.filter((id) => typeof id === "string").map(asMemoryId);
|
|
13021
|
+
} catch {}
|
|
13022
|
+
return {
|
|
13023
|
+
id: row.id,
|
|
13024
|
+
sessionId: row.session_id,
|
|
13025
|
+
packetHash: row.packet_hash,
|
|
13026
|
+
memoryIds,
|
|
13027
|
+
hasCurated: row.has_curated === 1,
|
|
13028
|
+
compactionCount: row.compaction_count,
|
|
13029
|
+
packetBytes: row.packet_bytes,
|
|
13030
|
+
createdAt: row.created_at
|
|
13031
|
+
};
|
|
13032
|
+
}
|
|
12278
13033
|
/** 把意象铭牌序列化为 JSON(缺省序列化为 null,落库)。 */
|
|
12279
13034
|
function imageryToJson(imagery) {
|
|
12280
13035
|
if (imagery === void 0) return null;
|
|
@@ -12305,6 +13060,7 @@ function rowToRecord(row) {
|
|
|
12305
13060
|
const hasReviewState = row.next_review_at !== null || row.ease_factor !== null || row.interval_days !== null;
|
|
12306
13061
|
return {
|
|
12307
13062
|
id: asMemoryId(row.id),
|
|
13063
|
+
memoryId: asMemoryId(row.memory_id ?? row.id),
|
|
12308
13064
|
scope: row.scope,
|
|
12309
13065
|
kind: row.kind,
|
|
12310
13066
|
content: row.content,
|
|
@@ -12488,7 +13244,7 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
12488
13244
|
db.exec(`
|
|
12489
13245
|
CREATE TABLE IF NOT EXISTS meta (key TEXT PRIMARY KEY, value TEXT NOT NULL);
|
|
12490
13246
|
CREATE TABLE IF NOT EXISTS nodes (
|
|
12491
|
-
id TEXT PRIMARY KEY, scope TEXT NOT NULL, kind TEXT NOT NULL, content TEXT NOT NULL,
|
|
13247
|
+
id TEXT PRIMARY KEY, memory_id TEXT, scope TEXT NOT NULL, kind TEXT NOT NULL, content TEXT NOT NULL,
|
|
12492
13248
|
importance REAL NOT NULL, confidence REAL NOT NULL, status TEXT NOT NULL,
|
|
12493
13249
|
created_at INTEGER NOT NULL, last_accessed_at INTEGER NOT NULL, access_count INTEGER NOT NULL,
|
|
12494
13250
|
source_session_id TEXT, source_round INTEGER, source_seq INTEGER, embedding BLOB, outcome TEXT,
|
|
@@ -12503,7 +13259,7 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
12503
13259
|
seq INTEGER PRIMARY KEY AUTOINCREMENT, at INTEGER NOT NULL, op TEXT NOT NULL,
|
|
12504
13260
|
target_id TEXT NOT NULL, detail TEXT);
|
|
12505
13261
|
CREATE TABLE IF NOT EXISTS nodes_revisions (
|
|
12506
|
-
node_id TEXT NOT NULL, content TEXT NOT NULL, kind TEXT NOT NULL,
|
|
13262
|
+
node_id TEXT NOT NULL, memory_id TEXT, content TEXT NOT NULL, kind TEXT NOT NULL,
|
|
12507
13263
|
importance REAL NOT NULL, superseded_at INTEGER NOT NULL);
|
|
12508
13264
|
CREATE TABLE IF NOT EXISTS tour_routes (
|
|
12509
13265
|
position INTEGER PRIMARY KEY, node_id TEXT NOT NULL);
|
|
@@ -12530,6 +13286,10 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
12530
13286
|
id TEXT PRIMARY KEY, scope TEXT NOT NULL, belief TEXT NOT NULL, status TEXT NOT NULL,
|
|
12531
13287
|
sources_json TEXT NOT NULL, proof_count INTEGER NOT NULL, embedding BLOB,
|
|
12532
13288
|
created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL, last_validated_at INTEGER NOT NULL);
|
|
13289
|
+
CREATE TABLE IF NOT EXISTS injection_snapshots (
|
|
13290
|
+
id TEXT PRIMARY KEY, session_id TEXT NOT NULL, packet_hash TEXT NOT NULL,
|
|
13291
|
+
packet TEXT NOT NULL, memory_ids_json TEXT NOT NULL, has_curated INTEGER NOT NULL,
|
|
13292
|
+
compaction_count INTEGER NOT NULL, created_at INTEGER NOT NULL);
|
|
12533
13293
|
CREATE VIRTUAL TABLE IF NOT EXISTS nodes_fts USING fts5(node_id UNINDEXED, content, tokenize='unicode61');
|
|
12534
13294
|
CREATE INDEX IF NOT EXISTS nodes_scope_status ON nodes (scope, status);
|
|
12535
13295
|
CREATE INDEX IF NOT EXISTS nodes_kind_created ON nodes (kind, created_at);
|
|
@@ -12538,11 +13298,13 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
12538
13298
|
CREATE INDEX IF NOT EXISTS node_entities_entity ON node_entities (entity_id);
|
|
12539
13299
|
CREATE INDEX IF NOT EXISTS facts_entity ON facts (entity_id);
|
|
12540
13300
|
CREATE INDEX IF NOT EXISTS observations_status ON observations (status, proof_count, updated_at);
|
|
13301
|
+
CREATE INDEX IF NOT EXISTS injection_session_created ON injection_snapshots (session_id, created_at);
|
|
12541
13302
|
`);
|
|
12542
13303
|
const versionRow = db.prepare("SELECT value FROM meta WHERE key = 'schema_version'").get();
|
|
12543
13304
|
if (versionRow === void 0) {
|
|
12544
13305
|
db.exec(`CREATE INDEX IF NOT EXISTS nodes_slot ON nodes (slot_room, slot_index);
|
|
12545
|
-
CREATE INDEX IF NOT EXISTS nodes_review_due ON nodes (next_review_at)
|
|
13306
|
+
CREATE INDEX IF NOT EXISTS nodes_review_due ON nodes (next_review_at);
|
|
13307
|
+
CREATE INDEX IF NOT EXISTS nodes_memory_created ON nodes (memory_id, created_at);`);
|
|
12546
13308
|
db.prepare("INSERT INTO meta (key, value) VALUES ('schema_version', ?)").run(String(SCHEMA_VERSION));
|
|
12547
13309
|
} else {
|
|
12548
13310
|
let version = Number(versionRow.value);
|
|
@@ -12551,13 +13313,18 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
12551
13313
|
supported: SCHEMA_VERSION,
|
|
12552
13314
|
path
|
|
12553
13315
|
}));
|
|
12554
|
-
if (version < SCHEMA_VERSION)
|
|
12555
|
-
|
|
12556
|
-
|
|
12557
|
-
|
|
12558
|
-
|
|
12559
|
-
|
|
12560
|
-
})
|
|
13316
|
+
if (version < SCHEMA_VERSION) {
|
|
13317
|
+
const snapshotPath = `${path}.pre-migration`;
|
|
13318
|
+
try {
|
|
13319
|
+
await rm(snapshotPath, { force: true });
|
|
13320
|
+
const quoted = snapshotPath.replaceAll("'", "''");
|
|
13321
|
+
db.exec(`VACUUM INTO '${quoted}'`);
|
|
13322
|
+
} catch (error) {
|
|
13323
|
+
console.warn(message("store.preMigrationSnapshotFailed", {
|
|
13324
|
+
path,
|
|
13325
|
+
msg: error instanceof Error ? error.message : String(error)
|
|
13326
|
+
}));
|
|
13327
|
+
}
|
|
12561
13328
|
}
|
|
12562
13329
|
while (version < SCHEMA_VERSION) {
|
|
12563
13330
|
const migration = MIGRATIONS[String(version)];
|
|
@@ -12612,18 +13379,22 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
12612
13379
|
storedDim = length;
|
|
12613
13380
|
};
|
|
12614
13381
|
const sqlGet = db.prepare("SELECT * FROM nodes WHERE id = ?");
|
|
13382
|
+
const sqlGetMemory = db.prepare(`SELECT * FROM nodes WHERE memory_id = ?
|
|
13383
|
+
ORDER BY CASE status WHEN 'active' THEN 0 ELSE 1 END, created_at DESC, id DESC LIMIT 1`);
|
|
12615
13384
|
const sqlInsert = db.prepare(`INSERT INTO nodes
|
|
12616
|
-
(id, scope, kind, content, importance, confidence, status, created_at, last_accessed_at, access_count,
|
|
13385
|
+
(id, memory_id, scope, kind, content, importance, confidence, status, created_at, last_accessed_at, access_count,
|
|
12617
13386
|
source_session_id, source_round, source_seq, embedding, imagery_json,
|
|
12618
13387
|
slot_room, slot_index, imagery_score, next_review_at, ease_factor, interval_days)
|
|
12619
|
-
VALUES (?, ?, ?, ?, ?, ?, 'active', ?, ?, 0, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
13388
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, 'active', ?, ?, 0, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
12620
13389
|
const sqlFtsInsert = db.prepare("INSERT INTO nodes_fts (node_id, content) VALUES (?, ?)");
|
|
12621
13390
|
const sqlSetStatus = db.prepare("UPDATE nodes SET status = ?, last_accessed_at = ? WHERE id = ?");
|
|
13391
|
+
const sqlActiveMemorySiblings = db.prepare(`SELECT id FROM nodes
|
|
13392
|
+
WHERE memory_id = ? AND id != ? AND status = 'active'`);
|
|
12622
13393
|
const sqlSetOutcome = db.prepare(`UPDATE nodes
|
|
12623
13394
|
SET outcome = ?,
|
|
12624
13395
|
confidence = CASE WHEN ? = 'success' THEN min(1.0, confidence + ${CONFIDENCE_BUMP}) ELSE max(0.0, confidence - ${OUTCOME_PENALTY}) END
|
|
12625
13396
|
WHERE id = ?`);
|
|
12626
|
-
const
|
|
13397
|
+
const sqlReinforce = db.prepare(`UPDATE nodes SET access_count = access_count + 1, last_accessed_at = ?,
|
|
12627
13398
|
confidence = MIN(1, confidence + ${CONFIDENCE_BUMP}) WHERE id = ?`);
|
|
12628
13399
|
const sqlLog = db.prepare("INSERT INTO op_log (at, op, target_id, detail) VALUES (?, ?, ?, ?)");
|
|
12629
13400
|
const sqlHasAudit = db.prepare("SELECT 1 AS x FROM op_log WHERE op = ? AND detail = ? LIMIT 1");
|
|
@@ -12647,13 +13418,24 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
12647
13418
|
)`);
|
|
12648
13419
|
const sqlRecentOps = db.prepare("SELECT at, op, target_id, detail FROM op_log ORDER BY seq DESC LIMIT ?");
|
|
12649
13420
|
const sqlCountOps = db.prepare("SELECT op, COUNT(*) AS n FROM op_log WHERE at >= ? GROUP BY op");
|
|
12650
|
-
const sqlRevisionInsert = db.prepare("INSERT INTO nodes_revisions (node_id, content, kind, importance, superseded_at) VALUES (?, ?, ?, ?, ?)");
|
|
12651
|
-
const sqlRevisionsById = db.prepare("SELECT content, kind, importance, superseded_at FROM nodes_revisions WHERE
|
|
13421
|
+
const sqlRevisionInsert = db.prepare("INSERT INTO nodes_revisions (node_id, memory_id, content, kind, importance, superseded_at) VALUES (?, ?, ?, ?, ?, ?)");
|
|
13422
|
+
const sqlRevisionsById = db.prepare("SELECT content, kind, importance, superseded_at FROM nodes_revisions WHERE memory_id = ? ORDER BY superseded_at DESC, rowid DESC");
|
|
12652
13423
|
const sqlTopActive = db.prepare("SELECT * FROM nodes WHERE scope = ? AND status = 'active' ORDER BY importance DESC, confidence DESC LIMIT ?");
|
|
12653
13424
|
const sqlEdgeUpsert = db.prepare("INSERT OR IGNORE INTO edges (from_id, to_id, type, created_at) VALUES (?, ?, ?, ?)");
|
|
12654
13425
|
const sqlNeighbors = db.prepare(`SELECT * FROM edges WHERE from_id IN (SELECT value FROM json_each(?))
|
|
12655
13426
|
AND type IN ('supports','refines','related') LIMIT ?`);
|
|
12656
13427
|
const sqlEdgesTouching = db.prepare("SELECT from_id, to_id, type FROM edges WHERE from_id = ? OR to_id = ?");
|
|
13428
|
+
const sqlContradictions = db.prepare(`SELECT e.from_id, e.to_id, e.created_at
|
|
13429
|
+
FROM edges e
|
|
13430
|
+
JOIN nodes first ON first.id = e.from_id AND first.status = 'active'
|
|
13431
|
+
JOIN nodes second ON second.id = e.to_id AND second.status = 'active'
|
|
13432
|
+
WHERE e.type = 'contradicts'
|
|
13433
|
+
ORDER BY e.created_at DESC LIMIT ?`);
|
|
13434
|
+
const sqlContradictionPair = db.prepare(`SELECT from_id, to_id, created_at FROM edges
|
|
13435
|
+
WHERE type = 'contradicts'
|
|
13436
|
+
AND ((from_id = ? AND to_id = ?) OR (from_id = ? AND to_id = ?))
|
|
13437
|
+
LIMIT 1`);
|
|
13438
|
+
const sqlEdgeDelete = db.prepare("DELETE FROM edges WHERE from_id = ? AND to_id = ? AND type = ?");
|
|
12657
13439
|
const sqlCountBy = db.prepare("SELECT status, COUNT(*) AS n FROM nodes GROUP BY status");
|
|
12658
13440
|
const sqlCountKind = db.prepare("SELECT kind, COUNT(*) AS n FROM nodes GROUP BY kind");
|
|
12659
13441
|
const sqlCountEdges = db.prepare("SELECT COUNT(*) AS n FROM edges");
|
|
@@ -12662,10 +13444,10 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
12662
13444
|
const sqlAllNodes = db.prepare("SELECT * FROM nodes ORDER BY created_at");
|
|
12663
13445
|
const sqlAllEdges = db.prepare("SELECT * FROM edges");
|
|
12664
13446
|
const sqlImportInsert = db.prepare(`INSERT INTO nodes
|
|
12665
|
-
(id, scope, kind, content, importance, confidence, status, created_at, last_accessed_at, access_count,
|
|
13447
|
+
(id, memory_id, scope, kind, content, importance, confidence, status, created_at, last_accessed_at, access_count,
|
|
12666
13448
|
source_session_id, source_round, source_seq, embedding, outcome, imagery_json,
|
|
12667
13449
|
slot_room, slot_index, imagery_score, next_review_at, ease_factor, interval_days, review_reps)
|
|
12668
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
13450
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
12669
13451
|
const sqlImportContentMatch = db.prepare("SELECT id FROM nodes WHERE content = ? LIMIT 1");
|
|
12670
13452
|
const sqlImportAttachEmbedding = db.prepare("UPDATE nodes SET embedding = ? WHERE id = ?");
|
|
12671
13453
|
const vectorStatements = /* @__PURE__ */ new Map();
|
|
@@ -12723,6 +13505,16 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
12723
13505
|
ON CONFLICT (session_id) DO UPDATE SET summary = excluded.summary, updated_at = excluded.updated_at`);
|
|
12724
13506
|
const sqlSessionSummaryGet = db.prepare("SELECT session_id, summary FROM session_summaries WHERE session_id = ?");
|
|
12725
13507
|
const sqlSessionSummaryMany = db.prepare("SELECT session_id, summary FROM session_summaries WHERE session_id IN (SELECT value FROM json_each(?))");
|
|
13508
|
+
const sqlInjectionInsert = db.prepare(`INSERT INTO injection_snapshots
|
|
13509
|
+
(id, session_id, packet_hash, packet, memory_ids_json, has_curated, compaction_count, created_at)
|
|
13510
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
13511
|
+
const sqlInjectionHead = `SELECT id, session_id, packet_hash, memory_ids_json, has_curated,
|
|
13512
|
+
compaction_count, LENGTH(CAST(packet AS BLOB)) AS packet_bytes, created_at FROM injection_snapshots`;
|
|
13513
|
+
const sqlInjectionList = db.prepare(`${sqlInjectionHead} ORDER BY created_at DESC, id DESC LIMIT ?`);
|
|
13514
|
+
const sqlInjectionListSession = db.prepare(`${sqlInjectionHead} WHERE session_id = ? ORDER BY created_at DESC, id DESC LIMIT ?`);
|
|
13515
|
+
const sqlInjectionGet = db.prepare(`SELECT id, session_id, packet_hash, packet, memory_ids_json, has_curated,
|
|
13516
|
+
compaction_count, LENGTH(CAST(packet AS BLOB)) AS packet_bytes, created_at FROM injection_snapshots WHERE id = ?`);
|
|
13517
|
+
const sqlInjectionPrune = db.prepare("DELETE FROM injection_snapshots WHERE created_at < ?");
|
|
12726
13518
|
const sqlEntityAll = db.prepare("SELECT * FROM entities");
|
|
12727
13519
|
const sqlEntityGet = db.prepare("SELECT * FROM entities WHERE id = ?");
|
|
12728
13520
|
const sqlEntityInsert = db.prepare("INSERT INTO entities (id, name, kind, aliases_json, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)");
|
|
@@ -12940,7 +13732,7 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
12940
13732
|
const initialReview = input.initialReviewAt ?? null;
|
|
12941
13733
|
const safeImportance = clampUnit(importance, .5);
|
|
12942
13734
|
const safeConfidence = clampUnit(confidence, .5);
|
|
12943
|
-
sqlInsert.run(id, input.scope, input.kind, content, safeImportance, safeConfidence, at, at, sourceSessionId, input.sourceRound ?? null, input.sourceSeq ?? null, stored, imageryToJson(imagery), input.slot?.room ?? null, input.slot?.index ?? null, input.imageryScore ?? null, initialReview, initialReview === null ? null : 2.5, initialReview === null ? null : 0);
|
|
13735
|
+
sqlInsert.run(id, input.memoryId ?? id, input.scope, input.kind, content, safeImportance, safeConfidence, at, at, sourceSessionId, input.sourceRound ?? null, input.sourceSeq ?? null, stored, imageryToJson(imagery), input.slot?.room ?? null, input.slot?.index ?? null, input.imageryScore ?? null, initialReview, initialReview === null ? null : 2.5, initialReview === null ? null : 0);
|
|
12944
13736
|
sqlFtsInsert.run(id, tokenizeForFts(content));
|
|
12945
13737
|
sqlLog.run(at, op, id, JSON.stringify({
|
|
12946
13738
|
kind: input.kind,
|
|
@@ -13055,6 +13847,11 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
13055
13847
|
const row = getRow(id);
|
|
13056
13848
|
return row === void 0 ? void 0 : rowToRecord(row);
|
|
13057
13849
|
},
|
|
13850
|
+
/** 稳定逻辑身份 → 当前修订(active 优先,其次最新版)。 */
|
|
13851
|
+
async getMemory(id) {
|
|
13852
|
+
const row = sqlGetMemory.get(id);
|
|
13853
|
+
return row === void 0 ? void 0 : rowToRecord(row);
|
|
13854
|
+
},
|
|
13058
13855
|
async getMany(ids) {
|
|
13059
13856
|
if (ids.length === 0) return [];
|
|
13060
13857
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -13186,8 +13983,14 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
13186
13983
|
name: r.name
|
|
13187
13984
|
})) }
|
|
13188
13985
|
});
|
|
13189
|
-
sqlTouch.run(Date.now(), id);
|
|
13190
13986
|
}
|
|
13987
|
+
sqlLog.run(Date.now(), "search-exposure", "AUX", JSON.stringify({
|
|
13988
|
+
ids: hits.map((hit) => String(hit.record.id)),
|
|
13989
|
+
scopes: query.scopes,
|
|
13990
|
+
limit,
|
|
13991
|
+
degraded,
|
|
13992
|
+
q: createHash("sha256").update(query.text).digest("hex").slice(0, 12)
|
|
13993
|
+
}));
|
|
13191
13994
|
return {
|
|
13192
13995
|
hits,
|
|
13193
13996
|
degraded
|
|
@@ -13283,6 +14086,31 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
13283
14086
|
async getSessionSummary(sessionId) {
|
|
13284
14087
|
return sqlSessionSummaryGet.get(sessionId)?.summary;
|
|
13285
14088
|
},
|
|
14089
|
+
async recordInjection(input) {
|
|
14090
|
+
const id = randomUUID();
|
|
14091
|
+
const at = Date.now();
|
|
14092
|
+
sqlInjectionInsert.run(id, input.sessionId, input.packetHash, input.packet, JSON.stringify(input.memoryIds), input.hasCurated ? 1 : 0, input.compactionCount, at);
|
|
14093
|
+
const row = sqlInjectionGet.get(id);
|
|
14094
|
+
return {
|
|
14095
|
+
...rowToInjectionHead(row),
|
|
14096
|
+
packet: row.packet
|
|
14097
|
+
};
|
|
14098
|
+
},
|
|
14099
|
+
async listInjections(filter) {
|
|
14100
|
+
const limit = Math.max(1, Math.min(200, Math.floor(filter.limit ?? 50)));
|
|
14101
|
+
return (filter.sessionId === void 0 ? sqlInjectionList.all(limit) : sqlInjectionListSession.all(filter.sessionId, limit)).map(rowToInjectionHead);
|
|
14102
|
+
},
|
|
14103
|
+
async getInjection(id) {
|
|
14104
|
+
const row = sqlInjectionGet.get(id);
|
|
14105
|
+
return row === void 0 ? void 0 : {
|
|
14106
|
+
...rowToInjectionHead(row),
|
|
14107
|
+
packet: row.packet
|
|
14108
|
+
};
|
|
14109
|
+
},
|
|
14110
|
+
async pruneInjections(olderThanMs) {
|
|
14111
|
+
const result = sqlInjectionPrune.run(olderThanMs);
|
|
14112
|
+
return Number(result.changes);
|
|
14113
|
+
},
|
|
13286
14114
|
async update(input) {
|
|
13287
14115
|
const old = getRow(input.id);
|
|
13288
14116
|
if (old === void 0) throw new EngramError("NOT_FOUND", message("store.entryNotFound", { id: input.id }));
|
|
@@ -13291,7 +14119,7 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
13291
14119
|
const id = asMemoryId(randomUUID());
|
|
13292
14120
|
const at = Date.now();
|
|
13293
14121
|
withTransaction(() => {
|
|
13294
|
-
sqlRevisionInsert.run(input.id, old.content, old.kind, old.importance, at);
|
|
14122
|
+
sqlRevisionInsert.run(input.id, old.memory_id, old.content, old.kind, old.importance, at);
|
|
13295
14123
|
sqlSetStatus.run("archived", at, input.id);
|
|
13296
14124
|
sqlLog.run(at, "superseded", input.id, JSON.stringify({ supersededBy: id }));
|
|
13297
14125
|
detachArchivedEvidence([input.id]);
|
|
@@ -13299,13 +14127,14 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
13299
14127
|
const updateInput = {
|
|
13300
14128
|
scope: input.scope,
|
|
13301
14129
|
kind: input.kind,
|
|
13302
|
-
content
|
|
14130
|
+
content,
|
|
14131
|
+
memoryId: asMemoryId(old.memory_id ?? input.id)
|
|
13303
14132
|
};
|
|
13304
14133
|
const automationFields = applyWriteAutomation(updateInput, id, at, imagery);
|
|
13305
14134
|
insertRecord(id, {
|
|
13306
14135
|
...updateInput,
|
|
13307
14136
|
...automationFields
|
|
13308
|
-
}, content, input.importance ?? old.importance, old.confidence, at, old.source_session_id, input.embedding
|
|
14137
|
+
}, content, input.importance ?? old.importance, old.confidence, at, old.source_session_id, input.embedding === void 0 ? old.embedding : input.embedding, imagery, "update");
|
|
13309
14138
|
sqlEdgeUpsert.run(id, input.id, "supersedes", at);
|
|
13310
14139
|
});
|
|
13311
14140
|
return rowToRecord(sqlGet.get(id));
|
|
@@ -13500,9 +14329,19 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
13500
14329
|
};
|
|
13501
14330
|
},
|
|
13502
14331
|
async restore(id) {
|
|
13503
|
-
|
|
13504
|
-
|
|
13505
|
-
|
|
14332
|
+
const row = getRow(id);
|
|
14333
|
+
if (row === void 0) throw new EngramError("NOT_FOUND", message("store.entryNotFound", { id }));
|
|
14334
|
+
const at = Date.now();
|
|
14335
|
+
withTransaction(() => {
|
|
14336
|
+
const memoryId = row.memory_id ?? row.id;
|
|
14337
|
+
const siblings = sqlActiveMemorySiblings.all(memoryId, id);
|
|
14338
|
+
for (const sibling of siblings) {
|
|
14339
|
+
sqlSetStatus.run("archived", at, asMemoryId(sibling.id));
|
|
14340
|
+
sqlEdgeUpsert.run(id, sibling.id, "supersedes", at);
|
|
14341
|
+
}
|
|
14342
|
+
sqlSetStatus.run("active", at, id);
|
|
14343
|
+
sqlLog.run(at, "restore", id, JSON.stringify({ archivedRevisions: siblings.map((s) => s.id) }));
|
|
14344
|
+
});
|
|
13506
14345
|
return rowToRecord(sqlGet.get(id));
|
|
13507
14346
|
},
|
|
13508
14347
|
async topActive(scope, n) {
|
|
@@ -13536,7 +14375,7 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
13536
14375
|
const row = getRow(id);
|
|
13537
14376
|
if (row === void 0) return void 0;
|
|
13538
14377
|
const operations = sqlOpLogById.all(id, REVIEW_LOG_LIMIT);
|
|
13539
|
-
const revisions = sqlRevisionsById.all(id);
|
|
14378
|
+
const revisions = sqlRevisionsById.all(row.memory_id ?? row.id);
|
|
13540
14379
|
return {
|
|
13541
14380
|
record: rowToRecord(row),
|
|
13542
14381
|
...edgeGroups(id),
|
|
@@ -13678,7 +14517,7 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
13678
14517
|
for (const c of eligible) {
|
|
13679
14518
|
const r = c.record;
|
|
13680
14519
|
const review = r.review;
|
|
13681
|
-
sqlImportInsert.run(r.id, r.scope, r.kind, c.content, clampUnit(r.importance, .5), clampUnit(r.confidence, .5), r.status, r.createdAt, r.lastAccessedAt, r.accessCount, r.sourceSessionId ?? null, r.sourceRound ?? null, r.sourceSeq ?? null, null, r.outcome ?? null, imageryToJson(r.imagery), r.slot?.room ?? null, r.slot?.index ?? null, r.imageryScore ?? null, review?.nextReviewAt ?? null, review?.easeFactor ?? null, review?.intervalDays ?? null, review?.reps ?? null);
|
|
14520
|
+
sqlImportInsert.run(r.id, r.memoryId ?? r.id, r.scope, r.kind, c.content, clampUnit(r.importance, .5), clampUnit(r.confidence, .5), r.status, r.createdAt, r.lastAccessedAt, r.accessCount, r.sourceSessionId ?? null, r.sourceRound ?? null, r.sourceSeq ?? null, null, r.outcome ?? null, imageryToJson(r.imagery), r.slot?.room ?? null, r.slot?.index ?? null, r.imageryScore ?? null, review?.nextReviewAt ?? null, review?.easeFactor ?? null, review?.intervalDays ?? null, review?.reps ?? null);
|
|
13682
14521
|
sqlFtsInsert.run(r.id, tokenizeForFts(c.content));
|
|
13683
14522
|
}
|
|
13684
14523
|
for (const edge of data.edges ?? []) {
|
|
@@ -13788,6 +14627,48 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
13788
14627
|
hits.sort((a, b) => b.sim - a.sim);
|
|
13789
14628
|
return hits.slice(0, limit).map((hit) => getRow(matrix.ids[hit.index])).filter((row) => row !== void 0).map((row) => rowToRecord(row));
|
|
13790
14629
|
},
|
|
14630
|
+
async listContradictions(limit) {
|
|
14631
|
+
const boundedLimit = Math.max(1, Math.min(200, Math.floor(limit)));
|
|
14632
|
+
const rows = sqlContradictions.all(boundedLimit);
|
|
14633
|
+
const result = [];
|
|
14634
|
+
for (const row of rows) {
|
|
14635
|
+
const first = getRow(row.from_id);
|
|
14636
|
+
const second = getRow(row.to_id);
|
|
14637
|
+
if (first === void 0 || second === void 0) continue;
|
|
14638
|
+
result.push({
|
|
14639
|
+
first: rowToRecord(first),
|
|
14640
|
+
second: rowToRecord(second),
|
|
14641
|
+
createdAt: row.created_at
|
|
14642
|
+
});
|
|
14643
|
+
}
|
|
14644
|
+
return result;
|
|
14645
|
+
},
|
|
14646
|
+
async resolveContradiction(input) {
|
|
14647
|
+
const forward = sqlContradictionPair.get(input.firstId, input.secondId, input.secondId, input.firstId);
|
|
14648
|
+
if (forward === void 0) throw new EngramError("NOT_FOUND", message("store.entryNotFound", { id: input.firstId }));
|
|
14649
|
+
const at = Date.now();
|
|
14650
|
+
if (input.action === "dismiss") {
|
|
14651
|
+
withTransaction(() => {
|
|
14652
|
+
sqlEdgeDelete.run(forward.from_id, forward.to_id, "contradicts");
|
|
14653
|
+
sqlEdgeUpsert.run(forward.from_id, forward.to_id, "related", at);
|
|
14654
|
+
sqlLog.run(at, "governance", "AUX", JSON.stringify({
|
|
14655
|
+
action: "dismiss",
|
|
14656
|
+
from: forward.from_id,
|
|
14657
|
+
to: forward.to_id
|
|
14658
|
+
}));
|
|
14659
|
+
});
|
|
14660
|
+
return { dismissed: true };
|
|
14661
|
+
}
|
|
14662
|
+
const loser = input.loserId === void 0 ? void 0 : getRow(input.loserId);
|
|
14663
|
+
if (loser === void 0 || loser.id !== forward.from_id && loser.id !== forward.to_id) throw new EngramError("NOT_FOUND", message("store.entryNotFound", { id: input.loserId ?? input.firstId }));
|
|
14664
|
+
sqlSetStatus.run("forgotten", at, loser.id);
|
|
14665
|
+
sqlLog.run(at, "forget", loser.id, null);
|
|
14666
|
+
detachArchivedEvidence([loser.id]);
|
|
14667
|
+
return {
|
|
14668
|
+
dismissed: false,
|
|
14669
|
+
forgotten: rowToRecord(sqlGet.get(loser.id))
|
|
14670
|
+
};
|
|
14671
|
+
},
|
|
13791
14672
|
async linkEdge(from, to, type) {
|
|
13792
14673
|
sqlEdgeUpsert.run(from, to, type, Date.now());
|
|
13793
14674
|
},
|
|
@@ -13831,7 +14712,7 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
|
|
|
13831
14712
|
},
|
|
13832
14713
|
async reinforce(id, note) {
|
|
13833
14714
|
if (sqlGet.get(id) === void 0) return void 0;
|
|
13834
|
-
|
|
14715
|
+
sqlReinforce.run(Date.now(), id);
|
|
13835
14716
|
sqlLog.run(Date.now(), "write-merge", id, note);
|
|
13836
14717
|
return rowToRecord(sqlGet.get(id));
|
|
13837
14718
|
},
|
|
@@ -16025,27 +16906,25 @@ function createEngramTools(baseDeps) {
|
|
|
16025
16906
|
},
|
|
16026
16907
|
async execute(args) {
|
|
16027
16908
|
const input = args;
|
|
16028
|
-
const content = redactSecrets(sanitizeProtocolText(input.content));
|
|
16029
|
-
if (content.trim() === "") throw new Error(message("tool.updateContentEmptyAfterSanitize"));
|
|
16030
16909
|
const scope = scopeOf(input.scope, "project");
|
|
16031
16910
|
const store = await deps.openStore(scope);
|
|
16032
|
-
const
|
|
16033
|
-
|
|
16911
|
+
const imagery = placardOf(input.placard);
|
|
16912
|
+
const record = await updateMemory({
|
|
16913
|
+
store,
|
|
16914
|
+
embedder: deps.embedder
|
|
16915
|
+
}, {
|
|
16916
|
+
id: input.id,
|
|
16917
|
+
scope,
|
|
16918
|
+
content: input.content,
|
|
16919
|
+
...input.kind === void 0 ? {} : { kind: input.kind },
|
|
16920
|
+
...imagery === void 0 ? {} : { imagery }
|
|
16921
|
+
});
|
|
16922
|
+
if (record === void 0) throw new Error(message("tool.updateNotFound", {
|
|
16034
16923
|
id: input.id,
|
|
16035
16924
|
scope
|
|
16036
16925
|
}));
|
|
16037
|
-
const embedder = await deps.embedder;
|
|
16038
|
-
const embeddings = embedder === void 0 ? void 0 : await embedder.embed([content.trim()]);
|
|
16039
|
-
const imagery = placardOf(input.placard);
|
|
16040
16926
|
return {
|
|
16041
|
-
id:
|
|
16042
|
-
id: input.id,
|
|
16043
|
-
scope,
|
|
16044
|
-
kind: input.kind ?? old.kind,
|
|
16045
|
-
content,
|
|
16046
|
-
...embeddings === void 0 ? {} : { embedding: embeddings[0] },
|
|
16047
|
-
...imagery === void 0 ? {} : { imagery }
|
|
16048
|
-
})).id,
|
|
16927
|
+
id: record.id,
|
|
16049
16928
|
superseded: input.id
|
|
16050
16929
|
};
|
|
16051
16930
|
}
|
|
@@ -17393,7 +18272,7 @@ function buildSelectionRationale(records) {
|
|
|
17393
18272
|
];
|
|
17394
18273
|
for (const record of records) {
|
|
17395
18274
|
const reasons = classifyRecord(record, now);
|
|
17396
|
-
lines.push(`- #${record.
|
|
18275
|
+
lines.push(`- #${record.memoryId.slice(0, 8)} [${record.kind}/${record.scope}] ${reasonsLabel(reasons)}:${record.content.slice(0, 40)}${record.content.length > 40 ? "…" : ""}`);
|
|
17397
18276
|
}
|
|
17398
18277
|
lines.push("", message("core.rationaleFooter"), "</engram_selection_rationale>");
|
|
17399
18278
|
return lines.join("\n");
|
|
@@ -17455,7 +18334,7 @@ function estimateTokens(text) {
|
|
|
17455
18334
|
return Math.ceil(wide * WIDE_TOKENS_PER_CHAR + narrow / 4);
|
|
17456
18335
|
}
|
|
17457
18336
|
// package.json
|
|
17458
|
-
var version = "0.
|
|
18337
|
+
var version = "0.12.0";
|
|
17459
18338
|
// src/index.ts
|
|
17460
18339
|
/**
|
|
17461
18340
|
* dsh-engram:DeepSeek Harness 跨会话长期记忆插件(host 半)。
|
|
@@ -17616,6 +18495,18 @@ async function compressProfileOverflow(ctx, agent, routeOverride, overflow, sign
|
|
|
17616
18495
|
return;
|
|
17617
18496
|
}
|
|
17618
18497
|
}
|
|
18498
|
+
function preStepSession(state, sessionId) {
|
|
18499
|
+
const existing = state.sessions.get(sessionId);
|
|
18500
|
+
if (existing !== void 0) return existing;
|
|
18501
|
+
const created = {
|
|
18502
|
+
lastProfileHash: null,
|
|
18503
|
+
lastProfileCompactions: null,
|
|
18504
|
+
lastAssessReminder: null,
|
|
18505
|
+
route: void 0
|
|
18506
|
+
};
|
|
18507
|
+
state.sessions.set(sessionId, created);
|
|
18508
|
+
return created;
|
|
18509
|
+
}
|
|
17619
18510
|
/**
|
|
17620
18511
|
* agent/pre-step waterfall:每轮第一步注入画像(内容与上次相同则跳过重复注入);
|
|
17621
18512
|
* 同时 fire-and-forget 触发上一轮的自动摄取(不阻塞请求);进程内首次第一步
|
|
@@ -17625,11 +18516,16 @@ async function compressProfileOverflow(ctx, agent, routeOverride, overflow, sign
|
|
|
17625
18516
|
async function preStep(ctx, openStore, openStoreForProjectCwd, resolved, embedder, state, logRequest, onDialogueLanguage, { agent, step, turn, signal }, next) {
|
|
17626
18517
|
const decision = await next();
|
|
17627
18518
|
if (decision.kind === "reject") return decision;
|
|
18519
|
+
const sessionId = String(agent.session.id);
|
|
18520
|
+
const sessionState = preStepSession(state, sessionId);
|
|
17628
18521
|
const observed = dominantLanguage(userTexts(agent.session.snapshotEvents()));
|
|
17629
18522
|
if (observed !== void 0) onDialogueLanguage(observed.language);
|
|
17630
18523
|
if (step === 1) {
|
|
17631
18524
|
const currentRoute = resolved.routeOverride ?? routeFromEvents(agent.session.snapshotEvents());
|
|
17632
|
-
if (currentRoute !== void 0)
|
|
18525
|
+
if (currentRoute !== void 0) {
|
|
18526
|
+
sessionState.route = currentRoute;
|
|
18527
|
+
state.route = currentRoute;
|
|
18528
|
+
}
|
|
17633
18529
|
}
|
|
17634
18530
|
const mode = resolved.ingest;
|
|
17635
18531
|
const routing = ingestWriteRouting(sessionCwd(agent.session), openStoreForProjectCwd, openStore);
|
|
@@ -17643,7 +18539,7 @@ async function preStep(ctx, openStore, openStoreForProjectCwd, resolved, embedde
|
|
|
17643
18539
|
embedder,
|
|
17644
18540
|
mode,
|
|
17645
18541
|
routeOverride: resolved.routeOverride,
|
|
17646
|
-
...resolveJevField(ctx, resolved.jev, resolved.dbDir, () =>
|
|
18542
|
+
...resolveJevField(ctx, resolved.jev, resolved.dbDir, () => sessionState.route?.provider),
|
|
17647
18543
|
call: (params) => streamText(ctx, {
|
|
17648
18544
|
...params,
|
|
17649
18545
|
sessionId: agent.session.id
|
|
@@ -17664,7 +18560,7 @@ async function preStep(ctx, openStore, openStoreForProjectCwd, resolved, embedde
|
|
|
17664
18560
|
embedder,
|
|
17665
18561
|
mode,
|
|
17666
18562
|
routeOverride: resolved.routeOverride,
|
|
17667
|
-
...resolveJevField(ctx, resolved.jev, resolved.dbDir, () =>
|
|
18563
|
+
...resolveJevField(ctx, resolved.jev, resolved.dbDir, () => sessionState.route?.provider),
|
|
17668
18564
|
call: (params) => streamText(ctx, {
|
|
17669
18565
|
...params,
|
|
17670
18566
|
sessionId: agent.session.id
|
|
@@ -17676,11 +18572,11 @@ async function preStep(ctx, openStore, openStoreForProjectCwd, resolved, embedde
|
|
|
17676
18572
|
});
|
|
17677
18573
|
}
|
|
17678
18574
|
if (step !== 1 && resolved.assessReminder) {
|
|
17679
|
-
const sessionId = String(agent.session.id);
|
|
17680
18575
|
const pending = evidenceBatches.pendingBatches(sessionId).length;
|
|
17681
18576
|
const reminder = buildAssessReminder(pending, evidenceBatches.insufficientStreak(sessionId));
|
|
17682
|
-
if (reminder
|
|
17683
|
-
|
|
18577
|
+
if (reminder === void 0) sessionState.lastAssessReminder = null;
|
|
18578
|
+
if (reminder !== void 0 && reminder !== sessionState.lastAssessReminder) {
|
|
18579
|
+
sessionState.lastAssessReminder = reminder;
|
|
17684
18580
|
return {
|
|
17685
18581
|
...decision,
|
|
17686
18582
|
messages: [...decision.messages, createUserMessage({
|
|
@@ -17700,9 +18596,10 @@ async function preStep(ctx, openStore, openStoreForProjectCwd, resolved, embedde
|
|
|
17700
18596
|
};
|
|
17701
18597
|
}
|
|
17702
18598
|
}
|
|
18599
|
+
if (!resolved.injectProfile) return decision;
|
|
17703
18600
|
const compactionCount = agent.session.snapshotEvents().filter((event) => event.type === "compaction/end").length;
|
|
17704
|
-
const compacted =
|
|
17705
|
-
if (
|
|
18601
|
+
const compacted = sessionState.lastProfileCompactions !== null && compactionCount > sessionState.lastProfileCompactions;
|
|
18602
|
+
if (sessionState.lastProfileCompactions === null) sessionState.lastProfileCompactions = compactionCount;
|
|
17706
18603
|
if (step !== 1 && !compacted) return decision;
|
|
17707
18604
|
const store = await openStore("user");
|
|
17708
18605
|
const curated = await store.getProfileBlock("user");
|
|
@@ -17747,13 +18644,24 @@ async function preStep(ctx, openStore, openStoreForProjectCwd, resolved, embedde
|
|
|
17747
18644
|
});
|
|
17748
18645
|
const textWithRationale = wrapWithRationale(text, top) + dueLine;
|
|
17749
18646
|
const hash = createHash("sha256").update(`${parts.curated ?? ""}\n${textWithRationale}`).digest("hex");
|
|
17750
|
-
if (!compacted &&
|
|
17751
|
-
|
|
17752
|
-
|
|
17753
|
-
|
|
17754
|
-
const requestText = currentUserRequestText(decision.messages);
|
|
17755
|
-
const memoryPacket = renderMemoryPacket(text, "turn_start", requestText);
|
|
18647
|
+
if (!compacted && hash === sessionState.lastProfileHash) return decision;
|
|
18648
|
+
sessionState.lastProfileHash = hash;
|
|
18649
|
+
sessionState.lastProfileCompactions = compactionCount;
|
|
18650
|
+
const memoryPacket = renderMemoryPacket(textWithRationale, "turn_start", currentUserRequestText(decision.messages));
|
|
17756
18651
|
const packet = parts.curated === void 0 ? memoryPacket : `${renderProfilePacket(parts.curated)}\n\n${memoryPacket}`;
|
|
18652
|
+
const packetHash = createHash("sha256").update(packet).digest("hex");
|
|
18653
|
+
try {
|
|
18654
|
+
await store.recordInjection({
|
|
18655
|
+
sessionId,
|
|
18656
|
+
packetHash,
|
|
18657
|
+
packet,
|
|
18658
|
+
memoryIds: top.map((record) => record.memoryId),
|
|
18659
|
+
hasCurated: parts.curated !== void 0,
|
|
18660
|
+
compactionCount
|
|
18661
|
+
});
|
|
18662
|
+
} catch (error) {
|
|
18663
|
+
console.warn(message("core.warnInjectionSnapshotFailed"), error);
|
|
18664
|
+
}
|
|
17757
18665
|
return {
|
|
17758
18666
|
...decision,
|
|
17759
18667
|
messages: [...decision.messages, createUserMessage({
|
|
@@ -18167,7 +19075,11 @@ async function apply(ctx, config = {}) {
|
|
|
18167
19075
|
}));
|
|
18168
19076
|
stores.clear();
|
|
18169
19077
|
};
|
|
18170
|
-
|
|
19078
|
+
let stopReflectJobs;
|
|
19079
|
+
ctx.effect(() => async () => {
|
|
19080
|
+
await stopReflectJobs?.();
|
|
19081
|
+
await closeStores();
|
|
19082
|
+
}, "dsh-engram: close stores");
|
|
18171
19083
|
/** cwd → 分库文件名(历史回填会对每个历史 cwd 解析一次,避免重复读 git 元数据)。 */
|
|
18172
19084
|
const cwdDbNames = /* @__PURE__ */ new Map();
|
|
18173
19085
|
/**
|
|
@@ -18222,10 +19134,7 @@ async function apply(ctx, config = {}) {
|
|
|
18222
19134
|
};
|
|
18223
19135
|
const preStepState = {
|
|
18224
19136
|
pendingReplayed: false,
|
|
18225
|
-
|
|
18226
|
-
lastProfileHash: null,
|
|
18227
|
-
lastProfileCompactions: null,
|
|
18228
|
-
lastAssessReminder: null,
|
|
19137
|
+
sessions: /* @__PURE__ */ new Map(),
|
|
18229
19138
|
route: void 0
|
|
18230
19139
|
};
|
|
18231
19140
|
/** 会话持久化服务(可选;缺席时历史回填不可用)。 */
|
|
@@ -18908,9 +19817,11 @@ async function apply(ctx, config = {}) {
|
|
|
18908
19817
|
}
|
|
18909
19818
|
});
|
|
18910
19819
|
});
|
|
18911
|
-
if (resolved.injectProfile || resolved.ingest !== "off") ctx.on("agent/pre-step", (payload, next) => preStep(ctx, openStore, openStoreForProjectCwd, resolved, embedder, preStepState, logIngestRequest, observeDialogueLanguage, payload, next), { prepend: true });
|
|
19820
|
+
if (resolved.injectProfile || resolved.ingest !== "off" || resolved.assessReminder) ctx.on("agent/pre-step", (payload, next) => preStep(ctx, openStore, openStoreForProjectCwd, resolved, embedder, preStepState, logIngestRequest, observeDialogueLanguage, payload, next), { prepend: true });
|
|
18912
19821
|
ctx.on("session/disposed", (session) => {
|
|
18913
|
-
|
|
19822
|
+
const sessionId = String(session.id);
|
|
19823
|
+
evidenceBatches.clear(sessionId);
|
|
19824
|
+
preStepState.sessions.delete(sessionId);
|
|
18914
19825
|
});
|
|
18915
19826
|
if (resolved.ingest !== "off") ctx.on("session/disposed", (session) => {
|
|
18916
19827
|
const mode = resolved.ingest;
|
|
@@ -18956,59 +19867,71 @@ async function apply(ctx, config = {}) {
|
|
|
18956
19867
|
console.warn(message("core.warnDisposeFinalFailed"), error);
|
|
18957
19868
|
});
|
|
18958
19869
|
});
|
|
18959
|
-
if (resolved.reflect
|
|
18960
|
-
const
|
|
19870
|
+
if (resolved.reflect === "suggest") ctx.on("session/disposed", (session) => {
|
|
19871
|
+
const sessionId = String(session.id);
|
|
19872
|
+
const cwd = sessionCwd(session);
|
|
19873
|
+
const targets = [{
|
|
19874
|
+
scope: "user",
|
|
19875
|
+
open: () => openStore("user")
|
|
19876
|
+
}];
|
|
19877
|
+
if (cwd !== void 0 && existsSync(join(resolved.dbDir, dbNameForCwd(cwd)))) targets.push({
|
|
19878
|
+
scope: "project",
|
|
19879
|
+
open: () => openStoreForProjectCwd(cwd)
|
|
19880
|
+
});
|
|
19881
|
+
(async () => {
|
|
19882
|
+
for (const target of targets) try {
|
|
19883
|
+
const store = await target.open();
|
|
19884
|
+
const detail = JSON.stringify({
|
|
19885
|
+
scope: target.scope,
|
|
19886
|
+
sessionId
|
|
19887
|
+
});
|
|
19888
|
+
if (!await store.hasAudit("reflect-pending", detail)) await store.audit("reflect-pending", "AUX", detail);
|
|
19889
|
+
} catch (error) {
|
|
19890
|
+
console.warn(message("core.warnReflectFailed", { scope: target.scope }), error);
|
|
19891
|
+
}
|
|
19892
|
+
})();
|
|
19893
|
+
});
|
|
19894
|
+
if (resolved.reflect === "auto") {
|
|
19895
|
+
const reflectJobs = await startAutoReflectWorker({
|
|
19896
|
+
dbDir: resolved.dbDir,
|
|
19897
|
+
dbNameForCwd,
|
|
19898
|
+
openStore,
|
|
19899
|
+
openProjectStore: openStoreForProjectCwd,
|
|
19900
|
+
embedder: () => embedderAttempt(),
|
|
19901
|
+
call: (sessionId, params) => streamText(ctx, {
|
|
19902
|
+
...params,
|
|
19903
|
+
sessionId
|
|
19904
|
+
})
|
|
19905
|
+
});
|
|
19906
|
+
stopReflectJobs = reflectJobs.stop;
|
|
18961
19907
|
ctx.on("session/disposed", (session) => {
|
|
18962
19908
|
const events = session.snapshotEvents();
|
|
18963
|
-
const sessionId = String(session.id);
|
|
18964
|
-
const cwd = sessionCwd(session);
|
|
18965
19909
|
const route = resolved.routeOverride ?? routeFromEvents(events);
|
|
18966
|
-
|
|
18967
|
-
|
|
18968
|
-
|
|
18969
|
-
|
|
18970
|
-
|
|
18971
|
-
|
|
18972
|
-
|
|
19910
|
+
if (route === void 0) return;
|
|
19911
|
+
const eventSeq = events.reduce((max, event) => typeof event.seq === "number" && Number.isInteger(event.seq) && event.seq > max ? event.seq : max, 0);
|
|
19912
|
+
const sessionId = String(session.id);
|
|
19913
|
+
try {
|
|
19914
|
+
reflectJobs.enqueue({
|
|
19915
|
+
sessionId,
|
|
19916
|
+
scope: "user",
|
|
19917
|
+
route,
|
|
19918
|
+
eventSeq
|
|
19919
|
+
});
|
|
19920
|
+
const cwd = sessionCwd(session);
|
|
19921
|
+
if (cwd !== void 0 && existsSync(join(resolved.dbDir, dbNameForCwd(cwd)))) reflectJobs.enqueue({
|
|
19922
|
+
sessionId,
|
|
19923
|
+
scope: "project",
|
|
19924
|
+
cwd,
|
|
19925
|
+
route,
|
|
19926
|
+
eventSeq
|
|
19927
|
+
});
|
|
19928
|
+
} catch (error) {
|
|
19929
|
+
console.warn(message("core.warnReflectFailed", { scope: "enqueue" }), error);
|
|
19930
|
+
return;
|
|
19931
|
+
}
|
|
19932
|
+
reflectJobs.pump().catch((error) => {
|
|
19933
|
+
console.warn(message("core.warnReflectFailed", { scope: "scheduler" }), error);
|
|
18973
19934
|
});
|
|
18974
|
-
(async () => {
|
|
18975
|
-
for (const target of targets) try {
|
|
18976
|
-
const store = await target.open();
|
|
18977
|
-
if (resolved.reflect === "suggest") {
|
|
18978
|
-
const detail = JSON.stringify({
|
|
18979
|
-
scope: target.scope,
|
|
18980
|
-
sessionId
|
|
18981
|
-
});
|
|
18982
|
-
if (!await store.hasAudit("reflect-pending", detail)) await store.audit("reflect-pending", "AUX", detail);
|
|
18983
|
-
continue;
|
|
18984
|
-
}
|
|
18985
|
-
if (route === void 0) continue;
|
|
18986
|
-
const outcome = await reflectObservations({
|
|
18987
|
-
store,
|
|
18988
|
-
embedder: await embedderAttempt(),
|
|
18989
|
-
scope: target.scope,
|
|
18990
|
-
call: (params) => streamText(ctx, {
|
|
18991
|
-
...params,
|
|
18992
|
-
sessionId: session.id
|
|
18993
|
-
}),
|
|
18994
|
-
logRequest: (data) => {
|
|
18995
|
-
store.audit("reflect-request", "AUX", JSON.stringify(data)).catch(() => {});
|
|
18996
|
-
},
|
|
18997
|
-
route,
|
|
18998
|
-
signal: AbortSignal.timeout(REFLECT_TIMEOUT_MS)
|
|
18999
|
-
});
|
|
19000
|
-
if (outcome.formed + outcome.refined + outcome.confirmed + outcome.refuted > 0) console.log(message("core.logReflectDone", {
|
|
19001
|
-
scope: target.scope,
|
|
19002
|
-
formed: outcome.formed,
|
|
19003
|
-
refined: outcome.refined,
|
|
19004
|
-
confirmed: outcome.confirmed,
|
|
19005
|
-
refuted: outcome.refuted
|
|
19006
|
-
}));
|
|
19007
|
-
if (outcome.dropped > 0) console.warn(message("core.warnReflectBatchDropped", { count: outcome.dropped }));
|
|
19008
|
-
} catch (error) {
|
|
19009
|
-
console.warn(message("core.warnReflectFailed", { scope: target.scope }), error);
|
|
19010
|
-
}
|
|
19011
|
-
})();
|
|
19012
19935
|
});
|
|
19013
19936
|
}
|
|
19014
19937
|
const OP_LOG_RETENTION_DAYS = 366;
|
|
@@ -19037,6 +19960,7 @@ async function apply(ctx, config = {}) {
|
|
|
19037
19960
|
}));
|
|
19038
19961
|
try {
|
|
19039
19962
|
await store.pruneOps(pruneBefore, LOG_KEEP_OPS);
|
|
19963
|
+
await store.pruneInjections(pruneBefore);
|
|
19040
19964
|
} catch (error) {
|
|
19041
19965
|
console.warn(message("core.warnDecayPeriodicFailed"), error);
|
|
19042
19966
|
}
|