diff --git a/internal/application/service/agent_service.go b/internal/application/service/agent_service.go index e0b48b97..6d06251e 100644 --- a/internal/application/service/agent_service.go +++ b/internal/application/service/agent_service.go @@ -200,6 +200,11 @@ func (s *agentService) CreateAgentEngine( } s.registerMCPTools(ctx, toolRegistry, config, eventBus, sessionID, assistantMessageID) + // File inspection tools are a pure sandbox capability independent of the + // skill switch: register them whenever the workspace sandbox supports a + // session filesystem, even when skills are disabled. + s.registerSandboxFileTools(ctx, toolRegistry, sessionID, config) + // 3. Resolve knowledge base and selected document metadata kbInfos, selectedDocs := s.resolveKBAndDocInfos(ctx, config) @@ -243,9 +248,18 @@ func (s *agentService) CreateAgentEngine( // remains so tests (and any caller that still points at a host // directory) can construct a manager. Install mode initializes for // the shell without hanging a skills manager on the engine. + // + // The shell entry point follows SkillsEnabled rather than requiring a + // ready skill to already exist: shell_exec can execute skill scripts, so + // it belongs under the workspace's explicit skill switch. A sandbox whose + // skills are still installing — or that simply has none yet — must still + // hand the model a shell when skills are enabled, otherwise an agent + // pointed at a fresh sandbox has no way to inspect its environment. + // offerSkills (below) still gates the skills manager that feeds the model + // the installed-skill list; it does not gate the shell. offerSkills := config.SkillsEnabled && (len(config.SkillDirs) > 0 || len(config.TenantSkills) > 0) - if offerSkills || config.SkillInstallMode() { + if config.SkillsEnabled || config.SkillInstallMode() { skillsManager, err := s.initializeSkillsManager(ctx, sessionID, config, toolRegistry) if err != nil { logger.Warnf(ctx, "Failed to initialize skills manager: %v", err) @@ -365,6 +379,51 @@ func (s *agentService) resolveKBAndDocInfos( return kbInfos, selectedDocs } +// registerSandboxFileTools registers list_sandbox_files / read_sandbox_file. +// +// These expose per-session filesystem inspection and are a pure sandbox +// capability, not a skill capability. They therefore do NOT follow +// SkillsEnabled: an agent with skills disabled must still be able to read +// staged attachments out of /workspace/input. Registration only requires a +// non-disabled sandbox whose manager advertises a SessionFileStore. The +// skill installer agent also receives them (its remote sandbox advertises a +// file store), which is harmless — a root shell can already list and read the +// same tree. +// +// It resolves the sandbox independently of initializeSkillsManager so the +// file tools are available even when skills are disabled and the skills +// manager is never built. +func (s *agentService) registerSandboxFileTools( + ctx context.Context, + toolRegistry *tools.ToolRegistry, + sessionID string, + config *types.AgentConfig, +) { + if s == nil { + return + } + tenantID, _ := types.TenantIDFromContext(ctx) + sandboxMgr, _, err := resolveSandboxForExecution( + ctx, s.sandboxResolver, s.sandboxMgr, s.sandboxPinner, + tenantID, sessionID, config.SandboxConfigID, s.sandboxPolicy, + ) + if err != nil { + logger.Warnf(ctx, "Failed to resolve sandbox for file tools: %v", err) + return + } + if sandboxMgr == nil || sandboxMgr.GetType() == sandbox.SandboxTypeDisabled { + return + } + if store := sessionSandboxFileStore(sandboxMgr); store != nil { + toolRegistry.RegisterTool(tools.NewListSandboxFilesTool(store)) + toolRegistry.RegisterTool(tools.NewReadSandboxFileTool(store)) + logger.Infof(ctx, "Registered list_sandbox_files and read_sandbox_file tools") + } else { + logger.Infof(ctx, "Sandbox backend does not advertise session filesystem capability; "+ + "list_sandbox_files/read_sandbox_file not registered") + } +} + // initializeSkillsManager creates and initializes the skills manager. // // The sandbox manager is resolved per workspace: backends differ in @@ -426,27 +485,11 @@ func (s *agentService) initializeSkillsManager( logger.Infof(ctx, "Registered execute_skill_script tool") } - // list_sandbox_files / read_sandbox_file expose per-session - // filesystem inspection. Registration is gated on the sandbox - // manager advertising a SessionFileStore capability. A manager that - // cannot honour that capability returns nil so we never surface - // tenant-isolated tools against a backend that cannot isolate them. - // - // They follow SkillsEnabled for the same reason the skill tools do: - // the installer agent is here only for the shell it needs to install - // dependencies, and its prompt tells it to use shell_exec alone. A - // root shell can already read and list anything these two could, so - // offering them adds nothing but tool surface and iteration budget. - if config.SkillsEnabled { - if store := sessionSandboxFileStore(sandboxMgr); store != nil { - toolRegistry.RegisterTool(tools.NewListSandboxFilesTool(store)) - toolRegistry.RegisterTool(tools.NewReadSandboxFileTool(store)) - logger.Infof(ctx, "Registered list_sandbox_files and read_sandbox_file tools") - } else { - logger.Infof(ctx, "Sandbox backend does not advertise session filesystem capability; "+ - "list_sandbox_files/read_sandbox_file not registered") - } - } + // shell_exec follows SkillsEnabled (it can execute skill scripts) and + // is registered by registerSandboxShellTool below. list_sandbox_files + // and read_sandbox_file are pure filesystem capabilities and are + // registered separately in registerSandboxFileTools, independent of + // SkillsEnabled. s.registerSandboxShellTool(ctx, toolRegistry, sandboxMgr, config) } diff --git a/internal/application/service/agent_service_test.go b/internal/application/service/agent_service_test.go index a0e8ca4e..20facce1 100644 --- a/internal/application/service/agent_service_test.go +++ b/internal/application/service/agent_service_test.go @@ -123,11 +123,12 @@ func toolOffered(names []string, name string) bool { return false } -// TestCreateAgentEngineOpensSandboxToolsOnlyForInstallMode pins the gate on -// the skill installer alone. shell_exec is a user-selectable entry in the tool -// picker, so gating on AllowedTools would hand a live sandbox shell (plus -// list_sandbox_files and read_sandbox_file) to every existing agent record -// that already lists it, on deploy, with nobody touching those agents. +// TestCreateAgentEngineOpensSandboxToolsOnlyForInstallMode pins the skill +// gate on the skill installer alone for shell_exec. shell_exec follows +// SkillsEnabled (it can execute skill scripts), while list_sandbox_files and +// read_sandbox_file are pure filesystem capabilities that follow the sandbox +// backend's SessionFileStore capability instead — so they are offered even +// when skills are disabled and to the installer agent. func TestCreateAgentEngineOpensSandboxToolsOnlyForInstallMode(t *testing.T) { ctx := context.WithValue(context.Background(), types.TenantIDContextKey, uint64(7)) @@ -139,9 +140,8 @@ func TestCreateAgentEngineOpensSandboxToolsOnlyForInstallMode(t *testing.T) { typ: sandbox.SandboxTypeCube, shell: &stubShellExecutor{}, // The manager advertises a session file store, the way - // every real remote backend does: withholding - // list_sandbox_files/read_sandbox_file from install mode - // must be a decision, not an accident of the fake. + // every real remote backend does. File tools are a pure + // sandbox capability, so the installer receives them too. files: stubSessionFileStore{}, installShell: &stubInstallShellExecutor{}, }, @@ -162,13 +162,13 @@ func TestCreateAgentEngineOpensSandboxToolsOnlyForInstallMode(t *testing.T) { require.True(t, toolOffered(chatModel.lastToolNames, tools.ToolShellExec)) require.False(t, toolOffered(chatModel.lastToolNames, tools.ToolReadSkill)) require.False(t, toolOffered(chatModel.lastToolNames, tools.ToolExecuteSkillScript)) - require.False(t, toolOffered(chatModel.lastToolNames, tools.ToolListSandboxFiles), - "the installer is here for the shell; a root shell already reads any file") - require.False(t, toolOffered(chatModel.lastToolNames, tools.ToolReadSandboxFile)) + require.True(t, toolOffered(chatModel.lastToolNames, tools.ToolListSandboxFiles), + "file inspection is a sandbox capability, not a skill, so the installer keeps it") + require.True(t, toolOffered(chatModel.lastToolNames, tools.ToolReadSandboxFile)) require.Nil(t, engine.(*agent.AgentEngine).GetSkillsManager()) }) - t.Run("an ordinary agent that lists shell_exec still gets no sandbox tools", func(t *testing.T) { + t.Run("an ordinary agent with skills off gets no shell but keeps file tools", func(t *testing.T) { chatModel := &fakeAgentChatModel{} svc := &agentService{ sandboxResolver: stubSandboxResolver{ @@ -191,13 +191,13 @@ func TestCreateAgentEngineOpensSandboxToolsOnlyForInstallMode(t *testing.T) { _, err = engine.Execute(ctx, "sess-1", "msg-1", "hello", nil) require.NoError(t, err) require.False(t, toolOffered(chatModel.lastToolNames, tools.ToolShellExec), - "an agent with skills off could never obtain a sandbox shell before this work") - require.False(t, toolOffered(chatModel.lastToolNames, tools.ToolListSandboxFiles)) - require.False(t, toolOffered(chatModel.lastToolNames, tools.ToolReadSandboxFile)) + "shell_exec follows SkillsEnabled; an agent with skills off gets no shell") + require.True(t, toolOffered(chatModel.lastToolNames, tools.ToolListSandboxFiles)) + require.True(t, toolOffered(chatModel.lastToolNames, tools.ToolReadSandboxFile)) require.Nil(t, engine.(*agent.AgentEngine).GetSkillsManager()) }) - t.Run("skills disabled without skills or install mode gets no sandbox or skill tools", func(t *testing.T) { + t.Run("skills disabled without skills or install mode gets no shell or skill tools but keeps file tools", func(t *testing.T) { chatModel := &fakeAgentChatModel{} svc := &agentService{ sandboxResolver: stubSandboxResolver{ @@ -219,8 +219,8 @@ func TestCreateAgentEngineOpensSandboxToolsOnlyForInstallMode(t *testing.T) { _, err = engine.Execute(ctx, "sess-1", "msg-1", "hello", nil) require.NoError(t, err) require.False(t, toolOffered(chatModel.lastToolNames, tools.ToolShellExec)) - require.False(t, toolOffered(chatModel.lastToolNames, tools.ToolListSandboxFiles)) - require.False(t, toolOffered(chatModel.lastToolNames, tools.ToolReadSandboxFile)) + require.True(t, toolOffered(chatModel.lastToolNames, tools.ToolListSandboxFiles)) + require.True(t, toolOffered(chatModel.lastToolNames, tools.ToolReadSandboxFile)) require.False(t, toolOffered(chatModel.lastToolNames, tools.ToolReadSkill)) require.False(t, toolOffered(chatModel.lastToolNames, tools.ToolExecuteSkillScript)) require.Nil(t, engine.(*agent.AgentEngine).GetSkillsManager()) @@ -337,6 +337,38 @@ func TestSkillToolsFollowSkillsEnabled(t *testing.T) { }) } +// shell_exec can execute skill scripts, so it follows SkillsEnabled rather +// than the presence of a ready skill. An agent whose skills are enabled but +// whose sandbox currently carries no ready skill must still receive the shell, +// otherwise it has no way to inspect a fresh or still-installing sandbox. +func TestCreateAgentEngineShellFollowsSkillsEnabledWithoutInstalledSkills(t *testing.T) { + ctx := context.WithValue(context.Background(), types.TenantIDContextKey, uint64(7)) + chatModel := &fakeAgentChatModel{} + svc := &agentService{ + sandboxResolver: stubSandboxResolver{ + mgr: &capableManager{ + typ: sandbox.SandboxTypeCube, + shell: &stubShellExecutor{}, + files: stubSessionFileStore{}, + installShell: &stubInstallShellExecutor{}, + }, + }, + } + + engine, err := svc.CreateAgentEngine(ctx, &types.AgentConfig{ + SandboxConfigID: "cfg-remote", + SkillsEnabled: true, + // No SkillDirs and no TenantSkills: skills are enabled, but the sandbox + // image carries none. + }, chatModel, nil, nil, "sess-1", "msg-1") + + require.NoError(t, err) + _, err = engine.Execute(ctx, "sess-1", "msg-1", "hello", nil) + require.NoError(t, err) + require.True(t, toolOffered(chatModel.lastToolNames, tools.ToolShellExec), + "an agent with skills enabled must have shell_exec even when no ready skill exists yet") +} + // Whether an installed skill is invocable is decided when the AgentConfig is // built (effectiveTenantSkills). This is the other half of that contract: the // manager the model is described from must carry exactly the rows it was