@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,99 @@
|
|
|
1
|
+
/** Days of history a replay reaches back when `--replay-days` names none. */
|
|
2
|
+
export declare const REPLAY_DEFAULT_DAYS = 30;
|
|
3
|
+
/** The most `--replay-days` accepts; a transcript older than the open charge period is never sent anyway. */
|
|
4
|
+
export declare const REPLAY_MAX_DAYS = 366;
|
|
5
|
+
/**
|
|
6
|
+
* Days into a month before the previous month's charges settle. A copy of core's
|
|
7
|
+
* `PLACEMENT_FINALIZATION_GRACE_DAYS` (packages/types/src/period-month.ts): the edge
|
|
8
|
+
* cannot import core, and the two must move together.
|
|
9
|
+
*/
|
|
10
|
+
export declare const REPLAY_FINALIZATION_GRACE_DAYS = 5;
|
|
11
|
+
/** Turns in one request. Small on purpose: the gateway budgets a batch at 8 s and the machine is someone's laptop. */
|
|
12
|
+
export declare const REPLAY_BATCH_TURNS = 20;
|
|
13
|
+
/** Between two requests, so a 1,000 turn history costs the backend under one request per five seconds. */
|
|
14
|
+
export declare const REPLAY_BATCH_INTERVAL_MS = 5000;
|
|
15
|
+
/** Between two transcript files, so the disk reads come up for air. */
|
|
16
|
+
export declare const REPLAY_FILE_PAUSE_MS = 200;
|
|
17
|
+
/** How long a busy machine (load, battery) is left alone before the next look. */
|
|
18
|
+
export declare const REPLAY_BUSY_PAUSE_MS = 30000;
|
|
19
|
+
/** The first wait after a failed request; it doubles up to {@link REPLAY_MAX_BACKOFF_MS}. */
|
|
20
|
+
export declare const REPLAY_BASE_BACKOFF_MS = 10000;
|
|
21
|
+
/** The longest wait after repeated failures. */
|
|
22
|
+
export declare const REPLAY_MAX_BACKOFF_MS = 300000;
|
|
23
|
+
/** Active time one run spends before it stops and waits to be resumed. */
|
|
24
|
+
export declare const REPLAY_RUN_BUDGET_MS: number;
|
|
25
|
+
/** A hook closed a turn this recently: an agent is working and replay waits. */
|
|
26
|
+
export declare const REPLAY_HOOK_QUIET_MS = 60000;
|
|
27
|
+
/** A progress record goes out every this many batches, besides its start, pauses and end. */
|
|
28
|
+
export declare const REPLAY_PROGRESS_EVERY_BATCHES = 5;
|
|
29
|
+
/**
|
|
30
|
+
* A turn that ended this recently may still be running; the live hook owns it.
|
|
31
|
+
* Replaying half a turn would hand the backend a smaller one than the hook's.
|
|
32
|
+
*/
|
|
33
|
+
export declare const REPLAY_LIVE_TURN_GRACE_MS: number;
|
|
34
|
+
/** A run whose state file has not been touched for this long is gone, whatever its pid says. */
|
|
35
|
+
export declare const REPLAY_HEARTBEAT_STALE_MS: number;
|
|
36
|
+
/** How often the battery is asked; `pmset` is a process start. */
|
|
37
|
+
export declare const REPLAY_BATTERY_CHECK_MS: number;
|
|
38
|
+
/** `nice` 19: the lowest priority the operating system offers. */
|
|
39
|
+
export declare const REPLAY_NICE = 19;
|
|
40
|
+
/** The 1-minute load average, per core, above which the machine counts as busy. */
|
|
41
|
+
export declare const REPLAY_BUSY_LOAD_PER_CORE = 0.5;
|
|
42
|
+
/** Where Claude Code keeps its transcripts, under HOME. */
|
|
43
|
+
export declare const CLAUDE_PROJECTS_SEGMENTS: readonly [".claude", "projects"];
|
|
44
|
+
/** The agents replay reads, by provider id: setup starts a replay when any of them is detected. */
|
|
45
|
+
export declare const REPLAY_AGENTS: ReadonlySet<string>;
|
|
46
|
+
/** Where Codex keeps its rollouts, under `CODEX_HOME`: `sessions/YYYY/MM/DD/rollout-*.jsonl`. */
|
|
47
|
+
export declare const CODEX_SESSIONS_DIR = "sessions";
|
|
48
|
+
export declare const CODEX_ROLLOUT_PREFIX = "rollout-";
|
|
49
|
+
/** The rollout rows replay reads; every other row (messages, reasoning, tool output) is skipped unparsed. */
|
|
50
|
+
export declare const RE_CODEX_REPLAY_ROW: RegExp;
|
|
51
|
+
/** A prompt row, read only when prompt text is allowed. */
|
|
52
|
+
export declare const RE_CODEX_PROMPT_ROW: RegExp;
|
|
53
|
+
/** The row that opens a Codex turn, for the cheap count. */
|
|
54
|
+
export declare const RE_CODEX_TURN_START: RegExp;
|
|
55
|
+
/** Gemini CLI's chat files, under its home: `tmp/<project>/chats/session-*.jsonl`, and the map of project folders. */
|
|
56
|
+
export declare const GEMINI_TMP_DIR = "tmp";
|
|
57
|
+
export declare const GEMINI_CHATS_DIR = "chats";
|
|
58
|
+
export declare const GEMINI_SESSION_PREFIX = "session-";
|
|
59
|
+
export declare const GEMINI_PROJECTS_FILE = "projects.json";
|
|
60
|
+
/** A user message, for the cheap count. */
|
|
61
|
+
export declare const RE_GEMINI_TURN_START: RegExp;
|
|
62
|
+
/** Directory under the identity state dir holding replay state. */
|
|
63
|
+
export declare const REPLAY_STATE_FILE_NAME = "replay.json";
|
|
64
|
+
/** Machine-wide claim that setup has already started or inherited an automatic replay attempt. */
|
|
65
|
+
export declare const REPLAY_ATTEMPTED_FILE_NAME = "replay-attempted";
|
|
66
|
+
/** The provider label replay progress reports; each turn carries its own agent's. */
|
|
67
|
+
export declare const REPLAY_PROVIDER = "claude";
|
|
68
|
+
/** What a real prompt never starts with: Claude Code's own slash-command and interrupt records. */
|
|
69
|
+
export declare const RE_NOT_A_PROMPT: RegExp;
|
|
70
|
+
/** Claude Code names a detached HEAD "HEAD" in `gitBranch`; the hook path reports no branch for it. */
|
|
71
|
+
export declare const DETACHED_BRANCH = "HEAD";
|
|
72
|
+
/** The fields a transcript line is cheaply recognised by before it is parsed. */
|
|
73
|
+
export declare const RE_USER_OR_ASSISTANT_LINE: RegExp;
|
|
74
|
+
/** Prompt id fallback namespace, so a hash never reads as a Claude id. */
|
|
75
|
+
export declare const REPLAY_FALLBACK_TURN_PREFIX = "replay-";
|
|
76
|
+
/** One send's network budget: a batch of twenty turns, to a gateway that budgets eight seconds. */
|
|
77
|
+
export declare const REPLAY_SEND_TIMEOUT_MS = 15000;
|
|
78
|
+
/** A repository's snapshot and tree flows share this; the hooks' own is one second. */
|
|
79
|
+
export declare const REPLAY_SNAPSHOT_BUDGET_MS = 5000;
|
|
80
|
+
/** Failed requests in a row (10 s doubling to 5 min is about ten minutes) before a run gives up until it is resumed. */
|
|
81
|
+
export declare const REPLAY_GIVE_UP_FAILURES = 6;
|
|
82
|
+
/** The sibling file that exists only while a replay is unfinished: the hook's one `stat` to know whether to resume it. */
|
|
83
|
+
export declare const REPLAY_PENDING_FILE_NAME = "replay-pending";
|
|
84
|
+
/** The provider label progress and snapshot records carry; Claude Code's public name. */
|
|
85
|
+
export declare const REPLAY_PROVIDER_LABEL = "claude-code";
|
|
86
|
+
/** A hook starts a stalled replay again at most this often. */
|
|
87
|
+
export declare const REPLAY_RESUME_INTERVAL_MS: number;
|
|
88
|
+
/** A nap is cut into pieces this long, each followed by a heartbeat, so a long wait is not mistaken for a dead run. */
|
|
89
|
+
export declare const REPLAY_HEARTBEAT_MS = 30000;
|
|
90
|
+
/**
|
|
91
|
+
* Node flags a replay runs under. A replay reads megabytes of JSON per second of
|
|
92
|
+
* wall time and keeps almost none of it, so left alone V8 lets the young
|
|
93
|
+
* generation and the heap grow into hundreds of megabytes of garbage (measured:
|
|
94
|
+
* 460 MB peak over 5,400 transcripts, against about 100 MB with these). They
|
|
95
|
+
* cost nothing in speed.
|
|
96
|
+
*/
|
|
97
|
+
export declare const REPLAY_NODE_FLAGS: readonly ["--max-semi-space-size=1", "--max-old-space-size=256"];
|
|
98
|
+
/** Files the identity pre-pass reads between two heartbeats (and looks for a `--stop`). */
|
|
99
|
+
export declare const PREPASS_PERSIST_EVERY = 25;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { CLAUDE_PROVIDER_ID } from '../../providers/claude/constants/claude.constants.js';
|
|
2
|
+
import { CODEX_PROVIDER_ID } from '../../providers/codex/constants/codex.constants.js';
|
|
3
|
+
import { GEMINI_PROVIDER_ID } from '../../providers/gemini/constants/gemini.constants.js';
|
|
4
|
+
/** Days of history a replay reaches back when `--replay-days` names none. */
|
|
5
|
+
export const REPLAY_DEFAULT_DAYS = 30;
|
|
6
|
+
/** The most `--replay-days` accepts; a transcript older than the open charge period is never sent anyway. */
|
|
7
|
+
export const REPLAY_MAX_DAYS = 366;
|
|
8
|
+
/**
|
|
9
|
+
* Days into a month before the previous month's charges settle. A copy of core's
|
|
10
|
+
* `PLACEMENT_FINALIZATION_GRACE_DAYS` (packages/types/src/period-month.ts): the edge
|
|
11
|
+
* cannot import core, and the two must move together.
|
|
12
|
+
*/
|
|
13
|
+
export const REPLAY_FINALIZATION_GRACE_DAYS = 5;
|
|
14
|
+
/** Turns in one request. Small on purpose: the gateway budgets a batch at 8 s and the machine is someone's laptop. */
|
|
15
|
+
export const REPLAY_BATCH_TURNS = 20;
|
|
16
|
+
/** Between two requests, so a 1,000 turn history costs the backend under one request per five seconds. */
|
|
17
|
+
export const REPLAY_BATCH_INTERVAL_MS = 5_000;
|
|
18
|
+
/** Between two transcript files, so the disk reads come up for air. */
|
|
19
|
+
export const REPLAY_FILE_PAUSE_MS = 200;
|
|
20
|
+
/** How long a busy machine (load, battery) is left alone before the next look. */
|
|
21
|
+
export const REPLAY_BUSY_PAUSE_MS = 30_000;
|
|
22
|
+
/** The first wait after a failed request; it doubles up to {@link REPLAY_MAX_BACKOFF_MS}. */
|
|
23
|
+
export const REPLAY_BASE_BACKOFF_MS = 10_000;
|
|
24
|
+
/** The longest wait after repeated failures. */
|
|
25
|
+
export const REPLAY_MAX_BACKOFF_MS = 300_000;
|
|
26
|
+
/** Active time one run spends before it stops and waits to be resumed. */
|
|
27
|
+
export const REPLAY_RUN_BUDGET_MS = 20 * 60_000;
|
|
28
|
+
/** A hook closed a turn this recently: an agent is working and replay waits. */
|
|
29
|
+
export const REPLAY_HOOK_QUIET_MS = 60_000;
|
|
30
|
+
/** A progress record goes out every this many batches, besides its start, pauses and end. */
|
|
31
|
+
export const REPLAY_PROGRESS_EVERY_BATCHES = 5;
|
|
32
|
+
/**
|
|
33
|
+
* A turn that ended this recently may still be running; the live hook owns it.
|
|
34
|
+
* Replaying half a turn would hand the backend a smaller one than the hook's.
|
|
35
|
+
*/
|
|
36
|
+
export const REPLAY_LIVE_TURN_GRACE_MS = 10 * 60_000;
|
|
37
|
+
/** A run whose state file has not been touched for this long is gone, whatever its pid says. */
|
|
38
|
+
export const REPLAY_HEARTBEAT_STALE_MS = 2 * 60_000;
|
|
39
|
+
/** How often the battery is asked; `pmset` is a process start. */
|
|
40
|
+
export const REPLAY_BATTERY_CHECK_MS = 5 * 60_000;
|
|
41
|
+
/** `nice` 19: the lowest priority the operating system offers. */
|
|
42
|
+
export const REPLAY_NICE = 19;
|
|
43
|
+
/** The 1-minute load average, per core, above which the machine counts as busy. */
|
|
44
|
+
export const REPLAY_BUSY_LOAD_PER_CORE = 0.5;
|
|
45
|
+
/** Where Claude Code keeps its transcripts, under HOME. */
|
|
46
|
+
export const CLAUDE_PROJECTS_SEGMENTS = ['.claude', 'projects'];
|
|
47
|
+
/** The agents replay reads, by provider id: setup starts a replay when any of them is detected. */
|
|
48
|
+
export const REPLAY_AGENTS = new Set([CLAUDE_PROVIDER_ID, CODEX_PROVIDER_ID, GEMINI_PROVIDER_ID]);
|
|
49
|
+
/** Where Codex keeps its rollouts, under `CODEX_HOME`: `sessions/YYYY/MM/DD/rollout-*.jsonl`. */
|
|
50
|
+
export const CODEX_SESSIONS_DIR = 'sessions';
|
|
51
|
+
export const CODEX_ROLLOUT_PREFIX = 'rollout-';
|
|
52
|
+
/** The rollout rows replay reads; every other row (messages, reasoning, tool output) is skipped unparsed. */
|
|
53
|
+
export const RE_CODEX_REPLAY_ROW = /"type":"(?:session_meta|turn_context|task_started|task_complete|token_count)"/;
|
|
54
|
+
/** A prompt row, read only when prompt text is allowed. */
|
|
55
|
+
export const RE_CODEX_PROMPT_ROW = /"type":"user_message"/;
|
|
56
|
+
/** The row that opens a Codex turn, for the cheap count. */
|
|
57
|
+
export const RE_CODEX_TURN_START = /"type":"task_started"/;
|
|
58
|
+
/** Gemini CLI's chat files, under its home: `tmp/<project>/chats/session-*.jsonl`, and the map of project folders. */
|
|
59
|
+
export const GEMINI_TMP_DIR = 'tmp';
|
|
60
|
+
export const GEMINI_CHATS_DIR = 'chats';
|
|
61
|
+
export const GEMINI_SESSION_PREFIX = 'session-';
|
|
62
|
+
export const GEMINI_PROJECTS_FILE = 'projects.json';
|
|
63
|
+
/** A user message, for the cheap count. */
|
|
64
|
+
export const RE_GEMINI_TURN_START = /"type":"user"/;
|
|
65
|
+
/** Directory under the identity state dir holding replay state. */
|
|
66
|
+
export const REPLAY_STATE_FILE_NAME = 'replay.json';
|
|
67
|
+
/** Machine-wide claim that setup has already started or inherited an automatic replay attempt. */
|
|
68
|
+
export const REPLAY_ATTEMPTED_FILE_NAME = 'replay-attempted';
|
|
69
|
+
/** The provider label replay progress reports; each turn carries its own agent's. */
|
|
70
|
+
export const REPLAY_PROVIDER = 'claude';
|
|
71
|
+
/** What a real prompt never starts with: Claude Code's own slash-command and interrupt records. */
|
|
72
|
+
export const RE_NOT_A_PROMPT = /^(?:<command-name>|<command-message>|<local-command-|\[Request interrupted)/;
|
|
73
|
+
/** Claude Code names a detached HEAD "HEAD" in `gitBranch`; the hook path reports no branch for it. */
|
|
74
|
+
export const DETACHED_BRANCH = 'HEAD';
|
|
75
|
+
/** The fields a transcript line is cheaply recognised by before it is parsed. */
|
|
76
|
+
export const RE_USER_OR_ASSISTANT_LINE = /"type":"(?:user|assistant)"/;
|
|
77
|
+
/** Prompt id fallback namespace, so a hash never reads as a Claude id. */
|
|
78
|
+
export const REPLAY_FALLBACK_TURN_PREFIX = 'replay-';
|
|
79
|
+
/** One send's network budget: a batch of twenty turns, to a gateway that budgets eight seconds. */
|
|
80
|
+
export const REPLAY_SEND_TIMEOUT_MS = 15_000;
|
|
81
|
+
/** A repository's snapshot and tree flows share this; the hooks' own is one second. */
|
|
82
|
+
export const REPLAY_SNAPSHOT_BUDGET_MS = 5_000;
|
|
83
|
+
/** Failed requests in a row (10 s doubling to 5 min is about ten minutes) before a run gives up until it is resumed. */
|
|
84
|
+
export const REPLAY_GIVE_UP_FAILURES = 6;
|
|
85
|
+
/** The sibling file that exists only while a replay is unfinished: the hook's one `stat` to know whether to resume it. */
|
|
86
|
+
export const REPLAY_PENDING_FILE_NAME = 'replay-pending';
|
|
87
|
+
/** The provider label progress and snapshot records carry; Claude Code's public name. */
|
|
88
|
+
export const REPLAY_PROVIDER_LABEL = 'claude-code';
|
|
89
|
+
/** A hook starts a stalled replay again at most this often. */
|
|
90
|
+
export const REPLAY_RESUME_INTERVAL_MS = 5 * 60_000;
|
|
91
|
+
/** A nap is cut into pieces this long, each followed by a heartbeat, so a long wait is not mistaken for a dead run. */
|
|
92
|
+
export const REPLAY_HEARTBEAT_MS = 30_000;
|
|
93
|
+
/**
|
|
94
|
+
* Node flags a replay runs under. A replay reads megabytes of JSON per second of
|
|
95
|
+
* wall time and keeps almost none of it, so left alone V8 lets the young
|
|
96
|
+
* generation and the heap grow into hundreds of megabytes of garbage (measured:
|
|
97
|
+
* 460 MB peak over 5,400 transcripts, against about 100 MB with these). They
|
|
98
|
+
* cost nothing in speed.
|
|
99
|
+
*/
|
|
100
|
+
export const REPLAY_NODE_FLAGS = ['--max-semi-space-size=1', '--max-old-space-size=256'];
|
|
101
|
+
/** Files the identity pre-pass reads between two heartbeats (and looks for a `--stop`). */
|
|
102
|
+
export const PREPASS_PERSIST_EVERY = 25;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ReplayRecord } from './types/replay.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The one line `agentwatch status` prints about a replay.
|
|
4
|
+
*
|
|
5
|
+
* @param record - The persisted state.
|
|
6
|
+
* @param nowMs - The clock.
|
|
7
|
+
* @param alive - Whether a pid exists; injectable for tests.
|
|
8
|
+
* @returns The line, e.g. "running 412/1,030 turns (40%)".
|
|
9
|
+
*/
|
|
10
|
+
export declare function describeReplay(record: ReplayRecord, nowMs: number, alive?: (pid: number) => boolean): string;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { isRunning } from './replay-state.js';
|
|
2
|
+
const PAUSE_REASONS = {
|
|
3
|
+
busy: 'laptop busy',
|
|
4
|
+
battery: 'on battery',
|
|
5
|
+
'agent-active': 'an agent is working',
|
|
6
|
+
backend: 'waiting for the backend',
|
|
7
|
+
budget: 'resumes with the next agent session'
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* The one line `agentwatch status` prints about a replay.
|
|
11
|
+
*
|
|
12
|
+
* @param record - The persisted state.
|
|
13
|
+
* @param nowMs - The clock.
|
|
14
|
+
* @param alive - Whether a pid exists; injectable for tests.
|
|
15
|
+
* @returns The line, e.g. "running 412/1,030 turns (40%)".
|
|
16
|
+
*/
|
|
17
|
+
export function describeReplay(record, nowMs, alive) {
|
|
18
|
+
const counts = `${record.turnsSent.toLocaleString('en-US')}/${record.turnsTotal.toLocaleString('en-US')} turns`;
|
|
19
|
+
const percent = record.turnsTotal > 0 ? ` (${Math.min(100, Math.floor((record.turnsSent / record.turnsTotal) * 100))}%)` : '';
|
|
20
|
+
if (record.state === 'done')
|
|
21
|
+
return `done: ${record.turnsSent.toLocaleString('en-US')} turns from ${record.sessionsSent.toLocaleString('en-US')} sessions`;
|
|
22
|
+
if (record.state === 'stopped')
|
|
23
|
+
return `stopped at ${counts}`;
|
|
24
|
+
if (!isRunning(record, nowMs, alive))
|
|
25
|
+
return `interrupted at ${counts}${percent}; continues with the next agent session`;
|
|
26
|
+
if (record.state === 'scanning')
|
|
27
|
+
return 'counting earlier sessions';
|
|
28
|
+
if (record.state === 'paused')
|
|
29
|
+
return `paused: ${PAUSE_REASONS[record.pause ?? 'busy']} (${counts}${percent})`;
|
|
30
|
+
return `running ${counts}${percent}`;
|
|
31
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ReplaySource, ReplayTurn } from './types/replay.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Gemini CLI's chat files as a replay source.
|
|
4
|
+
*
|
|
5
|
+
* Gemini's chat files do not record the id its live hook sends, so a replayed
|
|
6
|
+
* turn could never meet the live one downstream. Only turns that began before
|
|
7
|
+
* Gemini hooks were first installed on this machine are replayed: the live
|
|
8
|
+
* hooks sent every one after. An install with no first-install time hooked
|
|
9
|
+
* Gemini before that time was recorded, so the hooks already sent its history
|
|
10
|
+
* and nothing is replayed.
|
|
11
|
+
*
|
|
12
|
+
* @param home - Gemini CLI's home (`GEMINI_HOME`, or `~/.gemini`).
|
|
13
|
+
* @param firstInstalledMs - When AgentWatch first installed Gemini hooks here; undefined when unknown.
|
|
14
|
+
* @returns The source.
|
|
15
|
+
*/
|
|
16
|
+
export declare function geminiSource(home: string, firstInstalledMs: number | undefined): ReplaySource;
|
|
17
|
+
/**
|
|
18
|
+
* Read a Gemini CLI chat file as the turns its live hook closed.
|
|
19
|
+
*
|
|
20
|
+
* A turn opens at a user message and runs to the next one; its usage is the
|
|
21
|
+
* replies between them that carry `tokens`, each reply once (Gemini rewrites a
|
|
22
|
+
* reply under the same id when its tokens arrive, and a `$set` snapshot
|
|
23
|
+
* repeats earlier messages). A turn whose replies carry no tokens (a file
|
|
24
|
+
* written before gemini-cli 0.46) is not yielded. Reply and thought content is
|
|
25
|
+
* never kept; a prompt's text only when prompt text is allowed.
|
|
26
|
+
*
|
|
27
|
+
* ponytail: the file's messages are kept in memory as metadata (id, time, tokens)
|
|
28
|
+
* before turns are cut, because a `$set` snapshot can repeat any earlier message.
|
|
29
|
+
* A chat file is one session, so that is thousands of small records at most.
|
|
30
|
+
*
|
|
31
|
+
* @param file - A `session-*.jsonl` file.
|
|
32
|
+
* @param promptText - Whether to keep the prompt's text.
|
|
33
|
+
* @param cwd - The directory the file's project folder stands for, if known.
|
|
34
|
+
* @yields Turns, oldest first.
|
|
35
|
+
*/
|
|
36
|
+
export declare function readGeminiTurns(file: string, promptText: boolean, cwd: string | undefined): AsyncGenerator<ReplayTurn>;
|
|
@@ -0,0 +1,192 @@
|
|
|
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 { GEMINI_PROVIDER_ID } from '../providers/gemini/constants/gemini.constants.js';
|
|
8
|
+
import { GEMINI_TOKENS } from '../turns/constants/turns.constants.js';
|
|
9
|
+
import { geminiReplyUsage, replyRows } from '../turns/gemini-transcript.js';
|
|
10
|
+
import { accumulateUsage, parseJsonLine } from '../turns/transcript.js';
|
|
11
|
+
import { GEMINI_CHATS_DIR, GEMINI_PROJECTS_FILE, GEMINI_SESSION_PREFIX, GEMINI_TMP_DIR, RE_GEMINI_TURN_START, REPLAY_FALLBACK_TURN_PREFIX } from './constants/replay.constants.js';
|
|
12
|
+
import { estimateTurns, recentFiles } from './replay-scan.js';
|
|
13
|
+
/** The lines worth parsing: the session's metadata row, user messages, replies with tokens, and `$set` snapshots holding them. */
|
|
14
|
+
const RE_GEMINI_REPLAY_ROW = new RegExp(`"sessionId"|"type":"user"|"${GEMINI_TOKENS}"`);
|
|
15
|
+
/**
|
|
16
|
+
* Gemini CLI's chat files as a replay source.
|
|
17
|
+
*
|
|
18
|
+
* Gemini's chat files do not record the id its live hook sends, so a replayed
|
|
19
|
+
* turn could never meet the live one downstream. Only turns that began before
|
|
20
|
+
* Gemini hooks were first installed on this machine are replayed: the live
|
|
21
|
+
* hooks sent every one after. An install with no first-install time hooked
|
|
22
|
+
* Gemini before that time was recorded, so the hooks already sent its history
|
|
23
|
+
* and nothing is replayed.
|
|
24
|
+
*
|
|
25
|
+
* @param home - Gemini CLI's home (`GEMINI_HOME`, or `~/.gemini`).
|
|
26
|
+
* @param firstInstalledMs - When AgentWatch first installed Gemini hooks here; undefined when unknown.
|
|
27
|
+
* @returns The source.
|
|
28
|
+
*/
|
|
29
|
+
export function geminiSource(home, firstInstalledMs) {
|
|
30
|
+
let projects;
|
|
31
|
+
return {
|
|
32
|
+
agent: GEMINI_PROVIDER_ID,
|
|
33
|
+
list: async (sinceMs) => (firstInstalledMs === undefined || firstInstalledMs <= sinceMs ? [] : listChats(home, sinceMs)),
|
|
34
|
+
estimate: (file, sinceMs) => estimateTurns(file, sinceMs, RE_GEMINI_TURN_START),
|
|
35
|
+
read: async function* (file, promptText) {
|
|
36
|
+
projects ??= projectFolders(home);
|
|
37
|
+
const cwd = (await projects).get(path.basename(path.dirname(path.dirname(file))));
|
|
38
|
+
// No folder: no way to tell whether it ran under `ignoredRoots` or another root's identity.
|
|
39
|
+
if (!cwd)
|
|
40
|
+
return;
|
|
41
|
+
for await (const turn of readGeminiTurns(file, promptText, cwd))
|
|
42
|
+
if (firstInstalledMs !== undefined && Date.parse(turn.startedAt) < firstInstalledMs)
|
|
43
|
+
yield turn;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The chat files written inside the window, from `tmp/<project>/chats`.
|
|
49
|
+
*
|
|
50
|
+
* @param home - Gemini CLI's home.
|
|
51
|
+
* @param sinceMs - The window's start.
|
|
52
|
+
* @returns The files; empty when Gemini CLI has left none.
|
|
53
|
+
*/
|
|
54
|
+
async function listChats(home, sinceMs) {
|
|
55
|
+
const tmp = path.join(home, GEMINI_TMP_DIR);
|
|
56
|
+
const projects = await fsp.readdir(tmp, { withFileTypes: true }).catch(() => []);
|
|
57
|
+
const found = [];
|
|
58
|
+
for (const project of projects) {
|
|
59
|
+
if (project.isDirectory())
|
|
60
|
+
found.push(...(await recentFiles(path.join(tmp, project.name, GEMINI_CHATS_DIR), sinceMs, GEMINI_SESSION_PREFIX)));
|
|
61
|
+
}
|
|
62
|
+
return found;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Which folder each project folder under `tmp/` stands for: `projects.json` (`{projects: {<dir>: <folder>}}`), inverted.
|
|
66
|
+
*
|
|
67
|
+
* @param home - Gemini CLI's home.
|
|
68
|
+
* @returns Project folder name to absolute directory; empty when the file is missing or unreadable.
|
|
69
|
+
*/
|
|
70
|
+
async function projectFolders(home) {
|
|
71
|
+
const raw = await fsp.readFile(path.join(home, GEMINI_PROJECTS_FILE), 'utf8').catch(() => '');
|
|
72
|
+
const projects = asRecord(parseJsonLine(raw)?.['projects']) ?? {};
|
|
73
|
+
const folders = new Map();
|
|
74
|
+
for (const [dir, folder] of Object.entries(projects))
|
|
75
|
+
if (typeof folder === 'string' && path.isAbsolute(dir))
|
|
76
|
+
folders.set(folder, dir);
|
|
77
|
+
return folders;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Read a Gemini CLI chat file as the turns its live hook closed.
|
|
81
|
+
*
|
|
82
|
+
* A turn opens at a user message and runs to the next one; its usage is the
|
|
83
|
+
* replies between them that carry `tokens`, each reply once (Gemini rewrites a
|
|
84
|
+
* reply under the same id when its tokens arrive, and a `$set` snapshot
|
|
85
|
+
* repeats earlier messages). A turn whose replies carry no tokens (a file
|
|
86
|
+
* written before gemini-cli 0.46) is not yielded. Reply and thought content is
|
|
87
|
+
* never kept; a prompt's text only when prompt text is allowed.
|
|
88
|
+
*
|
|
89
|
+
* ponytail: the file's messages are kept in memory as metadata (id, time, tokens)
|
|
90
|
+
* before turns are cut, because a `$set` snapshot can repeat any earlier message.
|
|
91
|
+
* A chat file is one session, so that is thousands of small records at most.
|
|
92
|
+
*
|
|
93
|
+
* @param file - A `session-*.jsonl` file.
|
|
94
|
+
* @param promptText - Whether to keep the prompt's text.
|
|
95
|
+
* @param cwd - The directory the file's project folder stands for, if known.
|
|
96
|
+
* @yields Turns, oldest first.
|
|
97
|
+
*/
|
|
98
|
+
export async function* readGeminiTurns(file, promptText, cwd) {
|
|
99
|
+
const lines = readline.createInterface({ input: fs.createReadStream(file, { encoding: 'utf8' }), crlfDelay: Infinity });
|
|
100
|
+
const messages = new Map();
|
|
101
|
+
let sessionId;
|
|
102
|
+
try {
|
|
103
|
+
for await (const line of lines) {
|
|
104
|
+
if (!RE_GEMINI_REPLAY_ROW.test(line))
|
|
105
|
+
continue;
|
|
106
|
+
const row = parseJsonLine(line);
|
|
107
|
+
sessionId ??= firstStringOf(row?.['sessionId']);
|
|
108
|
+
for (const record of replyRows(row))
|
|
109
|
+
remember(messages, record, promptText);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
finally {
|
|
113
|
+
lines.close();
|
|
114
|
+
}
|
|
115
|
+
yield* cutTurns(messages, sessionId ?? path.basename(file), cwd);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Keep what replay needs of one message record; a later record with the same id replaces it.
|
|
119
|
+
*
|
|
120
|
+
* @param messages - The file's messages so far, by id.
|
|
121
|
+
* @param record - One message record.
|
|
122
|
+
* @param promptText - Whether to keep a prompt's text.
|
|
123
|
+
*/
|
|
124
|
+
function remember(messages, record, promptText) {
|
|
125
|
+
const id = firstStringOf(record['id']);
|
|
126
|
+
if (!id)
|
|
127
|
+
return;
|
|
128
|
+
const at = firstStringOf(record['timestamp']);
|
|
129
|
+
if (record['type'] === 'user') {
|
|
130
|
+
messages.set(id, { user: true, at, text: promptText ? textOf(record['content']) : undefined });
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const reply = geminiReplyUsage(record);
|
|
134
|
+
// A reply without tokens yet keeps its place; its rewrite with tokens fills it in.
|
|
135
|
+
if (reply || !messages.has(id))
|
|
136
|
+
messages.set(id, { user: false, at, reply });
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* The turns a session's messages make: one per user message, with the replies up to the next.
|
|
140
|
+
*
|
|
141
|
+
* @param messages - The messages by id, in the order the file first wrote them.
|
|
142
|
+
* @param sessionId - The session.
|
|
143
|
+
* @param cwd - Where it ran, if known.
|
|
144
|
+
* @yields Turns with usage.
|
|
145
|
+
*/
|
|
146
|
+
function* cutTurns(messages, sessionId, cwd) {
|
|
147
|
+
let open;
|
|
148
|
+
const close = () => {
|
|
149
|
+
const usage = open && accumulateUsage(open.usage);
|
|
150
|
+
if (!open?.prompt.at || !usage)
|
|
151
|
+
return undefined;
|
|
152
|
+
return {
|
|
153
|
+
provider: GEMINI_PROVIDER_ID,
|
|
154
|
+
sessionId,
|
|
155
|
+
turnId: REPLAY_FALLBACK_TURN_PREFIX + sha256Hex(`${sessionId}${open.id}`).slice(0, 32),
|
|
156
|
+
startedAt: open.prompt.at,
|
|
157
|
+
endedAt: open.endedAt,
|
|
158
|
+
cwd,
|
|
159
|
+
usage,
|
|
160
|
+
tools: [],
|
|
161
|
+
promptText: open.prompt.text?.trim() || undefined
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
for (const [id, message] of messages) {
|
|
165
|
+
if (message.user) {
|
|
166
|
+
const closed = close();
|
|
167
|
+
if (closed)
|
|
168
|
+
yield closed;
|
|
169
|
+
open = { id, prompt: message, endedAt: message.at ?? '', usage: new Map() };
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
if (!open || !message.reply)
|
|
173
|
+
continue;
|
|
174
|
+
open = { ...open, usage: new Map(open.usage).set(id, message.reply), endedAt: message.at && message.at > open.endedAt ? message.at : open.endedAt };
|
|
175
|
+
}
|
|
176
|
+
const last = close();
|
|
177
|
+
if (last)
|
|
178
|
+
yield last;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* The text of a user message: a string, or the `text` of its parts.
|
|
182
|
+
*
|
|
183
|
+
* @param content - The message's `content`.
|
|
184
|
+
* @returns The text, or undefined.
|
|
185
|
+
*/
|
|
186
|
+
function textOf(content) {
|
|
187
|
+
if (typeof content === 'string')
|
|
188
|
+
return content;
|
|
189
|
+
if (!Array.isArray(content))
|
|
190
|
+
return undefined;
|
|
191
|
+
return content.map((part) => firstStringOf(asRecord(part)?.['text']) ?? '').join('\n');
|
|
192
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { AgentWatchPaths } from '../storage/types/storage.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Whether the machine is running on its battery.
|
|
4
|
+
*
|
|
5
|
+
* Fails open to "no": a laptop that cannot say is treated as plugged in, since
|
|
6
|
+
* the other reading would silence replay on every machine without `pmset`.
|
|
7
|
+
*
|
|
8
|
+
* @param platform - The OS.
|
|
9
|
+
* @returns True only when the OS says the battery is discharging.
|
|
10
|
+
*/
|
|
11
|
+
export declare function readOnBattery(platform: NodeJS.Platform): Promise<boolean>;
|
|
12
|
+
/**
|
|
13
|
+
* Milliseconds since a hook last did work, read from the state it leaves behind.
|
|
14
|
+
*
|
|
15
|
+
* Every closing hook takes and drops a `turn-close-*` lock, and a lock file
|
|
16
|
+
* appearing or going moves its directory's mtime; a new session's state
|
|
17
|
+
* directory moves the turns directory's. One `stat` each: nothing is added to
|
|
18
|
+
* the hook path for this. A prompt hook alone leaves no trace, which is fine:
|
|
19
|
+
* the Stop is where the hook's weight is.
|
|
20
|
+
*
|
|
21
|
+
* @param paths - Where the edge keeps its state.
|
|
22
|
+
* @param nowMs - The clock.
|
|
23
|
+
* @returns The age, or Infinity when neither directory exists.
|
|
24
|
+
*/
|
|
25
|
+
export declare function hookAgeMs(paths: AgentWatchPaths, nowMs: number): Promise<number>;
|
|
26
|
+
/**
|
|
27
|
+
* The 1-minute load average and the core count.
|
|
28
|
+
*
|
|
29
|
+
* @returns Both; the load is 0 on Windows, where the OS reports none.
|
|
30
|
+
*/
|
|
31
|
+
export declare function readLoad(): {
|
|
32
|
+
readonly load1: number;
|
|
33
|
+
readonly cores: number;
|
|
34
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import fs from 'node:fs/promises';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
const POWER_SOURCE_TIMEOUT_MS = 3_000;
|
|
5
|
+
const RE_ON_BATTERY = /Battery Power/;
|
|
6
|
+
const SYS_POWER_SUPPLY = '/sys/class/power_supply';
|
|
7
|
+
/**
|
|
8
|
+
* Whether the machine is running on its battery.
|
|
9
|
+
*
|
|
10
|
+
* Fails open to "no": a laptop that cannot say is treated as plugged in, since
|
|
11
|
+
* the other reading would silence replay on every machine without `pmset`.
|
|
12
|
+
*
|
|
13
|
+
* @param platform - The OS.
|
|
14
|
+
* @returns True only when the OS says the battery is discharging.
|
|
15
|
+
*/
|
|
16
|
+
export async function readOnBattery(platform) {
|
|
17
|
+
if (platform === 'darwin')
|
|
18
|
+
return darwinOnBattery();
|
|
19
|
+
if (platform === 'linux')
|
|
20
|
+
return linuxOnBattery();
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
function darwinOnBattery() {
|
|
24
|
+
return new Promise((resolve) => {
|
|
25
|
+
execFile('pmset', ['-g', 'batt'], { timeout: POWER_SOURCE_TIMEOUT_MS }, (error, stdout) => resolve(!error && RE_ON_BATTERY.test(stdout)));
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
async function linuxOnBattery() {
|
|
29
|
+
try {
|
|
30
|
+
for (const name of await fs.readdir(SYS_POWER_SUPPLY)) {
|
|
31
|
+
if (!name.startsWith('BAT'))
|
|
32
|
+
continue;
|
|
33
|
+
const status = await fs.readFile(`${SYS_POWER_SUPPLY}/${name}/status`, 'utf8');
|
|
34
|
+
if (status.trim() === 'Discharging')
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Milliseconds since a hook last did work, read from the state it leaves behind.
|
|
45
|
+
*
|
|
46
|
+
* Every closing hook takes and drops a `turn-close-*` lock, and a lock file
|
|
47
|
+
* appearing or going moves its directory's mtime; a new session's state
|
|
48
|
+
* directory moves the turns directory's. One `stat` each: nothing is added to
|
|
49
|
+
* the hook path for this. A prompt hook alone leaves no trace, which is fine:
|
|
50
|
+
* the Stop is where the hook's weight is.
|
|
51
|
+
*
|
|
52
|
+
* @param paths - Where the edge keeps its state.
|
|
53
|
+
* @param nowMs - The clock.
|
|
54
|
+
* @returns The age, or Infinity when neither directory exists.
|
|
55
|
+
*/
|
|
56
|
+
export async function hookAgeMs(paths, nowMs) {
|
|
57
|
+
const [locks, turns] = await Promise.all([fs.stat(paths.locksDir).catch(() => undefined), fs.stat(paths.turnsDir).catch(() => undefined)]);
|
|
58
|
+
const newest = Math.max(locks?.mtimeMs ?? 0, turns?.mtimeMs ?? 0);
|
|
59
|
+
return newest === 0 ? Number.POSITIVE_INFINITY : Math.max(0, nowMs - newest);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The 1-minute load average and the core count.
|
|
63
|
+
*
|
|
64
|
+
* @returns Both; the load is 0 on Windows, where the OS reports none.
|
|
65
|
+
*/
|
|
66
|
+
export function readLoad() {
|
|
67
|
+
return { load1: os.loadavg()[0] ?? 0, cores: os.availableParallelism() };
|
|
68
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ReplayProgressEvent } from '../events/types/replay-progress.types.js';
|
|
2
|
+
import type { ReplayRecord } from './types/replay.types.js';
|
|
3
|
+
/** Who a progress record speaks for. */
|
|
4
|
+
export interface ReplayProgressIdentity {
|
|
5
|
+
readonly developerId: string;
|
|
6
|
+
readonly installationId?: string;
|
|
7
|
+
readonly surface?: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The `replay.progress` record for a replay's state: counts and the window, nothing else.
|
|
11
|
+
*
|
|
12
|
+
* The id is the run, its state, how many turns were sent and when it was
|
|
13
|
+
* written: each report is its own record. An unchanged repeat (a pause that
|
|
14
|
+
* lasts, a run that waits) must not collide with the last one, or the
|
|
15
|
+
* gateway's ten-minute duplicate window would swallow it.
|
|
16
|
+
*
|
|
17
|
+
* @param record - The replay's persisted state.
|
|
18
|
+
* @param who - The developer it is for.
|
|
19
|
+
* @returns The event.
|
|
20
|
+
*/
|
|
21
|
+
export declare function buildReplayProgress(record: ReplayRecord, who: ReplayProgressIdentity): ReplayProgressEvent;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { deriveEventId } from '../events/event-id.js';
|
|
2
|
+
import { EVENT_SCHEMA_VERSION } from '../events/constants/events.constants.js';
|
|
3
|
+
import { DEFAULT_SURFACE } from '../turns/constants/turns.constants.js';
|
|
4
|
+
import { compact } from '../core/object.js';
|
|
5
|
+
import { REPLAY_PROVIDER, REPLAY_PROVIDER_LABEL } from './constants/replay.constants.js';
|
|
6
|
+
/**
|
|
7
|
+
* The `replay.progress` record for a replay's state: counts and the window, nothing else.
|
|
8
|
+
*
|
|
9
|
+
* The id is the run, its state, how many turns were sent and when it was
|
|
10
|
+
* written: each report is its own record. An unchanged repeat (a pause that
|
|
11
|
+
* lasts, a run that waits) must not collide with the last one, or the
|
|
12
|
+
* gateway's ten-minute duplicate window would swallow it.
|
|
13
|
+
*
|
|
14
|
+
* @param record - The replay's persisted state.
|
|
15
|
+
* @param who - The developer it is for.
|
|
16
|
+
* @returns The event.
|
|
17
|
+
*/
|
|
18
|
+
export function buildReplayProgress(record, who) {
|
|
19
|
+
return compact({
|
|
20
|
+
schemaVersion: EVENT_SCHEMA_VERSION,
|
|
21
|
+
id: deriveEventId({
|
|
22
|
+
provider: REPLAY_PROVIDER,
|
|
23
|
+
providerEventType: 'replay.progress',
|
|
24
|
+
sessionId: `replay:${record.replayId}`,
|
|
25
|
+
payloadFingerprint: `${record.state}:${record.turnsSent}:${record.updatedAt}`
|
|
26
|
+
}),
|
|
27
|
+
timestamp: record.updatedAt,
|
|
28
|
+
event: { type: 'replay.progress', providerEventType: 'replay.progress' },
|
|
29
|
+
agent: { provider: REPLAY_PROVIDER_LABEL, name: REPLAY_PROVIDER_LABEL },
|
|
30
|
+
session: { id: `replay:${record.replayId}`, providerId: `replay:${record.replayId}` },
|
|
31
|
+
developer: who.installationId ? { installationId: who.installationId } : undefined,
|
|
32
|
+
provider: REPLAY_PROVIDER_LABEL,
|
|
33
|
+
surface: who.surface ?? DEFAULT_SURFACE,
|
|
34
|
+
developer_id: who.developerId,
|
|
35
|
+
replay_id: record.replayId,
|
|
36
|
+
state: record.state,
|
|
37
|
+
window_start: record.windowStart,
|
|
38
|
+
window_end: record.windowEnd,
|
|
39
|
+
turns_total: record.state === 'scanning' ? 0 : record.turnsTotal,
|
|
40
|
+
turns_sent: record.turnsSent,
|
|
41
|
+
sessions_total: record.state === 'scanning' ? 0 : record.sessionsTotal,
|
|
42
|
+
sessions_sent: record.sessionsSent,
|
|
43
|
+
started_at: record.startedAt
|
|
44
|
+
});
|
|
45
|
+
}
|