Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

9.4 KB
Raw Blame
// Copyright 2026 The OpenSandbox Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package source coordinates the Sources compiled into Node Agent.
package source

import (
	"context"
	"errors"
	"fmt"
	"slices"
	"strings"
	"sync"

	"github.com/alibaba/opensandbox/nodeagent/pkg/api"
)

// Child pairs one Source with its registry name. The name is also the
// StreamRef namespace prefix the Source must prefix to every stream ID it
// emits.
type Child struct {
	Name   string
	Source api.Source
}

// Mux fans several Sources into one event channel, routes acknowledgements
// back to the emitting Source, and enforces per-Source stream namespacing. It
// implements api.Source.
type Mux struct {
	children     []Child
	byName       map[string]api.Source
	kindsByName  map[string][]api.RecordKind
	capabilities api.Capabilities
	onError      func(error)

	mu      sync.Mutex
	started bool
	cancel  context.CancelFunc
	active  []Child
	done    chan struct{}
}

// NewMux validates the child set: names must be unique, non-empty, and
// slash-free, every Source must declare at least one record kind, and kinds
// must be unique across children.
func NewMux(children []Child, onError func(error)) (*Mux, error) {
	if len(children) == 0 {
		return nil, errors.New("source mux requires at least one Source")
	}
	mux := &Mux{
		children:    append([]Child(nil), children...),
		byName:      make(map[string]api.Source, len(children)),
		kindsByName: make(map[string][]api.RecordKind, len(children)),
		onError:     onError,
		done:        make(chan struct{}),
	}
	kinds := make(map[api.RecordKind]struct{})
	for _, child := range mux.children {
		if child.Name == "" || strings.Contains(child.Name, "/") || child.Source == nil {
			return nil, errors.New("source mux child requires a name and Source")
		}
		if _, exists := mux.byName[child.Name]; exists {
			return nil, fmt.Errorf("source mux contains duplicate Source %q", child.Name)
		}
		mux.byName[child.Name] = child.Source
		childKinds := child.Source.Capabilities().RecordKinds
		if len(childKinds) == 0 {
			return nil, fmt.Errorf("source %q declares no record kinds", child.Name)
		}
		mux.kindsByName[child.Name] = append([]api.RecordKind(nil), childKinds...)
		for _, kind := range childKinds {
			if kind == "" {
				return nil, fmt.Errorf("source %q declares an empty record kind", child.Name)
			}
			if _, exists := kinds[kind]; exists {
				continue
			}
			kinds[kind] = struct{}{}
			mux.capabilities.RecordKinds = append(mux.capabilities.RecordKinds, kind)
		}
	}
	return mux, nil
}

// Capabilities returns the union of the children's record kinds.
func (m *Mux) Capabilities() api.Capabilities {
	return api.Capabilities{RecordKinds: append([]api.RecordKind(nil), m.capabilities.RecordKinds...)}
}

// Start starts every child against runCtx and forwards their events to out.
// It closes out after all children have stopped. If a child fails to start,
// the already-started children are stopped and the error is joined with
// their Stop results.
func (m *Mux) Start(ctx context.Context, out chan<- api.SourceEvent) error {
	m.mu.Lock()
	if m.started {
		m.mu.Unlock()
		return errors.New("source mux already started")
	}
	runCtx, cancel := context.WithCancel(ctx)
	m.started = true
	m.cancel = cancel
	m.mu.Unlock()

	type childInput struct {
		child  Child
		events chan api.SourceEvent
	}
	inputs := make([]childInput, len(m.children))
	var forwarders sync.WaitGroup
	forwarders.Add(len(inputs))
	for index, child := range m.children {
		inputs[index] = childInput{child: child, events: make(chan api.SourceEvent)}
		go func(input childInput) {
			defer forwarders.Done()
			m.forward(runCtx, input.child, input.events, out)
		}(inputs[index])
	}
	go func() {
		forwarders.Wait()
		close(out)
		close(m.done)
	}()

	for _, input := range inputs {
		if err := input.child.Source.Start(runCtx, input.events); err != nil {
			cancel()
			m.mu.Lock()
			started := m.active
			m.active = nil
			m.mu.Unlock()
			stopErr := stopChildren(ctx, started)
			<-m.done
			return errors.Join(fmt.Errorf("start source %q: %w", input.child.Name, err), stopErr)
		}
		m.mu.Lock()
		m.active = append(m.active, input.child)
		m.mu.Unlock()
	}
	return nil
}

// Acknowledge routes a same-Source acknowledgement batch to the emitting
// child. Batches mixing several Sources are permanent errors.
func (m *Mux) Acknowledge(ctx context.Context, results []api.AckResult) error {
	if len(results) == 0 {
		return nil
	}
	name := results[0].Token.Source
	child := m.byName[name]
	if child == nil {
		return api.Permanent(fmt.Errorf("ack token refers to unknown Source %q", name))
	}
	for _, result := range results[1:] {
		if result.Token.Source != name {
			return api.Permanent(errors.New("acknowledgement batch contains multiple Sources"))
		}
	}
	return child.Acknowledge(ctx, results)
}

// AcknowledgeEnd routes a stream-end acknowledgement to the emitting child.
func (m *Mux) AcknowledgeEnd(ctx context.Context, token api.EndToken) error {
	child := m.byName[token.Source]
	if child == nil {
		return api.Permanent(fmt.Errorf("end token refers to unknown Source %q", token.Source))
	}
	return child.AcknowledgeEnd(ctx, token)
}

// Stop cancels the run context, stops every active child, and waits for the
// forwarded event channel to close or ctx to expire. It is safe to call
// before Start or twice.
func (m *Mux) Stop(ctx context.Context) error {
	m.mu.Lock()
	if !m.started {
		m.mu.Unlock()
		return nil
	}
	cancel := m.cancel
	active := append([]Child(nil), m.active...)
	m.active = nil
	m.mu.Unlock()
	cancel()
	stopErr := stopChildren(ctx, active)
	select {
	case <-m.done:
		return stopErr
	case <-ctx.Done():
		return errors.Join(stopErr, ctx.Err())
	}
}

func (m *Mux) forward(ctx context.Context, child Child, events <-chan api.SourceEvent, out chan<- api.SourceEvent) {
	for {
		select {
		case <-ctx.Done():
			return
		case event, ok := <-events:
			if !ok {
				if ctx.Err() == nil {
					m.fail(fmt.Errorf("source %q event channel closed unexpectedly", child.Name))
				}
				return
			}
			normalized, err := normalizeEvent(child, m.kindsByName[child.Name], event)
			if err != nil {
				m.fail(err)
				return
			}
			select {
			case out <- normalized:
			case <-ctx.Done():
				return
			}
		}
	}
}

func (m *Mux) fail(err error) {
	if m.onError != nil {
		m.onError(err)
	}
	m.mu.Lock()
	cancel := m.cancel
	m.mu.Unlock()
	if cancel != nil {
		cancel()
	}
}

// normalizeEvent enforces Source namespacing and token ownership before an
// event reaches the Pipeline: the stream ID must live below the Source's
// namespace, the kind must be declared by that Source, and ack/end tokens
// must be owned by it and reference the same stream.
func normalizeEvent(child Child, kinds []api.RecordKind, event api.SourceEvent) (api.SourceEvent, error) {
	if !event.Valid() {
		return api.SourceEvent{}, fmt.Errorf("source %q emitted an invalid event", child.Name)
	}
	if event.Delivery != nil {
		delivery := *event.Delivery
		if !sourceOwnsStream(child.Name, delivery.StreamRef.ID) {
			return api.SourceEvent{}, fmt.Errorf("source %q emitted non-namespaced stream ID %q", child.Name, delivery.StreamRef.ID)
		}
		kind := delivery.StreamRef.Kind
		if !slices.Contains(kinds, kind) {
			return api.SourceEvent{}, fmt.Errorf("source %q emitted unsupported record kind %q", child.Name, kind)
		}
		if delivery.Record.Kind != kind {
			return api.SourceEvent{}, fmt.Errorf("source %q emitted record kind %q for stream kind %q", child.Name, delivery.Record.Kind, kind)
		}
		if delivery.AckToken.Source != child.Name {
			return api.SourceEvent{}, fmt.Errorf("source %q emitted ack token owned by %q", child.Name, delivery.AckToken.Source)
		}
		if delivery.AckToken.StreamRef != delivery.StreamRef {
			return api.SourceEvent{}, fmt.Errorf("source %q emitted an ack token for a different stream", child.Name)
		}
		return api.SourceEvent{Delivery: &delivery}, nil
	}

	end := *event.End
	if !sourceOwnsStream(child.Name, end.StreamRef.ID) {
		return api.SourceEvent{}, fmt.Errorf("source %q emitted non-namespaced stream ID %q", child.Name, end.StreamRef.ID)
	}
	kind := end.StreamRef.Kind
	if !slices.Contains(kinds, kind) {
		return api.SourceEvent{}, fmt.Errorf("source %q emitted unsupported stream kind %q", child.Name, kind)
	}
	if end.EndToken.Source != child.Name {
		return api.SourceEvent{}, fmt.Errorf("source %q emitted end token owned by %q", child.Name, end.EndToken.Source)
	}
	if end.EndToken.StreamRef != end.StreamRef {
		return api.SourceEvent{}, fmt.Errorf("source %q emitted an end token for a different stream", child.Name)
	}
	return api.SourceEvent{End: &end}, nil
}

func sourceOwnsStream(source, streamID string) bool {
	prefix, localID, found := strings.Cut(streamID, "/")
	return found && prefix == source && localID != ""
}

func stopChildren(ctx context.Context, children []Child) error {
	results := make(chan error, len(children))
	for _, child := range children {
		go func(child Child) { results <- child.Source.Stop(ctx) }(child)
	}
	var errs []error
	for range children {
		errs = append(errs, <-results)
	}
	return errors.Join(errs...)
}