fix(extension): fall back to chunk when memory is whitespace-only

Graphite review: memory || chunk treated "   " as truthy and dropped a
valid chunk. Trim both fields before choosing.
This commit is contained in:
James Yang 2026-08-06 15:40:09 -04:00
parent 4d02f6d79c
commit c5db753fe3
3 changed files with 18 additions and 8 deletions

View file

@ -92,12 +92,13 @@ export const HYBRID_RECALL_FIXTURES: HybridRecallFixture[] = [
},
{
id: "empty-and-whitespace-noise",
description: "Empty / whitespace hits must not invent prompt lines",
expectedTexts: ["Keep this fact"],
chunkOnlyTexts: [],
description:
"Whitespace-only memory must not block a valid chunk; blanks invent no lines",
expectedTexts: ["Keep this fact", "Important document text"],
chunkOnlyTexts: ["Important document text"],
results: [
{ memory: "Keep this fact" },
{ memory: " " },
{ memory: " ", chunk: "Important document text" },
{ chunk: "" },
{},
],

View file

@ -37,6 +37,15 @@ describe("formatSearchHitText", () => {
)
})
it("falls back to chunk when memory is only whitespace", () => {
expect(
formatSearchHitText({
memory: " ",
chunk: "Important document text",
}),
).toBe("Important document text")
})
it("returns null for empty hits", () => {
expect(formatSearchHitText({})).toBeNull()
expect(formatSearchHitText({ memory: " " })).toBeNull()

View file

@ -23,10 +23,10 @@ export function buildSearchMemoriesBody(
/** Prefer extracted memory text; fall back to document chunk. */
export function formatSearchHitText(hit: SearchHit): string | null {
const text = hit.memory || hit.chunk
if (typeof text !== "string") return null
const trimmed = text.trim()
return trimmed.length > 0 ? trimmed : null
const memory = typeof hit.memory === "string" ? hit.memory.trim() : ""
const chunk = typeof hit.chunk === "string" ? hit.chunk.trim() : ""
const text = memory || chunk
return text.length > 0 ? text : null
}
/** Numbered prompt lines for Included Memories; skips empty hits. */