@@ -928,6 +928,90 @@ SELECT COUNT(*) FROM vector_push_state
928928 "both beta sessions have state rows under the recreated generation" )
929929}
930930
931+ // TestVectorPushFullRechecksGenerationBeforeRecordingWitness pins the last
932+ // incarnation race: a full reconciliation must verify the generation survived
933+ // intact before it records the durable witness that later scoped pushes trust.
934+ func TestVectorPushFullRechecksGenerationBeforeRecordingWitness (t * testing.T ) {
935+ pgURL := testPGURL (t )
936+ sync , localDB , pg := newVectorPushTestSync (
937+ t , pgURL , "agentsview_vector_push_full_witness_race_test" )
938+ ctx := context .Background ()
939+
940+ seedVectorSession (t , localDB , "A" )
941+ seedVectorSession (t , localDB , "B" )
942+ src := & fakeVectorSource {
943+ gen : VectorGenerationInfo {Fingerprint : "fp-full-race" , Model : "m" , Dimension : 4 },
944+ hasGen : true ,
945+ hashes : map [string ]string {"A" : "a1" , "B" : "b1" },
946+ docs : map [string ][]VectorPushDoc {
947+ "A" : {vdoc ("A" , "A#0" , 0 , "ca1" , "a1" , []float32 {1 , 0 , 0 , 0 })},
948+ "B" : {vdoc ("B" , "B#0" , 0 , "cb1" , "b1" , []float32 {0 , 1 , 0 , 0 })},
949+ },
950+ }
951+ sync .vectorSource = src
952+
953+ _ , err := sync .Push (ctx , false , nil )
954+ require .NoError (t , err , "baseline Push" )
955+
956+ var gen1 int64
957+ require .NoError (t , pg .QueryRow (
958+ `SELECT id FROM vector_generations WHERE fingerprint = $1` , "fp-full-race" ,
959+ ).Scan (& gen1 ), "gen1 id" )
960+ witnessKey , err := sync .vectorGenerationWitnessKey ()
961+ require .NoError (t , err , "vectorGenerationWitnessKey" )
962+
963+ src .genScopes = nil
964+ src .hashScopes = nil
965+ src .hashes = map [string ]string {"A" : "a2" , "B" : "b2" }
966+ src .docs = map [string ][]VectorPushDoc {
967+ "A" : {vdoc ("A" , "A#0" , 0 , "ca2" , "a2" , []float32 {0 , 0 , 1 , 0 })},
968+ "B" : {vdoc ("B" , "B#0" , 0 , "cb2" , "b2" , []float32 {0 , 0 , 0 , 1 })},
969+ }
970+ sync .afterVectorApply = func () {
971+ for _ , q := range []string {
972+ `DROP TABLE IF EXISTS ` + vectorChunkTable (gen1 ),
973+ `DROP TABLE IF EXISTS vector_push_state` ,
974+ `DROP TABLE IF EXISTS vector_generation_machines` ,
975+ `DROP TABLE IF EXISTS vector_documents` ,
976+ `DROP TABLE IF EXISTS vector_generations` ,
977+ } {
978+ _ , err := pg .Exec (q )
979+ require .NoError (t , err , q )
980+ }
981+ unavailable , err := ensureVectorBaseSchemaPG (ctx , pg )
982+ require .NoError (t , err , "recreate vector base schema" )
983+ require .Empty (t , unavailable , "pgvector must stay available in test" )
984+ gen2 , err := ensureVectorGeneration (
985+ ctx , pg , src .gen .Fingerprint , src .gen .Model , src .gen .Dimension ,
986+ )
987+ require .NoError (t , err , "re-register generation" )
988+ require .Equal (t , gen1 , gen2 ,
989+ "recreated tables restart the id sequence, which is the case under test" )
990+ }
991+
992+ vres , err := sync .pushVectors (ctx , false , nil , gen1 , nil , nil )
993+ require .NoError (t , err , "full push across a pre-witness recreation" )
994+ assert .Equal (t , 2 , vres .SessionsPushed ,
995+ "the recreated generation is repopulated generation-wide before success returns" )
996+ require .Len (t , src .genScopes , 2 )
997+ assert .Nil (t , src .genScopes [0 ])
998+ assert .Nil (t , src .genScopes [1 ],
999+ "the retry must reopen the full generation after the recreation" )
1000+ require .Len (t , src .hashScopes , 2 )
1001+ assert .Nil (t , src .hashScopes [0 ])
1002+ assert .Nil (t , src .hashScopes [1 ],
1003+ "both passes are generation-wide reads" )
1004+ assert .Equal (t , gen1 , vres .GenerationID ,
1005+ "the full retry reconciles the recreated generation even when the id is reused" )
1006+ assert .Equal (t , 1 , countRows (t , pg , `
1007+ SELECT COUNT(*) FROM vector_generation_machines
1008+ WHERE generation_id = $1 AND machine = $2` , gen1 , witnessKey ),
1009+ "the final witness is recorded only after the stable retry" )
1010+ assert .Equal (t , 2 , countRows (t , pg , `
1011+ SELECT COUNT(*) FROM vector_push_state WHERE generation_id = $1` , gen1 ),
1012+ "both sessions have state rows under the recreated generation" )
1013+ }
1014+
9311015func TestVectorPushRoundTrip (t * testing.T ) {
9321016 pgURL := testPGURL (t )
9331017 sync , localDB , pg := newVectorPushTestSync (
0 commit comments