johns-harness 2026.9.34 → 2026.9.35

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/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 2026.9.35
4
+
5
+ - Family 81 (session store memory, 2026-09-28): the gateway no longer runs out of memory every few days. `sessions.json` stops storing `skillsSnapshot` and `systemPromptReport` in every entry (they are rebuilt in memory on demand); existing stores shrink on the first write after the update (the live main store: 27 MB to 0.3 MB). Writes are compact JSON with one fewer full clone per load: 347 ms to 5 ms per session update on the live store copy.
6
+ - Session maintenance now defaults to `enforce` (prune after 30 days, 500 entries, rotate at 10 MB) and never removes the active session, main sessions, or anything updated in the last 24 hours. Set `session.maintenance.mode: "warn"` to keep the old behavior.
7
+ - The gateway starts with `--max-old-space-size=min(8192, RAM/2)` MB (only when that raises the limit); override with `JOHNNESS_GATEWAY_MAX_OLD_SPACE_MB=<MB>` or disable with `off`. Existing service installs pick it up on the next restart.
8
+ - Exact replay `scripts/patch-session-store-memory-81.py` (11 files); verifier checks 81.1-81.7. Roll back with `npm i -g johns-harness@2026.9.34`.
9
+
3
10
  ## 2026.9.34
4
11
 
5
12
  - Family 80 (daily model integration, 2026-09-24): GPT-6 Sol (`gpt-6-sol`) and GPT-6 Luna (`gpt-6-luna`) ship on `openai` and `openai-codex`, and xAI Grok 4.7 (`grok-4.7`) on `xai`. Card data from the official model pages checked 2026-09-24: GPT-6 Sol $2/$10 per MTok, Luna $0.10/$0.50, both 1.05M native context and 128K max output (272K harness window, like every GPT entry), efforts none through max; Grok 4.7 $2/$6, 500K native context (200K harness window and 64K max output, like Grok 4.6), efforts low through xhigh. All three allow `/think xhigh`. GPT-6 Sol/Luna drop `temperature` unless reasoning effort is `none` (the API rejects it otherwise, verified live).
package/dist/entry.js CHANGED
@@ -104,6 +104,7 @@ function applyCliProfileEnv(params) {
104
104
  if (profile === "dev" && !env.JOHNNESS_GATEWAY_PORT?.trim()) env.JOHNNESS_GATEWAY_PORT = "19001";
105
105
  }
106
106
  //#endregion
107
+ import v8 from "node:v8"; // JOHNNESS_PATCH_SESSION_STORE_MEMORY_81
107
108
  //#region src/cli/respawn-policy.ts
108
109
  function shouldSkipRespawnForArgv(argv) {
109
110
  return hasHelpOrVersion(argv);
@@ -326,14 +327,32 @@ if (!isMainModule({
326
327
  for (const arg of process$1.execArgv) if (arg === EXPERIMENTAL_WARNING_FLAG || arg === "--no-warnings") return true;
327
328
  return false;
328
329
  }
330
+ // JOHNNESS_PATCH_SESSION_STORE_MEMORY_81: gateway heap. Node's default old-space limit (~4 GB) was the OOM ceiling. The
331
+ // gateway runs with --max-old-space-size=min(8192, RAM/2) MB, only when that raises the limit.
332
+ // JOHNNESS_GATEWAY_MAX_OLD_SPACE_MB=<MB> sets it explicitly; 0/off disables. A heap flag already
333
+ // in NODE_OPTIONS or execArgv wins. Applied at launch, so existing service installs get it.
334
+ function resolveGatewayHeapArgs() {
335
+ if (!process$1.argv.slice(2).includes("gateway")) return [];
336
+ const nodeOptions = process$1.env.NODE_OPTIONS ?? "";
337
+ if (nodeOptions.includes("--max-old-space-size") || process$1.execArgv.some((arg) => arg.startsWith("--max-old-space-size"))) return [];
338
+ const raw = (process$1.env.JOHNNESS_GATEWAY_MAX_OLD_SPACE_MB ?? "").trim();
339
+ if (/^(0|off|false|no|none)$/i.test(raw)) return [];
340
+ const explicit = /^[1-9][0-9]*$/.test(raw) ? Number(raw) : void 0;
341
+ const mb = explicit ?? Math.min(8192, Math.floor(os.totalmem() / 1048576 / 2));
342
+ const currentMb = v8.getHeapStatistics().heap_size_limit / 1048576;
343
+ if (explicit === void 0 && !(mb > currentMb)) return [];
344
+ return [`--max-old-space-size=${mb}`];
345
+ }
329
346
  function ensureExperimentalWarningSuppressed() {
330
347
  if (shouldSkipRespawnForArgv(process$1.argv)) return false;
331
348
  if (isTruthyEnvValue(process$1.env.JOHNNESS_NO_RESPAWN)) return false;
332
349
  if (isTruthyEnvValue(process$1.env.JOHNNESS_NODE_OPTIONS_READY)) return false;
333
- if (hasExperimentalWarningSuppressed()) return false;
350
+ const gatewayHeapArgs = resolveGatewayHeapArgs();
351
+ if (hasExperimentalWarningSuppressed() && gatewayHeapArgs.length === 0) return false;
334
352
  process$1.env.JOHNNESS_NODE_OPTIONS_READY = "1";
335
353
  const child = spawn(process$1.execPath, [
336
354
  EXPERIMENTAL_WARNING_FLAG,
355
+ ...gatewayHeapArgs,
337
356
  ...process$1.execArgv,
338
357
  ...process$1.argv.slice(1)
339
358
  ], {
@@ -7467,7 +7467,15 @@ const log$2 = createSubsystemLogger("sessions/store");
7467
7467
  const DEFAULT_SESSION_PRUNE_AFTER_MS = 720 * 60 * 60 * 1e3;
7468
7468
  const DEFAULT_SESSION_MAX_ENTRIES = 500;
7469
7469
  const DEFAULT_SESSION_ROTATE_BYTES = 10485760;
7470
- const DEFAULT_SESSION_MAINTENANCE_MODE = "warn";
7470
+ const DEFAULT_SESSION_MAINTENANCE_MODE = "enforce"; // JOHNNESS_PATCH_SESSION_STORE_MEMORY_81: was "warn" (never pruned)
7471
+ const SESSION_MAINTENANCE_PROTECT_RECENT_MS = 24 * 60 * 60 * 1e3;
7472
+ function isProtectedSessionEntry(key, entry, opts = {}) {
7473
+ const active = typeof opts.activeSessionKey === "string" ? opts.activeSessionKey.trim() : "";
7474
+ if (active && key === active) return true;
7475
+ if (key === "main" || key === "global" || key.endsWith(":main")) return true;
7476
+ const updatedAt = entry?.updatedAt;
7477
+ return typeof updatedAt === "number" && Date.now() - updatedAt < SESSION_MAINTENANCE_PROTECT_RECENT_MS;
7478
+ }
7471
7479
  const DEFAULT_SESSION_DISK_BUDGET_HIGH_WATER_RATIO = .8;
7472
7480
  function resolvePruneAfterMs(maintenance) {
7473
7481
  const raw = maintenance?.pruneAfter ?? maintenance?.pruneDays;
@@ -7552,7 +7560,7 @@ function pruneStaleEntries(store, overrideMaxAgeMs, opts = {}) {
7552
7560
  const maxAgeMs = overrideMaxAgeMs ?? resolveMaintenanceConfig().pruneAfterMs;
7553
7561
  const cutoffMs = Date.now() - maxAgeMs;
7554
7562
  let pruned = 0;
7555
- for (const [key, entry] of Object.entries(store)) if (entry?.updatedAt != null && entry.updatedAt < cutoffMs) {
7563
+ for (const [key, entry] of Object.entries(store)) if (entry?.updatedAt != null && entry.updatedAt < cutoffMs && !isProtectedSessionEntry(key, entry, opts)) {
7556
7564
  opts.onPruned?.({
7557
7565
  key,
7558
7566
  entry
@@ -7601,7 +7609,7 @@ function capEntryCount(store, overrideMax, opts = {}) {
7601
7609
  const toRemove = keys.toSorted((a, b) => {
7602
7610
  const aTime = getEntryUpdatedAt(store[a]);
7603
7611
  return getEntryUpdatedAt(store[b]) - aTime;
7604
- }).slice(maxEntries);
7612
+ }).slice(maxEntries).filter((key) => !isProtectedSessionEntry(key, store[key], opts));
7605
7613
  for (const key of toRemove) {
7606
7614
  const entry = store[key];
7607
7615
  if (entry) opts.onCapped?.({
@@ -7753,6 +7761,59 @@ function normalizeSessionStore(store) {
7753
7761
  if (normalized !== entry) store[key] = normalized;
7754
7762
  }
7755
7763
  }
7764
+ // JOHNNESS_PATCH_SESSION_STORE_MEMORY_81: skillsSnapshot and systemPromptReport are rebuilt per process
7765
+ // (ensureSkillSnapshot, resolveCronSkillsSnapshot, resolveContextReport) and were 80%+ of
7766
+ // sessions.json. They live here, keyed by store path + session key + sessionId, never on disk.
7767
+ const SESSION_RUNTIME_FIELD_NAMES = ["skillsSnapshot", "systemPromptReport"];
7768
+ const SESSION_RUNTIME_FIELDS_KEEP_MS = 24 * 60 * 60 * 1e3;
7769
+ const SESSION_RUNTIME_FIELDS = /* @__PURE__ */ new Map();
7770
+ function stripSessionRuntimeFields(store) {
7771
+ let stripped = 0;
7772
+ for (const entry of Object.values(store)) {
7773
+ if (!entry || typeof entry !== "object") continue;
7774
+ for (const field of SESSION_RUNTIME_FIELD_NAMES) if (field in entry) {
7775
+ delete entry[field];
7776
+ stripped++;
7777
+ }
7778
+ }
7779
+ return stripped;
7780
+ }
7781
+ function attachSessionRuntimeFields(storePath, store) {
7782
+ const side = SESSION_RUNTIME_FIELDS.get(storePath);
7783
+ if (!side || side.size === 0) return store;
7784
+ for (const [key, kept] of side) {
7785
+ const entry = store[key];
7786
+ if (!entry || typeof entry !== "object" || entry.sessionId !== kept.sessionId) continue;
7787
+ for (const field of SESSION_RUNTIME_FIELD_NAMES) if (kept[field] !== void 0 && entry[field] === void 0) entry[field] = kept[field];
7788
+ }
7789
+ return store;
7790
+ }
7791
+ function persistableSessionStore(storePath, store) {
7792
+ const side = SESSION_RUNTIME_FIELDS.get(storePath) ?? /* @__PURE__ */ new Map();
7793
+ const cutoff = Date.now() - SESSION_RUNTIME_FIELDS_KEEP_MS;
7794
+ let out = store;
7795
+ for (const [key, entry] of Object.entries(store)) {
7796
+ if (!entry || typeof entry !== "object") continue;
7797
+ if (!SESSION_RUNTIME_FIELD_NAMES.some((field) => entry[field] !== void 0)) {
7798
+ side.delete(key);
7799
+ continue;
7800
+ }
7801
+ if (out === store) out = { ...store };
7802
+ const copy = { ...entry };
7803
+ const kept = { sessionId: entry.sessionId };
7804
+ for (const field of SESSION_RUNTIME_FIELD_NAMES) {
7805
+ if (copy[field] !== void 0) kept[field] = copy[field];
7806
+ delete copy[field];
7807
+ }
7808
+ out[key] = copy;
7809
+ if (typeof entry.updatedAt === "number" && entry.updatedAt < cutoff) side.delete(key);
7810
+ else side.set(key, kept);
7811
+ }
7812
+ for (const key of side.keys()) if (!store[key]) side.delete(key);
7813
+ if (side.size > 0) SESSION_RUNTIME_FIELDS.set(storePath, side);
7814
+ else SESSION_RUNTIME_FIELDS.delete(storePath);
7815
+ return out;
7816
+ }
7756
7817
  function loadSessionStore(storePath, opts = {}) {
7757
7818
  if (!opts.skipCache && isSessionStoreCacheEnabled()) {
7758
7819
  const currentFileStat = getFileStatSnapshot(storePath);
@@ -7762,7 +7823,7 @@ function loadSessionStore(storePath, opts = {}) {
7762
7823
  mtimeMs: currentFileStat?.mtimeMs,
7763
7824
  sizeBytes: currentFileStat?.sizeBytes
7764
7825
  });
7765
- if (cached) return cached;
7826
+ if (cached) return attachSessionRuntimeFields(storePath, cached);
7766
7827
  }
7767
7828
  let store = {};
7768
7829
  let fileStat = getFileStatSnapshot(storePath);
@@ -7793,6 +7854,7 @@ function loadSessionStore(storePath, opts = {}) {
7793
7854
  if (serializedFromDisk !== void 0) setSerializedSessionStore(storePath, serializedFromDisk);
7794
7855
  else setSerializedSessionStore(storePath, void 0);
7795
7856
  applySessionStoreMigrations(store);
7857
+ stripSessionRuntimeFields(store);
7796
7858
  if (!opts.skipCache && isSessionStoreCacheEnabled()) writeSessionStoreCache({
7797
7859
  storePath,
7798
7860
  store,
@@ -7800,7 +7862,8 @@ function loadSessionStore(storePath, opts = {}) {
7800
7862
  sizeBytes: fileStat?.sizeBytes,
7801
7863
  serialized: serializedFromDisk
7802
7864
  });
7803
- return structuredClone(store);
7865
+ // JOHNNESS_PATCH_SESSION_STORE_MEMORY_81: `store` was parsed just now and the cache holds its own clone.
7866
+ return attachSessionRuntimeFields(storePath, store);
7804
7867
  }
7805
7868
  function readSessionUpdatedAt(params) {
7806
7869
  try {
@@ -7874,10 +7937,10 @@ async function saveSessionStoreUnlocked(storePath, store, opts) {
7874
7937
  });
7875
7938
  } else {
7876
7939
  const removedSessionFiles = /* @__PURE__ */ new Map();
7877
- const pruned = pruneStaleEntries(store, maintenance.pruneAfterMs, { onPruned: ({ entry }) => {
7940
+ const pruned = pruneStaleEntries(store, maintenance.pruneAfterMs, { activeSessionKey: opts?.activeSessionKey, onPruned: ({ entry }) => {
7878
7941
  rememberRemovedSessionFile(removedSessionFiles, entry);
7879
7942
  } });
7880
- const capped = capEntryCount(store, maintenance.maxEntries, { onCapped: ({ entry }) => {
7943
+ const capped = capEntryCount(store, maintenance.maxEntries, { activeSessionKey: opts?.activeSessionKey, onCapped: ({ entry }) => {
7881
7944
  rememberRemovedSessionFile(removedSessionFiles, entry);
7882
7945
  } });
7883
7946
  const archivedDirs = /* @__PURE__ */ new Set();
@@ -7922,7 +7985,9 @@ async function saveSessionStoreUnlocked(storePath, store, opts) {
7922
7985
  }
7923
7986
  }
7924
7987
  await fs$1.promises.mkdir(path.dirname(storePath), { recursive: true });
7925
- const json = JSON.stringify(store, null, 2);
7988
+ // JOHNNESS_PATCH_SESSION_STORE_MEMORY_81: persist without runtime fields, compact (was 2-space indent).
7989
+ store = persistableSessionStore(storePath, store);
7990
+ const json = JSON.stringify(store);
7926
7991
  if (getSerializedSessionStore(storePath) === json) {
7927
7992
  updateSessionStoreWriteCaches({
7928
7993
  storePath,
@@ -7974,6 +8039,9 @@ async function saveSessionStoreUnlocked(storePath, store, opts) {
7974
8039
  }
7975
8040
  async function updateSessionStore(storePath, mutator, opts) {
7976
8041
  return await withSessionStoreLock(storePath, async () => {
8042
+ // JOHNNESS_PATCH_SESSION_STORE_MEMORY_81: skipCache stays. This runs under the cross-process file lock and the cache is
8043
+ // validated only by mtime + size, so a same-size write by another process could be missed.
8044
+ // The re-read is ~3 MB now that runtime fields are not persisted.
7977
8045
  const store = loadSessionStore(storePath, { skipCache: true });
7978
8046
  const result = await mutator(store);
7979
8047
  await saveSessionStoreUnlocked(storePath, store, opts);
@@ -7464,7 +7464,15 @@ const log$2 = createSubsystemLogger("sessions/store");
7464
7464
  const DEFAULT_SESSION_PRUNE_AFTER_MS = 720 * 60 * 60 * 1e3;
7465
7465
  const DEFAULT_SESSION_MAX_ENTRIES = 500;
7466
7466
  const DEFAULT_SESSION_ROTATE_BYTES = 10485760;
7467
- const DEFAULT_SESSION_MAINTENANCE_MODE = "warn";
7467
+ const DEFAULT_SESSION_MAINTENANCE_MODE = "enforce"; // JOHNNESS_PATCH_SESSION_STORE_MEMORY_81: was "warn" (never pruned)
7468
+ const SESSION_MAINTENANCE_PROTECT_RECENT_MS = 24 * 60 * 60 * 1e3;
7469
+ function isProtectedSessionEntry(key, entry, opts = {}) {
7470
+ const active = typeof opts.activeSessionKey === "string" ? opts.activeSessionKey.trim() : "";
7471
+ if (active && key === active) return true;
7472
+ if (key === "main" || key === "global" || key.endsWith(":main")) return true;
7473
+ const updatedAt = entry?.updatedAt;
7474
+ return typeof updatedAt === "number" && Date.now() - updatedAt < SESSION_MAINTENANCE_PROTECT_RECENT_MS;
7475
+ }
7468
7476
  const DEFAULT_SESSION_DISK_BUDGET_HIGH_WATER_RATIO = .8;
7469
7477
  function resolvePruneAfterMs(maintenance) {
7470
7478
  const raw = maintenance?.pruneAfter ?? maintenance?.pruneDays;
@@ -7549,7 +7557,7 @@ function pruneStaleEntries(store, overrideMaxAgeMs, opts = {}) {
7549
7557
  const maxAgeMs = overrideMaxAgeMs ?? resolveMaintenanceConfig().pruneAfterMs;
7550
7558
  const cutoffMs = Date.now() - maxAgeMs;
7551
7559
  let pruned = 0;
7552
- for (const [key, entry] of Object.entries(store)) if (entry?.updatedAt != null && entry.updatedAt < cutoffMs) {
7560
+ for (const [key, entry] of Object.entries(store)) if (entry?.updatedAt != null && entry.updatedAt < cutoffMs && !isProtectedSessionEntry(key, entry, opts)) {
7553
7561
  opts.onPruned?.({
7554
7562
  key,
7555
7563
  entry
@@ -7598,7 +7606,7 @@ function capEntryCount(store, overrideMax, opts = {}) {
7598
7606
  const toRemove = keys.toSorted((a, b) => {
7599
7607
  const aTime = getEntryUpdatedAt(store[a]);
7600
7608
  return getEntryUpdatedAt(store[b]) - aTime;
7601
- }).slice(maxEntries);
7609
+ }).slice(maxEntries).filter((key) => !isProtectedSessionEntry(key, store[key], opts));
7602
7610
  for (const key of toRemove) {
7603
7611
  const entry = store[key];
7604
7612
  if (entry) opts.onCapped?.({
@@ -7750,6 +7758,59 @@ function normalizeSessionStore(store) {
7750
7758
  if (normalized !== entry) store[key] = normalized;
7751
7759
  }
7752
7760
  }
7761
+ // JOHNNESS_PATCH_SESSION_STORE_MEMORY_81: skillsSnapshot and systemPromptReport are rebuilt per process
7762
+ // (ensureSkillSnapshot, resolveCronSkillsSnapshot, resolveContextReport) and were 80%+ of
7763
+ // sessions.json. They live here, keyed by store path + session key + sessionId, never on disk.
7764
+ const SESSION_RUNTIME_FIELD_NAMES = ["skillsSnapshot", "systemPromptReport"];
7765
+ const SESSION_RUNTIME_FIELDS_KEEP_MS = 24 * 60 * 60 * 1e3;
7766
+ const SESSION_RUNTIME_FIELDS = /* @__PURE__ */ new Map();
7767
+ function stripSessionRuntimeFields(store) {
7768
+ let stripped = 0;
7769
+ for (const entry of Object.values(store)) {
7770
+ if (!entry || typeof entry !== "object") continue;
7771
+ for (const field of SESSION_RUNTIME_FIELD_NAMES) if (field in entry) {
7772
+ delete entry[field];
7773
+ stripped++;
7774
+ }
7775
+ }
7776
+ return stripped;
7777
+ }
7778
+ function attachSessionRuntimeFields(storePath, store) {
7779
+ const side = SESSION_RUNTIME_FIELDS.get(storePath);
7780
+ if (!side || side.size === 0) return store;
7781
+ for (const [key, kept] of side) {
7782
+ const entry = store[key];
7783
+ if (!entry || typeof entry !== "object" || entry.sessionId !== kept.sessionId) continue;
7784
+ for (const field of SESSION_RUNTIME_FIELD_NAMES) if (kept[field] !== void 0 && entry[field] === void 0) entry[field] = kept[field];
7785
+ }
7786
+ return store;
7787
+ }
7788
+ function persistableSessionStore(storePath, store) {
7789
+ const side = SESSION_RUNTIME_FIELDS.get(storePath) ?? /* @__PURE__ */ new Map();
7790
+ const cutoff = Date.now() - SESSION_RUNTIME_FIELDS_KEEP_MS;
7791
+ let out = store;
7792
+ for (const [key, entry] of Object.entries(store)) {
7793
+ if (!entry || typeof entry !== "object") continue;
7794
+ if (!SESSION_RUNTIME_FIELD_NAMES.some((field) => entry[field] !== void 0)) {
7795
+ side.delete(key);
7796
+ continue;
7797
+ }
7798
+ if (out === store) out = { ...store };
7799
+ const copy = { ...entry };
7800
+ const kept = { sessionId: entry.sessionId };
7801
+ for (const field of SESSION_RUNTIME_FIELD_NAMES) {
7802
+ if (copy[field] !== void 0) kept[field] = copy[field];
7803
+ delete copy[field];
7804
+ }
7805
+ out[key] = copy;
7806
+ if (typeof entry.updatedAt === "number" && entry.updatedAt < cutoff) side.delete(key);
7807
+ else side.set(key, kept);
7808
+ }
7809
+ for (const key of side.keys()) if (!store[key]) side.delete(key);
7810
+ if (side.size > 0) SESSION_RUNTIME_FIELDS.set(storePath, side);
7811
+ else SESSION_RUNTIME_FIELDS.delete(storePath);
7812
+ return out;
7813
+ }
7753
7814
  function loadSessionStore(storePath, opts = {}) {
7754
7815
  if (!opts.skipCache && isSessionStoreCacheEnabled()) {
7755
7816
  const currentFileStat = getFileStatSnapshot(storePath);
@@ -7759,7 +7820,7 @@ function loadSessionStore(storePath, opts = {}) {
7759
7820
  mtimeMs: currentFileStat?.mtimeMs,
7760
7821
  sizeBytes: currentFileStat?.sizeBytes
7761
7822
  });
7762
- if (cached) return cached;
7823
+ if (cached) return attachSessionRuntimeFields(storePath, cached);
7763
7824
  }
7764
7825
  let store = {};
7765
7826
  let fileStat = getFileStatSnapshot(storePath);
@@ -7790,6 +7851,7 @@ function loadSessionStore(storePath, opts = {}) {
7790
7851
  if (serializedFromDisk !== void 0) setSerializedSessionStore(storePath, serializedFromDisk);
7791
7852
  else setSerializedSessionStore(storePath, void 0);
7792
7853
  applySessionStoreMigrations(store);
7854
+ stripSessionRuntimeFields(store);
7793
7855
  if (!opts.skipCache && isSessionStoreCacheEnabled()) writeSessionStoreCache({
7794
7856
  storePath,
7795
7857
  store,
@@ -7797,7 +7859,8 @@ function loadSessionStore(storePath, opts = {}) {
7797
7859
  sizeBytes: fileStat?.sizeBytes,
7798
7860
  serialized: serializedFromDisk
7799
7861
  });
7800
- return structuredClone(store);
7862
+ // JOHNNESS_PATCH_SESSION_STORE_MEMORY_81: `store` was parsed just now and the cache holds its own clone.
7863
+ return attachSessionRuntimeFields(storePath, store);
7801
7864
  }
7802
7865
  function readSessionUpdatedAt(params) {
7803
7866
  try {
@@ -7871,10 +7934,10 @@ async function saveSessionStoreUnlocked(storePath, store, opts) {
7871
7934
  });
7872
7935
  } else {
7873
7936
  const removedSessionFiles = /* @__PURE__ */ new Map();
7874
- const pruned = pruneStaleEntries(store, maintenance.pruneAfterMs, { onPruned: ({ entry }) => {
7937
+ const pruned = pruneStaleEntries(store, maintenance.pruneAfterMs, { activeSessionKey: opts?.activeSessionKey, onPruned: ({ entry }) => {
7875
7938
  rememberRemovedSessionFile(removedSessionFiles, entry);
7876
7939
  } });
7877
- const capped = capEntryCount(store, maintenance.maxEntries, { onCapped: ({ entry }) => {
7940
+ const capped = capEntryCount(store, maintenance.maxEntries, { activeSessionKey: opts?.activeSessionKey, onCapped: ({ entry }) => {
7878
7941
  rememberRemovedSessionFile(removedSessionFiles, entry);
7879
7942
  } });
7880
7943
  const archivedDirs = /* @__PURE__ */ new Set();
@@ -7919,7 +7982,9 @@ async function saveSessionStoreUnlocked(storePath, store, opts) {
7919
7982
  }
7920
7983
  }
7921
7984
  await syncFs.promises.mkdir(path.dirname(storePath), { recursive: true });
7922
- const json = JSON.stringify(store, null, 2);
7985
+ // JOHNNESS_PATCH_SESSION_STORE_MEMORY_81: persist without runtime fields, compact (was 2-space indent).
7986
+ store = persistableSessionStore(storePath, store);
7987
+ const json = JSON.stringify(store);
7923
7988
  if (getSerializedSessionStore(storePath) === json) {
7924
7989
  updateSessionStoreWriteCaches({
7925
7990
  storePath,
@@ -7976,6 +8041,9 @@ async function saveSessionStore(storePath, store, opts) {
7976
8041
  }
7977
8042
  async function updateSessionStore(storePath, mutator, opts) {
7978
8043
  return await withSessionStoreLock(storePath, async () => {
8044
+ // JOHNNESS_PATCH_SESSION_STORE_MEMORY_81: skipCache stays. This runs under the cross-process file lock and the cache is
8045
+ // validated only by mtime + size, so a same-size write by another process could be missed.
8046
+ // The re-read is ~3 MB now that runtime fields are not persisted.
7979
8047
  const store = loadSessionStore(storePath, { skipCache: true });
7980
8048
  const result = await mutator(store);
7981
8049
  await saveSessionStoreUnlocked(storePath, store, opts);
@@ -1469,7 +1469,15 @@ const log$3 = createSubsystemLogger("sessions/store");
1469
1469
  const DEFAULT_SESSION_PRUNE_AFTER_MS = 720 * 60 * 60 * 1e3;
1470
1470
  const DEFAULT_SESSION_MAX_ENTRIES = 500;
1471
1471
  const DEFAULT_SESSION_ROTATE_BYTES = 10485760;
1472
- const DEFAULT_SESSION_MAINTENANCE_MODE = "warn";
1472
+ const DEFAULT_SESSION_MAINTENANCE_MODE = "enforce"; // JOHNNESS_PATCH_SESSION_STORE_MEMORY_81: was "warn" (never pruned)
1473
+ const SESSION_MAINTENANCE_PROTECT_RECENT_MS = 24 * 60 * 60 * 1e3;
1474
+ function isProtectedSessionEntry(key, entry, opts = {}) {
1475
+ const active = typeof opts.activeSessionKey === "string" ? opts.activeSessionKey.trim() : "";
1476
+ if (active && key === active) return true;
1477
+ if (key === "main" || key === "global" || key.endsWith(":main")) return true;
1478
+ const updatedAt = entry?.updatedAt;
1479
+ return typeof updatedAt === "number" && Date.now() - updatedAt < SESSION_MAINTENANCE_PROTECT_RECENT_MS;
1480
+ }
1473
1481
  const DEFAULT_SESSION_DISK_BUDGET_HIGH_WATER_RATIO = .8;
1474
1482
  function resolvePruneAfterMs(maintenance) {
1475
1483
  const raw = maintenance?.pruneAfter ?? maintenance?.pruneDays;
@@ -1554,7 +1562,7 @@ function pruneStaleEntries(store, overrideMaxAgeMs, opts = {}) {
1554
1562
  const maxAgeMs = overrideMaxAgeMs ?? resolveMaintenanceConfig().pruneAfterMs;
1555
1563
  const cutoffMs = Date.now() - maxAgeMs;
1556
1564
  let pruned = 0;
1557
- for (const [key, entry] of Object.entries(store)) if (entry?.updatedAt != null && entry.updatedAt < cutoffMs) {
1565
+ for (const [key, entry] of Object.entries(store)) if (entry?.updatedAt != null && entry.updatedAt < cutoffMs && !isProtectedSessionEntry(key, entry, opts)) {
1558
1566
  opts.onPruned?.({
1559
1567
  key,
1560
1568
  entry
@@ -1603,7 +1611,7 @@ function capEntryCount(store, overrideMax, opts = {}) {
1603
1611
  const toRemove = keys.toSorted((a, b) => {
1604
1612
  const aTime = getEntryUpdatedAt(store[a]);
1605
1613
  return getEntryUpdatedAt(store[b]) - aTime;
1606
- }).slice(maxEntries);
1614
+ }).slice(maxEntries).filter((key) => !isProtectedSessionEntry(key, store[key], opts));
1607
1615
  for (const key of toRemove) {
1608
1616
  const entry = store[key];
1609
1617
  if (entry) opts.onCapped?.({
@@ -1755,6 +1763,59 @@ function normalizeSessionStore(store) {
1755
1763
  if (normalized !== entry) store[key] = normalized;
1756
1764
  }
1757
1765
  }
1766
+ // JOHNNESS_PATCH_SESSION_STORE_MEMORY_81: skillsSnapshot and systemPromptReport are rebuilt per process
1767
+ // (ensureSkillSnapshot, resolveCronSkillsSnapshot, resolveContextReport) and were 80%+ of
1768
+ // sessions.json. They live here, keyed by store path + session key + sessionId, never on disk.
1769
+ const SESSION_RUNTIME_FIELD_NAMES = ["skillsSnapshot", "systemPromptReport"];
1770
+ const SESSION_RUNTIME_FIELDS_KEEP_MS = 24 * 60 * 60 * 1e3;
1771
+ const SESSION_RUNTIME_FIELDS = /* @__PURE__ */ new Map();
1772
+ function stripSessionRuntimeFields(store) {
1773
+ let stripped = 0;
1774
+ for (const entry of Object.values(store)) {
1775
+ if (!entry || typeof entry !== "object") continue;
1776
+ for (const field of SESSION_RUNTIME_FIELD_NAMES) if (field in entry) {
1777
+ delete entry[field];
1778
+ stripped++;
1779
+ }
1780
+ }
1781
+ return stripped;
1782
+ }
1783
+ function attachSessionRuntimeFields(storePath, store) {
1784
+ const side = SESSION_RUNTIME_FIELDS.get(storePath);
1785
+ if (!side || side.size === 0) return store;
1786
+ for (const [key, kept] of side) {
1787
+ const entry = store[key];
1788
+ if (!entry || typeof entry !== "object" || entry.sessionId !== kept.sessionId) continue;
1789
+ for (const field of SESSION_RUNTIME_FIELD_NAMES) if (kept[field] !== void 0 && entry[field] === void 0) entry[field] = kept[field];
1790
+ }
1791
+ return store;
1792
+ }
1793
+ function persistableSessionStore(storePath, store) {
1794
+ const side = SESSION_RUNTIME_FIELDS.get(storePath) ?? /* @__PURE__ */ new Map();
1795
+ const cutoff = Date.now() - SESSION_RUNTIME_FIELDS_KEEP_MS;
1796
+ let out = store;
1797
+ for (const [key, entry] of Object.entries(store)) {
1798
+ if (!entry || typeof entry !== "object") continue;
1799
+ if (!SESSION_RUNTIME_FIELD_NAMES.some((field) => entry[field] !== void 0)) {
1800
+ side.delete(key);
1801
+ continue;
1802
+ }
1803
+ if (out === store) out = { ...store };
1804
+ const copy = { ...entry };
1805
+ const kept = { sessionId: entry.sessionId };
1806
+ for (const field of SESSION_RUNTIME_FIELD_NAMES) {
1807
+ if (copy[field] !== void 0) kept[field] = copy[field];
1808
+ delete copy[field];
1809
+ }
1810
+ out[key] = copy;
1811
+ if (typeof entry.updatedAt === "number" && entry.updatedAt < cutoff) side.delete(key);
1812
+ else side.set(key, kept);
1813
+ }
1814
+ for (const key of side.keys()) if (!store[key]) side.delete(key);
1815
+ if (side.size > 0) SESSION_RUNTIME_FIELDS.set(storePath, side);
1816
+ else SESSION_RUNTIME_FIELDS.delete(storePath);
1817
+ return out;
1818
+ }
1758
1819
  function loadSessionStore(storePath, opts = {}) {
1759
1820
  if (!opts.skipCache && isSessionStoreCacheEnabled()) {
1760
1821
  const currentFileStat = getFileStatSnapshot(storePath);
@@ -1764,7 +1825,7 @@ function loadSessionStore(storePath, opts = {}) {
1764
1825
  mtimeMs: currentFileStat?.mtimeMs,
1765
1826
  sizeBytes: currentFileStat?.sizeBytes
1766
1827
  });
1767
- if (cached) return cached;
1828
+ if (cached) return attachSessionRuntimeFields(storePath, cached);
1768
1829
  }
1769
1830
  let store = {};
1770
1831
  let fileStat = getFileStatSnapshot(storePath);
@@ -1795,6 +1856,7 @@ function loadSessionStore(storePath, opts = {}) {
1795
1856
  if (serializedFromDisk !== void 0) setSerializedSessionStore(storePath, serializedFromDisk);
1796
1857
  else setSerializedSessionStore(storePath, void 0);
1797
1858
  applySessionStoreMigrations(store);
1859
+ stripSessionRuntimeFields(store);
1798
1860
  if (!opts.skipCache && isSessionStoreCacheEnabled()) writeSessionStoreCache({
1799
1861
  storePath,
1800
1862
  store,
@@ -1802,7 +1864,8 @@ function loadSessionStore(storePath, opts = {}) {
1802
1864
  sizeBytes: fileStat?.sizeBytes,
1803
1865
  serialized: serializedFromDisk
1804
1866
  });
1805
- return structuredClone(store);
1867
+ // JOHNNESS_PATCH_SESSION_STORE_MEMORY_81: `store` was parsed just now and the cache holds its own clone.
1868
+ return attachSessionRuntimeFields(storePath, store);
1806
1869
  }
1807
1870
  function readSessionUpdatedAt(params) {
1808
1871
  try {
@@ -1876,10 +1939,10 @@ async function saveSessionStoreUnlocked(storePath, store, opts) {
1876
1939
  });
1877
1940
  } else {
1878
1941
  const removedSessionFiles = /* @__PURE__ */ new Map();
1879
- const pruned = pruneStaleEntries(store, maintenance.pruneAfterMs, { onPruned: ({ entry }) => {
1942
+ const pruned = pruneStaleEntries(store, maintenance.pruneAfterMs, { activeSessionKey: opts?.activeSessionKey, onPruned: ({ entry }) => {
1880
1943
  rememberRemovedSessionFile(removedSessionFiles, entry);
1881
1944
  } });
1882
- const capped = capEntryCount(store, maintenance.maxEntries, { onCapped: ({ entry }) => {
1945
+ const capped = capEntryCount(store, maintenance.maxEntries, { activeSessionKey: opts?.activeSessionKey, onCapped: ({ entry }) => {
1883
1946
  rememberRemovedSessionFile(removedSessionFiles, entry);
1884
1947
  } });
1885
1948
  const archivedDirs = /* @__PURE__ */ new Set();
@@ -1924,7 +1987,9 @@ async function saveSessionStoreUnlocked(storePath, store, opts) {
1924
1987
  }
1925
1988
  }
1926
1989
  await fs.promises.mkdir(path.dirname(storePath), { recursive: true });
1927
- const json = JSON.stringify(store, null, 2);
1990
+ // JOHNNESS_PATCH_SESSION_STORE_MEMORY_81: persist without runtime fields, compact (was 2-space indent).
1991
+ store = persistableSessionStore(storePath, store);
1992
+ const json = JSON.stringify(store);
1928
1993
  if (getSerializedSessionStore(storePath) === json) {
1929
1994
  updateSessionStoreWriteCaches({
1930
1995
  storePath,
@@ -1976,6 +2041,9 @@ async function saveSessionStoreUnlocked(storePath, store, opts) {
1976
2041
  }
1977
2042
  async function updateSessionStore(storePath, mutator, opts) {
1978
2043
  return await withSessionStoreLock(storePath, async () => {
2044
+ // JOHNNESS_PATCH_SESSION_STORE_MEMORY_81: skipCache stays. This runs under the cross-process file lock and the cache is
2045
+ // validated only by mtime + size, so a same-size write by another process could be missed.
2046
+ // The re-read is ~3 MB now that runtime fields are not persisted.
1979
2047
  const store = loadSessionStore(storePath, { skipCache: true });
1980
2048
  const result = await mutator(store);
1981
2049
  await saveSessionStoreUnlocked(storePath, store, opts);