fix(chat): recover web approvals and questions across clients and reconnects
This commit is contained in:
@@ -3,11 +3,13 @@ package server
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -15,7 +17,53 @@ import (
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
type browserModelFixture struct{ *fakeChat }
|
||||
type browserModelFixture struct {
|
||||
*fakeChat
|
||||
forms *browserForms
|
||||
}
|
||||
|
||||
type browserForms struct {
|
||||
mu sync.Mutex
|
||||
pending []protocol.ChatForm
|
||||
answered int
|
||||
}
|
||||
|
||||
func (b browserModelFixture) Forms(context.Context, string) ([]protocol.ChatForm, error) {
|
||||
b.forms.mu.Lock()
|
||||
defer b.forms.mu.Unlock()
|
||||
return append([]protocol.ChatForm(nil), b.forms.pending...), nil
|
||||
}
|
||||
|
||||
func (b browserModelFixture) Answer(_ context.Context, answer protocol.ChatAnswerRequest) error {
|
||||
b.forms.mu.Lock()
|
||||
defer b.forms.mu.Unlock()
|
||||
if answer.Answers["answer"] != "Browser answer" {
|
||||
return fmt.Errorf("unexpected answer")
|
||||
}
|
||||
for i, form := range b.forms.pending {
|
||||
if form.ID == answer.FormID {
|
||||
b.forms.pending = append(b.forms.pending[:i], b.forms.pending[i+1:]...)
|
||||
b.forms.answered++
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("unknown form")
|
||||
}
|
||||
|
||||
func (b browserModelFixture) Reply(ctx context.Context, answer protocol.ChatReplyRequest) error {
|
||||
if err := b.fakeChat.Reply(ctx, answer); err != nil {
|
||||
return err
|
||||
}
|
||||
b.fakeChat.mu.Lock()
|
||||
defer b.fakeChat.mu.Unlock()
|
||||
for i, p := range b.fakeChat.permissions {
|
||||
if p.RequestID == answer.RequestID {
|
||||
b.fakeChat.permissions = append(b.fakeChat.permissions[:i], b.fakeChat.permissions[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b browserModelFixture) Models(ctx context.Context, id string) ([]protocol.ChatModel, error) {
|
||||
select {
|
||||
@@ -57,9 +105,27 @@ func TestChatBrowserLifecycleAndModelDialog(t *testing.T) {
|
||||
{ID: "msg_user", Role: protocol.ChatRoleUser, Parts: []protocol.ChatPart{{Type: "text", Text: strings.Repeat("Earlier scratch prompt line\n", 100)}}},
|
||||
{ID: "msg_answer", Role: protocol.ChatRoleAssistant, Parts: []protocol.ChatPart{{Type: "reasoning", Text: "Controlled reasoning evidence"}, {Type: "text", Text: "Controlled answer"}}},
|
||||
}
|
||||
handler := HandlerWithOptions(ctx, Options{Chat: browserModelFixture{fake}}, store)
|
||||
forms := &browserForms{}
|
||||
handler := HandlerWithOptions(ctx, Options{Chat: browserModelFixture{fake, forms}}, store)
|
||||
defer func() { cancel(); WaitRuns(handler) }()
|
||||
srv := httptest.NewServer(handler)
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
if req.URL.Path == "/fixture-attention" && req.Method == http.MethodPost {
|
||||
fake.mu.Lock()
|
||||
fake.permissions = []protocol.ChatPermissionFrame{
|
||||
{SessionID: session.ID, RequestID: "per_first", Action: "shell", Resources: []string{"Controlled first command"}},
|
||||
{SessionID: session.ID, RequestID: "per_second", Action: "edit", Resources: []string{"Controlled second path"}},
|
||||
}
|
||||
fake.mu.Unlock()
|
||||
forms.mu.Lock()
|
||||
for _, id := range []string{"form_first", "form_second"} {
|
||||
forms.pending = append(forms.pending, protocol.ChatForm{ID: id, SessionID: session.ID, Title: "Recovered question", Fields: []protocol.ChatFormField{{Key: "answer", Type: "text", Title: "Your answer", Required: true}}})
|
||||
}
|
||||
forms.mu.Unlock()
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
handler.ServeHTTP(w, req)
|
||||
}))
|
||||
defer srv.Close()
|
||||
cmd := exec.CommandContext(ctx, "node", "../../web/test/chat-hardening-probe.mjs", strings.TrimPrefix(srv.URL, "http://"), session.ID)
|
||||
if output, err := cmd.CombinedOutput(); err != nil {
|
||||
@@ -67,4 +133,13 @@ func TestChatBrowserLifecycleAndModelDialog(t *testing.T) {
|
||||
} else {
|
||||
t.Log(string(output))
|
||||
}
|
||||
fake.mu.Lock()
|
||||
replies := fake.replies
|
||||
fake.mu.Unlock()
|
||||
forms.mu.Lock()
|
||||
answers := forms.answered
|
||||
forms.mu.Unlock()
|
||||
if replies != 2 || answers != 2 {
|
||||
t.Fatalf("browser did not deliver all interactions: approvals=%d answers=%d", replies, answers)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user