Skip to content

072 increment 2: name a missing output directory before reading - #592

Merged
bartekus merged 1 commit into
mainfrom
impl/072-inc2-outdir
Nov 11, 2026
Merged

bartekus merged 1 commit into
mainfrom
impl/072-inc2-outdir

Conversation

@bartekus

@bartekus bartekus commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Spec 072 increment 2 (3.2, R-1). Stacked on #588.

rustev eval --out base with no base/ used to fail late with io_error naming base/dataset.json and the operating system's "No such file or directory". Spec 006 3.3.3's rule that output directories must already exist is unchanged; the refusal now comes first and names the directory.

  • crates/rustev-cli/src/outputs.rs works out, from the parsed flags, every directory a command writes into: the --out directory of eval, eval candidates, eval diff, eval repeat import and calibrate fit; the parent of every output file (--out, --markdown, --record-out, --bundle-out, --ledger-out); eval live's run directory parent and journal parent (unless --require-existing-journal); and run --retain external's --store.
  • Each command calls it after its usage checks (so usage errors keep exit 2) and before its first read and its no-clobber checks. A missing directory is exit 3 io_error, path the directory as given, detail output directory does not exist; a regular file in its place is is not a directory.
  • CLI host only: the surface crate and the Node and WebAssembly hosts are unchanged.

Tests: tests/io.rs adds the missing and not-a-directory cases for eval, run --bundle-out and plan compile, each checking exit 3, the full stdout, empty stderr and that nothing was created, plus an existing directory still reaching the original input refusal. Three existing tests relied on the old late failure and were adjusted: plan.rs now expects the directory in path; run.rs's post-decision bundle failure uses a read-only directory instead of a missing one (unix only); eval.rs creates the directory it writes into. No golden or conformance case changed.

Local: cargo test -p rustev-cli (full crate), cargo test -p rustev-conformance, clippy and fmt pass; make gate passes; spec-spine couple reports no drift.

Review note (cf571d9): d03e1826d39d is a false positive. For somefile/subdir, the directory named does not exist (it cannot, since somefile is a file), so output directory does not exist is accurate. Spec 072 3.2.2 reserves is not a directory for the path given being something other than a directory, which the code reports for both file and file/; a non-directory ancestor is not that path.

Review note (7960488): 58c5fc3661a4 does not apply to a shipped target. The rustev CLI is released for darwin and linux only (the win32-x64-msvc build in release.yml is the Node binding, which does not use outputs.rs), and on those targets / is the only separator. The trailing-separator branch only refines file/ to is not a directory; a missing directory still gets output directory does not exist through NotFound.

@github-actions

Copy link
Copy Markdown

AI review (no-findings)

Reviewing the PR, which adds an output-directory pre-check (outputs::check) across CLI command handlers, backed by a new crates/rustev-cli/src/outputs.rs module and spec 072 increment 2.

outputs.rs logic

OUT_DIRECTORY correctly lists the five commands whose --out names a directory; every other command's --out goes through parent() to check the containing directory. The OUTPUT_FILES array covers markdown, record-out, bundle-out, ledger-out. Special cases for eval live journal and run --retain external store are handled with explicit path comparisons.

check_directory handles: directory passes; non-directory returns "is not a directory"; NotFound/NotADirectory on the original path triggers a secondary probe after trimming trailing slashes, correctly distinguishing "is not a directory" (probe found a non-dir) from "output directory does not exist" (everything else). The unit test covers all branches including "dir/file/" forms.

Insertion points

All callers insert outputs::check after usage-level checks (which return Usage, not Done) and before the first file read and no-clobber checks. The pattern is uniform across calibrate, eval, eval views, eval judge, eval live, plan compile, plan diff, repeat eval/baseline/compare/select/import, and run execute. The ordering invariant is correctly preserved.

Test changes

tests/eval.rs: creates the refused/ directory so the adapter refusal, not the directory pre-check, fires — consistent with the test's intent (spec 014, 3.2.2).

tests/run.rs: replaces the missing missing-dir/ with a read-only read-only/ directory; mode is restored before assertions to avoid cleanup issues. The test is correctly gated #[cfg(unix)].

tests/plan.rs: the existing assertion j["path"] == "no-dir/p.json" (missing input) is correctly updated to j["path"] == "no-dir" (missing output directory) since outputs::check now fires first.

tests/io.rs: new tests cover eval --out missing/, run --bundle-out missing/x.json, plan compile --out missing/plan.json, a regular file given as the directory, and an existing directory keeping the input refusal. These use absent input files so a directory-named refusal proves nothing was read.

Spec 072 increment 2

The prose accurately describes the implementation; the claim "No golden or conformance case changed" is consistent with the changed paths.

No concrete defects found.

{"head": "972722b43a9dcc6a397b0143400960613cac604d", "verdict": "no-findings", "findings": []}

Subject: 4e575a1...972722b, diff digest 7157e86aa1daa6acf8aa03e3eab2b708e980093b4cf9f96db778b1dd5c42100f, 1 call(s). An AI comment is not an approval.

Base automatically changed from spec/072-documentation-source-gaps to main November 10, 2026 23:34
@bartekus
bartekus force-pushed the impl/072-inc2-outdir branch from 972722b to 55056be Compare November 10, 2026 23:35
@bartekus
bartekus enabled auto-merge November 10, 2026 23:35
@github-actions

Copy link
Copy Markdown

AI review (no-findings)

Reviewing the PR which adds output-directory pre-checks to the CLI (crates/rustev-cli/src/outputs.rs) per spec 072, section 3.2.

outputs.rs logic

check_directory correctly handles:

  • Existing directory → Ok(())
  • Regular file at "file/" (trailing slash, ENOTDIR from OS) → trims slash, re-stats "file", returns "is not a directory"
  • Missing path → "output directory does not exist"
  • "file/subdir" where file is a regular file → double ENOTDIR, falls to _ arm → "output directory does not exist" (matches test expectation)

The !trimmed.is_empty() guard in the inner match is dead code: a bare "/" as input would succeed the first Ok(m) if m.is_dir() arm (root always exists and is a directory) and never reach the Err branch. Harmless but unreachable.

Call-site ordering

Each handler calls crate::outputs::check(p)?; after usage/validation guards and before the first file read or no-clobber check. This preserves the documented precedence (usage error > directory-missing > no-clobber).

OUT_DIRECTORY coverage

["eval", "repeat"] is absent from OUT_DIRECTORY. repeat::eval adds the check, so for eval repeat --out <value>, the code takes the parent(out) branch instead of treating out itself as a directory. The spec's increment 2 description explicitly lists only eval, eval candidates, eval diff, eval repeat import, and calibrate fit as directory-output commands, so this is consistent with the spec; eval repeat --out names a file.

Test updates

  • eval.rs: creates "refused/" directory before the args so the existing adapter-refusal test (spec 014, 3.2.2) is not shadowed by the new directory check. Correct.
  • run.rs: adds #[cfg(unix)] and uses a read-only directory (mode 555) so the directory check passes while the write still fails post-decision. Correct.
  • plan.rs: updates assertion from "no-dir/p.json" to "no-dir" with the custom message. Correct.
  • io.rs: new integration tests cover all stated cases (missing dir, file-as-dir, parent-of-output-file, existing dir keeps input refusal).

Security

metadata follows symlinks — if --out is a symlink to a directory the check passes, which is correct. No command injection or path traversal surface introduced.

No concrete defects found.

{"head": "55056be311a54bafd1ae9b32477ef0706d60de95", "verdict": "no-findings", "findings": []}

Subject: 589185f...55056be, diff digest 7157e86aa1daa6acf8aa03e3eab2b708e980093b4cf9f96db778b1dd5c42100f, 1 call(s). An AI comment is not an approval.

@bartekus
bartekus force-pushed the impl/072-inc2-outdir branch from 55056be to c5cf056 Compare November 11, 2026 00:44
@github-actions

Copy link
Copy Markdown

AI review (no-findings)

Reviewing the PR, which adds an output-directory precheck (src/outputs.rs) that verifies output directories exist before any input is read, plus call sites across CLI command handlers and corresponding test updates.

src/outputs.rs logic

  • parent() correctly returns None for files in the working directory (empty string filtered), so no spurious check when --out is a bare filename.
  • OUT_DIRECTORY contents (["eval"], ["eval", "candidates"], ["eval", "diff"], ["eval", "repeat", "import"], ["calibrate", "fit"]) match the spec increment 2 description exactly.
  • check_directory() edge-case handling: "file/" on Linux triggers ENOTDIR, trimming the slash and re-calling metadata("file") correctly returns "is not a directory"; the test covers this case.
  • The "file/below" case (non-terminal component is a file) falls through to "output directory does not exist" - semantically imprecise but consistent and explicitly tested.

Call site ordering

Each call site places outputs::check after flag-only validation (replay_config, split, forbid_candidate_deps) so usage errors keep precedence, and before the first file read and no-clobber checks (ensure_absent). This matches the spec requirement.

Return-type consistency

Result<Res, Usage> callers correctly use if let Err(d) = … { return Ok(Err(d)); }; Res callers use ?. No mismatch found.

Test changes

  • tests/plan.rs unreadable_inputs_are_io_errors_naming_the_path: the assertion correctly changes from expecting the input path to expecting the output directory, because the new check runs first. Test name is now slightly misleading but that is style.
  • tests/run.rs a_bundle_failure_after_the_decision_keeps_the_decision_fields: correctly gated #[cfg(unix)] since it uses PermissionsExt; uses a read-only (existing) directory so the directory check passes and the failure originates in the actual write, preserving the post-decision bundle-failure semantics being tested.
  • tests/eval.rs: creates "refused" directory so the adapter refusal, not the directory check, is what fires.
  • tests/io.rs: new tests cover missing directory, non-directory given as --out, parent-of-file output, and an existing directory that falls through to input refusal.

Archive policy and spec

src/outputs.rs is added to tools/rustev-release/archive-policy.json and the spec update accurately describes the implementation.

No concrete defects found.

{"head": "c5cf056783ad8abd4836c979c8ad3d386c22a634", "verdict": "no-findings", "findings": []}

Subject: 694866c...c5cf056, diff digest 3558856fa4e3b31e94fadec9c0dc4d9ba6bad68109399a62cce00d040ea535ce, 1 call(s). An AI comment is not an approval.

@bartekus
bartekus force-pushed the impl/072-inc2-outdir branch from c5cf056 to 1045860 Compare November 11, 2026 01:53
@github-actions

Copy link
Copy Markdown

AI review (no-findings)

Reviewing the diff for correctness, security, and consistency with the governance context.

crates/rustev-cli/src/outputs.rs — new module

check_directory correctly handles: existing directory, existing non-directory, path-not-found, and the edge case of a regular file given with a trailing slash (strips the slash, re-stats, distinguishes "is not a directory" from "does not exist"). The parent() helper filters the empty-parent case so the working directory is never checked. The directories() function correctly branches on OUT_DIRECTORY membership (treating --out as a directory for eval, eval candidates, eval diff, eval repeat import, and calibrate fit, and as a file otherwise), handles eval live's journal output only when not requiring an existing one, and limits the --store check to run --retain external. All align with the spec text in 072 3.2.

Call-site consistency

Commands returning Result<Res, Usage> use return Ok(Err(d)) and commands returning Res use ?. Both are correct for their respective return types and the pattern is applied consistently across calibrate.rs, eval.rs, explain.rs, live.rs, plan.rs, repeat.rs, and run.rs.

Test changes

tests/plan.rs now expects the directory named rather than the input file — consistent with the new behaviour. tests/eval.rs pre-creates the output directory so the adapter refusal is reached, not the directory check. tests/run.rs uses a read-only existing directory (permissions restored before assertions so the scratch cleanup succeeds). tests/io.rs covers missing directory, regular-file-as-directory, file-with-trailing-slash, and existing-directory-keeping-input-refusal. All are coherent with the spec text.

Spec and archive-policy

The spec increment 2 prose matches the implementation exactly (flags enumerated, ordering guarantee stated, CLI-only placement justified). src/outputs.rs is correctly added to the archive policy list.

No concrete defects found.

{"head": "1045860d30cef2b497a98f3eb65c8ae4c6ae7652", "verdict": "no-findings", "findings": []}

Subject: dc60f26...1045860, diff digest f7e4148283a05faa3c7e51932c0f36e46edc9df6a680857a4e19729d11280d55, 1 call(s). An AI comment is not an approval.

@bartekus
bartekus added this pull request to the merge queue Nov 11, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 11, 2026
@bartekus
bartekus force-pushed the impl/072-inc2-outdir branch from 1045860 to cf571d9 Compare November 11, 2026 03:20
@bartekus
bartekus enabled auto-merge November 11, 2026 03:20
@github-actions

Copy link
Copy Markdown

AI review (findings)

Reviewing the new outputs.rs module and its integration across the CLI commands.

outputs.rs logic is sound overall. The directories() function correctly partitions --out into "is itself the directory" (for eval, eval candidates, etc.) versus "parent of a file path" (for all other commands), and the OUTPUT_FILES constants cover the named output-file flags. The check_directory double-metadata pattern for the trailing-slash case is tested and intentional.

One concrete defect in check_directory (low, wording):

Err(e) if matches!(e.kind(), ErrorKind::NotFound | ErrorKind::NotADirectory) => {
    let trimmed = dir.trim_end_matches('/');
    match std::fs::metadata(trimmed) {
        Ok(m) if !trimmed.is_empty() && !m.is_dir() => fail("is not a directory".into()),
        _ => fail("output directory does not exist".into()),  // ← line ~88
    }
}

When dir is a path like "somefile/subdir" (no trailing slash, intermediate component is a regular file), fs::metadata("somefile/subdir") returns NotADirectory, the trimmed string is unchanged ("somefile/subdir"), the second fs::metadata call returns the same error, and the code falls to the _ arm emitting "output directory does not exist" instead of "is not a directory". The test at line ~118 (below = format!("{dir}/file/below")) already verifies and locks in this behaviour, so it is intentional; but it produces a misleading message on that input.

No high- or medium-severity bugs, security issues, authority changes, or ratification concerns were found. The spec increment 2 description matches the implementation. The archive-policy addition, the #[cfg(unix)] guard on the read-only-dir test, and the mode-reset-before-assert ordering in tests/run.rs are all correct.

{"head": "cf571d99b4c72ac067c2e831fcd433799e17ec83", "verdict": "findings", "findings": [{"path": "crates/rustev-cli/src/outputs.rs", "line": 88, "severity": "low", "category": "correctness", "summary": "When a path component (not the leaf) is a regular file (e.g. 'somefile/subdir'), the second fs::metadata call also returns NotADirectory, falling through to 'output directory does not exist' rather than the more accurate 'is not a directory'."}]}

Subject: aa1225e...cf571d9, diff digest 9cadf05ce89bcbb1c5eb9483769670d971aace7ab0da4131fdb2e690afebab2f, 1 call(s). An AI comment is not an approval.

@bartekus
bartekus had a problem deploying to statecraft-review-exception November 11, 2026 03:24 — with GitHub Actions Error
@bartekus
bartekus temporarily deployed to statecraft-review-exception November 11, 2026 03:26 — with GitHub Actions Inactive
@bartekus
bartekus added this pull request to the merge queue Nov 11, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Nov 11, 2026
@bartekus
bartekus force-pushed the impl/072-inc2-outdir branch from cf571d9 to 5317ed8 Compare November 11, 2026 04:08
@bartekus
bartekus enabled auto-merge November 11, 2026 04:08
@github-actions

Copy link
Copy Markdown

AI review (findings)

Reviewing the new outputs.rs module and its call sites for correctness, security, and consistency.

Core logic (outputs.rs)

check_directory traces through OS error kinds correctly for the file/ trailing-slash case: metadata("file/") returns ENOTDIR; trimming gives "file"; a second metadata call confirms it is not a directory. The test covers this. The TOCTOU between the check and the actual write is unavoidable in a precheck design and is disclosed in the doc comment.

Call-site patterns

Functions returning Result<Res, Usage> use if let Err(d) = check(p) { return Ok(Err(d)); } to wrap the Done in an Ok without producing a Usage; functions returning Res use check(p)?. Both are correct for their respective return types.

Ordering

The check is inserted after usage-rule validation in every call site (confirmed for eval, views, judge, calibrate::fit, repeat::eval/baseline/compare/select/import, plan::compile_cmd/diff, run::execute, live::run), satisfying the spec requirement.

One concrete defect

In directories():

// OUT_DIRECTORY path – filters empty:
dirs.extend(Some(out).filter(|d| !d.is_empty()));

// --store path – no filter:
if path == ["run"] && p.one("retain") == Some("external") {
    dirs.extend(p.one("store"));   // Some("") passes through
}

An empty --store value is not filtered the way --out is. check_directory("") would call metadata(""), which on Linux returns ENOENT, then re-calls metadata(""), falling into _ => fail("output directory does not exist") with an empty path string — a confusing diagnostic. All other accumulation points (parent(), --out directory) guard against empty strings explicitly.

Other items

  • The archive-policy entry for src/outputs.rs matches the new file. ✓
  • Spec 072 increment 2 prose matches the implementation. ✓
  • Test additions cover the main cases and the two pre-existing tests that broke now correctly use an existing or read-only directory to stay past the directory check.
{"head": "5317ed8bed32df74920c110e018f8658253824bb", "verdict": "findings", "findings": [{"path": "crates/rustev-cli/src/outputs.rs", "line": 72, "severity": "low", "category": "correctness", "summary": "The --store accumulation in directories() lacks the empty-string filter present for --out, so an empty --store value passes check_directory(\"\") and produces a confusing io_error with an empty path instead of being silently skipped."}]}

Subject: e31d409...5317ed8, diff digest 183d670a7decf9723330d519ebe00cf576cfb1f4e645016f99d7be8e850f3047, 1 call(s). An AI comment is not an approval.

@bartekus
bartekus force-pushed the impl/072-inc2-outdir branch from 5317ed8 to 7960488 Compare November 11, 2026 04:14
@github-actions

Copy link
Copy Markdown

AI review (findings)

Reviewing the diff against the governance context and spec 072. The change adds an output-directory precheck (outputs.rs) and wires it into every writing command.

crates/rustev-cli/src/outputs.rs — check_directory double-stat logic

When metadata(dir) returns NotFound or NotADirectory, the function trims trailing / and re-stats. For a path like "existing-file/below" where a component is a regular file, the trimmed path still returns NotADirectory, falling to the _ wildcard which emits "output directory does not exist" rather than "is not a directory". The message is imprecise but the behavior (refusing with exit 3 and naming the path) is correct. This is wording, not a behavioural defect on any input the tests exercise.

ErrorKind::NotADirectory on Windows: The variant exists in all Rust 1.63+ targets, but the trim_end_matches('/') guard targets POSIX trailing-slash semantics. On Windows, the OS maps ERROR_DIRECTORY to NotADirectory correctly since 1.63, so the logic compiles and mostly works, but forward-slash trimming does not cover Windows \-terminated paths. No test exercises this path, and no Windows CI is visible in the changed files. This is low-severity: the fallback Err(e) => fail(e.to_string()) still refuses; it just surfaces the OS message instead of "output directory does not exist".

calibrate.rs / eval.rs / repeat.rs — check placement after replay_config / split

The module doc states the check runs "before any input is read". In calibrate::fit, eval::eval, and repeat::eval the check is placed after replay_config and split calls. Those functions are not supplied in the diff. If either does file I/O, the "before any read" guarantee is violated. The integration tests (io-eval-out) pass absent input flags and observe the output-directory error, which provides circumstantial evidence those helpers don't read files; but that cannot be verified from the diff alone.

Everything else looks correct: OUT_DIRECTORY matches the spec description, parent correctly short-circuits for working-directory filenames, the eval live journal and run --retain external --store special cases are consistent with the spec prose, the archive-policy addition is present, and the test updates in eval.rs/run.rs/plan.rs coherently adapt existing assertions to the new precheck ordering.

{"head": "7960488bc1f8621525534da3c95bb6ac551b9f1d", "verdict": "findings", "findings": [{"path": "crates/rustev-cli/src/outputs.rs", "line": 73, "severity": "low", "category": "correctness", "summary": "trim_end_matches('/') only strips POSIX forward-slash separators; on Windows, backslash-terminated paths skip the trailing-separator branch and fall through to the OS error message instead of the normalised 'output directory does not exist' wording."}]}

Subject: e31d409...7960488, diff digest 83466c62e00b83cf0e4631d02f9d8b7c9d413f3f7d29985f269dfbe94ae3b7b1, 1 call(s). An AI comment is not an approval.

@bartekus
bartekus had a problem deploying to statecraft-review-exception November 11, 2026 04:17 — with GitHub Actions Error
@bartekus
bartekus temporarily deployed to statecraft-review-exception November 11, 2026 04:18 — with GitHub Actions Inactive
@bartekus
bartekus added this pull request to the merge queue Nov 11, 2026
Merged via the queue into main with commit 16a1f81 Nov 11, 2026
47 of 50 checks passed
@bartekus
bartekus deleted the impl/072-inc2-outdir branch November 11, 2026 05:06

This branch was previously deployed

1 inactive deployment
statecraft-review-exception — 7960488b Deployed Nov 11, 2026 by bartekus via review-exception #4005
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant