Files
SkillFactory/tests/test_contact_cycle_sim_native_monitor.py
chenlin 9fe9d27865 feat: checkpoint contact-cycle evaluation and RootXY PPO progress
Consolidate next-stage docs, archive superseded narratives, and preserve frozen document inputs. Retain invalid P2 pairs, partial RootXY training, failed attempts and nonselectable checkpoints as explicit limitations.

Include supervised evaluation/training, desktop GPU admission, bounded evidence and atomic-publication monitor repairs with CPU regressions. Ignore local dotenv configuration.

Validation: 1403 passed, 6 skipped for contact-cycle and related contracts/CLI tests. Full-suite legacy ABI and registration tests require Python 3.12/Torch 2.10/pxr; no runtime checks relaxed. No new physical run or accepted skill.
2026-10-15 14:18:17 +08:00

590 lines
27 KiB
Python

"""CPU-only native coverage tests on synthetic metadata and owned temporary trees."""
import errno
import importlib.util
import os
import stat
from pathlib import Path
from types import SimpleNamespace as NS
import pytest
ROOT = Path(__file__).resolve().parents[1]
spec = importlib.util.spec_from_file_location('native_monitor_test', ROOT / 'scripts/evaluate_cycle_sim_residual.py')
r = importlib.util.module_from_spec(spec)
spec.loader.exec_module(r)
def permission_case(tmp_path, monkeypatch, *, owner=None, code=errno.EACCES, access=False, recheck=None):
"""Never chmod, chown or inspect another user's tree; model the OS denial."""
denied = tmp_path / 'unrelated-private'
denied.mkdir()
(tmp_path / 'visible').write_bytes(b'12345')
real_stat, real_open, real_access = r.os.stat, r.os.open, r.os.access
calls = NS(stats=0, opens=0, access=0)
owner = os.geteuid() + 1 if owner is None else owner
def metadata(path, *args, **kwargs):
info = real_stat(path, *args, **kwargs)
if path == denied.name and kwargs.get('dir_fd') is not None:
calls.stats += 1
values = {k: getattr(info, k) for k in dir(info) if k.startswith('st_')}
values.update(st_uid=owner, st_mode=stat.S_IFDIR | 0o700)
if calls.stats > 1 and recheck:
recheck(values, calls.stats)
return NS(**values)
return info
def opening(path, flags, *args, **kwargs):
assert flags & os.O_NOFOLLOW
if path == denied.name and kwargs.get('dir_fd') is not None:
calls.opens += 1
raise OSError(code, 'injected directory operation failure', str(denied))
return real_open(path, flags, *args, **kwargs)
def accessible(path, *args, **kwargs):
if path == denied.name and kwargs.get('dir_fd') is not None:
assert kwargs['effective_ids'] and not kwargs['follow_symlinks']
calls.access += 1
return access
return real_access(path, *args, **kwargs)
monkeypatch.setattr(r.os, 'stat', metadata)
monkeypatch.setattr(r.os, 'open', opening)
monkeypatch.setattr(r.os, 'access', accessible)
return denied, calls
@pytest.mark.parametrize('code', [errno.EACCES, errno.EPERM])
def test_native_size_shared_unrelated_permission_gap_exact_evidence(tmp_path, monkeypatch, code):
denied, calls = permission_case(tmp_path, monkeypatch, code=code)
result = r.native_size(tmp_path, shared=True)
assert result['logical_bytes'] == 5 and result['skipped_count'] == 1
assert result['permission_gaps'] == [{
'path': str(denied), 'errno': code, 'operation': 'open',
'uid': os.geteuid() + 1, 'gid': denied.lstat().st_gid,
'mode': stat.S_IFDIR | 0o700, 'dev': denied.lstat().st_dev,
'ino': denied.lstat().st_ino, 'ctime_ns': denied.lstat().st_ctime_ns}]
assert calls.stats == 3 and calls.opens == 1 and calls.access == 1
assert result['free_space']['observed_path'] == str(tmp_path)
assert result['free_space']['free_bytes'] > 0
@pytest.mark.parametrize('protection', ['owned_uid', 'strict_root', 'task_cache', 'strict_descendant', 'not_shared'])
def test_native_size_owned_or_task_owned_paths_never_excluded(tmp_path, monkeypatch, protection):
denied, _ = permission_case(tmp_path, monkeypatch,
owner=os.geteuid() if protection == 'owned_uid' else None)
strict = {'strict_root': [tmp_path], 'task_cache': [denied],
'strict_descendant': [denied / 'task-cache']}.get(protection, [])
with pytest.raises(r.NativeScanError) as caught:
r.native_size(tmp_path, shared=protection != 'not_shared', strict_paths=strict)
failure = caught.value.report['failure']
assert failure['path'] == str(denied) and failure['errno'] == errno.EACCES
assert failure['metadata']['mode'] == stat.S_IFDIR | 0o700
assert caught.value.report['skipped_count'] == 0
@pytest.mark.parametrize('code', [errno.EIO, errno.ESTALE, errno.EMFILE, errno.ELOOP])
def test_native_size_other_io_errors_fatal(tmp_path, monkeypatch, code):
denied, _ = permission_case(tmp_path, monkeypatch, code=code)
with pytest.raises(r.NativeScanError) as caught:
r.native_size(tmp_path, shared=True)
assert caught.value.report['failure']['path'] == str(denied)
assert caught.value.report['failure']['errno'] == code
assert caught.value.report['skipped_count'] == 0
@pytest.mark.parametrize('change', ['unknown', 'disappeared', 'owner', 'inode', 'mode', 'ctime', 'access'])
def test_native_size_permission_metadata_races_fail_closed(tmp_path, monkeypatch, change):
def recheck(values, count):
if change in ('unknown', 'disappeared'):
code = errno.EACCES if change == 'unknown' else errno.ENOENT
raise OSError(code, 'injected metadata unavailable')
field = {'owner': 'st_uid', 'inode': 'st_ino', 'mode': 'st_mode', 'ctime': 'st_ctime_ns'}.get(change)
if field:
values[field] += 1
denied, _ = permission_case(tmp_path, monkeypatch, access=change == 'access', recheck=recheck)
with pytest.raises(r.NativeScanError) as caught:
r.native_size(tmp_path, shared=True)
assert caught.value.report['failure']['path'] == str(denied)
assert caught.value.report['skipped_count'] == 0
def test_native_size_unknown_initial_owner_is_fatal(tmp_path, monkeypatch):
denied = tmp_path / 'unknown'
denied.mkdir()
real_stat = r.os.stat
def unknown(path, *args, **kwargs):
if path == denied.name and kwargs.get('dir_fd') is not None:
raise PermissionError(errno.EACCES, 'metadata unavailable', str(denied))
return real_stat(path, *args, **kwargs)
monkeypatch.setattr(r.os, 'stat', unknown)
with pytest.raises(r.NativeScanError) as caught:
r.native_size(tmp_path, shared=True)
assert caught.value.report['failure']['metadata'] is None
assert caught.value.report['failure']['path'] == str(denied)
def test_native_size_disappearance_is_recorded_except_required_root(tmp_path, monkeypatch):
(tmp_path / 'vanished').write_text('gone')
real_stat = r.os.stat
def missing(path, *args, **kwargs):
if path == 'vanished' and kwargs.get('dir_fd') is not None:
raise FileNotFoundError(errno.ENOENT, 'injected churn')
return real_stat(path, *args, **kwargs)
monkeypatch.setattr(r.os, 'stat', missing)
result = r.native_size(tmp_path)
assert result['logical_bytes'] == 0
assert result['disappeared_paths'] == [str(tmp_path / 'vanished')]
assert r.native_size(tmp_path / 'missing')['disappeared_paths'] == [str(tmp_path / 'missing')]
with pytest.raises(r.NativeScanError, match='required_directory'):
r.native_size(tmp_path / 'missing', required=True)
@pytest.mark.parametrize('where', ['root', 'ancestor', 'owned_child', 'shared_child'])
def test_native_size_never_follows_symlinks(tmp_path, where):
target = tmp_path / 'target'
target.mkdir()
(target / 'data').write_bytes(b'not counted')
scanned = tmp_path / 'scanned'
scanned.mkdir()
link = scanned / 'link'
link.symlink_to(target, target_is_directory=True)
if where == 'shared_child':
result = r.native_size(scanned, shared=True)
assert result['logical_bytes'] == 0 and result['symlink_paths'] == [str(link)]
else:
path = {'root': link, 'ancestor': link / 'nested', 'owned_child': scanned}[where]
with pytest.raises(r.NativeScanError, match='symlink'):
r.native_size(path, strict_paths=[scanned] if where == 'owned_child' else [])
def test_native_size_directory_replaced_between_stat_and_open_fails(tmp_path, monkeypatch):
child = tmp_path / 'child'
child.mkdir()
replacement = tmp_path / 'replacement'
replacement.mkdir()
real_open = r.os.open
def raced(path, flags, *args, **kwargs):
if path == child.name and kwargs.get('dir_fd') is not None:
return real_open(replacement, flags)
return real_open(path, flags, *args, **kwargs)
monkeypatch.setattr(r.os, 'open', raced)
with pytest.raises(r.NativeScanError, match='open_identity_changed'):
r.native_size(child, shared=True)
def local_policy(root, shared):
return {'artifact_root': str(root), 'native_paths': [str(root / 'native'), str(shared)],
'shared_native_paths': [str(shared)], 'task_owned_cache_paths': [str(root / 'native/cache')],
'free_space_paths': [str(root), str(shared.parent)],
'free_space_mount_identities': {str(p): r.mount_identity(p) for p in (root, shared.parent)},
'mount_identity': r.mount_identity(root), 'min_free_bytes': 1,
'check_interval_seconds': 1, 'max_native_growth_bytes': 100}
def test_native_monitor_disjoint_baseline_growth_outputs_and_gap_history(tmp_path, monkeypatch):
# Campaign nested in a shared root: no duplicate native/output growth charges.
root = tmp_path / 'campaign'
(root / 'native/cache').mkdir(parents=True)
(root / 'native/cache/data').write_bytes(b'123')
(root / 'report').write_bytes(b'output')
denied, _ = permission_case(tmp_path, monkeypatch)
policy = local_policy(root, tmp_path)
clock = NS(now=10)
budget = NS(boottime=lambda: clock.now, STORAGE_SCOPE='test-output-only')
monitor = r.NativeMonitor(policy, budget)
before = monitor.report()
assert monitor.baseline == {str(root / 'native'): 3, str(tmp_path): 5}
assert before['latest_scans'][str(root)]['logical_bytes'] == 6
assert before['baseline_scans'] == before['latest_scans']
assert before['permission_gap_history'][0]['new'][0]['path'] == str(denied)
assert not before['coverage_complete'] and not before['native_hard_cap']
assert before['runtime_uid'] == os.geteuid()
(root / 'report').write_bytes(b'longer output does not add native growth')
(root / 'native/cache/data').write_bytes(b'1234567')
monitor.check() # interval not reached
assert monitor.latest == monitor.baseline
clock.now = 11
monitor.check()
report = monitor.report()
assert report['observed_positive_growth_bytes'] == 4
assert report['baseline_logical_bytes'] == before['baseline_logical_bytes']
assert report['latest_logical_bytes'][str(root / 'native')] == 7
assert report['permission_gap_history'][-1]['new'] == []
assert len(before['permission_gap_history']) == 1 # immutable published snapshot
denied.rmdir()
clock.now = 12
monitor.check()
assert monitor.report()['permission_gap_history'][-1]['no_longer_observed'][0]['path'] == str(denied)
assert monitor.report()['coverage_complete'] is False
def test_native_monitor_new_and_changed_gap_are_not_silent(tmp_path, monkeypatch):
root = tmp_path / 'campaign'
(root / 'native').mkdir(parents=True)
policy = local_policy(root, tmp_path)
monitor = r.NativeMonitor(policy, NS(boottime=lambda: 0, STORAGE_SCOPE='test'))
denied, _ = permission_case(tmp_path, monkeypatch)
monitor.check(force=True)
assert monitor.report()['permission_gap_history'][-1]['new'][0]['path'] == str(denied)
# A replacement with the same name but different stable metadata is fatal.
real_stat = r.os.stat
def changed(path, *args, **kwargs):
info = real_stat(path, *args, **kwargs)
if path == denied.name and kwargs.get('dir_fd') is not None:
info.st_uid += 1
return info
monkeypatch.setattr(r.os, 'stat', changed)
with pytest.raises(RuntimeError, match='gap_identity_changed'):
monitor.check(force=True)
assert monitor.report()['permission_gap_history'][-1]['changed']
def test_native_monitor_owned_output_denial_fatal_with_partial_report(tmp_path, monkeypatch):
denied, _ = permission_case(tmp_path, monkeypatch)
policy = local_policy(tmp_path, tmp_path / 'shared')
with pytest.raises(r.NativeScanError) as caught:
r.NativeMonitor(policy, NS(boottime=lambda: 0, STORAGE_SCOPE='test'))
report = caught.value.report
assert not report['latest_scan_completed']
assert not report['coverage_complete']
assert report['failed_scan']['failure']['path'] == str(denied)
assert report['failed_scan']['free_space']['free_bytes'] > 0
assert report['baseline_logical_bytes'] == {}
@pytest.mark.parametrize('code', [errno.EACCES, errno.EPERM, errno.EIO])
def test_native_size_scandir_denial_uses_directory_metadata(tmp_path, monkeypatch, code):
real_stat, real_fstat, real_scandir, real_access = os.stat, os.fstat, os.scandir, os.access
identity = tmp_path.stat().st_ino
def metadata(info):
if info.st_ino != identity:
return info
values = {k: getattr(info, k) for k in dir(info) if k.startswith('st_')}
values['st_uid'] = os.geteuid() + 1
return NS(**values)
def scanned(fd):
if real_fstat(fd).st_ino == identity:
raise OSError(code, 'injected scandir failure')
return real_scandir(fd)
monkeypatch.setattr(r.os, 'stat', lambda *a, **kw: metadata(real_stat(*a, **kw)))
monkeypatch.setattr(r.os, 'fstat', lambda fd: metadata(real_fstat(fd)))
monkeypatch.setattr(r.os, 'scandir', scanned)
monkeypatch.setattr(r.os, 'access', lambda p, *a, **kw:
False if p == tmp_path.name else real_access(p, *a, **kw))
if code == errno.EIO:
with pytest.raises(r.NativeScanError) as caught:
r.native_size(tmp_path, shared=True)
assert caught.value.report['failure']['errno'] == code
else:
report = r.native_size(tmp_path, shared=True)
assert report['permission_gaps'][0]['path'] == str(tmp_path)
assert report['permission_gaps'][0]['errno'] == code
assert report['logical_bytes'] == 0
@pytest.mark.parametrize('shared', [False, True])
def test_native_access_false_after_atomic_rename_keeps_bytes_not_permission_gap(tmp_path, monkeypatch, shared):
pending = tmp_path / 'counts.pending'
pending.write_bytes(b'x' * 73)
real_access = os.access
def access(name, *args, **kwargs):
if name == 'counts.pending':
pending.rename(tmp_path / 'counts.json')
return False # actual access(ENOENT) after successful earlier lstat
return real_access(name, *args, **kwargs)
monkeypatch.setattr(r.os, 'access', access)
report = r.native_size(tmp_path, shared=shared, strict_paths=[tmp_path])
assert report['logical_bytes'] >= 73 # never silently discard observed bytes
assert report['disappeared_paths'] == [str(pending)]
assert report['permission_gaps'] == [] and report['skipped_count'] == 0
assert 'failure' not in report
def test_native_size_unreadable_owned_file_fails(tmp_path, monkeypatch):
(tmp_path / 'owned').write_text('data')
real_access = os.access
monkeypatch.setattr(r.os, 'access', lambda p, *a, **kw: False if p == 'owned' else real_access(p, *a, **kw))
with pytest.raises(r.NativeScanError) as caught:
r.native_size(tmp_path, shared=True)
assert caught.value.report['failure']['operation'] == 'file_access'
assert caught.value.report['failure']['path'] == str(tmp_path / 'owned')
def test_native_size_free_space_error_fails_with_exact_observed_path(tmp_path, monkeypatch):
monkeypatch.setattr(r.os, 'fstatvfs', lambda _: (_ for _ in ()).throw(OSError(errno.EIO, 'injected')))
with pytest.raises(r.NativeScanError) as caught:
r.native_size(tmp_path, shared=True)
assert caught.value.report['failure']['operation'] == 'free_space'
assert caught.value.report['failure']['path'] == '/'
assert caught.value.report['failure']['errno'] == errno.EIO
def test_native_size_symlink_replacement_at_open_never_followed(tmp_path, monkeypatch):
child = tmp_path / 'child'
child.mkdir()
target = tmp_path / 'target'
target.mkdir()
real_open = os.open
def replace(path, flags, *args, **kwargs):
if path == child.name and kwargs.get('dir_fd') is not None:
child.rmdir()
child.symlink_to(target, target_is_directory=True)
return real_open(path, flags, *args, **kwargs)
monkeypatch.setattr(r.os, 'open', replace)
with pytest.raises(r.NativeScanError) as caught:
r.native_size(child, shared=True)
assert caught.value.report['failure']['errno'] in (errno.ELOOP, errno.ENOTDIR)
assert caught.value.report['skipped_count'] == 0
def test_native_monitor_previously_unrelated_gap_becoming_owned_is_fatal(tmp_path, monkeypatch):
root = tmp_path / 'campaign'
(root / 'native').mkdir(parents=True)
denied, _ = permission_case(tmp_path, monkeypatch)
monitor = r.NativeMonitor(local_policy(root, tmp_path), NS(boottime=lambda: 0, STORAGE_SCOPE='test'))
real_stat = r.os.stat
def newly_owned(path, *args, **kwargs):
info = real_stat(path, *args, **kwargs)
if path == denied.name and kwargs.get('dir_fd') is not None:
info.st_uid = os.geteuid()
return info
monkeypatch.setattr(r.os, 'stat', newly_owned)
with pytest.raises(r.NativeScanError) as caught:
monitor.check(force=True)
report = caught.value.report
assert report['failed_scan']['failure']['metadata']['uid'] == os.geteuid()
assert report['failed_scan']['skipped_count'] == 0
assert report['observed_positive_growth_bytes'] is None
assert report['baseline_scans'][str(tmp_path)]['skipped_count'] == 1
def test_native_monitor_baseline_threshold_failure_retains_observations(tmp_path):
root = tmp_path / 'campaign'
(root / 'native').mkdir(parents=True)
policy = local_policy(root, tmp_path)
policy['min_free_bytes'] = 1 << 100
with pytest.raises(RuntimeError, match='free_space_stop') as caught:
r.NativeMonitor(policy, NS(boottime=lambda: 0, STORAGE_SCOPE='test'))
assert not caught.value.report['latest_scan_completed']
assert caught.value.report['latest_free_space'][str(root)]['free_bytes'] > 0
assert caught.value.report['baseline_logical_bytes'] == {}
@pytest.mark.parametrize('change', ['unavailable', 'ownership', 'disappeared'])
def test_native_size_post_traversal_metadata_never_becomes_permission_gap(tmp_path, monkeypatch, change):
real_stat = os.stat
calls = 0
def changed(path, *args, **kwargs):
nonlocal calls
info = real_stat(path, *args, **kwargs)
if path == tmp_path.name and kwargs.get('dir_fd') is not None:
calls += 1
if calls > 1:
if change != 'ownership':
code = errno.EACCES if change == 'unavailable' else errno.ENOENT
raise OSError(code, 'injected post-traversal metadata failure')
values = {k: getattr(info, k) for k in dir(info) if k.startswith('st_')}
values['st_uid'] += 1
return NS(**values)
return info
monkeypatch.setattr(r.os, 'stat', changed)
with pytest.raises(r.NativeScanError) as caught:
r.native_size(tmp_path, shared=True)
assert caught.value.report['skipped_count'] == 0
assert caught.value.report['failure']['path'] == str(tmp_path)
expected = 'traversal_identity_changed' if change == 'ownership' else 'traversal_lstat'
assert caught.value.report['failure']['operation'] == expected
def test_native_size_directory_disappearing_at_open_fails(tmp_path, monkeypatch):
permission_case(tmp_path, monkeypatch, code=errno.ENOENT)
with pytest.raises(r.NativeScanError) as caught:
r.native_size(tmp_path, shared=True)
assert caught.value.report['failure']['errno'] == errno.ENOENT
assert caught.value.report['skipped_count'] == 0
@pytest.mark.parametrize('kind', ['file', 'symlink'])
def test_native_size_task_directory_alias_or_nondirectory_is_fatal(tmp_path, kind):
entry = tmp_path / 'entry'
if kind == 'file':
entry.write_text('not a directory')
else:
entry.symlink_to(tmp_path / 'not-followed')
with pytest.raises(r.NativeScanError):
r.native_size(tmp_path, shared=True, strict_paths=[entry / 'task-owned-cache'])
def test_native_size_closes_all_descriptors_after_gap_and_failure(tmp_path, monkeypatch):
permission_case(tmp_path, monkeypatch)
real_open, real_close = r.os.open, r.os.close
opened, closed = [], []
def opening(*args, **kwargs):
fd = real_open(*args, **kwargs)
opened.append(fd)
return fd
def closing(fd):
closed.append(fd)
return real_close(fd)
monkeypatch.setattr(r.os, 'open', opening)
monkeypatch.setattr(r.os, 'close', closing)
r.native_size(tmp_path, shared=True)
with pytest.raises(r.NativeScanError):
r.native_size(tmp_path, shared=False)
assert sorted(opened) == sorted(closed)
def v4_fixture(tmp_path, monkeypatch):
home, sim, system_tmp, root = [tmp_path / name for name in ('home', 'sim', 'system-tmp', 'campaign')]
for path in (home, sim, system_tmp, root):
path.mkdir()
monkeypatch.setattr(Path, 'home', classmethod(lambda cls: home))
monkeypatch.setattr(r, 'SIM_ROOT', sim)
policy = local_policy(root, home)
policy.update(native_paths=r.native_paths(root), shared_native_paths=r.shared_native_paths(),
task_owned_cache_paths=r.task_owned_cache_paths(root), coverage_gaps=list(r.NATIVE_GAPS),
free_space_paths=list(map(str, (root, home, sim, system_tmp))),
free_space_mount_identities={str(p): r.mount_identity(p) for p in (root, home, sim, system_tmp)})
return policy, home, sim, system_tmp, root
def test_native_monitor_v4_never_traverses_chrome_or_system_tmp_but_measures_writers(tmp_path, monkeypatch):
policy, home, sim, system_tmp, root = v4_fixture(tmp_path, monkeypatch)
unrelated = [home / '.cache/google-chrome/Default/Cache', system_tmp / 'private-system-job']
for path in unrelated:
path.mkdir(parents=True)
(path / 'not-task-evidence').write_bytes(b'x' * 1000)
for path in map(Path, policy['native_paths']):
path.mkdir(parents=True)
(path / 'writer').write_bytes(b'123')
(root / 'report').write_bytes(b'output only')
real_scandir = os.scandir
traversed = []
def scanned(fd):
path = Path(os.readlink(f'/proc/self/fd/{fd}'))
traversed.append(path)
assert path not in (home, home / '.cache', system_tmp)
assert not any(path.is_relative_to(p) for p in unrelated)
return real_scandir(fd)
monkeypatch.setattr(r.os, 'scandir', scanned)
monitor = r.NativeMonitor(policy, NS(boottime=lambda: 0, STORAGE_SCOPE='test'))
assert set(monitor.baseline) == set(policy['native_paths'])
assert set(monitor.baseline.values()) == {3}
assert monitor.latest_scans[str(root)]['logical_bytes'] == len(b'output only')
assert set(monitor.report()['latest_free_space']) == set(policy['free_space_paths'])
assert all(value['free_bytes'] > 0 for value in monitor.latest_free_space.values())
assert all(Path(p) in traversed for p in policy['native_paths'])
(sim / 'kit/data/writer').write_bytes(b'1234567')
for path in unrelated:
(path / 'not-task-evidence').write_bytes(b'x' * 2000)
monitor.check(force=True)
assert monitor.growth == 4 # no output/free-only/unknown-writer byte attribution
assert not monitor.report()['coverage_complete']
assert any('unlisted/global cache writers' in gap for gap in policy['coverage_gaps'])
assert any('runtime confirmation' in gap for gap in policy['coverage_gaps'])
def test_native_monitor_v4_shared_tmp_free_floor_stops_without_traversal(tmp_path, monkeypatch):
policy, _, _, system_tmp, _ = v4_fixture(tmp_path, monkeypatch)
real_usage = os.fstatvfs
def usage(fd):
if os.fstat(fd).st_ino == system_tmp.stat().st_ino:
return NS(f_bavail=0, f_frsize=4096)
return real_usage(fd)
monkeypatch.setattr(r.os, 'fstatvfs', usage)
monkeypatch.setattr(r.os, 'scandir', lambda *_: pytest.fail('free floor must stop before recursive work'))
with pytest.raises(RuntimeError, match='free_space_stop') as caught:
r.NativeMonitor(policy, NS(boottime=lambda: 0, STORAGE_SCOPE='test'))
report = caught.value.report
assert report['latest_free_space'][str(system_tmp)]['free_bytes'] == 0
assert report['baseline_logical_bytes'] == {} and report['observed_positive_growth_bytes'] is None
def test_native_monitor_v4_missing_candidates_are_absent_not_created(tmp_path, monkeypatch):
policy, _, sim, _, _ = v4_fixture(tmp_path, monkeypatch)
monitor = r.NativeMonitor(policy, NS(boottime=lambda: 0, STORAGE_SCOPE='test'))
for name in ('data', 'kit/data', 'kit/logs', 'kit/cache'):
path = sim / name
scan = monitor.latest_scans[str(path)]
assert scan['disappeared_paths'] and scan['logical_bytes'] == 0
assert scan['free_space']['observed_path'] == str(sim)
assert not path.exists()
@pytest.mark.parametrize('change', ['alias', 'race', 'mount'])
def test_native_monitor_v4_free_only_path_identity_failures(tmp_path, monkeypatch, change):
policy, _, _, system_tmp, _ = v4_fixture(tmp_path, monkeypatch)
replacement = tmp_path / 'replacement'
replacement.mkdir()
if change == 'alias':
system_tmp.rmdir()
system_tmp.symlink_to(replacement, target_is_directory=True)
elif change == 'race':
real_open = os.open
def raced(path, flags, *args, **kwargs):
if path == system_tmp.name and kwargs.get('dir_fd') is not None:
return real_open(replacement, flags)
return real_open(path, flags, *args, **kwargs)
monkeypatch.setattr(r.os, 'open', raced)
else:
policy['free_space_mount_identities'][str(system_tmp)] = 'wrong mount'
with pytest.raises(RuntimeError, match='symlink|open_identity_changed|free_space_mount_identity_changed'):
r.NativeMonitor(policy, NS(boottime=lambda: 0, STORAGE_SCOPE='test'))
def test_native_monitor_v4_exact_scope_and_actual_environment_redirects(tmp_path):
assert r.shared_native_paths() == sorted([
str(Path.home() / '.cache/ov' / name) for name in ('shaders', 'texturecache', 'Kit/107.3')
] + [str(r.SIM_ROOT / 'kit/cache')])
assert '/tmp' in r.free_space_paths(tmp_path) and '/tmp' not in r.native_paths(tmp_path)
assert str(Path.home() / '.cache') not in r.native_paths(tmp_path)
actual = list(r.native_redirects(tmp_path / 'native/preparation-support', preparation=True).values())
for index in range(3):
actual.extend(r.native_redirects(tmp_path / f'native/launch-{index}').values())
assert sorted(actual) == r.task_owned_cache_paths(tmp_path)
assert all(Path(p).is_relative_to(tmp_path / 'native') for p in actual)
@pytest.mark.parametrize('relative,shared', [('data', False), ('kit/data', False), ('kit/logs', False),
('kit/cache', True)])
def test_native_monitor_v4_installed_candidate_permissions_stay_classified(tmp_path, monkeypatch, relative, shared):
policy, _, sim, _, _ = v4_fixture(tmp_path, monkeypatch)
candidate = sim / relative
candidate.mkdir(parents=True)
denied, _ = permission_case(candidate, monkeypatch)
if shared:
monitor = r.NativeMonitor(policy, NS(boottime=lambda: 0, STORAGE_SCOPE='test'))
assert monitor.latest_scans[str(candidate)]['permission_gaps'][0]['path'] == str(denied)
assert monitor.latest[str(candidate)] == 5
else:
with pytest.raises(r.NativeScanError) as caught:
r.NativeMonitor(policy, NS(boottime=lambda: 0, STORAGE_SCOPE='test'))
assert caught.value.report['failed_scan']['failure']['path'] == str(denied)
assert caught.value.report['failed_scan']['skipped_count'] == 0