-
Notifications
You must be signed in to change notification settings - Fork 11
feat(doc-index): section-granularity inference, hashing, and caching fixes #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
d5e2be7
512975d
66b9cc2
9cf51f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| """Studio doc-index command — build/reuse a cached structural index for a | ||
| Markdown file, so heading-based JIT retrieval reads a file's structure once, | ||
| not once per query. | ||
|
|
||
| Thin CLI wrapper around ``studio.utils.doc_index``. | ||
|
|
||
| @cpt-flow:cpt-studio-flow-traceability-validation-validate:p1 | ||
| """ | ||
|
|
||
| import argparse | ||
| from pathlib import Path | ||
| from typing import List | ||
|
|
||
| from ..utils.doc_index import get_or_build_doc_index | ||
| from ..utils.ui import ui | ||
|
|
||
|
|
||
| def cmd_doc_index(argv: List[str]) -> int: | ||
| """Build (or reuse the cached) structural index for a Markdown file.""" | ||
| p = argparse.ArgumentParser( | ||
| prog="cfs doc-index", | ||
| description=( | ||
| "Build or reuse a cached heading/section index for a Markdown file, " | ||
| "so navigation reads the file's structure once, not once per query." | ||
| ), | ||
| ) | ||
| p.add_argument("file", help="Markdown file path") | ||
| p.add_argument( | ||
| "--rebuild", | ||
| action="store_true", | ||
| help="Force a fresh build even if a valid cached index exists", | ||
| ) | ||
| args = p.parse_args(argv) | ||
|
|
||
| filepath = Path(args.file).resolve() | ||
| if not filepath.is_file(): | ||
| ui.result( | ||
| {"file": str(filepath), "status": "ERROR", "message": "File not found"}, | ||
| human_fn=lambda d: ui.error(f"{d['file']}: {d['message']}"), | ||
| ) | ||
| return 2 | ||
|
|
||
| index = get_or_build_doc_index(filepath, force_rebuild=args.rebuild) | ||
|
|
||
| output = { | ||
| "file": str(filepath), | ||
| "cache_hit": index["cache_hit"], | ||
| "total_lines": index["total_lines"], | ||
| "section_count": len(index["sections"]), | ||
| "sections": index["sections"], | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| ui.result(output, human_fn=_human_doc_index) | ||
| return 0 | ||
|
|
||
|
|
||
| def _human_doc_index(data: dict) -> None: | ||
| ui.header("Doc Index") | ||
| hit = "cache hit — reused existing index" if data["cache_hit"] else "cache miss — built fresh index" | ||
| ui.substep(hit) | ||
| ui.substep(f"{data['section_count']} section(s), {data['total_lines']} total lines") | ||
| for s in data["sections"]: | ||
| summary = f" — {s['summary']}" if s.get("summary") else "" | ||
| ui.substep(f" H{s['level']} [{s['line_start']}-{s['line_end']}] {s['heading']}{summary}") | ||
| ui.blank() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ | |
| from pathlib import Path | ||
| from typing import List | ||
|
|
||
| from ..utils.toc import add_toc_max_level_argument, validate_toc | ||
| from ..utils.toc import DEFAULT_MAX_SECTION_LINES, add_toc_max_level_argument, validate_toc | ||
| from ..utils.ui import ui | ||
| # @cpt-end:cpt-studio-algo-traceability-validation-validate-toc:p1:inst-toc-imports | ||
|
|
||
|
|
@@ -31,6 +31,12 @@ def cmd_validate_toc(argv: List[str]) -> int: | |
| help="Markdown file path(s) to validate", | ||
| ) | ||
| add_toc_max_level_argument(p) | ||
| p.add_argument( | ||
| "--max-section-lines", | ||
| type=int, | ||
| default=DEFAULT_MAX_SECTION_LINES, | ||
| help=f"Warn when a section exceeds this many lines (default: {DEFAULT_MAX_SECTION_LINES})", | ||
| ) | ||
| p.add_argument( | ||
| "--verbose", | ||
| action="store_true", | ||
|
|
@@ -63,6 +69,7 @@ def cmd_validate_toc(argv: List[str]) -> int: | |
| content, | ||
| artifact_path=filepath, | ||
| max_heading_level=args.max_level, | ||
| max_section_lines=args.max_section_lines, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cmd_validate_toc aborts the entire multi-file validation batch on any unhandled per-file read error, discarding already-collected resultsSeverity: Minor-to-moderate Problem How to reproduce # files = [good.md, bad.md, good2.md]; bad.md has invalid UTF-8 bytes or revoked permissions
cmd_validate_toc([str(good), str(bad), str(good2)])
# raises uncaught after good.md was already validated and appended to `results` -- that
# result is lost, and good2.md is never reachedExpected behavior Actual behavior Impact Suggested correction How to verify |
||
| ) | ||
|
|
||
| errors = report.get("errors", []) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cfs doc-index --help never explains how section_level is inferred
Severity: Minor
Problem
cfs doc-indexpickssection_levelvia a frequency-based heuristic ininfer_section_level(): the heading level used most often wins, levels appearing exactly once are excluded as candidates, and ties (or an all-singletons document) fall back to the shallowest level. This choice directly determines which lines get grouped intoretrieval_sectionsin both JSON and human-readable output. None of this is mentioned anywhere a CLI user would see it.How to reproduce
Run
cfs doc-index --help(or read theargparse.ArgumentParserdescription incommands/doc_index.py). The description reads only: "Build or reuse a cached heading/section index for a Markdown file, so navigation reads the file's structure once, not once per query." No mention ofsection_level, how it's chosen, or that it can differ from the "obvious" chapter level in irregularly-leveled documents (the exact PDF-conversion scenarioinfer_section_level()'s own docstring cites as its motivating case).Expected behavior
A user seeing
"section_level": 5(say) instead of the level they expected should have a way, via--helpor the command's own output, to understand that the level was inferred from heading-frequency, not simply "H1/H2 is always the section level."Actual behavior
--helpgives no indication that section-level selection is heuristic at all.Impact
Minor UX/discoverability gap — doesn't cause incorrect behavior, but makes an already-nontrivial, silently-applied heuristic harder to audit or trust from the CLI alone.
Suggested correction
Extend the
argparse.ArgumentParser(description=...)incmd_doc_index()(or add a short note to_human_doc_index()'s output) with one sentence, e.g.: "section_levelis inferred from the most frequently repeated heading level (ties prefer the shallower level); a level used only once is never chosen."How to verify
Run
cfs doc-index --helpafter the fix and confirm the heuristic is described.