Repository navigation
fix(flow-compress): index compressed memories so search finds them #1709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
afb9377
baafa78
58d52ec
3ce30df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,10 @@ import type { IIIClient } from "iii-sdk"; | |
| import type { StateKV } from "../state/kv.js"; | ||
| import { KV, generateId } from "../state/schema.js"; | ||
| import type { Action, ActionEdge, RoutineRun, MemoryProvider } from "../types.js"; | ||
| import { recordAudit } from "./audit.js"; | ||
| import { getAgentId } from "../config.js"; | ||
| import { logger } from "../logger.js"; | ||
| import { safeAudit } from "./audit.js"; | ||
| import { indexRecords } from "./search.js"; | ||
|
|
||
| const FLOW_COMPRESS_SYSTEM = `You are a workflow summarizer. Given a completed action chain, produce a concise summary capturing: | ||
| 1. The overall goal and outcome | ||
|
|
@@ -82,20 +85,40 @@ export function registerFlowCompressFunction( | |
| ); | ||
| const summary = parseFlowSummary(response); | ||
| const ts = new Date().toISOString(); | ||
| const agentId = getAgentId(); | ||
| const rawText = response | ||
| .replace(/<\/?(?:summary|goal|outcome|steps|discoveries|lesson)(?:\s[^>]*)?>/gi, " ") | ||
| .replace(/\s+/g, " ") | ||
| .trim(); | ||
| const project = | ||
| typeof data.project === "string" && data.project.length > 0 | ||
| ? doneActions.every((a) => a.project === data.project) | ||
| ? data.project | ||
| : undefined | ||
| : new Set(doneActions.map((a) => a.project)).size === 1 | ||
| ? doneActions[0].project | ||
| : undefined; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| const memory = { | ||
| id: generateId("mem"), | ||
| createdAt: ts, | ||
| updatedAt: ts, | ||
| type: "workflow" as const, | ||
| title: summary.goal || `Workflow: ${doneActions.length} actions`, | ||
| content: formatSummary(summary), | ||
| title: summary.goal || rawText.slice(0, 80) || `Workflow: ${doneActions.length} actions`, | ||
| // Fall back to the tag-stripped response so an unparseable or empty | ||
| // summary never produces an unsearchable memory or indexed markup. | ||
| content: | ||
| formatSummary(summary) || | ||
| rawText || | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| `Workflow with ${doneActions.length} completed actions`, | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| concepts: extractConcepts(doneActions), | ||
| files: extractFiles(doneActions), | ||
| sessionIds: [], | ||
| strength: 1.0, | ||
| version: 1, | ||
| isLatest: true, | ||
| ...(agentId ? { agentId } : {}), | ||
| ...(project !== undefined ? { project } : {}), | ||
| metadata: { | ||
| flowCompressed: true, | ||
| actionCount: doneActions.length, | ||
|
|
@@ -104,12 +127,21 @@ export function registerFlowCompressFunction( | |
| }; | ||
|
|
||
| await kv.set(KV.memories, memory.id, memory); | ||
| await recordAudit(kv, "compress", "mem::flow-compress", [memory.id], { | ||
| await safeAudit(kv, "compress", "mem::flow-compress", [memory.id], { | ||
| action: "compress_flow", | ||
| flowCompressed: true, | ||
| actionCount: doneActions.length, | ||
| project: data.project, | ||
| project, | ||
| }); | ||
| // Indexing failures are logged, not fatal: the KV write already | ||
| // committed and the restart rebuild is the backstop. | ||
| try { | ||
| await indexRecords([], [memory]); | ||
|
devin-ai-integration[bot] marked this conversation as resolved.
Resolved
|
||
| } catch (err) { | ||
| logger.warn("Flow-compress indexing failed; restart rebuild will recover", { | ||
| error: err instanceof Error ? err.message : String(err), | ||
| }); | ||
| } | ||
|
Comment on lines
129
to
+144
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| return { | ||
| success: true, | ||
|
|
@@ -166,7 +198,7 @@ function parseFlowSummary(response: string): { | |
| } { | ||
| const extract = (tag: string): string => { | ||
| const match = response.match( | ||
| new RegExp(`<${tag}>([\\s\\S]*?)</${tag}>`), | ||
| new RegExp(`<${tag}(?:\\s[^>]*)?>([\\s\\S]*?)</${tag}>`), | ||
| ); | ||
| return match ? match[1].trim() : ""; | ||
| }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import { registerFlowCompressFunction } from "../src/functions/flow-compress.js"; | ||
| import { getSearchIndex } from "../src/functions/search.js"; | ||
| import type { Action, Memory, MemoryProvider } from "../src/types.js"; | ||
| import type { IIIClient } from "iii-sdk"; | ||
| import type { StateKV } from "../src/state/kv.js"; | ||
|
|
||
| vi.mock("../src/logger.js", () => ({ logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() } })); | ||
|
|
||
| function doneAction(id: string, project = "proj-a"): Action { | ||
| const ts = "2026-11-20T00:00:00.000Z"; | ||
| return { | ||
| id, | ||
| title: `Finish ${id}`, | ||
| description: `Work for ${id}`, | ||
| status: "done", | ||
| priority: 1, | ||
| createdAt: ts, | ||
| updatedAt: ts, | ||
| createdBy: "agent-a", | ||
| project, | ||
| tags: [], | ||
| sourceObservationIds: [], | ||
| sourceMemoryIds: [], | ||
| result: "shipped", | ||
| }; | ||
| } | ||
|
|
||
| const SUMMARY_XML = [ | ||
| "<summary>", | ||
| "<goal>Quasar deploy pipeline</goal>", | ||
| "<outcome>Pipeline shipped</outcome>", | ||
| "<steps>Built image, pushed, rolled out</steps>", | ||
| "<discoveries>Registry needed auth</discoveries>", | ||
| "<lesson>Cache credentials early</lesson>", | ||
| "</summary>", | ||
| ].join("\n"); | ||
|
|
||
| function fixture( | ||
| summarize: (system: string, prompt: string) => Promise<string> = async () => SUMMARY_XML, | ||
| extraScopes: Record<string, Map<string, unknown>> = {}, | ||
| ) { | ||
| const store = new Map<string, Map<string, unknown>>([ | ||
| ["mem:actions", new Map([["act_1", doneAction("act_1")]])], | ||
| ...Object.entries(extraScopes), | ||
| ]); | ||
| const kv = { | ||
| get: async (scope: string, id: string) => store.get(scope)?.get(id) ?? null, | ||
| set: async (scope: string, id: string, value: unknown) => { | ||
| if (!store.has(scope)) store.set(scope, new Map()); | ||
| store.get(scope)!.set(id, value); | ||
| return value; | ||
| }, | ||
| list: async (scope: string) => [...(store.get(scope)?.values() ?? [])], | ||
| }; | ||
| let handler: (args: object) => Promise<{ success: boolean; memoryId?: string; compressed?: number }>; | ||
| const sdk = { registerFunction: (_: string, fn: typeof handler) => { handler = fn; } }; | ||
| const provider = { summarize: vi.fn(summarize) }; | ||
| registerFlowCompressFunction(sdk as unknown as IIIClient, kv as unknown as StateKV, provider as unknown as MemoryProvider); | ||
| const memory = (id: string) => store.get("mem:memories")?.get(id) as Memory | undefined; | ||
| return { call: (args: object) => handler(args), provider, memory }; | ||
| } | ||
|
|
||
| describe("flow-compressed memories", () => { | ||
| afterEach(() => getSearchIndex().clear()); | ||
|
|
||
| it("are searchable immediately after mem::flow-compress returns", async () => { | ||
| const f = fixture(); | ||
| const result = await f.call({ actionIds: ["act_1"] }); | ||
| expect(result.success).toBe(true); | ||
| expect(result.memoryId).toMatch(/^mem_/); | ||
| expect(f.provider.summarize).toHaveBeenCalledOnce(); | ||
|
|
||
| const idx = getSearchIndex(); | ||
| expect(idx.has(result.memoryId!)).toBe(true); | ||
| expect(idx.layerOf(result.memoryId!)).toBe("memory"); | ||
| expect( | ||
| idx.search("quasar deploy pipeline", 5, "memory").map((r) => r.obsId), | ||
| ).toContain(result.memoryId); | ||
| expect(f.memory(result.memoryId!)).toMatchObject({ | ||
| id: result.memoryId, | ||
| title: "Quasar deploy pipeline", | ||
| isLatest: true, | ||
| metadata: expect.objectContaining({ flowCompressed: true }), | ||
| }); | ||
| }); | ||
|
|
||
| it("stamps the agent so scoped recall can find it", async () => { | ||
| vi.stubEnv("AGENT_ID", "agent-x"); | ||
| try { | ||
| const f = fixture(); | ||
| const result = await f.call({ actionIds: ["act_1"] }); | ||
| expect(result.success).toBe(true); | ||
| expect(f.memory(result.memoryId!)!.agentId).toBe("agent-x"); | ||
| } finally { | ||
| vi.unstubAllEnvs(); | ||
| } | ||
| }); | ||
|
|
||
| it("stay searchable when the provider output has no summary tags", async () => { | ||
| const f = fixture( | ||
| async () => "quasar deploy failed where left < threshold && threshold > right on retry", | ||
| ); | ||
| const result = await f.call({ actionIds: ["act_1"] }); | ||
| expect(result.success).toBe(true); | ||
| expect(result.memoryId).toBeTruthy(); | ||
|
|
||
| expect(getSearchIndex().has(result.memoryId!)).toBe(true); | ||
| expect(f.memory(result.memoryId!)!.content).toContain("quasar"); | ||
| expect(f.memory(result.memoryId!)!.content).toContain("left < threshold && threshold > right"); | ||
| }); | ||
|
|
||
| it("keeps long fallback summaries searchable past the embedding input limit", async () => { | ||
| const response = "deployment notes ".repeat(1100) + "Quasar rollback checkpoint"; | ||
| const f = fixture(async () => response); | ||
| const result = await f.call({ actionIds: ["act_1"] }); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| const stored = f.memory(result.memoryId!)!; | ||
| expect(stored.title.length).toBeLessThanOrEqual(80); | ||
| expect(stored.content).toBe(response); | ||
| expect( | ||
| getSearchIndex().search("quasar rollback checkpoint", 5, "memory").map((r) => r.obsId), | ||
| ).toContain(result.memoryId); | ||
| }); | ||
|
|
||
| it("parses attributed summary tags instead of dropping them", async () => { | ||
| const f = fixture( | ||
| async () => | ||
| '<summary format="xml"><goal confidence="high">Quasar pipeline</goal>' + | ||
| '<steps count="2">built and pushed</steps></summary>', | ||
| ); | ||
| const result = await f.call({ actionIds: ["act_1"] }); | ||
| expect(result.success).toBe(true); | ||
|
|
||
| const stored = f.memory(result.memoryId!)!; | ||
| expect(stored.title).toBe("Quasar pipeline"); | ||
| expect(stored.content).toContain("built and pushed"); | ||
| expect(stored.content).not.toContain("<"); | ||
| expect(getSearchIndex().has(result.memoryId!)).toBe(true); | ||
| }); | ||
|
|
||
| it("returns failure and indexes nothing when summarization fails", async () => { | ||
| const f = fixture(async () => { throw new Error("provider down"); }); | ||
| const result = await f.call({ actionIds: ["act_1"] }); | ||
| expect(result.success).toBe(false); | ||
| expect(result.memoryId).toBeUndefined(); | ||
| expect(getSearchIndex().size).toBe(0); | ||
| }); | ||
|
|
||
| it("falls back to generic content when the provider returns nothing", async () => { | ||
| const f = fixture(async () => ""); | ||
| const result = await f.call({ actionIds: ["act_1"] }); | ||
| expect(result.success).toBe(true); | ||
| expect(result.memoryId).toBeTruthy(); | ||
| expect(getSearchIndex().has(result.memoryId!)).toBe(true); | ||
| expect(f.memory(result.memoryId!)!.content).toContain("completed actions"); | ||
| }); | ||
|
|
||
| it("stamps the project only when project selected the actions", async () => { | ||
| const byProject = fixture(); | ||
| const scoped = await byProject.call({ project: "proj-a" }); | ||
| expect(scoped.success).toBe(true); | ||
| expect(byProject.memory(scoped.memoryId!)!.project).toBe("proj-a"); | ||
|
|
||
| const byIds = fixture(); | ||
| const unscoped = await byIds.call({ actionIds: ["act_1"], project: "other" }); | ||
| expect(unscoped.success).toBe(true); | ||
| expect(byIds.memory(unscoped.memoryId!)!.project).toBeUndefined(); | ||
| expect(getSearchIndex().has(unscoped.memoryId!)).toBe(true); | ||
|
|
||
| const matching = fixture(); | ||
| const confirmed = await matching.call({ actionIds: ["act_1"], project: "proj-a" }); | ||
| expect(confirmed.success).toBe(true); | ||
| expect(matching.memory(confirmed.memoryId!)!.project).toBe("proj-a"); | ||
|
|
||
| const run = { | ||
| id: "run_1", routineId: "r", status: "completed", startedAt: "2026-11-20T00:00:00.000Z", | ||
| actionIds: ["act_1"], stepStatus: {}, initiatedBy: "agent-a", | ||
| }; | ||
| const byRun = fixture(undefined, { "mem:routine-runs": new Map([["run_1", run]]) }); | ||
| const fromRun = await byRun.call({ runId: "run_1", project: "other" }); | ||
| expect(fromRun.success).toBe(true); | ||
| expect(byRun.memory(fromRun.memoryId!)!.project).toBeUndefined(); | ||
|
|
||
| const inferred = fixture(undefined, { "mem:routine-runs": new Map([["run_1", run]]) }); | ||
| const fromRunNoScope = await inferred.call({ runId: "run_1" }); | ||
| expect(fromRunNoScope.success).toBe(true); | ||
| expect(inferred.memory(fromRunNoScope.memoryId!)!.project).toBe("proj-a"); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { readFileSync } from "node:fs"; | ||
| import { join } from "node:path"; | ||
| import { runInNewContext } from "node:vm"; | ||
|
|
||
| const script = readFileSync(new URL("../scripts/release-gate/graph-index.mjs", import.meta.url), "utf8"); | ||
| const start = script.indexOf("function graphFiles(p) {"); | ||
| const end = script.indexOf("\nfunction ordinaryReads(", start); | ||
|
|
||
| function graphFiles(files: string[], stat: (path: string) => { size: number }) { | ||
| expect(start).toBeGreaterThanOrEqual(0); | ||
| expect(end).toBeGreaterThan(start); | ||
| return runInNewContext(`(${script.slice(start, end)})`, { | ||
| join, | ||
| readdirSync: () => files, | ||
| statSync: stat, | ||
| })({ data: "/fixture" }); | ||
| } | ||
|
|
||
| describe("packaged graph gate file diagnostics", () => { | ||
| it("ignores a temporary graph file removed by an atomic rename", () => { | ||
| const stat = vi.fn((path: string) => { | ||
| if (path.endsWith(".tmp")) throw Object.assign(new Error("file renamed"), { code: "ENOENT" }); | ||
| return { size: 128 }; | ||
| }); | ||
| expect(graphFiles([ | ||
| "mem%3Agraph%3Aretrieval-data.bin", | ||
| "mem%3Agraph%3Aretrieval-data.bin.tmp", | ||
| "mem%3Amemories.bin", | ||
| ], stat)).toEqual([{ scope: "mem:graph:retrieval-data", bytes: 128 }]); | ||
| expect(stat).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it("still reports stat failures for committed graph files", () => { | ||
| const error = Object.assign(new Error("permission denied"), { code: "EACCES" }); | ||
| expect(() => graphFiles(["mem%3Agraph%3Aretrieval-data.bin"], () => { throw error; })).toThrow(error); | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.