claude_profile 1.4.1

Claude Code account credential management and token status
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
// BUG-289 task/claude_profile/bug/289_son_running_false_haiku_touch_infinite_loop.md — resolve_model Auto gate simplified to son_idle only (Fix: BUG-289, BUG-290)

//! Subprocess model and effort resolution for the `apply_touch` pipeline.
//!
//! `resolve_model` maps `imodel::` + quota data → `IsolatedModel`;
//! `resolve_effort` maps the resolved model + `effort::` → optional effort flag;
//! `effort_pre_args` assembles the `--effort` arg slice for subprocess dispatch.

use super::types::{ AccountQuota, SubprocessModel, SubprocessEffort };

// ── Model resolution ──────────────────────────────────────────────────────────

/// Resolve the subprocess model for one account based on `imodel::` and quota data.
///
/// AC-01: `auto` selects Haiku for general keep-alive pings — Haiku conserves Sonnet
///        and Opus quota.
///        Utilization-aware Sonnet gate (Fix BUG-289, BUG-290, TSK-292; extended Fix BUG-301, TSK-311):
///        Selects `claude-sonnet-4-6` when `seven_day_sonnet` present AND either:
///          • `son_idle=true` (`resets_at=None`) — Haiku cannot start the Sonnet window; a single
///            Sonnet touch opens all idle dimensions simultaneously (5h, 7d, Son); OR
///          • `son_available=true` (`(100 - utilization) > 20%`) — remaining Sonnet quota should
///            not expire unused while Haiku pings keep sessions alive.
///        Falls through to Haiku when Sonnet tier absent or utilization ≥ 80% (≤ 20% remaining).
/// AC-02: `sonnet` always maps to `claude-sonnet-4-6`.
/// AC-03: `opus` always maps to `claude-opus-4-6`.
/// AC-04: `keep` passes `IsolatedModel::KeepCurrent` — no `--model` flag injected.
/// AC-13: `haiku` always maps to `claude-haiku-4-5-20251001`.
#[ inline ]
pub( crate ) fn resolve_model( aq : &AccountQuota, imodel : SubprocessModel ) -> claude_runner_core::IsolatedModel
{
  use claude_runner_core::IsolatedModel;
  match imodel
  {
    SubprocessModel::Sonnet => IsolatedModel::Specific( "claude-sonnet-4-6".to_string() ),
    SubprocessModel::Opus   => IsolatedModel::Specific( "claude-opus-4-6".to_string() ),
    SubprocessModel::Keep   => IsolatedModel::KeepCurrent,
    SubprocessModel::Haiku  => IsolatedModel::Specific( "claude-haiku-4-5-20251001".to_string() ),
    SubprocessModel::Auto   =>
    {
      // Fix(BUG-289, BUG-290, TSK-292): son_idle gate — Haiku cannot activate the 7d-Sonnet window.
      // Fix(BUG-301, TSK-311): son_available gate — remaining Sonnet quota must not expire unused.
      // Sonnet selected when: son_idle (resets_at=None) OR son_available (>20% remaining).
      if let Ok( ref data ) = aq.result
      {
        if let Some( ref son ) = data.seven_day_sonnet
        {
          let son_idle      = son.resets_at.is_none();
          let son_available = 100.0 - son.utilization > 20.0;
          if son_idle || son_available
          {
            return IsolatedModel::Specific( "claude-sonnet-4-6".to_string() );
          }
        }
      }
      // AC-01: Haiku for general keep-alive pings — conserves Sonnet and Opus quota.
      IsolatedModel::Specific( "claude-haiku-4-5-20251001".to_string() )
    }
  }
}

// ── Effort resolution ─────────────────────────────────────────────────────────

/// Resolve the `--effort` flag value for a subprocess given the resolved model.
///
/// Returns `None` when no `--effort` flag should be injected.
/// AC-05: `auto` → `low` for any model that supports effort (Sonnet, Opus); `None` for Haiku or `KeepCurrent`.
///         Haiku has no extended thinking; injecting `--effort` would have no effect or API error.
///         `KeepCurrent` → `None` (model unknown at dispatch time).
/// AC-06: `high` always injects `--effort high`.
/// AC-07: `max` always injects `--effort max`.
/// AC-14: `low` always injects `--effort low`.
/// AC-15: `normal` always injects `--effort normal`.
#[ inline ]
pub( crate ) fn resolve_effort( model : &claude_runner_core::IsolatedModel, effort : SubprocessEffort ) -> Option< &'static str >
{
  use claude_runner_core::IsolatedModel;
  match effort
  {
    SubprocessEffort::High   => Some( "high" ),
    SubprocessEffort::Max    => Some( "max" ),
    SubprocessEffort::Low    => Some( "low" ),
    SubprocessEffort::Normal => Some( "normal" ),
    SubprocessEffort::Auto => match model
    {
      IsolatedModel::Specific( m ) if m.as_str() == "claude-haiku-4-5-20251001" => None,
      IsolatedModel::Specific( _ )                                               => Some( "low" ),
      IsolatedModel::KeepCurrent | IsolatedModel::Default                       => None,
    },
  }
}

/// Build the `extra_pre_args` slice to prepend before `["--print", "."]` in a subprocess.
///
/// Returns `["--effort", value]` when effort resolves to `Some`, otherwise an empty vec.
#[ inline ]
pub( crate ) fn effort_pre_args( model : &claude_runner_core::IsolatedModel, effort : SubprocessEffort ) -> Vec< String >
{
  match resolve_effort( model, effort )
  {
    Some( e ) => vec![ "--effort".to_string(), e.to_string() ],
    None      => vec![],
  }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[ cfg( test ) ]
mod tests
{
  use super::*;
  use crate::usage::test_support::{ mk_aq_with_sonnet_util, mk_aq_no_sonnet_data, mk_aq_err, mk_aq_with_son_idle };

  // ── resolve_model ──────────────────────────────────────────────────────────

  /// FT-01: `imodel::auto` selects sonnet when 5h absent and `son_idle=true`.
  ///
  /// `mk_aq_with_sonnet_util(85.0)` produces `five_hour=None, son_idle=true`.
  /// Under the `son_idle` gate, Sonnet is selected regardless of 5h state.
  /// Verifies the old `five_h_running` constraint is gone. Fix(BUG-290).
  ///
  /// Spec: [`tests/docs/feature/26_subprocess_model_effort.md` FT-01]
  #[ test ]
  fn it_imodel_auto_selects_sonnet_when_5h_absent()
  {
    let aq       = mk_aq_with_sonnet_util( 85.0 );
    let model    = resolve_model( &aq, SubprocessModel::Auto );
    let model_id = match &model { claude_runner_core::IsolatedModel::Specific( m ) => m.as_str(), _ => "" };
    assert_eq!(
      model_id, "claude-sonnet-4-6",
      "imodel::auto must select sonnet when son_idle=true (5h absent does not block)",
    );
  }

  /// FT-02: `imodel::auto` selects sonnet with high Sonnet util and `son_idle=true`.
  ///
  /// Utilization percentage is irrelevant for model selection.
  ///
  /// Spec: [`tests/docs/feature/26_subprocess_model_effort.md` FT-02]
  #[ test ]
  fn it_imodel_auto_selects_sonnet_when_5h_absent_high_util()
  {
    let aq       = mk_aq_with_sonnet_util( 35.0 );
    let model    = resolve_model( &aq, SubprocessModel::Auto );
    let model_id = match &model { claude_runner_core::IsolatedModel::Specific( m ) => m.as_str(), _ => "" };
    assert_eq!(
      model_id, "claude-sonnet-4-6",
      "imodel::auto must select sonnet when son_idle=true regardless of utilization",
    );
  }

  /// FT-03: `imodel::auto` selects sonnet at util boundary and `son_idle=true`.
  ///
  /// Former 20% threshold boundary — utilization is irrelevant for model selection.
  ///
  /// Spec: [`tests/docs/feature/26_subprocess_model_effort.md` FT-03]
  #[ test ]
  fn it_imodel_auto_selects_sonnet_when_5h_absent_boundary_util()
  {
    let aq       = mk_aq_with_sonnet_util( 20.0 );
    let model    = resolve_model( &aq, SubprocessModel::Auto );
    let model_id = match &model { claude_runner_core::IsolatedModel::Specific( m ) => m.as_str(), _ => "" };
    assert_eq!(
      model_id, "claude-sonnet-4-6",
      "imodel::auto must select sonnet when son_idle=true at util boundary",
    );
  }

  /// FT-04: `imodel::auto` with absent quota data → haiku (quota not needed).
  ///
  /// Spec: [`tests/docs/feature/026_subprocess_model_effort.md` FT-04]
  #[ test ]
  fn it_imodel_auto_selects_haiku_without_quota_data()
  {
    let aq       = mk_aq_no_sonnet_data();
    let model    = resolve_model( &aq, SubprocessModel::Auto );
    let model_id = match &model { claude_runner_core::IsolatedModel::Specific( m ) => m.as_str(), _ => "" };
    assert_eq!(
      model_id, "claude-haiku-4-5-20251001",
      "imodel::auto with absent quota data must still select haiku",
    );
  }

  /// EC-9a: `imodel::auto` with account error result → haiku (quota data irrelevant).
  #[ test ]
  fn it_imodel_auto_selects_haiku_with_err_result()
  {
    let aq       = mk_aq_err();
    let model    = resolve_model( &aq, SubprocessModel::Auto );
    let model_id = match &model { claude_runner_core::IsolatedModel::Specific( m ) => m.as_str(), _ => "" };
    assert_eq!(
      model_id, "claude-haiku-4-5-20251001",
      "imodel::auto with Err result must select haiku (quota data not needed)",
    );
  }

  /// Auto model + `effort::auto` → `None` (haiku does not support extended thinking).
  ///
  /// End-to-end: `resolve_model(aq, Auto)` returns haiku; `resolve_effort(haiku, Auto)` → `None`.
  #[ test ]
  fn it_effort_auto_resolves_none_for_auto_haiku()
  {
    let aq     = mk_aq_no_sonnet_data();
    let model  = resolve_model( &aq, SubprocessModel::Auto );
    let effort = resolve_effort( &model, SubprocessEffort::Auto );
    assert!(
      effort.is_none(),
      "imodel::auto + effort::auto must produce None effort (haiku has no extended thinking), got: {effort:?}",
    );
  }

  /// FT-22: `imodel::auto` selects sonnet when `son_idle=true` (5h running, 7d absent).
  ///
  /// The 7d-Sonnet window only activates on Sonnet-family API calls.
  /// Haiku cannot start it. Fix(BUG-289, BUG-290, TSK-292): `son_idle` gate fires.
  ///
  /// Spec: [`tests/docs/feature/26_subprocess_model_effort.md` FT-22]
  #[ test ]
  fn it_imodel_auto_selects_sonnet_when_son_idle()
  {
    let aq       = mk_aq_with_son_idle();
    let model    = resolve_model( &aq, SubprocessModel::Auto );
    let model_id = match &model { claude_runner_core::IsolatedModel::Specific( m ) => m.as_str(), _ => "" };
    assert_eq!(
      model_id, "claude-sonnet-4-6",
      "imodel::auto must select sonnet when son_idle=true (BUG-289/BUG-290 fix)",
    );
  }

  /// EC-9b: `imodel::auto` selects Haiku when Sonnet window running AND exhausted (≤ 20% remaining).
  ///
  /// `seven_day_sonnet.resets_at=Some(...)` → `son_idle=false`.
  /// `seven_day_sonnet.utilization=90.0` → `son_available=(100-90>20)=false`.
  /// Both gate conditions false → Haiku selected. Conserves the last 10% of Sonnet quota.
  /// Fix(BUG-301, TSK-311): Haiku now requires BOTH gates to fail, not just `son_idle=false`.
  ///
  /// Test Matrix row 2: `five_h=running, d7=running, son=exhausted (90%)` → Haiku.
  #[ test ]
  fn it_imodel_auto_selects_haiku_when_son_running()
  {
    // Start from son_idle base; override son to running and exhausted (90% used, 10% remaining < 20%).
    let mut aq = mk_aq_with_son_idle();
    if let Ok( ref mut data ) = aq.result
    {
      if let Some( ref mut son ) = data.seven_day_sonnet
      {
        son.resets_at   = Some( "2026-06-14T10:00:00Z".to_string() );
        son.utilization = 90.0;   // Fix(BUG-301): exhausted → Haiku; available (e.g. 60%) → Sonnet
      }
    }
    let model    = resolve_model( &aq, SubprocessModel::Auto );
    let model_id = match &model { claude_runner_core::IsolatedModel::Specific( m ) => m.as_str(), _ => "" };
    assert_eq!(
      model_id, "claude-haiku-4-5-20251001",
      "imodel::auto must select haiku when son_idle=false AND son_available=false (90% used, 10% < 20% threshold)",
    );
  }

  /// EC-9c: `imodel::auto` selects Sonnet when 5h idle + `son_idle=true`.
  ///
  /// `five_hour.resets_at=None` (5h timer present but inactive). `son_idle=true` → gate fires
  /// regardless of 5h state. Verifies old `five_h_running` constraint removed. Fix(BUG-290).
  ///
  /// Test Matrix row 4: `five_h=idle (Some({resets_at:None})), d7=running, son_idle=true` → Sonnet.
  #[ test ]
  fn it_imodel_auto_selects_sonnet_when_5h_idle()
  {
    // Start from son_idle base; override 5h to idle (resets_at=None → five_h_running=false).
    let mut aq = mk_aq_with_son_idle();
    if let Ok( ref mut data ) = aq.result
    {
      if let Some( ref mut five_h ) = data.five_hour
      {
        five_h.resets_at = None;
      }
    }
    let model    = resolve_model( &aq, SubprocessModel::Auto );
    let model_id = match &model { claude_runner_core::IsolatedModel::Specific( m ) => m.as_str(), _ => "" };
    assert_eq!(
      model_id, "claude-sonnet-4-6",
      "imodel::auto must select sonnet when son_idle=true (5h idle does not block new gate)",
    );
  }

  /// FT-23: `imodel::auto` selects Haiku when Sonnet tier absent (`seven_day_sonnet=None`).
  ///
  /// `seven_day_sonnet=None` → `son_idle = None.is_some_and(...) = false`.
  /// `son_idle` gate requires `son_idle=true`; absent tier → gate does NOT fire.
  /// `auto` returns Haiku — no Sonnet window exists to start.
  ///
  /// Test Matrix row 3: `five_h=running, d7=None (running), son=absent` → Haiku.
  ///
  /// Spec: [`tests/docs/feature/026_subprocess_model_effort.md` FT-23]
  #[ test ]
  fn it_imodel_auto_selects_haiku_when_son_tier_absent()
  {
    // Start from son_idle base; remove Sonnet tier entirely → son_idle=false.
    let mut aq = mk_aq_with_son_idle();
    if let Ok( ref mut data ) = aq.result
    {
      data.seven_day_sonnet = None;
    }
    let model    = resolve_model( &aq, SubprocessModel::Auto );
    let model_id = match &model { claude_runner_core::IsolatedModel::Specific( m ) => m.as_str(), _ => "" };
    assert_eq!(
      model_id, "claude-haiku-4-5-20251001",
      "imodel::auto must select haiku when seven_day_sonnet=None (son_idle=false); gate requires son_idle=true",
    );
  }

  /// FT-24: `imodel::auto` selects Sonnet when 7d timer idle and `son_idle=true`.
  ///
  /// `seven_day=Some({resets_at:None})` → `d7_running=false`.
  /// `son_idle=true` → gate fires regardless of `d7_running` state.
  /// Verifies old `d7_running` constraint removed. Fix(BUG-290).
  ///
  /// Test Matrix row 6: `five_h=running, d7=Some({resets_at:None}) (idle), son_idle=true` → Sonnet.
  ///
  /// Spec: [`tests/docs/feature/26_subprocess_model_effort.md` FT-24]
  #[ test ]
  fn it_imodel_auto_selects_sonnet_when_d7_idle()
  {
    // Start from son_idle base (seven_day=None → d7=true); override to Some(resets_at=None).
    let mut aq = mk_aq_with_son_idle();
    if let Ok( ref mut data ) = aq.result
    {
      data.seven_day = Some( claude_quota::PeriodUsage { utilization: 50.0, resets_at: None } );
    }
    let model    = resolve_model( &aq, SubprocessModel::Auto );
    let model_id = match &model { claude_runner_core::IsolatedModel::Specific( m ) => m.as_str(), _ => "" };
    assert_eq!(
      model_id, "claude-sonnet-4-6",
      "imodel::auto must select sonnet when son_idle=true (d7 idle does not block new gate)",
    );
  }

  /// FT-25: `imodel::auto` selects Sonnet when 7d running via explicit Some path and `son_idle=true`.
  ///
  /// `seven_day=Some({resets_at:Some(...)})` exercises `map_or` Some-branch.
  /// `son_idle=true` → gate fires.
  ///
  /// Verifies the `seven_day=Some(running)` path correctly resolves to Sonnet.
  ///
  /// Test Matrix row 7: `five_h=running, d7=Some({resets_at:Some(...)}) (running via Some), son_idle=true` → Sonnet.
  ///
  /// Spec: [`tests/docs/feature/026_subprocess_model_effort.md` FT-25]
  #[ test ]
  fn it_imodel_auto_selects_sonnet_when_d7_running_explicit()
  {
    // Start from son_idle base; override seven_day to Some(running) — exercises map_or Some branch.
    let mut aq = mk_aq_with_son_idle();
    if let Ok( ref mut data ) = aq.result
    {
      data.seven_day = Some( claude_quota::PeriodUsage
      {
        utilization : 10.0,
        resets_at   : Some( "2026-06-15T10:00:00Z".to_string() ),
      } );
    }
    let model    = resolve_model( &aq, SubprocessModel::Auto );
    let model_id = match &model { claude_runner_core::IsolatedModel::Specific( m ) => m.as_str(), _ => "" };
    assert_eq!(
      model_id, "claude-sonnet-4-6",
      "imodel::auto must select sonnet when son_idle=true; d7 and 5h state irrelevant to new gate",
    );
  }

  /// FT-26: `imodel::auto` selects Sonnet when 5h absent + 7d running and `son_idle=true`.
  ///
  /// `five_hour=None` → `five_h_running=false`; `son_idle=true` → gate fires regardless.
  /// This is the BUG-290 cold account scenario. Verifies old `five_h_running` short-circuit
  /// is removed: a single Sonnet touch opens 5h and Son simultaneously. Fix(BUG-290).
  ///
  /// Test Matrix extra row: `five_h=absent, d7=Some(running via Some), son=idle` → Sonnet.
  ///
  /// Spec: [`tests/docs/feature/26_subprocess_model_effort.md` FT-26]
  #[ test ]
  fn it_imodel_auto_selects_sonnet_when_5h_absent_d7_some_running()
  {
    // Start from son_idle base; remove 5h + set d7 to Some(running) to exercise map_or Some-branch.
    let mut aq = mk_aq_with_son_idle();
    if let Ok( ref mut data ) = aq.result
    {
      data.five_hour  = None;
      data.seven_day  = Some( claude_quota::PeriodUsage
      {
        utilization : 20.0,
        resets_at   : Some( "2026-06-15T10:00:00Z".to_string() ),
      } );
    }
    let model    = resolve_model( &aq, SubprocessModel::Auto );
    let model_id = match &model { claude_runner_core::IsolatedModel::Specific( m ) => m.as_str(), _ => "" };
    assert_eq!(
      model_id, "claude-sonnet-4-6",
      "imodel::auto must select sonnet when son_idle=true (5h absent does not block new gate)",
    );
  }

  /// FT-27: `imodel::auto` selects Haiku when Sonnet running AND exhausted, regardless of 7d state.
  ///
  /// `seven_day_sonnet.resets_at=Some(...)` → `son_idle=false`.
  /// `seven_day_sonnet.utilization=90.0` → `son_available=(100-90>20)=false`.
  /// Both gate conditions false → Haiku selected. `d7_running` state is irrelevant.
  /// Exercises `son=exhausted` with 7d-idle. Fix(BUG-301, TSK-311).
  ///
  /// Test Matrix extra row: `five_h=running, d7=Some(idle), son=exhausted (90%)` → Haiku.
  ///
  /// Spec: [`tests/docs/feature/026_subprocess_model_effort.md` FT-27]
  #[ test ]
  fn it_imodel_auto_selects_haiku_when_d7_idle_and_son_running()
  {
    // Start from son_idle base; set d7=Some(idle) and son=exhausted (90% used, 10% remaining < 20%).
    let mut aq = mk_aq_with_son_idle();
    if let Ok( ref mut data ) = aq.result
    {
      data.seven_day = Some( claude_quota::PeriodUsage { utilization: 50.0, resets_at: None } );
      if let Some( ref mut son ) = data.seven_day_sonnet
      {
        son.resets_at   = Some( "2026-06-14T10:00:00Z".to_string() );
        son.utilization = 90.0;   // Fix(BUG-301): exhausted → Haiku regardless of 7d state
      }
    }
    let model    = resolve_model( &aq, SubprocessModel::Auto );
    let model_id = match &model { claude_runner_core::IsolatedModel::Specific( m ) => m.as_str(), _ => "" };
    assert_eq!(
      model_id, "claude-haiku-4-5-20251001",
      "imodel::auto must select haiku when son_idle=false AND son_available=false (90% used); d7 state irrelevant",
    );
  }

  /// FT-28: `imodel::auto` selects Haiku when Sonnet tier absent and 7d idle.
  ///
  /// `seven_day_sonnet=None` → `son_idle = None.is_some_and(...) = false`. Gate does NOT fire → Haiku.
  /// `d7_running` state is irrelevant to new gate.
  /// Exercises `son=absent(None)` combined with `d7=Some(idle)` — distinct from FT-24 (`son_idle=true`).
  ///
  /// Test Matrix extra row: `five_h=running, d7=Some(idle), son=absent` → Haiku.
  ///
  /// Spec: [`tests/docs/feature/026_subprocess_model_effort.md` FT-28]
  #[ test ]
  fn it_imodel_auto_selects_haiku_when_d7_idle_and_son_absent()
  {
    // Start from son_idle base; set d7=Some(idle) and remove son tier entirely.
    let mut aq = mk_aq_with_son_idle();
    if let Ok( ref mut data ) = aq.result
    {
      data.seven_day        = Some( claude_quota::PeriodUsage { utilization: 50.0, resets_at: None } );
      data.seven_day_sonnet = None;
    }
    let model    = resolve_model( &aq, SubprocessModel::Auto );
    let model_id = match &model { claude_runner_core::IsolatedModel::Specific( m ) => m.as_str(), _ => "" };
    assert_eq!(
      model_id, "claude-haiku-4-5-20251001",
      "imodel::auto must select haiku when son_idle=false (absent); d7 state irrelevant to new gate",
    );
  }

  /// FT-29: `imodel::auto` selects Haiku when 7d running via Some + Sonnet tier absent.
  ///
  /// `seven_day=Some({resets_at:Some(...)})` → `d7_running=true` via `map_or` Some-branch.
  /// `seven_day_sonnet=None` → `son_idle = None.is_some_and(...) = false`.
  /// Gate requires `son_idle=true`; absent Sonnet tier blocks it. Haiku selected.
  ///
  /// Completes the `d7=Some(running)` Some-branch column alongside FT-25 (Sonnet, son=idle)
  /// and FT-30 (Haiku, son=running): three cells exhausting the `d7=Z + son=*` combinations.
  ///
  /// Test Matrix row 25: `five_h=running, d7=Some(running via Some), son=absent` → Haiku.
  ///
  /// Spec: [`tests/docs/feature/026_subprocess_model_effort.md` FT-29]
  #[ test ]
  fn it_imodel_auto_selects_haiku_when_d7_some_running_and_son_absent()
  {
    // Start from son_idle base; override d7 to Some(running) and remove son tier.
    let mut aq = mk_aq_with_son_idle();
    if let Ok( ref mut data ) = aq.result
    {
      data.seven_day = Some( claude_quota::PeriodUsage
      {
        utilization : 10.0,
        resets_at   : Some( "2026-06-15T10:00:00Z".to_string() ),
      } );
      data.seven_day_sonnet = None;
    }
    let model    = resolve_model( &aq, SubprocessModel::Auto );
    let model_id = match &model { claude_runner_core::IsolatedModel::Specific( m ) => m.as_str(), _ => "" };
    assert_eq!(
      model_id, "claude-haiku-4-5-20251001",
      "imodel::auto must select haiku when d7_running=true (Some-branch) but son_idle=false (son absent); son blocks gate",
    );
  }

  /// MRE — BUG-301: `imodel::auto` selects Haiku when Sonnet window active with 40% remaining.
  ///
  /// Root cause: `subprocess.rs:41` binary `son_idle` gate checked only `resets_at.is_none()`.
  /// When Sonnet window is running (`resets_at=Some`) but 40% quota remains, `son_idle=false` and
  /// the old gate returned Haiku — wasting the remaining quota as the window timer expires.
  /// Fix: extended gate checks `son_available = (100.0 - utilization) > 20.0` alongside `son_idle`.
  ///
  /// Fix(BUG-301, TSK-311).
  ///
  /// Spec: [`tests/docs/feature/026_subprocess_model_effort.md` FT-31]
  #[ test ]
  fn mre_bug301_son_active_with_remaining_quota_selects_sonnet()
  {
    // Sonnet window active (resets_at=Some) with 60% utilization (40% remaining > 20% threshold).
    let mut aq = mk_aq_with_son_idle();
    if let Ok( ref mut data ) = aq.result
    {
      if let Some( ref mut son ) = data.seven_day_sonnet
      {
        son.resets_at   = Some( "2026-06-20T10:00:00Z".to_string() );
        son.utilization = 60.0;
      }
    }
    let model    = resolve_model( &aq, SubprocessModel::Auto );
    let model_id = match &model { claude_runner_core::IsolatedModel::Specific( m ) => m.as_str(), _ => "" };
    assert_eq!(
      model_id, "claude-sonnet-4-6",
      "imodel::auto must select sonnet when son_idle=false but son_available=true (40% remaining); Fix(BUG-301)",
    );
  }

  /// FT-30: `imodel::auto` selects Haiku when 7d running via Some + Sonnet exhausted.
  ///
  /// `seven_day=Some({resets_at:Some(...)})` → `d7_running=true` via `map_or` Some-branch.
  /// `seven_day_sonnet.resets_at=Some(...)` → `son_idle=false`.
  /// `seven_day_sonnet.utilization=90.0` → `son_available=(100-90>20)=false`.
  /// Both gate conditions false; Haiku selected (Sonnet exhausted, conserve last 10%).
  ///
  /// Completes the `d7=Some(running)` Some-branch column alongside FT-25 (Sonnet, son=idle)
  /// and FT-29 (Haiku, son=absent): all three `son` states under `d7=Z + five_h=running` now covered.
  ///
  /// Test Matrix row 27: `five_h=running, d7=Some(running via Some), son=exhausted` → Haiku.
  ///
  /// Spec: [`tests/docs/feature/026_subprocess_model_effort.md` FT-30]
  #[ test ]
  fn it_imodel_auto_selects_haiku_when_d7_some_running_and_son_running()
  {
    // Start from son_idle base; override d7 to Some(running) and set son to exhausted (90% used).
    // son_idle=false (resets_at=Some) AND son_available=(100-90>20)=false → both gates fail → Haiku.
    let mut aq = mk_aq_with_son_idle();
    if let Ok( ref mut data ) = aq.result
    {
      data.seven_day = Some( claude_quota::PeriodUsage
      {
        utilization : 10.0,
        resets_at   : Some( "2026-06-15T10:00:00Z".to_string() ),
      } );
      if let Some( ref mut son ) = data.seven_day_sonnet
      {
        son.resets_at   = Some( "2026-06-14T10:00:00Z".to_string() );
        son.utilization = 90.0;   // Fix(BUG-301): test Haiku via exhaustion, not binary gate
      }
    }
    let model    = resolve_model( &aq, SubprocessModel::Auto );
    let model_id = match &model { claude_runner_core::IsolatedModel::Specific( m ) => m.as_str(), _ => "" };
    assert_eq!(
      model_id, "claude-haiku-4-5-20251001",
      "imodel::auto must select haiku when son_idle=false and son_available=false (90% used, 10% remaining < 20%)",
    );
  }

  /// EC-6: `imodel::sonnet` always returns `IsolatedModel::Specific("claude-sonnet-4-6")`.
  ///
  /// Spec: [`tests/docs/cli/param/035_imodel.md` EC-6]
  #[ test ]
  fn it_imodel_sonnet_explicit()
  {
    let aq       = mk_aq_no_sonnet_data();
    let model    = resolve_model( &aq, SubprocessModel::Sonnet );
    let model_id = match &model { claude_runner_core::IsolatedModel::Specific( m ) => m.as_str(), _ => "" };
    assert_eq!( model_id, "claude-sonnet-4-6", "imodel::sonnet must always return claude-sonnet-4-6" );
  }

  /// EC-7: `imodel::opus` always returns `IsolatedModel::Specific("claude-opus-4-6")`.
  ///
  /// Spec: [`tests/docs/cli/param/035_imodel.md` EC-7]
  #[ test ]
  fn it_imodel_opus_explicit()
  {
    let aq       = mk_aq_no_sonnet_data();
    let model    = resolve_model( &aq, SubprocessModel::Opus );
    let model_id = match &model { claude_runner_core::IsolatedModel::Specific( m ) => m.as_str(), _ => "" };
    assert_eq!( model_id, "claude-opus-4-6", "imodel::opus must always return claude-opus-4-6" );
  }

  /// EC-8: `imodel::keep` returns `IsolatedModel::KeepCurrent` — no `--model` flag.
  ///
  /// Spec: [`tests/docs/cli/param/035_imodel.md` EC-8]
  #[ test ]
  fn it_imodel_keep_no_model_flag()
  {
    let aq    = mk_aq_no_sonnet_data();
    let model = resolve_model( &aq, SubprocessModel::Keep );
    assert!(
      matches!( model, claude_runner_core::IsolatedModel::KeepCurrent ),
      "imodel::keep must return KeepCurrent (no --model flag)",
    );
  }

  // ── resolve_effort ────────────────────────────────────────────────────────

  /// `effort::high` always returns `Some("high")` regardless of model.
  ///
  /// Spec: [`tests/docs/cli/param/036_effort.md` EC-5]
  #[ test ]
  fn it_effort_high_explicit()
  {
    let sonnet = claude_runner_core::IsolatedModel::Specific( "claude-sonnet-4-6".to_string() );
    let opus   = claude_runner_core::IsolatedModel::Specific( "claude-opus-4-6".to_string() );
    let keep   = claude_runner_core::IsolatedModel::KeepCurrent;
    assert_eq!( resolve_effort( &sonnet, SubprocessEffort::High ), Some( "high" ) );
    assert_eq!( resolve_effort( &opus,   SubprocessEffort::High ), Some( "high" ) );
    assert_eq!( resolve_effort( &keep,   SubprocessEffort::High ), Some( "high" ) );
  }

  /// `effort::max` always returns `Some("max")` regardless of model.
  ///
  /// Spec: [`tests/docs/cli/param/036_effort.md` EC-6]
  #[ test ]
  fn it_effort_max_explicit()
  {
    let sonnet = claude_runner_core::IsolatedModel::Specific( "claude-sonnet-4-6".to_string() );
    let opus   = claude_runner_core::IsolatedModel::Specific( "claude-opus-4-6".to_string() );
    let keep   = claude_runner_core::IsolatedModel::KeepCurrent;
    assert_eq!( resolve_effort( &sonnet, SubprocessEffort::Max ), Some( "max" ) );
    assert_eq!( resolve_effort( &opus,   SubprocessEffort::Max ), Some( "max" ) );
    assert_eq!( resolve_effort( &keep,   SubprocessEffort::Max ), Some( "max" ) );
  }

  /// AC-05: `effort::auto` → `low` for Sonnet and Opus; `None` for `KeepCurrent`.
  ///
  /// Spec: [`tests/docs/cli/param/036_effort.md` EC-7–EC-9]
  #[ test ]
  fn it_effort_auto_uniform_low()
  {
    let sonnet = claude_runner_core::IsolatedModel::Specific( "claude-sonnet-4-6".to_string() );
    let opus   = claude_runner_core::IsolatedModel::Specific( "claude-opus-4-6".to_string() );
    let keep   = claude_runner_core::IsolatedModel::KeepCurrent;
    assert_eq!( resolve_effort( &sonnet, SubprocessEffort::Auto ), Some( "low" ), "auto+sonnet must be low" );
    assert_eq!( resolve_effort( &opus,   SubprocessEffort::Auto ), Some( "low" ), "auto+opus must be low" );
    assert_eq!( resolve_effort( &keep,   SubprocessEffort::Auto ), None,          "auto+keep must be None" );
  }

  /// `imodel::keep` + `effort::auto` → no `--effort` flag (`effort_pre_args` returns empty vec).
  ///
  /// Spec: [`tests/docs/cli/param/035_imodel.md` EC-8 interaction note]
  #[ test ]
  fn it_imodel_keep_effort_auto_no_effort_flag()
  {
    let aq       = mk_aq_no_sonnet_data();
    let model    = resolve_model( &aq, SubprocessModel::Keep );
    let pre_args = effort_pre_args( &model, SubprocessEffort::Auto );
    assert!(
      pre_args.is_empty(),
      "imodel::keep + effort::auto must produce no pre-args (no --effort flag), got: {pre_args:?}",
    );
  }

  // ── TSK-209: haiku model + low/normal effort ─────────────────────────────

  /// FT-18 / EC-12 (035): `imodel::haiku` → `IsolatedModel::Specific("claude-haiku-4-5-20251001")`.
  ///
  /// Haiku is explicit-only; `imodel::auto` continues to select between Sonnet and Opus only.
  ///
  /// Spec: [`tests/docs/feature/026_subprocess_model_effort.md` FT-18]
  ///       [`tests/docs/cli/param/035_imodel.md` EC-12]
  #[ test ]
  fn it_imodel_haiku_explicit()
  {
    let aq       = mk_aq_no_sonnet_data();
    let model    = resolve_model( &aq, SubprocessModel::Haiku );
    let model_id = match &model { claude_runner_core::IsolatedModel::Specific( m ) => m.as_str(), _ => "" };
    assert_eq!(
      model_id, "claude-haiku-4-5-20251001",
      "imodel::haiku must always return claude-haiku-4-5-20251001",
    );
  }

  /// FT-20 / EC-12 (036): `effort::low` always returns `Some("low")` regardless of model.
  ///
  /// Spec: [`tests/docs/feature/026_subprocess_model_effort.md` FT-20]
  ///       [`tests/docs/cli/param/036_effort.md` EC-12]
  #[ test ]
  fn it_effort_low_explicit()
  {
    let sonnet = claude_runner_core::IsolatedModel::Specific( "claude-sonnet-4-6".to_string() );
    let haiku  = claude_runner_core::IsolatedModel::Specific( "claude-haiku-4-5-20251001".to_string() );
    let keep   = claude_runner_core::IsolatedModel::KeepCurrent;
    assert_eq!( resolve_effort( &sonnet, SubprocessEffort::Low ), Some( "low" ), "effort::low with sonnet must be low" );
    assert_eq!( resolve_effort( &haiku,  SubprocessEffort::Low ), Some( "low" ), "effort::low with haiku must be low" );
    assert_eq!( resolve_effort( &keep,   SubprocessEffort::Low ), Some( "low" ), "effort::low with keep must be low" );
  }

  /// FT-21 / EC-13 (036): `effort::normal` always returns `Some("normal")` regardless of model.
  ///
  /// Spec: [`tests/docs/feature/026_subprocess_model_effort.md` FT-21]
  ///       [`tests/docs/cli/param/036_effort.md` EC-13]
  #[ test ]
  fn it_effort_normal_explicit()
  {
    let sonnet = claude_runner_core::IsolatedModel::Specific( "claude-sonnet-4-6".to_string() );
    let haiku  = claude_runner_core::IsolatedModel::Specific( "claude-haiku-4-5-20251001".to_string() );
    let keep   = claude_runner_core::IsolatedModel::KeepCurrent;
    assert_eq!( resolve_effort( &sonnet, SubprocessEffort::Normal ), Some( "normal" ), "effort::normal with sonnet must be normal" );
    assert_eq!( resolve_effort( &haiku,  SubprocessEffort::Normal ), Some( "normal" ), "effort::normal with haiku must be normal" );
    assert_eq!( resolve_effort( &keep,   SubprocessEffort::Normal ), Some( "normal" ), "effort::normal with keep must be normal" );
  }

  /// FT-19 / EC-14 (036): `imodel::haiku` + `effort::auto` → `None` (Haiku lacks extended thinking).
  ///
  /// Injecting `--effort` with Haiku would either have no effect or be rejected by the API.
  /// Haiku is the only concrete model that maps to `None` under `effort::auto`.
  ///
  /// Spec: [`tests/docs/feature/026_subprocess_model_effort.md` FT-19]
  ///       [`tests/docs/cli/param/036_effort.md` EC-14]
  #[ test ]
  fn it_imodel_haiku_effort_auto_no_effort_flag()
  {
    let haiku    = claude_runner_core::IsolatedModel::Specific( "claude-haiku-4-5-20251001".to_string() );
    let pre_args = effort_pre_args( &haiku, SubprocessEffort::Auto );
    assert!(
      pre_args.is_empty(),
      "imodel::haiku + effort::auto must produce no pre-args (no --effort flag), got: {pre_args:?}",
    );
  }
}