Repository navigation
Expand file tree
/
Copy pathtest_explore_result_writeback.py
More file actions
1141 lines (1084 loc) · 50.3 KB
/
Copy pathtest_explore_result_writeback.py
File metadata and controls
1141 lines (1084 loc) · 50.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from canonical_authority_fixture import assert_todo_read_unchanged, read_fixture_todos
from copy import deepcopy
import json
import pytest
from tests.capabilities.test_explore_turn_context import registry
from loopx.control_plane.effect_runtime import EffectRuntimeRejected
from loopx.todos import add_goal_todo, update_goal_todo, list_goal_todos
from loopx.capabilities.explore.result_writeback import (
prepare_result_attachment,
deliver_result_attachment,
)
from loopx.capabilities.explore.turn_context import explore_turn_context
from loopx.capabilities.explore.result_log import (
explore_result_log_path,
load_explore_result_events,
)
ATTACHMENT = dict(
schema_version="explore_result_attachment_v0",
node_id="prefix-bound",
question="Does a finite prefix establish the tail bound?",
applicability="Finite prefix only; no uniform tail estimate.",
input_revision="fixture-v1",
observation="A divergent tail shares the tested prefix.",
interpretation="Use a uniform bound before transferring the result.",
status="refuted",
evidence_refs=["validation:counterexample-1"],
)
def fixture(tmp_path, enabled=True):
path = registry(tmp_path, graph=enabled, planning=enabled)
todo = add_goal_todo(
registry_path=path,
goal_id="research",
text="Establish the tail bound",
role="agent",
claimed_by="worker",
agent_id="worker",
)
return dict(
registry_path=path,
runtime_root=tmp_path / "runtime",
goal_id="research",
agent_id="worker",
todo_id=todo["todo_id"],
turn_instance_id="turn-one",
)
def payload():
return dict(
explore_result=deepcopy(ATTACHMENT),
generated_at="2026-01-01T00:00:00Z",
appended=True,
dry_run=False,
settlement_identity={"effect_id": "research:worker:turn-one"},
)
def test_real_result_link_read_and_replay(tmp_path):
args = fixture(tmp_path)
context = explore_turn_context(**{
key: args[key] for key in ("registry_path", "runtime_root", "goal_id", "agent_id")
})
attachment = context["graph"]["result_attachment_template"]
with pytest.raises((ValueError, EffectRuntimeRejected)):
prepare_result_attachment(attachment, **args)
assert not explore_result_log_path(args["runtime_root"], "research").exists()
assert set(attachment) == set(ATTACHMENT) - {"node_id"}
attachment.update(ATTACHMENT)
prepare_result_attachment(attachment, **args)
assert not explore_result_log_path(args["runtime_root"], "research").exists()
first = deliver_result_attachment(payload=payload(), **args)
assert first["ok"], first
second = deliver_result_attachment(payload=payload(), **args)
assert second["ok"] and second["idempotent_replay"], second
events = load_explore_result_events(
explore_result_log_path(args["runtime_root"], "research")
)
assert len(events) == 2
supplement = payload()
supplement["explore_result_recorded_at"] = supplement["generated_at"]
supplement["generated_at"] = "2026-01-02T00:00:00Z"
assert deliver_result_attachment(payload=supplement, **args)["idempotent_replay"]
assert (
len(
load_explore_result_events(
explore_result_log_path(args["runtime_root"], "research")
)
)
== 2
)
context = explore_turn_context(
**{
key: args[key]
for key in ("registry_path", "runtime_root", "goal_id", "agent_id")
}
)
assert context["graph"]["writeback_results"][0]["finding_id"] == first["finding_id"]
assert (
ATTACHMENT["interpretation"]
in context["graph"]["writeback_results"][0]["summary"]
)
audit = context["harness"]["selected_branches"][0]["typed_evidence_audit"]
assert ATTACHMENT["node_id"] in audit["requested_node_refs"]
assert audit["findings"][0]["status"] == "refuted"
def test_todo_additive_owner_preserves_all_links_and_rejects_conflicting_intents(tmp_path):
args = fixture(tmp_path)
keys = {
key: args[key] for key in ("registry_path", "goal_id", "todo_id", "agent_id")
}
update_goal_todo(**keys, explore_result_node_refs=["prior"])
for _ in range(2):
update_goal_todo(**keys, append_explore_result_node_refs=["prefix-bound"])
rows = list_goal_todos(registry_path=args["registry_path"], goal_id="research")[
"todos"
]
assert rows[0]["explore_result_node_refs"] == ["prior", "prefix-bound"]
additions = [f"node{i}" for i in range(12)]
update_goal_todo(**keys, append_explore_result_node_refs=additions)
exact = list_goal_todos(registry_path=args["registry_path"], goal_id="research",
todo_id=args["todo_id"])["todo"]
assert exact["explore_result_node_refs"] == ["prior", "prefix-bound", *additions]
with pytest.raises(ValueError):
update_goal_todo(
**keys,
explore_result_node_refs=[],
append_explore_result_node_refs=["other"],
)
@pytest.mark.parametrize(
"mutation",
[
"disabled",
"foreign-agent",
"unknown-todo",
"missing-turn",
"invalid-evidence",
"unknown-field",
],
)
def test_invalid_attachment_never_writes_graph(tmp_path, mutation):
args = fixture(tmp_path, enabled=mutation != "disabled")
attachment = deepcopy(ATTACHMENT)
if mutation == "foreign-agent":
args["agent_id"] = "other"
if mutation == "unknown-todo":
args["todo_id"] = "todo_missing"
if mutation == "missing-turn":
args["turn_instance_id"] = ""
if mutation == "invalid-evidence":
attachment["evidence_refs"] = ["/private/raw.log"]
if mutation == "unknown-field":
attachment["score"] = 1
with pytest.raises((ValueError, EffectRuntimeRejected)):
prepare_result_attachment(attachment, **args)
assert not explore_result_log_path(args["runtime_root"], "research").exists()
def test_partial_link_failure_replays_without_duplicate_or_primary_rollback(
tmp_path, monkeypatch
):
args = fixture(tmp_path)
import loopx.capabilities.explore.result_writeback as module
original = module.update_goal_todo
def interrupt(**kwargs):
if not kwargs.get("dry_run"):
raise OSError("simulated interruption after graph append")
return original(**kwargs)
monkeypatch.setattr(module, "update_goal_todo", interrupt)
first = deliver_result_attachment(payload=payload(), **args)
assert not first["ok"] and first["primary_committed"] and first["retryable"]
assert first["graph"]["appended_event_count"] == 2
monkeypatch.setattr(module, "update_goal_todo", original)
second = deliver_result_attachment(payload=payload(), **args)
assert second["ok"] and second["graph"]["appended_event_count"] == 0
@pytest.mark.parametrize("case", ["disabled", "null", "invalid", "blank-status", "conflict", "cross-result", "off-without-result",
"path-disabled", "path-missing", "path-no-evidence", "path-overflow"])
def test_real_cli_inline_validation_precedes_primary_commit(tmp_path, case):
from canonical_authority_fixture import initialize_canonical_authority
from loopx.control_plane.coordination.runtime_shadow import (
build_todo_runtime_shadow_projection,
)
from tests.control_plane.test_quota_settlement_cli import (
_write_fixture, _run_cli, GOAL_ID, AGENT_ID, TODO_ID, TURN_ID,
)
project, runtime, path = _write_fixture(tmp_path)
config = json.loads(path.read_text())
config["goals"][0]["explore_graph"] = {"enabled": case not in {"disabled", "off-without-result", "path-disabled"}}
path.write_text(json.dumps(config))
todos = read_fixture_todos(project / config["goals"][0]["state_file"])
initialize_canonical_authority(
runtime,
GOAL_ID,
build_todo_runtime_shadow_projection(
goal_id=GOAL_ID, todos=todos, handoff_mode="soft_claim"
),
state_path=project / config["goals"][0]["state_file"],
provider="file",
)
rc, claimed = _run_cli(path, runtime, "todo", "claim", "--goal-id", GOAL_ID,
"--todo-id", TODO_ID, "--agent-id", AGENT_ID,
"--claimed-by", AGENT_ID, cwd=project)
assert rc == 0, claimed
binding = ("--goal-id", GOAL_ID, "--agent-id", AGENT_ID, "--todo-id", TODO_ID,
"--turn-instance-id", TURN_ID)
rc, guard = _run_cli(path, runtime, "quota", "should-run", "--codex-app",
*binding, cwd=project)
assert rc == 0, guard
packet = {"schema_version": "goal_vision_replan_contract_v0",
"state": "vision_patch_proposed",
"vision_patch": {"acceptance_summary": "Require a uniform tail bound."}}
if case != "off-without-result":
packet["explore_result"] = deepcopy(ATTACHMENT)
if case == "null":
packet["explore_result"] = None
if case == "invalid":
packet["explore_result"]["evidence_refs"] = []
if case == "blank-status":
packet["explore_result"]["status"] = ""
if case == "cross-result":
attachment = packet["explore_result"]
attachment["evidence_refs"] = ["result:a", "result:b"]
attachment["execution_observation"] = {
"schema_version": "explore_execution_observation_v0",
"candidate_sha256": "a" * 64, "input_revision": attachment["input_revision"],
"planned_method_id": "probe", "executed_method_id": "probe", "change_reason": None,
"elapsed_seconds": 1, "limit_seconds": 10, "search_iterations": None,
"stop_reason": "process_exit:0", "evidence_refs": ["result:a"],
"result_ref": "result:a", "result_candidate_sha256": "a" * 64,
}
attachment["measurements"] = [{
"metric": "quality", "value": 8, "unit": "points", "higher_is_better": True,
"comparison_scope": "validation:budget", "input_revision": attachment["input_revision"],
"evaluation_ref": "evaluator:v1", "candidate_ref": "candidate:b", "evidence_ref": "result:b",
}]
if case.startswith("path-"):
packet["explore_result"] = {
key: ATTACHMENT[key] for key in
("node_id", "question", "applicability", "input_revision", "status")
}
packet["explore_result"]["schema_version"] = "explore_result_from_path_delta_v0"
if case != "path-missing":
packet["path_delta"] = {
"schema_version": "goal_path_delta_v0", "outcome": "continue",
"prior_assumption": "A finite prefix might bound the tail.",
"observed_reality": ATTACHMENT["observation"],
"changed": [ATTACHMENT["interpretation"]],
"evidence_refs": ATTACHMENT["evidence_refs"],
}
if case == "path-no-evidence":
packet["path_delta"]["evidence_refs"] = []
if case == "path-overflow":
packet["path_delta"]["observed_reality"] = "x" * 321
source = tmp_path / "vision.json"
source.write_text(json.dumps(packet))
extra = ()
if case == "conflict":
other = tmp_path / "other.json"
other.write_text(json.dumps({**ATTACHMENT, "interpretation": "Transfer without a uniform bound."}))
extra = ("--explore-result-json", str(other))
index = runtime / "goals" / GOAL_ID / "runs" / "index.jsonl"
before = index.read_bytes() if index.exists() else b""
rc, result = _run_cli(path, runtime, "refresh-state", *binding,
"--classification", "validated_change", "--delivery-batch-scale", "implementation",
"--delivery-outcome", "outcome_progress", "--no-global-sync",
"--suppress-external-sinks", "--agent-vision-json", str(source), *extra, cwd=project)
if case == "off-without-result":
assert rc == 0 and result["appended"], result
assert "explore_result_delivery" not in result
rc, context = _run_cli(path, runtime, "explore", "turn-context", "--goal-id", GOAL_ID,
"--agent-id", AGENT_ID, cwd=project)
assert rc == 0 and context["graph"] is None and context["harness"] is None
else:
assert rc == 1 and not result["appended"], result
assert (index.read_bytes() if index.exists() else b"") == before
if case == "cross-result":
assert "executed candidate's result reference" in result["error"]
assert not explore_result_log_path(runtime, GOAL_ID).exists()
@pytest.mark.parametrize("terminal", [False, True])
@pytest.mark.parametrize("interrupted", [False, True])
@pytest.mark.parametrize("source_mode", ["file", "vision", "both", "path_delta", "wide_path_delta", "linked_path_delta"])
@pytest.mark.parametrize("provider", ["file", "sqlite"])
def test_real_cli_normal_writeback_persists_attachment_and_rejects_conflicting_retry(
tmp_path, terminal, interrupted, source_mode, provider, monkeypatch, capsys,
):
from canonical_authority_fixture import initialize_canonical_authority, isolate_sqlite_runtime
from loopx.control_plane.coordination.runtime_shadow import (
build_todo_runtime_shadow_projection,
)
from loopx.control_plane.work_items.task_lease import acquire_task_lease
from loopx.rollout_event_log import rollout_event_log_path
from tests.control_plane.test_quota_settlement_cli import (
_write_fixture,
_run_cli,
GOAL_ID,
AGENT_ID,
TODO_ID,
TURN_ID,
)
isolate_sqlite_runtime(tmp_path, monkeypatch)
project, runtime, path = _write_fixture(tmp_path)
config = json.loads(path.read_text())
config["goals"][0]["explore_graph"] = {"enabled": True}
config["goals"][0]["spawn_policy"] = {
"allowed": False,
"explore_harness": {"enabled": True},
}
path.write_text(json.dumps(config))
todos = read_fixture_todos(project / config["goals"][0]["state_file"])
if source_mode == "linked_path_delta":
target = next(todo for todo in todos if todo["todo_id"] == TODO_ID)
target["explore_result_node_refs"] = [ATTACHMENT["node_id"]]
from loopx.capabilities.explore.result_log import (
append_explore_result_event, build_explore_node_event,
)
append_explore_result_event(explore_result_log_path(runtime, GOAL_ID),
build_explore_node_event(goal_id=GOAL_ID, node_id=ATTACHMENT["node_id"],
node_kind="question", title=ATTACHMENT["question"],
summary=ATTACHMENT["applicability"], status="open"))
initialize_canonical_authority(
runtime,
GOAL_ID,
build_todo_runtime_shadow_projection(
goal_id=GOAL_ID,
todos=todos,
handoff_mode="hard_lease" if terminal else "soft_claim",
),
state_path=project / config["goals"][0]["state_file"],
provider=provider,
)
terminal_proof = {}
if terminal:
lease = acquire_task_lease(
registry_path=path,
runtime_root=runtime,
goal_id=GOAL_ID,
todo_id=TODO_ID,
owner=AGENT_ID,
idempotency_key="cli-terminal-result",
)
terminal_proof = dict(
task_lease_idempotency_key="cli-terminal-result",
task_lease_expected_version=lease["lease"]["version"],
)
rc, claimed = _run_cli(
path,
runtime,
"todo",
"claim",
"--goal-id",
GOAL_ID,
"--todo-id",
TODO_ID,
"--agent-id",
AGENT_ID,
"--claimed-by",
AGENT_ID,
cwd=project,
)
assert rc == 0, claimed
if terminal:
from loopx.todos import complete_goal_todo
binding = (
"--goal-id",
GOAL_ID,
"--agent-id",
AGENT_ID,
"--todo-id",
TODO_ID,
"--turn-instance-id",
TURN_ID,
)
rc, guard = _run_cli(
path,
runtime,
"quota",
"should-run",
"--codex-app",
*binding,
cwd=project,
)
assert rc == 0, guard
if terminal:
complete_goal_todo(
registry_path=path,
goal_id=GOAL_ID,
todo_id=TODO_ID,
agent_id=AGENT_ID,
evidence="Synthetic validation passed",
**terminal_proof,
)
source = tmp_path / "result.json"
plan = guard["interaction_contract"]["cli_channel"]["settlement_plan"]
template = plan["ordered_steps"][1]["optional_attachments"][0]["attachment_template"]
assert set(template) == set(ATTACHMENT) - {"node_id"}
template.update(ATTACHMENT)
source.write_text(json.dumps(template))
vision = tmp_path / "vision.json"
packet = {
"schema_version": "goal_vision_replan_contract_v0",
"state": "vision_patch_proposed",
"vision_patch": {"acceptance_summary": "Require a uniform tail bound."},
"explore_result": template,
}
if source_mode in {"path_delta", "wide_path_delta", "linked_path_delta"}:
packet["path_delta"] = {
"schema_version": "goal_path_delta_v0", "outcome": "continue",
"prior_assumption": "A finite prefix might establish the bound.",
"observed_reality": ATTACHMENT["observation"],
"changed": [ATTACHMENT["interpretation"]],
"evidence_refs": ATTACHMENT["evidence_refs"],
}
packet["explore_result"] = {
key: ATTACHMENT[key] for key in
("node_id", "question", "applicability", "input_revision", "status")
}
packet["explore_result"]["schema_version"] = "explore_result_from_path_delta_v0"
if source_mode == "linked_path_delta":
for field in ("question", "applicability"):
del packet["explore_result"][field]
if source_mode == "wide_path_delta":
# Independent scope/tail oracle, exceeding the old 1200-character
# stored-summary cap and using all nine legal route entries.
packet["explore_result"]["input_revision"] = "r" * 160
packet["explore_result"]["applicability"] = "Finite prefix only; ".ljust(200, "a")
packet["path_delta"]["observed_reality"] = ATTACHMENT["observation"].ljust(320, "o")
for kind in ("retained", "changed", "stopped"):
packet["path_delta"][kind] = [f"{kind}-{i}: ".ljust(120, str(i)) for i in range(3)]
packet["path_delta"]["stopped"][-1] = "Do not transfer without a uniform tail bound.".rjust(120, "s")
packet["path_delta"]["evidence_refs"] = [*ATTACHMENT["evidence_refs"], ".loopx/evidence/probe.json"]
packet["explore_result"]["evidence_refs"] = ATTACHMENT["evidence_refs"]
vision.write_text(json.dumps(packet))
attachment_args = ()
if source_mode in {"file", "both"}:
attachment_args += ("--explore-result-json", str(source))
if source_mode in {"vision", "both", "path_delta", "wide_path_delta", "linked_path_delta"}:
attachment_args += ("--agent-vision-json", str(vision))
args = (
"refresh-state",
*binding,
"--classification",
"validated_change",
"--delivery-batch-scale",
"implementation",
"--delivery-outcome",
"outcome_progress",
"--no-global-sync",
"--suppress-external-sinks",
*attachment_args,
)
if interrupted:
from loopx.cli import main
import loopx.capabilities.explore.result_writeback as module
original = module.update_goal_todo
def interrupt_link(**kwargs):
if not kwargs.get("dry_run"):
raise OSError("simulated interruption after graph append")
return original(**kwargs)
# Exercise the real CLI/primary record and real owner, interrupting only
# the graph-to-Todo link. Recovery below runs in a fresh CLI process.
with monkeypatch.context() as patch:
patch.chdir(project)
patch.setattr(module, "update_goal_todo", interrupt_link)
rc = main(["--registry", str(path), "--runtime-root", str(runtime),
"--format", "json", *args])
first = json.loads(capsys.readouterr().out)
assert rc == 1 and first["appended"], first
assert first["explore_result_delivery"]["primary_committed"], first
assert not first["explore_result_delivery"]["ok"], first
assert "replay this exact refresh" in first["error"]
events = rollout_event_log_path(runtime, GOAL_ID)
assert not any(json.loads(line)["event_kind"] == "refresh_state"
for line in events.read_text().splitlines())
rc, premature = _run_cli(path, runtime, "quota", "spend-slot", *binding,
"--source", "heartbeat", "--slots", "1", "--execute", cwd=project)
assert rc != 0 and not premature.get("appended"), premature
else:
rc, first = _run_cli(path, runtime, *args, cwd=project)
assert rc == 0, first
assert first["explore_result_delivery"]["ok"] and first["appended"], first
index = runtime / "goals" / GOAL_ID / "runs" / "index.jsonl"
before = index.read_bytes()
rc, replay = _run_cli(path, runtime, *args, cwd=project)
assert rc == 0 and replay["receipt_repaired" if interrupted else "idempotent_replay"], replay
assert replay["explore_result_delivery"]["ok"], replay
assert index.read_bytes() == before
# Follow the owning hook's required read in a new process. Presence in the
# receiver packet is transport evidence, not a claim of model adoption.
import shlex
import subprocess
import sys
from loopx.capabilities.explore.turn_context import extend_turn_start_dispatch
hook = extend_turn_start_dispatch(
{}, registry_path=path, runtime_root=runtime, goal_id=GOAL_ID, agent_id=AGENT_ID,
)
command = shlex.split(hook["required_reads"][0]["command"])
read = subprocess.run([sys.executable, "-m", "loopx.cli", *command[1:]],
capture_output=True, text=True, check=True)
received = json.loads(read.stdout)
finding = received["graph"]["writeback_results"][0]
assert finding["finding_id"] == replay["explore_result_delivery"]["finding_id"]
assert ATTACHMENT["observation"] in finding["summary"]
if source_mode != "wide_path_delta":
assert ATTACHMENT["applicability"] in finding["summary"]
if source_mode == "wide_path_delta":
assert len(finding["summary"]) > 1200
assert packet["explore_result"]["input_revision"] in finding["summary"]
assert packet["explore_result"]["applicability"] in finding["summary"]
assert packet["path_delta"]["observed_reality"] in finding["summary"]
for kind in ("retained", "changed", "stopped"):
for item in packet["path_delta"][kind]:
assert item in finding["summary"]
assert finding["evidence_refs"] == ATTACHMENT["evidence_refs"]
assert len(finding["summary"]) <= 2000
assert index.read_bytes() == before
changed = {**ATTACHMENT, "interpretation": "A different conclusion."}
if source_mode in {"path_delta", "wide_path_delta", "linked_path_delta"}:
packet["path_delta"]["observed_reality"] = "A changed observation on retry."
vision.write_text(json.dumps(packet))
elif source_mode == "vision":
vision.write_text(json.dumps({**packet, "explore_result": changed}))
else:
source.write_text(json.dumps(changed))
rc, conflict = _run_cli(path, runtime, *args, cwd=project)
assert rc == 1, conflict
assert index.read_bytes() == before
assert (
len(load_explore_result_events(explore_result_log_path(runtime, GOAL_ID))) == 2
)
@pytest.mark.parametrize("provider", ["file", "sqlite"])
def test_canonical_delivery_keeps_existing_refs_and_receiver_reads_scoped_result(
tmp_path, provider
):
from tests.control_plane.test_native_todo_planning_update import (
fixture as canonical_fixture,
records,
)
path, _state = canonical_fixture(tmp_path, provider)
config = json.loads(path.read_text())
config["goals"][0]["explore_graph"] = {"enabled": True}
config["goals"][0]["spawn_policy"] = {
"allowed": False,
"explore_harness": {"enabled": True},
}
path.write_text(json.dumps(config))
args = dict(
registry_path=path,
runtime_root=tmp_path / "runtime",
goal_id="goal-a",
agent_id="agent-a",
todo_id="todo_target",
turn_instance_id="turn-one",
)
update_goal_todo(
registry_path=path,
goal_id="goal-a",
agent_id="agent-a",
todo_id="todo_target",
explore_result_node_refs=["prior-question"],
)
before = records(path)["todo_other"]
result = deliver_result_attachment(payload=payload(), **args)
assert result["ok"], result.get("error", result)
assert records(path)["todo_target"]["explore_result_node_refs"] == [
"prior-question",
"prefix-bound",
]
assert records(path)["todo_other"] == before
assert deliver_result_attachment(payload=payload(), **args)["idempotent_replay"]
def test_later_observation_preserves_question_state_and_scoped_history(tmp_path):
from loopx.capabilities.explore.result_log import (
append_explore_result_event,
build_explore_node_event,
)
args = fixture(tmp_path)
first = deliver_result_attachment(payload=payload(), **args)
assert first["ok"]
log = explore_result_log_path(args["runtime_root"], "research")
append_explore_result_event(
log,
build_explore_node_event(
goal_id="research",
node_id="prefix-bound",
node_kind="question",
title=ATTACHMENT["question"],
summary=ATTACHMENT["applicability"],
status="resolved",
),
)
second = payload()
second["generated_at"] = "2026-01-02T00:00:00Z"
second["settlement_identity"]["effect_id"] = "research:worker:turn-two"
second["explore_result"].update(
status="tentative",
input_revision="fixture-v2",
observation="The prerequisite did not build.",
interpretation="No conclusion about the bound.",
)
result = deliver_result_attachment(
payload=second, **{**args, "turn_instance_id": "turn-two"}
)
assert result["ok"], result.get("error", result)
context = explore_turn_context(
**{
key: args[key]
for key in ("registry_path", "runtime_root", "goal_id", "agent_id")
}
)
assert context["graph"]["recent_nodes"][0]["status"] == "resolved"
assert {row["status"] for row in context["graph"]["writeback_results"]} == {
"tentative",
"refuted",
}
bad = {**ATTACHMENT, "applicability": "Different question scope"}
with pytest.raises(ValueError, match="scope"):
prepare_result_attachment(bad, **args)
@pytest.mark.parametrize("provider", ["file", "sqlite"])
@pytest.mark.parametrize("mode", ["soft_claim", "hard_lease"])
def test_completed_work_can_attach_evidence_without_reopening_or_changing_completion(
tmp_path, provider, mode
):
from tests.control_plane.test_native_todo_planning_update import (
source_fixture as canonical_source_fixture,
records,
)
from canonical_authority_fixture import initialize_canonical_authority
from loopx.control_plane.coordination.runtime_shadow import (
build_todo_runtime_shadow_projection,
)
from loopx.control_plane.coordination.local_authority import (
read_canonical_todos_if_promoted,
)
from loopx.control_plane.work_items.task_lease import acquire_task_lease
from loopx.todos import complete_goal_todo
path, state = canonical_source_fixture(tmp_path)
config = json.loads(path.read_text())
config["goals"][0]["explore_graph"] = {"enabled": True}
path.write_text(json.dumps(config))
runtime = tmp_path / "runtime"
todos = read_fixture_todos(state)
initialize_canonical_authority(
runtime,
"goal-a",
build_todo_runtime_shadow_projection(
goal_id="goal-a", todos=todos, handoff_mode=mode
),
state_path=state,
provider=provider,
)
proof = {}
if mode == "hard_lease":
lease = acquire_task_lease(
registry_path=path,
runtime_root=runtime,
goal_id="goal-a",
todo_id="todo_target",
owner="agent-a",
idempotency_key="terminal-result",
)
proof = dict(
task_lease_idempotency_key="terminal-result",
task_lease_expected_version=lease["lease"]["version"],
)
complete_goal_todo(
registry_path=path,
goal_id="goal-a",
todo_id="todo_target",
agent_id="agent-a",
evidence="Synthetic validation passed",
**proof,
)
before = records(path)["todo_target"]
leases_before = read_canonical_todos_if_promoted(
runtime_root=runtime, goal_id="goal-a", include_leases=True
)["leases"]
args = dict(
registry_path=path,
runtime_root=runtime,
goal_id="goal-a",
agent_id="agent-a",
todo_id="todo_target",
turn_instance_id="terminal-turn",
)
assert before["status"] == "done"
prepare_result_attachment(ATTACHMENT, **args)
result = deliver_result_attachment(payload=payload(), **args)
assert result["ok"], result
after = records(path)["todo_target"]
assert after["status"] == "done" and after["claimed_by"] == "agent-a"
assert after.get("completion_continuation") == before.get("completion_continuation")
assert after["explore_result_node_refs"] == [ATTACHMENT["node_id"]]
assert (
read_canonical_todos_if_promoted(
runtime_root=runtime, goal_id="goal-a", include_leases=True
)["leases"]
== leases_before
)
with pytest.raises((ValueError, RuntimeError)):
update_goal_todo(
registry_path=path,
goal_id="goal-a",
agent_id="agent-b",
todo_id="todo_target",
append_explore_result_node_refs=["foreign"],
**proof,
)
with pytest.raises((ValueError, RuntimeError)):
update_goal_todo(
registry_path=path,
goal_id="goal-a",
agent_id="agent-a",
todo_id="todo_target",
append_explore_result_node_refs=["mixed"],
note="Other edit",
**proof,
)
if mode == "hard_lease":
# A mismatched retained lease version must not authorize even this
# narrow metadata operation; completion need not increment the version.
assert leases_before[0]["status"] == "released"
with pytest.raises((ValueError, RuntimeError)):
update_goal_todo(
registry_path=path,
goal_id="goal-a",
agent_id="agent-a",
todo_id="todo_target",
append_explore_result_node_refs=["stale"],
**{
**proof,
"task_lease_expected_version": proof["task_lease_expected_version"]
+ 1,
},
)
@pytest.mark.parametrize("provider", ["file", "sqlite"])
def test_hard_lease_is_required_for_new_delivery_but_not_successful_readback(
tmp_path, provider
):
from tests.control_plane.test_native_todo_planning_update import (
source_fixture as canonical_source_fixture,
)
from canonical_authority_fixture import initialize_canonical_authority
from loopx.control_plane.coordination.runtime_shadow import (
build_todo_runtime_shadow_projection,
)
from loopx.control_plane.work_items.task_lease import (
acquire_task_lease,
release_task_lease,
)
path, state = canonical_source_fixture(tmp_path)
config = json.loads(path.read_text())
config["goals"][0]["explore_graph"] = {"enabled": True}
path.write_text(json.dumps(config))
runtime = tmp_path / "runtime"
todos = read_fixture_todos(state)
projection = build_todo_runtime_shadow_projection(
goal_id="goal-a", todos=todos, handoff_mode="hard_lease"
)
initialize_canonical_authority(
runtime, "goal-a", projection, state_path=state, provider=provider
)
args = dict(
registry_path=path,
runtime_root=runtime,
goal_id="goal-a",
agent_id="agent-a",
todo_id="todo_target",
turn_instance_id="turn-one",
)
failed = deliver_result_attachment(payload=payload(), **args)
assert not failed["ok"] and "lease" in failed["error"]
assert not explore_result_log_path(runtime, "goal-a").exists()
lease_args = dict(
registry_path=path,
runtime_root=runtime,
goal_id="goal-a",
todo_id="todo_target",
owner="agent-a",
idempotency_key="result-test",
)
lease = acquire_task_lease(**lease_args)
result = deliver_result_attachment(payload=payload(), **args)
assert result["ok"], result.get("error", result)
release_task_lease(**lease_args, expected_version=lease["lease"]["version"])
replay = deliver_result_attachment(payload=payload(), **args)
assert replay["ok"] and replay["idempotent_replay"]
later = payload()
later["generated_at"] = "2026-01-02T00:00:00Z"
later["settlement_identity"]["effect_id"] = "second-result"
rejected = deliver_result_attachment(
payload=later, **{**args, "turn_instance_id": "turn-two"}
)
assert not rejected["ok"] and "lease" in rejected["error"]
assert (
len(load_explore_result_events(explore_result_log_path(runtime, "goal-a"))) == 2
)
@pytest.mark.parametrize("provider", ["file", "sqlite"])
def test_real_cli_open_capture_release_order_and_readback_replay(tmp_path, provider):
from canonical_authority_fixture import initialize_canonical_authority
from loopx.control_plane.coordination.runtime_shadow import build_todo_runtime_shadow_projection
from loopx.control_plane.coordination.local_authority import read_canonical_todos_if_promoted
from loopx.control_plane.work_items.task_lease import acquire_task_lease
from tests.control_plane.test_quota_settlement_cli import (
_write_fixture, _run_cli, GOAL_ID, AGENT_ID, TODO_ID, TURN_ID,
)
project, runtime, path = _write_fixture(tmp_path)
config = json.loads(path.read_text())
config["goals"][0]["explore_graph"] = {"enabled": True}
path.write_text(json.dumps(config))
def run(*command):
rc, packet = _run_cli(path, runtime, *command, cwd=project)
assert rc == 0, packet
return packet
source_todos = read_fixture_todos(project / config["goals"][0]["state_file"])
initialize_canonical_authority(runtime, GOAL_ID,
build_todo_runtime_shadow_projection(goal_id=GOAL_ID, todos=source_todos, handoff_mode="hard_lease"),
state_path=project / config["goals"][0]["state_file"], provider=provider)
acquire_task_lease(
registry_path=path, runtime_root=runtime, goal_id=GOAL_ID,
todo_id=TODO_ID, owner=AGENT_ID, idempotency_key="early-release",
)
run("todo", "claim", "--goal-id", GOAL_ID, "--todo-id", TODO_ID,
"--agent-id", AGENT_ID, "--claimed-by", AGENT_ID)
todos = list_goal_todos(registry_path=path, goal_id=GOAL_ID)["todos"]
binding = ["--goal-id", GOAL_ID, "--agent-id", AGENT_ID,
"--todo-id", TODO_ID, "--turn-instance-id", TURN_ID]
guard_command = ["quota", "should-run", "--codex-app", *binding,
]
guard = run(*guard_command)
hint = guard["interaction_contract"]["cli_channel"]["settlement_plan"]["ordered_steps"][1]["optional_attachments"][0]
assert "end that execution only after explore_result_delivery.ok=true" in hint["guidance"]
lease_binding = ["--goal-id", GOAL_ID, "--todo-id", TODO_ID, "--owner", AGENT_ID]
early = run("task-lease", "acquire", *lease_binding, "--idempotency-key", "early-release")["lease"]
run("task-lease", "release", *lease_binding, "--idempotency-key", "early-release",
"--expected-version", str(early["version"]))
vision = tmp_path / "vision.json"
vision.write_text(json.dumps({
"schema_version": "goal_vision_replan_contract_v0", "state": "vision_patch_proposed",
"vision_patch": {"acceptance_summary": "Require a uniform tail bound."},
"explore_result": ATTACHMENT,
}))
refresh = ["refresh-state", *binding, "--classification", "validated_change",
"--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress",
"--agent-vision-json", str(vision), "--no-global-sync", "--suppress-external-sinks"]
index = runtime / "goals" / GOAL_ID / "runs" / "index.jsonl"
before_index = index.read_bytes() if index.exists() else b""
rc, rejected = _run_cli(path, runtime, *refresh, cwd=project)
assert rc == 1 and not rejected["ok"] and not rejected["appended"], rejected
assert (index.read_bytes() if index.exists() else b"") == before_index
log = explore_result_log_path(runtime, GOAL_ID)
assert not log.exists()
assert list_goal_todos(registry_path=path, goal_id=GOAL_ID)["todos"] == todos
# Supported recovery: fresh admission and an active lease, then the same capture.
run(*guard_command)
current = run("task-lease", "acquire", *lease_binding, "--idempotency-key", "recovery")["lease"]
captured = run(*refresh)
assert captured["appended"] and captured["explore_result_delivery"]["ok"]
display_input = captured["explore_result_delivery"]["hook"]["intents"][0]["payload"]
assert display_input == {"attachment_ref": "$.explore_result"}
assert captured["explore_result"] == ATTACHMENT
assert len(load_explore_result_events(log)) == 2
committed_index, committed_log = index.read_bytes(), log.read_bytes()
rows = list_goal_todos(registry_path=path, goal_id=GOAL_ID)["todos"]
target = next(row for row in rows if row["todo_id"] == TODO_ID)
assert target["status"] == "open" and target["claimed_by"] == AGENT_ID
assert target["explore_result_node_refs"] == [ATTACHMENT["node_id"]]
run("task-lease", "release", *lease_binding, "--idempotency-key", "recovery",
"--expected-version", str(current["version"]))
leases = read_canonical_todos_if_promoted(
runtime_root=runtime, goal_id=GOAL_ID, include_leases=True)["leases"]
assert leases[0]["status"] == "released"
replay = run(*refresh)
assert replay["idempotent_replay"] and replay["explore_result_delivery"]["idempotent_replay"]
assert replay["explore_result"] == captured["explore_result"]
assert replay["explore_result_delivery"]["hook"]["intents"][0]["payload"] == display_input
assert index.read_bytes() == committed_index and log.read_bytes() == committed_log
assert list_goal_todos(registry_path=path, goal_id=GOAL_ID)["todos"] == rows
assert read_canonical_todos_if_promoted(
runtime_root=runtime, goal_id=GOAL_ID, include_leases=True)["leases"] == leases
context = run("explore", "turn-context", "--goal-id", GOAL_ID, "--agent-id", AGENT_ID)
finding = context["graph"]["writeback_results"][0]
assert finding["status"] == "refuted"
for field in ("input_revision", "applicability", "observation", "interpretation"):
assert ATTACHMENT[field] in finding["summary"]
@pytest.mark.parametrize("planning", [True, False])
def test_selected_route_retains_scope_after_unrelated_newer_results(tmp_path, planning):
from loopx.configure_goal import configure_goal
args = fixture(tmp_path)
configure_goal(
registry_path=args["registry_path"], goal_id="research", execute=True,
explore_mode="planning" if planning else "evidence",
)
for index in range(4):
record = payload()
record["generated_at"] = f"2026-01-01T00:0{index}:00Z"
record["explore_result"]["node_id"] = f"question-{index}"
turn = dict(args, turn_instance_id=f"result-{index}")
record["settlement_identity"]["effect_id"] = f"research:worker:result-{index}"
assert deliver_result_attachment(payload=record, **turn)["ok"]
# Only the older question is relevant to the next selected work item.
update_goal_todo(
registry_path=args["registry_path"], goal_id="research",
todo_id=args["todo_id"], agent_id="worker",
explore_result_node_refs=["question-0"],
)
before = {p: p.read_bytes() for p in tmp_path.rglob("*") if p.is_file()}
context = explore_turn_context(**{
key: args[key] for key in ("registry_path", "runtime_root", "goal_id", "agent_id")
})
results = context["graph"]["writeback_results"]
assert len(results) == 3
assert results[0]["node_id"] == ("question-0" if planning else "question-3")
assert ATTACHMENT["applicability"] in results[0]["summary"]
assert ATTACHMENT["interpretation"] in results[0]["summary"]
assert results[0]["evidence_refs"] == ATTACHMENT["evidence_refs"]
assert all(p.read_bytes() == content for p, content in before.items())
@pytest.mark.parametrize("planning", [True, False])
def test_linked_refutation_keeps_scope_after_later_positive_observations(tmp_path, planning):
from loopx.configure_goal import configure_goal
from loopx.capabilities.explore.result_log import (
append_explore_result_event, build_explore_finding_event,
)
args = fixture(tmp_path)
configure_goal(
registry_path=args["registry_path"], goal_id="research", execute=True,
explore_mode="planning" if planning else "evidence",
)
first = deliver_result_attachment(payload=payload(), **args)
assert first["ok"]
log = explore_result_log_path(args["runtime_root"], "research")
# Successful checks on a narrower input do not supersede the counterexample.
# Enough later results also cross the upstream audit detail cap.
for index in range(25):
append_explore_result_event(log, build_explore_finding_event(
goal_id="research", node_id=ATTACHMENT["node_id"],
finding_id=f"positive-{index}", title="Bound holds for this finite input",
summary="Applicability: finite input only. No conclusion about an infinite tail.",
status="confirmed", tags=["writeback-result"],
recorded_at=f"2026-01-02T00:{index:02d}:00Z",
))
before = {p: p.read_bytes() for p in tmp_path.rglob("*") if p.is_file()}
context = explore_turn_context(**{
key: args[key] for key in ("registry_path", "runtime_root", "goal_id", "agent_id")
})
results = context["graph"]["writeback_results"]
assert len(results) == 3
if planning:
counter = next(row for row in results if row["finding_id"] == first["finding_id"])
assert ATTACHMENT["applicability"] in counter["summary"]
assert ATTACHMENT["interpretation"] in counter["summary"]
assert any(row["status"] == "confirmed" for row in results)
audit = context["harness"]["selected_branches"][0]["typed_evidence_audit"]
assert "linked_finding_refuted" in audit["hazards"]
assert audit["omitted_audit_findings"] == 23
assert audit["score_delta"] == 0
else:
assert [row["finding_id"] for row in results] == ["positive-24", "positive-23", "positive-22"]
assert all(p.read_bytes() == content for p, content in before.items())
if planning:
# An explicit correction of the same finding supersedes its old status;