agent-first-mail 0.3.0

Let your AI agent work your inbox — email pulled into plain files it reads, sorts, and drafts on your machine, with nothing sent until you confirm.
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
454
455
456
457
458
459
460
461
462
463
464
use crate::error::{AppError, Result};
use serde_json::{json, Map, Value};
use std::io::ErrorKind;
use std::path::{Path, PathBuf};
use std::time::Instant;

pub(crate) type ProgressCallback<'a> = dyn FnMut(&str, Value) + 'a;

pub(crate) fn emit(progress: &mut Option<&mut ProgressCallback<'_>>, phase: &str, fields: Value) {
    let Some(callback) = progress.as_deref_mut() else {
        return;
    };
    callback(phase, fields);
}

pub(crate) fn object_with_phase(phase: &str, fields: Value) -> Value {
    let mut map = match fields {
        Value::Object(map) => map,
        _ => Map::new(),
    };
    map.insert("phase".to_string(), Value::String(phase.to_string()));
    Value::Object(map)
}

pub(crate) struct WorkspaceProgressSink {
    path: PathBuf,
    command: &'static str,
    started: Instant,
    started_rfc3339: String,
    last_phase: String,
    last_fields: Value,
}

impl WorkspaceProgressSink {
    pub(crate) fn start(root: &Path, command: &'static str) -> Self {
        let mut sink = Self {
            path: workspace_progress_path(root),
            command,
            started: Instant::now(),
            started_rfc3339: crate::store::now_rfc3339(),
            last_phase: "start".to_string(),
            last_fields: json!({}),
        };
        sink.write("running", "start", json!({}), None, None);
        sink
    }

    pub(crate) fn update(&mut self, phase: &str, fields: Value) {
        self.last_phase = phase.to_string();
        self.last_fields = fields.clone();
        self.write("running", phase, fields, None, None);
    }

    pub(crate) fn finish_success(&mut self, result: &Value) {
        let summary = scalar_summary(result);
        self.write(
            "succeeded",
            "finish",
            json!({"success": true}),
            Some(summary),
            None,
        );
    }

    pub(crate) fn finish_failure(&mut self, err: &AppError) {
        self.write(
            "failed",
            "finish",
            json!({
                "success": false,
                "failed_phase": self.last_phase.as_str(),
                "failed_fields": self.last_fields.clone(),
            }),
            None,
            Some(json!({
                "error_code": err.error_code,
                "error": err.message.as_str(),
                "retryable": err.retryable,
            })),
        );
    }

    fn write(
        &mut self,
        status: &str,
        phase: &str,
        fields: Value,
        result: Option<Value>,
        error: Option<Value>,
    ) {
        let value = json!({
            "schema_name": "workspace_progress",
            "schema_version": 1,
            "command": self.command,
            "status": status,
            "phase": phase,
            "message": progress_message(self.command, phase),
            "started_rfc3339": self.started_rfc3339.as_str(),
            "updated_rfc3339": crate::store::now_rfc3339(),
            "elapsed_ms": self.started.elapsed().as_millis() as u64,
            "fields": fields,
            "result": result,
            "error": error,
        });
        let _ = crate::util::write_json_pretty(&self.path, &value);
    }
}

pub(crate) fn workspace_status_progress(root: &Path) -> Result<Value> {
    let path = workspace_progress_path(root);
    let data = match std::fs::read_to_string(&path) {
        Ok(data) => data,
        Err(err) if err.kind() == ErrorKind::NotFound => {
            return Ok(json!({"status": "idle"}));
        }
        Err(err) => return Err(AppError::io("read workspace progress", &err)),
    };
    let value: Value =
        serde_json::from_str(&data).map_err(|e| AppError::json("parse workspace progress", &e))?;
    let Value::Object(snapshot) = &value else {
        return Err(AppError::new(
            "progress_invalid",
            "workspace progress snapshot must be a JSON object",
        ));
    };
    Ok(status_progress_from_snapshot(snapshot))
}

fn status_progress_from_snapshot(snapshot: &Map<String, Value>) -> Value {
    let status = string_field(snapshot, "status").unwrap_or("unknown");
    let command = string_field(snapshot, "command");
    let phase = string_field(snapshot, "phase");
    let fields = snapshot.get("fields").cloned().unwrap_or_else(|| json!({}));
    let error = snapshot.get("error").cloned().unwrap_or(Value::Null);
    let result = snapshot.get("result").cloned().unwrap_or(Value::Null);

    let mut out = Map::new();
    out.insert("status".to_string(), json!(status));
    insert_string(&mut out, "command", command);
    insert_string(&mut out, "phase", phase);
    out.insert(
        "summary".to_string(),
        json!(progress_summary(
            status, command, phase, &fields, &result, &error
        )),
    );
    let computed_fields = if status == "failed" {
        failed_fields(&fields)
    } else {
        &fields
    };
    insert_progress_counts(&mut out, command, computed_fields);
    if status == "failed" {
        insert_failed_progress_fields(&mut out, &fields);
    }
    out.insert("fields".to_string(), fields);
    out.insert("error".to_string(), error);
    Value::Object(out)
}

fn insert_progress_counts(out: &mut Map<String, Value>, command: Option<&str>, fields: &Value) {
    let Value::Object(fields) = fields else {
        return;
    };
    if let Some(value) = fields.get("processed_count").cloned() {
        out.insert("processed_count".to_string(), value);
    }
    if let Some(value) = fields.get("uid_count").cloned() {
        out.insert("total_count".to_string(), value);
    }
    if let (Some(processed), Some(total)) = (
        value_u64(fields.get("processed_count")),
        value_u64(fields.get("uid_count")),
    ) {
        if total > 0 {
            let percent = ((processed as f64 / total as f64) * 10000.0).round() / 100.0;
            out.insert("progress_percent".to_string(), json!(percent));
        }
    }
    for key in ["batch_index", "batch_count"] {
        if let Some(value) = fields.get(key).cloned() {
            out.insert(key.to_string(), value);
        }
    }
    match command {
        Some("pull") => insert_pull_progress_fields(out, fields),
        Some("push") => insert_push_progress_fields(out, fields),
        _ => {}
    }
}

fn insert_pull_progress_fields(out: &mut Map<String, Value>, fields: &Map<String, Value>) {
    for key in ["mailbox_id", "mailbox_name", "mailbox_count"] {
        if let Some(value) = fields.get(key).cloned() {
            out.insert(key.to_string(), value);
        }
    }
    if let Some(value) = fields.get("index").cloned() {
        out.insert("mailbox_index".to_string(), value);
    }
}

fn insert_push_progress_fields(out: &mut Map<String, Value>, fields: &Map<String, Value>) {
    for key in [
        "push_id",
        "mode",
        "kind",
        "display_kind",
        "item_index",
        "item_count",
        "step_index",
        "step_count",
        "label",
    ] {
        if let Some(value) = fields.get(key).cloned() {
            out.insert(key.to_string(), value);
        }
    }
    if !out.contains_key("item_index") {
        if let Some(value) = fields.get("index").cloned() {
            out.insert("item_index".to_string(), value);
        }
    }
}

fn insert_failed_progress_fields(out: &mut Map<String, Value>, fields: &Value) {
    let Value::Object(fields) = fields else {
        return;
    };
    if let Some(value) = fields.get("failed_phase").cloned() {
        out.insert("failed_phase".to_string(), value);
    }
    if let Some(value) = fields.get("failed_fields").cloned() {
        out.insert("failed_fields".to_string(), value);
    }
}

fn failed_fields(fields: &Value) -> &Value {
    fields
        .as_object()
        .and_then(|map| map.get("failed_fields"))
        .unwrap_or(fields)
}

fn progress_summary(
    status: &str,
    command: Option<&str>,
    phase: Option<&str>,
    fields: &Value,
    result: &Value,
    error: &Value,
) -> String {
    let command = command.unwrap_or("afmail");
    if status == "failed" {
        let (failed_phase, failed_fields) = failed_phase_and_fields(phase, fields);
        let error_code = error
            .as_object()
            .and_then(|map| string_field(map, "error_code"))
            .unwrap_or("error");
        let during = phase_summary(command, failed_phase, failed_fields);
        let during = during
            .strip_prefix(&format!("{command}: "))
            .unwrap_or(during.as_str());
        return format!("{command} failed: {error_code} during {during}");
    }
    if status == "succeeded" {
        return success_summary(command, result);
    }
    phase_summary(command, phase, fields)
}

fn failed_phase_and_fields<'a>(
    phase: Option<&'a str>,
    fields: &'a Value,
) -> (Option<&'a str>, &'a Value) {
    let Some(map) = fields.as_object() else {
        return (phase, fields);
    };
    let failed_phase = string_field(map, "failed_phase").or(phase);
    let failed_fields = map.get("failed_fields").unwrap_or(fields);
    (failed_phase, failed_fields)
}

fn success_summary(command: &str, result: &Value) -> String {
    let Some(result) = result.as_object() else {
        return format!("{command} succeeded");
    };
    match command {
        "pull" => {
            let new_count = value_u64(result.get("new_message_count")).unwrap_or(0);
            let mailbox_count = value_u64(result.get("mailbox_count")).unwrap_or(0);
            format!("pull succeeded: {new_count} new messages across {mailbox_count} mailboxes")
        }
        "push" => {
            let pushed = value_u64(result.get("pushed_count")).unwrap_or(0);
            let failed = value_u64(result.get("failed_count")).unwrap_or(0);
            format!("push succeeded: {pushed} pushed, {failed} failed")
        }
        _ => format!("{command} succeeded"),
    }
}

fn phase_summary(command: &str, phase: Option<&str>, fields: &Value) -> String {
    match command {
        "pull" => pull_phase_summary(phase, fields),
        "push" => push_phase_summary(phase, fields),
        _ => phase
            .map(|phase| format!("{command}: {phase}"))
            .unwrap_or_else(|| command.to_string()),
    }
}

fn pull_phase_summary(phase: Option<&str>, fields: &Value) -> String {
    let Some(fields) = fields.as_object() else {
        return phase
            .map(|phase| format!("pull: {phase}"))
            .unwrap_or_else(|| "pull".to_string());
    };
    let mailbox = mailbox_label(fields);
    match phase {
        Some("pull_mailbox_bodies_start")
        | Some("pull_mailbox_bodies_progress")
        | Some("pull_mailbox_bodies_done") => pull_bodies_summary(&mailbox, fields),
        Some("pull_mailbox_headers_start") => format!("pull: {mailbox} headers"),
        Some("pull_mailbox_headers_done") => {
            let new_candidates = value_u64(fields.get("new_candidate_count")).unwrap_or(0);
            format!("pull: {mailbox} headers done, {new_candidates} new candidates")
        }
        Some("pull_resolve_targets") => {
            let mailbox_count = value_u64(fields.get("mailbox_count")).unwrap_or(0);
            format!("pull: resolved {mailbox_count} mailboxes")
        }
        Some("pull_reconcile_start") => "pull: reconciling local remote state".to_string(),
        Some("pull_reconcile_done") => "pull: reconciled local remote state".to_string(),
        Some("pull_render_start") => "pull: refreshing generated views".to_string(),
        Some("pull_render_done") => "pull: refreshed generated views".to_string(),
        Some(phase) => format!("pull: {phase}"),
        None => "pull".to_string(),
    }
}

fn pull_bodies_summary(mailbox: &str, fields: &Map<String, Value>) -> String {
    let processed = value_u64(fields.get("processed_count")).unwrap_or(0);
    let total = value_u64(fields.get("uid_count")).unwrap_or(0);
    let mut summary = format!("pull: {mailbox} bodies {processed}/{total}");
    if let (Some(batch_index), Some(batch_count)) = (
        value_u64(fields.get("batch_index")),
        value_u64(fields.get("batch_count")),
    ) {
        summary.push_str(&format!(", batch {batch_index}/{batch_count}"));
    }
    summary
}

fn push_phase_summary(phase: Option<&str>, fields: &Value) -> String {
    let Some(fields) = fields.as_object() else {
        return phase
            .map(|phase| format!("push: {phase}"))
            .unwrap_or_else(|| "push".to_string());
    };
    let display_kind = string_field(fields, "display_kind")
        .or_else(|| string_field(fields, "mode"))
        .or_else(|| string_field(fields, "kind"))
        .unwrap_or("item");
    match phase {
        Some("push_start") => {
            let item_count = value_u64(fields.get("item_count")).unwrap_or(0);
            format!("push: {display_kind} started, {item_count} items")
        }
        Some("push_item_start") | Some("push_item_done") | Some("push_item_failed") => {
            let index = value_u64(fields.get("item_index"))
                .or_else(|| value_u64(fields.get("index")))
                .unwrap_or(0);
            let item_count = value_u64(fields.get("item_count")).unwrap_or(0);
            format!("push: {display_kind} item {index}/{item_count}")
        }
        Some("push_step_start") | Some("push_step_done") | Some("push_step_failed") => {
            let step_index = value_u64(fields.get("step_index")).unwrap_or(0);
            let step_count = value_u64(fields.get("step_count")).unwrap_or(0);
            let item = match (
                value_u64(fields.get("item_index")).or_else(|| value_u64(fields.get("index"))),
                value_u64(fields.get("item_count")),
            ) {
                (Some(item_index), Some(item_count)) => format!(" item {item_index}/{item_count},"),
                _ => String::new(),
            };
            format!("push: {display_kind}{item} step {step_index}/{step_count}")
        }
        Some("push_done") => {
            let pushed = value_u64(fields.get("pushed_count")).unwrap_or(0);
            let failed = value_u64(fields.get("failed_count")).unwrap_or(0);
            format!("push: done, {pushed} pushed, {failed} failed")
        }
        Some(phase) => format!("push: {phase}"),
        None => "push".to_string(),
    }
}

fn mailbox_label(fields: &Map<String, Value>) -> String {
    string_field(fields, "mailbox_name")
        .or_else(|| string_field(fields, "mailbox_id"))
        .unwrap_or("mailbox")
        .to_string()
}

fn insert_string(map: &mut Map<String, Value>, key: &str, value: Option<&str>) {
    if let Some(value) = value {
        map.insert(key.to_string(), json!(value));
    }
}

fn string_field<'a>(map: &'a Map<String, Value>, key: &str) -> Option<&'a str> {
    map.get(key).and_then(Value::as_str)
}

fn value_u64(value: Option<&Value>) -> Option<u64> {
    value.and_then(Value::as_u64)
}

fn workspace_progress_path(root: &Path) -> PathBuf {
    root.join(".afmail/workspace.progress.json")
}

fn scalar_summary(value: &Value) -> Value {
    let mut out = Map::new();
    let Value::Object(map) = value else {
        return Value::Object(out);
    };
    for (key, value) in map {
        if matches!(
            value,
            Value::Bool(_) | Value::Number(_) | Value::String(_) | Value::Null
        ) {
            out.insert(key.clone(), value.clone());
        }
    }
    Value::Object(out)
}

fn progress_message(command: &str, phase: &str) -> &'static str {
    match (command, phase) {
        (_, "start") => "afmail command started",
        (_, "finish") => "afmail command finished",
        ("pull", "pull_resolve_targets") => "resolved pull targets",
        ("pull", "pull_mailbox_headers_start") => "fetching mailbox headers",
        ("pull", "pull_mailbox_headers_done") => "fetched mailbox headers",
        ("pull", "pull_mailbox_bodies_start") => "fetching new message bodies",
        ("pull", "pull_mailbox_bodies_progress") => "fetching new message bodies",
        ("pull", "pull_mailbox_bodies_done") => "fetched new message bodies",
        ("pull", "pull_reconcile_start") => "reconciling local remote state",
        ("pull", "pull_reconcile_done") => "reconciled local remote state",
        ("pull", "pull_render_start") => "refreshing generated views",
        ("pull", "pull_render_done") => "refreshed generated views",
        ("push", "push_start") => "pushing queued remote effects",
        ("push", "push_item_start") => "pushing queued item",
        ("push", "push_item_done") => "pushed queued item",
        ("push", "push_item_failed") => "queued item push failed",
        ("push", "push_step_start") => "running push step",
        ("push", "push_step_done") => "finished push step",
        ("push", "push_step_failed") => "push step failed",
        ("push", "push_done") => "finished pushing queued remote effects",
        _ => "afmail command progress",
    }
}