@mainwave/cli 0.11.0 → 0.13.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.
Files changed (43) hide show
  1. package/README.md +203 -16
  2. package/dist/adapters/claude-code.js +3 -3
  3. package/dist/adapters/claude-desktop.js +35 -1
  4. package/dist/adapters/codex-cli.js +16 -16
  5. package/dist/adapters/cursor.js +7 -2
  6. package/dist/adapters/kiro.js +13 -8
  7. package/dist/commands.js +219 -0
  8. package/dist/core/apply.js +47 -8
  9. package/dist/core/auth-commands.js +2 -1
  10. package/dist/core/codecs/kiro-hooks-json.js +1 -1
  11. package/dist/core/codex-trust.js +178 -0
  12. package/dist/core/doctor.js +144 -12
  13. package/dist/core/entry-script.js +2 -3
  14. package/dist/core/format.js +0 -1
  15. package/dist/core/hook-copies.js +157 -0
  16. package/dist/core/install-hooks.js +198 -15
  17. package/dist/core/interview.js +36 -16
  18. package/dist/core/markers.js +209 -44
  19. package/dist/core/redaction.js +188 -0
  20. package/dist/core/skills.js +3 -11
  21. package/dist/core/status.js +21 -1
  22. package/dist/core/store.js +254 -4
  23. package/dist/core/wizard.js +91 -6
  24. package/dist/main.js +173 -163
  25. package/hooks/codex-turns.mjs +584 -0
  26. package/hooks/kiro-otlp.mjs +10 -1
  27. package/hooks/mainwave-hook.mjs +27 -3
  28. package/hooks/marker-core.mjs +66 -8
  29. package/hooks/matchers.json +98 -1
  30. package/hooks/redaction-data.mjs +209 -0
  31. package/hooks/redaction-json.mjs +215 -0
  32. package/hooks/redaction-pattern.mjs +503 -0
  33. package/hooks/redaction.d.mts +84 -0
  34. package/hooks/redaction.mjs +701 -0
  35. package/hooks/vocabulary.mjs +41 -2
  36. package/package.json +3 -1
  37. package/skills/mainwave-instrument/SKILL.md +24 -6
  38. package/skills/mainwave-instrument/reference/frameworks.md +4 -2
  39. package/skills/mainwave-instrument/reference/troubleshooting.md +4 -0
  40. package/skills/mainwave-instrument-node/SKILL.md +83 -38
  41. package/skills/mainwave-instrument-node/reference/api.md +16 -16
  42. package/skills/mainwave-instrument-node/reference/frameworks.md +42 -27
  43. package/skills/mainwave-instrument-node/reference/troubleshooting.md +11 -7
@@ -25,6 +25,7 @@ import { homedir } from 'node:os'
25
25
  import { basename, dirname, isAbsolute, join, resolve } from 'node:path'
26
26
  import { fileURLToPath } from 'node:url'
27
27
 
28
+ import { loadHookRedaction, redactOtlpTraces } from './redaction.mjs'
28
29
  import {
29
30
  ATTR_CALLER_SPAN_ID,
30
31
  ATTR_CALLER_TRACE_ID,
@@ -990,15 +991,19 @@ const opaqueIdOrUndefined = (v) => {
990
991
  * - `toolCallId` — the payload's `tool_use_id`, the value Claude Code's own
991
992
  * tool span carries as `gen_ai.tool.call.id`.
992
993
  * - `subagentId` — the payload's `agent_id`, present only when the hook runs
993
- * inside a subagent; Claude Code's own spans carry it as `agent_id`.
994
+ * inside a subagent; Claude Code's own spans carry it as `agent_id`. Codex
995
+ * sends the same field (the child's own conversation id), so a Codex-hosted
996
+ * marker carries it too.
994
997
  *
995
- * Claude Code only (`tool`, § `markerTool`): another host's hook environment is
996
- * inherited from the developer's shell, where a `TRACEPARENT` would name some
997
- * unrelated trace, and no other host's payload fields are known to mean the
998
- * same thing. Each field is read on its own, so a missing or malformed one
999
- * drops only itself.
998
+ * `caller` and `toolCallId` are Claude Code only (`tool`, § `markerTool`): a
999
+ * Codex hook's environment is inherited from the developer's shell, where a
1000
+ * `TRACEPARENT` would name some unrelated trace, and Codex's `tool_use_id` is
1001
+ * not verified to equal the call id its own spans carry. Any other host yields
1002
+ * nothing. Each field is read on its own, so a missing or malformed one drops
1003
+ * only itself.
1000
1004
  */
1001
1005
  export const markerCallContext = (tool, payload, env = process.env) => {
1006
+ if (tool === 'codex') return { subagentId: opaqueIdOrUndefined(payload?.agent_id) }
1002
1007
  if (tool !== 'claude-code') return {}
1003
1008
  return {
1004
1009
  caller: parseTraceparent(env.TRACEPARENT),
@@ -1142,13 +1147,57 @@ export const buildMarkerPayload = ({
1142
1147
  ],
1143
1148
  })
1144
1149
 
1150
+ // The redaction rules this process runs, loaded on first use, per Claude
1151
+ // config dir ('' for a hook that can't tell its own): a dispatcher run's
1152
+ // features (and the Kiro sender) share one read of the settings and the rules
1153
+ // file.
1154
+ const hookRedaction = new Map()
1155
+
1156
+ /** Forget the loaded rules so the next send reads them again.
1157
+ * @internal
1158
+ */
1159
+ export const resetHookRedaction = () => {
1160
+ hookRedaction.clear()
1161
+ }
1162
+
1163
+ /**
1164
+ * The body to send, or `undefined` when it must not leave the machine. The
1165
+ * one place redaction happens: every hook's POST goes through `emitMarker`,
1166
+ * and Kiro's spool write through here too, so a send path cannot skip it.
1167
+ * `configDir` is the Claude config dir the hook runs for, which picks the
1168
+ * organization packs it applies; a hook that can't tell (Cursor's, Kiro's,
1169
+ * Codex's) passes none and applies every recorded dir's. `applied` is whether
1170
+ * rules ran. The `secrets` pack runs unless the developer turned it off (and
1171
+ * recorded no rules file, and no organization packs are recorded), in which
1172
+ * case the body is returned as it is; with rules that cannot be loaded or
1173
+ * run, nothing is sent and one line naming the reason (never span content)
1174
+ * goes to `log`: redaction that was asked for does not silently turn off.
1175
+ */
1176
+ export const redactForSend = (body, label, log, { configDir } = {}) => {
1177
+ const key = configDir ?? ''
1178
+ if (!hookRedaction.has(key)) hookRedaction.set(key, loadHookRedaction({ configDir }))
1179
+ const loaded = hookRedaction.get(key)
1180
+ if (loaded.state === 'off') return { body, applied: false }
1181
+ if (loaded.state === 'error') {
1182
+ log(`redaction unavailable (${loaded.reason}); not sending ${label}`)
1183
+ return undefined
1184
+ }
1185
+ try {
1186
+ return { body: redactOtlpTraces(loaded.redactor, body), applied: true }
1187
+ } catch (e) {
1188
+ log(`redaction failed (${e?.name ?? 'Error'}); not sending ${label}`)
1189
+ return undefined
1190
+ }
1191
+ }
1192
+
1145
1193
  /** Whether an HTTP status is a 2xx. */
1146
1194
  export const isOkStatus = (status) => Number.isInteger(status) && status >= 200 && status < 300
1147
1195
 
1148
1196
  /**
1149
1197
  * POST the OTLP body to the collector, failing open (any error is swallowed).
1150
1198
  * Resolves the response's HTTP status, or `undefined` when no response came
1151
- * (network error, abort at `timeoutMs`). Every outcome is logged, except a
1199
+ * (network error, abort at `timeoutMs`), and also when § redactForSend holds
1200
+ * the body back. Every outcome is logged, except a
1152
1201
  * 2xx when `logOk` is false — a caller that sends on every prompt keeps its
1153
1202
  * log to what someone diagnoses and records success itself from the status.
1154
1203
  */
@@ -1160,14 +1209,23 @@ export const emitMarker = async ({
1160
1209
  log,
1161
1210
  timeoutMs = EMIT_TIMEOUT_MS,
1162
1211
  logOk = true,
1212
+ redacted = false,
1213
+ redactionConfigDir,
1163
1214
  }) => {
1215
+ // `redacted`: the body already went through § redactForSend (a spooled one).
1216
+ // `redactionConfigDir`: the session's Claude config dir, which the
1217
+ // dispatcher passes for a Claude Code hook.
1218
+ const out = redacted
1219
+ ? { body }
1220
+ : redactForSend(body, label, log, { configDir: redactionConfigDir })
1221
+ if (out === undefined) return undefined
1164
1222
  const controller = new AbortController()
1165
1223
  const timer = setTimeout(() => controller.abort(), timeoutMs)
1166
1224
  try {
1167
1225
  const res = await fetch(`${endpoint.replace(/\/$/, '')}/v1/traces`, {
1168
1226
  method: 'POST',
1169
1227
  headers: { 'content-type': 'application/json', authorization: bearer },
1170
- body: JSON.stringify(body),
1228
+ body: JSON.stringify(out.body),
1171
1229
  signal: controller.signal,
1172
1230
  })
1173
1231
  if (logOk || !isOkStatus(res.status)) log(`posted ${label}: status=${res.status}`)
@@ -56,7 +56,7 @@
56
56
  "changed-file, added-line and removed-line counts",
57
57
  "repository-relative file paths with per-file added and removed line counts and a binary flag"
58
58
  ],
59
- "why": "Built-in tool, exact name, narrowed by the handler's `if` to Bash commands that start with `git`. The feature still returns immediately unless the command ran 'git commit' at a command position, and sends nothing unless the tool output carries git's new-commit summary line, so a failed commit sends nothing. Each commit is then read with `git show --numstat` under a 5 s budget. A commit that isn't the command's first word (`cd x && git commit`) is matched by the feature but not by `git *`; that miss is accepted rather than widening the `if` to every Bash command."
59
+ "why": "Built-in tool, exact name, narrowed by the handler's `if` to Bash commands that start with `git` (compound commands included). The feature still returns immediately unless the command ran 'git commit' at a command position, and sends nothing unless the tool output carries git's new-commit summary line, so a failed commit sends nothing. Each commit is then read with `git show --numstat` under a 5 s budget."
60
60
  },
61
61
  {
62
62
  "event": "PostToolUse",
@@ -106,5 +106,102 @@
106
106
  "session id, prompt id, Claude account id and email"
107
107
  ],
108
108
  "why": "StopFailure fires instead of Stop when a turn ends on an API error. Rate-limit and billing errors are the developer's own limit hits, joined to the plan snapshot. No matcher: every error type is rare enough, and the feature sends only the type enum, never the error details or the rendered message. Notification is deliberately not used: Claude Code's usage-limit auto-resume notices (`quota_auto_resume_*`) can fire while the developer is idle, which would move the use case's last-seen with no work then, the same reason SessionEnd is not used."
109
+ },
110
+ {
111
+ "event": "UserPromptSubmit",
112
+ "route": "turns",
113
+ "features": ["codex-turns"],
114
+ "optOut": "markers",
115
+ "hosts": ["codex"],
116
+ "dataClasses": ["session id, turn id and agent id", "working directory", "start and end times"],
117
+ "why": "Opens a Codex turn. The input carries `turn_id` (a turn with no tool calls has one too), `session_id` and `cwd`; the feature reads only those and never the prompt. Takes no matcher. The group is async, so the prompt never waits on it."
118
+ },
119
+ {
120
+ "event": "Stop",
121
+ "route": "turns",
122
+ "features": ["codex-turns"],
123
+ "optOut": "markers",
124
+ "hosts": ["codex"],
125
+ "dataClasses": ["session id, turn id and agent id", "working directory", "start and end times"],
126
+ "why": "Closes the turn that UserPromptSubmit opened and sends it as one span, so Developer Work gets a turn and its duration. Codex fires no Stop for an interrupted turn (Interrupt closes that one). Takes no matcher. Synchronous with `timeout: 5`, unlike the other groups: `codex exec` exits without waiting for an async hook, so an async Stop never sends a headless run's last turn. Codex waits for it at the end of each turn, as long as one post takes (its lock wait is capped at 1.5 s and its post at 1.2 s)."
127
+ },
128
+ {
129
+ "event": "Interrupt",
130
+ "route": "turns",
131
+ "features": ["codex-turns"],
132
+ "optOut": "markers",
133
+ "hosts": ["codex"],
134
+ "dataClasses": ["session id, turn id and agent id", "working directory", "start and end times"],
135
+ "why": "Closes a turn the developer interrupted: Codex fires Interrupt with the turn's `turn_id` and no Stop after it, so without this group the turn would stay open. Codex gives Interrupt a 1 s default timeout and prints a clamp warning above 3 s, so the group sets `timeout: 2`; the hook waits at most 0.6 s for the state lock, sends one span and does no other work. Takes no matcher. Synchronous like Stop, so `codex exec` waits for it (the post is capped at 1 s)."
136
+ },
137
+ {
138
+ "event": "PermissionRequest",
139
+ "route": "turns",
140
+ "features": ["codex-turns"],
141
+ "optOut": "markers",
142
+ "hosts": ["codex"],
143
+ "dataClasses": [
144
+ "session id, turn id and agent id",
145
+ "working directory",
146
+ "start and end times",
147
+ "tool name of the call waiting for approval"
148
+ ],
149
+ "why": "Opens a permission wait: Codex asks the developer to approve a tool call. The feature reads `tool_name` (an id such as `shell`, never `tool_input`) and sends the wait when the next event closes it, so Developer Work counts the time as waiting, not working. Takes no matcher; async."
150
+ },
151
+ {
152
+ "event": "PostToolUse",
153
+ "route": "turns",
154
+ "matcher": ".*",
155
+ "features": ["codex-turns"],
156
+ "optOut": "markers",
157
+ "hosts": ["codex"],
158
+ "dataClasses": ["session id, turn id and agent id", "working directory", "start and end times"],
159
+ "why": "Ends a permission wait after an approved call ran, and records the turn's last event time so a turn that never ends can be closed at the last thing it did. It runs on every tool call, so its matcher is `.*`: it reads only `session_id`, `turn_id`, `agent_id`, `tool_name` and `cwd`, and the tool's input and response are never read. A second PostToolUse group (route `bash`) runs the pull-request and commit markers on the same call. Async."
160
+ },
161
+ {
162
+ "event": "PreModelRequest",
163
+ "route": "turns",
164
+ "features": ["codex-turns"],
165
+ "optOut": "markers",
166
+ "hosts": ["codex"],
167
+ "dataClasses": ["session id, turn id and agent id", "working directory", "start and end times"],
168
+ "why": "Ends a permission wait after a denied call: with no tool to run, the next thing Codex does is its next model request. It also records the turn's last event time. PreToolUse is not hooked because Codex runs it before the approval, and PostModelRequest is not because this event already ended any wait. Takes no matcher; async."
169
+ },
170
+ {
171
+ "event": "SubagentStart",
172
+ "route": "turns",
173
+ "features": ["codex-turns"],
174
+ "optOut": "markers",
175
+ "hosts": ["codex"],
176
+ "dataClasses": [
177
+ "session id, turn id and agent id",
178
+ "working directory",
179
+ "start and end times",
180
+ "agent type"
181
+ ],
182
+ "why": "Opens a subagent run. Subagent events keep the parent's `session_id` and add `agent_id` (the child's own conversation id) and `agent_type`. The span exists so a subagent's own telemetry can be attributed to the parent's turn; it is no turn in any count. Takes no matcher; async."
183
+ },
184
+ {
185
+ "event": "SubagentStop",
186
+ "route": "turns",
187
+ "features": ["codex-turns"],
188
+ "optOut": "markers",
189
+ "hosts": ["codex"],
190
+ "dataClasses": [
191
+ "session id, turn id and agent id",
192
+ "working directory",
193
+ "start and end times",
194
+ "agent type"
195
+ ],
196
+ "why": "Closes a subagent run and sends it as one span. Takes no matcher; async."
197
+ },
198
+ {
199
+ "event": "SessionStart",
200
+ "route": "turns",
201
+ "features": ["codex-turns"],
202
+ "optOut": "markers",
203
+ "hosts": ["codex"],
204
+ "dataClasses": ["session id, turn id and agent id", "working directory", "start and end times"],
205
+ "why": "Closes a turn that was still open when the session started, resumed or forked: the turn is sent ending at the last event it saw, never at the new start, so a killed Codex can't stretch a turn across the developer's absence. It also deletes turn state older than 7 days. It takes no matcher, so it runs for every source (the session-start refresh is a separate group that runs for `startup|resume|fork` only). Async."
109
206
  }
110
207
  ]
@@ -0,0 +1,209 @@
1
+ // The built-in redaction rule packs and the attribute keys redaction never
2
+ // scans: the same data the Mainwave SDKs ship, so a rules file means the same
3
+ // thing here as in an application. Generated; do not edit by hand.
4
+ //
5
+ // PACKS: pack name -> rules, in the order they run. A rule's effective name is
6
+ // `<pack>.<name>`; `validate: 'luhn'` is the one post-match check.
7
+ // BUILTIN_EXEMPT_KEYS: identity and routing keys the platform joins on, never
8
+ // scanned whatever the rules say.
9
+
10
+ export const PACKS_VERSION = 1
11
+
12
+ export const PACKS = {
13
+ secrets: [
14
+ {
15
+ name: 'aws_access_key',
16
+ pattern: '\\b(?:AKIA|ASIA)[A-Z0-9]{16}\\b',
17
+ case_insensitive: false,
18
+ },
19
+ {
20
+ name: 'github_token',
21
+ pattern: 'gh[pousr]_[A-Za-z0-9]{36,255}\\b',
22
+ case_insensitive: false,
23
+ },
24
+ {
25
+ name: 'github_fine_grained_pat',
26
+ pattern: 'github_pat_[A-Za-z0-9_]{22,255}\\b',
27
+ case_insensitive: false,
28
+ },
29
+ {
30
+ name: 'anthropic_api_key',
31
+ pattern: 'sk-ant-[A-Za-z0-9_-]{20,256}',
32
+ case_insensitive: false,
33
+ },
34
+ {
35
+ name: 'openai_style_scoped_key',
36
+ pattern: 'sk-(?:proj|svcacct|admin|None|or-v1)-[A-Za-z0-9_-]{20,256}',
37
+ case_insensitive: false,
38
+ },
39
+ {
40
+ name: 'openai_api_key',
41
+ pattern: '\\bsk-[A-Za-z0-9]{20,256}',
42
+ case_insensitive: false,
43
+ },
44
+ {
45
+ name: 'slack_token',
46
+ pattern: '(?:xox[abcdprs]|xapp)-[A-Za-z0-9-]{10,256}',
47
+ case_insensitive: false,
48
+ },
49
+ {
50
+ name: 'private_key',
51
+ pattern:
52
+ '-----BEGIN (?:[A-Z]{1,16} ){0,4}PRIVATE KEY(?: BLOCK)?-----[\\s\\S]{0,8192}?-----END (?:[A-Z]{1,16} ){0,4}PRIVATE KEY(?: BLOCK)?-----',
53
+ case_insensitive: false,
54
+ },
55
+ {
56
+ name: 'jwt',
57
+ pattern: 'eyJ[A-Za-z0-9_-]{10,1024}\\.[A-Za-z0-9_-]{10,8192}\\.[A-Za-z0-9_-]{10,8192}',
58
+ case_insensitive: false,
59
+ },
60
+ ],
61
+ 'pii.contact': [
62
+ {
63
+ name: 'email',
64
+ pattern:
65
+ "\\b[A-Za-z0-9._%+'-]{1,64}(?:@|%40)[A-Za-z0-9-]{1,63}(?:\\.[A-Za-z0-9-]{1,63}){0,8}\\.[A-Za-z]{2,24}\\b",
66
+ case_insensitive: false,
67
+ },
68
+ {
69
+ name: 'phone_nanp',
70
+ pattern:
71
+ '(?:\\+1[ .-]?)?(?:\\([2-9]\\d{2}\\)[ .-]?[2-9]\\d{2}[ .-]\\d{4}|\\b[2-9]\\d{2}[ .-][2-9]\\d{2}[ .-]\\d{4})\\b|\\+1[ .-]?[2-9]\\d{2}[ .-]?[2-9]\\d{2}[ .-]?\\d{4}\\b',
72
+ case_insensitive: false,
73
+ },
74
+ {
75
+ name: 'phone_e164',
76
+ pattern: '\\B\\+[1-9](?:[ .-]?\\d){7,14}\\b',
77
+ case_insensitive: false,
78
+ },
79
+ ],
80
+ 'pii.us_gov_id': [
81
+ {
82
+ name: 'ssn',
83
+ pattern:
84
+ '\\b(?:00[1-9]|0[1-9]\\d|[1-578]\\d\\d|6[0-57-9]\\d|66[0-57-9])-(?:0[1-9]|[1-9]\\d)-(?:000[1-9]|00[1-9]\\d|0[1-9]\\d\\d|[1-9]\\d{3})\\b',
85
+ case_insensitive: false,
86
+ },
87
+ ],
88
+ 'pii.payment_card': [
89
+ {
90
+ name: 'card_number_grouped_19',
91
+ pattern: '\\b[2-6]\\d{3}[ .-]\\d{4}[ .-]\\d{4}[ .-]\\d{4}[ .-]\\d{3}\\b',
92
+ case_insensitive: false,
93
+ validate: 'luhn',
94
+ },
95
+ {
96
+ name: 'card_number',
97
+ pattern:
98
+ '\\b[2-6](?:\\d{12,18}|\\d{3}[ .-]\\d{4}[ .-]\\d{4}[ .-]\\d{1,4}|\\d{3}[ .-]\\d{6}[ .-]\\d{4,5})\\b',
99
+ case_insensitive: false,
100
+ validate: 'luhn',
101
+ },
102
+ ],
103
+ }
104
+
105
+ export const BUILTIN_EXEMPT_KEYS = [
106
+ 'agent.decision',
107
+ 'agent_id',
108
+ 'ccr.session.id',
109
+ 'cowork.surface',
110
+ 'decision',
111
+ 'devtool.cr_number',
112
+ 'devtool.cr_url',
113
+ 'devtool.external_id',
114
+ 'devtool.review_id',
115
+ 'devtool.review_verdict',
116
+ 'devtool.wi_key',
117
+ 'devtool.wi_prev_state_category',
118
+ 'devtool.wi_state_category',
119
+ 'devtool.wi_url',
120
+ 'event.kind',
121
+ 'gen_ai.agent.id',
122
+ 'gen_ai.agent.name',
123
+ 'gen_ai.conversation.id',
124
+ 'gen_ai.generation.id',
125
+ 'gen_ai.operation.name',
126
+ 'gen_ai.provider.name',
127
+ 'gen_ai.response.id',
128
+ 'gen_ai.system',
129
+ 'gen_ai.tool.call.id',
130
+ 'gen_ai.tool.name',
131
+ 'gen_ai.tool.type',
132
+ 'github.pr_number',
133
+ 'github.pr_url',
134
+ 'kiro.account.id',
135
+ 'kiro.client.type',
136
+ 'kiro.file.op',
137
+ 'kiro.hook_event_name',
138
+ 'kiro.metric',
139
+ 'kiro.profile.arn',
140
+ 'kiro.profile.id',
141
+ 'kiro.surface',
142
+ 'kiro.tool_name',
143
+ 'kiro.user.email',
144
+ 'kiro.user.id',
145
+ 'linear.issue_id',
146
+ 'linear.issue_url',
147
+ 'linear.op',
148
+ 'llm.system',
149
+ 'mainwave.caller.span_id',
150
+ 'mainwave.caller.trace_id',
151
+ 'mainwave.claude_code.stop_failure.error',
152
+ 'mainwave.codex.agent_type',
153
+ 'mainwave.codex.parent_turn_id',
154
+ 'mainwave.codex.tool_name',
155
+ 'mainwave.codex.turn.end_reason',
156
+ 'mainwave.codex.turn_id',
157
+ 'mainwave.codex.wait.end_event',
158
+ 'mainwave.connector_id',
159
+ 'mainwave.connector_type',
160
+ 'mainwave.devtool_vendor',
161
+ 'mainwave.host_source',
162
+ 'mainwave.organization_id',
163
+ 'mainwave.reasoning.present',
164
+ 'mainwave.redaction.count',
165
+ 'mainwave.request.thinking',
166
+ 'mainwave.run_id',
167
+ 'mainwave.source',
168
+ 'mainwave.subagent.id',
169
+ 'mainwave.subscription.billing_type',
170
+ 'mainwave.subscription.organization_role',
171
+ 'mainwave.subscription.organization_type',
172
+ 'mainwave.subscription.provider',
173
+ 'mainwave.subscription.rate_limit_tier',
174
+ 'mainwave.subscription.seat_tier',
175
+ 'mainwave.subscription.trigger',
176
+ 'mainwave.subscription.workspace_role',
177
+ 'mainwave.ucid',
178
+ 'mainwave.ucid_source',
179
+ 'mainwave.user_email',
180
+ 'mcp_server_scope',
181
+ 'organization.id',
182
+ 'prompt.id',
183
+ 'query_source',
184
+ 'service.name',
185
+ 'status_code',
186
+ 'user.account_uuid',
187
+ 'user.email',
188
+ 'vcs.change.external_id',
189
+ 'vcs.change.file.status',
190
+ 'vcs.change.id',
191
+ 'vcs.change.merge_commit_sha',
192
+ 'vcs.change.review.id',
193
+ 'vcs.change.review.verdict',
194
+ 'vcs.change.state',
195
+ 'vcs.commit.parent_sha',
196
+ 'vcs.commit.sha',
197
+ 'vcs.provider.name',
198
+ 'vcs.repository.default_branch',
199
+ 'vcs.repository.id',
200
+ 'vcs.repository.url.full',
201
+ 'work.instance',
202
+ 'work.item.external_id',
203
+ 'work.item.id',
204
+ 'work.item.op',
205
+ 'work.item.previous_state',
206
+ 'work.item.state',
207
+ 'work.item.url',
208
+ 'work.vendor',
209
+ ]
@@ -0,0 +1,215 @@
1
+ // In-place redaction support for a JSON-valued attribute. The value is never
2
+ // parsed into objects and never re-serialized: a strict RFC 8259 tokenizer
3
+ // finds the string and number value tokens, and only a token whose text
4
+ // changes is rewritten. Key order, duplicate keys, whitespace, number spelling
5
+ // and untouched escapes survive byte for byte.
6
+ //
7
+ // The tokenizer keeps an explicit stack, so no depth of nesting can overflow
8
+ // the call stack. Pure: no I/O.
9
+
10
+ const WS = (c) => c === 0x20 || c === 0x09 || c === 0x0a || c === 0x0d
11
+ const NUMBER = /-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?/y
12
+
13
+ const isHex = (c) =>
14
+ (c >= 0x30 && c <= 0x39) || (c >= 0x41 && c <= 0x46) || (c >= 0x61 && c <= 0x66)
15
+
16
+ /** The index after the string that starts at `text[start]` (a quote), or -1
17
+ * when it is not a valid JSON string: a raw control character, an unknown
18
+ * escape or a short \u escape, or no closing quote. */
19
+ const stringEnd = (text, start) => {
20
+ const n = text.length
21
+ for (let i = start + 1; i < n; i++) {
22
+ const c = text.charCodeAt(i)
23
+ if (c === 0x22) return i + 1
24
+ if (c < 0x20) return -1
25
+ if (c !== 0x5c) continue
26
+ const next = text[i + 1]
27
+ if (next === undefined) return -1
28
+ if ('"\\/bfnrt'.includes(next)) {
29
+ i++
30
+ } else if (next === 'u') {
31
+ for (let k = 2; k <= 5; k++) {
32
+ if (i + k >= n || !isHex(text.charCodeAt(i + k))) return -1
33
+ }
34
+ i += 5
35
+ } else {
36
+ return -1
37
+ }
38
+ }
39
+ return -1
40
+ }
41
+
42
+ /** The value tokens of `text` when it is JSON (after JSON whitespace the next
43
+ * character is `{` or `[` and the whole value is one valid RFC 8259 text), in
44
+ * order, as `{ kind: 'string' | 'number', start, end }` (a string's span
45
+ * includes its quotes); undefined when it is not. Object keys, punctuation
46
+ * and the literals `true`, `false` and `null` are not tokens. */
47
+ export const tokenizeJson = (text) => {
48
+ const n = text.length
49
+ let i = 0
50
+ while (i < n && WS(text.charCodeAt(i))) i++
51
+ if (text[i] !== '{' && text[i] !== '[') return undefined
52
+
53
+ const tokens = []
54
+ // true: an object, false: an array.
55
+ const stack = []
56
+ // value | valueOrEnd | keyOrEnd | key | colon | after
57
+ let state = 'value'
58
+ for (;;) {
59
+ while (i < n && WS(text.charCodeAt(i))) i++
60
+ if (state === 'after' && stack.length === 0) return i === n ? tokens : undefined
61
+ if (i >= n) return undefined
62
+ const char = text[i]
63
+ switch (state) {
64
+ case 'valueOrEnd':
65
+ if (char === ']') {
66
+ stack.pop()
67
+ i++
68
+ state = 'after'
69
+ break
70
+ }
71
+ state = 'value'
72
+ continue
73
+ case 'value':
74
+ if (char === '{') {
75
+ stack.push(true)
76
+ i++
77
+ state = 'keyOrEnd'
78
+ } else if (char === '[') {
79
+ stack.push(false)
80
+ i++
81
+ state = 'valueOrEnd'
82
+ } else if (char === '"') {
83
+ const end = stringEnd(text, i)
84
+ if (end < 0) return undefined
85
+ tokens.push({ kind: 'string', start: i, end })
86
+ i = end
87
+ state = 'after'
88
+ } else if (char === '-' || (char >= '0' && char <= '9')) {
89
+ NUMBER.lastIndex = i
90
+ const match = NUMBER.exec(text)
91
+ if (match === null) return undefined
92
+ tokens.push({ kind: 'number', start: i, end: i + match[0].length })
93
+ i += match[0].length
94
+ state = 'after'
95
+ } else if (text.startsWith('true', i)) {
96
+ i += 4
97
+ state = 'after'
98
+ } else if (text.startsWith('false', i)) {
99
+ i += 5
100
+ state = 'after'
101
+ } else if (text.startsWith('null', i)) {
102
+ i += 4
103
+ state = 'after'
104
+ } else {
105
+ return undefined
106
+ }
107
+ break
108
+ case 'keyOrEnd':
109
+ if (char === '}') {
110
+ stack.pop()
111
+ i++
112
+ state = 'after'
113
+ break
114
+ }
115
+ state = 'key'
116
+ continue
117
+ case 'key': {
118
+ if (char !== '"') return undefined
119
+ const end = stringEnd(text, i)
120
+ if (end < 0) return undefined
121
+ i = end
122
+ state = 'colon'
123
+ break
124
+ }
125
+ case 'colon':
126
+ if (char !== ':') return undefined
127
+ i++
128
+ state = 'value'
129
+ break
130
+ case 'after': {
131
+ const inObject = stack[stack.length - 1]
132
+ if (char === ',') {
133
+ i++
134
+ state = inObject ? 'key' : 'value'
135
+ } else if (char === (inObject ? '}' : ']')) {
136
+ stack.pop()
137
+ i++
138
+ } else {
139
+ return undefined
140
+ }
141
+ break
142
+ }
143
+ }
144
+ }
145
+ }
146
+
147
+ const ESCAPES = { '"': '"', '\\': '\\', '/': '/', b: '\b', f: '\f', n: '\n', r: '\r', t: '\t' }
148
+
149
+ /** The decoded text of a valid JSON string token (quotes included in `raw`).
150
+ * A pair of surrogate escapes is one astral character in the result, as in
151
+ * any UTF-16 string; a lone surrogate escape stays a lone surrogate. */
152
+ export const decodeJsonString = (raw) => {
153
+ if (!raw.includes('\\')) return raw.slice(1, -1)
154
+ let out = ''
155
+ let run = 1
156
+ const end = raw.length - 1
157
+ for (let i = 1; i < end; i++) {
158
+ if (raw.charCodeAt(i) !== 0x5c) continue
159
+ out += raw.slice(run, i)
160
+ const next = raw[i + 1]
161
+ if (next === 'u') {
162
+ out += String.fromCharCode(parseInt(raw.slice(i + 2, i + 6), 16))
163
+ i += 5
164
+ } else {
165
+ out += ESCAPES[next]
166
+ i++
167
+ }
168
+ run = i + 1
169
+ }
170
+ return out + raw.slice(run, end)
171
+ }
172
+
173
+ /** Whether `text` holds a character the canonical encoding writes
174
+ * differently: a quote, a backslash, a control character or any surrogate. */
175
+ const needsEscape = (text) => {
176
+ for (let i = 0; i < text.length; i++) {
177
+ const c = text.charCodeAt(i)
178
+ if (c < 0x20 || c === 0x22 || c === 0x5c || (c >= 0xd800 && c <= 0xdfff)) return true
179
+ }
180
+ return false
181
+ }
182
+
183
+ const hex4 = (c) => `\\u${c.toString(16).padStart(4, '0')}`
184
+
185
+ /** The canonical JSON encoding of `text`: `"` and `\` escaped, backspace, form
186
+ * feed, newline, carriage return and tab as `\b \f \n \r \t`, every other
187
+ * character below U+0020 and every lone surrogate as `\u` plus four lowercase
188
+ * hex digits, everything else literal (`/`, U+007F, U+2028 and non-ASCII
189
+ * included; an astral character stays a literal surrogate pair). */
190
+ export const encodeJsonString = (text) => {
191
+ if (!needsEscape(text)) return `"${text}"`
192
+ let out = '"'
193
+ for (let i = 0; i < text.length; i++) {
194
+ const c = text.charCodeAt(i)
195
+ if (c === 0x22) out += '\\"'
196
+ else if (c === 0x5c) out += '\\\\'
197
+ else if (c === 0x08) out += '\\b'
198
+ else if (c === 0x0c) out += '\\f'
199
+ else if (c === 0x0a) out += '\\n'
200
+ else if (c === 0x0d) out += '\\r'
201
+ else if (c === 0x09) out += '\\t'
202
+ else if (c < 0x20) out += hex4(c)
203
+ else if (c >= 0xd800 && c <= 0xdbff) {
204
+ const next = text.charCodeAt(i + 1)
205
+ if (next >= 0xdc00 && next <= 0xdfff) {
206
+ out += text[i] + text[i + 1]
207
+ i++
208
+ } else {
209
+ out += hex4(c)
210
+ }
211
+ } else if (c >= 0xdc00 && c <= 0xdfff) out += hex4(c)
212
+ else out += text[i]
213
+ }
214
+ return `${out}"`
215
+ }