@mainwave/cli 0.13.1 → 0.15.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 +91 -2
- package/dist/adapters/claude-code.js +4 -21
- package/dist/adapters/codex-cli.js +456 -52
- package/dist/adapters/index.js +4 -2
- package/dist/commands.js +15 -3
- package/dist/core/apply.js +85 -19
- package/dist/core/codecs/toml-profile.js +239 -0
- package/dist/core/codex-trust.js +122 -31
- package/dist/core/detect.js +20 -1
- package/dist/core/doctor.js +72 -12
- package/dist/core/entry-script.js +24 -10
- package/dist/core/install-hooks.js +1 -1
- package/dist/core/markers.js +43 -24
- package/dist/core/pinned-use-case.js +2 -2
- package/dist/core/prompts.js +15 -0
- package/dist/core/remove.js +51 -10
- package/dist/core/setup-removal.js +197 -0
- package/dist/core/status.js +15 -7
- package/dist/core/store.js +37 -0
- package/dist/core/wizard.js +87 -4
- package/dist/main.js +41 -3
- package/hooks/codex-turns.mjs +137 -34
- package/hooks/git-commit-marker.mjs +8 -1
- package/hooks/github-pr-marker.mjs +8 -1
- package/hooks/glab-mr-marker.mjs +8 -1
- package/hooks/jira-ticket-marker.mjs +8 -1
- package/hooks/kiro-otlp.mjs +4 -15
- package/hooks/linear-ticket-marker.mjs +8 -1
- package/hooks/marker-core.mjs +126 -23
- package/hooks/matchers.json +1 -1
- package/hooks/plan-snapshot.mjs +9 -21
- package/package.json +7 -7
package/hooks/codex-turns.mjs
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
// across inserts, so each span is sent once, when it ends, with the times the
|
|
16
16
|
// hook process read. A small per-session state file under
|
|
17
17
|
// `~/.mainwave/state/codex-turns/` holds what is open between events; it is
|
|
18
|
-
// deleted once nothing is open.
|
|
18
|
+
// deleted once nothing is open and no abandoned turn awaits a late Stop.
|
|
19
19
|
//
|
|
20
20
|
// Sent: session, turn and agent ids, agent type, working directory, a tool
|
|
21
21
|
// name for a permission wait, and times. Never the prompt, the assistant
|
|
@@ -42,9 +42,10 @@ import {
|
|
|
42
42
|
writeFileSync,
|
|
43
43
|
} from 'node:fs'
|
|
44
44
|
import { homedir } from 'node:os'
|
|
45
|
-
import { isAbsolute, join } from 'node:path'
|
|
45
|
+
import { basename, isAbsolute, join } from 'node:path'
|
|
46
46
|
|
|
47
47
|
import {
|
|
48
|
+
codexConfigFile,
|
|
48
49
|
emitMarker,
|
|
49
50
|
hookFlag,
|
|
50
51
|
loadOtelConfig,
|
|
@@ -52,6 +53,7 @@ import {
|
|
|
52
53
|
markerResourceAttrs,
|
|
53
54
|
markerTool,
|
|
54
55
|
opaqueIdExact,
|
|
56
|
+
stampActivity,
|
|
55
57
|
stringAttr,
|
|
56
58
|
} from './marker-core.mjs'
|
|
57
59
|
import {
|
|
@@ -78,6 +80,12 @@ export const STATE_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000
|
|
|
78
80
|
|
|
79
81
|
const LOCK_WAIT_MS = 1500
|
|
80
82
|
const LOCK_STALE_MS = 10_000
|
|
83
|
+
/** A main SessionStart abandons an open turn only when the turn started at
|
|
84
|
+
* least this long before it. Codex fires SessionStart with the run's first
|
|
85
|
+
* prompt, and their hooks lock in either order milliseconds apart. It is
|
|
86
|
+
* also how long after the abandoning event a Stop or Interrupt for the
|
|
87
|
+
* abandoned turn may read its clock and still end it (§ `applyEvent`). */
|
|
88
|
+
export const SESSION_START_STALE_MS = 5000
|
|
81
89
|
|
|
82
90
|
/** Codex runs the Stop and Interrupt groups synchronously (`codex exec` exits
|
|
83
91
|
* without waiting for an async hook), so it waits for this feature at the
|
|
@@ -99,7 +107,8 @@ export const stateDir = (argv) =>
|
|
|
99
107
|
|
|
100
108
|
/** `abandoned` is the last turn a new prompt or session start abandoned, kept
|
|
101
109
|
* so a Stop or Interrupt for it that locks late can still end it (§
|
|
102
|
-
* `applyEvent`). It
|
|
110
|
+
* `applyEvent`). It keeps the state file until that Stop or a later turn's
|
|
111
|
+
* own end clears it (§ `isEmptyState`). */
|
|
103
112
|
export const emptyState = () => ({ v: 1, turn: null, subagents: {}, waits: {}, abandoned: null })
|
|
104
113
|
|
|
105
114
|
/** A well-formed state read back from disk, else an empty one. */
|
|
@@ -124,6 +133,17 @@ export const parseState = (text) => {
|
|
|
124
133
|
const OPAQUE_NAME_RE = /^[A-Za-z0-9_.:/ -]{1,128}$/
|
|
125
134
|
const opaqueName = (v) => (typeof v === 'string' && OPAQUE_NAME_RE.test(v) ? v : undefined)
|
|
126
135
|
|
|
136
|
+
/** SessionStart's `source` values this feature knows; any other reads as none. */
|
|
137
|
+
const SESSION_SOURCES = new Set(['startup', 'resume', 'clear', 'compact', 'fork'])
|
|
138
|
+
|
|
139
|
+
/** Sources whose SessionStart abandons nothing. A `startup`, `fork` or
|
|
140
|
+
* `clear` session id is new (a fork records its parent thread, and `/clear`
|
|
141
|
+
* starts a new chat), so its state holds only this run's events, which may
|
|
142
|
+
* have locked first. A `compact` start keeps the id inside the same running
|
|
143
|
+
* process, so the open turn is live. Only `resume` (or no known source) can
|
|
144
|
+
* find a killed run's leftovers. */
|
|
145
|
+
const ABANDONS_NOTHING = new Set(['startup', 'fork', 'clear', 'compact'])
|
|
146
|
+
|
|
127
147
|
/** The only fields of the hook input this feature reads. */
|
|
128
148
|
export const readInput = (payload, event) => ({
|
|
129
149
|
event,
|
|
@@ -132,6 +152,7 @@ export const readInput = (payload, event) => ({
|
|
|
132
152
|
agentId: opaqueIdExact(payload?.agent_id),
|
|
133
153
|
agentType: opaqueName(payload?.agent_type),
|
|
134
154
|
toolName: opaqueName(payload?.tool_name),
|
|
155
|
+
source: SESSION_SOURCES.has(payload?.source) ? payload.source : undefined,
|
|
135
156
|
cwd:
|
|
136
157
|
typeof payload?.cwd === 'string' && isAbsolute(payload.cwd) && payload.cwd.length <= 4096
|
|
137
158
|
? payload.cwd
|
|
@@ -146,16 +167,28 @@ export const readInput = (payload, event) => ({
|
|
|
146
167
|
* What closes what (the contract with the collector):
|
|
147
168
|
*
|
|
148
169
|
* - Only an event with no `agent_id` opens or closes a main turn. A new
|
|
149
|
-
* UserPromptSubmit
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
*
|
|
170
|
+
* UserPromptSubmit finds a turn still open and abandons it, ending it at the
|
|
171
|
+
* last event it saw, so a killed Codex never stretches a turn across the
|
|
172
|
+
* developer's absence; one that saw no later event is dropped. A main
|
|
173
|
+
* SessionStart whose `source` is `startup`, `fork`, `clear` or `compact`
|
|
174
|
+
* abandons nothing: the session id is new, or (`compact`) the same running
|
|
175
|
+
* process, so whatever is open is this run's own. Any other main
|
|
176
|
+
* SessionStart (`resume`, or none) abandons the same way
|
|
177
|
+
* only a turn that started more than `SESSION_START_STALE_MS` before it:
|
|
178
|
+
* Codex fires it together with the run's first UserPromptSubmit, so a turn
|
|
179
|
+
* just opened may be that prompt's own, and the prompt abandons a stale
|
|
180
|
+
* one anyway.
|
|
181
|
+
* - A main event whose turn id names another turn than the open one belongs
|
|
182
|
+
* to the next run, locking before that run's prompt. It neither extends
|
|
183
|
+
* the open turn's last event nor ends a wait under it, and as a
|
|
184
|
+
* PermissionRequest it opens no wait.
|
|
153
185
|
* - A permission wait ends at the first later PostToolUse, PreModelRequest,
|
|
154
|
-
* PermissionRequest, Stop, Interrupt
|
|
155
|
-
*
|
|
156
|
-
* its turn). PreToolUse is not one: Codex
|
|
157
|
-
* never ends that call's wait.
|
|
158
|
-
* the PreModelRequest before it
|
|
186
|
+
* PermissionRequest, Stop, Interrupt or SubagentStop of the same session
|
|
187
|
+
* and agent, or a subagent's own SessionStart (a main SessionStart ends a
|
|
188
|
+
* main wait by abandoning its stale turn). PreToolUse is not one: Codex
|
|
189
|
+
* runs it before the approval, so it never ends that call's wait.
|
|
190
|
+
* PostModelRequest is not one either, because the PreModelRequest before it
|
|
191
|
+
* already did.
|
|
159
192
|
* - A turn that ends takes its open waits (end = the turn's end) and any
|
|
160
193
|
* subagent still open (abandoned, end = its last event) with it.
|
|
161
194
|
* - Hooks read the clock before they take the state lock, so events can reach
|
|
@@ -165,14 +198,18 @@ export const readInput = (payload, event) => ({
|
|
|
165
198
|
* that turn's Stop. The reverse, an async UserPromptSubmit locking after its
|
|
166
199
|
* own synchronous Stop, needs a turn that finishes faster than Node starts.
|
|
167
200
|
* These guards cover the reorderings seen or reviewed, not every possible
|
|
168
|
-
* one: a
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
201
|
+
* one: a UserPromptSubmit abandons only a turn that started earlier; a
|
|
202
|
+
* `startup`, `fork`, `clear` or `compact` SessionStart abandons nothing,
|
|
203
|
+
* and any other only a subagent that started earlier or a turn that
|
|
204
|
+
* started well before; a Stop or Interrupt without a turn id closes only a
|
|
205
|
+
* turn that started no later than itself; a Stop or Interrupt whose turn id names
|
|
206
|
+
* the turn last abandoned, and that read its clock no more than
|
|
207
|
+
* `SESSION_START_STALE_MS` after the abandoning event, sends that turn
|
|
208
|
+
* again with its real end reason and end (the span ids are the same, so
|
|
209
|
+
* the row replaces the abandoned one); a PermissionRequest or SubagentStart
|
|
210
|
+
* older than the open turn, or with none open, is dropped; and an event ends only a wait that started no
|
|
211
|
+
* later than itself, except that a turn's or subagent's own end takes its
|
|
212
|
+
* waits.
|
|
176
213
|
*/
|
|
177
214
|
export const applyEvent = (state, input, now) => {
|
|
178
215
|
const st = structuredClone(state)
|
|
@@ -229,8 +266,12 @@ export const applyEvent = (state, input, now) => {
|
|
|
229
266
|
}
|
|
230
267
|
st.turn = null
|
|
231
268
|
if (reason === 'abandoned') {
|
|
232
|
-
st.abandoned = { turnId: t.turnId, startMs: t.startMs, cwd: t.cwd }
|
|
269
|
+
st.abandoned = { turnId: t.turnId, startMs: t.startMs, cwd: t.cwd, atMs: now }
|
|
233
270
|
if (t.lastMs <= t.startMs) return
|
|
271
|
+
} else {
|
|
272
|
+
// A later turn ended normally, so the abandoned one's Stop, which fires
|
|
273
|
+
// before that turn's prompt, is long past.
|
|
274
|
+
st.abandoned = null
|
|
234
275
|
}
|
|
235
276
|
out.push({
|
|
236
277
|
kind: 'turn',
|
|
@@ -243,10 +284,17 @@ export const applyEvent = (state, input, now) => {
|
|
|
243
284
|
})
|
|
244
285
|
}
|
|
245
286
|
|
|
287
|
+
// A main event naming another turn is the next run's, locking before that
|
|
288
|
+
// run's prompt: it is no work inside the open turn and ends none of its
|
|
289
|
+
// waits.
|
|
290
|
+
const mismatched = () =>
|
|
291
|
+
st.turn !== null && agentId === undefined && turnId !== undefined && turnId !== st.turn.turnId
|
|
292
|
+
const ofOpenTurn = () => st.turn !== null && !mismatched()
|
|
293
|
+
|
|
246
294
|
// A subagent's event is also work inside the open main turn, so a turn
|
|
247
295
|
// abandoned after a kill ends at its subagents' last event too.
|
|
248
296
|
const touch = () => {
|
|
249
|
-
if (
|
|
297
|
+
if (ofOpenTurn()) st.turn.lastMs = Math.max(st.turn.lastMs, now)
|
|
250
298
|
if (agentId !== undefined && st.subagents[agentId] !== undefined) {
|
|
251
299
|
st.subagents[agentId].lastMs = Math.max(st.subagents[agentId].lastMs, now)
|
|
252
300
|
}
|
|
@@ -281,8 +329,21 @@ export const applyEvent = (state, input, now) => {
|
|
|
281
329
|
// With no turn id, only a turn that started no later than this event
|
|
282
330
|
// can be the one it ends.
|
|
283
331
|
if (st.turn.startMs <= now) closeTurn(now, reason, event)
|
|
284
|
-
} else if (
|
|
332
|
+
} else if (
|
|
333
|
+
a !== null &&
|
|
334
|
+
turnId === a.turnId &&
|
|
335
|
+
a.startMs <= now &&
|
|
336
|
+
// A record written by an earlier release has no `atMs`; its late
|
|
337
|
+
// Stop is refused, so the abandoned row stays.
|
|
338
|
+
Number.isFinite(a.atMs) &&
|
|
339
|
+
now <= a.atMs + SESSION_START_STALE_MS
|
|
340
|
+
) {
|
|
285
341
|
// The turn a queued prompt abandoned before this event took the lock.
|
|
342
|
+
// Codex submits that prompt the moment the turn ends, so the two
|
|
343
|
+
// read their clocks within a Node start of each other. A later one
|
|
344
|
+
// would stretch the turn across a gap, so the abandoned row stays.
|
|
345
|
+
// A Stop that Codex replayed right after a resume would still pass;
|
|
346
|
+
// no replay has been seen, and no clock tells the two apart.
|
|
286
347
|
st.abandoned = null
|
|
287
348
|
out.push({
|
|
288
349
|
kind: 'turn',
|
|
@@ -299,8 +360,9 @@ export const applyEvent = (state, input, now) => {
|
|
|
299
360
|
case 'PermissionRequest':
|
|
300
361
|
touch()
|
|
301
362
|
// None open, or one that started after this request: the request
|
|
302
|
-
// belongs to an earlier turn.
|
|
303
|
-
|
|
363
|
+
// belongs to an earlier turn. A main request naming another turn
|
|
364
|
+
// belongs to a later one whose prompt has not locked yet.
|
|
365
|
+
if (!ofOpenTurn() || now < st.turn.startMs) break
|
|
304
366
|
if (st.waits[key] !== undefined && now < st.waits[key].startMs) break
|
|
305
367
|
closeWait(key, now, event)
|
|
306
368
|
st.waits[key] = {
|
|
@@ -314,7 +376,7 @@ export const applyEvent = (state, input, now) => {
|
|
|
314
376
|
case 'PostToolUse':
|
|
315
377
|
case 'PreModelRequest':
|
|
316
378
|
touch()
|
|
317
|
-
closeWait(key, now, event)
|
|
379
|
+
if (!mismatched()) closeWait(key, now, event)
|
|
318
380
|
break
|
|
319
381
|
case 'SubagentStart':
|
|
320
382
|
if (agentId === undefined) break
|
|
@@ -348,17 +410,27 @@ export const applyEvent = (state, input, now) => {
|
|
|
348
410
|
closeWait(key, now, event)
|
|
349
411
|
break
|
|
350
412
|
}
|
|
351
|
-
//
|
|
352
|
-
|
|
413
|
+
// A startup, fork or clear session id is new, and a compact start is
|
|
414
|
+
// the same running process, so everything in its state came from this
|
|
415
|
+
// run's own events, which may have locked first: nothing is left over
|
|
416
|
+
// to abandon, whatever the clocks say.
|
|
417
|
+
if (ABANDONS_NOTHING.has(input.source)) break
|
|
418
|
+
// Codex fires SessionStart with the run's first prompt, both hooks
|
|
419
|
+
// async, and either may read the clock first, so a turn that started
|
|
420
|
+
// just before may be this run's own new one: it is left to the next
|
|
421
|
+
// UserPromptSubmit, which abandons it if it is an earlier run's. One
|
|
422
|
+
// that started well before is an earlier run's.
|
|
423
|
+
if (st.turn !== null && st.turn.startMs < now - SESSION_START_STALE_MS) {
|
|
353
424
|
closeTurn(st.turn.lastMs, 'abandoned', event)
|
|
354
425
|
}
|
|
426
|
+
if (st.turn !== null) break
|
|
355
427
|
for (const id of Object.keys(st.subagents)) {
|
|
356
428
|
if (st.subagents[id].startMs < now) {
|
|
357
429
|
closeSubagent(id, st.subagents[id].lastMs, 'abandoned', event)
|
|
358
430
|
}
|
|
359
431
|
}
|
|
360
432
|
// Waits live only inside a turn; with none open, any left are stale.
|
|
361
|
-
|
|
433
|
+
st.waits = {}
|
|
362
434
|
break
|
|
363
435
|
default:
|
|
364
436
|
break
|
|
@@ -489,9 +561,13 @@ const writeAtomic = (file, text) => {
|
|
|
489
561
|
renameSync(tmp, file)
|
|
490
562
|
}
|
|
491
563
|
|
|
492
|
-
/** Nothing open
|
|
564
|
+
/** Nothing open and no abandoned turn a late Stop may still end: the state
|
|
565
|
+
* file can go (no file reads back as this). */
|
|
493
566
|
export const isEmptyState = (s) =>
|
|
494
|
-
s.turn === null &&
|
|
567
|
+
s.turn === null &&
|
|
568
|
+
s.abandoned === null &&
|
|
569
|
+
Object.keys(s.subagents).length === 0 &&
|
|
570
|
+
Object.keys(s.waits).length === 0
|
|
495
571
|
|
|
496
572
|
/** Delete every state file nobody has touched for STATE_MAX_AGE_MS. */
|
|
497
573
|
export const sweepStale = (dir, nowMs) => {
|
|
@@ -537,6 +613,7 @@ export const handle = async (
|
|
|
537
613
|
|
|
538
614
|
const sync = Object.hasOwn(SYNC_CAPS, name) ? SYNC_CAPS[name] : undefined
|
|
539
615
|
let closed = []
|
|
616
|
+
let abandonedId
|
|
540
617
|
const locked = await withLock(
|
|
541
618
|
file,
|
|
542
619
|
() => {
|
|
@@ -548,6 +625,7 @@ export const handle = async (
|
|
|
548
625
|
}
|
|
549
626
|
const next = applyEvent(before, input, t)
|
|
550
627
|
closed = next.closed
|
|
628
|
+
abandonedId = next.state.abandoned?.turnId
|
|
551
629
|
if (isEmptyState(next.state)) {
|
|
552
630
|
try {
|
|
553
631
|
unlinkSync(file)
|
|
@@ -565,15 +643,38 @@ export const handle = async (
|
|
|
565
643
|
return
|
|
566
644
|
}
|
|
567
645
|
if (name === 'SessionStart') sweepStale(dir, t)
|
|
646
|
+
// A main Stop or Interrupt that names a turn and found none to end can
|
|
647
|
+
// mean a turn went unsent: log it, so a lost turn leaves a trace. One with
|
|
648
|
+
// no turn id may belong to a turn this run already sent, and one that names
|
|
649
|
+
// the turn just recorded as abandoned has had that turn sent, so neither
|
|
650
|
+
// logs.
|
|
651
|
+
if (
|
|
652
|
+
(name === 'Stop' || name === 'Interrupt') &&
|
|
653
|
+
input.agentId === undefined &&
|
|
654
|
+
input.turnId !== undefined &&
|
|
655
|
+
input.turnId !== abandonedId &&
|
|
656
|
+
!closed.some((c) => c.kind === 'turn')
|
|
657
|
+
) {
|
|
658
|
+
log(`skip ${name}: no open turn`)
|
|
659
|
+
}
|
|
568
660
|
if (closed.length === 0) return
|
|
569
661
|
|
|
570
662
|
const tool = markerTool(argv)
|
|
571
|
-
const { endpoint, bearer, resourceAttrs } = loadOtelConfig(
|
|
663
|
+
const { endpoint, bearer, resourceAttrs } = loadOtelConfig(
|
|
664
|
+
undefined,
|
|
665
|
+
{},
|
|
666
|
+
tool,
|
|
667
|
+
undefined,
|
|
668
|
+
{},
|
|
669
|
+
argv,
|
|
670
|
+
)
|
|
572
671
|
if (!endpoint || !bearer) {
|
|
573
|
-
|
|
672
|
+
const file = codexConfigFile(argv)
|
|
673
|
+
const where = file === undefined ? 'no valid --codex-profile name' : basename(file)
|
|
674
|
+
log(`skip ${closed.length} codex span(s): no Mainwave collector in ${where}`)
|
|
574
675
|
return
|
|
575
676
|
}
|
|
576
|
-
await emit({
|
|
677
|
+
const status = await emit({
|
|
577
678
|
endpoint,
|
|
578
679
|
bearer,
|
|
579
680
|
body: buildBody(closed, resourceAttrs),
|
|
@@ -581,4 +682,6 @@ export const handle = async (
|
|
|
581
682
|
log,
|
|
582
683
|
...(sync ? { timeoutMs: sync.postTimeoutMs } : {}),
|
|
583
684
|
})
|
|
685
|
+
// The log also takes skips, so only this sidecar says a turn was delivered.
|
|
686
|
+
stampActivity(status, argv, CODEX_TURNS_LOG)
|
|
584
687
|
}
|
|
@@ -353,7 +353,14 @@ export const handle = async (payload, { argv = process.argv, emit = emitMarker }
|
|
|
353
353
|
const tool = markerTool(argv)
|
|
354
354
|
const managedDir = markerManagedDir(argv)
|
|
355
355
|
const env = markerSessionEnv(tool, payload)
|
|
356
|
-
const { endpoint, bearer, resourceAttrs } = loadOtelConfig(
|
|
356
|
+
const { endpoint, bearer, resourceAttrs } = loadOtelConfig(
|
|
357
|
+
projectDir,
|
|
358
|
+
env,
|
|
359
|
+
tool,
|
|
360
|
+
managedDir,
|
|
361
|
+
{},
|
|
362
|
+
argv,
|
|
363
|
+
)
|
|
357
364
|
log(
|
|
358
365
|
`commits=${shas.length} endpoint=${endpoint ? 'set' : 'MISSING'} ` +
|
|
359
366
|
`bearer=${bearer ? 'set' : 'MISSING'} ucid=${resourceAttrs['mainwave.ucid'] || 'MISSING'}`,
|
|
@@ -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(
|
|
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'}`,
|
package/hooks/glab-mr-marker.mjs
CHANGED
|
@@ -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(
|
|
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(
|
|
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'}`,
|
package/hooks/kiro-otlp.mjs
CHANGED
|
@@ -44,17 +44,16 @@ import { dirname, join, resolve } from 'node:path'
|
|
|
44
44
|
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
45
45
|
|
|
46
46
|
import {
|
|
47
|
+
activityMarkerPath,
|
|
47
48
|
bearerFromHeaders,
|
|
48
49
|
buildMarkerPayload,
|
|
49
50
|
emitMarker,
|
|
50
|
-
ensureLogDir,
|
|
51
51
|
hookFlag,
|
|
52
|
-
isOkStatus,
|
|
53
52
|
markerLog,
|
|
54
|
-
markerLogPath,
|
|
55
53
|
parseEnvFile,
|
|
56
54
|
parseKv,
|
|
57
55
|
redactForSend,
|
|
56
|
+
stampActivity,
|
|
58
57
|
} from './marker-core.mjs'
|
|
59
58
|
|
|
60
59
|
const LOG_NAME = 'kiro.log'
|
|
@@ -317,17 +316,7 @@ export const spanForPayload = (payload, config, { argv = [], env = process.env }
|
|
|
317
316
|
}
|
|
318
317
|
|
|
319
318
|
/** The sidecar whose mtime is the last delivered span — doctor reads it. */
|
|
320
|
-
export const activityPath = (argv) =>
|
|
321
|
-
|
|
322
|
-
const markDelivered = (argv) => {
|
|
323
|
-
try {
|
|
324
|
-
const p = activityPath(argv)
|
|
325
|
-
ensureLogDir(p)
|
|
326
|
-
writeFileSync(p, `${new Date().toISOString()}\n`, { mode: 0o600 })
|
|
327
|
-
} catch {
|
|
328
|
-
// best-effort, like every log write
|
|
329
|
-
}
|
|
330
|
-
}
|
|
319
|
+
export const activityPath = (argv) => activityMarkerPath(argv, LOG_NAME)
|
|
331
320
|
|
|
332
321
|
/**
|
|
333
322
|
* Read stdin up to `max` bytes, keeping the first `head` bytes of a payload
|
|
@@ -559,7 +548,7 @@ export const send = async (argv = process.argv) => {
|
|
|
559
548
|
// Redacted when it was spooled; one spooled with no rules is checked now.
|
|
560
549
|
redacted: spooled.redacted === true,
|
|
561
550
|
})
|
|
562
|
-
|
|
551
|
+
stampActivity(status, argv, LOG_NAME)
|
|
563
552
|
}
|
|
564
553
|
|
|
565
554
|
// Run only as a script, not when imported by tests.
|
|
@@ -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(
|
|
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'}`,
|
package/hooks/marker-core.mjs
CHANGED
|
@@ -112,6 +112,27 @@ export const markerLogPath = (argv, defaultName) =>
|
|
|
112
112
|
export const markerLog = (argv, defaultName, maxBytes = LOG_MAX_BYTES) =>
|
|
113
113
|
makeLog(markerLogPath(argv, defaultName), maxBytes)
|
|
114
114
|
|
|
115
|
+
/** The activity sidecar beside a hook's log: `<log>.ok`, one timestamp line a
|
|
116
|
+
* 2xx post rewrites, never appended to. Its mtime is the last successful
|
|
117
|
+
* send, which `doctor` reports as activity; the log's own mtime also moves on
|
|
118
|
+
* a skip or a failure, so it can't say whether anything was delivered. */
|
|
119
|
+
export const activityMarkerPath = (argv, defaultName) => `${markerLogPath(argv, defaultName)}.ok`
|
|
120
|
+
|
|
121
|
+
/** Rewrite the activity sidecar (§ `activityMarkerPath`) when `status` is a
|
|
122
|
+
* 2xx; anything else, no response or a non-2xx, leaves it alone. Best-effort
|
|
123
|
+
* like the log: true when it was written. */
|
|
124
|
+
export const stampActivity = (status, argv, defaultName, now = new Date()) => {
|
|
125
|
+
if (!isOkStatus(status)) return false
|
|
126
|
+
try {
|
|
127
|
+
const path = activityMarkerPath(argv, defaultName)
|
|
128
|
+
ensureLogDir(path)
|
|
129
|
+
writeFileSync(path, `${now.toISOString()}\n`, { mode: 0o600 })
|
|
130
|
+
return true
|
|
131
|
+
} catch {
|
|
132
|
+
return false
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
115
136
|
/**
|
|
116
137
|
* The project root whose `.claude/` settings Claude Code loaded for this
|
|
117
138
|
* session: `CLAUDE_PROJECT_DIR`, which Claude Code sets for every hook. Not the
|
|
@@ -786,18 +807,61 @@ export const markerOauthAccountPath = (tool, opts = {}) => {
|
|
|
786
807
|
return claudeAccountPath(opts.home, configDir)
|
|
787
808
|
}
|
|
788
809
|
|
|
789
|
-
/**
|
|
790
|
-
*
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
810
|
+
/** A TOML key with its quotes and surrounding whitespace removed, so
|
|
811
|
+
* `otel.exporter."otlp-http"` and `otel.exporter.otlp-http` compare equal. */
|
|
812
|
+
const plainTomlKey = (key) =>
|
|
813
|
+
key
|
|
814
|
+
.replace(/"/g, '')
|
|
815
|
+
.replace(/\s*\.\s*/g, '.')
|
|
816
|
+
.trim()
|
|
817
|
+
|
|
818
|
+
const TOML_HEADER_START_RE = /^\s*\[/
|
|
819
|
+
const TOML_TABLE_RE = /^\s*\[([^[\]]+)\]\s*(?:#.*)?$/
|
|
820
|
+
const TOML_STRING_RE = /^\s*("[^"]+"|[A-Za-z0-9_-]+)\s*=\s*"([^"]*)"/
|
|
821
|
+
const TOML_INLINE_RE = /^\s*headers\s*=\s*\{(.*)\}\s*(?:#.*)?$/
|
|
822
|
+
const TOML_PAIR_RE = /("[^"]+"|[A-Za-z0-9_-]+)\s*=\s*"([^"]*)"/g
|
|
823
|
+
|
|
824
|
+
/**
|
|
825
|
+
* The `[otel.*]` tables of a Codex `config.toml`, in file order, each as
|
|
826
|
+
* `{ endpoint, headers }`: its `endpoint = "…"` string and its one-line
|
|
827
|
+
* inline `headers = { … }` table, the shape setup and the manual-setup page
|
|
828
|
+
* write. Only the string values the markers read are kept; anything else in
|
|
829
|
+
* the file is skipped, so an unexpected shape yields no table, never a
|
|
830
|
+
* guess.
|
|
831
|
+
*/
|
|
832
|
+
const codexOtelTables = (text) => {
|
|
833
|
+
const tables = []
|
|
834
|
+
let current
|
|
835
|
+
for (const line of (text ?? '').split(/\r?\n/)) {
|
|
836
|
+
if (TOML_HEADER_START_RE.test(line)) {
|
|
837
|
+
// Any header, `[t]` or `[[t]]`, ends the table before it. Only a
|
|
838
|
+
// single-bracket `otel.*` header opens a new one.
|
|
839
|
+
const header = TOML_TABLE_RE.exec(line)
|
|
840
|
+
const name = header && plainTomlKey(header[1])
|
|
841
|
+
current = name?.startsWith('otel.') ? { endpoint: undefined, headers: {} } : undefined
|
|
842
|
+
if (current) tables.push(current)
|
|
843
|
+
continue
|
|
844
|
+
}
|
|
845
|
+
if (!current) continue
|
|
846
|
+
const inline = TOML_INLINE_RE.exec(line)
|
|
847
|
+
if (inline) {
|
|
848
|
+
for (const [, k, v] of inline[1].matchAll(TOML_PAIR_RE)) current.headers[plainTomlKey(k)] = v
|
|
849
|
+
continue
|
|
850
|
+
}
|
|
851
|
+
const pair = TOML_STRING_RE.exec(line)
|
|
852
|
+
if (pair && plainTomlKey(pair[1]) === 'endpoint') current.endpoint = pair[2]
|
|
853
|
+
}
|
|
854
|
+
return tables
|
|
799
855
|
}
|
|
800
856
|
|
|
857
|
+
/** Whether an exporter table is one Mainwave setup wrote: it names an
|
|
858
|
+
* endpoint and an `Authorization` header, and its headers carry the
|
|
859
|
+
* `X-Mainwave-Source` tag setup (and the manual-setup snippet) puts in every
|
|
860
|
+
* Codex exporter table. The collector host can't decide it: an organization
|
|
861
|
+
* may run its collector on its own domain. */
|
|
862
|
+
const isMainwaveExporter = (table) =>
|
|
863
|
+
Boolean(table.endpoint && table.headers.Authorization && table.headers['X-Mainwave-Source'])
|
|
864
|
+
|
|
801
865
|
/** Strip a per-signal OTLP suffix (`/v1/logs` …) back to the base endpoint the
|
|
802
866
|
* emit re-adds `/v1/traces` to. Codex writes per-signal endpoints; Claude Code's
|
|
803
867
|
* OTEL_EXPORTER_OTLP_ENDPOINT is already the base, so this is Codex-only. */
|
|
@@ -820,8 +884,31 @@ export const codexHomeDir = (env = process.env) => {
|
|
|
820
884
|
}
|
|
821
885
|
}
|
|
822
886
|
|
|
887
|
+
/** A Codex profile name a hook command may pass: the characters setup allows. */
|
|
888
|
+
const CODEX_PROFILE_RE = /^[A-Za-z0-9_-]{1,64}$/
|
|
889
|
+
|
|
890
|
+
/**
|
|
891
|
+
* The one Codex config file a hook reads its collector from. Without
|
|
892
|
+
* `--codex-profile` it is `config.toml` in the Codex home. With
|
|
893
|
+
* `--codex-profile <name>` it is `<name>.config.toml` there, the file Codex
|
|
894
|
+
* loads only under `--profile <name>`: nothing in the hook's environment or
|
|
895
|
+
* input names the active profile, so the hook command carries it. A missing or
|
|
896
|
+
* unsafe name gives `undefined` (send nothing), never a path built from it and
|
|
897
|
+
* never a fall back to `config.toml`, whose collector belongs to sessions run
|
|
898
|
+
* without that profile.
|
|
899
|
+
*/
|
|
900
|
+
export const codexConfigFile = (argv, env = process.env) => {
|
|
901
|
+
const i = (argv ?? []).indexOf('--codex-profile')
|
|
902
|
+
if (i === -1) return join(codexHomeDir(env), 'config.toml')
|
|
903
|
+
const name = argv[i + 1]
|
|
904
|
+
return typeof name === 'string' && CODEX_PROFILE_RE.test(name)
|
|
905
|
+
? join(codexHomeDir(env), `${name}.config.toml`)
|
|
906
|
+
: undefined
|
|
907
|
+
}
|
|
908
|
+
|
|
823
909
|
/**
|
|
824
|
-
* OTLP config from
|
|
910
|
+
* OTLP config from the hook's Codex config file (§ `codexConfigFile`): the
|
|
911
|
+
* global `config.toml`, or the profile file the hook command names. Codex carries
|
|
825
912
|
* tenancy on HTTP headers rather than OTEL_RESOURCE_ATTRIBUTES, so the UCID is the
|
|
826
913
|
* `X-Mainwave-UCID` header value and the bearer is the `Authorization` header — the
|
|
827
914
|
* marker re-emits the UCID as a `mainwave.ucid` resource attr, which the collector
|
|
@@ -829,18 +916,25 @@ export const codexHomeDir = (env = process.env) => {
|
|
|
829
916
|
* developer configured one explicitly; by default the collector derives it from
|
|
830
917
|
* the developer's email, so the `X-Mainwave-User-Email` header is re-emitted as
|
|
831
918
|
* the `mainwave.user_email` resource attr it promotes that header to — without
|
|
832
|
-
* it a Codex marker would carry no identity at all.
|
|
833
|
-
*
|
|
919
|
+
* it a Codex marker would carry no identity at all.
|
|
920
|
+
*
|
|
921
|
+
* Every value comes from one exporter table, the last that is Mainwave's
|
|
922
|
+
* (§ `isMainwaveExporter`). The `[otel]` block may belong to another
|
|
923
|
+
* vendor, and its endpoint and bearer must never receive a Mainwave span,
|
|
924
|
+
* so with no Mainwave table every field is undefined and the hook skips
|
|
925
|
+
* (fail-open), never a broken emit.
|
|
834
926
|
*/
|
|
835
|
-
export const codexOtelConfig = (readFile = readFileSafe, env = process.env) => {
|
|
836
|
-
//
|
|
927
|
+
export const codexOtelConfig = (readFile = readFileSafe, env = process.env, argv = []) => {
|
|
928
|
+
// User-level config only: Codex 0.130+ drops `otel` keys from a project's
|
|
837
929
|
// `.codex/config.toml` itself, so a checked-out repository can't redirect
|
|
838
930
|
// its telemetry, and the markers follow the same rule.
|
|
839
|
-
const
|
|
840
|
-
const
|
|
841
|
-
const
|
|
842
|
-
const
|
|
843
|
-
const
|
|
931
|
+
const file = codexConfigFile(argv, env)
|
|
932
|
+
const text = file === undefined ? '' : readFile(file)
|
|
933
|
+
const table = codexOtelTables(text).filter(isMainwaveExporter).at(-1)
|
|
934
|
+
const endpoint = stripSignalSuffix(table?.endpoint)
|
|
935
|
+
const bearer = table?.headers.Authorization
|
|
936
|
+
const ucid = table?.headers['X-Mainwave-UCID']
|
|
937
|
+
const userEmail = table?.headers['X-Mainwave-User-Email']
|
|
844
938
|
return {
|
|
845
939
|
endpoint: endpoint || undefined,
|
|
846
940
|
bearer: bearer || undefined,
|
|
@@ -852,11 +946,19 @@ export const codexOtelConfig = (readFile = readFileSafe, env = process.env) => {
|
|
|
852
946
|
}
|
|
853
947
|
|
|
854
948
|
/** Resolve OTLP config for the marker emit. `tool` selects the source format:
|
|
855
|
-
* 'codex' reads
|
|
949
|
+
* 'codex' reads the Codex config file the hook's `argv` selects (§
|
|
950
|
+
* `codexConfigFile`); the default reads Claude Code's settings.json env.
|
|
856
951
|
* `readers` (§ `onceReaders`) lets a caller share its file reads. */
|
|
857
|
-
export const loadOtelConfig = (
|
|
952
|
+
export const loadOtelConfig = (
|
|
953
|
+
cwd,
|
|
954
|
+
processEnv,
|
|
955
|
+
tool = 'claude-code',
|
|
956
|
+
managedDir,
|
|
957
|
+
readers = {},
|
|
958
|
+
argv = [],
|
|
959
|
+
) =>
|
|
858
960
|
tool === 'codex'
|
|
859
|
-
? codexOtelConfig()
|
|
961
|
+
? codexOtelConfig(undefined, undefined, argv)
|
|
860
962
|
: resolveOtelConfig(
|
|
861
963
|
settingsLayers(cwd, processEnv, readers.readFile, readers.listDir, managedDir),
|
|
862
964
|
processEnv,
|
|
@@ -1326,6 +1428,7 @@ export const sendClaudeEvent = async ({
|
|
|
1326
1428
|
tool,
|
|
1327
1429
|
managedDir,
|
|
1328
1430
|
readers,
|
|
1431
|
+
argv,
|
|
1329
1432
|
)
|
|
1330
1433
|
if (!endpoint || !bearer) {
|
|
1331
1434
|
log(`skip ${label}: collector endpoint/bearer not found in settings or env`)
|