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
use crate::*;
use carryctx::adapter::unit_of_work::UnitOfWork;
use carryctx::application;
use carryctx::application::collaboration::CreateDecisionInput;
use carryctx::application::runtime::InvocationContext;
use carryctx::error::{CarryCtxError, ExitCode};
use clap::Parser;
// ── Decision ─────────────────────────────────────────────────────────────
#[derive(Parser, Debug)]
pub enum DecisionCommand {
/// Record a new architectural or design decision (ADR)
Add {
/// The title or summary of the decision made
#[arg(long)]
title: String,
/// The context, problem statement, or background leading to this decision
#[arg(long)]
context: Option<String>,
/// The actual decision or chosen alternative
#[arg(long)]
decision: Option<String>,
/// The consequences, trade-offs, or impact of this decision
#[arg(long)]
consequences: Option<String>,
/// The reasoning behind this decision: why it was made, not just what was decided
#[arg(long)]
rationale: Option<String>,
/// Task ULID that prompted or is associated with this decision
#[arg(long)]
task: Option<String>,
},
/// List all decisions recorded in the project
List,
/// Show full details of a specific decision
Show { decision_ref: String },
/// Search decisions by keyword or content
Search { query: String },
/// Mark a previous decision as superseded by a new one
Supersede {
decision_ref: String,
/// The ULID of the new decision that supersedes this one
#[arg(long)]
by: String,
},
}
#[derive(Parser, Debug)]
pub struct DecisionArgs {
/// Decision subcommand to execute
#[command(subcommand)]
pub command: DecisionCommand,
}
// ═══════════════════════════════════════════════════════════════════════════
// Handler: decision
// ═══════════════════════════════════════════════════════════════════════════
pub fn handle_decision(
args: &DecisionArgs,
ctx: &InvocationContext,
is_json: bool,
) -> Result<ExitCode, ExitCode> {
if let Some(result) = check_dry_run(ctx, &format!("decision {:?}", args.command)) {
return result;
}
let mut runtime = try_open_runtime(ctx)?;
let verbose = ctx.verbose || runtime.config.output.verbose;
let project_id = &runtime.config.project.id;
let conn = runtime.database.connection_mut();
let now = chrono::Utc::now().to_rfc3339();
match &args.command {
DecisionCommand::Add {
title,
context,
decision,
consequences,
rationale,
task,
} => {
let task_id = match &task.clone().or_else(|| ctx.task.clone()) {
Some(t) if !t.is_empty() => match resolve_task_id(project_id, t, conn) {
Ok(id) => id,
Err(e) => {
return render_and_print_entity::<serde_json::Value>(
"decision.add",
Err(e),
is_json,
ctx.quiet,
verbose,
ctx.fields.as_deref(),
Some(&runtime.config.output.fields),
);
}
},
_ => {
return render_and_print_entity::<serde_json::Value>(
"decision.add",
Err(CarryCtxError::validation_error(
"No task specified. Provide --task <TASK_REF> for the decision.",
)),
is_json,
ctx.quiet,
verbose,
ctx.fields.as_deref(),
Some(&runtime.config.output.fields),
);
}
};
let agent_id = match ctx.agent.clone() {
Some(id) => id,
None => {
return render_and_print_entity::<serde_json::Value>(
"decision.add",
Err(CarryCtxError::validation_error(
"No agent specified. Set CARRYCTX_AGENT or use --agent <AGENT>.",
)),
is_json,
ctx.quiet,
verbose,
ctx.fields.as_deref(),
Some(&runtime.config.output.fields),
);
}
};
let tx = conn
.transaction()
.map_err(|e| CarryCtxError::database_error(format!("{e}")).exit_code)?;
let uow = UnitOfWork::new(tx);
let input = CreateDecisionInput {
task_id,
title: title.clone(),
context: context.clone(),
decision: decision.clone(),
consequences: consequences.clone(),
rationale: rationale.clone(),
related_tasks: vec![],
related_paths: vec![],
created_by_agent: agent_id,
created_by_session: ctx.session.clone(),
};
let result = application::collaboration::create_decision(project_id, &input, &uow);
let committed = result.and_then(|d| uow.commit().map(|_| d));
render_and_print_entity(
"decision.add",
committed,
is_json,
ctx.quiet,
verbose,
ctx.fields.as_deref(),
Some(&runtime.config.output.fields),
)
}
DecisionCommand::List => {
let decision_repo = SqliteDecisionRepository::new(conn);
let result = decision_repo.list(project_id);
// Markdown format support
if ctx.format == carryctx::application::runtime::OutputFormat::Markdown {
let md = match &result {
Ok(decisions) => {
let mut out = String::from("# Decisions\n\n");
out.push_str("| ID | Title | Agent | Created |\n");
out.push_str("|---|---|---|---|\n");
for d in decisions {
let title_short = if d.title.len() > 40 {
format!("{}...", &d.title[..40])
} else {
d.title.clone()
};
let agent_short =
&d.created_by_agent[..d.created_by_agent.len().min(8)];
out.push_str(&format!(
"| {} | {} | {} | {} |\n",
d.display_id,
title_short,
agent_short,
&d.created_at[..10]
));
}
out
}
Err(e) => format!("Error: {e}"),
};
if !ctx.quiet {
print!("{md}");
}
return Ok(ExitCode::Success);
}
render_and_print_entity(
"decision.list",
result,
is_json,
ctx.quiet,
verbose,
ctx.fields.as_deref(),
Some(&runtime.config.output.fields),
)
}
DecisionCommand::Show { decision_ref } => {
let decision_repo = SqliteDecisionRepository::new(conn);
let item = decision_repo
.find_by_display_id(project_id, decision_ref)
.map_err(|e| e.exit_code)?
.or_else(|| {
decision_repo
.find_by_id(project_id, decision_ref)
.ok()
.flatten()
})
.ok_or(ExitCode::ResourceNotFound)?;
render_and_print_entity(
"decision.show",
Ok(item),
is_json,
ctx.quiet,
verbose,
ctx.fields.as_deref(),
Some(&runtime.config.output.fields),
)
}
DecisionCommand::Search { query } => {
let decision_repo = SqliteDecisionRepository::new(conn);
let result = decision_repo.search(project_id, query);
render_and_print_entity(
"decision.search",
result,
is_json,
ctx.quiet,
verbose,
ctx.fields.as_deref(),
Some(&runtime.config.output.fields),
)
}
DecisionCommand::Supersede { decision_ref, by } => {
let decision_repo = SqliteDecisionRepository::new(conn);
let event_repo = SqliteEventRepository::new(conn);
let resolved = decision_repo
.find_by_display_id(project_id, decision_ref)
.map_err(|e| e.exit_code)?
.or_else(|| {
decision_repo
.find_by_id(project_id, decision_ref)
.ok()
.flatten()
})
.ok_or(ExitCode::ResourceNotFound)?;
let result = decision_repo.supersede(&resolved.id, project_id, by, &now);
if result.is_ok() {
let _ = event_repo.append(&NewEvent {
id: ulid::Ulid::generate().to_string(),
project_id: project_id.to_string(),
event_type: "decision.superseded".into(),
actor_agent_id: ctx.agent.clone(),
session_id: ctx.session.clone(),
task_id: None,
payload: serde_json::json!({
"decisionId": resolved.id,
"supersededBy": by
}),
occurred_at: chrono::Utc::now().to_rfc3339(),
});
}
render_and_print_entity(
"decision.supersede",
result,
is_json,
ctx.quiet,
verbose,
ctx.fields.as_deref(),
Some(&runtime.config.output.fields),
)
}
}
}