feat: complete native BOM and Harness execution acceptance

This commit is contained in:
阿白叔 committed 2026-11-12 19:21:55 +08:00
1 parent 4f8fbd7806
commit 65438cb7e5
234 files changed
+24402 -865

No files matched your search

+105 -7
View File
@@ -57,6 +57,47 @@ def test_cache_only_batch_does_not_create_desktop_or_cad(tmp_path):
assert not Desktop.created
def test_batch_forwards_business_failure_to_owned_cad_close(tmp_path):
from dx_productivity.trial_task_center.batch_runtime import BatchCadRuntime
runtime = BatchCadRuntime(tmp_path, 'execution', desktop_factory=Desktop)
runtime._ensure_desktop()
failure = ValueError('uncertain native write')
observed = []
runtime.cad = SimpleNamespace(__exit__=lambda *args: observed.append(args), record={})
runtime.__exit__(ValueError, failure, None)
assert observed == [(ValueError, failure, None)]
def test_failed_mentor_release_defers_desktop_until_mentor_has_exited(tmp_path):
from dx_productivity.trial_task_center.batch_runtime import BatchCadRuntime
from dx_productivity.trial_task_center.batch_resources import LIVE_RESOURCES
events = []
runtime = BatchCadRuntime(tmp_path, 'execution', desktop_factory=Desktop)
runtime._ensure_desktop()
def release_desktop():
events.append('desktop_release')
raise OSError('desktop is retained by the live Mentor process')
def failed_mentor(*args):
events.append('mentor_release')
raise OSError('Mentor process release failed')
runtime.desktop.release_desktop = release_desktop
runtime.cad = SimpleNamespace(__exit__=failed_mentor, cad=SimpleNamespace(poll=lambda: None), record={})
try:
with pytest.raises(TaskCenterError):
runtime.close()
assert events == ['mentor_release']
assert 'Mentor process release failed' in runtime.record['error']
assert runtime.record['state'] == 'blocked'
assert runtime.record['release_state'] == {'mentor': 'failed', 'desktop': 'waiting_for_mentor'}
for _ in range(3):
with pytest.raises(TaskCenterError):
runtime.close(reason='resource_recheck')
assert events == ['mentor_release']
finally:
LIVE_RESOURCES.pop(str(runtime.path), None)
def test_native_operations_share_one_desktop_but_release_each_worker(tmp_path):
from dx_productivity.trial_task_center.batch_runtime import BatchCadRuntime
with BatchCadRuntime(tmp_path, 'bom_import', desktop_factory=Desktop) as runtime:
@@ -87,7 +128,7 @@ def test_failed_final_release_blocks_next_batch_and_resource_cleanup_never_repla
guard_pending_resources(tmp_path)
assert not list(tmp_path.rglob('uncertain.flag'))
Desktop.fail_release = False
assert cleanup_pending_resources(tmp_path) == ()
assert cleanup_pending_resources(tmp_path, retry_failed=True) == ()
guard_pending_resources(tmp_path)
assert len(Desktop.created) == 1
@@ -151,7 +192,7 @@ def worker_runtime(tmp_path, monkeypatch, monitor):
return BatchCadRuntime(tmp_path / 'data', 'execution', desktop_factory=Desktop, cad_factory=Cad)
def test_same_project_reuses_cad_then_next_project_starts_a_fresh_session(tmp_path, monkeypatch):
def test_closed_projects_share_cad_until_batch_end(tmp_path, monkeypatch):
import subprocess
from dx_productivity.trial_task_center.batch_resources import project_checkpoint_released
one, _ = command_for(tmp_path)
@@ -166,12 +207,40 @@ def test_same_project_reuses_cad_then_next_project_starts_a_fresh_session(tmp_pa
assert runtime.cad is first
runtime.run_worker(two, 5)
assert project_checkpoint_released(Path(two[3]))
assert first.closed and Desktop.created[0].released
assert len(Cad.created) == len(Desktop.created) == 2
assert runtime.cad is first and not first.closed
assert not Desktop.created[0].released
assert runtime.generation == 1
assert len(Cad.created) == len(Desktop.created) == 1
assert all(cad.closed for cad in Cad.created)
assert all(desktop.released for desktop in Desktop.created)
@pytest.mark.parametrize('damage', ['missing', 'foreign_project', 'foreign_session'])
def test_cross_project_reuse_requires_previous_closed_checkpoint(tmp_path, monkeypatch, damage):
import subprocess
one, _ = command_for(tmp_path)
two, _ = command_for(tmp_path, 'two')
calls = []
def monitor(command, *args, **kwargs):
calls.append(command)
return subprocess.CompletedProcess(command, 0, '', '')
runtime = worker_runtime(tmp_path, monkeypatch, monitor)
with runtime:
runtime.run_worker(one, 5)
path = Path(one[3]) / 'project-checkpoint.json'
if damage == 'missing':
path.unlink()
else:
checkpoint = json.loads(path.read_text())
checkpoint['project_path' if damage == 'foreign_project' else 'session_id'] = (
str(tmp_path / 'foreign.prj') if damage == 'foreign_project' else 'f' * 32)
path.write_text(json.dumps(checkpoint))
with pytest.raises(TaskCenterError, match='关闭检查点'):
runtime.run_worker(two, 5)
assert len(calls) == 1
assert len(Cad.created) == 1
def test_completed_project_does_not_hold_resolution_wait_for_next_project_failure(tmp_path, monkeypatch):
import subprocess
command, _ = command_for(tmp_path)
@@ -212,6 +281,35 @@ def test_current_task_and_attempt_are_durable_before_worker_launch(tmp_path, mon
runtime.run_worker(two, 5)
def test_next_task_attribution_is_durable_before_its_library_preflight(tmp_path, monkeypatch):
import subprocess
from dx_productivity.trial_task_center.batch_supervisor import _mark_interrupted
command, project = command_for(tmp_path)
runtime = worker_runtime(tmp_path, monkeypatch, lambda command, *a, **k:
subprocess.CompletedProcess(command, 0, '', ''))
with runtime:
runtime.run_worker(command, 5)
old_job = runtime.record['active_job']
old_attempts = tuple(runtime._current_attempts)
runtime.begin_task('B')
saved = json.loads(runtime.path.read_text())
assert saved['active_task'] == 'B'
assert saved['active_project'] == str(project.resolve())
assert saved['active_job'] == old_job and tuple(runtime._current_attempts) == old_attempts
_mark_interrupted(runtime.root, {'batch_id':runtime.session_id,
'operation':'execute_task_queue', 'task_ids':['task','B']}, 'crash during B library read')
assert not (runtime.root / 'tasks/task/quick-run/uncertain.flag').exists()
assert (runtime.root / 'tasks/B/quick-run/uncertain.flag').is_file()
def test_begin_task_before_cad_does_not_create_invalid_resource_record(tmp_path):
from dx_productivity.trial_task_center.batch_runtime import BatchCadRuntime
runtime = BatchCadRuntime(tmp_path, 'task_queue', desktop_factory=Desktop)
runtime.begin_task('A')
assert runtime.record['active_task'] == 'A'
assert not runtime.path.exists() and not Desktop.created
@pytest.mark.parametrize('second_fails', [False, True])
def test_prewrite_failure_releases_old_desktop_and_retries_only_once(tmp_path, monkeypatch, second_fails):
import subprocess
@@ -488,11 +586,11 @@ def test_mock_different_projects_each_get_one_retry_without_accumulating_budget(
runtime.run_worker(one, 5)
assert len(Desktop.created) == 2
runtime.run_worker(two, 5)
assert len(Desktop.created) == 4 # Separate project startup plus its single rebuild.
for expected_sessions, command in enumerate((one, two), 5):
assert len(Desktop.created) == 3 # Each project's first failure rebuilds once; healthy switching does not.
for command in (one, two):
with pytest.raises(WorkerMonitorError, match='prewrite failure'):
runtime.run_worker(command, 5)
assert len(Desktop.created) == expected_sessions # Switching projects is not another retry.
assert len(Desktop.created) == 3 # Switching projects grants no extra retry or startup.
assert calls == {one[3]: 3, two[3]: 3}
assert set(runtime.record['retries']) == {
str(project_one.resolve()).casefold(), str(project_two.resolve()).casefold()}