GeezerVison is showing github.com — fix(mcpx): disposition Codex round-2 findings on the ssh transport (#… · developing-today/code@0a9ee54
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: pkgs/mcpx/internal/cli/client.go +17-1Lines changed: 17 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -407,6 +407,22 @@ func (c *Client) Ping(ctx context.Context) bool { | 407 | 407 | return err == nil | 408 | 408 | } | 409 | 409 |
| | 410 | +// reachable is Ping with the ssh:// fallback EnsureDaemon has always had: | | 411 | +// an ssh handshake can legitimately take longer than | | 412 | +// autostart.pingTimeout, so a timed-out Ping is retried once as a plain | | 413 | +// health request bounded only by the caller's ctx before the daemon is | | 414 | +// declared unreachable. Non-ssh endpoints get Ping exactly. | | 415 | +func (c *Client) reachable(ctx context.Context) bool { | | 416 | + if c.Ping(ctx) { | | 417 | + return true | | 418 | + } | | 419 | + if c.ssh == nil { | | 420 | + return false | | 421 | + } | | 422 | + _, err := c.do(ctx, http.MethodGet, "/v1/health", nil) | | 423 | + return err == nil | | 424 | +} | | 425 | + | 410 | 426 | // EnsureDaemon starts a background daemon if one is not already running and | 411 | 427 | // waits for it to become reachable. This is what makes every command work with | 412 | 428 | // no setup step. | @@ -422,7 +438,7 @@ func (c *Client) EnsureDaemon(ctx context.Context) error { | 422 | 438 | // Starting a local daemon because a remote one is unreachable would | 423 | 439 | // silently answer from the wrong machine, which is worse than | 424 | 440 | // failing. | 425 | | - if c.Ping(ctx) { | | 441 | + if c.reachable(ctx) { | 426 | 442 | return nil | 427 | 443 | } | 428 | 444 | if err := ctx.Err(); err != nil { | |
Collapse file Copy file name to clipboardExpand all lines: pkgs/mcpx/internal/cli/connect.go +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -92,7 +92,7 @@ func (a *App) Connect(ctx context.Context, opt ConnectOptions) (*Client, *Ladder | 92 | 92 | if opt.Endpoint != "" { | 93 | 93 | start := time.Now() | 94 | 94 | c := NewClientAt(paths, a.ConfigPath, opt.Endpoint) | 95 | | - if c.Ping(ctx) { | | 95 | + if c.reachable(ctx) { | 96 | 96 | record("url", opt.Endpoint, start, nil) | 97 | 97 | l.Chosen = "url" | 98 | 98 | return c, l, nil | |
Collapse file Copy file name to clipboardExpand all lines: pkgs/mcpx/internal/cli/ssh_endpoint_test.go +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -208,3 +208,30 @@ func socketPathFor(t *testing.T) string { | 208 | 208 | t.Helper() | 209 | 209 | return filepath.Join(t.TempDir(), "should-not-be-used.sock") | 210 | 210 | } | | 211 | + | | 212 | +// The CLI connection ladder gets the same slow-handshake fallback as | | 213 | +// EnsureDaemon: Connect's named-endpoint rung used to Ping once, so a | | 214 | +// first ssh handshake slower than autostart.pingTimeout (2s) made every | | 215 | +// ordinary command report a reachable daemon as dead. The fake ssh | | 216 | +// sleeps 3s on its first invocation only; the fallback health request | | 217 | +// dials again and answers at once. | | 218 | +func TestSSHConnectFallsBackPastSlowHandshake(t *testing.T) { | | 219 | + sock := testSocketServer(t) | | 220 | + t.Setenv("MCPX_TEST_SSH_SOCK", sock) | | 221 | + marker := filepath.Join(t.TempDir(), "ssh.slowed") | | 222 | + t.Setenv("MCPX_TEST_SSH_SLOW", marker) | | 223 | + fakeSSH(t, "#!/bin/sh\n"+ | | 224 | + "if [ ! -f \"$MCPX_TEST_SSH_SLOW\" ]; then : > \"$MCPX_TEST_SSH_SLOW\"; sleep 3; fi\n"+ | | 225 | + "exec \""+os.Args[0]+"\" -test.run '^TestSSHShimProcess$' -- shim\n") | | 226 | + | | 227 | + a := &App{Paths: daemon.Paths{State: t.TempDir(), Socket: socketPathFor(t)}} | | 228 | + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) | | 229 | + defer cancel() | | 230 | + c, ladder, err := a.Connect(ctx, ConnectOptions{Endpoint: "ssh://buildbox"}) | | 231 | + if err != nil { | | 232 | + t.Fatalf("Connect over a slow ssh handshake: %v\n%s", err, ladder.Describe()) | | 233 | + } | | 234 | + if c == nil || ladder.Chosen != "url" { | | 235 | + t.Fatalf("Connect chose %q; want the named ssh endpoint", ladder.Chosen) | | 236 | + } | | 237 | +} |
Collapse file Copy file name to clipboardExpand all lines: pkgs/mcpx/internal/cli/stdio_proxy.go +14-1Lines changed: 14 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -56,7 +56,20 @@ func (a *App) CmdStdioProxy(ctx context.Context, args []string) error { | 56 | 56 | } | 57 | 57 | defer conn.Close() | 58 | 58 |
| 59 | | - return pipeConn(conn, os.Stdin, os.Stdout) | | 59 | + return proxyPipe(ctx, conn, os.Stdin, os.Stdout) | | 60 | +} | | 61 | + | | 62 | +// proxyPipe pipes between the daemon socket and the session's stdio, | | 63 | +// ending when the caller's ctx does. pipeConn alone blocks in io.Copy | | 64 | +// once dialling is done, so a SIGINT/SIGTERM delivered through main's | | 65 | +// signal.NotifyContext would leave the proxy alive indefinitely on a | | 66 | +// streaming response (the daemon streams /v1/events until the | | 67 | +// connection closes). Closing the socket on cancellation is what makes | | 68 | +// the copy return. | | 69 | +func proxyPipe(ctx context.Context, conn net.Conn, in io.Reader, out io.Writer) error { | | 70 | + stop := context.AfterFunc(ctx, func() { _ = conn.Close() }) | | 71 | + defer stop() | | 72 | + return pipeConn(conn, in, out) | 60 | 73 | } | 61 | 74 |
| 62 | 75 | // pipeConn copies in to conn and conn to out until the far end closes. | |
Collapse file Copy file name to clipboardExpand all lines: pkgs/mcpx/internal/cli/stdio_proxy_test.go +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -72,3 +72,25 @@ func TestStdioProxyNamesTheSocket(t *testing.T) { | 72 | 72 | } | 73 | 73 | } | 74 | 74 | } | | 75 | + | | 76 | +// Cancellation ends the proxy even when the daemon never sends another | | 77 | +// byte and never closes: the ctx-triggered socket close is the only | | 78 | +// thing that can interrupt the copy. | | 79 | +func TestProxyPipeStopsOnContextCancel(t *testing.T) { | | 80 | + client, server := net.Pipe() | | 81 | + defer server.Close() | | 82 | + | | 83 | + ctx, cancel := context.WithCancel(context.Background()) | | 84 | + done := make(chan error, 1) | | 85 | + go func() { | | 86 | + done <- proxyPipe(ctx, client, strings.NewReader(""), io.Discard) | | 87 | + }() | | 88 | + | | 89 | + time.Sleep(100 * time.Millisecond) | | 90 | + cancel() | | 91 | + select { | | 92 | + case <-done: | | 93 | + case <-time.After(5 * time.Second): | | 94 | + t.Fatal("proxyPipe kept piping after its context was cancelled") | | 95 | + } | | 96 | +} |
|