Skip to main content

magi/
worker.rs

1//! Durable detached execution, independent of the scheduler and web lifetimes.
2//!
3//! A reservation is published before spawn, and the child must take its execution
4//! lease and acknowledge that reservation before dispatch. Reconciliation cancels
5//! an unacknowledged startup under the same store lock the child needs: a late
6//! child can never execute a cancelled operation. Process IDs are diagnostic only.
7//! Metadata publication closes its staging handle before atomic replacement.
8//! Windows replacement/delete-pending handoffs use the short, bounded lock-file
9//! retry policy; failed publication never releases an uncertain reservation.
10
11use std::fs::{File, OpenOptions};
12use std::io::{Read, Write};
13use std::path::{Path, PathBuf};
14use std::process::{Child, Command, Stdio};
15use std::sync::{LazyLock, Mutex, OnceLock};
16use std::time::{Duration, SystemTime, UNIX_EPOCH};
17
18use anyhow::{Context, Result, bail};
19use serde::{Deserialize, Serialize};
20use sha2::{Digest, Sha256};
21
22const STARTUP_SECONDS: u64 = 30;
23const RETAIN_FINISHED: usize = 256;
24
25/// Identity and executable selection inherited by descendants of a worker.
26/// Verification commands and standalone test fixtures remove these overrides:
27/// they test the checkout, rather than act on behalf of the enclosing worker.
28/// Keep runtime storage, seat metadata and process containment context intact.
29pub const INVOCATION_ENV: [&str; 4] = [
30    "MAGI_WORKER_ID",
31    "MAGI_WORKER_GENERATION",
32    "MAGI_WORKER_HOME",
33    "MAGI_WORKER_EXE",
34];
35
36/// Durable execution state. Starting reservations count as occupied slots.
37#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
38#[serde(rename_all = "snake_case")]
39pub enum Status {
40    /// Published but not yet acknowledged by the detached child.
41    Starting,
42    /// The child acknowledged startup and owns the execution lease.
43    Running,
44    /// The handler returned successfully.
45    Finished,
46    /// Startup, handler execution, or the owning process failed.
47    Failed,
48}
49
50/// An explicit owner control; upgrades do not send these to task workers.
51#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
52#[serde(rename_all = "snake_case")]
53pub enum Control {
54    /// Park at the handler's existing safe cancellation boundary.
55    Park,
56    /// Stop the operation through its handler's cancellation path.
57    Stop,
58}
59
60#[derive(Debug)]
61struct NotStarted(anyhow::Error);
62
63impl std::fmt::Display for NotStarted {
64    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65        write!(formatter, "{:#}", self.0)
66    }
67}
68
69impl std::error::Error for NotStarted {
70    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
71        Some(self.0.as_ref())
72    }
73}
74
75fn not_started_error(error: impl Into<anyhow::Error>) -> anyhow::Error {
76    anyhow::Error::new(NotStarted(error.into()))
77}
78
79/// Whether this spawn invocation provably did not execute its handler. This
80/// covers preparation failures and a failed OS spawn whose fresh reservation
81/// was exclusively leased and durably fenced before this error was returned.
82/// Only a fresh operation key makes this proof sufficient to restore a caller's
83/// publication. Unknown startup, existing work, or a failed fence is never tagged.
84pub fn not_started(error: &anyhow::Error) -> bool {
85    error.downcast_ref::<NotStarted>().is_some()
86}
87
88/// The wait timed out, but this reservation is still eligible to start.
89#[derive(Debug, thiserror::Error)]
90#[error(
91    "worker {id} startup is unacknowledged; its reservation remains occupied; inspect or retry the same operation key"
92)]
93pub(crate) struct StartupUnacknowledged {
94    pub(crate) id: String,
95}
96
97/// A durable operation record. Its payload belongs to the started generation.
98#[derive(Clone, Debug, Serialize, Deserialize)]
99pub struct Job {
100    /// Unique invocation identity.
101    pub id: String,
102    /// Durable total reservation order, independent of wall clock resolution.
103    #[serde(default)]
104    pub sequence: u64,
105    /// Dispatch handler, such as task, chat, scheduler, or service.
106    pub kind: String,
107    /// Exclusive logical operation identity, independent of executable generation.
108    pub key: String,
109    /// Handler-owned request.
110    pub payload: serde_json::Value,
111    /// SHA-256 identity of the immutable executable.
112    pub generation: String,
113    /// Version reported by the started executable, unknown before acknowledgement.
114    #[serde(default)]
115    pub version: Option<String>,
116    /// Last durable execution state.
117    pub status: Status,
118    /// Successful completion note or failure diagnostic.
119    pub result: Option<String>,
120    /// Diagnostic process ID, never used as an execution witness.
121    pub pid: Option<u32>,
122    /// Reservation publication time, seconds since the Unix epoch.
123    pub created_at: u64,
124    /// Terminal state publication time.
125    pub finished_at: Option<u64>,
126}
127
128/// An immutable, content-addressed executable.
129#[derive(Clone, Debug)]
130pub struct Generation {
131    /// SHA-256 digest of the executable bytes.
132    pub id: String,
133    /// Absolute path to the retained executable.
134    pub executable: PathBuf,
135}
136
137/// OS-held lock. Closing it, including on a crash, releases ownership.
138#[derive(Debug)]
139pub struct Lease {
140    _file: File,
141}
142
143impl Drop for Lease {
144    fn drop(&mut self) {
145        // flock's lock belongs to the open-file description; explicitly unlock
146        // so a temporarily inherited descriptor cannot prolong ownership.
147        #[cfg(unix)]
148        {
149            use std::os::fd::AsRawFd;
150            unsafe { libc::flock(self._file.as_raw_fd(), libc::LOCK_UN) };
151        }
152    }
153}
154
155fn root(home: &Path) -> PathBuf {
156    home.join("workers")
157}
158fn now() -> u64 {
159    SystemTime::now()
160        .duration_since(UNIX_EPOCH)
161        .unwrap_or_default()
162        .as_secs()
163}
164fn nonce() -> String {
165    crate::rng::SplitMix64::new(crate::rng::entropy()).uuid_v4()
166}
167fn valid_id(id: &str) -> bool {
168    !id.is_empty() && id.bytes().all(|b| b.is_ascii_hexdigit() || b == b'-')
169}
170fn job_dir(home: &Path, id: &str) -> Result<PathBuf> {
171    if !valid_id(id) {
172        bail!("invalid worker id");
173    }
174    Ok(root(home).join("jobs").join(id))
175}
176
177fn open_lock(path: &Path) -> Result<File> {
178    if let Some(parent) = path.parent() {
179        std::fs::create_dir_all(parent)
180            .with_context(|| format!("create worker lock directory {}", parent.display()))?;
181    }
182    OpenOptions::new()
183        .create(true)
184        .truncate(false)
185        .read(true)
186        .write(true)
187        .open(path)
188        .with_context(|| format!("open worker lock {}", path.display()))
189}
190
191fn try_lock(path: &Path) -> Result<Option<Lease>> {
192    let file = open_lock(path)?;
193    match crate::pr_head::try_lock_file(&file) {
194        Ok(()) => Ok(Some(Lease { _file: file })),
195        Err(e)
196            if e.kind() == std::io::ErrorKind::WouldBlock
197                || matches!(e.raw_os_error(), Some(33)) =>
198        {
199            Ok(None)
200        }
201        Err(e) => Err(e).with_context(|| format!("acquire worker lock {}", path.display())),
202    }
203}
204
205#[doc(hidden)]
206pub fn execution_lease_for_test(path: &Path) -> Lease {
207    try_lock(path)
208        .expect("execution lease")
209        .expect("free execution lease")
210}
211
212/// Try to own a named cross-process singleton, without PID-based stale handling.
213/// Names are hashed, so callers may use logical keys containing paths or colons.
214pub fn try_lease(home: &Path, name: &str) -> Result<Option<Lease>> {
215    try_lock(
216        &root(home)
217            .join("leases")
218            .join(format!("{}.lock", digest(name.as_bytes()))),
219    )
220}
221
222fn store_lock(home: &Path) -> Result<Lease> {
223    let deadline = std::time::Instant::now() + Duration::from_secs(10);
224    loop {
225        if let Some(lease) = try_lock(&root(home).join("store.lock"))? {
226            return Ok(lease);
227        }
228        if std::time::Instant::now() >= deadline {
229            bail!("worker store is busy; retry later");
230        }
231        std::thread::sleep(Duration::from_millis(5));
232    }
233}
234
235#[cfg(unix)]
236fn sync_dir(path: &Path) -> Result<()> {
237    File::open(path)
238        .with_context(|| format!("open worker directory for sync {}", path.display()))?
239        .sync_all()
240        .with_context(|| format!("sync worker directory {}", path.display()))?;
241    Ok(())
242}
243
244#[cfg(not(unix))]
245fn sync_dir(_path: &Path) -> Result<()> {
246    Ok(())
247}
248
249fn save(path: &Path, value: &impl Serialize) -> Result<()> {
250    let parent = path.parent().context("worker record has no parent")?;
251    std::fs::create_dir_all(parent)
252        .with_context(|| format!("create worker record directory {}", parent.display()))?;
253    let temp = parent.join(format!(".pending-{}", nonce()));
254    let result = (|| -> Result<()> {
255        let mut file = OpenOptions::new()
256            .create_new(true)
257            .write(true)
258            .open(&temp)
259            .with_context(|| format!("create staged worker record {}", temp.display()))?;
260        serde_json::to_writer(&mut file, value)
261            .with_context(|| format!("serialize worker record {}", path.display()))?;
262        file.flush()
263            .with_context(|| format!("flush staged worker record {}", temp.display()))?;
264        file.sync_all()
265            .with_context(|| format!("sync staged worker record {}", temp.display()))?;
266        // Publication must not retain our own writable staging handle. Windows
267        // readers may briefly deny deletion; only the atomic handoff retries.
268        drop(file);
269        replace(&temp, path).with_context(|| {
270            format!(
271                "publish worker record {} -> {}",
272                temp.display(),
273                path.display()
274            )
275        })?;
276        sync_dir(parent)
277    })();
278    if result.is_err() {
279        let _ = std::fs::remove_file(&temp);
280    }
281    result
282}
283
284#[cfg(not(windows))]
285fn replace(from: &Path, to: &Path) -> Result<()> {
286    Ok(std::fs::rename(from, to)?)
287}
288
289#[cfg(windows)]
290fn replace(from: &Path, to: &Path) -> Result<()> {
291    // std uses FileRenameInfoEx/POSIX semantics where supported, unlike
292    // MoveFileExW: a FILE_SHARE_DELETE reader may retain the previous record
293    // while the replacement becomes visible atomically. Readers that briefly
294    // deny deletion still get only the existing short Windows handoff budget.
295    crate::lock_file::Retry::new(None).run(|| std::fs::rename(from, to))?;
296    Ok(())
297}
298
299// Atomic replacement can leave the old Windows name briefly delete-pending.
300// Keep that handoff bounded; malformed records and permanent IO errors propagate.
301fn open_record(path: &Path) -> std::io::Result<File> {
302    #[cfg(windows)]
303    {
304        crate::lock_file::Retry::new(None).run(|| File::open(path))
305    }
306    #[cfg(not(windows))]
307    {
308        File::open(path)
309    }
310}
311
312/// Load a specific invocation, including a retained terminal result.
313pub fn get(home: &Path, id: &str) -> Result<Job> {
314    let path = job_dir(home, id)?.join("job.json");
315    let file = match open_record(&path) {
316        Ok(file) => file,
317        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
318            let retained = root(home).join("results").join(id).join("job.json");
319            open_record(&retained)
320                .with_context(|| format!("open retained worker record {}", retained.display()))?
321        }
322        Err(error) => {
323            return Err(error).with_context(|| format!("open worker record {}", path.display()));
324        }
325    };
326    serde_json::from_reader(file).with_context(|| format!("decode worker record {id}"))
327}
328
329/// Identify this detached invocation. Helpers inherit its identity, while a
330/// newly spawned worker receives its own id before exec; no global environment
331/// mutation is required inside the asynchronous handler runtime.
332pub fn current_job(home: &Path) -> Result<Option<Job>> {
333    match std::env::var("MAGI_WORKER_ID") {
334        Ok(id) => Ok(Some(get(home, &id)?)),
335        Err(std::env::VarError::NotPresent) => Ok(None),
336        Err(error) => Err(error.into()),
337    }
338}
339
340fn next_sequence(home: &Path) -> Result<u64> {
341    let path = root(home).join("sequence.json");
342    let previous: u64 = match open_record(&path) {
343        Ok(file) => serde_json::from_reader(file)
344            .with_context(|| format!("decode worker sequence {}", path.display()))?,
345        Err(error) if error.kind() == std::io::ErrorKind::NotFound => 0,
346        Err(error) => {
347            return Err(error).with_context(|| format!("open worker sequence {}", path.display()));
348        }
349    };
350    let sequence = previous
351        .checked_add(1)
352        .context("worker reservation sequence exhausted")?;
353    save(&path, &sequence)?;
354    Ok(sequence)
355}
356
357fn records(home: &Path) -> Result<Vec<Job>> {
358    let directory = root(home).join("jobs");
359    std::fs::create_dir_all(&directory)?;
360    let mut jobs = Vec::new();
361    let mut retired = false;
362    for entry in std::fs::read_dir(directory)? {
363        let entry = entry?;
364        if !entry.file_type()?.is_dir() {
365            continue;
366        }
367        let name = entry.file_name();
368        let Some(id) = name.to_str() else {
369            continue;
370        };
371        if id.starts_with(".new-") {
372            // Reservations are staged only while holding the store lock. Any
373            // staging directory visible to the next owner was left by a crash.
374            std::fs::remove_dir_all(entry.path())?;
375            continue;
376        }
377        if id.starts_with('.') {
378            continue;
379        }
380        for pending in std::fs::read_dir(entry.path())? {
381            let pending = pending?;
382            if pending.file_type()?.is_file()
383                && pending
384                    .file_name()
385                    .to_string_lossy()
386                    .starts_with(".pending-")
387            {
388                std::fs::remove_file(pending.path())?;
389            }
390        }
391        let job = get(home, id).with_context(|| format!("read worker {id}"))?;
392        if matches!(job.status, Status::Starting | Status::Running) {
393            jobs.push(job);
394        } else {
395            retired |= retire(home, &job);
396        }
397    }
398    if retired {
399        if let Err(error) = prune_results(home) {
400            tracing::warn!(%error, "worker result retention deferred");
401        }
402    }
403    Ok(jobs)
404}
405
406fn terminal(home: &Path, job: &mut Job, status: Status, result: String) -> Result<()> {
407    job.status = status;
408    job.result = Some(result);
409    job.finished_at = Some(now());
410    save(&job_dir(home, &job.id)?.join("job.json"), job)
411        .with_context(|| format!("publish worker {} terminal status {status:?}", job.id))?;
412    // The result is authoritative now. Keep the directory in place while this
413    // process owns live.lock and its stdout/stderr handles. Another store owner
414    // retires it opportunistically after execution ownership is released.
415    clear_control_locked(home, &job.key)
416}
417
418fn retire(home: &Path, job: &Job) -> bool {
419    let attempt = || -> Result<bool> {
420        let dir = job_dir(home, &job.id)?;
421        let Some(lease) = try_lock(&dir.join("live.lock"))? else {
422            return Ok(false);
423        };
424        drop(lease);
425        archive(home, job)?;
426        Ok(true)
427    };
428    // Windows can still deny the directory rename in the short interval after
429    // lease release but before process stdout/stderr are closed. Retry on the
430    // next store read; archival failure must never turn completion into failure
431    // or terminate an otherwise healthy scheduler.
432    match attempt() {
433        Ok(retired) => retired,
434        Err(error) => {
435            tracing::warn!(
436                worker = %job.id,
437                error = %format_args!("{error:#}"),
438                "worker result retirement deferred"
439            );
440            false
441        }
442    }
443}
444
445fn archive(home: &Path, job: &Job) -> Result<()> {
446    let results = root(home).join("results");
447    std::fs::create_dir_all(&results)
448        .with_context(|| format!("create worker results directory {}", results.display()))?;
449    let from = job_dir(home, &job.id)?;
450    let to = results.join(&job.id);
451    std::fs::rename(&from, &to).with_context(|| {
452        format!(
453            "archive worker result {} -> {}",
454            from.display(),
455            to.display()
456        )
457    })?;
458    sync_dir(&root(home).join("jobs"))?;
459    sync_dir(&results)
460}
461
462// Caller owns the store lock. Acquiring the execution lease and then marking a
463// dead startup terminal fences a late child: it must recheck state under store.
464fn reconcile(home: &Path, jobs: &mut [Job]) -> Result<()> {
465    let mut retired = false;
466    for job in jobs {
467        if !matches!(job.status, Status::Starting | Status::Running) {
468            continue;
469        }
470        let lease = try_lock(&job_dir(home, &job.id)?.join("live.lock"))?;
471        if lease.is_none() {
472            continue;
473        }
474        if job.status == Status::Starting && now().saturating_sub(job.created_at) < STARTUP_SECONDS
475        {
476            continue;
477        }
478        let reason = if job.status == Status::Starting {
479            "worker startup reservation expired before acknowledgement"
480        } else {
481            "worker exited without recording a result"
482        };
483        terminal(home, job, Status::Failed, reason.to_owned())?;
484        drop(lease);
485        retired |= retire(home, job);
486    }
487    if retired {
488        if let Err(error) = prune_results(home) {
489            tracing::warn!(%error, "worker result retention deferred");
490        }
491    }
492    Ok(())
493}
494
495/// List live operations and unresolved startups. A missing lease only frees a
496/// startup after its deadline and an atomic cancellation fence; PID is irrelevant.
497pub fn active(home: &Path) -> Result<Vec<Job>> {
498    let _store = store_lock(home)?;
499    let mut jobs = records(home)?;
500    reconcile(home, &mut jobs)?;
501    Ok(jobs
502        .into_iter()
503        .filter(|job| matches!(job.status, Status::Starting | Status::Running))
504        .collect())
505}
506
507/// Observe execution without creating, repairing, archiving, or locking the
508/// writable store. Sandboxed diagnostics need only file reads and a read-only
509/// handle for the OS lease probe. Starting reservations remain protected until
510/// an authorized writer fences them; a missing running lease is unknown.
511pub fn observe_active(home: &Path) -> Result<Vec<Job>> {
512    let entries = match std::fs::read_dir(root(home).join("jobs")) {
513        Ok(entries) => entries,
514        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
515        Err(error) => return Err(error.into()),
516    };
517    let mut active = Vec::new();
518    for entry in entries {
519        let entry = entry?;
520        if !entry.file_type()?.is_dir() {
521            continue;
522        }
523        let name = entry.file_name();
524        let id = name.to_str().context("invalid worker reservation name")?;
525        if id.starts_with('.') {
526            continue;
527        }
528        let job = get(home, id)?;
529        if observe_job_active(home, &job)? {
530            active.push(job);
531        }
532    }
533    Ok(active)
534}
535
536/// Passive lease observation for a single reservation already read by the caller.
537pub(crate) fn observe_job_active(home: &Path, job: &Job) -> Result<bool> {
538    match job.status {
539        Status::Starting => Ok(true),
540        Status::Running => {
541            let file = match File::open(job_dir(home, &job.id)?.join("live.lock")) {
542                Ok(file) => file,
543                Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(true),
544                Err(error) => return Err(error.into()),
545            };
546            match crate::pr_head::try_lock_file(&file) {
547                Ok(()) => {
548                    drop(Lease { _file: file });
549                    Ok(false)
550                }
551                Err(error)
552                    if error.kind() == std::io::ErrorKind::WouldBlock
553                        || cfg!(windows) && error.raw_os_error() == Some(33) =>
554                {
555                    Ok(true)
556                }
557                Err(error) => Err(error.into()),
558            }
559        }
560        Status::Finished | Status::Failed => Ok(false),
561    }
562}
563
564fn control_path(home: &Path, key: &str) -> PathBuf {
565    root(home)
566        .join("controls")
567        .join(format!("{}.json", digest(key.as_bytes())))
568}
569
570/// Persist a stop or park request; the operation's handler consumes it safely.
571pub fn request(home: &Path, key: &str, control: Control) -> Result<()> {
572    let _store = store_lock(home)?;
573    save(&control_path(home, key), &control)
574}
575
576/// Read the latest durable owner request for a logical operation.
577pub fn control(home: &Path, key: &str) -> Result<Option<Control>> {
578    let path = control_path(home, key);
579    match open_record(&path) {
580        Ok(file) => serde_json::from_reader(file)
581            .map(Some)
582            .with_context(|| format!("decode worker control {}", path.display())),
583        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
584        Err(e) => Err(e).with_context(|| format!("open worker control {}", path.display())),
585    }
586}
587
588fn clear_control_locked(home: &Path, key: &str) -> Result<()> {
589    let path = control_path(home, key);
590    match std::fs::remove_file(&path) {
591        Ok(()) => sync_dir(&root(home).join("controls")),
592        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
593        Err(e) => Err(e).with_context(|| format!("remove worker control {}", path.display())),
594    }
595}
596
597/// Clear controls at an explicitly owned new singleton invocation boundary.
598pub fn clear_control(home: &Path, key: &str) -> Result<()> {
599    let _store = store_lock(home)?;
600    clear_control_locked(home, key)
601}
602
603/// Resolve a retained executable strictly by its content identity.
604pub fn executable(home: &Path, generation: &str) -> Result<PathBuf> {
605    if generation.len() != 64 || !generation.bytes().all(|b| b.is_ascii_hexdigit()) {
606        bail!("invalid worker generation");
607    }
608    let path = root(home)
609        .join("generations")
610        .join(generation)
611        .join(if cfg!(windows) { "magi.exe" } else { "magi" });
612    if !path.is_file() {
613        bail!("worker generation {generation} is unavailable");
614    }
615    Ok(path)
616}
617
618/// Resolve helper invocations to this worker's started executable, not a newly
619/// installed binary. Outside a worker, pin the caller's current executable.
620pub fn helper_executable(home: &Path) -> Result<PathBuf> {
621    if let Some(generation) = std::env::var_os("MAGI_WORKER_GENERATION") {
622        return executable(
623            home,
624            generation
625                .to_str()
626                .context("invalid generation environment")?,
627        );
628    }
629    Ok(pin_current(home)?.executable)
630}
631
632/// Remember the installed executable pathname before an updater replaces it.
633/// A web process calls this at startup so Linux's deleted-executable marker or
634/// Windows's backup rename cannot redirect successor workers to the old binary.
635pub fn remember_current_executable() -> Result<()> {
636    if INSTALLED_EXECUTABLE.get().is_some() {
637        return Ok(());
638    }
639    let path = std::env::current_exe()?.canonicalize()?;
640    let _ = INSTALLED_EXECUTABLE.set(path);
641    Ok(())
642}
643
644static INSTALLED_EXECUTABLE: OnceLock<PathBuf> = OnceLock::new();
645
646/// Copy and SHA-256 address the executable. MAGI_WORKER_EXE is an explicit
647/// embedding/test override; it is never an in-process execution fallback.
648pub fn pin_current(home: &Path) -> Result<Generation> {
649    let source = match std::env::var_os("MAGI_WORKER_EXE") {
650        Some(path) => PathBuf::from(path),
651        None => match INSTALLED_EXECUTABLE.get() {
652            Some(path) => path.clone(),
653            None => std::env::current_exe()?,
654        },
655    };
656    let source = source.canonicalize().context("resolve worker executable")?;
657    // A worker already runs the immutable copy. Do not copy/hash a large binary
658    // again for every finite maintenance invocation in the same generation.
659    if let Some(directory) = source.parent() {
660        if directory.parent() == Some(root(home).join("generations").as_path()) {
661            if let Some(id) = directory.file_name().and_then(|name| name.to_str()) {
662                if id.len() == 64
663                    && id.bytes().all(|b| b.is_ascii_hexdigit())
664                    && executable(home, id)?.canonicalize()? == source
665                {
666                    return Ok(Generation {
667                        id: id.to_owned(),
668                        executable: source,
669                    });
670                }
671            }
672        }
673    }
674    let stamp = source_stamp(&source)?;
675    let mut cache = PIN_CACHE
676        .lock()
677        .map_err(|_| anyhow::anyhow!("worker generation cache poisoned"))?;
678    if let Some(entry) = cache.iter().find(|entry| {
679        entry.home == home
680            && entry.source == source
681            && entry.stamp == stamp
682            && entry.generation.executable.is_file()
683    }) {
684        return Ok(entry.generation.clone());
685    }
686    let generation = pin_source(home, &source)?;
687    if source_stamp(&source)? != stamp {
688        bail!("worker executable changed while being pinned; retry");
689    }
690    if cache.len() >= 8 {
691        cache.remove(0);
692    }
693    cache.push(CachedGeneration {
694        home: home.to_path_buf(),
695        source,
696        stamp,
697        generation: generation.clone(),
698    });
699    Ok(generation)
700}
701
702type SourceStamp = (u64, Option<SystemTime>, Option<SystemTime>, u64, u64);
703struct CachedGeneration {
704    home: PathBuf,
705    source: PathBuf,
706    stamp: SourceStamp,
707    generation: Generation,
708}
709static PIN_CACHE: LazyLock<Mutex<Vec<CachedGeneration>>> = LazyLock::new(|| Mutex::new(Vec::new()));
710
711fn source_stamp(path: &Path) -> Result<SourceStamp> {
712    let metadata = std::fs::metadata(path)?;
713    #[cfg(unix)]
714    let identity = {
715        use std::os::unix::fs::MetadataExt;
716        (metadata.dev(), metadata.ino())
717    };
718    #[cfg(not(unix))]
719    let identity = (0, 0);
720    Ok((
721        metadata.len(),
722        metadata.modified().ok(),
723        metadata.created().ok(),
724        identity.0,
725        identity.1,
726    ))
727}
728
729fn pin_source(home: &Path, source: &Path) -> Result<Generation> {
730    let generations = root(home).join("generations");
731    std::fs::create_dir_all(&generations)?;
732    let staging = generations.join(format!(".new-{}", nonce()));
733    let mut staging_lease = {
734        let _store = store_lock(home)?;
735        // Only unpublished copies are reclaimed, never executable generations.
736        for entry in std::fs::read_dir(&generations)? {
737            let entry = entry?;
738            if entry.file_type()?.is_dir()
739                && entry.file_name().to_string_lossy().starts_with(".new-")
740            {
741                if let Some(lease) = try_lock(&entry.path().join("pin.lock"))? {
742                    drop(lease);
743                    remove_staging(&entry.path())?;
744                }
745            }
746        }
747        std::fs::create_dir(&staging)?;
748        Some(try_lock(&staging.join("pin.lock"))?.context("generation staging lease unavailable")?)
749    };
750    let temp = staging.join(if cfg!(windows) { "magi.exe" } else { "magi" });
751    let result = (|| -> Result<Generation> {
752        let mut input = File::open(source)?;
753        let mut output = OpenOptions::new()
754            .create_new(true)
755            .write(true)
756            .open(&temp)?;
757        let mut hash = Sha256::new();
758        let mut buffer = [0u8; 65536];
759        loop {
760            let n = input.read(&mut buffer)?;
761            if n == 0 {
762                break;
763            }
764            hash.update(&buffer[..n]);
765            output.write_all(&buffer[..n])?;
766        }
767        let id = hex_digest(hash);
768        let mut permissions = input.metadata()?.permissions();
769        permissions.set_readonly(true);
770        output.set_permissions(permissions)?;
771        output.sync_all()?;
772        drop(output);
773        sync_dir(&staging)?;
774        let target = generations.join(&id);
775        // The store lock serializes publication with retention and other pins.
776        let _store = store_lock(home)?;
777        drop(staging_lease.take());
778        std::fs::remove_file(staging.join("pin.lock"))?;
779        if target.exists() {
780            let existing = target.join(temp.file_name().context("binary filename")?);
781            if hash_file(&existing)? != id {
782                bail!("immutable worker generation is corrupt: {id}");
783            }
784        } else {
785            std::fs::rename(&staging, &target)?;
786            sync_dir(&generations)?;
787        }
788        Ok(Generation {
789            executable: target.join(temp.file_name().context("binary filename")?),
790            id,
791        })
792    })();
793    let _store = store_lock(home)?;
794    drop(staging_lease);
795    if staging.exists() {
796        remove_staging(&staging)?;
797    }
798    result
799}
800
801fn remove_staging(staging: &Path) -> Result<()> {
802    #[cfg(windows)]
803    {
804        use std::os::windows::{ffi::OsStrExt, fs::MetadataExt};
805        #[link(name = "kernel32")]
806        unsafe extern "system" {
807            fn SetFileAttributesW(path: *const u16, attributes: u32) -> i32;
808        }
809        const READONLY: u32 = 0x1;
810        for entry in std::fs::read_dir(staging)? {
811            let entry = entry?;
812            let attributes = entry.metadata()?.file_attributes();
813            if attributes & READONLY != 0 {
814                let path: Vec<u16> = entry.path().as_os_str().encode_wide().chain([0]).collect();
815                // SAFETY: path is a live, NUL-terminated UTF-16 buffer. Clear
816                // only the Windows readonly attribute, preserving other bits.
817                if unsafe { SetFileAttributesW(path.as_ptr(), attributes & !READONLY) } == 0 {
818                    return Err(std::io::Error::last_os_error())
819                        .context("clear staging readonly attribute");
820                }
821            }
822        }
823    }
824    Ok(std::fs::remove_dir_all(staging)?)
825}
826
827fn hash_file(path: &Path) -> Result<String> {
828    let mut file = File::open(path)?;
829    let mut hash = Sha256::new();
830    let mut buffer = [0u8; 65536];
831    loop {
832        let n = file.read(&mut buffer)?;
833        if n == 0 {
834            break;
835        }
836        hash.update(&buffer[..n]);
837    }
838    Ok(hex_digest(hash))
839}
840
841fn reserve(
842    home: &Path,
843    generation: &Generation,
844    kind: &str,
845    key: &str,
846    payload: serde_json::Value,
847) -> Result<(Job, bool)> {
848    if kind.is_empty() || key.is_empty() {
849        bail!("worker kind and operation key must be nonempty");
850    }
851    let _store = store_lock(home)?;
852    let mut jobs = records(home)?;
853    reconcile(home, &mut jobs)?;
854    if let Some(job) = jobs
855        .iter()
856        .find(|job| job.key == key && matches!(job.status, Status::Starting | Status::Running))
857    {
858        if job.kind != kind || job.payload != payload {
859            bail!("worker operation key {key} is already reserved for a different request");
860        }
861        return Ok((job.clone(), false));
862    }
863    let id = nonce();
864    let job = Job {
865        id: id.clone(),
866        sequence: next_sequence(home)?,
867        kind: kind.to_owned(),
868        key: key.to_owned(),
869        payload,
870        generation: generation.id.clone(),
871        version: None,
872        status: Status::Starting,
873        result: None,
874        pid: None,
875        created_at: now(),
876        finished_at: None,
877    };
878    let jobs_dir = root(home).join("jobs");
879    let staging = jobs_dir.join(format!(".new-{id}"));
880    std::fs::create_dir(&staging)
881        .with_context(|| format!("create worker reservation staging {}", staging.display()))?;
882    save(&staging.join("job.json"), &job)?;
883    clear_control_locked(home, key)?;
884    std::fs::rename(&staging, job_dir(home, &id)?)
885        .with_context(|| format!("publish worker {id} reservation from {}", staging.display()))?;
886    sync_dir(&jobs_dir)?;
887    Ok((job, true))
888}
889
890pub(crate) fn running_fixture(
891    home: &Path,
892    key: &str,
893    payload: serde_json::Value,
894) -> Result<(Job, Lease)> {
895    let _store = store_lock(home)?;
896    let id = nonce();
897    let dir = job_dir(home, &id)?;
898    std::fs::create_dir_all(&dir)?;
899    let result = (|| {
900        let lease = try_lock(&dir.join("live.lock"))?.context("fixture lease unavailable")?;
901        let job = Job {
902            id,
903            sequence: next_sequence(home)?,
904            kind: "task".into(),
905            key: key.into(),
906            payload,
907            generation: "a".repeat(64),
908            version: None,
909            status: Status::Running,
910            result: None,
911            pid: Some(std::process::id()),
912            created_at: now(),
913            finished_at: None,
914        };
915        save(&dir.join("job.json"), &job)?;
916        Ok((job, lease))
917    })();
918    if result.is_err() {
919        let _ = std::fs::remove_dir_all(&dir);
920    }
921    result
922}
923
924fn prune_results(home: &Path) -> Result<()> {
925    let results = root(home).join("results");
926    std::fs::create_dir_all(&results)?;
927    let mut entries = Vec::new();
928    for entry in std::fs::read_dir(&results)? {
929        let entry = entry?;
930        if entry.file_type()?.is_dir() {
931            // Directory mtime changes when the terminal job.json is published.
932            // Retention needs no deserialization of old handler/config payloads.
933            entries.push((entry.metadata()?.modified()?, entry.path()));
934        }
935    }
936    entries.sort_unstable_by_key(|(modified, _)| std::cmp::Reverse(*modified));
937    for (_, dir) in entries.into_iter().skip(RETAIN_FINISHED) {
938        if let Some(lease) = try_lock(&dir.join("live.lock"))? {
939            drop(lease);
940            std::fs::remove_dir_all(dir)?;
941        }
942    }
943    sync_dir(&results)
944}
945
946fn detached_command(home: &Path, job: &Job, generation: &Generation) -> Result<Command> {
947    // Open before exec so argument parsing, startup panics, and task progress
948    // remain diagnosable even before the child can publish its acknowledgement.
949    // Archiving/pruning the reservation also archives/prunes its log.
950    let log_path = job_dir(home, &job.id)?.join("worker.log");
951    let log = OpenOptions::new()
952        .create(true)
953        .append(true)
954        .open(&log_path)
955        .with_context(|| format!("open worker startup log {}", log_path.display()))?;
956    let stderr = log
957        .try_clone()
958        .with_context(|| format!("clone worker startup log {}", log_path.display()))?;
959    let mut command = Command::new(&generation.executable);
960    command
961        .arg("__worker")
962        .arg("--home")
963        .arg(home)
964        .arg(&job.id)
965        .env("MAGI_HOME", home)
966        .env("MAGI_WORKER_HOME", home)
967        .env("MAGI_WORKER_ID", &job.id)
968        .env("MAGI_WORKER_GENERATION", &generation.id)
969        .env("MAGI_WORKER_EXE", &generation.executable)
970        .env_remove("MAGI_RUN")
971        .env_remove("MAGI_NODE")
972        .env_remove("MAGI_SEAT")
973        .stdin(Stdio::null())
974        .stdout(Stdio::from(log))
975        .stderr(Stdio::from(stderr));
976    crate::chat_control::bridge::clear_std_environment(&mut command);
977    let mut paths = vec![
978        generation
979            .executable
980            .parent()
981            .context("generation directory")?
982            .to_path_buf(),
983    ];
984    if let Some(path) = std::env::var_os("PATH") {
985        paths.extend(std::env::split_paths(&path));
986    }
987    command.env("PATH", std::env::join_paths(paths)?);
988    #[cfg(unix)]
989    {
990        use std::os::unix::process::CommandExt;
991        unsafe {
992            command.pre_exec(|| {
993                if libc::setsid() < 0 {
994                    return Err(std::io::Error::last_os_error());
995                }
996                // A scheduler's Ctrl-C handling must not be inherited as an
997                // ignored shutdown signal by its independent worker session.
998                libc::signal(libc::SIGINT, libc::SIG_DFL);
999                libc::signal(libc::SIGTERM, libc::SIG_DFL);
1000                Ok(())
1001            });
1002        }
1003    }
1004    #[cfg(windows)]
1005    {
1006        use std::os::windows::process::CommandExt;
1007        detached_standard_handles().context("exclude caller pipes from detached worker")?;
1008        command.creation_flags(
1009            crate::chat_watch::windows::detached_flags()
1010                .context("prepare independent Windows worker")?,
1011        );
1012    }
1013    Ok(command)
1014}
1015
1016#[cfg(windows)]
1017fn detached_standard_handles() -> std::io::Result<()> {
1018    use std::ffi::c_void;
1019    #[link(name = "kernel32")]
1020    unsafe extern "system" {
1021        fn GetStdHandle(kind: u32) -> *mut c_void;
1022        fn SetHandleInformation(handle: *mut c_void, mask: u32, flags: u32) -> i32;
1023    }
1024    // Explicit Stdio entries are duplicated by Command for the child. The
1025    // caller's original inherited handles must not also leak into a detached
1026    // process: that keeps an external caller's stdout pipe open after exit.
1027    // Leave them non-inheritable; subsequent ordinary Command launches still
1028    // inherit their explicitly selected standard streams.
1029    for kind in [-10_i32, -11, -12] {
1030        let handle = unsafe { GetStdHandle(kind as u32) };
1031        if handle.is_null() || handle as isize == -1 {
1032            continue;
1033        }
1034        if unsafe { SetHandleInformation(handle, 1, 0) } == 0 {
1035            let error = std::io::Error::last_os_error();
1036            // Console pseudo handles have no inheritable kernel handle flag.
1037            if error.raw_os_error() != Some(6) {
1038                return Err(error);
1039            }
1040        }
1041    }
1042    Ok(())
1043}
1044
1045/// A concurrent fork can inherit the generation's writable file descriptor.
1046/// Even after pinning closes its copy, Unix exec can return ETXTBSY until the
1047/// other child closes its copy on exec or exit. Retry only that spawn error.
1048async fn spawn_generation(
1049    mut spawn: impl FnMut() -> std::io::Result<Child>,
1050) -> std::io::Result<Child> {
1051    for delay_ms in [10, 20, 40, 80] {
1052        match spawn() {
1053            Err(error) if error.kind() == std::io::ErrorKind::ExecutableFileBusy => {
1054                tokio::time::sleep(Duration::from_millis(delay_ms)).await;
1055            }
1056            result => return result,
1057        }
1058    }
1059    spawn()
1060}
1061
1062/// Reserve an exclusive operation and detach its immutable executable. Returns
1063/// after acknowledgement; a timeout preserves the reservation and never kills
1064/// the child or silently executes in the caller. Repeated identical requests
1065/// attach to the existing reservation rather than dispatching twice.
1066pub async fn spawn(home: &Path, kind: &str, key: &str, payload: serde_json::Value) -> Result<Job> {
1067    std::fs::create_dir_all(home).map_err(not_started_error)?;
1068    let home = home
1069        .canonicalize()
1070        .context("resolve worker home")
1071        .map_err(not_started_error)?;
1072    let pin_home = home.clone();
1073    let generation = tokio::task::spawn_blocking(move || pin_current(&pin_home))
1074        .await
1075        .map_err(not_started_error)?
1076        .map_err(not_started_error)?;
1077    let (job, fresh) = reserve(&home, &generation, kind, key, payload)?;
1078    if fresh {
1079        let child = match detached_command(&home, &job, &generation) {
1080            Ok(mut command) => spawn_generation(|| command.spawn())
1081                .await
1082                .map_err(Into::into),
1083            Err(error) => Err(error),
1084        };
1085        match child {
1086            Ok(mut child) => {
1087                // std::process::Child has no kill-on-drop. The reaper is only
1088                // bookkeeping; runtime or parent exit cannot terminate a worker.
1089                std::thread::spawn(move || {
1090                    let _ = child.wait();
1091                });
1092            }
1093            Err(error) => {
1094                let _store = store_lock(&home)?;
1095                let mut job = get(&home, &job.id)?;
1096                if job.status == Status::Starting {
1097                    if let Some(_lease) = try_lock(&job_dir(&home, &job.id)?.join("live.lock"))? {
1098                        terminal(
1099                            &home,
1100                            &mut job,
1101                            Status::Failed,
1102                            format!("worker spawn failed: {error}"),
1103                        )?;
1104                        return Err(not_started_error(error));
1105                    }
1106                }
1107                return Err(error);
1108            }
1109        }
1110    }
1111    let deadline = tokio::time::Instant::now() + Duration::from_secs(10);
1112    loop {
1113        let current = get(&home, &job.id)?;
1114        match current.status {
1115            Status::Running | Status::Finished => return Ok(current),
1116            Status::Failed => bail!(
1117                "worker {} failed: {}",
1118                current.id,
1119                current.result.as_deref().unwrap_or("unknown failure")
1120            ),
1121            Status::Starting => {}
1122        }
1123        if tokio::time::Instant::now() >= deadline {
1124            return Err(StartupUnacknowledged { id: job.id }.into());
1125        }
1126        tokio::time::sleep(Duration::from_millis(20)).await;
1127    }
1128}
1129
1130/// Hidden detached entrypoint. The lease is acquired before acknowledgement and
1131/// is retained through handler result persistence. Only the recorded generation
1132/// can acknowledge a reservation, even if a newer CLI is accidentally invoked.
1133pub async fn run(home: &Path, id: &str) -> Result<()> {
1134    let dir = job_dir(home, id)?;
1135    let (job, _lease) = {
1136        let _store = store_lock(home)?;
1137        let mut job = get(home, id)?;
1138        if job.status != Status::Starting {
1139            bail!("worker reservation is no longer startable");
1140        }
1141        let lease =
1142            try_lock(&dir.join("live.lock"))?.context("worker execution lease is already held")?;
1143        let current = std::env::current_exe()
1144            .context("locate running worker executable")?
1145            .canonicalize()
1146            .context("resolve running worker executable")?;
1147        let expected = executable(home, &job.generation)?
1148            .canonicalize()
1149            .with_context(|| {
1150                format!("resolve worker {id} reserved generation {}", job.generation)
1151            })?;
1152        if current != expected {
1153            bail!("worker must execute its reserved immutable generation");
1154        }
1155        job.pid = Some(std::process::id());
1156        job.version = Some(env!("CARGO_PKG_VERSION").to_owned());
1157        job.status = Status::Running;
1158        save(&dir.join("job.json"), &job)
1159            .with_context(|| format!("acknowledge worker {id} startup"))?;
1160        (job, lease)
1161    };
1162    let result = match job.kind.as_str() {
1163        "task" => crate::daemon::run_task_worker(home, job.payload.clone()).await,
1164        "scheduler" => crate::daemon::run_scheduler_worker(home, job.payload.clone()).await,
1165        "chat" => crate::chat_worker::run_turn_worker(home, job.payload.clone()).await,
1166        "service" => crate::consult::run_service_worker(home, job.payload.clone()).await,
1167        "daemon-service" => crate::daemon::run_service_worker(home, job.payload.clone()).await,
1168        "deputy" => crate::deputy::run_worker(home, job.payload.clone()).await,
1169        kind => Err(anyhow::anyhow!("unknown worker kind {kind}")),
1170    };
1171    let _store = store_lock(home)?;
1172    let mut current = get(home, id)?;
1173    match &result {
1174        Ok(()) => terminal(home, &mut current, Status::Finished, "completed".to_owned())?,
1175        Err(error) => terminal(home, &mut current, Status::Failed, format!("{error:#}"))?,
1176    }
1177    result
1178}
1179
1180fn hex_digest(hash: Sha256) -> String {
1181    // sha2 0.11's `finalize` returns a `hybrid_array::Array`, which no
1182    // longer implements `LowerHex`, so format the bytes individually.
1183    hash.finalize()
1184        .iter()
1185        .map(|byte| format!("{byte:02x}"))
1186        .collect()
1187}
1188fn digest(bytes: &[u8]) -> String {
1189    let mut hash = Sha256::new();
1190    hash.update(bytes);
1191    hex_digest(hash)
1192}
1193
1194#[cfg(test)]
1195mod tests {
1196    use super::*;
1197
1198    #[test]
1199    fn observing_an_unused_store_requires_no_writes() {
1200        let home = tempfile::tempdir().unwrap();
1201        assert!(observe_active(home.path()).unwrap().is_empty());
1202        assert!(!home.path().join("workers").exists());
1203    }
1204
1205    #[test]
1206    fn observing_execution_probes_the_lease_without_rewriting_history() {
1207        let home = tempfile::tempdir().unwrap();
1208        let mut job = reservation(home.path(), "task:observer");
1209        let directory = job_dir(home.path(), &job.id).unwrap();
1210        let live_path = directory.join("live.lock");
1211        let lease = try_lock(&live_path).unwrap().unwrap();
1212        job.status = Status::Running;
1213        save(&directory.join("job.json"), &job).unwrap();
1214        #[cfg(unix)]
1215        {
1216            use std::os::unix::fs::PermissionsExt as _;
1217            std::fs::set_permissions(&live_path, std::fs::Permissions::from_mode(0o444)).unwrap();
1218        }
1219        let before = std::fs::read(directory.join("job.json")).unwrap();
1220        let observed = observe_active(home.path()).unwrap();
1221        assert_eq!(observed.len(), 1);
1222        assert_eq!(observed[0].id, job.id);
1223        drop(lease);
1224        assert!(observe_active(home.path()).unwrap().is_empty());
1225        assert_eq!(std::fs::read(directory.join("job.json")).unwrap(), before);
1226        assert_eq!(get(home.path(), &job.id).unwrap().status, Status::Running);
1227    }
1228    fn generation() -> Generation {
1229        Generation {
1230            id: "a".repeat(64),
1231            executable: PathBuf::from("unused"),
1232        }
1233    }
1234    fn reservation(home: &Path, key: &str) -> Job {
1235        reserve(
1236            home,
1237            &generation(),
1238            "task",
1239            key,
1240            serde_json::json!({"task":"example"}),
1241        )
1242        .unwrap()
1243        .0
1244    }
1245
1246    #[test]
1247    fn reservation_deduplicates_identical_keys_and_rejects_collision() {
1248        let home = tempfile::tempdir().unwrap();
1249        let first = reservation(home.path(), "task:one");
1250        assert_eq!(first.id, reservation(home.path(), "task:one").id);
1251        assert!(
1252            reserve(
1253                home.path(),
1254                &generation(),
1255                "chat",
1256                "task:one",
1257                serde_json::Value::Null
1258            )
1259            .is_err()
1260        );
1261        assert_ne!(first.id, reservation(home.path(), "task:two").id);
1262    }
1263
1264    #[test]
1265    fn reservation_order_is_durable_and_corruption_never_resets_it() {
1266        let home = tempfile::tempdir().unwrap();
1267        let first = reservation(home.path(), "chat:first");
1268        assert_eq!(
1269            first.sequence,
1270            reservation(home.path(), "chat:first").sequence
1271        );
1272        let second = reservation(home.path(), "chat:second");
1273        assert!(second.sequence > first.sequence);
1274        let encoded = serde_json::to_vec(&second).unwrap();
1275        assert_eq!(
1276            serde_json::from_slice::<Job>(&encoded).unwrap().sequence,
1277            second.sequence
1278        );
1279        let mut legacy = serde_json::to_value(&first).unwrap();
1280        legacy.as_object_mut().unwrap().remove("sequence");
1281        assert_eq!(serde_json::from_value::<Job>(legacy).unwrap().sequence, 0);
1282        std::fs::write(root(home.path()).join("sequence.json"), b"broken").unwrap();
1283        assert!(
1284            reserve(
1285                home.path(),
1286                &generation(),
1287                "chat",
1288                "chat:third",
1289                serde_json::Value::Null
1290            )
1291            .is_err()
1292        );
1293    }
1294
1295    #[tokio::test]
1296    async fn pre_reservation_failure_is_proven_without_history_absence_guesses() {
1297        let home = tempfile::tempdir().unwrap();
1298        let blocked = home.path().join("not-a-directory");
1299        std::fs::write(&blocked, b"file").unwrap();
1300        let error = spawn(&blocked, "chat", "fresh:chat", serde_json::Value::Null)
1301            .await
1302            .unwrap_err();
1303        assert!(not_started(&error));
1304        assert!(not_started(&error.context("publish recorded turn")));
1305        assert!(!not_started(&anyhow::anyhow!(
1306            "startup acknowledgement timed out"
1307        )));
1308        assert!(!blocked.join("workers").exists());
1309    }
1310
1311    #[test]
1312    fn failed_os_spawn_is_fenced_and_proven_not_started() {
1313        let home = tempfile::tempdir().unwrap();
1314        let executable = home.path().join("invalid-executable");
1315        std::fs::write(&executable, b"not an executable binary").unwrap();
1316        let output = Command::new(std::env::current_exe().unwrap())
1317            .args([
1318                "--exact",
1319                "worker::tests::unstarted_spawn_helper",
1320                "--ignored",
1321                "--nocapture",
1322            ])
1323            .env("MAGI_WORKER_EXE", &executable)
1324            .env("MAGI_SPAWN_FAILURE_HOME", home.path())
1325            .output()
1326            .unwrap();
1327        assert!(
1328            output.status.success(),
1329            "{}\n{}",
1330            String::from_utf8_lossy(&output.stdout),
1331            String::from_utf8_lossy(&output.stderr)
1332        );
1333    }
1334
1335    #[tokio::test]
1336    #[ignore = "isolated environment for the failed OS spawn test"]
1337    async fn unstarted_spawn_helper() {
1338        let home = PathBuf::from(std::env::var_os("MAGI_SPAWN_FAILURE_HOME").unwrap());
1339        let error = spawn(&home, "chat", "fresh:failed-exec", serde_json::Value::Null)
1340            .await
1341            .unwrap_err();
1342        assert!(not_started(&error));
1343        let entry = std::fs::read_dir(root(&home).join("jobs"))
1344            .unwrap()
1345            .next()
1346            .unwrap()
1347            .unwrap();
1348        let id = entry.file_name().to_str().unwrap().to_owned();
1349        let job = get(&home, &id).unwrap();
1350        assert_eq!(job.status, Status::Failed);
1351        assert_eq!(job.pid, None);
1352        assert!(active(&home).unwrap().is_empty());
1353        assert_eq!(get(&home, &id).unwrap().status, Status::Failed);
1354    }
1355
1356    #[test]
1357    fn unresolved_startup_remains_reserved_and_live_lease_never_expires() {
1358        let home = tempfile::tempdir().unwrap();
1359        let mut job = reservation(home.path(), "task:one");
1360        assert_eq!(active(home.path()).unwrap().len(), 1);
1361        job.created_at = 0;
1362        save(
1363            &job_dir(home.path(), &job.id).unwrap().join("job.json"),
1364            &job,
1365        )
1366        .unwrap();
1367        let lease = try_lock(&job_dir(home.path(), &job.id).unwrap().join("live.lock"))
1368            .unwrap()
1369            .unwrap();
1370        assert_eq!(active(home.path()).unwrap().len(), 1);
1371        drop(lease);
1372        assert!(active(home.path()).unwrap().is_empty());
1373        assert_eq!(get(home.path(), &job.id).unwrap().status, Status::Failed);
1374    }
1375
1376    #[tokio::test]
1377    async fn cancelled_startup_cannot_execute_late() {
1378        let home = tempfile::tempdir().unwrap();
1379        let mut job = reservation(home.path(), "task:one");
1380        job.created_at = 0;
1381        save(
1382            &job_dir(home.path(), &job.id).unwrap().join("job.json"),
1383            &job,
1384        )
1385        .unwrap();
1386        assert!(active(home.path()).unwrap().is_empty());
1387        assert!(
1388            run(home.path(), &job.id)
1389                .await
1390                .unwrap_err()
1391                .to_string()
1392                .contains("no longer startable")
1393        );
1394    }
1395
1396    #[test]
1397    fn running_without_lease_fails_regardless_of_pid() {
1398        let home = tempfile::tempdir().unwrap();
1399        let mut job = reservation(home.path(), "task:one");
1400        job.status = Status::Running;
1401        job.pid = Some(std::process::id());
1402        save(
1403            &job_dir(home.path(), &job.id).unwrap().join("job.json"),
1404            &job,
1405        )
1406        .unwrap();
1407        assert!(active(home.path()).unwrap().is_empty());
1408        assert_eq!(get(home.path(), &job.id).unwrap().status, Status::Failed);
1409    }
1410
1411    #[test]
1412    fn controls_survive_reopen_and_reset_at_new_invocation() {
1413        let home = tempfile::tempdir().unwrap();
1414        request(home.path(), "task:one", Control::Park).unwrap();
1415        assert_eq!(
1416            control(home.path(), "task:one").unwrap(),
1417            Some(Control::Park)
1418        );
1419        reservation(home.path(), "task:one");
1420        assert_eq!(control(home.path(), "task:one").unwrap(), None);
1421        request(home.path(), "task:one", Control::Stop).unwrap();
1422        assert_eq!(
1423            control(home.path(), "task:one").unwrap(),
1424            Some(Control::Stop)
1425        );
1426        assert_eq!(control(home.path(), "task:other").unwrap(), None);
1427    }
1428
1429    #[test]
1430    fn singleton_locks_release_without_pid_cleanup() {
1431        let home = tempfile::tempdir().unwrap();
1432        let lease = try_lease(home.path(), "scheduler").unwrap().unwrap();
1433        assert!(try_lease(home.path(), "scheduler").unwrap().is_none());
1434        assert!(try_lease(home.path(), "other").unwrap().is_some());
1435        drop(lease);
1436        assert!(try_lease(home.path(), "scheduler").unwrap().is_some());
1437    }
1438
1439    #[test]
1440    fn immutable_generation_keeps_old_content_after_source_replacement() {
1441        let home = tempfile::tempdir().unwrap();
1442        let source = home.path().join("source");
1443        std::fs::write(&source, b"old executable").unwrap();
1444        let old = pin_source(home.path(), &source).unwrap();
1445        std::fs::write(&source, b"new executable").unwrap();
1446        let new = pin_source(home.path(), &source).unwrap();
1447        assert_ne!(old.id, new.id);
1448        assert_eq!(
1449            std::fs::read(executable(home.path(), &old.id).unwrap()).unwrap(),
1450            b"old executable"
1451        );
1452        assert_eq!(std::fs::read(new.executable).unwrap(), b"new executable");
1453    }
1454
1455    #[tokio::test]
1456    async fn generation_spawn_does_not_retry_a_missing_executable() {
1457        let home = tempfile::tempdir().unwrap();
1458        let mut command = Command::new(home.path().join("missing-executable"));
1459        let mut attempts = 0;
1460        let error = spawn_generation(|| {
1461            attempts += 1;
1462            command.spawn()
1463        })
1464        .await
1465        .unwrap_err();
1466        assert_eq!(error.kind(), std::io::ErrorKind::NotFound);
1467        assert_eq!(attempts, 1);
1468    }
1469
1470    #[cfg(target_os = "linux")]
1471    fn busy_executable(dir: &Path) -> (Command, File) {
1472        use std::os::unix::fs::PermissionsExt;
1473
1474        let path = dir.join("busy-executable");
1475        std::fs::copy("/bin/sh", &path).unwrap();
1476        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o700)).unwrap();
1477        let writer = OpenOptions::new().write(true).open(&path).unwrap();
1478        let mut command = Command::new(path);
1479        command.args(["-c", "exit 0"]);
1480        (command, writer)
1481    }
1482
1483    #[cfg(target_os = "linux")]
1484    #[tokio::test]
1485    async fn generation_spawn_retries_until_the_writer_closes() {
1486        let home = tempfile::tempdir().unwrap();
1487        let (mut command, writer) = busy_executable(home.path());
1488        let error = command.spawn().unwrap_err();
1489        assert_eq!(error.kind(), std::io::ErrorKind::ExecutableFileBusy);
1490
1491        let (release, requested) = std::sync::mpsc::channel();
1492        let closer = std::thread::spawn(move || {
1493            if requested.recv().is_ok() {
1494                std::thread::sleep(Duration::from_millis(20));
1495            }
1496            drop(writer);
1497        });
1498        let mut release = Some(release);
1499        let child = spawn_generation(|| {
1500            let result = command.spawn();
1501            if let Some(release) = release.take() {
1502                release.send(()).unwrap();
1503            }
1504            result
1505        })
1506        .await;
1507        closer.join().unwrap();
1508        assert!(child.unwrap().wait().unwrap().success());
1509    }
1510
1511    #[cfg(target_os = "linux")]
1512    #[tokio::test]
1513    async fn generation_spawn_stops_retrying_while_the_writer_remains_open() {
1514        let home = tempfile::tempdir().unwrap();
1515        let (mut command, _writer) = busy_executable(home.path());
1516        let mut attempts = 0;
1517        let error = spawn_generation(|| {
1518            attempts += 1;
1519            assert!(attempts <= 5, "busy generation exceeded its retry bound");
1520            command.spawn()
1521        })
1522        .await
1523        .unwrap_err();
1524        assert_eq!(error.kind(), std::io::ErrorKind::ExecutableFileBusy);
1525    }
1526
1527    #[tokio::test]
1528    async fn acknowledgement_records_own_version_and_handler_failure() {
1529        let home = tempfile::tempdir().unwrap();
1530        let generation = pin_source(home.path(), &std::env::current_exe().unwrap()).unwrap();
1531        let (job, _) = reserve(
1532            home.path(),
1533            &generation,
1534            "unknown-kind",
1535            "unknown:one",
1536            serde_json::Value::Null,
1537        )
1538        .unwrap();
1539        let invoke = || async {
1540            let mut command = Command::new(&generation.executable);
1541            command
1542                .args([
1543                    "--exact",
1544                    "worker::tests::pinned_entrypoint_helper",
1545                    "--ignored",
1546                    "--nocapture",
1547                ])
1548                .env("MAGI_WORKER_TEST_HOME", home.path())
1549                .env("MAGI_WORKER_TEST_ID", &job.id)
1550                .stdin(Stdio::null())
1551                .stdout(Stdio::piped())
1552                .stderr(Stdio::piped());
1553            let output = spawn_generation(|| command.spawn())
1554                .await
1555                .unwrap()
1556                .wait_with_output()
1557                .unwrap();
1558            assert!(
1559                output.status.success(),
1560                "{}",
1561                String::from_utf8_lossy(&output.stderr)
1562            );
1563        };
1564        invoke().await;
1565        let failed = get(home.path(), &job.id).unwrap();
1566        assert_eq!(failed.status, Status::Failed);
1567        assert_eq!(failed.version.as_deref(), Some(env!("CARGO_PKG_VERSION")));
1568        assert!(failed.result.unwrap().contains("unknown worker kind"));
1569        assert!(active(home.path()).unwrap().is_empty());
1570        assert!(!job_dir(home.path(), &job.id).unwrap().exists());
1571        invoke().await;
1572        assert!(!job_dir(home.path(), &job.id).unwrap().exists());
1573    }
1574
1575    #[tokio::test]
1576    #[ignore = "subprocess entrypoint for the immutable-generation startup test"]
1577    async fn pinned_entrypoint_helper() {
1578        let home = PathBuf::from(std::env::var_os("MAGI_WORKER_TEST_HOME").unwrap());
1579        let id = std::env::var("MAGI_WORKER_TEST_ID").unwrap();
1580        assert!(run(&home, &id).await.is_err());
1581    }
1582
1583    #[test]
1584    fn unfinished_staging_is_reclaimed_without_removing_generations() {
1585        let home = tempfile::tempdir().unwrap();
1586        let old = root(home.path()).join("generations").join(".new-abandoned");
1587        std::fs::create_dir_all(&old).unwrap();
1588        let partial = old.join("partial");
1589        std::fs::write(&partial, b"interrupted copy").unwrap();
1590        #[cfg(windows)]
1591        {
1592            let mut permissions = std::fs::metadata(&partial).unwrap().permissions();
1593            permissions.set_readonly(true);
1594            std::fs::set_permissions(&partial, permissions).unwrap();
1595        }
1596        let source = home.path().join("source");
1597        std::fs::write(&source, b"complete executable").unwrap();
1598        let generation = pin_source(home.path(), &source).unwrap();
1599        assert!(!old.exists());
1600        assert!(generation.executable.is_file());
1601    }
1602
1603    #[tokio::test]
1604    async fn unrelated_executable_cannot_acknowledge_or_consume_reservation() {
1605        let home = tempfile::tempdir().unwrap();
1606        let source = home.path().join("other-binary");
1607        std::fs::write(&source, b"different executable of the same Cargo version").unwrap();
1608        let generation = pin_source(home.path(), &source).unwrap();
1609        let (job, _) = reserve(
1610            home.path(),
1611            &generation,
1612            "task",
1613            "task:one",
1614            serde_json::Value::Null,
1615        )
1616        .unwrap();
1617        assert!(
1618            run(home.path(), &job.id)
1619                .await
1620                .unwrap_err()
1621                .to_string()
1622                .contains("immutable generation")
1623        );
1624        assert_eq!(get(home.path(), &job.id).unwrap().status, Status::Starting);
1625        assert_eq!(active(home.path()).unwrap().len(), 1);
1626    }
1627
1628    #[test]
1629    fn abandoned_record_staging_is_bounded_and_never_executes() {
1630        let home = tempfile::tempdir().unwrap();
1631        let staging = root(home.path()).join("jobs").join(".new-interrupted");
1632        std::fs::create_dir_all(&staging).unwrap();
1633        std::fs::write(staging.join("job.json"), b"partial json").unwrap();
1634        assert!(active(home.path()).unwrap().is_empty());
1635        assert!(!staging.exists());
1636    }
1637
1638    #[cfg(unix)]
1639    #[tokio::test]
1640    async fn detached_startup_preserves_stdout_and_stderr_in_record_log() {
1641        use std::os::unix::fs::PermissionsExt;
1642        let home = tempfile::tempdir().unwrap();
1643        let source = home.path().join("startup-diagnostic");
1644        std::fs::write(
1645            &source,
1646            b"#!/bin/sh\nprintf 'startup stdout\\n'\nprintf 'startup stderr\\n' >&2\nexit 7\n",
1647        )
1648        .unwrap();
1649        std::fs::set_permissions(&source, std::fs::Permissions::from_mode(0o755)).unwrap();
1650        let generation = pin_source(home.path(), &source).unwrap();
1651        let (mut job, _) = reserve(
1652            home.path(),
1653            &generation,
1654            "task",
1655            "task:log",
1656            serde_json::Value::Null,
1657        )
1658        .unwrap();
1659        let mut command = detached_command(home.path(), &job, &generation).unwrap();
1660        for key in ["MAGI_RUN", "MAGI_NODE", "MAGI_SEAT"]
1661            .into_iter()
1662            .chain(crate::chat_control::bridge::ENVIRONMENT.iter().copied())
1663        {
1664            assert!(
1665                command
1666                    .get_envs()
1667                    .any(|(name, value)| name == key && value.is_none())
1668            );
1669        }
1670        let mut child = spawn_generation(|| command.spawn()).await.unwrap();
1671        assert_eq!(child.wait().unwrap().code(), Some(7));
1672        let log = job_dir(home.path(), &job.id).unwrap().join("worker.log");
1673        let output = std::fs::read_to_string(log).unwrap();
1674        assert!(output.contains("startup stdout"));
1675        assert!(output.contains("startup stderr"));
1676        terminal(
1677            home.path(),
1678            &mut job,
1679            Status::Failed,
1680            "startup failed".into(),
1681        )
1682        .unwrap();
1683        assert!(active(home.path()).unwrap().is_empty());
1684        assert!(
1685            root(home.path())
1686                .join("results")
1687                .join(&job.id)
1688                .join("worker.log")
1689                .is_file()
1690        );
1691    }
1692
1693    #[cfg(windows)]
1694    #[test]
1695    fn record_publication_survives_readers_and_short_delete_sharing_handoffs() {
1696        use std::os::windows::fs::OpenOptionsExt as _;
1697
1698        let home = tempfile::tempdir().unwrap();
1699        let mut job = reservation(home.path(), "task:metadata-handoff");
1700        let directory = job_dir(home.path(), &job.id).unwrap();
1701        let path = directory.join("job.json");
1702        let lease = try_lock(&directory.join("live.lock")).unwrap().unwrap();
1703        let snapshot = File::open(&path).unwrap();
1704        // This reader allows ordinary IO but temporarily denies replacement.
1705        let reader = OpenOptions::new()
1706            .read(true)
1707            .share_mode(1 | 2) // FILE_SHARE_READ | FILE_SHARE_WRITE
1708            .open(&path)
1709            .unwrap();
1710        std::thread::scope(|scope| {
1711            scope.spawn(move || {
1712                std::thread::sleep(Duration::from_millis(15));
1713                drop(reader);
1714            });
1715            job.status = Status::Running;
1716            save(&path, &job).unwrap();
1717        });
1718        let original: Job = serde_json::from_reader(snapshot).unwrap();
1719        assert_eq!(original.status, Status::Starting);
1720        assert_eq!(get(home.path(), &job.id).unwrap().status, Status::Running);
1721        let running_snapshot = File::open(&path).unwrap();
1722        terminal(home.path(), &mut job, Status::Finished, "completed".into()).unwrap();
1723        assert_eq!(get(home.path(), &job.id).unwrap().status, Status::Finished);
1724        let previous: Job = serde_json::from_reader(running_snapshot).unwrap();
1725        assert_eq!(previous.status, Status::Running);
1726        drop(lease);
1727        assert!(active(home.path()).unwrap().is_empty());
1728    }
1729
1730    #[cfg(windows)]
1731    #[test]
1732    fn denied_record_publication_preserves_reservation_and_original_os_error() {
1733        use std::os::windows::fs::OpenOptionsExt as _;
1734
1735        let home = tempfile::tempdir().unwrap();
1736        let job = reservation(home.path(), "task:metadata-denied");
1737        let directory = job_dir(home.path(), &job.id).unwrap();
1738        let path = directory.join("job.json");
1739        let reader = OpenOptions::new()
1740            .read(true)
1741            .share_mode(1 | 2) // Deny deletion for the entire handoff budget.
1742            .open(&path)
1743            .unwrap();
1744        let mut acknowledged = job.clone();
1745        acknowledged.status = Status::Running;
1746        let error = save(&path, &acknowledged).unwrap_err();
1747        assert!(format!("{error:#}").contains("publish worker record"));
1748        let io = error.downcast_ref::<std::io::Error>().unwrap();
1749        assert!(matches!(io.raw_os_error(), Some(5 | 32)));
1750        assert_eq!(get(home.path(), &job.id).unwrap().status, Status::Starting);
1751        assert_eq!(reservation(home.path(), &job.key).id, job.id);
1752        assert!(std::fs::read_dir(&directory).unwrap().all(|entry| {
1753            !entry
1754                .unwrap()
1755                .file_name()
1756                .to_string_lossy()
1757                .starts_with(".pending-")
1758        }));
1759        drop(reader);
1760        save(&path, &acknowledged).unwrap();
1761        assert_eq!(get(home.path(), &job.id).unwrap().status, Status::Running);
1762    }
1763
1764    #[test]
1765    fn terminal_result_survives_open_execution_and_output_handles() {
1766        let home = tempfile::tempdir().unwrap();
1767        let mut job = reservation(home.path(), "task:open-handles");
1768        let directory = job_dir(home.path(), &job.id).unwrap();
1769        let lease = try_lock(&directory.join("live.lock")).unwrap().unwrap();
1770        let mut log = OpenOptions::new()
1771            .create(true)
1772            .append(true)
1773            .open(directory.join("worker.log"))
1774            .unwrap();
1775        writeln!(log, "before completion").unwrap();
1776        terminal(home.path(), &mut job, Status::Finished, "completed".into()).unwrap();
1777        assert_eq!(get(home.path(), &job.id).unwrap().status, Status::Finished);
1778        assert!(active(home.path()).unwrap().is_empty());
1779        assert!(
1780            directory.exists(),
1781            "a held execution lease must prevent retirement"
1782        );
1783        drop(lease);
1784        // Retirement may still be denied by the output handle on Windows.
1785        // Either way, querying active work and the durable result must succeed.
1786        assert!(active(home.path()).unwrap().is_empty());
1787        assert_eq!(get(home.path(), &job.id).unwrap().status, Status::Finished);
1788        writeln!(log, "after completion").unwrap();
1789        drop(log);
1790        assert!(active(home.path()).unwrap().is_empty());
1791        assert!(
1792            root(home.path())
1793                .join("results")
1794                .join(&job.id)
1795                .join("worker.log")
1796                .is_file()
1797        );
1798        assert!(!directory.exists());
1799    }
1800
1801    #[test]
1802    fn terminal_records_are_bounded_without_pruning_live_reservations() {
1803        let home = tempfile::tempdir().unwrap();
1804        let live = reservation(home.path(), "task:live");
1805        // Exercise retention, not hundreds of durable reservation publications.
1806        // Use a real terminal record as the on-disk template; reservation and
1807        // terminal publication have their own tests above.
1808        let mut finished = reservation(home.path(), "task:finished");
1809        terminal(home.path(), &mut finished, Status::Finished, "done".into()).unwrap();
1810        let template_dir = job_dir(home.path(), &finished.id).unwrap();
1811        std::fs::remove_dir_all(template_dir).unwrap();
1812        for i in 0..RETAIN_FINISHED + 3 {
1813            let mut job = finished.clone();
1814            job.id = format!("{i:032x}");
1815            job.key = format!("task:{i}");
1816            job.sequence += i as u64;
1817            let directory = job_dir(home.path(), &job.id).unwrap();
1818            std::fs::create_dir_all(&directory).unwrap();
1819            std::fs::write(
1820                directory.join("job.json"),
1821                serde_json::to_vec(&job).unwrap(),
1822            )
1823            .unwrap();
1824        }
1825        assert_eq!(
1826            std::fs::read_dir(root(home.path()).join("jobs"))
1827                .unwrap()
1828                .count(),
1829            RETAIN_FINISHED + 4
1830        );
1831        let jobs = records(home.path()).unwrap();
1832        assert_eq!(jobs.len(), 1);
1833        assert_eq!(
1834            std::fs::read_dir(root(home.path()).join("results"))
1835                .unwrap()
1836                .count(),
1837            RETAIN_FINISHED
1838        );
1839        assert!(jobs.iter().any(|job| job.id == live.id));
1840    }
1841}