opencrabs 0.5.11

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Recommended: the 40MB prebuilt binary for macOS, Linux and Windows: https://github.com/opencrabs/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
//! Tests for `agent::service::compaction_prompts`.
//!
//! Regression context (2026-05-29): a Telegram user (leshchenko1979)
//! forwarded a post-compaction one-liner — the model dropping into
//! Russian мат when expressing frustration after recovering context —
//! to friends, calling it a "Skynet" moment. Earlier we had silenced
//! all post-compaction narration via commit fb325fb5; this test
//! locks in that the fun variant is the DEFAULT (so those delight
//! moments keep happening) and that the silent variant is reachable
//! when `[agent] silent_compaction = true`.
//!
//! These tests are deliberately string-sentinel based, not full
//! equality. The exact wording will drift; what must not drift is:
//!
//! - Default (silent=false) keeps the explicit "POST-COMPACTION
//!   PROTOCOL" header and at least one invitation to acknowledge.
//! - Silent (silent=true) keeps the explicit "Silently" directive
//!   and excludes the fun acknowledgement invitation.
//! - The auto-approve tail is appended in both modes when
//!   auto_approve=false.

use crate::brain::agent::service::compaction_prompts::{
    CompactionKind, PlanRecovery, append_skill_stamp, build_continuation,
};

const APPROVAL_TAIL: &str = "Tool approval is REQUIRED";
const SESSION_RECOVERY: &str = "SESSION RECOVERY";

#[test]
fn fun_regular_keeps_post_compaction_protocol_header() {
    let body = build_continuation(CompactionKind::Regular, false, true, PlanRecovery::Active);
    assert!(
        body.contains("POST-COMPACTION PROTOCOL"),
        "fun regular must include the numbered protocol header so the \
         model picks up the task; got: {body}"
    );
    assert!(
        !body.contains("Silently continue"),
        "fun variant must not carry the silent directive: {body}"
    );
}

#[test]
fn silent_regular_uses_silent_directive() {
    let body = build_continuation(CompactionKind::Regular, true, true, PlanRecovery::Active);
    assert!(
        body.contains("Silently continue"),
        "silent regular must explicitly tell the model to continue silently: {body}"
    );
    assert!(
        !body.contains("POST-COMPACTION PROTOCOL"),
        "silent variant must drop the verbose protocol header: {body}"
    );
}

#[test]
fn fun_emergency_invites_fun_cheeky_remark() {
    // The emergency path is where the most-shared one-liners came
    // from. The fun variant MUST carry an explicit invitation, not
    // just allow it implicitly — otherwise the model defaults to
    // silent recovery and the personality moment never fires.
    let body = build_continuation(CompactionKind::Emergency, false, true, PlanRecovery::Active);
    assert!(
        body.contains("fun/cheeky ROAST"),
        "fun emergency must explicitly invite a fun/cheeky roast: {body}"
    );
}

#[test]
fn silent_emergency_suppresses_acknowledgement() {
    let body = build_continuation(CompactionKind::Emergency, true, true, PlanRecovery::Active);
    assert!(
        body.contains("Silently resume"),
        "silent emergency must direct silent resumption: {body}"
    );
    assert!(
        !body.contains("fun/cheeky remark"),
        "silent variant must not invite a fun remark: {body}"
    );
}

#[test]
fn fun_post_tool_keeps_cursing_allowed_explicit() {
    // The post-tool prompt historically carried "cursing allowed" as
    // an explicit license. That's the line that produces the kind
    // of in-character output users have called out. If we ever
    // trim it we break the documented feature.
    let body = build_continuation(CompactionKind::PostTool, false, true, PlanRecovery::Active);
    assert!(
        body.contains("cursing allowed"),
        "fun post-tool must keep the explicit cursing-allowed license: {body}"
    );
    assert!(body.contains("IMMEDIATE TASK"));
}

#[test]
fn silent_post_tool_drops_cursing_invitation() {
    let body = build_continuation(CompactionKind::PostTool, true, true, PlanRecovery::Active);
    assert!(!body.contains("cursing allowed"));
    assert!(body.contains("Silently continue"));
}

#[test]
fn mid_loop_variants_diverge_on_silent_flag() {
    let fun = build_continuation(CompactionKind::MidLoop, false, true, PlanRecovery::Active);
    let silent = build_continuation(CompactionKind::MidLoop, true, true, PlanRecovery::Active);
    assert_ne!(fun, silent, "the two modes must produce different prompts");
    assert!(fun.contains("POST-COMPACTION PROTOCOL"));
    assert!(silent.contains("Silently continue"));
}

#[test]
fn approval_tail_appended_when_auto_approve_disabled_in_both_modes() {
    for kind in [
        CompactionKind::Regular,
        CompactionKind::MidLoop,
        CompactionKind::Emergency,
        CompactionKind::PostTool,
    ] {
        let fun = build_continuation(kind, false, false, PlanRecovery::Active);
        let silent = build_continuation(kind, true, false, PlanRecovery::Active);
        assert!(
            fun.contains(APPROVAL_TAIL),
            "fun {kind:?} must append the approval reminder when auto_approve=false: {fun}"
        );
        assert!(
            silent.contains(APPROVAL_TAIL),
            "silent {kind:?} must append the approval reminder when auto_approve=false: {silent}"
        );
    }
}

#[test]
fn approval_tail_omitted_when_auto_approve_enabled() {
    for kind in [
        CompactionKind::Regular,
        CompactionKind::MidLoop,
        CompactionKind::Emergency,
        CompactionKind::PostTool,
    ] {
        let fun = build_continuation(kind, false, true, PlanRecovery::Active);
        let silent = build_continuation(kind, true, true, PlanRecovery::Active);
        assert!(
            !fun.contains(APPROVAL_TAIL),
            "fun {kind:?} must NOT carry the approval tail when auto_approve=true: {fun}"
        );
        assert!(
            !silent.contains(APPROVAL_TAIL),
            "silent {kind:?} must NOT carry the approval tail when auto_approve=true: {silent}"
        );
    }
}

#[test]
fn default_agent_config_is_fun_mode() {
    // The `silent_compaction` flag is what selects between modes;
    // verify the config default keeps fun mode active so a fresh
    // install gets the personality moments out of the box.
    let cfg = crate::config::AgentConfig::default();
    assert!(
        !cfg.silent_compaction,
        "AgentConfig::default() must keep silent_compaction=false so fun \
         post-compaction narration is the out-of-the-box behaviour"
    );
}

#[test]
fn session_recovery_hint_present_in_all_variants() {
    // After compaction the agent must check for an active plan and,
    // if coding, load CODE.md. This hint is appended to ALL variants
    // (fun + silent, all 5 kinds) so it never gets lost.
    for kind in [
        CompactionKind::Regular,
        CompactionKind::MidLoop,
        CompactionKind::Emergency,
        CompactionKind::PostTool,
        CompactionKind::Manual,
    ] {
        for silent in [false, true] {
            let body = build_continuation(kind, silent, true, PlanRecovery::Active);
            assert!(
                body.contains(SESSION_RECOVERY),
                "{kind:?} silent={silent} must include the SESSION RECOVERY hint: {body}"
            );
            assert!(
                body.contains("plan") && body.contains("start"),
                "{kind:?} silent={silent} must tell the agent to call plan with start: {body}"
            );
            assert!(
                body.contains("CODE.md"),
                "{kind:?} silent={silent} must mention CODE.md for coding sessions: {body}"
            );
        }
    }
}

#[test]
fn manual_compaction_is_brief() {
    // Manual /compact uses a short sentence, not the full POST-COMPACTION
    // PROTOCOL with numbered steps. The user explicitly triggered it.
    for silent in [false, true] {
        let body = build_continuation(CompactionKind::Manual, silent, true, PlanRecovery::Active);
        assert!(
            !body.contains("POST-COMPACTION PROTOCOL"),
            "Manual compaction must NOT use the full protocol: {body}"
        );
        assert!(
            !body.contains("session_search"),
            "Manual compaction must NOT instruct session_search (brief only): {body}"
        );
        assert!(
            body.contains("IMMEDIATE TASK"),
            "Manual compaction must still reference the IMMEDIATE TASK: {body}"
        );
    }
}

// ── #125: advisory skill-inventory stamp ────────────────────────────────────
//
// The stamp must: (1) carry the sorted skill list with the advisory
// "consider, reload only those" framing, (2) be absent entirely when no
// skills were active (zero marginal tokens), (3) ride EVERY compaction
// kind in both fun and silent variants. String sentinels, same policy
// as the rest of this file: exact wording may drift, these invariants
// must not.

const STAMP_HEADER: &str = "SKILLS LOADED PRE-COMPACTION:";
const STAMP_ADVISORY: &str = "reload only those that are";

#[test]
fn skill_stamp_lists_active_skills_with_advisory_framing() {
    let body = build_continuation(CompactionKind::Regular, false, true, PlanRecovery::Active);
    let stamped = append_skill_stamp(body, &["opencrabs-dev".to_string(), "grafana".to_string()]);
    assert!(
        stamped.contains(STAMP_HEADER),
        "stamp must carry the inventory header: {stamped}"
    );
    assert!(
        stamped.contains("opencrabs-dev") && stamped.contains("grafana"),
        "stamp must list every active skill: {stamped}"
    );
    assert!(
        stamped.contains(STAMP_ADVISORY),
        "stamp must be advisory (consider, reload selectively) not prescriptive: {stamped}"
    );
}

#[test]
fn skill_stamp_is_silent_when_no_skills_active() {
    for kind in [
        CompactionKind::Regular,
        CompactionKind::MidLoop,
        CompactionKind::Emergency,
        CompactionKind::PostTool,
        CompactionKind::Manual,
    ] {
        for silent in [false, true] {
            let body = build_continuation(kind, silent, true, PlanRecovery::NoPlan);
            let stamped = append_skill_stamp(body, &[]);
            assert!(
                !stamped.contains(STAMP_HEADER),
                "{kind:?} silent={silent}: no skills → no stamp, zero marginal tokens: {stamped}"
            );
        }
    }
}

#[test]
fn skill_stamp_rides_all_kinds_and_variants() {
    let skills = vec!["opencrabs-dev".to_string()];
    for kind in [
        CompactionKind::Regular,
        CompactionKind::MidLoop,
        CompactionKind::Emergency,
        CompactionKind::PostTool,
        CompactionKind::Manual,
    ] {
        for silent in [false, true] {
            let body = build_continuation(kind, silent, true, PlanRecovery::Active);
            let stamped = append_skill_stamp(body, &skills);
            assert!(
                stamped.contains(STAMP_HEADER) && stamped.contains(STAMP_ADVISORY),
                "{kind:?} silent={silent}: stamp must ride every kind/variant: {stamped}"
            );
        }
    }
}

#[test]
fn skill_stamp_sorts_names_for_determinism() {
    let body = build_continuation(CompactionKind::Regular, false, true, PlanRecovery::NoPlan);
    let stamped = append_skill_stamp(
        body,
        &["zeta".to_string(), "alpha".to_string(), "mid".to_string()],
    );
    let header_pos = stamped.find(STAMP_HEADER).expect("stamp present");
    let list = &stamped[header_pos..];
    let alpha = list.find("alpha").expect("alpha present");
    let mid = list.find("mid").expect("mid present");
    let zeta = list.find("zeta").expect("zeta present");
    assert!(
        alpha < mid && mid < zeta,
        "stamp list must be sorted for deterministic rendering: {list}"
    );
}

/// The roast is a per-kind obligation inside `fun_body`, and that is
/// exactly how it vanished. `d3c239c1e` (2026-05-30, "restore fun
/// post-compaction narration") shipped the fun variant with the
/// acknowledgement line in only two of five arms, Emergency and
/// PostTool, leaving `Regular` and `MidLoop` with none. Those two are
/// the dominant auto paths (11 of 18 continuations across the logs
/// surviving on 2026-09-22), so the delight feature the restore commit
/// was meant to bring back stayed dark for 115 days.
///
/// `compaction_single_continuation_path_test.rs` guards the CALL SITE
/// (the loop reaches the builder exactly once). Nothing guarded the
/// ARMS, so a missing arm was invisible to every test in this file.
/// This pins the property the sentinel tests cannot.
#[test]
fn every_fun_arm_except_manual_invites_a_roast_by_name() {
    for kind in [
        CompactionKind::Regular,
        CompactionKind::MidLoop,
        CompactionKind::Emergency,
        CompactionKind::PostTool,
    ] {
        let body = build_continuation(kind, false, true, PlanRecovery::Active);
        assert!(
            body.to_lowercase().contains("roast"),
            "fun {kind:?} must name the ROAST explicitly. The model follows \
             the loudest directive and drops a parenthetical aside, which is \
             how a 'fun/cheeky remark' lost to an 'IMMEDIATELY continue ... \
             NOT optional' sibling for 115 days: {body}"
        );
    }
}

/// Manual is the deliberate exception: the user typed `/compact`, so they
/// already saw the confirmation and acknowledging it back to them is
/// noise. Pinned as an exception rather than an oversight, so the next
/// contributor does not "fix" it back in.
#[test]
fn manual_still_forbids_the_acknowledgement() {
    let body = build_continuation(CompactionKind::Manual, false, true, PlanRecovery::Active);
    assert!(
        body.contains("Do NOT acknowledge the compaction"),
        "manual must stay acknowledgement-free, the user asked for it: {body}"
    );
    assert!(
        !body.to_lowercase().contains("roast"),
        "manual must not invite a roast: {body}"
    );
}

/// The silent variant must stay free of the roast invitation, or
/// `silent_compaction = true` leaks personality into corporate
/// deployments, which is the only reason the flag exists.
#[test]
fn silent_arms_never_invite_a_roast() {
    for kind in [
        CompactionKind::Regular,
        CompactionKind::MidLoop,
        CompactionKind::Emergency,
        CompactionKind::PostTool,
        CompactionKind::Manual,
    ] {
        let body = build_continuation(kind, true, true, PlanRecovery::Active);
        assert!(
            !body.to_lowercase().contains("roast"),
            "silent {kind:?} must not invite a roast: {body}"
        );
    }
}

// ---------------------------------------------------------------------
// Issue #499 — a continuation document must never re-bless a COMPLETED
// obligation as the critical immediate task.
//
// Section 0 now carries an explicit status on its first line, and every
// continuation body defers to it. The population is 5 kinds x 2 modes =
// 10 bodies: the original design enumerated only four, and the missing
// kind (Emergency) carried an unconditional resume directive in its
// silent arm.
// ---------------------------------------------------------------------

const ALL_KINDS: [CompactionKind; 5] = [
    CompactionKind::Regular,
    CompactionKind::MidLoop,
    CompactionKind::Emergency,
    CompactionKind::PostTool,
    CompactionKind::Manual,
];

/// Positive arm: all 10 bodies must carry the status rule, so no body can
/// tell the model to continue an obligation unconditionally.
#[test]
fn every_body_carries_the_obligation_status_rule() {
    for kind in ALL_KINDS {
        for silent in [false, true] {
            let body = build_continuation(kind, silent, true, PlanRecovery::Active);
            assert!(
                body.contains("Obligation status"),
                "{kind:?} silent={silent} must defer to section 0's obligation \
                 status — otherwise it re-blesses a completed task: {body}"
            );
        }
    }
}

/// The rule must name all three statuses, must forbid redoing DONE work,
/// and must default an ABSENT status line to UNKNOWN — never to OPEN. An
/// absent status read as OPEN is the original defect with a new coat of
/// paint.
#[test]
fn status_rule_names_all_three_tokens_and_defaults_absent_to_unknown() {
    let body = build_continuation(CompactionKind::Regular, false, true, PlanRecovery::Active);
    for token in ["OPEN", "DONE", "UNKNOWN"] {
        assert!(
            body.contains(token),
            "the status rule must name the {token} status: {body}"
        );
    }
    assert!(
        body.contains("absent status line means UNKNOWN"),
        "an absent status line must default to UNKNOWN, never OPEN: {body}"
    );
    assert!(
        body.contains("do NOT redo"),
        "the DONE branch must forbid redoing the completed work: {body}"
    );
}

/// Negative arm, in code: no body may carry an UNGATED directive to
/// continue the obligation. Every directive must sit on a line that also
/// carries the status gate.
#[test]
fn no_body_carries_an_ungated_continue_directive() {
    const DIRECTIVES: [&str; 5] = [
        "IMMEDIATELY continue the task described",
        "Silently continue the IMMEDIATE TASK",
        "Silently resume from the IMMEDIATE TASK",
        "Silently resume the IMMEDIATE TASK",
        "Resume the IMMEDIATE TASK",
    ];
    let mut checked = 0usize;
    for kind in ALL_KINDS {
        for silent in [false, true] {
            let body = build_continuation(kind, silent, true, PlanRecovery::Active);
            for line in body.lines() {
                if DIRECTIVES.iter().any(|d| line.contains(d)) {
                    checked += 1;
                    assert!(
                        line.contains("Obligation status"),
                        "{kind:?} silent={silent} continues the obligation without \
                         an OPEN gate: {line}"
                    );
                }
            }
        }
    }
    assert!(
        checked >= 9,
        "the negative arm inspected only {checked} directive lines — the guard is \
         measuring the wrong surface"
    );
}