forked from docker/docker-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.go
More file actions
1029 lines (877 loc) · 32.4 KB
/
session.go
File metadata and controls
1029 lines (877 loc) · 32.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package session
import (
"bytes"
"encoding/json"
"log/slog"
"os"
"slices"
"strings"
"sync"
"time"
"github.com/google/uuid"
"github.com/docker/docker-agent/pkg/agent"
"github.com/docker/docker-agent/pkg/chat"
"github.com/docker/docker-agent/pkg/tools"
)
const (
// DefaultMaxOldToolCallTokens is the default maximum number of tokens to keep from tool call
// arguments and results. Older tool calls beyond this budget will have their
// content replaced with a placeholder. Tokens are approximated as len/4.
DefaultMaxOldToolCallTokens = 40000
// toolContentPlaceholder is the text used to replace truncated tool content
toolContentPlaceholder = "[content truncated]"
)
// Item represents either a message or a sub-session
type Item struct {
// Message holds a regular conversation message
Message *Message `json:"message,omitempty"`
// SubSession holds a complete sub-session from task transfers
SubSession *Session `json:"sub_session,omitempty"`
// Summary is a summary of the session up until this point
Summary string `json:"summary,omitempty"`
// FirstKeptEntry is the index (into the session's Messages slice) of the
// first message that was kept verbatim during compaction. Messages from
// this index onward (up to the summary item itself) are appended after
// the summary when reconstructing the conversation. A value of -1 (or 0
// with no summary) means no messages were kept.
FirstKeptEntry int `json:"first_kept_entry,omitempty"`
// Cost tracks the cost of operations associated with this item that
// don't produce a regular message (e.g., compaction/summarization).
Cost float64 `json:"cost,omitempty"`
}
// IsMessage returns true if this item contains a message
func (si *Item) IsMessage() bool {
return si.Message != nil
}
// IsSubSession returns true if this item contains a sub-session
func (si *Item) IsSubSession() bool {
return si.SubSession != nil
}
// Session represents the agent's state including conversation history and variables
type Session struct {
// mu protects Messages from concurrent read/write access.
mu sync.RWMutex `json:"-"`
// ID is the unique identifier for the session
ID string `json:"id"`
// Title is the title of the session, set by the runtime
Title string `json:"title"`
// Evals contains evaluation criteria for this session (used by eval framework)
Evals *EvalCriteria `json:"evals,omitempty"`
// EvalResult contains the evaluation scoring outcome (populated after eval run).
EvalResult *EvalResult `json:"eval_result,omitempty"`
// Messages holds the conversation history (messages and sub-sessions)
Messages []Item `json:"messages"`
// CreatedAt is the time the session was created
CreatedAt time.Time `json:"created_at"`
// ToolsApproved is a flag to indicate if the tools have been approved
ToolsApproved bool `json:"tools_approved"`
// HideToolResults is a flag to indicate if tool results should be hidden
HideToolResults bool `json:"hide_tool_results"`
// WorkingDir is the base directory used for filesystem-aware tools
WorkingDir string `json:"working_dir,omitempty"`
// SendUserMessage is a flag to indicate if the user message should be sent
SendUserMessage bool
// MaxIterations is the maximum number of agentic loop iterations to prevent infinite loops
// If 0, there is no limit
MaxIterations int `json:"max_iterations"`
// MaxConsecutiveToolCalls is the maximum number of consecutive identical tool call
// batches before the agent is terminated. Prevents degenerate loops where the model
// repeatedly issues the same call without making progress. Default: 5.
MaxConsecutiveToolCalls int `json:"max_consecutive_tool_calls,omitempty"`
// MaxOldToolCallTokens is the maximum number of tokens to keep from old tool call
// arguments and results. Older tool calls beyond this budget will have their
// content replaced with a placeholder. Tokens are approximated as len/4.
// Set to -1 to disable truncation (unlimited tool content).
// Default: 40000 (when not configured or set to 0).
MaxOldToolCallTokens int `json:"max_old_tool_call_tokens,omitempty"`
// Starred indicates if this session has been starred by the user
Starred bool `json:"starred"`
InputTokens int64 `json:"input_tokens"`
OutputTokens int64 `json:"output_tokens"`
Cost float64 `json:"cost"`
// Permissions holds session-level permission overrides.
// When set, these are evaluated before team-level permissions.
Permissions *PermissionsConfig `json:"permissions,omitempty"`
// AgentModelOverrides stores per-agent model overrides for this session.
// Key is the agent name, value is the model reference (e.g., "openai/gpt-4o" or a named model from config).
// When a session is loaded, these overrides are reapplied to the runtime.
AgentModelOverrides map[string]string `json:"agent_model_overrides,omitempty"`
// CustomModelsUsed tracks custom models (provider/model format) used during this session.
// These are shown in the model picker for easy re-selection.
CustomModelsUsed []string `json:"custom_models_used,omitempty"`
// ExcludedTools lists tool names that should be filtered out of the agent's
// tool list for this session. This is used by skill sub-sessions to prevent
// recursive run_skill calls.
ExcludedTools []string `json:"-"`
// AgentName, when set, tells RunStream which agent to use for this session
// instead of reading from the shared runtime currentAgent field. This is
// required for background agent tasks where multiple sessions may run
// concurrently on different agents.
AgentName string `json:"-"`
// ParentID indicates this is a sub-session created by task transfer.
// Sub-sessions are not persisted as standalone entries; they are embedded
// within the parent session's Messages array.
ParentID string `json:"-"`
// MessageUsageHistory stores per-message usage data for remote mode.
// In remote mode, messages are managed server-side, so we track usage separately.
// This is not persisted (json:"-") as it's only needed for the current session display.
MessageUsageHistory []MessageUsageRecord `json:"-"`
}
// MessageUsageRecord stores usage data for a single assistant message.
// Used in remote mode where messages aren't stored in the client-side session.
type MessageUsageRecord struct {
AgentName string `json:"agent_name"`
Model string `json:"model"`
Cost float64 `json:"cost"`
Usage chat.Usage `json:"usage"`
}
// PermissionsConfig defines session-level tool permission overrides
// using pattern-based rules (Allow/Ask/Deny arrays).
type PermissionsConfig struct {
// Allow lists tool name patterns that are auto-approved without user confirmation.
Allow []string `json:"allow,omitempty"`
// Ask lists tool name patterns that always require user confirmation,
// even for tools that are normally auto-approved (e.g. read-only tools).
Ask []string `json:"ask,omitempty"`
// Deny lists tool name patterns that are always rejected.
Deny []string `json:"deny,omitempty"`
}
// Message is a message from an agent
type Message struct {
// ID is the database ID of the message (used for persistence tracking)
ID int64 `json:"-"`
AgentName string `json:"agentName"` // TODO: rename to agent_name
Message chat.Message `json:"message"`
// Implicit is an optional field to indicate if the message shouldn't be shown to the user. It's needed for special situations
// like when an agent transfers a task to another agent - new session is created with a default user message, but this shouldn't be shown to the user.
// Such messages should be marked as true
Implicit bool `json:"implicit,omitempty"`
}
func ImplicitUserMessage(content string) *Message {
msg := UserMessage(content)
msg.Implicit = true
return msg
}
func UserMessage(content string, multiContent ...chat.MessagePart) *Message {
return &Message{
Message: chat.Message{
Role: chat.MessageRoleUser,
Content: content,
MultiContent: multiContent,
CreatedAt: time.Now().Format(time.RFC3339),
},
}
}
func NewAgentMessage(agentName string, message *chat.Message) *Message {
return &Message{
AgentName: agentName,
Message: *message,
}
}
func SystemMessage(content string) *Message {
return &Message{
Message: chat.Message{
Role: chat.MessageRoleSystem,
Content: content,
CreatedAt: time.Now().Format(time.RFC3339),
},
}
}
// Helper functions for creating SessionItems
// NewMessageItem creates a SessionItem containing a message
func NewMessageItem(msg *Message) Item {
return Item{Message: msg}
}
// NewSubSessionItem creates a SessionItem containing a sub-session
func NewSubSessionItem(subSession *Session) Item {
return Item{SubSession: subSession}
}
// EvalResult contains the evaluation scoring outcome for a session.
type EvalResult struct {
Passed bool `json:"passed"`
Successes []string `json:"successes,omitempty"`
Failures []string `json:"failures,omitempty"`
Error string `json:"error,omitempty"`
Cost float64 `json:"cost"`
OutputTokens int64 `json:"output_tokens"`
Checks EvalResultChecks `json:"checks"`
}
// EvalResultChecks groups the individual check results.
// Only checks that were evaluated will be present (omitted if nil).
type EvalResultChecks struct {
Size *SizeCheck `json:"size,omitempty"`
ToolCalls *ToolCallsCheck `json:"tool_calls,omitempty"`
Relevance *RelevanceCheck `json:"relevance,omitempty"`
}
// SizeCheck contains the result of the response size check.
type SizeCheck struct {
Passed bool `json:"passed"`
Actual string `json:"actual"`
Expected string `json:"expected"`
}
// ToolCallsCheck contains the result of the tool calls F1 score check.
type ToolCallsCheck struct {
Passed bool `json:"passed"`
Score float64 `json:"score"`
}
// RelevanceCheck contains the result of the LLM judge relevance check.
type RelevanceCheck struct {
Passed bool `json:"passed"`
PassedCount float64 `json:"passed_count"`
Total float64 `json:"total"`
Results []RelevanceCriterionResult `json:"results"`
}
// RelevanceCriterionResult contains the judge's verdict on a single relevance criterion.
type RelevanceCriterionResult struct {
Criterion string `json:"criterion"`
Passed bool `json:"passed"`
Reason string `json:"reason,omitempty"`
}
// EvalCriteria contains the evaluation criteria for a session.
type EvalCriteria struct {
Relevance []string `json:"relevance"` // Statements that should be true about the response
WorkingDir string `json:"working_dir,omitempty"` // Subdirectory under evals/working_dirs/
Size string `json:"size,omitempty"` // Expected response size: S, M, L, XL
Setup string `json:"setup,omitempty"` // Optional sh script to run in the container before docker agent run --exec
Image string `json:"image,omitempty"` // Custom Docker image for this eval (overrides --base-image)
}
// UnmarshalJSON implements custom JSON unmarshaling for EvalCriteria that
// rejects unknown fields. This ensures eval JSON files don't contain typos
// or unsupported fields that would be silently ignored.
func (e *EvalCriteria) UnmarshalJSON(data []byte) error {
type evalCriteria EvalCriteria // alias to avoid infinite recursion
var v evalCriteria
dec := json.NewDecoder(bytes.NewReader(data))
dec.DisallowUnknownFields()
if err := dec.Decode(&v); err != nil {
return err
}
*e = EvalCriteria(v)
return nil
}
// deepCopyMessage returns a deep copy of a session Message.
// It copies the inner chat.Message's slice and pointer fields so that the
// returned value shares no mutable state with the original.
func deepCopyMessage(m *Message) *Message {
cp := *m
cp.Message = deepCopyChatMessage(m.Message)
return &cp
}
// deepCopyChatMessage returns a deep copy of a chat.Message, duplicating
// all slice and pointer fields that would otherwise alias the original.
func deepCopyChatMessage(m chat.Message) chat.Message {
if m.MultiContent != nil {
orig := m.MultiContent
m.MultiContent = make([]chat.MessagePart, len(orig))
for i, part := range orig {
if part.ImageURL != nil {
imgCopy := *part.ImageURL
part.ImageURL = &imgCopy
}
if part.File != nil {
fileCopy := *part.File
part.File = &fileCopy
}
m.MultiContent[i] = part
}
}
if m.FunctionCall != nil {
fcCopy := *m.FunctionCall
m.FunctionCall = &fcCopy
}
if m.ToolCalls != nil {
m.ToolCalls = slices.Clone(m.ToolCalls)
}
if m.ToolDefinitions != nil {
m.ToolDefinitions = slices.Clone(m.ToolDefinitions)
}
if m.Usage != nil {
usageCopy := *m.Usage
m.Usage = &usageCopy
}
if m.ThoughtSignature != nil {
m.ThoughtSignature = slices.Clone(m.ThoughtSignature)
}
return m
}
// Session helper methods
// AddMessage adds a message to the session
func (s *Session) AddMessage(msg *Message) {
s.mu.Lock()
s.Messages = append(s.Messages, NewMessageItem(msg))
s.mu.Unlock()
}
// AddSubSession adds a sub-session to the session
func (s *Session) AddSubSession(subSession *Session) {
s.mu.Lock()
s.Messages = append(s.Messages, NewSubSessionItem(subSession))
s.mu.Unlock()
}
// Duration calculates the duration of the session from message timestamps.
func (s *Session) Duration() time.Duration {
messages := s.GetAllMessages()
if len(messages) < 2 {
return 0
}
first, err := time.Parse(time.RFC3339, messages[0].Message.CreatedAt)
if err != nil {
return 0
}
last, err := time.Parse(time.RFC3339, messages[len(messages)-1].Message.CreatedAt)
if err != nil {
return 0
}
return last.Sub(first)
}
// AllowedDirectories returns the directories that should be considered safe for tools
func (s *Session) AllowedDirectories() []string {
if s.WorkingDir == "" {
return nil
}
return []string{s.WorkingDir}
}
// GetAllMessages extracts all messages from the session, including from sub-sessions
func (s *Session) GetAllMessages() []Message {
s.mu.RLock()
items := make([]Item, len(s.Messages))
for i, item := range s.Messages {
if item.Message != nil {
items[i] = Item{Message: deepCopyMessage(item.Message)}
} else {
items[i] = item
}
}
s.mu.RUnlock()
var messages []Message
for _, item := range items {
if item.IsMessage() && item.Message.Message.Role != chat.MessageRoleSystem {
messages = append(messages, *item.Message)
} else if item.IsSubSession() {
// Recursively get messages from sub-sessions
subMessages := item.SubSession.GetAllMessages()
messages = append(messages, subMessages...)
}
}
return messages
}
func (s *Session) GetLastAssistantMessageContent() string {
return s.getLastMessageContentByRole(chat.MessageRoleAssistant)
}
func (s *Session) GetLastUserMessageContent() string {
return s.getLastMessageContentByRole(chat.MessageRoleUser)
}
// GetLastUserMessages returns up to n most recent user messages, ordered from oldest to newest.
// Returns nil if n <= 0.
func (s *Session) GetLastUserMessages(n int) []string {
if n <= 0 {
return nil
}
messages := s.GetAllMessages()
var userMessages []string
for i := range messages {
if messages[i].Message.Role == chat.MessageRoleUser {
content := strings.TrimSpace(messages[i].Message.Content)
if content != "" {
userMessages = append(userMessages, content)
}
}
}
if len(userMessages) <= n {
return userMessages
}
return userMessages[len(userMessages)-n:]
}
func (s *Session) getLastMessageContentByRole(role chat.MessageRole) string {
messages := s.GetAllMessages()
for i := len(messages) - 1; i >= 0; i-- {
if messages[i].Message.Role == role {
return strings.TrimSpace(messages[i].Message.Content)
}
}
return ""
}
// AddMessageUsageRecord appends a usage record for remote mode where messages aren't stored locally.
// This enables the /cost dialog to show per-message breakdown even when using a remote runtime.
func (s *Session) AddMessageUsageRecord(agentName, model string, cost float64, usage *chat.Usage) {
if usage == nil {
return
}
s.MessageUsageHistory = append(s.MessageUsageHistory, MessageUsageRecord{
AgentName: agentName,
Model: model,
Cost: cost,
Usage: *usage,
})
}
type Opt func(s *Session)
func WithUserMessage(content string) Opt {
return func(s *Session) {
s.AddMessage(UserMessage(content))
}
}
func WithImplicitUserMessage(content string) Opt {
return func(s *Session) {
s.AddMessage(ImplicitUserMessage(content))
}
}
func WithSystemMessage(content string) Opt {
return func(s *Session) {
s.AddMessage(SystemMessage(content))
}
}
func WithMaxIterations(maxIterations int) Opt {
return func(s *Session) {
s.MaxIterations = maxIterations
}
}
// WithMaxConsecutiveToolCalls sets the threshold for consecutive identical tool
// call detection. 0 means "use runtime default of 5". Negative values are
// ignored.
func WithMaxConsecutiveToolCalls(n int) Opt {
return func(s *Session) {
if n >= 0 {
s.MaxConsecutiveToolCalls = n
}
}
}
// WithMaxOldToolCallTokens sets the maximum token budget for old tool call content.
// Set to -1 to disable truncation (unlimited tool content).
// Set to 0 to use the default (40000).
func WithMaxOldToolCallTokens(n int) Opt {
return func(s *Session) {
s.MaxOldToolCallTokens = n
}
}
func WithWorkingDir(workingDir string) Opt {
return func(s *Session) {
s.WorkingDir = workingDir
}
}
func WithTitle(title string) Opt {
return func(s *Session) {
s.Title = title
}
}
func WithMessages(messages []Item) Opt {
return func(s *Session) {
s.Messages = messages
}
}
func WithToolsApproved(toolsApproved bool) Opt {
return func(s *Session) {
s.ToolsApproved = toolsApproved
}
}
func WithHideToolResults(hideToolResults bool) Opt {
return func(s *Session) {
s.HideToolResults = hideToolResults
}
}
func WithSendUserMessage(sendUserMessage bool) Opt {
return func(s *Session) {
s.SendUserMessage = sendUserMessage
}
}
func WithPermissions(perms *PermissionsConfig) Opt {
return func(s *Session) {
s.Permissions = perms
}
}
// WithAgentName pins this session to a specific agent. When set, RunStream
// resolves the agent from the session rather than the shared runtime state,
// which is required for concurrent background agent tasks.
func WithAgentName(name string) Opt {
return func(s *Session) {
s.AgentName = name
}
}
// WithParentID marks this session as a sub-session of the given parent.
// Sub-sessions are not persisted as standalone entries in the session store.
func WithParentID(parentID string) Opt {
return func(s *Session) {
s.ParentID = parentID
}
}
// WithID sets the session ID. If not set, a UUID will be generated.
func WithID(id string) Opt {
return func(s *Session) {
s.ID = id
}
}
// WithExcludedTools sets tool names that should be filtered out of the agent's
// tool list for this session. This prevents recursive tool calls in skill
// sub-sessions.
func WithExcludedTools(names []string) Opt {
return func(s *Session) {
s.ExcludedTools = names
}
}
// IsSubSession returns true if this session is a sub-session (has a parent).
func (s *Session) IsSubSession() bool {
return s.ParentID != ""
}
// MessageCount returns the number of items that contain a message.
func (s *Session) MessageCount() int {
s.mu.RLock()
defer s.mu.RUnlock()
n := 0
for _, item := range s.Messages {
if item.IsMessage() {
n++
}
}
return n
}
// TotalCost computes the total cost of a session by walking all messages,
// sub-sessions, and summary items. It does not use the session-level Cost
// field, which exists only for backward-compatible persistence.
func (s *Session) TotalCost() float64 {
s.mu.RLock()
defer s.mu.RUnlock()
var cost float64
for _, item := range s.Messages {
switch {
case item.IsMessage():
cost += item.Message.Message.Cost
case item.IsSubSession():
cost += item.SubSession.TotalCost()
}
cost += item.Cost
}
return cost
}
// OwnCost returns only this session's direct cost: its own messages and
// item-level costs (e.g. compaction). It excludes sub-session costs.
// This is used for live event emissions where sub-sessions report their
// own costs separately.
func (s *Session) OwnCost() float64 {
s.mu.RLock()
defer s.mu.RUnlock()
var cost float64
for _, item := range s.Messages {
if item.IsMessage() {
cost += item.Message.Message.Cost
}
cost += item.Cost
}
return cost
}
// New creates a new agent session
func New(opts ...Opt) *Session {
s := &Session{
ID: uuid.New().String(),
CreatedAt: time.Now(),
SendUserMessage: true,
}
for _, opt := range opts {
opt(s)
}
slog.Debug("Creating new session", "session_id", s.ID)
return s
}
func markLastMessageAsCacheControl(messages []chat.Message) {
if len(messages) > 0 {
messages[len(messages)-1].CacheControl = true
}
}
// buildInvariantSystemMessages builds system messages that are identical
// for all users of a given agent configuration. These messages can be
// cached efficiently as they don't change between sessions, users, or projects.
//
// These messages are determined solely by the agent configuration and
// remain constant across different sessions, users, and working directories.
func buildInvariantSystemMessages(a *agent.Agent) []chat.Message {
var messages []chat.Message
if a.HasSubAgents() {
subAgents := a.SubAgents()
var text strings.Builder
var validAgentIDs []string
for _, subAgent := range subAgents {
text.WriteString("Name: ")
text.WriteString(subAgent.Name())
text.WriteString(" | Description: ")
text.WriteString(subAgent.Description())
text.WriteString("\n")
validAgentIDs = append(validAgentIDs, subAgent.Name())
}
messages = append(messages, chat.Message{
Role: chat.MessageRoleSystem,
Content: "You are a multi-agent system, make sure to answer the user query in the most helpful way possible. You have access to these sub-agents:\n" + text.String() + "\nIMPORTANT: You can ONLY transfer tasks to the agents listed above using their ID. The valid agent names are: " + strings.Join(validAgentIDs, ", ") + ". You MUST NOT attempt to transfer to any other agent IDs - doing so will cause system errors.\n\nIf you are the best to answer the question according to your description, you can answer it.\n\nIf another agent is better for answering the question according to its description, call `transfer_task` function to transfer the question to that agent using the agent's ID. When transferring, do not generate any text other than the function call.\n\n",
})
}
if handoffs := a.Handoffs(); len(handoffs) > 0 {
var text strings.Builder
var validAgentIDs []string
for _, agent := range handoffs {
text.WriteString("Name: ")
text.WriteString(agent.Name())
text.WriteString(" | Description: ")
text.WriteString(agent.Description())
text.WriteString("\n")
validAgentIDs = append(validAgentIDs, agent.Name())
}
handoffPrompt := "You are part of a multi-agent team. Your goal is to answer the user query in the most helpful way possible.\n\n" +
"Available agents in your team:\n" + text.String() + "\n" +
"You can hand off the conversation to any of these agents at any time by using the `handoff` function with their ID. " +
"The valid agent IDs are: " + strings.Join(validAgentIDs, ", ") + ".\n\n" +
"When to hand off:\n" +
"- If another agent's description indicates they are better suited for the current task or question\n" +
"- If the user explicitly asks for a specific agent\n" +
"- If you need specialized capabilities that another agent provides\n\n" +
"If you are the best agent to handle the current request based on your capabilities, respond directly. " +
"When handing off to another agent, only handoff without talking about the handoff."
messages = append(messages, chat.Message{
Role: chat.MessageRoleSystem,
Content: handoffPrompt,
})
}
if instructions := a.Instruction(); instructions != "" {
messages = append(messages, chat.Message{
Role: chat.MessageRoleSystem,
Content: instructions,
})
}
for _, toolSet := range a.ToolSets() {
if instructions := tools.GetInstructions(toolSet); instructions != "" {
messages = append(messages, chat.Message{
Role: chat.MessageRoleSystem,
Content: instructions,
})
}
}
return messages
}
// buildContextSpecificSystemMessages builds system messages that vary
// per user, project, or time. These messages should come after
// the invariant checkpoint to maintain optimal caching behavior.
//
// These messages depend on runtime context (working directory, current date,
// user-specific skills) and cannot be cached across sessions or users.
// Note: Session summary is handled separately in buildSessionSummaryMessages.
func buildContextSpecificSystemMessages(a *agent.Agent, s *Session) []chat.Message {
var messages []chat.Message
if a.AddDate() {
messages = append(messages, chat.Message{
Role: chat.MessageRoleSystem,
Content: "Today's date: " + time.Now().Format("2006-01-02"),
})
}
wd := s.WorkingDir
if wd == "" {
var err error
wd, err = os.Getwd()
if err != nil {
slog.Error("getting current working directory for environment info", "error", err)
}
}
if wd != "" {
if a.AddEnvironmentInfo() {
messages = append(messages, chat.Message{
Role: chat.MessageRoleSystem,
Content: getEnvironmentInfo(wd),
})
}
for _, prompt := range a.AddPromptFiles() {
additionalPrompts, err := readPromptFiles(wd, prompt)
if err != nil {
slog.Error("reading prompt file", "file", prompt, "error", err)
continue
}
for _, additionalPrompt := range additionalPrompts {
messages = append(messages, chat.Message{
Role: chat.MessageRoleSystem,
Content: additionalPrompt,
})
}
}
}
return messages
}
// buildSessionSummaryMessages builds system messages containing the session summary
// if one exists. Session summaries are context-specific per session and thus should not have a checkpoint (they will be cached alongside the first user message anyway)
//
// startIndex is the index in items from which conversation messages should be
// emitted. When a summary with FirstKeptEntry is present, this points to the
// first kept message so that recent context is preserved after compaction.
// Otherwise it is lastSummaryIndex+1 (i.e. right after the summary item), or
// 0 when there is no summary.
func buildSessionSummaryMessages(items []Item) ([]chat.Message, int) {
var messages []chat.Message
// Find the last summary index to determine where conversation messages start
// and to include the summary in session summary messages
lastSummaryIndex := -1
for i := len(items) - 1; i >= 0; i-- {
if items[i].Summary != "" {
lastSummaryIndex = i
break
}
}
if lastSummaryIndex >= 0 && lastSummaryIndex < len(items) {
messages = append(messages, chat.Message{
Role: chat.MessageRoleUser,
Content: "Session Summary: " + items[lastSummaryIndex].Summary,
CreatedAt: time.Now().Format(time.RFC3339),
})
}
// Determine where conversation messages should start.
// If the summary has a FirstKeptEntry, we start from there so that
// messages kept during compaction are included after the summary.
startIndex := lastSummaryIndex + 1
if lastSummaryIndex >= 0 {
kept := items[lastSummaryIndex].FirstKeptEntry
if kept > 0 && kept < lastSummaryIndex {
startIndex = kept
}
}
return messages, startIndex
}
func (s *Session) GetMessages(a *agent.Agent) []chat.Message {
slog.Debug("Getting messages for agent", "agent", a.Name(), "session_id", s.ID)
// Build invariant system messages (cacheable across sessions/users/projects)
invariantMessages := buildInvariantSystemMessages(a)
markLastMessageAsCacheControl(invariantMessages)
// Build context-specific system messages (vary per user/project/time)
contextMessages := buildContextSpecificSystemMessages(a, s)
markLastMessageAsCacheControl(contextMessages)
// Take a snapshot of Messages under the lock, copying Message structs
// to avoid racing with UpdateMessage which may modify the pointed-to objects.
s.mu.RLock()
items := make([]Item, len(s.Messages))
for i, item := range s.Messages {
if item.Message != nil {
items[i] = Item{Message: deepCopyMessage(item.Message), Summary: item.Summary, SubSession: item.SubSession, Cost: item.Cost}
} else {
items[i] = item
}
}
s.mu.RUnlock()
// Build session summary messages (vary per session)
summaryMessages, startIndex := buildSessionSummaryMessages(items)
var messages []chat.Message
messages = append(messages, invariantMessages...)
messages = append(messages, contextMessages...)
messages = append(messages, summaryMessages...)
// Begin adding conversation messages
for i := startIndex; i < len(items); i++ {
item := items[i]
if item.IsMessage() {
messages = append(messages, item.Message.Message)
}
}
maxItems := a.NumHistoryItems()
if maxItems > 0 {
messages = trimMessages(messages, maxItems)
}
// Use configured max tokens or fall back to default constant if zero or unset.
// -1 means unlimited (no truncation).
maxOldToolCallTokens := s.MaxOldToolCallTokens
if maxOldToolCallTokens == 0 {
maxOldToolCallTokens = DefaultMaxOldToolCallTokens
}
if maxOldToolCallTokens > 0 { // If maxOldToolCallTokens is -1, skip truncation (unlimited)
messages = truncateOldToolContent(messages, maxOldToolCallTokens)
}
systemCount := 0
conversationCount := 0
for i := range messages {
if messages[i].Role == chat.MessageRoleSystem {
systemCount++
} else {
conversationCount++
}
}
slog.Debug("Retrieved messages for agent",
"agent", a.Name(),
"session_id", s.ID,
"total_messages", len(messages),
"system_messages", systemCount,
"conversation_messages", conversationCount,
"max_history_items", maxItems)
return messages
}
// trimMessages ensures we don't exceed the maximum number of messages while maintaining
// consistency between assistant messages and their tool call results.
// System messages and user messages are always preserved and not counted against the limit.
// User messages are protected from trimming to prevent the model from losing
// track of what was asked in long agentic loops.
func trimMessages(messages []chat.Message, maxItems int) []chat.Message {
// Separate system messages from conversation messages
var systemMessages []chat.Message
var conversationMessages []chat.Message
for i := range messages {
if messages[i].Role == chat.MessageRoleSystem {
systemMessages = append(systemMessages, messages[i])
} else {
conversationMessages = append(conversationMessages, messages[i])
}
}
// If conversation messages fit within limit, return all messages
if len(conversationMessages) <= maxItems {
return messages
}
// Identify user message indices — these are protected from trimming
protected := make(map[int]bool)
for i, msg := range conversationMessages {
if msg.Role == chat.MessageRoleUser {
protected[i] = true
}
}
// Keep track of tool call IDs that need to be removed
toolCallsToRemove := make(map[string]bool)
// Calculate how many conversation messages we need to remove
toRemove := len(conversationMessages) - maxItems
// Mark the oldest non-protected messages for removal
removed := make(map[int]bool)
for i := 0; i < len(conversationMessages) && len(removed) < toRemove; i++ {
if protected[i] {
continue
}
removed[i] = true
if conversationMessages[i].Role == chat.MessageRoleAssistant {
for _, toolCall := range conversationMessages[i].ToolCalls {
toolCallsToRemove[toolCall.ID] = true
}
}
}
// Combine system messages with trimmed conversation messages
result := make([]chat.Message, 0, len(systemMessages)+maxItems)
// Add all system messages first
result = append(result, systemMessages...)
// Add protected and non-removed conversation messages
for i, msg := range conversationMessages {
if removed[i] {
continue
}
// Skip orphaned tool results whose assistant message was removed
if msg.Role == chat.MessageRoleTool && toolCallsToRemove[msg.ToolCallID] {
continue
}
result = append(result, msg)
}
return result
}
// truncateOldToolContent replaces tool results with placeholders for older