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")