From 36f7d838125dfb7db0575b291d7679ba277a1f66 Mon Sep 17 00:00:00 2001 From: Devin Phillip Davis <354534380+quantum-osiris@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:18:55 -0500 Subject: [PATCH 1/4] F-34: run Qiskit/Aer on one long-lived thread with a bounded backlog qiskit 2.6.2 / qiskit-aer 0.18.2 / Python 3.14: a second simulation started from a new thread segfaults the process in qiskit._accelerate (ThreadingHTTPServer opens one thread per request). compile, slice/run, organism/run, files/run and dnalang/info, plus the compile step of assist, now run on osiris_cli.qiskit_worker: one thread, at most OSIRIS_QISKIT_MAX_PENDING (default 4) admitted calls, 503 SIMULATOR_BUSY beyond that, admitted runs never cancelled (their receipts are written), shutdown waits for them. tests/test_qiskit_worker.py: unit tests plus live runs through the real server and Aer: sequential, concurrent with per-request result/receipt association, overload, failure then success, client timeout then next request, subprocess restart with one valid chain. The restart test fails on fe35c915 (service drops the connection) and passes here. Co-Authored-By: Claude Opus 5.5 --- osiris_cli/ide_api.py | 38 +++++- osiris_cli/qiskit_worker.py | 78 ++++++++++++ tests/test_qiskit_worker.py | 240 ++++++++++++++++++++++++++++++++++++ 3 files changed, 354 insertions(+), 2 deletions(-) create mode 100644 osiris_cli/qiskit_worker.py create mode 100644 tests/test_qiskit_worker.py diff --git a/osiris_cli/ide_api.py b/osiris_cli/ide_api.py index 08693bb0..b0755acb 100644 --- a/osiris_cli/ide_api.py +++ b/osiris_cli/ide_api.py @@ -460,7 +460,9 @@ def files_runs(self, body: Dict[str, Any]) -> Dict[str, Any]: def assist(self, body: Dict[str, Any]) -> Dict[str, Any]: from osiris_cli import editor_assist try: - return editor_assist.assist(body, route_fn=self._assist_route_fn) + from osiris_cli.vertical_slice import compile_source + return editor_assist.assist(body, route_fn=self._assist_route_fn, + compile_fn=lambda src: on_qiskit_thread(lambda: compile_source(src))) except editor_assist.AssistError as e: raise ApiError(400, "INVALID_INPUT", str(e)) @@ -510,6 +512,35 @@ def experiments(self) -> Dict[str, Any]: } +# F-34: every route that reaches Qiskit/Aer runs on one long-lived thread with a bounded backlog +# (osiris_cli/qiskit_worker.py explains the crash and the policy). +# assist is not listed: only its compile step runs on the worker (Backend.assist), so a slow model +# cannot hold up simulations. dnalang_info is listed because it imports qiskit/qiskit_aer. +QISKIT_OPERATIONS = frozenset({"compile", "run_slice", "organism", "files_run", "dnalang_info"}) +QISKIT_WORKER = None + + +_WORKER_LOCK = threading.Lock() + + +def qiskit_worker(): + global QISKIT_WORKER + with _WORKER_LOCK: + if QISKIT_WORKER is None: + from osiris_cli.qiskit_worker import QiskitWorker + QISKIT_WORKER = QiskitWorker() + return QISKIT_WORKER + + +def on_qiskit_thread(call): + """Run `call` on the shared Qiskit thread; an overfull backlog is a 503, never an unbounded wait.""" + from osiris_cli.qiskit_worker import Busy + try: + return qiskit_worker().submit(call) + except Busy as e: + raise ApiError(503, "SIMULATOR_BUSY", str(e)) + + def make_handler(backend: Backend, token: str, console_dir: Optional[str], allowed_hosts: Tuple[str, ...]): class Handler(BaseHTTPRequestHandler): server_version = "osiris-ide-api/" + API_VERSION @@ -573,7 +604,8 @@ def _dispatch(self, method: str): if not isinstance(body, dict): raise ApiError(400, "BAD_JSON", "body must be a JSON object") fn = getattr(backend, name) - result = fn(body) if method == "POST" else fn() + call = (lambda: fn(body)) if method == "POST" else fn + result = on_qiskit_thread(call) if name in QISKIT_OPERATIONS else call() self._json(200, {"api_version": API_VERSION, **result}) except ApiError as e: self._error(e) @@ -669,6 +701,8 @@ def main(argv=None) -> int: pass finally: httpd.server_close() + if QISKIT_WORKER is not None: # admitted runs finish and write their receipts + QISKIT_WORKER.shutdown() return 0 diff --git a/osiris_cli/qiskit_worker.py b/osiris_cli/qiskit_worker.py new file mode 100644 index 00000000..c4c91257 --- /dev/null +++ b/osiris_cli/qiskit_worker.py @@ -0,0 +1,78 @@ +"""One long-lived thread for all Qiskit / Aer work in the IDE API, with a bounded backlog (F-34). + +Why: with qiskit 2.6.2 and qiskit-aer 0.18.2 on Python 3.14, a second simulation started from a +*different* short-lived thread segfaults the whole process inside ``qiskit._accelerate`` +(``get_standard_gate_name_mapping``, reached from ``AerSimulator.target`` during transpile). +``ThreadingHTTPServer`` opens one thread per request, so any two run requests could stop the +service. Running every Qiskit call on one persistent thread avoids it (reproduced and checked in +tests/test_qiskit_worker.py). + +Policy: +* Calls run one at a time, in arrival order, on the thread named ``osiris-qiskit-0``. +* At most ``max_pending`` calls may be admitted (running + waiting). Beyond that, ``submit`` raises + ``Busy`` at once; the API answers 503 ``SIMULATOR_BUSY``. Nothing queues without bound. +* A call is never cancelled once admitted: Aer cannot be interrupted safely, and a run that was + admitted must still write its receipt. If the client gives up, the work finishes, its slot is + released on completion, and the result is discarded with the request. +* Exceptions raised by the call (including ``ApiError``) are re-raised in the request thread, and + the worker stays usable. +* ``shutdown`` waits for admitted work, so receipts of admitted runs are written before exit. +""" +from __future__ import annotations + +import os +import threading +from concurrent.futures import ThreadPoolExecutor +from typing import Any, Callable, Optional + +DEFAULT_MAX_PENDING = 4 + + +class Busy(Exception): + def __init__(self, max_pending: int): + super().__init__(f"{max_pending} simulator requests are already admitted; retry shortly") + self.max_pending = max_pending + + +class QiskitWorker: + def __init__(self, max_pending: Optional[int] = None): + if max_pending is None: + max_pending = int(os.environ.get("OSIRIS_QISKIT_MAX_PENDING", DEFAULT_MAX_PENDING)) + if max_pending < 1: + raise ValueError("max_pending must be at least 1") + self.max_pending = max_pending + self._slots = threading.BoundedSemaphore(max_pending) + self._pool = ThreadPoolExecutor(max_workers=1, thread_name_prefix="osiris-qiskit") + self._lock = threading.Lock() + self._admitted = 0 + self._completed = 0 + self._rejected = 0 + + def submit(self, fn: Callable[[], Any]) -> Any: + """Run ``fn`` on the worker thread and return its result (or raise its exception).""" + if not self._slots.acquire(blocking=False): + with self._lock: + self._rejected += 1 + raise Busy(self.max_pending) + with self._lock: + self._admitted += 1 + try: + future = self._pool.submit(fn) + except BaseException: + self._release() + raise + future.add_done_callback(lambda _f: self._release()) + return future.result() + + def _release(self) -> None: + with self._lock: + self._completed += 1 + self._slots.release() + + def stats(self) -> dict: + with self._lock: + return {"max_pending": self.max_pending, "admitted": self._admitted, "completed": self._completed, + "in_flight": self._admitted - self._completed, "rejected_busy": self._rejected} + + def shutdown(self) -> None: + self._pool.shutdown(wait=True) diff --git a/tests/test_qiskit_worker.py b/tests/test_qiskit_worker.py new file mode 100644 index 00000000..a99e868a --- /dev/null +++ b/tests/test_qiskit_worker.py @@ -0,0 +1,240 @@ +"""F-34: Qiskit/Aer work runs on one long-lived thread with a bounded backlog. + +UNIT tests exercise osiris_cli.qiskit_worker directly. LIVE LOCAL tests start the real IDE API on a +loopback port and run the real dnalang compiler and Aer simulator through /api/v1/slice/run; one +test also runs the service as a subprocess and restarts it. No store, no network, no hardware. +""" +import http.client +import json +import os +import socket +import subprocess +import sys +import threading +import time + +import pytest + +pytest.importorskip("dnalang") +pytest.importorskip("qiskit_aer") +pytest.importorskip("osiris_governance.github_policy") + +from osiris_cli import ide_api # noqa: E402 +from osiris_cli.qiskit_worker import Busy, QiskitWorker # noqa: E402 +from osiris_cli.trio_authz import Receipts # noqa: E402 +from osiris_cli.vertical_slice import run_slice # noqa: E402 + +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +BELL = open(os.path.join(ROOT, "docs", "dnalang", "templates", "bell.dna")).read() +BROKEN = "organism Broken { genome { nothing_here() @ [0]; } }" + + +# ───────────────────────── UNIT ───────────────────────── +def test_successive_callers_share_one_thread(): + w = QiskitWorker(2) + seen = [] + for _ in range(4): + t = threading.Thread(target=lambda: seen.append(w.submit(threading.get_ident))) + t.start(); t.join() + assert len(set(seen)) == 1 and threading.get_ident() not in seen + w.shutdown() + + +def test_exception_propagates_and_worker_survives(): + w = QiskitWorker(1) + with pytest.raises(ZeroDivisionError): + w.submit(lambda: 1 / 0) + assert w.submit(lambda: 42) == 42 + assert w.stats()["in_flight"] == 0 + w.shutdown() + + +def test_backlog_is_bounded_and_slots_return(): + w = QiskitWorker(2) + gate = threading.Event() + out = [] + callers = [threading.Thread(target=lambda: out.append(w.submit(lambda: gate.wait(5) and "ran"))) for _ in range(2)] + for c in callers: + c.start() + deadline = time.time() + 5 + while w.stats()["in_flight"] < 2 and time.time() < deadline: + time.sleep(0.01) + with pytest.raises(Busy): + w.submit(lambda: "third") # refused at once, nothing queued + gate.set() + for c in callers: + c.join() + assert out == ["ran", "ran"] and w.stats()["rejected_busy"] == 1 + assert w.submit(lambda: "after") == "after" + w.shutdown() + + +def test_abandoned_call_still_completes_and_frees_its_slot(): + w = QiskitWorker(1) + done = threading.Event() + def slow(): + time.sleep(0.3); done.set(); return "late" + t = threading.Thread(target=lambda: w.submit(slow), daemon=True) + t.start() # this caller "gives up": nobody reads its result + time.sleep(0.05) + with pytest.raises(Busy): + w.submit(lambda: "x") + assert done.wait(2) + time.sleep(0.05) + assert w.submit(lambda: "next") == "next" + w.shutdown() + + +def test_shutdown_waits_for_admitted_work(): + w = QiskitWorker(2) + finished = [] + t = threading.Thread(target=lambda: w.submit(lambda: (time.sleep(0.2), finished.append(1)))) + t.start(); time.sleep(0.05) + w.shutdown() + assert finished == [1] + + +def test_every_qiskit_route_is_routed_through_the_worker(): + names = set(ide_api.ROUTES.values()) + assert set(ide_api.QISKIT_OPERATIONS) <= names + assert {"compile", "run_slice", "organism", "files_run", "dnalang_info"} <= ide_api.QISKIT_OPERATIONS + src = open(ide_api.__file__).read() + assert "compile_fn=lambda src: on_qiskit_thread(" in src # assist compiles proposals on the worker + + +# ───────────────────────── LIVE LOCAL: real server, real Aer ───────────────────────── +@pytest.fixture() +def api(tmp_path, monkeypatch): + def start(max_pending=4, runner=None): + monkeypatch.setattr(ide_api, "QISKIT_WORKER", QiskitWorker(max_pending)) + backend = ide_api.Backend(gateway_factory=lambda: None, slice_receipts=str(tmp_path / "slice.jsonl"), + examples_dir=str(tmp_path), slice_runner=runner) + httpd, token = ide_api.serve(backend, port=0, console_dir=None, token_file=str(tmp_path / "tok")) + threading.Thread(target=httpd.serve_forever, daemon=True).start() + started.append(httpd) + return {"port": httpd.server_address[1], "token": token, "receipts": str(tmp_path / "slice.jsonl")} + started = [] + yield start + for h in started: + h.shutdown(); h.server_close() + + +def post(s, path, body, timeout=120): + c = http.client.HTTPConnection("127.0.0.1", s["port"], timeout=timeout) + c.request("POST", path, body=json.dumps(body), headers={"Host": f"127.0.0.1:{s['port']}", "X-OSIRIS-Token": s["token"], + "Content-Type": "application/json"}) + r = c.getresponse(); data = json.loads(r.read()); c.close() + return r.status, data + + +def receipts_by_run(path): + out = {} + with open(path) as fh: + for line in fh: + if line.strip(): + e = json.loads(line); out[e["payload"]["run_id"]] = e + return out + + +def test_sequential_simulations_from_different_request_threads(api): + s = api() + runs = [post(s, "/api/v1/slice/run", {"source": BELL, "shots": 100 + i, "seed": i}) for i in range(4)] + assert all(st == 200 and r["status"] == "COMPLETED" for st, r in runs) + rec = receipts_by_run(s["receipts"]) + for i, (_, r) in enumerate(runs): + assert sum(r["simulation"]["counts"].values()) == 100 + i + assert rec[r["run_id"]]["payload"]["shots"] == 100 + i and rec[r["run_id"]]["hash"] == r["receipt"]["hash"] + assert Receipts.verify(s["receipts"]) is True + + +def test_concurrent_clients_each_get_their_own_result(api): + s = api(max_pending=8) + results = {} + def client(i): + results[i] = post(s, "/api/v1/slice/run", {"source": BELL, "shots": 200 + i, "seed": 10 + i}) + ts = [threading.Thread(target=client, args=(i,)) for i in range(6)] + for t in ts: t.start() + for t in ts: t.join() + rec = receipts_by_run(s["receipts"]) + assert len(rec) == 6 and Receipts.verify(s["receipts"]) is True + for i, (st, r) in results.items(): + assert st == 200 and r["status"] == "COMPLETED" + assert sum(r["simulation"]["counts"].values()) == 200 + i and r["requested"]["seed"] == 10 + i + assert rec[r["run_id"]]["payload"]["shots"] == 200 + i and rec[r["run_id"]]["payload"]["seed"] == 10 + i + + +def test_overload_is_refused_not_queued(api): + def slow(*a, **k): + time.sleep(0.4); return run_slice(*a, **k) + s = api(max_pending=1, runner=slow) + results = [] + ts = [threading.Thread(target=lambda: results.append(post(s, "/api/v1/slice/run", {"source": BELL, "shots": 50}))) for _ in range(4)] + for t in ts: t.start() + for t in ts: t.join() + codes = sorted(st for st, _ in results) + busy = [r for st, r in results if st == 503] + assert codes.count(200) >= 1 and busy and all(r["error"]["kind"] == "SIMULATOR_BUSY" for r in busy) + assert len(receipts_by_run(s["receipts"])) == codes.count(200) + assert post(s, "/api/v1/slice/run", {"source": BELL, "shots": 50})[0] == 200 + + +def test_failed_simulation_then_successful_one(api): + calls = {"n": 0} + def flaky(*a, **k): + calls["n"] += 1 + if calls["n"] == 2: + raise RuntimeError("simulated backend fault") + return run_slice(*a, **k) + s = api(runner=flaky) + st, rejected = post(s, "/api/v1/slice/run", {"source": BROKEN}) + assert st == 200 and rejected["status"] == "REJECTED" and "receipt" not in rejected + st, err = post(s, "/api/v1/slice/run", {"source": BELL, "shots": 64}) + assert st == 500 and err["error"]["kind"] == "INTERNAL" + st, ok = post(s, "/api/v1/slice/run", {"source": BELL, "shots": 64}) + assert st == 200 and ok["status"] == "COMPLETED" + assert list(receipts_by_run(s["receipts"])) == [ok["run_id"]] + + +def test_client_timeout_then_next_request(api): + def slow(*a, **k): + time.sleep(0.8); return run_slice(*a, **k) + s = api(max_pending=2, runner=slow) + with pytest.raises((socket.timeout, TimeoutError)): + post(s, "/api/v1/slice/run", {"source": BELL, "shots": 32}, timeout=0.2) + st, r = post(s, "/api/v1/slice/run", {"source": BELL, "shots": 33}) + assert st == 200 and r["status"] == "COMPLETED" + rec = receipts_by_run(s["receipts"]) + # the abandoned run was admitted, so it finished and was receipted; its result went nowhere + assert sorted(e["payload"]["shots"] for e in rec.values()) == [32, 33] and Receipts.verify(s["receipts"]) is True + + +def _free_port(): + with socket.socket() as so: + so.bind(("127.0.0.1", 0)); return so.getsockname()[1] + + +def test_service_restart_keeps_one_valid_receipt_chain(tmp_path): + receipts, tok = str(tmp_path / "slice.jsonl"), str(tmp_path / "tok") + env = dict(os.environ, PYTHONPATH=ROOT + os.pathsep + os.environ.get("PYTHONPATH", "")) + run_ids = [] + for round_ in range(2): + port = _free_port() + p = subprocess.Popen([sys.executable, "-m", "osiris_cli.ide_api", "--port", str(port), "--slice-receipts", receipts, + "--github-receipts", str(tmp_path / "gh.jsonl"), "--token-file", tok, "--examples-dir", str(tmp_path)], + cwd=str(tmp_path), env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) + try: + for _ in range(200): + try: + socket.create_connection(("127.0.0.1", port), timeout=0.2).close(); break + except OSError: + time.sleep(0.1) + s = {"port": port, "token": open(tok).read().strip()} + for i in range(2): # two runs per process: the F-34 crash needed exactly this + st, r = post(s, "/api/v1/slice/run", {"source": BELL, "shots": 40 + round_ * 10 + i}) + assert st == 200 and r["status"] == "COMPLETED", r + run_ids.append(r["run_id"]) + assert p.poll() is None, "service exited during the runs" + finally: + p.send_signal(2) # SIGINT: the KeyboardInterrupt path shuts the worker down + p.wait(30) + assert list(receipts_by_run(receipts)) == run_ids and Receipts.verify(receipts) is True From 66e0643fe69fe825cce9283fbce2710d0628f890 Mon Sep 17 00:00:00 2001 From: Devin Phillip Davis <354534380+quantum-osiris@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:24:26 -0500 Subject: [PATCH 2/4] IDE API 1.5.0: files/rename; GHZ correctness fixtures - POST /api/v1/files/rename: revision-guarded path change (same owner policy and trigger as save; id, project and content unchanged, so receipts still resolve). Stale base revision is CONFLICT, a taken path is EDITOR_PATH_EXISTS, another account gets 404. No migration change. - tests/fixtures/aura_ghz_proposal_2026-11-16.json: the GHZ change AURA (gemma3:4b) proposed in the notebook acceptance run, recovered verbatim from the stored revision 3, with its receipt. It compiles and runs and is not GHZ (H on all three qubits -> uniform). tests/test_ghz_fixtures.py checks an ideal GHZ fixture against the {000,111} support with an explicit 5-sigma binomial bound and checks that the recorded proposal fails it. Fixture-specific; not a product check. Co-Authored-By: Claude Opus 5.5 --- docs/api/osiris-ide-api-v1.json | 5 +- osiris_cli/editor_store.py | 24 +++++++ osiris_cli/ide_api.py | 7 ++- .../aura_ghz_proposal_2026-11-16.json | 29 +++++++++ tests/fixtures/ghz3_ideal.dna | 13 ++++ tests/test_editor_api.py | 28 +++++++++ tests/test_ghz_fixtures.py | 63 +++++++++++++++++++ 7 files changed, 166 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/aura_ghz_proposal_2026-11-16.json create mode 100644 tests/fixtures/ghz3_ideal.dna create mode 100644 tests/test_ghz_fixtures.py diff --git a/docs/api/osiris-ide-api-v1.json b/docs/api/osiris-ide-api-v1.json index 07fdeda1..784ad76a 100644 --- a/docs/api/osiris-ide-api-v1.json +++ b/docs/api/osiris-ide-api-v1.json @@ -1,6 +1,6 @@ { "contract": "osiris-ide-api", - "api_version": "1.4.0", + "api_version": "1.5.0", "compatibility": "Clients must check api_version from GET /api/v1/meta and refuse a different MAJOR version. MINOR versions only add optional fields.", "transport": { "bind": "127.0.0.1 or ::1 only", @@ -22,7 +22,7 @@ "routes": { "GET /api/v1/meta": { "response": {"type": "object", "required": ["api_version", "service", "github_writes_via_api", "evidence_labels"], - "properties": {"api_version": {"const": "1.4.0"}, "service": {"const": "osiris-ide-api"}, + "properties": {"api_version": {"const": "1.5.0"}, "service": {"const": "osiris-ide-api"}, "contract_sha256": {"type": ["string", "null"]}, "github_writes_via_api": {"const": false}, "evidence_labels": {"type": "array", "items": {"type": "string"}}}} }, @@ -118,6 +118,7 @@ "POST /api/v1/files/save": {"request": {"type": "object", "required": ["user_token", "file_id", "base_revision", "content"], "properties": {"base_revision": {"type": "integer"}}}, "response": {"type": "object", "required": ["status"], "properties": {"status": {"enum": ["SAVED", "CONFLICT"]}, "file": {"type": "object", "required": ["id", "project_id", "path", "revision", "content_sha256"], "properties": {"revision": {"type": "integer"}, "content_sha256": {"type": "string", "pattern": "^[0-9a-f]{64}$"}}}, "current": {"type": "object", "required": ["id", "project_id", "path", "revision", "content_sha256"], "properties": {"revision": {"type": "integer"}, "content_sha256": {"type": "string", "pattern": "^[0-9a-f]{64}$"}}}}}}, "POST /api/v1/files/delete": {"request": {"type": "object", "required": ["user_token", "file_id"]}, "response": {"type": "object", "required": ["deleted"]}}, "POST /api/v1/files/run": {"request": {"type": "object", "required": ["user_token", "file_id", "revision"], "properties": {"revision": {"type": "integer"}, "shots": {"type": "integer"}, "seed": {"type": "integer"}}}, "response": {"type": "object", "required": ["status", "executed"], "properties": {"status": {"enum": ["COMPLETED", "REJECTED", "FAILED", "REVISION_MISMATCH"]}, "executed": {"type": "boolean"}, "evidence_class": {"const": "SIMULATED"}, "hardware": {"const": false}, "source_binding": {"type": ["object", "null"], "required": ["project_id", "file_id", "path", "revision", "content_sha256"]}}}}, + "POST /api/v1/files/rename": {"request": {"type": "object", "required": ["user_token", "file_id", "base_revision", "path"], "properties": {"base_revision": {"type": "integer"}, "path": {"type": "string"}}}, "response": {"type": "object", "required": ["status"], "properties": {"status": {"enum": ["RENAMED", "CONFLICT"]}, "file": {"type": "object"}, "current": {"type": "object"}}}}, "POST /api/v1/notebook/run": {"request": {"type": "object", "required": ["user_token", "file_id", "revision", "cell_id"], "properties": {"revision": {"type": "integer"}, "cell_id": {"type": "string"}, "shots": {"type": "integer"}, "seed": {"type": "integer"}}}, "response": {"type": "object", "required": ["status", "executed"], "properties": {"status": {"enum": ["COMPLETED", "REJECTED", "FAILED", "REVISION_MISMATCH"]}, "executed": {"type": "boolean"}, "evidence_class": {"const": "SIMULATED"}, "hardware": {"const": false}, "source_binding": {"type": ["object", "null"], "required": ["project_id", "file_id", "path", "revision", "content_sha256", "notebook_format", "cell_id", "cell_source_sha256"]}}}}, "POST /api/v1/files/runs": {"request": {"type": "object", "required": ["user_token", "file_id"]}, "response": {"type": "object", "required": ["file_id", "chain_valid", "runs"], "properties": {"runs": {"type": "array"}}}}, "POST /api/v1/assist": {"request": {"type": "object", "required": ["action"], "properties": {"action": {"enum": ["explain_selection", "explain_diagnostic", "propose_change", "generate_example"]}, "allow_cloud": {"type": "boolean"}}}, "response": {"type": "object", "required": ["status", "action", "text", "proposed_source", "proposal_compiles", "route", "applied"], "properties": {"status": {"enum": ["ANSWERED", "UNAVAILABLE"]}, "applied": {"const": false}, "route": {"type": "object"}}}} diff --git a/osiris_cli/editor_store.py b/osiris_cli/editor_store.py index a773b646..25098389 100644 --- a/osiris_cli/editor_store.py +++ b/osiris_cli/editor_store.py @@ -247,6 +247,30 @@ def save_file(self, token: str, file_id: Any, base_revision: Any, content: Any) raise StoreError("NOT_FOUND", "no such file, or it is not yours") return {"status": "CONFLICT", "current": current[0]} + def rename_file(self, token: str, file_id: Any, base_revision: Any, path: Any) -> Dict[str, Any]: + """Change a file's path only if it is still at ``base_revision`` (the store bumps the revision; id, + project and content are unchanged, so run receipts that name the file id still find it). + Returns {"status": "RENAMED", "file": row} or {"status": "CONFLICT", "current": metadata}.""" + self._uuid(file_id) + self._check_path(path) + if isinstance(base_revision, bool) or not isinstance(base_revision, int) or base_revision < 1: + raise StoreError("INVALID_INPUT", "base_revision must be a positive integer") + try: + rows = self._rows("PATCH", f"/osiris_files?id=eq.{file_id}&revision=eq.{base_revision}" + f"&select={_FILE_COLUMNS}", token, {"path": path}) + except StoreError as e: + if e.code == "CONFLICT": + raise StoreError("PATH_EXISTS", "a file with this path already exists in the project") from None + raise + if len(rows) == 1: + if rows[0].get("path") != path or rows[0].get("revision") != base_revision + 1: + raise StoreError("BAD_RESPONSE", "the store did not confirm the new path and revision") + return {"status": "RENAMED", "file": rows[0]} + current = self._rows("GET", f"/osiris_files?select={_FILE_COLUMNS}&id=eq.{file_id}", token) + if len(current) != 1: + raise StoreError("NOT_FOUND", "no such file, or it is not yours") + return {"status": "CONFLICT", "current": current[0]} + def delete_file(self, token: str, file_id: Any) -> Dict[str, Any]: self._uuid(file_id) rows = self._rows("DELETE", f"/osiris_files?id=eq.{file_id}&select=id,path,revision", token) diff --git a/osiris_cli/ide_api.py b/osiris_cli/ide_api.py index f738534e..2ab1b183 100644 --- a/osiris_cli/ide_api.py +++ b/osiris_cli/ide_api.py @@ -31,7 +31,7 @@ from typing import Any, Callable, Dict, Optional, Tuple from urllib.parse import urlparse -API_VERSION = "1.4.0" +API_VERSION = "1.5.0" MAX_BODY = 256 * 1024 TOKEN_HEADER = "X-OSIRIS-Token" TOKEN_PLACEHOLDER = "__OSIRIS_SESSION_TOKEN__" @@ -410,6 +410,10 @@ def files_save(self, body: Dict[str, Any]) -> Dict[str, Any]: return self._editor_call(lambda s: s.save_file(body.get("user_token"), body.get("file_id"), body.get("base_revision"), body.get("content"))) + def files_rename(self, body: Dict[str, Any]) -> Dict[str, Any]: + return self._editor_call(lambda s: s.rename_file(body.get("user_token"), body.get("file_id"), + body.get("base_revision"), body.get("path"))) + def files_delete(self, body: Dict[str, Any]) -> Dict[str, Any]: return self._editor_call(lambda s: s.delete_file(body.get("user_token"), body.get("file_id"))) @@ -545,6 +549,7 @@ def experiments(self) -> Dict[str, Any]: ("POST", "/api/v1/files/create"): "files_create", ("POST", "/api/v1/files/save"): "files_save", ("POST", "/api/v1/files/delete"): "files_delete", + ("POST", "/api/v1/files/rename"): "files_rename", ("POST", "/api/v1/files/run"): "files_run", ("POST", "/api/v1/files/runs"): "files_runs", ("POST", "/api/v1/notebook/run"): "notebook_run", diff --git a/tests/fixtures/aura_ghz_proposal_2026-11-16.json b/tests/fixtures/aura_ghz_proposal_2026-11-16.json new file mode 100644 index 00000000..6a8cf5cb --- /dev/null +++ b/tests/fixtures/aura_ghz_proposal_2026-11-16.json @@ -0,0 +1,29 @@ +{ + "recovered_from": { + "path": "notebooks/bell-mwk39l4f.dnanb", + "revision": 3, + "content_sha256": "92fe80595900d4fdd374594dc38cdcb80c5c0a361980cf7c400822eb5df578e0", + "cell_id": "c5v101n5e10" + }, + "instruction": "Add a third qubit so the circuit prepares a 3-qubit GHZ state, measured on all three qubits.", + "model": "ollama gemma3:4b (local)", + "source": "organism GHZ {\n meta { author: \"Devin Phillip Davis\", purpose: \"GHZ state generation\" }\n\n gene ghz_state() on a, b, c {\n helix a;\n helix b;\n helix c;\n bond a, b;\n bond b, c;\n bond c, a;\n cleave a -> ca;\n cleave b -> cb;\n cleave c -> cc;\n }\n\n genome {\n ghz_state() @ [0, 1, 2];\n }\n}\n", + "observed_run": { + "run_id": "slice-546d6b4f4e50", + "receipt_hash": "1e5978a7259f91a5aa4c6f7da02fb815fe2588597d7d4c84bc2c4a9f39cae4b2", + "revision": 3, + "cell_source_sha256": "sha256:d5afc50f452c2de293130f13c5fecb8148f57500782e1c3fe2f495f58bb4d1a8", + "shots": 2000, + "seed": 1234, + "counts": { + "100": 250, + "101": 255, + "110": 239, + "111": 266, + "000": 243, + "001": 241, + "010": 246, + "011": 260 + } + } +} diff --git a/tests/fixtures/ghz3_ideal.dna b/tests/fixtures/ghz3_ideal.dna new file mode 100644 index 00000000..7e733615 --- /dev/null +++ b/tests/fixtures/ghz3_ideal.dna @@ -0,0 +1,13 @@ +// Ideal 3-qubit GHZ: H on q0, then CX q0->q1, CX q1->q2. Measured in the computational basis. +// Ideal outcome support is {000, 111}, each with probability 1/2. Fixture for tests only. +organism GHZ3 { + gene ghz() on a, b, c { + helix a; + bond a, b; + bond b, c; + cleave a -> ca; + cleave b -> cb; + cleave c -> cc; + } + genome { ghz() @ [0, 1, 2]; } +} diff --git a/tests/test_editor_api.py b/tests/test_editor_api.py index dc678665..1d5c8374 100644 --- a/tests/test_editor_api.py +++ b/tests/test_editor_api.py @@ -536,3 +536,31 @@ def test_notebook_format_rejects_malformed_content(): with pytest.raises(nbf.NotebookFormatError): nbf.parse(bad) assert nbf.parse(_notebook(("a", "dnalang", BELL)))["cells"][0]["id"] == "a" + + +# ───────────────────────── IDE API 1.5.0: rename ───────────────────────── +def test_rename_keeps_identity_and_receipts_and_refuses_stale_or_taken_paths(stack): + s, api = stack["store"], stack["api"] + tok = stack["sign_in"]("rita@example.test")["access_token"] + p = s.create_project(tok, "Rename") + f = s.create_file(tok, p["id"], "notebooks/a.dnanb", _notebook(("bell", "dnalang", BELL))) + s.create_file(tok, p["id"], "notebooks/taken.dnanb", _notebook()) + run = api.notebook_run({"user_token": tok, "file_id": f["id"], "revision": 1, "cell_id": "bell", "shots": 50}) + r = api.files_rename({"user_token": tok, "file_id": f["id"], "base_revision": 1, "path": "notebooks/b.dnanb"}) + check("POST /api/v1/files/rename", r) + assert r["status"] == "RENAMED" and r["file"]["id"] == f["id"] and r["file"]["revision"] == 2 + assert r["file"]["content_sha256"] == f["content_sha256"] + assert [x["receipt_hash"] for x in api.files_runs({"user_token": tok, "file_id": f["id"]})["runs"]] == [run["receipt"]["hash"]] + stale = api.files_rename({"user_token": tok, "file_id": f["id"], "base_revision": 1, "path": "notebooks/c.dnanb"}) + assert stale["status"] == "CONFLICT" and stale["current"]["path"] == "notebooks/b.dnanb" + with pytest.raises(ide_api.ApiError) as e: + api.files_rename({"user_token": tok, "file_id": f["id"], "base_revision": 2, "path": "notebooks/taken.dnanb"}) + assert e.value.kind == "EDITOR_PATH_EXISTS" and e.value.status == 409 + with pytest.raises(ide_api.ApiError) as e: + api.files_rename({"user_token": tok, "file_id": f["id"], "base_revision": 2, "path": "../escape"}) + assert e.value.status == 400 + other = stack["sign_in"]("sam@example.test")["access_token"] + with pytest.raises(ide_api.ApiError) as e: + api.files_rename({"user_token": other, "file_id": f["id"], "base_revision": 2, "path": "notebooks/mine.dnanb"}) + assert e.value.status == 404 + assert s.file(tok, f["id"])["path"] == "notebooks/b.dnanb" diff --git a/tests/test_ghz_fixtures.py b/tests/test_ghz_fixtures.py new file mode 100644 index 00000000..b5dd7086 --- /dev/null +++ b/tests/test_ghz_fixtures.py @@ -0,0 +1,63 @@ +"""Scientific-correctness fixtures, kept apart from compile and execution success. + +A circuit can compile and run and still be wrong. tests/fixtures/aura_ghz_proposal_2026-11-16.json +is the change a local model (gemma3:4b via AURA) proposed when asked for a 3-qubit GHZ state; it +compiled and executed, and its counts were uniform over all 8 outcomes. This file checks a +documented ideal GHZ fixture against the GHZ outcome support with an explicit finite-shot bound, +and checks that the recorded proposal fails that same check. The check is specific to these +fixtures. It is not a general correctness test for user circuits and the product does not use it. +""" +import json +import math +import os + +import pytest + +pytest.importorskip("dnalang") +pytest.importorskip("qiskit_aer") +pytest.importorskip("osiris_governance.github_policy") + +from osiris_cli.vertical_slice import compile_source, run_slice # noqa: E402 + +FIX = os.path.join(os.path.dirname(os.path.abspath(__file__)), "fixtures") +SHOTS = 4000 +Z = 5.0 # half-width of the binomial bound in standard deviations (two-sided p ~ 6e-7 per check) + + +def ghz_support_check(counts, shots): + """True iff counts are consistent with ideal noiseless GHZ sampling: nothing outside {000, 111} + (the simulator is noiseless, so any such count is disqualifying) and P(000) within Z sigma of 1/2.""" + total = sum(counts.values()) + outside = sum(v for k, v in counts.items() if k not in ("000", "111")) + p0 = counts.get("000", 0) / total + return total == shots and outside == 0 and abs(p0 - 0.5) <= Z * math.sqrt(0.25 / shots) + + +def test_ideal_ghz_fixture_has_ghz_support(tmp_path): + src = open(os.path.join(FIX, "ghz3_ideal.dna")).read() + r = run_slice(src, shots=SHOTS, seed=11, receipts_path=str(tmp_path / "r.jsonl")) + assert r["status"] == "COMPLETED" + assert ghz_support_check(r["simulation"]["counts"], SHOTS), r["simulation"]["counts"] + + +def test_recorded_aura_proposal_compiles_runs_and_is_not_ghz(tmp_path): + fx = json.load(open(os.path.join(FIX, "aura_ghz_proposal_2026-11-16.json"))) + comp = compile_source(fx["source"]) + assert comp["ok"] is True # compilation success … + r = run_slice(fx["source"], shots=SHOTS, seed=11, receipts_path=str(tmp_path / "r.jsonl")) + assert r["status"] == "COMPLETED" # … and execution success … + counts = r["simulation"]["counts"] + assert not ghz_support_check(counts, SHOTS) # … are not correctness + assert len(counts) == 8 # H on every qubit: all 8 outcomes + # the counts observed in the notebook session were the same kind: 8 outcomes, none dominant + obs = fx["observed_run"]["counts"] + assert len(obs) == 8 and not ghz_support_check(obs, sum(obs.values())) + + +def test_support_check_bound_depends_on_shots(): + # With 10 shots the bound is wide (sigma = 0.16): a 10/0 split is only 3.2 sigma from 1/2 and is NOT + # rejected. Few shots cannot establish the 1/2 balance; the same split at 100 shots is rejected. + assert ghz_support_check({"000": 10, "111": 0}, 10) + assert not ghz_support_check({"000": 100, "111": 0}, 100) + assert ghz_support_check({"000": 2010, "111": 1990}, 4000) + assert not ghz_support_check({"000": 1999, "111": 2000, "010": 1}, 4000) From 38c6f6bf4946b263a003bc8915297769aae82e9c Mon Sep 17 00:00:00 2001 From: Devin Phillip Davis <354534380+quantum-osiris@users.noreply.github.com> Date: Tue, 17 Nov 2026 03:08:05 -0500 Subject: [PATCH 3/4] ci: install cryptography for the edge IPC tests osiris_cli/edge_ipc_client.py (a74cf58f) imports cryptography at module level, so tests/test_edge_bridge_ipc.py fails to collect and every test job on main has failed since the 04:28Z push. cryptography is not declared in pyproject.toml; this only makes CI install it. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 30780e7e..8e84b189 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,7 +29,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install numpy scipy requests PyYAML rich networkx + pip install numpy scipy requests PyYAML rich networkx cryptography pip install pytest pytest-cov python -c 'import subprocess, sys; sys.version_info >= (3, 10) and subprocess.check_call([sys.executable, "-m", "pip", "install", "qiskit"])' From 0a2186f433a1fdb8a163ba222914b2e60cc6c0cd Mon Sep 17 00:00:00 2001 From: Devin Phillip Davis <354534380+quantum-osiris@users.noreply.github.com> Date: Tue, 17 Nov 2026 03:14:54 -0500 Subject: [PATCH 4/4] test: skip the IDE API auth-header test when osiris_governance is absent ide_api.default_backend imports github_gateway, which needs osiris_governance. CI does not install it, so this test failed on every Python version once the cryptography fix let the suite collect. The guard is inside this one test, so the file's other two tests still run on CI. This matches tests/test_ide_api.py and the other modules that importorskip the governance package. Co-Authored-By: Claude Opus 5.5 --- tests/test_contracts_and_s26_cartographer.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_contracts_and_s26_cartographer.py b/tests/test_contracts_and_s26_cartographer.py index 49748520..ede19b8e 100644 --- a/tests/test_contracts_and_s26_cartographer.py +++ b/tests/test_contracts_and_s26_cartographer.py @@ -116,6 +116,8 @@ def test_canonical_contracts_enforce_fail_closed_invariants(): def test_ide_api_accepts_x_osiris_token_x_session_token_and_bearer_auth(tmp_path): + # default_backend imports github_gateway, which needs the governance package; CI does not install it + pytest.importorskip("osiris_governance.github_policy") backend = ide_api.default_backend( examples_dir=str(tmp_path), slice_receipts=str(tmp_path / "slice.jsonl"),