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

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
@@ -112,7 +112,7 @@ const { hitRate, breaks } = await analyzeCache(session);
112
112
  | `compaction` | a compaction since the turn before: Claude Code's `compact_boundary` or `isCompactSummary` record, Codex's `compacted`. It counts in the file that holds it first; across a session's files it carries to the next file's first turn, and a later file's copy of it is never consulted |
113
113
  | `edited` | the prompt leaves the chain of the turn before (its prompt and answer) at an earlier message: `diverged` is that message's index |
114
114
  | `sidechain` | the turn opens a sidechain after a main-chain turn (Claude Code's `isSidechain`) |
115
- | `unknown` | none of the above |
115
+ | `unknown` | none of the above: the transcript shows no cause for the drop. That names no cause, the provider's included. A cache evicted early, or a request served where its cache was not, reads this way, and so does an edit the analyzer cannot see: across two of a session's files (their chains are not compared), in a Codex rollout, or in a session read without its chain |
116
116
 
117
117
  `chain: true` keeps the session's `chain` beside its turns: each record's id and its parent's (Claude Code's
118
118
  `uuid`/`parentUuid`, pi's `id`/`parentId`), a 16-hex sha256 of its content, never the content, and which record opens
package/dist/cache.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { type ParseOptions } from "./home.js";
2
2
  import { type CalculateCost, type Catalog } from "./price.js";
3
+ import { type Locator } from "./reader.js";
3
4
  import { type Source } from "./readers/index.js";
4
5
  import type { SessionOf, Usage } from "./types.js";
5
6
  /** The causes of a break, in the order they are tried: the first the transcript shows evidence of is the break's. */
@@ -80,6 +81,29 @@ export declare const CACHE_SOURCES: readonly Source[];
80
81
  * options are parseHome's but `state`: each call reads every file from its start. A source not in CACHE_SOURCES is
81
82
  * refused (RangeError) before any file is read. */
82
83
  export declare function cacheHome(home: string, { sources, readers, catalog, calculateCost, env, failures }?: Omit<ParseOptions, "state">): Promise<SessionCache[]>;
84
+ /** One file of a session analyzed alone, both ways (the cache before it written for 5 minutes or for an hour, `long`):
85
+ * which holds is known once the session's files before it are joined (sessionBreaks). With its first turn compared and
86
+ * the chain up to it (`head`), to compare with the last turn of the file before; `skipped`, the turns before that one. */
87
+ export type FileCache = {
88
+ alone: CacheReport;
89
+ long: CacheReport;
90
+ head?: Pick<SessionOf, "turns" | "chain" | "model">;
91
+ skipped: number;
92
+ };
93
+ /** `session` (one file's read, with its chain) as sessionBreaks joins it. */
94
+ export declare function fileCache(session: Pick<SessionOf, "turns" | "chain" | "model">, options?: Omit<CacheOptions, "before">): Promise<FileCache>;
95
+ /** A file of a conversation as sessionBreaks joins it: its FileCache (none for a file not read) and its requests. */
96
+ export type JoinedFile = {
97
+ file: FileCache | undefined;
98
+ requests: number;
99
+ };
100
+ /** A session's breaks: its own files (`parts`), one conversation, then each subagent's (`subagents`: each one's files,
101
+ * conversationOf's groups, in the import's order), its breaks with its `subagent` number from 1. */
102
+ export declare function sessionBreaks(parts: readonly JoinedFile[], subagents: readonly (readonly JoinedFile[])[], options?: Omit<CacheOptions, "before">): Promise<SessionCache["breaks"]>;
103
+ /** Which conversation a subagent's file holds, as a key: a thread naming its parent (a Codex subagent) is its thread, in
104
+ * however many rollouts; any other file (a Claude Code subagent's, whose name a workflow's may share a folder deeper) is
105
+ * its own. */
106
+ export declare const conversationOf: (locator: Locator, session: Pick<SessionOf, "id" | "child" | "parent">) => string;
83
107
  /** The breaks of `sessions` by cause, each cause there with 0 where none: how many, and their extra cost (USD, of the
84
108
  * priced ones); `unpriced`, the breaks whose extra cost is unknown. */
85
109
  export declare function cacheTotals(sessions: readonly Pick<SessionCache, "breaks">[]): {
package/dist/cache.js CHANGED
@@ -169,7 +169,7 @@ export async function cacheHome(home, { sources = CACHE_SOURCES, readers = READE
169
169
  // once the session's files before it are joined, and holds until the file writes the cache itself.
170
170
  const reports = new Map();
171
171
  const { tops, children, unread } = await readSource(home, source, readers, env, undefined, lookups, UNPRICED, true, undefined, async (read) => {
172
- reports.set(locatorKey(read.locator), { alone: await analyzed(read.session, priced, false), long: await analyzed(read.session, priced, true), ...headOf(read.session) });
172
+ reports.set(locatorKey(read.locator), await fileCache(read.session, priced));
173
173
  const { turns: _turns, chain: _chain, firstPrompt: _prompt, titleText: _title, sourceTitle: _source, cwd: _cwd, ...session } = read.session;
174
174
  return { locator: read.locator, session: { ...session, turns: [] }, requests: read.requests.map(({ at, usage }) => ({ role: "assistant", text: "", calls: [], at, ...(usage ? { usage } : {}) })) };
175
175
  });
@@ -183,20 +183,14 @@ export async function cacheHome(home, { sources = CACHE_SOURCES, readers = READE
183
183
  read += usage.cacheRead;
184
184
  prompt += promptOf(usage);
185
185
  }
186
- const breaks = await joined(parts, reports, priced);
187
- // Each subagent a conversation of its own, numbered from 1 in the import's order: a thread that goes on in another
188
- // rollout is one, its files joined as the session's are.
186
+ const fileOf = (part) => ({ file: reports.get(locatorKey(part.locator)), requests: requestsOf(part.read) });
187
+ // Each subagent a conversation of its own, in the import's order: a thread that goes on in another rollout is one.
189
188
  const conversations = new Map();
190
189
  for (const part of subagents) {
191
- const key = conversationOf(part);
190
+ const key = conversationOf(part.locator, part.read.session);
192
191
  (conversations.get(key) ?? conversations.set(key, []).get(key)).push(part);
193
192
  }
194
- let n = 0;
195
- for (const files of conversations.values()) {
196
- n += 1;
197
- for (const found of await joined(files, reports, priced))
198
- breaks.push({ ...found, subagent: n });
199
- }
193
+ const breaks = await sessionBreaks(parts.map(fileOf), [...conversations.values()].map((files) => files.map(fileOf)), priced);
200
194
  const first = parts[0];
201
195
  out.push({ ...keyOf(home, first.locator), source, id: first.read.session.id, hitRate: prompt > 0 ? round(read / prompt, 4) : null, breaks });
202
196
  }
@@ -206,35 +200,47 @@ export async function cacheHome(home, { sources = CACHE_SOURCES, readers = READE
206
200
  failures?.push(...failed);
207
201
  return out.sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0));
208
202
  }
209
- /** The breaks of one conversation's files, in the import's order: each file's first turn compared with the last turn
210
- * before it in the files before, its turns counted on from theirs, and a 1h cache write held until a file writes again. */
211
- async function joined(files, reports, priced) {
203
+ /** `session` (one file's read, with its chain) as sessionBreaks joins it. */
204
+ export async function fileCache(session, options = {}) {
205
+ return { alone: await analyzed(session, options, false), long: await analyzed(session, options, true), ...headOf(session) };
206
+ }
207
+ /** One conversation's breaks: its files in the import's order, each file's first turn compared with the last turn before
208
+ * it in the files before, its turns counted on from theirs, and a 1h cache write held until a file writes again. */
209
+ async function joined(files, options) {
212
210
  const breaks = [];
213
211
  let counted = 0;
214
212
  let carry;
215
- for (const part of files) {
216
- const report = reports.get(locatorKey(part.locator));
217
- if (!report)
213
+ for (const { file, requests } of files) {
214
+ if (!file)
218
215
  continue;
219
- if (carry && report.head) {
220
- const { breaks: [edge] } = await analyzeCache(report.head, { ...priced, before: carry });
216
+ if (carry && file.head) {
217
+ const { breaks: [edge], } = await analyzeCache(file.head, { ...options, before: carry });
221
218
  if (edge)
222
- breaks.push({ ...edge, turn: edge.turn + report.skipped + counted });
219
+ breaks.push({ ...edge, turn: edge.turn + file.skipped + counted });
223
220
  }
224
221
  // The file as the cache before it was written: a 1h write in an earlier file holds until this one writes.
225
- const mine = carry?.long ? report.long : report.alone;
222
+ const mine = carry?.long ? file.long : file.alone;
226
223
  for (const found of mine.breaks)
227
224
  breaks.push({ ...found, turn: found.turn + counted });
228
225
  // A file with no turn compared passes the carry on, with any compaction of its own.
229
226
  carry = mine.last ?? (carry && { ...carry, compacted: carry.compacted === true || mine.compacted });
230
- counted += requestsOf(part.read);
227
+ counted += requests;
231
228
  }
232
229
  return breaks;
233
230
  }
234
- /** Which conversation a subagent's file holds: a thread naming its parent (a Codex subagent, which groupsOf finds by that
235
- * name) is its thread, in however many rollouts; any other (a Claude Code subagent's file, which names its parent's
236
- * session) is its own. */
237
- const conversationOf = (part) => (part.read.session.child && part.read.session.parent ? `thread\0${part.read.session.id}` : `file\0${locatorKey(part.locator)}`);
231
+ /** A session's breaks: its own files (`parts`), one conversation, then each subagent's (`subagents`: each one's files,
232
+ * conversationOf's groups, in the import's order), its breaks with its `subagent` number from 1. */
233
+ export async function sessionBreaks(parts, subagents, options = {}) {
234
+ const breaks = await joined(parts, options);
235
+ for (const [n, files] of subagents.entries())
236
+ for (const found of await joined(files, options))
237
+ breaks.push({ ...found, subagent: n + 1 });
238
+ return breaks;
239
+ }
240
+ /** Which conversation a subagent's file holds, as a key: a thread naming its parent (a Codex subagent) is its thread, in
241
+ * however many rollouts; any other file (a Claude Code subagent's, whose name a workflow's may share a folder deeper) is
242
+ * its own. */
243
+ export const conversationOf = (locator, session) => (session.child && session.parent ? `thread\0${session.id}` : `file\0${locatorKey(locator)}`);
238
244
  /** The breaks of `sessions` by cause, each cause there with 0 where none: how many, and their extra cost (USD, of the
239
245
  * priced ones); `unpriced`, the breaks whose extra cost is unknown. */
240
246
  export function cacheTotals(sessions) {
package/dist/index.d.ts CHANGED
@@ -12,4 +12,4 @@ export { builtinCatalog, type CalculateCost, type Catalog, catalogOf, type Liste
12
12
  export { mirroredIn, ownRequests, sessionKey } from "./mirror.js";
13
13
  export { type HomeState, type ParseOptions, parseHome, type ReadFailure, ReadFailures, type SessionRequest, type SessionTotals, withSubagentCosts } from "./home.js";
14
14
  export { type Wrapped, wrappedHome } from "./wrapped.js";
15
- export { analyzeCache, BREAK_DROP, CACHE_SOURCES, cacheHome, cacheTotals, type SessionCache, type CacheBreak, type CacheCause, CACHE_CAUSES, type CacheOptions, type CacheReport, CACHE_TTL_LONG_MS, CACHE_TTL_MS } from "./cache.js";
15
+ export { analyzeCache, BREAK_DROP, CACHE_SOURCES, cacheHome, cacheTotals, conversationOf, fileCache, type FileCache, type JoinedFile, sessionBreaks, type SessionCache, type CacheBreak, type CacheCause, CACHE_CAUSES, type CacheOptions, type CacheReport, CACHE_TTL_LONG_MS, CACHE_TTL_MS } from "./cache.js";
package/dist/index.js CHANGED
@@ -11,4 +11,4 @@ export { builtinCatalog, catalogOf, priceRequests } from "./price.js";
11
11
  export { mirroredIn, ownRequests, sessionKey } from "./mirror.js";
12
12
  export { parseHome, ReadFailures, withSubagentCosts } from "./home.js";
13
13
  export { wrappedHome } from "./wrapped.js";
14
- export { analyzeCache, BREAK_DROP, CACHE_SOURCES, cacheHome, cacheTotals, CACHE_CAUSES, CACHE_TTL_LONG_MS, CACHE_TTL_MS } from "./cache.js";
14
+ export { analyzeCache, BREAK_DROP, CACHE_SOURCES, cacheHome, cacheTotals, conversationOf, fileCache, sessionBreaks, CACHE_CAUSES, CACHE_TTL_LONG_MS, CACHE_TTL_MS } from "./cache.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcha/agentrun-import",
3
- "version": "0.1.0-beta.61",
3
+ "version": "0.1.0-beta.63",
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",