billion-context-pi 0.1.83-pr.596.411 → 0.1.83-pr.601.415

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.
@@ -26,8 +26,8 @@ export declare function usageAnchorPredatesCompression(entries: AnchorEntry[]):
26
26
  export interface AnchorStaleness {
27
27
  predates: boolean;
28
28
  netReclaimed: number;
29
- anchorTotal: number;
30
- trustedCeiling: number;
29
+ fresh: boolean;
30
+ lastRealTokens: number;
31
31
  }
32
32
  export declare function compressionAnchorStaleness(entries: AnchorEntry[], blocks: readonly CompressionBlock[], countTokens: (text: string) => number): AnchorStaleness;
33
33
  export {};
package/dist/index.js CHANGED
@@ -19330,24 +19330,30 @@ function usageTotal(u) {
19330
19330
  function validAnchorUsage(u) {
19331
19331
  return usageTotal(u) > 0;
19332
19332
  }
19333
- function anchorUsageTotal(entry) {
19334
- return usageTotal(entry.message?.usage);
19333
+ function isFreshAnchor(m2) {
19334
+ return m2.role === "assistant" && m2.stopReason !== "aborted" && m2.stopReason !== "error" && validAnchorUsage(m2.usage);
19335
19335
  }
19336
19336
  function scanEntries(entries) {
19337
19337
  let lastUsageIdx = -1;
19338
19338
  let lastCompressIdx = -1;
19339
+ let lastAssistantIdx = -1;
19340
+ let lastUsageTotal = 0;
19339
19341
  const compressIdxByCallId = /* @__PURE__ */ new Map();
19340
19342
  for (let i = 0; i < entries.length; i++) {
19341
19343
  const m2 = entries[i].message;
19342
19344
  if (!m2) continue;
19343
- if (m2.role === "assistant" && m2.stopReason !== "aborted" && m2.stopReason !== "error" && validAnchorUsage(m2.usage)) {
19344
- lastUsageIdx = i;
19345
+ if (m2.role === "assistant") {
19346
+ lastAssistantIdx = i;
19347
+ if (isFreshAnchor(m2)) {
19348
+ lastUsageIdx = i;
19349
+ lastUsageTotal = usageTotal(m2.usage);
19350
+ }
19345
19351
  } else if (m2.role === "toolResult" && m2.toolName === "compress" && m2.toolCallId !== void 0 && m2.isError !== true && isCompressSuccessText(extractText(m2.content))) {
19346
19352
  lastCompressIdx = i;
19347
19353
  compressIdxByCallId.set(m2.toolCallId, i);
19348
19354
  }
19349
19355
  }
19350
- return { lastUsageIdx, lastCompressIdx, compressIdxByCallId };
19356
+ return { lastUsageIdx, lastCompressIdx, compressIdxByCallId, lastAssistantIdx, lastUsageTotal };
19351
19357
  }
19352
19358
  function usageAnchorPredatesCompression(entries) {
19353
19359
  const scan = scanEntries(entries);
@@ -19363,18 +19369,9 @@ function compressionAnchorStaleness(entries, blocks, countTokens) {
19363
19369
  const saved = (block.compressedTokens ?? 0) - countTokens(block.summary ?? "");
19364
19370
  netReclaimed += saved > 0 ? saved : 0;
19365
19371
  }
19366
- const anchorTotal = scan.lastUsageIdx >= 0 ? anchorUsageTotal(entries[scan.lastUsageIdx]) : 0;
19367
- let trailingEstimate = 0;
19368
- for (let i = scan.lastUsageIdx + 1; i < entries.length; i++) {
19369
- const m2 = entries[i].message;
19370
- if (m2 && m2.content != null) trailingEstimate += countTokens(extractText(m2.content));
19371
- }
19372
- return {
19373
- predates: scan.lastCompressIdx > scan.lastUsageIdx,
19374
- netReclaimed,
19375
- anchorTotal,
19376
- trustedCeiling: anchorTotal > 0 ? anchorTotal + trailingEstimate : 0
19377
- };
19372
+ const lastAssistant = scan.lastAssistantIdx >= 0 ? entries[scan.lastAssistantIdx] : void 0;
19373
+ const fresh = !!lastAssistant?.message && isFreshAnchor(lastAssistant.message);
19374
+ return { predates: scan.lastCompressIdx > scan.lastUsageIdx, netReclaimed, fresh, lastRealTokens: scan.lastUsageTotal };
19378
19375
  }
19379
19376
 
19380
19377
  // src/delegate-tool.ts
@@ -24109,16 +24106,14 @@ function wireContextTransform(pi, runtime, standDownIfProxied) {
24109
24106
  armedFloor = Math.floor(config.modelContextLimit * 0.95);
24110
24107
  logWarn("overflow-selfheal", { sid, event: "armed-emergency", floor: armedFloor, limit: config.modelContextLimit });
24111
24108
  }
24112
- const { predates, netReclaimed, anchorTotal, trustedCeiling } = compressionAnchorStaleness(entries, state.blocks, defaultCountTokens);
24113
- const realPromptTokens = realUsage?.tokens ?? 0;
24114
- const rawHostFloor = realPromptTokens > 0 ? Math.max(0, realPromptTokens - (predates ? netReclaimed : 0)) : 0;
24115
- const imageTokenSum = [...imageTokens.values()].reduce((a, b2) => a + b2, 0);
24116
- const hostCeiling = trustedCeiling > 0 ? trustedCeiling + imageTokenSum : 0;
24117
- const hostFloor = hostCeiling > 0 ? Math.min(rawHostFloor, hostCeiling) : rawHostFloor;
24118
- if (hostFloor < rawHostFloor) {
24119
- logWarn("turn", { sid, event: "host-floor-capped", reason: "host-fallback-estimate", raw: rawHostFloor, capped: hostFloor, anchor: anchorTotal, ceiling: hostCeiling, view: sentTokens });
24109
+ const { predates, netReclaimed, fresh, lastRealTokens } = compressionAnchorStaleness(entries, state.blocks, defaultCountTokens);
24110
+ const reportedHost = realUsage?.tokens ?? 0;
24111
+ const realPromptTokens = fresh ? reportedHost : lastRealTokens;
24112
+ if (!fresh && reportedHost > 0) {
24113
+ logInfo("turn", { sid, event: "host-tree-sum-rejected", reported: reportedHost, flooredAt: lastRealTokens });
24120
24114
  }
24121
- const hostUsageStable = !predates && realPromptTokens > 0 ? runtime.noteHostUsage(sid, realPromptTokens) : false;
24115
+ const hostFloor = realPromptTokens > 0 ? Math.max(0, realPromptTokens - (predates ? netReclaimed : 0)) : 0;
24116
+ const hostUsageStable = fresh && !predates && realPromptTokens > 0 ? runtime.noteHostUsage(sid, realPromptTokens) : false;
24122
24117
  const calibrate = (base) => hostUsageStable ? Math.min(base, Math.ceil(realPromptTokens * 1.2)) : base;
24123
24118
  const applyFloors = (base) => Math.max(calibrate(base), hostFloor, armedFloor);
24124
24119
  const guardBasis = (base) => Math.max(calibrate(base), hostFloor);
@@ -24142,7 +24137,7 @@ function wireContextTransform(pi, runtime, standDownIfProxied) {
24142
24137
  }
24143
24138
  }
24144
24139
  ov.noteSentView(guardTokens, config.modelContextLimit);
24145
- const sizeDivergent = !predates && realPromptTokens > 0 && Math.abs(tokenCount - realPromptTokens) / realPromptTokens > 0.5;
24140
+ const sizeDivergent = fresh && !predates && realPromptTokens > 0 && Math.abs(tokenCount - realPromptTokens) / realPromptTokens > 0.5;
24146
24141
  if (runtime.noteSizeDivergence(sid, sizeDivergent)) {
24147
24142
  logWarn("turn", { sid, event: "size-divergence", est: tokenCount, host: realPromptTokens, ratio: Number((tokenCount / realPromptTokens).toFixed(2)), stable: hostUsageStable });
24148
24143
  }