Retain Codex interaction callbacks across server restarts
This commit is contained in:
@@ -0,0 +1,260 @@
|
||||
// Package codexhost keeps the Codex App Server client connection alive across
|
||||
// Pipelines restarts. It never starts another Codex daemon. Callback identity
|
||||
// belongs to this upstream connection, so unanswered requests remain in memory
|
||||
// here until the next Pipelines client attaches and answers them.
|
||||
package codexhost
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
)
|
||||
|
||||
const maxFrame = 16 << 20
|
||||
const maxPending = 128
|
||||
const maxPendingBytes = 8 << 20
|
||||
const maxCalls = 256
|
||||
|
||||
type message map[string]json.RawMessage
|
||||
|
||||
type downstream struct {
|
||||
conn *websocket.Conn
|
||||
ready bool
|
||||
}
|
||||
type route struct {
|
||||
client *downstream
|
||||
id json.RawMessage
|
||||
}
|
||||
type pending struct {
|
||||
data []byte
|
||||
thread string
|
||||
}
|
||||
|
||||
type bridge struct {
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
upstream *websocket.Conn
|
||||
mu sync.Mutex // serializes routing and all websocket writes
|
||||
active *downstream
|
||||
next uint64
|
||||
calls map[string]route
|
||||
pending map[string]pending
|
||||
pendingBytes int
|
||||
initialize message
|
||||
initialized chan struct{}
|
||||
detached time.Time
|
||||
}
|
||||
|
||||
// Options describes one private, database-scoped bridge. MaxDetached bounds its
|
||||
// lifetime without a Pipelines client, even with an unresolved approval.
|
||||
type Options struct {
|
||||
Dir, Upstream string
|
||||
Idle, MaxDetached time.Duration
|
||||
Ready func(string)
|
||||
}
|
||||
|
||||
// Serve owns one upstream socket and serves replacement downstream clients until
|
||||
// cancellation, upstream failure or detached expiry. A singleton lock prevents
|
||||
// competing hosts from taking the same socket path.
|
||||
func Serve(ctx context.Context, options Options) error {
|
||||
if err := EnsureDir(options.Dir); err != nil {
|
||||
return err
|
||||
}
|
||||
socket, lockPath := Paths(options.Dir, options.Upstream)
|
||||
lock, err := os.OpenFile(lockPath, os.O_CREATE|os.O_RDWR, 0o600)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer lock.Close()
|
||||
if err := syscall.Flock(int(lock.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil {
|
||||
return fmt.Errorf("Codex host already owns this scope: %w", err)
|
||||
}
|
||||
defer syscall.Flock(int(lock.Fd()), syscall.LOCK_UN)
|
||||
if err := os.Remove(socket); err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
listener, err := net.Listen("unix", socket)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer listener.Close()
|
||||
defer os.Remove(socket)
|
||||
if err := os.Chmod(socket, 0o600); err != nil {
|
||||
return err
|
||||
}
|
||||
upstream, err := Dial(ctx, options.Upstream)
|
||||
if err != nil {
|
||||
return fmt.Errorf("connect Codex App Server: %w", err)
|
||||
}
|
||||
defer upstream.CloseNow()
|
||||
child, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
h := &bridge{ctx: child, cancel: cancel, upstream: upstream, calls: map[string]route{}, pending: map[string]pending{}, initialized: make(chan struct{}), detached: time.Now()}
|
||||
go h.readUpstream()
|
||||
init := message{"id": json.RawMessage(`"host-initialize"`), "method": json.RawMessage(`"initialize"`), "params": json.RawMessage(`{"clientInfo":{"name":"pipelines","title":"Pipelines","version":"0.1"}}`)}
|
||||
if err := h.write(upstream, init); err != nil {
|
||||
return err
|
||||
}
|
||||
timer := time.NewTimer(10 * time.Second)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-h.initialized:
|
||||
case <-child.Done():
|
||||
return errors.New("Codex App Server disconnected during initialization")
|
||||
case <-timer.C:
|
||||
return errors.New("Codex App Server initialization timed out")
|
||||
}
|
||||
if failure := h.initialize["error"]; len(failure) > 0 && string(failure) != "null" {
|
||||
return errors.New("Codex App Server refused initialization")
|
||||
}
|
||||
server := &http.Server{Handler: http.HandlerFunc(h.accept), ReadHeaderTimeout: 5 * time.Second}
|
||||
defer server.Close()
|
||||
go func() { <-child.Done(); _ = server.Close() }()
|
||||
if options.Idle <= 0 {
|
||||
options.Idle = 20 * time.Minute
|
||||
}
|
||||
if options.MaxDetached <= 0 {
|
||||
options.MaxDetached = 24 * time.Hour
|
||||
}
|
||||
go h.expire(options.Idle, options.MaxDetached)
|
||||
if options.Ready != nil {
|
||||
options.Ready(socket)
|
||||
}
|
||||
err = server.Serve(listener)
|
||||
h.mu.Lock()
|
||||
if h.active != nil {
|
||||
h.active.conn.CloseNow()
|
||||
}
|
||||
h.mu.Unlock()
|
||||
if err == http.ErrServerClosed {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (h *bridge) write(conn *websocket.Conn, msg message) error {
|
||||
data, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return h.writeRaw(conn, data)
|
||||
}
|
||||
func (h *bridge) writeRaw(conn *websocket.Conn, data []byte) error {
|
||||
ctx, cancel := context.WithTimeout(h.ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
return conn.Write(ctx, websocket.MessageText, data)
|
||||
}
|
||||
func (h *bridge) send(client *downstream, msg message) {
|
||||
if err := h.write(client.conn, msg); err != nil {
|
||||
client.conn.CloseNow()
|
||||
}
|
||||
}
|
||||
func (h *bridge) accept(w http.ResponseWriter, r *http.Request) {
|
||||
conn, err := websocket.Accept(w, r, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
conn.SetReadLimit(maxFrame)
|
||||
client := &downstream{conn: conn}
|
||||
defer conn.CloseNow()
|
||||
h.mu.Lock()
|
||||
if h.active != nil {
|
||||
h.active.conn.CloseNow()
|
||||
}
|
||||
// A previous client's in-flight RPC responses are never delivered to its
|
||||
// replacement, even when both start their request counters at one.
|
||||
h.calls = map[string]route{}
|
||||
h.active = client
|
||||
h.mu.Unlock()
|
||||
defer func() {
|
||||
h.mu.Lock()
|
||||
if h.active == client {
|
||||
h.active = nil
|
||||
h.detached = time.Now()
|
||||
h.calls = map[string]route{}
|
||||
}
|
||||
h.mu.Unlock()
|
||||
}()
|
||||
for {
|
||||
_, data, err := conn.Read(h.ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var msg message
|
||||
if json.Unmarshal(data, &msg) != nil {
|
||||
return
|
||||
}
|
||||
h.mu.Lock()
|
||||
if h.active != client {
|
||||
h.mu.Unlock()
|
||||
return
|
||||
}
|
||||
err = h.fromClient(client, msg)
|
||||
h.mu.Unlock()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (h *bridge) expire(idle, maxDetached time.Duration) {
|
||||
ticker := time.NewTicker(time.Second)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-h.ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
h.mu.Lock()
|
||||
expired := h.active == nil && (time.Since(h.detached) >= maxDetached || (len(h.pending) == 0 && time.Since(h.detached) >= idle))
|
||||
h.mu.Unlock()
|
||||
if expired {
|
||||
h.cancel()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dial opens a WebSocket over an existing local Unix control socket.
|
||||
func Dial(ctx context.Context, socket string) (*websocket.Conn, error) {
|
||||
transport := &http.Transport{DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
||||
return (&net.Dialer{}).DialContext(ctx, "unix", socket)
|
||||
}}
|
||||
conn, _, err := websocket.Dial(ctx, "ws://localhost/", &websocket.DialOptions{HTTPClient: &http.Client{Transport: transport}})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
conn.SetReadLimit(maxFrame)
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func EnsureDir(dir string) error {
|
||||
if !filepath.IsAbs(dir) {
|
||||
return errors.New("Codex host directory must be absolute")
|
||||
}
|
||||
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||
return err
|
||||
}
|
||||
info, err := os.Lstat(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
|
||||
return errors.New("Codex host directory is not a real directory")
|
||||
}
|
||||
if owner, ok := info.Sys().(*syscall.Stat_t); !ok || owner.Uid != uint32(os.Geteuid()) {
|
||||
return errors.New("Codex host directory has another owner")
|
||||
}
|
||||
return os.Chmod(dir, 0o700)
|
||||
}
|
||||
Reference in New Issue
Block a user