droboticflow/internal/worker/original_source_recheck.go
2026-11-08 10:00:31 -08:00

212 lines
7.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package worker
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"droboticflow/internal/deliveryindex"
"droboticflow/internal/domain"
"droboticflow/internal/sourceidentity"
"droboticflow/internal/workspacescope"
)
// Follow only validated persisted release ancestry, including retries carrying
// the same frozen package selection. Never infer a source from current intake.
func (r *Runtime) originalDeliveryRun(ctx context.Context, run *domain.RunRecord) (*domain.RunRecord, map[string]bool, error) {
if run == nil {
return nil, nil, fmt.Errorf("missing original delivery run")
}
original := run
lineage := map[string]bool{run.ID: true}
if run.Metadata != nil && run.Metadata.CaptureDeliveryPackage != nil {
wanted := run.Metadata.CaptureDeliveryPackage.SourceRunID
for original.ID != wanted {
if len(lineage) >= 32 || original.ParentRunID == "" {
return nil, nil, fmt.Errorf("original delivery lineage incomplete")
}
parent, err := r.store.GetRun(ctx, original.ParentRunID)
if err != nil {
return nil, nil, err
}
if parent == nil || lineage[parent.ID] || parent.OrganizationID != run.OrganizationID || parent.ProjectID != run.ProjectID || parent.WorkflowID != run.WorkflowID || parent.TaskID != run.TaskID {
return nil, nil, fmt.Errorf("original delivery parent scope mismatch")
}
lineage[parent.ID] = true
original = parent
}
} else if run.Trigger == domain.RunTriggerHumanRelease {
parent, err := r.store.GetRun(ctx, run.ParentRunID)
if err != nil {
return nil, nil, err
}
if parent == nil || parent.ID == run.ID || parent.OrganizationID != run.OrganizationID || parent.ProjectID != run.ProjectID || parent.WorkflowID != run.WorkflowID || parent.TaskID != run.TaskID {
return nil, nil, fmt.Errorf("original delivery parent scope mismatch")
}
original = parent
lineage[parent.ID] = true
}
return original, lineage, nil
}
func (r *Runtime) originalDeliverySelection(ctx context.Context, run *domain.RunRecord) (*sourceidentity.Selection, error) {
original, lineage, err := r.originalDeliveryRun(ctx, run)
if err != nil {
return nil, err
}
capture, err := r.store.GetCaptureRecordByRunID(ctx, original.ID)
if run.Metadata != nil && run.Metadata.CaptureDeliveryPackage != nil {
capture, err = r.frozenDeliveryCapture(ctx, run)
}
if err != nil {
return nil, err
}
if capture == nil || capture.ID != original.TaskID || !lineage[capture.RunID] {
return nil, fmt.Errorf("original delivery capture lineage mismatch")
}
return sourceidentity.FromRun(original, capture)
}
func (r *Runtime) recheckOriginalDeliverySelection(ctx context.Context, run *domain.RunRecord, selection *sourceidentity.Selection) error {
ref := selection.Ref
c, err := r.store.GetStorageConnection(ctx, ref.ConnectionID)
if err != nil {
return err
}
if c == nil || c.Provider != domain.StorageProviderBOS {
return fmt.Errorf("original BOS connection missing")
}
m, err := workspacescope.ForOrganization(run.OrganizationID)
if err != nil {
return err
}
if c.OrganizationID != run.OrganizationID {
if m == nil || m.ConnectionID != c.ID || m.ReadConnectionOrganizationID != c.OrganizationID || m.Bucket != ref.Bucket || !workspacescope.PrefixAllows(ref.Prefix, m.ReadPrefixes) {
return fmt.Errorf("original BOS connection outside delegated read scope")
}
}
if m != nil && m.ConnectionID == c.ID && (m.Bucket != ref.Bucket || !workspacescope.PrefixAllows(ref.Prefix, m.ReadPrefixes)) {
return fmt.Errorf("original BOS prefix outside read scope")
}
connection := *c
if connection.Endpoint == "" {
connection.Endpoint = r.globalBOSEndpoint
}
return selection.Recheck(ctx, func(ctx context.Context, bucket, key string) (domain.ObjectStorageObjectSummary, error) {
meta, err := r.outputArtifactPublisher().Head(ctx, connection, bucket, key)
kind := ""
if meta.Exists {
kind = "file"
}
return domain.ObjectStorageObjectSummary{Bucket: bucket, Key: key, Kind: kind, ETag: meta.ETag, SizeBytes: meta.SizeBytes}, err
})
}
func (r *Runtime) preflightV4OriginalSource(ctx context.Context, run *domain.RunRecord, node domain.WorkflowNode) error {
if !workflowUsesV4CaptureDelivery(domain.WorkflowRecord{Nodes: []domain.WorkflowNode{node}}) {
return nil
}
selection, err := r.originalDeliverySelection(ctx, run)
if err != nil {
return err
}
return r.recheckOriginalDeliverySelection(ctx, run, selection)
}
var errLegacyDeliveryRequiresV4 = errors.New("该工作流已启用 V4,禁止新发布旧格式交付包;请使用已验收数据重新生成 V4")
// Detect the actual output schema as well as the active format contract.
// This guard runs before prepareDeliveryTarget writes even its owner marker.
func (r *Runtime) recheckV4DeliveryArtifact(ctx context.Context, run *domain.RunRecord, artifact domain.ExecutionArtifact) error {
required := run != nil && run.Metadata != nil && run.Metadata.CaptureDeliveryPackage != nil
if !required && run != nil {
var err error
required, err = r.classificationDeliveryEnabled(ctx, run.WorkflowID)
if err != nil {
return err
}
}
legacy := func() error {
if required {
return errLegacyDeliveryRequiresV4
}
return nil
}
name := artifact.Path
info, err := os.Stat(name)
if err != nil {
return err
}
if info.IsDir() {
name = filepath.Join(name, "part.json")
} else if filepath.Base(name) != "part.json" {
return legacy()
}
f, err := os.Open(name)
if os.IsNotExist(err) {
return legacy()
} // Legacy directory has no part.json.
if err != nil {
return err
}
defer f.Close()
raw, err := io.ReadAll(io.LimitReader(f, partIndexMaxPeekBytes+1))
if err != nil {
return err
}
var part struct {
Schema string `json:"schemaVersion"`
Capture struct {
ID string `json:"captureId"`
Prefix string `json:"prefix"`
} `json:"capture"`
PartID string `json:"partId"`
OriginalSource struct {
Provider string `json:"provider"`
Bucket string `json:"bucket"`
Prefix string `json:"prefix"`
PartPrefix string `json:"partPrefix"`
Files map[string]struct {
Path string `json:"path"`
} `json:"files"`
} `json:"originalSource"`
}
if json.Unmarshal(raw, &part) != nil {
return fmt.Errorf("invalid delivery part JSON")
}
if part.Schema != deliveryindex.ClassificationPartSchema {
return legacy()
}
if info.IsDir() {
members, e := os.ReadDir(artifact.Path)
if e != nil {
return e
}
if len(members) != 1 || members[0].Name() != "part.json" || !members[0].Type().IsRegular() {
return fmt.Errorf("V4 客户交付目录必须只有 part.json")
}
}
if len(raw) > 8<<20 {
return fmt.Errorf("classification part exceeds 8 MiB")
}
selection, err := r.originalDeliverySelection(ctx, run)
if err != nil {
return err
}
if part.Capture.ID != filepath.Base(selection.Ref.Prefix) || part.Capture.Prefix != selection.Ref.Prefix || part.PartID != filepath.Base(selection.PartPrefix) {
return fmt.Errorf("classification capture/part differs from original run")
}
paths := map[string]string{}
for role, file := range part.OriginalSource.Files {
paths[role] = file.Path
}
if err = selection.MatchFiles(part.OriginalSource.Provider, part.OriginalSource.Bucket, part.OriginalSource.Prefix, part.OriginalSource.PartPrefix, paths); err != nil {
return err
}
return r.recheckOriginalDeliverySelection(ctx, run, selection)
}