@agent-watch-ai/edge 0.6.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (309) hide show
  1. package/CHANGELOG.md +288 -9
  2. package/README.md +307 -5
  3. package/dist/billing/billing-mode.d.ts +37 -5
  4. package/dist/billing/billing-mode.js +28 -19
  5. package/dist/billing/constants/billing.constants.d.ts +0 -2
  6. package/dist/billing/constants/billing.constants.js +0 -2
  7. package/dist/billing/index.d.ts +2 -1
  8. package/dist/billing/index.js +1 -1
  9. package/dist/billing/session-billing.d.ts +7 -3
  10. package/dist/billing/session-billing.js +34 -13
  11. package/dist/billing/types/billing.types.d.ts +7 -0
  12. package/dist/capture/capture-env.d.ts +147 -0
  13. package/dist/capture/capture-env.js +301 -0
  14. package/dist/capture/captured-step.d.ts +88 -0
  15. package/dist/capture/captured-step.js +503 -0
  16. package/dist/capture/constants/capture.constants.d.ts +85 -0
  17. package/dist/capture/constants/capture.constants.js +87 -0
  18. package/dist/capture/launched-runs.d.ts +43 -0
  19. package/dist/capture/launched-runs.js +157 -0
  20. package/dist/capture/node.cjs +697 -0
  21. package/dist/capture/node.d.cts +32 -0
  22. package/dist/capture/python/sitecustomize.py +832 -0
  23. package/dist/capture/schemas/capture.schema.d.ts +35 -0
  24. package/dist/capture/schemas/capture.schema.js +31 -0
  25. package/dist/capture/types/capture.types.d.ts +20 -0
  26. package/dist/capture/venv-loader.d.ts +68 -0
  27. package/dist/capture/venv-loader.js +189 -0
  28. package/dist/cli/constants/cli.constants.d.ts +23 -6
  29. package/dist/cli/constants/cli.constants.js +24 -7
  30. package/dist/cli/context.d.ts +19 -22
  31. package/dist/cli/context.js +23 -54
  32. package/dist/cli/doctor-run-capture.d.ts +21 -0
  33. package/dist/cli/doctor-run-capture.js +33 -0
  34. package/dist/cli/doctor.js +21 -68
  35. package/dist/cli/hook-check.d.ts +7 -2
  36. package/dist/cli/hook-check.js +30 -15
  37. package/dist/cli/hook.d.ts +14 -0
  38. package/dist/cli/hook.js +82 -1
  39. package/dist/cli/index.d.ts +1 -1
  40. package/dist/cli/index.js +1 -1
  41. package/dist/cli/misc.js +10 -2
  42. package/dist/cli/npm-global.d.ts +24 -0
  43. package/dist/cli/npm-global.js +77 -0
  44. package/dist/cli/npx-install.js +22 -6
  45. package/dist/cli/replay.d.ts +33 -0
  46. package/dist/cli/replay.js +228 -0
  47. package/dist/cli/setup.d.ts +10 -2
  48. package/dist/cli/setup.js +200 -31
  49. package/dist/cli/status.js +45 -5
  50. package/dist/cli/toggle.js +10 -0
  51. package/dist/cli/types/cli.types.d.ts +15 -1
  52. package/dist/cli/uninstall.js +42 -23
  53. package/dist/cli/update.js +3 -2
  54. package/dist/cli.js +104 -17
  55. package/dist/config/config-store.d.ts +15 -4
  56. package/dist/config/config-store.js +31 -7
  57. package/dist/config/config.d.ts +0 -2
  58. package/dist/config/config.js +0 -1
  59. package/dist/config/constants/config.constants.d.ts +8 -1
  60. package/dist/config/constants/config.constants.js +10 -1
  61. package/dist/config/events-transport.d.ts +15 -0
  62. package/dist/config/events-transport.js +29 -0
  63. package/dist/config/index.d.ts +2 -1
  64. package/dist/config/index.js +1 -0
  65. package/dist/config/root-config.d.ts +12 -0
  66. package/dist/config/root-config.js +24 -0
  67. package/dist/config/schemas/config.schema.d.ts +15 -2
  68. package/dist/config/schemas/config.schema.js +52 -8
  69. package/dist/config/types/config.types.d.ts +7 -0
  70. package/dist/core/index.d.ts +1 -1
  71. package/dist/core/index.js +1 -1
  72. package/dist/core/version.d.ts +9 -0
  73. package/dist/core/version.js +14 -0
  74. package/dist/core/which.d.ts +11 -0
  75. package/dist/core/which.js +18 -0
  76. package/dist/credentials/cloud-claim.d.ts +34 -0
  77. package/dist/credentials/cloud-claim.js +61 -0
  78. package/dist/credentials/constants/credentials.constants.d.ts +16 -1
  79. package/dist/credentials/constants/credentials.constants.js +17 -2
  80. package/dist/credentials/credential-status.js +1 -1
  81. package/dist/credentials/credentials.d.ts +3 -3
  82. package/dist/credentials/credentials.js +26 -12
  83. package/dist/credentials/device-api.d.ts +15 -1
  84. package/dist/credentials/device-api.js +12 -1
  85. package/dist/credentials/identity.d.ts +2 -2
  86. package/dist/credentials/identity.js +2 -2
  87. package/dist/credentials/install-key.d.ts +31 -0
  88. package/dist/credentials/install-key.js +58 -0
  89. package/dist/credentials/migration.d.ts +9 -7
  90. package/dist/credentials/migration.js +58 -18
  91. package/dist/credentials/secret-store.d.ts +12 -0
  92. package/dist/credentials/secret-store.js +18 -1
  93. package/dist/credentials/sign-in.d.ts +27 -0
  94. package/dist/credentials/sign-in.js +31 -1
  95. package/dist/credentials/token-refresher.d.ts +5 -1
  96. package/dist/credentials/token-refresher.js +10 -2
  97. package/dist/enforcement/enforcement.js +1 -1
  98. package/dist/events/constants/events.constants.d.ts +5 -2
  99. package/dist/events/constants/events.constants.js +4 -1
  100. package/dist/events/enrich.d.ts +14 -0
  101. package/dist/events/enrich.js +1 -1
  102. package/dist/events/index.d.ts +1 -0
  103. package/dist/events/product-event.d.ts +6 -3
  104. package/dist/events/types/events.types.d.ts +6 -0
  105. package/dist/events/types/replay-progress.types.d.ts +26 -0
  106. package/dist/events/types/replay-progress.types.js +1 -0
  107. package/dist/events/types/repo-tree.types.d.ts +2 -1
  108. package/dist/feature/ticket-candidates.d.ts +1 -1
  109. package/dist/git/checkout-root.d.ts +15 -0
  110. package/dist/git/checkout-root.js +18 -0
  111. package/dist/git/code-state.d.ts +2 -16
  112. package/dist/git/code-state.js +11 -12
  113. package/dist/git/constants/git.constants.d.ts +33 -11
  114. package/dist/git/constants/git.constants.js +45 -12
  115. package/dist/git/fingerprint.d.ts +3 -6
  116. package/dist/git/fingerprint.js +5 -8
  117. package/dist/git/git-context.js +5 -1
  118. package/dist/git/index.d.ts +1 -2
  119. package/dist/git/index.js +1 -1
  120. package/dist/git/repo-snapshot.d.ts +13 -4
  121. package/dist/git/repo-snapshot.js +53 -9
  122. package/dist/git/types/snapshot.types.d.ts +6 -0
  123. package/dist/pipeline/constants/pipeline.constants.d.ts +3 -0
  124. package/dist/pipeline/constants/pipeline.constants.js +3 -0
  125. package/dist/pipeline/hook-pipeline.js +82 -57
  126. package/dist/privacy/product-capture.js +2 -0
  127. package/dist/providers/antigravity/antigravity.capture.d.ts +2 -0
  128. package/dist/providers/antigravity/antigravity.capture.js +5 -0
  129. package/dist/providers/antigravity/antigravity.detect.js +8 -22
  130. package/dist/providers/antigravity/antigravity.provider.js +10 -3
  131. package/dist/providers/antigravity/constants/antigravity.constants.d.ts +1 -0
  132. package/dist/providers/antigravity/constants/antigravity.constants.js +1 -0
  133. package/dist/providers/claude/claude.adapter.js +9 -6
  134. package/dist/providers/claude/claude.detect.js +2 -17
  135. package/dist/providers/claude/claude.otel.d.ts +21 -3
  136. package/dist/providers/claude/claude.otel.js +216 -61
  137. package/dist/providers/claude/claude.provider.js +21 -1
  138. package/dist/providers/claude/claude.version.d.ts +9 -0
  139. package/dist/providers/claude/claude.version.js +28 -0
  140. package/dist/providers/claude/constants/claude.constants.d.ts +27 -0
  141. package/dist/providers/claude/constants/claude.constants.js +27 -0
  142. package/dist/providers/claude/constants/claude.otel.constants.d.ts +27 -0
  143. package/dist/providers/claude/constants/claude.otel.constants.js +45 -5
  144. package/dist/providers/claude/types/claude.types.d.ts +13 -0
  145. package/dist/providers/codex/codex.capture.d.ts +76 -0
  146. package/dist/providers/codex/codex.capture.js +454 -0
  147. package/dist/providers/codex/codex.detect.js +2 -4
  148. package/dist/providers/codex/codex.otel.d.ts +38 -2
  149. package/dist/providers/codex/codex.otel.js +50 -6
  150. package/dist/providers/codex/codex.provider.js +8 -0
  151. package/dist/providers/codex/codex.surface.d.ts +9 -0
  152. package/dist/{turns/codex-originator.js → providers/codex/codex.surface.js} +17 -3
  153. package/dist/providers/codex/codex.titles.d.ts +14 -0
  154. package/dist/providers/codex/codex.titles.js +35 -0
  155. package/dist/providers/codex/constants/codex.constants.d.ts +4 -0
  156. package/dist/providers/codex/constants/codex.constants.js +4 -0
  157. package/dist/providers/cursor/constants/cursor.constants.d.ts +17 -4
  158. package/dist/providers/cursor/constants/cursor.constants.js +25 -5
  159. package/dist/providers/cursor/cursor-mcp-memo.d.ts +34 -0
  160. package/dist/providers/cursor/cursor-mcp-memo.js +94 -0
  161. package/dist/providers/cursor/cursor.adapter.js +20 -82
  162. package/dist/providers/cursor/cursor.capture.d.ts +1 -0
  163. package/dist/providers/cursor/cursor.capture.js +4 -0
  164. package/dist/providers/cursor/cursor.detect.d.ts +5 -1
  165. package/dist/providers/cursor/cursor.detect.js +11 -26
  166. package/dist/providers/cursor/cursor.hooks.js +2 -2
  167. package/dist/providers/cursor/cursor.provider.js +16 -7
  168. package/dist/providers/cursor/cursor.titles.d.ts +14 -0
  169. package/dist/providers/cursor/cursor.titles.js +20 -0
  170. package/dist/providers/cursor/schemas/cursor.schema.d.ts +3 -3
  171. package/dist/providers/cursor/schemas/cursor.schema.js +5 -3
  172. package/dist/providers/gemini/constants/gemini.constants.d.ts +16 -0
  173. package/dist/providers/gemini/constants/gemini.constants.js +17 -0
  174. package/dist/providers/gemini/gemini.capture.d.ts +81 -0
  175. package/dist/providers/gemini/gemini.capture.js +210 -0
  176. package/dist/providers/gemini/gemini.detect.js +57 -24
  177. package/dist/providers/gemini/gemini.otel.d.ts +38 -2
  178. package/dist/providers/gemini/gemini.otel.js +56 -125
  179. package/dist/providers/gemini/gemini.provider.js +5 -0
  180. package/dist/providers/index.d.ts +1 -0
  181. package/dist/providers/index.js +1 -0
  182. package/dist/providers/provider.d.ts +8 -0
  183. package/dist/providers/provider.js +9 -0
  184. package/dist/providers/registry.d.ts +14 -0
  185. package/dist/providers/registry.js +14 -0
  186. package/dist/providers/shared/constants/hook-config.constants.d.ts +2 -0
  187. package/dist/providers/shared/constants/hook-config.constants.js +2 -0
  188. package/dist/providers/shared/constants/terminal-capture.constants.d.ts +6 -0
  189. package/dist/providers/shared/constants/terminal-capture.constants.js +6 -0
  190. package/dist/providers/shared/editor-paths.d.ts +8 -0
  191. package/dist/providers/shared/editor-paths.js +18 -0
  192. package/dist/providers/shared/hook-config.d.ts +5 -0
  193. package/dist/providers/shared/hook-config.js +39 -1
  194. package/dist/providers/shared/hooks-marker.d.ts +14 -0
  195. package/dist/providers/shared/hooks-marker.js +23 -0
  196. package/dist/providers/shared/install-record.d.ts +29 -0
  197. package/dist/providers/shared/install-record.js +52 -0
  198. package/dist/providers/shared/session-title-store.d.ts +23 -0
  199. package/dist/providers/shared/session-title-store.js +50 -0
  200. package/dist/providers/shared/terminal-capture.d.ts +41 -0
  201. package/dist/providers/shared/terminal-capture.js +109 -0
  202. package/dist/providers/shared/tooling.d.ts +21 -0
  203. package/dist/providers/shared/tooling.js +36 -1
  204. package/dist/providers/types/provider.types.d.ts +57 -4
  205. package/dist/replay/checkout-resolver.d.ts +56 -0
  206. package/dist/replay/checkout-resolver.js +173 -0
  207. package/dist/replay/codex-turns.d.ts +24 -0
  208. package/dist/replay/codex-turns.js +221 -0
  209. package/dist/replay/constants/replay.constants.d.ts +99 -0
  210. package/dist/replay/constants/replay.constants.js +102 -0
  211. package/dist/replay/describe.d.ts +10 -0
  212. package/dist/replay/describe.js +31 -0
  213. package/dist/replay/gemini-turns.d.ts +36 -0
  214. package/dist/replay/gemini-turns.js +192 -0
  215. package/dist/replay/machine.d.ts +34 -0
  216. package/dist/replay/machine.js +68 -0
  217. package/dist/replay/progress-event.d.ts +21 -0
  218. package/dist/replay/progress-event.js +45 -0
  219. package/dist/replay/replay-runner.d.ts +60 -0
  220. package/dist/replay/replay-runner.js +581 -0
  221. package/dist/replay/replay-scan.d.ts +84 -0
  222. package/dist/replay/replay-scan.js +182 -0
  223. package/dist/replay/replay-state.d.ts +108 -0
  224. package/dist/replay/replay-state.js +199 -0
  225. package/dist/replay/resume.d.ts +18 -0
  226. package/dist/replay/resume.js +34 -0
  227. package/dist/replay/start.d.ts +23 -0
  228. package/dist/replay/start.js +39 -0
  229. package/dist/replay/throttle.d.ts +36 -0
  230. package/dist/replay/throttle.js +44 -0
  231. package/dist/replay/transcript-turns.d.ts +35 -0
  232. package/dist/replay/transcript-turns.js +241 -0
  233. package/dist/replay/turn-event.d.ts +40 -0
  234. package/dist/replay/turn-event.js +127 -0
  235. package/dist/replay/types/replay.types.d.ts +198 -0
  236. package/dist/replay/types/replay.types.js +1 -0
  237. package/dist/replay/window.d.ts +28 -0
  238. package/dist/replay/window.js +41 -0
  239. package/dist/replay/worktree-map.d.ts +42 -0
  240. package/dist/replay/worktree-map.js +134 -0
  241. package/dist/snapshot/branch-selection.d.ts +40 -23
  242. package/dist/snapshot/branch-selection.js +71 -41
  243. package/dist/snapshot/constants/snapshot-stages.constants.d.ts +3 -1
  244. package/dist/snapshot/constants/snapshot-stages.constants.js +3 -1
  245. package/dist/snapshot/constants/snapshot.constants.d.ts +34 -7
  246. package/dist/snapshot/constants/snapshot.constants.js +34 -7
  247. package/dist/snapshot/index.d.ts +2 -2
  248. package/dist/snapshot/index.js +1 -1
  249. package/dist/snapshot/snapshot-pipeline.js +100 -26
  250. package/dist/snapshot/snapshot-state.js +6 -2
  251. package/dist/snapshot/tree-pipeline.d.ts +1 -1
  252. package/dist/snapshot/tree-pipeline.js +34 -15
  253. package/dist/snapshot/types/snapshot.types.d.ts +22 -3
  254. package/dist/storage/schemas/storage.schema.d.ts +2 -0
  255. package/dist/storage/schemas/storage.schema.js +6 -0
  256. package/dist/transport/delivery-stats.d.ts +13 -1
  257. package/dist/transport/delivery-stats.js +18 -2
  258. package/dist/transport/delivery.d.ts +4 -3
  259. package/dist/transport/delivery.js +21 -7
  260. package/dist/transport/identity-delivery.d.ts +20 -0
  261. package/dist/transport/identity-delivery.js +41 -0
  262. package/dist/transport/index.d.ts +2 -0
  263. package/dist/transport/index.js +1 -0
  264. package/dist/transport/types/delivery-flow.types.d.ts +7 -0
  265. package/dist/turns/claude-transcript.js +4 -12
  266. package/dist/turns/codex-transcript.d.ts +9 -0
  267. package/dist/turns/codex-transcript.js +27 -19
  268. package/dist/turns/constants/turns.constants.d.ts +29 -12
  269. package/dist/turns/constants/turns.constants.js +31 -19
  270. package/dist/turns/gemini-transcript.d.ts +19 -1
  271. package/dist/turns/gemini-transcript.js +29 -18
  272. package/dist/turns/index.d.ts +3 -4
  273. package/dist/turns/index.js +2 -3
  274. package/dist/turns/run-state.d.ts +71 -0
  275. package/dist/turns/run-state.js +114 -0
  276. package/dist/turns/tool-events.d.ts +11 -1
  277. package/dist/turns/tool-events.js +32 -9
  278. package/dist/turns/transcript.d.ts +15 -14
  279. package/dist/turns/transcript.js +23 -4
  280. package/dist/turns/turn-state.d.ts +82 -7
  281. package/dist/turns/turn-state.js +157 -10
  282. package/dist/turns/turn-summary.js +7 -2
  283. package/dist/turns/turn-tracker.d.ts +9 -16
  284. package/dist/turns/turn-tracker.js +163 -87
  285. package/dist/turns/types/transcript.types.d.ts +11 -1
  286. package/dist/turns/types/turn-provider.types.d.ts +33 -0
  287. package/dist/turns/types/turn-provider.types.js +1 -0
  288. package/dist/turns/types/turn-state.types.d.ts +9 -0
  289. package/dist/turns/types/turn-summary.types.d.ts +17 -1
  290. package/dist/turns/types/turn-tracker.types.d.ts +3 -1
  291. package/dist/turns/work-checkout.js +1 -2
  292. package/docs/AUDIT_PROMPT.md +10 -0
  293. package/docs/DATA_HANDLING.md +201 -13
  294. package/docs/ENTERPRISE_DEPLOYMENT.md +3 -3
  295. package/docs/PERFORMANCE.md +10 -0
  296. package/package.json +3 -2
  297. package/dist/events/canonical-event.d.ts +0 -10
  298. package/dist/events/canonical-event.js +0 -9
  299. package/dist/git/repository-root.d.ts +0 -39
  300. package/dist/git/repository-root.js +0 -131
  301. package/dist/git/types/repository-root.types.d.ts +0 -11
  302. package/dist/transport/transport.d.ts +0 -6
  303. package/dist/turns/codex-originator.d.ts +0 -11
  304. package/dist/turns/cursor-transcript.d.ts +0 -21
  305. package/dist/turns/cursor-transcript.js +0 -66
  306. package/dist/turns/native-session-titles.d.ts +0 -26
  307. package/dist/turns/native-session-titles.js +0 -102
  308. /package/dist/{git/types/repository-root.types.js → billing/types/billing.types.js} +0 -0
  309. /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 { findExecutable } from '../../core/which.js';
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 evidence = [];
47
- if (fs.existsSync(geminiHome(env)))
48
- evidence.push('~/.gemini directory exists');
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: await hasOurHooks(hookConfigPath)
60
+ hooksInstalled: hasOurHooks(settingsRecord(read))
63
61
  };
64
62
  }
65
63
  /**
66
- * The first of Gemini's executable names found on PATH.
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 env - Environment supplying PATH.
69
- * @returns The path, or undefined when neither is installed.
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 findFirstExecutable(env) {
72
- for (const name of GEMINI_EXECUTABLES) {
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 settingsPath - Gemini settings file.
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
- async function hasOurHooks(settingsPath) {
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 telemetry configuration into Gemini CLI's `.env`.
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
- * Remove the telemetry configuration AgentWatch wrote.
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, GEMINI_HOME_DIR, GEMINI_PROVIDER_ID } from './constants/gemini.constants.js';
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, RE_QUOTED_VALUE, STANDARD_OTLP_PROTOCOL, TELEMETRY_BLOCK_KEY } from './constants/gemini.otel.constants.js';
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 telemetry configuration into Gemini CLI's `.env`.
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. A
211
- // key already ours keeps what was recorded when it was taken.
212
- const owned = new Set(ownedKeys);
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 uninstall(context) {
292
+ async removeExport(context) {
256
293
  const owned = ownedKeysOf(context);
257
- const priors = priorValuesOf(context);
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, parsePrior(priors[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.uninstall(context);
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
- }