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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
from core import fq
|
| 6 |
+
from core.manifest_parser import ManifestParser, ManifestRegistry
|
| 7 |
+
from core.signal_registry import SignalRegistry
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def test_agents_in_discovers_broad_catalog_but_tunes_nothing_by_default():
|
| 11 |
+
raw = """
|
| 12 |
+
# Studio agent
|
| 13 |
+
|
| 14 |
+
This agent can render video. This agent can generate 3D models.
|
| 15 |
+
|
| 16 |
+
## Capabilities
|
| 17 |
+
- Search the web
|
| 18 |
+
- Execute Python code
|
| 19 |
+
"""
|
| 20 |
+
profile = fq.agents_in(raw, project="studio")
|
| 21 |
+
frequencies = {x["frequency"] for x in profile["catalog"]}
|
| 22 |
+
|
| 23 |
+
assert {"media.video.render", "model.3d.generate", "research.web.search", "code.execute"} <= frequencies
|
| 24 |
+
assert profile["tune"] == []
|
| 25 |
+
assert profile["playlist"] == {"available": 4, "tuned": 0}
|
| 26 |
+
assert profile["runtime_genome"]["closure_state"] == "seed"
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def test_tune_is_a_playlist_not_a_catalog_copy():
|
| 30 |
+
profile = fq.agents_in(
|
| 31 |
+
"This agent can render video. This agent can generate 3D models. This agent can search the web.",
|
| 32 |
+
project="demo",
|
| 33 |
+
)
|
| 34 |
+
tuned = fq.retune(profile, ["video", "3d"])
|
| 35 |
+
assert tuned["tune"] == ["media.video.render", "model.3d.generate"]
|
| 36 |
+
assert len(tuned["catalog"]) == 3
|
| 37 |
+
assert tuned["playlist"] == {"available": 3, "tuned": 2}
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def test_agents_out_mentions_only_resident_playlist_not_every_available_tool():
|
| 41 |
+
profile = fq.agents_in(
|
| 42 |
+
"This agent can render video. This agent can generate 3D models. This agent can search the web.",
|
| 43 |
+
project="demo",
|
| 44 |
+
tune=["video"],
|
| 45 |
+
)
|
| 46 |
+
out = fq.agents_out(profile, endpoint="http://mesh.local:7860")
|
| 47 |
+
patch = out["agents_md_patch"]
|
| 48 |
+
|
| 49 |
+
assert "AGENTS.fq" in patch
|
| 50 |
+
assert "http://mesh.local:7860" in patch
|
| 51 |
+
assert "media.video.render" in patch
|
| 52 |
+
assert "model.3d.generate" not in patch
|
| 53 |
+
assert "research.web.search" not in patch
|
| 54 |
+
assert "full catalog is available" in patch
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def test_declared_signalmesh_frequencies_are_preserved_and_can_be_tuned_exactly():
|
| 58 |
+
raw = """
|
| 59 |
+
# Existing node
|
| 60 |
+
- **Frequency:** `repo.review`
|
| 61 |
+
- **Mode:** `EMIT / BROADCAST`
|
| 62 |
+
- **Frequency:** `tests.run`
|
| 63 |
+
- **Mode:** `LISTEN`
|
| 64 |
+
"""
|
| 65 |
+
profile = fq.agents_in(raw, tune=["repo.review", "tests.run"])
|
| 66 |
+
assert profile["tune"] == ["repo.review", "tests.run"]
|
| 67 |
+
by = {x["frequency"]: x for x in profile["catalog"]}
|
| 68 |
+
assert by["repo.review"]["origin"] == "declared_frequency"
|
| 69 |
+
assert by["tests.run"]["mode"] == "LISTEN"
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def test_manifest_parser_retains_fq_sidecar_and_only_explicit_frequencies_start_tuned():
|
| 73 |
+
declared = ManifestParser(
|
| 74 |
+
"- **Frequency:** `repo.review`\n- **Mode:** `EMIT`\nThis agent can render video.",
|
| 75 |
+
"https://example.test/AGENTS.md",
|
| 76 |
+
).parse()
|
| 77 |
+
assert declared["agents_fq"]["tune"] == ["repo.review"]
|
| 78 |
+
assert "media.video.render" in {x["frequency"] for x in declared["agents_fq"]["catalog"]}
|
| 79 |
+
|
| 80 |
+
generic = ManifestParser(
|
| 81 |
+
"This agent can render video. This agent can generate 3D models.",
|
| 82 |
+
"https://example.test/generic/AGENTS.md",
|
| 83 |
+
).parse()
|
| 84 |
+
assert generic["frequencies"] == []
|
| 85 |
+
assert generic["agents_fq"]["tune"] == []
|
| 86 |
+
assert generic["agents_fq"]["playlist"]["available"] == 2
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def test_manifest_registry_registers_sidecar_without_broadcasting_discovered_catalog():
|
| 90 |
+
registry = ManifestRegistry()
|
| 91 |
+
signal_registry = SignalRegistry()
|
| 92 |
+
before = set(signal_registry.streams)
|
| 93 |
+
parsed = registry.ingest(
|
| 94 |
+
"This agent can render video. This agent can generate 3D models.",
|
| 95 |
+
"https://example.test/no-broadcast/AGENTS.md",
|
| 96 |
+
)
|
| 97 |
+
assert fq.fq_registry.get(parsed["agents_fq"]["id"])
|
| 98 |
+
assert set(signal_registry.streams) == before
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def test_fq_registry_broadcasts_only_tuned_stations():
|
| 102 |
+
signal_registry = SignalRegistry()
|
| 103 |
+
for name in ("media.video.render", "model.3d.generate", "research.web.search"):
|
| 104 |
+
signal_registry.streams.pop(name, None)
|
| 105 |
+
|
| 106 |
+
profile = fq.agents_in(
|
| 107 |
+
"This agent can render video. This agent can generate 3D models. This agent can search the web.",
|
| 108 |
+
project="broadcast-demo",
|
| 109 |
+
tune=["video"],
|
| 110 |
+
)
|
| 111 |
+
local = fq.FQRegistry()
|
| 112 |
+
local.ingest(profile, signal_registry=signal_registry)
|
| 113 |
+
|
| 114 |
+
assert signal_registry.streams.get("media.video.render")
|
| 115 |
+
assert not signal_registry.streams.get("model.3d.generate")
|
| 116 |
+
assert not signal_registry.streams.get("research.web.search")
|
| 117 |
+
signal_registry.streams.pop("media.video.render", None)
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def test_roundtrip_keeps_extension_protocol_and_kind_dispatch_open():
|
| 121 |
+
profile = fq.agents_in("This agent can render video.", project="roundtrip", tune=["video"])
|
| 122 |
+
encoded = fq.dump_fq(profile)
|
| 123 |
+
decoded = fq.parse_fq(encoded)
|
| 124 |
+
assert decoded["schema"] == fq.SCHEMA
|
| 125 |
+
assert decoded["kind"] == "agents"
|
| 126 |
+
assert decoded["tune"] == ["media.video.render"]
|
| 127 |
+
|
| 128 |
+
# .fq is broader than AGENTS.fq: a future kind can use the same envelope.
|
| 129 |
+
custom = {
|
| 130 |
+
"schema": fq.SCHEMA,
|
| 131 |
+
"kind": "browser-flow",
|
| 132 |
+
"id": "fq.browser.demo",
|
| 133 |
+
"catalog": [{"frequency": "browser.checkout", "label": "Checkout"}],
|
| 134 |
+
"tune": ["browser.checkout"],
|
| 135 |
+
"signals": [{"frequency": "browser.checkout", "content": {"flow_ref": "flow.checkout"}}],
|
| 136 |
+
}
|
| 137 |
+
assert fq.parse_fq(custom)["kind"] == "browser-flow"
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def test_fq_refuses_credentials_but_allows_requirement_names_and_refs():
|
| 141 |
+
profile = fq.agents_in("This agent can render video.", project="safe")
|
| 142 |
+
profile["runtime_genome"]["env_names"] = ["OPENAI_API_KEY", "DATABASE_URL"]
|
| 143 |
+
profile["runtime_genome"]["session_ref"] = "mport.session.owner"
|
| 144 |
+
fq.parse_fq(profile)
|
| 145 |
+
|
| 146 |
+
poisoned = json.loads(fq.dump_fq(profile))
|
| 147 |
+
poisoned["runtime_genome"]["session_ref"] = "Bearer abcdefghijklmnopqrstuvwxyz"
|
| 148 |
+
with pytest.raises(ValueError, match="credential-shaped"):
|
| 149 |
+
fq.parse_fq(poisoned)
|
| 150 |
+
|
| 151 |
+
poisoned = json.loads(fq.dump_fq(profile))
|
| 152 |
+
poisoned["api_key"] = "sk-abcdefghijklmnopqrstuvwxyz"
|
| 153 |
+
with pytest.raises(ValueError, match="credential field"):
|
| 154 |
+
fq.parse_fq(poisoned)
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
def test_selector_ambiguity_fails_instead_of_guessing_attention():
|
| 158 |
+
profile = {
|
| 159 |
+
"schema": fq.SCHEMA,
|
| 160 |
+
"kind": "agents",
|
| 161 |
+
"catalog": [
|
| 162 |
+
{"frequency": "image.generate", "label": "Generate image"},
|
| 163 |
+
{"frequency": "image.edit", "label": "Edit image"},
|
| 164 |
+
],
|
| 165 |
+
"tune": [],
|
| 166 |
+
"signals": [],
|
| 167 |
+
}
|
| 168 |
+
with pytest.raises(ValueError, match="ambiguous"):
|
| 169 |
+
fq.retune(profile, ["image"])
|