@reinsjs/core 0.1.0 → 0.2.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.
@@ -1,4 +1,4 @@
1
- import { B as BlobStore, E as EventLog, a as Event, M as MemoryStore, f as LoweringCapabilities, o as CoreEventDraft, Q as LoweringOutcome, a4 as ToRequestInput, L as Lowering, e as ModelRef, k as LoweredRequest, U as LoweringStreamContext, c as EventDraft } from '../types-BeGSiC1e.js';
1
+ import { B as BlobStore, E as EventLog, a as Event, M as MemoryStore, r as RunLease, f as LoweringCapabilities, o as CoreEventDraft, U as LoweringOutcome, a5 as ToRequestInput, L as Lowering, e as ModelRef, k as LoweredRequest, V as LoweringStreamContext, c as EventDraft } from '../types-DJB-13o3.js';
2
2
 
3
3
  /**
4
4
  * 一致性套件对测试框架的最小要求:只要 describe / it 两个函数。
@@ -37,6 +37,13 @@ declare function eventLogConformance(t: TestHarness, factory: () => EventLog | P
37
37
 
38
38
  declare function memoryStoreConformance(t: TestHarness, factory: () => MemoryStore | Promise<MemoryStore>): void;
39
39
 
40
+ /**
41
+ * RunLease 一致性套件(D4)。过期一项用真时间等(ttl 1 ms、等 30 ms):套件不知道后端的时钟怎么注入,
42
+ * 而 Postgres 实现刻意用数据库时间,测试时钟只能是真时钟。
43
+ */
44
+
45
+ declare function runLeaseConformance(t: TestHarness, factory: () => RunLease | Promise<RunLease>): void;
46
+
40
47
  /**
41
48
  * 脚本化降级层:按剧本逐轮吐出预设的事件草稿,不联网。
42
49
  * 给循环、脑子模块与适配器写单测用 —— 只关心"模型看到了什么、说了什么",不关心线协议。
@@ -74,4 +81,4 @@ declare const say: (text: string) => CoreEventDraft;
74
81
  declare const think: (text: string) => CoreEventDraft;
75
82
  declare const callTool: (toolCallId: string, name: string, args: unknown) => CoreEventDraft;
76
83
 
77
- export { ConformanceError, SCRIPTED_CAPABILITIES, type Script, ScriptedLowering, type ScriptedPayload, type ScriptedTurn, type TestHarness, assert, assertEqual, assertThrowsCode, blobStoreConformance, callTool, collect, eventLogConformance, makeEvents, memoryStoreConformance, say, think };
84
+ export { ConformanceError, SCRIPTED_CAPABILITIES, type Script, ScriptedLowering, type ScriptedPayload, type ScriptedTurn, type TestHarness, assert, assertEqual, assertThrowsCode, blobStoreConformance, callTool, collect, eventLogConformance, makeEvents, memoryStoreConformance, runLeaseConformance, say, think };
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  createCoreEvent,
3
3
  createCoreRegistry
4
- } from "../chunk-BOJD54XN.js";
4
+ } from "../chunk-63AJKR7J.js";
5
5
 
6
6
  // src/testing/harness.ts
7
7
  var ConformanceError = class extends Error {
@@ -17,8 +17,8 @@ function assertEqual(actual, expected, message) {
17
17
  const a = JSON.stringify(actual);
18
18
  const b = JSON.stringify(expected);
19
19
  if (a !== b) throw new ConformanceError(`${message}
20
- \u671F\u671B\uFF1A${b}
21
- \u5B9E\u9645\uFF1A${a}`);
20
+ expected: ${b}
21
+ actual: ${a}`);
22
22
  }
23
23
  async function assertThrowsCode(fn, code, message) {
24
24
  try {
@@ -28,11 +28,11 @@ async function assertThrowsCode(fn, code, message) {
28
28
  if (got === code) return;
29
29
  throw new ConformanceError(
30
30
  `${message}
31
- \u671F\u671B\u9519\u8BEF\u7801 ${code}\uFF0C\u5B9E\u9645 ${String(got)}\uFF1A${err.message}`
31
+ expected error code ${code}, got ${String(got)}: ${err.message}`
32
32
  );
33
33
  }
34
34
  throw new ConformanceError(`${message}
35
- \u671F\u671B\u629B\u51FA ${code}\uFF0C\u4F46\u6CA1\u6709\u629B\u9519`);
35
+ expected ${code} to be thrown, but nothing was`);
36
36
  }
37
37
  async function collect(iter) {
38
38
  const out = [];
@@ -42,51 +42,59 @@ async function collect(iter) {
42
42
 
43
43
  // src/testing/blob-store.ts
44
44
  function blobStoreConformance(t, factory) {
45
- t.describe("BlobStore \u4E00\u81F4\u6027", () => {
46
- t.it("put \u5B57\u8282 \u2192 get \u539F\u6837\u53D6\u56DE\uFF0Cmeta \u5E26 size \u4E0E mime", async () => {
45
+ t.describe("BlobStore conformance", () => {
46
+ t.it("put bytes -> get returns them verbatim, with size and mime in meta", async () => {
47
47
  const store = await factory();
48
48
  const bytes = new Uint8Array([1, 2, 3, 250]);
49
49
  const { id } = await store.put(bytes, { mime: "application/octet-stream", sessionId: "s1" });
50
- assert(typeof id === "string" && id.length > 0, "id \u5E94\u4E3A\u975E\u7A7A\u5B57\u7B26\u4E32");
50
+ assert(typeof id === "string" && id.length > 0, "id must be a non-empty string");
51
51
  const got = await store.get(id);
52
- assertEqual(Array.from(got.bytes), [1, 2, 3, 250], "\u5B57\u8282\u5185\u5BB9");
52
+ assertEqual(Array.from(got.bytes), [1, 2, 3, 250], "byte content");
53
53
  assertEqual(got.meta.mime, "application/octet-stream", "mime");
54
54
  assertEqual(got.meta.sessionId, "s1", "sessionId");
55
55
  assertEqual(got.meta.size, 4, "size");
56
- assert(typeof got.meta.createdAt === "number", "createdAt \u5E94\u4E3A\u6570\u5B57");
56
+ assert(typeof got.meta.createdAt === "number", "createdAt must be a number");
57
57
  });
58
- t.it("put \u5B57\u7B26\u4E32\u6309 UTF-8 \u5B58\u50A8", async () => {
58
+ t.it("a string is stored as UTF-8", async () => {
59
59
  const store = await factory();
60
60
  const { id } = await store.put("\u4F60\u597D, world", { mime: "text/plain", sessionId: "s1" });
61
61
  const got = await store.get(id);
62
- assertEqual(new TextDecoder().decode(got.bytes), "\u4F60\u597D, world", "\u89E3\u7801\u540E\u6587\u672C");
63
- assertEqual(got.meta.size, new TextEncoder().encode("\u4F60\u597D, world").byteLength, "size \u4E3A UTF-8 \u5B57\u8282\u6570");
62
+ assertEqual(new TextDecoder().decode(got.bytes), "\u4F60\u597D, world", "decoded text");
63
+ assertEqual(
64
+ got.meta.size,
65
+ new TextEncoder().encode("\u4F60\u597D, world").byteLength,
66
+ "size is the UTF-8 byte length"
67
+ );
64
68
  });
65
- t.it("\u6BCF\u6B21 put \u5F97\u5230\u4E0D\u540C id", async () => {
69
+ t.it("every put yields a different id", async () => {
66
70
  const store = await factory();
67
71
  const a = await store.put("x", { mime: "text/plain", sessionId: "s1" });
68
72
  const b = await store.put("x", { mime: "text/plain", sessionId: "s1" });
69
- assert(a.id !== b.id, "\u76F8\u540C\u5185\u5BB9\u4E24\u6B21 put \u4E5F\u5E94\u5F97\u5230\u4E0D\u540C id");
73
+ assert(a.id !== b.id, "putting the same content twice must still yield different ids");
70
74
  });
71
- t.it("get \u4E0D\u5B58\u5728\u7684 id \u2192 not_found", async () => {
75
+ t.it("get on an unknown id -> not_found", async () => {
72
76
  const store = await factory();
73
- await assertThrowsCode(() => store.get("no-such-id"), "not_found", "\u7F3A\u5931 blob");
77
+ await assertThrowsCode(() => store.get("no-such-id"), "not_found", "missing blob");
74
78
  });
75
- t.it("get \u8FD4\u56DE\u7684\u5B57\u8282\u662F\u526F\u672C", async () => {
79
+ t.it("get returns a copy of the bytes", async () => {
76
80
  const store = await factory();
77
81
  const { id } = await store.put(new Uint8Array([9, 9]), { mime: "x", sessionId: "s1" });
78
82
  const first = await store.get(id);
79
83
  first.bytes[0] = 0;
80
84
  const again = await store.get(id);
81
- assertEqual(Array.from(again.bytes), [9, 9], "\u5916\u90E8\u4FEE\u6539\u4E0D\u5E94\u5F71\u54CD\u5B58\u50A8");
85
+ assertEqual(Array.from(again.bytes), [9, 9], "an outside mutation must not affect the store");
82
86
  });
83
- t.it("slice\uFF08\u82E5\u5B9E\u73B0\uFF09\u8FD4\u56DE [start, end) \u4E14\u8D8A\u754C\u622A\u65AD", async () => {
87
+ t.it("slice (if implemented) returns [start, end) and clamps out-of-range bounds", async () => {
84
88
  const store = await factory();
85
89
  if (!store.slice) return;
86
90
  const { id } = await store.put(new Uint8Array([0, 1, 2, 3, 4]), { mime: "x", sessionId: "s1" });
87
- assertEqual(Array.from(await store.slice(id, { start: 1, end: 3 })), [1, 2], "\u4E2D\u6BB5");
88
- assertEqual(Array.from(await store.slice(id, { start: 3, end: 99 })), [3, 4], "\u5C3E\u90E8\u8D8A\u754C\u622A\u65AD");
89
- assertEqual(Array.from(await store.slice(id, { start: 5, end: 9 })), [], "\u8D77\u70B9\u8D8A\u754C\u5F97\u7A7A");
91
+ assertEqual(Array.from(await store.slice(id, { start: 1, end: 3 })), [1, 2], "middle slice");
92
+ assertEqual(Array.from(await store.slice(id, { start: 3, end: 99 })), [3, 4], "clamped at the end");
93
+ assertEqual(
94
+ Array.from(await store.slice(id, { start: 5, end: 9 })),
95
+ [],
96
+ "a start beyond the end gives nothing"
97
+ );
90
98
  });
91
99
  });
92
100
  }
@@ -108,76 +116,95 @@ function makeEvents(sessionId, n, startSeq = 1) {
108
116
  }
109
117
  var seqs = (events) => events.map((e) => e.seq);
110
118
  function eventLogConformance(t, factory) {
111
- t.describe("EventLog \u4E00\u81F4\u6027", () => {
112
- t.it("\u7A7A\u4F1A\u8BDD\u8BFB\u51FA\u7A7A\u6D41\uFF0Ctail \u4E3A\u7A7A", async () => {
119
+ t.describe("EventLog conformance", () => {
120
+ t.it("an empty session reads back an empty stream and an empty tail", async () => {
113
121
  const log = await factory();
114
- assertEqual(await collect(log.read("none")), [], "\u7A7A\u4F1A\u8BDD read");
115
- assertEqual(await log.tail("none", 5), [], "\u7A7A\u4F1A\u8BDD tail");
122
+ assertEqual(await collect(log.read("none")), [], "read on an empty session");
123
+ assertEqual(await log.tail("none", 5), [], "tail on an empty session");
116
124
  });
117
- t.it("append \u540E\u6309 seq \u5347\u5E8F\u8BFB\u56DE\uFF0C\u5185\u5BB9\u9010\u5B57\u6BB5\u4E00\u81F4", async () => {
125
+ t.it("after append, events read back in ascending seq order, field for field", async () => {
118
126
  const log = await factory();
119
127
  const events = makeEvents("s1", 3);
120
128
  await log.append(events);
121
- assertEqual(await collect(log.read("s1")), events, "read \u5168\u91CF");
129
+ assertEqual(await collect(log.read("s1")), events, "full read");
122
130
  });
123
- t.it("\u591A\u6279 append \u8FDE\u7EED\u7D2F\u79EF", async () => {
131
+ t.it("successive append batches accumulate", async () => {
124
132
  const log = await factory();
125
133
  await log.append(makeEvents("s1", 2));
126
134
  await log.append(makeEvents("s1", 2, 3));
127
- assertEqual(seqs(await collect(log.read("s1"))), [1, 2, 3, 4], "\u4E24\u6279\u5408\u5E76\u540E seq");
135
+ assertEqual(seqs(await collect(log.read("s1"))), [1, 2, 3, 4], "seq after merging two batches");
128
136
  });
129
- t.it("read \u652F\u6301 fromSeq / toSeq \u95ED\u533A\u95F4", async () => {
137
+ t.it("read supports the closed range fromSeq / toSeq", async () => {
130
138
  const log = await factory();
131
139
  await log.append(makeEvents("s1", 5));
132
- assertEqual(seqs(await collect(log.read("s1", { fromSeq: 2, toSeq: 4 }))), [2, 3, 4], "\u95ED\u533A\u95F4");
133
- assertEqual(seqs(await collect(log.read("s1", { fromSeq: 4 }))), [4, 5], "\u53EA\u7ED9 from");
134
- assertEqual(seqs(await collect(log.read("s1", { toSeq: 2 }))), [1, 2], "\u53EA\u7ED9 to");
135
- assertEqual(seqs(await collect(log.read("s1", { fromSeq: 9 }))), [], "from \u8D8A\u754C\u5F97\u7A7A");
136
- });
137
- t.it("tail \u8FD4\u56DE\u6700\u540E n \u6761\uFF0C\u5347\u5E8F\uFF1Bn \u5927\u4E8E\u603B\u6570\u8FD4\u56DE\u5168\u90E8\uFF1Bn=0 \u8FD4\u56DE\u7A7A", async () => {
138
- const log = await factory();
139
- await log.append(makeEvents("s1", 5));
140
- assertEqual(seqs(await log.tail("s1", 2)), [4, 5], "tail 2");
141
- assertEqual(seqs(await log.tail("s1", 10)), [1, 2, 3, 4, 5], "tail \u8D85\u91CF");
142
- assertEqual(seqs(await log.tail("s1", 0)), [], "tail 0");
140
+ assertEqual(seqs(await collect(log.read("s1", { fromSeq: 2, toSeq: 4 }))), [2, 3, 4], "closed range");
141
+ assertEqual(seqs(await collect(log.read("s1", { fromSeq: 4 }))), [4, 5], "from only");
142
+ assertEqual(seqs(await collect(log.read("s1", { toSeq: 2 }))), [1, 2], "to only");
143
+ assertEqual(
144
+ seqs(await collect(log.read("s1", { fromSeq: 9 }))),
145
+ [],
146
+ "a from beyond the end gives nothing"
147
+ );
143
148
  });
144
- t.it("\u4F1A\u8BDD\u4E4B\u95F4\u9694\u79BB", async () => {
149
+ t.it(
150
+ "tail returns the last n events in ascending order; an n above the total returns everything; n=0 returns nothing",
151
+ async () => {
152
+ const log = await factory();
153
+ await log.append(makeEvents("s1", 5));
154
+ assertEqual(seqs(await log.tail("s1", 2)), [4, 5], "tail 2");
155
+ assertEqual(seqs(await log.tail("s1", 10)), [1, 2, 3, 4, 5], "tail beyond the total");
156
+ assertEqual(seqs(await log.tail("s1", 0)), [], "tail 0");
157
+ }
158
+ );
159
+ t.it("sessions are isolated from each other", async () => {
145
160
  const log = await factory();
146
161
  await log.append(makeEvents("a", 2));
147
162
  await log.append(makeEvents("b", 3));
148
- assertEqual(seqs(await collect(log.read("a"))), [1, 2], "\u4F1A\u8BDD a");
149
- assertEqual(seqs(await collect(log.read("b"))), [1, 2, 3], "\u4F1A\u8BDD b");
163
+ assertEqual(seqs(await collect(log.read("a"))), [1, 2], "session a");
164
+ assertEqual(seqs(await collect(log.read("b"))), [1, 2, 3], "session b");
150
165
  });
151
- t.it("\u9996\u6761 seq \u5FC5\u987B\u4E3A 1", async () => {
166
+ t.it("the first event must have seq 1", async () => {
152
167
  const log = await factory();
153
- await assertThrowsCode(() => log.append(makeEvents("s1", 1, 2)), "seq_conflict", "\u9996\u6761 seq=2 \u5E94\u62D2\u7EDD");
154
- assertEqual(await collect(log.read("s1")), [], "\u62D2\u7EDD\u540E\u4E0D\u5E94\u7559\u4E0B\u4EFB\u4F55\u4E8B\u4EF6");
168
+ await assertThrowsCode(
169
+ () => log.append(makeEvents("s1", 1, 2)),
170
+ "seq_conflict",
171
+ "a first event with seq=2 must be rejected"
172
+ );
173
+ assertEqual(await collect(log.read("s1")), [], "nothing must be left behind after a rejection");
155
174
  });
156
- t.it("seq \u4E0D\u8FDE\u7EED \u2192 seq_conflict\uFF0C\u4E14\u6574\u6279\u4E0D\u5199\u5165\uFF08\u539F\u5B50\uFF09", async () => {
175
+ t.it("a gap in seq -> seq_conflict, and the whole batch is not written (atomic)", async () => {
157
176
  const log = await factory();
158
177
  await log.append(makeEvents("s1", 2));
159
178
  const bad = [...makeEvents("s1", 1, 3), ...makeEvents("s1", 1, 5)];
160
- await assertThrowsCode(() => log.append(bad), "seq_conflict", "\u8DF3\u53F7\u5E94\u62D2\u7EDD");
161
- assertEqual(seqs(await collect(log.read("s1"))), [1, 2], "\u6574\u6279\u56DE\u6EDA\uFF0Cseq=3 \u4E5F\u4E0D\u80FD\u5199\u8FDB\u53BB");
179
+ await assertThrowsCode(() => log.append(bad), "seq_conflict", "a gap must be rejected");
180
+ assertEqual(
181
+ seqs(await collect(log.read("s1"))),
182
+ [1, 2],
183
+ "the whole batch rolls back, so seq=3 must not land either"
184
+ );
162
185
  });
163
- t.it("\u91CD\u590D seq\uFF08\u5E76\u53D1\u5199\u5165\u8005\uFF09\u2192 seq_conflict", async () => {
186
+ t.it("a duplicate seq (a concurrent writer) -> seq_conflict", async () => {
164
187
  const log = await factory();
165
188
  await log.append(makeEvents("s1", 2));
166
- await assertThrowsCode(() => log.append(makeEvents("s1", 1, 2)), "seq_conflict", "\u91CD\u590D seq=2 \u5E94\u62D2\u7EDD");
189
+ await assertThrowsCode(
190
+ () => log.append(makeEvents("s1", 1, 2)),
191
+ "seq_conflict",
192
+ "a duplicate seq=2 must be rejected"
193
+ );
167
194
  });
168
- t.it("\u4E00\u6279\u6DF7\u591A\u4E2A\u4F1A\u8BDD \u2192 session_mismatch", async () => {
195
+ t.it("mixing sessions in one batch -> session_mismatch", async () => {
169
196
  const log = await factory();
170
197
  await assertThrowsCode(
171
198
  () => log.append([...makeEvents("a", 1), ...makeEvents("b", 1, 2)]),
172
199
  "session_mismatch",
173
- "\u6DF7\u4F1A\u8BDD\u5E94\u62D2\u7EDD"
200
+ "a mixed-session batch must be rejected"
174
201
  );
175
202
  });
176
- t.it("\u7A7A\u6570\u7EC4 \u2192 empty_batch", async () => {
203
+ t.it("an empty array -> empty_batch", async () => {
177
204
  const log = await factory();
178
- await assertThrowsCode(() => log.append([]), "empty_batch", "\u7A7A\u6279\u5E94\u62D2\u7EDD");
205
+ await assertThrowsCode(() => log.append([]), "empty_batch", "an empty batch must be rejected");
179
206
  });
180
- t.it("\u8BFB\u51FA\u7684\u5BF9\u8C61\u662F\u526F\u672C\uFF1A\u6539\u5B83\u4E0D\u5F71\u54CD\u65E5\u5FD7", async () => {
207
+ t.it("events read back are copies: mutating them does not affect the log", async () => {
181
208
  const log = await factory();
182
209
  await log.append(makeEvents("s1", 1));
183
210
  const [first] = await collect(log.read("s1"));
@@ -186,10 +213,10 @@ function eventLogConformance(t, factory) {
186
213
  assertEqual(
187
214
  again.payload.content.length,
188
215
  1,
189
- "\u5185\u90E8\u6570\u636E\u88AB\u5916\u90E8\u7BE1\u6539"
216
+ "internal data was tampered with from outside"
190
217
  );
191
218
  });
192
- t.it("fork\uFF1A\u590D\u5236 [1, atSeq]\uFF0C\u4FDD\u7559 id \u4E0E seq\uFF0C\u53EA\u6362 sessionId", async () => {
219
+ t.it("fork: copies [1, atSeq], keeping id and seq, changing only sessionId", async () => {
193
220
  const log = await factory();
194
221
  const src = makeEvents("s1", 4);
195
222
  await log.append(src);
@@ -198,54 +225,58 @@ function eventLogConformance(t, factory) {
198
225
  assertEqual(
199
226
  forked,
200
227
  src.slice(0, 2).map((e) => ({ ...e, sessionId: "s2" })),
201
- "\u5206\u53C9\u5185\u5BB9"
228
+ "forked content"
202
229
  );
203
230
  });
204
- t.it("fork \u540E\u4E24\u6761\u4F1A\u8BDD\u72EC\u7ACB\u6F14\u8FDB", async () => {
231
+ t.it("after a fork the two sessions evolve independently", async () => {
205
232
  const log = await factory();
206
233
  await log.append(makeEvents("s1", 2));
207
234
  await log.fork("s1", 2, "s2");
208
235
  await log.append(makeEvents("s1", 1, 3));
209
236
  await log.append(makeEvents("s2", 2, 3));
210
- assertEqual(seqs(await collect(log.read("s1"))), [1, 2, 3], "\u6E90\u4F1A\u8BDD");
211
- assertEqual(seqs(await collect(log.read("s2"))), [1, 2, 3, 4], "\u5206\u53C9\u4F1A\u8BDD");
237
+ assertEqual(seqs(await collect(log.read("s1"))), [1, 2, 3], "source session");
238
+ assertEqual(seqs(await collect(log.read("s2"))), [1, 2, 3, 4], "forked session");
212
239
  const s2 = await collect(log.read("s2"));
213
240
  assert(
214
241
  s2.every((e) => e.sessionId === "s2"),
215
- "\u5206\u53C9\u4F1A\u8BDD\u91CC\u6240\u6709\u4E8B\u4EF6 sessionId \u5E94\u4E3A s2"
242
+ "every event in the forked session must carry sessionId s2"
216
243
  );
217
244
  });
218
- t.it("fork \u76EE\u6807\u5DF2\u6709\u4E8B\u4EF6 \u2192 target_not_empty", async () => {
245
+ t.it("a fork target that already has events -> target_not_empty", async () => {
219
246
  const log = await factory();
220
247
  await log.append(makeEvents("s1", 2));
221
248
  await log.append(makeEvents("s2", 1));
222
- await assertThrowsCode(() => log.fork("s1", 1, "s2"), "target_not_empty", "\u76EE\u6807\u975E\u7A7A\u5E94\u62D2\u7EDD");
249
+ await assertThrowsCode(
250
+ () => log.fork("s1", 1, "s2"),
251
+ "target_not_empty",
252
+ "a non-empty target must be rejected"
253
+ );
223
254
  });
224
- t.it("fork \u7684 atSeq \u8D8A\u754C\u6216\u6E90\u4F1A\u8BDD\u4E0D\u5B58\u5728 \u2192 out_of_range", async () => {
255
+ t.it("an out-of-range atSeq or an unknown source session -> out_of_range", async () => {
225
256
  const log = await factory();
226
257
  await log.append(makeEvents("s1", 2));
227
- await assertThrowsCode(() => log.fork("s1", 3, "s2"), "out_of_range", "atSeq \u8D85\u51FA\u672B\u5C3E");
258
+ await assertThrowsCode(() => log.fork("s1", 3, "s2"), "out_of_range", "atSeq beyond the end");
228
259
  await assertThrowsCode(() => log.fork("s1", 0, "s2"), "out_of_range", "atSeq=0");
229
- await assertThrowsCode(() => log.fork("ghost", 1, "s2"), "out_of_range", "\u6E90\u4F1A\u8BDD\u4E0D\u5B58\u5728");
260
+ await assertThrowsCode(() => log.fork("ghost", 1, "s2"), "out_of_range", "unknown source session");
230
261
  });
231
262
  });
232
263
  }
233
264
 
234
265
  // src/testing/memory-store.ts
235
266
  function memoryStoreConformance(t, factory) {
236
- t.describe("MemoryStore \u4E00\u81F4\u6027", () => {
237
- t.it("read \u4E0D\u5B58\u5728\u7684\u8DEF\u5F84\u8FD4\u56DE null", async () => {
267
+ t.describe("MemoryStore conformance", () => {
268
+ t.it("read on an unknown path returns null", async () => {
238
269
  const store = await factory();
239
- assertEqual(await store.read("/memories/none.md"), null, "\u7F3A\u5931\u8DEF\u5F84");
270
+ assertEqual(await store.read("/memories/none.md"), null, "missing path");
240
271
  });
241
- t.it("write \u540E read \u539F\u6837\u8FD4\u56DE\uFF1B\u518D\u6B21 write \u8986\u76D6", async () => {
272
+ t.it("read returns what write stored; a second write overwrites it", async () => {
242
273
  const store = await factory();
243
274
  await store.write("/memories/a.md", "v1");
244
- assertEqual(await store.read("/memories/a.md"), "v1", "\u9996\u6B21\u5199");
275
+ assertEqual(await store.read("/memories/a.md"), "v1", "first write");
245
276
  await store.write("/memories/a.md", "v2");
246
- assertEqual(await store.read("/memories/a.md"), "v2", "\u8986\u76D6\u5199");
277
+ assertEqual(await store.read("/memories/a.md"), "v2", "overwriting write");
247
278
  });
248
- t.it("list \u6309\u524D\u7F00\u8FC7\u6EE4\u5E76\u6309\u5B57\u5178\u5E8F\u8FD4\u56DE", async () => {
279
+ t.it("list filters by prefix and returns paths in lexicographic order", async () => {
249
280
  const store = await factory();
250
281
  await store.write("/memories/b.md", "");
251
282
  await store.write("/memories/a.md", "");
@@ -254,24 +285,105 @@ function memoryStoreConformance(t, factory) {
254
285
  assertEqual(
255
286
  await store.list("/memories/"),
256
287
  ["/memories/a.md", "/memories/b.md", "/memories/sub/c.md"],
257
- "\u524D\u7F00 /memories/"
288
+ "prefix /memories/"
258
289
  );
259
- assertEqual(await store.list("/memories/sub/"), ["/memories/sub/c.md"], "\u5B50\u76EE\u5F55\u524D\u7F00");
260
- assertEqual(await store.list("/nope/"), [], "\u65E0\u5339\u914D");
261
- });
262
- t.it("delete \u540E read \u4E3A null\u3001list \u4E0D\u518D\u51FA\u73B0\uFF1B\u5220\u9664\u4E0D\u5B58\u5728\u7684\u8DEF\u5F84\u4E0D\u62A5\u9519", async () => {
263
- const store = await factory();
264
- await store.write("/memories/a.md", "x");
265
- await store.delete("/memories/a.md");
266
- assertEqual(await store.read("/memories/a.md"), null, "\u5220\u9664\u540E read");
267
- assertEqual(await store.list("/memories/"), [], "\u5220\u9664\u540E list");
268
- await store.delete("/memories/a.md");
290
+ assertEqual(await store.list("/memories/sub/"), ["/memories/sub/c.md"], "subdirectory prefix");
291
+ assertEqual(await store.list("/nope/"), [], "no match");
269
292
  });
270
- t.it("\u7A7A\u5B57\u7B26\u4E32\u662F\u5408\u6CD5\u5185\u5BB9\uFF0C\u4E0E\u4E0D\u5B58\u5728\u533A\u5206", async () => {
293
+ t.it(
294
+ "after delete, read is null and list no longer shows it; deleting an unknown path does not throw",
295
+ async () => {
296
+ const store = await factory();
297
+ await store.write("/memories/a.md", "x");
298
+ await store.delete("/memories/a.md");
299
+ assertEqual(await store.read("/memories/a.md"), null, "read after delete");
300
+ assertEqual(await store.list("/memories/"), [], "list after delete");
301
+ await store.delete("/memories/a.md");
302
+ }
303
+ );
304
+ t.it("an empty string is valid content, distinct from absence", async () => {
271
305
  const store = await factory();
272
306
  await store.write("/memories/empty.md", "");
273
- assertEqual(await store.read("/memories/empty.md"), "", "\u7A7A\u5185\u5BB9\u5E94\u8FD4\u56DE\u7A7A\u4E32\u800C\u975E null");
307
+ assertEqual(
308
+ await store.read("/memories/empty.md"),
309
+ "",
310
+ "empty content must read back as an empty string, not null"
311
+ );
312
+ });
313
+ });
314
+ }
315
+
316
+ // src/testing/run-lease.ts
317
+ var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
318
+ function runLeaseConformance(t, factory) {
319
+ t.describe("RunLease conformance", () => {
320
+ t.it(
321
+ "acquire succeeds on a free session, fails while someone else holds an unexpired lease, and sessions do not affect each other",
322
+ async () => {
323
+ const lease = await factory();
324
+ assertEqual(await lease.acquire("s1", "a", 6e4), true, "first acquire");
325
+ assertEqual(await lease.acquire("s1", "b", 6e4), false, "someone else is holding it");
326
+ assertEqual(await lease.acquire("s2", "b", 6e4), true, "another session is unaffected");
327
+ }
328
+ );
329
+ t.it(
330
+ "a repeated acquire by the same owner is idempotent (it renews) and does not change who holds the lease",
331
+ async () => {
332
+ const lease = await factory();
333
+ assertEqual(await lease.acquire("s1", "a", 6e4), true, "first");
334
+ assertEqual(await lease.acquire("s1", "a", 6e4), true, "the same owner acquires again");
335
+ assertEqual(await lease.acquire("s1", "b", 6e4), false, "someone else still cannot acquire");
336
+ assertEqual(await lease.renew("s1", "a", 6e4), true, "the owner can still renew");
337
+ }
338
+ );
339
+ t.it("renew: the holder succeeds; a non-holder fails; a never-acquired session fails", async () => {
340
+ const lease = await factory();
341
+ await lease.acquire("s1", "a", 6e4);
342
+ assertEqual(await lease.renew("s1", "a", 6e4), true, "the holder renews");
343
+ assertEqual(await lease.renew("s1", "b", 6e4), false, "a non-holder");
344
+ assertEqual(await lease.renew("nope", "a", 6e4), false, "an unknown session");
274
345
  });
346
+ t.it(
347
+ "release: after the holder releases, someone else can acquire; a release by a non-holder does nothing; releasing an unknown session does not throw",
348
+ async () => {
349
+ const lease = await factory();
350
+ await lease.acquire("s1", "a", 6e4);
351
+ await lease.release("s1", "b");
352
+ assertEqual(
353
+ await lease.acquire("s1", "b", 6e4),
354
+ false,
355
+ "a still holds the lease after a non-holder releases"
356
+ );
357
+ await lease.release("s1", "a");
358
+ assertEqual(
359
+ await lease.acquire("s1", "b", 6e4),
360
+ true,
361
+ "after the holder releases, the lease can be acquired again"
362
+ );
363
+ await lease.release("nope", "a");
364
+ }
365
+ );
366
+ t.it(
367
+ "expiry: once it lapses, someone else can take over, the old holder's renew is false, and the new holder renews normally",
368
+ async () => {
369
+ const lease = await factory();
370
+ assertEqual(await lease.acquire("s1", "a", 1), true, "short lease");
371
+ await sleep(30);
372
+ assertEqual(await lease.acquire("s1", "b", 6e4), true, "taking over after expiry");
373
+ assertEqual(await lease.renew("s1", "a", 6e4), false, "the party that lost the lease cannot renew");
374
+ assertEqual(await lease.renew("s1", "b", 6e4), true, "the new holder renews");
375
+ }
376
+ );
377
+ t.it(
378
+ "expired with no one taking over: the old holder's renew is false, but it can acquire again",
379
+ async () => {
380
+ const lease = await factory();
381
+ await lease.acquire("s1", "a", 1);
382
+ await sleep(30);
383
+ assertEqual(await lease.renew("s1", "a", 6e4), false, "no renew after expiry");
384
+ assert(await lease.acquire("s1", "a", 6e4), "acquiring again after expiry must succeed");
385
+ }
386
+ );
275
387
  });
276
388
  }
277
389
 
@@ -282,6 +394,7 @@ var SCRIPTED_CAPABILITIES = {
282
394
  thinkingReplay: true,
283
395
  parallelTools: true,
284
396
  taskBudget: false,
397
+ deferredTools: false,
285
398
  images: true,
286
399
  contextWindow: 2e5,
287
400
  maxOutputTokens: 8192
@@ -327,7 +440,10 @@ var ScriptedLowering = class {
327
440
  turnAt(turn, input) {
328
441
  if (typeof this.script === "function") return this.script(input, turn);
329
442
  const step = this.script[turn];
330
- if (!step) throw new Error(`\u5267\u672C\u53EA\u6709 ${this.script.length} \u8F6E\uFF0C\u7B2C ${turn + 1} \u8F6E\u6CA1\u6709\u53F0\u8BCD`);
443
+ if (!step)
444
+ throw new Error(
445
+ `the script has only ${this.script.length} turn(s); turn ${turn + 1} has no scripted response`
446
+ );
331
447
  return step;
332
448
  }
333
449
  };
@@ -360,6 +476,7 @@ export {
360
476
  eventLogConformance,
361
477
  makeEvents,
362
478
  memoryStoreConformance,
479
+ runLeaseConformance,
363
480
  say,
364
481
  think
365
482
  };