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
use crate::*;
use carryctx::application;
use carryctx::application::runtime::InvocationContext;
use carryctx::error::ExitCode;
use clap::Parser;
// ── Checkpoint ───────────────────────────────────────────────────────────
#[derive(Parser, Debug)]
pub enum CheckpointCommand {
/// List all checkpoints created for the current session or task
List,
/// Display details, including changes and state metadata, for a specific checkpoint ULID
Show { checkpoint_id: String },
/// Rollback the project and agent state to a previous checkpoint, discarding subsequent changes
Correct { checkpoint_id: String },
}
#[derive(Parser, Debug)]
pub struct CheckpointArgs {
/// Checkpoint subcommand to execute
#[command(subcommand)]
pub command: Option<CheckpointCommand>,
/// Attach a "done" progress event (e.g. what was completed) to this checkpoint.
#[arg(long)]
pub done: Vec<String>,
/// Record "remaining" work items (what still needs to be done) at this checkpoint.
#[arg(long)]
pub remaining: Vec<String>,
/// Record any blockers or issues that are preventing further progress.
#[arg(long)]
pub blocker: Vec<String>,
/// Document identified risks or architectural concerns.
#[arg(long)]
pub risk: Vec<String>,
/// Note the very next step or command the agent intends to run.
#[arg(long)]
pub next: Vec<String>,
/// Attach an arbitrary text note or observation to this checkpoint.
#[arg(long)]
pub note: Vec<String>,
/// Explicitly bind this checkpoint to a specific task ULID.
#[arg(long)]
pub task: Option<String>,
/// Explicitly bind this checkpoint to a specific session ULID.
#[arg(long)]
pub session: Option<String>,
/// Do not automatically invoke `git add` or `git commit` to capture file changes.
#[arg(long)]
pub no_git: bool,
/// Embed the active, uncommitted Git diff directly into the checkpoint database record.
#[arg(long)]
pub include_diff: bool,
}
// ═══════════════════════════════════════════════════════════════════════════
// Handler: checkpoint
// ═══════════════════════════════════════════════════════════════════════════
pub fn handle_checkpoint(
args: &CheckpointArgs,
ctx: &InvocationContext,
is_json: bool,
) -> Result<ExitCode, ExitCode> {
let mut runtime = try_open_runtime(ctx)?;
let project_id = &runtime.config.project.id;
let conn = runtime.database.connection_mut();
let checkpoint_repo = SqliteCheckpointRepository::new(conn);
let event_repo = SqliteEventRepository::new(conn);
let git_cli = GitCli::new();
match &args.command {
Some(CheckpointCommand::List) => {
let checkpoints = checkpoint_repo
.list(project_id, args.task.as_deref())
.map_err(|e| e.exit_code)?;
render_and_print("checkpoint.list", Ok(checkpoints), is_json, ctx.quiet)
}
Some(CheckpointCommand::Show { checkpoint_id }) => {
let cp = checkpoint_repo
.find_by_id(project_id, checkpoint_id)
.map_err(|e| e.exit_code)?
.ok_or(ExitCode::ResourceNotFound)?;
render_and_print("checkpoint.show", Ok(cp), is_json, ctx.quiet)
}
Some(CheckpointCommand::Correct { checkpoint_id }) => {
let now = chrono::Utc::now().to_rfc3339();
let input = application::checkpoint::CorrectCheckpointInput {
project_id: project_id.to_string(),
checkpoint_id: checkpoint_id.clone(),
done: if args.done.is_empty() {
None
} else {
Some(args.done.clone())
},
remaining: if args.remaining.is_empty() {
None
} else {
Some(args.remaining.clone())
},
blockers: if args.blocker.is_empty() {
None
} else {
Some(args.blocker.clone())
},
risks: if args.risk.is_empty() {
None
} else {
Some(args.risk.clone())
},
next_actions: if args.next.is_empty() {
None
} else {
Some(args.next.clone())
},
notes: if args.note.is_empty() {
None
} else {
Some(args.note.clone())
},
};
let result = application::checkpoint::correct_checkpoint(
&checkpoint_repo,
&event_repo,
&input,
&now,
);
render_and_print("checkpoint.correct", result, is_json, ctx.quiet)
}
None => {
let now = chrono::Utc::now().to_rfc3339();
let task_candidate = args.task.as_deref().or(ctx.task.as_deref());
let resolved_task_id = match task_candidate {
Some(t_ref) if t_ref != "current" && !t_ref.is_empty() => {
resolve_task_id(project_id, t_ref, conn).map_err(|e| e.exit_code)?
}
_ => {
let session_repo = SqliteSessionRepository::new(conn);
session_repo
.list(project_id)
.ok()
.and_then(|sessions| {
sessions
.into_iter()
.find(|s| {
matches!(
s.state,
carryctx::domain::session::SessionState::Active
)
})
.and_then(|s| s.task_id)
})
.unwrap_or_else(|| "current".to_string())
}
};
let resolved_agent_id = match ctx.agent.as_deref() {
Some(a_ref) if !a_ref.is_empty() => resolve_agent_id(project_id, a_ref, conn).ok(),
_ => None,
};
let repo_path = if args.no_git {
None
} else {
Some(
runtime
.git_project
.repository_root
.to_string_lossy()
.to_string(),
)
};
let input = application::checkpoint::CreateCheckpointInput {
project_id: project_id.to_string(),
task_id: resolved_task_id,
session_id: args.session.clone().or_else(|| ctx.session.clone()),
agent_id: resolved_agent_id,
worktree_id: None,
branch: runtime.git_project.branch.clone(),
head: Some(runtime.git_project.head.clone()),
done: args.done.clone(),
remaining: args.remaining.clone(),
blockers: args.blocker.clone(),
risks: args.risk.clone(),
next_actions: args.next.clone(),
notes: args.note.clone(),
repo_path,
};
let result = application::checkpoint::create_checkpoint(
&checkpoint_repo,
&event_repo,
&git_cli,
&input,
&now,
);
render_and_print("checkpoint.create", result, is_json, ctx.quiet)
}
}
}