opencrabs 0.3.57

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
//! Regression coverage for the pure helpers in
//! `src/brain/agent/service/tool_loop.rs`.
//!
//! Linor's 2026-05-04 repo audit flagged tool_loop.rs as the highest-
//! priority bug-risk hotspot: 3,731 lines, 167 commits, zero dedicated
//! test files. This module pins the small handful of pure helpers
//! exposed for testing — `strip_ansi_output`, `extract_path_for_
//! recent_buffer`, and `is_user_correction` — so that future
//! refactors of the surrounding loop don't silently break the
//! corner-cases they handle.
//!
//! Pure-helper coverage is the entry point for the bigger refactor
//! described in the Linor report (extracting cohesive concerns out of
//! the giant `run_tool_loop_inner`); the loop logic itself is too
//! intertwined with provider streams + DB writes to unit-test in
//! isolation, but at minimum we want every PURE function the loop
//! calls to have characterization tests that prevent regressions.
//!
//! When the bigger refactor lands and more pure functions surface,
//! add their tests to this file rather than splintering into many
//! tiny ones — the project rule (memory: tests live under
//! `src/tests/`) keeps the test surface flat.

use crate::brain::agent::service::tool_loop::{
    build_tool_result_content, extract_path_for_recent_buffer, is_implausible_token_report,
    is_user_correction, strip_ansi_output,
};
use serde_json::json;
use std::path::PathBuf;

// ── strip_ansi_output ──────────────────────────────────────────────

#[test]
fn strip_ansi_output_removes_basic_color_codes() {
    // SGR 31 = red; SGR 0 = reset. Any reasonable ANSI stripper should
    // collapse this to the bare text.
    let raw = "\x1b[31merror\x1b[0m: tool failed";
    let cleaned = strip_ansi_output(raw);
    assert_eq!(cleaned, "error: tool failed");
}

#[test]
fn strip_ansi_output_handles_no_ansi() {
    let raw = "plain output, no escapes here";
    let cleaned = strip_ansi_output(raw);
    assert_eq!(cleaned, raw);
}

#[test]
fn strip_ansi_output_handles_empty_string() {
    assert_eq!(strip_ansi_output(""), "");
}

#[test]
fn strip_ansi_output_strips_cursor_movement() {
    // CSI 2J (clear screen), CSI H (cursor home) etc. should not leak
    // into tool-result text rendered to the model.
    let raw = "\x1b[2J\x1b[Hready";
    assert_eq!(strip_ansi_output(raw), "ready");
}

#[test]
fn strip_ansi_output_preserves_unicode() {
    // Non-ANSI multi-byte content must round-trip unchanged. Caught a
    // class of stripper bugs where ESC-detection code accidentally
    // consumed a UTF-8 continuation byte.
    let raw = "🦀 OpenCrabs";
    assert_eq!(strip_ansi_output(raw), "🦀 OpenCrabs");
}

#[test]
fn strip_ansi_output_strips_24bit_truecolor() {
    // SGR 38;2;R;G;B sets 24-bit foreground. Real-world tool output
    // (cargo, ripgrep, --color=always git diff) emits these.
    let raw = "\x1b[38;2;255;100;0morange\x1b[0m";
    assert_eq!(strip_ansi_output(raw), "orange");
}

// ── extract_path_for_recent_buffer ────────────────────────────────

fn cwd() -> PathBuf {
    std::env::temp_dir().join("opencrabs-tool-loop-test")
}

#[test]
fn extract_path_returns_none_for_unrelated_tools() {
    // Only tools that operate on a single file path are tracked.
    // bash / glob / web_search / generate_image have no path arg
    // worth surfacing.
    for tool in &["bash", "glob", "web_search", "generate_image", "task"] {
        let result = extract_path_for_recent_buffer(tool, &json!({}), &cwd());
        assert!(
            result.is_none(),
            "tool '{tool}' should never contribute to recent_paths"
        );
    }
}

#[test]
fn extract_path_returns_none_when_path_missing() {
    // read_file IS path-bearing, but if the agent emitted a malformed
    // call without the field, we must NOT inject a phantom row.
    let result = extract_path_for_recent_buffer("read_file", &json!({}), &cwd());
    assert!(result.is_none());
}

#[test]
fn extract_path_returns_none_for_empty_path() {
    // Whitespace-only paths are agent slop — same outcome as missing.
    for empty in &["", " ", "  \t\n  "] {
        let result = extract_path_for_recent_buffer("read_file", &json!({ "path": empty }), &cwd());
        assert!(
            result.is_none(),
            "empty path '{empty:?}' must not be recorded"
        );
    }
}

#[test]
fn extract_path_resolves_relative_against_cwd() {
    // The agent often returns relative paths. The recent-paths buffer
    // stores absolute paths so re-display next turn doesn't depend on
    // the working directory at lookup time.
    let result =
        extract_path_for_recent_buffer("read_file", &json!({ "path": "src/main.rs" }), &cwd());
    let path = result.expect("relative path must resolve");
    assert!(
        path.is_absolute(),
        "stored path should be absolute, got {path:?}"
    );
    assert!(
        path.ends_with("src/main.rs"),
        "tail must preserve the original path, got {path:?}"
    );
}

#[test]
#[cfg(unix)]
fn extract_path_passes_through_absolute_path() {
    // Already-absolute paths must round-trip unchanged.
    let result =
        extract_path_for_recent_buffer("read_file", &json!({ "path": "/etc/hosts" }), &cwd());
    assert_eq!(result, Some(PathBuf::from("/etc/hosts")));
}

#[test]
#[cfg(windows)]
fn extract_path_passes_through_absolute_path() {
    // On Windows, absolute paths require a drive letter.
    let result = extract_path_for_recent_buffer(
        "read_file",
        &json!({ "path": "C:\\Windows\\System32\\drivers\\etc\\hosts" }),
        &cwd(),
    );
    assert_eq!(
        result,
        Some(PathBuf::from("C:\\Windows\\System32\\drivers\\etc\\hosts"))
    );
}

#[test]
#[cfg(unix)]
fn extract_path_covers_all_documented_tools() {
    // Pin the documented set of path-bearing tools (read_file, edit_file,
    // write_file, ls, grep). If any are removed silently the recent-
    // paths buffer would stop tracking those operations and the next-
    // turn anchor list would go stale.
    for tool in &["read_file", "edit_file", "write_file", "ls", "grep"] {
        let result = extract_path_for_recent_buffer(tool, &json!({ "path": "/abs/file" }), &cwd());
        assert_eq!(
            result,
            Some(PathBuf::from("/abs/file")),
            "tool '{tool}' must contribute to recent_paths"
        );
    }
}

#[test]
#[cfg(windows)]
fn extract_path_covers_all_documented_tools() {
    // On Windows, absolute paths require a drive letter.
    for tool in &["read_file", "edit_file", "write_file", "ls", "grep"] {
        let result =
            extract_path_for_recent_buffer(tool, &json!({ "path": "C:\\abs\\file" }), &cwd());
        assert_eq!(
            result,
            Some(PathBuf::from("C:\\abs\\file")),
            "tool '{tool}' must contribute to recent_paths"
        );
    }
}

// ── is_user_correction ─────────────────────────────────────────────

#[test]
fn user_correction_detects_short_no_phrases() {
    // The most common correction signals — terse user pushback. These
    // should ALL trigger the retry-context injection.
    for msg in &[
        "no, that's wrong",
        "no.",
        "no!",
        "no that's not what I asked",
        "nope",
        "wrong",
        "that's not right",
        "that's wrong",
        "thats wrong",
        "not what i wanted",
        "try again",
        "redo",
        "revert",
        "undo",
        "you broke it",
        "broke it",
        "doesn't work",
        "doesnt work",
        "didn't work",
        "didnt work",
        "not working",
        "stop",
        "don't do that",
        "dont do that",
        "i said no",
        "i asked for X",
        "not correct",
        "fix it",
        "fix this",
    ] {
        assert!(
            is_user_correction(msg),
            "message {msg:?} should be detected as a correction"
        );
    }
}

#[test]
fn user_correction_ignores_long_messages() {
    // Long messages are usually new instructions, not corrections —
    // even when they contain a "no" or "stop" somewhere.
    let long = "I have a new task involving the API surface for our \
                ingestion pipeline. Please don't do the same approach as \
                last time — that hit a deadlock under concurrent load. \
                Instead, write a server in Rust that responds to GET \
                requests with JSON. Use Tokio + axum. Implement /health, \
                /metrics, /readyz, /livez, and /version endpoints. Don't \
                include any frontend assets, TLS termination, or auth \
                middleware. Stop after the server is running locally and \
                report back with the full route table, the listening \
                address, and the binary footprint. Make sure tests pass.";
    assert!(
        long.len() > 500,
        "test fixture must actually be long ({}) for the gate to be exercised",
        long.len()
    );
    assert!(
        !is_user_correction(long),
        "long instruction must NOT be classified as correction"
    );
}

#[test]
fn user_correction_ignores_extremely_short_messages() {
    // Anything under 2 chars can't carry an unambiguous correction
    // signal — must not trigger a retry on a single-keystroke message.
    for msg in &["", "?"] {
        assert!(
            !is_user_correction(msg),
            "extremely short message {msg:?} must not trigger correction path"
        );
    }
}

#[test]
fn user_correction_is_case_insensitive() {
    // Patterns are matched against lowercased input — uppercase user
    // shouting must still be detected.
    for msg in &["NO!", "WRONG", "Try Again", "FIX IT"] {
        assert!(
            is_user_correction(msg),
            "uppercase {msg:?} must be detected"
        );
    }
}

#[test]
fn user_correction_does_not_fire_on_neutral_prose() {
    // Common phrasing in normal user prompts should NOT trigger the
    // retry-context injection; otherwise routine messages get treated
    // as failures.
    for msg in &[
        "what is the capital of France?",
        "show me the diff",
        "let's add a new feature",
        "explain how this works",
        "thanks",
        "ok",
        "got it",
    ] {
        assert!(
            !is_user_correction(msg),
            "neutral message {msg:?} must NOT be classified as correction"
        );
    }
}

#[test]
fn user_correction_only_scans_first_300_chars() {
    // The is_user_correction function explicitly takes only the first
    // 300 chars before lowercasing — pin that so a "stop" word buried
    // deep in a long message doesn't false-trigger.
    let prefix = "x".repeat(300);
    let buried = format!("{prefix} stop please");
    // Must not exceed the 500-char total cap, otherwise the length gate
    // discards before we hit the prefix scan.
    assert!(buried.len() <= 500);
    assert!(
        !is_user_correction(&buried),
        "trigger word past 300-char window must not match"
    );
}

// ── build_tool_result_content ──────────────────────────────────────

#[test]
fn build_tool_result_success_returns_output() {
    let content = build_tool_result_content(true, None, "command output");
    assert_eq!(content, "command output");
}

#[test]
fn build_tool_result_failure_includes_error_and_output() {
    let content = build_tool_result_content(
        false,
        Some("Command exited with code 1".to_string()),
        "stderr: file not found",
    );
    assert!(content.contains("Command exited with code 1"));
    assert!(content.contains("-- output captured before error --"));
    assert!(content.contains("stderr: file not found"));
}

#[test]
fn build_tool_result_failure_strips_ansi_from_error() {
    let content = build_tool_result_content(
        false,
        Some("\x1b[31merror\x1b[0m: build failed".to_string()),
        "",
    );
    assert!(!content.contains("\x1b["));
    assert!(content.contains("error: build failed"));
}

#[test]
fn build_tool_result_failure_strips_ansi_from_output() {
    let content = build_tool_result_content(
        false,
        Some("error".to_string()),
        "\x1b[32msuccess\x1b[0m: compiled",
    );
    assert!(!content.contains("\x1b["));
    assert!(content.contains("success: compiled"));
}

#[test]
fn build_tool_result_failure_caps_output_at_8000_chars() {
    let large_output = "x".repeat(10000);
    let content = build_tool_result_content(false, Some("error".to_string()), &large_output);
    // The captured portion should be truncated
    assert!(content.contains("(output truncated)"));
    // But should still contain some of the output
    assert!(content.contains("xxx"));
}

#[test]
fn build_tool_result_failure_no_truncation_for_small_output() {
    let content = build_tool_result_content(false, Some("error".to_string()), "small output");
    assert!(!content.contains("(output truncated)"));
    assert!(content.contains("small output"));
}

#[test]
fn build_tool_result_failure_default_error_message() {
    let content = build_tool_result_content(false, None, "some output");
    assert!(content.contains("Tool execution failed"));
}

// Sanity guard against a provider/proxy that over-reports input_tokens.
// The zhipu "coding" endpoint added a flat ~20k to every call's reported
// input regardless of content size (a fixed additive overhead, not a
// tokenizer ratio): an 8.4k-token request came back reported as 28.8k, which
// the ctx counter trusted and inflated to 28k for a one-word "yes" reply.
// The guard keeps the local estimate when the provider's number is >2× the
// real content size (system + messages + tool schemas).

#[test]
fn over_reporting_provider_is_rejected() {
    // The exact observed shape: ~8.4k real content, provider claims 28.8k.
    assert!(
        is_implausible_token_report(8439, 0, 28775),
        "a 3.4× over-report (the zhipu '+20k' inflation) must be rejected"
    );
    // Even when the inflation is a smaller ratio on a bigger request, >2× still trips.
    assert!(is_implausible_token_report(10000, 0, 21000));
}

#[test]
fn legitimate_usage_is_trusted() {
    // Real tokenizer differences stay within ~2× — must be accepted.
    assert!(
        !is_implausible_token_report(8439, 0, 11000),
        "a 1.3× tokenizer difference is normal and must be trusted"
    );
    // Tool schemas legitimately inflate the provider's count beyond the
    // system+messages estimate — that overhead is expected, not over-reporting.
    assert!(
        !is_implausible_token_report(8000, 12000, 26000),
        "system+messages(8k) + tool schemas(12k) → ~20k expected; 26k is within 2× and must be trusted"
    );
    // Exactly 2× is the boundary — trusted (must EXCEED 2× to reject).
    assert!(!is_implausible_token_report(5000, 0, 10000));
}

#[test]
fn tiny_requests_are_never_rejected() {
    // Below the 1000-token floor the ratio is noise — never reject (a fresh
    // session's first tiny turn shouldn't trip the guard).
    assert!(!is_implausible_token_report(200, 0, 5000));
}