-
Notifications
You must be signed in to change notification settings - Fork 0
api: emit canonical heading path on treewalk citations (HAL-70) #41
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
Merged
hallelx2
merged 2 commits into
main
from
halleluyaholudele/hal-70-engine-fix-citation-path-correctness-in-retrieval
Jun 17, 2026
+164
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "log/slog" | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| "github.com/hallelx2/vectorless-engine/pkg/retrieval" | ||
| "github.com/hallelx2/vectorless-engine/pkg/tree" | ||
| ) | ||
|
|
||
| // stubTOCProvider returns canned TOC bytes (or an error) for the | ||
| // citation builder's heading-path lookup. | ||
| type stubTOCProvider struct { | ||
| raw []byte | ||
| err error | ||
| } | ||
|
|
||
| func (s stubTOCProvider) GetTOC(context.Context, tree.DocumentID) ([]byte, error) { | ||
| return s.raw, s.err | ||
| } | ||
|
|
||
| // citationTestTOC mirrors buildTreeWalkTestTree's page layout as a | ||
| // logical outline: Setup{Install 1-2, Configuration 3-4}, | ||
| // Usage{Querying 5-7, Debt 8-9}. | ||
| func citationTestTOC(t *testing.T) []byte { | ||
| t.Helper() | ||
| toc := []tree.TOCNode{ | ||
| {Title: "Setup", StartPage: 1, EndPage: 4, Nodes: []tree.TOCNode{ | ||
| {Title: "Install", StartPage: 1, EndPage: 2}, | ||
| {Title: "Configuration", StartPage: 3, EndPage: 4}, | ||
| }}, | ||
| {Title: "Usage", StartPage: 5, EndPage: 9, Nodes: []tree.TOCNode{ | ||
| {Title: "Querying", StartPage: 5, EndPage: 7}, | ||
| {Title: "Debt", StartPage: 8, EndPage: 9}, | ||
| }}, | ||
| } | ||
| raw, err := json.Marshal(toc) | ||
| if err != nil { | ||
| t.Fatalf("marshal toc: %v", err) | ||
| } | ||
| return raw | ||
| } | ||
|
|
||
| // depsWithTOC builds a minimal Deps for buildTreeWalkCitations: no LLM | ||
| // (so quote extraction is skipped) and a stub TOC provider on the | ||
| // strategy. | ||
| func depsWithTOC(toc []byte, tocErr error) Deps { | ||
| return Deps{ | ||
| Logger: slog.Default(), | ||
| TreeWalkStrategy: &retrieval.TreeWalkStrategy{ | ||
| TOC: stubTOCProvider{raw: toc, err: tocErr}, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // TestBuildTreeWalkCitations_EmitsHeadingPath is the HAL-70 regression: | ||
| // a citation must carry the canonical heading path of its primary | ||
| // section, resolved from the TOC — the field the bench's | ||
| // path_correct@1 metric reads. | ||
| func TestBuildTreeWalkCitations_EmitsHeadingPath(t *testing.T) { | ||
| d := depsWithTOC(citationTestTOC(t), nil) | ||
| tr := buildTreeWalkTestTree() | ||
| res := &retrieval.Result{CitedPages: [][2]int{{1, 2}}} | ||
|
|
||
| cites := d.buildTreeWalkCitations(context.Background(), tr, res, "how do I install?", "") | ||
| if len(cites) != 1 { | ||
| t.Fatalf("want 1 citation, got %d: %v", len(cites), cites) | ||
| } | ||
| c := cites[0] | ||
|
|
||
| // Primary section for pages 1-2 is the leaf sec_a1 (Install). | ||
| ids, _ := c["section_ids"].([]tree.SectionID) | ||
| if len(ids) == 0 || ids[0] != "sec_a1" { | ||
| t.Fatalf("expected primary section sec_a1, got %v", c["section_ids"]) | ||
| } | ||
|
|
||
| got, ok := c["title_path"].([]string) | ||
| if !ok { | ||
| t.Fatalf("citation is missing a title_path: %#v", c) | ||
| } | ||
| if want := []string{"Setup", "Install"}; !reflect.DeepEqual(got, want) { | ||
| t.Fatalf("title_path mismatch: got=%v want=%v", got, want) | ||
| } | ||
| } | ||
|
|
||
| // TestBuildTreeWalkCitations_NoTOCOmitsHeadingPath: when no TOC is | ||
| // persisted (ErrNoTOC), the citation degrades gracefully — section_ids | ||
| // and pages are still present, title_path is simply absent. | ||
| func TestBuildTreeWalkCitations_NoTOCOmitsHeadingPath(t *testing.T) { | ||
| d := depsWithTOC(nil, retrieval.ErrNoTOC) | ||
| tr := buildTreeWalkTestTree() | ||
| res := &retrieval.Result{CitedPages: [][2]int{{5, 7}}} | ||
|
|
||
| cites := d.buildTreeWalkCitations(context.Background(), tr, res, "how do I query?", "") | ||
| if len(cites) != 1 { | ||
| t.Fatalf("want 1 citation, got %d", len(cites)) | ||
| } | ||
| if _, present := cites[0]["title_path"]; present { | ||
| t.Fatalf("title_path must be absent without a TOC, got %v", cites[0]["title_path"]) | ||
| } | ||
| if _, present := cites[0]["section_ids"]; !present { | ||
| t.Fatalf("section_ids must still be present") | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
suggestion (testing): Consider adding a test where the primary section has no corresponding heading path or an empty path to ensure title_path is omitted in that case.
Since the code branches on
if hp := headingPaths[sectionIDs[0]]; len(hp) > 0 { ... }, it’s important to also cover the cases where (a) there is no entry for the primary section ID, and (b) the entry exists but is empty. Please add a test using a TOC that produces either a missing or empty heading path for the primary section and assert thattitle_pathis omitted whilesection_idsand pages stay unchanged. This will guard against regressions where an empty or missing path incorrectly yields atitle_path.Suggested implementation:
internal/api/treewalk_citations_test.goimportsreflectif it does not already:reflectto the existing import block:import (... "reflect" ...).HeadingPathson the TOC value returned bycitationTestTOC(t)is assumed to exist and to have typemap[tree.SectionID][]string. If the actual field or type differs, adjusttocNoPaths.HeadingPaths = map[tree.SectionID][]string{}accordingly so that, for the primary section ID, either there is no map entry or the entry is an empty slice, making thelen(hp) > 0branch false."pages"or a different type than[][2]int, adapt the casts and the key name to match the existing tests in this file.