diff --git a/v3/@claude-flow/memory/src/learning-bridge.test.ts b/v3/@claude-flow/memory/src/learning-bridge.test.ts index c14b770ef7..09d2939e0e 100644 --- a/v3/@claude-flow/memory/src/learning-bridge.test.ts +++ b/v3/@claude-flow/memory/src/learning-bridge.test.ts @@ -331,6 +331,24 @@ describe('LearningBridge', () => { }); }); + it('should resolve a stored key and update the entry by its internal id', async () => { + const entry = createTestEntry({ + id: 'internal-1', + key: 'insight:debugging:12345:0', + metadata: { confidence: 0.5 }, + }); + backend.storedEntries.push(entry); + + await bridge.onInsightAccessed(entry.key); + + expect(backend.getByKey).toHaveBeenCalledWith('learnings', entry.key); + expect(backend.get).not.toHaveBeenCalled(); + expect(backend.update).toHaveBeenCalledWith('internal-1', { + metadata: expect.objectContaining({ confidence: 0.53 }), + }); + expect(entry.metadata?.confidence).toBeCloseTo(0.53); + }); + it('should cap confidence at maxConfidence', async () => { const entry = createTestEntry({ metadata: { confidence: 0.99 } }); (backend.get as any).mockResolvedValueOnce(entry); diff --git a/v3/@claude-flow/memory/src/learning-bridge.ts b/v3/@claude-flow/memory/src/learning-bridge.ts index ec621eca3d..2c0ae28681 100644 --- a/v3/@claude-flow/memory/src/learning-bridge.ts +++ b/v3/@claude-flow/memory/src/learning-bridge.ts @@ -218,7 +218,7 @@ export class LearningBridge extends EventEmitter { async onInsightAccessed(entryId: string): Promise { if (!this.config.enabled || this.destroyed) return; - const entry = await this.backend.get(entryId); + const entry = await this.getInsightEntry(entryId); if (!entry) return; const currentConf = (entry.metadata?.confidence as number) ?? 0.5; @@ -227,7 +227,7 @@ export class LearningBridge extends EventEmitter { currentConf + this.config.accessBoostAmount, ); - await this.backend.update(entryId, { + await this.backend.update(entry.id, { metadata: { ...entry.metadata, confidence: newConf }, }); @@ -251,6 +251,21 @@ export class LearningBridge extends EventEmitter { this.emit('insight:accessed', { entryId, newConfidence: newConf }); } + /** + * Resolve an insight the caller addresses by the key it was stored under. + * `get()` only knows the backend's internal `entry.id`, and + * `createDefaultEntry` mints that id independently of the caller's key, so a + * key lookup must go through `getByKey` first; fall back to `get()` for + * callers that really pass an id. + */ + private async getInsightEntry(entryId: string): Promise { + if (typeof this.backend.getByKey === 'function') { + const byKey = await this.backend.getByKey(this.config.insightNamespace, entryId); + if (byKey) return byKey; + } + return this.backend.get(entryId); + } + /** * Consolidate active trajectories by completing them in the neural system. * Only runs when there are enough active trajectories to justify the cost.