#!/usr/bin/env python3 """Codex SessionStart hook: inject AIPass identity context. Reads .trinity/passport.json and branch prompt, outputs Codex-format JSON with additionalContext for identity injection. """ import json import os import sys from pathlib import Path def find_repo_root(): """Walk up to find .git directory.""" p = Path.cwd() while p != p.parent: if (p / ".git").exists(): return p p = p.parent return None def get_branch_from_cwd(repo_root): """Determine branch name from CWD relative to repo.""" cwd = Path.cwd() try: rel = cwd.relative_to(repo_root / "src" / "aipass") return str(rel).split("/")[0] except ValueError: try: rel = cwd.relative_to(repo_root / "src") return str(rel).split("/")[0] except ValueError: return None def main(): try: input_data = json.loads(sys.stdin.read()) except Exception: input_data = {} repo_root = find_repo_root() if not repo_root: print(json.dumps({})) return context_parts = [] # 1. Global prompt global_prompt = repo_root / ".aipass" / "aipass_global_prompt.md" if global_prompt.exists(): context_parts.append(global_prompt.read_text(encoding="utf-8")[:8000]) # 2. Branch identity branch = get_branch_from_cwd(repo_root) if branch: branch_dir = repo_root / "src" / "aipass" / branch if not branch_dir.exists(): branch_dir = repo_root / "src" / branch # Passport passport = branch_dir / ".trinity" / "passport.json" if passport.exists(): try: data = json.loads(passport.read_text(encoding="utf-8")) identity = data.get("identity", {}) context_parts.append( f"# Branch Identity: {branch.upper()}\n" f"Role: {identity.get('role', 'unknown')}\n" f"Purpose: {identity.get('purpose', 'unknown')}\n" f"Class: {identity.get('citizen_class', 'unknown')}" ) except Exception: pass # Branch prompt branch_prompt = branch_dir / ".aipass" / "aipass_local_prompt.md" if branch_prompt.exists(): context_parts.append(branch_prompt.read_text(encoding="utf-8")[:4000]) if context_parts: context = "\n\n---\n\n".join(context_parts) output = { "hookSpecificOutput": { "hookEventName": "SessionStart", "additionalContext": context } } else: output = {} print(json.dumps(output)) if __name__ == "__main__": main()