bit-vector-tensor-control-policy / scripts /generate_control_packet.sh
J94's picture
Initial Space upload
3436bdd verified
#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
HOTGRAPH="$ROOT/hotgraph/source_hotgraph.json"
USECASES="$ROOT/configs/usecase_registry.json"
USECASE="${1:-migration_brief}"
GOAL_OVERRIDE="${2:-}"
RUN_DIR="$ROOT/runs/run-$(date -u +"%Y%m%dT%H%M%SZ")"
PACKET="$RUN_DIR/control_packet.json"
BRIEF="$RUN_DIR/brief.md"
mkdir -p "$RUN_DIR"
USECASE_JSON="$(jq -c --arg usecase "$USECASE" '.usecases[] | select(.id == $usecase)' "$USECASES")"
if [ -z "$USECASE_JSON" ]; then
echo "unknown usecase: $USECASE" >&2
exit 1
fi
if [ -n "$GOAL_OVERRIDE" ]; then
GOAL="$GOAL_OVERRIDE"
else
GOAL="$(printf '%s' "$USECASE_JSON" | jq -r '.goal_template')"
fi
score_row() {
local row_json="$1"
local usecase_id role priority package_count score text
usecase_id="$(printf '%s' "$USECASE_JSON" | jq -r '.id')"
role="$(printf '%s' "$row_json" | jq -r '.role')"
priority="$(printf '%s' "$row_json" | jq -r '.priority')"
package_count="$(printf '%s' "$row_json" | jq -r '.package_count')"
score=0
if [ "$priority" = "high" ]; then
score=$((score + 2))
fi
if [ "$package_count" -gt 4 ]; then
package_count=4
fi
score=$((score + package_count))
case "$usecase_id:$role" in
migration_brief:successor_branch) score=$((score + 5)) ;;
migration_brief:kernel_candidate|migration_brief:control_plane_candidate) score=$((score + 4)) ;;
migration_brief:product_runtime_reference) score=$((score + 3)) ;;
migration_brief:live_shell|migration_brief:memory_substrate|migration_brief:manifest_executor) score=$((score + 2)) ;;
control_scorecard:control_plane_candidate) score=$((score + 5)) ;;
control_scorecard:memory_substrate) score=$((score + 4)) ;;
control_scorecard:live_shell|control_scorecard:successor_branch|control_scorecard:product_runtime_reference) score=$((score + 3)) ;;
control_scorecard:kernel_candidate|control_scorecard:manifest_executor) score=$((score + 2)) ;;
runtime_split:successor_branch|runtime_split:kernel_candidate) score=$((score + 5)) ;;
runtime_split:product_runtime_reference|runtime_split:control_plane_candidate) score=$((score + 4)) ;;
runtime_split:manifest_executor) score=$((score + 3)) ;;
runtime_split:live_shell|runtime_split:memory_substrate) score=$((score + 2)) ;;
product_quickstart:product_runtime_reference) score=$((score + 5)) ;;
product_quickstart:live_shell) score=$((score + 4)) ;;
product_quickstart:successor_branch|product_quickstart:control_plane_candidate) score=$((score + 3)) ;;
product_quickstart:manifest_executor) score=$((score + 2)) ;;
product_quickstart:kernel_candidate|product_quickstart:memory_substrate) score=$((score + 1)) ;;
esac
text="$(printf '%s' "$GOAL" | tr '[:upper:]' '[:lower:]')"
case "$text:$role" in
*kernel*:kernel_candidate) score=$((score + 3)) ;;
esac
case "$text:$role" in
*control*:control_plane_candidate) score=$((score + 3)) ;;
esac
case "$text:$role" in
*product*:product_runtime_reference) score=$((score + 3)) ;;
esac
case "$text:$role" in
*live*:live_shell) score=$((score + 2)) ;;
esac
printf '%s' "$score"
}
RANKED_TMP="$(mktemp)"
trap 'rm -f "$RANKED_TMP"' EXIT
jq -c '.summary[]' "$HOTGRAPH" | while IFS= read -r row; do
score="$(score_row "$row")"
jq -n --argjson score "$score" --argjson row "$row" '$row + {score:$score}' >> "$RANKED_TMP"
done
RANKED_JSON="$(jq -s 'sort_by(.score, .priority == "high", .package_count, .id) | reverse' "$RANKED_TMP")"
RECOMMENDED_HOME="$(printf '%s' "$RANKED_JSON" | jq -r '.[0].id')"
TOP_ROLE="$(printf '%s' "$RANKED_JSON" | jq -r '.[0].role')"
FRONTIER_READ="top_ranked_source_selected"
case "$USECASE:$TOP_ROLE" in
migration_brief:successor_branch) FRONTIER_READ="favor_successor_branch_over_live_shell" ;;
control_scorecard:control_plane_candidate) FRONTIER_READ="favor_control_plane_for_metrics_logic" ;;
product_quickstart:product_runtime_reference) FRONTIER_READ="favor_thin_product_runtime" ;;
runtime_split:kernel_candidate) FRONTIER_READ="favor_kernel_extraction" ;;
esac
ONE_LINER="Use \`$RECOMMENDED_HOME\` as the recommended home."
case "$USECASE" in
migration_brief) ONE_LINER="Use \`$RECOMMENDED_HOME\` as the forward home for new runtime-shaping work." ;;
control_scorecard) ONE_LINER="Use \`$RECOMMENDED_HOME\` as the policy and scorecard home, with the hotgraph as intake." ;;
runtime_split) ONE_LINER="Split shell, kernel, and memory roles around \`$RECOMMENDED_HOME\` first." ;;
product_quickstart) ONE_LINER="Use \`$RECOMMENDED_HOME\` as the reference for the smallest user-facing runtime slice." ;;
esac
RISKS='[]'
case "$RECOMMENDED_HOME" in
nix_codecli) RISKS='["live shell may absorb more product logic than it should"]' ;;
tmp_meta3_engine_test) RISKS='["successor branch is richer but less settled than the live shell"]' ;;
meta3_graph_core) RISKS='["kernel candidate is too low-level to be the whole product by itself"]' ;;
esac
if [ "$USECASE" = "control_scorecard" ]; then
RISKS="$(printf '%s' "$RISKS" | jq '. + ["metrics may drift if the hotgraph source registry is not refreshed"]')"
fi
printf '%s' "$RANKED_JSON" | jq -n \
--arg generated_at "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \
--arg goal "$GOAL" \
--arg usecase_id "$USECASE" \
--arg hotgraph_ref "$HOTGRAPH" \
--arg target_artifact "$(printf '%s' "$USECASE_JSON" | jq -r '.target_artifact')" \
--arg one_liner "$ONE_LINER" \
--arg frontier_read "$FRONTIER_READ" \
--arg recommended_home "$RECOMMENDED_HOME" \
--argjson ranked "$RANKED_JSON" \
--argjson risks "$RISKS" \
'{
generated_at:$generated_at,
goal:$goal,
usecase_id:$usecase_id,
source_ids:($ranked | map(.id)),
hotgraph_ref:$hotgraph_ref,
target_artifact:$target_artifact,
decision:{
one_liner:$one_liner,
frontier_read:$frontier_read,
recommended_home:$recommended_home,
recommended_sources:($ranked | map(.id)[:4]),
risks:$risks
}
}' > "$PACKET"
{
echo "# Control Brief"
echo
echo "Goal: \`$(jq -r '.goal' "$PACKET")\`"
echo
echo "Use case: \`$(jq -r '.usecase_id' "$PACKET")\`"
echo
echo "Clean product one-liner: $(jq -r '.decision.one_liner' "$PACKET")"
echo
echo "Pareto read: \`$(jq -r '.decision.frontier_read' "$PACKET")\`"
echo
echo "Recommended home: \`$(jq -r '.decision.recommended_home' "$PACKET")\`"
echo
echo "## Source order"
echo
jq -r '.decision.recommended_sources[] | "- `\(.)`"' "$PACKET"
if [ "$(jq '.decision.risks | length' "$PACKET")" -gt 0 ]; then
echo
echo "## Risks"
echo
jq -r '.decision.risks[] | "- \(.)"' "$PACKET"
fi
} > "$BRIEF"
printf '%s\n' "$PACKET"
printf '%s\n' "$BRIEF"