forked from coredipper/operon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15_genome_telomere_lifecycle.py
More file actions
349 lines (282 loc) · 10.7 KB
/
15_genome_telomere_lifecycle.py
File metadata and controls
349 lines (282 loc) · 10.7 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
"""
Example 15: Agent Lifecycle with Genome and Telomere
====================================================
This example demonstrates:
- Genome: Immutable configuration with gene expression control
- Telomere: Lifecycle management and senescence
- Integration: How genome and telomere work together
Biological Analogy:
- Genome: DNA that defines the fundamental traits of an organism
- Genes: Individual traits that can be expressed or silenced
- Telomere: Chromosome caps that shorten with each division
- Senescence: Cellular aging leading to reduced function
"""
from operon_ai.state import (
ExpressionLevel,
Gene,
GeneType,
Genome,
LifecyclePhase,
SenescenceReason,
Telomere,
TelomereStatus,
)
def demonstrate_genome_basics():
"""Demonstrate basic genome operations."""
print("\n" + "="*60)
print("1. GENOME: IMMUTABLE CONFIGURATION")
print("="*60)
# Create a genome with initial genes
genome = Genome(
genes=[
Gene(name="model", value="gpt-4", gene_type=GeneType.STRUCTURAL, required=True),
Gene(name="temperature", value=0.7, gene_type=GeneType.REGULATORY),
Gene(name="max_tokens", value=4096, gene_type=GeneType.HOUSEKEEPING),
],
allow_mutations=False, # Immutable by default
silent=True,
)
print("\n--- Initial Genome ---")
for gene_info in genome.list_genes():
print(f" {gene_info['name']}: {gene_info['value']} "
f"({gene_info['type']}, {gene_info['expression']})")
# Try to add a duplicate (will fail - immutable)
print("\n--- Attempting Duplicate Gene (should fail) ---")
success = genome.add_gene(Gene(name="model", value="gpt-3.5"))
print(f" Add duplicate: {'Success' if success else 'Blocked (expected)'}")
# Get expressed configuration
print("\n--- Expressed Configuration ---")
config = genome.express()
for key, value in config.items():
print(f" {key}: {value}")
return genome
def demonstrate_gene_expression():
"""Demonstrate gene expression control."""
print("\n" + "="*60)
print("2. GENE EXPRESSION CONTROL")
print("="*60)
genome = Genome(
genes=[
Gene(name="verbose_mode", value=True, gene_type=GeneType.REGULATORY),
Gene(name="debug_mode", value=True, gene_type=GeneType.CONDITIONAL),
Gene(name="safety_checks", value=True, gene_type=GeneType.HOUSEKEEPING, required=True),
Gene(name="experimental_feature", value=True, gene_type=GeneType.DORMANT),
],
silent=True,
)
print("\n--- Initial Expression ---")
config = genome.express()
print(f" Active genes: {list(config.keys())}")
# Silence verbose mode
print("\n--- Silencing verbose_mode ---")
genome.silence_gene("verbose_mode", reason="user preference")
config = genome.express()
print(f" Active genes: {list(config.keys())}")
# Express conditional gene with context
print("\n--- Express with Context (debug_mode) ---")
config_with_context = genome.express(context={"debug_mode": True})
print(f" Active genes: {list(config_with_context.keys())}")
# Note: dormant genes are never expressed
print("\n--- Dormant Gene Status ---")
dormant = genome.get_gene("experimental_feature")
print(f" experimental_feature exists: {dormant is not None}")
print(f" experimental_feature in config: {'experimental_feature' in config}")
return genome
def demonstrate_genome_inheritance():
"""Demonstrate genome replication and inheritance."""
print("\n" + "="*60)
print("3. GENOME REPLICATION (INHERITANCE)")
print("="*60)
# Parent genome
parent = Genome(
genes=[
Gene(name="base_model", value="gpt-4"),
Gene(name="temperature", value=0.7),
Gene(name="creativity", value=0.8),
],
allow_mutations=True, # Allow mutations in children
mutation_rate=0.0, # No random mutations
silent=True,
)
print("\n--- Parent Genome ---")
print(f" Hash: {parent.get_hash()}")
print(f" Generation: {parent._generation}")
for gene_info in parent.list_genes():
print(f" {gene_info['name']}: {gene_info['value']}")
# Create child with specific mutations
print("\n--- Creating Child with Mutations ---")
child = parent.replicate(
mutations={"temperature": 0.9, "creativity": 0.5},
inherit_expression=True
)
print(f" Child Hash: {child.get_hash()}")
print(f" Child Generation: {child._generation}")
for gene_info in child.list_genes():
print(f" {gene_info['name']}: {gene_info['value']}")
# Show differences
print("\n--- Genome Diff (Parent vs Child) ---")
diff = parent.diff(child)
for gene_name, (parent_val, child_val) in diff.items():
print(f" {gene_name}: {parent_val} -> {child_val}")
return parent, child
def demonstrate_telomere_lifecycle():
"""Demonstrate telomere-based lifecycle management."""
print("\n" + "="*60)
print("4. TELOMERE: LIFECYCLE MANAGEMENT")
print("="*60)
def on_phase_change(old_phase, new_phase):
print(f" Phase: {old_phase.value} -> {new_phase.value}")
def on_senescence(reason):
print(f" Senescence triggered: {reason.value}")
telomere = Telomere(
max_operations=100,
error_threshold=10,
allow_renewal=True,
on_phase_change=on_phase_change,
on_senescence=on_senescence,
silent=True,
)
print("\n--- Initial State ---")
status = telomere.get_status()
print(f" Phase: {status.phase.value}")
print(f" Telomere: {status.telomere_length}/{status.max_telomere_length}")
print(f" Health: {status.health_score:.1%}")
# Perform operations (shortens telomeres)
print("\n--- Performing Operations ---")
for i in range(80):
can_continue = telomere.tick()
if i % 20 == 19: # Report every 20 ops
status = telomere.get_status()
print(f" After {i+1} ops: "
f"Telomere={status.telomere_length}, "
f"Phase={status.phase.value}")
if not can_continue:
print(f" Stopped at operation {i+1}")
break
return telomere
def demonstrate_telomere_renewal():
"""Demonstrate telomere renewal (telomerase)."""
print("\n" + "="*60)
print("5. TELOMERE RENEWAL (TELOMERASE)")
print("="*60)
telomere = Telomere(
max_operations=50,
allow_renewal=True,
silent=True,
)
# Deplete telomeres
print("\n--- Depleting Telomeres ---")
for i in range(45):
telomere.tick()
status = telomere.get_status()
print(f" After 45 ops: Telomere={status.telomere_length}, Phase={status.phase.value}")
# Renew telomeres
print("\n--- Applying Telomerase ---")
success = telomere.renew(amount=30, reset_errors=True)
print(f" Renewal: {'Success' if success else 'Failed'}")
status = telomere.get_status()
print(f" After renewal: Telomere={status.telomere_length}, Phase={status.phase.value}")
# Continue operations
print("\n--- Continuing Operations ---")
for i in range(20):
can_continue = telomere.tick()
if not can_continue:
print(f" Stopped after {i+1} more ops")
break
status = telomere.get_status()
print(f" Final: Telomere={status.telomere_length}, Phase={status.phase.value}")
return telomere
def demonstrate_error_senescence():
"""Demonstrate error-based senescence."""
print("\n" + "="*60)
print("6. ERROR-BASED SENESCENCE")
print("="*60)
def on_senescence(reason):
print(f" SENESCENCE: {reason.value}")
telomere = Telomere(
max_operations=1000,
error_threshold=5, # Low threshold for demo
on_senescence=on_senescence,
silent=True,
)
print("\n--- Simulating Errors ---")
for i in range(6):
can_continue = telomere.record_error()
status = telomere.get_status()
print(f" Error {i+1}: Can continue={can_continue}, Phase={status.phase.value}")
if not can_continue:
break
return telomere
def demonstrate_integrated_lifecycle():
"""Demonstrate genome and telomere working together."""
print("\n" + "="*60)
print("7. INTEGRATED LIFECYCLE")
print("="*60)
# Create an agent with genome and telomere
genome = Genome(
genes=[
Gene(name="agent_type", value="worker", gene_type=GeneType.STRUCTURAL),
Gene(name="max_ops", value=100),
Gene(name="allow_renewal", value=True),
],
silent=True,
)
# Configure telomere from genome
config = genome.express()
telomere = Telomere(
max_operations=config["max_ops"],
allow_renewal=config["allow_renewal"],
silent=True,
)
print("\n--- Agent Configuration ---")
print(f" Type: {config['agent_type']}")
print(f" Max Operations: {config['max_ops']}")
print(f" Renewal Allowed: {config['allow_renewal']}")
# Simulate agent lifecycle
print("\n--- Agent Lifecycle ---")
operations = 0
while telomere.is_operational():
can_continue = telomere.tick(cost=5) # Each op costs 5 telomere units
operations += 1
# Check for senescence
if telomere.get_phase() == LifecyclePhase.SENESCENT:
print(f" Senescence at operation {operations}")
# Decision: renew or terminate
if config["allow_renewal"]:
print(" Attempting renewal...")
telomere.renew()
break
if operations >= 50: # Safety limit for demo
break
status = telomere.get_status()
stats = telomere.get_statistics()
print(f"\n--- Final Status ---")
print(f" Operations: {stats['operations_count']}")
print(f" Phase: {status.phase.value}")
print(f" Health: {status.health_score:.1%}")
print(f" Genome Hash: {genome.get_hash()}")
return genome, telomere
def main():
"""Run all genome and telomere demonstrations."""
try:
print("="*60)
print("GENOME AND TELOMERE LIFECYCLE DEMONSTRATION")
print("Immutable Configuration + Lifecycle Management")
print("="*60)
demonstrate_genome_basics()
demonstrate_gene_expression()
demonstrate_genome_inheritance()
demonstrate_telomere_lifecycle()
demonstrate_telomere_renewal()
demonstrate_error_senescence()
demonstrate_integrated_lifecycle()
print("\n" + "="*60)
print("All demonstrations complete!")
print("="*60)
except KeyboardInterrupt:
print("\nInterrupted.")
except Exception as e:
print(f"Error: {e}")
raise
if __name__ == "__main__":
main()