wip(m4-003): memory/retrieval slice handoff from coder4

This commit is contained in:
Jarvis
2026-07-13 07:15:52 -05:00
parent 76325ca3f2
commit 5b99c82193
4 changed files with 166 additions and 0 deletions

View File

@@ -22,6 +22,13 @@ export type {
InsightSearchResult,
} from './types.js';
export { createMemoryAdapter, registerMemoryAdapter } from './factory.js';
export {
createOperatorMemoryPlugin,
type OperatorMemoryPlugin,
type OperatorMemoryScope,
type OperatorMemoryConfig,
type OperatorMemoryResult,
} from './operator-memory-plugin.js';
export { PgVectorAdapter } from './adapters/pgvector.js';
export { KeywordAdapter } from './adapters/keyword.js';

View File

@@ -0,0 +1,58 @@
import { describe, expect, it, vi } from 'vitest';
import { createOperatorMemoryPlugin } from './operator-memory-plugin.js';
function adapter() {
return {
name: 'test',
embedder: null,
storeInsight: vi.fn(async (value) => ({ ...value, id: '1', createdAt: new Date() })),
searchInsights: vi.fn(async () => []),
getInsight: vi.fn(),
deleteInsight: vi.fn(),
getPreference: vi.fn(),
setPreference: vi.fn(),
deletePreference: vi.fn(),
listPreferences: vi.fn(),
close: vi.fn(),
};
}
describe('OperatorMemoryPlugin', () => {
it('isolates adapter identity by server-derived tenant, owner, and namespace', async () => {
const memory = adapter();
const plugin = createOperatorMemoryPlugin({
adapter: memory,
instanceId: 'Nova',
redact: (v) => v,
});
await plugin.capture(
{ tenantId: 'tenant-a', ownerId: 'owner-a', sessionId: 's', namespace: 'private' },
{ content: 'one', source: 'test', category: 'note' },
);
await plugin.capture(
{ tenantId: 'tenant-b', ownerId: 'owner-a', sessionId: 's', namespace: 'private' },
{ content: 'two', source: 'test', category: 'note' },
);
expect(memory.storeInsight.mock.calls.map((call: unknown[]) => call[0].userId)).toEqual([
'tenant-a:owner-a:private',
'tenant-b:owner-a:private',
]);
});
it('uses a differently named configured instance in retrieval provenance', async () => {
const memory = adapter();
memory.searchInsights.mockResolvedValue([
{ id: '1', content: 'x', score: 1, metadata: { source: 'project' } },
]);
const plugin = createOperatorMemoryPlugin({
adapter: memory,
instanceId: 'Nova',
redact: (v) => v,
});
const results = await plugin.search(
{ tenantId: 't', ownerId: 'o', sessionId: 's', namespace: 'n' },
'x',
);
expect(results[0]?.provenance.instanceId).toBe('Nova');
});
});

View File

@@ -0,0 +1,84 @@
import type { Insight, InsightSearchResult, MemoryAdapter } from './types.js';
/** Immutable server-derived boundary; callers never choose an adapter namespace. */
export interface OperatorMemoryScope {
tenantId: string;
ownerId: string;
sessionId: string;
namespace: string;
}
export interface OperatorMemoryConfig {
adapter: MemoryAdapter;
instanceId: string;
maxStartupContext?: number;
redact(content: string): string;
}
export interface OperatorMemoryResult extends InsightSearchResult {
provenance: { instanceId: string; namespace: string; source: string };
}
export interface OperatorMemoryPlugin {
capture(
scope: OperatorMemoryScope,
input: { content: string; source: string; category: string },
): Promise<Insight>;
search(
scope: OperatorMemoryScope,
query: string,
limit?: number,
): Promise<OperatorMemoryResult[]>;
recent(scope: OperatorMemoryScope, limit?: number): Promise<OperatorMemoryResult[]>;
stats(scope: OperatorMemoryScope): Promise<{ namespace: string; resultCount: number }>;
startupContext(scope: OperatorMemoryScope): Promise<OperatorMemoryResult[]>;
}
function scopedUserId(scope: OperatorMemoryScope): string {
return `${scope.tenantId}:${scope.ownerId}:${scope.namespace}`;
}
/** Creates a leaf-package, replaceable memory adapter facade. */
export function createOperatorMemoryPlugin(config: OperatorMemoryConfig): OperatorMemoryPlugin {
const mapResult = (
scope: OperatorMemoryScope,
result: InsightSearchResult,
): OperatorMemoryResult => ({
...result,
provenance: {
instanceId: config.instanceId,
namespace: scope.namespace,
source: String(result.metadata?.['source'] ?? 'retrieval'),
},
});
const search = async (
scope: OperatorMemoryScope,
query: string,
limit = 10,
): Promise<OperatorMemoryResult[]> =>
(await config.adapter.searchInsights(scopedUserId(scope), query, { limit })).map((result) =>
mapResult(scope, result),
);
return {
async capture(scope, input) {
return config.adapter.storeInsight({
userId: scopedUserId(scope),
content: config.redact(input.content),
source: input.source,
category: input.category,
relevanceScore: 1,
metadata: { namespace: scope.namespace, instanceId: config.instanceId },
});
},
search,
async recent(scope, limit = 10) {
return search(scope, '*', limit);
},
async stats(scope) {
return { namespace: scope.namespace, resultCount: (await search(scope, '*', 100)).length };
},
async startupContext(scope) {
return search(scope, '*', config.maxStartupContext ?? 8);
},
};
}