@@ -840,6 +840,24 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
840840 expect ( mustGet ( after ) . last_activity_timestamp_ms ) . toBeGreaterThan ( mustGet ( before ) . last_activity_timestamp_ms ) ;
841841 } ) ;
842842
843+ it ( 'increments session.metrics.total_turns without cost or duration' , async ( ) => {
844+ const store = createStore ( ) ;
845+ await seedSession ( store ) ;
846+ await store . createTurn ( makeCreateTurnInput ( { sessionId, turnId : 'turn-1' } ) ) ;
847+ const afterFirst = await store . getSession ( { tenant_id : tenant , session_id : sessionId } ) ;
848+ expect ( mustGet ( afterFirst ) . metrics ) . toEqual ( {
849+ total_cost_in_usd : 0 ,
850+ total_duration_ms : 0 ,
851+ total_turns : 1 ,
852+ } ) ;
853+ await finishTurn ( store , 'turn-1' ) ;
854+ await store . createTurn (
855+ makeCreateTurnInput ( { sessionId, turnId : 'turn-2' , previousTurnId : 'turn-1' , firstTurnId : 'turn-1' } ) ,
856+ ) ;
857+ const afterSecond = await store . getSession ( { tenant_id : tenant , session_id : sessionId } ) ;
858+ expect ( mustGet ( afterSecond ) . metrics . total_turns ) . toBe ( 2 ) ;
859+ } ) ;
860+
843861 it ( 'update_session_title_if_not_exist sets once and never overwrites' , async ( ) => {
844862 const store = createStore ( ) ;
845863 await seedSession ( store ) ;
@@ -1322,6 +1340,38 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
13221340 } ) ;
13231341 } ) ;
13241342
1343+ it ( 'folds duration into session.metrics when cancel applies, not on a second freeze' , async ( ) => {
1344+ const store = createStore ( ) ;
1345+ await seedSession ( store ) ;
1346+ await store . createTurn ( makeCreateTurnInput ( { sessionId, turnId : 'turn-1' } ) ) ;
1347+ const cancelledState = makeCancelledTurnState ( CancellationReason . CancelledForNextTurn ) ;
1348+ const record = await store . freezeAndGetTurn ( {
1349+ session_id : sessionId ,
1350+ turn_id : 'turn-1' ,
1351+ reason : CancellationReason . CancelledForNextTurn ,
1352+ turn_done_event : makeTurnDoneEvent ( cancelledState ) ,
1353+ } ) ;
1354+ if ( record . state . status !== 'cancelled' ) {
1355+ throw new Error ( `expected cancelled turn, got ${ record . state . status } ` ) ;
1356+ }
1357+ const afterCancel = await store . getSession ( { tenant_id : tenant , session_id : sessionId } ) ;
1358+ const elapsed_ms = Date . parse ( record . state . completed_at ) - record . created_at . getTime ( ) ;
1359+ expect ( mustGet ( afterCancel ) . metrics ) . toEqual ( {
1360+ total_cost_in_usd : 0 ,
1361+ total_duration_ms : elapsed_ms > 0 ? Math . trunc ( elapsed_ms ) : 0 ,
1362+ total_turns : 1 ,
1363+ } ) ;
1364+
1365+ await store . freezeAndGetTurn ( {
1366+ session_id : sessionId ,
1367+ turn_id : 'turn-1' ,
1368+ reason : CancellationReason . CancelledForNextTurn ,
1369+ turn_done_event : makeTurnDoneEvent ( cancelledState ) ,
1370+ } ) ;
1371+ const afterSecond = await store . getSession ( { tenant_id : tenant , session_id : sessionId } ) ;
1372+ expect ( mustGet ( afterSecond ) . metrics ) . toEqual ( mustGet ( afterCancel ) . metrics ) ;
1373+ } ) ;
1374+
13251375 it ( 'on an already-terminal turn is a plain read without duplicating turn.done' , async ( ) => {
13261376 const store = createStore ( ) ;
13271377 await seedSession ( store ) ;
@@ -1460,6 +1510,115 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
14601510 expect ( doneEvents [ 0 ] ) . toEqual ( turnDone ) ;
14611511 } ) ;
14621512
1513+ it ( 'adds cost and duration into session.metrics on running → terminal' , async ( ) => {
1514+ const store = createStore ( ) ;
1515+ await seedSession ( store ) ;
1516+ await store . createTurn ( makeCreateTurnInput ( { sessionId, turnId : 'turn-1' } ) ) ;
1517+ const turn = await store . getTurn ( { session_id : sessionId , turn_id : 'turn-1' } ) ;
1518+ const createdAt = mustGet ( turn ) . created_at ;
1519+ const completedAt = new Date ( createdAt . getTime ( ) + 1500 ) . toISOString ( ) ;
1520+ const state = {
1521+ ...makeDoneTurnState ( ) ,
1522+ completed_at : completedAt ,
1523+ metrics : { total_cost_in_usd : 1.25 } ,
1524+ } ;
1525+ await store . updateTurnState ( {
1526+ session_id : sessionId ,
1527+ turn_id : 'turn-1' ,
1528+ state,
1529+ turn_done_event : makeTurnDoneEvent ( state ) ,
1530+ } ) ;
1531+ const session = await store . getSession ( { tenant_id : tenant , session_id : sessionId } ) ;
1532+ expect ( mustGet ( session ) . metrics ) . toEqual ( {
1533+ total_cost_in_usd : 1.25 ,
1534+ total_duration_ms : 1500 ,
1535+ total_turns : 1 ,
1536+ } ) ;
1537+ } ) ;
1538+
1539+ it ( 'does not add session.metrics again on a losing terminal write' , async ( ) => {
1540+ const store = createStore ( ) ;
1541+ await seedSession ( store ) ;
1542+ await store . createTurn ( makeCreateTurnInput ( { sessionId, turnId : 'turn-1' } ) ) ;
1543+ const turn = await store . getTurn ( { session_id : sessionId , turn_id : 'turn-1' } ) ;
1544+ const createdAt = mustGet ( turn ) . created_at ;
1545+ const doneState = {
1546+ ...makeDoneTurnState ( ) ,
1547+ completed_at : new Date ( createdAt . getTime ( ) + 1500 ) . toISOString ( ) ,
1548+ metrics : { total_cost_in_usd : 1.25 } ,
1549+ } ;
1550+ await store . updateTurnState ( {
1551+ session_id : sessionId ,
1552+ turn_id : 'turn-1' ,
1553+ state : doneState ,
1554+ turn_done_event : makeTurnDoneEvent ( doneState ) ,
1555+ } ) ;
1556+ const afterFirst = mustGet ( await store . getSession ( { tenant_id : tenant , session_id : sessionId } ) ) . metrics ;
1557+
1558+ const losingState = {
1559+ ...makeCancelledTurnState ( CancellationReason . ClientCancelled ) ,
1560+ completed_at : new Date ( createdAt . getTime ( ) + 8000 ) . toISOString ( ) ,
1561+ metrics : { total_cost_in_usd : 9.99 } ,
1562+ } ;
1563+ await expect (
1564+ store . updateTurnState ( {
1565+ session_id : sessionId ,
1566+ turn_id : 'turn-1' ,
1567+ state : losingState ,
1568+ turn_done_event : makeTurnDoneEvent ( losingState ) ,
1569+ } ) ,
1570+ ) . rejects . toBeInstanceOf ( SessionStoreConflictError ) ;
1571+
1572+ const afterSecond = await store . getSession ( { tenant_id : tenant , session_id : sessionId } ) ;
1573+ expect ( mustGet ( afterSecond ) . metrics ) . toEqual ( afterFirst ) ;
1574+ expect ( afterFirst ) . toEqual ( {
1575+ total_cost_in_usd : 1.25 ,
1576+ total_duration_ms : 1500 ,
1577+ total_turns : 1 ,
1578+ } ) ;
1579+ } ) ;
1580+
1581+ it ( 'adds cost and duration from a second done turn onto existing session.metrics' , async ( ) => {
1582+ const store = createStore ( ) ;
1583+ await seedSession ( store ) ;
1584+ await store . createTurn ( makeCreateTurnInput ( { sessionId, turnId : 'turn-1' } ) ) ;
1585+ const turn1 = await store . getTurn ( { session_id : sessionId , turn_id : 'turn-1' } ) ;
1586+ const turn1Done = {
1587+ ...makeDoneTurnState ( ) ,
1588+ completed_at : new Date ( mustGet ( turn1 ) . created_at . getTime ( ) + 1500 ) . toISOString ( ) ,
1589+ metrics : { total_cost_in_usd : 1.25 } ,
1590+ } ;
1591+ await store . updateTurnState ( {
1592+ session_id : sessionId ,
1593+ turn_id : 'turn-1' ,
1594+ state : turn1Done ,
1595+ turn_done_event : makeTurnDoneEvent ( turn1Done ) ,
1596+ } ) ;
1597+
1598+ await store . createTurn (
1599+ makeCreateTurnInput ( { sessionId, turnId : 'turn-2' , previousTurnId : 'turn-1' , firstTurnId : 'turn-1' } ) ,
1600+ ) ;
1601+ const turn2 = await store . getTurn ( { session_id : sessionId , turn_id : 'turn-2' } ) ;
1602+ const turn2Done = {
1603+ ...makeDoneTurnState ( ) ,
1604+ completed_at : new Date ( mustGet ( turn2 ) . created_at . getTime ( ) + 800 ) . toISOString ( ) ,
1605+ metrics : { total_cost_in_usd : 0.5 } ,
1606+ } ;
1607+ await store . updateTurnState ( {
1608+ session_id : sessionId ,
1609+ turn_id : 'turn-2' ,
1610+ state : turn2Done ,
1611+ turn_done_event : makeTurnDoneEvent ( turn2Done ) ,
1612+ } ) ;
1613+
1614+ const session = await store . getSession ( { tenant_id : tenant , session_id : sessionId } ) ;
1615+ expect ( mustGet ( session ) . metrics ) . toEqual ( {
1616+ total_cost_in_usd : 1.75 ,
1617+ total_duration_ms : 2300 ,
1618+ total_turns : 2 ,
1619+ } ) ;
1620+ } ) ;
1621+
14631622 it ( 'missing turn → not found' , async ( ) => {
14641623 const store = createStore ( ) ;
14651624 await seedSession ( store ) ;
0 commit comments