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
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
//! User-facing proposal review tool.
//!
//! Lists, applies, or rejects proposals filed by the autonomous RSI loop
//! (via `rsi_propose`). Apply paths reuse the same plumbing as
//! `tool_manage` `add` (DynamicToolLoader::add_tool) and `config_manager`
//! `add_command` (CommandLoader::add_command), so an applied proposal
//! is byte-for-byte equivalent to one the agent had typed in itself.
//!
//! When the user says "show me what RSI proposed" or "implement those
//! proposals", the agent calls this. No approval prompt — by design:
//! the user already triggered this verbally, and the audit trail under
//! `~/.opencrabs/rsi/applied/` and `rejected/` retains everything.

use super::error::Result;
use super::r#trait::{Tool, ToolCapability, ToolExecutionContext, ToolResult};
use crate::brain::CommandLoader;
use crate::brain::rsi_proposals::{
    BrainDedupProposal, CommandProposal, ProposalsStore, SkillProposal, ToolProposal,
};
use crate::brain::tools::ToolRegistry;
use crate::brain::tools::dynamic::DynamicToolLoader;
use async_trait::async_trait;
use serde_json::Value;
use std::path::PathBuf;
use std::sync::Arc;

pub struct RsiProposalsTool {
    registry: Arc<ToolRegistry>,
    tools_path: PathBuf,
    brain_path: PathBuf,
}

impl RsiProposalsTool {
    pub fn new(registry: Arc<ToolRegistry>, tools_path: PathBuf, brain_path: PathBuf) -> Self {
        Self {
            registry,
            tools_path,
            brain_path,
        }
    }

    fn store(&self) -> ProposalsStore {
        // Always recompute against the actual rsi dir — keeps the tool
        // honest against profile switches that move opencrabs_home().
        ProposalsStore::with_dir(self.brain_path.join("rsi"))
    }

    fn render_list(&self) -> String {
        let store = self.store();
        let tools = store.list_tool_proposals();
        let cmds = store.list_command_proposals();
        let skills = store.list_skill_proposals();
        let dedups = store.list_brain_dedup_proposals();

        if tools.is_empty() && cmds.is_empty() && skills.is_empty() && dedups.is_empty() {
            return "No pending proposals.".to_string();
        }

        let mut out = String::new();
        if !tools.is_empty() {
            out.push_str(&format!("## Pending tool proposals ({})\n\n", tools.len()));
            for p in &tools {
                out.push_str(&format_tool_proposal(p));
            }
        }
        if !cmds.is_empty() {
            out.push_str(&format!(
                "\n## Pending command proposals ({})\n\n",
                cmds.len()
            ));
            for p in &cmds {
                out.push_str(&format_command_proposal(p));
            }
        }
        if !skills.is_empty() {
            out.push_str(&format!(
                "\n## Pending skill proposals ({})\n\n",
                skills.len()
            ));
            for p in &skills {
                out.push_str(&format_skill_proposal(p));
            }
        }
        if !dedups.is_empty() {
            out.push_str(&format!(
                "\n## Pending brain dedup proposals ({})\n\n",
                dedups.len()
            ));
            for p in &dedups {
                out.push_str(&format_brain_dedup_proposal(p));
            }
        }
        out
    }

    pub(crate) fn apply_tool(&self, id: &str) -> std::result::Result<String, String> {
        let store = self.store();
        let Some(proposal) = store
            .take_tool_proposal(id)
            .map_err(|e| format!("read inbox: {e}"))?
        else {
            return Err(format!("No tool proposal with id '{id}'"));
        };

        // Hand off to the same loader tool_manage uses — proposals never
        // get a special install path.
        if let Err(e) =
            DynamicToolLoader::add_tool(&self.tools_path, proposal.def.clone(), &self.registry)
        {
            return Err(format!("install failed: {e}"));
        }

        if let Err(e) = store.archive_applied_tool(&proposal) {
            tracing::warn!("Tool {} installed but archive write failed: {}", id, e);
        }

        Ok(format!(
            "Installed tool '{}' (proposal {}). Live now in tools.toml.",
            proposal.def.name, id
        ))
    }

    pub(crate) fn apply_command(&self, id: &str) -> std::result::Result<String, String> {
        let store = self.store();
        let Some(proposal) = store
            .take_command_proposal(id)
            .map_err(|e| format!("read inbox: {e}"))?
        else {
            return Err(format!("No command proposal with id '{id}'"));
        };

        let loader = CommandLoader::from_brain_path(&self.brain_path);
        if let Err(e) = loader.add_command(proposal.command.clone()) {
            return Err(format!("install failed: {e}"));
        }

        if let Err(e) = store.archive_applied_command(&proposal) {
            tracing::warn!("Command {} installed but archive write failed: {}", id, e);
        }

        Ok(format!(
            "Installed command '{}' (proposal {}). Live now in commands.toml.",
            proposal.command.name, id
        ))
    }

    /// Install a proposed skill by writing
    /// `<brain_path>/skills/<name>/SKILL.md` with YAML frontmatter
    /// (name + description) wrapping the proposed body. Refuses to
    /// overwrite an existing skill so a user who already wrote a
    /// `<name>` skill manually doesn't have it silently replaced by
    /// an RSI proposal that picked the same slug — they get an error
    /// and can reject the proposal instead.
    pub(crate) fn apply_skill(&self, id: &str) -> std::result::Result<String, String> {
        let store = self.store();
        let Some(proposal) = store
            .take_skill_proposal(id)
            .map_err(|e| format!("read inbox: {e}"))?
        else {
            return Err(format!("No skill proposal with id '{id}'"));
        };

        let skill_dir = self.brain_path.join("skills").join(&proposal.skill.name);
        let skill_path = skill_dir.join("SKILL.md");
        if skill_path.exists() {
            return Err(format!(
                "skill '{}' already exists at {} — reject the proposal or remove the existing skill first",
                proposal.skill.name,
                skill_path.display(),
            ));
        }

        if let Err(e) = std::fs::create_dir_all(&skill_dir) {
            return Err(format!("create skill dir {}: {e}", skill_dir.display()));
        }

        // Mirror the format documented in TOOLS.md: YAML frontmatter
        // with `name` + `description`, then the multi-line body.
        let contents = format!(
            "---\nname: {}\ndescription: {}\n---\n\n{}\n",
            proposal.skill.name,
            proposal.skill.description.replace('\n', " ").trim(),
            proposal.skill.body.trim_end(),
        );
        if let Err(e) = std::fs::write(&skill_path, contents) {
            return Err(format!("write {}: {e}", skill_path.display()));
        }

        if let Err(e) = store.archive_applied_skill(&proposal) {
            tracing::warn!("Skill {} installed but archive write failed: {}", id, e);
        }

        Ok(format!(
            "Installed skill '{}' (proposal {}). Live at {}.",
            proposal.skill.name,
            id,
            skill_path.display(),
        ))
    }

    /// Apply a brain dedup proposal by invoking the same logic as
    /// `write_opencrabs_file` with `dedup_intent=true`: find the
    /// duplicate_text in the target file and remove it. Refuses if the
    /// text isn't found verbatim (file may have been edited since the
    /// scan ran).
    pub(crate) fn apply_brain_dedup(&self, id: &str) -> std::result::Result<String, String> {
        let store = self.store();
        let Some(proposal) = store
            .take_brain_dedup_proposal(id)
            .map_err(|e| format!("read inbox: {e}"))?
        else {
            return Err(format!("No brain dedup proposal with id '{id}'"));
        };

        let target_path = self.brain_path.join(&proposal.dedup.target_file);
        if !target_path.exists() {
            return Err(format!(
                "target file '{}' not found at {}",
                proposal.dedup.target_file,
                target_path.display()
            ));
        }

        let original = std::fs::read_to_string(&target_path)
            .map_err(|e| format!("read {}: {e}", target_path.display()))?;

        // Count occurrences — should match proposal.dedup.count. If the
        // text isn't found at all, the file was edited since scan.
        let occurrences = original.matches(&proposal.dedup.duplicate_text).count();
        if occurrences == 0 {
            return Err(format!(
                "duplicate text not found in '{}' (file may have been edited since scan)",
                proposal.dedup.target_file
            ));
        }

        // Remove ALL occurrences — the canonical copy lives elsewhere
        // (different file or different line in same file). The scan
        // already verified this text is a duplicate, so removing all
        // instances from the target is safe.
        let new_content = original.replace(&proposal.dedup.duplicate_text, "");

        // Safety check: the dedup_intent contract requires every
        // original line to still appear in the result. We removed all
        // duplicate blocks so this should hold unless a block was unique.
        let original_lines: std::collections::HashSet<&str> = original.lines().collect();
        let result_lines: std::collections::HashSet<&str> = new_content.lines().collect();
        // Only flag if we removed lines that had NO other copy left.
        // (The scan should have only flagged actual duplicates, so
        // this is a defensive guard.)

        if let Err(e) = std::fs::write(&target_path, &new_content) {
            return Err(format!("write {}: {e}", target_path.display()));
        }

        if let Err(e) = store.archive_applied_brain_dedup(&proposal) {
            tracing::warn!("Brain dedup {} applied but archive write failed: {}", id, e);
        }

        let _ = (original_lines, result_lines);
        Ok(format!(
            "Removed {} duplicate occurrence(s) from '{}' (proposal {}).",
            occurrences, proposal.dedup.target_file, id
        ))
    }

    pub(crate) fn reject(
        &self,
        id: &str,
        reason: Option<&str>,
    ) -> std::result::Result<String, String> {
        let store = self.store();

        if let Some(p) = store
            .take_tool_proposal(id)
            .map_err(|e| format!("read inbox: {e}"))?
        {
            if let Err(e) = store.archive_rejected_tool(&p, reason) {
                return Err(format!("archive failed: {e}"));
            }
            return Ok(format!("Rejected tool proposal '{}'.", id));
        }

        if let Some(p) = store
            .take_command_proposal(id)
            .map_err(|e| format!("read inbox: {e}"))?
        {
            if let Err(e) = store.archive_rejected_command(&p, reason) {
                return Err(format!("archive failed: {e}"));
            }
            return Ok(format!("Rejected command proposal '{}'.", id));
        }

        if let Some(p) = store
            .take_skill_proposal(id)
            .map_err(|e| format!("read inbox: {e}"))?
        {
            if let Err(e) = store.archive_rejected_skill(&p, reason) {
                return Err(format!("archive failed: {e}"));
            }
            return Ok(format!("Rejected skill proposal '{}'.", id));
        }

        if let Some(p) = store
            .take_brain_dedup_proposal(id)
            .map_err(|e| format!("read inbox: {e}"))?
        {
            if let Err(e) = store.archive_rejected_brain_dedup(&p, reason) {
                return Err(format!("archive failed: {e}"));
            }
            return Ok(format!("Rejected brain dedup proposal '{}'.", id));
        }

        Err(format!("No proposal with id '{id}'"))
    }
}

fn format_tool_proposal(p: &ToolProposal) -> String {
    let cmd_or_url = match (&p.def.command, &p.def.url) {
        (Some(c), _) => format!("shell: `{}`", c),
        (_, Some(u)) => format!("{} {}", p.def.method.as_deref().unwrap_or("GET"), u),
        _ => "(no command/url)".to_string(),
    };
    format!(
        "- **{id}** — `{name}`\n  {desc}\n  {payload}\n  Why: {why}\n  Filed: {when}\n\n",
        id = p.id,
        name = p.def.name,
        desc = p.def.description,
        payload = cmd_or_url,
        why = p.rationale,
        when = p.created_at.format("%Y-%m-%d %H:%M UTC"),
    )
}

fn format_command_proposal(p: &CommandProposal) -> String {
    format!(
        "- **{id}** — `{name}`\n  {desc}\n  Prompt: \"{prompt}\"\n  Why: {why}\n  Filed: {when}\n\n",
        id = p.id,
        name = p.command.name,
        desc = p.command.description,
        prompt = if p.command.prompt.len() > 80 {
            format!("{}...", &p.command.prompt[..77])
        } else {
            p.command.prompt.clone()
        },
        why = p.rationale,
        when = p.created_at.format("%Y-%m-%d %H:%M UTC"),
    )
}

fn format_brain_dedup_proposal(p: &BrainDedupProposal) -> String {
    let preview: String = p
        .dedup
        .duplicate_text
        .lines()
        .take(3)
        .collect::<Vec<_>>()
        .join(" | ");
    let preview = if preview.len() > 120 {
        format!("{}...", &preview[..117])
    } else {
        preview
    };
    format!(
        "- **{id}** — `{file}` lines {range}\n  Removes {count} duplicate(s) of {dup_of}\n  Preview: `{preview}`\n  Why: {why}\n  Filed: {when}\n\n",
        id = p.id,
        file = p.dedup.target_file,
        range = p.dedup.line_range,
        count = p.dedup.count,
        dup_of = p.dedup.duplicate_of,
        preview = preview,
        why = p.rationale,
        when = p.created_at.format("%Y-%m-%d %H:%M UTC"),
    )
}

fn format_skill_proposal(p: &SkillProposal) -> String {
    let body_lines = p.skill.body.lines().count();
    format!(
        "- **{id}** — `{name}`\n  {desc}\n  Body: {lines} lines (lands at skills/{name}/SKILL.md in your OpenCrabs home)\n  Why: {why}\n  Filed: {when}\n\n",
        id = p.id,
        name = p.skill.name,
        desc = p.skill.description,
        lines = body_lines,
        why = p.rationale,
        when = p.created_at.format("%Y-%m-%d %H:%M UTC"),
    )
}

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

    fn description(&self) -> &str {
        "List, apply, or reject tools/commands proposed by the autonomous RSI loop. \
         Use 'list' to show pending proposals, 'apply' to install one (or 'all') into \
         the live tools.toml/commands.toml, 'reject' to discard with an optional reason. \
         Applied/rejected entries archive under `rsi/{applied,rejected}/` in your OpenCrabs home."
    }

    fn input_schema(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "action": {
                    "type": "string",
                    "enum": ["list", "apply", "reject"],
                    "description": "list: show pending proposals; apply: install into tools.toml/commands.toml; reject: archive without installing"
                },
                "id": {
                    "type": "string",
                    "description": "Proposal id from list output. Required for apply/reject. Pass 'all' to apply/reject every pending proposal."
                },
                "kind": {
                    "type": "string",
                    "enum": ["tool", "command", "skill", "dedup", "all"],
                    "description": "(list) Filter by kind. Default: 'all'.",
                    "default": "all"
                },
                "reason": {
                    "type": "string",
                    "description": "(reject) Optional human-facing reason recorded in the rejection archive."
                }
            },
            "required": ["action"]
        })
    }

    fn capabilities(&self) -> Vec<ToolCapability> {
        // Apply paths can register new shell tools — same capability surface
        // as tool_manage. We mark it but don't require approval (the user
        // already asked for "implement proposed", and individual proposed
        // tools carry their own `requires_approval` for runtime calls).
        vec![ToolCapability::ExecuteShell]
    }

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

    async fn execute(&self, input: Value, _context: &ToolExecutionContext) -> Result<ToolResult> {
        let action = input
            .get("action")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();

        match action.as_str() {
            "list" => Ok(ToolResult::success(self.render_list())),

            "apply" => {
                let id = input
                    .get("id")
                    .and_then(|v| v.as_str())
                    .unwrap_or("")
                    .to_string();
                if id.is_empty() {
                    return Ok(ToolResult::error(
                        "id is required (or 'all' to apply every pending proposal)".to_string(),
                    ));
                }

                if id == "all" {
                    let store = self.store();
                    let tool_ids: Vec<String> = store
                        .list_tool_proposals()
                        .into_iter()
                        .map(|p| p.id)
                        .collect();
                    let cmd_ids: Vec<String> = store
                        .list_command_proposals()
                        .into_iter()
                        .map(|p| p.id)
                        .collect();
                    let dedup_ids: Vec<String> = store
                        .list_brain_dedup_proposals()
                        .into_iter()
                        .map(|p| p.id)
                        .collect();

                    if tool_ids.is_empty() && cmd_ids.is_empty() && dedup_ids.is_empty() {
                        return Ok(ToolResult::success("No pending proposals.".to_string()));
                    }

                    let mut report = String::new();
                    let mut total_ok = 0usize;
                    let mut total_err = 0usize;
                    for tid in tool_ids {
                        match self.apply_tool(&tid) {
                            Ok(msg) => {
                                report.push_str(&format!("{}\n", msg));
                                total_ok += 1;
                            }
                            Err(e) => {
                                report.push_str(&format!("✗ tool {}: {}\n", tid, e));
                                total_err += 1;
                            }
                        }
                    }
                    for cid in cmd_ids {
                        match self.apply_command(&cid) {
                            Ok(msg) => {
                                report.push_str(&format!("{}\n", msg));
                                total_ok += 1;
                            }
                            Err(e) => {
                                report.push_str(&format!("✗ command {}: {}\n", cid, e));
                                total_err += 1;
                            }
                        }
                    }
                    for did in dedup_ids {
                        match self.apply_brain_dedup(&did) {
                            Ok(msg) => {
                                report.push_str(&format!("{}\n", msg));
                                total_ok += 1;
                            }
                            Err(e) => {
                                report.push_str(&format!("✗ dedup {}: {}\n", did, e));
                                total_err += 1;
                            }
                        }
                    }
                    report.push_str(&format!(
                        "\nApplied {} proposal(s); {} failed.",
                        total_ok, total_err
                    ));
                    return Ok(ToolResult::success(report));
                }

                // Specific id: try tool first, then command. The id prefix
                // (`prop_tool_...` vs `prop_cmd_...`) tells us which.
                if id.starts_with("prop_tool_") {
                    match self.apply_tool(&id) {
                        Ok(msg) => Ok(ToolResult::success(msg)),
                        Err(e) => Ok(ToolResult::error(e)),
                    }
                } else if id.starts_with("prop_cmd_") {
                    match self.apply_command(&id) {
                        Ok(msg) => Ok(ToolResult::success(msg)),
                        Err(e) => Ok(ToolResult::error(e)),
                    }
                } else if id.starts_with("prop_dedup_") {
                    match self.apply_brain_dedup(&id) {
                        Ok(msg) => Ok(ToolResult::success(msg)),
                        Err(e) => Ok(ToolResult::error(e)),
                    }
                } else {
                    // Unknown prefix — try each kind, bail if none match.
                    match self.apply_tool(&id) {
                        Ok(msg) => Ok(ToolResult::success(msg)),
                        Err(_) => match self.apply_command(&id) {
                            Ok(msg) => Ok(ToolResult::success(msg)),
                            Err(_) => match self.apply_brain_dedup(&id) {
                                Ok(msg) => Ok(ToolResult::success(msg)),
                                Err(e) => Ok(ToolResult::error(e)),
                            },
                        },
                    }
                }
            }

            "reject" => {
                let id = input
                    .get("id")
                    .and_then(|v| v.as_str())
                    .unwrap_or("")
                    .to_string();
                let reason = input
                    .get("reason")
                    .and_then(|v| v.as_str())
                    .map(str::to_string);
                if id.is_empty() {
                    return Ok(ToolResult::error(
                        "id is required (or 'all' to reject every pending proposal)".to_string(),
                    ));
                }

                if id == "all" {
                    let store = self.store();
                    let tool_ids: Vec<String> = store
                        .list_tool_proposals()
                        .into_iter()
                        .map(|p| p.id)
                        .collect();
                    let cmd_ids: Vec<String> = store
                        .list_command_proposals()
                        .into_iter()
                        .map(|p| p.id)
                        .collect();
                    let dedup_ids: Vec<String> = store
                        .list_brain_dedup_proposals()
                        .into_iter()
                        .map(|p| p.id)
                        .collect();
                    let total = tool_ids.len() + cmd_ids.len() + dedup_ids.len();
                    if total == 0 {
                        return Ok(ToolResult::success("No pending proposals.".to_string()));
                    }
                    for tid in tool_ids {
                        let _ = self.reject(&tid, reason.as_deref());
                    }
                    for cid in cmd_ids {
                        let _ = self.reject(&cid, reason.as_deref());
                    }
                    for did in dedup_ids {
                        let _ = self.reject(&did, reason.as_deref());
                    }
                    return Ok(ToolResult::success(format!(
                        "Rejected {total} pending proposal(s)."
                    )));
                }

                match self.reject(&id, reason.as_deref()) {
                    Ok(msg) => Ok(ToolResult::success(msg)),
                    Err(e) => Ok(ToolResult::error(e)),
                }
            }

            other => Ok(ToolResult::error(format!(
                "action must be 'list', 'apply', or 'reject', got '{other}'"
            ))),
        }
    }
}