Skip to content

Commit c6678fa

Browse files
authored
refactor(harness): close the artifact-primitive edges from six Harness modules into Infra (R2) (#478)
* refactor(harness): close the artifact-primitive edges from six Harness modules into Infra (R2) agent_sessions, candidates, optimization, optimization_session, project and task_protocol take digest, sha256 and file_lock from the new harness/artifact_primitives.py and atomic_write_json from the existing harness/atomic_json.py. The function bodies equal those in infra/artifacts.py, so digests, hashes, JSON bytes, atomic replace and lock semantics are unchanged; infra/artifacts.py stays for its other users. first_wave_report() no longer reports the 12 harness -> model/infra hits. The only remaining blocker is the five runtime resource loaders (R3). Refs #412 Agent-assisted-by: Claude Code / claude-opus-5-5 [effort=xhigh] (author) Signed-off-by: GUOGUO <xwdong1998@163.com> * test(gate): expect only the R3 loader blocker in two more real-tree checks (R2) test_knowledge_closure_is_clean_while_overall_stays_false and test_real_tree_holds_no_forwarder_and_stays_incomplete still pinned the twelve harness -> infra hits that R2 removed. The H200 full suite caught both. They now expect only "runtime resource loaders: 5" and zero harness -> model/infra paths; isolation stays false. Refs #412 Agent-assisted-by: Claude Code / claude-opus-5-5 [effort=xhigh] (author) Signed-off-by: GUOGUO <xwdong1998@163.com> * test(harness): drop the in-lock read that Windows denies; pin the R2 runtime identity A Windows byte-range lock denies a read of the locked byte even from the same process, so reading the lock file inside file_lock() failed on Windows before the contention checks ran. Both contention checks stay, and the sentinel is still checked after release. Also assert that the optimization session's runtime identity covers artifact_primitives.py. Refs #412 Agent-assisted-by: Claude Code / claude-opus-5-5 [effort=xhigh] (author) Signed-off-by: GUOGUO <xwdong1998@163.com> * ci(host): run the R2 Harness primitive and writer nodes on hosted Windows The host-compatibility job ran only the eight plugin/DSH nodes and the installed-wheel check, so no independently verifiable job executed the R2 lock, hash, digest and atomic-writer regressions on Windows, where the file lock is an msvcrt byte range. Add a Windows-only step that runs those nodes and test_agent_sessions.py, fails on any skip, and retains its JUnit result with the other host results. macOS uses the same fcntl lock as the Linux full-check jobs and does not run the step. Refs #412 Agent-assisted-by: Claude Code / claude-opus-5-5 [effort=xhigh] (author) Signed-off-by: GUOGUO <xwdong1998@163.com> --------- Signed-off-by: GUOGUO <xwdong1998@163.com>
1 parent 0311393 commit c6678fa

16 files changed

Lines changed: 228 additions & 47 deletions

‎.github/workflows/ci-ready.yml‎

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,12 @@ jobs:
102102

103103
host-compatibility:
104104
# GitHub-hosted Windows and macOS CPU evidence, separate from the Ubuntu jobs above:
105-
# a pass on Linux is not a pass on these hosts. It runs only the eight plugin and DSH
106-
# nodes that an earlier Windows run recorded as failing (63 passed, 8 failed), then an
107-
# installed-wheel check. A pass here does not rewrite that record. No GPU, remote
108-
# service or credential is used; the DSH node talks to the fake SSH server that its
109-
# own check starts. One host at a time, one pytest process, no full suite.
105+
# a pass on Linux is not a pass on these hosts. It runs the eight plugin and DSH nodes
106+
# that an earlier Windows run recorded as failing (63 passed, 8 failed), on Windows the
107+
# Harness artifact-primitive and writer nodes, then an installed-wheel check. A pass
108+
# here does not rewrite that record. No GPU, remote service or credential is used; the
109+
# DSH node talks to the fake SSH server that its own check starts. One host at a time,
110+
# one pytest process per step, no full suite.
110111
strategy:
111112
fail-fast: false
112113
max-parallel: 1
@@ -172,6 +173,46 @@ jobs:
172173
print(counts, flush=True)
173174
if counts != {"tests": len(nodes), "errors": 0, "failures": 0, "skipped": 0}:
174175
raise SystemExit("every selected host node must run and pass")
176+
- name: Test the Harness artifact-primitive and writer nodes on Windows
177+
# #412 R2: Harness owns copies of the Infra digest, hash, file lock and atomic
178+
# writer. On Windows the lock is an msvcrt byte range that even denies a read of
179+
# the locked byte, which no Linux job can show. macOS uses the same fcntl lock as
180+
# the Linux jobs, so only Windows runs these nodes. Runs even if the step above
181+
# failed, so both results are recorded.
182+
if: ${{ !cancelled() && runner.os == 'Windows' }}
183+
shell: python
184+
env:
185+
RESULTS: ${{ runner.temp }}/host-primitives-results.xml
186+
run: |
187+
import os
188+
import subprocess
189+
import sys
190+
import xml.etree.ElementTree as ET
191+
192+
nodes = [
193+
"tests/test_skill_evaluation.py::test_harness_artifact_primitives_match_infra_bytes_and_semantics",
194+
"tests/test_skill_evaluation.py::test_harness_owned_writes_and_digests_match_infra_bytes",
195+
"tests/test_legacy_run_split.py::test_harness_writer_bytes_equal_the_infra_writer",
196+
"tests/test_legacy_run_split.py::test_harness_writer_rejects_nan_and_leaves_no_file",
197+
"tests/test_legacy_run_split.py::test_sampling_sha256_matches_the_infra_hasher",
198+
"tests/test_optimization_session.py::test_runtime_identity_covers_migrated_process_domain_and_not_removed_executor",
199+
"tests/test_agent_sessions.py",
200+
]
201+
results = os.environ["RESULTS"]
202+
command = [sys.executable, "-m", "pytest", "-vv", "-rs", "-o", "addopts=",
203+
"--junitxml=" + results, *nodes]
204+
status = subprocess.run(
205+
command, cwd=os.environ["GITHUB_WORKSPACE"], check=False
206+
).returncode
207+
if status:
208+
raise SystemExit(status)
209+
# pytest exits 0 with skips, and a skipped node is not a pass.
210+
suite = next(ET.parse(results).getroot().iter("testsuite"))
211+
counts = {name: int(suite.get(name))
212+
for name in ("tests", "errors", "failures", "skipped")}
213+
print(counts, flush=True)
214+
if counts["skipped"]:
215+
raise SystemExit("every selected primitive node must run and pass")
175216
- name: Check a wheel built from the sdist in a fresh environment outside the checkout
176217
# Runs even if a node above failed, so both results are recorded; either failure
177218
# still fails the job.
@@ -245,5 +286,6 @@ jobs:
245286
name: host-compatibility-${{ matrix.os }}
246287
path: |
247288
${{ runner.temp }}/host-compatibility-results.xml
289+
${{ runner.temp }}/host-primitives-results.xml
248290
${{ runner.temp }}/host-wheel.log
249291
retention-days: 30

‎CHANGELOG.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@
44

55
### Changed
66

7+
- Six Harness modules stop importing Infra (Refs #412, R2). `agent_sessions`,
8+
`candidates`, `optimization`, `optimization_session`, `project` and
9+
`task_protocol` take `digest`, `sha256` and `file_lock` from the new
10+
`harness/artifact_primitives.py` and `atomic_write_json` from the existing
11+
`harness/atomic_json.py`. The function bodies equal those in
12+
`infra/artifacts.py`, so digests, file hashes, JSON bytes, atomic replace and
13+
lock files are unchanged, and a Harness lock and an Infra lock on the same
14+
path still exclude each other. `infra/artifacts.py` stays for its other users.
15+
- `first_wave_report()` no longer reports `harness reaches model/infra`; it had
16+
12 hits, the package-level `inferrsi.infra` and `inferrsi.infra.artifacts`
17+
edge from each of the six modules. Harness has no `inferrsi.model` edge.
18+
- The optimization session's runtime identity adds
19+
`harness/artifact_primitives.py`. The six edited modules are already in it,
20+
so a session started before this change does not resume after it.
21+
- Isolation is still incomplete: the five runtime resource loaders (R3, #412)
22+
remain.
23+
724
- The local MindIE adapter moves into the Apps package and its four root files
825
are removed (Refs #452, M1), with no forwarding shell, `sys.modules` alias or
926
import fallback:
@@ -39,6 +56,7 @@
3956
- Isolation is still incomplete: the 12 Harness hits into Infra (R2, #412)
4057
and the five runtime resource loaders (R3, #412) remain.
4158

59+
4260
- The campaign chart renderer and the single-task launcher move into the Apps
4361
package and the two root modules are removed (Refs #408, A2), with no
4462
forwarding shell, `sys.modules` alias or import fallback. Both files move

‎docs/module-layout.md‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,23 @@ present; or the scan reports a violation. The three packages may appear in stage
226226
A clean graph for the new cores is reported separately as "new core business
227227
closure isolated" and never sets overall `isolation_complete` to true.
228228

229-
On the tree after A1 (#408), `first_wave_report()` has no violation and two
230-
blockers, so `isolation_complete` is false and A1 does not claim otherwise:
229+
On the tree after R2 (#412), `first_wave_report()` has no violation and one
230+
blocker, so `isolation_complete` is false and R2 does not claim otherwise:
231231

232232
| Blocker | Cause | Closed by |
233233
| --- | --- | --- |
234-
| `harness reaches model/infra` (12 hits) | `agent_sessions`, `candidates`, `optimization`, `optimization_session`, `project` and `task_protocol` each reach `inferrsi.infra` and `inferrsi.infra.artifacts` | R2 (#412) |
235234
| `runtime resource loaders: 5` | The five declared loaders in the table above | R3 (#412) |
236235

237236
R1 cleared the `allowlist` and `legacy run forwarders` blockers, and A1 cleared
238-
the `temporary leaf importers` blocker that R1 left for root `arguments.py`. The loaders
239-
also keep `new_core_closure_isolated` false.
237+
the `temporary leaf importers` blocker that R1 left for root `arguments.py`. R2
238+
cleared `harness reaches model/infra` (12 hits): `agent_sessions`, `candidates`,
239+
`optimization`, `optimization_session`, `project` and `task_protocol` each reached
240+
`inferrsi.infra` and `inferrsi.infra.artifacts`. They now take `digest`, `sha256`
241+
and `file_lock` from `harness/artifact_primitives.py` and `atomic_write_json` from
242+
`harness/atomic_json.py`, whose function bodies equal those in `infra/artifacts.py`.
243+
`infra/artifacts.py` stays for its other users. No Harness module reaches
244+
`inferrsi.infra` or `inferrsi.model`. The loaders also keep
245+
`new_core_closure_isolated` false.
240246

241247
`knowledge-model-policy` stays the only required status. Until a separate
242248
process decision makes this import gate required, every migration PR attaches

‎src/inferrsi/harness/agent_sessions.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
import uuid
1111
from pathlib import Path
1212

13-
from ..infra.artifacts import atomic_write_json, digest, file_lock, sha256
13+
from .artifact_primitives import digest, file_lock, sha256
14+
from .atomic_json import atomic_write_json
1415
from .usage import normalize_usage
1516

1617
TERMINAL = {"completed", "failed", "cancelled"}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Harness-owned digest, file hash and file lock. Each function body equals the one in
2+
# infra.artifacts, so digests, hashes and lock files are byte-identical and the two locks
3+
# exclude each other on the same path; Harness does not import that module here.
4+
5+
import hashlib
6+
import json
7+
import os
8+
from contextlib import contextmanager
9+
from pathlib import Path
10+
11+
12+
@contextmanager
13+
def file_lock(path: Path):
14+
"""Hold a nonblocking OS file lock, released even if the process crashes."""
15+
with path.open("a+b") as stream:
16+
if os.fstat(stream.fileno()).st_size == 0:
17+
stream.write(b"0")
18+
stream.flush()
19+
stream.seek(0)
20+
if os.name == "nt":
21+
import msvcrt
22+
23+
msvcrt.locking(stream.fileno(), msvcrt.LK_NBLCK, 1)
24+
else:
25+
import fcntl
26+
27+
fcntl.flock(stream.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
28+
try:
29+
yield
30+
finally:
31+
stream.seek(0)
32+
if os.name == "nt":
33+
msvcrt.locking(stream.fileno(), msvcrt.LK_UNLCK, 1)
34+
else:
35+
fcntl.flock(stream.fileno(), fcntl.LOCK_UN)
36+
37+
38+
def digest(value):
39+
return hashlib.sha256(
40+
json.dumps(
41+
value, sort_keys=True, separators=(",", ":"), ensure_ascii=False, allow_nan=False
42+
).encode()
43+
).hexdigest()
44+
45+
46+
def sha256(path):
47+
hasher = hashlib.sha256()
48+
with Path(path).open("rb") as stream:
49+
for block in iter(lambda: stream.read(1024 * 1024), b""):
50+
hasher.update(block)
51+
return hasher.hexdigest()

‎src/inferrsi/harness/candidates.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import stat
55
from pathlib import Path
66

7-
from ..infra.artifacts import digest, sha256
7+
from .artifact_primitives import digest, sha256
88

99

1010
def members(source: Path, includes: list[str] | None = None) -> dict[str, Path]:

‎src/inferrsi/harness/optimization.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
import uuid
88
from pathlib import Path
99

10-
from ..infra.artifacts import atomic_write_json, digest, file_lock, sha256
1110
from . import candidates
11+
from .artifact_primitives import digest, file_lock, sha256
12+
from .atomic_json import atomic_write_json
1213
from .usage import normalize_usage, usage_totals
1314

1415

‎src/inferrsi/harness/optimization_session.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ def _session_assess(root, directory, plan, execution):
5555

5656

5757
def _project_execute(root, directory, plan, deadline):
58+
from inferrsi.harness.atomic_json import atomic_write_json
5859
from inferrsi.harness.candidates import identity, members, retain
5960
from inferrsi.harness.project import inspect_step, steps
6061
from inferrsi.harness.scenarios.external import execute_trial
61-
from inferrsi.infra.artifacts import atomic_write_json
6262

6363
output = directory / "execution"
6464
output.mkdir()
@@ -107,13 +107,13 @@ def _project_execute(root, directory, plan, deadline):
107107

108108

109109
def _runtime_identity():
110-
from inferrsi.infra.artifacts import sha256
110+
from inferrsi.harness.artifact_primitives import sha256
111111

112112
root = Path(str(files("inferrsi")))
113113
names = ("harness/optimization_session.py", "harness/optimization.py",
114114
"harness/local_process.py",
115115
"infra/processes.py", "infra/artifacts.py", "harness/scenarios/external.py",
116-
"harness/atomic_json.py",
116+
"harness/atomic_json.py", "harness/artifact_primitives.py",
117117
"harness/usage.py", "harness/agent_sessions.py", "harness/task_protocol.py",
118118
"harness/candidates.py", "harness/project.py",
119119
"harness/records/knowledge_capture.py", "apps/campaign.py",
@@ -130,9 +130,9 @@ def session_command(args, *, task_source: Path | None = None, knowledge_reader=N
130130
if args.session_action == "start":
131131
import math
132132

133+
from inferrsi.harness.artifact_primitives import sha256
133134
from inferrsi.harness.candidates import identity, retain
134135
from inferrsi.harness.task_protocol import prepare
135-
from inferrsi.infra.artifacts import sha256
136136

137137
if args.rounds < 1 or any(not math.isfinite(value) or value <= 0 for value in (
138138
args.max_minutes, args.evaluation_timeout_seconds

‎src/inferrsi/harness/project.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pathlib import Path
1111
from statistics import median
1212

13-
from ..infra.artifacts import sha256
13+
from .artifact_primitives import sha256
1414

1515

1616
def validate(settings: dict) -> None:

‎src/inferrsi/harness/task_protocol.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import shutil
99
from pathlib import Path
1010

11-
from ..infra.artifacts import sha256
11+
from .artifact_primitives import sha256
1212

1313

1414
def protocol(root: Path):

0 commit comments

Comments
 (0)