part_of: 'prov' #
init over existing content — detection & adoption #
Working design. What
prov initshould do when the target directory already holds markdown (with or without frontmatter), an existing tree, or just unrelated files. Complements DESIGN §9 (extraction) and answers open question #2 (how first-class is the filesystemStructureSource).
The gap today #
cmd_init is greenfield-only:
- It bails only if
index.{md,dj,html,yaml,…}exists. - Otherwise it runs the interview and writes exactly two files — the root
index.<content-ext>andprov.<meta-ext>— and ignores everything else in the directory.
Two concrete failures follow in a non-empty folder:
- Silent orphans. Existing content files are never linked into the tree, and
validate.rshas no orphan/unreachable finding, socheck(which walks from the root) never mentions them. The structure silently omits most of the folder — the exact opposite of "hand it to any tool and it unfolds." - Config clobber. The config write is unconditional and only
index.*is guarded, so a pre-existingprov.yaml/registry.yamlis overwritten with no check.
There is also a latent find_root interaction: existing files with frontmatter
but no part_of are all root candidates; init writing index.md wins the
tie-break but leaves the others as silent competing roots.
Position #
Running init over existing content is the filesystem-intake / migration
story. We commit to making it first-class:
- Directory tree is a real structure source.
initcan mirror the folder hierarchy into the containment tree (the "import an Obsidian/diaryx vault" path), behind aStructureSourceseam — not merely a convenience. - Interactive by default. On a terminal,
initdetects the situation and asks; non-interactive runs (--yes, no tty) that would need to touch existing files refuse unless an explicit--adoptopts in. - Adoption writes into documents, but only on opt-in and after a preview.
Because structure lives in documents (DESIGN §1), adopting a file means adding
part_offrontmatter to it andcontentsto its parent. That mutation is never silent: previewed, idempotent, git-reversible, additive (never removes a user'stags/aliases/etc.).
Detect first, then branch #
Replace the single index.* guard with a classification of the target
directory, computed before the interview:
| Class | Signature | Behavior |
|---|---|---|
| A. Greenfield | empty, or only non-content files | Current behavior: write root + config, ignore the rest. |
| B. Loose content | content files, none carrying part_of/contents |
Choose/mint a root, then offer adoption of the loose files (interactive; --adopt non-interactively). |
| C. Already structured | ≥1 file with part_of/contents (a diaryx/prov-shaped tree) |
Do not mint a competing index.md. Detect the existing root; attach policy only (write prov.<ext> linked from it). Offer to adopt any still-loose files. |
| D. Initialized | a config document already present | Refuse; point to config / check. --force re-runs the interview. |
Cross-cutting rules (fix the footguns for every class):
- Never overwrite an existing
prov.<ext>/registry.<ext>/index.<ext>without--force. - Prefer an existing root — an
index/readmewith frontmatter, or a lone no-part_ofdocument — over mintingindex.md. When several no-part_ofcandidates exist: prompt to pick (interactive) or error listing them (batch). - Infer format from the adopted root. When an existing file becomes the root, read its content grammar / embed style / metadata format from the file instead of asking — the interview shrinks to policy questions.
The revised flow #
- Resolve + canonicalize the target dir.
- Classify (below).
- Class D → refuse (unless
--force). - Select the root: existing candidate (C, or B/A with
index/readme) vs mintindex.<ext>. - Interview for policy (identity, id-storage, reference style, …). Content / embed / metadata questions are skipped when the root is adopted (inferred).
- Class B/C with loose files → adoption: pick a strategy, show a
preview (proposed tree + the exact files that gain
part_of+ the root's newcontents), confirm. - Apply: write config (guarded), mint the root if needed, apply adoption as
mutations (reusing
create/link-maintenance), bootstrap the registry if identity will mint. - Summary +
next:hint.
Classification detail #
Walk the target dir (recursively for the tree case). Collect:
- content files (
ROOT_EXTS, per subdir), and which carry frontmatter; - whole-file metadata docs (
META_EXTS); - whether any file declares a structural link (
part_of/contents) → C; - whether a config document exists (a whole-file doc with config keys, or one
linked via the
configrelation) → D; - root candidates (metadata + no
part_of;index/readmestems preferred); - the directory shape (which subdirs hold content) for the mirror plan.
Adoption strategies #
Two mappings from filesystem → containment tree, offered at the adoption prompt:
mirror(folder-as-node) — default for nested trees. Every directory becomes a containment node: itsindex/readmeif present, otherwise a synthesized folder-note index. A folder node'scontents= its child files- child folder nodes; each child's
part_ofpoints up. A faithful, deep mirror where following links reproduces the filesystem — at the cost of minting index docs for bare folders.
- child folder nodes; each child's
flat— default for a single flat directory. Only directories that already have an index become nodes; loose files attach to the nearest ancestor index. Mints nothing extra; loses per-folder grouping.none— init the root + config only; leave files loose (they show up as orphans incheck, see below).
Rules that hold for every strategy:
- Additive frontmatter only. Add
part_of(andcontentson parents); preserve every existing field. A file that already has the rightpart_ofis left untouched (idempotent → safe re-run / incremental adopt). - Titles come from existing
titlefrontmatter, else the first H1, else the filename titleized. - Body
[[wikilinks]]are overlay references, not containment. Adoption does not touch them;checkresolves/reports them later. (A future pass can register them as overlay links / stamp ids — out of scope here.)
StructureSource seam #
Adoption is the first real caller of the plan-then-apply intake. As built
(intake.rs), the plan is a concrete value produced by plan_mirror (no trait
yet — see below):
struct StructurePlan {
// folder-notes to create for bare directories, parents-first
synthesized: Vec<SynthNode>, // { path, parent, title }
// existing files to link under a node
adoptions: Vec<Adoption>, // { child, parent }
}
plan_mirror+apply_planare themirrormapping — the concreteFilesystemSource.plan_mirroronly reads (previewable);apply_planreusescreate(synthesized notes) andadopt(existing files), so all the link-maintenance and identity hooks come for free.- A
StructureSourcetrait would abstract over alternative intakes — aFrontmatterSource(links already declared in docs; the class-C tree that needs no restructure) or a hybrid (filesystem fills the gaps frontmatter leaves). Deferred until a second source exists: one implementation behind a trait is premature abstraction.
init --adopt mirror = plan_mirror → (apply_plan) mutation batch. The
directory walk reuses content_documents (itself modeled on scan_ids_dir /
scan_titles).
Companion: an orphan finding in validate #
Add an Orphan finding — a content file on disk not reachable from the root.
This is independently valuable and it powers the rest:
- makes "you have N unadopted files" a checkable signal, not a one-time
initmessage; - gives an incremental
prov adopt(adopt files that appeared later) its work-list; - is the honest "what's missing" answer DESIGN §8 wants from the sleeper feature.
Its autofix is exactly an adoption step (attach under a chosen parent), so it shares the Phase-1 machinery.
Flags #
--adopt[=mirror|flat|none]— non-interactive opt-in + strategy (default strategy chosen by tree shape).--root <file>— adopt an existing file as the root instead of minting one.--dry-run— classify, print the plan, write nothing.--force— permit overwriting an existing config/root (re-init).--yes— take defaults; still needs--adoptbefore it will touch existing files.
Phasing #
- Phase 0 — safe & honest (small, ship first). ✅ Done.
initclassifies the directory (classify_dir→DirState) and branches: greenfield initializes as before; an initialized workspace or an existing structured tree is refused with guidance (the tree case names its root and points atprov config); loose content is confirmed interactively, noted non-interactively, and refused under--yes(so a script never orphans silently). The old narrowindex.*guard is subsumed, and aprov.*config is no longer clobbered.validategainedFinding::Orphan— a content document on disk that nothing reachable from the checked root links to — computed by diffingWorkspace::content_documentsagainst the walk's reachable set (census targets + separated-nodecontentbodies + the start). No file-rewriting yet; the orphan is diagnosis-only (suggest_fix→None). - Phase 1 — flat adoption. ✅ Done.
Workspace::adopt(child, parent)(mutate.rs) authors both directions — the child's inverse up, the parent's spanning entry down — over an existing file without creating, moving, or rewriting its body. Additive and idempotent; refuses a contested parent (mirrorssuggest_fixdeclining a claimed child); registers the parent when the workspace authors id links.initgained--adopt flat|none(andmirror, which errors as not-yet-built):flatlinks every loose document under the new root; the interactive path offers adopt / leave-unlinked / cancel.Orphanautofix rides the same primitive —prov check --fixoffers to adopt each orphan under the root. A path-only fix no longer bootstraps a spurious empty registry (the post-fixensure_registryis now gated on the index being dirty). Still deferred: a dry-run/preview before writing, and per-file parent choice (batch adoption always uses the root). - Phase 2 — directory-tree import. ✅ Done.
Workspace::plan_mirror(intake.rs) walks the directory tree and returns aStructurePlan— the folder-notes to synthesize (parents-first) and the files to adopt, computed without touching disk so it can be previewed;apply_planrealizes it, reusingcreatefor the folder-notes andadoptfor the files. Every directory holding content becomes a node — its ownindex/readmewhen present, else a synthesizedindex.<ext>titled after the folder — so following links reproduces the filesystem.creategained a crate-internalcreate_titledso a synthesized note's title and its parent's spanning-entry label are authored in one step (no stale[index]labels).init --adopt mirrorruns it (and the interactive menu offers "import the folder tree" whenever the loose files span subdirectories); a separated root — where folder-note grammar inheritance breaks — is refused byplan_mirrorandinitfalls back to flat with a note. TheStructureSourcetrait is deferred: with one concrete source (FilesystemSource, i.e. these two methods) an abstraction would be premature — it lands when a second source (frontmatter-only or hybrid) needs it. Still deferred: a--dry-runpreview of theStructurePlan, and per-file parent choice.
Rejected / non-goals #
- Sidecar-only tree (record containment in the registry without editing files) — contradicts DESIGN §1 (structure lives in documents). Rejected.
- Silent adoption / silent overwrite — every file mutation is opt-in and previewed; existing config/root files are guarded.
- Rewriting body wikilinks during adoption — overlay-link registration is a later, separate pass.