onetaskgraph-core 0.3.0

The onetaskgraph engine: the plugin registry, global-id qualification, and the plan every response carries.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
//! Tasks and documents created from a body or a template, and regenerated in place.
//!
//! An item created or regenerated from a template records where it came from under the
//! reserved `onetaskgraph.template` key ([`TemplateProvenance`]); one created from a plain
//! body records none. The answers a regenerate needs are kept only where a source keeps them
//! beside the item — `local-md`'s own file — and never in the item's content or metadata, so
//! nothing of them is duplicated into an issue body. Every write here lands the content, the
//! provenance and those answers in **one** call to the source, which the source makes one
//! write.
//!
//! Regeneration is this engine's, and it never resolves a caller's layers or runs a caller's
//! command: the template is the one given — a file, or a loader document a caller states —
//! else the recorded `template` when that is a readable file, else the regenerate is refused
//! naming the recorded reference.

use std::collections::BTreeMap;
use std::fmt;
use std::path::{Path, PathBuf};

use onetaskgraph_plugin_api::{
    DependencyEdge, DependencyEndpoint, DependencyKind, Document, ItemKind, ItemWrite, Label,
    MetadataKey, MetadataRecord, NativeId, Priority, Repository, SourceName, Status,
    StatusCategory, Task, TaskRef,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use super::delivery::{qualified_task, source_failed, targets};
use super::{Delivered, Engine, EngineError, Qualified};
use crate::GlobalId;
use crate::resolve::ResolvedSource;
use crate::template::{
    Answers, RenderedTemplate, Sha256Digest, Template, TemplateError, TemplateInput,
    TemplateProvenance,
};

/// The content a create writes: text given as it is, or a template's rendering.
///
/// Built only by [`Body::plain`], [`Body::rendered`] and [`Body::rendered_as`], so a rendered
/// body always carries the [`TemplateProvenance`] its rendering proves — a rendering whose
/// digest is not one is refused where the body is built, never later where it is written.
#[derive(Debug, Clone, PartialEq)]
pub struct Body(Content);

#[derive(Debug, Clone, PartialEq)]
enum Content {
    /// The item records no provenance and no answers are kept.
    Plain(String),
    /// The item records `provenance`, and a source that keeps answers keeps the rendering's
    /// resolved answers beside it.
    Rendered {
        rendered: RenderedTemplate,
        provenance: TemplateProvenance,
    },
}

impl Body {
    /// Text given as it is.
    #[must_use]
    pub fn plain(text: impl Into<String>) -> Self {
        Self(Content::Plain(text.into()))
    }

    /// A template's rendering, recorded by the reference `template` answers.
    ///
    /// # Errors
    ///
    /// As [`Body::rendered_as`].
    pub fn rendered(
        template: &TemplateInput,
        rendered: RenderedTemplate,
    ) -> Result<Self, TemplateError> {
        Self::rendered_as(template.reference(), rendered)
    }

    /// A template's rendering, recorded by `template` — a string of a library caller's own.
    ///
    /// # Errors
    ///
    /// [`TemplateError::Malformed`] when the rendering's `digest` is not one: a
    /// [`RenderedTemplate`] assembled by hand rather than answered by a render.
    pub fn rendered_as(
        template: impl Into<String>,
        rendered: RenderedTemplate,
    ) -> Result<Self, TemplateError> {
        let provenance = TemplateProvenance::of(template, &rendered)?;
        Ok(Self(Content::Rendered {
            rendered,
            provenance,
        }))
    }

    /// What a create writes of this body beside `metadata`.
    fn parts(&self, metadata: &BTreeMap<MetadataKey, Value>) -> Parts<'_> {
        let mut carried: BTreeMap<String, Value> = metadata
            .iter()
            .map(|(key, value)| (key.as_str().to_owned(), value.clone()))
            .collect();
        match &self.0 {
            Content::Plain(content) => Parts {
                content: content.clone(),
                metadata: carried,
                answers: None,
            },
            Content::Rendered {
                rendered,
                provenance,
            } => {
                carried.insert(TemplateProvenance::KEY.to_owned(), provenance.to_value());
                Parts {
                    content: rendered.body.clone(),
                    metadata: carried,
                    answers: Some(&rendered.answers),
                }
            }
        }
    }
}

/// What a create writes: the content, the metadata the item carries — the caller's keys and,
/// for a rendering, its provenance — and the answers a source that keeps them keeps beside it.
struct Parts<'a> {
    content: String,
    metadata: BTreeMap<String, Value>,
    answers: Option<&'a BTreeMap<String, Value>>,
}

/// One task to create in one source.
#[derive(Debug, Clone, PartialEq)]
pub struct TaskCreate {
    /// The configured source to create it in.
    pub source: SourceName,
    /// The project it is filed under, by that source's own id.
    pub project: NativeId,
    /// Its title.
    pub title: String,
    /// Its content.
    pub body: Body,
    /// Its status category; `todo` when none is given.
    pub status: Option<StatusCategory>,
    /// The names of its labels.
    pub labels: Vec<String>,
    /// The repositories its work changes.
    pub repositories: Vec<Repository>,
    /// The tasks it depends on — its own source's written without their source, any other's
    /// qualified.
    pub depends_on: Vec<GlobalId>,
    /// The tasks it delivers. Each is kept in step with it once it lands, as every write of a
    /// task that delivers keeps them.
    pub delivers: Vec<GlobalId>,
    /// The caller's own metadata keys — never one this product reserves, which a
    /// [`MetadataKey`] cannot name.
    pub metadata: BTreeMap<MetadataKey, Value>,
}

/// One project document to create, or replace, in one source.
#[derive(Debug, Clone, PartialEq)]
pub struct DocumentCreate {
    /// The configured source to write it in.
    pub source: SourceName,
    /// The project it is filed under, by that source's own id.
    pub project: NativeId,
    /// Its title.
    pub title: String,
    /// The id to write it under: a document the source already holds by this id is
    /// replaced, and otherwise one is created under it where the source lets a caller name
    /// what it creates. `None` creates one under an id derived from the title.
    pub id: Option<NativeId>,
    /// Its content.
    pub body: Body,
    /// The names of its labels.
    pub labels: Vec<String>,
    /// The repositories it concerns.
    pub repositories: Vec<Repository>,
    /// The caller's own metadata keys.
    pub metadata: BTreeMap<MetadataKey, Value>,
}

/// What creating a task came to: the task as its source reads it back, and every task it
/// delivers, kept in step with it.
#[derive(Debug, Clone, PartialEq)]
pub struct TaskCreated {
    /// The task, under its qualified id.
    pub task: Qualified<Task>,
    /// One entry per task it delivers; empty when it delivers none.
    pub delivered: Vec<Delivered>,
}

/// Which kind of item a regenerate or an answers read names.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RenderedRecord {
    /// A task.
    Task,
    /// A project document.
    Document,
}

impl fmt::Display for RenderedRecord {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.noun())
    }
}

impl RenderedRecord {
    fn noun(self) -> &'static str {
        match self {
            Self::Task => "task",
            Self::Document => "document",
        }
    }

    fn no_such(self, id: &GlobalId) -> EngineError {
        match self {
            Self::Task => EngineError::NoSuchTask { id: id.to_string() },
            Self::Document => EngineError::NoSuchDocument { id: id.to_string() },
        }
    }
}

/// Which template a regenerate renders.
///
/// One of two, rather than an optional template beside a search path: a search path means
/// something only for the recorded file, and a given template carries its own.
#[derive(Debug, Clone, PartialEq)]
pub enum RenderTemplate {
    /// The template given: a file with the directories its chain resolves over, or a loader
    /// document.
    Given(TemplateInput),
    /// The `template` the item records, when that is a readable file, its chain resolved over
    /// `search_path`. A recorded reference that is not a file is refused, never resolved.
    Recorded {
        /// The directories the recorded file's chain resolves over.
        search_path: Vec<PathBuf>,
    },
}

impl Default for RenderTemplate {
    fn default() -> Self {
        Self::Recorded {
            search_path: Vec::new(),
        }
    }
}

/// What `task render` and `document render` are asked.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct RenderRequest {
    /// The template to render.
    pub template: RenderTemplate,
    /// The answers laid over the base: the stored ones when they are in step with the item's
    /// provenance, none otherwise. An unset name drops its answer.
    pub answers: Answers,
    /// Read and render everything, and write nothing.
    pub dry_run: bool,
}

/// What `task render` and `document render` answer with.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct Regenerated {
    /// The item regenerated.
    pub id: GlobalId,
    /// The chain digest it rendered with.
    pub digest: Sha256Digest,
    /// The SHA-256 of the rendered content: what its provenance now records.
    pub body_digest: Sha256Digest,
    /// Whether its content, its provenance or its stored answers differ from what it held —
    /// what a write changed, or for a dry run what one would change.
    pub changed: bool,
    /// The rendered content.
    pub body: String,
}

/// What `task answers` and `document answers` answer with: the resolved answers an item was
/// last rendered from — defaults applied, `null` for an optional variable given neither — by
/// variable name, exactly as its source keeps them.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(transparent)]
pub struct TemplateAnswers(pub BTreeMap<String, Value>);

impl TemplateAnswers {
    /// The answers as a YAML mapping — the document an answers file holds, so what this
    /// prints can be handed back with `--answers`.
    ///
    /// # Errors
    ///
    /// Why the answers could not be written as YAML.
    pub fn to_yaml(&self) -> Result<String, String> {
        serde_norway::to_string(&self.0).map_err(|error| error.to_string())
    }
}

/// Why a regenerate did not start from the answers stored beside the item.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UnusedAnswers {
    /// The item records no template provenance, so nothing says which answers are its.
    NoProvenance,
    /// Its source keeps no answers beside it — it keeps none at all, or none for this item.
    NoneStored {
        /// The configured name of its source.
        source: String,
    },
    /// The answers stored beside it do not hash to the `answers_digest` its provenance
    /// records.
    OutOfStep,
    /// Its `onetaskgraph.template` entry is not one this product writes, so nothing trusted says
    /// which answers are its.
    MalformedProvenance {
        /// What is wrong with the entry.
        problem: String,
    },
}

impl fmt::Display for UnusedAnswers {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NoProvenance => formatter.write_str(
                "it records no template provenance, so no stored answers are known to be its",
            ),
            Self::NoneStored { source } => write!(
                formatter,
                "source {source} holds no stored answers for it — only a source that keeps an \
                 authoring file, such as local-md, stores them"
            ),
            Self::OutOfStep => formatter.write_str(
                "the answers stored beside it do not hash to the answers_digest its provenance \
                 records, so they changed after it was rendered and are not trusted",
            ),
            Self::MalformedProvenance { problem } => write!(
                formatter,
                "{problem}, so nothing trusted says which stored answers are its"
            ),
        }
    }
}

/// Everything a regenerate reads before it renders: the item's own content and provenance,
/// the template, and the answers a render starts from.
///
/// [`Engine::regeneration`] answers one and [`Engine::regenerate`] renders and writes it — the
/// two halves apart so a caller that asks for answers, as the command line does when
/// interactive, can ask between them. [`Engine::render_task`] and
/// [`Engine::render_document`] are the two together.
#[derive(Debug, Clone)]
pub struct Regeneration {
    id: GlobalId,
    record: RenderedRecord,
    template: Template,
    reference: String,
    base: Answers,
    stored: Stored,
    /// Whether its source keeps answers beside it, so that answers it does not hold are a
    /// difference a write repairs rather than the source's nature.
    keeps_answers: bool,
    content: String,
    provenance: Option<TemplateProvenance>,
}

/// The answers stored beside an item, and whether a render starts from them — one or the
/// other, never both: answers that are the base are in step with the provenance, and answers
/// that are not carry why.
#[derive(Debug, Clone)]
enum Stored {
    /// In step with the item's provenance, so a render starts from them.
    Trusted(BTreeMap<String, Value>),
    /// Not a base, for `reason`. What is held, if anything, is still what a render's own
    /// answers are compared with to say whether the item changed.
    Unused {
        reason: UnusedAnswers,
        held: Option<BTreeMap<String, Value>>,
    },
}

impl Stored {
    /// Whatever answers the item holds, trusted or not.
    fn held(&self) -> Option<&BTreeMap<String, Value>> {
        match self {
            Self::Trusted(answers) => Some(answers),
            Self::Unused { held, .. } => held.as_ref(),
        }
    }
}

impl Regeneration {
    /// The item being regenerated.
    #[must_use]
    pub fn id(&self) -> &GlobalId {
        &self.id
    }

    /// The template it renders.
    #[must_use]
    pub fn template(&self) -> &Template {
        &self.template
    }

    /// The answers a render starts from, before any new ones are laid over them.
    #[must_use]
    pub fn base(&self) -> &Answers {
        &self.base
    }

    /// Every variable the stored answers settle when they are the base — an optional one
    /// they left `null` included, which a render leaves unanswered again — and none when
    /// they are not.
    pub fn settled(&self) -> impl Iterator<Item = &str> {
        let trusted = match &self.stored {
            Stored::Trusted(answers) => Some(answers),
            Stored::Unused { .. } => None,
        };
        trusted
            .into_iter()
            .flat_map(BTreeMap::keys)
            .map(String::as_str)
    }

    /// Why the stored answers were not the base, when they were not.
    #[must_use]
    pub fn unused(&self) -> Option<&UnusedAnswers> {
        match &self.stored {
            Stored::Trusted(_) => None,
            Stored::Unused { reason, .. } => Some(reason),
        }
    }

    /// Render from the base with `answers` laid over it.
    ///
    /// A stored answer to a variable the template no longer declares is dropped rather than
    /// refused: it was this product's to keep, not the caller's to answer for.
    ///
    /// # Errors
    ///
    /// [`EngineError::MissingAnswers`] naming every required variable left unanswered and
    /// why the stored answers were not used; [`EngineError::Template`] for any other refusal
    /// of the answers or of the template.
    pub fn render(&self, answers: &Answers) -> Result<RenderedTemplate, EngineError> {
        let mut base = self.base.clone();
        loop {
            match self.template.render(&base.overlay(answers)) {
                Err(TemplateError::UnknownAnswer { names })
                    if names.iter().all(|name| {
                        base.names().any(|held| held == name)
                            && !answers.names().any(|given| given == name)
                    }) =>
                {
                    for name in names {
                        base.unset(name);
                    }
                }
                Err(TemplateError::MissingRequired { names }) => {
                    return Err(EngineError::MissingAnswers {
                        id: self.id.to_string(),
                        names,
                        reason: self.unused().map_or_else(
                            || {
                                "the stored answers were used and do not answer them: an \
                                 answer unset falls back to its default, and these have none"
                                    .to_owned()
                            },
                            |unused| format!("the stored answers were not used because {unused}"),
                        ),
                    });
                }
                Err(error) => return Err(EngineError::Template { error }),
                Ok(rendered) => return Ok(rendered),
            }
        }
    }
}

impl Engine {
    /// Create one task, from a plain body or a template's rendering, and keep every task it
    /// delivers in step with it.
    ///
    /// The content, the provenance and — where the source keeps them — the answers land in
    /// one write. A delivered task that cannot be kept in step is not an error: it is
    /// reported `failed` beside the task that was created.
    ///
    /// # Errors
    ///
    /// [`EngineError::UnknownSource`] and [`EngineError::SourceUnavailable`] for a source that
    /// cannot be reached, [`EngineError::NotCreatable`] for one with no write side — neither is
    /// written — and [`EngineError::SourceFailed`] when the source refuses the task.
    pub async fn create_task(&self, request: &TaskCreate) -> Result<TaskCreated, EngineError> {
        // A task goes where its repositories route it from the source named. Routed away, it
        // is filed under the named project's member project in the source it lands in.
        let placement = self.place(&request.source, &request.repositories);
        let near = &placement.destination;
        let source = self.creatable(near, MetadataRecord::Task)?;
        let filed = if near == &request.source {
            None
        } else {
            let home = GlobalId::new(request.source.clone(), request.project.clone());
            self.creatable(&request.source, MetadataRecord::Task)?;
            let filed = self.member_project(&home, near).await?;
            if filed.project.is_none() {
                return Err(EngineError::NoSuchProject {
                    id: home.to_string(),
                });
            }
            Some(filed)
        };
        let project = filed
            .as_ref()
            .and_then(|filed| filed.project.clone())
            .unwrap_or_else(|| request.project.clone());
        let Parts {
            content,
            metadata,
            answers,
        } = request.body.parts(&request.metadata);
        let category = request.status.unwrap_or(StatusCategory::Todo);
        let id = NativeId::from(slug(&request.title, "task").as_str());
        let depends_on = request
            .depends_on
            .iter()
            .map(|far| DependencyEdge {
                from: DependencyEndpoint::from_native(id.clone(), ItemKind::Task),
                to: endpoint(far, near),
                kind: DependencyKind::Blocks,
            })
            .collect();
        let write = ItemWrite {
            target: None,
            item: Task {
                id,
                key: None,
                title: request.title.clone(),
                content: Some(content),
                status: Status {
                    category,
                    name: category_word(category),
                },
                priority: Priority::None,
                labels: labels(&request.labels),
                project: Some(project),
                url: None,
                location: None,
                created_at: None,
                updated_at: None,
                metadata,
                repositories: request.repositories.clone(),
                delivers: request
                    .delivers
                    .iter()
                    .map(|task| TaskRef::qualified(&task.source, &task.native))
                    .collect(),
                delivered_by: Vec::new(),
            },
            depends_on,
        };
        let written = match match answers {
            Some(answers) => source.source().write_task_rendered(&write, answers).await,
            None => source.source().write_task(&write).await,
        } {
            Ok(written) => written,
            Err(error) => {
                let error = source_failed(source, error);
                return Err(match filed {
                    Some(filed) => self.unfile(filed, error).await,
                    None => error,
                });
            }
        };
        let id = GlobalId::new(near.clone(), written);
        let task = source
            .source()
            .get_task(&id.native)
            .await
            .map_err(|error| source_failed(source, error))?
            .ok_or_else(|| EngineError::NoSuchTask { id: id.to_string() })?;
        let delivers = targets(&task.delivers, near);
        let delivered = self
            .deliver(&id, task.status.category, &delivers, &[])
            .await;
        Ok(TaskCreated {
            task: qualified_task(id, task),
            delivered,
        })
    }

    /// Create one project document, or replace the one the source holds under
    /// [`DocumentCreate::id`], from a plain body or a template's rendering.
    ///
    /// # Errors
    ///
    /// As [`create_task`](Self::create_task), and [`EngineError::NoDocuments`] for a source
    /// declaring it has none, which is not asked.
    pub async fn create_document(
        &self,
        request: &DocumentCreate,
    ) -> Result<Qualified<Document>, EngineError> {
        let source = self.creatable(&request.source, MetadataRecord::Document)?;
        documentary(source)?;
        let target = match &request.id {
            Some(id) => source
                .source()
                .get_document(id)
                .await
                .map_err(|error| source_failed(source, error))?
                .map(|held| held.id),
            None => None,
        };
        let Parts {
            content,
            metadata,
            answers,
        } = request.body.parts(&request.metadata);
        let write = ItemWrite {
            target,
            item: Document {
                id: request
                    .id
                    .clone()
                    .unwrap_or_else(|| NativeId::from(slug(&request.title, "document").as_str())),
                title: request.title.clone(),
                content: Some(content),
                project: Some(request.project.clone()),
                labels: labels(&request.labels),
                url: None,
                location: None,
                created_at: None,
                updated_at: None,
                metadata,
                repositories: request.repositories.clone(),
            },
            depends_on: Vec::new(),
        };
        let written = match answers {
            Some(answers) => {
                source
                    .source()
                    .write_document_rendered(&write, answers)
                    .await
            }
            None => source.source().write_document(&write).await,
        }
        .map_err(|error| source_failed(source, error))?;
        let id = GlobalId::new(request.source.clone(), written);
        let document = source
            .source()
            .get_document(&id.native)
            .await
            .map_err(|error| source_failed(source, error))?
            .ok_or_else(|| EngineError::NoSuchDocument { id: id.to_string() })?;
        Ok(Qualified { id, item: document })
    }

    /// The answers the task or document `id` was last rendered from, as its source keeps them.
    ///
    /// # Errors
    ///
    /// [`EngineError::NoSuchTask`] or [`EngineError::NoSuchDocument`] when the item is not
    /// there, [`EngineError::NoStoredAnswers`] naming it when none are stored for it — which
    /// is every item of a source that keeps none — and [`EngineError::SourceFailed`] when the
    /// source cannot answer.
    pub async fn template_answers(
        &self,
        record: RenderedRecord,
        id: &GlobalId,
    ) -> Result<TemplateAnswers, EngineError> {
        let source = self.built(&id.source)?;
        if record == RenderedRecord::Document {
            documentary(source)?;
        }
        self.read_item(source, record, id).await?;
        self.stored(source, record, id)
            .await?
            .map(TemplateAnswers)
            .ok_or_else(|| EngineError::NoStoredAnswers {
                record,
                id: id.to_string(),
                reason: UnusedAnswers::NoneStored {
                    source: id.source.to_string(),
                }
                .to_string(),
            })
    }

    /// Read everything a regenerate of the task or document `id` needs, before it renders.
    ///
    /// # Errors
    ///
    /// [`EngineError::NoSuchTask`] or [`EngineError::NoSuchDocument`] when the item is not
    /// there; [`EngineError::NoTemplate`] when no template is given and the item records
    /// none; [`EngineError::MalformedProvenance`] when no template is given and the entry it
    /// records is not one this product writes; [`EngineError::TemplateNotAFile`] when no template is given and the one it
    /// records is not a readable file, which only a loader document can then stand in for;
    /// [`EngineError::Template`] when the template cannot be loaded; and the refusals of a
    /// source that cannot be reached or cannot answer.
    pub async fn regeneration(
        &self,
        record: RenderedRecord,
        id: &GlobalId,
        request: &RenderRequest,
    ) -> Result<Regeneration, EngineError> {
        let source = self.built(&id.source)?;
        if record == RenderedRecord::Document {
            documentary(source)?;
        }
        let (content, metadata) = self.read_item(source, record, id).await?;
        let read = TemplateProvenance::read(&metadata);
        let template = match (&request.template, &read) {
            (RenderTemplate::Given(given), _) => given.clone(),
            // An entry this product did not write names nothing it can trust: with no template
            // given there is nothing to render, and the refusal says why.
            (RenderTemplate::Recorded { .. }, Err(problem)) => {
                return Err(EngineError::MalformedProvenance {
                    record,
                    id: id.to_string(),
                    problem: problem.clone(),
                });
            }
            (RenderTemplate::Recorded { search_path }, Ok(Some(recorded)))
                if Path::new(&recorded.template).is_file() =>
            {
                TemplateInput::File {
                    path: PathBuf::from(&recorded.template),
                    search_path: search_path.clone(),
                }
            }
            (RenderTemplate::Recorded { .. }, Ok(Some(recorded))) => {
                return Err(EngineError::TemplateNotAFile {
                    record,
                    id: id.to_string(),
                    reference: recorded.template.clone(),
                });
            }
            (RenderTemplate::Recorded { .. }, Ok(None)) => {
                return Err(EngineError::NoTemplate {
                    record,
                    id: id.to_string(),
                });
            }
        };
        let reference = template.reference();
        let template = template
            .load()
            .map_err(|error| EngineError::Template { error })?;
        let held = self.stored(source, record, id).await?;
        let unused = |reason| Stored::Unused {
            reason,
            held: held.clone(),
        };
        let stored = match (&read, &held) {
            (Err(problem), _) => unused(UnusedAnswers::MalformedProvenance {
                problem: problem.clone(),
            }),
            (Ok(None), _) => unused(UnusedAnswers::NoProvenance),
            (Ok(Some(_)), None) => unused(UnusedAnswers::NoneStored {
                source: id.source.to_string(),
            }),
            (Ok(Some(recorded)), Some(answers))
                if crate::template::answers_digest(answers) != recorded.answers_digest.as_str() =>
            {
                unused(UnusedAnswers::OutOfStep)
            }
            (Ok(Some(_)), Some(answers)) => Stored::Trusted(answers.clone()),
        };
        let mut base = Answers::new();
        if let Stored::Trusted(answers) = &stored {
            // An optional variable given nothing resolved to `null`; left unanswered, it
            // resolves to that again.
            for (name, value) in answers.iter().filter(|(_, value)| !value.is_null()) {
                base.set(name.clone(), value.clone());
            }
        }
        Ok(Regeneration {
            id: id.clone(),
            record,
            template,
            reference,
            base,
            stored,
            keeps_answers: source.source().keeps_template_answers(),
            content,
            provenance: read.ok().flatten(),
        })
    }

    /// Render `regeneration` with `answers` laid over its base, and write the content, the
    /// provenance and the answers in one write — and nothing else about the item — when any of
    /// them differs from what it holds and this is not a dry run.
    ///
    /// # Errors
    ///
    /// Every refusal of [`Regeneration::render`]; [`EngineError::RenderingNotWritable`] for a
    /// source with no write side, which is not asked; and [`EngineError::SourceFailed`] when
    /// the source refuses the write.
    pub async fn regenerate(
        &self,
        regeneration: &Regeneration,
        answers: &Answers,
        dry_run: bool,
    ) -> Result<Regenerated, EngineError> {
        let rendered = regeneration.render(answers)?;
        let provenance = TemplateProvenance::of(regeneration.reference.clone(), &rendered)
            .map_err(|error| EngineError::Template { error })?;
        let changed = regeneration.content != rendered.body
            || regeneration.provenance.as_ref() != Some(&provenance)
            // Answers a source keeps and this item does not hold — a block deleted by hand —
            // are a difference the write repairs; a source keeping none holds none by nature.
            || regeneration
                .stored
                .held()
                .map_or(regeneration.keeps_answers, |stored| {
                    *stored != rendered.answers
                });
        let id = &regeneration.id;
        if changed && !dry_run {
            let source = self.built(&id.source)?;
            if !source.source().writes().is_supported() {
                return Err(EngineError::RenderingNotWritable {
                    name: source.name().to_string(),
                    kind: source.kind().to_owned(),
                    record: regeneration.record,
                });
            }
            let value = provenance.to_value();
            match regeneration.record {
                RenderedRecord::Task => {
                    source
                        .source()
                        .set_task_rendering(&id.native, &rendered.body, &value, &rendered.answers)
                        .await
                }
                RenderedRecord::Document => {
                    source
                        .source()
                        .set_document_rendering(
                            &id.native,
                            &rendered.body,
                            &value,
                            &rendered.answers,
                        )
                        .await
                }
            }
            .map_err(|error| source_failed(source, error))?
            .ok_or_else(|| regeneration.record.no_such(id))?;
        }
        Ok(Regenerated {
            id: id.clone(),
            digest: provenance.digest,
            body_digest: provenance.body_digest,
            changed,
            body: rendered.body,
        })
    }

    /// Regenerate one task in place: [`regeneration`](Self::regeneration) then
    /// [`regenerate`](Self::regenerate), never asking for an answer.
    ///
    /// # Errors
    ///
    /// Every refusal of either half.
    pub async fn render_task(
        &self,
        id: &GlobalId,
        request: &RenderRequest,
    ) -> Result<Regenerated, EngineError> {
        let regeneration = self.regeneration(RenderedRecord::Task, id, request).await?;
        self.regenerate(&regeneration, &request.answers, request.dry_run)
            .await
    }

    /// Regenerate one project document in place, on the terms of
    /// [`render_task`](Self::render_task).
    ///
    /// # Errors
    ///
    /// As [`render_task`](Self::render_task).
    pub async fn render_document(
        &self,
        id: &GlobalId,
        request: &RenderRequest,
    ) -> Result<Regenerated, EngineError> {
        let regeneration = self
            .regeneration(RenderedRecord::Document, id, request)
            .await?;
        self.regenerate(&regeneration, &request.answers, request.dry_run)
            .await
    }

    /// The built source called `name`, when it can be written through.
    fn creatable(
        &self,
        name: &SourceName,
        record: MetadataRecord,
    ) -> Result<&ResolvedSource, EngineError> {
        let source = self.built(name)?;
        if source.source().writes().is_supported() {
            return Ok(source);
        }
        Err(EngineError::NotCreatable {
            name: source.name().to_string(),
            kind: source.kind().to_owned(),
            record,
        })
    }

    /// The content and the metadata of the item `id` names.
    async fn read_item(
        &self,
        source: &ResolvedSource,
        record: RenderedRecord,
        id: &GlobalId,
    ) -> Result<(String, BTreeMap<String, Value>), EngineError> {
        let read = match record {
            RenderedRecord::Task => source
                .source()
                .get_task(&id.native)
                .await
                .map(|task| task.map(|task| (task.content, task.metadata))),
            RenderedRecord::Document => source
                .source()
                .get_document(&id.native)
                .await
                .map(|document| document.map(|document| (document.content, document.metadata))),
        }
        .map_err(|error| source_failed(source, error))?;
        let (content, metadata) = read.ok_or_else(|| record.no_such(id))?;
        Ok((content.unwrap_or_default(), metadata))
    }

    /// The answers the item `id` names has stored beside it, when it has any.
    async fn stored(
        &self,
        source: &ResolvedSource,
        record: RenderedRecord,
        id: &GlobalId,
    ) -> Result<Option<BTreeMap<String, Value>>, EngineError> {
        match record {
            RenderedRecord::Task => source.source().task_template_answers(&id.native).await,
            RenderedRecord::Document => source.source().document_template_answers(&id.native).await,
        }
        .map_err(|error| source_failed(source, error))
    }
}

/// Refuse a source declaring it has no documents, before it is asked anything.
fn documentary(source: &ResolvedSource) -> Result<(), EngineError> {
    if source.source().capabilities().documents.is_native() {
        return Ok(());
    }
    Err(EngineError::NoDocuments {
        name: source.name().to_string(),
        kind: source.kind().to_owned(),
    })
}

/// A dependency's far end as the near source writes it: its own item by its own id, another
/// source's qualified.
fn endpoint(far: &GlobalId, near: &SourceName) -> DependencyEndpoint {
    if &far.source == near {
        return DependencyEndpoint::from_native(far.native.clone(), ItemKind::Task);
    }
    DependencyEndpoint::new(far.to_string(), ItemKind::Task)
        .unwrap_or_else(|_| DependencyEndpoint::from_native(far.native.clone(), ItemKind::Task))
}

/// A create names each label by name alone, so its id is that name too: the id is what a
/// folder of Markdown writes, and the name is what a source that matches labels reads.
fn labels(names: &[String]) -> Vec<Label> {
    names
        .iter()
        .map(|name| Label {
            id: NativeId::from(name.as_str()),
            name: name.clone(),
            color: None,
        })
        .collect()
}

/// A category as this product spells it — the word every source's default mapping reads back
/// as that category.
fn category_word(category: StatusCategory) -> String {
    serde_json::to_value(category)
        .ok()
        .and_then(|word| word.as_str().map(str::to_owned))
        .unwrap_or_else(|| "todo".to_owned())
}

/// The id a created item is suggested under: its title in lower case, every run of anything
/// but an ASCII letter or digit one dash. A source is free to file it under another.
fn slug(title: &str, fallback: &str) -> String {
    let mut slug = String::with_capacity(title.len());
    for character in title.chars() {
        if character.is_ascii_alphanumeric() {
            slug.push(character.to_ascii_lowercase());
        } else if !slug.is_empty() && !slug.ends_with('-') {
            slug.push('-');
        }
    }
    let slug = slug.trim_end_matches('-');
    if slug.is_empty() {
        fallback.to_owned()
    } else {
        slug.to_owned()
    }
}