billion-context 0.1.74 → 0.1.75-pr.484.303

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
@@ -50106,6 +50106,58 @@ function reconcileNativeCompactionBoundary(session) {
50106
50106
  markDirty(session);
50107
50107
  return true;
50108
50108
  }
50109
+ function markCompactionBoundary(session) {
50110
+ session.metadata.compactionBoundary = {
50111
+ at: Date.now(),
50112
+ pending: true
50113
+ };
50114
+ markDirty(session);
50115
+ }
50116
+ function readPreCompactionArchive(session) {
50117
+ const raw = session.metadata.preCompactionArchive;
50118
+ if (raw && typeof raw === "object" && !Array.isArray(raw)) {
50119
+ return raw;
50120
+ }
50121
+ return {};
50122
+ }
50123
+ function preCompactionArchiveOf(session) {
50124
+ return readPreCompactionArchive(session);
50125
+ }
50126
+ function applyCompactionArchive(session, activeBefore, liveRawIds, log2) {
50127
+ const boundary = session.metadata.compactionBoundary;
50128
+ if (!boundary || typeof boundary !== "object" || !boundary.pending) {
50129
+ return;
50130
+ }
50131
+ const activeAfter = new Set(session.state.blocks.filter((b2) => b2.active).map((b2) => b2.blockId));
50132
+ const deactivated = [...activeBefore].filter((id) => !activeAfter.has(id));
50133
+ if (deactivated.length > 0) {
50134
+ const archive = readPreCompactionArchive(session);
50135
+ const at2 = Date.now();
50136
+ for (const id of deactivated) {
50137
+ archive[id] = { at: at2, reason: "content replaced by client native compaction summary" };
50138
+ }
50139
+ session.metadata.preCompactionArchive = archive;
50140
+ }
50141
+ const { byRaw, byRef } = session.state.messageRefs;
50142
+ const prunedByRaw = {};
50143
+ for (const [rawId, ref] of Object.entries(byRaw)) {
50144
+ if (liveRawIds.has(rawId)) prunedByRaw[rawId] = ref;
50145
+ }
50146
+ const prunedByRef = {};
50147
+ for (const [ref, rawId] of Object.entries(byRef)) {
50148
+ if (liveRawIds.has(rawId)) prunedByRef[ref] = rawId;
50149
+ }
50150
+ session.state.messageRefs.byRaw = prunedByRaw;
50151
+ session.state.messageRefs.byRef = prunedByRef;
50152
+ session.metadata.compactionBoundary = {
50153
+ ...boundary,
50154
+ pending: false,
50155
+ archivedAt: Date.now(),
50156
+ archivedBlocks: deactivated
50157
+ };
50158
+ markDirty(session);
50159
+ log2("info", `[${session.id}] native compaction boundary: archived ${deactivated.length} pre-compaction block(s)${deactivated.length > 0 ? ` (${deactivated.join(", ")})` : ""}; pruned ref maps to ${prunedByRaw.length} live raw id(s)`);
50160
+ }
50109
50161
  function evictOldest() {
50110
50162
  let oldestId;
50111
50163
  let oldestSeen = Infinity;
@@ -50182,6 +50234,10 @@ function resolveDecompress(args, ctx) {
50182
50234
  const blockId = rawBlockId.trim();
50183
50235
  const block = ctx.core.decompress(blockId, ctx.session.state);
50184
50236
  if (!block) return `[Block ${blockId} not found]`;
50237
+ const archived = preCompactionArchiveOf(ctx.session);
50238
+ if (archived[blockId] !== void 0) {
50239
+ return `[decompress FAILED: block ${blockId} is a pre-compaction archive \u2014 its content was in the history BEFORE the client's native compaction and is no longer reachable (replaced by the client's compaction summary). decompress is unavailable for archived blocks.]`;
50240
+ }
50185
50241
  const full = args.full === true;
50186
50242
  const cached = ctx.session.blockContents.get(blockId);
50187
50243
  let body;
@@ -51427,6 +51483,8 @@ function handleAcpStatus(args, ctx) {
51427
51483
  const nudge = ctx.nudge;
51428
51484
  const ranges = nudge?.compressibleRanges ?? [];
51429
51485
  const protectedRanges = nudge?.protectedRanges ?? [];
51486
+ const archive = preCompactionArchiveOf(ctx.session);
51487
+ const archivedIds = Object.keys(archive);
51430
51488
  const extra = [];
51431
51489
  if (nudge) {
51432
51490
  extra.push("");
@@ -51436,6 +51494,13 @@ function handleAcpStatus(args, ctx) {
51436
51494
  extra.push("");
51437
51495
  extra.push(formatRanges(ranges, protectedRanges));
51438
51496
  }
51497
+ if (archivedIds.length > 0) {
51498
+ extra.push("");
51499
+ extra.push(`PRE-COMPACTION ARCHIVE \u2014 ${archivedIds.length} block(s): content was replaced by the client's native compaction summary, so it is no longer in the session history and decompress is unavailable.`);
51500
+ for (const id of archivedIds) {
51501
+ extra.push(` ${id} \u2014 ${archive[id].reason}`);
51502
+ }
51503
+ }
51439
51504
  return extra.length > 0 ? `${base}
51440
51505
  ${extra.join("\n")}` : base;
51441
51506
  }
@@ -54239,6 +54304,32 @@ function handlePluginRegister(payload, res) {
54239
54304
  queuePluginRegister(conversationId2, agent, parsed.identity === true);
54240
54305
  res.end(JSON.stringify({ ok: true, conversationId: conversationId2, agent }));
54241
54306
  }
54307
+ function handlePluginCompact(payload, res) {
54308
+ let parsed;
54309
+ try {
54310
+ parsed = JSON.parse(payload);
54311
+ } catch {
54312
+ res.writeHead(400, { "content-type": "application/json" });
54313
+ res.end(JSON.stringify({ ok: false, error: "invalid JSON body" }));
54314
+ return;
54315
+ }
54316
+ const conversationId2 = typeof parsed.conversationId === "string" ? parsed.conversationId.trim() : "";
54317
+ if (!conversationId2) {
54318
+ res.writeHead(400, { "content-type": "application/json" });
54319
+ res.end(JSON.stringify({ ok: false, error: "conversationId is required" }));
54320
+ return;
54321
+ }
54322
+ const entry = conversations.get(conversationId2);
54323
+ const session = entry ? peekSession(entry.sessionId) : void 0;
54324
+ if (!entry || !session) {
54325
+ res.writeHead(404, { "content-type": "application/json" });
54326
+ res.end(JSON.stringify({ ok: false, error: "unknown plugin conversation (no model request has arrived with this conversation id yet)" }));
54327
+ return;
54328
+ }
54329
+ markCompactionBoundary(session);
54330
+ entry.lastSeen = Date.now();
54331
+ res.end(JSON.stringify({ ok: true, conversationId: conversationId2 }));
54332
+ }
54242
54333
  function handlePluginManifest(res) {
54243
54334
  res.writeHead(200, { "content-type": "application/json" });
54244
54335
  res.end(JSON.stringify({
@@ -56665,6 +56756,17 @@ async function handle(req, res, opts, core, config, log2, instanceId, instanceSt
56665
56756
  return;
56666
56757
  }
56667
56758
  }
56759
+ if (req.method === "POST" && req.url === "/__bili/plugin/compact") {
56760
+ try {
56761
+ const body = await readBody(req);
56762
+ handlePluginCompact(body.toString("utf8"), res);
56763
+ return;
56764
+ } catch (err2) {
56765
+ res.writeHead(err2 instanceof BodyTooLargeError ? 413 : 400, { "content-type": "application/json" });
56766
+ res.end(JSON.stringify({ ok: false, error: String(err2) }));
56767
+ return;
56768
+ }
56769
+ }
56668
56770
  if (isAdminPath) {
56669
56771
  res.writeHead(404, { "content-type": "application/json" });
56670
56772
  res.end(JSON.stringify({ error: { type: "not_found", message: "no such management endpoint" } }));
@@ -56894,6 +56996,7 @@ ${bodyBuffer.toString("utf8")}`);
56894
56996
  nativeFromFallback = false;
56895
56997
  log2("info", `[${session.id}] self-healed context window: ${resolved} \u2192 ${learnedLimit} (learned from an upstream overflow)`);
56896
56998
  }
56999
+ const nativeWindow = reqConfig.modelContextLimit;
56897
57000
  if (shouldReserveOutputHeadroom(protocol)) {
56898
57001
  const p2 = parsed;
56899
57002
  const rawMax = p2.max_tokens ?? p2.max_completion_tokens ?? p2.max_output_tokens;
@@ -56908,7 +57011,7 @@ ${bodyBuffer.toString("utf8")}`);
56908
57011
  acquireInFlight(session);
56909
57012
  try {
56910
57013
  await withSessionLock(session, async () => {
56911
- 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) : responsesCompact ? prepareResponsesCompact(bodyBuffer, parsed, session, req, core, reqConfig, log2) : prepareResponses(parsed, req, opts, core, reqConfig, reqPrompts, log2, session, responsesIdentity, pluginMode, upstreamOrigin);
57014
+ 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);
56912
57015
  prepared = runPrepare();
56913
57016
  if (!countTokens && !responsesCompact) {
56914
57017
  const outcome = await preflightCompressIfNeeded(
@@ -56992,7 +57095,7 @@ function diagNudge(turn, sessionId, tokenCount, limit, model, willInject) {
56992
57095
  const interval = b2["nudgeGrowthTokens"] ?? 0;
56993
57096
  const pendingT1 = b2["pendingT1"] ?? 0;
56994
57097
  const ref = b2["growthReference"] ?? 0;
56995
- const inject = !n.shouldInject ? "idle" : willInject ? `INJECT T${n.tier ?? "?"}` : `ARMED-SUPPRESSED T${n.tier ?? "?"}`;
57098
+ const inject = willInject ? n.shouldInject ? `INJECT T${n.tier ?? "?"}` : `INJECT-ESC T${n.tier ?? "?"}` : n.shouldInject ? `ARMED-SUPPRESSED T${n.tier ?? "?"}` : "idle";
56996
57099
  const modelTag = model ? ` model=${model}` : "";
56997
57100
  return `[${sessionId}] nudge ${inject}: usage=${pct2} (${tokenCount}/${limit}), growth=${growth}/${floor} (ref=${ref}, interval=${interval}), pendingT1=${pendingT1}/${interval}${modelTag}, reason="${n.reason.slice(0, 120)}"`;
56998
57101
  }
@@ -57016,6 +57119,7 @@ function prepareAnthropic(parsed, req, opts, core, config, prompts, log2, sessio
57016
57119
  originalMessages = msgs;
57017
57120
  extractSystem(parsed.system);
57018
57121
  const tokenCount = session.stats.lastInputTokens;
57122
+ const activeBefore = new Set(session.state.blocks.filter((b2) => b2.active).map((b2) => b2.blockId));
57019
57123
  const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount, renderTags: "text-only" });
57020
57124
  session.state = turn.state;
57021
57125
  session.stats.compressCreditTokens = 0;
@@ -57027,9 +57131,10 @@ function prepareAnthropic(parsed, req, opts, core, config, prompts, log2, sessio
57027
57131
  if (t) session.meta.title = t;
57028
57132
  }
57029
57133
  log2("info", diagTagSummary(turn.messages, sessionId, "text-only"));
57030
- const willInjectNudge = opts.compress.injectNudge && !!turn.nudge?.shouldInject;
57134
+ const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && (turn.nudge.shouldInject || emergencyNudge(turn.nudge));
57031
57135
  log2("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model, willInjectNudge));
57032
57136
  processedMessages = stripKernelSummaries(turn.messages, turn.state);
57137
+ applyCompactionArchive(session, activeBefore, new Set(msgs.map((m2) => m2.id)), log2);
57033
57138
  reapOrphanBlocks(session, msgs, deactivateBlock);
57034
57139
  rebuiltMessages = coreToAnthropic(processedMessages, cacheControls);
57035
57140
  systemOut = injectSystem(parsed, opts, prompts);
@@ -57055,7 +57160,36 @@ function prepareAnthropic(parsed, req, opts, core, config, prompts, log2, sessio
57055
57160
  delete rebuilt.prompt_cache_key;
57056
57161
  return { body: JSON.stringify(rebuilt), session, processedMessages, originalMessages, anthropicSystem: parsed.system, protocol: "anthropic", stream: stream2, compressInjected: injectTools, pluginMode, nudge, prompts, renderTags: "text-only" };
57057
57162
  }
57058
- function prepareOpenai(parsed, req, opts, core, config, prompts, log2, session, pluginMode) {
57163
+ var OUTPUT_CLAMP_MARGIN_PCT = 0.05;
57164
+ var OUTPUT_CLAMP_MIN_MARGIN = 2048;
57165
+ var OUTPUT_CLAMP_FLOOR = 1024;
57166
+ var EMERGENCY_NUDGE_ESCALATION_PCT = 0.7;
57167
+ function estimateInputTokens(processedMessages, systemText, tools, lastInputTokens) {
57168
+ const est = estimateCoreMessages(processedMessages) + defaultCountTokens(systemText ?? "") + defaultCountTokens(JSON.stringify(tools ?? []));
57169
+ return Math.max(lastInputTokens > 0 ? lastInputTokens : 0, est);
57170
+ }
57171
+ function clampOutputBudget(requested, inputEstimate, nativeWindow) {
57172
+ const margin = Math.max(OUTPUT_CLAMP_MIN_MARGIN, Math.ceil(inputEstimate * OUTPUT_CLAMP_MARGIN_PCT));
57173
+ const cap = nativeWindow - inputEstimate - margin;
57174
+ if (cap < OUTPUT_CLAMP_FLOOR || cap >= requested) return void 0;
57175
+ return cap;
57176
+ }
57177
+ function emergencyNudge(nudge, escalationPct = EMERGENCY_NUDGE_ESCALATION_PCT) {
57178
+ if (!nudge || nudge.shouldInject) return false;
57179
+ if (nudge.compressibleRanges.length === 0) return false;
57180
+ return nudge.contextUsage >= escalationPct;
57181
+ }
57182
+ function clampOutgoingOutput(rebuilt, field, ctx, sessionId, log2) {
57183
+ const raw = rebuilt[field];
57184
+ if (typeof raw !== "number") return;
57185
+ const inputEstimate = estimateInputTokens(ctx.processedMessages, ctx.systemText, ctx.tools, ctx.lastInputTokens);
57186
+ const capped = clampOutputBudget(raw, inputEstimate, ctx.nativeWindow);
57187
+ if (capped !== void 0) {
57188
+ rebuilt[field] = capped;
57189
+ log2("info", `[${sessionId}] output budget clamped ${raw} -> ${capped} (input~${inputEstimate}, window=${ctx.nativeWindow}); prevents input+output overflow (#453)`);
57190
+ }
57191
+ }
57192
+ function prepareOpenai(parsed, req, opts, core, config, prompts, log2, session, pluginMode, nativeWindow) {
57059
57193
  const sessionId = session.id;
57060
57194
  const stream2 = parsed.stream === true;
57061
57195
  ++session.stats.requests;
@@ -57074,6 +57208,7 @@ function prepareOpenai(parsed, req, opts, core, config, prompts, log2, session,
57074
57208
  openaiSystemText = systemText;
57075
57209
  originalMessages = msgs;
57076
57210
  const tokenCount = session.stats.lastInputTokens;
57211
+ const activeBefore = new Set(session.state.blocks.filter((b2) => b2.active).map((b2) => b2.blockId));
57077
57212
  const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount, renderTags: "text-only" });
57078
57213
  session.state = turn.state;
57079
57214
  session.stats.compressCreditTokens = 0;
@@ -57085,9 +57220,10 @@ function prepareOpenai(parsed, req, opts, core, config, prompts, log2, session,
57085
57220
  if (t) session.meta.title = t;
57086
57221
  }
57087
57222
  log2("info", diagTagSummary(turn.messages, sessionId, "text-only"));
57088
- const willInjectNudge = opts.compress.injectNudge && !!turn.nudge?.shouldInject && shouldInject;
57223
+ const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && shouldInject && (turn.nudge.shouldInject || emergencyNudge(turn.nudge));
57089
57224
  log2("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model, willInjectNudge));
57090
57225
  processedMessages = stripKernelSummaries(turn.messages, turn.state);
57226
+ applyCompactionArchive(session, activeBefore, new Set(msgs.map((m2) => m2.id)), log2);
57091
57227
  reapOrphanBlocks(session, msgs, deactivateBlock);
57092
57228
  rebuiltMessages = systemToUser(coreToOpenai(processedMessages));
57093
57229
  const sysParts = [];
@@ -57111,6 +57247,7 @@ function prepareOpenai(parsed, req, opts, core, config, prompts, log2, session,
57111
57247
  processedMessages = [];
57112
57248
  }
57113
57249
  const rebuilt = { ...parsed, messages: rebuiltMessages, tools: toolsOut };
57250
+ clampOutgoingOutput(rebuilt, typeof parsed.max_completion_tokens === "number" ? "max_completion_tokens" : "max_tokens", { systemText: openaiSystemText, tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, nativeWindow }, sessionId, log2);
57114
57251
  delete rebuilt.prompt_cache_retention;
57115
57252
  if (stream2 && rebuilt.stream_options === void 0) {
57116
57253
  rebuilt.stream_options = { include_usage: true };
@@ -57119,7 +57256,7 @@ function prepareOpenai(parsed, req, opts, core, config, prompts, log2, session,
57119
57256
  markDirty(session);
57120
57257
  return { body: JSON.stringify(rebuilt), session, processedMessages, originalMessages, protocol: "openai", stream: stream2, compressInjected: injectTools, pluginMode, nudge, prompts, openaiSystemText, renderTags: "text-only" };
57121
57258
  }
57122
- function prepareResponses(parsed, req, opts, core, config, prompts, log2, session, identity, pluginMode, upstreamOrigin) {
57259
+ function prepareResponses(parsed, req, opts, core, config, prompts, log2, session, identity, pluginMode, upstreamOrigin, nativeWindow) {
57123
57260
  const sessionId = session.id;
57124
57261
  const stream2 = parsed.stream === true;
57125
57262
  ++session.stats.requests;
@@ -57176,7 +57313,7 @@ function prepareResponses(parsed, req, opts, core, config, prompts, log2, sessio
57176
57313
  if (t) session.meta.title = t;
57177
57314
  }
57178
57315
  log2("info", diagTagSummary(turn.messages, sessionId, "text-only"));
57179
- const willInjectNudge = opts.compress.injectNudge && !!turn.nudge?.shouldInject && shouldInject && !isCompactionTrigger;
57316
+ const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && shouldInject && !isCompactionTrigger && (turn.nudge.shouldInject || emergencyNudge(turn.nudge));
57180
57317
  log2("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model, willInjectNudge));
57181
57318
  processedMessages = stripKernelSummaries(turn.messages, turn.state);
57182
57319
  reapOrphanBlocks(session, msgs, deactivateBlock);
@@ -57227,6 +57364,9 @@ function prepareResponses(parsed, req, opts, core, config, prompts, log2, sessio
57227
57364
  log2("info", `[${sessionId}] codex compact intercepted (trigger); forged SSE with ${summaries.length} block summary(s), upstream not contacted`);
57228
57365
  }
57229
57366
  const rebuilt = { ...parsed, input: rebuiltInput, tools: toolsOut };
57367
+ if (!isCompactionTrigger) {
57368
+ clampOutgoingOutput(rebuilt, "max_output_tokens", { systemText: (responsesProjection?.systemParts ?? []).join("\n"), tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, nativeWindow }, sessionId, log2);
57369
+ }
57230
57370
  const promptCacheKey = resolvePromptCacheKey(
57231
57371
  rebuilt.prompt_cache_key,
57232
57372
  identity,
@@ -62333,19 +62473,20 @@ function livePidHoldsOverlay(overlay) {
62333
62473
  function linkOverlayEntry(realHome, overlay, entry) {
62334
62474
  const target = path14.join(realHome, entry);
62335
62475
  const link = path14.join(overlay, entry);
62336
- try {
62337
- fs11.symlinkSync(target, link);
62338
- return true;
62339
- } catch (err2) {
62340
- const code = err2.code;
62341
- if (code !== "EPERM" && code !== "EACCES" && code !== "EINVAL") return false;
62342
- }
62343
62476
  let st2;
62344
62477
  try {
62345
62478
  st2 = fs11.lstatSync(target);
62346
62479
  } catch {
62347
62480
  return false;
62348
62481
  }
62482
+ const kind = st2.isDirectory() ? process.platform === "win32" ? "junction" : "dir" : "file";
62483
+ try {
62484
+ fs11.symlinkSync(target, link, kind);
62485
+ return true;
62486
+ } catch (err2) {
62487
+ const code = err2.code;
62488
+ if (code !== "EPERM" && code !== "EACCES" && code !== "EINVAL") return false;
62489
+ }
62349
62490
  if (st2.isDirectory()) {
62350
62491
  try {
62351
62492
  fs11.symlinkSync(target, link, "junction");
@@ -62376,17 +62517,39 @@ function isWriteThroughHardlink(overlayPath, realPath, st2) {
62376
62517
  }
62377
62518
  }
62378
62519
  function sqliteSetMembers(base) {
62379
- return [base, `${base}-wal`, `${base}-shm`];
62520
+ return [base, `${base}-wal`, `${base}-shm`, `${base}-journal`];
62521
+ }
62522
+ function freeConflictName(dst) {
62523
+ let candidate = `${dst}.bili-conflict`;
62524
+ let n = 1;
62525
+ while (n < 1e5) {
62526
+ try {
62527
+ fs11.lstatSync(candidate);
62528
+ candidate = `${dst}.bili-conflict.${n}`;
62529
+ n += 1;
62530
+ } catch {
62531
+ return candidate;
62532
+ }
62533
+ }
62534
+ return candidate;
62380
62535
  }
62381
62536
  function mergeSqliteSet(overlay, realHome, base) {
62382
- const members = sqliteSetMembers(base).filter((m2) => {
62537
+ const members = sqliteSetMembers(base);
62538
+ const statFile = (dir, m2) => {
62383
62539
  try {
62384
- return fs11.lstatSync(path14.join(overlay, m2)).isFile();
62540
+ const st2 = fs11.lstatSync(path14.join(dir, m2));
62541
+ return st2.isFile() ? st2 : void 0;
62385
62542
  } catch {
62386
- return false;
62543
+ return void 0;
62387
62544
  }
62388
- });
62389
- if (members.length === 0) return true;
62545
+ };
62546
+ const oMain = statFile(overlay, base);
62547
+ const rMain = statFile(realHome, base);
62548
+ let winner;
62549
+ if (oMain && rMain) winner = rMain.mtimeMs >= oMain.mtimeMs ? "real" : "overlay";
62550
+ else if (oMain) winner = "overlay";
62551
+ else if (rMain) winner = "real";
62552
+ else winner = "orphan";
62390
62553
  const undo = [];
62391
62554
  const rollback = () => {
62392
62555
  for (const step of undo.reverse()) {
@@ -62397,46 +62560,39 @@ function mergeSqliteSet(overlay, realHome, base) {
62397
62560
  }
62398
62561
  undo.length = 0;
62399
62562
  };
62563
+ const movePreserving = (src, dst) => {
62564
+ let dstStat;
62565
+ try {
62566
+ dstStat = fs11.lstatSync(dst);
62567
+ } catch {
62568
+ }
62569
+ if (dstStat) {
62570
+ if (dstStat.isDirectory()) throw new Error("target is a directory");
62571
+ const conflict = freeConflictName(dst);
62572
+ fs11.renameSync(dst, conflict);
62573
+ undo.push(() => fs11.renameSync(conflict, dst));
62574
+ }
62575
+ fs11.renameSync(src, dst);
62576
+ undo.push(() => fs11.renameSync(dst, src));
62577
+ };
62578
+ const preserveAsConflict = (src, name) => {
62579
+ const conflict = freeConflictName(path14.join(realHome, name));
62580
+ fs11.renameSync(src, conflict);
62581
+ undo.push(() => fs11.renameSync(conflict, src));
62582
+ };
62400
62583
  try {
62401
62584
  for (const m2 of members) {
62402
- const src = path14.join(overlay, m2);
62403
- const dst = path14.join(realHome, m2);
62404
- let dstStat;
62405
- try {
62406
- dstStat = fs11.lstatSync(dst);
62407
- } catch {
62408
- }
62409
- if (dstStat && dstStat.isDirectory()) {
62410
- rollback();
62411
- return false;
62412
- }
62413
- const srcStat = fs11.lstatSync(src);
62414
- if (dstStat && dstStat.mtimeMs >= srcStat.mtimeMs) {
62415
- try {
62416
- fs11.renameSync(src, `${dst}.bili-conflict`);
62417
- } catch {
62418
- rollback();
62419
- return false;
62420
- }
62421
- undo.push(() => fs11.renameSync(`${dst}.bili-conflict`, src));
62422
- continue;
62423
- }
62424
- if (dstStat) {
62425
- try {
62426
- fs11.renameSync(dst, `${dst}.bili-conflict`);
62427
- } catch {
62428
- rollback();
62429
- return false;
62430
- }
62431
- undo.push(() => fs11.renameSync(`${dst}.bili-conflict`, dst));
62432
- }
62433
- try {
62434
- fs11.renameSync(src, dst);
62435
- } catch {
62436
- rollback();
62437
- return false;
62585
+ const o = statFile(overlay, m2);
62586
+ const r = statFile(realHome, m2);
62587
+ if (winner === "overlay") {
62588
+ if (o) movePreserving(path14.join(overlay, m2), path14.join(realHome, m2));
62589
+ else if (r) preserveAsConflict(path14.join(realHome, m2), m2);
62590
+ } else if (winner === "real") {
62591
+ if (o) preserveAsConflict(path14.join(overlay, m2), m2);
62592
+ } else {
62593
+ if (o) preserveAsConflict(path14.join(overlay, m2), m2);
62594
+ else if (r) preserveAsConflict(path14.join(realHome, m2), m2);
62438
62595
  }
62439
- undo.push(() => fs11.renameSync(dst, src));
62440
62596
  }
62441
62597
  return true;
62442
62598
  } catch {
@@ -62445,6 +62601,8 @@ function mergeSqliteSet(overlay, realHome, base) {
62445
62601
  }
62446
62602
  }
62447
62603
  function refreshOverlayHome(realHome, overlay, generatedFile) {
62604
+ const generatedFiles = new Set(Array.isArray(generatedFile) ? generatedFile : [generatedFile]);
62605
+ const isGeneratedDraft = (name) => [...generatedFiles].some((g2) => name.startsWith(`.${g2}.`) && name.endsWith(".tmp"));
62448
62606
  try {
62449
62607
  fs11.mkdirSync(overlay, { recursive: true });
62450
62608
  } catch {
@@ -62475,7 +62633,7 @@ function refreshOverlayHome(realHome, overlay, generatedFile) {
62475
62633
  }
62476
62634
  const dbSets = [];
62477
62635
  for (const entry of overlayEntries) {
62478
- if (!entry.endsWith(".db") || entry === generatedFile) continue;
62636
+ if (!entry.endsWith(".db") || generatedFiles.has(entry)) continue;
62479
62637
  const members = sqliteSetMembers(entry);
62480
62638
  if (!members.some((m2) => m2 !== entry && overlayEntries.includes(m2))) continue;
62481
62639
  let mainSt;
@@ -62493,9 +62651,9 @@ function refreshOverlayHome(realHome, overlay, generatedFile) {
62493
62651
  }
62494
62652
  }
62495
62653
  for (const entry of overlayEntries) {
62496
- if (entry === generatedFile) continue;
62654
+ if (generatedFiles.has(entry)) continue;
62497
62655
  const overlayPath = path14.join(overlay, entry);
62498
- if (entry.startsWith(`.${generatedFile}.`) && entry.endsWith(".tmp")) {
62656
+ if (isGeneratedDraft(entry)) {
62499
62657
  try {
62500
62658
  fs11.unlinkSync(overlayPath);
62501
62659
  } catch {
@@ -62529,7 +62687,7 @@ function refreshOverlayHome(realHome, overlay, generatedFile) {
62529
62687
  fs11.unlinkSync(overlayPath);
62530
62688
  } catch {
62531
62689
  }
62532
- } else if (mergeOverlayEntry(overlayPath, realPath, generatedFile)) {
62690
+ } else if (mergeOverlayEntry(overlayPath, realPath, generatedFiles)) {
62533
62691
  try {
62534
62692
  fs11.rmSync(overlayPath, { recursive: true, force: true });
62535
62693
  } catch {
@@ -62551,7 +62709,7 @@ function refreshOverlayHome(realHome, overlay, generatedFile) {
62551
62709
  let total = 0;
62552
62710
  const linkFailures = [];
62553
62711
  for (const entry of realEntries) {
62554
- if (entry === generatedFile) continue;
62712
+ if (generatedFiles.has(entry)) continue;
62555
62713
  total += 1;
62556
62714
  const overlayPath = path14.join(overlay, entry);
62557
62715
  let present = false;
@@ -62585,7 +62743,7 @@ function refreshOverlayHome(realHome, overlay, generatedFile) {
62585
62743
  }
62586
62744
  return true;
62587
62745
  }
62588
- function mergeOverlayEntry(src, dst, excludedName) {
62746
+ function mergeOverlayEntry(src, dst, excludedNames) {
62589
62747
  let st2;
62590
62748
  try {
62591
62749
  st2 = fs11.lstatSync(src);
@@ -62600,7 +62758,7 @@ function mergeOverlayEntry(src, dst, excludedName) {
62600
62758
  if (st2.isDirectory()) {
62601
62759
  if (dstStat && !dstStat.isDirectory()) {
62602
62760
  try {
62603
- fs11.renameSync(dst, `${dst}.bili-conflict`);
62761
+ fs11.renameSync(dst, freeConflictName(dst));
62604
62762
  dstStat = void 0;
62605
62763
  } catch {
62606
62764
  return false;
@@ -62619,14 +62777,14 @@ function mergeOverlayEntry(src, dst, excludedName) {
62619
62777
  }
62620
62778
  let ok = true;
62621
62779
  for (const entry of entries) {
62622
- if (excludedName !== void 0 && entry === excludedName) continue;
62623
- if (!mergeOverlayEntry(path14.join(src, entry), path14.join(dst, entry), excludedName)) ok = false;
62780
+ if (excludedNames?.has(entry)) continue;
62781
+ if (!mergeOverlayEntry(path14.join(src, entry), path14.join(dst, entry), excludedNames)) ok = false;
62624
62782
  }
62625
62783
  return ok;
62626
62784
  }
62627
62785
  if (dstStat && dstStat.isDirectory()) {
62628
62786
  try {
62629
- fs11.renameSync(src, `${dst}.bili-conflict`);
62787
+ fs11.renameSync(src, freeConflictName(dst));
62630
62788
  return true;
62631
62789
  } catch {
62632
62790
  return false;
@@ -62649,7 +62807,7 @@ function mergeOverlayEntry(src, dst, excludedName) {
62649
62807
  }
62650
62808
  if (dstStat && dstStat.mtimeMs >= st2.mtimeMs) {
62651
62809
  try {
62652
- fs11.renameSync(src, `${dst}.bili-conflict`);
62810
+ fs11.renameSync(src, freeConflictName(dst));
62653
62811
  return true;
62654
62812
  } catch {
62655
62813
  return false;
@@ -62657,7 +62815,7 @@ function mergeOverlayEntry(src, dst, excludedName) {
62657
62815
  }
62658
62816
  if (dstStat) {
62659
62817
  try {
62660
- fs11.renameSync(dst, `${dst}.bili-conflict`);
62818
+ fs11.renameSync(dst, freeConflictName(dst));
62661
62819
  } catch {
62662
62820
  return false;
62663
62821
  }
@@ -62692,45 +62850,58 @@ function writeOverlayFileAtomic(overlay, fileName, contents) {
62692
62850
  }
62693
62851
  }
62694
62852
  }
62695
- function preparePiHttpRewrite(piHome, origin, httpRewrites, httpsRewrites) {
62696
- if (httpRewrites.length === 0 && httpsRewrites.length === 0) return void 0;
62697
- const modelsPath = path14.join(piHome, "models.json");
62698
- let txt;
62853
+ function writePiCompactionDisabledSettings(piHome, overlay) {
62854
+ let settings = {};
62699
62855
  try {
62700
- txt = fs11.readFileSync(modelsPath, "utf8");
62856
+ const parsed = JSON.parse(fs11.readFileSync(path14.join(piHome, "settings.json"), "utf8"));
62857
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
62858
+ settings = parsed;
62859
+ }
62701
62860
  } catch {
62702
- return void 0;
62703
62861
  }
62704
- let parsed;
62862
+ const compactionRaw = settings.compaction;
62863
+ const compaction = compactionRaw && typeof compactionRaw === "object" && !Array.isArray(compactionRaw) ? { ...compactionRaw } : {};
62864
+ compaction.enabled = false;
62865
+ settings.compaction = compaction;
62866
+ writeOverlayFileAtomic(overlay, "settings.json", JSON.stringify(settings, null, 2));
62867
+ }
62868
+ function preparePiHttpRewrite(piHome, origin, httpRewrites, httpsRewrites) {
62869
+ if (!fs11.existsSync(piHome)) return void 0;
62870
+ const overlay = `${piHome}-bili`;
62871
+ const generated = ["settings.json"];
62872
+ let modelsRoot;
62705
62873
  try {
62706
- parsed = JSON.parse(txt);
62874
+ const parsed = JSON.parse(fs11.readFileSync(path14.join(piHome, "models.json"), "utf8"));
62875
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
62876
+ modelsRoot = parsed;
62877
+ generated.push("models.json");
62878
+ }
62707
62879
  } catch {
62708
- return void 0;
62709
62880
  }
62710
- if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return void 0;
62711
- const root = parsed;
62712
- const providersVal = root.providers;
62713
- if (providersVal && typeof providersVal === "object" && !Array.isArray(providersVal)) {
62714
- const providers = providersVal;
62715
- for (const r of httpRewrites) {
62716
- const prov = providers[r.key];
62717
- if (prov && typeof prov === "object" && !Array.isArray(prov)) {
62718
- const p2 = prov;
62719
- const existing = typeof p2.baseUrl === "string" ? p2.baseUrl : r.realUpstream;
62720
- p2.baseUrl = wrapUpstream(origin, unwrapUpstream2(existing));
62881
+ if (!refreshOverlayHome(piHome, overlay, generated)) return void 0;
62882
+ if (modelsRoot) {
62883
+ const providersVal = modelsRoot.providers;
62884
+ if (providersVal && typeof providersVal === "object" && !Array.isArray(providersVal)) {
62885
+ const providers = providersVal;
62886
+ for (const r of httpRewrites) {
62887
+ const prov = providers[r.key];
62888
+ if (prov && typeof prov === "object" && !Array.isArray(prov)) {
62889
+ const p2 = prov;
62890
+ const existing = typeof p2.baseUrl === "string" ? p2.baseUrl : r.realUpstream;
62891
+ p2.baseUrl = wrapUpstream(origin, unwrapUpstream2(existing));
62892
+ }
62721
62893
  }
62722
- }
62723
- for (const r of httpsRewrites) {
62724
- const prov = providers[r.key];
62725
- if (prov && typeof prov === "object" && !Array.isArray(prov)) {
62726
- const p2 = prov;
62727
- p2.baseUrl = r.realUpstream;
62894
+ for (const r of httpsRewrites) {
62895
+ const prov = providers[r.key];
62896
+ if (prov && typeof prov === "object" && !Array.isArray(prov)) {
62897
+ const p2 = prov;
62898
+ p2.baseUrl = r.realUpstream;
62899
+ }
62728
62900
  }
62729
62901
  }
62902
+ writeOverlayFileAtomic(overlay, "models.json", JSON.stringify(modelsRoot));
62730
62903
  }
62731
- const overlay = `${piHome}-bili`;
62732
- if (!refreshOverlayHome(piHome, overlay, "models.json")) return void 0;
62733
- writeOverlayFileAtomic(overlay, "models.json", JSON.stringify(root));
62904
+ writePiCompactionDisabledSettings(piHome, overlay);
62734
62905
  return overlay;
62735
62906
  }
62736
62907
  function atomicWriteTextFile(filePath, contents) {
@@ -62781,55 +62952,145 @@ function unpackDeadProxyUrlsInFile(filePath, livePorts) {
62781
62952
  }
62782
62953
  return changed;
62783
62954
  }
62955
+ function mergeOmpCompactionDisabledYaml(text) {
62956
+ const lines = text.split(/\r?\n/);
62957
+ let compactionLine = -1;
62958
+ for (let i = 0; i < lines.length; i++) {
62959
+ const rawLine = lines[i];
62960
+ const trimmed = rawLine.trim();
62961
+ if (!trimmed || trimmed.startsWith("#")) continue;
62962
+ const indent = rawLine.length - rawLine.trimStart().length;
62963
+ if (indent === 0 && /^compaction:\s*(#.*)?$/.test(trimmed)) {
62964
+ compactionLine = i;
62965
+ break;
62966
+ }
62967
+ }
62968
+ if (compactionLine === -1) {
62969
+ const base = text === "" || text.endsWith("\n") ? text : `${text}
62970
+ `;
62971
+ return `${base}compaction:
62972
+ enabled: false
62973
+ `;
62974
+ }
62975
+ for (let i = compactionLine + 1; i < lines.length; i++) {
62976
+ const rawLine = lines[i];
62977
+ const trimmed = rawLine.trim();
62978
+ if (!trimmed || trimmed.startsWith("#")) continue;
62979
+ const indent = rawLine.length - rawLine.trimStart().length;
62980
+ if (indent === 0) break;
62981
+ if (/^enabled:/.test(trimmed)) {
62982
+ const leading = rawLine.slice(0, rawLine.length - rawLine.trimStart().length);
62983
+ const commentMatch = /\s+#.*$/.exec(rawLine);
62984
+ const comment = commentMatch ? commentMatch[0] : "";
62985
+ lines[i] = `${leading}enabled: false${comment}`;
62986
+ return lines.join("\n");
62987
+ }
62988
+ }
62989
+ let childIndent = 2;
62990
+ for (let i = compactionLine + 1; i < lines.length; i++) {
62991
+ const rawLine = lines[i];
62992
+ const trimmed = rawLine.trim();
62993
+ if (!trimmed || trimmed.startsWith("#")) continue;
62994
+ const indent = rawLine.length - rawLine.trimStart().length;
62995
+ if (indent === 0) break;
62996
+ childIndent = indent;
62997
+ break;
62998
+ }
62999
+ lines.splice(compactionLine + 1, 0, `${" ".repeat(childIndent)}enabled: false`);
63000
+ return lines.join("\n");
63001
+ }
63002
+ function writeOmpCompactionDisabledConfig(ompHome, overlay) {
63003
+ let base;
63004
+ let baseIsJson = false;
63005
+ for (const name of ["config.yml", "config.yaml"]) {
63006
+ try {
63007
+ base = fs11.readFileSync(path14.join(ompHome, name), "utf8");
63008
+ break;
63009
+ } catch {
63010
+ }
63011
+ }
63012
+ if (base === void 0) {
63013
+ try {
63014
+ base = fs11.readFileSync(path14.join(ompHome, "settings.json"), "utf8");
63015
+ baseIsJson = true;
63016
+ } catch {
63017
+ }
63018
+ }
63019
+ let merged;
63020
+ if (base === void 0) {
63021
+ merged = "compaction:\n enabled: false\n";
63022
+ } else if (baseIsJson) {
63023
+ let settings = {};
63024
+ try {
63025
+ const parsed = JSON.parse(base);
63026
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
63027
+ settings = parsed;
63028
+ }
63029
+ } catch {
63030
+ }
63031
+ const compactionRaw = settings.compaction;
63032
+ const compaction = compactionRaw && typeof compactionRaw === "object" && !Array.isArray(compactionRaw) ? { ...compactionRaw } : {};
63033
+ compaction.enabled = false;
63034
+ settings.compaction = compaction;
63035
+ merged = JSON.stringify(settings, null, 2);
63036
+ } else {
63037
+ merged = mergeOmpCompactionDisabledYaml(base);
63038
+ }
63039
+ writeOverlayFileAtomic(overlay, "config.yml", merged);
63040
+ }
62784
63041
  function prepareOmpHttpRewrite(ompHome, origin, httpRewrites, httpsRewrites) {
63042
+ if (!fs11.existsSync(ompHome)) return void 0;
63043
+ const overlay = `${ompHome}-bili`;
63044
+ const generated = ["config.yml"];
62785
63045
  const modelsPath = path14.join(ompHome, "models.yml");
62786
63046
  const unpacked = unpackDeadProxyUrlsInFile(modelsPath, liveProxyPorts());
62787
63047
  if (unpacked > 0) {
62788
63048
  console.error(`bili: unpacked ${unpacked} dead proxy URL(s) from the real ${modelsPath}`);
62789
63049
  }
62790
- if (httpRewrites.length === 0 && httpsRewrites.length === 0) return void 0;
62791
- let txt;
63050
+ let modelsText;
62792
63051
  try {
62793
- txt = fs11.readFileSync(modelsPath, "utf8");
63052
+ modelsText = fs11.readFileSync(modelsPath, "utf8");
63053
+ generated.push("models.yml");
62794
63054
  } catch {
62795
- return void 0;
62796
63055
  }
62797
- const httpMap = new Map(httpRewrites.map((r) => [r.key, wrapUpstream(origin, r.realUpstream)]));
62798
- const httpsMap = new Map(httpsRewrites.map((r) => [r.key, r.realUpstream]));
62799
- const lines = txt.split(/\r?\n/);
62800
- let providersIndent = -1;
62801
- let providerIndent = -1;
62802
- let currentProvider = null;
62803
- for (let i = 0; i < lines.length; i++) {
62804
- const rawLine = lines[i];
62805
- const trimmed = rawLine.trim();
62806
- if (!trimmed || trimmed.startsWith("#")) continue;
62807
- const indent = rawLine.length - rawLine.trimStart().length;
62808
- if (providersIndent === -1) {
62809
- if (/^providers:\s*(#.*)?$/.test(trimmed)) providersIndent = indent;
62810
- continue;
62811
- }
62812
- if (indent <= providersIndent) break;
62813
- if (providerIndent === -1) providerIndent = indent;
62814
- if (indent === providerIndent) {
62815
- const m2 = /^([A-Za-z0-9_.-]+):/.exec(trimmed);
62816
- currentProvider = m2 ? m2[1] : null;
62817
- } else if (indent > providerIndent && currentProvider) {
62818
- const baseMatch = /^(baseUrl:\s*)(\S+)/.exec(trimmed);
62819
- if (baseMatch) {
62820
- const target = httpMap.get(currentProvider) ?? httpsMap.get(currentProvider);
62821
- if (target) {
62822
- const leading = rawLine.slice(0, rawLine.length - rawLine.trimStart().length);
62823
- const commentMatch = /\s+#.*$/.exec(rawLine);
62824
- const comment = commentMatch ? commentMatch[0] : "";
62825
- lines[i] = `${leading}baseUrl: ${target}${comment}`;
63056
+ if (!refreshOverlayHome(ompHome, overlay, generated)) return void 0;
63057
+ if (modelsText !== void 0) {
63058
+ const httpMap = new Map(httpRewrites.map((r) => [r.key, wrapUpstream(origin, r.realUpstream)]));
63059
+ const httpsMap = new Map(httpsRewrites.map((r) => [r.key, r.realUpstream]));
63060
+ const lines = modelsText.split(/\r?\n/);
63061
+ let providersIndent = -1;
63062
+ let providerIndent = -1;
63063
+ let currentProvider = null;
63064
+ for (let i = 0; i < lines.length; i++) {
63065
+ const rawLine = lines[i];
63066
+ const trimmed = rawLine.trim();
63067
+ if (!trimmed || trimmed.startsWith("#")) continue;
63068
+ const indent = rawLine.length - rawLine.trimStart().length;
63069
+ if (providersIndent === -1) {
63070
+ if (/^providers:\s*(#.*)?$/.test(trimmed)) providersIndent = indent;
63071
+ continue;
63072
+ }
63073
+ if (indent <= providersIndent) break;
63074
+ if (providerIndent === -1) providerIndent = indent;
63075
+ if (indent === providerIndent) {
63076
+ const m2 = /^([A-Za-z0-9_.-]+):/.exec(trimmed);
63077
+ currentProvider = m2 ? m2[1] : null;
63078
+ } else if (indent > providerIndent && currentProvider) {
63079
+ const baseMatch = /^(baseUrl:\s*)(\S+)/.exec(trimmed);
63080
+ if (baseMatch) {
63081
+ const target = httpMap.get(currentProvider) ?? httpsMap.get(currentProvider);
63082
+ if (target) {
63083
+ const leading = rawLine.slice(0, rawLine.length - rawLine.trimStart().length);
63084
+ const commentMatch = /\s+#.*$/.exec(rawLine);
63085
+ const comment = commentMatch ? commentMatch[0] : "";
63086
+ lines[i] = `${leading}baseUrl: ${target}${comment}`;
63087
+ }
62826
63088
  }
62827
63089
  }
62828
63090
  }
63091
+ writeOverlayFileAtomic(overlay, "models.yml", lines.join("\n"));
62829
63092
  }
62830
- const overlay = `${ompHome}-bili`;
62831
- if (!refreshOverlayHome(ompHome, overlay, "models.yml")) return void 0;
62832
- writeOverlayFileAtomic(overlay, "models.yml", lines.join("\n"));
63093
+ writeOmpCompactionDisabledConfig(ompHome, overlay);
62833
63094
  return overlay;
62834
63095
  }
62835
63096
  function prepareHermesHome(hermesHome, origin, rewrites) {