Skip to content

fix(doctor): return conflict for invalid directory ownership - #445

Open
barbatdev wants to merge 1 commit into
mainfrom
fix/364-doctor-unreadable-state
Open

barbatdev wants to merge 1 commit into
mainfrom
fix/364-doctor-unreadable-state

Conversation

@barbatdev

Copy link
Copy Markdown
Contributor

Linked Issue

Closes #364

PR Type

  • type:bug Bug fix

Summary

Directory doctor already classified unreadable prior state as invalid after #438, but still exited successfully. Return conflict code 3 when prior ownership is invalid so unreadable state cannot be certified as a clean first install.

Changes

  • Propagate invalid ownership into the directory doctor conflict exit.
  • Assert conflict exits for mode-000 unreadable and malformed state independently.
  • Preserve absent, unowned, owned, and unwritable coverage; document the exit in help and operating guidance.

Focused Tests

  • RED: both new invalid-state exit assertions fail with exit 0 before the fix.
  • GREEN: directory doctor and root/QA help tests pass, including the non-root mode-000 case.
  • go vet ./...: pass.
  • go test ./...: pass.
  • /usr/bin/python3 -m unittest tests.test_community_policy tests.test_github_policy tests.test_runtime_adapter_contract: 16 tests pass.
  • git diff --check: pass.
  • Local go test -race ./... exceeded the 360-second timeout; exact-head CI remains the required race proof.

Runtime Harness

The CLI regression invokes the doctor command dispatcher against real temporary directory targets. Mode-000 prior state reports presence=present writability=writable ownership=invalid and exits 3; stderr is empty. Existing directory-state tests pass.

Rollback

Revert this commit to restore the prior directory-doctor exit behavior, tests, and documentation together. No stored data changes or migration.

Chain Dependencies / Out of Scope

One focused PR, no chain dependencies. Runtime-probed doctor, installation behavior, and ownership decoding are unchanged.
Risk: none; the change is confined to the requested directory-doctor conflict result.

Checklist

  • Linked issue is open and status:approved.
  • Exactly one matching type:* label is applied.
  • Focused tests and runtime harness evidence are recorded.
  • Rollback and out-of-scope boundaries are documented.

@barbatdev

Copy link
Copy Markdown
Contributor Author

Fleet review (qwen38)

Lens: evidence. The PR closes the single blocking acceptance criterion of issue #364: doctorTargetOwnership already classified an unreadable or undecodable prior state as invalid (#438), and this change propagates that classification into the exit code, so an invalid ownership now sets exitConflict (3) instead of exiting 0. The rewritten test asserts the conflict exit independently for both invalid variants (undecodable garbage and a mode-000 state file), checks stdout prints ownership=invalid and stderr is empty, which directly reproduces and now refutes the issue's failure scenario. The help text and docs/operating.md were updated to state the conflict-3 behavior, matching the PR's verification claims (root/QA help tests, go vet, go test). Evidence caveats: the mode-000 subtest skips when the runner is uid 0, so on a root CI only the undecodable path proves the new exit logic — acceptable because both classifications funnel through the same ownership == "invalid" check; local go test -race timed out at 360s and defers to exact-head CI, honestly disclosed. No concrete failure scenario found; verdict ready to merge.

Suggestion (1)

  • Malformed state fixture is only exercised via chmod path (internal/cli/doctor_directory_test.go:149): The undecodable subtest writes literal 'not json' and is representative of a corrupt state file. Consider that the unreadable subtest's chmod approach relies on filesystem permissions rather than an ownership/ACL mechanism; it is fine on Linux CI, but the fixture's realism depends on the runner being non-root (see the question above).

Question (1)

  • Mode-000 evidence is skipped when the test runner is root (internal/cli/doctor_directory_test.go:150): The unreadable-state subtest calls t.Skip when os.Getuid() == 0. If CI runs the suite as root, the exact scenario named in issue QA follow-up: feat(doctor): report directory target presence, writability, and ownership (#355) #364 (mode-000 install-state.json) is never exercised there; the new conflict exit is then only evidenced by the undecodable variant. Both invalid classifications share the same ownership == "invalid" branch in doctor.go, so the logic is still covered, but the representative fixture for the acceptance criterion would only run on non-root workers.

Verdict: ready to merge

@barbatdev

Copy link
Copy Markdown
Contributor Author

Fleet review (glmflash)

Lens: adversarial. The fix correctly propagates the existing invalid-ownership classification from doctorTargetOwnership (verified at the reviewed head: ErrNotExist -> unowned, any other read error or decode failure -> invalid) into the directory-doctor conflict exit. Every non-invalid path (absent, unowned, owned, unwritable) keeps its prior exit code, so the change is confined to the requested behavior. The rewritten test now asserts exit 3 independently for undecodable and mode-000-unreadable state in separate subtests, directly closing the acceptance scenario of issue #364 (mode-000 state file no longer reported as unowned with exit 0). Documentation (docs/operating.md) and the root help string were updated consistently. Boundary probes found no blocking defect: unwritable+invalid is idempotent (both set exitConflict), ENOTDIR on .cortex falls into invalid and exits conflict conservatively, and the root-skip for the mode-000 case is documented. Minor suggestion below is non-blocking.

Suggestion (1)

  • Transient read errors now surface as a hard conflict exit (internal/cli/doctor.go:278): doctorTargetOwnership returns invalid for any read error other than ErrNotExist, and this PR promotes that to exit 3. A transient I/O error (EIO, stale NFS handle) while reading .cortex/install-state.json now produces a conflict exit indistinguishable from a genuine undecodable state. This is conservative and safe (it can only block, never falsely certify a clean install), consistent with the operating.md wording, and the single-line report does not tell the user which kind of invalid was seen.

Verdict: ready to merge

@barbatdev

Copy link
Copy Markdown
Contributor Author

Fleet review (mimo)

Lens: requirements. Issue #364 has a single acceptance criterion — unreadable prior state must not be certified as a clean first install with exit 0 — and the diff satisfies it: internal/cli/doctor.go:156-159 maps ownership=invalid to exitConflict (3), consistent with the docs update (docs/operating.md:100-101) and the new help line (internal/cli/doctor.go:275), where exitConflict=3 matches every existing conflict site (unwritable dir, presence=unreadable, host prior-state blocker, install ownership/schema conflicts). The pre-existing assertions visible in the diff show the base already classified mode-000 and undecodable state as ownership=invalid but exit 0 (that label half came from #438), so this PR closes the remaining exit-code half of the quoted scenario: at this head the criterion's scenario prints ownership=invalid and exits 3 instead of ownership=unowned/exit 0, so it no longer reproduces. The restructured subtests assert exitConflict with exact stdout for both undecodable and unreadable state, while absent/unowned/owned stay exit 0 (their tests are untouched and only invalid triggers the new conflict); the rootHelp change breaks no test, since no test asserts the replaced doctor help string. One testability note below; no blocking findings.

Suggestion (1)

  • Exact acceptance-criterion scenario is only asserted when non-root (internal/cli/doctor_directory_test.go:121): The unreadable (mode-000) subtest encoding the criterion's exact scenario calls t.Skip when os.Getuid()==0, so on a root CI runner only the undecodable subtest proves invalid -> exit 3, and the mode-000 path itself rests on the author's local non-root GREEN run recorded in the PR body. The skip is pre-existing, but this PR moves the exit-0 -> exit-3 assertion behind it.

Verdict: ready to merge

@barbatdev

Copy link
Copy Markdown
Contributor Author

Fleet review (glm)

Lens: requirements + adversarial. The diff satisfies the single acceptance criterion of correction issue #364: a directory target whose .cortex/install-state.json exists but cannot be read (mode 000) or decoded is reported as ownership=invalid and now exits with conflict code 3 instead of 0, so unreadable prior state can no longer be certified as a clean first install. The ownership conflict propagation sits inside the presence=present branch (absent targets keep exit 0), composes correctly with the existing writability=unwritable conflict, and the host-probe doctor path already returns exitConflict for prior-state blockers, so the rewritten help line 'probe host/target; conflicts exit 3' is accurate for both doctor modes. Tests assert exitConflict independently for the undecodable and unreadable (mode-000, non-root) cases, and the root-skip on the unreadable subtest is backstopped by the undecodable subtest, which exercises the same invalid-to-conflict path. No contradicting, missing, or unverifiable criterion found; no boundary or failure-path defect found in the changed code.

No findings.

Verdict: ready to merge

@barbatdev

Copy link
Copy Markdown
Contributor Author

Fleet review (deepseek)

Lens: evidence + test design. The change is small and correct for the linked acceptance criterion: doctorTargetOwnership (internal/cli/doctor.go:172) already returned invalid for a state file it cannot read (line 177) or decode (line 181), and runDoctorDirectoryTarget now maps that classification to exitConflict (line 158-160), so the QA scenario from #364 (mode-000 .cortex/install-state.json reported as ownership=unowned with exit 0) no longer reproduces: the same target now prints ownership=invalid and exits 3. Acceptance evidence is real: both subtests in internal/cli/doctor_directory_test.go assert the conflict exit and the exact stdout line, the pre-fix code demonstrably returned exitOK for the invalid classification (old assertion expected exitOK), and exact-head CI (validate + race suite) is green at 54115aa. Docs and root help were updated consistently, and absent/unowned/owned/unwritable coverage is preserved (exitOK for absent, unowned and owned at internal/cli/doctor_directory_test.go:38-71). Two non-blocking evidence points remain: the code comments still claim the exit mirrors what install itself hits, while directory install fails earlier for those states with install_composition_failed/exit 70; and the only test exercising the read-error branch skips under root.

Suggestion (1)

  • Read-error branch is covered only by a root-skipped fixture (internal/cli/doctor_directory_test.go:122): doctorTargetOwnership's if err != nil { return "invalid" } branch (internal/cli/doctor.go:177-179) is reached only by the unreadable subtest, which calls t.Skip when uid is 0 because root can read a mode-000 file. CI runs on ubuntu-latest as a non-root user and passed at this head, so the branch is evidenced today, but any root container or self-hosted runner loses the sole coverage of the exact acceptance scenario while still reporting the suite green. Missing test condition: a root-independent read failure for .cortex/install-state.json (for example the path occupied by a directory, which makes ReadFile fail with EISDIR on Linux/macOS) asserting stdout ownership=invalid and exit exitConflict, so the read-error branch is pinned regardless of the uid the suite runs under.

Question (1)

  • Conflict exit does not match install's exit for the same invalid state; comment still claims it does (internal/cli/doctor.go:128): The new conflict exit is doctor-only. For the same invalid prior state, directory install refuses earlier than the ownership check: buildDirectoryRequests -> installobserve.Observe reads .cortex/install-state.json through readRegular, and a non-ENOENT read failure or a decode failure there returns filesystemInvalid (internal/installobserve/filesystem.go:151-162), which buildDirectoryRequests propagates (internal/cli/install.go:186-189) so runDirectoryInstall reports install_composition_failed with exitFailure (70) at internal/cli/install.go:126-130 - not exitConflict (3). Unsupported prior schema is the case that maps to 3 (internal/cli/install.go:289-295), and that state is not the unreadable one. Two comments still assert the stronger claim the corrected docs no longer make: conflicts exactly the way install would (this line) and a state file it cannot read or classify, which blocks install like the canonical prior-state blocker does (line 168-171). Please confirm whether exit 3 here means doctor refuses to certify rather than install will exit 3, and whether those comments should be reconciled with the measured install exit.

Verdict: ready to merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type:bug Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

QA follow-up: feat(doctor): report directory target presence, writability, and ownership (#355)

2 participants