Skip to content
Merged
6 changes: 4 additions & 2 deletions docs/operating.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ operation=install status=completed touch=applied create=33 replace=0 remove=0 un
- `status=completed touch=applied` — the transaction applied and was read back. Running install again is a no-op reported honestly as `create=0 unchanged=33`.
- `status=not_applied reason=compatibility_uncertified touch=denied` — no compatible runtime was found; nothing was written.
- `status=not_applied reason=projection_unrepresentable touch=denied` — a runtime exists but cannot represent a capability honestly; Cortex refused rather than approximating. Report it; do not work around it.
- `status=not_applied reason=ownership_conflict touch=denied` (exit 3) — a target file exists without verified Cortex ownership. Someone else wrote there. Stop and ask the user; never delete or rewrite foreign files to clear the path.
- `status=not_applied reason=ownership_conflict touch=denied` (exit 3) — a target file exists without verified Cortex ownership. Someone else wrote there. Stop and ask the user; never delete or rewrite foreign files to clear the path. The status line appends `conflict_paths=<relative path,...>` naming every asset whose ownership decision conflicted, so the conflicting files are named without reading transaction internals.
- `warning=uncertified_admission runtimes=N certification=not_certified` — the transaction configured N runtimes whose exact version has no verified evidence. This is disclosed non-certified admission, not a failure; the user may accept it once they know.

## Directory targets
Expand Down Expand Up @@ -100,6 +100,8 @@ cortex update --runtime pi --catalog ./catalog --apply # the plan, applied

Default mode touches nothing — run the plan first, read it, then decide. `--apply` writes only inside that runtime's Cortex-owned root. On Pi, if legacy externally-modified guidance is detected, the plan asks for explicit consent with `--adopt-guidance`: that is a one-time adoption of external bytes (they are preserved, not overwritten), not a drift override, and drift is always refused.

A plan or apply that hits an asset without verified Cortex ownership reports `operation=<logical id> action=conflict` and ends with `status=conflict conflict_paths=<relative path,...>` (exit 3), naming each conflicting asset by its path under the runtime root. Nothing is written; stop and ask the user rather than removing the file.

## Upgrade to alpha20

Use the [published alpha20 archive and checksum procedure](verification.md#verify-an-archive) for the machine's platform. Keep the extracted binary, catalog and licenses together in a versioned directory. Do not replace user model routes or runtime configuration with release defaults.
Expand Down Expand Up @@ -134,7 +136,7 @@ Preserve command exit codes, output and the last completed transaction. A failed

- `uninstall=completed remove=N` — removed exactly what it owned.
- `not_installed` — nothing of Cortex's there; nothing done. The grouped uninstall exits 0; `uninstall --target-dir` exits 3 because a directory target without ownership fails closed.
- `conflict` / `blocked` — shared actor state or unverified ownership blocks removal; the whole grouped uninstall fails closed and nothing is removed. Report the diagnostic notes to the user instead of forcing it.
- `conflict` / `blocked` — shared actor state or unverified ownership blocks removal; the whole grouped uninstall fails closed and nothing is removed. The runtime line appends `conflict_paths=<relative path,...>` naming the drifted prior-owned assets. Report the diagnostic notes to the user instead of forcing it.

## Never do this

Expand Down
3 changes: 3 additions & 0 deletions internal/cli/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,9 @@ func writeUninstallResult(stdout, stderr io.Writer, results []uninstallPreflight
for _, result := range results {
remove, absent, conflict := uninstallCounts(result.observation)
_, _ = fmt.Fprintf(&output, "runtime=%s uninstall=%s remove=%d absent=%d conflict=%d", result.root.RuntimeID(), result.status, remove, absent, conflict)
if paths := result.observation.ConflictPaths(); len(paths) > 0 {
_, _ = fmt.Fprintf(&output, " conflict_paths=%s", strings.Join(paths, ","))
}
if paths := result.observation.UnsupportedActorPaths(); len(paths) > 0 {
unsupported = true
_, _ = fmt.Fprintf(&output, " reason=unsupported_installation state=validated_canonical_v3 recorded_actor_paths=%s", strings.Join(paths, ","))
Expand Down
51 changes: 49 additions & 2 deletions internal/cli/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ func runDirectoryInstall(stdout, stderr io.Writer, target string, deps installDe
group, err := deps.applyGroup(requests, home, backupName)
if err != nil {
reason, code := installFailureReason(err)
return writeInstallResult(stdout, stderr, "install", "status=not_applied reason="+reason+" touch=denied", "", results, false, installtxn.Counts{}, code)
status := "status=not_applied reason=" + reason + " touch=denied"
if paths := installConflictPaths(requests, err); paths != "" {
status += " conflict_paths=" + paths
}
return writeInstallResult(stdout, stderr, "install", status, "", results, false, installtxn.Counts{}, code)
}
return writeInstallResult(stdout, stderr, "install", "status=completed touch=applied", "", results, true, group.Counts(), exitOK)
}
Expand Down Expand Up @@ -221,11 +225,54 @@ func runInstall(ctx context.Context, stdout, stderr io.Writer, runner runtimepro
group, err := deps.applyGroup(requests, home, backupName)
if err != nil {
reason, code := installFailureReason(err)
return writeInstallResult(stdout, stderr, operation, "status=not_applied reason="+reason+" touch=denied", "", results, false, installtxn.Counts{}, code)
status := "status=not_applied reason=" + reason + " touch=denied"
if paths := installConflictPaths(requests, err); paths != "" {
status += " conflict_paths=" + paths
}
return writeInstallResult(stdout, stderr, operation, status, "", results, false, installtxn.Counts{}, code)
}
return writeInstallResult(stdout, stderr, operation, "status=completed touch=applied", warning, results, true, group.Counts(), exitOK)
}

// installConflictPaths renders the conflicting logical assets as runtime-relative
// paths for the operator diagnostic (issue #373). Output is deduplicated and
// sorted by the transaction layer.
func installConflictPaths(requests []installtxn.GroupRequest, err error) string {
var conflict *installtxn.ConflictError
if !errors.As(err, &conflict) || len(conflict.LogicalIDs) == 0 {
return ""
}
plans := make([]installplan.Plan, 0, len(requests))
for _, request := range requests {
plans = append(plans, request.Plan)
}
return conflictRelativePaths(plans, conflict.LogicalIDs)
}

// conflictRelativePaths maps conflicting LogicalIDs to the runtime-relative
// paths their plans compose, preserving the given order and dropping
// duplicates. LogicalIDs without a file in the plans (the state artifact) are
// omitted, so every caller renders the same conflict_paths representation.
func conflictRelativePaths(plans []installplan.Plan, logicalIDs []string) string {
relative := make(map[string]string)
for _, plan := range plans {
for _, file := range plan.Files() {
relative[file.LogicalID()] = file.RelativePath()
}
}
paths := make([]string, 0, len(logicalIDs))
seen := make(map[string]bool, len(logicalIDs))
for _, id := range logicalIDs {
path, ok := relative[id]
if !ok || seen[path] {
continue
}
seen[path] = true
paths = append(paths, path)
}
return strings.Join(paths, ",")
}

// installFailureReason maps a grouped-transaction failure to the operator reason
// and exit code. Fail-closed refusals name their cause and remedy instead of the
// opaque transaction_failed (issue #281).
Expand Down
18 changes: 18 additions & 0 deletions internal/cli/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,24 @@ func TestRunInstallUnrepresentableProjectionIsNotApplied(t *testing.T) {
t.Fatalf("install = (%q, %q)", stdout.String(), stderr.String())
}
}
func TestRunInstallConflictListsConflictingPaths(t *testing.T) {
deps := compatibleInstallDependencies(t, t.TempDir())
deps.applyGroup = func([]installtxn.GroupRequest, string, string) (installtxn.GroupResult, error) {
return installtxn.GroupResult{}, &installtxn.ConflictError{LogicalIDs: []string{"skills/requirements-analyst"}}
}
var stdout, stderr bytes.Buffer
if code := runWithInstallDependencies(context.Background(), []string{"install"}, &stdout, &stderr, certifiedPiOnlyRunner(), deps); code != exitConflict {
t.Fatalf("install exit = %d, want %d", code, exitConflict)
}
if stderr.Len() != 0 {
t.Fatalf("stderr = %q, want empty", stderr.String())
}
want := "status=not_applied reason=ownership_conflict touch=denied conflict_paths=skills/cortex-requirements-analyst/SKILL.md"
if !strings.Contains(stdout.String(), want) {
t.Fatalf("stdout = %q, want conflict paths %q", stdout.String(), want)
}
}

func TestRunInstallFailuresDoNotReportCompletion(t *testing.T) {
for _, tc := range []struct {
name string
Expand Down
14 changes: 12 additions & 2 deletions internal/cli/uninstall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestRunUninstallRuntimeHarness(t *testing.T) {
t.Fatal(err)
}
code, stdout, stderr := runUninstallForTest(t, roots, nil)
if code != exitConflict || stderr != "" || stdout != uninstallLines("blocked", "conflict", "blocked") {
if code != exitConflict || stderr != "" || stdout != uninstallLines("blocked", "conflict", "blocked", "skills/cortex-alpha/SKILL.md") {
t.Fatalf("uninstall = (%d, %q, %q)", code, stdout, stderr)
}
for i := range roots {
Expand Down Expand Up @@ -216,6 +216,11 @@ func writeInstalled(t *testing.T, root skillroot.UninstallRoot, content string)
}

func uninstallLines(statuses ...string) string {
var conflictPaths string
if len(statuses) > 3 {
conflictPaths = statuses[3]
statuses = statuses[:3]
}
var output strings.Builder
for i, runtime := range []string{"pi", "opencode", "claude-code"} {
remove, conflict := 2, 0
Expand All @@ -224,7 +229,12 @@ func uninstallLines(statuses ...string) string {
} else if statuses[i] == "conflict" {
remove, conflict = 1, 1
}
_, _ = fmt.Fprintf(&output, "runtime=%s uninstall=%s remove=%d absent=0 conflict=%d\n", runtime, statuses[i], remove, conflict)
line := fmt.Sprintf("runtime=%s uninstall=%s remove=%d absent=0 conflict=%d", runtime, statuses[i], remove, conflict)
if statuses[i] == "conflict" && conflictPaths != "" {
line += " conflict_paths=" + conflictPaths
}
output.WriteString(line)
output.WriteByte('\n')
}
return output.String()
}
Expand Down
30 changes: 21 additions & 9 deletions internal/cli/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -128,7 +129,7 @@ func runWithUpdateDependencies(ctx context.Context, args []string, stdout, stder
}
if !apply {
header := fmt.Sprintf("runtime=%s outcome=%s action=%s root=%s mode=plan%s", decision.ID, decision.Outcome, decision.Action, plan.RootPath(), warning)
return writeUpdateOperations(stdout, header, classified, "planned")
return writeUpdateOperations(stdout, header, plan, classified, "planned")
}
return applyUpdate(stdout, stderr, plan, observation, classified, observations, warning)
}
Expand Down Expand Up @@ -266,7 +267,7 @@ func runDirectoryUpdate(stdout, stderr io.Writer, targetDir, catalogDir string,
}
header := fmt.Sprintf("runtime=%s outcome=%s action=%s root=%s mode=plan", decision.ID, decision.Outcome, decision.Action, plan.RootPath())
if !apply {
return writeUpdateOperations(stdout, header, classified, "planned")
return writeUpdateOperations(stdout, header, plan, classified, "planned")
}
return applyDirectoryUpdate(stdout, stderr, plan, observation, classified, header)
}
Expand Down Expand Up @@ -306,7 +307,7 @@ func applyDirectoryUpdate(stdout, stderr io.Writer, plan installplan.Plan, obser
return exitFailure
}
if !report.Ready() {
return writeUpdateOperations(stdout, header, classified, "conflict")
return writeUpdateOperations(stdout, header, plan, classified, "conflict")
}
backupRoot := filepath.Join(plan.RootPath(), ".cortex")
if err := os.MkdirAll(backupRoot, 0o700); err != nil {
Expand All @@ -317,7 +318,7 @@ func applyDirectoryUpdate(stdout, stderr io.Writer, plan installplan.Plan, obser
result, txErr := applyCandidate(plan, observation, backupRoot, backupName)
if txErr != nil {
if errors.Is(txErr, installtxn.ErrConflict) {
return writeUpdateOperations(stdout, header, classified, "conflict")
return writeUpdateOperations(stdout, header, plan, classified, "conflict")
}
writeError(stderr, "update_transaction_failed")
return exitTransaction
Expand Down Expand Up @@ -386,12 +387,16 @@ func updateRefusalReason(outcome runtimematrix.Outcome) string {
}

// writeUpdateOperations prints one read-only operation report with its status.
func writeUpdateOperations(stdout io.Writer, header string, classified installobserve.Result, status string) int {
// A conflict status names the conflicting assets by runtime-relative path in
// the same conflict_paths form install and uninstall use (issue #373).
func writeUpdateOperations(stdout io.Writer, header string, plan installplan.Plan, classified installobserve.Result, status string) int {
output := strings.Builder{}
output.WriteString(header + "\n")
var conflicts []string
for _, item := range classified.ArtifactDecisions() {
if item.Action == ownership.Conflict {
status = "conflict"
conflicts = append(conflicts, item.LogicalID)
}
if item.ModeDrift {
fmt.Fprintf(&output, "operation=%s action=%s mode_drift=true observed_mode=%04o canonical_mode=0600\n", item.LogicalID, item.Action, item.ObservedMode.Perm())
Expand All @@ -402,7 +407,14 @@ func writeUpdateOperations(stdout io.Writer, header string, classified installob
if classified.StateAction() == ownership.Conflict {
status = "conflict"
}
fmt.Fprintf(&output, "operation=state/install-state action=%s\nstatus=%s\n", classified.StateAction(), status)
fmt.Fprintf(&output, "operation=state/install-state action=%s\nstatus=%s", classified.StateAction(), status)
if status == "conflict" {
sort.Strings(conflicts)
if paths := conflictRelativePaths([]installplan.Plan{plan}, conflicts); paths != "" {
output.WriteString(" conflict_paths=" + paths)
}
}
output.WriteString("\n")
_, _ = io.WriteString(stdout, output.String())
if status == "conflict" {
return exitConflict
Expand All @@ -423,7 +435,7 @@ func applyUpdate(stdout, stderr io.Writer, plan installplan.Plan, observation in
return exitFailure
}
if !report.Ready() {
return writeUpdateOperations(stdout, header, classified, "conflict")
return writeUpdateOperations(stdout, header, plan, classified, "conflict")
}
backupRoot := filepath.Join(plan.RootPath(), ".cortex")
if err := os.MkdirAll(backupRoot, 0o700); err != nil {
Expand All @@ -434,7 +446,7 @@ func applyUpdate(stdout, stderr io.Writer, plan installplan.Plan, observation in
result, txErr := applyCandidate(plan, observation, backupRoot, backupName)
if txErr != nil {
if errors.Is(txErr, installtxn.ErrConflict) {
return writeUpdateOperations(stdout, header, classified, "conflict")
return writeUpdateOperations(stdout, header, plan, classified, "conflict")
}
writeError(stderr, "update_transaction_failed")
return exitTransaction
Expand Down Expand Up @@ -511,7 +523,7 @@ func updateActorGuidance(stdout, stderr io.Writer, canonical installplan.Plan, o
}
proof := preflight.Proof() // Retain the exact immutable candidate and evidence.
if !apply {
return writeUpdateOperations(stdout, header, proof.Result(), "planned")
return writeUpdateOperations(stdout, header, canonical, proof.Result(), "planned")
}
backupName := nextUpdateBackupName()
result, err := installtxn.ApplyActorGuidance(canonical, observation, proof, cwd, filepath.Join(canonical.RootPath(), ".cortex"), backupName)
Expand Down
36 changes: 36 additions & 0 deletions internal/cli/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,3 +478,39 @@ func TestUpdatePlanReportsModeDrift(t *testing.T) {
t.Fatalf("stdout %q does not have status=planned or contains status=conflict", stdout)
}
}

// TestUpdateConflictNamesUserOwnedPaths pins issue #373 for the explicit-runtime
// update path: a user-owned file occupying a catalog target is named by its
// runtime-relative path in both plan and apply refusals, and is never touched.
func TestUpdateConflictNamesUserOwnedPaths(t *testing.T) {
fixture := newUpdateFixture(t)
root := updateRootPath(t, fixture, runtimematrix.RuntimeOpenCode)
const relative = "skills/cortex-requirements-analyst/SKILL.md"
userFile := filepath.Join(root, filepath.FromSlash(relative))
mustOK(os.MkdirAll(filepath.Dir(userFile), 0o700))
mustOK(os.WriteFile(userFile, []byte("user-owned"), 0o600))
for _, apply := range []bool{false, true} {
args := []string{"update", "--runtime", "opencode", "--catalog", fixture.catalog}
mode := "plan"
if apply {
args = append(args, "--apply")
mode = "apply"
}
code, stdout, stderr := runUpdateCommand(t, fixture.dependencies(runtimematrix.RuntimeOpenCode), args...)
if code != exitConflict || stderr != "" {
t.Fatalf("conflict update (%s) = (%d, %q, %q)", mode, code, stdout, stderr)
}
if !strings.Contains(stdout, "mode="+mode) || !strings.Contains(stdout, "\noperation=skills/requirements-analyst action=conflict\n") {
t.Fatalf("conflict update (%s) stdout = %q", mode, stdout)
}
if !strings.HasSuffix(stdout, "\nstatus=conflict conflict_paths="+relative+"\n") {
t.Fatalf("conflict update (%s) status line lacks conflict_paths: %q", mode, stdout)
}
if string(must(os.ReadFile(userFile))) != "user-owned" {
t.Fatalf("conflict update (%s) changed the user-owned file", mode)
}
if _, err := os.Lstat(filepath.Join(root, ".cortex", "install-state.json")); !errors.Is(err, fs.ErrNotExist) {
t.Fatalf("conflict update (%s) wrote install state: %v", mode, err)
}
}
}
Loading
Loading