WIP: preserve remaining background task output work

This commit is contained in:
2026-10-18 17:42:15 +01:00
parent fd4c329eaf
commit ded7927778
12 changed files with 269 additions and 17 deletions
+25 -10
View File
@@ -50,6 +50,21 @@ func NewChat(conn *Connection) *Chat { return &Chat{conn: conn} }
// chat adapter and callers that must reject a request before any OpenCode call.
func ValidSessionID(id string) bool { return sessionID.MatchString(id) }
// ValidShellID accepts only the provider's opaque shell-id alphabet. Keeping
// this check shared by the websocket boundary and provider adapter ensures an
// invalid id is rejected before either network call or path construction.
func ValidShellID(id string) bool {
if len(id) <= 3 || len(id) > 128 || !strings.HasPrefix(id, "sh_") {
return false
}
for _, c := range id[3:] {
if (c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && (c < '0' || c > '9') && c != '_' && c != '-' {
return false
}
}
return true
}
// call serializes the adapter's HTTP calls and forwards them through the shared
// Connection, so no second transport is introduced and Connection's recorded
// last operation/status is never written by two goroutines at once.
@@ -392,7 +407,7 @@ func (c *Chat) Shells(ctx context.Context, sessionID string) ([]protocol.ChatShe
}
out := make([]protocol.ChatShell, 0, len(response.Data))
for _, info := range response.Data {
if info.ID == "" || info.shellSessionID() != sessionID || (info.Status != "" && info.Status != protocol.ChatShellRunning) {
if info.ID == "" || info.shellSessionID() != sessionID || (info.Status != "" && info.Status != protocol.ChatShellRunning && info.Status != protocol.ChatShellExited) {
continue
}
status := info.Status
@@ -422,7 +437,7 @@ func (c *Chat) Shells(ctx context.Context, sessionID string) ([]protocol.ChatShe
// record before reading the selected bounded output page. A shared directory is
// never ownership evidence.
func (c *Chat) ShellOutput(ctx context.Context, query protocol.ChatShellOutputQuery) (protocol.ChatShellOutputPage, error) {
if !ValidSessionID(query.SessionID) || len(query.ShellID) <= 3 || !strings.HasPrefix(query.ShellID, "sh_") || query.Cursor < 0 || query.Limit < 0 || query.Limit > 64<<10 {
if !ValidSessionID(query.SessionID) || !ValidShellID(query.ShellID) || query.Cursor < 0 || query.Cursor > protocol.ChatShellOutputMaxCursor || query.Limit <= 0 || query.Limit > protocol.ChatShellOutputLimit {
return protocol.ChatShellOutputPage{}, fmt.Errorf("invalid shell output query")
}
data, err := c.call(ctx, "get", "/api/shell/"+query.ShellID, nil)
@@ -435,7 +450,7 @@ func (c *Chat) ShellOutput(ctx context.Context, query protocol.ChatShellOutputQu
var detail struct {
Data chatShellInfo `json:"data"`
}
if err := json.Unmarshal(data, &detail); err != nil || detail.Data.ID != query.ShellID || detail.Data.shellSessionID() != query.SessionID {
if err := json.Unmarshal(data, &detail); err != nil || detail.Data.ID != query.ShellID || detail.Data.shellSessionID() != query.SessionID || (detail.Data.Status != "" && detail.Data.Status != protocol.ChatShellRunning && detail.Data.Status != protocol.ChatShellExited) {
return protocol.ChatShellOutputPage{}, fmt.Errorf("shell is not available for this session")
}
shell := protocol.ChatShell{ID: detail.Data.ID, Status: detail.Data.Status, Command: detail.Data.Command, Exit: detail.Data.Exit}
@@ -451,17 +466,17 @@ func (c *Chat) ShellOutput(ctx context.Context, query protocol.ChatShellOutputQu
return protocol.ChatShellOutputPage{}, err
}
var response struct {
Data struct {
Output string `json:"output"`
Cursor int64 `json:"cursor"`
Size int64 `json:"size"`
Truncated bool `json:"truncated"`
Data *struct {
Output *string `json:"output"`
Cursor *int64 `json:"cursor"`
Size *int64 `json:"size"`
Truncated *bool `json:"truncated"`
} `json:"data"`
}
if err := json.Unmarshal(data, &response); err != nil || len(response.Data.Output) > 64<<10 || response.Data.Cursor < query.Cursor || response.Data.Size < response.Data.Cursor {
if err := json.Unmarshal(data, &response); err != nil || response.Data == nil || response.Data.Output == nil || response.Data.Cursor == nil || response.Data.Size == nil || response.Data.Truncated == nil || len(*response.Data.Output) > query.Limit || *response.Data.Cursor < query.Cursor || *response.Data.Cursor > protocol.ChatShellOutputMaxCursor || *response.Data.Size < *response.Data.Cursor || *response.Data.Size > protocol.ChatShellOutputMaxCursor || *response.Data.Cursor-query.Cursor != int64(len(*response.Data.Output)) {
return protocol.ChatShellOutputPage{}, fmt.Errorf("invalid OpenCode shell output response")
}
return protocol.ChatShellOutputPage{Shell: shell, Output: response.Data.Output, Cursor: response.Data.Cursor, Size: response.Data.Size, Truncated: response.Data.Truncated}, nil
return protocol.ChatShellOutputPage{Shell: shell, Output: *response.Data.Output, Cursor: *response.Data.Cursor, Size: *response.Data.Size, Truncated: *response.Data.Truncated}, nil
}
// streamState returns the session's retained, verified shell identities. It is
+27
View File
@@ -450,6 +450,33 @@ func TestChatShellOutputVerifiesOwnerAndBoundsPage(t *testing.T) {
}
}
func TestChatShellOutputRejectsMissingRequiredPageFields(t *testing.T) {
for _, body := range []string{
`{"data":{}}`,
`{"data":{"output":"","cursor":0,"size":0}}`,
`{"data":{"output":"","size":0,"truncated":false}}`,
`{"data":{"output":"","cursor":0,"truncated":false}}`,
} {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/api/shell/sh_1":
_, _ = w.Write([]byte(`{"data":{"id":"sh_1","status":"running","metadata":{"sessionID":"ses_1"}}}`))
case "/api/shell/sh_1/output":
_, _ = w.Write([]byte(body))
default:
http.NotFound(w, r)
}
}))
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
_, err := chat.ShellOutput(context.Background(), protocol.ChatShellOutputQuery{SessionID: "ses_1", ShellID: "sh_1", Limit: 1})
srv.Close()
if err == nil {
t.Fatalf("response %s was accepted", body)
}
}
}
func TestChatReleaseShellsBoundsDisconnectedState(t *testing.T) {
chat := NewChat(&Connection{})
state := chat.streamState("ses_1")
+4 -2
View File
@@ -93,8 +93,10 @@ const (
// ChatShell.Status values, mirroring the OpenCode shell events. A running shell
// is one whose command has started and not yet exited; exited is final.
const (
ChatShellRunning = "running"
ChatShellExited = "exited"
ChatShellRunning = "running"
ChatShellExited = "exited"
ChatShellOutputLimit = 64 << 10
ChatShellOutputMaxCursor = int64(1 << 62)
)
// ChatStep.Status values, mirroring the OpenCode session.step events.
+4 -4
View File
@@ -8,8 +8,6 @@ import (
"git.a-wan.net/alan/pipelines/internal/protocol"
)
const chatShellOutputLimit = 64 << 10
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")
@@ -52,7 +50,7 @@ func handleChatShellOutput(ctx context.Context, request protocol.Message, broker
_ = 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.Limit < 0 || query.Limit > chatShellOutputLimit {
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
}
@@ -73,4 +71,6 @@ func handleChatShellOutput(ctx context.Context, request protocol.Message, broker
_ = write(protocol.Message{Type: protocol.ChatShellOutput, ID: request.ID, ChatShellOutput: &output})
}
func validShellID(id string) bool { return len(id) > 3 && len(id) <= 128 && id[:3] == "sh_" }
func validShellID(id string) bool {
return opencode.ValidShellID(id)
}
+47
View File
@@ -0,0 +1,47 @@
package server
import (
"context"
"testing"
"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)
}
}
+36
View File
@@ -13,6 +13,8 @@ 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 (
LargeChatFixtureNewestAnswer = "Controlled newest answer: large history fixture"
@@ -24,6 +26,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 +72,33 @@ 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}}]}`, LargeChatFixtureShellID, status, LargeChatFixtureSessionID))
})
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/"+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))
})
return mux
}