@@ -1370,6 +1370,177 @@ describe("V1Migration database workflow", () => {
13701370 )
13711371 } )
13721372
1373+ // A pre-upgrade V1 client can keep appending to a session V2 has already imported. The completion
1374+ // latch meant those messages never reached session_message, so export silently truncated the session.
1375+ test ( "appends legacy messages written after the import completed" , async ( ) => {
1376+ await database (
1377+ Effect . gen ( function * ( ) {
1378+ const { db } = yield * Database . Service
1379+ yield * db . run (
1380+ sql `INSERT INTO project (id, worktree, time_created, time_updated, sandboxes) VALUES ('global', '/tmp/test', 1, 2, '[]')` ,
1381+ )
1382+ yield * db . run (
1383+ sql `INSERT INTO session (id, project_id, slug, directory, title, version, time_created, time_updated) VALUES ('ses_test', 'global', 'test', '/tmp/test', 'Test', '1', 1, 2)` ,
1384+ )
1385+ const insertLegacy = ( message : V1Migration . SourceMessage ) =>
1386+ db . run (
1387+ sql `INSERT INTO message (id, session_id, time_created, time_updated, data) VALUES (${ message . id } , 'ses_test', ${ message . time_created } , ${ message . time_updated } , ${ message . data } )` ,
1388+ )
1389+
1390+ const first = user ( "msg_000000000010aaaaaaaaaaaaaa" , { } , 10 )
1391+ const second = user ( "msg_000000000020aaaaaaaaaaaaaa" , { } , 20 )
1392+ yield * insertLegacy ( first )
1393+ yield * insertLegacy ( second )
1394+ expect ( yield * V1Migration . run ( ) ) . toEqual ( { status : "completed" } )
1395+ expect ( yield * db . all ( sql `SELECT id, seq FROM session_message ORDER BY seq` ) ) . toEqual ( [
1396+ { id : first . id , seq : 0 } ,
1397+ { id : second . id , seq : 1 } ,
1398+ ] )
1399+
1400+ // V2 keeps writing to the session after the import, and advances the aggregate sequence.
1401+ yield * db . run (
1402+ sql `INSERT INTO session_message (id, session_id, type, seq, time_created, time_updated, data) VALUES ('msg_from_v2', 'ses_test', 'user', 2, 30, 31, '{"text":"from v2","time":{"created":30}}')` ,
1403+ )
1404+ yield * db . run ( sql `UPDATE event_sequence SET seq = 5 WHERE aggregate_id = 'ses_test'` )
1405+
1406+ const third = user ( "msg_000000000030aaaaaaaaaaaaaa" , { } , 40 )
1407+ const fourth = user ( "msg_000000000040aaaaaaaaaaaaaa" , { } , 50 )
1408+ yield * insertLegacy ( third )
1409+ yield * insertLegacy ( fourth )
1410+ expect ( yield * V1Migration . run ( ) ) . toEqual ( { status : "completed" } )
1411+
1412+ // The late messages append after what is already projected: nothing is deleted or renumbered.
1413+ expect ( yield * db . all ( sql `SELECT id, seq FROM session_message ORDER BY seq` ) ) . toEqual ( [
1414+ { id : first . id , seq : 0 } ,
1415+ { id : second . id , seq : 1 } ,
1416+ { id : "msg_from_v2" , seq : 2 } ,
1417+ { id : third . id , seq : 3 } ,
1418+ { id : fourth . id , seq : 4 } ,
1419+ ] )
1420+ // Appending must not rewind the aggregate sequence, which would corrupt the event log.
1421+ expect ( yield * db . get ( sql `SELECT seq FROM event_sequence WHERE aggregate_id = 'ses_test'` ) ) . toEqual ( { seq : 5 } )
1422+
1423+ // A third run finds nothing to append and leaves the projection alone.
1424+ expect ( yield * V1Migration . run ( ) ) . toEqual ( { status : "completed" } )
1425+ expect ( ( yield * db . get < { value : number } > ( sql `SELECT COUNT(*) AS value FROM session_message` ) ) ?. value ) . toBe ( 5 )
1426+ } ) ,
1427+ )
1428+ } )
1429+
1430+ // V2 removes rows on a committed revert, so "absent from session_message" alone cannot mean "never
1431+ // imported". The per-session watermark keeps a reverted message from coming back.
1432+ test ( "does not re-append a legacy message a V2 revert removed" , async ( ) => {
1433+ await database (
1434+ Effect . gen ( function * ( ) {
1435+ const { db } = yield * Database . Service
1436+ yield * db . run (
1437+ sql `INSERT INTO project (id, worktree, time_created, time_updated, sandboxes) VALUES ('global', '/tmp/test', 1, 2, '[]')` ,
1438+ )
1439+ yield * db . run (
1440+ sql `INSERT INTO session (id, project_id, slug, directory, title, version, time_created, time_updated) VALUES ('ses_test', 'global', 'test', '/tmp/test', 'Test', '1', 1, 2)` ,
1441+ )
1442+ const first = user ( "msg_000000000010aaaaaaaaaaaaaa" , { } , 10 )
1443+ const second = user ( "msg_000000000020aaaaaaaaaaaaaa" , { } , 20 )
1444+ for ( const message of [ first , second ] )
1445+ yield * db . run (
1446+ sql `INSERT INTO message (id, session_id, time_created, time_updated, data) VALUES (${ message . id } , 'ses_test', ${ message . time_created } , ${ message . time_updated } , ${ message . data } )` ,
1447+ )
1448+ expect ( yield * V1Migration . run ( ) ) . toEqual ( { status : "completed" } )
1449+
1450+ // RevertEvent.Committed deletes the projection from the boundary on and clears the session's revert.
1451+ yield * db . run ( sql `DELETE FROM session_message WHERE id = ${ second . id } ` )
1452+
1453+ expect ( yield * V1Migration . run ( ) ) . toEqual ( { status : "completed" } )
1454+ expect ( ( yield * db . all < { id : string } > ( sql `SELECT id FROM session_message` ) ) . map ( ( row ) => row . id ) ) . toEqual ( [ first . id ] )
1455+ } ) ,
1456+ )
1457+ } )
1458+
1459+ // `transformed.session` is a V1-only recomputation, so writing it over the live row would replace V2's
1460+ // totals and clear revert and time_compacting. Only the appended messages' own amounts may be added.
1461+ test ( "adds appended totals to V2 session state instead of replacing it" , async ( ) => {
1462+ await database (
1463+ Effect . gen ( function * ( ) {
1464+ const { db } = yield * Database . Service
1465+ yield * db . run (
1466+ sql `INSERT INTO project (id, worktree, time_created, time_updated, sandboxes) VALUES ('global', '/tmp/test', 1, 2, '[]')` ,
1467+ )
1468+ yield * db . run (
1469+ sql `INSERT INTO session (id, project_id, slug, directory, title, version, time_created, time_updated) VALUES ('ses_test', 'global', 'test', '/tmp/test', 'Test', '1', 1, 2)` ,
1470+ )
1471+ const first = user ( "msg_000000000010aaaaaaaaaaaaaa" , { } , 10 )
1472+ yield * db . run (
1473+ sql `INSERT INTO message (id, session_id, time_created, time_updated, data) VALUES (${ first . id } , 'ses_test', ${ first . time_created } , ${ first . time_updated } , ${ first . data } )` ,
1474+ )
1475+ expect ( yield * V1Migration . run ( ) ) . toEqual ( { status : "completed" } )
1476+
1477+ // V2 continues the session: it owns the totals and carries live revert and compaction state.
1478+ yield * db . run (
1479+ sql `UPDATE session_v2 SET cost = 42, tokens_input = 7, tokens_output = 8, agent = 'v2-agent', revert = '{"messageID":"' || ${ first . id } || '"}', time_compacting = 99 WHERE id = 'ses_test'` ,
1480+ )
1481+
1482+ const prompt = user ( "msg_000000000030aaaaaaaaaaaaaa" , { } , 30 )
1483+ const reply = assistant ( "msg_000000000040aaaaaaaaaaaaaa" , prompt . id , { } , 40 )
1484+ for ( const message of [ prompt , reply ] )
1485+ yield * db . run (
1486+ sql `INSERT INTO message (id, session_id, time_created, time_updated, data) VALUES (${ message . id } , 'ses_test', ${ message . time_created } , ${ message . time_updated } , ${ message . data } )` ,
1487+ )
1488+ expect ( yield * V1Migration . run ( ) ) . toEqual ( { status : "completed" } )
1489+
1490+ const row = yield * db . get (
1491+ sql `SELECT cost, tokens_input, tokens_output, agent, revert, time_compacting FROM session_v2 WHERE id = 'ses_test'` ,
1492+ )
1493+ expect ( row ) . toEqual ( {
1494+ cost : 43 ,
1495+ tokens_input : 9 ,
1496+ tokens_output : 11 ,
1497+ agent : "v2-agent" ,
1498+ revert : `{"messageID":"${ first . id } "}` ,
1499+ time_compacting : 99 ,
1500+ } )
1501+ } ) ,
1502+ )
1503+ } )
1504+
1505+ // Databases migrated before watermarks existed cannot tell a late message from a reverted one, so the
1506+ // first run adopts what is there without appending, and later messages are then picked up normally.
1507+ test ( "adopts a completed database that has no watermark" , async ( ) => {
1508+ await database (
1509+ Effect . gen ( function * ( ) {
1510+ const { db } = yield * Database . Service
1511+ yield * db . run (
1512+ sql `INSERT INTO project (id, worktree, time_created, time_updated, sandboxes) VALUES ('global', '/tmp/test', 1, 2, '[]')` ,
1513+ )
1514+ yield * db . run (
1515+ sql `INSERT INTO session (id, project_id, slug, directory, title, version, time_created, time_updated) VALUES ('ses_test', 'global', 'test', '/tmp/test', 'Test', '1', 1, 2)` ,
1516+ )
1517+ const first = user ( "msg_000000000010aaaaaaaaaaaaaa" , { } , 10 )
1518+ yield * db . run (
1519+ sql `INSERT INTO message (id, session_id, time_created, time_updated, data) VALUES (${ first . id } , 'ses_test', ${ first . time_created } , ${ first . time_updated } , ${ first . data } )` ,
1520+ )
1521+ expect ( yield * V1Migration . run ( ) ) . toEqual ( { status : "completed" } )
1522+ yield * db . run ( sql `DELETE FROM kv WHERE key = 'migration.v1-v2:through:ses_test'` )
1523+
1524+ const second = user ( "msg_000000000020aaaaaaaaaaaaaa" , { } , 20 )
1525+ yield * db . run (
1526+ sql `INSERT INTO message (id, session_id, time_created, time_updated, data) VALUES (${ second . id } , 'ses_test', ${ second . time_created } , ${ second . time_updated } , ${ second . data } )` ,
1527+ )
1528+ expect ( yield * V1Migration . run ( ) ) . toEqual ( { status : "completed" } )
1529+ expect ( ( yield * db . all < { id : string } > ( sql `SELECT id FROM session_message` ) ) . map ( ( row ) => row . id ) ) . toEqual ( [ first . id ] )
1530+
1531+ const third = user ( "msg_000000000030aaaaaaaaaaaaaa" , { } , 30 )
1532+ yield * db . run (
1533+ sql `INSERT INTO message (id, session_id, time_created, time_updated, data) VALUES (${ third . id } , 'ses_test', ${ third . time_created } , ${ third . time_updated } , ${ third . data } )` ,
1534+ )
1535+ expect ( yield * V1Migration . run ( ) ) . toEqual ( { status : "completed" } )
1536+ expect ( ( yield * db . all < { id : string } > ( sql `SELECT id FROM session_message ORDER BY seq` ) ) . map ( ( row ) => row . id ) ) . toEqual ( [
1537+ first . id ,
1538+ third . id ,
1539+ ] )
1540+ } ) ,
1541+ )
1542+ } )
1543+
13731544 test ( "serializes concurrent callers and migrates each session once" , async ( ) => {
13741545 await database (
13751546 Effect . gen ( function * ( ) {
0 commit comments