Skip to content

Commit b7f05ea

Browse files
committed
test(souls): pin the persona fields, the soul id rule and the front matter check
Signed-off-by: jddunn <[email protected]>
1 parent a10e8a9 commit b7f05ea

2 files changed

Lines changed: 97 additions & 6 deletions

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* A soul's one mapping and its one id: the persona fields the front matter sets, the id every path keys the agent by,
3+
* and the check that refuses a front matter field of the wrong type.
4+
*/
5+
import { describe, expect, it } from 'vitest';
6+
import { frontmatterToPersona, parseSoul, personaFieldsOf, soulAgentIdOf, SoulLoadError, validateSoulFrontmatter } from '../SoulLoader.js';
7+
import { stableIdOfName } from '../stableId.js';
8+
9+
describe('personaFieldsOf', () => {
10+
const frontmatter = {
11+
name: 'Aria', agentId: 'aria-1', role: 'Support',
12+
hexaco: { honestyHumility: 0.9, openness: 0.2 },
13+
defaultMood: 'calm', allowedMoods: ['calm', 'warm'],
14+
hardLimits: ['Never name a diagnosis.'],
15+
reasoningTrace: { maxEntries: 7 },
16+
metadata: { team: 'care' },
17+
};
18+
19+
it('sets the fields the mapping sets, and frontmatterToPersona adds only the version and the prompt', () => {
20+
const fields = personaFieldsOf(frontmatter);
21+
expect(fields).toMatchObject({
22+
id: 'aria-1', name: 'Aria', description: 'Support',
23+
personalityTraits: { honesty: 0.9, openness: 0.2 },
24+
moodAdaptation: { enabled: true, defaultMood: 'calm', allowedMoods: ['calm', 'warm'] },
25+
hardLimits: ['Never name a diagnosis.'],
26+
reasoningTraceConfig: { maxEntries: 7 },
27+
metadata: { team: 'care' },
28+
});
29+
expect(frontmatterToPersona(frontmatter, 'You are Aria.', 'Short sentences.')).toEqual({
30+
...fields, version: '1.0.0', baseSystemPrompt: 'You are Aria.\n\n## Style\n\nShort sentences.',
31+
});
32+
});
33+
34+
it('keeps the id of a soul with a name and no agentId as the slug of the name', () => {
35+
expect(personaFieldsOf({ name: 'Aria Stone' }).id).toBe('aria-stone');
36+
});
37+
});
38+
39+
describe('soulAgentIdOf', () => {
40+
it('takes agentId first, as given', () => {
41+
expect(soulAgentIdOf({ agentId: 'aria-1', name: 'Aria' }, 'Support')).toBe('aria-1');
42+
});
43+
it('takes the slug of the soul name when there is no agentId, whatever the agent is named', () => {
44+
expect(soulAgentIdOf({ name: 'Aria Stone' }, 'Support')).toBe('aria-stone');
45+
});
46+
it("takes the agent's name when the soul has neither, by the rule a named agent's id follows", () => {
47+
expect(soulAgentIdOf({}, 'Support Desk')).toBe('support-desk');
48+
expect(soulAgentIdOf({}, 'Zoë')).toBe(stableIdOfName('Zoë'));
49+
});
50+
it('is undefined when the soul has neither and the agent has no name, and for blank values', () => {
51+
expect(soulAgentIdOf({})).toBeUndefined();
52+
expect(soulAgentIdOf({ agentId: ' ', name: ' ' }, ' ')).toBeUndefined();
53+
});
54+
it('never answers a shared fallback id', () => {
55+
for (const id of [soulAgentIdOf({}), soulAgentIdOf({}, undefined)]) expect(id).not.toBe('agent');
56+
});
57+
});
58+
59+
describe('validateSoulFrontmatter', () => {
60+
it('returns valid front matter, keeps unknown keys and drops a declared field set to null', () => {
61+
expect(validateSoulFrontmatter({ name: 'Aria', hardLimits: ['a'], role: null, custom: 1 })).toEqual({ name: 'Aria', hardLimits: ['a'], custom: 1 });
62+
});
63+
it.each([
64+
['name', 42, 'a string'],
65+
['agentId', 7, 'a string'],
66+
['role', ['x'], 'a string'],
67+
['defaultMood', {}, 'a string'],
68+
['allowedMoods', 'calm', 'a list of strings'],
69+
['hardLimits', 'never', 'a list of strings'],
70+
['hardLimits', ['ok', 3], 'a list of strings'],
71+
['voice', 'rachel', 'an object'],
72+
['avatar', [], 'an object'],
73+
['reasoningTrace', 42, 'an object'],
74+
['metadata', 'x', 'an object'],
75+
['hexaco', { openness: 'high' }, 'an object of numbers between 0 and 1'],
76+
['hexaco', { openness: 1.5 }, 'an object of numbers between 0 and 1'],
77+
])('refuses %s of the wrong type, naming the field', (field, value, expected) => {
78+
let thrown: unknown;
79+
try {
80+
validateSoulFrontmatter({ [field]: value }, '/souls/aria');
81+
} catch (error) {
82+
thrown = error;
83+
}
84+
expect(thrown).toBeInstanceOf(SoulLoadError);
85+
expect(thrown).toMatchObject({ name: 'SoulLoadError', code: 'SOUL_LOAD_FAILED', source: '/souls/aria' });
86+
expect((thrown as Error).message).toBe(`The soul failed to load (/souls/aria): front matter field '${field}' must be ${expected}`);
87+
});
88+
it('is what parseSoul runs, with the inline source', () => {
89+
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");
90+
});
91+
});

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ describe('SoulLoader reasoning-trace limits', () => {
77
const soul = parseSoul('---\nreasoningTrace:\n maxEntries: 42\n maxMessageLength: 300\n---\nYou are Aria.');
88
expect(soul.personaDefinition.reasoningTraceConfig).toEqual({ maxEntries: 42, maxMessageLength: 300 });
99
});
10-
it('keeps a malformed reasoningTrace value for validation to report', () => {
11-
const soul = parseSoul('---\nreasoningTrace: 42\n---\nYou are Aria.');
12-
expect(soul.personaDefinition.reasoningTraceConfig as unknown).toBe(42);
10+
it('refuses a reasoningTrace that is not an object when the soul is loaded', () => {
11+
expect(() => parseSoul('---\nreasoningTrace: 42\n---\nYou are Aria.')).toThrow("front matter field 'reasoningTrace' must be an object");
1312
});
14-
it('renders a malformed value unchanged so validation still sees it', () => {
13+
it('renders a malformed value unchanged, and loading that text refuses it', () => {
1514
const persona = { id: 'aria', name: 'Aria', description: 'd', version: '1.0.0', baseSystemPrompt: 'You are Aria.', reasoningTraceConfig: 42 as unknown as IPersonaDefinition['reasoningTraceConfig'] };
16-
const back = parseSoul(renderSoulMarkdown(persona));
17-
expect(back.personaDefinition.reasoningTraceConfig as unknown).toBe(42);
15+
const rendered = renderSoulMarkdown(persona);
16+
expect(rendered).toContain('reasoningTrace: 42');
17+
expect(() => parseSoul(rendered)).toThrow("front matter field 'reasoningTrace' must be an object");
1818
});
1919
it('renders and parses a persona back with the same limits', () => {
2020
const persona: IPersonaDefinition = { id: 'aria', name: 'Aria', description: 'd', version: '1.0.0', baseSystemPrompt: 'You are Aria.', reasoningTraceConfig: { maxEntries: 7 } };

0 commit comments

Comments
 (0)