@agent-watch-ai/edge 0.7.0 → 0.7.2
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 +92 -0
- package/README.md +4 -1
- package/dist/billing/session-billing.d.ts +5 -2
- package/dist/billing/session-billing.js +27 -6
- package/dist/capture/capture-env.d.ts +3 -3
- package/dist/capture/capture-env.js +5 -5
- package/dist/capture/captured-step.js +24 -0
- package/dist/capture/constants/capture.constants.d.ts +15 -0
- package/dist/capture/constants/capture.constants.js +15 -0
- package/dist/capture/launched-runs.d.ts +27 -0
- package/dist/capture/launched-runs.js +15 -9
- package/dist/capture/python/sitecustomize.py +11 -2
- 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 +21 -0
- package/dist/cli/constants/cli.constants.js +22 -1
- package/dist/cli/doctor-run-capture.d.ts +8 -0
- package/dist/cli/doctor-run-capture.js +14 -0
- package/dist/cli/doctor.js +6 -2
- package/dist/cli/hook-check.d.ts +7 -2
- package/dist/cli/hook-check.js +30 -15
- package/dist/cli/hook.js +44 -3
- 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.js +42 -7
- package/dist/cli/setup.js +71 -7
- package/dist/cli/types/cli.types.d.ts +2 -0
- package/dist/cli/uninstall.js +34 -1
- package/dist/cli/update.js +3 -2
- package/dist/cli.js +4 -1
- package/dist/config/config-store.d.ts +15 -4
- package/dist/config/config-store.js +31 -7
- package/dist/config/config.d.ts +8 -0
- package/dist/config/config.js +10 -0
- package/dist/config/constants/config.constants.d.ts +13 -1
- package/dist/config/constants/config.constants.js +13 -1
- package/dist/config/repo-config.js +7 -2
- package/dist/config/schemas/config.schema.d.ts +6 -2
- package/dist/config/schemas/config.schema.js +17 -3
- package/dist/credentials/cloud-claim.d.ts +2 -0
- package/dist/credentials/cloud-claim.js +21 -2
- package/dist/credentials/constants/credentials.constants.d.ts +8 -0
- package/dist/credentials/constants/credentials.constants.js +8 -0
- 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/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/enforcement/enforcement.js +1 -1
- package/dist/events/enrich.d.ts +14 -0
- package/dist/events/enrich.js +1 -1
- package/dist/git/constants/git.constants.d.ts +2 -0
- package/dist/git/constants/git.constants.js +9 -0
- package/dist/git/repo-snapshot.js +8 -2
- package/dist/git/types/snapshot.types.d.ts +4 -0
- package/dist/pipeline/hook-pipeline.js +5 -2
- 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 +12 -6
- 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/providers/types/provider.types.d.ts +5 -5
- package/dist/replay/checkout-resolver.d.ts +41 -14
- package/dist/replay/checkout-resolver.js +134 -30
- package/dist/replay/replay-runner.d.ts +3 -1
- package/dist/replay/replay-runner.js +74 -22
- package/dist/replay/replay-scan.d.ts +2 -1
- package/dist/replay/replay-scan.js +14 -2
- package/dist/replay/throttle.d.ts +2 -1
- package/dist/replay/throttle.js +4 -1
- package/dist/replay/transcript-turns.js +29 -3
- package/dist/replay/turn-event.d.ts +4 -2
- package/dist/replay/turn-event.js +22 -11
- package/dist/replay/types/replay.types.d.ts +39 -3
- package/dist/replay/worktree-map.d.ts +42 -0
- package/dist/replay/worktree-map.js +134 -0
- package/dist/snapshot/snapshot-pipeline.js +15 -4
- package/dist/snapshot/types/snapshot.types.d.ts +2 -0
- 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 +67 -0
- package/dist/turns/constants/turns.constants.js +67 -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 +51 -1
- package/dist/turns/turn-state.js +124 -10
- package/dist/turns/turn-tracker.js +156 -18
- 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 +11 -6
- package/docs/ENTERPRISE_DEPLOYMENT.md +2 -2
- package/package.json +1 -1
|
@@ -1,69 +1,173 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
1
2
|
import path from 'node:path';
|
|
2
|
-
import {
|
|
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';
|
|
3
5
|
import { repositoryIdentity, runGit } from '../git/git-context.js';
|
|
6
|
+
import { learnedRootOf } from './worktree-map.js';
|
|
4
7
|
/**
|
|
5
8
|
* A resolver that remembers its answers for the run.
|
|
6
9
|
*
|
|
7
|
-
*
|
|
8
|
-
* folder that no longer exists answers
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
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.
|
|
12
15
|
*
|
|
16
|
+
* @param worktrees - What the transcripts recorded about worktrees; filled before the first answer.
|
|
13
17
|
* @returns The resolver.
|
|
14
18
|
*/
|
|
15
|
-
export function createCheckoutResolver() {
|
|
19
|
+
export function createCheckoutResolver(worktrees = new Map()) {
|
|
16
20
|
const answers = new Map();
|
|
17
21
|
return (cwd) => {
|
|
18
22
|
const known = answers.get(cwd);
|
|
19
23
|
if (known)
|
|
20
24
|
return known;
|
|
21
|
-
const asked = resolveCheckout(cwd);
|
|
25
|
+
const asked = resolveCheckout(cwd, worktrees);
|
|
22
26
|
answers.set(cwd, asked);
|
|
23
27
|
return asked;
|
|
24
28
|
};
|
|
25
29
|
}
|
|
26
|
-
|
|
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) {
|
|
27
61
|
const root = await runGit(GIT_REPO_ROOT_ARGS, cwd, GIT_TIMEOUT_MS);
|
|
28
62
|
if (!root)
|
|
29
63
|
return undefined;
|
|
30
64
|
const { repository } = repositoryIdentity(root, await runGit(GIT_REMOTE_ARGS, root, GIT_TIMEOUT_MS));
|
|
31
|
-
return repository ? { root, repository } : undefined;
|
|
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;
|
|
32
85
|
}
|
|
33
86
|
/** How many distinct folders of a turn's tool files are asked about before giving up. */
|
|
34
87
|
const MAX_TOOL_FOLDERS = 10;
|
|
35
88
|
/**
|
|
36
|
-
* The checkout a turn
|
|
89
|
+
* The checkout a turn reports, by the live hook's ladder as far as a transcript allows: `changed`, `carried`, then its folder.
|
|
37
90
|
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
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.
|
|
42
96
|
*
|
|
43
97
|
* @param turn - The turn.
|
|
44
98
|
* @param resolve - The run's resolver.
|
|
45
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.
|
|
46
|
-
* @
|
|
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.
|
|
47
102
|
*/
|
|
48
|
-
export async function checkoutOfTurn(turn, resolve, accept = () => true) {
|
|
103
|
+
export async function checkoutOfTurn(turn, resolve, accept = () => true, carried) {
|
|
49
104
|
const byFolder = turn.cwd ? await resolve(turn.cwd) : undefined;
|
|
50
|
-
|
|
51
|
-
|
|
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) {
|
|
52
134
|
const asked = new Set();
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
for (const tool of turn.tools) {
|
|
56
|
-
if (tool.access !== 'edit' || !tool.path || !path.isAbsolute(tool.path))
|
|
57
|
-
continue;
|
|
135
|
+
const edits = new Map();
|
|
136
|
+
for (const tool of tools.filter(isEdit)) {
|
|
58
137
|
const folder = path.dirname(tool.path);
|
|
59
|
-
if (asked.has(folder))
|
|
138
|
+
if (!asked.has(folder) && asked.size >= MAX_TOOL_FOLDERS)
|
|
60
139
|
continue;
|
|
61
|
-
if (asked.size >= MAX_TOOL_FOLDERS)
|
|
62
|
-
break;
|
|
63
140
|
asked.add(folder);
|
|
64
141
|
const found = await resolve(folder);
|
|
65
|
-
if (found
|
|
66
|
-
|
|
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);
|
|
67
171
|
}
|
|
68
|
-
return
|
|
172
|
+
return marked;
|
|
69
173
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ReplayPorts, ReplayRecord } from './types/replay.types.js';
|
|
1
|
+
import type { ReplayPorts, ReplayRecord, ReplayWindow } from './types/replay.types.js';
|
|
2
2
|
/** What a run of `agentwatch replay` ended as. */
|
|
3
3
|
export type ReplayOutcome = 'done' | 'paused' | 'stopped' | 'skipped' | 'failed';
|
|
4
4
|
/** What a dry run counted, in place of sending. */
|
|
@@ -25,6 +25,8 @@ export interface ReplayResult {
|
|
|
25
25
|
/** What a run was asked to do. */
|
|
26
26
|
export interface ReplayRequest {
|
|
27
27
|
readonly days: number;
|
|
28
|
+
/** Operator-only historical window. Automatic replay never supplies one. */
|
|
29
|
+
readonly window?: ReplayWindow;
|
|
28
30
|
readonly resume: boolean;
|
|
29
31
|
/** Build every record and count it; send nothing, write nothing. */
|
|
30
32
|
readonly dryRun: boolean;
|
|
@@ -8,11 +8,12 @@ import { CANONICAL_PROVIDER_NAMES } from '../otlp/constants/otlp.constants.js';
|
|
|
8
8
|
import { runSnapshotPipeline } from '../snapshot/snapshot-pipeline.js';
|
|
9
9
|
import { SnapshotStateStore } from '../snapshot/snapshot-state.js';
|
|
10
10
|
import { runTreePipeline } from '../snapshot/tree-pipeline.js';
|
|
11
|
+
import { SNAPSHOT_BRANCH_COUNT } from '../snapshot/constants/snapshot.constants.js';
|
|
11
12
|
import { acquireLock } from '../storage/lock.js';
|
|
12
13
|
import { ANY_DESTINATION, AUTH_REJECTED_STATUSES } from '../transport/constants/transport.constants.js';
|
|
13
14
|
import { sendEvents } from '../transport/send.js';
|
|
14
15
|
import { PREPASS_PERSIST_EVERY, REPLAY_BATCH_INTERVAL_MS, REPLAY_BATCH_TURNS, REPLAY_FILE_PAUSE_MS, REPLAY_GIVE_UP_FAILURES, REPLAY_HEARTBEAT_MS, REPLAY_LIVE_TURN_GRACE_MS, REPLAY_NICE, REPLAY_PROGRESS_EVERY_BATCHES, REPLAY_RUN_BUDGET_MS, REPLAY_SNAPSHOT_BUDGET_MS } from './constants/replay.constants.js';
|
|
15
|
-
import { checkoutOfTurn } from './checkout-resolver.js';
|
|
16
|
+
import { checkoutOfTurn, isEdit, markFilesOutsideCheckouts } from './checkout-resolver.js';
|
|
16
17
|
import { buildReplayProgress } from './progress-event.js';
|
|
17
18
|
import { countsByAgent, listSources, sourceOf } from './replay-scan.js';
|
|
18
19
|
import { decideStart, isRunning, readReplayRecord, setPending, writeReplayRecord } from './replay-state.js';
|
|
@@ -36,7 +37,7 @@ const SCAN_PERSIST_EVERY = 20;
|
|
|
36
37
|
* @returns How it ended.
|
|
37
38
|
*/
|
|
38
39
|
export async function runReplay(ports, request) {
|
|
39
|
-
const claimed = request.dryRun ? { record: freshRecord(ports, request.days), fresh: true } : await claim(ports, request);
|
|
40
|
+
const claimed = request.dryRun ? { record: freshRecord(ports, request.days, request.window), fresh: true } : await claim(ports, request);
|
|
40
41
|
if ('result' in claimed)
|
|
41
42
|
return claimed.result;
|
|
42
43
|
lowerPriority();
|
|
@@ -60,7 +61,7 @@ async function claim(ports, request) {
|
|
|
60
61
|
if (decision.action === 'skip')
|
|
61
62
|
return { result: { outcome: 'skipped', message: decision.message } };
|
|
62
63
|
const fresh = decision.action === 'fresh';
|
|
63
|
-
const record = fresh ? freshRecord(ports, request.days) : continuedRecord(decision.record, ports);
|
|
64
|
+
const record = fresh ? freshRecord(ports, request.days, request.window) : continuedRecord(decision.record, ports, request.window);
|
|
64
65
|
await writeReplayRecord(ports.stateFile, record);
|
|
65
66
|
await setPending(ports.stateFile, true);
|
|
66
67
|
return { record, fresh };
|
|
@@ -69,11 +70,12 @@ async function claim(ports, request) {
|
|
|
69
70
|
await release();
|
|
70
71
|
}
|
|
71
72
|
}
|
|
72
|
-
function freshRecord(ports, days) {
|
|
73
|
+
function freshRecord(ports, days, explicit) {
|
|
73
74
|
const now = ports.env.now();
|
|
74
|
-
const window = replayWindow(now, days);
|
|
75
|
+
const window = explicit ?? replayWindow(now, days);
|
|
75
76
|
const at = now.toISOString();
|
|
76
77
|
return {
|
|
78
|
+
operator: explicit !== undefined,
|
|
77
79
|
replayId: crypto.randomUUID(),
|
|
78
80
|
identity: ports.identity,
|
|
79
81
|
state: 'scanning',
|
|
@@ -104,11 +106,13 @@ function freshRecord(ports, days) {
|
|
|
104
106
|
* @param ports - The machine.
|
|
105
107
|
* @returns The record, owned by this process.
|
|
106
108
|
*/
|
|
107
|
-
function continuedRecord(record, ports) {
|
|
109
|
+
function continuedRecord(record, ports, explicit) {
|
|
108
110
|
const now = ports.env.now();
|
|
109
|
-
|
|
111
|
+
// A hook resumes with no operator environment, so the record has to remember the window it was given.
|
|
112
|
+
const pinned = explicit ?? (record.operator === true ? { start: new Date(record.windowStart), end: new Date(record.windowEnd) } : undefined);
|
|
113
|
+
const start = pinned?.start.getTime() ?? Math.max(Date.parse(record.windowStart), firstOpenInstant(now).getTime());
|
|
110
114
|
const at = now.toISOString();
|
|
111
|
-
return { ...record, state: 'running', pause: undefined, windowStart: new Date(start).toISOString(), windowEnd: at, updatedAt: at, pid: process.pid, heartbeatAt: at };
|
|
115
|
+
return { ...record, operator: pinned !== undefined, state: 'running', pause: undefined, windowStart: new Date(start).toISOString(), windowEnd: pinned?.end.toISOString() ?? at, updatedAt: at, pid: process.pid, heartbeatAt: at };
|
|
112
116
|
}
|
|
113
117
|
/** `nice` 19, so the operating system hands every other process the CPU first. Fails open: some systems refuse. */
|
|
114
118
|
function lowerPriority() {
|
|
@@ -233,7 +237,7 @@ async function execute(ports, request, start, fresh) {
|
|
|
233
237
|
return 'backend';
|
|
234
238
|
if (transport.destination && (await ports.authBlock.holds(transport.destination)))
|
|
235
239
|
return 'backend';
|
|
236
|
-
const verdict = (await ports.cooldown.active()) ? { go: false, pause: 'backend', waitMs: REPLAY_HEARTBEAT_MS } : throttleVerdict(await ports.machine());
|
|
240
|
+
const verdict = (await ports.cooldown.active()) ? { go: false, pause: 'backend', waitMs: REPLAY_HEARTBEAT_MS } : throttleVerdict(await ports.machine(), record.operator === true);
|
|
237
241
|
if (verdict.go) {
|
|
238
242
|
if (record.state === 'paused') {
|
|
239
243
|
await persist({ state: 'running', pause: undefined });
|
|
@@ -337,9 +341,11 @@ async function execute(ports, request, start, fresh) {
|
|
|
337
341
|
.filter(([, entry]) => !entry.done)
|
|
338
342
|
.sort(([, a], [, b]) => b.mtimeMs - a.mtimeMs)
|
|
339
343
|
.map(([file]) => file);
|
|
340
|
-
const cutoffMs = clock() - REPLAY_LIVE_TURN_GRACE_MS;
|
|
344
|
+
const cutoffMs = Math.min(clock() - REPLAY_LIVE_TURN_GRACE_MS, Date.parse(record.windowEnd));
|
|
341
345
|
const seen = new Set();
|
|
342
|
-
|
|
346
|
+
// Seen repositories outlive a pause: a resumed leg reads only the files still pending, and the snapshot of a
|
|
347
|
+
// repository it never reads again must still force the branches an earlier leg saw.
|
|
348
|
+
const repositories = new Map(Object.entries(record.repositories ?? {}).map(([root, seen]) => [root, { ...seen, branches: new Set(seen.branches) }]));
|
|
343
349
|
const context = { developerId: ports.developerId, developerName: ports.developerName, installationId: ports.installationId, capture, billingMode: ports.billingMode };
|
|
344
350
|
let buffer = [];
|
|
345
351
|
let finished = [];
|
|
@@ -360,7 +366,8 @@ async function execute(ports, request, start, fresh) {
|
|
|
360
366
|
turnsSent: record.turnsSent + (dry ? 0 : buffer.length),
|
|
361
367
|
// Distinct sessions: a resumed Codex session leaves one rollout file per resume. A file with no turn counts as its own.
|
|
362
368
|
sessionsSent: new Set(Object.entries(files).filter(([, entry]) => entry.done).map(([name, entry]) => entry.session ?? name)).size,
|
|
363
|
-
batches: record.batches + 1
|
|
369
|
+
batches: record.batches + 1,
|
|
370
|
+
repositories: Object.fromEntries([...repositories].map(([root, seen]) => [root, { ...seen, branches: [...seen.branches] }]))
|
|
364
371
|
});
|
|
365
372
|
if (!dry && buffer.length > 0) {
|
|
366
373
|
if (record.batches % REPLAY_PROGRESS_EVERY_BATCHES === 0)
|
|
@@ -379,7 +386,27 @@ async function execute(ports, request, start, fresh) {
|
|
|
379
386
|
// a folder that is gone belongs to a root by its path alone, as for the live hook.
|
|
380
387
|
const folders = new Map();
|
|
381
388
|
const knownRepos = new Map();
|
|
389
|
+
const carriedBySession = new Map();
|
|
382
390
|
const newestFirst = Object.entries(record.files).sort(([, x], [, y]) => y.mtimeMs - x.mtimeMs);
|
|
391
|
+
// Before anything is resolved: the resolver remembers its answers.
|
|
392
|
+
// A worktree another root's checkout made is that root's: learned here, its repository would be read and snapshotted.
|
|
393
|
+
// Over a long history this pass outlasts the stale-heartbeat limit, and a second replay would start beside it.
|
|
394
|
+
const beat = setInterval(() => void persist({}).catch(() => undefined), REPLAY_HEARTBEAT_MS);
|
|
395
|
+
try {
|
|
396
|
+
for (const source of ports.sources)
|
|
397
|
+
ports.learnWorktrees?.(((await source.worktreeAdds?.(() => stopRequested).catch(() => [])) ?? []).filter((entry) => ports.inScope(entry.main)));
|
|
398
|
+
}
|
|
399
|
+
finally {
|
|
400
|
+
clearInterval(beat);
|
|
401
|
+
}
|
|
402
|
+
await persist({});
|
|
403
|
+
if (stopRequested)
|
|
404
|
+
return finish('stop');
|
|
405
|
+
// `capture.git: false` skips the child-checkout scan: its git reads would serve only snapshots that never go.
|
|
406
|
+
for (const checkout of (capture.git === false ? [] : await ports.localCheckouts?.().catch(() => [])) ?? []) {
|
|
407
|
+
if (ports.inScope(checkout.root) && !knownRepos.has(checkout.repository))
|
|
408
|
+
knownRepos.set(checkout.repository, checkout.root);
|
|
409
|
+
}
|
|
383
410
|
for (const [index, [file, entry]] of newestFirst.entries()) {
|
|
384
411
|
// A long pass must still look alive, and still hear a `--stop`.
|
|
385
412
|
if (index % PREPASS_PERSIST_EVERY === 0)
|
|
@@ -425,22 +452,41 @@ async function execute(ports, request, start, fresh) {
|
|
|
425
452
|
// What a live hook in that folder would capture: a repository's own file may narrow it.
|
|
426
453
|
// A repository found some other way (a tool's file, a gone folder's remote) answers to its own checkout's file.
|
|
427
454
|
let turnCapture = turn.cwd ? await ports.captureFor(turn.cwd) : capture;
|
|
428
|
-
let checkout = turnCapture.git !== false ? await checkoutOfTurn(turn, ports.checkout, (found) => ports.inScope(found.root)) : undefined;
|
|
455
|
+
let checkout = turnCapture.git !== false ? await checkoutOfTurn(turn, ports.checkout, (found) => ports.inScope(found.root), carriedBySession.get(turn.sessionId)) : undefined;
|
|
456
|
+
if (checkout?.evidence === 'changed')
|
|
457
|
+
carriedBySession.set(turn.sessionId, checkout);
|
|
429
458
|
// ponytail: also learned as turns resolve, for Claude- and Gemini-only checkouts the pre-pass cannot see;
|
|
430
459
|
// a gone folder read before its repository is first seen answers to the global capture. Ceiling: a
|
|
431
460
|
// pre-pass over every transcript's folder; upgrade path: an `identity` on those sources.
|
|
432
461
|
if (checkout && !knownRepos.has(checkout.repository))
|
|
433
|
-
knownRepos.set(checkout.repository, checkout.root);
|
|
462
|
+
knownRepos.set(checkout.repository, checkout.gitRoot ?? checkout.root);
|
|
434
463
|
// Another folder's own capture file narrows the turn's, never widens it.
|
|
435
|
-
const otherRoot = checkout ? checkout.evidence && checkout.root : rootOfRemote(turn);
|
|
464
|
+
const otherRoot = checkout ? (checkout.gitRoot ?? (checkout.evidence && checkout.root)) : rootOfRemote(turn);
|
|
436
465
|
if (otherRoot)
|
|
437
466
|
turnCapture = narrower(turnCapture, await ports.captureFor(otherRoot));
|
|
438
|
-
if (
|
|
467
|
+
if (turnCapture.git === false)
|
|
439
468
|
checkout = undefined;
|
|
440
469
|
sessionOfFile.set(file, turn.sessionId);
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
470
|
+
// A turn whose edits all lie in no checkout reports none (`checkoutOfTurn`), so it borrows no folder identity either.
|
|
471
|
+
const replayTurn = checkout || turn.tools.some(isEdit) ? turn : withFolderIdentity(turn);
|
|
472
|
+
const remoteRoot = checkout || turnCapture.git === false ? undefined : rootOfRemote(replayTurn);
|
|
473
|
+
const snapshotRoot = checkout?.gitRoot ?? checkout?.root ?? remoteRoot;
|
|
474
|
+
const repository = checkout?.repository ?? (replayTurn.remote ? repositoryIdentity('', replayTurn.remote).remote : undefined);
|
|
475
|
+
if (snapshotRoot && repository) {
|
|
476
|
+
const seenRepository = repositories.get(snapshotRoot) ?? {
|
|
477
|
+
repository,
|
|
478
|
+
sessionId: turn.sessionId,
|
|
479
|
+
surface: turn.surface ?? 'cli',
|
|
480
|
+
provider: turn.provider,
|
|
481
|
+
branches: new Set()
|
|
482
|
+
};
|
|
483
|
+
const sentBranch = checkout?.branch ?? (checkout?.evidence ? undefined : replayTurn.branch);
|
|
484
|
+
if (sentBranch)
|
|
485
|
+
seenRepository.branches.add(sentBranch);
|
|
486
|
+
repositories.set(snapshotRoot, seenRepository);
|
|
487
|
+
}
|
|
488
|
+
const placed = { ...replayTurn, tools: turnCapture.git !== false ? await markFilesOutsideCheckouts(replayTurn.tools, ports.checkout, ports.inScope) : replayTurn.tools };
|
|
489
|
+
buffer.push(buildReplayTurnEvent(placed, checkout, { ...context, capture: turnCapture, billingMode, home: ports.env.home }));
|
|
444
490
|
builtBy.set(turn.provider, (builtBy.get(turn.provider) ?? 0) + 1);
|
|
445
491
|
if (buffer.length < REPLAY_BATCH_TURNS)
|
|
446
492
|
continue;
|
|
@@ -503,12 +549,18 @@ async function snapshotOf(ports, root, seen) {
|
|
|
503
549
|
capturedAt: ports.env.now().toISOString(),
|
|
504
550
|
// Real time, as the hook's: this bounds git subprocesses, which the wall clock times.
|
|
505
551
|
deadline: Date.now() + REPLAY_SNAPSHOT_BUDGET_MS,
|
|
506
|
-
run: runGit
|
|
552
|
+
run: runGit,
|
|
553
|
+
requiredBranches: [...seen.branches]
|
|
507
554
|
};
|
|
508
555
|
const store = new SnapshotStateStore(ports.paths.snapshotsDir);
|
|
509
|
-
|
|
556
|
+
const required = input.requiredBranches.length > 0 ? input.requiredBranches : [undefined];
|
|
557
|
+
for (let index = 0; index < required.length; index += SNAPSHOT_BRANCH_COUNT) {
|
|
558
|
+
const branches = required[0] === undefined ? [] : required.slice(index, index + SNAPSHOT_BRANCH_COUNT);
|
|
559
|
+
// Each group, and the tree, gets the whole budget: one deadline for all would leave the later groups none.
|
|
560
|
+
await runSnapshotPipeline({ input: { ...input, requiredBranches: branches, deadline: Date.now() + REPLAY_SNAPSHOT_BUDGET_MS }, store, queue: ports.queue });
|
|
561
|
+
}
|
|
510
562
|
if (input.files)
|
|
511
|
-
await runTreePipeline({ input, store, queue: ports.queue });
|
|
563
|
+
await runTreePipeline({ input: { ...input, deadline: Date.now() + REPLAY_SNAPSHOT_BUDGET_MS }, store, queue: ports.queue });
|
|
512
564
|
const transport = await ports.transport();
|
|
513
565
|
if (transport)
|
|
514
566
|
await ports.queue.drain(transport, ports.config.delivery.drainBatchSize, undefined, ports.authBlock);
|
|
@@ -26,9 +26,10 @@ export declare function claudeProjectsDir(home: string): string;
|
|
|
26
26
|
* Claude Code's transcripts as a replay source.
|
|
27
27
|
*
|
|
28
28
|
* @param projectsDir - `~/.claude/projects`.
|
|
29
|
+
* @param home - The developer's home, for `~/` in a command.
|
|
29
30
|
* @returns The source.
|
|
30
31
|
*/
|
|
31
|
-
export declare function claudeSource(projectsDir: string): ReplaySource;
|
|
32
|
+
export declare function claudeSource(projectsDir: string, home?: string): ReplaySource;
|
|
32
33
|
/**
|
|
33
34
|
* Every source's files, newest first, each tagged with its source.
|
|
34
35
|
*
|
|
@@ -6,6 +6,7 @@ import { sleep } from '../core/async.js';
|
|
|
6
6
|
import { CLAUDE_PROVIDER_ID } from '../providers/claude/constants/claude.constants.js';
|
|
7
7
|
import { CLAUDE_PROJECTS_SEGMENTS } from './constants/replay.constants.js';
|
|
8
8
|
import { readTranscriptTurns } from './transcript-turns.js';
|
|
9
|
+
import { worktreesInTranscript } from './worktree-map.js';
|
|
9
10
|
/** The pause after each hundred project folders, so a thousand of them are not one burst of disk reads. */
|
|
10
11
|
const FOLDER_BURST = 100;
|
|
11
12
|
const FOLDER_PAUSE_MS = 20;
|
|
@@ -42,14 +43,25 @@ export function claudeProjectsDir(home) {
|
|
|
42
43
|
* Claude Code's transcripts as a replay source.
|
|
43
44
|
*
|
|
44
45
|
* @param projectsDir - `~/.claude/projects`.
|
|
46
|
+
* @param home - The developer's home, for `~/` in a command.
|
|
45
47
|
* @returns The source.
|
|
46
48
|
*/
|
|
47
|
-
export function claudeSource(projectsDir) {
|
|
49
|
+
export function claudeSource(projectsDir, home) {
|
|
48
50
|
return {
|
|
49
51
|
agent: CLAUDE_PROVIDER_ID,
|
|
50
52
|
list: (sinceMs) => listTranscripts(projectsDir, sinceMs),
|
|
51
53
|
estimate: (file, sinceMs) => estimateTurns(file, sinceMs),
|
|
52
|
-
read: readTranscriptTurns
|
|
54
|
+
read: readTranscriptTurns,
|
|
55
|
+
// Every transcript, not the window's: the worktree a turn ran in is usually made by an earlier session.
|
|
56
|
+
worktreeAdds: async (stop) => {
|
|
57
|
+
const learned = [];
|
|
58
|
+
for (const { file } of await listTranscripts(projectsDir, 0)) {
|
|
59
|
+
if (stop?.())
|
|
60
|
+
break;
|
|
61
|
+
learned.push(...(await worktreesInTranscript(file, home)));
|
|
62
|
+
}
|
|
63
|
+
return learned;
|
|
64
|
+
}
|
|
53
65
|
};
|
|
54
66
|
}
|
|
55
67
|
/**
|
|
@@ -15,9 +15,10 @@ export type ThrottleVerdict = {
|
|
|
15
15
|
* load to show it. Load and battery are the two things a laptop owner feels.
|
|
16
16
|
*
|
|
17
17
|
* @param machine - What the machine says about itself.
|
|
18
|
+
* @param operatorRun - Operator replays are foreground lab work, not polite background work.
|
|
18
19
|
* @returns Go, or the reason and the wait.
|
|
19
20
|
*/
|
|
20
|
-
export declare function throttleVerdict(machine: MachineLoad): ThrottleVerdict;
|
|
21
|
+
export declare function throttleVerdict(machine: MachineLoad, operatorRun?: boolean): ThrottleVerdict;
|
|
21
22
|
/**
|
|
22
23
|
* How long to wait after another failed request: 10 s, doubling, never over 5 minutes.
|
|
23
24
|
*
|
package/dist/replay/throttle.js
CHANGED
|
@@ -7,9 +7,12 @@ import { REPLAY_BASE_BACKOFF_MS, REPLAY_BUSY_LOAD_PER_CORE, REPLAY_BUSY_PAUSE_MS
|
|
|
7
7
|
* load to show it. Load and battery are the two things a laptop owner feels.
|
|
8
8
|
*
|
|
9
9
|
* @param machine - What the machine says about itself.
|
|
10
|
+
* @param operatorRun - Operator replays are foreground lab work, not polite background work.
|
|
10
11
|
* @returns Go, or the reason and the wait.
|
|
11
12
|
*/
|
|
12
|
-
export function throttleVerdict(machine) {
|
|
13
|
+
export function throttleVerdict(machine, operatorRun = false) {
|
|
14
|
+
if (operatorRun)
|
|
15
|
+
return { go: true };
|
|
13
16
|
if (machine.hookAgeMs < REPLAY_HOOK_QUIET_MS)
|
|
14
17
|
return { go: false, pause: 'agent-active', waitMs: REPLAY_HOOK_QUIET_MS - machine.hookAgeMs };
|
|
15
18
|
if (machine.onBattery)
|
|
@@ -43,7 +43,7 @@ export async function* readTranscriptTurns(filePath, promptText) {
|
|
|
43
43
|
const closed = open && closeTurn(open);
|
|
44
44
|
if (closed)
|
|
45
45
|
yield closed;
|
|
46
|
-
open = { prompt, endedAt: prompt.at, assistantLines: 0, usage: new Map(), tools: [], toolIds: new Set(), keepText: promptText };
|
|
46
|
+
open = { prompt, endedAt: prompt.at, assistantLines: 0, usage: new Map(), tools: [], toolIdList: [], toolIds: new Set(), failedToolIds: new Set(), keepText: promptText };
|
|
47
47
|
continue;
|
|
48
48
|
}
|
|
49
49
|
if (open)
|
|
@@ -126,7 +126,7 @@ function closeTurn(open) {
|
|
|
126
126
|
branch: open.branch,
|
|
127
127
|
surface: open.surface,
|
|
128
128
|
usage: accumulateUsage(open.usage),
|
|
129
|
-
tools: open.tools,
|
|
129
|
+
tools: open.failedToolIds.size === 0 ? open.tools : open.tools.map((tool, index) => (open.failedToolIds.has(open.toolIdList[index] ?? '') ? { name: tool.name } : tool)),
|
|
130
130
|
promptText: text || undefined
|
|
131
131
|
};
|
|
132
132
|
}
|
|
@@ -141,6 +141,8 @@ function closeTurn(open) {
|
|
|
141
141
|
function extendTurn(open, entry, line) {
|
|
142
142
|
// Only the agent's own lines date the turn and place it: a slash command typed after the
|
|
143
143
|
// answer, or a line another process appended, is not where the turn ended.
|
|
144
|
+
if (entry['type'] === 'user')
|
|
145
|
+
return withFailures(open, entry);
|
|
144
146
|
if (entry['type'] !== 'assistant')
|
|
145
147
|
return open;
|
|
146
148
|
const at = entry['timestamp'];
|
|
@@ -186,6 +188,7 @@ function withAssistant(open, entry, line) {
|
|
|
186
188
|
if (!Array.isArray(content))
|
|
187
189
|
return { ...open, usage: counted };
|
|
188
190
|
const tools = [...open.tools];
|
|
191
|
+
const toolIdList = [...open.toolIdList];
|
|
189
192
|
const toolIds = new Set(open.toolIds);
|
|
190
193
|
for (const block of content) {
|
|
191
194
|
const call = toolUseOf(asRecord(block));
|
|
@@ -194,8 +197,31 @@ function withAssistant(open, entry, line) {
|
|
|
194
197
|
if (call.id !== undefined)
|
|
195
198
|
toolIds.add(call.id);
|
|
196
199
|
tools.push(call.tool);
|
|
200
|
+
toolIdList.push(call.id);
|
|
197
201
|
}
|
|
198
|
-
return { ...open, usage: counted, tools, toolIds };
|
|
202
|
+
return { ...open, usage: counted, tools, toolIdList, toolIds };
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* The turn with the calls a tool-result line says failed.
|
|
206
|
+
*
|
|
207
|
+
* @param open - The turn read so far.
|
|
208
|
+
* @param entry - A parsed user line.
|
|
209
|
+
* @returns The turn, with those call ids marked.
|
|
210
|
+
*/
|
|
211
|
+
function withFailures(open, entry) {
|
|
212
|
+
const content = asRecord(entry['message'])?.['content'];
|
|
213
|
+
if (!Array.isArray(content))
|
|
214
|
+
return open;
|
|
215
|
+
let failed;
|
|
216
|
+
for (const block of content) {
|
|
217
|
+
const result = asRecord(block);
|
|
218
|
+
const id = result?.['tool_use_id'];
|
|
219
|
+
if (result?.['type'] !== 'tool_result' || result['is_error'] !== true || typeof id !== 'string')
|
|
220
|
+
continue;
|
|
221
|
+
failed ??= new Set(open.failedToolIds);
|
|
222
|
+
failed.add(id);
|
|
223
|
+
}
|
|
224
|
+
return failed ? { ...open, failedToolIds: failed } : open;
|
|
199
225
|
}
|
|
200
226
|
/**
|
|
201
227
|
* A tool call out of a content block, with the id that makes it countable once.
|
|
@@ -10,12 +10,14 @@ export interface ReplayTurnContext {
|
|
|
10
10
|
/** The effective capture flags, consent gate applied: the same ones a live hook obeys. */
|
|
11
11
|
readonly capture: CaptureConfig;
|
|
12
12
|
readonly billingMode?: UsageBillingMode;
|
|
13
|
+
/** The developer's home, to send an outside file home-relative; without it no outside path is sent. */
|
|
14
|
+
readonly home?: string;
|
|
13
15
|
}
|
|
14
16
|
/**
|
|
15
17
|
* The `turn.summary` a transcript turn becomes: the live hook's record, built by the live hook's builder.
|
|
16
18
|
*
|
|
17
|
-
* It differs from a live one only where a transcript knows less:
|
|
18
|
-
*
|
|
19
|
+
* It differs from a live one only where a transcript knows less: only a Codex
|
|
20
|
+
* rollout's session-start commit is known, no tool events, no
|
|
19
21
|
* session titles, and a repository and branch read from the turn's last
|
|
20
22
|
* folder and `gitBranch`, not from the checkout as it is now. The record's
|
|
21
23
|
* `turn_id` is the transcript's prompt id, which is the live hook's turn id,
|