diff --git a/docs/roadmap/remediation.md b/docs/roadmap/remediation.md index 0c20cf3..4c6db19 100644 --- a/docs/roadmap/remediation.md +++ b/docs/roadmap/remediation.md @@ -1,7 +1,7 @@ # Automatic Usage Reporting Review -Status: four open findings: documentation R1, integration R2, and efficiency -opportunities R3–R4. Production code is unchanged by these reviews. +Status: R1–R4 complete. Each remediation was verified and committed separately. +Historical audit evidence and findings are retained below. ## Scope and evidence @@ -222,7 +222,12 @@ bindings. No timing threshold or benchmark is necessary. ## R4: Share invocation-flag lookup across installation contexts -**Priority:** P3. **Status:** open. **Plan coverage:** Stages 1–2. +**Priority:** P3. **Status:** complete. **Plan coverage:** Stages 1–2. + +Collision validation now indexes invocation spellings once and walks local +spellings. Alias contexts share one invocation map through a combined lookup. +Regressions cover inactive-alias activation, scoped/global and short aliases, +discovery help, collisions and rollback. All workspace suites passed. **Relevant code:** [validateInvocationFlagCollisions](../../registry.go), [collectInvocationFeatures](../../registry.go), and the invocation-flag insertion diff --git a/flag_aliases.go b/flag_aliases.go index c31cede..67302a8 100644 --- a/flag_aliases.go +++ b/flag_aliases.go @@ -24,9 +24,10 @@ type flagAliasMapping struct { } type flagAliasContext struct { - path commandpath.Path - canonical map[string]capturedFlagSpelling - discovery bool + path commandpath.Path + canonical map[string]capturedFlagSpelling + invocation map[string]capturedFlagSpelling + discovery bool } func (aliases resolvedFlagAliases) neutralRecords() []commandmeta.FlagAlias { @@ -64,6 +65,24 @@ func (spelling capturedFlagSpelling) token() string { return "--" + spelling.name } +func (context flagAliasContext) canonicalSpelling(key string) (capturedFlagSpelling, bool) { + if value, ok := context.canonical[key]; ok { + return value, true + } + value, ok := context.invocation[key] + return value, ok +} + +func invocationFlagSpellings(flags []commandmeta.Flag) map[string]capturedFlagSpelling { + result := make(map[string]capturedFlagSpelling, len(flags)) + for _, flag := range flags { + for _, spelling := range featureSpellings(flag) { + result[flagSpellingKey(spelling)] = spelling + } + } + return result +} + func compileFlagAliases(rules []RoutingRule, roots map[string]*canonicalNode, aliases interface { Resolve(commandpath.Path) commandpath.Path ProperSourcePrefixes() []commandpath.Path @@ -97,13 +116,11 @@ func compileFlagAliases(rules []RoutingRule, roots map[string]*canonicalNode, al canonicalContexts[key] = flagAliasContext{path: node.path.Clone(), canonical: canonicalFlagSpellings(node.command)} } discoveryContexts := discoverFlagContexts(rules, roots, aliases, canonicalNodes) + shared := invocationFlagSpellings(features) for _, contexts := range []map[string]flagAliasContext{canonicalContexts, discoveryContexts} { - for _, context := range contexts { - for _, flag := range features { - for _, spelling := range featureSpellings(flag) { - context.canonical[flagSpellingKey(spelling)] = spelling - } - } + for key, context := range contexts { + context.invocation = shared + contexts[key] = context } } @@ -245,7 +262,7 @@ func resolveFlagAliases(context flagAliasContext, mappings map[string]flagAliasM var resolve func(capturedFlagSpelling) (terminal, error) resolve = func(current capturedFlagSpelling) (terminal, error) { key := flagSpellingKey(current) - if endpoint, exists := context.canonical[key]; exists { + if endpoint, exists := context.canonicalSpelling(key); exists { return terminal{spelling: endpoint, active: true}, nil } if result, exists := resolved[key]; exists { @@ -288,7 +305,7 @@ func resolveFlagAliases(context flagAliasContext, mappings map[string]flagAliasM } continue } - if _, collision := context.canonical[sourceKey]; collision { + if _, collision := context.canonicalSpelling(sourceKey); collision { return resolvedFlagAliases{}, fmt.Errorf("flag alias %s conflicts with a canonical spelling in %s", source.token(), flagContextDescription(context)) } result.bySpelling[sourceKey] = resolvedFlagAlias{source: source, target: terminal.spelling} diff --git a/invocation_feature_test.go b/invocation_feature_test.go index a977e4a..04307a4 100644 --- a/invocation_feature_test.go +++ b/invocation_feature_test.go @@ -343,3 +343,41 @@ func TestFeatureOccurrencePartitionsPreserveCapturedOrder(t *testing.T) { t.Fatalf("%#v %#v", result, got) } } + +func TestSharedInvocationSpellingsActivateAliasesAndPreserveRollback(t *testing.T) { + starts := 0 + feature := NewInvocationFeature(InvocationFeatureSpec[struct{}]{Flags: []Flag[struct{}]{BoolFlag(FlagMeta{Name: "observe", Short: "o"}, false, func(*struct{}, Supplied[bool]) {})}, Start: func(Invocation, struct{}) (context.Context, func() Report) { starts++; return nil, nil }}) + run := NewCommand(CommandSpec[struct{}]{Name: "run", Run: func(Invocation, struct{}) Outcome { return Success() }}) + app, err := New(Identity{Name: "test"}, run, Alias("deep run", "run"), FlagAlias("--watch", "--observe")) + if err != nil { + t.Fatal(err) + } + if r := app.Execute(context.Background(), []string{"run", "--watch"}, Streams{}); r.Kind != OutcomeUsage { + t.Fatalf("inactive alias: %#v", r) + } + if err := app.Register(WithInvocationFeature(Group("owner", ""), feature), FlagAliasFor("run", "--inspect", "--observe")); err != nil { + t.Fatal(err) + } + for _, args := range [][]string{{"run", "--watch"}, {"deep", "run", "--inspect"}, {"run", "-o"}} { + if r := app.Execute(context.Background(), args, Streams{}); r.Status != 0 { + t.Fatalf("%v: %#v", args, r) + } + } + if starts != 3 { + t.Fatalf("starts=%d", starts) + } + var out bytes.Buffer + if r := app.Execute(context.Background(), []string{"deep", "--watch", "--help"}, Streams{Out: &out}); r.Status != 0 || !strings.Contains(out.String(), "--observe") || starts != 3 { + t.Fatalf("discovery: %#v %q", r, out.String()) + } + conflict := NewCommand(CommandSpec[struct{}]{Name: "conflict", Flags: []Flag[struct{}]{BoolFlag(FlagMeta{Name: "other", Short: "o"}, false, func(*struct{}, Supplied[bool]) {})}}) + if err := app.Register(conflict); err == nil { + t.Fatal("short spelling collision accepted") + } + if err := app.Register(FlagAlias("--observe", "--verbose")); err == nil { + t.Fatal("canonical alias source collision accepted") + } + if r := app.Execute(context.Background(), []string{"deep", "run", "--watch"}, Streams{}); r.Status != 0 || starts != 4 { + t.Fatalf("rollback: %#v starts=%d", r, starts) + } +} diff --git a/registry.go b/registry.go index 6fd7a25..5bfa09b 100644 --- a/registry.go +++ b/registry.go @@ -428,13 +428,11 @@ func featureSpellings(flag commandmeta.Flag) []capturedFlagSpelling { } func validateInvocationFlagCollisions(roots map[string]*canonicalNode, flags []commandmeta.Flag) error { + shared := invocationFlagSpellings(flags) for _, node := range indexCanonicalNodes(roots) { - local := canonicalFlagSpellings(node.command) - for _, flag := range flags { - for _, spelling := range featureSpellings(flag) { - if _, exists := local[flagSpellingKey(spelling)]; exists { - return fmt.Errorf("invocation feature flag %s conflicts with command %q", spelling.token(), node.path.String()) - } + for key, spelling := range canonicalFlagSpellings(node.command) { + if _, exists := shared[key]; exists { + return fmt.Errorf("invocation feature flag %s conflicts with command %q", spelling.token(), node.path.String()) } } }