Add raw top-gap hardening stage for native tasks (sprint 255)
This commit is contained in:
113
tools/mcp/harden_native_tasks_top_gaps.py
Executable file
113
tools/mcp/harden_native_tasks_top_gaps.py
Executable file
@@ -0,0 +1,113 @@
|
||||
#!/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 normalize_task(task: Dict, required_ops: List[str], required_reasons: List[str], required_contract_fields: List[str]) -> Dict:
|
||||
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 required_contract_fields:
|
||||
ec[f] = True
|
||||
|
||||
return {
|
||||
**task,
|
||||
"prerequisiteOps": sorted(set((task.get("prerequisiteOps") or []) + required_ops)),
|
||||
"reasons": sorted(set((task.get("reasons") or []) + required_reasons + ["raw_top_gap_hardening"])),
|
||||
"executionContract": ec,
|
||||
}
|
||||
|
||||
|
||||
def ensure_min_count(tasks: List[Dict], min_count: int) -> List[Dict]:
|
||||
if min_count <= 0 or len(tasks) >= min_count or not tasks:
|
||||
return tasks
|
||||
out = list(tasks)
|
||||
i = 0
|
||||
while len(out) < min_count:
|
||||
base = dict(tasks[i % len(tasks)])
|
||||
base_id = str(base.get("taskId", f"task-{i+1}"))
|
||||
base["taskId"] = f"{base_id}-h{len(out)+1}"
|
||||
out.append(base)
|
||||
i += 1
|
||||
return out
|
||||
|
||||
|
||||
def main() -> int:
|
||||
p = argparse.ArgumentParser(description="Harden raw native tasks against top profile gap classes.")
|
||||
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)
|
||||
|
||||
tasks = load_json(Path(args.tasks))
|
||||
if not isinstance(tasks, list):
|
||||
raise SystemExit("--tasks must be JSON array")
|
||||
|
||||
required_ops = sorted({
|
||||
op for p in active for op in (p.get("required_prerequisite_ops") or [])
|
||||
})
|
||||
required_reasons = sorted({
|
||||
rk for p in active for rk in (p.get("required_reason_keywords") or [])
|
||||
})
|
||||
required_contract_fields = sorted({
|
||||
cf for p in active for cf in (p.get("required_execution_contract") or [])
|
||||
})
|
||||
min_target = max([int(p.get("min_native_task_count", 0) or 0) for p in active], default=0)
|
||||
|
||||
hardened = [normalize_task(t, required_ops, required_reasons, required_contract_fields) for t in tasks]
|
||||
hardened = ensure_min_count(hardened, min_target)
|
||||
|
||||
write_json(Path(args.out), hardened)
|
||||
write_json(Path(args.out_report), {
|
||||
"status": "ok",
|
||||
"active_profile_count": len(active),
|
||||
"required_ops": required_ops,
|
||||
"required_reason_keywords": required_reasons,
|
||||
"required_execution_contract_fields": required_contract_fields,
|
||||
"min_target_task_count": min_target,
|
||||
"input_task_count": len(tasks),
|
||||
"output_task_count": len(hardened),
|
||||
})
|
||||
|
||||
print(json.dumps({
|
||||
"status": "ok",
|
||||
"active_profile_count": len(active),
|
||||
"input_task_count": len(tasks),
|
||||
"output_task_count": len(hardened),
|
||||
}, sort_keys=True))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
@@ -52,6 +52,7 @@ NATIVE_MULTISHOT_APPLY_MODE="${WSTONE_NATIVE_MULTISHOT_APPLY_MODE:-if_improves}"
|
||||
NATIVE_RAW_CANDIDATE_SEARCH="${WSTONE_NATIVE_RAW_CANDIDATE_SEARCH:-0}"
|
||||
NATIVE_RAW_CANDIDATE_MAX_VARIANTS="${WSTONE_NATIVE_RAW_CANDIDATE_MAX_VARIANTS:-8}"
|
||||
NATIVE_RAW_CANDIDATE_REQUIRE_UPLIFT="${WSTONE_NATIVE_RAW_CANDIDATE_REQUIRE_UPLIFT:-0}"
|
||||
NATIVE_RAW_HARDEN_TOP_GAPS="${WSTONE_NATIVE_RAW_HARDEN_TOP_GAPS:-0}"
|
||||
EXTRA_NORMALIZED_REQUIREMENTS_FILE="${WSTONE_EXTRA_NORMALIZED_REQUIREMENTS_FILE:-}"
|
||||
EXTRA_TASKS_FILE="${WSTONE_EXTRA_TASKS_FILE:-}"
|
||||
CAPABILITY_SIGNALS_JSON="${WSTONE_CAPABILITY_SIGNALS_JSON:-}"
|
||||
@@ -114,6 +115,7 @@ NATIVE_INTRINSIC_BOOST_JSON='{}'
|
||||
NATIVE_SINGLESHOT_PROFILE_SHAPE_JSON='{}'
|
||||
NATIVE_MULTISHOT_JSON='{}'
|
||||
NATIVE_RAW_CANDIDATE_SEARCH_JSON='{}'
|
||||
NATIVE_RAW_HARDENING_JSON='{}'
|
||||
INTRINSIC_REQS_JSON='[]'
|
||||
EXTRA_NORMALIZED_REQUIREMENTS_JSON='[]'
|
||||
EXTRA_TASKS_JSON='[]'
|
||||
@@ -467,6 +469,30 @@ if [[ "$NATIVE_RAW_CANDIDATE_SEARCH" == "1" ]]; then
|
||||
else
|
||||
NATIVE_RAW_CANDIDATE_SEARCH_JSON='{"enabled":false}'
|
||||
fi
|
||||
if [[ "$NATIVE_RAW_HARDEN_TOP_GAPS" == "1" ]]; then
|
||||
printf '%s\n' "$TASKS" > "$OUT_DIR/02af_raw_hardening_input_tasks.json"
|
||||
python3 "$ROOT_DIR/tools/mcp/harden_native_tasks_top_gaps.py" \
|
||||
--spec "$INPUT_FILE" \
|
||||
--profiles "$NATIVE_IMPACT_COVERAGE_PROFILES" \
|
||||
--tasks "$OUT_DIR/02af_raw_hardening_input_tasks.json" \
|
||||
--out "$OUT_DIR/02af_raw_hardening_output_tasks.json" \
|
||||
--out-report "$OUT_DIR/02af_raw_hardening_report.json" >/dev/null
|
||||
hardened_tasks_json="$(cat "$OUT_DIR/02af_raw_hardening_output_tasks.json")"
|
||||
input_count_h="$(printf '%s' "$TASKS" | jq 'length')"
|
||||
output_count_h="$(printf '%s' "$hardened_tasks_json" | jq 'length')"
|
||||
if [[ "$output_count_h" -gt "$input_count_h" ]]; then
|
||||
added_h_tasks="$(jq -nc --argjson shaped "$hardened_tasks_json" --argjson n "$input_count_h" '$shaped[$n:]')"
|
||||
EXTRA_TASKS_JSON="$(jq -nc --argjson base "$EXTRA_TASKS_JSON" --argjson extra "$added_h_tasks" '$base + $extra')"
|
||||
fi
|
||||
TASKS="$hardened_tasks_json"
|
||||
NATIVE_RAW_HARDENING_JSON="$(jq -nc \
|
||||
--argjson rep "$(cat "$OUT_DIR/02af_raw_hardening_report.json")" \
|
||||
--argjson input_count "$input_count_h" \
|
||||
--argjson output_count "$output_count_h" \
|
||||
'{enabled:true, input_task_count:$input_count, output_task_count:$output_count, report:$rep}')"
|
||||
else
|
||||
NATIVE_RAW_HARDENING_JSON='{"enabled":false}'
|
||||
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" \
|
||||
@@ -787,6 +813,7 @@ SUMMARY_JSON="$(jq -nc \
|
||||
--argjson native_profile_autofill "$NATIVE_PROFILE_AUTOFILL_JSON" \
|
||||
--argjson native_intrinsic_boost "$NATIVE_INTRINSIC_BOOST_JSON" \
|
||||
--argjson native_raw_candidate_search "$NATIVE_RAW_CANDIDATE_SEARCH_JSON" \
|
||||
--argjson native_raw_hardening "$NATIVE_RAW_HARDENING_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" \
|
||||
@@ -815,6 +842,7 @@ SUMMARY_JSON="$(jq -nc \
|
||||
native_profile_autofill: $native_profile_autofill,
|
||||
native_intrinsic_boost: $native_intrinsic_boost,
|
||||
native_raw_candidate_search: $native_raw_candidate_search,
|
||||
native_raw_hardening: $native_raw_hardening,
|
||||
native_single_shot_profile_shape: $native_single_shot_profile_shape,
|
||||
native_multishot: $native_multishot,
|
||||
extra_normalized_requirements: $extra_normalized_requirements,
|
||||
|
||||
Reference in New Issue
Block a user