Retain Codex interaction callbacks across server restarts
This commit is contained in:
@@ -4,7 +4,8 @@
|
||||
// normally reached over a Unix WebSocket socket (the desktop app and the CLI use
|
||||
// the same control socket). This adapter owns one client connection and mirrors
|
||||
// the protocol into the Pipelines chat frames; it never starts a second Codex
|
||||
// daemon or changes the operator's Codex configuration.
|
||||
// daemon or changes the operator's Codex configuration. Installed servers use
|
||||
// a private connection host to retain pending interactions during restarts.
|
||||
package codex
|
||||
|
||||
import (
|
||||
@@ -116,6 +117,11 @@ func Open(ctx context.Context) (*Chat, error) {
|
||||
if socket == "" {
|
||||
return nil, errors.New("Codex home is unavailable")
|
||||
}
|
||||
var socketErr error
|
||||
socket, socketErr = connectionSocket(ctx, socket)
|
||||
if socketErr != nil {
|
||||
return nil, fmt.Errorf("attach Codex connection host: %w", socketErr)
|
||||
}
|
||||
transport := &http.Transport{
|
||||
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
||||
return (&net.Dialer{}).DialContext(ctx, "unix", socket)
|
||||
@@ -180,8 +186,8 @@ func (c *Chat) Close() {
|
||||
// in the Codex App Server, not in this client, so closing the control
|
||||
// connection leaves them running; no turn is interrupted. The next server
|
||||
// process reconnects and reconciles each thread through thread/read. An
|
||||
// approval or question the App Server sent to this connection is bound to it,
|
||||
// so one still pending at shutdown is not answerable after the restart.
|
||||
// approval or question is retained by the configured connection host and
|
||||
// replayed on reattach. Direct mode cannot retain connection-bound callbacks.
|
||||
func (c *Chat) Detach() { c.Close() }
|
||||
|
||||
func (c *Chat) readLoop() {
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package codex
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/codexhost"
|
||||
)
|
||||
|
||||
var hostConfig struct {
|
||||
sync.Mutex
|
||||
dir, binary string
|
||||
}
|
||||
|
||||
// ConfigureHosts enables a private detachable App Server connection bridge for
|
||||
// new Chats. Empty dir or PIPELINES_CODEX_HOST=off keeps direct mode for tests
|
||||
// and installations that intentionally disable this facility.
|
||||
func ConfigureHosts(dir string) {
|
||||
hostConfig.Lock()
|
||||
defer hostConfig.Unlock()
|
||||
hostConfig.dir, hostConfig.binary = "", ""
|
||||
if strings.TrimSpace(dir) == "" {
|
||||
return
|
||||
}
|
||||
binary := strings.TrimSpace(os.Getenv("PIPELINES_CODEX_HOST"))
|
||||
if binary == "off" {
|
||||
return
|
||||
}
|
||||
if binary == "" {
|
||||
if executable, err := os.Executable(); err == nil {
|
||||
candidate := filepath.Join(filepath.Dir(executable), "pipelines-codex-host")
|
||||
if info, err := os.Stat(candidate); err == nil && !info.IsDir() {
|
||||
binary = candidate
|
||||
}
|
||||
}
|
||||
}
|
||||
if binary == "" {
|
||||
binary, _ = exec.LookPath("pipelines-codex-host")
|
||||
}
|
||||
if binary == "" {
|
||||
log.Print("codex chat: pipelines-codex-host was not found; pending approvals and questions cannot survive a server restart")
|
||||
return
|
||||
}
|
||||
hostConfig.dir, hostConfig.binary = dir, binary
|
||||
}
|
||||
|
||||
// DefaultHostDir scopes bridge ownership to the server's database identity.
|
||||
func DefaultHostDir(scope string) string {
|
||||
base, err := os.UserCacheDir()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
if absolute, err := filepath.Abs(scope); err == nil {
|
||||
scope = absolute
|
||||
}
|
||||
sum := sha256.Sum256([]byte(scope))
|
||||
return filepath.Join(base, "pipelines", "codex-hosts", hex.EncodeToString(sum[:6]))
|
||||
}
|
||||
func connectionSocket(ctx context.Context, upstream string) (string, error) {
|
||||
hostConfig.Lock()
|
||||
dir, binary := hostConfig.dir, hostConfig.binary
|
||||
hostConfig.Unlock()
|
||||
if dir == "" {
|
||||
return upstream, nil
|
||||
}
|
||||
return codexhost.Ensure(ctx, binary, dir, upstream)
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
package codex
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/codexhost"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"github.com/coder/websocket"
|
||||
"github.com/coder/websocket/wsjson"
|
||||
)
|
||||
|
||||
// Exercise the actual adapter's approval/form maps across a complete Chat
|
||||
// replacement, not merely a proxy socket reconnect.
|
||||
func TestHostedDetachRetainsAnswerableInteractions(t *testing.T) {
|
||||
dir, err := os.MkdirTemp("/tmp", "codex-adapter-host-")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { os.RemoveAll(dir) })
|
||||
t.Setenv("CODEX_HOME", dir)
|
||||
if err := os.MkdirAll(filepath.Dir(DefaultSocketPath()), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
listener, err := net.Listen("unix", DefaultSocketPath())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var mu sync.Mutex
|
||||
var upstream *websocket.Conn
|
||||
var initialized atomic.Int32
|
||||
responses := make(chan rpcMessage, 8)
|
||||
readyUpstream := make(chan struct{})
|
||||
server := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
conn, err := websocket.Accept(w, r, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
mu.Lock()
|
||||
upstream = conn
|
||||
mu.Unlock()
|
||||
close(readyUpstream)
|
||||
defer conn.CloseNow()
|
||||
for {
|
||||
var msg rpcMessage
|
||||
if wsjson.Read(ctx, conn, &msg) != nil {
|
||||
return
|
||||
}
|
||||
if msg.Method == "" {
|
||||
responses <- msg
|
||||
continue
|
||||
}
|
||||
if msg.Method == "initialize" {
|
||||
initialized.Add(1)
|
||||
}
|
||||
mu.Lock()
|
||||
err = wsjson.Write(ctx, conn, map[string]any{"id": msg.ID, "result": map[string]any{}})
|
||||
mu.Unlock()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
})}
|
||||
go server.Serve(listener)
|
||||
t.Cleanup(func() {
|
||||
server.Close()
|
||||
mu.Lock()
|
||||
if upstream != nil {
|
||||
upstream.CloseNow()
|
||||
}
|
||||
mu.Unlock()
|
||||
})
|
||||
hostDir := filepath.Join(dir, "private")
|
||||
ready := make(chan string, 1)
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- codexhost.Serve(ctx, codexhost.Options{Dir: hostDir, Upstream: DefaultSocketPath(), Ready: func(socket string) { ready <- socket }})
|
||||
}()
|
||||
select {
|
||||
case <-ready:
|
||||
case err := <-done:
|
||||
t.Fatalf("host start: %v", err)
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("host not ready")
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
cancel()
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Error("host did not close")
|
||||
}
|
||||
})
|
||||
hostConfig.Lock()
|
||||
oldDir, oldBinary := hostConfig.dir, hostConfig.binary
|
||||
hostConfig.dir, hostConfig.binary = hostDir, "unused-existing-host"
|
||||
hostConfig.Unlock()
|
||||
t.Cleanup(func() { hostConfig.Lock(); hostConfig.dir, hostConfig.binary = oldDir, oldBinary; hostConfig.Unlock() })
|
||||
before, err := Open(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(before.Close)
|
||||
send := func(raw string) {
|
||||
t.Helper()
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if err := upstream.Write(ctx, websocket.MessageText, []byte(raw)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
send(`{"id":7,"method":"item/commandExecution/requestApproval","params":{"threadId":"thread-a","itemId":"command","command":"pwd"}}`)
|
||||
send(`{"id":8,"method":"item/tool/requestUserInput","params":{"threadId":"thread-a","itemId":"question","questions":[{"id":"q","question":"Continue?","isOther":true}]}}`)
|
||||
wait := func(c *Chat) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
permissions, pErr := c.Permissions(ctx, "thread-a")
|
||||
forms, fErr := c.Forms(ctx, "thread-a")
|
||||
if pErr == nil && fErr == nil && len(permissions) == 1 && len(forms) == 1 {
|
||||
return
|
||||
}
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
t.Fatal("pending interactions did not arrive")
|
||||
}
|
||||
wait(before)
|
||||
before.Detach()
|
||||
after, err := Open(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(after.Close)
|
||||
wait(after)
|
||||
permissions, _ := after.Permissions(ctx, "thread-a")
|
||||
if err := after.Reply(ctx, protocol.ChatReplyRequest{SessionID: "thread-a", RequestID: permissions[0].RequestID, Decision: protocol.ChatDecisionOnce}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := after.Answer(ctx, protocol.ChatAnswerRequest{SessionID: "thread-a", FormID: "question", Answers: map[string]any{"q": "yes"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for range 2 {
|
||||
select {
|
||||
case msg := <-responses:
|
||||
seen[string(msg.ID)] = true
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("answer did not reach original upstream connection")
|
||||
}
|
||||
}
|
||||
if !seen["7"] || !seen["8"] {
|
||||
t.Fatalf("callback identities changed: %+v", seen)
|
||||
}
|
||||
if initialized.Load() != 1 {
|
||||
t.Fatalf("upstream initialized %d times", initialized.Load())
|
||||
}
|
||||
if err := after.Answer(ctx, protocol.ChatAnswerRequest{SessionID: "thread-a", FormID: "question", Answers: map[string]any{"q": "no"}}); err == nil {
|
||||
t.Fatal("question was answered twice")
|
||||
}
|
||||
// Reattach again and use an RPC response as a read-loop barrier: resolved
|
||||
// callbacks must not return in the new adapter's maps.
|
||||
after.Detach()
|
||||
third, err := Open(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(third.Close)
|
||||
if _, err := third.call(ctx, "fixture/barrier", json.RawMessage(`{}`)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
permissions, _ = third.Permissions(ctx, "thread-a")
|
||||
forms, _ := third.Forms(ctx, "thread-a")
|
||||
if len(permissions) != 0 || len(forms) != 0 {
|
||||
t.Fatalf("resolved interactions replayed: %+v %+v", permissions, forms)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user