@animalabs/context-manager 0.5.4 → 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 +84 -33
- package/dist/src/adaptive/kv-control.d.ts.map +1 -1
- package/dist/src/adaptive/kv-control.js +510 -145
- package/dist/src/adaptive/kv-control.js.map +1 -1
- package/dist/src/adaptive/picker.d.ts +21 -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 +67 -7
- 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 +38 -6
- package/dist/src/context-manager.d.ts.map +1 -1
- package/dist/src/context-manager.js +51 -0
- package/dist/src/context-manager.js.map +1 -1
- package/dist/src/index.d.ts +4 -2
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +3 -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/phase-channel.d.ts +13 -0
- package/dist/src/phase-channel.d.ts.map +1 -0
- package/dist/src/phase-channel.js +55 -0
- package/dist/src/phase-channel.js.map +1 -0
- package/dist/src/strategies/autobiographical.d.ts +247 -8
- package/dist/src/strategies/autobiographical.d.ts.map +1 -1
- package/dist/src/strategies/autobiographical.js +1419 -141
- 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/index.d.ts +1 -1
- package/dist/src/types/index.d.ts.map +1 -1
- package/dist/src/types/index.js.map +1 -1
- package/dist/src/types/strategy.d.ts +112 -11
- 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/pin-at-level.test.d.ts +16 -0
- package/dist/test/adaptive/pin-at-level.test.d.ts.map +1 -0
- package/dist/test/adaptive/pin-at-level.test.js +133 -0
- package/dist/test/adaptive/pin-at-level.test.js.map +1 -0
- 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/pins.test.js +40 -0
- package/dist/test/pins.test.js.map +1 -1
- package/dist/test/speculation-cap.test.d.ts +9 -3
- package/dist/test/speculation-cap.test.d.ts.map +1 -1
- package/dist/test/speculation-cap.test.js +179 -3
- 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 +554 -152
- package/src/adaptive/picker.ts +21 -0
- package/src/adaptive/strategies/kv-stable.ts +62 -12
- package/src/context-log.ts +11 -0
- package/src/context-manager.ts +61 -4
- package/src/index.ts +6 -2
- package/src/message-store.ts +202 -9
- package/src/phase-channel.ts +57 -0
- package/src/strategies/autobiographical.ts +1482 -167
- package/src/strategies/index.ts +1 -1
- package/src/strategies/knowledge.ts +25 -1
- package/src/types/index.ts +1 -0
- package/src/types/strategy.ts +120 -7
|
@@ -1,38 +1,50 @@
|
|
|
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
|
*/
|
|
23
32
|
import { SummaryTree } from './summary-tree.js';
|
|
24
|
-
import { renderLayout } from './render-offsets.js';
|
|
33
|
+
import { renderLayout, kvCost } from './render-offsets.js';
|
|
25
34
|
import { placeMarkers, CacheStore } from './kv-cache-sim.js';
|
|
26
35
|
/** Provider price multipliers relative to base input tokens (Anthropic-ish). */
|
|
27
36
|
export const PRICE = { miss: 1.0, cacheRead: 0.1, cacheWrite: 1.25 };
|
|
28
|
-
/**
|
|
29
|
-
|
|
37
|
+
/** Safety ceiling on saliency fold depth. The *effective* cap is
|
|
38
|
+
* `maxAvailableLevel(tree)` (fold only as deep as summaries actually exist),
|
|
39
|
+
* which every caller passes in; this constant is just a high sane upper bound so
|
|
40
|
+
* L4/L5/… are allowed as the merge pipeline builds them — never a pin at L3. */
|
|
41
|
+
export const MAX_FOLD_LEVEL = 8;
|
|
30
42
|
/**
|
|
31
43
|
* Per-chunk maximum fold depth from the saliency field: flat zone and pins are
|
|
32
44
|
* raw (0); otherwise log-age banded against the base-k geometry. The "finest
|
|
33
45
|
* requirement wins" — this is the cap the shedder may not exceed.
|
|
34
46
|
*/
|
|
35
|
-
export function foldDepthCap(chunk, now, flatZone, flatZoneChunks, mergeThreshold) {
|
|
47
|
+
export function foldDepthCap(chunk, now, flatZone, flatZoneChunks, mergeThreshold, maxLevel = MAX_FOLD_LEVEL) {
|
|
36
48
|
if (chunk.pinned || flatZone.has(chunk.id))
|
|
37
49
|
return 0;
|
|
38
50
|
const age = Math.max(0, now - chunk.sequence);
|
|
@@ -40,47 +52,174 @@ export function foldDepthCap(chunk, now, flatZone, flatZoneChunks, mergeThreshol
|
|
|
40
52
|
if (ratio < 1)
|
|
41
53
|
return 0; // just outside the flat zone → still raw
|
|
42
54
|
const band = Math.floor(Math.log(ratio) / Math.log(Math.max(2, mergeThreshold))) + 1;
|
|
43
|
-
return Math.max(0, Math.min(
|
|
55
|
+
return Math.max(0, Math.min(maxLevel, band));
|
|
56
|
+
}
|
|
57
|
+
/** Deepest summary level that actually exists in the tree (0 if none). The
|
|
58
|
+
* fold depth is bounded by this — NOT a hardcoded constant — so the controller
|
|
59
|
+
* uses L4/L5… summaries when production has built them (else it floors at L3 and
|
|
60
|
+
* refuses deeper summaries that exist; cf. the lab L3-floor-with-L4-present). */
|
|
61
|
+
function maxAvailableLevel(tree) {
|
|
62
|
+
let m = 0;
|
|
63
|
+
for (const s of tree.allSummaries())
|
|
64
|
+
if (s.level > m)
|
|
65
|
+
m = s.level;
|
|
66
|
+
return m;
|
|
67
|
+
}
|
|
68
|
+
/** Per-chunk salience: the coefficient on information loss (design §13.3).
|
|
69
|
+
* Defaults to 1. Lower = folds earlier and costs less when folded (code-heavy /
|
|
70
|
+
* tool-output / externalized content). Never overrides hard protections. */
|
|
71
|
+
function salienceOf(c) {
|
|
72
|
+
const s = c.salience;
|
|
73
|
+
if (s === undefined || !Number.isFinite(s))
|
|
74
|
+
return 1;
|
|
75
|
+
return Math.min(1, Math.max(0, s));
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Salience-weighted MISALLOCATION loss of a frontier: Σ salience(c) ·
|
|
79
|
+
* max(0, level(c) − shapeCap(c)) over foldable chunks — the excess fold depth
|
|
80
|
+
* beyond what the relevance shape prior (log-age band) would assign. Zero for
|
|
81
|
+
* any cut that folds nothing deeper than its prior; positive exactly where
|
|
82
|
+
* fidelity is spent in the wrong place. Recency-aware by construction (the
|
|
83
|
+
* cap grows with age), so an INVERTED profile — recent content deep, ancient
|
|
84
|
+
* content fine — scores high even when a naive Σ level would tie it with the
|
|
85
|
+
* correct gradient. Used only to compare cuts of the same tree (quality-gap
|
|
86
|
+
* tests, §13.4).
|
|
87
|
+
*/
|
|
88
|
+
function relevanceLoss(F, ordered, rawZone, caps) {
|
|
89
|
+
let loss = 0;
|
|
90
|
+
for (const c of ordered) {
|
|
91
|
+
if (rawZone.has(c.id))
|
|
92
|
+
continue;
|
|
93
|
+
const lvl = F.get(c.id) ?? 0;
|
|
94
|
+
if (lvl <= 0)
|
|
95
|
+
continue;
|
|
96
|
+
const cap = Math.max(0, caps.get(c.id) ?? 0);
|
|
97
|
+
if (lvl > cap)
|
|
98
|
+
loss += salienceOf(c) * (lvl - cap);
|
|
99
|
+
}
|
|
100
|
+
return loss;
|
|
44
101
|
}
|
|
45
102
|
/**
|
|
46
|
-
*
|
|
47
|
-
* (L1 groups, then L2, then L3) and oldest-first *within the reach window*. A
|
|
48
|
-
* chunk is editable this turn only if the raw tokens newer than it are <
|
|
49
|
-
* `reachTokens` — this bounds the divergence depth (and thus the per-turn KV
|
|
50
|
-
* perturbation) to ≈ reach. Each chunk is bounded by its saliency cap;
|
|
51
|
-
* flat-zone/pinned chunks (cap 0) are never touched.
|
|
103
|
+
* Incremental token accounting for whole-group fold/unfold moves.
|
|
52
104
|
*
|
|
53
|
-
* `
|
|
54
|
-
*
|
|
55
|
-
*
|
|
105
|
+
* `renderLayout` is O(n); calling it per accepted fold makes the ideal-cut
|
|
106
|
+
* construction O(n·folds) ≈ O(n²) — seconds on a 4k-chunk store, paid on
|
|
107
|
+
* EVERY compile under the single-path solve. The ledger tracks rendered units
|
|
108
|
+
* (raw shards / recall pairs) as a multiset keyed exactly like renderLayout's
|
|
109
|
+
* units, so a group fold/unfold is O(group) and `tokens` stays equal to
|
|
110
|
+
* `renderLayout(...).totalTokens` for the same frontier (both share the
|
|
111
|
+
* "missing ancestor → raw" fallback; siblings share one recall unit).
|
|
56
112
|
*/
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
113
|
+
class TokenLedger {
|
|
114
|
+
tree;
|
|
115
|
+
inputs;
|
|
116
|
+
unitTokens = new Map(); // unitKey → tokens
|
|
117
|
+
unitRefs = new Map(); // unitKey → leaf count
|
|
118
|
+
tokens = 0;
|
|
119
|
+
constructor(tree, inputs, ordered, F) {
|
|
120
|
+
this.tree = tree;
|
|
121
|
+
this.inputs = inputs;
|
|
122
|
+
this.tokens = inputs.headTokens + inputs.tailTokens;
|
|
123
|
+
for (const c of ordered) {
|
|
124
|
+
if (inputs.headChunkIds.has(c.id) || inputs.tailChunkIds.has(c.id))
|
|
125
|
+
continue;
|
|
126
|
+
const { key, tokens } = this.unitFor(c, F.get(c.id) ?? 0);
|
|
127
|
+
this.addRef(key, tokens);
|
|
128
|
+
}
|
|
64
129
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
130
|
+
/** Unit identity + cost for a leaf at a level — mirrors renderLayout. */
|
|
131
|
+
unitFor(c, level) {
|
|
132
|
+
const effective = c.pinned ? 0 : level;
|
|
133
|
+
if (effective > 0) {
|
|
134
|
+
const ancestor = this.tree.ancestorAt(c.id, effective);
|
|
135
|
+
if (ancestor)
|
|
136
|
+
return { key: `recall:${ancestor.id}`, tokens: ancestor.recallTokens };
|
|
137
|
+
}
|
|
138
|
+
return { key: `raw:${c.id}`, tokens: c.rawTokens };
|
|
139
|
+
}
|
|
140
|
+
addRef(key, tokens) {
|
|
141
|
+
const refs = this.unitRefs.get(key) ?? 0;
|
|
142
|
+
if (refs === 0) {
|
|
143
|
+
this.unitTokens.set(key, tokens);
|
|
144
|
+
this.tokens += tokens;
|
|
145
|
+
}
|
|
146
|
+
this.unitRefs.set(key, refs + 1);
|
|
147
|
+
}
|
|
148
|
+
dropRef(key) {
|
|
149
|
+
const refs = this.unitRefs.get(key) ?? 0;
|
|
150
|
+
if (refs <= 1) {
|
|
151
|
+
this.unitRefs.delete(key);
|
|
152
|
+
this.tokens -= this.unitTokens.get(key) ?? 0;
|
|
153
|
+
this.unitTokens.delete(key);
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
this.unitRefs.set(key, refs - 1);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/** Move one leaf from `fromLevel` to `toLevel`. Callers move whole groups
|
|
160
|
+
* (every leaf of the target node), which keeps units exact. */
|
|
161
|
+
move(c, byId, fromLevel, toLevel) {
|
|
162
|
+
if (this.inputs.headChunkIds.has(c.id) || this.inputs.tailChunkIds.has(c.id))
|
|
163
|
+
return;
|
|
164
|
+
void byId;
|
|
165
|
+
this.dropRef(this.unitFor(c, fromLevel).key);
|
|
166
|
+
const next = this.unitFor(c, toLevel);
|
|
167
|
+
this.addRef(next.key, next.tokens);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Build the IDEAL CUT from scratch — pure relevance, no P anywhere (§13.4).
|
|
172
|
+
*
|
|
173
|
+
* Fold order is (salience ascending, then oldest-first), level by level, so the
|
|
174
|
+
* profile is salience/age-monotone by construction: with uniform salience it
|
|
175
|
+
* reduces to the classic oldest-first shed. Two phases against the shape prior:
|
|
176
|
+
* A. fold under the soft `caps` (log-age shape + pin caps) until ≤ target;
|
|
177
|
+
* B. if still over W, keep folding past the shape caps (never past the
|
|
178
|
+
* hard-protected −1 sentinel or a pin cap) — the prior is a relevance
|
|
179
|
+
* shaper, never a feasibility wall;
|
|
180
|
+
* C. pack: un-fold youngest-first back toward `target` (accept-if-fit), so
|
|
181
|
+
* headroom is spent on the most recent foldable content.
|
|
182
|
+
* Ends projected to a valid group-consistent tree cut.
|
|
183
|
+
*/
|
|
184
|
+
function relevanceCut(inputs, tree, ordered, base, caps, pinCaps, rawZone, immovable, frozen, fixedLevels, target, windowTokens, maxFoldLevel) {
|
|
185
|
+
const F = new Map(base);
|
|
186
|
+
const byId = new Map(ordered.map((c) => [c.id, c]));
|
|
187
|
+
// Fold priority: cheapest information first — ascending salience, then oldest.
|
|
188
|
+
const priority = [...ordered].sort((a, b) => {
|
|
189
|
+
const sa = salienceOf(a);
|
|
190
|
+
const sb = salienceOf(b);
|
|
191
|
+
if (sa !== sb)
|
|
192
|
+
return sa - sb;
|
|
193
|
+
return a.sequence - b.sequence;
|
|
194
|
+
});
|
|
195
|
+
// Incremental accounting: O(group) per move instead of O(n) renderLayout.
|
|
196
|
+
const _tl = typeof process !== 'undefined' && process.env?.KV_TIMING ? Date.now() : 0;
|
|
197
|
+
const ledger = new TokenLedger(tree, inputs, ordered, F);
|
|
198
|
+
if (_tl)
|
|
199
|
+
console.error(`[kv-timing] ledger-init ${Date.now() - _tl}ms tokens=${ledger.tokens}`);
|
|
200
|
+
const foldPass = (ignoreShapeCaps, stopAt) => {
|
|
201
|
+
for (let level = 1; level <= maxFoldLevel; level++) {
|
|
202
|
+
for (const c of priority) {
|
|
203
|
+
if (ledger.tokens <= stopAt)
|
|
204
|
+
return true;
|
|
205
|
+
if ((F.get(c.id) ?? 0) >= level)
|
|
72
206
|
continue;
|
|
73
207
|
const ancestor = tree.ancestorAt(c.id, level);
|
|
74
208
|
if (!ancestor)
|
|
75
209
|
continue; // summary not produced yet → can't fold here
|
|
76
|
-
// Group-
|
|
77
|
-
//
|
|
78
|
-
//
|
|
79
|
-
//
|
|
80
|
-
// group-consistent and the picker convergent.
|
|
210
|
+
// Group-atomicity: fold the WHOLE group to `level` only if every
|
|
211
|
+
// covered leaf permits it. Hard protections (−1 sentinel: flat zone /
|
|
212
|
+
// pins / locked) always block; pin-max-level always blocks past its
|
|
213
|
+
// bound; the soft shape cap blocks only in phase A.
|
|
81
214
|
let eligible = true;
|
|
82
215
|
for (const leafId of ancestor.leafChunkIds) {
|
|
83
|
-
|
|
216
|
+
const cap = caps.get(leafId) ?? 0;
|
|
217
|
+
const pinCap = pinCaps.get(leafId);
|
|
218
|
+
if (pinCap !== undefined && level > pinCap) {
|
|
219
|
+
eligible = false;
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
if (ignoreShapeCaps ? cap < 0 : cap < level) {
|
|
84
223
|
eligible = false;
|
|
85
224
|
break;
|
|
86
225
|
}
|
|
@@ -88,80 +227,186 @@ function shedToTarget(frontier, inputs, tree, ordered, caps, target, reachTokens
|
|
|
88
227
|
if (!eligible)
|
|
89
228
|
continue;
|
|
90
229
|
for (const leafId of ancestor.leafChunkIds) {
|
|
91
|
-
|
|
92
|
-
|
|
230
|
+
const leaf = byId.get(leafId);
|
|
231
|
+
const from = F.get(leafId) ?? 0;
|
|
232
|
+
if (from < level) {
|
|
233
|
+
if (leaf)
|
|
234
|
+
ledger.move(leaf, byId, from, level);
|
|
235
|
+
F.set(leafId, level);
|
|
236
|
+
}
|
|
93
237
|
}
|
|
94
|
-
if (
|
|
238
|
+
if (ledger.tokens <= stopAt)
|
|
95
239
|
return true;
|
|
96
240
|
}
|
|
97
241
|
}
|
|
98
242
|
return false;
|
|
99
243
|
};
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
244
|
+
// Phase A: fold under the shape prior toward the target.
|
|
245
|
+
if (ledger.tokens > target)
|
|
246
|
+
foldPass(false, target);
|
|
247
|
+
// Phase B: the prior yields to W (only hard protections stand).
|
|
248
|
+
if (ledger.tokens > windowTokens)
|
|
249
|
+
foldPass(true, target);
|
|
250
|
+
// Phase C: pack youngest-first back toward the target (skip if infeasible).
|
|
251
|
+
if (ledger.tokens <= windowTokens && ledger.tokens < target) {
|
|
252
|
+
for (let level = maxFoldLevel; level >= 1 && ledger.tokens < target; level--) {
|
|
253
|
+
for (let oi = ordered.length - 1; oi >= 0; oi--) {
|
|
254
|
+
if (ledger.tokens >= target)
|
|
255
|
+
break;
|
|
256
|
+
const c = ordered[oi]; // youngest-first
|
|
257
|
+
if ((F.get(c.id) ?? 0) !== level)
|
|
258
|
+
continue;
|
|
259
|
+
if (rawZone.has(c.id) || immovable.has(c.id))
|
|
260
|
+
continue;
|
|
261
|
+
const node = tree.ancestorAt(c.id, level);
|
|
262
|
+
if (!node)
|
|
263
|
+
continue;
|
|
264
|
+
let eligible = true;
|
|
265
|
+
for (const leafId of node.leafChunkIds) {
|
|
266
|
+
if ((F.get(leafId) ?? 0) !== level || rawZone.has(leafId) || immovable.has(leafId)) {
|
|
267
|
+
eligible = false;
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
if (!eligible)
|
|
272
|
+
continue;
|
|
273
|
+
const before = ledger.tokens;
|
|
274
|
+
for (const leafId of node.leafChunkIds) {
|
|
275
|
+
const leaf = byId.get(leafId);
|
|
276
|
+
if (leaf)
|
|
277
|
+
ledger.move(leaf, byId, level, level - 1);
|
|
278
|
+
F.set(leafId, level - 1);
|
|
279
|
+
}
|
|
280
|
+
// Accept an un-fold that lands PAST the target when it still gets
|
|
281
|
+
// CLOSER to it (and stays under W). Merge groups are coarse quanta
|
|
282
|
+
// (8-15k on production trees); accept-only-if-under-target strands
|
|
283
|
+
// real headroom un-spent whenever every remaining quantum overshoots
|
|
284
|
+
// (mythos 2026-07-12: 134k rendered of a 183.6k hard budget). The
|
|
285
|
+
// target stays the attractor — W stays the only wall (§13.2).
|
|
286
|
+
const nt = ledger.tokens;
|
|
287
|
+
const accept = nt <= windowTokens && Math.abs(nt - target) < Math.abs(before - target);
|
|
288
|
+
if (!accept) {
|
|
289
|
+
// moves away from the target (or breaches W) → revert
|
|
290
|
+
for (const leafId of node.leafChunkIds) {
|
|
291
|
+
const leaf = byId.get(leafId);
|
|
292
|
+
if (leaf)
|
|
293
|
+
ledger.move(leaf, byId, level - 1, level);
|
|
294
|
+
F.set(leafId, level);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
106
299
|
}
|
|
107
|
-
|
|
300
|
+
const _tp = typeof process !== 'undefined' && process.env?.KV_TIMING ? Date.now() : 0;
|
|
301
|
+
projectToValidCut(F, tree, ordered, frozen, fixedLevels);
|
|
302
|
+
if (_tp)
|
|
303
|
+
console.error(`[kv-timing] cut-projection ${Date.now() - _tp}ms`);
|
|
304
|
+
// One authoritative render at the end (the ledger tracks it exactly, but the
|
|
305
|
+
// projection may have moved leaves — recompute once, not per move).
|
|
306
|
+
const tokens = renderLayout(inputs, tree, F).totalTokens;
|
|
307
|
+
return { F, tokens };
|
|
108
308
|
}
|
|
109
309
|
/**
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
* (highest value, shallowest divergence). Stops before exceeding `target`.
|
|
117
|
-
* rawZone/frozen chunks are never touched. Returns final tokens.
|
|
310
|
+
* Partial adoption of the ideal under the trust region (§13.4): keep `prev`
|
|
311
|
+
* before a boundary sequence, take `ideal` at and after it. Perturbation is
|
|
312
|
+
* prefix-based, so the cheapest partial plans are exactly the suffix cuts;
|
|
313
|
+
* binary-search the oldest boundary whose perturbation stays ≤ P (perturbation
|
|
314
|
+
* is monotone non-increasing as the boundary moves newer). Each candidate is
|
|
315
|
+
* projected to a valid cut before costing (mixing two cuts can bisect a group).
|
|
118
316
|
*/
|
|
119
|
-
function
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
acc += ordered[i].rawTokens;
|
|
317
|
+
function suffixAdopt(inputs, tree, ordered, prev, prevLayout, ideal, frozen, fixedLevels, P) {
|
|
318
|
+
const changedSeqs = [];
|
|
319
|
+
for (const c of ordered) {
|
|
320
|
+
if ((prev.get(c.id) ?? 0) !== (ideal.get(c.id) ?? 0))
|
|
321
|
+
changedSeqs.push(c.sequence);
|
|
125
322
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
for (
|
|
129
|
-
if (
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
323
|
+
const build = (boundary) => {
|
|
324
|
+
const F = new Map(prev);
|
|
325
|
+
for (const c of ordered) {
|
|
326
|
+
if (c.sequence >= boundary)
|
|
327
|
+
F.set(c.id, ideal.get(c.id) ?? 0);
|
|
328
|
+
}
|
|
329
|
+
projectToValidCut(F, tree, ordered, frozen, fixedLevels);
|
|
330
|
+
const layout = renderLayout(inputs, tree, F);
|
|
331
|
+
return { F, layout, pert: kvCost(prevLayout, layout) };
|
|
332
|
+
};
|
|
333
|
+
if (changedSeqs.length === 0) {
|
|
334
|
+
const layout = renderLayout(inputs, tree, new Map(prev));
|
|
335
|
+
return { F: new Map(prev), tokens: layout.totalTokens, perturbation: 0 };
|
|
336
|
+
}
|
|
337
|
+
// Binary search: smallest index (oldest boundary) with perturbation ≤ P.
|
|
338
|
+
let lo = 0;
|
|
339
|
+
let hi = changedSeqs.length; // hi = adopt nothing (pert 0, always feasible)
|
|
340
|
+
let best = build(Number.MAX_SAFE_INTEGER);
|
|
341
|
+
let bestIdx = hi;
|
|
342
|
+
while (lo < hi) {
|
|
343
|
+
const mid = (lo + hi) >> 1;
|
|
344
|
+
const cand = build(changedSeqs[mid]);
|
|
345
|
+
if (cand.pert <= P) {
|
|
346
|
+
best = cand;
|
|
347
|
+
bestIdx = mid;
|
|
348
|
+
hi = mid;
|
|
349
|
+
}
|
|
350
|
+
else {
|
|
351
|
+
lo = mid + 1;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
void bestIdx;
|
|
355
|
+
return { F: best.F, tokens: best.layout.totalTokens, perturbation: best.pert };
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Project a per-chunk frontier onto the nearest VALID TREE CUT (group-consistent).
|
|
359
|
+
*
|
|
360
|
+
* A leaf folded to level k renders as part of its L_k ancestor node, and that
|
|
361
|
+
* node is atomic over its whole covered range — so EVERY leaf under it must share
|
|
362
|
+
* level k, or the node is unrenderable. The picker walks group-atomic raise/lower
|
|
363
|
+
* ops, so a frontier that folds only SOME leaves under a node is an unreachable
|
|
364
|
+
* target: it raises the group (over-folding a sibling), lowers it (under-folding
|
|
365
|
+
* another), forever — non-convergence (the wedge this fixes).
|
|
366
|
+
*
|
|
367
|
+
* Frontiers seeded per-chunk from the carried `previous` frontier can violate
|
|
368
|
+
* this: as a summary group accretes new raw leaves over turns while older leaves
|
|
369
|
+
* stay folded, its leaves end up at mixed levels. shed/expand only re-consolidate
|
|
370
|
+
* groups they actively move; a mixed group sitting in the budget dead-band passes
|
|
371
|
+
* through untouched. This pass repairs it.
|
|
372
|
+
*
|
|
373
|
+
* Shallowest-first: where a group disagrees, UN-fold the folded leaves one level
|
|
374
|
+
* toward raw (preserving continuity), cascading until every folded leaf's group
|
|
375
|
+
* is unanimous. Monotone (only lowers) → terminates. Any budget breach from the
|
|
376
|
+
* extra raw is re-folded whole-group by the shed pass that runs afterward.
|
|
377
|
+
*
|
|
378
|
+
* Pinned (`fixedLevels`) and frozen (locked) leaves are already made
|
|
379
|
+
* group-consistent by the caller (pin-at-level fixes a whole node's leaves), so
|
|
380
|
+
* they are held here rather than lowered.
|
|
381
|
+
*/
|
|
382
|
+
function projectToValidCut(F, tree, ordered, frozen, fixedLevels) {
|
|
383
|
+
const maxPasses = (ordered.length + 1) * (maxAvailableLevel(tree) + 2);
|
|
384
|
+
let changed = true;
|
|
385
|
+
let pass = 0;
|
|
386
|
+
while (changed && pass++ < maxPasses) {
|
|
387
|
+
changed = false;
|
|
388
|
+
for (const c of ordered) {
|
|
389
|
+
const lvl = F.get(c.id) ?? 0;
|
|
390
|
+
if (lvl <= 0)
|
|
140
391
|
continue;
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
(
|
|
148
|
-
|
|
149
|
-
|
|
392
|
+
if (frozen.has(c.id) || fixedLevels.has(c.id))
|
|
393
|
+
continue; // held; consistent by construction
|
|
394
|
+
const node = tree.ancestorAt(c.id, lvl);
|
|
395
|
+
let unanimous = node != null;
|
|
396
|
+
if (node) {
|
|
397
|
+
for (const leafId of node.leafChunkIds) {
|
|
398
|
+
if ((F.get(leafId) ?? 0) !== lvl) {
|
|
399
|
+
unanimous = false;
|
|
400
|
+
break;
|
|
401
|
+
}
|
|
150
402
|
}
|
|
151
403
|
}
|
|
152
|
-
if (!
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
const nt = renderLayout(inputs, tree, frontier).totalTokens;
|
|
157
|
-
if (nt <= target)
|
|
158
|
-
tokens = nt; // accept the un-fold
|
|
159
|
-
else
|
|
160
|
-
for (const leafId of node.leafChunkIds)
|
|
161
|
-
frontier.set(leafId, level); // overshoot → revert
|
|
404
|
+
if (!unanimous) {
|
|
405
|
+
F.set(c.id, lvl - 1);
|
|
406
|
+
changed = true;
|
|
407
|
+
} // un-fold one level (shallowest-first)
|
|
162
408
|
}
|
|
163
409
|
}
|
|
164
|
-
return tokens;
|
|
165
410
|
}
|
|
166
411
|
/** Earliest source sequence whose resolution changed `prev → next`. */
|
|
167
412
|
function earliestChangedSequence(prev, next, ordered) {
|
|
@@ -199,56 +444,176 @@ function rawTailSet(ordered, tailTokens) {
|
|
|
199
444
|
return s;
|
|
200
445
|
}
|
|
201
446
|
const EMPTY_SET = new Set();
|
|
447
|
+
const EMPTY_LEVEL_MAP = new Map();
|
|
202
448
|
/**
|
|
203
|
-
* Plan one turn's frontier
|
|
204
|
-
* harness (`replayControlled`) and `KvStableStrategy`.
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
449
|
+
* Plan one turn's frontier — the rev 5.0 single-path solve (§13.4), shared by
|
|
450
|
+
* the replay harness (`replayControlled`) and `KvStableStrategy`.
|
|
451
|
+
*
|
|
452
|
+
* 1. Build the IDEAL CUT from scratch (pure relevance; W the only wall).
|
|
453
|
+
* 2. No carried frontier → adopt it (bootstrap; P not applicable).
|
|
454
|
+
* 3. Carried frontier in the [expandAt, foldAt] dead band with a small
|
|
455
|
+
* quality gap → hold it (zero perturbation). A LARGE gap falls through:
|
|
456
|
+
* a stuck misallocated profile self-heals instead of fossilizing.
|
|
457
|
+
* 4. Ideal within the trust region P (exact kvCost) → adopt it.
|
|
458
|
+
* 5. Otherwise adopt the ideal's newest changes only (suffix adoption) if
|
|
459
|
+
* that fits W and is not certifiably bad; else adopt the ideal and record
|
|
460
|
+
* the override ('infeasible' / 'quality-gap').
|
|
461
|
+
*
|
|
462
|
+
* There is no emergency path and no spatial eligibility gate: cache continuity
|
|
463
|
+
* is priced (and bounded by P) — never a reason a chunk can't move.
|
|
464
|
+
* Pure and deterministic.
|
|
213
465
|
*/
|
|
214
466
|
export function planControlledFrontier(inputs, tree, p) {
|
|
215
467
|
const ordered = [...inputs.chunks].sort((a, b) => a.sequence - b.sequence);
|
|
216
468
|
const frozen = p.frozen ?? EMPTY_SET;
|
|
469
|
+
const fixedLevels = p.fixedLevels ?? EMPTY_LEVEL_MAP;
|
|
470
|
+
const pinCaps = p.pinCaps ?? EMPTY_LEVEL_MAP;
|
|
217
471
|
const mergeThreshold = p.mergeThreshold ?? 6;
|
|
218
|
-
const
|
|
472
|
+
const P = p.reachTokens ?? p.windowTokens;
|
|
219
473
|
const expandAt = p.expandAtTokens ?? p.targetTokens;
|
|
220
|
-
const
|
|
474
|
+
const gapRatio = p.qualityGapRatio ?? 0.35;
|
|
475
|
+
// Fold as deep as the tree actually goes (L4/L5… when produced), not a constant.
|
|
476
|
+
const maxFoldLevel = maxAvailableLevel(tree);
|
|
477
|
+
// Immovable = never folded OR un-folded by the solve: the locked (frozen)
|
|
478
|
+
// set plus V2 pin-at-level-k chunks (held at their fixed level).
|
|
479
|
+
const immovable = new Set(frozen);
|
|
480
|
+
for (const id of fixedLevels.keys())
|
|
481
|
+
immovable.add(id);
|
|
482
|
+
// Base = the non-negotiable part of every cut: raw zone raw, pins at their
|
|
483
|
+
// level, frozen at their carried resolution. Everything else starts raw here;
|
|
484
|
+
// the carried frontier and the ideal cut both build on this base.
|
|
485
|
+
const base = new Map();
|
|
221
486
|
for (const c of ordered) {
|
|
222
|
-
if (p.rawZone.has(c.id))
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
487
|
+
if (p.rawZone.has(c.id)) {
|
|
488
|
+
base.set(c.id, 0);
|
|
489
|
+
}
|
|
490
|
+
else if (fixedLevels.has(c.id)) {
|
|
491
|
+
// Pin-at-k: fix to exactly k, clamped to the deepest produced level for
|
|
492
|
+
// this chunk (can't render at a level whose summary doesn't exist yet).
|
|
493
|
+
const k = Math.max(0, fixedLevels.get(c.id));
|
|
494
|
+
base.set(c.id, k === 0 ? 0 : Math.min(k, tree.maxLevel(c.id)));
|
|
495
|
+
}
|
|
496
|
+
else if (frozen.has(c.id)) {
|
|
497
|
+
base.set(c.id, p.previous.get(c.id) ?? c.currentResolution);
|
|
498
|
+
}
|
|
499
|
+
else {
|
|
500
|
+
base.set(c.id, 0);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
// Carried frontier (F_prev with the base enforced): what is actually
|
|
504
|
+
// rendered right now, and the reference for perturbation.
|
|
505
|
+
const carried = new Map(base);
|
|
506
|
+
let carriedNonEmpty = false;
|
|
507
|
+
for (const c of ordered) {
|
|
508
|
+
if (p.rawZone.has(c.id) || fixedLevels.has(c.id) || frozen.has(c.id))
|
|
509
|
+
continue;
|
|
510
|
+
// Enforce a pin-max-level immediately: un-fold anything carried deeper than
|
|
511
|
+
// its cap down to the cap (shallower summaries — its ancestors — are
|
|
512
|
+
// guaranteed to exist). The intended divergence cost of tightening a pin.
|
|
513
|
+
let lvl = p.previous.get(c.id) ?? 0;
|
|
514
|
+
const cap = pinCaps.get(c.id);
|
|
515
|
+
if (cap !== undefined && lvl > cap)
|
|
516
|
+
lvl = Math.max(0, cap);
|
|
517
|
+
carried.set(c.id, lvl);
|
|
228
518
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
519
|
+
for (const v of p.previous.values()) {
|
|
520
|
+
if (v !== 0) {
|
|
521
|
+
carriedNonEmpty = true;
|
|
522
|
+
break;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
projectToValidCut(carried, tree, ordered, frozen, fixedLevels);
|
|
526
|
+
const carriedLayout = renderLayout(inputs, tree, carried);
|
|
527
|
+
const carriedTokens = carriedLayout.totalTokens;
|
|
528
|
+
// Shape prior: soft per-chunk depth caps (log-age band + pin caps), with the
|
|
529
|
+
// −1 sentinel marking the hard-protected set (never folded by anything).
|
|
530
|
+
const flatZoneChunks = p.rawZone.size;
|
|
531
|
+
const caps = new Map();
|
|
532
|
+
for (const c of ordered) {
|
|
533
|
+
let cap;
|
|
534
|
+
if (p.rawZone.has(c.id) || immovable.has(c.id)) {
|
|
535
|
+
cap = -1; // hard-protected sentinel: never folded, W notwithstanding
|
|
536
|
+
}
|
|
537
|
+
else {
|
|
538
|
+
cap = foldDepthCap(c, p.now, p.rawZone, flatZoneChunks, mergeThreshold, maxFoldLevel);
|
|
539
|
+
const pinCap = pinCaps.get(c.id);
|
|
540
|
+
if (pinCap !== undefined)
|
|
541
|
+
cap = Math.min(cap, Math.max(0, pinCap));
|
|
542
|
+
}
|
|
543
|
+
caps.set(c.id, cap);
|
|
544
|
+
}
|
|
545
|
+
// 1. The ideal cut — pure relevance, no P anywhere.
|
|
546
|
+
const _t0 = typeof process !== 'undefined' && process.env?.KV_TIMING ? Date.now() : 0;
|
|
547
|
+
const ideal = relevanceCut(inputs, tree, ordered, base, caps, pinCaps, p.rawZone, immovable, frozen, fixedLevels, p.targetTokens, p.windowTokens, maxFoldLevel);
|
|
548
|
+
if (_t0)
|
|
549
|
+
console.error(`[kv-timing] relevanceCut ${Date.now() - _t0}ms idealTokens=${ideal.tokens}`);
|
|
550
|
+
const idealLayout = renderLayout(inputs, tree, ideal.F);
|
|
551
|
+
const idealLoss = relevanceLoss(ideal.F, ordered, p.rawZone, caps);
|
|
552
|
+
const gapCeiling = gapRatio * Math.max(1, idealLoss);
|
|
553
|
+
const flags = (F) => {
|
|
554
|
+
let folded = false;
|
|
555
|
+
let expanded = false;
|
|
237
556
|
for (const c of ordered) {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
557
|
+
const prev = carried.get(c.id) ?? 0;
|
|
558
|
+
const next = F.get(c.id) ?? 0;
|
|
559
|
+
if (next > prev)
|
|
560
|
+
folded = true;
|
|
561
|
+
else if (next < prev)
|
|
562
|
+
expanded = true;
|
|
563
|
+
if (folded && expanded)
|
|
564
|
+
break;
|
|
241
565
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
566
|
+
return { folded, expanded };
|
|
567
|
+
};
|
|
568
|
+
const adoptIdeal = (override) => ({
|
|
569
|
+
resolutions: ideal.F,
|
|
570
|
+
tokens: ideal.tokens,
|
|
571
|
+
...flags(ideal.F),
|
|
572
|
+
escalated: ideal.tokens > p.windowTokens,
|
|
573
|
+
perturbation: kvCost(carriedLayout, idealLayout),
|
|
574
|
+
...(override ? { override } : {}),
|
|
575
|
+
});
|
|
576
|
+
// 2. Bootstrap: nothing carried → nothing to preserve; pure relevance solve.
|
|
577
|
+
if (!carriedNonEmpty && p.previous.size === 0)
|
|
578
|
+
return adoptIdeal('bootstrap');
|
|
579
|
+
// 3. Dead band with self-heal: hold the carried frontier only when it is
|
|
580
|
+
// feasible, in band, AND not certifiably misallocated.
|
|
581
|
+
const carriedLoss = relevanceLoss(carried, ordered, p.rawZone, caps);
|
|
582
|
+
const inBand = carriedTokens <= p.foldAtTokens && carriedTokens >= expandAt;
|
|
583
|
+
if (inBand && carriedTokens <= p.windowTokens && carriedLoss - idealLoss <= gapCeiling) {
|
|
584
|
+
return {
|
|
585
|
+
resolutions: carried,
|
|
586
|
+
tokens: carriedTokens,
|
|
587
|
+
...flags(carried), // vs itself → both false unless projection moved it
|
|
588
|
+
escalated: false,
|
|
589
|
+
perturbation: 0,
|
|
590
|
+
};
|
|
246
591
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
592
|
+
// 4. Within the trust region → adopt the ideal outright.
|
|
593
|
+
const idealPert = kvCost(carriedLayout, idealLayout);
|
|
594
|
+
if (idealPert <= P)
|
|
595
|
+
return adoptIdeal();
|
|
596
|
+
// 5. Suffix adoption: the ideal's newest changes only, within P.
|
|
597
|
+
const partial = suffixAdopt(inputs, tree, ordered, carried, carriedLayout, ideal.F, frozen, fixedLevels, P);
|
|
598
|
+
const partialLoss = relevanceLoss(partial.F, ordered, p.rawZone, caps);
|
|
599
|
+
// Progress guard: when a shed is REQUIRED (carried over foldAt), a partial
|
|
600
|
+
// plan that neither reaches the band nor moves at all is not acceptable —
|
|
601
|
+
// an under-folded profile scores zero misallocation loss, so the quality
|
|
602
|
+
// gap alone cannot reject "do nothing forever". (P below the physical floor
|
|
603
|
+
// — the tail that any fold must invalidate — lands here and overrides.)
|
|
604
|
+
const mustShed = carriedTokens > p.foldAtTokens;
|
|
605
|
+
const madeProgress = !mustShed || partial.tokens <= p.foldAtTokens || partial.perturbation > 0;
|
|
606
|
+
if (partial.tokens <= p.windowTokens && partialLoss - idealLoss <= gapCeiling && madeProgress) {
|
|
607
|
+
return {
|
|
608
|
+
resolutions: partial.F,
|
|
609
|
+
tokens: partial.tokens,
|
|
610
|
+
...flags(partial.F),
|
|
611
|
+
escalated: false,
|
|
612
|
+
perturbation: partial.perturbation,
|
|
613
|
+
};
|
|
250
614
|
}
|
|
251
|
-
|
|
615
|
+
// Trust region priced out: feasibility or quality demands the ideal.
|
|
616
|
+
return adoptIdeal(partial.tokens > p.windowTokens ? 'infeasible' : 'quality-gap');
|
|
252
617
|
}
|
|
253
618
|
/**
|
|
254
619
|
* Run the controller over a session (the exported PickerInputs snapshot) as
|