-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha2a_git_aware.py
More file actions
264 lines (215 loc) · 8.1 KB
/
Copy patha2a_git_aware.py
File metadata and controls
264 lines (215 loc) · 8.1 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
a2a Git-Aware Features — Prevent work collisions in collaborative development.
Tracks git branch state and recent commits to alert agents about
potential work collisions and duplicated efforts.
"""
import subprocess
import json
from typing import Optional, Dict, List, Any
from pathlib import Path
from datetime import datetime
class GitAwareClient:
"""Git-aware agent client for work-collision prevention."""
def __init__(self, agent_id: str, repo_path: Optional[str] = None):
"""Initialize git-aware client.
Args:
agent_id: This agent's ID
repo_path: Path to git repository (defaults to cwd)
Raises:
ValueError: If agent_id is empty or whitespace-only
"""
if not agent_id or not agent_id.strip():
raise ValueError("agent_id must not be empty")
self.agent_id = agent_id
self.repo_path = Path(repo_path or ".")
def _run_git(self, cmd: str) -> str:
"""Run git command and return output.
Args:
cmd: Git command (without 'git' prefix)
Returns:
Command output
"""
try:
result = subprocess.run(
f"git {cmd}",
cwd=self.repo_path,
capture_output=True,
text=True,
shell=True,
timeout=5,
)
return result.stdout.strip()
except Exception as e:
return f"Error: {e}"
def get_current_branch(self) -> str:
"""Get current git branch name.
Returns:
Branch name or 'unknown'
"""
output = self._run_git("rev-parse --abbrev-ref HEAD")
return output if output and "Error" not in output else "unknown"
def get_recent_commits(self, count: int = 10) -> List[Dict[str, Any]]:
"""Get recent commits on current branch.
Args:
count: Number of commits to retrieve
Returns:
List of commit dicts with hash, author, message, timestamp
"""
cmd = f"log -{count} --format='%H|%an|%s|%aI'"
output = self._run_git(cmd)
commits = []
if output and "Error" not in output:
for line in output.split("\n"):
if not line:
continue
parts = line.split("|")
if len(parts) >= 4:
commits.append(
{
"hash": parts[0],
"author": parts[1],
"message": parts[2],
"timestamp": parts[3],
}
)
return commits
def get_changed_files(self) -> List[str]:
"""Get files changed since last commit.
Returns:
List of changed file paths
"""
output = self._run_git("diff --name-only")
return output.split("\n") if output and "Error" not in output else []
def get_branch_status(self) -> Dict[str, Any]:
"""Get detailed branch status.
Returns:
Dict with branch info, commits, changed files
"""
return {
"branch": self.get_current_branch(),
"commits": self.get_recent_commits(5),
"changed_files": self.get_changed_files(),
"timestamp": datetime.now().isoformat(),
"agent": self.agent_id,
}
def detect_work_collision(
self, other_agents_branches: List[Dict[str, Any]]
) -> Dict[str, Any]:
"""Detect potential work collisions with other agents.
Args:
other_agents_branches: List of branch status from other agents
Returns:
Dict with collision warnings and recommendations
"""
my_branch = self.get_current_branch()
my_files = set(self.get_changed_files())
my_commits = {c["hash"]: c for c in self.get_recent_commits()}
collisions = {
"agent": self.agent_id,
"branch": my_branch,
"warnings": [],
"recommendations": [],
}
for other in other_agents_branches:
# Same branch work
if other.get("branch") == my_branch:
collisions["warnings"].append(
f"⚠️ {other.get('agent')} also working on '{my_branch}'"
)
collisions["recommendations"].append(
f"Coordinate with {other.get('agent')} or use feature branches"
)
# Overlapping file changes
other_files = set(other.get("changed_files") or [])
overlap = my_files & other_files
if overlap:
collisions["warnings"].append(
f"⚠️ {other.get('agent')} modifying: {', '.join(list(overlap)[:3])}"
)
collisions["recommendations"].append(
"Consider pairing or sequential work on overlapping files"
)
# Duplicate commits
other_commits = {c["hash"] for c in (other.get("commits") or [])}
duplicate = set(my_commits.keys()) & other_commits
if duplicate:
collisions["warnings"].append(
f"⚠️ Duplicate commits detected with {other.get('agent')}"
)
collisions["recommendations"].append(
"Review recent commits - may indicate branch merge needed"
)
return collisions
def get_collaboration_summary(self) -> str:
"""Get human-readable collaboration summary.
Returns:
Formatted text summary
"""
status = self.get_branch_status()
summary = f"""
Git Status for {self.agent_id}:
- Branch: {status['branch']}
- Recent commits: {len(status['commits'])}
- Changed files: {len(status['changed_files'])}
- Last change: {status['timestamp']}
Changed files:
"""
for f in status["changed_files"][:10]:
summary += f" - {f}\n"
if status["changed_files"] and len(status["changed_files"]) > 10:
summary += f" ... and {len(status['changed_files']) - 10} more\n"
return summary
def format_for_bus(self) -> str:
"""Format git status as JSON for a2a bus.
Returns:
JSON string with git status
"""
return json.dumps(self.get_branch_status())
@staticmethod
def parse_bus_message(message: str) -> Optional[Dict[str, Any]]:
"""Parse git status from a2a bus message.
Args:
message: Message body from a2a bus
Returns:
Parsed dict or None if not valid git status
"""
try:
return json.loads(message)
except json.JSONDecodeError:
return None
def announce_work_status(client: "GitAwareClient", a2a_client) -> None:
"""Announce current work status to a2a bus.
Args:
client: GitAwareClient instance
a2a_client: A2AClient instance for messaging
"""
message = client.format_for_bus()
a2a_client.send("all", message)
print(f"✓ Work status announced to bus")
def check_collisions(
client: "GitAwareClient", a2a_client, other_agents: List[str]
) -> None:
"""Check for work collisions with other agents.
Args:
client: GitAwareClient instance
a2a_client: A2AClient instance
other_agents: List of agent IDs to check
"""
print("Checking for work collisions...")
other_statuses = []
for agent in (other_agents or []):
# In real use, would poll recent messages from other agents
# For now, just structure the function
pass
collisions = client.detect_work_collision(other_statuses)
if collisions["warnings"]:
print(f"\n⚠️ Collision warnings for {client.agent_id}:")
for warning in collisions["warnings"]:
print(f" {warning}")
print(f"\n💡 Recommendations:")
for rec in collisions["recommendations"]:
print(f" • {rec}")
else:
print("✓ No work collisions detected")