@@ -4,6 +4,7 @@ import (
44 "context"
55 "encoding/json"
66 "fmt"
7+ "strconv"
78 "strings"
89 "sync"
910 "time"
@@ -37,6 +38,11 @@ type Mysis struct {
3738 // For runtime tracking
3839 lastError error
3940 currentAccountUsername string
41+ activityState ActivityState
42+ activityUntil time.Time
43+ lastServerTick int64
44+ lastServerTickAt time.Time
45+ tickDuration time.Duration
4046 }
4147
4248 type contextStats struct {
@@ -52,13 +58,14 @@ type contextStats struct {
5258 // NewMysis creates a new mysis from stored data.
5359 func NewMysis(id, name string, createdAt time.Time, p provider.Provider, s *store.Store, bus *EventBus) *Mysis {
5460 return &Mysis{
55- id: id,
56- name: name,
57- createdAt: createdAt,
58- provider: p,
59- store: s,
60- bus: bus,
61- state: MysisStateIdle,
61+ id: id,
62+ name: name,
63+ createdAt: createdAt,
64+ provider: p,
65+ store: s,
66+ bus: bus,
67+ state: MysisStateIdle,
68+ activityState: ActivityStateIdle,
6269 }
6370 }
6471
@@ -165,6 +172,8 @@ func (a *Mysis) Start() error {
165172 oldState := a.state
166173 a.state = MysisStateRunning
167174 a.lastError = nil
175+ a.activityState = ActivityStateIdle
176+ a.activityUntil = time.Time{}
168177
169178 ctx, cancel := context.WithCancel(context.Background())
170179 a.ctx = ctx
@@ -435,6 +444,7 @@ func (a *Mysis) SendMessageFrom(content string, source store.MemorySource, sende
435444
436445 // Execute each tool call
437446 for _, tc := range response.ToolCalls {
447+ a.updateActivityFromToolCall(tc.Name)
438448 // Signal MCP activity
439449 a.bus.Publish(Event{
440450 Type: EventNetworkMCP,
@@ -444,6 +454,7 @@ func (a *Mysis) SendMessageFrom(content string, source store.MemorySource, sende
444454 })
445455
446456 result, execErr := a.executeToolCall(ctx, mcpProxy, tc)
457+ a.updateActivityFromToolResult(tc.Name, result, execErr)
447458
448459 // Store the tool result
449460 resultContent := a.formatToolResult(tc.ID, tc.Name, result, execErr)
@@ -833,7 +844,7 @@ func (a *Mysis) run(ctx context.Context) {
833844 isRunning := a.state == MysisStateRunning
834845 a.mu.RUnlock()
835846
836- if isRunning {
847+ if isRunning && a.shouldNudge(time.Now()) {
837848 // Only nudge if not already in a turn
838849 if a.turnMu.TryLock() {
839850 a.turnMu.Unlock()
@@ -844,6 +855,224 @@ func (a *Mysis) run(ctx context.Context) {
844855 }
845856 }
846857
858+func (a *Mysis) shouldNudge(now time.Time) bool {
859+ a.mu.Lock()
860+ defer a.mu.Unlock()
861+
862+ if a.activityState != ActivityStateTraveling && a.activityState != ActivityStateCooldown {
863+ return true
864+ }
865+
866+ if !a.activityUntil.IsZero() && now.Before(a.activityUntil) {
867+ return false
868+ }
869+
870+ a.activityState = ActivityStateIdle
871+ a.activityUntil = time.Time{}
872+ return true
873+}
874+
875+func (a *Mysis) updateActivityFromToolCall(toolName string) {
876+ switch toolName {
877+ case "travel":
878+ a.setActivity(ActivityStateTraveling, time.Now().Add(constants.WaitStateNudgeInterval))
879+ case "mine":
880+ a.setActivity(ActivityStateMining, time.Time{})
881+ case "attack", "attack_base":
882+ a.setActivity(ActivityStateInCombat, time.Time{})
883+ }
884+}
885+
886+func (a *Mysis) updateActivityFromToolResult(toolName string, result *mcp.ToolResult, err error) {
887+ if err != nil || result == nil || result.IsError {
888+ a.clearActivityIf(ActivityStateMining)
889+ a.clearActivityIf(ActivityStateInCombat)
890+ if toolName == "travel" {
891+ a.setActivity(ActivityStateIdle, time.Time{})
892+ }
893+ return
894+ }
895+
896+ now := time.Now()
897+ payload, ok := parseToolResultPayload(result)
898+ var currentTick int64
899+ var currentTickOK bool
900+ if ok {
901+ currentTick, currentTickOK = findIntField(payload, "current_tick", "tick")
902+ if currentTickOK {
903+ a.updateServerTick(now, currentTick)
904+ }
905+ }
906+
907+ if toolName == "travel" {
908+ arrivalTick, found := findIntField(payload, "arrival_tick")
909+ if found {
910+ if currentTickOK && arrivalTick <= currentTick {
911+ a.setActivity(ActivityStateIdle, time.Time{})
912+ return
913+ }
914+ until := a.estimateTravelUntil(now, arrivalTick, currentTick, currentTickOK)
915+ a.setActivity(ActivityStateTraveling, until)
916+ }
917+ return
918+ }
919+
920+ if cooldownTicks, found := findIntField(payload, "cooldown_ticks", "cooldown_remaining"); found && cooldownTicks > 0 {
921+ until := a.estimateCooldownUntil(now, cooldownTicks)
922+ a.setActivity(ActivityStateCooldown, until)
923+ return
924+ }
925+
926+ switch toolName {
927+ case "mine":
928+ a.clearActivityIf(ActivityStateMining)
929+ case "attack", "attack_base":
930+ a.clearActivityIf(ActivityStateInCombat)
931+ }
932+}
933+
934+func (a *Mysis) setActivity(state ActivityState, until time.Time) {
935+ a.mu.Lock()
936+ a.activityState = state
937+ a.activityUntil = until
938+ a.mu.Unlock()
939+}
940+
941+func (a *Mysis) clearActivityIf(state ActivityState) {
942+ a.mu.Lock()
943+ if a.activityState == state {
944+ a.activityState = ActivityStateIdle
945+ a.activityUntil = time.Time{}
946+ }
947+ a.mu.Unlock()
948+}
949+
950+func (a *Mysis) updateServerTick(now time.Time, tick int64) {
951+ if tick <= 0 {
952+ return
953+ }
954+
955+ a.mu.Lock()
956+ defer a.mu.Unlock()
957+
958+ if a.lastServerTick > 0 && tick > a.lastServerTick && !a.lastServerTickAt.IsZero() {
959+ elapsed := now.Sub(a.lastServerTickAt)
960+ delta := tick - a.lastServerTick
961+ if elapsed > 0 && delta > 0 {
962+ a.tickDuration = elapsed / time.Duration(delta)
963+ }
964+ }
965+
966+ a.lastServerTick = tick
967+ a.lastServerTickAt = now
968+}
969+
970+func (a *Mysis) estimateTravelUntil(now time.Time, arrivalTick, currentTick int64, currentTickOK bool) time.Time {
971+ if currentTickOK {
972+ a.mu.RLock()
973+ tickDuration := a.tickDuration
974+ a.mu.RUnlock()
975+ if tickDuration > 0 && arrivalTick > currentTick {
976+ return now.Add(time.Duration(arrivalTick-currentTick) * tickDuration)
977+ }
978+ }
979+
980+ return now.Add(constants.WaitStateNudgeInterval)
981+}
982+
983+func (a *Mysis) estimateCooldownUntil(now time.Time, cooldownTicks int64) time.Time {
984+ a.mu.RLock()
985+ tickDuration := a.tickDuration
986+ a.mu.RUnlock()
987+
988+ if tickDuration > 0 && cooldownTicks > 0 {
989+ return now.Add(time.Duration(cooldownTicks) * tickDuration)
990+ }
991+
992+ return now.Add(constants.WaitStateNudgeInterval)
993+}
994+
995+func parseToolResultPayload(result *mcp.ToolResult) (interface{}, bool) {
996+ if result == nil {
997+ return nil, false
998+ }
999+
1000+ var texts []string
1001+ for _, block := range result.Content {
1002+ if block.Type == "text" {
1003+ texts = append(texts, block.Text)
1004+ }
1005+ }
1006+
1007+ content := strings.TrimSpace(strings.Join(texts, "\n"))
1008+ if content == "" {
1009+ return nil, false
1010+ }
1011+ if !strings.HasPrefix(content, "{") && !strings.HasPrefix(content, "[") {
1012+ return nil, false
1013+ }
1014+
1015+ decoder := json.NewDecoder(strings.NewReader(content))
1016+ decoder.UseNumber()
1017+ var payload interface{}
1018+ if err := decoder.Decode(&payload); err != nil {
1019+ return nil, false
1020+ }
1021+
1022+ return payload, true
1023+}
1024+
1025+func findIntField(payload interface{}, keys ...string) (int64, bool) {
1026+ switch value := payload.(type) {
1027+ case map[string]interface{}:
1028+ for _, key := range keys {
1029+ if raw, ok := value[key]; ok {
1030+ if number, ok := normalizeInt(raw); ok {
1031+ return number, true
1032+ }
1033+ }
1034+ }
1035+ for _, child := range value {
1036+ if number, ok := findIntField(child, keys...); ok {
1037+ return number, true
1038+ }
1039+ }
1040+ case []interface{}:
1041+ for _, child := range value {
1042+ if number, ok := findIntField(child, keys...); ok {
1043+ return number, true
1044+ }
1045+ }
1046+ }
1047+
1048+ return 0, false
1049+}
1050+
1051+func normalizeInt(value interface{}) (int64, bool) {
1052+ switch v := value.(type) {
1053+ case int:
1054+ return int64(v), true
1055+ case int64:
1056+ return v, true
1057+ case float64:
1058+ return int64(v), true
1059+ case json.Number:
1060+ number, err := v.Int64()
1061+ if err != nil {
1062+ return 0, false
1063+ }
1064+ return number, true
1065+ case string:
1066+ number, err := strconv.ParseInt(v, 10, 64)
1067+ if err != nil {
1068+ return 0, false
1069+ }
1070+ return number, true
1071+ default:
1072+ return 0, false
1073+ }
1074+}
1075+
8471076 func (a *Mysis) emitStateChange(oldState, newState MysisState) {
8481077 a.bus.Publish(Event{
8491078 Type: EventMysisStateChanged,