opencrabs 0.3.47

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Install with: cargo install opencrabs
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
//! Hashline Edit Tool
//!
//! Hash-anchored file editing: models reference lines by 2-char content hashes
//! instead of reproducing text. Eliminates stale-line errors and reduces token
//! usage, especially for weaker models.

use super::hash::hash_all_lines;
use super::types::{HashRef, HashlineEditInput, HashlineEditOp, ResolvedEdit, ResolvedOp};
use crate::brain::tools::brain_file_safety;
use crate::brain::tools::edit::build_edit_diff;
use crate::brain::tools::error::{Result, ToolError, validate_file_path};
use crate::brain::tools::r#trait::{Tool, ToolCapability, ToolExecutionContext, ToolResult};
use async_trait::async_trait;
use serde_json::Value;
use tokio::fs;

/// Hashline edit tool
pub struct HashlineEditTool;

#[async_trait]
impl Tool for HashlineEditTool {
    fn name(&self) -> &str {
        "hashline_edit"
    }

    fn description(&self) -> &str {
        "Edit a file using hash-anchored line references. Each line is identified by a 2-char \
         content hash (from read_file with hashline=true). Reference lines by hash alone (e.g. 'VK' \
         or '#VK'); legacy 'LINE#HASH' format is accepted but the line number is ignored. Stale \
         hashes are rejected before any changes are applied. Supports batch edits (multiple \
         operations in one call)."
    }

    fn input_schema(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "path": {
                    "type": "string",
                    "description": "Path to the file to edit"
                },
                "edits": {
                    "type": "array",
                    "description": "Array of edit operations to apply atomically",
                    "items": {
                        "type": "object",
                        "properties": {
                            "op": {
                                "type": "string",
                                "enum": ["replace", "append", "prepend"],
                                "description": "Edit operation type"
                            },
                            "pos": {
                                "type": "string",
                                "description": "Anchor: 2-char content hash from hashline read (e.g. 'VK' or '#VK'). Legacy 'LINE#HASH' accepted; line number ignored. Required for replace, optional for append/prepend."
                            },
                            "end": {
                                "type": "string",
                                "description": "End hash for range replace (inclusive, same format as pos). Omit to replace a single line."
                            },
                            "lines": {
                                "type": "string",
                                "description": "Replacement or insertion text. Use \\n for multi-line content."
                            }
                        },
                        "required": ["op", "lines"]
                    },
                    "minItems": 1
                }
            },
            "required": ["path", "edits"]
        })
    }

    fn capabilities(&self) -> Vec<ToolCapability> {
        vec![
            ToolCapability::ReadFiles,
            ToolCapability::WriteFiles,
            ToolCapability::SystemModification,
        ]
    }

    fn requires_approval(&self) -> bool {
        true
    }

    fn validate_input(&self, input: &Value) -> Result<()> {
        let _: HashlineEditInput = serde_json::from_value(input.clone())
            .map_err(|e| ToolError::InvalidInput(format!("Invalid input: {}", e)))?;
        Ok(())
    }

    async fn execute(&self, input: Value, context: &ToolExecutionContext) -> Result<ToolResult> {
        let input: HashlineEditInput = serde_json::from_value(input)?;

        // Validate path
        let path = match validate_file_path(&input.path, &context.working_dir()) {
            Ok(p) => p,
            Err(msg) => return Ok(ToolResult::error(msg)),
        };

        // Brain-file guardrail
        if brain_file_safety::is_protected_path(&path) {
            return Ok(ToolResult::error(format!(
                "Refusing to edit protected brain file '{}' with hashline_edit. \
                 Use the `write_opencrabs_file` tool instead.",
                path.display()
            )));
        }

        // Read file
        let content = fs::read_to_string(&path).await.map_err(ToolError::Io)?;
        let original_lines: Vec<&str> = content.lines().collect();
        let total_lines = original_lines.len();

        // Compute hashes for all lines
        let line_hashes = hash_all_lines(&content);

        // Build a reverse lookup: hash → list of line numbers (for collision detection and lookup)
        let mut hash_to_lines: std::collections::HashMap<&str, Vec<usize>> =
            std::collections::HashMap::new();
        for (num, hash) in &line_hashes {
            hash_to_lines.entry(hash.as_str()).or_default().push(*num);
        }

        // Phase 1: Validate all hash references and resolve to line numbers
        let mut resolved = Vec::with_capacity(input.edits.len());
        for (i, edit) in input.edits.iter().enumerate() {
            match resolve_edit(edit, i, &hash_to_lines, total_lines)? {
                Ok(resolved_edit) => resolved.push(resolved_edit),
                Err(error_msg) => return Ok(ToolResult::error(error_msg)),
            }
        }

        // Phase 2: Sort bottom-up (highest line number first) to preserve line numbers
        resolved.sort_by(|a, b| {
            let line_a = edit_sort_line(a);
            let line_b = edit_sort_line(b);
            line_b.cmp(&line_a) // descending
        });

        // Phase 3: Detect overlapping ranges
        if let Some(overlap_err) = detect_overlaps(&resolved) {
            return Ok(ToolResult::error(overlap_err));
        }

        // Phase 4: Apply edits (bottom-up, so line numbers stay stable)
        let mut result_lines: Vec<String> = original_lines.iter().map(|s| s.to_string()).collect();

        for edit in &resolved {
            apply_edit(&mut result_lines, edit);
        }

        let new_content = result_lines.join("\n");

        // Preserve trailing newline if original had one
        let new_content = if content.ends_with('\n') && !new_content.ends_with('\n') {
            format!("{}\n", new_content)
        } else {
            new_content
        };

        // Write
        fs::write(&path, &new_content)
            .await
            .map_err(ToolError::Io)?;

        // Track file in session (fire and forget, path-only)
        if let Some(ref sc) = context.service_context {
            let fs = crate::services::FileService::new(sc.clone());
            let _ = fs
                .get_or_create_file(context.session_id, path.clone(), None)
                .await;
        }

        let lines_before = original_lines.len();
        let lines_after = new_content.lines().count();
        let diff = build_edit_diff(&content, &new_content);

        let mut output = format!(
            "Successfully edited {} (hashline). Lines: {} → {}\n",
            path.display(),
            lines_before,
            lines_after
        );
        output.push_str(&diff);

        Ok(ToolResult::success(output))
    }
}

/// Resolve a single edit operation, validating all hash references.
///
/// Returns Ok(Ok(resolved)) on success, Ok(Err(message)) on validation failure
/// (so we can return a user-friendly error without propagating ToolError).
fn resolve_edit(
    edit: &HashlineEditOp,
    index: usize,
    hash_to_lines: &std::collections::HashMap<&str, Vec<usize>>,
    total_lines: usize,
) -> Result<std::result::Result<ResolvedEdit, String>> {
    match edit {
        HashlineEditOp::Replace { pos, end, lines } => {
            let pos_ref = match HashRef::parse(pos) {
                Ok(r) => r,
                Err(e) => return Ok(Err(format!("Edit #{}: {}", index + 1, e))),
            };

            // Validate pos hash and get line number
            let start_line = match validate_hash(&pos_ref, hash_to_lines, total_lines) {
                Ok(line) => line,
                Err(e) => return Ok(Err(format!("Edit #{}: {}", index + 1, e))),
            };

            let end_line = if let Some(end_str) = end {
                let end_ref = match HashRef::parse(end_str) {
                    Ok(r) => r,
                    Err(e) => return Ok(Err(format!("Edit #{}: {}", index + 1, e))),
                };
                let end_line = match validate_hash(&end_ref, hash_to_lines, total_lines) {
                    Ok(line) => line,
                    Err(e) => return Ok(Err(format!("Edit #{}: {}", index + 1, e))),
                };
                if end_line < start_line {
                    return Ok(Err(format!(
                        "Edit #{}: end line ({}) must be >= start line ({})",
                        index + 1,
                        end_line,
                        start_line
                    )));
                }
                end_line
            } else {
                start_line
            };

            let new_lines = strip_hashline_prefixes(lines);

            Ok(Ok(ResolvedEdit {
                op: ResolvedOp::Replace {
                    start_line,
                    end_line,
                    new_lines,
                },
                index,
            }))
        }

        HashlineEditOp::Append { pos, lines } => {
            let after_line = if let Some(pos_str) = pos {
                let pos_ref = match HashRef::parse(pos_str) {
                    Ok(r) => r,
                    Err(e) => return Ok(Err(format!("Edit #{}: {}", index + 1, e))),
                };
                match validate_hash(&pos_ref, hash_to_lines, total_lines) {
                    Ok(line) => line,
                    Err(e) => return Ok(Err(format!("Edit #{}: {}", index + 1, e))),
                }
            } else {
                total_lines // append at EOF
            };

            let new_lines = strip_hashline_prefixes(lines);

            Ok(Ok(ResolvedEdit {
                op: ResolvedOp::Append {
                    after_line,
                    new_lines,
                },
                index,
            }))
        }

        HashlineEditOp::Prepend { pos, lines } => {
            let before_line = if let Some(pos_str) = pos {
                let pos_ref = match HashRef::parse(pos_str) {
                    Ok(r) => r,
                    Err(e) => return Ok(Err(format!("Edit #{}: {}", index + 1, e))),
                };
                match validate_hash(&pos_ref, hash_to_lines, total_lines) {
                    Ok(line) => line,
                    Err(e) => return Ok(Err(format!("Edit #{}: {}", index + 1, e))),
                }
            } else {
                1 // prepend at BOF
            };

            let new_lines = strip_hashline_prefixes(lines);

            Ok(Ok(ResolvedEdit {
                op: ResolvedOp::Prepend {
                    before_line,
                    new_lines,
                },
                index,
            }))
        }
    }
}

/// Validate that a HashRef's hash exists in the file and is unique.
///
/// Returns the line number where the hash was found.
///
/// Checks for:
/// 1. Hash not found in file
/// 2. Hash collision (same hash on multiple lines) → escalate to edit_file
fn validate_hash(
    href: &HashRef,
    hash_to_lines: &std::collections::HashMap<&str, Vec<usize>>,
    _total_lines: usize,
) -> std::result::Result<usize, String> {
    // Look up which line(s) have this hash
    match hash_to_lines.get(href.hash.as_str()) {
        None => Err(format!(
            "Hash #{} not found in file. The file may have changed since your last read. \
             Re-read with hashline=true to get updated references.",
            href.hash
        )),
        Some(lines_with_hash) if lines_with_hash.len() > 1 => {
            let line_list: Vec<String> = lines_with_hash.iter().map(|l| l.to_string()).collect();
            Err(format!(
                "Hash collision: #{} appears on {} lines ({}). \
                 This hash is ambiguous and cannot safely identify a single line. \
                 Use the `edit_file` tool with search/replace instead of hashline_edit.",
                href.hash,
                lines_with_hash.len(),
                line_list.join(", ")
            ))
        }
        Some(lines_with_hash) => {
            // Unique hash, return the line number
            Ok(lines_with_hash[0])
        }
    }
}

/// Get the sort key for an edit (used for bottom-up ordering).
fn edit_sort_line(edit: &ResolvedEdit) -> usize {
    match &edit.op {
        ResolvedOp::Replace { start_line, .. } => *start_line,
        ResolvedOp::Append { after_line, .. } => *after_line,
        ResolvedOp::Prepend { before_line, .. } => *before_line,
    }
}

/// Detect overlapping ranges in a set of resolved edits.
/// Returns Some(error_message) if overlaps found, None if clean.
fn detect_overlaps(edits: &[ResolvedEdit]) -> Option<String> {
    // Build a list of (start, end) ranges for each edit
    let mut ranges: Vec<(usize, usize, usize)> = edits
        .iter()
        .map(|e| {
            let (start, end) = match &e.op {
                ResolvedOp::Replace {
                    start_line,
                    end_line,
                    ..
                } => (*start_line, *end_line),
                ResolvedOp::Append { after_line, .. } => (*after_line + 1, *after_line + 1),
                ResolvedOp::Prepend { before_line, .. } => (*before_line, *before_line),
            };
            (start, end, e.index + 1)
        })
        .collect();

    // Sort by start line ascending
    ranges.sort_by_key(|r| r.0);

    // Check adjacent ranges for overlap
    for i in 0..ranges.len().saturating_sub(1) {
        let (_, end_a, idx_a) = ranges[i];
        let (start_b, _, idx_b) = ranges[i + 1];
        if end_a >= start_b {
            return Some(format!(
                "Overlapping edits: edit #{} (ending at line {}) overlaps with edit #{} (starting at line {}). \
                 Adjust the ranges so they don't overlap.",
                idx_a, end_a, idx_b, start_b
            ));
        }
    }

    None
}

/// Apply a single resolved edit to the line buffer.
/// Edits are applied bottom-up (highest line first) so earlier line numbers stay stable.
fn apply_edit(lines: &mut Vec<String>, edit: &ResolvedEdit) {
    match &edit.op {
        ResolvedOp::Replace {
            start_line,
            end_line,
            new_lines,
        } => {
            let start_idx = start_line - 1; // convert to 0-indexed
            let end_idx = *end_line; // exclusive end for drain

            // Clamp to valid range
            let start_idx = start_idx.min(lines.len());
            let end_idx = end_idx.min(lines.len());

            // Remove old lines
            lines.drain(start_idx..end_idx);

            // Insert new lines
            for (i, new_line) in new_lines.iter().enumerate() {
                lines.insert(start_idx + i, new_line.clone());
            }
        }

        ResolvedOp::Append {
            after_line,
            new_lines,
        } => {
            let insert_idx = (*after_line).min(lines.len());
            for (i, new_line) in new_lines.iter().enumerate() {
                lines.insert(insert_idx + i, new_line.clone());
            }
        }

        ResolvedOp::Prepend {
            before_line,
            new_lines,
        } => {
            let insert_idx = (before_line - 1).min(lines.len());
            for (i, new_line) in new_lines.iter().enumerate() {
                lines.insert(insert_idx + i, new_line.clone());
            }
        }
    }
}

/// Strip hashline prefixes from content lines if the model accidentally included them.
///
/// Detects lines starting with `DIGITS#XX|` pattern and strips the prefix.
fn strip_hashline_prefixes(text: &str) -> Vec<String> {
    text.lines()
        .map(|line| {
            // Check for pattern: digits + '#' + 2 chars + '|'
            if let Some(hash_pos) = line.find('#') {
                let before = &line[..hash_pos];
                let after = &line[hash_pos + 1..];

                // before must be all digits
                if !before.is_empty()
                    && before.chars().all(|c| c.is_ascii_digit())
                    && after.len() >= 3
                    && after.as_bytes()[0].is_ascii_uppercase()
                    && after.as_bytes()[1].is_ascii_uppercase()
                    && after.as_bytes()[2] == b'|'
                {
                    return after[3..].to_string();
                }
            }
            line.to_string()
        })
        .collect()
}