fix(skill): count create-time sandbox env vars in the required check

A required skill key filled only on the sandbox config was already in the
container, but the gate still treated it as unset.
main
hailongzhao 2026-09-22 16:32:53 +08:00 committed by lyingbug
parent 33c0333ec9
commit e55e342387
4 changed files with 100 additions and 9 deletions

View File

@ -17,9 +17,12 @@ import (
// row id is an implementation detail of the installed-skill source.
type SkillEnvResolver interface {
// ResolveEnv returns the values to inject and the names of any required
// variable that neither the admin nor this caller has filled in. An empty
// skillName asks for the caller's config-wide variables alone. The caller's
// identity is taken from ctx, never from a parameter.
// variable that neither the admin, this caller, nor the sandbox config's
// create-time env_vars has filled in. Create-time names satisfy required
// but are not in the returned map: they were baked into the sandbox at
// creation and must not be re-injected per exec. An empty skillName asks
// for the caller's config-wide variables alone. The caller's identity is
// taken from ctx, never from a parameter.
ResolveEnv(ctx context.Context, skillName string) (env map[string]string, missing []string, err error)
}

View File

@ -734,7 +734,39 @@ func (s *agentService) userEnvResolver(
if tenantID == 0 {
return nil
}
return NewUserEnvResolver(rows, repository.NewTenantSkillRepository(s.db), tenantID, configID)
return NewUserEnvResolver(
rows,
repository.NewTenantSkillRepository(s.db),
sandboxCreateTimeEnvVars(ctx, s.db, tenantID, configID),
tenantID, configID,
)
}
// sandboxCreateTimeEnvVars loads the config's env_vars once per agent run for
// the required check. They are not re-injected: the container already has them.
// A read failure degrades to empty so a DB blip does not surface as "nobody
// has set this credential".
func sandboxCreateTimeEnvVars(
ctx context.Context, db *gorm.DB, tenantID uint64, configID string,
) map[string]string {
if db == nil || db.Config == nil {
return nil
}
cfg, err := repository.NewTenantSandboxConfigRepository(db).GetByID(ctx, tenantID, configID)
if err != nil {
logger.Warnf(ctx, "[skill] sandbox config %s: create-time env unavailable: %v", configID, err)
return nil
}
if cfg == nil || cfg.Config == nil {
return nil
}
env := map[string]string{}
for name, value := range cfg.Config.EnvVars {
if value != "" {
env[name] = value
}
}
return env
}
// skillEnvCapture writes declared skill credentials a successful shell_exec

View File

@ -21,6 +21,11 @@ type userEnvResolver struct {
// byName indexes the rows by the name the model addresses skills with.
byName map[string]*types.TenantSkillEntity
userEnvs userEnvReader
// containerEnv is the sandbox config's env_vars, already loaded by the
// caller. They were baked into the container at creation and only
// participate in the required check — they are not copied into the
// per-exec map.
containerEnv map[string]string
// tenantID and configID are the scope the rows came from, not whatever the
// context happens to hold, so a lookup cannot resolve into another
// workspace or another config.
@ -32,7 +37,11 @@ type userEnvResolver struct {
// installed skills, already carrying their declarations and admin values; they
// may be empty, in which case only config-wide variables are resolved.
func NewUserEnvResolver(
rows []*types.TenantSkillEntity, userEnvs userEnvReader, tenantID uint64, configID string,
rows []*types.TenantSkillEntity,
userEnvs userEnvReader,
containerEnv map[string]string,
tenantID uint64,
configID string,
) *userEnvResolver {
byName := make(map[string]*types.TenantSkillEntity, len(rows))
for _, row := range rows {
@ -41,7 +50,11 @@ func NewUserEnvResolver(
}
}
return &userEnvResolver{
byName: byName, userEnvs: userEnvs, tenantID: tenantID, configID: configID,
byName: byName,
userEnvs: userEnvs,
containerEnv: containerEnv,
tenantID: tenantID,
configID: configID,
}
}
@ -53,6 +66,10 @@ func NewUserEnvResolver(
// An empty skillName resolves only the config-wide variables, which is what
// shell_exec gets when the model names no skill. An unknown name behaves the
// same way: host-staged skills carry no declaration.
//
// Sandbox-config env_vars (baked into the container at creation) are not in
// that overlay. They still satisfy a required declaration so a key filled in
// before the sandbox was created is not reported as unset.
func (r *userEnvResolver) ResolveEnv(
ctx context.Context, skillName string,
) (map[string]string, []string, error) {
@ -88,7 +105,7 @@ func (r *userEnvResolver) ResolveEnv(
var missing []string
if row != nil {
for _, declared := range row.Envs {
if declared.Required && env[declared.Name] == "" {
if declared.Required && env[declared.Name] == "" && r.containerEnv[declared.Name] == "" {
missing = append(missing, declared.Name)
}
}

View File

@ -81,7 +81,7 @@ func searchSkillRow() *types.TenantSkillEntity {
}
func newTestResolver(reader userEnvReader, rows ...*types.TenantSkillEntity) *userEnvResolver {
return NewUserEnvResolver(rows, reader, testEnvTenantID, testEnvConfigID)
return NewUserEnvResolver(rows, reader, nil, testEnvTenantID, testEnvConfigID)
}
func TestResolverUserValueOverridesWorkspaceValue(t *testing.T) {
@ -295,7 +295,7 @@ func TestResolverWorksWithNoInstalledSkills(t *testing.T) {
reader := &fakeUserEnvReader{tenantID: testEnvTenantID}
reader.put(webPrincipal("u1"), testEnvConfigID, "",
map[string]string{"HTTP_PROXY": "http://proxy:8080"})
resolver := NewUserEnvResolver(nil, reader, testEnvTenantID, testEnvConfigID)
resolver := NewUserEnvResolver(nil, reader, nil, testEnvTenantID, testEnvConfigID)
ctx := types.WithPrincipal(context.Background(), webPrincipal("u1"))
env, _, err := resolver.ResolveEnv(ctx, "")
@ -303,3 +303,42 @@ func TestResolverWorksWithNoInstalledSkills(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "http://proxy:8080", env["HTTP_PROXY"])
}
// A required skill key filled in on the sandbox config is already in the
// container from creation. The gate must not refuse that run, and must not
// copy the value into the per-exec map — that would change create-time
// injection into a live overlay.
func TestResolverSandboxConfigSatisfiesRequiredWithoutInjecting(t *testing.T) {
row := searchSkillRow()
row.Envs = types.SkillEnvVars{{Name: "TAVILY_API_KEY", Required: true}}
resolver := NewUserEnvResolver(
[]*types.TenantSkillEntity{row},
&fakeUserEnvReader{tenantID: testEnvTenantID},
map[string]string{"TAVILY_API_KEY": "sandbox-key"},
testEnvTenantID, testEnvConfigID,
)
ctx := types.WithPrincipal(context.Background(), webPrincipal("u1"))
env, missing, err := resolver.ResolveEnv(ctx, "web-search")
require.NoError(t, err)
require.Empty(t, missing)
require.NotContains(t, env, "TAVILY_API_KEY")
}
func TestResolverEmptySandboxConfigValueDoesNotSatisfyRequired(t *testing.T) {
row := searchSkillRow()
row.Envs = types.SkillEnvVars{{Name: "TAVILY_API_KEY", Required: true}}
resolver := NewUserEnvResolver(
[]*types.TenantSkillEntity{row},
&fakeUserEnvReader{tenantID: testEnvTenantID},
map[string]string{"TAVILY_API_KEY": ""},
testEnvTenantID, testEnvConfigID,
)
ctx := types.WithPrincipal(context.Background(), webPrincipal("u1"))
_, missing, err := resolver.ResolveEnv(ctx, "web-search")
require.NoError(t, err)
require.Equal(t, []string{"TAVILY_API_KEY"}, missing)
}