Skip to content

Commit 25df197

Browse files
committed
Create inheritance trace privately and exclusively
Held partial candidate: POSIX no-follow exclusive creation uses owner-only mode before writing and retains partial error evidence. Same-user and ancestor races, non-POSIX compatibility, ACLs, crash durability, independent review and experiment design holds remain. Synthetic adapters only; no model or pilot. GitNexus unavailable; no reindex.
1 parent e8ac6e5 commit 25df197

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

‎research/inheritance/experiment.py‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
model output. See README.md for protocol, limitations, and evidence rules.
77
"""
88
import argparse
9+
import contextlib
910
import hashlib
1011
import json
12+
import os
1113
from pathlib import Path
1214
import random
1315
import re
16+
import stat
1417
import subprocess
1518
import time
1619

@@ -34,6 +37,25 @@ def __init__(self, message, evidence):
3437
self.evidence = evidence
3538

3639

40+
@contextlib.contextmanager
41+
def _private_trace_file(path):
42+
"""Create one private trace exclusively; not same-user or ancestor isolation."""
43+
if os.name != 'posix' or not hasattr(os, 'O_NOFOLLOW') or not hasattr(os, 'geteuid'):
44+
raise OSError('private trace creation requires POSIX no-follow support')
45+
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o600)
46+
try:
47+
info = os.fstat(fd)
48+
if (not stat.S_ISREG(info.st_mode) or info.st_nlink != 1 or
49+
info.st_uid != os.geteuid() or info.st_mode & 0o077):
50+
raise OSError('trace is not a private, single-link regular file owned by this user')
51+
with os.fdopen(fd, 'w', encoding='utf-8') as stream:
52+
fd = -1
53+
yield stream
54+
finally:
55+
if fd >= 0:
56+
os.close(fd)
57+
58+
3759
def bounded(value, limit=4000):
3860
if isinstance(value, bytes):
3961
value = value.decode('utf-8', errors='replace')
@@ -216,7 +238,7 @@ def run(config, command, out, metadata, adapter=call_adapter):
216238
raise ValueError('adapter must attest stateless calls')
217239
backend_pins = validate_backend_pins(metadata, config['arms'])
218240
# Exclusive creation prevents accidentally replacing a prior run.
219-
with Path(out).open('x', encoding='utf-8') as stream:
241+
with _private_trace_file(out) as stream:
220242
def emit(row):
221243
stream.write(json.dumps(row, sort_keys=True) + '\n')
222244
stream.flush()

‎research/inheritance/test_experiment.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import unittest
1212
from unittest import mock
1313

14+
import experiment
1415
import ollama_adapter
1516
from ollama_adapter import payload
1617
from experiment import (call_adapter, digest, gate, request_for, require_backend_observation,
@@ -84,6 +85,44 @@ def fail_adapter(*args):
8485
self.assertEqual(stat.S_IMODE(path.stat().st_mode) & 0o077, 0)
8586
self.assertEqual(json.loads(path.read_text().splitlines()[-1])['kind'], 'error')
8687

88+
@unittest.skipUnless(os.name == 'posix', 'POSIX private file-mode contract')
89+
def test_trace_is_private_before_any_stream_write(self):
90+
with tempfile.TemporaryDirectory() as d:
91+
path = Path(d) / 'synthetic.jsonl'
92+
real_fdopen = os.fdopen
93+
modes = []
94+
def inspect(fd, *args, **kwargs):
95+
modes.append(stat.S_IMODE(os.fstat(fd).st_mode))
96+
return real_fdopen(fd, *args, **kwargs)
97+
previous = os.umask(0o022)
98+
try:
99+
with mock.patch.object(experiment.os, 'fdopen', side_effect=inspect):
100+
self.make_run(path)
101+
finally:
102+
os.umask(previous)
103+
self.assertEqual(len(modes), 1)
104+
self.assertEqual(modes[0] & 0o077, 0)
105+
106+
@unittest.skipUnless(os.name == 'posix', 'POSIX no-follow contract')
107+
def test_trace_does_not_replace_or_follow_existing_symlink(self):
108+
with tempfile.TemporaryDirectory() as d:
109+
target = Path(d) / 'other.txt'
110+
target.write_text('unchanged')
111+
path = Path(d) / 'synthetic.jsonl'
112+
path.symlink_to(target)
113+
with self.assertRaises(OSError):
114+
self.make_run(path)
115+
self.assertEqual(target.read_text(), 'unchanged')
116+
self.assertTrue(path.is_symlink())
117+
118+
def test_unsupported_platform_does_not_create_trace(self):
119+
with tempfile.TemporaryDirectory() as d:
120+
path = Path(d) / 'synthetic.jsonl'
121+
with mock.patch.object(experiment.os, 'name', 'nt'):
122+
with self.assertRaises(OSError):
123+
self.make_run(path)
124+
self.assertFalse(path.exists())
125+
87126
def test_missing_or_changed_backend_observation_cannot_be_scored(self):
88127
cfg = copy.deepcopy(CONFIG)
89128
cfg['cases'] = cfg['cases'][:1]

0 commit comments

Comments
 (0)
⌂ 换网址