58 lines
2.0 KiB
Python
58 lines
2.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Materialize and validate the bounded context packet for one Web task."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from web_context import CURRENT, context_manifest, write_context_manifest, write_context_summary
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("task_id", nargs="?", help="task card id; defaults to active_task")
|
|
parser.add_argument("--output", type=Path, help="optional manifest path")
|
|
args = parser.parse_args()
|
|
if args.task_id:
|
|
task_id = args.task_id
|
|
else:
|
|
state = json.loads((CURRENT / "task-state.json").read_text(encoding="utf-8"))
|
|
task_id = state.get("active_task")
|
|
if not task_id:
|
|
parser.error("no active task; pass a task id")
|
|
try:
|
|
manifest = context_manifest(task_id)
|
|
except (KeyError, FileNotFoundError, json.JSONDecodeError) as exc:
|
|
print(f"CONTEXT PACKET FAILED: {exc}")
|
|
return 1
|
|
output = write_context_manifest(task_id, args.output, manifest=manifest)
|
|
summary = write_context_summary(task_id, manifest=manifest)
|
|
limits = manifest["budget"]
|
|
totals = manifest["totals"]
|
|
over = []
|
|
if totals["bytes"] > limits["total_bytes_limit"]:
|
|
over.append("total bytes")
|
|
if totals["evidence_bytes"] > limits["evidence_bytes_limit"]:
|
|
over.append("evidence bytes")
|
|
if totals["evidence_files"] > limits["evidence_file_limit"]:
|
|
over.append("evidence file count")
|
|
print(
|
|
f"CONTEXT PACKET {'FAILED' if over else 'READY'}: {task_id}; "
|
|
f"{totals['bytes']}B/{limits['total_bytes_limit']}B, "
|
|
f"~{totals['estimated_tokens']} tokens, "
|
|
f"{totals['evidence_files']} evidence files; manifest={output}; summary={summary}"
|
|
)
|
|
if over:
|
|
print("- exceeds: " + ", ".join(over))
|
|
return 1
|
|
for item in manifest["excluded"]:
|
|
print(f"- excluded: {item['path']} ({item['reason']})")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|