@tangle-network/agent-bench 3.0.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tangle-network/agent-bench",
3
- "version": "3.0.0",
3
+ "version": "4.0.0",
4
4
  "type": "module",
5
5
  "description": "Benchmark adapters and execution for agent-runtime across coding, tool-use, RAG, memory, browser, and terminal tasks.",
6
6
  "repository": {
@@ -28,10 +28,10 @@
28
28
  }
29
29
  },
30
30
  "dependencies": {
31
- "@tangle-network/agent-eval": "^2.0.11",
32
- "@tangle-network/agent-interface": "^5.10.28",
31
+ "@tangle-network/agent-eval": "^2.0.29",
32
+ "@tangle-network/agent-interface": "^5.10.33",
33
33
  "@tangle-network/agent-knowledge": "^23.0.0",
34
- "@tangle-network/agent-runtime": "^10.0.16",
34
+ "@tangle-network/agent-runtime": "^11.0.0",
35
35
  "@tangle-network/sandbox": "^1.0.233"
36
36
  },
37
37
  "devDependencies": {
@@ -37,6 +37,11 @@ _PROFILE_PLAN_KIND = "agent-profile-workspace-plan"
37
37
  _PROFILE_ACTIVATION_KIND = "agent-candidate-profile-activation"
38
38
  _EXECUTION_EVIDENCE_KIND = "agent-candidate-execution-plan"
39
39
  _RUN_CELL_KIND = "agent-candidate-run-cell"
40
+ _SEARCH_PLAN_KIND = "agent-candidate-search-execution-plan-material-v1"
41
+ _SEARCH_RECEIPT_KIND = "agent-candidate-search-materialization-v1"
42
+ _SEARCH_EXECUTION_EVIDENCE_KIND = "agent-candidate-search-execution-plan-v1"
43
+ _SEARCH_RUN_CELL_KIND = "agent-candidate-search-run-cell-v1"
44
+ _SEARCH_ADMISSION_KIND = "agent-candidate-search-admission-v1"
40
45
  _BENCHMARK_SUITE_KIND = "agent-candidate-benchmark-suite"
41
46
  _BENCHMARK_TASK_KIND = "agent-candidate-benchmark-task"
42
47
  _STDIN_TASK_PATH = "/tangle/input/stdin.txt"
@@ -71,6 +76,14 @@ class ProfileFile:
71
76
  sha256: str
72
77
 
73
78
 
79
+ @dataclass(frozen=True)
80
+ class KnowledgeWorkspace:
81
+ root: str
82
+ files: tuple[WorkspaceFile, ...]
83
+ retrieval: WorkspaceFile | None
84
+ retrieval_path: str | None
85
+
86
+
74
87
  @dataclass(frozen=True)
75
88
  class PreparedCandidateContract:
76
89
  plan: dict[str, Any]
@@ -90,6 +103,7 @@ class PreparedCandidateContract:
90
103
  candidate_files: tuple[WorkspaceFile, ...]
91
104
  profile_target: str
92
105
  profile_files: tuple[ProfileFile, ...]
106
+ knowledge: KnowledgeWorkspace | None
93
107
  executable: str
94
108
  args: tuple[str, ...]
95
109
  env: dict[str, str]
@@ -330,6 +344,64 @@ def _workspace_files(value: Any, label: str) -> tuple[WorkspaceFile, ...]:
330
344
  return tuple(files)
331
345
 
332
346
 
347
+ def _knowledge_workspace(
348
+ digest: str | None,
349
+ receipt_digest: Any,
350
+ bundle_digest: str,
351
+ bundle_path: Path | None,
352
+ task_root: str,
353
+ env: dict[str, str],
354
+ task_paths: tuple[str, ...],
355
+ ) -> KnowledgeWorkspace | None:
356
+ root_env = env.get("TANGLE_CANDIDATE_KNOWLEDGE_ROOT")
357
+ config_env = env.get("TANGLE_CANDIDATE_KNOWLEDGE_RETRIEVAL_CONFIG")
358
+ if digest != receipt_digest:
359
+ raise CandidateContractError("plan and receipt knowledge digests differ")
360
+ if digest is None:
361
+ if bundle_path is not None or root_env is not None or config_env is not None:
362
+ raise CandidateContractError("unexpected knowledge workspace input")
363
+ return None
364
+ if bundle_path is None:
365
+ raise CandidateContractError("signed knowledge requires its sealed bundle")
366
+
367
+ bundle, raw = _read_json(bundle_path, "knowledge bundle")
368
+ _contract_object(bundle, "knowledge bundle", kind="agent-candidate-bundle")
369
+ if sha256_bytes(raw) != bundle_digest:
370
+ raise CandidateContractError("knowledge bundle differs from the sealed candidate")
371
+ knowledge = _object(bundle.get("knowledge"), "bundle.knowledge")
372
+ snapshot = _object(knowledge.get("snapshot"), "bundle.knowledge.snapshot")
373
+ if snapshot.get("digest") != digest or sha256_bytes(
374
+ _canonical_bytes(snapshot.get("material"), "bundle.knowledge.snapshot.material")
375
+ ) != digest:
376
+ raise CandidateContractError("knowledge manifest differs from the sealed execution")
377
+ files = _workspace_files(snapshot, "bundle.knowledge.snapshot")
378
+ retrieval = None
379
+ config_path = None
380
+ if "retrievalConfig" in knowledge:
381
+ artifact = _object(knowledge["retrievalConfig"], "bundle.knowledge.retrievalConfig")
382
+ retrieval = WorkspaceFile(
383
+ path="retrieval.json",
384
+ mode=0o644,
385
+ sha256=_digest(artifact.get("sha256"), "retrieval configuration digest"),
386
+ byte_length=_integer(artifact.get("byteLength"), "retrieval configuration length"),
387
+ )
388
+ config_path = f"{task_root}/.tangle/knowledge-retrieval-config.json"
389
+ root = f"{task_root}/.tangle/knowledge"
390
+ if root_env != root or config_env != config_path:
391
+ raise CandidateContractError("knowledge execution paths differ from the sealed launch")
392
+ reserved = tuple(path for path in (root, config_path) if path is not None)
393
+ for path in task_paths:
394
+ full_path = f"{task_root}/{path}"
395
+ if any(
396
+ full_path == target
397
+ or full_path.startswith(f"{target}/")
398
+ or target.startswith(f"{full_path}/")
399
+ for target in reserved
400
+ ):
401
+ raise CandidateContractError("knowledge paths overlap signed task or profile files")
402
+ return KnowledgeWorkspace(root, files, retrieval, config_path)
403
+
404
+
333
405
  def _profile_files(value: Any) -> tuple[ProfileFile, ...]:
334
406
  values = value.get("files") if isinstance(value, dict) else None
335
407
  if not isinstance(values, list):
@@ -393,23 +465,119 @@ def _instruction(value: Any, delivery_value: Any) -> InstructionEvidence:
393
465
  )
394
466
 
395
467
 
396
- def load_prepared_candidate_contract(
397
- plan_path: Path,
398
- receipt_path: Path,
399
- expected_receipt_digest: str,
400
- ) -> PreparedCandidateContract:
401
- """Load exact runtime bytes and prove the receipt binds the plan."""
468
+ def _model_network_domains(
469
+ model: dict[str, Any], max_model_calls: int
470
+ ) -> tuple[str, ...]:
471
+ model_access = _object(model.get("access"), "plan.model.access")
472
+ if set(model_access) != {"kind", "grantDigest", "network"}:
473
+ raise CandidateContractError("plan.model.access has unexpected fields")
474
+ if model_access.get("kind") != "evaluator-mediated":
475
+ raise CandidateContractError(
476
+ "plan.model.access.kind must equal evaluator-mediated"
477
+ )
478
+ _digest(model_access.get("grantDigest"), "plan.model.access.grantDigest")
479
+ model_network = _object(
480
+ model_access.get("network"), "plan.model.access.network"
481
+ )
482
+ if max_model_calls == 0:
483
+ if model_network != {"mode": "disabled"}:
484
+ raise CandidateContractError(
485
+ "zero-call plans cannot expose a model gateway"
486
+ )
487
+ model_network_domains: tuple[str, ...] = ()
488
+ else:
489
+ if set(model_network) != {"mode", "domains"} or model_network.get(
490
+ "mode"
491
+ ) != "gateway-only":
492
+ raise CandidateContractError(
493
+ "model-calling plans require one frozen gateway allowlist"
494
+ )
495
+ domain_values = model_network.get("domains")
496
+ if not isinstance(domain_values, list) or not domain_values:
497
+ raise CandidateContractError(
498
+ "plan.model.access.network.domains must be a non-empty array"
499
+ )
500
+ model_network_domains = tuple(
501
+ _gateway_domain(
502
+ value, f"plan.model.access.network.domains[{index}]"
503
+ )
504
+ for index, value in enumerate(domain_values)
505
+ )
506
+ if list(model_network_domains) != sorted(set(model_network_domains)):
507
+ raise CandidateContractError(
508
+ "model gateway domains must be unique and sorted"
509
+ )
510
+ return model_network_domains
402
511
 
403
- plan, plan_raw = _read_json(plan_path, "execution plan")
404
- receipt, receipt_raw = _read_json(receipt_path, "materialization receipt")
512
+
513
+ def _verify_material_digest(value: dict[str, Any], label: str) -> None:
514
+ expected = _digest(value.get("digest"), f"{label}.digest")
515
+ material = {key: item for key, item in value.items() if key != "digest"}
516
+ if sha256_bytes(_canonical_bytes(material, label)) != expected:
517
+ raise CandidateContractError(f"{label} digest does not match")
518
+
519
+
520
+ def _run_cell_coordinates(
521
+ plan: dict[str, Any], execution_id: str, *, search: bool
522
+ ) -> tuple[dict[str, Any], dict[str, Any]]:
523
+ label = "plan.runCell"
524
+ run_cell = _contract_object(
525
+ plan.get("runCell"),
526
+ label,
527
+ kind=_SEARCH_RUN_CELL_KIND if search else _RUN_CELL_KIND,
528
+ )
529
+ _verify_material_digest(run_cell, label)
530
+ if not search:
531
+ _digest(run_cell.get("experimentDigest"), f"{label}.experimentDigest")
532
+ if run_cell.get("arm") not in {"baseline", "candidate"}:
533
+ raise CandidateContractError(f"{label}.arm is unsupported")
534
+ return run_cell, run_cell
535
+
536
+ label = f"{label}.admission"
537
+ admission = _contract_object(
538
+ run_cell.get("admission"), label, kind=_SEARCH_ADMISSION_KIND
539
+ )
540
+ _verify_material_digest(admission, label)
541
+ identity = {
542
+ name: _string(admission.get(name), f"{label}.{name}")
543
+ for name in ("searchId", "cellId")
544
+ }
545
+ _string(admission.get("unitId"), f"{label}.unitId")
546
+ _string(admission.get("taskId"), f"{label}.taskId")
547
+ if admission.get("split") not in {"train", "selection"}:
548
+ raise CandidateContractError(f"{label}.split does not admit exploratory execution")
549
+ if "knowledgeRole" in admission and admission["knowledgeRole"] not in {
550
+ "baseline",
551
+ "candidate",
552
+ }:
553
+ raise CandidateContractError(f"{label}.knowledgeRole is unsupported")
554
+ if execution_id != "search:" + sha256_bytes(_canonical_bytes(identity, label))[7:]:
555
+ raise CandidateContractError(
556
+ "plan.executionId does not match the admitted search cell"
557
+ )
558
+ return run_cell, admission
559
+
560
+
561
+ def _verify_plan_envelope(
562
+ plan: dict[str, Any],
563
+ plan_raw: bytes,
564
+ receipt: dict[str, Any],
565
+ receipt_raw: bytes,
566
+ expected_receipt_digest: str,
567
+ ) -> tuple[bool, str, str]:
405
568
  receipt_digest = _digest(expected_receipt_digest, "expected receipt digest")
406
569
  if sha256_bytes(receipt_raw) != receipt_digest:
407
570
  raise CandidateContractError(
408
571
  "materialization receipt bytes do not match the expected digest"
409
572
  )
410
- plan = _contract_object(plan, "execution plan", kind=_PLAN_KIND)
411
- receipt = _contract_object(
412
- receipt, "materialization receipt", kind=_RECEIPT_KIND
573
+ search = plan.get("kind") == _SEARCH_PLAN_KIND
574
+ _contract_object(
575
+ plan, "execution plan", kind=_SEARCH_PLAN_KIND if search else _PLAN_KIND
576
+ )
577
+ _contract_object(
578
+ receipt,
579
+ "materialization receipt",
580
+ kind=_SEARCH_RECEIPT_KIND if search else _RECEIPT_KIND,
413
581
  )
414
582
  if receipt.get("digestAlgorithm") != "rfc8785-sha256" or "digest" in receipt:
415
583
  raise CandidateContractError(
@@ -419,7 +587,7 @@ def load_prepared_candidate_contract(
419
587
  execution_evidence = _contract_object(
420
588
  receipt.get("executionPlan"),
421
589
  "receipt.executionPlan",
422
- kind=_EXECUTION_EVIDENCE_KIND,
590
+ kind=_SEARCH_EXECUTION_EVIDENCE_KIND if search else _EXECUTION_EVIDENCE_KIND,
423
591
  )
424
592
  plan_digest = _digest(
425
593
  execution_evidence.get("digest"), "receipt.executionPlan.digest"
@@ -440,29 +608,30 @@ def load_prepared_candidate_contract(
440
608
  raise CandidateContractError(
441
609
  "receipt execution-plan artifact differs from the executed bytes"
442
610
  )
611
+ return search, receipt_digest, plan_digest
443
612
 
444
- run_cell = _contract_object(
445
- plan.get("runCell"), "plan.runCell", kind=_RUN_CELL_KIND
613
+
614
+ def load_prepared_candidate_contract(
615
+ plan_path: Path,
616
+ receipt_path: Path,
617
+ expected_receipt_digest: str,
618
+ knowledge_bundle_path: Path | None = None,
619
+ ) -> PreparedCandidateContract:
620
+ """Load exact runtime bytes and prove the receipt binds the plan."""
621
+
622
+ plan, plan_raw = _read_json(plan_path, "execution plan")
623
+ receipt, receipt_raw = _read_json(receipt_path, "materialization receipt")
624
+ search, receipt_digest, plan_digest = _verify_plan_envelope(
625
+ plan, plan_raw, receipt, receipt_raw, expected_receipt_digest
446
626
  )
447
- _digest(run_cell.get("experimentDigest"), "plan.runCell.experimentDigest")
448
- run_cell_digest = _digest(run_cell.get("digest"), "plan.runCell.digest")
449
- if run_cell.get("arm") not in {"baseline", "candidate"}:
450
- raise CandidateContractError("plan.runCell.arm is unsupported")
451
- run_cell_seed = _safe_integer(run_cell.get("seed"), "plan.runCell.seed")
452
- if sha256_bytes(
453
- _canonical_bytes(
454
- {key: value for key, value in run_cell.items() if key != "digest"},
455
- "plan.runCell",
456
- )
457
- ) != run_cell_digest:
458
- raise CandidateContractError("plan.runCell digest does not match")
627
+ execution_id = _string(plan.get("executionId"), "plan.executionId")
628
+ run_cell, coordinates = _run_cell_coordinates(plan, execution_id, search=search)
629
+ run_cell_seed = _safe_integer(coordinates.get("seed"), "plan.runCell.seed")
459
630
  bundle_digest = _digest(
460
- run_cell.get("bundleDigest"), "plan.runCell.bundleDigest"
631
+ coordinates.get("bundleDigest"), "plan.runCell.bundleDigest"
461
632
  )
462
633
  if receipt.get("bundleDigest") != bundle_digest:
463
634
  raise CandidateContractError("receipt and plan bundle digests differ")
464
- execution_id = _string(plan.get("executionId"), "plan.executionId")
465
-
466
635
  benchmark = _object(receipt.get("benchmark"), "receipt.benchmark")
467
636
  suite, _, suite_digest = _embedded_contract(
468
637
  benchmark.get("suite"),
@@ -475,14 +644,14 @@ def load_prepared_candidate_contract(
475
644
  kind=_BENCHMARK_TASK_KIND,
476
645
  )
477
646
  if (
478
- run_cell.get("suiteDigest") != suite_digest
479
- or run_cell.get("taskDigest") != task_digest
647
+ coordinates.get("suiteDigest") != suite_digest
648
+ or coordinates.get("taskDigest") != task_digest
480
649
  ):
481
650
  raise CandidateContractError(
482
651
  "run cell does not bind the receipt benchmark suite and task"
483
652
  )
484
- task_index = _integer(run_cell.get("taskIndex"), "plan.runCell.taskIndex")
485
- repetition = _integer(run_cell.get("repetition"), "plan.runCell.repetition")
653
+ task_index = _integer(coordinates.get("taskIndex"), "plan.runCell.taskIndex")
654
+ repetition = _integer(coordinates.get("repetition"), "plan.runCell.repetition")
486
655
  if suite.get("digestAlgorithm") != "rfc8785-sha256":
487
656
  raise CandidateContractError(
488
657
  "benchmark suite digestAlgorithm must equal rfc8785-sha256"
@@ -522,6 +691,13 @@ def load_prepared_candidate_contract(
522
691
  raise CandidateContractError(
523
692
  "benchmark task digestAlgorithm must equal rfc8785-sha256"
524
693
  )
694
+ if (
695
+ search
696
+ and _object(task.get("scenario"), "task.scenario").get("id") != coordinates["taskId"]
697
+ ):
698
+ raise CandidateContractError(
699
+ "search admission taskId differs from the benchmark task"
700
+ )
525
701
  outcome = _object(task.get("outcome"), "plan.task.outcome")
526
702
  if outcome.get("kind") != "workspace":
527
703
  raise CandidateContractError("Pier requires a workspace task outcome")
@@ -821,45 +997,7 @@ def load_prepared_candidate_contract(
821
997
  or task.get("model") != resolved_model
822
998
  ):
823
999
  raise CandidateContractError("receipt and plan resolved models differ")
824
- model_access = _object(model.get("access"), "plan.model.access")
825
- if set(model_access) != {"kind", "grantDigest", "network"}:
826
- raise CandidateContractError("plan.model.access has unexpected fields")
827
- if model_access.get("kind") != "evaluator-mediated":
828
- raise CandidateContractError(
829
- "plan.model.access.kind must equal evaluator-mediated"
830
- )
831
- _digest(model_access.get("grantDigest"), "plan.model.access.grantDigest")
832
- model_network = _object(
833
- model_access.get("network"), "plan.model.access.network"
834
- )
835
- if max_model_calls == 0:
836
- if model_network != {"mode": "disabled"}:
837
- raise CandidateContractError(
838
- "zero-call plans cannot expose a model gateway"
839
- )
840
- model_network_domains: tuple[str, ...] = ()
841
- else:
842
- if set(model_network) != {"mode", "domains"} or model_network.get(
843
- "mode"
844
- ) != "gateway-only":
845
- raise CandidateContractError(
846
- "model-calling plans require one frozen gateway allowlist"
847
- )
848
- domain_values = model_network.get("domains")
849
- if not isinstance(domain_values, list) or not domain_values:
850
- raise CandidateContractError(
851
- "plan.model.access.network.domains must be a non-empty array"
852
- )
853
- model_network_domains = tuple(
854
- _gateway_domain(
855
- value, f"plan.model.access.network.domains[{index}]"
856
- )
857
- for index, value in enumerate(domain_values)
858
- )
859
- if list(model_network_domains) != sorted(set(model_network_domains)):
860
- raise CandidateContractError(
861
- "model gateway domains must be unique and sorted"
862
- )
1000
+ model_network_domains = _model_network_domains(model, max_model_calls)
863
1001
  if receipt.get("harness") != plan.get("harness") or receipt.get(
864
1002
  "harnessVersion"
865
1003
  ) != plan.get("harnessVersion"):
@@ -871,9 +1009,22 @@ def load_prepared_candidate_contract(
871
1009
  raise CandidateContractError(
872
1010
  "Pier adapter does not yet support isolated candidate memory"
873
1011
  )
874
- if plan.get("knowledgeManifestDigest") is not None:
1012
+ knowledge_digest = plan.get("knowledgeManifestDigest")
1013
+ knowledge = _knowledge_workspace(
1014
+ _digest(knowledge_digest, "plan.knowledgeManifestDigest")
1015
+ if knowledge_digest is not None
1016
+ else None,
1017
+ receipt.get("knowledgeManifestDigest"),
1018
+ bundle_digest,
1019
+ knowledge_bundle_path,
1020
+ task_root,
1021
+ env,
1022
+ tuple(file.path for file in task_files)
1023
+ + (tuple(file.path for file in profile_files) if profile_target == "task" else ()),
1024
+ )
1025
+ if search and ("knowledgeRole" in coordinates) != (knowledge is not None):
875
1026
  raise CandidateContractError(
876
- "Pier adapter does not yet support candidate knowledge snapshots"
1027
+ "search admission Knowledge role differs from the mounted inputs"
877
1028
  )
878
1029
  network = _object(plan.get("network"), "plan.network")
879
1030
  if network != {"mode": "disabled"}:
@@ -899,6 +1050,7 @@ def load_prepared_candidate_contract(
899
1050
  candidate_files=candidate_files,
900
1051
  profile_target=profile_target,
901
1052
  profile_files=profile_files,
1053
+ knowledge=knowledge,
902
1054
  executable=executable,
903
1055
  args=args,
904
1056
  env=env,
@@ -18,7 +18,7 @@ import secrets
18
18
  import shlex
19
19
  import tempfile
20
20
  import time
21
- from dataclasses import dataclass
21
+ from dataclasses import dataclass, replace
22
22
  from importlib.metadata import PackageNotFoundError, version as package_version
23
23
  from pathlib import Path, PurePosixPath
24
24
  from typing import Any
@@ -170,6 +170,8 @@ class TangleCandidateAgent(BaseAgent):
170
170
  task_dir: str | None = None,
171
171
  profile_dir: str | None = None,
172
172
  candidate_dir: str | None = None,
173
+ knowledge_dir: str | None = None,
174
+ knowledge_bundle_path: str | None = None,
173
175
  pier_version: str | None = None,
174
176
  extra_env: dict[str, str] | None = None,
175
177
  *args: Any,
@@ -193,6 +195,9 @@ class TangleCandidateAgent(BaseAgent):
193
195
  Path(plan_path).expanduser().resolve(),
194
196
  Path(receipt_path).expanduser().resolve(),
195
197
  expected_receipt_digest,
198
+ Path(knowledge_bundle_path).expanduser().absolute()
199
+ if knowledge_bundle_path
200
+ else None,
196
201
  )
197
202
  if model_name != self._contract.requested_model:
198
203
  raise PierCandidateError(
@@ -211,6 +216,7 @@ class TangleCandidateAgent(BaseAgent):
211
216
  self._task_snapshot = snapshot_root / "task"
212
217
  self._profile_snapshot = snapshot_root / "profile"
213
218
  self._candidate_snapshot: Path | None = None
219
+ self._knowledge_snapshot: Path | None = None
214
220
  try:
215
221
  immutable_snapshot(
216
222
  Path(os.path.abspath(Path(task_dir).expanduser())),
@@ -236,6 +242,22 @@ class TangleCandidateAgent(BaseAgent):
236
242
  )
237
243
  elif candidate_dir:
238
244
  raise PierCandidateError("disabled code cannot receive candidate_dir")
245
+ if self._contract.knowledge is not None:
246
+ if not knowledge_dir:
247
+ raise PierCandidateError("signed knowledge requires knowledge_dir")
248
+ knowledge = self._contract.knowledge
249
+ self._knowledge_snapshot = snapshot_root / "knowledge"
250
+ staged_files = tuple(
251
+ replace(file, path=f"files/{file.path}") for file in knowledge.files
252
+ ) + ((knowledge.retrieval,) if knowledge.retrieval is not None else ())
253
+ immutable_snapshot(
254
+ Path(os.path.abspath(Path(knowledge_dir).expanduser())),
255
+ self._knowledge_snapshot,
256
+ staged_files,
257
+ "prepared knowledge directory",
258
+ )
259
+ elif knowledge_dir:
260
+ raise PierCandidateError("unexpected knowledge directory")
239
261
  except Exception:
240
262
  self._snapshots.cleanup()
241
263
  raise
@@ -571,6 +593,60 @@ done""", user="root")
571
593
  byte_length=source.stat().st_size,
572
594
  )
573
595
 
596
+ async def _apply_knowledge(self, environment: BaseEnvironment) -> None:
597
+ knowledge = self._contract.knowledge
598
+ if knowledge is None:
599
+ return
600
+ if self._knowledge_snapshot is None:
601
+ raise PierCandidateError("knowledge snapshot is unavailable")
602
+ parent = str(PurePosixPath(knowledge.root).parent)
603
+ await self._ensure_real_directory(
604
+ environment, parent, anchor=self._contract.task_root
605
+ )
606
+ root = shlex.quote(knowledge.root)
607
+ await self._exec(
608
+ environment,
609
+ f"test ! -e {root} && test ! -L {root} && mkdir -- {root}",
610
+ user="root",
611
+ )
612
+ await environment.upload_dir(self._knowledge_snapshot / "files", knowledge.root)
613
+ await self._workspace_boundary.verify_container_workspace(
614
+ environment, knowledge.root, knowledge.files, allow_task_metadata=False
615
+ )
616
+ if knowledge.retrieval is not None and knowledge.retrieval_path is not None:
617
+ config = shlex.quote(knowledge.retrieval_path)
618
+ await self._exec(
619
+ environment, f"test ! -e {config} && test ! -L {config}", user="root"
620
+ )
621
+ await self._write_verified_file(
622
+ environment,
623
+ self._knowledge_snapshot / knowledge.retrieval.path,
624
+ knowledge.retrieval_path,
625
+ anchor=self._contract.task_root,
626
+ mode=knowledge.retrieval.mode,
627
+ digest=knowledge.retrieval.sha256,
628
+ byte_length=knowledge.retrieval.byte_length,
629
+ )
630
+
631
+ async def _remove_knowledge(self, environment: BaseEnvironment) -> None:
632
+ knowledge = self._contract.knowledge
633
+ if knowledge is None:
634
+ return
635
+ parent = shlex.quote(str(PurePosixPath(knowledge.root).parent))
636
+ targets = [knowledge.root]
637
+ if knowledge.retrieval_path is not None:
638
+ targets.append(knowledge.retrieval_path)
639
+ await self._exec(
640
+ environment,
641
+ f"""set -eu
642
+ if test -e {parent} || test -L {parent}; then
643
+ test -d {parent} && test ! -L {parent}
644
+ test "$(realpath -e -- {parent})" = {parent}
645
+ rm -rf -- {shlex.join(targets)}
646
+ fi""",
647
+ user="root",
648
+ )
649
+
574
650
  async def setup(self, environment: BaseEnvironment) -> None:
575
651
  try:
576
652
  observed_pier = _runtime_pier_version()
@@ -632,6 +708,7 @@ done""", user="root")
632
708
  if self._contract.candidate_root is not None
633
709
  else ()
634
710
  )
711
+ await self._apply_knowledge(environment)
635
712
  await self._process_boundary.prepare_identity(environment, workspace_roots)
636
713
  await self._apply_profile(environment)
637
714
 
@@ -747,6 +824,7 @@ done""", user="root")
747
824
  env=git_env,
748
825
  user="root",
749
826
  )
827
+ await self._remove_knowledge(environment)
750
828
  for original in self._original_profile_files:
751
829
  target = f"{root}/{original.path}"
752
830
  quoted = shlex.quote(target)
@@ -2,6 +2,7 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
+ import re
5
6
  import secrets
6
7
  import shlex
7
8
  import tempfile
@@ -17,6 +18,13 @@ from .candidate_contract import (
17
18
  sha256_bytes,
18
19
  )
19
20
 
21
+ _OCI_PATH_COMPONENT = r"[a-z0-9]+(?:(?:[.]|_{1,2}|-+)[a-z0-9]+)*"
22
+ _PREBOUND_OCI = re.compile(
23
+ rf"127\.0\.0\.1:([1-9][0-9]{{0,4}})/{_OCI_PATH_COMPONENT}"
24
+ rf"(?:/{_OCI_PATH_COMPONENT})*(?::[A-Za-z0-9_][A-Za-z0-9_.-]{{0,127}})?"
25
+ r"@(sha256:[0-9a-f]{64})"
26
+ )
27
+
20
28
 
21
29
  def _workspace_check_script(
22
30
  root: str,
@@ -210,7 +218,27 @@ class CandidateWorkspaceBoundary:
210
218
  expected = self._contract.container
211
219
  configured_image = getattr(environment.task_env_config, "docker_image", None)
212
220
  pinned_image = f"{expected['image']}@{expected['indexDigest']}"
213
- if configured_image != pinned_image:
221
+ matched = configured_image == pinned_image
222
+ if (
223
+ expected["source"] == "evaluator-task-container"
224
+ and expected["image"] == expected["manifestDigest"]
225
+ ):
226
+ from pier.environments.docker.docker import DockerEnvironment
227
+
228
+ # Pin the signed platform manifest directly so Docker uses the exact image.
229
+ pinned_image = f"127.0.0.1:<port>/<repository>@{expected['manifestDigest']}"
230
+ local = (
231
+ _PREBOUND_OCI.fullmatch(configured_image)
232
+ if isinstance(configured_image, str)
233
+ else None
234
+ )
235
+ matched = (
236
+ isinstance(environment, DockerEnvironment)
237
+ and local is not None
238
+ and int(local.group(1)) <= 65535
239
+ and local.group(2) == expected["manifestDigest"]
240
+ )
241
+ if not matched:
214
242
  raise self._error(
215
243
  "Pier task image does not match the signed immutable image: "
216
244
  f"expected={pinned_image} observed={configured_image}"