88 CURRENT_SCHEMA_VERSION ,
99 IncompatibleSchemaError ,
1010 V1_SCHEMA_SQL ,
11+ V2_SCHEMA_SQL ,
1112} from "../storage/schema-version.js" ;
1213
1314describe ( "Database" , ( ) => {
@@ -98,7 +99,7 @@ describe("Database", () => {
9899 expect ( tables . map ( ( table ) => table . name ) ) . not . toContain ( "_migrations" ) ;
99100 } ) ;
100101
101- it ( "should upgrade a known v1 supervisor schema to v2 when user_version is unset" , ( ) => {
102+ it ( "should upgrade a known v1 supervisor schema to v3 when user_version is unset" , ( ) => {
102103 const dbPath = join ( tempDir , "v1.db" ) ;
103104 const rawDb = new DatabaseSync ( dbPath ) ;
104105 rawDb . exec ( "PRAGMA user_version = 0" ) ;
@@ -108,13 +109,14 @@ describe("Database", () => {
108109 db = openDatabase ( dbPath ) ;
109110
110111 const userVersion = db . prepare ( "PRAGMA user_version" ) . get ( ) as { user_version : number } ;
111- expect ( userVersion . user_version ) . toBe ( 2 ) ;
112+ expect ( userVersion . user_version ) . toBe ( CURRENT_SCHEMA_VERSION ) ;
112113
113114 const supervisorColumns = db . prepare ( "PRAGMA table_info(supervisors)" ) . all ( ) as Array < {
114115 name : string ;
115116 } > ;
116117 expect ( supervisorColumns . map ( ( column ) => column . name ) ) . toEqual (
117118 expect . arrayContaining ( [
119+ "target_id" ,
118120 "evaluator_model" ,
119121 "max_supervision_count" ,
120122 "completed_supervision_count" ,
@@ -138,6 +140,79 @@ describe("Database", () => {
138140 expect ( upgradedIndex ?. name ) . toBe ( "idx_supervisor_cycle_attempts_cycle" ) ;
139141 } ) ;
140142
143+ it ( "should upgrade a known v2 supervisor schema to v3 and backfill target ids" , ( ) => {
144+ const dbPath = join ( tempDir , "v2.db" ) ;
145+ const rawDb = new DatabaseSync ( dbPath ) ;
146+ rawDb . exec ( "PRAGMA user_version = 2" ) ;
147+ rawDb . exec ( V2_SCHEMA_SQL ) ;
148+ rawDb . exec ( `
149+ INSERT INTO workspaces (id, path, target_runtime, opened_at, last_active_at, ui_state)
150+ VALUES ('ws-1', '/workspace', 'native', 1, 1, '{}');
151+ ` ) ;
152+ rawDb . exec ( `
153+ INSERT INTO terminals (id, workspace_id, kind, cwd, argv, cols, rows, created_at)
154+ VALUES ('term-1', 'ws-1', 'agent', '/workspace', '[]', 120, 30, 1);
155+ ` ) ;
156+ rawDb . exec ( `
157+ INSERT INTO sessions (
158+ id, workspace_id, terminal_id, provider_id, capability, state, started_at, last_active_at
159+ ) VALUES ('sess-1', 'ws-1', 'term-1', 'codex', 'full', 'idle', 1, 1);
160+ ` ) ;
161+ rawDb . exec ( `
162+ INSERT INTO supervisors (
163+ id,
164+ session_id,
165+ workspace_id,
166+ state,
167+ objective,
168+ evaluator_provider_id,
169+ evaluator_model,
170+ max_supervision_count,
171+ completed_supervision_count,
172+ scheduled_at,
173+ stop_reason,
174+ last_cycle_at,
175+ last_evaluated_turn_id,
176+ error_reason,
177+ created_at,
178+ updated_at
179+ ) VALUES (
180+ 'sup-legacy',
181+ 'sess-1',
182+ 'ws-1',
183+ 'idle',
184+ 'Legacy supervisor',
185+ 'codex',
186+ NULL,
187+ 0,
188+ 0,
189+ NULL,
190+ NULL,
191+ NULL,
192+ NULL,
193+ NULL,
194+ 1,
195+ 1
196+ );
197+ ` ) ;
198+ rawDb . close ( ) ;
199+
200+ db = openDatabase ( dbPath ) ;
201+
202+ const userVersion = db . prepare ( "PRAGMA user_version" ) . get ( ) as { user_version : number } ;
203+ expect ( userVersion . user_version ) . toBe ( CURRENT_SCHEMA_VERSION ) ;
204+
205+ const supervisorColumns = db . prepare ( "PRAGMA table_info(supervisors)" ) . all ( ) as Array < {
206+ name : string ;
207+ } > ;
208+ expect ( supervisorColumns . map ( ( column ) => column . name ) ) . toContain ( "target_id" ) ;
209+
210+ const upgradedRow = db
211+ . prepare ( "SELECT target_id FROM supervisors WHERE id = ?" )
212+ . get ( "sup-legacy" ) as { target_id : string } ;
213+ expect ( upgradedRow . target_id ) . toBe ( "legacy_sup-legacy" ) ;
214+ } ) ;
215+
141216 it ( "should restamp user_version for an already-current schema when it is unset" , ( ) => {
142217 const dbPath = join ( tempDir , "current-unstamped.db" ) ;
143218 db = openDatabase ( dbPath ) ;
0 commit comments