@@ -3,12 +3,17 @@ import { randomUUID } from "node:crypto";
33import fs from "node:fs/promises" ;
44import path from "node:path" ;
55import { expectDefined } from "@openclaw/normalization-core" ;
6- import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
6+ import { afterEach , beforeEach , describe , expect , it } from "vitest" ;
7+ import {
8+ upsertSessionEntry ,
9+ type SessionTranscriptRuntimeTarget ,
10+ } from "../config/sessions/session-accessor.js" ;
711import {
812 clearInternalHooks ,
913 registerInternalHook ,
1014 type AgentBootstrapHookContext ,
1115} from "../hooks/internal-hooks.js" ;
16+ import { closeOpenClawAgentDatabasesForTest } from "../state/openclaw-agent-db.js" ;
1217import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js" ;
1318import { makeTempWorkspace } from "../test-helpers/workspace.js" ;
1419import { withEnvAsync } from "../test-utils/env.js" ;
@@ -24,6 +29,7 @@ import {
2429 resolveBootstrapFilesForRun ,
2530 resolveContextInjectionMode ,
2631} from "./bootstrap-files.js" ;
32+ import { SessionManager } from "./sessions/session-manager.js" ;
2733import { resetLegacyWorkspaceStateCheckForTest } from "./workspace-legacy-state.test-support.js" ;
2834import { mergeWorkspaceSetupState } from "./workspace-state-store.js" ;
2935import type { WorkspaceBootstrapFile } from "./workspace.js" ;
@@ -477,199 +483,108 @@ describe("resolveBootstrapContextForRun", () => {
477483
478484describe ( "hasCompletedBootstrapTurn" , ( ) => {
479485 let tmpDir : string ;
486+ let sessionTarget : SessionTranscriptRuntimeTarget ;
487+ let sessionManager : SessionManager ;
480488
481489 beforeEach ( async ( ) => {
482490 tmpDir = await fs . mkdtemp ( path . join ( await fs . realpath ( "/tmp" ) , "openclaw-bootstrap-turn-" ) ) ;
491+ sessionTarget = {
492+ agentId : "main" ,
493+ sessionId : randomUUID ( ) ,
494+ sessionKey : "agent:main:bootstrap-turn" ,
495+ storePath : path . join ( tmpDir , "sessions.json" ) ,
496+ } ;
497+ await upsertSessionEntry ( sessionTarget , {
498+ sessionId : sessionTarget . sessionId ,
499+ updatedAt : Date . now ( ) ,
500+ } ) ;
501+ sessionManager = SessionManager . open ( sessionTarget , tmpDir ) ;
483502 } ) ;
484503
485504 afterEach ( async ( ) => {
486- vi . restoreAllMocks ( ) ;
505+ closeOpenClawAgentDatabasesForTest ( ) ;
487506 await fs . rm ( tmpDir , { recursive : true , force : true } ) ;
488507 } ) ;
489508
490- it ( "returns false when session file does not exist" , async ( ) => {
491- expect ( await hasCompletedBootstrapTurn ( path . join ( tmpDir , "missing.jsonl" ) ) ) . toBe ( false ) ;
509+ it ( "returns false without a complete SQLite transcript identity" , async ( ) => {
510+ expect ( await hasCompletedBootstrapTurn ( ) ) . toBe ( false ) ;
511+ expect ( await hasCompletedBootstrapTurn ( { ...sessionTarget , storePath : undefined } ) ) . toBe ( false ) ;
492512 } ) ;
493513
494- it ( "returns false for SQLite transcript markers" , async ( ) => {
495- expect (
496- await hasCompletedBootstrapTurn ( "sqlite:main:session-1:/tmp/openclaw/sessions.json" ) ,
497- ) . toBe ( false ) ;
514+ it ( "returns false when the SQLite session has no transcript" , async ( ) => {
515+ expect ( await hasCompletedBootstrapTurn ( sessionTarget ) ) . toBe ( false ) ;
498516 } ) ;
499517
500- it ( "returns false for empty session files" , async ( ) => {
501- const sessionFile = path . join ( tmpDir , "empty.jsonl" ) ;
502- await fs . writeFile ( sessionFile , "" , "utf8" ) ;
503- expect ( await hasCompletedBootstrapTurn ( sessionFile ) ) . toBe ( false ) ;
504- } ) ;
518+ it ( "returns false when no full bootstrap marker has been recorded" , async ( ) => {
519+ sessionManager . appendMessage ( { role : "user" , content : "hello" , timestamp : 1 } ) ;
520+ sessionManager . appendCustomEntry ( "openclaw:unrelated" , { timestamp : 2 } ) ;
505521
506- it ( "returns false for header-only session files" , async ( ) => {
507- const sessionFile = path . join ( tmpDir , "header-only.jsonl" ) ;
508- await fs . writeFile ( sessionFile , `${ JSON . stringify ( { type : "session" , id : "s1" } ) } \n` , "utf8" ) ;
509- expect ( await hasCompletedBootstrapTurn ( sessionFile ) ) . toBe ( false ) ;
522+ expect ( await hasCompletedBootstrapTurn ( sessionTarget ) ) . toBe ( false ) ;
510523 } ) ;
511524
512- it ( "returns false when no assistant turn has been flushed yet" , async ( ) => {
513- const sessionFile = path . join ( tmpDir , "user-only.jsonl" ) ;
514- await fs . writeFile (
515- sessionFile ,
516- [
517- JSON . stringify ( { type : "session" , id : "s1" } ) ,
518- JSON . stringify ( { type : "message" , message : { role : "user" , content : "hello" } } ) ,
519- ] . join ( "\n" ) + "\n" ,
520- "utf8" ,
521- ) ;
522- expect ( await hasCompletedBootstrapTurn ( sessionFile ) ) . toBe ( false ) ;
523- } ) ;
525+ it ( "reads a completion marker persisted by the SQLite session manager" , async ( ) => {
526+ sessionManager . appendMessage ( { role : "user" , content : "hello" , timestamp : 1 } ) ;
527+ sessionManager . appendCustomEntry ( FULL_BOOTSTRAP_COMPLETED_CUSTOM_TYPE , { timestamp : 2 } ) ;
524528
525- it ( "returns false for assistant turns without a recorded full bootstrap marker" , async ( ) => {
526- const sessionFile = path . join ( tmpDir , "assistant-no-marker.jsonl" ) ;
527- await fs . writeFile (
528- sessionFile ,
529- [
530- JSON . stringify ( { type : "session" , id : "s1" } ) ,
531- JSON . stringify ( { type : "message" , message : { role : "user" , content : "hello" } } ) ,
532- JSON . stringify ( { type : "message" , message : { role : "assistant" , content : "hi" } } ) ,
533- ] . join ( "\n" ) + "\n" ,
534- "utf8" ,
535- ) ;
536- expect ( await hasCompletedBootstrapTurn ( sessionFile ) ) . toBe ( false ) ;
529+ expect ( await hasCompletedBootstrapTurn ( sessionTarget ) ) . toBe ( true ) ;
537530 } ) ;
538531
539- it ( "returns true when a full bootstrap completion marker exists" , async ( ) => {
540- const sessionFile = path . join ( tmpDir , "full-bootstrap.jsonl" ) ;
541- await fs . writeFile (
542- sessionFile ,
543- [
544- JSON . stringify ( { type : "message" , message : { role : "assistant" , content : "hi" } } ) ,
545- JSON . stringify ( {
546- type : "custom" ,
547- customType : FULL_BOOTSTRAP_COMPLETED_CUSTOM_TYPE ,
548- data : { timestamp : 1 } ,
549- } ) ,
550- ] . join ( "\n" ) + "\n" ,
551- "utf8" ,
552- ) ;
553- expect ( await hasCompletedBootstrapTurn ( sessionFile ) ) . toBe ( true ) ;
554- } ) ;
532+ it ( "invalidates a completion marker after compaction" , async ( ) => {
533+ const firstEntryId = sessionManager . appendMessage ( {
534+ role : "user" ,
535+ content : "hello" ,
536+ timestamp : 1 ,
537+ } ) ;
538+ sessionManager . appendCustomEntry ( FULL_BOOTSTRAP_COMPLETED_CUSTOM_TYPE , { timestamp : 2 } ) ;
539+ sessionManager . appendCompaction ( "trimmed" , firstEntryId , 10 ) ;
555540
556- it ( "returns false when compaction happened after the last assistant turn" , async ( ) => {
557- const sessionFile = path . join ( tmpDir , "post-compaction.jsonl" ) ;
558- await fs . writeFile (
559- sessionFile ,
560- [
561- JSON . stringify ( {
562- type : "custom" ,
563- customType : FULL_BOOTSTRAP_COMPLETED_CUSTOM_TYPE ,
564- data : { timestamp : 1 } ,
565- } ) ,
566- JSON . stringify ( { type : "compaction" , summary : "trimmed" } ) ,
567- ] . join ( "\n" ) + "\n" ,
568- "utf8" ,
569- ) ;
570- expect ( await hasCompletedBootstrapTurn ( sessionFile ) ) . toBe ( false ) ;
541+ expect ( await hasCompletedBootstrapTurn ( sessionTarget ) ) . toBe ( false ) ;
571542 } ) ;
572543
573- it ( "returns true when a later full bootstrap marker happens after compaction" , async ( ) => {
574- const sessionFile = path . join ( tmpDir , "assistant-after-compaction.jsonl" ) ;
575- await fs . writeFile (
576- sessionFile ,
577- [
578- JSON . stringify ( {
579- type : "custom" ,
580- customType : FULL_BOOTSTRAP_COMPLETED_CUSTOM_TYPE ,
581- data : { timestamp : 1 } ,
582- } ) ,
583- JSON . stringify ( { type : "compaction" , summary : "trimmed" } ) ,
584- JSON . stringify ( { type : "message" , message : { role : "user" , content : "new ask" } } ) ,
585- JSON . stringify ( { type : "message" , message : { role : "assistant" , content : "new reply" } } ) ,
586- JSON . stringify ( {
587- type : "custom" ,
588- customType : FULL_BOOTSTRAP_COMPLETED_CUSTOM_TYPE ,
589- data : { timestamp : 2 } ,
590- } ) ,
591- ] . join ( "\n" ) + "\n" ,
592- "utf8" ,
593- ) ;
594- expect ( await hasCompletedBootstrapTurn ( sessionFile ) ) . toBe ( true ) ;
544+ it ( "accepts a newer full bootstrap marker after compaction" , async ( ) => {
545+ const firstEntryId = sessionManager . appendMessage ( {
546+ role : "user" ,
547+ content : "hello" ,
548+ timestamp : 1 ,
549+ } ) ;
550+ sessionManager . appendCustomEntry ( FULL_BOOTSTRAP_COMPLETED_CUSTOM_TYPE , { timestamp : 2 } ) ;
551+ sessionManager . appendCompaction ( "trimmed" , firstEntryId , 10 ) ;
552+ sessionManager . appendCustomEntry ( FULL_BOOTSTRAP_COMPLETED_CUSTOM_TYPE , { timestamp : 3 } ) ;
553+
554+ expect ( await hasCompletedBootstrapTurn ( sessionTarget ) ) . toBe ( true ) ;
595555 } ) ;
596556
597- it ( "ignores malformed JSON lines" , async ( ) => {
598- const sessionFile = path . join ( tmpDir , "malformed.jsonl" ) ;
599- await fs . writeFile (
600- sessionFile ,
601- [
602- "{broken" ,
603- JSON . stringify ( {
604- type : "custom" ,
605- customType : FULL_BOOTSTRAP_COMPLETED_CUSTOM_TYPE ,
606- data : { timestamp : 1 } ,
607- } ) ,
608- ] . join ( "\n" ) + "\n" ,
609- "utf8" ,
610- ) ;
611- expect ( await hasCompletedBootstrapTurn ( sessionFile ) ) . toBe ( true ) ;
557+ it ( "invalidates a completion marker after a session reset" , async ( ) => {
558+ sessionManager . appendMessage ( { role : "user" , content : "hello" , timestamp : 1 } ) ;
559+ sessionManager . appendCustomEntry ( FULL_BOOTSTRAP_COMPLETED_CUSTOM_TYPE , { timestamp : 2 } ) ;
560+ sessionManager . appendResetBoundary ( "reset" ) ;
561+
562+ expect ( await hasCompletedBootstrapTurn ( sessionTarget ) ) . toBe ( false ) ;
612563 } ) ;
613564
614- it ( "finds a recent full bootstrap marker even when the scan starts mid-file" , async ( ) => {
615- const sessionFile = path . join ( tmpDir , "large-prefix.jsonl" ) ;
616- const hugePrefix = "x" . repeat ( 300 * 1024 ) ;
617- await fs . writeFile (
618- sessionFile ,
619- [
620- JSON . stringify ( { type : "message" , message : { role : "user" , content : hugePrefix } } ) ,
621- JSON . stringify ( {
622- type : "custom" ,
623- customType : FULL_BOOTSTRAP_COMPLETED_CUSTOM_TYPE ,
624- data : { timestamp : 1 } ,
625- } ) ,
626- ] . join ( "\n" ) + "\n" ,
627- "utf8" ,
628- ) ;
629- expect ( await hasCompletedBootstrapTurn ( sessionFile ) ) . toBe ( true ) ;
630- } ) ;
631-
632- it ( "finds a recent full bootstrap marker when the tail read returns short" , async ( ) => {
633- const sessionFile = path . join ( tmpDir , "short-read-tail.jsonl" ) ;
634- const lines = [
635- JSON . stringify ( { type : "message" , message : { role : "user" , content : "hello" } } ) ,
636- JSON . stringify ( { type : "message" , message : { role : "assistant" , content : "hi" } } ) ,
637- JSON . stringify ( {
638- type : "custom" ,
639- customType : FULL_BOOTSTRAP_COMPLETED_CUSTOM_TYPE ,
640- data : { timestamp : 1 } ,
641- } ) ,
642- ] ;
643- await fs . writeFile ( sessionFile , `${ lines . join ( "\n" ) } \n` , "utf8" ) ;
644-
645- const realOpen = fs . open . bind ( fs ) ;
646- vi . spyOn ( fs , "open" ) . mockImplementation ( async ( ...args ) => {
647- const handle = await realOpen ( ...args ) ;
648- const realRead = handle . read . bind ( handle ) ;
649- return new Proxy ( handle , {
650- get ( target , prop , receiver ) {
651- if ( prop === "read" ) {
652- return ( buffer : Buffer , offset : number , length : number , position : number ) =>
653- realRead ( buffer , offset , Math . min ( length , 16 ) , position ) ;
654- }
655- return Reflect . get ( target , prop , receiver ) ;
656- } ,
657- } ) ;
658- } ) ;
565+ it ( "accepts a newer full bootstrap marker after a session reset" , async ( ) => {
566+ sessionManager . appendMessage ( { role : "user" , content : "hello" , timestamp : 1 } ) ;
567+ sessionManager . appendCustomEntry ( FULL_BOOTSTRAP_COMPLETED_CUSTOM_TYPE , { timestamp : 2 } ) ;
568+ sessionManager . appendResetBoundary ( "reset" ) ;
569+ sessionManager . appendCustomEntry ( FULL_BOOTSTRAP_COMPLETED_CUSTOM_TYPE , { timestamp : 3 } ) ;
659570
660- expect ( await hasCompletedBootstrapTurn ( sessionFile ) ) . toBe ( true ) ;
571+ expect ( await hasCompletedBootstrapTurn ( sessionTarget ) ) . toBe ( true ) ;
661572 } ) ;
662573
663- it ( "returns false for symbolic links" , async ( ) => {
664- const realFile = path . join ( tmpDir , "real.jsonl" ) ;
665- const linkFile = path . join ( tmpDir , "link.jsonl" ) ;
666- await fs . writeFile (
667- realFile ,
668- `${ JSON . stringify ( { type : "custom" , customType : FULL_BOOTSTRAP_COMPLETED_CUSTOM_TYPE , data : { timestamp : 1 } } ) } \n` ,
669- "utf8" ,
670- ) ;
671- await fs . symlink ( realFile , linkFile ) ;
672- expect ( await hasCompletedBootstrapTurn ( linkFile ) ) . toBe ( false ) ;
574+ it ( "ignores completion markers on an inactive transcript branch" , async ( ) => {
575+ const firstEntryId = sessionManager . appendMessage ( {
576+ role : "user" ,
577+ content : "hello" ,
578+ timestamp : 1 ,
579+ } ) ;
580+ sessionManager . appendCustomEntry ( FULL_BOOTSTRAP_COMPLETED_CUSTOM_TYPE , { timestamp : 2 } ) ;
581+ expect ( await hasCompletedBootstrapTurn ( sessionTarget ) ) . toBe ( true ) ;
582+
583+ sessionManager . appendLeafControl ( {
584+ targetId : firstEntryId ,
585+ appendParentId : firstEntryId ,
586+ } ) ;
587+ expect ( await hasCompletedBootstrapTurn ( sessionTarget ) ) . toBe ( false ) ;
673588 } ) ;
674589} ) ;
675590
0 commit comments