@@ -183,25 +183,28 @@ func (m *Mysis) Start() error {
183183 }
184184
185185 oldState := a.state
186+ a.mu.Unlock()
187+
188+ // Create context first (before any state changes)
189+ ctx, cancel := context.WithCancel(context.Background())
190+
191+ // Update store BEFORE changing in-memory state
192+ // This ensures we don't start the goroutine if persistence fails
193+ if err := a.store.UpdateMysisState(a.id, store.MysisStateRunning); err != nil {
194+ cancel() // Clean up context since we're not starting
195+ return fmt.Errorf("failed to update state in store: %w", err)
196+ }
197+
198+ // Now that store update succeeded, update in-memory state
199+ a.mu.Lock()
186200 a.state = MysisStateRunning
187201 a.lastError = nil
188202 a.activityState = ActivityStateIdle
189203 a.activityUntil = time.Time{}
190-
191- ctx, cancel := context.WithCancel(context.Background())
192204 a.ctx = ctx
193205 a.cancel = cancel
194206 a.mu.Unlock()
195207
196- // Update store
197- if err := a.store.UpdateMysisState(a.id, store.MysisStateRunning); err != nil {
198- a.mu.Lock()
199- a.state = MysisStateErrored
200- a.lastError = err
201- a.mu.Unlock()
202- return err
203- }
204-
205208 // Add system prompt if this is the first time starting (no memories yet)
206209 count, err := a.store.CountMemories(a.id)
207210 if err == nil && count == 0 {
@@ -212,7 +215,7 @@ func (m *Mysis) Start() error {
212215 // Emit state change event
213216 a.emitStateChange(oldState, MysisStateRunning)
214217
215- // Start the processing goroutine
218+ // Start the processing goroutine (only after all setup succeeded)
216219 go a.run(ctx)
217220
218221 // Trigger initial turn to encourage autonomy
@@ -235,9 +238,21 @@ func (m *Mysis) Stop() error {
235238 }
236239 a.mu.Unlock()
237240
238- // Wait for current turn to finish
239- a.turnMu.Lock()
240- defer a.turnMu.Unlock()
241+ // Wait for current turn to finish with timeout
242+ done := make(chan struct{})
243+ go func() {
244+ a.turnMu.Lock()
245+ close(done)
246+ a.turnMu.Unlock()
247+ }()
248+
249+ select {
250+ case <-done:
251+ // Turn finished successfully
252+ case <-time.After(5 * time.Second):
253+ log.Warn().Str("mysis", a.name).Msg("Stop timeout - forcing shutdown")
254+ // Continue with cleanup even if turn didn't complete
255+ }
241256
242257 a.mu.Lock()
243258 if a.state != MysisStateRunning {
@@ -751,13 +766,25 @@ func (m *Mysis) formatToolResultDisplay(result *mcp.ToolResult, err error) strin
751766 return content
752767 }
753768
754-// setError sets the mysis last error and emits an error event.
769+// setError sets the mysis last error, transitions to errored state, and emits events.
770+// This method ensures proper state machine compliance by transitioning to MysisStateErrored.
755771 func (m *Mysis) setError(err error) {
756772 a := m
757773 a.mu.Lock()
774+ oldState := a.state
758775 a.lastError = err
776+ a.state = MysisStateErrored
759777 a.mu.Unlock()
760778
779+ // Update store
780+ if updateErr := a.store.UpdateMysisState(a.id, store.MysisStateErrored); updateErr != nil {
781+ log.Warn().Err(updateErr).Str("mysis", a.id).Msg("Failed to update state in store")
782+ }
783+
784+ // Emit state change event
785+ a.emitStateChange(oldState, MysisStateErrored)
786+
787+ // Emit error event
761788 a.publishCriticalEvent(Event{
762789 Type: EventMysisError,
763790 MysisID: a.id,