feat(dsh): add CAD verification recovery diff and issue broker
This commit is contained in:
1 parent
eee4e6bde9
commit
27334fef81
23 files changed
+553
-12
No files matched your search
@@ -0,0 +1,45 @@
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace as NS
|
||||
|
||||
import pytest
|
||||
|
||||
from dx_productivity.dsh.cad_verification import CadVerificationAdapter
|
||||
|
||||
|
||||
def service(tmp_path, *, probe=True, interrupted=False):
|
||||
project = tmp_path / 'board.prj'; project.write_text('KEY FrontEndSnapshot "DxD"\n', encoding='utf8')
|
||||
probe_path = tmp_path / 'probe.json'
|
||||
if probe: probe_path.write_text('{"project":"board"}', encoding='utf8')
|
||||
class Service:
|
||||
def task_context(self, task_id):
|
||||
return NS(task=NS(task_id=task_id, status=NS(value='ready')), project=NS(path=project, project_id='p1'),
|
||||
progress=NS(saved_items=1, failed_items=0, no_match_items=0, queue_complete=True),
|
||||
interrupted=interrupted, phase='complete')
|
||||
def expected_probe_path(self, task_id, project_id=None): return probe_path
|
||||
return Service(), project
|
||||
|
||||
|
||||
def test_adapter_reports_real_project_and_probe_identity(tmp_path):
|
||||
svc, project = service(tmp_path)
|
||||
result = CadVerificationAdapter(svc).preflight(['task-1'])
|
||||
assert result['status'] == 'ready' and result['mode'] == 'task_center_native'
|
||||
assert result['projects'][0]['path'] == str(project)
|
||||
assert result['projects'][0]['probe_available'] is True
|
||||
assert result['evidence_refs']
|
||||
|
||||
|
||||
def test_adapter_blocks_missing_probe_and_uncertain_result(tmp_path):
|
||||
svc, _ = service(tmp_path, probe=False, interrupted=True)
|
||||
result = CadVerificationAdapter(svc).preflight(['task-1'])
|
||||
assert result['status'] == 'blocked'
|
||||
assert 'probe_baseline_missing' in result['blockers']
|
||||
after = CadVerificationAdapter(svc).after_run(['task-1'])
|
||||
assert after['status'] == 'uncertain' and after['tasks'][0]['interrupted'] is True
|
||||
|
||||
|
||||
def test_adapter_rejects_missing_task_without_inventing_counts(tmp_path):
|
||||
svc, _ = service(tmp_path)
|
||||
svc.task_context = lambda task_id: (_ for _ in ()).throw(KeyError(task_id))
|
||||
result = CadVerificationAdapter(svc).preflight(['missing'])
|
||||
assert result['status'] == 'blocked'
|
||||
assert result['projects'][0]['component_count'] is None
|
||||
@@ -0,0 +1,14 @@
|
||||
from dx_productivity.dsh.failure_policy import classify_failure
|
||||
|
||||
|
||||
def test_failure_policy_allows_one_authorized_transient_retry():
|
||||
value = classify_failure('worker_timeout', 'failed', authorized=True)
|
||||
assert value == {'category': 'transient', 'retry_allowed': True, 'reason': '可重试的临时执行错误',
|
||||
'backoff_seconds': 5, 'max_attempts': 1}
|
||||
|
||||
|
||||
def test_failure_policy_blocks_uncertain_and_deterministic_errors():
|
||||
assert classify_failure('quick_run_uncertain', 'uncertain', interrupted=True)['category'] == 'uncertain'
|
||||
assert classify_failure('target_identity', 'failed', authorized=True)['retry_allowed'] is False
|
||||
assert classify_failure('paused', 'paused', authorized=True)['category'] == 'paused'
|
||||
assert classify_failure('new_error', 'failed')['category'] == 'observe'
|
||||
@@ -53,6 +53,7 @@ def test_verified_repair_runs_once_and_duplicate_does_not_repeat(tmp_path):
|
||||
assert second['incident_id'] == first['incident_id']
|
||||
assert service.calls == ['prepare', 'execute']
|
||||
assert first['verification']['failed_items'] == 0
|
||||
assert first['gates']['failure_policy']['category'] == 'transient'
|
||||
decisions = (tmp_path / 'dsh' / 'decisions.jsonl').read_text(encoding='utf8').splitlines()
|
||||
assert decisions
|
||||
assert json.loads(decisions[-1])['incident_id'] == first['incident_id']
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import json
|
||||
|
||||
from dx_productivity.dsh.issue_broker import IssueBroker, IssueBrokerConfig
|
||||
|
||||
|
||||
TARGET = {'server': 'https://git.test', 'repository': 'owner/repo'}
|
||||
|
||||
|
||||
def test_broker_exchange_issues_short_lived_fixed_target_token(monkeypatch):
|
||||
monkeypatch.setenv('DSH_BROKER_SECRET', 'server-secret')
|
||||
config = IssueBrokerConfig.from_environment({**TARGET, 'token': 'git-issue-token', 'secret': 'server-secret'})
|
||||
broker = IssueBroker(config)
|
||||
value = broker.exchange({**TARGET, 'instance_id': 'machine-1', 'instance_proof': broker.proof('machine-1'),
|
||||
'idempotency_key': 'event-1'})
|
||||
assert value['token'] == 'git-issue-token' and 30 <= value['expires_in'] <= 3600
|
||||
assert value['repository'] == TARGET['repository']
|
||||
|
||||
|
||||
def test_broker_rejects_wrong_target_and_reuses_idempotency(monkeypatch):
|
||||
monkeypatch.setenv('DSH_BROKER_SECRET', 'server-secret')
|
||||
config = IssueBrokerConfig.from_environment({**TARGET, 'token': 'git-issue-token', 'secret': 'server-secret'})
|
||||
broker = IssueBroker(config)
|
||||
payload = {'server': TARGET['server'], 'repository': TARGET['repository'], 'instance_id': 'machine-1',
|
||||
'instance_proof': broker.proof('machine-1'), 'idempotency_key': 'event-1'}
|
||||
first = broker.exchange(payload)
|
||||
assert broker.exchange(payload) == first
|
||||
try:
|
||||
broker.exchange(payload | {'repository': 'other/repo'})
|
||||
except ValueError as error:
|
||||
assert str(error) == 'invalid_target'
|
||||
else:
|
||||
raise AssertionError('wrong target accepted')
|
||||
@@ -8,7 +8,7 @@ TARGET = {'server': 'https://git.test', 'repository': 'owner/repo'}
|
||||
|
||||
def test_transport_uses_short_lived_broker_credential_without_caching_token(tmp_path, monkeypatch):
|
||||
config = tmp_path / 'issue-config.json'
|
||||
config.write_text(json.dumps({**TARGET, 'broker_url': 'https://broker.test/exchange', 'instance_id': 'machine-1'}), encoding='utf8')
|
||||
config.write_text(json.dumps({**TARGET, 'broker_url': 'https://broker.test/exchange', 'instance_id': 'machine-1', 'instance_proof': 'proof', 'idempotency_key': 'event-1'}), encoding='utf8')
|
||||
monkeypatch.setattr('dx_productivity.dsh.issue_transport._post_broker', lambda *args, **kwargs: {'token': 'short-secret', 'expires_in': 120})
|
||||
value = IssueTransport(config).credentials(TARGET)
|
||||
assert value['source'] == 'broker'
|
||||
@@ -29,7 +29,7 @@ def test_transport_marks_bootstrap_fallback_for_rotation(tmp_path, monkeypatch):
|
||||
|
||||
def test_transport_rejects_target_before_broker_or_credentials(tmp_path, monkeypatch):
|
||||
config = tmp_path / 'issue-config.json'
|
||||
config.write_text(json.dumps({**TARGET, 'broker_url': 'https://broker.test/exchange'}), encoding='utf8')
|
||||
config.write_text(json.dumps({**TARGET, 'broker_url': 'https://broker.test/exchange', 'instance_id': 'machine-1', 'instance_proof': 'proof', 'idempotency_key': 'event-1'}), encoding='utf8')
|
||||
monkeypatch.setattr('dx_productivity.dsh.issue_transport._post_broker', lambda *args, **kwargs: (_ for _ in ()).throw(AssertionError('network called')))
|
||||
value = IssueTransport(config).credentials({'server': 'https://evil.test', 'repository': 'owner/repo'})
|
||||
assert value['source'] == 'invalid_target'
|
||||
@@ -39,7 +39,7 @@ def test_transport_rejects_target_before_broker_or_credentials(tmp_path, monkeyp
|
||||
def test_git_publisher_connection_prefers_broker_transport(tmp_path, monkeypatch):
|
||||
from dx_productivity.dsh.git_issues import GitIssuePublisher
|
||||
config = tmp_path / 'issue-config.json'
|
||||
config.write_text(json.dumps({**TARGET, 'broker_url': 'https://broker.test/exchange'}), encoding='utf8')
|
||||
config.write_text(json.dumps({**TARGET, 'broker_url': 'https://broker.test/exchange', 'instance_id': 'machine-1', 'instance_proof': 'proof', 'idempotency_key': 'event-1'}), encoding='utf8')
|
||||
monkeypatch.setattr('dx_productivity.dsh.issue_transport._post_broker', lambda *args, **kwargs: {'token': 'ephemeral', 'expires_in': 60})
|
||||
_, headers, source = GitIssuePublisher(config).connection()
|
||||
assert headers == {'Authorization': 'token ephemeral'}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
from types import SimpleNamespace as NS
|
||||
|
||||
from dx_productivity.dsh.resume_diff import resume_diff
|
||||
|
||||
|
||||
class Service:
|
||||
def __init__(self, contexts, hashes=None): self.contexts, self.current_plan_hashes = contexts, hashes or {'t1': 'h1', 't2': 'h2'}
|
||||
def task_context(self, task_id): return self.contexts[task_id]
|
||||
|
||||
|
||||
def context(status='ready', interrupted=False):
|
||||
return NS(task=NS(status=NS(value=status), name='Board'), progress=NS(queue_complete=status == 'completed',
|
||||
saved_items=1 if status == 'completed' else 0, failed_items=0, no_match_items=0), interrupted=interrupted)
|
||||
|
||||
|
||||
def inspected():
|
||||
return {'run_id': 'a' * 32, 'task_ids': ['t1', 't2'], 'remaining_task_ids': ['t2'],
|
||||
'plan_hashes': {'t1': 'h1', 't2': 'h2'}, 'queue_available': True, 'resume_ready': True,
|
||||
'blockers': [], 'tasks': [{'task_id': 't1', 'status': 'completed'}, {'task_id': 't2', 'status': 'ready'}]}
|
||||
|
||||
|
||||
def test_resume_diff_is_safe_for_unchanged_frozen_scope():
|
||||
value = resume_diff(Service({'t1': context('completed'), 't2': context()}), inspected())
|
||||
assert value['safe'] is True and value['completed'] == ['t1'] and value['remaining'] == ['t2']
|
||||
assert value['changed'] == []
|
||||
|
||||
|
||||
def test_resume_diff_blocks_plan_change_and_uncertainty():
|
||||
row = inspected()
|
||||
value = resume_diff(Service({'t1': context('completed', interrupted=True), 't2': context()}, {'t1': 'changed', 't2': 'h2'}), row)
|
||||
assert value['safe'] is False
|
||||
assert 'plan_changed' in value['blockers'] and 'interrupted' in value['blockers']
|
||||
Reference in new issue
Block a user