← back to Terminal Status

configure_codex.py

63 lines

#!/usr/bin/env python3
"""One-time local Codex title/default instructions; private preimage backups."""
import hashlib
import json
from pathlib import Path
import re
from terminal_status import atomic_write

root = Path.home()
backup = root / '.local/state/abrams-terminal-status/config-backups'
config = root / '.codex/config.toml'
instructions = root / '.codex/AGENTS.md'
changes = {}
text = config.read_text()
if re.search(r'^\[tui\]\s*$', text, re.M):
    match = re.search(r'^\[tui\]\s*\n(?P<body>.*?)(?=^\[|\Z)', text, re.M | re.S)
    body = match['body']
    if re.search(r'^terminal_title\s*=', body, re.M):
        body = re.sub(r'^terminal_title\s*=.*$', 'terminal_title = []', body, flags=re.M)
    else:
        body = 'terminal_title = []\n' + body
    text = text[:match.start('body')] + body + text[match.end('body'):]
else:
    text += '\n# Shared TK/dot renderer owns terminal titles.\n[tui]\nterminal_title = []\n'
changes[config] = text
text = instructions.read_text()
marker = '## Every Codex build displays its ticket and semantic dot'
if marker not in text:
    text += '''

## Every Codex build displays its ticket and semantic dot

At the start of every non-trivial build, use the canonical tickets MCP
(CLI fallback ~/Projects/ticket-system/tk) to find/create/take its ticket.
Immediately bind it to this session:

    python3 ~/Projects/terminal-status/terminal_status.py ticket TK-NUMBER

Keep the ticket visible in the terminal header, badge, and desktop dots menu.
Set status using the existing Codex dot skills, all backed by this shared engine:
green = working; yellow = direction needed; purple = approval gated;
orange = pasted input needed; pink = parked. Put the short reason in the label.
Generic WORKING/repaint hooks preserve the ticket; never write a competing
status file or start a title-paint loop. For monitoring with a scheduled check,
use set green "MONITORING · next TIME" --monitoring on the same engine.
Update the binding when the main task changes. An unbound build must be
visibly TK REQUIRED until claimed; never invent or reuse a stale ticket.
Subagents inherit the ticket and must never paint their parent's terminal.
Implementation and rollback: ~/Projects/terminal-status/README.md.
'''
changes[instructions] = text
report = []
for path, desired in changes.items():
    original = path.read_text()
    saved = backup / (path.name + '.before')
    if not saved.exists():
        atomic_write(saved, original)
    if original != desired:
        atomic_write(path, desired)
    report.append({'path': str(path), 'backup': str(saved),
                   'after_sha256': hashlib.sha256(desired.encode()).hexdigest()})
print(json.dumps(report, indent=2))