@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.
Files changed (111) hide show
  1. package/dist/src/adaptive/kv-control.d.ts +69 -32
  2. package/dist/src/adaptive/kv-control.d.ts.map +1 -1
  3. package/dist/src/adaptive/kv-control.js +471 -172
  4. package/dist/src/adaptive/kv-control.js.map +1 -1
  5. package/dist/src/adaptive/picker.d.ts +8 -0
  6. package/dist/src/adaptive/picker.d.ts.map +1 -1
  7. package/dist/src/adaptive/picker.js.map +1 -1
  8. package/dist/src/adaptive/strategies/kv-stable.d.ts +12 -4
  9. package/dist/src/adaptive/strategies/kv-stable.d.ts.map +1 -1
  10. package/dist/src/adaptive/strategies/kv-stable.js +14 -5
  11. package/dist/src/adaptive/strategies/kv-stable.js.map +1 -1
  12. package/dist/src/context-log.d.ts.map +1 -1
  13. package/dist/src/context-log.js +11 -0
  14. package/dist/src/context-log.js.map +1 -1
  15. package/dist/src/context-manager.d.ts +28 -1
  16. package/dist/src/context-manager.d.ts.map +1 -1
  17. package/dist/src/context-manager.js +42 -0
  18. package/dist/src/context-manager.js.map +1 -1
  19. package/dist/src/index.d.ts +3 -2
  20. package/dist/src/index.d.ts.map +1 -1
  21. package/dist/src/index.js +1 -0
  22. package/dist/src/index.js.map +1 -1
  23. package/dist/src/message-store.d.ts +60 -0
  24. package/dist/src/message-store.d.ts.map +1 -1
  25. package/dist/src/message-store.js +165 -10
  26. package/dist/src/message-store.js.map +1 -1
  27. package/dist/src/strategies/autobiographical.d.ts +207 -2
  28. package/dist/src/strategies/autobiographical.d.ts.map +1 -1
  29. package/dist/src/strategies/autobiographical.js +1283 -124
  30. package/dist/src/strategies/autobiographical.js.map +1 -1
  31. package/dist/src/strategies/index.d.ts +1 -1
  32. package/dist/src/strategies/index.d.ts.map +1 -1
  33. package/dist/src/strategies/index.js.map +1 -1
  34. package/dist/src/strategies/knowledge.d.ts +8 -0
  35. package/dist/src/strategies/knowledge.d.ts.map +1 -1
  36. package/dist/src/strategies/knowledge.js +23 -1
  37. package/dist/src/strategies/knowledge.js.map +1 -1
  38. package/dist/src/types/strategy.d.ts +82 -5
  39. package/dist/src/types/strategy.d.ts.map +1 -1
  40. package/dist/src/types/strategy.js.map +1 -1
  41. package/dist/test/adaptive/doc-plus-chat.test.js +7 -3
  42. package/dist/test/adaptive/doc-plus-chat.test.js.map +1 -1
  43. package/dist/test/adaptive/hard-fail.test.js +22 -0
  44. package/dist/test/adaptive/hard-fail.test.js.map +1 -1
  45. package/dist/test/adaptive/ingestion.test.js +3 -1
  46. package/dist/test/adaptive/ingestion.test.js.map +1 -1
  47. package/dist/test/adaptive/kv-control.test.js +17 -11
  48. package/dist/test/adaptive/kv-control.test.js.map +1 -1
  49. package/dist/test/adaptive/single-path-solve.test.d.ts +16 -0
  50. package/dist/test/adaptive/single-path-solve.test.d.ts.map +1 -0
  51. package/dist/test/adaptive/single-path-solve.test.js +204 -0
  52. package/dist/test/adaptive/single-path-solve.test.js.map +1 -0
  53. package/dist/test/anti-redundancy-budget.test.d.ts +17 -0
  54. package/dist/test/anti-redundancy-budget.test.d.ts.map +1 -0
  55. package/dist/test/anti-redundancy-budget.test.js +197 -0
  56. package/dist/test/anti-redundancy-budget.test.js.map +1 -0
  57. package/dist/test/chunk-persistence.test.d.ts +29 -0
  58. package/dist/test/chunk-persistence.test.d.ts.map +1 -0
  59. package/dist/test/chunk-persistence.test.js +290 -0
  60. package/dist/test/chunk-persistence.test.js.map +1 -0
  61. package/dist/test/compression-dedup.test.d.ts +20 -0
  62. package/dist/test/compression-dedup.test.d.ts.map +1 -0
  63. package/dist/test/compression-dedup.test.js +92 -0
  64. package/dist/test/compression-dedup.test.js.map +1 -0
  65. package/dist/test/compression-shape-invariants.test.js +40 -3
  66. package/dist/test/compression-shape-invariants.test.js.map +1 -1
  67. package/dist/test/head-window-order.test.d.ts +19 -0
  68. package/dist/test/head-window-order.test.d.ts.map +1 -0
  69. package/dist/test/head-window-order.test.js +159 -0
  70. package/dist/test/head-window-order.test.js.map +1 -0
  71. package/dist/test/image-strip-render-stats.test.js +25 -3
  72. package/dist/test/image-strip-render-stats.test.js.map +1 -1
  73. package/dist/test/knowledge-tool-boundary.test.d.ts +14 -0
  74. package/dist/test/knowledge-tool-boundary.test.d.ts.map +1 -0
  75. package/dist/test/knowledge-tool-boundary.test.js +93 -0
  76. package/dist/test/knowledge-tool-boundary.test.js.map +1 -0
  77. package/dist/test/l1-holdback.test.d.ts +15 -0
  78. package/dist/test/l1-holdback.test.d.ts.map +1 -0
  79. package/dist/test/l1-holdback.test.js +119 -0
  80. package/dist/test/l1-holdback.test.js.map +1 -0
  81. package/dist/test/merge-contiguity.test.d.ts +11 -0
  82. package/dist/test/merge-contiguity.test.d.ts.map +1 -0
  83. package/dist/test/merge-contiguity.test.js +74 -0
  84. package/dist/test/merge-contiguity.test.js.map +1 -0
  85. package/dist/test/message-store-window.test.d.ts +13 -0
  86. package/dist/test/message-store-window.test.d.ts.map +1 -0
  87. package/dist/test/message-store-window.test.js +177 -0
  88. package/dist/test/message-store-window.test.js.map +1 -0
  89. package/dist/test/speculation-cap.test.js +4 -0
  90. package/dist/test/speculation-cap.test.js.map +1 -1
  91. package/dist/test/summary-log-consistency.test.d.ts +18 -0
  92. package/dist/test/summary-log-consistency.test.d.ts.map +1 -0
  93. package/dist/test/summary-log-consistency.test.js +97 -0
  94. package/dist/test/summary-log-consistency.test.js.map +1 -0
  95. package/dist/test/tool-pairing-invariant.test.d.ts +15 -0
  96. package/dist/test/tool-pairing-invariant.test.d.ts.map +1 -0
  97. package/dist/test/tool-pairing-invariant.test.js +225 -0
  98. package/dist/test/tool-pairing-invariant.test.js.map +1 -0
  99. package/dist/tsconfig.tsbuildinfo +1 -1
  100. package/package.json +11 -3
  101. package/src/adaptive/kv-control.ts +509 -184
  102. package/src/adaptive/picker.ts +8 -0
  103. package/src/adaptive/strategies/kv-stable.ts +22 -10
  104. package/src/context-log.ts +11 -0
  105. package/src/context-manager.ts +48 -2
  106. package/src/index.ts +3 -2
  107. package/src/message-store.ts +202 -9
  108. package/src/strategies/autobiographical.ts +1343 -148
  109. package/src/strategies/index.ts +1 -1
  110. package/src/strategies/knowledge.ts +25 -1
  111. package/src/types/strategy.ts +89 -5
@@ -1,22 +1,31 @@
1
1
  /**
2
- * KV-stable context controller (see `docs/kv-stable-context-control.md`).
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
- * 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.
7
+ * One algorithm per turn, one hard constraint:
10
8
  *
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).
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
  */
@@ -25,14 +34,17 @@ import type { ChunkId } from './folding-strategy.js';
25
34
  import type { PickerInputs, PickerChunk } from './picker.js';
26
35
  import type { SummaryEntry } from '../types/strategy.js';
27
36
  import { SummaryTree } from './summary-tree.js';
28
- import { renderLayout, type RenderLayout, type Frontier } from './render-offsets.js';
37
+ import { renderLayout, kvCost, type RenderLayout, type Frontier } from './render-offsets.js';
29
38
  import { placeMarkers, CacheStore } from './kv-cache-sim.js';
30
39
 
31
40
  /** Provider price multipliers relative to base input tokens (Anthropic-ish). */
32
41
  export const PRICE = { miss: 1.0, cacheRead: 0.1, cacheWrite: 1.25 } as const;
33
42
 
34
- /** Deepest fold level the saliency field will ever allow. */
35
- export const MAX_FOLD_LEVEL = 3;
43
+ /** Safety ceiling on saliency fold depth. The *effective* cap is
44
+ * `maxAvailableLevel(tree)` (fold only as deep as summaries actually exist),
45
+ * which every caller passes in; this constant is just a high sane upper bound so
46
+ * L4/L5/… are allowed as the merge pipeline builds them — never a pin at L3. */
47
+ export const MAX_FOLD_LEVEL = 8;
36
48
 
37
49
  export interface ControlOptions {
38
50
  /** Physical context window — the ONLY hard wall. */
@@ -127,157 +139,359 @@ function maxAvailableLevel(tree: SummaryTree): number {
127
139
  return m;
128
140
  }
129
141
 
142
+ /** Per-chunk salience: the coefficient on information loss (design §13.3).
143
+ * Defaults to 1. Lower = folds earlier and costs less when folded (code-heavy /
144
+ * tool-output / externalized content). Never overrides hard protections. */
145
+ function salienceOf(c: PickerChunk): number {
146
+ const s = c.salience;
147
+ if (s === undefined || !Number.isFinite(s)) return 1;
148
+ return Math.min(1, Math.max(0, s));
149
+ }
150
+
151
+ /**
152
+ * Salience-weighted MISALLOCATION loss of a frontier: Σ salience(c) ·
153
+ * max(0, level(c) − shapeCap(c)) over foldable chunks — the excess fold depth
154
+ * beyond what the relevance shape prior (log-age band) would assign. Zero for
155
+ * any cut that folds nothing deeper than its prior; positive exactly where
156
+ * fidelity is spent in the wrong place. Recency-aware by construction (the
157
+ * cap grows with age), so an INVERTED profile — recent content deep, ancient
158
+ * content fine — scores high even when a naive Σ level would tie it with the
159
+ * correct gradient. Used only to compare cuts of the same tree (quality-gap
160
+ * tests, §13.4).
161
+ */
162
+ function relevanceLoss(
163
+ F: ReadonlyMap<ChunkId, number>,
164
+ ordered: PickerChunk[],
165
+ rawZone: ReadonlySet<ChunkId>,
166
+ caps: ReadonlyMap<ChunkId, number>,
167
+ ): number {
168
+ let loss = 0;
169
+ for (const c of ordered) {
170
+ if (rawZone.has(c.id)) continue;
171
+ const lvl = F.get(c.id) ?? 0;
172
+ if (lvl <= 0) continue;
173
+ const cap = Math.max(0, caps.get(c.id) ?? 0);
174
+ if (lvl > cap) loss += salienceOf(c) * (lvl - cap);
175
+ }
176
+ return loss;
177
+ }
178
+
179
+ /**
180
+ * Incremental token accounting for whole-group fold/unfold moves.
181
+ *
182
+ * `renderLayout` is O(n); calling it per accepted fold makes the ideal-cut
183
+ * construction O(n·folds) ≈ O(n²) — seconds on a 4k-chunk store, paid on
184
+ * EVERY compile under the single-path solve. The ledger tracks rendered units
185
+ * (raw shards / recall pairs) as a multiset keyed exactly like renderLayout's
186
+ * units, so a group fold/unfold is O(group) and `tokens` stays equal to
187
+ * `renderLayout(...).totalTokens` for the same frontier (both share the
188
+ * "missing ancestor → raw" fallback; siblings share one recall unit).
189
+ */
190
+ class TokenLedger {
191
+ private readonly unitTokens = new Map<string, number>(); // unitKey → tokens
192
+ private readonly unitRefs = new Map<string, number>(); // unitKey → leaf count
193
+ tokens = 0;
194
+
195
+ constructor(
196
+ private readonly tree: SummaryTree,
197
+ private readonly inputs: PickerInputs,
198
+ ordered: PickerChunk[],
199
+ F: ReadonlyMap<ChunkId, number>,
200
+ ) {
201
+ this.tokens = inputs.headTokens + inputs.tailTokens;
202
+ for (const c of ordered) {
203
+ if (inputs.headChunkIds.has(c.id) || inputs.tailChunkIds.has(c.id)) continue;
204
+ const { key, tokens } = this.unitFor(c, F.get(c.id) ?? 0);
205
+ this.addRef(key, tokens);
206
+ }
207
+ }
208
+
209
+ /** Unit identity + cost for a leaf at a level — mirrors renderLayout. */
210
+ private unitFor(c: PickerChunk, level: number): { key: string; tokens: number } {
211
+ const effective = c.pinned ? 0 : level;
212
+ if (effective > 0) {
213
+ const ancestor = this.tree.ancestorAt(c.id, effective);
214
+ if (ancestor) return { key: `recall:${ancestor.id}`, tokens: ancestor.recallTokens };
215
+ }
216
+ return { key: `raw:${c.id}`, tokens: c.rawTokens };
217
+ }
218
+
219
+ private addRef(key: string, tokens: number): void {
220
+ const refs = this.unitRefs.get(key) ?? 0;
221
+ if (refs === 0) {
222
+ this.unitTokens.set(key, tokens);
223
+ this.tokens += tokens;
224
+ }
225
+ this.unitRefs.set(key, refs + 1);
226
+ }
227
+
228
+ private dropRef(key: string): void {
229
+ const refs = this.unitRefs.get(key) ?? 0;
230
+ if (refs <= 1) {
231
+ this.unitRefs.delete(key);
232
+ this.tokens -= this.unitTokens.get(key) ?? 0;
233
+ this.unitTokens.delete(key);
234
+ } else {
235
+ this.unitRefs.set(key, refs - 1);
236
+ }
237
+ }
238
+
239
+ /** Move one leaf from `fromLevel` to `toLevel`. Callers move whole groups
240
+ * (every leaf of the target node), which keeps units exact. */
241
+ move(c: PickerChunk, byId: ReadonlyMap<ChunkId, PickerChunk>, fromLevel: number, toLevel: number): void {
242
+ if (this.inputs.headChunkIds.has(c.id) || this.inputs.tailChunkIds.has(c.id)) return;
243
+ void byId;
244
+ this.dropRef(this.unitFor(c, fromLevel).key);
245
+ const next = this.unitFor(c, toLevel);
246
+ this.addRef(next.key, next.tokens);
247
+ }
248
+ }
249
+
130
250
  /**
131
- * Deepen `frontier` in place to bring rendered tokens `target`, leveled
132
- * (L1 groups, then L2, then L3) and oldest-first *within the reach window*. A
133
- * chunk is editable this turn only if the raw tokens newer than it are <
134
- * `reachTokens` — this bounds the divergence depth (and thus the per-turn KV
135
- * perturbation) to ≈ reach. Each chunk is bounded by its saliency cap;
136
- * flat-zone/pinned chunks (cap 0) are never touched.
251
+ * Build the IDEAL CUT from scratch pure relevance, no P anywhere (§13.4).
137
252
  *
138
- * `reachTokens` is the soft cap P; `windowTokens` is the hard wall W. If folding
139
- * within reach can't fit under W, reach is lifted (emergency) and folding
140
- * continues to caps yielding P minimally to keep W. Returns final tokens.
253
+ * Fold order is (salience ascending, then oldest-first), level by level, so the
254
+ * profile is salience/age-monotone by construction: with uniform salience it
255
+ * reduces to the classic oldest-first shed. Two phases against the shape prior:
256
+ * A. fold under the soft `caps` (log-age shape + pin caps) until ≤ target;
257
+ * B. if still over W, keep folding past the shape caps (never past the
258
+ * hard-protected −1 sentinel or a pin cap) — the prior is a relevance
259
+ * shaper, never a feasibility wall;
260
+ * C. pack: un-fold youngest-first back toward `target` (accept-if-fit), so
261
+ * headroom is spent on the most recent foldable content.
262
+ * Ends projected to a valid group-consistent tree cut.
141
263
  */
142
- function shedToTarget(
143
- frontier: Map<ChunkId, number>,
264
+ function relevanceCut(
144
265
  inputs: PickerInputs,
145
266
  tree: SummaryTree,
146
267
  ordered: PickerChunk[],
147
- caps: Map<ChunkId, number>,
268
+ base: ReadonlyMap<ChunkId, number>,
269
+ caps: ReadonlyMap<ChunkId, number>,
270
+ pinCaps: ReadonlyMap<ChunkId, number>,
271
+ rawZone: ReadonlySet<ChunkId>,
272
+ immovable: ReadonlySet<ChunkId>,
273
+ frozen: ReadonlySet<ChunkId>,
274
+ fixedLevels: ReadonlyMap<ChunkId, number>,
148
275
  target: number,
149
- reachTokens: number,
150
276
  windowTokens: number,
151
277
  maxFoldLevel: number,
152
- pinCaps: ReadonlyMap<ChunkId, number>,
153
- ): number {
154
- // Raw tokens strictly newer than each chunk (its distance from the live end).
155
- const newerTokens = new Map<ChunkId, number>();
156
- let acc = 0;
157
- for (let i = ordered.length - 1; i >= 0; i--) {
158
- newerTokens.set(ordered[i].id, acc);
159
- acc += ordered[i].rawTokens;
160
- }
161
-
162
- const foldWithinReach = (reach: number, ignoreCaps = false): boolean => {
278
+ ): { F: Map<ChunkId, number>; tokens: number } {
279
+ const F = new Map(base);
280
+ const byId = new Map(ordered.map((c) => [c.id, c] as const));
281
+ // Fold priority: cheapest information first — ascending salience, then oldest.
282
+ const priority = [...ordered].sort((a, b) => {
283
+ const sa = salienceOf(a);
284
+ const sb = salienceOf(b);
285
+ if (sa !== sb) return sa - sb;
286
+ return a.sequence - b.sequence;
287
+ });
288
+
289
+ // Incremental accounting: O(group) per move instead of O(n) renderLayout.
290
+ const _tl = typeof process !== 'undefined' && process.env?.KV_TIMING ? Date.now() : 0;
291
+ const ledger = new TokenLedger(tree, inputs, ordered, F);
292
+ if (_tl) console.error(`[kv-timing] ledger-init ${Date.now() - _tl}ms tokens=${ledger.tokens}`);
293
+
294
+ const foldPass = (ignoreShapeCaps: boolean, stopAt: number): boolean => {
163
295
  for (let level = 1; level <= maxFoldLevel; level++) {
164
- for (const c of ordered) {
165
- // oldest-first within reach
166
- if ((newerTokens.get(c.id) ?? 0) >= reach) continue;
167
- if ((frontier.get(c.id) ?? 0) >= level) continue;
296
+ for (const c of priority) {
297
+ if (ledger.tokens <= stopAt) return true;
298
+ if ((F.get(c.id) ?? 0) >= level) continue;
168
299
  const ancestor = tree.ancestorAt(c.id, level);
169
300
  if (!ancestor) continue; // summary not produced yet → can't fold here
170
- // Group-consistency + reach: fold the WHOLE group to `level` only if
171
- // every covered leaf permits it (within the saliency cap unless
172
- // `ignoreCaps`, and within reach). The picker raises whole groups, so a
173
- // partially-eligible group would be an unreachable target (it
174
- // oscillates). A uniform fold keeps the frontier group-consistent and
175
- // the picker convergent. `rawZone`/`frozen` always block (their cap is 0
176
- // and stays 0 even under ignoreCaps).
301
+ // Group-atomicity: fold the WHOLE group to `level` only if every
302
+ // covered leaf permits it. Hard protections (−1 sentinel: flat zone /
303
+ // pins / locked) always block; pin-max-level always blocks past its
304
+ // bound; the soft shape cap blocks only in phase A.
177
305
  let eligible = true;
178
306
  for (const leafId of ancestor.leafChunkIds) {
179
307
  const cap = caps.get(leafId) ?? 0;
180
- // A V2 pin-max-level is a HARD cap: never fold a pinned chunk deeper
181
- // than its bound, even in the W emergency (pins are hard-protected).
182
308
  const pinCap = pinCaps.get(leafId);
183
- if (pinCap !== undefined && level > pinCap) {
184
- eligible = false;
185
- break;
186
- }
187
- // Normal: respect the saliency depth cap. Emergency (ignoreCaps): only
188
- // the hard-protected set (cap = −1: flat zone / pins / locked) blocks;
189
- // the age-extended-raw band (cap 0) and shallower-than-cap content all
190
- // become foldable to fit under W.
191
- const capBlocks = ignoreCaps ? cap < 0 : cap < level;
192
- if (capBlocks || (newerTokens.get(leafId) ?? 0) >= reach) {
193
- eligible = false;
194
- break;
195
- }
309
+ if (pinCap !== undefined && level > pinCap) { eligible = false; break; }
310
+ if (ignoreShapeCaps ? cap < 0 : cap < level) { eligible = false; break; }
196
311
  }
197
312
  if (!eligible) continue;
198
313
  for (const leafId of ancestor.leafChunkIds) {
199
- if ((frontier.get(leafId) ?? 0) < level) frontier.set(leafId, level);
314
+ const leaf = byId.get(leafId);
315
+ const from = F.get(leafId) ?? 0;
316
+ if (from < level) {
317
+ if (leaf) ledger.move(leaf, byId, from, level);
318
+ F.set(leafId, level);
319
+ }
200
320
  }
201
- if (renderLayout(inputs, tree, frontier).totalTokens <= target) return true;
321
+ if (ledger.tokens <= stopAt) return true;
202
322
  }
203
323
  }
204
324
  return false;
205
325
  };
206
326
 
207
- foldWithinReach(reachTokens);
208
- let tokens = renderLayout(inputs, tree, frontier).totalTokens;
209
- // Hard wall: W is the only hard constraint. If folding within reach + caps
210
- // still exceeds W, the emergency lifts BOTH the reach cap (P) AND the saliency
211
- // depth cap — the cap is a soft *relevance* shaper, never a feasibility wall.
212
- // It still never folds rawZone/frozen (their cap stays 0). (docs
213
- // adaptive-resolution-design.md §12.4: fixes OverBudgetError-at-L2 when deeper
214
- // summaries exist and would fit.)
215
- if (tokens > windowTokens) {
216
- foldWithinReach(Infinity, /* ignoreCaps */ true);
217
- tokens = renderLayout(inputs, tree, frontier).totalTokens;
327
+ // Phase A: fold under the shape prior toward the target.
328
+ if (ledger.tokens > target) foldPass(false, target);
329
+ // Phase B: the prior yields to W (only hard protections stand).
330
+ if (ledger.tokens > windowTokens) foldPass(true, target);
331
+
332
+ // Phase C: pack youngest-first back toward the target (skip if infeasible).
333
+ if (ledger.tokens <= windowTokens && ledger.tokens < target) {
334
+ for (let level = maxFoldLevel; level >= 1 && ledger.tokens < target; level--) {
335
+ for (let oi = ordered.length - 1; oi >= 0; oi--) {
336
+ if (ledger.tokens >= target) break;
337
+ const c = ordered[oi]; // youngest-first
338
+ if ((F.get(c.id) ?? 0) !== level) continue;
339
+ if (rawZone.has(c.id) || immovable.has(c.id)) continue;
340
+ const node = tree.ancestorAt(c.id, level);
341
+ if (!node) continue;
342
+ let eligible = true;
343
+ for (const leafId of node.leafChunkIds) {
344
+ if ((F.get(leafId) ?? 0) !== level || rawZone.has(leafId) || immovable.has(leafId)) {
345
+ eligible = false;
346
+ break;
347
+ }
348
+ }
349
+ if (!eligible) continue;
350
+ const before = ledger.tokens;
351
+ for (const leafId of node.leafChunkIds) {
352
+ const leaf = byId.get(leafId);
353
+ if (leaf) ledger.move(leaf, byId, level, level - 1);
354
+ F.set(leafId, level - 1);
355
+ }
356
+ // Accept an un-fold that lands PAST the target when it still gets
357
+ // CLOSER to it (and stays under W). Merge groups are coarse quanta
358
+ // (8-15k on production trees); accept-only-if-under-target strands
359
+ // real headroom un-spent whenever every remaining quantum overshoots
360
+ // (mythos 2026-07-12: 134k rendered of a 183.6k hard budget). The
361
+ // target stays the attractor — W stays the only wall (§13.2).
362
+ const nt = ledger.tokens;
363
+ const accept = nt <= windowTokens && Math.abs(nt - target) < Math.abs(before - target);
364
+ if (!accept) {
365
+ // moves away from the target (or breaches W) → revert
366
+ for (const leafId of node.leafChunkIds) {
367
+ const leaf = byId.get(leafId);
368
+ if (leaf) ledger.move(leaf, byId, level - 1, level);
369
+ F.set(leafId, level);
370
+ }
371
+ }
372
+ }
373
+ }
218
374
  }
219
- return tokens;
375
+
376
+ const _tp = typeof process !== 'undefined' && process.env?.KV_TIMING ? Date.now() : 0;
377
+ projectToValidCut(F, tree, ordered, frozen, fixedLevels);
378
+ if (_tp) console.error(`[kv-timing] cut-projection ${Date.now() - _tp}ms`);
379
+ // One authoritative render at the end (the ledger tracks it exactly, but the
380
+ // projection may have moved leaves — recompute once, not per move).
381
+ const tokens = renderLayout(inputs, tree, F).totalTokens;
382
+ return { F, tokens };
220
383
  }
221
384
 
222
385
  /**
223
- * Raise `frontier` resolution in place (UN-fold toward raw) to fill up to
224
- * `target`, leveled (L3→L2, L2→L1, L1→raw) and **youngest-first within the reach
225
- * window** the mirror image of `shedToTarget`. Un-folding is a prefix
226
- * perturbation too, so it obeys the same reach cap (bounded divergence) and only
227
- * lowers whole groups (group-consistent the picker reaches it via `lower`
228
- * ops). Youngest-first spends the budget on the most recent foldable content
229
- * (highest value, shallowest divergence). Stops before exceeding `target`.
230
- * rawZone/frozen chunks are never touched. Returns final tokens.
386
+ * Partial adoption of the ideal under the trust region (§13.4): keep `prev`
387
+ * before a boundary sequence, take `ideal` at and after it. Perturbation is
388
+ * prefix-based, so the cheapest partial plans are exactly the suffix cuts;
389
+ * binary-search the oldest boundary whose perturbation stays P (perturbation
390
+ * is monotone non-increasing as the boundary moves newer). Each candidate is
391
+ * projected to a valid cut before costing (mixing two cuts can bisect a group).
231
392
  */
232
- function expandToTarget(
233
- frontier: Map<ChunkId, number>,
393
+ function suffixAdopt(
234
394
  inputs: PickerInputs,
235
395
  tree: SummaryTree,
236
396
  ordered: PickerChunk[],
237
- target: number,
238
- reachTokens: number,
239
- rawZone: ReadonlySet<ChunkId>,
240
- immovable: ReadonlySet<ChunkId>,
241
- maxFoldLevel: number,
242
- ): number {
243
- const newerTokens = new Map<ChunkId, number>();
244
- let acc = 0;
245
- for (let i = ordered.length - 1; i >= 0; i--) {
246
- newerTokens.set(ordered[i].id, acc);
247
- acc += ordered[i].rawTokens;
397
+ prev: ReadonlyMap<ChunkId, number>,
398
+ prevLayout: RenderLayout,
399
+ ideal: ReadonlyMap<ChunkId, number>,
400
+ frozen: ReadonlySet<ChunkId>,
401
+ fixedLevels: ReadonlyMap<ChunkId, number>,
402
+ P: number,
403
+ ): { F: Map<ChunkId, number>; tokens: number; perturbation: number } {
404
+ const changedSeqs: number[] = [];
405
+ for (const c of ordered) {
406
+ if ((prev.get(c.id) ?? 0) !== (ideal.get(c.id) ?? 0)) changedSeqs.push(c.sequence);
248
407
  }
249
408
 
250
- let tokens = renderLayout(inputs, tree, frontier).totalTokens;
251
- for (let level = maxFoldLevel; level >= 1 && tokens < target; level--) {
252
- for (let oi = ordered.length - 1; oi >= 0; oi--) {
253
- if (tokens >= target) break;
254
- const c = ordered[oi]; // youngest-first
255
- if ((frontier.get(c.id) ?? 0) !== level) continue;
256
- if (rawZone.has(c.id) || immovable.has(c.id)) continue;
257
- if ((newerTokens.get(c.id) ?? 0) >= reachTokens) continue;
258
- const node = tree.ancestorAt(c.id, level);
259
- if (!node) continue;
260
- // Lower the WHOLE group to level-1 only if every covered leaf is at this
261
- // level and within reach (group-consistent + reach-bounded).
262
- let eligible = true;
263
- for (const leafId of node.leafChunkIds) {
264
- if (
265
- (frontier.get(leafId) ?? 0) !== level ||
266
- rawZone.has(leafId) || immovable.has(leafId) ||
267
- (newerTokens.get(leafId) ?? 0) >= reachTokens
268
- ) {
269
- eligible = false;
270
- break;
409
+ const build = (boundary: number): { F: Map<ChunkId, number>; layout: RenderLayout; pert: number } => {
410
+ const F = new Map(prev);
411
+ for (const c of ordered) {
412
+ if (c.sequence >= boundary) F.set(c.id, ideal.get(c.id) ?? 0);
413
+ }
414
+ projectToValidCut(F, tree, ordered, frozen, fixedLevels);
415
+ const layout = renderLayout(inputs, tree, F);
416
+ return { F, layout, pert: kvCost(prevLayout, layout) };
417
+ };
418
+
419
+ if (changedSeqs.length === 0) {
420
+ const layout = renderLayout(inputs, tree, new Map(prev));
421
+ return { F: new Map(prev), tokens: layout.totalTokens, perturbation: 0 };
422
+ }
423
+
424
+ // Binary search: smallest index (oldest boundary) with perturbation P.
425
+ let lo = 0;
426
+ let hi = changedSeqs.length; // hi = adopt nothing (pert 0, always feasible)
427
+ let best = build(Number.MAX_SAFE_INTEGER);
428
+ let bestIdx = hi;
429
+ while (lo < hi) {
430
+ const mid = (lo + hi) >> 1;
431
+ const cand = build(changedSeqs[mid]);
432
+ if (cand.pert <= P) {
433
+ best = cand;
434
+ bestIdx = mid;
435
+ hi = mid;
436
+ } else {
437
+ lo = mid + 1;
438
+ }
439
+ }
440
+ void bestIdx;
441
+ return { F: best.F, tokens: best.layout.totalTokens, perturbation: best.pert };
442
+ }
443
+
444
+ /**
445
+ * Project a per-chunk frontier onto the nearest VALID TREE CUT (group-consistent).
446
+ *
447
+ * A leaf folded to level k renders as part of its L_k ancestor node, and that
448
+ * node is atomic over its whole covered range — so EVERY leaf under it must share
449
+ * level k, or the node is unrenderable. The picker walks group-atomic raise/lower
450
+ * ops, so a frontier that folds only SOME leaves under a node is an unreachable
451
+ * target: it raises the group (over-folding a sibling), lowers it (under-folding
452
+ * another), forever — non-convergence (the wedge this fixes).
453
+ *
454
+ * Frontiers seeded per-chunk from the carried `previous` frontier can violate
455
+ * this: as a summary group accretes new raw leaves over turns while older leaves
456
+ * stay folded, its leaves end up at mixed levels. shed/expand only re-consolidate
457
+ * groups they actively move; a mixed group sitting in the budget dead-band passes
458
+ * through untouched. This pass repairs it.
459
+ *
460
+ * Shallowest-first: where a group disagrees, UN-fold the folded leaves one level
461
+ * toward raw (preserving continuity), cascading until every folded leaf's group
462
+ * is unanimous. Monotone (only lowers) → terminates. Any budget breach from the
463
+ * extra raw is re-folded whole-group by the shed pass that runs afterward.
464
+ *
465
+ * Pinned (`fixedLevels`) and frozen (locked) leaves are already made
466
+ * group-consistent by the caller (pin-at-level fixes a whole node's leaves), so
467
+ * they are held here rather than lowered.
468
+ */
469
+ function projectToValidCut(
470
+ F: Map<ChunkId, number>,
471
+ tree: SummaryTree,
472
+ ordered: PickerChunk[],
473
+ frozen: ReadonlySet<ChunkId>,
474
+ fixedLevels: ReadonlyMap<ChunkId, number>,
475
+ ): void {
476
+ const maxPasses = (ordered.length + 1) * (maxAvailableLevel(tree) + 2);
477
+ let changed = true;
478
+ let pass = 0;
479
+ while (changed && pass++ < maxPasses) {
480
+ changed = false;
481
+ for (const c of ordered) {
482
+ const lvl = F.get(c.id) ?? 0;
483
+ if (lvl <= 0) continue;
484
+ if (frozen.has(c.id) || fixedLevels.has(c.id)) continue; // held; consistent by construction
485
+ const node = tree.ancestorAt(c.id, lvl);
486
+ let unanimous = node != null;
487
+ if (node) {
488
+ for (const leafId of node.leafChunkIds) {
489
+ if ((F.get(leafId) ?? 0) !== lvl) { unanimous = false; break; }
271
490
  }
272
491
  }
273
- if (!eligible) continue;
274
- for (const leafId of node.leafChunkIds) frontier.set(leafId, level - 1);
275
- const nt = renderLayout(inputs, tree, frontier).totalTokens;
276
- if (nt <= target) tokens = nt; // accept the un-fold
277
- else for (const leafId of node.leafChunkIds) frontier.set(leafId, level); // overshoot → revert
492
+ if (!unanimous) { F.set(c.id, lvl - 1); changed = true; } // un-fold one level (shallowest-first)
278
493
  }
279
494
  }
280
- return tokens;
281
495
  }
282
496
 
283
497
  /** Earliest source sequence whose resolution changed `prev → next`. */
@@ -332,11 +546,21 @@ export interface ControlPlanParams {
332
546
  expandAtTokens?: number;
333
547
  /** The attractor both fold and un-fold aim for (soft target). */
334
548
  targetTokens: number;
335
- /** Hard wall W — fold past the reach cap only to keep under this. */
549
+ /** Hard wall W — the only hard constraint (§13.2). */
336
550
  windowTokens: number;
337
- /** Per-turn divergence reach (perturbation cap P) bounds BOTH fold and
338
- * un-fold divergence. Default = windowTokens. */
551
+ /** Trust region P on per-turn perturbation, in tokens the provider would
552
+ * re-read (exact `kvCost`, not a spatial gate). Within P the solver adopts
553
+ * the ideal cut outright; beyond it it adopts the ideal's newest changes
554
+ * only (suffix adoption) unless an override fires (§13.4). Default =
555
+ * windowTokens (a rendered layout never exceeds W, so the default trust
556
+ * region never binds). */
339
557
  reachTokens?: number;
558
+ /** Quality-gap override threshold (§13.4): a plan within P is rejected — and
559
+ * the ideal adopted, paying the perturbation — when its relevance loss
560
+ * exceeds the ideal's by more than this fraction of the ideal's loss.
561
+ * Also bounds how misallocated a dead-band hold may be before the solver
562
+ * self-heals. Default 0.35. */
563
+ qualityGapRatio?: number;
340
564
  /** Chunks forced to raw and never folded (flat zone / head / tail / pinned). */
341
565
  rawZone: ReadonlySet<ChunkId>;
342
566
  /** Chunks kept at their carried resolution and never touched (locked). */
@@ -368,21 +592,36 @@ export interface ControlPlan {
368
592
  folded: boolean;
369
593
  /** An un-fold (raise toward raw, to use budget headroom) happened this turn. */
370
594
  expanded: boolean;
371
- /** Even full folding under caps could not fit under W (terminal). */
595
+ /** Even full folding could not fit under W (terminal — production problem). */
372
596
  escalated: boolean;
597
+ /** Exact prefix-invalidation cost of this plan vs the carried frontier:
598
+ * tokens the provider will re-read (0 on a hold/pure-append turn). */
599
+ perturbation: number;
600
+ /** Set when the trust region P did not bind normally (§13.4). Callers should
601
+ * log these loudly (`[kv-escalation]`):
602
+ * - 'bootstrap': no carried frontier — pure relevance solve, P not applicable;
603
+ * - 'infeasible': nothing within P fits under W — feasibility beats P;
604
+ * - 'quality-gap': the best plan within P was certifiably bad vs the ideal. */
605
+ override?: 'bootstrap' | 'infeasible' | 'quality-gap';
373
606
  }
374
607
 
375
608
  /**
376
- * Plan one turn's frontier under the controller policy shared by the replay
377
- * harness (`replayControlled`) and `KvStableStrategy`. Carries F_prev forward,
378
- * holds the raw zone raw and the frozen set fixed, then drives the rendered size
379
- * toward `targetTokens` from EITHER side, both bounded by the `reachTokens`
380
- * divergence cap (KV-continuity preserving in both directions):
381
- * - over `foldAtTokens` → deepen, oldest-first, under the log-age saliency
382
- * caps, yielding the reach cap only as needed to stay under the hard wall W;
383
- * - under `expandAtTokens` un-fold, youngest-first, to use budget headroom.
384
- * The [expandAt, foldAt] dead band leaves F_prev untouched (zero perturbation)
385
- * when already in range. Pure and deterministic.
609
+ * Plan one turn's frontier the rev 5.0 single-path solve (§13.4), shared by
610
+ * the replay harness (`replayControlled`) and `KvStableStrategy`.
611
+ *
612
+ * 1. Build the IDEAL CUT from scratch (pure relevance; W the only wall).
613
+ * 2. No carried frontier → adopt it (bootstrap; P not applicable).
614
+ * 3. Carried frontier in the [expandAt, foldAt] dead band with a small
615
+ * quality gap hold it (zero perturbation). A LARGE gap falls through:
616
+ * a stuck misallocated profile self-heals instead of fossilizing.
617
+ * 4. Ideal within the trust region P (exact kvCost) adopt it.
618
+ * 5. Otherwise adopt the ideal's newest changes only (suffix adoption) if
619
+ * that fits W and is not certifiably bad; else adopt the ideal and record
620
+ * the override ('infeasible' / 'quality-gap').
621
+ *
622
+ * There is no emergency path and no spatial eligibility gate: cache continuity
623
+ * is priced (and bounded by P) — never a reason a chunk can't move.
624
+ * Pure and deterministic.
386
625
  */
387
626
  export function planControlledFrontier(
388
627
  inputs: PickerInputs,
@@ -394,66 +633,152 @@ export function planControlledFrontier(
394
633
  const fixedLevels = p.fixedLevels ?? EMPTY_LEVEL_MAP;
395
634
  const pinCaps = p.pinCaps ?? EMPTY_LEVEL_MAP;
396
635
  const mergeThreshold = p.mergeThreshold ?? 6;
397
- const reach = p.reachTokens ?? p.windowTokens;
636
+ const P = p.reachTokens ?? p.windowTokens;
398
637
  const expandAt = p.expandAtTokens ?? p.targetTokens;
638
+ const gapRatio = p.qualityGapRatio ?? 0.35;
399
639
  // Fold as deep as the tree actually goes (L4/L5… when produced), not a constant.
400
640
  const maxFoldLevel = maxAvailableLevel(tree);
401
641
 
402
- // Immovable = never folded OR un-folded by the shed/expand passes: the locked
403
- // (frozen) set plus V2 pin-at-level-k chunks (held at their fixed level).
642
+ // Immovable = never folded OR un-folded by the solve: the locked (frozen)
643
+ // set plus V2 pin-at-level-k chunks (held at their fixed level).
404
644
  const immovable = new Set<ChunkId>(frozen);
405
645
  for (const id of fixedLevels.keys()) immovable.add(id);
406
646
 
407
- const F = new Map<ChunkId, number>();
647
+ // Base = the non-negotiable part of every cut: raw zone raw, pins at their
648
+ // level, frozen at their carried resolution. Everything else starts raw here;
649
+ // the carried frontier and the ideal cut both build on this base.
650
+ const base = new Map<ChunkId, number>();
408
651
  for (const c of ordered) {
409
652
  if (p.rawZone.has(c.id)) {
410
- F.set(c.id, 0);
653
+ base.set(c.id, 0);
411
654
  } else if (fixedLevels.has(c.id)) {
412
655
  // Pin-at-k: fix to exactly k, clamped to the deepest produced level for
413
656
  // this chunk (can't render at a level whose summary doesn't exist yet).
414
657
  const k = Math.max(0, fixedLevels.get(c.id)!);
415
- F.set(c.id, k === 0 ? 0 : Math.min(k, tree.maxLevel(c.id)));
658
+ base.set(c.id, k === 0 ? 0 : Math.min(k, tree.maxLevel(c.id)));
416
659
  } else if (frozen.has(c.id)) {
417
- F.set(c.id, p.previous.get(c.id) ?? c.currentResolution);
660
+ base.set(c.id, p.previous.get(c.id) ?? c.currentResolution);
661
+ } else {
662
+ base.set(c.id, 0);
663
+ }
664
+ }
665
+
666
+ // Carried frontier (F_prev with the base enforced): what is actually
667
+ // rendered right now, and the reference for perturbation.
668
+ const carried = new Map(base);
669
+ let carriedNonEmpty = false;
670
+ for (const c of ordered) {
671
+ if (p.rawZone.has(c.id) || fixedLevels.has(c.id) || frozen.has(c.id)) continue;
672
+ // Enforce a pin-max-level immediately: un-fold anything carried deeper than
673
+ // its cap down to the cap (shallower summaries — its ancestors — are
674
+ // guaranteed to exist). The intended divergence cost of tightening a pin.
675
+ let lvl = p.previous.get(c.id) ?? 0;
676
+ const cap = pinCaps.get(c.id);
677
+ if (cap !== undefined && lvl > cap) lvl = Math.max(0, cap);
678
+ carried.set(c.id, lvl);
679
+ }
680
+ for (const v of p.previous.values()) {
681
+ if (v !== 0) { carriedNonEmpty = true; break; }
682
+ }
683
+ projectToValidCut(carried, tree, ordered, frozen, fixedLevels);
684
+ const carriedLayout = renderLayout(inputs, tree, carried);
685
+ const carriedTokens = carriedLayout.totalTokens;
686
+
687
+ // Shape prior: soft per-chunk depth caps (log-age band + pin caps), with the
688
+ // −1 sentinel marking the hard-protected set (never folded by anything).
689
+ const flatZoneChunks = p.rawZone.size;
690
+ const caps = new Map<ChunkId, number>();
691
+ for (const c of ordered) {
692
+ let cap: number;
693
+ if (p.rawZone.has(c.id) || immovable.has(c.id)) {
694
+ cap = -1; // hard-protected sentinel: never folded, W notwithstanding
418
695
  } else {
419
- // Carry F_prev, but enforce a pin-max-level immediately: un-fold anything
420
- // carried deeper than its cap down to the cap (shallower summaries — its
421
- // ancestors are guaranteed to exist). This is the intended divergence
422
- // cost of adding/tightening a pin (design §7).
423
- let lvl = p.previous.get(c.id) ?? 0;
424
- const cap = pinCaps.get(c.id);
425
- if (cap !== undefined && lvl > cap) lvl = Math.max(0, cap);
426
- F.set(c.id, lvl);
696
+ cap = foldDepthCap(c, p.now, p.rawZone, flatZoneChunks, mergeThreshold, maxFoldLevel);
697
+ const pinCap = pinCaps.get(c.id);
698
+ if (pinCap !== undefined) cap = Math.min(cap, Math.max(0, pinCap));
427
699
  }
700
+ caps.set(c.id, cap);
428
701
  }
429
702
 
430
- let tokens = renderLayout(inputs, tree, F).totalTokens;
431
- const before = tokens;
432
- let folded = false;
433
- let expanded = false;
434
- let escalated = false;
435
- if (tokens > p.foldAtTokens) {
436
- const flatZoneChunks = p.rawZone.size;
437
- const caps = new Map<ChunkId, number>();
703
+ // 1. The ideal cut — pure relevance, no P anywhere.
704
+ const _t0 = typeof process !== 'undefined' && process.env?.KV_TIMING ? Date.now() : 0;
705
+ const ideal = relevanceCut(
706
+ inputs, tree, ordered, base, caps, pinCaps, p.rawZone, immovable, frozen, fixedLevels,
707
+ p.targetTokens, p.windowTokens, maxFoldLevel,
708
+ );
709
+ if (_t0) console.error(`[kv-timing] relevanceCut ${Date.now() - _t0}ms idealTokens=${ideal.tokens}`);
710
+ const idealLayout = renderLayout(inputs, tree, ideal.F);
711
+ const idealLoss = relevanceLoss(ideal.F, ordered, p.rawZone, caps);
712
+ const gapCeiling = gapRatio * Math.max(1, idealLoss);
713
+
714
+ const flags = (F: ReadonlyMap<ChunkId, number>): { folded: boolean; expanded: boolean } => {
715
+ let folded = false;
716
+ let expanded = false;
438
717
  for (const c of ordered) {
439
- let cap: number;
440
- if (p.rawZone.has(c.id) || immovable.has(c.id)) {
441
- cap = -1; // hard-protected sentinel: never fold, even in the W emergency
442
- } else {
443
- cap = foldDepthCap(c, p.now, p.rawZone, flatZoneChunks, mergeThreshold, maxFoldLevel);
444
- const pinCap = pinCaps.get(c.id);
445
- if (pinCap !== undefined) cap = Math.min(cap, Math.max(0, pinCap));
446
- }
447
- caps.set(c.id, cap);
718
+ const prev = carried.get(c.id) ?? 0;
719
+ const next = F.get(c.id) ?? 0;
720
+ if (next > prev) folded = true;
721
+ else if (next < prev) expanded = true;
722
+ if (folded && expanded) break;
448
723
  }
449
- tokens = shedToTarget(F, inputs, tree, ordered, caps, p.targetTokens, reach, p.windowTokens, maxFoldLevel, pinCaps);
450
- folded = tokens !== before; // a real fold only if it actually changed the render
451
- if (tokens > p.windowTokens) escalated = true;
452
- } else if (tokens < expandAt) {
453
- tokens = expandToTarget(F, inputs, tree, ordered, p.targetTokens, reach, p.rawZone, immovable, maxFoldLevel);
454
- expanded = tokens !== before; // a real un-fold only if it actually changed
724
+ return { folded, expanded };
725
+ };
726
+
727
+ const adoptIdeal = (override?: ControlPlan['override']): ControlPlan => ({
728
+ resolutions: ideal.F,
729
+ tokens: ideal.tokens,
730
+ ...flags(ideal.F),
731
+ escalated: ideal.tokens > p.windowTokens,
732
+ perturbation: kvCost(carriedLayout, idealLayout),
733
+ ...(override ? { override } : {}),
734
+ });
735
+
736
+ // 2. Bootstrap: nothing carried → nothing to preserve; pure relevance solve.
737
+ if (!carriedNonEmpty && p.previous.size === 0) return adoptIdeal('bootstrap');
738
+
739
+ // 3. Dead band with self-heal: hold the carried frontier only when it is
740
+ // feasible, in band, AND not certifiably misallocated.
741
+ const carriedLoss = relevanceLoss(carried, ordered, p.rawZone, caps);
742
+ const inBand = carriedTokens <= p.foldAtTokens && carriedTokens >= expandAt;
743
+ if (inBand && carriedTokens <= p.windowTokens && carriedLoss - idealLoss <= gapCeiling) {
744
+ return {
745
+ resolutions: carried,
746
+ tokens: carriedTokens,
747
+ ...flags(carried), // vs itself → both false unless projection moved it
748
+ escalated: false,
749
+ perturbation: 0,
750
+ };
751
+ }
752
+
753
+ // 4. Within the trust region → adopt the ideal outright.
754
+ const idealPert = kvCost(carriedLayout, idealLayout);
755
+ if (idealPert <= P) return adoptIdeal();
756
+
757
+ // 5. Suffix adoption: the ideal's newest changes only, within P.
758
+ const partial = suffixAdopt(
759
+ inputs, tree, ordered, carried, carriedLayout, ideal.F, frozen, fixedLevels, P,
760
+ );
761
+ const partialLoss = relevanceLoss(partial.F, ordered, p.rawZone, caps);
762
+ // Progress guard: when a shed is REQUIRED (carried over foldAt), a partial
763
+ // plan that neither reaches the band nor moves at all is not acceptable —
764
+ // an under-folded profile scores zero misallocation loss, so the quality
765
+ // gap alone cannot reject "do nothing forever". (P below the physical floor
766
+ // — the tail that any fold must invalidate — lands here and overrides.)
767
+ const mustShed = carriedTokens > p.foldAtTokens;
768
+ const madeProgress =
769
+ !mustShed || partial.tokens <= p.foldAtTokens || partial.perturbation > 0;
770
+ if (partial.tokens <= p.windowTokens && partialLoss - idealLoss <= gapCeiling && madeProgress) {
771
+ return {
772
+ resolutions: partial.F,
773
+ tokens: partial.tokens,
774
+ ...flags(partial.F),
775
+ escalated: false,
776
+ perturbation: partial.perturbation,
777
+ };
455
778
  }
456
- return { resolutions: F, tokens, folded, expanded, escalated };
779
+
780
+ // Trust region priced out: feasibility or quality demands the ideal.
781
+ return adoptIdeal(partial.tokens > p.windowTokens ? 'infeasible' : 'quality-gap');
457
782
  }
458
783
 
459
784
  /**