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 /// The registry-recorded content digest of `package@version` — for crates.io
102 /// the sparse-index `cksum`, the lowercase-hex SHA-256 of the published
103 /// `.crate` tarball.
104 ///
105 /// Used to **authenticate an idempotency skip**: a resumed publish that finds
106 /// the version already on the registry trusts that "already published" answer
107 /// only when this digest matches the digest of the artifact the cut would
108 /// upload, so the registry's crate is proven byte-identical to the intended
109 /// one before the publish is skipped (see
110 /// [`crate::release::adapters::cargo`]). Name + version existence alone is not
111 /// enough — a *different* artifact could occupy the version.
112 ///
113 /// **Fail-closed, like [`Self::published_versions`].** `Ok(<hex>)` is the
114 /// recorded digest; an outage, a version that is not on the index, or a
115 /// registry that exposes no digest for the version is an `Err` — never a
116 /// fabricated value that could mask a mismatch as a match. The default
117 /// implementation errors ([`io::ErrorKind::Unsupported`]): a backend wires this
118 /// only for the ecosystems whose skip path is digest-authenticated (crates.io
119 /// today), and the sole caller is the cargo adapter's resume skip.
120 fn published_checksum(
121 &self,
122 ecosystem: &str,
123 package: &str,
124 version: &str,
125 ) -> io::Result<String> {
126 let _ = (ecosystem, package, version);
127 Err(io::Error::new(
128 io::ErrorKind::Unsupported,
129 "this registry query does not expose published checksums",
130 ))
131 }
132}
133
134/// Read-only view of the git repository under audit/release. The detector and
135/// release engine read repo facts through this port rather than shelling out
136/// to `git` themselves.
137///
138/// The detector's queries are deliberately *best-effort*: an unborn or
139/// non-repository root makes several of these fail, and the detector treats
140/// every such failure as "absent" (no committers, no tags) rather than an
141/// error — mirroring the read-only, never-mutating `infer-repo-facts.py`.
142pub trait GitRepo {
143 /// The full commit hash of `HEAD`. `Err` on an unborn repository (no
144 /// commits yet) or a non-repository root — the detector reads this as
145 /// "the repo has no commits".
146 fn head_commit(&self) -> io::Result<String>;
147 /// Whether the root is inside a git work tree (`git rev-parse
148 /// --is-inside-work-tree` succeeds). `false` for a non-repository root or
149 /// on any git error.
150 fn is_work_tree(&self) -> bool;
151 /// Raw `git shortlog -sne --all HEAD` output — one line per distinct
152 /// committer. When `since` is set (a git date expression such as
153 /// `"1 year ago"`), the log is limited to commits at or after it. `Err` on
154 /// any git failure (an unborn/empty repo); the detector counts that as zero
155 /// committers.
156 fn shortlog(&self, since: Option<&str>) -> io::Result<String>;
157 /// The repository's tag names (`git tag --list`), trimmed and with empty
158 /// lines dropped. `Err` on any git failure; the detector reads that as "no
159 /// tags".
160 fn tags(&self) -> io::Result<Vec<String>>;
161 /// The **common** git directory (`git rev-parse --git-common-dir`), resolved
162 /// to an absolute path. The release journal roots its state under here so all
163 /// linked worktrees of one repo share a single release-state root, and
164 /// submodules / bare repos / `GIT_DIR` overrides resolve correctly — a
165 /// literal `.git/` concatenation is *not* a portable substitute (ADR-0003
166 /// §3, the panel's repeated correctness landmine). `Err` on any git failure.
167 fn git_common_dir(&self) -> io::Result<std::path::PathBuf>;
168}
169
170/// Durable, atomic, lockable storage for the release journal — the seam that
171/// keeps the event-sourced journal (ADR-0003) testable without touching the real
172/// filesystem, while pinning down the atomicity discipline its production impl
173/// **must** honor.
174///
175/// The append-then-apply contract (ADR-0003 §2, borrowed from `octl-core`) maps
176/// onto these operations:
177///
178/// 1. [`Self::append_line`] fsyncs the event so it is durable **before** the
179/// reducer applies it — a crash between append and apply replays as a clean
180/// no-op-or-apply, because the journal (read back by [`Self::read_lines`]) is
181/// the single source of truth.
182/// 2. [`Self::write_atomic`] persists the derived manifest via temp-file → flush
183/// → atomic rename → directory fsync, so a torn write can never leave a
184/// half-written manifest (it is disposable and rebuildable regardless).
185/// 3. [`Self::lock_exclusive`] enforces a single active cut per repo (a `flock`
186/// on the releases-dir `.lock`): a concurrent cut/resume fails fast rather
187/// than corrupting a run.
188///
189/// The port is deliberately path-driven (the journal computes paths via
190/// [`crate::release::journal::JournalPaths`]); the impl adds no policy, only the
191/// durability guarantees documented per method.
192pub trait JournalStore {
193 /// Take the single-active-cut exclusive lock at `lock_path` (creating parent
194 /// directories as needed). The returned guard holds the lock until dropped.
195 /// `Err` with [`io::ErrorKind::WouldBlock`] when another holder is active, so
196 /// the caller can fail fast and name the active run.
197 fn lock_exclusive(&self, lock_path: &std::path::Path) -> io::Result<Box<dyn JournalLock>>;
198 /// Append `line` (one serialized event, no embedded newline — the store adds
199 /// the single trailing `\n`) to the JSONL file at `path`, creating the file
200 /// and parent directories if absent, and **fsync** so it is durable before
201 /// returning. This is the append half of append-then-apply.
202 ///
203 /// The write must be **atomic at line granularity**: on return the line is
204 /// either fully present or not present, never truncated. The production impl
205 /// opens with `O_APPEND`, `write_all`s the line + newline, and fsyncs the file
206 /// **and** — when it created the file or a parent directory — the containing
207 /// directory, so a newly created `RunCreated` survives power loss (fsyncing
208 /// only the file leaves the new directory entry non-durable). A torn *final*
209 /// line from a hard kill mid-write is still possible in theory; recovering it
210 /// (truncate-to-last-good under the lock) is a documented follow-up, not part
211 /// of this port yet — [`crate::release::journal::read_events`] currently
212 /// rejects any malformed line.
213 fn append_line(&self, path: &std::path::Path, line: &str) -> io::Result<()>;
214 /// Read every line of the JSONL file at `path`. `Ok(vec![])` when the file is
215 /// absent (a not-yet-written journal is empty, not an error).
216 fn read_lines(&self, path: &std::path::Path) -> io::Result<Vec<String>>;
217 /// Read the full contents of the (atomically-written) file at `path`, or
218 /// `Ok(None)` when it is absent. Used for the torn-free fast-path read of the
219 /// `manifest.json` cache; unlike [`Self::read_lines`] it returns raw bytes.
220 fn read(&self, path: &std::path::Path) -> io::Result<Option<Vec<u8>>>;
221 /// Atomically replace the file at `path` with `bytes`: write a temp file in
222 /// the same directory, flush + fsync it, rename it over `path`, then fsync the
223 /// directory. Creates parent directories as needed.
224 fn write_atomic(&self, path: &std::path::Path, bytes: &[u8]) -> io::Result<()>;
225 /// The immediate entry *names* (not full paths) within `dir`, or `Ok(vec![])`
226 /// when `dir` is absent — used to enumerate run-id subdirectories for
227 /// `release list`.
228 fn list_dir(&self, dir: &std::path::Path) -> io::Result<Vec<String>>;
229}
230
231/// An opaque RAII guard for the single-active-cut lock taken by
232/// [`JournalStore::lock_exclusive`]. Dropping it releases the lock; there are no
233/// methods — its lifetime *is* the contract.
234pub trait JournalLock {}
235
236/// Creates and publishes the **one** shared release tag + GitHub Release for a
237/// cut — the external side of the coordinator's coordinator-only tag phase
238/// (ADR-0002 §2).
239///
240/// Tagging is deliberately **not** on the [`crate::release::adapters::ReleaseAdapter`]
241/// trait: no per-ecosystem adapter can create the shared tag, which is what makes
242/// "tag once, after every publish succeeds" a structural guarantee. The
243/// coordinator drives these three steps in order and journals each as its own
244/// resumable event (`tag_created_local` → `tag_pushed_remote` →
245/// `github_release_created`), so an interrupted tag phase resumes from the first
246/// incomplete step rather than re-tagging.
247///
248/// Every method is **idempotent-friendly**: the coordinator only calls a step
249/// whose journalled fact is not yet present, but a production impl should still
250/// treat "already exists" as success (a pushed tag that is already on the remote,
251/// a Release that already exists) rather than an error, so a resume after a crash
252/// *between* the external action and its journal write reconciles cleanly.
253pub trait Tagger {
254 /// Create the annotated tag `tag` (message `message`) pointing at the sealed
255 /// `commit` in the local repository.
256 ///
257 /// `commit` is the plan's sealed `HEAD` (not whatever `HEAD` happens to be at
258 /// tag time), so the tag authenticates the approved commit even if `HEAD`
259 /// moved during the cut. Idempotent-friendly: if the tag already exists **at
260 /// `commit`** this is success (a resumed cut re-reaching this step after a
261 /// crash between the tag and its journal write); a tag that exists pointing
262 /// **elsewhere** is a genuine conflict (`Err`), never silently overwritten.
263 fn create_tag(&self, tag: &str, commit: &str, message: &str) -> io::Result<()>;
264 /// Push the already-created tag `tag` to the remote. Idempotent-friendly: an
265 /// already-present identical remote tag is success; `Err` on a real push
266 /// failure (network, auth, a conflicting remote ref).
267 fn push_tag(&self, tag: &str) -> io::Result<()>;
268 /// Create the GitHub Release for `tag` (titled `title`), returning its URL
269 /// when the host reports one. Idempotent-friendly: an already-existing Release
270 /// is success (returning its URL); `Err` on a real creation failure.
271 fn create_github_release(&self, tag: &str, title: &str) -> io::Result<Option<String>>;
272}