chat-recall 0.7.46 → 0.7.48

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/cli.js CHANGED
@@ -28575,14 +28575,19 @@ function scanPool() {
28575
28575
  if (!shared) shared = new ScanPool();
28576
28576
  return shared;
28577
28577
  }
28578
- var WORKER_HEAP_MB, ScanPool, shared;
28578
+ var WORKER_HEAP_MB, MAX_TASKS_PER_WORKER, ScanPool, shared;
28579
28579
  var init_scan_pool = __esm({
28580
28580
  "src/scan-pool.ts"() {
28581
28581
  "use strict";
28582
- WORKER_HEAP_MB = Number(process.env.CHAT_RECALL_SCAN_WORKER_HEAP_MB) || 768;
28582
+ WORKER_HEAP_MB = Number(process.env.CHAT_RECALL_SCAN_WORKER_HEAP_MB) || 128;
28583
+ MAX_TASKS_PER_WORKER = (() => {
28584
+ const raw = Number(process.env.CHAT_RECALL_SCAN_TASKS_PER_WORKER);
28585
+ return Number.isFinite(raw) && raw >= 1 ? Math.floor(raw) : 1;
28586
+ })();
28583
28587
  ScanPool = class {
28584
- constructor(size = poolSize()) {
28588
+ constructor(size = poolSize(), overrideScript) {
28585
28589
  this.size = size;
28590
+ this.overrideScript = overrideScript;
28586
28591
  if (this.size < 1) this.disabled = true;
28587
28592
  }
28588
28593
  slots = [];
@@ -28590,21 +28595,36 @@ var init_scan_pool = __esm({
28590
28595
  nextId = 1;
28591
28596
  started = false;
28592
28597
  disabled = false;
28593
- /** Version of the rule pack each worker has installed, by slot index. */
28598
+ /** Version of the rule pack each worker generation has installed. */
28594
28599
  packSent = /* @__PURE__ */ new Map();
28600
+ /** Counts worker generations: a fresh worker needs the rule pack again. */
28601
+ gen = 0;
28595
28602
  /** True when tasks will actually run on a worker. */
28596
28603
  get enabled() {
28597
28604
  return !this.disabled;
28598
28605
  }
28606
+ /** How many workers this pool has started. Tests read it to see a recycle. */
28607
+ get workerGeneration() {
28608
+ return this.gen;
28609
+ }
28599
28610
  start() {
28600
28611
  if (this.started) return;
28601
28612
  this.started = true;
28602
- const script = workerScript();
28613
+ if (this.size < 1) {
28614
+ this.disabled = true;
28615
+ return;
28616
+ }
28617
+ for (let i = 0; i < this.size; i++) this.spawnSlot();
28618
+ if (this.slots.length === 0) this.disabled = true;
28619
+ }
28620
+ /** Start one worker, or leave the pool disabled when it cannot start. */
28621
+ spawnSlot() {
28622
+ const script = this.overrideScript ?? workerScript();
28603
28623
  if (!script) {
28604
28624
  this.disabled = true;
28605
28625
  return;
28606
28626
  }
28607
- for (let i = 0; i < this.size; i++) {
28627
+ {
28608
28628
  try {
28609
28629
  const worker = new Worker(script, {
28610
28630
  resourceLimits: { maxOldGenerationSizeMb: WORKER_HEAP_MB },
@@ -28616,14 +28636,17 @@ var init_scan_pool = __esm({
28616
28636
  // (resourceLimits), so an empty list is both correct and insulating.
28617
28637
  execArgv: []
28618
28638
  });
28619
- const slot = { worker, pending: null };
28639
+ const slot = { worker, pending: null, tasks: 0, retiring: false };
28640
+ this.gen += 1;
28620
28641
  worker.on("message", (r) => {
28621
28642
  const resolve3 = slot.pending;
28622
28643
  slot.pending = null;
28623
28644
  resolve3?.(r);
28645
+ this.retireIfDue(slot);
28624
28646
  this.pump();
28625
28647
  });
28626
28648
  const die = (err) => {
28649
+ if (slot.retiring) return;
28627
28650
  const resolve3 = slot.pending;
28628
28651
  slot.pending = null;
28629
28652
  this.slots = this.slots.filter((s) => s !== slot);
@@ -28640,13 +28663,28 @@ var init_scan_pool = __esm({
28640
28663
  } catch {
28641
28664
  }
28642
28665
  }
28643
- if (this.slots.length === 0) this.disabled = true;
28666
+ }
28667
+ /**
28668
+ * End a worker that has served its tasks, and take the slot out of the pool.
28669
+ *
28670
+ * `terminate()` returns the isolate's committed pages to the operating
28671
+ * system, which is the point of the whole exercise. The exit that follows is
28672
+ * ours, so `die` ignores it. The next task starts a fresh worker, and
28673
+ * `needsPack` then asks the caller for the rule pack again.
28674
+ */
28675
+ retireIfDue(slot) {
28676
+ slot.tasks += 1;
28677
+ if (slot.tasks < MAX_TASKS_PER_WORKER) return;
28678
+ slot.retiring = true;
28679
+ this.slots = this.slots.filter((s) => s !== slot);
28680
+ void slot.worker.terminate().catch(() => void 0);
28644
28681
  }
28645
28682
  pump() {
28646
28683
  if (this.queue.length === 0) {
28647
28684
  this.refIfBusy();
28648
28685
  return;
28649
28686
  }
28687
+ if (!this.disabled && this.slots.length === 0) this.spawnSlot();
28650
28688
  const slot = this.slots.find((s) => s.pending === null);
28651
28689
  if (!slot) {
28652
28690
  this.refIfBusy();
@@ -28711,15 +28749,19 @@ var init_scan_pool = __esm({
28711
28749
  * than a worker scanning with rules it never got.
28712
28750
  */
28713
28751
  needsPack(version2) {
28714
- return this.packSent.get(0) !== version2;
28752
+ this.start();
28753
+ if (!this.disabled && this.slots.length === 0) this.spawnSlot();
28754
+ return this.packSent.get(this.gen) !== version2;
28715
28755
  }
28716
28756
  markPackSent(version2) {
28717
- this.packSent.set(0, version2);
28757
+ this.packSent.clear();
28758
+ this.packSent.set(this.gen, version2);
28718
28759
  }
28719
28760
  async close() {
28720
28761
  const slots = this.slots;
28721
28762
  this.slots = [];
28722
28763
  this.disabled = true;
28764
+ for (const s of slots) s.retiring = true;
28723
28765
  await Promise.all(slots.map((s) => s.worker.terminate().catch(() => void 0)));
28724
28766
  }
28725
28767
  };
package/dist/mcp.js CHANGED
@@ -29542,14 +29542,19 @@ function scanPool() {
29542
29542
  if (!shared) shared = new ScanPool();
29543
29543
  return shared;
29544
29544
  }
29545
- var WORKER_HEAP_MB, ScanPool, shared;
29545
+ var WORKER_HEAP_MB, MAX_TASKS_PER_WORKER, ScanPool, shared;
29546
29546
  var init_scan_pool = __esm({
29547
29547
  "src/scan-pool.ts"() {
29548
29548
  "use strict";
29549
- WORKER_HEAP_MB = Number(process.env.CHAT_RECALL_SCAN_WORKER_HEAP_MB) || 768;
29549
+ WORKER_HEAP_MB = Number(process.env.CHAT_RECALL_SCAN_WORKER_HEAP_MB) || 128;
29550
+ MAX_TASKS_PER_WORKER = (() => {
29551
+ const raw = Number(process.env.CHAT_RECALL_SCAN_TASKS_PER_WORKER);
29552
+ return Number.isFinite(raw) && raw >= 1 ? Math.floor(raw) : 1;
29553
+ })();
29550
29554
  ScanPool = class {
29551
- constructor(size = poolSize()) {
29555
+ constructor(size = poolSize(), overrideScript) {
29552
29556
  this.size = size;
29557
+ this.overrideScript = overrideScript;
29553
29558
  if (this.size < 1) this.disabled = true;
29554
29559
  }
29555
29560
  slots = [];
@@ -29557,21 +29562,36 @@ var init_scan_pool = __esm({
29557
29562
  nextId = 1;
29558
29563
  started = false;
29559
29564
  disabled = false;
29560
- /** Version of the rule pack each worker has installed, by slot index. */
29565
+ /** Version of the rule pack each worker generation has installed. */
29561
29566
  packSent = /* @__PURE__ */ new Map();
29567
+ /** Counts worker generations: a fresh worker needs the rule pack again. */
29568
+ gen = 0;
29562
29569
  /** True when tasks will actually run on a worker. */
29563
29570
  get enabled() {
29564
29571
  return !this.disabled;
29565
29572
  }
29573
+ /** How many workers this pool has started. Tests read it to see a recycle. */
29574
+ get workerGeneration() {
29575
+ return this.gen;
29576
+ }
29566
29577
  start() {
29567
29578
  if (this.started) return;
29568
29579
  this.started = true;
29569
- const script = workerScript();
29580
+ if (this.size < 1) {
29581
+ this.disabled = true;
29582
+ return;
29583
+ }
29584
+ for (let i = 0; i < this.size; i++) this.spawnSlot();
29585
+ if (this.slots.length === 0) this.disabled = true;
29586
+ }
29587
+ /** Start one worker, or leave the pool disabled when it cannot start. */
29588
+ spawnSlot() {
29589
+ const script = this.overrideScript ?? workerScript();
29570
29590
  if (!script) {
29571
29591
  this.disabled = true;
29572
29592
  return;
29573
29593
  }
29574
- for (let i = 0; i < this.size; i++) {
29594
+ {
29575
29595
  try {
29576
29596
  const worker = new Worker(script, {
29577
29597
  resourceLimits: { maxOldGenerationSizeMb: WORKER_HEAP_MB },
@@ -29583,14 +29603,17 @@ var init_scan_pool = __esm({
29583
29603
  // (resourceLimits), so an empty list is both correct and insulating.
29584
29604
  execArgv: []
29585
29605
  });
29586
- const slot = { worker, pending: null };
29606
+ const slot = { worker, pending: null, tasks: 0, retiring: false };
29607
+ this.gen += 1;
29587
29608
  worker.on("message", (r) => {
29588
29609
  const resolve3 = slot.pending;
29589
29610
  slot.pending = null;
29590
29611
  resolve3?.(r);
29612
+ this.retireIfDue(slot);
29591
29613
  this.pump();
29592
29614
  });
29593
29615
  const die = (err) => {
29616
+ if (slot.retiring) return;
29594
29617
  const resolve3 = slot.pending;
29595
29618
  slot.pending = null;
29596
29619
  this.slots = this.slots.filter((s) => s !== slot);
@@ -29607,13 +29630,28 @@ var init_scan_pool = __esm({
29607
29630
  } catch {
29608
29631
  }
29609
29632
  }
29610
- if (this.slots.length === 0) this.disabled = true;
29633
+ }
29634
+ /**
29635
+ * End a worker that has served its tasks, and take the slot out of the pool.
29636
+ *
29637
+ * `terminate()` returns the isolate's committed pages to the operating
29638
+ * system, which is the point of the whole exercise. The exit that follows is
29639
+ * ours, so `die` ignores it. The next task starts a fresh worker, and
29640
+ * `needsPack` then asks the caller for the rule pack again.
29641
+ */
29642
+ retireIfDue(slot) {
29643
+ slot.tasks += 1;
29644
+ if (slot.tasks < MAX_TASKS_PER_WORKER) return;
29645
+ slot.retiring = true;
29646
+ this.slots = this.slots.filter((s) => s !== slot);
29647
+ void slot.worker.terminate().catch(() => void 0);
29611
29648
  }
29612
29649
  pump() {
29613
29650
  if (this.queue.length === 0) {
29614
29651
  this.refIfBusy();
29615
29652
  return;
29616
29653
  }
29654
+ if (!this.disabled && this.slots.length === 0) this.spawnSlot();
29617
29655
  const slot = this.slots.find((s) => s.pending === null);
29618
29656
  if (!slot) {
29619
29657
  this.refIfBusy();
@@ -29678,15 +29716,19 @@ var init_scan_pool = __esm({
29678
29716
  * than a worker scanning with rules it never got.
29679
29717
  */
29680
29718
  needsPack(version2) {
29681
- return this.packSent.get(0) !== version2;
29719
+ this.start();
29720
+ if (!this.disabled && this.slots.length === 0) this.spawnSlot();
29721
+ return this.packSent.get(this.gen) !== version2;
29682
29722
  }
29683
29723
  markPackSent(version2) {
29684
- this.packSent.set(0, version2);
29724
+ this.packSent.clear();
29725
+ this.packSent.set(this.gen, version2);
29685
29726
  }
29686
29727
  async close() {
29687
29728
  const slots = this.slots;
29688
29729
  this.slots = [];
29689
29730
  this.disabled = true;
29731
+ for (const s of slots) s.retiring = true;
29690
29732
  await Promise.all(slots.map((s) => s.worker.terminate().catch(() => void 0)));
29691
29733
  }
29692
29734
  };
@@ -36,6 +36,17 @@ export declare function memoryStatMb(dir: string, key: string): number | null;
36
36
  * anyway, so this only makes the peak smaller sooner.
37
37
  */
38
38
  export declare function reclaimCgroup(keepFileMb?: number, dir?: string | null): boolean;
39
+ /** The page cache the resident tick leaves in place, in MiB. */
40
+ export declare const DEFAULT_RECLAIM_FILE_MB = 128;
41
+ /**
42
+ * True when the cgroup holds more reclaimable file pages than the ceiling.
43
+ *
44
+ * The pass calls `reclaimCgroup` at its own boundaries, but the file pages keep
45
+ * growing between them: the transcripts are read, the watcher records inotify
46
+ * events, and every page stays in `memory.current` and in the unit peak. A tick
47
+ * every 30 s asks a pure decision here, so the peak tracks the live heap.
48
+ */
49
+ export declare function shouldReclaim(fileMb: number | null, limitMb?: number): boolean;
39
50
  /** `memory.current` of the cgroup in MiB, or null when it cannot be read. */
40
51
  export declare function cgroupCurrentMb(dir?: string | null): number | null;
41
52
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"memory-guard.d.ts","sourceRoot":"","sources":["../src/memory-guard.ts"],"names":[],"mappings":"AA6BA;;;GAGG;AACH,eAAO,MAAM,kBAAkB,MAAM,CAAC;AAEtC,yEAAyE;AACzE,eAAO,MAAM,qBAAqB,MAAM,CAAC;AAEzC,6CAA6C;AAC7C,wBAAgB,KAAK,IAAI,MAAM,CAE9B;AAED,+EAA+E;AAC/E,wBAAgB,aAAa,CAAC,GAAG,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAe,GAAG,MAAM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Ae,CAAC,GAAG,GAAE,MAAM,GAAG,IAAkB,GAAG,MAAM,GAAG,IAAI,CAM/E;AAED;;;;;;;;GAQG;AACH,wBAAgB,MAAM,CAAC,GAAG,SAAU,EAAE,MAAM,gBAAoB,GAAG,MAAM,CAExE;AAED,mGAAmG;AACnG,MAAM,WAAW,YAAY;IAC3B,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CACzB,IAAI,GAAE,MAAiB,EACvB,GAAG,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAe,GACpD,YAAY,CAEd;AAED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAG1F"}
@@ -127,6 +127,19 @@ export function reclaimCgroup(keepFileMb = 64, dir = cgroupDir()) {
127
127
  return false;
128
128
  }
129
129
  }
130
+ /** The page cache the resident tick leaves in place, in MiB. */
131
+ export const DEFAULT_RECLAIM_FILE_MB = 128;
132
+ /**
133
+ * True when the cgroup holds more reclaimable file pages than the ceiling.
134
+ *
135
+ * The pass calls `reclaimCgroup` at its own boundaries, but the file pages keep
136
+ * growing between them: the transcripts are read, the watcher records inotify
137
+ * events, and every page stays in `memory.current` and in the unit peak. A tick
138
+ * every 30 s asks a pure decision here, so the peak tracks the live heap.
139
+ */
140
+ export function shouldReclaim(fileMb, limitMb = DEFAULT_RECLAIM_FILE_MB) {
141
+ return fileMb !== null && limitMb >= 0 && fileMb > limitMb;
142
+ }
130
143
  /** `memory.current` of the cgroup in MiB, or null when it cannot be read. */
131
144
  export function cgroupCurrentMb(dir = cgroupDir()) {
132
145
  if (!dir)
@@ -1 +1 @@
1
- {"version":3,"file":"memory-guard.js","sourceRoot":"","sources":["../src/memory-guard.ts"],"names":[],"mapp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C,GAAG,GAAG,KAAK,EAAE,EAAE,MAAM,GAAG,eAAe,EAAE;IAC9D,OAAO,MAAM,KAAK,IAAI,IAAI,MAAM,GAAG,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC;AACxD,CAAC;AASD;;;;;;;GAOG;AACH,MAAM,UAAU,WAAW,CACzB,OAAe,MAAM,EAAE,EACvB,MAA0C,OAAO,CAAC,GAAG;IAErD,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,QAAQ,EAAE,aAAa,CAAC,GAAG,CAAC,EAAE,MAAM,EAAE,aAAa,CAAC,GAAG,CAAC,EAAE,CAAC;AACxF,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,WAAW,CAAC,IAAY,EAAE,MAAoB,EAAE,SAAiB;IAC/E,IAAI,SAAS,IAAI,CAAC;QAAE,OAAO,KAAK,CAAC;IACjC,OAAO,IAAI,IAAI,MAAM,CAAC,MAAM,IAAI,IAAI,GAAG,MAAM,CAAC,UAAU,IAAI,MAAM,CAAC,QAAQ,CAAC;AAC9E,CAAC"}
@@ -1,17 +1,33 @@
1
1
  import type { ScanTask, ScanResult, DerivedTask, DerivedResult } from './scan-worker.js';
2
2
  export declare class ScanPool {
3
3
  private readonly size;
4
+ private readonly overrideScript?;
4
5
  private slots;
5
6
  private queue;
6
7
  private nextId;
7
8
  private started;
8
9
  private disabled;
9
- /** Version of the rule pack each worker has installed, by slot index. */
10
+ /** Version of the rule pack each worker generation has installed. */
10
11
  private packSent;
11
- constructor(size?: number);
12
+ /** Counts worker generations: a fresh worker needs the rule pack again. */
13
+ private gen;
14
+ constructor(size?: number, overrideScript?: string | undefined);
12
15
  /** True when tasks will actually run on a worker. */
13
16
  get enabled(): boolean;
17
+ /** How many workers this pool has started. Tests read it to see a recycle. */
18
+ get workerGeneration(): number;
14
19
  private start;
20
+ /** Start one worker, or leave the pool disabled when it cannot start. */
21
+ private spawnSlot;
22
+ /**
23
+ * End a worker that has served its tasks, and take the slot out of the pool.
24
+ *
25
+ * `terminate()` returns the isolate's committed pages to the operating
26
+ * system, which is the point of the whole exercise. The exit that follows is
27
+ * ours, so `die` ignores it. The next task starts a fresh worker, and
28
+ * `needsPack` then asks the caller for the rule pack again.
29
+ */
30
+ private retireIfDue;
15
31
  private pump;
16
32
  /**
17
33
  * Keep the process alive exactly while the pool owes someone an answer.
@@ -1 +1 @@
1
- {"version":3,"file":"scan-pool.d.ts","sourceRoot":"","sources":["../src/scan-pool.ts"],"names":[],"mappings":"AA6BA,OAAO,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AA4CzF,qBAAa,QAAQ;IASP,OAAO,CAAC,QAAQ,CAAC,IAAI;IARjC,OAAO,CAAC,KAAK,CAAc;IAC3B,OAAO,CAAC,KAAK,CAAiG;IAC9G,OAAO,CAAC,MAAM,CAAK;IACnB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,QAAQ,CAAS;IACzB,yEAAyE;IACzE,OAAO,CAAC,QAAQ,CAA6B;gBAEhB,IAAI,SAAa;IAI9C,qDAAqD;IACrD,IAAI,OAAO,IAAI,OAAO,CAA2B;IAEjD,OAAO,CAAC,KAAK;IA0Db,OAAO,CAAC,IAAI;IAUZ;;;;;;OAMG;IACH,OAAO,CAAC,SAAS;IAOjB;;;;;;OAMG;IACG,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,GAAG,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC;IAIjE;;;;;OAKG;IACG,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,WAAW,EAAE,IAAI,GAAG,MAAM,CAAC,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC;YAIzE,QAAQ;IAYtB;;;;;;;;OAQG;IACH,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAInC,YAAY,CAAC,OAAO,EAAE,MAAM,GA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EAAE,IAAI,GAAG,MAAM,CAAC,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC;YAIzE,QAAQ;IAYtB;;;;;;;;OAQG;IACH,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAQnC,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAK7B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAO7B;AAID,wBAAgB,QAAQ,IAAI,QAAQ,CAGnC;AAED,4CAA4C;AAC5C,wBAAsB,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC,CAInD"}
package/dist/scan-pool.js CHANGED
@@ -22,13 +22,39 @@
22
22
  * so a pathological session kills one task instead of the daemon — the OOM
23
23
  * that started all of this was exactly a transcript-sized allocation with no
24
24
  * ceiling.
25
+ *
26
+ * 4. A WORKER IS RETIRED AFTER ITS TASK. A worker isolate commits the pages it
27
+ * touched and never returns them: measured 2026-11-13, the process held
28
+ * 471 MB of anonymous memory with the pool on and 293 MB with
29
+ * CHAT_RECALL_SCAN_WORKERS=0, while a heap snapshot of the main isolate held
30
+ * 56 MB of objects in total. Terminating the worker returns that memory to
31
+ * the operating system, and the next task starts on a fresh worker — a spawn
32
+ * takes milliseconds against a scan that does not.
25
33
  */
26
34
  import { Worker } from 'node:worker_threads';
27
35
  import { existsSync } from 'node:fs';
28
36
  import { fileURLToPath } from 'node:url';
29
37
  import { cpus } from 'node:os';
30
- /** Per-worker heap ceiling. One session's text plus its gzip, with headroom. */
31
- const WORKER_HEAP_MB = Number(process.env.CHAT_RECALL_SCAN_WORKER_HEAP_MB) || 768;
38
+ /**
39
+ * Per-worker heap ceiling. One session's text plus its gzip, with headroom.
40
+ *
41
+ * Measured on 2026-11-13: with the ceiling at 768 MB the watch unit reached
42
+ * 1.6 GiB of peak while the main isolate held 50 MB of heap and 537 MB of rss
43
+ * (report 14:57:24), and the scan worker was the only other isolate in the
44
+ * cgroup. A worker that needs more than 256 MB for one session dies on its
45
+ * limit, and the pool already fails that task and the caller runs it inline.
46
+ */
47
+ const WORKER_HEAP_MB = Number(process.env.CHAT_RECALL_SCAN_WORKER_HEAP_MB) || 128;
48
+ /**
49
+ * Tasks a worker serves before it is terminated.
50
+ *
51
+ * One by default: a worker that scanned a large session keeps that memory for
52
+ * its whole life (see constraint 4). A larger number trades memory for spawn
53
+ * time, and the environment can raise it. */
54
+ const MAX_TASKS_PER_WORKER = (() => {
55
+ const raw = Number(process.env.CHAT_RECALL_SCAN_TASKS_PER_WORKER);
56
+ return Number.isFinite(raw) && raw >= 1 ? Math.floor(raw) : 1;
57
+ })();
32
58
  /**
33
59
  * How many workers.
34
60
  *
@@ -65,30 +91,47 @@ function workerScript() {
65
91
  }
66
92
  export class ScanPool {
67
93
  size;
94
+ overrideScript;
68
95
  slots = [];
69
96
  queue = [];
70
97
  nextId = 1;
71
98
  started = false;
72
99
  disabled = false;
73
- /** Version of the rule pack each worker has installed, by slot index. */
100
+ /** Version of the rule pack each worker generation has installed. */
74
101
  packSent = new Map();
75
- constructor(size = poolSize()) {
102
+ /** Counts worker generations: a fresh worker needs the rule pack again. */
103
+ gen = 0;
104
+ constructor(size = poolSize(), overrideScript) {
76
105
  this.size = size;
106
+ this.overrideScript = overrideScript;
77
107
  if (this.size < 1)
78
108
  this.disabled = true;
79
109
  }
80
110
  /** True when tasks will actually run on a worker. */
81
111
  get enabled() { return !this.disabled; }
112
+ /** How many workers this pool has started. Tests read it to see a recycle. */
113
+ get workerGeneration() { return this.gen; }
82
114
  start() {
83
115
  if (this.started)
84
116
  return;
85
117
  this.started = true;
86
- const script = workerScript();
118
+ if (this.size < 1) {
119
+ this.disabled = true;
120
+ return;
121
+ }
122
+ for (let i = 0; i < this.size; i++)
123
+ this.spawnSlot();
124
+ if (this.slots.length === 0)
125
+ this.disabled = true;
126
+ }
127
+ /** Start one worker, or leave the pool disabled when it cannot start. */
128
+ spawnSlot() {
129
+ const script = this.overrideScript ?? workerScript();
87
130
  if (!script) {
88
131
  this.disabled = true;
89
132
  return;
90
133
  }
91
- for (let i = 0; i < this.size; i++) {
134
+ {
92
135
  try {
93
136
  const worker = new Worker(script, {
94
137
  resourceLimits: { maxOldGenerationSizeMb: WORKER_HEAP_MB },
@@ -100,16 +143,20 @@ export class ScanPool {
100
143
  // (resourceLimits), so an empty list is both correct and insulating.
101
144
  execArgv: [],
102
145
  });
103
- const slot = { worker, pending: null };
146
+ const slot = { worker, pending: null, tasks: 0, retiring: false };
147
+ this.gen += 1;
104
148
  worker.on('message', (r) => {
105
149
  const resolve = slot.pending;
106
150
  slot.pending = null;
107
151
  resolve?.(r);
152
+ this.retireIfDue(slot);
108
153
  this.pump();
109
154
  });
110
155
  // A dead worker must not strand its caller. Fail the task in flight so
111
156
  // the caller falls back inline, and retire the slot.
112
157
  const die = (err) => {
158
+ if (slot.retiring)
159
+ return;
113
160
  const resolve = slot.pending;
114
161
  slot.pending = null;
115
162
  this.slots = this.slots.filter((s) => s !== slot);
@@ -141,14 +188,30 @@ export class ScanPool {
141
188
  }
142
189
  catch { /* one failure is enough to know: fall back inline */ }
143
190
  }
144
- if (this.slots.length === 0)
145
- this.disabled = true;
191
+ }
192
+ /**
193
+ * End a worker that has served its tasks, and take the slot out of the pool.
194
+ *
195
+ * `terminate()` returns the isolate's committed pages to the operating
196
+ * system, which is the point of the whole exercise. The exit that follows is
197
+ * ours, so `die` ignores it. The next task starts a fresh worker, and
198
+ * `needsPack` then asks the caller for the rule pack again.
199
+ */
200
+ retireIfDue(slot) {
201
+ slot.tasks += 1;
202
+ if (slot.tasks < MAX_TASKS_PER_WORKER)
203
+ return;
204
+ slot.retiring = true;
205
+ this.slots = this.slots.filter((s) => s !== slot);
206
+ void slot.worker.terminate().catch(() => undefined);
146
207
  }
147
208
  pump() {
148
209
  if (this.queue.length === 0) {
149
210
  this.refIfBusy();
150
211
  return;
151
212
  }
213
+ if (!this.disabled && this.slots.length === 0)
214
+ this.spawnSlot();
152
215
  const slot = this.slots.find((s) => s.pending === null);
153
216
  if (!slot) {
154
217
  this.refIfBusy();
@@ -217,15 +280,23 @@ export class ScanPool {
217
280
  * than a worker scanning with rules it never got.
218
281
  */
219
282
  needsPack(version) {
220
- return this.packSent.get(0) !== version;
283
+ // A caller asks this before it dispatches, so the pool must be up now: a
284
+ // worker is only sent a pack it can install.
285
+ this.start();
286
+ if (!this.disabled && this.slots.length === 0)
287
+ this.spawnSlot();
288
+ return this.packSent.get(this.gen) !== version;
221
289
  }
222
290
  markPackSent(version) {
223
- this.packSent.set(0, version);
291
+ this.packSent.clear();
292
+ this.packSent.set(this.gen, version);
224
293
  }
225
294
  async close() {
226
295
  const slots = this.slots;
227
296
  this.slots = [];
228
297
  this.disabled = true;
298
+ for (const s of slots)
299
+ s.retiring = true;
229
300
  await Promise.all(slots.map((s) => s.worker.terminate().catch(() => undefined)));
230
301
  }
231
302
  }
@@ -1 +1 @@
1
- {"version":3,"file":"scan-pool.js","sourceRoot":"","sources":["../src/scan-pool.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAG/B,gFAAgF;AAChF,MAAM,cAAc,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,+BAA+B,CAAC,IAAI,GAAG,CAAC;AAElF;;;;;GAKG;AACH,SAAS,QAAQ;IACf,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,wBAAwB,CAAC;IACjD,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;QACtB,MAAM,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;QACtB,IAAI,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC;YAAE,OAAO,IA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CAAC,SAAS,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;IACnF,CAAC;CACF;AAED,6EAA6E;AAC7E,IAAI,MAAM,GAAoB,IAAI,CAAC;AACnC,MAAM,UAAU,QAAQ;IACtB,IAAI,CAAC,MAAM;QAAE,MAAM,GAAG,IAAI,QAAQ,EAAE,CAAC;IACrC,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,4CAA4C;AAC5C,MAAM,CAAC,KAAK,UAAU,aAAa;IACjC,MAAM,CAAC,GAAG,MAAM,CAAC;IACjB,MAAM,GAAG,IAAI,CAAC;IACd,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC;AACnB,CAAC"}
package/dist/watch.js CHANGED
@@ -13960,6 +13960,9 @@ function reclaimCgroup(keepFileMb = 64, dir = cgroupDir()) {
13960
13960
  return false;
13961
13961
  }
13962
13962
  }
13963
+ function shouldReclaim(fileMb, limitMb = DEFAULT_RECLAIM_FILE_MB) {
13964
+ return fileMb !== null && limitMb >= 0 && fileMb > limitMb;
13965
+ }
13963
13966
  function cgroupCurrentMb(dir = cgroupDir()) {
13964
13967
  if (!dir) return null;
13965
13968
  try {
@@ -13979,12 +13982,13 @@ function shouldPause(used, budget, unitsDone) {
13979
13982
  if (unitsDone <= 0) return false;
13980
13983
  return used >= budget.hardMb || used - budget.baselineMb >= budget.growthMb;
13981
13984
  }
13982
- var DEFAULT_RSS_MAX_MB, DEFAULT_GROWTH_MAX_MB;
13985
+ var DEFAULT_RSS_MAX_MB, DEFAULT_GROWTH_MAX_MB, DEFAULT_RECLAIM_FILE_MB;
13983
13986
  var init_memory_guard = __esm({
13984
13987
  "src/memory-guard.ts"() {
13985
13988
  "use strict";
13986
13989
  DEFAULT_RSS_MAX_MB = 896;
13987
13990
  DEFAULT_GROWTH_MAX_MB = 256;
13991
+ DEFAULT_RECLAIM_FILE_MB = 128;
13988
13992
  }
13989
13993
  });
13990
13994
 
@@ -28049,14 +28053,19 @@ function scanPool() {
28049
28053
  if (!shared) shared = new ScanPool();
28050
28054
  return shared;
28051
28055
  }
28052
- var WORKER_HEAP_MB, ScanPool, shared;
28056
+ var WORKER_HEAP_MB, MAX_TASKS_PER_WORKER, ScanPool, shared;
28053
28057
  var init_scan_pool = __esm({
28054
28058
  "src/scan-pool.ts"() {
28055
28059
  "use strict";
28056
- WORKER_HEAP_MB = Number(process.env.CHAT_RECALL_SCAN_WORKER_HEAP_MB) || 768;
28060
+ WORKER_HEAP_MB = Number(process.env.CHAT_RECALL_SCAN_WORKER_HEAP_MB) || 128;
28061
+ MAX_TASKS_PER_WORKER = (() => {
28062
+ const raw = Number(process.env.CHAT_RECALL_SCAN_TASKS_PER_WORKER);
28063
+ return Number.isFinite(raw) && raw >= 1 ? Math.floor(raw) : 1;
28064
+ })();
28057
28065
  ScanPool = class {
28058
- constructor(size = poolSize()) {
28066
+ constructor(size = poolSize(), overrideScript) {
28059
28067
  this.size = size;
28068
+ this.overrideScript = overrideScript;
28060
28069
  if (this.size < 1) this.disabled = true;
28061
28070
  }
28062
28071
  slots = [];
@@ -28064,21 +28073,36 @@ var init_scan_pool = __esm({
28064
28073
  nextId = 1;
28065
28074
  started = false;
28066
28075
  disabled = false;
28067
- /** Version of the rule pack each worker has installed, by slot index. */
28076
+ /** Version of the rule pack each worker generation has installed. */
28068
28077
  packSent = /* @__PURE__ */ new Map();
28078
+ /** Counts worker generations: a fresh worker needs the rule pack again. */
28079
+ gen = 0;
28069
28080
  /** True when tasks will actually run on a worker. */
28070
28081
  get enabled() {
28071
28082
  return !this.disabled;
28072
28083
  }
28084
+ /** How many workers this pool has started. Tests read it to see a recycle. */
28085
+ get workerGeneration() {
28086
+ return this.gen;
28087
+ }
28073
28088
  start() {
28074
28089
  if (this.started) return;
28075
28090
  this.started = true;
28076
- const script = workerScript();
28091
+ if (this.size < 1) {
28092
+ this.disabled = true;
28093
+ return;
28094
+ }
28095
+ for (let i = 0; i < this.size; i++) this.spawnSlot();
28096
+ if (this.slots.length === 0) this.disabled = true;
28097
+ }
28098
+ /** Start one worker, or leave the pool disabled when it cannot start. */
28099
+ spawnSlot() {
28100
+ const script = this.overrideScript ?? workerScript();
28077
28101
  if (!script) {
28078
28102
  this.disabled = true;
28079
28103
  return;
28080
28104
  }
28081
- for (let i = 0; i < this.size; i++) {
28105
+ {
28082
28106
  try {
28083
28107
  const worker = new Worker(script, {
28084
28108
  resourceLimits: { maxOldGenerationSizeMb: WORKER_HEAP_MB },
@@ -28090,14 +28114,17 @@ var init_scan_pool = __esm({
28090
28114
  // (resourceLimits), so an empty list is both correct and insulating.
28091
28115
  execArgv: []
28092
28116
  });
28093
- const slot = { worker, pending: null };
28117
+ const slot = { worker, pending: null, tasks: 0, retiring: false };
28118
+ this.gen += 1;
28094
28119
  worker.on("message", (r) => {
28095
28120
  const resolve3 = slot.pending;
28096
28121
  slot.pending = null;
28097
28122
  resolve3?.(r);
28123
+ this.retireIfDue(slot);
28098
28124
  this.pump();
28099
28125
  });
28100
28126
  const die = (err) => {
28127
+ if (slot.retiring) return;
28101
28128
  const resolve3 = slot.pending;
28102
28129
  slot.pending = null;
28103
28130
  this.slots = this.slots.filter((s) => s !== slot);
@@ -28114,13 +28141,28 @@ var init_scan_pool = __esm({
28114
28141
  } catch {
28115
28142
  }
28116
28143
  }
28117
- if (this.slots.length === 0) this.disabled = true;
28144
+ }
28145
+ /**
28146
+ * End a worker that has served its tasks, and take the slot out of the pool.
28147
+ *
28148
+ * `terminate()` returns the isolate's committed pages to the operating
28149
+ * system, which is the point of the whole exercise. The exit that follows is
28150
+ * ours, so `die` ignores it. The next task starts a fresh worker, and
28151
+ * `needsPack` then asks the caller for the rule pack again.
28152
+ */
28153
+ retireIfDue(slot) {
28154
+ slot.tasks += 1;
28155
+ if (slot.tasks < MAX_TASKS_PER_WORKER) return;
28156
+ slot.retiring = true;
28157
+ this.slots = this.slots.filter((s) => s !== slot);
28158
+ void slot.worker.terminate().catch(() => void 0);
28118
28159
  }
28119
28160
  pump() {
28120
28161
  if (this.queue.length === 0) {
28121
28162
  this.refIfBusy();
28122
28163
  return;
28123
28164
  }
28165
+ if (!this.disabled && this.slots.length === 0) this.spawnSlot();
28124
28166
  const slot = this.slots.find((s) => s.pending === null);
28125
28167
  if (!slot) {
28126
28168
  this.refIfBusy();
@@ -28185,15 +28227,19 @@ var init_scan_pool = __esm({
28185
28227
  * than a worker scanning with rules it never got.
28186
28228
  */
28187
28229
  needsPack(version) {
28188
- return this.packSent.get(0) !== version;
28230
+ this.start();
28231
+ if (!this.disabled && this.slots.length === 0) this.spawnSlot();
28232
+ return this.packSent.get(this.gen) !== version;
28189
28233
  }
28190
28234
  markPackSent(version) {
28191
- this.packSent.set(0, version);
28235
+ this.packSent.clear();
28236
+ this.packSent.set(this.gen, version);
28192
28237
  }
28193
28238
  async close() {
28194
28239
  const slots = this.slots;
28195
28240
  this.slots = [];
28196
28241
  this.disabled = true;
28242
+ for (const s of slots) s.retiring = true;
28197
28243
  await Promise.all(slots.map((s) => s.worker.terminate().catch(() => void 0)));
28198
28244
  }
28199
28245
  };
@@ -34956,6 +35002,15 @@ async function codeIndexTick() {
34956
35002
  codeIndexInFlight = false;
34957
35003
  }
34958
35004
  }
35005
+ function residentTick() {
35006
+ const dir = cgroupDir();
35007
+ const fileMb = dir ? memoryStatMb(dir, "file") : null;
35008
+ if (shouldReclaim(fileMb)) {
35009
+ reclaimCgroup(DEFAULT_RECLAIM_FILE_MB);
35010
+ daemonLog.info(`resident: returned the page cache above ${DEFAULT_RECLAIM_FILE_MB}MB (file=${fileMb}MB)`);
35011
+ }
35012
+ releaseMemory();
35013
+ }
34959
35014
  async function autoUpdateTick() {
34960
35015
  for (const cred of loadAllCredentials()) {
34961
35016
  const base3 = cred.serverUrl.replace(/\/+$/, "");
@@ -35276,6 +35331,8 @@ var init_indexer = __esm({
35276
35331
  }, 9e4);
35277
35332
  daemonLog.info(` Code intelligence: AUTO \u2014 discovers your repos & indexes them (every ${CODE_INDEX_MS / 6e4}m \xB7 ${CODE_INDEX_WINDOW_DAYS}d window \xB7 max ${CODE_INDEX_MAX})`);
35278
35333
  }
35334
+ setInterval(residentTick, 3e4).unref();
35335
+ setTimeout(residentTick, 45e3);
35279
35336
  try {
35280
35337
  const { refreshServiceDefinition: refreshServiceDefinition2 } = await Promise.resolve().then(() => (init_service_installer(), service_installer_exports));
35281
35338
  if (refreshServiceDefinition2()) daemonLog.info("service definition refreshed to this version");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chat-recall",
3
- "version": "0.7.46",
3
+ "version": "0.7.48",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/munhq/chat-recall.git"