diff --git a/apps/api/internal/processing/pipeline.go b/apps/api/internal/processing/pipeline.go index 31fc8ba..a2a8330 100644 --- a/apps/api/internal/processing/pipeline.go +++ b/apps/api/internal/processing/pipeline.go @@ -563,6 +563,14 @@ func (p *Pipeline) RetryJob(ctx context.Context, companyID, id uuid.UUID) (Job, if p.Limiter != nil && !p.Limiter.Allow(companyID) { return Job{}, ErrRateLimited } + // Load the exact output selections saved at job creation, so retry credit + // checks match the original charge (including jobs with a subset of outputs). + var selectedOutputs []string + if err := p.Pool.QueryRow(ctx, ` + SELECT processing_types FROM processing_jobs + WHERE id = $1 AND company_id = $2`, id, companyID).Scan(&selectedOutputs); err != nil { + return Job{}, fmt.Errorf("processing: load retry outputs for job %s: %w", id, err) + } if err := p.assertProcessingGates(ctx, companyID, job.ProcessingType, job.TotalProducts, selectedOutputs...); err != nil { return Job{}, err } diff --git a/apps/api/internal/processing/pipeline_retry_integration_test.go b/apps/api/internal/processing/pipeline_retry_integration_test.go index cd5743d..789554f 100644 --- a/apps/api/internal/processing/pipeline_retry_integration_test.go +++ b/apps/api/internal/processing/pipeline_retry_integration_test.go @@ -86,14 +86,15 @@ func TestRetryJobResetsCompletedJobForFullRerun(t *testing.T) { firstStep = progress[0].Step } + selectedOutputs := []string{"title", "seo_meta"} var jobID uuid.UUID err = pg.QueryRow(ctx, ` INSERT INTO processing_jobs ( company_id, user_id, status, total_products, processed_products, - processing_type, current_step, step_progress, started_at, completed_at - ) VALUES ($1, $2, 'completed', $3, $3, 'full', $4, $5::jsonb, now(), now()) + processing_type, processing_types, current_step, step_progress, started_at, completed_at + ) VALUES ($1, $2, 'completed', $3, $3, 'full', $4, $5, $6::jsonb, now(), now()) RETURNING id`, - companyID, userID, len(rawIDs), firstStep, progressJSON, + companyID, userID, len(rawIDs), firstStep, selectedOutputs, progressJSON, ).Scan(&jobID) if err != nil { t.Fatal(err) @@ -131,6 +132,21 @@ func TestRetryJobResetsCompletedJobForFullRerun(t *testing.T) { t.Fatalf("completed_at should be reset, got %v", *job.CompletedAt) } + // Retrying must preserve the saved processing_types; credit gates and the + // worker both need the original output selections, not legacy defaults. + var persistedOutputs []string + if err := pg.QueryRow(ctx, `SELECT processing_types FROM processing_jobs WHERE id = $1`, jobID).Scan(&persistedOutputs); err != nil { + t.Fatal(err) + } + if len(persistedOutputs) != len(selectedOutputs) { + t.Fatalf("processing_types=%v want %v", persistedOutputs, selectedOutputs) + } + for i := range selectedOutputs { + if persistedOutputs[i] != selectedOutputs[i] { + t.Fatalf("processing_types=%v want %v", persistedOutputs, selectedOutputs) + } + } + var pendingCount, processedCount int err = pg.QueryRow(ctx, ` SELECT