From 54115aaf5063315119b53b69dabfac3130c52a3b Mon Sep 17 00:00:00 2001 From: Tars Date: Sun, 22 Nov 2026 22:25:59 -0300 Subject: [PATCH] fix(doctor): return conflict for invalid directory ownership --- docs/operating.md | 4 +-- internal/cli/doctor.go | 8 +++-- internal/cli/doctor_directory_test.go | 48 ++++++++++++++------------- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/docs/operating.md b/docs/operating.md index 8cb44ea..fd9b835 100644 --- a/docs/operating.md +++ b/docs/operating.md @@ -97,8 +97,8 @@ runtime=directory presence=present compatibility=compatible action=configure tou - `cortex doctor --target-dir ` reports `presence`, `writability`, and `ownership` (`unowned`, `owned`, `invalid`) for the directory. Only a missing `.cortex/install-state.json` is `unowned`; a state file that exists but cannot be read or - decoded is `invalid`. An unwritable directory or an unreadable - prior state exits with the same conflict code install would hit. + decoded is `invalid`. An unwritable directory or an `invalid` prior state exits + with conflict code 3; unreadable state must not be treated as a clean first install. ## Running update diff --git a/internal/cli/doctor.go b/internal/cli/doctor.go index 98da2ff..b003e2b 100644 --- a/internal/cli/doctor.go +++ b/internal/cli/doctor.go @@ -153,7 +153,11 @@ func runDoctorDirectoryTarget(stdout io.Writer, targetDir string) int { line += " writability=unwritable" code = exitConflict } - line += " ownership=" + doctorTargetOwnership(targetDir) + ownership := doctorTargetOwnership(targetDir) + line += " ownership=" + ownership + if ownership == "invalid" { + code = exitConflict + } } if _, err := io.WriteString(stdout, line+"\n"); err != nil { return exitFailure @@ -268,7 +272,7 @@ const ( "so frame directives stay rejected while the quoted diff passes validation (issue #305).\n" rootHelp = "usage: cortex [arguments]\ncommands: doctor, install, update, uninstall, qa\noptions: --version, --help\n" + "install [--target-dir ]: configure every compatible present runtime, or project the\ncatalog as plain SKILL.md files into an existing directory (Agent Skills layout)\n" + - "doctor [--target-dir ]: probe runtimes, or report one directory target\n" + + "doctor [--target-dir ]: probe host/target; conflicts exit 3\n" + "uninstall [--target-dir ]: remove owned files from every runtime root, or from \n\n" + qaHelp // qaReportNote surfaces runtime prerequisites on non-catalog failures. It // names no route: a refusal happens with a profile route resolved, with a diff --git a/internal/cli/doctor_directory_test.go b/internal/cli/doctor_directory_test.go index b34e2e0..8b9e9d9 100644 --- a/internal/cli/doctor_directory_test.go +++ b/internal/cli/doctor_directory_test.go @@ -104,28 +104,30 @@ func TestDoctorDirectoryTargetUnreadableStateIsInvalid(t *testing.T) { return target } - // An undecodable state is the existing invalid classification. - garbage := writeState("garbage", []byte("not json")) - garbageCode, garbageOut, garbageErr := runDoctorOnTarget(t, garbage) - if garbageErr != "" || garbageOut != "target="+garbage+" presence=present writability=writable ownership=invalid\n" { - t.Fatalf("garbage: exit = %d stdout = %q stderr = %q", garbageCode, garbageOut, garbageErr) - } + t.Run("undecodable", func(t *testing.T) { + garbage := writeState("garbage", []byte("not json")) + code, stdout, stderr := runDoctorOnTarget(t, garbage) + if code != exitConflict || stderr != "" || stdout != "target="+garbage+" presence=present writability=writable ownership=invalid\n" { + t.Fatalf("garbage: exit = %d stdout = %q stderr = %q", code, stdout, stderr) + } + }) - // An unreadable state exists, so it is invalid exactly like an undecodable one. - unreadable := writeState("unreadable", []byte("{}")) - statePath := filepath.Join(unreadable, ".cortex", "install-state.json") - if err := os.Chmod(statePath, 0o000); err != nil { - t.Fatal(err) - } - t.Cleanup(func() { _ = os.Chmod(statePath, 0o600) }) - if os.Getuid() == 0 { - t.Skip("root can read a mode 0000 file") - } - code, stdout, stderr := runDoctorOnTarget(t, unreadable) - if want := "target=" + unreadable + " presence=present writability=writable ownership=invalid\n"; stdout != want { - t.Fatalf("unreadable stdout = %q, want %q", stdout, want) - } - if stderr != "" || code != exitOK || garbageCode != exitOK { - t.Fatalf("unreadable: exit = %d (invalid exits %d) stderr = %q", code, garbageCode, stderr) - } + t.Run("unreadable", func(t *testing.T) { + unreadable := writeState("unreadable", []byte("{}")) + statePath := filepath.Join(unreadable, ".cortex", "install-state.json") + if err := os.Chmod(statePath, 0o000); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = os.Chmod(statePath, 0o600) }) + if os.Getuid() == 0 { + t.Skip("root can read a mode 0000 file") + } + code, stdout, stderr := runDoctorOnTarget(t, unreadable) + if want := "target=" + unreadable + " presence=present writability=writable ownership=invalid\n"; stdout != want { + t.Fatalf("unreadable stdout = %q, want %q", stdout, want) + } + if stderr != "" || code != exitConflict { + t.Fatalf("unreadable: exit = %d, want conflict; stderr = %q", code, stderr) + } + }) }