opencrabs 0.3.58

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Install with: cargo install opencrabs
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
//! RSI Proposed Tools / Commands inbox.
//!
//! The autonomous RSI loop cannot directly install new tools or slash
//! commands — that path goes through `tool_manage` / `config_manager`,
//! which require user approval and are not in RSI's restricted tool
//! whitelist by design (a hallucinated `rm -rf` shell tool is a much
//! bigger blast radius than a hallucinated paragraph in `SOUL.md`).
//!
//! This module is the workaround: RSI writes *proposals* into TOML
//! inboxes under `~/.opencrabs/rsi/`. The user-facing agent reads them
//! on request ("show me proposed tools", "implement the proposed
//! command") and applies them via the same plumbing as `tool_manage`
//! `add` and `config_manager` `add_command`. Applied/rejected entries
//! are archived per-day so the trail is auditable forever.
//!
//! ## Layout
//!
//! ```text
//! ~/.opencrabs/rsi/
//! ├── proposed_tools.toml      # pending tool proposals
//! ├── proposed_commands.toml   # pending command proposals
//! ├── applied/
//! │   ├── 2026-05-01-tools.toml
//! │   └── 2026-05-01-commands.toml
//! └── rejected/
//!     ├── 2026-05-01-tools.toml
//!     └── 2026-05-01-commands.toml
//! ```

use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::{Path, PathBuf};

use crate::brain::commands::UserCommand;
use crate::brain::tools::dynamic::tool::DynamicToolDef;

const PROPOSED_TOOLS_FILE: &str = "proposed_tools.toml";
const PROPOSED_COMMANDS_FILE: &str = "proposed_commands.toml";
const PROPOSED_SKILLS_FILE: &str = "proposed_skills.toml";
const PROPOSED_BRAIN_DEDUP_FILE: &str = "proposed_brain_dedup.toml";
const APPLIED_DIR: &str = "applied";
const REJECTED_DIR: &str = "rejected";

/// A pending tool proposal authored by the autonomous RSI loop.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolProposal {
    /// Stable id (e.g. `prop_tool_2026-05-01_a1b2c3`). Used by `apply` / `reject`.
    pub id: String,
    /// UTC timestamp the proposal was filed.
    pub created_at: DateTime<Utc>,
    /// Who proposed it. `"rsi-autonomous"` for the background loop;
    /// could be `"user-suggested"` etc. in future.
    pub proposer: String,
    /// Free-text justification — what evidence in the feedback ledger
    /// drove this proposal? Shown to the user before they apply.
    pub rationale: String,
    /// The actual tool definition. Persisted verbatim to `tools.toml`
    /// on apply, with no further transformation.
    pub def: DynamicToolDef,
}

/// A pending command proposal authored by the autonomous RSI loop.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommandProposal {
    pub id: String,
    pub created_at: DateTime<Utc>,
    pub proposer: String,
    pub rationale: String,
    /// Persisted verbatim to `commands.toml` on apply.
    pub command: UserCommand,
}

/// A pending skill proposal authored by the autonomous RSI loop.
///
/// Skills are SKILL.md files that capture a multi-step workflow the
/// agent should follow when invoked via `/<name>` or auto-selected by
/// description. They're cheaper to author than dynamic tools (no
/// schema, no executor wiring) and a natural fit when the pattern
/// RSI observed is "this sequence of bash + http calls keeps coming
/// up — codify it as a workflow".
///
/// On apply, the user writes the body to
/// `~/.opencrabs/skills/<name>/SKILL.md` with the description in
/// YAML frontmatter.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SkillProposal {
    pub id: String,
    pub created_at: DateTime<Utc>,
    pub proposer: String,
    pub rationale: String,
    pub skill: ProposedSkill,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProposedSkill {
    /// Slug used both for the skill directory name and the `/<name>`
    /// invocation. snake_case or kebab-case, no spaces.
    pub name: String,
    /// One-line summary used by the LLM to decide when to auto-invoke
    /// the skill. Also rendered in the slash-command palette.
    pub description: String,
    /// Full markdown body of the SKILL.md file (everything below the
    /// YAML frontmatter). May span multiple steps, include shell
    /// snippets, etc.
    pub body: String,
}

#[derive(Debug, Default, Serialize, Deserialize)]
struct ToolProposalsFile {
    #[serde(default)]
    proposals: Vec<ToolProposal>,
}

#[derive(Debug, Default, Serialize, Deserialize)]
struct CommandProposalsFile {
    #[serde(default)]
    proposals: Vec<CommandProposal>,
}

#[derive(Debug, Default, Serialize, Deserialize)]
struct SkillProposalsFile {
    #[serde(default)]
    proposals: Vec<SkillProposal>,
}

/// A pending brain-file dedup proposal authored by the autonomous RSI loop.
///
/// When the periodic dedup scan finds duplicate lines or near-duplicate
/// blocks across brain files (SOUL.md, AGENTS.md, MEMORY.md, etc.), it
/// files one of these proposals per duplicate cluster. The user reviews
/// in Mission Control and applies to shrink the file via
/// `write_opencrabs_file` with `dedup_intent=true`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BrainDedupProposal {
    pub id: String,
    pub created_at: DateTime<Utc>,
    pub proposer: String,
    pub rationale: String,
    pub dedup: ProposedBrainDedup,
}

/// Payload for a brain-file dedup proposal.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProposedBrainDedup {
    /// Target brain file (e.g. "SOUL.md", "AGENTS.md").
    pub target_file: String,
    /// The exact text block to remove (must appear verbatim in the file).
    pub duplicate_text: String,
    /// Line range in the file where the duplicate appears (1-indexed, for display).
    pub line_range: String,
    /// What this duplicates — e.g. "line 42 of SOUL.md" or "AGENTS.md § Git Rules".
    pub duplicate_of: String,
    /// How many duplicate instances were found in this cluster.
    pub count: usize,
    /// Stub-risk warnings produced by the post-hoc scan: each entry names
    /// a header whose body region would be emptied by this removal,
    /// turning it into a header stub. Read-time strip (issue #164 fix 4)
    /// catches these later but the user sees them at proposal time too.
    /// Empty when no risk detected.
    #[serde(default)]
    pub warnings: Vec<String>,
}

#[derive(Debug, Default, Serialize, Deserialize)]
struct BrainDedupProposalsFile {
    #[serde(default)]
    proposals: Vec<BrainDedupProposal>,
}

/// On-disk store for proposals. Stateless — every method re-reads the
/// inbox file before mutating, so it's safe to call from concurrent
/// task contexts (the RSI background loop and the user-facing agent
/// can both touch the inbox without coordination).
#[derive(Debug, Clone)]
pub struct ProposalsStore {
    rsi_dir: PathBuf,
}

impl ProposalsStore {
    /// Create a store rooted at `<opencrabs_home>/rsi/`.
    pub fn new() -> Self {
        Self {
            rsi_dir: crate::config::opencrabs_home().join("rsi"),
        }
    }

    /// Override the rsi dir — only used by tests so they can write to
    /// a tmpdir without touching the user's actual inbox.
    pub fn with_dir(rsi_dir: PathBuf) -> Self {
        Self { rsi_dir }
    }

    fn ensure_dirs(&self) -> Result<()> {
        fs::create_dir_all(&self.rsi_dir)
            .with_context(|| format!("creating {}", self.rsi_dir.display()))?;
        fs::create_dir_all(self.rsi_dir.join(APPLIED_DIR))?;
        fs::create_dir_all(self.rsi_dir.join(REJECTED_DIR))?;
        Ok(())
    }

    fn tools_path(&self) -> PathBuf {
        self.rsi_dir.join(PROPOSED_TOOLS_FILE)
    }

    fn commands_path(&self) -> PathBuf {
        self.rsi_dir.join(PROPOSED_COMMANDS_FILE)
    }

    fn skills_path(&self) -> PathBuf {
        self.rsi_dir.join(PROPOSED_SKILLS_FILE)
    }

    fn brain_dedup_path(&self) -> PathBuf {
        self.rsi_dir.join(PROPOSED_BRAIN_DEDUP_FILE)
    }

    fn read_tools(&self) -> ToolProposalsFile {
        Self::read_file(&self.tools_path()).unwrap_or_default()
    }

    fn read_commands(&self) -> CommandProposalsFile {
        Self::read_file(&self.commands_path()).unwrap_or_default()
    }

    fn read_skills(&self) -> SkillProposalsFile {
        Self::read_file(&self.skills_path()).unwrap_or_default()
    }

    fn read_brain_dedup(&self) -> BrainDedupProposalsFile {
        Self::read_file(&self.brain_dedup_path()).unwrap_or_default()
    }

    fn read_file<T: serde::de::DeserializeOwned>(path: &Path) -> Option<T> {
        let contents = fs::read_to_string(path).ok()?;
        match toml::from_str(&contents) {
            Ok(parsed) => Some(parsed),
            Err(e) => {
                tracing::warn!("ProposalsStore: failed to parse {}: {}", path.display(), e);
                None
            }
        }
    }

    fn write_tools(&self, file: &ToolProposalsFile) -> Result<()> {
        self.ensure_dirs()?;
        let toml_str = toml::to_string_pretty(file)?;
        fs::write(self.tools_path(), toml_str)?;
        Ok(())
    }

    fn write_commands(&self, file: &CommandProposalsFile) -> Result<()> {
        self.ensure_dirs()?;
        let toml_str = toml::to_string_pretty(file)?;
        fs::write(self.commands_path(), toml_str)?;
        Ok(())
    }

    fn write_skills(&self, file: &SkillProposalsFile) -> Result<()> {
        self.ensure_dirs()?;
        let toml_str = toml::to_string_pretty(file)?;
        fs::write(self.skills_path(), toml_str)?;
        Ok(())
    }

    fn write_brain_dedup(&self, file: &BrainDedupProposalsFile) -> Result<()> {
        self.ensure_dirs()?;
        let toml_str = toml::to_string_pretty(file)?;
        fs::write(self.brain_dedup_path(), toml_str)?;
        Ok(())
    }

    /// Append a tool proposal. Generates the id, dedups against existing
    /// entries with the same `def.name` (latest wins), and persists.
    pub fn add_tool_proposal(
        &self,
        proposer: impl Into<String>,
        rationale: impl Into<String>,
        def: DynamicToolDef,
    ) -> Result<String> {
        let mut file = self.read_tools();
        let id = generate_id("tool", &def.name);

        // Dedup: a fresh proposal for the same tool name supersedes the
        // older one — keeps the inbox from filling with retries when RSI
        // observes the same gap on multiple cycles.
        file.proposals.retain(|p| p.def.name != def.name);

        file.proposals.push(ToolProposal {
            id: id.clone(),
            created_at: Utc::now(),
            proposer: proposer.into(),
            rationale: rationale.into(),
            def,
        });
        self.write_tools(&file)?;
        Ok(id)
    }

    pub fn add_command_proposal(
        &self,
        proposer: impl Into<String>,
        rationale: impl Into<String>,
        command: UserCommand,
    ) -> Result<String> {
        let mut file = self.read_commands();
        let id = generate_id("cmd", &command.name);
        file.proposals.retain(|p| p.command.name != command.name);
        file.proposals.push(CommandProposal {
            id: id.clone(),
            created_at: Utc::now(),
            proposer: proposer.into(),
            rationale: rationale.into(),
            command,
        });
        self.write_commands(&file)?;
        Ok(id)
    }

    /// Append a skill proposal. Same dedup-by-name semantics as
    /// `add_tool_proposal` — a fresh proposal for `name=github_workflow`
    /// supersedes the previous one so RSI cycles don't pile up retries.
    pub fn add_skill_proposal(
        &self,
        proposer: impl Into<String>,
        rationale: impl Into<String>,
        skill: ProposedSkill,
    ) -> Result<String> {
        let mut file = self.read_skills();
        let id = generate_id("skill", &skill.name);
        file.proposals.retain(|p| p.skill.name != skill.name);
        file.proposals.push(SkillProposal {
            id: id.clone(),
            created_at: Utc::now(),
            proposer: proposer.into(),
            rationale: rationale.into(),
            skill,
        });
        self.write_skills(&file)?;
        Ok(id)
    }

    /// Append a brain dedup proposal. Dedups by (target_file, duplicate_text)
    /// so the inbox doesn't fill with retries for the same duplicate cluster.
    pub fn add_brain_dedup_proposal(
        &self,
        proposer: impl Into<String>,
        rationale: impl Into<String>,
        dedup: ProposedBrainDedup,
    ) -> Result<String> {
        let mut file = self.read_brain_dedup();
        let id = generate_id("dedup", &dedup.target_file);
        file.proposals.retain(|p| {
            !(p.dedup.target_file == dedup.target_file
                && p.dedup.duplicate_text == dedup.duplicate_text)
        });
        file.proposals.push(BrainDedupProposal {
            id: id.clone(),
            created_at: Utc::now(),
            proposer: proposer.into(),
            rationale: rationale.into(),
            dedup,
        });
        self.write_brain_dedup(&file)?;
        Ok(id)
    }

    pub fn list_tool_proposals(&self) -> Vec<ToolProposal> {
        self.read_tools().proposals
    }

    pub fn list_command_proposals(&self) -> Vec<CommandProposal> {
        self.read_commands().proposals
    }

    pub fn list_skill_proposals(&self) -> Vec<SkillProposal> {
        self.read_skills().proposals
    }

    pub fn list_brain_dedup_proposals(&self) -> Vec<BrainDedupProposal> {
        self.read_brain_dedup().proposals
    }

    /// Total number of pending entries across all four inboxes —
    /// used by the TUI session-start banner.
    pub fn pending_count(&self) -> usize {
        self.read_tools().proposals.len()
            + self.read_commands().proposals.len()
            + self.read_skills().proposals.len()
            + self.read_brain_dedup().proposals.len()
    }

    /// Remove a tool proposal by id and return it (so the caller can
    /// install it). Returns `None` if no proposal with that id exists.
    pub fn take_tool_proposal(&self, id: &str) -> Result<Option<ToolProposal>> {
        let mut file = self.read_tools();
        let pos = file.proposals.iter().position(|p| p.id == id);
        let Some(idx) = pos else {
            return Ok(None);
        };
        let taken = file.proposals.remove(idx);
        self.write_tools(&file)?;
        Ok(Some(taken))
    }

    pub fn take_command_proposal(&self, id: &str) -> Result<Option<CommandProposal>> {
        let mut file = self.read_commands();
        let pos = file.proposals.iter().position(|p| p.id == id);
        let Some(idx) = pos else {
            return Ok(None);
        };
        let taken = file.proposals.remove(idx);
        self.write_commands(&file)?;
        Ok(Some(taken))
    }

    pub fn take_skill_proposal(&self, id: &str) -> Result<Option<SkillProposal>> {
        let mut file = self.read_skills();
        let pos = file.proposals.iter().position(|p| p.id == id);
        let Some(idx) = pos else {
            return Ok(None);
        };
        let taken = file.proposals.remove(idx);
        self.write_skills(&file)?;
        Ok(Some(taken))
    }

    pub fn take_brain_dedup_proposal(&self, id: &str) -> Result<Option<BrainDedupProposal>> {
        let mut file = self.read_brain_dedup();
        let pos = file.proposals.iter().position(|p| p.id == id);
        let Some(idx) = pos else {
            return Ok(None);
        };
        let taken = file.proposals.remove(idx);
        self.write_brain_dedup(&file)?;
        Ok(Some(taken))
    }

    /// Append an applied tool proposal to the daily archive
    /// (`applied/YYYY-MM-DD-tools.toml`).
    pub fn archive_applied_tool(&self, proposal: &ToolProposal) -> Result<()> {
        self.archive(APPLIED_DIR, "tools", proposal)
    }

    pub fn archive_applied_command(&self, proposal: &CommandProposal) -> Result<()> {
        self.archive(APPLIED_DIR, "commands", proposal)
    }

    pub fn archive_applied_skill(&self, proposal: &SkillProposal) -> Result<()> {
        self.archive(APPLIED_DIR, "skills", proposal)
    }

    pub fn archive_applied_brain_dedup(&self, proposal: &BrainDedupProposal) -> Result<()> {
        self.archive(APPLIED_DIR, "brain_dedup", proposal)
    }

    pub fn archive_rejected_brain_dedup(
        &self,
        proposal: &BrainDedupProposal,
        reason: Option<&str>,
    ) -> Result<()> {
        let mut wrapped = ArchivedProposal {
            inner: proposal.clone(),
            reason: reason.map(str::to_string),
        };
        wrapped.reason = reason.map(str::to_string);
        self.archive(REJECTED_DIR, "brain_dedup", &wrapped)
    }

    pub fn archive_rejected_skill(
        &self,
        proposal: &SkillProposal,
        reason: Option<&str>,
    ) -> Result<()> {
        let mut wrapped = ArchivedProposal {
            inner: proposal.clone(),
            reason: reason.map(str::to_string),
        };
        wrapped.reason = reason.map(str::to_string);
        self.archive(REJECTED_DIR, "skills", &wrapped)
    }

    pub fn archive_rejected_tool(
        &self,
        proposal: &ToolProposal,
        reason: Option<&str>,
    ) -> Result<()> {
        let mut wrapped = ArchivedProposal {
            inner: proposal.clone(),
            reason: reason.map(str::to_string),
        };
        // ArchivedProposal serializes inner via flatten so this is the
        // same shape as the input plus an optional `reason` field —
        // makes the archive readable by the same tooling that reads
        // the inbox.
        wrapped.reason = reason.map(str::to_string);
        self.archive(REJECTED_DIR, "tools", &wrapped)
    }

    pub fn archive_rejected_command(
        &self,
        proposal: &CommandProposal,
        reason: Option<&str>,
    ) -> Result<()> {
        let mut wrapped = ArchivedProposal {
            inner: proposal.clone(),
            reason: reason.map(str::to_string),
        };
        wrapped.reason = reason.map(str::to_string);
        self.archive(REJECTED_DIR, "commands", &wrapped)
    }

    fn archive<T: Serialize>(&self, dir_name: &str, kind: &str, proposal: &T) -> Result<()> {
        self.ensure_dirs()?;
        let date = Utc::now().format("%Y-%m-%d").to_string();
        let path = self
            .rsi_dir
            .join(dir_name)
            .join(format!("{}-{}.toml", date, kind));

        let mut existing = String::new();
        if path.exists() {
            existing = fs::read_to_string(&path).unwrap_or_default();
            if !existing.is_empty() && !existing.ends_with('\n') {
                existing.push('\n');
            }
        }

        // Each archive entry is its own `[[proposals]]` table; we just
        // append. Re-parsing on read is fine — these are append-only.
        let mut wrapper = toml::map::Map::new();
        wrapper.insert(
            "proposals".to_string(),
            toml::Value::Array(vec![toml::Value::try_from(proposal)?]),
        );
        let chunk = toml::to_string_pretty(&toml::Value::Table(wrapper))?;
        existing.push_str(&chunk);
        existing.push('\n');
        fs::write(&path, existing)?;
        Ok(())
    }
}

impl Default for ProposalsStore {
    fn default() -> Self {
        Self::new()
    }
}

/// Wrapper used when archiving a rejected proposal — adds an optional
/// `reason` field next to the original proposal payload.
#[derive(Debug, Serialize)]
struct ArchivedProposal<T: Serialize> {
    #[serde(flatten)]
    inner: T,
    #[serde(skip_serializing_if = "Option::is_none")]
    reason: Option<String>,
}

fn generate_id(kind: &str, name: &str) -> String {
    let date = Utc::now().format("%Y-%m-%d").to_string();
    let suffix = uuid::Uuid::new_v4().simple().to_string();
    let suffix_short: String = suffix.chars().take(6).collect();
    let safe_name: String = name
        .chars()
        .map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })
        .collect();
    format!("prop_{}_{}_{}_{}", kind, date, safe_name, suffix_short)
}