@@ -14,13 +14,19 @@ import {
1414import { resolveOpenClawStateSqlitePath } from "../../state/openclaw-state-db.paths.js" ;
1515import type { OpenClawConfig } from "../types.openclaw.js" ;
1616import { migrateLegacyMainSessionKeys } from "./legacy-main-session-migration.js" ;
17+ import { assignSessionOwner } from "./session-accessor.js" ;
1718import { readExactSessionEntryRowForCanonicalRepair } from "./session-accessor.sqlite-canonical-repair.js" ;
1819import { writeSessionEntry } from "./session-accessor.sqlite-entry-store.js" ;
1920import { readTranscriptEventRows } from "./session-accessor.sqlite-read.js" ;
2021import { appendTranscriptEventInTransaction } from "./session-accessor.sqlite-transcript-store.js" ;
2122import type { SessionEntry } from "./types.js" ;
2223
2324const tempDirs = useAutoCleanupTempDirTracker ( afterEach ) ;
25+ const humanOwner = {
26+ actor : { type : "human" , id : "alice" } ,
27+ assignedBy : { type : "human" , id : "bob" } ,
28+ assignedAt : 123 ,
29+ } as const ;
2430
2531type Fixture = {
2632 cfg : OpenClawConfig ;
@@ -48,6 +54,19 @@ function databasePath(stateDir: string, agentId: string): string {
4854 return path . join ( stateDir , "agents" , agentId , "agent" , "openclaw-agent.sqlite" ) ;
4955}
5056
57+ function assignHumanOwner ( storePath : string ) : void {
58+ expect (
59+ assignSessionOwner (
60+ { agentId : "main" , sessionKey : "agent:main:chat" , storePath } ,
61+ {
62+ owner : humanOwner . actor ,
63+ assignedBy : humanOwner . assignedBy ,
64+ assignedAt : humanOwner . assignedAt ,
65+ } ,
66+ ) ,
67+ ) . toEqual ( humanOwner ) ;
68+ }
69+
5170function seedClaim ( params : {
5271 databaseAgentId : string ;
5372 databasePath : string ;
@@ -400,6 +419,41 @@ describe("legacy main session migration", () => {
400419 ) . toBeDefined ( ) ;
401420 } ) ;
402421
422+ it . each ( [
423+ { kind : "migrated-in-place" , sharedStore : true } ,
424+ { kind : "migrated-cross-store" , sharedStore : false } ,
425+ ] ) ( "preserves the assigned human owner when $kind" , async ( { kind, sharedStore } ) => {
426+ const storePath = sharedStore
427+ ? path . join ( tempDirs . make ( "owned-in-place-migration-" ) , "sessions.sqlite" )
428+ : undefined ;
429+ const fixture = createFixture ( {
430+ agents : { entries : { ops : { } } } ,
431+ ...( storePath ? { session : { store : storePath } } : { } ) ,
432+ } ) ;
433+ const sourcePath = storePath ?? databasePath ( fixture . stateDir , "main" ) ;
434+ seedClaim ( { databaseAgentId : "main" , databasePath : sourcePath , key : "agent:main:chat" } ) ;
435+ assignHumanOwner ( sourcePath ) ;
436+
437+ const result = await migrateLegacyMainSessionKeys ( {
438+ cfg : fixture . cfg ,
439+ env : fixture . env ,
440+ mode : "automatic" ,
441+ } ) ;
442+
443+ expect ( result . complete ) . toBe ( true ) ;
444+ expect ( outcomeKinds ( result ) ) . toContain ( kind ) ;
445+ expect (
446+ readClaim ( {
447+ databaseAgentId : sharedStore ? "main" : "ops" ,
448+ databasePath : storePath ?? databasePath ( fixture . stateDir , "ops" ) ,
449+ key : "agent:ops:chat" ,
450+ } ) ?. entry . owner ,
451+ ) . toEqual ( humanOwner ) ;
452+ expect (
453+ readClaim ( { databaseAgentId : "main" , databasePath : sourcePath , key : "agent:main:chat" } ) ,
454+ ) . toBeUndefined ( ) ;
455+ } ) ;
456+
403457 it . each ( [
404458 { copiedBeforeCrash : true , label : "copy committed before source cleanup" } ,
405459 { copiedBeforeCrash : false , label : "source cleanup committed before ledger" } ,
@@ -452,47 +506,53 @@ describe("legacy main session migration", () => {
452506 ] ) ;
453507 } ) ;
454508
455- it ( "quarantines losing claims without overwriting existing quarantine keys" , async ( ) => {
456- const fixture = createFixture ( ) ;
457- const mainPath = databasePath ( fixture . stateDir , "main" ) ;
458- seedClaim ( {
459- databaseAgentId : "main" ,
460- databasePath : mainPath ,
461- entry : { sessionId : "legacy" , updatedAt : 100 } ,
462- key : "agent:main:chat" ,
463- } ) ;
464- seedClaim ( {
465- databaseAgentId : "main" ,
466- databasePath : mainPath ,
467- entry : { sessionId : "occupied" , updatedAt : 50 } ,
468- key : "agent:ops:legacy-main-conflict-1" ,
469- } ) ;
470- seedClaim ( {
471- databaseAgentId : "ops" ,
472- databasePath : databasePath ( fixture . stateDir , "ops" ) ,
473- entry : { sessionId : "canonical" , updatedAt : 200 } ,
474- key : "agent:ops:chat" ,
475- } ) ;
509+ it . each ( [ false , true ] ) (
510+ "quarantines losing claims without overwriting existing quarantine keys (assigned owner: %s)" ,
511+ async ( hasHumanOwner ) => {
512+ const fixture = createFixture ( ) ;
513+ const mainPath = databasePath ( fixture . stateDir , "main" ) ;
514+ seedClaim ( {
515+ databaseAgentId : "main" ,
516+ databasePath : mainPath ,
517+ entry : { sessionId : "legacy" , updatedAt : 100 } ,
518+ key : "agent:main:chat" ,
519+ } ) ;
520+ if ( hasHumanOwner ) {
521+ assignHumanOwner ( mainPath ) ;
522+ }
523+ seedClaim ( {
524+ databaseAgentId : "main" ,
525+ databasePath : mainPath ,
526+ entry : { sessionId : "occupied" , updatedAt : 50 } ,
527+ key : "agent:ops:legacy-main-conflict-1" ,
528+ } ) ;
529+ seedClaim ( {
530+ databaseAgentId : "ops" ,
531+ databasePath : databasePath ( fixture . stateDir , "ops" ) ,
532+ entry : { sessionId : "canonical" , updatedAt : 200 } ,
533+ key : "agent:ops:chat" ,
534+ } ) ;
476535
477- const result = await migrateLegacyMainSessionKeys ( {
478- cfg : fixture . cfg ,
479- env : fixture . env ,
480- mode : "doctor-fix" ,
481- } ) ;
536+ const result = await migrateLegacyMainSessionKeys ( {
537+ cfg : fixture . cfg ,
538+ env : fixture . env ,
539+ mode : "doctor-fix" ,
540+ } ) ;
482541
483- const outcome = result . outcomes . find ( ( entry ) => entry . kind === "divergent-canonical" ) ;
484- expect ( outcome ?. quarantinedKeys ) . toEqual ( [ "agent:ops:legacy-main-conflict-2" ] ) ;
485- expect (
486- readClaim ( { databaseAgentId : "main" , databasePath : mainPath , key : "agent:main:chat" } ) ,
487- ) . toBeUndefined ( ) ;
488- expect (
489- readClaim ( {
542+ const outcome = result . outcomes . find ( ( entry ) => entry . kind === "divergent-canonical" ) ;
543+ expect ( outcome ?. quarantinedKeys ) . toEqual ( [ "agent:ops:legacy-main-conflict-2" ] ) ;
544+ expect (
545+ readClaim ( { databaseAgentId : "main" , databasePath : mainPath , key : "agent:main:chat" } ) ,
546+ ) . toBeUndefined ( ) ;
547+ const quarantined = readClaim ( {
490548 databaseAgentId : "main" ,
491549 databasePath : mainPath ,
492550 key : "agent:ops:legacy-main-conflict-2" ,
493- } ) ?. events ,
494- ) . toEqual ( [ '{"type":"message","id":"event-1","text":"hello"}' ] ) ;
495- } ) ;
551+ } ) ;
552+ expect ( quarantined ?. events ) . toEqual ( [ '{"type":"message","id":"event-1","text":"hello"}' ] ) ;
553+ expect ( quarantined ?. entry . owner ) . toEqual ( hasHumanOwner ? humanOwner : undefined ) ;
554+ } ,
555+ ) ;
496556
497557 it ( "uses a completed ledger once and rearms when its identity changes" , async ( ) => {
498558 const fixture = createFixture ( ) ;
0 commit comments