{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "spec-first spec-work run artifact producer-available contract",
  "description": "Source-owned write-side contract. The internal producer writes v2 run.json artifacts for fresh workspace/run-id pairs, spec-work closeout can mark artifacts workflow_integrated when a durable evidence trigger calls the producer, and read/prune consumers remain compatible with v1 artifacts.",
  "x-spec-first-contract-status": "producer_available",
  "x-spec-first-producer": "internal spec-work-run-artifact write",
  "x-spec-first-producer-available": true,
  "x-spec-first-workflow-integrated": true,
  "x-spec-first-runtime-path": ".spec-first/workflows/spec-work/<workspace-slug>/<run-id>/run.json",
  "x-spec-first-boundary": "source-owned write-side producer; same workspace/run-id artifacts are immutable and return artifact-already-exists instead of overwriting; workflow_integrated true only when spec-work closeout calls the producer with durable evidence trigger reason_code",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "generated_at",
    "workflow",
    "run_id",
    "mode",
    "workspace_slug",
    "producer",
    "plan_path",
    "plan_source",
    "task_pack_path",
    "source_refs",
    "script_confirmed",
    "llm_asserted",
    "provider_untrusted",
    "retention",
    "artifact_path",
    "warnings"
  ],
  "properties": {
    "schema_version": {
      "type": "string",
      "enum": ["spec-work-run-artifact/v1", "spec-work-run-artifact/v2"]
    },
    "generated_at": {
      "type": "string"
    },
    "workflow": {
      "type": "string",
      "const": "spec-work"
    },
    "run_id": {
      "type": "string",
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._-]{0,80}$"
    },
    "mode": {
      "type": "string",
      "enum": ["interactive", "non-interactive"]
    },
    "workspace_slug": {
      "type": "string"
    },
    "producer": {
      "type": "object",
      "additionalProperties": false,
      "required": ["producer_available", "workflow_integrated", "reason_code"],
      "properties": {
        "producer_available": { "type": "boolean", "const": true },
        "workflow_integrated": { "type": "boolean", "enum": [true, false] },
        "reason_code": {
          "type": "string",
          "enum": [
            "trigger-task-pack",
            "trigger-not-run-validation",
            "trigger-deferred-follow-up",
            "trigger-substantive-work",
            "no-trigger-matched",
            "producer-error",
            "producer-write-side-only"
          ]
        }
      },
      "allOf": [
        {
          "if": {
            "type": "object",
            "required": ["workflow_integrated"],
            "properties": {
              "workflow_integrated": { "const": true }
            }
          },
          "then": {
            "type": "object",
            "properties": {
              "reason_code": {
                "enum": [
                  "trigger-task-pack",
                  "trigger-not-run-validation",
                  "trigger-deferred-follow-up",
                  "trigger-substantive-work"
                ]
              }
            }
          }
        },
        {
          "if": {
            "type": "object",
            "required": ["workflow_integrated"],
            "properties": {
              "workflow_integrated": { "const": false }
            }
          },
          "then": {
            "type": "object",
            "properties": {
              "reason_code": {
                "enum": [
                  "no-trigger-matched",
                  "producer-error",
                  "producer-write-side-only"
                ]
              }
            }
          }
        }
      ]
    },
    "plan_path": {
      "anyOf": [
        {
          "type": "string",
          "pattern": "^(?!/)(?![A-Za-z]:[\\\\/])(?!.*\\\\)(?!.*//)(?!.*(?:^|/)\\.\\.?(?:/|$))(?!\\.claude/)(?!\\.codex/)(?!\\.agents/skills/)(?!\\.spec-first/).+[^/]$"
        },
        { "type": "null" }
      ]
    },
    "plan_source": {
      "type": "string",
      "enum": ["explicit", "inferred", "missing"]
    },
    "task_pack_path": {
      "anyOf": [
        {
          "type": "string",
          "pattern": "^(?!/)(?![A-Za-z]:[\\\\/])(?!.*\\\\)(?!.*//)(?!.*(?:^|/)\\.\\.?(?:/|$))(?!\\.claude/)(?!\\.codex/)(?!\\.agents/skills/)(?!\\.spec-first/).+[^/]$"
        },
        { "type": "null" }
      ]
    },
    "source_refs": {
      "type": "array",
      "items": {
        "type": "string",
        "pattern": "^(?!/)(?![A-Za-z]:[\\\\/])(?!.*\\\\)(?!.*//)(?!.*(?:^|/)\\.\\.?(?:/|$))(?!\\.claude/)(?!\\.codex/)(?!\\.agents/skills/)(?!\\.spec-first/).+[^/]$"
      }
    },
    "script_confirmed": {
      "type": "object",
      "additionalProperties": false,
      "required": ["validation", "changed_files", "artifact_refs", "raw_log_ref", "resume_evidence"],
      "properties": {
        "validation": {
          "type": "object"
        },
        "changed_files": {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^(?!/)(?![A-Za-z]:[\\\\/])(?!.*\\\\)(?!.*//)(?!.*(?:^|/)\\.\\.?(?:/|$))(?!\\.claude/)(?!\\.codex/)(?!\\.agents/skills/)(?!\\.spec-first/).+[^/]$"
          }
        },
        "artifact_refs": {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^(?!/)(?![A-Za-z]:[\\\\/])(?!.*\\\\)(?!.*//)(?!.*(?:^|/)\\.\\.?(?:/|$))(?!\\.claude/)(?!\\.codex/)(?!\\.agents/skills/)(?!\\.spec-first/(?!workflows/)).+[^/]$"
          }
        },
        "raw_log_ref": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "kind",
            "display_ref",
            "secret_stripped",
            "redaction_status",
            "retention_status",
            "access_boundary",
            "reason_code"
          ],
          "properties": {
            "kind": { "type": "string", "enum": ["none", "repo_relative_artifact"] },
            "display_ref": { "type": "string" },
            "secret_stripped": { "type": "boolean", "const": true },
            "redaction_status": { "type": "string", "enum": ["redacted", "none-required"] },
            "retention_status": { "type": "string", "const": "lifecycle-deferred" },
            "access_boundary": { "type": "string", "enum": ["repo-local", "none"] },
            "reason_code": { "type": "string" }
          }
        },
        "resume_evidence": {
          "type": "object",
          "additionalProperties": false,
          "required": ["status"],
          "properties": {
            "status": { "type": "string", "enum": ["read", "not-found", "not-readable", "not-run"] },
            "reason_code": { "type": "string" }
          }
        }
      }
    },
    "llm_asserted": {
      "type": "object",
      "additionalProperties": false,
      "required": ["summary", "read_artifacts", "key_decisions", "deferred_follow_up", "next_action"],
      "properties": {
        "summary": { "type": "string", "maxLength": 1000 },
        "read_artifacts": {
          "type": "array",
          "items": {
            "type": "string",
            "maxLength": 500,
            "pattern": "^(?!/)(?![A-Za-z]:[\\\\/])(?!.*\\\\)(?!.*//)(?!.*(?:^|/)\\.\\.?(?:/|$))(?!\\.claude/)(?!\\.codex/)(?!\\.agents/skills/)(?!\\.spec-first/(?!workflows/)).+[^/]$"
          }
        },
        "key_decisions": {
          "type": "array",
          "items": { "type": "string", "maxLength": 500 }
        },
        "deferred_follow_up": {
          "type": "array",
          "items": { "type": "string", "maxLength": 500 }
        },
        "next_action": { "type": "string", "maxLength": 500 }
      }
    },
    "provider_untrusted": {
      "type": "object",
      "additionalProperties": false,
      "required": ["readiness_status", "summaries"],
      "properties": {
        "readiness_status": { "type": "string", "enum": ["fresh", "stale", "degraded", "not-run", "unknown"] },
        "summaries": {
          "type": "array",
          "maxItems": 20,
          "items": { "type": "string", "maxLength": 500 }
        }
      }
    },
    "graph_evidence_used": {
      "type": ["object", "null"],
      "description": "Legacy compact graph evidence summary kept for v1 artifact read/prune compatibility. New v2 producer payloads use direct_evidence_used unless a future graph contract is explicitly reintroduced.",
      "additionalProperties": false,
      "required": [
        "capabilities_used",
        "evidence_grade",
        "evidence_posture",
        "freshness_state",
        "repo_scope",
        "graph_findings_applied",
        "graph_findings_as_risk_only",
        "source_reads_validated",
        "redaction_status"
      ],
      "properties": {
        "capabilities_used": {
          "type": "array",
          "maxItems": 20,
          "items": { "type": "string", "maxLength": 160 }
        },
        "lanes_used": {
          "type": "array",
          "maxItems": 8,
          "items": {
            "type": "string",
            "enum": [
              "deterministic-helper",
              "workflow-native-session",
              "workspace-resource",
              "mutation-gated-maintenance"
            ]
          }
        },
        "evidence_grade": {
          "type": "string",
          "enum": ["primary", "session-local", "advisory", "stale"]
        },
        "evidence_posture": {
          "type": "string",
          "enum": ["primary", "fallback"]
        },
        "freshness_state": {
          "type": "string",
          "enum": ["fresh", "stale", "dirty-advisory", "query-unverified"]
        },
        "repo_scope": { "type": "string", "maxLength": 160 },
        "graph_findings_applied": {
          "type": "array",
          "maxItems": 20,
          "items": { "type": "string", "maxLength": 300 }
        },
        "graph_findings_as_risk_only": {
          "type": "array",
          "maxItems": 20,
          "items": { "type": "string", "maxLength": 300 }
        },
        "graph_findings_rejected_after_source_read": {
          "type": "array",
          "maxItems": 20,
          "items": { "type": "string", "maxLength": 300 }
        },
        "source_reads_required": {
          "type": "array",
          "maxItems": 20,
          "items": { "type": "string", "maxLength": 300 }
        },
        "source_reads_validated": {
          "type": "array",
          "maxItems": 20,
          "items": { "type": "string", "maxLength": 300 }
        },
        "redaction_status": {
          "type": "string",
          "enum": ["redacted", "none-required"]
        }
      }
    },
    "direct_evidence_used": {
      "type": ["object", "null"],
      "description": "Compact direct source/test/log evidence summary from plan intake or work closeout. Absent means the ordinary changed-file/test summary was sufficient. This is advisory Harness evidence, not scope authority.",
      "additionalProperties": false,
      "required": [
        "source_refs",
        "checks_or_logs",
        "repo_scope",
        "limitations",
        "redaction_status"
      ],
      "properties": {
        "source_refs": {
          "type": "array",
          "maxItems": 20,
          "items": { "type": "string", "maxLength": 300 }
        },
        "repo_scope": { "type": "string", "maxLength": 160 },
        "checks_or_logs": {
          "type": "array",
          "maxItems": 20,
          "items": { "type": "string", "maxLength": 300 }
        },
        "limitations": {
          "type": "array",
          "maxItems": 20,
          "items": { "type": "string", "maxLength": 300 }
        },
        "redaction_status": {
          "type": "string",
          "enum": ["redacted", "none-required"]
        }
      }
    },
    "retention": {
      "type": "object",
      "additionalProperties": false,
      "required": ["retention_status", "artifact_category", "raw_log_retention_impact", "redaction_status"],
      "properties": {
        "retention_status": { "type": "string", "const": "lifecycle-deferred" },
        "artifact_category": { "type": "string", "const": "spec-work-run-evidence" },
        "raw_log_retention_impact": { "type": "string", "enum": ["none", "repo-relative-redacted-ref"] },
        "redaction_status": { "type": "string", "enum": ["redacted", "none-required"] },
        "owner": { "type": "string" },
        "expires_at": { "type": "string" }
      }
    },
    "artifact_path": {
      "type": "string",
      "pattern": "^\\.spec-first/workflows/spec-work/[A-Za-z0-9._-]+/[A-Za-z0-9._-]+/run\\.json$"
    },
    "warnings": {
      "type": "array",
      "items": { "type": "string" }
    }
  },
  "$defs": {
    "validationV1": {
      "type": "object",
      "additionalProperties": false,
      "required": ["status", "commands"],
      "properties": {
        "status": { "type": "string", "enum": ["passed", "failed", "not-run", "degraded"] },
        "reason_code": { "type": "string" },
        "commands": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["command", "exit_code", "summary"],
            "properties": {
              "command": { "type": "string" },
              "exit_code": { "type": "integer" },
              "summary": { "type": "string", "maxLength": 500 }
            }
          }
        }
      }
    },
    "validationV2": {
      "type": "object",
      "additionalProperties": false,
      "required": ["status", "reason_code", "run_summary_ref"],
      "properties": {
        "status": { "type": "string", "enum": ["passed", "failed", "not-run", "degraded"] },
        "reason_code": { "type": "string" },
        "run_summary_ref": {
          "type": "string",
          "pattern": "^\\.spec-first/workflows/spec-work/[A-Za-z0-9._-]+/[A-Za-z0-9._-]+/verification-run-summary\\.json$"
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "type": "object",
        "required": ["schema_version"],
        "properties": {
          "schema_version": { "const": "spec-work-run-artifact/v1" }
        }
      },
      "then": {
        "type": "object",
        "properties": {
          "script_confirmed": {
            "type": "object",
            "properties": {
              "validation": { "$ref": "#/$defs/validationV1" }
            }
          }
        }
      }
    },
    {
      "if": {
        "type": "object",
        "required": ["schema_version"],
        "properties": {
          "schema_version": { "const": "spec-work-run-artifact/v2" }
        }
      },
      "then": {
        "type": "object",
        "properties": {
          "script_confirmed": {
            "type": "object",
            "properties": {
              "validation": { "$ref": "#/$defs/validationV2" }
            }
          }
        }
      }
    }
  ]
}
