138 lines
4.7 KiB
Go
138 lines
4.7 KiB
Go
package sandbox
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"strings"
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
)
|
|
|
|
// SkillImageFingerprint identifies the provider account a skill snapshot lives
|
|
// in. Snapshots are not visible across accounts, so when credentials change the
|
|
// stored snapshot silently stops existing for us - this fingerprint is how we
|
|
// notice and fall back instead of booting sessions against a dead image ID.
|
|
func SkillImageFingerprint(provider, apiKey, apiURL string) string {
|
|
sum := sha256.Sum256([]byte(strings.Join([]string{
|
|
strings.TrimSpace(provider),
|
|
strings.TrimSpace(apiKey),
|
|
strings.TrimSpace(apiURL),
|
|
}, "\n")))
|
|
return hex.EncodeToString(sum[:])
|
|
}
|
|
|
|
// SkillImageActive reports whether a config's stored snapshot is the image its
|
|
// sessions actually boot.
|
|
//
|
|
// It exists so the agent side can ask the same question the session side
|
|
// already answers, from the same inputs: a skill announced to the model while
|
|
// sessions keep booting the base template is a skill that cannot be invoked,
|
|
// and one hidden while the image carries it is an install nobody can use. It is
|
|
// false for backends that cannot snapshot at all (local/disabled), which is
|
|
// what "the base template is kept" means for them.
|
|
func SkillImageActive(tenantCfg *types.TenantSandboxConfig) bool {
|
|
if tenantCfg == nil {
|
|
return false
|
|
}
|
|
// The credentials are read from the stored provider block because
|
|
// ResolveEffectiveConfig clears the baseline's before applying it, so
|
|
// these are the ones the override is computed from there too.
|
|
//
|
|
// This switch is on the stored SandboxType alone, while
|
|
// ResolveEffectiveConfig falls back to the GLOBAL type when that field is
|
|
// empty - so in principle a config with no stored type could boot as e2b
|
|
// here and be judged "no image" there. The two agree today only because
|
|
// SkillOwnerFingerprint also returns "" for an empty SandboxType, and an
|
|
// empty OwnerFingerprint forces the override to "": such a config can
|
|
// never hold a usable image in the first place. That agreement is
|
|
// accidental. Keep the fingerprint guard, or make this switch take the
|
|
// global fallback too.
|
|
switch SandboxType(tenantCfg.SandboxType) {
|
|
case SandboxTypeCube:
|
|
if tenantCfg.Cube == nil {
|
|
return false
|
|
}
|
|
return skillImageTemplateOverride(
|
|
tenantCfg.SkillImage, "cube", tenantCfg.Cube.APIKey, tenantCfg.Cube.APIURL,
|
|
) != ""
|
|
case SandboxTypeE2B:
|
|
if tenantCfg.E2B == nil {
|
|
return false
|
|
}
|
|
return skillImageTemplateOverride(
|
|
tenantCfg.SkillImage, "e2b", tenantCfg.E2B.APIKey, tenantCfg.E2B.APIURL,
|
|
) != ""
|
|
case SandboxTypeDocker:
|
|
if tenantCfg.Docker == nil {
|
|
return false
|
|
}
|
|
host, tls := dockerSkillOwnerIdentity(tenantCfg.Docker)
|
|
return skillImageTemplateOverride(
|
|
tenantCfg.SkillImage, "docker", tls, host,
|
|
) != ""
|
|
}
|
|
return false
|
|
}
|
|
|
|
// SkillOwnerFingerprint is the provider-account identity stamped onto a
|
|
// skill snapshot. The install path refuses to record a snapshot when this is
|
|
// empty: a pointer with no owner is discarded at session start.
|
|
func SkillOwnerFingerprint(tenantCfg *types.TenantSandboxConfig) string {
|
|
if tenantCfg == nil {
|
|
return ""
|
|
}
|
|
switch SandboxType(tenantCfg.SandboxType) {
|
|
case SandboxTypeCube:
|
|
if tenantCfg.Cube != nil {
|
|
return SkillImageFingerprint("cube", tenantCfg.Cube.APIKey, tenantCfg.Cube.APIURL)
|
|
}
|
|
case SandboxTypeE2B:
|
|
if tenantCfg.E2B != nil {
|
|
return SkillImageFingerprint("e2b", tenantCfg.E2B.APIKey, tenantCfg.E2B.APIURL)
|
|
}
|
|
case SandboxTypeDocker:
|
|
return dockerSkillOwnerFingerprint(tenantCfg.Docker)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// dockerSkillOwnerIdentity is the daemon this config will actually dial.
|
|
// A blank host follows the local docker CLI, which is also how
|
|
// ResolveEffectiveConfig fills DockerHost, so the fingerprint computed here
|
|
// matches the override computed there.
|
|
func dockerSkillOwnerIdentity(docker *types.DockerSandboxConfig) (host, tls string) {
|
|
if docker == nil {
|
|
return "", ""
|
|
}
|
|
host = strings.TrimSpace(docker.Host)
|
|
if host == "" {
|
|
host = DetectLocalDockerHost()
|
|
}
|
|
return host, strings.TrimSpace(docker.TLSCertPath)
|
|
}
|
|
|
|
func dockerSkillOwnerFingerprint(docker *types.DockerSandboxConfig) string {
|
|
if docker == nil {
|
|
return ""
|
|
}
|
|
host, tls := dockerSkillOwnerIdentity(docker)
|
|
return SkillImageFingerprint("docker", tls, host)
|
|
}
|
|
|
|
// skillImageTemplateOverride returns the snapshot ID that should replace the
|
|
// base template, or "" when the base template must be kept.
|
|
func skillImageTemplateOverride(
|
|
image *types.SkillImageConfig, provider, apiKey, apiURL string,
|
|
) string {
|
|
if image == nil || strings.TrimSpace(image.SnapshotID) == "" {
|
|
return ""
|
|
}
|
|
if image.OwnerFingerprint == "" {
|
|
return ""
|
|
}
|
|
if image.OwnerFingerprint != SkillImageFingerprint(provider, apiKey, apiURL) {
|
|
return ""
|
|
}
|
|
return strings.TrimSpace(image.SnapshotID)
|
|
}
|