@gtheys/pi-interactive-subagents 0.0.0-stage → 0.2.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 (46) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +462 -2
  3. package/agents/claude-code.md +23 -0
  4. package/agents/planner.md +569 -0
  5. package/agents/reviewer.md +152 -0
  6. package/agents/scout.md +112 -0
  7. package/agents/visual-tester.md +202 -0
  8. package/agents/worker.md +109 -0
  9. package/config.json.example +6 -0
  10. package/config.schema.json +71 -0
  11. package/package.json +59 -4
  12. package/pi-extension/subagents/activity.ts +730 -0
  13. package/pi-extension/subagents/asks.ts +234 -0
  14. package/pi-extension/subagents/assignments.ts +395 -0
  15. package/pi-extension/subagents/config.ts +191 -0
  16. package/pi-extension/subagents/control.ts +172 -0
  17. package/pi-extension/subagents/dataroot.ts +52 -0
  18. package/pi-extension/subagents/delivered.ts +81 -0
  19. package/pi-extension/subagents/flow-env.ts +112 -0
  20. package/pi-extension/subagents/herdr.ts +323 -0
  21. package/pi-extension/subagents/inbox.ts +213 -0
  22. package/pi-extension/subagents/index.ts +5003 -0
  23. package/pi-extension/subagents/lock.ts +318 -0
  24. package/pi-extension/subagents/peer-bindings.ts +175 -0
  25. package/pi-extension/subagents/peers.ts +401 -0
  26. package/pi-extension/subagents/plan-skill.md +219 -0
  27. package/pi-extension/subagents/plugin/.claude-plugin/plugin.json +5 -0
  28. package/pi-extension/subagents/plugin/hooks/hooks.json +15 -0
  29. package/pi-extension/subagents/plugin/hooks/on-stop.sh +68 -0
  30. package/pi-extension/subagents/retention.ts +918 -0
  31. package/pi-extension/subagents/retirement.ts +216 -0
  32. package/pi-extension/subagents/session.ts +194 -0
  33. package/pi-extension/subagents/snapshot.ts +151 -0
  34. package/pi-extension/subagents/spawn.ts +237 -0
  35. package/pi-extension/subagents/spawnseq.ts +116 -0
  36. package/pi-extension/subagents/state-events.ts +152 -0
  37. package/pi-extension/subagents/status.ts +886 -0
  38. package/pi-extension/subagents/store.ts +245 -0
  39. package/pi-extension/subagents/subagent-done.ts +1452 -0
  40. package/pi-extension/subagents/surface.ts +365 -0
  41. package/pi-extension/subagents/task-composition.ts +113 -0
  42. package/pi-extension/subagents/tool-effects.ts +203 -0
  43. package/pi-extension/subagents/usage.ts +330 -0
  44. package/pi-extension/subagents/watermark.ts +126 -0
  45. package/pi-extension/subagents/worktrees.ts +147 -0
  46. package/skills/subagent-orchestration/SKILL.md +57 -0
@@ -0,0 +1,730 @@
1
+ import {
2
+ existsSync,
3
+ mkdirSync,
4
+ readFileSync,
5
+ renameSync,
6
+ unlinkSync,
7
+ writeFileSync,
8
+ } from 'node:fs'
9
+ import { dirname, join } from 'node:path'
10
+
11
+ // AIDEV-NOTE: 'blocked' (PMR-WXG0) = child called ask_owner and is alive at
12
+ // its prompt waiting for the owner's answer. Distinct from 'waiting' so the
13
+ // parent's status can show blocked and the stall rules can tell them apart.
14
+ export type SubagentActivityPhase =
15
+ | 'starting'
16
+ | 'active'
17
+ | 'waiting'
18
+ | 'blocked'
19
+ | 'retiring'
20
+ | 'done'
21
+ export type SubagentActivityScope =
22
+ | 'agent'
23
+ | 'turn'
24
+ | 'provider'
25
+ | 'streaming'
26
+ | 'tool'
27
+
28
+ export type SubagentActivityEvent =
29
+ | 'session_start'
30
+ | 'input'
31
+ | 'before_agent_start'
32
+ | 'agent_start'
33
+ | 'agent_end'
34
+ | 'turn_start'
35
+ | 'turn_end'
36
+ | 'before_provider_request'
37
+ | 'after_provider_response'
38
+ | 'message_update'
39
+ | 'tool_execution_start'
40
+ | 'tool_call'
41
+ | 'tool_execution_update'
42
+ | 'tool_result'
43
+ | 'tool_execution_end'
44
+ | 'caller_ping'
45
+ | 'ask_owner'
46
+ | 'context_retiring'
47
+ | 'subagent_done'
48
+ | 'session_shutdown'
49
+
50
+ export interface SubagentActivityState {
51
+ version: 1
52
+ runningChildId: string
53
+ createdAt: number
54
+ updatedAt: number
55
+ sequence: number
56
+ latestEvent: SubagentActivityEvent
57
+ phase: SubagentActivityPhase
58
+ agentActive: boolean
59
+ turnActive: boolean
60
+ providerActive: boolean
61
+ toolActive: boolean
62
+ activeScope?: SubagentActivityScope
63
+ activeSince?: number
64
+ waitingSince?: number
65
+ blockedSince?: number
66
+ turnIndex?: number
67
+ messageEventType?: string
68
+ toolCallId?: string
69
+ toolName?: string
70
+ toolStartedAt?: number
71
+ toolEndedAt?: number
72
+ }
73
+
74
+ export type ActivityReadResult =
75
+ | { ok: true; activity: SubagentActivityState }
76
+ | { ok: false; reason: 'missing' | 'invalid' | 'wrong-id'; error?: string }
77
+
78
+ export type SubagentShutdownReason =
79
+ | 'quit'
80
+ | 'reload'
81
+ | 'new'
82
+ | 'resume'
83
+ | 'fork'
84
+
85
+ export interface SubagentActivityRecorder {
86
+ sessionStart(): void
87
+ input(): void
88
+ beforeAgentStart(): void
89
+ agentStart(): void
90
+ agentEndWaiting(): void
91
+ agentEndDone(): void
92
+ turnStart(turnIndex?: number): void
93
+ turnEnd(turnIndex?: number): void
94
+ beforeProviderRequest(): void
95
+ afterProviderResponse(): void
96
+ messageUpdate(messageEventType?: string): void
97
+ toolExecutionStart(toolCallId?: string, toolName?: string): void
98
+ toolCall(toolCallId?: string, toolName?: string): void
99
+ toolExecutionUpdate(toolCallId?: string, toolName?: string): void
100
+ toolResult(toolCallId?: string, toolName?: string): void
101
+ toolExecutionEnd(toolCallId?: string, toolName?: string): void
102
+ callerPing(): void
103
+ blockedAsking(): void
104
+ retiring(): void
105
+ subagentDone(): void
106
+ sessionShutdown(reason: SubagentShutdownReason): void
107
+ }
108
+
109
+ const ACTIVITY_UPDATE_THROTTLE_MS = 500
110
+ const MAX_WRITE_FAILURES = 3
111
+ const KNOWN_PHASES = new Set<SubagentActivityPhase>([
112
+ 'starting',
113
+ 'active',
114
+ 'waiting',
115
+ 'blocked',
116
+ 'retiring',
117
+ 'done',
118
+ ])
119
+ const KNOWN_SCOPES = new Set<SubagentActivityScope>([
120
+ 'agent',
121
+ 'turn',
122
+ 'provider',
123
+ 'streaming',
124
+ 'tool',
125
+ ])
126
+ const KNOWN_EVENTS = new Set<SubagentActivityEvent>([
127
+ 'session_start',
128
+ 'input',
129
+ 'before_agent_start',
130
+ 'agent_start',
131
+ 'agent_end',
132
+ 'turn_start',
133
+ 'turn_end',
134
+ 'before_provider_request',
135
+ 'after_provider_response',
136
+ 'message_update',
137
+ 'tool_execution_start',
138
+ 'tool_call',
139
+ 'tool_execution_update',
140
+ 'tool_result',
141
+ 'tool_execution_end',
142
+ 'caller_ping',
143
+ 'ask_owner',
144
+ 'context_retiring',
145
+ 'subagent_done',
146
+ 'session_shutdown',
147
+ ])
148
+ const MAX_ACTIVITY_STRING_LENGTH = 200
149
+
150
+ export function getSubagentActivityFile(
151
+ artifactDir: string,
152
+ runningChildId: string,
153
+ ): string {
154
+ return join(artifactDir, 'subagent-activity', `${runningChildId}.json`)
155
+ }
156
+
157
+ function requireObject(value: unknown): Record<string, unknown> | null {
158
+ if (value == null || typeof value !== 'object' || Array.isArray(value))
159
+ return null
160
+ return value as Record<string, unknown>
161
+ }
162
+
163
+ function validateFiniteNumber(
164
+ object: Record<string, unknown>,
165
+ fieldName: string,
166
+ ): string | null {
167
+ return Number.isFinite(object[fieldName])
168
+ ? null
169
+ : `${fieldName} must be finite`
170
+ }
171
+
172
+ function validateOptionalFiniteNumber(
173
+ object: Record<string, unknown>,
174
+ fieldName: string,
175
+ ): string | null {
176
+ const value = object[fieldName]
177
+ return value == null || Number.isFinite(value)
178
+ ? null
179
+ : `${fieldName} must be finite when present`
180
+ }
181
+
182
+ function validateInteger(
183
+ object: Record<string, unknown>,
184
+ fieldName: string,
185
+ ): string | null {
186
+ return Number.isInteger(object[fieldName])
187
+ ? null
188
+ : `${fieldName} must be an integer`
189
+ }
190
+
191
+ function validateOptionalInteger(
192
+ object: Record<string, unknown>,
193
+ fieldName: string,
194
+ ): string | null {
195
+ const value = object[fieldName]
196
+ return value == null || Number.isInteger(value)
197
+ ? null
198
+ : `${fieldName} must be an integer when present`
199
+ }
200
+
201
+ function validateBoolean(
202
+ object: Record<string, unknown>,
203
+ fieldName: string,
204
+ ): string | null {
205
+ return typeof object[fieldName] === 'boolean'
206
+ ? null
207
+ : `${fieldName} must be a boolean`
208
+ }
209
+
210
+ function validateOptionalActivityString(
211
+ object: Record<string, unknown>,
212
+ fieldName: string,
213
+ ): string | null {
214
+ const value = object[fieldName]
215
+ if (value == null) return null
216
+ if (typeof value !== 'string')
217
+ return `${fieldName} must be a string when present`
218
+ if (/\r|\n/.test(value)) return `${fieldName} must not contain newlines`
219
+ return value.length <= MAX_ACTIVITY_STRING_LENGTH
220
+ ? null
221
+ : `${fieldName} is too long`
222
+ }
223
+
224
+ function invalidActivity(error: string): ActivityReadResult {
225
+ return { ok: false, reason: 'invalid', error }
226
+ }
227
+
228
+ function validateActivity(
229
+ value: unknown,
230
+ expectedRunningChildId: string,
231
+ ): ActivityReadResult {
232
+ const object = requireObject(value)
233
+ if (!object) return invalidActivity('activity must be an object')
234
+ if (object.version !== 1)
235
+ return invalidActivity('unsupported activity version')
236
+ if (typeof object.runningChildId !== 'string')
237
+ return invalidActivity('runningChildId must be a string')
238
+ if (object.runningChildId !== expectedRunningChildId)
239
+ return { ok: false, reason: 'wrong-id' }
240
+ if (
241
+ typeof object.latestEvent !== 'string' ||
242
+ !KNOWN_EVENTS.has(object.latestEvent as SubagentActivityEvent)
243
+ ) {
244
+ return invalidActivity('unknown latestEvent')
245
+ }
246
+ if (
247
+ typeof object.phase !== 'string' ||
248
+ !KNOWN_PHASES.has(object.phase as SubagentActivityPhase)
249
+ ) {
250
+ return invalidActivity('unknown activity phase')
251
+ }
252
+ if (
253
+ object.activeScope != null &&
254
+ (typeof object.activeScope !== 'string' ||
255
+ !KNOWN_SCOPES.has(object.activeScope as SubagentActivityScope))
256
+ ) {
257
+ return invalidActivity('unknown activeScope')
258
+ }
259
+
260
+ const validationError = [
261
+ validateFiniteNumber(object, 'createdAt'),
262
+ validateFiniteNumber(object, 'updatedAt'),
263
+ validateInteger(object, 'sequence'),
264
+ validateBoolean(object, 'agentActive'),
265
+ validateBoolean(object, 'turnActive'),
266
+ validateBoolean(object, 'providerActive'),
267
+ validateBoolean(object, 'toolActive'),
268
+ validateOptionalFiniteNumber(object, 'activeSince'),
269
+ validateOptionalFiniteNumber(object, 'waitingSince'),
270
+ validateOptionalFiniteNumber(object, 'blockedSince'),
271
+ validateOptionalInteger(object, 'turnIndex'),
272
+ validateOptionalFiniteNumber(object, 'toolStartedAt'),
273
+ validateOptionalFiniteNumber(object, 'toolEndedAt'),
274
+ validateOptionalActivityString(object, 'messageEventType'),
275
+ validateOptionalActivityString(object, 'toolCallId'),
276
+ validateOptionalActivityString(object, 'toolName'),
277
+ ].find((error) => error != null)
278
+ if (validationError) return invalidActivity(validationError)
279
+
280
+ return { ok: true, activity: object as unknown as SubagentActivityState }
281
+ }
282
+
283
+ export function readSubagentActivityFile(
284
+ activityFile: string,
285
+ expectedRunningChildId: string,
286
+ ): ActivityReadResult {
287
+ if (!existsSync(activityFile)) return { ok: false, reason: 'missing' }
288
+
289
+ let parsed: unknown
290
+ try {
291
+ parsed = JSON.parse(readFileSync(activityFile, 'utf8'))
292
+ } catch (error) {
293
+ const message = error instanceof Error ? error.message : String(error)
294
+ return { ok: false, reason: 'invalid', error: message }
295
+ }
296
+
297
+ return validateActivity(parsed, expectedRunningChildId)
298
+ }
299
+
300
+ export function writeSubagentActivityFile(
301
+ activityFile: string,
302
+ activity: SubagentActivityState,
303
+ ): void {
304
+ const dir = dirname(activityFile)
305
+ mkdirSync(dir, { recursive: true })
306
+ const tempFile = join(
307
+ dir,
308
+ `${activity.runningChildId}.json.${process.pid}.${activity.sequence}.tmp`,
309
+ )
310
+
311
+ try {
312
+ writeFileSync(tempFile, `${JSON.stringify(activity)}\n`, 'utf8')
313
+ renameSync(tempFile, activityFile)
314
+ } catch (error) {
315
+ try {
316
+ unlinkSync(tempFile)
317
+ } catch (cleanupError) {
318
+ // Temp cleanup is best effort; preserve the original write/rename failure
319
+ void cleanupError
320
+ }
321
+ throw error
322
+ }
323
+ }
324
+
325
+ function createNoopRecorder(): SubagentActivityRecorder {
326
+ return {
327
+ sessionStart() {},
328
+ input() {},
329
+ beforeAgentStart() {},
330
+ agentStart() {},
331
+ agentEndWaiting() {},
332
+ agentEndDone() {},
333
+ turnStart() {},
334
+ turnEnd() {},
335
+ beforeProviderRequest() {},
336
+ afterProviderResponse() {},
337
+ messageUpdate() {},
338
+ toolExecutionStart() {},
339
+ toolCall() {},
340
+ toolExecutionUpdate() {},
341
+ toolResult() {},
342
+ toolExecutionEnd() {},
343
+ callerPing() {},
344
+ blockedAsking() {},
345
+ retiring() {},
346
+ subagentDone() {},
347
+ sessionShutdown() {},
348
+ }
349
+ }
350
+
351
+ function clearActiveState(activity: SubagentActivityState): void {
352
+ activity.agentActive = false
353
+ activity.turnActive = false
354
+ activity.providerActive = false
355
+ activity.toolActive = false
356
+ delete activity.activeScope
357
+ delete activity.activeSince
358
+ }
359
+
360
+ function refreshActiveScope(activity: SubagentActivityState): void {
361
+ if (activity.toolActive) {
362
+ activity.phase = 'active'
363
+ activity.activeScope = 'tool'
364
+ return
365
+ }
366
+ if (activity.providerActive) {
367
+ activity.phase = 'active'
368
+ activity.activeScope = 'provider'
369
+ return
370
+ }
371
+ if (activity.turnActive) {
372
+ activity.phase = 'active'
373
+ activity.activeScope = 'turn'
374
+ return
375
+ }
376
+ if (activity.agentActive) {
377
+ activity.phase = 'active'
378
+ activity.activeScope = 'agent'
379
+ return
380
+ }
381
+ delete activity.activeScope
382
+ delete activity.activeSince
383
+ }
384
+
385
+ function markActive(
386
+ activity: SubagentActivityState,
387
+ scope: SubagentActivityScope,
388
+ now: number,
389
+ resetActiveSince = false,
390
+ ): void {
391
+ activity.phase = 'active'
392
+ activity.activeScope = scope
393
+ if (activity.activeSince == null || resetActiveSince)
394
+ activity.activeSince = now
395
+ delete activity.waitingSince
396
+ delete activity.blockedSince
397
+ }
398
+
399
+ export function createSubagentActivityRecorder(params: {
400
+ runningChildId?: string
401
+ activityFile?: string
402
+ now?: () => number
403
+ }): SubagentActivityRecorder {
404
+ const runningChildId = params.runningChildId?.trim()
405
+ const activityFile = params.activityFile?.trim()
406
+ if (!runningChildId || !activityFile) return createNoopRecorder()
407
+
408
+ const now = params.now ?? (() => Date.now())
409
+ const createdAt = now()
410
+ const activity: SubagentActivityState = {
411
+ version: 1,
412
+ runningChildId,
413
+ createdAt,
414
+ updatedAt: createdAt,
415
+ sequence: 0,
416
+ latestEvent: 'session_start',
417
+ phase: 'starting',
418
+ agentActive: false,
419
+ turnActive: false,
420
+ providerActive: false,
421
+ toolActive: false,
422
+ }
423
+
424
+ let disabled = false
425
+ let failureCount = 0
426
+ let lastFlushAt = 0
427
+ let pendingFlush: ReturnType<typeof setTimeout> | null = null
428
+ // AIDEV-NOTE: ask_owner is pending — agentEndWaiting must keep the phase at
429
+ // 'blocked' instead of 'waiting' so the parent never classifies a
430
+ // blocked-asking child as idle/stalled. Cleared when the answer arrives and
431
+ // the next agent run starts.
432
+ let askPending = false
433
+
434
+ function clearPendingFlush(): void {
435
+ if (!pendingFlush) return
436
+ clearTimeout(pendingFlush)
437
+ pendingFlush = null
438
+ }
439
+
440
+ function disable(): void {
441
+ disabled = true
442
+ clearPendingFlush()
443
+ }
444
+
445
+ function flushNow(): void {
446
+ if (disabled) return
447
+ try {
448
+ if (activityFile) {
449
+ writeSubagentActivityFile(activityFile, activity)
450
+ }
451
+ lastFlushAt = now()
452
+ failureCount = 0
453
+ } catch {
454
+ failureCount += 1
455
+ if (failureCount >= MAX_WRITE_FAILURES) disable()
456
+ }
457
+ }
458
+
459
+ function scheduleFlush(): void {
460
+ if (disabled || pendingFlush) return
461
+
462
+ const remainingMs = Math.max(
463
+ 0,
464
+ ACTIVITY_UPDATE_THROTTLE_MS - (now() - lastFlushAt),
465
+ )
466
+ if (remainingMs === 0) {
467
+ flushNow()
468
+ return
469
+ }
470
+
471
+ pendingFlush = setTimeout(() => {
472
+ pendingFlush = null
473
+ flushNow()
474
+ }, remainingMs)
475
+ }
476
+
477
+ function record(
478
+ latestEvent: SubagentActivityEvent,
479
+ update: (current: SubagentActivityState, now: number) => void,
480
+ flush: 'immediate' | 'throttled',
481
+ ): void {
482
+ if (disabled) return
483
+ if (flush === 'immediate') clearPendingFlush()
484
+
485
+ const observedAt = now()
486
+ activity.latestEvent = latestEvent
487
+ activity.updatedAt = observedAt
488
+ activity.sequence += 1
489
+ update(activity, observedAt)
490
+
491
+ if (flush === 'immediate') flushNow()
492
+ else scheduleFlush()
493
+ }
494
+
495
+ function markDone(latestEvent: SubagentActivityEvent): void {
496
+ record(
497
+ latestEvent,
498
+ (current) => {
499
+ current.phase = 'done'
500
+ clearActiveState(current)
501
+ delete current.waitingSince
502
+ delete current.blockedSince
503
+ },
504
+ 'immediate',
505
+ )
506
+ disable()
507
+ }
508
+
509
+ return {
510
+ sessionStart() {
511
+ record(
512
+ 'session_start',
513
+ (current) => {
514
+ current.phase = 'starting'
515
+ clearActiveState(current)
516
+ delete current.waitingSince
517
+ },
518
+ 'immediate',
519
+ )
520
+ },
521
+ input() {
522
+ record('input', () => {}, 'immediate')
523
+ },
524
+ beforeAgentStart() {
525
+ record(
526
+ 'before_agent_start',
527
+ (current, observedAt) => {
528
+ current.agentActive = true
529
+ askPending = false
530
+ markActive(current, 'agent', observedAt)
531
+ },
532
+ 'immediate',
533
+ )
534
+ },
535
+ agentStart() {
536
+ record(
537
+ 'agent_start',
538
+ (current, observedAt) => {
539
+ current.agentActive = true
540
+ markActive(current, 'agent', observedAt)
541
+ },
542
+ 'immediate',
543
+ )
544
+ },
545
+ agentEndWaiting() {
546
+ record(
547
+ 'agent_end',
548
+ (current, observedAt) => {
549
+ clearActiveState(current)
550
+ if (askPending) {
551
+ current.phase = 'blocked'
552
+ if (current.blockedSince == null) current.blockedSince = observedAt
553
+ } else {
554
+ current.phase = 'waiting'
555
+ current.waitingSince = observedAt
556
+ }
557
+ },
558
+ 'immediate',
559
+ )
560
+ },
561
+ // AIDEV-NOTE: ask_owner tool called — mark blocked immediately (the turn
562
+ // is ending); the askPending flag keeps later agent_end at 'blocked'.
563
+ // Cleared in beforeAgentStart when the owner's answer starts the next run.
564
+ blockedAsking() {
565
+ askPending = true
566
+ record(
567
+ 'ask_owner',
568
+ (current, observedAt) => {
569
+ current.phase = 'blocked'
570
+ clearActiveState(current)
571
+ delete current.waitingSince
572
+ if (current.blockedSince == null) current.blockedSince = observedAt
573
+ },
574
+ 'immediate',
575
+ )
576
+ },
577
+ // AIDEV-NOTE (PMR-HVM8): threshold compaction intercepted — child should
578
+ // converge (finish + subagent_done). Transient: the triggered converge
579
+ // turn flips the phase back to 'active' via before_agent_start.
580
+ retiring() {
581
+ record(
582
+ 'context_retiring',
583
+ (current) => {
584
+ current.phase = 'retiring'
585
+ clearActiveState(current)
586
+ delete current.waitingSince
587
+ delete current.blockedSince
588
+ },
589
+ 'immediate',
590
+ )
591
+ },
592
+ agentEndDone() {
593
+ markDone('agent_end')
594
+ },
595
+ turnStart(turnIndex) {
596
+ record(
597
+ 'turn_start',
598
+ (current, observedAt) => {
599
+ current.agentActive = true
600
+ current.turnActive = true
601
+ if (turnIndex != null) current.turnIndex = turnIndex
602
+ markActive(
603
+ current,
604
+ current.toolActive || current.providerActive
605
+ ? (current.activeScope ?? 'turn')
606
+ : 'turn',
607
+ observedAt,
608
+ )
609
+ },
610
+ 'immediate',
611
+ )
612
+ },
613
+ turnEnd(turnIndex) {
614
+ record(
615
+ 'turn_end',
616
+ (current) => {
617
+ current.turnActive = false
618
+ current.providerActive = false
619
+ current.toolActive = false
620
+ if (turnIndex != null) current.turnIndex = turnIndex
621
+ refreshActiveScope(current)
622
+ },
623
+ 'immediate',
624
+ )
625
+ },
626
+ beforeProviderRequest() {
627
+ record(
628
+ 'before_provider_request',
629
+ (current, observedAt) => {
630
+ current.providerActive = true
631
+ markActive(current, 'provider', observedAt, true)
632
+ },
633
+ 'immediate',
634
+ )
635
+ },
636
+ afterProviderResponse() {
637
+ record(
638
+ 'after_provider_response',
639
+ (current) => {
640
+ current.providerActive = false
641
+ refreshActiveScope(current)
642
+ },
643
+ 'immediate',
644
+ )
645
+ },
646
+ messageUpdate(messageEventType) {
647
+ record(
648
+ 'message_update',
649
+ (current, observedAt) => {
650
+ current.agentActive = true
651
+ current.turnActive = true
652
+ current.messageEventType = messageEventType
653
+ if (!current.toolActive) markActive(current, 'streaming', observedAt)
654
+ },
655
+ 'throttled',
656
+ )
657
+ },
658
+ toolExecutionStart(toolCallId, toolName) {
659
+ record(
660
+ 'tool_execution_start',
661
+ (current, observedAt) => {
662
+ current.toolActive = true
663
+ current.toolCallId = toolCallId
664
+ current.toolName = toolName
665
+ current.toolStartedAt = observedAt
666
+ markActive(current, 'tool', observedAt, true)
667
+ },
668
+ 'immediate',
669
+ )
670
+ },
671
+ toolCall(toolCallId, toolName) {
672
+ record(
673
+ 'tool_call',
674
+ (current, observedAt) => {
675
+ current.toolActive = true
676
+ current.toolCallId = toolCallId ?? current.toolCallId
677
+ current.toolName = toolName ?? current.toolName
678
+ markActive(current, 'tool', observedAt)
679
+ },
680
+ 'immediate',
681
+ )
682
+ },
683
+ toolExecutionUpdate(toolCallId, toolName) {
684
+ record(
685
+ 'tool_execution_update',
686
+ (current, observedAt) => {
687
+ current.toolActive = true
688
+ current.toolCallId = toolCallId ?? current.toolCallId
689
+ current.toolName = toolName ?? current.toolName
690
+ markActive(current, 'tool', observedAt)
691
+ },
692
+ 'throttled',
693
+ )
694
+ },
695
+ toolResult(toolCallId, toolName) {
696
+ record(
697
+ 'tool_result',
698
+ (current) => {
699
+ current.toolCallId = toolCallId ?? current.toolCallId
700
+ current.toolName = toolName ?? current.toolName
701
+ refreshActiveScope(current)
702
+ },
703
+ 'immediate',
704
+ )
705
+ },
706
+ toolExecutionEnd(toolCallId, toolName) {
707
+ record(
708
+ 'tool_execution_end',
709
+ (current, observedAt) => {
710
+ current.toolActive = false
711
+ current.toolCallId = toolCallId ?? current.toolCallId
712
+ current.toolName = toolName ?? current.toolName
713
+ current.toolEndedAt = observedAt
714
+ refreshActiveScope(current)
715
+ },
716
+ 'immediate',
717
+ )
718
+ },
719
+ callerPing() {
720
+ markDone('caller_ping')
721
+ },
722
+ subagentDone() {
723
+ markDone('subagent_done')
724
+ },
725
+ sessionShutdown(reason) {
726
+ if (reason === 'quit') markDone('session_shutdown')
727
+ else disable()
728
+ },
729
+ }
730
+ }