billion-context-pi 0.1.64-pr.357.148 → 0.1.64

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/dist/index.js CHANGED
@@ -10,7 +10,7 @@ import { readFileSync as readFileSync2 } from "fs";
10
10
  import { homedir as homedir8 } from "os";
11
11
  import { join as join12 } from "path";
12
12
 
13
- // node_modules/acp-kernel/dist/chunk-6TAK7DSI.js
13
+ // node_modules/acp-kernel/dist/chunk-JGNVAFFE.js
14
14
  import { createRequire } from "module";
15
15
  var require2 = createRequire(import.meta.url);
16
16
  function defaultCountTokens(text) {
@@ -19,12 +19,6 @@ function defaultCountTokens(text) {
19
19
  const cjkCount = cjk?.length ?? 0;
20
20
  return cjkCount + Math.ceil((text.length - cjkCount) / 4);
21
21
  }
22
- function thinkingTokenValue(thinking) {
23
- return typeof thinking === "number" && Number.isFinite(thinking) && thinking > 0 ? thinking : 0;
24
- }
25
- function countMessageTokens(message, countTokens = defaultCountTokens) {
26
- return countTokens(message.text ?? "") + thinkingTokenValue(message.thinkingTokens);
27
- }
28
22
  function estimateTokensFast(text) {
29
23
  if (!text) return 0;
30
24
  return Math.ceil(text.length / 4);
@@ -1570,8 +1564,7 @@ function renderMessage(message, map, countTokens, strategy, snapshot = null) {
1570
1564
  "^" + escapeRegex(TAG_OPEN) + "[^>]*" + GT + escapeRegex(ref) + escapeRegex(TAG_CLOSE) + "\\n?"
1571
1565
  );
1572
1566
  const cleanText = (message.text || "").replace(ownTagRe, "");
1573
- const textTokens = snapshot ? snapshot[ref] ?? (snapshot[ref] = countTokens(cleanText)) : countTokens(cleanText);
1574
- const tokens = textTokens + thinkingTokenValue(message.thinkingTokens);
1567
+ const tokens = snapshot ? snapshot[ref] ?? (snapshot[ref] = countTokens(cleanText)) : countTokens(cleanText);
1575
1568
  const type = classifyType(message);
1576
1569
  const prefix = acpTag(ref, tokens, type) + "\n";
1577
1570
  if (!cleanText) return { ...message, text: prefix };
@@ -1690,7 +1683,7 @@ function computeProtectedRefs(messages, state, config, countTokens = estimateTex
1690
1683
  if (isNeverPreserveRecent(msg)) continue;
1691
1684
  const ref = state.messageRefs.byRaw[msg.id];
1692
1685
  if (!ref || ref === "BLOCKED") continue;
1693
- visible.push({ ref, tokens: countMessageTokens(msg, countTokens) });
1686
+ visible.push({ ref, tokens: countTokens(msg.text ?? "") });
1694
1687
  }
1695
1688
  if (preserveN > 0) {
1696
1689
  for (const m2 of visible.slice(-preserveN)) {
@@ -1733,7 +1726,7 @@ function buildCompressibleRanges(messages, state, config, protectedZoneRefs, cou
1733
1726
  protectedMsgs.push({
1734
1727
  ref,
1735
1728
  gapBefore: skipSinceProtected,
1736
- tokens: countMessageTokens(msg, countTokens),
1729
+ tokens: countTokens(msg.text ?? ""),
1737
1730
  tools: msg.toolName ? [msg.toolName] : []
1738
1731
  });
1739
1732
  skipSinceProtected = false;
@@ -1748,7 +1741,7 @@ function buildCompressibleRanges(messages, state, config, protectedZoneRefs, cou
1748
1741
  compressibleMsgs.push({
1749
1742
  ref,
1750
1743
  gapBefore: skipSinceCompressible,
1751
- tokens: countMessageTokens(msg, countTokens),
1744
+ tokens: countTokens(msg.text ?? ""),
1752
1745
  chars: (msg.text ?? "").length,
1753
1746
  isTool: isToolMessage(msg),
1754
1747
  isUser: msg.role === "user"
@@ -2394,7 +2387,7 @@ function applySingleRange(input) {
2394
2387
  let compressedTokens = 0;
2395
2388
  for (const id of filteredIds) {
2396
2389
  const message = input.messages.find((entry) => entry.id === id);
2397
- compressedTokens += message ? countMessageTokens(message, input.countTokens) : 0;
2390
+ compressedTokens += input.countTokens(message?.text ?? "");
2398
2391
  }
2399
2392
  for (const consumedId of consumedBlockIds) {
2400
2393
  const consumed = blockById(input.state, consumedId);
@@ -2601,9 +2594,6 @@ function decideNudge(input) {
2601
2594
  const growthReady = firstSightMassReady || growthSinceReference >= growthFloor;
2602
2595
  const t2Count = tiers[2]?.targetBlocks.length ?? 0;
2603
2596
  const t3Count = tiers[3]?.targetBlocks.length ?? 0;
2604
- const tierCountUsageFloor = config.nudge.minContextLimitPct;
2605
- const t2CountReady = t2Count >= config.tiers.tier2Trigger && usage >= tierCountUsageFloor;
2606
- const t3CountReady = t3Count >= config.tiers.tier3Trigger && usage >= tierCountUsageFloor;
2607
2597
  if (pressure) {
2608
2598
  const candidates = [1];
2609
2599
  if (config.tiers.enabled) {
@@ -2626,19 +2616,19 @@ function decideNudge(input) {
2626
2616
  if (t1Eff >= nudgeGrowthTokens) {
2627
2617
  injectedTier = 1;
2628
2618
  injectedReason = `T1 effective ${t1Eff} >= ${nudgeGrowthTokens}, growth ${growthSinceReference}, usage ${Math.round(usage * 100)}%`;
2629
- } else if (config.tiers.enabled && (t2CountReady || t2Pen >= tier2Threshold && t2Pen > t1Eff)) {
2619
+ } else if (config.tiers.enabled && (t2Count >= config.tiers.tier2Trigger || t2Pen >= tier2Threshold && t2Pen > t1Eff)) {
2630
2620
  const lastShown = state.nudge.lastShownByTier[2] ?? 0;
2631
2621
  const cadenceMet = lastShown === 0 || tokenCount - lastShown >= growthFloor;
2632
2622
  if (cadenceMet) {
2633
2623
  injectedTier = 2;
2634
- injectedReason = t2CountReady ? `T2 distill ready: ${t2Count} tier-1 blocks >= tier2Trigger ${config.tiers.tier2Trigger} (${t2Pen} tokens), usage ${Math.round(usage * 100)}%` : `T2 distill ready: ${tiers[2].targetBlocks.length} tier-1 blocks (${t2Pen} tokens) >= ${tier2Threshold} (1.5x) and > T1 effective ${t1Eff}, usage ${Math.round(usage * 100)}%`;
2624
+ injectedReason = t2Count >= config.tiers.tier2Trigger ? `T2 distill ready: ${t2Count} tier-1 blocks >= tier2Trigger ${config.tiers.tier2Trigger} (${t2Pen} tokens), usage ${Math.round(usage * 100)}%` : `T2 distill ready: ${tiers[2].targetBlocks.length} tier-1 blocks (${t2Pen} tokens) >= ${tier2Threshold} (1.5x) and > T1 effective ${t1Eff}, usage ${Math.round(usage * 100)}%`;
2635
2625
  }
2636
- } else if (config.tiers.enabled && (t3CountReady || t3Pen >= tier2Threshold && t3Pen > t2Pen && t3Pen > t1Eff)) {
2626
+ } else if (config.tiers.enabled && (t3Count >= config.tiers.tier3Trigger || t3Pen >= tier2Threshold && t3Pen > t2Pen && t3Pen > t1Eff)) {
2637
2627
  const lastShown = state.nudge.lastShownByTier[3] ?? 0;
2638
2628
  const cadenceMet = lastShown === 0 || tokenCount - lastShown >= growthFloor;
2639
2629
  if (cadenceMet) {
2640
2630
  injectedTier = 3;
2641
- injectedReason = t3CountReady ? `T3 condense ready: ${t3Count} tier-2 blocks >= tier3Trigger ${config.tiers.tier3Trigger} (${t3Pen} tokens), usage ${Math.round(usage * 100)}%` : `T3 condense ready: ${tiers[3].targetBlocks.length} tier-2 blocks (${t3Pen} tokens) >= ${tier2Threshold} (1.5x) and > T2 ${t2Pen} and > T1 effective ${t1Eff}, usage ${Math.round(usage * 100)}%`;
2631
+ injectedReason = t3Count >= config.tiers.tier3Trigger ? `T3 condense ready: ${t3Count} tier-2 blocks >= tier3Trigger ${config.tiers.tier3Trigger} (${t3Pen} tokens), usage ${Math.round(usage * 100)}%` : `T3 condense ready: ${tiers[3].targetBlocks.length} tier-2 blocks (${t3Pen} tokens) >= ${tier2Threshold} (1.5x) and > T2 ${t2Pen} and > T1 effective ${t1Eff}, usage ${Math.round(usage * 100)}%`;
2642
2632
  }
2643
2633
  }
2644
2634
  }
@@ -2655,14 +2645,9 @@ function decideNudge(input) {
2655
2645
  } else {
2656
2646
  const tiersList = [1, 2, 3];
2657
2647
  const eligible = tiersList.filter((t) => config.tiers.enabled || t === 1);
2658
- const countReadyUngated = (t) => t === 2 ? t2Count >= config.tiers.tier2Trigger : t === 3 ? t3Count >= config.tiers.tier3Trigger : false;
2659
- const countReady = (t) => countReadyUngated(t) && usage >= tierCountUsageFloor;
2648
+ const countReady = (t) => t === 2 ? t2Count >= config.tiers.tier2Trigger : t === 3 ? t3Count >= config.tiers.tier3Trigger : false;
2660
2649
  const ready = eligible.filter((t) => (tiers[t]?.pending ?? 0) >= nudgeGrowthTokens).map((t) => `T${t} ${tiers[t].pending}`);
2661
- const readyCount = eligible.filter(
2662
- (t) => (tiers[t]?.pending ?? 0) < nudgeGrowthTokens && countReadyUngated(t)
2663
- ).map(
2664
- (t) => `T${t} ${t === 2 ? t2Count : t3Count} blocks (count${usage >= tierCountUsageFloor ? "" : ", usage-gated"})`
2665
- );
2650
+ const readyCount = eligible.filter((t) => (tiers[t]?.pending ?? 0) < nudgeGrowthTokens && countReady(t)).map((t) => `T${t} ${t === 2 ? t2Count : t3Count} blocks (count)`);
2666
2651
  const readyAll = [...ready, ...readyCount];
2667
2652
  const readyHint = readyAll.length > 0 ? `, ready: ${readyAll.join(", ")}` : "";
2668
2653
  const blocked = eligible.filter(
@@ -2724,7 +2709,7 @@ function computeContextBreakdown(messages, total, growth, countTokens) {
2724
2709
  const count = countTokens ?? ((t) => Math.ceil(t.length / 4));
2725
2710
  let system = 0, tool = 0, summaries = 0, code = 0, text = 0;
2726
2711
  for (const msg of messages) {
2727
- const tokens = countMessageTokens(msg, count);
2712
+ const tokens = count(msg.text ?? "");
2728
2713
  if (msg.text?.startsWith("[Compressed conversation section]")) {
2729
2714
  summaries += tokens;
2730
2715
  } else if (msg.contentType === "tool-call" || msg.contentType === "tool-result") {
@@ -2886,7 +2871,7 @@ function collectVisible(messages, state, countTokens) {
2886
2871
  if (coveredIds.has(message.id)) return;
2887
2872
  const ref = refForRaw(state.messageRefs, message.id);
2888
2873
  if (!ref) return;
2889
- const tokens = countMessageTokens(message, countTokens);
2874
+ const tokens = countTokens(message.text ?? "");
2890
2875
  const tool = isToolMessage(message) ? message.toolName ?? (message.toolCallId ? toolCallNames.get(message.toolCallId) : void 0) ?? "tool" : "text";
2891
2876
  if (tokens > 0) visible.push({ ref, tokens, tool, index });
2892
2877
  });
@@ -4103,8 +4088,6 @@ function projectMessage(message, id) {
4103
4088
  }];
4104
4089
  }
4105
4090
  if (role === "assistant") {
4106
- const thinking = thinkingTokenCount(msg.content);
4107
- const thinkingField = thinking > 0 ? { thinkingTokens: thinking } : {};
4108
4091
  const calls = allToolCalls(msg.content);
4109
4092
  if (calls.length > 0) {
4110
4093
  const textParts = extractText(msg.content);
@@ -4113,9 +4096,9 @@ function projectMessage(message, id) {
4113
4096
  const argStr = stringifyArgs(call.arguments);
4114
4097
  const text2 = argStr && textParts ? `${textParts}
4115
4098
  ${argStr}` : argStr || textParts;
4116
- return [{ id, role: "assistant", contentType: "tool-call", toolName: call.name, toolCallId: call.id, text: text2, ...thinkingField }];
4099
+ return [{ id, role: "assistant", contentType: "tool-call", toolName: call.name, toolCallId: call.id, text: text2 }];
4117
4100
  }
4118
- return calls.map((call, i) => {
4101
+ return calls.map((call) => {
4119
4102
  const argStr = stringifyArgs(call.arguments);
4120
4103
  return {
4121
4104
  id: `${id}#${call.id}`,
@@ -4123,14 +4106,13 @@ ${argStr}` : argStr || textParts;
4123
4106
  contentType: "tool-call",
4124
4107
  toolName: call.name,
4125
4108
  toolCallId: call.id,
4126
- text: argStr || textParts,
4127
- ...i === 0 ? thinkingField : {}
4109
+ text: argStr || textParts
4128
4110
  };
4129
4111
  });
4130
4112
  }
4131
4113
  const text = extractText(msg.content);
4132
4114
  if (!text.trim()) return [];
4133
- return [{ id, role: "assistant", contentType: "text", text, ...thinkingField }];
4115
+ return [{ id, role: "assistant", contentType: "text", text }];
4134
4116
  }
4135
4117
  const customText = extractText(msg.content) || fallbackText(msg);
4136
4118
  return customText.length > 0 ? [{ id, role: "user", contentType: "text", text: customText }] : [];
@@ -4158,15 +4140,6 @@ function extractText(content) {
4158
4140
  }
4159
4141
  return parts.join("\n");
4160
4142
  }
4161
- function thinkingTokenCount(content) {
4162
- if (!Array.isArray(content)) return 0;
4163
- const parts = [];
4164
- for (const block of content) {
4165
- const b2 = block;
4166
- if (b2.type === "thinking" && typeof b2.thinking === "string") parts.push(b2.thinking);
4167
- }
4168
- return parts.length > 0 ? defaultCountTokens(parts.join("\n")) : 0;
4169
- }
4170
4143
  function stripRefTag(text) {
4171
4144
  return text.replace(REF_TAG, "").replace(TRAILING_REF_TAG, "");
4172
4145
  }
@@ -9796,7 +9769,6 @@ function estimateTokens(messages, coveredIds, imageTokensById) {
9796
9769
  if (m2.toolName === "compress") continue;
9797
9770
  if (coveredIds?.has(m2.id)) continue;
9798
9771
  tokens += defaultCountTokens(m2.text ?? "");
9799
- tokens += m2.thinkingTokens ?? 0;
9800
9772
  const img = imageTokensById?.get(m2.id);
9801
9773
  if (img) tokens += img;
9802
9774
  }
@@ -16861,8 +16833,7 @@ async function statusReport(runtime, ctx) {
16861
16833
  const systemPromptTokens = systemPromptText ? defaultCountTokens(systemPromptText) : 0;
16862
16834
  const imageTokens = collectImageTokens(entries, modelSupportsImages(ctx.model));
16863
16835
  const imageTokensTotal = [...imageTokens.values()].reduce((a, b2) => a + b2, 0);
16864
- const thinkingTokensTotal = coreMessages.reduce((sum, m2) => sum + (m2.thinkingTokens ?? 0), 0);
16865
- const sessionTokens = !anchorStale && realUsage?.tokens && realUsage.tokens > 0 ? realUsage.tokens : defaultCountTokens(coreMessages.map((m2) => m2.text ?? "").join("\n")) + imageTokensTotal + thinkingTokensTotal;
16836
+ const sessionTokens = !anchorStale && realUsage?.tokens && realUsage.tokens > 0 ? realUsage.tokens : defaultCountTokens(coreMessages.map((m2) => m2.text ?? "").join("\n")) + imageTokensTotal;
16866
16837
  const coveredIds = collectCoveredMessageIds(state);
16867
16838
  const sentTokens = estimateTokens(coreMessages, coveredIds, imageTokens) + systemPromptTokens;
16868
16839
  const viewSentTokens = adjustedTokenCount(runtime.core, coreMessages, state, config, sentTokens, imageTokens, systemPromptTokens);
@@ -16875,7 +16846,7 @@ async function statusReport(runtime, ctx) {
16875
16846
  state: turn.state,
16876
16847
  nudge: turn.nudge,
16877
16848
  modelContextLimit: config.modelContextLimit,
16878
- unprunedTokens: coreMessages.reduce((sum, m2) => sum + defaultCountTokens(m2.text ?? "") + (m2.thinkingTokens ?? 0) + (imageTokens.get(m2.id) ?? 0), 0),
16849
+ unprunedTokens: coreMessages.reduce((sum, m2) => sum + defaultCountTokens(m2.text ?? "") + (imageTokens.get(m2.id) ?? 0), 0),
16879
16850
  cacheUsages: cacheUsageSamples(entries ?? [])
16880
16851
  });
16881
16852
  const delegateUsage = getDelegateUsage();