From 47ce4f5d186bf5534ad2c45271012892750a379d Mon Sep 17 00:00:00 2001 From: yiling Date: Wed, 20 May 2026 22:14:43 +0800 Subject: [PATCH] fix(skill): fallback to host environment only if skill config value is empty --- src/agent/executor.ts | 16 +-- src/agent/tools/read-skill.test.ts | 174 ++++++++++++++++++++++++++++- src/agent/tools/read-skill.ts | 5 +- 3 files changed, 185 insertions(+), 10 deletions(-) diff --git a/src/agent/executor.ts b/src/agent/executor.ts index 5b7562ad..e3421cb2 100644 --- a/src/agent/executor.ts +++ b/src/agent/executor.ts @@ -5,14 +5,14 @@ import { s3Service } from '@/services/s3/s3' import { SandboxManager } from '@anthropic-ai/sandbox-runtime' import { getModel, type ImageContent, type TextContent } from '@mariozechner/pi-ai' import { - AuthStorage, - createAgentSession, - DefaultResourceLoader, - defineTool, - ModelRegistry, - SessionManager, - SettingsManager, - type ToolDefinition, + AuthStorage, + createAgentSession, + DefaultResourceLoader, + defineTool, + ModelRegistry, + SessionManager, + SettingsManager, + type ToolDefinition, } from '@mariozechner/pi-coding-agent' import { Type, type TSchema } from '@sinclair/typebox' import * as fs from 'fs' diff --git a/src/agent/tools/read-skill.test.ts b/src/agent/tools/read-skill.test.ts index 052891ea..14e8f593 100644 --- a/src/agent/tools/read-skill.test.ts +++ b/src/agent/tools/read-skill.test.ts @@ -1,4 +1,4 @@ -import { describe, it, expect, vi, beforeEach } from 'vitest' +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' import { prisma } from '@/db' import { setupTestDbHooks } from '@/db-test-hooks' import { createReadSkillTool } from './read-skill' @@ -131,4 +131,176 @@ describe('readSkillTool', () => { expect(s3Service.getObject).toHaveBeenCalled() expect(fs.writeFileSync).toHaveBeenCalledWith(expect.stringContaining('.hash'), 'new-hash') }) + + describe('environment variables', () => { + let originalEnv: NodeJS.ProcessEnv + + beforeEach(() => { + originalEnv = { ...process.env } + }) + + afterEach(() => { + process.env = originalEnv + }) + + it('should use user-configured value if it is non-empty', async () => { + const team = await prisma.team.create({ data: { name: 'Test Team' } }) + const skill = await prisma.skill.create({ + data: { + name: 'Skill Env Test 1', + assetId: 'asset1', + hash: 'hash1', + teamId: team.id, + config: { + environmentVariables: [{ name: 'CONFIGURED_VAR', default: 'user-val' }], + }, + }, + }) + + delete process.env.CONFIGURED_VAR + + vi.spyOn(fs, 'existsSync').mockReturnValue(true) + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures + vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => { + if (path.toString().endsWith('.hash')) return 'hash1' + if (path.toString().endsWith('SKILL.md')) return '# Content' + return '' + }) + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const readSkillTool = createReadSkillTool(mockSessionManager as any) + await readSkillTool.execute( + '1', + { skillId: skill.id }, + undefined, + undefined, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + {} as unknown as any, + ) + + expect(mockSessionManager.addSkillEnvs).toHaveBeenCalledWith({ + CONFIGURED_VAR: 'user-val', + }) + }) + + it('should favor user-configured value even if host environment variable is also defined', async () => { + const team = await prisma.team.create({ data: { name: 'Test Team' } }) + const skill = await prisma.skill.create({ + data: { + name: 'Skill Env Test 2', + assetId: 'asset1', + hash: 'hash2', + teamId: team.id, + config: { + environmentVariables: [{ name: 'OVERRIDDEN_VAR', default: 'user-val' }], + }, + }, + }) + + process.env.OVERRIDDEN_VAR = 'host-val' + + vi.spyOn(fs, 'existsSync').mockReturnValue(true) + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures + vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => { + if (path.toString().endsWith('.hash')) return 'hash2' + if (path.toString().endsWith('SKILL.md')) return '# Content' + return '' + }) + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const readSkillTool = createReadSkillTool(mockSessionManager as any) + await readSkillTool.execute( + '1', + { skillId: skill.id }, + undefined, + undefined, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + {} as unknown as any, + ) + + expect(mockSessionManager.addSkillEnvs).toHaveBeenCalledWith({ + OVERRIDDEN_VAR: 'user-val', + }) + }) + + it('should fall back to host environment variable if user-configured value is empty string', async () => { + const team = await prisma.team.create({ data: { name: 'Test Team' } }) + const skill = await prisma.skill.create({ + data: { + name: 'Skill Env Test 3', + assetId: 'asset1', + hash: 'hash3', + teamId: team.id, + config: { + environmentVariables: [{ name: 'FALLBACK_VAR', default: '' }], + }, + }, + }) + + process.env.FALLBACK_VAR = 'host-val' + + vi.spyOn(fs, 'existsSync').mockReturnValue(true) + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures + vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => { + if (path.toString().endsWith('.hash')) return 'hash3' + if (path.toString().endsWith('SKILL.md')) return '# Content' + return '' + }) + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const readSkillTool = createReadSkillTool(mockSessionManager as any) + await readSkillTool.execute( + '1', + { skillId: skill.id }, + undefined, + undefined, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + {} as unknown as any, + ) + + expect(mockSessionManager.addSkillEnvs).toHaveBeenCalledWith({ + FALLBACK_VAR: 'host-val', + }) + }) + + it('should fall back to host environment variable if user-configured value is undefined', async () => { + const team = await prisma.team.create({ data: { name: 'Test Team' } }) + const skill = await prisma.skill.create({ + data: { + name: 'Skill Env Test 4', + assetId: 'asset1', + hash: 'hash4', + teamId: team.id, + config: { + environmentVariables: [{ name: 'FALLBACK_VAR_UNDEF' }], + }, + }, + }) + + process.env.FALLBACK_VAR_UNDEF = 'host-val2' + + vi.spyOn(fs, 'existsSync').mockReturnValue(true) + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- mocking node fs readFileSync which has complex overloaded signatures + vi.spyOn(fs, 'readFileSync').mockImplementation((path: any) => { + if (path.toString().endsWith('.hash')) return 'hash4' + if (path.toString().endsWith('SKILL.md')) return '# Content' + return '' + }) + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const readSkillTool = createReadSkillTool(mockSessionManager as any) + await readSkillTool.execute( + '1', + { skillId: skill.id }, + undefined, + undefined, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + {} as unknown as any, + ) + + expect(mockSessionManager.addSkillEnvs).toHaveBeenCalledWith({ + FALLBACK_VAR_UNDEF: 'host-val2', + }) + }) + }) }) diff --git a/src/agent/tools/read-skill.ts b/src/agent/tools/read-skill.ts index ffd1dfdd..6c7e56a9 100644 --- a/src/agent/tools/read-skill.ts +++ b/src/agent/tools/read-skill.ts @@ -35,7 +35,10 @@ export const createReadSkillTool = (sessionManager: DatabaseSessionManager) => if (config?.environmentVariables && Array.isArray(config.environmentVariables)) { const envs: Record = {} for (const envVar of config.environmentVariables) { - const value = process.env[envVar.name] || envVar.default + const value = + envVar.default !== undefined && envVar.default !== null && envVar.default !== '' + ? envVar.default + : process.env[envVar.name] if (value !== undefined) { envs[envVar.name] = value }