#!/usr/bin/env python3 """Exercise offline capture and server incorporation using disposable native projects.""" import argparse import hashlib import json from pathlib import Path import subprocess import tempfile def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--binary", type=Path, required=True) args = parser.parse_args() binary = args.binary.resolve() with tempfile.TemporaryDirectory(prefix="aie-native-requests-") as temporary: base = Path(temporary).resolve() source, destination = base / "source", base / "destination" source.mkdir() destination.mkdir() def run(cwd, *argv, expected=0): result = subprocess.run([str(binary), *argv], cwd=cwd, text=True, capture_output=True, timeout=25) if result.returncode != expected: raise AssertionError(f"Command {argv!r}: exit {result.returncode}\n{result.stderr}\n{result.stdout}") return result.stdout def command(*argv): return run(destination, *argv) try: run(source, "init", "--name", "Request producer") command("init", "--name", "Request consumer") nested = source / "nested" nested.mkdir() original = "\nUnicode é 请求\n\n---\n indentation\n" (nested / "text.txt").write_text(original, encoding="utf-8") database = destination / ".ai-engineering/aie.db" before = hashlib.sha256(database.read_bytes()).hexdigest() created = json.loads(run(nested, "request", "create", "Parser API", "--project", str(destination), "--author", "source-agent", "--source-work", "WRK-000A", "--text-file", "text.txt", "--format", "json")) second = json.loads(run(source, "--project", str(destination), "request", "create", "Follow-up", "--author", "source-agent", "--text", "Inline request", "--format", "json")) assert created["request"]["text"] == original assert created["request"]["sourceProject"] == str(source) assert created["request"]["sourceWork"] == "WRK-000A" assert hashlib.sha256(database.read_bytes()).hexdigest() == before for project in (source, destination): assert not (project / ".ai-engineering/.local/server").exists(), "Capture started a server" command("work", "create", "Handle parser Requests", "--mode", "EXPLORATORY", "--author", "smoke", "--choice-reason", "Explicit disposable smoke fixture", "--human-approved") pending = json.loads(command("request", "list", "--format", "json")) assert len(pending) == 2 identifier = created["request"]["id"] assert json.loads(command("request", "show", identifier, "--format", "json"))["text"] == original for captured in (created, second): result = json.loads(command("request", "incorporate", captured["request"]["id"], "--work", "WRK-0001", "--by", "destination-agent", "--format", "json")) assert result["reused"] is False assert not Path(captured["path"]).exists() repeated = json.loads(command("request", "incorporate", identifier, "--work", "WRK-0001", "--by", "destination-agent", "--format", "json")) assert repeated["reused"] is True context = json.loads(command("work", "context", "WRK-0001", "--format", "json")) assert len(context["discussionHistory"]) == 2 notes = {note["discussionId"]: note for note in context["discussionHistory"]} assert notes["request:" + identifier]["text"].endswith(original) assert notes["request:" + identifier]["author"] == "destination-agent" assert json.loads(command("request", "list", "--format", "json")) == [] assert json.loads(command("work", "validate", "WRK-0001", "--format", "json"))["valid"] is True assert not (source / ".ai-engineering/.local/server").exists() print(json.dumps({"requests": 2, "contextEntries": 2, "offlineDatabaseUnchanged": True, "retryReused": True, "validation": "PASS"})) finally: for project in (destination, source): if (project / ".ai-engineering/.local/server/endpoint.json").exists(): run(project, "stop", "--timeout-seconds", "10") if __name__ == "__main__": main()