Skip to content

Read Python source: docstrings and comments become located claims - #51

Merged
Deep-Sixed merged 3 commits into
mainfrom
claude/peaceful-rubin-1bpmhh
Nov 21, 2026
Merged

Deep-Sixed merged 3 commits into
mainfrom
claude/peaceful-rubin-1bpmhh

Conversation

@Deep-Sixed

@Deep-Sixed Deep-Sixed commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

What

Code-aware ingestion. Folder builds and refreshes now read .py files by default, for their prose only: docstrings and comments, never code. Parsing uses the standard library's ast and tokenize, so there are still no dependencies.

How it works

  • Exact locations. As with Markdown, everything that isn't prose is blanked in place, so claim spans and line numbers stay true to the file. ast reports columns in UTF-8 bytes, which are converted to characters.
  • Each claim records its definition: symbol: "ShardStore.put".
  • Not prose: code, doctests, :: literal blocks, docstring paragraphs and comment runs that parse as Python, pragmas (# noqa, # type:, …) and shebangs. reST roles and list markers are blanked, list items are separate blocks, and #: attribute comments read cleanly.
  • Confidence: docstring sentences are graded like paragraphs; comments no higher than medium.
  • Names: only names distinctive as written are marked up. That means qualified names, snake_case or capitalised definitions, the module's dotted path, and names imported from the project itself. Standard-library imports and plain lowercase words are not marked.
  • Invalid Python is skipped with the line it failed on.
  • Scale: an 843 KB file with 18,000 blocks parses in about 0.6 s.

Review

These checks were run against data, as asked:

Check Result
AST extraction Tests cover docstrings at every level, comments, examples, doctests, :: blocks, commented-out code, pragmas, list items, reST roles, #: comments, and invalid Python.
Source locations Spans match the file. Non-ASCII before a docstring and before a comment are both tested, which catches byte/character column mix-ups.
Entity relationships Found a problem, fixed. Every function name and import was marked up, so generic words became hub entities: graph 127 claims, file 91, read 49, plus json, field, Path. Now only distinctive names are marked. Entities on contextmesh/ went from 336 to 269.
Literal blocks Found a bug, fixed. Text after :: was treated as code whenever indented at all; reST requires more indentation than the introducing paragraph.
Determinism Identical graph and resolver under PYTHONHASHSEED 0 and 4242.
Memory limits 0.55 MB of code gives 1,001 claims at 86 MB peak, cheaper per MB than prose. The 4 MB total cap applies to code too.
Markdown compatibility A prose-only folder builds byte-identically on main and on this branch. All existing ingest and refresh tests pass.

Four parser behaviours were mutation-checked. Two tests were strengthened until they caught their sabotage.

Benchmarks

Suites now declare their file types (corpus.suffixes, prose by default). The docs suite reads Markdown and text only, and its accuracy and refusals are unchanged by code ingestion.

New code suite, bench/contextmesh-code.json, corpus 843be1ee936a360c:

Method Correct Mean tokens Out-of-scope refused
Graph walk 13/18 77 6/6
BM25 top-1 12/18 31 2/6
BM25 top-3 15/18 96 2/6
  • This PR's first version reported the walk at 15/18. Both of the lost answers had been reached through the generic-word hubs the review removed. For example, "not a field to be regenerated" was found via dataclasses.field. The corrected number is the honest one.
  • The code questions were written from the extracted claims, so the docs suite remains the fairer test.
  • The code suite's corpus is this project's own source, so it moves with the code. A new docstring saying "with a capital" made BM25 stop refusing "What is the capital of Australia?". Compare methods within one run, not across runs. docs/BENCHMARKS.md records all of this.

Deliberately not added

A --no-code CLI flag. Refresh treats a file it no longer reads as deleted, so building with code and refreshing without it would retire every code claim. Prose-only builds remain available through the Python API's suffixes=.

Checks

The full suite, ruff, the demo export (byte-identical) and the leak check pass.

Folder builds (and refreshes) now read .py files by default, for their
prose only, using the standard library's ast and tokenize, so there are
still no dependencies.

- parse_python(): docstring paragraphs and comment runs become blocks;
  everything else is blanked in place, so spans and line numbers stay
  true to the file (ast's UTF-8 byte columns are converted to characters).
  Each block records the definition it documents or sits in, and claims
  carry it as `symbol` ("ShardStore.put").
- Not prose: code, doctests, `::` literal blocks, docstring paragraphs and
  comment runs that parse as Python, pragmas (# noqa, # type:, ...),
  shebangs. reST roles (:meth:`x`) and list markers are blanked; list
  items are separate blocks; "#:" attribute comments read cleanly.
- Docstring sentences are graded like paragraphs; comments no higher than
  medium. Definitions, imports and the module's dotted path are marked-up
  names, so they become entities wherever prose mentions them.
- A file that is not valid Python is skipped with the line it failed on.
- The enclosing-definition lookup is a precomputed line map: an 843 KB
  file with 18,000 blocks parses in about 0.6 s.

Benchmarks: suites now name their file types (corpus.suffixes; prose by
default), so the docs suite is unaffected by the new default. A code
suite (bench/contextmesh-code.json, 18 + 6 questions over contextmesh/)
shows the walk at 15/18 vs BM25 top-1 12/18 and top-3 15/18, refusing 6/6
out-of-scope questions; docs/BENCHMARKS.md records it with the caveat
that its questions were written from the extracted claims.

Tests: offset preservation, prose vs code, symbols, pragmas/examples/
commented-out code excluded, list items and #: comments, marked names,
spans, non-ASCII columns for docstrings and comments, invalid Python,
folder builds, comment grading, entities from definitions, prose-only
builds, and a refresh retiring a changed docstring. Four behaviours were
mutation-checked; two tests were strengthened until they caught theirs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Found reviewing this PR:

- Code names made generic words into hub entities. Every function name,
  every import and the bare module name were marked up, so `graph`,
  `file`, `read`, `json`, `field`, `time`, `Path` became entities matching
  every prose use of the word (graph: 127 claims, file: 91, read: 49).
  Now only names distinctive as written are marked: dotted/qualified,
  snake_case or capitalised, the module's dotted path, and names imported
  from the project (relative imports). Absolute imports are not marked:
  separating stdlib from third-party needs a list that differs across
  Python versions. Entities on contextmesh/: 336 -> 269.
- Text after "::" was treated as a literal block whenever it was indented
  at all. reST makes it literal only when indented more than the paragraph
  introducing it; the block now ends at the first paragraph back at that
  indent.

Verified: identical graph and resolver under PYTHONHASHSEED 0 and 4242;
a prose-only folder builds byte-identically on main and on this branch;
0.55 MB of code gives 1,001 claims at 86 MB peak.

Benchmark: the code suite now shows the walk at 13/18 (was 15/18). Both
lost answers had been reached through generic hubs ("not a *field* to be
regenerated" via dataclasses.field). docs/BENCHMARKS.md records the
corrected numbers, why they moved, and that the code suite's corpus is
the project's own source, so it moves with the code: compare methods
within one run. The docs suite is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The README edit in the previous commit changed the docs corpus (it is part
of it): fingerprint 92c57aeb78f8236b, 805 claims. Accuracy and refusals are
unchanged; the walk's mean tokens and the read-everything total moved
slightly, and the write-up now quotes this run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Deep-Sixed
Deep-Sixed marked this pull request as ready for review November 21, 2026 05:26
@Deep-Sixed
Deep-Sixed merged commit 85baedb into main Nov 21, 2026
43 checks passed
@Deep-Sixed
Deep-Sixed deleted the claude/peaceful-rubin-1bpmhh branch November 21, 2026 05:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants