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            story_open: &[],
547            own_test: None,
548        };
549        let saved = crate::attempts::save(&self.checkout, &tmp, &self.feature, "in-place", &record)
550            .map_err(|e| InPlaceError::new("RETURN-BLOCKED", e.0))?;
551        let git = Git::at(&self.checkout);
552        let head_tree =
553            worktree::Checkpoint(git.line(&["rev-parse", "HEAD^{tree}"]).unwrap_or_default());
554        worktree::restore(&self.checkout, &tmp, &head_tree)
555            .map_err(|e| InPlaceError::new("RETURN-BLOCKED", e.0))?;
556        Ok(Some(saved.reference))
557    }
558
559    /// The checkout guard and the manifest, before every task: HEAD where
560    /// speccoder left it, nothing changed, the protected files as recorded.
561    pub fn check(&self, policy: &Policy) -> Result<(), InPlaceError> {
562        self.check_with(policy, true)
563    }
564
565    /// [`Session::check`] before a tick: the attempt's own writes are in the
566    /// checkout then, so only HEAD and the protected files are compared.
567    pub fn check_before_tick(&self, policy: &Policy) -> Result<(), InPlaceError> {
568        self.check_with(policy, false)
569    }
570
571    fn check_with(&self, policy: &Policy, clean: bool) -> Result<(), InPlaceError> {
572        // Where the sandbox cannot keep `.git` read-only, a change is caught
573        // here — before speccoder runs git there again for a tick.
574        if !self.git.is_empty() && git_fingerprint(&self.checkout, &self.branch) != self.git {
575            return Err(InPlaceError::new(
576                "GIT-CHANGED",
577                "the repository's configuration, hooks or branches changed during the run; speccoder does not run git against them",
578            )
579            .fix(&[
580                "git -C . config --list --local   # review, restore what changed",
581                "speccoder run --in-place --return",
582            ]));
583        }
584        let git = Git::at(&self.checkout);
585        let head_ref = git
586            .line(&["symbolic-ref", "-q", "HEAD"])
587            .unwrap_or_default();
588        let head = git.line(&["rev-parse", "HEAD"]).unwrap_or_default();
589        let fix = [
590            format!("git stash -u && git switch {}", self.original.target()),
591            "speccoder run --in-place --return".to_owned(),
592        ];
593        if head_ref != format!("refs/heads/{}", self.branch) || head != self.tip {
594            let mut e = InPlaceError::new(
595                "CHECKOUT-CHANGED",
596                format!(
597                    "the checkout moved during the run (HEAD is {}, speccoder left {} at {}); nothing was switched",
598                    if head_ref.is_empty() {
599                        head.clone()
600                    } else {
601                        head_ref.clone()
602                    },
603                    self.branch,
604                    self.tip.chars().take(10).collect::<String>()
605                ),
606            );
607            e.fix = fix.to_vec();
608            return Err(e);
609        }
610        if let Some(r) = clean
611            .then(|| worktree::require_clean(&self.checkout).err())
612            .flatten()
613        {
614            let mut e = InPlaceError::new(
615                "CHECKOUT-CHANGED",
616                "the checkout changed during the run; nothing was saved, restored or switched",
617            );
618            e.paths = r.paths;
619            e.fix = fix.to_vec();
620            return Err(e);
621        }
622        let now = manifest(&self.project, policy);
623        let changed = diff(&self.manifest, &now);
624        if !changed.is_empty() {
625            let intent = changed.iter().any(|(p, _)| {
626                policy.write_protected(p) == Some(crate::tools::paths::Class::Intent)
627            });
628            let mut e = InPlaceError::new(
629                if intent {
630                    "INTENT-CHANGED"
631                } else {
632                    "JUDGE-CHANGED"
633                },
634                if intent {
635                    "the spec changed under the running judge; the run stops before judging against it"
636                } else {
637                    "a judge input changed under the running judge"
638                },
639            );
640            e.paths = changed;
641            e.fix = vec![
642                "review the change, then run again: the next run judges against the spec as it is"
643                    .to_owned(),
644            ];
645            return Err(e);
646        }
647        Ok(())
648    }
649
650    /// After a speccoder commit: the new tip.
651    pub fn advance(&mut self, plane_dir: &Path) {
652        self.tip = Git::at(&self.checkout)
653            .line(&["rev-parse", "HEAD"])
654            .unwrap_or_default();
655        let _ = self.save(plane_dir);
656    }
657
658    /// After the engine's tick or append of `file` (absolute): the manifest
659    /// takes its new hash — only when the change is exactly that (`accept`
660    /// says what the bytes before and after may be).
661    pub fn accept(
662        &mut self,
663        plane_dir: &Path,
664        file: &Path,
665        before: &[u8],
666        accept: impl Fn(&[u8], &[u8]) -> bool,
667    ) -> Result<(), InPlaceError> {
668        let after = std::fs::read(file).unwrap_or_default();
669        let rel = file
670            .strip_prefix(&self.project)
671            .map(|r| r.to_string_lossy().replace('\\', "/"))
672            .unwrap_or_default();
673        if !accept(before, &after) {
674            return Err(InPlaceError::new(
675                "INTENT-CHANGED",
676                format!("{rel} changed beyond the engine's own write"),
677            ));
678        }
679        self.manifest.insert(rel, sha256_hex(&after));
680        let _ = self.save(plane_dir);
681        Ok(())
682    }
683
684    /// Switches the checkout back to where the person was, when it is as
685    /// speccoder left it; removes the record after.
686    pub fn finish(&self, plane_dir: &Path) -> Result<(), InPlaceError> {
687        let git = Git::at(&self.checkout);
688        let head = git.line(&["rev-parse", "HEAD"]).unwrap_or_default();
689        let on_branch = git
690            .line(&["symbolic-ref", "-q", "HEAD"])
691            .unwrap_or_default()
692            == format!("refs/heads/{}", self.branch);
693        if !on_branch || head != self.tip || worktree::require_clean(&self.checkout).is_err() {
694            return Err(InPlaceError::new(
695                "RETURN-BLOCKED",
696                format!(
697                    "the checkout is not as speccoder left it, so it stays on {}",
698                    self.branch
699                ),
700            )
701            .fix(&[
702                &format!("git stash -u && git switch {}", self.original.target()),
703                "speccoder run --in-place --return",
704            ]));
705        }
706        if self.original != Original::Branch(self.branch.clone()) {
707            let back = match &self.original {
708                Original::Branch(b) => git.run(&["switch", "-q", b]),
709                Original::Detached(c) => git.run(&["switch", "-q", "--detach", c]),
710            };
711            if let Err(e) = back {
712                return Err(InPlaceError::new("RETURN-BLOCKED", e.0)
713                    .fix(&[&format!("git switch {}", self.original.target())]));
714            }
715        }
716        let _ = std::fs::remove_file(Self::path(plane_dir));
717        Ok(())
718    }
719}
720
721impl Session {
722    /// A run killed between its verdict commit and the local tick: each
723    /// open task whose newest commit names its line exactly (`Task-Line`) is
724    /// ticked now, the manifest accepting it. Returns what was ticked.
725    pub fn reconcile(
726        &mut self,
727        plane_dir: &Path,
728        tasks_file: &Path,
729        tasks: &speccoder_core::snapshot::ParsedTasks,
730        tick: impl Fn(&Path, &str, &str) -> Result<(), String>,
731    ) -> Vec<String> {
732        let log = Git::at(&self.checkout)
733            .run(&[
734                "log",
735                "--format=%(trailers:key=Spec-Task,valueonly)\u{1f}%(trailers:key=Task-Line,valueonly)\u{1e}",
736                &self.branch,
737            ])
738            .map(|b| String::from_utf8_lossy(&b).into_owned())
739            .unwrap_or_default();
740        let mut newest: BTreeMap<String, String> = BTreeMap::new();
741        for record in log.split('\u{1e}') {
742            let Some((task, line)) = record.trim().split_once('\u{1f}') else {
743                continue;
744            };
745            if let Some(id) = task.trim().strip_prefix(&format!("{}/", self.feature)) {
746                newest
747                    .entry(id.to_owned())
748                    .or_insert_with(|| line.trim().to_owned());
749            }
750        }
751        let mut ticked = Vec::new();
752        for t in tasks.tasks.iter().filter(|t| !t.done) {
753            if newest
754                .get(&t.id)
755                .is_none_or(|l| l.is_empty() || *l != t.line_hash)
756            {
757                continue;
758            }
759            let before = std::fs::read(tasks_file).unwrap_or_default();
760            if tick(tasks_file, &t.id, &t.line_hash).is_ok()
761                && self.accept(plane_dir, tasks_file, &before, is_tick).is_ok()
762            {
763                ticked.push(t.id.clone());
764            }
765        }
766        if !ticked.is_empty() {
767            self.notes.push(format!(
768                "ticked {} from {} verdict commit(s): the run was interrupted before the tick",
769                ticked.join(", "),
770                ticked.len()
771            ));
772        }
773        ticked
774    }
775}
776
777/// What committing a task in place did.
778#[derive(Debug, Default)]
779pub struct Committed {
780    /// The verdict commit.
781    pub commit: Option<String>,
782    /// Whether the task is ticked.
783    pub ticked: bool,
784    /// What went wrong, for the report's integrity list.
785    pub problems: Vec<String>,
786}
787
788impl Session {
789    /// Commits a passed (or attested) task and ticks it. The evidence comes
790    /// first: a `tasks_file` git ignores is never in a commit, so the commit
791    /// names the tick as local (`Tick: local`), and the tick follows it. A
792    /// tracked one is ticked and committed together, as in a worktree.
793    /// Every commit says it was made in place, against which intent
794    /// (`intent_digest`) and which task line (`line_hash`, before the tick).
795    pub fn commit_and_tick(
796        &mut self,
797        plane_dir: &Path,
798        tasks_file: &Path,
799        line_hash: &str,
800        message: &str,
801        intent_digest: &str,
802        tick: impl Fn() -> Result<(), String>,
803    ) -> Committed {
804        let mut out = Committed::default();
805        let rel = tasks_file
806            .strip_prefix(&self.checkout)
807            .map(|r| r.to_string_lossy().replace('\\', "/"))
808            .unwrap_or_default();
809        let tracked = Git::at(&self.checkout)
810            .succeeds(&["ls-files", "--error-unmatch", "--", &rel])
811            .unwrap_or(false);
812        let mut trailers = format!(
813            "Mode: in-place\nIntent-Digest: sha256:{intent_digest}\nTask-Line: {line_hash}\n"
814        );
815        if !tracked {
816            trailers.push_str(&format!("Tick: local {rel}\n"));
817        }
818        let message = format!("{}\n{trailers}", message.trim_end());
819        let before = std::fs::read(tasks_file).unwrap_or_default();
820        let tick_now = |out: &mut Committed| match tick() {
821            Ok(()) => out.ticked = true,
822            Err(e) => out.problems.push(format!("tick refused: {e}")),
823        };
824        if tracked {
825            tick_now(&mut out);
826        }
827        if out.ticked || !tracked {
828            match crate::pipeline::commit::commit_attempt_as(&self.checkout, &message, !tracked) {
829                Ok(id) => out.commit = Some(id),
830                Err(e) => out.problems.push(format!("commit failed: {e}")),
831            }
832        }
833        if !tracked && out.commit.is_some() {
834            tick_now(&mut out);
835        }
836        if out.ticked
837            && let Err(e) = self.accept(plane_dir, tasks_file, &before, is_tick)
838        {
839            out.problems.push(e.message);
840        }
841        self.advance(plane_dir);
842        out
843    }
844}
845
846impl Session {
847    /// Commits a convergence round in place: the append to `tasks_file`
848    /// (old bytes an exact prefix of the new) is accepted into the manifest,
849    /// and the commit says it was made in place, against which intent; a
850    /// git-ignored task list is not in it, so the commit may be empty.
851    pub fn commit_round(
852        &mut self,
853        plane_dir: &Path,
854        tasks_file: &Path,
855        before: &[u8],
856        message: &str,
857        intent_digest: &str,
858    ) -> Result<String, String> {
859        self.accept(plane_dir, tasks_file, before, |b, a| a.starts_with(b))
860            .map_err(|e| e.message)?;
861        let rel = tasks_file
862            .strip_prefix(&self.checkout)
863            .map(|r| r.to_string_lossy().replace('\\', "/"))
864            .unwrap_or_default();
865        let tracked = Git::at(&self.checkout)
866            .succeeds(&["ls-files", "--error-unmatch", "--", &rel])
867            .unwrap_or(false);
868        let mut trailers = format!("Mode: in-place\nIntent-Digest: sha256:{intent_digest}\n");
869        if !tracked {
870            trailers.push_str(&format!("Tasks: local {rel}\n"));
871        }
872        let message = format!("{}\n{trailers}", message.trim_end());
873        let id = crate::pipeline::commit::commit_attempt_as(&self.checkout, &message, !tracked)?;
874        self.advance(plane_dir);
875        Ok(id)
876    }
877
878    /// The model's view of this checkout: the policy denies git that moves
879    /// it and hides what git ignored; the sandbox keeps `.git` read-only
880    /// and the ignored secrets unreadable.
881    #[must_use]
882    pub fn confine(
883        &self,
884        policy: Policy,
885        runner: crate::sandbox::Runner,
886    ) -> (Policy, crate::sandbox::Runner, Option<String>) {
887        let secrets: Vec<PathBuf> = self
888            .hidden
889            .iter()
890            .map(|h| h.trim_end_matches('/'))
891            .filter(|h| policy.protected().is_secret(h))
892            .map(|h| self.checkout.join(h))
893            .collect();
894        let (runner, notice) = runner.carve(&[self.checkout.join(".git")], &secrets);
895        (
896            policy.in_place().with_hidden(self.hidden.clone()),
897            runner,
898            notice,
899        )
900    }
901}
902
903/// A hash over what in `.git` could make git run code or move a branch the
904/// person owns: `config`, `hooks/`, `info/`, and every branch and tag except
905/// `branch` (speccoder's own).
906fn git_fingerprint(checkout: &Path, branch: &str) -> String {
907    let git = Git::at(checkout);
908    let Some(dir) = git
909        .line(&["rev-parse", "--path-format=absolute", "--git-common-dir"])
910        .ok()
911        .map(PathBuf::from)
912    else {
913        return String::new();
914    };
915    let mut parts = Vec::new();
916    let mut files = vec![dir.join("config")];
917    for sub in ["hooks", "info"] {
918        files.extend(
919            ignore::WalkBuilder::new(dir.join(sub))
920                .standard_filters(false)
921                .build()
922                .flatten()
923                .filter(|e| e.file_type().is_some_and(|t| t.is_file()))
924                .map(ignore::DirEntry::into_path),
925        );
926    }
927    files.sort();
928    for f in files {
929        let hash = std::fs::read(&f).map_or_else(|_| "-".to_owned(), |b| sha256_hex(&b));
930        parts.push(format!("{} {hash}", f.display()));
931    }
932    let own = format!("refs/heads/{branch} ");
933    let refs = git
934        .run(&[
935            "for-each-ref",
936            "--format=%(refname) %(objectname)",
937            "refs/heads",
938            "refs/tags",
939        ])
940        .map(|b| String::from_utf8_lossy(&b).into_owned())
941        .unwrap_or_default();
942    parts.extend(
943        refs.lines()
944            .filter(|l| !l.starts_with(&own))
945            .map(str::to_owned),
946    );
947    sha256_hex(parts.join("\n").as_bytes())
948}
949
950/// Whether the journal holds an attempt of `feature` that started at `head`.
951fn resumable(plane_dir: &Path, feature: &str, head: &str) -> bool {
952    std::fs::read_dir(plane_dir.join("resume").join(feature))
953        .into_iter()
954        .flatten()
955        .flatten()
956        .filter_map(|e| crate::resume::load(&e.path()))
957        .any(|m| m.head == head)
958}
959
960/// `run --in-place --return`: saves what an interrupted run left, switches
961/// back to the person's branch, removes the record. No model call.
962pub fn return_checkout(plane_dir: &Path) -> Result<String, InPlaceError> {
963    let Some(session) = Session::load(plane_dir) else {
964        return Err(InPlaceError::new(
965            "IN-PLACE-SESSION",
966            "there is no in-place run to return from",
967        ));
968    };
969    if session.is_live() {
970        return Err(InPlaceError::new(
971            "IN-PLACE-ACTIVE",
972            format!("the in-place run (pid {}) is still running", session.pid),
973        ));
974    }
975    let git = Git::at(&session.checkout);
976    let saved = session.set_aside(
977        plane_dir,
978        "left by an interrupted in-place run; saved by --return",
979    )?;
980    let fresh = Session {
981        tip: git.line(&["rev-parse", "HEAD"]).unwrap_or_default(),
982        ..session.clone()
983    };
984    fresh.finish(plane_dir)?;
985    Ok(match saved {
986        Some(r) => format!(
987            "back on {}; what the interrupted run left is saved as {r}",
988            session.original.describe()
989        ),
990        None => format!("back on {}", session.original.describe()),
991    })
992}
993
994/// The project's in-place session, if one is open or was left by a run
995/// that died: what `status` and the TUI show.
996#[must_use]
997pub fn session_at(path: &Path, state: &Path) -> Option<Session> {
998    let root = crate::format::open(path, None).map_or_else(
999        |_| std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf()),
1000        |o| PathBuf::from(&o.snapshot.root),
1001    );
1002    Session::load(&worktree::project_state_dir(state, &root))
1003}
1004
1005impl Session {
1006    /// `in-place · speccoder/<f> in <checkout> (returns to <original>)`.
1007    #[must_use]
1008    pub fn mode_line(&self) -> String {
1009        format!(
1010            "in-place · {} in {} (returns to {})",
1011            self.branch,
1012            self.checkout.display(),
1013            self.original.describe()
1014        )
1015    }
1016}
1017
1018/// A worktree-mode run of a feature that an in-place session holds: the
1019/// branch is checked out in the person's checkout, so it is refused.
1020#[must_use]
1021pub fn session_elsewhere(path: &Path, feature: Option<&str>, state: &Path) -> Option<InPlaceError> {
1022    let opened = crate::format::open(path, feature).ok()?;
1023    let plane_dir = worktree::project_state_dir(state, Path::new(&opened.snapshot.root));
1024    let session = Session::load(&plane_dir)?;
1025    let wanted = opened
1026        .snapshot
1027        .feature
1028        .as_ref()
1029        .map(|f| f.feature.name.as_str());
1030    if wanted.is_some_and(|w| w != session.feature) {
1031        return None;
1032    }
1033    Some(
1034        InPlaceError::new(
1035            "IN-PLACE-SESSION",
1036            format!(
1037                "{} is being run in place in {} (on {})",
1038                session.feature,
1039                session.checkout.display(),
1040                session.branch
1041            ),
1042        )
1043        .fix(&[
1044            "speccoder run --in-place",
1045            "speccoder run --in-place --return",
1046        ]),
1047    )
1048}
1049
1050/// `run --in-place --return` for the project at `path`, with the state in `state`.
1051#[must_use]
1052pub fn return_in_place(path: &Path, state: &Path) -> CommandResult {
1053    let root = crate::format::open(path, None).map_or_else(
1054        |_| std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf()),
1055        |o| PathBuf::from(&o.snapshot.root),
1056    );
1057    let plane_dir = worktree::project_state_dir(state, &root);
1058    match return_checkout(&plane_dir) {
1059        Ok(message) => CommandResult::new("run", speccoder_core::CommandStatus::Ok, &message),
1060        Err(e) => e.result("run"),
1061    }
1062}
1063
1064#[cfg(test)]
1065mod tests {
1066    use super::*;
1067
1068    #[test]
1069    fn only_the_engines_tick_is_a_tick() {
1070        let before = b"- [ ] T001 a\n- [ ] T002 b\n";
1071        let mut after = before.to_vec();
1072        after[3] = b'x';
1073        assert!(is_tick(before, &after));
1074        let mut two = after.clone();
1075        two[16] = b'X';
1076        assert!(!is_tick(before, &two), "two ticks are not one");
1077        let mut other = before.to_vec();
1078        other[8] = b'Z';
1079        assert!(!is_tick(before, &other), "any other byte is not a tick");
1080        assert!(!is_tick(before, b"- [ ] T001 a\n"));
1081    }
1082
1083    #[test]
1084    fn the_intent_digest_ignores_ticks_only() {
1085        assert_eq!(
1086            untick(b"- [x] T1\n- [X] T2\n- [ ] T3"),
1087            b"- [ ] T1\n- [ ] T2\n- [ ] T3"
1088        );
1089        assert_eq!(untick(b"a [xy] b"), b"a [xy] b");
1090    }
1091
1092    /// Re-checking a 2,000-file, 20 MB protected set takes at
1093    /// most a second (`cargo test --release -- --ignored`).
1094    #[test]
1095    #[ignore = "a timing check: run in release"]
1096    fn a_large_protected_set_is_rechecked_within_a_second() {
1097        let dir = tempfile::tempdir().unwrap();
1098        let chunk = "x".repeat(10 * 1024);
1099        for f in 0..40 {
1100            let feature = dir.path().join(format!("specs/{f:03}-f"));
1101            std::fs::create_dir_all(&feature).unwrap();
1102            for i in 0..50 {
1103                std::fs::write(feature.join(format!("checklist-{i}.md")), &chunk).unwrap();
1104            }
1105        }
1106        let policy = Policy::new(&[], &[]);
1107        let first = manifest(dir.path(), &policy);
1108        assert!(first.len() >= 2000, "{} files", first.len());
1109        let start = std::time::Instant::now();
1110        let again = manifest(dir.path(), &policy);
1111        let took = start.elapsed();
1112        assert!(diff(&first, &again).is_empty());
1113        assert!(took <= std::time::Duration::from_secs(1), "took {took:?}");
1114    }
1115
1116    #[test]
1117    fn manifests_diff_by_path() {
1118        let a: BTreeMap<String, String> = [("s.md", "1"), ("t.md", "2")]
1119            .iter()
1120            .map(|(p, h)| ((*p).to_owned(), (*h).to_owned()))
1121            .collect();
1122        let mut b = a.clone();
1123        b.insert("t.md".to_owned(), "3".to_owned());
1124        b.remove("s.md");
1125        b.insert("n.md".to_owned(), "4".to_owned());
1126        let d = diff(&a, &b);
1127        assert_eq!(
1128            d,
1129            [
1130                ("s.md".to_owned(), "deleted".to_owned()),
1131                ("t.md".to_owned(), "modified".to_owned()),
1132                ("n.md".to_owned(), "added".to_owned())
1133            ]
1134        );
1135        assert!(
1136            runtime_state(".specify/workflows/runs/x.json") && runtime_state("a/__pycache__/b.pyc")
1137        );
1138    }
1139}