Complete the Codex host initialization handshake once
This commit is contained in:
@@ -117,6 +117,11 @@ func Serve(ctx context.Context, options Options) error {
|
||||
if failure := h.initialize["error"]; len(failure) > 0 && string(failure) != "null" {
|
||||
return errors.New("Codex App Server refused initialization")
|
||||
}
|
||||
// Complete the upstream handshake once before accepting Pipelines clients.
|
||||
// Reattached clients only initialize their downstream connection.
|
||||
if err := h.write(upstream, message{"method": json.RawMessage(`"initialized"`)}); err != nil {
|
||||
return fmt.Errorf("complete Codex App Server initialization: %w", err)
|
||||
}
|
||||
server := &http.Server{Handler: http.HandlerFunc(h.accept), ReadHeaderTimeout: 5 * time.Second}
|
||||
defer server.Close()
|
||||
go func() { <-child.Done(); _ = server.Close() }()
|
||||
|
||||
@@ -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:
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,6 +54,9 @@ func TestEnsureSpawnsReusableDetachedHost(t *testing.T) {
|
||||
if wsjson.Read(ctx, conn, &msg) != nil {
|
||||
return
|
||||
}
|
||||
if method(msg) == "initialized" {
|
||||
continue
|
||||
}
|
||||
if err := wsjson.Write(ctx, conn, message{"id": msg["id"], "result": json.RawMessage(`{}`)}); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user