157 lines
5.3 KiB
Go
157 lines
5.3 KiB
Go
package worker
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"droboticflow/internal/deliveryindex"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/sourceidentity"
|
|
"droboticflow/internal/workspacescope"
|
|
)
|
|
|
|
// Release children carry accepted artifacts but no original Inputs. Resolve
|
|
// only the persisted human-release edge; retries are independent selections.
|
|
func (r *Runtime) originalDeliverySelection(ctx context.Context, run *domain.RunRecord) (*sourceidentity.Selection, error) {
|
|
if run == nil {
|
|
return nil, fmt.Errorf("missing original delivery run")
|
|
}
|
|
original := run
|
|
if run.Trigger == domain.RunTriggerHumanRelease {
|
|
parent, err := r.store.GetRun(ctx, run.ParentRunID)
|
|
if err != nil {
|
|
return 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, fmt.Errorf("original delivery parent scope mismatch")
|
|
}
|
|
original = parent
|
|
}
|
|
capture, err := r.store.GetCaptureRecordByRunID(ctx, original.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if capture == nil || capture.ID != original.TaskID || capture.RunID != original.ID && capture.RunID != run.ID {
|
|
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)
|
|
}
|
|
|
|
// Detect the actual output schema, independent of mutable workflow switches.
|
|
// This guard runs before prepareDeliveryTarget writes even its owner marker.
|
|
func (r *Runtime) recheckV4DeliveryArtifact(ctx context.Context, run *domain.RunRecord, artifact domain.ExecutionArtifact) error {
|
|
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 nil
|
|
}
|
|
f, err := os.Open(name)
|
|
if os.IsNotExist(err) {
|
|
return nil
|
|
} // 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 nil
|
|
}
|
|
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)
|
|
}
|