@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
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { ReplayFileEntry, ReplaySource, TranscriptFile } from './types/replay.types.js';
|
|
2
|
+
/** A listed file and the source it belongs to. */
|
|
3
|
+
export interface SourcedFile extends TranscriptFile {
|
|
4
|
+
readonly agent: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Files and built turns per agent, for the dry run.
|
|
8
|
+
*
|
|
9
|
+
* @param files - The files the run read.
|
|
10
|
+
* @param entries - The record's file map that lists them.
|
|
11
|
+
* @param builtBy - Turns built per agent.
|
|
12
|
+
* @returns The counts.
|
|
13
|
+
*/
|
|
14
|
+
export declare function countsByAgent(files: readonly string[], entries: Readonly<Record<string, ReplayFileEntry>>, builtBy: ReadonlyMap<string, number>): Record<string, {
|
|
15
|
+
readonly files: number;
|
|
16
|
+
readonly turns: number;
|
|
17
|
+
}>;
|
|
18
|
+
/**
|
|
19
|
+
* Where Claude Code keeps its transcripts.
|
|
20
|
+
*
|
|
21
|
+
* @param home - The developer's home.
|
|
22
|
+
* @returns `~/.claude/projects`.
|
|
23
|
+
*/
|
|
24
|
+
export declare function claudeProjectsDir(home: string): string;
|
|
25
|
+
/**
|
|
26
|
+
* Claude Code's transcripts as a replay source.
|
|
27
|
+
*
|
|
28
|
+
* @param projectsDir - `~/.claude/projects`.
|
|
29
|
+
* @param home - The developer's home, for `~/` in a command.
|
|
30
|
+
* @returns The source.
|
|
31
|
+
*/
|
|
32
|
+
export declare function claudeSource(projectsDir: string, home?: string): ReplaySource;
|
|
33
|
+
/**
|
|
34
|
+
* Every source's files, newest first, each tagged with its source.
|
|
35
|
+
*
|
|
36
|
+
* @param sources - The agents to list.
|
|
37
|
+
* @param sinceMs - The window's start.
|
|
38
|
+
* @returns The files.
|
|
39
|
+
*/
|
|
40
|
+
export declare function listSources(sources: readonly ReplaySource[], sinceMs: number): Promise<SourcedFile[]>;
|
|
41
|
+
/**
|
|
42
|
+
* The source a counted file belongs to. A file counted before Codex and Gemini replay carries no agent: it is Claude Code's.
|
|
43
|
+
*
|
|
44
|
+
* @param sources - The run's sources.
|
|
45
|
+
* @param entry - The file's entry in the record.
|
|
46
|
+
* @returns The source, or undefined when this run has no source for it.
|
|
47
|
+
*/
|
|
48
|
+
export declare function sourceOf(sources: readonly ReplaySource[], entry: ReplayFileEntry | undefined): ReplaySource | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* The transcripts that may hold a turn from the window, newest first.
|
|
51
|
+
*
|
|
52
|
+
* A file last written before the window began cannot hold a turn inside it, so
|
|
53
|
+
* one `stat` rules it out without opening it. Only `<project>/<session>.jsonl`
|
|
54
|
+
* is listed: the sub-agent files below a session are counted in their parent's
|
|
55
|
+
* turn, as live hooks count them.
|
|
56
|
+
*
|
|
57
|
+
* @param projectsDir - `~/.claude/projects`.
|
|
58
|
+
* @param sinceMs - The window's start.
|
|
59
|
+
* @returns The files, most recently written first; empty when Claude Code has left none.
|
|
60
|
+
*/
|
|
61
|
+
export declare function listTranscripts(projectsDir: string, sinceMs: number): Promise<TranscriptFile[]>;
|
|
62
|
+
/**
|
|
63
|
+
* The `.jsonl` files directly in a folder last written inside the window: one `stat` each, no open.
|
|
64
|
+
*
|
|
65
|
+
* @param dir - The folder.
|
|
66
|
+
* @param sinceMs - The window's start.
|
|
67
|
+
* @param prefix - A name prefix the files must carry, if any.
|
|
68
|
+
* @returns The files; empty when the folder is missing.
|
|
69
|
+
*/
|
|
70
|
+
export declare function recentFiles(dir: string, sinceMs: number, prefix?: string): Promise<TranscriptFile[]>;
|
|
71
|
+
/**
|
|
72
|
+
* About how many prompts a file holds from the window, without parsing it.
|
|
73
|
+
*
|
|
74
|
+
* Reads the file as text and tests each line against a pattern: no JSON is
|
|
75
|
+
* decoded, which is what makes the count pass cheap. It is an estimate for the
|
|
76
|
+
* progress bar (it counts a prompt that got no answer, which is never sent);
|
|
77
|
+
* the sent total replaces it when the replay ends.
|
|
78
|
+
*
|
|
79
|
+
* @param file - A transcript file.
|
|
80
|
+
* @param sinceMs - The window's start.
|
|
81
|
+
* @param opensTurn - What a line that opens a turn looks like; Claude Code's typed prompt by default.
|
|
82
|
+
* @returns The estimate.
|
|
83
|
+
*/
|
|
84
|
+
export declare function estimateTurns(file: string, sinceMs: number, opensTurn?: RegExp): Promise<number>;
|
|
@@ -0,0 +1,182 @@
|
|
|
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 { sleep } from '../core/async.js';
|
|
6
|
+
import { CLAUDE_PROVIDER_ID } from '../providers/claude/constants/claude.constants.js';
|
|
7
|
+
import { CLAUDE_PROJECTS_SEGMENTS } from './constants/replay.constants.js';
|
|
8
|
+
import { readTranscriptTurns } from './transcript-turns.js';
|
|
9
|
+
import { worktreesInTranscript } from './worktree-map.js';
|
|
10
|
+
/** The pause after each hundred project folders, so a thousand of them are not one burst of disk reads. */
|
|
11
|
+
const FOLDER_BURST = 100;
|
|
12
|
+
const FOLDER_PAUSE_MS = 20;
|
|
13
|
+
const TRANSCRIPT_SUFFIX = '.jsonl';
|
|
14
|
+
/** The leading fields of a prompt line a person typed, before its message: no braces, so no nesting to scan. */
|
|
15
|
+
const RE_TYPED_PROMPT = /^\{[^{}]{0,400}"isSidechain":false[^{}]{0,400}"type":"user","message":\{"role":"user","content":"(?!<command-|<local-command|\[Request interrupted)/;
|
|
16
|
+
const TIMESTAMP_KEY = '"timestamp":"';
|
|
17
|
+
/**
|
|
18
|
+
* Files and built turns per agent, for the dry run.
|
|
19
|
+
*
|
|
20
|
+
* @param files - The files the run read.
|
|
21
|
+
* @param entries - The record's file map that lists them.
|
|
22
|
+
* @param builtBy - Turns built per agent.
|
|
23
|
+
* @returns The counts.
|
|
24
|
+
*/
|
|
25
|
+
export function countsByAgent(files, entries, builtBy) {
|
|
26
|
+
const counts = {};
|
|
27
|
+
for (const file of files) {
|
|
28
|
+
const agent = entries[file]?.agent ?? CLAUDE_PROVIDER_ID;
|
|
29
|
+
counts[agent] = { files: (counts[agent]?.files ?? 0) + 1, turns: builtBy.get(agent) ?? 0 };
|
|
30
|
+
}
|
|
31
|
+
return counts;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Where Claude Code keeps its transcripts.
|
|
35
|
+
*
|
|
36
|
+
* @param home - The developer's home.
|
|
37
|
+
* @returns `~/.claude/projects`.
|
|
38
|
+
*/
|
|
39
|
+
export function claudeProjectsDir(home) {
|
|
40
|
+
return path.join(home, ...CLAUDE_PROJECTS_SEGMENTS);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Claude Code's transcripts as a replay source.
|
|
44
|
+
*
|
|
45
|
+
* @param projectsDir - `~/.claude/projects`.
|
|
46
|
+
* @param home - The developer's home, for `~/` in a command.
|
|
47
|
+
* @returns The source.
|
|
48
|
+
*/
|
|
49
|
+
export function claudeSource(projectsDir, home) {
|
|
50
|
+
return {
|
|
51
|
+
agent: CLAUDE_PROVIDER_ID,
|
|
52
|
+
list: (sinceMs) => listTranscripts(projectsDir, sinceMs),
|
|
53
|
+
estimate: (file, sinceMs) => estimateTurns(file, sinceMs),
|
|
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
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Every source's files, newest first, each tagged with its source.
|
|
69
|
+
*
|
|
70
|
+
* @param sources - The agents to list.
|
|
71
|
+
* @param sinceMs - The window's start.
|
|
72
|
+
* @returns The files.
|
|
73
|
+
*/
|
|
74
|
+
export async function listSources(sources, sinceMs) {
|
|
75
|
+
const found = [];
|
|
76
|
+
for (const source of sources)
|
|
77
|
+
for (const file of await source.list(sinceMs))
|
|
78
|
+
found.push({ ...file, agent: source.agent });
|
|
79
|
+
return found.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The source a counted file belongs to. A file counted before Codex and Gemini replay carries no agent: it is Claude Code's.
|
|
83
|
+
*
|
|
84
|
+
* @param sources - The run's sources.
|
|
85
|
+
* @param entry - The file's entry in the record.
|
|
86
|
+
* @returns The source, or undefined when this run has no source for it.
|
|
87
|
+
*/
|
|
88
|
+
export function sourceOf(sources, entry) {
|
|
89
|
+
const agent = entry?.agent ?? CLAUDE_PROVIDER_ID;
|
|
90
|
+
return sources.find((source) => source.agent === agent);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The transcripts that may hold a turn from the window, newest first.
|
|
94
|
+
*
|
|
95
|
+
* A file last written before the window began cannot hold a turn inside it, so
|
|
96
|
+
* one `stat` rules it out without opening it. Only `<project>/<session>.jsonl`
|
|
97
|
+
* is listed: the sub-agent files below a session are counted in their parent's
|
|
98
|
+
* turn, as live hooks count them.
|
|
99
|
+
*
|
|
100
|
+
* @param projectsDir - `~/.claude/projects`.
|
|
101
|
+
* @param sinceMs - The window's start.
|
|
102
|
+
* @returns The files, most recently written first; empty when Claude Code has left none.
|
|
103
|
+
*/
|
|
104
|
+
export async function listTranscripts(projectsDir, sinceMs) {
|
|
105
|
+
const folders = await fsp.readdir(projectsDir, { withFileTypes: true }).catch(() => []);
|
|
106
|
+
const found = [];
|
|
107
|
+
let seen = 0;
|
|
108
|
+
for (const folder of folders) {
|
|
109
|
+
if (!folder.isDirectory())
|
|
110
|
+
continue;
|
|
111
|
+
seen += 1;
|
|
112
|
+
if (seen % FOLDER_BURST === 0)
|
|
113
|
+
await sleep(FOLDER_PAUSE_MS);
|
|
114
|
+
found.push(...(await recentFiles(path.join(projectsDir, folder.name), sinceMs)));
|
|
115
|
+
}
|
|
116
|
+
return found.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* The `.jsonl` files directly in a folder last written inside the window: one `stat` each, no open.
|
|
120
|
+
*
|
|
121
|
+
* @param dir - The folder.
|
|
122
|
+
* @param sinceMs - The window's start.
|
|
123
|
+
* @param prefix - A name prefix the files must carry, if any.
|
|
124
|
+
* @returns The files; empty when the folder is missing.
|
|
125
|
+
*/
|
|
126
|
+
export async function recentFiles(dir, sinceMs, prefix = '') {
|
|
127
|
+
const names = await fsp.readdir(dir).catch(() => []);
|
|
128
|
+
const found = [];
|
|
129
|
+
for (const name of names) {
|
|
130
|
+
if (!name.startsWith(prefix) || !name.endsWith(TRANSCRIPT_SUFFIX))
|
|
131
|
+
continue;
|
|
132
|
+
const file = path.join(dir, name);
|
|
133
|
+
const stat = await fsp.stat(file).catch(() => undefined);
|
|
134
|
+
if (stat?.isFile() && stat.mtimeMs >= sinceMs)
|
|
135
|
+
found.push({ file, mtimeMs: stat.mtimeMs, size: stat.size });
|
|
136
|
+
}
|
|
137
|
+
return found;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* About how many prompts a file holds from the window, without parsing it.
|
|
141
|
+
*
|
|
142
|
+
* Reads the file as text and tests each line against a pattern: no JSON is
|
|
143
|
+
* decoded, which is what makes the count pass cheap. It is an estimate for the
|
|
144
|
+
* progress bar (it counts a prompt that got no answer, which is never sent);
|
|
145
|
+
* the sent total replaces it when the replay ends.
|
|
146
|
+
*
|
|
147
|
+
* @param file - A transcript file.
|
|
148
|
+
* @param sinceMs - The window's start.
|
|
149
|
+
* @param opensTurn - What a line that opens a turn looks like; Claude Code's typed prompt by default.
|
|
150
|
+
* @returns The estimate.
|
|
151
|
+
*/
|
|
152
|
+
export async function estimateTurns(file, sinceMs, opensTurn = RE_TYPED_PROMPT) {
|
|
153
|
+
const lines = readline.createInterface({ input: fs.createReadStream(file, { encoding: 'utf8' }), crlfDelay: Infinity });
|
|
154
|
+
let turns = 0;
|
|
155
|
+
try {
|
|
156
|
+
for await (const line of lines) {
|
|
157
|
+
if (!opensTurn.test(line) || timestampOf(line) < sinceMs)
|
|
158
|
+
continue;
|
|
159
|
+
turns += 1;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
return turns;
|
|
164
|
+
}
|
|
165
|
+
finally {
|
|
166
|
+
lines.close();
|
|
167
|
+
}
|
|
168
|
+
return turns;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* The time a transcript line carries, read off its last `timestamp` key.
|
|
172
|
+
*
|
|
173
|
+
* @param line - The verbatim line.
|
|
174
|
+
* @returns Epoch ms; 0 when the line has none, so it counts as outside the window.
|
|
175
|
+
*/
|
|
176
|
+
function timestampOf(line) {
|
|
177
|
+
const at = line.lastIndexOf(TIMESTAMP_KEY);
|
|
178
|
+
if (at < 0)
|
|
179
|
+
return 0;
|
|
180
|
+
const from = at + TIMESTAMP_KEY.length;
|
|
181
|
+
return Date.parse(line.slice(from, line.indexOf('"', from))) || 0;
|
|
182
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type { ReplayRecord } from './types/replay.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Where an identity's replay state lives: beside its stats and credential files.
|
|
4
|
+
*
|
|
5
|
+
* @param statsFile - The identity's stats file, which names its state directory.
|
|
6
|
+
* @returns The replay state file.
|
|
7
|
+
*/
|
|
8
|
+
export declare function replayStateFile(statsFile: string): string;
|
|
9
|
+
/**
|
|
10
|
+
* A replay's persisted state, when there is one that parses.
|
|
11
|
+
*
|
|
12
|
+
* @param file - The replay state file.
|
|
13
|
+
* @returns The record, or undefined when nothing was ever written or it is unreadable.
|
|
14
|
+
*/
|
|
15
|
+
export declare function readReplayRecord(file: string): Promise<ReplayRecord | undefined>;
|
|
16
|
+
/**
|
|
17
|
+
* Persist the record, atomically, so a crash leaves the last whole one.
|
|
18
|
+
*
|
|
19
|
+
* @param file - The replay state file.
|
|
20
|
+
* @param record - The state.
|
|
21
|
+
*/
|
|
22
|
+
export declare function writeReplayRecord(file: string, record: ReplayRecord): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Claim setup's one automatic replay for this machine.
|
|
25
|
+
*
|
|
26
|
+
* The marker is credential-independent: signing in again must not resend the
|
|
27
|
+
* same local history. Existing per-identity state from older Edge versions is
|
|
28
|
+
* adopted as an earlier attempt before the marker is claimed atomically.
|
|
29
|
+
*
|
|
30
|
+
* @param configDir - Durable machine configuration directory.
|
|
31
|
+
* @param dataDir - Machine state directory containing identity partitions.
|
|
32
|
+
* @returns True only for the caller that may start the automatic replay.
|
|
33
|
+
*/
|
|
34
|
+
export declare function claimAutomaticReplay(configDir: string, dataDir: string): Promise<boolean>;
|
|
35
|
+
/**
|
|
36
|
+
* Whether setup's automatic replay has already been attempted on this machine.
|
|
37
|
+
*
|
|
38
|
+
* Per-identity records predate the machine marker. Finding one adopts it by
|
|
39
|
+
* writing the marker, so a credential change cannot make old installs repeat.
|
|
40
|
+
*
|
|
41
|
+
* @param configDir - Durable machine configuration directory.
|
|
42
|
+
* @param dataDir - Machine state directory containing identity partitions.
|
|
43
|
+
* @returns True when setup must not launch another automatic replay.
|
|
44
|
+
*/
|
|
45
|
+
export declare function automaticReplayAttempted(configDir: string, dataDir: string): Promise<boolean>;
|
|
46
|
+
/**
|
|
47
|
+
* Whether a process is working on this replay right now.
|
|
48
|
+
*
|
|
49
|
+
* The heartbeat is what counts: a pid can be reused, and a lock file is broken
|
|
50
|
+
* after 30 s while a run lasts minutes. A live heartbeat from a process that
|
|
51
|
+
* answers signal 0 is a run; anything else is a run that died.
|
|
52
|
+
*
|
|
53
|
+
* @param record - The persisted state.
|
|
54
|
+
* @param nowMs - The clock.
|
|
55
|
+
* @param alive - Whether a pid exists; injectable for tests.
|
|
56
|
+
* @returns True when a run holds it.
|
|
57
|
+
*/
|
|
58
|
+
export declare function isRunning(record: ReplayRecord, nowMs: number, alive?: (pid: number) => boolean): boolean;
|
|
59
|
+
/** What a replay was asked to do, as {@link decideStart} reads it. */
|
|
60
|
+
export interface StartRequest {
|
|
61
|
+
/** Fingerprint of the identity this run sends as. */
|
|
62
|
+
readonly identity: string;
|
|
63
|
+
/** `replay --resume`: continue an unfinished one, never begin one. */
|
|
64
|
+
readonly resume: boolean;
|
|
65
|
+
/** Setup's start: never repeat an earlier replay attempt for this identity. */
|
|
66
|
+
readonly skipIfExisting: boolean;
|
|
67
|
+
readonly nowMs: number;
|
|
68
|
+
}
|
|
69
|
+
/** What to do about the replay already on disk, if any. */
|
|
70
|
+
export type StartDecision = {
|
|
71
|
+
readonly action: 'skip';
|
|
72
|
+
readonly message: string;
|
|
73
|
+
} | {
|
|
74
|
+
readonly action: 'continue';
|
|
75
|
+
readonly record: ReplayRecord;
|
|
76
|
+
} | {
|
|
77
|
+
readonly action: 'fresh';
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Whether a run begins a replay, continues one or leaves it.
|
|
81
|
+
*
|
|
82
|
+
* Pure so every combination is a test: a second `setup`, a hook that finds an
|
|
83
|
+
* unfinished replay, `--resume` after `--stop`, a different identity.
|
|
84
|
+
*
|
|
85
|
+
* @param existing - The persisted state, if any.
|
|
86
|
+
* @param request - What was asked.
|
|
87
|
+
* @param alive - Whether a pid exists; injectable for tests.
|
|
88
|
+
* @returns The decision.
|
|
89
|
+
*/
|
|
90
|
+
export declare function decideStart(existing: ReplayRecord | undefined, request: StartRequest, alive?: (pid: number) => boolean): StartDecision;
|
|
91
|
+
/**
|
|
92
|
+
* The marker that says a replay is unfinished.
|
|
93
|
+
*
|
|
94
|
+
* Beside the state file, and empty: the hook asks "is anything owed?" with one
|
|
95
|
+
* `stat` of this, where reading the state file (a map of every transcript) on
|
|
96
|
+
* every hook would cost real time.
|
|
97
|
+
*
|
|
98
|
+
* @param stateFile - The replay state file.
|
|
99
|
+
* @returns The marker's path.
|
|
100
|
+
*/
|
|
101
|
+
export declare function pendingMarker(stateFile: string): string;
|
|
102
|
+
/**
|
|
103
|
+
* Create the marker, or remove it.
|
|
104
|
+
*
|
|
105
|
+
* @param stateFile - The replay state file.
|
|
106
|
+
* @param pending - Whether a replay is unfinished.
|
|
107
|
+
*/
|
|
108
|
+
export declare function setPending(stateFile: string, pending: boolean): Promise<void>;
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { SECRET_FILE_MODE } from '../storage/constants/storage.constants.js';
|
|
4
|
+
import { writeFileAtomic } from '../storage/atomic-file.js';
|
|
5
|
+
import { readJsonFile } from '../storage/json-file.js';
|
|
6
|
+
import { IDENTITY_STATE_DIR_NAME } from '../transport/constants/transport.constants.js';
|
|
7
|
+
import { REPLAY_ATTEMPTED_FILE_NAME, REPLAY_HEARTBEAT_STALE_MS, REPLAY_PENDING_FILE_NAME, REPLAY_STATE_FILE_NAME } from './constants/replay.constants.js';
|
|
8
|
+
/**
|
|
9
|
+
* Where an identity's replay state lives: beside its stats and credential files.
|
|
10
|
+
*
|
|
11
|
+
* @param statsFile - The identity's stats file, which names its state directory.
|
|
12
|
+
* @returns The replay state file.
|
|
13
|
+
*/
|
|
14
|
+
export function replayStateFile(statsFile) {
|
|
15
|
+
return path.join(path.dirname(statsFile), REPLAY_STATE_FILE_NAME);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* A replay's persisted state, when there is one that parses.
|
|
19
|
+
*
|
|
20
|
+
* @param file - The replay state file.
|
|
21
|
+
* @returns The record, or undefined when nothing was ever written or it is unreadable.
|
|
22
|
+
*/
|
|
23
|
+
export async function readReplayRecord(file) {
|
|
24
|
+
const read = await readJsonFile(file);
|
|
25
|
+
if (read.state !== 'ok')
|
|
26
|
+
return undefined;
|
|
27
|
+
const record = read.value;
|
|
28
|
+
if (!record || typeof record.replayId !== 'string' || typeof record.state !== 'string' || typeof record.files !== 'object')
|
|
29
|
+
return undefined;
|
|
30
|
+
return record;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Persist the record, atomically, so a crash leaves the last whole one.
|
|
34
|
+
*
|
|
35
|
+
* @param file - The replay state file.
|
|
36
|
+
* @param record - The state.
|
|
37
|
+
*/
|
|
38
|
+
export async function writeReplayRecord(file, record) {
|
|
39
|
+
await writeFileAtomic(file, `${JSON.stringify(record)}\n`, SECRET_FILE_MODE);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Claim setup's one automatic replay for this machine.
|
|
43
|
+
*
|
|
44
|
+
* The marker is credential-independent: signing in again must not resend the
|
|
45
|
+
* same local history. Existing per-identity state from older Edge versions is
|
|
46
|
+
* adopted as an earlier attempt before the marker is claimed atomically.
|
|
47
|
+
*
|
|
48
|
+
* @param configDir - Durable machine configuration directory.
|
|
49
|
+
* @param dataDir - Machine state directory containing identity partitions.
|
|
50
|
+
* @returns True only for the caller that may start the automatic replay.
|
|
51
|
+
*/
|
|
52
|
+
export async function claimAutomaticReplay(configDir, dataDir) {
|
|
53
|
+
if (await automaticReplayAttempted(configDir, dataDir))
|
|
54
|
+
return false;
|
|
55
|
+
const marker = path.join(configDir, REPLAY_ATTEMPTED_FILE_NAME);
|
|
56
|
+
return createClaim(marker);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Whether setup's automatic replay has already been attempted on this machine.
|
|
60
|
+
*
|
|
61
|
+
* Per-identity records predate the machine marker. Finding one adopts it by
|
|
62
|
+
* writing the marker, so a credential change cannot make old installs repeat.
|
|
63
|
+
*
|
|
64
|
+
* @param configDir - Durable machine configuration directory.
|
|
65
|
+
* @param dataDir - Machine state directory containing identity partitions.
|
|
66
|
+
* @returns True when setup must not launch another automatic replay.
|
|
67
|
+
*/
|
|
68
|
+
export async function automaticReplayAttempted(configDir, dataDir) {
|
|
69
|
+
const marker = path.join(configDir, REPLAY_ATTEMPTED_FILE_NAME);
|
|
70
|
+
if (await hasFile(marker))
|
|
71
|
+
return true;
|
|
72
|
+
if (await hasReplayRecord(dataDir)) {
|
|
73
|
+
await createClaim(marker);
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
async function hasReplayRecord(dataDir) {
|
|
79
|
+
const root = path.join(dataDir, IDENTITY_STATE_DIR_NAME);
|
|
80
|
+
let entries;
|
|
81
|
+
try {
|
|
82
|
+
entries = await fs.readdir(root, { withFileTypes: true });
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
if (error.code === 'ENOENT')
|
|
86
|
+
return false;
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
for (const entry of entries) {
|
|
90
|
+
if (!entry.isDirectory())
|
|
91
|
+
continue;
|
|
92
|
+
if (await readReplayRecord(path.join(root, entry.name, REPLAY_STATE_FILE_NAME)))
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
async function createClaim(marker) {
|
|
98
|
+
await fs.mkdir(path.dirname(marker), { recursive: true });
|
|
99
|
+
try {
|
|
100
|
+
await fs.writeFile(marker, '', { flag: 'wx', mode: SECRET_FILE_MODE });
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
if (error.code === 'EEXIST')
|
|
105
|
+
return false;
|
|
106
|
+
throw error;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
async function hasFile(file) {
|
|
110
|
+
try {
|
|
111
|
+
await fs.stat(file);
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
if (error.code === 'ENOENT')
|
|
116
|
+
return false;
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Whether a process is working on this replay right now.
|
|
122
|
+
*
|
|
123
|
+
* The heartbeat is what counts: a pid can be reused, and a lock file is broken
|
|
124
|
+
* after 30 s while a run lasts minutes. A live heartbeat from a process that
|
|
125
|
+
* answers signal 0 is a run; anything else is a run that died.
|
|
126
|
+
*
|
|
127
|
+
* @param record - The persisted state.
|
|
128
|
+
* @param nowMs - The clock.
|
|
129
|
+
* @param alive - Whether a pid exists; injectable for tests.
|
|
130
|
+
* @returns True when a run holds it.
|
|
131
|
+
*/
|
|
132
|
+
export function isRunning(record, nowMs, alive = pidExists) {
|
|
133
|
+
if (record.pid === undefined || record.heartbeatAt === undefined)
|
|
134
|
+
return false;
|
|
135
|
+
if (nowMs - Date.parse(record.heartbeatAt) > REPLAY_HEARTBEAT_STALE_MS)
|
|
136
|
+
return false;
|
|
137
|
+
return alive(record.pid);
|
|
138
|
+
}
|
|
139
|
+
function pidExists(pid) {
|
|
140
|
+
try {
|
|
141
|
+
process.kill(pid, 0);
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
return error.code === 'EPERM';
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Whether a run begins a replay, continues one or leaves it.
|
|
150
|
+
*
|
|
151
|
+
* Pure so every combination is a test: a second `setup`, a hook that finds an
|
|
152
|
+
* unfinished replay, `--resume` after `--stop`, a different identity.
|
|
153
|
+
*
|
|
154
|
+
* @param existing - The persisted state, if any.
|
|
155
|
+
* @param request - What was asked.
|
|
156
|
+
* @param alive - Whether a pid exists; injectable for tests.
|
|
157
|
+
* @returns The decision.
|
|
158
|
+
*/
|
|
159
|
+
export function decideStart(existing, request, alive) {
|
|
160
|
+
if (existing && isRunning(existing, request.nowMs, alive))
|
|
161
|
+
return { action: 'skip', message: 'a replay is already running' };
|
|
162
|
+
const mine = existing?.identity === request.identity ? existing : undefined;
|
|
163
|
+
if (request.resume) {
|
|
164
|
+
if (!mine || mine.state === 'done')
|
|
165
|
+
return { action: 'skip', message: 'no unfinished replay to resume' };
|
|
166
|
+
return { action: 'continue', record: mine };
|
|
167
|
+
}
|
|
168
|
+
if (request.skipIfExisting && mine)
|
|
169
|
+
return { action: 'skip', message: 'setup already attempted a replay for this identity' };
|
|
170
|
+
return { action: 'fresh' };
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* The marker that says a replay is unfinished.
|
|
174
|
+
*
|
|
175
|
+
* Beside the state file, and empty: the hook asks "is anything owed?" with one
|
|
176
|
+
* `stat` of this, where reading the state file (a map of every transcript) on
|
|
177
|
+
* every hook would cost real time.
|
|
178
|
+
*
|
|
179
|
+
* @param stateFile - The replay state file.
|
|
180
|
+
* @returns The marker's path.
|
|
181
|
+
*/
|
|
182
|
+
export function pendingMarker(stateFile) {
|
|
183
|
+
return path.join(path.dirname(stateFile), REPLAY_PENDING_FILE_NAME);
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Create the marker, or remove it.
|
|
187
|
+
*
|
|
188
|
+
* @param stateFile - The replay state file.
|
|
189
|
+
* @param pending - Whether a replay is unfinished.
|
|
190
|
+
*/
|
|
191
|
+
export async function setPending(stateFile, pending) {
|
|
192
|
+
const marker = pendingMarker(stateFile);
|
|
193
|
+
if (!pending) {
|
|
194
|
+
await fs.rm(marker, { force: true });
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
await fs.mkdir(path.dirname(marker), { recursive: true });
|
|
198
|
+
await fs.writeFile(marker, '', { flag: 'a', mode: SECRET_FILE_MODE });
|
|
199
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Env } from '../core/types/core.types.js';
|
|
2
|
+
import type { AgentWatchPaths } from '../storage/types/storage.types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Pick an unfinished replay up again, from the hook that opens a session.
|
|
5
|
+
*
|
|
6
|
+
* The common case is one `stat` of a marker that does not exist. When it does,
|
|
7
|
+
* its mtime is the last time anyone looked: only after five minutes is the
|
|
8
|
+
* state read, and the marker touched first so the next sessions skip it.
|
|
9
|
+
* A replay that is running (a live heartbeat) is left alone; one that paused
|
|
10
|
+
* for its budget, died with the laptop's lid, or was never finished is started
|
|
11
|
+
* again with `--resume`. A stopped one has no marker.
|
|
12
|
+
*
|
|
13
|
+
* @param env - Ambient environment.
|
|
14
|
+
* @param paths - Where the edge keeps its state.
|
|
15
|
+
* @param token - The identity's install key or `device:<id>`.
|
|
16
|
+
* @param root - The configured root identity, or none for the machine identity.
|
|
17
|
+
*/
|
|
18
|
+
export declare function resumeReplayIfOwed(env: Env, paths: AgentWatchPaths, token: string | undefined, root?: string): Promise<void>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import { identityPaths } from '../transport/queue-partition.js';
|
|
3
|
+
import { REPLAY_RESUME_INTERVAL_MS } from './constants/replay.constants.js';
|
|
4
|
+
import { isRunning, pendingMarker, readReplayRecord, replayStateFile } from './replay-state.js';
|
|
5
|
+
import { startReplayDetached } from './start.js';
|
|
6
|
+
/**
|
|
7
|
+
* Pick an unfinished replay up again, from the hook that opens a session.
|
|
8
|
+
*
|
|
9
|
+
* The common case is one `stat` of a marker that does not exist. When it does,
|
|
10
|
+
* its mtime is the last time anyone looked: only after five minutes is the
|
|
11
|
+
* state read, and the marker touched first so the next sessions skip it.
|
|
12
|
+
* A replay that is running (a live heartbeat) is left alone; one that paused
|
|
13
|
+
* for its budget, died with the laptop's lid, or was never finished is started
|
|
14
|
+
* again with `--resume`. A stopped one has no marker.
|
|
15
|
+
*
|
|
16
|
+
* @param env - Ambient environment.
|
|
17
|
+
* @param paths - Where the edge keeps its state.
|
|
18
|
+
* @param token - The identity's install key or `device:<id>`.
|
|
19
|
+
* @param root - The configured root identity, or none for the machine identity.
|
|
20
|
+
*/
|
|
21
|
+
export async function resumeReplayIfOwed(env, paths, token, root) {
|
|
22
|
+
const stateFile = replayStateFile(identityPaths(paths, token).statsFile);
|
|
23
|
+
const marker = pendingMarker(stateFile);
|
|
24
|
+
const stat = await fs.stat(marker).catch(() => undefined);
|
|
25
|
+
const now = env.now();
|
|
26
|
+
if (!stat || now.getTime() - stat.mtimeMs < REPLAY_RESUME_INTERVAL_MS)
|
|
27
|
+
return;
|
|
28
|
+
await fs.utimes(marker, now, now);
|
|
29
|
+
const record = await readReplayRecord(stateFile);
|
|
30
|
+
if (!record || record.state === 'done' || record.state === 'stopped' || isRunning(record, now.getTime()))
|
|
31
|
+
return;
|
|
32
|
+
const selector = root === undefined ? ['--machine'] : ['--root', root];
|
|
33
|
+
startReplayDetached(env, ['replay', ...selector, '--resume'], root ?? env.home);
|
|
34
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Env } from '../core/types/core.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Start `agentwatch replay …` as a process of its own, detached from the caller.
|
|
4
|
+
*
|
|
5
|
+
* Setup must not wait for a replay and a hook must not carry one, so the replay
|
|
6
|
+
* outlives both. Only from the built CLI: run from source there is nothing to
|
|
7
|
+
* start. A spawn that fails emits 'error'; unheard, it would crash the caller.
|
|
8
|
+
*
|
|
9
|
+
* @param env - Its `startDetached` when injected (tests); otherwise the built CLI is spawned.
|
|
10
|
+
* @param args - The arguments after `agentwatch`.
|
|
11
|
+
* @param cwd - Where to run: the replay picks its identity from it, as a hook does.
|
|
12
|
+
*/
|
|
13
|
+
export declare function startReplayDetached(env: Env, args: readonly string[], cwd: string): void;
|
|
14
|
+
/**
|
|
15
|
+
* Run this `agentwatch replay` again under {@link REPLAY_NODE_FLAGS}, when it was not started with them.
|
|
16
|
+
*
|
|
17
|
+
* The flags cannot be set from inside a running Node, and a replay started by
|
|
18
|
+
* hand should weigh what one started by setup does.
|
|
19
|
+
*
|
|
20
|
+
* @param argv - The arguments after `agentwatch`.
|
|
21
|
+
* @returns The child's exit code, or undefined when this process already has the flags (or there is no built CLI to run).
|
|
22
|
+
*/
|
|
23
|
+
export declare function runWithSmallHeap(argv: readonly string[]): number | undefined;
|