pi-mega-compact 0.6.0 → 0.6.2

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.
@@ -718,14 +718,34 @@ export function addLesson(
718
718
  // file-backed memory caps a single entry at ~5k chars). We truncate content at
719
719
  // MEMORY_MAX_CHARS and evict the least-recently-referenced rows past
720
720
  // MEMORY_MAX_ROWS per repo via LRU. Both are SQLite-only (PREVENT-PI-004): no
721
- // file-backed memory is written anywhere.
721
+ // file-backed memory is written anywhere. Defaults are overridable via env
722
+ // (MEGACOMPACT_MEMORY_MAX_CHARS / MEGACOMPACT_MEMORY_MAX_ROWS).
722
723
  export const MEMORY_MAX_CHARS = 4000;
723
- export const MEMORY_MAX_ROWS = 200;
724
+ export const MEMORY_MAX_ROWS = 500;
725
+
726
+ /** Read an env override as a positive int, falling back to `fallback`. */
727
+ function envInt(name: string, fallback: number): number {
728
+ const v = process.env[name];
729
+ if (v == null || v === "") return fallback;
730
+ const n = Number(v);
731
+ return Number.isFinite(n) && n > 0 ? Math.floor(n) : fallback;
732
+ }
733
+
734
+ /** Effective per-entry char cap (env-overridable, default MEMORY_MAX_CHARS). */
735
+ export function memoryMaxChars(): number {
736
+ return envInt("MEGACOMPACT_MEMORY_MAX_CHARS", MEMORY_MAX_CHARS);
737
+ }
738
+
739
+ /** Effective per-repo row cap (env-overridable, default MEMORY_MAX_ROWS). */
740
+ export function memoryMaxRows(): number {
741
+ return envInt("MEGACOMPACT_MEMORY_MAX_ROWS", MEMORY_MAX_ROWS);
742
+ }
724
743
 
725
744
  /** Truncate memory content to the per-entry cap, preserving a trailing marker. */
726
745
  function capMemoryContent(content: string): string {
727
- if (content.length <= MEMORY_MAX_CHARS) return content;
728
- return content.slice(0, MEMORY_MAX_CHARS) + "…[truncated]";
746
+ const cap = memoryMaxChars();
747
+ if (content.length <= cap) return content;
748
+ return content.slice(0, cap) + "…[truncated]";
729
749
  }
730
750
 
731
751
  /**
@@ -736,6 +756,7 @@ function capMemoryContent(content: string): string {
736
756
  */
737
757
  function evictMemoryLru(repo: string | null, stateDir: string): void {
738
758
  const db = openStore(stateDir);
759
+ const maxRows = memoryMaxRows();
739
760
  // SQLite `= NULL` is never true, so the null-repo scope (memories are
740
761
  // stateDir-scoped when repo is null — the applyMemoryOps path) needs `IS NULL`.
741
762
  const where = repo == null ? "repo IS NULL" : "repo = ?";
@@ -743,7 +764,7 @@ function evictMemoryLru(repo: string | null, stateDir: string): void {
743
764
  ? db.prepare(`SELECT COUNT(*) AS n FROM memories WHERE ${where}`).get()
744
765
  : db.prepare(`SELECT COUNT(*) AS n FROM memories WHERE ${where}`).get(repo);
745
766
  const count = (countRow as { n: number }).n;
746
- const over = count - MEMORY_MAX_ROWS;
767
+ const over = count - maxRows;
747
768
  if (over <= 0) return;
748
769
  // Delete the `over` least-recently-used rows. ORDER BY the LRU key ASC, id ASC
749
770
  // (id ASC breaks ties deterministically — oldest created first). The `where`