pi-tuck 1.0.24 → 1.0.25

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/CHANGELOG.md CHANGED
@@ -3,6 +3,37 @@
3
3
  All notable changes to pi-tuck. The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and versions follow
4
4
  [Semantic Versioning](https://semver.org/).
5
5
 
6
+ ## [1.0.25] - 2026-11-10
7
+
8
+ ### Fixed
9
+
10
+ - **A request above the room could be admitted and then refused, leaving the session stuck where native Pi would have sent.** pi-tuck checked
11
+ its line in its own corrected token count. Pi's reply cap uses Pi's own estimate, which is higher when the provider counts fewer tokens
12
+ than pi-tuck's raw measure.
13
+ - Now each point that sends above the room checks Pi's own cap on the request it would send.
14
+ - When that leaves less than the reply floor, it cuts deeper: up to three re-plans, then the deepest cut that still carries the whole
15
+ prompt. It refuses only if that leaves less than the floor too.
16
+ - In the stress test, 7 refusals now answer, 3 of which had stranded the session.
17
+
18
+ ### Added
19
+
20
+ - **One retry after a reply that stops for length with thinking only.** pi-tuck retries the turn once, leaving the stopped reply out of the
21
+ request. It uses the deepest cut that still carries the whole prompt when that gives the reply more room; otherwise it retries uncut.
22
+ - After such a stop, the reply floor is the largest reply on the branch, the stopped one included, instead of the whole reserve. The full
23
+ reserve would refuse every request when the setup alone is above the room.
24
+ - A retried reply that stops again ends the run.
25
+ - `/tuck` counts retries.
26
+ - **A notice when a run ends on a reply cut at the output limit:** "the last reply stopped at the model's output limit and may be cut off".
27
+ It goes to stderr when there's no UI and never enters the model's context.
28
+ - **Stress runs strict in CI:** any finding fails it. A refusal is exempt only when an independent check allows it. One such check is Pi's
29
+ own reply cap on the smallest request that answers the prompt.
30
+
31
+ ### Release gates
32
+
33
+ - **Shipped without a live gate, because its replay matches 1.0.24 exactly:** 13,946 requests on 29 recordings, 0 different, 0 lost and
34
+ 0 dead tasks, clips 17 → 17.
35
+ - **Stress, 310 cells, strict:** 0 findings (1.0.24: 25 lines, 14 of them this defect). Tests (731) and CI.
36
+
6
37
  ## [1.0.24] - 2026-11-10
7
38
 
8
39
  ### Fixed
package/README.md CHANGED
@@ -64,8 +64,8 @@ Then start a new Pi session. That is all. pi-tuck takes over Pi's compaction, bo
64
64
 
65
65
  - Needs Pi 1.2.0 or later (tested on 1.2.0 and 1.4.0), on macOS or Linux. On an older Pi, pi-tuck stays off, Pi's own compaction runs, and pi-tuck says so once. Full features on macOS with Apple silicon; elsewhere, compaction and word recall.
66
66
  - On macOS with Apple silicon, the first session also downloads a local search model in the background (about 390 MB, once), and says once when it is set up. Until it is ready, search works by keyword. See [Semantic search](#semantic-search).
67
- - Prefer GitHub? `pi install git:github.com/galyavin/pi-tuck@v1.0.23`.
68
- - To pin a version, add it: `pi install npm:pi-tuck@1.0.23`.
67
+ - Prefer GitHub? `pi install git:github.com/galyavin/pi-tuck@v1.0.24`.
68
+ - To pin a version, add it: `pi install npm:pi-tuck@1.0.24`.
69
69
 
70
70
  ## Uninstall
71
71
 
@@ -133,6 +133,7 @@ Known limits today:
133
133
  - pi-tuck refused requests that fit the window, and could strand a session (1.0.0 to 1.0.21; fixed in 1.0.22). It refused at the room, the window less the full reply reserve, where native Pi still sends. With Pi's defaults and a large setup, a session could be refused at every prompt. The final refusal now sits at the window less a reply floor ([`CHANGELOG.md`](CHANGELOG.md) 1.0.22).
134
134
  - Near the 1.0.22 refusal line, replies were cut to a few hundred tokens (1.0.22; fixed in 1.0.23). 1.0.22 left only its reply floor and did not count Pi's 4,096-token margin on the reply cap ([`CHANGELOG.md`](CHANGELOG.md) 1.0.23).
135
135
  - Cost of the 1.0.22 change: between the room and the window, the reply reserve no longer cushions a wrong count. At a cold start an undercount can reach the provider. A provider that errors is caught and learned from; a server that truncates silently truncates, as it does for native Pi. With a setup above the room, cuts are forced and frequent, each freeing little, which beats a stranded session ([`CHANGELOG.md`](CHANGELOG.md) 1.0.22).
136
+ - A prompt's cut could reach a run it was not planned for (fixed in 1.0.24). pi-tuck plans a prompt's cut when the prompt arrives. When another extension handled that prompt, no run started, and the plan waited for the next run. A run started by a custom message carried a cut sized for the prompt that never ran, deeper than needed. After a move in the session tree (`/tree`), the plan was applied on the new branch, where its kept entry was not present, so the request lost the custom message that started the run, even on a branch that needed no cut ([`CHANGELOG.md`](CHANGELOG.md) 1.0.24).
136
137
  - Four `tuck_search` faults, fixed in 1.0.23: it said semantic recall was still indexing when the history was fully indexed; in the turn after a prompt's cut it ranked nothing ("nothing is out of view"); a keyword-search timeout on a very long history failed the whole search, where now the semantic results still answer; and the timeout message named a pattern instead of the history's size ([`CHANGELOG.md`](CHANGELOG.md) 1.0.23).
137
138
  - pi-tuck cut earlier than native Pi when a model's reply cap is above Pi's reserve (1.0.0 to 1.0.19; fixed in 1.0.20). It reserved the larger of Pi's `compaction.reserveTokens` and the model's `maxTokens`: with `maxTokens` 32,768 and a reserve of 16,384 on an 81,920 window, its room fell to 49,152 tokens, where native Pi compacts at 65,536 ([`CHANGELOG.md`](CHANGELOG.md) 1.0.20).
138
139
  - A summary could drop the task a session was working on (1.0.0 to 1.0.17; fixed in 1.0.18). Until 1.0.15 a summary quoted only the session's first user turn, up to 12,000 characters, and the newest one, so in a long session the current task, an older turn followed by short steps, was clipped or dropped while a finished first task stayed whole, and agents acted on a days-old task. 1.0.16 added more quotes. From 1.0.13 to 1.0.17, separate rules decided what a summary quoted and what the kept tail held, and the prompt that set the current task, or the first prompt, could lose its opening after a cut: the stress test found it 40 times in 32 cells on 1.0.16 and 1.0.17. Since 1.0.18 one ranked list fills a cut's room, and the stress test finds no lost opening in 287 cells. Short sessions, and the SWE-bench chains behind our benchmarks, keep each task's prompt in the kept tail and never hit it. Upgrade to 1.0.18 ([`CHANGELOG.md`](CHANGELOG.md) 1.0.18).
@@ -1 +1 @@
1
- export declare const BUILD_ID = "c3eefa3bb76e52635b1e8394a025d3ffd6a056fc5636ed9b35f89dfddc1a322a";
1
+ export declare const BUILD_ID = "48d3f777f203edb56b29154215540eb8245182237d18cdc50c6cbe704ea28e7e";
@@ -1 +1 @@
1
- export const BUILD_ID = "c3eefa3bb76e52635b1e8394a025d3ffd6a056fc5636ed9b35f89dfddc1a322a";
1
+ export const BUILD_ID = "48d3f777f203edb56b29154215540eb8245182237d18cdc50c6cbe704ea28e7e";
@@ -51,7 +51,8 @@ export interface CompactionOptions {
51
51
  cutAt?: number;
52
52
  /** A host's response reserve: without `responseExact: true`, the catalogue maximum output stays its floor; with it, `response` is exact.
53
53
  * Standalone cuts against Pi's reserveTokens, then admits up to the window less Pi's safety margin and max(3,584, largest branch output), capped at that reserve.
54
- * After a length stop or context overflow it holds the full reserve, also raised to the catalogue maximum.
54
+ * After a length stop with text/tool output or context overflow it holds the full reserve, also raised to the catalogue maximum.
55
+ * Thinking-only exhaustion keeps Pi's reserve and requires the branch's largest observed reply instead.
55
56
  * Resume, fork and /tree follow that branch's history; a new session has no inherited fallback. */
56
57
  requestReserve?: (ctx: ExtensionContext) => {
57
58
  response: number;
@@ -1,16 +1,17 @@
1
1
  import { textOf } from "./tuck/core/content.js";
2
- import { requestReplyFloor, carriesPrompt, piContextSafetyMargin } from "./request-capacity.js";
2
+ import { requestReplyFloor, carriesPrompt, needsFullReplyReserve, thinkingOnlyLength, piContextSafetyMargin } from "./request-capacity.js";
3
3
  import { BUILD_ID } from "./build-id.js";
4
4
  import { createSemanticRecall, budgetSemantic } from "./semantic-recall.js";
5
5
  import { DIAGNOSTIC_BUILD, IncidentRecorder, attachReceipt, permittedEntries, receiptId, findReceiptEntry } from "./diagnostics.js";
6
6
  import { createHash } from "node:crypto";
7
7
  import { exportSource } from "./source-export.js";
8
8
  import { isDeepStrictEqual } from "node:util";
9
- import { assertNativeToolRounds } from "./protocol.js";
9
+ import { assertNativeToolRounds, isDiscardedResponse } from "./protocol.js";
10
10
  import { projectedMessages } from "./omissions.js";
11
11
  import { applyClips, clipBudgets, clipFor } from "./clip.js";
12
12
  import { DEFAULT_CUT_AT, bandOf, chooseCut, conversationTokens, gateCut, incomingEventTokens, incomingTokens, measurableConversationTokens, planningReserve, projectedConversationTokens, systemPart, validCutAt } from "./budget.js";
13
13
  import { getCurrentSystemMessage, isContextOverflow, normalizeContext } from "@earendil-works/pi-ai";
14
+ import { estimateContextTokens } from "@earendil-works/pi-ai/utils/estimate";
14
15
  import { calculateContextTokens, convertToLlm, estimateTokens, shouldCompact, SessionManager, SettingsManager } from "@earendil-works/pi-coding-agent";
15
16
  import { Type } from "typebox";
16
17
  import { Check } from "typebox/value";
@@ -105,7 +106,7 @@ export function seedWindow(branch, key) {
105
106
  }
106
107
  export async function installCompaction(pi, options = {}) {
107
108
  const piMargin = options.requestReserve ? 0 : await piContextSafetyMargin();
108
- const piClamp = options.requestReserve ? undefined : await import('@earendil-works/pi-ai/api/simple-options')
109
+ const piClamp = await import('@earendil-works/pi-ai/api/simple-options')
109
110
  .then(module => module.clampMaxTokensToContext).catch(() => undefined);
110
111
  const cutAt = options.cutAt ?? DEFAULT_CUT_AT;
111
112
  if (!validCutAt(cutAt))
@@ -224,6 +225,7 @@ export async function installCompaction(pi, options = {}) {
224
225
  // Pi's native start/settled events bracket the run and its post-turn cuts.
225
226
  let admittedRun = false;
226
227
  let runPrompt;
228
+ let thinkingRetryPending = false;
227
229
  const protectedPrompt = (ctx) => runPrompt ? ctx.sessionManager.getBranch().findLast(entry => entry.type === "message" && entry.message.role === "user" && textOf(entry.message.content).includes(runPrompt))?.id : undefined;
228
230
  const promptRefusal = () => new Refusal("Prompt check failed: the whole starting prompt cannot fit after cutting to the band and room, shrinking the summary and clipping fresh results. Shorten the prompt or choose a model with a larger context window, then retry");
229
231
  let estimatedTokens = 0;
@@ -301,21 +303,21 @@ export async function installCompaction(pi, options = {}) {
301
303
  if (input > 0 && input <= ctx.model.contextWindow && Number.isSafeInteger(input) && Number.isFinite(ratio))
302
304
  learn(ratio);
303
305
  });
304
- pi.on("agent_start", () => { admittedRun = true; pending = undefined; phase = "idle"; });
305
- pi.on("agent_settled", () => { admittedRun = false; runPrompt = undefined; pending = undefined; phase = "idle"; forgetFailure(); clips.clear(); promptCut = undefined; });
306
+ pi.on("agent_start", () => { admittedRun = true; thinkingRetryPending = false; pending = undefined; phase = "idle"; });
307
+ pi.on("agent_settled", () => { admittedRun = false; runPrompt = undefined; thinkingRetryPending = false; pending = undefined; phase = "idle"; forgetFailure(); clips.clear(); promptCut = undefined; });
306
308
  const active = () => options.active?.() ?? true;
307
309
  const settingsFor = () => options.settings ?? SettingsManager.inMemory(pi.getSettings());
308
310
  // The one reserve decision; every consumer takes its room from limits. Standalone starts at Pi's native cut point. Once a reply did
309
- // not fit (a length stop, or a refusal pi-ai classes as a context overflow), hold the catalogue maximum too. Other errors, such as a 503,
310
- // keep native's room. Read the whole branch, including history before compactions, each time: no flag to persist or reset on resume,
311
+ // not fit (a length stop with text/tools, or a refusal pi-ai classes as a context overflow), hold the catalogue maximum too. A thinking-only
312
+ // length stop uses branchReplyFloor's observed output instead; reserving the catalogue maximum would strand setups above the room.
313
+ // Other errors, such as a 503, keep native's room. Read the whole branch, including history before compactions, each time: no flag to persist or reset on resume,
311
314
  // fork, /tree or a new session.
312
315
  const requestReserve = (ctx) => {
313
316
  const host = options.requestReserve?.(ctx);
314
317
  const budget = host ?? { response: settingsFor().getCompactionSettings(ctx.model).reserveTokens };
315
318
  if (!Number.isSafeInteger(budget.response) || budget.response < 0)
316
319
  throw new Error("Invalid host request reserve");
317
- const floor = host ? host.responseExact !== true : ctx.sessionManager.getBranch().some(entry => entry.type === "message" && entry.message.role === "assistant" &&
318
- (entry.message.stopReason === "length" || (entry.message.stopReason === "error" && isContextOverflow(entry.message))));
320
+ const floor = host ? host.responseExact !== true : ctx.sessionManager.getBranch().some(entry => entry.type === "message" && needsFullReplyReserve(entry.message));
319
321
  return { response: floor ? Math.max(budget.response, ctx.model?.maxTokens ?? 0) : budget.response };
320
322
  };
321
323
  const inputRoom = (response, ctx) => {
@@ -345,7 +347,7 @@ export async function installCompaction(pi, options = {}) {
345
347
  // The planner counts with the same function, except that it does not fail on a summary in another format: a cut can replace one
346
348
  // (an operator's /compact repairs a session whose saved summary is foreign). Identical to the strict one for this compactor's summaries.
347
349
  const plannerMeasureFor = (ctx) => Object.assign((messages) => measurableConversationTokens(applyClips(messages, clips), ctx, options.recallPolicy), { messageTokens: (message) => conversationTokens(applyClips([message], clips)) });
348
- const hostTokens = (ctx) => options.hostMessages ? conversationTokens(options.hostMessages(ctx)) : 0;
350
+ const hostMessages = (ctx) => options.hostMessages?.(ctx) ?? [];
349
351
  const toolShapes = (names) => {
350
352
  const selected = new Set(names);
351
353
  return pi.getAllTools().filter(tool => selected.has(tool.name)).map(tool => ({ name: tool.name, description: tool.description, parameters: tool.parameters }));
@@ -358,6 +360,53 @@ export async function installCompaction(pi, options = {}) {
358
360
  const future = saved.some(message => message.role === "system") ? historyToolDeclarations(saved, shapes, true) : saved;
359
361
  return systemPart(observedPrompt, shapes, future, guardPrompt);
360
362
  };
363
+ const clampMessage = "Pi's request clamp leaves less than the reply floor";
364
+ // Both the guard and the early projections ask Pi about the very same converted context. Pi's cap uses its own estimator,
365
+ // whereas our hard line is in corrected units; fitting that line alone need not leave the branch's reply floor.
366
+ const requestClamp = (messages, ctx) => {
367
+ const context = normalizeContext({ messages: convertToLlm(messages) });
368
+ const cap = piClamp?.(ctx.model, context, ctx.model.maxTokens);
369
+ return { cap, leavesFloor: cap === undefined || cap >= limits(ctx).replyFloor,
370
+ estimate: estimateContextTokens(context).tokens };
371
+ };
372
+ const projectedRequest = (messages, ctx, host, incoming, prompt, tools, cut) => {
373
+ const enabled = cut || historyToolsEnabled(ctx), shapes = toolShapes(historyToolNames(pi, tools, enabled));
374
+ const saved = messages.some(message => message.role === "system") ? messages :
375
+ [{ role: "system", content: prompt, toolsAdded: shapes, timestamp: 0 }, ...messages];
376
+ const declared = enabled ? historyToolDeclarations(saved, shapes, true) : saved;
377
+ return applyClips([...host, ...(enabled ? projectedMessages(declared, ctx, options.recallPolicy) : declared), ...incoming], clips);
378
+ };
379
+ const clampAboveRoom = (decision, ctx, room, target, total, project, replan, deepest) => {
380
+ if (options.requestReserve || !piClamp)
381
+ return decision;
382
+ const lastAttempt = () => {
383
+ const previousClips = new Map(clips);
384
+ const candidate = deepest();
385
+ if (candidate.kind === "cut" && fits(total(candidate), limits(ctx).hardLine, ctx) && requestClamp(project(candidate), ctx).leavesFloor)
386
+ return candidate;
387
+ clips.clear();
388
+ for (const [id, clip] of previousClips)
389
+ clips.set(id, clip);
390
+ return { kind: "fault", capacity: true, message: clampMessage };
391
+ };
392
+ for (let retry = 0;; retry++) {
393
+ if (decision.kind !== "cut" && decision.kind !== "none")
394
+ return decision;
395
+ if (corrected(total(decision), ctx) <= room)
396
+ return decision;
397
+ const check = requestClamp(project(decision), ctx);
398
+ if (check.leavesFloor)
399
+ return decision;
400
+ if (retry === 3)
401
+ return lastAttempt();
402
+ // Shrink in the planner's units by Pi's measured shortfall, at most three times. Once we reach the original room,
403
+ // use its existing band/room policy; admission below the room is deliberately unchanged.
404
+ target = Math.max(room, Math.floor(target * (ctx.model.contextWindow - piMargin - limits(ctx).replyFloor) / check.estimate));
405
+ decision = replan(target);
406
+ if (decision.kind === "defer" || decision.kind === "fault")
407
+ return lastAttempt();
408
+ }
409
+ };
361
410
  // Pi reports a manual cut with nothing to do as a failed compaction; the thrown error lacks the event's "Compaction failed: " prefix.
362
411
  const isNoWork = (message) => ["Already compacted", "Nothing to compact (session too small)"].includes(message?.replace(/^Compaction failed: /, "") ?? "");
363
412
  // A missing model is configuration, not capacity.
@@ -433,7 +482,7 @@ export async function installCompaction(pi, options = {}) {
433
482
  phase = "decided";
434
483
  try {
435
484
  syncHistoryTools(ctx);
436
- await preflight(ctx, incomingTokens(event.text, event.images), ctx.getSystemPrompt(), pi.getActiveTools());
485
+ await preflight(ctx, incomingTokens(event.text, event.images), [{ role: "user", content: [{ type: "text", text: event.text }, ...(event.images ?? [])], timestamp: 0 }], ctx.getSystemPrompt(), pi.getActiveTools());
437
486
  syncHistoryTools(ctx);
438
487
  }
439
488
  catch (error) {
@@ -535,7 +584,7 @@ export async function installCompaction(pi, options = {}) {
535
584
  // A cut Pi asks for (an operator's /compact, Pi's threshold or overflow recovery) charges what the request must carry whatever is
536
585
  // cut: the system part and the host input. A prompt's cut is pi-tuck's own and never comes here.
537
586
  const saved = ctx.sessionManager.buildSessionProjection().messages;
538
- const host = hostTokens(ctx);
587
+ const hostInput = hostMessages(ctx), host = conversationTokens(hostInput);
539
588
  const system = requestSystemPart(ctx.getSystemPrompt(), pi.getActiveTools(), saved, ctx.getSystemPrompt());
540
589
  const mandatory = system + host, band = decisionBand(ctx, room, system);
541
590
  const before = plannerMeasureFor(ctx)(saved);
@@ -561,7 +610,8 @@ export async function installCompaction(pi, options = {}) {
561
610
  // Every cut asked while no run is admitted (an operator's cut) is planned for a run that has not begun: the session has moved on
562
611
  // from any run that ended. A retry is inside its run.
563
612
  const draft = tuckDraft(tuck, event, ctx, reserve, plannerMeasureFor(ctx), options.recallPolicy, retain, operatorManual, operatorManual || !admittedRun, admittedRun ? protectedPrompt(ctx) : undefined);
564
- return draft.compaction ? { draft: true, after: draft.after, mode: draft.mode, compaction: draft.compaction, reserve }
613
+ return draft.compaction ? { draft: true, after: draft.after, mode: draft.mode, compaction: draft.compaction, reserve,
614
+ messages: previewCut(ctx, { type: "compaction", ...draft.compaction, tokensBefore: before }).buildSessionProjection().messages }
565
615
  : { draft: false, capacityRejected: Boolean(draft.capacityRejected), reason: draft.unavailable };
566
616
  },
567
617
  });
@@ -569,15 +619,14 @@ export async function installCompaction(pi, options = {}) {
569
619
  let decision = decide();
570
620
  const { hardLine } = limits(ctx);
571
621
  if (!operatorManual && hardLine > room && decision.kind === "fault") {
572
- if (fits(before + mandatory, hardLine, ctx))
573
- return { cancel: true };
574
- decision = decide(hardLine, hardLine);
622
+ decision = fits(before + mandatory, hardLine, ctx) ? { kind: "none" } : decide(hardLine, hardLine);
623
+ decision = clampAboveRoom(decision, ctx, room, hardLine, candidate => (candidate.kind === "cut" ? candidate.plan.after : before) + mandatory, candidate => projectedRequest(candidate.kind === "cut" ? candidate.plan.messages : saved, ctx, hostInput, [], ctx.getSystemPrompt(), pi.getActiveTools(), candidate.kind === "cut"), target => target <= room ? decide() : decide(target, target), () => deepestDecision(ctx, operatorManual || !admittedRun, retain));
575
624
  }
576
625
  if (decision.kind === "cut") {
577
626
  return { compaction: captureDraft(decision.plan.compaction, ctx, decision.plan.reserve, event.reason, event.preparation.tokensBefore, retain, operatorManual, event.preparation.previousSummary, { response, host, mandatory, target: decision.target, margin: room - band }) };
578
627
  }
579
628
  // An opportunistic cut inside the band that cannot reach it is no fault: the request goes to the guard.
580
- if (decision.kind === "none")
629
+ if (decision.kind === "none" || decision.kind === "defer")
581
630
  return { cancel: true };
582
631
  recorder.stop("no-cut");
583
632
  ctx.ui.notify(decision.message, "warning");
@@ -609,21 +658,78 @@ export async function installCompaction(pi, options = {}) {
609
658
  syncHistoryTools(ctx); });
610
659
  pi.on("turn_start", (_event, ctx) => syncHistoryTools(ctx));
611
660
  pi.on("agent_settled", (_event, ctx) => syncHistoryTools(ctx));
661
+ // A reply that stopped at the output limit may be cut off, and Pi says nothing. Tell the operator once per run, on the operator's side only:
662
+ // the notice never enters the model's context. Decided at the end of the run, so a later reply in the same run (a retry or a continuation)
663
+ // that ends normally clears it.
664
+ let lengthStop = false;
665
+ pi.on("agent_start", () => { lengthStop = false; });
666
+ pi.on("message_end", event => { if (event.message.role === "assistant")
667
+ lengthStop = event.message.stopReason === "length"; });
668
+ pi.on("agent_settled", (_event, ctx) => {
669
+ if (!lengthStop || !active())
670
+ return;
671
+ lengthStop = false;
672
+ const message = "pi-tuck: the last reply stopped at the model's output limit and may be cut off. Ask the agent to continue.";
673
+ try {
674
+ if (ctx.hasUI === false)
675
+ process.stderr.write(message + "\n");
676
+ else
677
+ ctx.ui.notify(message, "warning");
678
+ }
679
+ catch { /* ended */ } // with no UI (pi -p), stderr is the operator's
680
+ });
612
681
  // The session as it will be once a cut that is not saved yet is saved, and its conversation as the next request carries it.
613
- const cutSession = (ctx, cut) => {
682
+ const previewCut = (ctx, cut) => {
614
683
  const header = ctx.sessionManager.getHeader();
615
684
  if (!header)
616
685
  throw new Error("Missing native session header for the prompt's cut");
617
686
  const preview = SessionManager.inMemory(ctx.cwd, undefined, [header, ...ctx.sessionManager.getBranch()]);
618
- preview.appendCompaction(cut.summary, keptFrom(cut, ctx), 0, cut.details, true);
687
+ preview.appendCompaction(cut.summary, cut.firstKeptEntryId, 0, cut.details, true);
619
688
  return preview;
620
689
  };
690
+ const cutSession = (ctx, cut) => previewCut(ctx, { ...cut, firstKeptEntryId: keptFrom(cut, ctx) });
621
691
  const withCut = (ctx, cut) => cutSession(ctx, cut).buildSessionProjection().messages;
692
+ // The shared final planner: preserve the compiler's prompt/protocol floor and spend nothing on optional history. Fresh text results
693
+ // get the smallest existing head/tail + recall-locator representation; their originals remain in the session, as for ordinary clips.
694
+ const deepestDecision = (ctx, newPrompt, retain) => {
695
+ const saved = ctx.sessionManager.buildSessionProjection().messages;
696
+ if (!newPrompt) {
697
+ const at = saved.findLastIndex(message => message.role === "assistant" && !isDiscardedResponse(message));
698
+ const response = saved[at];
699
+ const calls = new Set(response?.role === "assistant" ? response.content.flatMap(block => block.type === "toolCall" ? [block.id] : []) : []);
700
+ const results = saved.slice(at + 1).filter((message) => message.role === "toolResult" && calls.has(message.toolCallId));
701
+ const entryIdOf = (id) => ctx.sessionManager.getBranch().findLast(entry => entry.type === "message" && entry.message.role === "toolResult" && entry.message.toolCallId === id)?.id;
702
+ for (const result of results) {
703
+ let low = 0, high = conversationTokens([result]) - 1;
704
+ if (!clipFor([result], entryIdOf, high).length)
705
+ continue;
706
+ while (low < high) {
707
+ const middle = Math.floor((low + high) / 2);
708
+ if (clipFor([result], entryIdOf, middle).length)
709
+ high = middle;
710
+ else
711
+ low = middle + 1;
712
+ }
713
+ for (const clip of clipFor([result], entryIdOf, low))
714
+ clips.set(clip.toolCallId, clip);
715
+ }
716
+ }
717
+ const before = measureFor(ctx)(saved);
718
+ const draft = tuckObservedDraft(tuck, { branchEntries: ctx.sessionManager.getBranch(), tokensBefore: before,
719
+ previousSummary: saved.find(message => message.role === "compactionSummary")?.summary, newPrompt, reRenderSummary: true,
720
+ protectedPromptEntryId: newPrompt ? undefined : protectedPrompt(ctx), deepest: true }, ctx, 0, plannerMeasureFor(ctx), options.recallPolicy, retain);
721
+ if (!draft.compaction)
722
+ return { kind: "fault", capacity: Boolean(draft.capacityRejected), message: draft.unavailable };
723
+ const messages = previewCut(ctx, { type: "compaction", ...draft.compaction }).buildSessionProjection().messages;
724
+ assertNativeToolRounds(messages);
725
+ return { kind: "cut", target: "room", plan: { draft: true, after: measureFor(ctx)(messages), mode: draft.mode,
726
+ compaction: draft.compaction, reserve: 0, messages } };
727
+ };
622
728
  // The cut before a run, decided on `input` while the session is idle; before_agent_start decides only for a run no input started (one a
623
729
  // custom message starts). The cut is pi-tuck's own, planned as at a tool boundary. It is not Pi's: Pi's cut finder keeps the last
624
730
  // keepRecentTokens, and where those are the whole conversation it has nothing to cut, so every later prompt was refused for good (t06 at
625
731
  // Pi's defaults on a 32k window). Each request of the run carries the cut, and the run's first boundary saves it.
626
- const preflight = (ctx, incomingPart, observedPrompt, tools, hostSnapshot) => {
732
+ const preflight = (ctx, incomingPart, incomingMessages, observedPrompt, tools, hostSnapshot) => {
627
733
  promptCut = undefined;
628
734
  promptCutAt = ctx.sessionManager.getBranch().length;
629
735
  if (!active())
@@ -638,7 +744,7 @@ export async function installCompaction(pi, options = {}) {
638
744
  const saved = ctx.sessionManager.buildSessionProjection().messages;
639
745
  const system = requestSystemPart(observedPrompt, tools, saved, ctx.getSystemPrompt());
640
746
  const band = decisionBand(ctx, room, system);
641
- const host = hostSnapshot ?? hostTokens(ctx), measure = measureFor(ctx), before = measure(saved);
747
+ const hostInput = hostSnapshot ?? hostMessages(ctx), host = conversationTokens(hostInput), measure = measureFor(ctx), before = measure(saved);
642
748
  const incoming = incomingPart + system, mandatory = incoming + host;
643
749
  observeBudget(before + mandatory, ctx);
644
750
  estimatedTokens = corrected(before + mandatory, ctx);
@@ -662,15 +768,15 @@ export async function installCompaction(pi, options = {}) {
662
768
  return { draft: false, capacityRejected: Boolean(draft.capacityRejected), reason: draft.unavailable };
663
769
  const messages = withCut(ctx, { type: "compaction", ...draft.compaction, tokensBefore: before });
664
770
  assertNativeToolRounds(messages);
665
- return { draft: true, after: measure(messages), mode: draft.mode, compaction: draft.compaction, reserve };
771
+ return { draft: true, after: measure(messages), mode: draft.mode, compaction: draft.compaction, reserve, messages };
666
772
  },
667
773
  });
668
774
  let decision = gate.kind === "plan" ? decide(room, band, gate.opportunistic) : { kind: "defer" };
669
775
  const { hardLine } = limits(ctx);
670
776
  if (hardLine > room && (decision.kind === "defer" || decision.kind === "fault")) {
671
- if (fits(before + mandatory, hardLine, ctx))
672
- return;
673
- decision = fits(mandatory, hardLine, ctx) ? decide(hardLine, hardLine, false) : { kind: "defer" };
777
+ decision = fits(before + mandatory, hardLine, ctx) ? { kind: "none" } :
778
+ fits(mandatory, hardLine, ctx) ? decide(hardLine, hardLine, false) : { kind: "defer" };
779
+ decision = clampAboveRoom(decision, ctx, room, hardLine, candidate => (candidate.kind === "cut" ? candidate.plan.after : before) + mandatory, candidate => projectedRequest(candidate.kind === "cut" ? candidate.plan.messages : saved, ctx, hostInput, incomingMessages, observedPrompt, tools, candidate.kind === "cut"), target => decide(target, target <= room ? band : target, false), () => deepestDecision(ctx, true, false));
674
780
  }
675
781
  if (decision.kind === "defer")
676
782
  return;
@@ -693,7 +799,8 @@ export async function installCompaction(pi, options = {}) {
693
799
  if (prompted)
694
800
  access(); // decided on input; a failure latched there still stops the run
695
801
  else
696
- await preflight(ctx, incomingEventTokens(event), event.systemPrompt, historyToolNames(pi, event.systemPromptOptions.selectedTools, historyToolsEnabled(ctx)));
802
+ await preflight(ctx, incomingEventTokens(event), [{ role: "user", timestamp: 0,
803
+ content: event.messageType === "custom" ? event.message.content : [{ type: "text", text: event.prompt }, ...(event.images ?? [])] }], event.systemPrompt, historyToolNames(pi, event.systemPromptOptions.selectedTools, historyToolsEnabled(ctx)));
697
804
  // Pi declares tool changes before turn_start, then snapshots executable tools in prepareRequest. Introduce our tools in
698
805
  // the request projection: recording an activation message here would shift every later session-global recall index.
699
806
  if (historyToolsEnabled(ctx))
@@ -720,11 +827,11 @@ export async function installCompaction(pi, options = {}) {
720
827
  const message = event.message;
721
828
  const incoming = incomingTokens(typeof message.content === "string" ? message.content : message.content.flatMap(b => b.type === "text" ? [b.text] : []).join("\n"), typeof message.content === "string" ? [] : message.content.filter((b) => b.type === "image"));
722
829
  const saved = ctx.sessionManager.buildSessionProjection().messages;
723
- const host = hostTokens(ctx);
830
+ const hostInput = hostMessages(ctx), host = conversationTokens(hostInput);
724
831
  const system = requestSystemPart(ctx.getSystemPrompt(), pi.getActiveTools(), saved, ctx.getSystemPrompt());
725
832
  if (fits(conversationTokens(saved) + system + incoming + host, limits(ctx).room, ctx))
726
833
  return;
727
- preflight(ctx, incoming, ctx.getSystemPrompt(), pi.getActiveTools(), host);
834
+ preflight(ctx, incoming, [message], ctx.getSystemPrompt(), pi.getActiveTools(), hostInput);
728
835
  syncHistoryTools(ctx);
729
836
  }
730
837
  catch (error) {
@@ -750,7 +857,7 @@ export async function installCompaction(pi, options = {}) {
750
857
  // request while tools still run, so it can be counted early: conservative, and nothing else would cut at that later boundary.
751
858
  const system = requestSystemPart(ctx.getSystemPrompt(), pi.getActiveTools(), saved, ctx.getSystemPrompt());
752
859
  const band = decisionBand(ctx, room, system);
753
- const host = hostTokens(ctx);
860
+ const hostInput = hostMessages(ctx), host = conversationTokens(hostInput);
754
861
  const queued = event.context.pendingMessages.length ? conversationTokens(event.context.pendingMessages) : 0;
755
862
  const mandatory = system + host + queued;
756
863
  // Without usable provider usage, Pi's own threshold uses its unwrapped message estimate as the compiler's density input.
@@ -777,12 +884,13 @@ export async function installCompaction(pi, options = {}) {
777
884
  let floorKept;
778
885
  let wholePromptFloor;
779
886
  const protectedId = reason === "tool-loop" ? protectedPrompt(ctx) : undefined;
780
- const decide = (targetRoom = room, targetBand = band) => {
887
+ const decide = (targetRoom = room, targetBand = band, required = false) => {
781
888
  previews = [];
782
889
  floorKept = undefined;
783
890
  wholePromptFloor = undefined;
784
891
  const before = measure(saved);
785
- const gate = gateCut({ total: before + mandatory, mandatory, room: targetRoom, band: targetBand, fits: (tokens, limit) => fits(tokens, limit, ctx) });
892
+ const gate = required && fits(mandatory, targetRoom, ctx) ? { kind: "plan", opportunistic: false } :
893
+ gateCut({ total: before + mandatory, mandatory, room: targetRoom, band: targetBand, fits: (tokens, limit) => fits(tokens, limit, ctx) });
786
894
  if (gate.kind !== "plan")
787
895
  return gate;
788
896
  return chooseCut({
@@ -811,7 +919,7 @@ export async function installCompaction(pi, options = {}) {
811
919
  const messages = preview.buildSessionProjection().messages;
812
920
  assertNativeToolRounds(messages);
813
921
  previews.push(messages);
814
- return { draft: true, after: measure(messages), mode: draft.mode, compaction: draft.compaction, reserve };
922
+ return { draft: true, after: measure(messages), mode: draft.mode, compaction: draft.compaction, reserve, messages };
815
923
  },
816
924
  });
817
925
  };
@@ -821,7 +929,7 @@ export async function installCompaction(pi, options = {}) {
821
929
  // clipBudgets states the budgets tried and why they are counted in the estimate's units; `kept` is measured from the previews this decision
822
930
  // built, so the second budget is the cut's own floor and not a new formula. When nothing is clipped or the decision still faults, the
823
931
  // refusal stands.
824
- const tryClips = (clipRoom, targetBand) => {
932
+ const tryClips = (clipRoom, targetBand, required = false) => {
825
933
  if (decision.kind !== "fault" || !event.toolResults)
826
934
  return;
827
935
  const branch = ctx.sessionManager.getBranch();
@@ -834,7 +942,7 @@ export async function installCompaction(pi, options = {}) {
834
942
  continue;
835
943
  for (const clip of round)
836
944
  clips.set(clip.toolCallId, clip);
837
- const clippedDecision = decide(clipRoom, targetBand);
945
+ const clippedDecision = decide(clipRoom, targetBand, required);
838
946
  if (clippedDecision.kind === "fault")
839
947
  for (const clip of round)
840
948
  clips.delete(clip.toolCallId);
@@ -851,9 +959,17 @@ export async function installCompaction(pi, options = {}) {
851
959
  const { hardLine } = limits(ctx);
852
960
  if (hardLine > room && (decision.kind === "defer" || decision.kind === "fault")) {
853
961
  if (fits(before + mandatory, hardLine, ctx))
854
- return;
855
- decision = decide(hardLine, hardLine);
856
- tryClips(hardLine, hardLine);
962
+ decision = { kind: "none" };
963
+ else {
964
+ decision = decide(hardLine, hardLine);
965
+ tryClips(hardLine, hardLine);
966
+ }
967
+ decision = clampAboveRoom(decision, ctx, room, hardLine, candidate => (candidate.kind === "cut" ? candidate.plan.after : before) + mandatory, candidate => projectedRequest(candidate.kind === "cut" ? candidate.plan.messages : saved, ctx, hostInput, event.context.pendingMessages, ctx.getSystemPrompt(), pi.getActiveTools(), candidate.kind === "cut"), target => {
968
+ const targetBand = target <= room ? band : target;
969
+ decision = decide(target, targetBand, target > room);
970
+ tryClips(target, targetBand, target > room);
971
+ return decision;
972
+ }, () => deepestDecision(ctx, false, true));
857
973
  }
858
974
  if (decision.kind === "fault" && reason === "tool-loop") {
859
975
  if (wholePromptFloor !== undefined && !fits(wholePromptFloor, hardLine, ctx))
@@ -880,10 +996,71 @@ export async function installCompaction(pi, options = {}) {
880
996
  setImmediate(() => { semantic.indexAtCut(semanticProvider(ctx)); });
881
997
  return { entries: [...event.entries, saved, ...(more?.entries ?? [])] };
882
998
  };
999
+ const retryThinking = (event, ctx) => {
1000
+ // Pi's supported boundary drafts commit before finishTurn honors `continue`. Omitting the selected failed response also prevents
1001
+ // Pi's later native length recovery from retrying it independently. The stored message and its output usage stay on the branch.
1002
+ const entries = [...(atBoundary(event, ctx, "run-end", false)?.entries ?? event.entries),
1003
+ { type: "context_edit", targetId: event.messageEntryId, replacement: null }];
1004
+ if (thinkingRetryPending)
1005
+ return { entries, continue: false };
1006
+ const header = ctx.sessionManager.getHeader();
1007
+ if (!header)
1008
+ throw new Error("Missing native session header for the thinking-only retry");
1009
+ const preview = SessionManager.inMemory(ctx.cwd, undefined, [header, ...ctx.sessionManager.getBranch()]);
1010
+ for (const entry of entries) {
1011
+ if (entry.type === "context_edit")
1012
+ preview.appendContextEdit(entry.targetId, entry.replacement);
1013
+ else if (entry.type === "compaction")
1014
+ preview.appendCompaction(entry.summary, entry.firstKeptEntryId, 0, entry.details, true, entry.usage);
1015
+ else if (entry.type === "custom")
1016
+ preview.appendCustomEntry(entry.customType, entry.data);
1017
+ else
1018
+ preview.appendCustomMessageEntry(entry.customType, entry.content, entry.display, entry.details);
1019
+ }
1020
+ const view = Object.create(ctx, { sessionManager: { value: preview } });
1021
+ const hostInput = hostMessages(ctx), host = conversationTokens(hostInput);
1022
+ // The deepest cut only when it buys reply room: where Pi's cap on the request as it stands (the stopped reply left out) is already
1023
+ // as large as the deepest cut's, the turn is retried uncut, and no history is given up for nothing.
1024
+ const asIs = projectedRequest(preview.buildSessionProjection().messages, view, hostInput, event.context.pendingMessages, ctx.getSystemPrompt(), pi.getActiveTools(), false);
1025
+ const asIsCheck = requestClamp(asIs, view);
1026
+ const previousClips = new Map(clips);
1027
+ const decision = deepestDecision(view, false, true);
1028
+ if (decision.kind === "cut" && asIsCheck.cap !== undefined && asIsCheck.leavesFloor &&
1029
+ requestClamp(projectedRequest(decision.plan.messages, view, hostInput, event.context.pendingMessages, ctx.getSystemPrompt(), pi.getActiveTools(), true), view).cap <= asIsCheck.cap) {
1030
+ clips.clear();
1031
+ for (const [id, clip] of previousClips)
1032
+ clips.set(id, clip);
1033
+ const { room, hardLine } = limits(view);
1034
+ thinkingRetryPending = true;
1035
+ return { entries: [...entries, { type: "custom", customType: "pi-tuck-capacity", data: { kind: "retry", stoppedReplyEntryId: event.messageEntryId,
1036
+ stoppedOutputTokens: event.message.role === "assistant" ? event.message.usage?.output ?? 0 : 0, cut: false,
1037
+ piEstimateTokens: asIsCheck.estimate, piCap: asIsCheck.cap, room, hardLine, buildId: BUILD_ID } }], continue: true };
1038
+ }
1039
+ if (decision.kind !== "cut")
1040
+ throw new CapacityError(clampMessage);
1041
+ const messages = projectedRequest(decision.plan.messages, view, hostInput, event.context.pendingMessages, ctx.getSystemPrompt(), pi.getActiveTools(), true);
1042
+ if (runPrompt !== undefined && !carriesPrompt(messages, runPrompt))
1043
+ throw promptRefusal();
1044
+ const system = requestSystemPart(ctx.getSystemPrompt(), pi.getActiveTools(), messages, ctx.getSystemPrompt());
1045
+ const tokens = system + conversationTokens(messages), check = requestClamp(messages, view);
1046
+ const { response, room, hardLine } = limits(view);
1047
+ if (!fits(tokens, hardLine, view) || check.cap === undefined || !check.leavesFloor)
1048
+ throw new CapacityError(clampMessage);
1049
+ const compaction = captureDraft(decision.plan.compaction, view, decision.plan.reserve, "length-retry", tokens, true, false, undefined, { response, host, mandatory: system + host + conversationTokens(event.context.pendingMessages), target: decision.target, margin: room - decisionBand(view, room, system) });
1050
+ thinkingRetryPending = true;
1051
+ pendingBoundaryCommit = true;
1052
+ return { entries: [...entries, { type: "compaction", summary: compaction.summary, firstKeptEntryId: compaction.firstKeptEntryId || null, details: compaction.details },
1053
+ { type: "custom", customType: "pi-tuck-capacity", data: { kind: "retry", stoppedReplyEntryId: event.messageEntryId,
1054
+ stoppedOutputTokens: event.message.role === "assistant" ? event.message.usage?.output ?? 0 : 0, cut: true,
1055
+ estimatedTokens: corrected(tokens, view), piEstimateTokens: check.estimate, piCap: check.cap, room, hardLine, buildId: BUILD_ID } }], continue: true };
1056
+ };
883
1057
  pi.on("turn_end", (event, ctx) => {
884
1058
  try {
885
1059
  if (!active() || !admittedRun)
886
1060
  return;
1061
+ if (thinkingOnlyLength(event.message) && !failure && !ctx.signal?.aborted && settingsFor().getCompactionSettings(ctx.model).enabled)
1062
+ return retryThinking(event, ctx);
1063
+ thinkingRetryPending = false;
887
1064
  const decide = event.outcome === "completed" && !ctx.signal?.aborted && event.context.canContinue && event.toolResults.length > 0 &&
888
1065
  settingsFor().getCompactionSettings(ctx.model).enabled;
889
1066
  if (!promptCut && !decide)
@@ -996,16 +1173,15 @@ export async function installCompaction(pi, options = {}) {
996
1173
  const tokens = system + conversationTokens(messages);
997
1174
  observeBudget(tokens, ctx);
998
1175
  estimatedTokens = corrected(tokens, ctx);
999
- const { room, hardLine, replyFloor } = limits(ctx);
1176
+ const { room, hardLine } = limits(ctx);
1000
1177
  if (runPrompt !== undefined && !carriesPrompt(messages, runPrompt))
1001
1178
  throw new Refusal("Prompt check failed: the request is missing the whole text of the prompt that started this run. Shorten the prompt or choose a model with a larger context window, then retry");
1002
1179
  if (!fits(tokens, hardLine, ctx))
1003
1180
  throw new CapacityError("Complete Pi context and response reserve exceed the estimated request budget");
1004
1181
  // Pi's request estimator and its compaction estimator need not agree. The hard line is
1005
1182
  // necessary; above the room, also require the real request clamp to preserve the floor.
1006
- if (!options.requestReserve && estimatedTokens > room && piClamp &&
1007
- piClamp(ctx.model, normalizeContext({ messages: convertToLlm(messages) }), ctx.model.maxTokens) < replyFloor)
1008
- throw new CapacityError("Pi's request clamp leaves less than the reply floor");
1183
+ if (!options.requestReserve && estimatedTokens > room && piClamp && !requestClamp(messages, ctx).leavesFloor)
1184
+ throw new CapacityError(clampMessage);
1009
1185
  if (estimatedTokens > room)
1010
1186
  receipt("above-room", "Request exceeds the room and fits the hard line", ctx);
1011
1187
  if (recorder.enabled && !recorder.waitingForCut)
@@ -1,6 +1,6 @@
1
1
  import { type RecallPolicy } from './recall.js';
2
2
  import type { SessionEntry } from '@earendil-works/pi-coding-agent';
3
- export declare const DIAGNOSTIC_BUILD = "c3eefa3bb76e52635b1e8394a025d3ffd6a056fc5636ed9b35f89dfddc1a322a";
3
+ export declare const DIAGNOSTIC_BUILD = "48d3f777f203edb56b29154215540eb8245182237d18cdc50c6cbe704ea28e7e";
4
4
  export interface Receipt {
5
5
  id: string;
6
6
  build: string;
@@ -1,11 +1,15 @@
1
1
  import type { SessionEntry } from '@earendil-works/pi-coding-agent';
2
- import type { Messages } from './budget.js';
2
+ import type { AgentMessage, Messages } from './budget.js';
3
3
  /** Read Pi's private safety allowance through its public clamp, rather than copying it.
4
4
  * Older/unavailable subpaths fall back to Pi 1.2–1.5's documented 4,096-token allowance.
5
5
  */
6
6
  export declare function piContextSafetyMargin(): Promise<number>;
7
7
  /** The smallest reply room a request above the room must leave after Pi's own clamp: above the user's p99 reply (3,270 tokens). */
8
8
  export declare const REPLY_FLOOR_MIN = 3584;
9
+ /** A spent thinking budget that produced neither a visible answer nor work. Empty/text/tool replies keep the existing length recovery. */
10
+ export declare const thinkingOnlyLength: (message: AgentMessage) => boolean;
11
+ /** Thinking-only exhaustion uses the observed largest reply, not the full reserve. Every other failed length/overflow keeps its override. */
12
+ export declare const needsFullReplyReserve: (message: AgentMessage) => boolean;
9
13
  /** Read the whole current branch, including replies before cuts. No session-local fallback flag. */
10
14
  export declare function branchReplyFloor(branch: readonly SessionEntry[], reserve: number): number;
11
15
  /** Admission never promises more output than a model with a positive reply cap can produce. */
@@ -20,20 +20,29 @@ export async function piContextSafetyMargin() {
20
20
  }
21
21
  /** The smallest reply room a request above the room must leave after Pi's own clamp: above the user's p99 reply (3,270 tokens). */
22
22
  export const REPLY_FLOOR_MIN = 3_584;
23
+ /** A spent thinking budget that produced neither a visible answer nor work. Empty/text/tool replies keep the existing length recovery. */
24
+ export const thinkingOnlyLength = (message) => message.role === 'assistant' && message.stopReason === 'length' &&
25
+ message.content.length > 0 && message.content.every(block => block.type === 'thinking');
26
+ /** Thinking-only exhaustion uses the observed largest reply, not the full reserve. Every other failed length/overflow keeps its override. */
27
+ export const needsFullReplyReserve = (message) => message.role === 'assistant' &&
28
+ (message.stopReason === 'length' && !thinkingOnlyLength(message) || message.stopReason === 'error' && isContextOverflow(message));
23
29
  /** Read the whole current branch, including replies before cuts. No session-local fallback flag. */
24
30
  export function branchReplyFloor(branch, reserve) {
25
- let largest = REPLY_FLOOR_MIN;
31
+ let largest = REPLY_FLOOR_MIN, thinkingStop = false;
26
32
  for (const entry of branch) {
27
33
  if (entry.type !== 'message' || entry.message.role !== 'assistant')
28
34
  continue;
29
35
  const message = entry.message;
30
- if (message.stopReason === 'length' || message.stopReason === 'error' && isContextOverflow(message))
36
+ if (needsFullReplyReserve(message))
31
37
  return reserve;
38
+ thinkingStop ||= thinkingOnlyLength(message);
32
39
  const output = message.usage?.output;
33
40
  if (typeof output === 'number' && Number.isFinite(output) && output > largest)
34
41
  largest = output;
35
42
  }
36
- return Math.min(reserve, Math.ceil(largest));
43
+ // Include the exhausted thinking reply itself, even across later cuts/restarts. Its observed output must fit again;
44
+ // requestReplyFloor applies the model cap. Ordinary branches retain their existing reserve cap.
45
+ return thinkingStop ? Math.ceil(largest) : Math.min(reserve, Math.ceil(largest));
37
46
  }
38
47
  /** Admission never promises more output than a model with a positive reply cap can produce. */
39
48
  export function requestReplyFloor(branch, reserve, maxTokens) {