Merge branch 'main' into retry-from-failed-stage

This commit is contained in:
2026-10-18 20:38:06 +01:00
15 changed files with 782 additions and 11 deletions
+10
View File
@@ -42,6 +42,10 @@ type chatBroker interface {
Reply(ctx context.Context, request protocol.ChatReplyRequest) error
}
type chatShellOutputReader interface {
ShellOutput(ctx context.Context, query protocol.ChatShellOutputQuery) (protocol.ChatShellOutputPage, error)
}
// chatShellReleaser is implemented by brokers that retain verified shell ids for
// attributing exit events. The hub calls it only after the last observer leaves
// a room; a later attach obtains a fresh authoritative snapshot.
@@ -128,6 +132,12 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker
case protocol.ChatReopen:
handleChatReopen(ctx, request, registry, write)
return true
case protocol.ChatShells:
handleChatShells(ctx, request, broker, registry, write)
return true
case protocol.ChatShellOutput:
handleChatShellOutput(ctx, request, broker, registry, write)
return true
}
return false
}
+76
View File
@@ -0,0 +1,76 @@
package server
import (
"context"
"fmt"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func operatorChatSession(ctx context.Context, registry operatorRegistry, id string) error {
if registry == nil {
return fmt.Errorf("operator session registry is not configured on this server")
}
_, found, err := registry.OperatorSession(ctx, id)
if err != nil {
return err
}
if !found {
return fmt.Errorf("operator session is not available")
}
return nil
}
func handleChatShells(ctx context.Context, request protocol.Message, broker chatBroker, registry operatorRegistry, write func(protocol.Message) error) {
query := request.ChatQuery
if broker == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
return
}
if query == nil || !opencode.ValidSessionID(query.SessionID) {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
return
}
if err := operatorChatSession(ctx, registry, query.SessionID); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
shells, err := broker.Shells(ctx, query.SessionID)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
_ = write(protocol.Message{Type: protocol.ChatShells, ID: request.ID, ChatShells: shells})
}
func handleChatShellOutput(ctx context.Context, request protocol.Message, broker chatBroker, registry operatorRegistry, write func(protocol.Message) error) {
query := request.ChatShellOutputQuery
if broker == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
return
}
if query == nil || !opencode.ValidSessionID(query.SessionID) || !validShellID(query.ShellID) || query.Cursor < 0 || query.Cursor > protocol.ChatShellOutputMaxCursor || query.Limit <= 0 || query.Limit > protocol.ChatShellOutputLimit {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid bounded shell output query is required"})
return
}
if err := operatorChatSession(ctx, registry, query.SessionID); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
reader, ok := broker.(chatShellOutputReader)
if !ok {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "shell output is not configured on this server"})
return
}
output, err := reader.ShellOutput(ctx, *query)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
_ = write(protocol.Message{Type: protocol.ChatShellOutput, ID: request.ID, ChatShellOutput: &output})
}
func validShellID(id string) bool {
return opencode.ValidShellID(id)
}
+280
View File
@@ -0,0 +1,280 @@
package server
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/coder/websocket"
"github.com/coder/websocket/wsjson"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
type shellOutputTestBroker struct {
*fakeChat
outputCalls int
}
func (b *shellOutputTestBroker) ShellOutput(context.Context, protocol.ChatShellOutputQuery) (protocol.ChatShellOutputPage, error) {
b.outputCalls++
return protocol.ChatShellOutputPage{Output: "unexpected"}, nil
}
func TestChatShellOutputRejectsBoundsBeforeRegistryOrProvider(t *testing.T) {
store := newOperatorStore(t)
broker := &shellOutputTestBroker{fakeChat: scriptedChat()}
queries := []protocol.ChatShellOutputQuery{
{SessionID: "ses_1", ShellID: "sh_1", Limit: 0},
{SessionID: "ses_1", ShellID: "sh_1", Limit: -1},
{SessionID: "ses_1", ShellID: "sh_1", Cursor: protocol.ChatShellOutputMaxCursor + 1, Limit: 1},
{SessionID: "ses_1", ShellID: "sh_1/../x", Limit: 1},
{SessionID: "ses_1", ShellID: "sh_1%2f", Limit: 1},
{SessionID: "ses_1", ShellID: "sh_1?cursor=0", Limit: 1},
{SessionID: "ses_1", ShellID: "sh_1#fragment", Limit: 1},
{SessionID: "ses_1", ShellID: "sh_%ZZ", Limit: 1},
{SessionID: "ses_1", ShellID: "sh_" + string(make([]byte, 126)), Limit: 1},
}
for _, query := range queries {
var response protocol.Message
handleChatShellOutput(context.Background(), protocol.Message{ChatShellOutputQuery: &query}, broker, store, func(m protocol.Message) error {
response = m
return nil
})
if response.Type != protocol.Error {
t.Fatalf("query %+v response = %+v", query, response)
}
}
if broker.outputCalls != 0 {
t.Fatalf("invalid queries reached provider %d times", broker.outputCalls)
}
}
// TestChatShellOutputRealHandlerWebsocketOwnedAndForeign drives the real
// authenticated adapter, HTTP fixture, HandlerWithOptions, and /connect path.
// The foreign detail deliberately has a marker that would be returned by its
// output route; the session metadata check must reject it first.
func TestChatShellOutputRealHandlerWebsocketOwnedAndForeign(t *testing.T) {
testChatShellOutputOwnedAndForeign(t, false)
}
func TestChatShellOutputRealHandlerExitedReadback(t *testing.T) {
testChatShellOutputOwnedAndForeign(t, true)
}
func testChatShellOutputOwnedAndForeign(t *testing.T, exited bool) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
var providerRequests atomic.Int32
fixtureHandler := LargeChatFixtureHandlerWithShell(t.TempDir(), exited)
fixture := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
providerRequests.Add(1)
fixtureHandler.ServeHTTP(w, r)
}))
defer fixture.Close()
attached, err := opencode.OpenEndpointAttached(ctx, fixture.URL, LargeChatFixturePassword, "", "")
if err != nil {
t.Fatal(err)
}
store := newOperatorStore(t)
if err := store.RecordOperatorSession(ctx, LargeChatFixtureSessionID, t.TempDir(), "owned", protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
srv := httptest.NewServer(HandlerWithOptions(ctx, Options{Chat: opencode.NewChat(attached.Connection), OperatorRegistry: store}))
defer srv.Close()
ws := dialShellOutputWebsocket(t, ctx, srv)
defer ws.CloseNow()
writeChat(t, ctx, ws, protocol.Message{Type: protocol.ChatShells, ID: "owned-shells", ChatQuery: &protocol.ChatQuery{SessionID: LargeChatFixtureSessionID}})
owned := readShellOutputMessage(t, ctx, ws)
if owned.Type != protocol.ChatShells || (exited && len(owned.ChatShells) != 0) || (!exited && (len(owned.ChatShells) != 1 || owned.ChatShells[0].ID != LargeChatFixtureShellID || owned.ChatShells[0].Status != protocol.ChatShellRunning)) {
t.Fatalf("owned shell listing = %+v", owned)
}
wantStatus := protocol.ChatShellRunning
if exited {
wantStatus = protocol.ChatShellExited
}
writeChat(t, ctx, ws, protocol.Message{Type: protocol.ChatShellOutput, ID: "owned-output", ChatShellOutputQuery: &protocol.ChatShellOutputQuery{SessionID: LargeChatFixtureSessionID, ShellID: LargeChatFixtureShellID, Limit: protocol.ChatShellOutputLimit}})
page := readShellOutputMessage(t, ctx, ws)
if page.Type != protocol.ChatShellOutput || page.ChatShellOutput == nil || page.ChatShellOutput.Output != LargeChatFixtureShellOutput || page.ChatShellOutput.Cursor != int64(len(LargeChatFixtureShellOutput)) || page.ChatShellOutput.Shell.Status != wantStatus {
t.Fatalf("owned output page = %+v", page)
}
if exited {
if page.ChatShellOutput.Shell.Exit == nil || *page.ChatShellOutput.Shell.Exit != 0 {
t.Fatalf("selected exited shell readback = %+v", page.ChatShellOutput.Shell)
}
} else if page.ChatShellOutput.Shell.Exit != nil {
t.Fatalf("running shell unexpectedly has exit = %+v", page.ChatShellOutput.Shell.Exit)
}
if page.ChatShellOutput.Cursor != int64(len(page.ChatShellOutput.Output)) || page.ChatShellOutput.Size != page.ChatShellOutput.Cursor {
t.Fatalf("non-deterministic output cursor/size = %+v", page.ChatShellOutput)
}
refusals := []struct {
name string
request protocol.Message
}{
{name: "foreign-output", request: protocol.Message{Type: protocol.ChatShellOutput, ID: "foreign-output", ChatShellOutputQuery: &protocol.ChatShellOutputQuery{SessionID: LargeChatFixtureSessionID, ShellID: LargeChatFixtureForeignShellID, Limit: protocol.ChatShellOutputLimit}}},
{name: "foreign-list", request: protocol.Message{Type: protocol.ChatShells, ID: "foreign-list", ChatQuery: &protocol.ChatQuery{SessionID: LargeChatFixtureForeignSessionID}}},
{name: "syntax-invalid-list", request: protocol.Message{Type: protocol.ChatShells, ID: "invalid-list", ChatQuery: &protocol.ChatQuery{SessionID: "ses_bad/invalid"}}},
{name: "unknown-output", request: protocol.Message{Type: protocol.ChatShellOutput, ID: "unknown-output", ChatShellOutputQuery: &protocol.ChatShellOutputQuery{SessionID: "ses_unregistered_fixture", ShellID: LargeChatFixtureShellID, Limit: protocol.ChatShellOutputLimit}}},
}
for _, refusal := range refusals {
beforeRefusal := providerRequests.Load()
writeChat(t, ctx, ws, refusal.request)
response := readShellOutputMessage(t, ctx, ws)
if response.Type != protocol.Error || strings.Contains(response.Error, LargeChatFixtureForeignShellOutput) {
t.Fatalf("%s response leaked or succeeded: %+v", refusal.name, response)
}
if got := providerRequests.Load(); got != beforeRefusal {
t.Fatalf("%s reached provider: requests %d -> %d", refusal.name, beforeRefusal, got)
}
}
}
func TestChatShellOutputRealHandlerUnavailableAndReconnect(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
fixture := httptest.NewServer(LargeChatFixtureHandlerWithShell(t.TempDir(), false))
defer fixture.Close()
attached, err := opencode.OpenEndpointAttached(ctx, fixture.URL, LargeChatFixturePassword, "", "")
if err != nil {
t.Fatal(err)
}
store := newOperatorStore(t)
if err := store.RecordOperatorSession(ctx, LargeChatFixtureSessionID, t.TempDir(), "owned", protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
srv := httptest.NewServer(HandlerWithOptions(ctx, Options{Chat: opencode.NewChat(attached.Connection), OperatorRegistry: store}))
defer srv.Close()
ws := dialShellOutputWebsocket(t, ctx, srv)
writeChat(t, ctx, ws, protocol.Message{Type: protocol.ChatShellOutput, ID: "missing-output", ChatShellOutputQuery: &protocol.ChatShellOutputQuery{SessionID: LargeChatFixtureSessionID, ShellID: "sh_missing_fixture", Limit: 64}})
missing := readShellOutputMessage(t, ctx, ws)
if missing.Type != protocol.ChatShellOutput || missing.ChatShellOutput == nil || !missing.ChatShellOutput.Unavailable {
t.Fatalf("404 shell output = %+v", missing)
}
ws = dialShellOutputWebsocket(t, ctx, srv)
defer ws.CloseNow()
writeChat(t, ctx, ws, protocol.Message{Type: protocol.ChatShells, ID: "reconnect", ChatQuery: &protocol.ChatQuery{SessionID: LargeChatFixtureSessionID}})
if got := readShellOutputMessage(t, ctx, ws); got.Type != protocol.ChatShells {
t.Fatalf("reconnect shell listing = %+v", got)
}
if _, err := opencode.OpenEndpointAttached(ctx, fixture.URL, "wrong-password", "", ""); err == nil {
t.Fatal("wrong provider authentication unexpectedly succeeded")
}
}
func TestChatShellOutputRejectsMalformedAndOversizedProviderPages(t *testing.T) {
for _, tc := range []struct {
name string
output string
}{
{name: "malformed", output: `{"data":{"output":"x"}}`},
{name: "oversized", output: fmt.Sprintf(`{"data":{"output":%q,"cursor":%d,"size":%d,"truncated":false}}`, strings.Repeat("x", protocol.ChatShellOutputLimit+1), protocol.ChatShellOutputLimit+1, protocol.ChatShellOutputLimit+1)},
} {
t.Run(tc.name, func(t *testing.T) {
fixture := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !largeChatFixtureAuthenticated(w, r) {
return
}
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/api/info":
_, _ = w.Write([]byte(`{"version":"fixture","urls":[]}`))
case "/api/event":
w.Header().Set("Content-Type", "text/event-stream")
case "/api/shell/sh_malformed_fixture":
_, _ = w.Write([]byte(`{"data":{"id":"sh_malformed_fixture","status":"running","metadata":{"sessionID":"ses_malformed_fixture"}}}`))
case "/api/shell/sh_malformed_fixture/output":
_, _ = w.Write([]byte(tc.output))
default:
if r.URL.Path == "/api/shell" {
_, _ = w.Write([]byte(`{"data":[]}`))
return
}
http.NotFound(w, r)
}
}))
defer fixture.Close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
attached, err := opencode.OpenEndpointAttached(ctx, fixture.URL, LargeChatFixturePassword, "", "")
if err != nil {
t.Fatal(err)
}
_, err = opencode.NewChat(attached.Connection).ShellOutput(ctx, protocol.ChatShellOutputQuery{SessionID: "ses_malformed_fixture", ShellID: "sh_malformed_fixture", Limit: protocol.ChatShellOutputLimit})
if err == nil {
t.Fatalf("%s provider page was accepted", tc.name)
}
})
}
}
func TestChatShellOutputRealHandlerCancellationStopsProvider(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
started := make(chan struct{})
canceled := make(chan struct{})
fixtureHandler := LargeChatFixtureHandlerWithShell(t.TempDir(), false)
fixture := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api/shell/sh_cancel_fixture" {
if !largeChatFixtureAuthenticated(w, r) {
return
}
close(started)
<-r.Context().Done()
close(canceled)
return
}
fixtureHandler.ServeHTTP(w, r)
}))
defer fixture.Close()
attached, err := opencode.OpenEndpointAttached(ctx, fixture.URL, LargeChatFixturePassword, "", "")
if err != nil {
t.Fatal(err)
}
store := newOperatorStore(t)
if err := store.RecordOperatorSession(ctx, "ses_cancel_fixture", t.TempDir(), "cancel", protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
serverCtx, serverCancel := context.WithCancel(ctx)
defer serverCancel()
srv := httptest.NewServer(HandlerWithOptions(serverCtx, Options{Chat: opencode.NewChat(attached.Connection), OperatorRegistry: store}))
defer srv.Close()
ws := dialShellOutputWebsocket(t, ctx, srv)
writeChat(t, ctx, ws, protocol.Message{Type: protocol.ChatShellOutput, ID: "cancel-output", ChatShellOutputQuery: &protocol.ChatShellOutputQuery{SessionID: "ses_cancel_fixture", ShellID: "sh_cancel_fixture", Limit: 64}})
select {
case <-started:
case <-ctx.Done():
t.Fatal("provider detail request did not start")
}
serverCancel()
select {
case <-canceled:
case <-ctx.Done():
t.Fatal("canceling handler did not cancel provider request")
}
}
func dialShellOutputWebsocket(t *testing.T, ctx context.Context, srv *httptest.Server) *websocket.Conn {
t.Helper()
ws, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect", nil)
if err != nil {
t.Fatal(err)
}
return ws
}
func readShellOutputMessage(t *testing.T, ctx context.Context, ws *websocket.Conn) protocol.Message {
t.Helper()
var message protocol.Message
if err := wsjson.Read(ctx, ws, &message); err != nil {
t.Fatal(err)
}
return message
}
+52
View File
@@ -13,6 +13,11 @@ const LargeChatFixtureSessionID = "ses_large_chat_fixture"
// LargeChatFixturePassword authenticates the disposable fixture endpoint. It is
// fixed fixture data, not an operator credential.
const LargeChatFixturePassword = "large-chat-fixture"
const LargeChatFixtureShellID = "sh_large_chat_fixture"
const LargeChatFixtureShellOutput = "fixture shell output marker\n"
const LargeChatFixtureForeignSessionID = "ses_foreign_fixture"
const LargeChatFixtureForeignShellID = "sh_foreign_fixture"
const LargeChatFixtureForeignShellOutput = "FOREIGN SHELL MARKER MUST NOT LEAK\n"
const (
LargeChatFixtureNewestAnswer = "Controlled newest answer: large history fixture"
@@ -24,6 +29,13 @@ const (
// It deliberately serves fixed, paged data: no model is invoked and no production
// history is read or written.
func LargeChatFixtureHandler(workspace string) http.Handler {
return LargeChatFixtureHandlerWithShell(workspace, false)
}
// LargeChatFixtureHandlerWithShell is the foreground fixture surface used by
// the readback check. exited selects the provider's observed terminal state;
// the output remains fixed and bounded in either state.
func LargeChatFixtureHandlerWithShell(workspace string, exited bool) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("GET /api/info", func(w http.ResponseWriter, r *http.Request) {
if !largeChatFixtureAuthenticated(w, r) {
@@ -63,6 +75,46 @@ func LargeChatFixtureHandler(workspace string) http.Handler {
}
writeLargeChatFixturePage(w, page)
})
status := "running"
if exited {
status = "exited"
}
mux.HandleFunc("GET /api/shell", func(w http.ResponseWriter, r *http.Request) {
if !largeChatFixtureAuthenticated(w, r) {
return
}
writeLargeChatFixtureJSON(w, fmt.Sprintf(`{"data":[{"id":%q,"status":%q,"command":"printf fixture","metadata":{"sessionID":%q}},{"id":%q,"status":"running","command":"printf foreign","metadata":{"sessionID":%q}}]}`, LargeChatFixtureShellID, status, LargeChatFixtureSessionID, LargeChatFixtureForeignShellID, LargeChatFixtureForeignSessionID))
})
mux.HandleFunc("GET /api/shell/"+LargeChatFixtureShellID, func(w http.ResponseWriter, r *http.Request) {
if !largeChatFixtureAuthenticated(w, r) {
return
}
exit := "null"
if exited {
exit = "0"
}
writeLargeChatFixtureJSON(w, fmt.Sprintf(`{"data":{"id":%q,"status":%q,"command":"printf fixture","exit":%s,"metadata":{"sessionID":%q}}}`, LargeChatFixtureShellID, status, exit, LargeChatFixtureSessionID))
})
mux.HandleFunc("GET /api/shell/"+LargeChatFixtureForeignShellID, func(w http.ResponseWriter, r *http.Request) {
if !largeChatFixtureAuthenticated(w, r) {
return
}
writeLargeChatFixtureJSON(w, fmt.Sprintf(`{"data":{"id":%q,"status":"running","command":"printf foreign","metadata":{"sessionID":%q}}}`, LargeChatFixtureForeignShellID, LargeChatFixtureForeignSessionID))
})
mux.HandleFunc("GET /api/shell/"+LargeChatFixtureShellID+"/output", func(w http.ResponseWriter, r *http.Request) {
if !largeChatFixtureAuthenticated(w, r) {
return
}
cursor := int64(len(LargeChatFixtureShellOutput))
writeLargeChatFixtureJSON(w, fmt.Sprintf(`{"data":{"output":%q,"cursor":%d,"size":%d,"truncated":false}}`, LargeChatFixtureShellOutput, cursor, cursor))
})
mux.HandleFunc("GET /api/shell/"+LargeChatFixtureForeignShellID+"/output", func(w http.ResponseWriter, r *http.Request) {
if !largeChatFixtureAuthenticated(w, r) {
return
}
cursor := int64(len(LargeChatFixtureForeignShellOutput))
writeLargeChatFixtureJSON(w, fmt.Sprintf(`{"data":{"output":%q,"cursor":%d,"size":%d,"truncated":false}}`, LargeChatFixtureForeignShellOutput, cursor, cursor))
})
return mux
}
+1 -1
View File
@@ -173,7 +173,7 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
}
for {
var request protocol.Message
if err := wsjson.Read(ctx, conn, &request); err != nil {
if err := wsjson.Read(connCtx, conn, &request); err != nil {
return
}
log.Printf("command received: client=%s type=%q id=%q pipeline=%d version=%d", r.RemoteAddr, request.Type, request.ID, request.PipelineID, request.Version)