From 0c4ca0e4f96b6bca350c6de9a94db1584d2671f2 Mon Sep 17 00:00:00 2001 From: Jonas Date: Thu, 3 Sep 2026 23:44:38 +0200 Subject: [PATCH] fix: eating stew --- .../cmd/westworldwithmessaging/main.go | 2 +- .../state_wifes_global_state.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/chapter-2-state-driven-agent-design/cmd/westworldwithmessaging/main.go b/chapter-2-state-driven-agent-design/cmd/westworldwithmessaging/main.go index d6fa96e..c77956b 100644 --- a/chapter-2-state-driven-agent-design/cmd/westworldwithmessaging/main.go +++ b/chapter-2-state-driven-agent-design/cmd/westworldwithmessaging/main.go @@ -22,7 +22,7 @@ func main() { elsa := westworldwithmessaging.NewElsa(2) const ( - numUpdateSteps = 40 + numUpdateSteps = 100 tickDelay = 100 * time.Millisecond ) diff --git a/chapter-2-state-driven-agent-design/westworldwithmessaging/state_wifes_global_state.go b/chapter-2-state-driven-agent-design/westworldwithmessaging/state_wifes_global_state.go index 5b7518b..d05a7ac 100644 --- a/chapter-2-state-driven-agent-design/westworldwithmessaging/state_wifes_global_state.go +++ b/chapter-2-state-driven-agent-design/westworldwithmessaging/state_wifes_global_state.go @@ -55,9 +55,26 @@ func (s *wifesGlobalState) Execute(e *Elsa) { // specifically because she should react to it "when in any state," // same reasoning as why the bathroom-call check above lives in the // global state rather than in DoHousework. +// +// One guard we added that the book's snippet didn't show: if Bob +// comes home again while a stew is already in the oven (his fatigue +// or thirst can send him home more than once before her 1.5-second +// cooking timer fires), this used to unconditionally call +// ChangeState(CookStew()) even though she was already IN CookStew. +// That triggered a pointless Exit-then-Enter round trip on the same +// state - CookStew.Exit fired (printing "Puttin' the stew on the +// table" while the stew was still cooking!) and then Enter's +// "!e.Cooking()" check correctly skipped starting a second stew, so +// nothing useful happened, just a confusing extra print. Checking +// Cooking() first avoids the whole misfire. func (s *wifesGlobalState) OnMessage(e *Elsa, msg Telegram) bool { switch MessageType(msg.Msg) { case MsgHiHoneyImHome: + if e.Cooking() { + // already got dinner covered - nothing new to do + return true + } + fmt.Printf("\nMessage handled by %s at time: %.5f", GetNameOfEntity(e.ID()), Clock.GetCurrentTime()) fmt.Printf("\n%s: Hi honey. Let me make you some of mah fine country stew", GetNameOfEntity(e.ID())) e.FSM().ChangeState(CookStew())