diff --git a/internal/platform/postgres/reconciliation.go b/internal/platform/postgres/reconciliation.go index 4f194d5..68eaff7 100644 --- a/internal/platform/postgres/reconciliation.go +++ b/internal/platform/postgres/reconciliation.go @@ -363,6 +363,20 @@ func getReconciliationFinding(ctx context.Context, queryer interface { return finding, nil } +func lockOpenReconciliationFinding(ctx context.Context, tx pgx.Tx, id string) error { + var state string + if err := tx.QueryRow(ctx, `SELECT action_state FROM reconciliation_findings WHERE id=$1 FOR UPDATE`, id).Scan(&state); err != nil { + if errors.Is(err, pgx.ErrNoRows) { + return ErrReconciliationNotFound + } + return fmt.Errorf("lock reconciliation finding: %w", err) + } + if state != "open" { + return ErrReconciliationConflict + } + return nil +} + func (s *Store) LoadReconciliationActionView(ctx context.Context, findingID string) (governance.ReconciliationActionView, error) { var view governance.ReconciliationActionView var currentExists bool @@ -417,6 +431,9 @@ func (s *Store) WritebackReconciliationFinding(ctx context.Context, findingID, t return governance.ReconciliationFinding{}, err } defer tx.Rollback(ctx) + if err := lockOpenReconciliationFinding(ctx, tx, findingID); err != nil { + return governance.ReconciliationFinding{}, err + } finding, err := getReconciliationFinding(ctx, tx, findingID) if errors.Is(err, ErrReconciliationNotFound) { return governance.ReconciliationFinding{}, err @@ -469,6 +486,9 @@ func (s *Store) RequeueReconciliationPublication(ctx context.Context, findingID, return "", err } defer tx.Rollback(ctx) + if err := lockOpenReconciliationFinding(ctx, tx, findingID); err != nil { + return "", err + } finding, err := getReconciliationFinding(ctx, tx, findingID) if err != nil { return "", err @@ -546,6 +566,9 @@ func (s *Store) MarkReconciliationOrphanRemoved(ctx context.Context, findingID, return err } defer tx.Rollback(ctx) + if err := lockOpenReconciliationFinding(ctx, tx, findingID); err != nil { + return err + } finding, err := getReconciliationFinding(ctx, tx, findingID) if err != nil { return err diff --git a/internal/reconciliation/scan.go b/internal/reconciliation/scan.go index f10effe..0f3f818 100644 --- a/internal/reconciliation/scan.go +++ b/internal/reconciliation/scan.go @@ -45,10 +45,18 @@ func (s *Service) scanKnowledge(ctx context.Context, kbID string) ([]weknora.Kno out = append(out, item) added++ } - if len(items) == 0 || added == 0 || len(items) < scanPageSize { + if len(items) == 0 || added == 0 { break } - if total > 0 && int64(len(seen)) >= total { + if total > 0 { + if int64(len(seen)) >= total { + break + } + // Keep paging even if the instance returned a smaller page than + // requested; only the reported total ends the scan. + continue + } + if len(items) < scanPageSize { break } } @@ -383,8 +391,8 @@ func (s *Service) buildFindings(ctx context.Context, run governance.Reconciliati } func (s *Service) r4Finding(row governance.ReconciliationLedgerRow, item weknora.Knowledge, normalized string) (governance.ReconciliationFinding, bool) { - declared := strings.ToLower(metadataString(item.Metadata, "content_sha256")) - ledgerSHA := strings.ToLower(row.SHA256) + declared := strings.ToLower(strings.TrimSpace(metadataString(item.Metadata, "content_sha256"))) + ledgerSHA := strings.ToLower(strings.TrimSpace(row.SHA256)) uploadMD5 := normalizeMD5(row.UploadMD5) fileMD5 := normalizeMD5(item.FileHash) evidence := "none" diff --git a/internal/reconciliation/service_test.go b/internal/reconciliation/service_test.go index 0a734ea..e722bd2 100644 --- a/internal/reconciliation/service_test.go +++ b/internal/reconciliation/service_test.go @@ -11,28 +11,24 @@ import ( ) func TestMatrixCoversEveryLedgerAndUpstreamState(t *testing.T) { - rows := []string{"planned", "pending", "processing", "finalizing", "completed", "failed", "stalled", "cancelled"} columns := []string{"pending", "processing", "finalizing", "completed", "failed", "cancelled", "deleting"} - for _, row := range rows { - for _, column := range columns { - verdict := classify(row, column) - if verdict == verdictSkip && row != "" { - t.Fatalf("classify(%s,%s) unexpectedly skipped", row, column) - } - if row == "completed" && column == "completed" && verdict != verdictConsistent { - t.Fatalf("completed/completed = %v", verdict) - } - if row == "stalled" && column == "completed" && verdict != verdictMismatch { - t.Fatalf("stalled/completed = %v", verdict) - } - if column == "deleting" && verdict != verdictDeleting { - t.Fatalf("%s/deleting = %v", row, verdict) + want := map[string][]verdict{ + "planned": {verdictMismatch, verdictMismatch, verdictMismatch, verdictMismatch, verdictMismatch, verdictMismatch, verdictDeleting}, + "pending": {verdictConsistent, verdictMismatch, verdictMismatch, verdictMismatch, verdictMismatch, verdictMismatch, verdictDeleting}, + "processing": {verdictMismatch, verdictConsistent, verdictMismatch, verdictMismatch, verdictMismatch, verdictMismatch, verdictDeleting}, + "finalizing": {verdictMismatch, verdictMismatch, verdictConsistent, verdictMismatch, verdictMismatch, verdictMismatch, verdictDeleting}, + "completed": {verdictMismatch, verdictMismatch, verdictMismatch, verdictConsistent, verdictMismatch, verdictMismatch, verdictDeleting}, + "failed": {verdictMismatch, verdictMismatch, verdictMismatch, verdictMismatch, verdictConsistent, verdictMismatch, verdictDeleting}, + "stalled": {verdictStalled, verdictStalled, verdictStalled, verdictMismatch, verdictStalled, verdictStalled, verdictDeleting}, + "cancelled": {verdictMismatch, verdictMismatch, verdictMismatch, verdictMismatch, verdictMismatch, verdictConsistent, verdictDeleting}, + } + for row, expected := range want { + for index, column := range columns { + if got := classify(row, column); got != expected[index] { + t.Fatalf("classify(%s,%s) = %v, want %v", row, column, got, expected[index]) } } } - if classify("completed", "pending") != verdictMismatch || classify("cancelled", "cancelled") != verdictConsistent { - t.Fatal("explicit completed/cancelled rows are wrong") - } if AllowedWriteback("completed", "pending") || AllowedWriteback("stalled", "completed") || !AllowedWriteback("failed", "pending") || AllowedWriteback("planned", "pending") || AllowedWriteback("pending", "cancelled") || AllowedWriteback("pending", "deleting") {