|
| 1 | +"""Verified native sources can reach the existing, separately granted inbox.""" |
| 2 | +import json |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from test_native_steward_private import steward, finish # noqa: F401 |
| 7 | +from loopx.capabilities.manager_context import ( |
| 8 | + POLICY_SCHEMA, _hash, _root, _write, authority, configure_delivery_target, |
| 9 | + configure_evidence_scope, deliver, pending, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +def ingress_path(store, row): |
| 14 | + return _root(store.root.parent) / "ingress" / ( |
| 15 | + _hash([row["session_id"], "external-" + row["request_ref"]]) + ".json") |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.parametrize("separate_coordination_root", [False, True]) |
| 19 | +def test_native_ingress_uses_coordination_owner_with_legacy_fallback(steward, monkeypatch, tmp_path, separate_coordination_root): # noqa: F811 |
| 20 | + store, runtime, provider, transport, binding, _, workspace = steward |
| 21 | + root = tmp_path / "coordination" if separate_coordination_root else store.root.parent |
| 22 | + if separate_coordination_root: |
| 23 | + runtime.coordination_runtime_root = root |
| 24 | + target = {"goal_id": "registered-work", "agent_id": "worker"} |
| 25 | + registry = json.loads(runtime.registry_path.read_text()) |
| 26 | + registry["runtime_root"] = str(root) |
| 27 | + registry["goals"] = [{"id": target["goal_id"], "repo": str(workspace), |
| 28 | + "coordination": {"registered_agents": [target["agent_id"]]}}] |
| 29 | + runtime.registry_path.write_text(json.dumps(registry)) |
| 30 | + # Observe provenance at the actual enqueue boundary, before model execution. |
| 31 | + monkeypatch.setattr(runtime, "resume_session_queue", lambda **kwargs: None) |
| 32 | + enqueue = runtime.enqueue_turn |
| 33 | + saved = [] |
| 34 | + |
| 35 | + def observe(**kwargs): |
| 36 | + path = _root(root) / "ingress" / (_hash([kwargs["session_id"], kwargs["client_turn_id"]]) + ".json") |
| 37 | + saved.append(path.read_bytes()) |
| 38 | + return enqueue(**kwargs) |
| 39 | + |
| 40 | + monkeypatch.setattr(runtime, "enqueue_turn", observe) |
| 41 | + event = provider.event("steward-app", "canonical", "Original request for the registered worker") |
| 42 | + assert transport.admit("steward-app", event)["status"] == "durably_accepted" |
| 43 | + row = transport.core.pending()[0] |
| 44 | + session = store.load_session(row["session_id"]) |
| 45 | + turn = store.load_turn(row["session_id"], row["turn_id"]) |
| 46 | + assert turn["status"] == "queued" and len(saved) == 1 |
| 47 | + if separate_coordination_root: |
| 48 | + assert not ingress_path(store, row).exists() |
| 49 | + policy_path = _root(root) / "policy.json" |
| 50 | + _write(policy_path, {"schema_version": POLICY_SCHEMA, "sources": {session["channel_id"]: { |
| 51 | + "sender_ids": [binding["operator_ref"]], "local_delivery_scope": "selected", "targets": [], |
| 52 | + }}}) |
| 53 | + assert authority(root, runtime.registry_path, session, turn)["targets"] == [] |
| 54 | + configure_evidence_scope(root, runtime.registry_path, channel=session["channel_id"], |
| 55 | + goal_ids=[target["goal_id"]], execute=True) |
| 56 | + configure_delivery_target(root, runtime.registry_path, channel=session["channel_id"], |
| 57 | + **target, grant=True, execute=True) |
| 58 | + receipt = deliver(root, runtime.registry_path, session=session, turn=turn, request=target) |
| 59 | + assert pending(root, **target)["items"][0]["message"] == event["content"] |
| 60 | + assert deliver(root, runtime.registry_path, session=session, turn=turn, |
| 61 | + request=target) == {**receipt, "replayed": True} |
| 62 | + assert transport.admit("steward-app", event)["status"] == "durably_accepted" |
| 63 | + assert len(saved) == 1 and len(store.list_sessions()) == 1 |
| 64 | + # A sibling App's project source cannot acquire manager provenance or policy. |
| 65 | + monkeypatch.setattr(runtime, "enqueue_turn", enqueue) |
| 66 | + transport.admit("notes-app", provider.event("notes-app", "other", "Other audience")) |
| 67 | + notes = next(item for item in transport.core.pending() if item["message"] == "Other audience") |
| 68 | + assert not authority(root, runtime.registry_path, store.load_session(notes["session_id"]), |
| 69 | + store.load_turn(notes["session_id"], notes["turn_id"]))["targets"] |
| 70 | + configure_delivery_target(root, runtime.registry_path, channel=session["channel_id"], |
| 71 | + **target, grant=False, execute=True) |
| 72 | + with pytest.raises(ValueError, match="not authorized"): |
| 73 | + deliver(root, runtime.registry_path, session=session, turn=turn, request=target) |
| 74 | + |
| 75 | + |
| 76 | +def test_verified_ingress_precedes_model_launch_and_survives_admission_replay(steward, monkeypatch): # noqa: F811 |
| 77 | + store, runtime, provider, transport, binding, _, _ = steward |
| 78 | + enqueue = runtime.enqueue_turn |
| 79 | + observations = [] |
| 80 | + |
| 81 | + def interrupted(**kwargs): |
| 82 | + row = transport.core.pending()[0] |
| 83 | + saved = json.loads(ingress_path(store, row).read_text()) |
| 84 | + assert saved["client_turn_id"] == kwargs["client_turn_id"] |
| 85 | + assert saved["sender_id"] == binding["operator_ref"] |
| 86 | + assert saved["source_id"] == row["request_ref"] |
| 87 | + assert saved["message_digest"] == _hash(kwargs["message"]) |
| 88 | + observations.append(saved) |
| 89 | + enqueue(**kwargs) |
| 90 | + raise OSError("owner interrupted after canonical admission") |
| 91 | + |
| 92 | + event = provider.event("steward-app", "first", "Original owner context") |
| 93 | + monkeypatch.setattr(runtime, "enqueue_turn", interrupted) |
| 94 | + with pytest.raises(OSError): |
| 95 | + transport.admit("steward-app", event) |
| 96 | + prepared = transport.core.pending()[0] |
| 97 | + before = ingress_path(store, prepared).read_bytes() |
| 98 | + turn = store.turn_for_client(prepared["session_id"], "external-" + prepared["request_ref"]) |
| 99 | + assert finish(runtime, {**prepared, "turn_id": turn["turn_id"]})["status"] == "completed" |
| 100 | + monkeypatch.setattr(runtime, "enqueue_turn", enqueue) |
| 101 | + assert transport.admit("steward-app", event)["status"] == "durably_accepted" |
| 102 | + accepted = transport.core.pending()[0] |
| 103 | + assert accepted["session_id"] == prepared["session_id"] and accepted["turn_id"] == turn["turn_id"] |
| 104 | + assert ingress_path(store, accepted).read_bytes() == before and len(observations) == 1 |
| 105 | + assert len(store.list_sessions()) == 1 |
| 106 | + # A regular project source cannot create manager provenance or a Goal. |
| 107 | + transport.admit("notes-app", provider.event("notes-app", "notes", "Private project context")) |
| 108 | + notes = next(row for row in transport.core.pending() if row["message"] == "Private project context") |
| 109 | + assert finish(runtime, notes)["status"] == "completed" |
| 110 | + assert not ingress_path(store, notes).exists() |
| 111 | + |
| 112 | + |
| 113 | +def test_native_steward_uses_separate_sender_target_grants_and_exact_return_route(steward): # noqa: F811 |
| 114 | + store, runtime, provider, transport, binding, _, workspace = steward |
| 115 | + target = {"goal_id": "fresh-commission", "agent_id": "operations"} |
| 116 | + registry = json.loads(runtime.registry_path.read_text()) |
| 117 | + registry["goals"] = [{"id": target["goal_id"], "repo": str(workspace), |
| 118 | + "coordination": {"registered_agents": [target["agent_id"]]}}] |
| 119 | + runtime.registry_path.write_text(json.dumps(registry)) |
| 120 | + registry_before = runtime.registry_path.read_bytes() |
| 121 | + event = provider.event("steward-app", "source", "Read this source; return private findings, never publish.") |
| 122 | + assert transport.admit("steward-app", event)["status"] == "durably_accepted" |
| 123 | + row = transport.core.pending()[0] |
| 124 | + assert finish(runtime, row)["status"] == "completed" |
| 125 | + session = store.load_session(row["session_id"]) |
| 126 | + turn = store.load_turn(row["session_id"], row["turn_id"]) |
| 127 | + root = store.root.parent |
| 128 | + assert authority(root, runtime.registry_path, session, turn)["targets"] == [] |
| 129 | + policy = {"schema_version": POLICY_SCHEMA, "sources": {session["channel_id"]: { |
| 130 | + "sender_ids": [binding["operator_ref"]], "local_delivery_scope": "selected", "targets": [], |
| 131 | + }}} |
| 132 | + policy_path = _root(root) / "policy.json" |
| 133 | + _write(policy_path, policy) |
| 134 | + # Reading a portfolio does not confer delivery authority. |
| 135 | + configure_evidence_scope(root, runtime.registry_path, channel=session["channel_id"], |
| 136 | + goal_ids=[target["goal_id"]], execute=True) |
| 137 | + assert authority(root, runtime.registry_path, session, turn)["targets"] == [] |
| 138 | + preview = configure_delivery_target(root, runtime.registry_path, channel=session["channel_id"], **target, grant=True) |
| 139 | + assert preview["would_change"] and not preview["executed"] |
| 140 | + assert authority(root, runtime.registry_path, session, turn)["targets"] == [] |
| 141 | + applied = configure_delivery_target(root, runtime.registry_path, channel=session["channel_id"], **target, grant=True, execute=True) |
| 142 | + assert applied["readback_verified"] |
| 143 | + assert authority(root, runtime.registry_path, session, turn)["targets"] == [target] |
| 144 | + assert not authority(root, runtime.registry_path, session, {**turn, "message": "forged"})["targets"] |
| 145 | + assert not authority(root, runtime.registry_path, session, {**turn, "origin": "web"})["targets"] |
| 146 | + receipt = deliver(root, runtime.registry_path, session=session, turn=turn, request=target) |
| 147 | + assert not receipt["todo_created"] and not receipt["execution_interrupted"] |
| 148 | + assert deliver(root, runtime.registry_path, session=session, turn=turn, request=target) == {**receipt, "replayed": True} |
| 149 | + assert pending(root, **target)["items"][0]["message"] == event["content"] |
| 150 | + assert runtime.registry_path.read_bytes() == registry_before |
| 151 | + assert policy_path.stat().st_mode & 0o777 == 0o600 |
| 152 | + # The independently verified second App cannot use this channel's grant. |
| 153 | + transport.admit("notes-app", provider.event("notes-app", "notes", "Other private audience")) |
| 154 | + notes = next(row for row in transport.core.pending() if row["message"] == "Other private audience") |
| 155 | + assert finish(runtime, notes)["status"] == "completed" |
| 156 | + assert not authority(root, runtime.registry_path, store.load_session(notes["session_id"]), |
| 157 | + store.load_turn(notes["session_id"], notes["turn_id"]))["targets"] |
| 158 | + policy["sources"][session["channel_id"]]["targets"] = [target] |
| 159 | + policy["sources"][session["channel_id"]]["sender_ids"] = ["another-App-owner"] |
| 160 | + _write(policy_path, policy) |
| 161 | + with pytest.raises(ValueError, match="not authorized"): |
| 162 | + deliver(root, runtime.registry_path, session=session, turn=turn, request=target) |
| 163 | + |
| 164 | + |
| 165 | +@pytest.mark.parametrize("channel", ["manager.external.native", "manager.external.native." + "a" * 24, |
| 166 | + "manager.external.native." + "a" * 24 + ".*", |
| 167 | + "manager.external." + "a" * 23]) |
| 168 | +def test_configuration_rejects_partial_or_wildcard_audiences(tmp_path, channel): |
| 169 | + with pytest.raises(ValueError, match="exact external"): |
| 170 | + configure_evidence_scope(tmp_path, tmp_path / "missing.json", channel=channel, goal_ids=[]) |
| 171 | + with pytest.raises(ValueError, match="exact external"): |
| 172 | + configure_delivery_target(tmp_path, tmp_path / "missing.json", channel=channel, |
| 173 | + goal_id="fresh-commission", agent_id="operations", grant=True) |
| 174 | + |
| 175 | + |
| 176 | +def test_inbox_bound_does_not_truncate_or_reject_ordinary_native_chat(steward, monkeypatch): # noqa: F811 |
| 177 | + store, runtime, provider, transport, _, _, _ = steward |
| 178 | + monkeypatch.setattr(runtime, "resume_session_queue", lambda **kwargs: None) |
| 179 | + message = "x" * 32001 |
| 180 | + assert transport.admit("steward-app", provider.event("steward-app", "large", message))["status"] == "durably_accepted" |
| 181 | + row = transport.core.pending()[0] |
| 182 | + turn = store.load_turn(row["session_id"], row["turn_id"]) |
| 183 | + assert turn["message"] == message and turn["status"] == "queued" |
| 184 | + assert not ingress_path(store, row).exists() |
| 185 | + assert not authority(store.root.parent, runtime.registry_path, store.load_session(row["session_id"]), turn)["targets"] |
0 commit comments