feat: apply terminal reconciliation cleanup

This commit is contained in:
2026-09-06 07:22:17 +00:00
parent 7e902d1103
commit 0a0b6f9707
5 changed files with 202 additions and 0 deletions
+25
View File
@@ -8,6 +8,7 @@ import (
rvboxv1 "github.com/rvbox/rvbox/gen/go/rvbox/v1"
"github.com/rvbox/rvbox/internal/agentproto"
"github.com/rvbox/rvbox/internal/domain"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
@@ -64,11 +65,35 @@ func TestReconcileSnapshot_HP_SES_08(t *testing.T) {
}
}
func TestApplyReconcileResult_HP_SES_12(t *testing.T) {
t.Parallel()
active := "019c46f1-1d02-7000-8000-000000000062"
terminal := "019c46f1-1d02-7000-8000-000000000063"
store := &reconcileStore{}
terminated, err := ApplyReconcileResult(context.Background(), store, &rvboxv1.ReconcileResult{
TerminateLocalIssueUuids: []string{active},
DiscardLocalTerminalIssueUuids: []string{terminal},
}, time.Date(2026, time.September, 6, 0, 0, 0, 0, time.UTC))
if err != nil || len(terminated) != 1 || terminated[0].String() != active || len(store.discarded) != 1 || store.discarded[0].String() != terminal {
t.Fatalf("ApplyReconcileResult = terminated=%v discarded=%v err=%v", terminated, store.discarded, err)
}
if _, err := ApplyReconcileResult(context.Background(), store, &rvboxv1.ReconcileResult{TerminateLocalIssueUuids: []string{active}, DiscardLocalTerminalIssueUuids: []string{active}}, time.Now()); !errors.Is(err, ErrInvalidReconcileResult) {
t.Fatalf("overlapping result error = %v", err)
}
}
type fakeTransport struct {
written []byte
read []byte
}
type reconcileStore struct{ discarded []domain.UUID }
func (store *reconcileStore) DiscardTerminal(_ context.Context, issue domain.UUID, _ time.Time) error {
store.discarded = append(store.discarded, issue)
return nil
}
func (transport *fakeTransport) Write(_ context.Context, value []byte) error {
transport.written = append([]byte(nil), value...)
return nil
+54
View File
@@ -0,0 +1,54 @@
package agent
import (
"context"
"errors"
"time"
rvboxv1 "github.com/rvbox/rvbox/gen/go/rvbox/v1"
"github.com/rvbox/rvbox/internal/domain"
)
var ErrInvalidReconcileResult = errors.New("invalid reconciliation result")
// ReconcileStore is the narrowly scoped durable operation required after the
// server has confirmed a terminal command. Keeping the interface small lets the
// network loop remain independent of SQLite and of the Windows supervisor.
type ReconcileStore interface {
DiscardTerminal(context.Context, domain.UUID, time.Time) error
}
// ApplyReconcileResult commits server-authorized terminal discards before the
// session starts new work. It returns the active command IDs that the caller
// must first terminate through the supervisor; marking those terminal is not
// safe until the owned process and output readers have stopped.
func ApplyReconcileResult(ctx context.Context, store ReconcileStore, result *rvboxv1.ReconcileResult, now time.Time) ([]domain.UUID, error) {
if store == nil || result == nil || now.IsZero() {
return nil, ErrInvalidReconcileResult
}
seen := make(map[domain.UUID]struct{}, len(result.GetTerminateLocalIssueUuids())+len(result.GetDiscardLocalTerminalIssueUuids()))
terminate := make([]domain.UUID, 0, len(result.GetTerminateLocalIssueUuids()))
for _, set := range [][]string{result.GetTerminateLocalIssueUuids(), result.GetDiscardLocalTerminalIssueUuids()} {
for _, text := range set {
issue, err := domain.ParseUUIDv7(text)
if err != nil {
return nil, ErrInvalidReconcileResult
}
if _, duplicate := seen[issue]; duplicate {
return nil, ErrInvalidReconcileResult
}
seen[issue] = struct{}{}
}
}
for _, text := range result.GetDiscardLocalTerminalIssueUuids() {
issue, _ := domain.ParseUUIDv7(text)
if err := store.DiscardTerminal(ctx, issue, now); err != nil {
return nil, err
}
}
for _, text := range result.GetTerminateLocalIssueUuids() {
issue, _ := domain.ParseUUIDv7(text)
terminate = append(terminate, issue)
}
return terminate, nil
}