15from typing
import TYPE_CHECKING, Any, Literal, Mapping
160def require_window_plan_block(
161 manifest: Mapping[str, Any],
162 provenance: Mapping[str, Any] |
None =
None,
164 """Admit selected metadata, including optional supplied provenance.
166 An explicit helper path cannot replace a missing expected identity. Ordinary
167 precompiled loading does not call this function; selected callers must do so.
168 This checks association metadata only, not plugin bytes or helper exports.
171 if not isinstance(manifest, Mapping)
or (
172 provenance
is not None and not isinstance(provenance, Mapping)
175 "window_plan_manifest_invalid",
176 "manifest and supplied provenance must be JSON objects",
178 if "window_plan" not in manifest:
179 if provenance
is not None and "window_plan" in provenance:
180 WindowPlanIdentity.from_dict(provenance[
"window_plan"])
182 "window_plan_identity",
183 "manifest.window_plan is missing but provenance.window_plan is present",
186 "window_plan_manifest_missing",
187 "selected execution requires manifest.window_plan with the expected helper "
188 "identity; an explicit path alone cannot supply it",
190 identity = WindowPlanIdentity.from_dict(manifest[
"window_plan"])
191 if provenance
is not None:
192 if "window_plan" not in provenance:
194 "window_plan_identity",
195 "provenance.window_plan is missing; the metadata pair is incomplete",
197 companion = WindowPlanIdentity.from_dict(provenance[
"window_plan"])
198 if companion != identity:
200 "window_plan_identity",
201 "manifest.window_plan differs from provenance.window_plan",
203 _check_compile_identity(
205 provenance.get(
"request_identity"),
208 if manifest.get(
"request_identity") != provenance.get(
"request_identity"):
210 "window_plan_identity",
211 "manifest and provenance request_identity differ",
213 _check_compile_identity(identity, manifest.get(
"request_identity"),
"manifest")
214 return identity.to_dict()
217def derive_window_plan_path(engine_layout: _EngineLayout) -> Path |
None:
218 """Derive only the sibling of archive candidate 1 or 2; never search."""
220 from .artifact
import ArtifactBuildError, _platform_link_mode
223 engine_layout.root /
"build" /
"lib" /
"libpineforge.a",
224 engine_layout.root /
"lib" /
"libpineforge.a",
226 archive = engine_layout.selected_archive_path
or engine_layout.static_library
227 if archive
not in candidates:
230 suffix, _ = _platform_link_mode()
231 except ArtifactBuildError
as error:
234 "window_plan derivation requires a supported platform",
236 return archive.with_name(f
"libpineforge_window_plan{suffix}")
328def resolve_window_plan_path(
329 explicit_path: str | os.PathLike[str] |
None,
330 engine_root: str | os.PathLike[str] |
None,
331 recorded_block: WindowPlanIdentity | Mapping[str, Any],
333 """Resolve a run-machine path; call :func:`verify_window_plan` before handoff.
335 Explicit paths take precedence with no fallback. Derivation uses only a
336 caller-supplied local engine root with matching compile content. The recorded
337 build_path is audit data and is never used for lookup.
340 from .artifact
import ArtifactBuildError, ArtifactBuilder
342 identity = _identity(recorded_block)
344 _absolute_helper_path(explicit_path)
if explicit_path
is not None else None
347 {
"system": platform.system(),
"machine": platform.machine()},
348 {
"system": identity.platform_system,
"machine": identity.platform_machine},
351 if explicit
is not None:
353 if engine_root
is None:
356 "an absolute window_plan path is required without a local engine_root",
360 engine_root=engine_root,
361 cache_dir=engine_root,
362 )._resolve_engine_layout()
363 except (ArtifactBuildError, OSError, RuntimeError, ValueError)
as error:
365 "window_plan_identity",
366 f
"cannot establish local engine association: {engine_root}: {error}",
368 _check_compile_identity(
371 "engine": engine.identity_dict(),
372 "platform": {
"system": platform.system(),
"machine": platform.machine()},
376 derived = derive_window_plan_path(engine)
380 "the selected engine archive has no derivable window_plan sibling; "
381 "supply an explicit absolute path",
383 return _absolute_helper_path(derived)
386def verify_window_plan(
387 block: WindowPlanIdentity | Mapping[str, Any],
388 path: str | os.PathLike[str],
390 """Check only the helper file's SHA-256 against the recorded expected bytes.
392 This pathname-based hash check is not race-free loading. Lane 2's native
393 consumer must copy/hash an opened file into a private snapshot and load that
394 verified snapshot, then validate exports, ABI and the frozen self-test.
397 from .artifact
import _file_sha256
399 identity = _identity(block)
400 resolved = _absolute_helper_path(path)
402 actual = _file_sha256(resolved)
403 except OSError
as error:
405 "window_plan_missing",
406 f
"cannot hash window_plan helper: {resolved}: {error}",
408 if actual != identity.sha256:
410 "window_plan_hash_mismatch",
411 f
"window_plan helper SHA-256 differs from manifest.window_plan.sha256: "