billion-context 0.1.83-pr.517.340 → 0.1.83

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
@@ -49806,7 +49806,6 @@ function rel(p2, base) {
49806
49806
 
49807
49807
  // src/persist.ts
49808
49808
  var PERSIST_VERSION = 3;
49809
- var STALE_WARN_THROTTLE_MS = 6e4;
49810
49809
  function mergeState(parsed) {
49811
49810
  const fresh = createInitialState();
49812
49811
  return {
@@ -49837,13 +49836,10 @@ var SessionStore = class {
49837
49836
  enabled;
49838
49837
  dir;
49839
49838
  store;
49840
- log;
49841
- staleWarnAt = /* @__PURE__ */ new Map();
49842
49839
  constructor(opts) {
49843
49840
  const debounceMs = opts?.debounceMs ?? defaultDebounce();
49844
49841
  this.enabled = (opts?.enabled ?? true) && debounceMs >= 0;
49845
49842
  this.dir = opts?.dir ?? defaultDir();
49846
- this.log = opts?.log ?? defaultLogger;
49847
49843
  this.store = new StateStore({
49848
49844
  dir: this.dir,
49849
49845
  version: PERSIST_VERSION,
@@ -49939,74 +49935,29 @@ var SessionStore = class {
49939
49935
  * LRU eviction). Sync fs is acceptable here because a miss is rare and
49940
49936
  * reads a single small file (~1ms). Returns null if missing/corrupt or the
49941
49937
  * body id does not match what we asked for. */
49942
- /** Shared envelope probe for the sync read paths: namespaced path
49943
- * first, then the _unknown/ location, letting the kernel store layer
49944
- * also check its discovered map and the flat legacy name. */
49945
- loadEnvelope(id, meta) {
49938
+ loadSync(id, meta) {
49939
+ if (!this.enabled) return null;
49946
49940
  const envelopes = [
49947
49941
  this.store.loadSync(id, relPathFor(id, meta?.protocol, meta?.upstreamOrigin)),
49948
49942
  meta?.protocol ? this.store.loadSync(id, relPathFor(id)) : null
49949
49943
  ];
49950
49944
  for (const envelope of envelopes) {
49951
- if (envelope) return envelope;
49945
+ if (envelope) return buildSession(envelope.payload);
49952
49946
  }
49953
49947
  return null;
49954
49948
  }
49955
- loadSync(id, meta) {
49956
- if (!this.enabled) return null;
49957
- const envelope = this.loadEnvelope(id, meta);
49958
- if (!envelope) return null;
49959
- return buildSession(envelope.payload);
49960
- }
49961
- /** #405 fix #4: dual-instance rollback guard. When two proxy processes
49962
- * share BILI_SESSIONS_DIR, whoever saves last used to win — an instance
49963
- * holding a STALE in-memory copy would roll counters back (requests:3 →
49964
- * 2). Both signals below are monotonic per session id, so either being
49965
- * strictly smaller proves staleness. Returns the fresher-on-disk payload
49966
- * when the incoming record is stale, else null. */
49967
- staleDiskPayload(incoming) {
49968
- const envelope = this.loadEnvelope(incoming.id, incoming.meta);
49969
- if (!envelope) return null;
49970
- const disk = envelope.payload;
49971
- const diskRequests = disk.stats?.requests ?? disk.requests ?? 0;
49972
- const incRequests = incoming.stats?.requests ?? incoming.requests ?? 0;
49973
- if (incRequests < diskRequests) return disk;
49974
- if (incRequests === diskRequests) {
49975
- const diskBlocks = disk.state?.nextBlockId ?? 0;
49976
- const incBlocks = incoming.state?.nextBlockId ?? 0;
49977
- if (incBlocks < diskBlocks) return disk;
49978
- }
49979
- return null;
49980
- }
49981
- guardedBuild(session) {
49982
- return () => {
49983
- const record = buildRecord(session);
49984
- const disk = this.staleDiskPayload(record);
49985
- if (disk === null) return record;
49986
- const now = Date.now();
49987
- const last = this.staleWarnAt.get(record.id) ?? 0;
49988
- if (now - last >= STALE_WARN_THROTTLE_MS) {
49989
- this.staleWarnAt.set(record.id, now);
49990
- this.log(
49991
- "warn",
49992
- `[persist] rejected stale snapshot for session ${record.id}: in-memory copy is older than the on-disk one (another bili instance holds newer state) \u2014 keeping disk state, no rollback (#405)`
49993
- );
49994
- }
49995
- return disk;
49996
- };
49997
- }
49998
49949
  /** Schedule a debounced write for a session. Multiple calls within the
49999
49950
  * window coalesce; the record is built at WRITE time, so the freshest
50000
49951
  * session state is persisted. Safe to call on the hot path. No-op if
50001
49952
  * disabled. */
50002
49953
  scheduleSave(session) {
50003
- this.store.scheduleSave(session.id, this.guardedBuild(session));
49954
+ this.store.scheduleSave(session.id, () => buildRecord(session));
50004
49955
  }
50005
49956
  /** Asynchronously persist a session right now (skips the debounce). Throws
50006
49957
  * on write failure so callers can react (e.g. avoid evicting). Serialized
50007
49958
  * per-session by the kernel store's write chains. */
50008
49959
  async writeNow(session) {
50009
- await this.store.writeNow(session.id, this.guardedBuild(session));
49960
+ await this.store.writeNow(session.id, () => buildRecord(session));
50010
49961
  }
50011
49962
  /** Synchronous flush for a single session. Used on memory eviction so a
50012
49963
  * dirty evicted session is not lost. Sync because eviction runs in the
@@ -50014,7 +49965,7 @@ var SessionStore = class {
50014
49965
  * Returns true on success, false on failure (caller must NOT evict on
50015
49966
  * failure for a never-persisted session or it is lost permanently). */
50016
49967
  flushSync(session) {
50017
- return this.store.flushSync(session.id, this.guardedBuild(session));
49968
+ return this.store.flushSync(session.id, () => buildRecord(session));
50018
49969
  }
50019
49970
  /** Flush all dirty sessions with a pending debounce timer. Called on
50020
49971
  * SIGTERM/SIGINT for graceful shutdown. The kernel store flushes its own
@@ -57013,7 +56964,7 @@ Content-Length: ${Buffer.byteLength(body)}\r
57013
56964
  );
57014
56965
  }
57015
56966
  if (opts.debug) {
57016
- log2("info", `[debug] build features: raw-HTTP-capture(${bodyDumpEnabled() ? "on" : "off"}) | remote_compaction_v2-strip(on) | cert-MITM-launcher(on) | strip-acp-summary(on) \u2014 seeing this line confirms the launcher build (not registry 0.1.34)`);
56967
+ log2("info", `[debug] build features: raw-HTTP-capture(on) | remote_compaction_v2-strip(on) | cert-MITM-launcher(on) | strip-acp-summary(on) \u2014 seeing this line confirms the launcher build (not registry 0.1.34)`);
57017
56968
  }
57018
56969
  };
57019
56970
  const attemptListen = (port) => {