Skip to content

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

9.8 KB
Raw Blame
package sandbox

import (
	"errors"
	"fmt"
	"path"
	"strings"

	"github.com/Tencent/WeKnora/internal/sandbox/workspace"
)

const (
	// SkillsImageRoot is where installed skills live inside the sandbox image.
	// It is outside /workspace on purpose: /workspace is per-session scratch
	// and is wiped before every snapshot.
	SkillsImageRoot = workspace.SkillsImageRoot

	// SkillsManifestPath lists what the image claims to contain. It is a
	// troubleshooting aid, never the source of truth for execution.
	SkillsManifestPath = SkillsImageRoot + "/.manifest.json"
)

// HostSkillTargetID is the install target of Lite's local skills. It is a
// reserved literal rather than a sandbox_configs row: the host backend has no
// config to store, and a UUID can never collide with it.
const HostSkillTargetID = "host"

// IsHostSkillTarget reports whether configID names Lite's local skill target.
func IsHostSkillTarget(configID string) bool {
	return strings.TrimSpace(configID) == HostSkillTargetID
}

const skillShellArgv0 = "weknora-skill"

// ErrInvalidSkillName is returned when a skill name would escape SkillsImageRoot
// or is not a single path segment.
var ErrInvalidSkillName = errors.New("sandbox: invalid skill name")

// IsValidSkillName reports whether name is a single directory segment under
// SkillsImageRoot. Empty names, "." / "..", and anything containing a path
// separator are rejected so install/exec cannot walk out of the skills root.
func IsValidSkillName(name string) bool {
	name = strings.TrimSpace(name)
	if name == "" || name == "." || name == ".." {
		return false
	}
	if strings.ContainsAny(name, "/\\\x00") {
		return false
	}
	return path.Base(path.Clean(name)) == name
}

// SkillDirFor returns the image directory of a skill. The key is the skill
// name from SKILL.md: that is what the installer writes, and what the agent
// is told to execute. The database id stays a row key and is not part of
// the path.
func SkillDirFor(skillName string) (string, error) {
	return SkillDirUnder(SkillsImageRoot, skillName)
}

// SkillDirUnder joins one validated skill name under an absolute skills root.
func SkillDirUnder(root, skillName string) (string, error) {
	if !IsValidSkillName(skillName) {
		return "", fmt.Errorf("%w %q", ErrInvalidSkillName, skillName)
	}
	clean := path.Clean(strings.TrimSpace(root))
	if clean == "." || clean == "/" || !path.IsAbs(clean) {
		return "", fmt.Errorf("sandbox: invalid skills root %q", root)
	}
	return path.Join(clean, skillName), nil
}

// SkillRequirementsPath is where the installer agent writes what the skill
// needs at run time. It lives inside the skill's own directory so it travels
// with the skill into the snapshot, and it is a file rather than a tool call
// on purpose: the agent reads a third-party SKILL.md, so nothing it produces
// may reach the database except as bytes the server parses and validates.
//
// An invalid skill name yields an empty path, which every caller treats as
// "no declaration": the name is already validated before an install starts,
// and a best-effort read is not a place to fail an install.
func SkillRequirementsPath(skillName string) string {
	dir, err := SkillDirFor(skillName)
	if err != nil {
		return ""
	}
	return SkillRequirementsPathIn(dir)
}

// SkillRequirementsPathIn is the declaration file inside one skill directory.
func SkillRequirementsPathIn(skillDir string) string {
	if strings.TrimSpace(skillDir) == "" {
		return ""
	}
	return path.Join(skillDir, ".weknora", "requirements.json")
}

// RunnableWorkspaceScript reports whether scriptPath is a session-writable
// file under /workspace that may be executed with an installed skill's
// interpreter. See RunnableWorkspaceScriptIn for shared machines.
func RunnableWorkspaceScript(scriptPath string) (string, bool) {
	return workspace.RunnableScriptIn(RemoteWorkspaceLayout(), scriptPath)
}

// RunnableWorkspaceScriptIn is RunnableWorkspaceScript for an explicit layout.
func RunnableWorkspaceScriptIn(l WorkspaceLayout, scriptPath string) (string, bool) {
	return workspace.RunnableScriptIn(l, scriptPath)
}

// ValidatedSessionOutputDir normalises a configured artifact directory and
// reports whether it may be used.
//
// It is the single gate for every WEKNORA_SKILL_OUTPUT_DIR override, wherever
// it comes from: the host environment the app reads at startup, or a tenant's
// sandbox config. Without it the two disagreed — execution validated the path
// and fell back to SessionOutputRoot, while the tools and the artifact
// collector took the host value as-is. Keep this separate from shell working
// directories: access to a sandbox path does not make it a delivery directory.
//
// SessionWorkspaceRoot itself is refused. An artifact directory equal to the
// workspace root is not a delivery tree — it is the whole workspace, drafts
// included — and callers that compare the two (artifact collection) read
// that as "this backend collects nothing", silently dropping every artifact.
func ValidatedSessionOutputDir(dir string) (string, bool) {
	return workspace.ValidOutputDirIn(RemoteWorkspaceLayout(), dir)
}

// ValidatedOutputDirIn is ValidatedSessionOutputDir for an explicit layout.
func ValidatedOutputDirIn(l WorkspaceLayout, dir string) (string, bool) {
	return workspace.ValidOutputDirIn(l, dir)
}

// ValidatedImageSkillDir reports whether skillDir is exactly one installed
// skill directory under SkillsImageRoot (for example /opt/weknora/tenant/skills/pdf).
func ValidatedImageSkillDir(skillDir string) (string, bool) {
	return ValidatedSkillDirUnder(SkillsImageRoot, skillDir)
}

// ValidatedSkillDirUnder reports whether skillDir is exactly one skill
// directory directly under root.
func ValidatedSkillDirUnder(root, skillDir string) (string, bool) {
	clean := path.Clean(strings.TrimSpace(skillDir))
	expected, err := SkillDirUnder(root, path.Base(clean))
	if err != nil || expected != clean {
		return "", false
	}
	return clean, true
}

// InterpreterSkillDir chooses the skill directory whose venv/node_modules
// should run remote. Image-skill scripts always win from the path so a
// mismatched SkillDir cannot redirect them. Workspace scripts require an
// explicit, validated SkillDir.
func InterpreterSkillDir(remotePath, skillDir string) (string, bool) {
	if dir, ok := SkillDirForImageScript(remotePath); ok {
		return dir, true
	}
	if _, ok := RunnableWorkspaceScript(remotePath); !ok {
		return "", false
	}
	return ValidatedImageSkillDir(skillDir)
}

// SkillNameFromImagePath reports whether p sits at or under SkillsImageRoot.
// The skills root itself returns ("", true). A skill directory or a file
// inside one returns (skillName, true). Paths outside the image return
// ("", false).
func SkillNameFromImagePath(p string) (name string, inImage bool) {
	clean := path.Clean(strings.TrimSpace(p))
	root := path.Clean(SkillsImageRoot)
	if clean == root {
		return "", true
	}
	prefix := root + "/"
	if !strings.HasPrefix(clean, prefix) {
		return "", false
	}
	name = strings.SplitN(strings.TrimPrefix(clean, prefix), "/", 2)[0]
	if !IsValidSkillName(name) {
		return "", false
	}
	return name, true
}

// SkillDirForImageScript returns the owning skill directory for an image script.
// It anchors on SkillsImageRoot so nested script layouts still use the venv that
// was installed beside the skill, not a shallower scripts directory.
func SkillDirForImageScript(scriptPath string) (string, bool) {
	name, inImage := SkillNameFromImagePath(scriptPath)
	if !inImage || name == "" {
		return "", false
	}
	clean := path.Clean(strings.TrimSpace(scriptPath))
	dir, err := SkillDirFor(name)
	if err != nil || clean == dir {
		return "", false
	}
	return dir, true
}

// SkillVenvPython is where a skill's own Python interpreter lives when the
// install created one. It is exported because the model needs to be told: the
// system python3 deliberately carries no skill dependencies, so anything that
// inspects or debugs a Python skill has to name this path.
func SkillVenvPython(skillDir string) string {
	return path.Join(skillDir, ".venv", "bin", "python")
}

// SkillInterpreterCommand picks how to run one script of a skill.
//
// The interpreter is derived per script rather than stored per skill: one skill
// may ship both .py and .js entry points, so a single stored "interpreter"
// column could never be right for all of them.
//
// For Python we prefer the skill's own venv. The choice is made inside the
// sandbox with a shell conditional instead of an extra round trip to stat the
// path, because the extra Exec would double the latency of every skill call.
func SkillInterpreterCommand(skillDir, scriptPath string) (string, []string) {
	switch strings.ToLower(path.Ext(scriptPath)) {
	case ".py":
		venvPython := SkillVenvPython(skillDir)
		script := ShellQuote(scriptPath)
		return "/bin/sh", []string{"-c", fmt.Sprintf(
			`if [ -x %s ]; then exec %s %s "$@"; else exec python3 %s "$@"; fi`,
			ShellQuote(venvPython), ShellQuote(venvPython), script, script,
		), skillShellArgv0}
	case ".js", ".mjs", ".cjs":
		return "node", []string{scriptPath}
	case ".sh":
		// bash, with sh only as a fallback. Skill shell scripts carry a
		// `#!/bin/bash` shebang almost exclusively, and /bin/sh is dash on
		// Debian: an array literal, `function f()`, a C-style for loop and
		// process substitution are all syntax errors there, so running these
		// files with sh breaks scripts that are perfectly valid. The
		// install-time check parses them with the same shell.
		script := ShellQuote(scriptPath)
		return "/bin/sh", []string{"-c", fmt.Sprintf(
			`if command -v bash >/dev/null 2>&1; then exec bash %s "$@"; else exec sh %s "$@"; fi`,
			script, script,
		), skillShellArgv0}
	default:
		return "/bin/sh", []string{scriptPath}
	}
}

// SkillCommandPath is shared by normal skill execution and installation verification.
func SkillCommandPath(dir string) string {
	return path.Join(dir, ".venv", "bin") + ":" +
		path.Join(dir, "node_modules", ".bin") + ":" + path.Join(dir, ".weknora", "bin")
}