@snylonue/dsh-context-policy 0.2.0 → 0.3.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.
- package/README.md +2 -2
- package/dist/core.d.ts +3 -3
- package/dist/core.d.ts.map +1 -1
- package/dist/core.js +3 -4
- package/dist/core.js.map +1 -1
- package/dist/projection.d.ts +21 -9
- package/dist/projection.d.ts.map +1 -1
- package/dist/projection.js +24 -26
- package/dist/projection.js.map +1 -1
- package/dist/token-meter.js +1 -1
- package/dist/token-meter.js.map +1 -1
- package/dist/types.d.ts +0 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +102 -93
- package/scripts/check.ts +2 -0
- package/src/core.ts +10 -10
- package/src/projection.ts +46 -38
- package/src/token-meter.ts +1 -1
- package/src/types.ts +0 -1
- package/tests/current-host.test.ts +15 -0
- package/tests/policy.test.ts +301 -5
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@ Percentage-based automatic compaction and model-configurable token accounting fo
|
|
|
5
5
|
## Requirements
|
|
6
6
|
|
|
7
7
|
- Node.js 24 or later.
|
|
8
|
-
- DSH 0.2.
|
|
8
|
+
- DSH 0.2.1-rc.2 only; older-host support is removed.
|
|
9
9
|
|
|
10
10
|
## Installation
|
|
11
11
|
|
|
@@ -201,7 +201,7 @@ Compared with DSH 0.2.0-rc.2's official `dsh-token-meter` and `dsh-compaction-ba
|
|
|
201
201
|
|
|
202
202
|
The compaction service extends `BasicCompactionEngine` and replaces automatic pressure range selection. Summarization, durable transactions, manual commands, and overflow listeners use the upstream implementation.
|
|
203
203
|
|
|
204
|
-
The meter uses the public Cordis service lifecycle and projection registration API. Its billing fold is maintained locally because the upstream definition is not exported at runtime.
|
|
204
|
+
The meter uses the public Cordis service lifecycle and projection registration API. Its billing fold is maintained locally because the upstream definition is not exported at runtime. Replay state does not retain fixed-density heuristic prices. Projection state, wire composition, and durable compaction shadow prices all use model-configured token counts. The current host still requires the upstream `heuristicTokens` field; it carries the same model count as `tokens`, not an older-host estimator. This does not change independent history estimators owned by other plugins.
|
|
205
205
|
|
|
206
206
|
## Development and release
|
|
207
207
|
|
package/dist/core.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
import type { default as LlmService, TokenUsage } from "@deepseek-ai/dsh-llm";
|
|
1
2
|
import type { EpochHeader, SessionEvent } from "@deepseek-ai/dsh-session";
|
|
2
|
-
import type { TokenMeasurementBaseline } from "@deepseek-ai/dsh-token-meter";
|
|
3
3
|
import { SessionLogOffset } from "@deepseek-ai/dsh-session";
|
|
4
|
-
import type {
|
|
5
|
-
import type { MeterConfig, MeterEvent,
|
|
4
|
+
import type { TokenMeasurementBaseline } from "@deepseek-ai/dsh-token-meter";
|
|
5
|
+
import type { MeterConfig, MeterEvent, ReplayNode, ReplayState } from "./types.js";
|
|
6
6
|
export interface MeterServices {
|
|
7
7
|
llm?: Pick<LlmService, "imageRequestPricing" | "fileRequestText">;
|
|
8
8
|
}
|
package/dist/core.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGX,OAAO,IAAI,UAAU,EAErB,UAAU,EACV,MAAM,sBAAsB,CAAC;AAC9B,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAC1E,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AAC7E,OAAO,KAAK,EACX,WAAW,EACX,UAAU,EACV,UAAU,EACV,WAAW,EACX,MAAM,YAAY,CAAC;AACpB,MAAM,WAAW,aAAa;IAC7B,GAAG,CAAC,EAAE,IAAI,CAAC,UAAU,EAAE,qBAAqB,GAAG,iBAAiB,CAAC,CAAC;CAClE;AAoBD,eAAO,MAAM,YAAY,QAAO,WAK9B,CAAC;AAEH,wBAAgB,WAAW,CAAC,KAAK,EAAE,UAAU,UAM5C;AAED,wBAAgB,OAAO,CACtB,KAAK,EAAE,OAAO,CACb,YAAY,EACZ;IAAE,IAAI,EAAE,mBAAmB,GAAG,mBAAmB,CAAA;CAAE,CACnD,GACC,UAAU,GAAG,SAAS,CAIxB;AAED,oEAAoE;AACpE,wBAAgB,IAAI,CAAC,KAAK,EAAE,WAAW,EAAE,KAAK,EAAE,UAAU,GAAG,WAAW,CA0DvE;AA+CD,wBAAgB,YAAY,CAC3B,KAAK,EAAE,SAAS,UAAU,EAAE,EAC5B,MAAM,EAAE,WAAW,GAAG,IAAI,EAC1B,MAAM,EAAE,WAAW,EACnB,QAAQ,EAAE,aAAa;;;;;;;;;;;;;EA2DvB;AAED;;GAEG;AACH,wBAAgB,YAAY,CAC3B,KAAK,EAAE,WAAW,EAClB,MAAM,EAAE,WAAW,EACnB,QAAQ,GAAE,aAAkB,EAC5B,aAAa,CAAC,EAAE,WAAW;;;;;;;;;;;;;;;EAkC3B"}
|
package/dist/core.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { SessionLogOffset } from "@deepseek-ai/dsh-session";
|
|
2
|
-
import { canonicalHeader, deriveEventMessage, headerEquals, isSurfaceEvent, } from "@deepseek-ai/dsh-session";
|
|
3
|
-
import { estimateMessage as shadowPrice } from "@deepseek-ai/dsh-token-meter/estimate";
|
|
4
2
|
import { lastAssistantStreamChunk } from "@deepseek-ai/dsh-llm";
|
|
3
|
+
import { canonicalHeader, deriveEventMessage, headerEquals, isSurfaceEvent, } from "@deepseek-ai/dsh-session";
|
|
5
4
|
import { deepFreeze } from "@deepseek-ai/dsh-util-values";
|
|
6
5
|
import { countText, resolvePolicy } from "./tokenizers.js";
|
|
7
6
|
export const initialState = () => ({
|
|
@@ -62,7 +61,6 @@ export function fold(state, event) {
|
|
|
62
61
|
const node = {
|
|
63
62
|
seq: event.seq,
|
|
64
63
|
message,
|
|
65
|
-
heuristicTokens: message ? shadowPrice(message) : 0,
|
|
66
64
|
};
|
|
67
65
|
if (event.surfaceOp === "append")
|
|
68
66
|
next.nodes = [...state.nodes, node];
|
|
@@ -154,7 +152,8 @@ export function priceSurface(nodes, header, config, services) {
|
|
|
154
152
|
message.content.some((b) => b.type === "text" || b.type === "tool-call"))
|
|
155
153
|
tokens = body + policy.tokensPerMessage;
|
|
156
154
|
}
|
|
157
|
-
|
|
155
|
+
// DSH 0.2.1-rc.2 compaction requires this field for shadowedTokenCount.
|
|
156
|
+
return { seq: node.seq, tokens, heuristicTokens: tokens };
|
|
158
157
|
});
|
|
159
158
|
const surfaceTokens = priced.reduce((sum, node) => sum + node.tokens, 0);
|
|
160
159
|
const toolsTokens = header?.tools?.length
|
package/dist/core.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core.js","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"core.js","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAoB5D,OAAO,EAAE,wBAAwB,EAAE,MAAM,sBAAsB,CAAC;AAChE,OAAO,EACN,eAAe,EACf,kBAAkB,EAClB,YAAY,EACZ,cAAc,GACd,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAE3D,MAAM,CAAC,MAAM,YAAY,GAAG,GAAgB,EAAE,CAAC,CAAC;IAC/C,KAAK,EAAE,EAAE;IACT,MAAM,EAAE,IAAI;IACZ,MAAM,EAAE,IAAI;IACZ,WAAW,EAAE,gBAAgB,CAAC,CAAC,CAAC;CAChC,CAAC,CAAC;AAE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,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC;gBAClC,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,EAAE;gBAC7B,CAAC,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,OAAO,CAAC,aAAa,EAAE,CAAC;IAC1D,OAAO,UAAU,CAAC;QACjB,WAAW,EAAE,KAAK,CAAC,WAAW;QAC9B,QAAQ;QACR,kBAAkB;QAClB,WAAW,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,QAAQ,CAAC,MAAM,GAAG,kBAAkB,CAAC;QAC9D,aAAa,EAAE,OAAO,CAAC,aAAa;QACpC,WAAW,EAAE,OAAO,CAAC,WAAW;QAChC,KAAK,EAAE,OAAO,CAAC,KAAK;QACpB,SAAS,EAAE,OAAO,CAAC,MAAM,CAAC,SAAS;QACnC,cAAc,EAAE,OAAO,CAAC,cAAc;QACtC,aAAa,EAAE,OAAO,CAAC,aAAa;KACpC,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/projection.d.ts
CHANGED
|
@@ -1,22 +1,26 @@
|
|
|
1
|
-
import { SessionLogOffset, SessionSeq } from "@deepseek-ai/dsh-session";
|
|
2
|
-
import type { EpochHeader, SessionEvent } from "@deepseek-ai/dsh-session";
|
|
3
1
|
import type { Message } from "@deepseek-ai/dsh-llm";
|
|
2
|
+
import type { EpochHeader, SessionEvent } from "@deepseek-ai/dsh-session";
|
|
3
|
+
import { SessionLogOffset, SessionSeq } from "@deepseek-ai/dsh-session";
|
|
4
4
|
import type { ProjectionDefinition } from "@deepseek-ai/dsh-session-projection";
|
|
5
|
-
import type {
|
|
5
|
+
import type { ContextBreakdownProjection, ContextPressureProjection } from "@deepseek-ai/dsh-token-meter";
|
|
6
6
|
import { z } from "zod";
|
|
7
7
|
import type { MeterServices } from "./core.js";
|
|
8
|
-
import type { MeterConfig,
|
|
8
|
+
import type { MeterConfig, ReplayNode, ReplayState } from "./types.js";
|
|
9
9
|
export declare const replayProjection: ProjectionDefinition<"contextPolicyMeter">;
|
|
10
10
|
interface DisplayState extends ReplayState {
|
|
11
|
+
systems: {
|
|
12
|
+
seq: SessionSeq;
|
|
13
|
+
tokens: number;
|
|
14
|
+
}[];
|
|
11
15
|
nodes: (ReplayNode & {
|
|
12
16
|
system: boolean;
|
|
17
|
+
heuristicTokens: number;
|
|
13
18
|
})[];
|
|
14
19
|
surfaceTokens: number;
|
|
15
20
|
breakdown: ContextBreakdownProjection;
|
|
16
21
|
contextWindow?: number;
|
|
17
22
|
}
|
|
18
23
|
declare function initialDisplayState(): DisplayState;
|
|
19
|
-
declare function foldDisplay(state: DisplayState, event: SessionEvent): DisplayState;
|
|
20
24
|
export declare function createDisplayProjections(config: MeterConfig, services: MeterServices): {
|
|
21
25
|
pressure: {
|
|
22
26
|
key: "contextPressure";
|
|
@@ -41,11 +45,15 @@ export declare function createDisplayProjections(config: MeterConfig, services:
|
|
|
41
45
|
usage: import("@deepseek-ai/dsh-llm").TokenUsage;
|
|
42
46
|
}>>;
|
|
43
47
|
logRevision: z.ZodPipe<z.ZodNumber, z.ZodTransform<SessionLogOffset, number>>;
|
|
48
|
+
systems: z.ZodArray<z.ZodObject<{
|
|
49
|
+
seq: z.ZodPipe<z.ZodNumber, z.ZodTransform<SessionSeq, number>>;
|
|
50
|
+
tokens: z.ZodNumber;
|
|
51
|
+
}, z.core.$strip>>;
|
|
44
52
|
nodes: z.ZodArray<z.ZodObject<{
|
|
45
53
|
seq: z.ZodPipe<z.ZodNumber, z.ZodTransform<SessionSeq, number>>;
|
|
46
54
|
message: z.ZodNullable<z.ZodCustom<Message, Message>>;
|
|
47
|
-
heuristicTokens: z.ZodNumber;
|
|
48
55
|
system: z.ZodBoolean;
|
|
56
|
+
heuristicTokens: z.ZodNumber;
|
|
49
57
|
}, z.core.$strip>>;
|
|
50
58
|
surfaceTokens: z.ZodNumber;
|
|
51
59
|
breakdown: z.ZodObject<{
|
|
@@ -56,7 +64,7 @@ export declare function createDisplayProjections(config: MeterConfig, services:
|
|
|
56
64
|
contextWindow: z.ZodOptional<z.ZodNumber>;
|
|
57
65
|
}, z.core.$strip>;
|
|
58
66
|
init: typeof initialDisplayState;
|
|
59
|
-
apply:
|
|
67
|
+
apply: (state: DisplayState, event: SessionEvent) => DisplayState;
|
|
60
68
|
};
|
|
61
69
|
breakdown: {
|
|
62
70
|
key: "contextBreakdown";
|
|
@@ -81,11 +89,15 @@ export declare function createDisplayProjections(config: MeterConfig, services:
|
|
|
81
89
|
usage: import("@deepseek-ai/dsh-llm").TokenUsage;
|
|
82
90
|
}>>;
|
|
83
91
|
logRevision: z.ZodPipe<z.ZodNumber, z.ZodTransform<SessionLogOffset, number>>;
|
|
92
|
+
systems: z.ZodArray<z.ZodObject<{
|
|
93
|
+
seq: z.ZodPipe<z.ZodNumber, z.ZodTransform<SessionSeq, number>>;
|
|
94
|
+
tokens: z.ZodNumber;
|
|
95
|
+
}, z.core.$strip>>;
|
|
84
96
|
nodes: z.ZodArray<z.ZodObject<{
|
|
85
97
|
seq: z.ZodPipe<z.ZodNumber, z.ZodTransform<SessionSeq, number>>;
|
|
86
98
|
message: z.ZodNullable<z.ZodCustom<Message, Message>>;
|
|
87
|
-
heuristicTokens: z.ZodNumber;
|
|
88
99
|
system: z.ZodBoolean;
|
|
100
|
+
heuristicTokens: z.ZodNumber;
|
|
89
101
|
}, z.core.$strip>>;
|
|
90
102
|
surfaceTokens: z.ZodNumber;
|
|
91
103
|
breakdown: z.ZodObject<{
|
|
@@ -96,7 +108,7 @@ export declare function createDisplayProjections(config: MeterConfig, services:
|
|
|
96
108
|
contextWindow: z.ZodOptional<z.ZodNumber>;
|
|
97
109
|
}, z.core.$strip>;
|
|
98
110
|
init: typeof initialDisplayState;
|
|
99
|
-
apply:
|
|
111
|
+
apply: (state: DisplayState, event: SessionEvent) => DisplayState;
|
|
100
112
|
};
|
|
101
113
|
};
|
|
102
114
|
export {};
|
package/dist/projection.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"projection.d.ts","sourceRoot":"","sources":["../src/projection.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"projection.d.ts","sourceRoot":"","sources":["../src/projection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAC1E,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AACxE,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,qCAAqC,CAAC;AAChF,OAAO,KAAK,EACX,0BAA0B,EAC1B,yBAAyB,EACzB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAE/C,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAavE,eAAO,MAAM,gBAAgB,EAAE,oBAAoB,CAAC,oBAAoB,CAMvE,CAAC;AAGF,UAAU,YAAa,SAAQ,WAAW;IACzC,OAAO,EAAE;QAAE,GAAG,EAAE,UAAU,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IAC/C,KAAK,EAAE,CAAC,UAAU,GAAG;QAAE,MAAM,EAAE,OAAO,CAAC;QAAC,eAAe,EAAE,MAAM,CAAA;KAAE,CAAC,EAAE,CAAC;IACrE,aAAa,EAAE,MAAM,CAAC;IACtB,SAAS,EAAE,0BAA0B,CAAC;IACtC,aAAa,CAAC,EAAE,MAAM,CAAC;CACvB;AAiBD,iBAAS,mBAAmB,IAAI,YAAY,CAQ3C;AAmDD,wBAAgB,wBAAwB,CACvC,MAAM,EAAE,WAAW,EACnB,QAAQ,EAAE,aAAa;;;;;;;;;0BAmDP,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;uBAZZ,YAAY,SAAS,YAAY;;;;;;;;;;0BAoBjC,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;uBApBZ,YAAY,SAAS,YAAY;;EAwBjD"}
|
package/dist/projection.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import { SessionLogOffset, SessionSeq } from "@deepseek-ai/dsh-session";
|
|
2
|
-
import { estimateToolsTokens } from "@deepseek-ai/dsh-token-meter/estimate";
|
|
3
2
|
import { z } from "zod";
|
|
4
|
-
import {
|
|
3
|
+
import { fold, initialState, measureState } from "./core.js";
|
|
5
4
|
const count = z.number().int().nonnegative();
|
|
6
5
|
const nodeSchema = z.object({
|
|
7
6
|
seq: count.transform(SessionSeq),
|
|
8
7
|
message: z.custom().nullable(),
|
|
9
|
-
heuristicTokens: count,
|
|
10
8
|
});
|
|
11
9
|
const replaySchema = z.object({
|
|
12
10
|
nodes: z.array(nodeSchema),
|
|
@@ -16,7 +14,7 @@ const replaySchema = z.object({
|
|
|
16
14
|
});
|
|
17
15
|
export const replayProjection = {
|
|
18
16
|
key: "contextPolicyMeter",
|
|
19
|
-
stateVersion:
|
|
17
|
+
stateVersion: 2,
|
|
20
18
|
stateSchema: replaySchema,
|
|
21
19
|
init: initialState,
|
|
22
20
|
apply: fold,
|
|
@@ -27,7 +25,8 @@ const breakdownSchema = z.object({
|
|
|
27
25
|
messageTokens: count,
|
|
28
26
|
});
|
|
29
27
|
const displaySchema = replaySchema.extend({
|
|
30
|
-
|
|
28
|
+
systems: z.array(z.object({ seq: count.transform(SessionSeq), tokens: count })),
|
|
29
|
+
nodes: z.array(nodeSchema.extend({ system: z.boolean(), heuristicTokens: count })),
|
|
31
30
|
surfaceTokens: count,
|
|
32
31
|
breakdown: breakdownSchema,
|
|
33
32
|
contextWindow: z.number().int().positive().optional(),
|
|
@@ -35,12 +34,13 @@ const displaySchema = replaySchema.extend({
|
|
|
35
34
|
function initialDisplayState() {
|
|
36
35
|
return {
|
|
37
36
|
...initialState(),
|
|
37
|
+
systems: [],
|
|
38
38
|
nodes: [],
|
|
39
39
|
surfaceTokens: 0,
|
|
40
40
|
breakdown: { systemTokens: 0, toolsTokens: 0, messageTokens: 0 },
|
|
41
41
|
};
|
|
42
42
|
}
|
|
43
|
-
function foldDisplay(state, event) {
|
|
43
|
+
function foldDisplay(state, event, config, services) {
|
|
44
44
|
const next = fold(state, event);
|
|
45
45
|
const { contextWindow, ...withoutWindow } = {
|
|
46
46
|
...next,
|
|
@@ -52,28 +52,27 @@ function foldDisplay(state, event) {
|
|
|
52
52
|
next.anchor === state.anchor &&
|
|
53
53
|
window === state.contextWindow)
|
|
54
54
|
return state;
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
: nodes.
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
55
|
+
const measurement = measureState(next, config, services);
|
|
56
|
+
// Empty system events have no derived Message but still occupy a surface position.
|
|
57
|
+
const systemSeqs = new Set(state.systems.map((node) => node.seq));
|
|
58
|
+
const nodes = next.nodes.map((node, index) => ({
|
|
59
|
+
...node,
|
|
60
|
+
system: systemSeqs.has(node.seq) ||
|
|
61
|
+
(event.type === "system/message" && node.seq === event.seq),
|
|
62
|
+
// Native consumers use this field; it carries the model count, not a second estimator.
|
|
63
|
+
heuristicTokens: measurement.nodes[index].tokens,
|
|
64
|
+
}));
|
|
65
|
+
const surfaceTokens = measurement.surfaceTokens;
|
|
66
|
+
const systems = nodes.flatMap((node) => node.system ? [{ seq: node.seq, tokens: node.heuristicTokens }] : []);
|
|
67
|
+
const systemTokens = systems.findLast((node) => node.tokens > 0)?.tokens ?? 0;
|
|
68
68
|
return {
|
|
69
69
|
...withoutWindow,
|
|
70
|
+
systems,
|
|
70
71
|
nodes,
|
|
71
72
|
surfaceTokens,
|
|
72
73
|
breakdown: {
|
|
73
74
|
systemTokens,
|
|
74
|
-
toolsTokens:
|
|
75
|
-
? state.breakdown.toolsTokens
|
|
76
|
-
: estimateToolsTokens(next.header ?? undefined),
|
|
75
|
+
toolsTokens: measurement.toolsTokens,
|
|
77
76
|
messageTokens: surfaceTokens - systemTokens,
|
|
78
77
|
},
|
|
79
78
|
...(window === undefined ? {} : { contextWindow: window }),
|
|
@@ -86,8 +85,7 @@ export function createDisplayProjections(config, services) {
|
|
|
86
85
|
if (cached)
|
|
87
86
|
return cached;
|
|
88
87
|
const measurement = measureState(state, config, services);
|
|
89
|
-
const
|
|
90
|
-
const systemTokens = measurement.nodes.find((node) => node.seq === system?.seq)?.tokens ?? 0;
|
|
88
|
+
const systemTokens = measurement.nodes.findLast((node, index) => state.nodes[index].system && node.tokens > 0)?.tokens ?? 0;
|
|
91
89
|
const pressure = {
|
|
92
90
|
projectedTokens: measurement.totalTokens,
|
|
93
91
|
...(measurement.baseline.kind === "usage"
|
|
@@ -107,10 +105,10 @@ export function createDisplayProjections(config, services) {
|
|
|
107
105
|
return value;
|
|
108
106
|
};
|
|
109
107
|
const replay = {
|
|
110
|
-
stateVersion:
|
|
108
|
+
stateVersion: 9,
|
|
111
109
|
stateSchema: displaySchema,
|
|
112
110
|
init: initialDisplayState,
|
|
113
|
-
apply: foldDisplay,
|
|
111
|
+
apply: (state, event) => foldDisplay(state, event, config, services),
|
|
114
112
|
};
|
|
115
113
|
const pressure = {
|
|
116
114
|
...replay,
|
package/dist/projection.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"projection.js","sourceRoot":"","sources":["../src/projection.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"projection.js","sourceRoot":"","sources":["../src/projection.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAMxE,OAAO,EAAE,CAAC,EAAE,MAAM,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OAAO,KAAK,CAAC;IACd,CAAC,CAAC;IACF,MAAM,MAAM,GAAG;QACd,YAAY,EAAE,CAAC;QACf,WAAW,EAAE,aAAa;QAC1B,IAAI,EAAE,mBAAmB;QACzB,KAAK,EAAE,CAAC,KAAmB,EAAE,KAAmB,EAAE,EAAE,CACnD,WAAW,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,CAAC;KAC5C,CAAC;IACF,MAAM,QAAQ,GAAG;QAChB,GAAG,MAAM;QACT,GAAG,EAAE,iBAAiB;QACtB,IAAI,EAAE;YACL,UAAU,EAAE,CAAC,CAAC,MAAM,CAAC;gBACpB,cAAc,EAAE,KAAK,CAAC,QAAQ,EAAE;gBAChC,eAAe,EAAE,KAAK;gBACtB,aAAa,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE;aACrD,CAAC;YACF,IAAI,EAAE,CAAC,KAAmB,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,QAAQ;SACnD;KAC+D,CAAC;IAClE,MAAM,SAAS,GAAG;QACjB,GAAG,MAAM;QACT,GAAG,EAAE,kBAAkB;QACvB,IAAI,EAAE;YACL,UAAU,EAAE,eAAe;YAC3B,IAAI,EAAE,CAAC,KAAmB,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,SAAS;SACpD;KACgE,CAAC;IACnE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC;AAChC,CAAC"}
|
package/dist/token-meter.js
CHANGED
|
@@ -53,7 +53,7 @@ export class ModelTokenMeter extends Service {
|
|
|
53
53
|
return measureState(state, this.config, this.services, requestHeader);
|
|
54
54
|
}
|
|
55
55
|
estimateMessage(message) {
|
|
56
|
-
return priceSurface([{ seq: SessionSeq(0), message
|
|
56
|
+
return priceSurface([{ seq: SessionSeq(0), message }], null, this.config, this.services).surfaceTokens;
|
|
57
57
|
}
|
|
58
58
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AAkB,CAAC,QAAQ,CAAC,gBAAgB,CAAC,CAAC;IACnD,CAAC;IACD,MAAM,CAAc;IACpB,QAAQ,CAAgB;IACxB,OAAO,CAAC,OAAgB,EAAE,aAA2B;QACpD,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,kBAAkB,CAAC,OAAO,CAChD,OAAO,EACP,oBAAoB,CACpB,CAAC;QACF,IAAI,CAAC,KAAK;YACT,MAAM,IAAI,KAAK,CACd,8DAA8D,CAC9D,CAAC;QACH,OAAO,YAAY,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,QAAQ,EAAE,aAAa,CAAC,CAAC;IACvE,CAAC;IACD,eAAe,CAAC,OAAgB;QAC/B,OAAO,YAAY,CAClB,CAAC,EAAE,GAAG,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EACjC,IAAI,EACJ,IAAI,CAAC,MAAM,EACX,IAAI,CAAC,QAAQ,CACb,CAAC,aAAa,CAAC;IACjB,CAAC;CACD;AAED,MAAM,UAAU,KAAK,CAAC,GAAY,EAAE,MAAmB;IACtD,IAAI,eAAe,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;AAClC,CAAC;AAED,eAAe,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC"}
|
package/dist/types.d.ts
CHANGED
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,WAAW,EACX,UAAU,EACV,gBAAgB,EAChB,YAAY,EACZ,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAEhE,6EAA6E;AAC7E,MAAM,MAAM,UAAU,GACnB,YAAY,GACZ;IACA,IAAI,EAAE,eAAe,CAAC;IACtB,GAAG,EAAE,UAA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|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
2
|
+
"name": "@snylonue/dsh-context-policy",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Percentage-only compaction and model-configurable GPT and DeepSeek token counting for DSH",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"dsh-plugin"
|
|
9
|
+
],
|
|
10
|
+
"repository": "github:snylonue/dsh-context-policy",
|
|
11
|
+
"packageManager": "pnpm@12.3.4",
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=24",
|
|
17
|
+
"dsh": "0.2.1-rc.2"
|
|
18
|
+
},
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/token-meter.d.ts",
|
|
22
|
+
"default": "./dist/token-meter.js"
|
|
23
|
+
},
|
|
24
|
+
"./token-meter": {
|
|
25
|
+
"types": "./dist/token-meter.d.ts",
|
|
26
|
+
"default": "./dist/token-meter.js"
|
|
27
|
+
},
|
|
28
|
+
"./compaction": {
|
|
29
|
+
"types": "./dist/compaction.d.ts",
|
|
30
|
+
"default": "./dist/compaction.js"
|
|
31
|
+
},
|
|
32
|
+
"./tokenizers": {
|
|
33
|
+
"types": "./dist/tokenizers.d.ts",
|
|
34
|
+
"default": "./dist/tokenizers.js"
|
|
35
|
+
},
|
|
36
|
+
"./core": {
|
|
37
|
+
"types": "./dist/core.d.ts",
|
|
38
|
+
"default": "./dist/core.js"
|
|
39
|
+
},
|
|
40
|
+
"./locale/*.json": "./locale/*.json",
|
|
41
|
+
"./package.json": "./package.json"
|
|
42
|
+
},
|
|
43
|
+
"files": [
|
|
44
|
+
"dist",
|
|
45
|
+
"src",
|
|
46
|
+
"assets",
|
|
47
|
+
"cordis.patch.yml",
|
|
48
|
+
"locale/*.json",
|
|
49
|
+
"README.md",
|
|
50
|
+
"LICENSE",
|
|
51
|
+
"UPSTREAM.md",
|
|
52
|
+
"examples",
|
|
53
|
+
"tsconfig.json",
|
|
54
|
+
"scripts",
|
|
55
|
+
"tsconfig.check.json",
|
|
56
|
+
"tests"
|
|
57
|
+
],
|
|
58
|
+
"scripts": {
|
|
59
|
+
"build": "tsc -p tsconfig.json",
|
|
60
|
+
"test": "node --test tests/*.test.ts",
|
|
61
|
+
"check": "node scripts/check.ts",
|
|
62
|
+
"prepack": "node scripts/check.ts",
|
|
63
|
+
"format": "biome format --write .",
|
|
64
|
+
"lint": "biome lint ."
|
|
65
|
+
},
|
|
66
|
+
"dependencies": {
|
|
67
|
+
"@huggingface/tokenizers": "0.2.0"
|
|
68
|
+
},
|
|
69
|
+
"peerDependencies": {
|
|
70
|
+
"@deepseek-ai/cordis": "~4.0.5",
|
|
71
|
+
"@deepseek-ai/dsh-compaction": "0.2.1-rc.2",
|
|
72
|
+
"@deepseek-ai/dsh-compaction-basic": "0.2.1-rc.2",
|
|
73
|
+
"@deepseek-ai/dsh-llm": "0.2.1-rc.2",
|
|
74
|
+
"@deepseek-ai/dsh-session": "0.2.1-rc.2",
|
|
75
|
+
"@deepseek-ai/dsh-session-projection": "0.2.1-rc.2",
|
|
76
|
+
"@deepseek-ai/dsh-token-meter": "0.2.1-rc.2",
|
|
77
|
+
"@deepseek-ai/dsh-util-values": "0.2.1-rc.2",
|
|
78
|
+
"@deepseek-ai/schemastery": "~3.18.5",
|
|
79
|
+
"zod": "^4.4.3"
|
|
80
|
+
},
|
|
81
|
+
"dsh": {
|
|
82
|
+
"manifestVersion": 1,
|
|
83
|
+
"bundle": {
|
|
84
|
+
"patch": "./cordis.patch.yml"
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
"devDependencies": {
|
|
88
|
+
"@biomejs/biome": "2",
|
|
89
|
+
"@deepseek-ai/cordis": "4.0.5",
|
|
90
|
+
"@deepseek-ai/dsh-app-boot": "0.2.1-rc.2",
|
|
91
|
+
"@deepseek-ai/dsh-compaction": "0.2.1-rc.2",
|
|
92
|
+
"@deepseek-ai/dsh-compaction-basic": "0.2.1-rc.2",
|
|
93
|
+
"@deepseek-ai/dsh-llm": "0.2.1-rc.2",
|
|
94
|
+
"@deepseek-ai/dsh-session": "0.2.1-rc.2",
|
|
95
|
+
"@deepseek-ai/dsh-session-projection": "0.2.1-rc.2",
|
|
96
|
+
"@deepseek-ai/dsh-token-meter": "0.2.1-rc.2",
|
|
97
|
+
"@deepseek-ai/dsh-util-values": "0.2.1-rc.2",
|
|
98
|
+
"@deepseek-ai/schemastery": "3.18.5",
|
|
99
|
+
"@types/node": "24.19.0",
|
|
100
|
+
"typescript": "5.9.3",
|
|
101
|
+
"zod": "4.4.3"
|
|
102
|
+
}
|
|
103
|
+
}
|
package/scripts/check.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { spawnSync } from "node:child_process";
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
|
+
|
|
4
5
|
const require = createRequire(import.meta.url);
|
|
5
6
|
const cwd = fileURLToPath(new URL("../", import.meta.url));
|
|
6
7
|
for (const args of [
|
|
@@ -8,6 +9,7 @@ for (const args of [
|
|
|
8
9
|
[require.resolve("typescript/bin/tsc"), "-p", "tsconfig.check.json"],
|
|
9
10
|
[
|
|
10
11
|
"--test",
|
|
12
|
+
"tests/current-host.test.ts",
|
|
11
13
|
"tests/policy.test.ts",
|
|
12
14
|
"tests/bundle.test.ts",
|
|
13
15
|
"tests/tokenizers.test.ts",
|
package/src/core.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import type { EpochHeader, SessionEvent } from "@deepseek-ai/dsh-session";
|
|
2
|
-
import type { TokenMeasurementBaseline } from "@deepseek-ai/dsh-token-meter";
|
|
3
|
-
import { SessionLogOffset } from "@deepseek-ai/dsh-session";
|
|
4
1
|
import type {
|
|
5
|
-
default as LlmService,
|
|
6
|
-
Message,
|
|
7
2
|
LlmImageRequestPrice,
|
|
8
3
|
LlmImageRequestPricing,
|
|
4
|
+
default as LlmService,
|
|
5
|
+
Message,
|
|
9
6
|
TokenUsage,
|
|
10
7
|
} from "@deepseek-ai/dsh-llm";
|
|
8
|
+
import type { EpochHeader, SessionEvent } from "@deepseek-ai/dsh-session";
|
|
9
|
+
import { SessionLogOffset } from "@deepseek-ai/dsh-session";
|
|
10
|
+
import type { TokenMeasurementBaseline } from "@deepseek-ai/dsh-token-meter";
|
|
11
11
|
import type {
|
|
12
12
|
MeterConfig,
|
|
13
13
|
MeterEvent,
|
|
14
|
-
ReplayState,
|
|
15
14
|
ReplayNode,
|
|
15
|
+
ReplayState,
|
|
16
16
|
} from "./types.js";
|
|
17
17
|
export interface MeterServices {
|
|
18
18
|
llm?: Pick<LlmService, "imageRequestPricing" | "fileRequestText">;
|
|
@@ -25,14 +25,14 @@ interface Attachments {
|
|
|
25
25
|
unpricedImages: boolean;
|
|
26
26
|
unpricedFiles: boolean;
|
|
27
27
|
}
|
|
28
|
+
|
|
29
|
+
import { lastAssistantStreamChunk } from "@deepseek-ai/dsh-llm";
|
|
28
30
|
import {
|
|
29
31
|
canonicalHeader,
|
|
30
32
|
deriveEventMessage,
|
|
31
33
|
headerEquals,
|
|
32
34
|
isSurfaceEvent,
|
|
33
35
|
} from "@deepseek-ai/dsh-session";
|
|
34
|
-
import { estimateMessage as shadowPrice } from "@deepseek-ai/dsh-token-meter/estimate";
|
|
35
|
-
import { lastAssistantStreamChunk } from "@deepseek-ai/dsh-llm";
|
|
36
36
|
import { deepFreeze } from "@deepseek-ai/dsh-util-values";
|
|
37
37
|
import { countText, resolvePolicy } from "./tokenizers.js";
|
|
38
38
|
|
|
@@ -105,7 +105,6 @@ export function fold(state: ReplayState, event: MeterEvent): ReplayState {
|
|
|
105
105
|
const node = {
|
|
106
106
|
seq: event.seq,
|
|
107
107
|
message,
|
|
108
|
-
heuristicTokens: message ? shadowPrice(message) : 0,
|
|
109
108
|
};
|
|
110
109
|
if (event.surfaceOp === "append") next.nodes = [...state.nodes, node];
|
|
111
110
|
else {
|
|
@@ -213,7 +212,8 @@ export function priceSurface(
|
|
|
213
212
|
)
|
|
214
213
|
tokens = body + policy.tokensPerMessage;
|
|
215
214
|
}
|
|
216
|
-
|
|
215
|
+
// DSH 0.2.1-rc.2 compaction requires this field for shadowedTokenCount.
|
|
216
|
+
return { seq: node.seq, tokens, heuristicTokens: tokens };
|
|
217
217
|
});
|
|
218
218
|
const surfaceTokens = priced.reduce((sum, node) => sum + node.tokens, 0);
|
|
219
219
|
const toolsTokens = header?.tools?.length
|
package/src/projection.ts
CHANGED
|
@@ -1,22 +1,20 @@
|
|
|
1
|
-
import { SessionLogOffset, SessionSeq } from "@deepseek-ai/dsh-session";
|
|
2
|
-
import type { EpochHeader, SessionEvent } from "@deepseek-ai/dsh-session";
|
|
3
1
|
import type { Message } from "@deepseek-ai/dsh-llm";
|
|
2
|
+
import type { EpochHeader, SessionEvent } from "@deepseek-ai/dsh-session";
|
|
3
|
+
import { SessionLogOffset, SessionSeq } from "@deepseek-ai/dsh-session";
|
|
4
4
|
import type { ProjectionDefinition } from "@deepseek-ai/dsh-session-projection";
|
|
5
5
|
import type {
|
|
6
|
-
ContextPressureProjection,
|
|
7
6
|
ContextBreakdownProjection,
|
|
7
|
+
ContextPressureProjection,
|
|
8
8
|
} from "@deepseek-ai/dsh-token-meter";
|
|
9
|
-
import { estimateToolsTokens } from "@deepseek-ai/dsh-token-meter/estimate";
|
|
10
9
|
import { z } from "zod";
|
|
11
|
-
import { initialState, fold, measureState } from "./core.js";
|
|
12
10
|
import type { MeterServices } from "./core.js";
|
|
13
|
-
import
|
|
11
|
+
import { fold, initialState, measureState } from "./core.js";
|
|
12
|
+
import type { MeterConfig, ReplayNode, ReplayState } from "./types.js";
|
|
14
13
|
|
|
15
14
|
const count = z.number().int().nonnegative();
|
|
16
15
|
const nodeSchema = z.object({
|
|
17
16
|
seq: count.transform(SessionSeq),
|
|
18
17
|
message: z.custom<Message>().nullable(),
|
|
19
|
-
heuristicTokens: count,
|
|
20
18
|
});
|
|
21
19
|
const replaySchema = z.object({
|
|
22
20
|
nodes: z.array(nodeSchema),
|
|
@@ -26,15 +24,16 @@ const replaySchema = z.object({
|
|
|
26
24
|
});
|
|
27
25
|
export const replayProjection: ProjectionDefinition<"contextPolicyMeter"> = {
|
|
28
26
|
key: "contextPolicyMeter",
|
|
29
|
-
stateVersion:
|
|
27
|
+
stateVersion: 2,
|
|
30
28
|
stateSchema: replaySchema,
|
|
31
29
|
init: initialState,
|
|
32
30
|
apply: fold,
|
|
33
31
|
};
|
|
34
32
|
|
|
35
|
-
//
|
|
33
|
+
// These native projection keys must satisfy the DSH 0.2.1-rc.2 state contract.
|
|
36
34
|
interface DisplayState extends ReplayState {
|
|
37
|
-
|
|
35
|
+
systems: { seq: SessionSeq; tokens: number }[];
|
|
36
|
+
nodes: (ReplayNode & { system: boolean; heuristicTokens: number })[];
|
|
38
37
|
surfaceTokens: number;
|
|
39
38
|
breakdown: ContextBreakdownProjection;
|
|
40
39
|
contextWindow?: number;
|
|
@@ -45,7 +44,12 @@ const breakdownSchema = z.object({
|
|
|
45
44
|
messageTokens: count,
|
|
46
45
|
});
|
|
47
46
|
const displaySchema = replaySchema.extend({
|
|
48
|
-
|
|
47
|
+
systems: z.array(
|
|
48
|
+
z.object({ seq: count.transform(SessionSeq), tokens: count }),
|
|
49
|
+
),
|
|
50
|
+
nodes: z.array(
|
|
51
|
+
nodeSchema.extend({ system: z.boolean(), heuristicTokens: count }),
|
|
52
|
+
),
|
|
49
53
|
surfaceTokens: count,
|
|
50
54
|
breakdown: breakdownSchema,
|
|
51
55
|
contextWindow: z.number().int().positive().optional(),
|
|
@@ -53,12 +57,18 @@ const displaySchema = replaySchema.extend({
|
|
|
53
57
|
function initialDisplayState(): DisplayState {
|
|
54
58
|
return {
|
|
55
59
|
...initialState(),
|
|
60
|
+
systems: [],
|
|
56
61
|
nodes: [],
|
|
57
62
|
surfaceTokens: 0,
|
|
58
63
|
breakdown: { systemTokens: 0, toolsTokens: 0, messageTokens: 0 },
|
|
59
64
|
};
|
|
60
65
|
}
|
|
61
|
-
function foldDisplay(
|
|
66
|
+
function foldDisplay(
|
|
67
|
+
state: DisplayState,
|
|
68
|
+
event: SessionEvent,
|
|
69
|
+
config: MeterConfig,
|
|
70
|
+
services: MeterServices,
|
|
71
|
+
): DisplayState {
|
|
62
72
|
const next = fold(state, event);
|
|
63
73
|
const { contextWindow, ...withoutWindow } = {
|
|
64
74
|
...next,
|
|
@@ -73,32 +83,30 @@ function foldDisplay(state: DisplayState, event: SessionEvent): DisplayState {
|
|
|
73
83
|
window === state.contextWindow
|
|
74
84
|
)
|
|
75
85
|
return state;
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
86
|
+
const measurement = measureState(next, config, services);
|
|
87
|
+
// Empty system events have no derived Message but still occupy a surface position.
|
|
88
|
+
const systemSeqs = new Set(state.systems.map((node) => node.seq));
|
|
89
|
+
const nodes = next.nodes.map((node, index) => ({
|
|
90
|
+
...node,
|
|
91
|
+
system:
|
|
92
|
+
systemSeqs.has(node.seq) ||
|
|
93
|
+
(event.type === "system/message" && node.seq === event.seq),
|
|
94
|
+
// Native consumers use this field; it carries the model count, not a second estimator.
|
|
95
|
+
heuristicTokens: measurement.nodes[index].tokens,
|
|
96
|
+
}));
|
|
97
|
+
const surfaceTokens = measurement.surfaceTokens;
|
|
98
|
+
const systems = nodes.flatMap((node) =>
|
|
99
|
+
node.system ? [{ seq: node.seq, tokens: node.heuristicTokens }] : [],
|
|
100
|
+
);
|
|
101
|
+
const systemTokens = systems.findLast((node) => node.tokens > 0)?.tokens ?? 0;
|
|
92
102
|
return {
|
|
93
103
|
...withoutWindow,
|
|
104
|
+
systems,
|
|
94
105
|
nodes,
|
|
95
106
|
surfaceTokens,
|
|
96
107
|
breakdown: {
|
|
97
108
|
systemTokens,
|
|
98
|
-
toolsTokens:
|
|
99
|
-
next.header === state.header
|
|
100
|
-
? state.breakdown.toolsTokens
|
|
101
|
-
: estimateToolsTokens(next.header ?? undefined),
|
|
109
|
+
toolsTokens: measurement.toolsTokens,
|
|
102
110
|
messageTokens: surfaceTokens - systemTokens,
|
|
103
111
|
},
|
|
104
112
|
...(window === undefined ? {} : { contextWindow: window }),
|
|
@@ -120,11 +128,10 @@ export function createDisplayProjections(
|
|
|
120
128
|
const cached = views.get(state);
|
|
121
129
|
if (cached) return cached;
|
|
122
130
|
const measurement = measureState(state, config, services);
|
|
123
|
-
const system = state.nodes.findLast(
|
|
124
|
-
(node) => node.system && node.message?.content.length,
|
|
125
|
-
);
|
|
126
131
|
const systemTokens =
|
|
127
|
-
measurement.nodes.
|
|
132
|
+
measurement.nodes.findLast(
|
|
133
|
+
(node, index) => state.nodes[index].system && node.tokens > 0,
|
|
134
|
+
)?.tokens ?? 0;
|
|
128
135
|
const pressure: ContextPressureProjection = {
|
|
129
136
|
projectedTokens: measurement.totalTokens,
|
|
130
137
|
...(measurement.baseline.kind === "usage"
|
|
@@ -144,10 +151,11 @@ export function createDisplayProjections(
|
|
|
144
151
|
return value;
|
|
145
152
|
};
|
|
146
153
|
const replay = {
|
|
147
|
-
stateVersion:
|
|
154
|
+
stateVersion: 9,
|
|
148
155
|
stateSchema: displaySchema,
|
|
149
156
|
init: initialDisplayState,
|
|
150
|
-
apply:
|
|
157
|
+
apply: (state: DisplayState, event: SessionEvent) =>
|
|
158
|
+
foldDisplay(state, event, config, services),
|
|
151
159
|
};
|
|
152
160
|
const pressure = {
|
|
153
161
|
...replay,
|
package/src/token-meter.ts
CHANGED
package/src/types.ts
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { test } from "node:test";
|
|
4
|
+
|
|
5
|
+
test("targets only the current DSH host rather than retaining older-host ranges", () => {
|
|
6
|
+
const manifest = JSON.parse(
|
|
7
|
+
readFileSync(new URL("../package.json", import.meta.url), "utf8"),
|
|
8
|
+
);
|
|
9
|
+
assert.equal(manifest.engines.dsh, "0.2.1-rc.2");
|
|
10
|
+
assert.equal(manifest.peerDependencies["@deepseek-ai/cordis"], "~4.0.5");
|
|
11
|
+
for (const group of ["dependencies", "devDependencies", "peerDependencies"])
|
|
12
|
+
for (const [name, version] of Object.entries(manifest[group] ?? {}))
|
|
13
|
+
if (name.startsWith("@deepseek-ai/dsh-"))
|
|
14
|
+
assert.equal(version, "0.2.1-rc.2", name);
|
|
15
|
+
});
|
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) =>
|
|
@@ -296,14 +297,16 @@ test("failure with zero usage preserves occupancy; retries still accumulate bill
|
|
|
296
297
|
}
|
|
297
298
|
});
|
|
298
299
|
|
|
299
|
-
test("replacement reprices removed content
|
|
300
|
+
test("replacement reprices removed content and uses model counts for durable shadow prices", () => {
|
|
300
301
|
let state = [
|
|
301
302
|
event(0, "request/header", { header: header() }),
|
|
302
303
|
user(1, "long ".repeat(600)),
|
|
303
304
|
assistant(2, { inputTokens: 2000, outputTokens: 2 }),
|
|
304
305
|
].reduce(fold, initialState());
|
|
305
306
|
const before = measureState(state, meterConfig);
|
|
306
|
-
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));
|
|
307
310
|
state = fold(
|
|
308
311
|
state,
|
|
309
312
|
user(3, "summary", { startSeq: SessionSeq(1), endSeq: SessionSeq(1) }),
|
|
@@ -316,8 +319,9 @@ test("replacement reprices removed content while keeping the fixed durable shado
|
|
|
316
319
|
[3, 2],
|
|
317
320
|
);
|
|
318
321
|
assert(state.anchor);
|
|
319
|
-
assert.equal(state.anchor.nodes[0].
|
|
320
|
-
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)));
|
|
321
325
|
});
|
|
322
326
|
|
|
323
327
|
test("header/model changes reject old usage anchor and reprice using mapped tokenizer", () => {
|
|
@@ -424,7 +428,11 @@ test("actual Cordis registry replays nonempty logs, validates all UI views and u
|
|
|
424
428
|
assert(ctx.sessionProjections.stateOf(s, "contextPolicyMeter"));
|
|
425
429
|
assert.equal(
|
|
426
430
|
ctx.sessionProjections.stateOf(s, "contextPressure")?.surfaceTokens,
|
|
427
|
-
actual.
|
|
431
|
+
actual.surfaceTokens,
|
|
432
|
+
);
|
|
433
|
+
assert.deepEqual(
|
|
434
|
+
ctx.sessionProjections.stateOf(s, "contextBreakdown")?.breakdown,
|
|
435
|
+
values.contextBreakdown,
|
|
428
436
|
);
|
|
429
437
|
assert.equal(values.tokenUsage?.uncachedInputTokens, 100);
|
|
430
438
|
const checkpoint = ctx.sessionProjections.checkpoint(s);
|
|
@@ -473,6 +481,294 @@ test("actual Cordis registry replays nonempty logs, validates all UI views and u
|
|
|
473
481
|
}
|
|
474
482
|
});
|
|
475
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
|
+
|
|
476
772
|
test("default 80% boundary triggers, independent of request output limits; pruner can avert compaction", async () => {
|
|
477
773
|
const ctx = new Context();
|
|
478
774
|
await ctx.plugin(Registry).await();
|