Add single-shot profile shaping closure path (sprint 247)

This commit is contained in:
Bill
2026-02-26 15:33:36 -07:00
parent daf9d9b8c4
commit dfb10ea40f
8 changed files with 194 additions and 2 deletions

View File

@@ -45,6 +45,7 @@ NATIVE_IMPACT_COVERAGE_PROFILES="${WSTONE_NATIVE_IMPACT_COVERAGE_PROFILES:-$ROOT
NATIVE_PROFILE_AUTOFILL="${WSTONE_NATIVE_PROFILE_AUTOFILL:-0}"
NATIVE_PROFILE_AUTOFILL_MAX_TASKS="${WSTONE_NATIVE_PROFILE_AUTOFILL_MAX_TASKS:-12}"
NATIVE_INTRINSIC_BOOST="${WSTONE_NATIVE_INTRINSIC_BOOST:-0}"
NATIVE_SINGLESHOT_PROFILE_SHAPE="${WSTONE_NATIVE_SINGLESHOT_PROFILE_SHAPE:-0}"
NATIVE_MULTISHOT_DECOMP="${WSTONE_NATIVE_MULTISHOT_DECOMP:-0}"
NATIVE_MULTISHOT_MAX_PROFILES="${WSTONE_NATIVE_MULTISHOT_MAX_PROFILES:-12}"
NATIVE_MULTISHOT_APPLY_MODE="${WSTONE_NATIVE_MULTISHOT_APPLY_MODE:-if_improves}"
@@ -107,6 +108,7 @@ NATIVE_DECOMP_RETRY_JSON='{}'
NATIVE_IMPACT_COVERAGE_JSON='{}'
NATIVE_PROFILE_AUTOFILL_JSON='{}'
NATIVE_INTRINSIC_BOOST_JSON='{}'
NATIVE_SINGLESHOT_PROFILE_SHAPE_JSON='{}'
NATIVE_MULTISHOT_JSON='{}'
INTRINSIC_REQS_JSON='[]'
EXTRA_NORMALIZED_REQUIREMENTS_JSON='[]'
@@ -383,6 +385,30 @@ else
--argjson target_min_task_count "$NATIVE_DECOMP_TARGET_MIN_TASKS" \
'{attempted:$attempted, applied:$applied, initial_task_count:$initial_task_count, target_min_task_count:$target_min_task_count}')"
fi
if [[ "$NATIVE_SINGLESHOT_PROFILE_SHAPE" == "1" ]]; then
printf '%s\n' "$TASKS" > "$OUT_DIR/02ad_single_shot_base_tasks.json"
python3 "$ROOT_DIR/tools/mcp/synthesize_single_shot_profile_shape_tasks.py" \
--spec "$INPUT_FILE" \
--profiles "$NATIVE_IMPACT_COVERAGE_PROFILES" \
--tasks "$OUT_DIR/02ad_single_shot_base_tasks.json" \
--out "$OUT_DIR/02ad_single_shot_shaped_tasks.json" \
--out-report "$OUT_DIR/02ad_single_shot_shape_report.json" >/dev/null
shaped_tasks_json="$(cat "$OUT_DIR/02ad_single_shot_shaped_tasks.json")"
base_count_ss="$(printf '%s' "$TASKS" | jq 'length')"
shaped_count_ss="$(printf '%s' "$shaped_tasks_json" | jq 'length')"
if [[ "$shaped_count_ss" -gt "$base_count_ss" ]]; then
added_shape_tasks="$(jq -nc --argjson shaped "$shaped_tasks_json" --argjson n "$base_count_ss" '$shaped[$n:]')"
EXTRA_TASKS_JSON="$(jq -nc --argjson base "$EXTRA_TASKS_JSON" --argjson extra "$added_shape_tasks" '$base + $extra')"
TASKS="$shaped_tasks_json"
fi
NATIVE_SINGLESHOT_PROFILE_SHAPE_JSON="$(jq -nc \
--argjson rep "$(cat "$OUT_DIR/02ad_single_shot_shape_report.json")" \
--argjson base_count "$base_count_ss" \
--argjson shaped_count "$shaped_count_ss" \
'{enabled:true, base_task_count:$base_count, shaped_task_count:$shaped_count, report:$rep}')"
else
NATIVE_SINGLESHOT_PROFILE_SHAPE_JSON='{"enabled":false}'
fi
if [[ "$NATIVE_MULTISHOT_DECOMP" == "1" ]]; then
if [[ "$(printf '%s' "$INTRINSIC_REQS_JSON" | jq 'length')" -eq 0 ]]; then
python3 "$ROOT_DIR/tools/mcp/synthesize_native_intrinsic_boost_requirements.py" \
@@ -678,6 +704,7 @@ SUMMARY_JSON="$(jq -nc \
--argjson native_decomposition_retry "$NATIVE_DECOMP_RETRY_JSON" \
--argjson native_profile_autofill "$NATIVE_PROFILE_AUTOFILL_JSON" \
--argjson native_intrinsic_boost "$NATIVE_INTRINSIC_BOOST_JSON" \
--argjson native_single_shot_profile_shape "$NATIVE_SINGLESHOT_PROFILE_SHAPE_JSON" \
--argjson native_multishot "$NATIVE_MULTISHOT_JSON" \
--argjson extra_normalized_requirements "$EXTRA_NORMALIZED_REQUIREMENTS_JSON" \
--argjson extra_tasks "$EXTRA_TASKS_JSON" \
@@ -704,6 +731,7 @@ SUMMARY_JSON="$(jq -nc \
native_decomposition_retry: $native_decomposition_retry,
native_profile_autofill: $native_profile_autofill,
native_intrinsic_boost: $native_intrinsic_boost,
native_single_shot_profile_shape: $native_single_shot_profile_shape,
native_multishot: $native_multishot,
extra_normalized_requirements: $extra_normalized_requirements,
extra_tasks: $extra_tasks,

View File

@@ -0,0 +1,87 @@
#!/usr/bin/env python3
import argparse
import json
from pathlib import Path
from typing import Dict, List
def load_json(path: Path):
with path.open("r", encoding="utf-8") as f:
return json.load(f)
def write_json(path: Path, obj) -> None:
with path.open("w", encoding="utf-8") as f:
json.dump(obj, f, indent=2, sort_keys=True)
f.write("\n")
def activate_profiles(profiles: List[Dict], spec_text: str) -> List[Dict]:
s = spec_text.lower()
out = []
for p in profiles:
keys = [str(k).lower() for k in (p.get("trigger_keywords") or [])]
if keys and any(k in s for k in keys):
out.append(p)
return out
def apply_profile(task: Dict, profile: Dict, idx: int) -> Dict:
pid = str(profile.get("id", f"profile_{idx}"))
ops = [str(x) for x in (profile.get("required_prerequisite_ops") or [])]
reasons = [str(x) for x in (profile.get("required_reason_keywords") or [])]
contracts = [str(x) for x in (profile.get("required_execution_contract") or [])]
ec = dict(task.get("executionContract") or {})
ec["deterministic"] = True
ec["rollbackRequired"] = bool(ec.get("rollbackRequired", True))
ec["replayValidationRequired"] = bool(ec.get("replayValidationRequired", True))
for f in contracts:
ec[f] = True
return {
**task,
"taskId": f"{task.get('taskId', f'task-{idx+1}')}-ss-{pid.replace('-', '_')}-{idx+1}",
"prerequisiteOps": sorted(set((task.get("prerequisiteOps") or []) + ops)),
"reasons": sorted(set((task.get("reasons") or []) + reasons + ["single_shot_profile_shape", f"impact_profile:{pid}"])),
"executionContract": ec,
}
def main() -> int:
p = argparse.ArgumentParser(description="Shape single-shot native tasks to satisfy active profile contracts.")
p.add_argument("--spec", required=True)
p.add_argument("--profiles", required=True)
p.add_argument("--tasks", required=True)
p.add_argument("--out", required=True)
p.add_argument("--out-report", required=True)
args = p.parse_args()
spec_text = Path(args.spec).read_text(encoding="utf-8", errors="ignore")
profiles_doc = load_json(Path(args.profiles))
profiles = list(profiles_doc.get("profiles") or [])
active = activate_profiles(profiles, spec_text)
base_tasks = load_json(Path(args.tasks))
if not isinstance(base_tasks, list):
raise SystemExit("--tasks must be JSON array")
shaped: List[Dict] = list(base_tasks)
if base_tasks and active:
for i, pf in enumerate(active):
base = base_tasks[i % len(base_tasks)]
shaped.append(apply_profile(base, pf, i))
write_json(Path(args.out), shaped)
write_json(Path(args.out_report), {
"status": "ok",
"base_task_count": len(base_tasks),
"active_profile_count": len(active),
"shaped_task_count": len(shaped),
})
print(json.dumps({"status": "ok", "base_task_count": len(base_tasks), "active_profile_count": len(active), "shaped_task_count": len(shaped)}, sort_keys=True))
return 0
if __name__ == "__main__":
raise SystemExit(main())