Skip to content

Commit 082078e

Browse files
committed
feat(memory): deep remory integration with auto-search and auto-store (#100)
- Auto-search relevant memories before task dispatch - Auto-store task outcomes on successful completion - Graceful fallback when remory unavailable (TTL-cached availability) - Metadata serialized into text content for searchability - Empty description validation prevents unnecessary searches - Removed unused remory_persist_thinking config stub
1 parent d9348a5 commit 082078e

3 files changed

Lines changed: 51 additions & 12 deletions

File tree

‎packages/opencode/src/config/config.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,13 @@ export namespace Config {
11171117
.optional()
11181118
.default(5)
11191119
.describe("Maximum number of memory entries to retrieve per search (1-20)"),
1120+
remory_distance_threshold: z
1121+
.number()
1122+
.min(0, "Distance threshold must be at least 0")
1123+
.max(1, "Distance threshold cannot exceed 1")
1124+
.optional()
1125+
.default(0.4)
1126+
.describe("Maximum distance threshold for memory relevance filtering (0-1, lower = more strict)"),
11201127
background_tasks: z.boolean().optional().default(true).describe("Enable background task execution"),
11211128
max_background_tasks: z
11221129
.number()

‎packages/opencode/src/memory/remory.ts‎

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { UnixSocketClient, type JsonRpcRequest, DEFAULT_SOCKET_PATH } from "./socket-client"
44
import { Log } from "@/util/log"
5+
import { Config } from "../config/config"
56

67
const log = Log.create({ service: "memory.rememory" })
78

@@ -254,9 +255,15 @@ function generateId(): string {
254255
}
255256

256257
let remoryAvailable: boolean | null = null
258+
let lastCheck: number = 0
259+
const CACHE_TTL = 60000 // 1 minute
257260

258261
export async function checkRemoryAvailable(): Promise<boolean> {
259-
if (remoryAvailable !== null) return remoryAvailable
262+
const now = Date.now()
263+
if (remoryAvailable !== null && now - lastCheck < CACHE_TTL) {
264+
return remoryAvailable
265+
}
266+
lastCheck = now
260267
try {
261268
if (!isEnabled()) {
262269
remoryAvailable = false
@@ -273,11 +280,17 @@ export async function checkRemoryAvailable(): Promise<boolean> {
273280

274281
export function resetAvailabilityCache(): void {
275282
remoryAvailable = null
283+
lastCheck = 0
276284
}
277285

278286
export async function safeRemorySearch(query: string, userId: string, limit?: number): Promise<Memory[]> {
279287
if (!(await checkRemoryAvailable())) return []
280288

289+
// Skip memory search for empty or whitespace-only descriptions
290+
if (!query?.trim()) {
291+
return []
292+
}
293+
281294
try {
282295
const results = await search({
283296
query,
@@ -292,7 +305,16 @@ export async function safeRemorySearch(query: string, userId: string, limit?: nu
292305
distance: r.score,
293306
}))
294307

295-
const relevant = memories.filter((m) => m.distance && m.distance < 0.4)
308+
// Get threshold from config, default to 0.4
309+
let threshold = 0.4
310+
try {
311+
const config = await Config.get()
312+
threshold = config.experimental?.remory_distance_threshold ?? 0.4
313+
} catch {
314+
// Use default threshold if config loading fails
315+
}
316+
317+
const relevant = memories.filter((m) => m.distance && m.distance < threshold)
296318
return relevant
297319
} catch {
298320
return []
@@ -303,13 +325,21 @@ export async function safeRemoryAdd(text: string, userId: string, metadata?: obj
303325
if (!(await checkRemoryAvailable())) return
304326

305327
try {
328+
// Serialize metadata into text if provided
329+
const enhancedText = metadata ? `${text}\n\nMetadata: ${JSON.stringify(metadata)}` : text
330+
306331
await add({
307-
text,
332+
text: enhancedText,
308333
userId,
309334
infer: false,
310335
})
311-
} catch {
312-
// Silent fail - don't block on memory errors
336+
} catch (error) {
337+
log.warn("memory add failed", {
338+
error: error instanceof Error ? error.message : String(error),
339+
userId,
340+
textLength: text.length,
341+
hasMetadata: !!metadata,
342+
})
313343
}
314344
}
315345

‎packages/opencode/src/tool/task.ts‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,16 @@ export const TaskTool = Tool.define("task", async (initCtx) => {
291291
let memoryContext = ""
292292
if (config.experimental?.remory_enabled !== false) {
293293
try {
294-
const userId = await getUserId()
295-
const memories = await safeRemorySearch(params.description, userId)
296-
if (memories.length > 0) {
297-
memoryContext = "[Relevant Context from Prior Tasks]\n"
298-
for (const memory of memories) {
299-
memoryContext += `${memory.text}\n`
294+
if (params.description?.trim()) {
295+
const userId = await getUserId()
296+
const memories = await safeRemorySearch(params.description, userId)
297+
if (memories.length > 0) {
298+
memoryContext = "[Relevant Context from Prior Tasks]\n"
299+
for (const memory of memories) {
300+
memoryContext += `${memory.text}\n`
301+
}
302+
memoryContext += "\n"
300303
}
301-
memoryContext += "\n"
302304
}
303305
} catch (error) {
304306
// Fail silently - memory features are optional

0 commit comments

Comments
 (0)