Report a stale client and refuse an unreadable newer server
Exchange a build/protocol identity so an older CLI can no longer talk to a newer native server silently: - protocol carries ProtocolVersion and every server frame (including the unsolicited welcome) is stamped with the server build and protocol, so a client that never pings still learns the server identity. - buildinfo.Compare classifies the relationship as InSync/Warn/Refuse and names both builds; the client Watch reports one warning per connection and returns a hard error when the server speaks a newer, uninterpretable protocol so the caller never acts on the frame. - client.ReceiveNotice decodes the raw frame so an unmodeled top-level field produces a one-time "unrecognized field(s)" notice instead of being dropped silently (the case that hid the artifacts field). - CLI routes warnings to stderr, keeps machine JSON stdout clean and fails non-zero on a refused mismatch; ping/connection print the client/server relationship. The TUI surfaces the same notices through the shared log. - scripts/setup-native.py now warns when an older `pipelines` earlier on PATH shadows prefix/bin/pipelines, naming both paths and versions, without installing or modifying anything else. - Docs, the packaged debugging skill reference and README no longer imply the installed CLI always matches the server. Tests cover the comparison table, unknown-field detection and caching, the server identity stamping on pong and welcome, the CLI/TUI reporting paths and the installer PATH shadowing warning.
This commit is contained in:
@@ -112,6 +112,16 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
|
||||
defer connCancel()
|
||||
var writeMu sync.Mutex
|
||||
write := func(message protocol.Message) error {
|
||||
// Every server frame (responses, the welcome and chat events) carries
|
||||
// the build identity and protocol so an older client fails fast or
|
||||
// warns once instead of silently misreading a field. build is
|
||||
// computed once per handler.
|
||||
if message.Build == nil {
|
||||
message.Build = &build
|
||||
}
|
||||
if message.Protocol == 0 {
|
||||
message.Protocol = protocol.ProtocolVersion
|
||||
}
|
||||
writeMu.Lock()
|
||||
defer writeMu.Unlock()
|
||||
writeCtx, cancel := context.WithTimeout(connCtx, 5*time.Second)
|
||||
@@ -149,7 +159,6 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
|
||||
response.Status = systemStatus(r, started, runErr)
|
||||
} else if request.Type == protocol.Ping {
|
||||
response.Time = time.Now().UTC().Format(time.RFC3339)
|
||||
response.Build = &build
|
||||
} else if request.Type == protocol.ListTemplates {
|
||||
response.Type = protocol.Templates
|
||||
response.Templates = examples.Catalog()
|
||||
@@ -208,6 +217,15 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
|
||||
} else {
|
||||
response.Pipelines = items
|
||||
}
|
||||
} else if request.Type == protocol.Search && len(stores) > 0 && runs != nil {
|
||||
response.Type = protocol.SearchResults
|
||||
matches, err := runs.search(request)
|
||||
if err != nil {
|
||||
response.Type = protocol.Error
|
||||
response.Error = err.Error()
|
||||
} else {
|
||||
response.Matches = matches
|
||||
}
|
||||
} else if runs != nil && (request.Type == protocol.RunPipeline || request.Type == protocol.RetryRun || request.Type == protocol.ListRuns || request.Type == protocol.GetRun || request.Type == protocol.CancelRun || request.Type == protocol.DecideRun || request.Type == protocol.SetRunTitle || request.Type == protocol.GetRunEvidence) {
|
||||
var err error
|
||||
switch request.Type {
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -14,6 +15,7 @@ import (
|
||||
"github.com/coder/websocket/wsjson"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/buildinfo"
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
@@ -77,6 +79,9 @@ func TestPersistentConnection(t *testing.T) {
|
||||
if response.Build == nil || response.Build.Version != wantBuild.Version || response.Build.Commit != wantBuild.Commit {
|
||||
t.Fatalf("pong missing server build identity: %+v", response.Build)
|
||||
}
|
||||
if response.Protocol != protocol.ProtocolVersion {
|
||||
t.Fatalf("pong protocol = %d, want %d", response.Protocol, protocol.ProtocolVersion)
|
||||
}
|
||||
stamp, err := time.Parse(time.RFC3339, response.Time)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -97,3 +102,38 @@ func TestPersistentConnection(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestWelcomeEnvelopeCarriesServerIdentity proves a client that never pings
|
||||
// still learns the server build and protocol from the unsolicited welcome.
|
||||
func TestWelcomeEnvelopeCarriesServerIdentity(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
store, err := pipeline.Open(filepath.Join(t.TempDir(), "identity.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
handler := Handler(ctx, store)
|
||||
srv := httptest.NewServer(handler)
|
||||
defer srv.Close()
|
||||
defer func() { cancel(); WaitRuns(handler) }()
|
||||
conn, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer conn.CloseNow()
|
||||
var welcome protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &welcome); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if welcome.Type != protocol.PipelineList {
|
||||
t.Fatalf("first frame = %q, want welcome pipelines list", welcome.Type)
|
||||
}
|
||||
wantBuild := buildinfo.Current()
|
||||
if welcome.Build == nil || welcome.Build.Version != wantBuild.Version || welcome.Build.Commit != wantBuild.Commit {
|
||||
t.Fatalf("welcome missing server build identity: %+v", welcome.Build)
|
||||
}
|
||||
if welcome.Protocol != protocol.ProtocolVersion {
|
||||
t.Fatalf("welcome protocol = %d, want %d", welcome.Protocol, protocol.ProtocolVersion)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user