@agent-watch-ai/edge 0.6.0 → 0.7.1
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 +288 -9
- package/README.md +307 -5
- package/dist/billing/billing-mode.d.ts +37 -5
- package/dist/billing/billing-mode.js +28 -19
- package/dist/billing/constants/billing.constants.d.ts +0 -2
- package/dist/billing/constants/billing.constants.js +0 -2
- package/dist/billing/index.d.ts +2 -1
- package/dist/billing/index.js +1 -1
- package/dist/billing/session-billing.d.ts +7 -3
- package/dist/billing/session-billing.js +34 -13
- package/dist/billing/types/billing.types.d.ts +7 -0
- package/dist/capture/capture-env.d.ts +147 -0
- package/dist/capture/capture-env.js +301 -0
- package/dist/capture/captured-step.d.ts +88 -0
- package/dist/capture/captured-step.js +503 -0
- package/dist/capture/constants/capture.constants.d.ts +85 -0
- package/dist/capture/constants/capture.constants.js +87 -0
- package/dist/capture/launched-runs.d.ts +43 -0
- package/dist/capture/launched-runs.js +157 -0
- package/dist/capture/node.cjs +697 -0
- package/dist/capture/node.d.cts +32 -0
- package/dist/capture/python/sitecustomize.py +832 -0
- package/dist/capture/schemas/capture.schema.d.ts +35 -0
- package/dist/capture/schemas/capture.schema.js +31 -0
- package/dist/capture/types/capture.types.d.ts +20 -0
- package/dist/capture/venv-loader.d.ts +68 -0
- package/dist/capture/venv-loader.js +189 -0
- package/dist/cli/constants/cli.constants.d.ts +23 -6
- package/dist/cli/constants/cli.constants.js +24 -7
- package/dist/cli/context.d.ts +19 -22
- package/dist/cli/context.js +23 -54
- package/dist/cli/doctor-run-capture.d.ts +21 -0
- package/dist/cli/doctor-run-capture.js +33 -0
- package/dist/cli/doctor.js +21 -68
- package/dist/cli/hook-check.d.ts +7 -2
- package/dist/cli/hook-check.js +30 -15
- package/dist/cli/hook.d.ts +14 -0
- package/dist/cli/hook.js +82 -1
- package/dist/cli/index.d.ts +1 -1
- package/dist/cli/index.js +1 -1
- package/dist/cli/misc.js +10 -2
- package/dist/cli/npm-global.d.ts +24 -0
- package/dist/cli/npm-global.js +77 -0
- package/dist/cli/npx-install.js +22 -6
- package/dist/cli/replay.d.ts +33 -0
- package/dist/cli/replay.js +228 -0
- package/dist/cli/setup.d.ts +10 -2
- package/dist/cli/setup.js +200 -31
- package/dist/cli/status.js +45 -5
- package/dist/cli/toggle.js +10 -0
- package/dist/cli/types/cli.types.d.ts +15 -1
- package/dist/cli/uninstall.js +42 -23
- package/dist/cli/update.js +3 -2
- package/dist/cli.js +104 -17
- package/dist/config/config-store.d.ts +15 -4
- package/dist/config/config-store.js +31 -7
- package/dist/config/config.d.ts +0 -2
- package/dist/config/config.js +0 -1
- package/dist/config/constants/config.constants.d.ts +8 -1
- package/dist/config/constants/config.constants.js +10 -1
- package/dist/config/events-transport.d.ts +15 -0
- package/dist/config/events-transport.js +29 -0
- package/dist/config/index.d.ts +2 -1
- package/dist/config/index.js +1 -0
- package/dist/config/root-config.d.ts +12 -0
- package/dist/config/root-config.js +24 -0
- package/dist/config/schemas/config.schema.d.ts +15 -2
- package/dist/config/schemas/config.schema.js +52 -8
- package/dist/config/types/config.types.d.ts +7 -0
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.js +1 -1
- package/dist/core/version.d.ts +9 -0
- package/dist/core/version.js +14 -0
- package/dist/core/which.d.ts +11 -0
- package/dist/core/which.js +18 -0
- package/dist/credentials/cloud-claim.d.ts +34 -0
- package/dist/credentials/cloud-claim.js +61 -0
- package/dist/credentials/constants/credentials.constants.d.ts +16 -1
- package/dist/credentials/constants/credentials.constants.js +17 -2
- package/dist/credentials/credential-status.js +1 -1
- package/dist/credentials/credentials.d.ts +3 -3
- package/dist/credentials/credentials.js +26 -12
- package/dist/credentials/device-api.d.ts +15 -1
- package/dist/credentials/device-api.js +12 -1
- package/dist/credentials/identity.d.ts +2 -2
- package/dist/credentials/identity.js +2 -2
- package/dist/credentials/install-key.d.ts +31 -0
- package/dist/credentials/install-key.js +58 -0
- package/dist/credentials/migration.d.ts +9 -7
- package/dist/credentials/migration.js +58 -18
- package/dist/credentials/secret-store.d.ts +12 -0
- package/dist/credentials/secret-store.js +18 -1
- package/dist/credentials/sign-in.d.ts +27 -0
- package/dist/credentials/sign-in.js +31 -1
- package/dist/credentials/token-refresher.d.ts +5 -1
- package/dist/credentials/token-refresher.js +10 -2
- package/dist/enforcement/enforcement.js +1 -1
- package/dist/events/constants/events.constants.d.ts +5 -2
- package/dist/events/constants/events.constants.js +4 -1
- package/dist/events/enrich.d.ts +14 -0
- package/dist/events/enrich.js +1 -1
- package/dist/events/index.d.ts +1 -0
- package/dist/events/product-event.d.ts +6 -3
- package/dist/events/types/events.types.d.ts +6 -0
- package/dist/events/types/replay-progress.types.d.ts +26 -0
- package/dist/events/types/replay-progress.types.js +1 -0
- package/dist/events/types/repo-tree.types.d.ts +2 -1
- package/dist/feature/ticket-candidates.d.ts +1 -1
- package/dist/git/checkout-root.d.ts +15 -0
- package/dist/git/checkout-root.js +18 -0
- package/dist/git/code-state.d.ts +2 -16
- package/dist/git/code-state.js +11 -12
- package/dist/git/constants/git.constants.d.ts +33 -11
- package/dist/git/constants/git.constants.js +45 -12
- package/dist/git/fingerprint.d.ts +3 -6
- package/dist/git/fingerprint.js +5 -8
- package/dist/git/git-context.js +5 -1
- package/dist/git/index.d.ts +1 -2
- package/dist/git/index.js +1 -1
- package/dist/git/repo-snapshot.d.ts +13 -4
- package/dist/git/repo-snapshot.js +53 -9
- package/dist/git/types/snapshot.types.d.ts +6 -0
- package/dist/pipeline/constants/pipeline.constants.d.ts +3 -0
- package/dist/pipeline/constants/pipeline.constants.js +3 -0
- package/dist/pipeline/hook-pipeline.js +82 -57
- package/dist/privacy/product-capture.js +2 -0
- package/dist/providers/antigravity/antigravity.capture.d.ts +2 -0
- package/dist/providers/antigravity/antigravity.capture.js +5 -0
- package/dist/providers/antigravity/antigravity.detect.js +8 -22
- package/dist/providers/antigravity/antigravity.provider.js +10 -3
- package/dist/providers/antigravity/constants/antigravity.constants.d.ts +1 -0
- package/dist/providers/antigravity/constants/antigravity.constants.js +1 -0
- package/dist/providers/claude/claude.adapter.js +9 -6
- package/dist/providers/claude/claude.detect.js +2 -17
- package/dist/providers/claude/claude.otel.d.ts +21 -3
- package/dist/providers/claude/claude.otel.js +216 -61
- package/dist/providers/claude/claude.provider.js +21 -1
- package/dist/providers/claude/claude.version.d.ts +9 -0
- package/dist/providers/claude/claude.version.js +28 -0
- package/dist/providers/claude/constants/claude.constants.d.ts +27 -0
- package/dist/providers/claude/constants/claude.constants.js +27 -0
- package/dist/providers/claude/constants/claude.otel.constants.d.ts +27 -0
- package/dist/providers/claude/constants/claude.otel.constants.js +45 -5
- package/dist/providers/claude/types/claude.types.d.ts +13 -0
- package/dist/providers/codex/codex.capture.d.ts +76 -0
- package/dist/providers/codex/codex.capture.js +454 -0
- package/dist/providers/codex/codex.detect.js +2 -4
- package/dist/providers/codex/codex.otel.d.ts +38 -2
- package/dist/providers/codex/codex.otel.js +50 -6
- package/dist/providers/codex/codex.provider.js +8 -0
- package/dist/providers/codex/codex.surface.d.ts +9 -0
- package/dist/{turns/codex-originator.js → providers/codex/codex.surface.js} +17 -3
- package/dist/providers/codex/codex.titles.d.ts +14 -0
- package/dist/providers/codex/codex.titles.js +35 -0
- package/dist/providers/codex/constants/codex.constants.d.ts +4 -0
- package/dist/providers/codex/constants/codex.constants.js +4 -0
- package/dist/providers/cursor/constants/cursor.constants.d.ts +17 -4
- package/dist/providers/cursor/constants/cursor.constants.js +25 -5
- package/dist/providers/cursor/cursor-mcp-memo.d.ts +34 -0
- package/dist/providers/cursor/cursor-mcp-memo.js +94 -0
- package/dist/providers/cursor/cursor.adapter.js +20 -82
- package/dist/providers/cursor/cursor.capture.d.ts +1 -0
- package/dist/providers/cursor/cursor.capture.js +4 -0
- package/dist/providers/cursor/cursor.detect.d.ts +5 -1
- package/dist/providers/cursor/cursor.detect.js +11 -26
- package/dist/providers/cursor/cursor.hooks.js +2 -2
- package/dist/providers/cursor/cursor.provider.js +16 -7
- package/dist/providers/cursor/cursor.titles.d.ts +14 -0
- package/dist/providers/cursor/cursor.titles.js +20 -0
- package/dist/providers/cursor/schemas/cursor.schema.d.ts +3 -3
- package/dist/providers/cursor/schemas/cursor.schema.js +5 -3
- package/dist/providers/gemini/constants/gemini.constants.d.ts +16 -0
- package/dist/providers/gemini/constants/gemini.constants.js +17 -0
- package/dist/providers/gemini/gemini.capture.d.ts +81 -0
- package/dist/providers/gemini/gemini.capture.js +210 -0
- package/dist/providers/gemini/gemini.detect.js +57 -24
- package/dist/providers/gemini/gemini.otel.d.ts +38 -2
- package/dist/providers/gemini/gemini.otel.js +56 -125
- package/dist/providers/gemini/gemini.provider.js +5 -0
- package/dist/providers/index.d.ts +1 -0
- package/dist/providers/index.js +1 -0
- package/dist/providers/provider.d.ts +8 -0
- package/dist/providers/provider.js +9 -0
- package/dist/providers/registry.d.ts +14 -0
- package/dist/providers/registry.js +14 -0
- package/dist/providers/shared/constants/hook-config.constants.d.ts +2 -0
- package/dist/providers/shared/constants/hook-config.constants.js +2 -0
- package/dist/providers/shared/constants/terminal-capture.constants.d.ts +6 -0
- package/dist/providers/shared/constants/terminal-capture.constants.js +6 -0
- package/dist/providers/shared/editor-paths.d.ts +8 -0
- package/dist/providers/shared/editor-paths.js +18 -0
- package/dist/providers/shared/hook-config.d.ts +5 -0
- package/dist/providers/shared/hook-config.js +39 -1
- package/dist/providers/shared/hooks-marker.d.ts +14 -0
- package/dist/providers/shared/hooks-marker.js +23 -0
- package/dist/providers/shared/install-record.d.ts +29 -0
- package/dist/providers/shared/install-record.js +52 -0
- package/dist/providers/shared/session-title-store.d.ts +23 -0
- package/dist/providers/shared/session-title-store.js +50 -0
- package/dist/providers/shared/terminal-capture.d.ts +41 -0
- package/dist/providers/shared/terminal-capture.js +109 -0
- package/dist/providers/shared/tooling.d.ts +21 -0
- package/dist/providers/shared/tooling.js +36 -1
- package/dist/providers/types/provider.types.d.ts +57 -4
- package/dist/replay/checkout-resolver.d.ts +56 -0
- package/dist/replay/checkout-resolver.js +173 -0
- package/dist/replay/codex-turns.d.ts +24 -0
- package/dist/replay/codex-turns.js +221 -0
- package/dist/replay/constants/replay.constants.d.ts +99 -0
- package/dist/replay/constants/replay.constants.js +102 -0
- package/dist/replay/describe.d.ts +10 -0
- package/dist/replay/describe.js +31 -0
- package/dist/replay/gemini-turns.d.ts +36 -0
- package/dist/replay/gemini-turns.js +192 -0
- package/dist/replay/machine.d.ts +34 -0
- package/dist/replay/machine.js +68 -0
- package/dist/replay/progress-event.d.ts +21 -0
- package/dist/replay/progress-event.js +45 -0
- package/dist/replay/replay-runner.d.ts +60 -0
- package/dist/replay/replay-runner.js +581 -0
- package/dist/replay/replay-scan.d.ts +84 -0
- package/dist/replay/replay-scan.js +182 -0
- package/dist/replay/replay-state.d.ts +108 -0
- package/dist/replay/replay-state.js +199 -0
- package/dist/replay/resume.d.ts +18 -0
- package/dist/replay/resume.js +34 -0
- package/dist/replay/start.d.ts +23 -0
- package/dist/replay/start.js +39 -0
- package/dist/replay/throttle.d.ts +36 -0
- package/dist/replay/throttle.js +44 -0
- package/dist/replay/transcript-turns.d.ts +35 -0
- package/dist/replay/transcript-turns.js +241 -0
- package/dist/replay/turn-event.d.ts +40 -0
- package/dist/replay/turn-event.js +127 -0
- package/dist/replay/types/replay.types.d.ts +198 -0
- package/dist/replay/types/replay.types.js +1 -0
- package/dist/replay/window.d.ts +28 -0
- package/dist/replay/window.js +41 -0
- package/dist/replay/worktree-map.d.ts +42 -0
- package/dist/replay/worktree-map.js +134 -0
- package/dist/snapshot/branch-selection.d.ts +40 -23
- package/dist/snapshot/branch-selection.js +71 -41
- package/dist/snapshot/constants/snapshot-stages.constants.d.ts +3 -1
- package/dist/snapshot/constants/snapshot-stages.constants.js +3 -1
- package/dist/snapshot/constants/snapshot.constants.d.ts +34 -7
- package/dist/snapshot/constants/snapshot.constants.js +34 -7
- package/dist/snapshot/index.d.ts +2 -2
- package/dist/snapshot/index.js +1 -1
- package/dist/snapshot/snapshot-pipeline.js +100 -26
- package/dist/snapshot/snapshot-state.js +6 -2
- package/dist/snapshot/tree-pipeline.d.ts +1 -1
- package/dist/snapshot/tree-pipeline.js +34 -15
- package/dist/snapshot/types/snapshot.types.d.ts +22 -3
- package/dist/storage/schemas/storage.schema.d.ts +2 -0
- package/dist/storage/schemas/storage.schema.js +6 -0
- package/dist/transport/delivery-stats.d.ts +13 -1
- package/dist/transport/delivery-stats.js +18 -2
- package/dist/transport/delivery.d.ts +4 -3
- package/dist/transport/delivery.js +21 -7
- package/dist/transport/identity-delivery.d.ts +20 -0
- package/dist/transport/identity-delivery.js +41 -0
- package/dist/transport/index.d.ts +2 -0
- package/dist/transport/index.js +1 -0
- package/dist/transport/types/delivery-flow.types.d.ts +7 -0
- package/dist/turns/claude-transcript.js +4 -12
- package/dist/turns/codex-transcript.d.ts +9 -0
- package/dist/turns/codex-transcript.js +27 -19
- package/dist/turns/constants/turns.constants.d.ts +29 -12
- package/dist/turns/constants/turns.constants.js +31 -19
- package/dist/turns/gemini-transcript.d.ts +19 -1
- package/dist/turns/gemini-transcript.js +29 -18
- package/dist/turns/index.d.ts +3 -4
- package/dist/turns/index.js +2 -3
- package/dist/turns/run-state.d.ts +71 -0
- package/dist/turns/run-state.js +114 -0
- package/dist/turns/tool-events.d.ts +11 -1
- package/dist/turns/tool-events.js +32 -9
- package/dist/turns/transcript.d.ts +15 -14
- package/dist/turns/transcript.js +23 -4
- package/dist/turns/turn-state.d.ts +82 -7
- package/dist/turns/turn-state.js +157 -10
- package/dist/turns/turn-summary.js +7 -2
- package/dist/turns/turn-tracker.d.ts +9 -16
- package/dist/turns/turn-tracker.js +163 -87
- package/dist/turns/types/transcript.types.d.ts +11 -1
- package/dist/turns/types/turn-provider.types.d.ts +33 -0
- package/dist/turns/types/turn-provider.types.js +1 -0
- package/dist/turns/types/turn-state.types.d.ts +9 -0
- package/dist/turns/types/turn-summary.types.d.ts +17 -1
- package/dist/turns/types/turn-tracker.types.d.ts +3 -1
- package/dist/turns/work-checkout.js +1 -2
- package/docs/AUDIT_PROMPT.md +10 -0
- package/docs/DATA_HANDLING.md +201 -13
- package/docs/ENTERPRISE_DEPLOYMENT.md +3 -3
- package/docs/PERFORMANCE.md +10 -0
- package/package.json +3 -2
- package/dist/events/canonical-event.d.ts +0 -10
- package/dist/events/canonical-event.js +0 -9
- package/dist/git/repository-root.d.ts +0 -39
- package/dist/git/repository-root.js +0 -131
- package/dist/git/types/repository-root.types.d.ts +0 -11
- package/dist/transport/transport.d.ts +0 -6
- package/dist/turns/codex-originator.d.ts +0 -11
- package/dist/turns/cursor-transcript.d.ts +0 -21
- package/dist/turns/cursor-transcript.js +0 -66
- package/dist/turns/native-session-titles.d.ts +0 -26
- package/dist/turns/native-session-titles.js +0 -102
- /package/dist/{git/types/repository-root.types.js → billing/types/billing.types.js} +0 -0
- /package/dist/{transport/transport.js → capture/types/capture.types.js} +0 -0
|
@@ -2,7 +2,15 @@ import path from 'node:path';
|
|
|
2
2
|
import { asRecord, firstString } from '../../core/object.js';
|
|
3
3
|
import { sanitizeText } from '../../privacy/sanitizer.js';
|
|
4
4
|
import { sha256Hex } from '../../events/event-id.js';
|
|
5
|
-
import { COMMAND_KEYS, FILE_EDIT_TOOLS, FILE_PATH_KEYS, FILE_READ_TOOLS, COMMAND_HEAD, MAX_TOOL_KEY_LENGTH, MCP_FOLDED_KEY, MCP_TOOL_PREFIX, MCP_TOOL_SEPARATOR, SHELL_TOOLS } from './constants/tooling.constants.js';
|
|
5
|
+
import { COMMAND_KEYS, FILE_EDIT_TOOLS, FILE_PATH_KEYS, FILE_READ_TOOLS, COMMAND_HEAD, MAX_TOOL_KEY_LENGTH, MCP_FOLDED_KEY, MCP_TOOL_PREFIX, MCP_TOOL_SEPARATOR, RE_HARNESS_BLOCK, SHELL_TOOLS } from './constants/tooling.constants.js';
|
|
6
|
+
/** Core's name rule (AWT-710); a value that fails it is never sent. */
|
|
7
|
+
const RE_NAME = /^[A-Za-z0-9][A-Za-z0-9_.:/-]{0,79}$/;
|
|
8
|
+
/**
|
|
9
|
+
* A prompt's leading `/token`. No `/` inside it: `/Users/me/a.ts fix this` is a path, not a command.
|
|
10
|
+
* ponytail: a one-segment path (`/tmp is full`) still reads as command `tmp`, one name-shaped word;
|
|
11
|
+
* upgrade path is checking the token against the agent's own command list.
|
|
12
|
+
*/
|
|
13
|
+
const RE_SLASH_COMMAND = /^\/([A-Za-z0-9][A-Za-z0-9_.:-]{0,79})(?:\s|$)/;
|
|
6
14
|
/**
|
|
7
15
|
* What kind of thing a tool call is, whatever the agent calls it.
|
|
8
16
|
*
|
|
@@ -162,6 +170,33 @@ export function extractCommand(toolInput) {
|
|
|
162
170
|
return undefined;
|
|
163
171
|
return firstString(record, COMMAND_KEYS);
|
|
164
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* The skill a skill call invoked, its name and nothing else.
|
|
175
|
+
*
|
|
176
|
+
* Read in memory from the call's input; its arguments are never looked at.
|
|
177
|
+
* A name core would refuse is dropped here, never the call it rides on.
|
|
178
|
+
*
|
|
179
|
+
* @param input - The skill tool's input as the agent passed it.
|
|
180
|
+
* @param key - The input field naming the skill.
|
|
181
|
+
* @returns The trimmed name, or undefined when there is no valid one.
|
|
182
|
+
*/
|
|
183
|
+
export function skillName(input, key) {
|
|
184
|
+
const name = asRecord(input)?.[key];
|
|
185
|
+
return typeof name === 'string' && RE_NAME.test(name.trim()) ? name.trim() : undefined;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* The slash command a prompt starts with, without its slash or arguments.
|
|
189
|
+
*
|
|
190
|
+
* Only the matched token survives: nothing after it, and nothing at all from
|
|
191
|
+
* a prompt that does not open with one.
|
|
192
|
+
*
|
|
193
|
+
* @param prompt - The prompt as the agent reported it.
|
|
194
|
+
* @returns The command name, or undefined.
|
|
195
|
+
*/
|
|
196
|
+
export function slashCommandName(prompt) {
|
|
197
|
+
// Harness blocks first, as `promptPatch` drops them: they are not what the person typed.
|
|
198
|
+
return prompt === undefined ? undefined : RE_SLASH_COMMAND.exec(prompt.replace(RE_HARNESS_BLOCK, '').trimStart())?.[1];
|
|
199
|
+
}
|
|
165
200
|
/**
|
|
166
201
|
* A shell call out of untrusted payload fields, type-checked.
|
|
167
202
|
*
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { AgentWatchConfig, OtelSignalName } from '../../config/types/config.types.js';
|
|
2
2
|
import type { Env } from '../../core/types/core.types.js';
|
|
3
3
|
import type { AgentWatchEvent } from '../../events/types/events.types.js';
|
|
4
|
+
import type { TurnProvider } from '../../turns/types/turn-provider.types.js';
|
|
4
5
|
import type { AgentWatchPaths, InstallState } from '../../storage/types/storage.types.js';
|
|
5
6
|
/** What detection found out about one agent on this machine. */
|
|
6
7
|
export interface DetectionResult {
|
|
@@ -43,6 +44,11 @@ export interface SetupOutcome {
|
|
|
43
44
|
readonly changed: boolean;
|
|
44
45
|
/** User-facing notes: required manual steps, skip reasons, errors. */
|
|
45
46
|
readonly messages: readonly string[];
|
|
47
|
+
/**
|
|
48
|
+
* Refused because the developer's own telemetry config is in the way. A
|
|
49
|
+
* skipped step, not a failure: nothing AgentWatch can or should repair.
|
|
50
|
+
*/
|
|
51
|
+
readonly conflict?: boolean;
|
|
46
52
|
/**
|
|
47
53
|
* Install state after this operation, when it recorded anything.
|
|
48
54
|
*
|
|
@@ -69,7 +75,23 @@ export interface NativeTelemetryStatus {
|
|
|
69
75
|
readonly conflict?: string;
|
|
70
76
|
readonly detail?: string;
|
|
71
77
|
}
|
|
78
|
+
/** Whether what this agent's shell tool starts loads the capture shims. */
|
|
79
|
+
export interface RunCaptureStatus {
|
|
80
|
+
readonly on: boolean;
|
|
81
|
+
/** Why it is off, in words a developer can act on. */
|
|
82
|
+
readonly reason?: string;
|
|
83
|
+
/** It is on, but something should be done about it. */
|
|
84
|
+
readonly warning?: string;
|
|
85
|
+
}
|
|
86
|
+
/** How one agent's launched processes get the capture shims in their environment. */
|
|
87
|
+
export interface RunCaptureRoute {
|
|
88
|
+
inspect(context: SetupContext): Promise<RunCaptureStatus>;
|
|
89
|
+
/** The file this route writes the shims into; the shared copy stays while any of them names it. */
|
|
90
|
+
configPath(env: Env): string;
|
|
91
|
+
}
|
|
72
92
|
export interface NativeTelemetryConfigurator {
|
|
93
|
+
/** The run-capture route, for an agent whose telemetry config also carries it. */
|
|
94
|
+
readonly runCapture?: RunCaptureRoute;
|
|
73
95
|
supported(env: Env): Promise<boolean>;
|
|
74
96
|
inspect(context: SetupContext): Promise<NativeTelemetryStatus>;
|
|
75
97
|
configure(context: SetupContext): Promise<SetupOutcome>;
|
|
@@ -99,8 +121,8 @@ export interface NativeTelemetryConfigurator {
|
|
|
99
121
|
*/
|
|
100
122
|
rewriteBearer?(context: BearerRewriteContext): Promise<boolean>;
|
|
101
123
|
}
|
|
102
|
-
|
|
103
|
-
|
|
124
|
+
/** One coding agent; the turn tracker reads the `TurnProvider` part. */
|
|
125
|
+
export interface AgentProvider extends TurnProvider {
|
|
104
126
|
readonly displayName: string;
|
|
105
127
|
detect(env: Env): Promise<DetectionResult>;
|
|
106
128
|
installHooks(context: SetupContext): Promise<SetupOutcome>;
|
|
@@ -125,6 +147,13 @@ export interface AgentProvider {
|
|
|
125
147
|
* channel, and the line is not shown.
|
|
126
148
|
*/
|
|
127
149
|
getNoticeResponse?(payload: unknown, message: string): ProviderHookResponse | undefined;
|
|
150
|
+
/**
|
|
151
|
+
* The session whose turn close this payload hands to a detached `agentwatch
|
|
152
|
+
* hook`, so the agent is answered at once instead of waiting on the send.
|
|
153
|
+
* Undefined processes it in place. Only for a hook that answers nothing; the
|
|
154
|
+
* session is named so its `SessionEnd` can see the close is still pending.
|
|
155
|
+
*/
|
|
156
|
+
detachedSession?(payload: unknown, env: Env): string | undefined;
|
|
128
157
|
/**
|
|
129
158
|
* Working directory this payload was produced in, when the provider does not
|
|
130
159
|
* report it as a top-level `cwd`. Git context, repository config and ticket
|
|
@@ -136,8 +165,9 @@ export interface AgentProvider {
|
|
|
136
165
|
* The shell command a tool-start payload is about to run, and the directory
|
|
137
166
|
* it runs in when the agent reports one apart from `cwd`.
|
|
138
167
|
*
|
|
139
|
-
* Read
|
|
140
|
-
*
|
|
168
|
+
* Read in memory, for the directory names in it and for what it runs; the text
|
|
169
|
+
* is dropped there and never reaches an event, a record or a log. The one thing
|
|
170
|
+
* kept is a captured run's scrubbed entry file (`src/capture/launched-runs.ts`).
|
|
141
171
|
* Undefined for any payload that is not a shell call.
|
|
142
172
|
*/
|
|
143
173
|
shellCall?(payload: unknown): ShellCall | undefined;
|
|
@@ -148,6 +178,29 @@ export interface AgentProvider {
|
|
|
148
178
|
*/
|
|
149
179
|
workspaceRoots?(payload: unknown): readonly string[] | undefined;
|
|
150
180
|
readonly nativeTelemetry?: NativeTelemetryConfigurator;
|
|
181
|
+
/** Run capture, for an agent with no telemetry config to carry it. */
|
|
182
|
+
readonly runCapture?: RunCaptureRoute;
|
|
183
|
+
/**
|
|
184
|
+
* What this agent cannot report, which no setting fixes. Setup, status and
|
|
185
|
+
* doctor show them, so an absent number reads as a known gap rather than a
|
|
186
|
+
* bug in the edge.
|
|
187
|
+
*/
|
|
188
|
+
readonly caveats?: readonly ProviderCaveat[];
|
|
189
|
+
/** What doctor should check about the agent itself, such as its version. */
|
|
190
|
+
readonly checks?: (env: Env) => Promise<readonly ProviderCheck[]>;
|
|
191
|
+
}
|
|
192
|
+
/** One doctor check an agent runs on itself; see `AgentProvider.checks`. */
|
|
193
|
+
export interface ProviderCheck {
|
|
194
|
+
readonly name: string;
|
|
195
|
+
readonly level: 'ok' | 'warn';
|
|
196
|
+
readonly detail: string;
|
|
197
|
+
}
|
|
198
|
+
/** One known limitation of an agent; see `AgentProvider.caveats`. */
|
|
199
|
+
export interface ProviderCaveat {
|
|
200
|
+
/** Short label, e.g. "Cursor token usage". */
|
|
201
|
+
readonly name: string;
|
|
202
|
+
/** One line saying what is missing and why. */
|
|
203
|
+
readonly detail: string;
|
|
151
204
|
}
|
|
152
205
|
/** A shell call as the agent is about to run it. Memory only; see `AgentProvider.shellCall`. */
|
|
153
206
|
export interface ShellCall {
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { ReplayCheckout, ReplayTool, ReplayTurn } from './types/replay.types.js';
|
|
2
|
+
import { type WorktreeMap } from './worktree-map.js';
|
|
3
|
+
/** Answers "which repository is this folder in?" for a transcript's folders, asking git once per folder. */
|
|
4
|
+
export type CheckoutResolver = (cwd: string) => Promise<ReplayCheckout | undefined>;
|
|
5
|
+
/**
|
|
6
|
+
* A resolver that remembers its answers for the run.
|
|
7
|
+
*
|
|
8
|
+
* Three git processes per distinct folder, however many turns ran there. A
|
|
9
|
+
* folder that no longer exists answers from the worktrees the transcripts
|
|
10
|
+
* recorded, and otherwise nothing, and so do the turns that ran in it (their
|
|
11
|
+
* tokens still count, with no repository). Not the whole git context the hooks
|
|
12
|
+
* take: no commit or status, because the commit is not known.
|
|
13
|
+
*
|
|
14
|
+
* @param worktrees - What the transcripts recorded about worktrees; filled before the first answer.
|
|
15
|
+
* @returns The resolver.
|
|
16
|
+
*/
|
|
17
|
+
export declare function createCheckoutResolver(worktrees?: WorktreeMap): CheckoutResolver;
|
|
18
|
+
/**
|
|
19
|
+
* The checkout a turn reports, by the live hook's ladder as far as a transcript allows: `changed`, `carried`, then its folder.
|
|
20
|
+
*
|
|
21
|
+
* The checkout that holds the most of the turn's edits wins (first seen on a tie), so an edit in another worktree
|
|
22
|
+
* is no longer dropped for the folder the session sat in. A turn with no edit in any checkout falls to the checkout
|
|
23
|
+
* the session last changed, then its folder; a turn whose edits all lie elsewhere reports neither, as live
|
|
24
|
+
* (`fileEvidenceAllows`: a session that wrote files elsewhere is not working in the one its shell sits in).
|
|
25
|
+
* Reads name no checkout: the live `referenced` rung is weaker than `changed` and would send that repository's snapshot.
|
|
26
|
+
*
|
|
27
|
+
* @param turn - The turn.
|
|
28
|
+
* @param resolve - The run's resolver.
|
|
29
|
+
* @param accept - Whether a tool file's checkout may be used (it may belong to another root's identity); a refused one is passed over.
|
|
30
|
+
* @param carried - The checkout the session last changed.
|
|
31
|
+
* @returns The checkout and how it was known (no `evidence`: the folder); undefined when none resolves.
|
|
32
|
+
*/
|
|
33
|
+
export declare function checkoutOfTurn(turn: Pick<ReplayTurn, 'cwd' | 'tools' | 'branch' | 'startedAt'>, resolve: CheckoutResolver, accept?: (checkout: ReplayCheckout) => boolean, carried?: ReplayCheckout): Promise<ReplayCheckout | undefined>;
|
|
34
|
+
/**
|
|
35
|
+
* Whether a tool call edited a file at a known place.
|
|
36
|
+
*
|
|
37
|
+
* @param tool - The call.
|
|
38
|
+
* @returns True for an edit with an absolute path.
|
|
39
|
+
*/
|
|
40
|
+
export declare function isEdit(tool: ReplayTool): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* The turn's tools with every file that lies in no checkout marked, as the live hook tells them apart.
|
|
43
|
+
*
|
|
44
|
+
* A folder that is gone answers nothing, so its files read as outside too, unless the path belongs to another
|
|
45
|
+
* identity's root or an ignored one, whose files are left out.
|
|
46
|
+
* ponytail: a root-scoped replay also leaves out files beyond its root, which a live hook would send. Ceiling: a
|
|
47
|
+
* root-scoped identity loses that outside-repo evidence; upgrade path: a scope port that tells "no root" from "another root".
|
|
48
|
+
* ponytail: asks git once per distinct folder of the run (the resolver remembers), with no per-turn cap. Ceiling:
|
|
49
|
+
* a machine whose agents touch tens of thousands of folders; the replay is niced and paced, so it only runs longer.
|
|
50
|
+
*
|
|
51
|
+
* @param tools - The turn's tools.
|
|
52
|
+
* @param resolve - The run's resolver.
|
|
53
|
+
* @param inScope - Whether a folder belongs to this identity.
|
|
54
|
+
* @returns A new list; a file inside some checkout is left unmarked.
|
|
55
|
+
*/
|
|
56
|
+
export declare function markFilesOutsideCheckouts(tools: readonly ReplayTool[], resolve: CheckoutResolver, inScope?: (folder: string) => boolean): Promise<ReplayTool[]>;
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { checkoutOf } from '../git/checkout-root.js';
|
|
4
|
+
import { GIT_BRANCH_ARGS, GIT_REMOTE_ARGS, GIT_REPO_ROOT_ARGS, GIT_TIMEOUT_MS } from '../git/constants/git.constants.js';
|
|
5
|
+
import { repositoryIdentity, runGit } from '../git/git-context.js';
|
|
6
|
+
import { learnedRootOf } from './worktree-map.js';
|
|
7
|
+
/**
|
|
8
|
+
* A resolver that remembers its answers for the run.
|
|
9
|
+
*
|
|
10
|
+
* Three git processes per distinct folder, however many turns ran there. A
|
|
11
|
+
* folder that no longer exists answers from the worktrees the transcripts
|
|
12
|
+
* recorded, and otherwise nothing, and so do the turns that ran in it (their
|
|
13
|
+
* tokens still count, with no repository). Not the whole git context the hooks
|
|
14
|
+
* take: no commit or status, because the commit is not known.
|
|
15
|
+
*
|
|
16
|
+
* @param worktrees - What the transcripts recorded about worktrees; filled before the first answer.
|
|
17
|
+
* @returns The resolver.
|
|
18
|
+
*/
|
|
19
|
+
export function createCheckoutResolver(worktrees = new Map()) {
|
|
20
|
+
const answers = new Map();
|
|
21
|
+
return (cwd) => {
|
|
22
|
+
const known = answers.get(cwd);
|
|
23
|
+
if (known)
|
|
24
|
+
return known;
|
|
25
|
+
const asked = resolveCheckout(cwd, worktrees);
|
|
26
|
+
answers.set(cwd, asked);
|
|
27
|
+
return asked;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
const TRUNKS = new Set(['main', 'master', 'develop', 'development', 'trunk']);
|
|
31
|
+
async function resolveCheckout(cwd, worktrees) {
|
|
32
|
+
const live = await liveCheckout(cwd);
|
|
33
|
+
if (live)
|
|
34
|
+
return { ...live, learned: worktrees.get(live.root) };
|
|
35
|
+
const root = learnedRootOf(worktrees, cwd);
|
|
36
|
+
const learned = root === undefined ? undefined : worktrees.get(root);
|
|
37
|
+
if (root && learned)
|
|
38
|
+
return learnedCheckout(root, learned);
|
|
39
|
+
// A directory deleted since still lies in the checkout around it, as the live hook's walk finds.
|
|
40
|
+
const around = await checkoutOf(cwd);
|
|
41
|
+
const found = around && (await liveCheckout(around.root));
|
|
42
|
+
return found && { ...found, learned: worktrees.get(found.root) };
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* A worktree that is gone, answered by the checkouts that created it.
|
|
46
|
+
*
|
|
47
|
+
* ponytail: the creating folder must still resolve in git, and a path two repositories reused answers nothing,
|
|
48
|
+
* because one answer per folder cannot say which repository a turn's date meant. Ceiling: those folders' turns
|
|
49
|
+
* stay unplaced; upgrade path: resolve by the turn's time.
|
|
50
|
+
*
|
|
51
|
+
* @param root - The worktree's path.
|
|
52
|
+
* @param learned - Its recorded `git worktree add`s.
|
|
53
|
+
* @returns The checkout, when exactly one repository made it.
|
|
54
|
+
*/
|
|
55
|
+
async function learnedCheckout(root, learned) {
|
|
56
|
+
const mains = (await Promise.all([...new Set(learned.map((entry) => entry.main))].map(liveCheckout))).filter((main) => main !== undefined);
|
|
57
|
+
const main = mains[0];
|
|
58
|
+
return main && new Set(mains.map((each) => each.repository)).size === 1 ? { root, repository: main.repository, learned, gitRoot: main.root } : undefined;
|
|
59
|
+
}
|
|
60
|
+
async function liveCheckout(cwd) {
|
|
61
|
+
const root = await runGit(GIT_REPO_ROOT_ARGS, cwd, GIT_TIMEOUT_MS);
|
|
62
|
+
if (!root)
|
|
63
|
+
return undefined;
|
|
64
|
+
const { repository } = repositoryIdentity(root, await runGit(GIT_REMOTE_ARGS, root, GIT_TIMEOUT_MS));
|
|
65
|
+
return repository ? { root, repository, head: await headBranch(root) } : undefined;
|
|
66
|
+
}
|
|
67
|
+
async function headBranch(root) {
|
|
68
|
+
const branch = await runGit(GIT_BRANCH_ARGS, root, GIT_TIMEOUT_MS);
|
|
69
|
+
const linked = await fs.stat(path.join(root, '.git')).then((stat) => stat.isFile()).catch(() => false);
|
|
70
|
+
return branch && (linked || TRUNKS.has(branch)) ? branch : undefined;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The branch a turn was on in a checkout: the transcript's (only ever given for the turn's own folder), else what a
|
|
74
|
+
* `git worktree add` gave that path as of the turn, else the checkout's own, today.
|
|
75
|
+
*
|
|
76
|
+
* @param checkout - The checkout.
|
|
77
|
+
* @param at - When the turn ran.
|
|
78
|
+
* @param transcript - The transcript's branch, when it belongs to this checkout.
|
|
79
|
+
* @returns The branch, when anything says.
|
|
80
|
+
*/
|
|
81
|
+
function branchOn(checkout, at, transcript) {
|
|
82
|
+
const learned = checkout.learned;
|
|
83
|
+
const asOf = learned?.filter((entry) => entry.at <= at).at(-1) ?? learned?.[0];
|
|
84
|
+
return transcript ?? asOf?.branch ?? checkout.head;
|
|
85
|
+
}
|
|
86
|
+
/** How many distinct folders of a turn's tool files are asked about before giving up. */
|
|
87
|
+
const MAX_TOOL_FOLDERS = 10;
|
|
88
|
+
/**
|
|
89
|
+
* The checkout a turn reports, by the live hook's ladder as far as a transcript allows: `changed`, `carried`, then its folder.
|
|
90
|
+
*
|
|
91
|
+
* The checkout that holds the most of the turn's edits wins (first seen on a tie), so an edit in another worktree
|
|
92
|
+
* is no longer dropped for the folder the session sat in. A turn with no edit in any checkout falls to the checkout
|
|
93
|
+
* the session last changed, then its folder; a turn whose edits all lie elsewhere reports neither, as live
|
|
94
|
+
* (`fileEvidenceAllows`: a session that wrote files elsewhere is not working in the one its shell sits in).
|
|
95
|
+
* Reads name no checkout: the live `referenced` rung is weaker than `changed` and would send that repository's snapshot.
|
|
96
|
+
*
|
|
97
|
+
* @param turn - The turn.
|
|
98
|
+
* @param resolve - The run's resolver.
|
|
99
|
+
* @param accept - Whether a tool file's checkout may be used (it may belong to another root's identity); a refused one is passed over.
|
|
100
|
+
* @param carried - The checkout the session last changed.
|
|
101
|
+
* @returns The checkout and how it was known (no `evidence`: the folder); undefined when none resolves.
|
|
102
|
+
*/
|
|
103
|
+
export async function checkoutOfTurn(turn, resolve, accept = () => true, carried) {
|
|
104
|
+
const byFolder = turn.cwd ? await resolve(turn.cwd) : undefined;
|
|
105
|
+
const changed = await mostEdited(turn.tools, resolve, accept);
|
|
106
|
+
// The transcript's branch is the folder's, so it says nothing about any other checkout.
|
|
107
|
+
if (changed)
|
|
108
|
+
return { ...changed, evidence: 'changed', branch: branchOn(changed, turn.startedAt, changed.root === byFolder?.root ? turn.branch : undefined) };
|
|
109
|
+
if (turn.tools.some(isEdit))
|
|
110
|
+
return undefined;
|
|
111
|
+
// The edit that set `carried` named its branch then; the transcript names this turn's (none when detached), when it is the same folder.
|
|
112
|
+
if (carried)
|
|
113
|
+
return { ...carried, evidence: 'carried', branch: carried.root === byFolder?.root ? turn.branch : carried.branch };
|
|
114
|
+
return byFolder && { ...byFolder, branch: branchOn(byFolder, turn.startedAt, turn.branch) };
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Whether a tool call edited a file at a known place.
|
|
118
|
+
*
|
|
119
|
+
* @param tool - The call.
|
|
120
|
+
* @returns True for an edit with an absolute path.
|
|
121
|
+
*/
|
|
122
|
+
export function isEdit(tool) {
|
|
123
|
+
return tool.access === 'edit' && tool.path !== undefined && path.isAbsolute(tool.path);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* The checkout holding the most of the tools' edited files.
|
|
127
|
+
*
|
|
128
|
+
* @param tools - The turn's tools.
|
|
129
|
+
* @param resolve - The run's resolver.
|
|
130
|
+
* @param accept - Whether a checkout may be used.
|
|
131
|
+
* @returns The checkout; undefined when no edit lies in one that may be.
|
|
132
|
+
*/
|
|
133
|
+
async function mostEdited(tools, resolve, accept) {
|
|
134
|
+
const asked = new Set();
|
|
135
|
+
const edits = new Map();
|
|
136
|
+
for (const tool of tools.filter(isEdit)) {
|
|
137
|
+
const folder = path.dirname(tool.path);
|
|
138
|
+
if (!asked.has(folder) && asked.size >= MAX_TOOL_FOLDERS)
|
|
139
|
+
continue;
|
|
140
|
+
asked.add(folder);
|
|
141
|
+
const found = await resolve(folder);
|
|
142
|
+
if (!found || !accept(found))
|
|
143
|
+
continue;
|
|
144
|
+
const entry = edits.get(found.root) ?? { checkout: found, files: new Set() };
|
|
145
|
+
entry.files.add(tool.path);
|
|
146
|
+
edits.set(found.root, entry);
|
|
147
|
+
}
|
|
148
|
+
// Sorted is stable and Map keeps first-seen order, so a tie goes to the first seen.
|
|
149
|
+
return [...edits.values()].sort((a, b) => b.files.size - a.files.size)[0]?.checkout;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* The turn's tools with every file that lies in no checkout marked, as the live hook tells them apart.
|
|
153
|
+
*
|
|
154
|
+
* A folder that is gone answers nothing, so its files read as outside too, unless the path belongs to another
|
|
155
|
+
* identity's root or an ignored one, whose files are left out.
|
|
156
|
+
* ponytail: a root-scoped replay also leaves out files beyond its root, which a live hook would send. Ceiling: a
|
|
157
|
+
* root-scoped identity loses that outside-repo evidence; upgrade path: a scope port that tells "no root" from "another root".
|
|
158
|
+
* ponytail: asks git once per distinct folder of the run (the resolver remembers), with no per-turn cap. Ceiling:
|
|
159
|
+
* a machine whose agents touch tens of thousands of folders; the replay is niced and paced, so it only runs longer.
|
|
160
|
+
*
|
|
161
|
+
* @param tools - The turn's tools.
|
|
162
|
+
* @param resolve - The run's resolver.
|
|
163
|
+
* @param inScope - Whether a folder belongs to this identity.
|
|
164
|
+
* @returns A new list; a file inside some checkout is left unmarked.
|
|
165
|
+
*/
|
|
166
|
+
export async function markFilesOutsideCheckouts(tools, resolve, inScope = () => true) {
|
|
167
|
+
const marked = [];
|
|
168
|
+
for (const tool of tools) {
|
|
169
|
+
const folder = tool.path !== undefined && path.isAbsolute(tool.path) ? path.dirname(tool.path) : undefined;
|
|
170
|
+
marked.push(folder && inScope(folder) && !(await resolve(folder)) ? { ...tool, outside: true } : tool);
|
|
171
|
+
}
|
|
172
|
+
return marked;
|
|
173
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ReplaySource, ReplayTurn } from './types/replay.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Codex's rollouts as a replay source.
|
|
4
|
+
*
|
|
5
|
+
* @param home - `CODEX_HOME`, as the provider resolves it.
|
|
6
|
+
* @returns The source.
|
|
7
|
+
*/
|
|
8
|
+
export declare function codexSource(home: string): ReplaySource;
|
|
9
|
+
/**
|
|
10
|
+
* Stream a Codex rollout as the turns the live hook closed.
|
|
11
|
+
*
|
|
12
|
+
* A turn runs from `task_started` to its `task_complete` (or the next
|
|
13
|
+
* `task_started`, or the end of the file) and carries the rollout's `turn_id`,
|
|
14
|
+
* which is the id the live Codex hook sends. Only the rows named in
|
|
15
|
+
* {@link RE_CODEX_REPLAY_ROW} are parsed, plus the prompt rows when prompt text
|
|
16
|
+
* is allowed: messages, reasoning and tool output are never decoded. A turn
|
|
17
|
+
* with no token row did nothing billable and is not yielded. The branch and
|
|
18
|
+
* remote are `session_meta.git`, recorded once at session start, not per turn: still better than none.
|
|
19
|
+
*
|
|
20
|
+
* @param file - A `rollout-*.jsonl` file.
|
|
21
|
+
* @param promptText - Whether to keep the prompt's text; off unless the config allows it.
|
|
22
|
+
* @yields Turns, oldest first.
|
|
23
|
+
*/
|
|
24
|
+
export declare function readCodexTurns(file: string, promptText: boolean): AsyncGenerator<ReplayTurn>;
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import fsp from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import readline from 'node:readline';
|
|
5
|
+
import { asRecord, firstStringOf } from '../core/object.js';
|
|
6
|
+
import { sha256Hex } from '../events/event-id.js';
|
|
7
|
+
import { CODEX_EXEC_ORIGINATOR, CODEX_PROVIDER_ID } from '../providers/codex/constants/codex.constants.js';
|
|
8
|
+
import { codexRowUsage } from '../turns/codex-transcript.js';
|
|
9
|
+
import { DEFAULT_SURFACE, EXEC_SURFACE } from '../turns/constants/turns.constants.js';
|
|
10
|
+
import { accumulateUsage, messageIdFor, parseJsonLine } from '../turns/transcript.js';
|
|
11
|
+
import { CODEX_ROLLOUT_PREFIX, CODEX_SESSIONS_DIR, DETACHED_BRANCH, RE_CODEX_PROMPT_ROW, RE_CODEX_REPLAY_ROW, RE_CODEX_TURN_START, REPLAY_FALLBACK_TURN_PREFIX } from './constants/replay.constants.js';
|
|
12
|
+
import { estimateTurns, recentFiles } from './replay-scan.js';
|
|
13
|
+
/**
|
|
14
|
+
* Codex's rollouts as a replay source.
|
|
15
|
+
*
|
|
16
|
+
* @param home - `CODEX_HOME`, as the provider resolves it.
|
|
17
|
+
* @returns The source.
|
|
18
|
+
*/
|
|
19
|
+
export function codexSource(home) {
|
|
20
|
+
const sessions = path.join(home, CODEX_SESSIONS_DIR);
|
|
21
|
+
return {
|
|
22
|
+
agent: CODEX_PROVIDER_ID,
|
|
23
|
+
list: (sinceMs) => listRollouts(sessions, sinceMs),
|
|
24
|
+
estimate: (file, sinceMs) => estimateTurns(file, sinceMs, RE_CODEX_TURN_START),
|
|
25
|
+
identity: codexIdentity,
|
|
26
|
+
read: readCodexTurns
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The rollouts written inside the window, from `sessions/YYYY/MM/DD`.
|
|
31
|
+
*
|
|
32
|
+
* ponytail: every file is stat'ed; no day folder is skipped by its name, because a resumed
|
|
33
|
+
* session appends to the rollout in the folder of the day it began. About a second for ten
|
|
34
|
+
* thousand rollouts.
|
|
35
|
+
*
|
|
36
|
+
* @param sessions - `CODEX_HOME/sessions`.
|
|
37
|
+
* @param sinceMs - The window's start.
|
|
38
|
+
* @returns The files; empty when Codex has left none.
|
|
39
|
+
*/
|
|
40
|
+
async function listRollouts(sessions, sinceMs) {
|
|
41
|
+
const found = [];
|
|
42
|
+
for (const day of await subfolders(sessions, 3))
|
|
43
|
+
found.push(...(await recentFiles(day, sinceMs, CODEX_ROLLOUT_PREFIX)));
|
|
44
|
+
return found;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The folders `depth` levels below a folder.
|
|
48
|
+
*
|
|
49
|
+
* @param dir - Where to start.
|
|
50
|
+
* @param depth - How many levels down.
|
|
51
|
+
* @returns Their paths; empty when the folder is missing.
|
|
52
|
+
*/
|
|
53
|
+
async function subfolders(dir, depth) {
|
|
54
|
+
if (depth === 0)
|
|
55
|
+
return [dir];
|
|
56
|
+
const entries = await fsp.readdir(dir, { withFileTypes: true }).catch(() => []);
|
|
57
|
+
const found = [];
|
|
58
|
+
for (const entry of entries)
|
|
59
|
+
if (entry.isDirectory())
|
|
60
|
+
found.push(...(await subfolders(path.join(dir, entry.name), depth - 1)));
|
|
61
|
+
return found;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* A rollout's folder, remote, commit and branch: its first row, `session_meta`.
|
|
65
|
+
*
|
|
66
|
+
* @param file - A `rollout-*.jsonl` file.
|
|
67
|
+
* @returns What it says, or undefined when it has no folder.
|
|
68
|
+
*/
|
|
69
|
+
async function codexIdentity(file) {
|
|
70
|
+
const input = fs.createReadStream(file, { encoding: 'utf8' });
|
|
71
|
+
const lines = readline.createInterface({ input, crlfDelay: Infinity });
|
|
72
|
+
try {
|
|
73
|
+
for await (const line of lines) {
|
|
74
|
+
const row = parseJsonLine(line);
|
|
75
|
+
const payload = asRecord(row?.['payload']);
|
|
76
|
+
if (row?.['type'] !== 'session_meta' || !payload)
|
|
77
|
+
return undefined;
|
|
78
|
+
const { cwd, remote, commit, branch } = metaOf(payload);
|
|
79
|
+
return cwd ? { cwd, remote, commit, branch } : undefined;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
finally {
|
|
83
|
+
lines.close();
|
|
84
|
+
// Closing the reader leaves the file open; one descriptor per rollout adds up.
|
|
85
|
+
input.destroy();
|
|
86
|
+
}
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Stream a Codex rollout as the turns the live hook closed.
|
|
91
|
+
*
|
|
92
|
+
* A turn runs from `task_started` to its `task_complete` (or the next
|
|
93
|
+
* `task_started`, or the end of the file) and carries the rollout's `turn_id`,
|
|
94
|
+
* which is the id the live Codex hook sends. Only the rows named in
|
|
95
|
+
* {@link RE_CODEX_REPLAY_ROW} are parsed, plus the prompt rows when prompt text
|
|
96
|
+
* is allowed: messages, reasoning and tool output are never decoded. A turn
|
|
97
|
+
* with no token row did nothing billable and is not yielded. The branch and
|
|
98
|
+
* remote are `session_meta.git`, recorded once at session start, not per turn: still better than none.
|
|
99
|
+
*
|
|
100
|
+
* @param file - A `rollout-*.jsonl` file.
|
|
101
|
+
* @param promptText - Whether to keep the prompt's text; off unless the config allows it.
|
|
102
|
+
* @yields Turns, oldest first.
|
|
103
|
+
*/
|
|
104
|
+
export async function* readCodexTurns(file, promptText) {
|
|
105
|
+
const lines = readline.createInterface({ input: fs.createReadStream(file, { encoding: 'utf8' }), crlfDelay: Infinity });
|
|
106
|
+
let state = { meta: { surface: DEFAULT_SURFACE } };
|
|
107
|
+
try {
|
|
108
|
+
for await (const line of lines) {
|
|
109
|
+
if (!RE_CODEX_REPLAY_ROW.test(line) && !(promptText && RE_CODEX_PROMPT_ROW.test(line)))
|
|
110
|
+
continue;
|
|
111
|
+
const row = parseJsonLine(line);
|
|
112
|
+
const payload = asRecord(row?.['payload']);
|
|
113
|
+
if (!row || !payload)
|
|
114
|
+
continue;
|
|
115
|
+
const step = stepRow(state, row, payload, line);
|
|
116
|
+
state = step.state;
|
|
117
|
+
if (step.closed)
|
|
118
|
+
yield step.closed;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
finally {
|
|
122
|
+
lines.close();
|
|
123
|
+
}
|
|
124
|
+
const last = state.open && closeTurn(state.open, state.meta);
|
|
125
|
+
if (last)
|
|
126
|
+
yield last;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* One row read: the state it leaves, and the turn it closed, if any.
|
|
130
|
+
*
|
|
131
|
+
* @param state - What the rollout said so far.
|
|
132
|
+
* @param row - The parsed row.
|
|
133
|
+
* @param payload - Its payload.
|
|
134
|
+
* @param line - The verbatim line.
|
|
135
|
+
* @returns The next state and a closed turn.
|
|
136
|
+
*/
|
|
137
|
+
function stepRow(state, row, payload, line) {
|
|
138
|
+
if (row['type'] === 'session_meta')
|
|
139
|
+
return { state: { ...state, meta: metaOf(payload) } };
|
|
140
|
+
const model = row['type'] === 'turn_context' ? (firstStringOf(payload['model']) ?? state.model) : state.model;
|
|
141
|
+
if (payload['type'] === 'task_started') {
|
|
142
|
+
const at = firstStringOf(row['timestamp']);
|
|
143
|
+
const open = at ? { turnId: firstStringOf(payload['turn_id']), startedAt: at, endedAt: at, model, usage: new Map() } : undefined;
|
|
144
|
+
return { state: { ...state, model, open }, closed: state.open && closeTurn(state.open, state.meta) };
|
|
145
|
+
}
|
|
146
|
+
if (!state.open)
|
|
147
|
+
return { state: { ...state, model } };
|
|
148
|
+
const open = foldRow(state.open, row, payload, line);
|
|
149
|
+
if (payload['type'] !== 'task_complete')
|
|
150
|
+
return { state: { ...state, model, open } };
|
|
151
|
+
return { state: { ...state, model, open: undefined }, closed: closeTurn(open, state.meta) };
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* The session's id, folder and surface, from `session_meta`.
|
|
155
|
+
*
|
|
156
|
+
* @param payload - The row's payload.
|
|
157
|
+
* @returns What it says.
|
|
158
|
+
*/
|
|
159
|
+
function metaOf(payload) {
|
|
160
|
+
const git = asRecord(payload['git']);
|
|
161
|
+
const branch = firstStringOf(git?.['branch']);
|
|
162
|
+
return {
|
|
163
|
+
sessionId: firstStringOf(payload['id']),
|
|
164
|
+
cwd: firstStringOf(payload['cwd']),
|
|
165
|
+
branch: branch === DETACHED_BRANCH ? undefined : branch,
|
|
166
|
+
remote: firstStringOf(git?.['repository_url']),
|
|
167
|
+
commit: firstStringOf(git?.['commit_hash']),
|
|
168
|
+
// As the live hook resolves it (`codexSurface`): a headless `codex exec` run.
|
|
169
|
+
surface: payload['originator'] === CODEX_EXEC_ORIGINATOR ? EXEC_SURFACE : DEFAULT_SURFACE
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Fold one row into the open turn.
|
|
174
|
+
*
|
|
175
|
+
* @param open - The turn read so far.
|
|
176
|
+
* @param row - The parsed row.
|
|
177
|
+
* @param payload - Its payload.
|
|
178
|
+
* @param line - The verbatim line, whose hash keys a token row as the live reader keys it.
|
|
179
|
+
* @returns The turn with the row in it.
|
|
180
|
+
*/
|
|
181
|
+
function foldRow(open, row, payload, line) {
|
|
182
|
+
const at = firstStringOf(row['timestamp']);
|
|
183
|
+
const timed = at && at > open.endedAt ? { ...open, endedAt: at } : open;
|
|
184
|
+
if (row['type'] === 'turn_context') {
|
|
185
|
+
return { ...timed, turnId: timed.turnId ?? firstStringOf(payload['turn_id']), cwd: firstStringOf(payload['cwd']) ?? timed.cwd, model: firstStringOf(payload['model']) ?? timed.model };
|
|
186
|
+
}
|
|
187
|
+
if (payload['type'] === 'user_message')
|
|
188
|
+
return timed.promptText === undefined ? { ...timed, promptText: firstStringOf(payload['message']) } : timed;
|
|
189
|
+
const usage = codexRowUsage(payload);
|
|
190
|
+
return usage ? { ...timed, usage: new Map(timed.usage).set(messageIdFor(undefined, line), { model: timed.model, usage }) } : timed;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* The turn a run of rows has become.
|
|
194
|
+
*
|
|
195
|
+
* @param open - The turn read so far.
|
|
196
|
+
* @param meta - The session's `session_meta`.
|
|
197
|
+
* @returns The turn, or undefined when it has no session id, no folder or no token row.
|
|
198
|
+
*/
|
|
199
|
+
function closeTurn(open, meta) {
|
|
200
|
+
const usage = accumulateUsage(open.usage);
|
|
201
|
+
const cwd = open.cwd ?? meta.cwd;
|
|
202
|
+
// No folder: no way to tell whether it ran under `ignoredRoots` or another root's identity.
|
|
203
|
+
if (!usage || !meta.sessionId || !cwd)
|
|
204
|
+
return undefined;
|
|
205
|
+
return {
|
|
206
|
+
provider: CODEX_PROVIDER_ID,
|
|
207
|
+
sessionId: meta.sessionId,
|
|
208
|
+
// A rollout from before turn ids: stable per session and start, so a second replay agrees with the first.
|
|
209
|
+
turnId: open.turnId ?? REPLAY_FALLBACK_TURN_PREFIX + sha256Hex(`${meta.sessionId}${open.startedAt}`).slice(0, 32),
|
|
210
|
+
startedAt: open.startedAt,
|
|
211
|
+
endedAt: open.endedAt,
|
|
212
|
+
cwd,
|
|
213
|
+
branch: meta.branch,
|
|
214
|
+
remote: meta.remote,
|
|
215
|
+
commit: meta.commit,
|
|
216
|
+
surface: meta.surface,
|
|
217
|
+
usage,
|
|
218
|
+
tools: [],
|
|
219
|
+
promptText: open.promptText?.trim() || undefined
|
|
220
|
+
};
|
|
221
|
+
}
|