Skip to content

Commit 2675bce

Browse files
authored
Merge pull request #24 from basicmachines-co/claw/fix-conversation-capture
fix: Conversation capture — catch any editNote error, fall through to create
2 parents cdd0080 + ca51efe commit 2675bce

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

bm-client.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,18 +523,23 @@ describe("BmClient MCP behavior", () => {
523523
expect(result.file_path).toBe("archive/my-note.md")
524524
})
525525

526-
it("indexConversation does not create fallback note on non-not-found edit errors", async () => {
526+
it("indexConversation falls through to writeNote on any editNote error", async () => {
527527
;(client as any).editNote = jest
528528
.fn()
529529
.mockRejectedValue(new Error("validation failed"))
530-
;(client as any).writeNote = jest.fn()
530+
;(client as any).writeNote = jest.fn().mockResolvedValue({
531+
title: "conversations",
532+
permalink: "conversations",
533+
content: "x",
534+
file_path: "conversations/x.md",
535+
})
531536

532537
await client.indexConversation(
533538
"user message long enough",
534539
"assistant reply long enough",
535540
)
536541

537-
expect((client as any).writeNote).not.toHaveBeenCalled()
542+
expect((client as any).writeNote).toHaveBeenCalledTimes(1)
538543
})
539544

540545
it("indexConversation creates fallback note only on note-not-found errors", async () => {

bm-client.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ function isRecoverableConnectionError(err: unknown): boolean {
196196
)
197197
}
198198

199-
function isNoteNotFoundError(err: unknown): boolean {
199+
function _isNoteNotFoundError(err: unknown): boolean {
200200
const msg = getErrorMessage(err).toLowerCase()
201201
return (
202202
msg.includes("entity not found") ||
@@ -830,10 +830,6 @@ export class BmClient {
830830
return payload as unknown as MetadataSearchResult
831831
}
832832

833-
private isNoteNotFoundError(err: unknown): boolean {
834-
return isNoteNotFoundError(err)
835-
}
836-
837833
async indexConversation(
838834
userMessage: string,
839835
assistantResponse: string,
@@ -855,22 +851,33 @@ export class BmClient {
855851
"---",
856852
].join("\n")
857853

854+
// Try append first — if it fails for ANY reason, fall through to create
858855
try {
859856
await this.editNote(title, "append", entry)
860857
log.debug(`appended conversation to: ${title}`)
858+
return
861859
} catch (err) {
862-
if (!this.isNoteNotFoundError(err)) {
863-
log.error("conversation append failed", err)
864-
return
865-
}
860+
log.debug(`append failed, will create: ${getErrorMessage(err)}`)
861+
}
866862

867-
const content = [`# Conversations ${dateStr}`, "", entry].join("\n")
868-
try {
869-
await this.writeNote(title, content, "conversations")
870-
log.debug(`created conversation note: ${title}`)
871-
} catch (createErr) {
872-
log.error("conversation index failed", createErr)
873-
}
863+
// Create the note with frontmatter and first entry
864+
const content = [
865+
"---",
866+
`title: Conversations ${dateStr}`,
867+
"type: Conversation",
868+
`date: "${dateStr}"`,
869+
"---",
870+
"",
871+
`# Conversations ${dateStr}`,
872+
"",
873+
entry,
874+
].join("\n")
875+
876+
try {
877+
await this.writeNote(title, content, "conversations")
878+
log.debug(`created conversation note: ${title}`)
879+
} catch (err) {
880+
log.error("conversation index failed", err)
874881
}
875882
}
876883

0 commit comments

Comments
 (0)