subspace_client_github/copilot_agents/
validate.rs1use subspace_core::http::Error;
4
5use super::MAX_TASK_ID_CHARS;
6use super::MODELS;
7use super::{MAX_PROMPT_UTF16, MAX_REF_CHARS, MAX_SUBJECT_ID_CHARS, StartTask, Subject, TaskScope};
8use crate::validate::{check_repo, invalid};
9
10pub fn validate_task_id(task_id: &str) -> Result<(), Error> {
12 if task_id.is_empty()
13 || task_id.len() > MAX_TASK_ID_CHARS
14 || !task_id
15 .bytes()
16 .all(|b| b.is_ascii_alphanumeric() || b == b'-')
17 {
18 return Err(invalid("task id is invalid."));
19 }
20 Ok(())
21}
22
23pub fn validate_subject(subject: &Subject) -> Result<(), Error> {
24 if subject.id.is_empty()
25 || subject.id.len() > MAX_SUBJECT_ID_CHARS
26 || !subject
27 .id
28 .bytes()
29 .all(|b| b.is_ascii_alphanumeric() || b"._:/-".contains(&b))
30 {
31 return Err(invalid("subject id must be 1-200 safe characters."));
32 }
33 Ok(())
34}
35fn ref_charset_ok(value: &str) -> bool {
36 !value.is_empty()
37 && value.len() <= MAX_REF_CHARS
38 && value
39 .bytes()
40 .all(|b| b.is_ascii_alphanumeric() || b"._/-".contains(&b))
41}
42
43fn ref_edges_ok(value: &str) -> bool {
44 !value.starts_with('/')
45 && !value.starts_with('-')
46 && !value.ends_with('/')
47 && !value.ends_with('.')
48}
49
50fn ref_segments_ok(value: &str) -> bool {
51 !value.contains("..")
52 && !value.contains("//")
53 && value
54 .split('/')
55 .all(|part| !part.starts_with('.') && !part.ends_with(".lock"))
56}
57
58pub fn validate_ref(value: &str) -> bool {
59 ref_charset_ok(value) && ref_edges_ok(value) && ref_segments_ok(value)
60}
61pub fn validate_prompt(prompt: &str) -> Result<(), Error> {
62 if prompt.trim().is_empty() {
63 return Err(invalid("prompt must be a non-empty string."));
64 }
65 if prompt.encode_utf16().count() > MAX_PROMPT_UTF16 {
66 return Err(invalid("prompt must be at most 60000 UTF-16 units."));
67 }
68 Ok(())
69}
70pub fn validate_start(input: &StartTask, scope: &TaskScope) -> Result<String, Error> {
71 validate_prompt(&input.prompt)?;
72 let (owner, name) = check_repo(&input.repo)?;
73 let repo = scope
74 .configured_repos
75 .iter()
76 .find(|r| r.eq_ignore_ascii_case(&format!("{owner}/{name}")))
77 .cloned()
78 .ok_or_else(|| invalid("repo is not allowed for cloud agent tasks."))?;
79 if let Some(model) = &input.model
80 && !MODELS.iter().flatten().any(|known| known == model)
81 {
82 return Err(invalid(
83 "model is not in the GitHub cloud-agent model whitelist.",
84 ));
85 }
86 if let Some(reference) = &input.base_ref
87 && !validate_ref(reference)
88 {
89 return Err(invalid("base_ref must be a valid branch name."));
90 }
91 if let Some(subject) = &input.subject {
92 validate_subject(subject)?;
93 }
94 Ok(repo)
95}