#!/usr/bin/env bash set -euo pipefail ROOT="$(cd "$(dirname "$0")/.." && pwd)" MANIFEST_PATH="${1:-}" SCHEMA_PATH="$ROOT/schemas/continuity_slice_manifest_v0.json" if [ -z "$MANIFEST_PATH" ]; then echo "usage: ./runtime/materialize_continuity_slice.sh " >&2 exit 1 fi if [ ! -f "$MANIFEST_PATH" ]; then echo "continuity manifest not found: $MANIFEST_PATH" >&2 exit 1 fi if [ "$(jq -r '.version' "$MANIFEST_PATH")" != "continuity_slice_manifest_v0" ]; then echo "unsupported continuity manifest: $MANIFEST_PATH" >&2 exit 1 fi MANIFEST_ABS="$(cd "$(dirname "$MANIFEST_PATH")" && pwd)/$(basename "$MANIFEST_PATH")" CONTINUITY_VERSION_ID="$(jq -r '.continuity_version_id' "$MANIFEST_ABS")" PREDECESSOR_ID="$(jq -r '.predecessor_id' "$MANIFEST_ABS")" ONE_LINER="$(jq -r '.one_liner' "$MANIFEST_ABS")" DEFAULT_SURFACE="$(jq -r '.default_surface // (.surfaces[0].id // "")' "$MANIFEST_ABS")" OUT_DIR="$ROOT/runs/continuity/$CONTINUITY_VERSION_ID" WORK_MANIFEST_PATH="$OUT_DIR/work_manifest.json" LINEAGE_REL="runs/continuity/$CONTINUITY_VERSION_ID/lineage.json" SURFACES_REL="runs/continuity/$CONTINUITY_VERSION_ID/surface_registry.json" POINTER_REL="runs/continuity/latest.json" GENERATED_AT="$(date -u +"%Y-%m-%dT%H:%M:%SZ")" mkdir -p "$OUT_DIR" ENDPOINTS_JSON="$(jq -c '.endpoints' "$MANIFEST_ABS")" SURFACES_JSON="$(jq -c '.surfaces' "$MANIFEST_ABS")" RECEIPTS_JSON="$(jq -c '.receipts_required // []' "$MANIFEST_ABS")" EVAL_GATE_JSON="$(jq -c '.eval_gate // []' "$MANIFEST_ABS")" SURFACE_IDS_JSON="$(jq -c '[.surfaces[].id]' "$MANIFEST_ABS")" ENDPOINT_IDS_JSON="$(jq -c '[.endpoints[].id]' "$MANIFEST_ABS")" LINEAGE_CONTENT="$(jq -n \ --arg version "continuity_lineage_v0" \ --arg generated_at "$GENERATED_AT" \ --arg continuity_version_id "$CONTINUITY_VERSION_ID" \ --arg predecessor_id "$PREDECESSOR_ID" \ --arg one_liner "$ONE_LINER" \ --arg manifest_path "$MANIFEST_ABS" \ --argjson endpoints "$ENDPOINTS_JSON" \ --argjson receipts_required "$RECEIPTS_JSON" \ --argjson eval_gate "$EVAL_GATE_JSON" \ '{ version:$version, generated_at:$generated_at, continuity_version_id:$continuity_version_id, predecessor_id:$predecessor_id, one_liner:$one_liner, manifest_path:$manifest_path, endpoints:$endpoints, receipts_required:$receipts_required, eval_gate:$eval_gate }' | jq '.')" SURFACES_CONTENT="$(jq -n \ --arg version "continuity_surface_registry_v0" \ --arg generated_at "$GENERATED_AT" \ --arg continuity_version_id "$CONTINUITY_VERSION_ID" \ --arg default_surface "$DEFAULT_SURFACE" \ --argjson surfaces "$SURFACES_JSON" \ '{ version:$version, generated_at:$generated_at, continuity_version_id:$continuity_version_id, default_surface:$default_surface, surfaces:$surfaces }' | jq '.')" POINTER_CONTENT="$(jq -n \ --arg version "continuity_pointer_v0" \ --arg updated_at "$GENERATED_AT" \ --arg continuity_version_id "$CONTINUITY_VERSION_ID" \ --arg predecessor_id "$PREDECESSOR_ID" \ --arg lineage_path "$LINEAGE_REL" \ --arg surface_registry_path "$SURFACES_REL" \ --arg one_liner "$ONE_LINER" \ --arg default_surface "$DEFAULT_SURFACE" \ --argjson surface_ids "$SURFACE_IDS_JSON" \ --argjson endpoint_ids "$ENDPOINT_IDS_JSON" \ '{ version:$version, updated_at:$updated_at, continuity_version_id:$continuity_version_id, predecessor_id:$predecessor_id, one_liner:$one_liner, default_surface:$default_surface, lineage_path:$lineage_path, surface_registry_path:$surface_registry_path, surface_ids:$surface_ids, endpoint_ids:$endpoint_ids, status:"materialized" }' | jq '.')" jq -n \ --arg manifest_id "materialize-$CONTINUITY_VERSION_ID" \ --arg goal "materialize continuity slice $CONTINUITY_VERSION_ID" \ --arg lineage_path "$LINEAGE_REL" \ --arg surfaces_path "$SURFACES_REL" \ --arg pointer_path "$POINTER_REL" \ --arg lineage_content "$LINEAGE_CONTENT" \ --arg surfaces_content "$SURFACES_CONTENT" \ --arg pointer_content "$POINTER_CONTENT" \ '{ version:"work_manifest_v0", manifest_id:$manifest_id, goal:$goal, lane:"execution", execution_gate:true, receipt_required:true, actions:[ {type:"write_file", path:$lineage_path, content:$lineage_content}, {type:"write_file", path:$surfaces_path, content:$surfaces_content}, {type:"write_file", path:$pointer_path, content:$pointer_content} ] }' > "$WORK_MANIFEST_PATH" printf '%s\n' "$WORK_MANIFEST_PATH"