@parcha/agentrun-import 0.1.0-beta.58 → 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/dist/read.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { Stats } from "node:fs";
2
- import type { Env, Locator, Reader, ReadOptions } from "./reader.js";
2
+ import type { Env, Locator, LocatorSize, Reader, ReadOptions } from "./reader.js";
3
3
  import { type Source } from "./readers/index.js";
4
4
  import type { SessionOf } from "./types.js";
5
5
  export type Session = SessionOf<Source>;
@@ -12,12 +12,22 @@ export declare function sessionLocators(home: string, source: string, { env, rea
12
12
  env?: Env;
13
13
  readers?: Readers;
14
14
  }): Promise<Locator[]>;
15
- /** The locators of `source`, each with its file's stats (one gone since it was listed is passed over), in the import's
16
- * order: the reader's (Claude Code's by project folder, oldest written first: the first file holding a copied record
17
- * keeps it), else as they were listed. */
15
+ /** What the session at `locator` takes and when it was last written: the reader's stat, else its file's size and mtime.
16
+ * null when it is gone (the reader says so, or its file is). Any other error (a database locked or damaged, no access)
17
+ * is thrown: a session whose size cannot be had is not one that is gone. */
18
+ export declare function locatorSize(locator: Locator, source: string, readers?: Readers): Promise<LocatorSize | null>;
19
+ /** The session the one at `locator` ran under, as its reader tells it without reading the session (locatorSize's
20
+ * `parent`, else the reader's bounded head read, parentOf): what ties a subagent too large to read to its run. */
21
+ export declare function locatorParent(locator: Locator, source: string, readers?: Readers): Promise<string | undefined>;
22
+ /** The locators of `source`, each with its file's stats and its own size (locatorSize; one gone since it was listed is
23
+ * passed over), in the import's order: the reader's (Claude Code's by project folder, oldest written first: the first
24
+ * file holding a copied record keeps it), else as they were listed. One whose size cannot be had is kept, `failed` with
25
+ * why and its file's size: the caller reads it (its read fails as it did) or says it is left out, never drops it unsaid. */
18
26
  export declare function inOrder(locators: readonly Locator[], source: string, readers?: Readers): Promise<{
19
27
  locator: Locator;
20
28
  stats: Stats;
29
+ size: LocatorSize;
30
+ failed?: unknown;
21
31
  }[]>;
22
32
  /** The subagents' sessions of `source` under `home` that name no parent themselves, each with the session it ran under
23
33
  * (Claude Code's <session id>/subagents/). */
package/dist/read.js CHANGED
@@ -17,19 +17,50 @@ export async function sessionLocators(home, source, { env, readers } = {}) {
17
17
  out.push(locator);
18
18
  return out;
19
19
  }
20
- /** The locators of `source`, each with its file's stats (one gone since it was listed is passed over), in the import's
21
- * order: the reader's (Claude Code's by project folder, oldest written first: the first file holding a copied record
22
- * keeps it), else as they were listed. */
20
+ /** What the session at `locator` takes and when it was last written: the reader's stat, else its file's size and mtime.
21
+ * null when it is gone (the reader says so, or its file is). Any other error (a database locked or damaged, no access)
22
+ * is thrown: a session whose size cannot be had is not one that is gone. */
23
+ export async function locatorSize(locator, source, readers) {
24
+ const own = readerOf(source, readers).stat;
25
+ if (own)
26
+ return own(locator);
27
+ const stats = await stat(locator.file).catch((error) => {
28
+ if (error.code === "ENOENT" || error.code === "ENOTDIR")
29
+ return null;
30
+ throw error;
31
+ });
32
+ return stats && { bytes: stats.size, mtimeMs: stats.mtimeMs };
33
+ }
34
+ /** The session the one at `locator` ran under, as its reader tells it without reading the session (locatorSize's
35
+ * `parent`, else the reader's bounded head read, parentOf): what ties a subagent too large to read to its run. */
36
+ export async function locatorParent(locator, source, readers) {
37
+ const reader = readerOf(source, readers);
38
+ const size = reader.stat ? await locatorSize(locator, source, readers).catch(() => null) : null;
39
+ return size?.parent ?? (await reader.parentOf?.(locator).catch(() => undefined));
40
+ }
41
+ /** The locators of `source`, each with its file's stats and its own size (locatorSize; one gone since it was listed is
42
+ * passed over), in the import's order: the reader's (Claude Code's by project folder, oldest written first: the first
43
+ * file holding a copied record keeps it), else as they were listed. One whose size cannot be had is kept, `failed` with
44
+ * why and its file's size: the caller reads it (its read fails as it did) or says it is left out, never drops it unsaid. */
23
45
  export async function inOrder(locators, source, readers) {
24
46
  const out = [];
25
47
  for (const locator of locators) {
26
48
  const stats = await stat(locator.file).catch(() => null);
27
- if (stats)
28
- out.push({ locator, stats });
49
+ if (!stats)
50
+ continue;
51
+ const file = { bytes: stats.size, mtimeMs: stats.mtimeMs };
52
+ if (!readerOf(source, readers).stat) {
53
+ out.push({ locator, stats, size: file });
54
+ continue;
55
+ }
56
+ const size = await locatorSize(locator, source, readers).then((size) => size && { size }, (failed) => ({ size: file, failed }));
57
+ if (size)
58
+ out.push({ locator, stats, ...size });
29
59
  }
30
60
  const order = readerOf(source, readers).order;
61
+ const key = ({ locator, stats, size }) => ({ file: locator.file, mtimeMs: stats.mtimeMs, ...(locator.id !== undefined ? { id: locator.id } : {}), ...(size.created !== undefined ? { created: size.created } : {}) });
31
62
  if (order)
32
- out.sort((a, b) => order({ file: a.locator.file, mtimeMs: a.stats.mtimeMs }, { file: b.locator.file, mtimeMs: b.stats.mtimeMs }));
63
+ out.sort((a, b) => order(key(a), key(b)));
33
64
  return out;
34
65
  }
35
66
  /** The subagents' sessions of `source` under `home` that name no parent themselves, each with the session it ran under
package/dist/reader.d.ts CHANGED
@@ -10,6 +10,25 @@ export type Locator = {
10
10
  };
11
11
  /** A locator as one string: the file, with `#id` after it when it names one. */
12
12
  export declare const locatorKey: ({ file, id }: Locator) => string;
13
+ /** What a locator's session takes: its bytes, and when it was last written (ms). A file's size and mtime for a file of
14
+ * one session; for one session of many in a file (a database), that session's own. */
15
+ export type LocatorSize = {
16
+ bytes: number;
17
+ mtimeMs: number;
18
+ /** The session it ran under, when the reader knows it without reading the session (a database row's parent): a caller
19
+ * that leaves a subagent out (still written, too large) leaves the session it ran under out with it. */
20
+ parent?: string;
21
+ /** When the session began (ms), when the reader knows it without reading the session (a database row's): what its
22
+ * order of reading may go by. */
23
+ created?: number;
24
+ };
25
+ /** What a reader's order of locators goes by. */
26
+ export type OrderKey = {
27
+ file: string;
28
+ mtimeMs: number;
29
+ id?: string;
30
+ created?: number;
31
+ };
13
32
  /** One session a read found: where it is, what it is, and every request read (each counted once), whether turns are kept
14
33
  * or not: what a total counts. A totals read (ReadOptions.totals) has them as `counts` alone, `requests` empty. */
15
34
  export type SessionRead<S extends string = string> = {
@@ -43,6 +62,13 @@ export type ReadOptions = {
43
62
  * `seen` like any other, as no later read takes it again. Default true: it is left out of `seen` until a read from
44
63
  * the cursor takes it whole. */
45
64
  keepsCursor?: boolean;
65
+ /** The longest line, in bytes, the read takes: a longer one ends it with a LineTooLong error, before the line is
66
+ * decoded. The read pool's workers set it to an eighth of their heap (pool.ts), so that no one line can end the
67
+ * process; parseHome then reads the file on its own thread, with no such bound. Default: none. */
68
+ maxLine?: number;
69
+ /** What a reader looks up once for a whole import, shared across its files: one map per import, each reader keying
70
+ * its own entries. Without one, each read looks up afresh. */
71
+ lookups?: Map<string, unknown>;
46
72
  };
47
73
  export type Reader<S extends string = string, C = unknown> = {
48
74
  source: S;
@@ -52,17 +78,21 @@ export type Reader<S extends string = string, C = unknown> = {
52
78
  harness: string;
53
79
  /** The candidate locators of the source under `home`, any order (`env`: where the source's own directory is named). */
54
80
  discover: (home: string, env?: Env) => Promise<Locator[]>;
81
+ /** The id the read gives the locator's session when no record of it names one (Claude Code's always: its file's
82
+ * name). A caller that names a session without reading it (a listing's probe) takes the same id. */
83
+ idOf: (locator: Locator) => string;
55
84
  /** One locator read, from its start or from `cursor` (one this reader returned for the same locator). A cursor that no
56
85
  * longer fits (the file replaced, truncated, rewritten) is read from the start, `resumed` false. */
57
86
  read: (locator: Locator, cursor?: C, options?: ReadOptions) => Promise<ReadResult<S, C>>;
58
- /** The import's order of the listed locators, when not their path's (a record copied into two files is the first's). */
59
- order?: (a: {
60
- file: string;
61
- mtimeMs: number;
62
- }, b: {
63
- file: string;
64
- mtimeMs: number;
65
- }) => number;
87
+ /** The import's order of the listed locators, when not their path's (a record copied into two files is the first's):
88
+ * each with its file's mtime, its id in the file, and when its session began where the reader's stat says. */
89
+ order?: (a: OrderKey, b: OrderKey) => number;
90
+ /** The locator's own size and last write, when it is not its file's (a session in a database of many): what a caller's
91
+ * size caps and liveness go by. null: the session is not there. Without it, the file's (locatorSize). */
92
+ stat?: (locator: Locator) => Promise<LocatorSize | null>;
93
+ /** The session the locator's ran under, from a bounded head of its file, never the rest (Codex's first line): what ties
94
+ * a subagent a caller will not read (over its cap) to its run. None: no parent named there. */
95
+ parentOf?: (locator: Locator) => Promise<string | undefined>;
66
96
  /** Subagents' sessions that name no parent themselves, each with the session it ran under (`of`). */
67
97
  children?: (home: string, env?: Env) => Promise<{
68
98
  locator: Locator;
@@ -6,7 +6,7 @@ import { lineReader } from "../lines.js";
6
6
  import { entries } from "../reader.js";
7
7
  import { addRecord, ownerOf, recordIndex } from "../record-index.js";
8
8
  import { packed, packInto } from "../record-key.js";
9
- import { argsOf, effortOf, INTERRUPTED, isObject, str, textOf } from "../text.js";
9
+ import { argsOf, effortOf, FAILED, INTERRUPTED, isObject, str, textOf } from "../text.js";
10
10
  import { normalizeUsage } from "../usage.js";
11
11
  // The titles Claude Code writes into a session's file, each again as it changes, and the field each holds it in.
12
12
  const TITLES = new Map([
@@ -153,7 +153,7 @@ function record(value, at, { state, seen, keep, add }) {
153
153
  return;
154
154
  const effort = effortOf(value.perTurnEffort) ?? effortOf(value.effort);
155
155
  // Without turns, no answer text: the request is kept for its counts, in the result and the cursor alike.
156
- const turn = { role: "assistant", text: keep ? text : "", calls, at, ...(key ? { key } : {}), ...(session.model ? { model: session.model } : {}), ...(effort ? { effort } : {}), ...(usage ? { usage } : {}), ...(failed ? { error: text || "API error" } : {}), ...mark };
156
+ const turn = { role: "assistant", text: keep ? text : "", calls, at, ...(key ? { key } : {}), ...(session.model ? { model: session.model } : {}), ...(effort ? { effort } : {}), ...(usage ? { usage } : {}), ...(failed ? { error: keep ? text || "API error" : FAILED } : {}), ...mark };
157
157
  if (key) {
158
158
  own.keyed.set(key, turn);
159
159
  seen.add(`m:${key}`);
@@ -1 +1,7 @@
1
- export declare const codex: import("../reader.js").Reader<"codex", import("../lines.js").FileCursor>;
1
+ import { type FileCursor } from "../lines.js";
2
+ import { type Reader } from "../reader.js";
3
+ /** Codex's reader: a read from a rollout's start first finds the parent's history a fork copied in (forkReplay); a read
4
+ * going on from a cursor has it in the cursor's state. Whether a read goes on is decided before reading, so each read
5
+ * reads the file once: a rollout the line reader would start over (rewritten in place), and a fork whose copy was still
6
+ * open where the last read ended and that has grown since, are read from their start with the copy found again. */
7
+ export declare const codex: Reader<"codex", FileCursor>;