billion-context 0.1.127 → 0.1.128

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
@@ -60741,6 +60741,9 @@ var MARKER_TAIL = /(?:^|\n)[^\x00-\x7F](?:[ \t])?(?:\[ACP|\[AC|\[A|\[)?$/u;
60741
60741
  function mayStartMarkerLine(s3) {
60742
60742
  return s3.includes("[ACP]") || MARKER_TAIL.test(s3);
60743
60743
  }
60744
+ function stripMarkerLines(text) {
60745
+ return text.replace(MARKER_LINE, "");
60746
+ }
60744
60747
  function stripAcpTags(text) {
60745
60748
  let out = text;
60746
60749
  for (; ; ) {
@@ -61168,6 +61171,19 @@ function executeAnthropicProxyTool(toolName, args, ctx) {
61168
61171
  function refNum3(ref) {
61169
61172
  return Number(ref.replace(/\D/g, "")) || 0;
61170
61173
  }
61174
+ function compressibleSpanHint(state) {
61175
+ const refs = Object.keys(state.messageRefs?.byRef ?? {});
61176
+ const highest = refs.reduce((m2, r) => Math.max(m2, r.startsWith("m") ? Number(r.slice(1)) || 0 : 0), 0);
61177
+ const boundary = state.blocks.reduce((m2, b2) => b2.active && b2.endRef?.startsWith("m") ? Math.max(m2, Number(b2.endRef.slice(1)) || 0) : m2, 0);
61178
+ const fmt = (n) => `m${String(n).padStart(5, "0")}`;
61179
+ if (highest <= boundary) {
61180
+ const actives = state.blocks.filter((b2) => b2.active).map((b2) => b2.blockId);
61181
+ const span = actives.length >= 2 ? ` (e.g. startId ${actives[0]}, endId ${actives[actives.length - 1]})` : "";
61182
+ return ` No raw refs are directly compressible right now \u2014 compress a run of ACTIVE blocks instead${span}: fold their summaries into one higher-tier block. acp_status lists the current active blocks.`;
61183
+ }
61184
+ const covered = boundary > 0 ? ` (everything up to ${fmt(boundary)} is already inside active blocks)` : "";
61185
+ return ` Live compressible refs: ${fmt(boundary + 1)}\u2013${fmt(highest)}${covered}. Retry NOW in this same turn with startId/endId inside that span.`;
61186
+ }
61171
61187
  var M_REF_NUM_RE = /^m0*(\d{1,7})$/i;
61172
61188
  function normalizeRangeOrder(ranges) {
61173
61189
  let swapped = 0;
@@ -61192,7 +61208,7 @@ var FAIL_STREAK_CAP = 5;
61192
61208
  function normalizedSpecKey(ranges) {
61193
61209
  return ranges.map((r) => refNum3(r.startRef) <= refNum3(r.endRef) ? `${r.startRef}..${r.endRef}` : `${r.endRef}..${r.startRef}`).sort().join(",");
61194
61210
  }
61195
- function recordCompressFailure(session, key) {
61211
+ function recordCompressFailure(session, key, repeatAdvice) {
61196
61212
  if (!key) return "";
61197
61213
  const prev = session.metadata[FAIL_STREAK_KEY];
61198
61214
  const keys = Array.isArray(prev) ? prev.filter((k2) => typeof k2 === "string") : [];
@@ -61202,7 +61218,8 @@ function recordCompressFailure(session, key) {
61202
61218
  session.metadata[FAIL_STREAK_KEY] = keys;
61203
61219
  markDirty(session);
61204
61220
  if (occurrences < 2) return "";
61205
- return ` [Repeat-failure guard: you have now requested this exact range set ${occurrences} time(s) in this session and it keeps failing with the same error. Repeating it deterministically fails the same way until the visible context changes \u2014 do NOT re-issue it. Call acp_status first and pick from its CURRENT compressible ranges, or extend your range(s) to cover more adjacent messages.]`;
61221
+ const advice = repeatAdvice ?? "Call acp_status first and pick from its CURRENT compressible ranges, or extend your range(s) to cover more adjacent messages.";
61222
+ return ` [Repeat-failure guard: you have now requested this exact spec ${occurrences} time(s) in this session and it keeps failing with the same error. Repeating it deterministically fails the same way until the visible context changes \u2014 do NOT re-issue it. ${advice}]`;
61206
61223
  }
61207
61224
  function clearCompressFailures(session) {
61208
61225
  if (session.metadata[FAIL_STREAK_KEY] !== void 0) {
@@ -61210,15 +61227,42 @@ function clearCompressFailures(session) {
61210
61227
  markDirty(session);
61211
61228
  }
61212
61229
  }
61230
+ function currentRefsSnapshot(ctx) {
61231
+ let loId = "";
61232
+ let hiId = "";
61233
+ let loN = Infinity;
61234
+ let hiN = -1;
61235
+ for (const ref of Object.keys(ctx.session.state.messageRefs?.byRef ?? {})) {
61236
+ const m2 = M_REF_NUM_RE.exec(ref);
61237
+ if (!m2) continue;
61238
+ const n = Number(m2[1]);
61239
+ if (n < loN) {
61240
+ loN = n;
61241
+ loId = ref;
61242
+ }
61243
+ if (n > hiN) {
61244
+ hiN = n;
61245
+ hiId = ref;
61246
+ }
61247
+ }
61248
+ if (!loId || !hiId) return "";
61249
+ const activeBlocks2 = ctx.session.state.blocks.filter((b2) => b2.active).length;
61250
+ return ` [Current context: ${ctx.messages.length} visible message(s), refs ${loId}\u2013${hiId}, ${activeBlocks2} active block(s). Refs restart at m00001 after a session-generation change \u2014 request only refs inside this span, or call acp_status for exact compressible ranges.]`;
61251
+ }
61213
61252
  function applyRanges(parsed, ctx) {
61214
61253
  const { ranges, diagnostics } = parsed;
61215
61254
  if (ranges.length === 0) {
61216
61255
  ctx.log("[acp-proxy: compress call had no valid ranges; nothing compressed.]");
61217
- const reasons = diagnostics.invalidReasons?.slice(0, 8).map((r) => r.length > 200 ? r.slice(0, 200) + "..." : r) ?? [];
61218
- const why = reasons.length > 0 ? ` Rejected entries:
61219
- ${reasons.map((r) => `- ${r}`).join("\n")}` : "";
61220
- return `[Compression FAILED: no valid ranges parsed (kind=${diagnostics.kind}, dropped=${diagnostics.invalidItems}).${why}
61221
- compress requires a non-empty 'content' array where each element is EITHER an object {startId, endId, summary} OR one line-form string whose first line is 'mNNNNN\u2013mNNNNN optional topic' with the summary markdown on the following lines (a separate summary-only element right after a bare header line is also accepted). startId/endId are mNNNNN message refs from the conversation. Re-issue the compress call with a valid content array.]`;
61256
+ const rawReasons = diagnostics.invalidReasons ?? [];
61257
+ for (const reason of rawReasons) ctx.log(`[acp-proxy: rejected entry: ${reason}]`);
61258
+ const reasons = rawReasons.slice(0, 3).map((r) => r.length > 160 ? r.slice(0, 160) + "..." : r);
61259
+ const why = reasons.length > 0 ? ` Rejected entries: ${reasons.join(" | ")}.` : "";
61260
+ const guard = recordCompressFailure(
61261
+ ctx.session,
61262
+ `parse:${diagnostics.kind}:${diagnostics.invalidItems}:${rawReasons.slice(0, 3).join("|")}`,
61263
+ "Fix the argument shape against the format below instead of re-issuing the same malformed call."
61264
+ );
61265
+ return `[Compression FAILED: no valid ranges parsed (kind=${diagnostics.kind}, dropped=${diagnostics.invalidItems}).${why} compress requires a non-empty 'content' array where each element is EITHER an object {startId, endId, summary} OR one line-form string whose first line is 'mNNNNN\u2013mNNNNN optional topic' with the summary markdown on the following lines (a separate summary-only element right after a bare header line is also accepted). startId/endId are mNNNNN message refs from the conversation (call acp_status to see current refs).${compressibleSpanHint(ctx.session.state)} Re-issue the compress call with a valid content array.${guard}]`;
61222
61266
  }
61223
61267
  const revs = reversedRanges(ranges);
61224
61268
  const swappedRanges = normalizeRangeOrder(ranges);
@@ -61261,7 +61305,8 @@ ${reasons.map((r) => `- ${r}`).join("\n")}` : "";
61261
61305
  const errs = r.errors.join("; ") || "no blocks created";
61262
61306
  const revNote = revs.length > 0 ? ` Note: startId > endId in range(s) ${revs.map((rg) => `${rg.startRef}\u2192${rg.endRef}`).join(", ")} \u2014 your refs were reversed; they were normalized to ascending order before evaluation, so check your ref order.` : "";
61263
61307
  ctx.log(`[acp-proxy: compress FAILED ${detail} \u2192 0 blocks. ${errs}${revs.length > 0 ? " [reversed refs]" : ""}]`);
61264
- return `[Compression FAILED: ${errs}${revNote}${recordCompressFailure(ctx.session, normalizedSpecKey(ranges))}]`;
61308
+ const spanHint = ctx.session.state.blocks.some((b2) => b2.active) ? compressibleSpanHint(ctx.session.state) : "";
61309
+ return `[Compression FAILED: ${errs}${revNote}${currentRefsSnapshot(ctx)}${recordCompressFailure(ctx.session, normalizedSpecKey(ranges))}${spanHint}]`;
61265
61310
  }
61266
61311
  clearCompressFailures(ctx.session);
61267
61312
  const preContext = ctx.session.stats.lastInputTokens;
@@ -63060,9 +63105,14 @@ function dumpRejectedBody(status, sessionId, body) {
63060
63105
  }
63061
63106
 
63062
63107
  // src/strict-echo.ts
63063
- function isStrictReasoningEcho(session, upstreamOrigin) {
63108
+ function isStrictReasoningEcho(session, upstreamOrigin, model) {
63064
63109
  if (session.metadata.strictReasoningEcho === true) return true;
63065
- return upstreamOrigin !== void 0 && /deepseek/i.test(upstreamOrigin);
63110
+ if (upstreamOrigin !== void 0 && /deepseek/i.test(upstreamOrigin)) return true;
63111
+ return typeof model === "string" && model.length > 0 && /deepseek/i.test(model);
63112
+ }
63113
+ function modelIdOf(body) {
63114
+ const m2 = body?.model;
63115
+ return typeof m2 === "string" ? m2 : void 0;
63066
63116
  }
63067
63117
  function normalizeStrictEchoReasoning(messages, enabled, log2, sessionId) {
63068
63118
  if (!enabled) return messages;
@@ -63283,6 +63333,7 @@ async function* runCompressLoop(upstream, ctx, requestBody, requestOptions, adap
63283
63333
  let truncationRetried = false;
63284
63334
  let continuationRetried = false;
63285
63335
  let degenerateRetried = false;
63336
+ const seenMarkers = /* @__PURE__ */ new Set();
63286
63337
  const fetchUpstream = (body) => fetchWithRetry(
63287
63338
  requestOptions.url,
63288
63339
  {
@@ -63510,7 +63561,15 @@ async function* runCompressLoop(upstream, ctx, requestBody, requestOptions, adap
63510
63561
  }
63511
63562
  const result = await withSessionLock(ctx.session, () => executeProxyTool(call.name, parsedArgs, ctx, call.callId));
63512
63563
  proxyResults.push({ name: call.name, callId: call.callId, result, arguments: call.arguments, signature: call.signature });
63513
- if (ctx.visibilityMarkers !== false) yield adapter.emitMarker(call.name, result);
63564
+ if (ctx.visibilityMarkers !== false) {
63565
+ const markerKey = `${call.name}\0${result}`;
63566
+ if (seenMarkers.has(markerKey)) {
63567
+ ctx.log(`[acp-loop] suppressed duplicate ${call.name} status marker (identical failure repeated this request)`);
63568
+ } else {
63569
+ seenMarkers.add(markerKey);
63570
+ yield adapter.emitMarker(call.name, result);
63571
+ }
63572
+ }
63514
63573
  } else {
63515
63574
  realToolCalls.push(call);
63516
63575
  realCalls += 1;
@@ -63653,7 +63712,7 @@ async function* runCompressLoop(upstream, ctx, requestBody, requestOptions, adap
63653
63712
  ctx.log(`[acp-loop] round ${round}: proxy tool executed; re-requesting so the model sees the result`);
63654
63713
  if (signal?.aborted) break;
63655
63714
  let newBody = adapter.buildRequest(coreMessages, systemPrompt, requestBody);
63656
- newBody = normalizeStrictEchoBody(newBody, isStrictReasoningEcho(ctx.session, strictEchoOrigin), (level, msg) => log(level, `[acp-loop] ${msg}`), ctx.session.id ?? "unknown");
63715
+ newBody = normalizeStrictEchoBody(newBody, isStrictReasoningEcho(ctx.session, strictEchoOrigin, modelIdOf(requestBody)), (level, msg) => log(level, `[acp-loop] ${msg}`), ctx.session.id ?? "unknown");
63657
63716
  if (process.env.ACP_DUMP_BODY === "1") {
63658
63717
  try {
63659
63718
  const fs16 = await import("fs");
@@ -66370,6 +66429,41 @@ function stripAcpPanelMessages(messages) {
66370
66429
  }
66371
66430
  return stripped;
66372
66431
  }
66432
+ function stripAcpStatusMarkers(messages) {
66433
+ if (!Array.isArray(messages)) return 0;
66434
+ let stripped = 0;
66435
+ for (const rec of messages) {
66436
+ if (rec === null || typeof rec !== "object") continue;
66437
+ const m2 = rec;
66438
+ if (m2.role !== "user" && m2.role !== "assistant") continue;
66439
+ const content = m2.content;
66440
+ if (typeof content === "string") {
66441
+ const out = stripMarkerLines(content);
66442
+ if (out !== content) {
66443
+ m2.content = out.trim().length > 0 ? out : " ";
66444
+ stripped += countMarkerLines(content);
66445
+ }
66446
+ } else if (Array.isArray(content)) {
66447
+ for (const part of content) {
66448
+ if (part === null || typeof part !== "object") continue;
66449
+ const p2 = part;
66450
+ const text = p2.text;
66451
+ if (typeof text !== "string") continue;
66452
+ const out = stripMarkerLines(text);
66453
+ if (out !== text) {
66454
+ p2.text = out.trim().length > 0 ? out : " ";
66455
+ stripped += countMarkerLines(text);
66456
+ }
66457
+ }
66458
+ }
66459
+ }
66460
+ return stripped;
66461
+ }
66462
+ function countMarkerLines(text) {
66463
+ let n = 0;
66464
+ for (const _2 of text.matchAll(MARKER_LINE)) n++;
66465
+ return n;
66466
+ }
66373
66467
  function stripAcpPanelResponsesInput(input) {
66374
66468
  if (!Array.isArray(input)) return 0;
66375
66469
  let stripped = 0;
@@ -71126,7 +71220,7 @@ function prepareAnthropic(parsed, req, opts, core, config, prompts, surface, log
71126
71220
  const stream2 = parsed.stream === true;
71127
71221
  ++session.stats.requests;
71128
71222
  const injectTools = opts.compress.injectTool && !pluginMode;
71129
- const stripReasoning = (msgs) => withReasoningDrop(msgs, reasoning, log2, sessionId, isStrictReasoningEcho(session, upstreamOrigin));
71223
+ const stripReasoning = (msgs) => withReasoningDrop(msgs, reasoning, log2, sessionId, isStrictReasoningEcho(session, upstreamOrigin, modelIdOf(parsed)));
71130
71224
  if (isAutoModeClassifier(parsed)) {
71131
71225
  log2("info", `[${sessionId}] auto-mode classifier passthrough (skipping compress injection)`);
71132
71226
  return { body: JSON.stringify(parsed), session, processedMessages: [], originalMessages: [], anthropicSystem: parsed.system, protocol: "anthropic", stream: stream2, compressInjected: false, pluginMode, nudge: void 0, prompts, surface };
@@ -71141,6 +71235,10 @@ function prepareAnthropic(parsed, req, opts, core, config, prompts, surface, log
71141
71235
  if (strippedPanels > 0) {
71142
71236
  log2("info", `[${sessionId}] stripped ${strippedPanels} ACP panel message(s) before projection (UI-only, issue #359)`);
71143
71237
  }
71238
+ const strippedMarkerLines = stripAcpStatusMarkers(parsed.messages);
71239
+ if (strippedMarkerLines > 0) {
71240
+ log2("info", `[${sessionId}] stripped ${strippedMarkerLines} ACP status marker line(s) from incoming history (ephemeral proxy status, issue #1029)`);
71241
+ }
71144
71242
  try {
71145
71243
  const { msgs, cacheControls } = anthropicToCore(parsed);
71146
71244
  originalMessages = msgs;
@@ -71209,7 +71307,7 @@ function prepareOpenai(parsed, req, opts, core, config, prompts, surface, log2,
71209
71307
  const stream2 = parsed.stream === true;
71210
71308
  ++session.stats.requests;
71211
71309
  let openaiSystemText = "";
71212
- const stripReasoning = (msgs) => withReasoningDrop(msgs, reasoning, log2, sessionId, isStrictReasoningEcho(session, upstreamOrigin));
71310
+ const stripReasoning = (msgs) => withReasoningDrop(msgs, reasoning, log2, sessionId, isStrictReasoningEcho(session, upstreamOrigin, modelIdOf(parsed)));
71213
71311
  let openaiOutboundSystem;
71214
71312
  let processedMessages = [];
71215
71313
  let originalMessages = [];
@@ -71224,6 +71322,10 @@ function prepareOpenai(parsed, req, opts, core, config, prompts, surface, log2,
71224
71322
  if (strippedPanels > 0) {
71225
71323
  log2("info", `[${sessionId}] stripped ${strippedPanels} ACP panel message(s) before projection (UI-only, issue #359)`);
71226
71324
  }
71325
+ const strippedMarkerLines = stripAcpStatusMarkers(parsed.messages);
71326
+ if (strippedMarkerLines > 0) {
71327
+ log2("info", `[${sessionId}] stripped ${strippedMarkerLines} ACP status marker line(s) from incoming history (ephemeral proxy status, issue #1029)`);
71328
+ }
71227
71329
  try {
71228
71330
  const { msgs, systemText } = openaiToCore(parsed);
71229
71331
  openaiSystemText = systemText;
@@ -71283,7 +71385,7 @@ function prepareOpenai(parsed, req, opts, core, config, prompts, surface, log2,
71283
71385
  log2("warn", `[${sessionId}] kernel transform failed, forwarding unchanged: ${String(err2)}`);
71284
71386
  processedMessages = [];
71285
71387
  }
71286
- rebuiltMessages = normalizeStrictEchoReasoning(rebuiltMessages, isStrictReasoningEcho(session, upstreamOrigin), log2, sessionId);
71388
+ rebuiltMessages = normalizeStrictEchoReasoning(rebuiltMessages, isStrictReasoningEcho(session, upstreamOrigin, modelIdOf(parsed)), log2, sessionId);
71287
71389
  const rebuilt = { ...parsed, messages: rebuiltMessages, tools: toolsOut };
71288
71390
  warnReasoningPairs(rebuiltMessages, log2, sessionId);
71289
71391
  clampOutgoingOutput(rebuilt, typeof parsed.max_completion_tokens === "number" ? "max_completion_tokens" : "max_tokens", { systemText: openaiSystemText, tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, nativeWindow, imageTokens: imageTokensInParsedBody("openai", rebuilt, imageBillingFor(opts, billingUpstream ?? upstreamOrigin)) }, sessionId, log2);
@@ -71420,7 +71522,7 @@ function prepareResponses(parsed, req, opts, core, config, prompts, surface, log
71420
71522
  const sessionId = session.id;
71421
71523
  const stream2 = parsed.stream === true;
71422
71524
  ++session.stats.requests;
71423
- const stripReasoning = (msgs) => withReasoningDrop(msgs, reasoning, log2, sessionId, isStrictReasoningEcho(session, upstreamOrigin));
71525
+ const stripReasoning = (msgs) => withReasoningDrop(msgs, reasoning, log2, sessionId, isStrictReasoningEcho(session, upstreamOrigin, modelIdOf(parsed)));
71424
71526
  if (reconcileNativeCompactionBoundary(session)) {
71425
71527
  log2("info", `[${sessionId}] reconciled ACP state after native Responses compact boundary`);
71426
71528
  }
@@ -71454,6 +71556,10 @@ function prepareResponses(parsed, req, opts, core, config, prompts, surface, log
71454
71556
  if (strippedPanels > 0) {
71455
71557
  log2("info", `[${sessionId}] stripped ${strippedPanels} ACP panel message(s) before projection (UI-only, issue #359)`);
71456
71558
  }
71559
+ const strippedMarkerLines = stripAcpStatusMarkers(parsed.input);
71560
+ if (strippedMarkerLines > 0) {
71561
+ log2("info", `[${sessionId}] stripped ${strippedMarkerLines} ACP status marker line(s) from incoming history (ephemeral proxy status, issue #1029)`);
71562
+ }
71457
71563
  const shouldInject = opts.compress.injectTool;
71458
71564
  const injectTools = shouldInject && !pluginMode;
71459
71565
  const isCompactionTrigger = hasCompactionTrigger(parsed.input);