Resolve the stable wiki name for ``src`` (Scheme A). Identity is keyed by path: a source we've seen before (same path, even with new content) keeps its name so re-ingest overwrites in place. Legacy registry entries (written before the path index) are matched by stem and backfilled w
(
src: Path,
kb_dir: Path,
registry: HashRegistry,
*,
persist_legacy: bool = True,
)
| 73 | |
| 74 | |
| 75 | def resolve_doc_name( |
| 76 | src: Path, |
| 77 | kb_dir: Path, |
| 78 | registry: HashRegistry, |
| 79 | *, |
| 80 | persist_legacy: bool = True, |
| 81 | ) -> str: |
| 82 | """Resolve the stable wiki name for ``src`` (Scheme A). |
| 83 | |
| 84 | Identity is keyed by path: a source we've seen before (same path, even |
| 85 | with new content) keeps its name so re-ingest overwrites in place. |
| 86 | Legacy registry entries (written before the path index) are matched by |
| 87 | stem and backfilled with the path. A brand-new source keeps the clean |
| 88 | sanitized stem unless another document already owns that name, in which |
| 89 | case it gets a deterministic ``-{sha256(path)[:8]}`` suffix. |
| 90 | """ |
| 91 | path_key = _registry_path(src, kb_dir) |
| 92 | |
| 93 | known = registry.get_by_path(path_key) |
| 94 | if known is not None: |
| 95 | stored = known.get("doc_name") or Path(known.get("name", "")).stem |
| 96 | if stored: |
| 97 | return stored |
| 98 | |
| 99 | legacy = registry.find_legacy_by_stem(src.stem) |
| 100 | if legacy is not None: |
| 101 | file_hash, meta = legacy |
| 102 | meta = dict(meta) |
| 103 | name = meta.get("doc_name") or Path(meta.get("name", "")).stem |
| 104 | if persist_legacy: |
| 105 | meta["doc_name"] = name |
| 106 | meta["path"] = path_key |
| 107 | registry.add(file_hash, meta) # backfill + persist |
| 108 | return name |
| 109 | |
| 110 | return resolve_doc_name_from_key(src.stem, path_key, registry) |
| 111 | |
| 112 | |
| 113 | def resolve_doc_name_from_key(stem: str, path_key: str, registry: HashRegistry) -> str: |