@@ -956,6 +956,52 @@ export function createCodexAppServerBindingStore(
956956 }
957957 } ;
958958
959+ const transitionSessionGeneration = async (
960+ identity : Extract < CodexAppServerBindingIdentity , { kind : "session" } > ,
961+ mode : "reset" | "retire" ,
962+ ) : Promise < CodexSessionGenerationRetirementResult > => {
963+ return await runBindingMutation ( async ( ) => {
964+ const key = bindingStoreKey ( identity ) ;
965+ const ttlMs =
966+ mode === "reset"
967+ ? leaseContext . getStore ( ) ?. has ( key )
968+ ? undefined
969+ : 1
970+ : identity . sessionKey ?. trim ( )
971+ ? undefined
972+ : PHYSICAL_SESSION_RETIRE_TTL_MS ;
973+ return await transactKey (
974+ key ,
975+ ( current , leaseToken ) => {
976+ if ( ! current ) {
977+ return { result : "absent" as const } ;
978+ }
979+ if ( ! ownsStoredSessionGeneration ( identity , current ) ) {
980+ return { result : "conflict" as const } ;
981+ }
982+ // Retirement is idempotent, but reset cannot clear a same-id deletion fence.
983+ // Only the authoritative session-store reclaim path can prove an in-place reset.
984+ if ( current . state === "cleared" && current . retired === true ) {
985+ return { result : mode === "retire" ? ( "applied" as const ) : ( "conflict" as const ) } ;
986+ }
987+ return {
988+ result : "applied" as const ,
989+ next : {
990+ version : 1 ,
991+ state : "cleared" ,
992+ ...( mode === "retire" ? { retired : true as const } : { } ) ,
993+ ...storedSessionGeneration ( identity , current ) ,
994+ ...( current . lease && current . lease . token === leaseToken
995+ ? { lease : current . lease }
996+ : { } ) ,
997+ } ,
998+ } ;
999+ } ,
1000+ ttlMs ,
1001+ ) ;
1002+ } ) ;
1003+ } ;
1004+
9591005 return {
9601006 async read ( identity ) {
9611007 const key = bindingStoreKey ( identity ) ;
@@ -1206,72 +1252,8 @@ export function createCodexAppServerBindingStore(
12061252 } ) ;
12071253 } ,
12081254
1209- async resetSessionGeneration ( identity ) {
1210- return await runBindingMutation ( async ( ) => {
1211- const key = bindingStoreKey ( identity ) ;
1212- return await transactKey (
1213- key ,
1214- ( current , leaseToken ) => {
1215- if ( ! current ) {
1216- return { result : "absent" as const } ;
1217- }
1218- if ( ! ownsStoredSessionGeneration ( identity , current ) ) {
1219- return { result : "conflict" as const } ;
1220- }
1221- // A retired same-id row may be a deletion fence. Only the authoritative
1222- // session-store reclaim path can prove it belongs to an in-place reset.
1223- if ( current . state === "cleared" && current . retired === true ) {
1224- return { result : "conflict" as const } ;
1225- }
1226- return {
1227- result : "applied" as const ,
1228- next : {
1229- version : 1 ,
1230- state : "cleared" ,
1231- ...storedSessionGeneration ( identity , current ) ,
1232- ...( current . lease && current . lease . token === leaseToken
1233- ? { lease : current . lease }
1234- : { } ) ,
1235- } ,
1236- } ;
1237- } ,
1238- leaseContext . getStore ( ) ?. has ( key ) ? undefined : 1 ,
1239- ) ;
1240- } ) ;
1241- } ,
1242-
1243- async retireSessionGeneration ( identity ) {
1244- return await runBindingMutation ( async ( ) => {
1245- const key = bindingStoreKey ( identity ) ;
1246- return await transactKey (
1247- key ,
1248- ( current , leaseToken ) => {
1249- if ( ! current ) {
1250- return { result : "absent" as const } ;
1251- }
1252- if ( ! ownsStoredSessionGeneration ( identity , current ) ) {
1253- return { result : "conflict" as const } ;
1254- }
1255- if ( current . state === "cleared" && current . retired === true ) {
1256- return { result : "applied" as const } ;
1257- }
1258- return {
1259- result : "applied" as const ,
1260- next : {
1261- version : 1 ,
1262- state : "cleared" ,
1263- retired : true ,
1264- ...storedSessionGeneration ( identity , current ) ,
1265- ...( current . lease && current . lease . token === leaseToken
1266- ? { lease : current . lease }
1267- : { } ) ,
1268- } ,
1269- } ;
1270- } ,
1271- identity . sessionKey ?. trim ( ) ? undefined : PHYSICAL_SESSION_RETIRE_TTL_MS ,
1272- ) ;
1273- } ) ;
1274- } ,
1255+ resetSessionGeneration : ( identity ) => transitionSessionGeneration ( identity , "reset" ) ,
1256+ retireSessionGeneration : ( identity ) => transitionSessionGeneration ( identity , "retire" ) ,
12751257
12761258 async withThreadArchiveFence ( run ) {
12771259 pendingArchives += 1 ;
0 commit comments