Skip to content

MongLong0214/frontier-simplify / skills/frontier-simplify-review

frontier-simplify-review

Unverified

Review a change for concrete defects, check them with a refuter and a reproducer, then check repairs and their regressions against the preserved review. Use for substantial PR reviews or repeated review rounds. Stops automatic review after at most three attempts per PR and names the calling agent's next action; does not guarantee safe merge convergence.

scripts/lib/witness.py

View on GitHub
"""Replay one unchanged, top-level Node test against two exact commits. No model verdicts."""
import argparse
import json
import math
import os
from pathlib import Path
import re
import signal
import subprocess
import sys
import tempfile

from protocol import digest, require, Rejected, child_environment

HERE = Path(__file__).resolve().parent


def environment():
    env = dict(child_environment(), PYTHONDONTWRITEBYTECODE='1')
    env.pop('NODE_OPTIONS', None)
    return env


def git(repo, *args):
    return subprocess.check_output(['git', '-C', str(repo), *args], env=environment(), stderr=subprocess.PIPE)


def finite_timeout(value):
    try:
        seconds = float(value)
    except (TypeError, ValueError, OverflowError):
        require(False, 'witness-timeout', 'timeout must be positive finite seconds')
    require(math.isfinite(seconds) and seconds > 0, 'witness-timeout',
            'timeout must be positive finite seconds')
    return seconds


def run_named(checkout, file, name, output, timeout=60):
    """Accept only a unique, unskipped named test result, never a process failure as a kill."""
    timeout = finite_timeout(timeout)
    # Escape for JavaScript's RegExp, whose accepted escapes differ from Python's re.escape.
    pattern = '^' + re.sub(r'([\\^$.*+?()\[\]{}|])', r'\\\1', name) + '$'
    command = ['node', '--test', '--test-reporter=' + str(HERE / 'node-witness-reporter.mjs'),
               '--test-name-pattern=' + pattern, file]
    (output / 'command.txt').write_text('\n'.join(command) + '\n')
    def cancelled(signum, frame):
        raise InterruptedError(f'witness cancelled by {signal.Signals(signum).name}')
    old_handlers = {}
    try:
        for sig in (signal.SIGINT, signal.SIGTERM):
            old_handlers[sig] = signal.signal(sig, cancelled)
        with (output / 'events.jsonl').open('wb') as stdout, (output / 'stderr.txt').open('wb') as stderr:
            child = None
            try:
                # Defer cancellation until Popen has returned the group we must reap.
                previous_mask = signal.pthread_sigmask(signal.SIG_BLOCK, {signal.SIGINT, signal.SIGTERM})
                try:
                    child = subprocess.Popen(command, cwd=checkout, env=environment(), stdout=stdout,
                                             stderr=stderr, stdin=subprocess.DEVNULL, start_new_session=True)
                finally:
                    signal.pthread_sigmask(signal.SIG_SETMASK, previous_mask)
                try:
                    rc = child.wait(timeout=timeout)
                except subprocess.TimeoutExpired:
                    (output / 'exit.txt').write_text('TIMEOUT\n')
                    raise Rejected('GUARD FAIL [witness-timeout] no completed named assertion')
                except InterruptedError as error:
                    (output / 'exit.txt').write_text(str(error) + '\n')
                    raise Rejected('GUARD FAIL [witness-cancelled] ' + str(error)) from error
                (output / 'exit.txt').write_text(str(rc) + '\n')
            finally:
                if child is not None:
                    try:
                        os.killpg(child.pid, signal.SIGKILL)
                    except ProcessLookupError:
                        pass
                    child.wait()
    finally:
        for sig, handler in old_handlers.items():
            signal.signal(sig, handler)
    rows = [json.loads(line) for line in (output / 'events.jsonl').read_text().splitlines()]
    named = [r for r in rows if r.get('name') == name and r.get('file') and
             Path(r['file']).resolve() == (checkout / file).resolve()]
    require(len(named) == 1, 'witness-selection', 'expected exactly one result for the named test in its file')
    result = named[0]
    require(result.get('kind') == 'test' and result.get('line') and
            not result.get('skip') and not result.get('todo'), 'witness-selection',
            'a suite, wrapper, skipped or TODO test is not an executed witness')
    if result['type'] == 'test:pass':
        require(rc == 0, 'witness-process', 'named test passed but its process failed')
        return 'PASS'
    require(rc > 0 and result.get('code') == 'ERR_ASSERTION' and
            result.get('failure') == 'testCodeFailure', 'witness-assertion',
            'the named test did not fail through an assertion; inspect raw events')
    return 'ASSERTION_FAILED'


def replay(repo, before, after, file, name, source, lesson, output, timeout=60):
    timeout = finite_timeout(timeout)
    repo, source, output = Path(repo).resolve(), Path(source).resolve(), Path(output).resolve()
    require(not output.is_relative_to(repo), 'witness-location', 'keep replay output outside the consumer checkout')
    path = Path(file)
    require(not path.is_absolute() and '..' not in path.parts and path.suffix == '.mjs',
            'witness-path', 'name a repository-relative .mjs test file')
    before, after = [git(repo, 'rev-parse', '--verify', ref + '^{commit}').decode().strip()
                     for ref in (before, after)]
    require(before != after, 'witness-target', 'before and after must be distinct commits')
    test = git(repo, 'show', f'{after}:{file}')
    source_bytes = source.read_bytes()
    require(test.strip() and source_bytes.strip() and lesson.strip() and name.strip(),
            'witness-input', 'test, test name, source review and lesson must be nonempty')
    # A failed replay cannot leave an older successful lead at this destination.
    output.mkdir(parents=True, exist_ok=False)
    version = subprocess.check_output(['node', '--version'], env=environment()).decode().strip()
    facts = (f'Before: {before}\nAfter: {after}\nNode: {version}\nTest: {file}\nName: {name}\n'
             f'Unchanged test SHA-256: {digest(test)}\nSource review: {source}\n'
             f'Source review SHA-256: {digest(source_bytes)}\n')
    (output / 'REPLAY.txt').write_text(facts)
    outcomes = []
    for label, head in [('before', before), ('after', after)]:
        logs = output / label
        logs.mkdir()
        with tempfile.TemporaryDirectory(prefix='witness-', dir='/tmp') as temp:
            checkout = Path(temp).resolve() / 'repo'
            subprocess.run(['git', 'clone', '--quiet', '--no-hardlinks', '--no-checkout', str(repo), str(checkout)],
                           env=environment(), check=True, stderr=subprocess.PIPE)
            git(checkout, 'checkout', '--quiet', '--detach', head)
            target = checkout / file
            require(target.resolve().is_relative_to(checkout) and not target.is_symlink(),
                    'witness-path', 'test path escapes the disposable checkout')
            target.parent.mkdir(parents=True, exist_ok=True)
            target.write_bytes(test)
            outcome = run_named(checkout, file, name, logs, timeout)
            require(target.read_bytes() == test, 'witness-bytes', 'execution changed the witness')
            outcomes.append(outcome)
            with (output / 'REPLAY.txt').open('a') as out:
                out.write(f'{label}: {outcome}; events SHA-256: {digest((logs / "events.jsonl").read_bytes())}\n')
    require(outcomes == ['ASSERTION_FAILED', 'PASS'], 'witness-contrast',
            'need the same named assertion failing before and passing after; no lead published')
    lead = ('Replayed review lead, selected by the host; investigate its applicability to this change.\n'
            + lesson.strip() + '\n\n' + (output / 'REPLAY.txt').read_text()
            + 'Observed contrast: named assertion failed before and passed after.\n'
            'This does not establish causal attribution to the source review, independent recurrence, '
            'complete coverage, reviewer recall or merge safety. Source prose was not parsed.\n')
    (output / 'LEAD.md').write_text(lead)
    return lead


def main():
    p = argparse.ArgumentParser(description=__doc__)
    for field in ('repository', 'before', 'after', 'file', 'name', 'source_review', 'output'):
        p.add_argument(field)
    p.add_argument('--lesson', required=True, help='host-selected question; never inferred from a verdict')
    p.add_argument('--timeout', type=float, default=60, help='seconds per named test process')
    a = p.parse_args()
    try:
        print(replay(a.repository, a.before, a.after, a.file, a.name, a.source_review,
                     a.lesson, a.output, a.timeout))
        return 0
    except (Rejected, OSError, ValueError, subprocess.CalledProcessError) as e:
        print('Witness replay unavailable: ' + str(e), file=sys.stderr)
        return 5


if __name__ == '__main__':
    sys.exit(main())
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall