Preserve JSONC authoring in cmux settings writes

Merged
TE
2mo ago
+2754-67

Reviewer summary

Editing a setting should not rewrite the whole cmux.json file. This keeps comments, ordering, formatting, and trailing commas while still validating and safely writing the change.

What changed

  • add a narrow dotted-path JSONC editor for CmuxSettings set/reset writes so ordinary changes retain comments, unrelated whitespace, ordering, sections, and existing trailing-comma style
  • keep JSONConfigStore safety behavior: resolve symlink targets once, refuse malformed input, validate edited JSONC before the atomic replace, refresh the cache, and directly signal subscribers after successful writes
  • keep semantic no-ops byte-stable and avoid the atomic replace entirely
  • mirror the same minimal-edit rules in the standalone cmux-settings helper while preserving its existing scalar-intermediate rejection policy
  • retain comment-only empty parents during reset so user-authored documentation survives; plain empty parents are still pruned

Focused coverage

Native and helper tests cover:

  • comments before/after an edited key
  • inline comments
  • trailing commas
  • nested creation/removal
  • unrelated key ordering
  • symlinked configs
  • malformed input refusal
  • byte-stable no-op set/reset

The helper regression suite is also wired into the existing skill-contract workflow.

Local checks

  • swiftc -typecheck JSONCPathEditor.swift
  • python3 -m py_compile cmux_jsonc_edit.py
  • focused standalone mutation cases for replacement, insertion, pruning, comment-only parents, and one-line objects

Tact Lane P: https://github.com/teamleaderleo/Tact/issues/79


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Summary by cubic

Settings writes now edit JSONC source text instead of reserializing the parsed document, preserving comments, ordering, formatting, trailing commas, encoding, and BOMs. Native and cmux-settings writes share a lock and use atomic compare-and-swap publishing, so concurrent changes are rejected without overwriting the winning writer.

  • No-op changes remain byte-stable; malformed or non-dictionary input is refused.
  • Duplicate keys follow each reader’s effective value: set edits that occurrence, while reset removes duplicate paths and hidden leaves.
  • Cache and subscriber updates use the validated document from the successful write.
  • Recovery preserves authored bytes and the losing conflict identity, including when rollback itself races or fails.
  • Lock waits run off the settings actor’s executor, and symlink targets resolve before atomic replacement.
  • The helper preserves scalar-intermediate rejection and infers project scope from the working directory, so a custom global config outside any project stays global.

Coverage

  • Adds native and helper tests for formatting, encodings, duplicates, nested paths, symlinks, locking, compare-and-swap races, recovery, rollback failures, cache agreement, and scope detection.
  • Runs JSONC mutation regressions in the skill-contract workflow and includes cmux_jsonc_edit.py in skill fixtures.
  • Includes the upstream process-identity import fix in Workspace+AgentResumeOwnership.swift (#13236).

Summary by CodeRabbit

  • New Features

    • Settings updates now preserve comments, whitespace, key order, trailing commas, and original text encoding.
    • Added support for creating nested settings paths and removing values while retaining relevant documentation comments.
    • Settings changes are written atomically and safely coordinate concurrent edits, including symlinked configuration files.
    • Project configuration discovery now uses the target settings file location for more accurate scope detection.
  • Bug Fixes

    • Malformed or concurrently modified configuration files are no longer overwritten.
    • No-op updates leave the existing file unchanged.

Replaces https://github.com/manaflow-ai/cmux/pull/13054 (same commits, head branch moved into the org so it gets the build cache and can be kept current with main).

🤖 Generated with Claude Code