@@ -4,6 +4,7 @@ import (
44 "context"
55 "encoding/json"
66 "fmt"
7+ "sort"
78 "strconv"
89 "strings"
910 "sync"
@@ -43,6 +44,7 @@ type Mysis struct {
4344 lastServerTick int64
4445 lastServerTickAt time.Time
4546 tickDuration time.Duration
47+ nudgeCh chan struct{}
4648 }
4749
4850 type contextStats struct {
@@ -66,6 +68,7 @@ func NewMysis(id, name string, createdAt time.Time, p provider.Provider, s *stor
6668 bus: bus,
6769 state: MysisStateIdle,
6870 activityState: ActivityStateIdle,
71+ nudgeCh: make(chan struct{}, 1),
6972 }
7073 }
7174
@@ -212,7 +215,7 @@ func (a *Mysis) Stop() error {
212215 a.mu.Lock()
213216 if a.state != MysisStateRunning {
214217 a.mu.Unlock()
215- return fmt.Errorf("mysis not running")
218+ return nil
216219 }
217220
218221 if a.cancel != nil {
@@ -225,6 +228,10 @@ func (a *Mysis) Stop() error {
225228 defer a.turnMu.Unlock()
226229
227230 a.mu.Lock()
231+ if a.state != MysisStateRunning {
232+ a.mu.Unlock()
233+ return nil
234+ }
228235 a.cancel = nil
229236 a.ctx = nil
230237
@@ -298,7 +305,7 @@ func (a *Mysis) SendMessageFrom(content string, source store.MemorySource, sende
298305 mcpTools, err := mcpProxy.ListTools(ctx)
299306 if err != nil {
300307 // Log error but continue - mysis can still chat without tools
301- a.bus.Publish(Event{
308+ a.publishCriticalEvent(Event{
302309 Type: EventMysisError,
303310 MysisID: a.id,
304311 MysisName: a.name,
@@ -328,7 +335,7 @@ func (a *Mysis) SendMessageFrom(content string, source store.MemorySource, sende
328335 }
329336 }
330337 } else {
331- a.bus.Publish(Event{
338+ a.publishCriticalEvent(Event{
332339 Type: EventMysisError,
333340 MysisID: a.id,
334341 MysisName: a.name,
@@ -693,7 +700,7 @@ func (a *Mysis) setError(err error) {
693700 a.lastError = err
694701 a.mu.Unlock()
695702
696- a.bus.Publish(Event{
703+ a.publishCriticalEvent(Event{
697704 Type: EventMysisError,
698705 MysisID: a.id,
699706 MysisName: a.name,
@@ -845,12 +852,17 @@ func (a *Mysis) run(ctx context.Context) {
845852 a.mu.RUnlock()
846853
847854 if isRunning && a.shouldNudge(time.Now()) {
848- // Only nudge if not already in a turn
849- if a.turnMu.TryLock() {
850- a.turnMu.Unlock()
851- go a.SendMessage(a.buildContinuePrompt(), store.MemorySourceSystem)
855+ select {
856+ case a.nudgeCh <- struct{}{}:
857+ default:
852858 }
853859 }
860+ case <-a.nudgeCh:
861+ if !a.turnMu.TryLock() {
862+ continue
863+ }
864+ a.turnMu.Unlock()
865+ go a.SendMessage(a.buildContinuePrompt(), store.MemorySourceSystem)
854866 }
855867 }
856868 }
@@ -964,7 +976,7 @@ func (a *Mysis) updateActivityFromToolResult(toolName string, result *mcp.ToolRe
964976 var currentTick int64
965977 var currentTickOK bool
966978 if ok {
967- currentTick, currentTickOK = findIntField(payload, "current_tick", "tick")
979+ currentTick, currentTickOK = findCurrentTick(payload)
968980 if currentTickOK {
969981 a.updateServerTick(now, currentTick)
970982 }
@@ -1089,23 +1101,69 @@ func parseToolResultPayload(result *mcp.ToolResult) (interface{}, bool) {
10891101 }
10901102
10911103 func findIntField(payload interface{}, keys ...string) (int64, bool) {
1092- switch value := payload.(type) {
1093- case map[string]interface{}:
1094- for _, key := range keys {
1095- if raw, ok := value[key]; ok {
1096- if number, ok := normalizeInt(raw); ok {
1097- return number, true
1104+ queue := []interface{}{payload}
1105+
1106+ for len(queue) > 0 {
1107+ current := queue[0]
1108+ queue = queue[1:]
1109+
1110+ switch value := current.(type) {
1111+ case map[string]interface{}:
1112+ for _, key := range keys {
1113+ if raw, ok := value[key]; ok {
1114+ if number, ok := normalizeInt(raw); ok {
1115+ return number, true
1116+ }
10981117 }
10991118 }
1100- }
1101- for _, child := range value {
1102- if number, ok := findIntField(child, keys...); ok {
1103- return number, true
1119+
1120+ childKeys := make([]string, 0, len(value))
1121+ for key := range value {
1122+ childKeys = append(childKeys, key)
1123+ }
1124+ sort.Strings(childKeys)
1125+ for _, key := range childKeys {
1126+ queue = append(queue, value[key])
11041127 }
1128+ case []interface{}:
1129+ queue = append(queue, value...)
11051130 }
1106- case []interface{}:
1107- for _, child := range value {
1108- if number, ok := findIntField(child, keys...); ok {
1131+ }
1132+
1133+ return 0, false
1134+}
1135+
1136+func findCurrentTick(payload interface{}) (int64, bool) {
1137+ if number, ok := findIntFieldAtKeys(payload, "current_tick"); ok {
1138+ return number, true
1139+ }
1140+ if number, ok := findIntFieldInWrappers(payload, "current_tick"); ok {
1141+ return number, true
1142+ }
1143+ if number, ok := findIntField(payload, "current_tick"); ok {
1144+ return number, true
1145+ }
1146+ if number, ok := findIntFieldAtKeys(payload, "tick"); ok {
1147+ return number, true
1148+ }
1149+ if number, ok := findIntFieldInWrappers(payload, "tick"); ok {
1150+ return number, true
1151+ }
1152+ if number, ok := findIntField(payload, "tick"); ok {
1153+ return number, true
1154+ }
1155+ return 0, false
1156+}
1157+
1158+func findIntFieldAtKeys(payload interface{}, keys ...string) (int64, bool) {
1159+ value, ok := payload.(map[string]interface{})
1160+ if !ok {
1161+ return 0, false
1162+ }
1163+
1164+ for _, key := range keys {
1165+ if raw, ok := value[key]; ok {
1166+ if number, ok := normalizeInt(raw); ok {
11091167 return number, true
11101168 }
11111169 }
@@ -1114,6 +1172,26 @@ func findIntField(payload interface{}, keys ...string) (int64, bool) {
11141172 return 0, false
11151173 }
11161174
1175+func findIntFieldInWrappers(payload interface{}, key string) (int64, bool) {
1176+ value, ok := payload.(map[string]interface{})
1177+ if !ok {
1178+ return 0, false
1179+ }
1180+
1181+ wrappers := []string{"data", "result", "payload", "response"}
1182+ for _, wrapper := range wrappers {
1183+ child, ok := value[wrapper]
1184+ if !ok {
1185+ continue
1186+ }
1187+ if number, ok := findIntField(child, key); ok {
1188+ return number, true
1189+ }
1190+ }
1191+
1192+ return 0, false
1193+}
1194+
11171195 func normalizeInt(value interface{}) (int64, bool) {
11181196 switch v := value.(type) {
11191197 case int:
@@ -1140,7 +1218,7 @@ func normalizeInt(value interface{}) (int64, bool) {
11401218 }
11411219
11421220 func (a *Mysis) emitStateChange(oldState, newState MysisState) {
1143- a.bus.Publish(Event{
1221+ a.publishCriticalEvent(Event{
11441222 Type: EventMysisStateChanged,
11451223 MysisID: a.id,
11461224 MysisName: a.name,
@@ -1159,10 +1237,16 @@ func (a *Mysis) setErrorState(err error) {
11591237 a.lastError = err
11601238 a.mu.Unlock()
11611239
1162- a.store.UpdateMysisState(a.id, store.MysisStateErrored)
1240+ if err := a.store.UpdateMysisState(a.id, store.MysisStateErrored); err != nil {
1241+ log.Warn().
1242+ Err(err).
1243+ Str("mysis_id", a.id).
1244+ Str("mysis_name", a.name).
1245+ Msg("failed to update mysis state to errored")
1246+ }
11631247 a.emitStateChange(oldState, MysisStateErrored)
11641248
1165- a.bus.Publish(Event{
1249+ a.publishCriticalEvent(Event{
11661250 Type: EventMysisError,
11671251 MysisID: a.id,
11681252 MysisName: a.name,
@@ -1170,3 +1254,15 @@ func (a *Mysis) setErrorState(err error) {
11701254 Timestamp: time.Now(),
11711255 })
11721256 }
1257+
1258+func (a *Mysis) publishCriticalEvent(event Event) {
1259+ if a.bus.PublishBlocking(event, constants.EventBusPublishTimeout) {
1260+ return
1261+ }
1262+
1263+ log.Warn().
1264+ Str("event_type", string(event.Type)).
1265+ Str("mysis_id", a.id).
1266+ Str("mysis_name", a.name).
1267+ Msg("event bus publish timeout")
1268+}