pi-ate-memory 1.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -1
- package/index.ts +4 -0
- package/lib/asset-catalog.ts +63 -0
- package/lib/read-tracker.ts +77 -0
- package/lib/routing-register.ts +70 -0
- package/lib/routing.ts +169 -0
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -233,10 +233,23 @@ extensions/memory/
|
|
|
233
233
|
```
|
|
234
234
|
|
|
235
235
|
**依赖**:
|
|
236
|
-
- `@pi-atelier/shared-utils
|
|
236
|
+
- `@pi-atelier/shared-utils`(^1.5.0)— scanMemoryDir、parseFileName、**judge 骨架(createJudge/setContext/三层模型链)**
|
|
237
237
|
- `@earendil-works/pi-coding-agent` — ExtensionAPI
|
|
238
238
|
- 无其他依赖
|
|
239
239
|
|
|
240
|
+
## 记忆按需路由(2026-11-23)
|
|
241
|
+
|
|
242
|
+
批量判定路由器:AI 叙述(text 块)计满 N 条 → LLM 判定与哪些**未读**记忆/spec 强相关 → steer 注入指路。
|
|
243
|
+
|
|
244
|
+
```jsonc
|
|
245
|
+
// settings.json(可选;缺省 enabled=true/batchSize=5/includeSpecs=true)
|
|
246
|
+
"memoryRouting": { "enabled": true, "batchSize": 5, "includeSpecs": true, "judgeModel": "" }
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
- `/memory-routing` — 查看当前配置;`/memory-routing 8` 改批量;`on/off` 开关(会话内存效,重启回 settings 缺省)
|
|
250
|
+
- 防护:白名单容错匹配(幻觉路径丢弃,max 3)、sha1 幂等、fail-open、已读剔除、已提醒未读标注自降权
|
|
251
|
+
- 判定日志:`~/.pi/agent/logs/memory-routing-llmjudge.log`
|
|
252
|
+
|
|
240
253
|
## 许可证
|
|
241
254
|
|
|
242
255
|
MIT
|
package/index.ts
CHANGED
|
@@ -31,11 +31,15 @@ import {
|
|
|
31
31
|
SOFT_FILE_LIMIT,
|
|
32
32
|
} from "./lib/types";
|
|
33
33
|
import { rebuildIndex } from "./lib/writer";
|
|
34
|
+
import { registerRouting } from "./lib/routing-register";
|
|
34
35
|
|
|
35
36
|
export default function memoryToolsExtension(pi: ExtensionAPI) {
|
|
36
37
|
// ── before_agent_start: 注入记忆说明 + 索引到 systemPrompt ──
|
|
37
38
|
registerMemoryHook(pi);
|
|
38
39
|
|
|
40
|
+
// ── memory routing: 批量判定式记忆路由(拉模式,change memory-routing)──
|
|
41
|
+
registerRouting(pi);
|
|
42
|
+
|
|
39
43
|
// ── memory_index: 查询记忆清单 ──────────────────────
|
|
40
44
|
pi.registerTool({
|
|
41
45
|
name: "memory_index",
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Asset Catalog — 知识资产清单构建(记忆 L1/L2 + openspec specs)
|
|
3
|
+
*
|
|
4
|
+
* 供 routing 判定拼装清单用:每条资产 = 相对路径 + 一句话描述。
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import * as fs from "node:fs";
|
|
8
|
+
import * as path from "node:path";
|
|
9
|
+
import { AGENT_DIR } from "./types";
|
|
10
|
+
|
|
11
|
+
export interface KnowledgeAsset {
|
|
12
|
+
/** 展示/匹配路径(相对,如 memory/xxx.md 或 openspec/specs/yyy/spec.md) */
|
|
13
|
+
entry: string;
|
|
14
|
+
/** 一句话描述(关键词 or Purpose 首句) */
|
|
15
|
+
desc: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** L1+L2 记忆条目:memory 目录扫描(MEMORY.md 索引文件本身除外) */
|
|
19
|
+
export function collectMemoryAssets(cwd: string): KnowledgeAsset[] {
|
|
20
|
+
const assets: KnowledgeAsset[] = [];
|
|
21
|
+
const sources: Array<[string, string]> = [
|
|
22
|
+
["memory", path.join(AGENT_DIR, "memory")],
|
|
23
|
+
[".pi/memory", path.join(cwd, ".pi", "memory")],
|
|
24
|
+
];
|
|
25
|
+
for (const [prefix, dir] of sources) {
|
|
26
|
+
try {
|
|
27
|
+
for (const f of fs.readdirSync(dir)) {
|
|
28
|
+
if (!f.endsWith(".md") || f === "MEMORY.md") continue;
|
|
29
|
+
assets.push({ entry: `${prefix}/${f}`, desc: "" });
|
|
30
|
+
}
|
|
31
|
+
} catch {
|
|
32
|
+
// 目录不存在(L2 常见):跳过
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return assets;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** spec 清单:openspec/specs/<cap>/spec.md 的 capability + Purpose 首句 */
|
|
39
|
+
export function collectSpecAssets(cwd: string): KnowledgeAsset[] {
|
|
40
|
+
const specsDir = path.join(cwd, "openspec", "specs");
|
|
41
|
+
const assets: KnowledgeAsset[] = [];
|
|
42
|
+
try {
|
|
43
|
+
for (const cap of fs.readdirSync(specsDir)) {
|
|
44
|
+
const p = path.join(specsDir, cap, "spec.md");
|
|
45
|
+
try {
|
|
46
|
+
const text = fs.readFileSync(p, "utf8");
|
|
47
|
+
const m = text.match(/^##\s*Purpose\s*\n+(.+)$/m);
|
|
48
|
+
const purpose = m?.[1] ?? "";
|
|
49
|
+
const firstSentence =
|
|
50
|
+
purpose.split(/[。\n]/)[0]?.replace(/^#+\s*/, "").trim() ?? "";
|
|
51
|
+
assets.push({
|
|
52
|
+
entry: `openspec/specs/${cap}/spec.md`,
|
|
53
|
+
desc: firstSentence.slice(0, 120),
|
|
54
|
+
});
|
|
55
|
+
} catch {
|
|
56
|
+
// 单条读失败跳过
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
} catch {
|
|
60
|
+
// 无 specs 目录
|
|
61
|
+
}
|
|
62
|
+
return assets;
|
|
63
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read Tracker — 会话内 read 路径跟踪 + 白名单校验
|
|
3
|
+
*
|
|
4
|
+
* 数据源:tool_result 钩子(toolName === "read" 且非 error)
|
|
5
|
+
* 用途:
|
|
6
|
+
* 1. 判定时剔除已读条目(防重复提醒)
|
|
7
|
+
* 2. 判定输出的路径白名单校验(幻觉路径丢弃)
|
|
8
|
+
*
|
|
9
|
+
* 路径归一化:全部转小写、正斜杠,便于跨形态匹配(C:\a\b.md vs c:/a/b.md)。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export function normalizePath(p: string): string {
|
|
13
|
+
return p.replace(/\\/g, "/").replace(/^\/+/, "").toLowerCase();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** 归一化后按尾段匹配(清单条目可能是相对路径,判定输出可能是绝对路径或反之) */
|
|
17
|
+
export function pathEndsWith(candidate: string, entry: string): boolean {
|
|
18
|
+
const c = normalizePath(candidate);
|
|
19
|
+
const e = normalizePath(entry);
|
|
20
|
+
return c === e || c.endsWith("/" + e) || e.endsWith("/" + c);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export class ReadTracker {
|
|
24
|
+
private readPaths = new Set<string>();
|
|
25
|
+
private readonly sessionStart: number;
|
|
26
|
+
|
|
27
|
+
constructor() {
|
|
28
|
+
this.sessionStart = Date.now();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** 记录一次成功 read(tool_result:toolName === "read" 且非 error) */
|
|
32
|
+
recordRead(toolName: string, args: unknown, isError: boolean): void {
|
|
33
|
+
if (toolName !== "read" || isError) return;
|
|
34
|
+
const p =
|
|
35
|
+
(args as { path?: unknown; file_path?: unknown } | null)?.path ??
|
|
36
|
+
(args as { file_path?: unknown } | null)?.file_path;
|
|
37
|
+
if (typeof p === "string" && p.length > 0) {
|
|
38
|
+
this.readPaths.add(normalizePath(p));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** 条目是否已被 read(尾段匹配,容忍绝对/相对差异) */
|
|
43
|
+
hasRead(entry: string): boolean {
|
|
44
|
+
for (const p of this.readPaths) {
|
|
45
|
+
if (pathEndsWith(p, entry)) return true;
|
|
46
|
+
}
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 快照(诊断用) */
|
|
51
|
+
snapshot(): readonly string[] {
|
|
52
|
+
return [...this.readPaths];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
get startedAt(): number {
|
|
56
|
+
return this.sessionStart;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** 判定输出行 → 白名单内条目映射;白名单外(幻觉)丢弃;上限 max 条 */
|
|
61
|
+
export function filterVerdictPaths(
|
|
62
|
+
lines: string[],
|
|
63
|
+
whitelist: string[],
|
|
64
|
+
max = 3,
|
|
65
|
+
): string[] {
|
|
66
|
+
const out: string[] = [];
|
|
67
|
+
for (const raw of lines) {
|
|
68
|
+
const line = raw.trim().replace(/^[-*\d.\s"']+|["']+$/g, "");
|
|
69
|
+
if (!line || line.length > 260) continue;
|
|
70
|
+
const hit = whitelist.find((w) => pathEndsWith(line, w));
|
|
71
|
+
if (hit && !out.some((o) => pathEndsWith(o, hit))) {
|
|
72
|
+
out.push(hit);
|
|
73
|
+
if (out.length >= max) break;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* memory-routing 事件接线与命令注册。
|
|
3
|
+
* 核心路由器见 routing.ts。
|
|
4
|
+
*/
|
|
5
|
+
import { mkdirSync, appendFileSync } from "node:fs";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
8
|
+
import { MemoryRouter, routingJudge, loadConfig, setRoutingConfig } from "./routing";
|
|
9
|
+
|
|
10
|
+
const AGENT_DIR = process.env.PI_AGENT_DIR ?? join(process.env.HOME ?? "", ".pi", "agent");
|
|
11
|
+
|
|
12
|
+
function appendRunLog(line: string): void {
|
|
13
|
+
try {
|
|
14
|
+
mkdirSync(join(AGENT_DIR, "logs"), { recursive: true });
|
|
15
|
+
appendFileSync(
|
|
16
|
+
join(AGENT_DIR, "logs", "memory-routing.log"),
|
|
17
|
+
`${new Date().toISOString()} ${line}\n`,
|
|
18
|
+
);
|
|
19
|
+
} catch {
|
|
20
|
+
// 日志失败不影响主流程
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function registerRouting(pi: ExtensionAPI): void {
|
|
25
|
+
const router = new MemoryRouter(process.cwd());
|
|
26
|
+
const pendingTools = new Map<string, unknown>();
|
|
27
|
+
pi.on("message_end", async (event: unknown, ctx: unknown) => {
|
|
28
|
+
if (ctx) routingJudge.setContext(ctx as never); // 事件回调是 ctx 权威来源(shepherd 模式)
|
|
29
|
+
router.accumulate(event);
|
|
30
|
+
await router.maybeTrigger(pi);
|
|
31
|
+
});
|
|
32
|
+
pi.on("tool_execution_start", (event: unknown) => {
|
|
33
|
+
const { toolName, args, toolCallId } = event as {
|
|
34
|
+
toolName?: string; args?: unknown; toolCallId?: string;
|
|
35
|
+
};
|
|
36
|
+
if (toolName === "read" && toolCallId) pendingTools.set(toolCallId, args);
|
|
37
|
+
});
|
|
38
|
+
pi.on("tool_result", (event: unknown) => {
|
|
39
|
+
const { toolCallId, isError } = event as { toolCallId?: string; isError?: boolean };
|
|
40
|
+
if (!toolCallId || !pendingTools.has(toolCallId)) return;
|
|
41
|
+
const args = pendingTools.get(toolCallId);
|
|
42
|
+
pendingTools.delete(toolCallId);
|
|
43
|
+
if (!isError) router.recordRead("read", args, false);
|
|
44
|
+
});
|
|
45
|
+
pi.registerCommand("memory-routing", {
|
|
46
|
+
handler: async (rawArgs: string, ctx) => {
|
|
47
|
+
const sub = rawArgs.trim();
|
|
48
|
+
const notify = (ctx as { ui?: { notify?: (t: string) => void } })?.ui?.notify;
|
|
49
|
+
if (!notify) return;
|
|
50
|
+
const cfg = loadConfig(process.cwd());
|
|
51
|
+
if (/^\d+$/.test(sub)) {
|
|
52
|
+
const n = Math.max(1, Math.min(20, Number(sub)));
|
|
53
|
+
const updated = setRoutingConfig({ batchSize: n });
|
|
54
|
+
router.setConfig({ batchSize: updated.batchSize });
|
|
55
|
+
notify(`✅ memoryRouting.batchSize = ${updated.batchSize}(已持久化 settings.json,重启后保留)`);
|
|
56
|
+
} else if (sub === "off" || sub === "on") {
|
|
57
|
+
const updated = setRoutingConfig({ enabled: sub === "on" });
|
|
58
|
+
router.setConfig({ enabled: updated.enabled });
|
|
59
|
+
notify(`✅ memoryRouting.enabled = ${updated.enabled}(已持久化 settings.json,重启后保留)`);
|
|
60
|
+
} else {
|
|
61
|
+
notify(
|
|
62
|
+
`memory-routing: enabled=${cfg.enabled} batchSize=${cfg.batchSize} ` +
|
|
63
|
+
`includeSpecs=${cfg.includeSpecs}\n用法: /memory-routing [N|on|off]`,
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
description: "记忆按需路由配置. 用法: /memory-routing | /memory-routing <N|on|off>",
|
|
68
|
+
});
|
|
69
|
+
appendRunLog("routing registered");
|
|
70
|
+
}
|
package/lib/routing.ts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* memory-routing 核心路由器:计数 → 批量判定 → 白名单校验 → 注入。
|
|
3
|
+
* 事件接线与命令注册见 routing-register.ts。
|
|
4
|
+
*/
|
|
5
|
+
import { createHash } from "node:crypto";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { getEffectiveConfig, patchSettingsSection } from "@pi-atelier/shared-utils";
|
|
8
|
+
import { createJudge } from "@pi-atelier/shared-utils/judge";
|
|
9
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
10
|
+
import {
|
|
11
|
+
collectMemoryAssets,
|
|
12
|
+
collectSpecAssets,
|
|
13
|
+
type KnowledgeAsset,
|
|
14
|
+
} from "./asset-catalog";
|
|
15
|
+
import { ReadTracker, filterVerdictPaths } from "./read-tracker";
|
|
16
|
+
|
|
17
|
+
const AGENT_DIR = process.env.PI_AGENT_DIR ?? join(process.env.HOME ?? "", ".pi", "agent");
|
|
18
|
+
const JUDGE_LOG = join(AGENT_DIR, "logs", "memory-routing-llmjudge.log");
|
|
19
|
+
const lastBatchHashes: string[] = [];
|
|
20
|
+
const DEDUP_WINDOW = 3;
|
|
21
|
+
|
|
22
|
+
export interface RoutingConfig {
|
|
23
|
+
enabled: boolean;
|
|
24
|
+
batchSize: number;
|
|
25
|
+
includeSpecs: boolean;
|
|
26
|
+
judgeModel: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function loadConfig(cwd: string): RoutingConfig {
|
|
30
|
+
const { config } = getEffectiveConfig(
|
|
31
|
+
"memoryRouting",
|
|
32
|
+
{ enabled: true, batchSize: 5, includeSpecs: true, judgeModel: "" },
|
|
33
|
+
cwd,
|
|
34
|
+
);
|
|
35
|
+
return {
|
|
36
|
+
enabled: config.enabled !== false,
|
|
37
|
+
batchSize: Math.max(1, Number(config.batchSize) || 5),
|
|
38
|
+
includeSpecs: config.includeSpecs !== false,
|
|
39
|
+
judgeModel: String(config.judgeModel ?? ""),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** 持久化配置到 settings.json(全局层;读仍三层合并,与生态 /aging-config 同形态) */
|
|
44
|
+
export function setRoutingConfig(patch: Partial<RoutingConfig>): RoutingConfig {
|
|
45
|
+
return patchSettingsSection<RoutingConfig>(
|
|
46
|
+
"memoryRouting",
|
|
47
|
+
patch,
|
|
48
|
+
{ enabled: true, batchSize: 5, includeSpecs: true, judgeModel: "" },
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const SYSTEM_PROMPT =
|
|
53
|
+
"你是知识资产路由判定器。根据会话最近的 AI 叙述,判断与哪些【未读】资产强相关。\n" +
|
|
54
|
+
"输出格式:每行一个路径(逐字取自清单),最多 3 条;无相关则只输出「无」。\n" +
|
|
55
|
+
"规则:只推荐未读条目;提醒 2+ 次仍未读的条目不再推荐;路径禁止构造,必须逐字来自清单。";
|
|
56
|
+
|
|
57
|
+
function parsePathList(raw: string): string[] | null {
|
|
58
|
+
const lines = raw.split(/[\r\n]+/).map((l) => l.trim()).filter(Boolean);
|
|
59
|
+
if (lines.length === 0) return null;
|
|
60
|
+
if (lines.length === 1 && /^(无|none|n\/a)$/i.test(lines[0])) return [];
|
|
61
|
+
const bad = lines.find((l) => l.includes(" ") || l.length > 200);
|
|
62
|
+
if (bad) return null;
|
|
63
|
+
return lines.slice(0, 3);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export const routingJudge = createJudge<string[]>({
|
|
67
|
+
label: "memory-routing",
|
|
68
|
+
systemPrompt: SYSTEM_PROMPT,
|
|
69
|
+
parse: parsePathList,
|
|
70
|
+
judgeLogPath: JUDGE_LOG,
|
|
71
|
+
legacySectionKeys: ["memoryRouting", "shepherd"],
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
export class MemoryRouter {
|
|
75
|
+
private buffer: string[] = [];
|
|
76
|
+
private seen = 0;
|
|
77
|
+
private remindedCount = new Map<string, number>();
|
|
78
|
+
private config: RoutingConfig;
|
|
79
|
+
private readTracker = new ReadTracker();
|
|
80
|
+
private cwd: string;
|
|
81
|
+
constructor(cwd: string, config?: RoutingConfig) {
|
|
82
|
+
this.cwd = cwd;
|
|
83
|
+
this.config = config ?? loadConfig(cwd);
|
|
84
|
+
}
|
|
85
|
+
get enabled(): boolean { return this.config.enabled; }
|
|
86
|
+
setConfig(patch: Partial<RoutingConfig>): void {
|
|
87
|
+
this.config = { ...this.config, ...patch };
|
|
88
|
+
}
|
|
89
|
+
/** message_end:积累 text 块(纯 tool 轮不计数) */
|
|
90
|
+
accumulate(event: unknown): void {
|
|
91
|
+
if (!this.config.enabled) return;
|
|
92
|
+
const message = (event as { message?: { role?: string; content?: unknown[] } }).message;
|
|
93
|
+
if (!message || message.role !== "assistant") return;
|
|
94
|
+
const blocks = Array.isArray(message.content) ? message.content : [];
|
|
95
|
+
const texts = blocks
|
|
96
|
+
.filter((b): b is { type: "text"; text: string } =>
|
|
97
|
+
typeof b === "object" && b !== null && (b as { type?: string }).type === "text")
|
|
98
|
+
.map((b) => b.text)
|
|
99
|
+
.filter((t) => t && t.trim().length > 0);
|
|
100
|
+
if (texts.length === 0) return;
|
|
101
|
+
this.seen += 1;
|
|
102
|
+
this.buffer.push(texts.join("\n"));
|
|
103
|
+
}
|
|
104
|
+
/** 计满触发异步判定(fire-and-forget,异常静默 = fail-open) */
|
|
105
|
+
async maybeTrigger(pi?: ExtensionAPI): Promise<void> {
|
|
106
|
+
if (!this.config.enabled) return;
|
|
107
|
+
if (this.seen < this.config.batchSize) return;
|
|
108
|
+
const batch = this.buffer.splice(0, this.buffer.length);
|
|
109
|
+
this.seen = 0;
|
|
110
|
+
try {
|
|
111
|
+
await this.runBatch(batch, pi);
|
|
112
|
+
} catch {
|
|
113
|
+
// fail-open:静默丢弃本批(specs §1.5)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
private async runBatch(batch: string[], pi?: ExtensionAPI): Promise<void> {
|
|
117
|
+
const narrative = batch.join("\n---\n").slice(-6000);
|
|
118
|
+
const hash = createHash("sha1").update(narrative).digest("hex");
|
|
119
|
+
if (lastBatchHashes.includes(hash)) return; // 幂等
|
|
120
|
+
lastBatchHashes.push(hash);
|
|
121
|
+
if (lastBatchHashes.length > DEDUP_WINDOW) lastBatchHashes.shift();
|
|
122
|
+
const assets = [
|
|
123
|
+
...collectMemoryAssets(this.cwd),
|
|
124
|
+
...(this.config.includeSpecs ? collectSpecAssets(this.cwd) : []),
|
|
125
|
+
];
|
|
126
|
+
const candidates = assets.filter((a) => !this.readTracker.hasRead(a.entry));
|
|
127
|
+
if (candidates.length === 0) return;
|
|
128
|
+
const catalog = candidates
|
|
129
|
+
.map((a: KnowledgeAsset) => {
|
|
130
|
+
const n = this.remindedCount.get(a.entry) ?? 0;
|
|
131
|
+
const noted = n > 0 ? `(已提醒 ${n} 次未读)` : "";
|
|
132
|
+
const kind = a.entry.startsWith("openspec/") ? "spec" : "memory";
|
|
133
|
+
return `- ${a.entry}${noted} [${kind}] ${a.desc}`;
|
|
134
|
+
})
|
|
135
|
+
.join("\n");
|
|
136
|
+
const paths = await routingJudge.judge({
|
|
137
|
+
question: "判断这批 AI 叙述与哪些未读知识资产强相关,输出路径清单",
|
|
138
|
+
text: `【最近 AI 叙述】\n${narrative}\n\n【未读资产清单】\n${catalog}`,
|
|
139
|
+
contextLabel: `batch-${hash.slice(0, 8)}(${batch.length}条)`,
|
|
140
|
+
model: this.config.judgeModel || undefined,
|
|
141
|
+
});
|
|
142
|
+
if (!paths || paths.length === 0) return;
|
|
143
|
+
// 白名单校验:尾段容错匹配,仅留清单内路径,其余(幻觉)丢弃(specs §1.4)
|
|
144
|
+
const validEntries = filterVerdictPaths(paths, candidates.map((a) => a.entry));
|
|
145
|
+
if (validEntries.length === 0) return;
|
|
146
|
+
const valid = candidates.filter((a) => validEntries.includes(a.entry));
|
|
147
|
+
for (const a of valid) {
|
|
148
|
+
this.remindedCount.set(a.entry, (this.remindedCount.get(a.entry) ?? 0) + 1);
|
|
149
|
+
}
|
|
150
|
+
if (pi) {
|
|
151
|
+
const lines = valid
|
|
152
|
+
.map((a) =>
|
|
153
|
+
`- ${a.entry}(${a.entry.startsWith("openspec/") ? "规格" : "判例/记忆"}:${a.desc})`)
|
|
154
|
+
.join("\n");
|
|
155
|
+
pi.sendMessage(
|
|
156
|
+
{
|
|
157
|
+
customType: "memory-routing",
|
|
158
|
+
content: `💡 记忆路由:本段工作与以下知识资产相关(你尚未读过):\n${lines}\n如相关请 read;无关可忽略。`,
|
|
159
|
+
display: true,
|
|
160
|
+
},
|
|
161
|
+
{ deliverAs: "steer" },
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/** read 成功对账回调(routing-register 的 tool 双事件对账后调用) */
|
|
166
|
+
recordRead(toolName: string, args: unknown, isError: boolean): void {
|
|
167
|
+
this.readTracker.recordRead(toolName, args, isError);
|
|
168
|
+
}
|
|
169
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-ate-memory",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "File-based persistent memory for AI coding agents",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package"
|
|
@@ -20,6 +20,9 @@
|
|
|
20
20
|
"optional": true
|
|
21
21
|
}
|
|
22
22
|
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@pi-atelier/shared-utils": "^1.5.0"
|
|
25
|
+
},
|
|
23
26
|
"devDependencies": {
|
|
24
27
|
"@sinclair/typebox": "^0.34.49",
|
|
25
28
|
"@types/node": "^25.6.2",
|