test: strengthen background shell output verification

This commit is contained in:
2026-10-18 20:23:43 +01:00
parent ded7927778
commit f5f682a990
6 changed files with 344 additions and 27 deletions
+20
View File
@@ -33,6 +33,10 @@ type Chat struct {
conn *Connection
shellMu sync.Mutex
shells map[string]*ChatStreamState
// shellOwners retains ownership from the last authoritative global shell
// listing so a shell known to belong to another session is rejected before
// its detail or output endpoint is queried.
shellOwners map[string]string
// mu serializes the adapter's HTTP calls. Connection records its last
// operation and status on the shared connection, so two calls must not
// overlap; the permission poller runs alongside the read loop's
@@ -405,6 +409,16 @@ func (c *Chat) Shells(ctx context.Context, sessionID string) ([]protocol.ChatShe
return nil, fmt.Errorf("invalid OpenCode shell response")
}
}
c.shellMu.Lock()
if c.shellOwners == nil {
c.shellOwners = make(map[string]string)
}
for _, info := range response.Data {
if ValidShellID(info.ID) && ValidSessionID(info.shellSessionID()) {
c.shellOwners[info.ID] = info.shellSessionID()
}
}
c.shellMu.Unlock()
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 && info.Status != protocol.ChatShellExited) {
@@ -440,6 +454,12 @@ func (c *Chat) ShellOutput(ctx context.Context, query protocol.ChatShellOutputQu
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")
}
c.shellMu.Lock()
owner := c.shellOwners[query.ShellID]
c.shellMu.Unlock()
if owner != "" && owner != query.SessionID {
return protocol.ChatShellOutputPage{}, fmt.Errorf("shell is not available for this session")
}
data, err := c.call(ctx, "get", "/api/shell/"+query.ShellID, nil)
if err != nil {
if c.conn.LastHTTPStatus == 404 {