-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_loop.go
More file actions
1010 lines (927 loc) · 34.5 KB
/
Copy pathmessage_loop.go
File metadata and controls
1010 lines (927 loc) · 34.5 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 main
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"regexp"
"strings"
"sync"
"time"
"sokratos/config"
"sokratos/db"
"sokratos/engine"
"sokratos/google"
"sokratos/grammar"
"sokratos/llm"
"sokratos/logger"
"sokratos/memory"
"sokratos/supervisor"
"sokratos/pipelines"
"sokratos/platform"
"sokratos/prompts"
"sokratos/routines"
"sokratos/textutil"
"sokratos/timeouts"
"sokratos/toolreg"
"sokratos/tools"
)
// messageContext bundles all session-level dependencies needed by the message
// handling functions. All fields are reference types (pointers, maps, funcs)
// so passing by value is safe.
type messageContext struct {
cfg *config.AppConfig
svc *serviceBundle
eng *engine.Engine
lb *llmBundle
registry *tools.Registry
triageCfg *pipelines.TriageConfig
confirmExec func(context.Context, json.RawMessage) (string, error)
skillMtimes map[string]time.Time
skillDeps tools.SkillDeps
rebuildGrammar func()
router engine.SlotRouter
platform platform.Platform
selector *toolreg.ToolSelector // nil = use full tool set (no dynamic selection)
}
// handleReload forces a full re-sync of routines.toml and skills from disk.
// Returns a human-readable summary of what changed.
func handleReload(mc messageContext) string {
added, updated, deleted := routines.SyncFromFile(db.Pool, ".config/routines.toml")
skillsChanged := tools.SyncSkills(mc.registry, "skills", mc.rebuildGrammar, mc.skillMtimes, mc.skillDeps)
var parts []string
if len(added)+len(updated)+len(deleted) > 0 {
parts = append(parts, fmt.Sprintf("Routines: +%d ~%d -%d", len(added), len(updated), len(deleted)))
}
if skillsChanged {
parts = append(parts, "Skills: reloaded")
}
if len(parts) > 0 {
return "Reloaded: " + strings.Join(parts, ", ")
}
return "Everything up to date."
}
// handleMetrics runs a pre-built metrics report. Accepts optional args:
// "/metrics" (overview, 1h), "/metrics slots", "/metrics dispatch 24h".
func handleMetrics(_ messageContext, args string) string {
parts := strings.Fields(args)
var report, window string
if len(parts) >= 1 {
report = parts[0]
}
if len(parts) >= 2 {
window = parts[1]
}
ctx, cancel := context.WithTimeout(context.Background(), timeouts.RoutineDB)
defer cancel()
result, err := tools.QueryMetricsReport(ctx, db.Pool, report, window)
if err != nil {
return "Metrics query failed: " + err.Error()
}
return result
}
// handleBootstrap launches a profile generation run in the background.
// Returns an immediate acknowledgement string.
func handleBootstrap(mc messageContext) string {
if db.Pool == nil || mc.svc.DTC == nil || mc.cfg.EmbedURL == "" {
return "Bootstrap requires database, deep thinker, and embedding service."
}
bootstrapSend := func(text string) {
mc.platform.Broadcast(context.Background(), text)
}
go pipelines.RunBootstrap(pipelines.BootstrapConfig{
PipelineDeps: pipelines.PipelineDeps{
Pool: db.Pool,
DTC: mc.svc.DTC,
EmbedEndpoint: mc.cfg.EmbedURL,
EmbedModel: mc.cfg.EmbedModel,
GrammarFn: mc.svc.BgGrammarFunc,
},
AgentName: mc.cfg.AgentName,
SendFunc: bootstrapSend,
OnProfile: func() {
mc.eng.RefreshProfile()
mc.eng.RefreshPersonality()
},
QueueFn: mc.svc.QueueFunc,
})
return "Profile generation started in the background. I'll notify you when it's ready."
}
// handleGoogle triggers Google OAuth re-authentication via the platform.
// Uses a single OAuth flow with combined Gmail+Calendar scopes so only
// one auth URL + code paste is needed. Re-initializes both services and
// registers tools that were previously disabled.
func handleGoogle(mc messageContext) string {
gmailWasNil := google.GmailService == nil
calWasNil := google.CalendarService == nil
// Delete existing token to force a fresh OAuth flow.
os.Remove(mc.cfg.GoogleTokenPath)
// Build auth IO that reads replies from the platform.
authIO := &google.AuthIO{
Send: func(msg string) {
mc.platform.Broadcast(context.Background(), msg)
},
Receive: func() (string, error) {
return mc.platform.ReadReply()
},
}
// Combined scopes for a single OAuth flow.
scopes := []string{
"https://www.googleapis.com/auth/gmail.readonly",
"https://www.googleapis.com/auth/gmail.send",
"https://www.googleapis.com/auth/calendar",
"https://www.googleapis.com/auth/contacts.readonly",
}
client, err := google.GetClient(
context.Background(), "Google",
mc.cfg.GmailCredsPath, mc.cfg.GoogleTokenPath,
scopes, authIO,
)
if err != nil {
return fmt.Sprintf("❌ Google auth failed: %v", err)
}
if client == nil {
return "⚠️ Google credentials file not found — features disabled"
}
// Create both services from the single authenticated client.
var results []string
if err := google.InitGmailFromClient(context.Background(), client); err != nil {
results = append(results, fmt.Sprintf("Gmail: ❌ %v", err))
} else {
results = append(results, "Gmail: ✅")
}
if err := google.InitCalendarFromClient(context.Background(), client); err != nil {
results = append(results, fmt.Sprintf("Calendar: ❌ %v", err))
} else {
results = append(results, "Calendar: ✅")
}
if err := google.InitPeopleFromClient(context.Background(), client); err != nil {
results = append(results, fmt.Sprintf("People: ❌ %v", err))
} else {
results = append(results, "People: ✅")
google.LoadContacts()
}
// Register tools that were previously disabled.
if gmailWasNil && google.GmailService != nil {
registerGmailTools(mc.registry, db.Pool, mc.triageCfg, mc.cfg.EmailDisplayBatch, mc.svc.Subagent)
mc.rebuildGrammar()
logger.Log.Info("[/google] Gmail tools registered")
}
if calWasNil && google.CalendarService != nil {
registerCalendarTools(mc.registry, db.Pool)
mc.rebuildGrammar()
logger.Log.Info("[/google] Calendar tools registered")
}
// Reset the auth error once so future expiry triggers a new notification.
mc.svc.AuthErrorOnce = &sync.Once{}
// Also invalidate the calendar list cache since we have a fresh token.
google.InvalidateCache()
return strings.Join(results, "\n")
}
// processMessage runs the full supervisor pipeline for a regular user message:
// prefetch → temporal context → supervisor → slide/archive → triage → reply.
func processMessage(mc messageContext, msg *platform.IncomingMessage, msgText, userPrompt string, visionParts []llm.ContentPart) {
messageStart := time.Now()
typingCancel := mc.platform.StartTyping(context.Background(), msg.ChannelID)
// Phase 0: Pipeline isolation — tag this message and exclude prior pipeline's memories.
pipelineID := msg.PipelineID()
excludePipelineID := mc.svc.StateMgr.LastPipelineID()
// Phase 1: Snapshot history (StateManager has its own RWMutex).
history := mc.svc.StateMgr.ReadMessages()
// Phase 1.5: Staleness trim — if there's a long gap since the last
// conversation message, old topics are stale and will confuse the model.
// Keep only recent messages for immediate context.
const staleGap = 30 * time.Minute
if len(history) > 4 {
lastMsg := history[len(history)-1]
if !lastMsg.Time.IsZero() && time.Since(lastMsg.Time) > staleGap {
history = history[len(history)-4:]
}
}
// Carry-forward: if last prefetch was useful, boost those memory IDs.
var boostIDs []int64
if mc.svc.StateMgr.PrefetchUseful() {
boostIDs = mc.svc.StateMgr.LastPrefetchIDs()
}
// Phase 2: Start prefetch + temporal context + tool selection in parallel.
type prefetchData struct {
content string
ids []int64
summaries string
temporal string
reflection string
}
pfCh := make(chan prefetchData, 1)
go func() {
pfStart := time.Now()
var pd prefetchData
var memoriesFound int
if db.Pool != nil && mc.cfg.EmbedURL != "" && strings.TrimSpace(msgText) != "" {
pfCtx, pfCancel := context.WithTimeout(context.Background(), tools.TimeoutPrefetch)
if pf := subconsciousPrefetch(pfCtx, db.Pool, mc.cfg.EmbedURL, mc.cfg.EmbedModel, msgText, history, excludePipelineID, boostIDs...); pf != nil {
pd.content = pf.Summaries
pd.ids = pf.IDs
pd.summaries = pf.Summaries
memoriesFound = len(pf.IDs)
}
pfCancel()
}
if db.Pool != nil {
pd.temporal = engine.BuildTemporalContext(context.Background(), db.Pool)
}
// Load latest reflection insight for the supervisor.
if db.Pool != nil {
rfCtx, rfCancel := context.WithTimeout(context.Background(), timeouts.DBQuery)
pd.reflection = memory.QueryLatestReflection(rfCtx, db.Pool)
rfCancel()
}
mc.svc.Metrics.Since("prefetch.duration", pfStart, map[string]string{
"memories_found": fmt.Sprintf("%d", memoriesFound),
})
pfCh <- pd
}()
// Tool selection: embed query and select relevant tools (parallel with prefetch).
type toolSelectionData struct {
agent *llm.ToolAgentConfig // nil = use full tool set
}
tsCh := make(chan toolSelectionData, 1)
go func() {
if mc.selector == nil {
tsCh <- toolSelectionData{}
return
}
tsCtx, tsCancel := context.WithTimeout(context.Background(), timeouts.Embedding)
defer tsCancel()
// Include recent conversation context for short/ambiguous queries so the
// embedding match has enough signal. "Remove it" alone is meaningless, but
// "...reminder about cousins in Poland | Remove it" matches check_background_task.
selectQuery := msgText
if len(msgText) < 40 {
if recent := mc.eng.SM.LastAssistantMessage(); recent != "" {
// Truncate prior response to keep the embedding focused.
if len(recent) > 150 {
recent = recent[:150]
}
selectQuery = recent + " | " + msgText
}
}
names, err := mc.selector.Select(tsCtx, selectQuery)
if err != nil {
logger.Log.Warnf("[tool-selector] selection failed, using full set: %v", err)
tsCh <- toolSelectionData{}
return
}
if names == nil {
tsCh <- toolSelectionData{}
return
}
// Build per-request tool descriptions and grammar.
// CompactIndex provides core tools (always visible); BuildSelectedToolIndex
// adds the RAG-selected extras. Both are needed so the supervisor always
// sees search_web, search_memory, etc.
coreIndex := mc.registry.CompactIndex()
selectedIndex := mc.registry.BuildSelectedToolIndex(names)
toolIndex := coreIndex
if selectedIndex != "" {
toolIndex += "\n" + selectedIndex
}
td := strings.Replace(prompts.Tools, "%TOOL_INDEX%", toolIndex, 1)
schemas := mc.registry.SchemasForTools(names)
grammarStr := grammar.BuildSubagentToolGrammar(schemas)
tsCh <- toolSelectionData{
agent: &llm.ToolAgentConfig{
ToolDescriptions: td,
Grammar: grammarStr,
},
}
}()
// Phase 3: Snapshot personality/profile under the lock (microseconds),
// then release before the multi-second inference call.
mc.eng.Mu.Lock()
personalityContent := mc.eng.PersonalityContent
profileContent := mc.eng.ProfileContent
mc.eng.Mu.Unlock()
// Phase 3.3: Inject recent system actions (routines, heartbeats) so the
// supervisor knows what the system recently did and avoids duplicate work.
if xml := mc.eng.FormatRecentActionsXML(2 * mc.cfg.HeartbeatInterval); xml != "" {
userPrompt += "\n\n" + xml
}
// Phase 3.4: Inject active background job context so the supervisor can
// route user messages to background Brain jobs via reply_to_job/cancel_job.
if jobCtx := buildJobContext(mc.svc.StateMgr.GetJobs()); jobCtx != "" {
userPrompt += "\n\n" + jobCtx
}
// Phase 3.5: Wait for prefetch and tool selection results.
pf := <-pfCh
ts := <-tsCh
// Track prefetch IDs for carry-forward.
mc.svc.StateMgr.SetLastPrefetchIDs(pf.ids)
mc.svc.StateMgr.SetPrefetchUseful(len(pf.ids) > 0)
// Inject latest reflection insight into temporal context if available.
if pf.reflection != "" {
// Extract actionable sections (patterns + predictions).
sections := extractReflectionForPrompt(pf.reflection)
if sections != "" {
if pf.temporal != "" {
pf.temporal += "\n\n"
}
pf.temporal += "<recent_reflection>\n" + sections + "\n</recent_reflection>"
}
}
// Phase 3.5: Brain strategic planning — proactive guidance before slot acquire.
// Non-blocking — if the Brain slot is busy, proceed without guidance.
// When the Brain provides guidance (task is complex), route to Brain for supervision.
var strategicGuidance string
if mc.svc.DTC != nil {
var historyCtx strings.Builder
for i := len(history) - 1; i >= 0 && historyCtx.Len() < 2000; i-- {
m := history[i]
if m.Role == "user" || m.Role == "assistant" {
line := fmt.Sprintf("[%s]: %s\n", m.Role, m.Content)
if len(line) > 400 {
line = line[:400] + "...\n"
}
historyCtx.WriteString(line)
}
}
historySection := ""
if historyCtx.Len() > 0 {
historySection = "Recent conversation:\n" + historyCtx.String()
}
memoriesSection := ""
if pf.content != "" {
memoriesSection = "Retrieved memories:\n" + pf.content
}
planPrompt := strings.NewReplacer(
"%QUERY%", msgText,
"%HISTORY%", historySection,
"%MEMORIES%", memoriesSection,
).Replace(prompts.BrainStrategic)
planCtx, planCancel := context.WithTimeout(context.Background(), 15*time.Second)
planCtx = engine.WithPriority(planCtx, engine.PriorityUser)
raw, err := mc.svc.DTC.TryCompleteNoThink(planCtx,
"You are a strategic planner guiding a smaller routing model.",
planPrompt, 256)
planCancel()
if err != nil {
logger.Log.Debugf("[brain-strategic] skipped: %v", err)
} else {
raw = textutil.StripThinkTags(raw)
raw = strings.TrimSpace(raw)
if !strings.EqualFold(raw, "NONE") && raw != "" {
strategicGuidance = raw
logger.Log.Infof("[brain-strategic] guidance: %.120s", raw)
}
}
}
useBrain := strategicGuidance != ""
// Phase 4: Acquire supervisor slot.
// When the Brain flagged the task as complex (provided guidance), prefer Brain
// for supervision — it has the reasoning depth for multi-step tool chains.
// Simple tasks (NONE) use the 9B for speed.
choice := mc.router.AcquireOrFallback(context.Background(), useBrain, engine.PriorityUser)
if useBrain {
logger.Log.Info("[supervisor] routed to Brain for complex task")
}
acquired := true
defer func() {
if acquired {
choice.Release()
}
}()
// Progress tracking: send "Thinking..." immediately, first tool call
// replaces it, subsequent tool calls send new messages (visible trail).
ph, _ := platform.NewProgressHandle(context.Background(), mc.platform, msg.ChannelID, "Thinking...", msg.ID)
firstToolCall := true
// Wrap confirmExec to inject progress reporting into the tool context.
progressExec := func(ctx context.Context, raw json.RawMessage) (string, error) {
if ph != nil {
ctx = tools.WithProgress(ctx, func(status string) {
ph.Update(context.Background(), status)
})
}
return mc.confirmExec(ctx, raw)
}
// Use per-request tool selection if available, otherwise fall back to full set.
toolAgent := mc.lb.ToolAgent
if ts.agent != nil {
toolAgent = ts.agent
}
// BrainFinalize: the Brain composes all substantive user-facing responses.
// The 9B handles tool routing; the Brain handles response quality.
var brainFinalize func(ctx context.Context, toolContext, draft string) (string, error)
if mc.svc.DTC != nil {
dtc := mc.svc.DTC
brainFinalize = func(ctx context.Context, toolContext, draft string) (string, error) {
if ph != nil {
ph.Update(context.Background(), "Composing response...")
}
// Build recent conversation context so the Brain knows what was
// discussed. Truncate to last ~2K chars to stay within budget.
var historyCtx strings.Builder
for i := len(history) - 1; i >= 0 && historyCtx.Len() < 2000; i-- {
m := history[i]
if m.Role == "user" || m.Role == "assistant" {
line := fmt.Sprintf("[%s]: %s\n", m.Role, m.Content)
if len(line) > 500 {
line = line[:500] + "...\n"
}
historyCtx.WriteString(line)
}
}
var prompt strings.Builder
if historyCtx.Len() > 0 {
fmt.Fprintf(&prompt, "Recent conversation:\n%s\n", historyCtx.String())
}
fmt.Fprintf(&prompt, "The user said: %s\n\n", msgText)
if toolContext != "" {
fmt.Fprintf(&prompt, "Tool results:\n%s\n", toolContext)
}
fmt.Fprintf(&prompt, "Draft response from the routing model:\n%s\n\n", draft)
prompt.WriteString("Compose a polished, concise response. Preserve all facts and URLs. Fix any errors — if the draft contradicts the conversation history, correct it. Be direct — no filler, no preamble.")
return dtc.CompleteNoThink(ctx, "You are composing a final response for a Telegram chat. Be concise and factual.", prompt.String(), 2048)
}
}
// BrainReview: mid-loop oversight and error escalation. Fires on round 2+
// (multi-step oversight) and after tool errors. Round-0 review is replaced
// by the strategic guidance call above.
var brainReview func(ctx context.Context, userQuery, thinking, context string) (string, error)
if mc.svc.DTC != nil {
dtc := mc.svc.DTC
brainReview = func(ctx context.Context, userQuery, thinking, reviewCtx string) (string, error) {
// Detect whether this is a mid-loop escalation (reviewCtx contains
// recent exchange with [user]/[assistant] lines) vs round-0 review.
isMidLoop := strings.Contains(reviewCtx, "[user]:")
var userPrompt string
if isMidLoop {
p := strings.NewReplacer("%QUERY%", userQuery, "%EXCHANGE%", reviewCtx).Replace(prompts.BrainEscalation)
userPrompt = p
} else {
p := strings.NewReplacer("%QUERY%", userQuery, "%TOOL_CALL%", reviewCtx).Replace(prompts.BrainReview)
if thinking != "" {
p = "The routing model thought: " + thinking + "\n\n" + p
}
userPrompt = p
}
// Include recent conversation history for context.
var historyCtx strings.Builder
for i := len(history) - 1; i >= 0 && historyCtx.Len() < 2000; i-- {
m := history[i]
if m.Role == "user" || m.Role == "assistant" {
line := fmt.Sprintf("[%s]: %s\n", m.Role, m.Content)
if len(line) > 400 {
line = line[:400] + "...\n"
}
historyCtx.WriteString(line)
}
}
if historyCtx.Len() > 0 {
userPrompt = "Recent conversation:\n" + historyCtx.String() + "\n" + userPrompt
}
maxTokens := 256
if isMidLoop {
maxTokens = 512
}
raw, err := dtc.CompleteNoThink(ctx,
"You are a senior engineer reviewing a tool routing decision. Be specific and actionable.",
userPrompt, maxTokens)
if err != nil {
return "", err
}
raw = textutil.StripThinkTags(raw)
raw = strings.TrimSpace(raw)
if strings.EqualFold(raw, "OK") || strings.HasPrefix(strings.ToLower(raw), "ok") {
return "", nil
}
return "BRAIN GUIDANCE: " + raw + "\nFollow this guidance for your next action.", nil
}
}
// SummarizeFn: condense oversized tool results via subagent instead of truncating.
// Uses TryComplete so it falls back to truncation if no slot is available.
var summarizeFn func(ctx context.Context, toolName, result, userGoal string) (string, error)
if mc.svc.Subagent != nil {
subagent := mc.svc.Subagent
summarizeFn = func(ctx context.Context, toolName, result, userGoal string) (string, error) {
userContent := fmt.Sprintf("The user asked: %s\n\nThe tool %q returned the following output (%d chars). "+
"Extract the information relevant to the task. Include:\n"+
"- Key facts, values, names, and identifiers\n"+
"- Code examples, API calls, curl commands, and parameter formats VERBATIM (do not paraphrase code)\n"+
"- URLs, endpoints, ports, and protocol details\n"+
"Omit: boilerplate, navigation menus, ads, repeated structure, XML namespaces.\n\n%s",
userGoal, toolName, len(result), result)
return subagent.TryComplete(ctx,
"You are a tool result summarizer. Preserve code examples and API details verbatim.",
userContent, 2048)
}
}
// Accumulate thinking tokens for UI display.
var thinkingBuf strings.Builder
var thinkingMu sync.Mutex
lastThinkUpdate := time.Now()
opts := llm.QuerySupervisorOpts{
Parts: visionParts,
History: history,
PersonalityContent: personalityContent,
ProfileContent: profileContent,
TemporalContext: pf.temporal,
PrefetchContent: pf.content,
StrategicGuidance: strategicGuidance,
MaxToolResultLen: mc.cfg.MaxToolResultLen,
MaxWebSources: mc.cfg.MaxWebSources,
ToolAgent: toolAgent,
MandatedBrainTools: mandatedBrainTools,
SummarizeFn: summarizeFn,
// Both Brain and 9B think on round 0 only. Brain doesn't need thinking
// on every round — it's slow (60-80s/round) and round-0 thinking is
// sufficient for strategy. The strategic guidance already provides context.
FirstRoundThinking: true,
BrainFinalize: brainFinalize,
BrainReview: brainReview,
OnThinkingToken: func(token string) {
thinkingMu.Lock()
defer thinkingMu.Unlock()
thinkingBuf.WriteString(token)
// Throttle updates to avoid flooding Telegram API (max 1 update/500ms).
if time.Since(lastThinkUpdate) >= 500*time.Millisecond && ph != nil {
text := strings.TrimSpace(thinkingBuf.String())
// Strip any residual think tags that leaked through.
text = strings.ReplaceAll(text, "</think>", "")
text = strings.ReplaceAll(text, "<think>", "")
if text == "" {
return
}
// Truncate from the front to keep the message under Telegram's
// 4096 char limit while showing the most recent reasoning.
const maxDisplay = 3800
if len(text) > maxDisplay {
text = "..." + text[len(text)-maxDisplay:]
}
if err := ph.Update(context.Background(), "Thinking:\n"+text); err != nil {
logger.Log.Warnf("[thinking-stream] progress update failed: %v", err)
} else {
logger.Log.Debugf("[thinking-stream] updated progress (%d chars)", len(text))
}
lastThinkUpdate = time.Now()
}
},
OnToolStart: func(toolSummary string) {
choice.ReleaseReserved()
acquired = false
// Format tool call summaries. For parallel calls (joined by " + "),
// each command gets its own collapsible block. For single calls,
// long content is wrapped in a collapsible block.
display := formatToolDisplay(toolSummary)
if firstToolCall {
firstToolCall = false
if ph != nil {
ph.Update(context.Background(), display)
}
} else {
msgID, err := mc.platform.Send(context.Background(), msg.ChannelID, display, "")
if err == nil && ph != nil {
ph.MessageID = msgID
}
}
},
OnToolEnd: func(reCtx context.Context) error {
if reErr := choice.Reacquire(reCtx); reErr != nil {
return reErr
}
acquired = true
return nil
},
OnToolResult: func(toolName, result string) {
if result == "" {
return
}
// Send full output in collapsible blocks. Split into chunks
// if needed to stay within Telegram's 4096 char message limit.
const maxChunk = 3800 // leave room for collapsible wrapper overhead
for i := 0; i < len(result); i += maxChunk {
end := i + maxChunk
isLast := end >= len(result)
if end > len(result) {
end = len(result)
}
chunk := result[i:end]
title := toolName + " result"
if !isLast {
title = fmt.Sprintf("%s result (part %d)", toolName, i/maxChunk+1)
}
collapsed := platform.FormatCollapsible(title, chunk)
msgID, err := mc.platform.Send(context.Background(), msg.ChannelID, collapsed, "")
if err == nil && ph != nil {
ph.MessageID = msgID
}
}
},
OnToolExec: func(tool string, dur time.Duration, toolErr error) {
result := "ok"
if toolErr != nil {
result = "hard_error"
}
mc.svc.Metrics.EmitDuration("tool.exec", dur, map[string]string{"tool": tool, "result": result})
},
}
// When Brain is supervising, disable self-review and self-finalize.
if useBrain {
opts.BrainFinalize = nil
opts.BrainReview = nil
}
// Tag the supervisor ctx with PriorityUser so all downstream DTC calls
// (brainFinalize, brainReview, deep_think, consult_deep_thinker) acquire
// the Brain slot ahead of background cognitive work.
supCtx := engine.WithPriority(context.Background(), engine.PriorityUser)
reply, msgs, err := llm.QuerySupervisor(supCtx, choice.Client, choice.Model, userPrompt, progressExec, mc.lb.TrimFn, &opts)
// Build thinking text for the collapsible block at the bottom of the reply.
// Includes strategic guidance (if any) + the 9B's reasoning.
thinkingMu.Lock()
thinkingText := thinkingBuf.String()
thinkingMu.Unlock()
var thinkingBlock string
{
var parts []string
if strategicGuidance != "" {
parts = append(parts, "Strategic guidance:\n"+strategicGuidance)
}
if thinkingText != "" {
clean := strings.TrimSpace(thinkingText)
clean = strings.ReplaceAll(clean, "</think>", "")
clean = strings.ReplaceAll(clean, "<think>", "")
clean = strings.TrimSpace(clean)
if clean != "" {
parts = append(parts, clean)
}
}
if len(parts) > 0 {
thinkingBlock = platform.FormatCollapsible("Thinking", strings.Join(parts, "\n\n"))
}
}
// Check for BackgroundJobRequest — spawn a background Brain job.
var bjr *supervisor.BackgroundJobRequest
if errors.As(err, &bjr) {
choice.Release()
acquired = false
typingCancel()
userGoal := bjr.UserGoal
if bjr.ProblemStatement != "" {
userGoal = bjr.ProblemStatement
}
job := mc.svc.StateMgr.CreateJob(bjr.Tool, userGoal, msg.ChannelID)
job.TaskType = bjr.TaskType
ack := brainSessionAcks[bjr.Tool]
if ack == "" {
ack = "Working on that in the background..."
}
mc.platform.Send(context.Background(), msg.ChannelID, ack, msg.ID)
// Store the user message in conversation state.
mc.svc.StateMgr.AppendMessage(llm.Message{Role: "user", Content: msgText})
mc.svc.StateMgr.AppendMessage(llm.Message{Role: "assistant", Content: ack})
go runBackgroundJob(mc, job)
return
}
typingCancel()
mc.svc.Metrics.Since("message.total", messageStart, map[string]string{"path": "supervisor"})
toolCtx, toolsUsed := summarizeToolContext(msgs)
// Track tool co-occurrence for selection learning.
if mc.selector != nil && toolsUsed {
usedTools := extractToolNames(msgs)
if len(usedTools) > 0 {
mc.selector.TrackToolRun(usedTools)
}
}
// Append thinking block to the reply so it appears at the bottom.
if thinkingBlock != "" && reply != "" && !strings.Contains(reply, "<NO_ACTION_REQUIRED>") {
reply = reply + "\n\n" + thinkingBlock
}
// Clear the streaming thinking from the progress message since it's
// now included in the reply. Update to a brief note so it's not blank.
if ph != nil && thinkingBlock != "" {
ph.Update(context.Background(), "Done")
}
completeMessageHandling(mc, msg, messageResult{
Reply: reply,
Messages: condenseToolResults(msgs),
ToolContext: toolCtx,
ToolsUsed: toolsUsed,
MsgText: msgText,
PrefetchIDs: pf.ids,
PrefetchSummaries: pf.summaries,
PipelineID: pipelineID,
Err: err,
})
}
// ---------------------------------------------------------------------------
// Shared post-processing for both Brain and dispatch paths.
// ---------------------------------------------------------------------------
// messageResult normalizes the output of the supervisor path so that
// completeMessageHandling can apply identical post-processing.
type messageResult struct {
Reply string // final text reply to send
Messages []llm.Message // condensed messages for state
ToolContext string // summarized tool usage for triage
ToolsUsed bool // whether tools were called
MsgText string // original user message text
PrefetchIDs []int64 // memory IDs from prefetch
PrefetchSummaries string // memory summaries from prefetch
PipelineID int64 // message ID for memory isolation
Err error // LLM/execution error (nil on success)
}
// completeMessageHandling runs shared post-processing after the supervisor:
// append to state, slide/archive, triage, memory usefulness evaluation,
// error fallback, and send reply.
func completeMessageHandling(mc messageContext, msg *platform.IncomingMessage, mr messageResult) {
// Append messages to conversation state.
for _, m := range mr.Messages {
mc.svc.StateMgr.AppendMessage(m)
}
// Slide and archive (token-budget-aware).
if db.Pool != nil && mc.cfg.EmbedURL != "" {
engine.SlideAndArchiveContext(context.Background(), mc.svc.StateMgr, engine.ArchiveDeps{
DB: db.Pool, EmbedEndpoint: mc.cfg.EmbedURL, EmbedModel: mc.cfg.EmbedModel,
MemoryFuncs: engine.MemoryFuncs{
DTCQueueFn: mc.svc.DTCQueueFunc, SubagentFn: mc.svc.SubagentFunc,
GrammarFn: mc.svc.GrammarFunc, BgGrammarFn: mc.svc.BgGrammarFunc, QueueFn: mc.svc.QueueFunc,
},
PipelineID: mr.PipelineID,
Tokenizer: mc.lb.Client,
TokenBudget: mc.cfg.ContextTokenBudget,
})
}
// Queue triage for deferred processing during heartbeat ticks.
// This keeps the Brain prompt cache warm for interactive messages.
if mr.Err == nil && db.Pool != nil && mc.cfg.EmbedURL != "" && mc.svc.DTC != nil && mc.triageCfg != nil {
exchange := mr.ToolContext + fmt.Sprintf("user: %s\nassistant: %s", mr.MsgText, mr.Reply)
pipelines.EnqueueConversationTriage(db.Pool, exchange, mr.ToolsUsed, mr.PipelineID)
}
// Record pipeline ID so the next message's prefetch can exclude stale memories.
if mr.PipelineID != 0 {
mc.svc.StateMgr.SetLastPipelineID(mr.PipelineID)
}
// Memory usefulness evaluation.
if mr.Err == nil && len(mr.PrefetchIDs) > 0 && db.Pool != nil && mc.svc.Subagent != nil {
capturedIDs := mr.PrefetchIDs
capturedReply := mr.Reply
capturedMsgText := mr.MsgText
capturedSubagent := mc.svc.Subagent
capturedSummaries := mr.PrefetchSummaries
go evaluateMemoryUsefulnessViaSubagent(db.Pool, capturedSubagent, capturedIDs, capturedMsgText, capturedReply, capturedSummaries)
}
reply := mr.Reply
if mr.Err != nil {
logger.Log.Errorf("LLM error: %v", mr.Err)
reply = "Sorry, something went wrong processing your message."
}
// Don't send supervisor control tags to the user.
if strings.Contains(reply, "<NO_ACTION_REQUIRED>") {
return
}
// Send reply via platform (handles formatting + fallback internally).
if _, err := mc.platform.Send(context.Background(), msg.ChannelID, reply, msg.ID); err != nil {
logger.Log.Errorf("Error sending message: %v", err)
}
// Emit curiosity signal when the supervisor expressed uncertainty.
emitConversationGapSignal(mc.eng, reply)
}
// formatToolDisplay formats a tool call summary for Telegram display.
// Single calls with long content get wrapped in a collapsible block.
// Parallel calls (joined by " + ") get one collapsible block with all commands listed.
func formatToolDisplay(toolSummary string) string {
// Parallel calls: wrap all commands in a single collapsible block.
if strings.Contains(toolSummary, " + ") {
parts := strings.Split(toolSummary, " + ")
// Build a title from unique tool names and list commands inside.
seen := make(map[string]struct{})
var names []string
var sb strings.Builder
for _, part := range parts {
part = strings.TrimSpace(part)
if idx := strings.Index(part, ": "); idx > 0 {
name := part[:idx]
if _, ok := seen[name]; !ok {
seen[name] = struct{}{}
names = append(names, name)
}
sb.WriteString(part[idx+2:])
} else {
sb.WriteString(part)
}
sb.WriteByte('\n')
}
title := fmt.Sprintf("%s (%d calls)", strings.Join(names, "+"), len(parts))
return platform.FormatCollapsible(title, strings.TrimSpace(sb.String()))
}
// Single call: wrap in collapsible if long.
if len(toolSummary) > 100 {
if idx := strings.Index(toolSummary, ": "); idx > 0 {
title := toolSummary[:idx]
content := toolSummary[idx+2:]
return platform.FormatCollapsible(title, content)
}
}
return toolSummary
}
// uncertaintyPatterns matches replies where the supervisor expressed a knowledge gap.
var uncertaintyPatterns = regexp.MustCompile(`(?i)(I don't have (?:enough )?information|I'm not sure|I couldn't find|I don't know|I wasn't able to find|I lack information)`)
// nonResearchablePatterns matches gaps that are config/access issues, not knowledge gaps.
var nonResearchablePatterns = regexp.MustCompile(`(?i)(credential|password|SSH|login|authenticate|permission|access denied|allowlist|not in.*list|key|token)`)
// emitConversationGapSignal checks if the supervisor's reply indicates
// uncertainty and emits a curiosity signal for background research.
func emitConversationGapSignal(eng *engine.Engine, reply string) {
if eng.CuriositySignals == nil || len(reply) < 20 {
return
}
match := uncertaintyPatterns.FindString(reply)
if match == "" {
return
}
// Don't emit signals for config/access issues — those aren't researchable.
if nonResearchablePatterns.MatchString(reply) {
return
}
// Extract a topic hint from the reply (first 120 chars after the match).
idx := strings.Index(reply, match)
topic := reply
if idx >= 0 {
end := idx + len(match) + 120
if end > len(reply) {
end = len(reply)
}
topic = reply[idx:end]
}
select {
case eng.CuriositySignals <- engine.CuriositySignal{
Source: "conversation",
Query: topic,
Priority: 5,
}:
logger.Log.Debugf("[curiosity-signal] conversation gap detected: %s", textutil.Truncate(topic, 80))
default:
// Channel full, drop.
}
}
// extractToolNames parses tool names from supervisor messages.
// Tool calls appear as assistant messages with JSON containing "name" fields.
func extractToolNames(msgs []llm.Message) []string {
seen := make(map[string]struct{})
var names []string
for _, m := range msgs {
if m.Role != "assistant" {
continue
}
// Extract tool name from grammar-constrained JSON.
var dec struct {
Name string `json:"name"`
Calls []struct {
Name string `json:"name"`
} `json:"calls"`
}
if err := json.Unmarshal([]byte(m.Content), &dec); err != nil {
continue
}
if dec.Name != "" && dec.Name != "respond" {
if _, ok := seen[dec.Name]; !ok {
seen[dec.Name] = struct{}{}
names = append(names, dec.Name)
}
}
for _, c := range dec.Calls {
if c.Name != "" {
if _, ok := seen[c.Name]; !ok {
seen[c.Name] = struct{}{}
names = append(names, c.Name)
}
}
}
}
return names
}
// extractReflectionForPrompt pulls PATTERNS and PREDICTIONS from a reflection for supervisor injection.
func extractReflectionForPrompt(summary string) string {
var result strings.Builder
lines := strings.Split(summary, "\n")
capturing := false
for _, line := range lines {
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, "**PATTERNS**") || strings.HasPrefix(trimmed, "**PREDICTIONS**") {
capturing = true
result.WriteString(trimmed)
result.WriteByte('\n')
continue
}
if strings.HasPrefix(trimmed, "**") && capturing {
capturing = false
}
if capturing && trimmed != "" {
result.WriteString(trimmed)
result.WriteByte('\n')