pi-tuck 1.3.2 → 1.3.3

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,17 @@
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.3.3] - 2026-11-20
7
+
8
+ ### Fixed
9
+
10
+ - **A learned server window no longer outlives a larger configured window.** When a server clips replies below the configured window,
11
+ pi-tuck learns the smaller window and keeps it in the session. It now also records the configured window it learned under, and
12
+ drops the learned value once the model's configured window differs, for example after a server and its models.json grew from
13
+ 68,000 to 131,072 tokens. Before, every reopened session stayed capped at the old window and compacted far too often. Learned
14
+ windows saved by earlier versions carry no configured window and are not reused; if the server still clips, the next clipped
15
+ reply learns it again.
16
+
6
17
  ## [1.3.2] - 2026-11-15
7
18
 
8
19
  ### Fixed
@@ -1 +1 @@
1
- export declare const BUILD_ID = "b6a612ac60abaf5564c6020053627662851d7c7162337cf023ed686c1d598466";
1
+ export declare const BUILD_ID = "1c07ecfee4b1f37efd4aacb4daef591585a9fafe75814b51ea6ae2a26d86f168";
@@ -1 +1 @@
1
- export const BUILD_ID = "b6a612ac60abaf5564c6020053627662851d7c7162337cf023ed686c1d598466";
1
+ export const BUILD_ID = "1c07ecfee4b1f37efd4aacb4daef591585a9fafe75814b51ea6ae2a26d86f168";
@@ -115,6 +115,8 @@ class Calibration {
115
115
  factor = 1;
116
116
  ratios = [];
117
117
  learnedContextWindow;
118
+ /** The configured window the learned window was learned under; the learned window applies only while the model still has it. */
119
+ learnedCatalogue;
118
120
  windowObservation;
119
121
  pending;
120
122
  lastCeiling = null;
@@ -132,6 +134,7 @@ class Calibration {
132
134
  if (reason === 'session') {
133
135
  this.factor = 1;
134
136
  this.learnedContextWindow = undefined;
137
+ this.learnedCatalogue = undefined;
135
138
  this.estimatedTokens = 0;
136
139
  // Keep the old ratios/ceiling until their existing readers replace them, as in 1.1.1.
137
140
  this.seededIdentity = '';
@@ -143,7 +146,8 @@ class Calibration {
143
146
  const branch = ctx.sessionManager.getBranch();
144
147
  this.ratios = seedWindow(branch, this.identity);
145
148
  this.factor = Math.max(1, ...this.ratios);
146
- this.learnedContextWindow = seedContextWindow(branch, this.identity);
149
+ this.learnedCatalogue = ctx.model?.contextWindow;
150
+ this.learnedContextWindow = seedContextWindow(branch, this.identity, this.learnedCatalogue);
147
151
  }
148
152
  learn(value, ctx, append) {
149
153
  this.ratios.push(value);
@@ -192,7 +196,7 @@ export async function installCompaction(pi, options = {}) {
192
196
  }
193
197
  });
194
198
  const captureDraft = (draft, ctx, reserve, reason, tokensBefore, retainCurrentInput, operatorManual, previousSummary, budget) => recorder.measureInline(() => {
195
- const attached = attachReceipt(draft, reason, reserve, contextWindow(ctx), { responseReserveTokens: budget?.response, hostInputTokens: budget?.host, mandatoryInputTokens: budget?.mandatory, target: budget?.target, triggerMarginTokens: budget?.margin, ...(cutAt === 1 ? {} : { cutAt }), calibrationFactor: calibration.factor, calibrationWindow: [...calibration.ratios], calibrationIdentity: calibration.identity, learnedContextWindow: calibration.learnedContextWindow, activeInputCeilingTokens: calibration.lastCeiling });
199
+ const attached = attachReceipt(draft, reason, reserve, contextWindow(ctx), { responseReserveTokens: budget?.response, hostInputTokens: budget?.host, mandatoryInputTokens: budget?.mandatory, target: budget?.target, triggerMarginTokens: budget?.margin, ...(cutAt === 1 ? {} : { cutAt }), calibrationFactor: calibration.factor, calibrationWindow: [...calibration.ratios], calibrationIdentity: calibration.identity, learnedContextWindow: calibration.learnedContextWindow, ...(calibration.learnedContextWindow !== undefined && calibration.learnedCatalogue !== undefined ? { catalogueContextWindow: calibration.learnedCatalogue } : {}), activeInputCeilingTokens: calibration.lastCeiling });
196
200
  proposalId = receiptId(attached);
197
201
  if (recorder.enabled) {
198
202
  try {
@@ -266,7 +270,9 @@ export async function installCompaction(pi, options = {}) {
266
270
  pi.on("model_select", (_event, ctx) => { calibration.select(ctx); });
267
271
  const contextWindow = (ctx) => {
268
272
  identityFor(ctx);
269
- return Math.min(ctx.model?.contextWindow ?? 0, calibration.learnedContextWindow ?? Infinity);
273
+ const model = ctx.model?.contextWindow ?? 0;
274
+ const learned = calibration.learnedCatalogue === ctx.model?.contextWindow ? calibration.learnedContextWindow : undefined;
275
+ return Math.min(model, learned ?? Infinity);
270
276
  };
271
277
  // This is the cap in Pi's built provider payload, including its context clamp and thinking adjustments.
272
278
  // An API with no explicit cap supplies no clipping evidence; never substitute the catalogue maximum.
@@ -339,9 +345,13 @@ export async function installCompaction(pi, options = {}) {
339
345
  if (message.stopReason === "length" && requested !== undefined &&
340
346
  Number.isSafeInteger(output) && output >= 0 && output < requested &&
341
347
  Number.isSafeInteger(bound) && bound > 0 && bound <= ctx.model.contextWindow) {
348
+ // A learned window from another configured window no longer bounds this one.
349
+ if (calibration.learnedCatalogue !== ctx.model.contextWindow)
350
+ calibration.learnedContextWindow = undefined;
351
+ calibration.learnedCatalogue = ctx.model.contextWindow;
342
352
  calibration.learnedContextWindow = Math.min(calibration.learnedContextWindow ?? Infinity, bound);
343
353
  serverClipped.add(message);
344
- calibration.windowObservation = { message, identity: key, window: calibration.learnedContextWindow };
354
+ calibration.windowObservation = { message, identity: key, window: calibration.learnedContextWindow, catalogue: ctx.model.contextWindow };
345
355
  }
346
356
  // A request that was answered cannot have held more than the model's window, so a report above it describes something that never happened.
347
357
  // Learning from one sets the factor for the requests after it: a 60,000-token cache read reported in a 32,768-token window made a
@@ -356,7 +366,7 @@ export async function installCompaction(pi, options = {}) {
356
366
  return;
357
367
  const observation = calibration.windowObservation;
358
368
  calibration.windowObservation = undefined;
359
- pi.appendEntry(WINDOW_RECEIPT, { calibrationIdentity: observation.identity, learnedContextWindow: observation.window,
369
+ pi.appendEntry(WINDOW_RECEIPT, { calibrationIdentity: observation.identity, learnedContextWindow: observation.window, catalogueContextWindow: observation.catalogue,
360
370
  serverClippedReplyId: event.messageEntryId });
361
371
  });
362
372
  // 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
@@ -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 = "b6a612ac60abaf5564c6020053627662851d7c7162337cf023ed686c1d598466";
3
+ export declare const DIAGNOSTIC_BUILD = "1c07ecfee4b1f37efd4aacb4daef591585a9fafe75814b51ea6ae2a26d86f168";
4
4
  export interface Receipt {
5
5
  id: string;
6
6
  build: string;
@@ -20,6 +20,7 @@ export interface Receipt {
20
20
  calibrationWindow?: number[];
21
21
  calibrationIdentity?: string;
22
22
  learnedContextWindow?: number;
23
+ catalogueContextWindow?: number;
23
24
  activeInputCeilingTokens?: number | null;
24
25
  };
25
26
  before: {
@@ -23,13 +23,18 @@ export declare const thinkingOnlyLength: (message: AgentMessage) => boolean;
23
23
  /** Thinking-only exhaustion uses the observed largest reply, not the full reserve. Every other failed length/overflow keeps its override. */
24
24
  export declare const needsFullReplyReserve: (message: AgentMessage) => boolean;
25
25
  /** Saved immediately at the reply's turn boundary, even when recovery cannot make a compaction. */
26
+ /** `catalogueContextWindow` is the model's configured window when the server clipped below it. A learned window holds only while the
27
+ * configured window is unchanged: a server or catalogue that grew must not stay capped by an old clip (Nautilus, 2026-11-20). */
26
28
  export interface WindowReceipt {
27
29
  calibrationIdentity: string;
28
30
  learnedContextWindow: number;
31
+ catalogueContextWindow: number;
29
32
  serverClippedReplyId: string;
30
33
  }
31
34
  export declare const WINDOW_RECEIPT = "pi-tuck-window";
32
- export declare function seedContextWindow(branch: readonly SessionEntry[], identity: string): number | undefined;
35
+ /** The newest learned window for `identity`, if it was learned under the model's current configured window. One recorded before 1.3.3
36
+ * carries no configured window and is not trusted: if the server still clips, the next clipped reply learns it again. */
37
+ export declare function seedContextWindow(branch: readonly SessionEntry[], identity: string, catalogue: number | undefined): number | undefined;
33
38
  /** One branch reader for the reserve override and reply floor, including history before cuts. A server-clipping receipt follows its reply. */
34
39
  export declare function replyFit(branch: readonly SessionEntry[]): {
35
40
  failed: boolean;
@@ -72,13 +72,17 @@ function windowSettings(entry) {
72
72
  return entry.details?.diagnosticReceipt?.settings;
73
73
  return undefined;
74
74
  }
75
- export function seedContextWindow(branch, identity) {
75
+ /** The newest learned window for `identity`, if it was learned under the model's current configured window. One recorded before 1.3.3
76
+ * carries no configured window and is not trusted: if the server still clips, the next clipped reply learns it again. */
77
+ export function seedContextWindow(branch, identity, catalogue) {
76
78
  for (let i = branch.length; i--;) {
77
79
  const settings = windowSettings(branch[i]);
78
80
  if (settings?.calibrationIdentity !== identity)
79
81
  continue;
80
82
  const window = settings.learnedContextWindow;
81
- return Number.isSafeInteger(window) && window > 0 ? window : undefined;
83
+ if (!(Number.isSafeInteger(window) && window > 0))
84
+ return undefined;
85
+ return catalogue !== undefined && settings.catalogueContextWindow === catalogue ? window : undefined;
82
86
  }
83
87
  return undefined;
84
88
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-tuck",
3
- "version": "1.3.2",
3
+ "version": "1.3.3",
4
4
  "description": "Deterministic compaction for Pi with no model call, plus exact recall of earlier history.",
5
5
  "keywords": [
6
6
  "pi-package",
package/src/build-id.ts CHANGED
@@ -1 +1 @@
1
- export const BUILD_ID = "b6a612ac60abaf5564c6020053627662851d7c7162337cf023ed686c1d598466";
1
+ export const BUILD_ID = "1c07ecfee4b1f37efd4aacb4daef591585a9fafe75814b51ea6ae2a26d86f168";
package/src/compaction.ts CHANGED
@@ -109,7 +109,9 @@ class Calibration {
109
109
  factor = 1;
110
110
  ratios: number[] = [];
111
111
  learnedContextWindow: number | undefined;
112
- windowObservation: { message: AgentMessage; identity: string; window: number } | undefined;
112
+ /** The configured window the learned window was learned under; the learned window applies only while the model still has it. */
113
+ learnedCatalogue: number | undefined;
114
+ windowObservation: { message: AgentMessage; identity: string; window: number; catalogue: number } | undefined;
113
115
  pending: { identity: string; tokens: number; messages: Parameters<typeof convertToLlm>[0]; snapshot: ReturnType<typeof convertToLlm>; requestedMaxTokens?: () => number | undefined } | undefined;
114
116
  lastCeiling: number | null = null;
115
117
  estimatedTokens = 0;
@@ -123,7 +125,7 @@ class Calibration {
123
125
  this.pending = undefined;
124
126
  if (reason !== 'identity') this.windowObservation = undefined;
125
127
  if (reason === 'session') {
126
- this.factor = 1; this.learnedContextWindow = undefined; this.estimatedTokens = 0;
128
+ this.factor = 1; this.learnedContextWindow = undefined; this.learnedCatalogue = undefined; this.estimatedTokens = 0;
127
129
  // Keep the old ratios/ceiling until their existing readers replace them, as in 1.1.1.
128
130
  this.seededIdentity = '';
129
131
  return;
@@ -133,7 +135,8 @@ class Calibration {
133
135
  const branch = ctx.sessionManager.getBranch();
134
136
  this.ratios = seedWindow(branch, this.identity);
135
137
  this.factor = Math.max(1, ...this.ratios);
136
- this.learnedContextWindow = seedContextWindow(branch, this.identity);
138
+ this.learnedCatalogue = ctx.model?.contextWindow;
139
+ this.learnedContextWindow = seedContextWindow(branch, this.identity, this.learnedCatalogue);
137
140
  }
138
141
  learn(value: number, ctx: ExtensionContext, append: (kind: string, data: unknown) => void) {
139
142
  this.ratios.push(value); if (this.ratios.length > CALIBRATION_WINDOW) this.ratios.shift(); this.factor = Math.max(1, ...this.ratios);
@@ -203,7 +206,7 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
203
206
  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; }
204
207
  });
205
208
  const captureDraft = (draft: NonNullable<ReturnType<typeof tuckDraft>["compaction"]>, ctx: ExtensionContext, reserve: number, reason: string, tokensBefore: number, retainCurrentInput: boolean, operatorManual: boolean, previousSummary?: string, budget?: { response: number; host: number; mandatory: number; target: Target; margin: number }) => recorder.measureInline(() => {
206
- const attached = attachReceipt(draft, reason, reserve, contextWindow(ctx), { responseReserveTokens: budget?.response, hostInputTokens: budget?.host, mandatoryInputTokens: budget?.mandatory, target: budget?.target, triggerMarginTokens: budget?.margin, ...(cutAt === 1 ? {} : { cutAt }), calibrationFactor: calibration.factor, calibrationWindow: [...calibration.ratios], calibrationIdentity: calibration.identity, learnedContextWindow: calibration.learnedContextWindow, activeInputCeilingTokens: calibration.lastCeiling });
209
+ const attached = attachReceipt(draft, reason, reserve, contextWindow(ctx), { responseReserveTokens: budget?.response, hostInputTokens: budget?.host, mandatoryInputTokens: budget?.mandatory, target: budget?.target, triggerMarginTokens: budget?.margin, ...(cutAt === 1 ? {} : { cutAt }), calibrationFactor: calibration.factor, calibrationWindow: [...calibration.ratios], calibrationIdentity: calibration.identity, learnedContextWindow: calibration.learnedContextWindow, ...(calibration.learnedContextWindow !== undefined && calibration.learnedCatalogue !== undefined ? { catalogueContextWindow: calibration.learnedCatalogue } : {}), activeInputCeilingTokens: calibration.lastCeiling });
207
210
  proposalId = receiptId(attached);
208
211
  if (recorder.enabled) { try {
209
212
  const filtered = permittedEntries(ctx.sessionManager.getEntries(), options.recallPolicy);
@@ -258,7 +261,9 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
258
261
  pi.on("model_select", (_event, ctx) => { calibration.select(ctx); });
259
262
  const contextWindow = (ctx: ExtensionContext) => {
260
263
  identityFor(ctx);
261
- return Math.min(ctx.model?.contextWindow ?? 0, calibration.learnedContextWindow ?? Infinity);
264
+ const model = ctx.model?.contextWindow ?? 0;
265
+ const learned = calibration.learnedCatalogue === ctx.model?.contextWindow ? calibration.learnedContextWindow : undefined;
266
+ return Math.min(model, learned ?? Infinity);
262
267
  };
263
268
  // This is the cap in Pi's built provider payload, including its context clamp and thinking adjustments.
264
269
  // An API with no explicit cap supplies no clipping evidence; never substitute the catalogue maximum.
@@ -319,9 +324,12 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
319
324
  if (message.stopReason === "length" && requested !== undefined &&
320
325
  Number.isSafeInteger(output) && output >= 0 && output < requested &&
321
326
  Number.isSafeInteger(bound) && bound > 0 && bound <= ctx.model.contextWindow) {
327
+ // A learned window from another configured window no longer bounds this one.
328
+ if (calibration.learnedCatalogue !== ctx.model.contextWindow) calibration.learnedContextWindow = undefined;
329
+ calibration.learnedCatalogue = ctx.model.contextWindow;
322
330
  calibration.learnedContextWindow = Math.min(calibration.learnedContextWindow ?? Infinity, bound);
323
331
  serverClipped.add(message);
324
- calibration.windowObservation = { message, identity: key, window: calibration.learnedContextWindow };
332
+ calibration.windowObservation = { message, identity: key, window: calibration.learnedContextWindow, catalogue: ctx.model.contextWindow };
325
333
  }
326
334
  // A request that was answered cannot have held more than the model's window, so a report above it describes something that never happened.
327
335
  // Learning from one sets the factor for the requests after it: a 60,000-token cache read reported in a 32,768-token window made a
@@ -332,7 +340,7 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
332
340
  if (standDown(ctx)) return;
333
341
  if (calibration.windowObservation?.message !== event.message) return;
334
342
  const observation = calibration.windowObservation; calibration.windowObservation = undefined;
335
- pi.appendEntry(WINDOW_RECEIPT, { calibrationIdentity: observation.identity, learnedContextWindow: observation.window,
343
+ pi.appendEntry(WINDOW_RECEIPT, { calibrationIdentity: observation.identity, learnedContextWindow: observation.window, catalogueContextWindow: observation.catalogue,
336
344
  serverClippedReplyId: event.messageEntryId } satisfies WindowReceipt);
337
345
  });
338
346
  // 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
@@ -8,7 +8,7 @@ import { createRecall, type RecallPolicy } from './recall.js';
8
8
  import type { SessionEntry } from '@earendil-works/pi-coding-agent';
9
9
  import { assertWindowsPrivateStorage, createPrivateDirectory, protectWindowsIncidentFile } from './windows-private-storage.js';
10
10
  export const DIAGNOSTIC_BUILD = BUILD_ID;
11
- export interface Receipt { id: string; build: string; reason: string; selectedView: string; boundary: string; settings: { draftReservedTokens: number; contextWindow: number; responseReserveTokens?: number; hostInputTokens?: number; mandatoryInputTokens?: number; target?: 'band' | 'room'; triggerMarginTokens?: number; cutAt?: number; calibrationFactor?: number; calibrationWindow?: number[]; calibrationIdentity?: string; learnedContextWindow?: number; activeInputCeilingTokens?: number | null }; before: { value: number; unit: 'estimated-input-tokens'; scope: 'native-preparation' | 'public-conversation' }; after: { value: number | null; unit: 'estimated-input-tokens'; scope: 'draft-preview' }; }
11
+ export interface Receipt { id: string; build: string; reason: string; selectedView: string; boundary: string; settings: { draftReservedTokens: number; contextWindow: number; responseReserveTokens?: number; hostInputTokens?: number; mandatoryInputTokens?: number; target?: 'band' | 'room'; triggerMarginTokens?: number; cutAt?: number; calibrationFactor?: number; calibrationWindow?: number[]; calibrationIdentity?: string; learnedContextWindow?: number; catalogueContextWindow?: number; activeInputCeilingTokens?: number | null }; before: { value: number; unit: 'estimated-input-tokens'; scope: 'native-preparation' | 'public-conversation' }; after: { value: number | null; unit: 'estimated-input-tokens'; scope: 'draft-preview' }; }
12
12
  export function attachReceipt<T extends { details?: unknown; firstKeptEntryId: string; tokensBefore: number }>(draft: T, reason: string, reserveTokens: number, contextWindow: number, resolved: Partial<Receipt['settings']> = {}): T & { details: Record<string, unknown> & { diagnosticReceipt: Receipt } } {
13
13
  const details = draft.details && typeof draft.details === 'object' ? draft.details as Record<string, unknown> : {};
14
14
  const receipt: Receipt = { id: randomUUID(), build: DIAGNOSTIC_BUILD, reason, selectedView: typeof details.view === 'string' ? details.view : 'deterministic-selected-draft', boundary: draft.firstKeptEntryId, settings: { draftReservedTokens: reserveTokens, contextWindow, ...resolved }, before: { value: draft.tokensBefore, unit: 'estimated-input-tokens', scope: reason === 'tool-loop' || reason === 'run-end' || reason === 'prompt' ? 'public-conversation' : 'native-preparation' }, after: { value: typeof details.tokensAfter === 'number' ? details.tokensAfter : null, unit: 'estimated-input-tokens', scope: 'draft-preview' } };
@@ -63,19 +63,24 @@ export const needsFullReplyReserve = (message: AgentMessage): boolean => message
63
63
  (message.stopReason === 'length' && !thinkingOnlyLength(message) || message.stopReason === 'error' && isContextOverflow(message));
64
64
 
65
65
  /** Saved immediately at the reply's turn boundary, even when recovery cannot make a compaction. */
66
- export interface WindowReceipt { calibrationIdentity: string; learnedContextWindow: number; serverClippedReplyId: string }
66
+ /** `catalogueContextWindow` is the model's configured window when the server clipped below it. A learned window holds only while the
67
+ * configured window is unchanged: a server or catalogue that grew must not stay capped by an old clip (Nautilus, 2026-11-20). */
68
+ export interface WindowReceipt { calibrationIdentity: string; learnedContextWindow: number; catalogueContextWindow: number; serverClippedReplyId: string }
67
69
  export const WINDOW_RECEIPT = 'pi-tuck-window';
68
70
  function windowSettings(entry: SessionEntry): Partial<WindowReceipt> | undefined {
69
71
  if (entry.type === 'custom' && entry.customType === WINDOW_RECEIPT) return entry.data as Partial<WindowReceipt> | undefined;
70
72
  if (entry.type === 'compaction') return (entry.details as { diagnosticReceipt?: { settings?: Partial<WindowReceipt> } } | undefined)?.diagnosticReceipt?.settings;
71
73
  return undefined;
72
74
  }
73
- export function seedContextWindow(branch: readonly SessionEntry[], identity: string): number | undefined {
75
+ /** The newest learned window for `identity`, if it was learned under the model's current configured window. One recorded before 1.3.3
76
+ * carries no configured window and is not trusted: if the server still clips, the next clipped reply learns it again. */
77
+ export function seedContextWindow(branch: readonly SessionEntry[], identity: string, catalogue: number | undefined): number | undefined {
74
78
  for (let i = branch.length; i--;) {
75
79
  const settings = windowSettings(branch[i]!);
76
80
  if (settings?.calibrationIdentity !== identity) continue;
77
81
  const window = settings.learnedContextWindow;
78
- return Number.isSafeInteger(window) && window! > 0 ? window : undefined;
82
+ if (!(Number.isSafeInteger(window) && window! > 0)) return undefined;
83
+ return catalogue !== undefined && settings.catalogueContextWindow === catalogue ? window : undefined;
79
84
  }
80
85
  return undefined;
81
86
  }