chat-recall 0.7.41 → 0.7.43

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.
@@ -1,27 +1,3 @@
1
- /**
2
- * Memory bound for the watch daemon (issue #16).
3
- *
4
- * The daemon is a thin collector with no local index, and its cgroup peak
5
- * reached 1.5 GiB. The measurement of 2026-11-13 showed that no object holds
6
- * the memory: a heap snapshot at 415 MB of resident anon memory held 55 MB of
7
- * live objects. The memory is V8 pages that V8 committed during churn and did
8
- * not return, plus allocator arenas.
9
- *
10
- * Two sources of churn dominate:
11
- * 1. one full listing of the sessions costs about 100 MB, and the daemon
12
- * listed them two or three times on every sync tick;
13
- * 2. the code-intelligence pass collects one workspace at a time and never
14
- * stops, so it adds a workspace on top of an already-full heap.
15
- *
16
- * This module gives the two bounds that stop the growth:
17
- * - `isOverMemoryLimit` stops a heavy pass at a set resident size and lets the
18
- * next tick continue the work, so the memory stays under the bound by
19
- * construction;
20
- * - `releaseMemory` returns the pages after a heavy unit (one workspace, one
21
- * walk target) when the daemon runs with `--expose-gc`.
22
- *
23
- * The limit is `CHAT_RECALL_WATCH_RSS_MAX_MB`, in MiB of resident memory.
24
- */
25
1
  /** Resident-size bound of the daemon, in MiB. */
26
2
  export declare const DEFAULT_RSS_MAX_MB = 512;
27
3
  /** Resident size of this process, in MiB. */
@@ -38,4 +14,31 @@ export declare function isOverMemoryLimit(rss: number, limitMb: number): boolean
38
14
  * which the tests use and the log lines do not.
39
15
  */
40
16
  export declare function releaseMemory(gc?: (() => void) | undefined): boolean;
17
+ /** The cgroup v2 directory of this process, or null when it has none. */
18
+ export declare function cgroupDir(cgroupFile?: string): string | null;
19
+ /** One key of `memory.stat`, in MiB, or null when it cannot be read. */
20
+ export declare function memoryStatMb(dir: string, key: string): number | null;
21
+ /**
22
+ * Ask the kernel to take back the page cache of this cgroup, and keep a floor.
23
+ *
24
+ * The pages are reclaimable, but they count in `memory.current` and in the
25
+ * cgroup peak. After a code-intelligence pass over 50 workspaces the watch unit
26
+ * held 775 MB of file pages, and that page cache was most of a 1.4 GiB peak
27
+ * while the live heap was small. The write needs no privilege when the user owns
28
+ * the cgroup. A failure is not an error: the kernel reclaims under pressure
29
+ * anyway, so this only makes the peak smaller sooner.
30
+ */
31
+ export declare function reclaimCgroup(keepFileMb?: number, dir?: string | null): boolean;
32
+ /** `memory.current` of the cgroup in MiB, or null when it cannot be read. */
33
+ export declare function cgroupCurrentMb(dir?: string | null): number | null;
34
+ /**
35
+ * The memory this daemon holds, in MiB, as the bound must see it.
36
+ *
37
+ * `process.memoryUsage().rss` sees only the daemon's own pages. A code pass
38
+ * spawns the `codeindex` child in the same cgroup and reads whole workspaces,
39
+ * so the file pages and the child's pages count in the cgroup peak but not in
40
+ * the rss. This returns the larger of the two, so the pass pauses on the number
41
+ * the unit is killed for.
42
+ */
43
+ export declare function usedMb(rss?: number, cgroup?: number | null): number;
41
44
  //# sourceMappingURL=memory-guard.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"memory-guard.d.ts","sourceRoot":"","sources":["../src/memory-guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,iDAAiD;AACjD,eAAO,MAAM,kBAAkB,MAAM,CAAC;AAEtC,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,CAK3F;AAED,sDAAsD;AACtD,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAEvE;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,EAAE,GAAE,CAAC,MAAM,IAAI,CAAC,GAAG,SAAkD,GAAG,OAAO,CAI5G"}
1
+ {"version":3,"file":"memory-guard.d.ts","sourceRoot":"","sources":["../src/memory-guard.ts"],"names":[],"mappings":"AA6BA,iDAAiD;AACjD,eAAO,MAAM,kBAAkB,MAAM,CAAC;AAEtC,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,CAK3F;AAED,sDAAsD;AACtD,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAEvE;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,EAAE,GAAE,CAAC,MAAM,IAAI,CAAC,GAAG,SAAkD,GAAG,OAAO,CAI5G;AAED,yEAAyE;AACzE,wBAAgB,SAAS,CAAC,UAAU,SAAsB,GAAG,MAAM,GAAG,IAAI,CAMzE;AAED,wEAAwE;AACxE,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAQpE;AAED;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAAC,UAAU,SAAK,EAAE,GAAG,GAAE,MAAM,GAAG,IAAkB,GAAG,OAAO,CAOxF;AAED,6EAA6E;AAC7E,wBAAgB,eAAe,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"}
@@ -18,10 +18,14 @@
18
18
  * next tick continue the work, so the memory stays under the bound by
19
19
  * construction;
20
20
  * - `releaseMemory` returns the pages after a heavy unit (one workspace, one
21
- * walk target) when the daemon runs with `--expose-gc`.
21
+ * walk target) when the daemon runs with `--expose-gc`;
22
+ * - `reclaimCgroup` takes back the page cache of the cgroup when a heavy pass
23
+ * ends, because the page cache of the pass counts in the cgroup peak.
22
24
  *
23
25
  * The limit is `CHAT_RECALL_WATCH_RSS_MAX_MB`, in MiB of resident memory.
24
26
  */
27
+ import { readFileSync, writeFileSync } from 'node:fs';
28
+ import { join } from 'node:path';
25
29
  /** Resident-size bound of the daemon, in MiB. */
26
30
  export const DEFAULT_RSS_MAX_MB = 512;
27
31
  /** Resident size of this process, in MiB. */
@@ -53,4 +57,85 @@ export function releaseMemory(gc = globalThis.gc) {
53
57
  gc();
54
58
  return true;
55
59
  }
60
+ /** The cgroup v2 directory of this process, or null when it has none. */
61
+ export function cgroupDir(cgroupFile = '/proc/self/cgroup') {
62
+ let text;
63
+ try {
64
+ text = readFileSync(cgroupFile, 'utf8');
65
+ }
66
+ catch {
67
+ return null;
68
+ }
69
+ const line = text.split('\n').find((l) => l.startsWith('0::'));
70
+ if (!line)
71
+ return null;
72
+ return `/sys/fs/cgroup${line.slice(3).trim()}`;
73
+ }
74
+ /** One key of `memory.stat`, in MiB, or null when it cannot be read. */
75
+ export function memoryStatMb(dir, key) {
76
+ let text;
77
+ try {
78
+ text = readFileSync(join(dir, 'memory.stat'), 'utf8');
79
+ }
80
+ catch {
81
+ return null;
82
+ }
83
+ for (const line of text.split('\n')) {
84
+ const [name, value] = line.split(' ');
85
+ if (name === key && value)
86
+ return Math.round(Number(value) / 1048576);
87
+ }
88
+ return null;
89
+ }
90
+ /**
91
+ * Ask the kernel to take back the page cache of this cgroup, and keep a floor.
92
+ *
93
+ * The pages are reclaimable, but they count in `memory.current` and in the
94
+ * cgroup peak. After a code-intelligence pass over 50 workspaces the watch unit
95
+ * held 775 MB of file pages, and that page cache was most of a 1.4 GiB peak
96
+ * while the live heap was small. The write needs no privilege when the user owns
97
+ * the cgroup. A failure is not an error: the kernel reclaims under pressure
98
+ * anyway, so this only makes the peak smaller sooner.
99
+ */
100
+ export function reclaimCgroup(keepFileMb = 64, dir = cgroupDir()) {
101
+ if (!dir)
102
+ return false;
103
+ const fileMb = memoryStatMb(dir, 'file');
104
+ if (fileMb === null)
105
+ return false;
106
+ const reclaimMb = Math.floor(fileMb - keepFileMb);
107
+ if (reclaimMb <= 0)
108
+ return false;
109
+ try {
110
+ writeFileSync(join(dir, 'memory.reclaim'), `${reclaimMb}M`);
111
+ return true;
112
+ }
113
+ catch {
114
+ return false;
115
+ }
116
+ }
117
+ /** `memory.current` of the cgroup in MiB, or null when it cannot be read. */
118
+ export function cgroupCurrentMb(dir = cgroupDir()) {
119
+ if (!dir)
120
+ return null;
121
+ try {
122
+ const n = Number(readFileSync(join(dir, 'memory.current'), 'utf8').trim());
123
+ return Number.isFinite(n) ? Math.round(n / 1048576) : null;
124
+ }
125
+ catch {
126
+ return null;
127
+ }
128
+ }
129
+ /**
130
+ * The memory this daemon holds, in MiB, as the bound must see it.
131
+ *
132
+ * `process.memoryUsage().rss` sees only the daemon's own pages. A code pass
133
+ * spawns the `codeindex` child in the same cgroup and reads whole workspaces,
134
+ * so the file pages and the child's pages count in the cgroup peak but not in
135
+ * the rss. This returns the larger of the two, so the pass pauses on the number
136
+ * the unit is killed for.
137
+ */
138
+ export function usedMb(rss = rssMb(), cgroup = cgroupCurrentMb()) {
139
+ return cgroup !== null && cgroup > rss ? cgroup : rss;
140
+ }
56
141
  //# sourceMappingURL=memory-guard.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"memory-guard.js","sourceRoot":"","sources":["../src/memory-guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,iDAAiD;AACjD,MAAM,CAAC,MAAM,kBAAkB,GAAG,GAAG,CAAC;AAEtC,6CAA6C;AAC7C,MAAM,UAAU,KAAK;IACnB,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC,GAAG,GAAG,OAAO,CAAC,CAAC;AACzD,CAAC;AAED,+EAA+E;AAC/E,MAAM,UAAU,aAAa,CAAC,MAA0C,OAAO,CAAC,GAAG;IACjF,MAAM,GAAG,GAAG,GAAG,CAAC,4BAA4B,CAAC;IA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