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
+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
}