Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 35 additions & 16 deletions integrations/openclaw/bm-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
90 changes: 90 additions & 0 deletions integrations/openclaw/integration/bm-client.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<string, unknown>,
) => 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<string, unknown>) => {
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()
Expand Down
24 changes: 18 additions & 6 deletions integrations/openclaw/tools/schema-diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("schema-diff tool", () => {

it("should show drift when fields differ", async () => {
;(mockClient.schemaDiff as jest.MockedFunction<any>).mockResolvedValue({
entity_type: "person",
note_type: "person",
schema_found: true,
new_fields: [
{
Expand All @@ -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<any>).mockResolvedValue({
entity_type: "person",
note_type: "person",
schema_found: true,
new_fields: [],
dropped_fields: [],
Expand All @@ -80,7 +92,7 @@ describe("schema-diff tool", () => {

it("should handle missing schema", async () => {
;(mockClient.schemaDiff as jest.MockedFunction<any>).mockResolvedValue({
entity_type: "unknown",
note_type: "unknown",
schema_found: false,
new_fields: [],
dropped_fields: [],
Expand All @@ -95,7 +107,7 @@ describe("schema-diff tool", () => {

it("should pass project to client.schemaDiff", async () => {
;(mockClient.schemaDiff as jest.MockedFunction<any>).mockResolvedValue({
entity_type: "person",
note_type: "person",
schema_found: true,
new_fields: [],
dropped_fields: [],
Expand Down
10 changes: 7 additions & 3 deletions integrations/openclaw/tools/schema-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -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})`,
)
}
}

Expand Down
10 changes: 6 additions & 4 deletions integrations/openclaw/tools/schema-infer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("schema-infer tool", () => {

it("should infer schema for a note type", async () => {
;(mockClient.schemaInfer as jest.MockedFunction<any>).mockResolvedValue({
entity_type: "person",
note_type: "person",
notes_analyzed: 12,
field_frequencies: [
{
Expand Down Expand Up @@ -70,15 +70,17 @@ 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")
})

it("should pass custom threshold", async () => {
;(mockClient.schemaInfer as jest.MockedFunction<any>).mockResolvedValue({
entity_type: "task",
note_type: "task",
notes_analyzed: 5,
field_frequencies: [],
suggested_schema: {},
Expand All @@ -98,7 +100,7 @@ describe("schema-infer tool", () => {

it("should pass project to client.schemaInfer", async () => {
;(mockClient.schemaInfer as jest.MockedFunction<any>).mockResolvedValue({
entity_type: "person",
note_type: "person",
notes_analyzed: 1,
field_frequencies: [],
suggested_schema: {},
Expand Down
2 changes: 1 addition & 1 deletion integrations/openclaw/tools/schema-infer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function registerSchemaInferTool(
)

const lines: string[] = [
`**Type:** ${result.entity_type}`,
`**Type:** ${result.note_type}`,
`**Notes analyzed:** ${result.notes_analyzed}`,
]

Expand Down
Loading
Loading