Complete the Codex host initialization handshake once

This commit is contained in:
2026-11-05 15:49:44 +00:00
parent 46f85556af
commit 1e886faa41
6 changed files with 62 additions and 9 deletions
+39 -6
View File
@@ -15,11 +15,12 @@ import (
)
type fakeServer struct {
ctx context.Context
conn *websocket.Conn
mu sync.Mutex
received chan message
ready chan struct{}
ctx context.Context
conn *websocket.Conn
mu sync.Mutex
received chan message
handshakes chan string
ready chan struct{}
}
func testHost(t *testing.T) (*fakeServer, string, context.CancelFunc, <-chan error) {
@@ -31,7 +32,7 @@ func testHost(t *testing.T) (*fakeServer, string, context.CancelFunc, <-chan err
t.Cleanup(func() { os.RemoveAll(dir) })
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
fake := &fakeServer{ctx: ctx, received: make(chan message, 64), ready: make(chan struct{})}
fake := &fakeServer{ctx: ctx, received: make(chan message, 64), handshakes: make(chan string, 8), ready: make(chan struct{})}
listener, err := net.Listen("unix", filepath.Join(dir, "upstream.sock"))
if err != nil {
t.Fatal(err)
@@ -58,7 +59,10 @@ func testHost(t *testing.T) (*fakeServer, string, context.CancelFunc, <-chan err
var method string
_ = json.Unmarshal(msg["method"], &method)
if method == "initialize" {
fake.handshakes <- method
fake.send(t, message{"id": msg["id"], "result": json.RawMessage(`{"userAgent":"fixture"}`)})
} else if method == "initialized" {
fake.handshakes <- method
} else {
select {
case fake.received <- msg:
@@ -239,3 +243,32 @@ func TestResolvedNotificationClearsOnlyMatchingThread(t *testing.T) {
t.Fatalf("resolved callback replayed: %s", got)
}
}
func TestBridgeCompletesUpstreamHandshakeExactlyOnce(t *testing.T) {
fake, socket, _, _ := testHost(t)
first := attach(t, socket)
writeClient(t, first, `{"method":"initialized"}`)
writeClient(t, first, `{"method":"initialized"}`)
first.CloseNow()
second := attach(t, socket)
writeClient(t, second, `{"method":"initialized"}`)
writeClient(t, second, `{"id":2,"method":"barrier"}`)
if got := fake.next(t); method(got) != "barrier" {
t.Fatalf("unexpected upstream call: %s", got)
}
for _, want := range []string{"initialize", "initialized"} {
select {
case got := <-fake.handshakes:
if got != want {
t.Fatalf("handshake order: got %s, want %s", got, want)
}
case <-time.After(time.Second):
t.Fatalf("missing upstream %s", want)
}
}
select {
case got := <-fake.handshakes:
t.Fatalf("upstream handshake repeated after downstream reconnect: %s", got)
default:
}
}