reflex-search 2.1.0

A local-first, structure-aware code search engine for AI agents
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
//! The writing-pass executor: plan, budget, call, cache, degrade.
//!
//! Flow of [`run_tasks`]:
//! 1. **Plan.** Suppress tasks with too little input. Compute each cache key. A hit is
//!    used unless the task is in the `--force-renarrate` scope. Tasks with the same key
//!    share one call.
//! 2. **Budget.** With `max_llm_tokens`, keep calls in `(priority, id)` order until the
//!    estimate reaches the cap; the rest are deferred. The cache fills, so later runs
//!    complete the rest.
//! 3. **Dry run** stops here.
//! 4. **Probe.** The first call runs alone. A fatal error (auth, unknown model, bad
//!    request) degrades the whole run to structural output.
//! 5. **Fan-out** under a semaphore, with retries and backoff for transient errors,
//!    halved concurrency after a 429, and a circuit breaker after consecutive failures.
//! 6. **Verdict.** If fewer than `degrade_threshold` of the eligible tasks succeeded, the
//!    whole run degrades. Successful answers stay cached either way.
//! 7. **Prune** the cache after a complete, successful run.

use super::cache::{CacheEntry, KeyMaterial, WriteCache};
use super::provider::LlmHandle;
use super::{ForceScope, LlmMode};
use crate::semantic::providers::{
    CompletionRequest, LlmProvider, OutputMode, ProviderError, ProviderErrorKind, StopReason, Usage,
};
use std::collections::BTreeMap;
use std::fmt::Write as _;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

/// The output contract of one task.
#[derive(Debug, Clone, PartialEq)]
pub enum OutputSpec {
    Text,
    JsonObject,
    JsonSchema {
        name: String,
        schema: serde_json::Value,
    },
}

impl OutputSpec {
    pub fn as_mode(&self) -> OutputMode<'_> {
        match self {
            OutputSpec::Text => OutputMode::Text,
            OutputSpec::JsonObject => OutputMode::JsonObject,
            OutputSpec::JsonSchema { name, schema } => OutputMode::JsonSchema { name, schema },
        }
    }
}

/// One unit of LLM writing.
#[derive(Debug, Clone)]
pub struct WriteTask {
    /// Unique and stable, e.g. `module:src/pulse`. Results are keyed by it.
    pub id: String,
    /// Task kind, used for `--force-renarrate` scopes and the plan table.
    pub kind: &'static str,
    /// Bump when parsing or prompt semantics change without a text change.
    pub prompt_version: u32,
    pub system: String,
    pub user: String,
    pub output: OutputSpec,
    pub max_tokens: u32,
    /// Lower runs first when a budget truncates the plan.
    pub priority: u16,
    /// Suppress the task when `user` has fewer words than this.
    pub min_words: usize,
}

impl WriteTask {
    /// A free-text task with default limits.
    pub fn text(
        id: impl Into<String>,
        kind: &'static str,
        system: impl Into<String>,
        user: impl Into<String>,
    ) -> Self {
        Self {
            id: id.into(),
            kind,
            prompt_version: 1,
            system: system.into(),
            user: user.into(),
            output: OutputSpec::Text,
            max_tokens: 1200,
            priority: 100,
            min_words: 15,
        }
    }

    pub fn with_output(mut self, output: OutputSpec) -> Self {
        self.output = output;
        self
    }

    pub fn with_max_tokens(mut self, max_tokens: u32) -> Self {
        self.max_tokens = max_tokens;
        self
    }

    pub fn with_priority(mut self, priority: u16) -> Self {
        self.priority = priority;
        self
    }

    pub fn with_prompt_version(mut self, v: u32) -> Self {
        self.prompt_version = v;
        self
    }

    fn est_input_tokens(&self) -> u64 {
        // ~3.5 characters per token; the budget headroom absorbs the error.
        ((self.system.len() + self.user.len()) as f64 / 3.5).ceil() as u64
    }

    fn est_output_tokens(&self) -> u64 {
        (self.max_tokens as f64 * 0.6).ceil() as u64
    }
}

/// Executor settings.
#[derive(Debug, Clone)]
pub struct WriteConfig {
    pub mode: LlmMode,
    /// Concurrent calls. `0` means one per job.
    pub concurrency: usize,
    pub max_llm_tokens: Option<u64>,
    pub force: ForceScope,
    pub dry_run: bool,
    /// Minimum share of eligible tasks that must succeed, else the run degrades.
    pub degrade_threshold: f32,
    pub keep_runs: usize,
    pub prune: bool,
    pub cache_model_agnostic: bool,
    /// Attempts per call, including the first.
    pub max_attempts: u32,
    pub base_backoff: Duration,
    /// Consecutive failed calls that stop the run.
    pub breaker_threshold: usize,
}

impl Default for WriteConfig {
    fn default() -> Self {
        Self {
            mode: LlmMode::On,
            concurrency: 4,
            max_llm_tokens: None,
            force: ForceScope::None,
            dry_run: false,
            degrade_threshold: 0.9,
            keep_runs: 3,
            prune: true,
            cache_model_agnostic: false,
            max_attempts: 3,
            base_backoff: Duration::from_secs(1),
            breaker_threshold: 5,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum TaskOutcome {
    /// Answer read from the cache.
    Cached,
    /// Answer from a provider call this run.
    Called,
    /// Not sent: the input is too thin to write about.
    Suppressed(String),
    /// Not sent: outside the token budget or the run stopped first.
    Deferred,
    /// Cache-only mode and no entry.
    NotCached,
    Failed(String),
    /// The LLM is off.
    Skipped,
}

#[derive(Debug, Clone)]
pub struct TaskResult {
    pub kind: &'static str,
    pub outcome: TaskOutcome,
    pub text: Option<String>,
    pub usage: Option<Usage>,
    est_input: u64,
    est_output: u64,
}

#[derive(Debug, Clone, Default)]
pub struct RunStats {
    pub cached: usize,
    pub called: usize,
    pub suppressed: usize,
    pub deferred: usize,
    pub not_cached: usize,
    pub failed: usize,
    pub skipped: usize,
    pub calls_made: usize,
    pub retries: usize,
    pub input_tokens: u64,
    pub output_tokens: u64,
    pub cached_input_tokens: u64,
    pub pruned: usize,
    pub elapsed: Duration,
}

/// Everything a run produced.
#[derive(Debug, Clone)]
pub struct WriteOutcome {
    pub mode: LlmMode,
    pub results: BTreeMap<String, TaskResult>,
    /// Set when the whole run fell back to structural output.
    pub degraded: Option<String>,
    /// Some calls were deferred by the token budget.
    pub truncated: bool,
    pub dry_run: bool,
    pub model: Option<String>,
    pub stats: RunStats,
}

impl WriteOutcome {
    /// The text for a task, or `None` when the section must render structurally.
    pub fn text(&self, id: &str) -> Option<&str> {
        if self.degraded.is_some() || self.dry_run {
            return None;
        }
        self.results.get(id)?.text.as_deref()
    }

    /// True when any task produced text.
    pub fn has_text(&self) -> bool {
        self.degraded.is_none() && !self.dry_run && self.results.values().any(|r| r.text.is_some())
    }

    /// Per-kind plan: tasks, cached, to-call, suppressed, deferred, estimated tokens.
    pub fn plan_table(&self) -> String {
        #[derive(Default)]
        struct Row {
            tasks: usize,
            cached: usize,
            to_call: usize,
            suppressed: usize,
            deferred: usize,
            est_in: u64,
            est_out: u64,
        }
        let mut rows: BTreeMap<&str, Row> = BTreeMap::new();
        let mut total = Row::default();
        for r in self.results.values() {
            for row in [rows.entry(r.kind).or_default(), &mut total] {
                row.tasks += 1;
                match r.outcome {
                    TaskOutcome::Cached => row.cached += 1,
                    TaskOutcome::Suppressed(_) => row.suppressed += 1,
                    TaskOutcome::Deferred => row.deferred += 1,
                    TaskOutcome::Called | TaskOutcome::Failed(_) => {
                        row.to_call += 1;
                        row.est_in += r.est_input;
                        row.est_out += r.est_output;
                    }
                    TaskOutcome::NotCached | TaskOutcome::Skipped => {}
                }
            }
        }
        let mut out = String::new();
        let _ = writeln!(
            out,
            "{:<14}{:>7}{:>8}{:>9}{:>12}{:>10}{:>10}{:>10}",
            "kind", "tasks", "cached", "to-call", "suppressed", "deferred", "est.in", "est.out"
        );
        let mut line = |name: &str, r: &Row| {
            let _ = writeln!(
                out,
                "{:<14}{:>7}{:>8}{:>9}{:>12}{:>10}{:>10}{:>10}",
                name,
                r.tasks,
                r.cached,
                r.to_call,
                r.suppressed,
                r.deferred,
                human_tokens(r.est_in),
                human_tokens(r.est_out)
            );
        };
        for (kind, r) in &rows {
            line(kind, r);
        }
        line("total", &total);
        if let Some(model) = &self.model {
            let _ = writeln!(out, "model: {model}");
        }
        out
    }

    /// One-line run summary for stderr.
    pub fn summary(&self) -> String {
        let s = &self.stats;
        let mut line = format!(
            "LLM: {} cached, {} called, {} suppressed",
            s.cached, s.called, s.suppressed
        );
        for (n, label) in [
            (s.deferred, "deferred"),
            (s.not_cached, "not cached"),
            (s.failed, "failed"),
        ] {
            if n > 0 {
                let _ = write!(line, ", {n} {label}");
            }
        }
        if s.calls_made > 0 {
            let _ = write!(
                line,
                "; tokens in {} (cached {}) / out {}",
                human_tokens(s.input_tokens),
                human_tokens(s.cached_input_tokens),
                human_tokens(s.output_tokens)
            );
        }
        if s.retries > 0 {
            let _ = write!(line, "; {} retries", s.retries);
        }
        if s.pruned > 0 {
            let _ = write!(line, "; pruned {} cache entries", s.pruned);
        }
        let _ = write!(line, " ({:.1}s)", s.elapsed.as_secs_f64());
        if let Some(reason) = &self.degraded {
            let _ = write!(line, "\nLLM output disabled for this run: {reason}");
        }
        line
    }
}

fn human_tokens(n: u64) -> String {
    if n >= 10_000 {
        format!("{:.1}k", n as f64 / 1000.0)
    } else {
        n.to_string()
    }
}

/// Tasks that share one cache key and therefore one call.
struct Job {
    key: String,
    ids: Vec<String>,
    task: WriteTask,
}

struct Shared {
    sem: tokio::sync::Semaphore,
    permits: AtomicUsize,
    consecutive_failures: AtomicUsize,
    stop: Mutex<Option<String>>,
    tokens_used: AtomicU64,
    retries: AtomicUsize,
    calls: AtomicUsize,
}

impl Shared {
    fn stopped(&self) -> bool {
        self.stop.lock().unwrap().is_some()
    }

    fn stop_with(&self, reason: String) {
        let mut stop = self.stop.lock().unwrap();
        if stop.is_none() {
            *stop = Some(reason);
        }
    }

    /// After a 429, halve the number of concurrent calls (floor 1).
    fn halve_concurrency(&self) {
        let current = self.permits.load(Ordering::SeqCst);
        if current > 1 {
            let forgotten = self.sem.forget_permits(current / 2);
            self.permits.fetch_sub(forgotten, Ordering::SeqCst);
            if forgotten > 0 {
                log::info!(
                    "pulse write: rate limited; concurrency {current} โ†’ {}",
                    current - forgotten
                );
            }
        }
    }
}

enum CallError {
    Fatal(String),
    Failed(String),
}

type CallResult = Result<(String, Option<Usage>), CallError>;

/// Run the writing pass.
pub fn run_tasks(
    mut tasks: Vec<WriteTask>,
    llm: &LlmHandle,
    cache: &WriteCache,
    cfg: &WriteConfig,
) -> WriteOutcome {
    let start = Instant::now();
    tasks.sort_by(|a, b| a.id.cmp(&b.id));
    tasks.dedup_by(|a, b| a.id == b.id);

    let mut outcome = WriteOutcome {
        mode: cfg.mode,
        results: BTreeMap::new(),
        degraded: None,
        truncated: false,
        dry_run: cfg.dry_run,
        model: llm
            .ident
            .as_ref()
            .map(|i| format!("{}/{}", i.provider, i.model)),
        stats: RunStats::default(),
    };
    let result = |task: &WriteTask, outcome: TaskOutcome| TaskResult {
        kind: task.kind,
        outcome,
        text: None,
        usage: None,
        est_input: task.est_input_tokens(),
        est_output: task.est_output_tokens(),
    };

    if cfg.mode == LlmMode::Off {
        for t in &tasks {
            outcome
                .results
                .insert(t.id.clone(), result(t, TaskOutcome::Skipped));
        }
        return finish(outcome, start);
    }

    let Some(ident) = llm.ident.clone() else {
        let reason = llm
            .unavailable
            .clone()
            .unwrap_or_else(|| "no LLM provider configured".into());
        for t in &tasks {
            outcome
                .results
                .insert(t.id.clone(), result(t, TaskOutcome::NotCached));
        }
        if cfg.mode == LlmMode::On {
            outcome.degraded = Some(reason);
        }
        return finish(outcome, start);
    };

    // โ”€โ”€ 1. Plan โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    let mut jobs: BTreeMap<String, Job> = BTreeMap::new();
    let mut used_keys: Vec<String> = Vec::new();
    for task in &tasks {
        let words = task.user.split_whitespace().count();
        if words < task.min_words {
            outcome.results.insert(
                task.id.clone(),
                result(
                    task,
                    TaskOutcome::Suppressed(format!("{words} words of input")),
                ),
            );
            continue;
        }
        let schema_json = match &task.output {
            OutputSpec::JsonSchema { schema, .. } => Some(schema.to_string()),
            _ => None,
        };
        let agnostic = cfg.cache_model_agnostic;
        let key = KeyMaterial {
            task_kind: task.kind,
            prompt_version: task.prompt_version,
            provider: (!agnostic).then_some(ident.provider.as_str()),
            model: (!agnostic).then_some(ident.model.as_str()),
            max_tokens: task.max_tokens,
            output_mode: task.output.as_mode().label(),
            output_schema: schema_json.as_deref(),
            system: &task.system,
            user: &task.user,
        }
        .key();

        if !cfg.force.matches(task)
            && let Some(hit) = cache.get(&key)
        {
            let mut r = result(task, TaskOutcome::Cached);
            r.text = Some(hit.text);
            outcome.results.insert(task.id.clone(), r);
            used_keys.push(key);
            continue;
        }
        if cfg.mode == LlmMode::CacheOnly {
            outcome
                .results
                .insert(task.id.clone(), result(task, TaskOutcome::NotCached));
            continue;
        }
        outcome
            .results
            .insert(task.id.clone(), result(task, TaskOutcome::Called));
        jobs.entry(key.clone())
            .or_insert_with(|| Job {
                key,
                ids: Vec::new(),
                task: task.clone(),
            })
            .ids
            .push(task.id.clone());
    }

    // โ”€โ”€ 2. Budget โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    let mut jobs: Vec<Job> = jobs.into_values().collect();
    jobs.sort_by(|a, b| (a.task.priority, &a.task.id).cmp(&(b.task.priority, &b.task.id)));
    if let Some(cap) = cfg.max_llm_tokens {
        let mut spent = 0u64;
        let mut kept = Vec::with_capacity(jobs.len());
        for job in jobs {
            let cost = job.task.est_input_tokens() + job.task.est_output_tokens();
            if spent + cost <= cap {
                spent += cost;
                kept.push(job);
            } else {
                outcome.truncated = true;
                for id in &job.ids {
                    if let Some(r) = outcome.results.get_mut(id) {
                        r.outcome = TaskOutcome::Deferred;
                    }
                }
            }
        }
        jobs = kept;
    }

    // โ”€โ”€ 3. Dry run โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    if cfg.dry_run || jobs.is_empty() {
        return verdict(outcome, used_keys, cache, cfg, start);
    }

    let Some(provider) = llm.provider.clone() else {
        outcome.degraded = Some(
            llm.unavailable
                .clone()
                .unwrap_or_else(|| "LLM provider unavailable".into()),
        );
        for job in &jobs {
            for id in &job.ids {
                if let Some(r) = outcome.results.get_mut(id) {
                    r.outcome = TaskOutcome::Failed("provider unavailable".into());
                }
            }
        }
        return finish(outcome, start);
    };

    // โ”€โ”€ 4โ€“5. Probe, then fan out โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    let permits = if cfg.concurrency == 0 {
        jobs.len()
    } else {
        cfg.concurrency
    }
    .max(1);
    let shared = Arc::new(Shared {
        sem: tokio::sync::Semaphore::new(permits),
        permits: AtomicUsize::new(permits),
        consecutive_failures: AtomicUsize::new(0),
        stop: Mutex::new(None),
        tokens_used: AtomicU64::new(0),
        retries: AtomicUsize::new(0),
        calls: AtomicUsize::new(0),
    });
    eprintln!(
        "  Writing: {} LLM call(s), {} concurrent ({})",
        jobs.len(),
        permits,
        outcome.model.as_deref().unwrap_or("?")
    );

    let runtime = match tokio::runtime::Builder::new_multi_thread()
        .worker_threads(2)
        .enable_all()
        .build()
    {
        Ok(rt) => rt,
        Err(e) => {
            outcome.degraded = Some(format!("could not start async runtime: {e}"));
            return finish(outcome, start);
        }
    };

    let cfg_arc = Arc::new(cfg.clone());
    let cache_arc = Arc::new(cache.clone());
    let finished: Vec<(Job, CallResult)> = runtime.block_on(async {
        let mut done = Vec::with_capacity(jobs.len());
        let mut rest = jobs.into_iter();
        let probe = rest.next().expect("jobs is non-empty");
        let probe_result =
            execute(Arc::clone(&provider), &probe, &cfg_arc, &shared, &cache_arc).await;
        done.push((probe, probe_result));

        let mut set = tokio::task::JoinSet::new();
        for job in rest {
            let provider = Arc::clone(&provider);
            let cfg = Arc::clone(&cfg_arc);
            let shared = Arc::clone(&shared);
            let cache = Arc::clone(&cache_arc);
            set.spawn(async move {
                let permit = shared.sem.acquire().await;
                if permit.is_err() || shared.stopped() {
                    return (job, None);
                }
                if let Some(cap) = cfg.max_llm_tokens
                    && shared.tokens_used.load(Ordering::SeqCst) >= cap
                {
                    return (job, None);
                }
                let r = execute(provider, &job, &cfg, &shared, &cache).await;
                (job, Some(r))
            });
        }
        while let Some(joined) = set.join_next().await {
            match joined {
                Ok((job, Some(r))) => done.push((job, r)),
                Ok((job, None)) => {
                    for id in &job.ids {
                        if let Some(r) = outcome.results.get_mut(id) {
                            r.outcome = TaskOutcome::Deferred;
                        }
                    }
                }
                Err(e) => log::warn!("pulse write task panicked: {e}"),
            }
        }
        done
    });

    outcome.stats.retries = shared.retries.load(Ordering::SeqCst);
    outcome.stats.calls_made = shared.calls.load(Ordering::SeqCst);
    for (job, r) in finished {
        match r {
            Ok((text, usage)) => {
                used_keys.push(job.key.clone());
                for id in &job.ids {
                    if let Some(res) = outcome.results.get_mut(id) {
                        res.outcome = TaskOutcome::Called;
                        res.text = Some(text.clone());
                        res.usage = usage;
                    }
                }
                if let Some(u) = usage {
                    outcome.stats.input_tokens += u.input_tokens as u64;
                    outcome.stats.output_tokens += u.output_tokens as u64;
                    outcome.stats.cached_input_tokens += u.cached_input_tokens as u64;
                }
            }
            Err(CallError::Fatal(msg)) | Err(CallError::Failed(msg)) => {
                for id in &job.ids {
                    if let Some(res) = outcome.results.get_mut(id) {
                        res.outcome = TaskOutcome::Failed(msg.clone());
                    }
                }
            }
        }
    }
    if let Some(reason) = shared.stop.lock().unwrap().take() {
        outcome.degraded = Some(reason);
    }
    verdict(outcome, used_keys, cache, cfg, start)
}

/// Call one job with retries; write the cache on success.
async fn execute(
    provider: Arc<dyn LlmProvider>,
    job: &Job,
    cfg: &WriteConfig,
    shared: &Shared,
    cache: &WriteCache,
) -> CallResult {
    let task = &job.task;
    let started = Instant::now();
    let mut max_tokens = task.max_tokens;
    let mut grown = false;
    let mut attempt = 0u32;
    loop {
        if shared.stopped() {
            return Err(CallError::Failed("run stopped".into()));
        }
        attempt += 1;
        let req = CompletionRequest {
            system: &task.system,
            user: &task.user,
            output: task.output.as_mode(),
            max_tokens,
            temperature: 0.0,
            tag: &task.id,
        };
        shared.calls.fetch_add(1, Ordering::SeqCst);
        match provider.complete_request(&req).await {
            Ok(resp) => {
                if let Some(u) = resp.usage {
                    shared.tokens_used.fetch_add(
                        u.input_tokens as u64 + u.output_tokens as u64,
                        Ordering::SeqCst,
                    );
                }
                // Truncated JSON never parses, and a reasoning model may spend the whole
                // budget before answering: retry once with twice the room.
                if resp.stop == StopReason::MaxTokens
                    && (task.output != OutputSpec::Text || resp.text.trim().is_empty())
                    && !grown
                {
                    grown = true;
                    max_tokens = max_tokens.saturating_mul(2);
                    shared.retries.fetch_add(1, Ordering::SeqCst);
                    continue;
                }
                if let StopReason::Other(reason) = &resp.stop
                    && reason == "refusal"
                {
                    return Err(fail(shared, cfg, format!("{}: model refused", task.id)));
                }
                shared.consecutive_failures.store(0, Ordering::SeqCst);
                let entry = CacheEntry {
                    key: job.key.clone(),
                    task_id: task.id.clone(),
                    task_kind: task.kind.to_string(),
                    model: resp.model.clone(),
                    usage: resp.usage,
                    text: resp.text.trim().to_string(),
                };
                if let Err(e) = cache.put(&entry) {
                    log::warn!("pulse write cache: {e:#}");
                }
                eprintln!(
                    "  Writing: {} (done, {:.1}s)",
                    task.id,
                    started.elapsed().as_secs_f64()
                );
                return Ok((entry.text, resp.usage));
            }
            Err(e) => {
                let classified = e.downcast_ref::<ProviderError>();
                let kind = classified.map(|p| p.kind);
                if kind.is_some_and(|k| k.is_fatal()) {
                    let reason = format!("{e}");
                    shared.stop_with(reason.clone());
                    return Err(CallError::Fatal(reason));
                }
                // Unclassified errors come from legacy `complete()` paths: retry them.
                let retryable = kind.is_none_or(|k| k.is_retryable());
                if retryable && attempt < cfg.max_attempts {
                    if kind == Some(ProviderErrorKind::RateLimited) {
                        shared.halve_concurrency();
                    }
                    let delay = classified
                        .and_then(|p| p.retry_after)
                        .unwrap_or_else(|| backoff(cfg.base_backoff, attempt, &task.id));
                    log::debug!(
                        "pulse write: {} attempt {attempt} failed: {e}; retry in {delay:?}",
                        task.id
                    );
                    shared.retries.fetch_add(1, Ordering::SeqCst);
                    tokio::time::sleep(delay).await;
                    continue;
                }
                eprintln!(
                    "  Writing: {} (failed, {:.1}s): {e}",
                    task.id,
                    started.elapsed().as_secs_f64()
                );
                return Err(fail(shared, cfg, format!("{e}")));
            }
        }
    }
}

fn fail(shared: &Shared, cfg: &WriteConfig, msg: String) -> CallError {
    let n = shared.consecutive_failures.fetch_add(1, Ordering::SeqCst) + 1;
    if n >= cfg.breaker_threshold {
        shared.stop_with(format!("{n} consecutive LLM failures; last: {msg}"));
    }
    CallError::Failed(msg)
}

/// `min(30s, base ยท 2^(attempt-1))` plus up to 25% jitter derived from the task id,
/// so reruns wait the same way.
fn backoff(base: Duration, attempt: u32, id: &str) -> Duration {
    let exp = base.saturating_mul(1u32 << (attempt - 1).min(5));
    let capped = exp.min(Duration::from_secs(30));
    let h = blake3::hash(id.as_bytes()).as_bytes()[0] as u32;
    capped + capped.mul_f64(h as f64 / 255.0 * 0.25)
}

fn verdict(
    mut outcome: WriteOutcome,
    used_keys: Vec<String>,
    cache: &WriteCache,
    cfg: &WriteConfig,
    start: Instant,
) -> WriteOutcome {
    if outcome.dry_run {
        return finish(outcome, start);
    }
    let mut ok = 0usize;
    let mut eligible = 0usize;
    for r in outcome.results.values() {
        match r.outcome {
            TaskOutcome::Cached | TaskOutcome::Called => {
                ok += 1;
                eligible += 1;
            }
            TaskOutcome::Failed(_) => eligible += 1,
            _ => {}
        }
    }
    if outcome.degraded.is_none()
        && cfg.mode == LlmMode::On
        && eligible > 0
        && (ok as f32) / (eligible as f32) < cfg.degrade_threshold
    {
        outcome.degraded = Some(format!(
            "only {ok} of {eligible} LLM sections succeeded (need {:.0}%)",
            cfg.degrade_threshold * 100.0
        ));
    }
    let complete = outcome.degraded.is_none() && !outcome.truncated;
    if complete && cfg.prune && cfg.mode == LlmMode::On && eligible > 0 {
        match cache.record_run_and_prune(used_keys, cfg.keep_runs) {
            Ok(n) => outcome.stats.pruned = n,
            Err(e) => log::warn!("pulse write cache prune failed: {e:#}"),
        }
    }
    finish(outcome, start)
}

fn finish(mut outcome: WriteOutcome, start: Instant) -> WriteOutcome {
    let s = &mut outcome.stats;
    for r in outcome.results.values() {
        match r.outcome {
            TaskOutcome::Cached => s.cached += 1,
            TaskOutcome::Called => s.called += 1,
            TaskOutcome::Suppressed(_) => s.suppressed += 1,
            TaskOutcome::Deferred => s.deferred += 1,
            TaskOutcome::NotCached => s.not_cached += 1,
            TaskOutcome::Failed(_) => s.failed += 1,
            TaskOutcome::Skipped => s.skipped += 1,
        }
    }
    if outcome.dry_run {
        // Nothing was called; "Called" in the plan means "would call".
        s.called = 0;
    }
    s.elapsed = start.elapsed();
    outcome
}