File size: 5,492 Bytes
3436bdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/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 = "<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:"<manifest_id>",
            goal:"<goal>",
            lane:"<lane>",
            execution_gate:false,
            receipt_required:true,
            actions:[
              {
                type:"write_file",
                path:"<path>",
                content:"<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