@@ -27,9 +27,11 @@ import type {
2727 SessionLifecycleArtifactCleanupParams ,
2828 SessionLifecycleArtifactCleanupResult ,
2929} from "./session-accessor.sqlite-contract.js" ;
30- import { sqliteSessionEntriesEqual } from "./session-accessor.sqlite-entry-equality.js" ;
3130import {
32- assertLifecycleTargetSnapshotUnchanged ,
31+ sqliteLifecycleTargetSnapshotsEqual ,
32+ sqliteSessionEntriesEqual ,
33+ } from "./session-accessor.sqlite-entry-equality.js" ;
34+ import {
3335 assertLifecycleTargetUnchanged ,
3436 deleteLifecycleTargetRows ,
3537 deleteLegacySessionEntryRows ,
@@ -296,6 +298,8 @@ async function deleteSqliteSessionEntryLifecycleInternal(
296298 }
297299}
298300
301+ const DELETE_EXPECTED_ENTRY_MISMATCH = Symbol ( "delete-expected-entry-mismatch" ) ;
302+
299303async function deleteSqliteSessionEntryLifecycleLocked (
300304 resolved : ReturnType < typeof resolveSqliteStoreScope > ,
301305 params : DeleteSessionEntryLifecycleParams ,
@@ -310,7 +314,7 @@ async function deleteSqliteSessionEntryLifecycleLocked(
310314 return null ;
311315 }
312316 if ( ! shouldDeleteSqliteSessionEntryLifecycle ( database , current . entry , params ) ) {
313- return null ;
317+ return DELETE_EXPECTED_ENTRY_MISMATCH ;
314318 }
315319 if ( current . entry . modelSelectionLocked === true && ! allowLockedEntryRemoval ) {
316320 throw new Error ( MODEL_SELECTION_LOCK_REMOVAL_MESSAGE ) ;
@@ -383,18 +387,21 @@ async function deleteSqliteSessionEntryLifecycleLocked(
383387 await publishSessionStateArchives ( resolved , [ ] ) ;
384388 return { archivedTranscripts : [ ] , deleted : false } ;
385389 }
390+ if ( prepared === DELETE_EXPECTED_ENTRY_MISMATCH ) {
391+ await publishSessionStateArchives ( resolved , [ ] ) ;
392+ return expectedEntryMismatchResult ( [ ] ) ;
393+ }
386394
387395 const historicalArchivedTranscripts : SessionLifecycleArchivedTranscript [ ] = [ ] ;
388396 for ( const sessionId of prepared . historicalGenerationIds ) {
389397 const plan = await runExclusiveSqliteSessionWrite ( resolved , async ( ) => {
390398 const database = openOpenClawAgentDatabase ( toDatabaseOptions ( resolved ) ) ;
391- assertLifecycleTargetSnapshotUnchanged (
392- prepared . targetSnapshot ,
393- readLifecycleTargetSnapshot ( database , params . target ) ,
394- "delete session entry" ,
395- ) ;
396- if ( ! shouldDeleteSqliteSessionEntryLifecycle ( database , prepared . current . entry , params ) ) {
397- return null ;
399+ const targetSnapshot = readLifecycleTargetSnapshot ( database , params . target ) ;
400+ if (
401+ ! sqliteLifecycleTargetSnapshotsEqual ( prepared . targetSnapshot , targetSnapshot ) ||
402+ ! shouldDeleteSqliteSessionEntryLifecycle ( database , targetSnapshot . primary ?. entry , params )
403+ ) {
404+ return DELETE_EXPECTED_ENTRY_MISMATCH ;
398405 }
399406 const referencedAfterDelete = readReferencedSessionIdsAfterTargetMutation (
400407 database ,
@@ -421,22 +428,25 @@ async function deleteSqliteSessionEntryLifecycleLocked(
421428 sessionId,
422429 } ) ;
423430 } ) ;
431+ if ( plan === DELETE_EXPECTED_ENTRY_MISMATCH ) {
432+ return expectedEntryMismatchResult ( historicalArchivedTranscripts ) ;
433+ }
424434 if ( ! plan ) {
425435 continue ;
426436 }
427437 const materializedGeneration = await materializeSessionStateDeletePlans ( [ plan ] ) ;
428- const archivedGeneration = await runExclusiveSqliteSessionWrite ( resolved , async ( ) => {
429- let committed : SessionLifecycleArchivedTranscript [ ] = [ ] ;
438+ const archivedGeneration = await runExclusiveSqliteSessionWrite ( resolved , async ( ) =>
430439 runOpenClawAgentWriteTransaction ( ( transactionDb ) => {
431- assertLifecycleTargetSnapshotUnchanged (
432- prepared . targetSnapshot ,
433- readLifecycleTargetSnapshot ( transactionDb , params . target ) ,
434- "delete session entry" ,
435- ) ;
440+ const targetSnapshot = readLifecycleTargetSnapshot ( transactionDb , params . target ) ;
436441 if (
437- ! shouldDeleteSqliteSessionEntryLifecycle ( transactionDb , prepared . current . entry , params )
442+ ! sqliteLifecycleTargetSnapshotsEqual ( prepared . targetSnapshot , targetSnapshot ) ||
443+ ! shouldDeleteSqliteSessionEntryLifecycle (
444+ transactionDb ,
445+ targetSnapshot . primary ?. entry ,
446+ params ,
447+ )
438448 ) {
439- return ;
449+ return DELETE_EXPECTED_ENTRY_MISMATCH ;
440450 }
441451 const fenceAtDelete = collectAdmissionProtectedSessionIds ( {
442452 database : transactionDb ,
@@ -447,10 +457,12 @@ async function deleteSqliteSessionEntryLifecycleLocked(
447457 `cannot delete session history while work is in flight for ${ sessionId } ; retry after the run completes` ,
448458 ) ;
449459 }
450- committed = deleteMaterializedSessionStatePlans ( transactionDb , materializedGeneration ) ;
451- } , toDatabaseOptions ( resolved ) ) ;
452- return committed ;
453- } ) ;
460+ return deleteMaterializedSessionStatePlans ( transactionDb , materializedGeneration ) ;
461+ } , toDatabaseOptions ( resolved ) ) ,
462+ ) ;
463+ if ( archivedGeneration === DELETE_EXPECTED_ENTRY_MISMATCH ) {
464+ return expectedEntryMismatchResult ( historicalArchivedTranscripts ) ;
465+ }
454466 // Publish each committed generation immediately: a later archive or
455467 // transaction failure aborts the deletion, and observers must still see
456468 // the removals that already happened (retry completes the remainder).
@@ -462,21 +474,15 @@ async function deleteSqliteSessionEntryLifecycleLocked(
462474 // Archive materialization is the expensive phase. It must run between short
463475 // writer-lane sections so unrelated writes to this store can keep progressing.
464476 const materializedPlans = await materializeSessionStateDeletePlans ( prepared . entryPlans ) ;
465- const result = await runExclusiveSqliteSessionWrite ( resolved , async ( ) => {
466- let committed : DeleteSessionEntryLifecycleResult = {
467- archivedTranscripts : [ ] ,
468- deleted : false ,
469- } ;
470- runOpenClawAgentWriteTransaction ( ( transactionDb ) => {
477+ const result = await runExclusiveSqliteSessionWrite ( resolved , async ( ) =>
478+ runOpenClawAgentWriteTransaction < DeleteSessionEntryLifecycleResult > ( ( transactionDb ) => {
471479 const transactionSnapshot = readLifecycleTargetSnapshot ( transactionDb , params . target ) ;
472- assertLifecycleTargetSnapshotUnchanged (
473- prepared . targetSnapshot ,
474- transactionSnapshot ,
475- "delete session entry" ,
476- ) ;
477480 const transactionEntry = transactionSnapshot . primary ?. entry ;
478- if ( ! shouldDeleteSqliteSessionEntryLifecycle ( transactionDb , transactionEntry , params ) ) {
479- return ;
481+ if (
482+ ! sqliteLifecycleTargetSnapshotsEqual ( prepared . targetSnapshot , transactionSnapshot ) ||
483+ ! shouldDeleteSqliteSessionEntryLifecycle ( transactionDb , transactionEntry , params )
484+ ) {
485+ return expectedEntryMismatchResult ( [ ] ) ;
480486 }
481487 const archivedTranscripts = deleteMaterializedSessionStatePlans (
482488 transactionDb ,
@@ -494,17 +500,16 @@ async function deleteSqliteSessionEntryLifecycleLocked(
494500 ...params . target . storeKeys ,
495501 ...transactionSnapshot . rows . map ( ( row ) => row . sessionKey ) ,
496502 ] ) ;
497- committed = {
503+ return {
498504 archivedTranscripts,
499505 deleted : true ,
500506 deletedEntry : cloneSessionEntry ( prepared . current . entry ) ,
501507 ...( prepared . current . entry . sessionId
502508 ? { deletedSessionId : prepared . current . entry . sessionId }
503509 : { } ) ,
504510 } ;
505- } , toDatabaseOptions ( resolved ) ) ;
506- return committed ;
507- } ) ;
511+ } , toDatabaseOptions ( resolved ) ) ,
512+ ) ;
508513 if ( result . deleted ) {
509514 emitSessionIdentityMutation ( {
510515 kind : "delete" ,
@@ -527,6 +532,12 @@ async function deleteSqliteSessionEntryLifecycleLocked(
527532 return result ;
528533}
529534
535+ function expectedEntryMismatchResult (
536+ archivedTranscripts : SessionLifecycleArchivedTranscript [ ] ,
537+ ) : DeleteSessionEntryLifecycleResult {
538+ return { archivedTranscripts, deleted : false , expectedEntryMismatch : true } ;
539+ }
540+
530541/** Deletes one persisted session entry using SQLite session rows. */
531542export async function deleteSessionEntryLifecycle (
532543 params : DeleteSessionEntryLifecycleParams ,
0 commit comments