Skip to content

Commit f1ddebb

Browse files
committed
fix(engine): M2 - make AddUser/AddAssistant/ForkConversation/etc nil-safe
The M2 refactor changed these methods to call s.Persistence().DAG() and s.MemorySvc().Memory() directly. That panics on &engine.Session{} struct literals (used by cmd tests) where s.persist and s.memory are nil. This commit wraps each call site in a nil-check on the parent service (s.Persistence() / s.MemorySvc()) so that struct-literal sessions still work the same way as before — the methods become no-ops when their backing sub-service is nil, which matches the previous behavior on s.ConvoDAG == nil and s.Memory == nil.
1 parent 1d06b3d commit f1ddebb

1 file changed

Lines changed: 56 additions & 32 deletions

File tree

‎internal/engine/session.go‎

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -500,24 +500,28 @@ func (s *Session) SetAPIKeys(apiKeys map[string]string) {
500500
}
501501

502502
func (s *Session) AddUser(content string) {
503-
s.Persistence().AddUser(content)
504-
if dag := s.Persistence().DAG(); dag != nil {
505-
parentID := ""
506-
if head, err := dag.Head(context.Background()); err == nil && head != nil {
507-
parentID = head.ID
508-
}
509-
_, _ = dag.Append(context.Background(), parentID, "user", content)
510-
}
511-
if mem := s.MemorySvc().Memory(); mem != nil && strings.Contains(strings.ToLower(content), "remember") {
512-
go func(c string) {
513-
// Use timeout context so goroutine doesn't hang if backend is slow.
514-
rCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
515-
defer cancel()
516-
_ = rCtx // timeout context available if Remember is extended to accept it
517-
if err := mem.Remember(c, "user_explicit"); err != nil {
518-
slog.Warn("background memory remember failed", "error", err)
503+
if p := s.Persistence(); p != nil {
504+
p.AddUser(content)
505+
if dag := p.DAG(); dag != nil {
506+
parentID := ""
507+
if head, err := dag.Head(context.Background()); err == nil && head != nil {
508+
parentID = head.ID
519509
}
520-
}(content)
510+
_, _ = dag.Append(context.Background(), parentID, "user", content)
511+
}
512+
}
513+
if memSvc := s.MemorySvc(); memSvc != nil {
514+
if mem := memSvc.Memory(); mem != nil && strings.Contains(strings.ToLower(content), "remember") {
515+
go func(c string) {
516+
// Use timeout context so goroutine doesn't hang if backend is slow.
517+
rCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
518+
defer cancel()
519+
_ = rCtx // timeout context available if Remember is extended to accept it
520+
if err := mem.Remember(c, "user_explicit"); err != nil {
521+
slog.Warn("background memory remember failed", "error", err)
522+
}
523+
}(content)
524+
}
521525
}
522526
}
523527

@@ -532,30 +536,38 @@ func (s *Session) AddUserWithImage(content string, imageBase64 string, imageType
532536
}
533537
s.messages = append(s.messages, msg)
534538
s.mu.Unlock()
535-
if dag := s.Persistence().DAG(); dag != nil {
536-
parentID := ""
537-
if head, err := dag.Head(context.Background()); err == nil && head != nil {
538-
parentID = head.ID
539+
if p := s.Persistence(); p != nil {
540+
if dag := p.DAG(); dag != nil {
541+
parentID := ""
542+
if head, err := dag.Head(context.Background()); err == nil && head != nil {
543+
parentID = head.ID
544+
}
545+
_, _ = dag.Append(context.Background(), parentID, "user", content+" [image attached]")
539546
}
540-
_, _ = dag.Append(context.Background(), parentID, "user", content+" [image attached]")
541547
}
542548
}
543549

544550
func (s *Session) AddAssistant(content string) {
545-
s.Persistence().AddAssistant(content)
546-
if dag := s.Persistence().DAG(); dag != nil {
547-
parentID := ""
548-
if head, err := dag.Head(context.Background()); err == nil && head != nil {
549-
parentID = head.ID
551+
if p := s.Persistence(); p != nil {
552+
p.AddAssistant(content)
553+
if dag := p.DAG(); dag != nil {
554+
parentID := ""
555+
if head, err := dag.Head(context.Background()); err == nil && head != nil {
556+
parentID = head.ID
557+
}
558+
_, _ = dag.Append(context.Background(), parentID, "assistant", content)
550559
}
551-
_, _ = dag.Append(context.Background(), parentID, "assistant", content)
552560
}
553561
}
554562

555563
// ForkConversation creates a new branch from a specific point in history.
556564
// Returns the fork node ID and the messages up to that point.
557565
func (s *Session) ForkConversation(nodeID string) (string, error) {
558-
dag := s.Persistence().DAG()
566+
p := s.Persistence()
567+
if p == nil {
568+
return "", nil
569+
}
570+
dag := p.DAG()
559571
if dag == nil {
560572
return "", nil
561573
}
@@ -581,7 +593,11 @@ func (s *Session) ForkConversation(nodeID string) (string, error) {
581593

582594
// SwitchBranch navigates to a different branch point and rebuilds messages.
583595
func (s *Session) SwitchBranch(nodeID string) error {
584-
dag := s.Persistence().DAG()
596+
p := s.Persistence()
597+
if p == nil {
598+
return nil
599+
}
600+
dag := p.DAG()
585601
if dag == nil {
586602
return nil
587603
}
@@ -605,7 +621,11 @@ func (s *Session) SwitchBranch(nodeID string) error {
605621

606622
// ListBranches returns child nodes (alternative branches) from a given node.
607623
func (s *Session) ListBranches(nodeID string) ([]*storage.DAGNode, error) {
608-
dag := s.Persistence().DAG()
624+
p := s.Persistence()
625+
if p == nil {
626+
return nil, nil
627+
}
628+
dag := p.DAG()
609629
if dag == nil {
610630
return nil, nil
611631
}
@@ -614,7 +634,11 @@ func (s *Session) ListBranches(nodeID string) ([]*storage.DAGNode, error) {
614634

615635
// ConvoHead returns the current conversation head node ID.
616636
func (s *Session) ConvoHead() string {
617-
dag := s.Persistence().DAG()
637+
p := s.Persistence()
638+
if p == nil {
639+
return ""
640+
}
641+
dag := p.DAG()
618642
if dag == nil {
619643
return ""
620644
}

0 commit comments

Comments
 (0)