Partition flag occurrences once per invocation

This commit is contained in:
2026-11-22 22:23:07 +00:00
parent 98e3958dfb
commit 3415d9337d
3 changed files with 67 additions and 6 deletions

View File

@@ -173,7 +173,11 @@ suites. No other new interaction defect requiring remediation was identified.
## R3: Partition occurrence preparation instead of rescanning argv for every feature ## R3: Partition occurrence preparation instead of rescanning argv for every feature
**Priority:** P3. **Status:** open. **Plan coverage:** Stages 2–4. **Priority:** P3. **Status:** complete. **Plan coverage:** Stages 2–4.
Partitioned canonical occurrences once by input owner, with dense relative
positions. Interleaved captured occurrence flags, aliases, local inputs and a
flagless feature are covered by regression tests. All workspace suites passed.
**Relevant code:** [execution.prepareFeatures](../../execute.go), **Relevant code:** [execution.prepareFeatures](../../execute.go),
[typedInvocationFeature.prepare](../../invocation_feature.go), and [typedInvocationFeature.prepare](../../invocation_feature.go), and

View File

@@ -48,7 +48,7 @@ func (a *App) Execute(ctx context.Context, args []string, streams Streams) Outco
payload != nil && payload.hasRun() && !routedSnapshot.builtins[parsed.Key] payload != nil && payload.hasRun() && !routedSnapshot.builtins[parsed.Key]
if parsed.Failure != nil { if parsed.Failure != nil {
outcome = Usage(parsed.Failure.Message) outcome = Usage(parsed.Failure.Message)
} else if err := current.prepareFeatures(parsed, featureRequired); err != nil { } else if err := current.prepareFeatures(&parsed, featureRequired); err != nil {
outcome = classifyInputError(err) outcome = classifyInputError(err)
} else if current.route.kind == routeDiscovery { } else if current.route.kind == routeDiscovery {
if parsed.HelpRequested { if parsed.HelpRequested {
@@ -83,17 +83,34 @@ func (a *App) Execute(ctx context.Context, args []string, streams Streams) Outco
return current.finalize(outcome, path, handlerRan) return current.finalize(outcome, path, handlerRan)
} }
func (current *execution) prepareFeatures(parsed commandmeta.Result, required bool) error { func (current *execution) prepareFeatures(parsed *commandmeta.Result, required bool) error {
if current.snapshot == nil { if current.snapshot == nil {
return nil return nil
} }
// Partition the canonical occurrence stream once. Positions within each owner
// remain dense and preserve alias/interleaved binding order.
owners := make(map[string]int, len(current.snapshot.featureFlags))
counts := make([]int, len(current.snapshot.features))
for i, feature := range current.snapshot.features {
flags := feature.payload.bridgeFlags()
counts[i] = len(flags)
for _, flag := range flags {
owners[flag.Name] = i + 1
}
}
occurrences := make([][]commandmeta.FlagOccurrence, len(counts)+1)
for _, occurrence := range parsed.FlagOccurrences {
owner := owners[occurrence.Name]
occurrences[owner] = append(occurrences[owner], occurrence)
}
parsed.FlagOccurrences = occurrences[0]
offset := 0 offset := 0
for _, feature := range current.snapshot.features { for i, feature := range current.snapshot.features {
count := len(feature.payload.bridgeFlags()) count := counts[i]
if offset+count > len(parsed.InvocationFlagValues) || offset+count > len(parsed.InvocationFlagSpellings) { if offset+count > len(parsed.InvocationFlagValues) || offset+count > len(parsed.InvocationFlagSpellings) {
return fmt.Errorf("missing invocation feature flag values") return fmt.Errorf("missing invocation feature flag values")
} }
start, err := feature.payload.prepare(parsed.InvocationFlagValues[offset:offset+count], parsed.InvocationFlagSpellings[offset:offset+count], parsed.FlagOccurrences, false) start, err := feature.payload.prepare(parsed.InvocationFlagValues[offset:offset+count], parsed.InvocationFlagSpellings[offset:offset+count], occurrences[i+1], false)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -303,3 +303,43 @@ func TestInvocationFeaturesChainContextsAndReports(t *testing.T) {
t.Fatalf("%#v %v %q", out, order, diag.String()) t.Fatalf("%#v %v %q", out, order, diag.String())
} }
} }
func TestFeatureOccurrencePartitionsPreserveCapturedOrder(t *testing.T) {
type inner struct{ order []string }
type outer struct{ values FlagValues[inner] }
got := make(map[string][]string)
bindings := make(map[string][]Flag[inner])
for _, prefix := range []string{"first", "second", "local"} {
for _, suffix := range []string{"a", "b"} {
bindings[prefix] = append(bindings[prefix], OccurrenceFlag(FlagMeta{Name: prefix + "-" + suffix}, func(s string) (string, error) { return s, nil }, func(i *inner, v Supplied[string]) {
if v.Set {
i.order = append(i.order, v.Value)
}
}))
}
}
capture := func(prefix string) []Flag[outer] {
return CaptureFlags(bindings[prefix], func(o *outer) *FlagValues[inner] { return &o.values })
}
apply := func(prefix string, o outer) {
var i inner
if err := o.values.ApplyTo(&i, bindings[prefix]...); err != nil {
t.Fatal(err)
}
got[prefix] = i.order
}
feature := func(prefix string) InvocationFeature {
return NewInvocationFeature(InvocationFeatureSpec[outer]{Flags: capture(prefix), Start: func(inv Invocation, o outer) (context.Context, func() Report) { apply(prefix, o); return nil, nil }})
}
cmd := NewCommand(CommandSpec[outer]{Name: "run", Flags: capture("local"), Run: func(inv Invocation, o outer) Outcome { apply("local", o); return Success() }})
cmd = WithInvocationFeature(WithInvocationFeature(cmd, feature("first")), feature("second"))
cmd = WithInvocationFeature(cmd, NewInvocationFeature(InvocationFeatureSpec[struct{}]{Start: func(Invocation, struct{}) (context.Context, func() Report) { return nil, nil }}))
app, err := New(Identity{Name: "test"}, cmd, FlagAlias("--pick", "--first-b"))
if err != nil {
t.Fatal(err)
}
result := app.Execute(context.Background(), []string{"run", "--pick=B", "--local-b=L2", "--second-a=S", "--first-a=A", "--local-a=L1"}, Streams{})
if result.Status != 0 || !reflect.DeepEqual(got, map[string][]string{"first": {"B", "A"}, "second": {"S"}, "local": {"L2", "L1"}}) {
t.Fatalf("%#v %#v", result, got)
}
}