github-copilot-sdk 1.0.17-preview.6

Rust SDK for programmatic control of the GitHub Copilot CLI via JSON-RPC.
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
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *--------------------------------------------------------------------------------------------*/

#![cfg(test)]

use super::*;

struct TestHooks;

#[async_trait]
impl SessionHooks for TestHooks {
    async fn on_hook(&self, event: HookEvent) -> HookOutput {
        match event {
            HookEvent::PreToolUse { input, .. } => {
                if input.tool_name == "dangerous_tool" {
                    HookOutput::PreToolUse(PreToolUseOutput {
                        permission_decision: Some("deny".to_string()),
                        permission_decision_reason: Some("blocked by policy".to_string()),
                        ..Default::default()
                    })
                } else {
                    HookOutput::None
                }
            }
            HookEvent::UserPromptSubmitted { input, .. } => {
                HookOutput::UserPromptSubmitted(UserPromptSubmittedOutput {
                    modified_prompt: Some(format!("[prefixed] {}", input.prompt)),
                    ..Default::default()
                })
            }
            HookEvent::UserPromptTransformed { input, .. } => {
                HookOutput::UserPromptTransformed(UserPromptTransformedOutput {
                    modified_transformed_prompt: Some(format!(
                        "[transformed] {}",
                        input.transformed_prompt
                    )),
                })
            }
            _ => HookOutput::None,
        }
    }
}

#[tokio::test]
async fn dispatch_pre_tool_use_deny() {
    let hooks = TestHooks;
    let input = serde_json::json!({
        "sessionId": "sess-1",
        "timestamp": 1234567890,
        "cwd": "/tmp",
        "toolName": "dangerous_tool",
        "toolArgs": {}
    });
    let result = dispatch_hook(&hooks, &SessionId::new("sess-1"), "preToolUse", input)
        .await
        .unwrap();
    let output = &result["output"];
    assert_eq!(output["permissionDecision"], "deny");
    assert_eq!(output["permissionDecisionReason"], "blocked by policy");
}

#[tokio::test]
async fn dispatch_pre_tool_use_passthrough() {
    let hooks = TestHooks;
    let input = serde_json::json!({
        "sessionId": "sess-1",
        "timestamp": 1234567890,
        "cwd": "/tmp",
        "toolName": "safe_tool",
        "toolArgs": {"key": "value"}
    });
    let result = dispatch_hook(&hooks, &SessionId::new("sess-1"), "preToolUse", input)
        .await
        .unwrap();
    // No hook registered for this tool — output should be empty object
    assert_eq!(result["output"], serde_json::json!({}));
}

#[tokio::test]
async fn dispatch_user_prompt_submitted() {
    let hooks = TestHooks;
    let input = serde_json::json!({
        "sessionId": "sess-1",
        "timestamp": 1234567890,
        "cwd": "/tmp",
        "prompt": "hello world"
    });
    let result = dispatch_hook(
        &hooks,
        &SessionId::new("sess-1"),
        "userPromptSubmitted",
        input,
    )
    .await
    .unwrap();
    assert_eq!(result["output"]["modifiedPrompt"], "[prefixed] hello world");
}

#[tokio::test]
async fn dispatch_user_prompt_transformed() {
    let hooks = TestHooks;
    let input = serde_json::json!({
        "sessionId": "sess-1",
        "timestamp": 1234567890,
        "cwd": "/tmp",
        "prompt": "hello world",
        "transformedPrompt": "<current_datetime>now</current_datetime>\nhello world"
    });
    let result = dispatch_hook(
        &hooks,
        &SessionId::new("sess-1"),
        "userPromptTransformed",
        input,
    )
    .await
    .unwrap();
    assert_eq!(
        result["output"]["modifiedTransformedPrompt"],
        "[transformed] <current_datetime>now</current_datetime>\nhello world"
    );
}

#[tokio::test]
async fn dispatch_unregistered_hook_returns_empty() {
    let hooks = TestHooks;
    let input = serde_json::json!({
        "sessionId": "sess-1",
        "timestamp": 1234567890,
        "cwd": "/tmp",
        "reason": "complete"
    });
    // TestHooks doesn't handle SessionEnd
    let result = dispatch_hook(&hooks, &SessionId::new("sess-1"), "sessionEnd", input)
        .await
        .unwrap();
    assert_eq!(result["output"], serde_json::json!({}));
}

#[tokio::test]
async fn dispatch_unknown_hook_type() {
    let hooks = TestHooks;
    let input = serde_json::json!({});
    let result = dispatch_hook(&hooks, &SessionId::new("sess-1"), "unknownHook", input)
        .await
        .unwrap();
    assert_eq!(result["output"], serde_json::json!({}));
}

#[tokio::test]
async fn dispatch_subagent_hooks_with_typed_input_and_output() {
    struct SubagentHooks;
    #[async_trait]
    impl SessionHooks for SubagentHooks {
        async fn on_subagent_start(
            &self,
            input: SubagentStartInput,
            ctx: HookContext,
        ) -> Option<SubagentStartOutput> {
            assert_eq!(ctx.session_id, SessionId::new("sess-1"));
            assert_eq!(input.session_id, "sess-1");
            assert_eq!(input.timestamp, 1234567890.0);
            assert_eq!(input.working_directory, PathBuf::from("/tmp"));
            assert_eq!(
                input.transcript_path,
                PathBuf::from("/tmp/transcript.jsonl")
            );
            assert_eq!(input.agent_name, "explore");
            assert_eq!(input.agent_display_name.as_deref(), Some("Explore Agent"));
            assert_eq!(input.agent_description.as_deref(), Some("Read code"));
            Some(SubagentStartOutput {
                additional_context: Some("Follow the file".to_string()),
            })
        }

        async fn on_subagent_stop(
            &self,
            input: SubagentStopInput,
            ctx: HookContext,
        ) -> Option<SubagentStopOutput> {
            assert_eq!(ctx.session_id, SessionId::new("sess-1"));
            assert_eq!(input.session_id, "sess-1");
            assert_eq!(input.timestamp, 1234567891.0);
            assert_eq!(input.working_directory, PathBuf::from("/tmp"));
            assert_eq!(
                input.transcript_path,
                PathBuf::from("/tmp/transcript.jsonl")
            );
            assert_eq!(input.agent_name, "explore");
            assert_eq!(input.agent_type, "explore");
            assert_eq!(input.agent_id.as_deref(), Some("read-file"));
            assert_eq!(input.agent_display_name.as_deref(), Some("Explore Agent"));
            assert_eq!(input.agent_description.as_deref(), Some("Read code"));
            assert_eq!(input.stop_reason, "end_turn");
            assert_eq!(input.response, "original answer");
            Some(SubagentStopOutput {
                modified_response: Some("rewritten answer".to_string()),
                ..Default::default()
            })
        }
    }

    let common = serde_json::json!({
        "sessionId": "sess-1",
        "timestamp": 1234567890,
        "cwd": "/tmp",
        "transcriptPath": "/tmp/transcript.jsonl",
        "agentName": "explore",
        "agentDisplayName": "Explore Agent",
        "agentDescription": "Read code"
    });
    let start = dispatch_hook(
        &SubagentHooks,
        &SessionId::new("sess-1"),
        "subagentStart",
        common.clone(),
    )
    .await
    .unwrap();
    assert_eq!(
        start,
        serde_json::json!({ "output": { "additionalContext": "Follow the file" } })
    );

    let mut stop = common;
    let object = stop.as_object_mut().unwrap();
    object.insert("timestamp".to_string(), serde_json::json!(1234567891));
    object.insert("agentType".to_string(), serde_json::json!("explore"));
    object.insert("agentId".to_string(), serde_json::json!("read-file"));
    object.insert("stopReason".to_string(), serde_json::json!("end_turn"));
    object.insert("response".to_string(), serde_json::json!("original answer"));
    let stopped = dispatch_hook(
        &SubagentHooks,
        &SessionId::new("sess-1"),
        "subagentStop",
        stop,
    )
    .await
    .unwrap();
    assert_eq!(
        stopped,
        serde_json::json!({ "output": { "modifiedResponse": "rewritten answer" } })
    );
}

#[tokio::test]
async fn dispatch_subagent_stop_block_output() {
    struct BlockHook;
    #[async_trait]
    impl SessionHooks for BlockHook {
        async fn on_subagent_stop(
            &self,
            _input: SubagentStopInput,
            _ctx: HookContext,
        ) -> Option<SubagentStopOutput> {
            Some(SubagentStopOutput {
                decision: Some("block".to_string()),
                reason: Some("Keep researching".to_string()),
                ..Default::default()
            })
        }
    }

    let input = serde_json::json!({
        "sessionId": "sess-1",
        "timestamp": 1234567890,
        "cwd": "/tmp",
        "transcriptPath": "/tmp/transcript.jsonl",
        "agentName": "explore",
        "agentType": "explore",
        "stopReason": "end_turn",
        "response": "draft answer"
    });
    let result = dispatch_hook(&BlockHook, &SessionId::new("sess-1"), "subagentStop", input)
        .await
        .unwrap();
    assert_eq!(
        result,
        serde_json::json!({ "output": { "decision": "block", "reason": "Keep researching" } })
    );
}

#[tokio::test]
async fn dispatch_mismatched_output_returns_empty() {
    struct MismatchHooks;
    #[async_trait]
    impl SessionHooks for MismatchHooks {
        async fn on_hook(&self, _event: HookEvent) -> HookOutput {
            // Always return SessionEnd output regardless of event type
            HookOutput::SessionEnd(SessionEndOutput {
                session_summary: Some("oops".to_string()),
                ..Default::default()
            })
        }
    }

    let hooks = MismatchHooks;
    let input = serde_json::json!({
        "sessionId": "sess-1",
        "timestamp": 1234567890,
        "cwd": "/tmp",
        "toolName": "some_tool",
        "toolArgs": {}
    });
    // preToolUse event gets a SessionEnd output — should be treated as empty
    let result = dispatch_hook(&hooks, &SessionId::new("sess-1"), "preToolUse", input)
        .await
        .unwrap();
    assert_eq!(result["output"], serde_json::json!({}));
}

#[tokio::test]
async fn dispatch_post_tool_use_default() {
    let hooks = TestHooks;
    let input = serde_json::json!({
        "sessionId": "sess-1",
        "timestamp": 1234567890,
        "cwd": "/tmp",
        "toolName": "some_tool",
        "toolArgs": {},
        "toolResult": "success"
    });
    let result = dispatch_hook(&hooks, &SessionId::new("sess-1"), "postToolUse", input)
        .await
        .unwrap();
    assert_eq!(result["output"], serde_json::json!({}));
}

#[tokio::test]
async fn dispatch_post_tool_use_failure_default() {
    // No handler override — should return an empty output object.
    let hooks = TestHooks;
    let input = serde_json::json!({
        "sessionId": "sess-1",
        "timestamp": 1234567890,
        "cwd": "/tmp",
        "toolName": "some_tool",
        "toolArgs": {"key": "value"},
        "error": "boom"
    });
    let result = dispatch_hook(
        &hooks,
        &SessionId::new("sess-1"),
        "postToolUseFailure",
        input,
    )
    .await
    .unwrap();
    assert_eq!(result["output"], serde_json::json!({}));
}

#[tokio::test]
async fn dispatch_post_tool_use_failure_returns_additional_context() {
    struct FailureHooks;
    #[async_trait]
    impl SessionHooks for FailureHooks {
        async fn on_post_tool_use_failure(
            &self,
            input: PostToolUseFailureInput,
            _ctx: HookContext,
        ) -> Option<PostToolUseFailureOutput> {
            assert_eq!(input.session_id, "sess-1");
            assert_eq!(input.tool_name, "some_tool");
            assert_eq!(input.error, "boom");
            assert_eq!(input.working_directory, PathBuf::from("/tmp"));
            Some(PostToolUseFailureOutput {
                additional_context: Some(format!(
                    "tool {} failed: {}",
                    input.tool_name, input.error
                )),
            })
        }
    }

    let input = serde_json::json!({
        "sessionId": "sess-1",
        "timestamp": 1234567890,
        "cwd": "/tmp",
        "toolName": "some_tool",
        "toolArgs": {},
        "error": "boom"
    });
    let result = dispatch_hook(
        &FailureHooks,
        &SessionId::new("sess-1"),
        "postToolUseFailure",
        input,
    )
    .await
    .unwrap();
    assert_eq!(
        result["output"]["additionalContext"],
        "tool some_tool failed: boom"
    );
}

#[tokio::test]
async fn dispatch_post_tool_use_failure_invalid_input_errors() {
    // Missing required `error` field — dispatcher should surface the
    // deserialization error rather than dispatching with empty input.
    let hooks = TestHooks;
    let input = serde_json::json!({
        "sessionId": "sess-1",
        "timestamp": 1234567890,
        "cwd": "/tmp",
        "toolName": "some_tool",
        "toolArgs": {}
    });
    let err = dispatch_hook(
        &hooks,
        &SessionId::new("sess-1"),
        "postToolUseFailure",
        input,
    )
    .await
    .unwrap_err();
    let msg = err.to_string().to_ascii_lowercase();
    assert!(
        msg.contains("error") || msg.contains("missing field"),
        "unexpected error: {msg}"
    );
}

#[tokio::test]
async fn dispatch_session_start() {
    struct StartHooks;
    #[async_trait]
    impl SessionHooks for StartHooks {
        async fn on_hook(&self, event: HookEvent) -> HookOutput {
            match event {
                HookEvent::SessionStart { .. } => HookOutput::SessionStart(SessionStartOutput {
                    additional_context: Some("extra context".to_string()),
                    ..Default::default()
                }),
                _ => HookOutput::None,
            }
        }
    }

    let hooks = StartHooks;
    let input = serde_json::json!({
        "sessionId": "sess-1",
        "timestamp": 1234567890,
        "cwd": "/tmp",
        "source": "new"
    });
    let result = dispatch_hook(&hooks, &SessionId::new("sess-1"), "sessionStart", input)
        .await
        .unwrap();
    assert_eq!(result["output"]["additionalContext"], "extra context");
}

#[tokio::test]
async fn dispatch_error_occurred() {
    struct ErrorHooks;
    #[async_trait]
    impl SessionHooks for ErrorHooks {
        async fn on_hook(&self, event: HookEvent) -> HookOutput {
            match event {
                HookEvent::ErrorOccurred { .. } => HookOutput::ErrorOccurred(ErrorOccurredOutput {
                    error_handling: Some("retry".to_string()),
                    retry_count: Some(3),
                    ..Default::default()
                }),
                _ => HookOutput::None,
            }
        }
    }

    let hooks = ErrorHooks;
    let input = serde_json::json!({
        "sessionId": "sess-1",
        "timestamp": 1234567890,
        "cwd": "/tmp",
        "error": "model timeout",
        "errorContext": "model_call",
        "recoverable": true
    });
    let result = dispatch_hook(&hooks, &SessionId::new("sess-1"), "errorOccurred", input)
        .await
        .unwrap();
    assert_eq!(result["output"]["errorHandling"], "retry");
    assert_eq!(result["output"]["retryCount"], 3);
}

#[tokio::test]
async fn dispatch_agent_stop_block() {
    struct AgentStopHooks;
    #[async_trait]
    impl SessionHooks for AgentStopHooks {
        async fn on_agent_stop(
            &self,
            input: AgentStopInput,
            ctx: HookContext,
        ) -> Option<AgentStopOutput> {
            assert_eq!(ctx.session_id, SessionId::new("sess-1"));
            assert_eq!(input.session_id, "sess-1");
            assert_eq!(input.stop_reason.as_deref(), Some("end_turn"));
            assert_eq!(
                input.transcript_path,
                Some(PathBuf::from("/tmp/transcript.jsonl"))
            );
            assert_eq!(input.stop_hook_active, Some(true));
            Some(AgentStopOutput {
                decision: Some("block".to_string()),
                reason: Some("finish the remaining work".to_string()),
            })
        }
    }

    let input = serde_json::json!({
        "sessionId": "sess-1",
        "timestamp": 1234567890,
        "cwd": "/tmp",
        "stopReason": "end_turn",
        "transcriptPath": "/tmp/transcript.jsonl",
        "stop_hook_active": true
    });
    let result = dispatch_hook(
        &AgentStopHooks,
        &SessionId::new("sess-1"),
        "agentStop",
        input,
    )
    .await
    .unwrap();

    assert_eq!(result["output"]["decision"], "block");
    assert_eq!(result["output"]["reason"], "finish the remaining work");
}