scv-tools 0.3.6

Workspace-scoped filesystem, process, skill, and agent tools for SCV
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
//! Unit tests for `src/project_environment.rs`.

use super::*;
use std::os::unix::fs::PermissionsExt as _;

struct Fixture {
    root: tempfile::TempDir,
    project: PathBuf,
    inherited: Vec<(OsString, OsString)>,
    adapter: Vec<(OsString, OsString)>,
}

fn script(path: &Path, text: &str) {
    std::fs::create_dir_all(path.parent().unwrap()).unwrap();
    std::fs::write(path, format!("#!/bin/sh\n{text}\n")).unwrap();
    std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).unwrap();
}

impl Fixture {
    fn new() -> Self {
        let root = tempfile::tempdir().unwrap();
        let project = root.path().join("project with spaces");
        std::fs::create_dir(&project).unwrap();
        std::fs::write(project.join("Cargo.toml"), "[package]\nrust-version='1.88'").unwrap();
        let host = root.path().join("user");
        let private = root.path().join("agent");
        let system = root.path().join("system");
        script(&system.join("rustc"), "echo 'rustc 1.75.0'");
        script(&system.join("cargo"), "echo 'cargo 1.75.0'");
        for version in ["1.75.0", "1.99.0", "1.100.0"] {
            for tool in ["rustc", "cargo", "rustdoc"] {
                script(
                    &host.join(format!(".rustup/toolchains/{version}/bin/{tool}")),
                    &format!("echo '{tool} {version}'"),
                );
            }
        }
        script(
            &host.join(".cargo/bin/rustup"),
            r#"
case "$1 $2" in
  'show active-toolchain')
    if [ -n "$RUSTUP_TOOLCHAIN" ]; then
      printf '%s (environment override)\n' "$RUSTUP_TOOLCHAIN"
    elif [ -f rust-toolchain.toml ] || [ -f rust-toolchain ]; then
      printf '%s (toolchain file)\n' "${FAKE_PIN:-1.100.0}"
    else
      printf '%s (default)\n' "${FAKE_DEFAULT:-1.100.0}"
    fi;;
  'toolchain list') printf '1.75.0\n1.99.0\n1.100.0\n';;
  'which rustc'|'which cargo')
    if [ -n "$RUSTUP_TOOLCHAIN" ]; then
      selected="$RUSTUP_TOOLCHAIN"
    elif [ -f rust-toolchain.toml ] || [ -f rust-toolchain ]; then
      selected="${FAKE_PIN:-1.100.0}"
    else
      selected="${FAKE_DEFAULT:-1.100.0}"
    fi
    if [ -x "$RUSTUP_HOME/toolchains/$selected/bin/$2" ]; then
      printf '%s/toolchains/%s/bin/%s\n' "$RUSTUP_HOME" "$selected" "$2"
    else
      echo 'toolchain is not installed' >&2; exit 1
    fi;;
  'which --toolchain')
    if [ -x "$RUSTUP_HOME/toolchains/$3/bin/$4" ]; then
      printf '%s/toolchains/%s/bin/%s\n' "$RUSTUP_HOME" "$3" "$4"
    else
      echo 'toolchain is not installed' >&2; exit 1
    fi;;
  *) exit 9;;
esac
"#,
        );
        let inherited = vec![("HOME".into(), host.into()), ("PATH".into(), system.into())];
        let adapter = vec![
            ("HOME".into(), private.clone().into()),
            ("CODEX_HOME".into(), private.into()),
        ];
        Self {
            root,
            project,
            inherited,
            adapter,
        }
    }

    async fn resolve(&self) -> Result<ProjectEnvironment> {
        resolve_from(
            &self.project,
            &self.adapter,
            self.inherited.clone(),
            CancellationToken::new(),
        )
        .await
    }

    fn launch_environment(&self) -> Vec<(OsString, OsString)> {
        let mut environment = self.adapter.clone();
        environment.extend([
            (
                "CARGO_HOME".into(),
                self.root.path().join("user/.cargo").into(),
            ),
            (
                "RUSTUP_HOME".into(),
                self.root.path().join("user/.rustup").into(),
            ),
            ("RUSTUP_TOOLCHAIN".into(), "1.100.0".into()),
            ("PATH".into(), self.root.path().join("system").into()),
        ]);
        environment
    }
}

#[tokio::test]
async fn finds_rustup_outside_service_path_without_reusing_cargo_credentials() {
    let fixture = Fixture::new();
    let result = fixture.resolve().await.unwrap();
    let env: std::collections::HashMap<_, _> = result.environment.iter().cloned().collect();
    assert_eq!(
        PathBuf::from(&env[OsStr::new("CARGO_HOME")]),
        fixture.root.path().join("agent/.cargo")
    );
    assert_eq!(
        PathBuf::from(&env[OsStr::new("RUSTUP_HOME")]),
        fixture.root.path().join("user/.rustup")
    );
    let first = std::env::split_paths(&env[OsStr::new("PATH")])
        .next()
        .unwrap();
    assert!(first.ends_with("1.100.0/bin"));
    assert!(!env.contains_key(OsStr::new("HOME")));
    assert!(!env.contains_key(OsStr::new("CODEX_HOME")));
    // Execute the tools through the environment actually injected into agents.
    let mut command = std::process::Command::new("/bin/sh");
    crate::apply_agent_environment(&mut command, &fixture.adapter);
    let output = command
        .envs(result.environment)
        .arg("-c")
        .arg("rustc --version; cargo --version")
        .output()
        .unwrap();
    assert!(output.status.success());
    assert_eq!(
        String::from_utf8(output.stdout).unwrap(),
        "rustc 1.100.0\ncargo 1.100.0\n"
    );
}

#[tokio::test]
async fn upgrades_only_an_unpinned_old_default_using_numeric_versions() {
    let mut fixture = Fixture::new();
    fixture
        .inherited
        .push(("FAKE_DEFAULT".into(), "1.75.0".into()));
    let result = fixture.resolve().await.unwrap();
    assert!(
        result
            .diagnostics
            .iter()
            .any(|line| line.contains("compatible rustup toolchain: 1.100.0"))
    );
    std::fs::write(
        fixture.project.join("rust-toolchain.toml"),
        "[toolchain]\nchannel='1.75.0'",
    )
    .unwrap();
    fixture.inherited.push(("FAKE_PIN".into(), "1.75.0".into()));
    assert!(
        fixture
            .resolve()
            .await
            .unwrap_err()
            .to_string()
            .contains("explicit toolchain")
    );
}

#[tokio::test]
async fn respects_explicit_overrides_and_never_falls_back_from_missing_pins() {
    let mut fixture = Fixture::new();
    fixture
        .inherited
        .push(("RUSTUP_TOOLCHAIN".into(), "1.99.0".into()));
    assert!(
        fixture
            .resolve()
            .await
            .unwrap()
            .diagnostics
            .iter()
            .any(|line| line.contains("rustc 1.99.0"))
    );
    fixture
        .inherited
        .push(("RUSTUP_TOOLCHAIN".into(), "1.75.0".into()));
    assert!(
        fixture
            .resolve()
            .await
            .unwrap_err()
            .to_string()
            .contains("explicit toolchain")
    );
    fixture
        .inherited
        .push(("RUSTUP_TOOLCHAIN".into(), "missing".into()));
    assert!(
        fixture
            .resolve()
            .await
            .unwrap_err()
            .to_string()
            .contains("not installed")
    );
}

#[tokio::test]
async fn custom_install_locations_and_adapter_path_are_respected() {
    let mut fixture = Fixture::new();
    let custom = fixture.root.path().join("custom");
    std::fs::rename(fixture.root.path().join("user/.cargo"), &custom).unwrap();
    fixture.inherited.push(("CARGO_HOME".into(), custom.into()));
    let toolchains = fixture.root.path().join("toolchains elsewhere");
    std::fs::rename(fixture.root.path().join("user/.rustup"), &toolchains).unwrap();
    fixture
        .inherited
        .push(("RUSTUP_HOME".into(), toolchains.into()));
    assert!(fixture.resolve().await.is_ok());
}

#[tokio::test]
async fn a_separate_cargo_cache_does_not_hide_the_original_rustup_install() {
    let mut fixture = Fixture::new();
    fixture.inherited.push((
        "CARGO_HOME".into(),
        fixture.root.path().join("cache-only").into(),
    ));
    assert!(fixture.resolve().await.is_ok());
}

#[tokio::test]
async fn rejects_old_system_tools_but_accepts_sufficient_ones_without_rustup() {
    let mut fixture = Fixture::new();
    fixture
        .inherited
        .push(("HOME".into(), fixture.root.path().join("no-rustup").into()));
    assert!(
        fixture
            .resolve()
            .await
            .unwrap_err()
            .to_string()
            .contains("obsolete tools")
    );
    std::fs::write(
        fixture.project.join("Cargo.toml"),
        "[package]\nrust-version='1.60'",
    )
    .unwrap();
    assert!(fixture.resolve().await.is_ok());
    std::fs::write(fixture.project.join("rust-toolchain"), "stable").unwrap();
    assert!(
        fixture
            .resolve()
            .await
            .unwrap_err()
            .to_string()
            .contains("needs rustup")
    );
}

#[tokio::test]
async fn system_tools_from_different_directories_keep_their_path_precedence() {
    let mut fixture = Fixture::new();
    fixture
        .inherited
        .push(("HOME".into(), fixture.root.path().join("no-rustup").into()));
    let cargo_bin = fixture.root.path().join("cargo-only");
    let rustc_bin = fixture.root.path().join("system");
    script(&cargo_bin.join("cargo"), "echo 'cargo 1.100.0'");
    script(&rustc_bin.join("rustc"), "echo 'rustc 1.100.0'");
    // The older Cargo in the compiler directory must stay shadowed.
    let path = std::env::join_paths([cargo_bin, rustc_bin]).unwrap();
    fixture.inherited.push(("PATH".into(), path.clone()));
    let result = fixture.resolve().await.unwrap();
    assert!(result.environment.contains(&("PATH".into(), path)));
}

#[tokio::test]
async fn non_rust_project_does_not_probe_or_change_environment() {
    let fixture = Fixture::new();
    std::fs::remove_file(fixture.project.join("Cargo.toml")).unwrap();
    assert!(
        resolve_from(
            &fixture.project,
            &[],
            vec![("PATH".into(), "/nonexistent".into())],
            CancellationToken::new()
        )
        .await
        .unwrap()
        .environment
        .is_empty()
    );
}

#[tokio::test]
async fn missing_cargo_or_obsolete_cargo_fails_preflight() {
    let fixture = Fixture::new();
    let cargo = fixture
        .root
        .path()
        .join("user/.rustup/toolchains/1.100.0/bin/cargo");
    script(&cargo, "echo 'cargo 1.75.0'");
    assert!(
        fixture
            .resolve()
            .await
            .unwrap_err()
            .to_string()
            .contains("obsolete tools")
    );
    std::fs::remove_file(cargo).unwrap();
    assert!(fixture.resolve().await.is_err());
}

#[tokio::test]
async fn probe_is_cancellable_and_output_is_bounded() {
    let fixture = Fixture::new();
    let token = CancellationToken::new();
    token.cancel();
    assert!(
        resolve_from(
            &fixture.project,
            &fixture.adapter,
            fixture.inherited.clone(),
            token
        )
        .await
        .unwrap_err()
        .to_string()
        .contains("cancelled")
    );
    script(
        &fixture.root.path().join("user/.cargo/bin/rustup"),
        "i=0; while [ $i -lt 20000 ]; do printf x; i=$((i+1)); done",
    );
    assert!(fixture.resolve().await.is_err());
}

#[tokio::test]
async fn cancellation_joins_a_running_probe() {
    let fixture = Fixture::new();
    script(
        &fixture.root.path().join("user/.cargo/bin/rustup"),
        "exec /bin/sleep 30",
    );
    let cancellation = CancellationToken::new();
    let stop = cancellation.clone();
    let (result, ()) = tokio::join!(
        resolve_from(
            &fixture.project,
            &fixture.adapter,
            fixture.inherited.clone(),
            cancellation
        ),
        async move {
            tokio::time::sleep(Duration::from_millis(50)).await;
            stop.cancel();
        }
    );
    assert!(result.unwrap_err().to_string().contains("cancelled"));
}

#[tokio::test]
async fn native_launch_receives_project_tools_and_keeps_private_home() {
    use crate::delegate::{
        adapters::{OutputFormat, Resume, Transport},
        agent::Backend as _,
        conversation::ConversationStore,
        native::NativeAgentTool,
    };
    let fixture = Fixture::new();
    let config = crate::AgentAdapterConfig {
        command: "/bin/sh".into(),
        args: vec!["-c".into(), "rustc --version; cargo --version; printf '%s\\n' \"$HOME\" \"$CODEX_HOME\" \"$CARGO_HOME\"".into()],
        prompt_args: Vec::new(), full_permission_args: None, model_args: Vec::new(),
        effort_args: Vec::new(), model_hint: String::new(), environment: fixture.launch_environment(),
        search_dirs: Vec::new(), output: OutputFormat::Text, resume: Resume::Unsupported,
        home: None, transport: Transport::Process, acp: None, use_for: None,
        defaults: crate::AgentDefaults::default(), options_file: None,
    };
    let tool = NativeAgentTool::new(
        "fake".into(),
        config,
        crate::args::Timeouts {
            default: Duration::from_secs(5),
            max: Duration::from_secs(5),
        },
        4096,
        None,
        std::sync::Arc::new(ConversationStore::new(
            crate::ToolsConfig::default().conversations,
            None,
        )),
    );
    let context = || scv_core::ToolContext::new(fixture.project.clone(), CancellationToken::new());
    let output = tool
        .execute(serde_json::json!({"prompt":"hello"}), context())
        .await
        .unwrap();
    assert!(!output.is_error(), "{}", output.content);
    let value: serde_json::Value = serde_json::from_str(&output.content).unwrap();
    assert_eq!(
        value["reply"],
        format!(
            "rustc 1.100.0\ncargo 1.100.0\n{0}/agent\n{0}/agent\n{0}/agent/.cargo",
            fixture.root.path().display()
        )
    );
    std::fs::write(
        fixture.project.join("Cargo.toml"),
        "[package]\nrust-version='1.101'",
    )
    .unwrap();
    assert!(
        tool.execute(serde_json::json!({"prompt":"hello"}), context())
            .await
            .unwrap_err()
            .message
            .contains("explicit toolchain")
    );
}

#[tokio::test]
async fn live_launch_used_by_acp_and_nested_scv_receives_project_tools() {
    use crate::delegate::live::{LiveChild, LiveLine, LiveSpec};
    let fixture = Fixture::new();
    let spec = || LiveSpec {
        executable: "/bin/sh".into(),
        args: vec![
            "-c".into(),
            "rustc --version; cargo --version; read ignored".into(),
        ],
        cwd: fixture.project.clone(),
        environment: fixture.launch_environment(),
        max_line_bytes: 1024,
    };
    let child = LiveChild::spawn(spec(), None, CancellationToken::new())
        .await
        .unwrap();
    for expected in ["rustc 1.100.0", "cargo 1.100.0"] {
        let line = child.recv().await;
        assert!(
            matches!(line, Some(LiveLine::Line(ref bytes)) if String::from_utf8_lossy(bytes).trim() == expected),
            "{line:?}"
        );
    }
    child.close().await;
    std::fs::write(
        fixture.project.join("Cargo.toml"),
        "[package]\nrust-version='1.101'",
    )
    .unwrap();
    assert!(
        LiveChild::spawn(spec(), None, CancellationToken::new())
            .await
            .unwrap_err()
            .message
            .contains("explicit toolchain")
    );
}