From d19b86b9e5419b7f73cd0237bb4a40e2adafb48d Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Wed, 7 Oct 2026 19:08:36 +0200 Subject: [PATCH] fix(observed_state): skip the hook-container query under ScopeStart Second Copilot review finding in the same vein as the previous network/volume fix: collectObservedState's mergeHookContainers call (getHookContainers, an extra ContainerList with a hook-specific label filter) is consumed exclusively by create-phase runner reconciliation (planPreStartHookRunners, reconcile.go) -- the start phase's own RunPreStart never reads observed.HookContainers at all; it executes a runner the create phase already prepared, or fails outright if none exists. Performing this query unconditionally meant a no-op `compose start` on a service declaring pre_start hooks could fail on a daemon call the old imperative engine never made. Gated behind the same scope.plansCreatePhase() check as the network/ volume listing. New regression test TestCollectObservedState_ScopeStartSkipsHookContainerQuery registers exactly one ContainerList expectation (the default Times(1)): gomock fails the test if the extra hook-container listing fires under ScopeStart. Verified by ablation (reverting the guard reproduces the failure deterministically, 3/3 runs). Verified: go build, go test ./pkg/compose/..., and golangci-lint all clean; full e2e suite re-run clean against a locally built binary (same 3 pre-existing, environment-specific failures as the stack's baseline: TestWatch, TestIPC, TestComposeModel). Signed-off-by: Nicolas De Loof --- pkg/compose/observed_state.go | 17 ++++++++++++++--- pkg/compose/observed_state_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/pkg/compose/observed_state.go b/pkg/compose/observed_state.go index aeb02acee..0d4c088c0 100644 --- a/pkg/compose/observed_state.go +++ b/pkg/compose/observed_state.go @@ -210,9 +210,20 @@ func (s *composeService) collectObservedState(ctx context.Context, project *type if err != nil { return nil, err } - raw, err = s.mergeHookContainers(ctx, project, raw) - if err != nil { - return nil, err + // Hook-runner snapshots are consumed exclusively by create-phase runner + // reconciliation (planPreStartHookRunners, reconcile.go) -- the start + // phase's own RunPreStart never reads observed.HookContainers, it + // executes a runner the create phase already prepared or fails outright + // if none exists. Skipping this for ScopeStart matches the + // network/volume guard below: one more unconditional daemon call the old + // imperative engine never made, which could otherwise fail a no-op + // `compose start` on a service with pre_start hooks declared (second + // Copilot review finding in the same vein as the first). + if scope.plansCreatePhase() { + raw, err = s.mergeHookContainers(ctx, project, raw) + if err != nil { + return nil, err + } } knownServices := map[string]bool{} diff --git a/pkg/compose/observed_state_test.go b/pkg/compose/observed_state_test.go index d84050f2b..7b05ae36d 100644 --- a/pkg/compose/observed_state_test.go +++ b/pkg/compose/observed_state_test.go @@ -295,6 +295,30 @@ func TestCollectObservedState_ScopeStartSkipsNetworksAndVolumes(t *testing.T) { assert.Equal(t, len(state.Volumes), 0) } +// TestCollectObservedState_ScopeStartSkipsHookContainerQuery is a regression +// test for a second Copilot review finding in the same vein: hook-runner +// snapshots (mergeHookContainers/getHookContainers) are consumed exclusively +// by create-phase runner reconciliation (planPreStartHookRunners) -- the +// start phase's own RunPreStart never reads observed.HookContainers. Only +// one ContainerList expectation is registered (the default Times(1)): +// gomock fails the test if collectObservedState issues the extra +// getHookContainers listing under ScopeStart for a service declaring +// pre_start hooks. +func TestCollectObservedState_ScopeStartSkipsHookContainerQuery(t *testing.T) { + svc, apiClient := newTestService(t) + project := &types.Project{ + Name: "myproject", + Services: types.Services{ + "web": {Name: "web", PreStart: []types.PreStartHook{{}}}, + }, + } + apiClient.EXPECT().ContainerList(gomock.Any(), gomock.Any()).Return(client.ContainerListResult{}, nil) + + state, err := svc.collectObservedState(t.Context(), project, ScopeStart) + assert.NilError(t, err) + assert.Equal(t, len(state.HookContainers["web"]), 0) +} + // TestCollectObservedState_LegacyHookRunnerNotDuplicated covers a runner // created before the ConfigHashLabel exclusion existed (or by an older // compose version): it still carries the label, so it matches getContainers'