Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions okf/src/reference_agent/tools/bundle_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@


def _section_content_lines(body: str, heading: str) -> list[str]:
"""Return non-blank lines under a top-level `# heading` section."""
"""Return non-blank lines under a top-level `# heading` section.

Fenced code blocks are treated as content: a `#`-prefixed line inside a
fence is not mistaken for a heading, so it does not prematurely end the
section.
"""
in_section = False
in_fence = False
out: list[str] = []
for line in body.splitlines():
stripped = line.strip()
if stripped.startswith("# "):
if stripped.startswith("```") or stripped.startswith("~~~"):
in_fence = not in_fence
elif not in_fence and stripped.startswith("# "):
in_section = stripped == heading
continue
if in_section and stripped:
Expand Down
38 changes: 37 additions & 1 deletion okf/tests/test_bundle_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

import pytest

from reference_agent.tools.bundle_tools import write_concept_doc
from reference_agent.tools.bundle_tools import (
_schema_field_names,
_section_content_lines,
write_concept_doc,
)
from reference_agent.tools.context import (
clear_web_state,
set_context,
Expand Down Expand Up @@ -148,3 +152,35 @@ def test_web_pass_skips_guard_for_non_bigquery_table_types(tmp_path):
"# Citations\n[1] [Src](https://src)\n",
)
assert "error" not in result


def test_section_survives_fenced_hash_comment():
body = (
"# Schema\n"
"- `id` STRING\n"
"```\n"
"# looks like a heading but is code\n"
"```\n"
"- `name` STRING\n"
"# Other\n"
"- ignored\n"
)
text = "\n".join(_section_content_lines(body, "# Schema"))
assert "`id`" in text
assert "`name`" in text
assert "ignored" not in text


def test_schema_fields_survive_fenced_hash_comment():
body = (
"# Schema\n"
"- `id` STRING\n"
"```sql\n"
"# partition by ingestion day\n"
"SELECT 1\n"
"```\n"
"- `name` STRING\n"
"# Citations\n"
"[1] [BQ](https://bq)\n"
)
assert _schema_field_names(body) == {"id", "name"}