billion-context 0.1.77-pr.502.321 → 0.1.77

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
@@ -49846,7 +49846,6 @@ var SessionStore = class {
49846
49846
  const byLabel = /* @__PURE__ */ new Map();
49847
49847
  let unlabeled = 0;
49848
49848
  for (const [id, envelope] of loaded) {
49849
- if (id.startsWith("pfa-")) continue;
49850
49849
  const session = buildSession(envelope.payload);
49851
49850
  const label = session.meta.label;
49852
49851
  if (!label) {
@@ -50037,201 +50036,6 @@ function getStore() {
50037
50036
  return _store;
50038
50037
  }
50039
50038
 
50040
- // src/prefix-affinity.ts
50041
- import { createHash as createHash4 } from "crypto";
50042
- var MIN_CANONICAL_BYTES = 24;
50043
- var MAX_TRACKED_SESSIONS = 256;
50044
- var TTL_MS2 = 7 * 24 * 60 * 60 * 1e3;
50045
- var TAIL_WINDOW = 8;
50046
- var MIN_TAIL_MATCH = 3;
50047
- var MAX_STORED_ITEMS = 128;
50048
- var MIN_FORK_PREFIX = 3;
50049
- function stableStringify(value) {
50050
- return JSON.stringify(sortKeys(value));
50051
- }
50052
- function sortKeys(value) {
50053
- if (Array.isArray(value)) return value.map(sortKeys);
50054
- if (value && typeof value === "object") {
50055
- const record = value;
50056
- const out = {};
50057
- for (const key of Object.keys(record).sort()) out[key] = sortKeys(record[key]);
50058
- return out;
50059
- }
50060
- return value;
50061
- }
50062
- function sha256(text) {
50063
- return createHash4("sha256").update(text, "utf8").digest("hex");
50064
- }
50065
- function hasUserMessage(messages) {
50066
- return messages.some((m2) => !!m2 && typeof m2 === "object" && m2.role === "user");
50067
- }
50068
- function chainHashes(messages) {
50069
- const hashes = [];
50070
- let prev = "";
50071
- let bytes = 0;
50072
- for (const message of messages) {
50073
- const canonical = stableStringify(message);
50074
- bytes += canonical.length;
50075
- prev = sha256(`${prev}\0${canonical}`);
50076
- hashes.push(prev);
50077
- }
50078
- return bytes >= MIN_CANONICAL_BYTES ? hashes : [];
50079
- }
50080
- function perItemHashes(messages) {
50081
- return messages.map((m2) => sha256(stableStringify(m2)));
50082
- }
50083
- function lcpLength(a, b2) {
50084
- const n = Math.min(a.length, b2.length);
50085
- let i = 0;
50086
- while (i < n && a[i] === b2[i]) i++;
50087
- return i;
50088
- }
50089
- var PrefixAffinityResolver = class {
50090
- trackedChains = /* @__PURE__ */ new Map();
50091
- /**
50092
- * Resolve an anonymous request to a session id.
50093
- * Returns null when the request carries no usable conversation signal
50094
- * (caller keeps the #286 explicit 400).
50095
- */
50096
- resolve(messages) {
50097
- const hashes = chainHashes(messages);
50098
- if (hashes.length === 0 || !hasUserMessage(messages)) return null;
50099
- const incomingDepth = hashes.length;
50100
- const tailHash = hashes[incomingDepth - 1];
50101
- const incItemHashes = perItemHashes(messages);
50102
- const storedItemHashes = incItemHashes.slice(-MAX_STORED_ITEMS);
50103
- const tracked = this.trackedChains;
50104
- this.expire(tracked);
50105
- let best;
50106
- for (const entry of tracked.values()) {
50107
- if (entry.depth > incomingDepth) continue;
50108
- if (hashes[entry.depth - 1] !== entry.tailHash) continue;
50109
- if (!best || entry.depth > best.depth || entry.depth === best.depth && entry.lastSeen > best.lastSeen) best = entry;
50110
- }
50111
- if (best) {
50112
- return {
50113
- sessionId: best.sessionId,
50114
- matchedDepth: best.depth,
50115
- storedDepth: best.depth,
50116
- incomingDepth,
50117
- tailHash,
50118
- via: "prefix",
50119
- itemHashes: storedItemHashes
50120
- };
50121
- }
50122
- const w2 = Math.min(TAIL_WINDOW, incomingDepth);
50123
- const candidates = [];
50124
- if (w2 >= MIN_TAIL_MATCH) {
50125
- for (const entry of tracked.values()) {
50126
- const stored = entry.itemHashes;
50127
- for (let j2 = 1; j2 + w2 <= stored.length; j2++) {
50128
- let match = true;
50129
- for (let i = 0; i < w2; i++) {
50130
- if (incItemHashes[i] !== stored[j2 + i]) {
50131
- match = false;
50132
- break;
50133
- }
50134
- }
50135
- if (match) {
50136
- candidates.push(entry);
50137
- break;
50138
- }
50139
- }
50140
- }
50141
- }
50142
- if (candidates.length === 1) {
50143
- const entry = candidates[0];
50144
- const w3 = Math.min(TAIL_WINDOW, incomingDepth, entry.depth);
50145
- return {
50146
- sessionId: entry.sessionId,
50147
- matchedDepth: w3,
50148
- storedDepth: entry.depth,
50149
- incomingDepth,
50150
- tailHash,
50151
- via: "tail-window",
50152
- itemHashes: storedItemHashes
50153
- };
50154
- }
50155
- let lineage;
50156
- if (candidates.length > 1) {
50157
- lineage = { parents: candidates.map((c) => c.sessionId), reason: "truncated" };
50158
- } else {
50159
- let forkParent;
50160
- let forkLcp = 0;
50161
- for (const entry of tracked.values()) {
50162
- if (entry.depth > MAX_STORED_ITEMS) continue;
50163
- const lcp = lcpLength(incItemHashes, entry.itemHashes);
50164
- if (lcp >= MIN_FORK_PREFIX && lcp > forkLcp) {
50165
- forkLcp = lcp;
50166
- forkParent = entry;
50167
- }
50168
- }
50169
- if (forkParent) lineage = { parents: [forkParent.sessionId], reason: "forked", sharedPrefix: forkLcp };
50170
- }
50171
- const sessionId = `pfa-${sha256(`${tailHash}\0${incomingDepth}`).slice(0, 16)}`;
50172
- return {
50173
- sessionId,
50174
- matchedDepth: 0,
50175
- storedDepth: 0,
50176
- incomingDepth,
50177
- tailHash,
50178
- via: "new",
50179
- itemHashes: storedItemHashes,
50180
- ...lineage ? { lineage } : {}
50181
- };
50182
- }
50183
- /** Record the chain of a session (on creation and after every anonymous
50184
- * request — the incoming history is the truth, appends extend it). */
50185
- note(sessionId, depth, tailHash, itemHashes) {
50186
- const tracked = this.trackedChains;
50187
- tracked.delete(sessionId);
50188
- tracked.set(sessionId, { sessionId, depth, tailHash, itemHashes, lastSeen: Date.now() });
50189
- while (tracked.size > MAX_TRACKED_SESSIONS) {
50190
- const oldest = [...tracked.values()].sort((a, b2) => a.lastSeen - b2.lastSeen)[0];
50191
- if (!oldest) break;
50192
- tracked.delete(oldest.sessionId);
50193
- }
50194
- }
50195
- /** Re-seed tracked chains from persisted metadata after a restart (#499).
50196
- * The in-memory table is the ONLY routing path for anonymous sessions:
50197
- * without this, a fresh process mints a NEW id for the client's next
50198
- * replay (its history has grown past the creation-time depth that anchored
50199
- * the deterministic id), orphaning the saved folded state and dropping the
50200
- * upstream prompt cache to 0%. Records are the raw
50201
- * session.metadata.anonymousPrefixAffinity objects written per request in
50202
- * server.ts; malformed records are skipped. Returns the number restored. */
50203
- reseed(records) {
50204
- let n = 0;
50205
- for (const record of records) {
50206
- if (!record || typeof record !== "object") continue;
50207
- const r = record;
50208
- if (typeof r.sessionId !== "string" || !r.sessionId.startsWith("pfa-")) continue;
50209
- const depth = r.depth;
50210
- if (typeof depth !== "number" || !Number.isInteger(depth) || depth <= 0) continue;
50211
- if (typeof r.tailHash !== "string" || r.tailHash.length === 0) continue;
50212
- const itemHashes = Array.isArray(r.itemHashes) ? r.itemHashes.filter((h) => typeof h === "string").slice(-MAX_STORED_ITEMS) : [];
50213
- this.note(r.sessionId, depth, r.tailHash, itemHashes);
50214
- n++;
50215
- }
50216
- return n;
50217
- }
50218
- /** Drop tracking for a removed session. */
50219
- forget(sessionId) {
50220
- this.trackedChains.delete(sessionId);
50221
- }
50222
- trackedSessionIds() {
50223
- return [...this.trackedChains.keys()];
50224
- }
50225
- expire(tracked) {
50226
- if (tracked.size === 0) return;
50227
- const now = Date.now();
50228
- for (const [id, entry] of tracked) {
50229
- if (now - entry.lastSeen > TTL_MS2) tracked.delete(id);
50230
- }
50231
- }
50232
- };
50233
- var prefixAffinity = new PrefixAffinityResolver();
50234
-
50235
50039
  // src/session.ts
50236
50040
  function lastCompressSuffix(info) {
50237
50041
  if (!info) return "";
@@ -50257,10 +50061,6 @@ async function initSessions() {
50257
50061
  } else {
50258
50062
  for (const [id, s3] of loaded) sessions.set(id, s3);
50259
50063
  }
50260
- const reseeded = prefixAffinity.reseed(
50261
- [...loaded.values()].map((s3) => s3.metadata.anonymousPrefixAffinity)
50262
- );
50263
- if (reseeded > 0) log("info", `[prefix-affinity] re-seeded ${reseeded} tracked chain(s) from persisted sessions`);
50264
50064
  }
50265
50065
  function getSession(id, meta) {
50266
50066
  const existing = sessions.get(id);
@@ -52180,9 +51980,9 @@ async function* runCompressLoop(upstream, ctx, requestBody, requestOptions, adap
52180
51980
  }
52181
51981
 
52182
51982
  // src/util.ts
52183
- import { createHash as createHash5 } from "crypto";
51983
+ import { createHash as createHash4 } from "crypto";
52184
51984
  function hashId2(s3) {
52185
- return createHash5("sha256").update(s3, "utf8").digest("hex").slice(0, 16);
51985
+ return createHash4("sha256").update(s3, "utf8").digest("hex").slice(0, 16);
52186
51986
  }
52187
51987
  function isLoopbackAddress(addr) {
52188
51988
  return !!addr && (addr.startsWith("127.") || addr === "::1" || addr.startsWith("::ffff:127."));
@@ -54138,6 +53938,178 @@ function codexTurnIdentity(headers) {
54138
53938
  return { value: threadHeader.trim(), threadSource };
54139
53939
  }
54140
53940
 
53941
+ // src/prefix-affinity.ts
53942
+ import { createHash as createHash5 } from "crypto";
53943
+ var MIN_CANONICAL_BYTES = 24;
53944
+ var MAX_TRACKED_SESSIONS = 256;
53945
+ var TTL_MS2 = 7 * 24 * 60 * 60 * 1e3;
53946
+ var TAIL_WINDOW = 8;
53947
+ var MIN_TAIL_MATCH = 3;
53948
+ var MAX_STORED_ITEMS = 128;
53949
+ var MIN_FORK_PREFIX = 3;
53950
+ function stableStringify(value) {
53951
+ return JSON.stringify(sortKeys(value));
53952
+ }
53953
+ function sortKeys(value) {
53954
+ if (Array.isArray(value)) return value.map(sortKeys);
53955
+ if (value && typeof value === "object") {
53956
+ const record = value;
53957
+ const out = {};
53958
+ for (const key of Object.keys(record).sort()) out[key] = sortKeys(record[key]);
53959
+ return out;
53960
+ }
53961
+ return value;
53962
+ }
53963
+ function sha256(text) {
53964
+ return createHash5("sha256").update(text, "utf8").digest("hex");
53965
+ }
53966
+ function hasUserMessage(messages) {
53967
+ return messages.some((m2) => !!m2 && typeof m2 === "object" && m2.role === "user");
53968
+ }
53969
+ function chainHashes(messages) {
53970
+ const hashes = [];
53971
+ let prev = "";
53972
+ let bytes = 0;
53973
+ for (const message of messages) {
53974
+ const canonical = stableStringify(message);
53975
+ bytes += canonical.length;
53976
+ prev = sha256(`${prev}\0${canonical}`);
53977
+ hashes.push(prev);
53978
+ }
53979
+ return bytes >= MIN_CANONICAL_BYTES ? hashes : [];
53980
+ }
53981
+ function perItemHashes(messages) {
53982
+ return messages.map((m2) => sha256(stableStringify(m2)));
53983
+ }
53984
+ function lcpLength(a, b2) {
53985
+ const n = Math.min(a.length, b2.length);
53986
+ let i = 0;
53987
+ while (i < n && a[i] === b2[i]) i++;
53988
+ return i;
53989
+ }
53990
+ var PrefixAffinityResolver = class {
53991
+ trackedChains = /* @__PURE__ */ new Map();
53992
+ /**
53993
+ * Resolve an anonymous request to a session id.
53994
+ * Returns null when the request carries no usable conversation signal
53995
+ * (caller keeps the #286 explicit 400).
53996
+ */
53997
+ resolve(messages) {
53998
+ const hashes = chainHashes(messages);
53999
+ if (hashes.length === 0 || !hasUserMessage(messages)) return null;
54000
+ const incomingDepth = hashes.length;
54001
+ const tailHash = hashes[incomingDepth - 1];
54002
+ const incItemHashes = perItemHashes(messages);
54003
+ const storedItemHashes = incItemHashes.slice(-MAX_STORED_ITEMS);
54004
+ const tracked = this.trackedChains;
54005
+ this.expire(tracked);
54006
+ let best;
54007
+ for (const entry of tracked.values()) {
54008
+ if (entry.depth > incomingDepth) continue;
54009
+ if (hashes[entry.depth - 1] !== entry.tailHash) continue;
54010
+ if (!best || entry.depth > best.depth || entry.depth === best.depth && entry.lastSeen > best.lastSeen) best = entry;
54011
+ }
54012
+ if (best) {
54013
+ return {
54014
+ sessionId: best.sessionId,
54015
+ matchedDepth: best.depth,
54016
+ storedDepth: best.depth,
54017
+ incomingDepth,
54018
+ tailHash,
54019
+ via: "prefix",
54020
+ itemHashes: storedItemHashes
54021
+ };
54022
+ }
54023
+ const w2 = Math.min(TAIL_WINDOW, incomingDepth);
54024
+ const candidates = [];
54025
+ if (w2 >= MIN_TAIL_MATCH) {
54026
+ for (const entry of tracked.values()) {
54027
+ const stored = entry.itemHashes;
54028
+ for (let j2 = 1; j2 + w2 <= stored.length; j2++) {
54029
+ let match = true;
54030
+ for (let i = 0; i < w2; i++) {
54031
+ if (incItemHashes[i] !== stored[j2 + i]) {
54032
+ match = false;
54033
+ break;
54034
+ }
54035
+ }
54036
+ if (match) {
54037
+ candidates.push(entry);
54038
+ break;
54039
+ }
54040
+ }
54041
+ }
54042
+ }
54043
+ if (candidates.length === 1) {
54044
+ const entry = candidates[0];
54045
+ const w3 = Math.min(TAIL_WINDOW, incomingDepth, entry.depth);
54046
+ return {
54047
+ sessionId: entry.sessionId,
54048
+ matchedDepth: w3,
54049
+ storedDepth: entry.depth,
54050
+ incomingDepth,
54051
+ tailHash,
54052
+ via: "tail-window",
54053
+ itemHashes: storedItemHashes
54054
+ };
54055
+ }
54056
+ let lineage;
54057
+ if (candidates.length > 1) {
54058
+ lineage = { parents: candidates.map((c) => c.sessionId), reason: "truncated" };
54059
+ } else {
54060
+ let forkParent;
54061
+ let forkLcp = 0;
54062
+ for (const entry of tracked.values()) {
54063
+ if (entry.depth > MAX_STORED_ITEMS) continue;
54064
+ const lcp = lcpLength(incItemHashes, entry.itemHashes);
54065
+ if (lcp >= MIN_FORK_PREFIX && lcp > forkLcp) {
54066
+ forkLcp = lcp;
54067
+ forkParent = entry;
54068
+ }
54069
+ }
54070
+ if (forkParent) lineage = { parents: [forkParent.sessionId], reason: "forked", sharedPrefix: forkLcp };
54071
+ }
54072
+ const sessionId = `pfa-${sha256(`${tailHash}\0${incomingDepth}`).slice(0, 16)}`;
54073
+ return {
54074
+ sessionId,
54075
+ matchedDepth: 0,
54076
+ storedDepth: 0,
54077
+ incomingDepth,
54078
+ tailHash,
54079
+ via: "new",
54080
+ itemHashes: storedItemHashes,
54081
+ ...lineage ? { lineage } : {}
54082
+ };
54083
+ }
54084
+ /** Record the chain of a session (on creation and after every anonymous
54085
+ * request — the incoming history is the truth, appends extend it). */
54086
+ note(sessionId, depth, tailHash, itemHashes) {
54087
+ const tracked = this.trackedChains;
54088
+ tracked.delete(sessionId);
54089
+ tracked.set(sessionId, { sessionId, depth, tailHash, itemHashes, lastSeen: Date.now() });
54090
+ while (tracked.size > MAX_TRACKED_SESSIONS) {
54091
+ const oldest = [...tracked.values()].sort((a, b2) => a.lastSeen - b2.lastSeen)[0];
54092
+ if (!oldest) break;
54093
+ tracked.delete(oldest.sessionId);
54094
+ }
54095
+ }
54096
+ /** Drop tracking for a removed session. */
54097
+ forget(sessionId) {
54098
+ this.trackedChains.delete(sessionId);
54099
+ }
54100
+ trackedSessionIds() {
54101
+ return [...this.trackedChains.keys()];
54102
+ }
54103
+ expire(tracked) {
54104
+ if (tracked.size === 0) return;
54105
+ const now = Date.now();
54106
+ for (const [id, entry] of tracked) {
54107
+ if (now - entry.lastSeen > TTL_MS2) tracked.delete(id);
54108
+ }
54109
+ }
54110
+ };
54111
+ var prefixAffinity = new PrefixAffinityResolver();
54112
+
54141
54113
  // node_modules/acp-kernel/dist/panel/index.js
54142
54114
  function topicFallback(summary) {
54143
54115
  const first = summary.split(/[.\n]/)[0] ?? "";
@@ -57146,11 +57118,9 @@ ${bodyBuffer.toString("utf8")}`);
57146
57118
  if (anonAffinity) {
57147
57119
  prefixAffinity.note(sessionId, anonAffinity.incomingDepth, anonAffinity.tailHash, anonAffinity.itemHashes);
57148
57120
  session.metadata.anonymousPrefixAffinity = {
57149
- sessionId: anonAffinity.sessionId,
57150
57121
  depth: anonAffinity.incomingDepth,
57151
57122
  tailHash: anonAffinity.tailHash,
57152
57123
  via: anonAffinity.via,
57153
- itemHashes: anonAffinity.itemHashes,
57154
57124
  ...anonAffinity.lineage ? { lineage: anonAffinity.lineage } : {}
57155
57125
  };
57156
57126
  }