wsx-core 0.25.0

Composable project, Git worktree, terminal runtime, hook, and model primitives for wsx.
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
//! Structured executable review protocol. See docs/worktree-review.md.
//! These types do not grant plugin execution or filesystem authority.

use super::WorktreeId;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

pub const REVIEW_API_VERSION: u32 = 1;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReviewComparison {
    WorkingAgainstHead,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReviewSpec {
    pub api_version: u32,
    pub priority: i32,
    pub comparisons: Vec<ReviewComparison>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReviewLimits {
    pub files: usize,
    pub hunks: usize,
    pub lines: usize,
}

impl Default for ReviewLimits {
    fn default() -> Self {
        Self {
            files: 1_000,
            hunks: 256,
            lines: 10_000,
        }
    }
}

impl ReviewLimits {
    pub fn is_valid(&self) -> bool {
        (1..=1_000).contains(&self.files)
            && (1..=256).contains(&self.hunks)
            && (1..=10_000).contains(&self.lines)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReviewRequest {
    pub api_version: u32,
    pub request_id: String,
    pub worktree_id: WorktreeId,
    pub worktree_path: PathBuf,
    pub comparison: ReviewComparison,
    pub limits: ReviewLimits,
    #[serde(flatten)]
    pub operation: ReviewOperation,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "operation", rename_all = "snake_case")]
pub enum ReviewOperation {
    ListFiles,
    FileDiff { snapshot: String, file_id: String },
}

impl ReviewRequest {
    pub fn validate(&self) -> Result<(), &'static str> {
        if self.api_version != REVIEW_API_VERSION
            || !self.limits.is_valid()
            || !token(&self.request_id)
        {
            return Err("invalid review request");
        }
        if let ReviewOperation::FileDiff { snapshot, file_id } = &self.operation {
            if !token(snapshot) || !token(file_id) {
                return Err("invalid review request identity");
            }
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReviewFileStatus {
    Added,
    Modified,
    Deleted,
    Renamed,
    Copied,
    Untracked,
    Unmerged,
    TypeChanged,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReviewContentKind {
    Text,
    Binary,
    Submodule,
    Unreadable,
    Oversized,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReviewFile {
    pub file_id: String,
    pub old_path: Option<String>,
    pub new_path: Option<String>,
    pub status: ReviewFileStatus,
    pub content_kind: ReviewContentKind,
    pub additions: Option<u64>,
    pub deletions: Option<u64>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReviewFileList {
    pub snapshot: String,
    pub comparison_label: String,
    pub files: Vec<ReviewFile>,
    /// None means the producer cannot determine the number omitted.
    pub omitted_files: Option<usize>,
    pub truncated: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReviewDiff {
    pub snapshot: String,
    pub file_id: String,
    pub content_kind: ReviewContentKind,
    pub hunks: Vec<ReviewHunk>,
    pub truncated: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReviewHunk {
    pub old_start: u64,
    pub old_count: u64,
    pub new_start: u64,
    pub new_count: u64,
    pub heading: String,
    pub lines: Vec<ReviewLine>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", content = "text", rename_all = "snake_case")]
pub enum ReviewLine {
    Context(String),
    Addition(String),
    Deletion(String),
    NoNewline,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReviewErrorCode {
    Unsupported,
    StaleSnapshot,
    Unavailable,
    InvalidRequest,
    LimitExceeded,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReviewResponse {
    pub api_version: u32,
    pub request_id: String,
    #[serde(flatten)]
    pub result: ReviewResult,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "result", content = "data", rename_all = "snake_case")]
pub enum ReviewResult {
    Files(ReviewFileList),
    Diff(ReviewDiff),
    Error {
        code: ReviewErrorCode,
        message: String,
    },
}

// ^ docs/worktree-review.md: decoding alone does not validate provider output.
impl ReviewResponse {
    pub fn validate_for(&self, request: &ReviewRequest) -> Result<(), &'static str> {
        if request.validate().is_err()
            || self.api_version != REVIEW_API_VERSION
            || self.request_id != request.request_id
        {
            return Err("invalid review envelope");
        }
        match (&request.operation, &self.result) {
            (_, ReviewResult::Error { message, .. }) if text(message) => Ok(()),
            (ReviewOperation::ListFiles, ReviewResult::Files(list)) => {
                if !token(&list.snapshot)
                    || !text(&list.comparison_label)
                    || list.files.len() > request.limits.files
                    || (!list.truncated && list.omitted_files != Some(0))
                {
                    return Err("invalid file list");
                }
                let mut ids = std::collections::HashSet::new();
                for file in &list.files {
                    if !token(&file.file_id)
                        || !ids.insert(&file.file_id)
                        || (file.old_path.is_none() && file.new_path.is_none())
                        || file
                            .old_path
                            .iter()
                            .chain(file.new_path.iter())
                            .any(|p| !relative_path(p))
                    {
                        return Err("invalid file identity or path");
                    }
                }
                Ok(())
            }
            (ReviewOperation::FileDiff { snapshot, file_id }, ReviewResult::Diff(diff)) => {
                if !token(snapshot)
                    || !token(file_id)
                    || diff.snapshot != *snapshot
                    || diff.file_id != *file_id
                    || diff.hunks.len() > request.limits.hunks
                    || (diff.content_kind != ReviewContentKind::Text && !diff.hunks.is_empty())
                {
                    return Err("invalid diff identity or kind");
                }
                let mut total = 0usize;
                for hunk in &diff.hunks {
                    total = total
                        .checked_add(hunk.lines.len())
                        .ok_or("too many lines")?;
                    if total > request.limits.lines
                        || !text(&hunk.heading)
                        || hunk.old_start.checked_add(hunk.old_count).is_none()
                        || hunk.new_start.checked_add(hunk.new_count).is_none()
                    {
                        return Err("invalid hunk bounds");
                    }
                    let (mut old, mut new) = (0u64, 0u64);
                    let mut previous_content = false;
                    for line in &hunk.lines {
                        match line {
                            ReviewLine::Context(value) if line_text(value) => {
                                old += 1;
                                new += 1;
                            }
                            ReviewLine::Addition(value) if line_text(value) => new += 1,
                            ReviewLine::Deletion(value) if line_text(value) => old += 1,
                            ReviewLine::NoNewline if previous_content => {}
                            _ => return Err("invalid diff line"),
                        }
                        previous_content = !matches!(line, ReviewLine::NoNewline);
                    }
                    // Truncation drops whole hunks, never silently incomplete ranges.
                    if old != hunk.old_count || new != hunk.new_count {
                        return Err("hunk ranges do not match lines");
                    }
                }
                Ok(())
            }
            _ => Err("unexpected review result"),
        }
    }
}

fn text(value: &str) -> bool {
    value.len() <= 4096 && !value.chars().any(char::is_control)
}

fn line_text(value: &str) -> bool {
    value.len() <= 4096 && !value.chars().any(|c| c.is_control() && c != '\t')
}

fn token(value: &str) -> bool {
    !value.is_empty() && value.len() <= 512 && text(value)
}

fn relative_path(value: &str) -> bool {
    !value.is_empty()
        && text(value)
        && std::path::Path::new(value)
            .components()
            .all(|part| matches!(part, std::path::Component::Normal(_)))
}

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

    #[test]
    fn raw_stale_snapshot_is_an_error_not_an_empty_diff() {
        let response: ReviewResponse = serde_json::from_str(
            r#"{"api_version":1,"request_id":"r1","result":"error","data":{"code":"stale_snapshot","message":"refresh required"}}"#,
        ).unwrap();
        assert!(matches!(
            response.result,
            ReviewResult::Error {
                code: ReviewErrorCode::StaleSnapshot,
                ..
            }
        ));
    }

    #[test]
    fn raw_diff_preserves_no_newline_and_zero_length_ranges() {
        let diff: ReviewDiff = serde_json::from_str(
            r#"{"snapshot":"s1","file_id":"f1","content_kind":"text","truncated":false,"hunks":[{"old_start":0,"old_count":0,"new_start":1,"new_count":1,"heading":"","lines":[{"kind":"addition","text":"hello"},{"kind":"no_newline"}]}]}"#,
        ).unwrap();
        assert_eq!(diff.hunks[0].old_count, 0);
        assert_eq!(diff.hunks[0].lines[1], ReviewLine::NoNewline);
    }

    #[test]
    fn response_validation_rejects_mismatched_identity_and_hunk_counts() {
        let request = ReviewRequest {
            api_version: 1,
            request_id: "r1".into(),
            worktree_id: WorktreeId(1),
            worktree_path: "/repo".into(),
            comparison: ReviewComparison::WorkingAgainstHead,
            limits: ReviewLimits::default(),
            operation: ReviewOperation::FileDiff {
                snapshot: "s1".into(),
                file_id: "f1".into(),
            },
        };
        let mut response = ReviewResponse {
            api_version: 1,
            request_id: "r1".into(),
            result: ReviewResult::Diff(ReviewDiff {
                snapshot: "s1".into(),
                file_id: "f1".into(),
                content_kind: ReviewContentKind::Text,
                truncated: false,
                hunks: vec![ReviewHunk {
                    old_start: 0,
                    old_count: 0,
                    new_start: 1,
                    new_count: 1,
                    heading: String::new(),
                    lines: vec![ReviewLine::Addition("hello".into())],
                }],
            }),
        };
        assert!(response.validate_for(&request).is_ok());
        response.request_id = "other".into();
        assert!(response.validate_for(&request).is_err());
        response.request_id = "r1".into();
        if let ReviewResult::Diff(diff) = &mut response.result {
            diff.hunks[0].new_count = 2;
        }
        assert!(response.validate_for(&request).is_err());
    }

    #[test]
    fn paths_and_terminal_controls_fail_closed() {
        for path in [
            "/etc/passwd",
            "../secret",
            "src/../../secret",
            "bad\u{1b}[31m",
            "",
        ] {
            assert!(!relative_path(path), "{path:?}");
        }
        assert!(relative_path("src/a file.rs"));
        assert!(line_text("\tindent"));
        assert!(!line_text("escape\u{1b}[31m"));
        assert!(!line_text("two\nlines"));
    }

    #[test]
    fn limits_reject_zero_and_excessive_requests() {
        assert!(ReviewLimits::default().is_valid());
        assert!(!ReviewLimits {
            files: 0,
            ..ReviewLimits::default()
        }
        .is_valid());
        assert!(!ReviewLimits {
            hunks: 257,
            ..ReviewLimits::default()
        }
        .is_valid());
        assert!(!ReviewLimits {
            lines: 10_001,
            ..ReviewLimits::default()
        }
        .is_valid());
    }

    #[test]
    fn requests_reject_controls_and_unbounded_operation_tokens() {
        let mut request = ReviewRequest {
            api_version: REVIEW_API_VERSION,
            request_id: "request-1".into(),
            worktree_id: WorktreeId(1),
            worktree_path: "/daemon/resolved".into(),
            comparison: ReviewComparison::WorkingAgainstHead,
            limits: ReviewLimits::default(),
            operation: ReviewOperation::FileDiff {
                snapshot: "snapshot-1".into(),
                file_id: "file-1".into(),
            },
        };
        assert!(request.validate().is_ok());
        request.request_id = "bad\nrequest".into();
        assert!(request.validate().is_err());
        request.request_id = "request-1".into();
        request.operation = ReviewOperation::FileDiff {
            snapshot: "s".repeat(513),
            file_id: "file-1".into(),
        };
        assert!(request.validate().is_err());
    }
}