pi-tuck 1.0.29 → 1.0.31

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,24 @@
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.31] - 2026-11-12
7
+
8
+ ### Fixed
9
+
10
+ - **Input that arrived after a tool round had been checked could make pi-tuck refuse a request it could still cut.** pi-tuck decides
11
+ whether to cut when a tool round ends. Input added after that point, such as another extension's context or a newly enabled tool, was
12
+ not seen there. When it pushed the next request past the hard line, the final check refused the request and the run stopped. It now cuts
13
+ at that check instead, carries the cut in that request, and saves it at the next boundary. It refuses only when no cut can fit.
14
+
15
+ ### Release gates
16
+
17
+ - **Shipped without a live gate under the defect rule, because its replay matches 1.0.29:** 13,946 requests, 0 different, 0 lost or
18
+ dead tasks, clips 17 → 17.
19
+ - **A new test reproduces the refusal on 1.0.29** and passes with the fix. On 1.0.29 it needed late input larger than the gap between the
20
+ early cut and the room.
21
+ - **Strict stress, 313 cells:** 0 findings, on Pi 1.2.0 and the newest Pi. Tests and CI. The packed tarball loaded in the real
22
+ installed Pi 1.5.2.
23
+
6
24
  ## [1.0.29] - 2026-11-11
7
25
 
8
26
  ### Fixed
@@ -1 +1 @@
1
- export declare const BUILD_ID = "7ff651306c4a9f937b9d1abdc44123ce28cc9a6892f0730b3bef7d8174574a94";
1
+ export declare const BUILD_ID = "5bf3e8c3b38641652836a07684de20f6e5b17aec8e3ad4051e4fd85a250b9a2d";
@@ -1 +1 @@
1
- export const BUILD_ID = "7ff651306c4a9f937b9d1abdc44123ce28cc9a6892f0730b3bef7d8174574a94";
1
+ export const BUILD_ID = "5bf3e8c3b38641652836a07684de20f6e5b17aec8e3ad4051e4fd85a250b9a2d";
@@ -1121,17 +1121,71 @@ export async function installCompaction(pi, options = {}) {
1121
1121
  }
1122
1122
  }
1123
1123
  });
1124
- // 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
1125
- // the conversation with the cut applied. Pi sends the saved system message apart from the request's messages, so neither side carries it
1126
- // here. A request that does not carry the saved conversation as it stands cannot take the cut.
1127
- const cutRequest = (messages, ctx) => {
1124
+ const projectContext = (messages, ctx, enabled = historyToolsEnabled(ctx)) => applyClips(enabled ? projectedMessages(messages, ctx, options.recallPolicy) : messages, clips());
1125
+ // Replace only the saved conversation, preserving other handlers' surrounding messages. At the final guard the saved side is already
1126
+ // projected. Replay system state as Pi does after a changed `context` result, so interleaved prompt/tool updates survive the cut too.
1127
+ const cutRequest = (messages, ctx, saved = ctx.sessionManager.buildSessionProjection().messages, kept = view(ctx).session.buildSessionProjection().messages) => {
1128
1128
  const conversation = (all) => all.filter(message => message.role !== "system");
1129
- const saved = conversation(ctx.sessionManager.buildSessionProjection().messages);
1130
- for (let at = 0; at + saved.length <= messages.length; at++)
1131
- if (saved.every((message, i) => isDeepStrictEqual(messages[at + i], message)))
1132
- return [...messages.slice(0, at), ...conversation(view(ctx).session.buildSessionProjection().messages), ...messages.slice(at + saved.length)];
1129
+ const source = conversation(saved), request = conversation(messages);
1130
+ for (let at = 0; at + source.length <= request.length; at++)
1131
+ if (source.every((message, i) => isDeepStrictEqual(request[at + i], message))) {
1132
+ const reduced = [...request.slice(0, at), ...conversation(kept), ...request.slice(at + source.length)];
1133
+ const head = getCurrentSystemMessage(messages.filter(message => message.role === "system"));
1134
+ return head ? [head, ...reduced] : reduced;
1135
+ }
1133
1136
  throw new Error("The prompt's cut cannot be applied: the request does not carry the saved conversation");
1134
1137
  };
1138
+ // A late cut prices everything outside the saved conversation from this request itself. Its pending view is carried now and saved by
1139
+ // atBoundary, just like a prompt cut; the existing final checks still decide admission on the rebuilt request.
1140
+ const guardCut = (messages, tokens, system, ctx) => {
1141
+ const saved = ctx.sessionManager.buildSessionProjection().messages, source = projectContext(saved, ctx);
1142
+ const before = conversationTokens(source), mandatory = tokens - before;
1143
+ const { response, room, hardLine } = limits(ctx), band = decisionBand(ctx, room, system);
1144
+ const previousSummary = saved.find(message => message.role === "compactionSummary")?.summary;
1145
+ const previousClips = new Map(clips());
1146
+ const measure = measureFor(ctx);
1147
+ const decide = (targetRoom, targetBand) => !fits(mandatory, targetRoom, ctx) ? { kind: "defer" } : chooseCut({
1148
+ before, mandatory, room: targetRoom, band: targetBand, fits: (tokens, limit) => fits(tokens, limit, ctx), opportunistic: false,
1149
+ unreduced: "Tool-loop preflight compaction did not reduce context within the estimated request budget",
1150
+ plan: (target) => {
1151
+ const reserve = planReserve(target, targetRoom, targetBand, mandatory, ctx);
1152
+ const draft = tuckObservedDraft(tuck, { branchEntries: ctx.sessionManager.getBranch(), tokensBefore: before, previousSummary,
1153
+ protectedPromptEntryId: protectedPrompt(ctx) }, ctx, reserve, plannerMeasureFor(ctx), options.recallPolicy, true);
1154
+ if (!draft.compaction)
1155
+ return { draft: false, capacityRejected: Boolean(draft.capacityRejected), reason: draft.unavailable };
1156
+ const kept = plannedView(ctx, { type: "compaction", ...draft.compaction, tokensBefore: before }).session.buildSessionProjection().messages;
1157
+ assertNativeToolRounds(kept);
1158
+ return { draft: true, after: measure(kept), mode: draft.mode, compaction: draft.compaction, reserve, messages: kept };
1159
+ },
1160
+ });
1161
+ const project = (candidate) => candidate.kind === "cut"
1162
+ ? historyToolDeclarations(cutRequest(messages, ctx, source, projectContext(candidate.plan.messages, ctx, true)), toolShapes(historyToolNames(pi, pi.getActiveTools(), true)), true) : messages;
1163
+ const total = (candidate) => (candidate.kind === "cut" ? candidate.plan.after : before) + mandatory;
1164
+ let decision = decide(room, band);
1165
+ if (hardLine > room && (decision.kind === "defer" || decision.kind === "fault")) {
1166
+ decision = decide(hardLine, hardLine);
1167
+ decision = clampAboveRoom(decision, ctx, room, hardLine, total, project, target => decide(target, target <= room ? band : target), () => deepestDecision(ctx, false, true));
1168
+ }
1169
+ const accepts = (candidate) => {
1170
+ if (candidate.kind !== "cut" || candidate.plan.after >= before)
1171
+ return false;
1172
+ const request = project(candidate);
1173
+ const reduced = requestSystemPart(ctx.getSystemPrompt(), pi.getActiveTools(), request, ctx.getSystemPrompt()) + conversationTokens(request);
1174
+ return fits(reduced, hardLine, ctx) && (Boolean(options.requestReserve) || corrected(reduced, ctx) <= room || requestClamp(request, ctx).leavesFloor);
1175
+ };
1176
+ if (!accepts(decision))
1177
+ decision = deepestDecision(ctx, false, true);
1178
+ if (decision.kind !== "cut" || !accepts(decision)) {
1179
+ clips().clear();
1180
+ for (const [id, clip] of previousClips)
1181
+ clips().set(id, clip);
1182
+ return messages;
1183
+ }
1184
+ const compaction = captureDraft({ ...decision.plan.compaction, tokensBefore: before }, ctx, decision.plan.reserve, "tool-loop", before, true, false, previousSummary, { response, host: conversationTokens(hostMessages(ctx)), mandatory, target: decision.target, margin: room - band });
1185
+ run.cut.plan({ type: "compaction", ...compaction, tokensBefore: before }, ctx.sessionManager.getBranch().length);
1186
+ syncHistoryTools(ctx);
1187
+ return historyToolDeclarations(cutRequest(messages, ctx, source, projectContext(view(ctx).session.buildSessionProjection().messages, ctx)), toolShapes(pi.getActiveTools()), true);
1188
+ };
1135
1189
  pi.on("context", (event, ctx) => {
1136
1190
  try {
1137
1191
  if (!active())
@@ -1141,7 +1195,7 @@ export async function installCompaction(pi, options = {}) {
1141
1195
  if (!historyToolsEnabled(ctx))
1142
1196
  return clips().size ? { messages: applyClips(event.messages, clips()) } : undefined;
1143
1197
  access();
1144
- return { messages: applyClips(projectedMessages(view(ctx).pending ? cutRequest(event.messages, ctx) : event.messages, ctx, options.recallPolicy), clips()) };
1198
+ return { messages: projectContext(view(ctx).pending ? cutRequest(event.messages, ctx) : event.messages, ctx) };
1145
1199
  }
1146
1200
  catch (error) {
1147
1201
  try {
@@ -1218,12 +1272,18 @@ export async function installCompaction(pi, options = {}) {
1218
1272
  // The complete request, counted after Pi's conversion (summary/custom/bash envelopes), with the effective system once.
1219
1273
  // Pi applies a forced prompt after this hook; the public prompt already observes it.
1220
1274
  syncHistoryTools(ctx);
1221
- const messages = historyToolsEnabled(ctx) ? historyToolDeclarations(event.messages, toolShapes(pi.getActiveTools()), true) : event.messages;
1222
- const system = requestSystemPart(ctx.getSystemPrompt(), pi.getActiveTools(), messages, ctx.getSystemPrompt());
1223
- const tokens = system + conversationTokens(messages);
1275
+ let messages = historyToolsEnabled(ctx) ? historyToolDeclarations(event.messages, toolShapes(pi.getActiveTools()), true) : event.messages;
1276
+ let system = requestSystemPart(ctx.getSystemPrompt(), pi.getActiveTools(), messages, ctx.getSystemPrompt());
1277
+ let tokens = system + conversationTokens(messages);
1278
+ const { room, hardLine } = limits(ctx);
1279
+ if (!fits(tokens, hardLine, ctx) && run?.phase === "admitted" && !view(ctx).pending && settingsFor().getCompactionSettings(ctx.model).enabled) {
1280
+ messages = guardCut(messages, tokens, system, ctx);
1281
+ system = requestSystemPart(ctx.getSystemPrompt(), pi.getActiveTools(), messages, ctx.getSystemPrompt());
1282
+ tokens = system + conversationTokens(messages);
1283
+ assertNativeToolRounds(messages);
1284
+ }
1224
1285
  observeBudget(tokens, ctx);
1225
1286
  estimatedTokens = corrected(tokens, ctx);
1226
- const { room, hardLine } = limits(ctx);
1227
1287
  if (run?.prompt && !carriesPrompt(messages, run.prompt.text(ctx.sessionManager.getBranch()), run.prompt.role))
1228
1288
  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");
1229
1289
  if (!fits(tokens, hardLine, ctx))
@@ -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 = "7ff651306c4a9f937b9d1abdc44123ce28cc9a6892f0730b3bef7d8174574a94";
3
+ export declare const DIAGNOSTIC_BUILD = "5bf3e8c3b38641652836a07684de20f6e5b17aec8e3ad4051e4fd85a250b9a2d";
4
4
  export interface Receipt {
5
5
  id: string;
6
6
  build: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-tuck",
3
- "version": "1.0.29",
3
+ "version": "1.0.31",
4
4
  "description": "Deterministic compaction for Pi with no model call, plus exact recall of earlier history. Search by meaning on Apple silicon.",
5
5
  "keywords": [
6
6
  "pi-package",
package/src/build-id.ts CHANGED
@@ -1 +1 @@
1
- export const BUILD_ID = "7ff651306c4a9f937b9d1abdc44123ce28cc9a6892f0730b3bef7d8174574a94";
1
+ export const BUILD_ID = "5bf3e8c3b38641652836a07684de20f6e5b17aec8e3ad4051e4fd85a250b9a2d";
package/src/compaction.ts CHANGED
@@ -986,17 +986,72 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
986
986
  }
987
987
  }
988
988
  });
989
- // 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
990
- // the conversation with the cut applied. Pi sends the saved system message apart from the request's messages, so neither side carries it
991
- // here. A request that does not carry the saved conversation as it stands cannot take the cut.
992
- const cutRequest = <M,>(messages: M[], ctx: ExtensionContext): M[] => {
993
- const conversation = (all: Messages) => all.filter(message => message.role !== "system") as unknown[];
994
- const saved = conversation(ctx.sessionManager.buildSessionProjection().messages);
995
- for (let at = 0; at + saved.length <= messages.length; at++)
996
- if (saved.every((message, i) => isDeepStrictEqual(messages[at + i], message)))
997
- return [...messages.slice(0, at), ...conversation(view(ctx).session.buildSessionProjection().messages) as M[], ...messages.slice(at + saved.length)];
989
+ const projectContext = (messages: Messages, ctx: ExtensionContext, enabled = historyToolsEnabled(ctx)) =>
990
+ applyClips(enabled ? projectedMessages(messages, ctx, options.recallPolicy) : messages, clips());
991
+ // Replace only the saved conversation, preserving other handlers' surrounding messages. At the final guard the saved side is already
992
+ // projected. Replay system state as Pi does after a changed `context` result, so interleaved prompt/tool updates survive the cut too.
993
+ const cutRequest = (messages: Messages, ctx: ExtensionContext, saved = ctx.sessionManager.buildSessionProjection().messages,
994
+ kept = view(ctx).session.buildSessionProjection().messages): Messages => {
995
+ const conversation = (all: Messages) => all.filter(message => message.role !== "system");
996
+ const source = conversation(saved), request = conversation(messages);
997
+ for (let at = 0; at + source.length <= request.length; at++)
998
+ if (source.every((message, i) => isDeepStrictEqual(request[at + i], message))) {
999
+ const reduced = [...request.slice(0, at), ...conversation(kept), ...request.slice(at + source.length)];
1000
+ const head = getCurrentSystemMessage(messages.filter(message => message.role === "system"));
1001
+ return head ? [head, ...reduced] : reduced;
1002
+ }
998
1003
  throw new Error("The prompt's cut cannot be applied: the request does not carry the saved conversation");
999
1004
  };
1005
+ // A late cut prices everything outside the saved conversation from this request itself. Its pending view is carried now and saved by
1006
+ // atBoundary, just like a prompt cut; the existing final checks still decide admission on the rebuilt request.
1007
+ const guardCut = (messages: Messages, tokens: number, system: number, ctx: ExtensionContext): Messages => {
1008
+ const saved = ctx.sessionManager.buildSessionProjection().messages, source = projectContext(saved, ctx);
1009
+ const before = conversationTokens(source), mandatory = tokens - before;
1010
+ const { response, room, hardLine } = limits(ctx), band = decisionBand(ctx, room, system);
1011
+ const previousSummary = saved.find(message => message.role === "compactionSummary")?.summary;
1012
+ const previousClips = new Map(clips());
1013
+ const measure = measureFor(ctx);
1014
+ const decide = (targetRoom: number, targetBand: number): Decision<CutPlan> => !fits(mandatory, targetRoom, ctx) ? { kind: "defer" } : chooseCut({
1015
+ before, mandatory, room: targetRoom, band: targetBand, fits: (tokens, limit) => fits(tokens, limit, ctx), opportunistic: false,
1016
+ unreduced: "Tool-loop preflight compaction did not reduce context within the estimated request budget",
1017
+ plan: (target): Plan<CutPlan> => {
1018
+ const reserve = planReserve(target, targetRoom, targetBand, mandatory, ctx);
1019
+ const draft = tuckObservedDraft(tuck, { branchEntries: ctx.sessionManager.getBranch(), tokensBefore: before, previousSummary,
1020
+ protectedPromptEntryId: protectedPrompt(ctx) }, ctx, reserve, plannerMeasureFor(ctx), options.recallPolicy, true);
1021
+ if (!draft.compaction) return { draft: false, capacityRejected: Boolean(draft.capacityRejected), reason: draft.unavailable };
1022
+ const kept = plannedView(ctx, { type: "compaction", ...draft.compaction, tokensBefore: before }).session.buildSessionProjection().messages;
1023
+ assertNativeToolRounds(kept);
1024
+ return { draft: true, after: measure(kept), mode: draft.mode, compaction: draft.compaction, reserve, messages: kept };
1025
+ },
1026
+ });
1027
+ const project = (candidate: Decision<CutPlan>) => candidate.kind === "cut"
1028
+ ? historyToolDeclarations(cutRequest(messages, ctx, source, projectContext(candidate.plan.messages, ctx, true)),
1029
+ toolShapes(historyToolNames(pi, pi.getActiveTools(), true)), true) : messages;
1030
+ const total = (candidate: Decision<CutPlan>) => (candidate.kind === "cut" ? candidate.plan.after : before) + mandatory;
1031
+ let decision = decide(room, band);
1032
+ if (hardLine > room && (decision.kind === "defer" || decision.kind === "fault")) {
1033
+ decision = decide(hardLine, hardLine);
1034
+ decision = clampAboveRoom(decision, ctx, room, hardLine, total, project,
1035
+ target => decide(target, target <= room ? band : target), () => deepestDecision(ctx, false, true));
1036
+ }
1037
+ const accepts = (candidate: Decision<CutPlan>) => {
1038
+ if (candidate.kind !== "cut" || candidate.plan.after >= before) return false;
1039
+ const request = project(candidate);
1040
+ const reduced = requestSystemPart(ctx.getSystemPrompt(), pi.getActiveTools(), request, ctx.getSystemPrompt()) + conversationTokens(request);
1041
+ return fits(reduced, hardLine, ctx) && (Boolean(options.requestReserve) || corrected(reduced, ctx) <= room || requestClamp(request, ctx).leavesFloor);
1042
+ };
1043
+ if (!accepts(decision)) decision = deepestDecision(ctx, false, true);
1044
+ if (decision.kind !== "cut" || !accepts(decision)) {
1045
+ clips().clear(); for (const [id, clip] of previousClips) clips().set(id, clip);
1046
+ return messages;
1047
+ }
1048
+ const compaction = captureDraft({ ...decision.plan.compaction, tokensBefore: before }, ctx, decision.plan.reserve, "tool-loop", before, true, false, previousSummary,
1049
+ { response, host: conversationTokens(hostMessages(ctx)), mandatory, target: decision.target, margin: room - band });
1050
+ run!.cut.plan({ type: "compaction", ...compaction, tokensBefore: before }, ctx.sessionManager.getBranch().length);
1051
+ syncHistoryTools(ctx);
1052
+ return historyToolDeclarations(cutRequest(messages, ctx, source, projectContext(view(ctx).session.buildSessionProjection().messages, ctx)),
1053
+ toolShapes(pi.getActiveTools()), true);
1054
+ };
1000
1055
  pi.on("context", (event, ctx) => {
1001
1056
  try {
1002
1057
  if (!active()) return;
@@ -1004,7 +1059,7 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
1004
1059
  // Before a cut, Pi owns the whole request, including its object identities and system-message serialization.
1005
1060
  if (!historyToolsEnabled(ctx)) return clips().size ? { messages: applyClips(event.messages, clips()) } : undefined;
1006
1061
  access();
1007
- return { messages: applyClips(projectedMessages(view(ctx).pending ? cutRequest(event.messages, ctx) : event.messages, ctx, options.recallPolicy), clips()) };
1062
+ return { messages: projectContext(view(ctx).pending ? cutRequest(event.messages, ctx) : event.messages, ctx) };
1008
1063
  } catch (error) { try { fail(error, ctx); } finally { return historyToolsEnabled(ctx) ? { messages: [] } : undefined; } }
1009
1064
  });
1010
1065
  pi.on("session_compact_failed", (event, ctx) => {
@@ -1050,12 +1105,18 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
1050
1105
  // The complete request, counted after Pi's conversion (summary/custom/bash envelopes), with the effective system once.
1051
1106
  // Pi applies a forced prompt after this hook; the public prompt already observes it.
1052
1107
  syncHistoryTools(ctx);
1053
- const messages = historyToolsEnabled(ctx) ? historyToolDeclarations(event.messages, toolShapes(pi.getActiveTools()), true) : event.messages;
1054
- const system = requestSystemPart(ctx.getSystemPrompt(), pi.getActiveTools(), messages, ctx.getSystemPrompt());
1055
- const tokens = system + conversationTokens(messages);
1108
+ let messages = historyToolsEnabled(ctx) ? historyToolDeclarations(event.messages, toolShapes(pi.getActiveTools()), true) : event.messages;
1109
+ let system = requestSystemPart(ctx.getSystemPrompt(), pi.getActiveTools(), messages, ctx.getSystemPrompt());
1110
+ let tokens = system + conversationTokens(messages);
1111
+ const { room, hardLine } = limits(ctx);
1112
+ if (!fits(tokens, hardLine, ctx) && run?.phase === "admitted" && !view(ctx).pending && settingsFor().getCompactionSettings(ctx.model).enabled) {
1113
+ messages = guardCut(messages, tokens, system, ctx);
1114
+ system = requestSystemPart(ctx.getSystemPrompt(), pi.getActiveTools(), messages, ctx.getSystemPrompt());
1115
+ tokens = system + conversationTokens(messages);
1116
+ assertNativeToolRounds(messages);
1117
+ }
1056
1118
  observeBudget(tokens, ctx);
1057
1119
  estimatedTokens = corrected(tokens, ctx);
1058
- const { room, hardLine } = limits(ctx);
1059
1120
  if (run?.prompt && !carriesPrompt(messages, run.prompt.text(ctx.sessionManager.getBranch()), run.prompt.role))
1060
1121
  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");
1061
1122
  if (!fits(tokens, hardLine, ctx))