beads/cmd/bd/dolt_remote_proxied_server.go
Harry Miller 29aba4f8b2
fix(config): bd config unset reported success while the key stayed set (#5760)
* fix(config): unset a nested key in config.yaml instead of reporting a no-op

commentOutYamlKey matched only a flat "a.b:" line, so unsetting a key
written in nested form left the file untouched while bd printed
"Unset <key> (in config.yaml)" and the value stayed effective. The write
side does not have this limitation: SetYamlConfig routes dotted keys
through updateNestedYamlKey, and readUserGlobalYamlValue accepts either
shape, so bd could produce and read a nested key it could not remove. The
shipped config.yaml template documents the nested form in its commented
examples, which is the likeliest way a user acquires one.

Locate the leaf through the yaml node tree and comment out its source
line rather than re-marshalling, so the file's comments and layout
survive. A leaf whose value is not a scalar on the key's own line is
refused: commenting one line of a multi-line value would leave the file
unparseable. The flat form is still tried first and still wins when a
file somehow carries both.

* fix(config): do not comment out a yaml key whose value is the block below it

commentOutYamlKey matched any line opening with the key, so unsetting a
key that names a mapping commented out only that line and left the block
beneath it indented under nothing. The result does not parse, and the
next bd command in that workspace fails to read its config rather than
losing one setting.

Skip a match whose value is empty and whose following content line is
indented deeper: that line is the head of a block, not a setting. A key
with a genuinely empty value is still commented out, including at end of
file, where there is no following line to judge.

* fix(config): clear the config.yaml layer on unset of a database-backed key

bd config unset routes a key IsYamlOnlyKey does not claim to
UnsetSetting, which deletes the database row and nothing else. A key that
also appears in .beads/config.yaml therefore survived its own unset:
"Unset <key>" was printed, and bd config show still resolved the value
from config.yaml on the next read. types.custom and types.infra are the
keys this bites, since neither is yaml-only and both are read back out of
the file.

Clear the file layer too when the key is set there, and name the
locations that were actually cleared so the message can be trusted. The
JSON form gains the same location field the yaml-only branch already
emits. Removing the key from config.yaml comments the line out rather
than deleting it, so the file keeps documenting the setting.

Stacked on the nested-key fix: without it this path would report
config.yaml as cleared while a nested key stayed effective.

* style(config): fix the misspell lint on the new doc comment

golangci-lint's misspell linter fails the PR on "re-marshalling" in the
commentOutNestedYamlKey doc comment: the enforced spelling is
"marshaling". Comment-only change.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(config): report the unset location from the write, not a merged pre-check

Two blocking items from review.

1. The location guard read viper's resolved value, not the config.yaml
   layer. config.GetYamlConfig is v.GetString on the merged viper, which
   serves SetDefault values and AutomaticEnv, so every non-yaml-only key
   with a non-empty default (no-hooks, no-db, json,
   events-journal-retain-days, events-journal-auto-prune) looked present in
   config.yaml even in a workspace with no config.yaml at all. That meant
   bd printed "Unset <key> (in database, config.yaml)" over a file it never
   touched - a more specific false claim than the message this PR set out
   to remove - and, with no project config.yaml, failed the command with
   "no .beads/config.yaml found" AFTER the database row was already gone,
   where main printed "Unset <key>" and exited 0.

   UnsetYamlConfig (and UnsetUserYamlConfig) now return whether the file
   was actually changed, threading out the found/ok that
   commentOutFlatYamlKey and commentOutNestedYamlKey already computed. A
   missing project config.yaml, and a key not present in a shape that can
   be safely commented, are both (false, nil) - "not in config.yaml" is an
   answer, not a failure. The command clears the layer unconditionally and
   derives the location from the result, so the string is a report of the
   write rather than a guess made before it. That also settles the
   non-blocking third point: a refusal path can no longer print "Unset ..."
   over a no-op.

2. commentOutNestedYamlKey over-deleted a flow mapping. It accepted any
   leaf whose key and scalar value shared a line, but in flow style the
   leaf's line IS the parent's, so `unset dolt.mode` commented out
   `dolt: {mode: server}` whole and took the entire mapping with it. On
   main the flat regex never matched this, so the loss is new. The leaf now
   has to start at the line's first non-space column.

Also from review: commentOutFlatYamlKey splits instead of scanning, so a
flat unset keeps the file's trailing newline like the nested and no-match
paths always did; and the "flat form wins when both are present" case says
why the nested copy survives, and what `changed` does and does not claim
when it does.

Regression coverage: four commentOutYamlKey cases (two flow-mapping, two
trailing-newline), a changed/content agreement assertion across the whole
table, and two embedded CLI subtests - one proving the location no longer
names config.yaml for a defaulted key that was never written there, one
proving an unset with no project config.yaml exits 0 and does not recreate
the file. All four reproduce red against the pre-fix code.

Reported-by: bee-ghosttrack
Agent-Signature: claude-code-opus-5-unknown-reasoning on behalf of Harry Miller

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(config): address #5760 review — top-level flat match, lists at the key's indent

Blockers from the re-review at d168df1841:

- The flat-key match accepted any indentation, so on the database-backed
  path `bd config unset enabled` commented out a nested backup.enabled and
  reported "in database, config.yaml". A flat key is a top-level key whose
  name contains the dots, so it now matches only at the document's
  top-level indentation, read from the first key line (yaml.v3 accepts a
  document whose whole mapping is indented, which main's "preserve
  indentation" case covers). The same change stops `mode` under
  `dolt: mode: x: 1` from triggering a refusal that did not apply.
- A list at the key's own indentation (`types.custom:` then `- step`)
  slipped past the refusal and left config.yaml unparseable. A `-` item at
  the key's indentation now counts as the key's value.

Smaller:

- Only a comment, an anchor or a tag after the colon (`b:  # note`,
  `base: &b`) still leaves the value to the lines beneath, and is refused.
- Blank-line detection uses TrimSpace, so a CRLF blank line no longer ends
  the value early.
- The message no longer calls a plain scalar on the next line "a mapping or
  list".

Each has a case in TestUnsetRefusesShapesItCannotEdit or
TestCommentOutYamlKey, and undoing each fix fails its case.

Agent-Signature: claude-code-opus-5-5-unknown-reasoning on behalf of Harry Miller
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(config): refuse a single-segment block scalar; pin the tag and dedent branches (#5760 review)

- unsupportedUnsetShape returned early for single-segment keys, so
  `notes: |` with an indented body plus `unset notes` commented the key and
  orphaned the body, and the file stopped parsing. The database-backed
  unset now reaches this path for every key, so single-segment keys are
  checked too.
- New pins: a tag alone after the colon (`m: !!map`) leaves the value
  below, and a dedent ends the value (`outer:` / `  k:` / `z: 1`, unset
  outer.k, is commented). Undoing either branch fails its case.
- The database-path error no longer claims a row was unset.
  UnsetSettingResult cannot say whether one existed.

Agent-Signature: claude-code-opus-5-5-unknown-reasoning on behalf of Harry Miller
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(config): restore CRLF normalization lost by the split-based unset rewrite

Base commentOutYamlKey scanned with bufio.Scanner, whose ScanLines drops a
trailing \r, so CRLF files came back LF-only. The PR switched to
strings.Split to preserve the file final newline, which carried every \r
through. Normalizing at entry keeps both properties: base own
TestCommentOutYamlKeyPreservesTheTrailingNewline now passes 9/9.

* fix(config): keep the no-config.yaml unset failure reportable for yaml-only keys

Base extracted the yaml-only unset into runConfigUnsetYamlOnly, which runs the
leaked-secret row cleanup independently of the YAML edit and folds the row's fate
into the returned error. This PR widened UnsetYamlConfig to (bool, error) and
swallowed the missing-config.yaml case as (false, nil) so its own new
database-backed call site would stop failing after the row was already gone.

Merging both turned base's report into a success, so
TestConfigUnsetCleansTheRowWhenTheYamlUnsetFails lost the error it asserts and
the leaked-credential rotation notice went to stdout instead of the error.

The two call sites need opposite answers, so report the case as a sentinel,
ErrNoProjectConfigYaml, and let each state its own contract. Resolution stays in
findProjectConfigYaml so neither caller re-derives it - a caller-side pre-check
is what produced the false "(in config.yaml)" claims this PR removes.

* fix(config): compare the unset against the normalized content, gate the hint

Maintainer review fixup for PR #5760 (adopt-pr iterations 1 and 2).
Iteration 1 addresses the synthesis Pre-Merge Checklist in full; iteration 2
takes both of its non-gating follow-ups.

New Finding 1 (major) — commentOutYamlKey rewrites CRLF to LF as its first
statement, but both public callers compared its result against the raw
os.ReadFile bytes. On any CRLF config.yaml those always differ, so the `changed`
bool meant "the bytes moved", not "the key was there": unsetting an ABSENT key
returned (true, nil), printed "Unset <key> (in config.yaml)" — the exact false
claim this PR exists to remove — rewrote the whole file to LF, and made the new
"was not set" branch unreachable on a supported platform. Both callers now
normalize once up front and compare like for like, so a no-op leaves the file
byte-identical. A real unset still normalizes to LF, which is base behavior; it
is pinned at the callee and, since iteration 2, through both callers. Note this
was introduced by the adopt lane's own CRLF-restoration repair (59463663e), not
by the contributor.

New Finding 1 test cell (required) — every change-report fixture in the package
was LF, so nothing in-tree could catch this. Added a CRLF absent-key cell for
BOTH callers asserting changed=false and byte-identity. Proven non-vacuous:
reverting one caller to the raw-byte compare reds that caller's subtest on both
assertions and leaves its sibling green.

New Finding 2 (minor) — printConfigSideEffects sat outside the if/else, so the
no-op branch printed "Backup config removed..." right after saying the key was
never set. All four side-effect keys are yaml-only by prefix, so 4/4 route
through the new branch. Gated on `changed`, which covers the --json arm too
since the hint goes to stderr and was never suppressed there.

New Finding 3 (minor) — the yaml-only --json payload rendered location as ""
both for an absent key and for an unpopulated field, with no way to tell them
apart; added a `changed` bool and pinned one --json cell per route.

New Finding 4 (nit) — the ErrNoProjectConfigYaml doc comment was written as a
caller census and was short by two. Re-censused: four non-test callers, taking
three different answers. Reworded to say so.

CHANGELOG: the honesty claim is no longer LF-only; recorded the no-op
byte-identity, the gated hint, and the new --json field.

Iteration 2, minor (test evidence) — the absent-key cell pins only the no-op
direction of the new compare. A caller that returned early whenever the
up-front normalization changed anything passed the whole package, and it turns
every unset on a CRLF config.yaml into a silent "<key> was not set" while the
key stays effective. Added TestUnsetOfAPresentKeyInACRLFFileReportsTheWrite for
both callers: changed=true, the key line commented out, and the file
LF-normalized. Proven non-vacuous: that early return in both callers reds only
this test, both subtests on both assertions; in one caller it reds only that
caller's subtest; and writing CRLF back reds only the file assertion. The rest
of the package stays green under each.

Iteration 2, nit (contract) — the CHANGELOG said `--json` carries a `changed`
boolean, but only the yaml-only route's payload does; the DB-backed route still
emits just key and location. Scoped the sentence to the yaml-only payload.

Validation (scrubbed env — ambient BEADS_* and BD_* variables fake reds here):
internal/config full package is 450 PASS / 0 FAIL; cmd/bd seam selector with
BEADS_TEST_EMBEDDED_DOLT=1 is 63 PASS / 0 FAIL / 2 SKIP over 65 runs, the two
skips being the cells that need a Dolt server. The new embedded cell executes
rather than skipping, and reverting the two config.go changes reds it on both
the payload and the hint assertions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
2026-10-01 05:07:04 -07:00

54 lines
1.3 KiB
Go

package main
import (
"context"
"fmt"
"os"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/storage/uow"
)
func runDoltRemoteRemoveProxied(ctx context.Context, name string) error {
if uowProvider == nil {
return HandleError("proxied-server UOW provider not initialized")
}
err := uow.RunTx(ctx, uowProvider, func(ctx context.Context, uw uow.UnitOfWork) (string, error) {
if err := uw.DoltRemoteUseCase().DeleteRemote(ctx, name); err != nil {
return "", err
}
return fmt.Sprintf("bd: remove remote %s", name), nil
})
if err != nil {
if jsonOutput {
_ = outputJSONError(err, "remote_remove_failed")
} else {
fmt.Fprintf(os.Stderr, "Error removing remote: %v\n", err)
}
return SilentExit()
}
if name == "origin" {
if current := config.GetYamlConfig("sync.remote"); current != "" {
if _, err := config.UnsetYamlConfig("sync.remote"); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to clear sync.remote from config.yaml: %v\n", err)
}
if isGitRepo() {
commitBeadsConfig("bd: clear sync.remote")
}
}
}
if jsonOutput {
if err := outputJSON(map[string]interface{}{
"name": name,
"removed": true,
}); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
} else {
fmt.Printf("Removed remote %q\n", name)
}
return nil
}