Skip to content

Commit a06c6b8

Browse files
authored
Merge pull request #1362 from trungutt/fix/subsession-persistence
Fix sub-sessions being saved as standalone entries
2 parents 753656f + 91441e1 commit a06c6b8

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

pkg/runtime/runtime.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ func (r *LocalRuntime) RunStream(ctx context.Context, sess *session.Session) <-c
709709
CreatedAt: time.Now().Format(time.RFC3339),
710710
}
711711
sess.AddMessage(session.NewAgentMessage(a, &assistantMessage))
712-
_ = r.sessionStore.UpdateSession(ctx, sess)
712+
r.saveSession(ctx, sess)
713713
return
714714
}
715715
case <-ctx.Done():
@@ -840,7 +840,7 @@ func (r *LocalRuntime) RunStream(ctx context.Context, sess *session.Session) <-c
840840
}
841841

842842
sess.AddMessage(session.NewAgentMessage(a, &assistantMessage))
843-
_ = r.sessionStore.UpdateSession(ctx, sess)
843+
r.saveSession(ctx, sess)
844844
slog.Debug("Added assistant message to session", "agent", a.Name(), "total_messages", len(sess.GetAllMessages()))
845845
} else {
846846
slog.Debug("Skipping empty assistant message (no content and no tool calls)", "agent", a.Name())
@@ -1419,7 +1419,7 @@ func (r *LocalRuntime) executeToolWithHandler(
14191419
CreatedAt: time.Now().Format(time.RFC3339),
14201420
}
14211421
sess.AddMessage(session.NewAgentMessage(a, &toolResponseMsg))
1422-
_ = r.sessionStore.UpdateSession(ctx, sess)
1422+
r.saveSession(ctx, sess)
14231423
}
14241424

14251425
// runTool executes agent tools from toolsets (MCP, filesystem, etc.).
@@ -1510,6 +1510,16 @@ func (r *LocalRuntime) addToolErrorResponse(ctx context.Context, sess *session.S
15101510
CreatedAt: time.Now().Format(time.RFC3339),
15111511
}
15121512
sess.AddMessage(session.NewAgentMessage(a, &toolResponseMsg))
1513+
r.saveSession(ctx, sess)
1514+
}
1515+
1516+
// saveSession persists the session to the store, but only for root sessions.
1517+
// Sub-sessions (those with a ParentID) are not persisted as standalone entries;
1518+
// they are embedded within the parent session's Messages array.
1519+
func (r *LocalRuntime) saveSession(ctx context.Context, sess *session.Session) {
1520+
if sess.IsSubSession() {
1521+
return
1522+
}
15131523
_ = r.sessionStore.UpdateSession(ctx, sess)
15141524
}
15151525

@@ -1587,6 +1597,7 @@ func (r *LocalRuntime) handleTaskTransfer(ctx context.Context, sess *session.Ses
15871597
session.WithTitle("Transferred task"),
15881598
session.WithToolsApproved(sess.ToolsApproved),
15891599
session.WithSendUserMessage(false),
1600+
session.WithParentID(sess.ID),
15901601
)
15911602

15921603
for event := range r.RunStream(ctx, s) {

pkg/session/session.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ type Session struct {
9494
// CustomModelsUsed tracks custom models (provider/model format) used during this session.
9595
// These are shown in the model picker for easy re-selection.
9696
CustomModelsUsed []string `json:"custom_models_used,omitempty"`
97+
98+
// ParentID indicates this is a sub-session created by task transfer.
99+
// Sub-sessions are not persisted as standalone entries; they are embedded
100+
// within the parent session's Messages array.
101+
ParentID string `json:"-"`
97102
}
98103

99104
// Permission mode constants
@@ -357,6 +362,19 @@ func WithPermissions(perms *PermissionsConfig) Opt {
357362
}
358363
}
359364

365+
// WithParentID marks this session as a sub-session of the given parent.
366+
// Sub-sessions are not persisted as standalone entries in the session store.
367+
func WithParentID(parentID string) Opt {
368+
return func(s *Session) {
369+
s.ParentID = parentID
370+
}
371+
}
372+
373+
// IsSubSession returns true if this session is a sub-session (has a parent).
374+
func (s *Session) IsSubSession() bool {
375+
return s.ParentID != ""
376+
}
377+
360378
// New creates a new agent session
361379
func New(opts ...Opt) *Session {
362380
sessionID := uuid.New().String()

0 commit comments

Comments
 (0)