fix(client): tolerate script replay after command completion

This commit is contained in:
cabbage committed 2026-11-09 08:04:42 +00:00
1 parent 6984a09486
commit 1b5c6dbf98
5 files changed
+180 -2

No files matched your search

+133
View File
@@ -0,0 +1,133 @@
package agent
import (
"bytes"
"context"
"crypto/sha256"
"errors"
"path/filepath"
"testing"
"time"
rvboxv1 "github.com/rvbox/rvbox/gen/go/rvbox/v1"
"github.com/rvbox/rvbox/internal/agentproto"
"github.com/rvbox/rvbox/internal/client/spool"
"github.com/rvbox/rvbox/internal/domain"
"github.com/rvbox/rvbox/internal/observability"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
func TestFinishedScriptReplayKeepsSessionUsable_REC_RUNTIME_06(t *testing.T) {
t.Parallel()
ctx := context.Background()
store, err := spool.Open(ctx, spool.Options{DataDir: filepath.Join(t.TempDir(), "spool"), BusyTimeout: time.Second})
if err != nil {
t.Fatal(err)
}
defer store.Close()
issue, _ := domain.ParseUUIDv7("019c46f1-1d02-7000-8000-0000000000e1")
body := []byte("echo finished script")
digest := sha256.Sum256(body)
descriptor := spool.ScriptDescriptor{SizeBytes: uint64(len(body)), SHA256: digest}
spec := &rvboxv1.ExecutionSpec{ShellType: rvboxv1.ShellType_SHELL_CMD, Source: &rvboxv1.ExecutionSpec_Script{
Script: &rvboxv1.ScriptDescriptor{Filename: "done.cmd", SizeBytes: uint64(len(body)), Sha256: digest[:]},
}}
encoded, _ := proto.Marshal(spec)
if _, err := store.AcceptCommand(ctx, spool.Command{IssueUUID: issue, ImmutableSHA256: digest, Revision: 1,
Phase: uint32(rvboxv1.CommandLifecycle_COMMAND_ACCEPTED), ExecutionSpec: encoded, Script: &descriptor}, time.Now()); err != nil {
t.Fatal(err)
}
if _, err := store.AppendScriptChunk(ctx, issue, 0, body, digest); err != nil {
t.Fatal(err)
}
if _, err := store.CommitScript(ctx, issue, descriptor); err != nil {
t.Fatal(err)
}
// A committed but live script must still reject new bytes. The shared
// ErrScriptTerminal value alone is not sufficient to ignore a frame.
if err := finishedScriptReplay(ctx, store, issue.String(), spool.ErrScriptTerminal); !errors.Is(err, spool.ErrScriptTerminal) {
t.Fatalf("live script error suppressed: %v", err)
}
for _, phase := range []rvboxv1.CommandLifecycle{rvboxv1.CommandLifecycle_COMMAND_RUNNING, rvboxv1.CommandLifecycle_COMMAND_SUCCEEDED} {
if _, err := store.AppendLifecycle(ctx, issue, uint32(phase), 1, "finished offline", time.Now()); err != nil {
t.Fatal(err)
}
}
if err := finishedScriptReplay(ctx, store, issue.String(), spool.ErrScriptDigest); !errors.Is(err, spool.ErrScriptDigest) {
t.Fatalf("checksum failure suppressed: %v", err)
}
original, err := store.ScriptBody(ctx, issue)
if err != nil {
t.Fatal(err)
}
marshal := func(payload *rvboxv1.AgentEnvelope) []byte {
t.Helper()
payload.SessionId, payload.SessionGeneration = "session", 1
encoded, err := proto.Marshal(payload)
if err != nil {
t.Fatal(err)
}
return encoded
}
fresh, _ := domain.ParseUUIDv7("019c46f1-1d02-7000-8000-0000000000e2")
transport := &scriptReplayTransport{runnerTransport: runnerTransport{reads: [][]byte{
marshal(&rvboxv1.AgentEnvelope{Payload: &rvboxv1.AgentEnvelope_ScriptChunk{ScriptChunk: &rvboxv1.ScriptChunk{
IssueUuid: issue.String(), Data: body, Sha256: digest[:],
}}}),
marshal(&rvboxv1.AgentEnvelope{Payload: &rvboxv1.AgentEnvelope_ScriptCommit{ScriptCommit: &rvboxv1.ScriptCommit{
IssueUuid: issue.String(), SizeBytes: uint64(len(body)), Sha256: digest[:],
}}}),
marshal(&rvboxv1.AgentEnvelope{Payload: &rvboxv1.AgentEnvelope_CommandDispatch{CommandDispatch: &rvboxv1.CommandDispatch{
IssueUuid: fresh.String(), CommandRevision: 1, TargetSessionGeneration: 1, IssueTime: timestamppb.Now(),
ImmutableRequestSha256: digest[:], Spec: &rvboxv1.ExecutionSpec{ShellType: rvboxv1.ShellType_SHELL_CMD,
Source: &rvboxv1.ExecutionSpec_CommandText{CommandText: "echo next"}},
}}}),
}}}
runCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
dispatched, done := make(chan struct{}, 1), make(chan error, 1)
readyCalls := 0
metrics := observability.New()
go func() {
done <- serveActive(runCtx, transport, RunnerOptions{Store: store, Limits: agentproto.DefaultLimits(), Now: time.Now, Metrics: metrics,
OnScriptReady: func(context.Context, Session, domain.UUID) error { readyCalls++; return nil },
OnDispatch: func(context.Context, Session, *rvboxv1.CommandDispatch) error { dispatched <- struct{}{}; return nil },
}, Session{ID: "session", Generation: 1}, map[domain.UUID]uint64{})
}()
select {
case err := <-done:
t.Fatalf("stale upload closed session: %v", err)
case <-dispatched:
cancel()
<-done
case <-runCtx.Done():
t.Fatal("fresh command could not pass stale upload")
}
if readyCalls != 0 {
t.Fatal("finished script was made launch-eligible again")
}
retained, err := store.ScriptBody(ctx, issue)
if err != nil || !bytes.Equal(original, retained) {
t.Fatalf("finished script bytes changed: %v", err)
}
_, _, counters := metrics.Snapshot()
if counters["terminal_script_frame"] != 2 {
t.Fatalf("stale frame count: %v", counters)
}
}
type scriptReplayTransport struct{ runnerTransport }
func (transport *scriptReplayTransport) Read(ctx context.Context) ([]byte, error) {
transport.mu.Lock()
if len(transport.reads) != 0 {
frame := transport.reads[0]
transport.reads = transport.reads[1:]
transport.mu.Unlock()
return frame, nil
}
transport.mu.Unlock()
<-ctx.Done()
return nil, ctx.Err()
}