ai-agent 0.88.0

Idiomatic agent sdk inspired by the claude code source leak
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
// Source: ~/claudecode/openclaudecode/src/utils/hooks/AsyncHookRegistry.ts
#![allow(dead_code)]
#![allow(static_mut_refs)]

use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use tokio::time::{Duration, interval};

/// Represents a hook event type
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HookEvent {
    SessionStart,
    Setup,
    PreToolUse,
    PostToolUse,
    PostToolUseFailure,
    PermissionDenied,
    Notification,
    UserPromptSubmit,
    SessionEnd,
    Stop,
    StopFailure,
    SubagentStart,
    SubagentStop,
    PreCompact,
    PostCompact,
    PermissionRequest,
    TeammateIdle,
    TaskCreated,
    TaskCompleted,
    Elicitation,
    ElicitationResult,
    ConfigChange,
    InstructionsLoaded,
    WorktreeCreate,
    WorktreeRemove,
    CwdChanged,
    FileChanged,
    StatusLine,
    FileSuggestion,
    Custom(String),
}

impl HookEvent {
    pub fn as_str(&self) -> &str {
        match self {
            HookEvent::SessionStart => "SessionStart",
            HookEvent::Setup => "Setup",
            HookEvent::PreToolUse => "PreToolUse",
            HookEvent::PostToolUse => "PostToolUse",
            HookEvent::PostToolUseFailure => "PostToolUseFailure",
            HookEvent::PermissionDenied => "PermissionDenied",
            HookEvent::Notification => "Notification",
            HookEvent::UserPromptSubmit => "UserPromptSubmit",
            HookEvent::SessionEnd => "SessionEnd",
            HookEvent::Stop => "Stop",
            HookEvent::StopFailure => "StopFailure",
            HookEvent::SubagentStart => "SubagentStart",
            HookEvent::SubagentStop => "SubagentStop",
            HookEvent::PreCompact => "PreCompact",
            HookEvent::PostCompact => "PostCompact",
            HookEvent::PermissionRequest => "PermissionRequest",
            HookEvent::TeammateIdle => "TeammateIdle",
            HookEvent::TaskCreated => "TaskCreated",
            HookEvent::TaskCompleted => "TaskCompleted",
            HookEvent::Elicitation => "Elicitation",
            HookEvent::ElicitationResult => "ElicitationResult",
            HookEvent::ConfigChange => "ConfigChange",
            HookEvent::InstructionsLoaded => "InstructionsLoaded",
            HookEvent::WorktreeCreate => "WorktreeCreate",
            HookEvent::WorktreeRemove => "WorktreeRemove",
            HookEvent::CwdChanged => "CwdChanged",
            HookEvent::FileChanged => "FileChanged",
            HookEvent::StatusLine => "StatusLine",
            HookEvent::FileSuggestion => "FileSuggestion",
            HookEvent::Custom(s) => s,
        }
    }
}

/// JSON output from an async hook
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AsyncHookJsonOutput {
    /// Timeout in seconds (0 means default)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub async_timeout: Option<u64>,
}

/// JSON output from a sync hook
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SyncHookJsonOutput {
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

/// Output from a shell command task
#[derive(Clone)]
pub struct TaskOutput {
    stdout: Arc<Mutex<String>>,
    stderr: Arc<Mutex<String>>,
}

impl TaskOutput {
    pub fn new() -> Self {
        Self {
            stdout: Arc::new(Mutex::new(String::new())),
            stderr: Arc::new(Mutex::new(String::new())),
        }
    }

    pub async fn get_stdout(&self) -> String {
        self.stdout.lock().unwrap().clone()
    }

    pub fn get_stderr(&self) -> String {
        self.stderr.lock().unwrap().clone()
    }

    pub fn append_stdout(&self, data: &str) {
        self.stdout.lock().unwrap().push_str(data);
    }

    pub fn append_stderr(&self, data: &str) {
        self.stderr.lock().unwrap().push_str(data);
    }
}

/// A shell command being executed
pub struct ShellCommand {
    pub status: ShellCommandStatus,
    pub task_output: TaskOutput,
    pub pid: Option<u32>,
}

impl ShellCommand {
    pub fn cleanup(&self) {
        if let Some(pid) = self.pid {
            unsafe {
                libc::kill(pid as i32, libc::SIGTERM);
            }
        }
    }

    pub fn kill(&mut self) {
        self.status = ShellCommandStatus::Killed;
        self.cleanup();
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum ShellCommandStatus {
    Running,
    Completed,
    Killed,
}

/// A pending async hook in the registry
pub struct PendingAsyncHook {
    pub process_id: String,
    pub hook_id: String,
    pub hook_name: String,
    pub hook_event: HookEvent,
    pub tool_name: Option<String>,
    pub plugin_id: Option<String>,
    pub start_time: std::time::SystemTime,
    pub timeout_ms: u64,
    pub command: String,
    pub response_attachment_sent: bool,
    pub shell_command: Option<ShellCommand>,
    pub progress_task_id: Option<u64>, // Simple ID for tracking (simplified)
}

/// Global registry state for pending async hooks
struct AsyncHookRegistryState {
    pending_hooks: HashMap<String, PendingAsyncHook>,
}

lazy_static::lazy_static! {
    static ref ASYNC_HOOK_REGISTRY: Arc<Mutex<AsyncHookRegistryState>> = Arc::new(Mutex::new(
        AsyncHookRegistryState {
            pending_hooks: HashMap::new(),
        }
    ));
}

/// Parameters for registering a pending async hook
pub struct RegisterPendingAsyncHookParams {
    pub process_id: String,
    pub hook_id: String,
    pub async_response: AsyncHookJsonOutput,
    pub hook_name: String,
    pub hook_event: HookEvent,
    pub command: String,
    pub shell_command: ShellCommand,
    pub tool_name: Option<String>,
    pub plugin_id: Option<String>,
}

/// Register a pending async hook
pub fn register_pending_async_hook(params: RegisterPendingAsyncHookParams) {
    let timeout = params.async_response.async_timeout.unwrap_or(15) * 1000; // Default 15s, convert to ms

    log_for_debugging(&format!(
        "Hooks: Registering async hook {} ({}) with timeout {}ms",
        params.process_id, params.hook_name, timeout
    ));

    let hook_id = params.hook_id.clone();
    let hook_name = params.hook_name.clone();
    let hook_event = params.hook_event.clone();
    let process_id = params.process_id.clone();
    let shell_task_output = params.shell_command.task_output.clone();

    // Create progress interval that polls shell command output
    let _progress_handle = start_hook_progress_interval(HookProgressParams {
        hook_id: params.hook_id.clone(),
        hook_name: params.hook_name.clone(),
        hook_event: params.hook_event.clone(),
        get_output: Arc::new(move || {
            let task_output = shell_task_output.clone();
            Box::pin(async move {
                let stdout = task_output.get_stdout().await;
                let stderr = task_output.get_stderr();
                let output = format!("{}{}", stdout, stderr);
                HookOutput {
                    stdout,
                    stderr,
                    output,
                }
            })
        }),
        interval_ms: None,
    });

    let pending_hook = PendingAsyncHook {
        process_id: params.process_id.clone(),
        hook_id: params.hook_id,
        hook_name: params.hook_name,
        hook_event: params.hook_event,
        tool_name: params.tool_name,
        plugin_id: params.plugin_id,
        start_time: std::time::SystemTime::now(),
        timeout_ms: timeout,
        command: params.command,
        response_attachment_sent: false,
        shell_command: Some(params.shell_command),
        progress_task_id: None,
    };

    let mut registry = ASYNC_HOOK_REGISTRY.lock().unwrap();
    registry
        .pending_hooks
        .insert(params.process_id, pending_hook);
}

/// Get all pending async hooks that haven't sent their response attachment
pub fn get_pending_async_hooks() -> Vec<Arc<Mutex<PendingAsyncHook>>> {
    let registry = ASYNC_HOOK_REGISTRY.lock().unwrap();
    registry
        .pending_hooks
        .values()
        .filter(|hook| !hook.response_attachment_sent)
        .map(|hook| {
            Arc::new(Mutex::new(PendingAsyncHook {
                process_id: hook.process_id.clone(),
                hook_id: hook.hook_id.clone(),
                hook_name: hook.hook_name.clone(),
                hook_event: hook.hook_event.clone(),
                tool_name: hook.tool_name.clone(),
                plugin_id: hook.plugin_id.clone(),
                start_time: hook.start_time,
                timeout_ms: hook.timeout_ms,
                command: hook.command.clone(),
                response_attachment_sent: hook.response_attachment_sent,
                shell_command: None, // Can't clone shell command
                progress_task_id: None,
            }))
        })
        .collect()
}

pub struct HookProgressParams {
    pub hook_id: String,
    pub hook_name: String,
    pub hook_event: HookEvent,
    pub get_output: Arc<
        dyn Fn() -> std::pin::Pin<Box<dyn std::future::Future<Output = HookOutput> + Send>>
            + Send
            + Sync,
    >,
    pub interval_ms: Option<u64>,
}

pub struct HookOutput {
    pub stdout: String,
    pub stderr: String,
    pub output: String,
}

const MAX_PENDING_EVENTS: usize = 100;

static mut EVENT_HANDLER: Option<Box<dyn Fn(HookExecutionEvent) + Send + Sync>> = None;
static mut PENDING_EVENTS: Vec<HookExecutionEvent> = Vec::new();
static mut ALL_HOOK_EVENTS_ENABLED: bool = false;

// Always emitted hook events regardless of includeHookEvents option
const ALWAYS_EMITTED_HOOK_EVENTS: [&str; 2] = ["SessionStart", "Setup"];

#[derive(Debug, Clone)]
pub enum HookExecutionEvent {
    Started {
        hook_id: String,
        hook_name: String,
        hook_event: String,
    },
    Progress {
        hook_id: String,
        hook_name: String,
        hook_event: String,
        stdout: String,
        stderr: String,
        output: String,
    },
    Response {
        hook_id: String,
        hook_name: String,
        hook_event: String,
        output: String,
        stdout: String,
        stderr: String,
        exit_code: Option<i32>,
        outcome: HookOutcome,
    },
}

#[derive(Debug, Clone)]
pub enum HookOutcome {
    Success,
    Error,
    Cancelled,
}

fn emit_hook_event(event: HookExecutionEvent) {
    unsafe {
        if let Some(ref handler) = EVENT_HANDLER {
            handler(event);
        } else {
            PENDING_EVENTS.push(event);
            if PENDING_EVENTS.len() > MAX_PENDING_EVENTS {
                PENDING_EVENTS.remove(0);
            }
        }
    }
}

fn should_emit(hook_event: &str) -> bool {
    if ALWAYS_EMITTED_HOOK_EVENTS.contains(&hook_event) {
        return true;
    }
    unsafe { ALL_HOOK_EVENTS_ENABLED }
}

/// Register a handler for hook execution events
pub fn register_hook_event_handler(handler: Option<Box<dyn Fn(HookExecutionEvent) + Send + Sync>>) {
    unsafe {
        let old_handler = EVENT_HANDLER.take();
        EVENT_HANDLER = handler;

        // If we have a new handler and pending events, deliver them
        if let Some(ref handler) = EVENT_HANDLER {
            let events: Vec<HookExecutionEvent> = PENDING_EVENTS.drain(..).collect();
            for event in events {
                handler(event);
            }
        } else {
            // Restore old handler if any
            if let Some(old) = old_handler {
                EVENT_HANDLER = Some(old);
            }
        }
    }
}

/// Emit hook started event
pub fn emit_hook_started(hook_id: &str, hook_name: &str, hook_event: &str) {
    if !should_emit(hook_event) {
        return;
    }
    emit_hook_event(HookExecutionEvent::Started {
        hook_id: hook_id.to_string(),
        hook_name: hook_name.to_string(),
        hook_event: hook_event.to_string(),
    });
}

/// Emit hook progress event
pub fn emit_hook_progress(params: HookProgressParams) {
    if !should_emit(params.hook_event.as_str()) {
        return;
    }
    emit_hook_event(HookExecutionEvent::Progress {
        hook_id: params.hook_id,
        hook_name: params.hook_name,
        hook_event: params.hook_event.as_str().to_string(),
        stdout: String::new(),
        stderr: String::new(),
        output: String::new(),
    });
}

/// Start a progress interval that periodically emits hook progress events.
/// Returns a JoinHandle that can be aborted to stop the interval.
pub fn start_hook_progress_interval(params: HookProgressParams) -> tokio::task::JoinHandle<()> {
    if !should_emit(params.hook_event.as_str()) {
        return tokio::spawn(async {});
    }

    let interval_ms = params.interval_ms.unwrap_or(1000);
    let hook_id = params.hook_id.clone();
    let hook_name = params.hook_name.clone();
    let hook_event = params.hook_event.clone();
    let get_output = params.get_output;

    // Spawn tokio task for progress polling
    tokio::spawn(async move {
        let mut last_emitted_output = String::new();
        let mut interval = interval(Duration::from_millis(interval_ms));

        loop {
            interval.tick().await;
            let output = get_output().await;
            if output.output == last_emitted_output {
                continue;
            }
            last_emitted_output = output.output.clone();

            emit_hook_event(HookExecutionEvent::Progress {
                hook_id: hook_id.clone(),
                hook_name: hook_name.clone(),
                hook_event: hook_event.as_str().to_string(),
                stdout: output.stdout,
                stderr: output.stderr,
                output: output.output,
            });
        }
    })
}

/// Emit hook response event
pub fn emit_hook_response(data: HookResponseData) {
    // Always log full hook output to debug log for verbose mode debugging
    let output_to_log =
        if !data.stdout.is_empty() || !data.stderr.is_empty() || !data.output.is_empty() {
            if !data.stdout.is_empty() {
                Some(&data.stdout)
            } else if !data.stderr.is_empty() {
                Some(&data.stderr)
            } else {
                Some(&data.output)
            }
        } else {
            None
        };

    if let Some(output) = output_to_log {
        log_for_debugging(&format!(
            "Hook {} ({}) {:?}:\n{}",
            data.hook_name, data.hook_event, data.outcome, output
        ));
    }

    if !should_emit(&data.hook_event) {
        return;
    }

    emit_hook_event(HookExecutionEvent::Response {
        hook_id: data.hook_id,
        hook_name: data.hook_name,
        hook_event: data.hook_event,
        output: data.output,
        stdout: data.stdout,
        stderr: data.stderr,
        exit_code: data.exit_code,
        outcome: data.outcome,
    });
}

pub struct HookResponseData {
    pub hook_id: String,
    pub hook_name: String,
    pub hook_event: String,
    pub output: String,
    pub stdout: String,
    pub stderr: String,
    pub exit_code: Option<i32>,
    pub outcome: HookOutcome,
}

/// Enable emission of all hook event types (beyond SessionStart and Setup)
pub fn set_all_hook_events_enabled(enabled: bool) {
    unsafe {
        ALL_HOOK_EVENTS_ENABLED = enabled;
    }
}

/// Clear hook event state
pub fn clear_hook_event_state() {
    unsafe {
        EVENT_HANDLER = None;
        PENDING_EVENTS.clear();
        ALL_HOOK_EVENTS_ENABLED = false;
    }
}

/// Finalize a hook after completion
async fn finalize_hook(_hook: &PendingAsyncHook, exit_code: i32, outcome: HookOutcome) {
    // Note: progress_task_id cannot be called through a shared reference
    // since it's a FnOnce. In practice, the caller would have already stopped it.

    let stdout = if let Some(shell_cmd) = &_hook.shell_command {
        shell_cmd.task_output.get_stdout().await
    } else {
        String::new()
    };
    let stderr = _hook
        .shell_command
        .as_ref()
        .map_or(String::new(), |s| s.task_output.get_stderr());

    if let Some(shell_cmd) = &_hook.shell_command {
        shell_cmd.cleanup();
    }

    emit_hook_response(HookResponseData {
        hook_id: _hook.hook_id.clone(),
        hook_name: _hook.hook_name.clone(),
        hook_event: _hook.hook_event.as_str().to_string(),
        output: format!("{}{}", stdout, stderr),
        stdout,
        stderr,
        exit_code: Some(exit_code),
        outcome,
    });
}

/// Response from check_for_async_hook_responses
pub struct AsyncHookResponse {
    pub process_id: String,
    pub response: SyncHookJsonOutput,
    pub hook_name: String,
    pub hook_event: HookEvent,
    pub tool_name: Option<String>,
    pub plugin_id: Option<String>,
    pub stdout: String,
    pub stderr: String,
    pub exit_code: Option<i32>,
}

/// Check for completed async hook responses
pub async fn check_for_async_hook_responses() -> Vec<AsyncHookResponse> {
    let mut responses: Vec<AsyncHookResponse> = Vec::new();

    let pending_count;
    let hooks_snapshot;
    {
        let registry = ASYNC_HOOK_REGISTRY.lock().unwrap();
        pending_count = registry.pending_hooks.len();
        hooks_snapshot = registry
            .pending_hooks
            .values()
            .map(|h| h.process_id.clone())
            .collect::<Vec<_>>();
    }

    log_for_debugging(&format!(
        "Hooks: Found {} total hooks in registry",
        pending_count
    ));

    let mut process_ids_to_remove: Vec<String> = Vec::new();
    let mut session_start_completed = false;

    for process_id in hooks_snapshot {
        let hook_result = {
            let mut registry = ASYNC_HOOK_REGISTRY.lock().unwrap();
            let hook = match registry.pending_hooks.get_mut(&process_id) {
                Some(h) => h,
                None => continue,
            };

            if !hook.shell_command.is_some() {
                log_for_debugging(&format!(
                    "Hooks: Hook {} has no shell command, removing from registry",
                    process_id
                ));
                hook.progress_task_id = None;

                process_ids_to_remove.push(process_id.clone());
                continue;
            }

            let shell_cmd = hook.shell_command.as_ref().unwrap();
            if shell_cmd.status == ShellCommandStatus::Killed {
                log_for_debugging(&format!(
                    "Hooks: Hook {} is killed, removing from registry",
                    process_id
                ));
                hook.progress_task_id = None;

                shell_cmd.cleanup();
                process_ids_to_remove.push(process_id.clone());
                continue;
            }

            if shell_cmd.status != ShellCommandStatus::Completed {
                continue;
            }

            if hook.response_attachment_sent {
                log_for_debugging(&format!(
                    "Hooks: Skipping hook {} - already delivered/sent",
                    process_id
                ));
                hook.progress_task_id = None;

                process_ids_to_remove.push(process_id.clone());
                continue;
            }

            let stdout = shell_cmd.task_output.get_stdout().await;
            if stdout.trim().is_empty() {
                log_for_debugging(&format!("Hooks: Skipping hook {} - no stdout", process_id));
                hook.progress_task_id = None;

                process_ids_to_remove.push(process_id.clone());
                continue;
            }

            let lines: Vec<&str> = stdout.lines().collect();
            log_for_debugging(&format!(
                "Hooks: Processing {} lines of stdout for {}",
                lines.len(),
                process_id
            ));

            let exit_code = 0; // Would come from shell command result

            let mut response = SyncHookJsonOutput {
                extra: HashMap::new(),
            };
            for line in &lines {
                if line.trim().starts_with('{') {
                    log_for_debugging(&format!(
                        "Hooks: Found JSON line: {}...",
                        &line.trim().chars().take(100).collect::<String>()
                    ));
                    if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(line.trim()) {
                        if !parsed
                            .as_object()
                            .map_or(false, |obj| obj.contains_key("async"))
                        {
                            log_for_debugging(&format!(
                                "Hooks: Found sync response from {}: {}",
                                process_id,
                                serde_json::to_string(&parsed).unwrap_or_default()
                            ));
                            if let Some(obj) = parsed.as_object() {
                                response.extra =
                                    obj.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
                            }
                            break;
                        }
                    }
                }
            }

            hook.response_attachment_sent = true;
            let is_session_start = hook.hook_event == HookEvent::SessionStart;

            // Finalize (spawn to avoid blocking)
            let hook_clone = PendingAsyncHook {
                process_id: hook.process_id.clone(),
                hook_id: hook.hook_id.clone(),
                hook_name: hook.hook_name.clone(),
                hook_event: hook.hook_event.clone(),
                tool_name: hook.tool_name.clone(),
                plugin_id: hook.plugin_id.clone(),
                start_time: hook.start_time,
                timeout_ms: hook.timeout_ms,
                command: hook.command.clone(),
                response_attachment_sent: true,
                shell_command: None,
                progress_task_id: None,
            };
            tokio::spawn(async move {
                finalize_hook(&hook_clone, exit_code, HookOutcome::Success).await;
            });

            process_ids_to_remove.push(process_id.clone());
            session_start_completed = session_start_completed || is_session_start;

            Some((
                process_id.clone(),
                response,
                hook.hook_name.clone(),
                hook.hook_event.clone(),
                hook.tool_name.clone(),
                hook.plugin_id.clone(),
                stdout,
                shell_cmd.task_output.get_stderr(),
                Some(exit_code),
            ))
        };

        if let Some((
            process_id,
            response,
            hook_name,
            hook_event,
            tool_name,
            plugin_id,
            stdout,
            stderr,
            exit_code,
        )) = hook_result
        {
            responses.push(AsyncHookResponse {
                process_id,
                response,
                hook_name,
                hook_event,
                tool_name,
                plugin_id,
                stdout,
                stderr,
                exit_code,
            });
        }
    }

    // Remove processed hooks
    {
        let mut registry = ASYNC_HOOK_REGISTRY.lock().unwrap();
        for process_id in process_ids_to_remove {
            registry.pending_hooks.remove(&process_id);
        }
    }

    if session_start_completed {
        log_for_debugging("Invalidating session env cache after SessionStart hook completed");
        invalidate_session_env_cache();
    }

    log_for_debugging(&format!(
        "Hooks: checkForNewResponses returning {} responses",
        responses.len()
    ));

    responses
}

/// Remove delivered async hooks from the registry
pub fn remove_delivered_async_hooks(process_ids: &[String]) {
    let mut registry = ASYNC_HOOK_REGISTRY.lock().unwrap();
    for process_id in process_ids {
        if let Some(hook) = registry.pending_hooks.get(process_id) {
            if hook.response_attachment_sent {
                log_for_debugging(&format!("Hooks: Removing delivered hook {}", process_id));
                // Note: can't call progress_task_id on borrowed ref
            }
        }
        registry.pending_hooks.remove(process_id);
    }
}

/// Finalize all pending async hooks (e.g., on shutdown)
pub async fn finalize_pending_async_hooks() {
    let hooks_snapshot;
    {
        let registry = ASYNC_HOOK_REGISTRY.lock().unwrap();
        hooks_snapshot = registry
            .pending_hooks
            .values()
            .map(|h| h.process_id.clone())
            .collect::<Vec<_>>();
    }

    let mut futures = Vec::new();
    for process_id in hooks_snapshot {
        let mut registry = ASYNC_HOOK_REGISTRY.lock().unwrap();
        if let Some(hook) = registry.pending_hooks.remove(&process_id) {
            let exit_code;
            let outcome;

            if let Some(ref shell_cmd) = hook.shell_command {
                if shell_cmd.status == ShellCommandStatus::Completed {
                    exit_code = 0;
                    outcome = HookOutcome::Success;
                } else {
                    if shell_cmd.status != ShellCommandStatus::Killed {
                        // Can't mutate through ref
                    }
                    exit_code = 1;
                    outcome = HookOutcome::Cancelled;
                }
            } else {
                exit_code = 1;
                outcome = HookOutcome::Cancelled;
            }

            futures.push(tokio::spawn(async move {
                finalize_hook(&hook, exit_code, outcome).await;
            }));
        }
    }

    // Wait for all finalize tasks
    for f in futures {
        let _ = f.await;
    }
}

/// Clear all async hooks (test utility)
pub fn clear_all_async_hooks() {
    let mut registry = ASYNC_HOOK_REGISTRY.lock().unwrap();
    for hook in registry.pending_hooks.values() {
        // Can't call progress_task_id through &ref
    }
    registry.pending_hooks.clear();
}

/// Log for debugging (simplified)
fn log_for_debugging(msg: &str) {
    log::debug!("{}", msg);
}

/// Invalidate session env cache (simplified)
fn invalidate_session_env_cache() {
    log::debug!("Invalidating session env cache");
}

/// JSON parse helper
fn json_parse(s: &str) -> Result<serde_json::Value, serde_json::Error> {
    serde_json::from_str(s)
}

/// JSON stringify helper
fn json_stringify(value: &serde_json::Value) -> String {
    serde_json::to_string(value).unwrap_or_default()
}