-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathworktree.rs
More file actions
240 lines (219 loc) · 9.45 KB
/
Copy pathworktree.rs
File metadata and controls
240 lines (219 loc) · 9.45 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
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//! Multi-Agent Worktree Integration Example
//!
//! Demonstrates the parts of the `WorktreeManager` API that are exposed for
//! multi-agent coordination:
//! - Creating multiple isolated agent workspaces inside one parent git repo.
//! - Listing, locking, and removing worktrees via `WorktreeManager`.
//! - Each agent owns an independent `GitVersionedKvStore` for its work.
//! - Bringing changes back together by writing into a shared main store
//! with `SemanticMergeResolver` for JSON-aware reconciliation.
//!
//! Note: `WorktreeVersionedKvStore::from_worktree` requires a fully populated
//! worktree checkout (a feature still in flight on the worktree adapter — see
//! `src/git/worktree.rs`), so this example uses the supported pattern of
//! independent per-agent stores under the worktree directory.
use parking_lot::Mutex;
use prollytree::diff::{ConflictResolver, MergeConflict, MergeResult, SemanticMergeResolver};
use prollytree::git::versioned_store::GitVersionedKvStore;
use prollytree::git::worktree::WorktreeManager;
use std::sync::Arc;
use tempfile::TempDir;
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Multi-Agent Worktree Integration Demo");
println!("=====================================");
let temp_dir = TempDir::new()?;
let repo_path = temp_dir.path();
init_git_repo(repo_path)?;
// Main shared store lives in <repo>/data
let mut main_store = GitVersionedKvStore::<16>::init(repo_path.join("data"))?;
main_store.insert(
b"shared_config".to_vec(),
br#"{"version": 1, "mode": "production"}"#.to_vec(),
)?;
main_store.insert(b"global_counter".to_vec(), b"0".to_vec())?;
main_store.commit("Initial shared data")?;
println!("[main] Initialized shared repository with shared_config + global_counter");
// The WorktreeManager tracks per-agent workspaces alongside the main repo.
let manager_arc = Arc::new(Mutex::new(WorktreeManager::new(repo_path)?));
let agent_scenarios = vec![
(
"agent1",
"customer-service",
br#"{"id": "cust_001", "issue": "billing", "priority": "high"}"#.as_slice(),
),
(
"agent2",
"data-analysis",
br#"{"users": 1250, "sessions": 3400, "conversion": 0.045}"#.as_slice(),
),
(
"agent3",
"content-generation",
br#"{"title": "Summer Sale", "body": "Get 20% off...", "target": "email"}"#.as_slice(),
),
];
// Each agent gets its own workspace tracked by the manager + an
// independent prolly store living in a sibling directory. The worktree
// path holds the linked-worktree metadata; the prolly store sits in its
// own self-contained git repo so we don't tangle the two layouts.
let agent_data_root = temp_dir.path().join("agent_data");
std::fs::create_dir_all(&agent_data_root)?;
let mut agent_stores: Vec<(String, GitVersionedKvStore<16>, String)> = Vec::new();
for (agent_id, session_id, _payload) in &agent_scenarios {
let workspace = repo_path.join(format!("{}_workspace", agent_id));
let branch_name = format!("{}-{}", agent_id, session_id);
let worktree_info = {
let mut manager = manager_arc.lock();
manager.add_worktree(&workspace, &branch_name, true)?
};
println!(
"[{}] Worktree id={} branch={}",
agent_id, worktree_info.id, worktree_info.branch
);
// Independent prolly dataset in its own git repo (outside the
// linked-worktree's path, which holds incomplete worktree metadata).
let agent_repo = agent_data_root.join(agent_id);
std::fs::create_dir_all(&agent_repo)?;
init_git_repo(&agent_repo)?;
let mut agent_store = GitVersionedKvStore::<16>::init(agent_repo.join("data"))?;
agent_store.commit("agent workspace bootstrap")?;
agent_stores.push((agent_id.to_string(), agent_store, branch_name));
}
// Each agent does its work in isolation.
println!("\n[agents] running independent workloads...");
for ((agent_id, agent_store, _branch), (_, _session, payload)) in
agent_stores.iter_mut().zip(agent_scenarios.iter())
{
match agent_id.as_str() {
"agent1" => {
agent_store.insert(b"customer:latest".to_vec(), payload.to_vec())?;
agent_store.insert(
b"response_templates".to_vec(),
br#"{"billing": "Thank you for contacting us about billing..."}"#.to_vec(),
)?;
agent_store.commit("agent1: customer-service updates")?;
}
"agent2" => {
agent_store.insert(b"analytics:daily".to_vec(), payload.to_vec())?;
agent_store.insert(b"global_counter".to_vec(), b"25".to_vec())?;
agent_store.commit("agent2: analytics updates")?;
}
"agent3" => {
agent_store.insert(b"content:campaign".to_vec(), payload.to_vec())?;
agent_store.insert(
b"shared_config".to_vec(),
br#"{"version": 1, "mode": "production", "feature_flags": {"new_ui": true}}"#
.to_vec(),
)?;
agent_store.commit("agent3: content updates")?;
}
_ => {}
}
}
println!("[agents] all completed.");
// Bring agent work back into main store. Each (key, value) is inserted
// into main with the SemanticMergeResolver applied when conflicts arise.
println!("\n[merge] bringing agent work into main store...");
let semantic = SemanticMergeResolver::default();
for (agent_id, agent_store, _branch) in agent_stores.iter() {
for key in agent_store.list_keys() {
let value = agent_store.get(&key).expect("agent value");
let merged = match main_store.get(&key) {
Some(existing) if existing != value => {
let conflict = MergeConflict {
key: key.clone(),
base_value: None,
source_value: Some(value.clone()),
destination_value: Some(existing.clone()),
};
match semantic.resolve_conflict(&conflict) {
Some(MergeResult::Modified(_, merged_value))
| Some(MergeResult::Added(_, merged_value)) => merged_value,
_ => value.clone(),
}
}
Some(_) => value.clone(),
None => value.clone(),
};
main_store.insert(key.clone(), merged)?;
}
main_store.commit(&format!("merge {}'s work into main", agent_id))?;
}
println!("[merge] done.");
// Inspect the converged state.
println!("\n[main] converged state:");
for key in main_store.list_keys() {
let value = main_store.get(&key).expect("key present");
let key_str = String::from_utf8_lossy(&key);
let val_str = String::from_utf8_lossy(&value);
println!(" {} -> {}", key_str, val_str);
}
// Demonstrate WorktreeManager housekeeping: list, lock, remove.
println!("\n[manager] worktree housekeeping...");
{
let manager = manager_arc.lock();
let listed = manager.list_worktrees();
println!(" {} worktrees registered", listed.len());
for info in &listed {
println!(" - id={} branch={}", info.id, info.branch);
}
}
{
let mut manager = manager_arc.lock();
let first_id = manager
.list_worktrees()
.first()
.map(|w| w.id.clone())
.expect("at least one worktree");
manager.lock_worktree(&first_id, "demo: hold for a long-running job")?;
println!(
" locked worktree {}: {}",
first_id,
manager.is_locked(&first_id)
);
manager.unlock_worktree(&first_id)?;
println!(
" unlocked worktree {}: {}",
first_id,
manager.is_locked(&first_id)
);
}
println!("\nDemo complete.");
Ok(())
}
fn init_git_repo(repo_path: &std::path::Path) -> Result<(), Box<dyn std::error::Error>> {
use std::process::Command;
Command::new("git")
.args(["init"])
.current_dir(repo_path)
.output()?;
Command::new("git")
.args(["config", "user.name", "Multi-Agent Demo"])
.current_dir(repo_path)
.output()?;
Command::new("git")
.args(["config", "user.email", "demo@multiagent.ai"])
.current_dir(repo_path)
.output()?;
std::fs::write(repo_path.join("README.md"), "# Multi-Agent Repository")?;
Command::new("git")
.args(["add", "."])
.current_dir(repo_path)
.output()?;
Command::new("git")
.args(["commit", "-m", "Initial repository setup"])
.current_dir(repo_path)
.output()?;
Ok(())
}