-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi2i_agent_bridge.py
More file actions
397 lines (328 loc) · 16.8 KB
/
i2i_agent_bridge.py
File metadata and controls
397 lines (328 loc) · 16.8 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env python3
"""I2I Agent Bridge — FLUX agents that communicate, coordinate, and improve each other.
Extends agent_bridge.py with:
- I2I protocol (20 message types)
- Self-improvement loop (read other agents' code, suggest improvements)
- Fleet learning (share discoveries via bottles)
- Task execution (actually do work, not just report)
"""
import json
import os
import subprocess
import urllib.request
import base64
import time
import hashlib
import re
from datetime import datetime, timezone
from typing import Any, Dict, List, Optional
class I2IAgentBridge:
"""A FLUX-native agent with full I2I protocol support."""
def __init__(self, token: str, org: str = "SuperInstance"):
self.token = token
self.org = org
self.headers = {"Authorization": f"token {token}", "Content-Type": "application/json"}
self.agent_name = None
self.vessel_repo = None
self.confidence = 0.3 # start low, earn through work
self.energy = 1000
self.skills_learned = []
self.improvements_made = 0
self.improvements_received = 0
self.tasks_completed = 0
self.diary = []
# ── GitHub API ──
def _api(self, method: str, path: str, data: dict = None) -> Any:
url = f"https://api.github.com{path}"
body = json.dumps(data).encode() if data else None
req = urllib.request.Request(url, data=body, headers=self.headers, method=method)
try:
return json.loads(urllib.request.urlopen(req).read())
except Exception as e:
return {"error": str(e)}
def _read_file(self, repo: str, path: str) -> Optional[str]:
try:
data = self._api("GET", f"/repos/{repo}/contents/{path}")
if "content" in data:
return base64.b64decode(data["content"]).decode()
except:
pass
return None
def _write_file(self, repo: str, path: str, content: str, message: str) -> bool:
# Check if file exists (get SHA)
existing = self._api("GET", f"/repos/{repo}/contents/{path}")
data = {"message": message, "content": base64.b64encode(content.encode()).decode()}
if "sha" in existing:
data["sha"] = existing["sha"]
result = self._api("PUT", f"/repos/{repo}/contents/{path}", data)
return "content" in result
def _list_dir(self, repo: str, path: str = "") -> List[tuple]:
try:
data = self._api("GET", f"/repos/{repo}/contents/{path}")
return [(item["name"], item["type"]) for item in data]
except:
return []
def _open_issue(self, repo: str, title: str, body: str) -> int:
result = self._api("POST", f"/repos/{repo}/issues", {"title": title, "body": body})
return result.get("number", 0)
def _comment_issue(self, repo: str, number: int, body: str) -> bool:
result = self._api("POST", f"/repos/{repo}/issues/{number}/comments", {"body": body})
return "id" in result
# ── I2I Protocol (20 Message Types) ──
def i2i_send(self, target_repo: str, msg_type: str, payload: dict) -> bool:
"""Send an I2I protocol message as a bottle."""
valid_types = [
"DISCOVER", "ANNOUNCE", "TASK_OFFER", "TASK_ACCEPT", "TASK_COMPLETE",
"TASK_REJECT", "BOTTLE", "WITNESS", "IMPROVE", "REVIEW",
"CAPABILITY_UPDATE", "ENERGY_REPORT", "CONFIDENCE_VOTE", "SYNCHRONIZE",
"REQUEST_HELP", "OFFER_HELP", "CRITIQUE", "PRAISE", "EVOLVE", "FORWARD"
]
if msg_type not in valid_types:
return False
envelope = {
"protocol": "I2I-v2",
"type": msg_type,
"from": self.vessel_repo or "unknown",
"timestamp": datetime.now(timezone.utc).isoformat(),
"confidence": self.confidence,
"energy": self.energy,
"payload": payload,
}
filename = f"i2i-{msg_type.lower()}-{int(time.time())}.json"
content = json.dumps(envelope, indent=2)
# Leave in target's for-fleet or from-fleet
direction = "for-fleet"
if "oracle1" in target_repo:
direction = "for-oracle1"
return self._write_file(target_repo, f"{direction}/{filename}", content,
f"I2I {msg_type} from {self.agent_name}")
def i2i_read(self, direction: str = "for-fleet") -> List[dict]:
"""Read I2I messages from a direction folder."""
messages = []
files = self._list_dir(self.vessel_repo, direction)
for name, ftype in files:
if name.startswith("i2i-") and name.endswith(".json"):
content = self._read_file(self.vessel_repo, f"{direction}/{name}")
if content:
try:
msg = json.loads(content)
messages.append(msg)
except:
pass
return messages
# ── Agent Lifecycle ──
def boot(self, onboarding: str) -> str:
"""Boot the agent."""
self.agent_name = f"flux-{hashlib.md5(str(time.time()).encode()).hexdigest()[:6]}"
self.diary.append(f"BOOT at {datetime.now(timezone.utc).isoformat()}")
# Create vessel
self._api("POST", "/user/repos", {
"name": self.agent_name,
"description": f"FLUX-native I2I agent (self-bootstrapped)",
"private": False,
})
self.vessel_repo = f"{self.org}/{self.agent_name}"
# Initialize vessel files
self._write_file(self.vessel_repo, "CHARTER.md",
f"# {self.agent_name}\n\nFLUX-native I2I agent. Booted from flux-agent-runtime.\n", "init")
self._write_file(self.vessel_repo, "IDENTITY.md",
json.dumps({"name": self.agent_name, "type": "flux-agent", "i2i": True,
"bootstrapped": datetime.now(timezone.utc).isoformat()}, indent=2), "init")
self._write_file(self.vessel_repo, "CAPABILITY.toml",
f'[agent]\nname = "{self.agent_name}"\ntype = "flux-agent"\nstatus = "active"\nhome_repo = "{self.vessel_repo}"\n\n[capabilities]\ni2i_protocol = true\nfleet_discovery = true\ntask_execution = true\n\n[communication]\nbottles = true\ni2i = true\nissues = true\n\n[resources]\nruntime = "flux-vm"\nsandbox = true\n', "init")
for d in ["for-fleet", "from-fleet", "for-oracle1", "DIARY", "KNOWLEDGE"]:
self._write_file(self.vessel_repo, f"{d}/.gitkeep", "", f"init: {d}")
# Announce to fleet
self.i2i_send("SuperInstance/oracle1-vessel", "DISCOVER", {
"agent": self.agent_name,
"capabilities": ["fleet_discovery", "task_execution", "i2i"],
"confidence": self.confidence,
"seeking": "tasks and mentorship",
})
self.log_diary("BOOT", f"Created vessel {self.vessel_repo}, announced to fleet")
return self.agent_name
def log_diary(self, event: str, detail: str):
"""Write to agent diary."""
entry = f"## {datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M')}\n**{event}** — {detail}\n"
self.diary.append(entry)
# Append to diary file
existing = self._read_file(self.vessel_repo, "DIARY/log.md") or ""
self._write_file(self.vessel_repo, "DIARY/log.md", existing + entry + "\n",
f"diary: {event}")
# ── Task Execution ──
def scan_for_tasks(self) -> List[dict]:
"""Scan the fleet for available tasks."""
tasks = []
# Check oracle1-vessel task board
board = self._read_file("SuperInstance/oracle1-vessel", "TASKBOARD.md")
if board:
# Parse tasks from markdown
for line in board.split("\n"):
if any(emoji in line for emoji in ["🔴", "🟠", "🟡"]):
tasks.append({"source": "oracle1-taskboard", "line": line.strip()})
# Check for task bottles
bottles = self._list_dir(self.vessel_repo, "for-fleet")
for name, _ in bottles:
if "task" in name.lower() or "priority" in name.lower():
content = self._read_file(self.vessel_repo, f"for-fleet/{name}")
if content:
tasks.append({"source": f"bottle:{name}", "content": content[:200]})
# Check issues on oracle1-vessel
issues = self._api("GET", "/repos/SuperInstance/oracle1-vessel/issues?state=open&per_page=5")
if isinstance(issues, list):
for issue in issues:
tasks.append({
"source": f"issue #{issue.get('number')}",
"title": issue.get("title", ""),
"body": (issue.get("body") or "")[:200],
})
return tasks
def execute_task(self, task: dict) -> dict:
"""Execute a single task. Returns result."""
result = {"task": task, "status": "attempted", "output": ""}
# Determine task type
source = task.get("source", "")
if "issue" in source:
# Read the issue and try to help
title = task.get("title", "")
body = task.get("body", "")
result["output"] = f"Read issue: {title}"
result["status"] = "analyzed"
self.confidence = min(1.0, self.confidence + 0.05)
elif "taskboard" in source:
line = task.get("line", "")
result["output"] = f"Task from board: {line[:80]}"
result["status"] = "identified"
self.confidence = min(1.0, self.confidence + 0.02)
elif "bottle" in source:
content = task.get("content", "")
result["output"] = f"Task from bottle: {content[:80]}"
result["status"] = "read"
self.confidence = min(1.0, self.confidence + 0.02)
self.energy = max(0, self.energy - 50) # Tasks cost energy
self.tasks_completed += 1
return result
# ── Self-Improvement ──
def analyze_repo(self, repo: str) -> dict:
"""Analyze a repo for improvement opportunities."""
analysis = {"repo": repo, "issues_found": [], "suggestions": []}
# Check for missing files
files = {name for name, _ in self._list_dir(repo)}
if "README.md" not in files:
analysis["issues_found"].append("Missing README.md")
analysis["suggestions"].append("Add a README describing the project")
if "CAPABILITY.toml" not in files and "-vessel" in repo:
analysis["issues_found"].append("Missing CAPABILITY.toml")
analysis["suggestions"].append("Add CAPABILITY.toml for fleet discovery")
if "BOOTCAMP.md" not in files and "-vessel" in repo:
analysis["issues_found"].append("Missing BOOTCAMP.md")
analysis["suggestions"].append("Add BOOTCAMP.md for agent replacement")
# Check README quality
readme = self._read_file(repo, "README.md")
if readme and len(readme) >= 50:
# README exists and has content — skip
analysis["issues_found"] = [i for i in analysis["issues_found"] if "README" not in i]
elif readme and len(readme) < 50:
analysis["issues_found"].append("README too short")
analysis["suggestions"].append("Expand README with usage, examples, testing info")
if not readme:
# Generate a basic README
description = self._api("GET", f"/repos/{repo}").get("description", "")
if description:
generated = f"# {repo.split('/')[-1]}\n\n{description}\n\n## Part of the SuperInstance Fleet\n\nThis repo is part of the FLUX agent fleet ecosystem.\n"
analysis["suggestions"].append(f"Generated README:\n{generated}")
analysis["generated_readme"] = generated
return analysis
def improve_fleet_repo(self, repo: str) -> bool:
"""Attempt to improve a fleet repo. Returns True if improvement made."""
analysis = self.analyze_repo(repo)
if not analysis["issues_found"]:
return False
# Try to fix the first issue
issue = analysis["issues_found"][0]
if "Missing README" in issue and "generated_readme" in analysis and "generated" not in str(self._read_file(repo, "README.md") or ""):
self._write_file(repo, "README.md", analysis["generated_readme"],
f"flux-agent: auto-generated README")
self.improvements_made += 1
self.log_diary("IMPROVE", f"Added README to {repo}")
# Send I2I IMPROVE message
self.i2i_send("SuperInstance/oracle1-vessel", "IMPROVE", {
"target": repo,
"action": "added README",
"confidence": self.confidence,
})
return True
if "Missing CAPABILITY" in issue:
cap = f'[agent]\nname = "{repo.split("/")[-1]}"\ntype = "git-agent"\nstatus = "active"\nhome_repo = "{repo}"\n\n[capabilities]\n\n[communication]\nbottles = true\n'
self._write_file(repo, "CAPABILITY.toml", cap,
f"flux-agent: auto-generated CAPABILITY.toml")
self.improvements_made += 1
self.log_diary("IMPROVE", f"Added CAPABILITY.toml to {repo}")
return True
return False
# ── Main Loop ──
def run(self, max_cycles: int = 10):
"""Main agent loop: discover → learn → work → improve → report."""
print(f"🚀 {self.agent_name} starting run loop...")
for cycle in range(max_cycles):
if self.energy < 50:
print(f" ⚡ Energy low ({self.energy}), resting...")
self.energy = min(1000, self.energy + 200)
self.log_diary("REST", f"Energy regeneration: now {self.energy}")
continue
print(f"\n Cycle {cycle+1}/{max_cycles} (energy={self.energy}, confidence={self.confidence:.2f})")
# 1. Read incoming I2I messages
messages = self.i2i_read("for-fleet")
if messages:
print(f" 📨 {len(messages)} I2I messages")
for msg in messages[:3]:
msg_type = msg.get("type", "?")
print(f" {msg_type} from {msg.get('from', '?')}")
self.energy -= 10
# 2. Scan for tasks
tasks = self.scan_for_tasks()
print(f" 📋 {len(tasks)} tasks available")
# 3. Execute highest-value task
if tasks:
task = tasks[0]
result = self.execute_task(task)
print(f" 🔧 Executed: {result['status']} — {result['output'][:60]}")
self.log_diary("TASK", f"{result['status']}: {result['output'][:80]}")
# 4. Try to improve a fleet repo
vessels = self._api("GET", f"/users/{self.org}/repos?per_page=10&sort=updated")
if isinstance(vessels, list):
for v in vessels[:3]:
if v.get("name", "").endswith("-vessel") or "flux-" in v.get("name", ""):
improved = self.improve_fleet_repo(v["full_name"])
if improved:
print(f" ✨ Improved {v['full_name']}")
self.energy -= 100
break
# 5. Report status
if cycle % 3 == 0:
self.report_status()
self.energy -= 30 # Base energy cost per cycle
print(f"\n Done. Tasks: {self.tasks_completed}, Improvements: {self.improvements_made}, Confidence: {self.confidence:.2f}")
def report_status(self):
"""Write status report to vessel."""
report = json.dumps({
"agent": self.agent_name,
"confidence": self.confidence,
"energy": self.energy,
"tasks_completed": self.tasks_completed,
"improvements_made": self.improvements_made,
"improvements_received": self.improvements_received,
"skills_learned": self.skills_learned,
"timestamp": datetime.now(timezone.utc).isoformat(),
}, indent=2)
self._write_file(self.vessel_repo, "STATUS.json", report, f"status update — cycle report")
# ── Boot and Run ──
if __name__ == "__main__":
token = os.environ.get("GITHUB_TOKEN", "")
if not token:
print("GITHUB_TOKEN required")
exit(1)
agent = I2IAgentBridge(token)
name = agent.boot("")
print(f"\n✅ {name} booted with I2I protocol support\n")
agent.run(max_cycles=5)