@@ -2,7 +2,7 @@ import { z } from 'zod';
22import { MAIN_THREAD_ID } from '../../../src/agent-session/models/TurnRecord' ;
33import type { PersistedTurnEvent } from '../../../src/agent-session/schemas/events' ;
44import { EventType } from '../../../src/agent-session/schemas/events' ;
5- import { CancellationReason } from '../../../src/agent-session/schemas/turn' ;
5+ import { CancellationReason , type NonTerminalTurnState } from '../../../src/agent-session/schemas/turn' ;
66import type { ISessionStore } from '../../../src/agent-session/store/ISessionStore' ;
77import { decodeSessionEventPageToken } from '../../../src/agent-session/store/SessionEventPageToken' ;
88import {
@@ -1418,6 +1418,90 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
14181418 } ) ;
14191419 } ) ;
14201420
1421+ describe ( 'last_turn_details' , ( ) => {
1422+ async function lastTurnDetails ( store : ISessionStore ) {
1423+ return mustGet ( await store . getSession ( { tenant_id : tenant , session_id : sessionId } ) ) . last_turn_details ;
1424+ }
1425+
1426+ async function setNonTerminalState ( store : ISessionStore , turnId : string , state : NonTerminalTurnState ) {
1427+ await store . updateTurnNonTerminalState ( {
1428+ active_executor_id : TEST_ACTIVE_EXECUTOR_ID ,
1429+ session_id : sessionId ,
1430+ turn_id : turnId ,
1431+ state,
1432+ turn_update_event : makeTurnUpdateEvent ( state ) ,
1433+ } ) ;
1434+ }
1435+
1436+ async function freezeTurn ( store : ISessionStore , turnId : string ) {
1437+ await store . freezeAndGetTurn ( {
1438+ session_id : sessionId ,
1439+ turn_id : turnId ,
1440+ reason : CancellationReason . ClientCancelled ,
1441+ turn_done_event : makeTurnDoneEvent ( makeCancelledTurnState ( CancellationReason . ClientCancelled ) ) ,
1442+ } ) ;
1443+ }
1444+
1445+ it ( 'is null before the first turn' , async ( ) => {
1446+ const store = createStore ( ) ;
1447+ await seedSession ( store ) ;
1448+ expect ( await lastTurnDetails ( store ) ) . toBeNull ( ) ;
1449+ } ) ;
1450+
1451+ it ( 'createTurn sets id and running status with last_turn_id' , async ( ) => {
1452+ const store = createStore ( ) ;
1453+ await seedSession ( store ) ;
1454+ await store . createTurn ( makeCreateTurnInput ( { sessionId, turnId : 'turn-1' } ) ) ;
1455+ const session = mustGet ( await store . getSession ( { tenant_id : tenant , session_id : sessionId } ) ) ;
1456+ expect ( session . last_turn_details ) . toEqual ( { id : 'turn-1' , status : 'running' } ) ;
1457+ expect ( session . last_turn_id ) . toBe ( 'turn-1' ) ;
1458+ } ) ;
1459+
1460+ it ( 'follows pause, resume, and terminal transitions of the last turn' , async ( ) => {
1461+ const store = createStore ( ) ;
1462+ await seedSession ( store ) ;
1463+ await store . createTurn ( makeCreateTurnInput ( { sessionId, turnId : 'turn-1' } ) ) ;
1464+
1465+ await setNonTerminalState ( store , 'turn-1' , makePausedTurnState ( ) ) ;
1466+ expect ( await lastTurnDetails ( store ) ) . toEqual ( { id : 'turn-1' , status : 'paused' } ) ;
1467+
1468+ await setNonTerminalState ( store , 'turn-1' , { status : 'running' } ) ;
1469+ expect ( await lastTurnDetails ( store ) ) . toEqual ( { id : 'turn-1' , status : 'running' } ) ;
1470+
1471+ await finishTurn ( store , 'turn-1' ) ;
1472+ expect ( await lastTurnDetails ( store ) ) . toEqual ( { id : 'turn-1' , status : 'done' } ) ;
1473+ } ) ;
1474+
1475+ it ( 'freezeAndGetTurn records cancelled, and a repeat freeze leaves it unchanged' , async ( ) => {
1476+ const store = createStore ( ) ;
1477+ await seedSession ( store ) ;
1478+ await store . createTurn ( makeCreateTurnInput ( { sessionId, turnId : 'turn-1' } ) ) ;
1479+ await freezeTurn ( store , 'turn-1' ) ;
1480+ expect ( await lastTurnDetails ( store ) ) . toEqual ( { id : 'turn-1' , status : 'cancelled' } ) ;
1481+ await freezeTurn ( store , 'turn-1' ) ;
1482+ expect ( await lastTurnDetails ( store ) ) . toEqual ( { id : 'turn-1' , status : 'cancelled' } ) ;
1483+ } ) ;
1484+
1485+ it ( 'a superseded turn changing status does not touch the newer last turn' , async ( ) => {
1486+ const store = createStore ( ) ;
1487+ await seedSession ( store ) ;
1488+ await store . createTurn ( makeCreateTurnInput ( { sessionId, turnId : 'turn-1' } ) ) ;
1489+ await store . createTurn ( makeCreateTurnInput ( { sessionId, turnId : 'turn-2' } ) ) ;
1490+ const expected = { id : 'turn-2' , status : 'running' } ;
1491+ expect ( await lastTurnDetails ( store ) ) . toEqual ( expected ) ;
1492+
1493+ await setNonTerminalState ( store , 'turn-1' , makePausedTurnState ( ) ) ;
1494+ expect ( await lastTurnDetails ( store ) ) . toEqual ( expected ) ;
1495+
1496+ await finishTurn ( store , 'turn-1' ) ;
1497+ expect ( await lastTurnDetails ( store ) ) . toEqual ( expected ) ;
1498+
1499+ await store . createTurn ( makeCreateTurnInput ( { sessionId, turnId : 'turn-3' } ) ) ;
1500+ await freezeTurn ( store , 'turn-2' ) ;
1501+ expect ( await lastTurnDetails ( store ) ) . toEqual ( { id : 'turn-3' , status : 'running' } ) ;
1502+ } ) ;
1503+ } ) ;
1504+
14211505 describe ( 'createTurn' , ( ) => {
14221506 it ( 'atomically links last_turn_id and bumps activity' , async ( ) => {
14231507 const store = createStore ( ) ;
0 commit comments