From b71b31c897efae7b6b8320ab0e5bdf796eb1e006 Mon Sep 17 00:00:00 2001 From: linus-liu-web <11640009+linus-liu-web@users.noreply.github.com> Date: Tue, 29 Sep 2026 20:50:12 +0800 Subject: [PATCH] =?UTF-8?q?fix(ima):=20=E7=AC=94=E8=AE=B0=E6=AD=A3?= =?UTF-8?q?=E6=96=87=E8=A2=AB=E6=B8=85=E7=A9=BA=E5=90=8E=E6=97=A7=E6=AD=A3?= =?UTF-8?q?=E6=96=87=E4=BB=8D=E7=95=99=E5=9C=A8=E7=9F=A5=E8=AF=86=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fetchNote 在正文为空时返回 fetchSkipped,walk 只把该 key 记进增量游标而 不产出任何 item。于是重新读到一篇已被清空的笔记时,入库层收不到「内容变 空」的信号:旧正文既不会被替换,游标还把它记为已同步。 Notion(b9be7d0d)与 Confluence 早就是这个写法:正文为空时仍然产出只有 标题的 markdown,让入库去替换旧正文。这里对齐同一写法,空正文改成 "# \n" 照常走 fetchOK。 --- .../datasource/connector/ima/connector.go | 7 ++- .../connector/ima/connector_test.go | 60 ++++++++++++++++++- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/internal/datasource/connector/ima/connector.go b/internal/datasource/connector/ima/connector.go index 4bf3ba17..dc07cd4e 100644 --- a/internal/datasource/connector/ima/connector.go +++ b/internal/datasource/connector/ima/connector.go @@ -419,8 +419,11 @@ func fetchNote( return types.FetchedItem{}, fetchFailed } if strings.TrimSpace(content) == "" { - logger.Infof(ctx, "[IMA] note %s (title=%q) is empty, skipping", noteID, f.Title) - return types.FetchedItem{}, fetchSkipped + // A cleared note still exists. Keep its title as content so ingestion + // replaces the stale body instead of silently acknowledging the edit: + // skipping here would leave the old text indexed forever. + logger.Infof(ctx, "[IMA] note %s (title=%q) is empty, syncing its title only", noteID, f.Title) + content = "# " + f.Title + "\n" } fileName := datasource.SanitizeFileName(f.Title) diff --git a/internal/datasource/connector/ima/connector_test.go b/internal/datasource/connector/ima/connector_test.go index 7a99f790..66cad9b6 100644 --- a/internal/datasource/connector/ima/connector_test.go +++ b/internal/datasource/connector/ima/connector_test.go @@ -276,7 +276,10 @@ func TestFetchIncremental_NoteReadFailureIsRetried(t *testing.T) { } } -func TestFetchAll_EmptyNoteIsSkipped(t *testing.T) { +// TestFetchAll_EmptyNoteSyncsTitleOnly covers a note whose body was cleared in +// IMA. The note still exists, so it must be emitted with its title as content: +// skipping it would leave whatever was indexed before the edit in place. +func TestFetchAll_EmptyNoteSyncsTitleOnly(t *testing.T) { f := newFakeIMA(t) f.setKB("kb1", []fakeFile{{ MediaID: "note_1", Title: "Blank", MediaType: mediaTypeNote, @@ -287,8 +290,59 @@ func TestFetchAll_EmptyNoteIsSkipped(t *testing.T) { if err != nil { t.Fatalf("FetchAll: %v", err) } - if len(items) != 0 { - t.Fatalf("an empty note must be skipped, got %s", describeItems(items)) + note := mustFindItem(t, items, logicalKey("kb1", "", "Blank")) + if string(note.Content) != "# Blank\n" { + t.Errorf("content = %q, want the title as markdown", note.Content) + } + if note.ContentType != "text/markdown" { + t.Errorf("ContentType = %q, want text/markdown", note.ContentType) + } + if !strings.HasSuffix(note.FileName, ".md") { + t.Errorf("FileName = %q, want an .md suffix", note.FileName) + } +} + +// TestFetchAll_ClearedNoteReplacesStaleContent is the regression test for stale +// note bodies: after a note that was already ingested is cleared in IMA, the +// next sync that re-reads it must emit an item with the same external id so +// ingestion overwrites the old text instead of keeping it indexed forever. +func TestFetchAll_ClearedNoteReplacesStaleContent(t *testing.T) { + f := newFakeIMA(t) + note := fakeFile{ + MediaID: "note_1", Title: "Runbook", MediaType: mediaTypeNote, + NotebookID: "777", NoteBody: "# Runbook\nold body", + } + f.setKB("kb1", []fakeFile{note}) + + c := NewConnector() + cfg := f.config("kb1") + key := logicalKey("kb1", "", "Runbook") + + items, err := c.FetchAll(context.Background(), cfg, []string{"kb1"}) + if err != nil { + t.Fatalf("first FetchAll: %v", err) + } + first := mustFindItem(t, items, key) + if string(first.Content) != "# Runbook\nold body" { + t.Fatalf("first content = %q, want the note body", first.Content) + } + + // The user clears the note body. The note itself still exists and keeps the + // same media_id, so only a sync that re-reads it can notice. + note.NoteBody = "" + f.setKB("kb1", []fakeFile{note}) + + items2, err := c.FetchAll(context.Background(), cfg, []string{"kb1"}) + if err != nil { + t.Fatalf("second FetchAll: %v", err) + } + replacement := mustFindItem(t, items2, key) + if replacement.ExternalID != first.ExternalID { + t.Errorf("external id = %q, want the original %q so ingestion updates in place", + replacement.ExternalID, first.ExternalID) + } + if string(replacement.Content) != "# Runbook\n" { + t.Errorf("content after clearing = %q, want the title only", replacement.Content) } }