@mainwave/cli 0.13.1 → 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.
@@ -786,18 +786,61 @@ export const markerOauthAccountPath = (tool, opts = {}) => {
786
786
  return claudeAccountPath(opts.home, configDir)
787
787
  }
788
788
 
789
- /** Last `key = "value"` match in TOML-ish text; last wins within the file. The key may
790
- * be bare (`X-Mainwave-UCID = …`) or quoted (`"X-Mainwave-UCID" = …`), covering
791
- * either TOML rendering. */
792
- const lastTomlString = (text, key) => {
793
- const escaped = key.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')
794
- const re = new RegExp(`"?${escaped}"?\\s*=\\s*"([^"]*)"`, 'g')
795
- let m
796
- let last
797
- while ((m = re.exec(text)) !== null) last = m[1]
798
- 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
799
834
  }
800
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
+
801
844
  /** Strip a per-signal OTLP suffix (`/v1/logs` …) back to the base endpoint the
802
845
  * emit re-adds `/v1/traces` to. Codex writes per-signal endpoints; Claude Code's
803
846
  * OTEL_EXPORTER_OTLP_ENDPOINT is already the base, so this is Codex-only. */
@@ -820,8 +863,31 @@ export const codexHomeDir = (env = process.env) => {
820
863
  }
821
864
  }
822
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
+
823
869
  /**
824
- * OTLP config from Codex's global `config.toml`. Codex carries
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
825
891
  * tenancy on HTTP headers rather than OTEL_RESOURCE_ATTRIBUTES, so the UCID is the
826
892
  * `X-Mainwave-UCID` header value and the bearer is the `Authorization` header — the
827
893
  * marker re-emits the UCID as a `mainwave.ucid` resource attr, which the collector
@@ -829,18 +895,25 @@ export const codexHomeDir = (env = process.env) => {
829
895
  * developer configured one explicitly; by default the collector derives it from
830
896
  * the developer's email, so the `X-Mainwave-User-Email` header is re-emitted as
831
897
  * the `mainwave.user_email` resource attr it promotes that header to — without
832
- * it a Codex marker would carry no identity at all. Missing keys → undefined
833
- * (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.
834
905
  */
835
- export const codexOtelConfig = (readFile = readFileSafe, env = process.env) => {
836
- // 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
837
908
  // `.codex/config.toml` itself, so a checked-out repository can't redirect
838
909
  // its telemetry, and the markers follow the same rule.
839
- const text = readFile(join(codexHomeDir(env), 'config.toml'))
840
- const endpoint = stripSignalSuffix(lastTomlString(text, 'endpoint'))
841
- const bearer = lastTomlString(text, 'Authorization')
842
- const ucid = lastTomlString(text, 'X-Mainwave-UCID')
843
- 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']
844
917
  return {
845
918
  endpoint: endpoint || undefined,
846
919
  bearer: bearer || undefined,
@@ -852,11 +925,19 @@ export const codexOtelConfig = (readFile = readFileSafe, env = process.env) => {
852
925
  }
853
926
 
854
927
  /** Resolve OTLP config for the marker emit. `tool` selects the source format:
855
- * '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.
856
930
  * `readers` (§ `onceReaders`) lets a caller share its file reads. */
857
- 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
+ ) =>
858
939
  tool === 'codex'
859
- ? codexOtelConfig()
940
+ ? codexOtelConfig(undefined, undefined, argv)
860
941
  : resolveOtelConfig(
861
942
  settingsLayers(cwd, processEnv, readers.readFile, readers.listDir, managedDir),
862
943
  processEnv,
@@ -1326,6 +1407,7 @@ export const sendClaudeEvent = async ({
1326
1407
  tool,
1327
1408
  managedDir,
1328
1409
  readers,
1410
+ argv,
1329
1411
  )
1330
1412
  if (!endpoint || !bearer) {
1331
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.1",
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": {
@@ -45,19 +45,19 @@
45
45
  "api:generate": "openapi-ts"
46
46
  },
47
47
  "dependencies": {
48
- "@inquirer/prompts": "8.7.3",
48
+ "@inquirer/prompts": "8.7.5",
49
49
  "picocolors": "1.1.1"
50
50
  },
51
51
  "devDependencies": {
52
52
  "@hey-api/openapi-ts": "^0.99.0",
53
53
  "@types/node": "22.20.5",
54
- "knip": "6.41.0",
55
- "lefthook": "2.2.1",
56
- "oxlint": "1.87.0",
54
+ "knip": "6.45.0",
55
+ "lefthook": "2.2.2",
56
+ "oxlint": "1.93.0",
57
57
  "prettier": "3.9.10",
58
58
  "typescript": "6.0.3",
59
- "vite": "8.3.4",
60
- "vitest": "5.0.3",
59
+ "vite": "8.3.7",
60
+ "vitest": "5.1.0",
61
61
  "yaml": "2.9.1"
62
62
  },
63
63
  "keywords": [