opencrabs 0.3.80

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Recommended: the 40MB prebuilt binary for macOS, Linux and Windows: https://github.com/adolfousier/opencrabs/releases
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
//! Governance tests for the seed brain-file templates.
//!
//! These lock in the structure established while de-duplicating the templates:
//! one owner per rule, ownership-map headers, no personal/user file names, the
//! real service/daemon setup documented, and the dead VOICE.md / BOOTSTRAP.md
//! files gone (and unseeded). Without these guards the duplication and stale
//! references that an audit found in live workspaces would creep back.

use std::fs;
use std::path::Path;

const TEMPLATE_DIR: &str = "src/docs/reference/templates";

/// Rule-bearing templates that must carry an ownership-map header.
/// HEARTBEAT.md is excluded — it's a near-empty config file, not a rules file.
const OWNED_TEMPLATES: &[&str] = &[
    "SOUL.md",
    "USER.md",
    "AGENTS.md",
    "CODE.md",
    "TOOLS.md",
    "SECURITY.md",
    "MEMORY.md",
    "BOOT.md",
];

fn read_template(name: &str) -> String {
    let path = Path::new(TEMPLATE_DIR).join(name);
    fs::read_to_string(&path).unwrap_or_else(|_| panic!("template {name} must exist at {path:?}"))
}

// ── ownership-map headers ────────────────────────────────────────────────────

#[test]
fn every_rule_template_has_ownership_header() {
    for name in OWNED_TEMPLATES {
        let content = read_template(name);
        assert!(
            content.contains("> **Owns:**"),
            "{name} must open with a `> **Owns:** …` ownership-map header so RSI \
             and future edits know what this file is the single source of truth for"
        );
    }
}

// ── hard rules live in AGENTS (always-loaded), SOUL is personality ───────────

#[test]
fn enforced_gates_live_in_agents_not_soul() {
    let soul = read_template("SOUL.md");
    let agents = read_template("AGENTS.md");
    // AGENTS owns the enforced permission/commit gates (it's always-loaded).
    assert!(
        agents.contains("create PRs only") && agents.contains("NEVER DO WITHOUT EXPLICIT APPROVAL"),
        "AGENTS.md must own the enforced hard rules / permission gates"
    );
    // SOUL no longer carries the full gate list — it just points to AGENTS.
    assert!(
        !soul.contains("create PRs only"),
        "SOUL.md must NOT duplicate the commit/push gate — it lives in AGENTS.md"
    );
    assert!(
        soul.contains("AGENTS.md"),
        "SOUL.md must point to AGENTS.md for the hard rules"
    );
}

#[test]
fn preamble_and_rsi_route_hard_rules_to_agents() {
    use crate::brain::prompt_builder::BRAIN_PREAMBLE;
    assert!(
        BRAIN_PREAMBLE.contains("AGENTS.md")
            && BRAIN_PREAMBLE.contains("hard rules")
            && BRAIN_PREAMBLE.contains("Always-loaded"),
        "preamble must route hard rules to the always-loaded AGENTS.md"
    );
    const RSI_SRC: &str = include_str!("../brain/rsi.rs");
    assert!(
        RSI_SRC.contains("ALWAYS-LOADED") && RSI_SRC.contains("hard rules"),
        "RSI taxonomy must route learned hard rules to the always-loaded AGENTS.md"
    );
}

// ── commands & skills discovery ──────────────────────────────────────────────

#[test]
fn agents_documents_command_and_skill_discovery() {
    let agents = read_template("AGENTS.md");
    assert!(
        agents.contains("Commands & Skills") && agents.contains("slash_command"),
        "AGENTS.md must document how to discover/run user commands & skills"
    );
    assert!(
        agents.contains("Available Commands & Skills"),
        "AGENTS.md must reference the always-injected live commands/skills index"
    );
}

#[test]
fn rsi_notes_new_commands_skills_are_auto_discoverable() {
    const RSI_SRC: &str = include_str!("../brain/rsi.rs");
    assert!(
        RSI_SRC.contains("Available Commands & Skills")
            || RSI_SRC.contains("discoverable automatically"),
        "RSI must note that a newly-applied command/skill is auto-discoverable via \
         the injected index, so it isn't re-documented in a brain file"
    );
}

// ── single source of truth (no rule duplicated across files) ─────────────────

#[test]
fn rust_first_policy_lives_only_in_code_md() {
    // The full policy text is the distinctive phrase. It must appear in CODE.md
    // and NOWHERE else — AGENTS.md and BOOT.md only carry a pointer.
    const FULL_TEXT: &str = "always prioritize Rust-based crates";
    assert!(
        read_template("CODE.md").contains(FULL_TEXT),
        "CODE.md must own the full Rust-First Policy"
    );
    for other in ["AGENTS.md", "BOOT.md"] {
        let c = read_template(other);
        assert!(
            !c.contains(FULL_TEXT),
            "{other} must NOT duplicate the Rust-First Policy text — point to CODE.md"
        );
        assert!(
            c.contains("Rust-First") && c.contains("CODE.md"),
            "{other} must keep a pointer to CODE.md for the Rust-First Policy"
        );
    }
}

#[test]
fn upgrade_procedure_lives_in_boot_md() {
    // The build-from-source upgrade block belongs to BOOT.md; AGENTS.md points.
    assert!(
        read_template("BOOT.md").contains("cargo build --release"),
        "BOOT.md must own the upgrade procedure"
    );
    let agents = read_template("AGENTS.md");
    assert!(
        agents.contains("Upgrading") && agents.contains("BOOT.md"),
        "AGENTS.md must point to BOOT.md for upgrading, not duplicate the steps"
    );
}

/// Memory-save triggers live in the ALWAYS-LOADED file, with BOOT.md pointing
/// at it (#1003).
///
/// This assertion used to run the other way, and the other way was wrong.
/// Ownership had been assigned by topic (BOOT.md owns runtime
/// self-maintenance) while loading was assigned by cost (BOOT.md is
/// contextual), and nobody reconciled the two. A save trigger fires
/// MID-SESSION, on an arbitrary turn, when the user corrects you. BOOT.md is
/// not in context at that moment unless something loaded it.
///
/// Automatic recall cannot cover the gap either: a correction is short and
/// conversational, which is exactly the message shape BM25 recall stays silent
/// on by design (#996). Same reasoning that keeps enforced gates in AGENTS.md
/// instead of behind retrieval.
///
/// The symptom was a live AGENTS.md that restated the whole trigger list inline
/// while also carrying a pointer calling BOOT.md the single source of truth.
/// That "duplication" was load-bearing: it was the only copy ever in context.
#[test]
fn memory_save_triggers_live_in_the_always_loaded_file() {
    let agents = read_template("AGENTS.md");
    let boot = read_template("BOOT.md");

    assert!(
        agents.contains("What triggers a save to"),
        "AGENTS.md must own the memory-save triggers: it is the always-loaded \
         file, and a trigger that fires mid-session has to be in context then"
    );
    assert!(
        boot.contains("AGENTS.md") && boot.contains("Auto-Save Important Memories"),
        "BOOT.md must point at AGENTS.md for the triggers rather than restate them"
    );
    assert!(
        !boot.contains("What triggers a save to"),
        "BOOT.md must not keep a second copy of the trigger list"
    );
    assert!(
        !boot.contains("The single home for \"when to save memory\""),
        "BOOT.md's ownership header must stop claiming the memory triggers"
    );
}

// ── no personal / user file names anywhere in the templates ──────────────────

#[test]
fn templates_reference_no_personal_user_files() {
    for name in OWNED_TEMPLATES.iter().chain(["HEARTBEAT.md"].iter()) {
        let content = read_template(name);
        for banned in ["VOICE.md", "AGENTVERSE.md"] {
            assert!(
                !content.contains(banned),
                "{name} must not reference the user file {banned} — those are \
                 arbitrary user-created files, not canonical brain files"
            );
        }
    }
}

// ── the real service / daemon setup is documented ────────────────────────────

#[test]
fn boot_md_documents_service_and_daemon_setup() {
    let boot = read_template("BOOT.md");
    assert!(
        boot.contains("opencrabs service install"),
        "BOOT.md must document `opencrabs service install` (systemd/launchd)"
    );
    assert!(
        boot.contains("opencrabs daemon"),
        "BOOT.md must mention the `opencrabs daemon` process the unit runs"
    );
}

// ── deleted templates are gone and unseeded ──────────────────────────────────

#[test]
fn deleted_templates_do_not_exist() {
    for gone in ["VOICE.md", "BOOTSTRAP.md"] {
        assert!(
            !Path::new(TEMPLATE_DIR).join(gone).exists(),
            "{gone} was removed as redundant — it must not come back"
        );
    }
}

#[test]
fn boot_md_is_seeded_and_dead_files_are_not() {
    use crate::tui::onboarding::TEMPLATE_FILES;
    assert!(
        TEMPLATE_FILES.iter().any(|(n, _)| *n == "BOOT.md"),
        "BOOT.md must be seeded so its on-demand load (and AGENTS.md's pointers \
         to it) resolve for fresh users"
    );
    for gone in ["BOOTSTRAP.md", "VOICE.md"] {
        assert!(
            !TEMPLATE_FILES.iter().any(|(n, _)| *n == gone),
            "{gone} must not be seeded"
        );
    }
}

// ── the ownership model reaches the system prompt and RSI ────────────────────

#[test]
fn preamble_carries_brain_file_ownership_map() {
    use crate::brain::prompt_builder::BRAIN_PREAMBLE;
    assert!(
        BRAIN_PREAMBLE.contains("BRAIN FILE OWNERSHIP"),
        "the system preamble must teach the agent the brain-file ownership map"
    );
    for f in [
        "SOUL.md",
        "MEMORY.md",
        "CODE.md",
        "TOOLS.md",
        "SECURITY.md",
        "BOOT.md",
    ] {
        assert!(
            BRAIN_PREAMBLE.contains(f),
            "preamble ownership map must name {f}"
        );
    }
    assert!(
        BRAIN_PREAMBLE.contains("never") && BRAIN_PREAMBLE.contains("duplicat"),
        "preamble must state that a rule is never duplicated across files"
    );
}

#[test]
fn rsi_taxonomy_routes_to_all_core_brain_files() {
    // Source-level check: the RSI improvement prompt must route to each core
    // brain file — including BOOT.md, which took over memory-save triggers,
    // upgrade, and service setup — and forbid cross-file duplication.
    const RSI_SRC: &str = include_str!("../brain/rsi.rs");
    for f in [
        "SOUL.md",
        "USER.md",
        "MEMORY.md",
        "AGENTS.md",
        "CODE.md",
        "TOOLS.md",
        "SECURITY.md",
        "BOOT.md",
    ] {
        assert!(
            RSI_SRC.contains(f),
            "RSI Target File Taxonomy must route improvements to {f}"
        );
    }
    assert!(
        RSI_SRC.contains("One kind of content per file"),
        "RSI must state the one-kind-per-file ownership principle"
    );
}

#[test]
fn memory_index_excludes_dead_files() {
    use crate::memory::BRAIN_FILES;
    for gone in ["BOOTSTRAP.md", "VOICE.md"] {
        assert!(
            !BRAIN_FILES.contains(&gone),
            "memory index must not list the removed {gone}"
        );
    }
}

// ── dead brain files are not promised by user-facing docs (#991) ─────────────

/// Files that were removed from the brain-file set. See the Removed Files
/// ledger in `src/docs/reference/BRAIN_CONSTITUTION.md`.
const DEAD_BRAIN_FILES: &[&str] = &["IDENTITY.md", "VOICE.md", "BOOTSTRAP.md"];

/// User-facing docs must not promise a brain file that no longer exists.
///
/// `deleted_templates_do_not_exist` above stops the template coming back, but
/// nothing stopped the docs describing it. The setup guide told users their
/// workspace is created with a starter `IDENTITY.md` and README diagrammed it
/// in the workspace tree, so a workspace built by following the docs carried a
/// file no code path reads, and anything written into it was silently inert.
///
/// `BRAIN_CONSTITUTION.md` is deliberately excluded: it is the ledger that
/// records these deletions, so naming them is its job. CHANGELOG is excluded
/// for the same reason, it is history.
#[test]
fn user_facing_docs_do_not_reference_dead_brain_files() {
    let mut checked = 0;
    for path in ["README.md", "src/docs/start", TEMPLATE_DIR] {
        for file in markdown_files(Path::new(path)) {
            if file.ends_with("BRAIN_CONSTITUTION.md") {
                continue;
            }
            let content = fs::read_to_string(&file).expect("doc reads");
            for dead in DEAD_BRAIN_FILES {
                assert!(
                    !content.contains(dead),
                    "{} references {dead}, which was removed from the brain-file set. \
                     Docs that promise a dead brain file produce workspaces carrying a \
                     file nothing reads.",
                    file.display()
                );
            }
            checked += 1;
        }
    }
    assert!(checked > 0, "scanned no docs, the paths must be wrong");
}

/// Every markdown file at or under `path` (a file yields just itself).
fn markdown_files(path: &Path) -> Vec<std::path::PathBuf> {
    if path.is_file() {
        return vec![path.to_path_buf()];
    }
    let mut out = Vec::new();
    let Ok(entries) = fs::read_dir(path) else {
        return out;
    };
    for entry in entries.flatten() {
        let p = entry.path();
        if p.is_dir() {
            out.extend(markdown_files(&p));
        } else if p.extension().is_some_and(|e| e == "md") {
            out.push(p);
        }
    }
    out
}

// ── operating rules reach users as syncable sections (#992) ──────────────────

/// Operating rules that must exist in SOUL.md as their own `## ` sections.
const SOUL_RULE_SECTIONS: &[&str] = &[
    "## Your Role",
    "## Operating Rules",
    "## Epistemic Honesty",
    "## Never Assume, Verify",
    "## Fix, Don't Narrate",
];

/// Each operating rule is a TOP-LEVEL `## ` section, not prose folded into an
/// existing one.
///
/// This is a delivery constraint, not a style preference. `rsi_sync` merges
/// brain files by section: `extract_new_sections` appends only `## ` sections
/// the local copy lacks and never rewrites existing prose. Fold these rules
/// into `## Core Truths` and sync sees a section that already exists locally,
/// skips it, and every existing install receives nothing. Only brand-new
/// profiles would ever get them.
#[test]
fn soul_operating_rules_are_their_own_sections() {
    let soul = read_template("SOUL.md");
    for heading in SOUL_RULE_SECTIONS {
        assert!(
            soul.lines().any(|l| l.trim_end() == *heading),
            "SOUL.md must carry `{heading}` as a top-level section on its own line. \
             Folded into another section, rsi_sync will never deliver it to an \
             existing install."
        );
    }
}

/// SOUL owns the posture, AGENTS owns the mechanism, neither restates the other.
#[test]
fn epistemic_posture_and_protocol_do_not_duplicate() {
    let soul = read_template("SOUL.md");
    let agents = read_template("AGENTS.md");

    // The distinctive posture line belongs to SOUL alone.
    const SNAPSHOT: &str = "snapshots go stale";
    assert!(
        soul.contains(SNAPSHOT),
        "SOUL.md must own the Never Assume, Verify posture"
    );
    assert!(
        !agents.contains(SNAPSHOT),
        "AGENTS.md must not duplicate the posture text, it owns the protocol"
    );

    // The tracking mechanism belongs to AGENTS alone.
    assert!(
        agents.contains("## Epistemic Protocol") && agents.contains("Decay:"),
        "AGENTS.md must own the epistemic tracking protocol"
    );
    assert!(
        !soul.contains("Decay:"),
        "SOUL.md must not duplicate the protocol, it points at the posture only"
    );
}

/// SOUL says what the agent is FOR, not only how it sounds.
#[test]
fn soul_defines_a_role_not_only_a_voice() {
    let soul = read_template("SOUL.md");
    assert!(
        soul.contains("protect the production environment")
            && soul.contains("plan before executing"),
        "SOUL.md must state the operating posture (protect production, plan first), \
         not just the voice. A template that defines a tone with no job ships \
         personality without judgement."
    );
}

/// The duplicate check must exist in BOTH places, and say the same thing.
///
/// The rule has to fire at the moment of the write. `write_opencrabs_file`'s
/// description is the only text guaranteed to be in context then, so it owns
/// the operative instruction; AGENTS.md is always-loaded and carries the same
/// rule for the agent reasoning about memory before it reaches for the tool.
///
/// Guarded because they drifted before: the live workspace had the rule (added
/// by hand) while the shipped template had none, so every new install wrote
/// duplicates by default (#1017).
#[test]
fn the_duplicate_check_is_stated_in_both_the_tool_and_the_template() {
    let agents = fs::read_to_string(Path::new(TEMPLATE_DIR).join("AGENTS.md"))
        .expect("AGENTS.md template must exist");
    let tool = fs::read_to_string("src/brain/tools/write_opencrabs_file.rs")
        .expect("write_opencrabs_file.rs must exist");

    for (name, text) in [("AGENTS.md template", &agents), ("tool description", &tool)] {
        assert!(
            text.contains("memory_search"),
            "{name} must name the tool that searches, since the agent cannot \
             tell by looking"
        );
        assert!(
            text.to_lowercase().contains("replace"),
            "{name} must offer the update-in-place outcome, or the only choices \
             are append and drop"
        );
        assert!(
            text.contains("recall"),
            "{name} must say per-turn recall does not substitute for the search: \
             recall is keyed to the user's message, not to the line being written"
        );
    }
}