From 8b2b22b4328e1d59d04fc042c630f5aad640fea9 Mon Sep 17 00:00:00 2001 From: geobelsky Date: Tue, 24 Mar 2026 13:11:12 +0000 Subject: [PATCH] fix: start agents before submitting intent to avoid SSE race MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The SSE init phase skips all existing events to find the cursor. When the intent was created BEFORE agents started listening, the init phase would skip past the delivery event — causing the agent to never process it. This was especially visible on macOS where network latency to the cloud server is higher. Now agents start listening (step 3) before scenarios apply (step 4), with a 500ms settle time to ensure SSE connections are established. --- cmd/axme/examples.go | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/cmd/axme/examples.go b/cmd/axme/examples.go index a60d81e..e84bc3f 100644 --- a/cmd/axme/examples.go +++ b/cmd/axme/examples.go @@ -220,9 +220,23 @@ func (rt *runtime) runExample(cmd *cobra.Command, exampleID string) error { fmt.Printf(" ✓ %d agent(s) provisioned\n", len(example.Agents)) } - // Step 3: Apply scenario (create intent via server-side apply) + // Step 3: Start built-in Go agents BEFORE submitting — so they are + // already listening when the intent is delivered (avoids race where + // the SSE init phase skips the new intent on high-latency connections). + if len(example.Agents) > 0 { + fmt.Println() + for i, agent := range example.Agents { + fmt.Printf(" [step 3] Starting agent %d/%d (%s)...\n", i+1, len(example.Agents), agent.Addr) + go rt.runBuiltinAgent(ctx, c, &agent) + } + fmt.Printf(" ✓ %d agent(s) listening\n", len(example.Agents)) + // Give agents a moment to connect SSE before submitting intent + time.Sleep(500 * time.Millisecond) + } + + // Step 4: Apply scenario (create intent via server-side apply) fmt.Println() - fmt.Println(" [step 3] Submitting scenario...") + fmt.Println(" [step 4] Submitting scenario...") applyStatus, applyBody, _, applyErr := rt.request(ctx, c, "POST", "/v1/scenarios/apply", nil, scenario, true) if applyErr != nil { @@ -237,17 +251,7 @@ func (rt *runtime) runExample(cmd *cobra.Command, exampleID string) error { } fmt.Printf(" ✓ Intent created: %s\n", intentID) - // Step 4: Start built-in Go agents - if len(example.Agents) > 0 { - fmt.Println() - for i, agent := range example.Agents { - fmt.Printf(" [step 4] Starting agent %d/%d (%s)...\n", i+1, len(example.Agents), agent.Addr) - go rt.runBuiltinAgent(ctx, c, &agent) - } - fmt.Printf(" ✓ %d agent(s) listening\n", len(example.Agents)) - } - - // Step 4: Watch intent lifecycle + // Step 5: Watch intent lifecycle return watchIntentLive(rt, cmd, intentID, nil) }