@mainwave/cli 0.10.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +176 -13
- package/dist/adapters/claude-code.js +5 -4
- package/dist/adapters/claude-desktop.js +35 -1
- package/dist/adapters/copilot-chat.js +7 -13
- package/dist/adapters/copilot-cli.js +9 -12
- package/dist/adapters/cursor.js +7 -2
- package/dist/adapters/kiro.js +13 -8
- package/dist/commands.js +217 -0
- package/dist/core/apply.js +13 -5
- package/dist/core/auth-commands.js +2 -1
- package/dist/core/codecs/kiro-hooks-json.js +1 -1
- package/dist/core/doctor.js +71 -8
- package/dist/core/entry-script.js +9 -4
- package/dist/core/format.js +0 -1
- package/dist/core/hook-copies.js +157 -0
- package/dist/core/install-hooks.js +148 -10
- package/dist/core/interview.js +8 -2
- package/dist/core/markers.js +90 -24
- package/dist/core/post-setup.js +5 -22
- package/dist/core/redaction.js +179 -0
- package/dist/core/skills.js +6 -14
- package/dist/core/status.js +21 -1
- package/dist/core/store.js +255 -5
- package/dist/core/url.js +1 -0
- package/dist/core/wizard.js +101 -7
- package/dist/main.js +163 -162
- package/hooks/git-commit-marker.mjs +406 -0
- package/hooks/kiro-otlp.mjs +10 -1
- package/hooks/mainwave-hook.mjs +17 -5
- package/hooks/marker-core.mjs +56 -2
- package/hooks/matchers.json +20 -3
- package/hooks/redaction-data.mjs +201 -0
- package/hooks/redaction-json.mjs +215 -0
- package/hooks/redaction-pattern.mjs +503 -0
- package/hooks/redaction.d.mts +84 -0
- package/hooks/redaction.mjs +701 -0
- package/hooks/vocabulary.d.mts +13 -0
- package/hooks/vocabulary.mjs +42 -1
- package/package.json +9 -7
- package/skills/mainwave-instrument/SKILL.md +24 -6
- package/skills/mainwave-instrument/reference/frameworks.md +4 -2
- package/skills/mainwave-instrument/reference/troubleshooting.md +4 -0
- package/skills/mainwave-instrument-node/SKILL.md +81 -30
- package/skills/mainwave-instrument-node/reference/api.md +3 -3
- package/skills/mainwave-instrument-node/reference/frameworks.md +42 -25
- package/skills/mainwave-instrument-node/reference/troubleshooting.md +11 -7
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// The git commit marker: a PostToolUse feature of the Mainwave hook dispatcher
|
|
3
|
+
// (mainwave-hook.mjs) for commits a coding agent makes with `git commit`.
|
|
4
|
+
//
|
|
5
|
+
// Nothing else records which commits an AI session made, or how many lines
|
|
6
|
+
// each one changed per file. This hook runs after each Bash tool call, and
|
|
7
|
+
// when the command ran `git commit` it reads the new commit's short name out
|
|
8
|
+
// of the tool result (git prints `[<branch> <sha>] <subject>` on success),
|
|
9
|
+
// asks git for that commit's numstat, and emits one `vcs.commit.created` span
|
|
10
|
+
// per commit carrying the full sha, the first parent, the totals and the
|
|
11
|
+
// per-file counts.
|
|
12
|
+
//
|
|
13
|
+
// The tool result is what proves the commit happened: a failed commit (nothing
|
|
14
|
+
// to commit, a rejected pre-commit hook, a bad message) prints no such line, so
|
|
15
|
+
// nothing is sent. Neither is anything for `git commit --quiet` or `--dry-run`,
|
|
16
|
+
// which print none either; that miss is accepted. The commit is looked up by
|
|
17
|
+
// the name git printed, not HEAD, so a command that commits twice, or moves
|
|
18
|
+
// HEAD after committing, still reports each commit it made. Git's answer is
|
|
19
|
+
// used only when its full name begins with the printed short name.
|
|
20
|
+
//
|
|
21
|
+
// Privacy: emits repository-relative paths, line counts and object names,
|
|
22
|
+
// never file contents, the diff or the commit message. A binary file (git
|
|
23
|
+
// prints `-` for both counts) counts as changed with 0 lines added and 0
|
|
24
|
+
// removed, and is flagged binary. Claude Code runs this only for a Bash
|
|
25
|
+
// command that starts with `git` (the hook group's `if` filter); it returns
|
|
26
|
+
// immediately, with no log and no work, for anything that isn't a commit, and
|
|
27
|
+
// fails open otherwise: git is given 5 s in all, and any error sends nothing.
|
|
28
|
+
|
|
29
|
+
import { execFile } from 'node:child_process'
|
|
30
|
+
import { isAbsolute, resolve } from 'node:path'
|
|
31
|
+
|
|
32
|
+
import {
|
|
33
|
+
buildMarkerPayload,
|
|
34
|
+
emitMarker,
|
|
35
|
+
hookFlag,
|
|
36
|
+
intAttr,
|
|
37
|
+
loadOtelConfig,
|
|
38
|
+
markerCallContext,
|
|
39
|
+
markerHostSource,
|
|
40
|
+
markerLog,
|
|
41
|
+
markerManagedDir,
|
|
42
|
+
markerOauthAccount,
|
|
43
|
+
markerProjectDir,
|
|
44
|
+
markerSessionEnv,
|
|
45
|
+
markerTool,
|
|
46
|
+
payloadShape,
|
|
47
|
+
toolOutput,
|
|
48
|
+
} from './marker-core.mjs'
|
|
49
|
+
import {
|
|
50
|
+
ATTR_VCS_COMMIT_FILE_IS_BINARY,
|
|
51
|
+
ATTR_VCS_COMMIT_FILE_LINES_ADDED,
|
|
52
|
+
ATTR_VCS_COMMIT_FILE_LINES_REMOVED,
|
|
53
|
+
ATTR_VCS_COMMIT_FILE_PATHS,
|
|
54
|
+
ATTR_VCS_COMMIT_FILES_CHANGED,
|
|
55
|
+
ATTR_VCS_COMMIT_LINES_ADDED,
|
|
56
|
+
ATTR_VCS_COMMIT_LINES_REMOVED,
|
|
57
|
+
ATTR_VCS_COMMIT_PARENT_COUNT,
|
|
58
|
+
ATTR_VCS_COMMIT_PARENT_SHA,
|
|
59
|
+
ATTR_VCS_COMMIT_SHA,
|
|
60
|
+
VCS_COMMIT_CREATED,
|
|
61
|
+
} from './vocabulary.mjs'
|
|
62
|
+
|
|
63
|
+
const LOG_NAME = 'git-commit-marker.log'
|
|
64
|
+
|
|
65
|
+
// Non-coding source/service so the marker lands as an ordinary span and stays
|
|
66
|
+
// out of the coding-work segmentation, as the other markers do. The vendor is
|
|
67
|
+
// git itself: a commit is local, whatever forge it is later pushed to.
|
|
68
|
+
const MARKER_SOURCE = 'hook.git'
|
|
69
|
+
const MARKER_SERVICE = 'mainwave.git-hook'
|
|
70
|
+
|
|
71
|
+
/** The most git may take, over every lookup one tool call makes. */
|
|
72
|
+
export const GIT_TIMEOUT_MS = 5000
|
|
73
|
+
|
|
74
|
+
/** The most commits one tool call reports. */
|
|
75
|
+
export const MAX_COMMITS = 20
|
|
76
|
+
|
|
77
|
+
/** The most files a span lists one by one. The totals always cover every
|
|
78
|
+
* file; past this the per-file lists stop, so their length says how many
|
|
79
|
+
* they hold. */
|
|
80
|
+
export const MAX_FILES = 2000
|
|
81
|
+
|
|
82
|
+
/** Past this many bytes of paths the per-file lists stop too, keeping the
|
|
83
|
+
* span far below the collector's per-attribute bound. */
|
|
84
|
+
export const MAX_PATH_BYTES = 128 * 1024
|
|
85
|
+
|
|
86
|
+
/** git's own output cap for one lookup (a commit touching many files). */
|
|
87
|
+
const MAX_GIT_OUTPUT_BYTES = 16 * 1024 * 1024
|
|
88
|
+
|
|
89
|
+
// A shell word: double-quoted, single-quoted, or bare.
|
|
90
|
+
const WORD = `(?:"[^"]*"|'[^']*'|[^\\s;&|()]+)`
|
|
91
|
+
// git's global options before the subcommand: `-C <dir>`, `-c <k=v>`, and
|
|
92
|
+
// `--flag` / `--flag=value` (`--no-pager`, `--git-dir=…`).
|
|
93
|
+
const GLOBAL_OPTS = `((?:\\s+-[Cc]\\s+${WORD}|\\s+--[A-Za-z-]+(?:=${WORD})?)*)`
|
|
94
|
+
// `git commit` at a command position: the start, after a separator, a
|
|
95
|
+
// subshell or command substitution opener, or a shell keyword; `VAR=value`
|
|
96
|
+
// prefixes allowed. `commit-tree` and `commit-graph` are other commands.
|
|
97
|
+
const GIT_COMMIT_RE = new RegExp(
|
|
98
|
+
`(?:^|&&|\\|\\||[;|\\n({]|\\$\\(|\\b(?:then|do|else)\\s)\\s*` +
|
|
99
|
+
`(?:[A-Za-z_][A-Za-z0-9_]*=${WORD}?\\s+)*` +
|
|
100
|
+
`git${GLOBAL_OPTS}\\s+commit(?![\\w-])`,
|
|
101
|
+
'g',
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
const unquote = (w) => (/^(["']).*\1$/.test(w) ? w.slice(1, -1) : w)
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Every `git commit` in a Bash command, each with the directory its `-C`
|
|
108
|
+
* options name (relative to the command's own directory), else undefined.
|
|
109
|
+
* Empty when the command runs no commit — the cheap guard every call meets.
|
|
110
|
+
*/
|
|
111
|
+
export const findGitCommits = (command) => {
|
|
112
|
+
if (typeof command !== 'string' || !command.includes('commit')) return []
|
|
113
|
+
const out = []
|
|
114
|
+
for (const m of command.matchAll(GIT_COMMIT_RE)) {
|
|
115
|
+
const dirs = [...(m[1] ?? '').matchAll(new RegExp(`-C\\s+(${WORD})`, 'g'))].map((d) =>
|
|
116
|
+
unquote(d[1]),
|
|
117
|
+
)
|
|
118
|
+
out.push({ dir: dirs.length ? dirs.reduce((a, d) => (a ? resolve(a, d) : d)) : undefined })
|
|
119
|
+
}
|
|
120
|
+
return out
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Whether a Bash command runs `git commit` at a command position. */
|
|
124
|
+
export const isGitCommit = (command) => findGitCommits(command).length > 0
|
|
125
|
+
|
|
126
|
+
// git's summary line for a new commit: `[main 1a2b3c4] subject`, also
|
|
127
|
+
// `[main (root-commit) 1a2b3c4]` and `[detached HEAD 1a2b3c4]`. The name is
|
|
128
|
+
// at least 7 hex digits (core.abbrev's floor), up to a SHA-256 name.
|
|
129
|
+
const COMMIT_LINE_RE = /^\[[^\n]*? ([0-9a-f]{7,64})\] /gm
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The short names of the commits a tool result reports, in order, distinct.
|
|
133
|
+
* Reads a bare string or Claude Code's `{stdout, stderr}`; a result with no
|
|
134
|
+
* commit line (a failed or quiet commit) yields none.
|
|
135
|
+
*/
|
|
136
|
+
export const commitsInOutput = (output) => {
|
|
137
|
+
const texts =
|
|
138
|
+
typeof output === 'string'
|
|
139
|
+
? [output]
|
|
140
|
+
: output && typeof output === 'object'
|
|
141
|
+
? [output.stdout, output.stderr].filter((t) => typeof t === 'string')
|
|
142
|
+
: []
|
|
143
|
+
const shas = []
|
|
144
|
+
for (const text of texts) {
|
|
145
|
+
for (const m of text.matchAll(COMMIT_LINE_RE)) {
|
|
146
|
+
if (!shas.includes(m[1])) shas.push(m[1])
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return shas.slice(0, MAX_COMMITS)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** A numstat count: an integer, or `-` for a binary file. */
|
|
153
|
+
const count = (raw) => (raw === '-' ? undefined : /^\d+$/.test(raw) ? Number(raw) : NaN)
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Parse `git show -z --numstat --format=%H%n%P <sha>` output: the commit's
|
|
157
|
+
* name and parents, then one record per file. With `-z` a record is
|
|
158
|
+
* `<added>\t<removed>\t<path>\0`, or for a rename `<added>\t<removed>\t\0<old
|
|
159
|
+
* path>\0<new path>\0` (the new path is kept). A binary file's counts are
|
|
160
|
+
* `-`: it is kept with 0 and 0 and flagged. Returns undefined for anything
|
|
161
|
+
* that doesn't parse.
|
|
162
|
+
*/
|
|
163
|
+
export const parseNumstat = (stdout) => {
|
|
164
|
+
if (typeof stdout !== 'string') return undefined
|
|
165
|
+
const headEnd = stdout.indexOf('\0')
|
|
166
|
+
if (headEnd === -1) return undefined
|
|
167
|
+
const [sha = '', parentLine = ''] = stdout.slice(0, headEnd).split('\n')
|
|
168
|
+
if (!/^[0-9a-f]{40}([0-9a-f]{24})?$/.test(sha)) return undefined
|
|
169
|
+
const parents = parentLine.split(' ').filter(Boolean)
|
|
170
|
+
if (!parents.every((p) => /^[0-9a-f]{40,64}$/.test(p))) return undefined
|
|
171
|
+
const tokens = stdout
|
|
172
|
+
.slice(headEnd + 1)
|
|
173
|
+
.replace(/^\n/, '')
|
|
174
|
+
.split('\0')
|
|
175
|
+
const files = []
|
|
176
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
177
|
+
const token = tokens[i]
|
|
178
|
+
if (token === '') continue
|
|
179
|
+
const m = /^(-|\d+)\t(-|\d+)\t([\s\S]*)$/.exec(token)
|
|
180
|
+
if (!m) return undefined
|
|
181
|
+
let path = m[3]
|
|
182
|
+
if (path === '') {
|
|
183
|
+
// A rename or copy: the old path, then the new one.
|
|
184
|
+
path = tokens[i + 2]
|
|
185
|
+
i += 2
|
|
186
|
+
}
|
|
187
|
+
if (typeof path !== 'string' || path === '') return undefined
|
|
188
|
+
const added = count(m[1])
|
|
189
|
+
const removed = count(m[2])
|
|
190
|
+
if (Number.isNaN(added) || Number.isNaN(removed)) return undefined
|
|
191
|
+
const isBinary = added === undefined || removed === undefined
|
|
192
|
+
files.push({
|
|
193
|
+
path,
|
|
194
|
+
linesAdded: isBinary ? 0 : added,
|
|
195
|
+
linesRemoved: isBinary ? 0 : removed,
|
|
196
|
+
isBinary,
|
|
197
|
+
})
|
|
198
|
+
}
|
|
199
|
+
return { sha, parents, files }
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* One commit's numstat, from `git show` run in `cwd`. Resolves undefined on
|
|
204
|
+
* any failure (git absent, not a repository, unknown name, timeout, output
|
|
205
|
+
* that doesn't parse) and when git's full name doesn't begin with `shortSha`.
|
|
206
|
+
* `bin` replaces `git`; `handle` takes it only from the hook's own
|
|
207
|
+
* `--git-bin` flag (tests pass a stub), never from the environment.
|
|
208
|
+
*/
|
|
209
|
+
export const fetchCommit = (shortSha, { cwd, bin = 'git', timeoutMs = GIT_TIMEOUT_MS } = {}) =>
|
|
210
|
+
new Promise((done) => {
|
|
211
|
+
if (!/^[0-9a-f]{7,64}$/.test(shortSha ?? '') || timeoutMs <= 0) return done(undefined)
|
|
212
|
+
try {
|
|
213
|
+
execFile(
|
|
214
|
+
bin,
|
|
215
|
+
[
|
|
216
|
+
'--no-pager',
|
|
217
|
+
// A repository's config can name a program git runs on index reads.
|
|
218
|
+
'-c',
|
|
219
|
+
'core.fsmonitor=false',
|
|
220
|
+
'show',
|
|
221
|
+
'--no-color',
|
|
222
|
+
'--no-ext-diff',
|
|
223
|
+
'--no-textconv',
|
|
224
|
+
'-z',
|
|
225
|
+
'--numstat',
|
|
226
|
+
'--format=%H%n%P',
|
|
227
|
+
shortSha,
|
|
228
|
+
'--',
|
|
229
|
+
],
|
|
230
|
+
{
|
|
231
|
+
cwd,
|
|
232
|
+
timeout: timeoutMs,
|
|
233
|
+
maxBuffer: MAX_GIT_OUTPUT_BYTES,
|
|
234
|
+
windowsHide: true,
|
|
235
|
+
env: { ...process.env, GIT_OPTIONAL_LOCKS: '0', GIT_TERMINAL_PROMPT: '0' },
|
|
236
|
+
},
|
|
237
|
+
(err, stdout) => {
|
|
238
|
+
if (err) return done(undefined)
|
|
239
|
+
const commit = parseNumstat(stdout)
|
|
240
|
+
done(commit && commit.sha.startsWith(shortSha) ? commit : undefined)
|
|
241
|
+
},
|
|
242
|
+
)
|
|
243
|
+
} catch {
|
|
244
|
+
done(undefined)
|
|
245
|
+
}
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
/** The span's attributes for one commit: the totals over every file, and the
|
|
249
|
+
* per-file lists up to § MAX_FILES files and § MAX_PATH_BYTES of paths. */
|
|
250
|
+
export const commitAttributes = ({ sha, parents, files }) => {
|
|
251
|
+
const listed = []
|
|
252
|
+
let pathBytes = 0
|
|
253
|
+
for (const f of files) {
|
|
254
|
+
pathBytes += Buffer.byteLength(f.path)
|
|
255
|
+
if (listed.length >= MAX_FILES || pathBytes > MAX_PATH_BYTES) break
|
|
256
|
+
listed.push(f)
|
|
257
|
+
}
|
|
258
|
+
const sum = (k) => files.reduce((n, f) => n + f[k], 0)
|
|
259
|
+
const array = (key, values) => ({ key, value: { arrayValue: { values } } })
|
|
260
|
+
return {
|
|
261
|
+
strings: {
|
|
262
|
+
[ATTR_VCS_COMMIT_SHA]: sha,
|
|
263
|
+
[ATTR_VCS_COMMIT_PARENT_SHA]: parents[0],
|
|
264
|
+
},
|
|
265
|
+
typed: [
|
|
266
|
+
intAttr(ATTR_VCS_COMMIT_PARENT_COUNT, parents.length),
|
|
267
|
+
intAttr(ATTR_VCS_COMMIT_FILES_CHANGED, files.length),
|
|
268
|
+
intAttr(ATTR_VCS_COMMIT_LINES_ADDED, sum('linesAdded')),
|
|
269
|
+
intAttr(ATTR_VCS_COMMIT_LINES_REMOVED, sum('linesRemoved')),
|
|
270
|
+
array(
|
|
271
|
+
ATTR_VCS_COMMIT_FILE_PATHS,
|
|
272
|
+
listed.map((f) => ({ stringValue: f.path })),
|
|
273
|
+
),
|
|
274
|
+
array(
|
|
275
|
+
ATTR_VCS_COMMIT_FILE_LINES_ADDED,
|
|
276
|
+
listed.map((f) => ({ intValue: String(f.linesAdded) })),
|
|
277
|
+
),
|
|
278
|
+
array(
|
|
279
|
+
ATTR_VCS_COMMIT_FILE_LINES_REMOVED,
|
|
280
|
+
listed.map((f) => ({ intValue: String(f.linesRemoved) })),
|
|
281
|
+
),
|
|
282
|
+
array(
|
|
283
|
+
ATTR_VCS_COMMIT_FILE_IS_BINARY,
|
|
284
|
+
listed.map((f) => ({ boolValue: f.isBinary })),
|
|
285
|
+
),
|
|
286
|
+
],
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** The OTLP/JSON payload for one or more commits: one `vcs.commit.created`
|
|
291
|
+
* span each, in one request. */
|
|
292
|
+
export const buildCommitPayload = ({
|
|
293
|
+
commits,
|
|
294
|
+
sessionId,
|
|
295
|
+
userEmail,
|
|
296
|
+
userAccountUuid,
|
|
297
|
+
resourceAttrs,
|
|
298
|
+
hostSource,
|
|
299
|
+
callContext,
|
|
300
|
+
nowNs,
|
|
301
|
+
}) => {
|
|
302
|
+
const bodies = commits.map((commit) => {
|
|
303
|
+
const { strings, typed } = commitAttributes(commit)
|
|
304
|
+
return buildMarkerPayload({
|
|
305
|
+
spanName: VCS_COMMIT_CREATED,
|
|
306
|
+
scopeName: 'mainwave.git-commit-marker',
|
|
307
|
+
attributes: strings,
|
|
308
|
+
typedAttributes: typed,
|
|
309
|
+
sessionId,
|
|
310
|
+
userEmail,
|
|
311
|
+
userAccountUuid,
|
|
312
|
+
resourceAttrs,
|
|
313
|
+
source: MARKER_SOURCE,
|
|
314
|
+
service: MARKER_SERVICE,
|
|
315
|
+
hostSource,
|
|
316
|
+
callContext,
|
|
317
|
+
nowNs,
|
|
318
|
+
})
|
|
319
|
+
})
|
|
320
|
+
const [first, ...rest] = bodies
|
|
321
|
+
const spans = first.resourceSpans[0].scopeSpans[0].spans
|
|
322
|
+
for (const body of rest) spans.push(...body.resourceSpans[0].scopeSpans[0].spans)
|
|
323
|
+
return first
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** The directories to look a commit up in: each `-C` a commit named
|
|
327
|
+
* (against the command's directory), then the command's directory. */
|
|
328
|
+
const lookupDirs = (calls, cwd) => {
|
|
329
|
+
const dirs = calls.map((c) =>
|
|
330
|
+
c.dir === undefined ? cwd : isAbsolute(c.dir) ? c.dir : resolve(cwd, c.dir),
|
|
331
|
+
)
|
|
332
|
+
return [...new Set([...dirs, cwd])]
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* The feature: one span per commit the Bash command made, else nothing.
|
|
337
|
+
* `ctx.argv` carries the hook command's flags (and `--tool <host>` under the
|
|
338
|
+
* dispatcher); `ctx.emit` posts.
|
|
339
|
+
*/
|
|
340
|
+
export const handle = async (payload, { argv = process.argv, emit = emitMarker } = {}) => {
|
|
341
|
+
// Cheap guard first: no log and no work unless the command ran a commit.
|
|
342
|
+
const calls = findGitCommits(payload?.tool_input?.command)
|
|
343
|
+
if (calls.length === 0) return
|
|
344
|
+
const log = markerLog(argv, LOG_NAME)
|
|
345
|
+
|
|
346
|
+
const shas = commitsInOutput(toolOutput(payload))
|
|
347
|
+
if (shas.length === 0) {
|
|
348
|
+
log(`skip: no new commit in tool output (${payloadShape(payload)})`)
|
|
349
|
+
return
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const projectDir = markerProjectDir(payload)
|
|
353
|
+
const tool = markerTool(argv)
|
|
354
|
+
const managedDir = markerManagedDir(argv)
|
|
355
|
+
const env = markerSessionEnv(tool, payload)
|
|
356
|
+
const { endpoint, bearer, resourceAttrs } = loadOtelConfig(projectDir, env, tool, managedDir)
|
|
357
|
+
log(
|
|
358
|
+
`commits=${shas.length} endpoint=${endpoint ? 'set' : 'MISSING'} ` +
|
|
359
|
+
`bearer=${bearer ? 'set' : 'MISSING'} ucid=${resourceAttrs['mainwave.ucid'] || 'MISSING'}`,
|
|
360
|
+
)
|
|
361
|
+
if (!endpoint || !bearer) {
|
|
362
|
+
log('skip: collector endpoint/bearer not found in settings or env')
|
|
363
|
+
return
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// git runs where the agent ran the commit; every lookup shares one budget.
|
|
367
|
+
const cwd = payload.cwd || process.cwd()
|
|
368
|
+
const dirs = lookupDirs(calls, cwd)
|
|
369
|
+
const bin = hookFlag(argv, '--git-bin')
|
|
370
|
+
const deadline = Date.now() + (Number(hookFlag(argv, '--git-timeout-ms')) || GIT_TIMEOUT_MS)
|
|
371
|
+
const commits = []
|
|
372
|
+
for (const sha of shas) {
|
|
373
|
+
let commit
|
|
374
|
+
for (const dir of dirs) {
|
|
375
|
+
commit = await fetchCommit(sha, { cwd: dir, bin, timeoutMs: deadline - Date.now() })
|
|
376
|
+
if (commit) break
|
|
377
|
+
}
|
|
378
|
+
if (commit) commits.push(commit)
|
|
379
|
+
else log(`skip: commit ${sha} not found or git failed`)
|
|
380
|
+
}
|
|
381
|
+
if (commits.length === 0) return
|
|
382
|
+
|
|
383
|
+
// Read only once the emit is certain: `.claude.json` can run to megabytes.
|
|
384
|
+
const { email: userEmail, accountUuid: userAccountUuid } = markerOauthAccount(tool, {
|
|
385
|
+
cwd: projectDir,
|
|
386
|
+
env,
|
|
387
|
+
managedDir,
|
|
388
|
+
})
|
|
389
|
+
const body = buildCommitPayload({
|
|
390
|
+
commits,
|
|
391
|
+
sessionId: payload.session_id,
|
|
392
|
+
userEmail,
|
|
393
|
+
userAccountUuid,
|
|
394
|
+
resourceAttrs,
|
|
395
|
+
hostSource: markerHostSource(tool),
|
|
396
|
+
callContext: markerCallContext(tool, payload),
|
|
397
|
+
nowNs: BigInt(Date.now()) * 1_000_000n,
|
|
398
|
+
})
|
|
399
|
+
await emit({
|
|
400
|
+
endpoint,
|
|
401
|
+
bearer,
|
|
402
|
+
body,
|
|
403
|
+
label: commits.map((c) => c.sha.slice(0, 12)).join(','),
|
|
404
|
+
log,
|
|
405
|
+
})
|
|
406
|
+
}
|
package/hooks/kiro-otlp.mjs
CHANGED
|
@@ -54,6 +54,7 @@ import {
|
|
|
54
54
|
markerLogPath,
|
|
55
55
|
parseEnvFile,
|
|
56
56
|
parseKv,
|
|
57
|
+
redactForSend,
|
|
57
58
|
} from './marker-core.mjs'
|
|
58
59
|
|
|
59
60
|
const LOG_NAME = 'kiro.log'
|
|
@@ -484,6 +485,9 @@ export const handOff = (
|
|
|
484
485
|
argv = process.argv,
|
|
485
486
|
script = fileURLToPath(import.meta.url),
|
|
486
487
|
) => {
|
|
488
|
+
// Redact before anything is written: plaintext never lands in the spool.
|
|
489
|
+
const out = redactForSend(body, label, log)
|
|
490
|
+
if (out === undefined) return false
|
|
487
491
|
const dir = ensureSpool()
|
|
488
492
|
const full = join(dir, FULL_MARK)
|
|
489
493
|
if (sweepSpool(dir) >= SPOOL_MAX_FILES) {
|
|
@@ -503,7 +507,10 @@ export const handOff = (
|
|
|
503
507
|
// not full before
|
|
504
508
|
}
|
|
505
509
|
const file = join(dir, `kiro-${Date.now()}-${randomBytes(6).toString('hex')}.json`)
|
|
506
|
-
writeFileSync(file, JSON.stringify({ label, body
|
|
510
|
+
writeFileSync(file, JSON.stringify({ label, body: out.body, redacted: out.applied }), {
|
|
511
|
+
mode: 0o600,
|
|
512
|
+
flag: 'wx',
|
|
513
|
+
})
|
|
507
514
|
const logFile = hookFlag(argv, '--log-file')
|
|
508
515
|
const child = spawn(
|
|
509
516
|
process.execPath,
|
|
@@ -549,6 +556,8 @@ export const send = async (argv = process.argv) => {
|
|
|
549
556
|
log,
|
|
550
557
|
timeoutMs: POST_TIMEOUT_MS,
|
|
551
558
|
logOk: false,
|
|
559
|
+
// Redacted when it was spooled; one spooled with no rules is checked now.
|
|
560
|
+
redacted: spooled.redacted === true,
|
|
552
561
|
})
|
|
553
562
|
if (isOkStatus(status)) markDelivered(argv)
|
|
554
563
|
}
|
package/hooks/mainwave-hook.mjs
CHANGED
|
@@ -27,6 +27,7 @@ import { basename, dirname, join } from 'node:path'
|
|
|
27
27
|
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
28
28
|
|
|
29
29
|
import {
|
|
30
|
+
claudeConfigDir,
|
|
30
31
|
emitMarker,
|
|
31
32
|
markerLog,
|
|
32
33
|
markerManagedDir,
|
|
@@ -52,12 +53,13 @@ export const WATCHDOG_MS = 9000
|
|
|
52
53
|
* Caps are each feature's own worst case plus 250 ms: the plan snapshot's
|
|
53
54
|
* 4 s send; the PR marker's 5 s `gh pr view` and 3 s post; the MR marker's
|
|
54
55
|
* 2.5 s lookup and 1.5 s post; the ticket markers' and the StopFailure
|
|
55
|
-
* span's 3 s post.
|
|
56
|
+
* span's 3 s post; the commit marker's 5 s of git and 3 s post.
|
|
56
57
|
*/
|
|
57
58
|
export const FEATURES = Object.freeze({
|
|
58
59
|
'plan-snapshot': { module: 'plan-snapshot.mjs', capMs: 4250, optOut: 'markers' },
|
|
59
60
|
'github-pr-marker': { module: 'github-pr-marker.mjs', capMs: 8250, optOut: 'markers' },
|
|
60
61
|
'glab-mr-marker': { module: 'glab-mr-marker.mjs', capMs: 4250, optOut: 'markers' },
|
|
62
|
+
'git-commit-marker': { module: 'git-commit-marker.mjs', capMs: 8250, optOut: 'markers' },
|
|
61
63
|
'linear-ticket-marker': { module: 'linear-ticket-marker.mjs', capMs: 3250, optOut: 'markers' },
|
|
62
64
|
'jira-ticket-marker': { module: 'jira-ticket-marker.mjs', capMs: 3250, optOut: 'markers' },
|
|
63
65
|
'stop-failure': { module: 'stop-failure.mjs', capMs: 3250, optOut: 'markers' },
|
|
@@ -66,17 +68,18 @@ export const FEATURES = Object.freeze({
|
|
|
66
68
|
/**
|
|
67
69
|
* `<host> <Event>[ <route>]` → the features that run there. Mirrors the
|
|
68
70
|
* packaged matchers.json (a test holds the two together). Codex has no `if`
|
|
69
|
-
* filter, so its one Bash route runs
|
|
70
|
-
* check the command themselves.
|
|
71
|
+
* filter, so its one Bash route runs the pull-request markers and the commit
|
|
72
|
+
* marker, which each check the command themselves.
|
|
71
73
|
*/
|
|
72
74
|
export const ROUTES = Object.freeze({
|
|
73
75
|
'claude-code UserPromptSubmit': ['plan-snapshot'],
|
|
74
76
|
'claude-code PostToolUse gh': ['github-pr-marker'],
|
|
75
77
|
'claude-code PostToolUse glab': ['glab-mr-marker'],
|
|
78
|
+
'claude-code PostToolUse git': ['git-commit-marker'],
|
|
76
79
|
'claude-code PostToolUse linear': ['linear-ticket-marker'],
|
|
77
80
|
'claude-code PostToolUse jira': ['jira-ticket-marker'],
|
|
78
81
|
'claude-code StopFailure': ['stop-failure'],
|
|
79
|
-
'codex PostToolUse bash': ['github-pr-marker', 'glab-mr-marker'],
|
|
82
|
+
'codex PostToolUse bash': ['github-pr-marker', 'glab-mr-marker', 'git-commit-marker'],
|
|
80
83
|
'codex PostToolUse linear': ['linear-ticket-marker'],
|
|
81
84
|
'codex PostToolUse jira': ['jira-ticket-marker'],
|
|
82
85
|
})
|
|
@@ -238,6 +241,10 @@ export const dispatch = async (
|
|
|
238
241
|
env: sessionEnv,
|
|
239
242
|
managedDir: markerManagedDir(hookArgv),
|
|
240
243
|
}
|
|
244
|
+
// A Claude Code hook applies its own config dir's organization packs. Codex
|
|
245
|
+
// can't tell which Claude config dir an org delivered to, so it applies
|
|
246
|
+
// every recorded dir's.
|
|
247
|
+
const redactionConfigDir = host === 'claude-code' ? claudeConfigDir(sessionEnv) : undefined
|
|
241
248
|
return Promise.all(
|
|
242
249
|
features.map(async (feature) => {
|
|
243
250
|
const spec = FEATURES[feature]
|
|
@@ -249,7 +256,12 @@ export const dispatch = async (
|
|
|
249
256
|
// Only what the feature is bound to is ever posted.
|
|
250
257
|
const emit = (args) => {
|
|
251
258
|
const body = allowlistPayload(args.body, feature)
|
|
252
|
-
|
|
259
|
+
// Never `redacted: true`: only Kiro's own sender posts a spooled,
|
|
260
|
+
// already-redacted body, so no feature can skip the rules. The
|
|
261
|
+
// organization packs are the session's config dir's (§ redactForSend).
|
|
262
|
+
return hasSpan(body)
|
|
263
|
+
? emitMarker({ ...args, body, redacted: false, redactionConfigDir })
|
|
264
|
+
: Promise.resolve(undefined)
|
|
253
265
|
}
|
|
254
266
|
const run = Promise.resolve(mod.handle(payload, { argv: hookArgv, emit })).then(
|
|
255
267
|
() => 'done',
|
package/hooks/marker-core.mjs
CHANGED
|
@@ -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,
|
|
@@ -1142,13 +1143,57 @@ export const buildMarkerPayload = ({
|
|
|
1142
1143
|
],
|
|
1143
1144
|
})
|
|
1144
1145
|
|
|
1146
|
+
// The redaction rules this process runs, loaded on first use, per Claude
|
|
1147
|
+
// config dir ('' for a hook that can't tell its own): a dispatcher run's
|
|
1148
|
+
// features (and the Kiro sender) share one read of the settings and the rules
|
|
1149
|
+
// file.
|
|
1150
|
+
const hookRedaction = new Map()
|
|
1151
|
+
|
|
1152
|
+
/** Forget the loaded rules so the next send reads them again.
|
|
1153
|
+
* @internal
|
|
1154
|
+
*/
|
|
1155
|
+
export const resetHookRedaction = () => {
|
|
1156
|
+
hookRedaction.clear()
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
/**
|
|
1160
|
+
* The body to send, or `undefined` when it must not leave the machine. The
|
|
1161
|
+
* one place redaction happens: every hook's POST goes through `emitMarker`,
|
|
1162
|
+
* and Kiro's spool write through here too, so a send path cannot skip it.
|
|
1163
|
+
* `configDir` is the Claude config dir the hook runs for, which picks the
|
|
1164
|
+
* organization packs it applies; a hook that can't tell (Cursor's, Kiro's,
|
|
1165
|
+
* Codex's) passes none and applies every recorded dir's. `applied` is whether
|
|
1166
|
+
* rules ran. The `secrets` pack runs unless the developer turned it off (and
|
|
1167
|
+
* recorded no rules file, and no organization packs are recorded), in which
|
|
1168
|
+
* case the body is returned as it is; with rules that cannot be loaded or
|
|
1169
|
+
* run, nothing is sent and one line naming the reason (never span content)
|
|
1170
|
+
* goes to `log`: redaction that was asked for does not silently turn off.
|
|
1171
|
+
*/
|
|
1172
|
+
export const redactForSend = (body, label, log, { configDir } = {}) => {
|
|
1173
|
+
const key = configDir ?? ''
|
|
1174
|
+
if (!hookRedaction.has(key)) hookRedaction.set(key, loadHookRedaction({ configDir }))
|
|
1175
|
+
const loaded = hookRedaction.get(key)
|
|
1176
|
+
if (loaded.state === 'off') return { body, applied: false }
|
|
1177
|
+
if (loaded.state === 'error') {
|
|
1178
|
+
log(`redaction unavailable (${loaded.reason}); not sending ${label}`)
|
|
1179
|
+
return undefined
|
|
1180
|
+
}
|
|
1181
|
+
try {
|
|
1182
|
+
return { body: redactOtlpTraces(loaded.redactor, body), applied: true }
|
|
1183
|
+
} catch (e) {
|
|
1184
|
+
log(`redaction failed (${e?.name ?? 'Error'}); not sending ${label}`)
|
|
1185
|
+
return undefined
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1145
1189
|
/** Whether an HTTP status is a 2xx. */
|
|
1146
1190
|
export const isOkStatus = (status) => Number.isInteger(status) && status >= 200 && status < 300
|
|
1147
1191
|
|
|
1148
1192
|
/**
|
|
1149
1193
|
* POST the OTLP body to the collector, failing open (any error is swallowed).
|
|
1150
1194
|
* Resolves the response's HTTP status, or `undefined` when no response came
|
|
1151
|
-
* (network error, abort at `timeoutMs`)
|
|
1195
|
+
* (network error, abort at `timeoutMs`), and also when § redactForSend holds
|
|
1196
|
+
* the body back. Every outcome is logged, except a
|
|
1152
1197
|
* 2xx when `logOk` is false — a caller that sends on every prompt keeps its
|
|
1153
1198
|
* log to what someone diagnoses and records success itself from the status.
|
|
1154
1199
|
*/
|
|
@@ -1160,14 +1205,23 @@ export const emitMarker = async ({
|
|
|
1160
1205
|
log,
|
|
1161
1206
|
timeoutMs = EMIT_TIMEOUT_MS,
|
|
1162
1207
|
logOk = true,
|
|
1208
|
+
redacted = false,
|
|
1209
|
+
redactionConfigDir,
|
|
1163
1210
|
}) => {
|
|
1211
|
+
// `redacted`: the body already went through § redactForSend (a spooled one).
|
|
1212
|
+
// `redactionConfigDir`: the session's Claude config dir, which the
|
|
1213
|
+
// dispatcher passes for a Claude Code hook.
|
|
1214
|
+
const out = redacted
|
|
1215
|
+
? { body }
|
|
1216
|
+
: redactForSend(body, label, log, { configDir: redactionConfigDir })
|
|
1217
|
+
if (out === undefined) return undefined
|
|
1164
1218
|
const controller = new AbortController()
|
|
1165
1219
|
const timer = setTimeout(() => controller.abort(), timeoutMs)
|
|
1166
1220
|
try {
|
|
1167
1221
|
const res = await fetch(`${endpoint.replace(/\/$/, '')}/v1/traces`, {
|
|
1168
1222
|
method: 'POST',
|
|
1169
1223
|
headers: { 'content-type': 'application/json', authorization: bearer },
|
|
1170
|
-
body: JSON.stringify(body),
|
|
1224
|
+
body: JSON.stringify(out.body),
|
|
1171
1225
|
signal: controller.signal,
|
|
1172
1226
|
})
|
|
1173
1227
|
if (logOk || !isOkStatus(res.status)) log(`posted ${label}: status=${res.status}`)
|
package/hooks/matchers.json
CHANGED
|
@@ -43,11 +43,26 @@
|
|
|
43
43
|
],
|
|
44
44
|
"why": "Built-in tool, exact name, narrowed by the handler's `if` to Bash commands that start with `glab`. The feature still returns immediately unless the command was 'glab mr create' or a POST of 'glab api' to a project's merge_requests endpoint. The feature's own detector also accepts env, sudo and exec wrappers and a path-qualified glab, which `glab *` doesn't match; that miss is accepted rather than widening the `if`."
|
|
45
45
|
},
|
|
46
|
+
{
|
|
47
|
+
"event": "PostToolUse",
|
|
48
|
+
"route": "git",
|
|
49
|
+
"matcher": "Bash",
|
|
50
|
+
"if": "Bash(git *)",
|
|
51
|
+
"features": ["git-commit-marker"],
|
|
52
|
+
"optOut": "markers",
|
|
53
|
+
"hosts": ["claude-code"],
|
|
54
|
+
"dataClasses": [
|
|
55
|
+
"commit sha and first parent sha",
|
|
56
|
+
"changed-file, added-line and removed-line counts",
|
|
57
|
+
"repository-relative file paths with per-file added and removed line counts and a binary flag"
|
|
58
|
+
],
|
|
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
|
+
},
|
|
46
61
|
{
|
|
47
62
|
"event": "PostToolUse",
|
|
48
63
|
"route": "bash",
|
|
49
64
|
"matcher": "Bash",
|
|
50
|
-
"features": ["github-pr-marker", "glab-mr-marker"],
|
|
65
|
+
"features": ["github-pr-marker", "glab-mr-marker", "git-commit-marker"],
|
|
51
66
|
"optOut": "markers",
|
|
52
67
|
"hosts": ["codex"],
|
|
53
68
|
"dataClasses": [
|
|
@@ -55,9 +70,11 @@
|
|
|
55
70
|
"changed-file, added-line and removed-line counts",
|
|
56
71
|
"merge request url, iid and title",
|
|
57
72
|
"instance-namespaced project id",
|
|
58
|
-
"changed-file count"
|
|
73
|
+
"changed-file count",
|
|
74
|
+
"commit sha and first parent sha",
|
|
75
|
+
"repository-relative file paths with per-file added and removed line counts and a binary flag"
|
|
59
76
|
],
|
|
60
|
-
"why": "Codex has no `if` filter, so one Bash group serves
|
|
77
|
+
"why": "Codex has no `if` filter, so one Bash group serves the pull-request markers and the commit marker: the shell entry reads the hook input and starts Node only when it contains `pr`…`create`, `mr`…`create`, `mr`…`new`, `merge_requests` or `git`…`commit` — a superset of what the three features' own detectors accept."
|
|
61
78
|
},
|
|
62
79
|
{
|
|
63
80
|
"event": "PostToolUse",
|