Skip to content

Commit d6226fc

Browse files
committed
test(personas): pin the trait paragraph of a persona on a runtime with no learning layer
Signed-off-by: jddunn <[email protected]>
1 parent 2642400 commit d6226fc

2 files changed

Lines changed: 105 additions & 2 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* @file agentos-persona-traits.e2e.test.ts
3+
* On the full runtime, a persona's HEXACO traits reach the provider as the personality section of the system prompt
4+
* with no learning layer; a persona without traits sends no section; the hard limits still close the prompt.
5+
*/
6+
import { afterEach, describe, expect, it, vi } from 'vitest';
7+
8+
const provider = vi.hoisted(() => ({
9+
seenMessages: [] as Array<Array<{ role: string; content: unknown }>>,
10+
}));
11+
12+
vi.mock('../../core/llm/providers/implementations/OpenAIProvider', () => {
13+
class OpenAIProvider {
14+
readonly providerId = 'openai';
15+
isInitialized = false;
16+
async initialize() { this.isInitialized = true; }
17+
async generateCompletion() { return { choices: [{ message: { role: 'assistant', content: 'ok' }, finishReason: 'stop' }], responseText: 'ok' }; }
18+
async *generateCompletionStream(_modelId: string, messages: Array<{ role: string; content: unknown }>) {
19+
provider.seenMessages.push(messages);
20+
yield { responseTextDelta: 'Noted.' };
21+
yield { isFinal: true, choices: [{ message: { role: 'assistant', content: 'Noted.' }, finishReason: 'stop' }] };
22+
}
23+
async generateEmbeddings() { return { embeddings: [] }; }
24+
async listAvailableModels() { return []; }
25+
async getModelInfo() { return undefined; }
26+
async checkHealth() { return { isHealthy: true }; }
27+
async shutdown() {}
28+
}
29+
return { OpenAIProvider };
30+
});
31+
32+
import { AgentOS } from '../AgentOS';
33+
import type { IPersonaDefinition } from '../../cognition/substrate/personas/IPersonaDefinition';
34+
import { hardLimitsBlock } from '../../cognition/substrate/personas/hardLimits';
35+
import { PERSONALITY_SECTION_HEADING, renderPersonalitySection } from '../../cognition/substrate/personas/personalityPrompt';
36+
import { parseSoul } from '../../cognition/substrate/personas/SoulLoader';
37+
38+
const TRAITS = { honesty: 0.9, extraversion: 0.8 };
39+
const warm = {
40+
id: 'warm', name: 'Warm', description: 'A persona with traits.', version: '1.0.0',
41+
baseSystemPrompt: 'You plan with the person.', personalityTraits: TRAITS, hardLimits: ['Never promise a date.'],
42+
} as unknown as IPersonaDefinition;
43+
const plain = { id: 'plain', name: 'Plain', description: 'A persona without traits.', version: '1.0.0', baseSystemPrompt: 'You are terse.' } as unknown as IPersonaDefinition;
44+
const souled = parseSoul('---\nname: Aria\nagentId: aria-1\nhexaco:\n honestyHumility: 0.9\n extraversion: 0.8\n---\nYou are Aria.').personaDefinition;
45+
46+
async function boot(): Promise<AgentOS> {
47+
return AgentOS.create({ modelProviderManagerConfig: { providers: [{ providerId: 'openai', enabled: true, isDefault: true, config: { apiKey: 'test' } }] }, turnPlanning: { enabled: false }, personas: [warm, plain, souled] } as any);
48+
}
49+
50+
const systemTextOf = (messages: Array<{ role: string; content: unknown }>): string =>
51+
messages
52+
.filter((m) => m.role === 'system')
53+
.map((m) => (typeof m.content === 'string' ? m.content : JSON.stringify(m.content)))
54+
.join('\n\n');
55+
56+
describe("a persona's traits in the system prompt with no learning layer", () => {
57+
let booted: AgentOS | undefined;
58+
afterEach(async () => {
59+
if (booted) await booted.shutdown();
60+
booted = undefined;
61+
provider.seenMessages.length = 0;
62+
vi.restoreAllMocks();
63+
});
64+
65+
it('renders the personality section once, before the hard limits, and none for a persona without traits', async () => {
66+
booted = await boot();
67+
for await (const _chunk of booted.processRequest({ userId: 'u', sessionId: 's-warm', selectedPersonaId: 'warm', textInput: 'Will I pass?' } as any)) { /* drain */ }
68+
const system = systemTextOf(provider.seenMessages.at(-1) ?? []);
69+
expect(system).toContain(renderPersonalitySection(TRAITS)!);
70+
expect(system.split(PERSONALITY_SECTION_HEADING)).toHaveLength(2);
71+
expect(system.trimEnd().endsWith(hardLimitsBlock(warm.hardLimits)!)).toBe(true);
72+
for await (const _chunk of booted.processRequest({ userId: 'u', sessionId: 's-plain', selectedPersonaId: 'plain', textInput: 'Hello.' } as any)) { /* drain */ }
73+
expect(systemTextOf(provider.seenMessages.at(-1) ?? [])).not.toContain(PERSONALITY_SECTION_HEADING);
74+
});
75+
76+
it("renders it for a persona loaded from a soul file's front matter", async () => {
77+
booted = await boot();
78+
for await (const _chunk of booted.processRequest({ userId: 'u', sessionId: 's-souled', selectedPersonaId: 'aria-1', textInput: 'Hello.' } as any)) { /* drain */ }
79+
const system = systemTextOf(provider.seenMessages.at(-1) ?? []);
80+
expect(system).toContain('You are Aria.');
81+
expect(system).toContain(renderPersonalitySection(TRAITS)!);
82+
});
83+
});

‎src/cognition/substrate/__tests__/GMIManager.learning.test.ts‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ const persona: IPersonaDefinition = {
3232
baseSystemPrompt: 'You review pull requests.',
3333
personalityTraits: { honesty: 0.9, extraversion: 0.5 },
3434
};
35+
/** A persona with no traits. */
36+
const traitless: IPersonaDefinition = {
37+
id: 'plain-reviewer',
38+
name: 'Plain reviewer',
39+
description: 'A persona without traits.',
40+
version: '1.0.0',
41+
baseSystemPrompt: 'You review pull requests.',
42+
};
3543
const turn = (over: Partial<TurnInputForLearning> = {}): TurnInputForLearning => ({
3644
turnId: 't',
3745
userId: 'user-1',
@@ -47,8 +55,8 @@ const managers: GMIManager[] = [];
4755
async function managerWith(learningLayer?: ILearningLayer, extra: Partial<GMIManagerConfig> = {}): Promise<GMIManager> {
4856
const personaLoader: IPersonaLoader = {
4957
initialize: async () => undefined,
50-
loadPersonaById: async (id: string) => (id === persona.id ? persona : undefined),
51-
loadAllPersonaDefinitions: async () => [persona],
58+
loadPersonaById: async (id: string) => [persona, traitless].find((known) => known.id === id),
59+
loadAllPersonaDefinitions: async () => [persona, traitless],
5260
};
5361
const conversationManager = {
5462
getOrCreateConversationContext: async () => new ConversationContext('conv-1'),
@@ -430,4 +438,16 @@ describe('a runtime without a learning layer', () => {
430438
const { gmi } = await manager.getOrCreateGMIForSession('user-1', 'session-1', persona.id, undefined, undefined, undefined, undefined, undefined, 'A');
431439
expect(gmi.getLearnerSession!()).toBeUndefined();
432440
});
441+
442+
it("gives a persona's GMI its traits, which the GMI renders itself, and a persona with none no traits", async () => {
443+
const initialize = vi.spyOn(GMI.prototype, 'initialize');
444+
const manager = await managerWith();
445+
await manager.getOrCreateGMIForSession('user-1', 'session-1', persona.id, undefined, undefined, undefined, undefined, undefined, 'A');
446+
await manager.getOrCreateGMIForSession('user-1', 'session-2', traitless.id, undefined, undefined, undefined, undefined, undefined, 'A');
447+
const [withTraits, without] = initialize.mock.calls.map(([, config]) => config);
448+
expect(withTraits.personaTraits).toEqual({ authored: { honesty: 0.9, extraversion: 0.5 } });
449+
expect(withTraits.learning).toBeUndefined();
450+
expect(without.personaTraits).toBeUndefined();
451+
expect(without.learning).toBeUndefined();
452+
});
433453
});

0 commit comments

Comments
 (0)