@snylonue/dsh-context-policy 0.1.1 → 0.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 -10
- package/dist/compaction.d.ts +6 -2
- package/dist/compaction.d.ts.map +1 -1
- package/dist/compaction.js +19 -7
- package/dist/compaction.js.map +1 -1
- package/dist/core.d.ts.map +1 -1
- package/dist/core.js +2 -3
- package/dist/core.js.map +1 -1
- package/dist/projection.d.ts +17 -5
- package/dist/projection.d.ts.map +1 -1
- package/dist/projection.js +23 -25
- package/dist/projection.js.map +1 -1
- package/dist/token-meter.d.ts.map +1 -1
- package/dist/token-meter.js +15 -7
- package/dist/token-meter.js.map +1 -1
- package/dist/tokenizers.d.ts.map +1 -1
- package/dist/tokenizers.js +7 -7
- package/dist/tokenizers.js.map +1 -1
- package/dist/types.d.ts +10 -6
- package/dist/types.d.ts.map +1 -1
- package/package.json +21 -11
- package/src/compaction.ts +25 -8
- package/src/core.ts +2 -3
- package/src/projection.ts +38 -32
- package/src/token-meter.ts +18 -7
- package/src/tokenizers.ts +12 -7
- package/src/types.ts +10 -6
- package/tests/policy.test.ts +307 -8
package/tests/policy.test.ts
CHANGED
|
@@ -24,6 +24,7 @@ import {
|
|
|
24
24
|
registerTokenizer,
|
|
25
25
|
} from "../dist/tokenizers.js";
|
|
26
26
|
import { initialState, fold, measureState } from "../dist/core.js";
|
|
27
|
+
import { createDisplayProjections, replayProjection } from "../dist/projection.js";
|
|
27
28
|
|
|
28
29
|
const meterConfig = { defaultTokenizer: "o200k_base", modelPolicies: [] };
|
|
29
30
|
const header = (provider = "p", model = "gpt-new", maxTokens = 128000) =>
|
|
@@ -157,9 +158,9 @@ test("DeepSeek V4 model policies reprice measurement and preserve the default ro
|
|
|
157
158
|
test("DeepSeek V4 is the global default; provider defaults price every model on that provider", () => {
|
|
158
159
|
const defaults = Meter.Config({});
|
|
159
160
|
assert.equal(defaults.defaultTokenizer, "deepseek_v4");
|
|
160
|
-
assert.deepEqual(defaults.
|
|
161
|
+
assert.deepEqual(defaults.providerPolicies, []);
|
|
161
162
|
assert.equal(resolvePolicy(defaults, null).tokenizer, "deepseek_v4");
|
|
162
|
-
const cfg = Meter.Config({
|
|
163
|
+
const cfg = Meter.Config({ providerPolicies: [{ provider: "openai", tokenizer: "o200k_base" }] });
|
|
163
164
|
for (const [provider, model, tokenizer, tokens] of [
|
|
164
165
|
["openai", "new-model", "o200k_base", 13],
|
|
165
166
|
["openai", "other-model", "o200k_base", 13],
|
|
@@ -179,7 +180,10 @@ test("DeepSeek V4 is the global default; provider defaults price every model on
|
|
|
179
180
|
|
|
180
181
|
test("matched model policy overrides the provider tokenizer, or inherits it when omitted", () => {
|
|
181
182
|
const cfg = Meter.Config({
|
|
182
|
-
|
|
183
|
+
providerPolicies: [
|
|
184
|
+
{ provider: "openai", tokenizer: "o200k_base" },
|
|
185
|
+
{ provider: "another", tokenizer: "o200k_base" },
|
|
186
|
+
],
|
|
183
187
|
modelPolicies: [
|
|
184
188
|
{ model: "shared", tokenizer: "deepseek_v4" },
|
|
185
189
|
{
|
|
@@ -293,14 +297,16 @@ test("failure with zero usage preserves occupancy; retries still accumulate bill
|
|
|
293
297
|
}
|
|
294
298
|
});
|
|
295
299
|
|
|
296
|
-
test("replacement reprices removed content
|
|
300
|
+
test("replacement reprices removed content and uses model counts for durable shadow prices", () => {
|
|
297
301
|
let state = [
|
|
298
302
|
event(0, "request/header", { header: header() }),
|
|
299
303
|
user(1, "long ".repeat(600)),
|
|
300
304
|
assistant(2, { inputTokens: 2000, outputTokens: 2 }),
|
|
301
305
|
].reduce(fold, initialState());
|
|
302
306
|
const before = measureState(state, meterConfig);
|
|
303
|
-
const
|
|
307
|
+
const originalMessage = state.nodes[0].message;
|
|
308
|
+
assert(state.nodes.every((node) => !("heuristicTokens" in node)));
|
|
309
|
+
assert(before.nodes.every((node) => node.heuristicTokens === node.tokens));
|
|
304
310
|
state = fold(
|
|
305
311
|
state,
|
|
306
312
|
user(3, "summary", { startSeq: SessionSeq(1), endSeq: SessionSeq(1) }),
|
|
@@ -313,8 +319,9 @@ test("replacement reprices removed content while keeping the fixed durable shado
|
|
|
313
319
|
[3, 2],
|
|
314
320
|
);
|
|
315
321
|
assert(state.anchor);
|
|
316
|
-
assert.equal(state.anchor.nodes[0].
|
|
317
|
-
assert
|
|
322
|
+
assert.equal(state.anchor.nodes[0].message, originalMessage);
|
|
323
|
+
assert(after.nodes.every((node) => node.heuristicTokens === node.tokens));
|
|
324
|
+
assert(state.nodes.every((node) => !("heuristicTokens" in node)));
|
|
318
325
|
});
|
|
319
326
|
|
|
320
327
|
test("header/model changes reject old usage anchor and reprice using mapped tokenizer", () => {
|
|
@@ -421,7 +428,11 @@ test("actual Cordis registry replays nonempty logs, validates all UI views and u
|
|
|
421
428
|
assert(ctx.sessionProjections.stateOf(s, "contextPolicyMeter"));
|
|
422
429
|
assert.equal(
|
|
423
430
|
ctx.sessionProjections.stateOf(s, "contextPressure")?.surfaceTokens,
|
|
424
|
-
actual.
|
|
431
|
+
actual.surfaceTokens,
|
|
432
|
+
);
|
|
433
|
+
assert.deepEqual(
|
|
434
|
+
ctx.sessionProjections.stateOf(s, "contextBreakdown")?.breakdown,
|
|
435
|
+
values.contextBreakdown,
|
|
425
436
|
);
|
|
426
437
|
assert.equal(values.tokenUsage?.uncachedInputTokens, 100);
|
|
427
438
|
const checkpoint = ctx.sessionProjections.checkpoint(s);
|
|
@@ -470,6 +481,294 @@ test("actual Cordis registry replays nonempty logs, validates all UI views and u
|
|
|
470
481
|
}
|
|
471
482
|
});
|
|
472
483
|
|
|
484
|
+
test("display state and serialized checkpoints use model prices across route and surface changes", () => {
|
|
485
|
+
const cfg = Meter.Config({
|
|
486
|
+
defaultTokenizer: "o200k_base",
|
|
487
|
+
modelPolicies: [
|
|
488
|
+
{ provider: "p", model: "gpt-new", tokensPerMessage: 31, toolsOverhead: 17 },
|
|
489
|
+
{ provider: "p", model: "other", tokenizer: "deepseek_v4", tokensPerMessage: 43 },
|
|
490
|
+
],
|
|
491
|
+
});
|
|
492
|
+
const services = {
|
|
493
|
+
llm: {
|
|
494
|
+
imageRequestPricing: () => ({
|
|
495
|
+
priceImages: (images: readonly { offloaded?: boolean }[]) =>
|
|
496
|
+
images.map((image) => ({ visualTokens: image.offloaded ? 0 : 123, text: "image" })),
|
|
497
|
+
}),
|
|
498
|
+
fileRequestText: () => "rendered file text",
|
|
499
|
+
},
|
|
500
|
+
};
|
|
501
|
+
const display = createDisplayProjections(cfg, services);
|
|
502
|
+
let pressure = display.pressure.init();
|
|
503
|
+
let breakdown = display.breakdown.init();
|
|
504
|
+
let replay = initialState();
|
|
505
|
+
const withTools = { ...header(), tools: [{ name: "tool", description: "description", parameters: { type: "object" } }] };
|
|
506
|
+
const events = [
|
|
507
|
+
event(0, "system/message", { message: { role: "system", content: [{ type: "text", text: "system instructions" }] } }, "append"),
|
|
508
|
+
event(1, "user/message", createUserMessage({ source: { kind: "user" }, content: [
|
|
509
|
+
{ type: "text", text: "你好世界" },
|
|
510
|
+
{ type: "image", image: "image" } as never,
|
|
511
|
+
{ type: "file", attachment: { id: "file" } } as never,
|
|
512
|
+
] }), "append"),
|
|
513
|
+
event(2, "request/header", { header: withTools }),
|
|
514
|
+
event(3, "request/context", { contextWindow: 272000 }),
|
|
515
|
+
assistant(4, { inputTokens: 300, outputTokens: 2 }),
|
|
516
|
+
event(5, "request/header", { header: { ...withTools, config: header("p", "other").config } }),
|
|
517
|
+
event(6, "image/offload", { targets: [{ seq: SessionSeq(1), imageIndexes: [0] }] }),
|
|
518
|
+
user(7, "replacement", { startSeq: SessionSeq(1), endSeq: SessionSeq(1) }),
|
|
519
|
+
event(8, "system/message", { message: { role: "system", content: [] } }, { startSeq: SessionSeq(0), endSeq: SessionSeq(0) }),
|
|
520
|
+
];
|
|
521
|
+
for (const next of events) {
|
|
522
|
+
replay = fold(replay, next);
|
|
523
|
+
pressure = display.pressure.apply(pressure, next);
|
|
524
|
+
breakdown = display.breakdown.apply(breakdown, next);
|
|
525
|
+
const measurement = measureState(replay, cfg, services);
|
|
526
|
+
for (const [definition, state] of [[display.pressure, pressure], [display.breakdown, breakdown]] as const) {
|
|
527
|
+
assert.equal(state.surfaceTokens, measurement.surfaceTokens);
|
|
528
|
+
assert.deepEqual(state.nodes.map((node) => node.heuristicTokens), measurement.nodes.map((node) => node.tokens));
|
|
529
|
+
assert.deepEqual(state.breakdown, display.breakdown.wire.view(breakdown));
|
|
530
|
+
assert.equal(state.breakdown.toolsTokens, measurement.toolsTokens);
|
|
531
|
+
const restored = definition.stateSchema.parse(JSON.parse(JSON.stringify(state)));
|
|
532
|
+
assert.deepEqual(restored, state);
|
|
533
|
+
assert.deepEqual(definition.wire.view(restored), definition.wire.view(state));
|
|
534
|
+
}
|
|
535
|
+
assert.equal(display.pressure.wire.view(pressure).projectedTokens, measurement.totalTokens);
|
|
536
|
+
assert(replay.nodes.every((node) => !("heuristicTokens" in node)));
|
|
537
|
+
}
|
|
538
|
+
assert.equal(breakdown.breakdown.systemTokens, 0);
|
|
539
|
+
assert(display.pressure.stateVersion > 7);
|
|
540
|
+
assert(replayProjection.stateVersion > 1);
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
test("display projections retain ordered system prices across routes and replacements", () => {
|
|
544
|
+
const cfg = Meter.Config({
|
|
545
|
+
defaultTokenizer: "o200k_base",
|
|
546
|
+
modelPolicies: [{ provider: "p", model: "other", tokensPerMessage: 37 }],
|
|
547
|
+
});
|
|
548
|
+
const display = createDisplayProjections(cfg, {});
|
|
549
|
+
let pressure = display.pressure.init();
|
|
550
|
+
let breakdown = display.breakdown.init();
|
|
551
|
+
let replay = initialState();
|
|
552
|
+
assert.deepEqual(breakdown.systems, []);
|
|
553
|
+
const events = [
|
|
554
|
+
event(
|
|
555
|
+
0,
|
|
556
|
+
"system/message",
|
|
557
|
+
{
|
|
558
|
+
message: {
|
|
559
|
+
role: "system",
|
|
560
|
+
content: [{ type: "text", text: "first system" }],
|
|
561
|
+
},
|
|
562
|
+
},
|
|
563
|
+
"append",
|
|
564
|
+
),
|
|
565
|
+
event(
|
|
566
|
+
1,
|
|
567
|
+
"system/message",
|
|
568
|
+
{
|
|
569
|
+
message: {
|
|
570
|
+
role: "system",
|
|
571
|
+
content: [{ type: "text", text: "second system" }],
|
|
572
|
+
},
|
|
573
|
+
},
|
|
574
|
+
"append",
|
|
575
|
+
),
|
|
576
|
+
event(
|
|
577
|
+
2,
|
|
578
|
+
"system/message",
|
|
579
|
+
{ message: { role: "system", content: [] } },
|
|
580
|
+
"append",
|
|
581
|
+
),
|
|
582
|
+
event(3, "request/header", { header: header() }),
|
|
583
|
+
event(4, "request/header", { header: header("p", "other") }),
|
|
584
|
+
event(
|
|
585
|
+
5,
|
|
586
|
+
"system/message",
|
|
587
|
+
{
|
|
588
|
+
message: {
|
|
589
|
+
role: "system",
|
|
590
|
+
content: [{ type: "text", text: "rewritten first" }],
|
|
591
|
+
},
|
|
592
|
+
},
|
|
593
|
+
{ startSeq: SessionSeq(0), endSeq: SessionSeq(0) },
|
|
594
|
+
),
|
|
595
|
+
user(6, "tail"),
|
|
596
|
+
user(7, "summary", { startSeq: SessionSeq(1), endSeq: SessionSeq(6) }),
|
|
597
|
+
event(
|
|
598
|
+
8,
|
|
599
|
+
"system/message",
|
|
600
|
+
{ message: { role: "system", content: [] } },
|
|
601
|
+
{ startSeq: SessionSeq(5), endSeq: SessionSeq(5) },
|
|
602
|
+
),
|
|
603
|
+
];
|
|
604
|
+
const expectedSeqs = [
|
|
605
|
+
[0],
|
|
606
|
+
[0, 1],
|
|
607
|
+
[0, 1, 2],
|
|
608
|
+
[0, 1, 2],
|
|
609
|
+
[0, 1, 2],
|
|
610
|
+
[5, 1, 2],
|
|
611
|
+
[5, 1, 2],
|
|
612
|
+
[5],
|
|
613
|
+
[8],
|
|
614
|
+
];
|
|
615
|
+
for (const next of events) {
|
|
616
|
+
replay = fold(replay, next);
|
|
617
|
+
pressure = display.pressure.apply(pressure, next);
|
|
618
|
+
breakdown = display.breakdown.apply(breakdown, next);
|
|
619
|
+
const measurement = measureState(replay, cfg);
|
|
620
|
+
const systems = expectedSeqs[next.seq].map((seq) => {
|
|
621
|
+
const node = measurement.nodes.find((node) => node.seq === seq);
|
|
622
|
+
assert(node);
|
|
623
|
+
return { seq: SessionSeq(seq), tokens: node.tokens };
|
|
624
|
+
});
|
|
625
|
+
for (const [definition, state] of [
|
|
626
|
+
[display.pressure, pressure],
|
|
627
|
+
[display.breakdown, breakdown],
|
|
628
|
+
] as const) {
|
|
629
|
+
assert.deepEqual(state.systems, systems);
|
|
630
|
+
assert.equal(
|
|
631
|
+
state.breakdown.systemTokens,
|
|
632
|
+
systems.findLast((node) => node.tokens > 0)?.tokens ?? 0,
|
|
633
|
+
);
|
|
634
|
+
assert.equal(
|
|
635
|
+
state.breakdown.messageTokens + state.breakdown.systemTokens,
|
|
636
|
+
measurement.surfaceTokens,
|
|
637
|
+
);
|
|
638
|
+
assert.deepEqual(
|
|
639
|
+
definition.stateSchema.parse(JSON.parse(JSON.stringify(state))),
|
|
640
|
+
state,
|
|
641
|
+
);
|
|
642
|
+
const { systems: _systems, ...legacy } = state;
|
|
643
|
+
assert.equal(definition.stateSchema.safeParse(legacy).success, false);
|
|
644
|
+
}
|
|
645
|
+
if (next.seq === 5)
|
|
646
|
+
assert.deepEqual(
|
|
647
|
+
breakdown.systems.map((node) => node.seq),
|
|
648
|
+
[5, 1, 2],
|
|
649
|
+
);
|
|
650
|
+
if (next.seq === 7)
|
|
651
|
+
assert.deepEqual(
|
|
652
|
+
breakdown.systems.map((node) => node.seq),
|
|
653
|
+
[5],
|
|
654
|
+
);
|
|
655
|
+
}
|
|
656
|
+
assert.deepEqual(breakdown.systems, [{ seq: SessionSeq(8), tokens: 0 }]);
|
|
657
|
+
assert.equal(display.breakdown.wire.view(breakdown).systemTokens, 0);
|
|
658
|
+
assert(display.breakdown.stateVersion > 8);
|
|
659
|
+
});
|
|
660
|
+
|
|
661
|
+
test("pre-systems display checkpoints rebuild and continue replay with current system prices", async () => {
|
|
662
|
+
const ctx = new Context();
|
|
663
|
+
await ctx.plugin(Registry).await();
|
|
664
|
+
await ctx
|
|
665
|
+
.plugin(Meter, Meter.Config({ defaultTokenizer: "o200k_base" }))
|
|
666
|
+
.await();
|
|
667
|
+
try {
|
|
668
|
+
const events = [
|
|
669
|
+
event(0, "request/header", { header: header() }),
|
|
670
|
+
event(
|
|
671
|
+
1,
|
|
672
|
+
"system/message",
|
|
673
|
+
{
|
|
674
|
+
message: {
|
|
675
|
+
role: "system",
|
|
676
|
+
content: [{ type: "text", text: "instructions" }],
|
|
677
|
+
},
|
|
678
|
+
},
|
|
679
|
+
"append",
|
|
680
|
+
),
|
|
681
|
+
user(2, "hello"),
|
|
682
|
+
];
|
|
683
|
+
const s = session(events);
|
|
684
|
+
const checkpoint = structuredClone(ctx.sessionProjections.checkpoint(s));
|
|
685
|
+
for (const key of ["contextPressure", "contextBreakdown"] as const) {
|
|
686
|
+
const row = checkpoint[key];
|
|
687
|
+
assert(row);
|
|
688
|
+
row.ver = 8;
|
|
689
|
+
assert(row.val !== null && typeof row.val === "object");
|
|
690
|
+
Reflect.deleteProperty(row.val, "systems");
|
|
691
|
+
}
|
|
692
|
+
const cached = ctx.sessionProjections.viewCheckpoint(checkpoint);
|
|
693
|
+
assert.equal(cached.contextPressure, undefined);
|
|
694
|
+
assert.equal(cached.contextBreakdown, undefined);
|
|
695
|
+
assert.equal(ctx.sessionProjections.restoreFloor(checkpoint), 0);
|
|
696
|
+
const rebuilt = ctx.sessionProjections.restore(
|
|
697
|
+
checkpoint,
|
|
698
|
+
events,
|
|
699
|
+
SessionLogOffset(0),
|
|
700
|
+
s.header,
|
|
701
|
+
SessionLogOffset(0),
|
|
702
|
+
);
|
|
703
|
+
assert.deepEqual(
|
|
704
|
+
rebuilt.snapshot.values,
|
|
705
|
+
ctx.sessionProjections.snapshot(s).values,
|
|
706
|
+
);
|
|
707
|
+
const expected = ctx.sessionProjections.stateOf(s, "contextBreakdown");
|
|
708
|
+
assert(expected);
|
|
709
|
+
assert.deepEqual(rebuilt.checkpoint.contextBreakdown?.val, expected);
|
|
710
|
+
const tail = [
|
|
711
|
+
event(
|
|
712
|
+
3,
|
|
713
|
+
"system/message",
|
|
714
|
+
{
|
|
715
|
+
message: {
|
|
716
|
+
role: "system",
|
|
717
|
+
content: [{ type: "text", text: "new instructions" }],
|
|
718
|
+
},
|
|
719
|
+
},
|
|
720
|
+
{ startSeq: SessionSeq(1), endSeq: SessionSeq(1) },
|
|
721
|
+
),
|
|
722
|
+
];
|
|
723
|
+
const continued = ctx.sessionProjections.restore(
|
|
724
|
+
rebuilt.checkpoint,
|
|
725
|
+
tail,
|
|
726
|
+
SessionLogOffset(3),
|
|
727
|
+
s.header,
|
|
728
|
+
SessionLogOffset(0),
|
|
729
|
+
);
|
|
730
|
+
const complete = session([...events, ...tail]);
|
|
731
|
+
assert.deepEqual(
|
|
732
|
+
continued.snapshot.values,
|
|
733
|
+
ctx.sessionProjections.snapshot(complete).values,
|
|
734
|
+
);
|
|
735
|
+
assert.deepEqual(
|
|
736
|
+
continued.checkpoint.contextBreakdown?.val,
|
|
737
|
+
ctx.sessionProjections.stateOf(complete, "contextBreakdown"),
|
|
738
|
+
);
|
|
739
|
+
} finally {
|
|
740
|
+
await ctx.fiber.dispose();
|
|
741
|
+
}
|
|
742
|
+
});
|
|
743
|
+
|
|
744
|
+
test("old heuristic checkpoints are invalidated and rebuilt from the log", async () => {
|
|
745
|
+
const ctx = new Context();
|
|
746
|
+
await ctx.plugin(Registry).await();
|
|
747
|
+
await ctx.plugin(Meter, Meter.Config({ defaultTokenizer: "o200k_base" })).await();
|
|
748
|
+
try {
|
|
749
|
+
const events = [event(0, "request/header", { header: header() }), user(1, "你好世界")];
|
|
750
|
+
const s = session(events);
|
|
751
|
+
const checkpoint = structuredClone(ctx.sessionProjections.checkpoint(s));
|
|
752
|
+
for (const key of ["contextPressure", "contextBreakdown", "contextPolicyMeter"] as const) {
|
|
753
|
+
const row = checkpoint[key];
|
|
754
|
+
assert(row);
|
|
755
|
+
row.ver = key === "contextPolicyMeter" ? 1 : 7;
|
|
756
|
+
Object.assign(row.val as object, { surfaceTokens: 99999, breakdown: { systemTokens: 99999, toolsTokens: 99999, messageTokens: 99999 } });
|
|
757
|
+
}
|
|
758
|
+
const cached = ctx.sessionProjections.viewCheckpoint(checkpoint);
|
|
759
|
+
assert.equal(cached.contextPressure, undefined);
|
|
760
|
+
assert.equal(cached.contextBreakdown, undefined);
|
|
761
|
+
assert.equal(ctx.sessionProjections.restoreFloor(checkpoint), 0);
|
|
762
|
+
const restored = ctx.sessionProjections.restore(checkpoint, events, SessionLogOffset(0), s.header, SessionLogOffset(0));
|
|
763
|
+
assert.deepEqual(restored.snapshot.values, ctx.sessionProjections.snapshot(s).values);
|
|
764
|
+
const state = restored.checkpoint.contextPressure?.val as { surfaceTokens: number; breakdown: unknown };
|
|
765
|
+
assert.equal(state.surfaceTokens, ctx.tokenMeter.measure(s).surfaceTokens);
|
|
766
|
+
assert.deepEqual(state.breakdown, restored.snapshot.values.contextBreakdown);
|
|
767
|
+
} finally {
|
|
768
|
+
await ctx.fiber.dispose();
|
|
769
|
+
}
|
|
770
|
+
});
|
|
771
|
+
|
|
473
772
|
test("default 80% boundary triggers, independent of request output limits; pruner can avert compaction", async () => {
|
|
474
773
|
const ctx = new Context();
|
|
475
774
|
await ctx.plugin(Registry).await();
|