Spaces:
Sleeping
Sleeping
| # codex_logger.py | |
| # Author: Liam Grinstead | |
| # Logs activation, mutation, and collapse with author credit and hash | |
| # All logs are saved as JSON files with timestamp and SHA-512 seal | |
| import json | |
| import os | |
| import hashlib | |
| from datetime import datetime | |
| LOG_DIR = "logs" | |
| os.makedirs(LOG_DIR, exist_ok=True) | |
| def log_artifact(agent, score, hash_val): | |
| """ | |
| Log an artifact with timestamp, agent details, fitness score, and SHA-512 seal. | |
| Saves to logs directory as JSON. | |
| """ | |
| timestamp = datetime.utcnow().isoformat() | |
| log = { | |
| "timestamp": timestamp, | |
| "agent_id": agent.get("id", "Unknown"), | |
| "status": agent.get("status"), | |
| "fitness_score": score, | |
| "hash": hash_val, | |
| "author": "Liam Grinstead" | |
| } | |
| # Seal the log entry itself | |
| entry_str = json.dumps(log, sort_keys=True) | |
| log["sha512"] = hashlib.sha512(entry_str.encode()).hexdigest() | |
| # Save to file | |
| filename = f"{LOG_DIR}/artifact_{timestamp}.json" | |
| with open(filename, "w") as f: | |
| json.dump(log, f, indent=2) | |
| print("Artifact Logged:", log) | |
| return log | |
| def list_logs(): | |
| """ | |
| List all saved logs. | |
| """ | |
| return sorted(os.listdir(LOG_DIR)) | |
| def load_log(filename): | |
| """ | |
| Load a specific log file. | |
| """ | |
| filepath = os.path.join(LOG_DIR, filename) | |
| with open(filepath, "r") as f: | |
| return json.load(f) | |