@agent-watch-ai/edge 0.5.0 → 0.7.0
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 +238 -7
- package/README.md +308 -8
- 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 +3 -2
- package/dist/billing/session-billing.js +8 -8
- 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 +479 -0
- package/dist/capture/constants/capture.constants.d.ts +70 -0
- package/dist/capture/constants/capture.constants.js +72 -0
- package/dist/capture/launched-runs.d.ts +16 -0
- package/dist/capture/launched-runs.js +151 -0
- package/dist/capture/node.cjs +697 -0
- package/dist/capture/node.d.cts +32 -0
- package/dist/capture/python/sitecustomize.py +823 -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/cli/constants/cli.constants.d.ts +2 -6
- package/dist/cli/constants/cli.constants.js +3 -7
- package/dist/cli/context.d.ts +19 -22
- package/dist/cli/context.js +23 -54
- package/dist/cli/doctor-run-capture.d.ts +13 -0
- package/dist/cli/doctor-run-capture.js +19 -0
- package/dist/cli/doctor.js +16 -67
- package/dist/cli/hook.d.ts +14 -0
- package/dist/cli/hook.js +40 -0
- 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/replay.d.ts +33 -0
- package/dist/cli/replay.js +193 -0
- package/dist/cli/setup.d.ts +10 -2
- package/dist/cli/setup.js +154 -26
- 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 +8 -22
- package/dist/cli.js +104 -17
- package/dist/config/config.d.ts +0 -2
- package/dist/config/config.js +0 -1
- package/dist/config/constants/config.constants.js +2 -0
- 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 +13 -0
- package/dist/config/schemas/config.schema.js +42 -6
- 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 +32 -0
- package/dist/credentials/cloud-claim.js +42 -0
- package/dist/credentials/constants/credentials.constants.d.ts +8 -1
- package/dist/credentials/constants/credentials.constants.js +9 -2
- package/dist/credentials/device-api.d.ts +15 -1
- package/dist/credentials/device-api.js +12 -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/events/constants/events.constants.d.ts +8 -2
- package/dist/events/constants/events.constants.js +7 -1
- package/dist/events/index.d.ts +2 -0
- package/dist/events/product-event.d.ts +10 -4
- 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-snapshot.types.d.ts +1 -4
- package/dist/events/types/repo-tree.types.d.ts +25 -0
- package/dist/events/types/repo-tree.types.js +1 -0
- 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 +31 -11
- package/dist/git/constants/git.constants.js +36 -12
- package/dist/git/constants/tree.constants.d.ts +71 -0
- package/dist/git/constants/tree.constants.js +92 -0
- package/dist/git/fingerprint.d.ts +3 -6
- package/dist/git/fingerprint.js +5 -8
- package/dist/git/git-context.d.ts +1 -0
- package/dist/git/git-context.js +8 -3
- 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 +47 -9
- package/dist/git/repo-tree.d.ts +69 -0
- package/dist/git/repo-tree.js +130 -0
- package/dist/git/types/git.types.d.ts +5 -2
- package/dist/git/types/snapshot.types.d.ts +14 -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 +112 -72
- package/dist/privacy/product-capture.js +36 -5
- 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 +17 -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 +29 -0
- package/dist/replay/checkout-resolver.js +69 -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 +58 -0
- package/dist/replay/replay-runner.js +529 -0
- package/dist/replay/replay-scan.d.ts +83 -0
- package/dist/replay/replay-scan.js +170 -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 +35 -0
- package/dist/replay/throttle.js +41 -0
- package/dist/replay/transcript-turns.d.ts +35 -0
- package/dist/replay/transcript-turns.js +215 -0
- package/dist/replay/turn-event.d.ts +38 -0
- package/dist/replay/turn-event.js +116 -0
- package/dist/replay/types/replay.types.d.ts +162 -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/snapshot/branch-selection.d.ts +40 -23
- package/dist/snapshot/branch-selection.js +74 -42
- package/dist/snapshot/budget.js +2 -2
- package/dist/snapshot/constants/snapshot-stages.constants.d.ts +10 -0
- package/dist/snapshot/constants/snapshot-stages.constants.js +10 -0
- package/dist/snapshot/constants/snapshot.constants.d.ts +37 -4
- package/dist/snapshot/constants/snapshot.constants.js +37 -4
- package/dist/snapshot/index.d.ts +6 -1
- package/dist/snapshot/index.js +3 -1
- package/dist/snapshot/snapshot-event.d.ts +7 -0
- package/dist/snapshot/snapshot-event.js +8 -5
- package/dist/snapshot/snapshot-pipeline.js +89 -26
- package/dist/snapshot/snapshot-state.js +24 -2
- package/dist/snapshot/tree-event.d.ts +14 -0
- package/dist/snapshot/tree-event.js +91 -0
- package/dist/snapshot/tree-pipeline.d.ts +12 -0
- package/dist/snapshot/tree-pipeline.js +172 -0
- package/dist/snapshot/types/snapshot.types.d.ts +62 -2
- package/dist/storage/schemas/storage.schema.d.ts +2 -0
- package/dist/storage/schemas/storage.schema.js +6 -0
- package/dist/transport/constants/transport.constants.d.ts +5 -0
- package/dist/transport/constants/transport.constants.js +5 -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/queue.js +40 -6
- 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 +13 -12
- package/dist/turns/constants/turns.constants.js +15 -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 +49 -7
- package/dist/turns/turn-state.js +78 -10
- package/dist/turns/turn-summary.js +10 -6
- package/dist/turns/turn-tracker.d.ts +9 -16
- package/dist/turns/turn-tracker.js +162 -86
- 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 +20 -6
- 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 +269 -32
- package/docs/ENTERPRISE_DEPLOYMENT.md +1 -1
- package/docs/PERFORMANCE.md +27 -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
package/dist/cli/misc.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import process from 'node:process';
|
|
2
2
|
import { eventsUrl, otlpBaseUrl, sharesOtlpCollector } from '../config/config.js';
|
|
3
3
|
import { loadEffectiveConfig } from '../config/repo-config.js';
|
|
4
|
-
import { applyRootOverride, credentialScope } from '../config/root-config.js';
|
|
4
|
+
import { applyRootOverride, credentialScope, isIgnoredRoot } from '../config/root-config.js';
|
|
5
5
|
import { isAllowedHost } from '../config/schemas/config.schema.js';
|
|
6
6
|
import { resolveBearer } from '../credentials/credentials.js';
|
|
7
7
|
import { claudeExporterTargets, claudeOverrideTargets } from '../providers/claude/claude.otel.js';
|
|
@@ -99,7 +99,15 @@ export async function runOtelHeaders(env) {
|
|
|
99
99
|
// The current access token, refreshed here whenever it would not outlast
|
|
100
100
|
// Claude Code's debounce on this helper: what it prints is reused until then.
|
|
101
101
|
// An install key still in the config is exchanged first.
|
|
102
|
-
|
|
102
|
+
// An ignored folder gets no bearer and no installation id: the exporter is the
|
|
103
|
+
// agent's own and goes straight to the backend, so a headerless export (401) is
|
|
104
|
+
// the only way this machine can make it hear nothing from that folder.
|
|
105
|
+
// ponytail: Claude Code runs this once per ~29 min, in the folder it runs in; a
|
|
106
|
+
// call already exported with a cached bearer, or a session whose folder changes
|
|
107
|
+
// mid-way, is not recalled. Codex and Gemini hold a static bearer in a config
|
|
108
|
+
// file and cannot be gated per folder from here.
|
|
109
|
+
const ignored = isIgnoredRoot(context.config.ignoredRoots, env.cwd);
|
|
110
|
+
const shared = !context.disabled && !ignored && sharesOtlpCollector(context.config, rooted) && (await exportsWhereConfigured(env, context.config));
|
|
103
111
|
const token = shared ? await resolveBearer({ env, paths: context.paths, timeoutMs: rooted.delivery.timeoutMs, scope: credentialScope(root), minRemainingMs: OTEL_HEADERS_MIN_TOKEN_LIFE_MS }, rooted) : undefined;
|
|
104
112
|
// The installation id rides with the bearer and on the same condition: an
|
|
105
113
|
// export that carries no credential names no machine either. It is what lets
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Env } from '../core/types/core.types.js';
|
|
2
|
+
/** What `agentwatch replay` was given. */
|
|
3
|
+
export interface ReplayCommandOptions {
|
|
4
|
+
readonly env: Env;
|
|
5
|
+
/** Select the machine identity regardless of the shell's directory. */
|
|
6
|
+
readonly machine?: boolean;
|
|
7
|
+
/** Select the configured identity governing this root regardless of the shell's directory. */
|
|
8
|
+
readonly root?: string;
|
|
9
|
+
/** `--replay-days`, as typed. */
|
|
10
|
+
readonly days?: string;
|
|
11
|
+
/** `--resume`: continue an unfinished replay; begin none. */
|
|
12
|
+
readonly resume: boolean;
|
|
13
|
+
/** `--stop`: end the replay. */
|
|
14
|
+
readonly stop: boolean;
|
|
15
|
+
/** `--dry-run`: build and count, send and write nothing. */
|
|
16
|
+
readonly dryRun: boolean;
|
|
17
|
+
/** Internal: setup's start, which never repeats an earlier replay attempt. */
|
|
18
|
+
readonly skipIfExisting: boolean;
|
|
19
|
+
/** Internal: atomically consume the machine's one automatic replay. */
|
|
20
|
+
readonly claimAutomatic?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* `agentwatch replay` — send this developer's earlier Claude Code, Codex and Gemini CLI sessions to the backend, slowly.
|
|
24
|
+
*
|
|
25
|
+
* Setup starts it in the background; run by hand it works in the foreground
|
|
26
|
+
* and can be interrupted (a rerun or `--resume` carries on from the last
|
|
27
|
+
* finished transcript). What it sends is exactly what the hooks send for a
|
|
28
|
+
* turn, from the transcripts on disk (see docs/DATA_HANDLING.md).
|
|
29
|
+
*
|
|
30
|
+
* @param options - The flags.
|
|
31
|
+
* @returns 0, or 1 when it could not start.
|
|
32
|
+
*/
|
|
33
|
+
export declare function runReplayCommand(options: ReplayCommandOptions): Promise<number>;
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import process from 'node:process';
|
|
3
|
+
import { sleep } from '../core/async.js';
|
|
4
|
+
import { hasCredential, identityFingerprint, identityOf } from '../credentials/identity.js';
|
|
5
|
+
import { billingModeOf } from '../billing/billing-mode.js';
|
|
6
|
+
import { claudeProvider } from '../providers/claude/claude.provider.js';
|
|
7
|
+
import { loadProvider } from '../providers/loaders.js';
|
|
8
|
+
import { isIgnoredRoot, selectRoot } from '../config/root-config.js';
|
|
9
|
+
import { loadEffectiveConfig } from '../config/repo-config.js';
|
|
10
|
+
import { developerDisplayName, developerIdentity } from '../git/git-context.js';
|
|
11
|
+
import { createCheckoutResolver } from '../replay/checkout-resolver.js';
|
|
12
|
+
import { REPLAY_BATTERY_CHECK_MS, REPLAY_SEND_TIMEOUT_MS } from '../replay/constants/replay.constants.js';
|
|
13
|
+
import { hookAgeMs, readLoad, readOnBattery } from '../replay/machine.js';
|
|
14
|
+
import { claimAutomaticReplay, replayStateFile } from '../replay/replay-state.js';
|
|
15
|
+
import { codexHome } from '../providers/codex/codex.detect.js';
|
|
16
|
+
import { GEMINI_PROVIDER_ID } from '../providers/gemini/constants/gemini.constants.js';
|
|
17
|
+
import { geminiSettingsPath } from '../providers/gemini/gemini.detect.js';
|
|
18
|
+
import { codexSource } from '../replay/codex-turns.js';
|
|
19
|
+
import { geminiSource } from '../replay/gemini-turns.js';
|
|
20
|
+
import { claudeProjectsDir, claudeSource } from '../replay/replay-scan.js';
|
|
21
|
+
import { runReplay, stopReplay } from '../replay/replay-runner.js';
|
|
22
|
+
import { parseReplayDays } from '../replay/window.js';
|
|
23
|
+
import { identityPaths } from '../transport/queue-partition.js';
|
|
24
|
+
import { buildCliContext, buildTransport, openIdentityDelivery, quoteArg, resolveContextBearer } from './context.js';
|
|
25
|
+
import { bold, dim, println, symbols } from './ui.js';
|
|
26
|
+
/**
|
|
27
|
+
* `agentwatch replay` — send this developer's earlier Claude Code, Codex and Gemini CLI sessions to the backend, slowly.
|
|
28
|
+
*
|
|
29
|
+
* Setup starts it in the background; run by hand it works in the foreground
|
|
30
|
+
* and can be interrupted (a rerun or `--resume` carries on from the last
|
|
31
|
+
* finished transcript). What it sends is exactly what the hooks send for a
|
|
32
|
+
* turn, from the transcripts on disk (see docs/DATA_HANDLING.md).
|
|
33
|
+
*
|
|
34
|
+
* @param options - The flags.
|
|
35
|
+
* @returns 0, or 1 when it could not start.
|
|
36
|
+
*/
|
|
37
|
+
export async function runReplayCommand(options) {
|
|
38
|
+
const days = parseReplayDays(options.days);
|
|
39
|
+
if (days === undefined) {
|
|
40
|
+
println(`${symbols.fail} invalid --replay-days "${options.days}" (expected a whole number of days, 1 to 366)`);
|
|
41
|
+
return 1;
|
|
42
|
+
}
|
|
43
|
+
if (options.machine === true && options.root !== undefined) {
|
|
44
|
+
println(`${symbols.fail} choose either --machine or --root, not both`);
|
|
45
|
+
return 1;
|
|
46
|
+
}
|
|
47
|
+
let identityAt;
|
|
48
|
+
if (options.machine === true)
|
|
49
|
+
identityAt = null;
|
|
50
|
+
if (options.root !== undefined)
|
|
51
|
+
identityAt = path.resolve(options.env.cwd, options.root);
|
|
52
|
+
const context = await buildCliContext(options.env, identityAt);
|
|
53
|
+
if (options.root !== undefined && context.identityRoot === undefined) {
|
|
54
|
+
println(`${symbols.fail} no replay identity is configured for root ${identityAt}`);
|
|
55
|
+
return 1;
|
|
56
|
+
}
|
|
57
|
+
let replayCommand = 'agentwatch replay';
|
|
58
|
+
if (options.machine === true)
|
|
59
|
+
replayCommand = 'agentwatch replay --machine';
|
|
60
|
+
if (context.identityRoot !== undefined)
|
|
61
|
+
replayCommand = `agentwatch replay --root ${quoteArg(context.identityRoot)}`;
|
|
62
|
+
const ports = await buildPorts(context, options.dryRun);
|
|
63
|
+
if (typeof ports === 'string') {
|
|
64
|
+
println(`${symbols.fail} ${ports}`);
|
|
65
|
+
return 1;
|
|
66
|
+
}
|
|
67
|
+
if (options.stop)
|
|
68
|
+
return report(await stopReplay(ports), replayCommand);
|
|
69
|
+
if (options.dryRun)
|
|
70
|
+
return runDry(ports, days);
|
|
71
|
+
if (options.claimAutomatic === true && !(await claimAutomaticReplay(context.paths.configDir, context.paths.dataDir))) {
|
|
72
|
+
println(`${symbols.off} automatic historical replay was already attempted on this machine`);
|
|
73
|
+
return 0;
|
|
74
|
+
}
|
|
75
|
+
if (!options.resume)
|
|
76
|
+
println(`Replaying the last ${days} days of Claude Code, Codex and Gemini CLI sessions. ${dim(`Ctrl-C stops it; \`${replayCommand} --resume\` carries on.`)}`);
|
|
77
|
+
return report(await runReplay(ports, { days, resume: options.resume, dryRun: false, skipIfExisting: options.skipIfExisting }), replayCommand);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Count what a replay would send, and what that cost this machine.
|
|
81
|
+
*
|
|
82
|
+
* @param ports - The machine.
|
|
83
|
+
* @param days - The window.
|
|
84
|
+
* @returns 0.
|
|
85
|
+
*/
|
|
86
|
+
async function runDry(ports, days) {
|
|
87
|
+
const started = performance.now();
|
|
88
|
+
const result = await runReplay(ports, { days, resume: false, dryRun: true, skipIfExisting: false });
|
|
89
|
+
const wallMs = performance.now() - started;
|
|
90
|
+
const usage = process.resourceUsage();
|
|
91
|
+
println(bold('Replay dry run — nothing was sent or written'));
|
|
92
|
+
println(`window: ${result.record?.windowStart} to ${result.record?.windowEnd}`);
|
|
93
|
+
println(`transcripts: ${result.dry?.files ?? 0}`);
|
|
94
|
+
println(`turns counted ahead of sending: ${result.dry?.estimate ?? 0}`);
|
|
95
|
+
println(`turns that would be sent: ${result.dry?.turns ?? 0}`);
|
|
96
|
+
println(`repositories: ${result.dry?.repositories ?? 0}`);
|
|
97
|
+
for (const [agent, counts] of Object.entries(result.dry?.agents ?? {}))
|
|
98
|
+
println(` ${agent}: ${counts.files} files, ${counts.turns} turns`);
|
|
99
|
+
println(`wall ${(wallMs / 1000).toFixed(1)} s, cpu ${((usage.userCPUTime + usage.systemCPUTime) / 1e6).toFixed(1)} s, peak memory ${(usage.maxRSS / 1024).toFixed(0)} MB`);
|
|
100
|
+
return 0;
|
|
101
|
+
}
|
|
102
|
+
function report(result, replayCommand) {
|
|
103
|
+
println(`${result.outcome === 'skipped' ? symbols.off : symbols.ok} ${result.message.replace('agentwatch replay', replayCommand)}`);
|
|
104
|
+
return 0;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Everything a run takes from this machine, or why it cannot run.
|
|
108
|
+
*
|
|
109
|
+
* @param context - Resolved CLI context.
|
|
110
|
+
* @param dryRun - A dry run needs no backend and no credential.
|
|
111
|
+
* @returns The ports, or a sentence saying what is missing.
|
|
112
|
+
*/
|
|
113
|
+
async function buildPorts(context, dryRun) {
|
|
114
|
+
const { env } = context;
|
|
115
|
+
const config = context.identityConfig;
|
|
116
|
+
const token = identityOf(config);
|
|
117
|
+
if (!dryRun) {
|
|
118
|
+
if (context.disabled)
|
|
119
|
+
return 'AgentWatch is disabled; run `agentwatch on` first';
|
|
120
|
+
if (context.configState === 'invalid')
|
|
121
|
+
return `the config at ${context.paths.configFile} is invalid`;
|
|
122
|
+
if (!hasCredential(config) || !config.endpoint)
|
|
123
|
+
return 'no backend is configured here; run `agentwatch setup`';
|
|
124
|
+
}
|
|
125
|
+
const developerId = (await developerIdentity(config.developerEmail, env.cwd, { home: env.home })) ?? (dryRun ? 'dry-run' : undefined);
|
|
126
|
+
if (!developerId)
|
|
127
|
+
return 'no developer identity: pass --developer-email to `agentwatch setup`, or set git user.email';
|
|
128
|
+
const paths = identityPaths(context.paths, token);
|
|
129
|
+
const opened = openIdentityDelivery(context);
|
|
130
|
+
let battery;
|
|
131
|
+
return {
|
|
132
|
+
env,
|
|
133
|
+
paths: context.paths,
|
|
134
|
+
config,
|
|
135
|
+
stateFile: replayStateFile(paths.statsFile),
|
|
136
|
+
identity: token ? identityFingerprint(token) : 'dry-run',
|
|
137
|
+
developerId,
|
|
138
|
+
developerName: await developerDisplayName(config.developerName, env.cwd, { home: env.home }),
|
|
139
|
+
installationId: config.installationId,
|
|
140
|
+
billingMode: await billingModeOf(claudeProvider, env),
|
|
141
|
+
sources: await replaySources(context),
|
|
142
|
+
transport: async () => (dryRun ? undefined : buildTransport(context, REPLAY_SEND_TIMEOUT_MS, await resolveContextBearer(context, REPLAY_SEND_TIMEOUT_MS))),
|
|
143
|
+
queue: opened.queue,
|
|
144
|
+
authBlock: opened.authBlock,
|
|
145
|
+
cooldown: opened.cooldown,
|
|
146
|
+
inScope: (cwd) => !isIgnoredRoot(context.config.ignoredRoots, cwd) && selectRoot(context.config.roots, cwd)?.path === context.identityRoot,
|
|
147
|
+
checkout: createCheckoutResolver(),
|
|
148
|
+
captureFor: cachedCaptureFor(async (cwd) => (await loadEffectiveConfig(context.paths, cwd)).config.capture),
|
|
149
|
+
machine: async () => {
|
|
150
|
+
const nowMs = env.now().getTime();
|
|
151
|
+
// `pmset` is a process start: asked every few minutes, not every batch.
|
|
152
|
+
if (!battery || nowMs - battery.at >= REPLAY_BATTERY_CHECK_MS)
|
|
153
|
+
battery = { at: nowMs, on: await readOnBattery(env.platform) };
|
|
154
|
+
return { ...readLoad(), onBattery: battery.on, hookAgeMs: await hookAgeMs(context.paths, nowMs) };
|
|
155
|
+
},
|
|
156
|
+
sleep,
|
|
157
|
+
log: (line) => println(dim(line))
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* The agents whose transcripts a replay reads, each where its provider looks for them, with its billing mode.
|
|
162
|
+
*
|
|
163
|
+
* @param context - Resolved CLI context.
|
|
164
|
+
* @returns Claude Code, Codex and Gemini CLI.
|
|
165
|
+
*/
|
|
166
|
+
async function replaySources(context) {
|
|
167
|
+
const { env } = context;
|
|
168
|
+
const firstInstalledAt = context.installState.agents[GEMINI_PROVIDER_ID]?.hooksFirstInstalledAt;
|
|
169
|
+
const firstInstalledMs = firstInstalledAt === undefined ? undefined : Date.parse(firstInstalledAt);
|
|
170
|
+
const sources = [
|
|
171
|
+
claudeSource(claudeProjectsDir(env.home)),
|
|
172
|
+
codexSource(codexHome(env)),
|
|
173
|
+
geminiSource(path.dirname(geminiSettingsPath(env)), Number.isNaN(firstInstalledMs) ? undefined : firstInstalledMs)
|
|
174
|
+
];
|
|
175
|
+
return Promise.all(sources.map(async (source) => {
|
|
176
|
+
const provider = await loadProvider(source.agent);
|
|
177
|
+
return { ...source, billingMode: await billingModeOf(provider, env) };
|
|
178
|
+
}));
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Remember each folder's answer for the run: the repo file is read once per folder, not once per turn.
|
|
182
|
+
*
|
|
183
|
+
* @param read - Reads the capture flags in force for a folder.
|
|
184
|
+
* @returns The same question, answered from memory after the first time.
|
|
185
|
+
*/
|
|
186
|
+
function cachedCaptureFor(read) {
|
|
187
|
+
const answers = new Map();
|
|
188
|
+
return (cwd) => {
|
|
189
|
+
const known = answers.get(cwd) ?? read(cwd);
|
|
190
|
+
answers.set(cwd, known);
|
|
191
|
+
return known;
|
|
192
|
+
};
|
|
193
|
+
}
|
package/dist/cli/setup.d.ts
CHANGED
|
@@ -10,10 +10,18 @@ export type { SetupOptions } from './types/cli.types.js';
|
|
|
10
10
|
* a missing token fails the run before the first file is touched. No endpoint
|
|
11
11
|
* at all means the production Agent Watch backend.
|
|
12
12
|
*
|
|
13
|
+
* `--cloud` is the managed SessionStart hook of a Claude Code cloud VM: it
|
|
14
|
+
* claims a device from the organization's managed settings and then runs the
|
|
15
|
+
* same config write and agent install. It returns 0 on every path, so a cloud
|
|
16
|
+
* session never fails over Agent Watch; a VM that already has a credential,
|
|
17
|
+
* or is no cloud session, returns at once.
|
|
18
|
+
*
|
|
13
19
|
* @param options - Environment and flags.
|
|
14
|
-
* @returns 0 when every step succeeded, else 1.
|
|
20
|
+
* @returns 0 when every step succeeded, else 1 (always 0 with `cloud`).
|
|
15
21
|
*/
|
|
16
|
-
export declare function runSetup(options: SetupOptions
|
|
22
|
+
export declare function runSetup(options: SetupOptions & {
|
|
23
|
+
readonly cloud?: boolean;
|
|
24
|
+
}): Promise<number>;
|
|
17
25
|
/**
|
|
18
26
|
* What to call this developer: the flag, then the config, then `git config
|
|
19
27
|
* user.name`.
|
package/dist/cli/setup.js
CHANGED
|
@@ -9,23 +9,28 @@ import { deliverableOrigin, needsTrust } from '../config/schemas/config.schema.j
|
|
|
9
9
|
import { applyRootOverride, canonicalRoot } from '../config/root-config.js';
|
|
10
10
|
import { transitionDestination } from '../config/destination.js';
|
|
11
11
|
import { asRecord, compact } from '../core/object.js';
|
|
12
|
-
import {
|
|
12
|
+
import { cloudIdentity } from '../credentials/cloud-claim.js';
|
|
13
|
+
import { CLAUDE_CODE_REMOTE_VAR, CREDENTIALS_DIR_NAME, DEVICE_SOURCE_BATCH_CODE } from '../credentials/constants/credentials.constants.js';
|
|
13
14
|
import { forgetDeviceIfUnused, nativeExporterBearer, revokeDeviceIfUnused } from '../credentials/credentials.js';
|
|
14
15
|
import { awaitingSignIn, credentialId, deviceFor, hasCredential, identityOf } from '../credentials/identity.js';
|
|
15
16
|
import { abandonDevice, migrateInstallKey } from '../credentials/migration.js';
|
|
16
|
-
import { enrollWithCode, signInDevice } from '../credentials/sign-in.js';
|
|
17
|
+
import { enrollWithCloudClaim, enrollWithCode, signInDevice } from '../credentials/sign-in.js';
|
|
17
18
|
import { identityPaths } from '../transport/queue-partition.js';
|
|
18
19
|
import { clearUninstalled, configScopes, uninstallScope } from '../storage/uninstalled.js';
|
|
19
20
|
import { storeLabel } from '../credentials/secret-store.js';
|
|
20
21
|
import { collectGitContext, developerDisplayName, developerIdentity } from '../git/git-context.js';
|
|
21
22
|
import { resolveEnrollment } from '../enrollment/enrollment.js';
|
|
22
23
|
import { providers } from '../providers/registry.js';
|
|
24
|
+
import { REPLAY_AGENTS } from '../replay/constants/replay.constants.js';
|
|
25
|
+
import { automaticReplayAttempted, readReplayRecord, replayStateFile } from '../replay/replay-state.js';
|
|
23
26
|
import { removeRefusedExporters } from '../providers/shared/exporter-allowlist.js';
|
|
24
27
|
import { saveInstallState } from '../storage/install-state.js';
|
|
25
28
|
import { readJsonFile } from '../storage/json-file.js';
|
|
26
29
|
import { ENROLLMENT_CODE_VAR, MACHINE_SCOPE, TOKEN_VAR } from '../storage/constants/storage.constants.js';
|
|
30
|
+
import { startReplayDetached } from '../replay/start.js';
|
|
31
|
+
import { parseReplayDays } from '../replay/window.js';
|
|
27
32
|
import { signInOpener } from './open-browser.js';
|
|
28
|
-
import { buildCliContext, buildHookCommand,
|
|
33
|
+
import { buildCliContext, buildHookCommand, openIdentityDelivery, quoteArg } from './context.js';
|
|
29
34
|
import { DEVELOPER_IDENTITY_REMEDIES, ENROLLMENT_CODE_STDIN, NO_CONFIG_WRITTEN, NO_DEVELOPER_IDENTITY, SETUP_EXCHANGE_TIMEOUT_MS } from './constants/cli.constants.js';
|
|
30
35
|
import { bold, customEndpointOverride, dim, printErrln, println, symbols } from './ui.js';
|
|
31
36
|
/**
|
|
@@ -37,11 +42,51 @@ import { bold, customEndpointOverride, dim, printErrln, println, symbols } from
|
|
|
37
42
|
* a missing token fails the run before the first file is touched. No endpoint
|
|
38
43
|
* at all means the production Agent Watch backend.
|
|
39
44
|
*
|
|
45
|
+
* `--cloud` is the managed SessionStart hook of a Claude Code cloud VM: it
|
|
46
|
+
* claims a device from the organization's managed settings and then runs the
|
|
47
|
+
* same config write and agent install. It returns 0 on every path, so a cloud
|
|
48
|
+
* session never fails over Agent Watch; a VM that already has a credential,
|
|
49
|
+
* or is no cloud session, returns at once.
|
|
50
|
+
*
|
|
40
51
|
* @param options - Environment and flags.
|
|
41
|
-
* @returns 0 when every step succeeded, else 1.
|
|
52
|
+
* @returns 0 when every step succeeded, else 1 (always 0 with `cloud`).
|
|
42
53
|
*/
|
|
43
54
|
export async function runSetup(options) {
|
|
44
55
|
const context = await buildCliContext(options.env);
|
|
56
|
+
if (options.cloud !== true)
|
|
57
|
+
return setup(options, context);
|
|
58
|
+
try {
|
|
59
|
+
if (options.env.vars[CLAUDE_CODE_REMOTE_VAR] !== 'true') {
|
|
60
|
+
println(`agentwatch setup --cloud: not a Claude Code cloud session (${CLAUDE_CODE_REMOTE_VAR} is not true); nothing to do`);
|
|
61
|
+
return 0;
|
|
62
|
+
}
|
|
63
|
+
// Resume, or a VM set up another way: never a second device.
|
|
64
|
+
if (context.configState === 'ok' && hasCredential(context.config))
|
|
65
|
+
return 0;
|
|
66
|
+
const identity = await cloudIdentity(options.env);
|
|
67
|
+
if ('skip' in identity) {
|
|
68
|
+
println(`agentwatch setup --cloud: ${identity.skip}`);
|
|
69
|
+
return 0;
|
|
70
|
+
}
|
|
71
|
+
// The flag path of resolveDeveloperEmail: git (noreply@anthropic.com here) is never asked.
|
|
72
|
+
// A fresh VM has no earlier sessions to replay.
|
|
73
|
+
await setup({ ...options, developerEmail: identity.email, noReplay: true }, context, identity);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
// Never the error: it can come from a device route.
|
|
77
|
+
println('agentwatch setup --cloud: setup stopped unexpectedly; Agent Watch not installed this session, records stay queued');
|
|
78
|
+
}
|
|
79
|
+
return 0;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The setup run itself, once `runSetup` has built the context.
|
|
83
|
+
*
|
|
84
|
+
* @param options - Environment and flags.
|
|
85
|
+
* @param context - Resolved CLI context.
|
|
86
|
+
* @param cloud - The managed-settings identity of a `--cloud` run.
|
|
87
|
+
* @returns 0 when every step succeeded, else 1.
|
|
88
|
+
*/
|
|
89
|
+
async function setup(options, context, cloud) {
|
|
45
90
|
println(bold('AgentWatch Setup'));
|
|
46
91
|
println();
|
|
47
92
|
if (context.disabled) {
|
|
@@ -75,11 +120,16 @@ export async function runSetup(options) {
|
|
|
75
120
|
println(`${symbols.fail} invalid --otel value "${options.otel}" (expected "all", "none" or a comma list of logs,traces,metrics)`);
|
|
76
121
|
return 1;
|
|
77
122
|
}
|
|
123
|
+
const replayDays = parseReplayDays(options.replayDays);
|
|
124
|
+
if (replayDays === undefined) {
|
|
125
|
+
println(`${symbols.fail} invalid --replay-days "${options.replayDays}" (expected a whole number of days, 1 to 366)`);
|
|
126
|
+
return 1;
|
|
127
|
+
}
|
|
78
128
|
reportRepository(await collectGitContext({ cwd: options.env.cwd, includeChangedFiles: false }));
|
|
79
129
|
println();
|
|
80
130
|
const detected = await reportDetection(options);
|
|
81
131
|
if (detected.length === 0) {
|
|
82
|
-
println('No supported agents found. Install Claude Code or Codex and re-run `agentwatch setup`.');
|
|
132
|
+
println(options.agents === undefined ? 'No supported agents found. Install Claude Code or Codex and re-run `agentwatch setup`.' : `${options.agents.map((provider) => provider.displayName).join(', ')} is not detected here. Install it, or run setup without --agent.`);
|
|
83
133
|
return 1;
|
|
84
134
|
}
|
|
85
135
|
const resolved = await resolveRoot(options, baseConfig);
|
|
@@ -97,12 +147,14 @@ export async function runSetup(options) {
|
|
|
97
147
|
println(`${symbols.fail} --defer-sign-in takes the organization enrollment URL and configures the machine, not a project root; nothing was written`);
|
|
98
148
|
return 1;
|
|
99
149
|
}
|
|
100
|
-
const
|
|
101
|
-
if ('error' in
|
|
102
|
-
println(`${symbols.fail} ${
|
|
150
|
+
const resolvedEnrollment = enroll({ ...options, setupUrl: options.setupUrl ?? pendingSignInUrl(options, baseConfig, rootPath) }, inherited, baseConfig.customEndpoints);
|
|
151
|
+
if ('error' in resolvedEnrollment) {
|
|
152
|
+
println(`${symbols.fail} ${resolvedEnrollment.error}`);
|
|
103
153
|
return 1;
|
|
104
154
|
}
|
|
105
|
-
|
|
155
|
+
// A cloud claim goes through the sign-in path: the endpoint as judged, never a key.
|
|
156
|
+
const enrolled = cloud ? { endpoint: resolvedEnrollment.endpoint, signIn: { organizationId: cloud.organizationId } } : resolvedEnrollment;
|
|
157
|
+
const code = await readEnrollmentCode(options, enrolled.signIn !== undefined && !cloud);
|
|
106
158
|
if ('error' in code) {
|
|
107
159
|
println(`${symbols.fail} ${code.error}`);
|
|
108
160
|
return 1;
|
|
@@ -128,7 +180,8 @@ export async function runSetup(options) {
|
|
|
128
180
|
println(`${symbols.fail} no token: pass --token or $${TOKEN_VAR}`);
|
|
129
181
|
return 1;
|
|
130
182
|
}
|
|
131
|
-
|
|
183
|
+
// A cloud VM's config may be a copy of another's: its installation is always its own.
|
|
184
|
+
const installationId = (cloud ? undefined : baseConfig.installationId) ?? crypto.randomUUID();
|
|
132
185
|
const developerEmail = await resolveDeveloperEmail(options, baseConfig);
|
|
133
186
|
// A sign-in or a code names the developer itself (checked once it has), and a deferred install names nobody yet.
|
|
134
187
|
if (!developerEmail && !enrolled.signIn) {
|
|
@@ -138,13 +191,14 @@ export async function runSetup(options) {
|
|
|
138
191
|
// Absent is a real answer and must stay absent: writing an empty name would
|
|
139
192
|
// hand the platform a value to store, and the backend coalesces — it keeps the
|
|
140
193
|
// name it already has only for as long as nobody sends a worse one.
|
|
141
|
-
|
|
194
|
+
// A cloud VM's git name is the agent's, not the developer's.
|
|
195
|
+
const developerName = cloud ? undefined : await resolveDeveloperName(options, baseConfig);
|
|
142
196
|
if (spent)
|
|
143
197
|
println(`${symbols.ok} already enrolled with this enrollment code; it was not redeemed again`);
|
|
144
198
|
// After every check that can refuse the run, so a developer never signs in
|
|
145
199
|
// for a setup that was going to fail anyway. A spent code signs nobody in.
|
|
146
200
|
// Deferred: no sign-in here. A device its developer already enrolled is kept below.
|
|
147
|
-
const signedIn = enrolled.signIn && !deferred && !spent ? await signIn(options, context, { ...enrolled.signIn, endpoint: enrolled.endpoint, installationId, code: redeem }) : undefined;
|
|
201
|
+
const signedIn = enrolled.signIn && !deferred && !spent ? await signIn(options, context, { ...enrolled.signIn, endpoint: enrolled.endpoint, installationId, code: redeem, cloud }) : undefined;
|
|
148
202
|
if (signedIn && 'error' in signedIn) {
|
|
149
203
|
println(`${symbols.fail} ${signedIn.error}`);
|
|
150
204
|
return 1;
|
|
@@ -172,7 +226,8 @@ export async function runSetup(options) {
|
|
|
172
226
|
device,
|
|
173
227
|
// A sign-in's device belongs to whoever approved it, not to this checkout's git email.
|
|
174
228
|
developerEmail: signedIn?.developerEmail ?? developerEmail,
|
|
175
|
-
|
|
229
|
+
// A cloud VM never keeps a name a copied config carried in.
|
|
230
|
+
...(developerName || cloud ? { developerName } : {})
|
|
176
231
|
};
|
|
177
232
|
const stored = rootPath === undefined ? baseConfig : baseConfig.roots?.[rootPath];
|
|
178
233
|
const previous = rootPath === undefined ? baseConfig.endpoint : previousEndpoint(stored, baseConfig);
|
|
@@ -247,6 +302,40 @@ export async function runSetup(options) {
|
|
|
247
302
|
if (detected.some(({ provider }) => provider.id === 'codex'))
|
|
248
303
|
println(dim(' Codex: approve the hooks with /hooks; until you do, no hook runs'));
|
|
249
304
|
println(dim(' run `agentwatch status` anytime, `agentwatch doctor` to diagnose'));
|
|
305
|
+
// Only a credential can send, and only these agents leave transcripts with token counts.
|
|
306
|
+
const replayed = detected.filter(({ provider }) => REPLAY_AGENTS.has(provider.id)).map(({ provider }) => provider.displayName);
|
|
307
|
+
const replayCommand = rootPath === undefined ? 'agentwatch replay --machine' : `agentwatch replay --root ${quoteArg(rootPath)}`;
|
|
308
|
+
const replayable = options.noReplay !== true && !deferred && !awaitingSignIn(config) && replayed.length > 0;
|
|
309
|
+
// A replay reads every agent's transcripts, so with --agent it would send the sessions of agents left out.
|
|
310
|
+
if (options.agents !== undefined && replayable)
|
|
311
|
+
println(dim(` to send earlier sessions of every agent, run \`${replayCommand}${options.replayDays === undefined ? '' : ` --replay-days ${replayDays}`}\``));
|
|
312
|
+
if (options.agents === undefined && replayable) {
|
|
313
|
+
const loaded = await loadConfig(context.paths);
|
|
314
|
+
const currentIdentity = rootPath === undefined ? loaded.config : applyRootOverride(loaded.config, rootPath).config;
|
|
315
|
+
const currentToken = identityOf(currentIdentity);
|
|
316
|
+
const currentFile = replayStateFile(identityPaths(context.paths, currentToken).statsFile);
|
|
317
|
+
const replay = currentToken ? await readReplayRecord(currentFile) : undefined;
|
|
318
|
+
const replaySelector = rootPath === undefined ? ['--machine'] : ['--root', rootPath];
|
|
319
|
+
let attempted = true;
|
|
320
|
+
let checkFailed = false;
|
|
321
|
+
try {
|
|
322
|
+
attempted = await automaticReplayAttempted(context.paths.configDir, context.paths.dataDir);
|
|
323
|
+
}
|
|
324
|
+
catch {
|
|
325
|
+
checkFailed = true;
|
|
326
|
+
println(`${symbols.warn} automatic historical replay was not started because its one-time marker could not be checked; run \`${replayCommand}\` explicitly if you want it`);
|
|
327
|
+
}
|
|
328
|
+
if (replay) {
|
|
329
|
+
println(`${symbols.warn} historical replay already exists (${replay.state}); setup did not start another. Use \`${replayCommand} --resume\` to continue it, \`${replayCommand} --stop\` to abandon it, or run \`${replayCommand}\` explicitly to repeat a finished replay.`);
|
|
330
|
+
}
|
|
331
|
+
if (!replay && attempted && !checkFailed) {
|
|
332
|
+
println(`${symbols.warn} automatic historical replay was already attempted on this machine; setup did not start another. Run \`${replayCommand}\` explicitly to repeat it.`);
|
|
333
|
+
}
|
|
334
|
+
if (!replay && !attempted && !checkFailed) {
|
|
335
|
+
println(`Replaying the last ${replayDays} days of ${replayed.join(', ')} sessions in the background; \`${replayCommand} --stop\` to cancel; \`--no-replay\` skips`);
|
|
336
|
+
startReplayDetached(options.env, ['replay', ...replaySelector, '--replay-days', String(replayDays), '--claim-automatic', '--skip-if-existing'], rootPath ?? options.env.home);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
250
339
|
return failures === 0 ? 0 : 1;
|
|
251
340
|
}
|
|
252
341
|
/**
|
|
@@ -329,10 +418,17 @@ async function readPipedStdin() {
|
|
|
329
418
|
* @param target.organizationId - The organization's public id.
|
|
330
419
|
* @param target.installationId - The installation the credential is bound to.
|
|
331
420
|
* @param target.code - The admin-issued code; the browser sign-in runs without one.
|
|
421
|
+
* @param target.cloud - The managed-settings identity a cloud VM claims its device with.
|
|
332
422
|
* @returns The device, or the message to show.
|
|
333
423
|
*/
|
|
334
424
|
async function signIn(options, context, target) {
|
|
335
425
|
const ctx = { env: options.env, paths: context.paths, timeoutMs: SETUP_EXCHANGE_TIMEOUT_MS, fetchFn: options.fetchFn };
|
|
426
|
+
if (target.cloud) {
|
|
427
|
+
const claimed = await enrollWithCloudClaim(ctx, { endpoint: target.endpoint, identity: target.cloud, installationId: target.installationId, name: os.hostname() });
|
|
428
|
+
if (!('error' in claimed))
|
|
429
|
+
println(`${symbols.ok} claimed a cloud device; device credential kept in ${storeLabel(claimed.device.store)}`);
|
|
430
|
+
return claimed;
|
|
431
|
+
}
|
|
336
432
|
if (target.code) {
|
|
337
433
|
const redeemed = await enrollWithCode(ctx, { endpoint: target.endpoint, code: target.code, installationId: target.installationId, name: os.hostname() });
|
|
338
434
|
if (!('error' in redeemed))
|
|
@@ -495,14 +591,14 @@ function reportRepository(git) {
|
|
|
495
591
|
println(`${symbols.ok} repository: ${git.repository ?? git.repositoryRoot}${git.branch ? dim(` (branch: ${git.branch})`) : ''}`);
|
|
496
592
|
}
|
|
497
593
|
/**
|
|
498
|
-
* Detect
|
|
594
|
+
* Detect the agents this run targets and report what was found.
|
|
499
595
|
*
|
|
500
|
-
* @param options - Environment.
|
|
596
|
+
* @param options - Environment and the agents `--agent` selected.
|
|
501
597
|
* @returns The agents that were detected.
|
|
502
598
|
*/
|
|
503
599
|
async function reportDetection(options) {
|
|
504
600
|
println(bold('Detected coding agents:'));
|
|
505
|
-
const results = await Promise.all(providers.map(async (provider) => ({ provider, detection: await provider.detect(options.env) })));
|
|
601
|
+
const results = await Promise.all((options.agents ?? providers).map(async (provider) => ({ provider, detection: await provider.detect(options.env) })));
|
|
506
602
|
for (const { provider, detection } of results) {
|
|
507
603
|
println(detection.detected ? `${symbols.ok} ${provider.displayName}` : `${symbols.off} ${provider.displayName} not detected`);
|
|
508
604
|
}
|
|
@@ -677,14 +773,14 @@ async function reportStrandedBacklog(context, previousConfig, config, owner) {
|
|
|
677
773
|
return;
|
|
678
774
|
if (previousUrl === configuredUrl && identityOf(previousConfig) === identityOf(config))
|
|
679
775
|
return;
|
|
680
|
-
const queue =
|
|
776
|
+
const queue = openIdentityDelivery({ ...context, identityConfig: previousConfig }).queue;
|
|
681
777
|
const stranded = await queue.pendingFor(previousUrl);
|
|
682
778
|
if (stranded === 0)
|
|
683
779
|
return;
|
|
684
780
|
println(`${symbols.warn} keeping ${stranded} offline event(s) captured under ${owner} under the previous identity; they expire after ${config.delivery.maxEventAgeDays} day(s)`);
|
|
685
781
|
}
|
|
686
782
|
/**
|
|
687
|
-
* Register hooks and native telemetry in every
|
|
783
|
+
* Register hooks in every detected agent, and native telemetry in those and every agent installed before.
|
|
688
784
|
*
|
|
689
785
|
* Install state is threaded from one operation to the next and persisted once at
|
|
690
786
|
* the end, so a failure part-way through cannot leave a record claiming
|
|
@@ -693,14 +789,20 @@ async function reportStrandedBacklog(context, previousConfig, config, owner) {
|
|
|
693
789
|
* @param options - Environment and the hook-command override.
|
|
694
790
|
* @param context - Resolved CLI context.
|
|
695
791
|
* @param config - The configuration just written.
|
|
696
|
-
* @param detected - Agents to install into.
|
|
792
|
+
* @param detected - Agents to install hooks into.
|
|
697
793
|
* @param bearer - What native exporters authenticate with; see `SetupContext.bearer`.
|
|
698
794
|
* @returns How many steps were skipped or failed.
|
|
699
795
|
*/
|
|
700
796
|
async function installAgents(options, context, config, detected, bearer) {
|
|
701
797
|
let installState = context.installState;
|
|
702
798
|
let failures = 0;
|
|
703
|
-
|
|
799
|
+
const hooked = new Set(detected.map(({ provider }) => provider.id));
|
|
800
|
+
for (const provider of installTargets(options, context.installState, hooked)) {
|
|
801
|
+
// No exporter before a sign-in: it would send with no credential at all.
|
|
802
|
+
const exports = provider.nativeTelemetry !== undefined && !awaitingSignIn(config) && (await provider.nativeTelemetry.supported(options.env));
|
|
803
|
+
// An agent visited only for its exporter has nothing to say when there is none to write.
|
|
804
|
+
if (!hooked.has(provider.id) && !exports)
|
|
805
|
+
continue;
|
|
704
806
|
println(bold(provider.displayName));
|
|
705
807
|
const setupContext = {
|
|
706
808
|
env: options.env,
|
|
@@ -710,12 +812,17 @@ async function installAgents(options, context, config, detected, bearer) {
|
|
|
710
812
|
installState,
|
|
711
813
|
...(bearer ? { bearer } : {})
|
|
712
814
|
};
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
815
|
+
if (hooked.has(provider.id)) {
|
|
816
|
+
const hooks = await provider.installHooks(setupContext);
|
|
817
|
+
installState = printOutcome(hooks, installState);
|
|
818
|
+
// About hooks that are in: a refused install has nothing to qualify.
|
|
819
|
+
if (hooks.ok)
|
|
820
|
+
for (const caveat of provider.caveats ?? [])
|
|
821
|
+
println(`${symbols.warn} note: ${caveat.detail}`);
|
|
822
|
+
if (!hooks.ok)
|
|
823
|
+
failures++;
|
|
824
|
+
}
|
|
825
|
+
if (exports && provider.nativeTelemetry) {
|
|
719
826
|
// First, so an exporter left pointing at a host this config refuses is
|
|
720
827
|
// gone even when there is no allowed route to write in its place.
|
|
721
828
|
const swept = await removeRefusedExporters({ ...setupContext, installState }, [provider]);
|
|
@@ -733,6 +840,27 @@ async function installAgents(options, context, config, detected, bearer) {
|
|
|
733
840
|
await saveInstallState(context.paths, installState);
|
|
734
841
|
return failures;
|
|
735
842
|
}
|
|
843
|
+
/**
|
|
844
|
+
* The agents this run visits: those it hooks, and with `--agent` every agent
|
|
845
|
+
* Agent Watch installed before.
|
|
846
|
+
*
|
|
847
|
+
* Signals, endpoint and identity are machine-wide, so `--agent` narrows the
|
|
848
|
+
* hooks only: an installed agent left out keeps exporting to an old endpoint,
|
|
849
|
+
* or not at all after a deferred sign-in. Installed is what install state
|
|
850
|
+
* records, never a hooks file that merely mentions Agent Watch: only what we
|
|
851
|
+
* installed is ours to point at a backend.
|
|
852
|
+
*
|
|
853
|
+
* @param options - The agents `--agent` selected, if any.
|
|
854
|
+
* @param installState - What earlier runs installed.
|
|
855
|
+
* @param hooked - Ids of the agents this run installs hooks into.
|
|
856
|
+
* @returns The providers in registry order.
|
|
857
|
+
*/
|
|
858
|
+
function installTargets(options, installState, hooked) {
|
|
859
|
+
return providers.filter((provider) => {
|
|
860
|
+
const installed = installState.agents[provider.id];
|
|
861
|
+
return hooked.has(provider.id) || (options.agents !== undefined && (installed?.hooksInstalledAt !== undefined || installed?.otelConfiguredAt !== undefined));
|
|
862
|
+
});
|
|
863
|
+
}
|
|
736
864
|
/**
|
|
737
865
|
* Print an outcome and carry its install state forward.
|
|
738
866
|
*
|