import json from dx_productivity.dsh.native_runtime import launch_environment, write_context from dx_productivity.dsh.snapshot import TaskSnapshot def test_context_publication_has_revision_and_no_unrelated_progress_fields(tmp_path): value = write_context(tmp_path, TaskSnapshot('t', 'paused', {'completed': 2, 'api_key': 'secret'})) assert value['revision'] assert 'secret' not in json.dumps(value) assert json.loads((tmp_path / 'task-context.json').read_text(encoding='utf8')) == value def test_launch_does_not_inherit_unrelated_credentials_or_user_harness_home(tmp_path, monkeypatch): monkeypatch.setenv('OPENAI_API_KEY', 'unrelated-secret') monkeypatch.setenv('DSH_HOME', 'unrelated-home') env = launch_environment(tmp_path) assert 'OPENAI_API_KEY' not in env assert 'DSH_HOME' not in env assert env['DX_DSH_HOME'].startswith(str(tmp_path)) def test_context_publication_survives_a_real_windows_reader_lock(tmp_path): import ctypes from ctypes import wintypes from threading import Timer write_context(tmp_path, TaskSnapshot('t', 'draft')) kernel = ctypes.WinDLL('kernel32', use_last_error=True) kernel.CreateFileW.argtypes = (wintypes.LPCWSTR, wintypes.DWORD, wintypes.DWORD, ctypes.c_void_p, wintypes.DWORD, wintypes.DWORD, wintypes.HANDLE) kernel.CreateFileW.restype = wintypes.HANDLE kernel.CloseHandle.argtypes = (wintypes.HANDLE,) handle = kernel.CreateFileW(str(tmp_path / 'task-context.json'), 0x80000000, 3, None, 3, 0, None) assert handle != ctypes.c_void_p(-1).value release = Timer(.03, lambda: kernel.CloseHandle(handle)) release.start() try: value = write_context(tmp_path, TaskSnapshot('t', 'failed', issue_code='controlled')) assert json.loads((tmp_path / 'task-context.json').read_text())['revision'] == value['revision'] assert not list(tmp_path.glob('context-*.tmp')) finally: release.join()