fix: preflight short batch attempts and preserve cleanup causes

This commit is contained in:
阿白叔 committed 2026-10-23 15:11:16 +08:00
1 parent b21960cda1
commit 7d5ea814ec
11 files changed
+332 -94

No files matched your search

@@ -8,15 +8,16 @@ import os
from pathlib import Path
import re
import tempfile
import time
from uuid import uuid4
from .path_access import canonical_path, internal_path, retry_sharing_violation
_IDENTITY = ('session_id', 'generation', 'attempt_id', 'task_id', 'job_id', 'project_path')
def _project(value):
return os.path.normcase(os.path.abspath(value))
return os.path.normcase(str(canonical_path(value)))
def _validate(record):
@@ -37,22 +38,14 @@ def _validate(record):
def _publish(path, record):
path = Path(path)
path = internal_path(path)
descriptor, temporary = tempfile.mkstemp(prefix=f'.{path.name}.', suffix='.tmp', dir=path.parent)
try:
with os.fdopen(descriptor, 'w', encoding='utf-8', newline='') as handle:
json.dump(record, handle, ensure_ascii=False, indent=2)
handle.flush()
os.fsync(handle.fileno())
deadline = time.monotonic() + 1
while True:
try:
os.replace(temporary, path)
break
except PermissionError:
if time.monotonic() >= deadline:
raise
time.sleep(.02)
retry_sharing_violation(lambda: os.replace(temporary, path))
# POSIX needs a directory sync for the atomic rename; Windows does not
# expose fsync on directories through the CRT.
if os.name != 'nt':
@@ -69,13 +62,13 @@ def prepare_attempt(path, *, session_id, generation, task_id, job_id, project_pa
record = _validate(dict(schema_version=1, session_id=str(session_id), generation=generation,
task_id=str(task_id), job_id=str(job_id), project_path=_project(project_path),
attempt_id=uuid4().hex if attempt_id is None else attempt_id, state='prepared'))
Path(path).parent.mkdir(parents=True, exist_ok=True)
retry_sharing_violation(lambda: internal_path(path).parent.mkdir(parents=True, exist_ok=True))
_publish(path, record)
return record
def read_attempt(path):
return _validate(json.loads(Path(path).read_text(encoding='utf-8')))
return _validate(json.loads(internal_path(path).read_text(encoding='utf-8')))
def _bound_attempt(path=None, *, job_id=None, task_id=None, project_path=None):
@@ -86,7 +79,7 @@ def _bound_attempt(path=None, *, job_id=None, task_id=None, project_path=None):
raw_path = os.environ.get('DXPART_ATTEMPT_FILE') if path is None else os.fspath(path)
if not raw_path or not Path(raw_path).is_absolute():
raise ValueError('invalid batch attempt file')
path = Path(raw_path)
path = internal_path(raw_path)
job_id = str(job_id) if job_id is not None else os.environ.get('DXPART_JOB_ID')
task_id = str(task_id) if task_id is not None else os.environ.get('DXPART_TASK_ID')
if path.is_dir():
@@ -9,6 +9,7 @@ import time
from uuid import uuid4
from .errors import TaskCenterError
from .path_access import canonical_path, internal_path, retry_sharing_violation
# Recovery retains owned native handles after failed cleanup. This registry is
@@ -17,29 +18,24 @@ LIVE_RESOURCES = {}
class BatchResourceCleanupError(TaskCenterError):
def __init__(self, message, *, completed_result=None):
def __init__(self, message, *, completed_result=None, primary_error=None, cleanup_error=None):
super().__init__('batch_resources_pending', message)
self.completed_result = completed_result
self.primary_error = primary_error
self.cleanup_error = cleanup_error
self.bom_report = getattr(primary_error, "bom_report", None)
def write_record(path, record):
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
path = internal_path(path)
retry_sharing_violation(lambda: path.parent.mkdir(parents=True, exist_ok=True))
temporary = path.with_name(path.name + '.' + uuid4().hex + '.tmp')
try:
with temporary.open('w', encoding='utf-8', newline='\n') as stream:
json.dump(record, stream, ensure_ascii=False, indent=2)
stream.flush()
os.fsync(stream.fileno())
deadline = time.monotonic() + 1.0
while True:
try:
os.replace(temporary, path)
break
except PermissionError:
if time.monotonic() >= deadline:
raise
time.sleep(.02)
retry_sharing_violation(lambda: os.replace(temporary, path))
finally:
temporary.unlink(missing_ok=True)
@@ -70,9 +66,10 @@ def _valid_record(record, path):
def pending_resources(root):
pending = []
for path in sorted((Path(root).resolve() / 'batch-resources').glob('*/session.json')):
for path in sorted(internal_path(canonical_path(root) / 'batch-resources').glob('*/session.json')):
path = canonical_path(path)
try:
record = json.loads(path.read_text(encoding='utf-8'))
record = json.loads(internal_path(path).read_text(encoding='utf-8'))
if not _valid_record(record, path):
raise ValueError('invalid resource ownership record')
if record.get('state') != 'released':
@@ -169,11 +166,11 @@ def cleanup_pending_resources(root, *, logging_context=None):
def project_checkpoint_released(root):
root = Path(root).resolve()
root = canonical_path(root)
try:
checkpoint = json.loads((root / 'project-checkpoint.json').read_text(encoding='utf-8'))
checkpoint = json.loads(internal_path(root / 'project-checkpoint.json').read_text(encoding='utf-8'))
path = Path(checkpoint['resource_record']).resolve()
record = json.loads(path.read_text(encoding='utf-8'))
record = json.loads(internal_path(path).read_text(encoding='utf-8'))
key = str(root).casefold()
return (_valid_record(record, path) and checkpoint.get('version') == 1
and type(checkpoint.get('generation')) is int
@@ -6,11 +6,12 @@ import json
from pathlib import Path
import subprocess
import time
from uuid import uuid4
from uuid import UUID, uuid4
from .batch_resources import (BatchResourceCleanupError, LIVE_RESOURCES, cleanup_pending_resources,
guard_pending_resources, pending_resources, process_identity, project_checkpoint_released, write_record)
from .errors import TaskCenterError
from .path_access import canonical_path, internal_path, legacy_path
from .isolated_desktop import PrivateDesktop
from .operation_control import check_cancelled
from .worker_monitor import WorkerMonitorError
@@ -43,8 +44,12 @@ class _DesktopLease:
return {'session_id': self.owner.session_id, 'generation': self.generation,
'attempt_id': self.attempt_id}
def __exit__(self, *_exc):
self.close()
def __exit__(self, error_type, error, traceback):
try:
self.close()
except BatchResourceCleanupError as cleanup:
raise BatchResourceCleanupError(cleanup.message, primary_error=error,
cleanup_error=cleanup) from cleanup
def start_process(self, command, **options):
available = self.owner.record['state'] == 'ready' or (
@@ -78,10 +83,10 @@ class _DesktopLease:
class BatchCadRuntime:
def __init__(self, root, kind, stop_event=None, *, desktop_factory=None, cad_factory=None):
self.root = Path(root).resolve()
def __init__(self, root, kind, stop_event=None, *, desktop_factory=None, cad_factory=None, session_id=None):
self.root = canonical_path(root)
self.kind, self.stop_event = kind, stop_event
self.session_id = uuid4().hex
self.session_id = uuid4().hex if session_id is None else UUID(str(session_id)).hex
self.path = self.root / 'batch-resources' / self.session_id / 'session.json'
self.desktop_factory = desktop_factory or PrivateDesktop
self.cad_factory = cad_factory
@@ -114,8 +119,20 @@ class BatchCadRuntime:
try:
self.close()
except Exception as cleanup:
raise BatchResourceCleanupError('批次资源释放待处理;已核验任务结果保留。' + str(cleanup),
completed_result=self.result) from cleanup
def detail(failure):
return {'type': type(failure).__name__, 'message': str(failure),
'code': getattr(failure, 'code', None)}
self.record['primary_error'] = detail(error) if error is not None else None
self.record['cleanup_error'] = detail(cleanup)
try:
self._save()
except Exception:
pass # Journal failure must not replace either original failure.
context = ('BOM 导入读取已停止;未生成可执行预览。'
if self.kind == 'bom_import' else
'已核验任务结果保留。' if self.result is not None else '本批操作已停止。')
raise BatchResourceCleanupError('批次资源释放待处理;' + context + str(cleanup),
completed_result=self.result, primary_error=error, cleanup_error=cleanup) from cleanup
def _save(self, **values):
record = dict(self.record, **values)
@@ -188,26 +205,26 @@ class BatchCadRuntime:
self._save(state='ready', cad_pid=cad.binding.pid, cad_guid=cad.binding.guid)
return self.cad
def _prepare_attempts(self, root, command, attempt_id):
def _prepare_attempts(self, root, command, attempt_id, generation):
from .batch_attempt import prepare_attempt
from .quick_run import _read_commands
rows = _read_commands(root / 'command.tsv')
rows = _read_commands(internal_path(root / 'command.tsv'))
selected_rows = [row for row in rows if row[1] == command[-1]]
if len(selected_rows) != 1 or len({row[1] for row in rows}) != len(rows):
raise TaskCenterError('target_identity', '批次工作命令身份缺失或重复,已停止。')
selected = selected_rows[0]
project = Path(selected[2]).resolve()
self._save(active_project=str(project), active_task=selected[0], active_job=selected[1])
directory = root / 'batch-attempts' / (self.session_id + f'-g{self.generation}-' + attempt_id)
project = canonical_path(selected[2])
self.record.update(active_project=str(project), active_task=selected[0], active_job=selected[1])
directory = self.path.parent / 'attempts' / f'g{generation}' / attempt_id
expected = []
for row in rows:
if Path(row[2]).resolve() == project:
if canonical_path(row[2]) == project:
path = directory / (row[1] + '.json')
record = prepare_attempt(path, session_id=self.session_id, generation=self.generation,
record = prepare_attempt(path, session_id=self.session_id, generation=generation,
task_id=row[0], job_id=row[1], project_path=str(project), attempt_id=attempt_id)
expected.append((path, record))
self._project_attempts.setdefault(str(project).casefold(), []).extend(expected)
return project, directory, expected
return project, legacy_path(directory), expected
def _can_retry(self, project, expected):
from .batch_attempt import can_retry_attempt
@@ -229,18 +246,21 @@ class BatchCadRuntime:
from .discovery_cache import project_fingerprint
from .isolated_cad import PrivateCadStartupError, is_identity_conflict
from .worker_monitor import run_monitored_process
root = Path(command[3]).resolve()
self._ensure_desktop(abort_event)
root = canonical_path(command[3])
while True:
check_cancelled(abort_event or self.stop_event)
attempt_id = uuid4().hex
# No previous empty-project certificate may authorize continuation
# once another operation could attach or open a project.
self.record['checkpoints'].pop(str(root).casefold(), None)
self._save(active_attempt=attempt_id)
project, directory, expected = self._prepare_attempts(root, command, attempt_id)
self.record['active_attempt'] = attempt_id
if self.generation:
self._save()
generation = self.generation + (self.desktop is None)
project, directory, expected = self._prepare_attempts(root, command, attempt_id, generation)
self._ensure_desktop(abort_event)
before = project_fingerprint(project, self.root)
status = root / 'status.tsv'
status = internal_path(root / 'status.tsv')
status_before = status.read_bytes() if status.exists() else None
started = time.monotonic()
failure = None
@@ -270,7 +290,7 @@ class BatchCadRuntime:
# available for a failed read, use the stricter full fingerprint.
safe = (not is_identity_conflict(failure) and before is not None and before == after
and status_before == status_after and self._can_retry(project, expected)
and not (root / 'recovery.request').exists()
and not internal_path(root / 'recovery.request').exists()
and not (abort_event is not None and abort_event.is_set())
and not (self.stop_event is not None and self.stop_event.is_set()))
if not safe:
@@ -281,7 +301,6 @@ class BatchCadRuntime:
self._save(state='recycling')
self.close(rebuilding=True) # Prove disappearance before creating another.
self.desktop = self.cad = None
self._ensure_desktop(abort_event)
def recover_native_failure(self, project, evidence, reason):
"""One read-only recovery, based on durable source and worker proof."""
@@ -11,6 +11,7 @@ import tempfile
from threading import RLock
from .errors import TaskCenterError
from .path_access import canonical_path, internal_path, retry_sharing_violation, strip_extended_prefix
def _short_path(path: Path) -> str | None:
@@ -18,8 +19,8 @@ def _short_path(path: Path) -> str | None:
function.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_uint]
function.restype = ctypes.c_uint
buffer = ctypes.create_unicode_buffer(32768)
count = function(str(path), buffer, len(buffer))
return buffer.value if count and count < len(buffer) else None
count = function(str(internal_path(path)), buffer, len(buffer))
return strip_extended_prefix(buffer.value) if count and count < len(buffer) else None
def _fits(root) -> bool:
@@ -34,12 +35,13 @@ class _AliasPool:
self.lock = RLock()
def alias(self, root: Path) -> Path:
root = canonical_path(root, strict=True)
import _winapi
with self.lock:
if self.directory is None:
local = Path(os.environ.get('LOCALAPPDATA') or tempfile.gettempdir())
parent = local / 'DxProductivity' / 'path-links'
parent.mkdir(parents=True, exist_ok=True)
internal_path(parent).mkdir(parents=True, exist_ok=True)
self.directory = Path(tempfile.mkdtemp(prefix='dxp-', dir=parent))
key = hashlib.sha256(os.path.normcase(str(root)).encode('utf-8')).hexdigest()[:20]
link = self.directory / key
@@ -50,7 +52,7 @@ class _AliasPool:
# junction contains no copies and never changes the target.
_winapi.CreateJunction(str(root), str(link))
self.links[link] = root
if not link.is_junction() or link.resolve(strict=True) != root:
if not link.is_junction() or canonical_path(link, strict=True) != root:
raise OSError('compatibility directory no longer points to the confirmed evidence')
return link
@@ -60,7 +62,7 @@ class _AliasPool:
for link, target in tuple(self.links.items()):
try:
if link.is_junction():
if link.resolve() != target:
if canonical_path(link) != target:
raise OSError('refusing to remove a redirected compatibility directory')
link.rmdir() # Remove only the link, never the target tree.
elif link.exists():
@@ -88,11 +90,12 @@ atexit.register(cleanup_worker_aliases)
def worker_paths(script: Path, root: Path) -> tuple[str, str]:
script, root = canonical_path(script), canonical_path(root)
if os.name == 'nt':
_prepare_project_access(root)
if os.name != 'nt' or _fits(root):
return str(script), str(root)
canonical = root.resolve(strict=True)
canonical = canonical_path(internal_path(root), strict=True)
short = _short_path(canonical)
if short and _fits(short):
return str(Path(short) / script.name), short
@@ -106,7 +109,7 @@ def worker_paths(script: Path, root: Path) -> tuple[str, str]:
def _prepare_project_access(root: Path):
"""Keep command identities canonical while giving legacy CAD short entry paths."""
command = root / 'command.tsv'
command = internal_path(root / 'command.tsv')
if not command.is_file():
return
rows = []
@@ -117,32 +120,32 @@ def _prepare_project_access(root: Path):
if not value or value in seen:
continue
seen.add(value)
project = Path(value).resolve(strict=False)
if (_fits(project.parent) and len(str(project)) < 260) or not project.is_file():
project = canonical_path(internal_path(value))
if (_fits(project.parent) and len(str(project)) < 260) or not internal_path(project).is_file():
continue
short = _short_path(project)
if short and _fits(Path(short).parent) and len(short) < 260:
access = Path(short)
else:
access = _aliases.alias(project.parent) / project.name
if not _fits(access.parent) or len(str(access)) >= 260 or access.resolve(strict=True) != project:
if not _fits(access.parent) or len(str(access)) >= 260 or canonical_path(access, strict=True) != project:
raise TaskCenterError('quick_run_path_length', '工程短路径入口不能与原工程身份对应,未启动 CAD 操作。')
pair = (str(project), str(access))
if pair not in rows:
rows.append(pair)
target = root / 'project-access.tsv'
target = internal_path(root / 'project-access.tsv')
if rows or target.exists():
temporary = target.with_suffix('.tmp')
with temporary.open('w', encoding='utf-8', newline='') as stream:
writer = csv.writer(stream, delimiter='\t', lineterminator='\n')
writer.writerow(('project_path', 'access_path'))
writer.writerows(rows)
temporary.replace(target)
retry_sharing_violation(lambda: temporary.replace(target))
def validate_project_access(root: Path):
"""Reject disappeared/redirected entry paths before and after a native worker."""
target = Path(root) / 'project-access.tsv'
target = internal_path(Path(root) / 'project-access.tsv')
if not target.is_file():
return
with target.open(encoding='utf-8-sig', newline='') as stream:
@@ -151,7 +154,7 @@ def validate_project_access(root: Path):
raise TaskCenterError('target_identity', '工程路径入口清单格式已改变')
for row in reader:
try:
same = Path(row['access_path']).resolve(strict=True) == Path(row['project_path']).resolve(strict=True)
same = canonical_path(internal_path(row['access_path']), strict=True) == canonical_path(internal_path(row['project_path']), strict=True)
except (OSError, ValueError, KeyError):
same = False
if not same:
@@ -7,11 +7,13 @@ import os
from pathlib import Path
import re
import shutil
import sys
import tempfile
import time
from uuid import uuid4
from .errors import TaskCenterError
from .path_access import canonical_path, internal_path, retry_sharing_violation
from .isolated_desktop import PrivateDesktop
from .native_inventory_protocol import NativeInventoryUnsupported, read_ascii_discovery, read_native_text
from .operation_control import check_cancelled, report_progress
@@ -27,20 +29,20 @@ class _NativeInventoryProcessError(NativeInventoryUnsupported):
def _hash(path):
with path.open('rb') as stream:
with internal_path(path).open('rb') as stream:
return hashlib.file_digest(stream, 'sha256').hexdigest()
def _verify_sources(hashes):
try:
if any(path.resolve(strict=True) != path or _hash(path) != digest for path, digest in hashes.items()):
if any(canonical_path(internal_path(path), strict=True) != path or _hash(path) != digest for path, digest in hashes.items()):
raise OSError('source identity changed')
except OSError as error:
raise TaskCenterError('target_identity', '原生采集期间源工程或数据库已变化或无法核验,请重新导入') from error
def _project_options(project):
text = read_native_text(project)
text = read_native_text(internal_path(project))
def option(name, default=None):
values = set(re.findall(r'^KEY ' + name + r' "([^"\r\n]*)"\s*$', text, re.MULTILINE))
if not values and default is not None:
@@ -54,8 +56,8 @@ def _project_options(project):
snapshot = option('FrontEndSnapshot', 'DxD')
if snapshot.casefold() != 'dxd':
raise NativeInventoryUnsupported('当前工程不是已验证的 DxD 数据库快照')
database = (project.parent / option('iCDBDir') / 'icdb.dat').resolve(strict=True)
if not database.is_relative_to(project.parent) or not database.is_file():
database = canonical_path(internal_path(project.parent / option('iCDBDir') / 'icdb.dat'), strict=True)
if not database.is_relative_to(project.parent) or not internal_path(database).is_file():
raise NativeInventoryUnsupported('数据库不在当前工程目录内')
return text, block, snapshot, database
@@ -83,7 +85,7 @@ class NativeInventoryReader:
def read(self, project, old_part_numbers, *, stop_event=None, on_progress=None, runtime=None):
check_cancelled(stop_event)
try:
project = Path(project).resolve(strict=True)
project = canonical_path(internal_path(project), strict=True)
text, block, snapshot, database = _project_options(project)
tools = self._tools()
except (NativeInventoryUnsupported, OSError, ImportError, UnicodeError, TaskCenterError) as error:
@@ -99,13 +101,13 @@ class NativeInventoryReader:
def _read(self, project, text, block, snapshot, database, tools, old_part_numbers, *, stop_event, on_progress, runtime=None, allow_retry=False):
token = uuid4().hex
evidence = self.data_root / 'quick-run-history' / ('native-inventory-' + token)
evidence.mkdir(parents=True, exist_ok=False)
evidence = internal_path(self.data_root / 'quick-run-history' / ('native-inventory-' + token))
retry_sharing_violation(lambda: evidence.mkdir(parents=True, exist_ok=False))
base = Path(self.temporary_root or Path(os.environ.get('LOCALAPPDATA') or tempfile.gettempdir())
/ 'DxProductivity' / 'native-inventory').resolve()
base.mkdir(parents=True, exist_ok=True)
workspace = Path(tempfile.mkdtemp(prefix='dx-cli-', dir=base)).resolve()
desktop = (runtime.native_scope(evidence) if runtime is not None
desktop = (runtime.native_scope(canonical_path(evidence)) if runtime is not None
else self.desktop_factory('dx-cli-' + token, enabled=True))
record = {'backend': 'native_cli', 'project': str(project), 'state': 'reading',
'workspace': str(workspace), 'tools': []}
@@ -115,12 +117,12 @@ class NativeInventoryReader:
source_hashes = {}
try:
source_hashes = {project: _hash(project), database: _hash(database)}
if read_native_text(project) != text:
if read_native_text(internal_path(project)) != text:
raise TaskCenterError('target_identity', '创建原生采集快照时工程配置已变化,请重新导入')
source = workspace / 'input.prj'
target_database = workspace / 'database'
target_database.mkdir()
shutil.copyfile(database, target_database / 'icdb.dat')
shutil.copyfile(internal_path(database), target_database / 'icdb.dat')
# No server address files or running database directory are copied.
text = re.sub(r'^KEY iCDBDir "[^"\r\n]*"\s*$',
'KEY iCDBDir "database"', text, flags=re.MULTILINE)
@@ -145,17 +147,21 @@ class NativeInventoryReader:
self._run_tool(desktop, command, workspace, environment, output,
record, stop_event)
finally:
primary_error = sys.exception()
try:
desktop.close()
clean = True
except Exception as error:
record.update(state='uncertain', cleanup_error=str(error))
record.update(state='uncertain', cleanup_error=str(error),
primary_error=str(primary_error) if primary_error is not None else None)
try:
(evidence / 'uncertain.flag').write_text(str(error), encoding='utf-8')
except OSError as marker_error:
record['uncertain_marker_error'] = str(marker_error)
raise TaskCenterError('inventory_cleanup',
f'原生采集进程未能确认安全释放,请保留诊断记录:{evidence}') from error
failure = TaskCenterError('inventory_cleanup',
f'原生采集进程未能确认安全释放,请保留诊断记录:{canonical_path(evidence)}')
failure.primary_error, failure.cleanup_error = primary_error, error
raise failure from error
_verify_sources(source_hashes)
check_cancelled(stop_event)
result = read_ascii_discovery(project, output / 'scope.txt', old_part_numbers,
@@ -178,7 +184,7 @@ class NativeInventoryReader:
write_record(evidence / 'native-retry-proof.json', {
'version': 1, 'native_read_only': True, 'project_path': str(project),
'source_hashes': {str(path): digest for path, digest in source_hashes.items()},
'workers_closed': True, 'evidence_root': str(evidence.resolve()),
'workers_closed': True, 'evidence_root': str(canonical_path(evidence)),
'binding': dict(desktop.identity),
})
_verify_sources(source_hashes)
@@ -248,11 +254,15 @@ class NativeInventoryReader:
if metric['exit_code'] != 0:
raise NativeInventoryUnsupported(f'{name} 返回退出码 {metric["exit_code"]}')
finally:
primary_error = sys.exception()
metric['seconds'] = time.monotonic() - started
if process is not None and callable(getattr(process, 'close', None)):
try:
process.close()
except Exception as error:
record.update(state='uncertain', cleanup_error=str(error))
raise TaskCenterError('inventory_cleanup',
f'原生读取进程及其子进程未能确认退出:{error}') from error
record.update(state='uncertain', cleanup_error=str(error),
primary_error=str(primary_error) if primary_error is not None else None)
failure = TaskCenterError('inventory_cleanup',
f'原生读取进程及其子进程未能确认退出:{error}')
failure.primary_error, failure.cleanup_error = primary_error, error
raise failure from error
@@ -0,0 +1,62 @@
"""Filesystem access paths never replace canonical project/resource identities."""
from __future__ import annotations
import ntpath
import os
from pathlib import Path
import time
def strip_extended_prefix(value):
value = os.fspath(value)
if value.startswith('\\\\?\\UNC\\'):
return '\\\\' + value[8:]
if value.startswith('\\\\?\\'):
return value[4:]
return value
def windows_extended_path(value):
value = os.fspath(value)
if value.startswith('\\\\?\\'):
return value
value = ntpath.abspath(value)
return '\\\\?\\UNC\\' + value[2:] if value.startswith('\\\\') else '\\\\?\\' + value
def canonical_path(value, *, strict=False):
return Path(strip_extended_prefix(internal_path(value).resolve(strict=strict)))
def internal_path(value):
"""Use Unicode extended Windows paths only at internal filesystem boundaries."""
return Path(windows_extended_path(value)) if os.name == 'nt' else Path(value)
def retry_sharing_violation(operation, *, sleep=time.sleep):
"""At most three attempts and 150ms delay; access denial is never replayed."""
for attempt in range(3):
try:
return operation()
except OSError as error:
if getattr(error, 'winerror', None) not in (32, 33) or attempt == 2:
raise
sleep(.05 * (attempt + 1))
def legacy_path(value):
"""Give CAD/WSH a short entry while retaining canonical identity elsewhere."""
from .legacy_paths import _aliases, _fits, _short_path
path = canonical_path(value, strict=True)
if os.name != 'nt' or (_fits(path.parent) and len(str(path)) < 260):
return path
short = _short_path(internal_path(path))
if short and _fits(Path(short).parent) and len(short) < 260:
access = Path(short)
elif path.is_dir():
access = _aliases.alias(path)
else:
access = _aliases.alias(path.parent) / path.name
if canonical_path(access, strict=True) != path:
raise OSError('compatibility entry does not identify the canonical path')
return access
@@ -10,6 +10,7 @@ import subprocess
from uuid import uuid4
from .errors import TaskCenterError
from .path_access import canonical_path, internal_path, retry_sharing_violation
from .quick_run_protocol import ProjectDiscovery, _encode_vbs_field
from .operation_control import check_cancelled, report_progress
@@ -64,13 +65,13 @@ def discover_project(
) -> ProjectDiscovery:
"""Enumerate sheets and requested source parts while the owned project is open."""
check_cancelled(stop_event)
project = project_path.resolve(strict=True)
project = canonical_path(internal_path(project_path), strict=True)
token = uuid4().hex
job_id = "discover_" + token
root = data_root / "quick-run-history" / ("sheet-discovery-" + token)
root.mkdir(parents=True, exist_ok=False)
root = internal_path(data_root / "quick-run-history" / ("sheet-discovery-" + token))
retry_sharing_violation(lambda: root.mkdir(parents=True, exist_ok=False))
script = root / "QuickPartReplace.vbs"
shutil.copyfile(script_source, script)
shutil.copyfile(internal_path(script_source), script)
(root / "command.tsv").write_text(
"task_id\tjob_id\tproject_path\told_part\tnew_part\tvalue_mode\r\n"
f"sheet_discovery\t{job_id}\t{project}\tDISCOVERY_OLD\tDISCOVERY_NEW\tpreserve\r\n",
@@ -93,7 +94,7 @@ def discover_project(
)
if collect_components:
(root / "component-inventory.request").write_text(job_id, encoding="ascii")
script_argument, root_argument = runtime.worker_paths(script, root)
script_argument, root_argument = runtime.worker_paths(canonical_path(script), canonical_path(root))
command = (
str(runtime.cscript_resolver()),
"//nologo",
+36
View File
@@ -161,3 +161,39 @@ def test_native_failure_does_not_reuse_previous_attempt_transaction(prepared, tm
assert report['stage'] == 'preflight' and 'transaction' not in report
assert report['batch_attempt']['job_id'] == 'job-a'
assert attempts.read_attempt(path)['state'] == 'prepared'
def test_attempt_write_handles_long_unicode_evidence_and_keeps_canonical_identity(tmp_path, monkeypatch):
from pathlib import Path
directory = tmp_path / ('long-' + 'x' * 110) / ('more-' + 'y' * 80)
path = directory / 'attempt.json'
real_mkdir = Path.mkdir
def strict_mkdir(target, *args, **kwargs):
if len(str(target)) >= 260 and not str(target).startswith(chr(92) * 2 + '?'):
raise OSError('legacy MAX_PATH')
return real_mkdir(target, *args, **kwargs)
monkeypatch.setattr(Path, 'mkdir', strict_mkdir)
project = tmp_path / 'board.prj'
record = attempts.prepare_attempt(path, session_id='session', generation=1,
task_id='task', job_id='job', project_path=project)
assert record['project_path'] == os.path.normcase(str(project.resolve()))
assert attempts.can_retry_attempt(path, record)
assert not str(record['project_path']).startswith(chr(92) * 2 + '?')
def test_publication_retries_sharing_violation_only_and_keeps_original_record(tmp_path, monkeypatch):
path = tmp_path / 'attempt.json'
first = attempts.prepare_attempt(path, session_id='session', generation=1,
task_id='task', job_id='job', project_path=tmp_path / 'board.prj')
calls = []
def fail(source, target):
calls.append((source, target))
error = PermissionError('sharing violation')
error.winerror = 32
raise error
monkeypatch.setattr(os, 'replace', fail)
with pytest.raises(PermissionError):
attempts.prepare_attempt(path, session_id='session', generation=2,
task_id='task', job_id='job', project_path=tmp_path / 'board.prj')
assert len(calls) == 3
assert attempts.read_attempt(path) == first
assert list(tmp_path.iterdir()) == [path]
+47 -2
View File
@@ -199,7 +199,7 @@ def test_possible_mutation_prevents_automatic_replay(tmp_path, monkeypatch):
calls = []
def monitor(*args, **kwargs):
calls.append(1)
path = next((Path(command[3]) / 'batch-attempts').rglob('job.json'))
path = next((runtime.path.parent / 'attempts').rglob('job.json'))
record = json.loads(path.read_text())
record['state'] = 'mutation_possible'
path.write_text(json.dumps(record))
@@ -228,7 +228,7 @@ def test_previous_checkpoint_invalidated_before_new_worker(tmp_path, monkeypatch
raise WorkerMonitorError('cannot certify empty project')
runtime.cad.checkpoint = fail_checkpoint
# No retry after evidence has been marked as possibly modified.
path = next((Path(command[3]) / 'batch-attempts').rglob('job.json'))
path = next((runtime.path.parent / 'attempts').rglob('job.json'))
record = json.loads(path.read_text())
record['state'] = 'mutation_possible'
path.write_text(json.dumps(record))
@@ -247,6 +247,9 @@ def test_attempt_publication_failure_never_launches_worker(tmp_path, monkeypatch
with runtime, pytest.raises(OSError):
runtime.run_worker(command, 5)
assert not Cad.created
assert not Desktop.created
from dx_productivity.trial_task_center.batch_resources import pending_resources
assert pending_resources(runtime.root) == ()
def test_boundary_pause_does_not_abort_current_cad_startup(tmp_path, monkeypatch):
@@ -498,3 +501,45 @@ def test_identity_conflict_never_rebuilds_or_launches_another_worker(tmp_path, m
assert len(workers) == (1 if conflict.startswith('checkpoint') or conflict == 'worker_identity' else 0)
assert runtime.generation == 1 and not runtime.record['retries']
assert Desktop.created[0].released
def test_request_session_identity_is_uuid_bound(tmp_path):
from dx_productivity.trial_task_center.batch_runtime import BatchCadRuntime
session = '12345678-1234-5678-1234-567812345678'
runtime = BatchCadRuntime(tmp_path, 'bom_import', session_id=session)
assert runtime.session_id == session.replace('-', '')
with pytest.raises(ValueError):
BatchCadRuntime(tmp_path, 'bom_import', session_id='../escape')
def test_cleanup_preserves_primary_bom_error_and_report(tmp_path):
from dx_productivity.trial_task_center.batch_runtime import BatchCadRuntime
primary = TaskCenterError('bom_inspection', 'read failed')
primary.bom_report = object()
Desktop.fail_release = True
with pytest.raises(TaskCenterError) as caught:
with BatchCadRuntime(tmp_path, 'bom_import', desktop_factory=Desktop) as runtime:
with runtime.native_scope(tmp_path / 'operation'):
pass
raise primary
error = caught.value
assert error.primary_error is primary
assert error.cleanup_error is not None
assert error.bom_report is primary.bom_report
assert 'BOM' in error.message and '已核验任务结果保留' not in error.message
assert runtime.record['primary_error']['code'] == primary.code
assert runtime.record['cleanup_error']
Desktop.fail_release = False
from dx_productivity.trial_task_center.batch_resources import cleanup_pending_resources
cleanup_pending_resources(tmp_path)
def test_attempts_live_under_short_session_root(tmp_path, monkeypatch):
import subprocess
command, _ = 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)
assert list((runtime.path.parent / 'attempts').rglob('job.json'))
assert not (Path(command[3]) / 'batch-attempts').exists()
@@ -231,6 +231,8 @@ def test_native_cleanup_failure_blocks_fallback_and_keeps_recovery_evidence(tmp_
with pytest.raises(TaskCenterError) as caught:
reader.read(project, ('OLD',), stop_event=stop)
assert caught.value.code == 'inventory_cleanup'
assert caught.value.primary_error.code == 'operation_cancelled'
assert isinstance(caught.value.cleanup_error, OSError)
assert released == [True] and list((tmp_path / 'evidence').rglob('uncertain.flag'))
assert list((tmp_path / 'temporary').iterdir())
+70
View File
@@ -0,0 +1,70 @@
import pytest
def test_windows_extended_paths_keep_canonical_identity():
from dx_productivity.trial_task_center.path_access import windows_extended_path, strip_extended_prefix
slash = chr(92)
local = 'C:' + slash + ('nested' + slash) * 45 + 'file.json'
assert windows_extended_path(local) == slash * 2 + '?' + slash + local
assert strip_extended_prefix(windows_extended_path(local)) == local
unc = slash * 2 + 'server' + slash + 'share' + slash + 'file.json'
assert windows_extended_path(unc) == slash * 2 + '?' + slash + 'UNC' + unc[1:]
assert strip_extended_prefix(windows_extended_path(unc)) == unc
def test_sharing_retry_is_bounded_and_does_not_retry_permission_denied():
from dx_productivity.trial_task_center.path_access import retry_sharing_violation
calls = []
def operation():
calls.append(1)
error = PermissionError('sharing violation')
error.winerror = 32
raise error
with pytest.raises(PermissionError):
retry_sharing_violation(operation, sleep=lambda delay: None)
assert len(calls) == 3
calls.clear()
def denied():
calls.append(1)
raise PermissionError('access denied')
with pytest.raises(PermissionError):
retry_sharing_violation(denied, sleep=lambda delay: None)
assert len(calls) == 1
def test_discovery_uses_extended_filesystem_access_before_legacy_launch(tmp_path, monkeypatch):
import os
import subprocess
from pathlib import Path
from dx_productivity.trial_task_center.project_discovery import discover_project, DiscoveryRuntime
if os.name != 'nt':
pytest.skip('Windows filesystem boundary')
data = tmp_path / ('nested-' + 'a' * 90) / ('more-' + 'b' * 70)
project = tmp_path / 'board.prj'
project.write_text('project')
script = tmp_path / 'worker.vbs'
script.write_text('script')
real_mkdir = Path.mkdir
def strict_mkdir(path, *args, **kwargs):
if len(str(path)) >= 260 and not str(path).startswith(chr(92) * 2 + '?'):
raise OSError('legacy MAX_PATH filesystem access')
return real_mkdir(path, *args, **kwargs)
monkeypatch.setattr(Path, 'mkdir', strict_mkdir)
reached = []
def launch(*args):
reached.append(1)
raise subprocess.SubprocessError('stop at launch')
runtime = DiscoveryRuntime(launch, lambda: Path('cscript'), lambda s, r: (str(s), str(r)),
lambda: (), str, lambda *a: (), lambda *a: {}, 5)
with pytest.raises(Exception):
discover_project(project, ('OLD',), data_root=data, script_source=script, runtime=runtime)
assert reached, 'long-path filesystem preparation failed before worker boundary'
def test_native_short_path_never_passes_extended_prefix_to_wsh(tmp_path):
import os
from dx_productivity.trial_task_center.legacy_paths import _short_path
if os.name != 'nt':
pytest.skip('Windows short-path boundary')
result = _short_path(tmp_path)
if result is None:
pytest.skip('filesystem does not provide short paths')
assert not result.startswith(chr(92) * 2 + '?')