rho-coding-agent 1.19.2

A lightweight agent harness inspired by Pi
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
use std::time::Duration;

use pretty_assertions::assert_eq;
use tokio::process::Command;

use super::*;

/// Stable system shell used by process tests.
///
/// Tests never exec a freshly written file: under parallel load Linux can
/// return `ETXTBSY` when a just-created script is still open for write
/// elsewhere. Script bodies live in argv (`/bin/sh -c <body> -- <args…>`).
#[cfg(unix)]
const STABLE_SH: &str = "/bin/sh";

/// Build a probe command that runs `body` under `/bin/sh -c` and appends the
/// auth argv after `--` so the script sees them as `$1`, `$2`, …
#[cfg(unix)]
fn sh_probe_command(body: &str, args: &[&str]) -> Command {
    let mut command = Command::new(STABLE_SH);
    command.arg("-c").arg(body).arg("--");
    command.args(args);
    command
}

#[cfg(unix)]
async fn run_sh_probe(body: &str, args: &[&str]) -> Result<BoundedOutput, ClaudeAuthError> {
    run_bounded_command_with_timeout(
        sh_probe_command(body, args),
        STABLE_SH.into(),
        PROBE_TIMEOUT,
    )
    .await
}

#[cfg(unix)]
async fn query_sh(body: &str) -> Result<ClaudeAuthStatus, ClaudeAuthError> {
    let output = run_sh_probe(body, &["auth", "status"]).await?;
    parse_auth_status_output(STABLE_SH, &output)
}

#[cfg(unix)]
async fn logout_sh(body: &str) -> Result<(), ClaudeAuthError> {
    let output = run_sh_probe(body, &["auth", "logout"]).await?;
    if output.status.success() {
        Ok(())
    } else {
        Err(ClaudeAuthError::ExitStatus {
            program: STABLE_SH.into(),
            status: output.status,
            stderr: output.stderr_lossy_trimmed(),
        })
    }
}

#[cfg(unix)]
async fn version_sh(body: &str) -> Result<String, ClaudeAuthError> {
    let output = run_sh_probe(body, &["--version"]).await?;
    if !output.status.success() {
        return Err(ClaudeAuthError::ExitStatus {
            program: STABLE_SH.into(),
            status: output.status,
            stderr: output.stderr_lossy_trimmed(),
        });
    }
    let stdout = String::from_utf8(output.stdout).map_err(|_| ClaudeAuthError::InvalidUtf8)?;
    let stderr = String::from_utf8_lossy(&output.stderr);
    let version = first_nonempty_line(&stdout)
        .or_else(|| first_nonempty_line(&stderr))
        .unwrap_or("unknown version")
        .to_string();
    Ok(version)
}

#[test]
fn describe_signed_in_includes_email_subscription_and_owner() {
    let status = ClaudeAuthStatus {
        logged_in: true,
        auth_method: Some("claude.ai".into()),
        api_provider: Some("firstParty".into()),
        email: Some("someone@example.com".into()),
        org_id: Some("org".into()),
        org_name: Some("Example".into()),
        subscription_type: Some("pro".into()),
    };

    assert_eq!(
        status.describe(),
        "claude code: signed in as someone@example.com (pro) - managed by the claude binary"
    );
}

#[test]
fn describe_signed_out_points_at_login() {
    let status = ClaudeAuthStatus {
        logged_in: false,
        auth_method: None,
        api_provider: None,
        email: None,
        org_id: None,
        org_name: None,
        subscription_type: None,
    };

    assert_eq!(
        status.describe(),
        "claude code: not signed in - run /login claude-code"
    );
}

#[test]
fn describe_login_success_keeps_ownership_clear() {
    let status = ClaudeAuthStatus {
        logged_in: true,
        auth_method: None,
        api_provider: None,
        email: Some("someone@example.com".into()),
        org_id: None,
        org_name: None,
        subscription_type: Some("pro".into()),
    };

    assert_eq!(
        status.describe_login_success(),
        "claude code: signed in as someone@example.com (pro)\n\
Managed by the claude binary. Rho reads this state with `claude auth status`."
    );
}

#[test]
fn parses_optional_auth_fields_and_requires_logged_in() {
    let status: ClaudeAuthStatus = serde_json::from_str(
        r#"{
            "loggedIn": true,
            "email": "someone@example.com"
        }"#,
    )
    .unwrap();
    assert!(status.logged_in);
    assert_eq!(status.email.as_deref(), Some("someone@example.com"));
    assert_eq!(status.subscription_type, None);

    let error = serde_json::from_str::<ClaudeAuthStatus>(r#"{"email":"x"}"#).unwrap_err();
    assert!(error.to_string().contains("loggedIn") || error.to_string().contains("missing field"));
}

#[test]
fn probe_snapshot_reports_typed_health_not_display_strings() {
    let signed_in = ClaudeProbeSnapshot {
        auth: Ok(ClaudeAuthStatus {
            logged_in: true,
            auth_method: None,
            api_provider: None,
            email: Some("a@b.c".into()),
            org_id: None,
            org_name: None,
            subscription_type: None,
        }),
        version: Ok("1.2.3".into()),
    };
    assert!(signed_in.auth_healthy());
    assert!(signed_in.binary_healthy());
    assert!(signed_in.auth_description().contains("signed in"));
    assert_eq!(signed_in.version_description(), "1.2.3");

    let missing = ClaudeProbeSnapshot {
        auth: Err(ClaudeAuthError::BinaryMissing.to_string()),
        version: Err(ClaudeAuthError::BinaryMissing.to_string()),
    };
    assert!(!missing.auth_healthy());
    assert!(!missing.binary_healthy());
    assert_eq!(
        missing.auth_description(),
        "claude code: binary not found on PATH"
    );

    let not_refreshed = ClaudeProbeSnapshot::not_refreshed_during_turn();
    assert!(!not_refreshed.auth_healthy());
    assert!(!not_refreshed.binary_healthy());
    assert!(not_refreshed
        .auth_description()
        .contains("not refreshed during a model turn"));
}

#[cfg(unix)]
#[tokio::test]
async fn query_program_parses_signed_in_status_json() {
    let status = query_sh(
        r#"
if [ "$1" = "auth" ] && [ "$2" = "status" ]; then
  printf '%s\n' '{"loggedIn":true,"email":"someone@example.com","subscriptionType":"pro"}'
  exit 0
fi
echo "unexpected: $*" >&2
exit 1
"#,
    )
    .await
    .unwrap();
    assert_eq!(
        status,
        ClaudeAuthStatus {
            logged_in: true,
            auth_method: None,
            api_provider: None,
            email: Some("someone@example.com".into()),
            org_id: None,
            org_name: None,
            subscription_type: Some("pro".into()),
        }
    );
}

#[cfg(unix)]
#[tokio::test]
async fn query_program_parses_signed_out_status_json() {
    let status = query_sh(
        r#"
if [ "$1" = "auth" ] && [ "$2" = "status" ]; then
  printf '%s\n' '{"loggedIn":false}'
  exit 0
fi
exit 1
"#,
    )
    .await
    .unwrap();
    assert!(!status.logged_in);
    assert_eq!(
        status.describe(),
        "claude code: not signed in - run /login claude-code"
    );
}

#[cfg(unix)]
#[tokio::test]
async fn query_program_accepts_valid_signed_out_json_on_nonzero_exit() {
    // Real `claude auth status` returns exit 1 with this shape when signed out.
    let status = query_sh(
        r#"
if [ "$1" = "auth" ] && [ "$2" = "status" ]; then
  printf '%s\n' '{"loggedIn":false,"authMethod":"none","apiProvider":"firstParty"}'
  exit 1
fi
exit 2
"#,
    )
    .await
    .unwrap();
    assert_eq!(
        status,
        ClaudeAuthStatus {
            logged_in: false,
            auth_method: Some("none".into()),
            api_provider: Some("firstParty".into()),
            email: None,
            org_id: None,
            org_name: None,
            subscription_type: None,
        }
    );
    assert_eq!(
        status.describe(),
        "claude code: not signed in - run /login claude-code"
    );
}

#[cfg(unix)]
#[tokio::test]
async fn query_program_rejects_malformed_json_on_nonzero_exit() {
    let error = query_sh(
        r#"
if [ "$1" = "auth" ] && [ "$2" = "status" ]; then
  printf '%s\n' 'not-json'
  echo boom >&2
  exit 1
fi
exit 2
"#,
    )
    .await
    .unwrap_err();
    assert!(
        matches!(error, ClaudeAuthError::InvalidJson(_)),
        "malformed stdout must stay InvalidJson, not ExitStatus: {error:?}"
    );
}

#[cfg(unix)]
#[tokio::test]
async fn query_program_reports_exit_status_when_stdout_empty_on_nonzero_exit() {
    let error = query_sh(
        r#"
if [ "$1" = "auth" ] && [ "$2" = "status" ]; then
  echo boom >&2
  exit 1
fi
exit 2
"#,
    )
    .await
    .unwrap_err();
    match error {
        ClaudeAuthError::ExitStatus { stderr, .. } => assert_eq!(stderr, "boom"),
        other => panic!("expected ExitStatus, got {other:?}"),
    }
}

#[cfg(unix)]
#[tokio::test]
async fn query_program_rejects_oversized_stdout() {
    let oversized = PROBE_OUTPUT_CAP_BYTES + 1;
    let body = format!(
        r#"
if [ "$1" = "auth" ] && [ "$2" = "status" ]; then
  # Emit more than the probe cap without sleeping.
  head -c {oversized} /dev/zero
  exit 0
fi
exit 1
"#
    );
    let error = query_sh(&body).await.unwrap_err();
    match error {
        ClaudeAuthError::OutputTooLarge { stream, cap, .. } => {
            assert_eq!(stream, "stdout");
            assert_eq!(cap, PROBE_OUTPUT_CAP_BYTES);
        }
        other => panic!("expected OutputTooLarge, got {other:?}"),
    }
}

#[cfg(unix)]
#[tokio::test]
async fn query_program_reports_missing_binary() {
    let directory = tempfile::tempdir().unwrap();
    let missing = directory.path().join("missing-claude");
    // Explicit missing-path spawn: resolve_named must not invent a binary.
    let error = executable::resolve_named(missing.to_str().unwrap()).unwrap_err();
    assert!(error.is_binary_missing());
    assert_eq!(error.to_string(), "claude code: binary not found on PATH");

    // Also cover the probe spawn path when the program image is absent.
    let mut command = Command::new(&missing);
    command.args(["auth", "status"]);
    let spawn_error =
        run_bounded_command_with_timeout(command, missing.display().to_string(), PROBE_TIMEOUT)
            .await
            .unwrap_err();
    assert!(spawn_error.is_binary_missing());
}

#[cfg(unix)]
#[tokio::test]
async fn query_program_rejects_malformed_json() {
    let error = query_sh(
        r#"
if [ "$1" = "auth" ] && [ "$2" = "status" ]; then
  printf '%s\n' 'not-json'
  exit 0
fi
exit 1
"#,
    )
    .await
    .unwrap_err();
    assert!(matches!(error, ClaudeAuthError::InvalidJson(_)));
}

#[cfg(unix)]
#[tokio::test]
async fn query_program_times_out_and_kills_hanging_child() {
    let body = r#"
if [ "$1" = "auth" ] && [ "$2" = "status" ]; then
  sleep 30
  exit 0
fi
exit 1
"#;
    let injected = Duration::from_millis(200);
    let started = std::time::Instant::now();
    let error = run_bounded_command_with_timeout(
        sh_probe_command(body, &["auth", "status"]),
        STABLE_SH.into(),
        injected,
    )
    .await
    .unwrap_err();
    match error {
        ClaudeAuthError::Timeout { timeout, .. } => assert_eq!(timeout, injected),
        other => panic!("expected Timeout, got {other:?}"),
    }
    // Must not wait out the production PROBE_TIMEOUT (10s) or the child sleep.
    assert!(started.elapsed() < Duration::from_secs(2));
}

#[test]
fn login_args_keep_supported_claudeai_flag() {
    // Verified against installed `claude auth login --help`: `--claudeai` is
    // the subscription login option (default). Do not drop a supported flag.
    assert_eq!(login_args(), &["auth", "login", "--claudeai"]);
}

#[cfg(unix)]
#[tokio::test]
async fn version_program_reads_first_line() {
    let version = version_sh(
        r#"
if [ "$1" = "--version" ]; then
  printf '%s\n' '1.2.3 (Claude Code)'
  exit 0
fi
exit 1
"#,
    )
    .await
    .unwrap();
    assert_eq!(version, "1.2.3 (Claude Code)");
}

#[cfg(unix)]
#[tokio::test]
async fn logout_then_status_is_source_of_truth_even_on_nonzero_exit() {
    let directory = tempfile::tempdir().unwrap();
    let state = directory.path().join("state");
    std::fs::write(&state, "in").unwrap();
    let body = format!(
        r#"
STATE="{state}"
if [ "$1" = "auth" ] && [ "$2" = "logout" ]; then
  printf 'out' > "$STATE"
  echo boom >&2
  exit 7
fi
if [ "$1" = "auth" ] && [ "$2" = "status" ]; then
  if [ "$(cat "$STATE")" = "out" ]; then
    printf '%s\n' '{{"loggedIn":false}}'
  else
    printf '%s\n' '{{"loggedIn":true,"email":"x@y.z"}}'
  fi
  exit 0
fi
exit 1
"#,
        state = state.display()
    );

    let logout_error = logout_sh(&body).await.unwrap_err();
    assert!(matches!(logout_error, ClaudeAuthError::ExitStatus { .. }));
    assert_eq!(logout_error.stderr_excerpt(), Some("boom"));

    let status = query_sh(&body).await.unwrap();
    assert!(
        !status.logged_in,
        "status after failed logout child must still report signed out"
    );
}

#[cfg(unix)]
#[tokio::test]
async fn logout_already_signed_out_status_is_success_truth() {
    let body = r#"
if [ "$1" = "auth" ] && [ "$2" = "logout" ]; then
  echo "not logged in" >&2
  exit 1
fi
if [ "$1" = "auth" ] && [ "$2" = "status" ]; then
  printf '%s\n' '{"loggedIn":false}'
  exit 0
fi
exit 1
"#;

    let _ = logout_sh(body).await;
    let status = query_sh(body).await.unwrap();
    assert!(!status.logged_in);
}

#[test]
fn describe_probe_result_prefers_typed_missing_binary_copy() {
    let missing = Err(ClaudeAuthError::BinaryMissing);
    assert_eq!(
        describe_probe_result(&missing),
        "claude code: binary not found on PATH"
    );
}

#[test]
fn missing_binary_error_display_matches_transparency_copy() {
    assert_eq!(
        ClaudeAuthError::BinaryMissing.to_string(),
        "claude code: binary not found on PATH"
    );
}

#[test]
fn logout_confirm_copy_states_global_sign_out() {
    let copy = logout_confirm_description();
    assert!(copy.contains("everywhere the claude binary is used"));
    assert!(copy.contains("Rho does not store this credential"));
}