Skip to content

Python context layer: pin dictionary prose as retrievable chunks - #284

Open
jat255 wants to merge 3 commits into
jat255/context-layer-retrievalfrom
jat255/context-layer-dictionary-chunks
Open

Python context layer: pin dictionary prose as retrievable chunks#284
jat255 wants to merge 3 commits into
jat255/context-layer-retrievalfrom
jat255/context-layer-dictionary-chunks

Conversation

@jat255

@jat255 jat255 commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

Third of four PRs for M4, the Python context layer (kata vp7e). Stacked on #241; #285 follows this and wires the chunks into an agent's layer on top.

This PR adds shared fixtures defining the expected way prose should be transformed into retrieval chunks. tests/shared/context_layer.json gains a dictionary_context_chunks section and both suites run it against their own reader: dataset details, then tables in declaration order, then glossary terms, then governed definitions, with a table that has no prose contributing nothing and column-level content staying out.

Implementing this PR found that python needed a guard for a source carrying no dictionary at all.

Governed definitions get no fixture case, deliberately. Both packages render the chunk text the same way and definitions.json pins it, but they compile definitions at different constructors, so they disagree on whether a dictionary not yet attached to a source has any. A case here would pin that disagreement rather than the contract.

R changes

@simonpcouch

One functional change in pkg-r/R/context-layer.R, required by an error found by the new fixture: dictionary_context_chunks() returned a named character vector and now returns an unnamed one.

The names came from vapply(names(dictionary$tables), ...), which labels each chunk with its table name, and c() propagated them to the whole vector (with "" for the dataset details and glossary entries). Those names rode into augment_context_layer() and onto the layer's docs vector. Nothing ever read them, but the R side was carrying metadata Python can't represent, which made the shared fixture unsatisfiable for both suites. The fix is a single unname(); chunk text and order are unchanged.

Blast radius: two call sites, neither reads names. augment_context_layer() concatenates the vector into documents, and the one test caller (test-definitions.R:401) uses grepl() and positional subsetting.

Verified by running the four test files that touch this path: test-context-layer.R (65 passing), test-data-dictionary.R (71 passing, 3 skipped on CRAN), test-definitions.R (114 passing), test-citations.R (84 passing). I skipped the full R suite — it hangs ~10 minutes on run_r's uncancelled later callbacks, fixed in #268.

One thing worth scrutiny: the new runner in test-context-layer.R feeds each fixture case through the real new_data_dictionary() reader, which works because both languages parse the same authored data-dict.yaml. If that ever diverges, this runner is where it will surface.

No hand-written tests were deleted; the new test is additive. The generated fixture pkg-r/tests/testthat/fixtures/shared/context_layer.json comes from scripts/sync-shared-fixtures.sh and needs no review.

@jat255 jat255 added this to the py-M4: context layer milestone Sep 6, 2026
@jat255 jat255 added py Affects the Python implementation needs-manual-review Agent-created work that needs a human review labels Sep 6, 2026
The dictionary already turns its prose into retrieval chunks in both
packages, but nothing held the two to the same answer. Add a
dictionary_context_chunks section to the shared context_layer fixture and
a runner in each suite, driving each package's own reader from the same
parsed shape.

The fixture exposed one divergence: R named its table chunks after their
tables, because vapply() over names() carries them, and those names rode
into the layer's documents. Chunks are text, so drop the names. Neither
call site read them.

Governed definitions carry no case. The two packages compile them at
different constructors, so they disagree on whether a dictionary that is
not attached to a source has any; definitions.json pins the chunk text.

Python needs no new chunking function: the dictionary owns it as
DataDictionary.context_chunks(). What was missing was the guard for a
source with no dictionary at all, which is what _dictionary_chunks()
adds and what folding source prose into a layer needs next.
@jat255
jat255 force-pushed the jat255/context-layer-dictionary-chunks branch from 7551a43 to da628ae Compare September 7, 2026 00:40
@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown

Preview deployed to Connect (dogfood.team.pct.posit.it): https://dogfood.team.pct.posit.it/connect/#/apps/d7a36cae-8f27-448b-a478-61b81fbe3942/draft/368305

Deployed from commit 6cbba98.

@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown

Preview deployed to Connect (connect.staging.pct.posit.it): https://connect.staging.pct.posit.it/connect/#/apps/ad662e1b-5048-4acc-9ad7-f9478c92274e/draft/2656

Deployed from commit 6cbba98.

Review follow-ups. Restore the empty-fixture guard on the
strip_frontmatter runner: tests/shared/README.md requires it of every
runner that enumerates cases. Pin that empty strings are dropped from
dictionary chunks, and pin table declaration order through the sequence
shape that authored data-dict.yaml files use, rather than only the
pre-keyed mapping.
Simon pushed back on the strip_frontmatter runner's guard on #240, and
the dictionary_context_chunks runner re-added the same pattern; drop it
from both so the file stays consistent with the reviewed state. The
Python suite keeps its standalone not-empty tests, the shape #240
approved.
@jat255 jat255 added the r Affects the R implementation label Sep 7, 2026
@jat255
jat255 requested a review from simonpcouch September 7, 2026 01:29
@jat255 jat255 removed the needs-manual-review Agent-created work that needs a human review label Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

py Affects the Python implementation r Affects the R implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant