mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-29 20:34:28 +00:00
feat(runner): replace server-side bash sandbox with remote runner (#530)
* feat(runner): replace server-side bash sandbox with remote runner * fix(runner): address review findings for output bounds, key rotation, defaults, and generated uploads * chore(docker): remove sandbox privileges and env vars from remaining compose files * feat(cli): add runner start with bundled SRT sandboxing (#535) * refactor(agent): expose runner bash without skill gating * feat(cli): add runner start with optional SRT sandboxing * refactor(cli): remove runner Python dependency * feat(cli): bundle SRT and use platform-native runner shells * refactor(cli): delegate runner processes to Execa * chore(cli): upgrade @anthropic-ai/sandbox-runtime to 0.0.83 * feat(cli): discover Bash for Windows runners Use Pi-style Bash discovery and launch behavior, exercise the packaged CLI shell, and wait for pending ZIP metadata reads before closing cancelled downloads. * fix(runner): enforce ownership and safe reconnects Gate dispatch on reconciliation, retain cancellations across disconnects, and stop replaced clients. Guard the single-server runner topology with a database lease. Restage missing generated inputs, report signal exits, and bound generated-file cleanup. * feat(webui): add Korean translations for agent runners * feat(cli): add runner event logging and --verbose flag (#536) * feat(cli): add runner event logging and --verbose flag * fix(cli): log runner capacity rejections accurately and use monotonic duration
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { describe, expect, test } from 'vitest'
|
||||
import AdmZip from 'adm-zip'
|
||||
import { describe, expect, test, vi } from 'vitest'
|
||||
import { runnerService } from '../runner/runner-service'
|
||||
import { s3Service } from '../s3/s3'
|
||||
import { AgentService } from './agent'
|
||||
import { appendSessionEntries } from './session-test-utils'
|
||||
|
||||
@@ -258,6 +261,109 @@ describe('AgentService', () => {
|
||||
expect(updated.user.image).toBe('files/01JK23456789ABCDEF01234567')
|
||||
})
|
||||
|
||||
test('Create and update Agent with runnerId and reset to null when runner is deleted', async () => {
|
||||
const db = prisma
|
||||
const svc = new AgentService()
|
||||
const { team, provider, model } = await setupTestData(db)
|
||||
|
||||
const { runner } = await runnerService.createRunner(team.id, { name: 'Bound Runner' })
|
||||
const otherTeam = await db.team.create({ data: { name: 'Other Team' } })
|
||||
const { runner: otherRunner } = await runnerService.createRunner(otherTeam.id, {
|
||||
name: 'Foreign Runner',
|
||||
})
|
||||
|
||||
// Rejects runner from another team
|
||||
await expect(
|
||||
svc.createAgent({
|
||||
teamId: team.id,
|
||||
name: 'Bad Runner Agent',
|
||||
type: 'chat',
|
||||
enabled: true,
|
||||
providerId: provider.id,
|
||||
modelId: model.id,
|
||||
runnerId: otherRunner.id,
|
||||
thinkingLevel: 'off',
|
||||
}),
|
||||
).rejects.toThrow('runner not found in team')
|
||||
|
||||
const agent = await svc.createAgent({
|
||||
teamId: team.id,
|
||||
name: 'Runner Agent',
|
||||
type: 'chat',
|
||||
enabled: true,
|
||||
providerId: provider.id,
|
||||
modelId: model.id,
|
||||
runnerId: runner.id,
|
||||
thinkingLevel: 'off',
|
||||
})
|
||||
expect(agent?.runnerId).toBe(runner.id)
|
||||
|
||||
// Clear runnerId back to null (use team default)
|
||||
const cleared = await svc.updateAgent({
|
||||
agentId: agent!.id,
|
||||
name: 'Runner Agent',
|
||||
type: 'chat',
|
||||
enabled: true,
|
||||
providerId: provider.id,
|
||||
modelId: model.id,
|
||||
runnerId: null,
|
||||
thinkingLevel: 'off',
|
||||
})
|
||||
expect(cleared.runnerId).toBeNull()
|
||||
|
||||
// Bind again and delete runner -> onDelete: SetNull sets agent.runnerId to null
|
||||
await svc.updateAgent({
|
||||
agentId: agent!.id,
|
||||
name: 'Runner Agent',
|
||||
type: 'chat',
|
||||
enabled: true,
|
||||
providerId: provider.id,
|
||||
modelId: model.id,
|
||||
runnerId: runner.id,
|
||||
thinkingLevel: 'off',
|
||||
})
|
||||
await runnerService.deleteRunner(team.id, runner.id)
|
||||
const afterDelete = await db.agent.findUniqueOrThrow({ where: { id: agent!.id } })
|
||||
expect(afterDelete.runnerId).toBeNull()
|
||||
})
|
||||
|
||||
test('getSkillContent extracts SKILL.md from S3 zip archive without local disk cache', async () => {
|
||||
const db = prisma
|
||||
const svc = new AgentService()
|
||||
const { team } = await setupTestData(db)
|
||||
|
||||
const asset = await db.asset.create({
|
||||
data: {
|
||||
name: 'skill.zip',
|
||||
type: 'file',
|
||||
status: 'uploaded',
|
||||
storageKey: { create: { key: 'skills/zip-skill.zip' } },
|
||||
},
|
||||
})
|
||||
const skill = await db.skill.create({
|
||||
data: {
|
||||
name: 'Zip Skill',
|
||||
teamId: team.id,
|
||||
assetId: asset.id,
|
||||
hash: 'zip-hash',
|
||||
},
|
||||
})
|
||||
|
||||
const zip = new AdmZip()
|
||||
zip.addFile('sub/SKILL.md', Buffer.from('# Zip Skill Docs', 'utf-8'))
|
||||
const getObjectSpy = vi.spyOn(s3Service, 'getObject').mockResolvedValue({
|
||||
buffer: zip.toBuffer(),
|
||||
contentType: 'application/zip',
|
||||
})
|
||||
|
||||
try {
|
||||
const content = await svc.getSkillContent(skill.id)
|
||||
expect(content).toBe('# Zip Skill Docs')
|
||||
} finally {
|
||||
getObjectSpy.mockRestore()
|
||||
}
|
||||
})
|
||||
|
||||
test('Update Agent preserves deniedTools when omitted, and allows explicit empty array', async () => {
|
||||
const db = prisma
|
||||
const svc = new AgentService()
|
||||
|
||||
@@ -19,8 +19,6 @@ import {
|
||||
UpdateAgentParams,
|
||||
} from '@shumai/dtos'
|
||||
import AdmZip from 'adm-zip'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { getAllowedAgentRoles } from './permissions'
|
||||
import { getPresetAvatarBuffer, getPresetAvatarStorageKey, isPresetAvatarId } from './presets'
|
||||
import { agentSessionService } from './session-service'
|
||||
@@ -41,6 +39,7 @@ interface AgentInfoSource {
|
||||
permission: TeamMemberRole
|
||||
providerId: string | null
|
||||
modelId: string | null
|
||||
runnerId?: string | null
|
||||
config: unknown
|
||||
soul: string | null
|
||||
user: { name: string; image: string | null }
|
||||
@@ -57,6 +56,8 @@ function extractS3Key(urlOrKey?: string | null): string | null | undefined {
|
||||
}
|
||||
|
||||
export class AgentService {
|
||||
private skillContentCache = new Map<string, string>()
|
||||
|
||||
constructor(private readonly prismaClient: typeof prisma = prisma) {}
|
||||
|
||||
async getSkillContent(skillId: string): Promise<string> {
|
||||
@@ -68,54 +69,44 @@ export class AgentService {
|
||||
throw new Error(`Skill with ID ${skillId} not found.`)
|
||||
}
|
||||
|
||||
const skillDir = path.join(process.cwd(), '.pi', 'skills', skill.id)
|
||||
const hashFile = path.join(skillDir, '.hash')
|
||||
let needsDownload = true
|
||||
|
||||
if (fs.existsSync(hashFile)) {
|
||||
const localHash = fs.readFileSync(hashFile, 'utf8')
|
||||
if (localHash === skill.hash) {
|
||||
needsDownload = false
|
||||
}
|
||||
const cacheKey = `${skill.id}:${skill.hash}`
|
||||
const cached = this.skillContentCache.get(cacheKey)
|
||||
if (cached !== undefined) {
|
||||
return cached
|
||||
}
|
||||
|
||||
if (needsDownload) {
|
||||
if (fs.existsSync(skillDir)) {
|
||||
fs.rmSync(skillDir, { recursive: true, force: true })
|
||||
}
|
||||
fs.mkdirSync(skillDir, { recursive: true })
|
||||
const asset = await this.prismaClient.asset.findUnique({
|
||||
where: { id: skill.assetId },
|
||||
include: { storageKey: true },
|
||||
})
|
||||
|
||||
const asset = await this.prismaClient.asset.findUnique({
|
||||
where: { id: skill.assetId },
|
||||
include: { storageKey: true },
|
||||
})
|
||||
if (!asset || !asset.storageKey?.key) {
|
||||
throw new Error('Skill asset not found or has no key')
|
||||
}
|
||||
|
||||
if (!asset || !asset.storageKey?.key) {
|
||||
throw new Error('Skill asset not found or has no key')
|
||||
}
|
||||
const { buffer: zipBuffer } = await s3Service.getObject(
|
||||
process.env.S3_BUCKET || 'shumai',
|
||||
asset.storageKey.key,
|
||||
)
|
||||
|
||||
const { buffer: zipBuffer } = await s3Service.getObject(
|
||||
process.env.S3_BUCKET || 'shumai',
|
||||
asset.storageKey.key,
|
||||
const zip = new AdmZip(zipBuffer)
|
||||
const entries = zip.getEntries()
|
||||
const skillMdEntry =
|
||||
entries.find((e) => !e.isDirectory && e.entryName === 'SKILL.md') ||
|
||||
entries.find((e) => !e.isDirectory && e.entryName === 'skill.md') ||
|
||||
entries.find(
|
||||
(e) =>
|
||||
!e.isDirectory &&
|
||||
(e.entryName.endsWith('/SKILL.md') || e.entryName.endsWith('/skill.md')),
|
||||
)
|
||||
|
||||
const zip = new AdmZip(zipBuffer)
|
||||
zip.extractAllTo(skillDir, true)
|
||||
|
||||
fs.writeFileSync(hashFile, skill.hash)
|
||||
if (!skillMdEntry) {
|
||||
throw new Error(`Skill downloaded but SKILL.md not found in skill ${skill.id}`)
|
||||
}
|
||||
|
||||
// Read SKILL.md
|
||||
let skillMdPath = path.join(skillDir, 'SKILL.md')
|
||||
if (!fs.existsSync(skillMdPath)) {
|
||||
skillMdPath = path.join(skillDir, 'skill.md')
|
||||
}
|
||||
|
||||
if (!fs.existsSync(skillMdPath)) {
|
||||
throw new Error(`Skill downloaded but SKILL.md not found in ${skillDir}`)
|
||||
}
|
||||
|
||||
return fs.readFileSync(skillMdPath, 'utf8')
|
||||
const content = skillMdEntry.getData().toString('utf8')
|
||||
this.skillContentCache.set(cacheKey, content)
|
||||
return content
|
||||
}
|
||||
|
||||
async getSkillEnvs(skillId: string): Promise<Record<string, string>> {
|
||||
@@ -150,6 +141,7 @@ export class AgentService {
|
||||
avatar,
|
||||
providerId,
|
||||
modelId,
|
||||
runnerId,
|
||||
thinkingLevel,
|
||||
systemPrompt,
|
||||
soul,
|
||||
@@ -206,6 +198,15 @@ export class AgentService {
|
||||
})
|
||||
if (!team) throw new Error('team not found')
|
||||
|
||||
if (runnerId) {
|
||||
const runner = await tx.runner.findFirst({
|
||||
where: { id: runnerId, teamId },
|
||||
})
|
||||
if (!runner) {
|
||||
throw new Error('runner not found in team')
|
||||
}
|
||||
}
|
||||
|
||||
const agentConfig: PrismaJson.AgentConfig = {
|
||||
provider: providerId || '',
|
||||
model: modelId || '',
|
||||
@@ -233,6 +234,7 @@ export class AgentService {
|
||||
permission: permission || 'reviewer',
|
||||
providerId: providerId || null,
|
||||
modelId: modelId || null,
|
||||
runnerId: runnerId || null,
|
||||
soul,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
config: agentConfig as any,
|
||||
@@ -280,6 +282,7 @@ export class AgentService {
|
||||
avatar,
|
||||
providerId,
|
||||
modelId,
|
||||
runnerId,
|
||||
thinkingLevel,
|
||||
systemPrompt,
|
||||
soul,
|
||||
@@ -380,6 +383,15 @@ export class AgentService {
|
||||
where: { agentId },
|
||||
})
|
||||
|
||||
if (runnerId) {
|
||||
const runner = await tx.runner.findFirst({
|
||||
where: { id: runnerId, teamId: agent.teamId },
|
||||
})
|
||||
if (!runner) {
|
||||
throw new Error('runner not found in team')
|
||||
}
|
||||
}
|
||||
|
||||
return tx.agent.update({
|
||||
where: { id: agentId },
|
||||
data: {
|
||||
@@ -388,6 +400,7 @@ export class AgentService {
|
||||
...(permission ? { permission } : {}),
|
||||
providerId: providerId || null,
|
||||
modelId: modelId || null,
|
||||
...(runnerId !== undefined ? { runnerId: runnerId || null } : {}),
|
||||
soul,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
config: agentConfig as any,
|
||||
@@ -478,6 +491,7 @@ export class AgentService {
|
||||
avatarPreset: config.avatarPreset,
|
||||
providerId: agent.providerId || undefined,
|
||||
modelId: agent.modelId || undefined,
|
||||
runnerId: agent.runnerId ?? null,
|
||||
thinkingLevel: config.thinkingLevel || 'off',
|
||||
systemPrompt: config.systemPrompt,
|
||||
soul: agent.soul || undefined,
|
||||
|
||||
Reference in New Issue
Block a user