car-server-core 0.55.0

Transport-neutral library for the CAR daemon JSON-RPC dispatcher (used by car-server and tokhn-daemon)
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
//! The live trial: real GitHub permissions, real models, real delivery.
//!
//! Everything else in this feature is either a unit test against a fake or an
//! end-to-end run with the model seamed. This file is what closes the last of
//! it — and it is `#[ignore]`d, because each test here needs credentials, a
//! network, and real money, none of which CI has.
//!
//! Run them deliberately:
//!
//! ```text
//! cargo test -p car-server-core --lib coder::heal_trial -- --ignored --nocapture
//! ```
//!
//! ## What each one closes
//!
//! [`a_real_maintainer_issue_resolves_to_a_tier_that_can_seed`] is the
//! provenance gate against **live GitHub**: `resolve_tier` performs its real
//! `gh api` permission lookup and `seed_session` applies its real freshness
//! window. Read-only — it lists issues and asks GitHub about an author, and
//! writes nothing anywhere.
//!
//! [`a_real_model_heals_a_real_repository_under_a_real_panel`] is the coding
//! half with nothing scripted: the daemon's own inference engine drives the
//! session, the contract is derived and then re-run by the runtime, and a real
//! cross-vendor panel judges the real diff. Delivery commits and pushes to a
//! real `origin` — a local bare repository, so the push is a real push and no
//! pull request is opened on anybody's GitHub. That last call is the single
//! step still seamed, and it is seamed on purpose: opening a pull request is an
//! outward, hard-to-reverse act on a repository a person owns, and it is theirs
//! to authorise, not this test's to assume.

use std::path::Path;
use std::sync::{Arc, Mutex};

use super::heal_intake::{Checkout, HealTarget};
use super::heal_live::{CoderRunner, LiveTickIo};
use super::heal_runner::{delivery_branch, LiveCoderRunner, Reviewer};
use super::heal_select::{Candidate, CandidateKind};
use super::heal_tick::{Intent, TickIo};
use super::merge::{
    CiCheck, CiState, CiSummary, GhError, GitHubApi, PrDeliveryOutcome, PrRecord, PrState,
};
use super::provenance::LocalSignatures;
use super::router::EngineChoice;
use crate::session::ServerState;

/// The repository these trials read. Read-only in every case.
const LIVE_REPO: &str = "Parslee-ai/car";

fn state() -> Arc<ServerState> {
    let journal = tempfile::tempdir().unwrap();
    // Leaked on purpose: the state outlives this handle and the directory must
    // survive with it. These are `--ignored` trials, not a suite.
    let path = journal.keep();
    Arc::new(ServerState::standalone(path))
}

fn git(dir: &Path, args: &[&str]) -> String {
    let out = std::process::Command::new("git")
        .arg("-C")
        .arg(dir)
        .args(args)
        .output()
        .expect("git runs");
    assert!(
        out.status.success(),
        "git {args:?}: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    String::from_utf8_lossy(&out.stdout).into_owned()
}

// --- Trial 1: the provenance gate, against live GitHub ----------------------

/// A real issue, a real author, a real permission lookup.
///
/// This is the one thing no other test can reach: `resolve_tier` asks GitHub
/// what an author's permission actually is, and deliberately memoizes nothing,
/// so it cannot be exercised without the network. Until this ran, "only
/// maintainer-or-better issues seed a session" was a property of the code and
/// not an observed fact.
#[tokio::test]
#[ignore = "live: needs `gh` auth and network"]
async fn a_real_maintainer_issue_resolves_to_a_tier_that_can_seed() {
    let io = LiveTickIo {
        issues: Arc::new(super::fix_issues::GhIssues),
        prs: Arc::new(super::heal_intake::GhPullRequests),
        oracle: Arc::new(super::provenance::GhPermissions),
        coder: Arc::new(Unreached),
        local_signatures: LocalSignatures::from_proposals(&[]),
        redactor: car_selfheal::redact::Redactor::from_env(std::env::vars()),
        // This trial reaches tiering, never delivery, so the panel it would be
        // reported with is not exercised here.
        panel: Vec::new(),
    };
    let target = HealTarget {
        repo: LIVE_REPO.into(),
        fix_repo: None,
        checkout: None,
        label: "self-heal".into(),
        base: "main".into(),
    };

    // The real scan, over the real queue.
    let candidates = io.candidates(&target).await.expect("live issue list");
    assert!(!candidates.is_empty(), "the live queue is empty");
    println!("scanned {} open issues", candidates.len());

    // Tier the newest one. `intent_for` is where the real `gh api` permission
    // lookup happens, and it is deliberately per-item.
    let newest = candidates
        .iter()
        .max_by_key(|c| c.number)
        .expect("a candidate");
    let intent = io.intent_for(newest).await.expect("live tier resolution");

    match intent {
        Intent::Seed(seed) => {
            println!(
                "#{} cleared to seed a session ({} chars of intent)",
                newest.number,
                seed.as_str().len()
            );
            assert!(!seed.as_str().is_empty());
        }
        Intent::Refused { tier, stale } => panic!(
            "#{} was refused (tier {tier:?}, stale {stale}) — this repository's own \
             maintainer must clear the gate, or the loop can never act on anything",
            newest.number
        ),
        Intent::Gone => println!("#{} closed between the scan and now", newest.number),
    }
}

/// A `CoderRunner` that must not be reached — trial 1 resolves a tier and
/// stops, and running a session there would be an unannounced side effect.
struct Unreached;

#[async_trait::async_trait]
impl CoderRunner for Unreached {
    async fn run(
        &self,
        _t: &HealTarget,
        _i: &Candidate,
        _s: &super::provenance::SessionSeed,
    ) -> Result<super::heal_tick::Attempt, super::heal_tick::RunFailure> {
        panic!("the tier trial must not start a coder session")
    }
    async fn deliver(
        &self,
        _t: &HealTarget,
        _i: &Candidate,
        _s: &str,
        _b: &str,
    ) -> Result<PrDeliveryOutcome, super::heal_tick::DeliverRefusal> {
        panic!("the tier trial must not deliver")
    }
    async fn abandon(&self, _s: &str) {}
}

// --- Trial 2: a real model, a real panel, a real push -----------------------

/// The final `gh` call, and only that.
struct RecordingGh {
    created: Mutex<Vec<(String, String)>>,
}

impl GitHubApi for RecordingGh {
    fn auth_status(&self) -> Result<(), GhError> {
        Ok(())
    }
    fn list_prs_for_head(&self, _d: &Path, _h: &str) -> Result<Vec<PrRecord>, GhError> {
        Ok(Vec::new())
    }
    fn create_pr(
        &self,
        _d: &Path,
        head: &str,
        base: &str,
        _t: &str,
        _b: &str,
        _draft: bool,
    ) -> Result<PrRecord, GhError> {
        self.created
            .lock()
            .unwrap()
            .push((head.to_string(), base.to_string()));
        Ok(PrRecord {
            number: 1,
            state: PrState::Open,
            url: "https://example.invalid/pull/1".into(),
            is_draft: false,
            base: base.to_string(),
        })
    }
    fn set_pr_body(&self, _d: &Path, _n: u64, _b: &str) -> Result<(), GhError> {
        Ok(())
    }
    fn ci_for_sha(&self, _d: &Path, _n: u64, head_sha: &str) -> Result<CiSummary, GhError> {
        Ok(CiSummary {
            observation_error: None,
            head_sha: head_sha.to_string(),
            state: CiState::Green,
            checks: vec![CiCheck {
                name: "trial".into(),
                state: CiState::Green,
            }],
        })
    }
}

/// The whole coding half, with nothing scripted.
///
/// A real model derives the contract and writes the code; the runtime re-runs
/// the contract; a real panel judges the real diff; delivery commits and pushes
/// to a real remote. The task is deliberately small and unambiguous — the point
/// is to observe the machinery under real inference, not to measure a model.
#[tokio::test(flavor = "multi_thread")]
#[ignore = "live: needs provider credentials, network, and real spend"]
async fn a_real_model_heals_a_real_repository_under_a_real_panel() {
    // A real repo with a real bare origin, so the push is a real push.
    let origin = tempfile::tempdir().unwrap();
    let repo = tempfile::tempdir().unwrap();
    git(origin.path(), &["init", "-q", "--bare", "-b", "main"]);
    git(repo.path(), &["init", "-q", "-b", "main"]);
    git(repo.path(), &["config", "user.name", "heal-trial"]);
    git(repo.path(), &["config", "user.email", "heal@car"]);
    // A repository shaped like one a contract can actually be derived for: a
    // bug, and a test that FAILS because of it. The first version of this trial
    // had no tests at all, and 12 iterations of real inference could not turn
    // a contract green because there was nothing for a contract to check —
    // which is a fact about the fixture, not about the loop.
    std::fs::write(
        repo.path().join("greet.py"),
        "def greet(name):\n    return 'Hello, ' + name\n",
    )
    .unwrap();
    std::fs::write(
        repo.path().join("test_greet.py"),
        "from greet import greet\n\n\n         def test_greets_a_string():\n    assert greet('Ada') == 'Hello, Ada'\n\n\n         def test_greets_a_number():\n    assert greet(7) == 'Hello, 7'\n",
    )
    .unwrap();
    std::fs::write(
        repo.path().join("README.md"),
        "A tiny library used by the CAR self-heal live trial. Run the tests with          `python3 -m pytest`.\n",
    )
    .unwrap();

    // The bug is real: prove the suite is RED before the loop touches it, so a
    // green contract afterwards means something happened.
    let before = std::process::Command::new("python3")
        .args(["-m", "pytest", "-q"])
        .current_dir(repo.path())
        .output()
        .expect("pytest runs");
    assert!(
        !before.status.success(),
        "the fixture is already green; there is nothing to heal"
    );
    println!("fixture is red, as intended");
    git(repo.path(), &["add", "-A"]);
    git(repo.path(), &["commit", "-qm", "seed"]);
    git(
        repo.path(),
        &["remote", "add", "origin", origin.path().to_str().unwrap()],
    );
    git(repo.path(), &["push", "-q", "origin", "main"]);

    let state = state();
    let state_dir = tempfile::tempdir().unwrap();
    let gh = Arc::new(RecordingGh {
        created: Mutex::new(Vec::new()),
    });

    // A real panel: three seats, three distinct models, and — read this before
    // quoting the run — ONE VENDOR. The design wants vendor diversity, and
    // `heal.toml` will happily give it; this trial is limited by what a
    // non-ACL'd test binary can reach from the keychain, which here is OpenAI
    // alone. So this observes the panel MACHINERY under real inference. It does
    // not demonstrate the non-correlation the panel exists for.
    //
    // `gpt-5.5` is NOT a seat, and that is not cosmetic: it is the model this
    // trial pins as the coder below, and the delivery gate now refuses a change
    // one of its authors also reviewed (car#1299). This trial builds
    // `LiveCoderRunner` directly, so `heal_service`'s assembly-time check never
    // saw the collision — every run would have spent a full live session and
    // then been refused at the gate, which is the gate working and the trial
    // observing nothing.
    let reviewers: Vec<Arc<dyn Reviewer>> = ["gpt-5.6", "gpt-5.6-sol", "gpt-5.4"]
        .iter()
        .map(|m| {
            Arc::new(super::heal_review::ModelReviewer::new(state.clone(), *m)) as Arc<dyn Reviewer>
        })
        .collect();

    let runner = LiveCoderRunner {
        state: state.clone(),
        // THE production generator: the daemon's own inference engine.
        generator: crate::handler::get_inference_engine(&state).clone(),
        state_dir: state_dir.path().to_path_buf(),
        reviewers,
        max_wall_secs: 20 * 60,
        max_iterations: Some(12),
        // `native` rather than the `foreman` default: foreman farms subtasks to
        // an external CLI, and what this trial is here to observe is CAR's own
        // loop end to end.
        engine: EngineChoice::Native,
        // PINNED. The first real run of this trial spent a whole 12-iteration
        // session on Parslee, whose token is expired here, while a working
        // credential sat beside it — adaptive routing picks per request and an
        // unattended loop has nobody to notice. That is exactly the failure the
        // pin exists to prevent, and it was found by running this.
        model: Some("gpt-5.5".into()),
        // The pin above makes these inert, but keep the trial's construction
        // faithful to the production runner shape.
        routing_exclusions: Vec::new(),
        // The real registry, as in production: the self-review gate compares
        // canonical ids, and the trial's whole point is to run what the daemon
        // runs.
        canonical_model: {
            let engine_handle = crate::handler::get_inference_engine(&state).clone();
            Arc::new(move |m: &str| {
                engine_handle
                    .model_schema(m)
                    .map(|s| s.id.clone())
                    .unwrap_or_else(|| m.to_string())
            })
        },
        github: gh.clone(),
    };

    let target = HealTarget {
        repo: "acme/greet".into(),
        fix_repo: None,
        checkout: Some(Checkout::Local(repo.path().to_path_buf())),
        label: "self-heal".into(),
        base: "main".into(),
    };
    let item = Candidate {
        repo: "acme/greet".into(),
        number: 1,
        tier: Some(super::provenance::ProvenanceTier::Maintainer),
        labelled: true,
        created_ms: 1,
        kind: CandidateKind::Issue,
    };
    let seed = super::provenance::SessionSeed::from_trusted(
        "greet() crashes when called with a non-string, e.g. greet(7) raises \
         TypeError. Make it accept any value by converting it to a string.",
    );

    println!("running a real coder session…");
    let attempt = runner
        .run(&target, &item, &seed)
        .await
        .expect("the real coder session");

    println!(
        "contract_passed={} panel={} verdicts={:?} unreachable={:?}",
        attempt.contract_passed,
        attempt.panel_size,
        attempt
            .verdicts
            .iter()
            .map(|v| (v.model.as_str(), v.pass))
            .collect::<Vec<_>>(),
        attempt.unreachable
    );
    // The derived contract, printed whatever happens: it is the thing the
    // runtime actually held the session to, and a red run is uninterpretable
    // without it.
    {
        let sessions = state.coder_sessions.lock().await;
        if let Some(entry) = sessions.get(&attempt.session_id) {
            let s = entry.session.lock().await;
            if let Some(c) = &s.contract {
                println!("--- derived contract ---");
                println!("description: {}", c.description);
                for check in &c.checks {
                    println!("  check {:?}: {}", check.name, check.command);
                }
            }
            println!("iterations: {}", s.iterations);
        }
    }
    assert!(
        attempt.contract_passed,
        "the runtime's own contract re-run went red: {}",
        attempt.contract_detail
    );
    assert!(
        !attempt.verdicts.is_empty(),
        "no reviewer produced a readable verdict — every seat was unreachable: {:?}",
        attempt.unreachable
    );

    // The real gate, over real verdicts.
    let gate = super::heal_gate::decide(
        attempt.contract_passed,
        &attempt.contract_detail,
        attempt.panel_size,
        &attempt.verdicts,
        &attempt.unreachable,
    );
    println!("gate: {}", gate.summary());
    assert!(
        gate.approved(),
        "the real panel refused the change: {}",
        gate.summary()
    );

    // Real delivery: commit, push to the real origin, reconcile one PR.
    let delivered = runner
        .deliver(&target, &item, &attempt.session_id, "live trial")
        .await
        .expect("real delivery");
    println!(
        "delivered: {} ({})",
        delivered.pr_url,
        delivered.delivery_report()
    );

    let branch = delivery_branch(&item);
    let on_origin = git(
        origin.path(),
        &["rev-parse", &format!("refs/heads/{branch}")],
    );
    println!("{branch} is on the remote at {}", on_origin.trim());

    // The fix is really in the pushed commit.
    let content = git(
        origin.path(),
        &["show", &format!("{}:greet.py", on_origin.trim())],
    );
    println!("--- delivered greet.py ---\n{content}");
    // Not a string match on the fix: check out the delivered commit and RUN
    // the suite. What matters is that the tests pass, not that the model chose
    // a particular spelling.
    let checkout = tempfile::tempdir().unwrap();
    git(
        origin.path(),
        &[
            "worktree",
            "add",
            "-q",
            "--detach",
            checkout.path().to_str().unwrap(),
            on_origin.trim(),
        ],
    );
    let after = std::process::Command::new("python3")
        .args(["-m", "pytest", "-q"])
        .current_dir(checkout.path())
        .output()
        .expect("pytest runs");
    println!(
        "delivered suite: {}",
        String::from_utf8_lossy(&after.stdout).trim()
    );
    assert!(
        after.status.success(),
        "the delivered commit does not pass the suite:\n{}",
        String::from_utf8_lossy(&after.stdout)
    );

    // `main` is untouched on both sides: the loop proposes, it does not land.
    for (which, files) in [
        ("local", git(repo.path(), &["show", "main:greet.py"])),
        ("origin", git(origin.path(), &["show", "main:greet.py"])),
    ] {
        assert_eq!(
            files, "def greet(name):\n    return 'Hello, ' + name\n",
            "the loop wrote to {which} main"
        );
    }

    // Exactly one pull request would have been opened, from the stable
    // per-item branch into the target's base.
    let created = gh.created.lock().unwrap().clone();
    assert_eq!(created, vec![(branch.clone(), "main".to_string())]);

    // The session reached a terminal state, so its worktree is released.
    let sessions = state.coder_sessions.lock().await;
    let entry = sessions.get(&attempt.session_id).expect("the session");
    let s = entry.session.lock().await;
    assert_eq!(s.state, super::session::CoderState::Merged);
    if let Some(w) = &s.workspace_path {
        assert!(!w.exists(), "the worktree outlived the session: {w:?}");
    }
    println!("session {} is {}", attempt.session_id, s.state.as_str());
}