Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

7.0 KB
Raw Blame
// Package container - workspace sandbox provider wiring.
package container

import (
	"context"
	"errors"
	"fmt"
	"os"
	"strings"

	"github.com/redis/go-redis/v9"
	"gorm.io/gorm"

	"github.com/Tencent/WeKnora/internal/application/service/sandboxalloc"
	apperrors "github.com/Tencent/WeKnora/internal/errors"
	"github.com/Tencent/WeKnora/internal/logger"
	"github.com/Tencent/WeKnora/internal/sandbox"
	"github.com/Tencent/WeKnora/internal/sandbox/admission"
	"github.com/Tencent/WeKnora/internal/sandbox/allocation"
	"github.com/Tencent/WeKnora/internal/types"
	"github.com/Tencent/WeKnora/internal/types/interfaces"
)

// newSandboxManager is deliberately disabled. Every executable backend now
// comes from a named workspace configuration resolved at request time.
func newSandboxManager(
	_ *redis.Client,
	_ interfaces.SessionRepository,
) sandbox.Manager {
	return sandbox.NewDisabledManager()
}

// sandboxRedisNamespace is the Redis key prefix shared by session bindings
// and user-machine digests. An empty env var means the historical default.
func sandboxRedisNamespace() string {
	namespace := strings.TrimSpace(os.Getenv("WEKNORA_REDIS_NAMESPACE"))
	if namespace == "" {
		namespace = "weknora"
	}
	return namespace
}

func selectSessionBindingStore(
	redisClient *redis.Client,
	requireRedis bool,
) (sandbox.SessionSandboxBindingStore, string, error) {
	namespace := sandboxRedisNamespace()
	requested := allocation.UserModeRequestedByEnv()
	if redisClient != nil {
		store, err := sandbox.NewRedisSessionSandboxBindingStore(redisClient, namespace)
		if err != nil {
			return nil, "", fmt.Errorf("build redis binding store: %w", err)
		}
		// User mode needs both the env opt-in and Redis; without Redis the
		// leases that coordinate one machine across replicas do not exist.
		allocation.SetUserModeAvailable(requested)
		if requested {
			logger.Infof(context.Background(), "[sandbox] user-mode allocation enabled (%s)", allocation.UserModeEnvVar)
		}
		return store, "redis", nil
	}
	allocation.SetUserModeAvailable(false)
	if requested {
		logger.Errorf(context.Background(),
			"[sandbox] %s is set but Redis is not configured; user-mode allocation stays disabled",
			allocation.UserModeEnvVar)
	}
	_ = requireRedis
	logger.Warnf(context.Background(),
		"[sandbox] No Redis configured, using in-memory binding store (single-instance)")
	return sandbox.NewMemorySessionSandboxBindingStore(), "memory", nil
}

// newSessionBindingStore selects the process-wide session→sandbox binding
// store. The tenant sandbox resolver and the background activity registry
// share it: the registry's Redis hash must use the binding store's client,
// namespace and hash tag so the turn-lease scripts can read it. A nil result
// disables per-tenant sandbox configs, as a failed selection always has.
func newSessionBindingStore(redisClient *redis.Client) sandbox.SessionSandboxBindingStore {
	ctx := context.Background()
	store, storeKind, err := selectSessionBindingStore(redisClient, true)
	if err != nil {
		logger.Warnf(ctx, "Per-tenant sandbox config disabled: %v", err)
		return nil
	}
	logger.Infof(ctx, "Session sandbox binding store: %s", storeKind)
	return store
}

// sessionExistenceLookup is the narrow slice of SessionRepository the
// session existence checker actually needs. Declaring it here (rather than
// depending on interfaces.SessionRepository) keeps the checker easy to test
// and lets the container inject a nil repository in Lite mode without
// dragging the whole database contract along.
type sessionExistenceLookup interface {
	GetByID(ctx context.Context, tenantID uint64, id string) (*types.Session, error)
}

// sessionExistenceCheckerFor returns a SessionExistenceChecker backed by the
// tenant session repository. When the repository is unavailable (Lite mode
// without a database) the returned checker is permissive so single-process
// deployments still work; multi-instance production paths always resolve a
// real repository because the container refuses to boot without one.
func sessionExistenceCheckerFor(
	lookup sessionExistenceLookup,
) sandbox.SessionExistenceChecker {
	if lookup == nil {
		return sandbox.PermissiveSessionExistenceChecker{}
	}
	return &repositorySessionExistenceChecker{lookup: lookup}
}

// repositorySessionExistenceChecker adapts SessionRepository.GetByID onto the
// SessionExistenceChecker contract. gorm.ErrRecordNotFound → false, other
// errors propagate so the lifecycle coordinator preserves bindings under
// transient database failures.
type repositorySessionExistenceChecker struct {
	lookup sessionExistenceLookup
}

func (c *repositorySessionExistenceChecker) SessionExists(
	ctx context.Context,
	key sandbox.SessionSandboxKey,
) (bool, error) {
	if c == nil || c.lookup == nil {
		return true, nil
	}
	session, err := c.lookup.GetByID(ctx, key.TenantID, key.SessionID)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) || errors.Is(err, apperrors.ErrSessionNotFound) {
			return false, nil
		}
		return false, fmt.Errorf("session existence check: %w", err)
	}
	return session != nil, nil
}

// buildGlobalSandboxConfig returns the process-wide *sandbox.Config that
// per-tenant overrides are merged onto.
func buildGlobalSandboxConfig() *sandbox.Config {
	cfg := sandbox.DefaultConfig()
	cfg.Type = sandbox.SandboxTypeDisabled
	return cfg
}

// newTenantSandboxResolver wires the workspace-config resolver. The
// process-wide manager is disabled; agents without a selected config stay
// disabled as well.
func newTenantSandboxResolver(
	defaultManager sandbox.Manager,
	loader sandbox.TenantSandboxConfigLoader,
	store sandbox.SessionSandboxBindingStore,
	sessionRepo interfaces.SessionRepository,
	bootstrapper sandbox.SessionBootstrapper,
	alloc *sandboxalloc.Service,
	reg admission.Registry,
) sandbox.TenantSandboxResolver {
	ctx := context.Background()

	// Tenants may configure any supported backend regardless of process startup
	// mode. Remote configs use this binding store for session persistence.
	if store == nil {
		logger.Warnf(ctx, "Per-tenant sandbox config disabled: no session binding store")
		return nil
	}
	resolver, err := sandbox.NewTenantSandboxResolver(sandbox.TenantSandboxResolverDeps{
		GlobalConfig:    buildGlobalSandboxConfig(),
		DefaultManager:  defaultManager,
		Loader:          loader,
		Store:           store,
		Checker:         sessionExistenceCheckerFor(sessionRepo),
		Bootstrapper:    bootstrapper,
		Allocations:     alloc,
		Admission:       reg,
		Namespace:       sandboxRedisNamespace(),
		SharedTransport: sandbox.NewGuardedTransport(),
	})
	if err != nil {
		logger.Warnf(ctx,
			"Failed to initialize tenant sandbox resolver: %v "+
				"(per-tenant sandbox config disabled)", err)
		return nil
	}
	logger.Infof(ctx, "Tenant sandbox resolver configured")
	return resolver
}

// newAdmissionRegistry shares Redis with the binding store; the key prefix
// matches the user binding keys so both live under one hash tag.
func newAdmissionRegistry(redisClient *redis.Client) admission.Registry {
	if redisClient == nil {
		return admission.NewMemory()
	}
	return admission.NewRedis(redisClient, "weknora:sandbox:user:")
}