From e55e34238799e5ee2b5ca7fa1f4b27512afb6d4e Mon Sep 17 00:00:00 2001 From: hailongzhao Date: Tue, 22 Sep 2026 16:32:53 +0800 Subject: [PATCH] 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. --- internal/agent/skills/env_resolver.go | 9 ++-- internal/application/service/agent_service.go | 34 ++++++++++++++- .../application/service/user_env_resolver.go | 23 ++++++++-- .../service/user_env_resolver_test.go | 43 ++++++++++++++++++- 4 files changed, 100 insertions(+), 9 deletions(-) diff --git a/internal/agent/skills/env_resolver.go b/internal/agent/skills/env_resolver.go index 249da432..8fe565ab 100644 --- a/internal/agent/skills/env_resolver.go +++ b/internal/agent/skills/env_resolver.go @@ -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) } diff --git a/internal/application/service/agent_service.go b/internal/application/service/agent_service.go index a1dc8aae..39c728f6 100644 --- a/internal/application/service/agent_service.go +++ b/internal/application/service/agent_service.go @@ -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 diff --git a/internal/application/service/user_env_resolver.go b/internal/application/service/user_env_resolver.go index ec1d1978..b297cd1e 100644 --- a/internal/application/service/user_env_resolver.go +++ b/internal/application/service/user_env_resolver.go @@ -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) } } diff --git a/internal/application/service/user_env_resolver_test.go b/internal/application/service/user_env_resolver_test.go index 585619c6..61eef6b2 100644 --- a/internal/application/service/user_env_resolver_test.go +++ b/internal/application/service/user_env_resolver_test.go @@ -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) +}