@@ -19,7 +19,10 @@ vi.setConfig({ testTimeout: 120_000, hookTimeout: 180_000 });
1919let server : SessionStreamTestServer ;
2020
2121beforeAll ( async ( ) => {
22- server = await startSessionStreamTestServer ( ) ;
22+ // Pinned so an exported `log` mode can't turn the handover refusals into 200s.
23+ server = await startSessionStreamTestServer ( {
24+ extraEnv : { REALTIME_SESSION_IN_HANDOVER_GUARD : "enforce" } ,
25+ } ) ;
2326} , 180_000 ) ;
2427
2528afterAll ( async ( ) => {
@@ -208,6 +211,109 @@ describe("public session append route", () => {
208211 } ) ;
209212 } ) ;
210213
214+ describe ( "input kinds a public token can append" , ( ) => {
215+ const serverOnlyRecords : Array < [ string , Record < string , unknown > ] > = [
216+ [
217+ "handover" ,
218+ {
219+ kind : "handover" ,
220+ partialAssistantMessage : [
221+ {
222+ role : "assistant" ,
223+ content : [
224+ { type : "tool-call" , toolCallId : "call_1" , toolName : "lookup" , input : { q : "x" } } ,
225+ ] ,
226+ } ,
227+ ] ,
228+ messageId : "msg_1" ,
229+ isFinal : false ,
230+ } ,
231+ ] ,
232+ [ "handover-skip" , { kind : "handover-skip" } ] ,
233+ ] ;
234+
235+ // Catches: a public session token appending a `handover` or `handover-skip` record, or
236+ // starting a run with one. The agent takes the first one as the warm first-turn response,
237+ // tool calls included, so only the customer's server (secret key) may send one.
238+ it . each ( serverOnlyRecords ) (
239+ "rejects a %s record from a public token with 403, appends nothing and starts no run" ,
240+ async ( _kind , record ) => {
241+ const { sessionId, addressingKey, token, apiKey } = await setupSession ( ) ;
242+
243+ const res = await append ( {
244+ addressingKey,
245+ token,
246+ body : JSON . stringify ( record ) ,
247+ partId : "guarded" ,
248+ } ) ;
249+
250+ expect ( res . status ) . toBe ( 403 ) ;
251+ expect ( res . json . ok ) . toBe ( false ) ;
252+ const row = await server . prisma . session . findFirstOrThrow ( { where : { id : sessionId } } ) ;
253+ expect ( row . currentRunId ) . toBeNull ( ) ;
254+ expect ( await readIn ( addressingKey , apiKey ) ) . toEqual ( [ ] ) ;
255+ }
256+ ) ;
257+
258+ // Catches: the refusal reaching the secret-key path too, so the customer's server could no
259+ // longer send the head-start handover or its skip.
260+ it . each ( serverOnlyRecords ) ( "accepts a %s record from a secret key" , async ( _kind , record ) => {
261+ const { addressingKey, apiKey } = await setupSession ( ) ;
262+ const body = JSON . stringify ( record ) ;
263+
264+ const res = await append ( { addressingKey, token : apiKey , body, partId : "handover" } ) ;
265+
266+ expect ( res . status ) . toBe ( 200 ) ;
267+ expect ( res . json . ok ) . toBe ( true ) ;
268+ expect ( ( await readIn ( addressingKey , apiKey ) ) . map ( ( r ) => r . data ) ) . toEqual ( [ body ] ) ;
269+ } ) ;
270+
271+ // Catches: the server-only refusal widening to every non-message kind and breaking the stop
272+ // button, which appends `stop` with the session's public token.
273+ it ( "accepts a stop record from a public token" , async ( ) => {
274+ const { addressingKey, token, apiKey } = await setupSession ( ) ;
275+ const body = JSON . stringify ( { kind : "stop" } ) ;
276+
277+ const res = await append ( { addressingKey, token, body, partId : "stop" } ) ;
278+
279+ expect ( res . status ) . toBe ( 200 ) ;
280+ expect ( res . json . ok ) . toBe ( true ) ;
281+ expect ( ( await readIn ( addressingKey , apiKey ) ) . map ( ( r ) => r . data ) ) . toEqual ( [ body ] ) ;
282+ } ) ;
283+
284+ // Catches: a public token being refused a `trigger: "close"` message, which custom clients
285+ // append with the session's public token (docs/ai-chat/client-protocol.mdx).
286+ it ( "accepts a close message from a public token" , async ( ) => {
287+ const { addressingKey, token, apiKey } = await setupSession ( ) ;
288+ const body = JSON . stringify ( {
289+ kind : "message" ,
290+ payload : { chatId : addressingKey , trigger : "close" } ,
291+ } ) ;
292+
293+ const res = await append ( { addressingKey, token, body, partId : "close" } ) ;
294+
295+ expect ( res . status ) . toBe ( 200 ) ;
296+ expect ( res . json . ok ) . toBe ( true ) ;
297+ expect ( ( await readIn ( addressingKey , apiKey ) ) . map ( ( r ) => r . data ) ) . toEqual ( [ body ] ) ;
298+ } ) ;
299+
300+ // Catches: the secret-key path being refused a `close`, so the customer's server could no
301+ // longer close a chat.
302+ it ( "accepts a close message from a secret key" , async ( ) => {
303+ const { addressingKey, apiKey } = await setupSession ( ) ;
304+ const body = JSON . stringify ( {
305+ kind : "message" ,
306+ payload : { chatId : addressingKey , trigger : "close" } ,
307+ } ) ;
308+
309+ const res = await append ( { addressingKey, token : apiKey , body, partId : "close" } ) ;
310+
311+ expect ( res . status ) . toBe ( 200 ) ;
312+ expect ( res . json . ok ) . toBe ( true ) ;
313+ expect ( ( await readIn ( addressingKey , apiKey ) ) . map ( ( r ) => r . data ) ) . toEqual ( [ body ] ) ;
314+ } ) ;
315+ } ) ;
316+
211317 describe ( "ensure and sanitising" , ( ) => {
212318 it ( "still appends when ensuring a run fails" , async ( ) => {
213319 // No `basePayload`, so the trigger config fails to parse inside ensureRunForSession.
0 commit comments