O4: parallel workers, locks, provider quota (#12)

This commit was merged in pull request #12.
This commit is contained in:
hermes committed 2026-11-12 23:52:22 +00:00
1 parent afafc41376
commit 06662a085f
10 files changed
+410 -43

No files matched your search

+18 -14
View File
@@ -4,7 +4,7 @@ use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
/// queued -> running -> gating -> (retry: running) -> done | failed | needs-human.
/// queued -> running -> gating -> (retry: running) -> done | failed | needs-human | requeued.
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Debug)]
#[serde(rename_all = "kebab-case")]
pub enum Status {
@@ -13,6 +13,8 @@ pub enum Status {
Gating,
Done,
Failed,
/// Provider quota used up: account paused, the task runs again later as a new run.
Requeued,
/// Gate still red after all retries: branch holds the best attempt, gate output in `ID.gate.log`.
NeedsHuman,
}
@@ -54,12 +56,19 @@ pub struct Run {
/// Sink result (PR URL, branch note) once published.
#[serde(default)]
pub published: Option<String>,
/// Unix time a full quota window resets (seen in this run's hx events).
#[serde(default)]
pub quota_until: Option<u64>,
}
pub fn now() -> u64 {
now_ms() / 1000
}
pub fn now_ms() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.as_secs())
.map_or(0, |d| d.as_millis() as u64)
}
pub fn runs_dir() -> PathBuf {
@@ -113,9 +122,10 @@ impl Run {
/// An active run whose orchestrator died (crash, kill -9, reboot) is marked failed/interrupted and
/// its hx process group is killed, so status never lies and no orphan keeps spending tokens.
/// ponytail: pid reuse can make a dead orch look alive; store a start time (or flock) in O4.
/// Liveness = the run's flock (held by the executing orch for the whole run, released by the kernel on
/// death), so pid reuse cannot fake a live run.
pub fn reconcile(&mut self) -> Result<()> {
if self.status.active() && !alive(self.orch_pid) {
if self.status.active() && crate::lock::run(&self.id)?.is_some() {
if let Some(p) = self.hx_pid {
kill_group(p);
}
@@ -128,11 +138,6 @@ impl Run {
}
}
pub fn alive(pid: u32) -> bool {
// SAFETY: signal 0 only checks existence.
unsafe { libc::kill(pid as i32, 0) == 0 || *libc::__errno_location() == libc::EPERM }
}
pub fn kill_group(pgid: u32) {
// SAFETY: plain syscall; hx is spawned with process_group(0) so pgid == its pid.
unsafe { libc::killpg(pgid as i32, libc::SIGKILL) };
@@ -165,6 +170,7 @@ mod tests {
turns: 0,
spent_tokens: 0,
published: None,
quota_until: None,
}
}
@@ -174,11 +180,9 @@ mod tests {
let _ = std::fs::remove_dir_all(&d);
// SAFETY: single test touching this var; other tests in this binary do not read it.
unsafe { std::env::set_var("ORCH_STATE_DIR", &d) };
let mut c = std::process::Command::new("true").spawn().unwrap();
let dead = c.id();
c.wait().unwrap();
sample("live", std::process::id()).save().unwrap();
sample("dead", dead).save().unwrap();
sample("live", 1).save().unwrap();
sample("dead", 1).save().unwrap();
let _held = crate::lock::run("live").unwrap().unwrap(); // the live run's orch holds its lock
let live = Run::load("live").unwrap();
assert_eq!(live.status, Status::Running);
let dead = Run::load("dead").unwrap();