-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path59_plasmid_registry.py
More file actions
168 lines (131 loc) · 6.26 KB
/
59_plasmid_registry.py
File metadata and controls
168 lines (131 loc) · 6.26 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
"""
Example 59: Plasmid Registry — Horizontal Gene Transfer
========================================================
Demonstrates dynamic tool acquisition from a searchable registry,
implementing the paper's horizontal gene transfer construction in §6.2.
Biological Analogy:
- Plasmid = small circular DNA encoding a useful gene (tool)
- Registry = environmental pool of plasmids
- Acquisition = bacterial conjugation / transformation
- Optional capability gating = restriction enzymes (when capability envelopes are configured)
- Release = plasmid curing
Key points:
1. Tools are no longer static at construction time
2. Agents can dynamically discover and acquire new capabilities
3. Optional capability gating prevents privilege escalation when enabled
4. Released tools are cleanly removed
References:
- Article Section 6.2: Horizontal Gene Transfer - Dynamic Tool Loading
"""
from operon_ai.core.types import Capability
from operon_ai.organelles.mitochondria import Mitochondria
from operon_ai.organelles.plasmid import Plasmid, PlasmidRegistry
def main():
try:
print("=" * 60)
print("Plasmid Registry — Horizontal Gene Transfer")
print("=" * 60)
# =================================================================
# SECTION 1: Create a Plasmid Registry
# =================================================================
print("\n--- Section 1: Plasmid Registry ---")
registry = PlasmidRegistry()
registry.register(Plasmid(
name="reverse",
description="Reverse a string",
func=lambda s: s[::-1],
tags=frozenset({"text", "utility"}),
))
registry.register(Plasmid(
name="word_count",
description="Count words in text",
func=lambda s: len(s.split()),
tags=frozenset({"text", "analysis"}),
))
registry.register(Plasmid(
name="fetch_url",
description="Fetch a URL (simulated)",
func=lambda url: f"<html>Content from {url}</html>",
tags=frozenset({"network", "io"}),
required_capabilities=frozenset({Capability.NET}),
))
print(f" Registry size: {len(registry)} plasmids")
for item in registry.list_available():
caps = item["required_capabilities"] or ["none"]
print(f" {item['name']}: tags={item['tags']}, caps={caps}")
# =================================================================
# SECTION 2: Search the Registry
# =================================================================
print("\n--- Section 2: Search ---")
text_tools = registry.search(tags={"text"})
print(f" Text tools: {[p.name for p in text_tools]}")
net_tools = registry.search("fetch")
print(f" Network tools: {[p.name for p in net_tools]}")
# =================================================================
# SECTION 3: Dynamic Acquisition
# =================================================================
print("\n--- Section 3: Dynamic Acquisition ---")
mito = Mitochondria(
allowed_capabilities={Capability.READ_FS},
silent=True,
)
print(f" Initial tools: {list(mito.tools.keys())}")
# Acquire a tool with no capability requirements
result = mito.acquire("reverse", registry)
print(f" Acquire 'reverse': success={result.success}")
result = mito.acquire("word_count", registry)
print(f" Acquire 'word_count': success={result.success}")
print(f" Tools after acquisition: {list(mito.tools.keys())}")
# =================================================================
# SECTION 4: Capability Gating
# =================================================================
print("\n--- Section 4: Capability Gating ---")
# fetch_url requires NET, but mito only allows READ_FS
result = mito.acquire("fetch_url", registry)
print(f" Acquire 'fetch_url': success={result.success}")
print(f" Reason: {result.error}")
# Create a mito with NET capability
net_mito = Mitochondria(
allowed_capabilities={Capability.NET},
silent=True,
)
result = net_mito.acquire("fetch_url", registry)
print(f" Acquire with NET cap: success={result.success}")
# =================================================================
# SECTION 5: Execute Acquired Tools
# =================================================================
print("\n--- Section 5: Execute Acquired Tools ---")
r = mito.metabolize('reverse("hello world")')
print(f" reverse('hello world') = {r.atp.value}")
r = mito.metabolize('word_count("the quick brown fox")')
print(f" word_count('the quick brown fox') = {r.atp.value}")
# =================================================================
# SECTION 6: Release (Plasmid Curing)
# =================================================================
print("\n--- Section 6: Plasmid Release ---")
print(f" Tools before release: {list(mito.tools.keys())}")
mito.release("reverse")
print(f" Tools after releasing 'reverse': {list(mito.tools.keys())}")
# Verify tool is gone
r = mito.metabolize('reverse("test")')
print(f" Execute after release: success={r.success}")
print(f" Error: {r.error}")
# Re-acquire is possible
result = mito.acquire("reverse", registry)
print(f" Re-acquire 'reverse': success={result.success}")
print(f" Tools: {list(mito.tools.keys())}")
# =================================================================
# SECTION 7: Schema Export
# =================================================================
print("\n--- Section 7: Schema Export ---")
schemas = mito.export_tool_schemas()
for s in schemas:
print(f" {s.name}: {s.description}")
print("\n" + "=" * 60)
print("DONE — Plasmid Registry demonstrated successfully")
print("=" * 60)
except Exception as e:
print(f"\nError: {e}")
raise
if __name__ == "__main__":
main()