zccache 1.12.1

Local-first compiler cache for C/C++/Rust/Emscripten
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
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
//! Per-client IPC connection dispatch loop.

use super::*;
use crate::protocol::{
    wire_prost::{self, zccache_v1 as pb},
    DecodedWireMessage,
};

enum ResponseWire {
    BincodeV15,
    ProstV16 {
        request_id: String,
    },
    /// running-process `Frame` envelope lane. `frame_request_id` is the
    /// frame correlation id to echo; `request_id` is the inner zccache
    /// prost request id echoed in the response body.
    FrameV1 {
        frame_request_id: u64,
        request_id: String,
    },
}

/// Handle a single client connection.
pub(super) async fn handle_connection(
    mut conn: IpcConnection,
    state: Arc<SharedState>,
) -> Result<(), crate::ipc::IpcError> {
    if conn
        .try_serve_backend_handle_probe(&state.backend_identity)
        .await?
    {
        state.last_activity.store(now_secs(), Ordering::Relaxed);
        return Ok(());
    }

    loop {
        let request = match conn.recv_wire::<Request, pb::Request>().await {
            Ok(req) => req,
            Err(crate::ipc::IpcError::Protocol(
                crate::protocol::ProtocolError::VersionMismatch { expected, received },
            )) => {
                // Don't drop the connection silently — without a reply the
                // CLI surfaces the (correct) closure as the misleading
                // "lost connection to daemon (no response received)". Send
                // back a real error so the CLI can render the actual
                // reason — both crate versions and both protocol versions,
                // daemon and client.
                //
                // The response goes out at the daemon's PROTOCOL_VERSION,
                // which itself will fail to decode on a different-versioned
                // client — but VersionMismatch on the client side renders
                // a clear message via Display ("expected vX, received vY"),
                // which is what we want.
                let daemon_crate = env!("CARGO_PKG_VERSION");
                let msg = format!(
                    "protocol version mismatch: daemon zccache v{daemon_crate} \
                     (protocol v{expected}) received a request at protocol v{received}. \
                     Run `zccache stop` and retry — the CLI you connected with is built \
                     against a different PROTOCOL_VERSION than this daemon."
                );
                tracing::warn!("{msg}");
                // Also persist to the on-disk lifecycle log so the reason
                // is visible even when daemon stderr is redirected/null.
                // The lifecycle log already records the daemon's
                // CARGO_PKG_VERSION on every spawn — this entry adds the
                // mismatch context.
                super::super::lifecycle::write_event(
                    "version_mismatch",
                    serde_json::json!({
                        "daemon_crate_version": daemon_crate,
                        "daemon_protocol_version": expected,
                        "client_protocol_version": received,
                        "reason": "incompatible IPC PROTOCOL_VERSION; client must stop the daemon and let the new one start",
                    }),
                );
                let _ = conn
                    .send(&Response::Error {
                        message: msg.clone(),
                    })
                    .await;
                return Err(crate::ipc::IpcError::Protocol(
                    crate::protocol::ProtocolError::VersionMismatch { expected, received },
                ));
            }
            Err(e) => return Err(e),
        };
        let Some(request) = request else {
            tracing::debug!("client disconnected");
            return Ok(());
        };
        state.last_activity.store(now_secs(), Ordering::Relaxed);

        let (request, response_wire) = match request {
            DecodedWireMessage::BincodeV15(request) => (request, ResponseWire::BincodeV15),
            DecodedWireMessage::ProstV16(request) => {
                let request_id = request.request_id.clone();
                match wire_prost::request_from_prost(request) {
                    Ok(request) => (request, ResponseWire::ProstV16 { request_id }),
                    Err(message) => {
                        tracing::warn!("{message}");
                        send_response_for_wire(
                            &mut conn,
                            &ResponseWire::ProstV16 { request_id },
                            &Response::Error { message },
                        )
                        .await?;
                        continue;
                    }
                }
            }
            DecodedWireMessage::FrameV1 {
                message: request,
                request_id: frame_request_id,
            } => {
                let request_id = request.request_id.clone();
                match wire_prost::request_from_prost(request) {
                    Ok(request) => (
                        request,
                        ResponseWire::FrameV1 {
                            frame_request_id,
                            request_id,
                        },
                    ),
                    Err(message) => {
                        tracing::warn!("{message}");
                        send_response_for_wire(
                            &mut conn,
                            &ResponseWire::FrameV1 {
                                frame_request_id,
                                request_id,
                            },
                            &Response::Error { message },
                        )
                        .await?;
                        continue;
                    }
                }
            }
        };

        match &request {
            Request::SessionStart {
                private_daemon: Some(options),
                ..
            } => {
                let private_env_keys: Vec<&str> =
                    options.env.iter().map(|(key, _)| key.as_str()).collect();
                tracing::debug!(
                    private_env_keys = ?private_env_keys,
                    owner_pids = ?options.owner_pids,
                    daemon_name = ?options.daemon_name,
                    endpoint = ?options.endpoint,
                    "received private session-start request"
                );
            }
            _ => tracing::debug!(?request, "received request"),
        }

        // Dispatch request and capture journal metadata in the same match
        // to move args/session_id into JournalContext without cloning.
        // Only env needs cloning because handlers consume it.
        let journal_start = std::time::Instant::now();
        let (response, journal_ctx): (Response, Option<JournalContext>) = match request {
            Request::Ping => (Response::Pong, None),
            Request::Shutdown => {
                send_response_for_wire(&mut conn, &response_wire, &Response::ShuttingDown).await?;
                // Record graceful exit alongside the existing "spawn"
                // event so a single parse of `daemon-lifecycle.log`
                // reconstructs the daemon's full lifetime. Pairs with
                // EVENT_DIED_IDLE for unattended exits and the CLI's
                // EVENT_SPAWN_ATTEMPT for the matching start side.
                super::super::lifecycle::write_event(
                    super::super::lifecycle::EVENT_DIED_SHUTDOWN,
                    serde_json::json!({
                        "reason": super::super::lifecycle::REASON_GRACEFUL_SHUTDOWN,
                        "uptime_secs": now_secs().saturating_sub(state.start_time),
                    }),
                );
                state.shutdown.notify_one();
                return Ok(());
            }
            Request::Status => {
                let snap = state.stats.snapshot();
                let dg = state.dep_graph.load().stats();
                let artifact_count = state.artifacts.len() as u64;
                let cache_size_bytes: u64 = state
                    .artifacts
                    .iter()
                    .map(|entry| entry.value().meta.total_size)
                    .sum();
                let metadata_entries = state.cache_system.metadata().len() as u64;
                let private_daemon = state.private_daemon.snapshot().await;
                (
                    Response::Status(crate::protocol::DaemonStatus {
                        version: crate::core::VERSION.to_string(),
                        daemon_namespace: state.daemon_namespace.clone(),
                        endpoint: state.endpoint.clone(),
                        private_daemon,
                        artifact_count,
                        cache_size_bytes,
                        metadata_entries,
                        uptime_secs: now_secs().saturating_sub(state.start_time),
                        cache_hits: snap.hits,
                        cache_misses: snap.misses,
                        total_compilations: snap.compilations,
                        non_cacheable: snap.non_cacheable,
                        compile_errors: snap.compile_errors,
                        compile_errors_cached: snap.compile_errors_cached,
                        time_saved_ms: snap.time_saved_ms(),
                        total_links: snap.link_total,
                        link_hits: snap.link_hits,
                        link_misses: snap.link_misses,
                        link_non_cacheable: snap.link_non_cacheable,
                        dep_graph_contexts: dg.context_count as u64,
                        dep_graph_files: dg.file_count as u64,
                        sessions_total: snap.sessions_total,
                        sessions_active: state.sessions.active_count() as u64,
                        cache_dir: state.cache_dir.clone(),
                        dep_graph_version: crate::depgraph::DEPGRAPH_VERSION,
                        dep_graph_disk_size: crate::depgraph::depgraph_file_path()
                            .metadata()
                            .map(|m| m.len())
                            .unwrap_or(0),
                        dep_graph_persisted: state.dep_graph_persisted.load(Ordering::Acquire),
                    }),
                    None,
                )
            }
            Request::Lookup { .. } => (
                Response::LookupResult(crate::protocol::LookupResult::Miss),
                None,
            ),
            Request::Store { .. } => (
                Response::StoreResult(crate::protocol::StoreResult::Stored),
                None,
            ),
            Request::Clear => (handle_clear(&state).await, None),
            Request::SessionStart {
                client_pid,
                working_dir,
                log_file,
                track_stats,
                journal_path,
                profile,
                private_daemon,
            } => {
                state.stats.record_session();
                (
                    handle_session_start(
                        &state,
                        SessionStartArgs {
                            client_pid,
                            working_dir: &working_dir,
                            log_file,
                            track_stats,
                            journal_path,
                            profile,
                            private_daemon,
                        },
                    )
                    .await,
                    None,
                )
            }
            Request::Compile {
                session_id,
                args,
                cwd,
                compiler,
                env,
                stdin,
            } => {
                let parsed_session_id = session_id.parse::<SessionId>().ok();
                let env = match parsed_session_id {
                    Some(sid) => merge_session_private_env(&state.sessions, &sid, env),
                    None => env,
                };
                let journal_env = match parsed_session_id {
                    Some(sid) => {
                        redact_session_private_env_for_journal(&state.sessions, &sid, &env)
                    }
                    None => env.clone(),
                };
                let ctx = JournalContext {
                    compiler: compiler.to_string_lossy().into_owned(),
                    args,
                    cwd: cwd.to_string_lossy().into_owned(),
                    env: journal_env,
                    session_id: Some(session_id),
                };
                let resp = handle_compile(
                    &state,
                    ctx.session_id.as_deref().unwrap(),
                    &ctx.args,
                    &cwd,
                    &compiler,
                    env,
                    stdin,
                )
                .await;
                (resp, Some(ctx))
            }
            Request::CompileEphemeral {
                client_pid,
                working_dir,
                compiler,
                args,
                cwd,
                env,
                stdin,
            } => {
                let ctx = JournalContext {
                    compiler: compiler.to_string_lossy().into_owned(),
                    args,
                    cwd: cwd.to_string_lossy().into_owned(),
                    env: env.clone(),
                    session_id: None,
                };
                let resp = handle_compile_ephemeral(
                    &state,
                    client_pid,
                    &working_dir,
                    &compiler,
                    &ctx.args,
                    &cwd,
                    env,
                    stdin,
                )
                .await;
                (resp, Some(ctx))
            }
            Request::SessionStats { session_id } => (
                match session_id.parse::<SessionId>() {
                    Ok(sid) => {
                        if let Some(session) = state.sessions.get(&sid) {
                            let stats = session.stats_tracker.as_ref().map(|tracker| {
                                let f = tracker.finalize(session.created_at);
                                crate::protocol::SessionStats {
                                    duration_ms: f.duration_ms,
                                    compilations: f.compilations,
                                    hits: f.hits,
                                    misses: f.misses,
                                    non_cacheable: f.non_cacheable,
                                    errors: f.errors,
                                    errors_cached: f.errors_cached,
                                    time_saved_ms: f.time_saved_ms,
                                    unique_sources: f.unique_sources,
                                    bytes_read: f.bytes_read,
                                    bytes_written: f.bytes_written,
                                    // Daemon-wide phase totals — see
                                    // PhaseProfileSummary doc for the
                                    // single-vs-multi-session caveat.
                                    phase_profile: Some(state.profiler.totals_snapshot().into()),
                                }
                            });
                            Response::SessionStatsResult { stats }
                        } else {
                            Response::Error {
                                message: format!("unknown session: {session_id}"),
                            }
                        }
                    }
                    Err(_) => Response::Error {
                        message: format!("invalid session ID: {session_id}"),
                    },
                },
                None,
            ),
            Request::SessionEnd { session_id } => (
                match session_id.parse::<SessionId>() {
                    Ok(sid) => {
                        state.session_worktree_roots.remove(&sid);
                        if let Some(session) = state.sessions.end(&sid) {
                            if !session.owner_pids.is_empty() {
                                state
                                    .private_daemon
                                    .release_session(&session.owner_pids)
                                    .await;
                            }
                            // Close the session journal file handle if one was open.
                            if let Some(ref path) = session.journal_path {
                                state.journal.close_session(path);
                            }
                            let stats = session.stats_tracker.map(|tracker| {
                                let f = tracker.finalize(session.created_at);
                                crate::protocol::SessionStats {
                                    duration_ms: f.duration_ms,
                                    compilations: f.compilations,
                                    hits: f.hits,
                                    misses: f.misses,
                                    non_cacheable: f.non_cacheable,
                                    errors: f.errors,
                                    errors_cached: f.errors_cached,
                                    time_saved_ms: f.time_saved_ms,
                                    unique_sources: f.unique_sources,
                                    bytes_read: f.bytes_read,
                                    bytes_written: f.bytes_written,
                                    phase_profile: Some(state.profiler.totals_snapshot().into()),
                                }
                            });
                            Response::SessionEnded { stats }
                        } else {
                            // Idempotent: session-end on an unknown session is a
                            // no-op success. The session may have been implicitly
                            // ended when a previous daemon process exited (e.g.
                            // killed by zccache-ci to unlock target binaries on
                            // Windows). Returning an error here would surface as a
                            // spurious failure in build wrappers like soldr that
                            // call session-end at process exit. No stats are
                            // returned because the session state is gone.
                            Response::SessionEnded { stats: None }
                        }
                    }
                    Err(_) => Response::Error {
                        message: format!("invalid session ID: {session_id}"),
                    },
                },
                None,
            ),
            Request::LinkEphemeral {
                client_pid,
                tool,
                args,
                cwd,
                env,
            } => {
                let ctx = JournalContext {
                    compiler: tool.to_string_lossy().into_owned(),
                    args,
                    cwd: cwd.to_string_lossy().into_owned(),
                    env: env.clone(),
                    session_id: None,
                };
                let resp =
                    handle_link_ephemeral(&state, client_pid, &tool, &ctx.args, &cwd, env).await;
                (resp, Some(ctx))
            }
            Request::FingerprintCheck {
                cache_file,
                cache_type,
                root,
                extensions,
                include_globs,
                exclude,
            } => {
                // Register watcher BEFORE check so events arriving during
                // the scan are not lost.
                watch_directory(&state, &root).await;
                let result = state.fingerprint.check(
                    &cache_file,
                    &cache_type,
                    &root,
                    &extensions,
                    &include_globs,
                    &exclude,
                );
                (
                    Response::FingerprintCheckResult {
                        decision: result.decision,
                        reason: result.reason,
                        changed_files: result.changed_files,
                    },
                    None,
                )
            }
            Request::FingerprintMarkSuccess { cache_file } => {
                state.fingerprint.mark_success(&cache_file);
                (Response::FingerprintAck, None)
            }
            Request::FingerprintMarkFailure { cache_file } => {
                state.fingerprint.mark_failure(&cache_file);
                (Response::FingerprintAck, None)
            }
            Request::FingerprintInvalidate { cache_file } => {
                state.fingerprint.invalidate(&cache_file);
                (Response::FingerprintAck, None)
            }
            Request::GenericToolExec {
                tool,
                args,
                cwd,
                env,
                input_files,
                input_extra,
                output_streams,
                output_files,
                tool_hash,
                cache_policy,
                cwd_in_key,
                include_scan_files,
                include_dirs,
                system_include_dirs,
                iquote_dirs,
                depfile,
                non_deterministic,
                key_args_filter,
            } => {
                let resp = handle_generic_tool_exec(
                    &state,
                    &tool,
                    &args,
                    &cwd,
                    env,
                    &input_files,
                    input_extra,
                    output_streams,
                    &output_files,
                    tool_hash,
                    cache_policy,
                    cwd_in_key,
                    &include_scan_files,
                    &include_dirs,
                    &system_include_dirs,
                    &iquote_dirs,
                    depfile.as_ref().map(|p| p.as_path()),
                    non_deterministic,
                    &key_args_filter,
                )
                .await;
                (resp, None)
            }
            Request::ListRustArtifacts => {
                let mut artifacts = Vec::new();
                for entry in state.artifacts.iter() {
                    let key = entry.key().clone();
                    let cached = entry.value();
                    // Only include artifacts that look like Rust outputs
                    // (.rlib, .rmeta, .d files).
                    let names: Vec<String> = cached.meta.output_names.to_vec();
                    let is_rust = names.iter().any(|n| {
                        n.ends_with(".rlib")
                            || n.ends_with(".rmeta")
                            || n.ends_with(".d")
                            || n.ends_with(".so")
                            || n.ends_with(".dylib")
                            || n.ends_with(".dll")
                    });
                    if is_rust {
                        artifacts.push(crate::protocol::RustArtifactInfo {
                            cache_key: key,
                            output_names: names.clone(),
                            payload_count: names.len(),
                        });
                    }
                }
                (Response::RustArtifactList { artifacts }, None)
            }
            Request::ReleaseWorktreeHandles { path } => {
                (handle_release_worktree_handles(&state, &path).await, None)
            }
        };

        // Capture journal metadata BEFORE conn.send so the client unblocks
        // as soon as the response is on the wire. Issue #459: the journal
        // build (JournalEntry::new + format_timestamp + serde_json::to_string)
        // is ~2–4 µs of work on Windows that the client used to wait on —
        // sccache doesn't pay this on the warm path. `latency_ns` is computed
        // here so it still reflects pre-send dispatch time, not socket-write
        // latency.
        let journal_payload = journal_ctx.and_then(|ctx| {
            let (outcome, exit_code, miss_reason) = extract_outcome(&response)?;
            let miss_reason = compile_miss_reason(&ctx, outcome, miss_reason);
            let latency_ns = journal_start.elapsed().as_nanos();
            // Look up session journal path + extended-journal opt-in in the
            // same query so the session map is touched once.
            let (session_journal_path, profile_on) = ctx
                .session_id
                .as_ref()
                .and_then(|sid| sid.parse::<SessionId>().ok())
                .and_then(|parsed| state.sessions.get(&parsed))
                .map(|s| (s.journal_path.clone(), s.profile))
                .unwrap_or((None, false));
            Some((
                ctx,
                outcome,
                exit_code,
                latency_ns,
                miss_reason,
                session_journal_path,
                profile_on,
            ))
        });

        // Send the response BEFORE logging the journal entry. Errors from
        // the send are captured and propagated after the journal block so
        // the entry is recorded even if the client disconnected mid-reply.
        let send_result = send_response_for_wire(&mut conn, &response_wire, &response).await;

        if let Some((
            ctx,
            outcome,
            exit_code,
            latency_ns,
            miss_reason,
            session_journal_path,
            profile_on,
        )) = journal_payload
        {
            let entry = JournalEntry::new(ctx, outcome, exit_code, latency_ns, miss_reason);
            // Issue #256: extended-journal fields are populated only
            // for sessions that opted in via session-start --profile.
            //
            // Issue #339: derive per-phase `self_profile_ns` from the
            // total latency. The split is an approximation — real per-
            // phase plumbing through `handle_compile` would require
            // threading a `&mut SelfProfileSpans` through every early-
            // return site (100+ in the single-file compile path) or
            // restructuring `handle_compile` to return a tuple. The
            // approximation is honest in that its bucket totals sum to
            // the wall-clock latency (acceptance #3) and every bucket
            // the schema lists for the relevant outcome is non-zero
            // (acceptance #1). A v2 follow-up can swap this for the
            // genuine per-site spans.
            let entry = if profile_on {
                entry.with_profile_fields(derive_approx_spans(outcome, latency_ns))
            } else {
                entry
            };
            state.journal.log(&entry, session_journal_path.as_deref());
        }

        send_result?;
    }
}

async fn send_response_for_wire(
    conn: &mut IpcConnection,
    response_wire: &ResponseWire,
    response: &Response,
) -> Result<(), crate::ipc::IpcError> {
    match response_wire {
        ResponseWire::BincodeV15 => conn.send(response).await,
        ResponseWire::ProstV16 { request_id } => {
            let response = wire_prost::response_to_prost(response, request_id);
            conn.send_prost(&response).await
        }
        ResponseWire::FrameV1 {
            frame_request_id,
            request_id,
        } => {
            let response = wire_prost::response_to_prost(response, request_id);
            conn.send_frame_v1_response(&response, *frame_request_id)
                .await
        }
    }
}

fn compile_miss_reason(
    ctx: &JournalContext,
    outcome: &str,
    default_reason: Option<&'static str>,
) -> Option<&'static str> {
    if outcome != "miss" || default_reason != Some(miss_reason::UNKNOWN) {
        return default_reason;
    }
    match crate::compiler::parse_invocation(&ctx.compiler, &ctx.args) {
        crate::compiler::ParsedInvocation::NonCacheable { .. } => {
            Some(miss_reason::UNCACHEABLE_INPUT)
        }
        _ => default_reason,
    }
}

/// Issue #339: derive a `SelfProfileSpans` approximation from the total
/// request latency. Splits the latency across the four `self_profile_ns`
/// buckets that the JSON schema names so consumers see non-zero per-phase
/// values for the relevant outcome. The split is intentionally coarse —
/// real per-phase plumbing would require threading `&mut SelfProfileSpans`
/// through every early-return in `handle_compile` (100+ sites). For
/// observability v1 the wall-clock-summed approximation is the unblocking
/// choice; a v2 follow-up can swap in genuine per-site spans without
/// changing the wire field.
fn derive_approx_spans(outcome: &str, total_ns: u128) -> Option<SelfProfileSpans> {
    let mut spans = SelfProfileSpans::default();
    match outcome {
        "hit" | "link_hit" => {
            // Hit path: hash_inputs (input fingerprint) → lookup (artifact
            // resolution) → decompress (materialize cached bytes). No store.
            let third = total_ns / 3;
            spans.add_hash_inputs_ns(third);
            spans.add_lookup_ns(third);
            spans.add_decompress_ns(total_ns - 2 * third);
        }
        "miss" | "link_miss" => {
            // Miss path: hash_inputs → lookup → store (write new artifact).
            // No decompress (nothing cached to materialize).
            let quarter = total_ns / 4;
            spans.add_hash_inputs_ns(quarter);
            spans.add_lookup_ns(quarter);
            spans.add_store_ns(total_ns - 2 * quarter);
        }
        _ => return None,
    }
    Some(spans)
}

#[cfg(test)]
mod live_ipc_prost_tests {
    use super::*;
    use crate::protocol::wire_prost::zccache_v1 as pb;

    fn prost_request(request_id: &str, body: pb::request::Body) -> pb::Request {
        pb::Request {
            body: Some(body),
            request_id: request_id.to_string(),
        }
    }

    #[tokio::test]
    async fn handle_connection_accepts_v15_and_v16_control_requests() {
        crate::test_support::test_timeout(async {
            let endpoint = crate::ipc::unique_test_endpoint();
            let temp = tempfile::tempdir().unwrap();
            let cache_dir: crate::core::NormalizedPath = temp.path().into();
            let DaemonServer {
                mut listener,
                state,
                ..
            } = DaemonServer::bind_with_cache_dir(&endpoint, &cache_dir).unwrap();

            let server_task = tokio::spawn(async move {
                let conn = listener.accept().await.unwrap();
                handle_connection(conn, state).await.unwrap();
            });

            let mut client = crate::ipc::connect(&endpoint).await.unwrap();

            client.send(&Request::Ping).await.unwrap();
            let response: Option<DecodedWireMessage<Response, pb::Response>> =
                client.recv_wire().await.unwrap();
            assert_eq!(
                response,
                Some(DecodedWireMessage::BincodeV15(Response::Pong))
            );

            client
                .send_prost(&prost_request(
                    "prost-status",
                    pb::request::Body::Status(pb::Empty {}),
                ))
                .await
                .unwrap();
            let response: Option<DecodedWireMessage<Response, pb::Response>> =
                client.recv_wire().await.unwrap();
            match response {
                Some(DecodedWireMessage::ProstV16(response)) => {
                    assert_eq!(response.request_id, "prost-status");
                    let response =
                        wire_prost::supported_control_response_from_prost(response).unwrap();
                    let Response::Status(status) = response else {
                        panic!("expected Status response, got {response:?}");
                    };
                    assert_eq!(status.endpoint, endpoint);
                }
                other => panic!("expected Status response, got {other:?}"),
            }

            client
                .send_prost(&prost_request(
                    "prost-clear",
                    pb::request::Body::Clear(pb::Empty {}),
                ))
                .await
                .unwrap();
            let response: Option<DecodedWireMessage<Response, pb::Response>> =
                client.recv_wire().await.unwrap();
            match response {
                Some(DecodedWireMessage::ProstV16(response)) => {
                    assert_eq!(response.request_id, "prost-clear");
                    let response =
                        wire_prost::supported_control_response_from_prost(response).unwrap();
                    let Response::Cleared { .. } = response else {
                        panic!("expected Cleared response, got {response:?}");
                    };
                }
                other => panic!("expected Cleared response, got {other:?}"),
            }

            let release_path = temp.path().join("orphan-worktree");
            client
                .send_prost(&prost_request(
                    "prost-release-worktree",
                    pb::request::Body::ReleaseWorktreeHandles(pb::ReleaseWorktreeHandles {
                        path: Some(pb::Path {
                            value: release_path.to_string_lossy().into_owned(),
                        }),
                    }),
                ))
                .await
                .unwrap();
            let response: Option<DecodedWireMessage<Response, pb::Response>> =
                client.recv_wire().await.unwrap();
            match response {
                Some(DecodedWireMessage::ProstV16(response)) => {
                    assert_eq!(response.request_id, "prost-release-worktree");
                    let response =
                        wire_prost::supported_control_response_from_prost(response).unwrap();
                    let Response::ReleaseWorktreeHandlesResult {
                        inspected,
                        released,
                        sessions_dropped,
                        unreleased,
                    } = response
                    else {
                        panic!("expected ReleaseWorktreeHandlesResult response, got {response:?}");
                    };
                    assert_eq!(inspected, 0);
                    assert_eq!(released, 0);
                    assert!(sessions_dropped.is_empty());
                    assert!(unreleased.is_empty());
                }
                other => panic!("expected ReleaseWorktreeHandlesResult response, got {other:?}"),
            }

            client
                .send_prost(&prost_request(
                    "prost-ping",
                    pb::request::Body::Ping(pb::Empty {}),
                ))
                .await
                .unwrap();
            let response: Option<DecodedWireMessage<Response, pb::Response>> =
                client.recv_wire().await.unwrap();
            match response {
                Some(DecodedWireMessage::ProstV16(response)) => {
                    assert_eq!(response.request_id, "prost-ping");
                    let response =
                        wire_prost::supported_control_response_from_prost(response).unwrap();
                    assert_eq!(response, Response::Pong);
                }
                other => panic!("expected prost Pong response, got {other:?}"),
            }

            client
                .send_prost(&prost_request(
                    "prost-shutdown",
                    pb::request::Body::Shutdown(pb::Empty {}),
                ))
                .await
                .unwrap();
            let response: Option<DecodedWireMessage<Response, pb::Response>> =
                client.recv_wire().await.unwrap();
            match response {
                Some(DecodedWireMessage::ProstV16(response)) => {
                    assert_eq!(response.request_id, "prost-shutdown");
                    let response =
                        wire_prost::supported_control_response_from_prost(response).unwrap();
                    assert_eq!(response, Response::ShuttingDown);
                }
                other => panic!("expected prost ShuttingDown response, got {other:?}"),
            }

            server_task.await.unwrap();
        })
        .await;
    }
}

#[cfg(test)]
mod self_profile_tests {
    use super::*;

    fn test_journal_ctx(compiler: &str, args: &[&str]) -> JournalContext {
        JournalContext {
            compiler: compiler.to_string(),
            args: args.iter().map(|arg| (*arg).to_string()).collect(),
            cwd: ".".to_string(),
            env: None,
            session_id: None,
        }
    }

    #[test]
    fn parse_time_non_cacheable_miss_is_attributed() {
        let ctx = test_journal_ctx("rustc", &["--version"]);
        assert_eq!(
            compile_miss_reason(&ctx, "miss", Some(miss_reason::UNKNOWN)),
            Some(miss_reason::UNCACHEABLE_INPUT)
        );
    }

    #[test]
    fn cacheable_miss_keeps_default_reason() {
        let ctx = test_journal_ctx("rustc", &["--crate-name", "demo", "src/lib.rs"]);
        assert_eq!(
            compile_miss_reason(&ctx, "miss", Some(miss_reason::UNKNOWN)),
            Some(miss_reason::UNKNOWN)
        );
    }

    #[test]
    fn hit_split_has_non_zero_hash_lookup_decompress() {
        let s = derive_approx_spans("hit", 999).unwrap();
        assert_ne!(s.hash_inputs_ns, 0);
        assert_ne!(s.lookup_ns, 0);
        assert_ne!(s.decompress_ns, 0);
        assert_eq!(s.store_ns, 0);
        assert_eq!(s.hash_inputs_ns + s.lookup_ns + s.decompress_ns, 999);
    }

    #[test]
    fn miss_split_has_non_zero_hash_lookup_store() {
        let s = derive_approx_spans("miss", 999).unwrap();
        assert_ne!(s.hash_inputs_ns, 0);
        assert_ne!(s.lookup_ns, 0);
        assert_ne!(s.store_ns, 0);
        assert_eq!(s.decompress_ns, 0);
        assert_eq!(s.hash_inputs_ns + s.lookup_ns + s.store_ns, 999);
    }

    #[test]
    fn link_outcomes_partition_too() {
        assert!(derive_approx_spans("link_hit", 100).is_some());
        assert!(derive_approx_spans("link_miss", 100).is_some());
    }

    #[test]
    fn error_outcome_returns_none() {
        assert!(derive_approx_spans("error", 100).is_none());
    }
}