inker 0.1.0

Modular engine/renderer controller — selects and orchestrates content engines (serval HTML lanes, nematic smolweb, scrying/graft/weld surface engines).
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

//! The transclusion resolve pass — `include` fences fetched and spliced
//! (knot evaluation + export plan, K1).
//!
//! Engines stay pure: an ` ```include <url> ` fence parses as an ordinary
//! code block (its body is the authored **fallback**, shown wherever
//! resolution doesn't happen — which makes inert rendering the default
//! everywhere, for free). This pass is the host-driven other half: walk the
//! document, and for each include fence the policy allows, fetch the
//! target, render the response through whatever engine fits, splice the
//! produced blocks in place, and record where every spliced block came
//! from.
//!
//! **Policy is the caller's** (declarative data in, no ambient authority):
//! the host maps a knot's standing — own SelfAsserted note vs received
//! flora — onto a [`TranscludePolicy`]. The pass never fetches more than
//! the policy names, never follows more depth than it allows, and never
//! revisits a URL (cycle guard).
//!
//! Fetch and render arrive as closures so this file stays decoupled from
//! both the network stack and the routing layer; a host typically wraps
//! netfetcher (and, later, the smolweb clients) and its `EngineRegistry`.
//! The closures are synchronous in this first cut — an async host adapts
//! with its own executor at the closure boundary.
//!
//! v1 limit, stated: only top-level fences resolve (an include inside a
//! quote or list stays inert). Per-block provenance indexing is defined on
//! top-level blocks; lifting the limit comes with anchor-based provenance.

use super::block_provenance::{BlockProvenance, BlockProvenanceMap};
use super::{Block, EngineDocument};
use crate::engine::EngineInput;
use std::collections::HashSet;

/// What a fetch closure returns: the body plus the content type the
/// transport reported (`None` lets the renderer sniff).
#[derive(Clone, Debug)]
pub struct Fetched {
    pub content_type: Option<String>,
    pub body: String,
}

/// What the resolve pass may do, as plain data. The host builds one per
/// document from the document's standing (own note vs received) and the
/// user's settings; [`TransclusionPolicy::deny_all`] is the safe floor.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TransclusionPolicy {
    /// Master switch — `false` renders every fence inert (the fallback).
    pub enabled: bool,
    /// URL schemes that may resolve (e.g. `https`, `gemini`). Anything
    /// else is denied with a reason.
    pub allowed_schemes: Vec<String>,
    /// How many include-within-included-content generations may resolve.
    /// `1` resolves only fences authored in the document itself.
    pub max_depth: u8,
}

impl TransclusionPolicy {
    /// The safe floor: nothing resolves. The right default for any
    /// received document until the user consents.
    pub fn deny_all() -> Self {
        Self {
            enabled: false,
            allowed_schemes: Vec::new(),
            max_depth: 0,
        }
    }

    /// A permissive policy for the user's **own** notes, per setting.
    pub fn for_own_notes(allowed_schemes: Vec<String>, max_depth: u8) -> Self {
        Self {
            enabled: true,
            allowed_schemes,
            max_depth,
        }
    }
}

/// What happened, faithfully: counts and reasons, plus the provenance of
/// every spliced block (top-level index → source), for the host to carry
/// the way clips carry theirs.
#[derive(Debug, Default)]
pub struct TranscludeOutcome {
    pub resolved: usize,
    /// `(url, reason)` for fences the policy refused.
    pub denied: Vec<(String, String)>,
    /// `(url, error)` for fences that resolved in policy but failed in
    /// fetch or render. Their fallback stays in place.
    pub failed: Vec<(String, String)>,
    pub provenance: BlockProvenanceMap,
}

/// Parse an include fence's info string: `include <url>` (anything after
/// the URL is reserved). Returns the URL.
pub fn parse_include(language: &str) -> Option<&str> {
    let mut tokens = language.split_whitespace();
    if tokens.next()? != "include" {
        return None;
    }
    tokens.next()
}

fn scheme_of(url: &str) -> Option<&str> {
    url.split_once("://").map(|(scheme, _)| scheme)
}

/// Resolve the document's top-level `include` fences in place. See the
/// module docs for the contract; the outcome reports everything that
/// happened (and didn't).
pub fn resolve_transclusions(
    document: &mut EngineDocument,
    fetch: &mut dyn FnMut(&str) -> Result<Fetched, String>,
    render: &mut dyn FnMut(&EngineInput) -> Result<EngineDocument, String>,
    policy: &TransclusionPolicy,
) -> TranscludeOutcome {
    let mut outcome = TranscludeOutcome::default();
    let mut visited: HashSet<String> = HashSet::new();

    // Each pass resolves the fences currently present; content spliced by
    // pass N is scanned in pass N+1, up to the policy's depth. A disabled
    // policy still gets one scan so every fence's denial is reported.
    let passes = policy.max_depth.max(1);
    for _pass in 0..passes {
        let mut any_resolved = false;
        let old_blocks = std::mem::take(&mut document.blocks);
        let old_provenance = std::mem::replace(&mut outcome.provenance, BlockProvenanceMap::new());
        let mut blocks: Vec<Block> = Vec::with_capacity(old_blocks.len());

        for (old_index, block) in old_blocks.into_iter().enumerate() {
            let new_index = blocks.len();
            // Provenance recorded for this block by an earlier pass moves
            // with it (indices shift as splices land before it).
            let carried = old_provenance.get(old_index).cloned();

            let include_url = match &block {
                Block::CodeBlock {
                    language: Some(language),
                    ..
                } => parse_include(language).map(str::to_string),
                _ => None,
            };

            // Every path that keeps the original block also keeps its
            // carried provenance.
            let keep = |block: Block, blocks: &mut Vec<Block>, outcome: &mut TranscludeOutcome| {
                if let Some(p) = carried.clone() {
                    outcome.provenance.insert(new_index, p);
                }
                blocks.push(block);
            };

            let Some(url) = include_url else {
                keep(block, &mut blocks, &mut outcome);
                continue;
            };

            if !policy.enabled {
                outcome.denied.push((url, "transclusion disabled".into()));
                keep(block, &mut blocks, &mut outcome);
                continue;
            }
            let scheme_allowed = scheme_of(&url)
                .map(|s| policy.allowed_schemes.iter().any(|a| a == s))
                .unwrap_or(false);
            if !scheme_allowed {
                outcome
                    .denied
                    .push((url, "scheme not in the allowlist".into()));
                keep(block, &mut blocks, &mut outcome);
                continue;
            }
            if !visited.insert(url.clone()) {
                outcome
                    .denied
                    .push((url, "already resolved (cycle guard)".into()));
                keep(block, &mut blocks, &mut outcome);
                continue;
            }

            let fetched = match fetch(&url) {
                Ok(fetched) => fetched,
                Err(error) => {
                    outcome.failed.push((url, error));
                    keep(block, &mut blocks, &mut outcome);
                    continue;
                }
            };
            let mut input = EngineInput::new(url.clone(), fetched.body);
            input.content_type = fetched.content_type;
            let child = match render(&input) {
                Ok(child) => child,
                Err(error) => {
                    outcome.failed.push((url, error));
                    keep(block, &mut blocks, &mut outcome);
                    continue;
                }
            };

            let source = BlockProvenance::from_document(child.provenance.clone());
            for (offset, child_block) in child.blocks.into_iter().enumerate() {
                outcome
                    .provenance
                    .insert(new_index + offset, source.clone());
                blocks.push(child_block);
            }
            outcome.resolved += 1;
            any_resolved = true;
        }

        document.blocks = blocks;
        if !any_resolved {
            break;
        }
    }

    outcome
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::document::{DocumentProvenance, DocumentTrustState, InlineSpan};

    fn doc_with(blocks: Vec<Block>) -> EngineDocument {
        EngineDocument {
            address: "test.knot".into(),
            title: None,
            content_type: "text/x-knot".into(),
            lang: None,
            provenance: DocumentProvenance::default(),
            trust: DocumentTrustState::Unknown,
            diagnostics: Vec::new(),
            blocks,
        }
    }

    fn include_fence(url: &str, fallback: &str) -> Block {
        Block::CodeBlock {
            language: Some(format!("include {url}")),
            text: fallback.to_string(),
        }
    }

    /// A render stub: every fetched body becomes one paragraph, and the
    /// child document's provenance carries the input address.
    fn stub_render(input: &EngineInput) -> Result<EngineDocument, String> {
        let mut child = doc_with(vec![Block::Paragraph {
            spans: vec![InlineSpan::Text(input.body.clone())],
        }]);
        child.provenance.canonical_uri = Some(input.address.clone());
        Ok(child)
    }

    fn policy(schemes: &[&str], depth: u8) -> TransclusionPolicy {
        TransclusionPolicy::for_own_notes(schemes.iter().map(|s| s.to_string()).collect(), depth)
    }

    #[test]
    fn an_allowed_include_splices_with_provenance() {
        let mut document = doc_with(vec![
            Block::Paragraph {
                spans: vec![InlineSpan::Text("before".into())],
            },
            include_fence("gemini://x.test/page.gmi", "fallback"),
        ]);
        let mut fetch = |url: &str| {
            assert_eq!(url, "gemini://x.test/page.gmi");
            Ok(Fetched {
                content_type: Some("text/gemini".into()),
                body: "from the capsule".into(),
            })
        };
        let outcome = resolve_transclusions(
            &mut document,
            &mut fetch,
            &mut stub_render,
            &policy(&["gemini"], 1),
        );

        assert_eq!(outcome.resolved, 1);
        assert!(outcome.denied.is_empty() && outcome.failed.is_empty());
        assert_eq!(document.blocks.len(), 2);
        assert!(matches!(
            &document.blocks[1],
            Block::Paragraph { spans } if spans == &vec![InlineSpan::Text("from the capsule".into())]
        ));
        let provenance = outcome
            .provenance
            .get(1)
            .expect("spliced block has provenance");
        assert_eq!(
            provenance.provenance.canonical_uri.as_deref(),
            Some("gemini://x.test/page.gmi")
        );
    }

    #[test]
    fn policy_denies_keep_the_fallback_visible() {
        let mut document = doc_with(vec![include_fence("https://x.test/", "fallback")]);
        let mut fetch = |_: &str| -> Result<Fetched, String> {
            panic!("fetch must not run for a denied fence")
        };

        // Disabled entirely.
        let outcome = resolve_transclusions(
            &mut document,
            &mut fetch,
            &mut stub_render,
            &TransclusionPolicy::deny_all(),
        );
        assert_eq!(outcome.resolved, 0);
        assert!(matches!(&document.blocks[0], Block::CodeBlock { .. }));

        // Enabled but scheme not allowed.
        let outcome = resolve_transclusions(
            &mut document,
            &mut fetch,
            &mut stub_render,
            &policy(&["gemini"], 1),
        );
        assert_eq!(outcome.denied.len(), 1);
        assert!(outcome.denied[0].1.contains("allowlist"));
        assert!(matches!(&document.blocks[0], Block::CodeBlock { .. }));
    }

    #[test]
    fn depth_and_cycles_are_capped() {
        // The fetched content contains another include fence — rendered by
        // a stub that produces a fence pointing back at the SAME url.
        let mut document = doc_with(vec![include_fence("gemini://x.test/a", "")]);
        let mut fetch = |_: &str| {
            Ok(Fetched {
                content_type: None,
                body: "irrelevant".into(),
            })
        };
        let mut render = |input: &EngineInput| -> Result<EngineDocument, String> {
            Ok(doc_with(vec![include_fence("gemini://x.test/a", "")])).map(|mut d| {
                d.provenance.canonical_uri = Some(input.address.clone());
                d
            })
        };
        let outcome = resolve_transclusions(
            &mut document,
            &mut fetch,
            &mut render,
            &policy(&["gemini"], 3),
        );
        assert_eq!(outcome.resolved, 1, "the cycle resolves once");
        assert!(
            outcome.denied.iter().any(|(_, r)| r.contains("cycle")),
            "the second visit is refused: {:?}",
            outcome.denied
        );

        // Depth 1 never scans spliced content for more fences.
        let mut document = doc_with(vec![include_fence("gemini://x.test/b", "")]);
        let mut render_chain = |input: &EngineInput| -> Result<EngineDocument, String> {
            let mut d = doc_with(vec![include_fence("gemini://x.test/c", "")]);
            d.provenance.canonical_uri = Some(input.address.clone());
            Ok(d)
        };
        let outcome = resolve_transclusions(
            &mut document,
            &mut fetch,
            &mut render_chain,
            &policy(&["gemini"], 1),
        );
        assert_eq!(outcome.resolved, 1);
        assert!(
            matches!(&document.blocks[0], Block::CodeBlock { .. }),
            "the nested fence stays inert at depth 1"
        );
    }

    #[test]
    fn fetch_failures_keep_the_fallback_and_report() {
        let mut document = doc_with(vec![include_fence("gemini://down.test/", "fallback")]);
        let mut fetch = |_: &str| -> Result<Fetched, String> { Err("connection refused".into()) };
        let outcome = resolve_transclusions(
            &mut document,
            &mut fetch,
            &mut stub_render,
            &policy(&["gemini"], 2),
        );
        assert_eq!(outcome.failed.len(), 1);
        assert!(matches!(&document.blocks[0], Block::CodeBlock { .. }));
    }

    #[test]
    fn parse_include_is_strict() {
        assert_eq!(
            parse_include("include gemini://x.test/"),
            Some("gemini://x.test/")
        );
        assert_eq!(parse_include("include"), None);
        assert_eq!(parse_include("rust"), None);
        assert_eq!(parse_include("included gemini://x.test/"), None);
    }
}