feat: persist batch attempt proof before CAD mutations

This commit is contained in:
阿白叔 committed 2026-10-21 14:28:25 +08:00
1 parent e15ee259f8
commit 832ab5d563
6 files changed
+457 -1

No files matched your search

+163
View File
@@ -0,0 +1,163 @@
import json
import os
import pytest
from dx_productivity.trial_task_center import batch_attempt as attempts
@pytest.fixture
def prepared(tmp_path, monkeypatch):
path = tmp_path / 'job.json'
record = attempts.prepare_attempt(path, session_id='session-a', generation=1,
task_id='task-a', job_id='job-a', project_path=tmp_path / 'board.prj')
monkeypatch.setenv('DXPART_ATTEMPT_FILE', str(path))
monkeypatch.setenv('DXPART_BATCH_SESSION_ID', 'session-a')
monkeypatch.setenv('DXPART_BATCH_GENERATION', '1')
monkeypatch.setenv('DXPART_ATTEMPT_ID', record['attempt_id'])
monkeypatch.setenv('DXPART_TASK_ID', 'task-a')
monkeypatch.setenv('DXPART_JOB_ID', 'job-a')
return path, record
def test_only_current_prepared_attempt_can_retry(prepared):
path, expected = prepared
assert attempts.can_retry_attempt(path, expected)
attempts.mark_mutation_possible(job_id='job-a')
assert attempts.read_attempt(path)['state'] == 'mutation_possible'
assert not attempts.can_retry_attempt(path, expected)
@pytest.mark.parametrize('field,value', [('session_id', 'old'), ('generation', 2),
('attempt_id', '0' * 32), ('task_id', 'old'), ('job_id', 'old'),
('project_path', 'other.prj')])
def test_retry_rejects_stale_identity(prepared, field, value):
path, expected = prepared
assert not attempts.can_retry_attempt(path, dict(expected, **{field: value}))
@pytest.mark.parametrize('contents', [None, '{truncated', '{}', '[]'])
def test_unreadable_or_incomplete_proof_never_allows_retry(prepared, contents):
path, expected = prepared
if contents is None:
path.unlink()
else:
path.write_text(contents)
assert not attempts.can_retry_attempt(path, expected)
with pytest.raises((OSError, ValueError)):
attempts.mark_mutation_possible(job_id='job-a')
@pytest.mark.parametrize('name,value', [('DXPART_BATCH_SESSION_ID', 'old'),
('DXPART_BATCH_GENERATION', '2'), ('DXPART_ATTEMPT_ID', '0' * 32),
('DXPART_TASK_ID', 'old'), ('DXPART_JOB_ID', 'old')])
def test_marker_blocks_stale_process_identity(prepared, monkeypatch, name, value):
path, _ = prepared
monkeypatch.setenv(name, value)
with pytest.raises(ValueError):
attempts.mark_mutation_possible(job_id='job-a')
assert attempts.read_attempt(path)['state'] == 'prepared'
def test_marker_fsync_failure_blocks_caller_without_publishing_mutation(prepared, monkeypatch):
path, _ = prepared
def fail(_):
raise OSError('durability failed')
monkeypatch.setattr(os, 'fsync', fail)
with pytest.raises(OSError, match='durability failed'):
attempts.mark_mutation_possible(job_id='job-a')
assert json.loads(path.read_text())['state'] == 'prepared'
def test_legacy_no_environment_remains_supported(monkeypatch):
for name in tuple(os.environ):
if name.startswith('DXPART_') and ('ATTEMPT' in name or 'BATCH_' in name):
monkeypatch.delenv(name)
assert attempts.mark_mutation_possible() is None
def test_blank_batch_path_is_not_legacy(monkeypatch):
monkeypatch.setenv('DXPART_ATTEMPT_FILE', '')
with pytest.raises(ValueError):
attempts.mark_mutation_possible()
def test_preparation_is_durable_before_return(tmp_path, monkeypatch):
fsynced = []
real_fsync = os.fsync
def fsync(fd):
fsynced.append(True)
return real_fsync(fd)
monkeypatch.setattr(os, 'fsync', fsync)
path = tmp_path / 'attempt.json'
first = attempts.prepare_attempt(path, session_id='s', generation=1,
task_id='t', job_id='j', project_path=tmp_path / 'board.prj')
assert fsynced and attempts.read_attempt(path)['state'] == 'prepared'
second = attempts.prepare_attempt(tmp_path / 'second.json', session_id='s', generation=1,
task_id='t', job_id='j', project_path=tmp_path / 'board.prj')
assert first['attempt_id'] != second['attempt_id']
def test_reused_worker_marks_each_logical_job(tmp_path, monkeypatch):
directory = tmp_path / 'attempt'
for job in ('job-a', 'job-b'):
attempts.prepare_attempt(directory / f'{job}.json', session_id='session-a', generation=1,
task_id='task-a', job_id=job, project_path=tmp_path / 'board.prj', attempt_id='a' * 32)
monkeypatch.setenv('DXPART_ATTEMPT_FILE', str(directory))
monkeypatch.setenv('DXPART_BATCH_SESSION_ID', 'session-a')
monkeypatch.setenv('DXPART_BATCH_GENERATION', '1')
monkeypatch.setenv('DXPART_ATTEMPT_ID', 'a' * 32)
for job in ('job-a', 'job-b'):
attempts.mark_mutation_possible(job_id=job, task_id='task-a', project_path=tmp_path / 'board.prj')
assert attempts.read_attempt(directory / f'{job}.json')['state'] == 'mutation_possible'
def test_native_guard_binds_snapshot_to_command_and_current_attempt(prepared, tmp_path):
from dx_productivity.trial_task_center.native_helper import mark_snapshot_mutation
path, _ = prepared
snapshot = tmp_path / 'snapshot-job-a.tsv'
snapshot.write_text(f'M\t{tmp_path / "board.prj"}\tOLD\tNEW\tsheet:1\nC\tu1\tR1\t0\t0\t0\t0\t0\n')
(tmp_path / 'command.tsv').write_text('task_id\tjob_id\tproject_path\n'
f'task-a\tjob-a\t{tmp_path / "board.prj"}\n')
assert mark_snapshot_mutation(snapshot)['state'] == 'mutation_possible'
assert attempts.read_attempt(path)['state'] == 'mutation_possible'
def test_native_guard_rejects_other_project_before_marking(prepared, tmp_path):
from dx_productivity.trial_task_center.native_helper import mark_snapshot_mutation
path, _ = prepared
snapshot = tmp_path / 'snapshot-job-a.tsv'
snapshot.write_text(f'M\t{tmp_path / "other.prj"}\tOLD\tNEW\tsheet:1\nC\tu1\tR1\t0\t0\t0\t0\t0\n')
(tmp_path / 'command.tsv').write_text('task_id\tjob_id\tproject_path\n'
f'task-a\tjob-a\t{tmp_path / "board.prj"}\n')
with pytest.raises(ValueError):
mark_snapshot_mutation(snapshot)
assert attempts.read_attempt(path)['state'] == 'prepared'
def test_marker_cli_fails_closed_on_stale_job(prepared):
from dx_productivity.trial_task_center.application import main
path, record = prepared
assert main(['--mark-batch-attempt', '--attempt-job', 'other', '--attempt-task', 'task-a',
'--attempt-project', record['project_path']]) == 1
assert attempts.read_attempt(path)['state'] == 'prepared'
def test_native_failure_does_not_reuse_previous_attempt_transaction(prepared, tmp_path, monkeypatch):
from dx_productivity.trial_task_center import native_helper as native
path, _ = prepared
snapshot = tmp_path / 'snapshot-job-a.tsv'
snapshot.write_text(f'M\t{tmp_path / "board.prj"}\tOLD\tNEW\tsheet:1\nC\tu1\tR1\t0\t0\t0\t0\t0\n')
(tmp_path / 'command.tsv').write_text('task_id\tjob_id\tproject_path\n'
f'task-a\tjob-a\t{tmp_path / "board.prj"}\n')
report_path = tmp_path / 'snapshot-job-a.tsv.native.json'
report_path.write_text(json.dumps({'stage': 'native_replace_started',
'transaction': {'old': 'unrelated'}, 'batch_attempt': {'attempt_id': 'old'}}))
def unavailable_cad(snapshot, report):
raise RuntimeError('CAD unavailable before mutation')
monkeypatch.setattr(native, 'run_native', unavailable_cad)
assert native.run(snapshot) == 1
report = json.loads(report_path.read_text())
assert report['stage'] == 'preflight' and 'transaction' not in report
assert report['batch_attempt']['job_id'] == 'job-a'
assert attempts.read_attempt(path)['state'] == 'prepared'
+60
View File
@@ -0,0 +1,60 @@
import json
from pathlib import Path
import sys
import pytest
from test_accuracy_vbs_guards import run_vbs
from dx_productivity.trial_task_center.batch_attempt import prepare_attempt
@pytest.mark.parametrize('fault', ['', 'missing', 'stale', 'wrong-project', 'missing-path'])
def test_vbs_guard_prevents_property_write_without_durable_current_proof(tmp_path, monkeypatch, fault):
root = Path(__file__).resolve().parents[2]
monkeypatch.setenv('PYTHONPATH', str(root / 'src'))
directory = tmp_path / 'attempt'
record_path = directory / 'job-a.json'
record = prepare_attempt(record_path, session_id='session-a', generation=1,
task_id='task-a', job_id='job-a', project_path=tmp_path / 'board.prj')
monkeypatch.setenv('DXPART_ATTEMPT_FILE', str(directory))
monkeypatch.setenv('DXPART_BATCH_SESSION_ID', 'session-a')
monkeypatch.setenv('DXPART_BATCH_GENERATION', '1')
monkeypatch.setenv('DXPART_ATTEMPT_ID', '0' * 32 if fault == 'stale' else record['attempt_id'])
if fault == 'missing':
record_path.unlink()
if fault == 'missing-path':
monkeypatch.delenv('DXPART_ATTEMPT_FILE')
monkeypatch.delenv('DXPART_BATCH_GENERATION')
monkeypatch.delenv('DXPART_ATTEMPT_ID')
project = tmp_path / ('other.prj' if fault == 'wrong-project' else 'board.prj')
output = run_vbs(tmp_path, ['QPMarkBatchMutation'], '''
Dim Command, Attr, Failure
Set Command = CreateObject("Scripting.Dictionary")
Command.Add "job_id", "job-a": Command.Add "task_id", "task-a"
Command.Add "project_path", "''' + str(project) + '''"
Set Attr = New PropertySink
On Error Resume Next
GuardedWrite Command, Attr
Failure = Err.Number
On Error GoTo 0
WScript.Echo CStr(Failure <> 0)
WScript.Echo Attr.Value
''', '''
Dim QP_HelperExecutable, QP_HelperScript, QP_Fso
Set QP_Fso = CreateObject("Scripting.FileSystemObject")
QP_HelperExecutable = "''' + sys.executable + '''"
QP_HelperScript = "''' + str(root / 'src/dx_productivity/trial_task_center/application.py') + '''"
Class PropertySink
Public Value
Private Sub Class_Initialize
Value = "unchanged"
End Sub
End Class
Sub GuardedWrite(ByRef Command, ByRef Attr)
QPMarkBatchMutation Command
Attr.Value = "changed"
End Sub
''')
assert output.splitlines() == (['False', 'changed'] if not fault else ['True', 'unchanged'])
if not fault:
assert json.loads(record_path.read_text())['state'] == 'mutation_possible'