WIP: preserve background task output API
This commit is contained in:
@@ -418,6 +418,52 @@ func (c *Chat) Shells(ctx context.Context, sessionID string) ([]protocol.ChatShe
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ShellOutput validates explicit session metadata from the authoritative detail
|
||||
// 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 {
|
||||
return protocol.ChatShellOutputPage{}, fmt.Errorf("invalid shell output query")
|
||||
}
|
||||
data, err := c.call(ctx, "get", "/api/shell/"+query.ShellID, nil)
|
||||
if err != nil {
|
||||
if c.conn.LastHTTPStatus == 404 {
|
||||
return protocol.ChatShellOutputPage{Unavailable: true}, nil
|
||||
}
|
||||
return protocol.ChatShellOutputPage{}, err
|
||||
}
|
||||
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 {
|
||||
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}
|
||||
if shell.Status == "" {
|
||||
shell.Status = protocol.ChatShellRunning
|
||||
}
|
||||
path := fmt.Sprintf("/api/shell/%s/output?cursor=%d&limit=%d", query.ShellID, query.Cursor, query.Limit)
|
||||
data, err = c.call(ctx, "get", path, nil)
|
||||
if err != nil {
|
||||
if c.conn.LastHTTPStatus == 404 {
|
||||
return protocol.ChatShellOutputPage{Shell: shell, Unavailable: true}, nil
|
||||
}
|
||||
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"`
|
||||
} `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 {
|
||||
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
|
||||
}
|
||||
|
||||
// streamState returns the session's retained, verified shell identities. It is
|
||||
// deliberately kept across stream replacement so an exit after reconnect remains
|
||||
// attributable; Shells reconciles its contents from authoritative readback on
|
||||
@@ -592,6 +638,7 @@ type chatShellInfo struct {
|
||||
ID string `json:"id"`
|
||||
Status string `json:"status"`
|
||||
Command string `json:"command"`
|
||||
Exit *int `json:"exit"`
|
||||
Metadata map[string]any `json:"metadata"`
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -425,6 +426,30 @@ func TestChatShellReadbackSeedsReplacementStream(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatShellOutputVerifiesOwnerAndBoundsPage(t *testing.T) {
|
||||
var outputPath string
|
||||
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":"exited","command":"job","metadata":{"sessionID":"ses_1"}}}`))
|
||||
case "/api/shell/sh_1/output":
|
||||
outputPath = r.URL.RawQuery
|
||||
_, _ = w.Write([]byte(`{"data":{"output":"marker","cursor":6,"size":6,"truncated":false}}`))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
page, err := NewChat(&Connection{Backend: "v2", URL: srv.URL}).ShellOutput(context.Background(), protocol.ChatShellOutputQuery{SessionID: "ses_1", ShellID: "sh_1", Limit: 64 << 10})
|
||||
if err != nil || page.Output != "marker" || page.Shell.Status != protocol.ChatShellExited {
|
||||
t.Fatalf("ShellOutput = %+v, %v", page, err)
|
||||
}
|
||||
if !strings.Contains(outputPath, "cursor=0") || !strings.Contains(outputPath, "limit=65536") {
|
||||
t.Fatalf("output query = %q", outputPath)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatReleaseShellsBoundsDisconnectedState(t *testing.T) {
|
||||
chat := NewChat(&Connection{})
|
||||
state := chat.streamState("ses_1")
|
||||
|
||||
Reference in New Issue
Block a user