github-copilot-sdk 1.0.0-beta.10

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

//! Client-level "empty" mode for minimal/safe defaults.
//!
//! See the plan in <https://github.com/github/copilot-agent-runtime/issues/7155>:
//! [`ClientMode::Empty`] disables ambient CLI-style behavior by default so an
//! app must explicitly opt back into features. This module exposes the public
//! enum, the [`ToolSet`] builder for source-qualified tool filter patterns,
//! and the [`BUILTIN_TOOLS_ISOLATED`] curated allowlist.

use std::collections::HashMap;

use crate::types::{SectionOverride, SystemMessageConfig};

/// Controls SDK defaults for ambient CLI-style behavior.
///
/// - [`ClientMode::CopilotCli`] (default): defaults equivalent to Copilot CLI.
///   Useful when building a coding agent that shares sessions with Copilot CLI.
///   **Do not use this mode for server-based multi-user applications** — the
///   default coding agent has tools and capabilities that operate across
///   sessions and can access the host OS environment.
/// - [`ClientMode::Empty`]: disables optional features by default. The app
///   must explicitly opt into anything it needs. Required for any scenario
///   where CLI-like ambient behavior is unsafe (e.g. multi-user servers).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ClientMode {
    /// Defaults equivalent to Copilot CLI (the default).
    #[default]
    CopilotCli,
    /// Disables optional features by default; app must opt in explicitly.
    Empty,
}

/// Tool name character set enforced by the runtime at every registration
/// boundary. Mirrors the runtime's `VALID_TOOL_NAME_REGEX`.
fn is_valid_tool_name(name: &str) -> bool {
    !name.is_empty()
        && name
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
}

fn validate_name(kind: &str, name: &str) -> Result<(), crate::Error> {
    if name == "*" {
        return Ok(());
    }
    if !is_valid_tool_name(name) {
        return Err(crate::Error::with_message(
            crate::ErrorKind::InvalidConfig,
            format!(
                "Invalid {kind} tool name '{name}': tool names must match \
             /^[a-zA-Z0-9_-]+$/ or be the wildcard '*'."
            ),
        ));
    }
    Ok(())
}

/// Builder that produces source-qualified tool filter strings (e.g.
/// `"builtin:bash"`, `"mcp:*"`, `"custom:foo"`) for the session's
/// `available_tools` list.
///
/// Tools are classified by the runtime at registration time, not from name
/// parsing — so `add_builtin("foo")` matches only tools registered as
/// built-in, even if an MCP server happens to register a tool with the same
/// wire name.
///
/// # Example
///
/// ```
/// # use github_copilot_sdk::mode::{ToolSet, BUILTIN_TOOLS_ISOLATED};
/// let tools = ToolSet::new()
///     .add_builtin_many(BUILTIN_TOOLS_ISOLATED)?
///     .add_mcp("*")?
///     .add_custom("*")?
///     .to_vec();
/// # Ok::<(), github_copilot_sdk::Error>(())
/// ```
#[derive(Debug, Clone, Default)]
pub struct ToolSet {
    items: Vec<String>,
}

impl ToolSet {
    /// Construct an empty tool set.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a single built-in tool pattern. Pass a specific name (e.g.
    /// `"bash"`) or `"*"` to match all built-in tools.
    pub fn add_builtin(mut self, name: &str) -> Result<Self, crate::Error> {
        validate_name("builtin", name)?;
        self.items.push(format!("builtin:{name}"));
        Ok(self)
    }

    /// Add a list of built-in tool patterns (e.g. [`BUILTIN_TOOLS_ISOLATED`]).
    pub fn add_builtin_many<I, S>(mut self, names: I) -> Result<Self, crate::Error>
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        for name in names {
            let name = name.as_ref();
            validate_name("builtin", name)?;
            self.items.push(format!("builtin:{name}"));
        }
        Ok(self)
    }

    /// Add a custom tool pattern. Matches tools registered via the SDK's
    /// `tools` option or via custom agents.
    pub fn add_custom(mut self, name: &str) -> Result<Self, crate::Error> {
        validate_name("custom", name)?;
        self.items.push(format!("custom:{name}"));
        Ok(self)
    }

    /// Add an MCP tool pattern. Pass the runtime's canonical wire name
    /// (e.g. `"github-list_issues"`) or `"*"` to match all MCP tools.
    pub fn add_mcp(mut self, tool_name: &str) -> Result<Self, crate::Error> {
        validate_name("mcp", tool_name)?;
        self.items.push(format!("mcp:{tool_name}"));
        Ok(self)
    }

    /// Returns a defensive copy of the accumulated filter strings.
    pub fn to_vec(&self) -> Vec<String> {
        self.items.clone()
    }

    /// Returns the accumulated filter strings, consuming the builder.
    pub fn into_vec(self) -> Vec<String> {
        self.items
    }

    /// Number of accumulated filter strings.
    pub fn len(&self) -> usize {
        self.items.len()
    }

    /// Returns `true` if no filter strings have been added.
    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }
}

impl From<ToolSet> for Vec<String> {
    fn from(value: ToolSet) -> Self {
        value.into_vec()
    }
}

/// Built-in tools that operate only within the bounds of a single session —
/// no host filesystem access outside the session, no cross-session state,
/// no host environment access, no network.
///
/// Safe to enable in [`ClientMode::Empty`] scenarios (e.g. multi-tenant
/// servers) without leaking host capabilities.
///
/// **Contract:** tools in this set MUST NOT be extended (even behind options
/// or args) to read or write state outside the session boundary. Adding
/// cross-session or host-state behavior to one of these tools is a breaking
/// change that requires removing it from this set.
pub const BUILTIN_TOOLS_ISOLATED: &[&str] = &[
    "ask_user",
    "task_complete",
    "exit_plan_mode",
    "task",
    "read_agent",
    "write_agent",
    "list_agents",
    "send_inbox",
    "context_board",
    "skill",
];

/// Validate a tool filter list (`available_tools` or `excluded_tools`).
/// Rejects the bare `"*"` shorthand with a clear error pointing the developer
/// at the source-qualified forms.
pub(crate) fn validate_tool_filter_list(
    field: &str,
    list: Option<&[String]>,
) -> Result<(), crate::Error> {
    let Some(list) = list else { return Ok(()) };
    for item in list {
        if item == "*" {
            return Err(crate::Error::with_message(
                crate::ErrorKind::InvalidConfig,
                format!(
                    "{field} contains a bare '*' which matches no tool. Use \
                 source-qualified wildcards instead: \
                 ToolSet::new().add_builtin(\"*\").add_mcp(\"*\").add_custom(\"*\")."
                ),
            ));
        }
    }
    Ok(())
}

/// Returns the system message config to use, adjusted for the current mode.
/// In empty mode we ensure the `environment_context` section is removed
/// unless the app has already taken control of it.
pub(crate) fn system_message_for_mode(
    mode: ClientMode,
    supplied: Option<SystemMessageConfig>,
) -> Option<SystemMessageConfig> {
    if mode != ClientMode::Empty {
        return supplied;
    }
    let strip_env = || {
        let mut sections = HashMap::new();
        sections.insert(
            "environment_context".to_string(),
            SectionOverride {
                action: Some("remove".to_string()),
                content: None,
            },
        );
        sections
    };
    let Some(supplied) = supplied else {
        return Some(SystemMessageConfig {
            mode: Some("customize".to_string()),
            content: None,
            sections: Some(strip_env()),
        });
    };
    let mode_str = supplied.mode.as_deref().unwrap_or("append");
    match mode_str {
        "replace" => Some(supplied),
        "customize" => {
            if supplied
                .sections
                .as_ref()
                .is_some_and(|s| s.contains_key("environment_context"))
            {
                Some(supplied)
            } else {
                let mut sections = supplied.sections.unwrap_or_default();
                sections.insert(
                    "environment_context".to_string(),
                    SectionOverride {
                        action: Some("remove".to_string()),
                        content: None,
                    },
                );
                Some(SystemMessageConfig {
                    mode: Some("customize".to_string()),
                    content: supplied.content,
                    sections: Some(sections),
                })
            }
        }
        // "append" or any unrecognized value: promote to customize so we
        // can also strip environment_context; the runtime appends `content`
        // to additional instructions either way.
        _ => Some(SystemMessageConfig {
            mode: Some("customize".to_string()),
            content: supplied.content,
            sections: Some(strip_env()),
        }),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn tool_set_emits_source_qualified_patterns() {
        let v = ToolSet::new()
            .add_builtin("bash")
            .unwrap()
            .add_builtin("*")
            .unwrap()
            .add_custom("foo")
            .unwrap()
            .add_custom("*")
            .unwrap()
            .add_mcp("github-list_issues")
            .unwrap()
            .add_mcp("*")
            .unwrap()
            .to_vec();
        assert_eq!(
            v,
            vec![
                "builtin:bash",
                "builtin:*",
                "custom:foo",
                "custom:*",
                "mcp:github-list_issues",
                "mcp:*",
            ]
        );
    }

    #[test]
    fn tool_set_add_builtin_many() {
        let v = ToolSet::new()
            .add_builtin_many(BUILTIN_TOOLS_ISOLATED)
            .unwrap()
            .into_vec();
        assert_eq!(v.len(), BUILTIN_TOOLS_ISOLATED.len());
        assert_eq!(v[0], format!("builtin:{}", BUILTIN_TOOLS_ISOLATED[0]));
    }

    #[test]
    fn tool_set_rejects_invalid_names() {
        for bad in ["bash!", "with space", "colon:name", "", "wild*card"] {
            assert!(
                ToolSet::new().add_builtin(bad).is_err(),
                "expected '{bad}' to be rejected"
            );
            assert!(ToolSet::new().add_custom(bad).is_err());
            assert!(ToolSet::new().add_mcp(bad).is_err());
        }
    }

    #[test]
    fn tool_set_accepts_wildcard_and_underscores_and_dashes() {
        assert!(ToolSet::new().add_builtin("*").is_ok());
        assert!(ToolSet::new().add_mcp("github-list_issues").is_ok());
        assert!(ToolSet::new().add_custom("A_b-9").is_ok());
    }

    #[test]
    fn into_vec_is_idempotent_with_to_vec() {
        let ts = ToolSet::new().add_builtin("bash").unwrap();
        assert_eq!(ts.to_vec(), vec!["builtin:bash"]);
        assert_eq!(ts.into_vec(), vec!["builtin:bash"]);
    }

    #[test]
    fn into_vec_string_conversion() {
        let v: Vec<String> = ToolSet::new().add_mcp("*").unwrap().into();
        assert_eq!(v, vec!["mcp:*"]);
    }

    #[test]
    fn validate_tool_filter_list_rejects_bare_star() {
        let bad = vec!["*".to_string()];
        assert!(validate_tool_filter_list("availableTools", Some(&bad)).is_err());
    }

    #[test]
    fn validate_tool_filter_list_allows_qualified_star() {
        let ok = vec!["builtin:*".to_string(), "mcp:*".to_string()];
        assert!(validate_tool_filter_list("availableTools", Some(&ok)).is_ok());
    }

    #[test]
    fn validate_tool_filter_list_none_is_ok() {
        assert!(validate_tool_filter_list("availableTools", None).is_ok());
    }

    #[test]
    fn builtin_tools_isolated_contents() {
        assert!(BUILTIN_TOOLS_ISOLATED.contains(&"ask_user"));
        assert!(BUILTIN_TOOLS_ISOLATED.contains(&"task_complete"));
        assert!(BUILTIN_TOOLS_ISOLATED.contains(&"skill"));
        assert!(!BUILTIN_TOOLS_ISOLATED.contains(&"bash"));
        assert!(!BUILTIN_TOOLS_ISOLATED.contains(&"edit"));
        assert!(!BUILTIN_TOOLS_ISOLATED.contains(&"web_fetch"));
    }

    #[test]
    fn client_mode_default_is_copilot_cli() {
        assert_eq!(ClientMode::default(), ClientMode::CopilotCli);
    }

    #[test]
    fn system_message_copilot_cli_passes_through_unchanged() {
        let cfg = SystemMessageConfig {
            mode: Some("append".to_string()),
            content: Some("hello".to_string()),
            sections: None,
        };
        let out = system_message_for_mode(ClientMode::CopilotCli, Some(cfg.clone()));
        let out = out.unwrap();
        assert_eq!(out.mode.as_deref(), Some("append"));
        assert_eq!(out.content.as_deref(), Some("hello"));
    }

    #[test]
    fn system_message_empty_none_injects_strip() {
        let out = system_message_for_mode(ClientMode::Empty, None).unwrap();
        assert_eq!(out.mode.as_deref(), Some("customize"));
        let sections = out.sections.unwrap();
        let env = sections.get("environment_context").unwrap();
        assert_eq!(env.action.as_deref(), Some("remove"));
    }

    #[test]
    fn system_message_empty_append_promoted_to_customize() {
        let cfg = SystemMessageConfig {
            mode: Some("append".to_string()),
            content: Some("hi".to_string()),
            sections: None,
        };
        let out = system_message_for_mode(ClientMode::Empty, Some(cfg)).unwrap();
        assert_eq!(out.mode.as_deref(), Some("customize"));
        assert_eq!(out.content.as_deref(), Some("hi"));
        let sections = out.sections.unwrap();
        assert!(sections.contains_key("environment_context"));
    }

    #[test]
    fn system_message_empty_replace_passes_through() {
        let cfg = SystemMessageConfig {
            mode: Some("replace".to_string()),
            content: Some("verbatim".to_string()),
            sections: None,
        };
        let out = system_message_for_mode(ClientMode::Empty, Some(cfg.clone())).unwrap();
        assert_eq!(out.mode.as_deref(), Some("replace"));
        assert_eq!(out.content.as_deref(), Some("verbatim"));
        assert!(out.sections.is_none());
    }

    #[test]
    fn system_message_empty_customize_with_env_context_preserved() {
        let mut sections = HashMap::new();
        sections.insert(
            "environment_context".to_string(),
            SectionOverride {
                action: Some("replace".to_string()),
                content: Some("custom env".to_string()),
            },
        );
        let cfg = SystemMessageConfig {
            mode: Some("customize".to_string()),
            content: None,
            sections: Some(sections),
        };
        let out = system_message_for_mode(ClientMode::Empty, Some(cfg)).unwrap();
        let env = out.sections.unwrap().remove("environment_context").unwrap();
        assert_eq!(env.action.as_deref(), Some("replace"));
        assert_eq!(env.content.as_deref(), Some("custom env"));
    }

    #[test]
    fn system_message_empty_customize_without_env_context_gets_strip() {
        let mut sections = HashMap::new();
        sections.insert(
            "other_section".to_string(),
            SectionOverride {
                action: Some("replace".to_string()),
                content: Some("body".to_string()),
            },
        );
        let cfg = SystemMessageConfig {
            mode: Some("customize".to_string()),
            content: None,
            sections: Some(sections),
        };
        let out = system_message_for_mode(ClientMode::Empty, Some(cfg)).unwrap();
        let secs = out.sections.unwrap();
        assert!(secs.contains_key("other_section"));
        let env = secs.get("environment_context").unwrap();
        assert_eq!(env.action.as_deref(), Some("remove"));
    }
}