Skip to main content

onetaskgraph_core/
plan.rs

1//! What the engine did, carried back with every response.
2//!
3//! The point of a capability declaration is that two sources answer the same
4//! query differently and both answers are correct. These types make that visible
5//! instead of leaving a user to guess why one source was fast and another was not:
6//! `--explain` renders a [`QueryPlan`] and `--json` carries it as a field.
7
8use onetaskgraph_plugin_api::{SourceError, SourceName};
9use schemars::JsonSchema;
10use serde::{Deserialize, Serialize};
11
12use crate::engine::{Owed, Resumption, StreamState};
13use crate::failure::{FailureClass, classify};
14
15/// One page of engine output, with the plan that produced it.
16#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
17pub struct QueryResponse<T> {
18    /// This page's items, already qualified and merged across sources.
19    pub items: Vec<T>,
20    /// Where to resume, or `None` when every source is exhausted.
21    pub next: Option<PageToken>,
22    /// What each source was asked to do, and what the engine did instead.
23    pub plan: QueryPlan,
24    /// Sources that failed. One failure never fails the whole query.
25    pub errors: Vec<SourceFailure>,
26}
27
28/// What the engine did, per source.
29#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize, JsonSchema)]
30pub struct QueryPlan {
31    /// One entry per source the query reached.
32    pub per_source: Vec<SourcePlan>,
33}
34
35/// What one source was asked for, and what happened to each predicate.
36#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
37pub struct SourcePlan {
38    /// The configured source this describes.
39    pub source: SourceName,
40    /// The plugin kind behind it.
41    // llmlint: ignore[invalid_states_unrepresentable] SECOND PERMITTED REASON — this restates at a new site the justification already recorded at `Capabilities.max_page_size` (capability.rs) and `PageRequest.limit` (query.rs): `kind: String` is approved contract text. It is also the one field that could not be narrowed even if it were free — a plan must carry the kind a subprocess-hosted plugin reports, an open vocabulary no compile-time type can enumerate.
42    pub kind: String,
43    /// Predicates the source applied itself.
44    ///
45    /// The four predicate vectors below partition one set of outcomes, and nothing in
46    /// the type says so: a `Predicate` could appear in two of them at once, or in none.
47    /// One `Vec<(Predicate, Outcome)>` — or a map keyed by predicate — would make that
48    /// unrepresentable. See the directive below for why it stays as it is.
49    // llmlint: ignore[invalid_states_unrepresentable] SECOND PERMITTED REASON — this
50    // restates at a new site the justification already recorded at
51    // `Capabilities.max_page_size` (capability.rs) and `PageRequest.limit` (query.rs):
52    // `SourcePlan`'s four-vector shape is approved contract text, reproduced field for
53    // field, and `--json` publishes it as the wire format both SDKs are generated from.
54    // Collapsing the four vectors into one outcome-tagged collection is a change to that
55    // contract, which is the contract owner's call and is expressly forbidden to any node
56    // of this plan while other nodes are being written against this text. The finding is
57    // correct and is surfaced as a contract defect rather than dismissed: the contract can
58    // represent a plan its own rules forbid.
59    pub pushed_down: Vec<Predicate>,
60    /// Predicates the engine applied in memory over a wider result set.
61    pub applied_locally: Vec<Predicate>,
62    /// Predicates the engine answered by a bounded scan of the source.
63    pub emulated: Vec<Predicate>,
64    /// Predicates neither side could answer, so the result is unconstrained.
65    ///
66    /// Never [`Predicate::ReverseDependencies`]: `DependencySupport` has no
67    /// unsupported variant, so a reverse-dependency read is answered natively or
68    /// emulated by the engine's bounded scan, never abandoned. The type cannot say
69    /// so — see the directive below.
70    // llmlint: ignore[invalid_states_unrepresentable] SECOND PERMITTED REASON — this
71    // restates at a new site the justification already recorded at
72    // `Capabilities.max_page_size` (capability.rs) and `PageRequest.limit` (query.rs):
73    // `unavailable: Vec<Predicate>` and the `Predicate` enum are both approved contract
74    // text, so a narrower element type here is the contract owner's call, not this
75    // crate's. The finding is correct and is being surfaced as a contract defect rather
76    // than dismissed: the contract can express a state its own rules forbid.
77    pub unavailable: Vec<Predicate>,
78    /// How many pages the engine pulled from this source to answer.
79    pub pages_fetched: u32,
80}
81
82/// One thing a query can ask of a source.
83#[derive(
84    Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, JsonSchema,
85)]
86#[serde(rename_all = "kebab-case")]
87pub enum Predicate {
88    /// Filter by label name.
89    Label,
90    /// Filter by status category.
91    Status,
92    /// Filter by priority.
93    Priority,
94    /// Search titles.
95    SearchTitle,
96    /// Search bodies.
97    SearchContent,
98    /// Filter by owning project.
99    Project,
100    /// Read the source's documents.
101    ///
102    /// Not a filter, and reported only as [`unavailable`](SourcePlan::unavailable): a
103    /// source declaring it has no documents contributes no document rows and there is
104    /// nothing for the engine to narrow, which is the same shape `Project` takes for a
105    /// source with no project table.
106    Document,
107    /// Walk dependency edges backwards.
108    ReverseDependencies,
109}
110
111/// One source's failure, kept beside the results the other sources returned.
112///
113/// Written with a `class` beside the error, which is not a field here: it is computed by
114/// [`classify`](crate::failure::classify) as the entry is written, so it cannot disagree
115/// with the error it classifies, and a document read back in has it recomputed rather than
116/// trusted.
117#[derive(Debug, Clone, PartialEq, Deserialize)]
118pub struct SourceFailure {
119    /// The source that failed.
120    pub source: SourceName,
121    /// Why.
122    pub error: SourceError,
123}
124
125// A `SourceFailure` as it is written: the failure, and its class. Its doc comment is the
126// schema's description, which is why it is `SourceFailure`'s own sentence.
127/// One source's failure, kept beside the results the other sources returned.
128#[derive(Serialize, JsonSchema)]
129#[schemars(rename = "SourceFailure")]
130struct ClassifiedSourceFailure<'a> {
131    /// The source that failed.
132    source: &'a SourceName,
133    /// Why.
134    error: &'a SourceError,
135    /// Whether repeating the request unchanged could change this source's answer.
136    class: FailureClass,
137}
138
139impl<'a> From<&'a SourceFailure> for ClassifiedSourceFailure<'a> {
140    fn from(failure: &'a SourceFailure) -> Self {
141        Self {
142            source: &failure.source,
143            error: &failure.error,
144            class: classify(Some(&failure.error)),
145        }
146    }
147}
148
149impl Serialize for SourceFailure {
150    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
151        ClassifiedSourceFailure::from(self).serialize(serializer)
152    }
153}
154
155impl JsonSchema for SourceFailure {
156    fn schema_name() -> std::borrow::Cow<'static, str> {
157        ClassifiedSourceFailure::schema_name()
158    }
159
160    fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
161        ClassifiedSourceFailure::json_schema(generator)
162    }
163}
164
165/// The engine's own resume token: one plugin cursor per source stream, opaque to the
166/// caller exactly as a plugin's cursor is opaque to the engine.
167///
168/// Rendered as lower-case hex, which is not obfuscation — the inside is not a secret —
169/// but the one property a token a person copies off a terminal has to have: it survives
170/// a shell. The document underneath holds a plugin's own cursor, and a cursor may hold
171/// anything at all, so a token spelled as the raw JSON would carry quotes, braces and
172/// spaces straight into the next command line. Hex has no character a shell reads.
173///
174/// # What a token is and is not checked for
175///
176/// Both ways in go through [`parse`](Self::parse) — including deserialising one — and
177/// what that establishes is **structural**: the string is hex, the bytes are this
178/// engine's own resume document, and every state in it is well formed. It does not, and
179/// cannot, establish that this engine is the one that wrote it. A token is not a
180/// credential and carries nothing secret; forging one buys a caller nothing they could
181/// not have asked for outright, since every cursor inside is handed straight back to the
182/// source that issued it and is validated there.
183///
184/// What a forged token *could* do is name a stream this configuration has no source for,
185/// or resume further into a page than the engine ever pages. Both are refused where the
186/// token meets the query it is resuming, by
187/// [`Engine`](crate::Engine) — see `EngineError::Token` — because only the engine knows
188/// which sources are configured and what page ceiling each declares.
189#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
190#[serde(try_from = "String", into = "String")]
191pub struct PageToken(String);
192
193impl PageToken {
194    /// Encode where every stream still walking picks up.
195    ///
196    /// Crate-private on purpose: what a token *means* is the engine's, and a caller able
197    /// to build one from parts could name a stream no query addressed. A caller with a
198    /// token in hand reaches it through [`parse`](Self::parse) instead, which checks its
199    /// structure — see this type's own note for what that does and does not establish.
200    ///
201    /// Infallible: a stream state is a source name, a stream kind, an optional cursor
202    /// and a count, and none of those can fail to serialise.
203    ///
204    /// Only ever reached with at least one stream, because a walk with nothing left to
205    /// resume reports no token at all — which is why [`parse`](Self::parse) refuses an
206    /// empty one.
207    pub(crate) fn encode(query: &str, owed: Option<Owed>, streams: &[StreamState]) -> Self {
208        let document = serde_json::to_string(&Resumption {
209            query: query.to_owned(),
210            owed,
211            streams: streams.to_vec(),
212        })
213        .expect("a resumption is plain data and always serialises");
214        Self(to_hex(&document))
215    }
216
217    /// Accept a token from a caller — a `--page` argument, or a deserialised response —
218    /// refusing anything that is not this engine's own resume document.
219    ///
220    /// Structural only, deliberately: see the type's own note for what this establishes
221    /// and what [`Engine`](crate::Engine) checks instead.
222    ///
223    /// # Errors
224    ///
225    /// Returns [`SourceError::Malformed`] when `raw` is not hex, is not this engine's
226    /// document, or holds a state that is not well formed.
227    pub fn parse(raw: impl Into<String>) -> Result<Self, SourceError> {
228        let token = Self(raw.into());
229        token.resumption()?;
230        Ok(token)
231    }
232
233    /// Borrow the underlying token.
234    #[must_use]
235    pub fn as_str(&self) -> &str {
236        &self.0
237    }
238
239    /// Where each stream claims to pick up.
240    ///
241    /// Infallible, and that is a property of the type rather than an assumption: the only
242    /// two ways to obtain a `PageToken` are [`encode`](Self::encode), which built this
243    /// document, and [`parse`](Self::parse), which refuses anything that does not decode
244    /// — and deserialising one goes through `parse`. So a token that does not decode
245    /// never exists to be read here. Whether what it *says* is usable against the query
246    /// being resumed is the engine's to decide, not this type's.
247    pub(crate) fn decode(&self) -> Resumption {
248        self.resumption()
249            .expect("every way to build a PageToken validates it")
250    }
251
252    /// The document inside, or why this is not one of this engine's tokens.
253    fn resumption(&self) -> Result<Resumption, SourceError> {
254        let document = from_hex(&self.0).ok_or_else(|| SourceError::Malformed {
255            message: "that is not a page token this engine writes: it is not even hex".to_owned(),
256        })?;
257        let resumption: Resumption =
258            serde_json::from_str(&document).map_err(|error| SourceError::Malformed {
259                message: format!("that is not a page token this engine writes: {error}"),
260            })?;
261        let streams = &resumption.streams;
262        // A token with nothing to resume is one this engine never writes: `encode` is
263        // reached only while at least one stream still has rows to give, and a walk with
264        // none reports no token at all. Accepting one would answer an empty page and exit
265        // zero, which reads as a walk that ended rather than as the mistake it is.
266        if streams.is_empty() {
267            return Err(SourceError::Malformed {
268                message: "that is not a page token this engine writes: it resumes nothing"
269                    .to_owned(),
270            });
271        }
272        Ok(resumption)
273    }
274}
275
276/// Deserialising a token goes through [`PageToken::parse`], so a response carrying
277/// a token this engine never issued is refused where it is read.
278impl TryFrom<String> for PageToken {
279    type Error = SourceError;
280
281    fn try_from(value: String) -> Result<Self, Self::Error> {
282        Self::parse(value)
283    }
284}
285
286/// A token is its string, so serialising one is that string and nothing else — the
287/// checking all happens on the way in, where a caller's input is.
288impl From<PageToken> for String {
289    fn from(value: PageToken) -> Self {
290        value.0
291    }
292}
293
294impl std::fmt::Display for PageToken {
295    /// The opaque string a caller passes back as `--page`.
296    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
297        f.write_str(&self.0)
298    }
299}
300
301/// Render `document` as lower-case hex.
302fn to_hex(document: &str) -> String {
303    let mut rendered = String::with_capacity(document.len() * 2);
304    for byte in document.as_bytes() {
305        rendered.push(nibble(byte >> 4));
306        rendered.push(nibble(byte & 0x0f));
307    }
308    rendered
309}
310
311fn nibble(value: u8) -> char {
312    char::from_digit(u32::from(value), 16).expect("a nibble is a hex digit")
313}
314
315/// Read hex back, or `None` when `raw` is not hex of valid UTF-8.
316fn from_hex(raw: &str) -> Option<String> {
317    if !raw.len().is_multiple_of(2) {
318        return None;
319    }
320    let digits: Vec<u8> = raw
321        .chars()
322        .map(|digit| digit.to_digit(16))
323        .collect::<Option<Vec<u32>>>()?
324        .into_iter()
325        .map(|digit| u8::try_from(digit).expect("a hex digit fits in a byte"))
326        .collect();
327    let bytes: Vec<u8> = digits
328        .chunks(2)
329        .map(|pair| (pair[0] << 4) | pair[1])
330        .collect();
331    String::from_utf8(bytes).ok()
332}