mcp-usage-core 0.5.0

Billing-attribution semantics for MCP 2026-07-28. No I/O, no async, no transport.
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
//! The billing decision: what a request/response pair is worth.
//!
//! # The rule
//!
//! **Charge on terminal delivery, never on request.**
//!
//! Every consequence a meter needs falls out of that one sentence, with no chain
//! correlation, no session, and no state:
//!
//! | Shape | Why it lands right |
//! |---|---|
//! | An MRTR chain of any length | Only its final round trip is `resultType: "complete"`, so it bills once |
//! | An abandoned chain | Never reaches `complete`, so it bills nothing |
//! | A task polled 300 times | Only the poll reporting `status: "completed"` delivered anything |
//! | A task that fails or is cancelled | Terminal, but delivered nothing, so nothing is owed |
//! | Discovery traffic | Never billable; it is the cost of connecting |
//! | A protocol error | The server failed; charging for that is indefensible |
//!
//! This replaced an earlier design that tried to collapse MRTR chains by
//! correlating their round trips. That design cannot be built by an intermediary:
//! `requestState` is "an opaque string meaningful only to the server", which
//! servers MUST integrity-protect with HMAC or AEAD, so the only correlator on
//! the wire is encrypted under a key the meter does not hold. Charging on
//! delivery needs no correlator, which is why it is both correct and cheaper.
//!
//! # Continuation state
//!
//! It does not consider whether the *request* was a continuation. Under the rule
//! above that fact is irrelevant to billing, and requiring it would force a body
//! peek on every `tools/call`. [`crate::peek::request`] still exposes it for the
//! cache, because MRTR-derived results MUST NOT be cached.
//!
//! # Replay
//!
//! Two double-billing vectors exist, and they are treated differently on purpose.
//!
//! A **terminal task can be polled forever.** Terminal state "does not change", so
//! every later `tasks/get` reports `completed` again. That is one unit of work
//! reported many times, so [`Billable::idempotency_key`] carries the `taskId` and
//! the caller MUST suppress repeats.
//!
//! A **replayed MRTR continuation** may produce a second `complete`. The spec
//! bounds the replay window but explicitly does not guarantee single use. We bill
//! it because the origin executed the tool a second time. Two completed executions
//! produce two charges.

use crate::peek::{ResponsePeek, ResultType};
use crate::price::PriceBook;
use crate::{Method, TaskStatus};

/// The call being priced, as known from its (trusted) headers.
///
/// `name` must already be decoded from the `Mcp-Name` sentinel form.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Call {
    /// MCP method whose delivered work is being priced.
    pub method: Method,
    /// Decoded `Mcp-Name`, when the method carries one.
    pub name: Option<String>,
}

impl Call {
    /// Construct a classified call from trusted, decoded headers.
    #[must_use]
    pub fn new(method: Method, name: Option<String>) -> Self {
        Self { method, name }
    }
}

/// Non-identifying method category retained for a durable task.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TaskOriginKind {
    /// A task created by `tools/call`.
    ToolsCall,
    /// A task created by `resources/read`.
    ResourcesRead,
    /// A task created by `prompts/get`.
    PromptsGet,
    /// A task created by an extension or unexpected method.
    Other,
}

impl TaskOriginKind {
    /// Reduce a method to a fixed category that cannot retain extension text.
    #[must_use]
    pub const fn from_method(method: &Method) -> Self {
        match method {
            Method::ToolsCall => Self::ToolsCall,
            Method::ResourcesRead => Self::ResourcesRead,
            Method::PromptsGet => Self::PromptsGet,
            _ => Self::Other,
        }
    }

    fn method(self) -> Method {
        match self {
            Self::ToolsCall => Method::ToolsCall,
            Self::ResourcesRead => Method::ResourcesRead,
            Self::PromptsGet => Method::PromptsGet,
            Self::Other => Method::Other("[redacted-task-origin]".to_owned()),
        }
    }
}

/// Pre-priced durable-task attribution without the original name or URI.
///
/// Construct this when a task is created, then persist it under the task ID.
/// The resolved units preserve named pricing while avoiding storage of a tool
/// name, prompt name, resource URI, or extension method string.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TaskAttribution {
    origin_kind: TaskOriginKind,
    units: u64,
}

impl TaskAttribution {
    /// Construct an attribution from an already resolved method category and price.
    #[must_use]
    pub const fn new(origin_kind: TaskOriginKind, units: u64) -> Self {
        Self { origin_kind, units }
    }

    /// Resolve a call against the current price book and discard identifying text.
    #[must_use]
    pub fn from_call(call: &Call, prices: &PriceBook) -> Self {
        Self {
            origin_kind: TaskOriginKind::from_method(&call.method),
            units: prices.units_for(&call.method, call.name.as_deref()),
        }
    }

    /// Fixed, non-identifying category of the call that created the task.
    #[must_use]
    pub const fn origin_kind(self) -> TaskOriginKind {
        self.origin_kind
    }

    /// Units resolved when the task was created.
    #[must_use]
    pub const fn units(self) -> u64 {
        self.units
    }
}

/// A charge to record against a tenant.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Billable {
    /// Method that originally commissioned the delivered work.
    pub method: Method,
    /// Decoded tool, prompt, or resource name when available.
    pub name: Option<String>,
    /// Integer units to record with the billing backend.
    pub units: u64,
    /// When present, the caller MUST NOT record this charge more than once for
    /// this key. Carried by task completions, whose terminal state can be
    /// re-observed by any number of later polls.
    pub idempotency_key: Option<String>,
}

/// Why a call was not billed. Kept specific so an operator answering "why is my
/// bill lower than my request count" gets a real answer from the metrics.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FreeReason {
    /// `tools/list` and friends: the cost of connecting, not of work.
    Discovery,
    /// An interim `input_required` result. Nothing delivered yet.
    InterimInputRequired,
    /// A `CreateTaskResult`: work accepted, not performed.
    TaskCreated,
    /// A poll of a task still `working` or awaiting input.
    TaskInProgress,
    /// A task that reached a terminal state without delivering: failed, cancelled.
    TaskNotDelivered,
    /// `tasks/update` or `tasks/cancel`: driving a task, not receiving its output.
    TaskDrive,
    /// A completed task could not be joined to the call that created it.
    MissingTaskAttribution,
    /// A completed task omitted its required durable identifier, so repeat
    /// polls could not be made idempotent.
    MissingTaskId,
    /// A `subscriptions/listen` stream. Not a discrete call; see the findings doc.
    Subscription,
    /// A JSON-RPC error. The server did not deliver.
    ProtocolError,
    /// A result shape this build does not recognize. Fails toward not charging.
    UnrecognizedResult,
}

impl FreeReason {
    /// Every reason, in a stable order.
    ///
    /// Callers index metric slots by position, so appending is safe and
    /// reordering is not.
    pub const ALL: [Self; 11] = [
        Self::Discovery,
        Self::InterimInputRequired,
        Self::TaskCreated,
        Self::TaskInProgress,
        Self::TaskNotDelivered,
        Self::TaskDrive,
        Self::MissingTaskAttribution,
        Self::MissingTaskId,
        Self::Subscription,
        Self::ProtocolError,
        Self::UnrecognizedResult,
    ];

    /// The stable wire name.
    ///
    /// One spelling per variant, defined here so metrics, bindings and the
    /// conformance vectors cannot drift into three different answers for the
    /// same reason. Written out rather than derived from the identifier: a
    /// rename must not silently change what an operator's dashboard or a
    /// caller's branch is keyed on.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Discovery => "discovery",
            Self::InterimInputRequired => "interim_input_required",
            Self::TaskCreated => "task_created",
            Self::TaskInProgress => "task_in_progress",
            Self::TaskNotDelivered => "task_not_delivered",
            Self::TaskDrive => "task_drive",
            Self::MissingTaskAttribution => "missing_task_attribution",
            Self::MissingTaskId => "missing_task_id",
            Self::Subscription => "subscription",
            Self::ProtocolError => "protocol_error",
            Self::UnrecognizedResult => "unrecognized_result",
        }
    }

    /// Position in [`Self::ALL`], for indexing a fixed array of counters.
    #[must_use]
    pub const fn index(self) -> usize {
        match self {
            Self::Discovery => 0,
            Self::InterimInputRequired => 1,
            Self::TaskCreated => 2,
            Self::TaskInProgress => 3,
            Self::TaskNotDelivered => 4,
            Self::TaskDrive => 5,
            Self::MissingTaskAttribution => 6,
            Self::MissingTaskId => 7,
            Self::Subscription => 8,
            Self::ProtocolError => 9,
            Self::UnrecognizedResult => 10,
        }
    }
}

/// The verdict.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Charge {
    /// Delivered work that should be recorded.
    Billable(Billable),
    /// An exchange for which no usage is owed.
    Free(FreeReason),
}

impl Charge {
    /// Units owed, or zero when free.
    #[must_use]
    pub fn units(&self) -> u64 {
        match self {
            Self::Billable(b) => b.units,
            Self::Free(_) => 0,
        }
    }

    /// Whether the verdict records delivered work (including zero-priced work).
    #[must_use]
    pub const fn is_billable(&self) -> bool {
        matches!(self, Self::Billable(_))
    }

    /// The idempotency key, when this charge carries one.
    #[must_use]
    pub fn idempotency_key(&self) -> Option<&str> {
        match self {
            Self::Billable(b) => b.idempotency_key.as_deref(),
            Self::Free(_) => None,
        }
    }
}

/// Decide what a completed request/response exchange is worth.
///
/// Ordering matters: errors are checked before anything else so a failure is
/// never billed regardless of which method produced it, and task handling
/// precedes the generic `resultType` arm because a task poll legitimately carries
/// `resultType: "complete"` while delivering nothing but a progress report.
#[must_use]
pub fn decide(call: &Call, response: &ResponsePeek, prices: &PriceBook) -> Charge {
    decide_with_task_origin(call, response, prices, None)
}

enum TaskPricing<'a> {
    Call(&'a Call),
    Attribution(&'a TaskAttribution),
}

/// Decide an exchange while supplying the call that created a polled task.
///
/// A `tasks/get` request carries no `Mcp-Name`, so its terminal response cannot
/// be priced correctly without the attribution captured from the original
/// `tools/call`. Callers must persist that association when they observe a
/// `resultType: "task"` response and pass it here on subsequent polls.
#[must_use]
pub fn decide_with_task_origin(
    call: &Call,
    response: &ResponsePeek,
    prices: &PriceBook,
    task_origin: Option<&Call>,
) -> Charge {
    decide_with_task_pricing(call, response, prices, task_origin.map(TaskPricing::Call))
}

/// Decide an exchange using pre-priced, non-identifying task attribution.
///
/// This is the privacy-preserving form for durable storage. The task's units
/// are resolved when it is created, so a later terminal poll does not need the
/// original name, URI, or extension method string.
#[must_use]
pub fn decide_with_task_attribution(
    call: &Call,
    response: &ResponsePeek,
    prices: &PriceBook,
    task_attribution: Option<&TaskAttribution>,
) -> Charge {
    decide_with_task_pricing(
        call,
        response,
        prices,
        task_attribution.map(TaskPricing::Attribution),
    )
}

fn decide_with_task_pricing(
    call: &Call,
    response: &ResponsePeek,
    prices: &PriceBook,
    task_pricing: Option<TaskPricing<'_>>,
) -> Charge {
    if response.is_error {
        return Charge::Free(FreeReason::ProtocolError);
    }

    if call.method.is_discovery() {
        return Charge::Free(FreeReason::Discovery);
    }

    if matches!(call.method, Method::SubscriptionsListen) {
        return Charge::Free(FreeReason::Subscription);
    }

    // A task handle accepts work but does not deliver it, regardless of the
    // seed status fields carried beside the discriminant.
    if matches!(response.result_type, ResultType::Task) {
        return Charge::Free(FreeReason::TaskCreated);
    }

    // Task lifecycle. A poll's own resultType says the poll succeeded, which says
    // nothing about the job, so the task status decides. The original call, not
    // `tasks/get`, owns the price and attribution.
    if matches!(call.method, Method::TasksGet)
        && let Some(task) = &response.task
    {
        return match &task.status {
            s if s.delivered() => {
                let Some(task_id) = task.task_id.clone() else {
                    return Charge::Free(FreeReason::MissingTaskId);
                };
                let Some(origin) = task_pricing else {
                    return Charge::Free(FreeReason::MissingTaskAttribution);
                };
                let (method, name, units) = match origin {
                    TaskPricing::Call(origin) => (
                        origin.method.clone(),
                        origin.name.clone(),
                        prices.units_for(&origin.method, origin.name.as_deref()),
                    ),
                    TaskPricing::Attribution(attribution) => {
                        (attribution.origin_kind.method(), None, attribution.units)
                    }
                };
                Charge::Billable(Billable {
                    method,
                    name,
                    units,
                    // A terminal task reports `completed` to every later poll.
                    idempotency_key: Some(task_id),
                })
            }
            s if s.is_terminal() => Charge::Free(FreeReason::TaskNotDelivered),
            TaskStatus::Working | TaskStatus::InputRequired => {
                Charge::Free(FreeReason::TaskInProgress)
            }
            _ => Charge::Free(FreeReason::TaskInProgress),
        };
    }

    if call.method.is_task_drive() {
        return Charge::Free(FreeReason::TaskDrive);
    }

    match &response.result_type {
        ResultType::Complete => Charge::Billable(Billable {
            method: call.method.clone(),
            name: call.name.clone(),
            units: prices.units_for(&call.method, call.name.as_deref()),
            idempotency_key: None,
        }),
        ResultType::InputRequired => Charge::Free(FreeReason::InterimInputRequired),
        ResultType::Task => Charge::Free(FreeReason::TaskCreated),
        ResultType::Unknown(_) | ResultType::Absent => Charge::Free(FreeReason::UnrecognizedResult),
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn every_reason_has_a_distinct_name_and_a_matching_index() {
        use super::FreeReason;
        let mut names: Vec<&str> = FreeReason::ALL.iter().map(|r| r.as_str()).collect();
        let total = names.len();
        names.sort_unstable();
        names.dedup();
        assert_eq!(names.len(), total, "two reasons share a wire name");

        for (position, reason) in FreeReason::ALL.iter().enumerate() {
            assert_eq!(
                reason.index(),
                position,
                "{} indexes a slot that is not its own",
                reason.as_str()
            );
        }
    }

    use super::*;
    use crate::peek::{TaskPeek, response as peek_response};
    use serde_json::json;

    fn call(method: Method, name: &str) -> Call {
        Call::new(method, Some(name.to_owned()))
    }

    fn book() -> PriceBook {
        PriceBook::flat(1)
    }

    fn complete() -> ResponsePeek {
        peek_response(&json!({ "result": { "resultType": "complete", "content": [] } }))
    }

    fn input_required() -> ResponsePeek {
        peek_response(&json!({
            "result": {
                "resultType": "input_required",
                "inputRequests": { "k": { "method": "elicitation/create" } },
                "requestState": "AEAD-protected blob"
            }
        }))
    }

    fn task_poll(status: &str, id: &str) -> ResponsePeek {
        peek_response(&json!({
            "result": { "resultType": "complete", "taskId": id, "status": status }
        }))
    }

    #[test]
    fn a_delivered_tool_call_bills_once() {
        let charge = decide(
            &call(Method::ToolsCall, "get_weather"),
            &complete(),
            &book(),
        );
        assert!(charge.is_billable());
        assert_eq!(charge.units(), 1);
        assert_eq!(charge.idempotency_key(), None);
    }

    #[test]
    fn an_mrtr_chain_of_any_length_bills_exactly_once() {
        // The property that motivated the whole crate. Rounds 1..n-1 are interim;
        // only the last one delivers.
        for rounds in 1..=10 {
            let c = call(Method::ToolsCall, "get_weather");
            let mut total = 0;
            for round in 1..=rounds {
                let response = if round == rounds {
                    complete()
                } else {
                    input_required()
                };
                total += decide(&c, &response, &book()).units();
            }
            assert_eq!(total, 1, "a {rounds}-round chain billed {total} units");
        }
    }

    #[test]
    fn an_abandoned_chain_bills_nothing() {
        let c = call(Method::ToolsCall, "get_weather");
        let total: u64 = (0..5)
            .map(|_| decide(&c, &input_required(), &book()).units())
            .sum();
        assert_eq!(total, 0);
    }

    #[test]
    fn mrtr_applies_to_resources_read_and_prompts_get_too() {
        for method in [Method::ResourcesRead, Method::PromptsGet] {
            let c = call(method.clone(), "x");
            assert_eq!(decide(&c, &input_required(), &book()).units(), 0);
            assert_eq!(decide(&c, &complete(), &book()).units(), 1);
        }
    }

    #[test]
    fn a_task_polled_many_times_bills_once() {
        let poll = Call::new(Method::TasksGet, None);
        let origin = call(Method::ToolsCall, "long_job");
        let mut total = 0;
        let mut billed_keys: Vec<String> = Vec::new();

        // 300 progress polls, then the completion.
        for _ in 0..300 {
            total += decide_with_task_origin(
                &poll,
                &task_poll("working", "tsk_1"),
                &book(),
                Some(&origin),
            )
            .units();
        }
        let final_charge = decide_with_task_origin(
            &poll,
            &task_poll("completed", "tsk_1"),
            &book(),
            Some(&origin),
        );
        if let Some(key) = final_charge.idempotency_key() {
            billed_keys.push(key.to_owned());
        }
        total += final_charge.units();

        assert_eq!(total, 1, "300 polls plus a completion billed {total} units");
        assert_eq!(billed_keys, vec!["tsk_1".to_owned()]);
    }

    #[test]
    fn a_completed_task_carries_its_id_so_repeat_polls_can_be_suppressed() {
        // Terminal state does not change, so nothing stops a client polling a
        // finished task forever. Core flags it; the caller enforces once-only.
        let poll = Call::new(Method::TasksGet, None);
        let origin = call(Method::ToolsCall, "long_job");
        let first = decide_with_task_origin(
            &poll,
            &task_poll("completed", "tsk_9"),
            &book(),
            Some(&origin),
        );
        let second = decide_with_task_origin(
            &poll,
            &task_poll("completed", "tsk_9"),
            &book(),
            Some(&origin),
        );

        assert!(first.is_billable() && second.is_billable());
        assert_eq!(first.idempotency_key(), Some("tsk_9"));
        assert_eq!(first.idempotency_key(), second.idempotency_key());
    }

    #[test]
    fn failed_and_cancelled_tasks_are_free() {
        let c = Call::new(Method::TasksGet, None);
        assert_eq!(
            decide(&c, &task_poll("failed", "t"), &book()),
            Charge::Free(FreeReason::TaskNotDelivered)
        );
        assert_eq!(
            decide(&c, &task_poll("cancelled", "t"), &book()),
            Charge::Free(FreeReason::TaskNotDelivered)
        );
    }

    #[test]
    fn a_task_awaiting_input_is_free() {
        let c = Call::new(Method::TasksGet, None);
        assert_eq!(
            decide(&c, &task_poll("input_required", "t"), &book()),
            Charge::Free(FreeReason::TaskInProgress)
        );
    }

    #[test]
    fn creating_a_task_is_not_delivering_it() {
        let created = peek_response(&json!({
            "result": { "resultType": "task", "taskId": "tsk_1", "status": "working" }
        }));
        assert_eq!(
            decide(&call(Method::ToolsCall, "t"), &created, &book()),
            Charge::Free(FreeReason::TaskCreated)
        );
    }

    #[test]
    fn task_drive_calls_are_free() {
        let ack = peek_response(&json!({ "result": { "resultType": "complete" } }));
        for method in [Method::TasksUpdate, Method::TasksCancel] {
            assert_eq!(
                decide(&Call::new(method, None), &ack, &book()),
                Charge::Free(FreeReason::TaskDrive)
            );
        }
    }

    #[test]
    fn discovery_is_never_billed() {
        for method in [
            Method::ToolsList,
            Method::PromptsList,
            Method::ResourcesList,
            Method::ResourcesTemplatesList,
            Method::ServerDiscover,
        ] {
            assert_eq!(
                decide(&Call::new(method, None), &complete(), &book()),
                Charge::Free(FreeReason::Discovery)
            );
        }
    }

    #[test]
    fn subscription_streams_are_free() {
        assert_eq!(
            decide(
                &Call::new(Method::SubscriptionsListen, None),
                &complete(),
                &book()
            ),
            Charge::Free(FreeReason::Subscription)
        );
    }

    #[test]
    fn errors_are_never_billed_whatever_the_method() {
        let err = peek_response(&json!({ "error": { "code": -32020, "message": "mismatch" } }));
        for method in [Method::ToolsCall, Method::TasksGet, Method::ResourcesRead] {
            assert_eq!(
                decide(&call(method, "x"), &err, &book()),
                Charge::Free(FreeReason::ProtocolError)
            );
        }
    }

    #[test]
    fn unrecognized_results_fail_toward_not_charging() {
        let weird = peek_response(&json!({ "result": { "resultType": "some_future_shape" } }));
        assert_eq!(
            decide(&call(Method::ToolsCall, "t"), &weird, &book()),
            Charge::Free(FreeReason::UnrecognizedResult)
        );

        let empty = peek_response(&json!({ "jsonrpc": "2.0" }));
        assert_eq!(
            decide(&call(Method::ToolsCall, "t"), &empty, &book()),
            Charge::Free(FreeReason::UnrecognizedResult)
        );
    }

    #[test]
    fn price_book_drives_the_units() {
        let prices = PriceBook::flat(1).with_name("expensive", 250);
        assert_eq!(
            decide(&call(Method::ToolsCall, "expensive"), &complete(), &prices).units(),
            250
        );
        assert_eq!(
            decide(&call(Method::ToolsCall, "cheap"), &complete(), &prices).units(),
            1
        );
    }

    #[test]
    fn a_zero_priced_tool_still_reports_as_billable_with_zero_units() {
        // Distinguishing "free by price" from "free by protocol shape" keeps the
        // metrics honest about why a call cost nothing.
        let prices = PriceBook::flat(5).with_name("loss_leader", 0);
        let charge = decide(
            &call(Method::ToolsCall, "loss_leader"),
            &complete(),
            &prices,
        );
        assert!(charge.is_billable());
        assert_eq!(charge.units(), 0);
    }

    #[test]
    fn an_unknown_task_status_is_not_billed() {
        let paused = peek_response(&json!({
            "result": { "resultType": "complete", "taskId": "t", "status": "paused" }
        }));
        assert!(!decide(&Call::new(Method::TasksGet, None), &paused, &book()).is_billable());
    }

    #[test]
    fn a_task_without_an_id_fails_toward_not_charging() {
        let peek = ResponsePeek {
            result_type: ResultType::Complete,
            is_error: false,
            task: Some(TaskPeek {
                task_id: None,
                status: TaskStatus::Completed,
            }),
        };
        let charge = decide_with_task_origin(
            &Call::new(Method::TasksGet, None),
            &peek,
            &book(),
            Some(&call(Method::ToolsCall, "j")),
        );
        assert_eq!(charge, Charge::Free(FreeReason::MissingTaskId));
    }

    #[test]
    fn a_completed_task_without_origin_fails_toward_not_charging() {
        let charge = decide(
            &Call::new(Method::TasksGet, None),
            &task_poll("completed", "tsk_orphan"),
            &book(),
        );
        assert_eq!(charge, Charge::Free(FreeReason::MissingTaskAttribution));
    }

    #[test]
    fn a_completed_task_uses_the_originating_tool_price() {
        let prices = PriceBook::flat(1).with_name("expensive_job", 250);
        let charge = decide_with_task_origin(
            &Call::new(Method::TasksGet, None),
            &task_poll("completed", "tsk_expensive"),
            &prices,
            Some(&call(Method::ToolsCall, "expensive_job")),
        );
        assert_eq!(charge.units(), 250);
        let Charge::Billable(billable) = charge else {
            panic!("completed task should be billable");
        };
        assert_eq!(billable.method, Method::ToolsCall);
        assert_eq!(billable.name.as_deref(), Some("expensive_job"));
    }

    #[test]
    fn prepriced_task_attribution_discards_identifying_names() {
        let prices = PriceBook::flat(1).with_name("file:///private/customer-record", 250);
        let origin = Call::new(
            Method::ResourcesRead,
            Some("file:///private/customer-record".to_owned()),
        );
        let attribution = TaskAttribution::from_call(&origin, &prices);

        assert_eq!(attribution.units(), 250);
        assert_eq!(attribution.origin_kind(), TaskOriginKind::ResourcesRead);
        assert!(!format!("{attribution:?}").contains("customer-record"));

        let charge = decide_with_task_attribution(
            &Call::new(Method::TasksGet, None),
            &task_poll("completed", "tsk_private"),
            &prices,
            Some(&attribution),
        );
        let Charge::Billable(billable) = charge else {
            panic!("completed task should be billable");
        };
        assert_eq!(billable.units, 250);
        assert_eq!(billable.method, Method::ResourcesRead);
        assert_eq!(billable.name, None);
    }
}