@animalabs/context-manager 0.5.5 → 0.5.9
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/kv-control.d.ts +69 -32
- package/dist/src/adaptive/kv-control.d.ts.map +1 -1
- package/dist/src/adaptive/kv-control.js +471 -172
- package/dist/src/adaptive/kv-control.js.map +1 -1
- package/dist/src/adaptive/picker.d.ts +8 -0
- package/dist/src/adaptive/picker.d.ts.map +1 -1
- package/dist/src/adaptive/picker.js.map +1 -1
- package/dist/src/adaptive/strategies/kv-stable.d.ts +12 -4
- package/dist/src/adaptive/strategies/kv-stable.d.ts.map +1 -1
- package/dist/src/adaptive/strategies/kv-stable.js +14 -5
- package/dist/src/adaptive/strategies/kv-stable.js.map +1 -1
- package/dist/src/context-log.d.ts.map +1 -1
- package/dist/src/context-log.js +11 -0
- package/dist/src/context-log.js.map +1 -1
- package/dist/src/context-manager.d.ts +28 -1
- package/dist/src/context-manager.d.ts.map +1 -1
- package/dist/src/context-manager.js +42 -0
- package/dist/src/context-manager.js.map +1 -1
- package/dist/src/index.d.ts +3 -2
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/message-store.d.ts +60 -0
- package/dist/src/message-store.d.ts.map +1 -1
- package/dist/src/message-store.js +165 -10
- package/dist/src/message-store.js.map +1 -1
- package/dist/src/strategies/autobiographical.d.ts +207 -2
- package/dist/src/strategies/autobiographical.d.ts.map +1 -1
- package/dist/src/strategies/autobiographical.js +1283 -124
- package/dist/src/strategies/autobiographical.js.map +1 -1
- package/dist/src/strategies/index.d.ts +1 -1
- package/dist/src/strategies/index.d.ts.map +1 -1
- package/dist/src/strategies/index.js.map +1 -1
- package/dist/src/strategies/knowledge.d.ts +8 -0
- package/dist/src/strategies/knowledge.d.ts.map +1 -1
- package/dist/src/strategies/knowledge.js +23 -1
- package/dist/src/strategies/knowledge.js.map +1 -1
- package/dist/src/types/strategy.d.ts +82 -5
- package/dist/src/types/strategy.d.ts.map +1 -1
- package/dist/src/types/strategy.js.map +1 -1
- package/dist/test/adaptive/doc-plus-chat.test.js +7 -3
- package/dist/test/adaptive/doc-plus-chat.test.js.map +1 -1
- package/dist/test/adaptive/hard-fail.test.js +22 -0
- package/dist/test/adaptive/hard-fail.test.js.map +1 -1
- package/dist/test/adaptive/ingestion.test.js +3 -1
- package/dist/test/adaptive/ingestion.test.js.map +1 -1
- package/dist/test/adaptive/kv-control.test.js +17 -11
- package/dist/test/adaptive/kv-control.test.js.map +1 -1
- package/dist/test/adaptive/single-path-solve.test.d.ts +16 -0
- package/dist/test/adaptive/single-path-solve.test.d.ts.map +1 -0
- package/dist/test/adaptive/single-path-solve.test.js +204 -0
- package/dist/test/adaptive/single-path-solve.test.js.map +1 -0
- package/dist/test/anti-redundancy-budget.test.d.ts +17 -0
- package/dist/test/anti-redundancy-budget.test.d.ts.map +1 -0
- package/dist/test/anti-redundancy-budget.test.js +197 -0
- package/dist/test/anti-redundancy-budget.test.js.map +1 -0
- package/dist/test/chunk-persistence.test.d.ts +29 -0
- package/dist/test/chunk-persistence.test.d.ts.map +1 -0
- package/dist/test/chunk-persistence.test.js +290 -0
- package/dist/test/chunk-persistence.test.js.map +1 -0
- package/dist/test/compression-dedup.test.d.ts +20 -0
- package/dist/test/compression-dedup.test.d.ts.map +1 -0
- package/dist/test/compression-dedup.test.js +92 -0
- package/dist/test/compression-dedup.test.js.map +1 -0
- package/dist/test/compression-shape-invariants.test.js +40 -3
- package/dist/test/compression-shape-invariants.test.js.map +1 -1
- package/dist/test/head-window-order.test.d.ts +19 -0
- package/dist/test/head-window-order.test.d.ts.map +1 -0
- package/dist/test/head-window-order.test.js +159 -0
- package/dist/test/head-window-order.test.js.map +1 -0
- package/dist/test/image-strip-render-stats.test.js +25 -3
- package/dist/test/image-strip-render-stats.test.js.map +1 -1
- package/dist/test/knowledge-tool-boundary.test.d.ts +14 -0
- package/dist/test/knowledge-tool-boundary.test.d.ts.map +1 -0
- package/dist/test/knowledge-tool-boundary.test.js +93 -0
- package/dist/test/knowledge-tool-boundary.test.js.map +1 -0
- package/dist/test/l1-holdback.test.d.ts +15 -0
- package/dist/test/l1-holdback.test.d.ts.map +1 -0
- package/dist/test/l1-holdback.test.js +119 -0
- package/dist/test/l1-holdback.test.js.map +1 -0
- package/dist/test/merge-contiguity.test.d.ts +11 -0
- package/dist/test/merge-contiguity.test.d.ts.map +1 -0
- package/dist/test/merge-contiguity.test.js +74 -0
- package/dist/test/merge-contiguity.test.js.map +1 -0
- package/dist/test/message-store-window.test.d.ts +13 -0
- package/dist/test/message-store-window.test.d.ts.map +1 -0
- package/dist/test/message-store-window.test.js +177 -0
- package/dist/test/message-store-window.test.js.map +1 -0
- package/dist/test/speculation-cap.test.js +4 -0
- package/dist/test/speculation-cap.test.js.map +1 -1
- package/dist/test/summary-log-consistency.test.d.ts +18 -0
- package/dist/test/summary-log-consistency.test.d.ts.map +1 -0
- package/dist/test/summary-log-consistency.test.js +97 -0
- package/dist/test/summary-log-consistency.test.js.map +1 -0
- package/dist/test/tool-pairing-invariant.test.d.ts +15 -0
- package/dist/test/tool-pairing-invariant.test.d.ts.map +1 -0
- package/dist/test/tool-pairing-invariant.test.js +225 -0
- package/dist/test/tool-pairing-invariant.test.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +11 -3
- package/src/adaptive/kv-control.ts +509 -184
- package/src/adaptive/picker.ts +8 -0
- package/src/adaptive/strategies/kv-stable.ts +22 -10
- package/src/context-log.ts +11 -0
- package/src/context-manager.ts +48 -2
- package/src/index.ts +3 -2
- package/src/message-store.ts +202 -9
- package/src/strategies/autobiographical.ts +1343 -148
- package/src/strategies/index.ts +1 -1
- package/src/strategies/knowledge.ts +25 -1
- package/src/types/strategy.ts +89 -5
|
@@ -1,22 +1,31 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* KV-stable context controller
|
|
2
|
+
* KV-stable context controller — the rev 5.0 single-path solve
|
|
3
|
+
* (see `docs/adaptive-resolution-design.md` §13; supersedes the two-path
|
|
4
|
+
* fold-within-reach + emergency design of §12, and the older prose in
|
|
5
|
+
* `docs/kv-stable-context-control.md` where they conflict).
|
|
3
6
|
*
|
|
4
|
-
*
|
|
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.
|
|
7
|
+
* One algorithm per turn, one hard constraint:
|
|
10
8
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
9
|
+
* minimize relevance_loss(F) + perturbation(F, F_prev) [lexicographic]
|
|
10
|
+
* subject to render(F) ≤ W — the only physics
|
|
11
|
+
* perturbation(F, F_prev) ≤ P — trust region; overridable
|
|
12
|
+
*
|
|
13
|
+
* - The IDEAL CUT is built from scratch each turn by pure relevance: fold in
|
|
14
|
+
* salience-then-age priority order, level by level, under the soft shape
|
|
15
|
+
* prior (`foldDepthCap`), past it if W demands, never past the hard
|
|
16
|
+
* protections (flat zone / pins / locked); then pack youngest-first back
|
|
17
|
+
* toward the target. Age/salience-monotone by construction.
|
|
18
|
+
* - PERTURBATION is exact, not modeled: `kvCost` — the tokens the provider
|
|
19
|
+
* will re-read from the earliest layout divergence to the end.
|
|
20
|
+
* - P (`reachTokens`, re-priced) is a TRUST REGION on per-turn divergence,
|
|
21
|
+
* not a spatial eligibility gate. Within it the solver adopts the ideal;
|
|
22
|
+
* beyond it it adopts the ideal's newest changes only (suffix adoption —
|
|
23
|
+
* perturbation is prefix-based, so partial adoption is exactly a suffix
|
|
24
|
+
* cut) and converges over turns, receding-horizon style.
|
|
25
|
+
* - OVERRIDES (same code path, recorded on the plan, logged by the caller):
|
|
26
|
+
* 'bootstrap' (no carried frontier — nothing to preserve), 'infeasible'
|
|
27
|
+
* (nothing within P fits under W), 'quality-gap' (the best plan within P
|
|
28
|
+
* is certifiably bad vs the ideal). There is no emergency path.
|
|
20
29
|
*
|
|
21
30
|
* Pure and deterministic.
|
|
22
31
|
*/
|
|
@@ -29,8 +38,11 @@ export declare const PRICE: {
|
|
|
29
38
|
readonly cacheRead: 0.1;
|
|
30
39
|
readonly cacheWrite: 1.25;
|
|
31
40
|
};
|
|
32
|
-
/**
|
|
33
|
-
|
|
41
|
+
/** Safety ceiling on saliency fold depth. The *effective* cap is
|
|
42
|
+
* `maxAvailableLevel(tree)` (fold only as deep as summaries actually exist),
|
|
43
|
+
* which every caller passes in; this constant is just a high sane upper bound so
|
|
44
|
+
* L4/L5/… are allowed as the merge pipeline builds them — never a pin at L3. */
|
|
45
|
+
export declare const MAX_FOLD_LEVEL = 8;
|
|
34
46
|
export interface ControlOptions {
|
|
35
47
|
/** Physical context window — the ONLY hard wall. */
|
|
36
48
|
windowTokens: number;
|
|
@@ -107,11 +119,21 @@ export interface ControlPlanParams {
|
|
|
107
119
|
expandAtTokens?: number;
|
|
108
120
|
/** The attractor both fold and un-fold aim for (soft target). */
|
|
109
121
|
targetTokens: number;
|
|
110
|
-
/** Hard wall W —
|
|
122
|
+
/** Hard wall W — the only hard constraint (§13.2). */
|
|
111
123
|
windowTokens: number;
|
|
112
|
-
/**
|
|
113
|
-
*
|
|
124
|
+
/** Trust region P on per-turn perturbation, in tokens the provider would
|
|
125
|
+
* re-read (exact `kvCost`, not a spatial gate). Within P the solver adopts
|
|
126
|
+
* the ideal cut outright; beyond it it adopts the ideal's newest changes
|
|
127
|
+
* only (suffix adoption) unless an override fires (§13.4). Default =
|
|
128
|
+
* windowTokens (a rendered layout never exceeds W, so the default trust
|
|
129
|
+
* region never binds). */
|
|
114
130
|
reachTokens?: number;
|
|
131
|
+
/** Quality-gap override threshold (§13.4): a plan within P is rejected — and
|
|
132
|
+
* the ideal adopted, paying the perturbation — when its relevance loss
|
|
133
|
+
* exceeds the ideal's by more than this fraction of the ideal's loss.
|
|
134
|
+
* Also bounds how misallocated a dead-band hold may be before the solver
|
|
135
|
+
* self-heals. Default 0.35. */
|
|
136
|
+
qualityGapRatio?: number;
|
|
115
137
|
/** Chunks forced to raw and never folded (flat zone / head / tail / pinned). */
|
|
116
138
|
rawZone: ReadonlySet<ChunkId>;
|
|
117
139
|
/** Chunks kept at their carried resolution and never touched (locked). */
|
|
@@ -142,20 +164,35 @@ export interface ControlPlan {
|
|
|
142
164
|
folded: boolean;
|
|
143
165
|
/** An un-fold (raise toward raw, to use budget headroom) happened this turn. */
|
|
144
166
|
expanded: boolean;
|
|
145
|
-
/** Even full folding
|
|
167
|
+
/** Even full folding could not fit under W (terminal — production problem). */
|
|
146
168
|
escalated: boolean;
|
|
169
|
+
/** Exact prefix-invalidation cost of this plan vs the carried frontier:
|
|
170
|
+
* tokens the provider will re-read (0 on a hold/pure-append turn). */
|
|
171
|
+
perturbation: number;
|
|
172
|
+
/** Set when the trust region P did not bind normally (§13.4). Callers should
|
|
173
|
+
* log these loudly (`[kv-escalation]`):
|
|
174
|
+
* - 'bootstrap': no carried frontier — pure relevance solve, P not applicable;
|
|
175
|
+
* - 'infeasible': nothing within P fits under W — feasibility beats P;
|
|
176
|
+
* - 'quality-gap': the best plan within P was certifiably bad vs the ideal. */
|
|
177
|
+
override?: 'bootstrap' | 'infeasible' | 'quality-gap';
|
|
147
178
|
}
|
|
148
179
|
/**
|
|
149
|
-
* Plan one turn's frontier
|
|
150
|
-
* harness (`replayControlled`) and `KvStableStrategy`.
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
180
|
+
* Plan one turn's frontier — the rev 5.0 single-path solve (§13.4), shared by
|
|
181
|
+
* the replay harness (`replayControlled`) and `KvStableStrategy`.
|
|
182
|
+
*
|
|
183
|
+
* 1. Build the IDEAL CUT from scratch (pure relevance; W the only wall).
|
|
184
|
+
* 2. No carried frontier → adopt it (bootstrap; P not applicable).
|
|
185
|
+
* 3. Carried frontier in the [expandAt, foldAt] dead band with a small
|
|
186
|
+
* quality gap → hold it (zero perturbation). A LARGE gap falls through:
|
|
187
|
+
* a stuck misallocated profile self-heals instead of fossilizing.
|
|
188
|
+
* 4. Ideal within the trust region P (exact kvCost) → adopt it.
|
|
189
|
+
* 5. Otherwise adopt the ideal's newest changes only (suffix adoption) if
|
|
190
|
+
* that fits W and is not certifiably bad; else adopt the ideal and record
|
|
191
|
+
* the override ('infeasible' / 'quality-gap').
|
|
192
|
+
*
|
|
193
|
+
* There is no emergency path and no spatial eligibility gate: cache continuity
|
|
194
|
+
* is priced (and bounded by P) — never a reason a chunk can't move.
|
|
195
|
+
* Pure and deterministic.
|
|
159
196
|
*/
|
|
160
197
|
export declare function planControlledFrontier(inputs: PickerInputs, tree: SummaryTree, p: ControlPlanParams): ControlPlan;
|
|
161
198
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"kv-control.d.ts","sourceRoot":"","sources":["../../../src/adaptive/kv-control.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"kv-control.d.ts","sourceRoot":"","sources":["../../../src/adaptive/kv-control.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;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;;;iFAGiF;AACjF,eAAO,MAAM,cAAc,IAAI,CAAC;AA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