diff --git a/CHANGELOG.md b/CHANGELOG.md index 182c09f9c..2de340015 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -557,6 +557,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- **A bare hash that two prefixes share is reported as ambiguous instead of + resolving to one of them.** With `hacker-news-abc123` and `ops-abc123` in + one workspace (under `allowed_prefixes`, say), `abc123` matched both + exactly, and the resolver kept whichever the store listed last: + `bd update abc123 --status in_progress` updated `ops-abc123` and printed a + success line, `bd comment abc123` wrote to it, and `bd delete abc123` + previewed it and printed `bd delete ops-abc123 --force` as the next step. + An exact hash match is not an abbreviation, so the partial-id notice stayed + silent too. Every command that resolves an id through the shared resolver + now refuses such a hash with `ambiguous issue ID: "abc123" matches 2 + issues: [hacker-news-abc123 ops-abc123]` and asks for the full id, as + [Working with IDs](docs/core-concepts/hash-ids.md#working-with-ids) + documents for ambiguous cases; ephemeral wisps follow the same rule. A hash + that only one prefix uses, a full id, and the workspace's own prefix + (`bd-abc123` in a `bd` workspace, still looked up first) resolve as before. + A connected HTTP workspace resolves exact ids only and was not affected. + - **`bd gate check` works over a connected HTTP workspace.** Its open-gate listing built the same raw `SearchIssues` filter `bd gate list` did before S20, so the HTTP backend refused the command before reading a gate and gc's diff --git a/internal/utils/id_parser.go b/internal/utils/id_parser.go index 8c42c0c95..86a41e68e 100644 --- a/internal/utils/id_parser.go +++ b/internal/utils/id_parser.go @@ -101,6 +101,7 @@ func parseIssueID(input string, prefix string) string { // - The input is a bare tooling sentinel ("", "null", "undefined", "none", "nil") // - No issue found matching the ID // - Multiple issues match (ambiguous prefix) +// - Multiple issues share the input's hash under different prefixes func ResolvePartialID(ctx context.Context, store PartialIDResolverStore, input string) (string, error) { return resolvePartialID(ctx, store, input, true) } @@ -232,6 +233,9 @@ func resolvePartialID(ctx context.Context, store PartialIDResolverStore, input s var matches []string var exactMatch string + // hashMatches collects every candidate whose hash equals the input's, so + // a hash that two prefixes share is reported, not picked (soleHashMatch). + var hashMatches []string // abbrevOnly collects candidates that matched ONLY via leading-prefix // abbreviation while allowAbbrev is false — never resolved to, only used // to make the final "not found" error truthful (see ErrAbbreviatedIDNotAllowed). @@ -256,7 +260,7 @@ func resolvePartialID(ctx context.Context, store PartialIDResolverStore, input s // Check for exact hash match (excluding hierarchical children) if issueHash == hashPart { - exactMatch = id + hashMatches = append(hashMatches, id) // Don't break - keep searching in case there's a full ID match } else if strings.HasPrefix(issueHash, hashPart) { // Leading-prefix abbreviation (documented UX, e.g. "a3f8" -> "a3f8e9..."). @@ -277,6 +281,9 @@ func resolvePartialID(ctx context.Context, store PartialIDResolverStore, input s if exactMatch != "" { return exactMatch, nil } + if len(hashMatches) > 0 { + return soleHashMatch(input, hashMatches) + } // Fallback: explicitly search wisps table for partial ID resolution. // DoltStore.SearchIssues merges wisps when Ephemeral is nil, but @@ -302,7 +309,7 @@ func resolvePartialID(ctx context.Context, store PartialIDResolverStore, input s // against the isolated hash, not the full "wisp-t3st" string. wispHash := strings.TrimPrefix(wHash, "wisp-") if wHash == hashPart || wispHash == hashPart { - exactMatch = wID + hashMatches = append(hashMatches, wID) } else if strings.HasPrefix(wispHash, hashPart) { if allowAbbrev { matches = append(matches, wID) @@ -311,8 +318,8 @@ func resolvePartialID(ctx context.Context, store PartialIDResolverStore, input s } } } - if exactMatch != "" { - return exactMatch, nil + if len(hashMatches) > 0 { + return soleHashMatch(input, hashMatches) } } } @@ -350,6 +357,19 @@ func resolvePartialID(ctx context.Context, store PartialIDResolverStore, input s return resolved, nil } +// soleHashMatch resolves to the one candidate whose hash equals the input's. +// Two prefixes can share a hash (hacker-news-abc123 and ops-abc123), and the +// store's return order is not a contract, so with more than one candidate the +// input is ambiguous and nothing is picked. Typing more characters cannot tell +// such IDs apart; only the full ID can. +func soleHashMatch(input string, candidates []string) (string, error) { + if len(candidates) == 1 { + return candidates[0], nil + } + sort.Strings(candidates) + return "", fmt.Errorf("%w: %q matches %d issues: %v\nUse the full ID to disambiguate", ErrAmbiguousID, input, len(candidates), candidates) +} + // resolveExactIDOnly is resolvePartialID for an ExactIDLookupStore: the exact // lookups only, with the same input normalization, and a not-found that says // why no abbreviation was tried. diff --git a/internal/utils/id_parser_fake_test.go b/internal/utils/id_parser_fake_test.go index eea8e2f5c..7d387fe47 100644 --- a/internal/utils/id_parser_fake_test.go +++ b/internal/utils/id_parser_fake_test.go @@ -394,3 +394,89 @@ func TestResolvePartialID_FakeStore_QualifiedInputsNeverResolveToAnotherBead(t * }) } } + +// TestResolvePartialID_FakeStore_SharedHashAcrossPrefixesIsAmbiguous pins +// mc-ke8mg. When two prefixes share a hash and neither is the configured +// prefix, the bare hash names both issues equally well. The resolver used to +// keep whichever one the store returned last, silently, and without the +// partial-ID notice, because the match counts as exact. So the store's return +// order picked the issue a command acted on. Both resolvers must refuse with +// ErrAmbiguousID and name every candidate, whatever order the store answers in. +func TestResolvePartialID_FakeStore_SharedHashAcrossPrefixesIsAmbiguous(t *testing.T) { + ctx := context.Background() + config := map[string]string{"issue_prefix": "bd", "allowed_prefixes": "hacker-news,ops"} + resolvers := []struct { + name string + resolve func(context.Context, utils.PartialIDResolverStore, string) (string, error) + }{ + {"ResolvePartialID", utils.ResolvePartialID}, + {"ResolvePartialIDExact", utils.ResolvePartialIDExact}, + } + + cases := []struct { + name string + a, b fakeIssue + input string + wantErr string + }{ + { + name: "issues", + a: fakeIssue{id: "hacker-news-abc123"}, + b: fakeIssue{id: "ops-abc123"}, + input: "abc123", + wantErr: "ambiguous issue ID: \"abc123\" matches 2 issues: [hacker-news-abc123 ops-abc123]\nUse the full ID to disambiguate", + }, + { + // The wisp fallback strips the "wisp-" infix before it compares + // hashes, so it reaches the same choice on its own. + name: "wisps", + a: fakeIssue{id: "hacker-news-wisp-t3st", ephemeral: true}, + b: fakeIssue{id: "ops-wisp-t3st", ephemeral: true}, + input: "t3st", + wantErr: "ambiguous issue ID: \"t3st\" matches 2 issues: [hacker-news-wisp-t3st ops-wisp-t3st]\nUse the full ID to disambiguate", + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + // Try both store orders, since the order is what used to decide. + for _, issues := range [][]fakeIssue{{tc.a, tc.b}, {tc.b, tc.a}} { + store := &fakeResolverStore{issues: issues, config: config} + for _, r := range resolvers { + got, err := r.resolve(ctx, store, tc.input) + if !errors.Is(err, utils.ErrAmbiguousID) { + t.Errorf("%s(%q) with %s first = (%q, %v); want an ErrAmbiguousID error", r.name, tc.input, issues[0].id, got, err) + } else if err.Error() != tc.wantErr { + t.Errorf("%s(%q) error = %q; want %q", r.name, tc.input, err, tc.wantErr) + } + } + } + + // A full ID still names exactly one issue. A hash that only one + // prefix uses is still an exact match. + for _, r := range resolvers { + store := &fakeResolverStore{issues: []fakeIssue{tc.a, tc.b}, config: config} + if got, err := r.resolve(ctx, store, tc.b.id); err != nil || got != tc.b.id { + t.Errorf("%s(%q) = (%q, %v); want (%q, nil)", r.name, tc.b.id, got, err, tc.b.id) + } + store = &fakeResolverStore{issues: []fakeIssue{tc.a}, config: config} + if got, err := r.resolve(ctx, store, tc.input); err != nil || got != tc.a.id { + t.Errorf("%s(%q) with only %s = (%q, %v); want (%q, nil)", r.name, tc.input, tc.a.id, got, err, tc.a.id) + } + } + }) + } + + // The configured prefix's own spelling is looked up before any search. + // So a bare hash that prefix uses still resolves to it, even when other + // prefixes share the hash. + store := &fakeResolverStore{ + issues: []fakeIssue{{id: "hacker-news-abc123"}, {id: "ops-abc123"}, {id: "bd-abc123"}}, + config: config, + } + for _, r := range resolvers { + if got, err := r.resolve(ctx, store, "abc123"); err != nil || got != "bd-abc123" { + t.Errorf("%s(%q) = (%q, %v); want (%q, nil)", r.name, "abc123", got, err, "bd-abc123") + } + } +}