kaish-types 0.16.0

Pure data types for kaish — structured output, values, tool schemas
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
//! Backend data types — errors, results, and operations.
//!
//! These types define the data contract for `KernelBackend` implementations.
//! The trait itself lives in kaish-kernel (it depends on async_trait and ExecContext).

use std::collections::BTreeMap;
use std::path::PathBuf;

use serde_json::Value as JsonValue;
use thiserror::Error;

use crate::output::OutputData;
use crate::result::{json_to_value_no_envelope, value_to_json, ExecResult};
use crate::tool::ToolSchema;

/// Result type for backend operations.
pub type BackendResult<T> = Result<T, BackendError>;

/// Information about a mount point.
///
/// Returned by `KernelBackend::mounts`. Pure data so it can live in the leaf
/// types crate alongside the rest of the backend contract.
#[derive(Debug, Clone)]
pub struct MountInfo {
    /// The mount path (e.g., "/mnt/project").
    pub path: PathBuf,
    /// Whether this mount is read-only.
    pub read_only: bool,
    /// Memory-resident content bytes held by this mount, if it tracks them.
    ///
    /// `Some(n)` for memory-backed mounts (`MemoryFs`, `OverlayFs`).
    /// `None` for disk-backed mounts (`LocalFs`) — disk residency is the
    /// host's concern (`df`), not this counter. Embedder-supplied `MountInfo`
    /// values that do not track residency should use `None` so `kaish-mounts`
    /// renders `-` rather than a misleading number.
    pub resident_bytes: Option<u64>,
}

/// Backend operation errors.
#[derive(Debug, Clone, Error)]
#[non_exhaustive]
pub enum BackendError {
    #[error("not found: {0}")]
    NotFound(String),
    #[error("already exists: {0}")]
    AlreadyExists(String),
    #[error("permission denied: {0}")]
    PermissionDenied(String),
    #[error("is a directory: {0}")]
    IsDirectory(String),
    #[error("not a directory: {0}")]
    NotDirectory(String),
    #[error("read-only filesystem")]
    ReadOnly,
    #[error("conflict: {0}")]
    Conflict(ConflictError),
    #[error("tool not found: {0}")]
    ToolNotFound(String),
    #[error("io error: {0}")]
    Io(String),
    #[error("invalid operation: {0}")]
    InvalidOperation(String),
}

impl From<std::io::Error> for BackendError {
    fn from(err: std::io::Error) -> Self {
        use std::io::ErrorKind;
        match err.kind() {
            ErrorKind::NotFound => BackendError::NotFound(err.to_string()),
            ErrorKind::AlreadyExists => BackendError::AlreadyExists(err.to_string()),
            ErrorKind::PermissionDenied => BackendError::PermissionDenied(err.to_string()),
            ErrorKind::IsADirectory => BackendError::IsDirectory(err.to_string()),
            ErrorKind::NotADirectory => BackendError::NotDirectory(err.to_string()),
            ErrorKind::ReadOnlyFilesystem => BackendError::ReadOnly,
            _ => BackendError::Io(err.to_string()),
        }
    }
}

/// Error when CAS (compare-and-set) check fails during patching.
#[derive(Debug, Clone, Error)]
#[error("conflict at {location}: expected {expected:?}, found {actual:?}")]
pub struct ConflictError {
    /// Location of the conflict (e.g., "offset 42" or "line 7")
    pub location: String,
    /// Expected content at that location
    pub expected: String,
    /// Actual content found at that location
    pub actual: String,
}

/// Generic patch operation for file modifications.
///
/// Maps to POSIX operations, CRDTs, or REST APIs. All positional ops
/// support compare-and-set (CAS) via optional `expected` field.
/// If `expected` is Some, the operation fails with ConflictError if the
/// current content at that position doesn't match.
///
/// # Line Ending Normalization
///
/// Line-based operations (`InsertLine`, `DeleteLine`, `ReplaceLine`) normalize
/// line endings to Unix-style (`\n`). Files with `\r\n` (Windows) line endings
/// will be converted to `\n` after a line-based patch. This is intentional for
/// kaish's Unix-first design. Use byte-based operations (`Insert`, `Delete`,
/// `Replace`) to preserve original line endings.
#[derive(Debug, Clone)]
pub enum PatchOp {
    /// Insert content at byte offset.
    Insert { offset: usize, content: String },

    /// Delete bytes from offset to offset+len.
    /// `expected`: if Some, must match content being deleted (CAS)
    Delete {
        offset: usize,
        len: usize,
        expected: Option<String>,
    },

    /// Replace content at offset.
    /// `expected`: if Some, must match content being replaced (CAS)
    Replace {
        offset: usize,
        len: usize,
        content: String,
        expected: Option<String>,
    },

    /// Insert a line at line number (1-indexed).
    InsertLine { line: usize, content: String },

    /// Delete a line at line number (1-indexed).
    /// `expected`: if Some, must match line being deleted (CAS)
    DeleteLine { line: usize, expected: Option<String> },

    /// Replace a line at line number (1-indexed).
    /// `expected`: if Some, must match line being replaced (CAS)
    ReplaceLine {
        line: usize,
        content: String,
        expected: Option<String>,
    },

    /// Append content to end of file (no CAS needed - always safe).
    Append { content: String },
}

/// Range specification for partial file reads.
#[derive(Debug, Clone, Default)]
pub struct ReadRange {
    /// Start line (1-indexed). If set, read from this line.
    pub start_line: Option<usize>,
    /// End line (1-indexed, inclusive). If set, read until this line.
    pub end_line: Option<usize>,
    /// Byte offset to start reading from.
    pub offset: Option<u64>,
    /// Maximum number of bytes to read.
    pub limit: Option<u64>,
}

impl ReadRange {
    /// Create a range for reading specific lines.
    pub fn lines(start: usize, end: usize) -> Self {
        Self {
            start_line: Some(start),
            end_line: Some(end),
            ..Default::default()
        }
    }

    /// Create a range for reading bytes at an offset.
    pub fn bytes(offset: u64, limit: u64) -> Self {
        Self {
            offset: Some(offset),
            limit: Some(limit),
            ..Default::default()
        }
    }

    /// Apply this range to already-read file content.
    ///
    /// Byte ranges win over line ranges when both are set. A line range on
    /// non-UTF-8 content returns the content untouched (there are no lines to
    /// slice). This is the single source of truth for range slicing, shared by
    /// the `Filesystem::read_range` default and the kernel backends.
    pub fn apply(&self, content: &[u8]) -> Vec<u8> {
        // Byte-based range
        if self.offset.is_some() || self.limit.is_some() {
            let offset = self.offset.unwrap_or(0) as usize;
            let limit = self.limit.map(|l| l as usize).unwrap_or(content.len());
            let end = offset.saturating_add(limit).min(content.len());
            return content.get(offset..end).unwrap_or(&[]).to_vec();
        }

        // Line-based range
        if self.start_line.is_some() || self.end_line.is_some() {
            let content_str = match std::str::from_utf8(content) {
                Ok(s) => s,
                Err(_) => return content.to_vec(),
            };
            let lines: Vec<&str> = content_str.lines().collect();
            let start = self.start_line.unwrap_or(1).saturating_sub(1);
            let end = self.end_line.unwrap_or(lines.len()).min(lines.len());
            let selected: Vec<&str> = lines.get(start..end).unwrap_or(&[]).to_vec();
            let mut result = selected.join("\n");
            // Preserve a trailing newline only when reading to the implicit end
            // and the original content had one.
            if self.end_line.is_none() && content_str.ends_with('\n') && !result.is_empty() {
                result.push('\n');
            }
            return result.into_bytes();
        }

        content.to_vec()
    }
}

/// Write mode for file operations.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Default)]
pub enum WriteMode {
    /// Fail if file already exists.
    CreateNew,
    /// Overwrite existing file (default, like `>`).
    #[default]
    Overwrite,
    /// Fail if file does not exist.
    UpdateOnly,
    /// Explicitly truncate file before writing.
    Truncate,
}

/// Result from tool execution via backend.
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct ToolResult {
    /// Exit code (0 = success).
    pub code: i32,
    /// Standard output.
    pub stdout: String,
    /// Standard error.
    pub stderr: String,
    /// Structured data (if any).
    pub data: Option<JsonValue>,
    /// Structured output data for rendering (preserved from ExecResult).
    pub output: Option<OutputData>,
    /// True if the output limiter capped this result (propagated from
    /// `ExecResult`). See `ExecResult.did_spill` for the full semantics (disk
    /// spill vs. in-memory truncation, exit code remap to 3).
    pub did_spill: bool,
    /// The command's original exit code before spill logic overwrote it
    /// (propagated from `ExecResult`). Present only when `did_spill` is true
    /// and `code` was changed. See `ExecResult.original_code`.
    pub original_code: Option<i64>,
    /// MIME content type hint (propagated from ExecResult).
    pub content_type: Option<String>,
    /// Opaque key-value context (propagated from ExecResult).
    pub baggage: BTreeMap<String, String>,
}

impl ToolResult {
    /// Create a successful result.
    pub fn success(stdout: impl Into<String>) -> Self {
        Self {
            code: 0,
            stdout: stdout.into(),
            stderr: String::new(),
            data: None,
            output: None,
            did_spill: false,
            original_code: None,
            content_type: None,
            baggage: BTreeMap::new(),
        }
    }

    /// Create a failed result.
    pub fn failure(code: i32, stderr: impl Into<String>) -> Self {
        Self {
            code,
            stdout: String::new(),
            stderr: stderr.into(),
            data: None,
            output: None,
            did_spill: false,
            original_code: None,
            content_type: None,
            baggage: BTreeMap::new(),
        }
    }

    /// Create a result with structured data.
    pub fn with_data(stdout: impl Into<String>, data: JsonValue) -> Self {
        Self {
            code: 0,
            stdout: stdout.into(),
            stderr: String::new(),
            data: Some(data),
            output: None,
            did_spill: false,
            original_code: None,
            content_type: None,
            baggage: BTreeMap::new(),
        }
    }

    /// Check if the tool execution succeeded.
    pub fn ok(&self) -> bool {
        self.code == 0
    }

    /// Set the structured output-data payload, returning self for chaining.
    pub fn with_output(mut self, output: Option<OutputData>) -> Self {
        self.output = output;
        self
    }

    /// Set the content-type hint, returning self for chaining.
    pub fn with_content_type(mut self, ct: impl Into<String>) -> Self {
        self.content_type = Some(ct.into());
        self
    }

    /// Replace the baggage map, returning self for chaining.
    pub fn with_baggage(mut self, baggage: BTreeMap<String, String>) -> Self {
        self.baggage = baggage;
        self
    }

    /// Set the spill flag, returning self for chaining.
    pub fn with_did_spill(mut self, did_spill: bool) -> Self {
        self.did_spill = did_spill;
        self
    }

    /// Set the pre-spill original exit code, returning self for chaining.
    pub fn with_original_code(mut self, original_code: Option<i64>) -> Self {
        self.original_code = original_code;
        self
    }
}

impl From<ExecResult> for ToolResult {
    fn from(mut exec: ExecResult) -> Self {
        // Saturating cast: codes outside i32 range clamp to i32::MIN/MAX
        let code = exec.code.clamp(i32::MIN as i64, i32::MAX as i64) as i32;

        // HAZARD: `text_out()` is the infallible/lossy decoder — a binary
        // `OutputPayload::Bytes` (already produced by `cat`/`head`/`tail`/
        // `base64 -d`/`xxd -r`/`dd`/`tee`/external commands, so this is real
        // today, not merely a future risk) gets its invalid-UTF-8 bytes
        // replaced with U+FFFD here. This `From` impl is infallible by
        // signature, so it cannot fail loud the way `try_text_out()` does —
        // this is an accepted, deliberate exception, not an oversight. The
        // binary survives losslessly in the preserved `output: Option<OutputData>`
        // field below; a structured/binary-aware consumer MUST read `output`,
        // never `stdout`, to avoid the lossy decode. If a future embedder seam
        // needs a fallible conversion here, add a `TryFrom` (or a bytes-carrying
        // field) rather than changing this `From`'s behavior. See
        // `docs/binary-data.md`.
        let stdout = exec.text_out().into_owned();
        let output = exec.take_output();

        // Convert ast::Value to serde_json::Value if present
        let data = exec.data.map(|v| value_to_json(&v));

        Self {
            code,
            stdout,
            stderr: exec.err,
            data,
            output,
            did_spill: exec.did_spill,
            original_code: exec.original_code,
            content_type: exec.content_type,
            baggage: exec.baggage,
        }
    }
}

impl From<ToolResult> for ExecResult {
    /// The symmetric peer of `From<ExecResult> for ToolResult` above — every
    /// field that direction preserves, this direction must preserve too, or a
    /// backend-registered tool's structured `data`/`content_type`/`baggage`
    /// silently vanishes crossing back into the kernel (the embedder seam:
    /// `x=$(embedder_tool)` and `for r in $(embedder_tool)` need `.data` to
    /// see typed results, not just stdout text).
    ///
    /// `data` uses [`json_to_value_no_envelope`] rather than the internal
    /// round-trip conversion: a backend tool's JSON is external input, so an
    /// object shaped like the byte envelope must stay a plain record, never
    /// silently auto-decode to `Value::Bytes`.
    fn from(result: ToolResult) -> Self {
        let mut exec = ExecResult::from_output(result.code as i64, result.stdout, result.stderr);
        exec.set_output(result.output);
        exec.data = result.data.map(json_to_value_no_envelope);
        exec.did_spill = result.did_spill;
        exec.original_code = result.original_code;
        exec.content_type = result.content_type;
        exec.baggage = result.baggage;
        exec
    }
}

/// Information about an available tool.
#[derive(Debug, Clone)]
pub struct ToolInfo {
    /// Tool name.
    pub name: String,
    /// Tool description.
    pub description: String,
    /// Full tool schema.
    pub schema: ToolSchema,
}

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

    #[test]
    fn tool_result_from_exec_result_preserves_content_type_and_baggage() {
        let mut exec = ExecResult::success("hello");
        exec.content_type = Some("text/markdown".to_string());
        exec.baggage.insert("traceparent".to_string(), "00-abc-def-01".to_string());

        let tool_result = ToolResult::from(exec);
        assert_eq!(tool_result.content_type.as_deref(), Some("text/markdown"));
        assert_eq!(
            tool_result.baggage.get("traceparent").map(|s| s.as_str()),
            Some("00-abc-def-01")
        );
    }

    #[test]
    fn tool_result_constructors_default_to_empty_baggage() {
        let success = ToolResult::success("ok");
        assert!(success.baggage.is_empty());
        assert!(success.content_type.is_none());

        let failure = ToolResult::failure(1, "err");
        assert!(failure.baggage.is_empty());
        assert!(failure.content_type.is_none());
    }

    #[test]
    fn tool_result_constructors_default_did_spill_and_original_code() {
        // GH #93 item 3 baseline: the new fields must not silently drift the
        // existing constructors' defaults.
        assert!(!ToolResult::success("ok").did_spill);
        assert!(ToolResult::success("ok").original_code.is_none());
        assert!(!ToolResult::failure(1, "err").did_spill);
        assert!(!ToolResult::with_data("ok", serde_json::json!(1)).did_spill);
    }

    #[test]
    fn tool_result_from_exec_result_preserves_did_spill_and_original_code() {
        // GH #93 item 3: ExecResult -> ToolResult must not drop the spill
        // metadata — an embedder reading a ToolResult off this seam needs to
        // know the output was capped and what the code was before the remap.
        let mut exec = ExecResult::success("hello");
        exec.did_spill = true;
        exec.original_code = Some(0);

        let tool_result = ToolResult::from(exec);
        assert!(tool_result.did_spill);
        assert_eq!(tool_result.original_code, Some(0));
    }

    #[test]
    fn exec_result_from_tool_result_preserves_did_spill_and_original_code() {
        // The reverse direction: a backend tool that reports a capped result
        // (e.g. an embedder fronting its own output limiter) must have that
        // survive back into the kernel's ExecResult.
        let tool_result = ToolResult::success("hello")
            .with_did_spill(true)
            .with_original_code(Some(5));

        let exec = ExecResult::from(tool_result);
        assert!(exec.did_spill);
        assert_eq!(exec.original_code, Some(5));
    }

    #[test]
    fn tool_result_builder_setters_chain() {
        // Exercises the ergonomic construction surface added for
        // `#[non_exhaustive]`: every field not covered by success/failure/
        // with_data gets a with_* setter, matching the ExecResult style.
        let mut baggage = BTreeMap::new();
        baggage.insert("k".to_string(), "v".to_string());


        let result = ToolResult::success("hi")
            .with_output(Some(OutputData::text("hi")))
            .with_content_type("text/plain")
            .with_baggage(baggage.clone())
            .with_did_spill(true)
            .with_original_code(Some(2));

        assert!(result.output.is_some());
        assert_eq!(result.content_type.as_deref(), Some("text/plain"));
        assert_eq!(result.baggage, baggage);
        assert!(result.did_spill);
        assert_eq!(result.original_code, Some(2));
    }

}