trusty-common 0.49.0

Shared utilities and provider-agnostic streaming chat (ChatProvider, OllamaProvider, OpenRouter, tool-use) for trusty-* projects
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
//! Reading the inbox back out and driving the pipeline (#5192, ADR-0034 ยง5).
//!
//! Why: #5182 made the receiver take durable ownership of a delivery before
//! acknowledging it, and acknowledging is what licenses `trusty-console` to
//! delete its only copy. That closed the loss window and opened a different
//! one: the delivery became the receiver's problem and nothing on the receiver
//! read it back. A webhook reached a file and stopped โ€” no review, no analysis,
//! no comment, and a spool that reported empty because every delivery had been
//! successfully accepted by a service that then did nothing with it.
//!
//! What: [`drain_once`] is one full pass over the inbox. It claims each entry
//! exclusively ([`super::Claim`]), hands the delivery to a
//! [`DeliveryProcessor`], and removes the entry ONLY after the processor said
//! it accepted the work. Failures are counted durably and bounded
//! ([`super::retry`]); an entry out of retries is quarantined, never deleted.
//!
//! ๐Ÿ”ด **The four rules this module exists to hold, and where each one lives.**
//!
//! 1. *Nothing is DESTROYED before the pipeline accepted it.*
//!    [`super::retry::remove_processed`] is the only call that destroys a
//!    delivery, and it sits behind one arm of [`drain_once`]'s match โ€” the arm
//!    reached on `Ok(_)` from the processor.
//!    [`super::retry::quarantine`] also unlinks, but only after `hard_link` has
//!    put the same inode under `<inbox>/quarantine/`, so it moves a delivery and
//!    never destroys one. Those are the two, and the distinction between them is
//!    the invariant โ€” not the count.
//! 2. *A failure is visible.* Every non-success lands in
//!    [`DrainReport::failures`] with the delivery id and the processor's own
//!    words, and [`DrainReport::log_summary`] raises it at `error!`. There is no
//!    swallowed `Err` and no arm that logs at `debug!` and moves on.
//! 3. *Counts are true under failure.* [`DrainReport::processed`] counts
//!    entries the processor accepted AND that are gone from the inbox. An
//!    acceptance whose removal failed is [`FailureOutcome::Stuck`], counted
//!    nowhere else โ€” the count under-reports success rather than inventing it.
//! 4. *A stuck inbox is not healthy.* Nothing here deletes work to make a
//!    number go down, so [`super::held_count`] and
//!    [`super::quarantined_count`] โ€” which is what `trusty-console` renders โ€”
//!    keep reporting the backlog for exactly as long as it exists.
//!
//! An interrupted pass is safe by construction: the claim is an `flock` the
//! kernel releases on process death, so a delivery a SIGKILLed drainer was
//! mid-way through is claimable by the next one.
//!
//! ๐Ÿ”ด **That crash-safety design is itself what makes a duplicate possible, and
//! the drain โ€” not each receiver โ€” closes it (#5192 critic round 1).** A
//! drainer can return `Ok` from the pipeline and die before the entry is
//! unlinked; the entry is then claimable *by design*, and a receiver with a
//! side effect (`trusty-analyze` posts a PR comment) would perform it a second
//! time, unbounded on repeated crashes. So [`drain_once`] records the delivery
//! in [`super::retry::mark_processed`] BEFORE removing it, and consults
//! [`super::retry::is_processed`] before ever calling a processor. Every
//! receiver gets the `delivery_id` deduplication the relay contract
//! ([`super::RelayParams::attempts`]) requires, from one implementation โ€” a
//! per-crate copy is how one target ends up with the guarantee and its sibling
//! silently does not.
//!
//! Test: `tests.rs` โ€” `drain_*`.

use std::path::{Path, PathBuf};

use super::RelayDelivery;
use super::claim::{Claim, ClaimOutcome};
use super::inbox::{Inbox, InboxError};
use super::retry::{self, DEFAULT_MAX_ATTEMPTS};

/// What a processor did with a delivery it accepted responsibility for.
///
/// Both variants remove the entry, and they are separate so a count of real
/// work is never inflated by deliveries the pipeline deliberately skipped.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Disposition {
    /// The pipeline ran for this delivery.
    Processed,
    /// The pipeline examined it and there is deliberately nothing to do โ€” the
    /// wrong event type, a filtered action, a PR already reviewed at this SHA.
    Ignored {
        /// Why, for the log. Not an error.
        reason: String,
    },
}

/// Why a processor could not accept a delivery.
///
/// Why: `retryable` is the only thing standing between a transient GitHub 5xx
/// and a poisoned payload, and getting it wrong in either direction is a
/// defect โ€” a permanent error retried forever pins the drain, and a transient
/// error quarantined on the first failure needs a human for something that
/// would have healed itself.
/// Test: `drain_retries_a_retryable_failure_until_the_bound_then_quarantines`,
/// `drain_quarantines_a_permanent_failure_immediately`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProcessFailure {
    /// What went wrong, in the processor's own words. Stored durably and
    /// reported.
    pub reason: String,
    /// Whether another attempt could plausibly succeed.
    pub retryable: bool,
}

impl ProcessFailure {
    /// A failure another attempt might survive.
    pub fn retryable(reason: impl Into<String>) -> Self {
        Self {
            reason: reason.into(),
            retryable: true,
        }
    }

    /// A failure no number of attempts will fix. Quarantined on the spot.
    pub fn permanent(reason: impl Into<String>) -> Self {
        Self {
            reason: reason.into(),
            retryable: false,
        }
    }
}

/// Whatever turns a held delivery into the work it represents.
///
/// Why: `trusty-review` runs its review pipeline and `trusty-analyze` fetches,
/// analyses and comments. Neither can live here, and the ordering rule that
/// makes the drain safe must not be reimplemented twice โ€” so the crates supply
/// the pipeline and this module supplies the ordering, exactly as `DeliverySink`
/// splits the receive half.
///
/// What: async, because both pipelines are network-bound. Returning `Ok` MUST
/// mean the work has been done or has been deliberately declined โ€” it is what
/// licenses the entry's removal, and there is no second chance after it.
///
/// Test: `drain_removes_an_entry_the_processor_accepted`,
/// `drain_keeps_an_entry_whose_processor_failed`.
#[async_trait::async_trait]
pub trait DeliveryProcessor: Send + Sync + 'static {
    /// Do the work for one delivery, or say why it could not be done.
    async fn process(&self, delivery: &RelayDelivery) -> Result<Disposition, ProcessFailure>;
}

/// How hard the drain tries before giving up on one entry.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DrainPolicy {
    /// Failures after which an entry is quarantined rather than retried.
    pub max_attempts: u32,
}

impl Default for DrainPolicy {
    fn default() -> Self {
        Self {
            max_attempts: DEFAULT_MAX_ATTEMPTS,
        }
    }
}

/// What became of one entry that did not succeed.
///
/// Test: `drain_report_accounts_for_every_scanned_entry`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FailureOutcome {
    /// Left in the inbox; the next pass will try again.
    Retrying,
    /// Moved to `<inbox>/quarantine/`. Needs a human.
    Quarantined,
    /// Neither retried cleanly nor quarantined โ€” the drain could not act on it
    /// at all. Includes the case where the pipeline accepted the delivery and
    /// the entry could not then be removed.
    Stuck,
}

/// One entry's failure, as reported.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DrainFailure {
    /// Delivery id, or a placeholder when the entry could not be decoded.
    pub delivery_id: String,
    /// Entry the failure is about.
    pub path: PathBuf,
    /// Failures recorded against it so far, this one included.
    pub attempts: u32,
    /// What went wrong.
    pub reason: String,
    /// Where that left the entry.
    pub outcome: FailureOutcome,
}

/// The result of one pass, in numbers that hold under failure.
///
/// ๐Ÿ”ด Every field is a disjoint count of entries seen in this pass, and
/// [`DrainReport::accounted`] must equal [`DrainReport::scanned`]. That
/// identity is asserted by a test precisely so a future arm that forgets to
/// count something cannot silently shrink the totals โ€” the shape of defect
/// where a drain reports `0 dropped` while dropping work.
///
/// Test: `drain_report_accounts_for_every_scanned_entry`.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct DrainReport {
    /// Entries present when the pass started.
    pub scanned: usize,
    /// Accepted by the processor and removed from the inbox.
    pub processed: usize,
    /// Deliberately declined by the processor and removed.
    pub ignored: usize,
    /// Failed, still within the retry bound, still held.
    pub retry_pending: usize,
    /// Moved to quarantine this pass.
    pub quarantined: usize,
    /// Held by another drainer.
    pub skipped_in_flight: usize,
    /// Gone before this pass could claim them โ€” drained by a concurrent pass.
    pub vanished: usize,
    /// Already in the processed ledger, so removed without running the pipeline
    /// again. Non-zero means a previous pass died between accepting a delivery
    /// and unlinking it โ€” see the module docs.
    pub deduplicated: usize,
    /// Every non-success, with the delivery id and the reason.
    pub failures: Vec<DrainFailure>,
    /// Why the inbox itself could not be listed, if it could not.
    pub scan_error: Option<String>,
}

impl DrainReport {
    /// Entries this pass reached a verdict on. Must equal [`Self::scanned`].
    pub fn accounted(&self) -> usize {
        self.processed
            + self.ignored
            + self.retry_pending
            + self.quarantined
            + self.skipped_in_flight
            + self.vanished
            + self.deduplicated
            + self
                .failures
                .iter()
                .filter(|f| f.outcome == FailureOutcome::Stuck)
                .count()
    }

    /// True only when nothing needs an operator's attention.
    ///
    /// `skipped_in_flight` is deliberately not a fault: it means another
    /// drainer has the entry, which is the concurrency control working.
    pub fn is_clean(&self) -> bool {
        self.scan_error.is_none() && self.failures.is_empty() && self.quarantined == 0
    }

    /// Emit the pass at a level that matches what it found.
    ///
    /// Why: rule 2 in the module docs. A drain that reports its failures at
    /// `debug!` has the same operational value as one that swallows them.
    /// Test: not asserted directly; the counts it prints are.
    pub fn log_summary(&self, source: &str) {
        if let Some(e) = &self.scan_error {
            tracing::error!(source, error = %e, "webhook inbox could not be listed; nothing was drained");
            return;
        }
        if self.is_clean() {
            if self.processed > 0 || self.ignored > 0 {
                tracing::info!(
                    source,
                    processed = self.processed,
                    ignored = self.ignored,
                    skipped_in_flight = self.skipped_in_flight,
                    "drained the webhook inbox"
                );
            }
            return;
        }
        for failure in &self.failures {
            tracing::error!(
                source,
                delivery_id = %failure.delivery_id,
                path = %failure.path.display(),
                attempts = failure.attempts,
                outcome = ?failure.outcome,
                reason = %failure.reason,
                "webhook delivery was not processed"
            );
        }
        tracing::error!(
            source,
            scanned = self.scanned,
            processed = self.processed,
            retry_pending = self.retry_pending,
            quarantined = self.quarantined,
            "webhook inbox drain finished with unprocessed deliveries"
        );
    }

    /// Record a quarantine, or the failure to quarantine.
    fn quarantine(&mut self, root: &Path, path: &Path, id: &str, attempts: u32, reason: &str) {
        match retry::quarantine(root, path) {
            Ok(target) => {
                self.quarantined += 1;
                self.failures.push(DrainFailure {
                    delivery_id: id.to_string(),
                    path: target,
                    attempts,
                    reason: reason.to_string(),
                    outcome: FailureOutcome::Quarantined,
                });
            }
            Err(e) => {
                self.failures.push(DrainFailure {
                    delivery_id: id.to_string(),
                    path: path.to_path_buf(),
                    attempts,
                    reason: format!("{reason}; and quarantining it failed: {e}"),
                    outcome: FailureOutcome::Stuck,
                });
            }
        }
    }

    /// Record an accepted delivery, or the failure to retire it afterwards.
    ///
    /// ๐Ÿ”ด Ledger first, unlink second. Reversed, a crash in between erases the
    /// only evidence the work happened and the next redelivery repeats it.
    fn accept(&mut self, root: &Path, path: &Path, id: &str, disposition: &Disposition) {
        if let Err(e) = retry::mark_processed(root, path, id, now_unix_ms()) {
            // Deliberately NOT followed by a removal. An unrecorded success is
            // a duplicate waiting to happen; keeping the entry turns it into a
            // visible, retried one, which the ledger will then suppress.
            self.failures.push(DrainFailure {
                delivery_id: id.to_string(),
                path: path.to_path_buf(),
                attempts: 0,
                reason: format!(
                    "the pipeline accepted this delivery but it could not be recorded as \
                     processed, so the entry is kept rather than removed unrecorded: {e}"
                ),
                outcome: FailureOutcome::Stuck,
            });
            return;
        }
        match retry::remove_processed(path) {
            Ok(()) => match disposition {
                Disposition::Processed => self.processed += 1,
                Disposition::Ignored { reason } => {
                    self.ignored += 1;
                    tracing::info!(delivery_id = %id, reason = %reason, "webhook delivery needed no work");
                }
            },
            // The work happened; the bookkeeping did not. Counting this as
            // `processed` would report an entry gone that is still on disk and
            // will be processed again, so it is reported as what it is.
            Err(e) => self.failures.push(DrainFailure {
                delivery_id: id.to_string(),
                path: path.to_path_buf(),
                attempts: 0,
                reason: format!(
                    "the pipeline accepted this delivery but its inbox entry could not be \
                     removed; the processed ledger stops it running again: {e}"
                ),
                outcome: FailureOutcome::Stuck,
            }),
        }
    }
}

/// Drain every claimable entry in `inbox` through `processor`, once.
///
/// Why: one bounded pass rather than an internal loop, so the caller owns the
/// schedule and a test owns the clock. See the module docs for the four rules
/// this upholds.
///
/// What: lists the inbox oldest-first by mtime (order is a courtesy, not a
/// correctness requirement โ€” every entry is independent), then for each entry
/// claims, checks the retry budget, processes, and disposes. An entry another
/// drainer holds is skipped immediately rather than waited on.
///
/// Blocking filesystem work runs inline. Each operation is a small local-file
/// syscall against a `0700` directory, and the caller is a dedicated drain task
/// in a short-lived process โ€” not a shared runtime worker serving connections.
///
/// Never returns `Err`: a pass that could not even list the inbox reports
/// [`DrainReport::scan_error`], because a drain that returns early with an
/// error and no report is a drain whose failure has no counts attached to it.
///
/// Test: `drain_removes_an_entry_the_processor_accepted`,
/// `drain_does_not_reprocess_a_delivery_marked_done`,
/// `drain_marks_a_processed_delivery_before_removing_it`,
/// `drain_keeps_an_entry_whose_processor_failed`,
/// `drain_retries_a_retryable_failure_until_the_bound_then_quarantines`,
/// `drain_quarantines_an_entry_whose_attempt_budget_was_already_spent`,
/// `drain_quarantines_a_permanent_failure_immediately`,
/// `drain_does_not_double_process_a_claimed_entry`,
/// `drain_report_accounts_for_every_scanned_entry`,
/// `drain_reports_a_scan_error_rather_than_an_empty_pass`.
pub async fn drain_once(
    inbox: &Inbox,
    processor: &dyn DeliveryProcessor,
    policy: DrainPolicy,
) -> DrainReport {
    let root = inbox.root().to_path_buf();
    let mut report = DrainReport::default();

    let entries = match candidate_entries(&root) {
        Ok(entries) => entries,
        Err(e) => {
            report.scan_error = Some(format!("{e}"));
            return report;
        }
    };
    report.scanned = entries.len();
    retry::prune_processed(&root, retry::PROCESSED_RETENTION);

    for path in entries {
        let claim = match Claim::try_acquire(&path) {
            Ok(ClaimOutcome::Claimed(claim)) => claim,
            Ok(ClaimOutcome::InFlight) => {
                report.skipped_in_flight += 1;
                continue;
            }
            Ok(ClaimOutcome::Vanished) => {
                report.vanished += 1;
                continue;
            }
            // No processor can ever accept these, so retrying is a loop with no
            // exit. Quarantine keeps the bytes for an operator to look at.
            Ok(ClaimOutcome::Undecodable { path, reason }) => {
                report.quarantine(
                    &root,
                    &path,
                    "<undecodable>",
                    0,
                    &format!("inbox entry is not a decodable delivery: {reason}"),
                );
                continue;
            }
            Err(e) => {
                report.failures.push(DrainFailure {
                    delivery_id: "<unclaimed>".to_string(),
                    path,
                    attempts: 0,
                    reason: format!("{e}"),
                    outcome: FailureOutcome::Stuck,
                });
                continue;
            }
        };

        let id = claim.delivery().delivery_id.clone();

        // ๐Ÿ”ด Before the pipeline, never after: a delivery already in the ledger
        // has had its work done, and the only reason its entry survives is that
        // the drainer which did it died before the unlink. Running the pipeline
        // again here is the double-post this check exists to stop.
        if retry::is_processed(&root, &path) {
            tracing::warn!(
                delivery_id = %id,
                "webhook delivery was already processed; removing its entry without re-running \
                 the pipeline (a previous drainer died between accepting it and removing it)"
            );
            match retry::remove_processed(&path) {
                Ok(()) => report.deduplicated += 1,
                Err(e) => report.failures.push(DrainFailure {
                    delivery_id: id,
                    path: path.clone(),
                    attempts: 0,
                    reason: format!("already processed, but its entry could not be removed: {e}"),
                    outcome: FailureOutcome::Stuck,
                }),
            }
            continue;
        }

        let already = retry::load_attempts(&path);
        if already.attempts >= policy.max_attempts {
            report.quarantine(
                &root,
                &path,
                &id,
                already.attempts,
                &format!(
                    "out of retries after {} failed attempts; last error: {}",
                    already.attempts, already.last_error
                ),
            );
            continue;
        }

        match processor.process(claim.delivery()).await {
            Ok(disposition) => report.accept(&root, &path, &id, &disposition),
            Err(failure) => {
                let record = match retry::record_failure(&path, &failure.reason, now_unix_ms()) {
                    Ok(record) => record,
                    // The bound cannot be enforced, and an unbounded retry is
                    // the failure this whole path exists to avoid.
                    Err(e) => {
                        report.quarantine(
                            &root,
                            &path,
                            &id,
                            0,
                            &format!(
                                "{}; and the attempt record could not be written, so the retry \
                                 bound cannot be enforced: {e}",
                                failure.reason
                            ),
                        );
                        continue;
                    }
                };

                if !failure.retryable || record.attempts >= policy.max_attempts {
                    let why = if failure.retryable {
                        format!(
                            "{} (attempt {} of {})",
                            failure.reason, record.attempts, policy.max_attempts
                        )
                    } else {
                        format!("{} (permanent; not retried)", failure.reason)
                    };
                    report.quarantine(&root, &path, &id, record.attempts, &why);
                } else {
                    report.retry_pending += 1;
                    report.failures.push(DrainFailure {
                        delivery_id: id,
                        path: path.clone(),
                        attempts: record.attempts,
                        reason: failure.reason,
                        outcome: FailureOutcome::Retrying,
                    });
                }
            }
        }
    }

    report
}

/// Every `*.json` directly in `root`, oldest mtime first.
///
/// Deliberately does NOT decode: a decode here would race a concurrent drainer
/// and would have to guess what an undecodable file means. Both questions
/// belong to [`Claim::try_acquire`], under the lock.
fn candidate_entries(root: &Path) -> Result<Vec<PathBuf>, InboxError> {
    let read = match std::fs::read_dir(root) {
        Ok(read) => read,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
        Err(source) => {
            return Err(InboxError::Read {
                path: root.to_path_buf(),
                source,
            });
        }
    };
    let mut entries: Vec<(std::time::SystemTime, PathBuf)> = read
        .flatten()
        .map(|e| e.path())
        .filter(|p| p.extension().is_some_and(|x| x == "json"))
        .map(|p| {
            let mtime = std::fs::metadata(&p)
                .and_then(|m| m.modified())
                .unwrap_or(std::time::UNIX_EPOCH);
            (mtime, p)
        })
        .collect();
    entries.sort();
    Ok(entries.into_iter().map(|(_, p)| p).collect())
}

/// Wall clock in milliseconds, or 0 if the clock is before the epoch.
fn now_unix_ms() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_millis() as u64)
        .unwrap_or(0)
}