Skip to content

Commit 0ec580c

Browse files
committed
test(souls): pin the persona gmi() builds from a soul
Signed-off-by: jddunn <[email protected]>
1 parent 25655a1 commit 0ec580c

2 files changed

Lines changed: 120 additions & 2 deletions

File tree

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,68 @@ describe("a soul's hard limits, traits and name on the default runtime", () => {
221221
expect(systemOf(s.seen[0])).not.toContain('## Hard limits');
222222
});
223223
});
224+
225+
describe("a soul's identity on the GMI path", () => {
226+
it.each([
227+
["agent({ runtime: 'gmi', soul })", async (k: string) => agent(stubbed(k, { runtime: 'gmi', soul: dir }))],
228+
["souledAgent({ runtime: 'gmi' })", async (k: string) => souledAgent({ ...stubbed(k, { runtime: 'gmi' }), soul: dir } as never)],
229+
] as const)('%s: the system text of every model call ends with the hard-limits block, and holds the trait paragraph once', async (_name, build) => {
230+
writeSoul(FULL);
231+
const k = key();
232+
// More replies than calls: the compile on close answers from them, and an unexpected call would throw.
233+
const s = script('openai', k, { whole: true, replies: [reply.text('Hello.'), reply.text('Notes.'), reply.text('Notes.'), reply.text('Notes.')] });
234+
const a = await build(k);
235+
try {
236+
await a.session('s').send('Hi.');
237+
} finally {
238+
await a.close();
239+
}
240+
const [turn] = turnCalls(s);
241+
expect(systemOf(turn).trimEnd().endsWith(BLOCK)).toBe(true);
242+
expect(systemOf(turn)).toContain('You are Aria.');
243+
expect(systemOf(turn).split(PERSONALITY_SECTION_HEADING)).toHaveLength(2);
244+
expect(systemOf(turn)).toContain(renderPersonalitySection({ honestyHumility: 0.9, extraversion: 0.8 })!);
245+
});
246+
247+
it('closes the prompt with the limits after systemBlocks text too', async () => {
248+
writeSoul(FULL);
249+
vi.spyOn(console, 'warn').mockImplementation(() => undefined);
250+
const k = key();
251+
const s = script('openai', k, { replies: [reply.text('Hello.')] });
252+
await gmi(stubbed(k, { soul: dir, systemBlocks: [{ text: 'Host block.', cacheBreakpoint: true }] })).session('s').send('Hi.');
253+
expect(systemOf(s.seen[0])).toContain('Host block.');
254+
expect(systemOf(s.seen[0]).trimEnd().endsWith(BLOCK)).toBe(true);
255+
});
256+
257+
it('lets the personality option win over hexaco, and names the agent by the name option', async () => {
258+
writeSoul(FULL);
259+
const k = key();
260+
const s = script('openai', k, { replies: [reply.text('Hello.')] });
261+
await gmi(stubbed(k, { soul: dir, name: 'Support', personality: { openness: 0.9 } })).session('s').send('Hi.');
262+
expect(systemOf(s.seen[0])).toContain(renderPersonalitySection({ openness: 0.9 })!);
263+
expect(systemOf(s.seen[0]).split(PERSONALITY_SECTION_HEADING)).toHaveLength(2);
264+
expect(systemOf(s.seen[0])).toContain('Assistant name: Support.');
265+
expect(systemOf(s.seen[0])).not.toContain('Assistant name: Aria.');
266+
});
267+
268+
it('writes one soul.field_ignored line for a front matter voice, and the turn runs', async () => {
269+
writeSoul(`${FULL}\nvoice:\n provider: elevenlabs\n voiceId: rachel`);
270+
const k = key();
271+
script('openai', k, { replies: [reply.text('Hello.')] });
272+
const a = gmi(stubbed(k, { soul: dir }));
273+
expect((await a.session('s').send('Hi.')).text).toBe('Hello.');
274+
expect(events('soul.field_ignored')).toEqual([expect.objectContaining({ field: 'voice' })]);
275+
});
276+
277+
it('runs a soul with no agentId and no name, on an agent with no name, with its limits last and its traits', async () => {
278+
writeSoul(['hexaco:', ' honestyHumility: 0.9', 'hardLimits:', ...LIMITS.map((limit) => ` - ${limit}`)].join('\n'), 'You are terse.');
279+
const k = key();
280+
const s = script('openai', k, { whole: true, replies: [reply.text('A.'), reply.text('B.')] });
281+
await gmi(stubbed(k, { soul: dir })).session('s').send('Hi.');
282+
await agent(stubbed(k, { soul: dir })).generate('Hi.');
283+
for (const call of s.seen) {
284+
expect(systemOf(call).trimEnd().endsWith(BLOCK)).toBe(true);
285+
expect(systemOf(call)).toContain(renderPersonalitySection({ honestyHumility: 0.9 })!);
286+
}
287+
});
288+
});

‎src/api/runtime/__tests__/gmiPersona.test.ts‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { describe, expect, it } from 'vitest';
2-
import { personaFromAgentOptions } from '../gmiPersona.js';
2+
import { agentStableIdOf, derivedSoulPersonaId, personaFromAgentOptions } from '../gmiPersona.js';
33
import { resolveCognition } from '../gmiCognition.js';
44
import { buildSystemPrompt, type AgentOptions } from '../../agent.js';
55
import { ALL_METAPROMPT_PRESETS } from '../../../cognition/substrate/personas/metaprompt_presets.js';
6+
import { parseSoul } from '../../../cognition/substrate/personas/SoulLoader.js';
67

78
const tool = {
89
id: 'lookup-tool',
@@ -27,7 +28,8 @@ describe('personaFromAgentOptions', () => {
2728
};
2829
const p = personaFromAgentOptions(opts, resolveCognition({}), []);
2930
expect(p).toMatchObject({ id: 'ops-helper', name: 'Ops Helper', description: 'Answer briefly.', version: '1.0.0', defaultProviderId: 'openai', defaultModelId: 'gpt-4o' });
30-
expect(p.baseSystemPrompt).toBe(buildSystemPrompt(opts));
31+
// The persona has traits, so the GMI renders the personality section itself and the base prompt leaves it out.
32+
expect(p.baseSystemPrompt).toBe(buildSystemPrompt({ ...opts, personality: undefined }));
3133
expect(p.personalityTraits).toMatchObject({ openness: 0.9 });
3234
expect(p.defaultModelCompletionOptions).toEqual({ maxTokens: 300, effort: 'low', requestTimeout: 9000 });
3335
expect(p.metaPrompts).toEqual([]);
@@ -69,3 +71,54 @@ describe('personaFromAgentOptions', () => {
6971
expect(idOf('助手')).not.toBe(idOf('秘書'));
7072
});
7173
});
74+
75+
describe('personaFromAgentOptions with a soul', () => {
76+
const base = { provider: 'openai', model: 'gpt-4o' } as const;
77+
const soul = parseSoul(
78+
'---\nname: Aria\nagentId: aria-1\nrole: Support for the clinic\nhexaco:\n honestyHumility: 0.9\ndefaultMood: calm\nallowedMoods: [calm, warm]\n' +
79+
'hardLimits:\n - Never name a diagnosis.\nreasoningTrace:\n maxEntries: 7\nvoice:\n provider: x\navatar:\n type: y\nmetadata:\n team: care\n---\nYou are Aria.',
80+
);
81+
82+
it('takes the id, role, traits, mood, trace limits, hard limits and metadata from the soul, and never its voice or avatar', () => {
83+
const p = personaFromAgentOptions({ ...base, instructions: 'Answer briefly.' }, resolveCognition({}), [], soul) as unknown as Record<string, unknown>;
84+
expect(p).toMatchObject({
85+
id: 'aria-1',
86+
name: 'Aria',
87+
description: 'Support for the clinic',
88+
personalityTraits: { honesty: 0.9 },
89+
moodAdaptation: { enabled: true, defaultMood: 'calm', allowedMoods: ['calm', 'warm'] },
90+
reasoningTraceConfig: { maxEntries: 7 },
91+
hardLimits: ['Never name a diagnosis.'],
92+
metadata: { team: 'care' },
93+
});
94+
expect(p.voiceConfig).toBeUndefined();
95+
expect(p.avatarConfig).toBeUndefined();
96+
// The traits render in the GMI's own section, so the base prompt holds none, and it holds no limits block.
97+
expect(p.baseSystemPrompt).toBe(buildSystemPrompt({ ...base, instructions: 'Answer briefly.' }, soul, { personality: false }));
98+
expect(String(p.baseSystemPrompt)).not.toContain('## Hard limits');
99+
});
100+
101+
it('lets the name and personality options win, and keeps the id from the soul', () => {
102+
const p = personaFromAgentOptions({ ...base, name: 'Support', personality: { openness: 0.2 } }, resolveCognition({}), [], soul);
103+
expect(p).toMatchObject({ id: 'aria-1', name: 'Support', personalityTraits: { openness: 0.2 } });
104+
});
105+
106+
it('names a soul with a name and no agentId by the slug of that name', () => {
107+
expect(personaFromAgentOptions({ ...base, name: 'Support' }, resolveCognition({}), [], parseSoul('---\nname: Aria Stone\n---\nHi.')).id).toBe('aria-stone');
108+
});
109+
110+
it('gives a soul with no agentId and no name, on an agent with no name, an id derived from its text', () => {
111+
const bare = parseSoul('---\nhardLimits:\n - Never guess.\n---\nYou are terse.');
112+
const other = parseSoul('---\nhardLimits:\n - Never guess.\n---\nYou are warm.');
113+
const id = personaFromAgentOptions(base, resolveCognition({}), [], bare).id;
114+
expect(id).toMatch(/^soul-[0-9a-f]{16}$/);
115+
expect(personaFromAgentOptions(base, resolveCognition({}), [], bare).id).toBe(id);
116+
expect(derivedSoulPersonaId(bare)).toBe(id);
117+
expect(personaFromAgentOptions(base, resolveCognition({}), [], other).id).not.toBe(id);
118+
expect(personaFromAgentOptions(base, resolveCognition({}), [], bare).hardLimits).toEqual(['Never guess.']);
119+
// The derived id is the persona's alone: the agent has no stable id.
120+
expect(agentStableIdOf(base, bare)).toBeUndefined();
121+
expect(agentStableIdOf({ name: 'Ops' }, null)).toBe('ops');
122+
expect(agentStableIdOf({ name: 'Ops' }, soul)).toBe('aria-1');
123+
});
124+
});

0 commit comments

Comments
 (0)