Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions internal/host/harness/claudecode/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,12 +502,12 @@ func (rt *runtime) prompt(in session.TurnInput, priority string) error {
// Nothing to steer into: the CLI would make the message a turn of its own, which the
// service would not know was coming. The service queues it instead.
rt.mu.Unlock()
return &TurnEndedError{Reason: "no turn is running"}
return &session.TurnEndedError{Reason: "no turn is running"}
} else if rt.tr.Ended() != in.TurnsEnded {
// The turn the service meant has ended here, and it has not heard yet: the message
// would land in a turn it does not know of.
rt.mu.Unlock()
return &TurnEndedError{Reason: "the turn it was meant for has ended"}
return &session.TurnEndedError{Reason: "the turn it was meant for has ended"}
} else {
rt.tr.Steered(id, in.MessageID) // the turn's result says if it was never taken in
}
Expand All @@ -516,17 +516,6 @@ func (rt *runtime) prompt(in session.TurnInput, priority string) error {
return rt.write(UserLine(native, id, in.Text, images, priority))
}

// TurnEndedError is what Steer returns when there is no turn to add the message to. The message
// was not delivered; the service makes it a turn of its own.
type TurnEndedError struct{ Reason string }

func (e *TurnEndedError) Error() string {
return "the turn had already ended, so the message was not added to it (" + e.Reason + ")"
}

// TurnEnded marks the error for a caller that cannot name this package's types.
func (e *TurnEndedError) TurnEnded() bool { return true }

func (rt *runtime) StartTurn(in session.TurnInput) error { return rt.prompt(in, "") }

// Steer adds a message to the turn in progress. A turn the CLI has just finished, whose result
Expand Down
16 changes: 2 additions & 14 deletions internal/host/harness/codex/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,25 +478,13 @@ func (rt *runtime) awaitTurn(endedBefore string, limit time.Duration) string {
}
}

// TurnEndedError is what Steer returns when there is no turn to add the message to: it ended
// before the message got there. The message was not delivered, and was not made into a turn of
// its own; that is the caller's decision.
type TurnEndedError struct{ Reason string }

func (e *TurnEndedError) Error() string {
return "the turn had already ended, so the message was not added to it (" + e.Reason + ")"
}

// TurnEnded marks the error for a caller that cannot name this package's types.
func (e *TurnEndedError) TurnEnded() bool { return true }

// Steer adds a message to the turn in progress.
func (rt *runtime) Steer(in session.TurnInput) error {
rt.mu.Lock()
thread, turn := rt.tr.ThreadID(), rt.tr.TurnID()
rt.mu.Unlock()
if turn == "" {
return &TurnEndedError{Reason: "no turn is running"}
return &session.TurnEndedError{Reason: "no turn is running"}
}
params := map[string]any{"threadId": thread, "expectedTurnId": turn, "input": input(in)}
if in.MessageID != "" {
Expand All @@ -509,7 +497,7 @@ func (rt *runtime) Steer(in session.TurnInput) error {
strings.Contains(err.Error(), "cannot steer a compact turn")) {
// Codex refused because the turn it was meant for is over (seen as "no active turn to
// steer"), or a newer one has taken its place.
return &TurnEndedError{Reason: err.Error()}
return &session.TurnEndedError{Reason: err.Error()}
}
return err
}
Expand Down
12 changes: 12 additions & 0 deletions internal/host/session/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ type TurnInput struct {
TurnsEnded int
}

// TurnEndedError is what Steer returns when there is no turn to add the message to: it ended
// before the message got there. The message was not delivered; the service makes it a turn of
// its own.
type TurnEndedError struct{ Reason string }

func (e *TurnEndedError) Error() string {
return "the turn had already ended, so the message was not added to it (" + e.Reason + ")"
}

// TurnEnded marks the error (steerFailed matches on this method).
func (e *TurnEndedError) TurnEnded() bool { return true }

type Answer struct {
Decision string
Answers map[string][]string
Expand Down
24 changes: 12 additions & 12 deletions internal/host/session/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,14 +742,9 @@ func (s *Service) planSendNow(ctx context.Context, l *live, sess *Session, w *Wr
}
now := time.Now().UTC()
if s.canSteer(l, sess) {
var b map[string]any
if json.Unmarshal(item.Body, &b) != nil || b == nil {
b = map[string]any{}
}
b["intent"] = "steer"
// It joins the running turn where it lands now, after what that turn has said so far.
l.itemOrd++
item.TurnID, item.Ordinal, item.StartedAt, item.EndedAt, item.Body = *sess.ActiveTurnID, l.itemOrd, now, &now, body(b)
item.TurnID, item.Ordinal, item.StartedAt, item.EndedAt, item.Body = *sess.ActiveTurnID, l.itemOrd, now, &now, withIntent(item.Body, "steer")
w.Changes = append(w.Changes, TurnRemoved{SessionID: sess.ID, TurnID: turn.ID}, item, touch(sess))
return []func(){s.steerEffect(w, l, sess, in)}, nil
}
Expand All @@ -762,6 +757,16 @@ func (s *Service) planSendNow(ctx context.Context, l *live, sess *Session, w *Wr
return []func(){s.startTurn(w, sess.ID, turn.ID, in)}, nil
}

// withIntent is a user message's body with its delivery intent changed.
func withIntent(raw json.RawMessage, intent string) json.RawMessage {
var b map[string]any
if json.Unmarshal(raw, &b) != nil || b == nil {
b = map[string]any{}
}
b["intent"] = intent
return body(b)
}

// steerFailed deals with a message the harness did not add to the running turn, so it is never
// lost without a word. A harness that found the turn already over has not delivered it: the
// message becomes a queued turn of its own, started now if nothing else is running or else
Expand Down Expand Up @@ -806,14 +811,9 @@ func (s *Service) steerFailed(sessionID string, in TurnInput, cause error) error
}
l.turnOrd++
turn := &Turn{ID: newID("trn"), SessionID: sessionID, Ordinal: l.turnOrd, Status: status, Trigger: "user", StartedAt: now}
var b map[string]any
if json.Unmarshal(item.Body, &b) != nil || b == nil {
b = map[string]any{}
}
b["intent"] = "queued"
// It moves to the end, after what the turn it missed went on to say.
l.itemOrd++
item.TurnID, item.Ordinal, item.Body = turn.ID, l.itemOrd, body(b)
item.TurnID, item.Ordinal, item.Body = turn.ID, l.itemOrd, withIntent(item.Body, "queued")
w.Changes = append(w.Changes, turn, item)
if status == TurnStarting {
sess.Status, sess.ActiveTurnID, sess.LastError = StatusStarting, &turn.ID, nil
Expand Down
9 changes: 2 additions & 7 deletions internal/mcpserver/agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ func (s *Server) agentCreate(ctx context.Context, req *mcp.CallToolRequest, in A
defer cancel()
c, h, ask, early, err := s.reach(ctx, req, u, in.Machine, peer.ScopeSteer)
if c == nil {
var out AgentCreated
if early != nil {
out = AgentCreated{MachineID: h.ID, Access: early.Access, ApproveURL: early.ApproveURL, Error: early.Error}
}
return ask, out, err
return ask, AgentCreated{MachineID: h.ID, Access: early.Access, ApproveURL: early.ApproveURL, Error: early.Error}, err
}
defer c.Close()
// The MCP service handles the private prompt, then sends it over the encrypted channel.
Expand All @@ -69,8 +65,7 @@ func (s *Server) agentCreate(ctx context.Context, req *mcp.CallToolRequest, in A
if errors.As(err, &rpcErr) {
switch {
case needsSteer(err):
recordGrantRequest(ctx, c, u, peer.ScopeSteer)
ask, approval, err := s.machineApproval(u, req, h, peer.ScopeSteer)
ask, approval, err := s.askSteer(ctx, c, u, req, h)
return ask, AgentCreated{MachineID: h.ID, Access: approval.Access, ApproveURL: approval.ApproveURL, Error: "Choose View and steer sessions for this machine to create an agent."}, err
case rpcErr.Code == wire.CodeMethodNotFound || (rpcErr.Code == peer.CodeForbidden && strings.Contains(rpcErr.Message, `"admin" scope`)):
return nil, AgentCreated{}, fmt.Errorf("update Outpost on %s to a version that supports agent_create", h.Name)
Expand Down
7 changes: 7 additions & 0 deletions internal/mcpserver/grant_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ func needsSteer(err error) bool {
return errors.As(err, &rpcErr) && rpcErr.Code == peer.CodeForbidden && strings.Contains(rpcErr.Message, `"steer" scope`)
}

// askSteer answers a machine that refused a command for want of steer: it records the request
// on the machine and sends the person to approve steer there.
func (s *Server) askSteer(ctx context.Context, c *client.Conn, u user, req *mcp.CallToolRequest, h relayHost) (*mcp.CallToolResult, SessionRead, error) {
recordGrantRequest(ctx, c, u, peer.ScopeSteer)
return s.machineApproval(u, req, h, peer.ScopeSteer)
}

func (s *Server) machineApproval(u user, req *mcp.CallToolRequest, h relayHost, scope string) (*mcp.CallToolResult, SessionRead, error) {
key, err := s.key(u)
if err != nil {
Expand Down
48 changes: 24 additions & 24 deletions internal/mcpserver/icons.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mcpserver
import (
"embed"
"encoding/base64"
"strings"

"github.com/modelcontextprotocol/go-sdk/mcp"
)
Expand Down Expand Up @@ -39,30 +38,31 @@ func svgURI(svg []byte) string {
return "data:image/svg+xml;base64," + base64.StdEncoding.EncodeToString(svg)
}

// serverIcons is the brand mark for light and dark backgrounds; light first, for a client that
// takes the first it can show.
func serverIcons() []mcp.Icon {
light, _ := brand.ReadFile("icons/outpost-light.svg")
dark, _ := brand.ReadFile("icons/outpost-dark.svg")
return []mcp.Icon{
{Source: svgURI(light), MIMEType: "image/svg+xml", Sizes: []string{"any"}, Theme: mcp.IconThemeLight},
{Source: svgURI(dark), MIMEType: "image/svg+xml", Sizes: []string{"any"}, Theme: mcp.IconThemeDark},
}
}
// The service is stateless, so every request builds a fresh MCP server; the icons are encoded
// once here rather than on each build.
var (
brandIcons = func() []mcp.Icon {
light, _ := brand.ReadFile("icons/outpost-light.svg")
dark, _ := brand.ReadFile("icons/outpost-dark.svg")
return themed(svgURI(light), svgURI(dark))
}()
glyphIcons = func() map[string][]mcp.Icon {
out := make(map[string][]mcp.Icon, len(toolGlyphs))
for name, glyph := range toolGlyphs {
ink := func(color string) string {
return svgURI([]byte(`<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="` + color +
`" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">` + glyph + `</svg>`))
}
out[name] = themed(ink(inkOnLight), ink(inkOnDark))
}
return out
}()
)

// toolIcons is a tool's glyph inked for light and dark backgrounds; nil for a tool without one.
func toolIcons(name string) []mcp.Icon {
glyph, ok := toolGlyphs[name]
if !ok {
return nil
}
ink := func(color string) string {
return svgURI([]byte(strings.Join([]string{
`<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="`, color,
`" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">`, glyph, `</svg>`}, "")))
}
// themed is a light and a dark SVG; light first, for a client that takes the first it can show.
func themed(light, dark string) []mcp.Icon {
return []mcp.Icon{
{Source: ink(inkOnLight), MIMEType: "image/svg+xml", Sizes: []string{"any"}, Theme: mcp.IconThemeLight},
{Source: ink(inkOnDark), MIMEType: "image/svg+xml", Sizes: []string{"any"}, Theme: mcp.IconThemeDark},
{Source: light, MIMEType: "image/svg+xml", Sizes: []string{"any"}, Theme: mcp.IconThemeLight},
{Source: dark, MIMEType: "image/svg+xml", Sizes: []string{"any"}, Theme: mcp.IconThemeDark},
}
}
Loading
Loading