fix(reconciliation): 收紧确认页与并发处置
为什么:净室评审发现两处可复发风险——扫描在实例返回小于请求页时可能提前结束;并发重复处置可能各写一条审计。 做什么:扫描以 total 为准继续翻页(total 未知才按短页结束);处置前对 finding 行 FOR UPDATE 并只接受 open;R4 sha256 统一 trim。 验证:go test ./internal/reconciliation ./internal/platform/postgres;make verify 全绿。
This commit is contained in:
@@ -363,6 +363,20 @@ func getReconciliationFinding(ctx context.Context, queryer interface {
|
|||||||
return finding, nil
|
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) {
|
func (s *Store) LoadReconciliationActionView(ctx context.Context, findingID string) (governance.ReconciliationActionView, error) {
|
||||||
var view governance.ReconciliationActionView
|
var view governance.ReconciliationActionView
|
||||||
var currentExists bool
|
var currentExists bool
|
||||||
@@ -417,6 +431,9 @@ func (s *Store) WritebackReconciliationFinding(ctx context.Context, findingID, t
|
|||||||
return governance.ReconciliationFinding{}, err
|
return governance.ReconciliationFinding{}, err
|
||||||
}
|
}
|
||||||
defer tx.Rollback(ctx)
|
defer tx.Rollback(ctx)
|
||||||
|
if err := lockOpenReconciliationFinding(ctx, tx, findingID); err != nil {
|
||||||
|
return governance.ReconciliationFinding{}, err
|
||||||
|
}
|
||||||
finding, err := getReconciliationFinding(ctx, tx, findingID)
|
finding, err := getReconciliationFinding(ctx, tx, findingID)
|
||||||
if errors.Is(err, ErrReconciliationNotFound) {
|
if errors.Is(err, ErrReconciliationNotFound) {
|
||||||
return governance.ReconciliationFinding{}, err
|
return governance.ReconciliationFinding{}, err
|
||||||
@@ -469,6 +486,9 @@ func (s *Store) RequeueReconciliationPublication(ctx context.Context, findingID,
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
defer tx.Rollback(ctx)
|
defer tx.Rollback(ctx)
|
||||||
|
if err := lockOpenReconciliationFinding(ctx, tx, findingID); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
finding, err := getReconciliationFinding(ctx, tx, findingID)
|
finding, err := getReconciliationFinding(ctx, tx, findingID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@@ -546,6 +566,9 @@ func (s *Store) MarkReconciliationOrphanRemoved(ctx context.Context, findingID,
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer tx.Rollback(ctx)
|
defer tx.Rollback(ctx)
|
||||||
|
if err := lockOpenReconciliationFinding(ctx, tx, findingID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
finding, err := getReconciliationFinding(ctx, tx, findingID)
|
finding, err := getReconciliationFinding(ctx, tx, findingID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -45,10 +45,18 @@ func (s *Service) scanKnowledge(ctx context.Context, kbID string) ([]weknora.Kno
|
|||||||
out = append(out, item)
|
out = append(out, item)
|
||||||
added++
|
added++
|
||||||
}
|
}
|
||||||
if len(items) == 0 || added == 0 || len(items) < scanPageSize {
|
if len(items) == 0 || added == 0 {
|
||||||
break
|
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
|
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) {
|
func (s *Service) r4Finding(row governance.ReconciliationLedgerRow, item weknora.Knowledge, normalized string) (governance.ReconciliationFinding, bool) {
|
||||||
declared := strings.ToLower(metadataString(item.Metadata, "content_sha256"))
|
declared := strings.ToLower(strings.TrimSpace(metadataString(item.Metadata, "content_sha256")))
|
||||||
ledgerSHA := strings.ToLower(row.SHA256)
|
ledgerSHA := strings.ToLower(strings.TrimSpace(row.SHA256))
|
||||||
uploadMD5 := normalizeMD5(row.UploadMD5)
|
uploadMD5 := normalizeMD5(row.UploadMD5)
|
||||||
fileMD5 := normalizeMD5(item.FileHash)
|
fileMD5 := normalizeMD5(item.FileHash)
|
||||||
evidence := "none"
|
evidence := "none"
|
||||||
|
|||||||
@@ -11,28 +11,24 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestMatrixCoversEveryLedgerAndUpstreamState(t *testing.T) {
|
func TestMatrixCoversEveryLedgerAndUpstreamState(t *testing.T) {
|
||||||
rows := []string{"planned", "pending", "processing", "finalizing", "completed", "failed", "stalled", "cancelled"}
|
|
||||||
columns := []string{"pending", "processing", "finalizing", "completed", "failed", "cancelled", "deleting"}
|
columns := []string{"pending", "processing", "finalizing", "completed", "failed", "cancelled", "deleting"}
|
||||||
for _, row := range rows {
|
want := map[string][]verdict{
|
||||||
for _, column := range columns {
|
"planned": {verdictMismatch, verdictMismatch, verdictMismatch, verdictMismatch, verdictMismatch, verdictMismatch, verdictDeleting},
|
||||||
verdict := classify(row, column)
|
"pending": {verdictConsistent, verdictMismatch, verdictMismatch, verdictMismatch, verdictMismatch, verdictMismatch, verdictDeleting},
|
||||||
if verdict == verdictSkip && row != "" {
|
"processing": {verdictMismatch, verdictConsistent, verdictMismatch, verdictMismatch, verdictMismatch, verdictMismatch, verdictDeleting},
|
||||||
t.Fatalf("classify(%s,%s) unexpectedly skipped", row, column)
|
"finalizing": {verdictMismatch, verdictMismatch, verdictConsistent, verdictMismatch, verdictMismatch, verdictMismatch, verdictDeleting},
|
||||||
}
|
"completed": {verdictMismatch, verdictMismatch, verdictMismatch, verdictConsistent, verdictMismatch, verdictMismatch, verdictDeleting},
|
||||||
if row == "completed" && column == "completed" && verdict != verdictConsistent {
|
"failed": {verdictMismatch, verdictMismatch, verdictMismatch, verdictMismatch, verdictConsistent, verdictMismatch, verdictDeleting},
|
||||||
t.Fatalf("completed/completed = %v", verdict)
|
"stalled": {verdictStalled, verdictStalled, verdictStalled, verdictMismatch, verdictStalled, verdictStalled, verdictDeleting},
|
||||||
}
|
"cancelled": {verdictMismatch, verdictMismatch, verdictMismatch, verdictMismatch, verdictMismatch, verdictConsistent, verdictDeleting},
|
||||||
if row == "stalled" && column == "completed" && verdict != verdictMismatch {
|
}
|
||||||
t.Fatalf("stalled/completed = %v", verdict)
|
for row, expected := range want {
|
||||||
}
|
for index, column := range columns {
|
||||||
if column == "deleting" && verdict != verdictDeleting {
|
if got := classify(row, column); got != expected[index] {
|
||||||
t.Fatalf("%s/deleting = %v", row, verdict)
|
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") ||
|
if AllowedWriteback("completed", "pending") || AllowedWriteback("stalled", "completed") ||
|
||||||
!AllowedWriteback("failed", "pending") || AllowedWriteback("planned", "pending") ||
|
!AllowedWriteback("failed", "pending") || AllowedWriteback("planned", "pending") ||
|
||||||
AllowedWriteback("pending", "cancelled") || AllowedWriteback("pending", "deleting") {
|
AllowedWriteback("pending", "cancelled") || AllowedWriteback("pending", "deleting") {
|
||||||
|
|||||||
Reference in New Issue
Block a user