agent-team-mail 1.2.3

CLI for local agent team mail workflows.
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
use atm_core::error::{AtmError, AtmErrorCode};
use atm_core::observability::{
    AtmLogQuery, AtmLogSnapshot, AtmObservabilityHealth, CommandEvent, LogTailSession,
    ObservabilityPort, action_name, outcome_label,
};
use atm_core::types::{AgentName, TeamName};

use crate::constants::ATM_SERVICE_NAME;
/// Structured CLI-owned observability construction options.
///
/// L.5 intentionally keeps the release surface narrow: one explicit
/// construction entry point without introducing a broader builder or unified
/// observer abstraction.
#[allow(
    unfulfilled_lint_expectations,
    reason = "This release-surface options type is live in normal builds even though the dead-code expectation remains documented for narrower test configurations."
)]
#[expect(
    dead_code,
    reason = "CliObservabilityOptions is a release-surface construction option even when some binaries do not exercise every field."
)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct CliObservabilityOptions {
    pub stderr_logs: bool,
}

/// ATM CLI observability handle.
///
/// Clone is intentionally not derived; see rationale below.
///
/// `Clone` is intentionally not implemented because the concrete adapter owns a
/// boxed trait object without a shared-clone contract.
pub struct CliObservability {
    inner: Box<dyn ObservabilityPort + Send + Sync>,
}

impl std::fmt::Debug for CliObservability {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CliObservability").finish_non_exhaustive()
    }
}

impl CliObservability {
    pub(crate) fn from_boxed_port(inner: Box<dyn ObservabilityPort + Send + Sync>) -> Self {
        Self { inner }
    }

    /// Test-bootstrap escape hatch; production paths must use
    /// `CliObservability::new`.
    pub fn fallback() -> Self {
        Self {
            inner: Box::new(atm_core::observability::NullObservability),
        }
    }

    pub fn report_fatal_error(
        &self,
        stage: &'static str,
        error: &(dyn std::error::Error + 'static),
    ) {
        let (code, message) = if let Some(atm_error) = error.downcast_ref::<AtmError>() {
            (atm_error.code, atm_error.to_string())
        } else {
            (AtmErrorCode::InternalError, error.to_string())
        };

        let identity = std::env::var("ATM_IDENTITY").unwrap_or_else(|_| "unknown".to_string());
        let team = std::env::var("ATM_TEAM").unwrap_or_else(|_| "unknown".to_string());
        let fallback_agent: AgentName = match "unknown".parse() {
            Ok(agent) => agent,
            Err(_) => return,
        };
        let fallback_team: TeamName = match "unknown".parse() {
            Ok(team) => team,
            Err(_) => return,
        };
        let agent = identity.parse().unwrap_or(fallback_agent);
        if let Err(emit_error) = self.emit(CommandEvent {
            command: ATM_SERVICE_NAME,
            action: action_name(stage),
            outcome: outcome_label("error"),
            team: team.parse().unwrap_or(fallback_team),
            agent: agent.clone(),
            sender: agent,
            message_id: None,
            requires_ack: false,
            dry_run: false,
            task_id: None,
            error_code: Some(code),
            error_message: Some(message),
        }) {
            eprintln!("{}", fatal_emit_failure_message(stage, &emit_error));
        }
    }

    pub(crate) fn emit_command_event(&self, event: CommandEvent) {
        let command = event.command;
        let action = event.action.as_str().to_string();
        if let Err(emit_error) = self.emit(event) {
            eprintln!(
                "{}",
                command_emit_failure_message(command, &action, &emit_error)
            );
        }
    }

    /// Test-only helper for injecting a synthetic observability port without
    /// exposing the boxed inner field to production callers.
    #[cfg(test)]
    pub(crate) fn from_test_port(port: impl ObservabilityPort + Send + Sync + 'static) -> Self {
        Self {
            inner: Box::new(port),
        }
    }

    #[allow(
        unfulfilled_lint_expectations,
        reason = "The explicit constructor remains part of the production surface even when the dead-code expectation is not triggered in this build graph."
    )]
    #[expect(
        dead_code,
        reason = "CliObservability::new is retained as the explicit production constructor even when some tests bootstrap through alternate seams."
    )]
    pub fn new(
        home_dir: &std::path::Path,
        options: CliObservabilityOptions,
    ) -> Result<Self, AtmError> {
        Ok(Self::from_boxed_port(crate::new_adapter_port(
            home_dir,
            options.stderr_logs,
        )?))
    }
}

impl ObservabilityPort for CliObservability {
    fn emit(&self, event: CommandEvent) -> Result<(), AtmError> {
        self.inner.emit(event)
    }

    fn query(&self, req: AtmLogQuery) -> Result<AtmLogSnapshot, AtmError> {
        self.inner.query(req)
    }

    fn follow(&self, req: AtmLogQuery) -> Result<LogTailSession, AtmError> {
        self.inner.follow(req)
    }

    fn health(&self) -> Result<AtmObservabilityHealth, AtmError> {
        self.inner.health()
    }
}

// L.5 dispositions:
// - UX-002 retained: boxed trait-object dispatch remains acceptable for
//   initial release because it keeps CLI bootstrap simple without forcing a
//   wider unified observer abstraction.
// - BP-001 retained: the sealed boundary remains in place so external crates
//   cannot bypass the intended ATM-owned adapter contract with arbitrary
//   ObservabilityPort impls.
// - UNI-003 retained as a defer decision: DoctorCommand injectability does not
//   participate in the ObservabilityPort contract; defer injectability to a
//   future sprint unless a concrete testing or feature need appears.
impl atm_core::boundary::sealed::Sealed for CliObservability {}

fn fatal_emit_failure_message(stage: &str, emit_error: &AtmError) -> String {
    format!("ATM fatal diagnostic emission failed during {stage}: {emit_error}")
}

fn command_emit_failure_message(command: &str, action: &str, emit_error: &AtmError) -> String {
    format!("ATM command observability emit failed for {command}/{action}: {emit_error}")
}

#[cfg(test)]
mod tests {
    use atm_core::error::AtmError;
    use atm_core::observability::{
        AtmLogQuery, AtmObservabilityHealth, AtmObservabilityHealthState, CommandEvent,
        LogLevelFilter, LogMode, LogOrder, LogTailSession, ObservabilityPort,
    };
    use atm_core::test_support::{EnvGuard, TEST_SENDER, TEST_TEAM};
    use serial_test::serial;
    use tempfile::TempDir;

    use super::{
        CliObservability, CliObservabilityOptions, command_emit_failure_message,
        fatal_emit_failure_message,
    };

    struct FailingEmitObservability;

    impl atm_core::boundary::sealed::Sealed for FailingEmitObservability {}

    impl ObservabilityPort for FailingEmitObservability {
        fn emit(&self, _event: CommandEvent) -> Result<(), AtmError> {
            Err(AtmError::observability_emit("synthetic emit failure"))
        }

        fn query(
            &self,
            _req: AtmLogQuery,
        ) -> Result<atm_core::observability::AtmLogSnapshot, AtmError> {
            Ok(atm_core::observability::AtmLogSnapshot::default())
        }

        fn follow(&self, _req: AtmLogQuery) -> Result<LogTailSession, AtmError> {
            Ok(LogTailSession::empty())
        }

        fn health(&self) -> Result<AtmObservabilityHealth, AtmError> {
            Ok(AtmObservabilityHealth {
                active_log_path: None,
                logging_state: AtmObservabilityHealthState::Unavailable,
                query_state: Some(AtmObservabilityHealthState::Unavailable),
                maintenance: None,
                diagnostic: None,
                detail: Some("synthetic".to_string()),
            })
        }
    }

    fn query(order: LogOrder) -> AtmLogQuery {
        AtmLogQuery {
            mode: LogMode::Snapshot,
            levels: vec![LogLevelFilter::Info],
            field_matches: vec![],
            since: None,
            until: None,
            limit: None,
            order,
        }
    }

    fn event(message_id: Option<&str>) -> CommandEvent {
        CommandEvent {
            command: "send",
            action: atm_core::observability::action_name("send"),
            outcome: atm_core::observability::outcome_label("sent"),
            team: TEST_TEAM.parse().expect("team"),
            agent: TEST_SENDER.parse().expect("agent"),
            sender: TEST_SENDER.parse().expect("agent"),
            message_id: message_id.map(|value| value.parse().expect("message id")),
            requires_ack: false,
            dry_run: false,
            task_id: Some("TASK-1".parse().expect("task id")),
            error_code: None,
            error_message: None,
        }
    }

    #[test]
    #[serial]
    fn concrete_adapter_uses_host_scoped_default_log_path() {
        let tempdir = TempDir::new().expect("tempdir");
        let _env = EnvGuard::set_many([
            ("ATM_LOG", Some("info")),
            ("ATM_LOG_DIR", None),
            ("HOME", Some(tempdir.path().to_str().expect("utf8 path"))),
        ]);
        let observability =
            CliObservability::new(tempdir.path(), CliObservabilityOptions::default())
                .expect("concrete adapter");

        observability
            .emit(event(Some("550e8400-e29b-41d4-a716-446655440000")))
            .expect("emit backlog");

        let health = observability.health().expect("health");
        assert_eq!(
            health.active_log_path,
            Some(
                tempdir
                    .path()
                    .join(".atm")
                    .join("logs")
                    .join("atm.log.jsonl")
            )
        );
    }

    #[test]
    #[serial]
    fn concrete_adapter_emits_queries_follows_and_reports_health() {
        let tempdir = TempDir::new().expect("tempdir");
        let log_dir = tempdir.path().join(".atm").join("logs");
        let _env = EnvGuard::set_many([
            ("ATM_LOG", Some("info")),
            ("ATM_LOG_DIR", Some(log_dir.to_str().expect("utf8 path"))),
            ("HOME", Some(tempdir.path().to_str().expect("utf8 path"))),
        ]);
        let observability =
            CliObservability::new(tempdir.path(), CliObservabilityOptions::default())
                .expect("concrete adapter");

        observability
            .emit(event(Some("550e8400-e29b-41d4-a716-446655440000")))
            .expect("emit backlog");

        let initial = observability
            .query(query(LogOrder::OldestFirst))
            .expect("initial query");
        assert_eq!(initial.records.len(), 1);
        assert_eq!(initial.records[0].service.as_str(), "atm");
        assert_eq!(initial.records[0].action.as_deref(), Some("send"));
        assert_eq!(
            initial.records[0]
                .fields
                .get("command")
                .and_then(atm_core::observability::LogFieldValue::as_str),
            Some("send")
        );

        let health = observability.health().expect("health");
        assert_eq!(health.logging_state, AtmObservabilityHealthState::Healthy);
        assert_eq!(
            health.query_state,
            Some(AtmObservabilityHealthState::Healthy)
        );
        assert_eq!(health.active_log_path, Some(log_dir.join("atm.log.jsonl")));
        let detail = health
            .detail
            .as_deref()
            .expect("maintenance detail should be projected");
        assert!(detail.contains("maintenance state="));
        assert!(detail.contains("rotated_files_total="));
        assert!(detail.contains("pruned_files_total="));
        assert!(detail.contains("last_pass_at="));

        let mut follow = observability
            .follow(AtmLogQuery {
                mode: LogMode::Tail,
                ..query(LogOrder::OldestFirst)
            })
            .expect("follow");
        observability
            .emit(event(Some("550e8400-e29b-41d4-a716-446655440001")))
            .expect("emit followed");

        let followed_message_id = "550e8400-e29b-41d4-a716-446655440001"
            .parse::<atm_core::schema::AtmMessageId>()
            .expect("message id")
            .to_string();
        let followed = follow.poll().expect("follow poll");
        assert!(
            followed.records.iter().any(|record| {
                record
                    .fields
                    .get("message_id")
                    .and_then(atm_core::observability::LogFieldValue::as_str)
                    == Some(followed_message_id.as_str())
            }),
            "follow poll should include the newly emitted normalized message id even if the shared tail surface also returns the prior backlog entry"
        );
    }

    #[test]
    #[serial]
    fn concrete_adapter_fails_closed_when_atm_log_dir_is_invalid() {
        let tempdir = TempDir::new().expect("tempdir");
        let _env = EnvGuard::set_many([
            ("ATM_LOG", Some("info")),
            ("ATM_LOG_DIR", Some("relative/logs")),
            ("HOME", Some(tempdir.path().to_str().expect("utf8 path"))),
        ]);

        let error = CliObservability::new(tempdir.path(), CliObservabilityOptions::default())
            .expect_err("invalid ATM_LOG_DIR should fail closed");
        assert!(error.is_config());
        assert!(error.message.contains("absolute path"));
    }

    #[test]
    fn cli_observability_is_debuggable() {
        let observability =
            CliObservability::from_test_port(atm_core::observability::NullObservability);
        let debug = format!("{observability:?}");
        assert!(debug.contains("CliObservability"));
    }

    #[test]
    fn fatal_emit_failure_message_mentions_stage_and_error() {
        let message = fatal_emit_failure_message(
            "service",
            &AtmError::observability_emit("synthetic emit failure"),
        );
        assert!(message.contains("ATM fatal diagnostic emission failed during service"));
        assert!(message.contains("synthetic emit failure"));
    }

    #[test]
    fn command_emit_failure_message_mentions_command_action_and_error() {
        let message = command_emit_failure_message(
            "send",
            "send",
            &AtmError::observability_emit("synthetic emit failure"),
        );
        assert!(message.contains("send/send"));
        assert!(message.contains("synthetic emit failure"));
    }

    #[test]
    fn emit_fatal_error_executes_secondary_failure_path_without_panicking() {
        let observability = CliObservability::from_test_port(FailingEmitObservability);
        observability.report_fatal_error("service", &AtmError::validation("boom"));
    }
}