chat-recall 0.7.47 → 0.7.49
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 +52 -10
- package/dist/mcp.js +52 -10
- package/dist/scan-pool.d.ts +18 -2
- package/dist/scan-pool.d.ts.map +1 -1
- package/dist/scan-pool.js +73 -10
- package/dist/scan-pool.js.map +1 -1
- package/dist/watch.js +52 -10
- package/package.json +1 -1
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) ||
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
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) ||
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
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
|
};
|
package/dist/scan-pool.d.ts
CHANGED
|
@@ -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
|
|
10
|
+
/** Version of the rule pack each worker generation has installed. */
|
|
10
11
|
private packSent;
|
|
11
|
-
|
|
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.
|
package/dist/scan-pool.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scan-pool.d.ts","sourceRoot":"","sources":["../src/scan-pool.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"scan-pool.d.ts","sourceRoot":"","sources":["../src/scan-pool.ts"],"names":[],"mappings":"AAqCA,OAAO,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAmEzF,qBAAa,QAAQ;IAWP,OAAO,CAAC,QAAQ,CAAC,IAAI;IAAe,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC;IAVhF,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,qEAAqE;IACrE,OAAO,CAAC,QAAQ,CAA6B;IAC7C,2EAA2E;IAC3E,OAAO,CAAC,GAAG,CAAK;gBAEa,IAAI,SAAa,EAAmB,cAAc,CAAC,EAAE,MAAM,YAAA;IAIxF,qDAAqD;IACrD,IAAI,OAAO,IAAI,OAAO,CAA2B;IAEjD,8EAA8E;IAC9E,IAAI,gBAAgB,IAAI,MAAM,CAAqB;IAEnD,OAAO,CAAC,KAAK;IAQb,yEAAyE;IACzE,OAAO,CAAC,SAAS;IA0DjB;;;;;;;OAOG;IACH,OAAO,CAAC,WAAW;IAQnB,OAAO,CAAC,IAAI;IAWZ;;;;;;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;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,6 +22,14 @@
|
|
|
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';
|
|
@@ -36,7 +44,17 @@ import { cpus } from 'node:os';
|
|
|
36
44
|
* cgroup. A worker that needs more than 256 MB for one session dies on its
|
|
37
45
|
* limit, and the pool already fails that task and the caller runs it inline.
|
|
38
46
|
*/
|
|
39
|
-
const WORKER_HEAP_MB = Number(process.env.CHAT_RECALL_SCAN_WORKER_HEAP_MB) ||
|
|
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
|
+
})();
|
|
40
58
|
/**
|
|
41
59
|
* How many workers.
|
|
42
60
|
*
|
|
@@ -73,30 +91,47 @@ function workerScript() {
|
|
|
73
91
|
}
|
|
74
92
|
export class ScanPool {
|
|
75
93
|
size;
|
|
94
|
+
overrideScript;
|
|
76
95
|
slots = [];
|
|
77
96
|
queue = [];
|
|
78
97
|
nextId = 1;
|
|
79
98
|
started = false;
|
|
80
99
|
disabled = false;
|
|
81
|
-
/** Version of the rule pack each worker has installed
|
|
100
|
+
/** Version of the rule pack each worker generation has installed. */
|
|
82
101
|
packSent = new Map();
|
|
83
|
-
|
|
102
|
+
/** Counts worker generations: a fresh worker needs the rule pack again. */
|
|
103
|
+
gen = 0;
|
|
104
|
+
constructor(size = poolSize(), overrideScript) {
|
|
84
105
|
this.size = size;
|
|
106
|
+
this.overrideScript = overrideScript;
|
|
85
107
|
if (this.size < 1)
|
|
86
108
|
this.disabled = true;
|
|
87
109
|
}
|
|
88
110
|
/** True when tasks will actually run on a worker. */
|
|
89
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; }
|
|
90
114
|
start() {
|
|
91
115
|
if (this.started)
|
|
92
116
|
return;
|
|
93
117
|
this.started = true;
|
|
94
|
-
|
|
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();
|
|
95
130
|
if (!script) {
|
|
96
131
|
this.disabled = true;
|
|
97
132
|
return;
|
|
98
133
|
}
|
|
99
|
-
|
|
134
|
+
{
|
|
100
135
|
try {
|
|
101
136
|
const worker = new Worker(script, {
|
|
102
137
|
resourceLimits: { maxOldGenerationSizeMb: WORKER_HEAP_MB },
|
|
@@ -108,16 +143,20 @@ export class ScanPool {
|
|
|
108
143
|
// (resourceLimits), so an empty list is both correct and insulating.
|
|
109
144
|
execArgv: [],
|
|
110
145
|
});
|
|
111
|
-
const slot = { worker, pending: null };
|
|
146
|
+
const slot = { worker, pending: null, tasks: 0, retiring: false };
|
|
147
|
+
this.gen += 1;
|
|
112
148
|
worker.on('message', (r) => {
|
|
113
149
|
const resolve = slot.pending;
|
|
114
150
|
slot.pending = null;
|
|
115
151
|
resolve?.(r);
|
|
152
|
+
this.retireIfDue(slot);
|
|
116
153
|
this.pump();
|
|
117
154
|
});
|
|
118
155
|
// A dead worker must not strand its caller. Fail the task in flight so
|
|
119
156
|
// the caller falls back inline, and retire the slot.
|
|
120
157
|
const die = (err) => {
|
|
158
|
+
if (slot.retiring)
|
|
159
|
+
return;
|
|
121
160
|
const resolve = slot.pending;
|
|
122
161
|
slot.pending = null;
|
|
123
162
|
this.slots = this.slots.filter((s) => s !== slot);
|
|
@@ -149,14 +188,30 @@ export class ScanPool {
|
|
|
149
188
|
}
|
|
150
189
|
catch { /* one failure is enough to know: fall back inline */ }
|
|
151
190
|
}
|
|
152
|
-
|
|
153
|
-
|
|
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);
|
|
154
207
|
}
|
|
155
208
|
pump() {
|
|
156
209
|
if (this.queue.length === 0) {
|
|
157
210
|
this.refIfBusy();
|
|
158
211
|
return;
|
|
159
212
|
}
|
|
213
|
+
if (!this.disabled && this.slots.length === 0)
|
|
214
|
+
this.spawnSlot();
|
|
160
215
|
const slot = this.slots.find((s) => s.pending === null);
|
|
161
216
|
if (!slot) {
|
|
162
217
|
this.refIfBusy();
|
|
@@ -225,15 +280,23 @@ export class ScanPool {
|
|
|
225
280
|
* than a worker scanning with rules it never got.
|
|
226
281
|
*/
|
|
227
282
|
needsPack(version) {
|
|
228
|
-
|
|
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;
|
|
229
289
|
}
|
|
230
290
|
markPackSent(version) {
|
|
231
|
-
this.packSent.
|
|
291
|
+
this.packSent.clear();
|
|
292
|
+
this.packSent.set(this.gen, version);
|
|
232
293
|
}
|
|
233
294
|
async close() {
|
|
234
295
|
const slots = this.slots;
|
|
235
296
|
this.slots = [];
|
|
236
297
|
this.disabled = true;
|
|
298
|
+
for (const s of slots)
|
|
299
|
+
s.retiring = true;
|
|
237
300
|
await Promise.all(slots.map((s) => s.worker.terminate().catch(() => undefined)));
|
|
238
301
|
}
|
|
239
302
|
}
|
package/dist/scan-pool.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scan-pool.js","sourceRoot":"","sources":["../src/scan-pool.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"scan-pool.js","sourceRoot":"","sources":["../src/scan-pool.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;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;;;;;;;;GAQG;AACH,MAAM,cAAc,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,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SAAS,EAAE,CAAC;QAChE,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,OAAO,CAAC;IACjD,CAAC;IAED,YAAY,CAAC,OAAe;QAC1B,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;QACtB,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;IACvC,CAAC;IAED,KAAK,CAAC,KAAK;QACT,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;QACzB,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;QAChB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;QACrB,KAAK,MAAM,CAAC,IAAI,KAAK;YAAE,CAAC,CAAC,QAAQ,GAAG,IAAI,CAAC;QACzC,MAAM,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,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
|
@@ -28053,14 +28053,19 @@ function scanPool() {
|
|
|
28053
28053
|
if (!shared) shared = new ScanPool();
|
|
28054
28054
|
return shared;
|
|
28055
28055
|
}
|
|
28056
|
-
var WORKER_HEAP_MB, ScanPool, shared;
|
|
28056
|
+
var WORKER_HEAP_MB, MAX_TASKS_PER_WORKER, ScanPool, shared;
|
|
28057
28057
|
var init_scan_pool = __esm({
|
|
28058
28058
|
"src/scan-pool.ts"() {
|
|
28059
28059
|
"use strict";
|
|
28060
|
-
WORKER_HEAP_MB = Number(process.env.CHAT_RECALL_SCAN_WORKER_HEAP_MB) ||
|
|
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
|
+
})();
|
|
28061
28065
|
ScanPool = class {
|
|
28062
|
-
constructor(size = poolSize()) {
|
|
28066
|
+
constructor(size = poolSize(), overrideScript) {
|
|
28063
28067
|
this.size = size;
|
|
28068
|
+
this.overrideScript = overrideScript;
|
|
28064
28069
|
if (this.size < 1) this.disabled = true;
|
|
28065
28070
|
}
|
|
28066
28071
|
slots = [];
|
|
@@ -28068,21 +28073,36 @@ var init_scan_pool = __esm({
|
|
|
28068
28073
|
nextId = 1;
|
|
28069
28074
|
started = false;
|
|
28070
28075
|
disabled = false;
|
|
28071
|
-
/** Version of the rule pack each worker has installed
|
|
28076
|
+
/** Version of the rule pack each worker generation has installed. */
|
|
28072
28077
|
packSent = /* @__PURE__ */ new Map();
|
|
28078
|
+
/** Counts worker generations: a fresh worker needs the rule pack again. */
|
|
28079
|
+
gen = 0;
|
|
28073
28080
|
/** True when tasks will actually run on a worker. */
|
|
28074
28081
|
get enabled() {
|
|
28075
28082
|
return !this.disabled;
|
|
28076
28083
|
}
|
|
28084
|
+
/** How many workers this pool has started. Tests read it to see a recycle. */
|
|
28085
|
+
get workerGeneration() {
|
|
28086
|
+
return this.gen;
|
|
28087
|
+
}
|
|
28077
28088
|
start() {
|
|
28078
28089
|
if (this.started) return;
|
|
28079
28090
|
this.started = true;
|
|
28080
|
-
|
|
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();
|
|
28081
28101
|
if (!script) {
|
|
28082
28102
|
this.disabled = true;
|
|
28083
28103
|
return;
|
|
28084
28104
|
}
|
|
28085
|
-
|
|
28105
|
+
{
|
|
28086
28106
|
try {
|
|
28087
28107
|
const worker = new Worker(script, {
|
|
28088
28108
|
resourceLimits: { maxOldGenerationSizeMb: WORKER_HEAP_MB },
|
|
@@ -28094,14 +28114,17 @@ var init_scan_pool = __esm({
|
|
|
28094
28114
|
// (resourceLimits), so an empty list is both correct and insulating.
|
|
28095
28115
|
execArgv: []
|
|
28096
28116
|
});
|
|
28097
|
-
const slot = { worker, pending: null };
|
|
28117
|
+
const slot = { worker, pending: null, tasks: 0, retiring: false };
|
|
28118
|
+
this.gen += 1;
|
|
28098
28119
|
worker.on("message", (r) => {
|
|
28099
28120
|
const resolve3 = slot.pending;
|
|
28100
28121
|
slot.pending = null;
|
|
28101
28122
|
resolve3?.(r);
|
|
28123
|
+
this.retireIfDue(slot);
|
|
28102
28124
|
this.pump();
|
|
28103
28125
|
});
|
|
28104
28126
|
const die = (err) => {
|
|
28127
|
+
if (slot.retiring) return;
|
|
28105
28128
|
const resolve3 = slot.pending;
|
|
28106
28129
|
slot.pending = null;
|
|
28107
28130
|
this.slots = this.slots.filter((s) => s !== slot);
|
|
@@ -28118,13 +28141,28 @@ var init_scan_pool = __esm({
|
|
|
28118
28141
|
} catch {
|
|
28119
28142
|
}
|
|
28120
28143
|
}
|
|
28121
|
-
|
|
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);
|
|
28122
28159
|
}
|
|
28123
28160
|
pump() {
|
|
28124
28161
|
if (this.queue.length === 0) {
|
|
28125
28162
|
this.refIfBusy();
|
|
28126
28163
|
return;
|
|
28127
28164
|
}
|
|
28165
|
+
if (!this.disabled && this.slots.length === 0) this.spawnSlot();
|
|
28128
28166
|
const slot = this.slots.find((s) => s.pending === null);
|
|
28129
28167
|
if (!slot) {
|
|
28130
28168
|
this.refIfBusy();
|
|
@@ -28189,15 +28227,19 @@ var init_scan_pool = __esm({
|
|
|
28189
28227
|
* than a worker scanning with rules it never got.
|
|
28190
28228
|
*/
|
|
28191
28229
|
needsPack(version) {
|
|
28192
|
-
|
|
28230
|
+
this.start();
|
|
28231
|
+
if (!this.disabled && this.slots.length === 0) this.spawnSlot();
|
|
28232
|
+
return this.packSent.get(this.gen) !== version;
|
|
28193
28233
|
}
|
|
28194
28234
|
markPackSent(version) {
|
|
28195
|
-
this.packSent.
|
|
28235
|
+
this.packSent.clear();
|
|
28236
|
+
this.packSent.set(this.gen, version);
|
|
28196
28237
|
}
|
|
28197
28238
|
async close() {
|
|
28198
28239
|
const slots = this.slots;
|
|
28199
28240
|
this.slots = [];
|
|
28200
28241
|
this.disabled = true;
|
|
28242
|
+
for (const s of slots) s.retiring = true;
|
|
28201
28243
|
await Promise.all(slots.map((s) => s.worker.terminate().catch(() => void 0)));
|
|
28202
28244
|
}
|
|
28203
28245
|
};
|