Skip to content

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

fix(sandbox): harden tenant skill install against races and credential rotation

wizardchen03755f11f7f8
diff --git a/internal/agent/tools/shell_exec.go b/internal/agent/tools/shell_exec.go
index dfba8d7e9..c24ddb32e 100644
--- a/internal/agent/tools/shell_exec.go
+++ b/internal/agent/tools/shell_exec.go
@@ -253,6 +253,10 @@ type ShellExecTool struct {
// workDirRoots are the directories work_dir may point inside. Ordinary
// sessions get /workspace only; install mode adds the skills image root.
workDirRoots []string
+ // defaultTimeout is applied when the caller omits timeout_sec. Ordinary
+ // sessions keep the 120s default; install mode uses the 10-minute cap
+ // because dependency installs routinely exceed two minutes.
+ defaultTimeout time.Duration
}
// NewShellExecTool constructs the tool. `executor` MUST NOT be nil:
@@ -271,9 +275,10 @@ func NewShellExecTool(executor SandboxCommandExecutor) *ShellExecTool {
// the built-in skill installer agent (see AgentConfig.SkillInstallMode).
func NewInstallShellExecTool(executor SandboxInstallCommandExecutor) *ShellExecTool {
return &ShellExecTool{
- BaseTool: shellExecTool,
- executor: installShellExecutor{inner: executor},
- workDirRoots: []string{defaultShellExecWorkDir, sandbox.SkillsImageRoot},
+ BaseTool: shellExecTool,
+ executor: installShellExecutor{inner: executor},
+ workDirRoots: []string{defaultShellExecWorkDir, sandbox.SkillsImageRoot},
+ defaultTimeout: shellExecMaxTimeout,
}
}
@@ -347,7 +352,10 @@ func (t *ShellExecTool) Execute(ctx context.Context, args json.RawMessage) (*typ
}
workDir = cleanWorkDir
- timeout := defaultShellExecTimeout
+ timeout := t.defaultTimeout
+ if timeout <= 0 {
+ timeout = defaultShellExecTimeout
+ }
if input.TimeoutSec > 0 {
timeout = time.Duration(input.TimeoutSec) * time.Second
}
diff --git a/internal/agent/tools/shell_exec_test.go b/internal/agent/tools/shell_exec_test.go
index 3dd69033e..dc4fd8a87 100644
--- a/internal/agent/tools/shell_exec_test.go
+++ b/internal/agent/tools/shell_exec_test.go
@@ -68,6 +68,31 @@ func TestShellExecTimeoutHonorsAndCapsRequestedValue(t *testing.T) {
assert.Equal(t, shellExecMaxTimeout, executor.timeout)
}
+type fakeInstallShellExecutor struct {
+ timeout time.Duration
+}
+
+func (f *fakeInstallShellExecutor) ExecShellCommandWithOptions(
+ _ context.Context, _ string, _ string, opts sandbox.ShellExecOptions,
+) (*sandbox.ExecuteResult, error) {
+ f.timeout = opts.Timeout
+ return &sandbox.ExecuteResult{ExitCode: 0}, nil
+}
+
+func TestInstallShellExecDefaultsToTheTenMinuteBudget(t *testing.T) {
+ inner := &fakeInstallShellExecutor{}
+ tool := NewInstallShellExecTool(inner)
+
+ result, err := tool.Execute(shellExecTestContext(), json.RawMessage(
+ `{"command":"pip install -r requirements.txt"}`,
+ ))
+
+ require.NoError(t, err)
+ require.True(t, result.Success)
+ assert.Equal(t, shellExecMaxTimeout, inner.timeout,
+ "install-mode shell_exec must not keep the ordinary 120s default")
+}
+
func TestShellExecConfigurableOutputAndRegistryOverride(t *testing.T) {
content := strings.Repeat("x", 24*1024)
executor := &fakeShellExecutor{result: &sandbox.ExecuteResult{
diff --git a/internal/application/service/tenant_skill_install.go b/internal/application/service/tenant_skill_install.go
index e92202ce8..e4cf8638d 100644
--- a/internal/application/service/tenant_skill_install.go
+++ b/internal/application/service/tenant_skill_install.go
@@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"path"
+ "sort"
"strings"
"time"
@@ -97,7 +98,7 @@ func (s *TenantSkillService) InstallSkill(
logger.Warnf(ctx, "[skill] store bundle for %s failed: %v", skillID, err)
}
- s.publishProgress(ctx, configID, skillID, SkillProgress{
+ s.publishProgress(ctx, tenantID, configID, skillID, SkillProgress{
Percent: 10, Stage: "accepted", Status: types.SkillStatusInstalling,
})
@@ -122,15 +123,6 @@ func (s *TenantSkillService) runInstall(
ctx = context.WithValue(ctx, types.TenantIDContextKey, tenantID)
ctx = types.WithSandboxTenantID(ctx, tenantID)
- // The name comes from SKILL.md and is already validated on parse, so a
- // rejection here means the bundle was accepted by a looser rule than the
- // one the image path enforces. Failing before any sandbox work keeps that
- // disagreement cheap.
- skillDir, err := sandbox.SkillDirFor(bundle.Name)
- if err != nil {
- return err
- }
-
// Cleanup runs on a context that cannot be cancelled by whatever it is
// compensating for. withConfigLock cancels ctx the moment lock renewal
// fails, and the two things that must still happen then are a provider
@@ -166,6 +158,26 @@ func (s *TenantSkillService) runInstall(
s.failSkill(failCtx, tenantID, configID, skillID, err)
}()
+ // InstallSkill queues this run before the per-config lock. A remove that
+ // won the lock (or a newer upload of the same name) already owns the row;
+ // snapshotting anyway would bake a skill the ledger no longer names.
+ owned, err := s.installStillOwnsTheRow(ctx, tenantID, configID, skillID, bundle)
+ if err != nil {
+ return err
+ }
+ if !owned {
+ return nil
+ }
+
+ // The name comes from SKILL.md and is already validated on parse, so a
+ // rejection here means the bundle was accepted by a looser rule than the
+ // one the image path enforces. Failing before any sandbox work keeps that
+ // disagreement cheap.
+ skillDir, err := sandbox.SkillDirFor(bundle.Name)
+ if err != nil {
+ return err
+ }
+
cfgEntity, err := s.configs.GetByID(ctx, tenantID, configID)
if err != nil {
return fmt.Errorf("load sandbox config %s: %w", configID, err)
@@ -176,6 +188,17 @@ func (s *TenantSkillService) runInstall(
if err := ensureUsableImage(cfgEntity); err != nil {
return err
}
+ // Snapshots are private to the provider account that created them. The
+ // fingerprint is captured here, from the credentials this sandbox will
+ // actually use, and switchImagePointer refuses to stamp a later rotation
+ // onto that ID — that would make the session layer trust a snapshot the
+ // live key cannot resolve.
+ builtFingerprint := skillOwnerFingerprint(cfgEntity.Config)
+ if strings.TrimSpace(builtFingerprint) == "" {
+ return fmt.Errorf(
+ "sandbox config %s has no usable owner fingerprint for a skill image", configID,
+ )
+ }
// 1. Install session + sandbox. ResolveEffectiveConfig has already turned
// the current snapshot into the template, so this sandbox boots from the
@@ -192,7 +215,7 @@ func (s *TenantSkillService) runInstall(
s.releaseSandbox(releaseCtx, mgr, sess.ID)
}()
- s.publishProgress(ctx, configID, skillID, SkillProgress{Percent: 25, Stage: "sandbox_ready"})
+ s.publishProgress(ctx, tenantID, configID, skillID, SkillProgress{Percent: 25, Stage: "sandbox_ready"})
// 2. Provision the target directory, then seed the source files
// server-side. The agent only installs dependencies; it never has to
@@ -203,13 +226,13 @@ func (s *TenantSkillService) runInstall(
if err := s.seedSkillFiles(ctx, mgr, sess.ID, skillDir, bundle); err != nil {
return err
}
- s.publishProgress(ctx, configID, skillID, SkillProgress{Percent: 35, Stage: "seeded"})
+ s.publishProgress(ctx, tenantID, configID, skillID, SkillProgress{Percent: 35, Stage: "seeded"})
// 3. Let the installer agent install dependencies.
if err := s.driveInstallerAgent(ctx, tenantID, sess, mgr, skillDir, bundle); err != nil {
return err
}
- s.publishProgress(ctx, configID, skillID, SkillProgress{Percent: 80, Stage: "agent_done"})
+ s.publishProgress(ctx, tenantID, configID, skillID, SkillProgress{Percent: 80, Stage: "agent_done"})
// 4. Hand the tree to the execution user BEFORE verifying it. The agent
// created these files as root, and the smoke run below deliberately
@@ -228,7 +251,7 @@ func (s *TenantSkillService) runInstall(
if err := s.writeManifestEntry(ctx, mgr, sess.ID, skillID, bundle); err != nil {
return err
}
- s.publishProgress(ctx, configID, skillID, SkillProgress{Percent: 90, Stage: "verified"})
+ s.publishProgress(ctx, tenantID, configID, skillID, SkillProgress{Percent: 90, Stage: "verified"})
// 6. Wipe the scratch state. It must happen BEFORE the snapshot, or the
// per-session workspace and every package cache land in the image.
@@ -279,7 +302,7 @@ func (s *TenantSkillService) runInstall(
}
// 8. Switch the pointer. One DB write; everything after this is cleanup.
- if err := s.switchImagePointer(ctx, tenantID, configID, ref.ID, generation); err != nil {
+ if err := s.switchImagePointer(ctx, tenantID, configID, ref.ID, generation, builtFingerprint); err != nil {
s.abandonSnapshot(cleanupBase, tenantID, mgr, installRowID, ref.ID)
return err
}
@@ -295,7 +318,7 @@ func (s *TenantSkillService) runInstall(
return err
}
s.markConfigSandboxesStale(ctx, tenantID, configID)
- s.publishProgress(ctx, configID, skillID, SkillProgress{
+ s.publishProgress(ctx, tenantID, configID, skillID, SkillProgress{
Percent: 100, Stage: "done", Status: types.SkillStatusReady,
})
return nil
@@ -493,6 +516,9 @@ func (s *TenantSkillService) verifySkill(
return fmt.Errorf("script %s is missing after install: %w", rel, err)
}
}
+ if err := s.verifyDeclaredDependencies(ctx, mgr, sessionID, skillDir, bundle); err != nil {
+ return err
+ }
if entry := primaryEntryScript(bundle); entry != "" {
res, err := s.execSmoke(ctx, mgr, sessionID, skillDir, path.Join(skillDir, entry))
if err != nil {
@@ -505,6 +531,29 @@ func (s *TenantSkillService) verifySkill(
return nil
}
+// verifyDeclaredDependencies checks that the isolated trees the installer was
+// told to create actually exist. Seeded source files surviving is not evidence
+// that pip/npm ran: those files were written server-side before the agent.
+func (s *TenantSkillService) verifyDeclaredDependencies(
+ ctx context.Context, mgr sandbox.Manager, sessionID, skillDir string, bundle *SkillBundle,
+) error {
+ if bundleHasPythonDeps(bundle) {
+ venvPython := path.Join(skillDir, ".venv", "bin", "python")
+ if _, err := s.execInstall(ctx, mgr, sessionID,
+ fmt.Sprintf("test -x %s", sandbox.ShellQuote(venvPython))); err != nil {
+ return fmt.Errorf("python dependencies were not installed into %s/.venv: %w", skillDir, err)
+ }
+ }
+ if bundleHasNodeDeps(bundle) {
+ nodeModules := path.Join(skillDir, "node_modules")
+ if _, err := s.execInstall(ctx, mgr, sessionID,
+ fmt.Sprintf("test -d %s", sandbox.ShellQuote(nodeModules))); err != nil {
+ return fmt.Errorf("node dependencies were not installed into %s/node_modules: %w", skillDir, err)
+ }
+ }
+ return nil
+}
+
// normalizeSkillPermissions hands the skill tree to the execution user.
// Installs run as root, so without this the non-root user that actually runs
// skills could not read them — which is also why it runs before verification:
@@ -705,11 +754,36 @@ func (s *TenantSkillService) failSkill(
e.Error = cause.Error()
e.InstallingSince = nil
})
- s.publishProgress(ctx, configID, skillID, SkillProgress{
+ s.publishProgress(ctx, tenantID, configID, skillID, SkillProgress{
Percent: 100, Stage: "failed", Status: types.SkillStatusFailed, Log: cause.Error(),
})
}
+// installStillOwnsTheRow is the lock-side counterpart of InstallSkill's
+// optimistic row write. A remove that ran first deleted the row; a newer
+// upload of the same name replaced BundleSHA256; a queued remove flipped the
+// status. Any of those means this run must not snapshot — failSkill would
+// stamp the newer owner's row, and a snapshot with no matching row is an
+// orphan the ledger cannot name.
+func (s *TenantSkillService) installStillOwnsTheRow(
+ ctx context.Context, tenantID uint64, configID, skillID string, bundle *SkillBundle,
+) (bool, error) {
+ current, err := s.skills.GetSkill(ctx, tenantID, configID, skillID)
+ if err != nil {
+ return false, fmt.Errorf("load skill %s: %w", skillID, err)
+ }
+ if current == nil {
+ return false, nil
+ }
+ if current.Status == types.SkillStatusRemoving {
+ return false, nil
+ }
+ if bundle != nil && current.BundleSHA256 != "" && current.BundleSHA256 != bundle.SHA256 {
+ return false, nil
+ }
+ return true, nil
+}
+
// startMaintenanceSession opens the session one image operation runs in. The
// operation name is carried into the session because the transcript is kept
// deliberately, for troubleshooting: filing a removal's under "Skill install"
@@ -720,6 +794,20 @@ func (s *TenantSkillService) startMaintenanceSession(
if s.sessions == nil {
return nil, nil, errors.New("session service is not configured")
}
+ // Honour the workspace kill switch before creating a billed sandbox or a
+ // session row. resolveTenantSandboxForConfig is the same choke point every
+ // other sandbox caller uses; going through TenantSandboxResolver.Resolve
+ // directly would let an install run while scripts are disabled.
+ mgr, err := resolveTenantSandboxForConfig(ctx, s.sandboxes, nil, tenantID, configID, s.sandboxPolicy)
+ if err != nil {
+ return nil, nil, fmt.Errorf("resolve sandbox config: %w", err)
+ }
+ if mgr == nil {
+ return nil, nil, errors.New("sandbox resolver returned nil manager")
+ }
+ if mgr.GetType() == sandbox.SandboxTypeDisabled {
+ return nil, nil, errors.New("sandbox execution is disabled for this workspace")
+ }
sess, err := s.sessions.CreateSession(ctx, &types.Session{
TenantID: tenantID,
Title: "Skill " + operation,
@@ -732,16 +820,6 @@ func (s *TenantSkillService) startMaintenanceSession(
if sess == nil {
return nil, nil, fmt.Errorf("create %s session returned nil", operation)
}
- if s.sandboxes == nil {
- return nil, nil, errors.New("sandbox resolver is not configured")
- }
- mgr, err := s.sandboxes.Resolve(ctx, tenantID, configID)
- if err != nil {
- return nil, nil, fmt.Errorf("resolve sandbox config: %w", err)
- }
- if mgr == nil {
- return nil, nil, errors.New("sandbox resolver returned nil manager")
- }
return sess, mgr, nil
}
@@ -792,16 +870,21 @@ func (s *TenantSkillService) deleteSnapshotBestEffort(
// repository Update overwrites name, description, sandbox type and the entire
// config blob, and config edits are serialised by the config service's cordon
// rather than by this task's lock — so writing back the stale entity would
-// silently revert an admin's credential rotation. It would also compute the
-// fingerprint from the stale credentials, and a pointer whose fingerprint does
-// not match the live ones is discarded at session start: the install would
-// report success while every session kept booting the base template.
+// silently revert an admin's rename or credential rotation.
+//
+// builtFingerprint is the account the maintenance sandbox (and therefore the
+// snapshot) actually belongs to. A live fingerprint that no longer matches
+// means the credentials rotated while this run was in flight: stamping the
+// new fingerprint onto the old snapshot would make the session layer trust an
+// ID the live key cannot resolve. Abort instead, and leave the previous image
+// in place.
func (s *TenantSkillService) switchImagePointer(
ctx context.Context,
tenantID uint64,
configID string,
snapshotID string,
generation int,
+ builtFingerprint string,
) error {
cfgEntity, err := s.configs.GetByID(ctx, tenantID, configID)
if err != nil {
@@ -811,15 +894,19 @@ func (s *TenantSkillService) switchImagePointer(
return fmt.Errorf("sandbox config %s disappeared during the install", configID)
}
- fingerprint := skillOwnerFingerprint(cfgEntity.Config)
- if strings.TrimSpace(fingerprint) == "" {
- // An empty fingerprint is treated as "keep the base template" when a
- // session resolves the image, so persisting one would mark the skill
- // ready while no sandbox ever sees it.
+ if strings.TrimSpace(builtFingerprint) == "" {
return fmt.Errorf(
"sandbox config %s has no usable owner fingerprint for a skill image", configID,
)
}
+ live := skillOwnerFingerprint(cfgEntity.Config)
+ if live != builtFingerprint {
+ return fmt.Errorf(
+ "sandbox config %s credentials changed during the image build; "+
+ "the snapshot belongs to the previous provider account",
+ configID,
+ )
+ }
// Only the SkillImage portion is touched; everything else in the entity is
// whatever the latest read says it is.
@@ -828,7 +915,7 @@ func (s *TenantSkillService) switchImagePointer(
Generation: generation,
BuiltAt: s.now(),
BaseTemplateID: effectiveBaseTemplate(cfgEntity),
- OwnerFingerprint: fingerprint,
+ OwnerFingerprint: builtFingerprint,
}
cfgEntity.TenantID = tenantID
return s.configs.Update(ctx, cfgEntity)
@@ -908,6 +995,7 @@ Hard requirements:
- Python dependencies must go into %s/.venv. Do not install into system Python.
- Node dependencies must go under %s/node_modules. Do not install global packages unless no local alternative exists.
- Use shell_exec only. You may set work_dir to %s.
+- Each command has a 10-minute budget; you do not need to set timeout_sec.
- When finished, report what you installed and any global/system packages you changed.
SKILL.md:
@@ -921,15 +1009,37 @@ func primaryEntryScript(bundle *SkillBundle) string {
}
preferred := []string{".py", ".js", ".mjs", ".sh"}
for _, suffix := range preferred {
+ var matches []string
for rel := range bundle.Files {
if strings.HasPrefix(rel, "scripts/") && strings.HasSuffix(rel, suffix) {
- return rel
+ matches = append(matches, rel)
}
}
+ if len(matches) > 0 {
+ sort.Strings(matches)
+ return matches[0]
+ }
}
return ""
}
+func bundleHasPythonDeps(bundle *SkillBundle) bool {
+ if bundle == nil {
+ return false
+ }
+ _, req := bundle.Files["requirements.txt"]
+ _, pyproject := bundle.Files["pyproject.toml"]
+ return req || pyproject
+}
+
+func bundleHasNodeDeps(bundle *SkillBundle) bool {
+ if bundle == nil {
+ return false
+ }
+ _, ok := bundle.Files["package.json"]
+ return ok
+}
+
func (s *TenantSkillService) probeUv(ctx context.Context, mgr sandbox.Manager, sessionID string) bool {
_, err := s.execInstall(ctx, mgr, sessionID, "uv --version")
return err == nil
diff --git a/internal/application/service/tenant_skill_install_test.go b/internal/application/service/tenant_skill_install_test.go
index 8d371ab49..33d30674c 100644
--- a/internal/application/service/tenant_skill_install_test.go
+++ b/internal/application/service/tenant_skill_install_test.go
@@ -189,25 +189,117 @@ func TestRunInstallSupersedesThePreviousLedgerRowWithoutDeletingIt(t *testing.T)
"the base template is recorded once and never re-derived from a snapshot")
}
-func TestSwitchImagePointerKeepsAConcurrentConfigEdit(t *testing.T) {
+func TestSwitchImagePointerKeepsAConcurrentNameEdit(t *testing.T) {
fx := newInstallFixture(t)
- // An admin rotates the provider credential and renames the config while
- // the (minutes-long) install is running. Config edits are serialised by
- // the config service's own cordon, not by the skill image lock.
+ // Config edits are serialised by the config service's own cordon, not by
+ // the skill image lock, so a rename can land while this run is still
+ // driving the agent. The pointer switch must re-read and keep that name.
fx.configRepo.editAfterFirstRead = func(e *types.TenantSandboxConfigEntity) {
e.Name = "renamed"
- e.Config.E2B.APIKey = "key-2"
}
require.NoError(t, fx.svc.runInstall(context.Background(), 7, "cfg-1", "sk-1", fx.bundle))
saved := fx.configRepo.saved
require.Equal(t, "renamed", saved.Name, "the pointer switch must not revert a config edit")
- require.Equal(t, "key-2", saved.Config.E2B.APIKey)
- require.Equal(t,
- sandbox.SkillImageFingerprint("e2b", "key-2", "https://e2b.example"),
- saved.Config.SkillImage.OwnerFingerprint,
- "a fingerprint taken from the stale key would make every session ignore the snapshot")
+ require.Equal(t, fx.fingerprint, saved.Config.SkillImage.OwnerFingerprint,
+ "the snapshot was built under the original credentials")
+}
+
+func TestSwitchImagePointerAbandonsSnapshotWhenCredentialsRotate(t *testing.T) {
+ fx := newInstallFixture(t)
+ // Rotating the API key mid-install does not move the already-created
+ // snapshot to the new account. Stamping the new fingerprint onto that ID
+ // would make every session trust a snapshot the live key cannot resolve.
+ fx.configRepo.editAfterFirstRead = func(e *types.TenantSandboxConfigEntity) {
+ e.Config.E2B.APIKey = "key-2"
+ }
+
+ err := fx.svc.runInstall(context.Background(), 7, "cfg-1", "sk-1", fx.bundle)
+
+ require.Error(t, err)
+ require.ErrorContains(t, err, "credentials changed")
+ require.Nil(t, fx.configRepo.saved, "an unresolvable pointer must never be persisted")
+ require.Contains(t, fx.deletedSnapshots, "snap-1")
+
+ skill, _ := fx.skillRepo.GetSkill(context.Background(), 7, "cfg-1", "sk-1")
+ require.Equal(t, types.SkillStatusFailed, skill.Status)
+}
+
+func TestRunInstallAbortsWhenTheSkillRowWasRemoved(t *testing.T) {
+ fx := newInstallFixture(t)
+ require.NoError(t, fx.skillRepo.DeleteSkill(context.Background(), 7, "cfg-1", "sk-1"))
+
+ require.NoError(t, fx.svc.runInstall(context.Background(), 7, "cfg-1", "sk-1", fx.bundle))
+
+ require.NotContains(t, fx.events, "create-snapshot",
+ "a queued install must not bake a skill whose row a remove already deleted")
+ require.Nil(t, fx.configRepo.saved)
+ skill, err := fx.skillRepo.GetSkill(context.Background(), 7, "cfg-1", "sk-1")
+ require.NoError(t, err)
+ require.Nil(t, skill)
+}
+
+func TestRunInstallAbortsWhenANewerBundleOwnsTheRow(t *testing.T) {
+ fx := newInstallFixture(t)
+ newer := strings.Repeat("b", 64)
+ require.NoError(t, fx.skillRepo.UpdateSkill(context.Background(), &types.TenantSkillEntity{
+ ID: "sk-1", TenantID: 7, SandboxConfigID: "cfg-1",
+ Name: fx.bundle.Name, BundleSHA256: newer,
+ Status: types.SkillStatusInstalling,
+ }))
+
+ require.NoError(t, fx.svc.runInstall(context.Background(), 7, "cfg-1", "sk-1", fx.bundle))
+
+ require.NotContains(t, fx.events, "create-snapshot")
+ skill, err := fx.skillRepo.GetSkill(context.Background(), 7, "cfg-1", "sk-1")
+ require.NoError(t, err)
+ require.Equal(t, newer, skill.BundleSHA256)
+ require.Equal(t, types.SkillStatusInstalling, skill.Status,
+ "failing this run must not stamp the newer owner's row")
+}
+
+func TestRunInstallRefusesWhenWorkspaceScriptsAreDisabled(t *testing.T) {
+ fx := newInstallFixture(t)
+ fx.svc.sandboxPolicy = stubWorkspaceSandboxPolicy{disabled: true}
+
+ err := fx.svc.runInstall(context.Background(), 7, "cfg-1", "sk-1", fx.bundle)
+
+ require.Error(t, err)
+ require.ErrorContains(t, err, "disabled")
+ require.Empty(t, fx.sessionCalls, "the kill switch must fire before a billed session is created")
+ require.Nil(t, fx.configRepo.saved)
+}
+
+func TestRunInstallRequiresVenvWhenRequirementsExist(t *testing.T) {
+ fx := newInstallFixture(t)
+ fx.bundle.Files["requirements.txt"] = []byte("pypdf==4.0.0\n")
+ fx.depsExitCode = 1
+
+ err := fx.svc.runInstall(context.Background(), 7, "cfg-1", "sk-1", fx.bundle)
+
+ require.Error(t, err)
+ require.ErrorContains(t, err, ".venv")
+ require.NotContains(t, fx.events, "create-snapshot")
+ require.Nil(t, fx.configRepo.saved)
+}
+
+func TestPrimaryEntryScriptIsDeterministic(t *testing.T) {
+ bundle := &SkillBundle{Files: map[string][]byte{
+ "scripts/z-helper.py": []byte("x"),
+ "scripts/a-main.py": []byte("x"),
+ "scripts/mid.js": []byte("x"),
+ }}
+ require.Equal(t, "scripts/a-main.py", primaryEntryScript(bundle),
+ "python is preferred over js, and the name is sorted so the pick is stable")
+}
+
+type stubWorkspaceSandboxPolicy struct {
+ disabled bool
+}
+
+func (s stubWorkspaceSandboxPolicy) WorkspaceScriptsDisabled(context.Context, uint64) (bool, error) {
+ return s.disabled, nil
}
func TestSwitchImagePointerRefusesAnUnusableFingerprint(t *testing.T) {
@@ -223,7 +315,9 @@ func TestSwitchImagePointerRefusesAnUnusableFingerprint(t *testing.T) {
require.Error(t, err)
require.ErrorContains(t, err, "fingerprint")
require.Nil(t, fx.configRepo.saved, "an ineffective pointer must never be persisted")
- require.Contains(t, fx.deletedSnapshots, "snap-1")
+ require.NotContains(t, fx.events, "create-snapshot",
+ "a config that cannot own a skill image must not spend a billed snapshot")
+ require.Empty(t, fx.deletedSnapshots)
}
// TestRunInstallStopsWhenTheLockIsLost also covers the cleanup that has to
@@ -242,8 +336,8 @@ func TestRunInstallStopsWhenTheLockIsLost(t *testing.T) {
require.NotContains(t, fx.events, "create-snapshot",
"losing the lock means another install may already be writing; do not snapshot")
require.Nil(t, fx.configRepo.saved)
- require.Equal(t, []string{"sess-1"}, fx.destroyedSandboxes,
- "a sandbox left running on the provider is billed until its TTL expires")
+ require.Empty(t, fx.destroyedSandboxes,
+ "the lock was already gone before a sandbox was created")
skill, err := fx.skillRepo.GetSkill(context.Background(), 7, "cfg-1", "sk-1")
require.NoError(t, err)
@@ -552,6 +646,8 @@ type installFixture struct {
fingerprint string
smokeExitCode int
smokeResult *sandbox.ExecuteResult
+ // depsExitCode fails the declared-dependency check (venv / node_modules).
+ depsExitCode int
// execResult is scoped to execResultCommand: an unscoped stub result
// applies to the first command issued, which is not the command any of
// these tests is about.
@@ -646,6 +742,7 @@ func newInstallFixture(t *testing.T) *installFixture {
fx.configRepo,
&installStorageResolver{fx: fx},
&installSandboxResolver{mgr: fx.sandboxMgr},
+ nil,
fx.agentSvc,
&installCustomAgentService{fx: fx},
&installSessionService{fx: fx},
@@ -1067,6 +1164,11 @@ func (m *installSandboxManager) ExecShellCommandWithOptions(
m.fx.record("verify-structure")
m.structureSeen = true
}
+ case strings.HasPrefix(command, "test -x ") || strings.HasPrefix(command, "test -d "):
+ m.fx.record("verify-deps")
+ if m.fx.depsExitCode != 0 {
+ return &sandbox.ExecuteResult{ExitCode: m.fx.depsExitCode, Stderr: "deps missing"}, nil
+ }
case command == installSmokeCommand:
m.fx.smokeRanAsRoot = opts.AsRoot
m.fx.record("verify-smoke")
diff --git a/internal/application/service/tenant_skill_progress.go b/internal/application/service/tenant_skill_progress.go
index 9c5089e05..726b57085 100644
--- a/internal/application/service/tenant_skill_progress.go
+++ b/internal/application/service/tenant_skill_progress.go
@@ -23,15 +23,15 @@ type SkillProgress struct {
Status string `json:"status,omitempty"`
}
-func skillProgressKey(configID, skillID string) string {
- return fmt.Sprintf("weknora-skill-install:%s:%s", configID, skillID)
+func skillProgressKey(tenantID uint64, configID, skillID string) string {
+ return fmt.Sprintf("weknora-skill-install:%d:%s:%s", tenantID, configID, skillID)
}
// publishProgress stores the latest value and broadcasts it. Without Redis both
// are no-ops and the UI falls back to the DB status alone; the install itself
// keeps working, which is the point.
func (s *TenantSkillService) publishProgress(
- ctx context.Context, configID, skillID string, p SkillProgress,
+ ctx context.Context, tenantID uint64, configID, skillID string, p SkillProgress,
) {
if s.redis == nil {
return
@@ -40,7 +40,7 @@ func (s *TenantSkillService) publishProgress(
if err != nil {
return
}
- key := skillProgressKey(configID, skillID)
+ key := skillProgressKey(tenantID, configID, skillID)
if err := s.redis.Set(ctx, key, payload, skillProgressTTL).Err(); err != nil {
logger.Warnf(ctx, "[skill] store progress %s failed: %v", key, err)
}
@@ -50,14 +50,16 @@ func (s *TenantSkillService) publishProgress(
}
// LastProgress returns the last known value so a fresh SSE connection can paint
-// immediately instead of waiting for the next tick.
+// immediately instead of waiting for the next tick. tenantID is part of the
+// key so a caller that only has config/skill IDs cannot read another
+// workspace's progress.
func (s *TenantSkillService) LastProgress(
- ctx context.Context, configID, skillID string,
+ ctx context.Context, tenantID uint64, configID, skillID string,
) (SkillProgress, bool) {
if s.redis == nil {
return SkillProgress{}, false
}
- raw, err := s.redis.Get(ctx, skillProgressKey(configID, skillID)).Bytes()
+ raw, err := s.redis.Get(ctx, skillProgressKey(tenantID, configID, skillID)).Bytes()
if err != nil {
return SkillProgress{}, false
}
diff --git a/internal/application/service/tenant_skill_remove.go b/internal/application/service/tenant_skill_remove.go
index f033b06b2..6c6377d3a 100644
--- a/internal/application/service/tenant_skill_remove.go
+++ b/internal/application/service/tenant_skill_remove.go
@@ -37,7 +37,7 @@ func (s *TenantSkillService) RemoveSkill(
if err := s.skills.UpdateSkill(ctx, skill); err != nil {
return err
}
- s.publishProgress(ctx, configID, skillID, SkillProgress{
+ s.publishProgress(ctx, tenantID, configID, skillID, SkillProgress{
Percent: 5, Stage: "accepted", Status: types.SkillStatusRemoving,
})
@@ -137,6 +137,7 @@ func (s *TenantSkillService) runRemove(
if err := ensureUsableImage(cfgEntity); err != nil {
return err
}
+ builtFingerprint := skillOwnerFingerprint(cfgEntity.Config)
// Everything below either creates provider resources or moves the image
// pointer, and the fallback moves it without taking a snapshot first. A
@@ -180,7 +181,7 @@ func (s *TenantSkillService) runRemove(
defer cancelRelease()
s.releaseSandbox(releaseCtx, mgr, sess.ID)
}()
- s.publishProgress(ctx, configID, skillID, SkillProgress{Percent: 30, Stage: "sandbox_ready"})
+ s.publishProgress(ctx, tenantID, configID, skillID, SkillProgress{Percent: 30, Stage: "sandbox_ready"})
if err := s.removeSkillDirectory(ctx, mgr, sess.ID, skillDir); err != nil {
return err
@@ -193,7 +194,7 @@ func (s *TenantSkillService) runRemove(
if err := s.cleanImageScratch(ctx, mgr, sess.ID); err != nil {
return err
}
- s.publishProgress(ctx, configID, skillID, SkillProgress{Percent: 60, Stage: "removed"})
+ s.publishProgress(ctx, tenantID, configID, skillID, SkillProgress{Percent: 60, Stage: "removed"})
// Checked again: everything above is confined to a sandbox we are about
// to destroy, and the wipe itself takes minutes, so the lock may have been
@@ -231,7 +232,7 @@ func (s *TenantSkillService) runRemove(
return err
}
- if err := s.switchImagePointer(ctx, tenantID, configID, ref.ID, generation); err != nil {
+ if err := s.switchImagePointer(ctx, tenantID, configID, ref.ID, generation, builtFingerprint); err != nil {
s.abandonSnapshot(cleanupBase, tenantID, mgr, removeRowID, ref.ID)
return err
}
@@ -389,7 +390,7 @@ func (s *TenantSkillService) finishRemoval(
s.deleteBundleBestEffort(ctx, tenantID, skill.BundleRef)
}
s.markConfigSandboxesStale(ctx, tenantID, configID)
- s.publishProgress(ctx, configID, skillID, SkillProgress{
+ s.publishProgress(ctx, tenantID, configID, skillID, SkillProgress{
Percent: 100, Stage: "done", Status: "removed",
})
return nil
@@ -413,7 +414,7 @@ func (s *TenantSkillService) restoreSkillAfterFailedRemoval(
e.Error = cause.Error()
e.InstallingSince = nil
})
- s.publishProgress(ctx, configID, skillID, SkillProgress{
+ s.publishProgress(ctx, tenantID, configID, skillID, SkillProgress{
Percent: 100, Stage: "failed", Status: status, Log: cause.Error(),
})
}
diff --git a/internal/application/service/tenant_skill_service.go b/internal/application/service/tenant_skill_service.go
index 76f230a32..c29bfb971 100644
--- a/internal/application/service/tenant_skill_service.go
+++ b/internal/application/service/tenant_skill_service.go
@@ -25,11 +25,12 @@ const (
// TenantSkillService owns the skill image lifecycle for sandbox configs.
type TenantSkillService struct {
- skills repository.TenantSkillRepository
- configs repository.TenantSandboxConfigRepository
- resolver interfaces.StorageBackendResolver
- sandboxes sandbox.TenantSandboxResolver
- agents interfaces.AgentService
+ skills repository.TenantSkillRepository
+ configs repository.TenantSandboxConfigRepository
+ resolver interfaces.StorageBackendResolver
+ sandboxes sandbox.TenantSandboxResolver
+ sandboxPolicy WorkspaceSandboxPolicy
+ agents interfaces.AgentService
// installerAgents reads the stored installer record. It is a separate
// dependency from agents because GetAgentByID lives on the custom agent
// service, not on interfaces.AgentService.
@@ -54,6 +55,7 @@ func NewTenantSkillService(
configsRepo repository.TenantSandboxConfigRepository,
resolver interfaces.StorageBackendResolver,
sandboxes sandbox.TenantSandboxResolver,
+ sandboxPolicy WorkspaceSandboxPolicy,
agents interfaces.AgentService,
customAgents interfaces.CustomAgentService,
sessions interfaces.SessionService,
@@ -65,6 +67,7 @@ func NewTenantSkillService(
configs: configsRepo,
resolver: resolver,
sandboxes: sandboxes,
+ sandboxPolicy: sandboxPolicy,
agents: agents,
installerAgents: customAgents,
sessions: sessions,
diff --git a/internal/application/service/tenant_skill_service_test.go b/internal/application/service/tenant_skill_service_test.go
index 223cf36c9..9bb494762 100644
--- a/internal/application/service/tenant_skill_service_test.go
+++ b/internal/application/service/tenant_skill_service_test.go
@@ -9,8 +9,14 @@ import (
"github.com/stretchr/testify/require"
)
+func TestSkillProgressKeyIncludesTheTenant(t *testing.T) {
+ require.Equal(t, "weknora-skill-install:7:cfg-1:sk-1", skillProgressKey(7, "cfg-1", "sk-1"))
+ require.NotEqual(t, skillProgressKey(7, "cfg-1", "sk-1"), skillProgressKey(8, "cfg-1", "sk-1"),
+ "two workspaces must not share a progress slot because they happened to reuse IDs")
+}
+
func TestTenantSkillServiceWithConfigLockLocalRespectsCanceledContext(t *testing.T) {
- svc := NewTenantSkillService(nil, nil, nil, nil, nil, nil, nil, nil, nil)
+ svc := NewTenantSkillService(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
entered := make(chan struct{})
releaseHolder := make(chan struct{})
holderDone := make(chan error, 1)