fix(transport): align WebSocket read limits with agent protocol

This commit is contained in:
cabbage committed 2026-11-09 07:53:18 +00:00
1 parent ef6e65e6f1
commit 6984a09486
11 files changed
+376 -4

No files matched your search

@@ -0,0 +1,179 @@
package clientagent_test
import (
"bytes"
"context"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"time"
rvboxv1 "github.com/rvbox/rvbox/gen/go/rvbox/v1"
"github.com/rvbox/rvbox/internal/agentproto"
"github.com/rvbox/rvbox/internal/client/agent"
"github.com/rvbox/rvbox/internal/domain"
"github.com/rvbox/rvbox/internal/server/control"
"github.com/rvbox/rvbox/internal/server/session"
"github.com/rvbox/rvbox/internal/server/store"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
func TestLargeOutputReplaysAfterReconnect_REC_WS_01(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
fixture := newFrameFixture(t)
transport, accepted := fixture.connect(t, ctx, &rvboxv1.ReconcileSnapshot{})
dispatch := fixture.dispatch(t, ctx, transport, "echo fixture")
send := func(event *rvboxv1.CommandEvent, sequence uint64) {
t.Helper()
if err := agent.SendCommandEvent(ctx, transport, accepted, event, agentproto.DefaultLimits()); err != nil {
t.Fatal(err)
}
ack := readFrame(t, ctx, transport).GetEventAck()
if ack == nil || ack.IssueUuid != dispatch.IssueUuid || ack.ThroughEventSeq != sequence {
t.Fatalf("event acknowledgement = %v", ack)
}
}
send(&rvboxv1.CommandEvent{IssueUuid: dispatch.IssueUuid, EventSeq: 1, ObservedAt: timestamppb.Now(),
Payload: &rvboxv1.CommandEvent_Lifecycle{Lifecycle: &rvboxv1.LifecycleChange{
Lifecycle: rvboxv1.CommandLifecycle_COMMAND_RUNNING, CommandRevision: 1,
}}}, 1)
data := bytes.Repeat([]byte("large output\n"), 5000)
chunk := &rvboxv1.OutputChunk{Stream: rvboxv1.StreamKind_STREAM_STDOUT, Compression: rvboxv1.Compression_COMPRESSION_NONE,
Data: data, UncompressedSize: uint64(len(data)), CompressedSize: uint64(len(data))}
event := &rvboxv1.CommandEvent{IssueUuid: dispatch.IssueUuid, EventSeq: 2, ObservedAt: timestamppb.Now(),
Payload: &rvboxv1.CommandEvent_Output{Output: chunk}}
send(event, 2)
if err := transport.Close(); err != nil {
t.Fatal(err)
}
// Simulate a lost acknowledgement: replay the same durable bytes in a new
// session. They must be accepted once without rerunning the command.
transport, accepted = fixture.connect(t, ctx, &rvboxv1.ReconcileSnapshot{RetainedCommands: []*rvboxv1.ReconcileCommandState{{
IssueUuid: dispatch.IssueUuid, Lifecycle: rvboxv1.CommandLifecycle_COMMAND_RUNNING,
CommandRevision: 1, LastClientEventSeq: 2, ImmutableRequestSha256: dispatch.ImmutableRequestSha256,
}}})
send(event, 2)
send(&rvboxv1.CommandEvent{IssueUuid: dispatch.IssueUuid, EventSeq: 3, ObservedAt: timestamppb.Now(),
Payload: &rvboxv1.CommandEvent_Lifecycle{Lifecycle: &rvboxv1.LifecycleChange{
Lifecycle: rvboxv1.CommandLifecycle_COMMAND_SUCCEEDED, CommandRevision: 1,
}}}, 3)
issue, _ := domain.ParseUUIDv7(dispatch.IssueUuid)
events, err := fixture.persistence.ReadCommandEvents(ctx, issue, 0, 10)
if err != nil || len(events) != 3 {
t.Fatalf("deduplicated events=%d error=%v", len(events), err)
}
}
func TestLargeDispatchAndReconciliation_HP_WS_01(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
fixture := newFrameFixture(t)
snapshot := &rvboxv1.ReconcileSnapshot{}
for i := 0; i < 1100; i++ {
issue, err := domain.NewUUIDv7()
if err != nil {
t.Fatal(err)
}
snapshot.RetainedCommands = append(snapshot.RetainedCommands, &rvboxv1.ReconcileCommandState{
IssueUuid: issue.String(), Lifecycle: rvboxv1.CommandLifecycle_COMMAND_SUCCEEDED,
CommandRevision: 1, Tombstoned: true, ImmutableRequestSha256: make([]byte, 32),
})
}
transport, _ := fixture.connect(t, ctx, snapshot)
command := "echo " + strings.Repeat("x", 64<<10)
if got := fixture.dispatch(t, ctx, transport, command).GetSpec().GetCommandText(); got != command {
t.Fatal("large dispatch text changed")
}
}
type frameFixture struct {
persistence *store.Store
control *control.Service
server *httptest.Server
hello *rvboxv1.ClientHello
}
func newFrameFixture(t *testing.T) *frameFixture {
t.Helper()
persistence, err := store.Open(context.Background(), store.Options{DataDir: filepath.Join(t.TempDir(), "state"), BusyTimeout: time.Second})
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = persistence.Close() })
registry := session.NewRegistry()
server := httptest.NewTLSServer(&session.AgentServer{Store: persistence, Registry: registry, Limits: agentproto.DefaultLimits()})
t.Cleanup(server.Close)
controlService, err := control.NewService(control.Options{Store: persistence, WakeClient: registry.Wake})
if err != nil {
t.Fatal(err)
}
return &frameFixture{persistence: persistence, control: controlService, server: server,
hello: &rvboxv1.ClientHello{ClientId: "large-frame-client", ClientInstanceId: fixedIssueID(0x91),
SupportedProtocol: &rvboxv1.ProtocolRange{Major: 1}, DaemonVersion: "frame-test", Platform: rvboxv1.Platform_PLATFORM_WINDOWS,
Architecture: "amd64", DaemonCwd: `C:\`, SupportedShells: []rvboxv1.ShellType{rvboxv1.ShellType_SHELL_CMD},
MaxRunningCommands: 2, MaxQueuedCommands: 4, SentAt: timestamppb.Now()}}
}
func (fixture *frameFixture) connect(t *testing.T, ctx context.Context, snapshot *rvboxv1.ReconcileSnapshot) (*agent.WebSocketTransport, agent.Session) {
t.Helper()
transport, err := agent.DialWebSocket(ctx, "wss"+strings.TrimPrefix(fixture.server.URL, "https")+"/v1/agent", fixture.server.Client())
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = transport.Close() })
accepted, err := agent.Handshake(ctx, transport, fixture.hello, agentproto.DefaultLimits())
if err != nil {
t.Fatal(err)
}
result, err := agent.Reconcile(ctx, transport, accepted, snapshot, agentproto.DefaultLimits())
if err != nil {
t.Fatal(err)
}
if len(snapshot.RetainedCommands) > 1 && len(result.DiscardLocalTerminalIssueUuids) != len(snapshot.RetainedCommands) {
t.Fatal("large reconciliation result incomplete")
}
return transport, accepted
}
func (fixture *frameFixture) dispatch(t *testing.T, ctx context.Context, transport *agent.WebSocketTransport, text string) *rvboxv1.CommandDispatch {
t.Helper()
if _, err := fixture.control.RunCommand(ctx, &rvboxv1.RunCommandRequest{TargetClientId: fixture.hello.ClientId,
Spec: &rvboxv1.ExecutionSpec{ShellType: rvboxv1.ShellType_SHELL_CMD, Source: &rvboxv1.ExecutionSpec_CommandText{CommandText: text}},
}); err != nil {
t.Fatal(err)
}
envelope := readFrame(t, ctx, transport)
dispatch := envelope.GetCommandDispatch()
if dispatch == nil {
t.Fatal("expected command dispatch")
}
ack, err := proto.Marshal(&rvboxv1.AgentEnvelope{SessionId: envelope.SessionId, SessionGeneration: envelope.SessionGeneration,
Payload: &rvboxv1.AgentEnvelope_CommandAccepted{CommandAccepted: &rvboxv1.CommandAccepted{
IssueUuid: dispatch.IssueUuid, CommandRevision: dispatch.CommandRevision, Accepted: true,
}}})
if err != nil {
t.Fatal(err)
}
if err := transport.Write(ctx, ack); err != nil {
t.Fatal(err)
}
return dispatch
}
func readFrame(t *testing.T, ctx context.Context, transport *agent.WebSocketTransport) *rvboxv1.AgentEnvelope {
t.Helper()
encoded, err := transport.Read(ctx)
if err != nil {
t.Fatal(err)
}
envelope, err := agentproto.DecodeEnvelope(encoded, agentproto.DefaultLimits(), rvboxv1.Platform_PLATFORM_WINDOWS)
if err != nil {
t.Fatal(err)
}
return envelope
}