|
1 | 1 | """Shared context references preserve full instructions across existing entrypoints.""" |
2 | 2 | from copy import deepcopy |
3 | | -import hashlib |
4 | 3 | import json |
5 | 4 |
|
6 | 5 | import pytest |
@@ -52,10 +51,15 @@ def test_shared_guidance_reaches_existing_readers_without_changing_settlement(ca |
52 | 51 | if captured: |
53 | 52 | projected = _turn_envelope_result("replan_cli_view", payload=shared) |
54 | 53 | assert graph(projected) == graph(source) |
55 | | - digest = "sha256:" + hashlib.sha256(json.dumps(shared, ensure_ascii=False, |
56 | | - sort_keys=True, separators=(",", ":")).encode()).hexdigest() |
| 54 | + # Use the existing source signature, also embedded in captured read |
| 55 | + # commands. Python's numeric spelling is not its canonical hash codec. |
| 56 | + digest = envelope["action_signature"]["source_decision_hash"] |
57 | 57 | writer = _turn_envelope_result("replan_view", payload=shared, source_hash=digest, stage="writeback") |
58 | 58 | assert writer["writeback"]["settlement_plan"] == source["interaction_contract"]["cli_channel"]["settlement_plan"] |
| 59 | + changed = deepcopy(shared) |
| 60 | + changed["reason"] += " Mutated capture." |
| 61 | + with pytest.raises(RuntimeError, match="matching source hash"): |
| 62 | + _turn_envelope_result("replan_view", payload=changed, source_hash=digest, stage="writeback") |
59 | 63 |
|
60 | 64 |
|
61 | 65 | @pytest.mark.parametrize("mutation", ["different", "wrong-step", "wrong-capability", "missing-owner", "short", "wrong-source"]) |
@@ -193,3 +197,48 @@ def unavailable(*args, **kwargs): |
193 | 197 | for source in ({"ok": False, "diagnostic": "unavailable"}, |
194 | 198 | {"interaction_contract": {"agent_channel": {"work_context": {"sources": []}}}}): |
195 | 199 | assert _reference_context(source) is source |
| 200 | + |
| 201 | + |
| 202 | +@pytest.mark.parametrize("value", [1.0, -0.0, 2**60 + 1, float(2**60), 0.000001]) |
| 203 | +@pytest.mark.parametrize("entrypoint", ["reference", "diagnostics", "cli-detail"]) |
| 204 | +def test_context_sharing_keeps_original_numeric_facts(value, entrypoint): |
| 205 | + from loopx.control_plane.quota.cli_projection import compact_quota_should_run_cli_payload |
| 206 | + source, _ = packet() |
| 207 | + facts = {"future_measurement": value, "nested": [value, False, None]} |
| 208 | + source["future_facts"] = deepcopy(facts) |
| 209 | + attachment(source)["future_facts"] = deepcopy(facts) |
| 210 | + original = json.dumps(source) |
| 211 | + |
| 212 | + def project(payload): |
| 213 | + if entrypoint == "cli-detail": |
| 214 | + return compact_quota_should_run_cli_payload(payload, |
| 215 | + include_todo_summary_detail=True, include_user_todo_summary_detail=True) |
| 216 | + return _reference_context(payload, **({"diagnostics": {}} if entrypoint == "diagnostics" else {})) |
| 217 | + |
| 218 | + shared = project(source) |
| 219 | + # JSON spelling distinguishes float/int, negative zero and adjacent large |
| 220 | + # integers; Python equality alone cannot establish that facts survived. |
| 221 | + assert json.dumps(shared["future_facts"]) == json.dumps(facts) |
| 222 | + assert json.dumps(attachment(shared)["future_facts"]) == json.dumps(facts) |
| 223 | + assert graph(shared)["result_writeback_guidance_ref"].endswith("optional_attachments[0].guidance") |
| 224 | + assert json.dumps(project(shared)) == json.dumps(shared) |
| 225 | + assert json.dumps(source) == original |
| 226 | + |
| 227 | + |
| 228 | +def test_reference_transport_does_not_return_untouched_material(monkeypatch): |
| 229 | + from loopx.control_plane.quota import turn_envelope |
| 230 | + source, _ = packet() |
| 231 | + body = "Untouched supporting material. " * 2048 |
| 232 | + source["future_facts"] = {"body": body} |
| 233 | + responses, real = [], turn_envelope._turn_envelope_result |
| 234 | + |
| 235 | + def observed(operation, **params): |
| 236 | + result = real(operation, **params) |
| 237 | + responses.append(json.dumps(result)) |
| 238 | + return result |
| 239 | + |
| 240 | + monkeypatch.setattr(turn_envelope, "_turn_envelope_result", observed) |
| 241 | + shared = _reference_context(source) |
| 242 | + assert shared["future_facts"]["body"] == body |
| 243 | + assert len(responses) == 1 |
| 244 | + assert body not in responses[0] |
0 commit comments