From ee233859a7247f3e4f43e6facdc2ddf30540f9d0 Mon Sep 17 00:00:00 2001 From: bendtherules Date: Wed, 22 Apr 2026 13:43:13 +0530 Subject: [PATCH] fix(agent-tools): include recursively fetched child sections in output Previously when recursive=true, child sections were fetched into sectionsData but never included in the returned sections array. Now child sections are properly appended to the output when recursive fetching is enabled. - Refactor section building from .map() to for..of for clarity - Add logic to append child sections not in original request - Add test coverage for recursive child section inclusion --- src/agent-tools/getSectionContent.ts | 57 ++++++++++++++++++++-------- src/test/getSectionContent.test.ts | 16 ++++++++ 2 files changed, 58 insertions(+), 15 deletions(-) diff --git a/src/agent-tools/getSectionContent.ts b/src/agent-tools/getSectionContent.ts index aa90f35..8091ecb 100644 --- a/src/agent-tools/getSectionContent.ts +++ b/src/agent-tools/getSectionContent.ts @@ -126,8 +126,10 @@ export function createGetSectionContentTool(table: Table) { // Sort by partindex to maintain order (nulls last for single-part sections) const sortedResults = results.sort((a: unknown, b: unknown) => { - const aIndex = (a as { partindex?: number }).partindex ?? Infinity; - const bIndex = (b as { partindex?: number }).partindex ?? Infinity; + const aIndex = + (a as { partindex?: number }).partindex ?? Infinity; + const bIndex = + (b as { partindex?: number }).partindex ?? Infinity; return aIndex - bIndex; }); @@ -168,27 +170,52 @@ export function createGetSectionContentTool(table: Table) { } } - // Build output array from all requested sections - // Missing sections are included with found: false and error message - const sections = sectionIds.map((id) => { + // Build output array from all fetched sections + // Missing originally requested sections are included with found: false + const sections: { + sectionId: string; + content: string; + found: boolean; + error?: string; + sectionTitle?: string; + childrensectionids?: string[]; + }[] = []; + + for (const id of sectionIds) { const data = sectionsData.get(id); if (data) { - return { + sections.push({ sectionId: id, content: data.content.join("\n\n"), found: true, sectionTitle: data.title, childrensectionids: data.childrenSectionIds, - }; + }); + } else { + sections.push({ + sectionId: id, + content: "", + found: false, + error: `Section '${id}' not found in database`, + }); } - return { - sectionId: id, - content: "", - found: false, - error: `Section '${id}' not found in database`, - }; - }); - + } + + // Include recursively fetched child sections that were not in the original request + if (recursive) { + for (const [id, data] of sectionsData) { + if (!sectionIds.includes(id)) { + sections.push({ + sectionId: id, + content: data.content.join("\n\n"), + found: true, + sectionTitle: data.title, + childrensectionids: data.childrenSectionIds, + }); + } + } + } + const totalSectionsFetched = sectionsData.size; const totalContentLength = sections.reduce( (sum, s) => sum + s.content.length, diff --git a/src/test/getSectionContent.test.ts b/src/test/getSectionContent.test.ts index 583ed59..fb019aa 100644 --- a/src/test/getSectionContent.test.ts +++ b/src/test/getSectionContent.test.ts @@ -108,6 +108,22 @@ describe("getSectionContent", () => { expect(requestedSection?.found).toBe(true); }); + test("should include recursively fetched child sections in output", async () => { + const result = await getContentTool({ + sectionIds: ["sec-catch-clause"], + recursive: true, + }); + + // sec-catch-clause has sec-try-statement as a child in mock data + const childSection = result.sections.find( + (s) => s.sectionId === "sec-try-statement", + ); + expect(childSection).toBeDefined(); + expect(childSection?.found).toBe(true); + expect(childSection?.content).toBeString(); + expect(childSection?.content.length).toBeGreaterThan(0); + }); + test("should preserve input order in output", async () => { const sectionIds = ["sec-for-statement", "sec-if-statement"]; const result = await getContentTool({