MCPcopy Create free account
hub / github.com/Qinbf/groundmap / read_block

Function read_block

scripts/k.py:1611–1648  ·  view source on GitHub ↗

根据 ^p-/^t-/^c-/^f- anchor 取出单个 block 原文。 精确锚点未命中时,做一次**哈希容错回收**:按 anchor 末段的 hash6(内容指纹)在全文块里找, 仅当**恰好一个**块命中该 hash6 时才返回它(附 recovered_from)。这样能确定性修复"内容哈希抄对、 但 type/seq 写错"的引用(典型:表格被解析为 ^p- 却被引为 ^t-;或把 ^p-7-x 误写成 ^t-7-x), 而内容真不在本页的孤儿锚点(hash6 零命中)仍按原样报"未找到"、由上层降级处理。

(md_path: Path, anchor: str)

Source from the content-addressed store, hash-verified

1609
1610
1611def read_block(md_path: Path, anchor: str) -> dict:
1612 """根据 ^p-/^t-/^c-/^f- anchor 取出单个 block 原文。
1613
1614 精确锚点未命中时,做一次**哈希容错回收**:按 anchor 末段的 hash6(内容指纹)在全文块里找,
1615 仅当**恰好一个**块命中该 hash6 时才返回它(附 recovered_from)。这样能确定性修复"内容哈希抄对、
1616 但 type/seq 写错"的引用(典型:表格被解析为 ^p- 却被引为 ^t-;或把 ^p-7-x 误写成 ^t-7-x),
1617 而内容真不在本页的孤儿锚点(hash6 零命中)仍按原样报"未找到"、由上层降级处理。
1618 """
1619 target = anchor.lstrip("^")
1620 blocks = parse_blocks_with_anchors(md_path)
1621 for blk in blocks:
1622 if blk.anchor == target:
1623 text = ANCHOR_TAIL_RE.sub("", blk.text).rstrip()
1624 return {
1625 "path": _to_rel_posix(md_path),
1626 "anchor": target,
1627 "kind": blk.kind,
1628 "line_start": blk.line_start,
1629 "line_end": blk.line_end,
1630 "content": text,
1631 }
1632 # 哈希容错回收:仅在 hash6 全文唯一命中时恢复,避免误配
1633 th = _anchor_hash6(target)
1634 if th:
1635 matches = [b for b in blocks if b.anchor and _anchor_hash6(b.anchor) == th]
1636 if len(matches) == 1:
1637 blk = matches[0]
1638 text = ANCHOR_TAIL_RE.sub("", blk.text).rstrip()
1639 return {
1640 "path": _to_rel_posix(md_path),
1641 "anchor": blk.anchor,
1642 "kind": blk.kind,
1643 "line_start": blk.line_start,
1644 "line_end": blk.line_end,
1645 "content": text,
1646 "recovered_from": target, # 原引用锚点的 type/seq 与真实块不符,已按内容 hash6 校正
1647 }
1648 raise LookupError(f"未找到 anchor: {anchor}")
1649
1650
1651def list_all_blocks(md_path: Path) -> dict:

Callers 1

_main_implFunction · 0.70

Calls 3

_to_rel_posixFunction · 0.85
_anchor_hash6Function · 0.85

Tested by

no test coverage detected