Skip to content

Commit ebab68a

Browse files
committed
fix(pin): 显式钉住的行不被文档升格收走;pin 回执算上 pinnedInjectBudget
#593 把 pin 从「按类型推断」改成显式存储(pinned_memories 表)后,两处消费方 没跟上,各自退回旧口径: ① src/document.js 的 evidence 吸收只按 pinnedTypes.has(row.type) 判「谁属于 保真池」,拿不到 pin 表就只能按类型猜 —— 一条被显式钉住的 pattern / decision 在文档升格时照样被 setArchived 静默归档。新增 store.isPinned(memoryId) 单行 点查(pinned_memories 的唯一读侧谓词),吸收判据改为「显式钉住 或 类型保真池」 两条并列;谓词缺席时退回类型口径(fail-safe)。 ② src/tools.js 的 injectable 只看遗忘 / 归档 / 暂停作用域,没看 pin 池的总 开关。pin 池由 pinnedInjectBudget 决定(src/service.js:1885 的 pinnedBudget > 0 才读表选路),而它默认 0 = 关闭 —— 预算 0 时钉住的动作确实成功,但一条都不会 被注入,回执照样报 true,正是「返回成功不等于生效」。补上 pinnedBudget > 0, 上限与 service.js 同款 Math.min(5, ...),工具描述与 blocked 文案同步。 候选池侧的 type / importance / codingGate 闸仍不建模:那几道闸随候选池变化, 回执只回答「pin 这条通道此刻开不开」。 回归锁: - test/document.test.js 用 type:"pattern"(刻意不在 PINNED_MEMORY_TYPES 里, 否则会被类型口径顺手护住、测不到缺口)钉一条不钉一条,断言被钉行未归档。 - test/tools.test.js 锁预算 0 时 pinned:true 与 injectable:false 分开报。 - test/tools-dto-schema-matrix.test.js 的 injectable 断言由 true 改 false (该矩阵用默认配置,预算本就是 0)。 验证:npm test 2874 tests / 2873 pass / 0 fail / 1 skipped;npm run sync 95 文件一致。
1 parent 6a521d7 commit ebab68a

10 files changed

Lines changed: 141 additions & 17 deletions

File tree

‎dsh-mneme/CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
## [Unreleased]
44

5+
- **修复 #593 留下的两处旁路:显式钉住的行仍会被文档升格吸收静默归档;`memory_pin` 回执在 `pinnedInjectBudget=0`(默认关闭)时仍报 `injectable: true`**。#593 把 pin 从「按类型推断」改成显式存储(`pinned_memories` 表),但两处消费方没跟上:吸收只拿得到记忆行、拿不到 pin 表,于是退回按类型猜;回执只看了遗忘/归档/暂停作用域,没看预算开关。
6+
7+
- **判据①:新增 `store.isPinned(memoryId)` 单行点查**(`pinned_memories` 的唯一读侧谓词)。`src/document.js` 的 evidence 吸收循环改为两条并列判据:`store.isPinned(row.id)`(显式钉住)**或** `pinnedTypes.has(row.type)`(#249 逐字保真池类型 constraint/preference)——任一成立就不归档。上一版只有后者,所以一条被钉的 `pattern` / `decision` 在文档升格时照样 `setArchived`:用户钉过的行被悄悄收走,pin 语义当场失效。`store.isPinned` 缺席时自动退回类型口径(fail-safe,不因新谓词缺失而放宽)。
8+
- **判据②:`injectable` 加上 `pinnedBudget > 0`**,上限与 `src/service.js:1885` 同款 `Math.max(0, Math.min(5, Math.floor(config?.pinnedInjectBudget ?? 0)))`。pin 池整体由这个预算开关(`service.js` 里 `pinnedBudget > 0` 才读表选路),默认 0 = 关闭;预算 0 时钉住动作确实成功、但一条都不会被注入,回执照样报 true 正是「返回成功不等于生效」。工具 description、schema 注释与 blocked 文案同步补上这一条。**仍未建模的**:候选池侧的 type / importance / codingGate 闸(一条被钉的 `document` 行同样进不了注入块),那几道闸随候选池变化,回执只回答「pin 这条通道此刻开不开」。
9+
- **回归锁**:`test/document.test.js` 新增一例用 `type:"pattern"`(刻意不在 `PINNED_MEMORY_TYPES` 里,否则会被类型口径顺手护住、测不到缺口)——钉一条、不钉一条,注册文档吸收两条,断言被钉行 `archived===false`、未钉行 `archived===true`、`evidence_archived===1`;`test/tools.test.js` 新增一例锁预算 0 时 `pinned:true` 与 `injectable:false` 分开报;`test/tools-dto-schema-matrix.test.js` 的 `injectable` 断言由 true 改 **false**(该矩阵用默认配置,预算本就是 0)。
10+
- **验证**:`npm test` 2874 tests / 2873 pass / 0 fail / 1 skipped;`npm run sync` 95 文件一致。
11+
512
- **修复 #588:`codingGate` 词表补齐 34 项工程协作词——「合并 / PR / CI / 发版」类任务此前被判成非编码,`pitfall` / `constraint` / `rejected_solution` 三类教训记忆整类不注入**。`codingKeywords` 56 → 90 项,改的是默认值,词表本身仍可配置覆盖。
613

714
- **病灶是判据只看词表不看内容**:`codingGate` 按 `m.type` 判,而「这条任务算不算编码」唯一依据是 `codingKeywords` 子串命中。原表 56 项里没有「合并 / 分支 / 提交 / 冲突 / 验收 / 审查 / 评审 / 审评 / 发版 / 仓库 / 回滚 / 推送 / 拉取 / 变基 / 上线 / 打包 / 迁移 / 字段 / 鉴权 / 超时 / 并发 / 闪退 / 白屏 / 端口 / 进程 / 守护 / 定时任务 / 索引 / 缓存 / 死锁 / 内存泄漏 / 日志 / 版本号 / 环境变量」——用户日常说的「合并吧」「看看 CI 和审评」「今天就能发版」全被判成非编码,于是这三类记忆一次都不注入。生产日志实测(`recall_runs`,剔除系统提示污染后 1,070 query / 61,446 次调用):缺口占非编码调用的 17.1~21.7%,补词后翻转 114 query / 8,352 次,编码任务占比 23.4% → 37.0%。

‎dsh-mneme/lib/document.js‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,9 @@ export function createDocumentRegistrar({ store, config, embedQuery, pushContent
276276
}
277277
// #275 拍板 5(#230 验收口径的补丁):升格吸收的 evidence 行随之退出活跃面——
278278
// 一次吸收 20 条,库里就是「21 行不是 1 行」。四条口径:①与本行同一事务(要么
279-
// 都成、要么都不成);②pinned(constraint / preference,即 #249 的逐字保真池)
280-
// 永不自动归档——升格不能绕过它;③自有生命周期的类型(document / summary,见
279+
// 都成、要么都不成);②pinned 永不自动归档——升格不能绕过它,而 pinned 有**两个**
280+
// 判据:显式钉住的行(pinned_memories,#593 起为唯一正源)与 #249 的逐字保真池
281+
// 类型(constraint / preference);③自有生命周期的类型(document / summary,见
281282
// ABSORB_EXEMPT_TYPES)不吸收,吸收的是原子条;④只翻标志位、内容与审计全留
282283
// (可恢复),opt-out 走 archiveEvidence。
283284
// pinnedTypes 拿不到就不做这一步:宁可少做,也不能把保真池当普通行收走。
@@ -294,7 +295,12 @@ export function createDocumentRegistrar({ store, config, embedQuery, pushContent
294295
const row = store.getById(id);
295296
if (!row || row.archived || row.forgotten) continue;
296297
if (isImportedRow(row)) { evidenceSkipped += 1; continue; }
297-
if (pinnedTypes.has(row.type) || ABSORB_EXEMPT_TYPES.has(row.type)) continue;
298+
// #593:显式 pin(pinned_memories)与类型保真池是**两个**判据。上一版只问
299+
// pinnedTypes.has(row.type),等于把「被钉住的 pattern / decision」当普通行
300+
// 收走——用户钉过的东西在文档升格时被静默归档,正是「钉着 ≠ 会注入」的
301+
// 反面:这里连「钉着」都没保住。store.isPinned 缺席时退回类型口径(旧行为)。
302+
const explicitlyPinned = typeof store.isPinned === "function" && store.isPinned(row.id);
303+
if (explicitlyPinned || pinnedTypes.has(row.type) || ABSORB_EXEMPT_TYPES.has(row.type)) continue;
298304
store.setArchived(id, true);
299305
evidenceArchived += 1;
300306
}

‎dsh-mneme/lib/store.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4030,6 +4030,20 @@ export function createStore(path) {
40304030
return db.prepare("SELECT count(*) AS c FROM pinned_memories").get().c;
40314031
}
40324032

4033+
/**
4034+
* 这条记忆是否被显式钉住(单行点查,读侧)。
4035+
*
4036+
* 存在的理由是 #593 的缺口:升格吸收(document.js 的 evidence 归档)只拿得到
4037+
* 记忆行、拿不到 pin 表,于是它只能按**类型**猜「谁属于保真池」。显式 pin 落地
4038+
* 后,一条 pattern / decision 被钉住仍然是 pin,按类型猜就把它当普通行收走了。
4039+
* 谓词必须来自本表,不能从类型推——这正是 #593 把 pin 从类型推断改成显式存储
4040+
* 的同一件事,别在第二个地方又推回去。
4041+
*/
4042+
function isPinned(memoryId) {
4043+
if (typeof memoryId !== "string" || !memoryId.trim()) return false;
4044+
return db.prepare("SELECT 1 AS hit FROM pinned_memories WHERE memory_id = ?").get(memoryId.trim()) !== undefined;
4045+
}
4046+
40334047
/**
40344048
* 按 id 点查注入所需的行(绕开 200 行窗口的唯一通道)。
40354049
*
@@ -5248,6 +5262,7 @@ export function createStore(path) {
52485262
pinMemory,
52495263
unpinMemory,
52505264
countPinned,
5265+
isPinned,
52515266
listByIdsForInject,
52525267
createEntity,
52535268
findEntityByName,

‎dsh-mneme/lib/tools.js‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ export function createTools(ctx, service, config, embedder, sessionGate = null,
498498
description:
499499
"Pin a memory so it is injected at the top of the memory block every turn, regardless of its type or how it ranks against other memories. " +
500500
"Use this only when the user explicitly says an entry must always be present (a hard rule, a standing preference); it is not a way to mark something as important — importance already affects ranking. " +
501-
"Pinned entries still obey forgetting, archiving and the scope gate, so a pinned entry that is forgotten, archived or in a paused scope is not injected — the receipt reports this as injectable: false. " +
501+
"Pinned entries still obey forgetting, archiving, the scope gate and the pinnedInjectBudget gate, so a pinned entry that is forgotten, archived, in a paused scope, or pinned while pinnedInjectBudget is 0 is not injected — the receipt reports this as injectable: false. " +
502502
"Pass unpin: true to release it. Pin order is first-pinned-first, so the oldest pin holds its slot when the budget is tight.",
503503
parameters: {
504504
id: { type: "string", required: true, description: "Memory id to pin (or unpin with unpin: true)" },
@@ -516,8 +516,8 @@ export function createTools(ctx, service, config, embedder, sessionGate = null,
516516
// 回显它才看得出「这次其实没改变顺序」。
517517
created_at: { type: "string" },
518518
note: { type: "string" },
519-
// 钉着 ≠ 会注入。遗忘/归档/暂停作用域都优先于 pin,把这件事放进回执,
520-
// 而不是让用户以为钉完就一定出现在上下文里。
519+
// 钉着 ≠ 会注入。遗忘/归档/暂停作用域/pinnedInjectBudget=0 都优先于 pin,
520+
// 把这件事放进回执,而不是让用户以为钉完就一定出现在上下文里。
521521
injectable: { type: "boolean", required: true }
522522
}
523523
},
@@ -526,7 +526,7 @@ export function createTools(ctx, service, config, embedder, sessionGate = null,
526526
const when = value.created_at ? ` (pinned at ${value.created_at})` : "";
527527
const blocked = value.injectable
528528
? ""
529-
: " It will not be injected while it is forgotten, archived or inside a paused scope.";
529+
: " It will not be injected while it is forgotten, archived, inside a paused scope, or while pinnedInjectBudget is 0 (the pin pool is off by default).";
530530
return TEXT_OUTPUT(`Memory ${value.memory_id} is pinned${when}.${blocked}`);
531531
}
532532
},
@@ -539,8 +539,20 @@ export function createTools(ctx, service, config, embedder, sessionGate = null,
539539
// 两个标志位对此一无所知——只算这两项,一条被暂停作用域挡住的行就会报
540540
// injectable: true 而实际一条都没注入。filterPaused 正是那个出口(开关关、
541541
// 或只开观察档时它原样返回,于是此时报 true 是对的:那种状态下本来就不拦)。
542+
// #593 遗留缺口:pin 池整体由 pinnedInjectBudget 开关(src/service.js:1885 的
543+
// pinnedBudget > 0 才读表选路),而它默认 0 = 关闭。只看遗忘/归档/暂停作用域时,
544+
// 预算为 0 的一条回执照样报 injectable: true——钉住的动作确实成功了,但它此刻
545+
// 一条都不会被注入,这正是「返回成功不等于生效」。预算 0 必须报 false。
546+
//
547+
// 仍然没建模的:候选池侧的 type / importance / codingGate 闸(一条被钉的
548+
// document 行同样进不了注入块)。那几道闸随候选池变化,回执不做猜测;
549+
// 这里只回答「pin 这条通道此刻开不开」。
550+
const pinnedBudget = Math.max(0, Math.min(5, Math.floor(config?.pinnedInjectBudget ?? 0)));
542551
const injectable =
543-
!target.forgotten && !target.archived && service.filterPaused([target]).length > 0;
552+
pinnedBudget > 0 &&
553+
!target.forgotten &&
554+
!target.archived &&
555+
service.filterPaused([target]).length > 0;
544556
if (args.unpin === true) {
545557
service.unpinMemory(target.id);
546558
return { memory_id: target.id, pinned: false, injectable };

‎dsh-mneme/src/document.js‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,9 @@ export function createDocumentRegistrar({ store, config, embedQuery, pushContent
276276
}
277277
// #275 拍板 5(#230 验收口径的补丁):升格吸收的 evidence 行随之退出活跃面——
278278
// 一次吸收 20 条,库里就是「21 行不是 1 行」。四条口径:①与本行同一事务(要么
279-
// 都成、要么都不成);②pinned(constraint / preference,即 #249 的逐字保真池)
280-
// 永不自动归档——升格不能绕过它;③自有生命周期的类型(document / summary,见
279+
// 都成、要么都不成);②pinned 永不自动归档——升格不能绕过它,而 pinned 有**两个**
280+
// 判据:显式钉住的行(pinned_memories,#593 起为唯一正源)与 #249 的逐字保真池
281+
// 类型(constraint / preference);③自有生命周期的类型(document / summary,见
281282
// ABSORB_EXEMPT_TYPES)不吸收,吸收的是原子条;④只翻标志位、内容与审计全留
282283
// (可恢复),opt-out 走 archiveEvidence。
283284
// pinnedTypes 拿不到就不做这一步:宁可少做,也不能把保真池当普通行收走。
@@ -294,7 +295,12 @@ export function createDocumentRegistrar({ store, config, embedQuery, pushContent
294295
const row = store.getById(id);
295296
if (!row || row.archived || row.forgotten) continue;
296297
if (isImportedRow(row)) { evidenceSkipped += 1; continue; }
297-
if (pinnedTypes.has(row.type) || ABSORB_EXEMPT_TYPES.has(row.type)) continue;
298+
// #593:显式 pin(pinned_memories)与类型保真池是**两个**判据。上一版只问
299+
// pinnedTypes.has(row.type),等于把「被钉住的 pattern / decision」当普通行
300+
// 收走——用户钉过的东西在文档升格时被静默归档,正是「钉着 ≠ 会注入」的
301+
// 反面:这里连「钉着」都没保住。store.isPinned 缺席时退回类型口径(旧行为)。
302+
const explicitlyPinned = typeof store.isPinned === "function" && store.isPinned(row.id);
303+
if (explicitlyPinned || pinnedTypes.has(row.type) || ABSORB_EXEMPT_TYPES.has(row.type)) continue;
298304
store.setArchived(id, true);
299305
evidenceArchived += 1;
300306
}

‎dsh-mneme/src/store.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4030,6 +4030,20 @@ export function createStore(path) {
40304030
return db.prepare("SELECT count(*) AS c FROM pinned_memories").get().c;
40314031
}
40324032

4033+
/**
4034+
* 这条记忆是否被显式钉住(单行点查,读侧)。
4035+
*
4036+
* 存在的理由是 #593 的缺口:升格吸收(document.js 的 evidence 归档)只拿得到
4037+
* 记忆行、拿不到 pin 表,于是它只能按**类型**猜「谁属于保真池」。显式 pin 落地
4038+
* 后,一条 pattern / decision 被钉住仍然是 pin,按类型猜就把它当普通行收走了。
4039+
* 谓词必须来自本表,不能从类型推——这正是 #593 把 pin 从类型推断改成显式存储
4040+
* 的同一件事,别在第二个地方又推回去。
4041+
*/
4042+
function isPinned(memoryId) {
4043+
if (typeof memoryId !== "string" || !memoryId.trim()) return false;
4044+
return db.prepare("SELECT 1 AS hit FROM pinned_memories WHERE memory_id = ?").get(memoryId.trim()) !== undefined;
4045+
}
4046+
40334047
/**
40344048
* 按 id 点查注入所需的行(绕开 200 行窗口的唯一通道)。
40354049
*
@@ -5248,6 +5262,7 @@ export function createStore(path) {
52485262
pinMemory,
52495263
unpinMemory,
52505264
countPinned,
5265+
isPinned,
52515266
listByIdsForInject,
52525267
createEntity,
52535268
findEntityByName,

‎dsh-mneme/src/tools.js‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ export function createTools(ctx, service, config, embedder, sessionGate = null,
498498
description:
499499
"Pin a memory so it is injected at the top of the memory block every turn, regardless of its type or how it ranks against other memories. " +
500500
"Use this only when the user explicitly says an entry must always be present (a hard rule, a standing preference); it is not a way to mark something as important — importance already affects ranking. " +
501-
"Pinned entries still obey forgetting, archiving and the scope gate, so a pinned entry that is forgotten, archived or in a paused scope is not injected — the receipt reports this as injectable: false. " +
501+
"Pinned entries still obey forgetting, archiving, the scope gate and the pinnedInjectBudget gate, so a pinned entry that is forgotten, archived, in a paused scope, or pinned while pinnedInjectBudget is 0 is not injected — the receipt reports this as injectable: false. " +
502502
"Pass unpin: true to release it. Pin order is first-pinned-first, so the oldest pin holds its slot when the budget is tight.",
503503
parameters: {
504504
id: { type: "string", required: true, description: "Memory id to pin (or unpin with unpin: true)" },
@@ -516,8 +516,8 @@ export function createTools(ctx, service, config, embedder, sessionGate = null,
516516
// 回显它才看得出「这次其实没改变顺序」。
517517
created_at: { type: "string" },
518518
note: { type: "string" },
519-
// 钉着 ≠ 会注入。遗忘/归档/暂停作用域都优先于 pin,把这件事放进回执,
520-
// 而不是让用户以为钉完就一定出现在上下文里。
519+
// 钉着 ≠ 会注入。遗忘/归档/暂停作用域/pinnedInjectBudget=0 都优先于 pin,
520+
// 把这件事放进回执,而不是让用户以为钉完就一定出现在上下文里。
521521
injectable: { type: "boolean", required: true }
522522
}
523523
},
@@ -526,7 +526,7 @@ export function createTools(ctx, service, config, embedder, sessionGate = null,
526526
const when = value.created_at ? ` (pinned at ${value.created_at})` : "";
527527
const blocked = value.injectable
528528
? ""
529-
: " It will not be injected while it is forgotten, archived or inside a paused scope.";
529+
: " It will not be injected while it is forgotten, archived, inside a paused scope, or while pinnedInjectBudget is 0 (the pin pool is off by default).";
530530
return TEXT_OUTPUT(`Memory ${value.memory_id} is pinned${when}.${blocked}`);
531531
}
532532
},
@@ -539,8 +539,20 @@ export function createTools(ctx, service, config, embedder, sessionGate = null,
539539
// 两个标志位对此一无所知——只算这两项,一条被暂停作用域挡住的行就会报
540540
// injectable: true 而实际一条都没注入。filterPaused 正是那个出口(开关关、
541541
// 或只开观察档时它原样返回,于是此时报 true 是对的:那种状态下本来就不拦)。
542+
// #593 遗留缺口:pin 池整体由 pinnedInjectBudget 开关(src/service.js:1885 的
543+
// pinnedBudget > 0 才读表选路),而它默认 0 = 关闭。只看遗忘/归档/暂停作用域时,
544+
// 预算为 0 的一条回执照样报 injectable: true——钉住的动作确实成功了,但它此刻
545+
// 一条都不会被注入,这正是「返回成功不等于生效」。预算 0 必须报 false。
546+
//
547+
// 仍然没建模的:候选池侧的 type / importance / codingGate 闸(一条被钉的
548+
// document 行同样进不了注入块)。那几道闸随候选池变化,回执不做猜测;
549+
// 这里只回答「pin 这条通道此刻开不开」。
550+
const pinnedBudget = Math.max(0, Math.min(5, Math.floor(config?.pinnedInjectBudget ?? 0)));
542551
const injectable =
543-
!target.forgotten && !target.archived && service.filterPaused([target]).length > 0;
552+
pinnedBudget > 0 &&
553+
!target.forgotten &&
554+
!target.archived &&
555+
service.filterPaused([target]).length > 0;
544556
if (args.unpin === true) {
545557
service.unpinMemory(target.id);
546558
return { memory_id: target.id, pinned: false, injectable };

0 commit comments

Comments
 (0)