fix(codex): accept prepared chat attachments with model overrides

This commit is contained in:
2026-10-20 18:06:11 +01:00
parent 83fef10331
commit 68761652cf
8 changed files with 383 additions and 16 deletions
+36 -1
View File
@@ -5,6 +5,8 @@ import (
"path/filepath"
"testing"
"git.a-wan.net/alan/pipelines/internal/codex"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
@@ -13,6 +15,19 @@ type selectedModelChat struct {
*fakeChat
model, nativeID string
overrides int
files []opencode.PromptFile
}
func TestCodexManagerAttachmentsRejectUnpreparedURI(t *testing.T) {
// Invalid input must fail before any JSON-RPC/socket access, including when
// no model override is saved.
manager := &chatManager{codex: &codex.Chat{}, cache: map[string]string{"thread": "codex"}}
if err := manager.CheckAttachments(context.Background(), "thread"); err != nil {
t.Fatal(err)
}
if err := manager.PromptWithMessageIDFiles(context.Background(), "thread", "inspect", "msg", []opencode.PromptFile{{URI: "file:///etc/passwd"}}); err == nil {
t.Fatal("unprepared URI accepted")
}
}
func (c *selectedModelChat) PromptWithMessageIDModel(_ context.Context, _, _, nativeID, model string) error {
@@ -21,7 +36,13 @@ func (c *selectedModelChat) PromptWithMessageIDModel(_ context.Context, _, _, na
return nil
}
func (c *selectedModelChat) PromptWithMessageIDModelFiles(ctx context.Context, id, text, nativeID, model string, files []opencode.PromptFile) error {
c.files = append([]opencode.PromptFile(nil), files...)
return c.PromptWithMessageIDModel(ctx, id, text, nativeID, model)
}
func TestCodexModelSelectionSurvivesServerRestart(t *testing.T) {
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", filepath.Join(t.TempDir(), "attachments"))
ctx := context.Background()
path := filepath.Join(t.TempDir(), "chat.db")
store, err := pipeline.Open(path)
@@ -85,11 +106,25 @@ func TestCodexModelSelectionSurvivesServerRestart(t *testing.T) {
if fake.overrides != 3 || fake.nativeID != "msg_empty" {
t.Fatal("empty attachments bypassed saved choice")
}
files, _, err := prepareChatAttachments(id, []protocol.ChatAttachment{{Name: "report.txt", Data: "aGVsbG8="}, {Name: "image.png", Mime: "image/png", Data: "aW1hZ2UgYnl0ZXM="}})
if err != nil {
t.Fatal(err)
}
defer releaseChatAttachments(files)
if err := manager.PromptWithMessageIDFiles(ctx, id, "with files", "msg_files", files); err != nil {
t.Fatal(err)
}
if fake.overrides != 4 || fake.model != "fixture-model" || fake.nativeID != "msg_files" || len(fake.files) != 2 || !fake.files[0].ServerPrepared() {
t.Fatalf("attachment model override lost: %+v", fake)
}
fake.sessionRecord.ModelProvider = "different-provider"
if err := manager.Prompt(ctx, id, "must not send"); err == nil {
t.Fatal("saved model sent to a different provider")
}
if fake.overrides != 3 {
if err := manager.PromptWithMessageIDFiles(ctx, id, "must not send files", "msg_bad", files); err == nil {
t.Fatal("saved model with files sent to a different provider")
}
if fake.overrides != 4 {
t.Fatal("mismatched provider started a turn")
}
// A failed durable save must never acknowledge the next-turn selection.