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:
2026-10-16 18:37:01 +01:00
parent 98bbc2deac
commit 8c1eea53a9
25 changed files with 1239 additions and 81 deletions
+60
View File
@@ -0,0 +1,60 @@
package protocol
import (
"encoding/json"
"reflect"
"sort"
"strings"
"sync"
)
// ProtocolVersion is the integer wire protocol this build speaks. Ordering
// between peers compares this integer; build strings are for naming only and are
// never parsed or sorted to infer ordering.
const ProtocolVersion = 1
// MaxSupportedProtocol is the newest server protocol this client can interpret.
// A server reporting a higher value is refused rather than silently misread.
const MaxSupportedProtocol = ProtocolVersion
var (
knownFieldsOnce sync.Once
knownFields map[string]struct{}
)
// KnownFields returns the set of top-level JSON keys protocol.Message models,
// derived once from the struct tags and cached. The map is read-only.
func KnownFields() map[string]struct{} {
knownFieldsOnce.Do(func() {
knownFields = map[string]struct{}{}
message := reflect.TypeOf(Message{})
for index := 0; index < message.NumField(); index++ {
name := strings.Split(message.Field(index).Tag.Get("json"), ",")[0]
if name == "" || name == "-" {
continue
}
knownFields[name] = struct{}{}
}
})
return knownFields
}
// UnknownFields returns the sorted top-level JSON keys in data that protocol.Message
// does not model. It performs one bounded map decode plus O(1) set lookups, so a
// forward-compatible frame is never a per-frame reflection walk. A frame that is
// not a JSON object (or cannot be parsed) reports no unknown fields.
func UnknownFields(data []byte) []string {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return nil
}
known := KnownFields()
var unknown []string
for key := range raw {
if _, ok := known[key]; !ok {
unknown = append(unknown, key)
}
}
sort.Strings(unknown)
return unknown
}
+52
View File
@@ -0,0 +1,52 @@
package protocol
import (
"fmt"
"reflect"
"testing"
)
func TestUnknownFieldsReportsOnlyUnmodeledSortedKeys(t *testing.T) {
if got := UnknownFields([]byte(`{"type":"pong"}`)); got != nil {
t.Fatalf("fully modeled frame reported %v", got)
}
got := UnknownFields([]byte(`{"type":"pong","zzz":1,"aaa":2}`))
if !reflect.DeepEqual(got, []string{"aaa", "zzz"}) {
t.Fatalf("unknown fields = %v, want sorted [aaa zzz]", got)
}
}
func TestUnknownFieldsIgnoresMalformedAndNonObjectFrames(t *testing.T) {
for _, data := range []string{
`not json`,
`[1,2,3]`,
`"a string"`,
`42`,
`null`,
} {
if got := UnknownFields([]byte(data)); got != nil {
t.Fatalf("frame %q reported unknown fields %v", data, got)
}
}
}
func TestKnownFieldsCachesTheModeledKeySet(t *testing.T) {
first := KnownFields()
second := KnownFields()
if len(first) == 0 {
t.Fatal("known field set is empty")
}
if _, ok := first["type"]; !ok {
t.Fatal("known field set is missing the modeled type key")
}
if _, ok := first["build"]; !ok {
t.Fatal("known field set is missing the modeled build key")
}
if _, ok := first["protocol"]; !ok {
t.Fatal("known field set is missing the modeled protocol key")
}
// The sync.Once must return the same cached map on every call.
if fmt.Sprintf("%p", first) != fmt.Sprintf("%p", second) {
t.Fatal("KnownFields re-derived the set instead of using the cache")
}
}
+12 -1
View File
@@ -102,10 +102,17 @@ type PipelineSummary struct {
UpdatedAt string `json:"updated_at"`
}
// Message is the JSON envelope shared by the server and client.
// Message is the JSON envelope shared by the server and client. It also carries
// the omni-search request/response: a client sends Type "search" with an ID, the
// Query text, an optional Kind type filter and an optional Limit, and the server
// replies with Type "search_results", the echoed ID and ranked Matches. Every
// SearchMatch carries a stable Target plus the label and per-kind evidence; all
// search fields are optional so older peers ignore them. See search.go for the
// kind constants and wire-shape detail.
type Message struct {
DefaultHarness string `json:"default_harness,omitempty"`
Build *buildinfo.Info `json:"build,omitempty"`
Protocol int `json:"protocol,omitempty"`
DefaultModel string `json:"default_model,omitempty"`
Evidence []SessionEvidence `json:"evidence,omitempty"`
Usage *UsageStats `json:"usage,omitempty"`
@@ -123,6 +130,8 @@ type Message struct {
Source string `json:"source,omitempty"`
Title string `json:"title,omitempty"`
Version int `json:"version,omitempty"`
Query string `json:"query,omitempty"`
Kind string `json:"kind,omitempty"`
Pipelines []PipelineSummary `json:"pipelines,omitempty"`
PipelineID int64 `json:"pipeline_id,omitempty"`
Detail *PipelineDetail `json:"detail,omitempty"`
@@ -132,11 +141,13 @@ type Message struct {
Stage string `json:"stage,omitempty"`
Option string `json:"option,omitempty"`
Feedback string `json:"feedback,omitempty"`
Matches []SearchMatch `json:"matches,omitempty"`
Input map[string]any `json:"input,omitempty"`
AllowTools bool `json:"allow_tools,omitempty"`
WritePaths []string `json:"write_paths,omitempty"`
RequestKey string `json:"request_key,omitempty"`
Workspace string `json:"workspace,omitempty"`
Limit int `json:"limit,omitempty"`
// Chat is the brokered OpenCode chat traffic (route B). See chat.go for the
// per-type payload structs and the wire shapes they mirror.
ChatSessions []ChatSession `json:"chat_sessions,omitempty"`