Skip to content

Commit e4b4f86

Browse files
committed
test(souls): pin the learner key of a gmi() agent whose soul gains an id
Signed-off-by: jddunn <[email protected]>
1 parent a2e4916 commit e4b4f86

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

‎src/api/__tests__/learning-store-and-scopes.e2e.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3276,6 +3276,19 @@ function learningStoreCases(name: string, open: () => Promise<OpenedLearningStor
32763276
expect(await store.sessionRow('A', 'tutor', 's-1', '42', 'visitor')).toBeUndefined();
32773277
});
32783278

3279+
it('answers whether an agent key has learner rows in a namespace', async () => {
3280+
const { store } = await fresh();
3281+
await store.putSessionRow(sessionOf({ sessionId: 's1', namespace: 'n1', agentKey: 'aria', userId: 'ana' }));
3282+
// A learner's chain head counts; the agent's own chain head (no user) does not.
3283+
await store.insertHead({ chainKey: 'u|n1|coach|7', namespace: 'n1', agentKey: 'coach', userId: '7', lastId: 'c-1', lastHash: 'hash-c-1', seq: 1, forgotten: false });
3284+
await store.insertHead({ chainKey: 'a|n1|ops', namespace: 'n1', agentKey: 'ops', userId: null, lastId: 'a-1', lastHash: 'hash-a-1', seq: 1, forgotten: false });
3285+
expect(await store.agentHasLearners!('n1', 'aria')).toBe(true);
3286+
expect(await store.agentHasLearners!('n1', 'coach')).toBe(true);
3287+
expect(await store.agentHasLearners!('n1', 'aria-1')).toBe(false);
3288+
expect(await store.agentHasLearners!('n2', 'aria')).toBe(false);
3289+
expect(await store.agentHasLearners!('n1', 'ops')).toBe(false);
3290+
});
3291+
32793292
it("forgets a user in one namespace: marks the user's heads, deletes every row of theirs and counts them, and keeps the tombstone and every other user's and tenant's rows", async () => {
32803293
const made = await fresh();
32813294
const { store } = made;

‎src/api/__tests__/soul-identity.gmi.e2e.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { resetLoggerFactory, setLogger } from '../../core/logging/loggerFactory'
2222
import { reply, script, type ProviderScript } from './helpers/stubProviders';
2323
import { globalLLMProviderHealth } from '../../core/safety/LLMProviderHealthRegistry';
2424
import { Brain } from '../../cognition/memory/retrieval/store/Brain';
25+
import { InMemoryLearningStore } from '../../cognition/learning/store/InMemoryLearningStore';
2526

2627
let n = 0;
2728
const key = () => `k-soul-identity-${++n}`;
@@ -354,3 +355,61 @@ describe("the soul's id as the agent's key", () => {
354355
expect(() => gmi(stubbed(key(), { memory: { store: { path: path.join(dir, 'y.sqlite') } }, learning: { mode: 'off' } }))).toThrow(/one agent to the learning layer\.$/);
355356
});
356357
});
358+
359+
describe('learners filed under the slug of the agent name before the soul had an id', () => {
360+
const learning = (store: InMemoryLearningStore, extra: Record<string, unknown> = {}) =>
361+
({ runtime: 'gmi', name: 'Aria', personality: { openness: 0.9 }, learning: { store, traits: true, ...extra } });
362+
/** The agent keys of one user's session rows. */
363+
const keysOf = async (store: InMemoryLearningStore, userId: string) => new Set((await store.sessionRowsOf('', userId)).map((row) => row.agentKey));
364+
365+
it('stay under it by one logged fallback, and forgetUser reaches them', async () => {
366+
vi.spyOn(console, 'log').mockImplementation(() => undefined);
367+
const store = new InMemoryLearningStore();
368+
const k = key();
369+
script('openai', k, { replies: [reply.text('One.'), reply.text('Two.'), reply.text('Three.')] });
370+
const before = agent(stubbed(k, learning(store)));
371+
await before.session('s1', { userId: 'ana' }).send('Hello.');
372+
await before.close();
373+
expect(await keysOf(store, 'ana')).toEqual(new Set(['aria']));
374+
375+
writeSoul(FULL);
376+
const after = agent(stubbed(k, { ...learning(store), soul: dir }));
377+
await after.session('s2', { userId: 'ana' }).send('Hello again.');
378+
await after.session('s3', { userId: 'ana' }).send('And again.');
379+
expect(events('soul.id_fallback')).toEqual([expect.objectContaining({ reason: 'old_slug_learner' })]);
380+
expect(await keysOf(store, 'ana')).toEqual(new Set(['aria']));
381+
await after.learning!.forgetUser('ana');
382+
expect(await store.sessionRowsOf('', 'ana')).toEqual([]);
383+
await after.close();
384+
});
385+
386+
it('are left where they are, with no line, when learning.learnerKey names the key', async () => {
387+
vi.spyOn(console, 'log').mockImplementation(() => undefined);
388+
const store = new InMemoryLearningStore();
389+
const k = key();
390+
script('openai', k, { replies: [reply.text('One.'), reply.text('Two.')] });
391+
const before = agent(stubbed(k, learning(store)));
392+
await before.session('s1', { userId: 'ben' }).send('Hello.');
393+
await before.close();
394+
395+
writeSoul(FULL);
396+
const chosen = agent(stubbed(k, { ...learning(store, { learnerKey: 'aria-1' }), soul: dir }));
397+
await chosen.session('s2', { userId: 'ben' }).send('Hello again.');
398+
await chosen.close();
399+
expect(events('soul.id_fallback')).toEqual([]);
400+
expect(await keysOf(store, 'ben')).toEqual(new Set(['aria', 'aria-1']));
401+
});
402+
403+
it('files a new agent under the soul id with no line', async () => {
404+
vi.spyOn(console, 'log').mockImplementation(() => undefined);
405+
writeSoul(FULL);
406+
const store = new InMemoryLearningStore();
407+
const k = key();
408+
script('openai', k, { replies: [reply.text('One.')] });
409+
const fresh = agent(stubbed(k, { ...learning(store), soul: dir }));
410+
await fresh.session('s1', { userId: 'cy' }).send('Hello.');
411+
await fresh.close();
412+
expect(events('soul.id_fallback')).toEqual([]);
413+
expect(await keysOf(store, 'cy')).toEqual(new Set(['aria-1']));
414+
});
415+
});

0 commit comments

Comments
 (0)