Skip to content

Commit f5144ba

Browse files
committed
fix(cli): name conflicting asset paths in update conflict output
Refs #385
1 parent cfbc99b commit f5144ba

4 files changed

Lines changed: 79 additions & 21 deletions

File tree

‎docs/operating.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ cortex update --runtime pi --catalog ./catalog --apply # the plan, applied
100100

101101
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.
102102

103+
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.
104+
103105
## Upgrade to alpha20
104106

105107
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.

‎internal/cli/install.go‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -235,28 +235,36 @@ func runInstall(ctx context.Context, stdout, stderr io.Writer, runner runtimepro
235235
}
236236

237237
// installConflictPaths renders the conflicting logical assets as runtime-relative
238-
// paths for the operator diagnostic (issue #373). LogicalIDs without a file in
239-
// the composed requests map to their own identity, so the state artifact still
240-
// names itself. Output is deduplicated and sorted by the transaction layer.
238+
// paths for the operator diagnostic (issue #373). Output is deduplicated and
239+
// sorted by the transaction layer.
241240
func installConflictPaths(requests []installtxn.GroupRequest, err error) string {
242241
var conflict *installtxn.ConflictError
243242
if !errors.As(err, &conflict) || len(conflict.LogicalIDs) == 0 {
244243
return ""
245244
}
246-
relative := make(map[string]string)
245+
plans := make([]installplan.Plan, 0, len(requests))
247246
for _, request := range requests {
248-
for _, file := range request.Plan.Files() {
247+
plans = append(plans, request.Plan)
248+
}
249+
return conflictRelativePaths(plans, conflict.LogicalIDs)
250+
}
251+
252+
// conflictRelativePaths maps conflicting LogicalIDs to the runtime-relative
253+
// paths their plans compose, preserving the given order and dropping
254+
// duplicates. LogicalIDs without a file in the plans (the state artifact) are
255+
// omitted, so every caller renders the same conflict_paths representation.
256+
func conflictRelativePaths(plans []installplan.Plan, logicalIDs []string) string {
257+
relative := make(map[string]string)
258+
for _, plan := range plans {
259+
for _, file := range plan.Files() {
249260
relative[file.LogicalID()] = file.RelativePath()
250261
}
251262
}
252-
paths := make([]string, 0, len(conflict.LogicalIDs))
253-
seen := make(map[string]bool, len(conflict.LogicalIDs))
254-
for _, id := range conflict.LogicalIDs {
263+
paths := make([]string, 0, len(logicalIDs))
264+
seen := make(map[string]bool, len(logicalIDs))
265+
for _, id := range logicalIDs {
255266
path, ok := relative[id]
256-
if !ok {
257-
continue
258-
}
259-
if seen[path] {
267+
if !ok || seen[path] {
260268
continue
261269
}
262270
seen[path] = true

‎internal/cli/update.go‎

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"os"
99
"path/filepath"
10+
"sort"
1011
"strconv"
1112
"strings"
1213
"time"
@@ -128,7 +129,7 @@ func runWithUpdateDependencies(ctx context.Context, args []string, stdout, stder
128129
}
129130
if !apply {
130131
header := fmt.Sprintf("runtime=%s outcome=%s action=%s root=%s mode=plan%s", decision.ID, decision.Outcome, decision.Action, plan.RootPath(), warning)
131-
return writeUpdateOperations(stdout, header, classified, "planned")
132+
return writeUpdateOperations(stdout, header, plan, classified, "planned")
132133
}
133134
return applyUpdate(stdout, stderr, plan, observation, classified, observations, warning)
134135
}
@@ -266,7 +267,7 @@ func runDirectoryUpdate(stdout, stderr io.Writer, targetDir, catalogDir string,
266267
}
267268
header := fmt.Sprintf("runtime=%s outcome=%s action=%s root=%s mode=plan", decision.ID, decision.Outcome, decision.Action, plan.RootPath())
268269
if !apply {
269-
return writeUpdateOperations(stdout, header, classified, "planned")
270+
return writeUpdateOperations(stdout, header, plan, classified, "planned")
270271
}
271272
return applyDirectoryUpdate(stdout, stderr, plan, observation, classified, header)
272273
}
@@ -306,7 +307,7 @@ func applyDirectoryUpdate(stdout, stderr io.Writer, plan installplan.Plan, obser
306307
return exitFailure
307308
}
308309
if !report.Ready() {
309-
return writeUpdateOperations(stdout, header, classified, "conflict")
310+
return writeUpdateOperations(stdout, header, plan, classified, "conflict")
310311
}
311312
backupRoot := filepath.Join(plan.RootPath(), ".cortex")
312313
if err := os.MkdirAll(backupRoot, 0o700); err != nil {
@@ -317,7 +318,7 @@ func applyDirectoryUpdate(stdout, stderr io.Writer, plan installplan.Plan, obser
317318
result, txErr := applyCandidate(plan, observation, backupRoot, backupName)
318319
if txErr != nil {
319320
if errors.Is(txErr, installtxn.ErrConflict) {
320-
return writeUpdateOperations(stdout, header, classified, "conflict")
321+
return writeUpdateOperations(stdout, header, plan, classified, "conflict")
321322
}
322323
writeError(stderr, "update_transaction_failed")
323324
return exitTransaction
@@ -386,12 +387,16 @@ func updateRefusalReason(outcome runtimematrix.Outcome) string {
386387
}
387388

388389
// writeUpdateOperations prints one read-only operation report with its status.
389-
func writeUpdateOperations(stdout io.Writer, header string, classified installobserve.Result, status string) int {
390+
// A conflict status names the conflicting assets by runtime-relative path in
391+
// the same conflict_paths form install and uninstall use (issue #373).
392+
func writeUpdateOperations(stdout io.Writer, header string, plan installplan.Plan, classified installobserve.Result, status string) int {
390393
output := strings.Builder{}
391394
output.WriteString(header + "\n")
395+
var conflicts []string
392396
for _, item := range classified.ArtifactDecisions() {
393397
if item.Action == ownership.Conflict {
394398
status = "conflict"
399+
conflicts = append(conflicts, item.LogicalID)
395400
}
396401
if item.ModeDrift {
397402
fmt.Fprintf(&output, "operation=%s action=%s mode_drift=true observed_mode=%04o canonical_mode=0600\n", item.LogicalID, item.Action, item.ObservedMode.Perm())
@@ -402,7 +407,14 @@ func writeUpdateOperations(stdout io.Writer, header string, classified installob
402407
if classified.StateAction() == ownership.Conflict {
403408
status = "conflict"
404409
}
405-
fmt.Fprintf(&output, "operation=state/install-state action=%s\nstatus=%s\n", classified.StateAction(), status)
410+
fmt.Fprintf(&output, "operation=state/install-state action=%s\nstatus=%s", classified.StateAction(), status)
411+
if status == "conflict" {
412+
sort.Strings(conflicts)
413+
if paths := conflictRelativePaths([]installplan.Plan{plan}, conflicts); paths != "" {
414+
output.WriteString(" conflict_paths=" + paths)
415+
}
416+
}
417+
output.WriteString("\n")
406418
_, _ = io.WriteString(stdout, output.String())
407419
if status == "conflict" {
408420
return exitConflict
@@ -423,7 +435,7 @@ func applyUpdate(stdout, stderr io.Writer, plan installplan.Plan, observation in
423435
return exitFailure
424436
}
425437
if !report.Ready() {
426-
return writeUpdateOperations(stdout, header, classified, "conflict")
438+
return writeUpdateOperations(stdout, header, plan, classified, "conflict")
427439
}
428440
backupRoot := filepath.Join(plan.RootPath(), ".cortex")
429441
if err := os.MkdirAll(backupRoot, 0o700); err != nil {
@@ -434,7 +446,7 @@ func applyUpdate(stdout, stderr io.Writer, plan installplan.Plan, observation in
434446
result, txErr := applyCandidate(plan, observation, backupRoot, backupName)
435447
if txErr != nil {
436448
if errors.Is(txErr, installtxn.ErrConflict) {
437-
return writeUpdateOperations(stdout, header, classified, "conflict")
449+
return writeUpdateOperations(stdout, header, plan, classified, "conflict")
438450
}
439451
writeError(stderr, "update_transaction_failed")
440452
return exitTransaction
@@ -511,7 +523,7 @@ func updateActorGuidance(stdout, stderr io.Writer, canonical installplan.Plan, o
511523
}
512524
proof := preflight.Proof() // Retain the exact immutable candidate and evidence.
513525
if !apply {
514-
return writeUpdateOperations(stdout, header, proof.Result(), "planned")
526+
return writeUpdateOperations(stdout, header, canonical, proof.Result(), "planned")
515527
}
516528
backupName := nextUpdateBackupName()
517529
result, err := installtxn.ApplyActorGuidance(canonical, observation, proof, cwd, filepath.Join(canonical.RootPath(), ".cortex"), backupName)

‎internal/cli/update_test.go‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,3 +478,39 @@ func TestUpdatePlanReportsModeDrift(t *testing.T) {
478478
t.Fatalf("stdout %q does not have status=planned or contains status=conflict", stdout)
479479
}
480480
}
481+
482+
// TestUpdateConflictNamesUserOwnedPaths pins issue #373 for the explicit-runtime
483+
// update path: a user-owned file occupying a catalog target is named by its
484+
// runtime-relative path in both plan and apply refusals, and is never touched.
485+
func TestUpdateConflictNamesUserOwnedPaths(t *testing.T) {
486+
fixture := newUpdateFixture(t)
487+
root := updateRootPath(t, fixture, runtimematrix.RuntimeOpenCode)
488+
const relative = "skills/cortex-requirements-analyst/SKILL.md"
489+
userFile := filepath.Join(root, filepath.FromSlash(relative))
490+
mustOK(os.MkdirAll(filepath.Dir(userFile), 0o700))
491+
mustOK(os.WriteFile(userFile, []byte("user-owned"), 0o600))
492+
for _, apply := range []bool{false, true} {
493+
args := []string{"update", "--runtime", "opencode", "--catalog", fixture.catalog}
494+
mode := "plan"
495+
if apply {
496+
args = append(args, "--apply")
497+
mode = "apply"
498+
}
499+
code, stdout, stderr := runUpdateCommand(t, fixture.dependencies(runtimematrix.RuntimeOpenCode), args...)
500+
if code != exitConflict || stderr != "" {
501+
t.Fatalf("conflict update (%s) = (%d, %q, %q)", mode, code, stdout, stderr)
502+
}
503+
if !strings.Contains(stdout, "mode="+mode) || !strings.Contains(stdout, "\noperation=skills/requirements-analyst action=conflict\n") {
504+
t.Fatalf("conflict update (%s) stdout = %q", mode, stdout)
505+
}
506+
if !strings.HasSuffix(stdout, "\nstatus=conflict conflict_paths="+relative+"\n") {
507+
t.Fatalf("conflict update (%s) status line lacks conflict_paths: %q", mode, stdout)
508+
}
509+
if string(must(os.ReadFile(userFile))) != "user-owned" {
510+
t.Fatalf("conflict update (%s) changed the user-owned file", mode)
511+
}
512+
if _, err := os.Lstat(filepath.Join(root, ".cortex", "install-state.json")); !errors.Is(err, fs.ErrNotExist) {
513+
t.Fatalf("conflict update (%s) wrote install state: %v", mode, err)
514+
}
515+
}
516+
}

0 commit comments

Comments
 (0)