Skip to content
Merged
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"])'

Expand Down
5 changes: 3 additions & 2 deletions docs/api/osiris-ide-api-v1.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"}}}}
},
Expand Down Expand Up @@ -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"}}}}
Expand Down
24 changes: 24 additions & 0 deletions osiris_cli/editor_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
50 changes: 39 additions & 11 deletions osiris_cli/ide_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@
import sys
import threading
import time
from concurrent.futures import ThreadPoolExecutor
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
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__"
Expand Down Expand Up @@ -412,6 +411,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")))

Expand Down Expand Up @@ -501,7 +504,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))

Expand Down Expand Up @@ -545,20 +550,41 @@ 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",
("POST", "/api/v1/assist"): "assist",
}


# Every operation that reaches Qiskit/Aer runs on this one long-lived thread. 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
# (ThreadingHTTPServer opens one per request) segfaults the process inside qiskit._accelerate
# (get_standard_gate_name_mapping). Reproduced outside the server; one shared worker thread avoids it.
# This also serialises simulations, which the API already documents as synchronous and unqueued.
QISKIT_OPERATIONS = frozenset({"compile", "run_slice", "organism", "files_run", "notebook_run"})
_QISKIT_THREAD = ThreadPoolExecutor(max_workers=1, thread_name_prefix="osiris-qiskit")
# 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", "notebook_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, ...]):
Expand Down Expand Up @@ -638,7 +664,7 @@ def _dispatch(self, method: str):
raise ApiError(400, "BAD_JSON", "body must be a JSON object")
fn = getattr(backend, name)
call = (lambda: fn(body)) if method == "POST" else fn
result = _QISKIT_THREAD.submit(call).result() if name in QISKIT_OPERATIONS else call()
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)
Expand Down Expand Up @@ -734,6 +760,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


Expand Down
78 changes: 78 additions & 0 deletions osiris_cli/qiskit_worker.py
Original file line number Diff line number Diff line change
@@ -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)
29 changes: 29 additions & 0 deletions tests/fixtures/aura_ghz_proposal_2026-11-16.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}
13 changes: 13 additions & 0 deletions tests/fixtures/ghz3_ideal.dna
Original file line number Diff line number Diff line change
@@ -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]; }
}
2 changes: 2 additions & 0 deletions tests/test_contracts_and_s26_cartographer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
28 changes: 28 additions & 0 deletions tests/test_editor_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Loading
Loading