#!/usr/bin/env python3 """Post-write hook to update entity status in manifest.""" import argparse import json import os from datetime import datetime def load_manifest(manifest_path: str) -> dict | None: """Load manifest if it exists.""" if not os.path.exists(manifest_path): return None with open(manifest_path) as f: return json.load(f) def save_manifest(manifest_path: str, manifest: dict): """Save manifest to file.""" with open(manifest_path, "w") as f: json.dump(manifest, f, indent=2) def find_entity_by_path(manifest: dict, file_path: str) -> tuple: """Find entity by file path, return (entity_type, index, entity).""" entities = manifest.get("entities", {}) for entity_type in ["pages", "components", "api_endpoints", "database_tables"]: for idx, entity in enumerate(entities.get(entity_type, [])): if entity.get("file_path") == file_path: return (entity_type, idx, entity) return (None, None, None) def main(): parser = argparse.ArgumentParser(description="Post-write hook") parser.add_argument("--manifest", required=True, help="Path to manifest") parser.add_argument("--file", help="File that was written") args = parser.parse_args() manifest = load_manifest(args.manifest) if manifest is None: return 0 # If file provided, update entity status if args.file: # Normalize the file path file_path = args.file.lstrip('./') entity_type, idx, entity = find_entity_by_path(manifest, args.file) # Try without leading ./ if not entity: entity_type, idx, entity = find_entity_by_path(manifest, file_path) if entity and entity.get("status") == "APPROVED": manifest["entities"][entity_type][idx]["status"] = "IMPLEMENTED" manifest["entities"][entity_type][idx]["implemented_at"] = datetime.now().isoformat() # Add to history (ensure it exists) if "state" not in manifest: manifest["state"] = {} if "revision_history" not in manifest["state"]: manifest["state"]["revision_history"] = [] manifest["state"]["revision_history"].append({ "action": "ENTITY_IMPLEMENTED", "timestamp": datetime.now().isoformat(), "details": f"Implemented {entity.get('id', 'unknown')}" }) save_manifest(args.manifest, manifest) print(f"GUARDRAIL: Updated {entity.get('id')} to IMPLEMENTED") return 0 if __name__ == "__main__": exit(main())