Skip to main content

sqry_daemon_protocol/
protocol.rs

1//! Wire types for the sqryd daemon IPC.
2//!
3//! Every type in this module serialises as UTF-8 JSON through serde.
4//! The wire format is versioned via the `envelope_version` field on
5//! [`DaemonHelloResponse`] / [`ShimRegisterAck`]; clients negotiate
6//! compatibility during the handshake before issuing any JSON-RPC
7//! request or entering the shim byte-pump.
8//!
9//! # JSON-RPC 2.0 conformance
10//!
11//! - Requests and responses carry the mandatory `"jsonrpc": "2.0"` tag
12//!   enforced by [`JsonRpcVersion`]'s manual serde impls.
13//! - Response ids follow the spec exactly: a response to a request
14//!   with a missing/invalid id MUST carry `id: null`; `Option<JsonRpcId>`
15//!   on [`JsonRpcResponse::id`] is NOT marked `skip_serializing_if`, so
16//!   `None` serialises as JSON `null` instead of being omitted.
17//! - Batches are implemented in the sqry-daemon router; this module
18//!   only provides the single-request envelope types.
19//!
20//! # `shim/register`
21//!
22//! [`ShimRegister`] / [`ShimProtocol`] / [`ShimRegisterAck`] are the
23//! Phase 8c shim handshake wire types. The router in sqry-daemon
24//! discriminates on the very first frame:
25//!
26//! - If the frame object has both `protocol` + `pid` keys (shim-shaped),
27//!   the router enters the shim path and deserialises as [`ShimRegister`]
28//!   with `deny_unknown_fields`. On deserialisation failure (e.g. extra
29//!   keys from the hello shape, or an unknown `protocol` variant) the
30//!   server writes [`ShimRegisterAck`]`{ accepted: false, reason: Some(..) }`
31//!   and closes. **Not** a JSON-RPC `-32600` — the shim client expects a
32//!   [`ShimRegisterAck`] as the first response, so the wire-form stays
33//!   coherent.
34//! - Otherwise the router falls through to the [`DaemonHello`] path
35//!   (JSON-RPC). A frame with neither shape is rejected with
36//!   `-32600 Invalid Request` and `id: null`.
37
38use std::marker::PhantomData;
39
40use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
41
42// ---------------------------------------------------------------------------
43// WorkspaceId — protocol-side wire wrapper for sqry-core's WorkspaceId.
44// ---------------------------------------------------------------------------
45
46/// 32-byte stable identity for a logical workspace, byte-identical to
47/// `sqry_core::workspace::WorkspaceId`.
48///
49/// Defined here in the leaf protocol crate so the daemon wire types
50/// (`DaemonHello.logical_workspace`, `daemon/load.logical_workspace`,
51/// `daemon/workspaceStatus.workspace_id`) can carry the identity without
52/// the protocol crate taking a `sqry-core` dependency. The `sqry-daemon`
53/// binary owns the `From`/`Into` bridge against the canonical
54/// `sqry_core::workspace::WorkspaceId` type — both use the same 32-byte
55/// representation, so the bridge is a zero-cost newtype unwrap.
56///
57/// STEP_6 (workspace-aware-cross-repo DAG) introduced this type. Older
58/// daemon clients that send `DaemonHello` without `logical_workspace`
59/// continue to work because the field is `#[serde(default)]` — they
60/// reproduce today's per-source-root semantics, with `workspace_id =
61/// None` on the matching [`crate::WorkspaceState`] entries.
62#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
63pub struct WorkspaceId([u8; 32]);
64
65impl WorkspaceId {
66    /// Construct from raw 32 bytes. Callers in `sqry-daemon` use this
67    /// to bridge from `sqry_core::workspace::WorkspaceId::as_bytes()`.
68    #[must_use]
69    pub const fn from_bytes(bytes: [u8; 32]) -> Self {
70        Self(bytes)
71    }
72
73    /// Borrow the 32-byte digest. Callers cross the bridge by feeding
74    /// these bytes back into `sqry_core::workspace::WorkspaceId`.
75    #[must_use]
76    pub const fn as_bytes(&self) -> &[u8; 32] {
77        &self.0
78    }
79
80    /// First 16 hex characters. Suitable for log lines / short
81    /// identifiers; **not** sufficient for cross-process identity.
82    #[must_use]
83    pub fn as_short_hex(&self) -> String {
84        let full = self.as_full_hex();
85        full[..16].to_string()
86    }
87
88    /// Full 64-character hex digest. Use this for any identity
89    /// comparison.
90    #[must_use]
91    pub fn as_full_hex(&self) -> String {
92        use std::fmt::Write as _;
93        let mut s = String::with_capacity(64);
94        for byte in &self.0 {
95            // `write!` to a `String` is infallible.
96            let _ = write!(s, "{byte:02x}");
97        }
98        s
99    }
100}
101
102impl std::fmt::Display for WorkspaceId {
103    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
104        f.write_str(&self.as_short_hex())
105    }
106}
107
108// ---------------------------------------------------------------------------
109// LogicalWorkspaceWire — daemon-IPC wire form of sqry-core's LogicalWorkspace.
110// ---------------------------------------------------------------------------
111
112/// Wire-form summary of a `LogicalWorkspace`, attached to
113/// [`DaemonHello`] / `daemon/load` payloads. Carries the workspace
114/// identity plus the canonical source-root paths the client wants the
115/// daemon to bind under a single grouping `workspace_id`.
116///
117/// `member_folders` and `exclusions` are explicitly **not** carried on
118/// this wire shape — they are MCP / redaction-side concerns (Step 7 of
119/// the workspace-aware-cross-repo plan), not daemon admission concerns.
120/// The daemon only needs `workspace_id` + the source-root list to build
121/// one [`crate::WorkspaceState`]-keyed entry per source root.
122#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
123#[serde(deny_unknown_fields)]
124pub struct LogicalWorkspaceWire {
125    /// 32-byte BLAKE3-256 identity of the logical workspace.
126    pub workspace_id: WorkspaceId,
127    /// Canonical absolute source-root paths. The daemon constructs one
128    /// `WorkspaceKey { workspace_id: Some(this id), source_root: <p>, .. }`
129    /// per entry, all sharing the same `workspace_id` for grouping.
130    pub source_roots: Vec<std::path::PathBuf>,
131    /// STEP_11_4 — per-source-root bindings. Each entry's `path` MUST
132    /// appear in [`Self::source_roots`]; the binding's
133    /// `config_fingerprint` overrides the workspace-level default for
134    /// that root only. Empty in the common case so the wire stays
135    /// pre-STEP_11_4-compatible.
136    #[serde(default, skip_serializing_if = "Vec::is_empty")]
137    pub source_root_bindings: Vec<SourceRootBinding>,
138    /// STEP_11_4 — workspace-level config fingerprint applied to any
139    /// source root that does not carry its own
140    /// [`SourceRootBinding::config_fingerprint`] override. `0` is the
141    /// "fingerprint not set" sentinel.
142    #[serde(default, skip_serializing_if = "is_zero_u64")]
143    pub workspace_config_fingerprint: u64,
144}
145
146fn is_zero_u64(value: &u64) -> bool {
147    *value == 0
148}
149
150/// STEP_11_4 — per-source-root binding inside a [`LogicalWorkspaceWire`].
151///
152/// `path` MUST appear in the parent [`LogicalWorkspaceWire::source_roots`]
153/// vector; the daemon matches bindings to source roots by canonical path
154/// equality. A binding whose `path` is not in `source_roots` is silently
155/// ignored.
156#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
157#[serde(deny_unknown_fields)]
158pub struct SourceRootBinding {
159    /// Canonical absolute path of the source root this binding applies to.
160    pub path: std::path::PathBuf,
161    /// Per-source-root override of the config fingerprint. `0` means
162    /// "use the workspace-level fingerprint"; non-zero overrides for
163    /// this source root only.
164    #[serde(default, skip_serializing_if = "is_zero_u64")]
165    pub config_fingerprint: u64,
166    /// Optional pre-resolved classpath directory for this source root.
167    #[serde(default, skip_serializing_if = "Option::is_none")]
168    pub classpath_dir: Option<std::path::PathBuf>,
169}
170
171// ---------------------------------------------------------------------------
172// WorkspaceIndexStatus — daemon/workspaceStatus result payload.
173// ---------------------------------------------------------------------------
174
175/// Aggregate status of a single source root inside a logical workspace.
176/// Mirrors the per-source-root subset of `WorkspaceStatus` so cross-repo
177/// MCP / LSP queries can render a per-source-root state without paying
178/// the cost of the full `daemon/status` snapshot.
179///
180/// STEP_11_4 (workspace-aware-cross-repo, 2026-04-26) — adds the
181/// `classpath_present` flag so consumers of `daemon/workspaceStatus`
182/// know which source roots have JVM classpath analysis available
183/// (`<source_root>/.sqry/classpath/` exists) without having to make a
184/// separate filesystem probe. The flag is per-source-root, never
185/// aggregated, so a workspace mixing JVM and non-JVM source roots
186/// reports accurate per-root granularity.
187#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
188pub struct WorkspaceSourceRootStatus {
189    /// Canonical absolute path to the source root.
190    pub source_root: std::path::PathBuf,
191    /// Per-source-root lifecycle state. `Evicted` is a valid (and
192    /// useful — partial eviction is observable here) value for a
193    /// source root that has been LRU'd out while sibling source roots
194    /// remain `Loaded`.
195    pub state: WorkspaceState,
196    /// Live graph size for this source root, in bytes.
197    pub current_bytes: u64,
198    /// STEP_11_4 — `true` when the daemon observed
199    /// `<source_root>/.sqry/classpath/` as a directory at status time.
200    /// `false` when the directory is absent or the probe failed (the
201    /// daemon never blocks status on a classpath probe; failures
202    /// surface through the LSP-side `WorkspaceIndexStatus.warnings`
203    /// channel instead).
204    ///
205    /// `#[serde(default)]` so v1 IPC payloads (which never carried the
206    /// flag) round-trip into `false`. `skip_serializing_if = ...` is
207    /// deliberately NOT applied — the flag must be serialised even
208    /// when `false` so consumers can distinguish "JVM-aware daemon
209    /// reporting no classpath" from "older daemon that does not yet
210    /// surface the flag".
211    #[serde(default)]
212    pub classpath_present: bool,
213}
214
215/// Aggregate status of a logical workspace, returned by
216/// `daemon/workspaceStatus { workspace_id }`.
217///
218/// The daemon walks every `WorkspaceKey` whose `workspace_id` matches
219/// the request and aggregates them into this view. A workspace is
220/// "partially evicted" when at least one source root reports
221/// [`WorkspaceState::Evicted`] but at least one other reports any
222/// non-Evicted state — see [`Self::partially_evicted`].
223///
224/// STEP_12 (workspace-aware-cross-repo, 2026-04-26) introduced the
225/// hex-string telemetry fields `workspace_id_short` (16 hex chars,
226/// display) and `workspace_id_full` (64 hex chars, machine identity).
227/// Scripts consuming this payload should key on `workspace_id_full` —
228/// the 32-byte `workspace_id` is the canonical bytewise identity but
229/// the hex string is what humans / shell tooling read. The two hex
230/// fields are derived from `workspace_id`; they are NOT independent
231/// inputs — they exist purely for ergonomic JSON consumption.
232#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
233pub struct WorkspaceIndexStatus {
234    /// Identity the request matched against.
235    pub workspace_id: WorkspaceId,
236    /// STEP_12 — short (16 hex) form of `workspace_id`, suitable for
237    /// CLI columns and human-scale log lines. Display only.
238    pub workspace_id_short: String,
239    /// STEP_12 — full (64 hex) form of `workspace_id`. Machine
240    /// identity. Cross-process script consumers MUST key on this
241    /// rather than the short form to avoid the (remote, non-zero)
242    /// possibility of short-hex collisions across hundreds of
243    /// thousands of distinct workspaces.
244    pub workspace_id_full: String,
245    /// Per-source-root status rows, sorted by `source_root` for
246    /// deterministic CLI / test output.
247    pub source_roots: Vec<WorkspaceSourceRootStatus>,
248}
249
250impl WorkspaceIndexStatus {
251    /// Whether at least one source root is in [`WorkspaceState::Evicted`]
252    /// while at least one other is not. `false` for fully-loaded or
253    /// fully-evicted aggregates.
254    #[must_use]
255    pub fn partially_evicted(&self) -> bool {
256        let any_evicted = self
257            .source_roots
258            .iter()
259            .any(|r| matches!(r.state, WorkspaceState::Evicted));
260        let any_alive = self
261            .source_roots
262            .iter()
263            .any(|r| !matches!(r.state, WorkspaceState::Evicted));
264        any_evicted && any_alive
265    }
266}
267
268// ---------------------------------------------------------------------------
269// Wire envelope version.
270// ---------------------------------------------------------------------------
271
272/// Version of the daemon wire envelope ([`DaemonHelloResponse::envelope_version`],
273/// [`ShimRegisterAck::envelope_version`]).
274///
275/// Bumped when the [`ResponseEnvelope`] schema changes in an incompatible way.
276/// Kept at `1` per the Amendment-2 2026-04-09 freeze.
277///
278/// This constant lives in the leaf wire-type crate (`sqry-daemon-protocol`) so
279/// every consumer of the wire format — the daemon itself, the daemon client
280/// (`sqry-daemon-client`), and the shim-mode callers inside `sqry-lsp` /
281/// `sqry-mcp` — validates against exactly one source of truth. Clients MUST
282/// reject a response whose `envelope_version` differs from this constant
283/// rather than proceed on a mismatched wire format.
284pub const ENVELOPE_VERSION: u32 = 1;
285
286// ---------------------------------------------------------------------------
287// WorkspaceState — moved here from sqry-daemon/src/workspace/state.rs
288// ---------------------------------------------------------------------------
289
290/// Six-state workspace lifecycle per plan Task 6 Step 1 and Amendment 2 §G.5 /
291/// §G.7.
292///
293/// The `#[repr(u8)]` is load-bearing: `sqry-daemon`'s `LoadedWorkspace::state`
294/// is an `AtomicU8`, and the conversions [`Self::from_u8`] / [`Self::as_u8`]
295/// serialise the state machine without allocation. Values are deliberately
296/// contiguous from 0 so adding a variant stays backwards-compatible with
297/// persisted telemetry.
298///
299/// This type lives in the leaf wire-type crate so [`ResponseMeta`] can
300/// carry a canonical workspace_state string on every successful tool
301/// response without the leaf crate taking a dep on `sqry-daemon` itself.
302#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
303#[repr(u8)]
304pub enum WorkspaceState {
305    /// Workspace entry exists but no graph has been loaded yet.
306    Unloaded = 0,
307
308    /// Initial load is in progress — a single blocking read from disk or
309    /// a full rebuild with no prior snapshot.
310    Loading = 1,
311
312    /// Graph is loaded, idle, and ready to serve queries.
313    Loaded = 2,
314
315    /// A rebuild (incremental or full) is actively running on the
316    /// dispatcher's background task. Queries keep serving the prior
317    /// `ArcSwap<CodeGraph>` snapshot until `publish_and_retain` swaps
318    /// the new graph in.
319    Rebuilding = 3,
320
321    /// Workspace was LRU-evicted or explicitly unloaded. The entry is
322    /// REMOVED from the manager map — the next query must re-load via
323    /// `get_or_load`. This discriminant exists for the short window
324    /// between `execute_eviction` storing the state and
325    /// `workspaces.remove(key)` completing (both under
326    /// `workspaces.write()`); external observers routed through
327    /// `WorkspaceManager::classify_for_serve` see the map-missing arm
328    /// first and get `DaemonError::WorkspaceEvicted` regardless.
329    Evicted = 4,
330
331    /// The most recent rebuild failed. Queries are served from the last
332    /// good snapshot with `meta.stale = true`; if the
333    /// `stale_serve_max_age_hours` cap is exceeded, queries receive the
334    /// JSON-RPC `-32002 workspace_stale_expired` error instead.
335    Failed = 5,
336}
337
338impl WorkspaceState {
339    /// Round-trip the state to its discriminant.
340    #[must_use]
341    pub const fn as_u8(self) -> u8 {
342        self as u8
343    }
344
345    /// Parse a discriminant back to a state. Returns `None` on any value
346    /// outside the current enum range — callers should treat this as a
347    /// telemetry corruption rather than silently map to `Unloaded`.
348    #[must_use]
349    pub const fn from_u8(value: u8) -> Option<Self> {
350        match value {
351            0 => Some(Self::Unloaded),
352            1 => Some(Self::Loading),
353            2 => Some(Self::Loaded),
354            3 => Some(Self::Rebuilding),
355            4 => Some(Self::Evicted),
356            5 => Some(Self::Failed),
357            _ => None,
358        }
359    }
360
361    /// Canonical display string. Used by `daemon/status` output and
362    /// tracing spans.
363    #[must_use]
364    pub const fn as_str(self) -> &'static str {
365        match self {
366            Self::Unloaded => "unloaded",
367            Self::Loading => "loading",
368            Self::Loaded => "loaded",
369            Self::Rebuilding => "rebuilding",
370            Self::Evicted => "evicted",
371            Self::Failed => "failed",
372        }
373    }
374
375    /// Whether the workspace can still serve queries in this state.
376    ///
377    /// `true` for [`Self::Loaded`], [`Self::Rebuilding`] (old snapshot
378    /// still served), and [`Self::Failed`] (stale-serve subject to the
379    /// age cap). `false` for [`Self::Unloaded`], [`Self::Loading`],
380    /// and [`Self::Evicted`].
381    #[must_use]
382    pub const fn is_serving(self) -> bool {
383        matches!(self, Self::Loaded | Self::Rebuilding | Self::Failed)
384    }
385}
386
387impl std::fmt::Display for WorkspaceState {
388    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
389        f.write_str(self.as_str())
390    }
391}
392
393// ---------------------------------------------------------------------------
394// Handshake types.
395// ---------------------------------------------------------------------------
396
397/// Pre-handshake header sent as the very first frame by a CLI client.
398/// The server responds with [`DaemonHelloResponse`] before the
399/// JSON-RPC request loop begins.
400#[derive(Debug, Clone, Serialize, Deserialize)]
401#[serde(deny_unknown_fields)]
402pub struct DaemonHello {
403    /// Free-form client identifier (`env!("CARGO_PKG_VERSION")` plus
404    /// user-agent suffix). Informational only.
405    pub client_version: String,
406
407    /// Wire protocol version. Phase 8a accepts exactly `1`.
408    pub protocol_version: u32,
409
410    /// Optional logical-workspace binding hint (STEP_6 of the
411    /// workspace-aware-cross-repo plan). When present, every
412    /// subsequent `daemon/load` on this connection that does not
413    /// itself supply `logical_workspace` inherits this binding —
414    /// keeping today's anonymous behaviour for clients that do not
415    /// set the hint.
416    ///
417    /// `#[serde(default)]` so older clients (and the standalone
418    /// `sqry-mcp` / `sqry-lsp` shims that have not yet learned about
419    /// logical workspaces) keep working with `None`. The daemon
420    /// router synthesises one `WorkspaceKey` per source root with
421    /// `workspace_id = Some(this id)`.
422    #[serde(default, skip_serializing_if = "Option::is_none")]
423    pub logical_workspace: Option<LogicalWorkspaceWire>,
424}
425
426/// Server's reply to [`DaemonHello`]. If `compatible` is `false` the
427/// server closes the connection immediately after the frame is sent.
428#[derive(Debug, Clone, Serialize, Deserialize)]
429#[serde(deny_unknown_fields)]
430pub struct DaemonHelloResponse {
431    pub compatible: bool,
432    pub daemon_version: String,
433    pub envelope_version: u32,
434}
435
436// ---------------------------------------------------------------------------
437// Shim handshake (Phase 8c wire types).
438// ---------------------------------------------------------------------------
439
440/// Which client protocol the shim will pump bytes for. Phase 8c surface.
441#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
442#[serde(rename_all = "lowercase")]
443pub enum ShimProtocol {
444    Lsp,
445    Mcp,
446}
447
448/// Shim registration header sent as the first frame by a
449/// `sqry lsp --daemon` or `sqry mcp --daemon` process. The router in
450/// sqry-daemon shape-discriminates between [`DaemonHello`] and this
451/// type using `#[serde(deny_unknown_fields)]`.
452#[derive(Debug, Clone, Serialize, Deserialize)]
453#[serde(deny_unknown_fields)]
454pub struct ShimRegister {
455    pub protocol: ShimProtocol,
456    pub pid: u32,
457}
458
459/// Server's reply to [`ShimRegister`]. If `accepted` is `false` the
460/// server closes the connection after sending the ack and the shim
461/// client surfaces `reason` to its parent process. When `accepted` is
462/// `true`, `reason` is omitted from the wire form (skip-if-none).
463#[derive(Debug, Clone, Serialize, Deserialize)]
464#[serde(deny_unknown_fields)]
465pub struct ShimRegisterAck {
466    pub accepted: bool,
467    pub daemon_version: String,
468    /// Rejection reason. Omitted from the wire when accepted=true.
469    #[serde(skip_serializing_if = "Option::is_none")]
470    pub reason: Option<String>,
471    pub envelope_version: u32,
472}
473
474// ---------------------------------------------------------------------------
475// ResponseEnvelope.
476// ---------------------------------------------------------------------------
477
478/// Uniform successful-response wrapper. Every successful method
479/// response is serialised as `ResponseEnvelope<T>` at the JSON-RPC
480/// `result` field — clients can rely on the [`ResponseMeta`] shape
481/// being present on every successful reply regardless of method.
482#[derive(Debug, Clone, Serialize, Deserialize)]
483pub struct ResponseEnvelope<T> {
484    pub result: T,
485    pub meta: ResponseMeta,
486}
487
488/// Metadata attached to every successful response. For Phase 8a
489/// management methods the staleness fields are always absent
490/// (`stale = false`, no last_good_at, no last_error,
491/// `workspace_state = None`). Phase 8b populates them from the
492/// server-side `ServeVerdict` for tool-method responses.
493#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
494pub struct ResponseMeta {
495    pub stale: bool,
496
497    #[serde(skip_serializing_if = "Option::is_none")]
498    pub last_good_at: Option<String>,
499
500    #[serde(skip_serializing_if = "Option::is_none")]
501    pub last_error: Option<String>,
502
503    /// Canonical workspace state string (serde form of
504    /// [`WorkspaceState`]). `None` for methods not tied to a workspace.
505    #[serde(skip_serializing_if = "Option::is_none")]
506    pub workspace_state: Option<WorkspaceState>,
507
508    pub daemon_version: String,
509}
510
511impl ResponseMeta {
512    /// Construct the [`ResponseMeta`] used by daemon management methods
513    /// (`daemon/status`, `daemon/unload`, `daemon/stop` — the ones not
514    /// bound to a specific workspace).
515    #[must_use]
516    pub fn management(daemon_version: &str) -> Self {
517        Self {
518            stale: false,
519            last_good_at: None,
520            last_error: None,
521            workspace_state: None,
522            daemon_version: daemon_version.to_owned(),
523        }
524    }
525
526    /// Construct the [`ResponseMeta`] for a successful `daemon/load`.
527    /// Phase 8b adds `fresh_from` / `stale_from` constructors for
528    /// MCP tool-method responses that route through `classify_for_serve`.
529    #[must_use]
530    pub fn loaded(daemon_version: &str) -> Self {
531        Self {
532            stale: false,
533            last_good_at: None,
534            last_error: None,
535            workspace_state: Some(WorkspaceState::Loaded),
536            daemon_version: daemon_version.to_owned(),
537        }
538    }
539
540    /// Construct [`ResponseMeta`] for a tool-method response served from a
541    /// Fresh workspace verdict (`WorkspaceState::Loaded` or `Rebuilding`).
542    ///
543    /// Phase 8b Task 7 — populated by the `tool_dispatch` helper when
544    /// the daemon's `WorkspaceManager::classify_for_serve` returns
545    /// `ServeVerdict::Fresh`. `stale` is `false` and both `last_good_at`
546    /// and `last_error` are absent from the wire form (they are skipped
547    /// by `serde(skip_serializing_if = "Option::is_none")`).
548    #[must_use]
549    pub fn fresh_from(state: WorkspaceState, daemon_version: &str) -> Self {
550        Self {
551            stale: false,
552            last_good_at: None,
553            last_error: None,
554            workspace_state: Some(state),
555            daemon_version: daemon_version.to_owned(),
556        }
557    }
558
559    /// Construct [`ResponseMeta`] for a tool-method response served from a
560    /// Stale verdict. `last_good_at` is rendered as RFC3339 UTC-Zulu via
561    /// `chrono::DateTime::<Utc>::from(SystemTime) -> to_rfc3339_opts(Secs, true)`.
562    ///
563    /// `workspace_state` is fixed at [`WorkspaceState::Failed`] because
564    /// `WorkspaceManager::classify_for_serve` only emits a Stale verdict
565    /// when the observed state is `Failed`. Keeping this constructor
566    /// intentionally rigid (no caller-supplied state) prevents the wire
567    /// form from claiming `stale = true` with a workspace_state the
568    /// classifier could never have produced.
569    #[must_use]
570    pub fn stale_from(
571        last_good_at: std::time::SystemTime,
572        last_error: Option<String>,
573        daemon_version: &str,
574    ) -> Self {
575        use chrono::{DateTime, SecondsFormat, Utc};
576        let rfc3339 =
577            DateTime::<Utc>::from(last_good_at).to_rfc3339_opts(SecondsFormat::Secs, true);
578        Self {
579            stale: true,
580            last_good_at: Some(rfc3339),
581            last_error,
582            workspace_state: Some(WorkspaceState::Failed),
583            daemon_version: daemon_version.to_owned(),
584        }
585    }
586}
587
588// ---------------------------------------------------------------------------
589// daemon/load result wire type.
590// ---------------------------------------------------------------------------
591
592/// `daemon/load` success result payload.
593///
594/// Serialised under the `result` field of [`ResponseEnvelope`]. Living
595/// in the leaf protocol crate lets both the daemon (writer) and
596/// [`sqry-daemon-client`][] (reader) share a single typed definition —
597/// clients can `serde_json::from_value::<ResponseEnvelope<LoadResult>>`
598/// and get compile-time schema checking instead of stringly-typed
599/// `serde_json::Value::get` lookups.
600///
601/// [`sqry-daemon-client`]: ../../sqry-daemon-client/index.html
602#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
603#[serde(deny_unknown_fields)]
604pub struct LoadResult {
605    /// The canonicalised workspace root path that the daemon loaded.
606    pub root: std::path::PathBuf,
607
608    /// Resident graph memory footprint for the loaded workspace, in
609    /// bytes. Matches `LoadedWorkspace::heap_bytes()` at the moment of
610    /// the response.
611    pub current_bytes: u64,
612
613    /// The canonical workspace lifecycle state after the load
614    /// completes. Always [`WorkspaceState::Loaded`] on the successful
615    /// `daemon/load` path — the field is typed so clients do not have
616    /// to re-parse the string.
617    pub state: WorkspaceState,
618}
619
620/// Status of a `daemon/rebuild` invocation (cluster-G §2.4).
621///
622/// Distinguishes the four outcomes the dispatcher can produce so a
623/// `--timeout 0` (fire-and-forget) caller can distinguish "started in
624/// the background" from "actually completed in this call". Pre-§2.4
625/// callers received only the `Completed` shape and a missing field
626/// here is interpreted as `Completed` for backward compatibility.
627#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)]
628#[serde(rename_all = "snake_case")]
629pub enum RebuildStatus {
630    /// The rebuild ran to completion in this call. `duration_ms`,
631    /// `nodes`, `edges`, `files_indexed`, and `was_full` are all
632    /// populated.
633    #[default]
634    Completed,
635    /// `--timeout 0` (fire-and-forget): the runner-role was acquired
636    /// and the rebuild is running in the background. The stat fields
637    /// are absent. The caller should poll `daemon/status` to observe
638    /// completion.
639    Started,
640    /// Another runner is active; this request was coalesced into the
641    /// pending lane. The stat fields reflect the runner's *previous*
642    /// publish if known, or are absent.
643    Coalesced,
644    /// Reservation failed before the pipeline started (e.g.
645    /// `MemoryBudgetExceeded`, `WorkspaceOversize`). The stat fields
646    /// are absent.
647    Rejected,
648}
649
650/// `daemon/rebuild` success result payload (schema_version 2 — see
651/// cluster-G §2.4).
652///
653/// Serialised under the `result` field of [`ResponseEnvelope`]. The
654/// stat fields are `Option`-typed because `--timeout 0` callers
655/// receive them populated only when `status == Completed`.
656#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
657pub struct RebuildResult {
658    /// The canonicalised workspace root path that was rebuilt.
659    pub root: std::path::PathBuf,
660    /// Outcome of the dispatch. New in cluster-G §2.4. Older clients
661    /// that pre-date the schema bump are tolerated by the
662    /// `#[serde(default)]` here — they read the field as `Completed`
663    /// and continue to work because pre-§2.4 daemons only ever
664    /// produced the completed shape.
665    #[serde(default)]
666    pub status: RebuildStatus,
667    /// Wall-clock time the rebuild took, in milliseconds. Populated
668    /// only when `status == Completed`.
669    #[serde(default, skip_serializing_if = "Option::is_none")]
670    pub duration_ms: Option<u64>,
671    /// Node count of the freshly published graph. Populated only
672    /// when `status == Completed`.
673    #[serde(default, skip_serializing_if = "Option::is_none")]
674    pub nodes: Option<u64>,
675    /// Edge count of the freshly published graph. Populated only
676    /// when `status == Completed`.
677    #[serde(default, skip_serializing_if = "Option::is_none")]
678    pub edges: Option<u64>,
679    /// Number of source files indexed in the freshly published
680    /// graph. Populated only when `status == Completed`.
681    #[serde(default, skip_serializing_if = "Option::is_none")]
682    pub files_indexed: Option<u64>,
683    /// `true` when the rebuild was a full (non-incremental) rebuild.
684    /// Populated only when `status == Completed`.
685    #[serde(default, skip_serializing_if = "Option::is_none")]
686    pub was_full: Option<bool>,
687}
688
689/// `daemon/cancel_rebuild` success result payload.
690///
691/// Serialised under the `result` field of [`ResponseEnvelope`].
692#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
693pub struct CancelRebuildResult {
694    /// The canonicalised workspace root path whose rebuild was signalled for
695    /// cancellation.
696    pub root: std::path::PathBuf,
697    /// `true` when a rebuild was actually in flight at the moment the
698    /// cancellation signal was dispatched.
699    pub cancelled: bool,
700}
701
702// ---------------------------------------------------------------------------
703// `daemon/search` wire types (verivus-oss/sqry#238 Tier 2).
704//
705// Mirror the relevant arguments of `commands::search::run_search` so the
706// CLI shim at `sqry-cli/src/commands/search.rs` can attempt the daemon
707// path before falling through to in-process load. Field shape follows the
708// established protocol-crate convention: leaf-only (no upstream sqry deps,
709// no tower-lsp types), flat fields rather than nested LSP `Location`
710// wrappers — the CLI converts to `DisplaySymbol` for output formatting,
711// keeping parity at the formatter layer rather than the wire layer.
712// ---------------------------------------------------------------------------
713
714/// `daemon/search` request payload.
715///
716/// Submitted as the `params` field of a [`JsonRpcRequest`] with
717/// `method == "daemon/search"`. Wire-compatible with `--exact`, regex, and
718/// fuzzy search modes; the daemon handler dispatches to the same
719/// `find_by_exact_name` / regex / fuzzy logic the in-process CLI uses
720/// (verifiable by the `DAEMON_SEARCH_TESTS` parity assertions).
721///
722/// Errors: a request whose `search_path` does not resolve to a
723/// daemon-loaded workspace returns `WorkspaceEvicted` (-32004) or
724/// `WorkspaceNotLoaded`; an incompatible plugin selection returns
725/// `WorkspaceIncompatibleGraph` (-32005). The CLI shim treats either as
726/// a soft failure and falls through to the in-process path.
727#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
728#[serde(deny_unknown_fields)]
729pub struct SearchRequest {
730    /// Wire envelope version. Defaults to [`ENVELOPE_VERSION`] for
731    /// forward-compatible serde when the field is absent.
732    #[serde(default = "default_envelope_version")]
733    pub envelope_version: u32,
734    /// Pattern to search for. Interpreted per `mode`.
735    pub pattern: String,
736    /// Workspace root the search should resolve against. The daemon
737    /// normalises and looks this up via `WorkspaceManager`.
738    pub search_path: String,
739    /// Search interpretation mode. Maps to the CLI's `--exact` / `--fuzzy`
740    /// flags; everything else falls into [`SearchMode::Regex`].
741    pub mode: SearchMode,
742    /// Optional kind filter (e.g. `"function"`, `"class"`). Matches
743    /// the in-process `Cli::kind` semantics.
744    #[serde(default, skip_serializing_if = "Option::is_none")]
745    pub kind: Option<String>,
746    /// Optional language filter (e.g. `"rust"`, `"python"`).
747    #[serde(default, skip_serializing_if = "Option::is_none")]
748    pub lang: Option<String>,
749    /// Maximum result count. `None` lets the daemon apply its default
750    /// (mirrors `Cli::limit`).
751    #[serde(default, skip_serializing_if = "Option::is_none")]
752    pub limit: Option<u32>,
753    /// Mirror of the CLI's `--include-generated` flag (default in-process:
754    /// `false` — macro-generated symbols are dropped). When `false` the
755    /// daemon applies the same `filter_nodes_by_macro_boundary` contract
756    /// as `sqry-cli/src/commands/search.rs::run_regular_search` so the
757    /// daemon route does not surface macro-generated symbols the
758    /// in-process path would have dropped.
759    ///
760    /// Serde default is `true` for backward compatibility — a request
761    /// body that omits the field gets the same "no filter" behaviour
762    /// the tier-2 daemon handler shipped with originally (the
763    /// `DAEMON_SEARCH_HANDLER` unit's approved tests rely on that
764    /// default and continue to pass without modification).
765    #[serde(default = "default_include_generated")]
766    pub include_generated: bool,
767}
768
769fn default_envelope_version() -> u32 {
770    ENVELOPE_VERSION
771}
772
773fn default_include_generated() -> bool {
774    // True keeps the pre-`include_generated` wire-shape behaviour: the
775    // daemon returns every candidate, including macro-generated ones.
776    // Callers that need parity with the CLI's default exact search must
777    // set this to `false` so the daemon drops `macro_generated == Some(true)`
778    // nodes before serialising the result set.
779    true
780}
781
782/// Search interpretation mode for [`SearchRequest::mode`].
783#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
784#[serde(rename_all = "lowercase")]
785pub enum SearchMode {
786    /// Regex pattern over interned symbol names. Default behaviour for
787    /// `sqry search <pat>` and `sqry <pat>`.
788    Regex,
789    /// Literal byte-exact symbol-name match (`--exact` / planner
790    /// `name:<literal>` contract).
791    Exact,
792    /// Trigram fuzzy match (`--fuzzy`).
793    Fuzzy,
794}
795
796/// One search hit. Flat shape (no LSP `Location` wrapper) so the protocol
797/// crate stays leaf-only; the CLI shim converts to `DisplaySymbol` for
798/// output formatting.
799///
800/// Line/column semantics match `DisplaySymbol`:
801///   - `start_line` / `end_line` are 1-based
802///   - `start_column` / `end_column` are 0-based byte offsets
803#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
804#[serde(deny_unknown_fields)]
805pub struct SearchItem {
806    pub name: String,
807    pub qualified_name: String,
808    pub kind: String,
809    pub language: String,
810    pub file_path: String,
811    pub start_line: u32,
812    pub start_column: u32,
813    pub end_line: u32,
814    pub end_column: u32,
815    /// Fuzzy match score. Absent for regex / exact hits.
816    #[serde(default, skip_serializing_if = "Option::is_none")]
817    pub score: Option<f32>,
818}
819
820/// `daemon/search` success result payload.
821///
822/// Serialised under the `result` field of [`ResponseEnvelope`]. The CLI
823/// shim reads `total` + `truncated` separately so it can emit the same
824/// "Showing N of M matches" banner the in-process path does.
825#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
826#[serde(deny_unknown_fields)]
827pub struct SearchResult {
828    /// The hits returned for this query, post-limit.
829    pub items: Vec<SearchItem>,
830    /// Pre-truncation match count. When `truncated == false` this equals
831    /// `items.len()`; when `truncated == true` it is at least `limit + 1`
832    /// (lower-bound sentinel, matching the LSP `list_unused_symbols` /
833    /// `list_circular_dependencies` convention).
834    pub total: u64,
835    /// True when the result was capped by `SearchRequest::limit`.
836    pub truncated: bool,
837    /// Reserved for future cursor-based pagination. Tier-2 always returns
838    /// `None`; clients should not assume any specific format.
839    #[serde(default, skip_serializing_if = "Option::is_none")]
840    pub cursor: Option<String>,
841}
842
843// ---------------------------------------------------------------------------
844// JSON-RPC 2.0 envelope types.
845// ---------------------------------------------------------------------------
846
847/// JSON-RPC `"2.0"` version tag. Manual serde impls enforce exact
848/// string match on the wire so malformed requests never leak into the
849/// method dispatcher.
850#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
851pub struct JsonRpcVersion;
852
853impl Serialize for JsonRpcVersion {
854    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
855        s.serialize_str("2.0")
856    }
857}
858
859impl<'de> Deserialize<'de> for JsonRpcVersion {
860    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
861        struct Vis(PhantomData<JsonRpcVersion>);
862        impl<'de> de::Visitor<'de> for Vis {
863            type Value = JsonRpcVersion;
864            fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
865                f.write_str("the string \"2.0\"")
866            }
867            fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E> {
868                if v == "2.0" {
869                    Ok(JsonRpcVersion)
870                } else {
871                    Err(E::invalid_value(de::Unexpected::Str(v), &"\"2.0\""))
872                }
873            }
874        }
875        d.deserialize_str(Vis(PhantomData))
876    }
877}
878
879/// JSON-RPC id: `null`, integer (signed or unsigned), or string.
880/// `I64` covers `i64::MIN..=i64::MAX`; `U64` covers
881/// `i64::MAX + 1..=u64::MAX`. Serde's untagged deserialize tries
882/// variants in order so `0..=i64::MAX` lands in `I64` and
883/// `i64::MAX + 1..=u64::MAX` in `U64`.
884#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
885#[serde(untagged)]
886pub enum JsonRpcId {
887    /// Signed integer id.
888    I64(i64),
889    /// Unsigned integer id above `i64::MAX`.
890    U64(u64),
891    /// String id.
892    Str(String),
893}
894
895/// JSON-RPC 2.0 request.
896#[derive(Debug, Clone, Serialize, Deserialize)]
897pub struct JsonRpcRequest {
898    pub jsonrpc: JsonRpcVersion,
899
900    /// `None` ≙ notification (no response expected).
901    #[serde(default, skip_serializing_if = "Option::is_none")]
902    pub id: Option<JsonRpcId>,
903
904    pub method: String,
905
906    #[serde(default)]
907    pub params: serde_json::Value,
908}
909
910/// JSON-RPC 2.0 response. `id` is [`Option<JsonRpcId>`] with **no**
911/// `skip_serializing_if` — the `None` case serialises as JSON `null`,
912/// which is exactly what the spec demands for parse-error and
913/// invalid-request responses.
914#[derive(Debug, Clone, Serialize, Deserialize)]
915pub struct JsonRpcResponse {
916    pub jsonrpc: JsonRpcVersion,
917
918    /// `null` on the wire when the server could not determine the
919    /// originating request id (parse error, invalid request shape,
920    /// batch element with un-parseable id).
921    pub id: Option<JsonRpcId>,
922
923    #[serde(flatten)]
924    pub payload: JsonRpcPayload,
925}
926
927/// Tagged success-or-error payload. Serde `untagged` so the wire form
928/// is `{... "result": ...}` or `{... "error": ...}`, never both.
929#[derive(Debug, Clone, Serialize, Deserialize)]
930#[serde(untagged)]
931pub enum JsonRpcPayload {
932    Success { result: serde_json::Value },
933    Error { error: JsonRpcError },
934}
935
936/// JSON-RPC 2.0 error payload.
937#[derive(Debug, Clone, Serialize, Deserialize)]
938pub struct JsonRpcError {
939    pub code: i32,
940    pub message: String,
941    #[serde(skip_serializing_if = "Option::is_none")]
942    pub data: Option<serde_json::Value>,
943}
944
945impl JsonRpcResponse {
946    /// Construct a successful response.
947    #[must_use]
948    pub fn success(id: Option<JsonRpcId>, result: serde_json::Value) -> Self {
949        Self {
950            jsonrpc: JsonRpcVersion,
951            id,
952            payload: JsonRpcPayload::Success { result },
953        }
954    }
955
956    /// Construct an error response.
957    #[must_use]
958    pub fn error(
959        id: Option<JsonRpcId>,
960        code: i32,
961        message: impl Into<String>,
962        data: Option<serde_json::Value>,
963    ) -> Self {
964        Self {
965            jsonrpc: JsonRpcVersion,
966            id,
967            payload: JsonRpcPayload::Error {
968                error: JsonRpcError {
969                    code,
970                    message: message.into(),
971                    data,
972                },
973            },
974        }
975    }
976}
977
978#[cfg(test)]
979mod tests {
980    use super::*;
981
982    #[test]
983    fn jsonrpc_version_roundtrip() {
984        let wire = serde_json::to_string(&JsonRpcVersion).unwrap();
985        assert_eq!(wire, r#""2.0""#);
986        let back: JsonRpcVersion = serde_json::from_str(&wire).unwrap();
987        assert_eq!(back, JsonRpcVersion);
988    }
989
990    #[test]
991    fn jsonrpc_version_rejects_wrong_string() {
992        let err = serde_json::from_str::<JsonRpcVersion>(r#""1.0""#)
993            .expect_err("must reject non-\"2.0\"");
994        assert!(err.to_string().contains("\"2.0\""));
995    }
996
997    #[test]
998    fn jsonrpc_id_untagged_roundtrip() {
999        let cases: &[(&str, JsonRpcId)] = &[
1000            ("0", JsonRpcId::I64(0)),
1001            ("-7", JsonRpcId::I64(-7)),
1002            (&i64::MAX.to_string(), JsonRpcId::I64(i64::MAX)),
1003            ("\"abc\"", JsonRpcId::Str("abc".into())),
1004        ];
1005        for (wire, expected) in cases {
1006            let parsed: JsonRpcId = serde_json::from_str(wire).expect(wire);
1007            assert_eq!(&parsed, expected, "round-trip failed for {wire}");
1008        }
1009        // i64::MAX + 1 routes to U64.
1010        let u: JsonRpcId = serde_json::from_str("9223372036854775808").unwrap();
1011        assert_eq!(u, JsonRpcId::U64(9_223_372_036_854_775_808));
1012    }
1013
1014    #[test]
1015    fn response_id_none_serializes_as_json_null() {
1016        let resp = JsonRpcResponse::error(None, -32700, "Parse error", None);
1017        let wire = serde_json::to_string(&resp).unwrap();
1018        assert!(
1019            wire.contains(r#""id":null"#),
1020            "expected id:null in wire form, got: {wire}"
1021        );
1022    }
1023
1024    #[test]
1025    fn response_id_some_serializes_as_value() {
1026        let resp = JsonRpcResponse::success(Some(JsonRpcId::I64(7)), serde_json::json!({}));
1027        let wire = serde_json::to_string(&resp).unwrap();
1028        assert!(wire.contains(r#""id":7"#));
1029    }
1030
1031    #[test]
1032    fn response_meta_management_has_none_workspace_state() {
1033        let meta = ResponseMeta::management("8.0.6");
1034        let wire = serde_json::to_string(&meta).unwrap();
1035        assert!(!wire.contains("workspace_state"), "wire: {wire}");
1036        assert!(wire.contains(r#""stale":false"#));
1037        assert!(wire.contains(r#""daemon_version":"8.0.6""#));
1038    }
1039
1040    #[test]
1041    fn response_meta_loaded_has_loaded_workspace_state() {
1042        let meta = ResponseMeta::loaded("8.0.6");
1043        let wire = serde_json::to_string(&meta).unwrap();
1044        assert!(
1045            wire.contains(r#""workspace_state":"Loaded""#),
1046            "wire: {wire}"
1047        );
1048    }
1049
1050    #[test]
1051    fn response_meta_fresh_from_emits_state() {
1052        let meta = ResponseMeta::fresh_from(WorkspaceState::Loaded, "8.0.6");
1053        let wire = serde_json::to_string(&meta).unwrap();
1054        assert!(
1055            wire.contains(r#""workspace_state":"Loaded""#),
1056            "wire: {wire}"
1057        );
1058        assert!(wire.contains(r#""stale":false"#), "wire: {wire}");
1059        // `last_good_at` / `last_error` are omitted for a Fresh verdict.
1060        assert!(!wire.contains("last_good_at"), "wire: {wire}");
1061        assert!(!wire.contains("last_error"), "wire: {wire}");
1062
1063        // Rebuilding is also a valid Fresh variant per `classify_for_serve`.
1064        let meta_rebuild = ResponseMeta::fresh_from(WorkspaceState::Rebuilding, "8.0.6");
1065        let wire_rebuild = serde_json::to_string(&meta_rebuild).unwrap();
1066        assert!(
1067            wire_rebuild.contains(r#""workspace_state":"Rebuilding""#),
1068            "wire: {wire_rebuild}"
1069        );
1070    }
1071
1072    #[test]
1073    fn response_meta_stale_from_rfc3339_and_workspace_state() {
1074        let anchor =
1075            std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1_760_000_000);
1076        let meta = ResponseMeta::stale_from(anchor, Some("boom".to_owned()), "8.0.6");
1077        let wire = serde_json::to_string(&meta).unwrap();
1078        assert!(wire.contains(r#""stale":true"#), "wire: {wire}");
1079        assert!(
1080            wire.contains(r#""workspace_state":"Failed""#),
1081            "wire: {wire}"
1082        );
1083        assert!(wire.contains(r#""last_error":"boom""#), "wire: {wire}");
1084        // RFC3339 UTC-Zulu — the rendered timestamp must terminate with `Z"`.
1085        let last_good_marker = r#""last_good_at":""#;
1086        let start = wire
1087            .find(last_good_marker)
1088            .unwrap_or_else(|| panic!("missing last_good_at in wire: {wire}"))
1089            + last_good_marker.len();
1090        let rest = &wire[start..];
1091        let end = rest
1092            .find('"')
1093            .expect("last_good_at must be a closed string");
1094        let rfc = &rest[..end];
1095        assert!(rfc.ends_with('Z'), "expected UTC-Zulu, got: {rfc}");
1096        assert!(
1097            rfc.contains('T'),
1098            "RFC3339 must carry a 'T' separator: {rfc}"
1099        );
1100    }
1101
1102    // ------------------------------------------------------------------
1103    // ShimRegisterAck tests (Phase 8c U1 new surface).
1104    // ------------------------------------------------------------------
1105
1106    #[test]
1107    fn shim_register_ack_accepted_omits_reason_on_wire() {
1108        let ack = ShimRegisterAck {
1109            accepted: true,
1110            daemon_version: "8.0.6".to_owned(),
1111            reason: None,
1112            envelope_version: 1,
1113        };
1114        let wire = serde_json::to_string(&ack).unwrap();
1115        assert!(!wire.contains("reason"), "wire: {wire}");
1116        assert!(wire.contains(r#""accepted":true"#), "wire: {wire}");
1117        assert!(wire.contains(r#""daemon_version":"8.0.6""#), "wire: {wire}");
1118        assert!(wire.contains(r#""envelope_version":1"#), "wire: {wire}");
1119    }
1120
1121    #[test]
1122    fn shim_register_ack_rejected_includes_reason() {
1123        let ack = ShimRegisterAck {
1124            accepted: false,
1125            daemon_version: "8.0.6".to_owned(),
1126            reason: Some("cap".to_owned()),
1127            envelope_version: 1,
1128        };
1129        let wire = serde_json::to_string(&ack).unwrap();
1130        assert!(wire.contains(r#""reason":"cap""#), "wire: {wire}");
1131        assert!(wire.contains(r#""accepted":false"#), "wire: {wire}");
1132    }
1133
1134    // ------------------------------------------------------------------
1135    // deny_unknown_fields verification (iter-1 M1 fix).
1136    // ------------------------------------------------------------------
1137
1138    #[test]
1139    fn daemon_hello_rejects_unknown_fields() {
1140        let wire = r#"{"client_version":"x","protocol_version":1,"extra":true}"#;
1141        let err = serde_json::from_str::<DaemonHello>(wire)
1142            .expect_err("DaemonHello must reject unknown fields");
1143        // serde's `deny_unknown_fields` error message contains
1144        // "unknown field" — enough to assert without pinning exact phrasing.
1145        let msg = err.to_string();
1146        assert!(
1147            msg.contains("unknown field"),
1148            "expected 'unknown field' in error, got: {msg}"
1149        );
1150    }
1151
1152    #[test]
1153    fn shim_register_rejects_unknown_fields() {
1154        let wire = r#"{"protocol":"lsp","pid":1,"extra":true}"#;
1155        let err = serde_json::from_str::<ShimRegister>(wire)
1156            .expect_err("ShimRegister must reject unknown fields");
1157        let msg = err.to_string();
1158        assert!(
1159            msg.contains("unknown field"),
1160            "expected 'unknown field' in error, got: {msg}"
1161        );
1162    }
1163
1164    // ── daemon/search (verivus-oss/sqry#238 Tier 2) ─────────────────
1165
1166    #[test]
1167    fn search_request_roundtrip_minimal() {
1168        let req = SearchRequest {
1169            envelope_version: ENVELOPE_VERSION,
1170            pattern: "foo".into(),
1171            search_path: "/tmp/ws".into(),
1172            mode: SearchMode::Exact,
1173            kind: None,
1174            lang: None,
1175            limit: None,
1176            include_generated: true,
1177        };
1178        let wire = serde_json::to_string(&req).expect("serialize");
1179        let back: SearchRequest = serde_json::from_str(&wire).expect("deserialize");
1180        assert_eq!(back, req);
1181    }
1182
1183    #[test]
1184    fn search_request_roundtrip_with_all_filters() {
1185        let req = SearchRequest {
1186            envelope_version: ENVELOPE_VERSION,
1187            pattern: "test_.*".into(),
1188            search_path: "/srv/repo".into(),
1189            mode: SearchMode::Regex,
1190            kind: Some("function".into()),
1191            lang: Some("rust".into()),
1192            limit: Some(50),
1193            include_generated: false,
1194        };
1195        let wire = serde_json::to_string(&req).expect("serialize");
1196        let back: SearchRequest = serde_json::from_str(&wire).expect("deserialize");
1197        assert_eq!(back, req);
1198    }
1199
1200    #[test]
1201    fn search_request_mode_lowercase_on_wire() {
1202        let req = SearchRequest {
1203            envelope_version: ENVELOPE_VERSION,
1204            pattern: "f".into(),
1205            search_path: ".".into(),
1206            mode: SearchMode::Fuzzy,
1207            kind: None,
1208            lang: None,
1209            limit: None,
1210            include_generated: true,
1211        };
1212        let wire = serde_json::to_string(&req).expect("serialize");
1213        // The `#[serde(rename_all = "lowercase")]` attribute on SearchMode
1214        // must surface the variant as `"fuzzy"`, not `"Fuzzy"`.
1215        assert!(
1216            wire.contains(r#""mode":"fuzzy""#),
1217            "expected mode=fuzzy lowercase; got: {wire}"
1218        );
1219    }
1220
1221    #[test]
1222    fn search_request_rejects_unknown_field() {
1223        let wire =
1224            r#"{"envelope_version":1,"pattern":"f","search_path":"/","mode":"exact","bogus":true}"#;
1225        let err = serde_json::from_str::<SearchRequest>(wire)
1226            .expect_err("SearchRequest must reject unknown fields");
1227        assert!(
1228            err.to_string().contains("unknown field"),
1229            "expected unknown-field error, got: {err}"
1230        );
1231    }
1232
1233    #[test]
1234    fn search_request_include_generated_defaults_when_absent() {
1235        // Forward-compat: a request body that predates the
1236        // `include_generated` wire field deserialises to the legacy
1237        // "no filter" behaviour (`true`), preserving the
1238        // `DAEMON_SEARCH_HANDLER` unit's approved tests after the
1239        // Codex round-1 CLI shim review fix.
1240        let wire = r#"{"pattern":"f","search_path":"/","mode":"exact"}"#;
1241        let req: SearchRequest =
1242            serde_json::from_str(wire).expect("deserialize w/o include_generated");
1243        assert!(req.include_generated);
1244    }
1245
1246    #[test]
1247    fn search_request_include_generated_round_trips_when_false() {
1248        let wire = r#"{"pattern":"f","search_path":"/","mode":"exact","include_generated":false}"#;
1249        let req: SearchRequest =
1250            serde_json::from_str(wire).expect("deserialize include_generated=false");
1251        assert!(!req.include_generated);
1252        // Re-serialise and confirm the field is preserved.
1253        let back = serde_json::to_string(&req).expect("serialize");
1254        assert!(
1255            back.contains(r#""include_generated":false"#),
1256            "include_generated=false must survive a round-trip: {back}"
1257        );
1258    }
1259
1260    #[test]
1261    fn search_request_envelope_version_defaults_when_absent() {
1262        // Forward-compat: older clients can omit `envelope_version` and
1263        // the daemon defaults it to ENVELOPE_VERSION.
1264        let wire = r#"{"pattern":"f","search_path":"/","mode":"exact"}"#;
1265        let req: SearchRequest =
1266            serde_json::from_str(wire).expect("deserialize w/o envelope_version");
1267        assert_eq!(req.envelope_version, ENVELOPE_VERSION);
1268    }
1269
1270    #[test]
1271    fn search_result_roundtrip_empty() {
1272        let result = SearchResult {
1273            items: vec![],
1274            total: 0,
1275            truncated: false,
1276            cursor: None,
1277        };
1278        let wire = serde_json::to_string(&result).expect("serialize");
1279        let back: SearchResult = serde_json::from_str(&wire).expect("deserialize");
1280        assert_eq!(back, result);
1281    }
1282
1283    #[test]
1284    fn search_result_roundtrip_single_hit() {
1285        let result = SearchResult {
1286            items: vec![SearchItem {
1287                name: "start_kernel".into(),
1288                qualified_name: "kernel::start_kernel".into(),
1289                kind: "function".into(),
1290                language: "c".into(),
1291                file_path: "/linux/init/main.c".into(),
1292                start_line: 985,
1293                start_column: 0,
1294                end_line: 1100,
1295                end_column: 1,
1296                score: None,
1297            }],
1298            total: 1,
1299            truncated: false,
1300            cursor: None,
1301        };
1302        let wire = serde_json::to_string(&result).expect("serialize");
1303        let back: SearchResult = serde_json::from_str(&wire).expect("deserialize");
1304        assert_eq!(back, result);
1305    }
1306
1307    #[test]
1308    fn search_result_roundtrip_truncated_with_score() {
1309        let result = SearchResult {
1310            items: (0..3)
1311                .map(|i| SearchItem {
1312                    name: format!("hit_{i}"),
1313                    qualified_name: format!("crate::hit_{i}"),
1314                    kind: "function".into(),
1315                    language: "rust".into(),
1316                    file_path: "/repo/src/lib.rs".into(),
1317                    start_line: 10 + i,
1318                    start_column: 0,
1319                    end_line: 12 + i,
1320                    end_column: 1,
1321                    score: Some(0.5_f32 + (i as f32) * 0.1),
1322                })
1323                .collect(),
1324            total: 101,
1325            truncated: true,
1326            cursor: None,
1327        };
1328        let wire = serde_json::to_string(&result).expect("serialize");
1329        let back: SearchResult = serde_json::from_str(&wire).expect("deserialize");
1330        assert_eq!(back, result);
1331    }
1332
1333    #[test]
1334    fn search_item_rejects_unknown_field() {
1335        let wire = r#"{"name":"f","qualified_name":"f","kind":"function","language":"rust","file_path":"/","start_line":1,"start_column":0,"end_line":1,"end_column":0,"bogus":1}"#;
1336        let err = serde_json::from_str::<SearchItem>(wire)
1337            .expect_err("SearchItem must reject unknown fields");
1338        assert!(err.to_string().contains("unknown field"));
1339    }
1340
1341    #[test]
1342    fn search_result_rejects_unknown_field() {
1343        let wire = r#"{"items":[],"total":0,"truncated":false,"bogus":1}"#;
1344        let err = serde_json::from_str::<SearchResult>(wire)
1345            .expect_err("SearchResult must reject unknown fields");
1346        assert!(err.to_string().contains("unknown field"));
1347    }
1348
1349    #[test]
1350    fn search_result_score_omitted_when_none() {
1351        // `skip_serializing_if = "Option::is_none"` on `score` keeps the
1352        // wire compact for regex/exact hits where no score is meaningful.
1353        let item = SearchItem {
1354            name: "f".into(),
1355            qualified_name: "f".into(),
1356            kind: "function".into(),
1357            language: "rust".into(),
1358            file_path: "/".into(),
1359            start_line: 1,
1360            start_column: 0,
1361            end_line: 1,
1362            end_column: 0,
1363            score: None,
1364        };
1365        let wire = serde_json::to_string(&item).expect("serialize");
1366        assert!(
1367            !wire.contains("\"score\""),
1368            "score must be omitted when None; got: {wire}"
1369        );
1370    }
1371
1372    #[test]
1373    fn search_result_envelope_wraps_payload() {
1374        // SearchResult is intended to ride inside ResponseEnvelope<T>, just
1375        // like LoadResult / RebuildResult. Verify the wrap.
1376        let envelope = ResponseEnvelope {
1377            result: SearchResult {
1378                items: vec![],
1379                total: 0,
1380                truncated: false,
1381                cursor: None,
1382            },
1383            meta: ResponseMeta::management("test"),
1384        };
1385        let wire = serde_json::to_string(&envelope).expect("serialize");
1386        let back: ResponseEnvelope<SearchResult> =
1387            serde_json::from_str(&wire).expect("deserialize");
1388        assert_eq!(back.result, envelope.result);
1389    }
1390}