feat: persist and reconcile owned native sessions

This commit is contained in:
阿白叔 committed 2026-11-06 15:42:29 +08:00
1 parent 7bd7cd3966
commit 97a3de20c9
2 files changed
+457

No files matched your search

+145
View File
@@ -0,0 +1,145 @@
import json
import os
from pathlib import Path
import subprocess
import sys
import pytest
from cbb_refresh_verify import session_record as sessions
def record(tmp_path, processes):
path = tmp_path/'session.json'
sessions.write_session_record(path, dict(operation_id='test-session', operation='verify',
library=str(tmp_path/'lib.lmc'), block='A', processes=processes))
return path
@pytest.fixture
def child():
process = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(30)'])
try:
yield process
finally:
process.terminate()
process.wait(5)
def test_round_trip_and_exact_live_process(tmp_path, child):
identity = sessions.process_identity(child.pid)
path = record(tmp_path, [dict(role='worker', **identity)])
value = sessions.read_session_record(path, evidence_root=tmp_path)
assert value['protocol'] == 'cbb-runtime-session'
assert value['version'] == 1
result = sessions.reconcile_session_record(path, evidence_root=tmp_path)
assert result['status'] == 'ACTIVE'
assert result['release_proof'] is None
assert child.poll() is None
@pytest.mark.parametrize('key,value', [('created_handle', 'old'), ('image', 'C:/old.exe'),
('owner_sid', 'S-1-old'), ('session_id', -1)])
def test_reused_pid_is_observed_never_acted_on(tmp_path, child, key, value):
identity = sessions.process_identity(child.pid)
identity[key] = value
path = record(tmp_path, [dict(role='worker', **identity)])
result = sessions.reconcile_session_record(path, evidence_root=tmp_path)
assert result['status'] == 'IDENTITY_CHANGED'
assert result['release_proof'] is None
assert child.poll() is None
@pytest.mark.parametrize('kind', ['outside', 'large', 'invalid', 'unknown_protocol'])
def test_untrusted_record_is_unconfirmed_not_an_occupancy_fact(tmp_path, kind):
owned = tmp_path/'owned'
owned.mkdir()
path = (tmp_path if kind == 'outside' else owned)/'session.json'
path.write_text('x'*65537 if kind == 'large' else '{' if kind == 'invalid' else '{}')
result = sessions.reconcile_session_record(path, evidence_root=owned)
assert result['status'] == 'UNCONFIRMED'
assert result['release_proof'] is None
def test_exited_process_alone_is_not_release_proof(tmp_path, child, monkeypatch):
path = record(tmp_path, [dict(role='worker', **sessions.process_identity(child.pid))])
child.terminate()
child.wait(5)
def busy(*a, **kw):
raise OSError('database still occupied')
monkeypatch.setattr('cbb_refresh_verify.library_lock.release_retained_library', busy)
result = sessions.reconcile_session_record(path, evidence_root=tmp_path)
assert result['status'] == 'UNCONFIRMED'
assert result['release_proof'] is None
assert 'occupied' in result['detail']
def test_release_uses_independent_proof_and_does_not_replay(tmp_path, child, monkeypatch):
path = record(tmp_path, [dict(role='worker', **sessions.process_identity(child.pid))])
child.terminate()
child.wait(5)
proof = {'status': 'RELEASE_CONFIRMED', 'library': str(tmp_path/'lib.lmc')}
monkeypatch.setattr('cbb_refresh_verify.library_lock.release_retained_library', lambda *a, **kw: proof)
result = sessions.reconcile_session_record(path, evidence_root=tmp_path)
assert result['status'] == 'RELEASED'
assert result['release_proof'] == proof
def test_observation_can_stop_without_claiming_native_release(tmp_path, child):
import threading
path = record(tmp_path, [dict(role='worker', **sessions.process_identity(child.pid))])
stop = threading.Event()
stop.set()
result = sessions.observe_retained_session(path, evidence_root=tmp_path, stop=stop)
assert result['status'] == 'ACTIVE'
assert result['release_proof'] is None
assert child.poll() is None
def test_worker_state_cannot_replace_another_operation(tmp_path, child):
path = record(tmp_path, [dict(role='worker', **sessions.process_identity(child.pid))])
sessions.write_session_record(tmp_path/'worker-state.json', {
'operation_id': 'different', 'processes': [], 'phase': 'FINISHED'})
result = sessions.reconcile_session_record(path, evidence_root=tmp_path)
assert result['status'] == 'UNCONFIRMED'
assert child.poll() is None
def test_retained_session_yields_global_queue_but_keeps_same_library_waiting(tmp_path, child):
import threading
from cbb_refresh_verify.operation_queue import NativeQueue
from cbb_refresh_verify.session_record import handoff_session, library_available
library = tmp_path/'lib.lmc'
path = record(tmp_path, [dict(role='worker', **sessions.process_identity(child.pid))])
queue = NativeQueue()
observation_stop = threading.Event()
queue.run(lambda: handoff_session(path, evidence_root=tmp_path, stop=observation_stop))
assert library_available(library) is False
assert library_available(tmp_path/'other.lmc') is True
assert queue.run(lambda: 'other-completed', ready=lambda: library_available(tmp_path/'other.lmc')) == 'other-completed'
stop = threading.Event()
stop.set()
from cbb_refresh_verify.operation_queue import QueueCancelled
with pytest.raises(QueueCancelled):
queue.run(lambda: pytest.fail('overlapping writer'), stop=stop,
ready=lambda: library_available(library))
observation_stop.set()
def test_bad_journal_does_not_drop_in_memory_supervision(tmp_path, child):
import threading
import time
from types import SimpleNamespace
path = tmp_path/'session.json'
path.write_text('{')
stop = threading.Event()
process = SimpleNamespace(poll=child.poll, close=lambda: None, cbb_library=tmp_path/'lib.lmc')
sessions.handoff_session(path, evidence_root=tmp_path, process=process, stop=stop)
key = str(path.absolute()).casefold()
try:
time.sleep(.1)
with sessions._observer_guard:
assert key in sessions._observers, 'active owned handle lost its observer because JSON was unreadable'
assert child.poll() is None
finally:
stop.set()