@animalabs/context-manager 0.5.2 → 0.5.4
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/dist/src/adaptive/index.d.ts +6 -0
- package/dist/src/adaptive/index.d.ts.map +1 -1
- package/dist/src/adaptive/index.js +8 -0
- package/dist/src/adaptive/index.js.map +1 -1
- package/dist/src/adaptive/kv-cache-sim.d.ts +128 -0
- package/dist/src/adaptive/kv-cache-sim.d.ts.map +1 -0
- package/dist/src/adaptive/kv-cache-sim.js +0 -0
- package/dist/src/adaptive/kv-cache-sim.js.map +1 -0
- package/dist/src/adaptive/kv-control.d.ts +152 -0
- package/dist/src/adaptive/kv-control.d.ts.map +1 -0
- package/dist/src/adaptive/kv-control.js +368 -0
- package/dist/src/adaptive/kv-control.js.map +1 -0
- package/dist/src/adaptive/kv-replay.d.ts +76 -0
- package/dist/src/adaptive/kv-replay.d.ts.map +1 -0
- package/dist/src/adaptive/kv-replay.js +175 -0
- package/dist/src/adaptive/kv-replay.js.map +1 -0
- package/dist/src/adaptive/render-offsets.d.ts +61 -0
- package/dist/src/adaptive/render-offsets.d.ts.map +1 -0
- package/dist/src/adaptive/render-offsets.js +88 -0
- package/dist/src/adaptive/render-offsets.js.map +1 -0
- package/dist/src/adaptive/strategies/kv-stable.d.ts +48 -0
- package/dist/src/adaptive/strategies/kv-stable.d.ts.map +1 -0
- package/dist/src/adaptive/strategies/kv-stable.js +102 -0
- package/dist/src/adaptive/strategies/kv-stable.js.map +1 -0
- package/dist/src/adaptive/summary-tree.d.ts +98 -0
- package/dist/src/adaptive/summary-tree.d.ts.map +1 -0
- package/dist/src/adaptive/summary-tree.js +214 -0
- package/dist/src/adaptive/summary-tree.js.map +1 -0
- package/dist/src/context-log.d.ts.map +1 -1
- package/dist/src/context-log.js +7 -2
- package/dist/src/context-log.js.map +1 -1
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/message-store.js +1 -1
- package/dist/src/message-store.js.map +1 -1
- package/dist/src/normalize-tool-messages.d.ts +33 -0
- package/dist/src/normalize-tool-messages.d.ts.map +1 -1
- package/dist/src/normalize-tool-messages.js +59 -0
- package/dist/src/normalize-tool-messages.js.map +1 -1
- package/dist/src/strategies/autobiographical.d.ts +101 -39
- package/dist/src/strategies/autobiographical.d.ts.map +1 -1
- package/dist/src/strategies/autobiographical.js +398 -222
- package/dist/src/strategies/autobiographical.js.map +1 -1
- package/dist/src/types/strategy.d.ts +40 -1
- package/dist/src/types/strategy.d.ts.map +1 -1
- package/dist/src/types/strategy.js +2 -0
- package/dist/src/types/strategy.js.map +1 -1
- package/dist/test/adaptive/kv-cache-sim.test.d.ts +16 -0
- package/dist/test/adaptive/kv-cache-sim.test.d.ts.map +1 -0
- package/dist/test/adaptive/kv-cache-sim.test.js +173 -0
- package/dist/test/adaptive/kv-cache-sim.test.js.map +1 -0
- package/dist/test/adaptive/kv-control.test.d.ts +14 -0
- package/dist/test/adaptive/kv-control.test.d.ts.map +1 -0
- package/dist/test/adaptive/kv-control.test.js +130 -0
- package/dist/test/adaptive/kv-control.test.js.map +1 -0
- package/dist/test/adaptive/kv-replay.test.d.ts +15 -0
- package/dist/test/adaptive/kv-replay.test.d.ts.map +1 -0
- package/dist/test/adaptive/kv-replay.test.js +112 -0
- package/dist/test/adaptive/kv-replay.test.js.map +1 -0
- package/dist/test/adaptive/kv-stable-strategy.test.d.ts +10 -0
- package/dist/test/adaptive/kv-stable-strategy.test.d.ts.map +1 -0
- package/dist/test/adaptive/kv-stable-strategy.test.js +89 -0
- package/dist/test/adaptive/kv-stable-strategy.test.js.map +1 -0
- package/dist/test/adaptive/render-offsets.test.d.ts +11 -0
- package/dist/test/adaptive/render-offsets.test.d.ts.map +1 -0
- package/dist/test/adaptive/render-offsets.test.js +85 -0
- package/dist/test/adaptive/render-offsets.test.js.map +1 -0
- package/dist/test/adaptive/summary-tree.test.d.ts +5 -0
- package/dist/test/adaptive/summary-tree.test.d.ts.map +1 -0
- package/dist/test/adaptive/summary-tree.test.js +72 -0
- package/dist/test/adaptive/summary-tree.test.js.map +1 -0
- package/dist/test/chunker-tool-boundary.test.d.ts +12 -0
- package/dist/test/chunker-tool-boundary.test.d.ts.map +1 -0
- package/dist/test/chunker-tool-boundary.test.js +89 -0
- package/dist/test/chunker-tool-boundary.test.js.map +1 -0
- package/dist/test/compression-shape-invariants.test.d.ts +22 -0
- package/dist/test/compression-shape-invariants.test.d.ts.map +1 -0
- package/dist/test/compression-shape-invariants.test.js +312 -0
- package/dist/test/compression-shape-invariants.test.js.map +1 -0
- package/dist/test/image-strip-render-stats.test.d.ts +2 -0
- package/dist/test/image-strip-render-stats.test.d.ts.map +1 -0
- package/dist/test/image-strip-render-stats.test.js +130 -0
- package/dist/test/image-strip-render-stats.test.js.map +1 -0
- package/dist/test/image-stripping.test.d.ts +13 -0
- package/dist/test/image-stripping.test.d.ts.map +1 -0
- package/dist/test/image-stripping.test.js +104 -0
- package/dist/test/image-stripping.test.js.map +1 -0
- package/dist/test/normalize-tool-messages.test.js +61 -1
- package/dist/test/normalize-tool-messages.test.js.map +1 -1
- package/dist/test/render-stats.test.d.ts +2 -0
- package/dist/test/render-stats.test.d.ts.map +1 -0
- package/dist/test/render-stats.test.js +91 -0
- package/dist/test/render-stats.test.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/adaptive/index.ts +48 -0
- package/src/adaptive/kv-cache-sim.ts +0 -0
- package/src/adaptive/kv-control.ts +497 -0
- package/src/adaptive/kv-replay.ts +238 -0
- package/src/adaptive/render-offsets.ts +116 -0
- package/src/adaptive/strategies/kv-stable.ts +123 -0
- package/src/adaptive/summary-tree.ts +252 -0
- package/src/context-log.ts +7 -2
- package/src/index.ts +1 -1
- package/src/message-store.ts +1 -1
- package/src/normalize-tool-messages.ts +61 -0
- package/src/strategies/autobiographical.ts +414 -271
- package/src/types/strategy.ts +40 -1
|
@@ -12,6 +12,12 @@ export { chunkMessage, DEFAULT_CHUNKER_OPTIONS, type ChunkerOptions, type Shard,
|
|
|
12
12
|
export type { FoldingStrategy, FoldingState, FoldingBudget, FoldOp, ChunkView, ChunkId, ChunkRange, SummaryId, } from './folding-strategy.js';
|
|
13
13
|
export { FlatProfileStrategy } from './strategies/flat-profile.js';
|
|
14
14
|
export { OldestFirstStrategy } from './strategies/oldest-first.js';
|
|
15
|
+
export { KvStableStrategy, type KvStableOptions } from './strategies/kv-stable.js';
|
|
16
|
+
export { SummaryTree, nodeTokens, type TreeNode, type LeafNode, type SummaryNode, } from './summary-tree.js';
|
|
17
|
+
export { renderLayout, kvCost, earliestDivergenceIndex, type RenderLayout, type RenderedUnit, type Frontier, } from './render-offsets.js';
|
|
18
|
+
export { placeMarkers, evaluateCacheHit, CacheStore, MAX_CACHE_MARKERS, type CacheMarker, type CacheHit, type CacheStoreOptions, type CacheReadResult, } from './kv-cache-sim.js';
|
|
19
|
+
export { replaySession, type ReplayOptions, type ReplayStep, type ReplayResult, } from './kv-replay.js';
|
|
20
|
+
export { replayControlled, planControlledFrontier, foldDepthCap, PRICE, MAX_FOLD_LEVEL, type ControlOptions, type ControlStep, type ControlResult, type ControlPlanParams, type ControlPlan, } from './kv-control.js';
|
|
15
21
|
export { Picker, OverBudgetError, type PickerInputs, type PickerResult, type PickerChunk, } from './picker.js';
|
|
16
22
|
export { concatBodyGroups, placeholderRecallText } from './render.js';
|
|
17
23
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adaptive/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,EACL,YAAY,EACZ,uBAAuB,EACvB,KAAK,cAAc,EACnB,KAAK,KAAK,EACV,KAAK,cAAc,GACpB,MAAM,cAAc,CAAC;AAGtB,YAAY,EACV,eAAe,EACf,YAAY,EACZ,aAAa,EACb,MAAM,EACN,SAAS,EACT,OAAO,EACP,UAAU,EACV,SAAS,GACV,MAAM,uBAAuB,CAAC;AAG/B,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adaptive/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,EACL,YAAY,EACZ,uBAAuB,EACvB,KAAK,cAAc,EACnB,KAAK,KAAK,EACV,KAAK,cAAc,GACpB,MAAM,cAAc,CAAC;AAGtB,YAAY,EACV,eAAe,EACf,YAAY,EACZ,aAAa,EACb,MAAM,EACN,SAAS,EACT,OAAO,EACP,UAAU,EACV,SAAS,GACV,MAAM,uBAAuB,CAAC;AAG/B,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,EAAE,gBAAgB,EAAE,KAAK,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAGnF,OAAO,EACL,WAAW,EACX,UAAU,EACV,KAAK,QAAQ,EACb,KAAK,QAAQ,EACb,KAAK,WAAW,GACjB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,YAAY,EACZ,MAAM,EACN,uBAAuB,EACvB,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,QAAQ,GACd,MAAM,qBAAqB,CAAC;AAG7B,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,UAAU,EACV,iBAAiB,EACjB,KAAK,WAAW,EAChB,KAAK,QAAQ,EACb,KAAK,iBAAiB,EACtB,KAAK,eAAe,GACrB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,aAAa,EACb,KAAK,aAAa,EAClB,KAAK,UAAU,EACf,KAAK,YAAY,GAClB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,gBAAgB,EAChB,sBAAsB,EACtB,YAAY,EACZ,KAAK,EACL,cAAc,EACd,KAAK,cAAc,EACnB,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,iBAAiB,EACtB,KAAK,WAAW,GACjB,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EACL,MAAM,EACN,eAAe,EACf,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,WAAW,GACjB,MAAM,aAAa,CAAC;AAGrB,OAAO,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -13,6 +13,14 @@ export { chunkMessage, DEFAULT_CHUNKER_OPTIONS, } from './chunker.js';
|
|
|
13
13
|
// Concrete strategies
|
|
14
14
|
export { FlatProfileStrategy } from './strategies/flat-profile.js';
|
|
15
15
|
export { OldestFirstStrategy } from './strategies/oldest-first.js';
|
|
16
|
+
export { KvStableStrategy } from './strategies/kv-stable.js';
|
|
17
|
+
// Summary tree + rendered-unit accounting (shared substrate)
|
|
18
|
+
export { SummaryTree, nodeTokens, } from './summary-tree.js';
|
|
19
|
+
export { renderLayout, kvCost, earliestDivergenceIndex, } from './render-offsets.js';
|
|
20
|
+
// KV-cache simulation + session replay (provider-cache stability measurement)
|
|
21
|
+
export { placeMarkers, evaluateCacheHit, CacheStore, MAX_CACHE_MARKERS, } from './kv-cache-sim.js';
|
|
22
|
+
export { replaySession, } from './kv-replay.js';
|
|
23
|
+
export { replayControlled, planControlledFrontier, foldDepthCap, PRICE, MAX_FOLD_LEVEL, } from './kv-control.js';
|
|
16
24
|
// Picker
|
|
17
25
|
export { Picker, OverBudgetError, } from './picker.js';
|
|
18
26
|
// Render helpers
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/adaptive/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,UAAU;AACV,OAAO,EACL,YAAY,EACZ,uBAAuB,GAIxB,MAAM,cAAc,CAAC;AActB,sBAAsB;AACtB,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/adaptive/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,UAAU;AACV,OAAO,EACL,YAAY,EACZ,uBAAuB,GAIxB,MAAM,cAAc,CAAC;AActB,sBAAsB;AACtB,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,EAAE,gBAAgB,EAAwB,MAAM,2BAA2B,CAAC;AAEnF,6DAA6D;AAC7D,OAAO,EACL,WAAW,EACX,UAAU,GAIX,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,YAAY,EACZ,MAAM,EACN,uBAAuB,GAIxB,MAAM,qBAAqB,CAAC;AAE7B,8EAA8E;AAC9E,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,UAAU,EACV,iBAAiB,GAKlB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,aAAa,GAId,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,gBAAgB,EAChB,sBAAsB,EACtB,YAAY,EACZ,KAAK,EACL,cAAc,GAMf,MAAM,iBAAiB,CAAC;AAEzB,SAAS;AACT,OAAO,EACL,MAAM,EACN,eAAe,GAIhB,MAAM,aAAa,CAAC;AAErB,iBAAiB;AACjB,OAAO,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAEtE,qEAAqE;AACrE,4EAA4E;AAC5E,qEAAqE"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Anthropic-style KV cache simulation over a rendered layout.
|
|
3
|
+
*
|
|
4
|
+
* Real providers don't cache "the longest byte-identical prefix" for free — the
|
|
5
|
+
* Anthropic API honors up to FOUR explicit `cache_control` breakpoints, and on
|
|
6
|
+
* each request reuses the LONGEST previously-marked prefix that is still
|
|
7
|
+
* byte-identical to the new prompt. Everything after that match is recomputed
|
|
8
|
+
* at full input price (`docs/best-fit-frontier-resolution.md` §4).
|
|
9
|
+
*
|
|
10
|
+
* This module models that discrete-breakpoint behavior, so the playground can
|
|
11
|
+
* measure the cache hit a real deployment would actually get — not the
|
|
12
|
+
* idealized ceiling that `render-offsets.kvCost` computes.
|
|
13
|
+
*
|
|
14
|
+
* - `placeMarkers` chooses ≤N breakpoint positions on a layout (the markers we
|
|
15
|
+
* attach to THIS render, written into the cache for FUTURE renders to hit).
|
|
16
|
+
* - `CacheStore` is a PERSISTENT, content-keyed cache: each `write` records the
|
|
17
|
+
* exact bytes of the marked prefixes (as a content hash + write step), and
|
|
18
|
+
* they stay live across turns (subject to a TTL). `read` returns the LONGEST
|
|
19
|
+
* live stored prefix that is byte-identical to the current render. This is
|
|
20
|
+
* the real provider behaviour: a prefix cached many turns ago is still a hit
|
|
21
|
+
* today if it hasn't changed — even if intervening turns diverged and the
|
|
22
|
+
* frontier later reverted. A single-turn "compare to the previous render"
|
|
23
|
+
* check (see `evaluateCacheHit`) cannot model that, so the replay uses the
|
|
24
|
+
* store.
|
|
25
|
+
*
|
|
26
|
+
* A marker's `unitIndex` is the number of leading rendered units it caches
|
|
27
|
+
* (so `unitIndex` ∈ [0, units.length]); `offset` is the cumulative tokens of
|
|
28
|
+
* that prefix. `unitIndex === units.length` caches the whole render (the
|
|
29
|
+
* pure-append breakpoint).
|
|
30
|
+
*
|
|
31
|
+
* Cache identity: two rendered units are byte-identical iff they share
|
|
32
|
+
* (kind, key) — the same identity `render-offsets` uses — because a recall unit
|
|
33
|
+
* keyed by a summary id renders that summary's fixed recall pair, a raw unit
|
|
34
|
+
* keyed by a chunk id renders that chunk verbatim, and head/tail are fixed
|
|
35
|
+
* blocks. So a content hash over the unit (kind,key) sequence is a faithful,
|
|
36
|
+
* deterministic stand-in for the cached bytes.
|
|
37
|
+
*
|
|
38
|
+
* Pure and deterministic (no Date/random; the store tracks a logical step clock
|
|
39
|
+
* advanced by the caller).
|
|
40
|
+
*/
|
|
41
|
+
import { type RenderLayout } from './render-offsets.js';
|
|
42
|
+
/** Maximum cache breakpoints the Anthropic API honors. */
|
|
43
|
+
export declare const MAX_CACHE_MARKERS = 4;
|
|
44
|
+
/** A cache breakpoint: caches the first `unitIndex` rendered units (`offset` tokens). */
|
|
45
|
+
export interface CacheMarker {
|
|
46
|
+
/** Number of leading units cached by this breakpoint (0 … units.length). */
|
|
47
|
+
unitIndex: number;
|
|
48
|
+
/** Cumulative tokens of the cached prefix. */
|
|
49
|
+
offset: number;
|
|
50
|
+
}
|
|
51
|
+
export interface CacheHit {
|
|
52
|
+
/** Tokens served from cache in `next` (longest matching marked prefix). */
|
|
53
|
+
cachedTokens: number;
|
|
54
|
+
/** Tokens recomputed in `next` (everything after the cached prefix). */
|
|
55
|
+
recomputedTokens: number;
|
|
56
|
+
/** cachedTokens / next.totalTokens, in [0, 1]. 0 when next is empty. */
|
|
57
|
+
hitRate: number;
|
|
58
|
+
/** unitIndex of the winning marker (−1 if no marker matched → full recompute). */
|
|
59
|
+
markerUnitIndex: number;
|
|
60
|
+
/** First unit index at which `next` diverges from `prev` (units.length-style;
|
|
61
|
+
* equals the identical-prefix length). */
|
|
62
|
+
divergenceUnits: number;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Choose ≤`count` cache breakpoints for `layout`. The placement mirrors real
|
|
66
|
+
* Anthropic usage and the stable solver's structure:
|
|
67
|
+
*
|
|
68
|
+
* - the END of the render (caches everything → a pure-append next turn is a
|
|
69
|
+
* full hit) is always included;
|
|
70
|
+
* - the stable-prefix seam (`opts.boundaryUnitIndex`, where the frontier was
|
|
71
|
+
* frozen vs F_prev) — the most likely longest-surviving prefix under churn;
|
|
72
|
+
* - the HEAD boundary (the fixed system block) when present;
|
|
73
|
+
* - remaining budget spent on evenly-spaced tail seams.
|
|
74
|
+
*
|
|
75
|
+
* Returns distinct markers sorted by `unitIndex` ascending, capped to
|
|
76
|
+
* `min(count, MAX_CACHE_MARKERS)`.
|
|
77
|
+
*/
|
|
78
|
+
export declare function placeMarkers(layout: RenderLayout, count: number, opts?: {
|
|
79
|
+
boundaryUnitIndex?: number;
|
|
80
|
+
}): CacheMarker[];
|
|
81
|
+
/**
|
|
82
|
+
* SINGLE-TURN cache check: score `next` against the breakpoints we placed on the
|
|
83
|
+
* IMMEDIATELY PRECEDING render `prev`. The cache serves the longest marked
|
|
84
|
+
* prefix of `prev` that is still byte-identical to `next`; everything after is
|
|
85
|
+
* recomputed.
|
|
86
|
+
*
|
|
87
|
+
* This is a one-step primitive and intentionally has NO memory beyond `prev` —
|
|
88
|
+
* it cannot model a prefix cached several turns ago surviving an intervening
|
|
89
|
+
* divergence. For session replay use {@link CacheStore}, which persists entries
|
|
90
|
+
* across turns. `prevMarkers` must be markers produced from `prev`. On a cold
|
|
91
|
+
* start pass an empty `prevMarkers` (no cache → full recompute).
|
|
92
|
+
*/
|
|
93
|
+
export declare function evaluateCacheHit(prev: RenderLayout, prevMarkers: readonly CacheMarker[], next: RenderLayout): CacheHit;
|
|
94
|
+
export interface CacheStoreOptions {
|
|
95
|
+
/** Entries written more than this many steps ago are evicted before a read
|
|
96
|
+
* (models the provider TTL). Default Infinity (keep all — the faithful upper
|
|
97
|
+
* bound on reuse). */
|
|
98
|
+
ttlSteps?: number;
|
|
99
|
+
}
|
|
100
|
+
export interface CacheReadResult {
|
|
101
|
+
/** Tokens served from cache (longest live stored prefix matching `layout`). */
|
|
102
|
+
cachedTokens: number;
|
|
103
|
+
/** Units served from cache (the matched prefix length). */
|
|
104
|
+
cachedUnits: number;
|
|
105
|
+
/** How many steps ago the winning entry was written (0 = this step, −1 = miss). */
|
|
106
|
+
ageSteps: number;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* A persistent provider cache. Holds, per cached prefix length, the set of
|
|
110
|
+
* content hashes ever written at that length and the step each was last
|
|
111
|
+
* (re)written. `read` finds the longest live stored prefix byte-identical to
|
|
112
|
+
* the current render; `write` records the current render's breakpoint prefixes;
|
|
113
|
+
* `tick` advances the logical turn clock used for TTL and age.
|
|
114
|
+
*/
|
|
115
|
+
export declare class CacheStore {
|
|
116
|
+
private readonly ttl;
|
|
117
|
+
/** length → (contentHash → last-written step). */
|
|
118
|
+
private readonly byLength;
|
|
119
|
+
private step;
|
|
120
|
+
constructor(opts?: CacheStoreOptions);
|
|
121
|
+
/** Longest live cached prefix that is byte-identical to `layout`. */
|
|
122
|
+
read(layout: RenderLayout): CacheReadResult;
|
|
123
|
+
/** Record the marked prefixes of `layout` as cache entries at the current step. */
|
|
124
|
+
write(layout: RenderLayout, markers: readonly CacheMarker[]): void;
|
|
125
|
+
/** Advance the logical turn clock (call once per replay step, after write). */
|
|
126
|
+
tick(): void;
|
|
127
|
+
}
|
|
128
|
+
//# sourceMappingURL=kv-cache-sim.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kv-cache-sim.d.ts","sourceRoot":"","sources":["../../../src/adaptive/kv-cache-sim.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAEH,OAAO,EAA2B,KAAK,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEjF,0DAA0D;AAC1D,eAAO,MAAM,iBAAiB,IAAI,CAAC;AAEnC,yFAAyF;AACzF,MAAM,WAAW,WAAW;IAC1B,4EAA4E;IAC5E,SAAS,EAAE,MAAM,CAAC;IAClB,8CAA8C;IAC9C,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,QAAQ;IACvB,2EAA2E;IAC3E,YAAY,EAAE,MAAM,CAAC;IACrB,wEAAwE;IACxE,gBAAgB,EAAE,MAAM,CAAC;IACzB,wEAAwE;IACxE,OAAO,EAAE,MAAM,CAA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C;IACH,CAAC;IAED,+EAA+E;IAC/E,IAAI;QACF,IAAI,CAAC,IAAI,EAAE,CAAC;QACZ,IAAI,IAAI,CAAC,GAAG,KAAK,QAAQ;YAAE,OAAO;QAClC,KAAK,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAC1C,KAAK,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,MAAM,EAAE,CAAC;gBACrC,IAAI,IAAI,CAAC,IAAI,GAAG,OAAO,GAAG,IAAI,CAAC,GAAG;oBAAE,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;YAC1D,CAAC;YACD,IAAI,MAAM,CAAC,IAAI,KAAK,CAAC;gBAAE,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACnD,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* KV-stable context controller (see `docs/kv-stable-context-control.md`).
|
|
3
|
+
*
|
|
4
|
+
* A receding-horizon policy that replaces the `value − λ·KVcost` solve. One
|
|
5
|
+
* objective — minimize real billed recompute — under a constraint hierarchy with
|
|
6
|
+
* a single hard wall (the physical window W) and shaped soft constraints
|
|
7
|
+
* (flat zone, perturbation cap, pins/saliency, operating budget). State (the
|
|
8
|
+
* carried frontier + the persistent cache) makes it a controller, not a
|
|
9
|
+
* per-turn solve.
|
|
10
|
+
*
|
|
11
|
+
* This prototype uses a hysteresis band [LW, HW] as the controllable operating
|
|
12
|
+
* point on the cost↔continuity frontier:
|
|
13
|
+
* - append every turn; if rendered tokens exceed HW, shed down to ≤ LW;
|
|
14
|
+
* otherwise do nothing (pure append → zero perturbation, full cache hit);
|
|
15
|
+
* - shed oldest-first, leveled (L1 groups, then L2, then L3), each chunk
|
|
16
|
+
* capped by the saliency field (log-age + flat zone + pins) — base-k
|
|
17
|
+
* grouping driven by budget, not a fixed clock;
|
|
18
|
+
* - W is the only hard wall; if folding everything to its cap can't fit under
|
|
19
|
+
* W, escalate (don't thrash).
|
|
20
|
+
*
|
|
21
|
+
* Pure and deterministic.
|
|
22
|
+
*/
|
|
23
|
+
import type { ChunkId } from './folding-strategy.js';
|
|
24
|
+
import type { PickerInputs, PickerChunk } from './picker.js';
|
|
25
|
+
import { SummaryTree } from './summary-tree.js';
|
|
26
|
+
/** Provider price multipliers relative to base input tokens (Anthropic-ish). */
|
|
27
|
+
export declare const PRICE: {
|
|
28
|
+
readonly miss: 1;
|
|
29
|
+
readonly cacheRead: 0.1;
|
|
30
|
+
readonly cacheWrite: 1.25;
|
|
31
|
+
};
|
|
32
|
+
/** Deepest fold level the saliency field will ever allow. */
|
|
33
|
+
export declare const MAX_FOLD_LEVEL = 3;
|
|
34
|
+
export interface ControlOptions {
|
|
35
|
+
/** Physical context window — the ONLY hard wall. */
|
|
36
|
+
windowTokens: number;
|
|
37
|
+
/** Operating budget B (< W). High-watermark HW defaults to this. */
|
|
38
|
+
budgetTokens: number;
|
|
39
|
+
/** Shed down to this low-watermark when HW is breached. (HW − LW) sets fold
|
|
40
|
+
* frequency/amplitude. Default 0.8·budget. */
|
|
41
|
+
lowWatermark?: number;
|
|
42
|
+
/** High-watermark; fold is triggered above it. Default = budgetTokens. */
|
|
43
|
+
highWatermark?: number;
|
|
44
|
+
/** Per-turn divergence reach: how far back (tokens from the live end) a fold
|
|
45
|
+
* may normally edit — the structural perturbation cap P, and the primary
|
|
46
|
+
* cost↔continuity knob. Smaller = gentler per-turn KV perturbation (shallow
|
|
47
|
+
* divergence) but less efficient compression → more/again folding. Exceeded
|
|
48
|
+
* ONLY to avoid the hard wall W. Default = budgetTokens (effectively
|
|
49
|
+
* unbounded). */
|
|
50
|
+
reachTokens?: number;
|
|
51
|
+
/** Attended window kept raw + stable (the flat zone). */
|
|
52
|
+
attendedWindowTokens: number;
|
|
53
|
+
/** Base-k summary grouping (matches mergeThreshold). Default 6. */
|
|
54
|
+
mergeThreshold?: number;
|
|
55
|
+
/** Cache breakpoints per turn (1..4). Default 4. */
|
|
56
|
+
markerCount?: number;
|
|
57
|
+
/** Approx replay steps (down-sample). Default 120. */
|
|
58
|
+
targetSteps?: number;
|
|
59
|
+
/** Cache TTL in steps. Default Infinity. */
|
|
60
|
+
cacheTtlSteps?: number;
|
|
61
|
+
}
|
|
62
|
+
export interface ControlStep {
|
|
63
|
+
now: number;
|
|
64
|
+
numChunks: number;
|
|
65
|
+
renderedTokens: number;
|
|
66
|
+
cachedTokens: number;
|
|
67
|
+
recomputedTokens: number;
|
|
68
|
+
/** Billed recompute at provider multipliers. */
|
|
69
|
+
billed: number;
|
|
70
|
+
/** Prefix churn = recompute beyond genuinely new content (the KV perturbation
|
|
71
|
+
* of the attended region). 0 on a pure-append turn. */
|
|
72
|
+
perturbation: number;
|
|
73
|
+
/** True when the controller folded this turn (a perturbation event). */
|
|
74
|
+
folded: boolean;
|
|
75
|
+
/** True when even full folding could not fit under W (terminal). */
|
|
76
|
+
escalated: boolean;
|
|
77
|
+
deepestLevel: number;
|
|
78
|
+
}
|
|
79
|
+
export interface ControlResult {
|
|
80
|
+
steps: ControlStep[];
|
|
81
|
+
totalRecomputed: number;
|
|
82
|
+
totalBilled: number;
|
|
83
|
+
overallHitRate: number;
|
|
84
|
+
/** Worst single-turn perturbation (the continuity ceiling). */
|
|
85
|
+
maxPerturbation: number;
|
|
86
|
+
/** RMS per-turn perturbation (variance-continuity). */
|
|
87
|
+
rmsPerturbation: number;
|
|
88
|
+
/** Count of turns that perturbed the prefix. */
|
|
89
|
+
foldEvents: number;
|
|
90
|
+
/** Count of terminal (over-W) turns. */
|
|
91
|
+
escalations: number;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Per-chunk maximum fold depth from the saliency field: flat zone and pins are
|
|
95
|
+
* raw (0); otherwise log-age banded against the base-k geometry. The "finest
|
|
96
|
+
* requirement wins" — this is the cap the shedder may not exceed.
|
|
97
|
+
*/
|
|
98
|
+
export declare function foldDepthCap(chunk: PickerChunk, now: number, flatZone: ReadonlySet<ChunkId>, flatZoneChunks: number, mergeThreshold: number): number;
|
|
99
|
+
export interface ControlPlanParams {
|
|
100
|
+
/** Carried frontier (F_prev). */
|
|
101
|
+
previous: ReadonlyMap<ChunkId, number>;
|
|
102
|
+
/** Fold (deepen) when rendered tokens exceed this (high-watermark). */
|
|
103
|
+
foldAtTokens: number;
|
|
104
|
+
/** Un-fold (raise toward raw) when rendered tokens are below this
|
|
105
|
+
* (low-watermark). Default = targetTokens. The band (foldAt − expandAt) is a
|
|
106
|
+
* no-op dead zone that prevents fold↔unfold oscillation. Set below foldAt. */
|
|
107
|
+
expandAtTokens?: number;
|
|
108
|
+
/** The attractor both fold and un-fold aim for (soft target). */
|
|
109
|
+
targetTokens: number;
|
|
110
|
+
/** Hard wall W — fold past the reach cap only to keep under this. */
|
|
111
|
+
windowTokens: number;
|
|
112
|
+
/** Per-turn divergence reach (perturbation cap P) — bounds BOTH fold and
|
|
113
|
+
* un-fold divergence. Default = windowTokens. */
|
|
114
|
+
reachTokens?: number;
|
|
115
|
+
/** Chunks forced to raw and never folded (flat zone / head / tail / pinned). */
|
|
116
|
+
rawZone: ReadonlySet<ChunkId>;
|
|
117
|
+
/** Chunks kept at their carried resolution and never touched (locked). */
|
|
118
|
+
frozen?: ReadonlySet<ChunkId>;
|
|
119
|
+
/** Newest source sequence — the age reference for the log-age fold caps. */
|
|
120
|
+
now: number;
|
|
121
|
+
/** Base-k summary grouping. Default 6. */
|
|
122
|
+
mergeThreshold?: number;
|
|
123
|
+
}
|
|
124
|
+
export interface ControlPlan {
|
|
125
|
+
resolutions: Map<ChunkId, number>;
|
|
126
|
+
tokens: number;
|
|
127
|
+
/** A fold (deepen) happened this turn. */
|
|
128
|
+
folded: boolean;
|
|
129
|
+
/** An un-fold (raise toward raw, to use budget headroom) happened this turn. */
|
|
130
|
+
expanded: boolean;
|
|
131
|
+
/** Even full folding under caps could not fit under W (terminal). */
|
|
132
|
+
escalated: boolean;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Plan one turn's frontier under the controller policy — shared by the replay
|
|
136
|
+
* harness (`replayControlled`) and `KvStableStrategy`. Carries F_prev forward,
|
|
137
|
+
* holds the raw zone raw and the frozen set fixed, then drives the rendered size
|
|
138
|
+
* toward `targetTokens` from EITHER side, both bounded by the `reachTokens`
|
|
139
|
+
* divergence cap (KV-continuity preserving in both directions):
|
|
140
|
+
* - over `foldAtTokens` → deepen, oldest-first, under the log-age saliency
|
|
141
|
+
* caps, yielding the reach cap only as needed to stay under the hard wall W;
|
|
142
|
+
* - under `expandAtTokens` → un-fold, youngest-first, to use budget headroom.
|
|
143
|
+
* The [expandAt, foldAt] dead band leaves F_prev untouched (zero perturbation)
|
|
144
|
+
* when already in range. Pure and deterministic.
|
|
145
|
+
*/
|
|
146
|
+
export declare function planControlledFrontier(inputs: PickerInputs, tree: SummaryTree, p: ControlPlanParams): ControlPlan;
|
|
147
|
+
/**
|
|
148
|
+
* Run the controller over a session (the exported PickerInputs snapshot) as
|
|
149
|
+
* growing history, measuring billed recompute and KV perturbation per turn.
|
|
150
|
+
*/
|
|
151
|
+
export declare function replayControlled(fullInputs: PickerInputs, opts: ControlOptions): ControlResult;
|
|
152
|
+
//# sourceMappingURL=kv-control.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kv-control.d.ts","sourceRoot":"","sources":["../../../src/adaptive/kv-control.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AACrD,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE7D,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAIhD,gFAAgF;AAChF,eAAO,MAAM,KAAK;;;;CAA2D,CAAC;AAE9E,6DAA6D;AAC7D,eAAO,MAAM,cA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