CoolFace
Apppublic

J94/bit-vector-tensor-control-policy

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
run_turn.sh400 linesDownload Raw Back to api
1#!/usr/bin/env bash2set -euo pipefail3 4ROOT="$(cd "$(dirname "$0")/.." && pwd)"5USER_INPUT="${1:-}"6MANIFEST_PATH="${2:-}"7INFERENCE_CONFIG_PATH="$ROOT/inference.yaml"8INFERENCE_SCHEMA_PATH="$ROOT/schemas/inference_output_v0.json"9 10if [ -z "$USER_INPUT" ]; then11  echo "usage: ./api/run_turn.sh <user_input> [manifest.json]" >&212  exit 113fi14 15TURN_ID="$(date -u +"%Y%m%dT%H%M%SZ")-$$"16OUT_DIR="$ROOT/runs/api/$TURN_ID"17RESPONSE_PATH="$OUT_DIR/response.json"18SYSTEM_CONTEXT_PATH="$OUT_DIR/system_context.json"19PROMPT_PATH="$OUT_DIR/inference_prompt.txt"20INFERENCE_PATH="$OUT_DIR/inference.json"21 22mkdir -p "$OUT_DIR"23"$ROOT/api/build_system_context.sh" "$SYSTEM_CONTEXT_PATH" >/dev/null24 25INPUT_LOWER="$(printf '%s' "$USER_INPUT" | tr '[:upper:]' '[:lower:]')"26CONTINUITY_PROVIDER_SCRIPT="$ROOT/scripts/continuity_provider.sh"27 28ANSWER_TEXT=""29ABSTAINED=false30INFERENCE_AVAILABLE=false31INFERENCE_RESULT_JSON='null'32PACKET_POINTER_PATH=""33PACKET_REGISTRY_PATH=""34PACKET_CONTEXT_JSON='null'35TENSOR_STRONGEST_SOURCE_OVERRIDE=""36TENSOR_DOMINANT_STAGE_OVERRIDE=""37TENSOR_WEAKEST_METRIC_OVERRIDE=""38TENSOR_FRONTIER_READ_OVERRIDE=""39 40latest_runtime_artifact_graph() {41  find "$ROOT/runs/runtime" -name graph_state.json -type f 2>/dev/null | sort | while read -r graph; do42    if jq -e '.effects[]? | select((.path // "") != "")' "$graph" >/dev/null; then43      printf '%s\n' "$graph"44    fi45  done | tail -n 146}47 48latest_continuity_pointer() {49  if [ -f "$ROOT/runs/continuity/latest.json" ]; then50    printf '%s\n' "$ROOT/runs/continuity/latest.json"51  fi52}53 54continuity_provider_status() {55  if [ -x "$CONTINUITY_PROVIDER_SCRIPT" ]; then56    "$CONTINUITY_PROVIDER_SCRIPT" status 2>/dev/null || true57  fi58}59 60continuity_provider_surfaces() {61  if [ -x "$CONTINUITY_PROVIDER_SCRIPT" ]; then62    "$CONTINUITY_PROVIDER_SCRIPT" surfaces 2>/dev/null || true63  fi64}65 66continuity_provider_lineage() {67  local version_id="$1"68  if [ -x "$CONTINUITY_PROVIDER_SCRIPT" ]; then69    "$CONTINUITY_PROVIDER_SCRIPT" lineage "$version_id" 2>/dev/null || true70  fi71}72 73continuity_resume_packet() {74  if [ -x "$ROOT/scripts/render_continuity_flow.sh" ]; then75    "$ROOT/scripts/render_continuity_flow.sh" "$1" 2>/dev/null || true76  fi77}78 79resolve_path_from_root() {80  local candidate="$1"81  case "$candidate" in82    "")83      printf '%s' ""84      ;;85    /*)86      printf '%s' "$candidate"87      ;;88    *)89      printf '%s' "$ROOT/$candidate"90      ;;91  esac92}93 94load_packet_context() {95  local packet_enabled latest_pointer registry_path96  packet_enabled="$(yq -r '.packet_context.enabled // false' "$INFERENCE_CONFIG_PATH")"97  if [ "$packet_enabled" != "true" ]; then98    return 199  fi100 101  latest_pointer="$(yq -r '.packet_context.latest_pointer_path // ""' "$INFERENCE_CONFIG_PATH")"102  PACKET_POINTER_PATH="$(resolve_path_from_root "$latest_pointer")"103  if [ ! -f "$PACKET_POINTER_PATH" ]; then104    return 1105  fi106 107  registry_path="$(jq -r '.registry_path // empty' "$PACKET_POINTER_PATH")"108  PACKET_REGISTRY_PATH="$(resolve_path_from_root "$registry_path")"109  if [ ! -f "$PACKET_REGISTRY_PATH" ]; then110    return 1111  fi112 113  PACKET_CONTEXT_JSON="$(jq -c '{generated_at, packet_count, abstraction_ids, packets: [.packets[] | {abstraction_id, observer_basis, bits, tensor_slice}]}' "$PACKET_REGISTRY_PATH")"114  return 0115}116 117handle_exact_query() {118  if printf '%s' "$INPUT_LOWER" | grep -Eq 'continuity'; then119    local continuity_status continuity_surfaces_json continuity_lineage_json continuity_version continuity_predecessor continuity_lineage continuity_surfaces continuity_default_surface continuity_provider continuity_surface_count continuity_resume_json continuity_resume_command120    continuity_status="$(continuity_provider_status)"121    continuity_provider="$(printf '%s' "$continuity_status" | jq -r '.provider // "unknown"' 2>/dev/null || printf '%s' "unknown")"122    if printf '%s' "$continuity_status" | jq -e '.available == true or (.items | length) > 0' >/dev/null 2>&1; then123      if printf '%s' "$continuity_status" | jq -e '.items' >/dev/null 2>&1; then124        continuity_version="$(printf '%s' "$continuity_status" | jq -r '.items[0].version_id // empty')"125        continuity_predecessor="$(printf '%s' "$continuity_status" | jq -r '.items[0].predecessor // ""')"126      else127        continuity_version="$(printf '%s' "$continuity_status" | jq -r '.continuity_version_id // empty')"128        continuity_predecessor="$(printf '%s' "$continuity_status" | jq -r '.predecessor_id // ""')"129      fi130      continuity_default_surface=""131      continuity_lineage=""132      continuity_surfaces=""133      continuity_resume_json=""134      if [ -n "$continuity_version" ] && printf '%s' "$INPUT_LOWER" | grep -Eq 'lineage|predecessor|ancestor'; then135        continuity_lineage_json="$(continuity_provider_lineage "$continuity_version")"136        continuity_lineage="$(printf '%s' "$continuity_lineage_json" | jq -r '.graph_id // .manifest_path // .lineage_path // ""' 2>/dev/null || true)"137      fi138      if printf '%s' "$INPUT_LOWER" | grep -Eq 'resume|desk|next move|continue'; then139        continuity_resume_json="$(continuity_resume_packet "$continuity_version")"140        continuity_default_surface="$(printf '%s' "$continuity_resume_json" | jq -r '.context.active_surface // .action.recommended_surface // ""' 2>/dev/null || true)"141        continuity_resume_command="$(printf '%s' "$continuity_resume_json" | jq -r '.action.resume_packet.command // ""' 2>/dev/null || true)"142        DECISION_BRIEF="Answered from the deterministic kernel using the provider-first continuity resume desk."143        ANSWER_TEXT="Resume continuity from $continuity_version on surface $continuity_default_surface. The next desk command is $continuity_resume_command."144      elif printf '%s' "$INPUT_LOWER" | grep -Eq 'surface|resume|picker'; then145        continuity_surfaces_json="$(continuity_provider_surfaces)"146        continuity_surfaces="$(printf '%s' "$continuity_surfaces_json" | jq -r '[.items[].surface_id] | join(", ")' 2>/dev/null || true)"147        continuity_surface_count="$(printf '%s' "$continuity_surfaces_json" | jq -r '(.items | length)' 2>/dev/null || printf '%s' "0")"148        continuity_default_surface="$(printf '%s' "$continuity_surfaces_json" | jq -r '.items[] | select(.activated == true) | .surface_id' 2>/dev/null | head -n 1)"149        if [ -z "$continuity_default_surface" ]; then150          continuity_default_surface="$(printf '%s' "$continuity_surfaces_json" | jq -r '.items[0].surface_id // ""' 2>/dev/null || true)"151        fi152        DECISION_BRIEF="Answered from the deterministic kernel using the provider-first continuity surface registry."153        ANSWER_TEXT="The active continuity version is $continuity_version. Its materialized surfaces are $continuity_surfaces, with default surface $continuity_default_surface, from provider $continuity_provider."154      elif printf '%s' "$INPUT_LOWER" | grep -Eq 'lineage|predecessor|ancestor'; then155        DECISION_BRIEF="Answered from the deterministic kernel using the provider-first continuity lineage slice."156        ANSWER_TEXT="The active continuity version is $continuity_version, derived from $continuity_predecessor. Its lineage state is recorded at $continuity_lineage through provider $continuity_provider."157      else158        DECISION_BRIEF="Answered from the deterministic kernel using the provider-first continuity status surface."159        ANSWER_TEXT="The active continuity slice is $continuity_version, derived from $continuity_predecessor, from provider $continuity_provider."160      fi161      ABSTAINED=false162    else163      DECISION_BRIEF="Abstained because no continuity slice has been materialized yet."164      ANSWER_TEXT="I do not know. No continuity slice has been materialized yet."165      ABSTAINED=true166    fi167    TENSOR_STRONGEST_SOURCE_OVERRIDE="receipt_state"168    TENSOR_DOMINANT_STAGE_OVERRIDE="select"169    TENSOR_WEAKEST_METRIC_OVERRIDE="promotion_readiness"170    TENSOR_FRONTIER_READ_OVERRIDE="deterministic_exact_lookup"171    return 0172  fi173 174  if printf '%s' "$INPUT_LOWER" | grep -Eq 'weekly schedule|schedule|calendar'; then175    DECISION_BRIEF="Answered from the deterministic kernel because no supported weekly schedule exists in current graph state."176    ANSWER_TEXT="I do not know. No weekly schedule is present in the current graph state."177    ABSTAINED=true178    TENSOR_STRONGEST_SOURCE_OVERRIDE="graph_state"179    TENSOR_DOMINANT_STAGE_OVERRIDE="select"180    TENSOR_WEAKEST_METRIC_OVERRIDE="promotion_readiness"181    TENSOR_FRONTIER_READ_OVERRIDE="deterministic_exact_lookup"182    return 0183  fi184 185  if printf '%s' "$INPUT_LOWER" | grep -Eq 'freshest|latest|most recent'; then186    local latest_graph latest_receipt latest_effect_path187    latest_graph="$(latest_runtime_artifact_graph)"188    if [ -n "$latest_graph" ] && [ -f "$latest_graph" ]; then189      latest_receipt="$(jq -r '.receipt' "$latest_graph")"190      latest_effect_path="$(jq -r '[.effects[]? | select((.path // "") != "")][0].path // empty' "$latest_graph")"191      DECISION_BRIEF="Answered from the deterministic kernel using the latest receipt-backed runtime artifact."192      ANSWER_TEXT="The freshest runtime artifact is $latest_effect_path from receipt $latest_receipt."193      ABSTAINED=false194    else195      DECISION_BRIEF="Abstained because no receipt-backed runtime state exists yet."196      ANSWER_TEXT="I do not know. No receipt-backed runtime state exists yet."197      ABSTAINED=true198    fi199    TENSOR_STRONGEST_SOURCE_OVERRIDE="receipt_state"200    TENSOR_DOMINANT_STAGE_OVERRIDE="select"201    TENSOR_WEAKEST_METRIC_OVERRIDE="promotion_readiness"202    TENSOR_FRONTIER_READ_OVERRIDE="deterministic_exact_lookup"203    return 0204  fi205 206  return 1207}208 209run_codex_inference() {210  local backend_id provider enabled command_name model sandbox ephemeral skip_git use_chatgpt211  local system_context_compact policy_context_compact packet_bits packet_tensor212  local -a codex_cmd213 214  backend_id="$(yq -r '.default_backend' "$INFERENCE_CONFIG_PATH")"215  enabled="$(yq -r ".backends.\"$backend_id\".enabled" "$INFERENCE_CONFIG_PATH")"216  if [ "$enabled" != "true" ]; then217    return 1218  fi219 220  provider="$(yq -r ".backends.\"$backend_id\".provider" "$INFERENCE_CONFIG_PATH")"221  command_name="$(yq -r ".backends.\"$backend_id\".command" "$INFERENCE_CONFIG_PATH")"222  model="$(yq -r ".backends.\"$backend_id\".model" "$INFERENCE_CONFIG_PATH")"223  sandbox="$(yq -r ".backends.\"$backend_id\".sandbox" "$INFERENCE_CONFIG_PATH")"224  ephemeral="$(yq -r ".backends.\"$backend_id\".ephemeral" "$INFERENCE_CONFIG_PATH")"225  skip_git="$(yq -r ".backends.\"$backend_id\".skip_git_repo_check" "$INFERENCE_CONFIG_PATH")"226  use_chatgpt="$(yq -r ".backends.\"$backend_id\".use_chatgpt_subscription" "$INFERENCE_CONFIG_PATH")"227  packet_bits="$(yq -r '.packet_context.reread_on_bits | join(", ")' "$INFERENCE_CONFIG_PATH" 2>/dev/null || true)"228  packet_tensor="$(yq -r '.packet_context.reread_on_tensor | join(", ")' "$INFERENCE_CONFIG_PATH" 2>/dev/null || true)"229  system_context_compact="$(jq -c '{current_position, runtime_contract, latest_runtime_state, agent_bootstrap}' "$SYSTEM_CONTEXT_PATH")"230  policy_context_compact="$(jq -c '{bits, vectors, tensors, invariants}' "$ROOT/policy/control_language_v0.json")"231 232  cat > "$PROMPT_PATH" <<EOF233You are the inference backend for bit_vector_tensor_control_policy.234Work as the reasoning engine under the deterministic turn kernel.235Do not execute code, write files, or open an execution lane.236The runtime kernel owns lane choice; assume graph-first memory mode unless the runtime explicitly opens execution elsewhere.237Abstain plainly when the ask depends on missing or unsupported state.238Keep \`answer_text\` concise and user-facing.239Keep \`decision_brief\` to one sentence.240Return JSON only matching the provided schema.241 242System context:243$system_context_compact244 245Policy context:246$policy_context_compact247 248Packet context:249$PACKET_CONTEXT_JSON250 251Packet-first rule:252Reason over packet state before reopening raw evidence.253Reopen raw evidence only when packet bits among [$packet_bits] indicate it or packet tensor metrics among [$packet_tensor] are non-zero or unresolved.254 255User ask: $USER_INPUT256EOF257 258  codex_cmd=("$command_name" "exec" "-m" "$model" "-s" "$sandbox" "-C" "$ROOT" "--output-schema" "$INFERENCE_SCHEMA_PATH" "-o" "$INFERENCE_PATH" "-")259  if [ "$ephemeral" = "true" ]; then260    codex_cmd+=("--ephemeral")261  fi262  if [ "$skip_git" = "true" ]; then263    codex_cmd+=("--skip-git-repo-check")264  fi265 266  if ! "${codex_cmd[@]}" < "$PROMPT_PATH" >/dev/null 2>"$OUT_DIR/inference.stderr"; then267    return 1268  fi269 270  jq \271    --arg id "$backend_id" \272    --arg provider "$provider" \273    --arg model "$model" \274    --arg sandbox "$sandbox" \275    --argjson uses_chatgpt_subscription "$use_chatgpt" \276    '. + {277      backend: {278        id: $id,279        provider: $provider,280        model: $model,281        sandbox: $sandbox,282        uses_chatgpt_subscription: $uses_chatgpt_subscription283      }284    }' "$INFERENCE_PATH" > "$INFERENCE_PATH.tmp"285  mv "$INFERENCE_PATH.tmp" "$INFERENCE_PATH"286}287 288if [ -n "$MANIFEST_PATH" ]; then289  EXEC_OUTPUT="$("$ROOT/runtime/execute_manifest.sh" "$MANIFEST_PATH")"290  RECEIPT_PATH="$(printf '%s\n' "$EXEC_OUTPUT" | sed -n '1p')"291  SUMMARY_PATH="$(printf '%s\n' "$EXEC_OUTPUT" | sed -n '2p')"292  LANE="execution"293  EXEC_ALLOWED=true294  DECISION_BRIEF="Executed bounded manifest and emitted receipt-backed result."295  UI_MODE="confirm"296  UI_LAYOUT="chat_with_sidecar"297  ANSWER_TEXT="Executed bounded manifest and emitted receipt-backed result."298else299  RECEIPT_PATH=""300  SUMMARY_PATH=""301  EXEC_ALLOWED=false302  LANE="memory"303  UI_MODE="chat"304  UI_LAYOUT="chat_only"305 306  if [ -f "$INFERENCE_CONFIG_PATH" ]; then307    load_packet_context || true308  fi309 310  if handle_exact_query; then311    :312  elif [ -f "$INFERENCE_CONFIG_PATH" ] && run_codex_inference; then313    INFERENCE_AVAILABLE=true314    INFERENCE_RESULT_JSON="$(cat "$INFERENCE_PATH")"315    DECISION_BRIEF="$(jq -r '.decision_brief' "$INFERENCE_PATH")"316    ANSWER_TEXT="$(jq -r '.answer_text' "$INFERENCE_PATH")"317    ABSTAINED="$(jq -r '.abstained' "$INFERENCE_PATH")"318  else319    DECISION_BRIEF="Stayed in graph-first reasoning mode; no execution lane opened."320    ANSWER_TEXT="The current runtime stays graph-first until a bounded execution task is selected."321  fi322fi323 324jq -n \325  --slurpfile system_context "$SYSTEM_CONTEXT_PATH" \326  --arg user_input "$USER_INPUT" \327  --arg lane "$LANE" \328  --arg decision_brief "$DECISION_BRIEF" \329  --arg answer_text "$ANSWER_TEXT" \330  --arg receipt_path "$RECEIPT_PATH" \331  --arg summary_path "$SUMMARY_PATH" \332  --arg ui_mode "$UI_MODE" \333  --arg ui_layout "$UI_LAYOUT" \334  --arg inference_path "$INFERENCE_PATH" \335  --arg inference_config_path "$INFERENCE_CONFIG_PATH" \336  --arg inference_error_path "$OUT_DIR/inference.stderr" \337  --arg packet_pointer_path "$PACKET_POINTER_PATH" \338  --arg packet_registry_path "$PACKET_REGISTRY_PATH" \339  --arg tensor_strongest_source_override "$TENSOR_STRONGEST_SOURCE_OVERRIDE" \340  --arg tensor_dominant_stage_override "$TENSOR_DOMINANT_STAGE_OVERRIDE" \341  --arg tensor_weakest_metric_override "$TENSOR_WEAKEST_METRIC_OVERRIDE" \342  --arg tensor_frontier_read_override "$TENSOR_FRONTIER_READ_OVERRIDE" \343  --argjson inference_result "$INFERENCE_RESULT_JSON" \344  --argjson execution_allowed "$EXEC_ALLOWED" \345  --argjson abstained "$ABSTAINED" \346  --argjson inference_available "$INFERENCE_AVAILABLE" \347  '{348    user_input:$user_input,349    lane:$lane,350    control_vector:(if $execution_allowed then351      {plan:0.7, execute:0.8, review:0.6, promote:0.2}352    elif $lane == "audit" then353      {plan:0.5, execute:0.0, review:0.9, promote:0.4}354    elif $lane == "tooling" then355      {plan:0.6, execute:0.0, review:0.5, promote:0.3}356    else357      {plan:0.7, execute:0.1, review:0.6, promote:0.2}358    end),359    tensor_surface:{360      strongest_source:(if $tensor_strongest_source_override != "" then $tensor_strongest_source_override elif $execution_allowed then "receipt_state" elif $lane == "audit" then "receipts" elif $lane == "tooling" then "external_tools" elif ($packet_registry_path | length) > 0 then "graph_state" else "graph_state" end),361      dominant_stage:(if $tensor_dominant_stage_override != "" then $tensor_dominant_stage_override elif $execution_allowed then "execute" elif $lane == "audit" then "review" elif $lane == "tooling" then "select" else "discover" end),362      weakest_metric:(if $tensor_weakest_metric_override != "" then $tensor_weakest_metric_override elif $execution_allowed then "promotion_readiness" elif $lane == "audit" then "latency" else "promotion_readiness" end),363      frontier_read:(if $tensor_frontier_read_override != "" then $tensor_frontier_read_override elif $execution_allowed then "bounded_execution_with_receipt" elif $inference_available and ($packet_registry_path | length) > 0 then "corpus_packet_first_reasoning" elif $inference_available then "codex_cli_graph_first_reasoning" else "graph_first_reasoning" end)364    },365    execution_gate:{allowed:$execution_allowed},366    receipt_requirements:{minimum_receipts:(if $execution_allowed then 1 else 0 end)},367    ui_projection:{368      mode:$ui_mode,369      layout:$ui_layout,370      components:(if $execution_allowed371        then ["chat_thread","proposal_card","receipt_panel"]372        else ["chat_thread","next_step_card"]373      end)374    },375    answer_text:$answer_text,376    abstained:$abstained,377    decision_brief:$decision_brief,378    inference:(if $inference_available and ($inference_path | length) > 0379      then {380        config_path:$inference_config_path,381        output_path:$inference_path,382        backend:$inference_result.backend,383        packet_pointer_path:(if $packet_pointer_path == "" then null else $packet_pointer_path end),384        packet_registry_path:(if $packet_registry_path == "" then null else $packet_registry_path end)385      }386      else {387        config_path:(if $inference_config_path == "" then null else $inference_config_path end),388        output_path:(if $inference_path == "" then null else $inference_path end),389        error_path:(if $inference_error_path == "" then null else $inference_error_path end),390        packet_pointer_path:(if $packet_pointer_path == "" then null else $packet_pointer_path end),391        packet_registry_path:(if $packet_registry_path == "" then null else $packet_registry_path end)392      }393    end),394    system_context:$system_context[0],395    receipt_path:(if $receipt_path == "" then null else $receipt_path end),396    runtime_summary:(if $summary_path == "" then null else $summary_path end)397  }' > "$RESPONSE_PATH"398 399printf '%s\n' "$RESPONSE_PATH"400