Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions protocol/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,20 @@ func (c RuntimeAdapterContract) SupportsAdapterKind(kind RuntimeAdapterKind) boo

const MaxRuntimeResultPreviewBytes = 16 * 1024

func ValidateRuntimeResultPreview(preview map[string]any) error {
if len(preview) == 0 {
return nil
}
data, err := json.Marshal(preview)
if err != nil {
return fmt.Errorf("result_preview must be JSON-serializable: %w", err)
}
if len(data) > MaxRuntimeResultPreviewBytes {
return fmt.Errorf("result_preview must be at most %d bytes", MaxRuntimeResultPreviewBytes)
}
return nil
}

type RuntimeExecutionResult struct {
StartedAt time.Time `json:"started_at,omitempty"`
FinishedAt time.Time `json:"finished_at,omitempty"`
Expand All @@ -480,13 +494,8 @@ func (r RuntimeExecutionResult) Validate() error {
if r.ArtifactHash != "" && !validSHA256Ref(r.ArtifactHash) {
errs = append(errs, errors.New("artifact_hash must be sha256:<64 hex chars>"))
}
if len(r.ResultPreview) > 0 {
data, err := json.Marshal(r.ResultPreview)
if err != nil {
errs = append(errs, fmt.Errorf("result_preview must be JSON-serializable: %w", err))
} else if len(data) > MaxRuntimeResultPreviewBytes {
errs = append(errs, fmt.Errorf("result_preview must be at most %d bytes", MaxRuntimeResultPreviewBytes))
}
if err := ValidateRuntimeResultPreview(r.ResultPreview); err != nil {
errs = append(errs, err)
}
return errors.Join(errs...)
}
Expand Down
12 changes: 12 additions & 0 deletions protocol/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ func TestRuntimeExecutionResultValidatesTimingAndPreview(t *testing.T) {
}
}

func TestValidateRuntimeResultPreview(t *testing.T) {
if err := protocol.ValidateRuntimeResultPreview(map[string]any{"ok": true}); err != nil {
t.Fatalf("bounded preview rejected: %v", err)
}
if err := protocol.ValidateRuntimeResultPreview(map[string]any{"payload": strings.Repeat("x", protocol.MaxRuntimeResultPreviewBytes+1)}); err == nil || !strings.Contains(err.Error(), "result_preview") {
t.Fatalf("expected oversized preview error, got %v", err)
}
if err := protocol.ValidateRuntimeResultPreview(map[string]any{"bad": func() {}}); err == nil || !strings.Contains(err.Error(), "JSON-serializable") {
t.Fatalf("expected JSON serialization error, got %v", err)
}
}

func TestRuntimeServiceResultValidatesSLOEvidence(t *testing.T) {
result := protocol.RuntimeServiceResult{
StartedAt: time.Unix(1, 0).UTC(),
Expand Down
Loading