feat(ingest/github-documents): nest OKF bundle documents by name.md and folder index.md
Summary
OKF bundles imported with github-documents / gitlab-documents had two hierarchy problems:
name.mdnext toname/became two siblings.metrics/dau.mdnested under an emptymetricsfolder document instead of under theMetricsconcept frommetrics.md.- A folder's
index.mdwas imported as its own document, titledindex.md, with its YAML frontmatter in the body, while the folder (and repository root) document stayed empty.
This PR fixes both inside OKF bundles:
name.mdstands forname/. Its folder's contents nest under thename.mddocument, and no folder document is created forname/. This works at any depth (metrics.md→metrics/engagement.md→metrics/engagement/sessions.md).- A folder's
index.mdis the folder document's content. Its body becomes the folder's body, and its frontmatter is never shown. The folder's title is the bundletitle(bundle-rootindex.mdonly), else the listing's first#heading, else the directory name. Theindex.mdat the import root (repository root orpath_prefix) fills the repository root document. It is no longer a document of its own.
Before / after (integration fixture bundle)
Before:
- analytics (empty)
- Metrics ← metrics.md
- guides (empty)
- Monthly active users
- broken.md
- index.md ← listing imported as a document
- no-type.md
- plain.md
- index.md ← bundle index, YAML in the body
- log.md
- metrics (empty) ← duplicate node for metrics/
- Daily active users
After:
- Analytics knowledge bundle ← title and body from index.md
- Metrics ← metrics.md
- Daily active users
- Guides ← title and body from guides/index.md
- Monthly active users
- broken.md
- no-type.md
- plain.md
- log.md
What the OKF spec says, and what this follows
- §3.1 / §11.2:
index.md(andlog.md) are reserved at every level, and consumers must not treat them as concepts. That is now true: anindex.mdis never a concept or a separate document. - §8: an
index.mdis the directory listing for progressive disclosure. A subdirectory'sindex.mdhas no frontmatter, so its folder takes its title from the listing's first H1, then the directory name. A nestedindex.mdthat has frontmatter anyway is parsed and stripped, and atitlekey there is ignored. Only the bundle-rootindex.mdmay carry frontmatter, which is bundle metadata (§8.1). Itstitlenames the bundle root's document. - §8.1
descriptionandlicense(new in 0.3) are not stored yet.DocumentInfohas no description field, and the natural home isOkfBundle(title, description, license). That is a model change, and it changes every bundle file's source hash, so it is left to the follow-up OKF 0.3 ingestion-conformance PR, together with the 0.3 bundle-detection rule. Nothing here depends on how bundles are detected: the hierarchy uses whichever bundles discovery returns. - §3.2 (0.3): files and folders whose names start with
.below a bundle root (such as.github/orguides/.draft.md) are not part of the bundle. They are not imported at all and are counted inokf_dot_paths_excluded. A root listed inokf_bundle_rootsmay itself be a dot-folder, and dot paths outside every bundle are imported as before. The helpers (has_dot_segment,is_excluded_from_bundle) and the skip are the same code as in #21885, so the two PRs agree and their merge conflicts resolve by taking one side. - §6 (0.3): relative path fields resolve from the file's own folder. The hierarchy code does not resolve path fields, so this PR doesn't touch that. Path-field resolution belongs to the conformance work.
- A bundle root keeps its own folder document even if a
name.mdsits next to it, because itsindex.mdholds the bundle metadata. - The spec does not define
name.md+name/. It says only the directory tree defines parent/child (§2). Makingname.mdthe folder's node is a DataHub presentation choice. It does not change concept ids, which still come from the file path. log.mdis unchanged: it is still imported as a plain document.
Why the change is in the shared repository source
Both the GitHub and GitLab sources build documents through repository_documents/source.py, so the change is there and applies to both. The planning is a pure function in a new module, okf_hierarchy.py. It is specific to OKF bundles, and no other source needs it, so it stays in this package.
Scope: non-OKF repositories are unchanged
The new rules apply only to paths inside a discovered or configured OKF bundle with enable_okf: true. Plain markdown trees keep their current behaviour: every folder is a folder document, and every file, index.md included, is a document. Folding index.md / name.md into folders could also suit plain trees (MkDocs, Hugo). But that changes existing imports without a spec to anchor it, so it is left out of this PR.
Migration and idempotence
- URNs do not change. File, folder and repository-root documents keep their source ids. Only parent links change.
- Unchanged documents that moved are re-parented on the next run. For files in a bundle, the unchanged-file skip now also checks the stored
parentDocument. If it differs, the file is re-imported once. Existing folder documents are still emitted withoutDocumentInfo, so DataHub-side edits survive. In a bundle, only theirparentDocumentis rewritten when it moved. The storedDocumentInforead for identity and change detection is cached per run, so the parent check adds no GMS requests for files. - Documents that are no longer produced are the empty
name/folder documents and the documents imported from bundleindex.mdfiles. Stateful ingestion soft-deletes them as stale. Without stateful ingestion they remain, as for any deleted file. The docs say so. - A folder document with an
index.mdis imported like a file. It stores asource_content_hashand is skipped while the index and its parent are unchanged. The repository then owns that folder's title and body. Folders without an index keep the existing "metadata-only after creation" behaviour. - A skipped (unchanged)
name.mdis still registered as the parent of its folder's contents, with its stored title for browse paths. - The clearing path (a file stops being a concept) is untouched. Change detection hashes and the
content_hashsync-back contract are unchanged. - An
index.mdwith no document to fill is not imported and is counted inokf_index_files_not_imported, with an info entry. This happens whenname.mdstands for its folder, or when the import root has no repository root document. - Read failures keep the previous state. An unreadable folder
index.mdleaves the folder as it was, with a warning. An unreadable rootindex.mdkeeps the previous root document. An unreadablename.mdstill parents its folder's contents under its existing URN. A document whose parent was not read this run keeps its stored parent. - Turning
enable_okfoff restores the plain layout. Files re-import because their source hash changes, and existing folders nested under aname.mdmove back under their folder. - If a folder's
index.mdis deleted, the folder returns to its directory name and an empty body. - Existing folder documents are moved with a read-modify-write of
documentInfo, because the server has no PATCH template for that aspect. OnlyparentDocumentchanges, plus the title, body and index properties when the index was removed. Other fields, including DataHub-side edits, are written back as read.
I don't treat this as a breaking change: recipes and URNs are unchanged, and the behaviour only changes for opt-in OKF imports. So there is no breaking-changes.md entry, which also keeps this PR clear of #21146.
Relation to #21146
#21146 changes compute_source_hash and the existing-folder branch of _emit_folder_document. This PR changes neither. Index-backed folders use a new _emit_indexed_folder_document, which sets StatusClass(removed=False) the way files do today. Whichever PR merges second should move that call to _imported_status().
Tests
tests/unit/ingestion/source/repository_documents/test_okf_hierarchy.py: the planner (nesting at depth,name.md+name/, folder index, index shadowed byname.md, bundle root keeps its folder, import-root index with and without a root document, paths outside bundles, reserved names).test_github_documents_source.py: the end-to-end hierarchy on a probe bundle, with the bundle-root index filling the root document; the same tree with OKF off (unchanged); migration (an unchanged file and an existing folder are moved from the old folder node toname.md, and a second run re-emits neither); an unchangedname.mdstill parents its folder's contents; an unreadable folder index.- Regression tests for an unreadable
name.md, OKF turned off after an OKF import, a deleted folderindex.md, and an unreadable rootindex.md. test_okf_bundles.py: updated the reserved-file test to the new rule.- GitHub and GitLab integration goldens: added
metrics.md+metrics/dau.mdand a nestedguides/index.mdto the fixture bundle.
The new tests fail on master. The existing repository-documents unit and integration tests pass.
Checklist
- PR title follows the Conventional Commits format
- Tests added/updated
- Docs updated (
github-documents_post.md, "Bundle hierarchy") - Breaking changes: none (see above)
🤖 Opened with cclaude on behalf of @shirshanka