thirdshift 0.16.3

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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! Session trace recording: interpret the launch context, checkpoint stream Models,
//! and replace them only when completed Interpretation supplies a report.
//! The captured owning Command also governs child inheritance and collection.

use std::fs;
use std::io::Read;
#[cfg(test)]
use std::io::{BufRead, BufReader};
use std::os::fd::AsRawFd;
use std::path::{Path, PathBuf};
use std::process::{ChildStdout, Command};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};

use crate::harness::interpretation::{Ended, Interpretation};
use crate::harness::{Choice, process};
use crate::logs::owner::Owner;
use crate::{logs, progress};

/// Immutable Command identity, captured after Command logging is established.
#[derive(Clone)]
pub struct Recording {
    /// A Trial explicitly has no Command owner. Allocation failures in a
    /// Command remain errors; they must never select the Trial's storage.
    owner: std::result::Result<Option<Owner>, String>,
    trial_context: Option<logs::trace::Context>,
}

/// Session interpretation policy, prompt transport and execution limits.
#[derive(Default)]
pub struct Input<'a> {
    pub prompt: &'a str,
    pub policy: Policy,
    pub stdin: Option<&'a str>,
    pub deadline: Option<Instant>,
    /// Scoring Graders share the Eval plan-limit policy while retaining Command ownership.
    pub stop_at_limit: bool,
    /// Keep decoded progress and completion text out of factory Command logs.
    pub private: bool,
}

/// Interpretation rules are independent of log labels and output privacy.
#[derive(Default, Clone, Copy)]
pub enum Policy {
    #[default]
    Ordinary,
    Security,
    /// Blind Scoring packets must not consult retained production history.
    StreamOnly,
}

impl Recording {
    /// Capture current logging facts once, preferring a local Command log to
    /// inherited ownership. Child Runs retain their parent's exact path.
    pub fn capture() -> Self {
        Self {
            owner: logs::captured_owner().map(Some),
            trial_context: None,
        }
    }

    /// Trial artifacts have their own empty output directory and no Command.
    pub fn for_trial() -> Self {
        Self {
            owner: Ok(None),
            trial_context: std::env::var(logs::trace::TRIAL_CONTEXT)
                .ok()
                .and_then(|json| serde_json::from_str(&json).ok()),
        }
    }

    /// Construct from captured facts; fixtures need no process-global changes.
    #[cfg(test)]
    pub fn new(local: Option<PathBuf>, inherited: Option<PathBuf>, stamp: String) -> Self {
        Self {
            owner: local
                .or(inherited)
                .map(|log| Owner::fixture(Some(log), stamp))
                .map(Some)
                .ok_or_else(|| "no owner in fixture".into()),
            trial_context: None,
        }
    }

    pub fn session_path(&self, dir: &Path, subject: &str, kind: &str) -> Result<PathBuf> {
        let owner = self
            .owner
            .as_ref()
            .map_err(|cause| anyhow::anyhow!("cannot name Session trace: {cause}"))?;
        Ok(match owner {
            Some(owner) => owner.session_path(dir, subject, kind),
            None => dir.join(format!("{subject}-{kind}.logs.jsonl")),
        })
    }

    fn owner(&self) -> Option<&Owner> {
        self.owner.as_ref().ok()?.as_ref()
    }

    fn command_log(&self) -> Option<&Path> {
        self.owner()?.log_path()
    }

    /// Give a child this exact owning Command, including through nested Runs.
    /// Absence removes stale ownership from the child's environment.
    pub fn inherit_command(&self, command: &mut Command) {
        Owner::inherit(self.owner(), command);
    }

    /// Collection failures cost attribution, never notification delivery.
    pub fn notification_lines(&self) -> Vec<String> {
        match self.collect() {
            Ok(lines) => lines,
            Err(error) => {
                progress::step(format_args!(
                    "warning: could not read session models for the Run notification: {error:#}"
                ));
                Vec::new()
            }
        }
    }

    /// Interpret and record one Session using its selected Harness and launch context.
    /// Preserve stream evidence before supervision can discard recovered state;
    /// a present completion report replaces it, even with no observed Models.
    /// Evidence writes are warnings and never change the Session outcome.
    pub fn run(
        &self,
        kind: &str,
        choice: &Choice,
        invocation: &mut dyn crate::process::Invocation,
        input: Input<'_>,
        log: &Path,
    ) -> Result<Ended> {
        let adapter = choice.harness.adapter();
        let command = invocation.command();
        let directory = match command.get_current_dir() {
            Some(dir) if dir.is_absolute() => dir.to_path_buf(),
            Some(dir) => std::env::current_dir()
                .context("could not resolve Session launch directory")?
                .join(dir),
            None => {
                std::env::current_dir().context("could not resolve Session launch directory")?
            }
        };
        let mut stream = adapter.interpretation(&directory, input.prompt);
        if let Some(home) = command
            .get_envs()
            .find_map(|(name, value)| (name == "HOME").then_some(value).flatten())
        {
            stream.set_home(Path::new(home));
        }
        stream = match input.policy {
            Policy::Ordinary => stream,
            Policy::Security => stream.for_security(choice.model.as_deref()),
            Policy::StreamOnly => stream.without_retained(),
        };
        command.env_remove(logs::trace::TRIAL_CONTEXT);
        if matches!(self.owner, Ok(None)) {
            // A Trial must not lend a caller's stale Command identity to
            // the Harness or to thirdshift processes it might start.
            self.inherit_command(command);
        }
        if let Some(dir) = log.parent() {
            fs::create_dir_all(dir)
                .with_context(|| format!("could not create {}", dir.display()))?;
        }
        let started = Instant::now();
        let context = self
            .trial_context
            .clone()
            .or_else(logs::trace_context)
            .or_else(|| self.owner().map(logs::trace::Context::of_owner))
            .unwrap_or_else(|| logs::trace::Context::of_session(log));
        let trace = Arc::new(Mutex::new(super::trace::Trace::create(
            log,
            kind,
            choice,
            input.prompt,
            context,
            command,
            self.command_log(),
        )?));
        let streaming_trace = Arc::clone(&trace);
        let (kind_owned, log_owned) = (kind.to_string(), log.to_path_buf());
        let choice_owned = choice.clone();
        let recording = self.clone();
        let private = input.private;
        let stop_at_limit = input.stop_at_limit || matches!(self.owner, Ok(None));
        let executed = process::streaming(
            adapter,
            invocation,
            input.stdin,
            input.deadline,
            stream,
            move |output, stream| {
                let followed = follow(
                    &kind_owned,
                    output,
                    &log_owned,
                    stream,
                    &streaming_trace,
                    StreamPolicy {
                        stop_at_limit,
                        private,
                    },
                );
                // The process owner joins this reader before returning, including
                // on interruption, when it deliberately suppresses state recovery.
                recording.keep(
                    &kind_owned,
                    &log_owned,
                    &choice_owned,
                    stream.observed_models(),
                );
                followed
            },
        );

        let executed = match executed {
            Ok(executed) => executed,
            Err(error) => {
                let finished = trace.lock().unwrap().finish(Some(&format!("{error:#}")));
                warn_unfinished_trace(finished);
                return Err(error);
            }
        };

        let completion = executed.state.finish(executed.execution);
        let finished = (|| {
            let mut trace = trace.lock().unwrap();
            if let Some(report) = &completion.report {
                for record in &report.retained_records {
                    trace.record(&record.raw, &record.events)?;
                }
                trace.reconcile(&report.events)?;
            }
            trace.finish(
                completion
                    .outcome
                    .as_ref()
                    .err()
                    .map(|error| error.to_string())
                    .as_deref(),
            )
        })();
        warn_unfinished_trace(finished);
        if let Some(report) = completion.report {
            self.keep(kind, log, choice, report.models.clone());
            if private {
                return completion.outcome;
            }
            for line in report.completion_progress {
                progress::step(format_args!("{kind}: {line}"));
            }
            for warning in report.warnings {
                progress::step(format_args!("{kind}: {warning}"));
            }
            let elapsed = minutes_and_seconds(started.elapsed());
            let summary = report
                .summary
                .map_or(String::new(), |summary| format!(": {summary}"));
            progress::step(format_args!(
                "{kind}: session ended after {elapsed}{summary}"
            ));
        }
        completion.outcome
    }

    /// Record only model metadata; failure costs attribution, never the session.
    fn keep(&self, kind: &str, log: &Path, choice: &Choice, observed: Vec<String>) {
        let record = SessionModels {
            command_log: self.command_log().map(Path::to_path_buf),
            kind: kind.to_string(),
            harness: choice.harness.name().to_string(),
            requested: choice.model.clone(),
            effort: choice.effort.clone(),
            observed,
        };
        let path = logs::session::original(log).with_extension("models.json");
        let kept = serde_json::to_vec(&record)
            .map_err(anyhow::Error::from)
            .and_then(|json| fs::write(&path, json).map_err(anyhow::Error::from));
        if let Err(error) = kept {
            progress::step(format_args!(
                "warning: could not record session models: {error:#}"
            ));
        }
    }

    /// Read this Command's evidence, including child Runs sharing its owner.
    /// Other commands in the same repository and folder are excluded.
    fn collect(&self) -> Result<Vec<String>> {
        let Some(command_log) = self.command_log() else {
            return Ok(Vec::new());
        };
        let Some(dir) = command_log.parent() else {
            return Ok(Vec::new());
        };
        let mut paths = Vec::new();
        for entry in fs::read_dir(dir)? {
            let path = entry?.path();
            if self.owner().is_some_and(|owner| owner.models_match(&path)) {
                paths.push(path);
            }
        }
        paths.sort();
        let mut lines = Vec::new();
        for path in paths {
            let read = fs::read(&path)
                .map_err(anyhow::Error::from)
                .and_then(|json| {
                    serde_json::from_slice::<SessionModels>(&json).map_err(anyhow::Error::from)
                })
                .with_context(|| format!("could not read {}", path.display()));
            let record = match read {
                Ok(record) => record,
                Err(error) => {
                    progress::step(format_args!(
                        "warning: could not read session models for the Run notification: {error:#}"
                    ));
                    continue;
                }
            };
            if record.command_log.as_deref() != Some(command_log) {
                continue;
            }
            let models = if record.observed.is_empty() {
                record.requested.map_or_else(
                    || "Harness default (actual model not reported)".to_string(),
                    |model| format!("{model} (requested; actual model not reported)"),
                )
            } else {
                let mut names = Vec::new();
                for model in record.observed {
                    if !names.contains(&model) {
                        names.push(model);
                    }
                }
                format!("{} (reported)", names.join(", "))
            };
            lines.push(format!(
                "- {}: {} · {models} · session effort: {}",
                record.kind,
                record.harness,
                record.effort.as_deref().unwrap_or("default effort")
            ));
        }
        Ok(lines)
    }
}

#[derive(Serialize, Deserialize)]
pub(crate) struct SessionModels {
    command_log: Option<PathBuf>,
    kind: String,
    pub(crate) harness: String,
    pub(crate) requested: Option<String>,
    pub(crate) effort: Option<String>,
    observed: Vec<String>,
}

impl SessionModels {
    /// Read only the exact Session's evidence belonging to this Command.
    pub(crate) fn for_session(log: &Path, command_log: &Path) -> Option<Self> {
        let path = logs::session::original(log).with_extension("models.json");
        let evidence: Self = serde_json::from_slice(&fs::read(path).ok()?).ok()?;
        (evidence.command_log.as_deref() == Some(command_log)).then_some(evidence)
    }
}

struct StreamPolicy {
    stop_at_limit: bool,
    private: bool,
}

/// Record every line in the Session trace. Ordinary sessions also
/// print progress; Eval sessions stop at plan limits while preserving evidence.
fn follow(
    kind: &str,
    mut stream: ChildStdout,
    log: &Path,
    progress: &mut Interpretation,
    trace: &Mutex<super::trace::Trace>,
    policy: StreamPolicy,
) -> Result<()> {
    let mut pending = Vec::new();
    let mut buffer = [0u8; 8192];
    loop {
        // The pipe may stay idle while Codex writes a native quota report.
        // Poll in this owned reader so process cleanup still joins it.
        let mut descriptor = libc::pollfd {
            fd: stream.as_raw_fd(),
            events: libc::POLLIN,
            revents: 0,
        };
        let ready = unsafe {
            libc::poll(
                &mut descriptor,
                1,
                if policy.stop_at_limit { 100 } else { -1 },
            )
        };
        if ready < 0 {
            let error = std::io::Error::last_os_error();
            if error.kind() == std::io::ErrorKind::Interrupted {
                continue;
            }
            return Err(error.into());
        }
        let mut eof = false;
        if ready > 0 {
            let count = stream
                .read(&mut buffer)
                .context("could not read the session stream")?;
            eof = count == 0;
            pending.extend_from_slice(&buffer[..count]);
            let complete = if eof {
                pending.len()
            } else {
                pending
                    .iter()
                    .rposition(|byte| *byte == b'\n')
                    .map_or(0, |index| index + 1)
            };
            for line in pending[..complete].split_inclusive(|byte| *byte == b'\n') {
                record_line(kind, line, log, progress, trace, policy.private)?;
            }
            pending.drain(..complete);
        }
        if policy.stop_at_limit {
            for raw in progress.limit_records() {
                record_line(kind, raw.as_bytes(), log, progress, trace, policy.private)?;
            }
            if progress.plan_limit_reached() {
                return super::limits::stop();
            }
        }
        if eof {
            return Ok(());
        }
    }
}

fn record_line(
    kind: &str,
    line: &[u8],
    log: &Path,
    progress: &mut Interpretation,
    trace: &Mutex<super::trace::Trace>,
    private: bool,
) -> Result<()> {
    let raw = String::from_utf8_lossy(line);
    let decoded = progress.decode(&raw);
    trace
        .lock()
        .unwrap()
        .record(line, &decoded.events)
        .with_context(|| format!("could not write Session trace {}", log.display()))?;
    if !private {
        for condensed in decoded.progress {
            progress::step(format_args!("{kind}: {condensed}"));
        }
    }
    Ok(())
}

/// A Session trace that cannot be finished costs evidence, never the session.
fn warn_unfinished_trace(finished: std::io::Result<()>) {
    if let Err(error) = finished {
        progress::step(format_args!(
            "warning: could not finish the session trace: {error:#}"
        ));
    }
}

/// `5m 32s`, or `8s` under a minute.
fn minutes_and_seconds(duration: Duration) -> String {
    let seconds = duration.as_secs();
    match seconds / 60 {
        0 => format!("{seconds}s"),
        minutes => format!("{minutes}m {}s", seconds % 60),
    }
}

#[cfg(test)]
mod tests;