Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,15 @@ jobs:
python -m pip install maturin==1.9.6
npm ci --prefix bindings/typescript
- run: cargo xtask ci compliance
- name: Verify interrupted demo state and viewer publication
timeout-minutes: 3
run: |
node bindings/typescript/node_modules/typescript/bin/tsc -p demos/cross-company-incident-response/northstar-service/tsconfig.json
node --test demos/cross-company-incident-response/northstar-service/tests/state-ownership.mjs
node bindings/typescript/node_modules/typescript/bin/tsc -p demos/cross-company-incident-response/control-room/tsconfig.json
node demos/cross-company-incident-response/control-room/build.mjs
git diff --exit-code -- demos/cross-company-incident-response/control-room/public/app.js
node --input-type=module -e 'import { viewerScopeContract } from "./demos/cross-company-incident-response/tests/viewer-scope-contract.mjs"; await viewerScopeContract();'
- name: Preserve compliance evidence
if: always() && hashFiles('target/compliance/**') != ''
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
Expand Down
6 changes: 6 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,12 @@ late-completion, cleanup failure/retry, and resource-publication behavior.
A terminal flag must not stand in for unfinished cleanup, and an error or
cancellation must not be treated as proof of non-effect.

Resource and pathname ownership must span admission, publication, actual work,
and cleanup; a stale destructor must not remove a successor's resource. A file
replacement can succeed before persistence reports failure. Continuing in the
same process after that uncertainty requires explicit invalidation or an
authoritative reload, not continued decisions from a stale memory snapshot.

Keep one semantic authority for each managed transition. Extend the existing
kernel, journal, or resource owner; do not add a parallel executor, recovery
ledger, or universal lifecycle framework. Persist necessary recovery facts
Expand Down
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions architecture/dependency-graph.json
Original file line number Diff line number Diff line change
Expand Up @@ -8797,6 +8797,18 @@
"default_features": true,
"features": []
},
{
"source": "auths-proof-exchange-https",
"source_layer": "exchange",
"target": "rcgen",
"target_layer": null,
"scope": "external",
"kind": "dev",
"target_condition": null,
"optional": false,
"default_features": true,
"features": []
},
{
"source": "auths-proof-exchange-https",
"source_layer": "exchange",
Expand All @@ -8814,6 +8826,43 @@
"rustls"
]
},
{
"source": "auths-proof-exchange-https",
"source_layer": "exchange",
"target": "rustls",
"target_layer": null,
"scope": "external",
"kind": "dev",
"target_condition": null,
"optional": false,
"default_features": false,
"features": [
"ring",
"std",
"tls12"
]
},
{
"source": "auths-proof-exchange-https",
"source_layer": "exchange",
"target": "tokio",
"target_layer": null,
"scope": "external",
"kind": "dev",
"target_condition": null,
"optional": false,
"default_features": true,
"features": [
"fs",
"io-util",
"macros",
"net",
"rt-multi-thread",
"signal",
"sync",
"time"
]
},
{
"source": "auths-proof-exchange-iroh",
"source_layer": "exchange",
Expand Down
10 changes: 10 additions & 0 deletions bindings/python-adapters/sqlite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,13 @@
transactions.
It provides durable first-use, exact-replay, and conflict decisions without
exposing Auths lifecycle or authorization semantics.

An admitted reservation retains its worker until the SQLite transaction settles.
Cancelling the caller only detaches its wait; it does not imply rollback.
`aclose()` stops new reservations and joins all admitted work. Concurrent close
callers share that drain, and cancelling one close waiter does not cancel it.
A successful close means no work from that instance can commit later. `reopen()`
creates a separate instance over the same database.
Admission validates bounded builtin fields and retains immutable copies of
commitment and value bytes before scheduling the worker. Mutating a caller's
bytearray after admission cannot change the reserved record.
72 changes: 62 additions & 10 deletions bindings/python-adapters/sqlite/python/auths_sqlite/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,27 @@
import asyncio
import sqlite3
from pathlib import Path
from typing import Literal, Union
from typing import Literal, Optional, Set, Union

from auths.adapters.reservations import ReservationRecord


_Decision = Literal["acquired", "exact-replay", "conflict"]


class SQLiteAtomicReservationStore:
"""Own admitted transactions independently of their callers.

Cancelling a reserve waiter does not certify rollback: its worker still
settles. Close stops admission and shares one drain operation; cancelling
a close waiter leaves that operation running. Closed means quiescent.
"""

def __init__(self, path: Union[str, Path]) -> None:
self._path = str(Path(path))
self._closed = False
self._state: Literal["open", "closing", "closed"] = "open"
self._workers: Set[asyncio.Task[_Decision]] = set()
self._closing: Optional[asyncio.Task[None]] = None
self._initialize()

@property
Expand All @@ -31,24 +43,64 @@ def durability(self) -> Literal["single-machine-durable"]:
async def reserve(
self, record: ReservationRecord
) -> Literal["acquired", "exact-replay", "conflict"]:
if self._closed:
if self._state != "open":
raise RuntimeError("atomic reservation store is closed")
if type(record) is not ReservationRecord:
raise ValueError("invalid atomic reservation")
key, commitment, value = record.key, record.commitment, record.value
if (
type(record) is not ReservationRecord
or not record.key
or len(record.key.encode()) > 256
or len(record.commitment) != 32
or not record.value
or len(record.value) > 262_144
type(key) is not str
or not key
or len(key) > 256
or len(key.encode()) > 256
or type(commitment) not in (bytes, bytearray)
or len(commitment) != 32
or type(value) not in (bytes, bytearray)
or not value
or len(value) > 262_144
):
raise ValueError("invalid atomic reservation")
# A frozen record can still contain caller-owned bytearrays. Validate
# exact bounded builtins before copying; no hostile conversion callback
# or caller buffer may cross the admitted worker boundary.
snapshot = ReservationRecord(key, bytes(commitment), bytes(value))
# Even an eager task factory must wait until ownership is registered
# before it can submit foreign work to the thread pool.
start: asyncio.Future[None] = asyncio.get_running_loop().create_future()
worker = asyncio.create_task(self._reserve_owned(snapshot, start))
self._workers.add(worker)
worker.add_done_callback(self._settled)
start.set_result(None)
return await asyncio.shield(worker)

async def _reserve_owned(
self, record: ReservationRecord, start: asyncio.Future[None]
) -> _Decision:
await start
return await asyncio.to_thread(self._reserve, record)

def _settled(self, worker: asyncio.Task[_Decision]) -> None:
self._workers.discard(worker)
# Consume failures even after the last reservation waiter detached.
if not worker.cancelled():
worker.exception()

async def reopen(self) -> SQLiteAtomicReservationStore:
return SQLiteAtomicReservationStore(self._path)

async def aclose(self) -> None:
self._closed = True
if self._state == "open":
self._state = "closing"
self._closing = asyncio.create_task(self._drain())
closing = self._closing
if closing is not None:
await asyncio.shield(closing)

async def _drain(self) -> None:
# Closing prevents new admission; this cohort contains every worker
# that can still mutate the database on behalf of this instance.
await asyncio.gather(*tuple(self._workers), return_exceptions=True)
self._state = "closed"

def _connect(self) -> sqlite3.Connection:
connection = sqlite3.connect(self._path, timeout=5, isolation_level=None)
Expand Down
145 changes: 145 additions & 0 deletions bindings/python-adapters/sqlite/tests/test_sqlite_store.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from __future__ import annotations

import asyncio
import gc
import sqlite3
import threading
from pathlib import Path

import pytest
Expand Down Expand Up @@ -28,3 +32,144 @@ def factory(name: str) -> SQLiteAtomicReservationStore:
)
== "conflict"
)


class CommitBarrierStore(SQLiteAtomicReservationStore):
"""Use a real transaction, held immediately before its commit."""

def __init__(self, path: Path, *, fail: bool = False) -> None:
self.entered = threading.Event()
self.release = threading.Event()
self.fail = fail
super().__init__(path)

def _connect(self) -> sqlite3.Connection:
owner = self

class Connection(sqlite3.Connection):
def execute(self, sql, parameters=()):
if sql == "COMMIT":
owner.entered.set()
if not owner.release.wait(5):
raise TimeoutError("test did not release the commit barrier")
if owner.fail:
raise sqlite3.OperationalError("injected commit failure")
return super().execute(sql, parameters)

connection = sqlite3.connect(
self._path, timeout=5, isolation_level=None, factory=Connection
)
connection.execute("PRAGMA busy_timeout = 5000")
return connection

async def wait_for_commit(self) -> None:
assert await asyncio.wait_for(asyncio.to_thread(self.entered.wait, 5), 6)


@pytest.mark.asyncio
async def test_close_joins_actual_reservation_commit(tmp_path: Path) -> None:
store = CommitBarrierStore(tmp_path / "closing.sqlite3")
record = ReservationRecord("owned", bytes([3]) * 32, b"original")
reserve = asyncio.create_task(store.reserve(record))
try:
await store.wait_for_commit()
first = asyncio.create_task(store.aclose())
second = asyncio.create_task(store.aclose())
await asyncio.sleep(0) # Run both admitted close waiters; no clock delay.
assert not first.done() and not second.done()
with pytest.raises(RuntimeError, match="closed"):
await store.reserve(record)
store.release.set()
assert await asyncio.wait_for(reserve, 6) == "acquired"
await asyncio.wait_for(asyncio.gather(first, second), 6)
with pytest.raises(RuntimeError, match="closed"):
await store.reserve(record)
reopened = await store.reopen()
try:
assert await reopened.reserve(record) == "exact-replay"
finally:
await reopened.aclose()
finally:
store.release.set()
await store.aclose()


@pytest.mark.asyncio
@pytest.mark.parametrize("fail", [False, True])
async def test_cancelled_waiters_retain_worker_and_shared_close(
tmp_path: Path, fail: bool
) -> None:
store = CommitBarrierStore(tmp_path / "detached.sqlite3", fail=fail)
record = ReservationRecord("detached", bytes([4]) * 32, b"retained")
loop = asyncio.get_running_loop()
previous_handler = loop.get_exception_handler()
unhandled = []
loop.set_exception_handler(lambda _loop, context: unhandled.append(context))
reserve = asyncio.create_task(store.reserve(record))
try:
await store.wait_for_commit()
reserve.cancel()
with pytest.raises(asyncio.CancelledError):
await reserve
cancelled_close = asyncio.create_task(store.aclose())
remaining_close = asyncio.create_task(store.aclose())
await asyncio.sleep(0)
cancelled_close.cancel()
with pytest.raises(asyncio.CancelledError):
await cancelled_close
assert not remaining_close.done()
store.release.set()
await asyncio.wait_for(remaining_close, 6)
reopened = await store.reopen()
try:
assert await reopened.reserve(record) == (
"acquired" if fail else "exact-replay"
)
finally:
await reopened.aclose()
gc.collect()
await asyncio.sleep(0)
assert unhandled == []
finally:
store.release.set()
await store.aclose()
loop.set_exception_handler(previous_handler)


@pytest.mark.asyncio
async def test_admitted_worker_retains_private_bounded_record(tmp_path: Path) -> None:
entered, release = threading.Event(), threading.Event()

class EntryBarrierStore(SQLiteAtomicReservationStore):
def _reserve(self, record):
entered.set()
if not release.wait(5):
raise TimeoutError("test did not release the worker barrier")
return super()._reserve(record)

store = EntryBarrierStore(tmp_path / "snapshot.sqlite3")
commitment = bytearray([7]) * 32
value = bytearray(b"original")
# Runtime callers can pass mutable bytes despite the frozen dataclass's
# annotations; its admission snapshot must precede foreign worker code.
reserve = asyncio.create_task(store.reserve(ReservationRecord("snapshot", commitment, value)))
try:
assert await asyncio.wait_for(asyncio.to_thread(entered.wait, 5), 6)
commitment[:] = bytes([8]) * 32
value[:] = b"rewritten"
release.set()
assert await asyncio.wait_for(reserve, 6) == "acquired"
await store.aclose()
reopened = await store.reopen()
try:
assert await reopened.reserve(
ReservationRecord("snapshot", bytes([7]) * 32, b"original")
) == "exact-replay"
assert await reopened.reserve(
ReservationRecord("snapshot", bytes(commitment), bytes(value))
) == "conflict"
finally:
await reopened.aclose()
finally:
release.set()
await store.aclose()
Loading
Loading