Expose session-owned chat automation metadata
This commit is contained in:
@@ -85,6 +85,13 @@ type chatShellOutputReader interface {
|
||||
ShellOutput(ctx context.Context, query protocol.ChatShellOutputQuery) (protocol.ChatShellOutputPage, error)
|
||||
}
|
||||
|
||||
// chatAutomationReader is optional so every broker and test fake does not need
|
||||
// to grow a scheduler-shaped interface. Implementations must read only
|
||||
// provider-owned, session-scoped metadata and return unavailable explicitly.
|
||||
type chatAutomationReader interface {
|
||||
Automations(ctx context.Context, sessionID string) (protocol.ChatAutomationInfo, error)
|
||||
}
|
||||
|
||||
// chatShellReleaser is implemented by brokers that retain verified shell ids for
|
||||
// attributing exit events. The hub calls it only after the last observer leaves
|
||||
// a room; a later attach obtains a fresh authoritative snapshot.
|
||||
@@ -251,6 +258,9 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker
|
||||
case protocol.ChatAttachmentImage:
|
||||
handleChatAttachmentImage(request, broker, write)
|
||||
return true
|
||||
case protocol.ChatAutomations:
|
||||
handleChatAutomations(ctx, request, broker, registry, write)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
func validChatAutomationInfo(info protocol.ChatAutomationInfo, sessionID string) error {
|
||||
if info.SessionID != "" && info.SessionID != sessionID {
|
||||
return fmt.Errorf("provider returned automation metadata for another session")
|
||||
}
|
||||
if info.Status != protocol.ChatAutomationSupported && info.Status != protocol.ChatAutomationUnavailable {
|
||||
return fmt.Errorf("provider returned an invalid automation status")
|
||||
}
|
||||
if info.ActiveCount != nil && *info.ActiveCount < 0 {
|
||||
return fmt.Errorf("provider returned a negative automation count")
|
||||
}
|
||||
if info.Status == protocol.ChatAutomationUnavailable && info.ActiveCount != nil {
|
||||
return fmt.Errorf("unavailable automation metadata must not include a count")
|
||||
}
|
||||
if info.Status == protocol.ChatAutomationSupported && info.ActiveCount == nil {
|
||||
return fmt.Errorf("supported automation metadata is missing a count")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleChatAutomations(ctx context.Context, request protocol.Message, broker chatBroker, registry operatorRegistry, write func(protocol.Message) error) {
|
||||
if broker == nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
|
||||
return
|
||||
}
|
||||
query := request.ChatQuery
|
||||
if query == nil || !validChatSessionID(query.SessionID) {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid chat session id is required"})
|
||||
return
|
||||
}
|
||||
if err := operatorChatSession(ctx, registry, query.SessionID); err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
||||
return
|
||||
}
|
||||
reader, ok := broker.(chatAutomationReader)
|
||||
if !ok {
|
||||
_ = write(protocol.Message{Type: protocol.ChatAutomations, ID: request.ID, ChatAutomations: &protocol.ChatAutomationInfo{
|
||||
SessionID: query.SessionID, Status: protocol.ChatAutomationUnavailable,
|
||||
Reason: "this chat provider has no authoritative scheduled-work read API",
|
||||
}})
|
||||
return
|
||||
}
|
||||
info, err := reader.Automations(ctx, query.SessionID)
|
||||
if err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: fmt.Sprintf("read chat automations: %v", err)})
|
||||
return
|
||||
}
|
||||
if info.SessionID == "" {
|
||||
info.SessionID = query.SessionID
|
||||
}
|
||||
if err := validChatAutomationInfo(info, query.SessionID); err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "read chat automations: " + err.Error()})
|
||||
return
|
||||
}
|
||||
_ = write(protocol.Message{Type: protocol.ChatAutomations, ID: request.ID, ChatAutomations: &info})
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
type automationChat struct {
|
||||
*fakeChat
|
||||
info protocol.ChatAutomationInfo
|
||||
readCalled int
|
||||
}
|
||||
|
||||
func (c *automationChat) Automations(context.Context, string) (protocol.ChatAutomationInfo, error) {
|
||||
c.readCalled++
|
||||
return c.info, nil
|
||||
}
|
||||
|
||||
func automationReply(t *testing.T, request protocol.Message, broker chatBroker, registry operatorRegistry) protocol.Message {
|
||||
t.Helper()
|
||||
var reply protocol.Message
|
||||
handleChatAutomations(context.Background(), request, broker, registry, func(message protocol.Message) error {
|
||||
reply = message
|
||||
return nil
|
||||
})
|
||||
return reply
|
||||
}
|
||||
|
||||
func TestChatAutomationsRequiresOwnedSession(t *testing.T) {
|
||||
store := newOperatorStore(t)
|
||||
if err := store.RecordOperatorSession(context.Background(), "ses_owned", t.TempDir(), "owned", protocol.ChatSessionTagOperator); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
broker := &automationChat{fakeChat: scriptedChat(), info: protocol.ChatAutomationInfo{Status: protocol.ChatAutomationSupported}}
|
||||
reply := automationReply(t, protocol.Message{Type: protocol.ChatAutomations, ID: "a1", ChatQuery: &protocol.ChatQuery{SessionID: "ses_foreign"}}, broker, store)
|
||||
if reply.Type != protocol.Error || reply.Error != "operator session is not available" {
|
||||
t.Fatalf("foreign automation read = %+v", reply)
|
||||
}
|
||||
if broker.readCalled != 0 {
|
||||
t.Fatal("provider read happened before ownership check")
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatAutomationsDistinguishesUnavailableFromEmpty(t *testing.T) {
|
||||
store := newOperatorStore(t)
|
||||
if err := store.RecordOperatorSession(context.Background(), "ses_empty", t.TempDir(), "empty", protocol.ChatSessionTagOperator); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request := protocol.Message{Type: protocol.ChatAutomations, ID: "a2", ChatQuery: &protocol.ChatQuery{SessionID: "ses_empty"}}
|
||||
unsupported := automationReply(t, request, scriptedChat(), store)
|
||||
if unsupported.Type != protocol.ChatAutomations || unsupported.ChatAutomations == nil || unsupported.ChatAutomations.Status != protocol.ChatAutomationUnavailable || unsupported.ChatAutomations.ActiveCount != nil {
|
||||
t.Fatalf("unsupported automation read = %+v", unsupported)
|
||||
}
|
||||
empty := 0
|
||||
broker := &automationChat{fakeChat: scriptedChat(), info: protocol.ChatAutomationInfo{SessionID: "ses_empty", Provider: "opencode", Status: protocol.ChatAutomationSupported, ActiveCount: &empty}}
|
||||
supported := automationReply(t, request, broker, store)
|
||||
if supported.Type != protocol.ChatAutomations || supported.ChatAutomations == nil || supported.ChatAutomations.Status != protocol.ChatAutomationSupported || supported.ChatAutomations.ActiveCount == nil || *supported.ChatAutomations.ActiveCount != 0 {
|
||||
t.Fatalf("empty automation read = %+v", supported)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatAutomationsRejectsMalformedSessionBounds(t *testing.T) {
|
||||
broker := &automationChat{fakeChat: scriptedChat()}
|
||||
for _, query := range []*protocol.ChatQuery{
|
||||
nil,
|
||||
{SessionID: ""},
|
||||
{SessionID: "not-a-session"},
|
||||
{SessionID: "ses_" + string(make([]byte, 201))},
|
||||
} {
|
||||
reply := automationReply(t, protocol.Message{Type: protocol.ChatAutomations, ID: "a3", ChatQuery: query}, broker, nil)
|
||||
if reply.Type != protocol.Error {
|
||||
t.Fatalf("malformed query %+v returned %+v", query, reply)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatAutomationsRejectsMalformedProviderMetadata(t *testing.T) {
|
||||
store := newOperatorStore(t)
|
||||
if err := store.RecordOperatorSession(context.Background(), "ses_bad", t.TempDir(), "bad", protocol.ChatSessionTagOperator); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
count := -1
|
||||
broker := &automationChat{fakeChat: scriptedChat(), info: protocol.ChatAutomationInfo{SessionID: "ses_bad", Status: protocol.ChatAutomationSupported, ActiveCount: &count}}
|
||||
reply := automationReply(t, protocol.Message{Type: protocol.ChatAutomations, ID: "a4", ChatQuery: &protocol.ChatQuery{SessionID: "ses_bad"}}, broker, store)
|
||||
if reply.Type != protocol.Error {
|
||||
t.Fatalf("malformed provider metadata = %+v", reply)
|
||||
}
|
||||
}
|
||||
@@ -332,6 +332,21 @@ func (m *chatManager) brokerFor(ctx context.Context, sessionID string) (chatBrok
|
||||
return adapter, nil
|
||||
}
|
||||
|
||||
// Automations routes the optional readback capability without adding it to the
|
||||
// base broker contract. The session's registry-selected provider remains the
|
||||
// ownership boundary; no provider-wide or cwd-wide schedule list is consulted.
|
||||
func (m *chatManager) Automations(ctx context.Context, sessionID string) (protocol.ChatAutomationInfo, error) {
|
||||
adapter, err := m.brokerFor(ctx, sessionID)
|
||||
if err != nil {
|
||||
return protocol.ChatAutomationInfo{SessionID: sessionID, Status: protocol.ChatAutomationUnavailable, Reason: err.Error()}, nil
|
||||
}
|
||||
reader, ok := adapter.(chatAutomationReader)
|
||||
if !ok {
|
||||
return protocol.ChatAutomationInfo{SessionID: sessionID, Provider: m.backendFor(ctx, sessionID), Status: protocol.ChatAutomationUnavailable, Reason: "this chat provider has no authoritative scheduled-work read API"}, nil
|
||||
}
|
||||
return reader.Automations(ctx, sessionID)
|
||||
}
|
||||
|
||||
// chatTitleSetter is a harness that names its own sessions from the operator's
|
||||
// chat title (Claude Code passes it as --name on the next spawn). SetTitle
|
||||
// records the title in memory only and does no I/O.
|
||||
|
||||
Reference in New Issue
Block a user