thirdshift 0.26.0

Turn a GitHub issue into a ready-for-review pull request, unattended
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
//! One scoped Trial execution, through its completed mechanical Score.
//!
//! Trace and active evidence remain private and alive through publication.

mod monitor;
mod toolchains;

use std::cell::RefCell;
use std::fs;
use std::os::unix::fs::{PermissionsExt, symlink};
use std::os::unix::process::ExitStatusExt;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};

use anyhow::{Context, Result, bail, ensure};
use serde_json::json;

use super::{Arm, Setup, Task, arms, grading, integrity, store, trial_results};
use crate::process::{self, Control, Interruption};
use crate::session::limits;
use crate::trial::{Outcome, Output, VoidCause};
use crate::{interrupt, logs, progress};

/// Fixed inputs shared by both workers. Caps and PATH are read per execution.
pub(super) struct Execution<'a> {
    directory: &'a Path,
    prepared: &'a arms::Prepared,
    setups: [&'a Setup; 2],
    timeout: Duration,
    signal: &'a Path,
    toolchains: toolchains::Toolchains,
    pricing: Option<&'a crate::trial::accounting::Prices>,
}

pub(super) struct Request<'a> {
    pub task: &'a Task,
    pub identity: trial_results::Identity<'a>,
    pub attempt: usize,
    pub trace: &'a logs::trace::Context,
}

impl<'a> Execution<'a> {
    /// Prepare pinned Harness directories once, before either worker starts.
    pub fn prepare(
        directory: &'a Path,
        prepared: &'a arms::Prepared,
        setups: [&'a Setup; 2],
        timeout: Duration,
        signal: &'a Path,
    ) -> Result<Self> {
        for (name, arm) in [
            ("baseline", &prepared.baseline),
            ("candidate", &prepared.candidate),
        ] {
            let harness = directory.join("harnesses").join(name);
            fs::create_dir_all(&harness)?;
            let harnesses = std::iter::once(&arm.harness).chain(
                arm.process
                    .iter()
                    .flat_map(|process| process.steps.iter().map(|step| &step.harness)),
            );
            for fixed in harnesses {
                let link = harness.join(&fixed.name);
                if !link.exists() {
                    symlink(&fixed.path, link)?;
                }
            }
        }
        Ok(Self {
            directory,
            prepared,
            setups,
            timeout,
            signal,
            toolchains: toolchains::Toolchains::new(directory),
            pricing: None,
        })
    }

    pub fn with_pricing(mut self, pricing: Option<&'a crate::trial::accounting::Prices>) -> Self {
        self.pricing = pricing;
        self
    }

    /// Invoke completion exactly once after classification and grading.
    /// On every exit Trace finalizes before active evidence is removed.
    pub fn run<T>(
        &self,
        request: Request<'_>,
        complete: impl FnOnce(PathBuf, Output, grading::Score) -> Result<T>,
    ) -> Result<T> {
        let task = request.task;
        let (arm, setup) = match request.identity.arm {
            Arm::Baseline => (&self.prepared.baseline, self.setups[0]),
            Arm::Candidate => (&self.prepared.candidate, self.setups[1]),
        };
        for arm in [&self.prepared.baseline, &self.prepared.candidate] {
            if let Some(process) = &arm.process {
                process.verify()?;
            }
        }
        let key = request.identity.key();
        let directory = self.directory.join("trials").join(&key);
        fs::create_dir_all(&directory)?;
        let attempt = tempfile::Builder::new()
            .prefix("attempt-")
            .tempdir_in(&directory)?
            .keep();
        let caches = super::retention::Caches(attempt.clone());
        let prepared_repo = super::super::repository::prepare_trial(
            &task.directory,
            &task.manifest,
            &attempt.join("repository"),
        )?;
        let started = Instant::now();
        let mut trace = super::trace::Attempt::start(&attempt, &key, request.trace)?;
        let _active = super::evidence::Active::start(&directory, &attempt, request.attempt)?;
        let toolchains = match self.toolchains.ensure(&prepared_repo.repository) {
            Ok(toolchains) => toolchains,
            Err(error) => {
                interrupt::check()?;
                let mut output =
                    Output::failed(started.elapsed().as_millis(), &format!("{error:#}"));
                output.outcome = Outcome::Void;
                output.void_cause = Some(VoidCause::SessionUnavailable);
                output.timings.session_ms = 0;
                let score = trace.grade(|| {
                    grading::score(task, &prepared_repo.repository, &output, self.timeout)
                })?;
                trace.outcome(output.outcome);
                return complete(attempt, output, score);
            }
        };
        // Establish the Harness's idempotent instruction fallback before the
        // comparison and the private repository captured for mechanical checks.
        setup
            .choice()
            .harness
            .adapter()
            .link_instruction_fallback(&prepared_repo.repository)?;
        let before = integrity::Snapshot::capture(&prepared_repo.repository, &task.manifest)?;
        // This directory is outside the Trial's sandbox. Graders must see
        // the prepared state even when the review fixes and commits code.
        // The pass-through launcher can write this temp directory. Checks
        // run only when the copy still matches the pre-session digest.
        let grading_snapshot = if task.manifest.checks.is_empty()
            || task.manifest.session == crate::evals::format::Session::Process
        {
            None
        } else {
            // Owner-only: this directory lives in the shared temporary
            // directory and holds the prepared repository.
            let directory = tempfile::Builder::new()
                .prefix("prepared-grader-")
                .permissions(fs::Permissions::from_mode(0o700))
                .tempdir()?;
            let repository = directory.path().join("repository");
            crate::sandbox::code_grader::copy_repository(&prepared_repo.repository, &repository)?;
            let digest = integrity::tree_digest(&repository)?;
            Some(SealedSnapshot {
                repository,
                digest,
                _directory: directory,
            })
        };
        let grading_repository = grading_snapshot.as_ref().map_or_else(
            || prepared_repo.repository.clone(),
            |snapshot| snapshot.repository.clone(),
        );
        let inputs = task
            .manifest
            .trial
            .as_ref()
            .context("missing Trial inputs")?;
        let request_path = attempt.join("request.json");
        let mut document = json!({
            "version": 1, "repository": prepared_repo.repository, "session_kind": task.manifest.session,
            "inputs": inputs, "harness": setup.harness, "model": setup.model, "effort": setup.effort,
            "output_dir": attempt.join("session")
        });
        if task.manifest.session == crate::evals::format::Session::Process {
            document["process"] = match &arm.process {
                Some(process) => {
                    let file = process.install(&attempt.join("home"))?;
                    // Earlier Arm builds already support the Built-in name.
                    // Its frozen bytes were read from that exact executable.
                    if setup.process.as_deref() == Some("ticket-run") {
                        json!("ticket-run")
                    } else {
                        json!(file)
                    }
                }
                None => json!(setup.process.as_deref().unwrap_or("ticket-run")),
            };
        }
        store::write(&request_path, &document)?;
        fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(self.directory.join("started"))?;
        limits::check(self.signal)?;
        progress::step(format!("Trial {} starting", key));
        let monitor = RefCell::new(monitor::Monitor::new(
            &attempt.join("session"),
            &prepared_repo.repository,
            &setup.choice(),
        ));
        let started = Instant::now();
        // Machine caps are read live per attempt, not fixed with the Bar: an
        // operator edit between attempts applies to the next one.
        let caps = crate::config::UserConfig::load()?.trial_caps.with_env()?;
        let mut launched =
            self.command(arm, &request, &request_path, &caps, toolchains.as_deref())?;
        // The unit name stays in the attempt for `journalctl --user -u` after.
        fs::write(attempt.join("scope"), launched.unit())?;
        trace.inherit(launched.command())?;
        let output = process::output_until(
            &mut launched,
            None,
            Control {
                name: "Trial",
                interruption: Interruption::Ordinary,
                stop: &|child| process::stop(child, &[libc::SIGTERM, libc::SIGKILL]),
            },
            Instant::now() + self.timeout,
            &|| monitor.borrow_mut().check(self.signal),
        );
        let stopped_at = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)?
            .as_nanos()
            .try_into()?;
        drop(caches);
        let interruption = self.accounting_interruption()?;
        // A kernel kill in the Trial's scope is the outer launcher's evidence
        // the cap killed Trial work. The live watch catches slow kills; the
        // journal's `oom-kill` result covers fast ones, whose scope is already
        // gone. Only failed Trials consult it: explicit Void causes stay as
        // the Trial diagnosed them, so limit pauses keep pausing, and an
        // agent's own out-of-memory error leaves no kernel kill behind.
        let cap_killed = || launched.oom_killed();
        let mut protocol = if let Some(error) = interruption {
            void_interrupted(started.elapsed().as_millis(), &error)
        } else {
            match output {
                Ok(output) => {
                    fs::write(attempt.join("stdout"), &output.stdout)?;
                    fs::write(attempt.join("stderr"), &output.stderr)?;
                    // systemd-run reports its scope's signal death as an exit
                    // code, so a scoped SIGKILL arrives as 137, not as a signal.
                    let sigkilled = output.status.signal() == Some(libc::SIGKILL)
                        || output.status.code() == Some(128 + libc::SIGKILL);
                    if sigkilled {
                        Output::failed(
                            started.elapsed().as_millis(),
                            "Trial process killed (SIGKILL); external cause unknown",
                        )
                    } else if !output.status.success() {
                        if cap_killed() {
                            void_memory_cap(started.elapsed().as_millis())
                        } else {
                            bail!(
                                "Arm Trial protocol failed ({}): {}",
                                output.status,
                                String::from_utf8_lossy(&output.stderr)
                            );
                        }
                    } else {
                        match serde_json::from_slice::<Output>(&output.stdout) {
                            Ok(protocol) => {
                                ensure!(protocol.version == 1, "unsupported Trial result");
                                protocol
                            }
                            Err(error) => {
                                if cap_killed() {
                                    void_memory_cap(started.elapsed().as_millis())
                                } else {
                                    return Err(error).context("invalid Trial result");
                                }
                            }
                        }
                    }
                }
                Err(error) if error.is::<process::TimedOut>() => {
                    Output::failed(u128::from(self.timeout.as_secs()) * 1000, "Trial timed out")
                }
                Err(error) if error.is::<limits::Reached>() => monitor
                    .into_inner()
                    .cut_short(started.elapsed().as_millis()),
                Err(error) => return Err(error),
            }
        };
        if protocol.outcome == Outcome::Failed && cap_killed() {
            protocol.outcome = Outcome::Void;
            protocol.void_cause = Some(VoidCause::MemoryCap);
            protocol.cause = Some(crate::scope::OOM_KILL_CAUSE.into());
        }
        if task.manifest.session == crate::evals::format::Session::Process
            && protocol.steps.is_empty()
        {
            let path = attempt.join("session/steps.json");
            if path.is_file() {
                protocol.steps = serde_json::from_slice(&fs::read(path)?)?;
            }
        }
        // A successful protocol can also claim interruption. Only an attempt
        // that is already void for that cause skips the repository walk.
        let unchanged = (protocol.outcome == Outcome::Void
            && protocol.void_cause == Some(VoidCause::Interrupted))
            || task.manifest.session == crate::evals::format::Session::Process
            || before
                .unchanged(&prepared_repo.repository)
                .is_ok_and(|unchanged| unchanged);
        let snapshot_intact = grading_snapshot.as_ref().is_none_or(|snapshot| {
            integrity::tree_digest(&snapshot.repository)
                .is_ok_and(|digest| digest == snapshot.digest)
        });
        if let Some(error) = self.accounting_interruption()? {
            protocol.outcome = Outcome::Void;
            protocol.void_cause = Some(VoidCause::Interrupted);
            protocol.cause = Some(format!("{error:#}"));
        }
        // Keep known failures and external Void causes, including limit
        // stops. Repository changes invalidate an otherwise successful Trial,
        // including a process-shaped delivered outcome on a review session.
        if matches!(protocol.outcome, Outcome::Succeeded | Outcome::Delivered)
            && (!unchanged || !snapshot_intact)
        {
            protocol.outcome = Outcome::Void;
            protocol.void_cause = Some(VoidCause::RepositoryChanged);
            protocol.cause = Some("Trial repository changed during its session".into());
        }
        let score = trace.grade(|| {
            if let Some(prices) = self.pricing {
                let role = serde_json::to_value(&task.manifest.session)?;
                let accounting = super::accounting::capture(
                    &attempt,
                    &protocol,
                    role.as_str().unwrap(),
                    prices,
                    stopped_at,
                )?;
                if protocol.outcome != Outcome::Void
                    && let Some(gap) = accounting.gap()
                {
                    protocol.outcome = Outcome::Void;
                    protocol.void_cause = Some(VoidCause::AccountingGap);
                    protocol.cause = Some(gap.into());
                }
                protocol.accounting = Some(accounting);
            }
            match grading::score(task, &grading_repository, &protocol, self.timeout) {
                Err(error) if self.pricing.is_some() && interrupt::check().is_err() => {
                    protocol.outcome = Outcome::Void;
                    protocol.void_cause = Some(VoidCause::Interrupted);
                    protocol.cause = Some(format!("{error:#}"));
                    // Interrupted code checks produced no Score. Retain the
                    // sessions already priced before propagating interruption.
                    grading::score(task, &grading_repository, &protocol, self.timeout)
                }
                Err(error) if error.is::<crate::sandbox::Unavailable>() => {
                    protocol.outcome = Outcome::Void;
                    protocol.void_cause = Some(VoidCause::SandboxUnavailable);
                    protocol.cause = Some(format!("{error:#}"));
                    grading::score(task, &grading_repository, &protocol, self.timeout)
                }
                score => score,
            }
        })?;
        trace.steps(&protocol.steps)?;
        trace.outcome(protocol.outcome);
        if protocol.void_cause == Some(VoidCause::Interrupted)
            && interrupt::check().is_err()
            && let Some(accounting) = &mut protocol.accounting
        {
            accounting.incomplete = true;
        }
        let completed = complete(attempt, protocol, score)?;
        interrupt::check()?;
        Ok(completed)
    }

    // Priced Experiments must retain every started Session before propagating
    // interruption. Legacy Experiments keep their original checkpoint policy.
    fn accounting_interruption(&self) -> Result<Option<anyhow::Error>> {
        match interrupt::check() {
            Err(error) if self.pricing.is_some() => Ok(Some(error)),
            result => result.map(|()| None),
        }
    }

    fn command(
        &self,
        arm: &arms::Arm,
        request: &Request<'_>,
        path: &Path,
        caps: &crate::scope::Caps,
        toolchains: Option<&Path>,
    ) -> Result<crate::scope::Launch> {
        let directory = self
            .directory
            .join("harnesses")
            .join(request.identity.arm.name());
        let path_env = std::env::join_paths(
            std::iter::once(directory)
                .chain(toolchains.map(|root| root.join("session/bin")))
                .chain(std::env::split_paths(
                    &std::env::var_os("PATH").unwrap_or_default(),
                )),
        )?;
        let path_env = path_env
            .into_string()
            .map_err(|_| anyhow::anyhow!("PATH is not valid Unicode"))?;
        let path = path
            .to_str()
            .context("Trial request path is not valid Unicode")?;
        let mut launched = crate::scope::trial_command(
            &arm.binary,
            &["--trial", path],
            &request.identity.key(),
            request.attempt,
            caps,
        )?;
        launched
            .command()
            .env(limits::SIGNAL_ENV, self.signal)
            .env("PATH", path_env)
            .env_remove(crate::skills::TRIAL_HOME);
        if arm.process.is_some()
            && request.task.manifest.session == crate::evals::format::Session::Process
        {
            launched.command().env(
                crate::skills::TRIAL_HOME,
                Path::new(path)
                    .parent()
                    .context("Trial request has no directory")?
                    .join("home"),
            );
        }
        if let Some(toolchains) = toolchains {
            launched.command().env("CARGO_HOME", toolchains);
        }
        Ok(launched)
    }
}

/// Prepared files checks grade, sealed before the session starts.
struct SealedSnapshot {
    repository: PathBuf,
    digest: Vec<u8>,
    _directory: tempfile::TempDir,
}

/// A Trial its scope's cap killed: void with `memory_cap`, from the scope's
/// `memory.events` or journal evidence. Only the outer launcher makes these,
/// never the Trial itself.
fn void_interrupted(total_ms: u128, error: &anyhow::Error) -> Output {
    let mut output = Output::failed(total_ms, &format!("{error:#}"));
    output.outcome = Outcome::Void;
    output.void_cause = Some(VoidCause::Interrupted);
    output
}

fn void_memory_cap(total_ms: u128) -> Output {
    let mut output = Output::failed(total_ms, crate::scope::OOM_KILL_CAUSE);
    output.outcome = Outcome::Void;
    output.void_cause = Some(VoidCause::MemoryCap);
    output
}

#[cfg(test)]
mod tests;