diff --git a/integrations/openclaw/bm-client.ts b/integrations/openclaw/bm-client.ts index f82d2ff80..97b67a388 100644 --- a/integrations/openclaw/bm-client.ts +++ b/integrations/openclaw/bm-client.ts @@ -202,23 +202,39 @@ export interface WorkspaceResult { workspace_type: string | null } +// The bm_schema response shapes. They mirror the backend models in +// src/basic_memory/schemas/schema.py (ValidationReport, InferenceReport, +// DriftReport); the client casts the payload without translating fields, so +// a name that drifts from the backend renders as undefined (#1997). +// JSON responses drop null fields, so nullable backend fields are optional. +export interface SchemaNoteValidation { + note_identifier: string + schema_entity: string + passed: boolean + warnings: string[] + errors: string[] +} + export interface SchemaValidationResult { - entity_type: string | null + note_type?: string | null total_notes: number total_entities: number valid_count: number warning_count: number error_count: number - results: Array<{ - identifier: string - valid: boolean - warnings: string[] - errors: string[] + results: SchemaNoteValidation[] + type_summaries?: Array<{ + note_type: string + total_notes: number + total_entities: number + valid_count: number + warning_count: number + error_count: number }> } export interface SchemaInferResult { - entity_type: string + note_type: string notes_analyzed: number field_frequencies: Array<{ name: string @@ -236,18 +252,21 @@ export interface SchemaInferResult { excluded: string[] } +export interface SchemaDriftField { + name: string + source: string + count: number + total: number + percentage: number +} + export interface SchemaDiffResult { - entity_type: string + note_type: string schema_found: boolean - new_fields: Array<{ - name: string - source: string - count: number - total: number - percentage: number - }> - dropped_fields: Array<{ name: string; source: string; declared_in?: string }> + new_fields: SchemaDriftField[] + dropped_fields: SchemaDriftField[] cardinality_changes: string[] + notes_compared?: number | null } function getErrorMessage(err: unknown): string { diff --git a/integrations/openclaw/integration/bm-client.integration.test.ts b/integrations/openclaw/integration/bm-client.integration.test.ts index c06c35d6f..4c56a5ec3 100644 --- a/integrations/openclaw/integration/bm-client.integration.test.ts +++ b/integrations/openclaw/integration/bm-client.integration.test.ts @@ -9,6 +9,9 @@ import type { OpenClawPluginApi } from "openclaw/plugin-sdk/plugin-entry" import { BmClient } from "../bm-client.ts" import { registerProjectListTool } from "../tools/list-memory-projects.ts" import { registerReadTool } from "../tools/read-note.ts" +import { registerSchemaDiffTool } from "../tools/schema-diff.ts" +import { registerSchemaInferTool } from "../tools/schema-infer.ts" +import { registerSchemaValidateTool } from "../tools/schema-validate.ts" import { registerWriteTool } from "../tools/write-note.ts" const RUN_INTEGRATION = process.env.BM_INTEGRATION === "1" @@ -369,6 +372,93 @@ if (!RUN_INTEGRATION) { expect(after.details.checksum).not.toBe(checksum) }) + // Regression (#1997): the schema tools read field names BM never sent + // (entity_type, identifier, valid, declared_in), so every note rendered as + // "**undefined** — invalid". Unit fixtures are hand-written; this checks + // the plugin against a real bm_schema response. + it("renders schema tool results from real bm_schema responses", async () => { + const { client } = getHarness() + const tools = new Map< + string, + ( + id: string, + params: Record, + ) => Promise<{ content: Array<{ text: string }> }> + >() + const api = { + registerTool: (tool: { name: string; execute: any }) => { + tools.set(tool.name, tool.execute) + }, + } as unknown as OpenClawPluginApi + registerSchemaValidateTool(api, client) + registerSchemaInferTool(api, client) + registerSchemaDiffTool(api, client) + const text = async (name: string, params: Record) => { + const execute = tools.get(name) + if (!execute) throw new Error(`tool ${name} was not registered`) + return (await execute("integration", params)).content[0].text + } + + await client.writeNote( + "Contact", + "# Contact", + "schema", + undefined, + false, + { + noteType: "schema", + metadata: { + entity: "contact", + version: 1, + schema: { name: "string, full name", "fax?": "string, legacy" }, + settings: { validation: "warn" }, + }, + }, + ) + await client.writeNote( + "Ada", + "# Ada\n\n- [name] Ada Lovelace", + "contacts", + undefined, + false, + { noteType: "contact" }, + ) + await client.writeNote( + "Bob", + "# Bob\n\n- [role] Analyst", + "contacts", + undefined, + false, + { noteType: "contact" }, + ) + + // The raw payload carries every field the plugin's types declare. + const raw = await client.schemaValidate("contact") + expect(raw.note_type).toBe("contact") + expect(raw.results.map((r) => [r.note_identifier, r.passed])).toEqual( + expect.arrayContaining([ + ["Ada", true], + ["Bob", true], + ]), + ) + + const validated = await text("schema_validate", { noteType: "contact" }) + expect(validated).toContain("**Type:** contact") + expect(validated).toMatch(/- \*\*Ada\*\* — valid/) + // Warn mode: a missing required field is a warning, and the note passes. + expect(validated).toMatch(/- \*\*Bob\*\* — valid\n {2}- warning: /) + expect(validated).not.toContain("undefined") + + const inferred = await text("schema_infer", { noteType: "contact" }) + expect(inferred).toContain("**Type:** contact") + expect(inferred).not.toContain("undefined") + + const drift = await text("schema_diff", { noteType: "contact" }) + expect(drift).toContain("**Type:** contact") + expect(drift).toContain("- **fax** (") + expect(drift).not.toContain("undefined") + }) + it("lists the integration project through real BM MCP", async () => { const { client, projectName } = getHarness() const projects = await client.listProjects() diff --git a/integrations/openclaw/tools/schema-diff.test.ts b/integrations/openclaw/tools/schema-diff.test.ts index 66a5bb42f..db77d5f0c 100644 --- a/integrations/openclaw/tools/schema-diff.test.ts +++ b/integrations/openclaw/tools/schema-diff.test.ts @@ -39,7 +39,7 @@ describe("schema-diff tool", () => { it("should show drift when fields differ", async () => { ;(mockClient.schemaDiff as jest.MockedFunction).mockResolvedValue({ - entity_type: "person", + note_type: "person", schema_found: true, new_fields: [ { @@ -50,23 +50,35 @@ describe("schema-diff tool", () => { percentage: 0.6, }, ], + // BM's DriftFieldResponse has no declared_in (#1997). dropped_fields: [ - { name: "fax", source: "observation", declared_in: "schema" }, + { + name: "fax", + source: "observation", + count: 1, + total: 10, + percentage: 0.1, + }, ], cardinality_changes: [], + notes_compared: 10, }) const result = await execute("call-1", { noteType: "person" }) expect(mockClient.schemaDiff).toHaveBeenCalledWith("person", undefined) + expect(result.content[0].text).toContain("**Type:** person") expect(result.content[0].text).toContain("phone") - expect(result.content[0].text).toContain("fax") + expect(result.content[0].text).toContain( + "- **fax** (observation) — 10% (1/10)", + ) + expect(result.content[0].text).not.toContain("undefined") expect(result.details.new_fields).toHaveLength(1) }) it("should show no drift when in sync", async () => { ;(mockClient.schemaDiff as jest.MockedFunction).mockResolvedValue({ - entity_type: "person", + note_type: "person", schema_found: true, new_fields: [], dropped_fields: [], @@ -80,7 +92,7 @@ describe("schema-diff tool", () => { it("should handle missing schema", async () => { ;(mockClient.schemaDiff as jest.MockedFunction).mockResolvedValue({ - entity_type: "unknown", + note_type: "unknown", schema_found: false, new_fields: [], dropped_fields: [], @@ -95,7 +107,7 @@ describe("schema-diff tool", () => { it("should pass project to client.schemaDiff", async () => { ;(mockClient.schemaDiff as jest.MockedFunction).mockResolvedValue({ - entity_type: "person", + note_type: "person", schema_found: true, new_fields: [], dropped_fields: [], diff --git a/integrations/openclaw/tools/schema-diff.ts b/integrations/openclaw/tools/schema-diff.ts index 08f18646d..105062538 100644 --- a/integrations/openclaw/tools/schema-diff.ts +++ b/integrations/openclaw/tools/schema-diff.ts @@ -50,7 +50,7 @@ export function registerSchemaDiffTool( } } - const lines: string[] = [`**Type:** ${result.entity_type}`] + const lines: string[] = [`**Type:** ${result.note_type}`] if ( result.new_fields.length === 0 && @@ -69,9 +69,13 @@ export function registerSchemaDiffTool( } if (result.dropped_fields.length > 0) { - lines.push("", "### Dropped Fields (in schema, not in notes)") + // A dropped field can still appear in a few notes, so report how + // often it does, as BM's own drift report does. + lines.push("", "### Dropped Fields (in schema, rare in notes)") for (const f of result.dropped_fields) { - lines.push(`- **${f.name}** — declared in ${f.declared_in}`) + lines.push( + `- **${f.name}** (${f.source}) — ${(f.percentage * 100).toFixed(0)}% (${f.count}/${f.total})`, + ) } } diff --git a/integrations/openclaw/tools/schema-infer.test.ts b/integrations/openclaw/tools/schema-infer.test.ts index 58c338be7..891b282b0 100644 --- a/integrations/openclaw/tools/schema-infer.test.ts +++ b/integrations/openclaw/tools/schema-infer.test.ts @@ -39,7 +39,7 @@ describe("schema-infer tool", () => { it("should infer schema for a note type", async () => { ;(mockClient.schemaInfer as jest.MockedFunction).mockResolvedValue({ - entity_type: "person", + note_type: "person", notes_analyzed: 12, field_frequencies: [ { @@ -70,7 +70,9 @@ describe("schema-infer tool", () => { undefined, undefined, ) - expect(result.content[0].text).toContain("person") + // Regression (#1997): the type heading reads BM's note_type. + expect(result.content[0].text).toContain("**Type:** person") + expect(result.content[0].text).not.toContain("undefined") expect(result.content[0].text).toContain("12") expect(result.content[0].text).toContain("name") expect(result.details.suggested_required).toContain("name") @@ -78,7 +80,7 @@ describe("schema-infer tool", () => { it("should pass custom threshold", async () => { ;(mockClient.schemaInfer as jest.MockedFunction).mockResolvedValue({ - entity_type: "task", + note_type: "task", notes_analyzed: 5, field_frequencies: [], suggested_schema: {}, @@ -98,7 +100,7 @@ describe("schema-infer tool", () => { it("should pass project to client.schemaInfer", async () => { ;(mockClient.schemaInfer as jest.MockedFunction).mockResolvedValue({ - entity_type: "person", + note_type: "person", notes_analyzed: 1, field_frequencies: [], suggested_schema: {}, diff --git a/integrations/openclaw/tools/schema-infer.ts b/integrations/openclaw/tools/schema-infer.ts index fdcb3595a..47bdc46f8 100644 --- a/integrations/openclaw/tools/schema-infer.ts +++ b/integrations/openclaw/tools/schema-infer.ts @@ -47,7 +47,7 @@ export function registerSchemaInferTool( ) const lines: string[] = [ - `**Type:** ${result.entity_type}`, + `**Type:** ${result.note_type}`, `**Notes analyzed:** ${result.notes_analyzed}`, ] diff --git a/integrations/openclaw/tools/schema-validate.test.ts b/integrations/openclaw/tools/schema-validate.test.ts index e264c11c3..0c25c1981 100644 --- a/integrations/openclaw/tools/schema-validate.test.ts +++ b/integrations/openclaw/tools/schema-validate.test.ts @@ -37,24 +37,53 @@ describe("schema-validate tool", () => { execute = call[0].execute }) - it("should validate by note type", async () => { + // Regression (#1997): fixtures follow BM's ValidationReport + // (src/basic_memory/schemas/schema.py). The tool used to read identifier, + // valid and entity_type, which BM never sends, so every note rendered as + // "**undefined** — invalid". + it("renders each note by note_identifier and its passed result", async () => { ;( mockClient.schemaValidate as jest.MockedFunction ).mockResolvedValue({ - entity_type: "person", - total_notes: 5, - total_entities: 5, - valid_count: 4, + note_type: "person", + total_notes: 3, + total_entities: 3, + valid_count: 2, warning_count: 1, - error_count: 0, + error_count: 1, results: [ { - identifier: "john-doe", - valid: false, - warnings: ["missing optional field: email"], + note_identifier: "people/ada", + schema_entity: "schema/person", + passed: true, + field_results: [], + unmatched_observations: {}, + unmatched_relations: [], + warnings: [], + errors: [], + }, + { + note_identifier: "people/bob", + schema_entity: "schema/person", + passed: true, + field_results: [], + unmatched_observations: {}, + unmatched_relations: [], + warnings: ["Missing optional field: email"], errors: [], }, + { + note_identifier: "people/cy", + schema_entity: "schema/person", + passed: false, + field_results: [], + unmatched_observations: {}, + unmatched_relations: [], + warnings: [], + errors: ["Missing required field: name"], + }, ], + type_summaries: [], }) const result = await execute("call-1", { noteType: "person" }) @@ -64,23 +93,41 @@ describe("schema-validate tool", () => { undefined, undefined, ) - expect(result.content[0].text).toContain("person") - expect(result.content[0].text).toContain("Valid:** 4") - expect(result.content[0].text).toContain("john-doe") - expect(result.details.valid_count).toBe(4) + expect(result.content[0].text).toBe( + [ + "**Type:** person", + "**Notes:** 3 | **Valid:** 2 | **Warnings:** 1 | **Errors:** 1", + "", + "- **people/ada** — valid", + // A warning does not fail a note: passed stays true. + "- **people/bob** — valid", + " - warning: Missing optional field: email", + "- **people/cy** — invalid", + " - error: Missing required field: name", + ].join("\n"), + ) + expect(result.content[0].text).not.toContain("undefined") + expect(result.details.valid_count).toBe(2) }) it("should validate by identifier", async () => { ;( mockClient.schemaValidate as jest.MockedFunction ).mockResolvedValue({ - entity_type: null, total_notes: 1, total_entities: 1, valid_count: 1, warning_count: 0, error_count: 0, - results: [], + results: [ + { + note_identifier: "notes/my-note", + schema_entity: "schema/note", + passed: true, + warnings: [], + errors: [], + }, + ], }) const result = await execute("call-1", { @@ -92,14 +139,17 @@ describe("schema-validate tool", () => { "notes/my-note", undefined, ) - expect(result.content[0].text).toContain("Valid:** 1") + // BM drops a null note_type from JSON, so no Type line is rendered. + expect(result.content[0].text).toBe( + "**Notes:** 1 | **Valid:** 1 | **Warnings:** 0 | **Errors:** 0\n\n- **notes/my-note** — valid", + ) }) it("should pass project to client.schemaValidate", async () => { ;( mockClient.schemaValidate as jest.MockedFunction ).mockResolvedValue({ - entity_type: "person", + note_type: "person", total_notes: 1, total_entities: 1, valid_count: 1, diff --git a/integrations/openclaw/tools/schema-validate.ts b/integrations/openclaw/tools/schema-validate.ts index 8cc393d0e..7e1682387 100644 --- a/integrations/openclaw/tools/schema-validate.ts +++ b/integrations/openclaw/tools/schema-validate.ts @@ -58,8 +58,8 @@ export function registerSchemaValidateTool( } const lines: string[] = [] - if (result.entity_type) { - lines.push(`**Type:** ${result.entity_type}`) + if (result.note_type) { + lines.push(`**Type:** ${result.note_type}`) } lines.push( `**Notes:** ${result.total_notes ?? 0} | **Valid:** ${result.valid_count ?? 0} | **Warnings:** ${result.warning_count ?? 0} | **Errors:** ${result.error_count ?? 0}`, @@ -68,8 +68,9 @@ export function registerSchemaValidateTool( if (result.results && result.results.length > 0) { lines.push("") for (const r of result.results) { - const status = r.valid ? "valid" : "invalid" - lines.push(`- **${r.identifier}** — ${status}`) + // passed is true when there are no errors; warnings still pass. + const status = r.passed ? "valid" : "invalid" + lines.push(`- **${r.note_identifier}** — ${status}`) for (const w of r.warnings) { lines.push(` - warning: ${w}`) }