@parcha/agentrun-import 0.1.0-beta.57 → 0.1.0-beta.59
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 +107 -18
- package/dist/cli.js +23 -8
- package/dist/home.d.ts +22 -1
- package/dist/home.js +324 -131
- package/dist/index.d.ts +7 -5
- package/dist/index.js +4 -2
- package/dist/lines.d.ts +12 -1
- package/dist/lines.js +66 -8
- package/dist/mirror.d.ts +13 -0
- package/dist/mirror.js +15 -0
- package/dist/pool-worker.d.ts +6 -0
- package/dist/pool-worker.js +62 -0
- package/dist/pool.d.ts +80 -0
- package/dist/pool.js +243 -0
- package/dist/price.d.ts +13 -5
- package/dist/price.js +31 -21
- package/dist/read.d.ts +14 -4
- package/dist/read.js +37 -6
- package/dist/reader.d.ts +38 -8
- package/dist/readers/claude-code.js +2 -2
- package/dist/readers/codex.d.ts +7 -1
- package/dist/readers/codex.js +429 -9
- package/dist/readers/copilot.d.ts +4 -0
- package/dist/readers/copilot.js +224 -0
- package/dist/readers/index.d.ts +2 -0
- package/dist/readers/index.js +4 -0
- package/dist/readers/opencode.d.ts +42 -0
- package/dist/readers/opencode.js +920 -0
- package/dist/readers/pi.js +2 -2
- package/dist/record-index.d.ts +4 -0
- package/dist/record-index.js +35 -0
- package/dist/text.d.ts +2 -0
- package/dist/text.js +2 -0
- package/dist/types.d.ts +20 -3
- package/dist/usage.d.ts +6 -2
- package/dist/usage.js +42 -1
- package/dist/wrapped.d.ts +2 -0
- package/dist/wrapped.js +10 -4
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# @parcha/agentrun-import
|
|
2
2
|
|
|
3
|
-
Reads the coding-agent sessions under a home (Claude Code, Codex, pi) into one Session shape: normalized token
|
|
4
|
-
subagents counted under their parent, and
|
|
5
|
-
|
|
3
|
+
Reads the coding-agent sessions under a home (Claude Code, Codex, pi, OpenCode, GitHub Copilot CLI) into one Session shape: normalized token
|
|
4
|
+
usage, subagents counted under their parent, and cursors so a second pass reads only what changed. AgentRun's server
|
|
5
|
+
imports past sessions through it.
|
|
6
6
|
|
|
7
7
|
## Totals for a home
|
|
8
8
|
|
|
@@ -14,19 +14,41 @@ const sessions = await parseHome(homedir(), { sources: ["claude-code", "codex"]
|
|
|
14
14
|
```
|
|
15
15
|
|
|
16
16
|
- `key` is the sha256 (hex) of the session's file path relative to the home, `"/"` between its parts, with `#<id>` after
|
|
17
|
-
it for a file that holds many sessions. Lines come
|
|
17
|
+
it for a file that holds many sessions (an OpenCode database: `.local/share/opencode/opencode.db#ses_…`). Lines come
|
|
18
|
+
sorted by key.
|
|
18
19
|
- `tokens.input` is the input neither read from nor written to the cache, for every source (see `normalizeUsage`).
|
|
19
|
-
- `cost` is USD: the source's own when it wrote one (Claude Code's `cost-state`, pi's per-request cost
|
|
20
|
-
requests at pi-ai's list prices (a dependency, loaded on the
|
|
20
|
+
- `cost` is USD: the source's own when it wrote one (Claude Code's `cost-state`, pi's and OpenCode's per-request cost,
|
|
21
|
+
an OpenCode subagent's added to its parent's), else the requests at pi-ai's list prices (a dependency, loaded on the
|
|
22
|
+
first price; a load that fails throws with its reason), else `null`.
|
|
21
23
|
- `turns` counts the person's messages; `calls` the model requests, its subagents' with them, each once.
|
|
22
24
|
- `state: {}` handed to each call keeps cursors between calls: the second call reads only the bytes appended since, and
|
|
23
25
|
returns what a fresh call would. When a file holding a copied record now comes before the file it counts in (an append
|
|
24
26
|
moved the original after its resumed copy, or a new file sorts before one it shares records with), that source is read
|
|
25
27
|
again from the start. The state holds counts, record ids and paths, never a prompt, title, working directory or
|
|
26
28
|
answer; a file that fails to read gives up what it held and is read from its start next time.
|
|
27
|
-
- `failures: []` collects each file its reader could not read (`{ key, file, source, error }
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
- `failures: []` collects each file its reader could not read (`{ key, file, source, error }`; an OpenCode database
|
|
30
|
+
that does not open, for each session it lists or as itself when it lists none) while the rest are totalled; without
|
|
31
|
+
it, parseHome throws `ReadFailures`. An unreadable file is never taken for an empty one.
|
|
32
|
+
- `env` names where a source keeps its own directory (`CLAUDE_CONFIG_DIR`, `CODEX_HOME`, `COPILOT_HOME`, OpenCode's `XDG_DATA_HOME` and
|
|
33
|
+
`OPENCODE_DB`); without it the home's.
|
|
34
|
+
- `threads` reads the files on that many worker threads (default: the cores the process may run on, at most 8; `1`
|
|
35
|
+
reads them in order on the calling thread; at most 64). A read is kept only when it is the one the import's order would make; any
|
|
36
|
+
other file is read again in order, so the totals are the same whatever the count. A call with a `state`, or with
|
|
37
|
+
readers of its own, reads on the calling thread.
|
|
38
|
+
- `readAhead` caps the bytes of finished reads waiting to be taken (default 1 GiB, or a sixteenth of the memory the
|
|
39
|
+
process may use, its container's limit when one is set, when that is less: 512 MiB at 8 GiB). Under it, files of 64 MiB and over are read first, largest first, then
|
|
40
|
+
the rest in order; at it, the threads read only the file the import waits on. It is a cap on finished reads waiting,
|
|
41
|
+
plus at most one read in flight per thread (one of those can be a giant file), not on the process's memory.
|
|
42
|
+
- `workerOldMb` caps each worker's old generation (default 64). A line over an eighth of it, or a file that runs a worker
|
|
43
|
+
out of memory, is read on the calling thread instead; past two such losses a thread, the calling thread reads the rest.
|
|
44
|
+
|
|
45
|
+
On a 73.7 GB home, 8 threads took 90.9 s (median of 5) at a peak of 1.83 GiB, and 1 thread 441.1 s at 1.05 GiB
|
|
46
|
+
(x1); with `readAhead` at 256 MiB, 8 threads took 136.5 s (x1). Measured on a loaded machine (load 17 to 45).
|
|
47
|
+
- A request two sources wrote is counted once. OpenCode's claude-agent provider runs Claude Code, which writes each
|
|
48
|
+
request into its own session file too; OpenCode names that session on the request (`mirrors`). When the same call
|
|
49
|
+
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),
|
|
50
|
+
request by request: one OpenCode session can mix claude-agent requests with other providers'. When it does not, the
|
|
51
|
+
request counts in OpenCode's.
|
|
30
52
|
|
|
31
53
|
## A year of work
|
|
32
54
|
|
|
@@ -42,17 +64,21 @@ splits: `tokens`, `requests`, `days` (`{day, requests, tokens}`), `hours` (24) a
|
|
|
42
64
|
`harnesses` and `sources`. A session counts in `sessions` (and its prompts in `turns`) for the year its first request is in.
|
|
43
65
|
`cost` is the catalog's list price of the requests it could price; `unpricedRequests` counts the rest. Like the totals, it
|
|
44
66
|
holds counts, names and times only: no prompt, title, path or project. `parseHome`'s `requests` option is the hook it is
|
|
45
|
-
built on: each session's totals and its requests, as they are totalled.
|
|
67
|
+
built on: each session's totals and its requests, as they are totalled. With it, parseHome keeps every request (its model,
|
|
68
|
+
tokens and time) until its session is totalled; without it, each request is priced as its file is read and only its
|
|
69
|
+
cost is kept. Each call reads with a pool of its own, at the defaults unless `threads` or `readAhead` is passed: two
|
|
70
|
+
calls at once hold two pools' memory.
|
|
46
71
|
|
|
47
72
|
## Known answer
|
|
48
73
|
|
|
49
74
|
```sh
|
|
50
|
-
node dist/cli.js <home> [--sources claude-code,codex,pi] --known-answer
|
|
75
|
+
node dist/cli.js <home> [--sources claude-code,codex,pi,opencode,copilot] [--threads N] [--read-ahead BYTES[k|m|g]] --known-answer
|
|
51
76
|
```
|
|
52
77
|
|
|
53
78
|
One JSONL line per session, sorted by key: `{key, source, tokens: {input, output, cacheRead, cacheWrite, cacheWrite1h},
|
|
54
79
|
cost, turns, calls}`. Numbers only: no content, title, directory or path is printed. `--help` (`-h`) prints the usage
|
|
55
|
-
and exits 0; a bad argument exits 2.
|
|
80
|
+
and exits 0; a bad argument exits 2. `--threads` and `--read-ahead` set parseHome's `threads` and `readAhead` (`1`, or
|
|
81
|
+
`256m`, holds less memory); the answer is the same. A file that cannot be read prints no answer: one stderr line per such file
|
|
56
82
|
(`agentrun-import: cannot read <source> <key> (<code>)`) and exit 1. The CLI imports pi-ai statically for its prices:
|
|
57
83
|
a bundle (`bun build --compile`) carries it, prints the same bytes as under Node, and a build or run without pi-ai
|
|
58
84
|
fails instead of pricing nothing. pi-ai is a dependency of the package, so `npx @parcha/agentrun-import` runs the
|
|
@@ -60,19 +86,49 @@ CLI as it is; the library loads it only to price.
|
|
|
60
86
|
|
|
61
87
|
## Usage
|
|
62
88
|
|
|
63
|
-
`normalizeUsage(format, raw)` reads one request's usage as `"anthropic"`, `"openai"` or `"
|
|
89
|
+
`normalizeUsage(format, raw, options?)` reads one request's usage as `"anthropic"`, `"openai"`, `"pi"`, `"opencode"` or `"copilot"` writes it:
|
|
64
90
|
|
|
65
91
|
| Source | `input` | `cacheWrite1h` | `reasoning` |
|
|
66
92
|
| --- | --- | --- | --- |
|
|
67
93
|
| Anthropic | `input_tokens` (already excludes the cache) | `cache_creation.ephemeral_1h_input_tokens` | |
|
|
68
94
|
| OpenAI, Codex | `input_tokens - cached_input_tokens` | | `reasoning_output_tokens` (part of `output`) |
|
|
69
95
|
| pi | `input` | `cacheWrite1h` | |
|
|
96
|
+
| OpenCode | `input` (already excludes the cache; `cache.read`, `cache.write`) | | `reasoning`, added to `output` |
|
|
97
|
+
| Copilot CLI | `inputTokens - cacheReadTokens - cacheWriteTokens` | | `reasoningTokens` (part of `output`) |
|
|
70
98
|
|
|
71
99
|
A Claude Code session's id is its file's name (`<id>.jsonl`): a resumed session's file is its own session, and a
|
|
72
100
|
record copied into it from an earlier file counts only in that file; a file holding nothing but copies is no session.
|
|
73
101
|
Codex sessions sum each request's `last_token_usage`, never `total_token_usage`. Claude Code writes one line per content
|
|
74
102
|
block of a response, each with the response's usage: a request (`message.id`, `requestId`) counts once, at its last
|
|
75
|
-
line's usage.
|
|
103
|
+
line's usage. OpenCode's `output` leaves the reasoning out, so `output` is `output + reasoning`. An OpenCode before
|
|
104
|
+
1.3.16 (`options.version`, the session's `version`) wrote it in `output` already, so its `output` stays as written; where
|
|
105
|
+
a step records `total`, `output` is what the total leaves after the input and the cache, whatever the version. Each
|
|
106
|
+
step of an OpenCode message is one request, at its `step-finish` part's usage (the message keeps only the last step's).
|
|
107
|
+
|
|
108
|
+
### GitHub Copilot CLI
|
|
109
|
+
|
|
110
|
+
Sessions are `$COPILOT_HOME/session-state/<id>/events.jsonl` (`~/.copilot` without `COPILOT_HOME`), and an older CLI's
|
|
111
|
+
`session-state/<id>.jsonl`. Each model call is one request, keyed by its first `assistant.message`'s `messageId`; a
|
|
112
|
+
response split into chunks (each chunk a message with its own `messageId`, the call's `apiCallId` the same) is one. Each
|
|
113
|
+
event counts once by its `id` and each message once by its `messageId`, across files too: a copied session adds no
|
|
114
|
+
request, and its copied shutdown no tokens. A rotated journal's `checkpoint: true` records are copies. A user message
|
|
115
|
+
is the person's unless a subagent, autopilot, a skill or another agent wrote it. A session's id is its `session.start`
|
|
116
|
+
`sessionId`, else `copilotSessionId(file)`: the folder's name, or the older flat file's.
|
|
117
|
+
|
|
118
|
+
Copilot writes no request's tokens to disk: `assistant.usage` is ephemeral. Its tokens are the `modelMetrics` of the
|
|
119
|
+
`session.shutdown` each CLI process writes as it exits, per model, subagents included. A CLI that keeps its accounting
|
|
120
|
+
across processes (`session.usage_checkpoint`, a shutdown with `usageAccountingWatermarks`) writes the session's whole
|
|
121
|
+
total there, so a shutdown counts only what it adds to the ones before it; an older CLI's counts its own process. A
|
|
122
|
+
model's tokens go on the last request of that model since the process started or resumed the session, or on a request
|
|
123
|
+
of their own when there is none (a compaction's model). That model's other requests in the process are `folded` into
|
|
124
|
+
it: zeros, known, never unknown. So:
|
|
125
|
+
|
|
126
|
+
- A session with no shutdown (still running, killed, or left with `/new`) has tokens 0 and cost `null`.
|
|
127
|
+
- Copilot bills premium requests and AI credits and writes no dollar cost. `cost` is the catalog's list price of the
|
|
128
|
+
requests that carry the tokens, and `null` when a request's tokens are unknown or its model is not in the catalog.
|
|
129
|
+
- `events.N.jsonl` archives (written only with `COPILOT_SESSION_EVENT_LOG_ROTATION_BYTES`) and the pre-0.0.342
|
|
130
|
+
`history-session-state` folder are not read. A rotated session's tokens still come whole from its last shutdown;
|
|
131
|
+
the requests in its archives are not counted.
|
|
76
132
|
|
|
77
133
|
## Readers
|
|
78
134
|
|
|
@@ -85,17 +141,44 @@ type Reader<S extends string, C> = {
|
|
|
85
141
|
name: string;
|
|
86
142
|
harness: string;
|
|
87
143
|
discover(home: string, env?: Env): Promise<Locator[]>;
|
|
88
|
-
|
|
144
|
+
idOf(locator: Locator): string;
|
|
145
|
+
read(locator: Locator, cursor?: C, options?: { turns?: boolean; totals?: boolean; seen?: Set<string>; lookups?: Map<string, unknown> }): Promise<{ sessions: SessionRead<S>[]; cursor: C; read: number; resumed: boolean }>;
|
|
89
146
|
order?(a, b): number;
|
|
90
147
|
children?(home: string, env?: Env): Promise<{ locator: Locator; of: Locator }[]>;
|
|
148
|
+
stat?(locator: Locator): Promise<{ bytes: number; mtimeMs: number; parent?: string } | null>;
|
|
149
|
+
parentOf?(locator: Locator): Promise<string | undefined>;
|
|
91
150
|
};
|
|
92
151
|
```
|
|
93
152
|
|
|
153
|
+
`idOf` is the id a read gives the locator's session when no record names one (Claude Code's always: its file's name;
|
|
154
|
+
pi's the last `_` part of its file's name; an OpenCode locator's `id`): a caller that names a session without reading it
|
|
155
|
+
takes the same id.
|
|
156
|
+
|
|
157
|
+
`lookups` is one map per import, shared by all its reads: a reader keeps there what it looks up once for the whole
|
|
158
|
+
import (the Codex reader, the rollouts under a sessions directory, listed the first time a fork's parent is not on its
|
|
159
|
+
own date). `parseHome` passes one; with threads, each worker of its read pool keeps one for the import. Without it, each
|
|
160
|
+
read looks up afresh.
|
|
161
|
+
|
|
94
162
|
The cursor is the reader's own: hand it back to the same reader for the same locator. A JSONL source builds its reader
|
|
95
163
|
with `lineReader` from a per-record function; its cursor is the file's device and inode, the byte after the last whole
|
|
96
164
|
line read, and the read's state. A file replaced or truncated since is read from its start, and a last line with no
|
|
97
165
|
newline yet is read but not consumed. A source that keeps many sessions in one file (a SQLite database) implements
|
|
98
|
-
`discover` and `read` itself and returns one locator per session, with its `id
|
|
166
|
+
`discover`, `idOf` and `read` itself and returns one locator per session, with its `id`, and `stat`: the session's own size and
|
|
167
|
+
last write, which a caller's size caps and "still being written" check go by (`locatorSize`; without `stat`, the file's).
|
|
168
|
+
|
|
169
|
+
OpenCode is that source. Its sessions are in `$XDG_DATA_HOME/opencode` (`~/.local/share/opencode`): `opencode*.db` (1.x
|
|
170
|
+
and 2.x schemas; a 2.x session wins over the 1.x copy beside it), the database `OPENCODE_DB` names, and the JSON
|
|
171
|
+
`storage/` OpenCode kept before SQLite. A locator is `{ file: <db>, id: <session id> }`, or a storage session's own
|
|
172
|
+
JSON file; each session is listed once, from the database that wrote it last (its rows' latest `time_updated`; a tie,
|
|
173
|
+
the first database). Its cursor is the file's device and inode
|
|
174
|
+
and a stamp of the session's rows (each table's row count and latest `time_updated`): an unchanged session is not read
|
|
175
|
+
again, a changed one is read again whole, a replaced database from its start. A fork's rows copied from its source
|
|
176
|
+
session count nothing. A 2.x fork marks them. A 1.x fork (OpenCode's `fork()` copies each message under a new id, its
|
|
177
|
+
time, tokens and cost as they were) and a 2.x row with no mark are told by a fingerprint: a request's time, step, model,
|
|
178
|
+
tokens and cost as written, a prompt's time and text. `order` reads a home's sessions oldest first, so a source comes
|
|
179
|
+
before its forks, and the cursor keeps the fingerprints its read took, so a session read again is no copy of itself.
|
|
180
|
+
A row time past now reads as its database's last write (its file's or its `-wal`'s, never past now): the same at each look, so the session ages out as its file does. A `turns: false` read keeps a request's error as `failed`, never its text. A database session's `stat` is the bytes of its messages' and parts' `data` and the latest
|
|
181
|
+
`time_updated` of its rows; a storage session's, its files' together. It reads SQLite with `node:sqlite` under Node and `bun:sqlite` under Bun, read-only, waiting up to 5 s on a write lock.
|
|
99
182
|
|
|
100
183
|
With `turns: false`, `totals: true` asks for only what `parseHome` totals: each session read then has `counts` set and
|
|
101
184
|
`requests` empty. Only a `lineReader` whose source sets `totals: true` (one that sets no turn's model, usage or time after
|
|
@@ -123,5 +206,11 @@ for (const locator of await reader.discover(home)) {
|
|
|
123
206
|
}
|
|
124
207
|
```
|
|
125
208
|
|
|
126
|
-
`readSession(locator, source)`, `sessionLocators(home, source)`, `inOrder(locators, source)`
|
|
127
|
-
`childrenOf(home, source)` are what
|
|
209
|
+
`readSession(locator, source)`, `sessionLocators(home, source)`, `inOrder(locators, source)` (each locator with its
|
|
210
|
+
`locatorSize`; one whose size query throws is kept, marked `failed`, for the caller to read or report), `locatorSize(locator, source)` (null when the session is gone, any other error thrown), `locatorParent(locator, source)` and `childrenOf(home, source)` are what
|
|
211
|
+
serve's import reads through. `locatorParent` names the session a subagent ran under without reading the subagent: the
|
|
212
|
+
`parent` its reader's `stat` gives (an OpenCode row's), else its reader's `parentOf`. Codex's reads a rollout's first
|
|
213
|
+
line, `session_meta`, 256 KiB at a time and never past 64 MiB: a first line longer than that names no parent. serve uses it to tie a subagent over the cap to its run, which
|
|
214
|
+
it then holds back. The
|
|
215
|
+
mirror rule is one export for both callers: `mirroredIn(counted)` over the `sessionKey(source, id)` of the sessions
|
|
216
|
+
counted, and `ownRequests(requests, cost, mirrored)` for a session's own requests and cost.
|
package/dist/cli.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// The known answer for a home: one JSONL line per session, sorted by key, numbers only (no content, title, directory or
|
|
3
3
|
// path), so two readers of the same home can be compared line for line.
|
|
4
|
-
// node dist/cli.js <home> [--sources claude-code,codex,pi] --known-answer
|
|
4
|
+
// node dist/cli.js <home> [--sources claude-code,codex,pi,opencode,copilot] [--threads N] [--read-ahead BYTES] --known-answer
|
|
5
5
|
// A line: {key, source, tokens: {input, output, cacheRead, cacheWrite, cacheWrite1h}, cost, turns, calls}. key is the
|
|
6
|
-
// sha256 of the session file's path relative to <home
|
|
7
|
-
//
|
|
6
|
+
// sha256 of the session file's path relative to <home>, with "#<session id>" after it for a file of many sessions (an
|
|
7
|
+
// OpenCode database); turns, the person's user messages; calls, model requests (its subagents' with them); cost, USD,
|
|
8
|
+
// null when unknown. --threads and --read-ahead are parseHome's `threads` and
|
|
9
|
+
// `readAhead` (BYTES: a whole number, or with k, m or g after it for KiB, MiB or GiB); fewer threads or a smaller
|
|
10
|
+
// read-ahead hold less memory. --help (-h) prints the usage and exits 0; a usage error exits 2.
|
|
8
11
|
// A file that cannot be read makes the answer incomplete: no line is printed, one stderr line names each such file by
|
|
9
12
|
// its source, key and error code (never its path), and the exit is 1.
|
|
10
13
|
//
|
|
@@ -13,10 +16,19 @@
|
|
|
13
16
|
import { parseArgs } from "node:util";
|
|
14
17
|
import { calculateCost } from "@earendil-works/pi-ai";
|
|
15
18
|
import { builtinModels } from "@earendil-works/pi-ai/providers/all";
|
|
16
|
-
import { parseHome } from "./home.js";
|
|
19
|
+
import { MOST_THREADS, parseHome } from "./home.js";
|
|
17
20
|
import { catalogOf } from "./price.js";
|
|
18
21
|
import { SOURCES } from "./readers/index.js";
|
|
19
|
-
const USAGE = `usage: agentrun-import <home> [--sources ${SOURCES.join(",")}] --known-answer`;
|
|
22
|
+
const USAGE = `usage: agentrun-import <home> [--sources ${SOURCES.join(",")}] [--threads N] [--read-ahead BYTES[k|m|g]] --known-answer`;
|
|
23
|
+
const UNITS = { "": 1, k: 2 ** 10, m: 2 ** 20, g: 2 ** 30 };
|
|
24
|
+
/** A flag's whole number (with a unit when `units`), or a usage error. */
|
|
25
|
+
function wholeOf(flag, value, least, units = false, most = Number.MAX_SAFE_INTEGER) {
|
|
26
|
+
const match = (units ? /^(\d+)([kmg]?)$/i : /^(\d+)()$/).exec(value.trim());
|
|
27
|
+
const n = match ? Number(match[1]) * UNITS[match[2].toLowerCase()] : Number.NaN;
|
|
28
|
+
if (!Number.isSafeInteger(n) || n < least || n > most)
|
|
29
|
+
usage(`${flag}: a whole number ${most < Number.MAX_SAFE_INTEGER ? `from ${least} to ${most}` : `of at least ${least}`}${units ? " (k, m or g after it for KiB, MiB, GiB)" : ""}, not ${value}`);
|
|
30
|
+
return n;
|
|
31
|
+
}
|
|
20
32
|
function usage(message) {
|
|
21
33
|
process.stderr.write(`${message}\n${USAGE}\n`);
|
|
22
34
|
process.exit(2);
|
|
@@ -29,7 +41,7 @@ const codeOf = (error) => {
|
|
|
29
41
|
async function main() {
|
|
30
42
|
let parsed;
|
|
31
43
|
try {
|
|
32
|
-
parsed = parseArgs({ allowPositionals: true, options: { sources: { type: "string" }, "known-answer": { type: "boolean" }, help: { type: "boolean", short: "h" } } });
|
|
44
|
+
parsed = parseArgs({ allowPositionals: true, options: { sources: { type: "string" }, threads: { type: "string" }, "read-ahead": { type: "string" }, "known-answer": { type: "boolean" }, help: { type: "boolean", short: "h" } } });
|
|
33
45
|
}
|
|
34
46
|
catch (error) {
|
|
35
47
|
usage(error.message);
|
|
@@ -43,12 +55,15 @@ async function main() {
|
|
|
43
55
|
usage("one home directory, please");
|
|
44
56
|
if (!values["known-answer"])
|
|
45
57
|
usage("--known-answer is the one output there is");
|
|
46
|
-
|
|
58
|
+
// A source named twice is read once: a repeat would print its every session twice.
|
|
59
|
+
const sources = values.sources === undefined ? SOURCES : [...new Set(values.sources.split(",").map((name) => name.trim()))];
|
|
47
60
|
const unknown = sources.filter((name) => !SOURCES.includes(name));
|
|
48
61
|
if (unknown.length || !sources.length)
|
|
49
62
|
usage(`unknown source: ${unknown.join(", ") || "(none)"}`);
|
|
63
|
+
const threads = values.threads === undefined ? undefined : wholeOf("--threads", values.threads, 1, false, MOST_THREADS);
|
|
64
|
+
const readAhead = values["read-ahead"] === undefined ? undefined : wholeOf("--read-ahead", values["read-ahead"], 0, true);
|
|
50
65
|
const failures = [];
|
|
51
|
-
const sessions = await parseHome(positionals[0], { sources: sources, catalog: catalogOf(builtinModels().getModels()), calculateCost: calculateCost, failures });
|
|
66
|
+
const sessions = await parseHome(positionals[0], { sources: sources, catalog: catalogOf(builtinModels().getModels()), calculateCost: calculateCost, failures, ...(threads === undefined ? {} : { threads }), ...(readAhead === undefined ? {} : { readAhead }) });
|
|
52
67
|
if (failures.length) {
|
|
53
68
|
for (const { source, key, error } of failures)
|
|
54
69
|
process.stderr.write(`agentrun-import: cannot read ${source} ${key} (${codeOf(error)})\n`);
|
package/dist/home.d.ts
CHANGED
|
@@ -67,6 +67,20 @@ export type ParseOptions = {
|
|
|
67
67
|
* their times and models, text-free as the totals are. `started`: the time (ms) of the first request of the session's
|
|
68
68
|
* own files (not its subagents'), absent when it has none. For a caller that buckets the same numbers (wrapped.ts). */
|
|
69
69
|
requests?: (totals: SessionTotals, requests: readonly SessionRequest[], started: number | undefined) => void;
|
|
70
|
+
/** Threads that read the sources' files ahead, largest first, without a state and with the registry's readers (default:
|
|
71
|
+
* the cores this process may run on, at most 8: past that the largest files set the time, and each worker costs its
|
|
72
|
+
* heap). Each read is kept only when it is the one parseHome would make in order; 1 reads every file in order on this
|
|
73
|
+
* thread. At most MOST_THREADS (64). */
|
|
74
|
+
threads?: number;
|
|
75
|
+
/** With threads, a cap on the bytes of finished reads made ahead of the one being taken that may wait (default
|
|
76
|
+
* readAheadBytes(): 1 GiB, or a sixteenth of the machine's memory when less); at it, the threads read only the file
|
|
77
|
+
* being taken. Large files (BIG_FILE, 64 MiB, and over) are read first, the rest in the import's order. More holds
|
|
78
|
+
* more memory and waits less behind a large file. A cap on finished reads waiting, plus at most one read in flight
|
|
79
|
+
* per thread; not on the process's memory. */
|
|
80
|
+
readAhead?: number;
|
|
81
|
+
/** With threads, each worker's old generation in MB (default OLD_MB, 64). A worker that runs out of it gives its file
|
|
82
|
+
* back, which is read on this thread, and another takes its place; past 2 x threads such losses the rest is read here. */
|
|
83
|
+
workerOldMb?: number;
|
|
70
84
|
};
|
|
71
85
|
/** One model request of a session: when (ms), by which model ("provider/model", the session's when the request names none),
|
|
72
86
|
* and its tokens where the source counted them. */
|
|
@@ -88,6 +102,13 @@ export declare class ReadFailures extends Error {
|
|
|
88
102
|
readonly failures: ReadFailure[];
|
|
89
103
|
constructor(failures: ReadFailure[]);
|
|
90
104
|
}
|
|
105
|
+
/** The most threads parseHome takes: each is a worker with a heap of its own, and past the cores the largest files set
|
|
106
|
+
* the time anyway. More is refused before any worker starts. */
|
|
107
|
+
export declare const MOST_THREADS = 64;
|
|
108
|
+
/** A session's cost with its subagents': Claude Code's written cost holds its subagents' already (its cost-state), so it
|
|
109
|
+
* stays as it is; every other source writes a subagent's cost on the subagent's own session, so each is added. Unknown
|
|
110
|
+
* when the session's is, or when one it adds is: never a subagent counted at 0. parseHome's totals and serve's import. */
|
|
111
|
+
export declare function withSubagentCosts(source: string, cost: number | undefined, subagents: readonly (number | undefined)[]): number | undefined;
|
|
91
112
|
/** Every session of `sources` under `home`, as totals, sorted by key. */
|
|
92
|
-
export declare function parseHome(home: string, { sources, readers, state, catalog, calculateCost, env, failures, requests }?: ParseOptions): Promise<SessionTotals[]>;
|
|
113
|
+
export declare function parseHome(home: string, { sources, readers, state, catalog, calculateCost, env, failures, requests, threads, readAhead, workerOldMb }?: ParseOptions): Promise<SessionTotals[]>;
|
|
93
114
|
export {};
|