Something went wrong. Try again.
Personal fork of prime agent. Mirror of: https://mnemosyne.sigilzero.dev/sigilzero/prime-agent
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281#!/usr/bin/env python3"""Hosted actual TS/Rust quoted @ completion keys and menu captures.
Ordinary PTY clients use a controlled protocol-7 peer with no selected model.The actual binaries enumerate a temporary project containing spaces. Rust mustrefresh directory children after Tab, Enter, a typed quoted slash, and deletinginside that quoted token. TS'sobserved reaction and complete frames are retained as an intentional comparison,not normalized into equality or used to claim the new ranking matches TS."""import argparseimport jsonimport osfrom pathlib import Pathimport signalimport shutilimport subprocessimport sysimport tempfileimport time
from review_agent_bounce_parity import Clientfrom review_dock_screen import DockScreenfrom review_draft_disconnect_parity import ( Supervisor, Terminal, digest, env_for, TS_ARCHIVE_SHA256, TS_SOURCE_COMMIT,)from review_goal_fixture_lifecycle import OwnedProcesses, TAG, finish, process_identity
DIRECTORY = 'dir with spaces'CHILD = 'only_child.rs'APPLIED = '@"dir with spaces/"'CASES = ('tab', 'enter', 'typed_slash', 'backspace', 'delete')
def completion_rows(text): """Match actual menu rows, excluding the editor's quoted token and chrome.""" return [line.strip() for line in text.splitlines() if (CHILD in line or DIRECTORY + '/' in line) and '@' not in line]
def children_visible(text): return any(CHILD in row for row in completion_rows(text))
def frame(terminal): screen = terminal.screen assert not screen.unsupported_terminal, screen.unsupported_terminal assert not screen.unsupported_sgr, screen.unsupported_sgr return {'text': screen.text(), 'menu_rows': completion_rows(screen.text()), 'children_visible': children_visible(screen.text()), 'cursor': [screen.row, screen.col], 'raw_ansi_complete_hex': bytes(terminal.raw).hex()}
def stable_frame(terminal, server, label, predicate): terminal.wait(label, lambda: predicate(terminal.screen.text()), server) previous, count = None, 0
def stable(): nonlocal previous, count current = terminal.screen.text() count = count + 1 if current == previous else 0 previous = current return count >= 3 and predicate(current)
terminal.wait(label + ' stable observable frame', stable, server) return frame(terminal)
def discover_direct(scope, command): """Recover an owned spawn even if its PTY constructor failed before assignment.""" expected = [str(arg).encode() for arg in command] for entry in Path('/proc').iterdir(): if not entry.name.isdigit(): continue pid = int(entry.name) try: identity, state = process_identity(pid) if state == 'Z': continue env = dict(item.split(b'=', 1) for item in (entry / 'environ').read_bytes().split(b'\0') if b'=' in item) argv = (entry / 'cmdline').read_bytes().rstrip(b'\0').split(b'\0') if env.get(TAG.encode()) != str(scope.root).encode() or argv != expected: continue scope.direct[pid] = (identity, expected) scope.targets[pid] = identity if not scope.ownership(pid, identity): scope.direct.pop(pid, None) scope.targets.pop(pid, None) except (OSError, ValueError): continue scope.discover()
def discover_hello(binary, kind, root, observed): """Learn the real schema; graceful ACK + close + exit, with fallback RED.""" root.mkdir(parents=True) env = env_for(root, kind) env[TAG] = str(root) socket_path = root / 'hello.sock' scope = OwnedProcesses(binary, root, socket_path) process = client = None command = [str(binary), '--mode', 'daemon', '--daemon-socket', str(socket_path)] with (root / 'hello.log').open('wb') as log: try: process = subprocess.Popen(command, cwd=root, env=env, stdout=log, stderr=subprocess.STDOUT, start_new_session=True) scope.register(process, command) client = Client(socket_path, process) hello = client.hello assert hello.get('schemaId'), 'actual daemon greeting lacks schema identity' observed['hello'] = hello finally: discover_direct(scope, command) finish(scope, client, process, observed) observed['daemon_log'] = (root / 'hello.log').read_text(errors='replace') assert observed.get('cleanup_verified'), observed return hello
def close_terminal(terminal, scope, server, observed): """Exit using normal editor keys; forced PIDFD cleanup never passes.""" observed['cleanup_verified'] = False errors = [] try: discover_direct(scope, [str(scope.binary), '--daemon-socket', str(server.path)]) if terminal is not None: terminal.send('\x15\x0b\x04') # Delete both sides of cursor, Ctrl+D on empty input. deadline = time.monotonic() + 12 while terminal.process.poll() is None and time.monotonic() < deadline: terminal.pump() assert terminal.process.poll() == 0, 'ordinary TUI exit did not complete cleanly' scope.wait_gone(3) except Exception as error: errors.append(str(error)) observed['fallback_signals'] = [] scope.discover() for sig in (signal.SIGTERM, signal.SIGKILL): for pid in scope.live(): if scope.send(pid, sig): observed['fallback_signals'].append({'pid': pid, 'signal': sig.name}) if terminal is not None: try: terminal.process.wait(timeout=3) except subprocess.TimeoutExpired: pass try: scope.wait_gone(3) except Exception as fallback_error: errors.append(str(fallback_error)) finally: if terminal is not None: observed['raw_ansi_complete_hex'] = bytes(terminal.raw).hex() try: os.close(terminal.master) except OSError as error: errors.append('owned PTY close: ' + str(error)) try: server.close() except Exception as error: errors.append('controlled supervisor cleanup: ' + str(error)) observed['wire_commands'] = server.commands observed['fixture_errors'] = server.errors observed['owned_process_identities'] = dict(scope.targets) if server.errors: errors.append('controlled supervisor errors: ' + str(server.errors)) if errors: observed['cleanup_errors'] = errors raise AssertionError('; '.join(errors)) observed['cleanup_verified'] = True
def run_case(binary, kind, root, hello, case, observed): root.mkdir(parents=True) env = env_for(root, kind) env[TAG] = str(root) (root / DIRECTORY).mkdir() (root / DIRECTORY / CHILD).write_text('fixture child\n') socket_path = root / 'fixture.sock' server = Supervisor(socket_path, hello, 'at_completion', root) scope = OwnedProcesses(binary, root, socket_path) terminal = None observed['frames'] = {} observed['inputs'] = [] command = [str(binary), '--daemon-socket', str(socket_path)] try: terminal = Terminal(command, root, env) scope.register(terminal.process, command) terminal.screen = DockScreen() terminal.wait('actual ordinary UI attached', lambda: server.attaches >= 1 and bool(terminal.screen.text().strip()), server) token = ('@"dir with spaces' if case == 'typed_slash' else '@"dir with spaces/x' if case in ('backspace', 'delete') else '@dir') terminal.send(token) observed['inputs'].append(token.encode().hex()) if case in ('tab', 'enter'): observed['frames']['before'] = stable_frame(terminal, server, 'directory suggestion', lambda text: any(DIRECTORY + '/' in row for row in completion_rows(text))) else: observed['frames']['before'] = stable_frame(terminal, server, 'quoted token typed', lambda text: token in text) key = {'tab': '\t', 'enter': '\r', 'typed_slash': '/', 'backspace': '\x7f', 'delete': '\x1b[D\x1b[3~'}[case] terminal.send(key) observed['inputs'].append(key.encode().hex()) expected_token = APPLIED if case in ('tab', 'enter') else '@"dir with spaces/' observed['frames']['after'] = stable_frame(terminal, server, 'directory action painted', lambda text: expected_token in text and (token not in text if case in ('backspace', 'delete') else True) and (children_visible(text) if kind == 'rust' else True)) assert not server.prompts, 'completion key accidentally submitted a prompt' if kind == 'rust': assert observed['frames']['after']['children_visible'], 'Rust directory children menu missing' observed['behavior_verified'] = True finally: close_terminal(terminal, scope, server, observed)
def compare(receipt): """Fail closed on missing execution/cleanup; report actual differences.""" comparisons = {} for case in CASES: sides = [receipt['runs'][kind]['cases'][case] for kind in ('ts', 'rust')] for side in sides: assert side.get('behavior_verified') and side.get('cleanup_verified'), case assert not side.get('error') and not side.get('cleanup_errors'), case assert sides[1]['frames']['after']['children_visible'], case comparisons[case] = { 'inputs_equal': sides[0]['inputs'] == sides[1]['inputs'], 'ts_children_visible': sides[0]['frames']['after']['children_visible'], 'rust_children_visible': sides[1]['frames']['after']['children_visible'], 'full_text_equal': sides[0]['frames']['after']['text'] == sides[1]['frames']['after']['text'], 'menu_rows_equal': sides[0]['frames']['after']['menu_rows'] == sides[1]['frames']['after']['menu_rows'], } assert comparisons[case]['inputs_equal'], 'different key sequences are not a comparison' return comparisons
def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--receipt', type=Path, required=True) args = parser.parse_args() receipt = {'verified': False, 'runs': {}, 'scope': __doc__, 'ts_source_commit': TS_SOURCE_COMMIT, 'intentional_difference': 'Rust refreshes children after accepting or typing a quoted directory. Ranking changes are intentional; captured rows are not normalized into TS equality.', 'not_verified': ['pixel/font equality', 'complete ranking matrix', 'in-flight search race timing', 'real daemon/provider completion transport', 'model inference', 'file submission']} try: assert sys.platform == 'linux', 'hosted Linux fixture only' assert digest(Path(os.environ['PA_TS_ARCHIVE'])) == TS_ARCHIVE_SHA256, 'TS archive mismatch' receipt['ts_archive_sha256'] = TS_ARCHIVE_SHA256 fd = shutil.which('fd') assert fd, 'real fd executable is required for pinned TS @ completion' receipt['fd'] = {'path': fd, 'sha256': digest(Path(fd)), 'version': subprocess.check_output([fd, '--version'], text=True, timeout=10).strip()} with tempfile.TemporaryDirectory(prefix='pa-at-', dir='/tmp') as folder: for kind in ('ts', 'rust'): binary = Path(os.environ['PA_' + kind.upper() + '_BINARY']).resolve() side = receipt['runs'][kind] = {'binary_sha256': digest(binary), 'hello_discovery': {}, 'cases': {}} hello = discover_hello(binary, kind, Path(folder) / (kind + '-hello'), side['hello_discovery']) for case in CASES: observed = side['cases'][case] = {} try: run_case(binary, kind, Path(folder) / (kind + '-' + case + ' project space'), hello, case, observed) except Exception as error: observed['error'] = str(error) receipt['comparisons'] = compare(receipt) receipt['verified'] = True except Exception as error: receipt['error'] = str(error) args.receipt.parent.mkdir(parents=True, exist_ok=True) args.receipt.write_text(json.dumps(receipt, ensure_ascii=False, indent=2)) print(json.dumps({'verified': receipt['verified'], 'error': receipt.get('error')})) return 0 if receipt['verified'] else 1
if __name__ == '__main__': sys.exit(main())