agent-teams 0.1.0

Generic Rust agent teams framework replicating Claude Code Agent Teams architecture with pluggable backends for Claude Code, Codex, and Gemini CLI
Documentation
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//! Checkpoint data model for attaching agent session context to Git commits.
//!
//! Checkpoints are structured JSON metadata stored as Git notes
//! (`refs/notes/agent-checkpoints`). Each checkpoint captures the full
//! agent session context alongside the code it produced.
//!
//! Two data tiers:
//! - **Core** (always, <10KB): session, commit, branch, files, team state, task summaries
//! - **Extended** (optional, 10-100KB): tool_calls, token_usage, full metadata

use std::collections::HashMap;
use std::path::PathBuf;

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

// Re-export token types that were extracted to the feature-independent token module.
// This preserves backward compatibility for code importing from `models::checkpoint::`.
pub use super::token::{
    TokenUsage, ToolCallRecord, CostSummary, AgentTokenUsage,
    MAX_PROMPT_SUMMARY_LEN, MAX_TOOL_INPUT_SUMMARY_LEN, truncate_string,
    estimate_cost,
};

/// Top-level checkpoint attached to a Git commit via notes.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Checkpoint {
    /// Unique checkpoint ID (derived from commit SHA + timestamp).
    pub id: String,

    /// The Git commit SHA this checkpoint is attached to.
    pub commit_sha: String,

    /// Branch name at checkpoint creation time.
    pub branch: String,

    /// When the checkpoint was created.
    pub created_at: DateTime<Utc>,

    /// Agent session context.
    pub session: CheckpointSession,

    /// Team state snapshot (if working in a team).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub team: Option<CheckpointTeamState>,

    /// Lightweight task snapshots from the team's task list.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tasks: Vec<CheckpointTask>,

    /// Files involved in this commit (from git diff).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub files: Vec<CheckpointFile>,

    /// Token usage statistics (extended tier).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub token_usage: Option<TokenUsage>,

    /// Tool call records (extended tier).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tool_calls: Vec<ToolCallRecord>,

    /// Arbitrary metadata for extensibility.
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub metadata: HashMap<String, serde_json::Value>,
}

/// Agent session context at checkpoint time.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CheckpointSession {
    /// Agent name (e.g. "team-lead", "coder-1").
    pub agent_name: String,

    /// Backend type string (e.g. "claude-code", "codex", "gemini-cli").
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub backend_type: Option<String>,

    /// Model used (e.g. "claude-sonnet-4-5-20250929").
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,

    /// Truncated prompt summary (max 2000 chars).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub prompt_summary: Option<String>,

    /// Working directory.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cwd: Option<PathBuf>,

    /// Session ID (for correlation with JSONL session files).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub session_id: Option<String>,
}

/// Team state snapshot embedded in a checkpoint.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CheckpointTeamState {
    /// Team name.
    pub team_name: String,

    /// Team description.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// Team members at checkpoint time.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub members: Vec<CheckpointMember>,
}

/// A team member snapshot.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CheckpointMember {
    /// Member name.
    pub name: String,
    /// Agent type (e.g. "general-purpose", "researcher").
    pub agent_type: String,
}

/// Lightweight task snapshot for checkpoint embedding.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CheckpointTask {
    /// Task ID.
    pub id: String,
    /// Task subject.
    pub subject: String,
    /// Task status.
    pub status: String,
    /// Task owner (agent name).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub owner: Option<String>,
}

/// Role of a file in a checkpoint (derived from git diff).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FileRole {
    Created,
    Modified,
    Deleted,
    Referenced,
}

impl std::fmt::Display for FileRole {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            FileRole::Created => write!(f, "created"),
            FileRole::Modified => write!(f, "modified"),
            FileRole::Deleted => write!(f, "deleted"),
            FileRole::Referenced => write!(f, "referenced"),
        }
    }
}

/// A file involved in a checkpoint.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CheckpointFile {
    /// File path relative to repo root.
    pub path: String,
    /// Role of the file in this commit.
    pub role: FileRole,
    /// SHA-256 hash of file content (for integrity verification).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub content_hash: Option<String>,
}

// TokenUsage, ToolCallRecord, MAX_PROMPT_SUMMARY_LEN, MAX_TOOL_INPUT_SUMMARY_LEN,
// and truncate_string are re-exported from super::token (see top of file).

impl Checkpoint {
    /// Create a new checkpoint with minimal required fields.
    pub fn new(
        commit_sha: impl Into<String>,
        branch: impl Into<String>,
        session: CheckpointSession,
    ) -> Self {
        let commit_sha = commit_sha.into();
        let now = Utc::now();
        let id = format!(
            "ckpt-{}-{}",
            &commit_sha[..7.min(commit_sha.len())],
            now.timestamp()
        );
        Self {
            id,
            commit_sha,
            branch: branch.into(),
            created_at: now,
            session,
            team: None,
            tasks: Vec::new(),
            files: Vec::new(),
            token_usage: None,
            tool_calls: Vec::new(),
            metadata: HashMap::new(),
        }
    }

    /// Check whether this checkpoint has extended data (tool calls or token usage).
    pub fn has_extended_data(&self) -> bool {
        self.token_usage.is_some() || !self.tool_calls.is_empty()
    }

    /// Approximate JSON size in bytes.
    pub fn estimated_size(&self) -> usize {
        // Quick estimate: serialize and check length
        serde_json::to_string(self).map(|s| s.len()).unwrap_or(0)
    }
}

impl CheckpointSession {
    /// Create a session from a `SessionState`.
    pub fn from_session_state(state: &crate::models::session::SessionState) -> Self {
        Self {
            agent_name: state.name.clone(),
            backend_type: Some(state.backend_type.clone()),
            model: state.model.clone(),
            prompt_summary: Some(truncate_string(&state.prompt, MAX_PROMPT_SUMMARY_LEN)),
            cwd: state.cwd.clone(),
            session_id: None,
        }
    }

    /// Create a minimal session with just an agent name.
    pub fn new(agent_name: impl Into<String>) -> Self {
        Self {
            agent_name: agent_name.into(),
            backend_type: None,
            model: None,
            prompt_summary: None,
            cwd: None,
            session_id: None,
        }
    }
}

impl CheckpointTeamState {
    /// Create from a `TeamConfig`.
    pub fn from_team_config(config: &crate::models::team::TeamConfig) -> Self {
        Self {
            team_name: config.team_name.clone(),
            description: config.description.clone(),
            members: config
                .members
                .iter()
                .map(|m| CheckpointMember {
                    name: m.name().to_string(),
                    agent_type: m.agent_type().to_string(),
                })
                .collect(),
        }
    }
}

impl CheckpointTask {
    /// Create from a `TaskFile`.
    pub fn from_task_file(task: &crate::models::task::TaskFile) -> Self {
        Self {
            id: task.id.clone(),
            subject: task.subject.clone(),
            status: task.status.to_string(),
            owner: task.owner.clone(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn serde_round_trip_checkpoint() {
        let session = CheckpointSession {
            agent_name: "test-agent".into(),
            backend_type: Some("claude-code".into()),
            model: Some("claude-sonnet-4-5-20250929".into()),
            prompt_summary: Some("You are a helpful assistant.".into()),
            cwd: Some(PathBuf::from("/tmp/test")),
            session_id: None,
        };

        let checkpoint = Checkpoint {
            id: "ckpt-abc1234-1700000000".into(),
            commit_sha: "abc1234567890".into(),
            branch: "main".into(),
            created_at: Utc::now(),
            session,
            team: Some(CheckpointTeamState {
                team_name: "my-team".into(),
                description: Some("Test team".into()),
                members: vec![CheckpointMember {
                    name: "lead".into(),
                    agent_type: "team-lead".into(),
                }],
            }),
            tasks: vec![CheckpointTask {
                id: "1".into(),
                subject: "Fix bug".into(),
                status: "in_progress".into(),
                owner: Some("coder".into()),
            }],
            files: vec![CheckpointFile {
                path: "src/main.rs".into(),
                role: FileRole::Modified,
                content_hash: Some("abc123".into()),
            }],
            token_usage: Some(TokenUsage {
                input_tokens: 1000,
                output_tokens: 500,
                cache_read_tokens: Some(200),
                cache_write_tokens: None,
            }),
            tool_calls: vec![ToolCallRecord {
                tool_name: "Read".into(),
                input_summary: Some("src/main.rs".into()),
                timestamp: Some(Utc::now()),
            }],
            metadata: HashMap::new(),
        };

        let json = serde_json::to_string_pretty(&checkpoint).unwrap();
        let parsed: Checkpoint = serde_json::from_str(&json).unwrap();

        assert_eq!(parsed.id, "ckpt-abc1234-1700000000");
        assert_eq!(parsed.commit_sha, "abc1234567890");
        assert_eq!(parsed.branch, "main");
        assert_eq!(parsed.session.agent_name, "test-agent");
        assert_eq!(parsed.team.as_ref().unwrap().team_name, "my-team");
        assert_eq!(parsed.tasks.len(), 1);
        assert_eq!(parsed.files.len(), 1);
        assert_eq!(parsed.files[0].role, FileRole::Modified);
        assert!(parsed.token_usage.is_some());
        assert_eq!(parsed.tool_calls.len(), 1);
    }

    #[test]
    fn truncate_string_works() {
        assert_eq!(truncate_string("hello", 10), "hello");
        assert_eq!(truncate_string("hello world", 8), "hello...");
        assert_eq!(truncate_string("", 5), "");
        assert_eq!(truncate_string("ab", 2), "ab");
    }

    #[test]
    fn checkpoint_new_generates_id() {
        let session = CheckpointSession::new("test-agent");
        let ckpt = Checkpoint::new("abc1234567890", "main", session);

        assert!(ckpt.id.starts_with("ckpt-abc1234-"));
        assert_eq!(ckpt.commit_sha, "abc1234567890");
        assert_eq!(ckpt.branch, "main");
        assert!(!ckpt.has_extended_data());
    }

    #[test]
    fn checkpoint_has_extended_data() {
        let session = CheckpointSession::new("test-agent");
        let mut ckpt = Checkpoint::new("abc1234567890", "main", session);

        assert!(!ckpt.has_extended_data());

        ckpt.token_usage = Some(TokenUsage {
            input_tokens: 100,
            output_tokens: 50,
            cache_read_tokens: None,
            cache_write_tokens: None,
        });
        assert!(ckpt.has_extended_data());
    }

    #[test]
    fn file_role_display() {
        assert_eq!(FileRole::Created.to_string(), "created");
        assert_eq!(FileRole::Modified.to_string(), "modified");
        assert_eq!(FileRole::Deleted.to_string(), "deleted");
        assert_eq!(FileRole::Referenced.to_string(), "referenced");
    }

    #[test]
    fn checkpoint_session_from_session_state() {
        let state = crate::models::session::SessionState {
            name: "coder-1".into(),
            backend_type: "claude-code".into(),
            prompt: "You are a Rust expert.".into(),
            model: Some("claude-opus-4-6".into()),
            cwd: Some(PathBuf::from("/project")),
            max_turns: None,
            allowed_tools: vec![],
            permission_mode: None,
            reasoning_effort: None,
            env: HashMap::new(),
            memory_config: None,
            metadata: HashMap::new(),
            created_at: Utc::now(),
        };

        let session = CheckpointSession::from_session_state(&state);
        assert_eq!(session.agent_name, "coder-1");
        assert_eq!(session.backend_type.as_deref(), Some("claude-code"));
        assert_eq!(session.model.as_deref(), Some("claude-opus-4-6"));
        assert_eq!(
            session.prompt_summary.as_deref(),
            Some("You are a Rust expert.")
        );
    }

    #[test]
    fn checkpoint_task_from_task_file() {
        let task = crate::models::task::TaskFile {
            id: "42".into(),
            subject: "Implement auth".into(),
            description: Some("Add JWT tokens".into()),
            active_form: None,
            status: crate::models::task::TaskStatus::InProgress,
            owner: Some("coder".into()),
            blocks: vec![],
            blocked_by: vec![],
            metadata: None,
        };

        let ckpt_task = CheckpointTask::from_task_file(&task);
        assert_eq!(ckpt_task.id, "42");
        assert_eq!(ckpt_task.subject, "Implement auth");
        assert_eq!(ckpt_task.status, "in_progress");
        assert_eq!(ckpt_task.owner.as_deref(), Some("coder"));
    }

    #[test]
    fn deserialize_minimal_checkpoint() {
        let json = r#"{
            "id": "ckpt-test-1",
            "commitSha": "abc123",
            "branch": "main",
            "createdAt": "2025-01-01T00:00:00Z",
            "session": {
                "agentName": "test"
            }
        }"#;

        let ckpt: Checkpoint = serde_json::from_str(json).unwrap();
        assert_eq!(ckpt.id, "ckpt-test-1");
        assert!(ckpt.team.is_none());
        assert!(ckpt.tasks.is_empty());
        assert!(ckpt.files.is_empty());
        assert!(ckpt.token_usage.is_none());
        assert!(ckpt.tool_calls.is_empty());
    }
}