Skip to main content

speccoder_runtime/
in_place.rs

1//! In-place runs: `speccoder run --in-place` works on
2//! `speccoder/<feature>` in the person's own checkout instead of a separate
3//! worktree — the way to run features whose spec git ignores (a worktree
4//! holds only commits).
5//!
6//! What the worktree gave, replaced:
7//! - a clean checkout before the run, and the person's branch never touched:
8//!   the run switches to `speccoder/<feature>` and back;
9//! - an intent the run cannot read differently later: every intent, judge
10//!   and context file — git-ignored ones included — is hashed when the run
11//!   starts (the manifest) and re-checked before every task and every tick;
12//!   only the engine's own one-byte tick and convergence append change it;
13//! - a checkout nobody else changes: HEAD, the branch and a clean tree are
14//!   re-checked before every task and tick (the checkout guard).
15//!
16//! Evidence is committed before the tick: a gitignored `tasks.md` is never in
17//! a commit, so its tick is local, and the commit says so (`Tick: local`).
18
19use std::collections::BTreeMap;
20use std::path::{Path, PathBuf};
21
22use serde::{Deserialize, Serialize};
23use speccoder_core::{CommandData, CommandResult, CommandStatus, InPlaceReport, RefusedPath};
24
25use crate::git::Git;
26use crate::integrity::{JudgeSnapshot, sha256_hex};
27use crate::tools::policy::Policy;
28use crate::worktree;
29
30/// The session record in a project's state directory.
31pub const FILE: &str = "in-place.json";
32
33/// Runtime state under the intent that is not intent.
34fn runtime_state(path: &str) -> bool {
35    path.contains("/workflows/runs/") || path.split('/').any(|c| c == "__pycache__")
36}
37
38/// Why an in-place run refuses or stops.
39#[derive(Debug, Clone, PartialEq, Eq)]
40pub struct InPlaceError {
41    /// `DIRTY-CHECKOUT`, `INTENT-CHANGED`, ….
42    pub code: &'static str,
43    /// What happened.
44    pub message: String,
45    /// The paths involved, with why.
46    pub paths: Vec<(String, String)>,
47    /// What to run: the first is `fix:`, the rest `or:`.
48    pub fix: Vec<String>,
49    /// The person's own commands among them, as argvs.
50    pub commands: Vec<speccoder_core::step::Argvs>,
51}
52
53impl InPlaceError {
54    fn new(code: &'static str, message: impl Into<String>) -> Self {
55        Self {
56            code,
57            message: message.into(),
58            paths: Vec::new(),
59            fix: Vec::new(),
60            commands: Vec::new(),
61        }
62    }
63
64    /// The person's own command as argvs: its line is the fix.
65    fn command(mut self, commands: Vec<Vec<String>>) -> Self {
66        self.fix = vec![speccoder_core::step::render(&commands)];
67        self.commands = vec![commands];
68        self
69    }
70
71    fn fix(mut self, lines: &[&str]) -> Self {
72        self.fix = lines.iter().map(|l| (*l).to_owned()).collect();
73        self
74    }
75
76    /// A stop during the run (exit 23) rather than a refusal before it (14).
77    #[must_use]
78    pub fn is_stop(&self) -> bool {
79        matches!(
80            self.code,
81            "INTENT-CHANGED"
82                | "JUDGE-CHANGED"
83                | "CHECKOUT-CHANGED"
84                | "GIT-CHANGED"
85                | "RETURN-BLOCKED"
86        )
87    }
88
89    /// One line naming the code, for a run report's reason and a saved
90    /// attempt: `stopped[INTENT-CHANGED]: … (specs/x/spec.md: changed)`.
91    #[must_use]
92    pub fn line(&self) -> String {
93        let word = if self.is_stop() { "stopped" } else { "error" };
94        let mut line = format!("{word}[{}]: {}", self.code, self.message);
95        for (p, why) in &self.paths {
96            line.push_str(&format!("; {p} ({why})"));
97        }
98        line
99    }
100
101    /// The command's result, its code and fixes on the envelope.
102    #[must_use]
103    pub fn result(&self, command: &str) -> CommandResult {
104        let status = match self.code {
105            "IN-PLACE-PARALLEL" => CommandStatus::UsageError,
106            _ if self.is_stop() => CommandStatus::NeedsHuman,
107            _ => CommandStatus::GitPrecondition,
108        };
109        CommandResult::new(command, status, &self.message)
110            .with_code(self.code)
111            .with_fix(self.fix.clone())
112            .with_commands(self.commands.clone())
113            .with_data(CommandData::InPlace(Box::new(InPlaceReport {
114                paths: self
115                    .paths
116                    .iter()
117                    .map(|(path, reason)| RefusedPath {
118                        path: path.clone(),
119                        reason: reason.clone(),
120                    })
121                    .collect(),
122            })))
123    }
124}
125
126/// The branch the person was on.
127#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
128#[serde(rename_all = "snake_case")]
129pub enum Original {
130    /// A branch name.
131    Branch(String),
132    /// A detached commit.
133    Detached(String),
134}
135
136impl Original {
137    fn target(&self) -> &str {
138        match self {
139            Self::Branch(b) | Self::Detached(b) => b,
140        }
141    }
142
143    /// As a person reads it.
144    #[must_use]
145    pub fn describe(&self) -> String {
146        match self {
147            Self::Branch(b) => b.clone(),
148            Self::Detached(c) => format!("detached {}", c.chars().take(10).collect::<String>()),
149        }
150    }
151}
152
153/// An in-place run in progress: written before the checkout is switched,
154/// removed after it is switched back.
155#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
156pub struct Session {
157    /// The feature.
158    pub feature: String,
159    /// `speccoder/<feature>`.
160    pub branch: String,
161    /// Where the person was.
162    pub original: Original,
163    /// The checkout (the repository's root).
164    pub checkout: PathBuf,
165    /// The project root inside it.
166    pub project: PathBuf,
167    /// The branch's tip as speccoder last left it.
168    pub tip: String,
169    /// Intent, judge and context files by hash, as the run expects them.
170    pub manifest: BTreeMap<String, String>,
171    /// The running process.
172    pub pid: u32,
173    /// Seconds since the epoch.
174    pub started_at: u64,
175    /// What starting this run did on its own (a resume, work set aside, a
176    /// tick restored): told with the outcome, never recorded.
177    #[serde(skip)]
178    pub notes: Vec<String>,
179    /// What git ignored when the session began (directories end in `/`):
180    /// hidden from the model's file tools, as a worktree would not hold it.
181    #[serde(default)]
182    pub hidden: Vec<String>,
183    /// The repository's configuration, hooks, `info/` and refs (other than
184    /// the session's branch) by hash: nothing a run does may change them.
185    #[serde(default)]
186    pub git: String,
187}
188
189/// The manifest of `project`: every intent, judge and context file, ignored
190/// ones included, minus runtime state.
191#[must_use]
192pub fn manifest(project: &Path, policy: &Policy) -> BTreeMap<String, String> {
193    JudgeSnapshot::take(project, policy)
194        .entries()
195        .iter()
196        .filter(|(p, _)| !runtime_state(p))
197        .map(|(p, h)| (p.clone(), h.clone()))
198        .collect()
199}
200
201/// The paths that differ between two manifests, with how.
202#[must_use]
203pub fn diff(
204    expected: &BTreeMap<String, String>,
205    now: &BTreeMap<String, String>,
206) -> Vec<(String, String)> {
207    let mut out = Vec::new();
208    for (p, h) in expected {
209        match now.get(p) {
210            None => out.push((p.clone(), "deleted".to_owned())),
211            Some(n) if n != h => out.push((p.clone(), "modified".to_owned())),
212            _ => {}
213        }
214    }
215    for p in now.keys().filter(|p| !expected.contains_key(*p)) {
216        out.push((p.clone(), "added".to_owned()));
217    }
218    out
219}
220
221/// Whether `after` is `before` with exactly one byte changed, from a space
222/// to `x`/`X` (the engine's tick).
223#[must_use]
224pub fn is_tick(before: &[u8], after: &[u8]) -> bool {
225    let changed: Vec<(u8, u8)> = before
226        .iter()
227        .zip(after)
228        .filter(|(a, b)| a != b)
229        .map(|(a, b)| (*a, *b))
230        .collect();
231    before.len() == after.len() && matches!(changed.as_slice(), [(b' ', b'x' | b'X')])
232}
233
234/// SHA-256 over the intent files except the task lists (files named
235/// `tasks_file`, whose task lines the commits pin one by one with
236/// `Task-Line:`): what the verdict commits name (`Intent-Digest:`), so a
237/// later reader can tell the spec, plan or constitution changed — not a
238/// tick or a convergence round.
239#[must_use]
240pub fn intent_digest(project: &Path, policy: &Policy, tasks_file: &str) -> String {
241    let mut parts = Vec::new();
242    for (path, hash) in manifest(project, policy) {
243        if policy.write_protected(&path) != Some(crate::tools::paths::Class::Intent) {
244            continue;
245        }
246        if path.rsplit('/').next() == Some(tasks_file) {
247            continue;
248        }
249        parts.push(format!("{path} {hash}"));
250    }
251    sha256_hex(parts.join("\n").as_bytes())
252}
253
254pub(crate) use crate::integrity::untick;
255
256fn now() -> u64 {
257    std::time::SystemTime::now()
258        .duration_since(std::time::UNIX_EPOCH)
259        .map_or(0, |d| d.as_secs())
260}
261
262impl Session {
263    /// The record's path in a project's state directory.
264    #[must_use]
265    pub fn path(plane_dir: &Path) -> PathBuf {
266        plane_dir.join(FILE)
267    }
268
269    /// The record, if a session exists.
270    #[must_use]
271    pub fn load(plane_dir: &Path) -> Option<Self> {
272        let text = std::fs::read_to_string(Self::path(plane_dir)).ok()?;
273        serde_json::from_str(&text).ok()
274    }
275
276    /// Writes the record (temporary file, then rename).
277    pub fn save(&self, plane_dir: &Path) -> Result<(), String> {
278        std::fs::create_dir_all(plane_dir).map_err(|e| e.to_string())?;
279        let tmp = plane_dir.join(format!("{FILE}.tmp"));
280        let text = serde_json::to_string_pretty(self).map_err(|e| e.to_string())?;
281        std::fs::write(&tmp, text).map_err(|e| e.to_string())?;
282        std::fs::rename(&tmp, Self::path(plane_dir)).map_err(|e| e.to_string())
283    }
284
285    /// Whether the process that owns it is running.
286    #[must_use]
287    pub fn is_live(&self) -> bool {
288        self.pid != std::process::id() && crate::progress::alive(self.pid)
289    }
290
291    /// Checks the preconditions, records the session, and switches the
292    /// checkout to `speccoder/<feature>`.
293    pub fn begin(
294        project: &Path,
295        plane_dir: &Path,
296        feature: &str,
297        policy: &Policy,
298    ) -> Result<Self, InPlaceError> {
299        let root_of = Git::at(project).toplevel().ok().flatten().ok_or_else(|| {
300            InPlaceError::new("UNBORN-HEAD", "the project is not inside a git repository")
301                .fix(&["git init && git add -A && git commit -m init"])
302        })?;
303        let checkout = std::fs::canonicalize(&root_of).unwrap_or(root_of);
304        let project = std::fs::canonicalize(project).unwrap_or_else(|_| project.to_path_buf());
305        let git = Git::at(&checkout);
306        if let Some(s) = Self::load(plane_dir).filter(Self::is_live) {
307            return Err(InPlaceError::new(
308                "IN-PLACE-ACTIVE",
309                format!(
310                    "an in-place run of {} is running (pid {})",
311                    s.feature, s.pid
312                ),
313            ));
314        }
315        if git.head().ok().flatten().is_none() {
316            return Err(
317                InPlaceError::new("UNBORN-HEAD", "the repository has no commits yet")
318                    .fix(&["git add -A && git commit -m init"]),
319            );
320        }
321        // A run of this feature that died on its branch, at the commit it
322        // recorded: with the intent unchanged it is taken over — an attempt
323        // the journal can resume resumes; with the intent changed, what it
324        // left is saved and the task starts fresh.
325        let mut notes = Vec::new();
326        if let Some(mut prior) = Self::load(plane_dir).filter(|p| {
327            p.feature == feature
328                && git
329                    .line(&["symbolic-ref", "-q", "HEAD"])
330                    .unwrap_or_default()
331                    == format!("refs/heads/{}", p.branch)
332                && git.line(&["rev-parse", "HEAD"]).unwrap_or_default() == p.tip
333        }) {
334            let dirty = worktree::require_clean(&checkout).is_err();
335            if diff(&prior.manifest, &manifest(&project, policy)).is_empty() {
336                if !dirty || resumable(plane_dir, feature, &prior.tip) {
337                    prior.pid = std::process::id();
338                    prior.started_at = now();
339                    prior
340                        .save(plane_dir)
341                        .map_err(|e| InPlaceError::new("IN-PLACE-ACTIVE", e))?;
342                    prior.notes.push(format!(
343                        "took over the interrupted in-place run (returns to {})",
344                        prior.original.describe()
345                    ));
346                    return Ok(prior);
347                }
348            } else if dirty
349                && let Some(r) = prior.set_aside(
350                    plane_dir,
351                    "left by an interrupted in-place run; the spec changed since, so it starts fresh",
352                )? {
353                    notes.push(format!(
354                        "the spec changed since the run was interrupted: what it left is saved as {r} and the task starts fresh"
355                    ));
356                }
357        }
358        if let Err(r) = worktree::require_clean(&checkout) {
359            let mut e = InPlaceError::new(
360                "DIRTY-CHECKOUT",
361                "the checkout has uncommitted changes; an in-place run needs a clean one",
362            )
363            .fix(&["git stash -u", "git commit"]);
364            if let Some((p, _)) = r.paths.iter().find(|(p, _)| {
365                policy.write_protected(p) == Some(crate::tools::paths::Class::Intent)
366            }) {
367                e.fix.push(format!("echo {p} >> .git/info/exclude"));
368            }
369            e.paths = r.paths;
370            return Err(e);
371        }
372        let config =
373            crate::verify::find_config(&project).map(|d| d.join(crate::verify::config::FILE));
374        let tracked = config.as_ref().is_some_and(|c| {
375            git.succeeds(&["ls-files", "--error-unmatch", "--", &c.to_string_lossy()])
376                .unwrap_or(false)
377        });
378        if !tracked {
379            return Err(InPlaceError::new(
380                "CONFIG-NOT-TRACKED",
381                "speccoder.toml is not committed; the checks that judge a run are reviewed like code",
382            )
383            .fix(&["git add speccoder.toml && git commit -m \"speccoder config\""]));
384        }
385        let branch = format!("speccoder/{feature}");
386        let listing = git
387            .run(&["worktree", "list", "--porcelain"])
388            .map(|b| String::from_utf8_lossy(&b).into_owned())
389            .unwrap_or_default();
390        let mut current_wt = String::new();
391        for line in listing.lines() {
392            if let Some(p) = line.strip_prefix("worktree ") {
393                current_wt = p.to_owned();
394            } else if line == format!("branch refs/heads/{branch}")
395                && Path::new(&current_wt) != checkout
396            {
397                return Err(InPlaceError::new(
398                    "BRANCH-IN-WORKTREE",
399                    format!("{branch} is checked out in the worktree {current_wt}"),
400                )
401                .command(vec![
402                    // An absolute path (git's listing): never a flag.
403                    ["git", "worktree", "remove"]
404                        .map(str::to_owned)
405                        .into_iter()
406                        .chain([current_wt.clone()])
407                        .collect(),
408                ]));
409            }
410        }
411        let head_ref = git
412            .line(&["symbolic-ref", "-q", "HEAD"])
413            .unwrap_or_default();
414        // A run that died leaves its record: on its branch, the branch to
415        // go back to is the recorded one, not the one checked out now.
416        let prior = Self::load(plane_dir);
417        if let Some(p) = prior.as_ref().filter(|p| p.feature != feature) {
418            return Err(InPlaceError::new(
419                "IN-PLACE-SESSION",
420                format!(
421                    "an interrupted in-place run of {} holds this checkout",
422                    p.feature
423                ),
424            )
425            .fix(&["speccoder run --in-place --return"]));
426        }
427        let original = match (prior, head_ref.strip_prefix("refs/heads/")) {
428            (Some(p), Some(b)) if b == branch => p.original,
429            (_, Some(b)) => Original::Branch(b.to_owned()),
430            (_, None) => Original::Detached(git.line(&["rev-parse", "HEAD"]).unwrap_or_default()),
431        };
432        let mut session = Self {
433            feature: feature.to_owned(),
434            branch: branch.clone(),
435            original: original.clone(),
436            checkout: checkout.clone(),
437            project: project.clone(),
438            tip: String::new(),
439            manifest: BTreeMap::new(),
440            pid: std::process::id(),
441            started_at: now(),
442            notes,
443            hidden: Vec::new(),
444            git: String::new(),
445        };
446        // Written before switching: a crash after the switch still knows
447        // where to go back to.
448        session
449            .save(plane_dir)
450            .map_err(|e| InPlaceError::new("IN-PLACE-ACTIVE", e))?;
451        if original != Original::Branch(branch.clone()) {
452            let exists = git
453                .succeeds(&[
454                    "rev-parse",
455                    "--verify",
456                    "-q",
457                    &format!("refs/heads/{branch}"),
458                ])
459                .unwrap_or(false);
460            let switched = if exists {
461                git.run(&["switch", "-q", &branch])
462            } else {
463                git.run(&["switch", "-q", "-c", &branch, "HEAD"])
464            };
465            if let Err(e) = switched {
466                let _ = std::fs::remove_file(Self::path(plane_dir));
467                return Err(InPlaceError::new(
468                    "DIRTY-CHECKOUT",
469                    format!("cannot switch to {branch}: {}", e.0),
470                ));
471            }
472            // The person's committed spec changes reach the branch.
473            if exists
474                && let Original::Branch(b) = &original
475                && !git
476                    .succeeds(&["merge-base", "--is-ancestor", b, "HEAD"])
477                    .unwrap_or(true)
478            {
479                let mut merge = crate::attempts::IDENTITY.to_vec();
480                merge.extend(["merge", "-q", "--no-edit", b.as_str()]);
481                if git.run(&merge).is_err() {
482                    let _ = git.run(&["merge", "--abort"]);
483                    let _ = git.run(&["switch", "-q", original.target()]);
484                    let _ = std::fs::remove_file(Self::path(plane_dir));
485                    return Err(InPlaceError::new(
486                        "MERGE-CONFLICT",
487                        format!("{b} does not merge cleanly into {branch}"),
488                    )
489                    .fix(&[&format!(
490                        "git switch {branch} && git merge {b}   # resolve, commit, then run again"
491                    )]));
492                }
493            }
494        }
495        session.tip = git.line(&["rev-parse", "HEAD"]).unwrap_or_default();
496        session.manifest = manifest(&project, policy);
497        session.git = git_fingerprint(&checkout, &branch);
498        session.hidden = git
499            .run(&[
500                "ls-files",
501                "-z",
502                "--others",
503                "--ignored",
504                "--exclude-standard",
505                "--directory",
506            ])
507            .map(|b| {
508                String::from_utf8_lossy(&b)
509                    .split('\0')
510                    .filter(|p| !p.is_empty())
511                    .map(str::to_owned)
512                    .collect()
513            })
514            .unwrap_or_default();
515        session
516            .save(plane_dir)
517            .map_err(|e| InPlaceError::new("IN-PLACE-ACTIVE", e))?;
518        Ok(session)
519    }
520
521    /// Saves what a dead run left in the checkout under an attempt ref,
522    /// restores the branch tip, and forgets the attempts that cannot resume
523    /// any more. `None` when there was nothing to save.
524    fn set_aside(&self, plane_dir: &Path, reason: &str) -> Result<Option<String>, InPlaceError> {
525        // A call that waited for a person waits on nothing now. One that
526        // cannot be withdrawn stops this: its attempt would resume into a
527        // checkout that no longer holds its writes.
528        crate::approvals::withdraw_waiting(&self.project, plane_dir, &self.feature, reason)
529            .map_err(|e| {
530                InPlaceError::new(
531                    "RETURN-BLOCKED",
532                    format!("the calls waiting for a person could not be withdrawn: {e}"),
533                )
534            })?;
535        let _ = std::fs::remove_dir_all(plane_dir.join("resume").join(&self.feature));
536        if worktree::require_clean(&self.checkout).is_ok() {
537            return Ok(None);
538        }
539        let tmp = plane_dir.join("tmp");
540        let record = crate::attempts::Record {
541            run: None,
542            outcome: "interrupted",
543            reason,
544            pending: None,
545            usage: None,
546        };
547        let saved = crate::attempts::save(&self.checkout, &tmp, &self.feature, "in-place", &record)
548            .map_err(|e| InPlaceError::new("RETURN-BLOCKED", e.0))?;
549        let git = Git::at(&self.checkout);
550        let head_tree =
551            worktree::Checkpoint(git.line(&["rev-parse", "HEAD^{tree}"]).unwrap_or_default());
552        worktree::restore(&self.checkout, &tmp, &head_tree)
553            .map_err(|e| InPlaceError::new("RETURN-BLOCKED", e.0))?;
554        Ok(Some(saved.reference))
555    }
556
557    /// The checkout guard and the manifest, before every task: HEAD where
558    /// speccoder left it, nothing changed, the protected files as recorded.
559    pub fn check(&self, policy: &Policy) -> Result<(), InPlaceError> {
560        self.check_with(policy, true)
561    }
562
563    /// [`Session::check`] before a tick: the attempt's own writes are in the
564    /// checkout then, so only HEAD and the protected files are compared.
565    pub fn check_before_tick(&self, policy: &Policy) -> Result<(), InPlaceError> {
566        self.check_with(policy, false)
567    }
568
569    fn check_with(&self, policy: &Policy, clean: bool) -> Result<(), InPlaceError> {
570        // Where the sandbox cannot keep `.git` read-only, a change is caught
571        // here — before speccoder runs git there again for a tick.
572        if !self.git.is_empty() && git_fingerprint(&self.checkout, &self.branch) != self.git {
573            return Err(InPlaceError::new(
574                "GIT-CHANGED",
575                "the repository's configuration, hooks or branches changed during the run; speccoder does not run git against them",
576            )
577            .fix(&[
578                "git -C . config --list --local   # review, restore what changed",
579                "speccoder run --in-place --return",
580            ]));
581        }
582        let git = Git::at(&self.checkout);
583        let head_ref = git
584            .line(&["symbolic-ref", "-q", "HEAD"])
585            .unwrap_or_default();
586        let head = git.line(&["rev-parse", "HEAD"]).unwrap_or_default();
587        let fix = [
588            format!("git stash -u && git switch {}", self.original.target()),
589            "speccoder run --in-place --return".to_owned(),
590        ];
591        if head_ref != format!("refs/heads/{}", self.branch) || head != self.tip {
592            let mut e = InPlaceError::new(
593                "CHECKOUT-CHANGED",
594                format!(
595                    "the checkout moved during the run (HEAD is {}, speccoder left {} at {}); nothing was switched",
596                    if head_ref.is_empty() {
597                        head.clone()
598                    } else {
599                        head_ref.clone()
600                    },
601                    self.branch,
602                    self.tip.chars().take(10).collect::<String>()
603                ),
604            );
605            e.fix = fix.to_vec();
606            return Err(e);
607        }
608        if let Some(r) = clean
609            .then(|| worktree::require_clean(&self.checkout).err())
610            .flatten()
611        {
612            let mut e = InPlaceError::new(
613                "CHECKOUT-CHANGED",
614                "the checkout changed during the run; nothing was saved, restored or switched",
615            );
616            e.paths = r.paths;
617            e.fix = fix.to_vec();
618            return Err(e);
619        }
620        let now = manifest(&self.project, policy);
621        let changed = diff(&self.manifest, &now);
622        if !changed.is_empty() {
623            let intent = changed.iter().any(|(p, _)| {
624                policy.write_protected(p) == Some(crate::tools::paths::Class::Intent)
625            });
626            let mut e = InPlaceError::new(
627                if intent {
628                    "INTENT-CHANGED"
629                } else {
630                    "JUDGE-CHANGED"
631                },
632                if intent {
633                    "the spec changed under the running judge; the run stops before judging against it"
634                } else {
635                    "a judge input changed under the running judge"
636                },
637            );
638            e.paths = changed;
639            e.fix = vec![
640                "review the change, then run again: the next run judges against the spec as it is"
641                    .to_owned(),
642            ];
643            return Err(e);
644        }
645        Ok(())
646    }
647
648    /// After a speccoder commit: the new tip.
649    pub fn advance(&mut self, plane_dir: &Path) {
650        self.tip = Git::at(&self.checkout)
651            .line(&["rev-parse", "HEAD"])
652            .unwrap_or_default();
653        let _ = self.save(plane_dir);
654    }
655
656    /// After the engine's tick or append of `file` (absolute): the manifest
657    /// takes its new hash — only when the change is exactly that (`accept`
658    /// says what the bytes before and after may be).
659    pub fn accept(
660        &mut self,
661        plane_dir: &Path,
662        file: &Path,
663        before: &[u8],
664        accept: impl Fn(&[u8], &[u8]) -> bool,
665    ) -> Result<(), InPlaceError> {
666        let after = std::fs::read(file).unwrap_or_default();
667        let rel = file
668            .strip_prefix(&self.project)
669            .map(|r| r.to_string_lossy().replace('\\', "/"))
670            .unwrap_or_default();
671        if !accept(before, &after) {
672            return Err(InPlaceError::new(
673                "INTENT-CHANGED",
674                format!("{rel} changed beyond the engine's own write"),
675            ));
676        }
677        self.manifest.insert(rel, sha256_hex(&after));
678        let _ = self.save(plane_dir);
679        Ok(())
680    }
681
682    /// Switches the checkout back to where the person was, when it is as
683    /// speccoder left it; removes the record after.
684    pub fn finish(&self, plane_dir: &Path) -> Result<(), InPlaceError> {
685        let git = Git::at(&self.checkout);
686        let head = git.line(&["rev-parse", "HEAD"]).unwrap_or_default();
687        let on_branch = git
688            .line(&["symbolic-ref", "-q", "HEAD"])
689            .unwrap_or_default()
690            == format!("refs/heads/{}", self.branch);
691        if !on_branch || head != self.tip || worktree::require_clean(&self.checkout).is_err() {
692            return Err(InPlaceError::new(
693                "RETURN-BLOCKED",
694                format!(
695                    "the checkout is not as speccoder left it, so it stays on {}",
696                    self.branch
697                ),
698            )
699            .fix(&[
700                &format!("git stash -u && git switch {}", self.original.target()),
701                "speccoder run --in-place --return",
702            ]));
703        }
704        if self.original != Original::Branch(self.branch.clone()) {
705            let back = match &self.original {
706                Original::Branch(b) => git.run(&["switch", "-q", b]),
707                Original::Detached(c) => git.run(&["switch", "-q", "--detach", c]),
708            };
709            if let Err(e) = back {
710                return Err(InPlaceError::new("RETURN-BLOCKED", e.0)
711                    .fix(&[&format!("git switch {}", self.original.target())]));
712            }
713        }
714        let _ = std::fs::remove_file(Self::path(plane_dir));
715        Ok(())
716    }
717}
718
719impl Session {
720    /// A run killed between its verdict commit and the local tick: each
721    /// open task whose newest commit names its line exactly (`Task-Line`) is
722    /// ticked now, the manifest accepting it. Returns what was ticked.
723    pub fn reconcile(
724        &mut self,
725        plane_dir: &Path,
726        tasks_file: &Path,
727        tasks: &speccoder_core::snapshot::ParsedTasks,
728        tick: impl Fn(&Path, &str, &str) -> Result<(), String>,
729    ) -> Vec<String> {
730        let log = Git::at(&self.checkout)
731            .run(&[
732                "log",
733                "--format=%(trailers:key=Spec-Task,valueonly)\u{1f}%(trailers:key=Task-Line,valueonly)\u{1e}",
734                &self.branch,
735            ])
736            .map(|b| String::from_utf8_lossy(&b).into_owned())
737            .unwrap_or_default();
738        let mut newest: BTreeMap<String, String> = BTreeMap::new();
739        for record in log.split('\u{1e}') {
740            let Some((task, line)) = record.trim().split_once('\u{1f}') else {
741                continue;
742            };
743            if let Some(id) = task.trim().strip_prefix(&format!("{}/", self.feature)) {
744                newest
745                    .entry(id.to_owned())
746                    .or_insert_with(|| line.trim().to_owned());
747            }
748        }
749        let mut ticked = Vec::new();
750        for t in tasks.tasks.iter().filter(|t| !t.done) {
751            if newest
752                .get(&t.id)
753                .is_none_or(|l| l.is_empty() || *l != t.line_hash)
754            {
755                continue;
756            }
757            let before = std::fs::read(tasks_file).unwrap_or_default();
758            if tick(tasks_file, &t.id, &t.line_hash).is_ok()
759                && self.accept(plane_dir, tasks_file, &before, is_tick).is_ok()
760            {
761                ticked.push(t.id.clone());
762            }
763        }
764        if !ticked.is_empty() {
765            self.notes.push(format!(
766                "ticked {} from {} verdict commit(s): the run was interrupted before the tick",
767                ticked.join(", "),
768                ticked.len()
769            ));
770        }
771        ticked
772    }
773}
774
775/// What committing a task in place did.
776#[derive(Debug, Default)]
777pub struct Committed {
778    /// The verdict commit.
779    pub commit: Option<String>,
780    /// Whether the task is ticked.
781    pub ticked: bool,
782    /// What went wrong, for the report's integrity list.
783    pub problems: Vec<String>,
784}
785
786impl Session {
787    /// Commits a passed (or attested) task and ticks it. The evidence comes
788    /// first: a `tasks_file` git ignores is never in a commit, so the commit
789    /// names the tick as local (`Tick: local`), and the tick follows it. A
790    /// tracked one is ticked and committed together, as in a worktree.
791    /// Every commit says it was made in place, against which intent
792    /// (`intent_digest`) and which task line (`line_hash`, before the tick).
793    pub fn commit_and_tick(
794        &mut self,
795        plane_dir: &Path,
796        tasks_file: &Path,
797        line_hash: &str,
798        message: &str,
799        intent_digest: &str,
800        tick: impl Fn() -> Result<(), String>,
801    ) -> Committed {
802        let mut out = Committed::default();
803        let rel = tasks_file
804            .strip_prefix(&self.checkout)
805            .map(|r| r.to_string_lossy().replace('\\', "/"))
806            .unwrap_or_default();
807        let tracked = Git::at(&self.checkout)
808            .succeeds(&["ls-files", "--error-unmatch", "--", &rel])
809            .unwrap_or(false);
810        let mut trailers = format!(
811            "Mode: in-place\nIntent-Digest: sha256:{intent_digest}\nTask-Line: {line_hash}\n"
812        );
813        if !tracked {
814            trailers.push_str(&format!("Tick: local {rel}\n"));
815        }
816        let message = format!("{}\n{trailers}", message.trim_end());
817        let before = std::fs::read(tasks_file).unwrap_or_default();
818        let tick_now = |out: &mut Committed| match tick() {
819            Ok(()) => out.ticked = true,
820            Err(e) => out.problems.push(format!("tick refused: {e}")),
821        };
822        if tracked {
823            tick_now(&mut out);
824        }
825        if out.ticked || !tracked {
826            match crate::pipeline::commit::commit_attempt_as(&self.checkout, &message, !tracked) {
827                Ok(id) => out.commit = Some(id),
828                Err(e) => out.problems.push(format!("commit failed: {e}")),
829            }
830        }
831        if !tracked && out.commit.is_some() {
832            tick_now(&mut out);
833        }
834        if out.ticked
835            && let Err(e) = self.accept(plane_dir, tasks_file, &before, is_tick)
836        {
837            out.problems.push(e.message);
838        }
839        self.advance(plane_dir);
840        out
841    }
842}
843
844impl Session {
845    /// Commits a convergence round in place: the append to `tasks_file`
846    /// (old bytes an exact prefix of the new) is accepted into the manifest,
847    /// and the commit says it was made in place, against which intent; a
848    /// git-ignored task list is not in it, so the commit may be empty.
849    pub fn commit_round(
850        &mut self,
851        plane_dir: &Path,
852        tasks_file: &Path,
853        before: &[u8],
854        message: &str,
855        intent_digest: &str,
856    ) -> Result<String, String> {
857        self.accept(plane_dir, tasks_file, before, |b, a| a.starts_with(b))
858            .map_err(|e| e.message)?;
859        let rel = tasks_file
860            .strip_prefix(&self.checkout)
861            .map(|r| r.to_string_lossy().replace('\\', "/"))
862            .unwrap_or_default();
863        let tracked = Git::at(&self.checkout)
864            .succeeds(&["ls-files", "--error-unmatch", "--", &rel])
865            .unwrap_or(false);
866        let mut trailers = format!("Mode: in-place\nIntent-Digest: sha256:{intent_digest}\n");
867        if !tracked {
868            trailers.push_str(&format!("Tasks: local {rel}\n"));
869        }
870        let message = format!("{}\n{trailers}", message.trim_end());
871        let id = crate::pipeline::commit::commit_attempt_as(&self.checkout, &message, !tracked)?;
872        self.advance(plane_dir);
873        Ok(id)
874    }
875
876    /// The model's view of this checkout: the policy denies git that moves
877    /// it and hides what git ignored; the sandbox keeps `.git` read-only
878    /// and the ignored secrets unreadable.
879    #[must_use]
880    pub fn confine(
881        &self,
882        policy: Policy,
883        runner: crate::sandbox::Runner,
884    ) -> (Policy, crate::sandbox::Runner, Option<String>) {
885        let secrets: Vec<PathBuf> = self
886            .hidden
887            .iter()
888            .map(|h| h.trim_end_matches('/'))
889            .filter(|h| policy.protected().is_secret(h))
890            .map(|h| self.checkout.join(h))
891            .collect();
892        let (runner, notice) = runner.carve(&[self.checkout.join(".git")], &secrets);
893        (
894            policy.in_place().with_hidden(self.hidden.clone()),
895            runner,
896            notice,
897        )
898    }
899}
900
901/// A hash over what in `.git` could make git run code or move a branch the
902/// person owns: `config`, `hooks/`, `info/`, and every branch and tag except
903/// `branch` (speccoder's own).
904fn git_fingerprint(checkout: &Path, branch: &str) -> String {
905    let git = Git::at(checkout);
906    let Some(dir) = git
907        .line(&["rev-parse", "--path-format=absolute", "--git-common-dir"])
908        .ok()
909        .map(PathBuf::from)
910    else {
911        return String::new();
912    };
913    let mut parts = Vec::new();
914    let mut files = vec![dir.join("config")];
915    for sub in ["hooks", "info"] {
916        files.extend(
917            ignore::WalkBuilder::new(dir.join(sub))
918                .standard_filters(false)
919                .build()
920                .flatten()
921                .filter(|e| e.file_type().is_some_and(|t| t.is_file()))
922                .map(ignore::DirEntry::into_path),
923        );
924    }
925    files.sort();
926    for f in files {
927        let hash = std::fs::read(&f).map_or_else(|_| "-".to_owned(), |b| sha256_hex(&b));
928        parts.push(format!("{} {hash}", f.display()));
929    }
930    let own = format!("refs/heads/{branch} ");
931    let refs = git
932        .run(&[
933            "for-each-ref",
934            "--format=%(refname) %(objectname)",
935            "refs/heads",
936            "refs/tags",
937        ])
938        .map(|b| String::from_utf8_lossy(&b).into_owned())
939        .unwrap_or_default();
940    parts.extend(
941        refs.lines()
942            .filter(|l| !l.starts_with(&own))
943            .map(str::to_owned),
944    );
945    sha256_hex(parts.join("\n").as_bytes())
946}
947
948/// Whether the journal holds an attempt of `feature` that started at `head`.
949fn resumable(plane_dir: &Path, feature: &str, head: &str) -> bool {
950    std::fs::read_dir(plane_dir.join("resume").join(feature))
951        .into_iter()
952        .flatten()
953        .flatten()
954        .filter_map(|e| crate::resume::load(&e.path()))
955        .any(|m| m.head == head)
956}
957
958/// `run --in-place --return`: saves what an interrupted run left, switches
959/// back to the person's branch, removes the record. No model call.
960pub fn return_checkout(plane_dir: &Path) -> Result<String, InPlaceError> {
961    let Some(session) = Session::load(plane_dir) else {
962        return Err(InPlaceError::new(
963            "IN-PLACE-SESSION",
964            "there is no in-place run to return from",
965        ));
966    };
967    if session.is_live() {
968        return Err(InPlaceError::new(
969            "IN-PLACE-ACTIVE",
970            format!("the in-place run (pid {}) is still running", session.pid),
971        ));
972    }
973    let git = Git::at(&session.checkout);
974    let saved = session.set_aside(
975        plane_dir,
976        "left by an interrupted in-place run; saved by --return",
977    )?;
978    let fresh = Session {
979        tip: git.line(&["rev-parse", "HEAD"]).unwrap_or_default(),
980        ..session.clone()
981    };
982    fresh.finish(plane_dir)?;
983    Ok(match saved {
984        Some(r) => format!(
985            "back on {}; what the interrupted run left is saved as {r}",
986            session.original.describe()
987        ),
988        None => format!("back on {}", session.original.describe()),
989    })
990}
991
992/// The project's in-place session, if one is open or was left by a run
993/// that died: what `status` and the TUI show.
994#[must_use]
995pub fn session_at(path: &Path, state: &Path) -> Option<Session> {
996    let root = crate::format::open(path, None).map_or_else(
997        |_| std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf()),
998        |o| PathBuf::from(&o.snapshot.root),
999    );
1000    Session::load(&worktree::project_state_dir(state, &root))
1001}
1002
1003impl Session {
1004    /// `in-place · speccoder/<f> in <checkout> (returns to <original>)`.
1005    #[must_use]
1006    pub fn mode_line(&self) -> String {
1007        format!(
1008            "in-place · {} in {} (returns to {})",
1009            self.branch,
1010            self.checkout.display(),
1011            self.original.describe()
1012        )
1013    }
1014}
1015
1016/// A worktree-mode run of a feature that an in-place session holds: the
1017/// branch is checked out in the person's checkout, so it is refused.
1018#[must_use]
1019pub fn session_elsewhere(path: &Path, feature: Option<&str>, state: &Path) -> Option<InPlaceError> {
1020    let opened = crate::format::open(path, feature).ok()?;
1021    let plane_dir = worktree::project_state_dir(state, Path::new(&opened.snapshot.root));
1022    let session = Session::load(&plane_dir)?;
1023    let wanted = opened
1024        .snapshot
1025        .feature
1026        .as_ref()
1027        .map(|f| f.feature.name.as_str());
1028    if wanted.is_some_and(|w| w != session.feature) {
1029        return None;
1030    }
1031    Some(
1032        InPlaceError::new(
1033            "IN-PLACE-SESSION",
1034            format!(
1035                "{} is being run in place in {} (on {})",
1036                session.feature,
1037                session.checkout.display(),
1038                session.branch
1039            ),
1040        )
1041        .fix(&[
1042            "speccoder run --in-place",
1043            "speccoder run --in-place --return",
1044        ]),
1045    )
1046}
1047
1048/// `run --in-place --return` for the project at `path`, with the state in `state`.
1049#[must_use]
1050pub fn return_in_place(path: &Path, state: &Path) -> CommandResult {
1051    let root = crate::format::open(path, None).map_or_else(
1052        |_| std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf()),
1053        |o| PathBuf::from(&o.snapshot.root),
1054    );
1055    let plane_dir = worktree::project_state_dir(state, &root);
1056    match return_checkout(&plane_dir) {
1057        Ok(message) => CommandResult::new("run", speccoder_core::CommandStatus::Ok, &message),
1058        Err(e) => e.result("run"),
1059    }
1060}
1061
1062#[cfg(test)]
1063mod tests {
1064    use super::*;
1065
1066    #[test]
1067    fn only_the_engines_tick_is_a_tick() {
1068        let before = b"- [ ] T001 a\n- [ ] T002 b\n";
1069        let mut after = before.to_vec();
1070        after[3] = b'x';
1071        assert!(is_tick(before, &after));
1072        let mut two = after.clone();
1073        two[16] = b'X';
1074        assert!(!is_tick(before, &two), "two ticks are not one");
1075        let mut other = before.to_vec();
1076        other[8] = b'Z';
1077        assert!(!is_tick(before, &other), "any other byte is not a tick");
1078        assert!(!is_tick(before, b"- [ ] T001 a\n"));
1079    }
1080
1081    #[test]
1082    fn the_intent_digest_ignores_ticks_only() {
1083        assert_eq!(
1084            untick(b"- [x] T1\n- [X] T2\n- [ ] T3"),
1085            b"- [ ] T1\n- [ ] T2\n- [ ] T3"
1086        );
1087        assert_eq!(untick(b"a [xy] b"), b"a [xy] b");
1088    }
1089
1090    /// Re-checking a 2,000-file, 20 MB protected set takes at
1091    /// most a second (`cargo test --release -- --ignored`).
1092    #[test]
1093    #[ignore = "a timing check: run in release"]
1094    fn a_large_protected_set_is_rechecked_within_a_second() {
1095        let dir = tempfile::tempdir().unwrap();
1096        let chunk = "x".repeat(10 * 1024);
1097        for f in 0..40 {
1098            let feature = dir.path().join(format!("specs/{f:03}-f"));
1099            std::fs::create_dir_all(&feature).unwrap();
1100            for i in 0..50 {
1101                std::fs::write(feature.join(format!("checklist-{i}.md")), &chunk).unwrap();
1102            }
1103        }
1104        let policy = Policy::new(&[], &[]);
1105        let first = manifest(dir.path(), &policy);
1106        assert!(first.len() >= 2000, "{} files", first.len());
1107        let start = std::time::Instant::now();
1108        let again = manifest(dir.path(), &policy);
1109        let took = start.elapsed();
1110        assert!(diff(&first, &again).is_empty());
1111        assert!(took <= std::time::Duration::from_secs(1), "took {took:?}");
1112    }
1113
1114    #[test]
1115    fn manifests_diff_by_path() {
1116        let a: BTreeMap<String, String> = [("s.md", "1"), ("t.md", "2")]
1117            .iter()
1118            .map(|(p, h)| ((*p).to_owned(), (*h).to_owned()))
1119            .collect();
1120        let mut b = a.clone();
1121        b.insert("t.md".to_owned(), "3".to_owned());
1122        b.remove("s.md");
1123        b.insert("n.md".to_owned(), "4".to_owned());
1124        let d = diff(&a, &b);
1125        assert_eq!(
1126            d,
1127            [
1128                ("s.md".to_owned(), "deleted".to_owned()),
1129                ("t.md".to_owned(), "modified".to_owned()),
1130                ("n.md".to_owned(), "added".to_owned())
1131            ]
1132        );
1133        assert!(
1134            runtime_state(".specify/workflows/runs/x.json") && runtime_state("a/__pycache__/b.pyc")
1135        );
1136    }
1137}