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
use crate::*;
use carryctx::adapter::unit_of_work::UnitOfWork;
use carryctx::application;
use carryctx::application::runtime::{InvocationContext, ProjectRuntime};
use carryctx::error::ExitCode;
use clap::Parser;
// ── Event ────────────────────────────────────────────────────────────────
#[derive(Parser, Debug)]
pub enum EventCommand {
/// List events matching the specified filters
List {
/// Filter by associated task ULID
#[arg(long)]
task: Option<String>,
/// Filter by associated agent ULID
#[arg(long)]
agent: Option<String>,
/// Filter by associated session ULID
#[arg(long)]
session: Option<String>,
/// Filter by event type (e.g., TaskTransition, SessionStarted)
#[arg(long)]
event_type: Option<String>,
/// Only show events after this timestamp or relative duration
#[arg(long)]
since: Option<String>,
/// Only show events before this timestamp or relative duration
#[arg(long)]
until: Option<String>,
/// Limit the number of returned events
#[arg(long)]
limit: Option<u64>,
/// Resume listing after a previous page's opaque next_cursor token
#[arg(long)]
cursor: Option<String>,
},
/// Show full raw JSON details for a specific event ULID
Show { event_id: String },
}
#[derive(Parser, Debug)]
pub struct EventArgs {
/// Event subcommand to execute
#[command(subcommand)]
pub command: EventCommand,
}
// ═══════════════════════════════════════════════════════════════════════════
// Handler: event
// ═══════════════════════════════════════════════════════════════════════════
pub fn handle_event(
args: &EventArgs,
pre_opened: Option<ProjectRuntime>,
ctx: &InvocationContext,
is_json: bool,
) -> Result<ExitCode, ExitCode> {
// Reuse the dispatcher's pre-opened runtime when available; a second
// open only happens (and reports) when that failed.
let mut runtime = match pre_opened {
Some(runtime) => runtime,
None => open_runtime_or_report(ctx, "event")?,
};
let verbose = ctx.verbose || runtime.config.output.verbose;
let project_id = &runtime.config.project.id;
let conn = runtime.database.connection_mut();
match &args.command {
EventCommand::List {
task,
agent,
session,
event_type,
since,
until,
limit,
cursor,
} => {
// Resolve agent/task references loudly (CTX-0080): swallowing a
// rejected reference here — e.g. a deactivated agent — used to
// widen the filter to ALL events. `search --assignee` errors
// through resolve_or_render; this handler must behave
// identically, so both share the loud failure path.
let resolved = resolve_or_render(
"event.list",
(|| -> Result<(Option<String>, Option<String>), CarryCtxError> {
let resolved_agent_id = match agent.as_deref() {
Some(a) if !a.trim().is_empty() => {
// The local --agent clashes with the global
// --agent (CARRYCTX_AGENT env), so resolve it to
// a ULID instead of filtering by raw name.
Some(resolve_agent_id(project_id, a, conn)?)
}
_ => None,
};
let resolved_task_id = match task.as_deref() {
Some(t) if !t.trim().is_empty() => {
Some(resolve_task_id(project_id, t, conn)?)
}
_ => None,
};
Ok((resolved_agent_id, resolved_task_id))
})(),
ctx,
is_json,
verbose,
ctx.fields.as_deref(),
Some(&runtime.config.output.fields),
)?;
let (resolved_agent_id, resolved_task_id) = resolved;
let filter = EventFilter {
project_id: project_id.to_string(),
task_id: resolved_task_id,
agent_id: resolved_agent_id,
session_id: session.clone(),
event_type: event_type.clone(),
since: since.clone(),
until: until.clone(),
limit: *limit,
};
// Keyset pagination lives in the application layer: opaque
// `(occurred_at, id)` cursor tokens keep bulk transitions that
// share one timestamp from repeating across pages, and a full
// page emits a real `next_cursor`.
let uow = UnitOfWork::begin(conn).map_err(|e| e.exit_code)?;
let page = resolve_or_render(
"event.list",
application::event::list_events(project_id, &filter, cursor.as_deref(), &uow),
ctx,
is_json,
verbose,
ctx.fields.as_deref(),
Some(&runtime.config.output.fields),
)?;
// Markdown format support
if ctx.format == carryctx::application::runtime::OutputFormat::Markdown {
let mut out = String::from("# Events\n\n");
out.push_str("| Type | Agent | Occurred At |\n");
out.push_str("|---|---|---|\n");
for e in &page.events {
let agent = e
.actor_agent_id
.as_deref()
.map(|a| truncate_chars(a, 8))
.unwrap_or_else(|| "-".to_string());
out.push_str(&format!(
"| {} | {} | {} |\n",
e.event_type,
agent,
truncate_chars(&e.occurred_at, 19)
));
}
if !ctx.quiet {
print!("{out}");
}
return Ok(ExitCode::Success);
}
let result =
serde_json::json!({"events": page.events, "next_cursor": page.next_cursor});
render_and_print_entity(
"event.list",
Ok(result),
is_json,
ctx.quiet,
verbose,
ctx.fields.as_deref(),
Some(&runtime.config.output.fields),
)
}
EventCommand::Show { event_id } => {
let uow = UnitOfWork::begin(conn).map_err(|e| e.exit_code)?;
let result = application::event::show_event(project_id, event_id, &uow);
render_and_print_entity(
"event.show",
result,
is_json,
ctx.quiet,
verbose,
ctx.fields.as_deref(),
Some(&runtime.config.output.fields),
)
}
}
}