@@ -2,9 +2,14 @@ import fs from "node:fs";
22import path from "node:path" ;
33import { afterEach , describe , expect , it } from "vitest" ;
44import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js" ;
5+ import {
6+ lookupSessionGoalOperation ,
7+ type SessionGoalOperation ,
8+ } from "../config/sessions/goals-operations.js" ;
59import {
610 loadExactSessionEntry ,
711 listSessionParticipantsReadOnly ,
12+ persistSessionTranscriptTurn ,
813 upsertSessionEntryCore ,
914} from "../config/sessions/session-accessor.js" ;
1015import {
@@ -67,8 +72,36 @@ async function createStore(layout: "shared" | "custom") {
6772
6873async function createHistoricalSharedStore ( corruptIndex = false ) {
6974 const store = await createStore ( "shared" ) ;
75+ const goalOperation = {
76+ action : "start" ,
77+ operationId : "goal-before-participant-upgrade" ,
78+ issuedAtMs : Date . now ( ) ,
79+ requestFingerprint : "shared-store-goal-fixture" ,
80+ objective : "Preserve this Goal across the participant migration." ,
81+ } satisfies SessionGoalOperation ;
82+ const turn = await persistSessionTranscriptTurn (
83+ { ...store . scope , sessionId : "doctor-session" } ,
84+ {
85+ expectedSessionId : "doctor-session" ,
86+ messages : [ { message : { role : "user" , content : goalOperation . objective } } ] ,
87+ sessionTurnMutation : { kind : "goal" , operation : goalOperation , runId : "goal-start" } ,
88+ updateMode : "none" ,
89+ } ,
90+ ) ;
91+ const goalReceipt = turn . sessionTurnMutationResult ?. result ;
92+ expect ( goalReceipt ) . toMatchObject ( { action : "start" , status : "started" , runId : "goal-start" } ) ;
93+ expect (
94+ lookupSessionGoalOperation ( {
95+ ...store . scope ,
96+ expectedSessionId : "doctor-session" ,
97+ operation : goalOperation ,
98+ } ) ,
99+ ) . toEqual ( goalReceipt ) ;
100+ closeOpenClawAgentDatabasesForTest ( ) ;
101+ closeOpenClawStateDatabaseForTest ( ) ;
70102 const database = openNodeSqliteDatabase ( store . sqlitePath ) ;
71103 try {
104+ const goalState = readStoredGoalState ( database , store . scope . sessionKey ) ;
72105 // Restore the actual v17 table contract as well as both version markers;
73106 // header-only downgrades test refusal, not an ordinary supported upgrade.
74107 database . exec ( "DROP TABLE session_participants;" ) ;
@@ -94,10 +127,31 @@ async function createHistoricalSharedStore(corruptIndex = false) {
94127 const schemaVersion = Number ( database . prepare ( "PRAGMA schema_version" ) . get ( ) ?. schema_version ) ;
95128 database . exec ( `PRAGMA schema_version = ${ schemaVersion + 1 } ;` ) ;
96129 }
130+ return { ...store , goalOperation, goalReceipt, goalState } ;
97131 } finally {
98132 database . close ( ) ;
99133 }
100- return store ;
134+ }
135+
136+ function readStoredGoalState (
137+ database : ReturnType < typeof openNodeSqliteDatabase > ,
138+ sessionKey : string ,
139+ ) {
140+ return {
141+ goal : database
142+ . prepare (
143+ "SELECT json_extract(entry_json, '$.goal') AS goal_json FROM session_nodes WHERE session_key = ?" ,
144+ )
145+ . get ( sessionKey ) ,
146+ receipts : database
147+ . prepare ( "SELECT * FROM session_goal_operations WHERE session_key = ? ORDER BY operation_id" )
148+ . all ( sessionKey ) ,
149+ ddl : database
150+ . prepare (
151+ "SELECT name, sql FROM sqlite_schema WHERE tbl_name = 'session_goal_operations' ORDER BY name" ,
152+ )
153+ . all ( ) ,
154+ } ;
101155}
102156
103157async function repairHistoricalSharedStore (
@@ -122,7 +176,7 @@ async function repairHistoricalSharedStore(
122176 }
123177}
124178
125- function expectUpgradedSharedStore ( store : Awaited < ReturnType < typeof createStore > > ) {
179+ function expectUpgradedSharedStore ( store : Awaited < ReturnType < typeof createHistoricalSharedStore > > ) {
126180 const reopened = openOpenClawAgentDatabase ( store . options ) ;
127181 expect ( reopened . agentId ) . toBe ( "main" ) ;
128182 expect ( reopened . db . prepare ( "PRAGMA user_version" ) . get ( ) ?. user_version ) . toBe ( 18 ) ;
@@ -131,6 +185,15 @@ function expectUpgradedSharedStore(store: Awaited<ReturnType<typeof createStore>
131185 schema_version : 18 ,
132186 } ) ;
133187 expect ( loadExactSessionEntry ( store . scope ) ?. entry . sessionId ) . toBe ( "doctor-session" ) ;
188+ expect ( loadExactSessionEntry ( store . scope ) ?. entry . goal ) . toEqual ( store . goalReceipt ?. goal ) ;
189+ expect ( readStoredGoalState ( reopened . db , store . scope . sessionKey ) ) . toEqual ( store . goalState ) ;
190+ expect (
191+ lookupSessionGoalOperation ( {
192+ ...store . scope ,
193+ expectedSessionId : "doctor-session" ,
194+ operation : store . goalOperation ,
195+ } ) ,
196+ ) . toEqual ( store . goalReceipt ) ;
134197 expect ( listSessionParticipantsReadOnly ( store . scope ) . get ( store . scope . sessionKey ) ) . toEqual ( [
135198 {
136199 identity : { type : "profile" , id : "person" } ,
@@ -220,6 +283,7 @@ describe("Doctor canonical session SQLite targets", () => {
220283 ) . toBe ( false ) ;
221284 const preserved = openNodeSqliteDatabase ( store . sqlitePath , { readOnly : true } ) ;
222285 try {
286+ expect ( readStoredGoalState ( preserved , store . scope . sessionKey ) ) . toEqual ( store . goalState ) ;
223287 expect ( preserved . prepare ( "PRAGMA user_version" ) . get ( ) ?. user_version ) . toBe ( 17 ) ;
224288 expect ( preserved . prepare ( "SELECT agent_id, schema_version FROM schema_meta" ) . get ( ) ) . toEqual ( {
225289 agent_id : "main" ,
0 commit comments