@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
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import fs from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { copiedShims, inspectCaptureEnv, installedCaptureEnv, isLinkedConfig, linkedConfigReason, removeCaptureCopy, shimPaths, withoutCaptureEnv } from '../../capture/capture-env.js';
|
|
5
|
+
import { RUN_CAPTURE_ENV_NODE, RUN_CAPTURE_ENV_PYTHON } from '../../capture/constants/capture.constants.js';
|
|
6
|
+
import { otelEnabled } from '../../config/config.js';
|
|
7
|
+
import { backupFile, writeFileAtomic } from '../../storage/atomic-file.js';
|
|
8
|
+
import { GEMINI_ENV_FILE, GEMINI_HOME_DIR } from './constants/gemini.constants.js';
|
|
9
|
+
import { RE_ENV_LINE } from './constants/gemini.otel.constants.js';
|
|
10
|
+
import { geminiEnvPath } from './gemini.detect.js';
|
|
11
|
+
const CAPTURE_KEYS = [RUN_CAPTURE_ENV_NODE, RUN_CAPTURE_ENV_PYTHON];
|
|
12
|
+
const RE_QUOTED_WITH_COMMENT = /^(['"])(.*?)\1\s*(?:#.*)?$/;
|
|
13
|
+
/**
|
|
14
|
+
* The `.env` text.
|
|
15
|
+
*
|
|
16
|
+
* @param filePath - The file.
|
|
17
|
+
* @returns Its contents, undefined when there is none, or null when it cannot
|
|
18
|
+
* be read (so it is never replaced by a file holding only our lines).
|
|
19
|
+
*/
|
|
20
|
+
export async function readEnvFile(filePath) {
|
|
21
|
+
try {
|
|
22
|
+
return await fs.readFile(filePath, 'utf8');
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
return error.code === 'ENOENT' ? undefined : null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The `KEY=value` lines of a dotenv file, the last one winning as in Gemini's
|
|
30
|
+
* loader. Quotes around a value and a trailing comment are dropped, as dotenv does: a
|
|
31
|
+
* comment kept here would be merged into `NODE_OPTIONS` as a real option.
|
|
32
|
+
*
|
|
33
|
+
* @param raw - File contents.
|
|
34
|
+
* @returns Values by key.
|
|
35
|
+
*/
|
|
36
|
+
export function parseEnv(raw) {
|
|
37
|
+
const entries = [];
|
|
38
|
+
for (const line of raw.split('\n')) {
|
|
39
|
+
const match = RE_ENV_LINE.exec(line);
|
|
40
|
+
if (!match)
|
|
41
|
+
continue;
|
|
42
|
+
const value = match[2];
|
|
43
|
+
const quoted = RE_QUOTED_WITH_COMMENT.exec(value);
|
|
44
|
+
entries.push([match[1], quoted ? quoted[2] : value.split('#')[0].trim()]);
|
|
45
|
+
}
|
|
46
|
+
return Object.fromEntries(entries);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The `.env` text with `set` written and `drop` removed; every other line,
|
|
50
|
+
* comments included, stays as it was.
|
|
51
|
+
*
|
|
52
|
+
* @param raw - Current contents.
|
|
53
|
+
* @param set - Whole lines by key: replacing that key's line where it stands,
|
|
54
|
+
* appended otherwise.
|
|
55
|
+
* @param drop - Keys to remove.
|
|
56
|
+
* @returns The next contents; `raw` itself when nothing changes.
|
|
57
|
+
*/
|
|
58
|
+
export function rewriteEnv(raw, set, drop) {
|
|
59
|
+
const body = raw.endsWith('\n') ? raw.slice(0, -1) : raw;
|
|
60
|
+
const lines = [];
|
|
61
|
+
const written = new Set();
|
|
62
|
+
for (const line of body === '' ? [] : body.split('\n')) {
|
|
63
|
+
const key = RE_ENV_LINE.exec(line)?.[1];
|
|
64
|
+
const setting = key !== undefined && Object.hasOwn(set, key);
|
|
65
|
+
if (key === undefined || (!setting && !drop.has(key))) {
|
|
66
|
+
lines.push(line);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (setting && !written.has(key))
|
|
70
|
+
lines.push(set[key]);
|
|
71
|
+
written.add(key);
|
|
72
|
+
}
|
|
73
|
+
for (const [key, line] of Object.entries(set)) {
|
|
74
|
+
if (!written.has(key))
|
|
75
|
+
lines.push(line);
|
|
76
|
+
}
|
|
77
|
+
const next = lines.length === 0 ? '' : `${lines.join('\n')}\n`;
|
|
78
|
+
return next.trimEnd() === raw.trimEnd() ? raw : next;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* A `.env` Gemini would load from this folder in place of ours.
|
|
82
|
+
*
|
|
83
|
+
* Mirrors Gemini's lookup for a trusted folder: walking up, `<dir>/.gemini/.env`
|
|
84
|
+
* then `<dir>/.env`, the first found wins. (In an untrusted folder Gemini skips
|
|
85
|
+
* ours anyway.)
|
|
86
|
+
*
|
|
87
|
+
* @param cwd - The folder Gemini starts in.
|
|
88
|
+
* @param ours - Our `.env`.
|
|
89
|
+
* @returns The shadowing file, or undefined when ours is the one loaded.
|
|
90
|
+
*/
|
|
91
|
+
export function shadowingEnvFile(cwd, ours) {
|
|
92
|
+
for (let dir = path.resolve(cwd);; dir = path.dirname(dir)) {
|
|
93
|
+
const found = [path.join(dir, GEMINI_HOME_DIR, GEMINI_ENV_FILE), path.join(dir, GEMINI_ENV_FILE)].find((file) => existsSync(file));
|
|
94
|
+
if (found)
|
|
95
|
+
return found === ours ? undefined : found;
|
|
96
|
+
if (path.dirname(dir) === dir)
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
// dotenv ends an unquoted value at `#`, which would turn a --require into a missing file.
|
|
101
|
+
function envLine(key, value) {
|
|
102
|
+
return `${key}=${value.includes('#') ? `'${value}'` : value}`;
|
|
103
|
+
}
|
|
104
|
+
/** The file with the two keys as the block has them; a key the block lost is dropped. */
|
|
105
|
+
function withCaptureKeys(raw, block) {
|
|
106
|
+
const current = parseEnv(raw);
|
|
107
|
+
const set = {};
|
|
108
|
+
const drop = new Set();
|
|
109
|
+
for (const key of CAPTURE_KEYS) {
|
|
110
|
+
const value = block[key];
|
|
111
|
+
if (typeof value !== 'string') {
|
|
112
|
+
drop.add(key);
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
// An untouched line keeps the quotes the developer gave it.
|
|
116
|
+
if (value !== current[key])
|
|
117
|
+
set[key] = envLine(key, value);
|
|
118
|
+
}
|
|
119
|
+
return rewriteEnv(raw, set, drop);
|
|
120
|
+
}
|
|
121
|
+
/*
|
|
122
|
+
* Gemini loads one `.env`, and at the home folder it looks in `.gemini/.env` before `.env`.
|
|
123
|
+
* So creating ours would stop it loading `~/.env`, and a GEMINI_API_KEY kept there with it.
|
|
124
|
+
*/
|
|
125
|
+
function wouldHideHomeEnv(context, raw) {
|
|
126
|
+
return raw === undefined && existsSync(path.join(context.env.home, GEMINI_ENV_FILE));
|
|
127
|
+
}
|
|
128
|
+
function hidesHomeEnv(context, file) {
|
|
129
|
+
return `creating ${file} would stop Gemini loading ${path.join(context.env.home, GEMINI_ENV_FILE)}; move that file's lines to ${file} and run \`agentwatch setup\``;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Merge the two keys into `~/.gemini/.env`, as owned as a key can be without being
|
|
133
|
+
* recorded: only our part of each ever goes.
|
|
134
|
+
*
|
|
135
|
+
* Only the file's own value moves in, not the ambient one. Gemini's loader never
|
|
136
|
+
* overrides what the shell set (`inspectGeminiCapture` says when that hides ours), so
|
|
137
|
+
* copying the shell's value here would only spread it to shells that do not have it.
|
|
138
|
+
*
|
|
139
|
+
* @param context - Environment and paths.
|
|
140
|
+
* @param source - The package's shims.
|
|
141
|
+
* @returns Whether the file changed, and what to tell the developer.
|
|
142
|
+
*/
|
|
143
|
+
export async function applyGeminiCapture(context, source = shimPaths()) {
|
|
144
|
+
const file = geminiEnvPath(context.env);
|
|
145
|
+
const raw = await readEnvFile(file);
|
|
146
|
+
if (raw === null)
|
|
147
|
+
return { ok: true, changed: false, messages: [`run capture skipped: ${file} is unreadable`] };
|
|
148
|
+
if (wouldHideHomeEnv(context, raw))
|
|
149
|
+
return { ok: true, changed: false, messages: [`run capture skipped: ${hidesHomeEnv(context, file)}`] };
|
|
150
|
+
if (await isLinkedConfig(file))
|
|
151
|
+
return { ok: true, changed: false, messages: [`run capture skipped: ${linkedConfigReason(file)}`] };
|
|
152
|
+
const next = withCaptureKeys(raw ?? '', await installedCaptureEnv(parseEnv(raw ?? ''), {}, context.paths.dataDir, source));
|
|
153
|
+
if (next === (raw ?? ''))
|
|
154
|
+
return { ok: true, changed: false, messages: [] };
|
|
155
|
+
await backupFile(file, context.paths.backupsDir, context.env.now());
|
|
156
|
+
// No mode: the target's own is kept, so a file holding the exporter's bearer stays 0600.
|
|
157
|
+
await writeFileAtomic(file, next);
|
|
158
|
+
return { ok: true, changed: true, messages: ['run capture configured; restart running Gemini CLI sessions to pick it up'] };
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Take our part of the two keys out, then the shared copy once nothing else needs it.
|
|
162
|
+
*
|
|
163
|
+
* @param context - Environment and paths.
|
|
164
|
+
* @returns Whether the file changed, and what to tell the developer.
|
|
165
|
+
*/
|
|
166
|
+
export async function removeGeminiCapture(context) {
|
|
167
|
+
const file = geminiEnvPath(context.env);
|
|
168
|
+
const raw = await readEnvFile(file);
|
|
169
|
+
if (raw === null)
|
|
170
|
+
return { ok: false, changed: false, messages: [`run capture: cannot read ${file}; not modified`] };
|
|
171
|
+
const next = raw === undefined ? '' : withCaptureKeys(raw, withoutCaptureEnv(parseEnv(raw), copiedShims(context.paths.dataDir)));
|
|
172
|
+
const changed = raw !== undefined && next !== raw;
|
|
173
|
+
if (changed) {
|
|
174
|
+
await backupFile(file, context.paths.backupsDir, context.env.now());
|
|
175
|
+
await (next.trim() ? writeFileAtomic(await fs.realpath(file).catch(() => file), next) : fs.rm(file, { force: true }));
|
|
176
|
+
}
|
|
177
|
+
await removeCaptureCopy(context.paths.dataDir, context.env);
|
|
178
|
+
return { ok: true, changed, messages: changed ? ['run capture configuration removed'] : [] };
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Whether what Gemini's shell tool starts loads the shims, and when not, why.
|
|
182
|
+
*
|
|
183
|
+
* Gemini's loader (`loadEnvironment`, read in 0.46.0's bundle) reads the first `.env`
|
|
184
|
+
* found walking up and never overrides a variable the shell set, so either can hide ours.
|
|
185
|
+
*
|
|
186
|
+
* ponytail: it also skips these keys in a folder the developer has not trusted. That is
|
|
187
|
+
* per folder and lives in Gemini's own trust file, so it is not read here.
|
|
188
|
+
*
|
|
189
|
+
* @param context - Environment, paths and config.
|
|
190
|
+
* @param source - The package's shims.
|
|
191
|
+
* @returns On, or off with the reason.
|
|
192
|
+
*/
|
|
193
|
+
export async function inspectGeminiCapture(context, source = shimPaths()) {
|
|
194
|
+
const file = geminiEnvPath(context.env);
|
|
195
|
+
const raw = await readEnvFile(file);
|
|
196
|
+
if (raw === null)
|
|
197
|
+
return { on: false, reason: `${file} cannot be read` };
|
|
198
|
+
if (wouldHideHomeEnv(context, raw))
|
|
199
|
+
return { on: false, reason: hidesHomeEnv(context, file) };
|
|
200
|
+
const status = await inspectCaptureEnv(parseEnv(raw ?? ''), context.config, otelEnabled(context.config), context.paths.dataDir, source);
|
|
201
|
+
if (!status.on)
|
|
202
|
+
return (await isLinkedConfig(file)) ? { on: false, reason: linkedConfigReason(file) } : status;
|
|
203
|
+
const shadow = shadowingEnvFile(context.env.cwd, file);
|
|
204
|
+
if (shadow)
|
|
205
|
+
return { on: false, reason: `Gemini loads ${shadow} in this folder, not ${file}` };
|
|
206
|
+
const shims = copiedShims(context.paths.dataDir);
|
|
207
|
+
const ours = { [RUN_CAPTURE_ENV_NODE]: shims.node, [RUN_CAPTURE_ENV_PYTHON]: shims.python };
|
|
208
|
+
const masked = CAPTURE_KEYS.filter((key) => context.env.vars[key] !== undefined && !context.env.vars[key].includes(ours[key]));
|
|
209
|
+
return masked.length > 0 ? { on: false, reason: `this shell sets ${masked.join(', ')}, and Gemini keeps a shell's value over ${file}` } : status;
|
|
210
|
+
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
+
import { canonicalRoot } from '../../config/root-config.js';
|
|
3
4
|
import { asRecord } from '../../core/object.js';
|
|
4
|
-
import {
|
|
5
|
+
import { findFirstExecutable } from '../../core/which.js';
|
|
5
6
|
import { readJsonFile } from '../../storage/json-file.js';
|
|
6
7
|
import { HOOK_COMMAND_MARKER } from '../constants/provider.constants.js';
|
|
7
|
-
import { GEMINI_CLI_VAR, GEMINI_ENV_FILE, GEMINI_EXECUTABLES, GEMINI_HOME_DIR, GEMINI_HOME_VAR, GEMINI_SETTINGS_FILE } from './constants/gemini.constants.js';
|
|
8
|
+
import { GEMINI_CLI_VAR, GEMINI_ENV_FILE, GEMINI_EXECUTABLES, GEMINI_HOME_DIR, GEMINI_HOME_EVIDENCE, GEMINI_HOME_VAR, GEMINI_SETTINGS_FILE, GEMINI_SETTINGS_KEYS_WE_WRITE } from './constants/gemini.constants.js';
|
|
8
9
|
/**
|
|
9
10
|
* Gemini CLI's configuration root.
|
|
10
11
|
*
|
|
@@ -43,48 +44,80 @@ export function geminiEnvPath(env) {
|
|
|
43
44
|
* @returns What was found.
|
|
44
45
|
*/
|
|
45
46
|
export async function detectGemini(env) {
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
if (fs.existsSync(path.join(env.cwd, GEMINI_HOME_DIR)))
|
|
50
|
-
evidence.push('.gemini directory in working tree');
|
|
47
|
+
const hookConfigPath = geminiSettingsPath(env);
|
|
48
|
+
const read = await readJsonFile(hookConfigPath);
|
|
49
|
+
const evidence = geminiFileEvidence(env, read);
|
|
51
50
|
if (env.vars[GEMINI_CLI_VAR])
|
|
52
51
|
evidence.push('GEMINI_CLI environment variable set');
|
|
53
|
-
const executablePath = findFirstExecutable(env);
|
|
52
|
+
const executablePath = findFirstExecutable(env, GEMINI_EXECUTABLES);
|
|
54
53
|
if (executablePath)
|
|
55
54
|
evidence.push(`gemini executable on PATH (${executablePath})`);
|
|
56
|
-
const hookConfigPath = geminiSettingsPath(env);
|
|
57
55
|
return {
|
|
58
56
|
detected: evidence.length > 0,
|
|
59
57
|
evidence,
|
|
60
58
|
executablePath,
|
|
61
59
|
hookConfigPath,
|
|
62
|
-
hooksInstalled:
|
|
60
|
+
hooksInstalled: hasOurHooks(settingsRecord(read))
|
|
63
61
|
};
|
|
64
62
|
}
|
|
65
63
|
/**
|
|
66
|
-
*
|
|
64
|
+
* What on disk shows Gemini CLI has been used here.
|
|
65
|
+
*
|
|
66
|
+
* Not the Gemini home directory itself: Antigravity creates `~/.gemini` too.
|
|
67
|
+
* Only entries Gemini CLI alone writes count, plus a settings.json AgentWatch
|
|
68
|
+
* did not write alone. A working-tree `.gemini` that is the Gemini home or the
|
|
69
|
+
* `~/.gemini` Antigravity uses (it ignores `GEMINI_HOME`) is not a project's.
|
|
70
|
+
*
|
|
71
|
+
* @param env - Environment supplying HOME, cwd and GEMINI_HOME.
|
|
72
|
+
* @param read - settings.json as read.
|
|
73
|
+
* @returns Evidence lines, empty when nothing points to Gemini CLI.
|
|
74
|
+
*/
|
|
75
|
+
function geminiFileEvidence(env, read) {
|
|
76
|
+
const home = geminiHome(env);
|
|
77
|
+
const evidence = GEMINI_HOME_EVIDENCE.filter((entry) => fs.existsSync(path.join(home, entry))).map((entry) => `${path.join(home, entry)} exists`);
|
|
78
|
+
const settings = settingsEvidence(read);
|
|
79
|
+
if (settings)
|
|
80
|
+
evidence.push(`${path.join(home, GEMINI_SETTINGS_FILE)} ${settings}`);
|
|
81
|
+
const project = path.join(env.cwd, GEMINI_HOME_DIR);
|
|
82
|
+
const shared = new Set([home, path.join(env.home, GEMINI_HOME_DIR)].map(canonicalRoot));
|
|
83
|
+
if (fs.existsSync(project) && !shared.has(canonicalRoot(project)))
|
|
84
|
+
evidence.push('.gemini directory in working tree');
|
|
85
|
+
return evidence;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Why settings.json shows Gemini CLI or the developer wrote it: a key
|
|
89
|
+
* AgentWatch never writes, or content AgentWatch could not have written.
|
|
90
|
+
*
|
|
91
|
+
* @param read - settings.json as read.
|
|
92
|
+
* @returns The reason, or undefined when the file is missing or only ours.
|
|
93
|
+
*/
|
|
94
|
+
function settingsEvidence(read) {
|
|
95
|
+
if (read.state === 'missing')
|
|
96
|
+
return undefined;
|
|
97
|
+
if (read.state === 'invalid')
|
|
98
|
+
return `cannot be read (${read.error})`;
|
|
99
|
+
const settings = settingsRecord(read);
|
|
100
|
+
if (!settings)
|
|
101
|
+
return 'is not a JSON object';
|
|
102
|
+
const foreign = Object.keys(settings).filter((key) => !GEMINI_SETTINGS_KEYS_WE_WRITE.has(key));
|
|
103
|
+
return foreign.length > 0 ? `has keys AgentWatch did not write (${foreign.join(', ')})` : undefined;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* settings.json as an object.
|
|
67
107
|
*
|
|
68
|
-
* @param
|
|
69
|
-
* @returns The
|
|
108
|
+
* @param read - settings.json as read.
|
|
109
|
+
* @returns The object, or undefined when the file is missing, unparseable or not an object.
|
|
70
110
|
*/
|
|
71
|
-
function
|
|
72
|
-
|
|
73
|
-
const found = findExecutable(env, name);
|
|
74
|
-
if (found)
|
|
75
|
-
return found;
|
|
76
|
-
}
|
|
77
|
-
return undefined;
|
|
111
|
+
function settingsRecord(read) {
|
|
112
|
+
return read.state === 'ok' ? asRecord(read.value) : undefined;
|
|
78
113
|
}
|
|
79
114
|
/**
|
|
80
115
|
* Whether the settings file already names the agentwatch hook command.
|
|
81
116
|
*
|
|
82
|
-
* @param
|
|
117
|
+
* @param settings - settings.json, when it is an object.
|
|
83
118
|
* @returns True when our marker appears in the hooks block.
|
|
84
119
|
*/
|
|
85
|
-
|
|
86
|
-
const read = await readJsonFile(settingsPath);
|
|
87
|
-
const settings = read.state === 'ok' ? asRecord(read.value) : undefined;
|
|
120
|
+
function hasOurHooks(settings) {
|
|
88
121
|
if (!settings)
|
|
89
122
|
return false;
|
|
90
123
|
return JSON.stringify(settings['hooks'] ?? {}).includes(HOOK_COMMAND_MARKER);
|
|
@@ -1,8 +1,24 @@
|
|
|
1
1
|
import type { Env } from '../../core/types/core.types.js';
|
|
2
2
|
import type { AgentWatchConfig, OtelSignalName } from '../../config/types/config.types.js';
|
|
3
3
|
import type { BearerRewriteContext, NativeTelemetryConfigurator, NativeTelemetryStatus, SetupContext, SetupOutcome } from '../types/provider.types.js';
|
|
4
|
+
import { geminiEnvPath } from './gemini.detect.js';
|
|
4
5
|
/** Configures Gemini CLI's own OTLP export, through `~/.gemini/.env` and settings.json. */
|
|
5
6
|
export declare class GeminiOtelConfigurator implements NativeTelemetryConfigurator {
|
|
7
|
+
private readonly captureSource;
|
|
8
|
+
/**
|
|
9
|
+
* Bind to the shims setup copies.
|
|
10
|
+
*
|
|
11
|
+
* @param captureSource - The package's shims; tests name their own.
|
|
12
|
+
*/
|
|
13
|
+
constructor(captureSource?: {
|
|
14
|
+
node: string;
|
|
15
|
+
python: string;
|
|
16
|
+
});
|
|
17
|
+
/** Run capture lives in the same `.env`, and comes and goes with the exporters. */
|
|
18
|
+
readonly runCapture: {
|
|
19
|
+
configPath: typeof geminiEnvPath;
|
|
20
|
+
inspect: (context: SetupContext) => Promise<import("../types/provider.types.js").RunCaptureStatus>;
|
|
21
|
+
};
|
|
6
22
|
/**
|
|
7
23
|
* Whether this agent can export native telemetry at all.
|
|
8
24
|
*
|
|
@@ -18,7 +34,11 @@ export declare class GeminiOtelConfigurator implements NativeTelemetryConfigurat
|
|
|
18
34
|
*/
|
|
19
35
|
inspect(context: SetupContext): Promise<NativeTelemetryStatus>;
|
|
20
36
|
/**
|
|
21
|
-
* Write the
|
|
37
|
+
* Write the exporters and run capture into Gemini CLI's `.env`.
|
|
38
|
+
*
|
|
39
|
+
* Capture does not wait on the export: a developer's own telemetry keys, or content
|
|
40
|
+
* consent withheld, leaves the export out, and neither has anything to do with counting
|
|
41
|
+
* a script's calls.
|
|
22
42
|
*
|
|
23
43
|
* @param context - Environment, paths, config and install state.
|
|
24
44
|
* @returns Whether the file changed, what to tell the user, and the next
|
|
@@ -26,13 +46,29 @@ export declare class GeminiOtelConfigurator implements NativeTelemetryConfigurat
|
|
|
26
46
|
*/
|
|
27
47
|
configure(context: SetupContext): Promise<SetupOutcome>;
|
|
28
48
|
/**
|
|
29
|
-
*
|
|
49
|
+
* Take the exporters and run capture out of Gemini CLI's files.
|
|
30
50
|
*
|
|
31
51
|
* @param context - Environment, paths, config and install state.
|
|
32
52
|
* @returns Whether a file changed, what to tell the user, and the next
|
|
33
53
|
* install state.
|
|
34
54
|
*/
|
|
35
55
|
uninstall(context: SetupContext): Promise<SetupOutcome>;
|
|
56
|
+
/**
|
|
57
|
+
* Write the telemetry configuration into Gemini CLI's `.env`.
|
|
58
|
+
*
|
|
59
|
+
* @param context - Environment, paths, config and install state.
|
|
60
|
+
* @returns Whether the file changed, what to tell the user, and the next
|
|
61
|
+
* install state.
|
|
62
|
+
*/
|
|
63
|
+
private configureExport;
|
|
64
|
+
/**
|
|
65
|
+
* Remove the telemetry configuration AgentWatch wrote.
|
|
66
|
+
*
|
|
67
|
+
* @param context - Environment, paths, config and install state.
|
|
68
|
+
* @returns Whether a file changed, what to tell the user, and the next
|
|
69
|
+
* install state.
|
|
70
|
+
*/
|
|
71
|
+
private removeExport;
|
|
36
72
|
/**
|
|
37
73
|
* Where the exporter keys `uninstall` would remove send.
|
|
38
74
|
*
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { existsSync } from 'node:fs';
|
|
2
1
|
import fs from 'node:fs/promises';
|
|
3
2
|
import path from 'node:path';
|
|
3
|
+
import { shimPaths, skippedOnError, withCaptureOutcome } from '../../capture/capture-env.js';
|
|
4
4
|
import { asRecord, omitKeys } from '../../core/object.js';
|
|
5
|
-
import { enabledSignalNames, otlpBaseUrl, toolContentConsented } from '../../config/config.js';
|
|
5
|
+
import { enabledSignalNames, otelEnabled, otlpBaseUrl, toolContentConsented } from '../../config/config.js';
|
|
6
6
|
import { INSTALLATION_HEADER } from '../../transport/constants/transport.constants.js';
|
|
7
7
|
import { backupFile, currentMode, writeFileAtomic, writeIfUnchanged } from '../../storage/atomic-file.js';
|
|
8
8
|
import { SECRET_FILE_MODE } from '../../storage/constants/storage.constants.js';
|
|
@@ -10,10 +10,11 @@ import { readJsonFile } from '../../storage/json-file.js';
|
|
|
10
10
|
import { deviceFor, hasCredential } from '../../credentials/identity.js';
|
|
11
11
|
import { BEARER_PLACEHOLDER, HOOK_COMMAND_MARKER } from '../constants/provider.constants.js';
|
|
12
12
|
import { writeJsonValidated } from '../shared/hook-config.js';
|
|
13
|
-
import { withOtelInstall, withoutOtelInstall } from '../shared/install-record.js';
|
|
13
|
+
import { foldPriorValues, otelPriorValuesOf, parsePriorValue, withOtelInstall, withoutOtelInstall } from '../shared/install-record.js';
|
|
14
|
+
import { applyGeminiCapture, inspectGeminiCapture, parseEnv, readEnvFile, removeGeminiCapture, rewriteEnv, shadowingEnvFile } from './gemini.capture.js';
|
|
14
15
|
import { geminiEnvPath, geminiSettingsPath } from './gemini.detect.js';
|
|
15
|
-
import { GEMINI_ENV_FILE,
|
|
16
|
-
import { ENV_BLOCK_KEY, GEMINI_ENDPOINT_KEYS, GEMINI_LEGACY_OWNED_KEYS, GEMINI_OTLP_PROTOCOL, GEMINI_TELEMETRY_TARGET_LOCAL, LEGACY_HELPER_KEY, OTEL_EXPORTER_NONE, OTEL_EXPORTER_OTLP, OTLP_HEADERS_KEY, RE_BEARER_PAIR, RE_ENV_LINE,
|
|
16
|
+
import { GEMINI_ENV_FILE, GEMINI_PROVIDER_ID } from './constants/gemini.constants.js';
|
|
17
|
+
import { ENV_BLOCK_KEY, GEMINI_ENDPOINT_KEYS, GEMINI_LEGACY_OWNED_KEYS, GEMINI_OTLP_PROTOCOL, GEMINI_TELEMETRY_TARGET_LOCAL, LEGACY_HELPER_KEY, OTEL_EXPORTER_NONE, OTEL_EXPORTER_OTLP, OTLP_HEADERS_KEY, RE_BEARER_PAIR, RE_ENV_LINE, STANDARD_OTLP_PROTOCOL, TELEMETRY_BLOCK_KEY } from './constants/gemini.otel.constants.js';
|
|
17
18
|
/**
|
|
18
19
|
* Gemini's own prompt-logging switch, forced off.
|
|
19
20
|
*
|
|
@@ -119,6 +120,20 @@ function telemetryField(key) {
|
|
|
119
120
|
}
|
|
120
121
|
/** Configures Gemini CLI's own OTLP export, through `~/.gemini/.env` and settings.json. */
|
|
121
122
|
export class GeminiOtelConfigurator {
|
|
123
|
+
captureSource;
|
|
124
|
+
/**
|
|
125
|
+
* Bind to the shims setup copies.
|
|
126
|
+
*
|
|
127
|
+
* @param captureSource - The package's shims; tests name their own.
|
|
128
|
+
*/
|
|
129
|
+
constructor(captureSource = shimPaths()) {
|
|
130
|
+
this.captureSource = captureSource;
|
|
131
|
+
}
|
|
132
|
+
/** Run capture lives in the same `.env`, and comes and goes with the exporters. */
|
|
133
|
+
runCapture = {
|
|
134
|
+
configPath: geminiEnvPath,
|
|
135
|
+
inspect: (context) => inspectGeminiCapture(context, this.captureSource)
|
|
136
|
+
};
|
|
122
137
|
/**
|
|
123
138
|
* Whether this agent can export native telemetry at all.
|
|
124
139
|
*
|
|
@@ -177,13 +192,40 @@ export class GeminiOtelConfigurator {
|
|
|
177
192
|
return { supported: true, configured: true };
|
|
178
193
|
}
|
|
179
194
|
/**
|
|
180
|
-
* Write the
|
|
195
|
+
* Write the exporters and run capture into Gemini CLI's `.env`.
|
|
196
|
+
*
|
|
197
|
+
* Capture does not wait on the export: a developer's own telemetry keys, or content
|
|
198
|
+
* consent withheld, leaves the export out, and neither has anything to do with counting
|
|
199
|
+
* a script's calls.
|
|
181
200
|
*
|
|
182
201
|
* @param context - Environment, paths, config and install state.
|
|
183
202
|
* @returns Whether the file changed, what to tell the user, and the next
|
|
184
203
|
* install state.
|
|
185
204
|
*/
|
|
186
205
|
async configure(context) {
|
|
206
|
+
const outcome = await this.configureExport(context);
|
|
207
|
+
const capture = otelEnabled(context.config) ? await skippedOnError(applyGeminiCapture(context, this.captureSource)) : await removeGeminiCapture(context);
|
|
208
|
+
return withCaptureOutcome(outcome, capture);
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Take the exporters and run capture out of Gemini CLI's files.
|
|
212
|
+
*
|
|
213
|
+
* @param context - Environment, paths, config and install state.
|
|
214
|
+
* @returns Whether a file changed, what to tell the user, and the next
|
|
215
|
+
* install state.
|
|
216
|
+
*/
|
|
217
|
+
async uninstall(context) {
|
|
218
|
+
const capture = await removeGeminiCapture(context);
|
|
219
|
+
return withCaptureOutcome(await this.removeExport(context), capture);
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Write the telemetry configuration into Gemini CLI's `.env`.
|
|
223
|
+
*
|
|
224
|
+
* @param context - Environment, paths, config and install state.
|
|
225
|
+
* @returns Whether the file changed, what to tell the user, and the next
|
|
226
|
+
* install state.
|
|
227
|
+
*/
|
|
228
|
+
async configureExport(context) {
|
|
187
229
|
const desired = desiredGeminiOtelEnv(context, context.bearer);
|
|
188
230
|
if (!desired)
|
|
189
231
|
return this.configureDisabled(context);
|
|
@@ -201,21 +243,16 @@ export class GeminiOtelConfigurator {
|
|
|
201
243
|
return {
|
|
202
244
|
ok: false,
|
|
203
245
|
changed: false,
|
|
246
|
+
conflict: true,
|
|
204
247
|
messages: [`skipping native OpenTelemetry: ${conflicting.join(', ')} already set in ${envPath} or settings.json (not AgentWatch-owned)`]
|
|
205
248
|
};
|
|
206
249
|
}
|
|
207
250
|
const legacy = await updateSettings(context, false, fields);
|
|
208
251
|
if (!legacy.ok)
|
|
209
252
|
return legacy;
|
|
210
|
-
// What each key held before we first set it, so uninstall gives it back.
|
|
211
|
-
|
|
212
|
-
const
|
|
213
|
-
const previous = priorValuesOf(context);
|
|
214
|
-
const before = { ...envLinesByKey(raw), ...legacy.prior };
|
|
215
|
-
const priorValues = Object.fromEntries([...Object.keys(desired), ...Object.keys(fields)].flatMap((key) => {
|
|
216
|
-
const prior = owned.has(key) ? previous[key] : before[key];
|
|
217
|
-
return prior === undefined ? [] : [[key, prior]];
|
|
218
|
-
}));
|
|
253
|
+
// What each key held before we first set it, so uninstall gives it back.
|
|
254
|
+
const previous = otelPriorValuesOf(context.installState, GEMINI_PROVIDER_ID);
|
|
255
|
+
const priorValues = foldPriorValues([...Object.keys(desired), ...Object.keys(fields)], ownedKeys, previous, { ...envLinesByKey(raw), ...legacy.prior });
|
|
219
256
|
// Keys we own that this configuration no longer wants (a signal turned off
|
|
220
257
|
// since the last run) would otherwise linger; the developer's own go back.
|
|
221
258
|
const staleKeys = ownedKeys.filter((key) => desired[key] === undefined && !Object.hasOwn(fields, key));
|
|
@@ -252,9 +289,9 @@ export class GeminiOtelConfigurator {
|
|
|
252
289
|
* @returns Whether a file changed, what to tell the user, and the next
|
|
253
290
|
* install state.
|
|
254
291
|
*/
|
|
255
|
-
async
|
|
292
|
+
async removeExport(context) {
|
|
256
293
|
const owned = ownedKeysOf(context);
|
|
257
|
-
const priors =
|
|
294
|
+
const priors = otelPriorValuesOf(context.installState, GEMINI_PROVIDER_ID);
|
|
258
295
|
const record = context.installState.agents[GEMINI_PROVIDER_ID];
|
|
259
296
|
// The file setup wrote, even when HOME or GEMINI_HOME differ now.
|
|
260
297
|
const envPath = owned.length > 0 && record?.otelConfigPath ? record.otelConfigPath : geminiEnvPath(context.env);
|
|
@@ -274,7 +311,7 @@ export class GeminiOtelConfigurator {
|
|
|
274
311
|
}
|
|
275
312
|
// After the .env, so a settings.json Gemini parses but we cannot never
|
|
276
313
|
// keeps the bearer in place. Our fields go back to what they held, or go.
|
|
277
|
-
const fields = Object.fromEntries(owned.filter((key) => telemetryField(key)).map((key) => [key,
|
|
314
|
+
const fields = Object.fromEntries(owned.filter((key) => telemetryField(key)).map((key) => [key, parsePriorValue(priors[key])]));
|
|
278
315
|
const legacy = await updateSettings(context, true, fields);
|
|
279
316
|
if (!legacy.ok)
|
|
280
317
|
return { ...legacy, changed: changed || legacy.changed };
|
|
@@ -316,7 +353,7 @@ export class GeminiOtelConfigurator {
|
|
|
316
353
|
async configureDisabled(context) {
|
|
317
354
|
if (anySignal(safeGeminiSignals(context.config)))
|
|
318
355
|
return { ok: false, changed: false, messages: ['no backend endpoint configured'] };
|
|
319
|
-
const removed = await this.
|
|
356
|
+
const removed = await this.removeExport(context);
|
|
320
357
|
if (!removed.ok)
|
|
321
358
|
return removed;
|
|
322
359
|
return {
|
|
@@ -547,49 +584,6 @@ async function settingsTelemetry(env) {
|
|
|
547
584
|
const read = await readJsonFile(geminiSettingsPath(env));
|
|
548
585
|
return asRecord(asRecord(read.state === 'ok' ? read.value : undefined)?.[TELEMETRY_BLOCK_KEY]) ?? {};
|
|
549
586
|
}
|
|
550
|
-
/**
|
|
551
|
-
* A `.env` Gemini would load from this folder in place of ours.
|
|
552
|
-
*
|
|
553
|
-
* Mirrors Gemini's lookup for a trusted folder: walking up, `<dir>/.gemini/.env`
|
|
554
|
-
* then `<dir>/.env`, the first found wins. (In an untrusted folder Gemini skips
|
|
555
|
-
* ours anyway.)
|
|
556
|
-
*
|
|
557
|
-
* @param cwd - The folder Gemini starts in.
|
|
558
|
-
* @param ours - Our `.env`.
|
|
559
|
-
* @returns The shadowing file, or undefined when ours is the one loaded.
|
|
560
|
-
*/
|
|
561
|
-
function shadowingEnvFile(cwd, ours) {
|
|
562
|
-
for (let dir = path.resolve(cwd);; dir = path.dirname(dir)) {
|
|
563
|
-
const found = [path.join(dir, GEMINI_HOME_DIR, GEMINI_ENV_FILE), path.join(dir, GEMINI_ENV_FILE)].find((file) => existsSync(file));
|
|
564
|
-
if (found)
|
|
565
|
-
return found === ours ? undefined : found;
|
|
566
|
-
if (path.dirname(dir) === dir)
|
|
567
|
-
return undefined;
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
/**
|
|
571
|
-
* What owned keys held before a setup first set them.
|
|
572
|
-
*
|
|
573
|
-
* @param context - Install state.
|
|
574
|
-
* @returns Prior text by key; `.env` keys hold their whole line.
|
|
575
|
-
*/
|
|
576
|
-
function priorValuesOf(context) {
|
|
577
|
-
return context.installState.agents[GEMINI_PROVIDER_ID]?.otelPriorValues ?? {};
|
|
578
|
-
}
|
|
579
|
-
/**
|
|
580
|
-
* A recorded settings.json value.
|
|
581
|
-
*
|
|
582
|
-
* @param prior - Its JSON text, if one was recorded.
|
|
583
|
-
* @returns The value; undefined (remove the field) when absent or unparseable.
|
|
584
|
-
*/
|
|
585
|
-
function parsePrior(prior) {
|
|
586
|
-
try {
|
|
587
|
-
return prior === undefined ? undefined : JSON.parse(prior);
|
|
588
|
-
}
|
|
589
|
-
catch {
|
|
590
|
-
return undefined;
|
|
591
|
-
}
|
|
592
|
-
}
|
|
593
587
|
/**
|
|
594
588
|
* The developer's own `.env` lines to put back for these keys.
|
|
595
589
|
*
|
|
@@ -600,21 +594,6 @@ function parsePrior(prior) {
|
|
|
600
594
|
function restoredLines(priors, keys) {
|
|
601
595
|
return Object.fromEntries(keys.flatMap((key) => (telemetryField(key) === undefined && priors[key] !== undefined ? [[key, priors[key]]] : [])));
|
|
602
596
|
}
|
|
603
|
-
/**
|
|
604
|
-
* The `.env` text.
|
|
605
|
-
*
|
|
606
|
-
* @param filePath - The file.
|
|
607
|
-
* @returns Its contents, undefined when there is none, or null when it cannot
|
|
608
|
-
* be read (so it is never replaced by a file holding only our lines).
|
|
609
|
-
*/
|
|
610
|
-
async function readEnvFile(filePath) {
|
|
611
|
-
try {
|
|
612
|
-
return await fs.readFile(filePath, 'utf8');
|
|
613
|
-
}
|
|
614
|
-
catch (error) {
|
|
615
|
-
return error.code === 'ENOENT' ? undefined : null;
|
|
616
|
-
}
|
|
617
|
-
}
|
|
618
597
|
/**
|
|
619
598
|
* The refusal for a `.env` that exists but cannot be read.
|
|
620
599
|
*
|
|
@@ -624,22 +603,6 @@ async function readEnvFile(filePath) {
|
|
|
624
603
|
function unreadable(envPath) {
|
|
625
604
|
return { ok: false, changed: false, messages: [`refusing to modify unreadable ${envPath}`] };
|
|
626
605
|
}
|
|
627
|
-
/**
|
|
628
|
-
* The `KEY=value` lines of a dotenv file, the last one winning as in Gemini's
|
|
629
|
-
* loader. Quotes around a value are dropped.
|
|
630
|
-
*
|
|
631
|
-
* @param raw - File contents.
|
|
632
|
-
* @returns Values by key.
|
|
633
|
-
*/
|
|
634
|
-
function parseEnv(raw) {
|
|
635
|
-
const entries = [];
|
|
636
|
-
for (const line of raw.split('\n')) {
|
|
637
|
-
const match = RE_ENV_LINE.exec(line);
|
|
638
|
-
if (match)
|
|
639
|
-
entries.push([match[1], match[2].replace(RE_QUOTED_VALUE, '$2')]);
|
|
640
|
-
}
|
|
641
|
-
return Object.fromEntries(entries);
|
|
642
|
-
}
|
|
643
606
|
/**
|
|
644
607
|
* The last `KEY=value` line for each key, as written.
|
|
645
608
|
*
|
|
@@ -655,35 +618,3 @@ function envLinesByKey(raw) {
|
|
|
655
618
|
}
|
|
656
619
|
return lines;
|
|
657
620
|
}
|
|
658
|
-
/**
|
|
659
|
-
* The `.env` text with `set` written and `drop` removed; every other line,
|
|
660
|
-
* comments included, stays as it was.
|
|
661
|
-
*
|
|
662
|
-
* @param raw - Current contents.
|
|
663
|
-
* @param set - Whole lines by key: replacing that key's line where it stands,
|
|
664
|
-
* appended otherwise.
|
|
665
|
-
* @param drop - Keys to remove.
|
|
666
|
-
* @returns The next contents; `raw` itself when nothing changes.
|
|
667
|
-
*/
|
|
668
|
-
function rewriteEnv(raw, set, drop) {
|
|
669
|
-
const body = raw.endsWith('\n') ? raw.slice(0, -1) : raw;
|
|
670
|
-
const lines = [];
|
|
671
|
-
const written = new Set();
|
|
672
|
-
for (const line of body === '' ? [] : body.split('\n')) {
|
|
673
|
-
const key = RE_ENV_LINE.exec(line)?.[1];
|
|
674
|
-
const setting = key !== undefined && Object.hasOwn(set, key);
|
|
675
|
-
if (key === undefined || (!setting && !drop.has(key))) {
|
|
676
|
-
lines.push(line);
|
|
677
|
-
continue;
|
|
678
|
-
}
|
|
679
|
-
if (setting && !written.has(key))
|
|
680
|
-
lines.push(set[key]);
|
|
681
|
-
written.add(key);
|
|
682
|
-
}
|
|
683
|
-
for (const [key, line] of Object.entries(set)) {
|
|
684
|
-
if (!written.has(key))
|
|
685
|
-
lines.push(line);
|
|
686
|
-
}
|
|
687
|
-
const next = lines.length === 0 ? '' : `${lines.join('\n')}\n`;
|
|
688
|
-
return next.trimEnd() === raw.trimEnd() ? raw : next;
|
|
689
|
-
}
|