80 lines
2.9 KiB
Go
80 lines
2.9 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"path"
|
|
"sort"
|
|
"strings"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
// Freeze only credential-free facts from the run's original input, never the
|
|
// current workflow configuration or a later source-directory listing.
|
|
func annotationOriginalSource(run *domain.RunRecord, capture *domain.CaptureRecord) (map[string]any, error) {
|
|
if run == nil || capture == nil || len(run.Inputs) == 0 {
|
|
return nil, nil
|
|
}
|
|
if capture.RunID != run.ID || capture.OrganizationID != run.OrganizationID {
|
|
return nil, fmt.Errorf("original capture scope mismatch")
|
|
}
|
|
root := capture.SourcePrefix
|
|
if root == "" {
|
|
root = capture.Prefix
|
|
}
|
|
var result map[string]any
|
|
for _, input := range run.Inputs {
|
|
ref := input.Ref
|
|
if ref == nil || ref.Provider != domain.StorageProviderBOS || ref.Kind != domain.ArtifactRefKindDirectory || ref.Bucket != capture.Bucket || ref.Prefix != root {
|
|
continue
|
|
}
|
|
if ref.CaptureUnit != "" && ref.CaptureUnit != capture.CaptureID {
|
|
continue
|
|
}
|
|
if len(ref.ObjectKeys) == 0 {
|
|
continue
|
|
} // Older non-selected directory inputs need explicit recovery.
|
|
if root == "" || path.Clean(root) != root || strings.HasPrefix(root, "/") || strings.Contains(root, "\\") {
|
|
return nil, fmt.Errorf("unsafe original capture prefix")
|
|
}
|
|
keys := append([]string(nil), ref.ObjectKeys...)
|
|
sort.Strings(keys)
|
|
for i, key := range keys {
|
|
if !strings.HasPrefix(key, root+"/") || path.Clean(key) != key || strings.Contains(key, "\\") || i > 0 && key == keys[i-1] {
|
|
return nil, fmt.Errorf("unsafe original capture object selection")
|
|
}
|
|
relative := strings.TrimPrefix(key, root+"/")
|
|
if strings.Contains(relative, "/") && !strings.HasPrefix(key, capture.Prefix+"/") {
|
|
return nil, fmt.Errorf("original capture selection includes another part")
|
|
}
|
|
}
|
|
candidate := map[string]any{"provider": string(ref.Provider), "bucket": ref.Bucket, "prefix": root, "partPrefix": capture.Prefix, "captureUnit": capture.CaptureID, "objectKeys": keys, "snapshotFingerprint": ref.VersionID}
|
|
if result != nil {
|
|
a, _ := json.Marshal(result)
|
|
b, _ := json.Marshal(candidate)
|
|
if string(a) != string(b) {
|
|
return nil, fmt.Errorf("ambiguous original capture inputs")
|
|
}
|
|
}
|
|
result = candidate
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// Source provenance is optional for legacy materialization, but mandatory at
|
|
// v4 export. Never invent a part selection from an ambiguous capture input.
|
|
// Tenant/run violations are still fatal; storage failures remain with callers.
|
|
func annotationRevisionOriginalSource(run *domain.RunRecord, capture *domain.CaptureRecord) (map[string]any, error) {
|
|
source, err := annotationOriginalSource(run, capture)
|
|
if err == nil {
|
|
return source, nil
|
|
}
|
|
if run != nil && capture != nil && (capture.RunID != run.ID || capture.OrganizationID != run.OrganizationID) {
|
|
return nil, err
|
|
}
|
|
log.Printf("[api] original_source_evidence_unavailable reason=%s", err)
|
|
return nil, nil
|
|
}
|