@mainwave/cli 0.13.0 → 0.14.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.
@@ -207,7 +207,14 @@ export const handle = async (payload, { argv = process.argv, emit = emitMarker }
207
207
  const tool = markerTool(argv)
208
208
  const managedDir = markerManagedDir(argv)
209
209
  const env = markerSessionEnv(tool, payload)
210
- const { endpoint, bearer, resourceAttrs } = loadOtelConfig(projectDir, env, tool, managedDir)
210
+ const { endpoint, bearer, resourceAttrs } = loadOtelConfig(
211
+ projectDir,
212
+ env,
213
+ tool,
214
+ managedDir,
215
+ {},
216
+ argv,
217
+ )
211
218
  log(
212
219
  `pr=${number} endpoint=${endpoint ? 'set' : 'MISSING'} ` +
213
220
  `bearer=${bearer ? 'set' : 'MISSING'} ucid=${resourceAttrs['mainwave.ucid'] || 'MISSING'}`,
@@ -711,7 +711,14 @@ export const handle = async (payload, { argv = process.argv, emit = emitMarker }
711
711
  const tool = markerTool(argv)
712
712
  const managedDir = markerManagedDir(argv)
713
713
  const env = markerSessionEnv(tool, payload)
714
- const { endpoint, bearer, resourceAttrs } = loadOtelConfig(projectDir, env, tool, managedDir)
714
+ const { endpoint, bearer, resourceAttrs } = loadOtelConfig(
715
+ projectDir,
716
+ env,
717
+ tool,
718
+ managedDir,
719
+ {},
720
+ argv,
721
+ )
715
722
  log(
716
723
  `mr=${mr.iid} endpoint=${endpoint ? 'set' : 'MISSING'} ` +
717
724
  `bearer=${bearer ? 'set' : 'MISSING'} ucid=${resourceAttrs['mainwave.ucid'] || 'MISSING'}`,
@@ -218,7 +218,14 @@ export const handle = async (payload, { argv = process.argv, emit = emitMarker }
218
218
  const tool = markerTool(argv)
219
219
  const managedDir = markerManagedDir(argv)
220
220
  const env = markerSessionEnv(tool, payload)
221
- const { endpoint, bearer, resourceAttrs } = loadOtelConfig(projectDir, env, tool, managedDir)
221
+ const { endpoint, bearer, resourceAttrs } = loadOtelConfig(
222
+ projectDir,
223
+ env,
224
+ tool,
225
+ managedDir,
226
+ {},
227
+ argv,
228
+ )
222
229
  log(
223
230
  `issue=${issueKey} op=${op} endpoint=${endpoint ? 'set' : 'MISSING'} ` +
224
231
  `bearer=${bearer ? 'set' : 'MISSING'} ucid=${resourceAttrs['mainwave.ucid'] || 'MISSING'}`,
@@ -264,7 +264,14 @@ export const handle = async (payload, { argv = process.argv, emit = emitMarker }
264
264
  const tool = markerTool(argv)
265
265
  const managedDir = markerManagedDir(argv)
266
266
  const env = markerSessionEnv(tool, payload)
267
- const { endpoint, bearer, resourceAttrs } = loadOtelConfig(projectDir, env, tool, managedDir)
267
+ const { endpoint, bearer, resourceAttrs } = loadOtelConfig(
268
+ projectDir,
269
+ env,
270
+ tool,
271
+ managedDir,
272
+ {},
273
+ argv,
274
+ )
268
275
  log(
269
276
  `issue=${issueKey} op=${op} endpoint=${endpoint ? 'set' : 'MISSING'} ` +
270
277
  `bearer=${bearer ? 'set' : 'MISSING'} ucid=${resourceAttrs['mainwave.ucid'] || 'MISSING'}`,
@@ -17,6 +17,7 @@ import {
17
17
  mkdirSync,
18
18
  readFileSync,
19
19
  readdirSync,
20
+ realpathSync,
20
21
  renameSync,
21
22
  statSync,
22
23
  writeFileSync,
@@ -785,18 +786,61 @@ export const markerOauthAccountPath = (tool, opts = {}) => {
785
786
  return claudeAccountPath(opts.home, configDir)
786
787
  }
787
788
 
788
- /** Last `key = "value"` match in TOML-ish text; last wins within the file. The key may
789
- * be bare (`X-Mainwave-UCID = …`) or quoted (`"X-Mainwave-UCID" = …`), covering
790
- * either TOML rendering. */
791
- const lastTomlString = (text, key) => {
792
- const escaped = key.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')
793
- const re = new RegExp(`"?${escaped}"?\\s*=\\s*"([^"]*)"`, 'g')
794
- let m
795
- let last
796
- while ((m = re.exec(text)) !== null) last = m[1]
797
- return last
789
+ /** A TOML key with its quotes and surrounding whitespace removed, so
790
+ * `otel.exporter."otlp-http"` and `otel.exporter.otlp-http` compare equal. */
791
+ const plainTomlKey = (key) =>
792
+ key
793
+ .replace(/"/g, '')
794
+ .replace(/\s*\.\s*/g, '.')
795
+ .trim()
796
+
797
+ const TOML_HEADER_START_RE = /^\s*\[/
798
+ const TOML_TABLE_RE = /^\s*\[([^[\]]+)\]\s*(?:#.*)?$/
799
+ const TOML_STRING_RE = /^\s*("[^"]+"|[A-Za-z0-9_-]+)\s*=\s*"([^"]*)"/
800
+ const TOML_INLINE_RE = /^\s*headers\s*=\s*\{(.*)\}\s*(?:#.*)?$/
801
+ const TOML_PAIR_RE = /("[^"]+"|[A-Za-z0-9_-]+)\s*=\s*"([^"]*)"/g
802
+
803
+ /**
804
+ * The `[otel.*]` tables of a Codex `config.toml`, in file order, each as
805
+ * `{ endpoint, headers }`: its `endpoint = "…"` string and its one-line
806
+ * inline `headers = { … }` table, the shape setup and the manual-setup page
807
+ * write. Only the string values the markers read are kept; anything else in
808
+ * the file is skipped, so an unexpected shape yields no table, never a
809
+ * guess.
810
+ */
811
+ const codexOtelTables = (text) => {
812
+ const tables = []
813
+ let current
814
+ for (const line of (text ?? '').split(/\r?\n/)) {
815
+ if (TOML_HEADER_START_RE.test(line)) {
816
+ // Any header, `[t]` or `[[t]]`, ends the table before it. Only a
817
+ // single-bracket `otel.*` header opens a new one.
818
+ const header = TOML_TABLE_RE.exec(line)
819
+ const name = header && plainTomlKey(header[1])
820
+ current = name?.startsWith('otel.') ? { endpoint: undefined, headers: {} } : undefined
821
+ if (current) tables.push(current)
822
+ continue
823
+ }
824
+ if (!current) continue
825
+ const inline = TOML_INLINE_RE.exec(line)
826
+ if (inline) {
827
+ for (const [, k, v] of inline[1].matchAll(TOML_PAIR_RE)) current.headers[plainTomlKey(k)] = v
828
+ continue
829
+ }
830
+ const pair = TOML_STRING_RE.exec(line)
831
+ if (pair && plainTomlKey(pair[1]) === 'endpoint') current.endpoint = pair[2]
832
+ }
833
+ return tables
798
834
  }
799
835
 
836
+ /** Whether an exporter table is one Mainwave setup wrote: it names an
837
+ * endpoint and an `Authorization` header, and its headers carry the
838
+ * `X-Mainwave-Source` tag setup (and the manual-setup snippet) puts in every
839
+ * Codex exporter table. The collector host can't decide it: an organization
840
+ * may run its collector on its own domain. */
841
+ const isMainwaveExporter = (table) =>
842
+ Boolean(table.endpoint && table.headers.Authorization && table.headers['X-Mainwave-Source'])
843
+
800
844
  /** Strip a per-signal OTLP suffix (`/v1/logs` …) back to the base endpoint the
801
845
  * emit re-adds `/v1/traces` to. Codex writes per-signal endpoints; Claude Code's
802
846
  * OTEL_EXPORTER_OTLP_ENDPOINT is already the base, so this is Codex-only. */
@@ -804,7 +848,46 @@ const stripSignalSuffix = (endpoint) =>
804
848
  (endpoint ?? '').replace(/\/v1\/(logs|traces|metrics)\/?$/, '')
805
849
 
806
850
  /**
807
- * OTLP config from Codex's global `config.toml`. Codex carries
851
+ * Codex's user-level config directory: `$CODEX_HOME` when set and non-empty,
852
+ * `~/.codex` otherwise, as Codex resolves it. A hook runs with the environment
853
+ * of the Codex process that started it, so it reads the config that Codex
854
+ * read. The CLI adapter resolves it by the same rule.
855
+ */
856
+ export const codexHomeDir = (env = process.env) => {
857
+ const dir = env.CODEX_HOME
858
+ if (dir === undefined || dir === '') return join(homedir(), '.codex')
859
+ try {
860
+ return realpathSync(resolve(dir))
861
+ } catch {
862
+ return resolve(dir)
863
+ }
864
+ }
865
+
866
+ /** A Codex profile name a hook command may pass: the characters setup allows. */
867
+ const CODEX_PROFILE_RE = /^[A-Za-z0-9_-]{1,64}$/
868
+
869
+ /**
870
+ * The one Codex config file a hook reads its collector from. Without
871
+ * `--codex-profile` it is `config.toml` in the Codex home. With
872
+ * `--codex-profile <name>` it is `<name>.config.toml` there, the file Codex
873
+ * loads only under `--profile <name>`: nothing in the hook's environment or
874
+ * input names the active profile, so the hook command carries it. A missing or
875
+ * unsafe name gives `undefined` (send nothing), never a path built from it and
876
+ * never a fall back to `config.toml`, whose collector belongs to sessions run
877
+ * without that profile.
878
+ */
879
+ export const codexConfigFile = (argv, env = process.env) => {
880
+ const i = (argv ?? []).indexOf('--codex-profile')
881
+ if (i === -1) return join(codexHomeDir(env), 'config.toml')
882
+ const name = argv[i + 1]
883
+ return typeof name === 'string' && CODEX_PROFILE_RE.test(name)
884
+ ? join(codexHomeDir(env), `${name}.config.toml`)
885
+ : undefined
886
+ }
887
+
888
+ /**
889
+ * OTLP config from the hook's Codex config file (§ `codexConfigFile`): the
890
+ * global `config.toml`, or the profile file the hook command names. Codex carries
808
891
  * tenancy on HTTP headers rather than OTEL_RESOURCE_ATTRIBUTES, so the UCID is the
809
892
  * `X-Mainwave-UCID` header value and the bearer is the `Authorization` header — the
810
893
  * marker re-emits the UCID as a `mainwave.ucid` resource attr, which the collector
@@ -812,18 +895,25 @@ const stripSignalSuffix = (endpoint) =>
812
895
  * developer configured one explicitly; by default the collector derives it from
813
896
  * the developer's email, so the `X-Mainwave-User-Email` header is re-emitted as
814
897
  * the `mainwave.user_email` resource attr it promotes that header to — without
815
- * it a Codex marker would carry no identity at all. Missing keys → undefined
816
- * (the hook skips, fail-open), never a broken emit.
898
+ * it a Codex marker would carry no identity at all.
899
+ *
900
+ * Every value comes from one exporter table, the last that is Mainwave's
901
+ * (§ `isMainwaveExporter`). The `[otel]` block may belong to another
902
+ * vendor, and its endpoint and bearer must never receive a Mainwave span,
903
+ * so with no Mainwave table every field is undefined and the hook skips
904
+ * (fail-open), never a broken emit.
817
905
  */
818
- export const codexOtelConfig = (readFile = readFileSafe) => {
819
- // Global config only: Codex 0.130+ drops `otel` keys from a project's
906
+ export const codexOtelConfig = (readFile = readFileSafe, env = process.env, argv = []) => {
907
+ // User-level config only: Codex 0.130+ drops `otel` keys from a project's
820
908
  // `.codex/config.toml` itself, so a checked-out repository can't redirect
821
909
  // its telemetry, and the markers follow the same rule.
822
- const text = readFile(join(homedir(), '.codex', 'config.toml'))
823
- const endpoint = stripSignalSuffix(lastTomlString(text, 'endpoint'))
824
- const bearer = lastTomlString(text, 'Authorization')
825
- const ucid = lastTomlString(text, 'X-Mainwave-UCID')
826
- const userEmail = lastTomlString(text, 'X-Mainwave-User-Email')
910
+ const file = codexConfigFile(argv, env)
911
+ const text = file === undefined ? '' : readFile(file)
912
+ const table = codexOtelTables(text).filter(isMainwaveExporter).at(-1)
913
+ const endpoint = stripSignalSuffix(table?.endpoint)
914
+ const bearer = table?.headers.Authorization
915
+ const ucid = table?.headers['X-Mainwave-UCID']
916
+ const userEmail = table?.headers['X-Mainwave-User-Email']
827
917
  return {
828
918
  endpoint: endpoint || undefined,
829
919
  bearer: bearer || undefined,
@@ -835,11 +925,19 @@ export const codexOtelConfig = (readFile = readFileSafe) => {
835
925
  }
836
926
 
837
927
  /** Resolve OTLP config for the marker emit. `tool` selects the source format:
838
- * 'codex' reads config.toml; the default reads Claude Code's settings.json env.
928
+ * 'codex' reads the Codex config file the hook's `argv` selects (§
929
+ * `codexConfigFile`); the default reads Claude Code's settings.json env.
839
930
  * `readers` (§ `onceReaders`) lets a caller share its file reads. */
840
- export const loadOtelConfig = (cwd, processEnv, tool = 'claude-code', managedDir, readers = {}) =>
931
+ export const loadOtelConfig = (
932
+ cwd,
933
+ processEnv,
934
+ tool = 'claude-code',
935
+ managedDir,
936
+ readers = {},
937
+ argv = [],
938
+ ) =>
841
939
  tool === 'codex'
842
- ? codexOtelConfig()
940
+ ? codexOtelConfig(undefined, undefined, argv)
843
941
  : resolveOtelConfig(
844
942
  settingsLayers(cwd, processEnv, readers.readFile, readers.listDir, managedDir),
845
943
  processEnv,
@@ -1309,6 +1407,7 @@ export const sendClaudeEvent = async ({
1309
1407
  tool,
1310
1408
  managedDir,
1311
1409
  readers,
1410
+ argv,
1312
1411
  )
1313
1412
  if (!endpoint || !bearer) {
1314
1413
  log(`skip ${label}: collector endpoint/bearer not found in settings or env`)
@@ -499,6 +499,7 @@ export const handle = async (payload, { argv = process.argv, emit = emitMarker }
499
499
  tool,
500
500
  managedDir,
501
501
  readers,
502
+ argv,
502
503
  )
503
504
  if (!endpoint || !bearer) {
504
505
  log('skip: collector endpoint/bearer not found in settings or env')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mainwave/cli",
3
- "version": "0.13.0",
3
+ "version": "0.14.0",
4
4
  "description": "Setup wizard for connecting third-party coding tools to Mainwave AI.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -25,7 +25,10 @@
25
25
  "pnpm": {
26
26
  "onlyBuiltDependencies": [
27
27
  "lefthook"
28
- ]
28
+ ],
29
+ "overrides": {
30
+ "js-yaml@>=4.0.0 <4.3.2": "^4.3.2"
31
+ }
29
32
  },
30
33
  "scripts": {
31
34
  "build": "tsc -p tsconfig.build.json",
@@ -42,19 +45,19 @@
42
45
  "api:generate": "openapi-ts"
43
46
  },
44
47
  "dependencies": {
45
- "@inquirer/prompts": "8.7.3",
48
+ "@inquirer/prompts": "8.7.5",
46
49
  "picocolors": "1.1.1"
47
50
  },
48
51
  "devDependencies": {
49
52
  "@hey-api/openapi-ts": "^0.99.0",
50
53
  "@types/node": "22.20.5",
51
- "knip": "6.41.0",
52
- "lefthook": "2.2.1",
53
- "oxlint": "1.87.0",
54
+ "knip": "6.45.0",
55
+ "lefthook": "2.2.2",
56
+ "oxlint": "1.93.0",
54
57
  "prettier": "3.9.10",
55
58
  "typescript": "6.0.3",
56
- "vite": "8.3.4",
57
- "vitest": "5.0.3",
59
+ "vite": "8.3.7",
60
+ "vitest": "5.1.0",
58
61
  "yaml": "2.9.1"
59
62
  },
60
63
  "keywords": [