pi-tuck 1.0.27 → 1.0.28

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/src/compaction.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { textOf } from "./tuck/core/content.js";
2
- import { requestReplyFloor, carriesPrompt, needsFullReplyReserve, thinkingOnlyLength, piContextSafetyMargin, loadPiRequestMath } from "./request-capacity.js";
2
+ import { PendingCut, StartingPrompt, createRun, type CutEntry, type Run } from "./run.js";
3
+ import { requestReplyFloor, replyFit, carriesPrompt, thinkingOnlyLength, piContextSafetyMargin, loadPiRequestMath } from "./request-capacity.js";
3
4
  import { BUILD_ID } from "./build-id.js";
4
5
  import { createSemanticRecall, budgetSemantic, type SemanticRecallController } from "./semantic-recall";
5
6
  import { DIAGNOSTIC_BUILD, IncidentRecorder, attachReceipt, permittedEntries, receiptId, findReceiptEntry, type Receipt, type RecorderOptions } from "./diagnostics.js";
@@ -12,7 +13,7 @@ import { applyClips, clipBudgets, clipFor, type Clip } from "./clip.js";
12
13
  import { DEFAULT_CUT_AT, bandOf, chooseCut, conversationTokens, gateCut, incomingEventTokens, incomingTokens, measurableConversationTokens, planningReserve, projectedConversationTokens, systemPart, validCutAt,
13
14
  type AgentMessage, type Decision, type Messages, type Plan, type Target } from "./budget.js";
14
15
  import { getCurrentSystemMessage, isContextOverflow, normalizeContext, type ImageContent, type ToolResultMessage } from "@earendil-works/pi-ai";
15
- import { calculateContextTokens, convertToLlm, estimateTokens, shouldCompact, SessionManager, SettingsManager, type ExtensionAPI, type ExtensionContext, type SessionEntry, type SessionBoundaryDraft, type TurnEndEvent } from "@earendil-works/pi-coding-agent";
16
+ import { calculateContextTokens, convertToLlm, estimateTokens, shouldCompact, SettingsManager, type ExtensionAPI, type ExtensionContext, type SessionEntry, type SessionBoundaryDraft, type TurnEndEvent } from "@earendil-works/pi-coding-agent";
16
17
  import { Type } from "typebox";
17
18
  import { Check } from "typebox/value";
18
19
  import type { RecallPolicy } from "./recall.js";
@@ -22,7 +23,7 @@ import { tuck, tuckDraft, tuckObservedDraft, tuckRecall } from "./tuck.js";
22
23
  import { LOADED_VERSION, statusText } from "./status.js";
23
24
  import { ImageRecallInput, historyImage, historyAround } from "./history-image.js";
24
25
  import { eligibilityKey, eligibilityKeyMoved, memoizedEligibleSources } from "./eligibility-cache";
25
- import { applyHistoryTools, hasTuckCut, historyToolDeclarations, historyToolNames, stageHistoryTools } from "./history-tools.js";
26
+ import { applyHistoryTools, historyToolDeclarations, historyToolNames, stageHistoryTools } from "./history-tools.js";
26
27
 
27
28
  const query = Type.String({ minLength: 1, maxLength: MAX_RECALL_QUERY_CHARACTERS });
28
29
  const entryId = Type.String({ minLength: 1, maxLength: MAX_RECALL_ENTRY_ID_CHARACTERS });
@@ -47,7 +48,7 @@ const RecallInput = Type.Union([
47
48
  const SearchInput = Type.Object({query,priorResults:Type.Optional(Type.Array(Type.Object({sourceSha:Type.String({minLength:64,maxLength:64}),read:Type.Object({entryId,offset:Type.Integer({minimum:0}),limit:Type.Integer({minimum:0})},{additionalProperties:false})},{additionalProperties:false}),{maxItems:20}))},{additionalProperties:false});
48
49
  const reply = (value: unknown) => ({ content: [{ type: "text" as const, text: JSON.stringify(value) }], details: {} });
49
50
  /** The compactor's verdict that this attempt cannot be made: the cut it needs cannot be made, or the request cannot fit. Like every failure but
50
- * a `CommitFault`, it ends with its attempt (`forgetFailure`), and the next attempt plans afresh from the session as it is. */
51
+ * a `CommitFault`, it ends with its attempt (`resetRun`), and the next attempt plans afresh from the session as it is. */
51
52
  class Refusal extends Error {}
52
53
  /** A cut this compactor supplied that Pi then failed to commit. Pi appends a cut to the session in memory before it writes the file, so the
53
54
  * session may hold a cut its file lacks; what is written after would build on it. The one failure that stays until the session restarts. */
@@ -150,11 +151,16 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
150
151
  // Semantic indexing outlives any one attempt, so it checks only that the host still grants access: a compactor failure, which ends with its
151
152
  // attempt, must not read as a revocation (that disables semantic recall and deletes its index for the rest of the session).
152
153
  // A prompt's cut reaches the requests at once but the session only at the end of the first turn: until then search sees the history as the model does.
153
- const semanticProvider = (ctx: ExtensionContext) => () => memoizedEligibleSources(ctx, options.recallPolicy, hostAccess, promptCut ? promptCut.firstKeptEntryId || ctx.sessionManager.getBranch().findLast(e => e.type === "message" && e.message.role === "user")?.id : undefined);
154
+ const view = (ctx: ExtensionContext, threshold = false) => (threshold && run?.expired ? liveCut : run?.cut ?? liveCut).view(ctx);
155
+ const liveCut = new PendingCut();
156
+ const semanticProvider = (ctx: ExtensionContext) => () => {
157
+ const current = view(ctx);
158
+ return memoizedEligibleSources(ctx, options.recallPolicy, hostAccess, current.pending ? current.keptFromId : undefined);
159
+ };
154
160
  const recorder = new IncidentRecorder(options.diagnostics);
155
161
  let proposalId: string | undefined;
156
162
  const observe = (ctx: ExtensionContext) => recorder.measureInline(() => {
157
- if (!active() || failure) return;
163
+ if (!active() || failure()) return;
158
164
  if (!proposalId) return;
159
165
  const found = findReceiptEntry(ctx.sessionManager.getBranch(), proposalId);
160
166
  if (found) { recorder.capture("observed-entry", "current-native-branch-memory-not-durable", { entryId: found.id, diagnosticReceiptId: proposalId, details: found.type === "compaction" ? found.details : undefined }); recorder.cut(); proposalId = undefined; }
@@ -214,17 +220,18 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
214
220
  ctx.ui.notify(JSON.stringify({...semanticStatus(),...(errors.length?{errors}:{})}),errors.length?"warning":"info");
215
221
  }});
216
222
  pi.registerCommand("tuck", { description: "pi-tuck's state in this session: version, cuts, cut point, search", handler: async (_args, ctx) => {
217
- const { state, lastError } = semantic.status();
218
- ctx.ui.notify(statusText({ version: LOADED_VERSION, active: active(), branch: ctx.sessionManager.getBranch(), cutAt: options.cutAt, search, semantic: { state, lastError } }), "info");
223
+ const state = semantic.status();
224
+ ctx.ui.notify(statusText({ version: LOADED_VERSION, active: active(), branch: ctx.sessionManager.getBranch(), cutAt: options.cutAt, search, semantic: state, pending: view(ctx).pending }), "info");
219
225
  } });
220
226
  // Pi's native start/settled events bracket the run and its post-turn cuts.
221
- let admittedRun = false;
222
- let runPrompt: string | undefined;
223
- let runPromptMessageType: "user" | "custom" = "user";
224
- let thinkingRetryPending = false;
225
- const protectedPrompt = (ctx: ExtensionContext) => runPrompt ? ctx.sessionManager.getBranch().findLast(entry =>
226
- runPromptMessageType === "custom" ? entry.type === "custom_message" && textOf(entry.content) === runPrompt :
227
- entry.type === "message" && entry.message.role === "user" && textOf(entry.message.content).includes(runPrompt!))?.id : undefined;
227
+ let run: Run | undefined;
228
+ // A native append may have mutated memory before its disk write failed. This session fault outlives the run.
229
+ let commitFault: Error | undefined;
230
+ const failure = () => commitFault ?? run?.failure;
231
+ const protectedPrompt = (ctx: ExtensionContext) => run?.prompt?.id(ctx.sessionManager.getBranch());
232
+ const clips = () => run?.clips ?? emptyClips;
233
+ const emptyClips = new Map<string, Clip>();
234
+ const resetRun = () => { run = undefined; pending = undefined; };
228
235
  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
236
  let estimatedTokens = 0;
230
237
  // One serial request, one accounting identity; observations never certify new content. A new identity (the first request after a session
@@ -259,7 +266,7 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
259
266
  const paired = pending; pending = undefined;
260
267
  const key = identityFor(ctx), message = event.message;
261
268
  const overflow = message.stopReason === "error" && isContextOverflow(message);
262
- if (!paired || !key || paired.identity !== key || !active() || failure || ctx.signal?.aborted || (message.errorMessage && !overflow) ||
269
+ if (!paired || !key || paired.identity !== key || !active() || failure() || ctx.signal?.aborted || (message.errorMessage && !overflow) ||
263
270
  message.provider !== ctx.model?.provider || message.model !== ctx.model.id || message.api !== ctx.model.api ||
264
271
  (!overflow && !["stop", "toolUse", "length"].includes(message.stopReason)) ||
265
272
  !isDeepStrictEqual(convertToLlm(paired.messages), paired.snapshot)) return;
@@ -284,8 +291,19 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
284
291
  // 6,000-token prompt refuse. The overflow branch above learns from a refusal, where the count is a lower bound, and the ceiling is unchanged.
285
292
  if (input > 0 && input <= ctx.model.contextWindow && Number.isSafeInteger(input) && Number.isFinite(ratio)) learn(ratio);
286
293
  });
287
- pi.on("agent_start", () => { admittedRun = true; thinkingRetryPending = false; pending = undefined; phase = "idle"; });
288
- pi.on("agent_settled", () => { admittedRun = false; runPrompt = undefined; thinkingRetryPending = false; pending = undefined; phase = "idle"; forgetFailure(); clips.clear(); promptCut = undefined; });
294
+ // The operator's length-stop notice is decided at settle and is not attempt state: a run reset before then (a tree move inside another
295
+ // extension's agent_settled, a failure) must not erase it. Only the final assistant reply decides it; a successful retry clears it.
296
+ let lengthStop = false;
297
+ pi.on("agent_start", () => { run ??= createRun("admitted"); run.phase = "admitted"; run.thinkingRetryPending = false; lengthStop = false; pending = undefined; });
298
+ pi.on("message_end", event => { if (event.message.role === "assistant") lengthStop = event.message.stopReason === "length"; });
299
+ pi.on("agent_settled", (_event, ctx) => {
300
+ try {
301
+ if (!lengthStop || !active()) return;
302
+ lengthStop = false;
303
+ const message = "pi-tuck: the last reply stopped at the model's output limit and may be cut off. Ask the agent to continue.";
304
+ try { if (ctx.hasUI === false) process.stderr.write(message + "\n"); else ctx.ui.notify(message, "warning"); } catch { /* ended */ }
305
+ } finally { resetRun(); }
306
+ });
289
307
  const active = () => options.active?.() ?? true;
290
308
  const settingsFor = () => options.settings ?? SettingsManager.inMemory(pi.getSettings());
291
309
  // The one reserve decision; every consumer takes its room from limits. Standalone starts at Pi's native cut point. Once a reply did
@@ -298,8 +316,7 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
298
316
  const budget = host ?? { response: settingsFor().getCompactionSettings(ctx.model).reserveTokens };
299
317
  if (!Number.isSafeInteger(budget.response) || budget.response < 0)
300
318
  throw new Error("Invalid host request reserve");
301
- const floor = host ? host.responseExact !== true : ctx.sessionManager.getBranch().some(entry =>
302
- entry.type === "message" && needsFullReplyReserve(entry.message));
319
+ const floor = host ? host.responseExact !== true : replyFit(ctx.sessionManager.getBranch()).failed;
303
320
  return { response: floor ? Math.max(budget.response, ctx.model?.maxTokens ?? 0) : budget.response };
304
321
  };
305
322
  const inputRoom = (response: number, ctx: ExtensionContext) => {
@@ -317,20 +334,20 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
317
334
  const hardLine = Math.min(Math.max(room, (ctx.model?.contextWindow ?? 0) - piMargin - floor), lastCeiling ?? Infinity);
318
335
  return { response, room, hardLine, replyFloor: floor };
319
336
  };
320
- const decisionBand = (ctx: ExtensionContext, room: number, system: number) => {
321
- const last = ctx.sessionManager.getBranch().findLast(entry => entry.type === "compaction");
337
+ const decisionBand = (ctx: ExtensionContext, room: number, system: number, current = view(ctx)) => {
338
+ const last = current.session.getBranch().findLast(entry => entry.type === "compaction");
322
339
  const after = last?.type === "compaction" ? (last.details as { tokensAfter?: unknown } | undefined)?.tokensAfter : undefined;
323
340
  return bandOf(room, cutAt, options.cutAt === undefined && typeof after === "number" && Number.isFinite(after) && after >= 0
324
341
  ? { system, tokensAfter: after, factor } : undefined);
325
342
  };
326
343
  const fits = (tokens: number, limit: number, ctx: ExtensionContext) => corrected(tokens, ctx) < limit;
327
344
  // The saved conversation and a cut's quantities are counted by the one function the guard counts with (strict, as the context hook is).
328
- const measureFor = (ctx: ExtensionContext) => (messages: Messages) => projectedConversationTokens(applyClips(messages, clips), ctx, options.recallPolicy);
345
+ const measureFor = (ctx: ExtensionContext) => (messages: Messages) => projectedConversationTokens(applyClips(messages, clips()), ctx, options.recallPolicy);
329
346
  // The planner counts with the same function, except that it does not fail on a summary in another format: a cut can replace one
330
347
  // (an operator's /compact repairs a session whose saved summary is foreign). Identical to the strict one for this compactor's summaries.
331
348
  const plannerMeasureFor = (ctx: ExtensionContext) => Object.assign(
332
- (messages: Messages) => measurableConversationTokens(applyClips(messages, clips), ctx, options.recallPolicy),
333
- { messageTokens: (message: Messages[number]) => conversationTokens(applyClips([message], clips)) });
349
+ (messages: Messages) => measurableConversationTokens(applyClips(messages, clips()), ctx, options.recallPolicy),
350
+ { messageTokens: (message: Messages[number]) => conversationTokens(applyClips([message], clips())) });
334
351
  const hostMessages = (ctx: ExtensionContext) => options.hostMessages?.(ctx) ?? [];
335
352
  const toolShapes = (names: Iterable<string>) => {
336
353
  const selected = new Set(names);
@@ -358,17 +375,17 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
358
375
  const saved = messages.some(message => message.role === "system") ? messages :
359
376
  [{ role: "system" as const, content: prompt, toolsAdded: shapes, timestamp: 0 }, ...messages];
360
377
  const declared = enabled ? historyToolDeclarations(saved, shapes, true) : saved;
361
- return applyClips([...host, ...(enabled ? projectedMessages(declared, ctx, options.recallPolicy) : declared), ...incoming], clips);
378
+ return applyClips([...host, ...(enabled ? projectedMessages(declared, ctx, options.recallPolicy) : declared), ...incoming], clips());
362
379
  };
363
380
  const clampAboveRoom = <D extends object>(decision: Decision<D>, ctx: ExtensionContext, room: number, target: number,
364
381
  total: (decision: Decision<D>) => number, project: (decision: Decision<D>) => Messages, replan: (target: number) => Decision<D>, deepest: () => Decision<D>): Decision<D> => {
365
382
  if (options.requestReserve || !piClamp) return decision;
366
383
  const lastAttempt = (): Decision<D> => {
367
- const previousClips = new Map(clips);
384
+ const previousClips = new Map(clips());
368
385
  const candidate = deepest();
369
386
  if (candidate.kind === "cut" && fits(total(candidate), limits(ctx).hardLine, ctx) && requestClamp(project(candidate), ctx).leavesFloor)
370
387
  return candidate;
371
- clips.clear(); for (const [id, clip] of previousClips) clips.set(id, clip);
388
+ clips().clear(); for (const [id, clip] of previousClips) clips().set(id, clip);
372
389
  return { kind: "fault", capacity: true, message: clampMessage };
373
390
  };
374
391
  for (let retry = 0; ; retry++) {
@@ -388,22 +405,7 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
388
405
  const isNoWork = (message: string | undefined) => ["Already compacted", "Nothing to compact (session too small)"].includes(message?.replace(/^Compaction failed: /, "") ?? "");
389
406
  // A missing model is configuration, not capacity.
390
407
  const missingModel = () => new Error("No context model for the request budget");
391
- // A prompt's cut, until its run saves it at its first boundary: every request of that run carries it (the context hook). A run that ends
392
- // before a boundary drops it, and the next prompt decides afresh.
393
- type CutEntry = { type: "compaction"; summary: string; firstKeptEntryId: string; tokensBefore: number; details?: unknown };
394
408
  type CutPlan = { compaction: NonNullable<ReturnType<typeof tuckDraft>["compaction"]>; reserve: number; messages: Messages };
395
- let promptCut: CutEntry | undefined;
396
- // Where the branch ended when the prompt's cut was planned: the prompt is the entry Pi appends there. A cut that keeps nothing of the conversation
397
- // before it names no entry to keep from, and read as it stands that drops the prompt too, so the model never sees it.
398
- let promptCutAt = 0;
399
- const keptFrom = (cut: CutEntry, ctx: ExtensionContext) => cut.firstKeptEntryId ||
400
- ctx.sessionManager.getBranch().slice(promptCutAt).find(entry => entry.type !== "custom" || entry.customType !== "pi-tuck-capacity")?.id || "";
401
- let failure: Error | undefined;
402
- let refusalReceipted = false;
403
- let pendingBoundaryCommit = false;
404
- // Fresh tool results the room cannot hold, by call id: each request of the run carries the clip in their place; the session keeps them
405
- // whole. A clip ends with its run like any failure would: the next run plans from the session as it is, where the result is no longer fresh.
406
- const clips = new Map<string, Clip>();
407
409
  const receipt = (kind: "refusal" | "above-room", reason: string, ctx: ExtensionContext) => {
408
410
  let room: number | null = null, hardLine: number | null = null;
409
411
  try { ({ room, hardLine } = limits(ctx)); } catch { /* Invalid configuration has no usable capacity. Preserve its original failure. */ }
@@ -413,42 +415,40 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
413
415
  recorder.stop("native-attempt-failed");
414
416
  // Every refusal says why, once per attempt: a capacity error with what the user can change, a cut Pi failed to commit with what that
415
417
  // means for the session, anything else as it is (an attempt that ended with no word looked like a hang).
416
- const notify = !failure;
417
- // A CommitFault stays whatever fails after it.
418
- if (!(failure instanceof CommitFault)) failure = error instanceof Error ? error : new Error(String(error));
419
- pendingBoundaryCommit = false;
420
- pending = undefined;
421
- promptCut = undefined;
422
- try {
423
- if (!refusalReceipted) { refusalReceipted = true; receipt("refusal", failure.message, ctx); }
418
+ const previous = failure(), notify = !previous;
419
+ const fault = previous instanceof CommitFault ? previous : error instanceof Error ? error : new Error(String(error));
420
+ const receipted = run?.refusalReceipted;
421
+ resetRun();
422
+ // Keep only a terminal attempt record until Pi settles: its fresh abort controller must still see the refusal.
423
+ run = createRun("failed"); run.failure = fault; run.refusalReceipted = true;
424
+ if (fault instanceof CommitFault) commitFault = fault;
425
+ try {
426
+ if (!receipted) receipt("refusal", fault.message, ctx);
424
427
  options.fail?.(error);
425
428
  } finally {
426
- try { if (notify) ctx.ui.notify(failure instanceof CapacityError ? refusalNotice(failure.message, factor) : failure instanceof CommitFault
427
- ? `pi-tuck: ${failure.message}; the session holds a cut its file may lack, so start a new session.`
428
- : `pi-tuck could not plan this request (${failure.message}); the next prompt is planned afresh.`, "error"); }
429
+ try { if (notify) ctx.ui.notify(fault instanceof CapacityError ? refusalNotice(fault.message, factor) : fault instanceof CommitFault
430
+ ? `pi-tuck: ${fault.message}; the session holds a cut its file may lack, so start a new session.`
431
+ : `pi-tuck could not plan this request (${fault.message}); the next prompt is planned afresh.`, "error"); }
429
432
  finally { ctx.abort(); }
430
433
  }
431
- };
432
- // The one place a failure is dropped: when its attempt has settled, when a prompt arrives, and when the operator asks for a cut. A run that no
433
- // input started (a custom message) has only the first of these. No failure but a CommitFault outlives its attempt.
434
- const forgetFailure = () => { refusalReceipted = false; if (!(failure instanceof CommitFault)) failure = undefined; };
434
+ };
435
+
435
436
  const access = () => {
436
- if (failure) throw failure;
437
+ if (failure()) throw failure();
437
438
  hostAccess();
438
439
  };
439
- // Abort alone is insufficient: Pi starts a fresh abort controller after
440
- // before_agent_start. Keep this attempt blocked, then recheck on new idle input.
441
- // "decided": `input` has decided for the run it starts, so that run's before_agent_start does not decide again. "starting": a run is
442
- // between before_agent_start and agent_start, where Pi 1.1 still reports idle but a message joins that run.
443
- let phase: "idle" | "decided" | "starting" = "idle";
444
440
  pi.on("input", async (event, ctx) => {
445
- if (!ctx.isIdle() || phase === "starting") return; // a steering or follow-up message joins the running request
446
- forgetFailure();
447
- phase = "decided";
441
+ if (!ctx.isIdle() || run?.phase === "starting") return; // steering joins the existing run
442
+ run = createRun("decided");
443
+ const decision = run;
448
444
  try { syncHistoryTools(ctx); await preflight(ctx, incomingTokens(event.text, event.images),
449
445
  [{ role: "user", content: [{ type: "text", text: event.text }, ...(event.images ?? [])], timestamp: 0 }], ctx.getSystemPrompt(), pi.getActiveTools()); syncHistoryTools(ctx); }
450
446
  catch (error) { fail(error, ctx); }
447
+ // Pi normally reaches its pre-run threshold check within microtasks. Auth I/O or a later extension's input I/O can make a
448
+ // real prompt expire too; safely fall back to carrying its cut in the request and saving it at the first boundary.
449
+ setImmediate(() => { if (run === decision && !decision.claimed) decision.expired = true; });
451
450
  });
451
+
452
452
  pi.registerTool({
453
453
  name: "tuck_recall", label: "pi-tuck history", parameters: RecallInput,
454
454
  description: "Keyword lookup locates particular text in your current branch. {query} returns ranked passages with read coordinates; punctuation separates words. Pass the returned next as {continuation} unchanged. " + (search ? "Use tuck_search for historical information relevant to a question, including different wording and qualifications. " : "") + "Knowing an identifier does not itself justify narrowing discovery. {entryId} or {index} (a summary sourceIndex or #N) reads one source, optionally with query, offset and limit in UTF-16 characters (limit defaults to 8000). {} browses history; {around} expands neighbors; {entryId,image} reads an image; outputPath writes an original to a file with the native write tool. Hidden reasoning and system records are excluded.",
@@ -502,26 +502,28 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
502
502
  if (event.signal.aborted || ctx.signal?.aborted) { recorder.stop("cut-cancelled"); return { cancel: true }; }
503
503
  // Pi checks its own threshold after a prompt's input and before the run starts: the prompt's cut, planned with the prompt counted,
504
504
  // is the answer, and it is saved before the first request.
505
- if (promptCut && !admittedRun && event.reason === "threshold") {
506
- access();
507
- const { type: _, ...compaction } = promptCut;
508
- promptCut = undefined;
509
- return { compaction };
505
+ if (run?.phase === "decided" && !run.expired && event.reason === "threshold") {
506
+ run.claimed = true;
507
+ if (view(ctx).pending) {
508
+ access();
509
+ const current = view(ctx), { type: _, ...compaction } = run.cut.take()!;
510
+ return { compaction: { ...compaction, firstKeptEntryId: current.keptFromId! } };
511
+ }
510
512
  }
511
513
  const operatorManual = event.reason === "manual";
512
- if (operatorManual && !admittedRun) forgetFailure();
514
+ if (operatorManual && run?.phase !== "admitted") resetRun();
513
515
  access();
514
516
  // pi-tuck's summary is built from the session itself, so text after /compact cannot steer it. The operator asked for a cut: make the one
515
517
  // plain /compact makes and say the text was not used, rather than cancel (which Pi shows as an error, with no cut).
516
518
  if (event.customInstructions?.trim()) ctx.ui.notify("pi-tuck: /compact ignores the text after it, since pi-tuck builds its summary from the session itself. Cutting as plain /compact does.", "info");
517
519
  const { response, room } = limits(ctx);
518
- const retain = operatorManual || admittedRun;
520
+ const retain = operatorManual || run?.phase === "admitted";
519
521
  // A cut Pi asks for (an operator's /compact, Pi's threshold or overflow recovery) charges what the request must carry whatever is
520
522
  // cut: the system part and the host input. A prompt's cut is pi-tuck's own and never comes here.
521
523
  const saved = ctx.sessionManager.buildSessionProjection().messages;
522
524
  const hostInput = hostMessages(ctx), host = conversationTokens(hostInput);
523
525
  const system = requestSystemPart(ctx.getSystemPrompt(), pi.getActiveTools(), saved, ctx.getSystemPrompt());
524
- const mandatory = system + host, band = decisionBand(ctx, room, system);
526
+ const mandatory = system + host, band = decisionBand(ctx, room, system, view(ctx, event.reason === "threshold"));
525
527
  const before = plannerMeasureFor(ctx)(saved);
526
528
  observeBudget(before + mandatory, ctx);
527
529
  // Pi's own usage threshold (reason "threshold") counts the last reply's output tokens, reasoning included, plus the messages after
@@ -543,9 +545,9 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
543
545
  const reserve = planReserve(target, targetRoom, targetBand, mandatory, ctx);
544
546
  // 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
545
547
  // from any run that ended. A retry is inside its run.
546
- const draft = tuckDraft(tuck, event, ctx, reserve, plannerMeasureFor(ctx), options.recallPolicy, retain, operatorManual, operatorManual || !admittedRun, admittedRun ? protectedPrompt(ctx) : undefined);
548
+ const draft = tuckDraft(tuck, event, ctx, reserve, plannerMeasureFor(ctx), options.recallPolicy, retain, operatorManual, operatorManual || run?.phase !== "admitted", run?.phase === "admitted" ? protectedPrompt(ctx) : undefined);
547
549
  return draft.compaction ? { draft: true, after: draft.after, mode: draft.mode, compaction: draft.compaction, reserve,
548
- messages: previewCut(ctx, { type: "compaction", ...draft.compaction, tokensBefore: before }).buildSessionProjection().messages }
550
+ messages: plannedView(ctx, { type: "compaction", ...draft.compaction, tokensBefore: before }).session.buildSessionProjection().messages }
549
551
  : { draft: false, capacityRejected: Boolean(draft.capacityRejected), reason: draft.unavailable };
550
552
  },
551
553
  });
@@ -558,7 +560,7 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
558
560
  candidate => (candidate.kind === "cut" ? candidate.plan.after : before) + mandatory,
559
561
  candidate => projectedRequest(candidate.kind === "cut" ? candidate.plan.messages : saved, ctx, hostInput, [], ctx.getSystemPrompt(), pi.getActiveTools(), candidate.kind === "cut"),
560
562
  target => target <= room ? decide() : decide(target, target),
561
- () => deepestDecision(ctx, operatorManual || !admittedRun, retain));
563
+ () => deepestDecision(ctx, operatorManual || run?.phase !== "admitted", retain));
562
564
  }
563
565
  if (decision.kind === "cut") { return { compaction: captureDraft(decision.plan.compaction, ctx, decision.plan.reserve, event.reason, event.preparation.tokensBefore,
564
566
  retain, operatorManual, event.preparation.previousSummary, { response, host, mandatory, target: decision.target, margin: room - band }) }; }
@@ -574,38 +576,21 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
574
576
  }
575
577
  });
576
578
  pi.on("session_before_tree", (event) => event.preparation.userWantsSummary ? { cancel: true } : undefined);
577
- pi.on("session_start", () => { clips.clear(); promptCut = undefined; recorder.stop("session-transition"); proposalId = undefined; failure = undefined; refusalReceipted = false; admittedRun = false; runPrompt = undefined; estimatedTokens = 0; pendingBoundaryCommit = false; identity = ""; factor = 1; pending = undefined; phase = "idle"; });
578
- const historyToolsEnabled = (ctx: ExtensionContext) => Boolean(promptCut) || hasTuckCut(ctx.sessionManager.getBranch());
579
+ pi.on("session_start", () => { resetRun(); recorder.stop("session-transition"); proposalId = undefined; commitFault = undefined; estimatedTokens = 0; identity = ""; factor = 1; });
580
+ const historyToolsEnabled = (ctx: ExtensionContext) => view(ctx).hasTuckCut;
579
581
  const syncHistoryTools = (ctx: ExtensionContext) => { if (active()) applyHistoryTools(pi, historyToolsEnabled(ctx)); };
580
582
  pi.on("session_start", (_event, ctx) => syncHistoryTools(ctx));
581
583
  // A plan decided on `input` is for the branch it was decided on: moving in the tree leaves it planned for another branch, whose kept entry
582
584
  // may not be on this one.
583
- pi.on("session_tree", (_event, ctx) => { promptCut = undefined; phase = "idle"; syncHistoryTools(ctx); });
584
- pi.on("session_compact", (_event, ctx) => { if (!admittedRun) syncHistoryTools(ctx); });
585
+ pi.on("session_tree", (_event, ctx) => { resetRun(); syncHistoryTools(ctx); });
586
+ pi.on("session_compact", (_event, ctx) => { if (run?.phase !== "admitted") syncHistoryTools(ctx); });
585
587
  pi.on("turn_start", (_event, ctx) => syncHistoryTools(ctx));
586
588
  pi.on("agent_settled", (_event, ctx) => syncHistoryTools(ctx));
587
- // 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:
588
- // 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)
589
- // that ends normally clears it.
590
- let lengthStop = false;
591
- pi.on("agent_start", () => { lengthStop = false; });
592
- pi.on("message_end", event => { if (event.message.role === "assistant") lengthStop = event.message.stopReason === "length"; });
593
- pi.on("agent_settled", (_event, ctx) => {
594
- if (!lengthStop || !active()) return;
595
- lengthStop = false;
596
- const message = "pi-tuck: the last reply stopped at the model's output limit and may be cut off. Ask the agent to continue.";
597
- try { if (ctx.hasUI === false) process.stderr.write(message + "\n"); else ctx.ui.notify(message, "warning"); } catch { /* ended */ } // with no UI (pi -p), stderr is the operator's
598
- });
599
- // 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.
600
- const previewCut = (ctx: ExtensionContext, cut: CutEntry) => {
601
- const header = ctx.sessionManager.getHeader();
602
- if (!header) throw new Error("Missing native session header for the prompt's cut");
603
- const preview = SessionManager.inMemory(ctx.cwd, undefined, [header, ...ctx.sessionManager.getBranch()]);
604
- preview.appendCompaction(cut.summary, cut.firstKeptEntryId, 0, cut.details, true);
605
- return preview;
589
+ const plannedView = (ctx: ExtensionContext, cut: CutEntry) => {
590
+ const pending = new PendingCut();
591
+ pending.plan(cut, ctx.sessionManager.getBranch().length);
592
+ return pending.view(ctx);
606
593
  };
607
- const cutSession = (ctx: ExtensionContext, cut: CutEntry) => previewCut(ctx, { ...cut, firstKeptEntryId: keptFrom(cut, ctx) });
608
- const withCut = (ctx: ExtensionContext, cut: CutEntry) => cutSession(ctx, cut).buildSessionProjection().messages;
609
594
  // The shared final planner: preserve the compiler's prompt/protocol floor and spend nothing on optional history. Fresh text results
610
595
  // get the smallest existing head/tail + recall-locator representation; their originals remain in the session, as for ordinary clips.
611
596
  const deepestDecision = (ctx: ExtensionContext, newPrompt: boolean, retain: boolean): Decision<CutPlan> => {
@@ -623,7 +608,7 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
623
608
  const middle = Math.floor((low + high) / 2);
624
609
  if (clipFor([result], entryIdOf, middle).length) high = middle; else low = middle + 1;
625
610
  }
626
- for (const clip of clipFor([result], entryIdOf, low)) clips.set(clip.toolCallId, clip);
611
+ for (const clip of clipFor([result], entryIdOf, low)) clips().set(clip.toolCallId, clip);
627
612
  }
628
613
  }
629
614
  const before = measureFor(ctx)(saved);
@@ -631,7 +616,7 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
631
616
  previousSummary: saved.find(message => message.role === "compactionSummary")?.summary, newPrompt, reRenderSummary: true,
632
617
  protectedPromptEntryId: newPrompt ? undefined : protectedPrompt(ctx), deepest: true }, ctx, 0, plannerMeasureFor(ctx), options.recallPolicy, retain);
633
618
  if (!draft.compaction) return { kind: "fault", capacity: Boolean(draft.capacityRejected), message: draft.unavailable };
634
- const messages = previewCut(ctx, { type: "compaction", ...draft.compaction }).buildSessionProjection().messages;
619
+ const messages = plannedView(ctx, { type: "compaction", ...draft.compaction }).session.buildSessionProjection().messages;
635
620
  assertNativeToolRounds(messages);
636
621
  return { kind: "cut", target: "room", plan: { draft: true, after: measureFor(ctx)(messages), mode: draft.mode,
637
622
  compaction: draft.compaction, reserve: 0, messages } };
@@ -641,8 +626,8 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
641
626
  // keepRecentTokens, and where those are the whole conversation it has nothing to cut, so every later prompt was refused for good (t06 at
642
627
  // Pi's defaults on a 32k window). Each request of the run carries the cut, and the run's first boundary saves it.
643
628
  const preflight = (ctx: ExtensionContext, incomingPart: number, incomingMessages: Messages, observedPrompt: string, tools: readonly string[], hostSnapshot?: Messages) => {
644
- promptCut = undefined;
645
- promptCutAt = ctx.sessionManager.getBranch().length;
629
+ run!.cut.clear();
630
+ const branchLength = ctx.sessionManager.getBranch().length;
646
631
  if (!active()) return;
647
632
  access();
648
633
  if (!settingsFor().getCompactionSettings(ctx.model).enabled) return;
@@ -672,7 +657,9 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
672
657
  // there buys nothing the request needed and costs history the next recall would have found.
673
658
  const draft = tuckObservedDraft(tuck, { branchEntries: ctx.sessionManager.getBranch(), tokensBefore: before, previousSummary, newPrompt: true, reRenderSummary: !opportunistic }, ctx, reserve, plannerMeasureFor(ctx), options.recallPolicy, false);
674
659
  if (!draft.compaction) return { draft: false, capacityRejected: Boolean(draft.capacityRejected), reason: draft.unavailable };
675
- const messages = withCut(ctx, { type: "compaction", ...draft.compaction, tokensBefore: before });
660
+ const candidate = new PendingCut();
661
+ candidate.plan({ type: "compaction", ...draft.compaction, tokensBefore: before }, branchLength);
662
+ const messages = candidate.view(ctx).session.buildSessionProjection().messages;
676
663
  assertNativeToolRounds(messages);
677
664
  return { draft: true, after: measure(messages), mode: draft.mode, compaction: draft.compaction, reserve, messages };
678
665
  },
@@ -694,16 +681,18 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
694
681
  if (decision.kind === "fault") throw new (decision.capacity ? CapacityError : Refusal)(decision.message);
695
682
  const compaction = captureDraft(decision.plan.compaction, ctx, decision.plan.reserve, "prompt", before, false, false, previousSummary,
696
683
  { response, host, mandatory, target: decision.target, margin: room - band });
697
- promptCut = { type: "compaction", summary: compaction.summary, firstKeptEntryId: compaction.firstKeptEntryId, tokensBefore: before, details: compaction.details };
684
+ run!.cut.plan({ type: "compaction", summary: compaction.summary, firstKeptEntryId: compaction.firstKeptEntryId, tokensBefore: before, details: compaction.details }, branchLength);
698
685
  };
699
686
  pi.on("before_agent_start", async (event, ctx) => {
700
687
  // A decided plan is for the prompt `input` saw. A run that a custom message starts is not that run (another extension handled the
701
688
  // input, so no run started for it): plan afresh.
702
- const prompted = phase === "decided" && event.messageType !== "custom";
703
- phase = "starting";
704
- runPromptMessageType = event.messageType;
705
- // Image-only and empty custom messages have no starting text to quote.
706
- runPrompt = event.messageType === "custom" ? textOf(event.message.content) || undefined : event.prompt;
689
+ const prompted = run?.phase === "decided" && event.messageType !== "custom";
690
+ if (!prompted && run?.phase !== "failed") run = createRun("starting");
691
+ run ??= createRun("starting");
692
+ run.claimed = true;
693
+ run.phase = "starting";
694
+ const prompt = event.messageType === "custom" ? textOf(event.message.content) : event.prompt;
695
+ run.prompt = prompt ? new StartingPrompt(prompt, ctx.sessionManager.getBranch().length, event.messageType) : undefined;
707
696
  try {
708
697
  syncHistoryTools(ctx);
709
698
  if (prompted) access(); // decided on input; a failure latched there still stops the run
@@ -723,7 +712,7 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
723
712
  // handlers have run. Pi's prepareRequest has not yet snapshotted executable tools. A first cut discovered in context_with_system
724
713
  // would be too late for that snapshot, even if its declarations were repaired there, so make the late prompt decision here.
725
714
  pi.on("message_end", (event, ctx) => {
726
- if (event.message.role !== "user" || !active() || !admittedRun || promptCut) return;
715
+ if (event.message.role !== "user" || !active() || run?.phase !== "admitted" || view(ctx).pending) return;
727
716
  try {
728
717
  access();
729
718
  if (!ctx.model) throw missingModel();
@@ -808,11 +797,7 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
808
797
  }
809
798
  return { draft: false, capacityRejected: Boolean(draft.capacityRejected), reason: draft.unavailable };
810
799
  }
811
- const header = ctx.sessionManager.getHeader();
812
- if (!header) throw new Error("Missing native session header at tool-loop boundary");
813
- const preview = SessionManager.inMemory(ctx.cwd, undefined, [header, ...ctx.sessionManager.getBranch()]);
814
- preview.appendCompaction(draft.compaction.summary, draft.compaction.firstKeptEntryId, before, draft.compaction.details, true);
815
- const messages = preview.buildSessionProjection().messages;
800
+ const messages = plannedView(ctx, { type: "compaction", ...draft.compaction, tokensBefore: before }).session.buildSessionProjection().messages;
816
801
  assertNativeToolRounds(messages);
817
802
  previews.push(messages);
818
803
  return { draft: true, after: measure(messages), mode: draft.mode, compaction: draft.compaction, reserve, messages };
@@ -835,9 +820,9 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
835
820
  for (const budget of clipBudgets(clipRoom, factor, mandatory, kept)) {
836
821
  const round = clipFor(event.toolResults, entryIdOf, budget);
837
822
  if (!round.length) continue;
838
- for (const clip of round) clips.set(clip.toolCallId, clip);
823
+ for (const clip of round) run!.clips.set(clip.toolCallId, clip);
839
824
  const clippedDecision = decide(clipRoom, targetBand, required);
840
- if (clippedDecision.kind === "fault") for (const clip of round) clips.delete(clip.toolCallId);
825
+ if (clippedDecision.kind === "fault") for (const clip of round) run!.clips.delete(clip.toolCallId);
841
826
  else { decision = clippedDecision; break; }
842
827
  }
843
828
  };
@@ -873,14 +858,14 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
873
858
  // the session as it is with that cut: a fresh result can still need a cut of its own there.
874
859
  type Boundary = Parameters<typeof boundaryCut>[0];
875
860
  const atBoundary = (event: Boundary, ctx: ExtensionContext, reason: "tool-loop" | "run-end", decide: boolean) => {
876
- if (!promptCut) return decide ? boundaryCut(event, ctx, reason) : undefined;
877
- const entry = promptCut;
878
- promptCut = undefined;
879
- const preview = cutSession(ctx, entry), view: ExtensionContext = Object.create(ctx, { sessionManager: { value: preview } });
861
+ const current = view(ctx);
862
+ if (!current.pending) return decide ? boundaryCut(event, ctx, reason) : undefined;
863
+ const entry = run!.cut.take()!;
864
+ const preview = current.session, context: ExtensionContext = Object.create(ctx, { sessionManager: { value: preview } });
880
865
  const more = decide && !event.entries.length
881
- ? boundaryCut({ ...event, context: { ...event.context, contextMessages: preview.buildSessionProjection().messages } }, view, reason) : undefined;
866
+ ? boundaryCut({ ...event, context: { ...event.context, contextMessages: preview.buildSessionProjection().messages } }, context, reason) : undefined;
882
867
  const { tokensBefore: _, ...cut } = entry;
883
- const saved = { ...cut, firstKeptEntryId: keptFrom(entry, ctx) };
868
+ const saved = { ...cut, firstKeptEntryId: current.keptFromId! };
884
869
  // As for a cut Pi makes (session_compact): the history the cut leaves behind is indexed, once Pi has committed the entries.
885
870
  setImmediate(() => { semantic.indexAtCut(semanticProvider(ctx)); });
886
871
  return { entries: [...event.entries as CutEntry[], saved, ...(more?.entries ?? [])] };
@@ -890,44 +875,36 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
890
875
  // Pi's later native length recovery from retrying it independently. The stored message and its output usage stay on the branch.
891
876
  const entries: SessionBoundaryDraft[] = [...(atBoundary(event, ctx, "run-end", false)?.entries ?? event.entries),
892
877
  { type: "context_edit", targetId: event.messageEntryId, replacement: null }];
893
- if (thinkingRetryPending) return { entries, continue: false };
894
- const header = ctx.sessionManager.getHeader();
895
- if (!header) throw new Error("Missing native session header for the thinking-only retry");
896
- const preview = SessionManager.inMemory(ctx.cwd, undefined, [header, ...ctx.sessionManager.getBranch()]);
897
- for (const entry of entries) {
898
- if (entry.type === "context_edit") preview.appendContextEdit(entry.targetId, entry.replacement);
899
- else if (entry.type === "compaction") preview.appendCompaction(entry.summary, entry.firstKeptEntryId, 0, entry.details, true, entry.usage);
900
- else if (entry.type === "custom") preview.appendCustomEntry(entry.customType, entry.data);
901
- else preview.appendCustomMessageEntry(entry.customType, entry.content, entry.display, entry.details);
902
- }
878
+ if (run!.thinkingRetryPending) return { entries, continue: false };
879
+ const preview = liveCut.view(ctx, entries).session;
903
880
  const view: ExtensionContext = Object.create(ctx, { sessionManager: { value: preview } });
904
881
  const hostInput = hostMessages(ctx), host = conversationTokens(hostInput);
905
882
  // 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
906
883
  // as large as the deepest cut's, the turn is retried uncut, and no history is given up for nothing.
907
884
  const asIs = projectedRequest(preview.buildSessionProjection().messages, view, hostInput, event.context.pendingMessages, ctx.getSystemPrompt(), pi.getActiveTools(), false);
908
885
  const asIsCheck = requestClamp(asIs, view);
909
- const previousClips = new Map(clips);
886
+ const previousClips = new Map(clips());
910
887
  const decision = deepestDecision(view, false, true);
911
888
  if (decision.kind === "cut" && asIsCheck.cap !== undefined && asIsCheck.leavesFloor &&
912
889
  requestClamp(projectedRequest(decision.plan.messages, view, hostInput, event.context.pendingMessages, ctx.getSystemPrompt(), pi.getActiveTools(), true), view).cap! <= asIsCheck.cap) {
913
- clips.clear(); for (const [id, clip] of previousClips) clips.set(id, clip);
890
+ clips().clear(); for (const [id, clip] of previousClips) clips().set(id, clip);
914
891
  const { room, hardLine } = limits(view);
915
- thinkingRetryPending = true;
892
+ run!.thinkingRetryPending = true;
916
893
  return { entries: [...entries, { type: "custom" as const, customType: "pi-tuck-capacity", data: { kind: "retry", stoppedReplyEntryId: event.messageEntryId,
917
894
  stoppedOutputTokens: event.message.role === "assistant" ? event.message.usage?.output ?? 0 : 0, cut: false,
918
895
  piEstimateTokens: asIsCheck.estimate, piCap: asIsCheck.cap, room, hardLine, buildId: BUILD_ID } }], continue: true };
919
896
  }
920
897
  if (decision.kind !== "cut") throw new CapacityError(clampMessage);
921
898
  const messages = projectedRequest(decision.plan.messages, view, hostInput, event.context.pendingMessages, ctx.getSystemPrompt(), pi.getActiveTools(), true);
922
- if (runPrompt !== undefined && !carriesPrompt(messages, runPrompt, runPromptMessageType)) throw promptRefusal();
899
+ if (run?.prompt && !carriesPrompt(messages, run.prompt.text(ctx.sessionManager.getBranch()), run.prompt.role)) throw promptRefusal();
923
900
  const system = requestSystemPart(ctx.getSystemPrompt(), pi.getActiveTools(), messages, ctx.getSystemPrompt());
924
901
  const tokens = system + conversationTokens(messages), check = requestClamp(messages, view);
925
902
  const { response, room, hardLine } = limits(view);
926
903
  if (!fits(tokens, hardLine, view) || check.cap === undefined || !check.leavesFloor) throw new CapacityError(clampMessage);
927
904
  const compaction = captureDraft(decision.plan.compaction, view, decision.plan.reserve, "length-retry", tokens, true, false, undefined,
928
905
  { response, host, mandatory: system + host + conversationTokens(event.context.pendingMessages), target: decision.target, margin: room - decisionBand(view, room, system) });
929
- thinkingRetryPending = true;
930
- pendingBoundaryCommit = true;
906
+ run!.thinkingRetryPending = true;
907
+ run!.pendingBoundaryCommit = true;
931
908
  return { entries: [...entries, { type: "compaction" as const, summary: compaction.summary, firstKeptEntryId: compaction.firstKeptEntryId || null, details: compaction.details },
932
909
  { type: "custom" as const, customType: "pi-tuck-capacity", data: { kind: "retry", stoppedReplyEntryId: event.messageEntryId,
933
910
  stoppedOutputTokens: event.message.role === "assistant" ? event.message.usage?.output ?? 0 : 0, cut: true,
@@ -935,16 +912,16 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
935
912
  };
936
913
  pi.on("turn_end", (event, ctx) => {
937
914
  try {
938
- if (!active() || !admittedRun) return;
939
- if (thinkingOnlyLength(event.message) && !failure && !ctx.signal?.aborted && settingsFor().getCompactionSettings(ctx.model).enabled)
915
+ if (!active() || run?.phase !== "admitted") return;
916
+ if (thinkingOnlyLength(event.message) && !failure() && !ctx.signal?.aborted && settingsFor().getCompactionSettings(ctx.model).enabled)
940
917
  return retryThinking(event, ctx);
941
- thinkingRetryPending = false;
918
+ run.thinkingRetryPending = false;
942
919
  const decide = event.outcome === "completed" && !ctx.signal?.aborted && event.context.canContinue && event.toolResults.length > 0 &&
943
920
  settingsFor().getCompactionSettings(ctx.model).enabled;
944
- if (!promptCut && !decide) return;
921
+ if (!view(ctx).pending && !decide) return;
945
922
  const result = atBoundary(event, ctx, "tool-loop", decide);
946
923
  if (result) {
947
- pendingBoundaryCommit = true;
924
+ run!.pendingBoundaryCommit = true;
948
925
  }
949
926
  return result;
950
927
  } catch (error) { fail(error, ctx); }
@@ -960,21 +937,22 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
960
937
  // A request of a run whose prompt's cut is not saved yet: the saved conversation in it, which other handlers may surround, is replaced by
961
938
  // the conversation with the cut applied. Pi sends the saved system message apart from the request's messages, so neither side carries it
962
939
  // here. A request that does not carry the saved conversation as it stands cannot take the cut.
963
- const cutRequest = <M,>(messages: M[], ctx: ExtensionContext, cut: CutEntry): M[] => {
940
+ const cutRequest = <M,>(messages: M[], ctx: ExtensionContext): M[] => {
964
941
  const conversation = (all: Messages) => all.filter(message => message.role !== "system") as unknown[];
965
942
  const saved = conversation(ctx.sessionManager.buildSessionProjection().messages);
966
943
  for (let at = 0; at + saved.length <= messages.length; at++)
967
944
  if (saved.every((message, i) => isDeepStrictEqual(messages[at + i], message)))
968
- return [...messages.slice(0, at), ...conversation(withCut(ctx, cut)) as M[], ...messages.slice(at + saved.length)];
945
+ return [...messages.slice(0, at), ...conversation(view(ctx).session.buildSessionProjection().messages) as M[], ...messages.slice(at + saved.length)];
969
946
  throw new Error("The prompt's cut cannot be applied: the request does not carry the saved conversation");
970
947
  };
971
948
  pi.on("context", (event, ctx) => {
972
949
  try {
973
950
  if (!active()) return;
951
+ run?.prompt?.id(ctx.sessionManager.getBranch());
974
952
  // Before a cut, Pi owns the whole request, including its object identities and system-message serialization.
975
- if (!historyToolsEnabled(ctx)) return clips.size ? { messages: applyClips(event.messages, clips) } : undefined;
953
+ if (!historyToolsEnabled(ctx)) return clips().size ? { messages: applyClips(event.messages, clips()) } : undefined;
976
954
  access();
977
- return { messages: applyClips(projectedMessages(promptCut ? cutRequest(event.messages, ctx, promptCut) : event.messages, ctx, options.recallPolicy), clips) };
955
+ return { messages: applyClips(projectedMessages(view(ctx).pending ? cutRequest(event.messages, ctx) : event.messages, ctx, options.recallPolicy), clips()) };
978
956
  } catch (error) { try { fail(error, ctx); } finally { return historyToolsEnabled(ctx) ? { messages: [] } : undefined; } }
979
957
  });
980
958
  pi.on("session_compact_failed", (event, ctx) => {
@@ -992,21 +970,21 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
992
970
  if (active() && !event.aborted) fail(new (event.fromExtension ? CommitFault : Error)(event.errorMessage ?? "Compaction failed"), ctx);
993
971
  });
994
972
  pi.on("agent_before_settle", (event, ctx) => {
995
- if (pendingBoundaryCommit) {
973
+ if (run?.pendingBoundaryCommit) {
996
974
  if (event.outcome === "error") return void fail(new CommitFault("Native tool-loop boundary draft remained unconfirmed at error settlement"), ctx);
997
- pendingBoundaryCommit = false;
975
+ if (run) run.pendingBoundaryCommit = false;
998
976
  }
999
977
  // Pi commits these entries before the run settles.
1000
978
  try {
1001
- if (!active() || !admittedRun || failure || !settingsFor().getCompactionSettings(ctx.model).enabled) return;
979
+ if (!active() || run?.phase !== "admitted" || failure() || !settingsFor().getCompactionSettings(ctx.model).enabled) return;
1002
980
  const decide = event.outcome === "completed" && !ctx.signal?.aborted;
1003
- if (promptCut ? event.outcome === "error" : !decide) return;
981
+ if (view(ctx).pending ? event.outcome === "error" : !decide) return;
1004
982
  return atBoundary(event, ctx, "run-end", decide);
1005
983
  } catch (error) { fail(error, ctx); }
1006
984
  });
1007
985
  pi.on("context_with_system", (event, ctx) => {
1008
986
  // Reaching the next request boundary proves native commit returned, not durable storage.
1009
- pendingBoundaryCommit = false;
987
+ if (run) run.pendingBoundaryCommit = false;
1010
988
  pending = undefined;
1011
989
  try {
1012
990
  if (!active()) return;
@@ -1023,7 +1001,7 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
1023
1001
  observeBudget(tokens, ctx);
1024
1002
  estimatedTokens = corrected(tokens, ctx);
1025
1003
  const { room, hardLine } = limits(ctx);
1026
- if (runPrompt !== undefined && !carriesPrompt(messages, runPrompt, runPromptMessageType))
1004
+ if (run?.prompt && !carriesPrompt(messages, run.prompt.text(ctx.sessionManager.getBranch()), run.prompt.role))
1027
1005
  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");
1028
1006
  if (!fits(tokens, hardLine, ctx))
1029
1007
  throw new CapacityError("Complete Pi context and response reserve exceed the estimated request budget");