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

119 lines
4.1 KiB
Go

package worker
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"path/filepath"
"droboticflow/internal/domain"
"droboticflow/internal/store"
)
// Internal evidence is pinned with the accepted package, independently from
// customer classification JSON. Never resolve it from a later live workspace.
func (runtime *Runtime) deliveryAuditTrace(ctx context.Context, org string, items []domain.HumanItemRecord) ([]map[string]any, error) {
trace := []map[string]any{}
seen := map[string]bool{}
for _, item := range items {
if seen[item.ID] {
continue
}
seen[item.ID] = true
if item.OrganizationID != org || item.ReviewRound > 10000 {
return nil, fmt.Errorf("invalid audit item scope/round")
}
attempts, err := runtime.store.ListHumanStageAttempts(ctx, org, item.ID)
if err != nil {
return nil, err
}
events, err := runtime.store.ListHumanEvents(ctx, org, item.ID)
if err != nil {
return nil, err
}
rejects, err := runtime.store.ListHumanRejects(ctx, org, domain.HumanRejectQuery{ItemID: item.ID})
if err != nil {
return nil, err
}
baselines := []domain.ReworkBaselineRecord{}
handlingRecords := []domain.ReworkHandlingRecord{}
for round := 1; round <= item.ReviewRound; round++ {
baseline, err := runtime.store.GetReworkBaseline(ctx, store.ReworkBaselineID(item.ID, round))
if err != nil {
return nil, err
}
handling, err := runtime.store.GetReworkHandling(ctx, store.ReworkHandlingID(item.ID, round))
if err != nil {
return nil, err
}
if handling != nil {
handlingRecords = append(handlingRecords, *handling)
}
if baseline != nil {
baselines = append(baselines, *baseline)
}
}
trace = append(trace, map[string]any{"humanItemId": item.ID, "taskId": item.LegacyTaskID, "revision": item.CurrentRevision, "reviewRound": item.ReviewRound, "stageAttempts": attempts, "events": events, "rejects": rejects, "reworkBaselines": baselines, "reworkHandling": handlingRecords})
}
body, err := json.Marshal(trace)
if err != nil {
return nil, err
}
if len(body) > 6<<20 {
return nil, fmt.Errorf("internal delivery trace exceeds 6 MiB; refusing incomplete export")
}
return trace, nil
}
func (runtime *Runtime) classificationDeliveryEnabled(ctx context.Context, workflowID string) (bool, error) {
workflow, err := runtime.store.GetWorkflow(ctx, workflowID)
if err != nil {
return false, err
}
if workflow == nil {
return false, nil
}
if workflowUsesV4CaptureDelivery(*workflow) {
return true, nil
}
policy, err := runtime.activeV4DeliveryPolicy(ctx, workflow)
return policy != nil, err
}
// Reuse normal internal-artifact persistence rather than exceeding the 64 KiB
// inline transport limit with a multi-round audit. Customer publication is a
// different path and never sees this source artifact.
func (runtime *Runtime) persistClassificationSummary(ctx context.Context, run *domain.RunRecord, node domain.WorkflowNode, partitionID string, artifact domain.ExecutionArtifact, body []byte) (domain.ExecutionArtifact, error) {
if len(body) > 8<<20 {
return artifact, fmt.Errorf("acceptance audit summary exceeds 8 MiB")
}
if len(body) <= domain.MaxInlinePayloadBytes && !structuredJSONRequiresReference(artifact, artifact.Data) {
return artifact, nil
}
if runtime.workspaceRoot == "" {
return artifact, fmt.Errorf("classification audit requires a persistent workspace")
}
sum := sha256.Sum256(body)
directory := filepath.Join(runtime.workspaceRoot, "system", "runs", run.ID, "release-audits", artifactSlug(partitionID), hex.EncodeToString(sum[:]))
if err := os.MkdirAll(directory, 0700); err != nil {
return artifact, err
}
artifact.Path = filepath.Join(directory, "acceptance-summary.json")
if err := os.WriteFile(artifact.Path, body, 0600); err != nil {
return artifact, err
}
artifact.Payload = nil
workflow, err := runtime.store.GetWorkflow(ctx, run.WorkflowID)
if err != nil {
return artifact, err
}
result, err := runtime.normalizeOutputArtifacts(ctx, workflow, run, node.ID, []domain.ExecutionArtifact{artifact})
if err != nil {
return artifact, err
}
return result[0], nil
}