Files
notplanned/ventures/codex-goal/demo.py
T
claudeandClaude Sonnet 5 70b7a8bbcd Build codex-goal venture: persistent objectives for codex exec sessions
Inspired by openai/codex#26966 (closed not planned). codex_goal.py is a
standalone CLI that stores one versioned, project-keyed goal per repo
outside Codex's own sandbox, and injects it as plain user context on
every new or resumed `codex exec` run. Writes are atomic and locked,
runs never mutate the goal, Codex is spawned via argv (no shell), and
signals are forwarded for normal cancellation behavior. 16 unit tests
plus a recorded two-run + crash-recovery demo, verified on macOS and
in the project's Colima Linux container. Launch page at
ventures/codex-goal/site/index.html uses a self-contained
scoreboard/arcade identity (vendored OFL DSEG7 font, no external
requests) distinct from the other ventures.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-10 13:37:00 -07:00

105 lines
3.7 KiB
Python

#!/usr/bin/env python3
"""Recorded, runnable demo for codex-goal.
Uses a disposable project dir, a disposable $CODEX_GOAL_HOME, and the
deterministic tests/fake_codex.py stand-in -- no network calls, no real
Codex account/auth needed, nothing billed. It demonstrates:
1. `set` then `show` (round trip).
2. Run 1: a fresh `codex exec`, goal injected, fake run exits 0.
3. Run 2: `--last` resume, goal injected again -- same goal survives
across the two separate process invocations (the thing upstream
closed as not planned).
4. A run that crashes (killed mid-flight): the stored goal is untouched
afterward, proving the wrapper never half-writes state.
Prints each command and its output, so this file doubles as demo.txt's
generator: `python3 demo.py > demo.txt`.
"""
import json
import os
import shlex
import subprocess
import sys
import tempfile
HERE = os.path.dirname(os.path.abspath(__file__))
TOOL = os.path.join(HERE, "codex_goal.py")
FAKE_CODEX = os.path.join(HERE, "tests", "fake_codex.py")
def display(argv, project):
pretty = []
for a in argv:
if a == sys.executable:
pretty.append("python3")
elif a == TOOL:
pretty.append("codex_goal.py")
elif a == FAKE_CODEX:
pretty.append("tests/fake_codex.py")
elif a == project:
pretty.append("$PROJECT")
else:
pretty.append(a)
return " ".join(shlex.quote(a) for a in pretty)
def run(argv, env, label, project):
print(f"$ {display(argv, project)}")
proc = subprocess.run(argv, env=env, capture_output=True, text=True)
out = (proc.stdout + proc.stderr).rstrip()
if out:
print(out)
print(f"[exit {proc.returncode}] {label}\n")
return proc
def main() -> int:
os.chmod(FAKE_CODEX, 0o755)
with tempfile.TemporaryDirectory() as home, tempfile.TemporaryDirectory() as parent:
project = os.path.join(parent, "myapp")
os.makedirs(project)
env = dict(os.environ)
env["CODEX_GOAL_HOME"] = home
env.pop("FAKE_CODEX_SLEEP", None)
print("# 1. set + show")
run([sys.executable, TOOL, "set", "-C", project,
"Ship codex-goal's two-run persistence demo"], env, "goal saved", project)
run([sys.executable, TOOL, "show", "-C", project], env, "goal shown", project)
print("# 2. run 1 -- fresh session (fake codex, exit 0)")
env1 = dict(env, FAKE_CODEX_EXIT="0")
run([sys.executable, TOOL, "run", "-C", project,
"--codex-bin", FAKE_CODEX], env1, "run 1 done", project)
print("# 3. run 2 -- resume --last, in a *new* process, goal still there")
run([sys.executable, TOOL, "run", "-C", project, "--last",
"--codex-bin", FAKE_CODEX], env1, "run 2 done", project)
print("# 4. crash mid-run: kill -9 the wrapper while codex is 'working'")
env2 = dict(env, FAKE_CODEX_SLEEP="3")
proc = subprocess.Popen(
[sys.executable, TOOL, "run", "-C", project, "--codex-bin", FAKE_CODEX],
env=env2,
)
import time
time.sleep(0.4)
proc.kill()
proc.wait()
print(f"$ (background run) ; kill -9 {proc.pid}\n[killed] simulated crash\n")
print("# 5. show again -- goal survived the crash, byte for byte")
show = run([sys.executable, TOOL, "show", "-C", project, "--json"],
env, "goal after crash", project)
payload = json.loads(show.stdout)
assert payload["current"]["text"] == "Ship codex-goal's two-run persistence demo", (
"goal did not survive the crash"
)
print("ASSERTION PASSED: goal text identical before and after the crash.")
return 0
if __name__ == "__main__":
sys.exit(main())