@@ -10,11 +10,14 @@ import * as fs from 'node:fs';
1010import * as os from 'node:os' ;
1111import * as path from 'node:path' ;
1212import pino from 'pino' ;
13+ import { z } from 'zod' ;
1314import { agent , type Agent , type AgentOptions } from '../agent' ;
1415import { gmi } from '../gmi' ;
1516import { souledAgent } from '../souledAgent' ;
1617import { SoulLoadError } from '../../cognition/substrate/personas/SoulLoader' ;
1718import { 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' ;
1821import { resetLoggerFactory , setLogger } from '../../core/logging/loggerFactory' ;
1922import { reply , script , type ProviderScript } from './helpers/stubProviders' ;
2023import { 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+ } ) ;
0 commit comments