ossctl_core/ports.rs
1//! Injected effect ports — the seam that keeps every domain testable in
2//! isolation without touching the real filesystem, git, network, or clock
3//! (ADR-0001 §2).
4//!
5//! Domain code (`contract`, `facts`, `audit`, `release`) takes these traits by
6//! reference instead of calling `std::process`, `std::time`, or the network
7//! directly. Production supplies real implementations in `ossctl-cli`; tests
8//! supply deterministic fakes. At founding these are the **trait shapes** only;
9//! concrete implementations land with their consuming units.
10
11use std::io;
12
13/// Output of a subprocess run through a [`CommandRunner`].
14#[derive(Debug, Clone)]
15pub struct CommandOutput {
16 /// Process exit status code (`None` if terminated by a signal).
17 pub status: Option<i32>,
18 /// Captured standard output.
19 pub stdout: String,
20 /// Captured standard error.
21 pub stderr: String,
22}
23
24/// Runs external commands (git, package-manager, registry CLIs) on behalf of a
25/// domain, capturing their output. The single seam for shelling out — nothing
26/// in `ossctl-core` calls `std::process::Command` directly.
27pub trait CommandRunner {
28 /// Run `program` with `args` in `cwd`, capturing stdout/stderr.
29 fn run(&self, program: &str, args: &[&str], cwd: &std::path::Path)
30 -> io::Result<CommandOutput>;
31}
32
33/// Supplies the current time. Injected so time-dependent logic (journal
34/// timestamps, run ages) is deterministic under test.
35pub trait Clock {
36 /// Current time as whole seconds since the Unix epoch.
37 fn now_unix(&self) -> u64;
38
39 /// Block for `dur` before returning — the passage of real time, injected so
40 /// waits are deterministic and instant under test.
41 ///
42 /// The release engine's crates.io index-wait (the multi-crate workspace
43 /// publish path, [`crate::release::adapters::cargo`]) polls between attempts
44 /// through this method. The default performs a genuine
45 /// [`std::thread::sleep`], so the production [`Clock`] waits for real without
46 /// implementing anything extra; a deterministic test fake overrides it to
47 /// advance a virtual clock instead of sleeping, so a bounded-wait loop
48 /// terminates instantly and without a real delay.
49 fn sleep(&self, dur: std::time::Duration) {
50 std::thread::sleep(dur);
51 }
52}
53
54/// Generates opaque, unique, non-deterministic identifiers — run ids and the
55/// like. Injected so id-dependent output is deterministic under test.
56///
57/// Note: this is **not** the source of `plan_id`. A release plan id is
58/// *content-addressed* — derived deterministically from the sealed plan's
59/// canonical bytes (ADR-0002), not generated here. Do not route plan sealing
60/// through this port.
61pub trait IdGen {
62 /// Produce a fresh, unique identifier.
63 fn new_id(&self) -> String;
64}
65
66/// Read/write access to the filesystem — the `Fs` half of the `Fs/Git` seam
67/// (ADR-0001 §2). Domain code (contract loading, journal persistence, sealed
68/// plans) goes through this port rather than calling `std::fs` directly, so it
69/// is testable against an in-memory fake. At founding this is a deliberately
70/// small surface; it grows (atomic writes, dir listing, metadata) as the
71/// journal and plan-sealing units land.
72pub trait Fs {
73 /// Read a file's full contents as bytes.
74 fn read(&self, path: &std::path::Path) -> io::Result<Vec<u8>>;
75 /// Whether `path` exists.
76 fn exists(&self, path: &std::path::Path) -> bool;
77 /// Whether `path` exists and is a directory (used for the contract's
78 /// fragment-dir producer check, which is a *directory*, not a file).
79 fn is_dir(&self, path: &std::path::Path) -> bool;
80 /// Whether `path` exists and is a *regular file* — not a directory, FIFO,
81 /// socket, or device (mirrors `os.path.isfile`). The facts detector gates
82 /// every manifest/config read on this so a non-regular node named
83 /// `Cargo.toml` neither marks an ecosystem nor blocks [`Self::read`] (a
84 /// blocking `read` on a FIFO is a real hang the `exists && !is_dir`
85 /// approximation would not prevent).
86 fn is_file(&self, path: &std::path::Path) -> bool;
87 /// List the immediate entry *names* (not full paths) within `dir` — the
88 /// facts detector's CI probe needs to know whether `.github/workflows`
89 /// holds at least one entry, not merely that the directory exists.
90 /// `Ok(vec![])` for an empty directory; `Err` when `dir` is absent or
91 /// unreadable (the detector treats that as "no entries").
92 fn read_dir(&self, dir: &std::path::Path) -> io::Result<Vec<String>>;
93}
94
95/// Queries a package registry for already-published state — the "remote is
96/// ground truth" source the release reconciler consults (ADR-0003).
97pub trait RegistryQuery {
98 /// Versions of `package` already published to `ecosystem`'s registry.
99 fn published_versions(&self, ecosystem: &str, package: &str) -> io::Result<Vec<String>>;
100}
101
102/// Read-only view of the git repository under audit/release. The detector and
103/// release engine read repo facts through this port rather than shelling out
104/// to `git` themselves.
105///
106/// The detector's queries are deliberately *best-effort*: an unborn or
107/// non-repository root makes several of these fail, and the detector treats
108/// every such failure as "absent" (no committers, no tags) rather than an
109/// error — mirroring the read-only, never-mutating `infer-repo-facts.py`.
110pub trait GitRepo {
111 /// The full commit hash of `HEAD`. `Err` on an unborn repository (no
112 /// commits yet) or a non-repository root — the detector reads this as
113 /// "the repo has no commits".
114 fn head_commit(&self) -> io::Result<String>;
115 /// Whether the root is inside a git work tree (`git rev-parse
116 /// --is-inside-work-tree` succeeds). `false` for a non-repository root or
117 /// on any git error.
118 fn is_work_tree(&self) -> bool;
119 /// Raw `git shortlog -sne --all HEAD` output — one line per distinct
120 /// committer. When `since` is set (a git date expression such as
121 /// `"1 year ago"`), the log is limited to commits at or after it. `Err` on
122 /// any git failure (an unborn/empty repo); the detector counts that as zero
123 /// committers.
124 fn shortlog(&self, since: Option<&str>) -> io::Result<String>;
125 /// The repository's tag names (`git tag --list`), trimmed and with empty
126 /// lines dropped. `Err` on any git failure; the detector reads that as "no
127 /// tags".
128 fn tags(&self) -> io::Result<Vec<String>>;
129 /// The **common** git directory (`git rev-parse --git-common-dir`), resolved
130 /// to an absolute path. The release journal roots its state under here so all
131 /// linked worktrees of one repo share a single release-state root, and
132 /// submodules / bare repos / `GIT_DIR` overrides resolve correctly — a
133 /// literal `.git/` concatenation is *not* a portable substitute (ADR-0003
134 /// §3, the panel's repeated correctness landmine). `Err` on any git failure.
135 fn git_common_dir(&self) -> io::Result<std::path::PathBuf>;
136}
137
138/// Durable, atomic, lockable storage for the release journal — the seam that
139/// keeps the event-sourced journal (ADR-0003) testable without touching the real
140/// filesystem, while pinning down the atomicity discipline its production impl
141/// **must** honor.
142///
143/// The append-then-apply contract (ADR-0003 §2, borrowed from `octl-core`) maps
144/// onto these operations:
145///
146/// 1. [`Self::append_line`] fsyncs the event so it is durable **before** the
147/// reducer applies it — a crash between append and apply replays as a clean
148/// no-op-or-apply, because the journal (read back by [`Self::read_lines`]) is
149/// the single source of truth.
150/// 2. [`Self::write_atomic`] persists the derived manifest via temp-file → flush
151/// → atomic rename → directory fsync, so a torn write can never leave a
152/// half-written manifest (it is disposable and rebuildable regardless).
153/// 3. [`Self::lock_exclusive`] enforces a single active cut per repo (a `flock`
154/// on the releases-dir `.lock`): a concurrent cut/resume fails fast rather
155/// than corrupting a run.
156///
157/// The port is deliberately path-driven (the journal computes paths via
158/// [`crate::release::journal::JournalPaths`]); the impl adds no policy, only the
159/// durability guarantees documented per method.
160pub trait JournalStore {
161 /// Take the single-active-cut exclusive lock at `lock_path` (creating parent
162 /// directories as needed). The returned guard holds the lock until dropped.
163 /// `Err` with [`io::ErrorKind::WouldBlock`] when another holder is active, so
164 /// the caller can fail fast and name the active run.
165 fn lock_exclusive(&self, lock_path: &std::path::Path) -> io::Result<Box<dyn JournalLock>>;
166 /// Append `line` (one serialized event, no embedded newline — the store adds
167 /// the single trailing `\n`) to the JSONL file at `path`, creating the file
168 /// and parent directories if absent, and **fsync** so it is durable before
169 /// returning. This is the append half of append-then-apply.
170 ///
171 /// The write must be **atomic at line granularity**: on return the line is
172 /// either fully present or not present, never truncated. The production impl
173 /// opens with `O_APPEND`, `write_all`s the line + newline, and fsyncs the file
174 /// **and** — when it created the file or a parent directory — the containing
175 /// directory, so a newly created `RunCreated` survives power loss (fsyncing
176 /// only the file leaves the new directory entry non-durable). A torn *final*
177 /// line from a hard kill mid-write is still possible in theory; recovering it
178 /// (truncate-to-last-good under the lock) is a documented follow-up, not part
179 /// of this port yet — [`crate::release::journal::read_events`] currently
180 /// rejects any malformed line.
181 fn append_line(&self, path: &std::path::Path, line: &str) -> io::Result<()>;
182 /// Read every line of the JSONL file at `path`. `Ok(vec![])` when the file is
183 /// absent (a not-yet-written journal is empty, not an error).
184 fn read_lines(&self, path: &std::path::Path) -> io::Result<Vec<String>>;
185 /// Read the full contents of the (atomically-written) file at `path`, or
186 /// `Ok(None)` when it is absent. Used for the torn-free fast-path read of the
187 /// `manifest.json` cache; unlike [`Self::read_lines`] it returns raw bytes.
188 fn read(&self, path: &std::path::Path) -> io::Result<Option<Vec<u8>>>;
189 /// Atomically replace the file at `path` with `bytes`: write a temp file in
190 /// the same directory, flush + fsync it, rename it over `path`, then fsync the
191 /// directory. Creates parent directories as needed.
192 fn write_atomic(&self, path: &std::path::Path, bytes: &[u8]) -> io::Result<()>;
193 /// The immediate entry *names* (not full paths) within `dir`, or `Ok(vec![])`
194 /// when `dir` is absent — used to enumerate run-id subdirectories for
195 /// `release list`.
196 fn list_dir(&self, dir: &std::path::Path) -> io::Result<Vec<String>>;
197}
198
199/// An opaque RAII guard for the single-active-cut lock taken by
200/// [`JournalStore::lock_exclusive`]. Dropping it releases the lock; there are no
201/// methods — its lifetime *is* the contract.
202pub trait JournalLock {}
203
204/// Creates and publishes the **one** shared release tag + GitHub Release for a
205/// cut — the external side of the coordinator's coordinator-only tag phase
206/// (ADR-0002 §2).
207///
208/// Tagging is deliberately **not** on the [`crate::release::adapters::ReleaseAdapter`]
209/// trait: no per-ecosystem adapter can create the shared tag, which is what makes
210/// "tag once, after every publish succeeds" a structural guarantee. The
211/// coordinator drives these three steps in order and journals each as its own
212/// resumable event (`tag_created_local` → `tag_pushed_remote` →
213/// `github_release_created`), so an interrupted tag phase resumes from the first
214/// incomplete step rather than re-tagging.
215///
216/// Every method is **idempotent-friendly**: the coordinator only calls a step
217/// whose journalled fact is not yet present, but a production impl should still
218/// treat "already exists" as success (a pushed tag that is already on the remote,
219/// a Release that already exists) rather than an error, so a resume after a crash
220/// *between* the external action and its journal write reconciles cleanly.
221pub trait Tagger {
222 /// Create the annotated tag `tag` (message `message`) pointing at the sealed
223 /// `commit` in the local repository.
224 ///
225 /// `commit` is the plan's sealed `HEAD` (not whatever `HEAD` happens to be at
226 /// tag time), so the tag authenticates the approved commit even if `HEAD`
227 /// moved during the cut. Idempotent-friendly: if the tag already exists **at
228 /// `commit`** this is success (a resumed cut re-reaching this step after a
229 /// crash between the tag and its journal write); a tag that exists pointing
230 /// **elsewhere** is a genuine conflict (`Err`), never silently overwritten.
231 fn create_tag(&self, tag: &str, commit: &str, message: &str) -> io::Result<()>;
232 /// Push the already-created tag `tag` to the remote. Idempotent-friendly: an
233 /// already-present identical remote tag is success; `Err` on a real push
234 /// failure (network, auth, a conflicting remote ref).
235 fn push_tag(&self, tag: &str) -> io::Result<()>;
236 /// Create the GitHub Release for `tag` (titled `title`), returning its URL
237 /// when the host reports one. Idempotent-friendly: an already-existing Release
238 /// is success (returning its URL); `Err` on a real creation failure.
239 fn create_github_release(&self, tag: &str, title: &str) -> io::Result<Option<String>>;
240}