Skip to content

fix(#1071): preserve .sequant/settings.json on re-init - #1092

Merged
admarble merged 2 commits into
mainfrom
feature/1071-bug-init-init-agent-name-on-an-initialized-repo-ov
Sep 19, 2026
Merged

admarble merged 2 commits into
mainfrom
feature/1071-bug-init-init-agent-name-on-an-initialized-repo-ov

Conversation

@admarble

Copy link
Copy Markdown
Contributor

Summary

  • createDefaultSettings wrote generateSettingsJsonc(DEFAULT_SETTINGS) straight over .sequant/settings.json, so an init over an existing install reset run.timeout (5400 → 1800) and deleted the whole run.phases block — under the success banner, with no diff, no backup and no mention.
  • Guarded: when the file exists and --force is absent, only the keys the flags actually set are merged in (today run.agent); when no flag key changes, nothing is written at all, so the file stays byte-identical, JSONC comments included. --force keeps the old overwrite as the documented escape hatch.
  • The merge lands on the raw parsed object, never round-tripping through generateSettingsJsonc (emits no run.phases / run.modelRoles / scopeAssessment) or validateSettings (nested Zod schemas strip unknown keys) — either route would reproduce the exact data loss being fixed.
  • The guard keys off .sequant/settings.json itself, not the .claude/settings.json that init's "already initialized" warning checks: a repo with tuned settings and no .claude/settings.json was clobbered with no warning at all.
  • settingsExist() — exported and dead since it was written — is now module-local and is the guard (AC-6).

Pre-PR AC Verification

AC Source Description Status Evidence
AC-1 Original Existing settings preserved; run.timeout / run.phases survive ✅ Implemented src/lib/settings.ts:1285 guard; init.test.ts case "AC-1" — no write to the settings path at all
AC-2 Original --agent codex still records run.agent while preserving the rest (#1059 kept) ✅ Implemented preserveExistingSettings raw-object merge; init.test.ts case "AC-2"
AC-3 Original --force still overwrites with defaults ✅ Implemented !force && in the guard; init.test.ts case "AC-3" asserts timeout back to 1800 and no phases
AC-4 Original init reports what it preserved instead of succeeding silently ✅ Implemented src/commands/init.ts console.log of the decision; init.test.ts case "AC-4" asserts captured stdout names the file
AC-5 Original Clobber impossible with no .claude/settings.json either ✅ Implemented Guard reads SETTINGS_PATH; init.test.ts case "AC-5" with a fixture that has only .sequant/settings.json
AC-6 Original settingsExist() used by the new guard or deleted — no exported dead code ✅ Implemented export dropped; called by createDefaultSettings. Evidence command prints PASS
AC-7 Original Manual end-to-end repro prints the preserved result ✅ Implemented Captured output below
Derived
D-1 Error Handling (Quality Plan) Invalid-JSON settings file preserved untouched with a warning, never replaced by defaults ✅ Implemented preserveExistingSettings catch branch; init.test.ts case "derived: leaves an unparseable settings file untouched and warns"
D-2 Open Question 1 (spec) An explicit --agent claude-code switches back off a recorded driver instead of a silent no-op ✅ Implemented Absent run.agent treated as claude-code; init.test.ts case "explicit --agent claude-code switches back"

AC-7 — manual end-to-end repro

Ran the issue's script from the worktree root against this branch's own dist/bin/cli.js (not the global sequant, which points at main's dist). Captured output:

RESULT: PRESERVED (agent recorded, tuned keys intact)

The resulting .sequant/settings.json — run.agent recorded, tuned keys intact, run.phases still whole:

{
  "version": 1,
  "run": {
    "agent": "codex",
    "timeout": 5400,
    "phases": { "spec": "sonnet", "exec": "sonnet", "qa": "opus" }
  }
}

And the decision init printed (AC-4), from the same run:

📋 Preserved existing .sequant/settings.json (updated only run.agent) — use --force to replace it with defaults

AC-6 evidence

$ bash -c 'grep -q settingsExist src/commands/init.ts || ! grep -q "export async function settingsExist" src/lib/settings.ts' && echo PASS
PASS

Mutation verification

Tree was committed clean before each probe and restored after (git status --short → 0).

Mutation-verified: AC-1 — removed the if (!force && (await settingsExist())) guard from createDefaultSettings so the defaults write unconditionally; init.test.ts > 1071 … > AC-1: preserves run.timeout and run.phases instead of writing defaults failed; restored.

Mutation-verified: AC-5 — keyed settingsExist() off .claude/settings.json instead of SETTINGS_PATH; init.test.ts > 1071 … > AC-5: preserves even with no .claude/settings.json present failed — and it was the only failure, so the case genuinely discriminates the file the guard keys off; restored.

Test plan

Known gaps

  • The single preservation rule every writer consults is explicitly out of scope (issue's "Out of scope"), and so are the sibling sites the spec flagged in the same class: writeCodexConfig overwrites .codex/config.toml unconditionally and saveConfig overwrites .claude/.sequant/config.json. Both want a follow-up issue rather than widening this PR.
  • On the merge branch the file is rewritten as plain JSON, so a user's JSONC comments are lost when a flag key actually changes (the no-change path keeps them). Same precedent as saveSettings; a surgical JSONC line patch was considered and rejected in spec review as two code paths plus a regex over user-formatted JSONC.

Closes #1071

🤖 Generated with Claude Code

createDefaultSettings wrote defaults over an existing settings file, resetting run.timeout and deleting run.phases under a success banner. Guard on the file it writes; merge only flag-set keys onto the raw parsed object; report the decision; --force keeps the overwrite.

Entire-Checkpoint: 7c277720c97f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(init): init --agent <name> on an initialized repo overwrites .sequant/settings.json with defaults

1 participant