agent-orchestration-process 0.6.2__tar.gz → 0.7.0__tar.gz

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.
Files changed (29) hide show
  1. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/PKG-INFO +1 -1
  2. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/pyproject.toml +1 -1
  3. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/pyproject.toml.orig +1 -1
  4. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/cli.py +57 -6
  5. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/migration.py +25 -1
  6. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/model_listing.py +8 -0
  7. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/models.py +9 -0
  8. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/runner.py +445 -61
  9. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/worktrees.py +43 -5
  10. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/LICENSE +0 -0
  11. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/README.md +0 -0
  12. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/__init__.py +0 -0
  13. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/__main__.py +0 -0
  14. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/batch.py +0 -0
  15. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/codex_auth.py +0 -0
  16. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/codex_routes.py +0 -0
  17. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/dsh_runner.mjs +0 -0
  18. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/egress.py +0 -0
  19. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/egress_bridge.py +0 -0
  20. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/integration.py +0 -0
  21. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/isolation.py +0 -0
  22. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/locks.py +0 -0
  23. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/model_catalog.py +0 -0
  24. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/pricing.py +0 -0
  25. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/prompt_transport.py +0 -0
  26. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/provider_versions.py +0 -0
  27. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/sandbox_launch.py +0 -0
  28. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/zcode.py +0 -0
  29. {agent_orchestration_process-0.6.2 → agent_orchestration_process-0.7.0}/src/agent_orchestration_process/zcode_protocol.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: agent-orchestration-process
3
- Version: 0.6.2
3
+ Version: 0.7.0
4
4
  Summary: Run bounded, non-interactive jobs across agent harnesses with isolation, exact resume, and provenance
5
5
  Keywords: agent-orchestration,ai-agents,autoresearch,coding-agents,git-worktrees
6
6
  Author: AOP contributors
@@ -4,7 +4,7 @@ build-backend = "uv_build"
4
4
 
5
5
  [project]
6
6
  name = "agent-orchestration-process"
7
- version = "0.6.2"
7
+ version = "0.7.0"
8
8
  description = "Run bounded, non-interactive jobs across agent harnesses with isolation, exact resume, and provenance"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.11"
@@ -4,7 +4,7 @@ build-backend = "uv_build"
4
4
 
5
5
  [project]
6
6
  name = "agent-orchestration-process"
7
- version = "0.6.2"
7
+ version = "0.7.0"
8
8
  description = "Run bounded, non-interactive jobs across agent harnesses with isolation, exact resume, and provenance"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.11"
@@ -33,6 +33,7 @@ from .runner import (
33
33
  )
34
34
  from .migration import migrate_archive, migrate_runs, migration_roots, print_counts
35
35
  from .worktrees import (
36
+ AdmissionRefused,
36
37
  AOPError,
37
38
  WorktreeManager,
38
39
  compact_state_roots,
@@ -229,6 +230,7 @@ def build_parser() -> argparse.ArgumentParser:
229
230
  "instead of a task worktree; TASK becomes optional and AOP keeps its "
230
231
  "records in the current directory",
231
232
  )
233
+ _add_admit_argument(run)
232
234
  run.add_argument(
233
235
  "--native-state",
234
236
  action="store_true",
@@ -271,6 +273,7 @@ def build_parser() -> argparse.ArgumentParser:
271
273
  "--json", action="store_true", help="print the normalized result as JSON"
272
274
  )
273
275
  _add_artifact_arguments(resume)
276
+ _add_admit_argument(resume)
274
277
  _add_input_arguments(resume, default=None, resume=True)
275
278
  _add_prompt_arguments(resume)
276
279
 
@@ -383,13 +386,23 @@ def main(argv: Sequence[str] | None = None) -> int:
383
386
  if args.command == "cleanup" and args.dry_run and not args.all:
384
387
  raise AOPError("--dry-run requires --all")
385
388
  if args.command == "cleanup" and args.all:
386
- return _cleanup_all(manager, dry_run=args.dry_run)
389
+ # --workdir and sealed runs keep their records in the current
390
+ # directory's .aop, which inside a repository differs from the
391
+ # repository's own state, so both are cleaned.
392
+ managers = [manager]
393
+ local = Path.cwd() / ".aop"
394
+ if local.is_dir() and local.resolve() != manager.state_dir.resolve():
395
+ managers.append(WorktreeManager.standalone(Path.cwd()))
396
+ return max(_cleanup_all(item, dry_run=args.dry_run) for item in managers)
387
397
  if args.command == "cleanup":
388
398
  request = RunStore(manager.state_dir / "runs").load_request(args.run_id)
389
399
  if request.profile == "sealed":
390
400
  _cleanup_sealed(manager, request)
391
401
  elif request.workdir is not None:
392
- pass # The caller's directory is not AOP's to remove.
402
+ # The caller's directory is not AOP's to remove; the run's
403
+ # private harness home and scratch are.
404
+ for path in manager.remove_private_state(request.task):
405
+ print(f"aop: removed {path}", file=sys.stderr)
393
406
  elif any(item.task == request.task for item in manager.list()):
394
407
  _report_archived(manager.remove(request.task, force=True))
395
408
  print(request.task)
@@ -522,6 +535,7 @@ def main(argv: Sequence[str] | None = None) -> int:
522
535
  writable_workspace=args.writable_workspace,
523
536
  thinking_display=args.thinking_display,
524
537
  reminders=args.reminders,
538
+ admit_command=args.admit_command,
525
539
  )
526
540
  return _report_run(result, manager, json_output=args.json)
527
541
 
@@ -534,6 +548,7 @@ def main(argv: Sequence[str] | None = None) -> int:
534
548
  timeout_seconds=args.timeout,
535
549
  artifacts=args.artifact,
536
550
  input_paths=args.input_paths,
551
+ admit_command=args.admit_command,
537
552
  )
538
553
  return _report_run(result, manager, json_output=args.json)
539
554
 
@@ -553,6 +568,12 @@ def main(argv: Sequence[str] | None = None) -> int:
553
568
  return 0
554
569
 
555
570
  raise AOPError("unsupported command")
571
+ except AdmissionRefused as error:
572
+ # Standard output would carry the answer; a refused run has none, so
573
+ # it carries this line, which callers can tell from a result.
574
+ print(json.dumps({"admitted": False, "reason": str(error), **error.decision}))
575
+ print(f"aop: {error}", file=sys.stderr)
576
+ return 2
556
577
  except AOPError as error:
557
578
  print(f"aop: {error}", file=sys.stderr)
558
579
  return 2
@@ -581,6 +602,16 @@ def _add_prompt_arguments(parser: argparse.ArgumentParser) -> None:
581
602
  )
582
603
 
583
604
 
605
+ def _add_admit_argument(parser: argparse.ArgumentParser) -> None:
606
+ parser.add_argument(
607
+ "--admit-command",
608
+ metavar="CMD",
609
+ help="command that must exit 0 before the harness starts; it receives "
610
+ "the run as JSON on stdin and AOP_ADMIT_* variables (default: "
611
+ "AOP_ADMIT_COMMAND; empty for none)",
612
+ )
613
+
614
+
584
615
  def _add_artifact_arguments(parser: argparse.ArgumentParser) -> None:
585
616
  parser.add_argument(
586
617
  "--artifact",
@@ -844,10 +875,12 @@ def _report_archived(ref: str | None) -> None:
844
875
 
845
876
 
846
877
  def _cleanup_all(manager: WorktreeManager, *, dry_run: bool) -> int:
847
- """Clean every task worktree and sealed run in this state, skipping busy ones.
878
+ """Clean every task worktree, sealed run, and --workdir run's private state
879
+ in this state, skipping busy ones.
848
880
 
849
881
  Worktrees are found from Git, so a task whose run record is missing is
850
- cleaned too. Run records, results, logs, and artifacts are kept.
882
+ cleaned too. Run records, results, logs, and artifacts are kept, and so is
883
+ every --workdir run's own directory.
851
884
  """
852
885
  skipped = 0
853
886
  try:
@@ -855,6 +888,7 @@ def _cleanup_all(manager: WorktreeManager, *, dry_run: bool) -> int:
855
888
  except AOPError:
856
889
  tasks = [] # Not a Git repository: only sealed runs.
857
890
  sealed = []
891
+ in_place: dict[str, None] = {}
858
892
  runs = manager.state_dir / "runs"
859
893
  for run_dir in sorted(runs.iterdir()) if runs.is_dir() else []:
860
894
  try:
@@ -865,6 +899,12 @@ def _cleanup_all(manager: WorktreeManager, *, dry_run: bool) -> int:
865
899
  continue
866
900
  if request.profile == "sealed" and _sealed_paths(manager, request):
867
901
  sealed.append(request)
902
+ elif (
903
+ request.workdir is not None
904
+ and request.task not in tasks
905
+ and manager.private_state(request.task)
906
+ ):
907
+ in_place[request.task] = None
868
908
  for task in tasks:
869
909
  if dry_run:
870
910
  print(f"would remove task {task}")
@@ -885,6 +925,16 @@ def _cleanup_all(manager: WorktreeManager, *, dry_run: bool) -> int:
885
925
  except AOPError as error:
886
926
  skipped += 1
887
927
  print(f"aop: skipped sealed run {request.run_id}: {error}", file=sys.stderr)
928
+ for task in in_place:
929
+ if dry_run:
930
+ print(f"would clean in-place task {task}")
931
+ continue
932
+ try:
933
+ manager.remove_private_state(task)
934
+ print(f"cleaned in-place task {task}")
935
+ except AOPError as error:
936
+ skipped += 1
937
+ print(f"aop: skipped in-place task {task}: {error}", file=sys.stderr)
888
938
  return 1 if skipped else 0
889
939
 
890
940
 
@@ -1129,7 +1179,7 @@ def _report_models(
1129
1179
  else:
1130
1180
  print(
1131
1181
  "agent\tprovider\tmodel\tavailability\tprice-scope\tinput\tcache-read\t"
1132
- "cache-write\toutput"
1182
+ "cache-write\toutput\tcache-write-1h"
1133
1183
  )
1134
1184
  for model in models:
1135
1185
  print(
@@ -1138,7 +1188,8 @@ def _report_models(
1138
1188
  f"{model.price_scope}\t{_price(model.input_per_million_usd)}\t"
1139
1189
  f"{_price(model.cached_input_per_million_usd)}\t"
1140
1190
  f"{_price(model.cache_write_per_million_usd)}\t"
1141
- f"{_price(model.output_per_million_usd)}"
1191
+ f"{_price(model.output_per_million_usd)}\t"
1192
+ f"{_price(model.cache_write_1h_per_million_usd)}"
1142
1193
  )
1143
1194
  print(
1144
1195
  f"aop: prices_per_million_usd basis={model_prices.pricing_basis()} "
@@ -51,7 +51,7 @@ def migrate_runs(runs_dir: Path, *, dry_run: bool = False) -> Counter[str]:
51
51
  for run_dir in sorted(path for path in runs_dir.iterdir() if path.is_dir()):
52
52
  for name, upgrade in (
53
53
  ("request.json", _upgraded_request),
54
- ("result.json", _upgraded_result),
54
+ ("result.json", _upgraded_result_with_project),
55
55
  ):
56
56
  path = run_dir / name
57
57
  value = _migrate_file(path, run_dir, upgrade, name, counts, dry_run)
@@ -168,6 +168,30 @@ def _upgraded_request(
168
168
  return request.to_dict(), "+".join(conversions) or "normalized"
169
169
 
170
170
 
171
+ def _upgraded_result_with_project(
172
+ raw: dict[str, Any], run_dir: Path
173
+ ) -> tuple[dict[str, Any], str] | None:
174
+ """Upgrade a result, and give a --workdir run recorded before results had
175
+ project_dir the directory its request names."""
176
+ upgraded = _upgraded_result(raw, run_dir)
177
+ value, outcome = upgraded if upgraded is not None else (raw, None)
178
+ if value.get("project_dir") is not None:
179
+ return upgraded
180
+ try:
181
+ workdir = json.loads((run_dir / "request.json").read_text()).get("workdir")
182
+ except (OSError, ValueError):
183
+ workdir = None
184
+ if not isinstance(workdir, str) or not workdir:
185
+ return upgraded
186
+ value = {**value, "project_dir": workdir}
187
+ if outcome is None:
188
+ value["migration"] = _record(value.get("usage_schema"), ["project-dir"], "result.json")
189
+ return value, "project-dir"
190
+ value["migration"]["conversions"].append("project-dir")
191
+ conversions, note, rest = outcome.partition(" (")
192
+ return value, f"{conversions}+project-dir{note}{rest}"
193
+
194
+
171
195
  def _upgraded_result(
172
196
  raw: dict[str, Any], run_dir: Path
173
197
  ) -> tuple[dict[str, Any], str] | None:
@@ -62,6 +62,9 @@ class AvailableModel:
62
62
  input_per_million_usd: float | None = None
63
63
  cached_input_per_million_usd: float | None = None
64
64
  cache_write_per_million_usd: float | None = None
65
+ # Writes to a longer-lived cache, such as Anthropic's one-hour cache; equal
66
+ # to cache_write_per_million_usd where those cost the ordinary write price.
67
+ cache_write_1h_per_million_usd: float | None = None
65
68
  output_per_million_usd: float | None = None
66
69
  pricing_source: str | None = None
67
70
 
@@ -877,6 +880,11 @@ def _record(
877
880
  input_per_million_usd=rates.input,
878
881
  cached_input_per_million_usd=rates.cache_read,
879
882
  cache_write_per_million_usd=rates.cache_write,
883
+ cache_write_1h_per_million_usd=(
884
+ rates.input * rates.cache_write_1h_multiple
885
+ if rates.cache_write_1h_multiple is not None
886
+ else rates.cache_write
887
+ ),
880
888
  output_per_million_usd=rates.output,
881
889
  pricing_source=rates.source,
882
890
  )
@@ -121,6 +121,8 @@ class RunRequest:
121
121
  # Which Muse reminder agents run: "all", "none", or a comma-separated
122
122
  # subset such as "goal,verify"; None leaves Muse's own settings in charge.
123
123
  reminders: str | None = None
124
+ # The admission command that allowed this run and what it answered.
125
+ admission: dict[str, Any] | None = None
124
126
  # How `aop migrate-results` converted an older record, if it did.
125
127
  migration: dict[str, Any] | None = None
126
128
 
@@ -214,6 +216,13 @@ class RunResult:
214
216
  # when reported, the models that refused, the messages the harness
215
217
  # withdrew, and the line of events.jsonl where the refusal begins.
216
218
  refusal: dict[str, Any] | None = None
219
+ # The run's admission decision: the command and its answer, or how it
220
+ # was skipped; None when no admission command was configured.
221
+ admission: dict[str, Any] | None = None
222
+ # The directory whose work the run did: the --workdir directory, or the
223
+ # repository that owns the task worktree; None for a sealed run, which
224
+ # works on its own empty workspace.
225
+ project_dir: str | None = None
217
226
  accounting_status: str = "complete"
218
227
  usage_schema: str = TOKEN_USAGE_SCHEMA
219
228
  # The .aop state directory the run was recorded in, as listed in the
@@ -12,11 +12,13 @@ import re
12
12
  import shutil
13
13
  import stat
14
14
  import sqlite3
15
+ import shlex
15
16
  import subprocess
16
17
  import sys
17
18
  import tempfile
18
19
  import threading
19
20
  import time
21
+ import tomllib
20
22
  import urllib.parse
21
23
  import uuid
22
24
  from contextlib import contextmanager
@@ -62,6 +64,7 @@ from .pricing import (
62
64
  from .provider_versions import require_supported_agy
63
65
  from . import egress, sandbox_launch
64
66
  from .worktrees import (
67
+ AdmissionRefused,
65
68
  AOPError,
66
69
  Worktree,
67
70
  WorktreeManager,
@@ -266,6 +269,7 @@ def _build_run_result(
266
269
  provider_status=provider_status,
267
270
  provider_error=provider_error,
268
271
  refusal=refusal,
272
+ admission=request.admission,
269
273
  )
270
274
 
271
275
 
@@ -1364,8 +1368,22 @@ class ClaudeAdapter:
1364
1368
  run_dir: Path,
1365
1369
  environment: dict[str, str],
1366
1370
  ) -> RunResult:
1371
+ login = None
1367
1372
  if not request.native_state:
1373
+ source_config, _ = _claude_source(environment)
1374
+ credentials = Path(
1375
+ environment.get("CLAUDE_SECURESTORAGE_CONFIG_DIR") or source_config
1376
+ )
1368
1377
  _prepare_claude_environment(environment)
1378
+ if request.profile != "host":
1379
+ # A sandboxed run cannot share the user's store, so its private
1380
+ # copy is kept in step with it.
1381
+ login = _ClaudeLogin(
1382
+ credentials,
1383
+ Path(environment["AOP_PROVIDER_STATE_DIR"])
1384
+ / "claude" / "home" / ".claude",
1385
+ )
1386
+ login.start()
1369
1387
  if request.provider_base_url is not None:
1370
1388
  environment["ANTHROPIC_BASE_URL"] = request.provider_base_url
1371
1389
  environment["AOP_PROVIDER_BASE_URL_ENV"] = "ANTHROPIC_BASE_URL"
@@ -1373,19 +1391,20 @@ class ClaudeAdapter:
1373
1391
  self._command(request, worktree), request, worktree, environment
1374
1392
  )
1375
1393
  started_at = _now()
1376
- capture = _capture_process(
1377
- command,
1378
- cwd=worktree.path,
1379
- environment=environment,
1380
- prompt=(
1381
- prompt_transport.claude_message(request.prompt, request.prompt_parts)
1382
- if request.prompt_parts is not None
1383
- else prompt_transport.stdin_prompt(self.provider, request.prompt)
1384
- ),
1385
- timeout_seconds=request.timeout_seconds,
1386
- is_response=self._is_response,
1387
- live_dir=run_dir,
1388
- )
1394
+ with login.watching() if login is not None else contextlib.nullcontext():
1395
+ capture = _capture_process(
1396
+ command,
1397
+ cwd=worktree.path,
1398
+ environment=environment,
1399
+ prompt=(
1400
+ prompt_transport.claude_message(request.prompt, request.prompt_parts)
1401
+ if request.prompt_parts is not None
1402
+ else prompt_transport.stdin_prompt(self.provider, request.prompt)
1403
+ ),
1404
+ timeout_seconds=request.timeout_seconds,
1405
+ is_response=self._is_response,
1406
+ live_dir=run_dir,
1407
+ )
1389
1408
  previous_total_cost_usd = (
1390
1409
  self._reported_total_cost_usd(
1391
1410
  run_dir.parent / request.parent_run_id / "events.jsonl"
@@ -2736,7 +2755,12 @@ class DevinAdapter:
2736
2755
  # Devin gives up on a usage limit after three retries within a second, even
2737
2756
  # when its error names a reset minutes away. AOP waits for that reset and
2738
2757
  # continues the same session, as long as the run's deadline allows.
2739
- _LIMIT_RESET = re.compile(r"will reset in [^()]*\(at (\d{1,2}):(\d{2}) UTC\)")
2758
+ # "Your limit will reset in 2 minutes (at 12:21 UTC)", or with a date when
2759
+ # the reset is further off, "(at Nov 6 00:01 UTC)".
2760
+ _LIMIT_RESET = re.compile(
2761
+ r"will reset in (\d+) (second|minute|hour)s?"
2762
+ r"(?: \(at (?:([A-Z][a-z]{2}) (\d{1,2}) )?(\d{1,2}):(\d{2}) UTC\))?"
2763
+ )
2740
2764
  _LIMIT_MARGIN_SECONDS = 30.0
2741
2765
  _MAX_LIMIT_WAITS = 10
2742
2766
  CONTINUE_PROMPT = (
@@ -2779,7 +2803,10 @@ class DevinAdapter:
2779
2803
  )
2780
2804
  turn, turn_command = request, command
2781
2805
  capture = None
2782
- for waits in range(self._MAX_LIMIT_WAITS + 1):
2806
+ waits = 0
2807
+ while True:
2808
+ # Each attempt's export, if any, describes the session so far.
2809
+ export_path.unlink(missing_ok=True)
2783
2810
  attempt = _capture_process(
2784
2811
  turn_command,
2785
2812
  cwd=worktree.path,
@@ -2792,7 +2819,8 @@ class DevinAdapter:
2792
2819
  )
2793
2820
  capture = attempt if capture is None else _joined_captures(capture, attempt)
2794
2821
  wait = self._limit_wait(attempt)
2795
- if wait is None or waits == self._MAX_LIMIT_WAITS or export_path.is_file():
2822
+ # A deadline bounds the waiting; without one, the count does.
2823
+ if wait is None or (deadline is None and waits == self._MAX_LIMIT_WAITS):
2796
2824
  break
2797
2825
  session = request.session_id or _devin_session_for_prompt(
2798
2826
  environment, request.prompt
@@ -2808,6 +2836,7 @@ class DevinAdapter:
2808
2836
  flush=True,
2809
2837
  )
2810
2838
  time.sleep(wait)
2839
+ waits += 1
2811
2840
  turn = replace(request, session_id=session, prompt=self.CONTINUE_PROMPT)
2812
2841
  turn_command = _provider_command(
2813
2842
  self._command(turn, export_path), turn, worktree, environment
@@ -2862,6 +2891,9 @@ class DevinAdapter:
2862
2891
  usage=parsed["usage"],
2863
2892
  usage_observed=parsed["usage_observed"],
2864
2893
  calculated_cost=None,
2894
+ # Devin reports usage only in its export, which a turn that ends
2895
+ # in an error does not write.
2896
+ require_observed=True,
2865
2897
  )
2866
2898
  return _build_run_result(
2867
2899
  request,
@@ -2886,18 +2918,33 @@ class DevinAdapter:
2886
2918
  """Seconds until the usage limit Devin stopped at resets, if it did."""
2887
2919
  if not capture.exit_code or capture.timed_out:
2888
2920
  return None
2889
- match = cls._LIMIT_RESET.search(capture.stderr)
2890
- if match is None:
2921
+ matches = list(cls._LIMIT_RESET.finditer(capture.stderr))
2922
+ if not matches:
2891
2923
  return None
2924
+ amount, unit, month, day, hour, minute = matches[-1].groups()
2892
2925
  now = datetime.now(UTC)
2893
- reset = now.replace(
2894
- hour=int(match[1]), minute=int(match[2]), second=0, microsecond=0
2895
- )
2896
- # The error names only a time of day: take the one nearest to now.
2897
- if reset < now - timedelta(hours=12):
2898
- reset += timedelta(days=1)
2899
- elif reset > now + timedelta(hours=12):
2900
- reset -= timedelta(days=1)
2926
+ if hour is None:
2927
+ reset = now + timedelta(
2928
+ seconds=int(amount) * {"second": 1, "minute": 60, "hour": 3600}[unit]
2929
+ )
2930
+ elif month is not None:
2931
+ reset = datetime.strptime(
2932
+ f"{now.year} {month} {day} {hour}:{minute}", "%Y %b %d %H:%M"
2933
+ ).replace(tzinfo=UTC)
2934
+ # The date carries no year: take the one nearest to now.
2935
+ if reset < now - timedelta(days=180):
2936
+ reset = reset.replace(year=now.year + 1)
2937
+ elif reset > now + timedelta(days=180):
2938
+ reset = reset.replace(year=now.year - 1)
2939
+ else:
2940
+ reset = now.replace(
2941
+ hour=int(hour), minute=int(minute), second=0, microsecond=0
2942
+ )
2943
+ # A time of day alone: take the one nearest to now.
2944
+ if reset < now - timedelta(hours=12):
2945
+ reset += timedelta(days=1)
2946
+ elif reset > now + timedelta(hours=12):
2947
+ reset -= timedelta(days=1)
2901
2948
  return max(0.0, (reset - now).total_seconds()) + cls._LIMIT_MARGIN_SECONDS
2902
2949
 
2903
2950
  def _command(self, request: RunRequest, export_path: Path) -> list[str]:
@@ -6125,10 +6172,19 @@ def _prepare_claude_environment(environment: dict[str, str]) -> None:
6125
6172
  if settings.is_file() and not (config / ".claude.json").exists():
6126
6173
  os.replace(settings, config / ".claude.json")
6127
6174
  environment["CLAUDE_CONFIG_DIR"] = os.fspath(config)
6175
+ # The run signs in with the user's own credentials file and refresh
6176
+ # lock, so Claude coordinates its refreshes with the user's sessions;
6177
+ # a host run already has the user's account, so this exposes nothing
6178
+ # new. A private copy left from before would only go stale.
6179
+ environment.setdefault(
6180
+ "CLAUDE_SECURESTORAGE_CONFIG_DIR", os.fspath(source_config)
6181
+ )
6182
+ (config / ".credentials.json").unlink(missing_ok=True)
6128
6183
  return
6129
6184
  environment["HOME"] = os.fspath(destination)
6130
6185
  # The run uses the private copy in its standard place.
6131
6186
  environment.pop("CLAUDE_CONFIG_DIR", None)
6187
+ environment.pop("CLAUDE_SECURESTORAGE_CONFIG_DIR", None)
6132
6188
 
6133
6189
 
6134
6190
  def _seed_claude_home(
@@ -6145,15 +6201,33 @@ def _seed_claude_home(
6145
6201
  shutil.copytree(
6146
6202
  source_config,
6147
6203
  private_config,
6204
+ # Runtime records of the user's own sessions, not configuration:
6205
+ # jobs/ alone can hold gigabytes of background-job scratch.
6148
6206
  ignore=shutil.ignore_patterns(
6149
6207
  "*.lock",
6208
+ "backups",
6209
+ "cache",
6210
+ "daemon",
6211
+ "daemon-*",
6212
+ "daemon.*",
6150
6213
  "debug",
6214
+ "downloads",
6215
+ "feedback",
6216
+ "file-history",
6151
6217
  "history.jsonl",
6218
+ "ide",
6219
+ "jobs",
6220
+ "paste-cache",
6152
6221
  "projects",
6153
6222
  "session-env",
6223
+ "sessions",
6154
6224
  "shell-snapshots",
6225
+ "stats-cache.json",
6155
6226
  "statsig",
6227
+ "telemetry",
6156
6228
  "todos",
6229
+ "usage-limits",
6230
+ "usage-limits.json",
6157
6231
  ),
6158
6232
  copy_function=_copy_unless_removed,
6159
6233
  )
@@ -6161,6 +6235,122 @@ def _seed_claude_home(
6161
6235
  shutil.copy2(source_settings, destination / ".claude.json")
6162
6236
 
6163
6237
 
6238
+ class _ClaudeLogin:
6239
+ """Keep a task's private Claude sign-in in step with the user's.
6240
+
6241
+ A Claude sign-in refreshes with a rotating refresh token: each refresh
6242
+ returns a new one and invalidates the old. Claude processes that share one
6243
+ credentials file follow each other's refreshes, but a task's private copy
6244
+ does not, so a refresh on either side used to break the other: the run
6245
+ failed with "OAuth session expired", or the user's own sessions did. AOP
6246
+ therefore copies whichever side refreshed onto the other, before the run,
6247
+ within a fraction of a second while it runs, and after it ends: a refresh
6248
+ revokes the previous access token at once, and Claude retries a rejected
6249
+ request within a second, so a slower copy arrives too late.
6250
+ """
6251
+
6252
+ POLL_SECONDS = 0.1
6253
+
6254
+ @contextmanager
6255
+ def watching(self) -> Iterator[None]:
6256
+ """Copy refreshes between the two sign-ins while the block runs."""
6257
+ stopped = threading.Event()
6258
+
6259
+ def watch() -> None:
6260
+ seen = None
6261
+ while not stopped.wait(self.POLL_SECONDS):
6262
+ current = (self._stamp(self.source), self._stamp(self.private))
6263
+ if current != seen:
6264
+ self.sync()
6265
+ seen = (self._stamp(self.source), self._stamp(self.private))
6266
+
6267
+ watcher = threading.Thread(target=watch, daemon=True)
6268
+ watcher.start()
6269
+ try:
6270
+ yield
6271
+ finally:
6272
+ stopped.set()
6273
+ watcher.join(timeout=5)
6274
+ self.sync()
6275
+
6276
+ @staticmethod
6277
+ def _stamp(path: Path) -> tuple[int, int] | None:
6278
+ try:
6279
+ status = path.stat()
6280
+ except OSError:
6281
+ return None
6282
+ return status.st_mtime_ns, status.st_ino
6283
+
6284
+ def __init__(self, source_config: Path, private_config: Path):
6285
+ self.source = source_config / ".credentials.json"
6286
+ self.private = private_config / ".credentials.json"
6287
+ self.shared: str | None = None
6288
+
6289
+ def start(self) -> None:
6290
+ """Give the private copy the user's sign-in unless it holds a newer one."""
6291
+ source, private = self._token(self.source), self._token(self.private)
6292
+ if source is not None and (
6293
+ private is None
6294
+ or _claude_login_expiry(self.source) > _claude_login_expiry(self.private)
6295
+ ):
6296
+ _replace_private_file(self.source, self.private)
6297
+ private = source
6298
+ self.shared = private
6299
+
6300
+ def sync(self) -> None:
6301
+ """Copy a refresh made on one side to the other."""
6302
+ if self.shared is None:
6303
+ return
6304
+ source, private = self._token(self.source), self._token(self.private)
6305
+ try:
6306
+ if private is not None and private != self.shared and source == self.shared:
6307
+ # The run refreshed, which invalidated the user's refresh token.
6308
+ _replace_private_file(self.private, self.source)
6309
+ self.shared = private
6310
+ elif source is not None and source != self.shared:
6311
+ # The user's sessions refreshed, or the user signed in again.
6312
+ _replace_private_file(self.source, self.private)
6313
+ self.shared = source
6314
+ except AOPError:
6315
+ pass # A later check, or the next run, tries again.
6316
+
6317
+ @staticmethod
6318
+ def _token(credentials: Path) -> str | None:
6319
+ """The sign-in's refresh token, or None when there is no usable one."""
6320
+ try:
6321
+ token = json.loads(credentials.read_text())["claudeAiOauth"]["refreshToken"]
6322
+ except (OSError, ValueError, KeyError, TypeError):
6323
+ return None
6324
+ return token if isinstance(token, str) and token else None
6325
+
6326
+
6327
+ def _replace_private_file(source: Path, target: Path) -> None:
6328
+ """Replace `target` with a copy of `source` atomically, readable by the user only."""
6329
+ temporary = target.with_name(f".{target.name}.{uuid.uuid4().hex}.tmp")
6330
+ try:
6331
+ shutil.copyfile(source, temporary)
6332
+ temporary.chmod(0o600)
6333
+ os.replace(temporary, target)
6334
+ except OSError as error:
6335
+ raise AOPError(f"could not update a Claude sign-in: {error}") from error
6336
+ finally:
6337
+ temporary.unlink(missing_ok=True)
6338
+
6339
+
6340
+ def _claude_login_expiry(credentials: Path) -> float:
6341
+ """When a Claude credentials file's login expires, or else when it was written."""
6342
+ try:
6343
+ expires = json.loads(credentials.read_text())["claudeAiOauth"]["expiresAt"]
6344
+ if isinstance(expires, (int, float)) and not isinstance(expires, bool):
6345
+ return expires / 1000
6346
+ except (OSError, ValueError, KeyError, TypeError):
6347
+ pass
6348
+ try:
6349
+ return credentials.stat().st_mtime
6350
+ except OSError:
6351
+ return 0.0
6352
+
6353
+
6164
6354
  def _copy_unless_removed(source: str, target: str) -> None:
6165
6355
  """Copy a file, skipping one that a running Claude removed after listing."""
6166
6356
  try:
@@ -6709,6 +6899,80 @@ def _hermes_runtime_environment(
6709
6899
  _write_hermes_auth(shared_auth, task_auth)
6710
6900
 
6711
6901
 
6902
+ ADMIT_TIMEOUT_SECONDS = 60.0
6903
+
6904
+
6905
+ def admission_config_path() -> Path:
6906
+ base = os.environ.get("XDG_CONFIG_HOME") or Path.home() / ".config"
6907
+ return Path(base) / "aop" / "config.toml"
6908
+
6909
+
6910
+ def _admit_command(explicit: str | None) -> tuple[str | None, str | None]:
6911
+ """The admission command and where it was set, most specific first.
6912
+
6913
+ --admit-command, then AOP_ADMIT_COMMAND, then admit_command in the user's
6914
+ AOP config. An empty value at any level turns admission off from there.
6915
+ """
6916
+ if explicit is not None:
6917
+ return explicit, "--admit-command"
6918
+ if "AOP_ADMIT_COMMAND" in os.environ:
6919
+ return os.environ["AOP_ADMIT_COMMAND"], "AOP_ADMIT_COMMAND"
6920
+ path = admission_config_path()
6921
+ try:
6922
+ config = tomllib.loads(path.read_text())
6923
+ except FileNotFoundError:
6924
+ return None, None
6925
+ except (OSError, tomllib.TOMLDecodeError) as error:
6926
+ raise AdmissionRefused(
6927
+ f"admission refused: could not read {path}: {error}",
6928
+ {"by": os.fspath(path), "exit_code": None},
6929
+ ) from error
6930
+ unknown = set(config) - {"admit_command"}
6931
+ command = config.get("admit_command")
6932
+ if unknown or (command is not None and not isinstance(command, str)):
6933
+ raise AdmissionRefused(
6934
+ f"admission refused: {path} may set only admit_command, as a string",
6935
+ {"by": os.fspath(path), "exit_code": None},
6936
+ )
6937
+ return command, (None if command is None else os.fspath(path))
6938
+
6939
+
6940
+ def _backend(request: RunRequest) -> str | None:
6941
+ """The inference provider a run will spend, where AOP can tell."""
6942
+ if request.provider == "hermes":
6943
+ try:
6944
+ return _hermes_inference_provider(request)
6945
+ except AOPError:
6946
+ return None
6947
+ if request.provider == "opencode":
6948
+ prefix, separator, _ = (request.model or "").partition("/")
6949
+ return prefix if separator else None
6950
+ if request.provider == "dsh":
6951
+ return request.inference_provider or "deepseek-official"
6952
+ return request.inference_provider
6953
+
6954
+
6955
+ def _credential_home(provider: str, environment: dict[str, str]) -> str | None:
6956
+ """The harness home a run's credentials come from, for admission commands."""
6957
+ homes: dict[str, Callable[[], Path | None]] = {
6958
+ "claude": lambda: _claude_source(environment)[0],
6959
+ "codex": lambda: _codex_source_home(environment),
6960
+ "cursor": lambda: _cursor_source_home(environment),
6961
+ "devin": lambda: _devin_source_data(environment),
6962
+ "grok": lambda: _grok_source_home(environment),
6963
+ "agy": lambda: _agy_source_dir(environment),
6964
+ "hermes": lambda: _hermes_source_home(environment),
6965
+ "muse": lambda: _muse_source_config(environment),
6966
+ "opencode": lambda: _opencode_source_config(environment),
6967
+ "zcode": lambda: zcode.source_home(environment),
6968
+ }
6969
+ try:
6970
+ home = homes[provider]() if provider in homes else None
6971
+ except (AOPError, OSError):
6972
+ return None
6973
+ return None if home is None else os.fspath(home)
6974
+
6975
+
6712
6976
  def adapter_for(agent: str) -> AgentAdapter:
6713
6977
  if agent == "codex":
6714
6978
  return CodexAdapter()
@@ -6775,6 +7039,7 @@ class AgentRunner:
6775
7039
  workdir: str | os.PathLike[str] | None = None,
6776
7040
  native_state: bool = False,
6777
7041
  reminders: str | None = None,
7042
+ admit_command: str | None = None,
6778
7043
  ) -> RunResult:
6779
7044
  self._validate_mode(mode)
6780
7045
  workdir = self._validated_workdir(workdir, profile)
@@ -6852,6 +7117,7 @@ class AgentRunner:
6852
7117
  native_state=native_state,
6853
7118
  reminders=reminders,
6854
7119
  )
7120
+ request = self._admitted(request, admit_command)
6855
7121
  worktree = (
6856
7122
  Worktree(task=task, path=Path(workdir), head="")
6857
7123
  if workdir is not None
@@ -6886,6 +7152,7 @@ class AgentRunner:
6886
7152
  artifacts: Sequence[str] = (),
6887
7153
  input_paths: Sequence[str | os.PathLike[str]] | None = None,
6888
7154
  prompt_parts: Sequence[PromptPart] | None = None,
7155
+ admit_command: str | None = None,
6889
7156
  _task_lock_held: bool = False,
6890
7157
  ) -> RunResult:
6891
7158
  parent_request = self.store.load_request(run_id)
@@ -6995,6 +7262,7 @@ class AgentRunner:
6995
7262
  native_state=parent_request.native_state,
6996
7263
  reminders=parent_request.reminders,
6997
7264
  )
7265
+ request = self._admitted(request, admit_command)
6998
7266
  return self._execute(
6999
7267
  request,
7000
7268
  worktree,
@@ -7003,6 +7271,79 @@ class AgentRunner:
7003
7271
  task_lock_held=_task_lock_held,
7004
7272
  )
7005
7273
 
7274
+ def _admitted(self, request: RunRequest, command: str | None) -> RunRequest:
7275
+ """Ask the admission command whether this run may start.
7276
+
7277
+ The command comes from --admit-command or AOP_ADMIT_COMMAND; an empty
7278
+ value means no admission. It receives the run as JSON on stdin and as
7279
+ AOP_ADMIT_* variables, and exit status 0 admits the run. Anything
7280
+ else, including a command that cannot run or does not finish in time,
7281
+ refuses it before any task state exists.
7282
+ """
7283
+ command, source = _admit_command(command)
7284
+ if command is None:
7285
+ return request
7286
+ if not command.strip():
7287
+ # An explicit opt-out stays visible in the run's records.
7288
+ return replace(request, admission={"skipped": True, "by": source})
7289
+ route = {
7290
+ "run_id": request.run_id,
7291
+ "parent_run_id": request.parent_run_id,
7292
+ "session_id": request.session_id,
7293
+ "agent": request.provider,
7294
+ "model": request.model,
7295
+ "effort": request.effort,
7296
+ "inference_provider": request.inference_provider,
7297
+ "profile": request.profile,
7298
+ "task": request.task,
7299
+ "workdir": request.workdir,
7300
+ # The run's environment is a copy of this one, and its credentials
7301
+ # are seeded from this home.
7302
+ "credential_home": _credential_home(request.provider, dict(os.environ)),
7303
+ "backend": _backend(request),
7304
+ "timeout_seconds": request.timeout_seconds,
7305
+ }
7306
+ environment = {
7307
+ **os.environ,
7308
+ # stdin carries every field in full; the variables are a
7309
+ # convenience, bounded so no value can exceed the exec limits.
7310
+ **{
7311
+ f"AOP_ADMIT_{key.upper()}": "" if value is None else str(value)[:4096]
7312
+ for key, value in route.items()
7313
+ },
7314
+ }
7315
+ try:
7316
+ argv = shlex.split(command)
7317
+ completed = subprocess.run(
7318
+ argv,
7319
+ input=json.dumps(route),
7320
+ capture_output=True,
7321
+ text=True,
7322
+ env=environment,
7323
+ timeout=ADMIT_TIMEOUT_SECONDS,
7324
+ )
7325
+ exit_code, stdout, stderr = (
7326
+ completed.returncode, completed.stdout, completed.stderr
7327
+ )
7328
+ except (OSError, ValueError) as error:
7329
+ exit_code, stdout, stderr = None, "", str(error)
7330
+ except subprocess.TimeoutExpired:
7331
+ exit_code, stdout = None, ""
7332
+ stderr = f"did not finish within {ADMIT_TIMEOUT_SECONDS:g} seconds"
7333
+ decision = {
7334
+ "by": source,
7335
+ "command": command,
7336
+ "exit_code": exit_code,
7337
+ "stdout": stdout.strip()[-4000:],
7338
+ "stderr": stderr.strip()[-4000:],
7339
+ }
7340
+ if exit_code != 0:
7341
+ reason = decision["stdout"] or decision["stderr"] or f"exit status {exit_code}"
7342
+ raise AdmissionRefused(
7343
+ f"admission refused by {command!r}: {reason.splitlines()[-1]}", decision
7344
+ )
7345
+ return replace(request, admission=decision)
7346
+
7006
7347
  def _execute(
7007
7348
  self,
7008
7349
  request: RunRequest,
@@ -7332,6 +7673,13 @@ class AgentRunner:
7332
7673
  inference_provider=request.inference_provider or result.inference_provider,
7333
7674
  inference_route=request.inference_route,
7334
7675
  inputs=request.inputs,
7676
+ project_dir=(
7677
+ request.workdir
7678
+ if request.workdir is not None
7679
+ else None
7680
+ if request.profile == "sealed"
7681
+ else os.fspath(self.manager.root)
7682
+ ),
7335
7683
  )
7336
7684
  if result.succeeded and request.artifacts:
7337
7685
  result = _archive_artifacts(result, request.artifacts, output_dir, run_dir)
@@ -7672,25 +8020,40 @@ def _path_provenance(path: Path) -> dict[str, object] | None:
7672
8020
  }
7673
8021
 
7674
8022
 
8023
+ ARTIFACT_ALTERNATIVES = "|"
8024
+
8025
+
8026
+ def artifact_alternatives(declaration: str) -> tuple[str, ...]:
8027
+ """The paths a declaration accepts: one, or alternatives such as "a.svg|a.png"."""
8028
+ return tuple(declaration.split(ARTIFACT_ALTERNATIVES))
8029
+
8030
+
7675
8031
  def normalize_artifacts(artifacts: Sequence[str]) -> tuple[str, ...]:
8032
+ """Validate artifact declarations; "a|b" declares alternatives, of which at
8033
+ least one must be written."""
7676
8034
  normalized: list[str] = []
7677
- for value in artifacts:
7678
- if not value:
7679
- raise AOPError("artifact path must not be empty")
7680
- if "\0" in value:
7681
- raise AOPError("artifact path must not contain a null byte")
7682
- path = PurePosixPath(value)
7683
- if path.is_absolute() or path == PurePosixPath(".") or ".." in path.parts:
7684
- raise AOPError(f"artifact path must be relative and contained: {value}")
7685
- logical_path = path.as_posix()
7686
- if logical_path in normalized:
7687
- raise AOPError(f"duplicate artifact path: {logical_path}")
7688
- for existing in map(PurePosixPath, normalized):
7689
- if path in existing.parents or existing in path.parents:
7690
- raise AOPError(
7691
- f"overlapping artifact paths: {existing.as_posix()} and {logical_path}"
7692
- )
7693
- normalized.append(logical_path)
8035
+ paths: list[str] = []
8036
+ for declaration in artifacts:
8037
+ alternatives = []
8038
+ for value in artifact_alternatives(declaration):
8039
+ if not value:
8040
+ raise AOPError("artifact path must not be empty")
8041
+ if "\0" in value:
8042
+ raise AOPError("artifact path must not contain a null byte")
8043
+ path = PurePosixPath(value)
8044
+ if path.is_absolute() or path == PurePosixPath(".") or ".." in path.parts:
8045
+ raise AOPError(f"artifact path must be relative and contained: {value}")
8046
+ logical_path = path.as_posix()
8047
+ if logical_path in paths:
8048
+ raise AOPError(f"duplicate artifact path: {logical_path}")
8049
+ for existing in map(PurePosixPath, paths):
8050
+ if path in existing.parents or existing in path.parents:
8051
+ raise AOPError(
8052
+ f"overlapping artifact paths: {existing.as_posix()} and {logical_path}"
8053
+ )
8054
+ paths.append(logical_path)
8055
+ alternatives.append(logical_path)
8056
+ normalized.append(ARTIFACT_ALTERNATIVES.join(alternatives))
7694
8057
  return tuple(normalized)
7695
8058
 
7696
8059
 
@@ -7982,7 +8345,12 @@ def _run_prompt(
7982
8345
  def _artifact_prompt(prompt: str, artifacts: tuple[str, ...], output_dir: Path) -> str:
7983
8346
  if not artifacts:
7984
8347
  return prompt
7985
- paths = "\n".join(f"- {artifact}" for artifact in artifacts)
8348
+ paths = "\n".join(
8349
+ f"- {artifact}"
8350
+ if len(artifact_alternatives(artifact)) == 1
8351
+ else "- at least one of: " + ", ".join(artifact_alternatives(artifact))
8352
+ for artifact in artifacts
8353
+ )
7986
8354
  return (
7987
8355
  f"{prompt.rstrip()}\n\n"
7988
8356
  "AOP artifact contract:\n"
@@ -8002,23 +8370,39 @@ def _archive_artifacts(
8002
8370
  output_dir: Path,
8003
8371
  run_dir: Path,
8004
8372
  ) -> RunResult:
8005
- try:
8006
- sources: list[tuple[str, Path]] = []
8007
- for declaration in declarations:
8008
- sources.extend(_collect_artifact(declaration, output_dir))
8009
- artifacts = tuple(
8010
- _archive_artifact(logical_path, source, run_dir)
8011
- for logical_path, source in sources
8012
- )
8013
- except (AOPError, OSError) as error:
8014
- shutil.rmtree(run_dir / "artifacts", ignore_errors=True)
8015
- diagnostic = (
8016
- str(error)
8017
- if isinstance(error, AOPError)
8018
- else f"could not archive artifacts: {error}"
8019
- )
8020
- return replace(result, error=diagnostic, artifacts=())
8021
- return replace(result, artifacts=artifacts)
8373
+ """Archive each declared artifact that was written correctly.
8374
+
8375
+ One missing or invalid artifact fails the run but does not discard the
8376
+ others. A declaration with alternatives is satisfied by any of them, and
8377
+ every alternative that was written is archived.
8378
+ """
8379
+ artifacts: list[RunArtifact] = []
8380
+ problems: list[str] = []
8381
+ for declaration in declarations:
8382
+ alternatives = artifact_alternatives(declaration)
8383
+ missing = []
8384
+ for logical_path in alternatives:
8385
+ try:
8386
+ sources = _collect_artifact(logical_path, output_dir)
8387
+ artifacts.extend(
8388
+ _archive_artifact(path, source, run_dir) for path, source in sources
8389
+ )
8390
+ except AOPError as error:
8391
+ if str(error) == f"artifact {logical_path}: missing":
8392
+ missing.append(logical_path)
8393
+ else:
8394
+ problems.append(str(error))
8395
+ except OSError as error:
8396
+ problems.append(f"artifact {logical_path}: could not archive: {error}")
8397
+ if len(missing) == len(alternatives):
8398
+ problems.append(
8399
+ f"artifact {missing[0]}: missing"
8400
+ if len(missing) == 1
8401
+ else f"artifact {declaration}: missing (none of {', '.join(missing)})"
8402
+ )
8403
+ if problems and result.error is None:
8404
+ result = replace(result, error="; ".join(problems))
8405
+ return replace(result, artifacts=tuple(artifacts))
8022
8406
 
8023
8407
 
8024
8408
  def _collect_artifact(
@@ -88,6 +88,14 @@ class AOPError(RuntimeError):
88
88
  """A user-actionable orchestration error."""
89
89
 
90
90
 
91
+ class AdmissionRefused(AOPError):
92
+ """The run's admission command declined it, so the harness never started."""
93
+
94
+ def __init__(self, message: str, decision: dict[str, object]):
95
+ super().__init__(message)
96
+ self.decision = decision
97
+
98
+
91
99
  @dataclass(frozen=True)
92
100
  class Worktree:
93
101
  task: str
@@ -296,13 +304,43 @@ class WorktreeManager:
296
304
  metadata_path = self._metadata_path(task)
297
305
  if metadata_path.exists():
298
306
  metadata_path.unlink()
299
- shutil.rmtree(self.state_dir / "overlays" / task, ignore_errors=True)
300
- shutil.rmtree(
301
- self.state_dir / "provider-state" / task, ignore_errors=True
302
- )
303
- shutil.rmtree(self.state_dir / "scratch" / task, ignore_errors=True)
307
+ self._remove_private_dirs(task)
304
308
  return archived
305
309
 
310
+ def remove_private_state(self, task: str) -> list[Path]:
311
+ """Remove the private state of a task that has no worktree, such as a
312
+ --workdir run's harness home and scratch, and return what was removed.
313
+
314
+ The caller's own directory is never touched.
315
+ """
316
+ from .locks import exclusive_lock, task_lock_path
317
+
318
+ with exclusive_lock(task_lock_path(self.state_dir, task), f"task {task}"):
319
+ return self._remove_private_dirs(task)
320
+
321
+ def private_state(self, task: str) -> list[Path]:
322
+ """The private directories a task has, without removing them."""
323
+ self._validate_private_task(task)
324
+ return [
325
+ path
326
+ for path in (
327
+ self.state_dir / kind / task
328
+ for kind in ("overlays", "provider-state", "scratch")
329
+ )
330
+ if path.exists()
331
+ ]
332
+
333
+ def _remove_private_dirs(self, task: str) -> list[Path]:
334
+ removed = self.private_state(task)
335
+ for path in removed:
336
+ shutil.rmtree(path, ignore_errors=True)
337
+ return removed
338
+
339
+ def _validate_private_task(self, task: str) -> None:
340
+ # A --workdir run's task may be a generated UUID, which is also valid.
341
+ if "/" in task or task in {"", ".", ".."}:
342
+ raise AOPError(f"invalid task name: {task!r}")
343
+
306
344
  def metadata(self, task: str) -> TaskMetadata:
307
345
  self._validate_task(task)
308
346
  path = self._metadata_path(task)