#!/usr/bin/env bash set -euo pipefail ROOT="$(cd "$(dirname "$0")/.." && pwd)" OUT_PATH="${1:-}" API_CONTRACT="$ROOT/api/conversational_api_v0.json" CONTEXT_CONTRACT="$ROOT/api/system_context_v0.json" RUNTIME_CONTRACT="$ROOT/runtime/work_manifest_v0.json" MANIFEST_SCHEMA="$ROOT/runtime/work_manifest_packet_v0.json" CONTROL_LANGUAGE="$ROOT/policy/control_language_v0.json" RUNTIME_PROFILES="$ROOT/policy/runtime_profiles_v0.json" VSM_POLICY="$ROOT/policy/vsm_policy_v0.json" PRODUCT_SLICES="$ROOT/configs/product_slices.json" latest_runtime_graph() { find "$ROOT/runs/runtime" -name graph_state.json -type f 2>/dev/null | sort | tail -n 1 } GRAPH_PATH="$(latest_runtime_graph)" RECEIPT_PATH="" SUMMARY_PATH="" LATEST_MANIFEST_ID="" LATEST_LANE="" LATEST_EFFECT_COUNT=0 if [ -n "$GRAPH_PATH" ] && [ -f "$GRAPH_PATH" ]; then GRAPH_DIR="$(cd "$(dirname "$GRAPH_PATH")" && pwd)" if [ -f "$GRAPH_DIR/receipt.json" ]; then RECEIPT_PATH="$GRAPH_DIR/receipt.json" fi if [ -f "$GRAPH_DIR/summary.json" ]; then SUMMARY_PATH="$GRAPH_DIR/summary.json" fi LATEST_MANIFEST_ID="$(jq -r '.manifest_id // ""' "$GRAPH_PATH")" LATEST_LANE="$(jq -r '.lane // ""' "$GRAPH_PATH")" LATEST_EFFECT_COUNT="$(jq -r '(.effects // []) | length' "$GRAPH_PATH")" fi build_context() { jq -n \ --slurpfile api "$API_CONTRACT" \ --slurpfile context "$CONTEXT_CONTRACT" \ --slurpfile runtime "$RUNTIME_CONTRACT" \ --slurpfile manifest "$MANIFEST_SCHEMA" \ --slurpfile control "$CONTROL_LANGUAGE" \ --slurpfile profiles "$RUNTIME_PROFILES" \ --slurpfile vsm "$VSM_POLICY" \ --slurpfile slices "$PRODUCT_SLICES" \ --arg generated_at "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \ --arg graph_path "$GRAPH_PATH" \ --arg receipt_path "$RECEIPT_PATH" \ --arg summary_path "$SUMMARY_PATH" \ --arg latest_manifest_id "$LATEST_MANIFEST_ID" \ --arg latest_lane "$LATEST_LANE" \ --argjson latest_effect_count "$LATEST_EFFECT_COUNT" \ '{ version:"system_context_v0", generated_at:$generated_at, one_liner:$context[0].one_liner, entrypoint:{ api:"POST /turn", cli:"bvtctl", contract_ref:"api/conversational_api_v0.json", why:"all agent interaction should start from one front door before widening into internal slices" }, current_position:{ slice_id:$vsm[0].identity.entry_slice, default_profile:$vsm[0].identity.default_profile, lineage_view:"operator_lineage", role:"single front-door operator over the whole system" }, lineage:{ views:$vsm[0].lineage_views, slice_map:($slices[0].slices | map({ id, path, purpose, upstream_reference })) }, taxonomy:$vsm[0].taxonomy, policy:{ vsm:$vsm[0], control_language:{ product_read:$control[0].product_read, meta_modes:$control[0].meta_modes, bits:$control[0].bits, vectors:$control[0].vectors, tensors:$control[0].tensors, invariants:$control[0].invariants }, runtime_profiles:$profiles[0].profiles }, runtime_contract:{ one_liner:$runtime[0].one_liner, contract:$runtime[0].runtime_contract, acceptance_bar:$runtime[0].acceptance_bar }, payload_templates:{ turn_request:{ endpoint:"POST /turn", auto_hydrated_fields:[ "system_context", "policy_context", "lineage_context" ], user_supplied_fields:["user_input"], optional_state_fields:$api[0].request_contract.fields, template:( $api[0].request_contract.fields | map({key:., value:null}) | from_entries | .user_input = "" ) }, turn_response_fields:$api[0].response_contract.fields, work_manifest:{ schema_title:$manifest[0].title, required_fields:$manifest[0].required, template:{ version:"work_manifest_v0", manifest_id:"", goal:"", lane:"", execution_gate:false, receipt_required:true, actions:[ { type:"write_file", path:"", content:"" } ] } } }, latest_runtime_state:{ graph_state_path:(if $graph_path == "" then null else $graph_path end), receipt_path:(if $receipt_path == "" then null else $receipt_path end), summary_path:(if $summary_path == "" then null else $summary_path end), latest_manifest_id:(if $latest_manifest_id == "" then null else $latest_manifest_id end), latest_lane:(if $latest_lane == "" then null else $latest_lane end), latest_effect_count:$latest_effect_count }, agent_bootstrap:{ trust_order:[ "graph_state", "receipts", "policy", "runtime_contract", "docs" ], first_move:"read this packet, stay inside POST /turn, and escalate to manifests only when policy opens the gate", promise:$vsm[0].identity.operator_promise } }' } if [ -n "$OUT_PATH" ]; then mkdir -p "$(dirname "$OUT_PATH")" build_context > "$OUT_PATH" printf '%s\n' "$OUT_PATH" else build_context fi