fix: isolate ICDB checks and recover scoped native sessions

This commit is contained in:
阿白叔 committed 2026-11-05 21:52:38 +08:00
1 parent 1acfbd6374
commit 1bd7589fef
82 files changed
+7770 -1583

No files matched your search

+74 -1
View File
@@ -412,6 +412,79 @@ def test_agent_start_failure_releases_listener(tmp_path):
DialogAgentClient.start(FailedWorker(), tmp_path)
def _slow_start_client(tmp_path, monkeypatch, *, delay, stop=None):
import socket
from types import SimpleNamespace
from cbb_refresh_verify import dialog_agent as module
client = DialogAgentClient.prepare(tmp_path, desktop_name='test')
client.listener.close()
elapsed = [0.0]
class Listener:
closed = False
def settimeout(self, timeout): self.timeout = timeout
def accept(self):
if elapsed[0] < delay:
elapsed[0] += self.timeout
if stop is not None and elapsed[0] >= .4: stop.set()
raise socket.timeout()
return SimpleNamespace(setsockopt=lambda *a: None), None
def close(self): self.closed = True
class Channel:
def __init__(self, connection): pass
def receive(self, timeout): return {'token': client.paths.token}
def send(self, *a): pass
def close(self): pass
client.listener = Listener()
monkeypatch.setattr(module, 'JsonChannel', Channel)
monkeypatch.setattr(module.time, 'monotonic', lambda: elapsed[0])
monkeypatch.setattr(DialogAgentClient, 'prepare', classmethod(lambda cls, *a, **k: client))
worker = SimpleNamespace(name='test', start_companion=lambda command: None)
return client, worker, elapsed
def test_frozen_agent_can_initialize_after_the_old_ten_second_deadline(tmp_path, monkeypatch):
client, worker, elapsed = _slow_start_client(tmp_path, monkeypatch, delay=12)
assert DialogAgentClient.start(worker, tmp_path) is client
assert elapsed[0] >= 12 and client.channel is not None
assert client.timeout_seconds == 2 # Normal IPC does not inherit the startup budget.
def test_agent_startup_wait_responds_to_cancel_without_starting_pcb(tmp_path, monkeypatch):
import pytest
from threading import Event
stop = Event()
client, worker, elapsed = _slow_start_client(tmp_path, monkeypatch, delay=100, stop=stop)
with pytest.raises(InterruptedError, match='VERIFY_CANCELLED'):
DialogAgentClient.start(worker, tmp_path, stop=stop)
assert elapsed[0] < 1 and client.listener.closed
def test_agent_startup_still_has_an_absolute_deadline(tmp_path, monkeypatch):
import pytest
client, worker, elapsed = _slow_start_client(tmp_path, monkeypatch, delay=100)
with pytest.raises(TimeoutError):
DialogAgentClient.start(worker, tmp_path)
assert 29 <= elapsed[0] <= 31 and client.listener.closed
def test_cancellation_during_authenticated_handshake_never_releases_startup(tmp_path, monkeypatch):
import pytest
from threading import Event
from cbb_refresh_verify import dialog_agent as module
stop = Event()
client, worker, _ = _slow_start_client(tmp_path, monkeypatch, delay=0)
class Channel:
def __init__(self, connection): pass
def receive(self, timeout):
stop.set()
return {'token': client.paths.token}
def close(self): pass
monkeypatch.setattr(module, 'JsonChannel', Channel)
with pytest.raises(InterruptedError, match='VERIFY_CANCELLED'):
DialogAgentClient.start(worker, tmp_path, stop=stop)
assert client.channel is None and client.listener.closed
def test_memory_channel_timeout_is_bounded_and_partial_packets_survive():
import socket
import pytest
@@ -436,7 +509,7 @@ def test_loopback_connection_rejects_wrong_agent_token(tmp_path):
import socket
import pytest
from cbb_refresh_verify.dialog_agent import JsonChannel, DialogAgentProtocolError
client = DialogAgentClient.prepare(tmp_path, desktop_name='test', timeout_seconds=.3)
client = DialogAgentClient.prepare(tmp_path, desktop_name='test', timeout_seconds=2)
wrong = JsonChannel(socket.create_connection(('127.0.0.1', client.paths.port), timeout=1))
try:
wrong.send({'token': 'not-the-agent'})