feat(ingest/github-documents): nest OKF bundle documents by name.md and folder index.md

SH
3h ago
+1716-147

Summary

OKF bundles imported with github-documents / gitlab-documents had two hierarchy problems:

  1. name.md next to name/ became two siblings. metrics/dau.md nested under an empty metrics folder document instead of under the Metrics concept from metrics.md.
  2. A folder's index.md was imported as its own document, titled index.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.md stands for name/. Its folder's contents nest under the name.md document, and no folder document is created for name/. This works at any depth (metrics.md → metrics/engagement.md → metrics/engagement/sessions.md).
  • A folder's index.md is the folder document's content. Its body becomes the folder's body, and its frontmatter is never shown. The folder's title is the bundle title (bundle-root index.md only), else the listing's first # heading, else the directory name. The index.md at the import root (repository root or path_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 (and log.md) are reserved at every level, and consumers must not treat them as concepts. That is now true: an index.md is never a concept or a separate document.
  • §8: an index.md is the directory listing for progressive disclosure. A subdirectory's index.md has no frontmatter, so its folder takes its title from the listing's first H1, then the directory name. A nested index.md that has frontmatter anyway is parsed and stripped, and a title key there is ignored. Only the bundle-root index.md may carry frontmatter, which is bundle metadata (§8.1). Its title names the bundle root's document.
  • §8.1 description and license (new in 0.3) are not stored yet. DocumentInfo has no description field, and the natural home is OkfBundle (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/ or guides/.draft.md) are not part of the bundle. They are not imported at all and are counted in okf_dot_paths_excluded. A root listed in okf_bundle_roots may 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.md sits next to it, because its index.md holds the bundle metadata.
  • The spec does not define name.md + name/. It says only the directory tree defines parent/child (§2). Making name.md the folder's node is a DataHub presentation choice. It does not change concept ids, which still come from the file path.
  • log.md is 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 without DocumentInfo, so DataHub-side edits survive. In a bundle, only their parentDocument is rewritten when it moved. The stored DocumentInfo read 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 bundle index.md files. 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.md is imported like a file. It stores a source_content_hash and 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.md is 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_hash sync-back contract are unchanged.
  • An index.md with no document to fill is not imported and is counted in okf_index_files_not_imported, with an info entry. This happens when name.md stands for its folder, or when the import root has no repository root document.
  • Read failures keep the previous state. An unreadable folder index.md leaves the folder as it was, with a warning. An unreadable root index.md keeps the previous root document. An unreadable name.md still parents its folder's contents under its existing URN. A document whose parent was not read this run keeps its stored parent.
  • Turning enable_okf off restores the plain layout. Files re-import because their source hash changes, and existing folders nested under a name.md move back under their folder.
  • If a folder's index.md is 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. Only parentDocument changes, 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 by name.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 to name.md, and a second run re-emits neither); an unchanged name.md still 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 folder index.md, and an unreadable root index.md.
  • test_okf_bundles.py: updated the reserved-file test to the new rule.
  • GitHub and GitLab integration goldens: added metrics.md + metrics/dau.md and a nested guides/index.md to 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