|
| 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 | +}); |
0 commit comments