vole-document 0.1.0-alpha.16

Persistent procedural document runtime: byte-exact reconstruction plus a content-addressed procedural seed DAG, queryable observations with provenance, and selective late materialization.
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
//! The deterministic observation planner (Phase 11.5–11.6, ADR-0026, DEC-9).
//!
//! [`plan`] is a pure function of validated metadata: it reads the field manifest
//! and (read-only) the hierarchical index, decides which frontier the observation
//! will select, and reports what will and will not be materialized. It never
//! materializes seed nodes and never writes to the store, so calling it cannot
//! change the outcome of a subsequent [`crate::field::observe::observe`].
//!
//! Correctness is a precondition, not a cost. Shapes are named, not scored here:
//! the reference implementation has exactly one admissible shape per supported
//! selector/representation pair, and an unsupported pair is a typed error.

use crate::error::{Error, Result};
use crate::field::FieldStore;
use crate::field::document_format::DocumentFormat;
use crate::field::index::{FsIndexStore, IndexEntry, SEL_PAGE, SelectorKey, lookup};
use crate::field::manifest::FieldRoot;
use crate::store::NodeId;

use super::observe::{ObserveRequest, Representation, Selector, derived_nodes};

/// The selected execution shape of an observation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PlanShape {
    /// Answered purely from validated metadata (no node materialization).
    CachedObservation,
    /// Resolved through the hierarchical index to one exact node.
    IndexLookup,
    /// A `SourceSlice` node synthesized on the fly from the descriptor program.
    SourceSlice,
    /// One or more existing seed nodes are materialized as-is.
    NodeMaterialize,
    /// A Stage-C promotion runs first, then the derived node is materialized.
    DeepenThenObserve,
    /// The whole source is materialized (`materialize_exact`).
    FullMaterialize,
}

impl PlanShape {
    /// Stable lower-case name (used in EXPLAIN JSON).
    pub const fn name(self) -> &'static str {
        match self {
            PlanShape::CachedObservation => "cached_observation",
            PlanShape::IndexLookup => "index_lookup",
            PlanShape::SourceSlice => "source_slice",
            PlanShape::NodeMaterialize => "node_materialize",
            PlanShape::DeepenThenObserve => "deepen_then_observe",
            PlanShape::FullMaterialize => "full_materialize",
        }
    }
}

/// The plan for one observation. `required_nodes` and the materialization lists
/// are deterministic estimates derived from the selector and index shape; they do
/// not require loading node bodies.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ObservePlan {
    /// The selected shape.
    pub shape: PlanShape,
    /// Index descents the observation will perform.
    pub index_reads: u64,
    /// Seed nodes the observation is expected to fetch.
    pub required_nodes: u64,
    /// Node kinds / byte classes that will be materialized.
    pub will_materialize: Vec<String>,
    /// Node kinds / byte classes that will not be materialized.
    pub will_not_materialize: Vec<String>,
}

/// Plan an observation. Pure: no materialization and no writes.
pub fn plan(manifest: &FieldRoot, store: &FieldStore, req: &ObserveRequest) -> Result<ObservePlan> {
    // Common (format-neutral) selectors plan through the detected format's
    // capability set, so an unsupported pair fails closed here exactly as it does
    // at evaluation time (Phase 12.7).
    if req.selector.is_common() {
        return common_plan(manifest, req);
    }
    use Representation as R;
    match (&req.selector, req.representation) {
        (Selector::Document, R::FullDocument | R::ExactBytes) => Ok(ObservePlan {
            shape: PlanShape::FullMaterialize,
            index_reads: 0,
            required_nodes: 1,
            will_materialize: kinds(&["DocumentExact"]),
            will_not_materialize: Vec::new(),
        }),
        (Selector::Document, R::Metadata) => Ok(ObservePlan {
            shape: PlanShape::CachedObservation,
            index_reads: 0,
            required_nodes: 0,
            will_materialize: Vec::new(),
            will_not_materialize: kinds(&["whole-document", "seed-nodes"]),
        }),
        (Selector::ByteRange { .. }, R::ExactBytes) => Ok(ObservePlan {
            shape: PlanShape::SourceSlice,
            index_reads: 0,
            required_nodes: 0,
            will_materialize: kinds(&["SourceSlice"]),
            will_not_materialize: kinds(&["whole-document"]),
        }),
        (Selector::Object(_), R::ExactBytes | R::EncodedBytes) => Ok(index_plan("PdfObject")),
        (Selector::Revision(_), R::ExactBytes) => Ok(index_plan("PdfRevision")),
        (Selector::Member(_), R::EncodedBytes) => Ok(ObservePlan {
            shape: PlanShape::IndexLookup,
            index_reads: 1,
            required_nodes: 1,
            will_materialize: kinds(&["PackageMemberRaw"]),
            will_not_materialize: kinds(&["other-members", "whole-document"]),
        }),
        (Selector::Member(_), R::DecodedBytes) => Ok(ObservePlan {
            shape: PlanShape::NodeMaterialize,
            index_reads: 1,
            required_nodes: 2,
            will_materialize: kinds(&["PackageMemberDecoded", "PackageMemberRaw"]),
            will_not_materialize: kinds(&["other-members", "whole-document"]),
        }),
        (Selector::PackagePart(_), R::Metadata) => Ok(ObservePlan {
            shape: PlanShape::DeepenThenObserve,
            index_reads: 1,
            required_nodes: 2,
            will_materialize: kinds(&["PackageOpcModel"]),
            will_not_materialize: kinds(&["other-parts", "whole-document"]),
        }),
        (Selector::PackagePart(_), R::ExactBytes) => Ok(ObservePlan {
            shape: PlanShape::DeepenThenObserve,
            index_reads: 2,
            required_nodes: 3,
            will_materialize: kinds(&["PackageOpcModel", "PackageMemberRaw"]),
            will_not_materialize: kinds(&["other-parts", "whole-document"]),
        }),
        (Selector::PackagePart(_), R::DecodedBytes) => Ok(ObservePlan {
            shape: PlanShape::DeepenThenObserve,
            index_reads: 2,
            required_nodes: 4,
            will_materialize: kinds(&[
                "PackageOpcModel",
                "PackageMemberDecoded",
                "PackageMemberRaw",
            ]),
            will_not_materialize: kinds(&["other-parts", "whole-document"]),
        }),
        (Selector::Relationship(_), R::Metadata) => Ok(ObservePlan {
            shape: PlanShape::DeepenThenObserve,
            index_reads: 1,
            required_nodes: 2,
            will_materialize: kinds(&["PackageOpcModel"]),
            will_not_materialize: kinds(&["other-relationships", "whole-document"]),
        }),
        (Selector::Relationship(_), R::ExactBytes) => Ok(ObservePlan {
            shape: PlanShape::DeepenThenObserve,
            index_reads: 2,
            required_nodes: 3,
            will_materialize: kinds(&["PackageOpcModel", "PackageMemberRaw"]),
            will_not_materialize: kinds(&["external-targets", "whole-document"]),
        }),
        (Selector::Relationship(_), R::DecodedBytes) => Ok(ObservePlan {
            shape: PlanShape::DeepenThenObserve,
            index_reads: 2,
            required_nodes: 4,
            will_materialize: kinds(&[
                "PackageOpcModel",
                "PackageMemberDecoded",
                "PackageMemberRaw",
            ]),
            will_not_materialize: kinds(&["external-targets", "whole-document"]),
        }),
        (Selector::Stream(_), R::EncodedBytes) => Ok(index_plan("PdfStreamEncoded")),
        (Selector::Stream(_), R::DecodedBytes) => Ok(ObservePlan {
            shape: PlanShape::NodeMaterialize,
            index_reads: 1,
            required_nodes: 2,
            will_materialize: kinds(&["PdfStreamDecoded", "PdfStreamEncoded"]),
            will_not_materialize: kinds(&["images", "whole-document"]),
        }),
        (Selector::Stream(_), R::Operators) => Ok(ObservePlan {
            shape: PlanShape::NodeMaterialize,
            index_reads: 1,
            required_nodes: 2,
            will_materialize: kinds(&["PdfStreamDecoded", "ContentOperators"]),
            will_not_materialize: kinds(&["images", "whole-document"]),
        }),
        (Selector::Page(n), R::Text) => page_plan(manifest, store, *n, PageRepr::Text),
        (Selector::Page(n), R::Preview) => page_plan(manifest, store, *n, PageRepr::Preview),
        (Selector::Page(n), R::Structure) => page_plan(manifest, store, *n, PageRepr::Structure),
        (Selector::TextMatch(_), R::Text) => Ok(ObservePlan {
            shape: PlanShape::DeepenThenObserve,
            index_reads: 1,
            required_nodes: 3,
            will_materialize: kinds(&["PageContent", "ContentOperators", "TextRuns"]),
            will_not_materialize: kinds(&["images", "xobjects", "whole-document"]),
        }),
        #[cfg(feature = "docx")]
        (Selector::DocxStory { .. }, R::Text | R::Structure | R::Metadata)
        | (Selector::DocxParagraph { .. }, R::Text | R::Metadata)
        | (Selector::DocxTable { .. }, R::Text | R::Metadata)
        | (Selector::DocxCell { .. }, R::Text | R::Metadata)
        | (Selector::DocxFind { .. }, R::Text) => Ok(ObservePlan {
            shape: PlanShape::DeepenThenObserve,
            index_reads: 3,
            required_nodes: 4,
            will_materialize: kinds(&[
                "DocxModel",
                "DocxStory",
                "PackageMemberDecoded",
                "PackageMemberRaw",
            ]),
            will_not_materialize: kinds(&["other-stories", "whole-document"]),
        }),
        #[cfg(feature = "epub")]
        (Selector::EpubPackage, R::Metadata | R::Structure)
        | (Selector::EpubNav, R::Metadata | R::Structure)
        | (Selector::EpubNavNode { .. }, R::Metadata) => Ok(ObservePlan {
            shape: PlanShape::DeepenThenObserve,
            index_reads: 1,
            required_nodes: 3,
            will_materialize: kinds(&["EpubModel", "PackageMemberDecoded", "PackageMemberRaw"]),
            will_not_materialize: kinds(&["other-resources", "whole-document"]),
        }),
        #[cfg(feature = "epub")]
        (Selector::EpubManifestItem { .. }, R::Metadata)
        | (Selector::EpubSpineItem { .. }, R::Metadata)
        | (Selector::EpubResource(_), R::Metadata) => Ok(ObservePlan {
            shape: PlanShape::DeepenThenObserve,
            index_reads: 1,
            required_nodes: 2,
            will_materialize: kinds(&["EpubModel"]),
            will_not_materialize: kinds(&["external-targets", "whole-document"]),
        }),
        #[cfg(feature = "epub")]
        (Selector::EpubSpineItem { .. }, R::Text | R::Structure | R::Preview)
        | (Selector::EpubBlock { .. }, R::Text | R::Metadata | R::Structure)
        | (Selector::EpubCell { .. }, R::Text | R::Metadata)
        | (Selector::EpubLink { .. }, R::Metadata)
        | (Selector::EpubFind { .. }, R::Text) => Ok(ObservePlan {
            shape: PlanShape::DeepenThenObserve,
            index_reads: 2,
            required_nodes: 4,
            will_materialize: kinds(&[
                "EpubModel",
                "EpubContent",
                "PackageMemberDecoded",
                "PackageMemberRaw",
            ]),
            will_not_materialize: kinds(&["other-spine-items", "whole-document"]),
        }),
        #[cfg(feature = "epub")]
        (Selector::EpubManifestItem { .. }, R::ExactBytes | R::DecodedBytes)
        | (Selector::EpubSpineItem { .. }, R::ExactBytes | R::DecodedBytes)
        | (Selector::EpubResource(_), R::ExactBytes | R::DecodedBytes) => Ok(ObservePlan {
            shape: PlanShape::DeepenThenObserve,
            index_reads: 2,
            required_nodes: 3,
            will_materialize: kinds(&["EpubModel", "PackageMemberRaw", "PackageMemberDecoded"]),
            will_not_materialize: kinds(&["external-targets", "whole-document"]),
        }),
        _ => Err(Error::unsupported_feature(format!(
            "unsupported observation: selector {} with representation {}",
            req.selector.canonical(),
            req.representation.name()
        ))),
    }
}

#[derive(Debug, Clone, Copy)]
enum PageRepr {
    Text,
    Preview,
    Structure,
}

fn kinds(names: &[&str]) -> Vec<String> {
    names.iter().map(|s| (*s).to_string()).collect()
}

fn index_plan(kind: &str) -> ObservePlan {
    ObservePlan {
        shape: PlanShape::IndexLookup,
        index_reads: 1,
        required_nodes: 1,
        will_materialize: kinds(&[kind]),
        will_not_materialize: kinds(&["images", "xobjects", "PageContent", "whole-document"]),
    }
}

/// Plan a common (format-neutral) observation. Pure and capability-checked: an
/// unsupported pair fails closed with the same typed error the evaluator raises.
fn common_plan(manifest: &FieldRoot, req: &ObserveRequest) -> Result<ObservePlan> {
    use crate::field::capabilities;
    let fmt = DocumentFormat::from_provenance(&manifest.provenance).ok_or_else(|| {
        Error::unsupported_feature(
            "field manifest does not record a document format; common observations are unavailable",
        )
    })?;
    if !capabilities::common_supported(fmt, &req.selector, req.representation) {
        return Err(Error::unsupported_feature(format!(
            "unsupported common observation: format {} does not support selector {} with representation {}",
            fmt.name(),
            req.selector.canonical(),
            req.representation.name()
        )));
    }
    // Document metadata is answered from already-validated state; every other
    // common observation materializes the format's model/content closure.
    if matches!(req.selector, Selector::Metadata) {
        return Ok(ObservePlan {
            shape: PlanShape::CachedObservation,
            index_reads: 0,
            required_nodes: 0,
            will_materialize: Vec::new(),
            will_not_materialize: kinds(&["whole-document", "seed-nodes"]),
        });
    }
    Ok(ObservePlan {
        shape: PlanShape::DeepenThenObserve,
        index_reads: 2,
        required_nodes: 4,
        will_materialize: kinds(common_materialize(fmt)),
        will_not_materialize: kinds(&["other-spine-items", "other-stories", "whole-document"]),
    })
}

fn common_materialize(fmt: DocumentFormat) -> &'static [&'static str] {
    match fmt {
        DocumentFormat::Pdf => &["PageContent", "TextRuns"],
        DocumentFormat::Docx => &[
            "DocxModel",
            "DocxStory",
            "PackageMemberDecoded",
            "PackageMemberRaw",
        ],
        DocumentFormat::Epub => &[
            "EpubModel",
            "EpubContent",
            "PackageMemberDecoded",
            "PackageMemberRaw",
        ],
        DocumentFormat::Opaque => &[],
    }
}

fn index_entries(
    manifest: &FieldRoot,
    store: &FieldStore,
    key: SelectorKey,
) -> Result<Vec<IndexEntry>> {
    if !manifest.has_index() {
        return Ok(Vec::new());
    }
    let istore = FsIndexStore::open(store.root())?;
    let root = NodeId::from_bytes(manifest.index_root);
    lookup(&istore, &root, &key)
}

fn page_plan(
    manifest: &FieldRoot,
    store: &FieldStore,
    page: u32,
    repr: PageRepr,
) -> Result<ObservePlan> {
    let entry = index_entries(manifest, store, SelectorKey::new(SEL_PAGE, page))?
        .into_iter()
        .next()
        .ok_or_else(|| {
            Error::unsupported_feature(format!(
                "no page matching selector number {page} in the observation index"
            ))
        })?;
    let (_ops, text, preview) = derived_nodes(page, entry.node_id);
    let target = match repr {
        PageRepr::Text => text,
        PageRepr::Preview | PageRepr::Structure => preview,
    };
    let present = store.seeds().contains_node(&target.content_id())?;
    let shape = if present {
        PlanShape::NodeMaterialize
    } else {
        PlanShape::DeepenThenObserve
    };
    let (materialize, required) = match repr {
        PageRepr::Text => (kinds(&["PageContent", "ContentOperators", "TextRuns"]), 3),
        PageRepr::Preview => (kinds(&["PageContent", "PagePreview"]), 2),
        PageRepr::Structure => (
            kinds(&["PageContent", "PagePreview", "ContentOperators"]),
            3,
        ),
    };
    Ok(ObservePlan {
        shape,
        index_reads: 1,
        required_nodes: required,
        will_materialize: materialize,
        will_not_materialize: kinds(&["images", "xobjects", "whole-document"]),
    })
}

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

    #[test]
    fn shape_names_are_lower_snake_case() {
        for shape in [
            PlanShape::CachedObservation,
            PlanShape::IndexLookup,
            PlanShape::SourceSlice,
            PlanShape::NodeMaterialize,
            PlanShape::DeepenThenObserve,
            PlanShape::FullMaterialize,
        ] {
            let name = shape.name();
            assert!(name.chars().all(|c| c.is_ascii_lowercase() || c == '_'));
        }
    }
}