Poll OpenCode permission requests and forward chat_reply

This commit is contained in:
2026-10-16 08:06:39 +01:00
parent 9d1676df0a
commit 3ff5cd8838
10 changed files with 889 additions and 40 deletions
+158 -7
View File
@@ -2,6 +2,7 @@ package server
import (
"context"
"errors"
"log"
"strings"
"sync"
@@ -22,9 +23,22 @@ type chatBroker interface {
Stream(ctx context.Context, sessionID string, ready func(), emit func(protocol.ChatEventFrame) error) error
Interrupt(ctx context.Context, sessionID string) error
Messages(ctx context.Context, sessionID string, limit int) ([]protocol.ChatMessage, error)
// Permissions lists the session's pending OpenCode approval requests so the
// handler can push chat_permission frames.
Permissions(ctx context.Context, sessionID string) ([]protocol.ChatPermissionFrame, error)
Reply(ctx context.Context, request protocol.ChatReplyRequest) error
}
// chatPermissionPollInterval is the fixed interval between permission polls: one
// GET per poll through the shared Connection bounds, never a busy loop. The first
// poll runs immediately so a request pending when the prompt is sent is surfaced
// without waiting a full interval.
const chatPermissionPollInterval = 2 * time.Second
// chatPermissionRetainedLimit bounds the dedup set of already-pushed permission
// request ids one session retains, so a long turn cannot grow it without limit.
const chatPermissionRetainedLimit = 256
// chatRetainedLimit bounds the state one session may retain so a stalled reader
// or a runaway turn cannot grow memory without limit. It matches the OpenCode
// response bound (1 MiB).
@@ -69,6 +83,11 @@ type chatTurn struct {
size int
overflow bool
updated time.Time
// permissions holds the request ids already pushed to the client so a
// repeated poll cannot push the same pending request twice; permissionOrder
// bounds it to chatPermissionRetainedLimit.
permissions map[string]struct{}
permissionOrder []string
}
func newChatHub() *chatHub {
@@ -93,13 +112,7 @@ func (h *chatHub) apply(frame protocol.ChatEventFrame) {
}
h.mu.Lock()
defer h.mu.Unlock()
turn := h.turns[frame.SessionID]
if turn == nil {
h.evictLocked()
turn = &chatTurn{sessionID: frame.SessionID, messages: map[string]*chatMessage{}}
h.turns[frame.SessionID] = turn
}
turn.updated = h.clock()
turn := h.turnLocked(frame.SessionID)
switch frame.Kind {
case protocol.ChatEventStep:
turn.step = frame.Step
@@ -210,6 +223,21 @@ func (h *chatHub) evictLocked() {
}
}
// turnLocked returns the session's retained turn, creating it (and evicting the
// least-recently-updated turn first when the cap is reached) and refreshing its
// update time. It is the shared turn-initialization path for apply and
// seenPermission, so the two cannot drift. Callers must hold h.mu.
func (h *chatHub) turnLocked(sessionID string) *chatTurn {
turn := h.turns[sessionID]
if turn == nil {
h.evictLocked()
turn = &chatTurn{sessionID: sessionID, messages: map[string]*chatMessage{}}
h.turns[sessionID] = turn
}
turn.updated = h.clock()
return turn
}
// retain records n more bytes and reports whether the turn is still within the
// bound. The first overflow records one diagnostic and then drops further state
// instead of growing without limit.
@@ -265,6 +293,55 @@ func (h *chatHub) snapshot(sessionID string) []protocol.ChatMessage {
return messages
}
// seenPermission records requestID as pushed for the session and reports whether
// it is newly seen (true) or a duplicate (false). The retained ids are bounded:
// the oldest is dropped once the session exceeds chatPermissionRetainedLimit, so
// the same pending request is pushed at most once while the state stays bounded.
func (h *chatHub) seenPermission(sessionID, requestID string) bool {
if sessionID == "" || requestID == "" {
return false
}
h.mu.Lock()
defer h.mu.Unlock()
turn := h.turnLocked(sessionID)
if turn.permissions == nil {
turn.permissions = map[string]struct{}{}
}
if _, ok := turn.permissions[requestID]; ok {
return false
}
turn.permissions[requestID] = struct{}{}
turn.permissionOrder = append(turn.permissionOrder, requestID)
if len(turn.permissionOrder) > chatPermissionRetainedLimit {
oldest := turn.permissionOrder[0]
turn.permissionOrder = turn.permissionOrder[1:]
delete(turn.permissions, oldest)
}
return true
}
// forgetPermission releases a request id reserved by seenPermission when its
// frame could not be written, so a later poll re-pushes it instead of the id
// staying "seen" while the phone never received the request.
func (h *chatHub) forgetPermission(sessionID, requestID string) {
if sessionID == "" || requestID == "" {
return
}
h.mu.Lock()
defer h.mu.Unlock()
turn := h.turns[sessionID]
if turn == nil {
return
}
delete(turn.permissions, requestID)
for i, id := range turn.permissionOrder {
if id == requestID {
turn.permissionOrder = append(turn.permissionOrder[:i], turn.permissionOrder[i+1:]...)
break
}
}
}
// handleChat dispatches one chat_* request. It returns true when the request was
// a chat type and has been fully answered (the caller must not fall through to
// the generic response). Frames are pushed after the ack, each through the
@@ -388,6 +465,70 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat
}
bufferMu.Unlock()
go drainChatStream(ctx, streamResult, streamCancel, sessionID, write)
// Poll for pending approval requests alongside the event stream, sharing
// streamCtx so the poller stops when the stream ends or the client
// disconnects. The first poll runs immediately.
go pollChatPermissions(streamCtx, broker, hub, sessionID, write)
}
// pollChatPermissions polls the broker's pending-approval list on a fixed
// interval, sharing the chat subscription's context so it stops on disconnect or
// when the turn's stream ends. The hub dedups the request ids, so each pending
// request is pushed at most once; a poll failure is surfaced once through the
// same write closure instead of staying silent. A backend that does not expose
// the approval surface (opencode.ErrPermissionUnsupported) stops the poller
// quietly, so the documented limitation is not reported as a per-prompt error.
func pollChatPermissions(ctx context.Context, broker chatBroker, hub *chatHub, sessionID string, write func(protocol.Message) error) {
ticker := time.NewTicker(chatPermissionPollInterval)
defer ticker.Stop()
reported := false
for {
if err := pushChatPermissions(ctx, broker, hub, sessionID, write); err != nil {
if ctx.Err() != nil {
return
}
if errors.Is(err, opencode.ErrPermissionUnsupported) {
log.Printf("chat permission poll session=%s: %v", sessionID, err)
return
}
if reported {
log.Printf("chat permission poll session=%s: %v", sessionID, err)
} else {
reported = true
_ = write(protocol.Message{Type: protocol.Error, Error: "chat permission poll: " + err.Error()})
}
}
select {
case <-ctx.Done():
return
case <-ticker.C:
}
}
}
// pushChatPermissions performs one poll and writes a chat_permission frame for
// every newly seen request of the session, through the same connection-scoped
// write closure as the chat stream. It never re-pushes a request id, and releases
// the reservation on a write failure so the frame is not silently dropped.
func pushChatPermissions(ctx context.Context, broker chatBroker, hub *chatHub, sessionID string, write func(protocol.Message) error) error {
pending, err := broker.Permissions(ctx, sessionID)
if err != nil {
return err
}
for _, request := range pending {
if request.SessionID != sessionID || request.RequestID == "" {
continue
}
if !hub.seenPermission(sessionID, request.RequestID) {
continue
}
frame := request
if err := write(protocol.Message{Type: protocol.ChatPermission, ChatPermission: &frame}); err != nil {
hub.forgetPermission(sessionID, request.RequestID)
return err
}
}
return nil
}
// drainChatStream reports the terminal error of a chat subscription and releases
@@ -450,6 +591,16 @@ func handleChatSimple(ctx context.Context, request protocol.Message, broker chat
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
return
}
if strings.TrimSpace(reply.RequestID) == "" {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a permission request id is required"})
return
}
switch reply.Decision {
case protocol.ChatDecisionOnce, protocol.ChatDecisionAlways, protocol.ChatDecisionReject:
default:
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid permission decision is required"})
return
}
if err := broker.Reply(ctx, *reply); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
+322 -2
View File
@@ -26,10 +26,19 @@ type fakeChat struct {
promptCalls int
interrupts int
replies int
replyRequest protocol.ChatReplyRequest
replyErr error
messageCalls int
messageSessionID string
messageLimit int
messages []protocol.ChatMessage
permissionCalls int
permissionSessionID string
permissions []protocol.ChatPermissionFrame
permissionErr error
permBlock bool
permDone chan struct{}
permOnce sync.Once
script []protocol.ChatEventFrame
started chan struct{}
done chan struct{}
@@ -102,11 +111,49 @@ func (f *fakeChat) messageQuery() (int, string, int) {
return f.messageCalls, f.messageSessionID, f.messageLimit
}
func (f *fakeChat) Reply(context.Context, protocol.ChatReplyRequest) error {
func (f *fakeChat) Reply(_ context.Context, request protocol.ChatReplyRequest) error {
f.mu.Lock()
defer f.mu.Unlock()
f.replies++
return nil
f.replyRequest = request
return f.replyErr
}
// Permissions returns the seeded pending requests, or blocks until the caller's
// context is canceled when permBlock is set (used to prove the poller stops on
// disconnect). Every call is counted so a test can assert polling happened.
func (f *fakeChat) Permissions(ctx context.Context, sessionID string) ([]protocol.ChatPermissionFrame, error) {
f.mu.Lock()
f.permissionCalls++
f.permissionSessionID = sessionID
frames := append([]protocol.ChatPermissionFrame(nil), f.permissions...)
err := f.permissionErr
block := f.permBlock
done := f.permDone
f.mu.Unlock()
if block {
<-ctx.Done()
if done != nil {
f.permOnce.Do(func() { close(done) })
}
return nil, ctx.Err()
}
return frames, err
}
// permissionQuery reports the recorded permission poll count and session id.
func (f *fakeChat) permissionQuery() (int, string) {
f.mu.Lock()
defer f.mu.Unlock()
return f.permissionCalls, f.permissionSessionID
}
// replyQuery reports the recorded chat_reply count and the last forwarded
// request, so a test can assert the exact session/request/decision.
func (f *fakeChat) replyQuery() (int, protocol.ChatReplyRequest) {
f.mu.Lock()
defer f.mu.Unlock()
return f.replies, f.replyRequest
}
func (f *fakeChat) counts() (int, int, int) {
@@ -508,6 +555,9 @@ func TestChatInterruptEndsRunningPrompt(t *testing.T) {
mu.Unlock()
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"data":{"id":"msg_1","sessionID":"ses_1","type":"user"}}`))
case r.Method == http.MethodGet && r.URL.Path == "/api/permission/request":
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"location":{"directory":"/work"},"data":[]}`))
case r.Method == http.MethodGet && r.URL.Path == "/api/event":
flusher, ok := w.(http.Flusher)
if !ok {
@@ -758,6 +808,9 @@ func realAdapterChat(t *testing.T, frames []string) []protocol.ChatEventFrame {
prompts.Add(1)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"data":{"id":"msg_1","sessionID":"ses_1","type":"user"}}`))
case r.Method == http.MethodGet && r.URL.Path == "/api/permission/request":
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"location":{"directory":"/work"},"data":[]}`))
case r.Method == http.MethodGet && r.URL.Path == "/api/event":
streams.Add(1)
w.Header().Set("Content-Type", "text/event-stream")
@@ -894,3 +947,270 @@ func TestChatSessionsAndCreateAnswerExplicitly(t *testing.T) {
}
}
}
// TestChatPromptPushesPermissionAndForwardsReply is the task evidence: with a
// blocking scripted stream keeping the subscription open, a seeded pending
// approval request is pushed as a chat_permission frame carrying the seeded
// session/request id after the prompt ack, and a chat_reply with Decision "once"
// is forwarded to the broker with the matching SessionID/RequestID/Decision.
//
// The live end-to-end approval flow against the real OpenCode service is NOT
// tested: this broker is the same interface seam the production handler uses, and
// the adapter's exact reply path/body are covered by the opencode package tests
// over an httptest fixture.
func TestChatPromptPushesPermissionAndForwardsReply(t *testing.T) {
broker := scriptedChat()
broker.block = true
broker.started = make(chan struct{})
broker.permissions = []protocol.ChatPermissionFrame{{
SessionID: "ses_1", RequestID: "req_1", Action: "bash", Resources: []string{"rm -rf /tmp"},
}}
ctx, _, conn := dialChat(t, broker)
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "run it"}}); err != nil {
t.Fatal(err)
}
var ack protocol.Message
if err := wsjson.Read(ctx, conn, &ack); err != nil {
t.Fatal(err)
}
if ack.Type != protocol.ChatPrompt || ack.ID != "r1" {
t.Fatalf("prompt ack = %+v", ack)
}
var permission protocol.Message
if err := wsjson.Read(ctx, conn, &permission); err != nil {
t.Fatal(err)
}
if permission.Type == protocol.Error {
t.Fatalf("unexpected error frame: %s", permission.Error)
}
if permission.Type != protocol.ChatPermission || permission.ChatPermission == nil {
t.Fatalf("expected a chat_permission frame, got %+v", permission)
}
if permission.ChatPermission.SessionID != "ses_1" || permission.ChatPermission.RequestID != "req_1" || permission.ChatPermission.Action != "bash" {
t.Fatalf("permission frame = %+v", permission.ChatPermission)
}
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatReply, ID: "p1", ChatReply: &protocol.ChatReplyRequest{SessionID: "ses_1", RequestID: "req_1", Decision: protocol.ChatDecisionOnce}}); err != nil {
t.Fatal(err)
}
var response protocol.Message
if err := wsjson.Read(ctx, conn, &response); err != nil {
t.Fatal(err)
}
if response.Type != protocol.ChatReply || response.ID != "p1" {
t.Fatalf("reply response = %+v", response)
}
count, forwarded := broker.replyQuery()
if count != 1 {
t.Fatalf("reply forwarded %d times, want 1", count)
}
if forwarded.SessionID != "ses_1" || forwarded.RequestID != "req_1" || forwarded.Decision != protocol.ChatDecisionOnce {
t.Fatalf("forwarded reply = %+v", forwarded)
}
}
// TestChatPermissionDedupesRepeatedPolls pins that the poller pushes each
// pending request at most once even though it re-reads the same list, and that a
// foreign session's request is never surfaced.
func TestChatPermissionDedupesRepeatedPolls(t *testing.T) {
broker := scriptedChat()
broker.permissions = []protocol.ChatPermissionFrame{
{SessionID: "ses_1", RequestID: "req_1", Action: "bash"},
{SessionID: "ses_other", RequestID: "req_x", Action: "edit"},
}
hub := newChatHub()
var mu sync.Mutex
var pushed []protocol.Message
write := func(message protocol.Message) error {
mu.Lock()
pushed = append(pushed, message)
mu.Unlock()
return nil
}
for i := 0; i < 3; i++ {
if err := pushChatPermissions(context.Background(), broker, hub, "ses_1", write); err != nil {
t.Fatal(err)
}
}
mu.Lock()
defer mu.Unlock()
if len(pushed) != 1 {
t.Fatalf("pushed %d permission frames, want 1: %+v", len(pushed), pushed)
}
if pushed[0].Type != protocol.ChatPermission || pushed[0].ChatPermission == nil || pushed[0].ChatPermission.RequestID != "req_1" {
t.Fatalf("pushed frame = %+v", pushed[0])
}
if calls, sessionID := broker.permissionQuery(); calls != 3 || sessionID != "ses_1" {
t.Fatalf("permission polls = %d/%q, want 3/ses_1", calls, sessionID)
}
}
// TestChatDisconnectCancelsPermissionPoller pins that the permission poller is
// canceled on disconnect (reusing the stream's context), so it cannot leak a
// goroutine after the client goes away.
func TestChatDisconnectCancelsPermissionPoller(t *testing.T) {
broker := scriptedChat()
broker.block = true
broker.started = make(chan struct{})
broker.done = make(chan struct{})
broker.permBlock = true
broker.permDone = make(chan struct{})
ctx, _, conn := dialChat(t, broker)
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "hi"}}); err != nil {
t.Fatal(err)
}
var ack protocol.Message
if err := wsjson.Read(ctx, conn, &ack); err != nil {
t.Fatal(err)
}
select {
case <-broker.started:
case <-time.After(2 * time.Second):
t.Fatal("stream never started")
}
select {
case <-broker.permDone:
t.Fatal("permission poller returned before disconnect")
default:
}
conn.CloseNow()
select {
case <-broker.permDone:
case <-time.After(3 * time.Second):
t.Fatal("permission poller was not canceled on disconnect")
}
select {
case <-broker.done:
case <-time.After(3 * time.Second):
t.Fatal("chat stream was not canceled on disconnect")
}
}
// TestChatPermissionPollErrorSurfaced pins that a malformed permission list is
// reported to the client as an error frame rather than silence.
func TestChatPermissionPollErrorSurfaced(t *testing.T) {
broker := scriptedChat()
broker.block = true
broker.started = make(chan struct{})
broker.permissionErr = fmt.Errorf("invalid OpenCode permission response")
ctx, _, conn := dialChat(t, broker)
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "hi"}}); err != nil {
t.Fatal(err)
}
var ack protocol.Message
if err := wsjson.Read(ctx, conn, &ack); err != nil {
t.Fatal(err)
}
var response protocol.Message
if err := wsjson.Read(ctx, conn, &response); err != nil {
t.Fatal(err)
}
if response.Type != protocol.Error || !strings.Contains(response.Error, "permission poll") {
t.Fatalf("malformed permission list not surfaced: %+v", response)
}
}
// TestChatReplyRejectsInvalidInput pins that a missing session id, an empty
// request id and an invalid decision are rejected with a protocol.Error before
// any broker call.
func TestChatReplyRejectsInvalidInput(t *testing.T) {
broker := scriptedChat()
ctx, _, conn := dialChat(t, broker)
cases := []*protocol.ChatReplyRequest{
nil,
{SessionID: "", RequestID: "req_1", Decision: protocol.ChatDecisionOnce},
{SessionID: "not-a-session", RequestID: "req_1", Decision: protocol.ChatDecisionOnce},
{SessionID: "ses_1", RequestID: "", Decision: protocol.ChatDecisionOnce},
{SessionID: "ses_1", RequestID: "req_1", Decision: ""},
{SessionID: "ses_1", RequestID: "req_1", Decision: "maybe"},
}
for _, reply := range cases {
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatReply, ID: "bad", ChatReply: reply}); err != nil {
t.Fatal(err)
}
var response protocol.Message
if err := wsjson.Read(ctx, conn, &response); err != nil {
t.Fatal(err)
}
if response.Type != protocol.Error || response.ID != "bad" {
t.Fatalf("reply %+v not rejected: %+v", reply, response)
}
}
if count, _ := broker.replyQuery(); count != 0 {
t.Fatalf("invalid replies reached the broker: %d", count)
}
}
// TestChatReplyWithoutBroker pins that an unconfigured server answers chat_reply
// with the existing "not configured" error.
func TestChatReplyWithoutBroker(t *testing.T) {
ctx, _, conn := dialChat(t, nil)
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatReply, ID: "p1", ChatReply: &protocol.ChatReplyRequest{SessionID: "ses_1", RequestID: "req_1", Decision: protocol.ChatDecisionOnce}}); err != nil {
t.Fatal(err)
}
var response protocol.Message
if err := wsjson.Read(ctx, conn, &response); err != nil {
t.Fatal(err)
}
if response.Type != protocol.Error || !strings.Contains(response.Error, "not configured") {
t.Fatalf("unconfigured reply not rejected: %+v", response)
}
}
// TestChatPermissionUnsupportedBackendIsQuiet pins that a backend which does not
// expose the approval surface (the documented native v1 limitation, reported as
// opencode.ErrPermissionUnsupported) stops the poller without writing a
// per-prompt error frame, so a working prompt is not misread as a failure.
func TestChatPermissionUnsupportedBackendIsQuiet(t *testing.T) {
broker := scriptedChat()
broker.block = true
broker.started = make(chan struct{})
broker.permissionErr = opencode.ErrPermissionUnsupported
ctx, _, conn := dialChat(t, broker)
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "hi"}}); err != nil {
t.Fatal(err)
}
var ack protocol.Message
if err := wsjson.Read(ctx, conn, &ack); err != nil {
t.Fatal(err)
}
if ack.Type != protocol.ChatPrompt || ack.ID != "r1" {
t.Fatalf("prompt ack = %+v", ack)
}
// No error frame may follow: a read with a short deadline must time out.
readCtx, cancel := context.WithTimeout(ctx, 300*time.Millisecond)
defer cancel()
var unexpected protocol.Message
if err := wsjson.Read(readCtx, conn, &unexpected); err == nil {
t.Fatalf("unsupported permission surface must not write a frame, got %+v", unexpected)
}
if calls, _ := broker.permissionQuery(); calls != 1 {
t.Fatalf("permission polls = %d, want 1 (the poller must stop on the documented limitation)", calls)
}
}
// TestChatPermissionWriteFailureDoesNotDropRequest pins that a request id is not
// retained as "seen" when its frame could not be written, so the next poll
// re-pushes it instead of silently dropping the approval request.
func TestChatPermissionWriteFailureDoesNotDropRequest(t *testing.T) {
broker := scriptedChat()
broker.permissions = []protocol.ChatPermissionFrame{{SessionID: "ses_1", RequestID: "req_1", Action: "bash"}}
hub := newChatHub()
attempts := 0
write := func(protocol.Message) error {
attempts++
if attempts == 1 {
return fmt.Errorf("connection closed")
}
return nil
}
if err := pushChatPermissions(context.Background(), broker, hub, "ses_1", write); err == nil {
t.Fatal("a failed write must surface an error")
}
if err := pushChatPermissions(context.Background(), broker, hub, "ses_1", write); err != nil {
t.Fatalf("retry after a failed write: %v", err)
}
if attempts != 2 {
t.Fatalf("push attempts = %d, want the request re-pushed after the failure", attempts)
}
}