Skip to content

Commit 3ebe33e

Browse files
committed
test(souls): pin the id of a non-Latin soul name, trace limit types, a cleared call prompt and a store outage
- soulAgentIdOf and personaFieldsOf give one id: a blank agentId reads as unset, a padded one is trimmed, a name with no Latin letters is hashed - reasoningTrace members of the wrong type are refused at load - generate(p, { system: undefined }) still closes with the hard limits - a learning store that fails its first open does not move old learners - the docs name the system messages a turn adds after the limits Signed-off-by: jddunn <[email protected]>
1 parent 8f013a1 commit 3ebe33e

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ vi.mock('../../core/llm/providers/implementations/OpenAIProvider', async () => (
99
import * as fs from 'node:fs';
1010
import * as os from 'node:os';
1111
import * as path from 'node:path';
12+
import { fileURLToPath } from 'node:url';
1213
import pino from 'pino';
1314
import { z } from 'zod';
1415
import { agent, type Agent, type AgentOptions } from '../agent';
@@ -23,6 +24,7 @@ import { reply, script, type ProviderScript } from './helpers/stubProviders';
2324
import { globalLLMProviderHealth } from '../../core/safety/LLMProviderHealthRegistry';
2425
import { Brain } from '../../cognition/memory/retrieval/store/Brain';
2526
import { InMemoryLearningStore } from '../../cognition/learning/store/InMemoryLearningStore';
27+
import { stableIdOfName } from '../../cognition/substrate/personas/stableId';
2628

2729
let n = 0;
2830
const key = () => `k-soul-identity-${++n}`;
@@ -215,6 +217,14 @@ describe("a soul's hard limits, traits and name on the default runtime", () => {
215217
expect(systemOf(s.seen[1])).toBe(`Answer in one word.\n\n${BLOCK}`);
216218
});
217219

220+
it('closes a call whose own system prompt is set to undefined with the limits', async () => {
221+
writeSoul(FULL);
222+
const k = key();
223+
const s = script('openai', k, { whole: true, replies: [reply.text('A.')] });
224+
await agent(stubbed(k, { soul: dir })).generate('Hi.', { system: undefined } as never);
225+
expect(systemTexts(s.seen[0]).join('\n\n').endsWith(BLOCK)).toBe(true);
226+
});
227+
218228
it('adds no block for a soul with no hard limits', async () => {
219229
writeSoul('name: Aria\nagentId: aria-1');
220230
const k = key();
@@ -320,6 +330,22 @@ describe("the soul's id as the agent's key", () => {
320330
}
321331
});
322332

333+
it.each([['an agent with no name', {}], ["an agent named 'Support'", { name: 'Support' }]] as const)(
334+
'files under a stable id of a soul name with no Latin letters, on %s',
335+
async (_name, extra) => {
336+
writeSoul('name: アリア');
337+
const k = key();
338+
script('openai', k, { whole: true, replies: [reply.text('Lisbon it is.'), reply.text('Notes.')] });
339+
const a = await souledAgent({ ...stubbed(k, extra), soul: dir } as never);
340+
try {
341+
await a.session('s').send('I moved to Lisbon.');
342+
await vi.waitFor(async () => expect((await a.memory!.recall('Lisbon', { scope: 'persona', scopeId: stableIdOfName('アリア') })).length).toBe(2));
343+
} finally {
344+
await a.close();
345+
}
346+
},
347+
);
348+
323349
it('souledAgent() rejects a soul that gives no id on an agent with no name, and makes no store', async () => {
324350
writeSoul('hardLimits:\n - Never guess.', 'You are terse.');
325351
await expect(souledAgent({ ...stubbed(key()), soul: dir } as never)).rejects.toThrow(
@@ -383,6 +409,27 @@ describe('learners filed under the slug of the agent name before the soul had an
383409
await after.close();
384410
});
385411

412+
it('stay under it when the store fails its first open: the key is asked again once the store answers', async () => {
413+
vi.spyOn(console, 'log').mockImplementation(() => undefined);
414+
vi.spyOn(console, 'warn').mockImplementation(() => undefined);
415+
const store = new InMemoryLearningStore();
416+
const k = key();
417+
script('openai', k, { replies: [reply.text('One.'), reply.text('Two.'), reply.text('Three.')] });
418+
const before = agent(stubbed(k, learning(store)));
419+
await before.session('s1', { userId: 'dee' }).send('Hello.');
420+
await before.close();
421+
expect(await keysOf(store, 'dee')).toEqual(new Set(['aria']));
422+
423+
writeSoul(FULL);
424+
vi.spyOn(store, 'freezeRows').mockRejectedValueOnce(new Error('store unavailable'));
425+
const after = agent(stubbed(k, { ...learning(store), soul: dir }));
426+
await after.session('s2', { userId: 'dee' }).send('Hello again.');
427+
await after.session('s3', { userId: 'dee' }).send('And again.');
428+
await after.close();
429+
expect(events('soul.id_fallback')).toEqual([expect.objectContaining({ reason: 'old_slug_learner' })]);
430+
expect(await keysOf(store, 'dee')).toEqual(new Set(['aria']));
431+
});
432+
386433
it('are left where they are, with no line, when learning.learnerKey names the key', async () => {
387434
vi.spyOn(console, 'log').mockImplementation(() => undefined);
388435
const store = new InMemoryLearningStore();
@@ -413,3 +460,15 @@ describe('learners filed under the slug of the agent name before the soul had an
413460
expect(await keysOf(store, 'cy')).toEqual(new Set(['aria-1']));
414461
});
415462
});
463+
464+
describe('what the docs say about the limits closing the prompt', () => {
465+
const PACKAGE_ROOT = fileURLToPath(new URL('../../../', import.meta.url));
466+
const doc = (file: string) => fs.readFileSync(path.join(PACKAGE_ROOT, file), 'utf8');
467+
468+
it('name the system messages a turn can add after the limits', () => {
469+
const exception = "on a turn where a memory provider's `getContext` returns text (the wiki's recall on a souled agent), that text is a system message after the limits, and so is the plan of a call with `planning` on";
470+
expect(doc('docs/SOUL_FILES.md')).toContain(exception);
471+
expect(doc('docs/safety/GUARDRAILS_USAGE.md')).toContain(exception);
472+
expect(doc('README.md')).not.toContain('Identity, voice, hard limits, and HEXACO scores');
473+
});
474+
});

‎src/cognition/substrate/personas/__tests__/SoulLoader.identity.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ describe('personaFieldsOf', () => {
3434
it('keeps the id of a soul with a name and no agentId as the slug of the name', () => {
3535
expect(personaFieldsOf({ name: 'Aria Stone' }).id).toBe('aria-stone');
3636
});
37+
it('gives the id soulAgentIdOf gives: a blank agentId reads as unset, a padded one is trimmed, a name with no Latin letters is hashed', () => {
38+
expect(personaFieldsOf({ agentId: '', name: 'Aria' }).id).toBe('aria');
39+
expect(personaFieldsOf({ agentId: ' aria-1 ', name: 'Aria' }).id).toBe('aria-1');
40+
expect(personaFieldsOf({ name: 'アリア' }).id).toBe(stableIdOfName('アリア'));
41+
expect(personaFieldsOf({}).id).toBe('unnamed-agent');
42+
});
3743
});
3844

3945
describe('soulAgentIdOf', () => {
@@ -47,6 +53,18 @@ describe('soulAgentIdOf', () => {
4753
expect(soulAgentIdOf({}, 'Support Desk')).toBe('support-desk');
4854
expect(soulAgentIdOf({}, 'Zoë')).toBe(stableIdOfName('Zoë'));
4955
});
56+
it('trims a padded agentId', () => {
57+
expect(soulAgentIdOf({ agentId: ' aria-1 ', name: 'Aria' })).toBe('aria-1');
58+
});
59+
it('gives a soul name with no Latin letters or digits a stable id of its own, whatever the agent is named', () => {
60+
const id = stableIdOfName('アリア');
61+
expect(id).toMatch(/^agent-[0-9a-f]{12}$/);
62+
expect(soulAgentIdOf({ name: 'アリア' })).toBe(id);
63+
expect(soulAgentIdOf({ name: 'アリア' }, 'Support')).toBe(id);
64+
});
65+
it('keeps the slug a Latin soul name had, so an existing id does not move', () => {
66+
expect(soulAgentIdOf({ name: 'Zoë' })).toBe('zo');
67+
});
5068
it('is undefined when the soul has neither and the agent has no name, and for blank values', () => {
5169
expect(soulAgentIdOf({})).toBeUndefined();
5270
expect(soulAgentIdOf({ agentId: ' ', name: ' ' }, ' ')).toBeUndefined();
@@ -85,6 +103,14 @@ describe('validateSoulFrontmatter', () => {
85103
expect(thrown).toMatchObject({ name: 'SoulLoadError', code: 'SOUL_LOAD_FAILED', source: '/souls/aria' });
86104
expect((thrown as Error).message).toBe(`The soul failed to load (/souls/aria): front matter field '${field}' must be ${expected}`);
87105
});
106+
it.each([
107+
['maxEntries', 'many'],
108+
['maxMessageLength', '300'],
109+
])('refuses reasoningTrace.%s that is not a number, naming the field', (member, value) => {
110+
expect(() => validateSoulFrontmatter({ reasoningTrace: { [member]: value } }, '/souls/aria')).toThrow(
111+
new SoulLoadError('/souls/aria', `front matter field 'reasoningTrace.${member}' must be a number`),
112+
);
113+
});
88114
it('is what parseSoul runs, with the inline source', () => {
89115
expect(() => parseSoul("---\nhardLimits: never\n---\nYou are Aria.")).toThrow("The soul failed to load (inline soul): front matter field 'hardLimits' must be a list of strings");
90116
});

0 commit comments

Comments
 (0)