dsh-zcode-memory 0.0.0-stage → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +251 -2
- package/client.js +573 -0
- package/cordis.patch.yml +6 -0
- package/index.js +7143 -0
- package/package.json +49 -4
- package/tests/abort-loop.mjs +70 -0
- package/tests/ancestor-lookup.mjs +169 -0
- package/tests/append-race.mjs +90 -0
- package/tests/backlog-state.mjs +274 -0
- package/tests/client-bundle.mjs +256 -0
- package/tests/context-memo.mjs +85 -0
- package/tests/extract-cost.mjs +312 -0
- package/tests/extract-retry.mjs +88 -0
- package/tests/extract-system.mjs +138 -0
- package/tests/extract-window.mjs +236 -0
- package/tests/extract.mjs +126 -0
- package/tests/func.mjs +217 -0
- package/tests/index-budget.mjs +182 -0
- package/tests/index-comments.mjs +92 -0
- package/tests/index-truncate.mjs +237 -0
- package/tests/memory-notes.mjs +90 -0
- package/tests/native-tools-edge.mjs +297 -0
- package/tests/native-tools.mjs +182 -0
- package/tests/prompt-split.mjs +253 -0
- package/tests/recall-score.mjs +225 -0
- package/tests/resume-recovery.mjs +245 -0
- package/tests/sandbox-grants.mjs +494 -0
- package/tests/save-append-edges.mjs +190 -0
- package/tests/sensitive-path.mjs +227 -0
- package/tests/settings-api.mjs +399 -0
- package/tests/settings-disable.mjs +280 -0
- package/tests/smoke.mjs +111 -0
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
// G2(v0.12)重启/恢复后补抽:
|
|
2
|
+
// - 恢复的会话 seed 事件不重放 session/event,首个 turn-stopping 要从 snapshotEvents 补缓冲
|
|
3
|
+
// - 有持久化游标时,只补游标之后的区间(不重抽已覆盖部分),且积压不再被裁到 1 窗
|
|
4
|
+
// - 游标落在回合中间时仍要恢复 turn(否则 userScore 缺失 → 误判 short-prose)
|
|
5
|
+
// - fork 子会话不重抽父会话前缀(header.isSeeded + inheritedEventCount)
|
|
6
|
+
// - 跳过的回合也要把游标落盘(重启后不重复分析)
|
|
7
|
+
// 用法: node resume-recovery.mjs [index.js 路径]
|
|
8
|
+
import { pathToFileURL } from "node:url";
|
|
9
|
+
import { mkdtempSync, writeFileSync, existsSync, readFileSync, rmSync } from "node:fs";
|
|
10
|
+
import { tmpdir } from "node:os";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
|
|
13
|
+
const modUrl = process.argv[2] ? pathToFileURL(process.argv[2]).href : new URL("../index.js", import.meta.url).href;
|
|
14
|
+
const mod = await import(modUrl);
|
|
15
|
+
|
|
16
|
+
const fails = [];
|
|
17
|
+
const check = (cond, msg, extra) => {
|
|
18
|
+
if (!cond) fails.push(msg + (extra !== undefined ? ` :: ${JSON.stringify(extra).slice(0, 400)}` : ""));
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const createdAt = "2026-12-05T12:00:00.000Z";
|
|
22
|
+
const DEFAULT_SEED = [
|
|
23
|
+
{ type: "turn/start", seq: 0, data: { turn: 1 } },
|
|
24
|
+
{
|
|
25
|
+
type: "user/message",
|
|
26
|
+
seq: 1,
|
|
27
|
+
data: { source: { kind: "user" }, content: [{ type: "text", text: "HISTORY-FACT:这个项目用 pnpm release 发布,产物在 dist。" }] },
|
|
28
|
+
},
|
|
29
|
+
{ type: "assistant/message", seq: 2, data: { turn: 1, message: { content: [{ type: "text", text: "记下了,发布命令是 pnpm release。" }] } } },
|
|
30
|
+
{ type: "tool/result", seq: 3, data: { turn: 1, message: { toolCallId: "c-old", content: [{ type: "text", text: "OLD-TOOL-OUTPUT" }] } } },
|
|
31
|
+
];
|
|
32
|
+
const DEFAULT_LIVE = [
|
|
33
|
+
{ type: "turn/start", seq: 4, data: { turn: 2 } },
|
|
34
|
+
{
|
|
35
|
+
type: "user/message",
|
|
36
|
+
seq: 5,
|
|
37
|
+
data: { source: { kind: "user" }, content: [{ type: "text", text: "继续:把刚才那个结论和 NEW-FACT 都记到长期记忆。" }] },
|
|
38
|
+
},
|
|
39
|
+
{ type: "assistant/message", seq: 6, data: { turn: 2, message: { content: [{ type: "text", text: "好的。" }] } } },
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
async function runScenario(opts = {}) {
|
|
43
|
+
const { id } = opts;
|
|
44
|
+
const seedCursor = opts.seedCursor || 0;
|
|
45
|
+
const seedEvents = opts.seedEvents || DEFAULT_SEED;
|
|
46
|
+
const liveEvents = opts.liveEvents || DEFAULT_LIVE;
|
|
47
|
+
const firstLive = opts.firstLiveSeq ?? 4;
|
|
48
|
+
const inherited = opts.inheritedEventCount;
|
|
49
|
+
const headerExtra = opts.headerExtra || {};
|
|
50
|
+
const turn = opts.turn ?? 2;
|
|
51
|
+
const root = mkdtempSync(join(tmpdir(), "zcm-resroot-"));
|
|
52
|
+
const cwd = mkdtempSync(join(tmpdir(), "zcm-rescwd-"));
|
|
53
|
+
if (seedCursor) {
|
|
54
|
+
writeFileSync(
|
|
55
|
+
join(root, "_extract-state.json"),
|
|
56
|
+
JSON.stringify({ cursorBySession: { [id]: { seq: seedCursor, createdAt: Date.parse(createdAt) } } }),
|
|
57
|
+
"utf8",
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
const seen = [];
|
|
61
|
+
const reg = { handlers: new Map() };
|
|
62
|
+
const ctx = {
|
|
63
|
+
systemPrompt: { section: () => {}, context: () => {} },
|
|
64
|
+
tools: { register: () => {} },
|
|
65
|
+
commands: { register: () => {} },
|
|
66
|
+
on: (ev, fn) => {
|
|
67
|
+
if (!reg.handlers.has(ev)) reg.handlers.set(ev, []);
|
|
68
|
+
reg.handlers.get(ev).push(fn);
|
|
69
|
+
},
|
|
70
|
+
llm: {
|
|
71
|
+
stream: async function* (opts2) {
|
|
72
|
+
const text = (opts2.messages || []).map((m) => (m.content || []).map((b) => b.text || "").join("")).join("\n");
|
|
73
|
+
seen.push(text);
|
|
74
|
+
yield { type: "text-delta", text: JSON.stringify({ writes: [] }) };
|
|
75
|
+
yield { type: "finish", reason: { kind: "stop" } };
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
mod.apply(ctx, { memoriesRoot: root, enabled: true, promptStyle: "native" });
|
|
80
|
+
|
|
81
|
+
const session = {
|
|
82
|
+
header: { id, cwd, createdAt, ...headerExtra },
|
|
83
|
+
firstLiveSeq: firstLive,
|
|
84
|
+
snapshotEvents: opts.snapshot === false ? undefined : () => seedEvents,
|
|
85
|
+
};
|
|
86
|
+
if (inherited !== undefined) session.inheritedEventCount = inherited;
|
|
87
|
+
const agent = {
|
|
88
|
+
session,
|
|
89
|
+
status: "idle",
|
|
90
|
+
inbox: { nextTurn: [], nextStep: [] },
|
|
91
|
+
model: { current: () => ({ provider: "mock", model: "mock-1" }) },
|
|
92
|
+
whenIdle: () => Promise.resolve(),
|
|
93
|
+
runMaintenance: async (fn) => fn(new AbortController().signal),
|
|
94
|
+
};
|
|
95
|
+
const emit = async (ev, ...args) => {
|
|
96
|
+
for (const fn of reg.handlers.get(ev) || []) await fn(...args);
|
|
97
|
+
};
|
|
98
|
+
for (const ev of liveEvents) await emit("session/event", session, ev);
|
|
99
|
+
await emit("agent/turn-stopping", { agent, turn });
|
|
100
|
+
for (let i = 0; i < 400 && !seen.length; i++) await new Promise((r) => setTimeout(r, 10));
|
|
101
|
+
const lastSeq = Math.max(...liveEvents.map((e) => Number(e.seq) || 0), ...seedEvents.map((e) => Number(e.seq) || 0));
|
|
102
|
+
for (let i = 0; i < 300; i++) {
|
|
103
|
+
const f = join(root, "_extract-state.json");
|
|
104
|
+
if (existsSync(f)) {
|
|
105
|
+
try {
|
|
106
|
+
const s = JSON.parse(readFileSync(f, "utf8"));
|
|
107
|
+
if ((s?.cursorBySession?.[id]?.seq || 0) >= lastSeq) break;
|
|
108
|
+
} catch {
|
|
109
|
+
/* 写一半 */
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
113
|
+
}
|
|
114
|
+
const state = existsSync(join(root, "_extract-state.json"))
|
|
115
|
+
? JSON.parse(readFileSync(join(root, "_extract-state.json"), "utf8"))
|
|
116
|
+
: null;
|
|
117
|
+
try {
|
|
118
|
+
rmSync(root, { recursive: true, force: true });
|
|
119
|
+
rmSync(cwd, { recursive: true, force: true });
|
|
120
|
+
} catch {
|
|
121
|
+
/* noop */
|
|
122
|
+
}
|
|
123
|
+
return { prompt: seen[0] || "", prompts: seen, calls: seen.length, state, id, lastSeq };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// 1. 没有持久化游标:恢复的历史要补进这次抽取(seed 段 seq < firstLiveSeq)。
|
|
127
|
+
{
|
|
128
|
+
const r = await runScenario({ id: "sess-resume-1" });
|
|
129
|
+
check(r.calls >= 1, "resumed turn should trigger extraction");
|
|
130
|
+
check(r.prompt.includes("HISTORY-FACT"), "seeded history must be recoverable", r.prompt.slice(0, 500));
|
|
131
|
+
check(r.prompt.includes("pnpm release"), "seeded user fact must reach the prompt");
|
|
132
|
+
check(r.prompt.includes("NEW-FACT"), "live message must still be included");
|
|
133
|
+
check(r.prompt.includes("OLD-TOOL-OUTPUT"), "seeded tool output should be recoverable");
|
|
134
|
+
check(r.state?.cursorBySession?.[r.id]?.seq === r.lastSeq, "cursor must cover the live tail", r.state?.cursorBySession);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// 2. 有持久化游标 2:只补 seq > 2 的部分,已覆盖的 HISTORY-FACT(seq 1)不能再送一遍。
|
|
138
|
+
{
|
|
139
|
+
const r = await runScenario({ id: "sess-resume-2", seedCursor: 2 });
|
|
140
|
+
check(r.calls >= 1, "with cursor, extraction should still run");
|
|
141
|
+
check(!r.prompt.includes("HISTORY-FACT"), "already-extracted interval must not be re-read", r.prompt.slice(0, 400));
|
|
142
|
+
check(r.prompt.includes("NEW-FACT"), "live message must be included");
|
|
143
|
+
check(r.state?.cursorBySession?.[r.id]?.seq === r.lastSeq, "cursor must advance to live tail", r.state?.cursorBySession);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// 3. 恢复积压 > 1 窗:seed 120 条、游标 2——必须全部补抽,不能被 trim 丢成 48 条后游标越过。
|
|
147
|
+
{
|
|
148
|
+
const seed = [{ type: "turn/start", seq: 0, data: { turn: 1 } }];
|
|
149
|
+
seed.push({ type: "user/message", seq: 1, data: { source: { kind: "user" }, content: [{ type: "text", text: "SEED#USER# 请把下面这些工具结论都整理好。" }] } });
|
|
150
|
+
for (let i = 1; i <= 120; i++) {
|
|
151
|
+
seed.push({ type: "tool/result", seq: i + 1, data: { turn: 1, message: { toolCallId: `c${i}`, content: [{ type: "text", text: `SEED#${i}#` }] } } });
|
|
152
|
+
}
|
|
153
|
+
const live = [
|
|
154
|
+
{ type: "turn/start", seq: 122, data: { turn: 2 } },
|
|
155
|
+
{ type: "user/message", seq: 123, data: { source: { kind: "user" }, content: [{ type: "text", text: "把新事实 LIVE#FACT# 也记下来。" }] } },
|
|
156
|
+
];
|
|
157
|
+
const r = await runScenario({ id: "sess-resume-3", seedCursor: 2, seedEvents: seed, liveEvents: live, firstLiveSeq: 122 });
|
|
158
|
+
check(r.calls >= 1, `recovered backlog should be extracted (calls=${r.calls})`);
|
|
159
|
+
check(r.state?.cursorBySession?.[r.id]?.seq === r.lastSeq, "cursor must cover the recovered backlog tail");
|
|
160
|
+
check((r.state?.droppedUnextracted || 0) === 0, "recovered backlog must not be trimmed away", r.state?.droppedUnextracted);
|
|
161
|
+
const joined = r.prompts.join("\n---\n");
|
|
162
|
+
check(joined.includes("SEED#120#"), "oldest-to-newest recovery must reach the last seed message", joined.slice(-300));
|
|
163
|
+
// cursor=2 表示 seq≤2 已抽过:SEED#1 在 seq2,不应再送;从 SEED#2(seq3)开始。
|
|
164
|
+
check(!joined.includes("SEED#1#"), "already-extracted seed edge must not be re-read");
|
|
165
|
+
check(joined.includes("SEED#2#"), "recovery must start from the cursor edge", joined.slice(0, 300));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// 4. 游标落在回合中间、且没有 live turn/start:仍要恢复 st.turn,否则误判 short-prose。
|
|
169
|
+
{
|
|
170
|
+
const seed = [
|
|
171
|
+
{ type: "turn/start", seq: 890, data: { turn: 2 } },
|
|
172
|
+
{ type: "user/message", seq: 901, data: { source: { kind: "user" }, content: [{ type: "text", text: "MIDTURN#USER# 这段是游标之后很长的用户消息,不应该被跳过。" }] } },
|
|
173
|
+
];
|
|
174
|
+
for (let i = 1; i <= 40; i++) {
|
|
175
|
+
seed.push({ type: "tool/result", seq: 901 + i, data: { turn: 2, message: { toolCallId: `m${i}`, content: [{ type: "text", text: `MIDTURN#${i}#` }] } } });
|
|
176
|
+
}
|
|
177
|
+
const r = await runScenario({
|
|
178
|
+
id: "sess-resume-4",
|
|
179
|
+
seedCursor: 900,
|
|
180
|
+
seedEvents: seed,
|
|
181
|
+
liveEvents: [{ type: "tool/result", seq: 942, data: { turn: 2, message: { toolCallId: "m41", content: [{ type: "text", text: "MIDTURN#41#" }] } } }],
|
|
182
|
+
firstLiveSeq: 941,
|
|
183
|
+
turn: 2,
|
|
184
|
+
});
|
|
185
|
+
check(r.calls >= 1, "mid-turn resume must not be skipped as short-prose", r.calls);
|
|
186
|
+
check(r.prompt.includes("MIDTURN#USER#"), "mid-turn user message must reach the prompt", r.prompt.slice(0, 400));
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// 5. fork 子会话:父会话前缀(< inheritedEventCount)不能重抽。
|
|
190
|
+
{
|
|
191
|
+
const seed = [
|
|
192
|
+
{ type: "turn/start", seq: 0, data: { turn: 1 } },
|
|
193
|
+
{ type: "user/message", seq: 1, data: { source: { kind: "user" }, content: [{ type: "text", text: "PARENT#FACT# 父会话已经抽过的内容。" }] } },
|
|
194
|
+
{ type: "assistant/message", seq: 2, data: { turn: 1, message: { content: [{ type: "text", text: "父会话回复。" }] } } },
|
|
195
|
+
{ type: "tool/result", seq: 3, data: { turn: 1, message: { toolCallId: "p1", content: [{ type: "text", text: "PARENT#TOOL#" }] } } },
|
|
196
|
+
{ type: "user/message", seq: 4, data: { source: { kind: "user" }, content: [{ type: "text", text: "PARENT#FACT2# 父会话第二条。" }] } },
|
|
197
|
+
{ type: "turn/start", seq: 5, data: { turn: 2 } },
|
|
198
|
+
{ type: "user/message", seq: 6, data: { source: { kind: "user" }, content: [{ type: "text", text: "CHILD#FACT# 分叉后子会话自己的事实。" }] } },
|
|
199
|
+
{ type: "assistant/message", seq: 7, data: { turn: 2, message: { content: [{ type: "text", text: "子会话回复。" }] } } },
|
|
200
|
+
{ type: "tool/result", seq: 8, data: { turn: 2, message: { toolCallId: "c1", content: [{ type: "text", text: "CHILD#TOOL#" }] } } },
|
|
201
|
+
];
|
|
202
|
+
const live = [
|
|
203
|
+
{ type: "turn/start", seq: 9, data: { turn: 3 } },
|
|
204
|
+
{ type: "user/message", seq: 10, data: { source: { kind: "user" }, content: [{ type: "text", text: "LIVE#FACT# 继续子会话。" }] } },
|
|
205
|
+
];
|
|
206
|
+
const r = await runScenario({
|
|
207
|
+
id: "sess-resume-5",
|
|
208
|
+
seedEvents: seed,
|
|
209
|
+
liveEvents: live,
|
|
210
|
+
firstLiveSeq: 9,
|
|
211
|
+
inheritedEventCount: 6,
|
|
212
|
+
headerExtra: { isSeeded: true, parentSession: "sess-parent" },
|
|
213
|
+
turn: 3,
|
|
214
|
+
});
|
|
215
|
+
const joined = r.prompts.join("\n---\n");
|
|
216
|
+
check(!joined.includes("PARENT#FACT#"), "fork must not re-extract the parent prefix", joined.slice(0, 400));
|
|
217
|
+
check(!joined.includes("PARENT#FACT2#"), "fork must not re-extract parent messages after inherited cut");
|
|
218
|
+
check(joined.includes("CHILD#FACT#"), "fork must keep its own child events");
|
|
219
|
+
check(joined.includes("LIVE#FACT#"), "fork must include live events");
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// 6. 跳过回合(用户文本太短)也要把推进后的游标落盘,重启后不重复分析。
|
|
223
|
+
{
|
|
224
|
+
const live = [
|
|
225
|
+
{ type: "turn/start", seq: 7, data: { turn: 2 } },
|
|
226
|
+
{ type: "user/message", seq: 8, data: { source: { kind: "user" }, content: [{ type: "text", text: "ok" }] } },
|
|
227
|
+
];
|
|
228
|
+
const r = await runScenario({
|
|
229
|
+
id: "sess-resume-6",
|
|
230
|
+
seedEvents: [],
|
|
231
|
+
liveEvents: live,
|
|
232
|
+
firstLiveSeq: 0,
|
|
233
|
+
turn: 2,
|
|
234
|
+
});
|
|
235
|
+
check(r.calls === 0, "short prose turn must not call the model", r.calls);
|
|
236
|
+
check(r.state?.lastSkipReason === "short-prose", "skip reason recorded", r.state?.lastSkipReason);
|
|
237
|
+
check(r.state?.cursorBySession?.[r.id]?.seq === 8, "skipped turn must persist the advanced cursor", r.state?.cursorBySession);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (fails.length) {
|
|
241
|
+
console.error("FAIL resume-recovery");
|
|
242
|
+
for (const f of fails) console.error(" -", f);
|
|
243
|
+
process.exit(1);
|
|
244
|
+
}
|
|
245
|
+
console.log("PASS resume-recovery");
|