@parcha/agentrun-import 0.1.0-beta.63 → 0.1.0-beta.64

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/README.md CHANGED
@@ -45,6 +45,9 @@ const sessions = await parseHome(homedir(), { sources: ["claude-code", "codex"]
45
45
  - `workerOldMb` caps each worker's old generation (default 64, at least 8, refused with a RangeError under it: at 1 or
46
46
  2 MB V8 ends the whole process as a worker starts, and at 3 to 7 the workers run out of memory). A line over an eighth of it, or a file that runs a worker
47
47
  out of memory, is read on the calling thread instead; past two such losses a thread, the calling thread reads the rest.
48
+ Each reading thread (each worker, and the calling thread) also holds up to 8 MiB of scanner memory outside its heap:
49
+ the scanner's stack and tables, and room for the longest line it read. A line that would take it past 8 MiB (one over
50
+ about 6.7 MiB) is parsed whole instead, with the same totals.
48
51
  - A request two sources wrote is counted once. OpenCode's claude-agent provider runs Claude Code, which writes each
49
52
  request into its own session file too; OpenCode names that session on the request (`mirrors`). When the same call
50
53
  reads that Claude Code session (serve's import: or its store holds it), the request counts there and not in the OpenCode session (tokens, calls and cost),
@@ -218,7 +221,7 @@ read looks up afresh.
218
221
  The cursor is the reader's own: hand it back to the same reader for the same locator. A JSONL source builds its reader
219
222
  with `lineReader` from a per-record function; its cursor is the file's device and inode, the byte after the last whole
220
223
  line read, and the read's state. A file replaced or truncated since is read from its start, as is a cursor whose state
221
- is of another shape than the reader's (its `stateVersion`: a Codex cursor saved by 0.1.0-beta.58 or before), and a last
224
+ is of another shape than the reader's (its `stateVersion`: a Codex cursor saved by 0.1.0-beta.59 or before), and a last
222
225
  line with no newline yet is read but not consumed. A source that keeps many sessions in one file (a SQLite database) implements
223
226
  `discover`, `idOf` and `read` itself and returns one locator per session, with its `id`, and `stat`: the session's own size and
224
227
  last write, which a caller's size caps and "still being written" check go by (`locatorSize`; without `stat`, the file's).
package/dist/pool.d.ts CHANGED
@@ -25,8 +25,18 @@ export declare const BIG_FILE: number;
25
25
  * parseHome waits for. A cap on finished reads waiting, plus at most one read in flight per thread; not on the process's
26
26
  * memory. */
27
27
  export declare const READ_AHEAD_BYTES: number;
28
- /** The memory this process may use: its container's limit (process.constrainedMemory) when one is set below the
29
- * machine's, else the machine's. A 2 GiB container on a 512 GiB host has 2 GiB. */
28
+ /** The container's memory limit: process.constrainedMemory(), or the lowest memory.max or memory.high of this process's
29
+ * cgroup v2 and every cgroup above it when that is lower. Node reads only the process's own cgroup (the lower of its
30
+ * memory.max and memory.high, libuv 1.52); this takes the same rule up the tree. A container whose process sits in
31
+ * /init.scope (memory.max "max") under a namespace root of 4 GiB had no limit by Node, and the pool was sized for the
32
+ * host's 810 GB. Where no cgroup v2 level can be read, `constrained` as it is. */
33
+ export declare function constrainedMemory(root?: string, self?: string, constrained?: number): number;
34
+ /** The memory this process's cgroups still have room for: at each level with a limit (constrainedMemory's), the limit
35
+ * less its working set (workingSetOf), the least of them, never under 0. Infinity where no level has both a limit and a
36
+ * readable working set. A 2 GiB container already holding 1 GiB has 1 GiB of room, though the machine has hundreds free. */
37
+ export declare function cgroupRoom(root?: string, self?: string): number;
38
+ /** The memory this process may use: its container's limit (constrainedMemory) when one is set below the machine's, else
39
+ * the machine's. A 2 GiB container on a 512 GiB host has 2 GiB. */
30
40
  export declare const memoryLimit: (total?: number, constrained?: number) => number;
31
41
  /** The default read-ahead: READ_AHEAD_BYTES, or a sixteenth of the memory this process may use when that is less (512 MiB
32
42
  * at 8 GiB, 128 MiB in a 2 GiB container). */
@@ -39,10 +49,12 @@ export declare const DEFAULT_THREADS_CEILING = 16;
39
49
  * The pool's peak grew by about this, 80 MiB a worker at the default, from 8 threads to 24 (the runs DEFAULT_THREADS_CEILING cites). */
40
50
  export declare const workerBytes: (oldMb?: number) => number;
41
51
  /** The memory still free to this process: the machine's (os.freemem, MemAvailable on Linux: page cache it may reclaim
42
- * counts as free), at most its container's limit (memoryLimit). Not process.availableMemory: under a cgroup it takes the
43
- * limit less memory.current, which counts the page cache as used. On a 503 GiB machine with 345 GiB free, in a cgroup
44
- * of 350 GiB whose 346 GiB in use were 250 GiB of page cache, it read 130 MiB to 3.6 GiB from one second to the next. */
45
- export declare const freeMemory: (free?: number, limit?: number) => number;
52
+ * counts as free), at most its container's limit (memoryLimit) and the room its cgroups have left (cgroupRoom: each
53
+ * limit less what is in use, page cache the kernel takes back first not counted). Not process.availableMemory: under a
54
+ * cgroup it takes the limit less memory.current, which counts all the page cache as used. On a 503 GiB machine with
55
+ * 345 GiB free, in a cgroup of 350 GiB whose 346 GiB in use were 250 GiB of page cache, it read 130 MiB to 3.6 GiB from
56
+ * one second to the next. */
57
+ export declare const freeMemory: (free?: number, limit?: number, room?: number) => number;
46
58
  /** parseHome's default threads: the fewest of
47
59
  * - the cores this process may run on, one left for the thread that takes the reads and totals them (two cores keep
48
60
  * two threads: one worker would read no faster than the calling thread alone);
package/dist/pool.js CHANGED
@@ -7,7 +7,9 @@
7
7
  // (readAheadBytes()), the pool reads large files first (BIG_FILE and over, the largest first: they set the time), then
8
8
  // the rest in the import's order; at the read-ahead it reads only the one parseHome waits for. A worker's heap is bounded (OLD_MB): one that
9
9
  // runs out gives up its file, which parseHome reads itself, and another worker takes its place.
10
+ import { readFileSync } from "node:fs";
10
11
  import { availableParallelism, freemem, totalmem } from "node:os";
12
+ import { join } from "node:path";
11
13
  import { Worker } from "node:worker_threads";
12
14
  import { indexArrays } from "./record-index.js";
13
15
  /** Each worker's young generation: the default lets eight workers' garbage add up to more than the reads themselves. The
@@ -34,9 +36,79 @@ export const BIG_FILE = 64 * 2 ** 20;
34
36
  * parseHome waits for. A cap on finished reads waiting, plus at most one read in flight per thread; not on the process's
35
37
  * memory. */
36
38
  export const READ_AHEAD_BYTES = 2 ** 30;
37
- /** The memory this process may use: its container's limit (process.constrainedMemory) when one is set below the
38
- * machine's, else the machine's. A 2 GiB container on a 512 GiB host has 2 GiB. */
39
- export const memoryLimit = (total = totalmem(), constrained = process.constrainedMemory()) => (constrained > 0 && constrained < total ? constrained : total);
39
+ /** A cgroup file's bytes: Infinity when it is "max", missing (the host's root cgroup has none) or not a number. */
40
+ function bytesIn(dir, file) {
41
+ try {
42
+ const value = readFileSync(join(dir, file), "utf8").trim();
43
+ return /^\d+$/.test(value) && Number(value) > 0 ? Number(value) : Infinity;
44
+ }
45
+ catch {
46
+ return Infinity;
47
+ }
48
+ }
49
+ /** What a cgroup holds that the kernel will not give back: memory.current, which counts page cache, less the page cache it
50
+ * takes back first (inactive_file). Raw memory.current swung by gigabytes a second on a machine whose cache came and
51
+ * went; this does not count that cache. Undefined where either file is unreadable. */
52
+ function workingSetOf(dir) {
53
+ try {
54
+ const current = readFileSync(join(dir, "memory.current"), "utf8").trim();
55
+ const inactive = /^inactive_file (\d+)$/m.exec(readFileSync(join(dir, "memory.stat"), "utf8"));
56
+ if (!/^\d+$/.test(current) || !inactive)
57
+ return undefined;
58
+ return Math.max(0, Number(current) - Number(inactive[1]));
59
+ }
60
+ catch {
61
+ return undefined;
62
+ }
63
+ }
64
+ /** This process's cgroup v2 and every cgroup above it, the leaf first: undefined for cgroup v1 (no "0::" line), no
65
+ * /proc/self/cgroup (not Linux), a path outside the mount, or any error. Synchronous: read while the pool is sized, a
66
+ * few small files. */
67
+ function cgroupLevels(root, self) {
68
+ try {
69
+ const line = readFileSync(self, "utf8").split("\n").find((l) => l.startsWith("0::"));
70
+ if (line === undefined)
71
+ return undefined;
72
+ const parts = line.slice(3).split("/").filter((p) => p !== "" && p !== ".");
73
+ if (parts.includes(".."))
74
+ return undefined;
75
+ const levels = [];
76
+ for (let depth = parts.length; depth >= 0; depth -= 1) {
77
+ const dir = join(root, ...parts.slice(0, depth));
78
+ const limit = Math.min(bytesIn(dir, "memory.max"), bytesIn(dir, "memory.high"));
79
+ levels.push(limit < Infinity ? { limit, workingSet: workingSetOf(dir) } : { limit });
80
+ }
81
+ return levels;
82
+ }
83
+ catch {
84
+ return undefined;
85
+ }
86
+ }
87
+ /** The container's memory limit: process.constrainedMemory(), or the lowest memory.max or memory.high of this process's
88
+ * cgroup v2 and every cgroup above it when that is lower. Node reads only the process's own cgroup (the lower of its
89
+ * memory.max and memory.high, libuv 1.52); this takes the same rule up the tree. A container whose process sits in
90
+ * /init.scope (memory.max "max") under a namespace root of 4 GiB had no limit by Node, and the pool was sized for the
91
+ * host's 810 GB. Where no cgroup v2 level can be read, `constrained` as it is. */
92
+ export function constrainedMemory(root = "/sys/fs/cgroup", self = "/proc/self/cgroup", constrained = process.constrainedMemory()) {
93
+ const levels = cgroupLevels(root, self);
94
+ if (!levels)
95
+ return constrained;
96
+ const lowest = Math.min(constrained > 0 ? constrained : Infinity, ...levels.map((level) => level.limit));
97
+ return lowest === Infinity ? constrained : lowest;
98
+ }
99
+ /** The memory this process's cgroups still have room for: at each level with a limit (constrainedMemory's), the limit
100
+ * less its working set (workingSetOf), the least of them, never under 0. Infinity where no level has both a limit and a
101
+ * readable working set. A 2 GiB container already holding 1 GiB has 1 GiB of room, though the machine has hundreds free. */
102
+ export function cgroupRoom(root = "/sys/fs/cgroup", self = "/proc/self/cgroup") {
103
+ let room = Infinity;
104
+ for (const { limit, workingSet } of cgroupLevels(root, self) ?? [])
105
+ if (workingSet !== undefined)
106
+ room = Math.min(room, Math.max(0, limit - workingSet));
107
+ return room;
108
+ }
109
+ /** The memory this process may use: its container's limit (constrainedMemory) when one is set below the machine's, else
110
+ * the machine's. A 2 GiB container on a 512 GiB host has 2 GiB. */
111
+ export const memoryLimit = (total = totalmem(), constrained = constrainedMemory()) => (constrained > 0 && constrained < total ? constrained : total);
40
112
  /** The default read-ahead: READ_AHEAD_BYTES, or a sixteenth of the memory this process may use when that is less (512 MiB
41
113
  * at 8 GiB, 128 MiB in a 2 GiB container). */
42
114
  export const readAheadBytes = (memory = memoryLimit()) => Math.min(READ_AHEAD_BYTES, Math.floor(memory / 16));
@@ -48,10 +120,12 @@ export const DEFAULT_THREADS_CEILING = 16;
48
120
  * The pool's peak grew by about this, 80 MiB a worker at the default, from 8 threads to 24 (the runs DEFAULT_THREADS_CEILING cites). */
49
121
  export const workerBytes = (oldMb = OLD_MB) => (YOUNG_MB + oldMb) * 2 ** 20;
50
122
  /** The memory still free to this process: the machine's (os.freemem, MemAvailable on Linux: page cache it may reclaim
51
- * counts as free), at most its container's limit (memoryLimit). Not process.availableMemory: under a cgroup it takes the
52
- * limit less memory.current, which counts the page cache as used. On a 503 GiB machine with 345 GiB free, in a cgroup
53
- * of 350 GiB whose 346 GiB in use were 250 GiB of page cache, it read 130 MiB to 3.6 GiB from one second to the next. */
54
- export const freeMemory = (free = freemem(), limit = memoryLimit()) => Math.min(free, limit);
123
+ * counts as free), at most its container's limit (memoryLimit) and the room its cgroups have left (cgroupRoom: each
124
+ * limit less what is in use, page cache the kernel takes back first not counted). Not process.availableMemory: under a
125
+ * cgroup it takes the limit less memory.current, which counts all the page cache as used. On a 503 GiB machine with
126
+ * 345 GiB free, in a cgroup of 350 GiB whose 346 GiB in use were 250 GiB of page cache, it read 130 MiB to 3.6 GiB from
127
+ * one second to the next. */
128
+ export const freeMemory = (free = freemem(), limit = memoryLimit(), room = cgroupRoom()) => Math.min(free, limit, room);
55
129
  /** parseHome's default threads: the fewest of
56
130
  * - the cores this process may run on, one left for the thread that takes the reads and totals them (two cores keep
57
131
  * two threads: one worker would read no faster than the calling thread alone);
@@ -629,7 +629,7 @@ const spec = {
629
629
  totals: true,
630
630
  fields: FIELDS,
631
631
  // 2: a fork's copy of its parent's history (replay, looked, forkedFrom). 1 (never released): the open and unpaid
632
- // requests by their index (openAt, unpaidAt). 0.1.0-beta.58 and before held them as Answers (open, unpaid), which this
632
+ // requests by their index (openAt, unpaidAt). 0.1.0-beta.59 or before held them as Answers (open, unpaid), which this
633
633
  // record never reads. A cursor of another shape goes on from nothing.
634
634
  stateVersion: 2,
635
635
  // Older Codex writes each of the person's turns twice, as a user_message event and as a user message; current Codex
package/dist/scan.d.ts CHANGED
@@ -15,6 +15,12 @@ export type Fields = {
15
15
  export declare function fieldsOf(shape: Shape): Fields;
16
16
  /** The table's next free word: how much of the walk's table the fields compiled so far take (a test reads it). */
17
17
  export declare const tableUsed: () => number;
18
+ /** The walker's memory, read-only: its bytes and where a line starts in them (below that, its table, keys and events),
19
+ * or undefined before the first scan on this thread (a test reads it). */
20
+ export declare const walkerMemory: () => {
21
+ bytes: number;
22
+ base: number;
23
+ } | undefined;
18
24
  /** The line b[from, to) as JSON.parse of its UTF-8 text gives it, cut to `fields`: an object; undefined for a line that
19
25
  * is no object, is not JSON, or that scan leaves to JSON.parse (an escaped key, nested deeper than it goes, longer than
20
26
  * it reads). */
package/dist/scan.js CHANGED
@@ -32,6 +32,9 @@ let keysTop = 0;
32
32
  const named = [""];
33
33
  /** The table's next free word: how much of the walk's table the fields compiled so far take (a test reads it). */
34
34
  export const tableUsed = () => tableTop;
35
+ /** The walker's memory, read-only: its bytes and where a line starts in them (below that, its table, keys and events),
36
+ * or undefined before the first scan on this thread (a test reads it). */
37
+ export const walkerMemory = () => (walker ? { bytes: walker.memory.buffer.byteLength, base: walker.base() } : undefined);
35
38
  /** The walker, built on first use: a module of a few kilobytes, compiled where it is used (each worker its own). */
36
39
  function walkerOf() {
37
40
  return (walker ??= new WebAssembly.Instance(new WebAssembly.Module(Buffer.from(SCAN_WASM, "base64"))).exports);
@@ -61,9 +64,11 @@ function compile(w, shape) {
61
64
  }
62
65
  return node;
63
66
  }
64
- /** The longest line scan reads, in the walker's memory, which holds as much as the longest line it has read for as long
65
- * as the process lives: one longer is decoded as before. */
66
- const LONGEST = 1 << 26;
67
+ /** The most memory a thread's walker takes, 8 MiB: its stack and tables (from 0 to `base()`, about 1.3 MiB) and room for
68
+ * the longest line it has read, which it keeps for as long as its thread lives, outside the thread's heap limits. So each
69
+ * reading thread (a pool worker, or the thread that calls parseHome) holds up to 8 MiB of scanner memory beyond its
70
+ * heap, and no more. A line that would take the walker past it is parsed whole, as before, with the same answer. */
71
+ const SCANNER_BYTES = 1 << 23;
67
72
  /** `length` bytes of room for a line, or false where memory cannot grow to it. */
68
73
  function room(w, length) {
69
74
  const need = w.base() + length;
@@ -120,9 +125,11 @@ const CLOSE = 5;
120
125
  * it reads). */
121
126
  export function scan(b, from, to, fields) {
122
127
  const e = to - from;
123
- if (e > LONGEST || from < 0 || to > b.length)
128
+ if (e > SCANNER_BYTES || from < 0 || to > b.length)
124
129
  return undefined;
125
130
  const w = walkerOf();
131
+ if (w.base() + e > SCANNER_BYTES)
132
+ return undefined;
126
133
  fields.node ??= compile(w, fields.shape);
127
134
  if (fields.node < 0 || !room(w, e))
128
135
  return undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcha/agentrun-import",
3
- "version": "0.1.0-beta.63",
3
+ "version": "0.1.0-beta.64",
4
4
  "description": "Reads the coding-agent sessions under a home (Claude Code, Codex, pi, OpenCode, GitHub Copilot CLI) into one Session shape, with normalized token usage, subagents under their parent, byte-offset cursors for re-reads, and per-session totals.",
5
5
  "keywords": [
6
6
  "agentrun",