#!/usr/bin/env python3 """Pexpect smoke test for the /goal extension against MiniMax.""" from __future__ import annotations import argparse import re import sys import time from pathlib import Path try: import pexpect except ImportError as e: raise SystemExit("missing Python package: pexpect") from e ROOT = Path(__file__).resolve().parents[1] OUT = ROOT / ".demo" MODEL = "minimax/MiniMax-M2.7-highspeed" IDEAS = [ "Reply exactly PEXPECT_GOAL_OK and nothing else.", "Write exactly one sentence about persistent goals.", "List exactly three demo-safe commands a user might try.", "Explain goal status in exactly nine words.", "Create a tiny three-item checklist for finishing a code review.", "Write a haiku about persistent goals.", "Name one risk in endless autonomous loops.", "Draft a one-line release note for a goal-loop extension.", "Say whether a clear command should survive reloads, with one reason.", "Produce a two-item TODO list for improving tests.", ] ANSI_RE = re.compile(r"\x1b\[[0-?]*[ -/]*[@-~]") OSC_RE = re.compile(r"\x1b\].*?\x07") def clean(text: str) -> str: return OSC_RE.sub("", ANSI_RE.sub("", text)).replace("\r", "") class Driver: def __init__(self, child: pexpect.spawn): self.child = child self.raw = "" def pump(self, seconds: float) -> None: end = time.time() + seconds while time.time() < end and self.child.isalive(): try: data = self.child.read_nonblocking(size=8192, timeout=0.05) if data: self.raw += data sys.stdout.write(data) sys.stdout.flush() except pexpect.TIMEOUT: pass except pexpect.EOF: break def type_line(self, text: str, after: float) -> None: for ch in text: self.child.send(ch) self.pump(0.025) self.child.send("\r") self.pump(after) def pump_until(self, predicate, timeout: float) -> None: end = time.time() + timeout while time.time() < end and not predicate(self.text): self.pump(0.25) @property def text(self) -> str: return clean(self.raw) def require(self, needle: str) -> None: if needle.lower() not in self.text.lower(): raise AssertionError(f"missing {needle!r}") def pi_command() -> str: return " ".join( [ "pi", "--model", MODEL, "--thinking", "off", "--no-session", "--no-context-files", "--no-skills", "--no-prompt-templates", "--no-extensions", "--no-builtin-tools", "-e", "extensions/goal", ] ) def run(objective: str, llm_wait: float) -> Path: OUT.mkdir(exist_ok=True) child = pexpect.spawn( pi_command(), cwd=str(ROOT), encoding="utf-8", timeout=max(90, int(llm_wait + 30)), dimensions=(28, 100), ) d = Driver(child) try: d.pump(5) d.require("minimax") d.require("MiniMax-M2.7-highspeed") d.type_line("/goal status", 3) d.require("No active goal") d.type_line(f"/goal {objective}", 1) d.require("Goal set") d.pump_until( lambda text: ( "goal marked complete" in text.lower() or "goal complete" in text.lower() or "complete ยท" in text.lower() ), llm_wait, ) d.type_line("/goal status", 4) d.require(objective) if "complete" not in d.text.lower(): raise AssertionError("goal did not reach complete status") d.type_line("/goal clear", 2) d.require("Goal cleared") finally: try: child.sendcontrol("c") d.pump(0.5) finally: child.terminate(force=True) transcript = OUT / "goal_pexpect_smoke.txt" transcript.write_text(d.text) return transcript def main() -> int: parser = argparse.ArgumentParser() parser.add_argument("--idea", type=int, default=1, choices=range(1, len(IDEAS) + 1)) parser.add_argument("--objective") parser.add_argument("--llm-wait", type=float, default=45.0) parser.add_argument("--list-ideas", action="store_true") args = parser.parse_args() if args.list_ideas: for i, idea in enumerate(IDEAS, 1): print(f"{i}. {idea}") return 0 objective = args.objective or IDEAS[args.idea - 1] transcript = run(objective, args.llm_wait) print(f"\nOK goal pexpect smoke via {MODEL}") print(f"OK transcript {transcript}") return 0 if __name__ == "__main__": raise SystemExit(main())