magpie icon indicating copy to clipboard operation
magpie copied to clipboard

TestGatewayModeListsNoOwnSessions: /api/sessions/said asked while the list is still being read (listing:true) finds nothing

Open yetone opened this issue 2 weeks ago • 5 comments

TestGatewayModeListsNoOwnSessions went red in a full go test -count=1 -timeout 45m ./internal/gui on mica (load ~38 on 12 cores) at origin/main 48fffb60c plus a tray-panel window-level change that doesn't touch sessions:

--- FAIL: TestGatewayModeListsNoOwnSessions (11.33s)
    sessions_gateway_mode_test.go:117: gateway mode off: every agent's search: {"hits":[],"indexed":0,"total":0,"listing":true}

Run alone right after (-run TestGatewayModeListsNoOwnSessions), it passed (14.6s).

The reply says "listing":true, "indexed":0: the search over every agent's sessions was asked while the list it searches was still being read, so it found nothing. The test asks once and expects hits; it should wait until listing is false (the state), not assume the list is read by the time it asks. Not taken by me.

yetone avatar Nov 18 '26 08:11 yetone

Seen in a full go test ./... -timeout 45m on mica at 998e40c85 (origin/main 48fffb60c + a Library change): sessions_gateway_mode_test.go:117: gateway mode off: every agent's search: {"hits":[],"indexed":0,"total":0,"listing":true}. Passes alone 3 of 3 at 998e40c85 and at origin/main 534b85bf9. Not taking it.

yetone avatar Nov 18 '26 08:11 yetone

Seen on mica at origin/main 48fffb60c: -run TestGatewayModeListsNoOwnSessions -count=6 1 of 6 red ("listing":true, no hits), and 3 of 6 on 16e6bee32 (gateway/provider only) at load ~22. Not taking it.

yetone avatar Nov 18 '26 09:11 yetone

Test-only candidate patch for this race, based on 2416beed69575c1e51e9c44a446e7b9c8d4e400f. Sharing the complete patch here under the community-fix path in docs/contributors.md; no external PR or fork commit has been created.

The test now waits for both the initial listing and indexing to finish, not for the expected hit. It then makes a fresh request and keeps the original assertions, so a completed search that loses the local session still fails. The extra read matters because searchSince selects keys before reading the index progress; a response crossing the completion boundary can report ready while containing the earlier hit snapshot. The minute limit only bounds a stuck test; it is not a fixed readiness sleep.

No production behavior, gateway privacy guard, dependency, or documentation contract changes. The gateway-mode assertions later in the same test are unchanged.

Reproduction and verification

Linux amd64, official Go 1.26.8, isolated temporary HOME with GOPATH/GOMODCACHE/GOCACHE pinned.

  • Untouched focused baseline: passed.
  • Controlled reproduction: temporarily inserted time.Sleep(3 * time.Second) at the start of (*saidIndex).refresh, before x.list(). With the original test, the focused run failed:
    --- FAIL: TestGatewayModeListsNoOwnSessions (3.04s)
        sessions_gateway_mode_test.go:117: gateway mode off: every agent's search: {"hits":[],"indexed":0,"total":0,"listing":true}
    FAIL github.com/yetone/magpie/internal/gui 3.122s
    
  • With the patch and the same delay, go test -tags nogui ./internal/gui -run '^TestGatewayModeListsNoOwnSessions$' -count=3: passed, 18.409s.
  • Removed the delay completely and confirmed that internal/sessions/said_index.go has no diff.
  • Final patch: go test -race -tags nogui ./internal/gui -run '^TestGatewayModeListsNoOwnSessions$' -count=20: passed, 9.405s.
  • go test -race -tags nogui ./internal/gui ./internal/sessions -run 'TestGatewayMode|TestSearchSaid|TestTranscriptAt|TestSaid' -shuffle=on -count=1: both packages passed (2.289s / 3.977s).
  • go vet -tags nogui ./internal/gui ./internal/sessions, gofmt -l on the changed file, and git diff --check: clean.
  • Separate read-only review found no substantive defect.

Not run: full-repository test/build/vet gates, macOS/Windows checks, or browser UI tests. This is a focused, verified candidate for maintainer review, not a claim of full release acceptance.

Root-cause credit: @yetone's report and comments. If adopting the patch, please follow the repository's reporter/patch-author credit rules.

Complete patch

diff --git a/internal/gui/sessions_gateway_mode_test.go b/internal/gui/sessions_gateway_mode_test.go
index ac5949a..a410ff9 100644
--- a/internal/gui/sessions_gateway_mode_test.go
+++ b/internal/gui/sessions_gateway_mode_test.go
@@ -112,7 +112,26 @@ func TestGatewayModeListsNoOwnSessions(t *testing.T) {
 		t.Fatalf("gateway mode off: the local transcript: %s", b)
 	}
 	// every agent's sessions searched at once, and one read where the
-	// words were said
+	// words were said. A search can return while its first listing or
+	// indexing is still running (#7064); wait for that state, not a hit,
+	// so a completed search that lost the local session still fails.
+	deadline := time.Now().Add(time.Minute)
+	for {
+		b := ok("GET", "/api/sessions/said?q=secret+plan", "")
+		var found sessions.SaidFound
+		if err := json.Unmarshal([]byte(b), &found); err != nil {
+			t.Fatal(err)
+		}
+		if !found.Listing && found.Indexed == found.Total {
+			break
+		}
+		if time.Now().After(deadline) {
+			t.Fatalf("gateway mode off: search still indexing: %s", b)
+		}
+		time.Sleep(10 * time.Millisecond)
+	}
+	// Read again after indexing: the previous response may have selected
+	// its hits before the last session finished being indexed.
 	if b := ok("GET", "/api/sessions/said?q=secret+plan", ""); !strings.Contains(b, `"id":"`+local+`"`) || !strings.Contains(b, `"agent":"claude"`) {
 		t.Fatalf("gateway mode off: every agent's search: %s", b)
 	}

AI-assisted implementation and verification.

yhdhaidong avatar Nov 18 '26 09:11 yhdhaidong

Seen in a full run on mica at 162527f08 (gateway mode off: every agent's search: {"hits":[],"indexed":0,"total":0,"listing":true}); passes alone there. Not taking it.

yetone avatar Nov 18 '26 11:11 yetone

Now red when run alone too, not only in a full run: on mica (load ~28 on 12 cores) at a clean copy of origin/main 38242a6ce, go test -tags nogui -count=2 -run TestGatewayModeListsNoOwnSessions ./internal/gui failed 2 of 2, and 3 of 3 more beside an unrelated change (Codex's applies to):

--- FAIL: TestGatewayModeListsNoOwnSessions (13.37s)
    sessions_gateway_mode_test.go:117: gateway mode off: every agent's search: {"hits":[],"indexed":0,"total":0,"listing":true}

So "passes alone" no longer holds on a busy machine. yhdhaidong's patch above (wait for listing done and indexed == total, then read again) addresses exactly this state. Not taken in the Codex change; it needs its own commit.

yetone avatar Nov 29 '26 18:11 yetone