fix(chat): settle authoritative turns and make capabilities explicit

This commit is contained in:
2026-10-20 15:45:09 +01:00
parent 016fcb552a
commit e504780976
16 changed files with 615 additions and 56 deletions
+65 -2
View File
@@ -12,6 +12,7 @@ import (
"sort"
"strings"
"sync"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
@@ -729,10 +730,71 @@ func (c *Chat) Stream(ctx context.Context, id string, ready func(), emit func(pr
return err
}
defer body.Close()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var delivery sync.Mutex
state.mu.Lock()
busy := state.Active
state.mu.Unlock()
setBusy := func(value bool) {
busy = value
state.mu.Lock()
state.Active = value
state.mu.Unlock()
}
deliver := func(frame protocol.ChatEventFrame) error {
delivery.Lock()
defer delivery.Unlock()
if frame.Kind == protocol.ChatEventStep || frame.Kind == protocol.ChatEventText || frame.Kind == protocol.ChatEventTool || frame.Kind == protocol.ChatEventReasoning {
setBusy(true)
}
return emit(frame)
}
// V2 /api/session/active describes foreground drains, not individual
// provider steps. Poll it even after reconnect: a busy drain may have
// started before this SSE subscription. Failed readbacks never imply idle.
watchDone := make(chan struct{})
go func() {
defer close(watchDone)
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
}
delivery.Lock()
readCtx, readCancel := context.WithTimeout(ctx, 5*time.Second)
data, err := c.call(readCtx, "get", "/api/session/active", nil)
readCancel()
var emitErr error
var response struct {
Data *map[string]json.RawMessage `json:"data"`
}
if err == nil && json.Unmarshal(data, &response) == nil && response.Data != nil {
_, active := (*response.Data)[id]
if active && !busy {
setBusy(true)
emitErr = emit(protocol.ChatEventFrame{SessionID: id, Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepStarted}})
} else if !active && busy {
setBusy(false)
emitErr = emit(protocol.ChatEventFrame{SessionID: id, Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded}})
}
}
delivery.Unlock()
if emitErr != nil {
cancel()
_ = body.Close()
return
}
}
}()
defer func() { cancel(); <-watchDone }()
if ready != nil {
ready()
}
return c.scanChatEventsWithState(ctx, body, id, state, emit)
return c.scanChatEventsWithState(ctx, body, id, state, deliver)
}
// scanChatEvents turns an SSE body into protocol frames, one bounded frame at a
@@ -905,6 +967,7 @@ func (i *chatShellInfo) shellSessionID() string {
// (which carry no session) can be attributed to the right chat.
type ChatStreamState struct {
mu sync.Mutex
Active bool // last observed foreground drain, retained across SSE reconnects
LastMessageID string
Shells map[string]struct{}
Forms map[string]string
@@ -1056,7 +1119,7 @@ func NormalizeChatEvent(payload []byte, sessionID string, stream *ChatStreamStat
frame.Part = &protocol.ChatPart{Type: protocol.ChatPartReasoning, ID: protocol.ChatDeltaPartID(messageID, protocol.ChatPartReasoning, data.Ordinal)}
case outer.Type == "session.step.started" || outer.Type == "session.step.streamed" || outer.Type == "session.step.ended":
frame.Kind = protocol.ChatEventStep
frame.Step = &protocol.ChatStep{Status: strings.TrimPrefix(outer.Type, "session.step."), Index: stepIndex(data)}
frame.Step = &protocol.ChatStep{Status: strings.TrimPrefix(outer.Type, "session.step."), Index: stepIndex(data), Intermediate: outer.Type == "session.step.ended"}
case strings.HasPrefix(outer.Type, "session.tool."):
status, ok := toolStatus(outer.Type)
if !ok {
+109
View File
@@ -0,0 +1,109 @@
package opencode
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// The active endpoint, not finish/tool-calls or a step boundary, owns the
// foreground drain. Reconnecting after the last event must still settle it.
func TestChatActivityMultiStepAndReconnect(t *testing.T) {
var active atomic.Bool
var invalid atomic.Bool
active.Store(true)
events := make(chan string, 8)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/session/active":
w.Header().Set("Content-Type", "application/json")
if invalid.Load() {
fmt.Fprint(w, `{"data":null}`)
return
}
if active.Load() {
fmt.Fprint(w, `{"data":{"ses_1":{"type":"running"}}}`)
} else {
fmt.Fprint(w, `{"data":{}}`)
}
case "/api/event":
w.Header().Set("Content-Type", "text/event-stream")
w.WriteHeader(200)
w.(http.Flusher).Flush()
for {
select {
case <-r.Context().Done():
return
case event := <-events:
fmt.Fprintf(w, "data: %s\n\n", event)
w.(http.Flusher).Flush()
}
}
default:
http.NotFound(w, r)
}
}))
defer srv.Close()
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
frames := make(chan protocol.ChatEventFrame, 32)
start := func() (context.CancelFunc, <-chan error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
done := make(chan error, 1)
go func() {
done <- chat.Stream(ctx, "ses_1", nil, func(frame protocol.ChatEventFrame) error { frames <- frame; return nil })
}()
return cancel, done
}
next := func() protocol.ChatEventFrame {
t.Helper()
select {
case frame := <-frames:
return frame
case <-time.After(3 * time.Second):
t.Fatal("no activity frame")
return protocol.ChatEventFrame{}
}
}
cancel, done := start()
defer cancel()
// Active readback restores Running even with no SSE replay on reconnect.
if frame := next(); frame.Step == nil || frame.Step.Status != protocol.ChatStepStarted {
t.Fatalf("active = %+v", frame)
}
for i := 0; i < 2; i++ {
events <- `{"type":"session.step.ended","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","finish":"tool-calls"}}`
frame := next()
if frame.Step == nil || !frame.Step.Intermediate {
t.Fatalf("step must be intermediate: %+v", frame)
}
}
select {
case frame := <-frames:
t.Fatalf("premature completion: %+v", frame)
case <-time.After(650 * time.Millisecond):
}
cancel()
<-done
active.Store(false)
invalid.Store(true)
cancel2, done2 := start()
defer cancel2()
select {
case frame := <-frames:
t.Fatalf("invalid readback settled turn: %+v", frame)
case <-time.After(650 * time.Millisecond):
}
invalid.Store(false)
frame := next()
if frame.Step == nil || frame.Step.Status != protocol.ChatStepEnded || frame.Step.Intermediate {
t.Fatalf("idle after reconnect = %+v", frame)
}
cancel2()
<-done2
}
+8 -2
View File
@@ -179,6 +179,10 @@ type ChatModelRequest struct {
// lifted to Directory, CreatedAt and UpdatedAt.
type ChatSession struct {
ID string `json:"id"`
// InstructionStatus is the observed create-time preload outcome: applied,
// unsupported, failed, or not_requested. Ordinary list rows omit it.
InstructionStatus string `json:"instruction_status,omitempty"`
InstructionError string `json:"instruction_error,omitempty"`
// Backend names the harness that owns this session: opencode, codex or
// claude. Empty on older peers and for legacy OpenCode rows.
Backend string `json:"backend,omitempty"`
@@ -238,8 +242,8 @@ type ChatCreateRequest struct {
Backend string `json:"backend,omitempty"`
OperationID string `json:"operation_id,omitempty"`
// Instructions preloads the session's durable behavior instruction, when the
// harness supports session instruction entries (OpenCode does). Best effort:
// a harness without the surface still creates the chat.
// harness supports session instruction entries (OpenCode does). The reply's
// instruction_status explicitly reports applied, unsupported or failed.
Instructions string `json:"instructions,omitempty"`
// Ephemeral creates a chat for automated verification: the session is created,
// bootstrapped and usable exactly like a normal one, but no operator-registry
@@ -435,6 +439,8 @@ type ChatEventFrame struct {
type ChatStep struct {
Status string `json:"status"`
Index int `json:"index,omitempty"`
// Intermediate distinguishes a provider step from authoritative turn completion.
Intermediate bool `json:"intermediate,omitempty"`
}
// ChatUsage is the token and cost total of a turn.
+98 -25
View File
@@ -3,16 +3,41 @@ package server
import (
"encoding/base64"
"fmt"
"io/fs"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"time"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// Files survive process restarts and asynchronous harness inbox admission.
// Only unleased files older than seven days are collected, on the next upload.
const chatAttachmentRetention = 7 * 24 * time.Hour
const chatAttachmentQuota = 64 << 20
const chatAttachmentFileQuota = 512
var chatAttachmentStore = struct {
sync.Mutex
leased map[string]bool
}{leased: make(map[string]bool)}
func releaseChatAttachments(files []opencode.PromptFile) {
chatAttachmentStore.Lock()
defer chatAttachmentStore.Unlock()
for _, file := range files {
u, err := url.Parse(file.URI)
if err != nil || !chatAttachmentStore.leased[u.Path] {
continue
}
delete(chatAttachmentStore.leased, u.Path)
}
}
const (
// maxChatAttachments bounds one prompt's browser files. OpenCode receives a
// small number of local files; more is not a useful chat message.
@@ -24,7 +49,7 @@ const (
)
// prepareChatAttachments writes browser attachment bytes into a private
// temporary directory and returns the file references OpenCode accepts plus the
// durable directory and returns the file references OpenCode accepts plus the
// display parts the user turn carries. A browser File reference is not readable
// by the agent service, so the bytes must cross the websocket.
func prepareChatAttachments(sessionID string, attachments []protocol.ChatAttachment) ([]opencode.PromptFile, []protocol.ChatPart, error) {
@@ -34,21 +59,74 @@ func prepareChatAttachments(sessionID string, attachments []protocol.ChatAttachm
if len(attachments) > maxChatAttachments {
return nil, nil, fmt.Errorf("at most %d attachments are allowed", maxChatAttachments)
}
dir := filepath.Join(os.TempDir(), "pipelines-chat-attachments", safeChatAttachmentSegment(sessionID))
if err := os.MkdirAll(dir, 0o700); err != nil {
dir := os.Getenv("PIPELINES_CHAT_ATTACHMENT_DIR")
if dir == "" {
home, err := os.UserHomeDir()
if err != nil {
return nil, nil, err
}
dir = filepath.Join(home, ".pipelines-chat-attachments")
}
if !filepath.IsAbs(dir) {
return nil, nil, fmt.Errorf("chat attachment storage must be an absolute path")
}
dir = filepath.Clean(dir)
chatAttachmentStore.Lock()
defer chatAttachmentStore.Unlock()
if err := os.Mkdir(dir, 0o700); err != nil && !os.IsExist(err) {
return nil, nil, fmt.Errorf("prepare attachment directory: %w", err)
}
info, err := os.Lstat(dir)
if err != nil || !info.IsDir() || info.Mode().Perm()&0o077 != 0 {
return nil, nil, fmt.Errorf("attachment storage must be a private directory, not a symlink")
}
root, err := os.OpenRoot(dir)
if err != nil {
return nil, nil, err
}
defer root.Close()
actual, err := root.Stat(".")
if err != nil || !os.SameFile(info, actual) {
return nil, nil, fmt.Errorf("chat attachment storage changed while opening")
}
entries, err := fs.ReadDir(root.FS(), ".")
if err != nil {
return nil, nil, err
}
var stored int64
var storedFiles int
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
return nil, nil, err
}
if !info.Mode().IsRegular() {
continue
}
if !chatAttachmentStore.leased[filepath.Join(dir, entry.Name())] && time.Since(info.ModTime()) > chatAttachmentRetention {
if err := root.Remove(entry.Name()); err == nil {
continue
}
}
stored += info.Size()
storedFiles++
}
var files []opencode.PromptFile
var parts []protocol.ChatPart
var created []string
cleanup := func() {
for _, path := range created {
_ = os.Remove(path)
_ = root.Remove(filepath.Base(path))
delete(chatAttachmentStore.leased, path)
}
}
total := 0
for index, attachment := range attachments {
data := strings.TrimSpace(attachment.Data)
if len(data) > base64.StdEncoding.EncodedLen(maxChatAttachmentBytes) {
cleanup()
return nil, nil, fmt.Errorf("attachments exceed the 1 MiB limit")
}
if data == "" {
cleanup()
return nil, nil, fmt.Errorf("attachment %q has no data", attachment.Name)
@@ -63,27 +141,41 @@ func prepareChatAttachments(sessionID string, attachments []protocol.ChatAttachm
cleanup()
return nil, nil, fmt.Errorf("attachments exceed the %s limit", "1 MiB")
}
if stored+int64(total) > chatAttachmentQuota {
cleanup()
return nil, nil, fmt.Errorf("chat attachment storage quota exceeded (64 MiB); retry after retention cleanup")
}
if storedFiles+index+1 > chatAttachmentFileQuota {
cleanup()
return nil, nil, fmt.Errorf("chat attachment storage quota exceeded (512 files)")
}
name := safeChatAttachmentName(attachment.Name)
mime := strings.TrimSpace(attachment.Mime)
if mime == "" {
mime = "application/octet-stream"
}
path := filepath.Join(dir, fmt.Sprintf("%d-%02d-%s", time.Now().UnixNano(), index, name))
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
file, err := root.OpenFile(filepath.Base(path), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
if err != nil {
cleanup()
return nil, nil, fmt.Errorf("write attachment %q: %w", name, err)
}
created = append(created, path)
chatAttachmentStore.leased[path] = true
if _, err := file.Write(decoded); err != nil {
_ = file.Close()
cleanup()
return nil, nil, fmt.Errorf("write attachment %q: %w", name, err)
}
if err := file.Sync(); err != nil {
_ = file.Close()
cleanup()
return nil, nil, fmt.Errorf("sync attachment %q: %w", name, err)
}
if err := file.Close(); err != nil {
cleanup()
return nil, nil, fmt.Errorf("close attachment %q: %w", name, err)
}
created = append(created, path)
uri := (&url.URL{Scheme: "file", Path: path}).String()
files = append(files, opencode.PromptFile{URI: uri, Name: name})
parts = append(parts, protocol.ChatPart{Type: protocol.ChatPartFile, Name: name, Mime: mime, URI: uri})
@@ -91,25 +183,6 @@ func prepareChatAttachments(sessionID string, attachments []protocol.ChatAttachm
return files, parts, nil
}
func safeChatAttachmentSegment(value string) string {
value = strings.TrimSpace(value)
value = strings.Map(func(r rune) rune {
switch {
case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9', r == '_', r == '-':
return r
default:
return '_'
}
}, value)
if value == "" {
return "session"
}
if len(value) > 80 {
value = value[:80]
}
return value
}
func safeChatAttachmentName(value string) string {
name := filepath.Base(strings.TrimSpace(value))
name = strings.Map(func(r rune) rune {
+62
View File
@@ -6,11 +6,13 @@ import (
"path/filepath"
"strings"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func TestPrepareChatAttachmentsWritesPrivateFileAndPart(t *testing.T) {
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", filepath.Join(t.TempDir(), "uploads"))
data := base64.StdEncoding.EncodeToString([]byte("hello attachment"))
files, parts, err := prepareChatAttachments("ses_test", []protocol.ChatAttachment{{
Name: "../../note.txt", Mime: "text/plain", Data: data,
@@ -44,7 +46,67 @@ func TestPrepareChatAttachmentsWritesPrivateFileAndPart(t *testing.T) {
}
}
func TestChatAttachmentStorageRetentionLeaseAndSymlink(t *testing.T) {
dir := filepath.Join(t.TempDir(), "uploads")
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", dir)
attachment := []protocol.ChatAttachment{{Name: "x", Data: base64.StdEncoding.EncodeToString([]byte("private"))}}
files, _, err := prepareChatAttachments("ses_test", attachment)
if err != nil {
t.Fatal(err)
}
path := strings.TrimPrefix(files[0].URI, "file://")
old := time.Now().Add(-chatAttachmentRetention - time.Hour)
if err := os.Chtimes(path, old, old); err != nil {
t.Fatal(err)
}
more, _, err := prepareChatAttachments("ses_test", attachment)
if err != nil {
t.Fatal(err)
}
defer releaseChatAttachments(more)
if _, err := os.Stat(path); err != nil {
t.Fatal("queued/active file collected", err)
}
releaseChatAttachments(files)
last, _, err := prepareChatAttachments("ses_test", attachment)
if err != nil {
t.Fatal(err)
}
defer releaseChatAttachments(last)
if _, err := os.Stat(path); !os.IsNotExist(err) {
t.Fatal("expired unleased file not collected", err)
}
link := filepath.Join(t.TempDir(), "link")
if err := os.Symlink(dir, link); err != nil {
t.Fatal(err)
}
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", link)
if _, _, err := prepareChatAttachments("ses_test", attachment); err == nil {
t.Fatal("symlink storage accepted")
}
}
func TestChatAttachmentStorageQuota(t *testing.T) {
dir := t.TempDir()
if err := os.Chmod(dir, 0o700); err != nil {
t.Fatal(err)
}
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", dir)
file, err := os.Create(filepath.Join(dir, "quota"))
if err != nil {
t.Fatal(err)
}
if err := file.Truncate(chatAttachmentQuota); err != nil {
t.Fatal(err)
}
file.Close()
if _, _, err := prepareChatAttachments("ses_test", []protocol.ChatAttachment{{Name: "x", Data: "eA=="}}); err == nil || !strings.Contains(err.Error(), "quota") {
t.Fatalf("quota error = %v", err)
}
}
func TestPrepareChatAttachmentsRejectsInvalidAndOversized(t *testing.T) {
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", filepath.Join(t.TempDir(), "uploads"))
if _, _, err := prepareChatAttachments("ses_test", []protocol.ChatAttachment{{Name: "x", Data: "not base64"}}); err == nil {
t.Fatal("invalid base64 accepted")
}
+60
View File
@@ -0,0 +1,60 @@
package server
import (
"context"
"errors"
"os"
"path/filepath"
"testing"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func TestChatRejectsUnsupportedAttachmentsBeforeStorageOrQueue(t *testing.T) {
broker := newChannelChat() // deliberately has no file capability
manager := &chatManager{opencode: broker, cache: map[string]string{}}
dir := filepath.Join(t.TempDir(), "uploads")
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", dir)
var reply protocol.Message
handleChatPrompt(context.Background(), protocol.Message{ID: "p", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "hello", Attachments: []protocol.ChatAttachment{{Name: "x", Data: "eA=="}}}}, manager, newChatHub(), nil, "client", func(msg protocol.Message) error { reply = msg; return nil })
if reply.Type != protocol.Error || reply.Error != errChatAttachmentsUnsupported.Error() {
t.Fatalf("reply = %+v", reply)
}
if broker.promptCount() != 0 {
t.Fatal("unsupported prompt reached harness")
}
if _, err := os.Stat(dir); !os.IsNotExist(err) {
t.Fatal("unsupported upload wrote storage", err)
}
if err := manager.PromptWithMessageIDFiles(context.Background(), "ses_1", "hello", "", []opencode.PromptFile{{URI: "file:///x"}}); !errors.Is(err, errChatAttachmentsUnsupported) {
t.Fatalf("silent fallback: %v", err)
}
}
type failingChatInstructions struct{ *fakeChat }
func (f failingChatInstructions) SetInstruction(context.Context, string, string, any) error {
return errors.New("instruction service unavailable")
}
func TestChatCreateReportsInstructionCapabilities(t *testing.T) {
for _, status := range []string{"applied", "failed", "unsupported"} {
t.Run(status, func(t *testing.T) {
fake := scriptedChat()
fake.created = protocol.ChatSession{ID: "ses_new"}
var broker chatBroker = fake
if status == "failed" {
broker = failingChatInstructions{fake}
}
if status == "unsupported" {
broker = newChannelChat()
}
var reply protocol.Message
handleChatCreate(context.Background(), protocol.Message{ID: "create", ChatCreate: &protocol.ChatCreateRequest{Directory: t.TempDir(), Instructions: "be careful"}}, broker, newOperatorStore(t), func(msg protocol.Message) error { reply = msg; return nil })
if reply.ChatSession == nil || reply.ChatSession.InstructionStatus != status {
t.Fatalf("reply = %+v", reply)
}
})
}
}
+20 -4
View File
@@ -3,11 +3,11 @@ package server
import (
"context"
"errors"
"git.a-wan.net/alan/pipelines/internal/opencode"
"sort"
"sync"
"git.a-wan.net/alan/pipelines/internal/codex"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
@@ -138,8 +138,7 @@ func (m *chatManager) PromptWithMessageID(ctx context.Context, sessionID, text,
}
// PromptWithMessageIDFiles forwards a prompt with the private local files the
// server wrote for browser attachments. A backend without file support falls
// back to the text-only path rather than dropping the prompt.
// server wrote for browser attachments. Never silently discard files.
func (m *chatManager) PromptWithMessageIDFiles(ctx context.Context, sessionID, text, messageID string, files []opencode.PromptFile) error {
adapter, err := m.brokerFor(ctx, sessionID)
if err != nil {
@@ -148,9 +147,26 @@ func (m *chatManager) PromptWithMessageIDFiles(ctx context.Context, sessionID, t
if withFiles, ok := adapter.(chatPromptWithFiles); ok {
return withFiles.PromptWithMessageIDFiles(ctx, sessionID, text, messageID, files)
}
if len(files) > 0 {
return errChatAttachmentsUnsupported
}
return m.PromptWithMessageID(ctx, sessionID, text, messageID)
}
var errChatAttachmentsUnsupported = errors.New("this chat harness does not support attachments")
var errChatInstructionsUnsupported = errors.New("this chat harness does not support session instructions")
func (m *chatManager) CheckAttachments(ctx context.Context, sessionID string) error {
adapter, err := m.brokerFor(ctx, sessionID)
if err != nil {
return err
}
if _, ok := adapter.(chatPromptWithFiles); !ok {
return errChatAttachmentsUnsupported
}
return nil
}
// SetInstruction forwards one preloaded behavior instruction to the session's
// harness when it supports durable instruction entries.
func (m *chatManager) SetInstruction(ctx context.Context, sessionID, key string, value any) error {
@@ -160,7 +176,7 @@ func (m *chatManager) SetInstruction(ctx context.Context, sessionID, key string,
}
instr, ok := adapter.(chatInstructioner)
if !ok {
return errors.New("this chat harness does not support session instructions")
return errChatInstructionsUnsupported
}
return instr.SetInstruction(ctx, sessionID, key, value)
}
+76 -1
View File
@@ -6,6 +6,7 @@ import (
"net/http/httptest"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
@@ -16,6 +17,71 @@ import (
"git.a-wan.net/alan/pipelines/internal/protocol"
)
type reconnectingChat struct {
*channelChat
streams atomic.Int32
disconnect chan struct{}
reconnected chan struct{}
}
func (c *reconnectingChat) Stream(ctx context.Context, id string, ready func(), emit func(protocol.ChatEventFrame) error) error {
n := c.streams.Add(1)
if ready != nil {
ready()
}
if n == 1 {
select {
case <-c.disconnect:
return errors.New("test transport disconnect")
case <-ctx.Done():
return ctx.Err()
}
}
if n == 2 {
close(c.reconnected)
}
for {
select {
case <-ctx.Done():
return ctx.Err()
case frame := <-c.frames:
if err := emit(frame); err != nil {
return err
}
}
}
}
func TestChatStreamDisconnectPreservesQueuedTurn(t *testing.T) {
broker := &reconnectingChat{channelChat: newChannelChat(), disconnect: make(chan struct{}), reconnected: make(chan struct{})}
ctx, _, _, conns := dialChatServer(t, Options{Chat: broker}, 1)
conn := conns[0]
for _, text := range []string{"one", "two"} {
writeChat(t, ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: text, ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: text}})
ack := readUntil(t, ctx, conn, protocol.ChatPrompt)
if ack.ChatQueued != (text == "two") {
t.Fatalf("ack = %+v", ack)
}
}
close(broker.disconnect)
select {
case <-broker.reconnected:
case <-ctx.Done():
t.Fatal("stream did not reconnect")
}
if broker.promptCount() != 1 {
t.Fatal("transport loss advanced queue")
}
broker.push(protocol.ChatEventFrame{SessionID: "ses_1", Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded}})
deadline := time.Now().Add(time.Second)
for broker.promptCount() < 2 && time.Now().Before(deadline) {
time.Sleep(5 * time.Millisecond)
}
if broker.promptCount() != 2 {
t.Fatal("authoritative completion did not advance queue")
}
}
// channelChat is a broker whose stream is driven frame-by-frame from a channel,
// so a test can hold a turn in flight and prove prompt serialisation, fan-out and
// exactly-once approval behaviour without a real OpenCode service.
@@ -286,7 +352,16 @@ func TestChatPromptSerialisedPerSession(t *testing.T) {
t.Fatalf("prompts forwarded = %d, want 1 while the first turn is in flight", broker.promptCount())
}
// End the first turn; the queued prompt is then posted.
// Provider/tool steps are not foreground-drain completion. Neither step
// boundary may start the queued prompt, including after a subscriber attach.
for i := 0; i < 2; i++ {
broker.push(protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_1", Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded, Index: i + 1, Intermediate: true}})
time.Sleep(30 * time.Millisecond)
if broker.promptCount() != 1 {
t.Fatalf("intermediate step advanced queue: %v", broker.promptTexts())
}
}
// End the first turn authoritatively; the queued prompt is then posted.
broker.push(protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_1", Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded, Index: 1}})
deadline := time.Now().Add(3 * time.Second)
for broker.promptCount() < 2 {
+24 -6
View File
@@ -358,8 +358,8 @@ func (h *chatHub) broadcastUser(frame protocol.ChatEventFrame, exceptClientID st
// emit folds one broker frame into the retained state and fans it out. While the
// first turn's ack is still pending the frames are buffered and flushed in order
// once it is written. A step-ended frame finishes the turn, letting the queue
// advance.
// once it is written. Only an authoritative (non-intermediate) ended frame
// finishes the turn and lets the queue advance.
func (h *chatHub) emit(frame protocol.ChatEventFrame) {
h.emitRoom(nil, frame)
}
@@ -384,7 +384,7 @@ func (h *chatHub) emitRoom(source *chatRoom, frame protocol.ChatEventFrame) {
}
}
}
if room != nil && frame.Kind == protocol.ChatEventStep && frame.Step != nil && frame.Step.Status == protocol.ChatStepEnded {
if room != nil && frame.Kind == protocol.ChatEventStep && frame.Step != nil && frame.Step.Status == protocol.ChatStepEnded && !frame.Step.Intermediate {
h.finishTurnLocked(room)
}
// Shell replay and live lifecycle delivery must enqueue in the same order
@@ -630,6 +630,13 @@ func (h *chatHub) pushPermission(sessionID string, frame protocol.ChatPermission
// posts each to the broker and waits for the turn to end, so two prompts of one
// session can never interleave.
func (h *chatHub) runRoom(room *chatRoom) {
defer func() {
h.mu.Lock()
defer h.mu.Unlock()
for _, p := range room.queue {
releaseChatAttachments(p.files)
}
}()
for {
p, done, ok := h.nextPrompt(room)
if !ok {
@@ -666,6 +673,7 @@ func (h *chatHub) nextPrompt(room *chatRoom) (*chatQueuedPrompt, chan struct{},
// runTurn subscribes to the broker stream when needed, posts one prompt, writes
// its ack and waits for the turn to end.
func (h *chatHub) runTurn(room *chatRoom, p *chatQueuedPrompt, done chan struct{}) {
defer releaseChatAttachments(p.files)
broker := h.roomBroker(room)
first := false
h.mu.Lock()
@@ -785,17 +793,27 @@ func (h *chatHub) startStream(room *chatRoom, broker chatBroker) error {
}
// drainStream reports the terminal error of a room's subscription and clears the
// streaming flag so a later turn re-subscribes; it also releases a turn still
// waiting on a stream that ended.
// streaming flag and reconnects. Transport loss never releases a turn slot.
func (h *chatHub) drainStream(room *chatRoom, result <-chan error) {
err := <-result
h.mu.Lock()
room.streaming = false
h.finishTurnLocked(room)
h.mu.Unlock()
if err != nil && room.ctx.Err() == nil {
log.Printf("chat stream session=%s: %v", room.sessionID, err)
}
// Losing the transport does not end the harness turn. Re-subscribe without
// releasing its queue slot; the harness must report authoritative completion.
for room.ctx.Err() == nil {
select {
case <-room.ctx.Done():
return
case <-time.After(time.Second):
}
if err := h.ensureObserving(room, h.roomBroker(room)); err == nil {
return
}
}
}
// pollRoom polls the broker's pending-approval list for the room on a fixed
+18 -11
View File
@@ -3,7 +3,6 @@ package server
import (
"context"
"errors"
"log"
"os"
"path/filepath"
"strings"
@@ -180,16 +179,6 @@ func handleChatCreate(ctx context.Context, request protocol.Message, broker chat
// Reserve the activation before calling the external service. If the
// process dies after the call but before the success checkpoint, a retry
// must report the ambiguous operation rather than append the skill twice.
// Preload the web chat's behavior brief before the first turn. This is
// best effort: an older OpenCode build or another harness without the
// experimental instruction surface still gets a usable chat.
if instructions != "" {
if instr, ok := broker.(chatInstructioner); ok {
if err := instr.SetInstruction(ctx, session.ID, "pipelines.behavior", instructions); err != nil {
log.Printf("chat %s preload instructions: %v", session.ID, err)
}
}
}
if err := registry.AdvanceChatCreate(ctx, operationID, "created", "activating"); err != nil {
chatCreateConflictError(request, err, write)
return
@@ -239,6 +228,24 @@ func handleChatCreate(ctx context.Context, request protocol.Message, broker chat
session.Directory = resolved
session.Tag = protocol.ChatSessionTagOperator
session.Backend = backend
// Setting a named instruction replaces it idempotently. Repeat on a retried
// create so the reply reports an observed result, never a guessed success.
session.InstructionStatus = "not_requested"
if instructions != "" {
session.InstructionStatus = "unsupported"
if instr, ok := broker.(chatInstructioner); ok {
err := instr.SetInstruction(ctx, session.ID, "pipelines.behavior", instructions)
switch {
case err == nil:
session.InstructionStatus = "applied"
case errors.Is(err, errChatInstructionsUnsupported):
session.InstructionError = err.Error()
default:
session.InstructionStatus = "failed"
session.InstructionError = err.Error()
}
}
}
response := protocol.Message{Type: protocol.ChatCreate, ID: request.ID, ChatSession: &session}
// The same registry row is echoed on the list field so a client can splice it
// into chat_sessions without a re-list. An ephemeral create has no listed row
+21
View File
@@ -45,11 +45,31 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat prompt exceeds 64 KiB"})
return
}
if len(prompt.Attachments) > 0 {
var err error
if checker, ok := broker.(interface {
CheckAttachments(context.Context, string) error
}); ok {
err = checker.CheckAttachments(ctx, prompt.SessionID)
} else if _, ok := broker.(chatPromptWithFiles); !ok {
err = errChatAttachmentsUnsupported
}
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
}
attachmentFiles, attachmentParts, err := prepareChatAttachments(prompt.SessionID, prompt.Attachments)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
accepted := false
defer func() {
if !accepted {
releaseChatAttachments(attachmentFiles)
}
}()
sessionID := prompt.SessionID
sub := hub.attach(sessionID, clientID, sanitizeChatClientLabel(prompt.Client), broker, write)
if sub == nil {
@@ -85,6 +105,7 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " already has the maximum number of prompts queued"})
return
}
accepted = true
// Publish the user turn to every other client; the sender already has it.
hub.broadcastUser(userFrame, clientID)
if queued {
+1
View File
@@ -473,6 +473,7 @@ func TestChatModelsAndSwitch(t *testing.T) {
}
func TestChatPromptWritesAttachmentAndForwardsLocalFile(t *testing.T) {
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", filepath.Join(t.TempDir(), "uploads"))
broker := scriptedChat()
ctx, _, conn := dialChat(t, broker)
data := base64.StdEncoding.EncodeToString([]byte("hello attachment"))
+19
View File
@@ -8,6 +8,25 @@ test, or a code change without a capture, is `partial` at best.
Status legend: `todo` · `partial` (some of it works) · `done` (working + captured).
### Chat audit follow-up (2026-10-20, partial — fresh UI captures pending)
The following supersedes older C8/B18 implementation details below:
- Web attachments are limited to **1 MiB total and eight files**, matching the
server. Unsupported harnesses reject attachments before upload storage/queueing.
- Uploads live in a private durable `~/.pipelines-chat-attachments` directory
(override: `PIPELINES_CHAT_ATTACHMENT_DIR`, must already have a parent and be
private). Storage is capped at 64 MiB/512 files; the next upload collects unleased files
older than seven days. Queued/in-flight uploads are leased, never collected.
Restart retains files, but not in-memory leases or the existing prompt queue.
- OpenCode provider-step ends are intermediate progress; foreground completion
is read from V2 `/api/session/active`. Failed readback does not advance a queue.
- Create replies report `instruction_status` as `applied`, `unsupported`, `failed`
or `not_requested`; the UI shows applied/unsupported/failed as chat notices.
- The model picker uses the shared modal manager for focus trapping and Escape.
Unit/HTTP-fixture regressions cover these changes. No live workflow or deployed
service was used; new rendered-state captures are **not yet available**.
## Transport and connection
| # | Feature (Android counterpart) | Protocol | Status |
| --- | --- | --- | --- |
+3 -2
View File
@@ -1612,6 +1612,7 @@ export function applyChatEvent(thread, frame, now = Date.now()) {
case PART_TOOL:
return applyTool(current, frame, now);
case EVENT_STEP:
if (frame?.step?.intermediate) return current;
return setThreadRunning(current, str(frame?.step?.status) !== STEP_ENDED, now);
// A shell frame only folds its shell into the retained set: it NEVER marks
// the thread running, because a background process is not the turn — a shell
@@ -1948,7 +1949,7 @@ export function queuedAfterFrame(queued, frame) {
* running bit, which the composer's Stop control reads.
*/
export function runningAfterFrame(running, frame) {
if (frame?.kind === "step" && frame?.step?.status === "ended") return false;
if (frame?.kind === "step" && frame?.step?.status === "ended" && !frame?.step?.intermediate) return false;
return running === true;
}
@@ -1961,7 +1962,7 @@ export function runningAfterFrame(running, frame) {
* A browser-side guard, so a runaway pick is refused with a reason instead of
* being handed to the server; the server applies its own limits on top.
*/
export const MAX_ATTACHMENT_BYTES = 25 * 1024 * 1024;
export const MAX_ATTACHMENT_BYTES = 1024 * 1024;
/** The fallback display name when neither the name nor the uri gives one. */
export const ATTACHMENT_FALLBACK_NAME = "file";
+15
View File
@@ -555,6 +555,14 @@ function cancelNewChat() {
* of the list, becomes the open conversation and loads its (empty) history; on
* failure the server's own reason stays in the pane, which does not close.
*/
function reportInstructionStatus(created) {
const status = created?.instruction_status;
if (status === "applied") addChatNotice(created.id, "Session instructions applied.");
else if (status === "unsupported" || status === "failed") {
addChatNotice(created.id, `Session instructions ${status}: ${created.instruction_error || "this harness does not support preloaded instructions"}.`);
}
}
async function createNewChat() {
const create = state.chatCreate;
if (!create || create.creating) return;
@@ -570,6 +578,7 @@ async function createNewChat() {
state.chatCreate = null;
state.chatSessions = chatCreateSucceeded(state.chatSessions, created);
await openChatSession(created.id);
reportInstructionStatus(created);
await loadChatSessions();
} catch (error) {
if (!state.chatCreate) return;
@@ -1559,6 +1568,7 @@ async function startChatSession(directory) {
});
const title = String(created.title ?? "").trim() || "New chat";
addChatNotice(created.id, `Started a new session “${title}” in ${directory}.`);
reportInstructionStatus(created);
await openChatSession(created.id);
// An ephemeral (verification) create is intentionally absent from the server's
// list, so a refresh here would replace the just-opened conversation and drop
@@ -1717,6 +1727,7 @@ function base64FromBuffer(buffer) {
async function chatAttachmentPayloads(attachments) {
const valid = (attachments ?? []).filter((attachment) => attachment?.error == null);
if (valid.length === 0) return [];
if (valid.length > 8) throw new Error("At most 8 attachments are allowed.");
let total = 0;
const payloads = [];
for (const attachment of valid) {
@@ -2736,6 +2747,10 @@ function renderChatModelDialog(session) {
);
panel.append(actions);
overlay.append(panel);
modal.stage("chat-model", overlay, {
onClose: closeChatModelPicker,
fallbackSelectors: ["#chatComposer", "#detailTitle", "#pane-main"],
});
return overlay;
}
+16 -3
View File
@@ -888,14 +888,27 @@ test("attachmentDisplayName prefers the name, then the uri's basename, then file
assert.equal(attachmentDisplayName("", ""), "file");
});
test("attachmentError reports an unreadable file and the 25 MiB limit by name", () => {
test("intermediate tool steps preserve Stop and the running turn", () => {
let thread = applyChatEvent(newChatThread("ses_1"), { kind: "step", step: { status: "started" } }, 100);
for (let index = 0; index < 3; index++) {
const frame = { kind: "step", step: { status: "ended", intermediate: true, index } };
thread = applyChatEvent(thread, frame, 200 + index);
assert.equal(thread.running, true);
assert.equal(runningAfterFrame(true, frame), true);
assert.equal(composerAction(thread), "stop");
}
thread = applyChatEvent(thread, { kind: "step", step: { status: "ended" } }, 300);
assert.equal(thread.running, false);
});
test("attachmentError reports an unreadable file and the server's 1 MiB limit by name", () => {
assert.equal(attachmentError("a.txt", 3, true), null);
assert.equal(attachmentError("a.txt", 3, false), '"a.txt" could not be read.');
assert.equal(attachmentError("", 3, false), '"file" could not be read.');
assert.equal(MAX_ATTACHMENT_BYTES, 25 * 1024 * 1024);
assert.equal(MAX_ATTACHMENT_BYTES, 1024 * 1024);
assert.equal(
attachmentError("big.bin", MAX_ATTACHMENT_BYTES + 1, true),
'"big.bin" is too large (25.0 MiB limit).',
'"big.bin" is too large (1.0 MiB limit).',
);
// Exactly at the limit is allowed; the limit reads through the shared humanBytes.
assert.equal(attachmentError("big.bin", MAX_ATTACHMENT_BYTES, true), null);