11/**
22 * Gateway session preview resolve tests.
33 */
4- import { expect , test } from "vitest" ;
4+ import { expect , test , vi } from "vitest" ;
5+ import * as sessionAccessor from "../config/sessions/session-accessor.js" ;
6+ import * as sessionHistoryEvents from "../config/sessions/session-accessor.sqlite-history-events.js" ;
57import type { GatewayClient } from "./server-methods/types.js" ;
68import { createToolSummaryPreviewTranscriptLines } from "./session-preview.test-helpers.js" ;
79import { rpcReq , writeSessionStore } from "./test-helpers.js" ;
@@ -14,6 +16,23 @@ import {
1416
1517const { createSessionStoreDir, openClient } = setupGatewaySessionsTestHarness ( ) ;
1618
19+ async function seedPreviewTail (
20+ sessionId : string ,
21+ messages : Array < { role : string ; content : string } > ,
22+ ) : Promise < void > {
23+ const { storePath } = await createSessionStoreDir ( ) ;
24+ await writeSessionStore ( {
25+ entries : { "agent:main:main" : sessionStoreEntry ( sessionId ) } ,
26+ } ) ;
27+ await sessionAccessor . persistSessionTranscriptTurn (
28+ { agentId : "main" , sessionId, sessionKey : "agent:main:main" , storePath } ,
29+ {
30+ messages : messages . map ( ( message ) => ( { message } ) ) ,
31+ touchSessionEntry : false ,
32+ } ,
33+ ) ;
34+ }
35+
1736function identifiedClient ( profileId : string , scopes : string [ ] = [ "operator.read" ] ) : GatewayClient {
1837 return {
1938 connect : {
@@ -113,6 +132,74 @@ test("sessions.preview honors maxChars up to the shared cap", async () => {
113132 ] ) ;
114133} ) ;
115134
135+ test ( "sessions.preview reads only a bounded tail from a large transcript" , async ( ) => {
136+ await seedPreviewTail (
137+ "sess-preview-bounded-tail" ,
138+ Array . from ( { length : 1024 } , ( _ , index ) => ( {
139+ role : "assistant" ,
140+ content : `message ${ String ( index ) } ` ,
141+ } ) ) ,
142+ ) ;
143+ const fullRead = vi . spyOn ( sessionAccessor , "readSessionTranscriptMessageEvents" ) ;
144+ const tailRead = vi . spyOn ( sessionHistoryEvents , "readRecentSessionTranscriptHistoryEvents" ) ;
145+
146+ try {
147+ const preview = await directSessionReq < {
148+ previews : Array < { items : Array < { role : string ; text : string } > } > ;
149+ } > ( "sessions.preview" , { keys : [ "main" ] , limit : 12 , maxChars : 120 } ) ;
150+
151+ expect ( preview . ok ) . toBe ( true ) ;
152+ expect ( preview . payload ?. previews [ 0 ] ?. items ) . toEqual (
153+ Array . from ( { length : 12 } , ( _ , index ) => ( {
154+ role : "assistant" ,
155+ text : `message ${ String ( 1012 + index ) } ` ,
156+ } ) ) ,
157+ ) ;
158+ expect ( fullRead ) . not . toHaveBeenCalled ( ) ;
159+ expect ( tailRead ) . toHaveBeenCalledOnce ( ) ;
160+ expect ( tailRead . mock . results [ 0 ] ) . toMatchObject ( {
161+ type : "return" ,
162+ value : { events : { length : 64 } , totalMessages : 1024 } ,
163+ } ) ;
164+ } finally {
165+ fullRead . mockRestore ( ) ;
166+ tailRead . mockRestore ( ) ;
167+ }
168+ } ) ;
169+
170+ test ( "sessions.preview widens its bounded tail past filtered tool-result rows" , async ( ) => {
171+ await seedPreviewTail ( "sess-preview-sparse-tail" , [
172+ ...Array . from ( { length : 12 } , ( _ , index ) => ( {
173+ role : "assistant" ,
174+ content : `visible ${ String ( index ) } ` ,
175+ } ) ) ,
176+ ...Array . from ( { length : 320 } , ( _ , index ) => ( {
177+ role : "toolResult" ,
178+ content : `tool ${ String ( index ) } ` ,
179+ } ) ) ,
180+ ] ) ;
181+ const tailRead = vi . spyOn ( sessionHistoryEvents , "readRecentSessionTranscriptHistoryEvents" ) ;
182+
183+ try {
184+ const preview = await directSessionReq < {
185+ previews : Array < { items : Array < { role : string ; text : string } > } > ;
186+ } > ( "sessions.preview" , { keys : [ "main" ] , limit : 12 , maxChars : 120 } ) ;
187+
188+ expect ( preview . payload ?. previews [ 0 ] ?. items ) . toEqual (
189+ Array . from ( { length : 12 } , ( _ , index ) => ( {
190+ role : "assistant" ,
191+ text : `visible ${ String ( index ) } ` ,
192+ } ) ) ,
193+ ) ;
194+ expect ( tailRead . mock . calls . map ( ( [ , options ] ) => options ) ) . toEqual ( [
195+ { maxBytes : 1024 * 1024 , maxLines : 64 , maxMessages : 64 } ,
196+ { maxBytes : 8 * 1024 * 1024 , maxLines : 1024 , maxMessages : 1024 } ,
197+ ] ) ;
198+ } finally {
199+ tailRead . mockRestore ( ) ;
200+ }
201+ } ) ;
202+
116203test ( "sessions.resolve by sessionId ignores fuzzy-search list limits and returns the exact match" , async ( ) => {
117204 await createSessionStoreDir ( ) ;
118205 const now = Date . now ( ) ;
0 commit comments