From c0f5750bca1644bb733fb51c55eaac63148a822d Mon Sep 17 00:00:00 2001 From: Alassane <79718216+yattdev@users.noreply.github.com> Date: Mon, 10 Aug 2026 05:21:56 -0400 Subject: [PATCH] fix(github): unify CI auto-fix direct-dispatch metadata (#2469) Co-authored-by: ayattara --- ...vent_handlers_github_ci_automation_test.go | 154 +++++++++++++++++- 1 file changed, 152 insertions(+), 2 deletions(-) diff --git a/apps/backend/internal/orchestrator/event_handlers_github_ci_automation_test.go b/apps/backend/internal/orchestrator/event_handlers_github_ci_automation_test.go index a4429727d8..aca9913d39 100644 --- a/apps/backend/internal/orchestrator/event_handlers_github_ci_automation_test.go +++ b/apps/backend/internal/orchestrator/event_handlers_github_ci_automation_test.go @@ -1923,6 +1923,69 @@ func TestHandleTaskPRCIAutomationAutoMergeRunsAfterAutoFixExhaustion(t *testing. } } +// runEmptyDeltaInFlightFixTest drives handleTaskPRCIAutomationWithRefresh with +// an empty feedback delta (no failing checks, no comments) against a prior +// fix attempt recorded with the same signature, varying how long ago that fix +// was enqueued. It returns the resulting merge call count so callers can +// assert the in-flight-fix merge block (ciAutomationFixBlockWindow) is +// honored or has expired. +func runEmptyDeltaInFlightFixTest(t *testing.T, lastFixEnqueuedAt time.Time) int { + t.Helper() + ctx := context.Background() + repo := setupTestRepo(t) + seedTaskAndSession(t, repo, "task-1", "session-1", models.TaskSessionStateRunning) + svc := createTestService(repo, newMockStepGetter(), newMockTaskRepo()) + now := time.Now().UTC() + pr := &github.TaskPR{ + TaskID: "task-1", + RepositoryID: "repo-1", + Owner: "acme", + Repo: "widget", + PRNumber: 42, + State: "open", + ChecksState: "success", + ReviewState: "approved", + MergeableState: "clean", + LastSyncedAt: &now, + } + _, emptySignature := encodeCIAutomationCheckpoint(ciAutomationCheckpoint{}) + ghSvc := &mockGitHubService{ + ciOptionsResp: &github.TaskCIOptionsResponse{ + TaskID: "task-1", + AutoFixEnabled: true, + AutoMergeEnabled: true, + }, + ciPRState: &github.TaskCIPRAutomationState{ + TaskID: "task-1", + RepositoryID: "repo-1", + PRNumber: 42, + LastFixSignature: emptySignature, + LastFixEnqueuedAt: &lastFixEnqueuedAt, + }, + prFeedback: &github.PRFeedback{}, + } + svc.SetGitHubService(ghSvc) + + if err := svc.handleTaskPRCIAutomationWithRefresh(ctx, pr, false); err != nil { + t.Fatalf("handle empty-delta CI automation: %v", err) + } + return ghSvc.mergeCalls +} + +func TestHandleTaskPRCIAutoFixEmptyDeltaBlocksMergeWithinFixBlockWindow(t *testing.T) { + lastFixEnqueuedAt := time.Now().UTC() + if mergeCalls := runEmptyDeltaInFlightFixTest(t, lastFixEnqueuedAt); mergeCalls != 0 { + t.Fatalf("expected in-flight fix (enqueued %s ago) to block auto-merge, got %d merge calls", time.Since(lastFixEnqueuedAt), mergeCalls) + } +} + +func TestHandleTaskPRCIAutoFixEmptyDeltaAllowsMergeAfterFixBlockWindow(t *testing.T) { + lastFixEnqueuedAt := time.Now().UTC().Add(-2 * ciAutomationFixBlockWindow) + if mergeCalls := runEmptyDeltaInFlightFixTest(t, lastFixEnqueuedAt); mergeCalls != 1 { + t.Fatalf("expected fix enqueued outside the block window to allow auto-merge, got %d merge calls", mergeCalls) + } +} + func TestDispatchCIAutomationPromptForPRIdleRoundCapUsesDedicatedError(t *testing.T) { ctx := context.Background() svc := &Service{} @@ -2346,8 +2409,15 @@ func TestDispatchCIAutomationPromptForPRRecordsUserMessageBeforeDirectPrompt(t * if len(agentMgr.capturedPromptCalls) != 1 || !agentMgr.capturedPromptCalls[0].DispatchOnly { t.Fatalf("expected CI automation direct prompt to dispatch only, got %+v", agentMgr.capturedPromptCalls) } - if messageCreator.userMessages[0].metadata["origin"] != ciAutomationOrigin { - t.Fatalf("expected CI automation user message metadata, got %+v", messageCreator.userMessages[0].metadata) + gotMeta := messageCreator.userMessages[0].metadata + if gotMeta["origin"] != ciAutomationOrigin { + t.Fatalf("expected CI automation user message metadata, got %+v", gotMeta) + } + wantMeta := ciAutomationMessageMetadataForPR(pr, "signature") + for key, wantVal := range wantMeta { + if gotVal := gotMeta[key]; gotVal != wantVal { + t.Fatalf("expected metadata[%q] = %v, got %v (full metadata: %+v)", key, wantVal, gotVal, gotMeta) + } } } @@ -2889,6 +2959,86 @@ func waitForCIAutomationIdle(t *testing.T, svc *Service, key string, timeout tim } } +// TestHandleTaskPRCIAutoFixDirectDispatchPersistsWideMetadataEndToEnd drives the +// real auto-fix flow (handleTaskPRCIAutomation, not dispatchCIAutomationPromptForPR +// in isolation) against a WaitingForInput session so the direct-dispatch branch +// runs. The isolated dispatch test hands the function its own pr and a literal +// "signature", so it cannot detect a call site that threads the wrong value into +// recordCIAutomationUserMessage. Here the expected values come from the flow +// itself — the signature is whatever the handler actually recorded on the fix +// attempt, and the PR fields come from the PR the handler was driven with. +func TestHandleTaskPRCIAutoFixDirectDispatchPersistsWideMetadataEndToEnd(t *testing.T) { + ctx := context.Background() + repo := setupTestRepo(t) + seedTaskAndSession(t, repo, "task-1", "session-1", models.TaskSessionStateWaitingForInput) + agentMgr := &mockAgentManager{isAgentRunning: true, repoForExecutionLookup: repo} + svc := createTestService(repo, newMockStepGetter(), newMockTaskRepo()) + svc.agentManager = agentMgr + svc.executor = executor.NewExecutor(agentMgr, repo, testLogger(), executor.ExecutorConfig{}) + messageCreator := &mockMessageCreator{} + svc.messageCreator = messageCreator + seedExecutorRunning(t, repo, "session-1", "task-1", "exec-1") + session, err := repo.GetTaskSession(ctx, "session-1") + if err != nil { + t.Fatalf("load session: %v", err) + } + session.AgentExecutionID = "exec-1" + if err := repo.UpdateTaskSession(ctx, session); err != nil { + t.Fatalf("update session execution: %v", err) + } + now := time.Now().UTC() + pr := &github.TaskPR{ + TaskID: "task-1", + RepositoryID: "repo-1", + Owner: "acme", + Repo: "widget", + PRNumber: 42, + State: "open", + ChecksState: "success", + LastSyncedAt: &now, + } + ghSvc := &mockGitHubService{ + ciOptionsResp: &github.TaskCIOptionsResponse{ + TaskID: "task-1", + AutoFixEnabled: true, + EffectiveAutoFixPrompt: "Fix the PR\n\n{{pr.feedback}}", + }, + prFeedback: &github.PRFeedback{ + Comments: []github.PRComment{{ID: 99, Body: "plain PR comment should trigger auto-fix"}}, + }, + } + svc.SetGitHubService(ghSvc) + + if err := svc.handleTaskPRCIAutomationWithRefresh(ctx, pr, false); err != nil { + t.Fatalf("handle auto-fix: %v", err) + } + + if len(messageCreator.userMessages) != 1 { + t.Fatalf("expected one visible CI automation user message, got %d", len(messageCreator.userMessages)) + } + if len(ghSvc.fixAttempts) != 1 { + t.Fatalf("expected one recorded fix attempt, got %d", len(ghSvc.fixAttempts)) + } + gotMeta := messageCreator.userMessages[0].metadata + // The signature must be the one the flow computed and recorded, not a value + // the test supplied — this is what pins the call site's threading. + wantMeta := ciAutomationMessageMetadataForPR(pr, ghSvc.fixAttempts[0].Signature) + for key, wantVal := range wantMeta { + if gotVal := gotMeta[key]; gotVal != wantVal { + t.Fatalf("expected metadata[%q] = %v, got %v (full metadata: %+v)", key, wantVal, gotVal, gotMeta) + } + } + if gotMeta["origin"] != ciAutomationOrigin { + t.Fatalf("expected origin %q, got %v", ciAutomationOrigin, gotMeta["origin"]) + } + if gotMeta["feedback_signature"] == "" || gotMeta["feedback_signature"] == nil { + t.Fatalf("expected a non-empty feedback signature, got %v", gotMeta["feedback_signature"]) + } + if gotMeta["pr_number"] != 42 || gotMeta["owner"] != "acme" || gotMeta["repo"] != "widget" { + t.Fatalf("expected PR identity from the driven PR, got %+v", gotMeta) + } +} + func ptrString(value string) *string { return &value }