From 245ab1f1388d00cc8e6c1d01f9ddb1e48e09a463 Mon Sep 17 00:00:00 2001 From: Eddie the Engineer Date: Mon, 19 Oct 2026 02:54:11 +0000 Subject: [PATCH 01/14] fix(config): refuse to unset a nested value that wraps onto the next line `bd config unset sync.remote` on sync: remote: {url: a, branch: b} commented out the key line only and exited 0. The rest of the value stayed behind and re-read as `sync.branch: "b}"`, a key nobody set. unsupportedUnsetShape refused a flow-style parent and a block-scalar leaf, but a flow or quoted leaf that wraps onto the next line was neither. Add spansLinesUnsetRefusal. It cuts the document off after the key's line and parses what is left. Every entry above that line is finished, and the enclosing block mappings end with the document, so the cut parses whenever the entry ends on that line, and fails while a flow collection or quoted string opened on it is still open. Cutting rather than parsing the line alone keeps anchors in scope: `remote: *r` and `remote: [*r, x]` are whole entries that reference an anchor defined above them. The nested walk now keeps the leaf's key node and runs the probe after the flow-style and block-scalar checks, so those shapes keep their specific diagnoses. A plain scalar continued onto the next line still parses when cut, so it stays out of reach, as the design scoped it; the doc says so. AC: AC-1 Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 --- internal/config/yaml_config.go | 41 ++++++++++++++++++- internal/config/yaml_nested_roundtrip_test.go | 7 ++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/internal/config/yaml_config.go b/internal/config/yaml_config.go index 9a2630c42..be47f09ca 100644 --- a/internal/config/yaml_config.go +++ b/internal/config/yaml_config.go @@ -1456,6 +1456,7 @@ func unsupportedUnsetShape(content, key string) error { } node := root.Content[0] + var keyNode *yaml.Node parents := make([]*yaml.Node, 0, len(segments)) for _, segment := range segments { if node.Kind != yaml.MappingNode { @@ -1466,7 +1467,7 @@ func unsupportedUnsetShape(content, key string) error { return nil } parents = append(parents, node) - node = node.Content[idx+1] + keyNode, node = node.Content[idx], node.Content[idx+1] } for i, parent := range parents { @@ -1479,7 +1480,10 @@ func unsupportedUnsetShape(content, key string) error { } return fmt.Errorf("cannot unset %q: %s is written in flow style ({...}), which bd cannot edit; remove the key by hand", key, where) } - return blockScalarUnsetRefusal(node, key) + if err := blockScalarUnsetRefusal(node, key); err != nil { + return err + } + return spansLinesUnsetRefusal(content, keyNode, key) } // blockScalarUnsetRefusal refuses a key whose value is written as a block @@ -1493,6 +1497,39 @@ func blockScalarUnsetRefusal(valueNode *yaml.Node, key string) error { return fmt.Errorf("cannot unset %q: its value is a block scalar (| or >), and commenting the key out would leave the body behind as a value of its own; remove the key by hand", key) } +// spansLinesUnsetRefusal refuses a key of a block mapping whose value starts on +// the key's own line and does not end there: a flow collection or a quoted +// string wrapped onto the next line (`remote: {url: a,` then `branch: b}`). +// The line matcher comments out only the key's line, so the rest of the value +// would stay behind with its opening bracket or quote gone: as a line that +// re-reads as a key nobody set (`branch: b}`), or one that stops the file from +// parsing. +// +// It is decided by where the entry ends, not by the value's style: the same +// collection written on one line comments out cleanly. Everything above an +// entry of a block mapping is either a finished entry or an enclosing block +// mapping, and those end with the document, so the document cut off after the +// key's line parses whenever the entry ends on that line, and fails while a +// flow collection or quoted string opened on it is still open. Cutting the +// document rather than parsing the one line on its own, as replaceFlatKeyLine +// does, keeps anchors in scope: `remote: *r` is a whole entry, but its alias +// resolves only against an anchor defined above it. +// +// A plain scalar continued onto the next line (`remote: file:///a`, then a +// deeper `b`) is not caught: cut after any of its lines it still parses, as a +// shorter value, and yaml.v3 records where a value starts, not where it ends. +func spansLinesUnsetRefusal(content string, keyNode *yaml.Node, key string) error { + lines := strings.Split(content, "\n") + if keyNode.Line < 1 || keyNode.Line > len(lines) { + return nil + } + var probe yaml.Node + if yaml.Unmarshal([]byte(strings.Join(lines[:keyNode.Line], "\n")), &probe) == nil { + return nil + } + return fmt.Errorf("cannot unset %q: its value spans more than one line, and commenting the key out would leave the rest of it behind; remove the key by hand", key) +} + // blockScalarIndent reports the indentation of a key whose value is a literal // or folded block scalar (`notes: |`), or -1 when the line opens no block. func blockScalarIndent(line string) int { diff --git a/internal/config/yaml_nested_roundtrip_test.go b/internal/config/yaml_nested_roundtrip_test.go index df464551a..8229f71d7 100644 --- a/internal/config/yaml_nested_roundtrip_test.go +++ b/internal/config/yaml_nested_roundtrip_test.go @@ -507,6 +507,13 @@ func TestUnsetRefusesShapesItCannotEdit(t *testing.T) { {name: "plain scalar on the next line", seed: "dolt.mode:\n server\n", key: "dolt.mode", want: "lines beneath it"}, // A CRLF blank line is a lone carriage return, not content at column 0. {name: "mapping behind a CRLF blank line", seed: "backup:\r\n\r\n enabled: false\r\n", key: "backup", want: "lines beneath it"}, + // A value that starts on the key's line and wraps onto the next one: + // commenting the key line out leaves the rest of the value behind + // without its opening bracket or quote, as a line that re-reads as a + // key of the section (`sync.branch: "b}"`) or one that stops the file + // from parsing. The same value written on one line still comments out + // cleanly. + {name: "flow mapping wrapped onto the next line", seed: "sync:\n remote: {url: a,\n branch: b}\nnode_id: mini\n", key: "sync.remote", want: "spans more than one line"}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { From a88917d69ff421fc956e0f88401ff027d7e9dbc9 Mon Sep 17 00:00:00 2001 From: Eddie the Engineer Date: Mon, 19 Oct 2026 02:57:52 +0000 Subject: [PATCH 02/14] fix(config): refuse to unset a flat dotted key whose value wraps The flat spelling has the same hole as the nested one: sync.remote: {url: a, branch: b} node_id: mini unset commented out the key line and exited 0. With its `{` gone, the indented `branch: b}` parsed as the whole document, so the file read as `branch: "b}"` and `node_id` read as unset (measured on the base through GetStringFromDir). The flat block of unsupportedUnsetShape already ran the block-scalar check on the top-level entry; it now runs spansLinesUnsetRefusal on that entry's key node as well. The unsupportedUnsetShape doc now names the third shape and the one wrapped value it still cannot see (a plain scalar continued onto the next line), and the [Unreleased] CHANGELOG entry names the new refusal next to the flow-style and block-scalar ones. AC: AC-2 Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 13 +++++--- internal/config/yaml_config.go | 31 ++++++++++++------- internal/config/yaml_nested_roundtrip_test.go | 4 +++ 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a54cb677a..232577f92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2862,16 +2862,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `sync.remote` silently left the remote live. Keys the write does not own, including dotted ones somebody else put there, are left exactly as they are. - Four things that used to succeed now report an error instead, because each one + Five things that used to succeed now report an error instead, because each one produced a value no reader could see, or claimed to remove a value it had left live. On `bd config set`: a key under a parent that already holds a value (`sync: enabled`, then `bd config set sync.remote`), and a key in a file whose top level is not a mapping. On `bd config unset`: a key inside a flow-style mapping (`sync: {remote: "..."}`), which bd matches by line and so cannot - edit, and a key whose value is a block scalar (`remote: |`, in either - spelling), where commenting the key out would leave the body behind as a value - of its own. Unsetting a key that is not set remains a successful no-op in - every shape. + edit; a key whose value is a block scalar (`remote: |`, in either spelling), + where commenting the key out would leave the body behind as a value of its + own; and a key whose value starts on its line and wraps onto the next (a + `{...}` or `[...]` collection or a quoted string spread over lines, in either + spelling), where commenting the key line out would leave the rest of the value + behind. The same value written on one line still unsets. Unsetting a key that + is not set remains a successful no-op in every shape. - **A self-hosted GitLab no longer re-creates its own issues on every push** ([#6735](https://github.com/gastownhall/beads/issues/6735)). The tracker decided whether a stored `external_ref` was one of its own by looking for diff --git a/internal/config/yaml_config.go b/internal/config/yaml_config.go index be47f09ca..43bca9efd 100644 --- a/internal/config/yaml_config.go +++ b/internal/config/yaml_config.go @@ -1414,18 +1414,24 @@ func (w *nestedKeyWalk) step(name string, indent int) bool { return false } -// unsupportedUnsetShape reports the two shapes named in the changelog — a key -// under a flow-style mapping, and a key whose value is a block scalar — in -// whichever spelling the file uses. Both used to be silent: the flow-style one -// reported success while the value stayed live, and the block scalar had its key -// line commented out while the body stayed behind as a value of its own. The set -// direction already refuses what it cannot write correctly, so unset says so too. +// unsupportedUnsetShape reports the shapes named in the changelog — a key under +// a flow-style mapping, a key whose value is a block scalar, and a key whose +// flow collection or quoted string wraps past its own line — in whichever +// spelling the file uses. All three used to be silent: the flow-style one +// reported success while the value stayed live, and the other two had their +// key line commented out while the rest of the value stayed behind, to be +// re-read as something else or to stop the file from parsing. The set +// direction already refuses what it cannot write correctly, so unset says so +// too. // -// This is a list of known shapes, not a decision procedure for the whole class: -// a value that spans lines in flow or quoted style lands its body in the same -// place and is still silent. A mapping-valued key is refused by the line -// matcher itself (mappingValueUnsetRefusal), which also covers single-segment -// keys this function never walks. +// The first two are named shapes with diagnoses of their own. The third is +// decided by where the entry ends rather than by the value's style +// (spansLinesUnsetRefusal), so the same value written on one line still +// comments out cleanly. A plain scalar continued onto the next line wraps as +// well and is still silent: nothing yaml.v3 records says where it ends. A +// mapping-valued key is refused by the line matcher itself +// (mappingValueUnsetRefusal), which also covers single-segment keys this +// function never walks. // // Only a key that is actually present can be refused: unsetting a key that was // never there has always been a successful no-op, and staying silent about a @@ -1452,6 +1458,9 @@ func unsupportedUnsetShape(content, key string) error { if err := blockScalarUnsetRefusal(top.Content[idx+1], key); err != nil { return err } + if err := spansLinesUnsetRefusal(content, top.Content[idx], key); err != nil { + return err + } } } diff --git a/internal/config/yaml_nested_roundtrip_test.go b/internal/config/yaml_nested_roundtrip_test.go index 8229f71d7..cee8a2a5d 100644 --- a/internal/config/yaml_nested_roundtrip_test.go +++ b/internal/config/yaml_nested_roundtrip_test.go @@ -514,6 +514,10 @@ func TestUnsetRefusesShapesItCannotEdit(t *testing.T) { // from parsing. The same value written on one line still comments out // cleanly. {name: "flow mapping wrapped onto the next line", seed: "sync:\n remote: {url: a,\n branch: b}\nnode_id: mini\n", key: "sync.remote", want: "spans more than one line"}, + // Under the flat spelling the rest lands at the top level, where the + // indented `branch: b}` parses as the whole document and the keys + // after it (`node_id`) stop reading at all. + {name: "flow mapping wrapped under the flat spelling", seed: "sync.remote: {url: a,\n branch: b}\nnode_id: mini\n", key: "sync.remote", want: "spans more than one line"}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { From e5a2650b86b4e27c9df1e01b5d8cbfa24b6b669e Mon Sep 17 00:00:00 2001 From: Eddie the Engineer Date: Mon, 19 Oct 2026 02:58:34 +0000 Subject: [PATCH 03/14] test(config): pin the span refusal when the rest closes at the key's indentation sync: remote: {url: a, branch: b} On the base, unset commented out the key line and exited 0, and the leftover `branch: b}` re-read as `sync.branch`. The leftover sits at the key's own indentation, so it looks like a sibling key, not like lines beneath the key: a rule that looks for deeper-indented lines, like the one behind the "lines beneath it" refusal, would let it through. The AC-1 probe refuses it because the document cut after the key's line ends inside an unclosed `{`. This row fails on the base (run with yaml_config.go overlaid from 5c9c02ab77a3) and passes at head. AC: AC-3 Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 --- internal/config/yaml_nested_roundtrip_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/config/yaml_nested_roundtrip_test.go b/internal/config/yaml_nested_roundtrip_test.go index cee8a2a5d..929ba4532 100644 --- a/internal/config/yaml_nested_roundtrip_test.go +++ b/internal/config/yaml_nested_roundtrip_test.go @@ -518,6 +518,10 @@ func TestUnsetRefusesShapesItCannotEdit(t *testing.T) { // indented `branch: b}` parses as the whole document and the keys // after it (`node_id`) stop reading at all. {name: "flow mapping wrapped under the flat spelling", seed: "sync.remote: {url: a,\n branch: b}\nnode_id: mini\n", key: "sync.remote", want: "spans more than one line"}, + // The rest can sit at the key's own indentation, where it reads as a + // sibling key rather than as lines beneath this one, so no indentation + // rule sees it; only where the entry ends does. + {name: "flow mapping that closes at the key's own indentation", seed: "sync:\n remote: {url: a,\n branch: b}\nnode_id: mini\n", key: "sync.remote", want: "spans more than one line"}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { From 3b605d971637712da675a9b65f5600d50c337a51 Mon Sep 17 00:00:00 2001 From: Eddie the Engineer Date: Mon, 19 Oct 2026 02:58:54 +0000 Subject: [PATCH 04/14] test(config): pin the span refusal for a wrapped flow sequence types.custom: [step, slot] On the base, unset commented out the key line, left ` slot]` behind at the top level, and exited 0, so every later read of the file failed to parse. The AC-2 flat-spelling probe already refuses it. This row fails on the base (run with yaml_config.go overlaid from 5c9c02ab77a3) and passes at head. AC: AC-4 Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 --- internal/config/yaml_nested_roundtrip_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/config/yaml_nested_roundtrip_test.go b/internal/config/yaml_nested_roundtrip_test.go index 929ba4532..404ceaec9 100644 --- a/internal/config/yaml_nested_roundtrip_test.go +++ b/internal/config/yaml_nested_roundtrip_test.go @@ -522,6 +522,7 @@ func TestUnsetRefusesShapesItCannotEdit(t *testing.T) { // sibling key rather than as lines beneath this one, so no indentation // rule sees it; only where the entry ends does. {name: "flow mapping that closes at the key's own indentation", seed: "sync:\n remote: {url: a,\n branch: b}\nnode_id: mini\n", key: "sync.remote", want: "spans more than one line"}, + {name: "flow sequence wrapped onto the next line", seed: "types.custom: [step,\n slot]\nnode_id: mini\n", key: "types.custom", want: "spans more than one line"}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { From 9fe128536fa6f41c6cb81b1d14b58a92aaa24637 Mon Sep 17 00:00:00 2001 From: Eddie the Engineer Date: Mon, 19 Oct 2026 03:02:57 +0000 Subject: [PATCH 05/14] test(config): pin the span refusal for a wrapped double-quoted string notes: "line one line two" A double-quoted string may wrap; YAML folds the break into a space. On the base, unset commented out the key line and exited 0, and the leftover `line two"` made the next read fail with "mapping values are not allowed in this context". The flat-block probe from AC-2 already refuses it, single-segment key included. This row fails on the base (run with yaml_config.go overlaid from 5c9c02ab77a3) and passes at head. AC: AC-5 Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 --- internal/config/yaml_nested_roundtrip_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/config/yaml_nested_roundtrip_test.go b/internal/config/yaml_nested_roundtrip_test.go index 404ceaec9..76d4f4adf 100644 --- a/internal/config/yaml_nested_roundtrip_test.go +++ b/internal/config/yaml_nested_roundtrip_test.go @@ -523,6 +523,9 @@ func TestUnsetRefusesShapesItCannotEdit(t *testing.T) { // rule sees it; only where the entry ends does. {name: "flow mapping that closes at the key's own indentation", seed: "sync:\n remote: {url: a,\n branch: b}\nnode_id: mini\n", key: "sync.remote", want: "spans more than one line"}, {name: "flow sequence wrapped onto the next line", seed: "types.custom: [step,\n slot]\nnode_id: mini\n", key: "types.custom", want: "spans more than one line"}, + // A quoted string can wrap as well; YAML folds the break into a space. + // Left behind, `line two"` stops the file from parsing. + {name: "double-quoted string wrapped onto the next line", seed: "notes: \"line one\n line two\"\nnode_id: mini\n", key: "notes", want: "spans more than one line"}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { From 17ff27311050a1a80306d047a4f97853f11be81c Mon Sep 17 00:00:00 2001 From: Eddie the Engineer Date: Mon, 19 Oct 2026 03:04:51 +0000 Subject: [PATCH 06/14] test(config): pin the span refusal for a wrapped single-quoted string dolt.host: '10.0.0.1 more' On the base, unset commented out the key line and exited 0, and the leftover `more'` made the next read fail with "mapping values are not allowed in this context". The flat-block probe from AC-2 already refuses it. This row fails on the base (run with yaml_config.go overlaid from 5c9c02ab77a3) and passes at head. AC: AC-6 Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 --- internal/config/yaml_nested_roundtrip_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/config/yaml_nested_roundtrip_test.go b/internal/config/yaml_nested_roundtrip_test.go index 76d4f4adf..070b1843b 100644 --- a/internal/config/yaml_nested_roundtrip_test.go +++ b/internal/config/yaml_nested_roundtrip_test.go @@ -526,6 +526,7 @@ func TestUnsetRefusesShapesItCannotEdit(t *testing.T) { // A quoted string can wrap as well; YAML folds the break into a space. // Left behind, `line two"` stops the file from parsing. {name: "double-quoted string wrapped onto the next line", seed: "notes: \"line one\n line two\"\nnode_id: mini\n", key: "notes", want: "spans more than one line"}, + {name: "single-quoted string wrapped under the flat spelling", seed: "dolt.host: '10.0.0.1\n more'\nnode_id: mini\n", key: "dolt.host", want: "spans more than one line"}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { From 92f3b50d0017fd8f71bf1da96a6b689a9c225057 Mon Sep 17 00:00:00 2001 From: Eddie the Engineer Date: Mon, 19 Oct 2026 03:06:02 +0000 Subject: [PATCH 07/14] test(config): pin the span refusal on a CRLF file The AC-1 seed with CRLF line endings. On the base, unset exited 0, left `sync.branch: "b}"` behind, and rewrote the whole file to LF. The gate runs on the LF-normalized text inside commentOutYamlKey, and both callers return before writing on its error, so at head the refusal names the key and the file keeps every carriage return. This row fails on the base (run with yaml_config.go overlaid from 5c9c02ab77a3) and passes at head. AC: AC-7 Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 --- internal/config/yaml_nested_roundtrip_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/config/yaml_nested_roundtrip_test.go b/internal/config/yaml_nested_roundtrip_test.go index 070b1843b..3a1cade59 100644 --- a/internal/config/yaml_nested_roundtrip_test.go +++ b/internal/config/yaml_nested_roundtrip_test.go @@ -527,6 +527,9 @@ func TestUnsetRefusesShapesItCannotEdit(t *testing.T) { // Left behind, `line two"` stops the file from parsing. {name: "double-quoted string wrapped onto the next line", seed: "notes: \"line one\n line two\"\nnode_id: mini\n", key: "notes", want: "spans more than one line"}, {name: "single-quoted string wrapped under the flat spelling", seed: "dolt.host: '10.0.0.1\n more'\nnode_id: mini\n", key: "dolt.host", want: "spans more than one line"}, + // The probe reads the file after CRLF is normalized to LF, and a + // refusal writes nothing, so a CRLF file keeps its line endings too. + {name: "flow mapping wrapped in a CRLF file", seed: "sync:\r\n remote: {url: a,\r\n branch: b}\r\nnode_id: mini\r\n", key: "sync.remote", want: "spans more than one line"}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { From 4f2bb71741ccae611a0d2a9376fadc4319c40a77 Mon Sep 17 00:00:00 2001 From: Eddie the Engineer Date: Mon, 19 Oct 2026 03:07:00 +0000 Subject: [PATCH 08/14] test(config): pin the span refusal on the user-global config.yaml UnsetUserYamlConfig runs the same commentOutYamlKey gate. With node_id: "mini box" in the user config, the base exited 0, reported a change, and left ` box"` behind as the only content of the file. At head the unset names node_id and the shape, reports no change, and leaves the file byte-identical. The test sits next to the other UnsetUserYamlConfig cases and isolates HOME and XDG_CONFIG_HOME the same way. It fails on the base (run with yaml_config.go overlaid from 5c9c02ab77a3) and passes at head. AC: AC-8 Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 --- internal/config/yaml_unset_newline_test.go | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/internal/config/yaml_unset_newline_test.go b/internal/config/yaml_unset_newline_test.go index 47dea8907..a3b5569bd 100644 --- a/internal/config/yaml_unset_newline_test.go +++ b/internal/config/yaml_unset_newline_test.go @@ -255,6 +255,46 @@ func TestUnsetOfAPresentKeyInACRLFFileReportsTheWrite(t *testing.T) { }) } +// The user-global config.yaml goes through the same line matcher, so a value +// that wraps onto the next line is refused there as well, with nothing written +// and no change reported. node_id has a single segment, so this is the +// top-level half of the refusal. +func TestUnsetUserYamlConfigRefusesAValueThatSpansLines(t *testing.T) { + const body = "node_id: \"mini\n box\"\n" + + t.Setenv("HOME", t.TempDir()) + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + path, err := UserConfigYamlPath() + if err != nil { + t.Fatalf("UserConfigYamlPath: %v", err) + } + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte(body), 0o600); err != nil { + t.Fatal(err) + } + + changed, err := UnsetUserYamlConfig("node_id") + if err == nil { + after, _ := os.ReadFile(path) //nolint:gosec,errcheck // test-owned temp path; diagnostic + t.Fatalf("UnsetUserYamlConfig reported success on a value that spans two lines:\n%q", string(after)) + } + if !strings.Contains(err.Error(), "node_id") || !strings.Contains(err.Error(), "spans more than one line") { + t.Errorf("the error names neither the key nor the shape: %v", err) + } + if changed { + t.Error("UnsetUserYamlConfig() changed = true for an unset it refused") + } + after, err := os.ReadFile(path) //nolint:gosec // test-owned temp path + if err != nil { + t.Fatal(err) + } + if string(after) != body { + t.Errorf("a refused unset still changed the file:\ngot: %q\nwant: %q", string(after), body) + } +} + // The set twin of TestCommentOutYamlKeyPreservesTheTrailingNewline. // updateYamlKey's flat-key path read the document with bufio.Scanner and joined // the lines back with "\n" — the shape commentOutYamlKey used to have — so it From e4341dcb84784e5272f8ca9d7c649019185840d5 Mon Sep 17 00:00:00 2001 From: Eddie the Engineer Date: Mon, 19 Oct 2026 03:07:52 +0000 Subject: [PATCH 09/14] test(config): pin that a one-line flow leaf still unsets sync: remote: {url: a, branch: b} is a whole entry on the key's line, so commenting that line out removes it cleanly. The unset reports a change, sync.remote reads empty, and node_id still reads "mini", so the file still parses. This guards against the fix being too broad, so it passes on the base too. A mutant that refuses any flow-style leaf by its style (overlaid yaml_config.go) fails this row with "spans more than one line" and leaves every refusal row passing. The row therefore separates the where-the-entry-ends probe from a style rule. AC: AC-9 Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 --- internal/config/yaml_nested_roundtrip_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/internal/config/yaml_nested_roundtrip_test.go b/internal/config/yaml_nested_roundtrip_test.go index 3a1cade59..9a5ee6a25 100644 --- a/internal/config/yaml_nested_roundtrip_test.go +++ b/internal/config/yaml_nested_roundtrip_test.go @@ -410,6 +410,17 @@ func TestUnsetRemovesADottedKeyInEveryShape(t *testing.T) { key: "dolt.host", survivors: map[string]string{"other.dolt.host": "should-survive"}, }, + { + // A flow collection that opens and closes on the key's own line is + // a whole entry on that line, so commenting the line out removes + // it cleanly. TestUnsetRefusesShapesItCannotEdit refuses the same + // collection wrapped onto the next line; that refusal is decided by + // where the entry ends, not by its style, and must not reach this. + name: "flow mapping leaf on one line", + seed: "sync:\n remote: {url: a, branch: b}\nnode_id: mini\n", + key: "sync.remote", + survivors: map[string]string{"node_id": "mini"}, + }, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { From aeea88fbade8b92543ae457ff6ffc6ad0a1fe53b Mon Sep 17 00:00:00 2001 From: Eddie the Engineer Date: Mon, 19 Oct 2026 03:09:50 +0000 Subject: [PATCH 10/14] test(config): pin that an alias to an anchor above still unsets sync: remote: &r file:///a backup: remote: *r `backup.remote` is a whole entry on its line, but `remote: *r` on its own does not parse, because the anchor is defined above it. The unset reports a change, and sync.remote still reads file:///a. A second row puts the alias inside a one-line collection (`remotes: [*r, file:///b]`). Both rows guard against the fix being too broad, so both pass on the base. Graded with overlaid yaml_config.go mutants, all 25 refusal rows passing under each: - probe the key's line on its own: both rows fail; - the same plus an exception for a value that is a single bare alias: the scalar row passes, the collection row fails. Cutting the document after the key's line, as spansLinesUnsetRefusal does, keeps the anchor in scope and passes both. AC: AC-10 Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 --- internal/config/yaml_nested_roundtrip_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/internal/config/yaml_nested_roundtrip_test.go b/internal/config/yaml_nested_roundtrip_test.go index 9a5ee6a25..5dd6d7aa5 100644 --- a/internal/config/yaml_nested_roundtrip_test.go +++ b/internal/config/yaml_nested_roundtrip_test.go @@ -421,6 +421,24 @@ func TestUnsetRemovesADottedKeyInEveryShape(t *testing.T) { key: "sync.remote", survivors: map[string]string{"node_id": "mini"}, }, + { + // An alias resolves only against an anchor defined above it, so + // the key's line parsed on its own fails on `*r` even though the + // entry ends right there. The refusal must judge the line with the + // anchor in scope, or it refuses a value it can remove cleanly. + name: "alias to an anchor above", + seed: "sync:\n remote: &r file:///a\nbackup:\n remote: *r\n", + key: "backup.remote", + survivors: map[string]string{"sync.remote": "file:///a"}, + }, + { + // The same holds for an alias inside a one-line collection, which an + // exception for a value that is a bare alias would not cover. + name: "alias inside a one-line collection", + seed: "sync:\n remote: &r file:///a\nbackup:\n remotes: [*r, file:///b]\n", + key: "backup.remotes", + survivors: map[string]string{"sync.remote": "file:///a"}, + }, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { From 8e8e0b2fe848cff5995bdcb9c13e4200f6a75571 Mon Sep 17 00:00:00 2001 From: Eddie the Engineer Date: Mon, 19 Oct 2026 03:16:25 +0000 Subject: [PATCH 11/14] fix(config): leave a flow-style top level to its flow-style refusal {notes: x, node_id: mini} Before this branch, unsetting `notes` here was refused for the flow style. The flat-spelling span probe cuts the document after the key's line. When the top-level braces close on a later line, that cut ends inside them and fails to parse whatever the value is, so the probe called `notes: x` a value that spans lines and preempted the flow-style diagnosis. The probe holds only for an entry of a block mapping, so the flat spelling now runs it only under a block-style top level. The nested walk already returns at a flow-style parent before it reaches the probe. The new row in TestUnsetRefusesShapesItCannotEdit passes on the base, fails on the previous commit ("its value spans more than one line"), and passes here. A flat dotted key under a flow-style top level (`{sync.remote: x, ...}`) was a no-op on the base and still is. `go test ./internal/config/` passes, including the existing rows that expect "flow style", "block scalar" and "lines beneath it". AC: AC-11 Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 --- internal/config/yaml_config.go | 9 +++++++-- internal/config/yaml_nested_roundtrip_test.go | 4 ++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/internal/config/yaml_config.go b/internal/config/yaml_config.go index 43bca9efd..d96733c0c 100644 --- a/internal/config/yaml_config.go +++ b/internal/config/yaml_config.go @@ -1458,8 +1458,13 @@ func unsupportedUnsetShape(content, key string) error { if err := blockScalarUnsetRefusal(top.Content[idx+1], key); err != nil { return err } - if err := spansLinesUnsetRefusal(content, top.Content[idx], key); err != nil { - return err + // The probe holds only for an entry of a block mapping: under a + // flow-style top level the cut can end inside the mapping's own + // braces, which fails whatever the value is. + if top.Style&yaml.FlowStyle == 0 { + if err := spansLinesUnsetRefusal(content, top.Content[idx], key); err != nil { + return err + } } } } diff --git a/internal/config/yaml_nested_roundtrip_test.go b/internal/config/yaml_nested_roundtrip_test.go index 5dd6d7aa5..f81fc1ba8 100644 --- a/internal/config/yaml_nested_roundtrip_test.go +++ b/internal/config/yaml_nested_roundtrip_test.go @@ -504,6 +504,10 @@ func TestUnsetRefusesShapesItCannotEdit(t *testing.T) { }{ {name: "flow style section", seed: "sync: {remote: \"file:///origin.git\"}\n", key: "sync.remote", want: "flow style"}, {name: "flow style nested deeper", seed: "dolt:\n limits: {host: mini}\n", key: "dolt.limits.host", want: "flow style"}, + // A flow-style top level spread over lines is still named for its + // style. The key's own value ends on its line; only the mapping + // around it goes on. + {name: "flow style top level spread over lines", seed: "{notes: x,\n node_id: mini}\n", key: "notes", want: "flow style"}, {name: "literal block scalar", seed: "sync:\n remote: |\n line1\n line2\n", key: "sync.remote", want: "block scalar"}, {name: "folded block scalar", seed: "sync:\n remote: >\n line1\n", key: "sync.remote", want: "block scalar"}, // The flat spelling reaches the same body-left-behind outcome by the From bc1cd64c7825b35053c64c02a1a32ba84ee2cd8d Mon Sep 17 00:00:00 2001 From: Julian Knutsen Date: Tue, 20 Oct 2026 07:25:38 +0000 Subject: [PATCH 12/14] fix(cli): refuse graph workspaces marked by graph-preview-format (#7722) The graph-mode fence decides a workspace's format from graph_mode in its metadata alone. Graph-preview builds also write a .beads/graph-preview-format marker before metadata.json (donnabox fork commit deea8d57df, Donna Box), so that an interrupted or metadata-lost graph workspace stays fenced. bd ignored the marker. When a bd that predates graph_mode had rewritten metadata.json without the field, bd opened the graph database by its recorded name and `bd list` ran Issue schema initialization against it. When metadata.json was never written or was lost, bd fell back to the default database name and created an Issue database inside the graph workspace, and `bd init` initialized an Issue workspace there. checkWorkspaceFormat now also refuses a beadsDir that holds anything at graph-preview-format, whatever its content and whatever the metadata says (absent, stripped, or an explicit "dependency"). The graph_mode check still runs first, so a normal graph workspace is still reported by its graph_mode. The refusal is the same typed error with its own message and a hint to restore metadata.json, so every call site, the --json rendering and exit code 16 (ExitUnsupportedWorkspaceFormat) are unchanged. An empty beadsDir never probes the working directory. loadAndCheckWorkspaceFormat reads the metadata with LoadForDiscovery and refuses metadata that cannot be parsed beside the marker as the marker's format: the marker alone declares it. Command admission (guardLegacyUpgradeWorkspaceForInit) uses it, and checkDiscoveredWorkspaceFormat now delegates to it, so the paths that check before a migrating configfile.Load (the four store factories, create --repo's ensureBeadsDirForPath and dry-run target open, the bd dolt loaders, bootstrap's undiscovered walk) refuse a marked workspace too, with or without readable metadata. Other metadata load failures are still left to the caller's own load, so a malformed legacy config.json without the marker keeps create --repo's auto-initialization. bootstrap's ancestor walk (findParentConfig) refuses an ancestor that holds the marker without metadata it can read, as it refuses one whose metadata declares graph_mode. It used to skip a marker-only ancestor and take a more distant workspace's metadata or the defaults, and to report a marked ancestor with unparseable metadata as a load failure (exit 1). With the check in checkWorkspaceFormat, bootstrap in a .beads holding only the marker refuses it (exit 16) instead of reporting no workspace. validateConfiguredBackend returns early for nil metadata, so its backstop covers the marker only when metadata was loaded; every caller runs admission or checkDiscoveredWorkspaceFormat on the same beadsDir first, and its comment now says so. Tests: guard unit tests for marker-only, metadata lost, stripped (metadata.json and legacy config.json), server, explicit dependency, unparseable metadata, marker over a legacy Dolt root, and arbitrary marker content; the four store factories (cgo, and the CGO_ENABLED=0 copies goreleaser ships) with stripped, missing and, for cgo, unparseable metadata beside the marker; findParentConfig with a marked ancestor below an ordinary workspace; the exit-code mapping; ensureBeadsDirForPath with a malformed config.json with and without the marker; and the subprocess e2e matrices (in-workspace commands, plain `bd init`, and the outside entry points: create --repo, create --dry-run, --db, BEADS_DB, init --proxied-server, bootstrap below it) extended with four marker fixtures that must exit 16 with the marker refusal and leave .beads byte-identical, while store-less commands still run. The e2e subprocess timeout goes from 10s to 20s: refused runs finish in well under a second, but an unrefused one creates or opens a database and can approach 10s under load, so without the fence the rows fail on their assertion rather than on the timeout. Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen Co-authored-by: Claude Opus 5.5 (1M context) --- CHANGELOG.md | 15 ++ cmd/bd/backend_support.go | 6 +- cmd/bd/bootstrap.go | 6 + cmd/bd/create.go | 6 +- cmd/bd/graph_mode_admission_e2e_test.go | 161 ++++++++++++------ cmd/bd/legacy_upgrade_guard.go | 7 +- cmd/bd/store_factory_nocgo_test.go | 60 +++++-- cmd/bd/workspace_format_guard.go | 86 ++++++++-- cmd/bd/workspace_format_guard_test.go | 215 +++++++++++++++++++++++- 9 files changed, 469 insertions(+), 93 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a54cb677a..0a0ba3976 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -868,6 +868,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 A `metadata.json` rewrite keeps `graph_mode` and the `graph_*` fields graph-preview builds write, and like those builds it omits a zero `graph_schema_version` and a false `graph_ready`. +- **A graph workspace without `graph_mode` in its metadata is refused too.** + Graph-preview builds write a `.beads/graph-preview-format` marker before + `metadata.json`. bd ignored it. When an older bd had rewritten + `metadata.json` without `graph_mode`, bd opened the graph database by its + recorded name and ran Issue schema initialization against it; when + `metadata.json` was never written or was lost, bd fell back to the default + database name and created an Issue database inside the graph workspace, and + `bd init` initialized an Issue workspace there. A `.beads` holding that + marker, whatever it contains and whatever its metadata says (absent, + stripped, unparseable or an explicit `dependency`), is now refused wherever + a `graph_mode` workspace is: commands run in it or naming it with `--db` or + `BEADS_DB`, `bd create --repo` aimed at it, and `bd bootstrap` in a + workspace below it that would take its metadata exit with code 16 and a + hint to restore `metadata.json`, and routed and cross-repository opens fail + without opening its database. - **`bd setup claude --remove` no longer leaves a blank-line hole.** Removing the beads section from `CLAUDE.md` (or, with the other setup recipes, from `AGENTS.md` or `GEMINI.md`) left the blank lines around it behind, three in diff --git a/cmd/bd/backend_support.go b/cmd/bd/backend_support.go index 60d256cd1..a0fb89e99 100644 --- a/cmd/bd/backend_support.go +++ b/cmd/bd/backend_support.go @@ -17,7 +17,11 @@ import ( // open (checkWorkspaceFormat). Command admission refuses those first, and the // store factories check before their migrating configfile.Load // (checkDiscoveredWorkspaceFormat), so here it backs up any other caller that -// validates metadata it loaded itself. +// validates metadata it loaded itself. It returns early for a nil cfg, so that +// backstop covers the graph format marker only when metadata was loaded; a +// caller that may have none must check beadsDir with +// checkDiscoveredWorkspaceFormat or command admission first, as every caller +// does today. func validateConfiguredBackend(cfg *configfile.Config, beadsDir string) error { if cfg == nil { return nil diff --git a/cmd/bd/bootstrap.go b/cmd/bd/bootstrap.go index b7c28aded..8d07611cf 100644 --- a/cmd/bd/bootstrap.go +++ b/cmd/bd/bootstrap.go @@ -1594,6 +1594,9 @@ func isNonInteractiveBootstrap(flagValue bool) bool { // parent workspace does. Returns nil if no parent config is found. A malformed // or unreadable ancestor metadata file is authoritative and returned as an error; // bootstrap must not skip it and select a more distant workspace or defaults. +// An ancestor holding the graph format marker without metadata it can read is +// a graph workspace, refused like one whose metadata declares graph_mode, +// rather than skipped or reported as a metadata load failure. func findParentConfig(beadsDir string) (*configfile.Config, error) { // Start from the parent of beadsDir's enclosing directory. // beadsDir is typically "/.beads", so we start from 's parent. @@ -1611,6 +1614,9 @@ func findParentConfig(beadsDir string) (*configfile.Config, error) { for dir, ok := walk.Next(); ok; dir, ok = walk.Next() { candidate := filepath.Join(dir, ".beads") cfg, err := configfile.LoadForDiscovery(candidate) + if (err != nil || cfg == nil) && hasGraphFormatMarker(candidate) { + return nil, &unsupportedWorkspaceFormatError{beadsDir: candidate, formatMarker: true} + } if err != nil { return nil, fmt.Errorf("%s: %w", configfile.ConfigPath(candidate), err) } diff --git a/cmd/bd/create.go b/cmd/bd/create.go index c54ce7fb8..d3df37f1b 100644 --- a/cmd/bd/create.go +++ b/cmd/bd/create.go @@ -1085,9 +1085,9 @@ func ensureBeadsDirForPath(ctx context.Context, targetPath string, sourceStore s metadataPath := filepath.Join(beadsDir, "metadata.json") // A target in a format this build cannot open is refused before anything - // below: one marked only by a legacy config.json has no metadata.json, so - // it would otherwise be initialized here and lose its marker to the - // metadata.json written beside it. + // below: one marked only by a legacy config.json or by the graph format + // marker file has no metadata.json, so it would otherwise be initialized + // here, with an Issue database and a metadata.json written beside it. if err := checkDiscoveredWorkspaceFormat(beadsDir); err != nil { return err } diff --git a/cmd/bd/graph_mode_admission_e2e_test.go b/cmd/bd/graph_mode_admission_e2e_test.go index c0c91187c..6270c25c7 100644 --- a/cmd/bd/graph_mode_admission_e2e_test.go +++ b/cmd/bd/graph_mode_admission_e2e_test.go @@ -14,29 +14,43 @@ import ( "time" ) -// graphModeAdmissionFixture is a workspace whose metadata declares a -// graph_mode this build does not open. +// graphModeAdmissionFixture is a workspace whose metadata or format marker +// declares a format this build does not open. type graphModeAdmissionFixture struct { - name string + name string + // filename is the metadata file; "" lays down no metadata. filename string - mode string metadata string - // embedded adds what a real graph workspace holds beside its metadata: - // the embedded Dolt root of its graph database and the format sentinel - // graph-preview builds write. Without the guard, an ordinary bd opens - // that database as an Issue store and migrates it. + // embedded adds the embedded Dolt root of a graph database. Without the + // guard, an ordinary bd opens that database as an Issue store and + // migrates it. embedded bool + // marker adds the graph-preview-format sentinel graph-preview builds + // write before metadata.json. + marker bool + // refusal lists what the refusal must mention besides "this bd build + // does not support": the declared format and the start of its hint. + refusal []string } -// write lays the fixture down under beadsDir and returns the metadata path. +// write lays the fixture down under beadsDir and returns the metadata path, +// or "" when the fixture has no metadata. func (f graphModeAdmissionFixture) write(t *testing.T, beadsDir string) string { t.Helper() - path := filepath.Join(beadsDir, f.filename) - writeFile(t, path, []byte(f.metadata)) + if err := os.MkdirAll(beadsDir, 0o700); err != nil { + t.Fatal(err) + } + path := "" + if f.filename != "" { + path = filepath.Join(beadsDir, f.filename) + writeFile(t, path, []byte(f.metadata)) + } if f.embedded { if err := os.MkdirAll(filepath.Join(beadsDir, "embeddeddolt", "beads_graph_0123456789abcdef0123456789abcdef", ".dolt"), 0o700); err != nil { t.Fatal(err) } + } + if f.marker { writeFile(t, filepath.Join(beadsDir, "graph-preview-format"), []byte("link-preview-v9\n")) } if err := os.Chmod(beadsDir, 0o700); err != nil { @@ -46,19 +60,48 @@ func (f graphModeAdmissionFixture) write(t *testing.T, beadsDir string) string { } func graphModeAdmissionFixtures() []graphModeAdmissionFixture { + graphModeRefusal := func(mode string) []string { + return []string{"graph_mode", mode, "use a bd build that supports graph_mode"} + } var fixtures []graphModeAdmissionFixture fixtures = append(fixtures, graphModeAdmissionFixture{ - name: "metadata.json/link/embedded", filename: "metadata.json", mode: "link", - metadata: graphLinkWorkspaceMetadata, embedded: true, + name: "metadata.json/link/embedded", filename: "metadata.json", + metadata: graphLinkWorkspaceMetadata, embedded: true, marker: true, + refusal: graphModeRefusal("link"), }) for _, filename := range []string{"metadata.json", "config.json"} { for _, mode := range []string{"link", "future-format"} { fixtures = append(fixtures, graphModeAdmissionFixture{ - name: filename + "/" + mode + "/server", filename: filename, mode: mode, + name: filename + "/" + mode + "/server", filename: filename, metadata: `{"backend":"dolt","dolt_mode":"server","graph_mode":"` + mode + `"}`, + refusal: graphModeRefusal(mode), }) } } + // Only the format marker declares these: graph init stopped before + // metadata.json, metadata.json was lost, or a bd that predates + // graph_mode rewrote it without the field. + markerRefusal := []string{"graph-preview-format", "restore metadata.json"} + fixtures = append(fixtures, + graphModeAdmissionFixture{ + name: "marker/only", marker: true, + refusal: markerRefusal, + }, + graphModeAdmissionFixture{ + name: "marker/metadata-lost/embedded", embedded: true, marker: true, + refusal: markerRefusal, + }, + graphModeAdmissionFixture{ + name: "marker/metadata-stripped/embedded", filename: "metadata.json", + metadata: graphStrippedWorkspaceMetadata, embedded: true, marker: true, + refusal: markerRefusal, + }, + graphModeAdmissionFixture{ + name: "marker/dependency/server", filename: "metadata.json", + metadata: `{"backend":"dolt","dolt_mode":"server","graph_mode":"dependency"}`, marker: true, + refusal: markerRefusal, + }, + ) return fixtures } @@ -73,7 +116,11 @@ func runGraphModeAdmissionBD(t *testing.T, bd, repoDir string, args ...string) ( // added to bd's environment. func runGraphModeAdmissionBDEnv(t *testing.T, bd, dir string, env []string, args ...string) (string, int) { t.Helper() - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + // A refused run takes well under a second. An unrefused one (what these + // tests catch) goes on to create or open a database, which under load + // can approach 10s; the bound leaves room for it to fail on the + // assertion that names the regression rather than on the timeout. + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) defer cancel() cmd := exec.CommandContext(ctx, bd, args...) cmd.Dir = dir @@ -111,7 +158,7 @@ func requireGraphModeRefusal(t *testing.T, fixture graphModeAdmissionFixture, ar } // --json renders the refusal as a JSON error, which escapes the quotes // around the mode, so match its parts. - for _, want := range []string{"graph_mode", fixture.mode, "this bd build does not support", "use a bd build that supports graph_mode"} { + for _, want := range append([]string{"this bd build does not support"}, fixture.refusal...) { if !strings.Contains(output, want) { t.Fatalf("bd %s refusal does not mention %q:\n%s", strings.Join(args, " "), want, output) } @@ -131,13 +178,18 @@ func TestGraphModeCLIRefusesLegacyStoreWithoutWorkspaceEffects(t *testing.T) { {"dolt", "start"}, {"doctor", "--json"}, {"bootstrap", "--dry-run", "--yes"}, + {"init", "--quiet", "--non-interactive", "--skip-hooks", "--skip-agents"}, {"init", "--force", "--quiet", "--non-interactive", "--skip-hooks", "--skip-agents"}, // What the installed git hooks run; their shim passes exit 16 on. {"hooks", "run", "pre-commit"}, } for _, fixture := range graphModeAdmissionFixtures() { for _, args := range commands { - t.Run(fixture.name+"/"+args[0], func(t *testing.T) { + name := args[0] + if name == "init" && args[1] == "--force" { + name = "init-force" + } + t.Run(fixture.name+"/"+name, func(t *testing.T) { repoDir := t.TempDir() initGitRepoAt(t, repoDir) beadsDir := filepath.Join(repoDir, ".beads") @@ -149,8 +201,10 @@ func TestGraphModeCLIRefusesLegacyStoreWithoutWorkspaceEffects(t *testing.T) { if after := legacyUpgradeTreeDigest(t, beadsDir); after != before { t.Fatalf("refused bd %s changed the workspace\n%s", strings.Join(args, " "), output) } - if got, err := os.ReadFile(metadataPath); err != nil || string(got) != fixture.metadata { - t.Fatalf("refused bd %s rewrote %s: %q, %v", strings.Join(args, " "), fixture.filename, got, err) + if metadataPath != "" { + if got, err := os.ReadFile(metadataPath); err != nil || string(got) != fixture.metadata { + t.Fatalf("refused bd %s rewrote %s: %q, %v", strings.Join(args, " "), fixture.filename, got, err) + } } }) } @@ -160,7 +214,8 @@ func TestGraphModeCLIRefusesLegacyStoreWithoutWorkspaceEffects(t *testing.T) { // Commands also reach a workspace they do not run in: create --repo, from // another workspace or from none and as a dry run, --db and BEADS_DB, // init --proxied-server, and bootstrap in a workspace with no metadata of its -// own, which takes the nearest ancestor's. Each must refuse a graph workspace +// own, which takes the nearest ancestor's (and refuses a graph ancestor even +// when only its format marker declares it, rather than skip past it). Each must refuse a graph workspace // the same way, before resolving it migrates a legacy config.json or anything // opens its store. func TestGraphModeOutsideEntryPointsRefuseWithoutWorkspaceEffects(t *testing.T) { @@ -216,10 +271,12 @@ func TestGraphModeOutsideEntryPointsRefuseWithoutWorkspaceEffects(t *testing.T) output, code := runGraphModeAdmissionBDEnv(t, bd, dir, env, args...) requireGraphModeRefusal(t, fixture, args, output, code) if after := legacyUpgradeTreeDigest(t, beadsDir); after != before { - t.Fatalf("refused bd %s changed the workspace\n%s", strings.Join(args, " "), output) + t.Fatalf("bd %s changed the graph workspace\n%s", strings.Join(args, " "), output) } - if got, err := os.ReadFile(metadataPath); err != nil || string(got) != fixture.metadata { - t.Fatalf("refused bd %s rewrote %s: %q, %v", strings.Join(args, " "), fixture.filename, got, err) + if metadataPath != "" { + if got, err := os.ReadFile(metadataPath); err != nil || string(got) != fixture.metadata { + t.Fatalf("refused bd %s rewrote %s: %q, %v", strings.Join(args, " "), fixture.filename, got, err) + } } }) } @@ -230,30 +287,40 @@ func TestGraphModeOutsideEntryPointsRefuseWithoutWorkspaceEffects(t *testing.T) // refuses, and leave it untouched. func TestGraphModeWorkspaceAdmitsStorelessCommands(t *testing.T) { bd := buildBDUnderTest(t) - fixture := graphModeAdmissionFixtures()[0] - for _, args := range [][]string{ - {"version"}, - {"--version"}, - {"help"}, - {"completion", "bash"}, - } { - t.Run(strings.Join(args, " "), func(t *testing.T) { - repoDir := t.TempDir() - initGitRepoAt(t, repoDir) - beadsDir := filepath.Join(repoDir, ".beads") - fixture.write(t, beadsDir) - before := legacyUpgradeTreeDigest(t, beadsDir) + var fixtures []graphModeAdmissionFixture + for _, fixture := range graphModeAdmissionFixtures() { + if fixture.name == "metadata.json/link/embedded" || fixture.name == "marker/only" { + fixtures = append(fixtures, fixture) + } + } + if len(fixtures) != 2 { + t.Fatalf("store-less fixtures = %d, want 2", len(fixtures)) + } + for _, fixture := range fixtures { + for _, args := range [][]string{ + {"version"}, + {"--version"}, + {"help"}, + {"completion", "bash"}, + } { + t.Run(fixture.name+"/"+strings.Join(args, " "), func(t *testing.T) { + repoDir := t.TempDir() + initGitRepoAt(t, repoDir) + beadsDir := filepath.Join(repoDir, ".beads") + fixture.write(t, beadsDir) + before := legacyUpgradeTreeDigest(t, beadsDir) - output, code := runGraphModeAdmissionBD(t, bd, repoDir, args...) - if code != 0 { - t.Fatalf("bd %s exit = %d, want 0\n%s", strings.Join(args, " "), code, output) - } - if strings.Contains(output, "graph_mode") { - t.Fatalf("store-less bd %s reported the workspace format:\n%s", strings.Join(args, " "), output) - } - if after := legacyUpgradeTreeDigest(t, beadsDir); after != before { - t.Fatalf("bd %s changed the workspace", strings.Join(args, " ")) - } - }) + output, code := runGraphModeAdmissionBD(t, bd, repoDir, args...) + if code != 0 { + t.Fatalf("bd %s exit = %d, want 0\n%s", strings.Join(args, " "), code, output) + } + if strings.Contains(output, "this bd build does not support") { + t.Fatalf("store-less bd %s reported the workspace format:\n%s", strings.Join(args, " "), output) + } + if after := legacyUpgradeTreeDigest(t, beadsDir); after != before { + t.Fatalf("bd %s changed the workspace", strings.Join(args, " ")) + } + }) + } } } diff --git a/cmd/bd/legacy_upgrade_guard.go b/cmd/bd/legacy_upgrade_guard.go index bbaf45f08..db1570cb0 100644 --- a/cmd/bd/legacy_upgrade_guard.go +++ b/cmd/bd/legacy_upgrade_guard.go @@ -42,14 +42,11 @@ func guardLegacyUpgradeWorkspaceForInit(beadsDir string, initSelectsServer bool) if beadsDir == "" { return nil } - cfg, err := configfile.LoadForDiscovery(beadsDir) - if err != nil { - return fmt.Errorf("loading config: %w", err) - } // This guard admits only the ordinary Issue store. A workspace in another // format is refused before any legacy classification, which could // otherwise admit it, or report it as a legacy store to migrate. - if err := checkWorkspaceFormat(cfg, beadsDir); err != nil { + cfg, err := loadAndCheckWorkspaceFormat(beadsDir) + if err != nil { return err } if isHistoricalSQLiteWorkspace(beadsDir, cfg) { diff --git a/cmd/bd/store_factory_nocgo_test.go b/cmd/bd/store_factory_nocgo_test.go index acf430c12..52dc49e89 100644 --- a/cmd/bd/store_factory_nocgo_test.go +++ b/cmd/bd/store_factory_nocgo_test.go @@ -205,25 +205,54 @@ func TestNocgoFactoriesHonorSharedServer(t *testing.T) { // .goreleaser.yml ships open routed, cross-repository and create --repo // targets through their own copies of these factories, so each must refuse a // graph workspace before its configfile.Load, which would otherwise migrate -// one marked only by a legacy config.json into metadata.json first. It runs in -// CI only because bazel.yml's PURE_CMD_BD_TESTS allowlists it by name. +// one marked only by a legacy config.json into metadata.json first, and one +// declared only by the graph-preview-format marker, whose metadata is lost or +// stripped. It runs in CI only because bazel.yml's PURE_CMD_BD_TESTS +// allowlists it by name. func TestNocgoStoreFactoriesRefuseGraphWorkspace(t *testing.T) { isolateSharedServerSignals(t) pinLegacyUpgradeGuardEnv(t) - const metadata = `{"backend":"dolt","graph_mode":"link"}` + const graphMetadata = `{"backend":"dolt","graph_mode":"link"}` + // What a bd that predates graph_mode leaves of a graph workspace's + // metadata: the database name still selects the graph database. + const strippedMetadata = `{"backend":"dolt","dolt_mode":"server","dolt_database":"beads_graph_0123456789abcdef0123456789abcdef"}` factories := map[string]func(context.Context, string) (storage.DoltStorage, error){ "read-write": newDoltStoreFromConfig, "routed": newExistingDoltStoreFromConfig, "read-only": newReadOnlyStoreFromConfig, "preview": newPreviewStoreFromConfig, } - for _, filename := range []string{"metadata.json", "config.json"} { + layouts := []struct { + name string + // filename is the metadata file; "" lays down no metadata. + filename string + metadata string + // marker adds the graph-preview-format sentinel. + marker bool + }{ + {name: "metadata.json", filename: "metadata.json", metadata: graphMetadata}, + {name: "config.json", filename: "config.json", metadata: graphMetadata}, + {name: "marker only", marker: true}, + {name: "marker, metadata stripped", filename: "metadata.json", metadata: strippedMetadata, marker: true}, + } + for _, layout := range layouts { for name, open := range factories { - t.Run(filename+"/"+name, func(t *testing.T) { + t.Run(layout.name+"/"+name, func(t *testing.T) { beadsDir := t.TempDir() - path := filepath.Join(beadsDir, filename) - if err := os.WriteFile(path, []byte(metadata), 0o600); err != nil { - t.Fatal(err) + want := map[string]bool{} + path := "" + if layout.filename != "" { + path = filepath.Join(beadsDir, layout.filename) + if err := os.WriteFile(path, []byte(layout.metadata), 0o600); err != nil { + t.Fatal(err) + } + want[layout.filename] = true + } + if layout.marker { + if err := os.WriteFile(filepath.Join(beadsDir, "graph-preview-format"), []byte("link-preview-v9\n"), 0o600); err != nil { + t.Fatal(err) + } + want["graph-preview-format"] = true } store, err := open(t.Context(), beadsDir) if err == nil { @@ -232,12 +261,19 @@ func TestNocgoStoreFactoriesRefuseGraphWorkspace(t *testing.T) { if !isUnsupportedWorkspaceFormat(err) { t.Fatalf("%s open = %v, want unsupported workspace format refusal", name, err) } - if got, readErr := os.ReadFile(path); readErr != nil || string(got) != metadata { - t.Fatalf("%s open rewrote %s: %q, %v", name, filename, got, readErr) + if path != "" { + if got, readErr := os.ReadFile(path); readErr != nil || string(got) != layout.metadata { + t.Fatalf("%s open rewrote %s: %q, %v", name, layout.filename, got, readErr) + } } entries, readErr := os.ReadDir(beadsDir) - if readErr != nil || len(entries) != 1 { - t.Fatalf("%s open left %d entries in the workspace, want only %s (%v)", name, len(entries), filename, readErr) + if readErr != nil || len(entries) != len(want) { + t.Fatalf("%s open left %d entries in the workspace, want %d (%v)", name, len(entries), len(want), readErr) + } + for _, entry := range entries { + if !want[entry.Name()] { + t.Fatalf("%s open created %s in the workspace", name, entry.Name()) + } } }) } diff --git a/cmd/bd/workspace_format_guard.go b/cmd/bd/workspace_format_guard.go index 4e9daa0a4..1ae6c3995 100644 --- a/cmd/bd/workspace_format_guard.go +++ b/cmd/bd/workspace_format_guard.go @@ -3,13 +3,16 @@ package main import ( "errors" "fmt" + "os" + "path/filepath" "github.com/steveyegge/beads/internal/configfile" ) // ExitUnsupportedWorkspaceFormat is the exit code for a command refused // because its workspace declares a format this bd build cannot open: a -// metadata.json graph_mode other than "dependency". Stable so a wrapper can +// metadata.json graph_mode other than "dependency", or a graph workspace +// format marker (graphFormatMarkerFile). Stable so a wrapper can // tell "this workspace needs a different bd build" apart from a generic // failure (1) without grepping stderr. // @@ -18,35 +21,86 @@ import ( // §E3, which freezes 10/11/12/130. const ExitUnsupportedWorkspaceFormat = 16 -// unsupportedWorkspaceFormatError refuses a workspace whose metadata declares -// a data model other than the Issue store this build opens. Graph-preview -// builds persist graph_mode "link" plus graph_* binding fields beside an -// ordinary Dolt backend, so nothing else in the metadata tells this build -// that the database behind it is not an Issue store; opening it as one would -// run Issue migrations into it. +// graphFormatMarkerFile is the format sentinel graph-preview builds write in +// .beads before metadata.json, so that a graph workspace whose metadata was +// never written, was lost, or was rewritten without graph_mode by a bd that +// does not know the field still declares its format. +const graphFormatMarkerFile = "graph-preview-format" + +// unsupportedWorkspaceFormatError refuses a workspace whose metadata or format +// marker declares a data model other than the Issue store this build opens. +// Graph-preview builds persist graph_mode "link" plus graph_* binding fields +// beside an ordinary Dolt backend, and the graphFormatMarkerFile beside them, +// so nothing else tells this build that the database behind it is not an +// Issue store; opening it as one would run Issue migrations into it. type unsupportedWorkspaceFormatError struct { beadsDir string graphMode string + // formatMarker reports that graphFormatMarkerFile, not graph_mode, + // declared the format. + formatMarker bool } func (e *unsupportedWorkspaceFormatError) Error() string { + if e.formatMarker { + return fmt.Sprintf("workspace %s has a %s marker, which this bd build does not support; %s", + e.beadsDir, graphFormatMarkerFile, configfile.BackendNotOpenedGuarantee) + } return fmt.Sprintf("workspace %s uses format graph_mode %q, which this bd build does not support; %s", e.beadsDir, e.graphMode, configfile.BackendNotOpenedGuarantee) } func (e *unsupportedWorkspaceFormatError) hint() string { + if e.formatMarker { + return fmt.Sprintf("restore metadata.json (graph_mode and the graph_* fields) from version control or a backup, then use a bd build that supports graph workspaces; do not remove .beads/%s", graphFormatMarkerFile) + } return fmt.Sprintf("use a bd build that supports graph_mode %q workspaces; leave graph_mode and the graph_* fields in metadata.json unchanged", e.graphMode) } // checkWorkspaceFormat refuses metadata whose graph_mode marker names any // model but the default. An unknown marker is refused too: it may describe a // format newer than this build, so it must never fall back to the Issue store. -// cfg may be nil (no metadata), which is an ordinary workspace. +// It also refuses a beadsDir holding the graph format marker file, whatever +// the metadata says and whatever the file holds. cfg may be nil (no metadata); +// beadsDir may be "" (no workspace directory, so no marker to find). func checkWorkspaceFormat(cfg *configfile.Config, beadsDir string) error { - if mode, err := cfg.GetGraphMode(); err == nil && mode == configfile.GraphModeDependency { - return nil + if mode, err := cfg.GetGraphMode(); err != nil || mode != configfile.GraphModeDependency { + return &unsupportedWorkspaceFormatError{beadsDir: beadsDir, graphMode: cfg.GraphMode} } - return &unsupportedWorkspaceFormatError{beadsDir: beadsDir, graphMode: cfg.GraphMode} + if hasGraphFormatMarker(beadsDir) { + return &unsupportedWorkspaceFormatError{beadsDir: beadsDir, formatMarker: true} + } + return nil +} + +// loadAndCheckWorkspaceFormat reads beadsDir's discovery metadata without +// migrating it and refuses a workspace in a format this build cannot open +// (checkWorkspaceFormat). cfg is nil when beadsDir has no metadata. Metadata +// that cannot be read beside the graph format marker is refused as that +// format too: the marker alone declares it. +func loadAndCheckWorkspaceFormat(beadsDir string) (*configfile.Config, error) { + cfg, err := configfile.LoadForDiscovery(beadsDir) + if err != nil { + if hasGraphFormatMarker(beadsDir) { + return nil, &unsupportedWorkspaceFormatError{beadsDir: beadsDir, formatMarker: true} + } + return nil, fmt.Errorf("loading config: %w", err) + } + if err := checkWorkspaceFormat(cfg, beadsDir); err != nil { + return nil, err + } + return cfg, nil +} + +// hasGraphFormatMarker reports whether anything exists at beadsDir's graph +// format marker path. Callers read beadsDir's metadata first, so a directory +// bd cannot search has already failed there. +func hasGraphFormatMarker(beadsDir string) bool { + if beadsDir == "" { + return false + } + _, err := os.Lstat(filepath.Join(beadsDir, graphFormatMarkerFile)) + return err == nil } // checkDiscoveredWorkspaceFormat runs checkWorkspaceFormat on beadsDir's @@ -54,13 +108,13 @@ func checkWorkspaceFormat(cfg *configfile.Config, beadsDir string) error { // A path that opens or initializes a workspace without passing through command // admission runs it first: configfile.Load would otherwise migrate a legacy // config.json into metadata.json before the format check could refuse it. -// Metadata that does not load is left to the caller's own load to report. +// Metadata that does not load is left to the caller's own load to report, +// unless the graph format marker sits beside it (loadAndCheckWorkspaceFormat). func checkDiscoveredWorkspaceFormat(beadsDir string) error { - cfg, err := configfile.LoadForDiscovery(beadsDir) - if err != nil { - return nil + if _, err := loadAndCheckWorkspaceFormat(beadsDir); isUnsupportedWorkspaceFormat(err) { + return err } - return checkWorkspaceFormat(cfg, beadsDir) + return nil } func isUnsupportedWorkspaceFormat(err error) bool { diff --git a/cmd/bd/workspace_format_guard_test.go b/cmd/bd/workspace_format_guard_test.go index a0609ed82..d66f03f29 100644 --- a/cmd/bd/workspace_format_guard_test.go +++ b/cmd/bd/workspace_format_guard_test.go @@ -6,6 +6,7 @@ import ( "context" "os" "path/filepath" + "strconv" "strings" "testing" @@ -29,11 +30,26 @@ const graphLinkWorkspaceMetadata = `{ "project_id": "0123456789abcdef0123456789abcdef" }` +// graphStrippedWorkspaceMetadata is graphLinkWorkspaceMetadata as a bd that +// predates graph_mode leaves it after any Save: the graph_* keys are gone, and +// the database name still selects the graph database. +const graphStrippedWorkspaceMetadata = `{ + "database": "", + "backend": "dolt", + "dolt_mode": "embedded", + "dolt_database": "beads_graph_0123456789abcdef0123456789abcdef", + "project_id": "0123456789abcdef0123456789abcdef" +}` + // graphWorkspaceLayout is a .beads directory whose metadata declares a // graph_mode, plus whatever else the case lays down beside it. type graphWorkspaceLayout struct { + // filename is the metadata file; "" lays down no metadata. filename string metadata string + // marker adds the graph-preview-format sentinel graph-preview builds + // write before metadata.json. + marker bool // embedded adds the embedded Dolt root a graph database lives in. embedded bool // doltRoot adds a .beads/dolt root without a version witness, which @@ -44,11 +60,18 @@ type graphWorkspaceLayout struct { sqlite bool } -// write lays the layout down under beadsDir and returns the metadata path. +// write lays the layout down under beadsDir and returns the metadata path, or +// "" when the layout has no metadata. func (l graphWorkspaceLayout) write(t *testing.T, beadsDir string) string { t.Helper() - path := filepath.Join(beadsDir, l.filename) - writeFile(t, path, []byte(l.metadata)) + path := "" + if l.filename != "" { + path = filepath.Join(beadsDir, l.filename) + writeFile(t, path, []byte(l.metadata)) + } + if l.marker { + writeFile(t, filepath.Join(beadsDir, "graph-preview-format"), []byte("link-preview-v9\n")) + } if l.embedded { if err := os.MkdirAll(filepath.Join(beadsDir, "embeddeddolt", "beads_graph_0123456789abcdef0123456789abcdef", ".dolt"), 0o700); err != nil { t.Fatal(err) @@ -116,6 +139,114 @@ func TestWorkspaceFormatGuardRefusesGraphWorkspaces(t *testing.T) { } } +// A graph workspace whose metadata does not declare graph_mode is still +// refused by its format marker: graph-preview builds write the marker before +// metadata.json, and a bd that predates graph_mode drops the field on Save. +func TestWorkspaceFormatGuardRefusesFormatMarker(t *testing.T) { + pinLegacyUpgradeGuardEnv(t) + tests := []struct { + name string + layout graphWorkspaceLayout + }{ + // Graph init interrupted after the marker, before metadata.json. + {name: "marker only", layout: graphWorkspaceLayout{marker: true}}, + {name: "metadata lost", layout: graphWorkspaceLayout{marker: true, embedded: true}}, + {name: "metadata stripped", layout: graphWorkspaceLayout{filename: "metadata.json", metadata: graphStrippedWorkspaceMetadata, marker: true, embedded: true}}, + {name: "legacy config.json stripped", layout: graphWorkspaceLayout{filename: "config.json", metadata: graphStrippedWorkspaceMetadata, marker: true, embedded: true}}, + {name: "server metadata stripped", layout: graphWorkspaceLayout{filename: "metadata.json", metadata: `{"backend":"dolt","dolt_mode":"server"}`, marker: true}}, + {name: "explicit dependency", layout: graphWorkspaceLayout{filename: "metadata.json", metadata: `{"backend":"dolt","graph_mode":"dependency"}`, marker: true, embedded: true}}, + // Metadata that does not parse (a conflicted merge, a truncated + // write) cannot outrank the marker. + {name: "unparseable metadata", layout: graphWorkspaceLayout{filename: "metadata.json", metadata: `{"backend":"dolt",`, marker: true, embedded: true}}, + // Without the format check first, this is reported as a legacy + // Dolt workspace to migrate. + {name: "marker over legacy dolt root", layout: graphWorkspaceLayout{marker: true, doltRoot: true}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + beadsDir := t.TempDir() + path := tt.layout.write(t, beadsDir) + before := legacyUpgradeTreeDigest(t, beadsDir) + + err := guardLegacyUpgradeWorkspace(beadsDir) + if !isUnsupportedWorkspaceFormat(err) { + t.Fatalf("guardLegacyUpgradeWorkspace() = %v, want unsupported workspace format refusal", err) + } + for _, want := range []string{beadsDir, "graph-preview-format", "this bd build does not support", "no storage database was opened or modified"} { + if !strings.Contains(err.Error(), want) { + t.Fatalf("refusal %q does not mention %q", err, want) + } + } + if after := legacyUpgradeTreeDigest(t, beadsDir); after != before { + t.Fatalf("guard changed the workspace") + } + if path != "" { + if got, readErr := os.ReadFile(path); readErr != nil || string(got) != tt.layout.metadata { + t.Fatalf("guard rewrote %s: %q, %v", tt.layout.filename, got, readErr) + } + } + }) + } + // The marker's presence is the signal, not its content. + for _, content := range []string{"", "link-preview-v5\n", "not a generation"} { + t.Run("content "+strconv.Quote(content), func(t *testing.T) { + beadsDir := t.TempDir() + writeFile(t, filepath.Join(beadsDir, "graph-preview-format"), []byte(content)) + if err := guardLegacyUpgradeWorkspace(beadsDir); !isUnsupportedWorkspaceFormat(err) { + t.Fatalf("guardLegacyUpgradeWorkspace() = %v, want unsupported workspace format refusal", err) + } + }) + } +} + +// With no workspace directory there is no marker to look for: an empty +// beadsDir must not probe the working directory. +func TestWorkspaceFormatCheckWithoutWorkspaceDirectory(t *testing.T) { + t.Chdir(t.TempDir()) + writeFile(t, "graph-preview-format", []byte("link-preview-v9\n")) + if err := checkWorkspaceFormat(nil, ""); err != nil { + t.Fatalf("checkWorkspaceFormat(nil, \"\") = %v, want nil", err) + } + if err := validateConfiguredBackend(&configfile.Config{Backend: configfile.BackendDolt}, ""); err != nil { + t.Fatalf("validateConfiguredBackend(dolt, \"\") = %v, want nil", err) + } +} + +// Bootstrap in a workspace with no metadata of its own takes its nearest +// ancestor's. An ancestor declared a graph workspace only by its format marker +// is refused like one declaring graph_mode: neither skipped for a more distant +// workspace or the defaults, nor reported as a metadata load failure. +func TestFindParentConfigRefusesMarkedAncestor(t *testing.T) { + pinLegacyUpgradeGuardEnv(t) + for name, layout := range map[string]graphWorkspaceLayout{ + "marker only": {marker: true}, + "marker, metadata unparseable": {filename: "metadata.json", metadata: `{"backend":`, marker: true, embedded: true}, + "marker, metadata stripped": {filename: "metadata.json", metadata: graphStrippedWorkspaceMetadata, marker: true, embedded: true}, + } { + t.Run(name, func(t *testing.T) { + outer := t.TempDir() + // An ordinary workspace further up must not be selected instead. + writeFile(t, filepath.Join(outer, ".beads", "metadata.json"), []byte(`{"backend":"dolt","dolt_database":"outer"}`)) + graphDir := filepath.Join(outer, "graph") + graphBeadsDir := filepath.Join(graphDir, ".beads") + layout.write(t, graphBeadsDir) + before := legacyUpgradeTreeDigest(t, graphBeadsDir) + t.Setenv("HOME", outer) + + cfg, err := findParentConfig(filepath.Join(graphDir, "inner", ".beads")) + if !isUnsupportedWorkspaceFormat(err) { + t.Fatalf("findParentConfig() = %+v, %v, want unsupported workspace format refusal", cfg, err) + } + if !strings.Contains(err.Error(), "graph-preview-format") { + t.Fatalf("refusal %q does not name the marker", err) + } + if after := legacyUpgradeTreeDigest(t, graphBeadsDir); after != before { + t.Fatal("findParentConfig changed the graph workspace") + } + }) + } +} + func TestWorkspaceFormatGuardAdmitsDependencyWorkspaces(t *testing.T) { pinLegacyUpgradeGuardEnv(t) for _, metadata := range []string{ @@ -140,7 +271,9 @@ func TestWorkspaceFormatGuardAdmitsDependencyWorkspaces(t *testing.T) { // workspace's store without passing through command admission, so the store // factories must refuse a graph workspace themselves, before any open. That // includes one marked only by a legacy config.json, which configfile.Load -// would migrate into metadata.json before a check of the loaded metadata ran. +// would migrate into metadata.json before a check of the loaded metadata ran, +// and one declared only by the graph format marker, whose metadata is +// stripped, lost or unparseable. func TestStoreFactoriesRefuseGraphWorkspace(t *testing.T) { pinLegacyUpgradeGuardEnv(t) factories := map[string]func(context.Context, string) (storage.DoltStorage, error){ @@ -149,11 +282,23 @@ func TestStoreFactoriesRefuseGraphWorkspace(t *testing.T) { "read-only": newReadOnlyStoreFromConfig, "preview": newPreviewStoreFromConfig, } - for _, filename := range []string{"metadata.json", "config.json"} { + layouts := map[string]graphWorkspaceLayout{ + "metadata.json graph_mode link": {filename: "metadata.json", metadata: graphLinkWorkspaceMetadata, embedded: true}, + "config.json graph_mode link": {filename: "config.json", metadata: graphLinkWorkspaceMetadata, embedded: true}, + "marker, metadata stripped": {filename: "metadata.json", metadata: graphStrippedWorkspaceMetadata, marker: true, embedded: true}, + // No metadata at all: the factories must not fall back to the + // default database before looking for the marker. + "marker, metadata lost": {marker: true, embedded: true}, + // Metadata that does not parse beside the marker: the marker alone + // declares the format, so this is refused as that, not reported as + // a metadata load failure. + "marker, metadata unparseable": {filename: "metadata.json", metadata: `{"backend":`, marker: true, embedded: true}, + } + for layoutName, layout := range layouts { for name, open := range factories { - t.Run(filename+"/"+name, func(t *testing.T) { + t.Run(layoutName+"/"+name, func(t *testing.T) { beadsDir := t.TempDir() - path := graphWorkspaceLayout{filename: filename, metadata: graphLinkWorkspaceMetadata, embedded: true}.write(t, beadsDir) + path := layout.write(t, beadsDir) before := legacyUpgradeTreeDigest(t, beadsDir) store, err := open(t.Context(), beadsDir) if err == nil { @@ -165,8 +310,10 @@ func TestStoreFactoriesRefuseGraphWorkspace(t *testing.T) { if after := legacyUpgradeTreeDigest(t, beadsDir); after != before { t.Fatalf("%s open changed the workspace", name) } - if got, readErr := os.ReadFile(path); readErr != nil || string(got) != graphLinkWorkspaceMetadata { - t.Fatalf("%s open rewrote %s: %q, %v", name, filename, got, readErr) + if path != "" { + if got, readErr := os.ReadFile(path); readErr != nil || string(got) != layout.metadata { + t.Fatalf("%s open rewrote %s: %q, %v", name, layout.filename, got, readErr) + } } }) } @@ -233,12 +380,19 @@ func TestHandleWorkspaceGuardErrorExitCodes(t *testing.T) { t.Fatalf("handleWorkspaceGuardError(nil) = %v", err) } formatErr := &unsupportedWorkspaceFormatError{beadsDir: "/w/.beads", graphMode: "link"} + markerDir := t.TempDir() + writeFile(t, filepath.Join(markerDir, "graph-preview-format"), []byte("link-preview-v9\n")) + markerErr := checkWorkspaceFormat(nil, markerDir) + if markerErr == nil { + t.Fatalf("checkWorkspaceFormat(no metadata, format marker) = nil, want a refusal") + } tests := []struct { name string err error want int }{ {name: "unsupported format", err: formatErr, want: ExitUnsupportedWorkspaceFormat}, + {name: "format marker", err: markerErr, want: ExitUnsupportedWorkspaceFormat}, {name: "legacy refusal", err: legacyUpgradeRefusal("legacy Dolt workspace"), want: 1}, } for _, tt := range tests { @@ -257,3 +411,46 @@ func TestHandleWorkspaceGuardErrorExitCodes(t *testing.T) { t.Fatalf("ExitUnsupportedWorkspaceFormat = %d; it is a stable scripting contract", ExitUnsupportedWorkspaceFormat) } } + +// A create target without metadata.json is refused when it declares a graph +// format, and is otherwise left to the auto-initialization path as before, +// even when its legacy config.json cannot be parsed. +func TestEnsureBeadsDirForPathChecksWorkspaceFormat(t *testing.T) { + malformedConfig := `{"backend":"dolt",` + for _, tc := range []struct { + name string + marker bool + wantFormat bool + }{ + {name: "malformed config.json", marker: false, wantFormat: false}, + {name: "malformed config.json beside the marker", marker: true, wantFormat: true}, + } { + t.Run(tc.name, func(t *testing.T) { + targetDir := t.TempDir() + beadsDir := filepath.Join(targetDir, ".beads") + if err := os.MkdirAll(beadsDir, 0o750); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(beadsDir, "config.json"), []byte(malformedConfig), 0o600); err != nil { + t.Fatal(err) + } + if tc.marker { + if err := os.WriteFile(filepath.Join(beadsDir, "graph-preview-format"), []byte("1\n"), 0o600); err != nil { + t.Fatal(err) + } + } + // allowCreate=false stops just after the format check, before + // anything is created, so the result shows which branch ran. + err := ensureBeadsDirForPath(context.Background(), targetDir, nil, false) + if err == nil { + t.Fatal("ensureBeadsDirForPath() = nil, want an error") + } + if got := isUnsupportedWorkspaceFormat(err); got != tc.wantFormat { + t.Fatalf("ensureBeadsDirForPath() = %v, unsupported format = %v, want %v", err, got, tc.wantFormat) + } + if !tc.wantFormat && !strings.Contains(err.Error(), "no beads workspace found") { + t.Fatalf("ensureBeadsDirForPath() = %v, want the not-auto-created refusal", err) + } + }) + } +} From f3c47390e7885c6cbfc0fd043b4a7a90d002755e Mon Sep 17 00:00:00 2001 From: Julian Knutsen Date: Tue, 20 Oct 2026 09:29:54 +0000 Subject: [PATCH 13/14] fix(storage): refuse graph_mode workspaces on library opens (#7721) #7671 fences graph workspaces in cmd/bd: command admission, the store factories and the other CLI paths refuse one, and #7722 extends that fence to a .beads holding the graph-preview-format marker whatever its metadata says. But the public library opens (beads.OpenBestAvailable, OpenBestAvailableWith, OpenFromConfig, OpenGated) and the Dolt store's metadata-driven constructors (NewFromConfig, NewFromConfigWithOptions, NewFromConfigWithCLIOptions) still opened either kind of graph workspace as an Issue store and ran Issue schema migrations into the graph database (or, with no metadata beside the marker, created an Issue database inside the graph workspace). - Move the typed refusal into internal/configfile (UnsupportedWorkspaceFormatError with BeadsDir, GraphMode and FormatMarker; CheckWorkspaceFormat; GraphFormatMarkerFile; HasGraphFormatMarker) with byte-identical text and hints for both the graph_mode and the marker refusal, and move cmd/bd's discovery pre-checks there as LoadAndCheckWorkspaceFormat and CheckDiscoveredWorkspaceFormat (LoadForDiscovery, which never migrates a legacy config.json; unreadable metadata beside the marker is refused as the marker's format). cmd/bd's checkWorkspaceFormat, loadAndCheckWorkspaceFormat, hasGraphFormatMarker and checkDiscoveredWorkspaceFormat become thin delegates, so the CLI keeps its behaviour (exit 16 with the same hints) and every caller of them is unchanged; bootstrap's ancestor walk builds the configfile type. - Every public metadata-driven open runs CheckDiscoveredWorkspaceFormat as its first statement: before OpenGated takes its gates and before OpenBestAvailableWith's Load can migrate config.json. A refused open leaves the workspace byte-identical. The type is exported as beads.UnsupportedWorkspaceFormatError. - The Dolt constructors run CheckDiscoveredWorkspaceFormat before configfile.Load, so a graph workspace marked only by a legacy config.json, or by the marker beside stripped, missing or unparseable metadata, is refused before Load can migrate it into metadata.json (and drop graph_* keys this build does not know). requireDoltBackend re-checks the loaded config as a backstop, so they also refuse before resolving a server or writing a port file or Dolt root. Tests: configfile unit tests for both refusals (exact marker text and hint, graph_mode winning over the marker, unparseable metadata with and without the marker); the public opens and the three Dolt constructors against graph_mode workspaces and against marker-only, stripped (metadata.json and legacy config.json), server, explicit dependency and unparseable-metadata marker workspaces, each refused with the typed error and the workspace left unchanged. Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen Co-authored-by: Claude Opus 5.5 (1M context) --- CHANGELOG.md | 9 + beads.go | 20 ++ beads_cgo.go | 5 + beads_nocgo.go | 5 + cmd/bd/bootstrap.go | 2 +- cmd/bd/workspace_format_guard.go | 100 ++-------- cmd/bd/workspace_format_guard_test.go | 2 +- internal/configfile/BUILD.bazel | 2 + internal/configfile/workspace_format.go | 118 ++++++++++++ internal/configfile/workspace_format_test.go | 160 ++++++++++++++++ internal/storage/dolt/open.go | 18 ++ internal/storage/dolt/open_test.go | 133 +++++++++++++ open_backend_selection_test.go | 191 +++++++++++++++++++ 13 files changed, 682 insertions(+), 83 deletions(-) create mode 100644 internal/configfile/workspace_format.go create mode 100644 internal/configfile/workspace_format_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a0ba3976..884a0d7f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -883,6 +883,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 workspace below it that would take its metadata exit with code 16 and a hint to restore `metadata.json`, and routed and cross-repository opens fail without opening its database. +- **The Go library's opens refuse a graph workspace too.** + `beads.OpenBestAvailable`, `OpenBestAvailableWith`, `OpenFromConfig` and + `OpenGated` opened a workspace whose `metadata.json` declares a + `graph_mode` other than `dependency`, or whose `.beads` holds the + `graph-preview-format` marker, as an Issue store and ran Issue schema + migrations into the graph database. They now + return `*beads.UnsupportedWorkspaceFormatError` (match it with `errors.As`) + before opening, creating or changing anything, the same refusal the CLI + exits 16 with. - **`bd setup claude --remove` no longer leaves a blank-line hole.** Removing the beads section from `CLAUDE.md` (or, with the other setup recipes, from `AGENTS.md` or `GEMINI.md`) left the blank lines around it behind, three in diff --git a/beads.go b/beads.go index 3b7d03fde..235415c84 100644 --- a/beads.go +++ b/beads.go @@ -99,6 +99,18 @@ func configuredBackendUnavailable(backend, beadsDir string, cfg *configfile.Conf } } +// UnsupportedWorkspaceFormatError is returned by OpenBestAvailable, +// OpenBestAvailableWith, OpenFromConfig and OpenGated when the workspace +// declares a data model this build cannot open: a metadata.json graph_mode +// other than "dependency", such as the "link" workspaces graph-preview builds +// of bd write, or the .beads/graph-preview-format marker those builds write +// beside it (FormatMarker), whatever the metadata says. Nothing was opened, +// created or modified. Match it with errors.As to read BeadsDir, GraphMode and +// FormatMarker; the bd CLI refuses the same workspaces with exit code 16. +// Aliased here because internal/configfile is unimportable outside this +// module. +type UnsupportedWorkspaceFormatError = configfile.UnsupportedWorkspaceFormatError + // Transaction provides atomic multi-operation support within a database transaction. // Use Storage.RunInTransaction() to obtain a Transaction instance. type Transaction = beads.Transaction @@ -369,6 +381,9 @@ func Open(ctx context.Context, dbPath string) (Storage, error) { // name configuration. // beadsDir is the path to the .beads directory. func OpenFromConfig(ctx context.Context, beadsDir string) (Storage, error) { + if err := configfile.CheckDiscoveredWorkspaceFormat(beadsDir); err != nil { + return nil, err + } return dolt.NewFromConfigWithOptions(ctx, beadsDir, &dolt.Config{CreateIfMissing: true}) } @@ -403,6 +418,11 @@ func OpenFromConfig(ctx context.Context, beadsDir string) (Storage, error) { // operation holds one exclusively; zero means fail fast. A busy gate is // reported as ErrGateBusy (match with errors.Is). func OpenGated(ctx context.Context, beadsDir string, wait time.Duration) (Storage, error) { + // Before the gates: taking them creates gate files in and beside a + // workspace this build must leave untouched. + if err := configfile.CheckDiscoveredWorkspaceFormat(beadsDir); err != nil { + return nil, err + } wsGate, err := workspacegate.ForWorkspace(beadsDir) if err != nil { return nil, err diff --git a/beads_cgo.go b/beads_cgo.go index d36297ce3..31b91c683 100644 --- a/beads_cgo.go +++ b/beads_cgo.go @@ -46,6 +46,11 @@ func OpenBestAvailable(ctx context.Context, beadsDir string) (Storage, error) { // typed refusal (the matching ErrXWithoutOpenWith sentinel), never a silent // open with the field ignored. func OpenBestAvailableWith(ctx context.Context, beadsDir string, opts OpenOptions) (Storage, error) { + // Before Load, which can migrate a legacy config.json, and before any + // backend is chosen, as the CLI store factories do. + if err := configfile.CheckDiscoveredWorkspaceFormat(beadsDir); err != nil { + return nil, err + } cfg, err := configfile.Load(beadsDir) if err != nil { return nil, fmt.Errorf("loading storage metadata: %w", err) diff --git a/beads_nocgo.go b/beads_nocgo.go index ba6853c4a..878a87d45 100644 --- a/beads_nocgo.go +++ b/beads_nocgo.go @@ -32,6 +32,11 @@ func OpenBestAvailable(ctx context.Context, beadsDir string) (Storage, error) { // rationale; behavior here is identical except that embedded Dolt is // unavailable without CGO. func OpenBestAvailableWith(ctx context.Context, beadsDir string, opts OpenOptions) (Storage, error) { + // Before Load, which can migrate a legacy config.json, and before any + // backend is chosen, as the CLI store factories do. + if err := configfile.CheckDiscoveredWorkspaceFormat(beadsDir); err != nil { + return nil, err + } cfg, err := configfile.Load(beadsDir) if err != nil { return nil, fmt.Errorf("loading storage metadata: %w", err) diff --git a/cmd/bd/bootstrap.go b/cmd/bd/bootstrap.go index 8d07611cf..c910aa300 100644 --- a/cmd/bd/bootstrap.go +++ b/cmd/bd/bootstrap.go @@ -1615,7 +1615,7 @@ func findParentConfig(beadsDir string) (*configfile.Config, error) { candidate := filepath.Join(dir, ".beads") cfg, err := configfile.LoadForDiscovery(candidate) if (err != nil || cfg == nil) && hasGraphFormatMarker(candidate) { - return nil, &unsupportedWorkspaceFormatError{beadsDir: candidate, formatMarker: true} + return nil, &configfile.UnsupportedWorkspaceFormatError{BeadsDir: candidate, FormatMarker: true} } if err != nil { return nil, fmt.Errorf("%s: %w", configfile.ConfigPath(candidate), err) diff --git a/cmd/bd/workspace_format_guard.go b/cmd/bd/workspace_format_guard.go index 1ae6c3995..2b7d35124 100644 --- a/cmd/bd/workspace_format_guard.go +++ b/cmd/bd/workspace_format_guard.go @@ -2,9 +2,6 @@ package main import ( "errors" - "fmt" - "os" - "path/filepath" "github.com/steveyegge/beads/internal/configfile" ) @@ -12,7 +9,7 @@ import ( // ExitUnsupportedWorkspaceFormat is the exit code for a command refused // because its workspace declares a format this bd build cannot open: a // metadata.json graph_mode other than "dependency", or a graph workspace -// format marker (graphFormatMarkerFile). Stable so a wrapper can +// format marker (configfile.GraphFormatMarkerFile). Stable so a wrapper can // tell "this workspace needs a different bd build" apart from a generic // failure (1) without grepping stderr. // @@ -21,86 +18,28 @@ import ( // §E3, which freezes 10/11/12/130. const ExitUnsupportedWorkspaceFormat = 16 -// graphFormatMarkerFile is the format sentinel graph-preview builds write in -// .beads before metadata.json, so that a graph workspace whose metadata was -// never written, was lost, or was rewritten without graph_mode by a bd that -// does not know the field still declares its format. -const graphFormatMarkerFile = "graph-preview-format" - -// unsupportedWorkspaceFormatError refuses a workspace whose metadata or format -// marker declares a data model other than the Issue store this build opens. -// Graph-preview builds persist graph_mode "link" plus graph_* binding fields -// beside an ordinary Dolt backend, and the graphFormatMarkerFile beside them, -// so nothing else tells this build that the database behind it is not an -// Issue store; opening it as one would run Issue migrations into it. -type unsupportedWorkspaceFormatError struct { - beadsDir string - graphMode string - // formatMarker reports that graphFormatMarkerFile, not graph_mode, - // declared the format. - formatMarker bool -} - -func (e *unsupportedWorkspaceFormatError) Error() string { - if e.formatMarker { - return fmt.Sprintf("workspace %s has a %s marker, which this bd build does not support; %s", - e.beadsDir, graphFormatMarkerFile, configfile.BackendNotOpenedGuarantee) - } - return fmt.Sprintf("workspace %s uses format graph_mode %q, which this bd build does not support; %s", - e.beadsDir, e.graphMode, configfile.BackendNotOpenedGuarantee) -} - -func (e *unsupportedWorkspaceFormatError) hint() string { - if e.formatMarker { - return fmt.Sprintf("restore metadata.json (graph_mode and the graph_* fields) from version control or a backup, then use a bd build that supports graph workspaces; do not remove .beads/%s", graphFormatMarkerFile) - } - return fmt.Sprintf("use a bd build that supports graph_mode %q workspaces; leave graph_mode and the graph_* fields in metadata.json unchanged", e.graphMode) -} - // checkWorkspaceFormat refuses metadata whose graph_mode marker names any -// model but the default. An unknown marker is refused too: it may describe a -// format newer than this build, so it must never fall back to the Issue store. -// It also refuses a beadsDir holding the graph format marker file, whatever -// the metadata says and whatever the file holds. cfg may be nil (no metadata); -// beadsDir may be "" (no workspace directory, so no marker to find). +// model but the default, and a beadsDir holding the graph format marker file, +// with a *configfile.UnsupportedWorkspaceFormatError. It is +// configfile.CheckWorkspaceFormat, the check the public beads package and the +// Dolt store's metadata-driven opens apply too, so the CLI and a library +// caller refuse the same workspaces with the same error. func checkWorkspaceFormat(cfg *configfile.Config, beadsDir string) error { - if mode, err := cfg.GetGraphMode(); err != nil || mode != configfile.GraphModeDependency { - return &unsupportedWorkspaceFormatError{beadsDir: beadsDir, graphMode: cfg.GraphMode} - } - if hasGraphFormatMarker(beadsDir) { - return &unsupportedWorkspaceFormatError{beadsDir: beadsDir, formatMarker: true} - } - return nil + return configfile.CheckWorkspaceFormat(cfg, beadsDir) } // loadAndCheckWorkspaceFormat reads beadsDir's discovery metadata without -// migrating it and refuses a workspace in a format this build cannot open -// (checkWorkspaceFormat). cfg is nil when beadsDir has no metadata. Metadata -// that cannot be read beside the graph format marker is refused as that -// format too: the marker alone declares it. +// migrating it and refuses a workspace in a format this build cannot open, +// including unreadable metadata beside the graph format marker. It is +// configfile.LoadAndCheckWorkspaceFormat. func loadAndCheckWorkspaceFormat(beadsDir string) (*configfile.Config, error) { - cfg, err := configfile.LoadForDiscovery(beadsDir) - if err != nil { - if hasGraphFormatMarker(beadsDir) { - return nil, &unsupportedWorkspaceFormatError{beadsDir: beadsDir, formatMarker: true} - } - return nil, fmt.Errorf("loading config: %w", err) - } - if err := checkWorkspaceFormat(cfg, beadsDir); err != nil { - return nil, err - } - return cfg, nil + return configfile.LoadAndCheckWorkspaceFormat(beadsDir) } // hasGraphFormatMarker reports whether anything exists at beadsDir's graph -// format marker path. Callers read beadsDir's metadata first, so a directory -// bd cannot search has already failed there. +// format marker path (configfile.HasGraphFormatMarker). func hasGraphFormatMarker(beadsDir string) bool { - if beadsDir == "" { - return false - } - _, err := os.Lstat(filepath.Join(beadsDir, graphFormatMarkerFile)) - return err == nil + return configfile.HasGraphFormatMarker(beadsDir) } // checkDiscoveredWorkspaceFormat runs checkWorkspaceFormat on beadsDir's @@ -110,15 +49,14 @@ func hasGraphFormatMarker(beadsDir string) bool { // config.json into metadata.json before the format check could refuse it. // Metadata that does not load is left to the caller's own load to report, // unless the graph format marker sits beside it (loadAndCheckWorkspaceFormat). +// It is configfile.CheckDiscoveredWorkspaceFormat, the pre-check the public +// beads package's opens run too. func checkDiscoveredWorkspaceFormat(beadsDir string) error { - if _, err := loadAndCheckWorkspaceFormat(beadsDir); isUnsupportedWorkspaceFormat(err) { - return err - } - return nil + return configfile.CheckDiscoveredWorkspaceFormat(beadsDir) } func isUnsupportedWorkspaceFormat(err error) bool { - var formatErr *unsupportedWorkspaceFormatError + var formatErr *configfile.UnsupportedWorkspaceFormatError return errors.As(err, &formatErr) } @@ -130,10 +68,10 @@ func handleWorkspaceGuardError(err error) error { if err == nil { return nil } - var formatErr *unsupportedWorkspaceFormatError + var formatErr *configfile.UnsupportedWorkspaceFormatError if !errors.As(err, &formatErr) { return HandleError("%v", err) } - _ = HandleErrorWithHint(formatErr.Error(), formatErr.hint()) + _ = HandleErrorWithHint(formatErr.Error(), formatErr.Hint()) return &exitError{Code: ExitUnsupportedWorkspaceFormat} } diff --git a/cmd/bd/workspace_format_guard_test.go b/cmd/bd/workspace_format_guard_test.go index d66f03f29..a19ae0d80 100644 --- a/cmd/bd/workspace_format_guard_test.go +++ b/cmd/bd/workspace_format_guard_test.go @@ -379,7 +379,7 @@ func TestHandleWorkspaceGuardErrorExitCodes(t *testing.T) { if err := handleWorkspaceGuardError(nil); err != nil { t.Fatalf("handleWorkspaceGuardError(nil) = %v", err) } - formatErr := &unsupportedWorkspaceFormatError{beadsDir: "/w/.beads", graphMode: "link"} + formatErr := &configfile.UnsupportedWorkspaceFormatError{BeadsDir: "/w/.beads", GraphMode: "link"} markerDir := t.TempDir() writeFile(t, filepath.Join(markerDir, "graph-preview-format"), []byte("link-preview-v9\n")) markerErr := checkWorkspaceFormat(nil, markerDir) diff --git a/internal/configfile/BUILD.bazel b/internal/configfile/BUILD.bazel index b56381c3c..44d63260a 100644 --- a/internal/configfile/BUILD.bazel +++ b/internal/configfile/BUILD.bazel @@ -12,6 +12,7 @@ go_library( "external_dolt_config.go", "graph_mode.go", "proxied_server_client_info.go", + "workspace_format.go", ], importpath = "github.com/steveyegge/beads/internal/configfile", visibility = ["//:__subpackages__"], @@ -30,6 +31,7 @@ go_test( "credentials_test.go", "external_dolt_config_test.go", "graph_mode_test.go", + "workspace_format_test.go", ], embed = [":configfile"], deps = ["//internal/config"], diff --git a/internal/configfile/workspace_format.go b/internal/configfile/workspace_format.go new file mode 100644 index 000000000..9da5f88b1 --- /dev/null +++ b/internal/configfile/workspace_format.go @@ -0,0 +1,118 @@ +package configfile + +import ( + "errors" + "fmt" + "os" + "path/filepath" +) + +// GraphFormatMarkerFile is the format sentinel graph-preview builds write in +// .beads before metadata.json, so that a graph workspace whose metadata was +// never written, was lost, or was rewritten without graph_mode by a bd that +// does not know the field still declares its format. +const GraphFormatMarkerFile = "graph-preview-format" + +// UnsupportedWorkspaceFormatError refuses a workspace whose metadata or format +// marker declares a data model other than the Issue store this build opens. +// Graph-preview builds persist graph_mode "link" plus graph_* binding fields +// beside an ordinary Dolt backend, and the GraphFormatMarkerFile beside them, +// so nothing else tells this build that the database behind it is not an +// Issue store; opening it as one would run Issue migrations into it. +// +// It is defined here, beside the metadata, so that every open path refuses +// with the same type and text: cmd/bd's admission guard and store factories, +// the public beads package, and the Dolt store's metadata-driven constructors. +type UnsupportedWorkspaceFormatError struct { + // BeadsDir is the workspace whose metadata or marker declared the format. + BeadsDir string + // GraphMode is the graph_mode marker exactly as the metadata wrote it. + GraphMode string + // FormatMarker reports that GraphFormatMarkerFile, not graph_mode, + // declared the format. + FormatMarker bool +} + +func (e *UnsupportedWorkspaceFormatError) Error() string { + if e.FormatMarker { + return fmt.Sprintf("workspace %s has a %s marker, which this bd build does not support; %s", + e.BeadsDir, GraphFormatMarkerFile, BackendNotOpenedGuarantee) + } + return fmt.Sprintf("workspace %s uses format graph_mode %q, which this bd build does not support; %s", + e.BeadsDir, e.GraphMode, BackendNotOpenedGuarantee) +} + +// Hint names the remedy: a build that supports the format, with the +// workspace's metadata and marker left as they are (or, for a marker without +// graph metadata, the metadata restored first). +func (e *UnsupportedWorkspaceFormatError) Hint() string { + if e.FormatMarker { + return fmt.Sprintf("restore metadata.json (graph_mode and the graph_* fields) from version control or a backup, then use a bd build that supports graph workspaces; do not remove .beads/%s", GraphFormatMarkerFile) + } + return fmt.Sprintf("use a bd build that supports graph_mode %q workspaces; leave graph_mode and the graph_* fields in metadata.json unchanged", e.GraphMode) +} + +// CheckWorkspaceFormat refuses metadata whose graph_mode marker names any +// model but the default, with an *UnsupportedWorkspaceFormatError. An unknown +// marker is refused too: it may describe a format newer than this build, so +// it must never fall back to the Issue store. It also refuses a beadsDir +// holding GraphFormatMarkerFile, whatever the metadata says and whatever the +// file holds. cfg may be nil (no metadata); beadsDir may be "" (no workspace +// directory, so no marker to find). +func CheckWorkspaceFormat(cfg *Config, beadsDir string) error { + if mode, err := cfg.GetGraphMode(); err != nil || mode != GraphModeDependency { + return &UnsupportedWorkspaceFormatError{BeadsDir: beadsDir, GraphMode: cfg.GraphMode} + } + if HasGraphFormatMarker(beadsDir) { + return &UnsupportedWorkspaceFormatError{BeadsDir: beadsDir, FormatMarker: true} + } + return nil +} + +// LoadAndCheckWorkspaceFormat reads beadsDir's discovery metadata without +// migrating it (LoadForDiscovery) and refuses a workspace in a format this +// build cannot open (CheckWorkspaceFormat). cfg is nil when beadsDir has no +// metadata. Metadata that cannot be read beside the graph format marker is +// refused as that format too: the marker alone declares it. Any other load +// failure is returned wrapped. +func LoadAndCheckWorkspaceFormat(beadsDir string) (*Config, error) { + cfg, err := LoadForDiscovery(beadsDir) + if err != nil { + if HasGraphFormatMarker(beadsDir) { + return nil, &UnsupportedWorkspaceFormatError{BeadsDir: beadsDir, FormatMarker: true} + } + return nil, fmt.Errorf("loading config: %w", err) + } + if err := CheckWorkspaceFormat(cfg, beadsDir); err != nil { + return nil, err + } + return cfg, nil +} + +// HasGraphFormatMarker reports whether anything exists at beadsDir's graph +// format marker path. Callers read beadsDir's metadata first, so a directory +// bd cannot search has already failed there. An empty beadsDir never probes +// the working directory. +func HasGraphFormatMarker(beadsDir string) bool { + if beadsDir == "" { + return false + } + _, err := os.Lstat(filepath.Join(beadsDir, GraphFormatMarkerFile)) + return err == nil +} + +// CheckDiscoveredWorkspaceFormat runs CheckWorkspaceFormat on beadsDir's +// metadata as LoadForDiscovery reads it, which never rewrites the workspace. +// An open that has not yet decided this build may touch the workspace runs it +// first: Load would otherwise migrate a legacy config.json into metadata.json +// before the format check could refuse it. Metadata that does not load is +// left to the caller's own load to report, unless the graph format marker +// sits beside it (LoadAndCheckWorkspaceFormat). +func CheckDiscoveredWorkspaceFormat(beadsDir string) error { + _, err := LoadAndCheckWorkspaceFormat(beadsDir) + var formatErr *UnsupportedWorkspaceFormatError + if errors.As(err, &formatErr) { + return err + } + return nil +} diff --git a/internal/configfile/workspace_format_test.go b/internal/configfile/workspace_format_test.go new file mode 100644 index 000000000..ddcf6f2ca --- /dev/null +++ b/internal/configfile/workspace_format_test.go @@ -0,0 +1,160 @@ +package configfile + +import ( + "errors" + "os" + "path/filepath" + "strings" + "testing" +) + +func TestCheckWorkspaceFormatRefusesOtherModels(t *testing.T) { + const beadsDir = "/work/repo/.beads" + for _, mode := range []string{GraphModeLink, "future-format", "Dependency"} { + t.Run(mode, func(t *testing.T) { + err := CheckWorkspaceFormat(&Config{Backend: BackendDolt, GraphMode: mode}, beadsDir) + var formatErr *UnsupportedWorkspaceFormatError + if !errors.As(err, &formatErr) { + t.Fatalf("CheckWorkspaceFormat() = %v, want *UnsupportedWorkspaceFormatError", err) + } + if formatErr.BeadsDir != beadsDir || formatErr.GraphMode != mode { + t.Fatalf("refusal = %+v, want BeadsDir %q and GraphMode %q", formatErr, beadsDir, mode) + } + for _, want := range []string{beadsDir, `graph_mode "` + mode + `"`, "this bd build does not support", BackendNotOpenedGuarantee} { + if !strings.Contains(err.Error(), want) { + t.Errorf("refusal %q does not mention %q", err, want) + } + } + if hint := formatErr.Hint(); !strings.Contains(hint, `graph_mode "`+mode+`"`) || !strings.Contains(hint, "unchanged") { + t.Errorf("hint %q does not name the mode or ask to keep the marker", hint) + } + }) + } +} + +func TestCheckWorkspaceFormatAdmitsIssueWorkspaces(t *testing.T) { + for name, cfg := range map[string]*Config{ + "no metadata": nil, + "absent marker": {Backend: BackendDolt}, + "dependency marker": {Backend: BackendDolt, GraphMode: GraphModeDependency}, + } { + t.Run(name, func(t *testing.T) { + if err := CheckWorkspaceFormat(cfg, "/work/repo/.beads"); err != nil { + t.Fatalf("CheckWorkspaceFormat() = %v, want admission", err) + } + }) + } +} + +// TestCheckDiscoveredWorkspaceFormatLeavesWorkspaceUnchanged pins the +// pre-check every open runs before Load: it refuses a graph workspace marked +// in either metadata file without migrating a legacy config.json, and leaves +// a workspace with no metadata to the caller's own load. +func TestCheckDiscoveredWorkspaceFormatLeavesWorkspaceUnchanged(t *testing.T) { + const metadata = `{"backend":"dolt","graph_mode":"link"}` + for _, name := range []string{ConfigFileName, "config.json"} { + t.Run(name, func(t *testing.T) { + beadsDir := t.TempDir() + if err := os.WriteFile(filepath.Join(beadsDir, name), []byte(metadata), 0o600); err != nil { + t.Fatal(err) + } + err := CheckDiscoveredWorkspaceFormat(beadsDir) + var formatErr *UnsupportedWorkspaceFormatError + if !errors.As(err, &formatErr) || formatErr.BeadsDir != beadsDir || formatErr.GraphMode != GraphModeLink { + t.Fatalf("CheckDiscoveredWorkspaceFormat() = %v, want the link refusal for %s", err, beadsDir) + } + entries, err := os.ReadDir(beadsDir) + if err != nil || len(entries) != 1 || entries[0].Name() != name { + t.Fatalf("pre-check changed the workspace: %v, %v", entries, err) + } + }) + } + if err := CheckDiscoveredWorkspaceFormat(t.TempDir()); err != nil { + t.Fatalf("CheckDiscoveredWorkspaceFormat(no metadata) = %v, want nil", err) + } +} + +// TestCheckWorkspaceFormatRefusesFormatMarker pins the marker half of the +// check: a beadsDir holding GraphFormatMarkerFile is refused whatever its +// metadata says, with its own text and hint, while graph_mode still wins when +// both declare the format. +func TestCheckWorkspaceFormatRefusesFormatMarker(t *testing.T) { + beadsDir := t.TempDir() + if err := os.WriteFile(filepath.Join(beadsDir, GraphFormatMarkerFile), []byte("anything"), 0o600); err != nil { + t.Fatal(err) + } + for name, cfg := range map[string]*Config{ + "no metadata": nil, + "stripped metadata": {Backend: BackendDolt}, + "dependency marker": {Backend: BackendDolt, GraphMode: GraphModeDependency}, + } { + t.Run(name, func(t *testing.T) { + err := CheckWorkspaceFormat(cfg, beadsDir) + var formatErr *UnsupportedWorkspaceFormatError + if !errors.As(err, &formatErr) || !formatErr.FormatMarker || formatErr.BeadsDir != beadsDir || formatErr.GraphMode != "" { + t.Fatalf("CheckWorkspaceFormat() = %#v, want the format marker refusal", err) + } + wantErr := "workspace " + beadsDir + " has a graph-preview-format marker, which this bd build does not support; " + BackendNotOpenedGuarantee + if err.Error() != wantErr { + t.Errorf("refusal = %q, want %q", err, wantErr) + } + wantHint := "restore metadata.json (graph_mode and the graph_* fields) from version control or a backup, then use a bd build that supports graph workspaces; do not remove .beads/graph-preview-format" + if formatErr.Hint() != wantHint { + t.Errorf("hint = %q, want %q", formatErr.Hint(), wantHint) + } + }) + } + err := CheckWorkspaceFormat(&Config{Backend: BackendDolt, GraphMode: GraphModeLink}, beadsDir) + var formatErr *UnsupportedWorkspaceFormatError + if !errors.As(err, &formatErr) || formatErr.FormatMarker || formatErr.GraphMode != GraphModeLink { + t.Fatalf("CheckWorkspaceFormat(link beside marker) = %#v, want the graph_mode refusal", err) + } + if err := CheckWorkspaceFormat(nil, ""); err != nil { + t.Fatalf("CheckWorkspaceFormat(nil, \"\") = %v, want admission without probing the working directory", err) + } +} + +// TestCheckDiscoveredWorkspaceFormatRefusesFormatMarker pins that the +// pre-check refuses a marked workspace with no, stripped or unparseable +// metadata, and still leaves unparseable metadata without the marker to the +// caller's own load. +func TestCheckDiscoveredWorkspaceFormatRefusesFormatMarker(t *testing.T) { + for name, files := range map[string]map[string]string{ + "marker only": {GraphFormatMarkerFile: ""}, + "stripped metadata.json": {GraphFormatMarkerFile: "x", ConfigFileName: `{"backend":"dolt"}`}, + "stripped legacy config.json": {GraphFormatMarkerFile: "x", "config.json": `{"backend":"dolt"}`}, + "unparseable metadata.json": {GraphFormatMarkerFile: "x", ConfigFileName: `{"backend":`}, + "unparseable legacy config.json": {GraphFormatMarkerFile: "x", "config.json": `{"backend":`}, + } { + t.Run(name, func(t *testing.T) { + beadsDir := t.TempDir() + for file, content := range files { + if err := os.WriteFile(filepath.Join(beadsDir, file), []byte(content), 0o600); err != nil { + t.Fatal(err) + } + } + err := CheckDiscoveredWorkspaceFormat(beadsDir) + var formatErr *UnsupportedWorkspaceFormatError + if !errors.As(err, &formatErr) || !formatErr.FormatMarker || formatErr.BeadsDir != beadsDir { + t.Fatalf("CheckDiscoveredWorkspaceFormat() = %v, want the format marker refusal", err) + } + if _, err := LoadAndCheckWorkspaceFormat(beadsDir); !errors.As(err, &formatErr) || !formatErr.FormatMarker { + t.Fatalf("LoadAndCheckWorkspaceFormat() = %v, want the format marker refusal", err) + } + entries, err := os.ReadDir(beadsDir) + if err != nil || len(entries) != len(files) { + t.Fatalf("pre-check changed the workspace: %v, %v", entries, err) + } + }) + } + unparseable := t.TempDir() + if err := os.WriteFile(filepath.Join(unparseable, ConfigFileName), []byte(`{"backend":`), 0o600); err != nil { + t.Fatal(err) + } + if err := CheckDiscoveredWorkspaceFormat(unparseable); err != nil { + t.Fatalf("CheckDiscoveredWorkspaceFormat(unparseable, no marker) = %v, want nil", err) + } + if _, err := LoadAndCheckWorkspaceFormat(unparseable); err == nil || !strings.Contains(err.Error(), "loading config") { + t.Fatalf("LoadAndCheckWorkspaceFormat(unparseable, no marker) = %v, want the load failure", err) + } +} diff --git a/internal/storage/dolt/open.go b/internal/storage/dolt/open.go index 6de61bbff..9d55757c7 100644 --- a/internal/storage/dolt/open.go +++ b/internal/storage/dolt/open.go @@ -83,7 +83,15 @@ func ApplyResolvedServerPort(beadsDir string, cfg *Config) { // closed instead of opening a new, empty Dolt database. beadsDir is the workspace // the config came from, so the refusal can offer the metadata heal when that // workspace already holds a Dolt database. +// +// It first refuses a workspace whose metadata or graph format marker declares a +// data model this build cannot open (configfile.CheckWorkspaceFormat): such a +// workspace keeps another model's database behind an ordinary Dolt backend, +// and opening it here would run Issue migrations into that database. func requireDoltBackend(fileCfg *configfile.Config, beadsDir string) error { + if err := configfile.CheckWorkspaceFormat(fileCfg, beadsDir); err != nil { + return err + } switch fileCfg.Backend { case configfile.BackendPostgres, configfile.BackendMySQL, configfile.BackendSQLite: return fmt.Errorf("configured storage backend %q is no longer supported and cannot be opened as Dolt: %s", fileCfg.Backend, configfile.RemovedBackendDetailAt(fileCfg.Backend, beadsDir, fileCfg)) @@ -109,6 +117,11 @@ func NewFromConfig(ctx context.Context, beadsDir string) (*DoltStore, error) { // `bd doctor` that should behave the same way as normal top-level CLI commands // while still honoring externally managed server mode. func NewFromConfigWithCLIOptions(ctx context.Context, beadsDir string, cfg *Config) (*DoltStore, error) { + // Refuse a graph workspace before Load can migrate a legacy config.json + // into metadata.json; requireDoltBackend re-checks the loaded config. + if err := configfile.CheckDiscoveredWorkspaceFormat(beadsDir); err != nil { + return nil, err + } fileCfg, err := configfile.Load(beadsDir) if err != nil { return nil, fmt.Errorf("loading config: %w", err) @@ -139,6 +152,11 @@ func NewFromConfigWithCLIOptions(ctx context.Context, beadsDir string, cfg *Conf // NewFromConfigWithOptions creates a DoltStore with options from metadata.json. // Options in cfg override those from the config file. Pass nil for default options. func NewFromConfigWithOptions(ctx context.Context, beadsDir string, cfg *Config) (*DoltStore, error) { + // Refuse a graph workspace before Load can migrate a legacy config.json + // into metadata.json; requireDoltBackend re-checks the loaded config. + if err := configfile.CheckDiscoveredWorkspaceFormat(beadsDir); err != nil { + return nil, err + } fileCfg, err := configfile.Load(beadsDir) if err != nil { return nil, fmt.Errorf("loading config: %w", err) diff --git a/internal/storage/dolt/open_test.go b/internal/storage/dolt/open_test.go index c3d86077d..10a79f28b 100644 --- a/internal/storage/dolt/open_test.go +++ b/internal/storage/dolt/open_test.go @@ -3,6 +3,7 @@ package dolt import ( "bytes" "context" + "errors" "io" "os" "path/filepath" @@ -208,6 +209,138 @@ func TestConfigConstructorsRejectNonDoltBackends(t *testing.T) { } } +// TestConfigConstructorsRefuseGraphWorkspace pins that every metadata-driven +// constructor refuses a workspace whose metadata declares another data model, +// before it resolves a server or writes anything into the workspace (a port +// file, a Dolt root, or the metadata.json that Load would migrate a legacy +// config.json into). The CLI's own store open does not come through here; the +// public beads package's config opens and cmd/bd's direct NewFromConfig* +// callers do. +func TestConfigConstructorsRefuseGraphWorkspace(t *testing.T) { + // If the refusal were missing, the open must fail fast rather than reach + // the suite's shared test server. + t.Setenv("BEADS_DOLT_SERVER_HOST", "127.0.0.1") + t.Setenv("BEADS_DOLT_SERVER_PORT", "1") + for _, mode := range []string{configfile.GraphModeLink, "future-format"} { + for _, doltMode := range []string{configfile.DoltModeEmbedded, configfile.DoltModeServer} { + // "config.json" is the legacy name Load migrates into metadata.json; + // a refused open must not run that migration either. + for _, fileName := range []string{configfile.ConfigFileName, "config.json"} { + t.Run(mode+"/"+doltMode+"/"+fileName, func(t *testing.T) { + beadsDir := t.TempDir() + metadata := `{"backend":"dolt","dolt_mode":"` + doltMode + `","dolt_database":"beads_graph_0123","graph_mode":"` + mode + `","graph_future_binding":"keep-me"}` + path := filepath.Join(beadsDir, fileName) + if err := os.WriteFile(path, []byte(metadata), 0o600); err != nil { + t.Fatal(err) + } + constructors := map[string]func() (*DoltStore, error){ + "default": func() (*DoltStore, error) { + return NewFromConfig(t.Context(), beadsDir) + }, + "standard": func() (*DoltStore, error) { + return NewFromConfigWithOptions(t.Context(), beadsDir, &Config{DisableAutoStart: true, CreateIfMissing: true}) + }, + "cli": func() (*DoltStore, error) { + return NewFromConfigWithCLIOptions(t.Context(), beadsDir, &Config{DisableAutoStart: true, ReadOnly: true}) + }, + } + for name, construct := range constructors { + t.Run(name, func(t *testing.T) { + store, err := construct() + if err == nil { + _ = store.Close() + t.Fatalf("graph_mode %q workspace opened as Dolt", mode) + } + var formatErr *configfile.UnsupportedWorkspaceFormatError + if !errors.As(err, &formatErr) || formatErr.BeadsDir != beadsDir || formatErr.GraphMode != mode { + t.Fatalf("constructor error = %v, want UnsupportedWorkspaceFormatError for %s", err, beadsDir) + } + entries, readErr := os.ReadDir(beadsDir) + if readErr != nil || len(entries) != 1 { + t.Fatalf("refused open changed the workspace: %v, %v", entries, readErr) + } + if got, readErr := os.ReadFile(path); readErr != nil || string(got) != metadata { + t.Fatalf("refused open rewrote %s: %q, %v", fileName, got, readErr) + } + }) + } + }) + } + } + } +} + +// TestConfigConstructorsRefuseGraphFormatMarker pins that every +// metadata-driven constructor refuses a workspace holding the +// graph-preview-format marker graph-preview builds write, whatever its +// metadata says (none, stripped of graph_mode in either metadata file, an +// explicit dependency, or unparseable), and leaves the workspace unchanged. +func TestConfigConstructorsRefuseGraphFormatMarker(t *testing.T) { + // If the refusal were missing, the open must fail fast rather than reach + // the suite's shared test server. + t.Setenv("BEADS_DOLT_SERVER_HOST", "127.0.0.1") + t.Setenv("BEADS_DOLT_SERVER_PORT", "1") + layouts := []struct { + name string + fileName string // "" writes no metadata file + metadata string + }{ + {name: "marker only"}, + {name: "stripped embedded metadata", fileName: configfile.ConfigFileName, metadata: `{"backend":"dolt","dolt_mode":"embedded","dolt_database":"beads_graph_0123"}`}, + {name: "stripped server metadata", fileName: configfile.ConfigFileName, metadata: `{"backend":"dolt","dolt_mode":"server","dolt_database":"beads_graph_0123"}`}, + {name: "stripped legacy config.json", fileName: "config.json", metadata: `{"backend":"dolt","dolt_mode":"embedded","dolt_database":"beads_graph_0123"}`}, + {name: "dependency metadata", fileName: configfile.ConfigFileName, metadata: `{"backend":"dolt","dolt_mode":"server","graph_mode":"dependency"}`}, + {name: "unparseable metadata", fileName: configfile.ConfigFileName, metadata: `{"backend":"dolt",`}, + } + for _, layout := range layouts { + t.Run(layout.name, func(t *testing.T) { + beadsDir := t.TempDir() + want := map[string]string{configfile.GraphFormatMarkerFile: "link-preview-v9\n"} + if layout.fileName != "" { + want[layout.fileName] = layout.metadata + } + for name, content := range want { + if err := os.WriteFile(filepath.Join(beadsDir, name), []byte(content), 0o600); err != nil { + t.Fatal(err) + } + } + constructors := map[string]func() (*DoltStore, error){ + "default": func() (*DoltStore, error) { + return NewFromConfig(t.Context(), beadsDir) + }, + "standard": func() (*DoltStore, error) { + return NewFromConfigWithOptions(t.Context(), beadsDir, &Config{DisableAutoStart: true, CreateIfMissing: true}) + }, + "cli": func() (*DoltStore, error) { + return NewFromConfigWithCLIOptions(t.Context(), beadsDir, &Config{DisableAutoStart: true, ReadOnly: true}) + }, + } + for name, construct := range constructors { + t.Run(name, func(t *testing.T) { + store, err := construct() + if err == nil { + _ = store.Close() + t.Fatalf("workspace with a %s marker opened as Dolt", configfile.GraphFormatMarkerFile) + } + var formatErr *configfile.UnsupportedWorkspaceFormatError + if !errors.As(err, &formatErr) || formatErr.BeadsDir != beadsDir || !formatErr.FormatMarker { + t.Fatalf("constructor error = %v, want the format marker refusal for %s", err, beadsDir) + } + entries, readErr := os.ReadDir(beadsDir) + if readErr != nil || len(entries) != len(want) { + t.Fatalf("refused open changed the workspace: %v, %v", entries, readErr) + } + for fileName, content := range want { + if got, readErr := os.ReadFile(filepath.Join(beadsDir, fileName)); readErr != nil || string(got) != content { + t.Fatalf("refused open rewrote %s: %q, %v", fileName, got, readErr) + } + } + }) + } + }) + } +} + func TestCLIDirUsesSharedDoltRootInSharedServerMode(t *testing.T) { sharedRoot := t.TempDir() t.Setenv("BEADS_DOLT_SHARED_SERVER", "1") diff --git a/open_backend_selection_test.go b/open_backend_selection_test.go index dc79697d7..36ca6bb7e 100644 --- a/open_backend_selection_test.go +++ b/open_backend_selection_test.go @@ -3,9 +3,11 @@ package beads_test import ( "context" "errors" + "maps" "net/http" "os" "path/filepath" + "slices" "strings" "testing" @@ -475,3 +477,192 @@ func TestOpenBestAvailableWithRefusesPerOpenOptionsOnPlainDolt(t *testing.T) { }) } } + +// graphWorkspaceMetadata is the metadata.json a graph-preview build of bd +// writes for a graph_mode "link" workspace: the marker and its binding beside +// an ordinary embedded Dolt backend. +const graphWorkspaceMetadata = `{ + "database": "", + "backend": "dolt", + "graph_mode": "link", + "graph_scope_url": "https://example.test/scope", + "graph_authority_id": "0123456789abcdef0123456789abcdef", + "graph_workspace": "/work/repo/.beads", + "graph_schema_version": 7, + "graph_ready": true, + "dolt_mode": "embedded", + "dolt_database": "beads_graph_0123456789abcdef0123456789abcdef", + "project_id": "0123456789abcdef0123456789abcdef" +}` + +// graphWorkspaceMetadataStripped is graphWorkspaceMetadata as a bd that +// predates graph_mode rewrites it: the graph fields are gone, but the database +// name still points at the graph database. +const graphWorkspaceMetadataStripped = `{ + "database": "", + "backend": "dolt", + "dolt_mode": "embedded", + "dolt_database": "beads_graph_0123456789abcdef0123456789abcdef", + "project_id": "0123456789abcdef0123456789abcdef" +}` + +// publicOpens are the public entry points that open a workspace from its +// metadata. +var publicOpens = map[string]func(context.Context, string) (beads.Storage, error){ + "OpenBestAvailable": beads.OpenBestAvailable, + "OpenBestAvailableWith": func(ctx context.Context, beadsDir string) (beads.Storage, error) { + return beads.OpenBestAvailableWith(ctx, beadsDir, beads.OpenOptions{}) + }, + "OpenFromConfig": beads.OpenFromConfig, + "OpenGated": func(ctx context.Context, beadsDir string) (beads.Storage, error) { + return beads.OpenGated(ctx, beadsDir, 0) + }, +} + +// workspaceTreeDigest records every path under root with its kind and, for +// files, its content, so a test can prove an open left a workspace untouched. +func workspaceTreeDigest(t *testing.T, root string) map[string]string { + t.Helper() + digest := map[string]string{} + err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error { + if err != nil { + return err + } + rel, err := filepath.Rel(root, path) + if err != nil { + return err + } + if d.IsDir() { + digest[rel] = "dir" + return nil + } + content, err := os.ReadFile(path) + if err != nil { + return err + } + digest[rel] = "file:" + string(content) + return nil + }) + if err != nil { + t.Fatalf("digest %s: %v", root, err) + } + return digest +} + +// TestPublicOpensRefuseGraphWorkspace is the library half of the graph_mode +// fence the CLI applies at admission: every public open of a workspace whose +// metadata or graph-preview-format marker declares another data model refuses +// it with the typed error before it opens, creates or modifies anything, in +// the workspace or beside it (the workspace gate file OpenGated takes lives +// beside .beads). +func TestPublicOpensRefuseGraphWorkspace(t *testing.T) { + // If the fence were missing, a server-mode open must fail fast rather + // than reach TestMain's shared test server. + t.Setenv("BEADS_DOLT_SERVER_HOST", "127.0.0.1") + t.Setenv("BEADS_DOLT_SERVER_PORT", "1") + layouts := []struct { + name string + filename string // "" writes no metadata file + metadata string + mode string + embedded bool + // marker writes the graph-preview-format marker graph-preview builds + // leave in .beads; the refusal then names the marker, not a mode. + marker bool + }{ + {name: "link embedded", filename: "metadata.json", metadata: graphWorkspaceMetadata, mode: "link", embedded: true}, + {name: "link server", filename: "metadata.json", metadata: `{"backend":"dolt","dolt_mode":"server","dolt_database":"beads_graph_0123","graph_mode":"link"}`, mode: "link"}, + {name: "unknown marker", filename: "metadata.json", metadata: `{"backend":"dolt","graph_mode":"future-format"}`, mode: "future-format"}, + // A refused open must not migrate a legacy config.json either. + {name: "link legacy config.json", filename: "config.json", metadata: graphWorkspaceMetadata, mode: "link", embedded: true}, + // A graph workspace whose metadata was never written, was lost, or + // was rewritten without graph_mode is still declared by the marker. + {name: "marker only", marker: true}, + {name: "marker beside stripped metadata", filename: "metadata.json", metadata: graphWorkspaceMetadataStripped, embedded: true, marker: true}, + {name: "marker beside stripped legacy config.json", filename: "config.json", metadata: graphWorkspaceMetadataStripped, embedded: true, marker: true}, + {name: "marker beside server metadata", filename: "metadata.json", metadata: `{"backend":"dolt","dolt_mode":"server","dolt_database":"beads_graph_0123"}`, marker: true}, + {name: "marker beside dependency metadata", filename: "metadata.json", metadata: `{"backend":"dolt","dolt_mode":"server","graph_mode":"dependency"}`, marker: true}, + {name: "marker beside unparseable metadata", filename: "metadata.json", metadata: `{"backend":"dolt",`, marker: true}, + } + for _, layout := range layouts { + for name, open := range publicOpens { + t.Run(layout.name+"/"+name, func(t *testing.T) { + root := t.TempDir() + beadsDir := filepath.Join(root, ".beads") + if err := os.MkdirAll(beadsDir, 0o750); err != nil { + t.Fatal(err) + } + if layout.filename != "" { + if err := os.WriteFile(filepath.Join(beadsDir, layout.filename), []byte(layout.metadata), 0o600); err != nil { + t.Fatal(err) + } + } + if layout.marker { + if err := os.WriteFile(filepath.Join(beadsDir, "graph-preview-format"), []byte("link-preview-v9\n"), 0o600); err != nil { + t.Fatal(err) + } + } + if layout.embedded { + graphDB := filepath.Join(beadsDir, "embeddeddolt", "beads_graph_0123456789abcdef0123456789abcdef", ".dolt") + if err := os.MkdirAll(graphDB, 0o750); err != nil { + t.Fatal(err) + } + } + before := workspaceTreeDigest(t, root) + + // Check err, not store: a failed OpenFromConfig can return a + // typed-nil store. + store, err := open(t.Context(), beadsDir) + if err == nil { + _ = store.Close() + t.Fatalf("%s opened a graph_mode %q workspace", name, layout.mode) + } + var formatErr *beads.UnsupportedWorkspaceFormatError + if !errors.As(err, &formatErr) { + t.Fatalf("%s error = %v, want *beads.UnsupportedWorkspaceFormatError", name, err) + } + if formatErr.BeadsDir != beadsDir || formatErr.GraphMode != layout.mode || formatErr.FormatMarker != layout.marker { + t.Fatalf("refusal = %+v, want BeadsDir %q, GraphMode %q and FormatMarker %v", formatErr, beadsDir, layout.mode, layout.marker) + } + if layout.marker && !strings.Contains(err.Error(), "graph-preview-format marker") { + t.Fatalf("refusal does not name the format marker: %v", err) + } + if !strings.Contains(err.Error(), "no storage database was opened or modified") { + t.Fatalf("refusal lacks the data-safety guarantee: %v", err) + } + if after := workspaceTreeDigest(t, root); !maps.Equal(after, before) { + t.Fatalf("%s changed the workspace: before %v, after %v", name, slices.Sorted(maps.Keys(before)), slices.Sorted(maps.Keys(after))) + } + }) + } + } +} + +// TestPublicOpensAdmitDependencyWorkspace pins the other side of the fence: a +// "dependency" marker is the ordinary Issue workspace, so the open proceeds to +// its backend (here, a server nothing listens on) instead of being refused. +func TestPublicOpensAdmitDependencyWorkspace(t *testing.T) { + t.Setenv("BEADS_DOLT_SERVER_HOST", "127.0.0.1") + t.Setenv("BEADS_DOLT_SERVER_PORT", "1") + for name, open := range publicOpens { + t.Run(name, func(t *testing.T) { + beadsDir := filepath.Join(t.TempDir(), ".beads") + if err := os.MkdirAll(beadsDir, 0o750); err != nil { + t.Fatal(err) + } + metadata := `{"backend":"dolt","dolt_mode":"server","graph_mode":"dependency"}` + if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(metadata), 0o600); err != nil { + t.Fatal(err) + } + store, err := open(t.Context(), beadsDir) + if err == nil { + _ = store.Close() + t.Fatalf("%s opened a store on an unreachable server", name) + } + var formatErr *beads.UnsupportedWorkspaceFormatError + if errors.As(err, &formatErr) || !strings.Contains(err.Error(), "unreachable") { + t.Fatalf("%s error = %v, want the backend's unreachable-server error", name, err) + } + }) + } +} From 5aa98ae7e177f605a641e35e62f4bff157485535 Mon Sep 17 00:00:00 2001 From: Eddie the Engineer <338826946+gascity-eddie[bot]@users.noreply.github.com> Date: Tue, 20 Oct 2026 07:40:02 +0000 Subject: [PATCH 14/14] fix(config): refuse a wrapped plain value and any unset that changes more than its key Maintainer fixup for the first three review passes of #7684. A flat dotted key under a flow-style top level skipped the span probe, and the nested walk never sees that spelling, so it reached the line matcher unchecked (finding 1, major). On the opening line (`{sync.remote: a, node_id: b}`) it is never matched: the value stays live and the unset reports success. On a line of its own, commenting it out can take the closing brace with it or leave the rest of a wrapped value behind; the review's seed re-read as `{branch: b}` with node_id gone. Such a key is a key inside a flow-style mapping, so it now gets the flow-style refusal a single-segment key there already had (the review's first option). That includes a one-line value on a line of its own, which the matcher used to comment out cleanly: refusing it is what the CHANGELOG promises for a key inside a flow-style mapping, and a row pins it. A plain scalar that starts on the key's line and continues onto a deeper one parses when cut after the key's line, as a shorter value, so the probe let it through and the continuation stayed behind (finding 2, major): `sync:` / ` remote: file:///a` / ` /b` re-read as `sync: /b`, and a wrapped `notes:` stopped the file from parsing. As both reviewers proposed, the probe now also compares the value in the cut with the whole file's. The cut holds the key's line and nothing after it, so the key's value is its last node, and a value that goes on reads shorter there. Aliases and collections are not compared: a cut that parses has closed them. The probe cut the document at "\n", but yaml.v3 numbers node lines at every YAML line break: LF, CR, CRLF, NEL, LS and PS (finding 3). A line separator or a lone carriage return above the key moved the cut, refusing a one-line value the base unsets and missing a wrapped one. yamlLineEnd counts breaks as yaml.v3 does. TestYamlLineEndCutsWhereYamlV3EndsALine checks the cut against yaml.v3's own line numbers for every break, also with the break as the file's first byte, where the look back for a CR before an LF has nothing to look at. The refusals before the edit judge the first document's parse, so some edits still got past them: a wrapped value in a later document (finding 4), a later duplicate of the key whose value wraps, an anchor on the key's line that an alias further down uses, and, beside the key, a line inside another value that only looks like it. Rather than name each shape, both unset callers now check commentOutYamlKey's result before they write (collateralUnsetRefusal). Read back, every document must hold what it held before, less some of the key's own entries in either spelling; an edit that adds, removes or changes anything else is refused. Documents are compared by position, because bd reads only the first one and an emptied first document would let the second take its place. An alias is compared by where the anchor it resolves to is, and never expanded, so the check stays linear in the file. A file that did not parse before the edit keeps the line matcher's best effort, as before. The second pass found that this check also refused the unset of a key the file does not hold, whenever a line inside another value looked like the key (finding 1, major). Over `notes: "see` / `jira.url: https://x"`, `bd config unset jira.url` failed with "remove the key by hand", and on that key's database-backed route it deleted the row first and then reported jira.url "still set in config.yaml". UnsetYamlConfig and UnsetUserYamlConfig document (false, nil) for an absent key. So commentOutYamlKey now asks the parse first (yamlHoldsKey): when no document holds the key in either spelling, the file is left as it is, and both callers report that nothing changed. A null value holds the key, so does either copy of a section the file writes twice (the matcher comments out both), and a file yaml.v3 cannot parse is taken to hold it, so the matcher's best effort stands there. The review suggested asking only once the collateral check fails. Asking before the matcher also covers a key inside a list item that check never sees: with a mapping beneath it the unset was refused as a value that goes on below, and with no value it was commented out, leaving no leaf to compare. The base did both, and both are now no-ops. The two refusal rows for absent keys moved into TestUnsetOfAnAbsentKeyStillSucceeds, with those two shapes and the flat spelling. A row pins that a line which only looks like the key is still refused when the key is set beside it, and another that the key under the second copy of its section is found. An alias was compared by its anchor's name, which YAML lets a later anchor reuse: with the later anchor's line commented out, the `*r` under backup.remote resolved to an earlier `&r keep`, and the check saw nothing change (finding 2, minor). The leaf now names where the anchor is as well, which commenting a line out never moves for an untouched node, so a key inside a mapping that an alias elsewhere shares still unsets. The third pass found that both checks still failed open when a later document did not parse (finding 1, major). yamlDocuments dropped the documents it had decoded as soon as one did not decode, so yamlHoldsKey took the key to be held and collateralUnsetRefusal left the edit unchecked. Over `notes: "see` / `jira.url: https://x"` / `---` / `other: [unclosed`, an unset of jira.url commented out the second line of notes and reported success, though bd reads that first document and saw notes change. yamlDocuments now returns the documents decoded before the error, and when there are any, they answer: yamlHoldsKey asks only them, and the check compares only them and requires the edit to leave every one of them parsing. So commenting out the only key of a first document cannot let the document that does not parse take its place, where bd could not read the file at all. The document that does not parse, and any after it, has no reading to compare and is read by nobody, so a key only there is not set. A file whose first document does not parse keeps the line matcher's best effort, as before. Listing the leaves copied the whole key path at every level, so its cost grew with the square of the nesting depth, which yaml.v3 lets reach 10000: one unset of a 45 KB config.yaml nested 9000 deep allocated 1.7 GB in 2.8 s (finding 2, minor). The check now refuses a file nested more than 64 keys and list items deep, before the edit or after it, without listing its leaves. That unset is refused in 0.2 s, allocating 24 MB. The CHANGELOG entry names the wrapped plain value, the flat key under a flow-style top level, the general refusal and the refusal of a file nested more than 64 levels deep. It says again that unsetting a key that is not set is a successful no-op in every shape, and that it no longer edits a line inside another value that only looks like the key. The flow-style message is built in one place, which brings unsupportedUnsetShape from cognitive complexity 26 to 13. First pass, validated with bazel-test: //internal/config:config_test 495/495, the cmd/bd unset paths 40/40, and TestEmbeddedConfig* in the embedded lane (24 PASS). On the PR head's code 22 of its new rows failed. Of 15 mutants of its code, 14 were killed; the survivor swaps list indexes for a constant marker, which is equivalent. Unsetting every key of the generated config template, filled in, refuses only the mapping-valued keys it refused before. Second pass, validated with bazel-test: //internal/config:config_test 502/502, the cmd/bd unset paths 28/28 (TestConfigUnset*, TestYamlOnlyConfigWithoutDatabase, TestRemoveStoredSecretRow*, TestCheckConfigUnsetSideEffects_*), and TestEmbeddedConfig* with TestIsDoltLocalOnly_FalseAfterUnset in the embedded lane (23/23). On the first pass's code its rows fail 7 times: the five absent-key rows with a line that looks like the key, the user-global no-op, and the reused anchor name. Of 9 mutants of its code, 8 are killed. The survivor drops the column from the anchor's position, which is equivalent: every anchor on a commented line goes with it. End to end, with bd built by Bazel in a scratch workspace: over that notes value, the database-backed `bd config unset jira.url` prints "Unset jira.url (in database)" and the YAML-only `bd config unset routing.mode` prints "routing.mode was not set in config.yaml", both exiting 0 with config.yaml byte-identical; the first pass's binary failed both with "remove the key by hand". Third pass, validated with bazel-test: //internal/config:config_test 509/509, the cmd/bd unset paths 27/27 (TestConfigUnset*, TestYamlOnlyConfigWithoutDatabase, TestRemoveStoredSecretRow*, TestCheckConfigUnsetSideEffects_*), and TestEmbeddedConfig* with TestIsDoltLocalOnly_FalseAfterUnset in the embedded lane (23/23). On the second pass's code 5 of its 7 rows fail; the other two pin an unset that still succeeds above a document that does not parse and beside a value nested 64 deep. All 10 mutants of its code are killed: dropping the decoded documents, holding the key on any decode error, allowing the edit on any error before it, letting a shorter reading after the edit through, refusing the edit because the document that did not parse still does not, skipping the depth check, not counting lists, an off-by-one in the depth count, and moving the limit either way. End to end, with bd built by Bazel in a scratch workspace, above a document that does not parse: over the review's seed, the database-backed `bd config unset jira.url` prints "Unset jira.url (in database)" and exits 0 with config.yaml byte-identical; the YAML-only unset of routing.mode, set beside a line that looks like it, is refused with nothing written; and sync.remote is commented out. Over the depth-9000 file the unset is refused in 0.2 s at 118 MB peak RSS. Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 (1M context) --- CHANGELOG.md | 33 +- internal/config/yaml_config.go | 412 +++++++++++++++--- internal/config/yaml_nested_roundtrip_test.go | 214 ++++++++- internal/config/yaml_unset_newline_test.go | 126 ++++++ 4 files changed, 709 insertions(+), 76 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 232577f92..6b4fb494f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2862,19 +2862,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `sync.remote` silently left the remote live. Keys the write does not own, including dotted ones somebody else put there, are left exactly as they are. - Five things that used to succeed now report an error instead, because each one - produced a value no reader could see, or claimed to remove a value it had - left live. On `bd config set`: a key under a parent that already holds a value - (`sync: enabled`, then `bd config set sync.remote`), and a key in a file whose - top level is not a mapping. On `bd config unset`: a key inside a flow-style - mapping (`sync: {remote: "..."}`), which bd matches by line and so cannot - edit; a key whose value is a block scalar (`remote: |`, in either spelling), - where commenting the key out would leave the body behind as a value of its - own; and a key whose value starts on its line and wraps onto the next (a - `{...}` or `[...]` collection or a quoted string spread over lines, in either - spelling), where commenting the key line out would leave the rest of the value - behind. The same value written on one line still unsets. Unsetting a key that - is not set remains a successful no-op in every shape. + Six things that used to succeed now report an error instead, because each one + produced a value no reader could see, claimed to remove a value it had left + live, or damaged the rest of the file. On `bd config set`: a key under a parent + that already holds a value (`sync: enabled`, then `bd config set sync.remote`), + and a key in a file whose top level is not a mapping. On `bd config unset`: a + key inside a flow-style mapping (`sync: {remote: "..."}`, or a flat + `sync.remote` in a top level written that way), which bd matches by line and + so cannot edit; a key whose value is a block scalar (`remote: |`, in either + spelling), where commenting the key out would leave the body behind as a + value of its own; a key whose value starts on its line and wraps onto the + next (a `{...}` or `[...]` collection, a quoted string or a plain value spread + over lines, in either spelling), where commenting the key line out would + leave the rest of the value behind; and any other unset whose edit would + change more of the file than the key, such as a duplicate of the key that + wraps, the key in a later YAML document, or an anchor on its line that an + alias further down still uses, or that bd cannot check because the file + nests more than 64 levels deep. The same value written on one line still + unsets. Unsetting a key that is not set remains a successful no-op in every + shape, and no longer edits a line inside another value that only looks like + it. - **A self-hosted GitLab no longer re-creates its own issues on every push** ([#6735](https://github.com/gastownhall/beads/issues/6735)). The tracker decided whether a stored `external_ref` was one of its own by looking for diff --git a/internal/config/yaml_config.go b/internal/config/yaml_config.go index d96733c0c..6bd220d8e 100644 --- a/internal/config/yaml_config.go +++ b/internal/config/yaml_config.go @@ -4,10 +4,12 @@ import ( "bufio" "errors" "fmt" + "io" "os" "os/exec" "path/filepath" "regexp" + "slices" "strconv" "strings" @@ -668,6 +670,9 @@ func UnsetUserYamlConfig(key string) (bool, error) { if newContent == normalized { return false, nil } + if err := collateralUnsetRefusal(normalized, newContent, key); err != nil { + return false, err + } // Preserve the owner-private 0600 posture every other user-global writer // uses (SetUserYamlConfig, setYamlConfigAtPath, the metrics bootstrap); @@ -774,6 +779,9 @@ func UnsetYamlConfig(key string) (bool, error) { if newContent == normalized { return false, nil } + if err := collateralUnsetRefusal(normalized, newContent, key); err != nil { + return false, err + } if err := os.WriteFile(configPath, []byte(newContent), 0600); err != nil { //nolint:gosec // configPath is validated return false, fmt.Errorf("failed to write config.yaml: %w", err) @@ -1200,6 +1208,16 @@ func commentOutYamlKey(content, key string) (string, error) { // long-standing behavior. content = strings.ReplaceAll(content, "\r\n", "\n") + // A key no document holds is not in the file, whatever its lines look + // like. The matcher below reads lines, so a line inside a quoted value or + // a list item can match the key. Commenting that line out edits a value + // nobody named, and refusing it fails the unset of a key that is not + // there, which has always been a successful no-op. So the file is left as + // it is, and both unset callers report that nothing changed. + if !yamlHoldsKey(content, key) { + return content, nil + } + if err := unsupportedUnsetShape(content, key); err != nil { return "", err } @@ -1416,26 +1434,24 @@ func (w *nestedKeyWalk) step(name string, indent int) bool { // unsupportedUnsetShape reports the shapes named in the changelog — a key under // a flow-style mapping, a key whose value is a block scalar, and a key whose -// flow collection or quoted string wraps past its own line — in whichever -// spelling the file uses. All three used to be silent: the flow-style one -// reported success while the value stayed live, and the other two had their -// key line commented out while the rest of the value stayed behind, to be -// re-read as something else or to stop the file from parsing. The set -// direction already refuses what it cannot write correctly, so unset says so -// too. +// value wraps past its own line — in whichever spelling the file uses. All +// three used to be silent: the flow-style one reported success while the value +// stayed live, and the other two had their key line commented out while the +// rest of the value stayed behind, to be re-read as something else or to stop +// the file from parsing. The set direction already refuses what it cannot write +// correctly, so unset says so too. // // The first two are named shapes with diagnoses of their own. The third is // decided by where the entry ends rather than by the value's style // (spansLinesUnsetRefusal), so the same value written on one line still -// comments out cleanly. A plain scalar continued onto the next line wraps as -// well and is still silent: nothing yaml.v3 records says where it ends. A -// mapping-valued key is refused by the line matcher itself -// (mappingValueUnsetRefusal), which also covers single-segment keys this -// function never walks. +// comments out cleanly. A mapping-valued key is refused by the line matcher +// itself (mappingValueUnsetRefusal), which also covers single-segment keys this +// function never walks. An edit none of these names is still judged by its +// effect once the line matcher has made it (collateralUnsetRefusal). // -// Only a key that is actually present can be refused: unsetting a key that was -// never there has always been a successful no-op, and staying silent about a -// shape nobody asked to touch is the point. +// This function refuses only a key that is actually present: unsetting a key +// that was never there has always succeeded, and staying silent about a shape +// nobody asked to touch is the point. func unsupportedUnsetShape(content, key string) error { // Single-segment keys are checked too. The database-backed unset clears // the config.yaml layer for every key, so `notes: |` with an indented body @@ -1452,21 +1468,11 @@ func unsupportedUnsetShape(content, key string) error { // The flat spelling first, and by the same order the line matcher uses: it // comments a literal `sync.remote:` line out without ever walking the nested // path, so the body left behind orphans at the TOP level, where it stops the - // whole document from parsing rather than just its own section. - if top := root.Content[0]; top.Kind == yaml.MappingNode { - if idx := findMappingChild(top, key); idx != -1 { - if err := blockScalarUnsetRefusal(top.Content[idx+1], key); err != nil { - return err - } - // The probe holds only for an entry of a block mapping: under a - // flow-style top level the cut can end inside the mapping's own - // braces, which fails whatever the value is. - if top.Style&yaml.FlowStyle == 0 { - if err := spansLinesUnsetRefusal(content, top.Content[idx], key); err != nil { - return err - } - } - } + // whole document from parsing rather than just its own section. A + // single-segment key is its own flat spelling, and the walk below would only + // find the same entry again. + if err := flatKeyUnsetRefusal(content, root.Content[0], key); err != nil || len(segments) == 1 { + return err } node := root.Content[0] @@ -1485,19 +1491,54 @@ func unsupportedUnsetShape(content, key string) error { } for i, parent := range parents { - if parent.Style&yaml.FlowStyle == 0 { - continue + if parent.Style&yaml.FlowStyle != 0 { + return flowStyleUnsetRefusal(key, segments[:i]) } - where := "the top level of this config file" - if i > 0 { - where = fmt.Sprintf("%q", strings.Join(segments[:i], ".")) - } - return fmt.Errorf("cannot unset %q: %s is written in flow style ({...}), which bd cannot edit; remove the key by hand", key, where) } if err := blockScalarUnsetRefusal(node, key); err != nil { return err } - return spansLinesUnsetRefusal(content, keyNode, key) + return spansLinesUnsetRefusal(content, keyNode, node, key) +} + +// flatKeyUnsetRefusal runs unsupportedUnsetShape's checks on the flat spelling +// of key: a top-level entry whose name is the whole key, dots included. +// +// Under a flow-style top level that entry is a key inside a flow-style mapping +// like any other, and the line matcher cannot edit it either. On the opening +// line (`{sync.remote: a, node_id: b}`) it is never matched at all, so the +// value stays live while the unset reports success. On a line of its own it +// can be matched, but that line may close the braces as well, or hold only the +// start of a value that wraps. So it gets the flow-style refusal, as a +// single-segment key there always has. The span probe would not hold here +// anyway: cut inside the mapping's own braces, the document fails to parse +// whatever the value is. +func flatKeyUnsetRefusal(content string, top *yaml.Node, key string) error { + if top.Kind != yaml.MappingNode { + return nil + } + idx := findMappingChild(top, key) + if idx == -1 { + return nil + } + if top.Style&yaml.FlowStyle != 0 { + return flowStyleUnsetRefusal(key, nil) + } + if err := blockScalarUnsetRefusal(top.Content[idx+1], key); err != nil { + return err + } + return spansLinesUnsetRefusal(content, top.Content[idx], top.Content[idx+1], key) +} + +// flowStyleUnsetRefusal refuses a key inside a flow-style mapping, which the +// line matcher cannot edit. parent is the path of that mapping, empty for the +// top level. +func flowStyleUnsetRefusal(key string, parent []string) error { + where := "the top level of this config file" + if len(parent) > 0 { + where = fmt.Sprintf("%q", strings.Join(parent, ".")) + } + return fmt.Errorf("cannot unset %q: %s is written in flow style ({...}), which bd cannot edit; remove the key by hand", key, where) } // blockScalarUnsetRefusal refuses a key whose value is written as a block @@ -1512,38 +1553,297 @@ func blockScalarUnsetRefusal(valueNode *yaml.Node, key string) error { } // spansLinesUnsetRefusal refuses a key of a block mapping whose value starts on -// the key's own line and does not end there: a flow collection or a quoted -// string wrapped onto the next line (`remote: {url: a,` then `branch: b}`). -// The line matcher comments out only the key's line, so the rest of the value -// would stay behind with its opening bracket or quote gone: as a line that -// re-reads as a key nobody set (`branch: b}`), or one that stops the file from -// parsing. +// the key's own line and does not end there: a flow collection, a quoted string +// or a plain scalar wrapped onto the next line (`remote: {url: a,` then +// `branch: b}`). The line matcher comments out only the key's line, so the rest +// of the value would stay behind with its opening bracket or quote gone: as a +// line that re-reads as a key nobody set (`branch: b}`) or as the value of the +// enclosing key (`sync: b`), or one that stops the file from parsing. // // It is decided by where the entry ends, not by the value's style: the same // collection written on one line comments out cleanly. Everything above an // entry of a block mapping is either a finished entry or an enclosing block // mapping, and those end with the document, so the document cut off after the // key's line parses whenever the entry ends on that line, and fails while a -// flow collection or quoted string opened on it is still open. Cutting the -// document rather than parsing the one line on its own, as replaceFlatKeyLine -// does, keeps anchors in scope: `remote: *r` is a whole entry, but its alias -// resolves only against an anchor defined above it. +// flow collection or quoted string opened on it is still open. The cut counts +// lines the way yaml.v3 numbers them (yamlLineEnd), not by "\n" alone: a +// carriage return or a Unicode line separator above the key would otherwise +// move the cut to another line, refusing a value that ends on its own line +// and missing one that wraps. Cutting the document rather than parsing the one +// line on its own, as replaceFlatKeyLine does, keeps anchors in scope: +// `remote: *r` is a whole entry, but its alias resolves only against an anchor +// defined above it. // -// A plain scalar continued onto the next line (`remote: file:///a`, then a -// deeper `b`) is not caught: cut after any of its lines it still parses, as a -// shorter value, and yaml.v3 records where a value starts, not where it ends. -func spansLinesUnsetRefusal(content string, keyNode *yaml.Node, key string) error { - lines := strings.Split(content, "\n") - if keyNode.Line < 1 || keyNode.Line > len(lines) { +// A plain scalar continued onto a deeper line (`remote: file:///a`, then `b`) +// needs one more look, because it parses when cut after any of its lines, as a +// shorter value. The cut holds the key's line and nothing after it, so the +// key's value is the last node in it, and a value that started on the key's +// line and goes on reads shorter there than in the whole file. An alias or a +// collection is not compared: a cut that parses has already closed it. +func spansLinesUnsetRefusal(content string, keyNode, valueNode *yaml.Node, key string) error { + end, ok := yamlLineEnd(content, keyNode.Line) + if !ok { + // Nothing follows the key's line for its value to wrap onto. return nil } - var probe yaml.Node - if yaml.Unmarshal([]byte(strings.Join(lines[:keyNode.Line], "\n")), &probe) == nil { + var cut yaml.Node + if yaml.Unmarshal([]byte(content[:end]), &cut) == nil && !plainScalarGoesOn(&cut, keyNode, valueNode) { return nil } return fmt.Errorf("cannot unset %q: its value spans more than one line, and commenting the key out would leave the rest of it behind; remove the key by hand", key) } +// plainScalarGoesOn reports whether a scalar value that starts on the key's +// line reads differently in cut, the document cut off after that line, than +// in the whole file: a plain scalar continued onto the lines below. +func plainScalarGoesOn(cut, keyNode, valueNode *yaml.Node) bool { + if valueNode.Kind != yaml.ScalarNode || valueNode.Line != keyNode.Line { + return false + } + last := cut + for len(last.Content) > 0 { + last = last.Content[len(last.Content)-1] + } + return last.Value != valueNode.Value +} + +// yamlLineEnd reports the offset of the line break that ends the given 1-based +// line of content, counting breaks as yaml.v3 does when it numbers node lines: +// LF, CR, CRLF as one break, NEL (U+0085) and the Unicode line and paragraph +// separators (U+2028, U+2029). ok is false when no break ends that line, +// because it is the last line or out of range. +func yamlLineEnd(content string, line int) (end int, ok bool) { + breaks := 0 + for i, r := range content { + switch { + case r == '\n' && i > 0 && content[i-1] == '\r': + continue // the LF of a CRLF, already counted at its CR + case r == '\n', r == '\r', r == '\u0085', r == '\u2028', r == '\u2029': + breaks++ + if breaks == line { + return i, true + } + } + } + return 0, false +} + +// collateralUnsetRefusal refuses an unset whose edit changed more of the file +// than the key. Both unset callers run it on commentOutYamlKey's result before +// they write. The refusals before the edit name the shapes they know, judged +// from the first document's parse; this one judges the edit by its effect, so +// it also catches the shapes they cannot see: a later duplicate of the key +// whose value wraps, the key in a later document, an anchor on the key's line +// that an alias further down still uses, or a line inside another value that +// only looks like the key, beside the key itself. A file that does not hold +// the key at all never gets here: commentOutYamlKey leaves it as it is. +// +// Read back, after must hold what before held, document by document, less +// some of the key's own entries in either spelling: nothing added, and nothing +// else removed or changed. Documents are compared by position because bd reads +// only the first one: commenting out all of a first document that no `---` +// opens would move the second one into its place. +// +// A later document that does not parse leaves the ones before it readable, the +// first of them being what bd reads, so those are compared all the same, and +// the edit must leave every one of them parsing. The document that does not +// parse, and any after it, has no reading to compare, before the edit or after +// it. A file whose first document does not parse is left to the line matcher's +// best effort, as it always has been. A file that nests deeper than +// maxYamlLeafDepth is refused: it costs too much to compare. +func collateralUnsetRefusal(before, after, key string) error { + was, wasErr := yamlDocuments(before) + if wasErr != nil && len(was) == 0 { + return nil + } + is, isErr := yamlDocuments(after) + if wasErr != nil && len(is) >= len(was) { + is, isErr = is[:len(was)], nil + } + wasLeaves, ok := yamlLeaves(was) + if !ok { + return fmt.Errorf("cannot unset %q: this file nests more than %d levels deep, too deep for bd to check what else commenting out the key would change; remove the key by hand", key, maxYamlLeafDepth) + } + isLeaves, ok := yamlLeaves(is) + if ok && isErr == nil && onlyKeyLeavesRemoved(wasLeaves, isLeaves, key) { + return nil + } + return fmt.Errorf("cannot unset %q: commenting out the lines that match it would change more of this file than the key itself; remove the key by hand", key) +} + +// yamlLeaf is one scalar of a YAML stream: the document it is in, the keys and +// list indexes that lead to it, and its value. An alias is a leaf of its own, +// naming the anchor it resolves to rather than expanding it, so the list never +// grows past the size of the file, however the aliases nest. +type yamlLeaf struct { + doc int + path []string + value string +} + +func (l yamlLeaf) id() string { + return fmt.Sprintf("%d %q %q", l.doc, l.path, l.value) +} + +// maxYamlLeafDepth is how many keys and list items deep yamlLeaves reads. A +// leaf carries the whole path to it, so listing a document's leaves costs +// their number times their depth, and yaml.v3 lets a document nest 10000 +// deep: one unset of a 45 KB config.yaml nested 9000 deep allocated 1.7 GB. +// No config bd reads comes near this depth. +const maxYamlLeafDepth = 64 + +// yamlLeaves lists the leaves of docs. A null scalar or an empty collection +// holds no leaf, so a section left holding nothing reads the same as one that +// is gone. ok is false, and no leaves come back, when a document nests more +// than maxYamlLeafDepth keys and list items deep. +func yamlLeaves(docs []*yaml.Node) (leaves []yamlLeaf, ok bool) { + for doc, root := range docs { + if yamlNestsDeeper(root, maxYamlLeafDepth) { + return nil, false + } + leaves = appendYamlLeaves(leaves, doc, nil, root) + } + return leaves, true +} + +// yamlNestsDeeper reports whether some node under n has more than depth +// mappings and sequences above it, counting n. Under a document, those are the +// keys and list items on the node's path. It looks no deeper than that. +func yamlNestsDeeper(n *yaml.Node, depth int) bool { + if n.Kind == yaml.MappingNode || n.Kind == yaml.SequenceNode { + depth-- + } + for _, child := range n.Content { + if depth < 0 || yamlNestsDeeper(child, depth) { + return true + } + } + return false +} + +// yamlDocuments decodes the documents of content, in order. When one does not +// parse, the documents decoded before it come back with the error. +func yamlDocuments(content string) ([]*yaml.Node, error) { + dec := yaml.NewDecoder(strings.NewReader(content)) + var docs []*yaml.Node + for { + var root yaml.Node + err := dec.Decode(&root) + if errors.Is(err, io.EOF) { + return docs, nil + } + if err != nil { + return docs, err + } + docs = append(docs, &root) + } +} + +// appendYamlLeaves appends the leaves of n, which path leads to, to leaves. +func appendYamlLeaves(leaves []yamlLeaf, doc int, path []string, n *yaml.Node) []yamlLeaf { + switch n.Kind { + case yaml.DocumentNode: + for _, child := range n.Content { + leaves = appendYamlLeaves(leaves, doc, path, child) + } + case yaml.MappingNode: + for i := 0; i+1 < len(n.Content); i += 2 { + leaves = appendYamlLeaves(leaves, doc, append(path[:len(path):len(path)], n.Content[i].Value), n.Content[i+1]) + } + case yaml.SequenceNode: + for i, item := range n.Content { + leaves = appendYamlLeaves(leaves, doc, append(path[:len(path):len(path)], strconv.Itoa(i)), item) + } + case yaml.AliasNode: + // By where its anchor is, not by name alone: YAML lets a later anchor + // reuse a name, and with the later one's line commented out the alias + // would resolve to the earlier one, reading the same while its value + // changed. Commenting a line out moves no other node, so an alias + // whose anchor is untouched reads the same. + leaves = append(leaves, yamlLeaf{doc: doc, path: path, value: fmt.Sprintf("*%s@%d:%d", n.Value, n.Alias.Line, n.Alias.Column)}) + case yaml.ScalarNode: + if n.ShortTag() != "!!null" { + leaves = append(leaves, yamlLeaf{doc: doc, path: path, value: n.Value}) + } + } + return leaves +} + +// onlyKeyLeavesRemoved reports whether is holds exactly was's leaves less some +// that belong to key: under the top-level entry named key, dots and all, or +// under its nested path, the two spellings the line matcher comments out. +func onlyKeyLeavesRemoved(was, is []yamlLeaf, key string) bool { + left := make(map[string]int, len(was)) + for _, leaf := range was { + left[leaf.id()]++ + } + for _, leaf := range is { + id := leaf.id() + if left[id] == 0 { + return false + } + left[id]-- + } + segments := strings.Split(key, ".") + for _, leaf := range was { + p := leaf.path + ofKey := (len(p) > 0 && p[0] == key) || (len(p) >= len(segments) && slices.Equal(p[:len(segments)], segments)) + if left[leaf.id()] > 0 && !ofKey { + return false + } + } + return true +} + +// yamlHoldsKey reports whether some document of content has an entry for key +// in either spelling the line matcher comments out: a top-level entry named +// key, dots and all, or its nested path. An entry whose value is null holds +// the key too, and so does one under either copy of a section the file writes +// twice, since the matcher comments out both. +// +// When a later document does not parse, the documents before it answer: bd +// reads the first of them, and an edit to them is checked like any other +// (collateralUnsetRefusal). The document that does not parse, and any after +// it, is read by nobody, so a key there is not set. A file whose first +// document does not parse gives no answer, so it is taken to hold the key, +// and the line matcher keeps its best effort there, as it always has. +func yamlHoldsKey(content, key string) bool { + docs, err := yamlDocuments(content) + if err != nil && len(docs) == 0 { + return true + } + segments := strings.Split(key, ".") + for _, doc := range docs { + for _, top := range doc.Content { + if mappingHoldsPath(top, []string{key}) || mappingHoldsPath(top, segments) { + return true + } + } + } + return false +} + +// mappingHoldsPath reports whether n leads down path through mappings, one key +// per step. Every entry of a name that repeats is followed. +func mappingHoldsPath(n *yaml.Node, path []string) bool { + level := []*yaml.Node{n} + for _, name := range path { + var next []*yaml.Node + for _, m := range level { + if m.Kind != yaml.MappingNode { + continue + } + for i := 0; i+1 < len(m.Content); i += 2 { + if k := m.Content[i]; k.Kind == yaml.ScalarNode && k.Value == name { + next = append(next, m.Content[i+1]) + } + } + } + level = next + } + return len(level) > 0 +} + // blockScalarIndent reports the indentation of a key whose value is a literal // or folded block scalar (`notes: |`), or -1 when the line opens no block. func blockScalarIndent(line string) int { diff --git a/internal/config/yaml_nested_roundtrip_test.go b/internal/config/yaml_nested_roundtrip_test.go index f81fc1ba8..91ba0b5b2 100644 --- a/internal/config/yaml_nested_roundtrip_test.go +++ b/internal/config/yaml_nested_roundtrip_test.go @@ -439,6 +439,81 @@ func TestUnsetRemovesADottedKeyInEveryShape(t *testing.T) { key: "backup.remotes", survivors: map[string]string{"sync.remote": "file:///a"}, }, + { + // yaml.v3 ends a line at a Unicode line separator or a lone carriage + // return as well as at LF, even inside a quoted string, so either + // one above the key numbers its line one past the line a "\n" split + // finds it on. Cut by "\n", the probe ended inside the collection + // below and refused a value that ends on its own line. + name: "one-line value below a line separator", + seed: "notes: \"a\u2028b\"\nremote: x\nother: {a: 1,\n b: 2}\n", + key: "remote", + survivors: map[string]string{"other.b": "2"}, + }, + { + name: "one-line value below a lone carriage return", + seed: "notes: \"a\rb\"\nremote: x\nother: {a: 1,\n b: 2}\n", + key: "remote", + survivors: map[string]string{"other.b": "2"}, + }, + { + // With no newline after it, the key's line is the last one, which a + // "\n" split counts one short of yaml.v3: nothing follows it for the + // value to wrap onto. + name: "last line with no newline, below a line separator", + seed: "notes: \"a\u2028b\"\nremote: x", + key: "remote", + survivors: map[string]string{"notes": "a\u2028b"}, + }, + { + // A flat key under a flow-style top level is refused, but only when + // it is there: an unset with nothing to remove still succeeds. + name: "absent from a flow-style top level", + seed: "{node_id: mini}\n", + key: "sync.remote", + survivors: map[string]string{"node_id": "mini"}, + }, + { + // A plain scalar that ends on its line reads the same cut after it + // as in the whole file, and a comment below it is not a + // continuation, however deep. + name: "plain scalar with a deeper comment below it", + seed: "sync:\n remote: file:///a\n # the shared remote\n branch: b\n", + key: "sync.remote", + survivors: map[string]string{"sync.branch": "b"}, + }, + { + // Later documents are checked as well, but an edit to the first one + // leaves them as they were. + name: "key in the first of two documents", + seed: "sync:\n remote: x\nnode_id: mini\n---\nother: y\n", + key: "sync.remote", + survivors: map[string]string{"node_id": "mini"}, + }, + { + // Opened by its own ---, the first document stays a document, if an + // empty one, after its only key is commented out, so the second + // document does not move up into its place. + name: "only key of a first document that --- opens", + seed: "---\nremote: x\n---\nnode_id: mini\n", + key: "remote", + }, + { + // A later document that does not parse leaves the first one + // readable, and the edit there is checked and made all the same. + name: "key above a document that does not parse", + seed: "sync:\n remote: x\nnode_id: mini\n---\nother: [unclosed\n", + key: "sync.remote", + survivors: map[string]string{"node_id": "mini"}, + }, + { + // The check of the edit reads 64 keys and list items deep, which is + // as deep as this sibling goes. + name: "beside a value nested 64 deep", + seed: "sync:\n remote: x\nnode_id: mini\ndeep: " + strings.Repeat("[", 63) + "x" + strings.Repeat("]", 63) + "\n", + key: "sync.remote", + survivors: map[string]string{"node_id": "mini"}, + }, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { @@ -508,6 +583,17 @@ func TestUnsetRefusesShapesItCannotEdit(t *testing.T) { // style. The key's own value ends on its line; only the mapping // around it goes on. {name: "flow style top level spread over lines", seed: "{notes: x,\n node_id: mini}\n", key: "notes", want: "flow style"}, + // A flat key is a key of the top level, so a flow-style top level holds + // it in a flow-style mapping too. The line matcher never finds it on + // the opening line, takes the closing brace with it, or leaves the rest + // of a wrapped value behind, so it gets the same refusal, even for a + // value on a line of its own. + {name: "flat key on the opening line of a flow-style top level", seed: "{sync.remote: a, node_id: b}\n", key: "sync.remote", want: "the top level of this config file is written in flow style"}, + {name: "flat key wrapped from the opening line of a flow-style top level", seed: "{sync.remote: {url: a,\n branch: b}}\n", key: "sync.remote", want: "the top level of this config file is written in flow style"}, + {name: "flat key whose flow value wraps in a flow-style top level", seed: "{\n sync.remote: {url: a,\n branch: b},\n node_id: mini\n}\n", key: "sync.remote", want: "the top level of this config file is written in flow style"}, + {name: "flat key whose quoted value wraps in a flow-style top level", seed: "{\n sync.remote: \"file:///a\n /b\",\n node_id: mini\n}\n", key: "sync.remote", want: "the top level of this config file is written in flow style"}, + {name: "flat key on the line that closes a flow-style top level", seed: "{\n node_id: mini,\n sync.remote: x}\n", key: "sync.remote", want: "the top level of this config file is written in flow style"}, + {name: "flat key on a line of its own in a flow-style top level", seed: "{\n sync.remote: file:///a,\n node_id: mini\n}\n", key: "sync.remote", want: "the top level of this config file is written in flow style"}, {name: "literal block scalar", seed: "sync:\n remote: |\n line1\n line2\n", key: "sync.remote", want: "block scalar"}, {name: "folded block scalar", seed: "sync:\n remote: >\n line1\n", key: "sync.remote", want: "block scalar"}, // The flat spelling reaches the same body-left-behind outcome by the @@ -563,6 +649,61 @@ func TestUnsetRefusesShapesItCannotEdit(t *testing.T) { // The probe reads the file after CRLF is normalized to LF, and a // refusal writes nothing, so a CRLF file keeps its line endings too. {name: "flow mapping wrapped in a CRLF file", seed: "sync:\r\n remote: {url: a,\r\n branch: b}\r\nnode_id: mini\r\n", key: "sync.remote", want: "spans more than one line"}, + // yaml.v3 ends a line at a Unicode line separator or a lone carriage + // return too, even inside a quoted string. Above the key, either one + // numbers the key's line one past the line a "\n" split finds it on, + // so a probe cut by "\n" took in the whole wrapped value and let it + // through. + {name: "wrapped value below a line separator", seed: "notes: \"a\u2028b\"\nsync:\n remote: {url: a,\n branch: b}\nnode_id: mini\n", key: "sync.remote", want: "spans more than one line"}, + {name: "wrapped value below a lone carriage return", seed: "notes: \"a\rb\"\nsync:\n remote: {url: a,\n branch: b}\nnode_id: mini\n", key: "sync.remote", want: "spans more than one line"}, + // In the key's own value, the separator is a wrap: the commented-out + // line ends at it, and `b"` is left behind to stop the file from + // parsing. + {name: "quoted value holding a line separator", seed: "remote: \"a\u2028b\"\nnode_id: mini\n", key: "remote", want: "spans more than one line"}, + // A plain scalar continued onto a deeper line wraps too. Cut after the + // key's line it still parses, as a shorter value, so the probe compares + // the two. Left behind, the rest re-reads as the value of the enclosing + // key (`sync: /b`), or stops the file from parsing. + {name: "plain scalar continued onto a deeper line", seed: "sync:\n remote: file:///a\n /b\nnode_id: mini\n", key: "sync.remote", want: "spans more than one line"}, + {name: "plain scalar continued past a blank line", seed: "sync:\n remote: file:///a\n\n /b\nnode_id: mini\n", key: "sync.remote", want: "spans more than one line"}, + {name: "plain scalar continued under the flat spelling", seed: "dolt.host: 10.0.0.1\n more\nnode_id: mini\n", key: "dolt.host", want: "spans more than one line"}, + {name: "single-segment plain scalar continued", seed: "notes: a long note that the\n editor wrapped\nnode_id: mini\n", key: "notes", want: "spans more than one line"}, + // The refusals above judge the first document's parse before anything + // is edited. What gets past them is refused by what the edit would do: + // the file read back has to say what it said before, less the key. + // The probe sees the first of two duplicates, and the line matcher + // comments out both, leaving `line two"` behind. + {name: "later duplicate whose value wraps", seed: "notes: ok\nnotes: \"line one\n line two\"\nnode_id: mini\n", key: "notes", want: "more of this file than the key"}, + // A section the file writes twice holds the key in its second copy, + // which the refusals before the edit, reading the first, never see. + {name: "wrapped value under the second copy of its section", seed: "sync:\n branch: b\nsync:\n remote: \"x\n y\"\nnode_id: mini\n", key: "sync.remote", want: "more of this file than the key"}, + // A later document is not in the first document's parse at all. + {name: "wrapped value in a later document", seed: "node_id: mini\n---\nnotes: \"line one\n line two\"\n", key: "notes", want: "more of this file than the key"}, + // The anchor goes with the key's line, and the alias below it then + // names nothing, so the file stops parsing. + {name: "anchor on the key's line used further down", seed: "sync:\n remote: &r file:///a\nbackup:\n remote: *r\n", key: "sync.remote", want: "more of this file than the key"}, + // YAML lets a later anchor reuse a name. With the key's line commented + // out, the alias below resolves to the anchor above it instead, so the + // file still parses, but backup.remote reads keep rather than a. + {name: "anchor on the key's line reusing the name of one above", seed: "x: &r keep\nsync:\n remote: &r a\nbackup:\n remote: *r\n", key: "sync.remote", want: "more of this file than the key"}, + // A line that only looks like the key is someone else's even when the + // key is set too: the line matcher comments out both lines, and notes + // would change. TestUnsetOfAnAbsentKeyStillSucceeds has the same line + // in a file that does not hold the key. + {name: "the key beside a line inside another value that looks like it", seed: "notes: \"see\njira.url: https://x\"\njira.url: https://y\n", key: "jira.url", want: "more of this file than the key"}, + // bd reads only the first document. With its only key commented out, + // that document is gone, and node_id would be read in its place. + {name: "only key of the first document", seed: "remote: x\n---\nnode_id: mini\n", key: "remote", want: "more of this file than the key"}, + // A later document that does not parse leaves the ones before it + // readable, so an edit to them is checked as in any other file. Here + // notes would change, and with the first document gone, the one that + // does not parse would be read in its place. + {name: "the key beside a line that looks like it, above a document that does not parse", seed: "notes: \"see\njira.url: https://x\"\njira.url: https://y\n---\nother: [unclosed\n", key: "jira.url", want: "more of this file than the key"}, + {name: "only key of the first document, above one that does not parse", seed: "remote: x\n---\nother: [unclosed\n", key: "remote", want: "more of this file than the key"}, + // Each leaf the check compares carries its whole path, so a file + // nested past 64 keys and list items deep costs too much to compare, + // and yaml.v3 lets one nest 10000 deep. + {name: "beside a value nested 65 deep", seed: "sync:\n remote: x\ndeep: " + strings.Repeat("[", 64) + "x" + strings.Repeat("]", 64) + "\n", key: "sync.remote", want: "nests more than 64 levels deep"}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { @@ -595,11 +736,70 @@ func TestUnsetRefusesShapesItCannotEdit(t *testing.T) { // A shape bd cannot edit is only worth refusing when the key is actually there. // Unsetting a key that was never set has always been a successful no-op, and a -// flow-style mapping elsewhere in the file is not the operator's problem. +// flow-style mapping elsewhere in the file is not the operator's problem. Nor +// is a line that only looks like the key: the line matcher reads lines, and +// commenting such a line out edits a value nobody named, while refusing it +// fails an unset with nothing to remove. Either way the file stays as it was. func TestUnsetOfAnAbsentKeyStillSucceeds(t *testing.T) { + cases := []struct { + name string + seed string + key string + }{ + {name: "flow-style mapping elsewhere", seed: "sync: {branch: beads-sync}\ndolt:\n port: 3307\n", key: "sync.remote"}, + // The matching line is the second line of notes, in either spelling. + {name: "line inside another value that looks like the key", seed: "sync:\n notes: \"x\n remote: y\"\n", key: "sync.remote"}, + {name: "line inside another value that looks like the flat key", seed: "notes: \"see\njira.url: https://x\"\n", key: "jira.url"}, + // sync is a list, so sync.remote is not set, but the nested walk + // matches the key of its item. An alias there is a value like any + // other, though it is not expanded. The matcher refused the same key + // with a mapping beneath it as a value that goes on below, and + // commented it out with no value at all, which left nothing to compare. + {name: "key inside a list item", seed: "base: &r file:///a\nsync:\n -\n remote: *r\n", key: "sync.remote"}, + {name: "key with a mapping beneath it inside a list item", seed: "sync:\n -\n remote:\n url: a\n", key: "sync.remote"}, + {name: "key with no value inside a list item", seed: "sync:\n -\n remote:\n", key: "sync.remote"}, + // A later document that does not parse leaves the first one readable, + // and that one does not hold the key either. + {name: "line that looks like the key, above a document that does not parse", seed: "notes: \"see\njira.url: https://x\"\n---\nother: [unclosed\n", key: "jira.url"}, + // Nobody reads a document that does not parse, so a key there is not + // set. + {name: "key only in a document that does not parse", seed: "node_id: mini\n---\nsync:\n remote: x\nother: [unclosed\n", key: "sync.remote"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "config.yaml") + if err := os.WriteFile(path, []byte(tc.seed), 0o600); err != nil { + t.Fatalf("seed: %v", err) + } + + t.Setenv("BEADS_DIR", dir) + changed, err := UnsetYamlConfig(tc.key) + if err != nil { + t.Fatalf("UnsetYamlConfig on an unset key: %v", err) + } + if changed { + t.Error("UnsetYamlConfig on an unset key reported a write") + } + body, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read: %v", err) + } + if string(body) != tc.seed { + t.Errorf("the no-op unset changed the file:\n%s", body) + } + }) + } +} + +// A file whose first document yaml.v3 cannot parse has no reading to check an +// edit against, so the line matcher's best effort stands there, as it always +// has: the key's line is commented out and the rest of the file is left as it +// was. +func TestUnsetInAFileThatDoesNotParseStillCommentsTheKeyOut(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "config.yaml") - seed := "sync: {branch: beads-sync}\ndolt:\n port: 3307\n" + seed := "sync:\n remote: x\nother: [unclosed\n" if err := os.WriteFile(path, []byte(seed), 0o600); err != nil { t.Fatalf("seed: %v", err) } @@ -607,17 +807,17 @@ func TestUnsetOfAnAbsentKeyStillSucceeds(t *testing.T) { t.Setenv("BEADS_DIR", dir) changed, err := UnsetYamlConfig("sync.remote") if err != nil { - t.Fatalf("UnsetYamlConfig on an unset key: %v", err) + t.Fatalf("UnsetYamlConfig in a file that does not parse: %v", err) } - if changed { - t.Error("UnsetYamlConfig on an unset key reported a write") + if !changed { + t.Error("UnsetYamlConfig reported no write for a key it commented out") } body, err := os.ReadFile(path) if err != nil { t.Fatalf("read: %v", err) } - if got := GetStringFromDir(dir, "sync.branch"); got != "beads-sync" { - t.Errorf("the no-op unset changed sync.branch to %q:\n%s", got, body) + if want := "sync:\n # remote: x\nother: [unclosed\n"; string(body) != want { + t.Errorf("the unset left\n%q\nwant\n%q", body, want) } } diff --git a/internal/config/yaml_unset_newline_test.go b/internal/config/yaml_unset_newline_test.go index a3b5569bd..09ac65195 100644 --- a/internal/config/yaml_unset_newline_test.go +++ b/internal/config/yaml_unset_newline_test.go @@ -1,10 +1,13 @@ package config import ( + "fmt" "os" "path/filepath" "strings" "testing" + + "gopkg.in/yaml.v3" ) // Neither an unset nor a set may also rewrite the end of the file. @@ -295,6 +298,81 @@ func TestUnsetUserYamlConfigRefusesAValueThatSpansLines(t *testing.T) { } } +// The user-global unset checks the edit by its effect too, before it writes. +// node_id is not in the first document, so no refusal before the edit looks +// at it, but the line matcher still finds it in the second, where commenting +// out its first line would leave `box"` behind. +func TestUnsetUserYamlConfigRefusesAnEditThatChangesMoreThanTheKey(t *testing.T) { + const body = "metrics:\n disabled: true\n---\nnode_id: \"mini\n box\"\n" + + t.Setenv("HOME", t.TempDir()) + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + path, err := UserConfigYamlPath() + if err != nil { + t.Fatalf("UserConfigYamlPath: %v", err) + } + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte(body), 0o600); err != nil { + t.Fatal(err) + } + + changed, err := UnsetUserYamlConfig("node_id") + if err == nil { + after, _ := os.ReadFile(path) //nolint:gosec,errcheck // test-owned temp path; diagnostic + t.Fatalf("UnsetUserYamlConfig reported success on an edit that breaks the second document:\n%q", string(after)) + } + if !strings.Contains(err.Error(), "node_id") || !strings.Contains(err.Error(), "more of this file than the key") { + t.Errorf("the error names neither the key nor the reason: %v", err) + } + if changed { + t.Error("UnsetUserYamlConfig() changed = true for an unset it refused") + } + after, err := os.ReadFile(path) //nolint:gosec // test-owned temp path + if err != nil { + t.Fatal(err) + } + if string(after) != body { + t.Errorf("a refused unset still changed the file:\ngot: %q\nwant: %q", string(after), body) + } +} + +// The user-global unset of a key the file does not hold is the same no-op as +// the project one, though a line inside another value looks like the key: +// (false, nil) and the file as it was, not a refusal to edit notes. +func TestUnsetUserYamlConfigOfAnAbsentKeyLeavesTheFileAlone(t *testing.T) { + const body = "metrics:\n disabled: true\nnotes: \"see\nnode_id: mini\"\n" + + t.Setenv("HOME", t.TempDir()) + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + path, err := UserConfigYamlPath() + if err != nil { + t.Fatalf("UserConfigYamlPath: %v", err) + } + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte(body), 0o600); err != nil { + t.Fatal(err) + } + + changed, err := UnsetUserYamlConfig("node_id") + if err != nil { + t.Fatalf("UnsetUserYamlConfig on a key the file does not hold: %v", err) + } + if changed { + t.Error("UnsetUserYamlConfig() changed = true for a key the file does not hold") + } + after, err := os.ReadFile(path) //nolint:gosec // test-owned temp path + if err != nil { + t.Fatal(err) + } + if string(after) != body { + t.Errorf("the no-op unset changed the file:\ngot: %q\nwant: %q", string(after), body) + } +} + // The set twin of TestCommentOutYamlKeyPreservesTheTrailingNewline. // updateYamlKey's flat-key path read the document with bufio.Scanner and joined // the lines back with "\n" — the shape commentOutYamlKey used to have — so it @@ -508,3 +586,51 @@ func TestUpdateYamlKeyLeavesTheNestedPathAlone(t *testing.T) { }) } } + +// The span refusal cuts the document after the key's line as yaml.v3 numbers +// it, and yaml.v3 ends a line at more than LF: at a CR, at a CRLF (once), at a +// NEL, and at the Unicode line and paragraph separators. Counting any of them +// differently moves the cut onto another line, which refuses a value that ends +// on its own line or misses one that wraps. An unset normalizes CRLF before the +// probe runs, so a CRLF reaches the count only as what is left of a CR CR LF; +// the count has to get it right all the same. Each break is also tried as the +// file's first byte, a blank first line, where the look back for a CR before +// an LF has nothing to look at. +func TestYamlLineEndCutsWhereYamlV3EndsALine(t *testing.T) { + lineBreaks := map[string]string{ + "LF": "\n", "CR": "\r", "CRLF": "\r\n", "CR CRLF": "\r\r\n", + "NEL": "\u0085", "LS": "\u2028", "PS": "\u2029", + } + for name, lineBreak := range lineBreaks { + for _, lead := range []string{"", lineBreak} { + t.Run(fmt.Sprintf("%s lead %q", name, lead), func(t *testing.T) { + testYamlLineEndCuts(t, lead+"a: 1"+lineBreak+"b: 2"+lineBreak+"c: 3") + }) + } + } + // Out of range, before line 1 or past the last line, there is no cut. + for _, line := range []int{0, 4} { + if end, ok := yamlLineEnd("a: 1\nb: 2\nc: 3\n", line); ok { + t.Errorf("yamlLineEnd(line %d of 3) = %d, true; want ok = false", line, end) + } + } +} + +func testYamlLineEndCuts(t *testing.T, content string) { + t.Helper() + var root yaml.Node + if err := yaml.Unmarshal([]byte(content), &root); err != nil { + t.Fatalf("parse %q: %v", content, err) + } + top := root.Content[0] + for i := 0; i < len(top.Content); i += 2 { + key, value := top.Content[i], top.Content[i+1] + end, ok := yamlLineEnd(content, key.Line) + if last := i == len(top.Content)-2; ok == last { + t.Fatalf("yamlLineEnd(%q, %d) ok = %v for key %s", content, key.Line, ok, key.Value) + } + if entry := key.Value + ": " + value.Value; ok && !strings.HasSuffix(content[:end], entry) { + t.Errorf("the cut after line %d (key %s) is %q, want it to end with %q", key.Line, key.Value, content[:end], entry) + } + } +}