Preserve JSONC authoring in cmux settings writes

Closed
TE
2mo ago
+1761-45

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

Preserves JSONC authoring in CmuxSettings set/reset writes. Previously writes reserialized the whole document; now edits mutate only the targeted path, keeping comments, whitespace, key ordering, and trailing-comma style. Semantic no-ops remain byte-stable and skip the atomic replace.

Behavior

  • Duplicate-key lookup follows JSON's last-wins semantics: set edits the effective occurrence, and reset removes every duplicate leaf and keeps duplicated ancestors empty so shadowed values can't reappear.
  • New nested settings follow the file's existing indentation and trailing-comma style.
  • Resets prune plain empty parents but retain comment-only parents so user documentation survives.
  • Source text is decoded through JSONCSanitizer, so BOM and UTF-8/16/32 encodings survive writes.
  • Symlinked configs resolve once on write so atomic replace updates the target and keeps the link.
  • Malformed or non-dictionary input is refused before any write.
  • The cmux-settings helper uses the same minimal-edit rules while preserving its scalar-intermediate rejection policy.

Coverage

  • Adds store and helper mutation tests for duplicate keys, comments, trailing commas, nested paths, symlinks, malformed input, and byte-stable no-ops.
  • Wires the settings mutation regression suite into the skill-contract workflow.

Addresses Tact issue #79.

Summary by CodeRabbit

  • Bug Fixes

    • Settings updates now preserve comments, formatting, whitespace, ordering, and trailing commas in JSONC configuration files.
    • Added safer handling for symlinked settings files, duplicate keys, malformed input, and no-op changes.
    • Nested settings paths can be created or removed while retaining unrelated documentation and structure.
  • Tests

    • Added regression coverage for lossless JSONC edits across the settings tools and macOS configuration store.