From 474afb3484f66526bab7ef7afb912fde3a438563 Mon Sep 17 00:00:00 2001 From: cabbage Date: Mon, 9 Nov 2026 08:21:18 +0000 Subject: [PATCH] fix(server): keep script chunks within legacy client read limits --- docs/operations-runbook.md | 6 ++ internal/server/session/agent_server.go | 11 ++- skills/rvbox/references/troubleshooting.md | 6 ++ test/coverage.toml | 6 ++ .../websocket_limits_integration_test.go | 79 +++++++++++++++++++ 5 files changed, 107 insertions(+), 1 deletion(-) diff --git a/docs/operations-runbook.md b/docs/operations-runbook.md index 9607717..56a7e26 100644 --- a/docs/operations-runbook.md +++ b/docs/operations-runbook.md @@ -136,6 +136,12 @@ exact ignored `dist/` directory after preserving any evidence. Preserve spool/history: rebooting cannot remove a replayable oversized event. New server logs include bounded read/reconciliation errors and an `agent_message_too_big` counter; new clients advertise the actual build version. + If the server reports receiving that close code from an older client, the + client rejected a server-to-client frame. Outbound script chunks target + 16 KiB (or a lower configured chunk ceiling), preserving offsets/digests and + the 1 MiB send window, so pending uploads can reach older clients. This does + not lower the server's inbound limits. Large inline command text, stdin, or + reconciliation responses still require the corrected client reader. - **`terminal command cannot accept script data` during reconnect:** the server may replay an upload while the client is still delivering that command's terminal events. Corrected clients ignore these frames only after verifying diff --git a/internal/server/session/agent_server.go b/internal/server/session/agent_server.go index 2600666..d3db0f5 100644 --- a/internal/server/session/agent_server.go +++ b/internal/server/session/agent_server.go @@ -513,7 +513,10 @@ func (server *AgentServer) enqueueNextDispatch(ctx context.Context, queue *Write return candidate.IssueUUID, true, nil } -const scriptSendWindowBytes uint64 = 1 << 20 +const ( + scriptSendWindowBytes uint64 = 1 << 20 + scriptChunkTargetBytes uint64 = 16 << 10 +) type scriptTransfer struct { Body []byte @@ -531,6 +534,12 @@ func (server *AgentServer) enqueueNextScript(queue *WriterQueue, sessionID strin if chunkLimit == 0 { return false, errors.New("script chunk limit is zero") } + // The protocol ceiling is an upper bound, not a required chunk size. + // Leave room for envelope metadata below the historical 32 KiB client + // reader so an installed older client can receive its pending script. + if chunkLimit > scriptChunkTargetBytes { + chunkLimit = scriptChunkTargetBytes + } sentMu.Lock() keys := make([]string, 0, len(transfers)) for key := range transfers { diff --git a/skills/rvbox/references/troubleshooting.md b/skills/rvbox/references/troubleshooting.md index d2ff414..2abd125 100644 --- a/skills/rvbox/references/troubleshooting.md +++ b/skills/rvbox/references/troubleshooting.md @@ -50,6 +50,12 @@ command payloads, and count `agent_message_too_big` failures. Earlier clients reported the placeholder daemon version `v1`; use the installed executable's `--version` or PE product version when the server cannot identify the release. +If the server log says it **received** a `StatusMessageTooBig` close from the +client, inspect outbound traffic: the old client reader rejected it. Corrected +servers send scripts in 16 KiB chunks, allowing pending uploads to reach older +clients without lowering inbound validation limits. Large inline commands, +stdin, and reconciliation responses still need the updated client reader. + ## Windows identity, GUI, and shells `--elevated` is the only execution-context request toggle. Selection and diff --git a/test/coverage.toml b/test/coverage.toml index 9272f81..cdd243c 100644 --- a/test/coverage.toml +++ b/test/coverage.toml @@ -1163,6 +1163,12 @@ layer = "integration" status = "implemented" tests = ["test/integration/clientagent/websocket_limits_integration_test.go:TestLargeOutputReplaysAfterReconnect_REC_WS_01"] +[[requirements]] +id = "REC-WS-02" +layer = "integration" +status = "implemented" +tests = ["test/integration/clientagent/websocket_limits_integration_test.go:TestLargeScriptFitsLegacyReader_REC_WS_02"] + [[requirements]] id = "REC-RUNTIME-06" layer = "unit" diff --git a/test/integration/clientagent/websocket_limits_integration_test.go b/test/integration/clientagent/websocket_limits_integration_test.go index b2dcd7b..d11c6f1 100644 --- a/test/integration/clientagent/websocket_limits_integration_test.go +++ b/test/integration/clientagent/websocket_limits_integration_test.go @@ -3,6 +3,7 @@ package clientagent_test import ( "bytes" "context" + "crypto/sha256" "net/http/httptest" "path/filepath" "strings" @@ -91,6 +92,84 @@ func TestLargeDispatchAndReconciliation_HP_WS_01(t *testing.T) { } } +func TestLargeScriptFitsLegacyReader_REC_WS_02(t *testing.T) { + t.Parallel() + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + fixture := newFrameFixture(t) + legacy := agentproto.DefaultLimits() + legacy.MaxEnvelopeBytes = 32 << 10 + transport, err := agent.DialWebSocketWithLimits(ctx, "wss"+strings.TrimPrefix(fixture.server.URL, "https")+"/v1/agent", fixture.server.Client(), legacy) + if err != nil { + t.Fatal(err) + } + defer transport.Close() + accepted, err := agent.Handshake(ctx, transport, fixture.hello, legacy) + if err != nil { + t.Fatal(err) + } + if _, err := agent.Reconcile(ctx, transport, accepted, &rvboxv1.ReconcileSnapshot{}, legacy); err != nil { + t.Fatal(err) + } + body := bytes.Repeat([]byte("echo large-script\r\n"), 10000) + digest := sha256.Sum256(body) + queued, err := fixture.control.RunCommand(ctx, &rvboxv1.RunCommandRequest{TargetClientId: fixture.hello.ClientId, + Spec: &rvboxv1.ExecutionSpec{ShellType: rvboxv1.ShellType_SHELL_CMD, + Source: &rvboxv1.ExecutionSpec_Script{Script: &rvboxv1.ScriptDescriptor{ + Filename: "large.cmd", SizeBytes: uint64(len(body)), Sha256: digest[:], + }}}, ScriptContent: body}) + if err != nil { + t.Fatal(err) + } + dispatch := readFrame(t, ctx, transport).GetCommandDispatch() + if dispatch == nil || dispatch.IssueUuid != queued.IssueUuid { + t.Fatal("missing script dispatch") + } + ack, _ := proto.Marshal(&rvboxv1.AgentEnvelope{SessionId: accepted.ID, SessionGeneration: accepted.Generation, + Payload: &rvboxv1.AgentEnvelope_CommandAccepted{CommandAccepted: &rvboxv1.CommandAccepted{ + IssueUuid: dispatch.IssueUuid, CommandRevision: 1, Accepted: true, + }}}) + if err := transport.Write(ctx, ack); err != nil { + t.Fatal(err) + } + var received []byte + for { + envelope := readFrame(t, ctx, transport) + if chunk := envelope.GetScriptChunk(); chunk != nil { + if chunk.Offset != uint64(len(received)) || sha256.Sum256(chunk.Data) != bytesToSHA(chunk.Sha256) { + t.Fatal("script offset/checksum changed") + } + received = append(received, chunk.Data...) + continue + } + commit := envelope.GetScriptCommit() + if commit == nil || commit.SizeBytes != uint64(len(body)) || !bytes.Equal(commit.Sha256, digest[:]) || !bytes.Equal(received, body) { + t.Fatal("script commit/body changed") + } + break + } + for i, phase := range []rvboxv1.CommandLifecycle{rvboxv1.CommandLifecycle_COMMAND_RUNNING, rvboxv1.CommandLifecycle_COMMAND_SUCCEEDED} { + if err := agent.SendCommandEvent(ctx, transport, accepted, &rvboxv1.CommandEvent{IssueUuid: dispatch.IssueUuid, + EventSeq: uint64(i + 1), ObservedAt: timestamppb.Now(), Payload: &rvboxv1.CommandEvent_Lifecycle{ + Lifecycle: &rvboxv1.LifecycleChange{Lifecycle: phase, CommandRevision: 1}, + }}, agentproto.DefaultLimits()); err != nil { + t.Fatal(err) + } + if ack := readFrame(t, ctx, transport).GetEventAck(); ack == nil || ack.ThroughEventSeq != uint64(i+1) { + t.Fatal("script completion not acknowledged") + } + } + if next := fixture.dispatch(t, ctx, transport, "echo still-connected"); next.Spec.GetCommandText() != "echo still-connected" { + t.Fatal("legacy session failed after script upload") + } +} + +func bytesToSHA(value []byte) [sha256.Size]byte { + var result [sha256.Size]byte + copy(result[:], value) + return result +} + type frameFixture struct { persistence *store.Store control *control.Service