billion-context 0.1.75-pr.490.310 → 0.1.75-pr.491.314
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 +81 -3
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -48479,11 +48479,14 @@ ${summary}` }]
|
|
|
48479
48479
|
}
|
|
48480
48480
|
return { items, replaced, dropped };
|
|
48481
48481
|
}
|
|
48482
|
-
function
|
|
48483
|
-
if (
|
|
48482
|
+
function codexCompactGatePre(session, effectiveLimit) {
|
|
48483
|
+
if (effectiveLimit <= 0) return false;
|
|
48484
48484
|
if (session.stats.lastInputTokens >= effectiveLimit * 0.9) return false;
|
|
48485
48485
|
return session.state.blocks.some((b2) => b2.active);
|
|
48486
48486
|
}
|
|
48487
|
+
function codexCompactGate(session, effectiveLimit, transformOk) {
|
|
48488
|
+
return transformOk && codexCompactGatePre(session, effectiveLimit);
|
|
48489
|
+
}
|
|
48487
48490
|
function buildTriggerForgeBody(summaryText, usage, stream2) {
|
|
48488
48491
|
const compactionItem = {
|
|
48489
48492
|
type: "compaction",
|
|
@@ -51306,6 +51309,63 @@ function reapOrphanBlocks(session, visible, deactivate) {
|
|
|
51306
51309
|
return { reaped };
|
|
51307
51310
|
}
|
|
51308
51311
|
|
|
51312
|
+
// src/ref-gc.ts
|
|
51313
|
+
var CITED_REF = /\bm(\d{5})\b/g;
|
|
51314
|
+
var REF_CAPACITY_WARN = 9e4;
|
|
51315
|
+
function gcMessageRefs(session, incoming, outgoing, log2) {
|
|
51316
|
+
const { byRaw, byRef } = session.state.messageRefs;
|
|
51317
|
+
const tokenSnapshot = session.state.tokenSnapshot;
|
|
51318
|
+
const live = /* @__PURE__ */ new Set();
|
|
51319
|
+
for (const m2 of incoming) if (m2.id) live.add(m2.id);
|
|
51320
|
+
for (const m2 of outgoing) if (m2.id) live.add(m2.id);
|
|
51321
|
+
for (const match of citedRefs(outgoing)) {
|
|
51322
|
+
const rawId = byRef[`m${match}`];
|
|
51323
|
+
if (rawId) live.add(rawId);
|
|
51324
|
+
}
|
|
51325
|
+
for (const b2 of session.state.blocks) {
|
|
51326
|
+
if (!b2.active) continue;
|
|
51327
|
+
for (const id of b2.effectiveMessageIds) live.add(id);
|
|
51328
|
+
}
|
|
51329
|
+
const nextByRaw = {};
|
|
51330
|
+
let pruned = 0;
|
|
51331
|
+
for (const [rawId, ref] of Object.entries(byRaw)) {
|
|
51332
|
+
if (live.has(rawId)) nextByRaw[rawId] = ref;
|
|
51333
|
+
else pruned++;
|
|
51334
|
+
}
|
|
51335
|
+
const nextByRef = {};
|
|
51336
|
+
for (const [ref, rawId] of Object.entries(byRef)) {
|
|
51337
|
+
if (live.has(rawId)) nextByRef[ref] = rawId;
|
|
51338
|
+
}
|
|
51339
|
+
if (pruned === 0) return { pruned };
|
|
51340
|
+
const nextSnapshot = {};
|
|
51341
|
+
for (const [ref, tokens] of Object.entries(tokenSnapshot ?? {})) {
|
|
51342
|
+
if (nextByRef[ref] !== void 0) nextSnapshot[ref] = tokens;
|
|
51343
|
+
}
|
|
51344
|
+
session.state = {
|
|
51345
|
+
...session.state,
|
|
51346
|
+
messageRefs: {
|
|
51347
|
+
...session.state.messageRefs,
|
|
51348
|
+
byRaw: nextByRaw,
|
|
51349
|
+
byRef: nextByRef
|
|
51350
|
+
},
|
|
51351
|
+
tokenSnapshot: nextSnapshot
|
|
51352
|
+
};
|
|
51353
|
+
log2("info", `[${session.id}] ref gc: pruned ${pruned} dead ref mapping(s), ${Object.keys(nextByRaw).length} live`);
|
|
51354
|
+
const highest = highestUsedIndex(session.state.messageRefs);
|
|
51355
|
+
if (highest >= REF_CAPACITY_WARN) {
|
|
51356
|
+
log2("warn", `[${session.id}] message refs at m${String(highest).padStart(5, "0")} approaching capacity 99999 \u2014 compression may stall if exhausted`);
|
|
51357
|
+
}
|
|
51358
|
+
return { pruned };
|
|
51359
|
+
}
|
|
51360
|
+
function citedRefs(messages) {
|
|
51361
|
+
const refs = /* @__PURE__ */ new Set();
|
|
51362
|
+
for (const m2 of messages) {
|
|
51363
|
+
if (!m2.text) continue;
|
|
51364
|
+
for (const match of m2.text.matchAll(CITED_REF)) refs.add(match[1]);
|
|
51365
|
+
}
|
|
51366
|
+
return refs;
|
|
51367
|
+
}
|
|
51368
|
+
|
|
51309
51369
|
// src/instance.ts
|
|
51310
51370
|
import { randomUUID as randomUUID3 } from "crypto";
|
|
51311
51371
|
import fs3 from "fs";
|
|
@@ -57074,6 +57134,21 @@ ${bodyBuffer.toString("utf8")}`);
|
|
|
57074
57134
|
try {
|
|
57075
57135
|
await withSessionLock(session, async () => {
|
|
57076
57136
|
const runPrepare = () => countTokens ? prepareCountTokens(parsed, core, reqConfig, log2, session) : protocol === "anthropic" ? prepareAnthropic(parsed, req, opts, core, reqConfig, reqPrompts, log2, session, pluginMode) : protocol === "openai" ? prepareOpenai(parsed, req, opts, core, reqConfig, reqPrompts, log2, session, pluginMode, nativeWindow) : responsesCompact ? prepareResponsesCompact(bodyBuffer, parsed, session, req, core, reqConfig, log2) : prepareResponses(parsed, req, opts, core, reqConfig, reqPrompts, log2, session, responsesIdentity, pluginMode, upstreamOrigin, nativeWindow);
|
|
57137
|
+
const isCodexCompactTrigger = protocol === "responses" && !responsesCompact && isCodexClient(req.headers) && hasCompactionTrigger(parsed.input);
|
|
57138
|
+
if (isCodexCompactTrigger) {
|
|
57139
|
+
const mode = codexCompactMode();
|
|
57140
|
+
const gatePre = codexCompactGatePre(session, reqConfig.modelContextLimit);
|
|
57141
|
+
if (mode === "intercept" && gatePre) prepared = runPrepare();
|
|
57142
|
+
if (prepared?.codexForge) {
|
|
57143
|
+
await forward(req, res, opts, prepared.body, prepared, core, reqConfig, log2, route, instanceId, affinity);
|
|
57144
|
+
rememberPluginMessages(sessionId, prepared.processedMessages, prepared.originalMessages, prepared.nudge);
|
|
57145
|
+
return;
|
|
57146
|
+
}
|
|
57147
|
+
const why = mode !== "intercept" ? "BILI_CODEX_COMPACT=pass" : !gatePre ? "gate preconditions not met" : "transform/forge failed";
|
|
57148
|
+
log2("info", `[${session.id}] codex compaction_trigger request not intercepted (${why}) \u2014 forwarding verbatim (no preflight, no rebuild, no window clamp)`);
|
|
57149
|
+
await forward(req, res, opts, bodyBuffer, null, core, reqConfig, log2, route, instanceId, affinity);
|
|
57150
|
+
return;
|
|
57151
|
+
}
|
|
57077
57152
|
prepared = runPrepare();
|
|
57078
57153
|
if (!countTokens && !responsesCompact) {
|
|
57079
57154
|
const outcome = await preflightCompressIfNeeded(
|
|
@@ -57198,6 +57273,7 @@ function prepareAnthropic(parsed, req, opts, core, config, prompts, log2, sessio
|
|
|
57198
57273
|
processedMessages = stripKernelSummaries(turn.messages, turn.state);
|
|
57199
57274
|
applyCompactionArchive(session, activeBefore, new Set(msgs.map((m2) => m2.id)), log2);
|
|
57200
57275
|
reapOrphanBlocks(session, msgs, deactivateBlock);
|
|
57276
|
+
gcMessageRefs(session, msgs, turn.messages, log2);
|
|
57201
57277
|
rebuiltMessages = coreToAnthropic(processedMessages, cacheControls);
|
|
57202
57278
|
systemOut = injectSystem(parsed, opts, prompts);
|
|
57203
57279
|
if (injectTools) {
|
|
@@ -57287,6 +57363,7 @@ function prepareOpenai(parsed, req, opts, core, config, prompts, log2, session,
|
|
|
57287
57363
|
processedMessages = stripKernelSummaries(turn.messages, turn.state);
|
|
57288
57364
|
applyCompactionArchive(session, activeBefore, new Set(msgs.map((m2) => m2.id)), log2);
|
|
57289
57365
|
reapOrphanBlocks(session, msgs, deactivateBlock);
|
|
57366
|
+
gcMessageRefs(session, msgs, turn.messages, log2);
|
|
57290
57367
|
rebuiltMessages = systemToUser(coreToOpenai(processedMessages));
|
|
57291
57368
|
const sysParts = [];
|
|
57292
57369
|
if (systemText) sysParts.push(systemText);
|
|
@@ -57379,6 +57456,7 @@ function prepareResponses(parsed, req, opts, core, config, prompts, log2, sessio
|
|
|
57379
57456
|
log2("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model, willInjectNudge));
|
|
57380
57457
|
processedMessages = stripKernelSummaries(turn.messages, turn.state);
|
|
57381
57458
|
reapOrphanBlocks(session, msgs, deactivateBlock);
|
|
57459
|
+
gcMessageRefs(session, msgs, turn.messages, log2);
|
|
57382
57460
|
rebuiltInput = patchResponsesInput(projection, processedMessages);
|
|
57383
57461
|
const forgedSummaries = echoReplaced ? [] : session.metadata.codexForgedSummaries ?? [];
|
|
57384
57462
|
if (shouldInject && !isCompactionTrigger && !process.env.ACP_NO_COMPRESS_PROMPT) {
|
|
@@ -57519,7 +57597,7 @@ function prepareResponsesCompact(body, parsed, session, req, core, config, log2)
|
|
|
57519
57597
|
compressInjected: false,
|
|
57520
57598
|
resetAfterSuccess: true
|
|
57521
57599
|
};
|
|
57522
|
-
if (codexCompactMode() !== "intercept" || !isCodexClient(req.headers) || !Array.isArray(parsed.input)) {
|
|
57600
|
+
if (codexCompactMode() !== "intercept" || !isCodexClient(req.headers) || !Array.isArray(parsed.input) || !codexCompactGatePre(session, config.modelContextLimit)) {
|
|
57523
57601
|
return base;
|
|
57524
57602
|
}
|
|
57525
57603
|
const prevState = session.state;
|