@@ -202,10 +202,12 @@ func (c *PTYConversation) Start(ctx context.Context) {
202202
203203 var loadErr string
204204 if c .initialPromptReady && c .loadStateStatus == LoadStatePending && c .cfg .StatePersistenceConfig .LoadState {
205- if err := c .loadStateLocked (); err != nil {
206- c .cfg .Logger .Error ("Failed to load state" , "error" , err )
207- loadErr = fmt .Sprintf ("Failed to restore previous session: %v" , err )
205+ if err , shouldEmit := c .loadStateLocked (); err != nil {
208206 c .loadStateStatus = LoadStateFailed
207+ if shouldEmit {
208+ c .cfg .Logger .Error ("Failed to load state" , "error" , err )
209+ loadErr = fmt .Sprintf ("Failed to restore previous session: %v" , err )
210+ }
209211 } else {
210212 c .loadStateStatus = LoadStateSucceeded
211213 }
@@ -638,25 +640,26 @@ func (c *PTYConversation) SaveState() error {
638640 return nil
639641}
640642
641- // loadStateLocked loads the state, this method assumes that caller holds the Lock
642- func (c * PTYConversation ) loadStateLocked () error {
643+ // loadStateLocked loads the state, this method assumes that caller holds the Lock.
644+ // Returns (error, shouldEmit) where shouldEmit indicates if the error should be emitted to the user.
645+ func (c * PTYConversation ) loadStateLocked () (error , bool ) {
643646 stateFile := c .cfg .StatePersistenceConfig .StateFile
644647 loadState := c .cfg .StatePersistenceConfig .LoadState
645648
646649 if ! loadState || c .loadStateStatus != LoadStatePending {
647- return nil
650+ return nil , false
648651 }
649652
650653 // Check if file exists
651654 if _ , err := os .Stat (stateFile ); os .IsNotExist (err ) {
652655 c .cfg .Logger .Info ("No previous state to load (file does not exist)" , "path" , stateFile )
653- return xerrors .Errorf ("No previous state to load (file does not exist)" )
656+ return xerrors .Errorf ("No previous state to load (file does not exist)" ), false
654657 }
655658
656659 // Open state file
657660 f , err := os .Open (stateFile )
658661 if err != nil {
659- return xerrors .Errorf ("failed to open state file: %w" , err )
662+ return xerrors .Errorf ("failed to open state file: %w" , err ), true
660663 }
661664 defer func () {
662665 if closeErr := f .Close (); closeErr != nil {
@@ -667,12 +670,12 @@ func (c *PTYConversation) loadStateLocked() error {
667670 var agentState AgentState
668671 decoder := json .NewDecoder (f )
669672 if err := decoder .Decode (& agentState ); err != nil {
670- return xerrors .Errorf ("failed to unmarshal state (corrupted or invalid JSON): %w" , err )
673+ return xerrors .Errorf ("failed to unmarshal state (corrupted or invalid JSON): %w" , err ), true
671674 }
672675
673676 // Validate version
674677 if agentState .Version != 1 {
675- return xerrors .Errorf ("unsupported state file version %d (expected 1)" , agentState .Version )
678+ return xerrors .Errorf ("unsupported state file version %d (expected 1)" , agentState .Version ), true
676679 }
677680
678681 // Handle initial prompt restoration:
@@ -699,5 +702,5 @@ func (c *PTYConversation) loadStateLocked() error {
699702 c .dirty = false
700703
701704 c .cfg .Logger .Info ("Successfully loaded state" , "path" , stateFile , "messages" , len (c .messages ))
702- return nil
705+ return nil , false
703706}
0 commit comments