refactor(memory): run agent recall, memory tools and the server on the memory contract (P1.5)
- Orchestrator, memory tools, context loader and pattern write-back use a MemoryStore and a workspace scope instead of MindDB layers; the knowledge graph tools, cognify, memory linker and feedback handler are gone (graph hidden in v1, ADR-0002). - The server opens one mem0 store in <dataDir>/memory/, embedded by the in-process model on first use, and extracts facts about the user in the background with the model the user chose (memoryExtraction setting). - Legacy .mind memories are copied into the store once, in the background; the legacy files are only read. - Weaver consolidation, harvest, wiki, memory import, memory lanes, vector backfill and index reconciliation are removed; agent runs, loops, sub-agent results, connector fetch and the failed-turn capture write through the contract. Workspace sessions no longer hold a mind handle. - FileIndexer and the install audit move to waggle.db; recall date helpers and the reranker are copied into core. - The CLI REPL opens the same memory store.
This commit is contained in:
83
packages/core/tests/recall/inprocess-reranker.test.ts
Normal file
83
packages/core/tests/recall/inprocess-reranker.test.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const transformers = vi.hoisted(() => ({
|
||||
env: { allowRemoteModels: false, cacheDir: '' },
|
||||
model: vi.fn(),
|
||||
modelFromPretrained: vi.fn(),
|
||||
tokenizer: vi.fn(),
|
||||
tokenizerFromPretrained: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@huggingface/transformers', () => ({
|
||||
env: transformers.env,
|
||||
AutoModelForSequenceClassification: {
|
||||
from_pretrained: transformers.modelFromPretrained,
|
||||
},
|
||||
AutoTokenizer: {
|
||||
from_pretrained: transformers.tokenizerFromPretrained,
|
||||
},
|
||||
}));
|
||||
|
||||
import { createInProcessReranker } from '../../src/recall/inprocess-reranker.js';
|
||||
|
||||
const tempRoots: string[] = [];
|
||||
|
||||
describe('createInProcessReranker', () => {
|
||||
beforeEach(() => {
|
||||
transformers.env.allowRemoteModels = false;
|
||||
transformers.env.cacheDir = '';
|
||||
transformers.model.mockReset();
|
||||
transformers.modelFromPretrained.mockReset();
|
||||
transformers.tokenizer.mockReset();
|
||||
transformers.tokenizerFromPretrained.mockReset();
|
||||
transformers.modelFromPretrained.mockResolvedValue(transformers.model);
|
||||
transformers.tokenizerFromPretrained.mockResolvedValue(transformers.tokenizer);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
for (const root of tempRoots.splice(0)) {
|
||||
fs.rmSync(root, { force: true, recursive: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('requests tensors and supports single and batch scoring', async () => {
|
||||
transformers.tokenizer.mockResolvedValue({ input_ids: 'tokens' });
|
||||
transformers.model
|
||||
.mockResolvedValueOnce({ logits: { data: new Float32Array([0.75]), dims: [1, 1] } })
|
||||
.mockResolvedValueOnce({ logits: { data: new Float32Array([0.25, 0.5]), dims: [2, 1] } });
|
||||
const cacheDir = fs.mkdtempSync(path.join(os.tmpdir(), 'reranker-test-'));
|
||||
tempRoots.push(cacheDir);
|
||||
|
||||
const reranker = await createInProcessReranker({ cacheDir });
|
||||
await expect(reranker.score('query', 'document')).resolves.toBeCloseTo(0.75);
|
||||
await expect(reranker.scoreBatch('query', ['first', 'second'])).resolves.toEqual([
|
||||
0.25,
|
||||
0.5,
|
||||
]);
|
||||
|
||||
const canonicalCacheDir = fs.realpathSync.native(cacheDir);
|
||||
expect(transformers.tokenizerFromPretrained).toHaveBeenCalledWith(
|
||||
'Xenova/ms-marco-MiniLM-L-6-v2',
|
||||
{ cache_dir: canonicalCacheDir },
|
||||
);
|
||||
expect(transformers.modelFromPretrained).toHaveBeenCalledWith(
|
||||
'Xenova/ms-marco-MiniLM-L-6-v2',
|
||||
{ dtype: 'fp32', cache_dir: canonicalCacheDir },
|
||||
);
|
||||
expect(transformers.tokenizer).toHaveBeenNthCalledWith(1, 'query', {
|
||||
text_pair: 'document',
|
||||
padding: true,
|
||||
truncation: true,
|
||||
return_tensor: true,
|
||||
});
|
||||
expect(transformers.tokenizer).toHaveBeenNthCalledWith(2, ['query', 'query'], {
|
||||
text_pair: ['first', 'second'],
|
||||
padding: true,
|
||||
truncation: true,
|
||||
return_tensor: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
63
packages/core/tests/recall/parse-date-window.test.ts
Normal file
63
packages/core/tests/recall/parse-date-window.test.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { parseDateWindow } from '../../src/recall/parse-date-window.js';
|
||||
|
||||
/**
|
||||
* W4.1b — query-side date-window parser (production port of the Wave-3.1
|
||||
* benchmark parser, validated on the full N=1540 LoCoMo run). Shapes below
|
||||
* mirror the mined failure cases the benchmark lane was built against.
|
||||
*/
|
||||
|
||||
describe('parseDateWindow', () => {
|
||||
it('parses "last week of <month> <year>"', () => {
|
||||
const w = parseDateWindow('Where was Calvin in the last week of October 2023?');
|
||||
expect(w).toEqual({ since: '2023-10-25', until: '2023-10-31', label: 'the last week of october 2023' });
|
||||
});
|
||||
|
||||
it('parses "first week of <month> <year>"', () => {
|
||||
const w = parseDateWindow('events in the first week of May 2023');
|
||||
expect(w).toEqual({ since: '2023-05-01', until: '2023-05-07', label: 'the first week of may 2023' });
|
||||
});
|
||||
|
||||
it('parses "early <month> <year>"', () => {
|
||||
const w = parseDateWindow('What happened in early June 2023?');
|
||||
expect(w).toEqual({ since: '2023-06-01', until: '2023-06-10', label: 'early june 2023' });
|
||||
});
|
||||
|
||||
it('parses "<day> <month> <year>" with a ±2-day buffer', () => {
|
||||
const w = parseDateWindow('What painting did Melanie show on 13 October 2023?');
|
||||
expect(w).toEqual({ since: '2023-10-11', until: '2023-10-15', label: '13 october 2023' });
|
||||
});
|
||||
|
||||
it('parses "<month> <day>, <year>" (US order) with a ±2-day buffer', () => {
|
||||
const w = parseDateWindow('What did Mel paint on October 13, 2023?');
|
||||
expect(w).toEqual({ since: '2023-10-11', until: '2023-10-15', label: '13 october 2023' });
|
||||
});
|
||||
|
||||
it('parses bare "<month> <year>" as the whole month', () => {
|
||||
const w = parseDateWindow('their latest project in July 2023');
|
||||
expect(w).toEqual({ since: '2023-07-01', until: '2023-07-31', label: 'july 2023' });
|
||||
});
|
||||
|
||||
it('handles February month-length correctly', () => {
|
||||
expect(parseDateWindow('in February 2024')?.until).toBe('2024-02-29'); // leap
|
||||
expect(parseDateWindow('in February 2023')?.until).toBe('2023-02-28');
|
||||
});
|
||||
|
||||
it('parses "in <year>" as the whole year (requires in/during)', () => {
|
||||
const w = parseDateWindow('What did we decide in 2022?');
|
||||
expect(w).toEqual({ since: '2022-01-01', until: '2022-12-31', label: '2022' });
|
||||
});
|
||||
|
||||
it('does NOT window a bare year without in/during (ids, names)', () => {
|
||||
expect(parseDateWindow('open ticket 2024 about the login flow')).toBeNull();
|
||||
});
|
||||
|
||||
it('does NOT window relative phrases (write-side concern)', () => {
|
||||
expect(parseDateWindow('what did we decide last week?')).toBeNull();
|
||||
expect(parseDateWindow('two months ago we shipped something')).toBeNull();
|
||||
});
|
||||
|
||||
it('returns null for queries with no temporal constraint', () => {
|
||||
expect(parseDateWindow('favorite painting colors')).toBeNull();
|
||||
});
|
||||
});
|
||||
76
packages/core/tests/recall/resolve-relative-date.test.ts
Normal file
76
packages/core/tests/recall/resolve-relative-date.test.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { resolveRelativeDate } from '../../src/recall/resolve-relative-date.js';
|
||||
|
||||
describe('resolveRelativeDate', () => {
|
||||
// Reference: 2023-05-08 is a Monday.
|
||||
const REF = '2023-05-08';
|
||||
|
||||
it('resolves the canonical benchmark case (conv-26 q000)', () => {
|
||||
// "I went to a LGBTQ support group yesterday" said on 2023-05-08 → event 2023-05-07.
|
||||
const r = resolveRelativeDate('I went to a LGBTQ support group yesterday and it was powerful.', REF);
|
||||
expect(r).toEqual({ cue: 'yesterday', iso: '2023-05-07' });
|
||||
});
|
||||
|
||||
it('resolves "the day before yesterday" before "yesterday"', () => {
|
||||
const r = resolveRelativeDate('We met the day before yesterday.', REF);
|
||||
expect(r).toEqual({ cue: 'the day before yesterday', iso: '2023-05-06' });
|
||||
});
|
||||
|
||||
it('resolves "N days ago"', () => {
|
||||
expect(resolveRelativeDate('finished it 3 days ago', REF)).toEqual({ cue: '3 days ago', iso: '2023-05-05' });
|
||||
});
|
||||
|
||||
it('resolves "last week" as −7 days', () => {
|
||||
expect(resolveRelativeDate('ran a race last week', REF)).toEqual({ cue: 'last week', iso: '2023-05-01' });
|
||||
});
|
||||
|
||||
it('resolves "N weeks ago"', () => {
|
||||
expect(resolveRelativeDate('started 2 weeks ago', REF)).toEqual({ cue: '2 weeks ago', iso: '2023-04-24' });
|
||||
});
|
||||
|
||||
it('resolves "last month" with month arithmetic', () => {
|
||||
expect(resolveRelativeDate('moved house last month', REF)).toEqual({ cue: 'last month', iso: '2023-04-08' });
|
||||
});
|
||||
|
||||
it('resolves "N months ago"', () => {
|
||||
expect(resolveRelativeDate('quit 2 months ago', REF)).toEqual({ cue: '2 months ago', iso: '2023-03-08' });
|
||||
});
|
||||
|
||||
it('resolves "last year" (Memori worked example: 4 May 2022 + "last year" → 2021)', () => {
|
||||
expect(resolveRelativeDate('we went to India last year', '2022-05-04')).toEqual({ cue: 'last year', iso: '2021-05-04' });
|
||||
});
|
||||
|
||||
it('resolves "last <weekday>" to the most recent prior occurrence', () => {
|
||||
// REF 2023-05-08 is Monday; "last Friday" = 2023-05-05.
|
||||
expect(resolveRelativeDate('went to a meeting last Friday', REF)).toEqual({ cue: 'last friday', iso: '2023-05-05' });
|
||||
// "last Monday" from a Monday → the previous Monday (strictly before), 2023-05-01.
|
||||
expect(resolveRelativeDate('it happened last Monday', REF)).toEqual({ cue: 'last monday', iso: '2023-05-01' });
|
||||
});
|
||||
|
||||
it('handles month-end clamp (Mar 31 − 1 month → Feb 28, not Mar 3)', () => {
|
||||
expect(resolveRelativeDate('it was last month', '2023-03-31')).toEqual({ cue: 'last month', iso: '2023-02-28' });
|
||||
});
|
||||
|
||||
it('accepts a datetime reference, not just a date', () => {
|
||||
expect(resolveRelativeDate('yesterday', '2023-05-08T13:56:00Z')).toEqual({ cue: 'yesterday', iso: '2023-05-07' });
|
||||
});
|
||||
|
||||
it('returns null when there is no relative cue', () => {
|
||||
expect(resolveRelativeDate('Caroline values self-acceptance.', REF)).toBeNull();
|
||||
expect(resolveRelativeDate('I am playing Cyberpunk 2077 right now', REF)).toBeNull();
|
||||
});
|
||||
|
||||
it('returns null on an unusable reference date', () => {
|
||||
expect(resolveRelativeDate('yesterday', null)).toBeNull();
|
||||
expect(resolveRelativeDate('yesterday', 'not-a-date')).toBeNull();
|
||||
expect(resolveRelativeDate('yesterday', undefined)).toBeNull();
|
||||
});
|
||||
|
||||
it('returns null on empty text', () => {
|
||||
expect(resolveRelativeDate('', REF)).toBeNull();
|
||||
});
|
||||
|
||||
it('does not match relative cues embedded in unrelated words', () => {
|
||||
expect(resolveRelativeDate('he messaged me', REF)).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user