use crate::prompt::Clock;
use std::cell::RefCell;
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use tempfile::TempDir;
pub(super) fn driver_target() -> &'static Path {
Path::new("lernie")
}
#[derive(Default)]
pub(super) struct FixedClock {
iso_calls: RefCell<u32>,
}
impl Clock for FixedClock {
fn now_iso8601(&self) -> String {
*self.iso_calls.borrow_mut() += 1;
format!("iso-{}", self.iso_calls.borrow())
}
fn now_compact(&self) -> String {
"ct".into()
}
}
pub(super) struct HarnessRoot {
pub(super) dir: TempDir,
}
impl HarnessRoot {
pub(super) fn new() -> Self {
let dir = TempDir::new().expect("tempdir");
std::fs::create_dir_all(dir.path().join(super::super::TOOLS_DIR)).expect("mkdir tools/");
Self { dir }
}
pub(super) fn path(&self) -> &Path {
self.dir.path()
}
pub(super) fn install(&self, name: &str, body: &str) -> PathBuf {
let path = self.dir.path().join(super::super::TOOLS_DIR).join(format!(
"{}{}",
super::super::EXTERNAL_PREFIX,
name
));
write_script(&path, body);
path
}
}
pub(super) fn write_script(path: &Path, body: &str) {
let mut script = String::from("#!/usr/bin/env bash\n");
script.push_str(body);
if !script.ends_with('\n') {
script.push('\n');
}
std::fs::write(path, script).expect("write script");
let mut perm = std::fs::metadata(path).expect("stat").permissions();
perm.set_mode(0o755);
std::fs::set_permissions(path, perm).expect("chmod");
}
pub(super) struct StepDir {
_root: TempDir,
pub(super) path: PathBuf,
pub(super) worktree: PathBuf,
}
pub(super) const AGENT_ID: &str = "convid";
impl StepDir {
pub(super) fn new() -> Self {
let root = TempDir::new().expect("step tempdir");
let path = root.path().join("steps").join(AGENT_ID).join("001");
std::fs::create_dir_all(&path).expect("mkdir step");
let worktree = crate::workspace::agent_worktree(root.path(), AGENT_ID);
std::fs::create_dir_all(&worktree).expect("mkdir worktree");
Self {
_root: root,
path,
worktree,
}
}
}