@agent-watch-ai/edge 0.7.1 → 0.7.3
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/CHANGELOG.md +29 -0
- package/README.md +2 -1
- package/dist/capture/capture-env.d.ts +3 -3
- package/dist/capture/capture-env.js +5 -5
- package/dist/capture/captured-step.js +1 -1
- package/dist/cli/constants/cli.constants.js +1 -1
- package/dist/cli/setup.js +26 -3
- package/dist/cli/types/cli.types.d.ts +2 -0
- package/dist/cli/uninstall.js +1 -1
- package/dist/cli.js +7 -4
- package/dist/config/config.d.ts +8 -0
- package/dist/config/config.js +10 -0
- package/dist/config/constants/config.constants.d.ts +5 -0
- package/dist/config/constants/config.constants.js +5 -0
- package/dist/config/repo-config.js +7 -2
- package/dist/config/schemas/config.schema.d.ts +4 -0
- package/dist/config/schemas/config.schema.js +7 -1
- package/dist/enrollment/enrollment.js +7 -4
- package/dist/pipeline/hook-pipeline.js +3 -1
- package/dist/providers/antigravity/antigravity.adapter.d.ts +14 -0
- package/dist/providers/antigravity/antigravity.adapter.js +86 -18
- package/dist/providers/antigravity/antigravity.provider.js +7 -4
- package/dist/providers/antigravity/constants/antigravity.constants.d.ts +10 -0
- package/dist/providers/antigravity/constants/antigravity.constants.js +10 -0
- package/dist/providers/antigravity/schemas/antigravity.schema.js +7 -13
- package/dist/providers/claude/claude.otel.d.ts +33 -1
- package/dist/providers/claude/claude.otel.js +81 -12
- package/dist/providers/claude/claude.provider.js +3 -1
- package/dist/providers/codex/codex.capture.d.ts +1 -1
- package/dist/providers/codex/codex.capture.js +3 -3
- package/dist/providers/codex/codex.otel.js +2 -2
- package/dist/providers/codex/codex.provider.js +2 -1
- package/dist/providers/gemini/gemini.capture.js +2 -2
- package/dist/providers/gemini/gemini.otel.js +2 -2
- package/dist/providers/gemini/gemini.provider.js +2 -1
- package/dist/providers/shared/terminal-capture.js +3 -3
- package/dist/turns/antigravity-transcript.d.ts +40 -0
- package/dist/turns/antigravity-transcript.js +102 -0
- package/dist/turns/claude-transcript.d.ts +35 -1
- package/dist/turns/claude-transcript.js +185 -2
- package/dist/turns/codex-transcript.d.ts +22 -1
- package/dist/turns/codex-transcript.js +161 -2
- package/dist/turns/constants/turns.constants.d.ts +51 -0
- package/dist/turns/constants/turns.constants.js +51 -0
- package/dist/turns/gemini-transcript.d.ts +17 -1
- package/dist/turns/gemini-transcript.js +36 -2
- package/dist/turns/transcript.d.ts +31 -1
- package/dist/turns/transcript.js +74 -0
- package/dist/turns/turn-state.d.ts +18 -1
- package/dist/turns/turn-state.js +45 -10
- package/dist/turns/turn-tracker.js +154 -16
- package/dist/turns/types/transcript.types.d.ts +25 -0
- package/dist/turns/types/turn-provider.types.d.ts +18 -1
- package/dist/turns/types/turn-state.types.d.ts +10 -0
- package/docs/DATA_HANDLING.md +3 -0
- package/package.json +1 -1
|
@@ -2,7 +2,7 @@ import fs from 'node:fs/promises';
|
|
|
2
2
|
import { existsSync } from 'node:fs';
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import { copiedShims, inspectCaptureEnv, installedCaptureEnv, removeCaptureCopy, shimPaths, skippedOnError, withCaptureOutcome, withoutCaptureEnv } from '../../capture/capture-env.js';
|
|
5
|
-
import {
|
|
5
|
+
import { runCaptureEnabled } from '../../config/config.js';
|
|
6
6
|
import { asRecord, omitKeys } from '../../core/object.js';
|
|
7
7
|
import { backupFile } from '../../storage/atomic-file.js';
|
|
8
8
|
import { readJsonFile } from '../../storage/json-file.js';
|
|
@@ -97,12 +97,12 @@ export function terminalCapture(app, verified, source = shimPaths()) {
|
|
|
97
97
|
if (read.state === 'invalid')
|
|
98
98
|
return { on: false, reason: `${file} is not plain JSON, so Edge cannot edit it` };
|
|
99
99
|
const block = asRecord(asRecord(read.state === 'ok' ? read.value : undefined)?.[terminalEnvKey(context.env)]) ?? {};
|
|
100
|
-
return inspectCaptureEnv(block, context.config,
|
|
100
|
+
return inspectCaptureEnv(block, context.config, runCaptureEnabled(context.config), context.paths.dataDir, source);
|
|
101
101
|
}
|
|
102
102
|
},
|
|
103
103
|
install: (inner) => async (context) => {
|
|
104
104
|
const outcome = await inner(context);
|
|
105
|
-
return withCaptureOutcome(outcome, await (verified &&
|
|
105
|
+
return withCaptureOutcome(outcome, await (verified && runCaptureEnabled(context.config) ? skippedOnError(apply(context)) : remove(context)));
|
|
106
106
|
},
|
|
107
107
|
uninstall: (inner) => async (context) => withCaptureOutcome(await inner(context), await remove(context))
|
|
108
108
|
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ReadTurnUsageRetry, TranscriptUsageEntry, TurnUsage } from './types/transcript.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Per-turn token usage from an Antigravity transcript
|
|
4
|
+
* (`<app dir>/brain/<conversationId>/.system_generated/logs/transcript_full.jsonl`).
|
|
5
|
+
*
|
|
6
|
+
* Antigravity has no OTel export and no usage API, so this file is its only
|
|
7
|
+
* money source. One line per step; model steps (`source: "MODEL"`) carry
|
|
8
|
+
* `step_index`, `created_at`, `model`, `input_tokens`, `cache_read_tokens` and
|
|
9
|
+
* `output_tokens` (agy 1.5.0). Only those fields are read; content, thinking
|
|
10
|
+
* and tool calls are never looked at, so nothing a developer wrote leaves here.
|
|
11
|
+
*
|
|
12
|
+
* Older builds wrote the tokens without `model` (the summary then keeps the
|
|
13
|
+
* hook's model), and older still no tokens at all: a step without any count is
|
|
14
|
+
* skipped, so such a turn stays `pending` rather than reporting zero.
|
|
15
|
+
*
|
|
16
|
+
* Exactly once: each step is claimed by its `step_index`, unique within the
|
|
17
|
+
* conversation, through the tracker's claim ledger under the session usage lock.
|
|
18
|
+
*
|
|
19
|
+
* ponytail: `created_at` is whole seconds, so the lower bound is floored to the
|
|
20
|
+
* second, or a step written in the prompt's own second would be missed. An
|
|
21
|
+
* unclaimed step of the previous turn from that same second can join this one;
|
|
22
|
+
* claimed steps never do. The upper bound stays exact: a racing next prompt's
|
|
23
|
+
* step stamped in that prompt's own second goes to this turn (still once).
|
|
24
|
+
* Same 4 MiB tail ceiling as the other readers.
|
|
25
|
+
*
|
|
26
|
+
* @param transcriptPath - The transcript the hook named, or the brain-directory fallback.
|
|
27
|
+
* @param sinceIso - When the turn started.
|
|
28
|
+
* @param retry - How long to wait for the transcript flush to settle.
|
|
29
|
+
* @param untilIso - Upper bound, keeping a racing next prompt's steps out.
|
|
30
|
+
* @param excludeMessageIds - Steps another turn already claimed.
|
|
31
|
+
* @returns The turn's usage, or undefined when the transcript reports none.
|
|
32
|
+
*/
|
|
33
|
+
export declare function readAntigravityTurnUsage(transcriptPath: string, sinceIso: string, retry?: ReadTurnUsageRetry, untilIso?: string, excludeMessageIds?: ReadonlySet<string>): Promise<TurnUsage | undefined>;
|
|
34
|
+
/**
|
|
35
|
+
* The usage one transcript step carries, when it is a model step with counts.
|
|
36
|
+
*
|
|
37
|
+
* @param row - One parsed transcript line.
|
|
38
|
+
* @returns The step's model and counts under the transcript field names, or undefined.
|
|
39
|
+
*/
|
|
40
|
+
export declare function antigravityStepUsage(row: Readonly<Record<string, unknown>>): TranscriptUsageEntry | undefined;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { firstStringOf } from '../core/object.js';
|
|
2
|
+
import { accumulateUsage, messageIdFor, parseJsonLine, readUntilSettled, readWindowedTail } from './transcript.js';
|
|
3
|
+
import { ANTIGRAVITY_CACHE_READ_TOKENS, ANTIGRAVITY_COUNT_FIELDS, ANTIGRAVITY_INPUT_TOKENS, ANTIGRAVITY_MODEL_SOURCE, ANTIGRAVITY_OUTPUT_TOKENS, ANTIGRAVITY_STEP_ID_PREFIX, ANTIGRAVITY_TIMESTAMP_RESOLUTION_MS, TRANSCRIPT_CACHE_READ_TOKENS, TRANSCRIPT_INPUT_TOKENS, TRANSCRIPT_OUTPUT_TOKENS } from './constants/turns.constants.js';
|
|
4
|
+
/**
|
|
5
|
+
* Per-turn token usage from an Antigravity transcript
|
|
6
|
+
* (`<app dir>/brain/<conversationId>/.system_generated/logs/transcript_full.jsonl`).
|
|
7
|
+
*
|
|
8
|
+
* Antigravity has no OTel export and no usage API, so this file is its only
|
|
9
|
+
* money source. One line per step; model steps (`source: "MODEL"`) carry
|
|
10
|
+
* `step_index`, `created_at`, `model`, `input_tokens`, `cache_read_tokens` and
|
|
11
|
+
* `output_tokens` (agy 1.5.0). Only those fields are read; content, thinking
|
|
12
|
+
* and tool calls are never looked at, so nothing a developer wrote leaves here.
|
|
13
|
+
*
|
|
14
|
+
* Older builds wrote the tokens without `model` (the summary then keeps the
|
|
15
|
+
* hook's model), and older still no tokens at all: a step without any count is
|
|
16
|
+
* skipped, so such a turn stays `pending` rather than reporting zero.
|
|
17
|
+
*
|
|
18
|
+
* Exactly once: each step is claimed by its `step_index`, unique within the
|
|
19
|
+
* conversation, through the tracker's claim ledger under the session usage lock.
|
|
20
|
+
*
|
|
21
|
+
* ponytail: `created_at` is whole seconds, so the lower bound is floored to the
|
|
22
|
+
* second, or a step written in the prompt's own second would be missed. An
|
|
23
|
+
* unclaimed step of the previous turn from that same second can join this one;
|
|
24
|
+
* claimed steps never do. The upper bound stays exact: a racing next prompt's
|
|
25
|
+
* step stamped in that prompt's own second goes to this turn (still once).
|
|
26
|
+
* Same 4 MiB tail ceiling as the other readers.
|
|
27
|
+
*
|
|
28
|
+
* @param transcriptPath - The transcript the hook named, or the brain-directory fallback.
|
|
29
|
+
* @param sinceIso - When the turn started.
|
|
30
|
+
* @param retry - How long to wait for the transcript flush to settle.
|
|
31
|
+
* @param untilIso - Upper bound, keeping a racing next prompt's steps out.
|
|
32
|
+
* @param excludeMessageIds - Steps another turn already claimed.
|
|
33
|
+
* @returns The turn's usage, or undefined when the transcript reports none.
|
|
34
|
+
*/
|
|
35
|
+
export async function readAntigravityTurnUsage(transcriptPath, sinceIso, retry, untilIso, excludeMessageIds) {
|
|
36
|
+
return readUntilSettled(() => readOnce(transcriptPath, sinceIso, untilIso, excludeMessageIds), retry);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* One pass over the transcript tail.
|
|
40
|
+
*
|
|
41
|
+
* @param transcriptPath - Transcript file.
|
|
42
|
+
* @param sinceIso - Lower bound of the turn's window.
|
|
43
|
+
* @param untilIso - Upper bound, or undefined for "no upper bound".
|
|
44
|
+
* @param excludeMessageIds - Steps another turn already claimed.
|
|
45
|
+
* @returns The usage found in this pass.
|
|
46
|
+
*/
|
|
47
|
+
async function readOnce(transcriptPath, sinceIso, untilIso, excludeMessageIds) {
|
|
48
|
+
const tail = await readWindowedTail(transcriptPath, sinceIso, untilIso);
|
|
49
|
+
if (tail === undefined)
|
|
50
|
+
return undefined;
|
|
51
|
+
const { raw, until } = tail;
|
|
52
|
+
const since = Math.floor(tail.since / ANTIGRAVITY_TIMESTAMP_RESOLUTION_MS) * ANTIGRAVITY_TIMESTAMP_RESOLUTION_MS;
|
|
53
|
+
const byStep = new Map();
|
|
54
|
+
for (const line of raw.split('\n')) {
|
|
55
|
+
// Cheap prefilter: user, tool and system steps carry no counts.
|
|
56
|
+
if (!ANTIGRAVITY_COUNT_FIELDS.some((field) => line.includes(`"${field}"`)))
|
|
57
|
+
continue;
|
|
58
|
+
const row = parseJsonLine(line);
|
|
59
|
+
const entry = row ? antigravityStepUsage(row) : undefined;
|
|
60
|
+
const timestamp = typeof row?.['created_at'] === 'string' ? Date.parse(row['created_at']) : NaN;
|
|
61
|
+
if (!row || !entry || Number.isNaN(timestamp) || timestamp < since || timestamp > until)
|
|
62
|
+
continue;
|
|
63
|
+
const step = row['step_index'];
|
|
64
|
+
const id = Number.isInteger(step) ? `${ANTIGRAVITY_STEP_ID_PREFIX}${String(step)}` : messageIdFor(undefined, line);
|
|
65
|
+
if (excludeMessageIds?.has(id))
|
|
66
|
+
continue;
|
|
67
|
+
byStep.set(id, entry);
|
|
68
|
+
}
|
|
69
|
+
return accumulateUsage(byStep);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The usage one transcript step carries, when it is a model step with counts.
|
|
73
|
+
*
|
|
74
|
+
* @param row - One parsed transcript line.
|
|
75
|
+
* @returns The step's model and counts under the transcript field names, or undefined.
|
|
76
|
+
*/
|
|
77
|
+
export function antigravityStepUsage(row) {
|
|
78
|
+
if (row['source'] !== ANTIGRAVITY_MODEL_SOURCE)
|
|
79
|
+
return undefined;
|
|
80
|
+
const input = finiteOrUndefined(row[ANTIGRAVITY_INPUT_TOKENS]);
|
|
81
|
+
const output = finiteOrUndefined(row[ANTIGRAVITY_OUTPUT_TOKENS]);
|
|
82
|
+
const cacheRead = finiteOrUndefined(row[ANTIGRAVITY_CACHE_READ_TOKENS]);
|
|
83
|
+
if (input === undefined && output === undefined && cacheRead === undefined)
|
|
84
|
+
return undefined;
|
|
85
|
+
return {
|
|
86
|
+
model: firstStringOf(row['model']),
|
|
87
|
+
usage: {
|
|
88
|
+
[TRANSCRIPT_INPUT_TOKENS]: input,
|
|
89
|
+
[TRANSCRIPT_OUTPUT_TOKENS]: output,
|
|
90
|
+
[TRANSCRIPT_CACHE_READ_TOKENS]: cacheRead
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* A count as a number, or undefined when it is missing or not a finite number.
|
|
96
|
+
*
|
|
97
|
+
* @param value - Candidate count.
|
|
98
|
+
* @returns The finite number, or undefined.
|
|
99
|
+
*/
|
|
100
|
+
function finiteOrUndefined(value) {
|
|
101
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
|
|
102
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ReadTurnUsageRetry, SessionTitles, TurnUsage } from './types/transcript.types.js';
|
|
1
|
+
import type { ReadTurnUsageRetry, SessionTitles, SubagentTurn, TurnUsage } from './types/transcript.types.js';
|
|
2
2
|
export type { ReadTurnUsageRetry, TranscriptUsageEntry, TurnUsage } from './types/transcript.types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Authoritative per-turn token usage from Claude Code's transcript JSONL.
|
|
@@ -16,6 +16,40 @@ export type { ReadTurnUsageRetry, TranscriptUsageEntry, TurnUsage } from './type
|
|
|
16
16
|
* @returns The turn's usage, or undefined when the transcript reports none.
|
|
17
17
|
*/
|
|
18
18
|
export declare function readTurnUsage(transcriptPath: string, sinceIso: string, retry?: ReadTurnUsageRetry, untilIso?: string, excludeMessageIds?: ReadonlySet<string>): Promise<TurnUsage | undefined>;
|
|
19
|
+
/**
|
|
20
|
+
* The usage a turn's subagents recorded, out of their own transcripts.
|
|
21
|
+
*
|
|
22
|
+
* Claude Code writes each subagent to `<session>/subagents/agent-<id>.jsonl`,
|
|
23
|
+
* never to the session's own transcript. A subagent's user lines name the
|
|
24
|
+
* parent's prompt id, the one that launched it or a later one that resumed it,
|
|
25
|
+
* so each assistant line belongs to the prompt id of the user line above it.
|
|
26
|
+
* Lines before the first such line (a fork's copy of the parent's last reply)
|
|
27
|
+
* are no one's here; the parent's own read counts that message. A file longer
|
|
28
|
+
* than the tail takes the last prompt id stated before the tail.
|
|
29
|
+
*
|
|
30
|
+
* A Stop reads every subagent file written since the turn began; a subagent's
|
|
31
|
+
* own stop reads only that subagent's file. One pass, no settle loop: a
|
|
32
|
+
* foreground subagent has stopped before its parent does, and a background one
|
|
33
|
+
* re-reads on its own stop. Never throws.
|
|
34
|
+
*
|
|
35
|
+
* @param payload - The raw hook payload.
|
|
36
|
+
* @param turn - The parent turn; its window is not used, the prompt id is exact.
|
|
37
|
+
* @param excludeMessageIds - Messages already claimed.
|
|
38
|
+
* @returns The subagents' usage in this turn, or undefined when there is none.
|
|
39
|
+
*/
|
|
40
|
+
export declare function readClaudeSubagentUsage(payload: unknown, turn: SubagentTurn, excludeMessageIds: ReadonlySet<string>): Promise<TurnUsage | undefined>;
|
|
41
|
+
/**
|
|
42
|
+
* The turns a stopping subagent's lines name, oldest first: the prompt that
|
|
43
|
+
* launched it and any that resumed it.
|
|
44
|
+
*
|
|
45
|
+
* A `SubagentStop` names the session's current prompt (Claude Code 2.1.325),
|
|
46
|
+
* not the one that launched the subagent, so the turn a background subagent's
|
|
47
|
+
* late usage belongs to is read from its own lines.
|
|
48
|
+
*
|
|
49
|
+
* @param payload - The subagent's stop payload.
|
|
50
|
+
* @returns The prompt ids; empty when the payload names no subagent or its file cannot be read.
|
|
51
|
+
*/
|
|
52
|
+
export declare function readClaudeSubagentTurnIds(payload: unknown): Promise<string[]>;
|
|
19
53
|
/**
|
|
20
54
|
* The session's titles, from the tail of Claude Code's transcript.
|
|
21
55
|
*
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
1
3
|
import { asRecord, compact } from '../core/object.js';
|
|
2
|
-
import { CLAUDE_CUSTOM_TITLE_FIELDS, CLAUDE_GENERATED_TITLE_FIELDS, RE_CLAUDE_TITLE_LINE } from './constants/turns.constants.js';
|
|
3
|
-
import { accumulateUsage, messageIdFor, parseJsonLine, readTranscriptTail, readUntilSettled, readWindowedTail } from './transcript.js';
|
|
4
|
+
import { CLAUDE_CUSTOM_TITLE_FIELDS, CLAUDE_GENERATED_TITLE_FIELDS, CLAUDE_SUBAGENTS_DIR, PROMPT_SEED_CHUNK_BYTES, PROMPT_SEED_OVERLAP_BYTES, PROMPT_SEED_SCAN_BYTES, RE_CLAUDE_AGENT_ID, RE_CLAUDE_PROMPT_ID, RE_CLAUDE_SUBAGENT_FILE, RE_CLAUDE_TITLE_LINE, TRANSCRIPT_PATH_KEY } from './constants/turns.constants.js';
|
|
5
|
+
import { accumulateUsage, filesWrittenSince, messageIdFor, parseJsonLine, readTailSnapshot, readTranscriptTail, readUntilSettled, readWindowedTail } from './transcript.js';
|
|
4
6
|
/**
|
|
5
7
|
* Authoritative per-turn token usage from Claude Code's transcript JSONL.
|
|
6
8
|
*
|
|
@@ -71,6 +73,187 @@ function assistantUsageEntry(line, since, until, excludeMessageIds) {
|
|
|
71
73
|
const model = typeof message?.['model'] === 'string' ? message['model'] : undefined;
|
|
72
74
|
return { id, value: { model, usage } };
|
|
73
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* The usage a turn's subagents recorded, out of their own transcripts.
|
|
78
|
+
*
|
|
79
|
+
* Claude Code writes each subagent to `<session>/subagents/agent-<id>.jsonl`,
|
|
80
|
+
* never to the session's own transcript. A subagent's user lines name the
|
|
81
|
+
* parent's prompt id, the one that launched it or a later one that resumed it,
|
|
82
|
+
* so each assistant line belongs to the prompt id of the user line above it.
|
|
83
|
+
* Lines before the first such line (a fork's copy of the parent's last reply)
|
|
84
|
+
* are no one's here; the parent's own read counts that message. A file longer
|
|
85
|
+
* than the tail takes the last prompt id stated before the tail.
|
|
86
|
+
*
|
|
87
|
+
* A Stop reads every subagent file written since the turn began; a subagent's
|
|
88
|
+
* own stop reads only that subagent's file. One pass, no settle loop: a
|
|
89
|
+
* foreground subagent has stopped before its parent does, and a background one
|
|
90
|
+
* re-reads on its own stop. Never throws.
|
|
91
|
+
*
|
|
92
|
+
* @param payload - The raw hook payload.
|
|
93
|
+
* @param turn - The parent turn; its window is not used, the prompt id is exact.
|
|
94
|
+
* @param excludeMessageIds - Messages already claimed.
|
|
95
|
+
* @returns The subagents' usage in this turn, or undefined when there is none.
|
|
96
|
+
*/
|
|
97
|
+
export async function readClaudeSubagentUsage(payload, turn, excludeMessageIds) {
|
|
98
|
+
const { turnId, startedAt } = turn;
|
|
99
|
+
// No prompt id (Claude Code before 2.1.196): no subagent line can be matched to the turn.
|
|
100
|
+
if (turnId === undefined)
|
|
101
|
+
return undefined;
|
|
102
|
+
try {
|
|
103
|
+
const files = await subagentFiles(asRecord(payload), Date.parse(startedAt));
|
|
104
|
+
const byMessageId = new Map();
|
|
105
|
+
for (const file of files) {
|
|
106
|
+
const tail = await subagentTail(file);
|
|
107
|
+
// Most files are another turn's: skip them without a parse.
|
|
108
|
+
if (!tail || (tail.seed !== turnId && !tail.raw.includes(turnId)))
|
|
109
|
+
continue;
|
|
110
|
+
for (const { id, value } of subagentEntries(tail.raw, turnId, excludeMessageIds, tail.seed))
|
|
111
|
+
byMessageId.set(id, value);
|
|
112
|
+
}
|
|
113
|
+
return accumulateUsage(byMessageId);
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* The turns a stopping subagent's lines name, oldest first: the prompt that
|
|
121
|
+
* launched it and any that resumed it.
|
|
122
|
+
*
|
|
123
|
+
* A `SubagentStop` names the session's current prompt (Claude Code 2.1.325),
|
|
124
|
+
* not the one that launched the subagent, so the turn a background subagent's
|
|
125
|
+
* late usage belongs to is read from its own lines.
|
|
126
|
+
*
|
|
127
|
+
* @param payload - The subagent's stop payload.
|
|
128
|
+
* @returns The prompt ids; empty when the payload names no subagent or its file cannot be read.
|
|
129
|
+
*/
|
|
130
|
+
export async function readClaudeSubagentTurnIds(payload) {
|
|
131
|
+
const record = asRecord(payload);
|
|
132
|
+
// Only a subagent's own stop: a Stop would list every subagent of the session.
|
|
133
|
+
if (typeof record?.['agent_id'] !== 'string')
|
|
134
|
+
return [];
|
|
135
|
+
try {
|
|
136
|
+
const ids = [];
|
|
137
|
+
for (const file of await subagentFiles(record, Number.NaN)) {
|
|
138
|
+
const tail = await subagentTail(file);
|
|
139
|
+
if (!tail)
|
|
140
|
+
continue;
|
|
141
|
+
if (tail.seed !== undefined)
|
|
142
|
+
ids.push(tail.seed);
|
|
143
|
+
for (const match of tail.raw.matchAll(RE_CLAUDE_PROMPT_ID))
|
|
144
|
+
ids.push(match[1]);
|
|
145
|
+
}
|
|
146
|
+
return [...new Set(ids)];
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
return [];
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* A subagent transcript's tail with its seed, both from one size of the file.
|
|
154
|
+
*
|
|
155
|
+
* @param file - A subagent transcript.
|
|
156
|
+
* @returns The tail, or undefined when the file cannot be read.
|
|
157
|
+
*/
|
|
158
|
+
async function subagentTail(file) {
|
|
159
|
+
const tail = await readTailSnapshot(file);
|
|
160
|
+
if (!tail)
|
|
161
|
+
return undefined;
|
|
162
|
+
return { raw: tail.raw, seed: tail.start > 0 ? await promptIdBefore(file, tail.start) : undefined };
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* The last prompt id stated before a byte offset, read backwards a chunk at a time.
|
|
166
|
+
*
|
|
167
|
+
* ponytail: gives up PROMPT_SEED_SCAN_BYTES back; a subagent that ran longer
|
|
168
|
+
* than that on one prompt loses its first tail lines until the next prompt line.
|
|
169
|
+
*
|
|
170
|
+
* @param file - A subagent transcript.
|
|
171
|
+
* @param before - Where the tail starts.
|
|
172
|
+
* @returns The prompt id, or undefined when none is found.
|
|
173
|
+
*/
|
|
174
|
+
async function promptIdBefore(file, before) {
|
|
175
|
+
const handle = await fs.open(file, 'r');
|
|
176
|
+
const floor = Math.max(0, before - PROMPT_SEED_SCAN_BYTES);
|
|
177
|
+
try {
|
|
178
|
+
for (let end = before; end > floor;) {
|
|
179
|
+
const start = Math.max(floor, end - PROMPT_SEED_CHUNK_BYTES);
|
|
180
|
+
const buffer = Buffer.allocUnsafe(end - start);
|
|
181
|
+
const { bytesRead } = await handle.read(buffer, 0, end - start, start);
|
|
182
|
+
const found = lastPromptId(buffer.subarray(0, bytesRead).toString('utf8'));
|
|
183
|
+
if (found !== undefined || start === floor)
|
|
184
|
+
return found;
|
|
185
|
+
end = start + PROMPT_SEED_OVERLAP_BYTES;
|
|
186
|
+
}
|
|
187
|
+
return undefined;
|
|
188
|
+
}
|
|
189
|
+
finally {
|
|
190
|
+
await handle.close();
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* The last prompt id a run of transcript text states.
|
|
195
|
+
*
|
|
196
|
+
* @param text - Transcript text, possibly cut mid-line at either end.
|
|
197
|
+
* @returns The id, or undefined when it states none.
|
|
198
|
+
*/
|
|
199
|
+
function lastPromptId(text) {
|
|
200
|
+
let last;
|
|
201
|
+
for (const match of text.matchAll(RE_CLAUDE_PROMPT_ID))
|
|
202
|
+
last = match[1];
|
|
203
|
+
return last;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* The subagent transcripts one hook should read.
|
|
207
|
+
*
|
|
208
|
+
* @param payload - The raw hook payload.
|
|
209
|
+
* @param since - Turn start, epoch ms; a file untouched since holds nothing of the turn.
|
|
210
|
+
* @returns Absolute paths; empty when the payload names no transcript.
|
|
211
|
+
*/
|
|
212
|
+
async function subagentFiles(payload, since) {
|
|
213
|
+
const transcriptPath = payload?.[TRANSCRIPT_PATH_KEY];
|
|
214
|
+
const own = payload?.['agent_transcript_path'];
|
|
215
|
+
// A subagent's own stop: its file alone, as named, else derived from its id.
|
|
216
|
+
if (typeof own === 'string' && own)
|
|
217
|
+
return [own];
|
|
218
|
+
if (typeof transcriptPath !== 'string' || !transcriptPath.endsWith('.jsonl'))
|
|
219
|
+
return [];
|
|
220
|
+
const dir = path.join(transcriptPath.slice(0, -'.jsonl'.length), CLAUDE_SUBAGENTS_DIR);
|
|
221
|
+
const agentId = payload?.['agent_id'];
|
|
222
|
+
if (typeof agentId === 'string')
|
|
223
|
+
return RE_CLAUDE_AGENT_ID.test(agentId) ? [path.join(dir, `agent-${agentId}.jsonl`)] : [];
|
|
224
|
+
return filesWrittenSince(dir, RE_CLAUDE_SUBAGENT_FILE, since);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* One subagent transcript's usage entries of a turn.
|
|
228
|
+
*
|
|
229
|
+
* @param raw - The transcript tail.
|
|
230
|
+
* @param turnId - The parent turn.
|
|
231
|
+
* @param excludeMessageIds - Messages already claimed.
|
|
232
|
+
* @param seed - The prompt the tail's first lines ran under, when the tail is cut.
|
|
233
|
+
* @returns The entries, in file order; a repeated message id repeats.
|
|
234
|
+
*/
|
|
235
|
+
function subagentEntries(raw, turnId, excludeMessageIds, seed) {
|
|
236
|
+
const entries = [];
|
|
237
|
+
let promptId = seed;
|
|
238
|
+
for (const line of raw.split('\n')) {
|
|
239
|
+
if (!line.trim())
|
|
240
|
+
continue;
|
|
241
|
+
const entry = parseJsonLine(line);
|
|
242
|
+
if (entry?.['type'] === 'user' && typeof entry['promptId'] === 'string')
|
|
243
|
+
promptId = entry['promptId'];
|
|
244
|
+
if (entry?.['type'] !== 'assistant' || promptId !== turnId)
|
|
245
|
+
continue;
|
|
246
|
+
const message = asRecord(entry['message']);
|
|
247
|
+
const usage = asRecord(message?.['usage']);
|
|
248
|
+
if (!usage)
|
|
249
|
+
continue;
|
|
250
|
+
const id = messageIdFor(typeof message?.['id'] === 'string' ? message['id'] : undefined, line);
|
|
251
|
+
if (excludeMessageIds.has(id))
|
|
252
|
+
continue;
|
|
253
|
+
entries.push({ id, value: { model: typeof message?.['model'] === 'string' ? message['model'] : undefined, usage } });
|
|
254
|
+
}
|
|
255
|
+
return entries;
|
|
256
|
+
}
|
|
74
257
|
/**
|
|
75
258
|
* The session's titles, from the tail of Claude Code's transcript.
|
|
76
259
|
*
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ReadTurnUsageRetry, TurnUsage } from './types/transcript.types.js';
|
|
1
|
+
import type { ReadTurnUsageRetry, SubagentTurn, TurnUsage } from './types/transcript.types.js';
|
|
2
2
|
/**
|
|
3
3
|
* Per-turn token usage from a Codex rollout (`~/.codex/sessions/…/rollout-*.jsonl`).
|
|
4
4
|
*
|
|
@@ -28,6 +28,27 @@ import type { ReadTurnUsageRetry, TurnUsage } from './types/transcript.types.js'
|
|
|
28
28
|
* @returns The turn's usage, or undefined when the rollout reports none.
|
|
29
29
|
*/
|
|
30
30
|
export declare function readCodexTurnUsage(transcriptPath: string, sinceIso: string, retry?: ReadTurnUsageRetry, untilIso?: string, excludeMessageIds?: ReadonlySet<string>): Promise<TurnUsage | undefined>;
|
|
31
|
+
/**
|
|
32
|
+
* The usage of the session's subagent threads inside the turn's window, out of
|
|
33
|
+
* their own rollouts.
|
|
34
|
+
*
|
|
35
|
+
* A `spawn_agent` child is a thread of its own, writing its own rollout beside
|
|
36
|
+
* the parent's, whose `session_meta` names the parent (`source.subagent.
|
|
37
|
+
* thread_spawn.parent_thread_id`). It fires no Stop and no prompt hook (only
|
|
38
|
+
* SubagentStart and SubagentStop on the parent's session, verified on
|
|
39
|
+
* codex-cli 0.166.0), and its own turn ids are not the parent's, so its rows
|
|
40
|
+
* join the parent's turn by window. Children of children are followed. Never throws.
|
|
41
|
+
*
|
|
42
|
+
* ponytail: rows a background child writes after the parent's Stop and before
|
|
43
|
+
* its next prompt fall in no window and stay uncounted; native OTel still has
|
|
44
|
+
* them. Upgrade path: a re-send on that child's SubagentStop, as Claude does.
|
|
45
|
+
*
|
|
46
|
+
* @param payload - The raw hook payload, naming the parent rollout and session.
|
|
47
|
+
* @param turn - The parent turn's window.
|
|
48
|
+
* @param excludeMessageIds - Rows already claimed.
|
|
49
|
+
* @returns The children's usage in the window, or undefined when there is none.
|
|
50
|
+
*/
|
|
51
|
+
export declare function readCodexSubagentUsage(payload: unknown, turn: SubagentTurn, excludeMessageIds: ReadonlySet<string>): Promise<TurnUsage | undefined>;
|
|
31
52
|
/**
|
|
32
53
|
* The counts one rollout row carries, when it is an `event_msg` / `token_count` row.
|
|
33
54
|
*
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
1
3
|
import { asRecord, firstStringOf } from '../core/object.js';
|
|
2
|
-
import { accumulateUsage, messageIdFor, parseJsonLine, readUntilSettled, readWindowedTail } from './transcript.js';
|
|
3
|
-
import { CODEX_CACHED_INPUT_TOKENS, CODEX_LAST_TOKEN_USAGE, CODEX_TOKEN_COUNT, CODEX_TURN_CONTEXT, TRANSCRIPT_CACHE_READ_TOKENS, TRANSCRIPT_INPUT_TOKENS, TRANSCRIPT_OUTPUT_TOKENS } from './constants/turns.constants.js';
|
|
4
|
+
import { accumulateUsage, mergeUsage, messageIdFor, parseJsonLine, readUntilSettled, readWindowedTail } from './transcript.js';
|
|
5
|
+
import { CODEX_CACHED_INPUT_TOKENS, CODEX_MAX_SCAN_DAYS, CODEX_MAX_SPAWN_DEPTH, CODEX_META_HEAD_BYTES, DAY_MS, RE_CODEX_ROLLOUT, TRANSCRIPT_PATH_KEY, CODEX_LAST_TOKEN_USAGE, CODEX_TOKEN_COUNT, CODEX_TURN_CONTEXT, TRANSCRIPT_CACHE_READ_TOKENS, TRANSCRIPT_INPUT_TOKENS, TRANSCRIPT_OUTPUT_TOKENS } from './constants/turns.constants.js';
|
|
4
6
|
/**
|
|
5
7
|
* Per-turn token usage from a Codex rollout (`~/.codex/sessions/…/rollout-*.jsonl`).
|
|
6
8
|
*
|
|
@@ -74,6 +76,163 @@ async function readOnce(transcriptPath, sinceIso, untilIso, excludeMessageIds) {
|
|
|
74
76
|
}
|
|
75
77
|
return accumulateUsage(byRowId);
|
|
76
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* The usage of the session's subagent threads inside the turn's window, out of
|
|
81
|
+
* their own rollouts.
|
|
82
|
+
*
|
|
83
|
+
* A `spawn_agent` child is a thread of its own, writing its own rollout beside
|
|
84
|
+
* the parent's, whose `session_meta` names the parent (`source.subagent.
|
|
85
|
+
* thread_spawn.parent_thread_id`). It fires no Stop and no prompt hook (only
|
|
86
|
+
* SubagentStart and SubagentStop on the parent's session, verified on
|
|
87
|
+
* codex-cli 0.166.0), and its own turn ids are not the parent's, so its rows
|
|
88
|
+
* join the parent's turn by window. Children of children are followed. Never throws.
|
|
89
|
+
*
|
|
90
|
+
* ponytail: rows a background child writes after the parent's Stop and before
|
|
91
|
+
* its next prompt fall in no window and stay uncounted; native OTel still has
|
|
92
|
+
* them. Upgrade path: a re-send on that child's SubagentStop, as Claude does.
|
|
93
|
+
*
|
|
94
|
+
* @param payload - The raw hook payload, naming the parent rollout and session.
|
|
95
|
+
* @param turn - The parent turn's window.
|
|
96
|
+
* @param excludeMessageIds - Rows already claimed.
|
|
97
|
+
* @returns The children's usage in the window, or undefined when there is none.
|
|
98
|
+
*/
|
|
99
|
+
export async function readCodexSubagentUsage(payload, turn, excludeMessageIds) {
|
|
100
|
+
const record = asRecord(payload);
|
|
101
|
+
const transcriptPath = record?.[TRANSCRIPT_PATH_KEY];
|
|
102
|
+
// Codex has named the session both ways, as the adapter reads it.
|
|
103
|
+
const sessionId = firstStringOf(record?.['session_id'], record?.['thread_id']);
|
|
104
|
+
if (typeof transcriptPath !== 'string' || !sessionId)
|
|
105
|
+
return undefined;
|
|
106
|
+
try {
|
|
107
|
+
let usage;
|
|
108
|
+
for (const child of await childRollouts(transcriptPath, sessionId, Date.parse(turn.startedAt))) {
|
|
109
|
+
usage = mergeUsage(usage, await readOnce(child, turn.startedAt, turn.untilIso, excludeMessageIds));
|
|
110
|
+
}
|
|
111
|
+
return usage;
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* The rollouts of the session's descendant threads written since the turn began.
|
|
119
|
+
*
|
|
120
|
+
* Codex files a rollout under the local date its thread started, so every
|
|
121
|
+
* descendant sits in a day folder from the parent's to today's. Only a file
|
|
122
|
+
* written since the turn began can hold its rows; whether it descends from the
|
|
123
|
+
* session is followed up its chain of parents, found by the thread id that ends
|
|
124
|
+
* every rollout name, so an idle child still links a busy grandchild.
|
|
125
|
+
*
|
|
126
|
+
* @param transcriptPath - The parent rollout.
|
|
127
|
+
* @param sessionId - The parent thread id.
|
|
128
|
+
* @param since - Turn start, epoch ms.
|
|
129
|
+
* @returns The descendants' rollout paths.
|
|
130
|
+
*/
|
|
131
|
+
async function childRollouts(transcriptPath, sessionId, since) {
|
|
132
|
+
const byThread = new Map();
|
|
133
|
+
const candidates = [];
|
|
134
|
+
for (const dir of dayFolders(transcriptPath)) {
|
|
135
|
+
for (const name of await fs.readdir(dir).catch(() => [])) {
|
|
136
|
+
const thread = RE_CODEX_ROLLOUT.exec(name)?.[1];
|
|
137
|
+
const file = path.join(dir, name);
|
|
138
|
+
if (!thread || file === transcriptPath)
|
|
139
|
+
continue;
|
|
140
|
+
byThread.set(thread, file);
|
|
141
|
+
const stat = await fs.stat(file).catch(() => undefined);
|
|
142
|
+
if (stat && !(stat.mtimeMs < since))
|
|
143
|
+
candidates.push(file);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
// A shared ancestor's head is read once.
|
|
147
|
+
const heads = new Map();
|
|
148
|
+
const headOf = (file) => heads.get(file) ?? heads.set(file, rolloutThread(file)).get(file);
|
|
149
|
+
const picked = [];
|
|
150
|
+
for (const file of candidates) {
|
|
151
|
+
if (await descendsFrom(file, sessionId, byThread, headOf))
|
|
152
|
+
picked.push(file);
|
|
153
|
+
}
|
|
154
|
+
return picked;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Whether a rollout's thread descends from the session, walking up its parents.
|
|
158
|
+
*
|
|
159
|
+
* @param file - A rollout.
|
|
160
|
+
* @param sessionId - The root thread.
|
|
161
|
+
* @param byThread - Rollout path per thread id, from the file names.
|
|
162
|
+
* @param headOf - A rollout's threads, read from its head.
|
|
163
|
+
* @returns True when the chain reaches the session.
|
|
164
|
+
*/
|
|
165
|
+
async function descendsFrom(file, sessionId, byThread, headOf) {
|
|
166
|
+
let current = file;
|
|
167
|
+
for (let depth = 0; current !== undefined && depth < CODEX_MAX_SPAWN_DEPTH; depth++) {
|
|
168
|
+
const parent = (await headOf(current))?.parent;
|
|
169
|
+
if (parent === undefined)
|
|
170
|
+
return false;
|
|
171
|
+
if (parent === sessionId)
|
|
172
|
+
return true;
|
|
173
|
+
current = byThread.get(parent);
|
|
174
|
+
}
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* A rollout's own thread and the thread that spawned it, from its first line.
|
|
179
|
+
*
|
|
180
|
+
* @param file - A rollout.
|
|
181
|
+
* @returns Both ids, or undefined for a rollout no thread spawned.
|
|
182
|
+
*/
|
|
183
|
+
async function rolloutThread(file) {
|
|
184
|
+
const handle = await fs.open(file, 'r').catch(() => undefined);
|
|
185
|
+
if (!handle)
|
|
186
|
+
return undefined;
|
|
187
|
+
try {
|
|
188
|
+
const buffer = Buffer.allocUnsafe(CODEX_META_HEAD_BYTES);
|
|
189
|
+
const { bytesRead } = await handle.read(buffer, 0, CODEX_META_HEAD_BYTES, 0);
|
|
190
|
+
const head = buffer.subarray(0, bytesRead).toString('utf8');
|
|
191
|
+
const end = head.indexOf('\n');
|
|
192
|
+
// ponytail: a first line longer than the head is skipped; raise CODEX_META_HEAD_BYTES if one shows up.
|
|
193
|
+
const meta = asRecord(parseJsonLine(end < 0 ? head : head.slice(0, end))?.['payload']);
|
|
194
|
+
const spawn = asRecord(asRecord(asRecord(meta?.['source'])?.['subagent'])?.['thread_spawn']);
|
|
195
|
+
const thread = firstStringOf(meta?.['id']);
|
|
196
|
+
const parent = firstStringOf(spawn?.['parent_thread_id']);
|
|
197
|
+
return thread && parent ? { thread, parent } : undefined;
|
|
198
|
+
}
|
|
199
|
+
finally {
|
|
200
|
+
await handle.close();
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* The day folders a descendant of the session can sit in: the parent rollout's
|
|
205
|
+
* through today's, at most CODEX_MAX_SCAN_DAYS of them.
|
|
206
|
+
*
|
|
207
|
+
* ponytail: a session older than CODEX_MAX_SCAN_DAYS loses the children filed
|
|
208
|
+
* in its first days; raise the cap if long-lived sessions spawn across weeks.
|
|
209
|
+
*
|
|
210
|
+
* @param transcriptPath - The parent rollout, `<root>/YYYY/MM/DD/rollout-…`.
|
|
211
|
+
* @returns The folders, oldest first.
|
|
212
|
+
*/
|
|
213
|
+
function dayFolders(transcriptPath) {
|
|
214
|
+
const dir = path.dirname(transcriptPath);
|
|
215
|
+
const sessionsRoot = path.resolve(dir, '..', '..', '..');
|
|
216
|
+
const [year, month, day] = path.relative(sessionsRoot, dir).split(path.sep).map(Number);
|
|
217
|
+
const today = new Date();
|
|
218
|
+
const first = new Date(year, month - 1, day);
|
|
219
|
+
const start = Number.isNaN(first.getTime()) || first > today ? today : first;
|
|
220
|
+
const folders = [];
|
|
221
|
+
for (let at = new Date(Math.max(start.getTime(), today.getTime() - (CODEX_MAX_SCAN_DAYS - 1) * DAY_MS)); at <= today; at = new Date(at.getFullYear(), at.getMonth(), at.getDate() + 1)) {
|
|
222
|
+
folders.push(dayFolder(sessionsRoot, at));
|
|
223
|
+
}
|
|
224
|
+
return [...new Set([dir, ...folders])];
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* The folder Codex files a thread started on a day under.
|
|
228
|
+
*
|
|
229
|
+
* @param sessionsRoot - `~/.codex/sessions`.
|
|
230
|
+
* @param day - The day, local time as Codex names it.
|
|
231
|
+
* @returns `<root>/YYYY/MM/DD`.
|
|
232
|
+
*/
|
|
233
|
+
function dayFolder(sessionsRoot, day) {
|
|
234
|
+
return path.join(sessionsRoot, String(day.getFullYear()), String(day.getMonth() + 1).padStart(2, '0'), String(day.getDate()).padStart(2, '0'));
|
|
235
|
+
}
|
|
77
236
|
/**
|
|
78
237
|
* The counts one rollout row carries, when it is an `event_msg` / `token_count` row.
|
|
79
238
|
*
|