@snylonue/dsh-context-policy 0.3.0 → 0.3.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/README.md +3 -1
- package/dist/builtin-tokenizers.js +1 -1
- package/dist/builtin-tokenizers.js.map +1 -1
- package/dist/compact.d.ts +56 -0
- package/dist/compact.d.ts.map +1 -0
- package/dist/compact.js +139 -0
- package/dist/compact.js.map +1 -0
- package/dist/core.d.ts +16 -3
- package/dist/core.d.ts.map +1 -1
- package/dist/core.js +34 -13
- package/dist/core.js.map +1 -1
- package/dist/projection.d.ts +266 -33
- package/dist/projection.d.ts.map +1 -1
- package/dist/projection.js +27 -38
- package/dist/projection.js.map +1 -1
- package/dist/token-meter.d.ts +3 -2
- package/dist/token-meter.d.ts.map +1 -1
- package/dist/token-meter.js +5 -4
- package/dist/token-meter.js.map +1 -1
- package/dist/tokenizers.d.ts +2 -0
- package/dist/tokenizers.d.ts.map +1 -1
- package/dist/tokenizers.js +5 -0
- package/dist/tokenizers.js.map +1 -1
- package/dist/types.d.ts +12 -3
- package/dist/types.d.ts.map +1 -1
- package/package.json +17 -17
- package/scripts/check.ts +2 -0
- package/src/builtin-tokenizers.ts +1 -1
- package/src/compact.ts +189 -0
- package/src/core.ts +46 -19
- package/src/projection.ts +41 -45
- package/src/token-meter.ts +13 -6
- package/src/tokenizers.ts +6 -0
- package/src/types.ts +12 -3
- package/tests/compact.test.ts +475 -0
- package/tests/current-host.test.ts +19 -6
- package/tests/policy.test.ts +6 -6
- package/tests/replay.test.ts +255 -0
package/src/types.ts
CHANGED
|
@@ -51,15 +51,24 @@ export interface ResolvedPolicy {
|
|
|
51
51
|
replyPrimingTokens: number;
|
|
52
52
|
includeReasoning: boolean;
|
|
53
53
|
}
|
|
54
|
+
export interface ReplayHeader extends EpochHeader {
|
|
55
|
+
toolsKey?: string;
|
|
56
|
+
toolCounts?: Record<string, number> | null;
|
|
57
|
+
}
|
|
54
58
|
export interface ReplayNode {
|
|
55
59
|
seq: SessionSeq;
|
|
56
|
-
message: Message | null;
|
|
60
|
+
message: Pick<Message, "content"> | null;
|
|
61
|
+
counts?: {
|
|
62
|
+
text: Record<string, number>;
|
|
63
|
+
reasoning: Record<string, number>;
|
|
64
|
+
envelope: boolean;
|
|
65
|
+
};
|
|
57
66
|
}
|
|
58
67
|
export interface ReplayState {
|
|
59
68
|
nodes: readonly ReplayNode[];
|
|
60
|
-
header:
|
|
69
|
+
header: ReplayHeader | null;
|
|
61
70
|
anchor: {
|
|
62
|
-
header:
|
|
71
|
+
header: ReplayHeader;
|
|
63
72
|
nodes: readonly ReplayNode[];
|
|
64
73
|
usage: TokenUsage;
|
|
65
74
|
} | null;
|
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { Context } from "@deepseek-ai/cordis";
|
|
4
|
+
import Registry from "@deepseek-ai/dsh-session-projection";
|
|
5
|
+
import { createUserMessage } from "@deepseek-ai/dsh-llm";
|
|
6
|
+
import type {
|
|
7
|
+
EpochHeader,
|
|
8
|
+
Session,
|
|
9
|
+
SessionEvent,
|
|
10
|
+
} from "@deepseek-ai/dsh-session";
|
|
11
|
+
import { SessionSeq, SessionLogOffset } from "@deepseek-ai/dsh-session";
|
|
12
|
+
import Meter from "../dist/token-meter.js";
|
|
13
|
+
import { fold, initialState, measureState } from "../dist/core.js";
|
|
14
|
+
import {
|
|
15
|
+
compactSchema,
|
|
16
|
+
foldCompact,
|
|
17
|
+
initialCompactState,
|
|
18
|
+
measureCompact,
|
|
19
|
+
} from "../dist/compact.js";
|
|
20
|
+
import {
|
|
21
|
+
createDisplayProjections,
|
|
22
|
+
createReplayProjection,
|
|
23
|
+
} from "../dist/projection.js";
|
|
24
|
+
import { registerTokenizer } from "../dist/tokenizers.js";
|
|
25
|
+
|
|
26
|
+
function event(
|
|
27
|
+
seq: number,
|
|
28
|
+
type: string,
|
|
29
|
+
data: unknown,
|
|
30
|
+
surfaceOp?: unknown,
|
|
31
|
+
): SessionEvent {
|
|
32
|
+
return {
|
|
33
|
+
seq: SessionSeq(seq),
|
|
34
|
+
type,
|
|
35
|
+
data,
|
|
36
|
+
...(surfaceOp === undefined ? {} : { surfaceOp }),
|
|
37
|
+
} as SessionEvent;
|
|
38
|
+
}
|
|
39
|
+
const header = (model = "m"): EpochHeader => ({
|
|
40
|
+
config: { provider: "p", model, maxTokens: 1000 },
|
|
41
|
+
tools: [],
|
|
42
|
+
});
|
|
43
|
+
const user = (seq: number, text: string, op: unknown = "append") =>
|
|
44
|
+
event(
|
|
45
|
+
seq,
|
|
46
|
+
"user/message",
|
|
47
|
+
createUserMessage({
|
|
48
|
+
source: { kind: "user" },
|
|
49
|
+
content: [{ type: "text", text }],
|
|
50
|
+
}),
|
|
51
|
+
op,
|
|
52
|
+
);
|
|
53
|
+
const assistant = (seq: number, inputTokens = 100) =>
|
|
54
|
+
event(
|
|
55
|
+
seq,
|
|
56
|
+
"assistant/message",
|
|
57
|
+
{
|
|
58
|
+
turn: 0,
|
|
59
|
+
step: seq,
|
|
60
|
+
stream: [],
|
|
61
|
+
usage: {
|
|
62
|
+
inputTokens,
|
|
63
|
+
outputTokens: 10,
|
|
64
|
+
cacheReadTokens: inputTokens > 0 ? 3 : 0,
|
|
65
|
+
cacheWriteTokens: inputTokens > 0 ? 2 : 0,
|
|
66
|
+
},
|
|
67
|
+
message: {
|
|
68
|
+
id: "a",
|
|
69
|
+
role: "assistant",
|
|
70
|
+
content: [
|
|
71
|
+
{ type: "text", text: "reply" },
|
|
72
|
+
{ type: "reasoning", text: "hidden reasoning" },
|
|
73
|
+
{ type: "tool-call", name: "tool", id: "call", arguments: "{}" },
|
|
74
|
+
],
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
"append",
|
|
78
|
+
);
|
|
79
|
+
function session(events: SessionEvent[]): Session {
|
|
80
|
+
return {
|
|
81
|
+
seq: events.length,
|
|
82
|
+
header: {},
|
|
83
|
+
inheritedEventCount: 0,
|
|
84
|
+
snapshotEvents: () => events.slice(),
|
|
85
|
+
eventAt: (seq: number) => events[seq],
|
|
86
|
+
} as unknown as Session;
|
|
87
|
+
}
|
|
88
|
+
function stringCharacters(value: unknown): number {
|
|
89
|
+
if (typeof value === "string") return value.length;
|
|
90
|
+
if (!value || typeof value !== "object") return 0;
|
|
91
|
+
return Object.values(value).reduce<number>(
|
|
92
|
+
(sum, child) => sum + stringCharacters(child),
|
|
93
|
+
0,
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
test("all three live and persisted projections stay body-free with 720M logical legacy characters", async (t) => {
|
|
98
|
+
const unregister = registerTokenizer("memory-test", (text) => text.length);
|
|
99
|
+
const ctx = new Context();
|
|
100
|
+
await ctx.plugin(Registry).await();
|
|
101
|
+
const config = Meter.Config({ defaultTokenizer: "memory-test" });
|
|
102
|
+
await ctx.plugin(Meter, config).await();
|
|
103
|
+
try {
|
|
104
|
+
const payload = `private-message-body:${"x".repeat(2_500_000 - 21)}`;
|
|
105
|
+
const events = [
|
|
106
|
+
event(0, "request/header", {
|
|
107
|
+
header: {
|
|
108
|
+
...header(),
|
|
109
|
+
tools: [
|
|
110
|
+
{
|
|
111
|
+
name: "tool",
|
|
112
|
+
description: `private-tool-description:${"y".repeat(100_000)}`,
|
|
113
|
+
parameters: {},
|
|
114
|
+
},
|
|
115
|
+
],
|
|
116
|
+
},
|
|
117
|
+
}),
|
|
118
|
+
];
|
|
119
|
+
for (let i = 0; i < 48; i++) events.push(user(events.length, payload));
|
|
120
|
+
events.push(assistant(events.length));
|
|
121
|
+
const s = session(events);
|
|
122
|
+
const legacy = events.reduce(fold, initialState());
|
|
123
|
+
assert.deepEqual(ctx.tokenMeter.measure(s), measureState(legacy, config));
|
|
124
|
+
const checkpoint = ctx.sessionProjections.checkpoint(s);
|
|
125
|
+
const keys = [
|
|
126
|
+
"contextPolicyMeter",
|
|
127
|
+
"contextPressure",
|
|
128
|
+
"contextBreakdown",
|
|
129
|
+
] as const;
|
|
130
|
+
let newCharacters = 0,
|
|
131
|
+
serializedCharacters = 0;
|
|
132
|
+
for (const key of keys) {
|
|
133
|
+
const state = ctx.sessionProjections.stateOf(s, key);
|
|
134
|
+
const encoded = JSON.stringify(state);
|
|
135
|
+
assert(!encoded.includes("private-message-body"));
|
|
136
|
+
assert(!encoded.includes("private-tool-description"));
|
|
137
|
+
assert(!encoded.includes('"arguments":'));
|
|
138
|
+
assert(!encoded.includes('"content":'));
|
|
139
|
+
assert(!encoded.includes('"hidden reasoning"'));
|
|
140
|
+
assert.deepEqual(
|
|
141
|
+
JSON.parse(JSON.stringify(checkpoint[key]?.val)),
|
|
142
|
+
JSON.parse(encoded),
|
|
143
|
+
);
|
|
144
|
+
newCharacters += stringCharacters(state);
|
|
145
|
+
serializedCharacters += encoded.length;
|
|
146
|
+
}
|
|
147
|
+
assert(stringCharacters(legacy) * 3 >= 720_000_000);
|
|
148
|
+
assert(newCharacters < 5000);
|
|
149
|
+
assert(serializedCharacters < 100_000);
|
|
150
|
+
const roundtrip = JSON.parse(JSON.stringify(checkpoint));
|
|
151
|
+
assert.deepEqual(
|
|
152
|
+
ctx.sessionProjections.viewCheckpoint(roundtrip),
|
|
153
|
+
ctx.sessionProjections.snapshot(s).values,
|
|
154
|
+
);
|
|
155
|
+
// A checkpoint must suffice: no transient text side table or full log needed.
|
|
156
|
+
const restored = ctx.sessionProjections.restore(
|
|
157
|
+
roundtrip,
|
|
158
|
+
[],
|
|
159
|
+
SessionLogOffset(events.length),
|
|
160
|
+
s.header,
|
|
161
|
+
SessionLogOffset(0),
|
|
162
|
+
);
|
|
163
|
+
assert.deepEqual(
|
|
164
|
+
restored.snapshot.values,
|
|
165
|
+
ctx.sessionProjections.snapshot(s).values,
|
|
166
|
+
);
|
|
167
|
+
t.diagnostic(
|
|
168
|
+
JSON.stringify({
|
|
169
|
+
legacyLogicalCharacters: stringCharacters(legacy) * 3,
|
|
170
|
+
retainedStringCharacters: newCharacters,
|
|
171
|
+
serializedCharacters,
|
|
172
|
+
}),
|
|
173
|
+
);
|
|
174
|
+
} finally {
|
|
175
|
+
await ctx.fiber.dispose();
|
|
176
|
+
unregister();
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test("compact counts match full replay through route changes, usage, offload and replacements", () => {
|
|
181
|
+
const unregister = registerTokenizer(
|
|
182
|
+
"compact-characters",
|
|
183
|
+
(text) => text.length,
|
|
184
|
+
);
|
|
185
|
+
const config = Meter.Config({
|
|
186
|
+
defaultTokenizer: "o200k_base",
|
|
187
|
+
providerPolicies: [{ provider: "q", tokenizer: "deepseek_v4" }],
|
|
188
|
+
modelPolicies: [
|
|
189
|
+
{
|
|
190
|
+
model: "other",
|
|
191
|
+
tokenizer: "compact-characters",
|
|
192
|
+
includeReasoning: true,
|
|
193
|
+
tokensPerMessage: 31,
|
|
194
|
+
toolsOverhead: 17,
|
|
195
|
+
replyPrimingTokens: 5,
|
|
196
|
+
},
|
|
197
|
+
],
|
|
198
|
+
});
|
|
199
|
+
const services = {
|
|
200
|
+
llm: {
|
|
201
|
+
imageRequestPricing: (_provider: string, model: string) => ({
|
|
202
|
+
priceImages: (images: readonly { offloaded?: boolean }[]) =>
|
|
203
|
+
images.map((image, i) => ({
|
|
204
|
+
visualTokens: image.offloaded
|
|
205
|
+
? 0
|
|
206
|
+
: (model === "other" ? 100 : 200) + i,
|
|
207
|
+
text: "image placeholder",
|
|
208
|
+
})),
|
|
209
|
+
}),
|
|
210
|
+
fileRequestText: () => "rendered file handle",
|
|
211
|
+
},
|
|
212
|
+
};
|
|
213
|
+
try {
|
|
214
|
+
const events = [
|
|
215
|
+
event(
|
|
216
|
+
0,
|
|
217
|
+
"system/message",
|
|
218
|
+
{ message: { role: "system", content: [] } },
|
|
219
|
+
"append",
|
|
220
|
+
),
|
|
221
|
+
user(1, "你好世界"),
|
|
222
|
+
event(2, "request/header", { header: header() }),
|
|
223
|
+
event(
|
|
224
|
+
3,
|
|
225
|
+
"user/message",
|
|
226
|
+
createUserMessage({
|
|
227
|
+
source: { kind: "user" },
|
|
228
|
+
content: [
|
|
229
|
+
{ type: "image", attachment: { id: "image" } } as never,
|
|
230
|
+
{ type: "image", attachment: { id: "image2" } } as never,
|
|
231
|
+
{ type: "file", attachment: { id: "file" } } as never,
|
|
232
|
+
],
|
|
233
|
+
}),
|
|
234
|
+
"append",
|
|
235
|
+
),
|
|
236
|
+
assistant(4),
|
|
237
|
+
event(5, "image/offload", {
|
|
238
|
+
targets: [{ seq: SessionSeq(3), imageIndexes: [0] }],
|
|
239
|
+
}),
|
|
240
|
+
assistant(6, 0),
|
|
241
|
+
event(7, "assistant/attempt", {
|
|
242
|
+
stream: [
|
|
243
|
+
{ type: "usage", usage: { inputTokens: 500, outputTokens: 3 } },
|
|
244
|
+
],
|
|
245
|
+
turn: 0,
|
|
246
|
+
step: 7,
|
|
247
|
+
}),
|
|
248
|
+
event(8, "llm/retry-started", { turn: 0, step: 7 }),
|
|
249
|
+
event(9, "request/header", {
|
|
250
|
+
header: {
|
|
251
|
+
...header("other"),
|
|
252
|
+
tools: [
|
|
253
|
+
{
|
|
254
|
+
name: "tool",
|
|
255
|
+
description: "schema",
|
|
256
|
+
parameters: { type: "object" },
|
|
257
|
+
},
|
|
258
|
+
],
|
|
259
|
+
},
|
|
260
|
+
}),
|
|
261
|
+
assistant(10),
|
|
262
|
+
user(11, "summary", { startSeq: SessionSeq(1), endSeq: SessionSeq(3) }),
|
|
263
|
+
event(12, "request/header", {
|
|
264
|
+
header: { ...header(), config: { ...header().config, provider: "q" } },
|
|
265
|
+
}),
|
|
266
|
+
event(13, "request/header", { header: header() }),
|
|
267
|
+
];
|
|
268
|
+
for (const pricing of [services, {}]) {
|
|
269
|
+
let full = initialState(),
|
|
270
|
+
compact = initialCompactState();
|
|
271
|
+
const display = createDisplayProjections(config, pricing);
|
|
272
|
+
let pressure = display.pressure.init(),
|
|
273
|
+
breakdown = display.breakdown.init();
|
|
274
|
+
for (const next of events) {
|
|
275
|
+
full = fold(full, next);
|
|
276
|
+
compact = foldCompact(compact, next, config);
|
|
277
|
+
pressure = display.pressure.apply(pressure, next);
|
|
278
|
+
breakdown = display.breakdown.apply(breakdown, next);
|
|
279
|
+
const expected = measureState(full, config, pricing);
|
|
280
|
+
assert.deepEqual(measureCompact(compact, config, pricing), expected);
|
|
281
|
+
assert.equal(
|
|
282
|
+
display.pressure.wire.view(pressure).projectedTokens,
|
|
283
|
+
expected.totalTokens,
|
|
284
|
+
);
|
|
285
|
+
assert.equal(breakdown.surfaceTokens, expected.surfaceTokens);
|
|
286
|
+
compact = compactSchema.parse(JSON.parse(JSON.stringify(compact)));
|
|
287
|
+
pressure = display.pressure.stateSchema.parse(
|
|
288
|
+
JSON.parse(JSON.stringify(pressure)),
|
|
289
|
+
);
|
|
290
|
+
breakdown = display.breakdown.stateSchema.parse(
|
|
291
|
+
JSON.parse(JSON.stringify(breakdown)),
|
|
292
|
+
);
|
|
293
|
+
for (const request of [header(), header("other")])
|
|
294
|
+
assert.deepEqual(
|
|
295
|
+
measureCompact(compact, config, pricing, request),
|
|
296
|
+
measureState(full, config, pricing, request),
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
} finally {
|
|
301
|
+
unregister();
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
test("body-bearing cache versions are rejected and rebuilt without their payloads", async () => {
|
|
306
|
+
const ctx = new Context();
|
|
307
|
+
await ctx.plugin(Registry).await();
|
|
308
|
+
await ctx
|
|
309
|
+
.plugin(Meter, Meter.Config({ defaultTokenizer: "o200k_base" }))
|
|
310
|
+
.await();
|
|
311
|
+
try {
|
|
312
|
+
const events = [
|
|
313
|
+
event(0, "request/header", { header: header() }),
|
|
314
|
+
user(1, "legacy-secret-payload"),
|
|
315
|
+
assistant(2),
|
|
316
|
+
];
|
|
317
|
+
const s = session(events);
|
|
318
|
+
const old = ctx.sessionProjections.checkpoint(s);
|
|
319
|
+
const legacy = events.reduce(fold, initialState());
|
|
320
|
+
for (const key of [
|
|
321
|
+
"contextPressure",
|
|
322
|
+
"contextBreakdown",
|
|
323
|
+
"contextPolicyMeter",
|
|
324
|
+
] as const) {
|
|
325
|
+
const row = old[key];
|
|
326
|
+
assert(row);
|
|
327
|
+
row.ver = key === "contextPolicyMeter" ? 2 : 9;
|
|
328
|
+
row.val = legacy;
|
|
329
|
+
}
|
|
330
|
+
assert.equal(ctx.sessionProjections.restoreFloor(old), 0);
|
|
331
|
+
assert.equal(
|
|
332
|
+
ctx.sessionProjections.viewCheckpoint(old).contextPressure,
|
|
333
|
+
undefined,
|
|
334
|
+
);
|
|
335
|
+
const restored = ctx.sessionProjections.restore(
|
|
336
|
+
old,
|
|
337
|
+
events,
|
|
338
|
+
SessionLogOffset(0),
|
|
339
|
+
s.header,
|
|
340
|
+
SessionLogOffset(0),
|
|
341
|
+
);
|
|
342
|
+
assert.deepEqual(
|
|
343
|
+
restored.snapshot.values,
|
|
344
|
+
ctx.sessionProjections.snapshot(s).values,
|
|
345
|
+
);
|
|
346
|
+
assert(
|
|
347
|
+
!JSON.stringify(restored.checkpoint).includes("legacy-secret-payload"),
|
|
348
|
+
);
|
|
349
|
+
} finally {
|
|
350
|
+
await ctx.fiber.dispose();
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
test("counting and restoring text-only summaries never retokenizes retained bodies", () => {
|
|
355
|
+
let calls = 0;
|
|
356
|
+
const unregister = registerTokenizer("count-once", (text) => {
|
|
357
|
+
calls++;
|
|
358
|
+
return text.length;
|
|
359
|
+
});
|
|
360
|
+
try {
|
|
361
|
+
const config = Meter.Config({ defaultTokenizer: "count-once" });
|
|
362
|
+
let state = initialCompactState();
|
|
363
|
+
state = foldCompact(
|
|
364
|
+
state,
|
|
365
|
+
event(0, "request/header", { header: header() }),
|
|
366
|
+
config,
|
|
367
|
+
);
|
|
368
|
+
state = foldCompact(state, user(1, "x".repeat(2_000_001)), config);
|
|
369
|
+
assert.equal(calls, 1);
|
|
370
|
+
for (let i = 0; i < 10; i++) {
|
|
371
|
+
state = compactSchema.parse(JSON.parse(JSON.stringify(state)));
|
|
372
|
+
assert.equal(measureCompact(state, config).surfaceTokens, 2_000_004);
|
|
373
|
+
measureCompact(state, config, {}, header("other"));
|
|
374
|
+
}
|
|
375
|
+
assert.equal(calls, 1);
|
|
376
|
+
} finally {
|
|
377
|
+
unregister();
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
test("compact calibration summaries survive adapter availability and pricing changes", () => {
|
|
382
|
+
const config = Meter.Config({ defaultTokenizer: "o200k_base" });
|
|
383
|
+
const available = {
|
|
384
|
+
llm: {
|
|
385
|
+
imageRequestPricing: () => ({
|
|
386
|
+
priceImages: (images: readonly { offloaded?: boolean }[]) =>
|
|
387
|
+
images.map((image, i) => ({
|
|
388
|
+
visualTokens: image.offloaded ? 0 : 100 + i,
|
|
389
|
+
text: "image handle",
|
|
390
|
+
})),
|
|
391
|
+
}),
|
|
392
|
+
fileRequestText: () => "saved file handle",
|
|
393
|
+
},
|
|
394
|
+
};
|
|
395
|
+
const events = [
|
|
396
|
+
event(0, "request/header", { header: header() }),
|
|
397
|
+
event(
|
|
398
|
+
1,
|
|
399
|
+
"user/message",
|
|
400
|
+
createUserMessage({
|
|
401
|
+
source: { kind: "user" },
|
|
402
|
+
content: [
|
|
403
|
+
{ type: "image", attachment: { id: "i" } } as never,
|
|
404
|
+
{ type: "file", attachment: { id: "f" } } as never,
|
|
405
|
+
],
|
|
406
|
+
}),
|
|
407
|
+
"append",
|
|
408
|
+
),
|
|
409
|
+
user(2, "text-only body must not enter the anchor"),
|
|
410
|
+
assistant(3),
|
|
411
|
+
event(4, "image/offload", {
|
|
412
|
+
targets: [{ seq: SessionSeq(1), imageIndexes: [0] }],
|
|
413
|
+
}),
|
|
414
|
+
];
|
|
415
|
+
const full = events.reduce(fold, initialState());
|
|
416
|
+
let state = events.reduce(
|
|
417
|
+
(s, e) => foldCompact(s, e, config),
|
|
418
|
+
initialCompactState(),
|
|
419
|
+
);
|
|
420
|
+
state = compactSchema.parse(JSON.parse(JSON.stringify(state)));
|
|
421
|
+
assert.equal(state.anchor?.nodes.filter((node) => node.message).length, 1);
|
|
422
|
+
assert.deepEqual(
|
|
423
|
+
state.anchor?.nodes.filter((node) => node.message).map((node) => node.seq),
|
|
424
|
+
[1],
|
|
425
|
+
);
|
|
426
|
+
for (const services of [
|
|
427
|
+
available,
|
|
428
|
+
{},
|
|
429
|
+
{
|
|
430
|
+
llm: {
|
|
431
|
+
...available.llm,
|
|
432
|
+
fileRequestText: () => "changed file path and name",
|
|
433
|
+
},
|
|
434
|
+
},
|
|
435
|
+
]) {
|
|
436
|
+
assert.deepEqual(
|
|
437
|
+
measureCompact(state, config, services),
|
|
438
|
+
measureState(full, config, services),
|
|
439
|
+
);
|
|
440
|
+
}
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
test("display checkpoints remove an unknown context window without retaining an old value", () => {
|
|
444
|
+
const display = createDisplayProjections(Meter.Config({}), {});
|
|
445
|
+
let state = display.pressure.init();
|
|
446
|
+
state = display.pressure.apply(
|
|
447
|
+
state,
|
|
448
|
+
event(0, "request/context", { contextWindow: 128000 }),
|
|
449
|
+
);
|
|
450
|
+
assert.equal(display.pressure.wire.view(state).contextWindow, 128000);
|
|
451
|
+
state = display.pressure.apply(state, event(1, "request/context", {}));
|
|
452
|
+
assert.equal("contextWindow" in state, false);
|
|
453
|
+
assert.equal("contextWindow" in display.pressure.wire.view(state), false);
|
|
454
|
+
assert.deepEqual(
|
|
455
|
+
display.pressure.stateSchema.parse(JSON.parse(JSON.stringify(state))),
|
|
456
|
+
state,
|
|
457
|
+
);
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
test("checkpoint versions change with every counting policy", () => {
|
|
461
|
+
const config = Meter.Config({});
|
|
462
|
+
const base = createReplayProjection(config);
|
|
463
|
+
assert.equal(
|
|
464
|
+
base.stateVersion,
|
|
465
|
+
createReplayProjection(structuredClone(config)).stateVersion,
|
|
466
|
+
);
|
|
467
|
+
for (const input of [
|
|
468
|
+
{ defaultTokenizer: "o200k_base" },
|
|
469
|
+
{ providerPolicies: [{ provider: "p", tokensPerMessage: 7 }] },
|
|
470
|
+
{ modelPolicies: [{ model: "m", includeReasoning: true }] },
|
|
471
|
+
]) {
|
|
472
|
+
const changed = createReplayProjection(Meter.Config(input));
|
|
473
|
+
assert.notEqual(changed.stateVersion, base.stateVersion);
|
|
474
|
+
}
|
|
475
|
+
});
|
|
@@ -2,14 +2,27 @@ import assert from "node:assert/strict";
|
|
|
2
2
|
import { readFileSync } from "node:fs";
|
|
3
3
|
import { test } from "node:test";
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
const supportedHosts = "0.2.1-rc.2 || 0.2.1-rc.3";
|
|
6
|
+
const testedHost = "0.2.1-rc.3";
|
|
7
|
+
|
|
8
|
+
test("declares only the verified DSH rc.2 and rc.3 hosts", () => {
|
|
6
9
|
const manifest = JSON.parse(
|
|
7
10
|
readFileSync(new URL("../package.json", import.meta.url), "utf8"),
|
|
8
11
|
);
|
|
9
|
-
assert.equal(manifest.engines.dsh,
|
|
12
|
+
assert.equal(manifest.engines.dsh, supportedHosts);
|
|
10
13
|
assert.equal(manifest.peerDependencies["@deepseek-ai/cordis"], "~4.0.5");
|
|
11
|
-
for (const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
for (const [name, version] of Object.entries(manifest.peerDependencies))
|
|
15
|
+
if (name.startsWith("@deepseek-ai/dsh-"))
|
|
16
|
+
assert.equal(version, supportedHosts, name);
|
|
17
|
+
for (const [name, version] of Object.entries(manifest.devDependencies)) {
|
|
18
|
+
if (!name.startsWith("@deepseek-ai/dsh-")) continue;
|
|
19
|
+
assert.equal(version, testedHost, name);
|
|
20
|
+
const installed = JSON.parse(
|
|
21
|
+
readFileSync(
|
|
22
|
+
new URL(`../node_modules/${name}/package.json`, import.meta.url),
|
|
23
|
+
"utf8",
|
|
24
|
+
),
|
|
25
|
+
);
|
|
26
|
+
assert.equal(installed.version, testedHost, `${name} installed version`);
|
|
27
|
+
}
|
|
15
28
|
});
|
package/tests/policy.test.ts
CHANGED
|
@@ -464,14 +464,14 @@ test("actual Cordis registry replays nonempty logs, validates all UI views and u
|
|
|
464
464
|
updated.contextBreakdown.messageTokens >
|
|
465
465
|
values.contextBreakdown.messageTokens,
|
|
466
466
|
);
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
);
|
|
467
|
+
// Counts and numeric anchors are policy-specific; changed configuration
|
|
468
|
+
// invalidates all counting checkpoints rather than reusing stale prices.
|
|
469
|
+
assert.equal(ctx.sessionProjections.viewCheckpoint(checkpoint).contextPressure, undefined);
|
|
470
|
+
assert.equal(ctx.sessionProjections.restoreFloor(checkpoint), 0);
|
|
471
471
|
const restored = ctx.sessionProjections.restore(
|
|
472
472
|
checkpoint,
|
|
473
|
-
events
|
|
474
|
-
SessionLogOffset(
|
|
473
|
+
events,
|
|
474
|
+
SessionLogOffset(0),
|
|
475
475
|
s.header,
|
|
476
476
|
SessionLogOffset(0),
|
|
477
477
|
);
|