Skip to content

Commit 97536ef

Browse files
committed
fix(chat): skip the 1s MCP catalog wait on OpenCode 2.2.6+ (PROJ-14)
OpenCode 2.2.6 ships the fix for anomalyco/opencode#51812 (tools reaching the model after a server reports connected), so the fixed 1s wait before the first prompt is dead time there. Patio now parses the full OpenCode version and keeps the wait only for older installs.
1 parent 6ec132a commit 97536ef

6 files changed

Lines changed: 84 additions & 47 deletions

File tree

‎internal/api/chat_driver_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1729,7 +1729,7 @@ func TestOpenCodePicksUpMCPChangesOnNextSend(t *testing.T) {
17291729
var configs []map[string]json.RawMessage
17301730
server := New(database, "local", false, "dev", root, Options{
17311731
DBPath: filepath.Join(root, "test.db"), findOpencode: func() (string, error) { return "opencode-test", nil },
1732-
opencodeMajor: func(context.Context, string, []string) (int, error) { return 1, nil },
1732+
opencodeVersion: func(context.Context, string, []string) ([3]int, error) { return [3]int{1}, nil },
17331733
opencodeStarter: func(path string, args, env []string) (opencodeProcess, error) {
17341734
for i, item := range env {
17351735
if strings.HasPrefix(item, "OPENCODE_CONFIG_CONTENT=") {

‎internal/api/chat_opencode.go‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type opencodeServer struct {
5050
registryKey string
5151
inputKey string
5252
major int
53+
catalogLags bool
5354
baseURL string
5455
password string
5556
client *http.Client
@@ -185,14 +186,15 @@ func acquireOpenCodeServer(m *chatManager, connectorID, configDir string, extern
185186
return server, nil
186187
}
187188
}
188-
majorDetector := m.options.opencodeMajor
189-
if majorDetector == nil {
190-
majorDetector = connectorcli.OpencodeMajor
189+
versionDetector := m.options.opencodeVersion
190+
if versionDetector == nil {
191+
versionDetector = connectorcli.OpencodeVersion
191192
}
192-
major, err := majorDetector(context.Background(), path, env)
193+
version, err := versionDetector(context.Background(), path, env)
193194
if err != nil {
194195
return nil, fmt.Errorf("detect opencode version: %w", err)
195196
}
197+
major := version[0]
196198
listener, err := net.Listen("tcp", "127.0.0.1:0")
197199
if err != nil {
198200
return nil, err
@@ -241,7 +243,7 @@ func acquireOpenCodeServer(m *chatManager, connectorID, configDir string, extern
241243
}
242244
ctx, cancel := context.WithCancel(context.Background())
243245
baseURL := "http://127.0.0.1:" + strconv.Itoa(port)
244-
server := &opencodeServer{connectorID: connectorID, registryKey: registryKey, inputKey: inputKey, major: major, baseURL: baseURL, password: password, client: &http.Client{Timeout: 0}, process: process,
246+
server := &opencodeServer{connectorID: connectorID, registryKey: registryKey, inputKey: inputKey, major: major, catalogLags: opencodeCatalogLags(version), baseURL: baseURL, password: password, client: &http.Client{Timeout: 0}, process: process,
245247
ctx: ctx, cancel: cancel, done: make(chan struct{}), chats: make(map[string]*opencodeRuntime), ancestors: make(map[string]string), refs: 1}
246248
if err := server.ready(); err != nil {
247249
cancel()
@@ -594,6 +596,17 @@ func (d *opencodeRuntime) triggerMCPReconnect() {
594596
}()
595597
}
596598

599+
// opencodeCatalogLags reports OpenCode versions before 2.2.6, which report an MCP server
600+
// connected before its tools reach the model (anomalyco/opencode#51812).
601+
func opencodeCatalogLags(version [3]int) bool {
602+
for i, fixed := range [3]int{2, 2, 6} {
603+
if version[i] != fixed {
604+
return version[i] < fixed
605+
}
606+
}
607+
return false
608+
}
609+
597610
// awaitMCPSettled polls until no configured server is missing or pending, or budget passes.
598611
func (d *opencodeRuntime) awaitMCPSettled(started time.Time) map[string]opencodeMCPState {
599612
ctx, cancel := context.WithTimeout(d.ctx, opencodeMCPSettleBudget)
@@ -618,7 +631,7 @@ func (d *opencodeRuntime) awaitMCPSettled(started time.Time) map[string]opencode
618631
firstPoll = false
619632
if settled {
620633
// burnedout: assumed 1s; OpenCode adds tools to the catalog shortly after reporting connected
621-
if needsCatalogSettle && !sleepCtx(ctx, time.Second) {
634+
if needsCatalogSettle && d.server.catalogLags && !sleepCtx(ctx, time.Second) {
622635
log.Printf("OpenCode MCP wait timed out chat=%s after %s", d.chatID, time.Since(started))
623636
return states
624637
}

‎internal/api/chat_opencode_test.go‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ func TestOpenCodeChatServerAndEvents(t *testing.T) {
5050
emittedMu.Unlock()
5151
},
5252
findOpencode: func() (string, error) { return "opencode-test", nil },
53-
opencodeMajor: func(context.Context, string, []string) (int, error) {
53+
opencodeVersion: func(context.Context, string, []string) ([3]int, error) {
5454
versionChecks.Add(1)
55-
return 1, nil
55+
return [3]int{1}, nil
5656
},
5757
opencodeStarter: func(_ string, args, env []string) (opencodeProcess, error) {
5858
return fake.start(args, env)
@@ -590,9 +590,9 @@ func TestOpenCodeV2ChatAndMissingSessionFallback(t *testing.T) {
590590
server := New(database, "local", false, "dev", root, Options{
591591
DBPath: filepath.Join(root, "test.db"),
592592
findOpencode: func() (string, error) { return "opencode-v2-test", nil },
593-
opencodeMajor: func(context.Context, string, []string) (int, error) {
593+
opencodeVersion: func(context.Context, string, []string) ([3]int, error) {
594594
versionChecks.Add(1)
595-
return 2, nil
595+
return [3]int{2}, nil
596596
},
597597
opencodeStarter: func(_ string, args, env []string) (opencodeProcess, error) {
598598
return fake.start(args, env)
@@ -740,7 +740,7 @@ func TestOpenCodeV2WaitsForMCPAndNotesFailure(t *testing.T) {
740740
var emitted []map[string]any
741741
server := New(database, "local", false, "dev", root, Options{
742742
DBPath: filepath.Join(root, "test.db"), findOpencode: func() (string, error) { return "opencode-v2-test", nil },
743-
opencodeMajor: func(context.Context, string, []string) (int, error) { return 2, nil },
743+
opencodeVersion: func(context.Context, string, []string) ([3]int, error) { return [3]int{2}, nil },
744744
opencodeStarter: func(_ string, args, env []string) (opencodeProcess, error) { return fake.start(args, env) },
745745
Emit: func(_ string, event any) {
746746
emittedMu.Lock()
@@ -808,7 +808,7 @@ func TestOpenCodeV2LaterPromptDoesNotWaitForMCP(t *testing.T) {
808808
fake.mcpMode = "failed"
809809
server := New(database, "local", false, "dev", root, Options{
810810
DBPath: filepath.Join(root, "test.db"), findOpencode: func() (string, error) { return "opencode-v2-test", nil },
811-
opencodeMajor: func(context.Context, string, []string) (int, error) { return 2, nil },
811+
opencodeVersion: func(context.Context, string, []string) ([3]int, error) { return [3]int{2}, nil },
812812
opencodeStarter: func(_ string, args, env []string) (opencodeProcess, error) { return fake.start(args, env) },
813813
})
814814
addChatMCPTestServers(t, server)
@@ -850,7 +850,7 @@ func TestOpenCodeV2PendingMCPRefreshesAndReconnects(t *testing.T) {
850850
fake.mcpMode = "hang"
851851
server := New(database, "local", false, "dev", root, Options{
852852
DBPath: filepath.Join(root, "test.db"), findOpencode: func() (string, error) { return "opencode-v2-test", nil },
853-
opencodeMajor: func(context.Context, string, []string) (int, error) { return 2, nil },
853+
opencodeVersion: func(context.Context, string, []string) ([3]int, error) { return [3]int{2}, nil },
854854
opencodeStarter: func(_ string, args, env []string) (opencodeProcess, error) { return fake.start(args, env) },
855855
})
856856
addChatMCPTestServers(t, server)
@@ -890,7 +890,7 @@ func TestOpenCodeV1ReconnectsFailedMCPBeforePrompt(t *testing.T) {
890890
fake.mcpFailed = "stdio-test"
891891
server := New(database, "local", false, "dev", root, Options{
892892
DBPath: filepath.Join(root, "test.db"), findOpencode: func() (string, error) { return "opencode-test", nil },
893-
opencodeMajor: func(context.Context, string, []string) (int, error) { return 1, nil },
893+
opencodeVersion: func(context.Context, string, []string) ([3]int, error) { return [3]int{1}, nil },
894894
opencodeStarter: func(_ string, args, env []string) (opencodeProcess, error) { return fake.start(args, env) },
895895
})
896896
addChatMCPTestServers(t, server)
@@ -926,7 +926,7 @@ func TestOpenCodeV2ReconnectsFailedMCPBeforePrompt(t *testing.T) {
926926
fake.mcpMode = "recover"
927927
server := New(database, "local", false, "dev", root, Options{
928928
DBPath: filepath.Join(root, "test.db"), findOpencode: func() (string, error) { return "opencode-v2-test", nil },
929-
opencodeMajor: func(context.Context, string, []string) (int, error) { return 2, nil },
929+
opencodeVersion: func(context.Context, string, []string) ([3]int, error) { return [3]int{2}, nil },
930930
opencodeStarter: func(_ string, args, env []string) (opencodeProcess, error) { return fake.start(args, env) },
931931
})
932932
addChatMCPTestServers(t, server)
@@ -1329,7 +1329,7 @@ func TestOpenCodeV2QueueDrainResumesSessionWithoutDeadlock(t *testing.T) {
13291329
fake := newOpenCodeV2Fake(t)
13301330
server := New(database, "local", false, "dev", root, Options{
13311331
DBPath: filepath.Join(root, "test.db"), findOpencode: func() (string, error) { return "opencode-v2-test", nil },
1332-
opencodeMajor: func(context.Context, string, []string) (int, error) { return 2, nil },
1332+
opencodeVersion: func(context.Context, string, []string) ([3]int, error) { return [3]int{2}, nil },
13331333
opencodeStarter: func(_ string, args, env []string) (opencodeProcess, error) { return fake.start(args, env) },
13341334
})
13351335
addChatMCPTestServers(t, server)
@@ -1383,7 +1383,7 @@ func TestReloadMCPRetiresSharedOpenCodeServer(t *testing.T) {
13831383
}
13841384
a, b := createDriverTestChat(t, database, connectorID, root), createDriverTestChat(t, database, connectorID, root)
13851385
fake := newOpenCodeFake(t)
1386-
server := New(database, "local", false, "dev", root, Options{DBPath: filepath.Join(root, "test.db"), mcpExecutable: "/usr/bin/patio", findOpencode: func() (string, error) { return "opencode-test", nil }, opencodeMajor: func(context.Context, string, []string) (int, error) { return 1, nil }, opencodeStarter: func(_ string, args, env []string) (opencodeProcess, error) { return fake.start(args, env) }})
1386+
server := New(database, "local", false, "dev", root, Options{DBPath: filepath.Join(root, "test.db"), mcpExecutable: "/usr/bin/patio", findOpencode: func() (string, error) { return "opencode-test", nil }, opencodeVersion: func(context.Context, string, []string) ([3]int, error) { return [3]int{1}, nil }, opencodeStarter: func(_ string, args, env []string) (opencodeProcess, error) { return fake.start(args, env) }})
13871387
addChatMCPTestServers(t, server)
13881388
t.Cleanup(func() {
13891389
if err := server.Close(); err != nil {
@@ -1427,3 +1427,18 @@ func TestReloadMCPRetiresSharedOpenCodeServer(t *testing.T) {
14271427
t.Fatalf("starts=%d kills=%d after B restart", starts, kills)
14281428
}
14291429
}
1430+
1431+
func TestOpencodeCatalogLags(t *testing.T) {
1432+
for version, want := range map[[3]int]bool{
1433+
{1, 18, 35}: true,
1434+
{2, 1, 2}: true,
1435+
{2, 2, 5}: true,
1436+
{2, 2, 6}: false,
1437+
{2, 3, 0}: false,
1438+
{3, 0, 0}: false,
1439+
} {
1440+
if got := opencodeCatalogLags(version); got != want {
1441+
t.Errorf("opencodeCatalogLags(%v) = %v, want %v", version, got, want)
1442+
}
1443+
}
1444+
}

‎internal/api/server.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ type Options struct {
7878
MCPActor string
7979
opencodeStarter opencodeProcessStarter
8080
findOpencode func() (string, error)
81-
opencodeMajor func(context.Context, string, []string) (int, error)
81+
opencodeVersion func(context.Context, string, []string) ([3]int, error)
8282
mcpCheckTimeout time.Duration
8383
mcpConnect mcpConnector
8484
openOAuthURL func(string) error

‎internal/connector/connector.go‎

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,12 @@ func Check(ctx context.Context, opts Options) (out Status) {
276276
case "codex":
277277
statusErr = codexStatus(ctx, &out, opts)
278278
case "opencode":
279-
major, err := parseOpencodeMajor(version)
279+
parsed, err := parseOpencodeVersion(version)
280280
if err != nil {
281281
out.Note = "version: " + err.Error()
282282
return out
283283
}
284-
statusErr = openCodeStatus(ctx, &out, opts, major)
284+
statusErr = openCodeStatus(ctx, &out, opts, parsed[0])
285285
case "copilot":
286286
statusErr = copilotStatus(ctx, &out, opts)
287287
default:
@@ -293,30 +293,39 @@ func Check(ctx context.Context, opts Options) (out Status) {
293293
return out
294294
}
295295

296-
var opencodeVersion = regexp.MustCompile(`(?:^|[^0-9])v?([0-9]+)\.[0-9]+(?:\.[0-9]+)?`)
296+
var opencodeVersion = regexp.MustCompile(`(?:^|[^0-9])v?([0-9]+)\.([0-9]+)(?:\.([0-9]+))?`)
297297

298-
func parseOpencodeMajor(version string) (int, error) {
298+
// parseOpencodeVersion returns major, minor and patch; a missing patch is 0.
299+
func parseOpencodeVersion(version string) ([3]int, error) {
300+
var out [3]int
299301
match := opencodeVersion.FindStringSubmatch(strings.TrimSpace(version))
300-
if len(match) != 2 {
301-
return 0, fmt.Errorf("could not parse opencode version %q", strings.TrimSpace(version))
302+
if len(match) != 4 {
303+
return out, fmt.Errorf("could not parse opencode version %q", strings.TrimSpace(version))
302304
}
303-
major, err := strconv.Atoi(match[1])
304-
if err != nil {
305-
return 0, err
305+
for i, part := range match[1:] {
306+
if part == "" {
307+
continue
308+
}
309+
n, err := strconv.Atoi(part)
310+
if err != nil {
311+
return out, err
312+
}
313+
out[i] = n
306314
}
307-
return major, nil
315+
return out, nil
308316
}
309317

310318
func opencodeMajor(ctx context.Context, path string, env []string) (int, error) {
319+
version, err := OpencodeVersion(ctx, path, env)
320+
return version[0], err
321+
}
322+
323+
func OpencodeVersion(ctx context.Context, path string, env []string) ([3]int, error) {
311324
version, err := OpencodeCommandOutput(ctx, path, []string{"--version"}, env)
312325
if err != nil {
313-
return 0, err
326+
return [3]int{}, err
314327
}
315-
return parseOpencodeMajor(version)
316-
}
317-
318-
func OpencodeMajor(ctx context.Context, path string, env []string) (int, error) {
319-
return opencodeMajor(ctx, path, env)
328+
return parseOpencodeVersion(version)
320329
}
321330

322331
func commandOutput(ctx context.Context, path string, args []string, env []string) (string, error) {

‎internal/connector/connector_test.go‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,22 @@ func TestParseOpenCodeAuthJSON(t *testing.T) {
8787
}
8888
}
8989

90-
func TestParseOpencodeMajor(t *testing.T) {
90+
func TestParseOpencodeVersion(t *testing.T) {
9191
for _, tc := range []struct {
9292
version string
93-
major int
93+
want [3]int
9494
wantErr bool
9595
}{
96-
{"1.18.35", 1, false},
97-
{"v1.18.35", 1, false},
98-
{"opencode v2.0.26", 2, false},
99-
{"2.0.26", 2, false},
100-
{"unknown", 0, true},
96+
{"1.18.35", [3]int{1, 18, 35}, false},
97+
{"v1.18.35", [3]int{1, 18, 35}, false},
98+
{"opencode v2.0.26", [3]int{2, 0, 26}, false},
99+
{"2.2", [3]int{2, 2, 0}, false},
100+
{"unknown", [3]int{}, true},
101101
} {
102102
t.Run(tc.version, func(t *testing.T) {
103-
got, err := parseOpencodeMajor(tc.version)
104-
if (err != nil) != tc.wantErr || got != tc.major {
105-
t.Fatalf("major = %d, %v", got, err)
103+
got, err := parseOpencodeVersion(tc.version)
104+
if (err != nil) != tc.wantErr || got != tc.want {
105+
t.Fatalf("version = %v, %v", got, err)
106106
}
107107
})
108108
}
@@ -113,8 +113,8 @@ func TestParseOpencodeMajor(t *testing.T) {
113113
if err := os.WriteFile(path, []byte("#!/bin/sh\nprintf '%s\\n' 'opencode v2.0.26'\n"), 0o700); err != nil {
114114
t.Fatal(err)
115115
}
116-
if got, err := OpencodeMajor(context.Background(), path, []string{}); err != nil || got != 2 {
117-
t.Fatalf("OpencodeMajor = %d, %v", got, err)
116+
if got, err := OpencodeVersion(context.Background(), path, []string{}); err != nil || got != [3]int{2, 0, 26} {
117+
t.Fatalf("OpencodeVersion = %v, %v", got, err)
118118
}
119119
}
120120

0 commit comments

Comments
 (0)