|
| 1 | +"""Supported source leases retain readback without the historic mutation facade.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from shadow_e2e_fixture import workspace |
| 12 | +from test_canonical_todo_writer_isolation import ( |
| 13 | + isolated_todo_distribution, # noqa: F401 -- distribution fixture |
| 14 | + retirement_wheel, # noqa: F401 -- distribution fixtures |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.parametrize("facade", ["present", "absent"]) |
| 19 | +@pytest.mark.parametrize("capture", [False, True], ids=["capture-off", "capture-on"]) |
| 20 | +@pytest.mark.parametrize("mode", ["legacy", "hard_lease", "soft_claim"]) |
| 21 | +def test_source_lease_lifecycle_uses_retained_reader( |
| 22 | + tmp_path, |
| 23 | + isolated_todo_distribution, # noqa: F811 |
| 24 | + facade, |
| 25 | + capture, |
| 26 | + mode, # noqa: F811 |
| 27 | +): |
| 28 | + package, command = isolated_todo_distribution |
| 29 | + w = workspace(tmp_path / "source", bootstrap=False) |
| 30 | + w.command, w.cwd, w.package = tuple(command), tmp_path, package |
| 31 | + w.state.write_text( |
| 32 | + w.state.read_text().replace("handoff_mode: hard_lease", f"handoff_mode: {mode}") |
| 33 | + + "- [ ] [P1] Preserve source work — 完整内容.\n" |
| 34 | + " <!-- loopx:todo todo_id=todo_source status=open task_class=advancement_task -->\n" |
| 35 | + ) |
| 36 | + config = json.loads(w.registry.read_text()) |
| 37 | + config["goals"][0]["coordination"]["runtime_shadow"]["enabled"] = capture |
| 38 | + w.registry.write_text(json.dumps(config)) |
| 39 | + if capture: |
| 40 | + assert ( |
| 41 | + w.cli("coordination-shadow", "bootstrap", "--execute")["bootstrap"][ |
| 42 | + "status" |
| 43 | + ] |
| 44 | + == "applied" |
| 45 | + ) |
| 46 | + if facade == "absent": |
| 47 | + (package / "todos.py").unlink() |
| 48 | + subprocess.run( |
| 49 | + [ |
| 50 | + sys.executable, |
| 51 | + "-S", |
| 52 | + "-c", |
| 53 | + "from importlib.util import find_spec;assert find_spec('loopx.todos') is None", |
| 54 | + ], |
| 55 | + cwd=tmp_path, |
| 56 | + check=True, |
| 57 | + capture_output=True, |
| 58 | + text=True, |
| 59 | + timeout=30, |
| 60 | + ) |
| 61 | + original = w.state.read_bytes(), w.registry.read_bytes() |
| 62 | + |
| 63 | + def lease(action, *args, success=True): |
| 64 | + return w.cli( |
| 65 | + "task-lease", action, "--todo-id", "todo_source", *args, success=success |
| 66 | + ) |
| 67 | + |
| 68 | + first = lease( |
| 69 | + "acquire", |
| 70 | + "--owner", |
| 71 | + "agent-a", |
| 72 | + "--idempotency-key", |
| 73 | + "source-a", |
| 74 | + "--ttl-seconds", |
| 75 | + "600", |
| 76 | + success=mode != "soft_claim", |
| 77 | + ) |
| 78 | + if mode == "soft_claim": |
| 79 | + assert not first["ok"] and first["error_code"] == "handoff_mode_forbids_lease" |
| 80 | + assert lease("inspect")["lease"] is None |
| 81 | + assert (w.state.read_bytes(), w.registry.read_bytes()) == original |
| 82 | + return |
| 83 | + |
| 84 | + assert first["ok"] and first["acquired"] and first["lease"]["owner"] == "agent-a" |
| 85 | + assert first["lease"]["version"] == first["lease"]["lease_epoch"] == 1 |
| 86 | + assert lease("inspect")["active"] is True |
| 87 | + retry = lease( |
| 88 | + "acquire", |
| 89 | + "--owner", |
| 90 | + "agent-a", |
| 91 | + "--idempotency-key", |
| 92 | + "source-a", |
| 93 | + "--ttl-seconds", |
| 94 | + "600", |
| 95 | + ) |
| 96 | + assert retry["idempotent"] and retry["lease"] == first["lease"] |
| 97 | + denied = lease( |
| 98 | + "acquire", "--owner", "agent-b", "--idempotency-key", "other", success=False |
| 99 | + ) |
| 100 | + assert not denied["ok"] and denied["error_code"] == "todo_lease_conflict" |
| 101 | + renewed = lease( |
| 102 | + "renew", |
| 103 | + "--owner", |
| 104 | + "agent-a", |
| 105 | + "--idempotency-key", |
| 106 | + "source-a", |
| 107 | + "--expected-version", |
| 108 | + "1", |
| 109 | + ) |
| 110 | + assert renewed["lease"]["version"] == 2 and renewed["lease"]["lease_epoch"] == 1 |
| 111 | + # The exact original renewal is receipt recovery, not a stale new write. |
| 112 | + replay = lease( |
| 113 | + "renew", |
| 114 | + "--owner", |
| 115 | + "agent-a", |
| 116 | + "--idempotency-key", |
| 117 | + "source-a", |
| 118 | + "--expected-version", |
| 119 | + "1", |
| 120 | + ) |
| 121 | + assert replay["idempotent"] and replay["lease"] == renewed["lease"] |
| 122 | + assert lease("inspect")["lease"] == renewed["lease"] |
| 123 | + transferred = lease( |
| 124 | + "transfer", |
| 125 | + "--owner", |
| 126 | + "agent-a", |
| 127 | + "--idempotency-key", |
| 128 | + "source-a", |
| 129 | + "--expected-version", |
| 130 | + "2", |
| 131 | + "--new-owner", |
| 132 | + "agent-b", |
| 133 | + "--new-idempotency-key", |
| 134 | + "source-b", |
| 135 | + ) |
| 136 | + assert ( |
| 137 | + transferred["lease"]["version"] == 3 |
| 138 | + and transferred["lease"]["lease_epoch"] == 2 |
| 139 | + ) |
| 140 | + assert transferred["lease"]["owner"] == "agent-b" |
| 141 | + stale = lease( |
| 142 | + "renew", |
| 143 | + "--owner", |
| 144 | + "agent-b", |
| 145 | + "--idempotency-key", |
| 146 | + "source-b", |
| 147 | + "--expected-version", |
| 148 | + "2", |
| 149 | + success=False, |
| 150 | + ) |
| 151 | + assert not stale["ok"] and stale["error_code"] == "version_mismatch" |
| 152 | + assert lease("inspect")["lease"] == transferred["lease"] |
| 153 | + released = lease( |
| 154 | + "release", |
| 155 | + "--owner", |
| 156 | + "agent-b", |
| 157 | + "--idempotency-key", |
| 158 | + "source-b", |
| 159 | + "--expected-version", |
| 160 | + "3", |
| 161 | + ) |
| 162 | + assert released["lease"]["status"] == "released" |
| 163 | + retained = lease("inspect") |
| 164 | + assert not retained["active"] and retained["lease"] == released["lease"] |
| 165 | + assert (w.state.read_bytes(), w.registry.read_bytes()) == original |
| 166 | + if capture: |
| 167 | + # Read the retained journal after the lifecycle; no capture remains pending. |
| 168 | + history = json.loads(subprocess.check_output( |
| 169 | + [sys.executable, "-S", "-c", |
| 170 | + "import json,sys;from pathlib import Path;" |
| 171 | + "from loopx.control_plane.coordination.local_authority_shadow_history " |
| 172 | + "import outbox_summary;" |
| 173 | + "print(json.dumps(outbox_summary(Path(sys.argv[1]),sys.argv[2])))", |
| 174 | + str(w.runtime), w.goal], |
| 175 | + cwd=tmp_path, text=True, timeout=30, |
| 176 | + )) |
| 177 | + assert history["leases"]["prepared_only"] == 0 |
| 178 | + assert history["leases"]["committed_pending"] == 0 |
0 commit comments