Skip to content

Commit 07ac59e

Browse files
committed
test(souls): pin the hard limits, the trait paragraph and the name on the default runtime
Signed-off-by: jddunn <[email protected]>
1 parent 7429805 commit 07ac59e

2 files changed

Lines changed: 150 additions & 0 deletions

File tree

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

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ import * as fs from 'node:fs';
1010
import * as os from 'node:os';
1111
import * as path from 'node:path';
1212
import pino from 'pino';
13+
import { z } from 'zod';
1314
import { agent, type Agent, type AgentOptions } from '../agent';
1415
import { gmi } from '../gmi';
1516
import { souledAgent } from '../souledAgent';
1617
import { SoulLoadError } from '../../cognition/substrate/personas/SoulLoader';
1718
import { hardLimitsBlock } from '../../cognition/substrate/personas/hardLimits';
19+
import { PERSONALITY_SECTION_HEADING, renderPersonalitySection } from '../../cognition/substrate/personas/personalityPrompt';
20+
import { SCHEMA_INSTRUCTION_LEAD } from '../runtime/structuredReply';
1821
import { resetLoggerFactory, setLogger } from '../../core/logging/loggerFactory';
1922
import { reply, script, type ProviderScript } from './helpers/stubProviders';
2023
import { globalLLMProviderHealth } from '../../core/safety/LLMProviderHealthRegistry';
@@ -118,3 +121,103 @@ describe('a soul that fails to load', () => {
118121
expect(lines[0]).not.toHaveProperty('msg');
119122
});
120123
});
124+
125+
/** The agent built on the soul folder by each way that runs on the default runtime. */
126+
const plainPaths = [
127+
['agent({ soul })', async (k: string, extra: Record<string, unknown> = {}) => agent(stubbed(k, { soul: dir, ...extra }))],
128+
['souledAgent()', async (k: string, extra: Record<string, unknown> = {}) => souledAgent({ ...stubbed(k, extra), soul: dir } as never)],
129+
] as const;
130+
131+
describe("a soul's hard limits, traits and name on the default runtime", () => {
132+
// One turn on a new store: the wiki's recall block is still a system message, so a turn that recalled something
133+
// would have that block after the limits until recalled text leaves the system role.
134+
it.each(plainPaths)('%s: the last system text of the model call is the hard-limits block', async (_name, build) => {
135+
writeSoul(FULL);
136+
const k = key();
137+
const s = script('openai', k, { whole: true, replies: [reply.text('Hello.'), reply.text('Notes.')] });
138+
const a = await build(k);
139+
try {
140+
await a.session('s').send('Hi.');
141+
} finally {
142+
await a.close();
143+
}
144+
const [turn] = turnCalls(s);
145+
expect(systemTexts(turn).join('\n\n').endsWith(BLOCK)).toBe(true);
146+
expect(systemOf(turn).indexOf('You are Aria.')).toBeLessThan(systemOf(turn).indexOf(BLOCK));
147+
expect(systemOf(turn).split(BLOCK)).toHaveLength(2);
148+
});
149+
150+
it("with systemBlocks, the limits are one more block after the host's, and the cache breakpoint stays on the host's block", async () => {
151+
writeSoul(FULL);
152+
vi.spyOn(console, 'warn').mockImplementation(() => undefined);
153+
const k = key();
154+
const s = script('openai', k, { whole: true, replies: [reply.text('Hello.')] });
155+
await agent(stubbed(k, { soul: dir, systemBlocks: [{ text: 'Host block.', cacheBreakpoint: true }] })).generate('Hi.');
156+
const system = s.seen[0].messages.filter((m) => m.role === 'system');
157+
expect(system).toHaveLength(1);
158+
const parts = system[0].content as unknown as Array<{ text: string; cache_control?: unknown }>;
159+
expect(parts.map((part) => part.text)).toEqual(['Host block.', BLOCK]);
160+
expect(parts[0].cache_control).toEqual({ type: 'ephemeral' });
161+
expect(parts[1].cache_control).toBeUndefined();
162+
});
163+
164+
it("renders the soul's hexaco paragraph when no personality is set, once, and the personality option wins over it", async () => {
165+
writeSoul(FULL);
166+
const k = key();
167+
const s = script('openai', k, { whole: true, replies: [reply.text('A.'), reply.text('B.')] });
168+
await agent(stubbed(k, { soul: dir })).generate('Hi.');
169+
await agent(stubbed(k, { soul: dir, personality: { openness: 0.9 } })).generate('Hi.');
170+
expect(systemOf(s.seen[0])).toContain(renderPersonalitySection({ honestyHumility: 0.9, extraversion: 0.8 })!);
171+
expect(systemOf(s.seen[0]).split(PERSONALITY_SECTION_HEADING)).toHaveLength(2);
172+
expect(systemOf(s.seen[1])).toContain(renderPersonalitySection({ openness: 0.9 })!);
173+
expect(systemOf(s.seen[1])).not.toContain(renderPersonalitySection({ honestyHumility: 0.9, extraversion: 0.8 })!);
174+
// The limits still close both prompts.
175+
for (const call of s.seen) expect(systemOf(call).endsWith(BLOCK)).toBe(true);
176+
});
177+
178+
it("names the agent by the name option, else by the soul's name", async () => {
179+
writeSoul(FULL);
180+
const k = key();
181+
const s = script('openai', k, { whole: true, replies: [reply.text('A.'), reply.text('B.')] });
182+
await agent(stubbed(k, { soul: dir, name: 'Support' })).generate('Hi.');
183+
await agent(stubbed(k, { soul: dir })).generate('Hi.');
184+
expect(systemOf(s.seen[0])).toContain('Assistant name: Support.');
185+
expect(systemOf(s.seen[0])).not.toContain('Assistant name: Aria.');
186+
expect(systemOf(s.seen[1])).toContain('Assistant name: Aria.');
187+
});
188+
189+
it('a structured send whose payload carries no schema has the schema instruction right before the limits, which stay last', async () => {
190+
writeSoul(FULL);
191+
const k = key();
192+
const s = script('openai', k, { whole: true, replies: [reply.text('{"city":"Lyon","scores":{"a":1}}')] });
193+
// A record schema is outside OpenAI's strict JSON schema, so the schema rides the prompt.
194+
const schema = z.object({ city: z.string(), scores: z.record(z.string(), z.number()) });
195+
const r = await agent(stubbed(k, { soul: dir })).session('s').send('Where?', { responseSchema: schema });
196+
expect(r.object).toEqual({ city: 'Lyon', scores: { a: 1 } });
197+
const texts = systemTexts(s.seen[0]);
198+
expect(texts.at(-1)).toBe(BLOCK);
199+
expect(texts.at(-2)).toContain(SCHEMA_INSTRUCTION_LEAD);
200+
expect(texts.slice(0, -2).join('\n\n')).toContain('You are Aria.');
201+
expect(texts.slice(0, -2).join('\n\n')).not.toContain(BLOCK);
202+
});
203+
204+
it('keeps the limits last under a working-memory block and under a per-call system prompt', async () => {
205+
writeSoul(FULL);
206+
const k = key();
207+
const s = script('openai', k, { whole: true, replies: [reply.text('A.'), reply.text('B.')] });
208+
const a = agent(stubbed(k, { soul: dir, memory: { working: async () => 'WORKING NOTES' } }));
209+
await a.session('s').send('Hi.');
210+
await a.generate('Hi.', { system: 'Answer in one word.' } as never);
211+
expect(systemOf(s.seen[0]).startsWith('WORKING NOTES')).toBe(true);
212+
expect(systemOf(s.seen[0]).endsWith(BLOCK)).toBe(true);
213+
expect(systemOf(s.seen[1])).toBe(`Answer in one word.\n\n${BLOCK}`);
214+
});
215+
216+
it('adds no block for a soul with no hard limits', async () => {
217+
writeSoul('name: Aria\nagentId: aria-1');
218+
const k = key();
219+
const s = script('openai', k, { whole: true, replies: [reply.text('A.')] });
220+
await agent(stubbed(k, { soul: dir })).generate('Hi.');
221+
expect(systemOf(s.seen[0])).not.toContain('## Hard limits');
222+
});
223+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* The hard-limits block as the last part of an agent's system prompt, and the split generateText makes when it sends
3+
* something else before that block.
4+
*/
5+
import { describe, expect, it } from 'vitest';
6+
import { splitHardLimits, withHardLimitsLast } from '../hardLimitsSystem.js';
7+
import { hardLimitsBlock } from '../../../cognition/substrate/personas/hardLimits.js';
8+
9+
const LIMITS = ['Never name a diagnosis.'];
10+
const BLOCK = hardLimitsBlock(LIMITS)!;
11+
12+
describe('withHardLimitsLast', () => {
13+
it('leaves the prompt as it is when there are no limits', () => {
14+
const blocks = [{ text: 'A', cacheBreakpoint: true }];
15+
expect(withHardLimitsLast('Prose.', undefined)).toBe('Prose.');
16+
expect(withHardLimitsLast('Prose.', ['', ' '])).toBe('Prose.');
17+
expect(withHardLimitsLast(blocks, [])).toBe(blocks);
18+
expect(withHardLimitsLast(undefined, undefined)).toBeUndefined();
19+
});
20+
it('appends the block to a string after a blank line, and is the block alone with no prompt', () => {
21+
expect(withHardLimitsLast('Prose.', LIMITS)).toBe(`Prose.\n\n${BLOCK}`);
22+
expect(withHardLimitsLast(undefined, LIMITS)).toBe(BLOCK);
23+
expect(withHardLimitsLast('', LIMITS)).toBe(BLOCK);
24+
});
25+
it("adds one marked block after the host's blocks, with no cache breakpoint, and leaves theirs as they are", () => {
26+
const blocks = [{ text: 'A', cacheBreakpoint: true, cacheTtl: '1h' as const }, { text: 'B' }];
27+
expect(withHardLimitsLast(blocks, LIMITS)).toEqual([...blocks, { text: BLOCK, _hardLimits: true }]);
28+
expect(blocks).toHaveLength(2);
29+
});
30+
});
31+
32+
describe('splitHardLimits', () => {
33+
it('splits a string that ends with the block it is told of', () => {
34+
expect(splitHardLimits(`Prose.\n\n${BLOCK}`, BLOCK)).toEqual({ head: 'Prose.', limits: BLOCK });
35+
expect(splitHardLimits(BLOCK, BLOCK)).toEqual({ head: undefined, limits: BLOCK });
36+
});
37+
it('does not split a string it was told nothing of, or one that does not end with the block', () => {
38+
expect(splitHardLimits(`Prose.\n\n${BLOCK}`, undefined)).toEqual({ head: `Prose.\n\n${BLOCK}`, limits: undefined });
39+
expect(splitHardLimits(`${BLOCK}\n\nMore.`, BLOCK)).toEqual({ head: `${BLOCK}\n\nMore.`, limits: undefined });
40+
});
41+
it('splits blocks at a marked last block only', () => {
42+
const host = [{ text: 'A', cacheBreakpoint: true }];
43+
expect(splitHardLimits([...host, { text: BLOCK, _hardLimits: true }], undefined)).toEqual({ head: host, limits: BLOCK });
44+
expect(splitHardLimits([{ text: BLOCK, _hardLimits: true }], undefined)).toEqual({ head: undefined, limits: BLOCK });
45+
expect(splitHardLimits([...host, { text: BLOCK }], BLOCK)).toEqual({ head: [...host, { text: BLOCK }], limits: undefined });
46+
});
47+
});

0 commit comments

Comments
 (0)