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.
This commit is contained in:
yiling
2026-11-18 12:04:29 +08:00
parent fd4dc211f8
commit a9281cbc75
23 changed files with 691 additions and 128 deletions
+27
View File
@@ -8,6 +8,7 @@ import { s3Service, getStorageBackend } from '@shumai/core/src/s3/s3'
import * as archiveStorageModule from '@shumai/core/src/s3/archive-storage'
import { uploadService } from '@shumai/core/src/upload/upload'
import { metadataService } from '@shumai/core/src/metadata/metadata'
import { watermarkService } from '@shumai/core/src/watermark/watermark'
import { AssetService } from './asset'
import { ensureJpegInStorage } from '@shumai/core/src/s3/preview-converter'
@@ -22,6 +23,7 @@ vi.mock('@shumai/core/src/s3/s3', () => ({
deletePrefix: vi.fn().mockResolvedValue(1),
copyObject: vi.fn().mockResolvedValue(undefined),
getStoragePath: vi.fn((bucket: string, key: string) => `/mock/data/${bucket}/${key}`),
listObjects: vi.fn().mockResolvedValue([]),
},
getStorageBackend: vi.fn().mockReturnValue('local'),
}))
@@ -47,6 +49,31 @@ describe('AssetService', () => {
vi.clearAllMocks()
})
it('checks generated file expiration hourly while continuing regular cleanup', async () => {
vi.useFakeTimers({ toFake: ['Date', 'setTimeout', 'clearTimeout'] })
const startedAt = Date.now()
const regularCleanup = vi
.spyOn(watermarkService, 'purgeOrphanWatermarkConfigs')
.mockResolvedValue(0)
try {
await assetService.startCleanupJob()
await vi.waitFor(() => expect(s3Service.listObjects).toHaveBeenCalledTimes(1))
expect(s3Service.listObjects).toHaveBeenCalledWith(expect.any(String), 'temp/generated/')
await vi.advanceTimersByTimeAsync(5_000)
await vi.waitFor(() => expect(regularCleanup).toHaveBeenCalledTimes(2))
expect(s3Service.listObjects).toHaveBeenCalledTimes(1)
vi.setSystemTime(startedAt + 60 * 60 * 1000)
await vi.advanceTimersByTimeAsync(5_000)
await vi.waitFor(() => expect(s3Service.listObjects).toHaveBeenCalledTimes(2))
} finally {
assetService.stopCleanupJob()
regularCleanup.mockRestore()
vi.useRealTimers()
}
})
const setupBasicAssets = async () => {
const user = await prisma.user.create({
data: {
+7 -1
View File
@@ -1585,7 +1585,9 @@ export class AssetService {
private cleanupJobsRunning = false
async startCleanupJob() {
if (this.cleanupJobsRunning) return
this.cleanupJobsRunning = true
let nextGeneratedCleanupAt = 0
const runCleanup = async () => {
if (!this.cleanupJobsRunning) return
@@ -1594,8 +1596,12 @@ export class AssetService {
await this.expireTrashedAssets()
await this.purgePendingAssets()
await this.purgeUnreferencedStorageKeys()
await this.purgeExpiredGeneratedFiles()
await watermarkService.purgeOrphanWatermarkConfigs()
const now = Date.now()
if (now >= nextGeneratedCleanupAt) {
nextGeneratedCleanupAt = now + 60 * 60 * 1000
await this.purgeExpiredGeneratedFiles()
}
} catch (e: unknown) {
console.error('Error in asset cleanup job:', e)
}
+199 -1
View File
@@ -1,4 +1,5 @@
import { prisma } from '@shumai/db'
import * as advisoryLocks from '@shumai/db/src/advisory-lock'
import { setupTestDbHooks } from '@shumai/db/test'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { s3Service } from '../s3/s3'
@@ -32,6 +33,24 @@ function createMockWs() {
}
}
function reconcile(
service: RunnerService,
runnerId: string,
ws: RunnerSocket,
activeIds: string[] = [],
) {
service.handleMessage(
runnerId,
ws,
JSON.stringify({
type: 'reconcile',
environment: { platform: 'linux', shell: '/bin/sh' },
activeExecutions: activeIds.map((executionId) => ({ executionId, nextOffset: 0 })),
completedExecutions: [],
}),
)
}
describe('RunnerService', () => {
setupTestDbHooks()
@@ -49,11 +68,48 @@ describe('RunnerService', () => {
teamId = team.id
})
afterEach(() => {
afterEach(async () => {
service.stopHeartbeatMonitor()
await service.stopServerTopology()
vi.restoreAllMocks()
})
it('allows replicas without runners and enforces a single server while runners exist', async () => {
const replica = new RunnerService()
try {
await service.startServerTopology()
await replica.startServerTopology()
await expect(service.createRunner(teamId, { name: 'Blocked' })).rejects.toMatchObject({
status: 409,
message: expect.stringContaining('single main Shumai server'),
})
expect(await service.listRunners(teamId)).toEqual([])
await replica.stopServerTopology()
const { runner } = await service.createRunner(teamId, { name: 'Exclusive runner' })
await expect(replica.startServerTopology()).rejects.toThrow('single main Shumai server')
await service.stopServerTopology()
await replica.startServerTopology()
await expect(service.startServerTopology()).rejects.toThrow('single main Shumai server')
const acquireLease = advisoryLocks.tryAcquireUpgradeableAdvisoryLock
vi.spyOn(advisoryLocks, 'tryAcquireUpgradeableAdvisoryLock').mockImplementationOnce(
async (lockKey, exclusive) => {
// The newcomer has already looked up the existing runner.
await replica.deleteRunner(teamId, runner.id)
return acquireLease(lockKey, exclusive)
},
)
await service.startServerTopology()
await expect(replica.createRunner(teamId, { name: 'Blocked again' })).rejects.toMatchObject({
status: 409,
})
} finally {
await replica.stopServerTopology()
}
})
describe('Key & workspace path helpers', () => {
it('keeps validated OS and shell metadata only for the current connection', async () => {
const { runner, key } = await service.createRunner(teamId, { name: 'Windows runner' })
@@ -201,6 +257,25 @@ describe('RunnerService', () => {
})
describe('WebSocket connection lifecycle & eviction', () => {
it('waits for reconciliation before reporting online or dispatching a command', async () => {
const { runner, key } = await service.createRunner(teamId, { name: 'Handshaking runner' })
const conn = createMockWs()
await service.registerConnection(
{ id: runner.id, teamId, name: runner.name, keyHash: hashRunnerKey(key) },
conn.ws,
)
expect(conn.lastSentJson()).toHaveProperty('type', 'welcome')
expect(service.isRunnerConnected(runner.id)).toBe(false)
await expect(service.exec(runner.id, { command: 'echo ready' })).rejects.toThrow(
'Runner is not connected',
)
expect(conn.sent).toHaveLength(1)
reconcile(service, runner.id, conn.ws)
expect(service.isRunnerConnected(runner.id)).toBe(true)
})
it('evicts existing connection with 4000 when a new connection arrives', async () => {
const { runner, key } = await service.createRunner(teamId, { name: 'Runner WS' })
const keyHash = hashRunnerKey(key)
@@ -211,6 +286,7 @@ describe('RunnerService', () => {
{ id: runner.id, teamId, name: runner.name, keyHash },
conn1.ws,
)
reconcile(service, runner.id, conn1.ws)
expect(service.isRunnerConnected(runner.id)).toBe(true)
await service.registerConnection(
@@ -221,6 +297,7 @@ describe('RunnerService', () => {
code: 4000,
reason: 'Replaced by a newer connection',
})
reconcile(service, runner.id, conn2.ws)
expect(service.isRunnerConnected(runner.id)).toBe(true)
})
@@ -294,6 +371,7 @@ describe('RunnerService', () => {
{ id: r2.runner.id, teamId, name: r2.runner.name, keyHash: hashRunnerKey(r2.key) },
conn2.ws,
)
reconcile(service, r2.runner.id, conn2.ws)
const online = await service.requireOnlineRunner(teamId, r2.runner.id)
expect(online.id).toBe(r2.runner.id)
})
@@ -307,6 +385,7 @@ describe('RunnerService', () => {
{ id: runner.id, teamId, name: runner.name, keyHash: hashRunnerKey(key) },
conn.ws,
)
reconcile(service, runner.id, conn.ws)
const chunks: Array<{ stream: string; text: string }> = []
const execPromise = service.exec(runner.id, {
@@ -383,6 +462,7 @@ describe('RunnerService', () => {
conn.ws,
)
reconcile(service, runner.id, conn.ws)
const streamed: string[] = []
const noCollectPromise = service.exec(runner.id, {
command: 'yes | head -n 100',
@@ -456,6 +536,7 @@ describe('RunnerService', () => {
{ id: runner.id, teamId, name: runner.name, keyHash: hashRunnerKey(key) },
conn.ws,
)
reconcile(service, runner.id, conn.ws)
const abortController = new AbortController()
const execPromise = service.exec(runner.id, {
@@ -489,6 +570,121 @@ describe('RunnerService', () => {
expect(res.aborted).toBe(true)
})
it.each([false, true])(
'replays cancellation on reconnect after notifying caller=%s',
async (reported) => {
const { runner, key } = await service.createRunner(teamId, {
name: 'Cancelled while offline',
})
const authenticated = {
id: runner.id,
teamId,
name: runner.name,
keyHash: hashRunnerKey(key),
}
const conn = createMockWs()
await service.registerConnection(authenticated, conn.ws)
reconcile(service, runner.id, conn.ws)
const abort = new AbortController()
const execution = service.exec(runner.id, { command: 'sleep 30', signal: abort.signal })
const { executionId } = conn.lastSentJson()
service.handleDisconnect(runner.id, conn.ws)
abort.abort()
if (reported) await expect(execution).resolves.toMatchObject({ aborted: true })
const reconnected = createMockWs()
await service.registerConnection(authenticated, reconnected.ws)
expect(reconnected.lastSentJson()).toMatchObject({ inFlightExecutionIds: [executionId] })
reconcile(service, runner.id, reconnected.ws, [executionId])
const forwarded = reconnected.lastSentJson()
service.handleMessage(
runner.id,
reconnected.ws,
JSON.stringify({
type: 'exec.result',
executionId,
exitCode: 0,
signal: null,
timedOut: false,
}),
)
expect(forwarded).toEqual({ type: 'exec.cancel', executionId })
await expect(execution).resolves.toMatchObject({ aborted: true })
},
)
it('restages a generated input that was removed from the runner workspace', async () => {
vi.spyOn(s3Service, 'headObject').mockResolvedValue({
key: 'temp/generated/example/input.jpg',
size: 100,
lastModified: new Date(),
contentType: 'image/jpeg',
eTag: 'fixture',
})
vi.spyOn(s3Service, 'presign').mockResolvedValue('https://signed.example.com/input.jpg')
const { runner, key } = await service.createRunner(teamId, { name: 'Generated file runner' })
const conn = createMockWs()
await service.registerConnection(
{ id: runner.id, teamId, name: runner.name, keyHash: hashRunnerKey(key) },
conn.ws,
)
reconcile(service, runner.id, conn.ws)
const destPath = '.pi/generated/example/input.jpg'
const acknowledgeDownload = () => {
const download = conn.lastSentJson()
expect(download).toMatchObject({ type: 'file.download', destPath })
service.handleMessage(
runner.id,
conn.ws,
JSON.stringify({
type: 'file.download.result',
executionId: download.executionId,
ok: true,
}),
)
}
const first = service.ensureGeneratedFileOnRunner(runner.id, destPath)
await vi.waitFor(() => expect(conn.lastSentJson()?.type).toBe('file.download'))
acknowledgeDownload()
await expect(first).resolves.toBe(destPath)
const second = service.ensureGeneratedFileOnRunner(runner.id, destPath)
const stat = conn.lastSentJson()
expect(stat).toMatchObject({ type: 'file.stat', srcPath: destPath })
service.handleMessage(
runner.id,
conn.ws,
JSON.stringify({
type: 'file.stat.result',
executionId: stat.executionId,
ok: true,
exists: false,
}),
)
await vi.waitFor(() => expect(conn.lastSentJson()?.type).toBe('file.download'))
acknowledgeDownload()
await expect(second).resolves.toBe(destPath)
const third = service.ensureGeneratedFileOnRunner(runner.id, destPath)
const present = conn.lastSentJson()
expect(present).toMatchObject({ type: 'file.stat', srcPath: destPath })
service.handleMessage(
runner.id,
conn.ws,
JSON.stringify({
type: 'file.stat.result',
executionId: present.executionId,
ok: true,
exists: true,
isFile: true,
}),
)
await expect(third).resolves.toBe(destPath)
expect(conn.lastSentJson()).toEqual(present)
})
it('handles downloadToRunner, uploadFromRunner, statOnRunner, and session caching', async () => {
vi.spyOn(s3Service, 'presign').mockResolvedValue('https://signed.example.com/file')
const { runner, key } = await service.createRunner(teamId, { name: 'File Runner' })
@@ -497,6 +693,7 @@ describe('RunnerService', () => {
{ id: runner.id, teamId, name: runner.name, keyHash: hashRunnerKey(key) },
conn.ws,
)
reconcile(service, runner.id, conn.ws)
// 1. downloadToRunner
const dlPromise = service.downloadToRunner(runner.id, {
@@ -616,6 +813,7 @@ describe('RunnerService', () => {
{ id: runner.id, teamId, name: runner.name, keyHash },
conn1.ws,
)
reconcile(service, runner.id, conn1.ws)
const execPromise = service.exec(runner.id, { command: 'long-job' })
const startMsg = conn1.lastSentJson()
+131 -102
View File
@@ -12,6 +12,7 @@ import {
RunnerFileUploadResultMessage,
RunnerInfo,
RunnerEnvironment,
RUNNER_REPLACED_CLOSE_CODE,
RunnerToServerMessage,
runnerToServerMessageSchema,
ServerToRunnerMessage,
@@ -26,6 +27,7 @@ import type {
RunnerExecResult,
RunnerSocket,
} from './types'
import { RunnerTopology } from './runner-topology'
const RUNNER_KEY_PREFIX = 'shm_rn_'
const DEFAULT_RECONNECT_GRACE_MS = 30_000
@@ -60,6 +62,8 @@ interface PendingExecOperation {
resolve: (result: RunnerExecResult) => void
reject: (err: Error) => void
timeoutTimer?: ReturnType<typeof setTimeout>
abortTimer?: ReturnType<typeof setTimeout>
cancelRequested?: boolean
cleanupSignal?: () => void
}
@@ -132,11 +136,23 @@ export function validateRunnerWorkspacePath(targetPath: string): string {
export class RunnerService {
private sessions = new Map<string, ActiveRunnerSession>()
private closedSockets = new WeakSet<RunnerSocket>()
private pendingOps = new Map<string, PendingOperation>()
private disconnectGraceTimers = new Map<string, ReturnType<typeof setTimeout>>()
private credentialRevisions = new Map<string, number>()
private heartbeatTimer: ReturnType<typeof setInterval> | null = null
private reconnectGraceMs = DEFAULT_RECONNECT_GRACE_MS
private serverTopology?: RunnerTopology
async startServerTopology(): Promise<AbortSignal> {
this.serverTopology ??= await RunnerTopology.start()
return this.serverTopology.signal
}
async stopServerTopology(): Promise<void> {
await this.serverTopology?.close()
this.serverTopology = undefined
}
setReconnectGraceMs(ms: number): void {
this.reconnectGraceMs = ms
@@ -157,7 +173,7 @@ export class RunnerService {
this.handleDisconnect(runnerId, session.ws, 1001)
continue
}
this.sendToSession(session, { type: 'ping', ts: now })
if (session.environment) this.sendToSession(session, { type: 'ping', ts: now })
}
}, HEARTBEAT_INTERVAL_MS)
if (typeof this.heartbeatTimer.unref === 'function') {
@@ -173,7 +189,7 @@ export class RunnerService {
}
isRunnerConnected(runnerId: string): boolean {
return this.sessions.has(runnerId)
return this.getReadySession(runnerId) !== undefined
}
getRunnerEnvironment(runnerId: string): RunnerEnvironment | undefined {
@@ -203,6 +219,14 @@ export class RunnerService {
}
async createRunner(teamId: string, req: CreateRunnerRequest): Promise<CreateRunnerResponse> {
const create = () => this.persistRunner(teamId, req)
return this.serverTopology ? this.serverTopology.changeRunners(create, true) : create()
}
private async persistRunner(
teamId: string,
req: CreateRunnerRequest,
): Promise<CreateRunnerResponse> {
const name = req.name.trim()
const { key, keyPrefix, keyHash } = generateRunnerKey()
@@ -349,6 +373,11 @@ export class RunnerService {
}
async deleteRunner(teamId: string, runnerId: string): Promise<void> {
const remove = () => this.removeRunner(teamId, runnerId)
await (this.serverTopology ? this.serverTopology.changeRunners(remove) : remove())
}
private async removeRunner(teamId: string, runnerId: string): Promise<void> {
this.bumpCredentialRevision(runnerId)
await prisma.$transaction(async (tx) => {
@@ -472,6 +501,7 @@ export class RunnerService {
},
select: { id: true, teamId: true, name: true },
})
if (this.closedSockets.has(ws)) return
const currentRevision = this.credentialRevisions.get(runner.id) ?? 0
if (!verified || currentRevision !== startRevision) {
try {
@@ -482,19 +512,13 @@ export class RunnerService {
return
}
const existingTimer = this.disconnectGraceTimers.get(runner.id)
if (existingTimer) {
clearTimeout(existingTimer)
this.disconnectGraceTimers.delete(runner.id)
}
const existingSession = this.sessions.get(runner.id)
if (existingSession && existingSession.ws !== ws) {
logger.info({ runnerId: runner.id }, 'Evicting previous runner WebSocket connection')
// Remove from sessions before closing so handleDisconnect is a no-op for the old socket
this.sessions.delete(runner.id)
try {
existingSession.ws.close(4000, 'Replaced by a newer connection')
existingSession.ws.close(RUNNER_REPLACED_CLOSE_CODE, 'Replaced by a newer connection')
} catch {
// Ignore close errors
}
@@ -512,19 +536,6 @@ export class RunnerService {
this.sessions.set(runner.id, session)
this.startHeartbeatMonitor()
await prisma.runner
.update({
where: { id: runner.id },
data: { lastConnectedAt: new Date() },
})
.catch((err) => {
logger.warn({ err, runnerId: runner.id }, 'Failed to update runner lastConnectedAt')
})
if (this.sessions.get(runner.id) !== session) {
return
}
const inFlightExecutionIds = this.getInFlightExecutionIdsForRunner(runner.id)
this.sendToSession(session, {
type: 'welcome',
@@ -565,8 +576,13 @@ export class RunnerService {
case 'pong':
break
case 'reconcile':
session.environment = msg.environment
if (session.environment) break
this.handleReconcileMessage(session, msg)
session.environment = msg.environment
this.clearDisconnectGraceTimer(runnerId)
void prisma.runner
.update({ where: { id: runnerId }, data: { lastConnectedAt: new Date() } })
.catch((err) => logger.warn({ err, runnerId }, 'Failed to update runner lastConnectedAt'))
break
case 'exec.output':
this.handleExecOutputMessage(runnerId, msg)
@@ -587,6 +603,7 @@ export class RunnerService {
}
handleDisconnect(runnerId: string, ws: RunnerSocket, code?: number): void {
this.closedSockets.add(ws)
const session = this.sessions.get(runnerId)
if (!session || session.ws !== ws) {
return
@@ -608,6 +625,7 @@ export class RunnerService {
}
if (code === 1008 || this.reconnectGraceMs <= 0) {
this.clearDisconnectGraceTimer(runnerId)
this.failPendingOperationsForRunner(
runnerId,
new Error('Runner disconnected while operation was in progress'),
@@ -615,6 +633,7 @@ export class RunnerService {
return
}
if (this.disconnectGraceTimers.has(runnerId)) return
const timer = setTimeout(() => {
this.disconnectGraceTimers.delete(runnerId)
if (!this.isRunnerConnected(runnerId)) {
@@ -636,10 +655,7 @@ export class RunnerService {
// ----------------------------------------------------------------------
async exec(runnerId: string, options: RunnerExecOptions): Promise<RunnerExecResult> {
const session = this.sessions.get(runnerId)
if (!session) {
throw new Error('Runner is not connected')
}
const session = this.requireReadySession(runnerId)
if (options.signal?.aborted) {
throw new Error('Command aborted before execution started')
@@ -665,51 +681,22 @@ export class RunnerService {
const safetyTimeoutMs = Math.max((timeoutSec + 15) * 1000, 15_000)
const timeoutTimer = setTimeout(() => {
const pending = this.pendingOps.get(executionId)
if (!pending) return
if (pending?.kind !== 'exec') return
this.cleanupPendingOp(executionId)
const currentSession = this.sessions.get(runnerId)
const currentSession = this.getReadySession(runnerId)
if (currentSession) {
this.sendToSession(currentSession, { type: 'exec.cancel', executionId })
}
resolve({
executionId,
output: pending.kind === 'exec' ? this.readBufferedExecOutput(pending) : '',
output: this.readBufferedExecOutput(pending),
exitCode: null,
signal: 'SIGKILL',
timedOut: true,
aborted: false,
timedOut: !pending.cancelRequested,
aborted: pending.cancelRequested ?? false,
})
}, safetyTimeoutMs)
let cleanupSignal: (() => void) | undefined
if (options.signal) {
const onAbort = () => {
const currentSession = this.sessions.get(runnerId)
if (currentSession) {
this.sendToSession(currentSession, { type: 'exec.cancel', executionId })
}
// Give runner a short window to report exec.result, otherwise resolve as aborted
const abortTimer = setTimeout(() => {
const pending = this.pendingOps.get(executionId)
if (!pending) return
this.cleanupPendingOp(executionId)
resolve({
executionId,
output: pending.kind === 'exec' ? this.readBufferedExecOutput(pending) : '',
exitCode: null,
signal: 'SIGKILL',
timedOut: false,
aborted: true,
})
}, 2_000)
if (typeof abortTimer.unref === 'function') {
abortTimer.unref()
}
}
options.signal.addEventListener('abort', onAbort, { once: true })
cleanupSignal = () => options.signal?.removeEventListener('abort', onAbort)
}
const pendingOp: PendingExecOperation = {
kind: 'exec',
executionId,
@@ -724,7 +711,26 @@ export class RunnerService {
resolve,
reject,
timeoutTimer,
cleanupSignal,
}
if (options.signal) {
const signal = options.signal
const onAbort = () => {
pendingOp.cancelRequested = true
// Release the caller after a short grace period. Retain cancellation for reconnects.
pendingOp.abortTimer = setTimeout(() => {
this.reportCancelledExecution(pendingOp)
pendingOp.onOutput = undefined
pendingOp.collectOutput = false
pendingOp.outputChunks = []
pendingOp.bufferedOutputBytes = 0
}, 2_000)
pendingOp.abortTimer.unref?.()
const currentSession = this.getReadySession(runnerId)
if (currentSession)
this.sendToSession(currentSession, { type: 'exec.cancel', executionId })
}
signal.addEventListener('abort', onAbort, { once: true })
pendingOp.cleanupSignal = () => signal.removeEventListener('abort', onAbort)
}
this.pendingOps.set(executionId, pendingOp)
@@ -742,10 +748,7 @@ export class RunnerService {
timeoutMs?: number
},
): Promise<RunnerFileDownloadResultMessage> {
const session = this.sessions.get(runnerId)
if (!session) {
throw new Error('Runner is not connected')
}
const session = this.requireReadySession(runnerId)
const normalizedDestPath = validateRunnerWorkspacePath(options.destPath)
const executionId = ulid()
@@ -784,10 +787,7 @@ export class RunnerService {
srcPath: string,
timeoutMs = DEFAULT_FILE_OP_TIMEOUT_MS,
): Promise<RunnerFileStatResultMessage> {
const session = this.sessions.get(runnerId)
if (!session) {
throw new Error('Runner is not connected')
}
const session = this.requireReadySession(runnerId)
const normalizedSrcPath = validateRunnerWorkspacePath(srcPath)
const executionId = ulid()
@@ -826,10 +826,7 @@ export class RunnerService {
timeoutMs?: number
},
): Promise<RunnerFileUploadResultMessage> {
const session = this.sessions.get(runnerId)
if (!session) {
throw new Error('Runner is not connected')
}
const session = this.requireReadySession(runnerId)
const normalizedSrcPath = validateRunnerWorkspacePath(options.srcPath)
const executionId = ulid()
@@ -866,10 +863,7 @@ export class RunnerService {
runnerId: string,
skill: { id: string; assetId: string; hash: string },
): Promise<string> {
const session = this.sessions.get(runnerId)
if (!session) {
throw new Error('Runner is not connected')
}
const session = this.requireReadySession(runnerId)
const destPath = `.pi/skills/${skill.id}`
if (session.stagedSkills.get(skill.id) === skill.hash) {
@@ -910,10 +904,7 @@ export class RunnerService {
}
async ensureGeneratedFileOnRunner(runnerId: string, virtualPath: string): Promise<string> {
const session = this.sessions.get(runnerId)
if (!session) {
throw new Error('Runner is not connected')
}
const session = this.requireReadySession(runnerId)
const normalizedPath = validateRunnerWorkspacePath(virtualPath)
if (!normalizedPath.startsWith('.pi/generated/')) {
@@ -923,7 +914,10 @@ export class RunnerService {
}
if (session.stagedGeneratedFiles.has(normalizedPath)) {
return normalizedPath
const stat = await this.statOnRunner(runnerId, normalizedPath)
if (stat.ok && stat.exists && stat.isFile && this.getReadySession(runnerId) === session) {
return normalizedPath
}
}
// Virtual path .pi/generated/<ulid>/<filename> maps to storage key temp/generated/<ulid>/<filename>
@@ -961,34 +955,54 @@ export class RunnerService {
// Internal Helpers
// ----------------------------------------------------------------------
private getReadySession(runnerId: string): ActiveRunnerSession | undefined {
const session = this.sessions.get(runnerId)
return session?.environment ? session : undefined
}
private requireReadySession(runnerId: string): ActiveRunnerSession {
const session = this.getReadySession(runnerId)
if (!session) throw new Error('Runner is not connected')
return session
}
private handleReconcileMessage(
session: ActiveRunnerSession,
msg: Extract<RunnerToServerMessage, { type: 'reconcile' }>,
): void {
const completedIds = new Set<string>()
for (const completed of msg.completedExecutions) {
completedIds.add(completed.executionId)
this.handleExecResultMessage(session.runnerId, completed)
}
const activeIds = new Set(msg.activeExecutions.map((a) => a.executionId))
for (const executionId of activeIds) {
const pending = this.pendingOps.get(executionId)
if (
pending?.runnerId !== session.runnerId ||
pending.kind !== 'exec' ||
pending.cancelRequested
) {
// A timeout or cancellation may have occurred after the welcome snapshot.
this.sendToSession(session, { type: 'exec.cancel', executionId })
}
}
for (const [executionId, pending] of this.pendingOps.entries()) {
if (pending.runnerId !== session.runnerId) continue
if (completedIds.has(executionId) || activeIds.has(executionId)) {
continue
}
if (pending.kind === 'exec') {
if (pending.kind !== 'exec') {
// Re-send idempotent file control operations on reconnect.
this.sendToSession(session, pending.message)
} else if (!activeIds.has(executionId)) {
this.cleanupPendingOp(executionId)
if (pending.cancelRequested) {
this.reportCancelledExecution(pending)
continue
}
pending.reject(
new Error(
`Runner reconnected without active execution ${executionId} (runner process may have restarted)`,
),
)
} else {
// Re-send idempotent file control operations on reconnect
this.sendToSession(session, pending.message)
}
}
}
@@ -1031,8 +1045,8 @@ export class RunnerService {
output: this.readBufferedExecOutput(pending),
exitCode: msg.exitCode,
signal: msg.signal,
timedOut: msg.timedOut,
aborted: msg.aborted ?? false,
timedOut: msg.timedOut && !pending.cancelRequested,
aborted: pending.cancelRequested || (msg.aborted ?? false),
fullOutputPath: msg.fullOutputPath,
error: msg.error,
})
@@ -1133,11 +1147,7 @@ export class RunnerService {
private revokeActiveSession(runnerId: string, reason: string): void {
this.bumpCredentialRevision(runnerId)
const timer = this.disconnectGraceTimers.get(runnerId)
if (timer) {
clearTimeout(timer)
this.disconnectGraceTimers.delete(runnerId)
}
this.clearDisconnectGraceTimer(runnerId)
const session = this.sessions.get(runnerId)
if (session) {
@@ -1152,6 +1162,24 @@ export class RunnerService {
this.failPendingOperationsForRunner(runnerId, new Error(reason))
}
private clearDisconnectGraceTimer(runnerId: string): void {
const timer = this.disconnectGraceTimers.get(runnerId)
if (!timer) return
clearTimeout(timer)
this.disconnectGraceTimers.delete(runnerId)
}
private reportCancelledExecution(pending: PendingExecOperation): void {
pending.resolve({
executionId: pending.executionId,
output: this.readBufferedExecOutput(pending),
exitCode: null,
signal: 'SIGKILL',
timedOut: false,
aborted: true,
})
}
private failPendingOperationsForRunner(runnerId: string, error: Error): void {
for (const [executionId, pending] of this.pendingOps.entries()) {
if (pending.runnerId === runnerId) {
@@ -1168,8 +1196,9 @@ export class RunnerService {
if (pending.timeoutTimer) {
clearTimeout(pending.timeoutTimer)
}
if (pending.kind === 'exec' && pending.cleanupSignal) {
pending.cleanupSignal()
if (pending.kind === 'exec') {
clearTimeout(pending.abortTimer)
pending.cleanupSignal?.()
}
}
@@ -0,0 +1,76 @@
import { HTTPException } from 'hono/http-exception'
import { prisma } from '@shumai/db'
import { tryAcquireUpgradeableAdvisoryLock } from '@shumai/db/src/advisory-lock'
import { logger } from '@shumai/core/src/logger'
const RUNNER_TOPOLOGY_KEY = 'shumai:runner-topology'
const SINGLE_SERVER_REQUIRED =
'Agent runners require a single main Shumai server per database. Stop the other main server replicas.'
async function hasConfiguredRunners() {
return (await prisma.runner.findFirst({ select: { id: true } })) !== null
}
/** Each main server holds a shared lease. A configured runner requires an exclusive lease. */
export class RunnerTopology {
private mutations = Promise.resolve()
private constructor(
private readonly lease: NonNullable<
Awaited<ReturnType<typeof tryAcquireUpgradeableAdvisoryLock>>
>,
) {}
static async start() {
const configured = await hasConfiguredRunners()
let lease = await tryAcquireUpgradeableAdvisoryLock(RUNNER_TOPOLOGY_KEY, configured)
if (!lease && configured) {
// If the last runner was just deleted, the incumbent may now hold a shared lease.
lease = await tryAcquireUpgradeableAdvisoryLock(RUNNER_TOPOLOGY_KEY, false)
}
if (!lease) throw new Error(SINGLE_SERVER_REQUIRED)
try {
// A concurrently stopping server may have changed the catalog.
if (await hasConfiguredRunners()) {
if (!(await lease.tryUpgrade())) throw new Error(SINGLE_SERVER_REQUIRED)
} else {
await lease.downgrade()
}
return new RunnerTopology(lease)
} catch (error) {
await lease.release()
throw error
}
}
get signal() {
return this.lease.signal
}
/** Serialize runner creation and deletion until the exclusive lease can be safely downgraded. */
async changeRunners<T>(operation: () => Promise<T>, adding = false): Promise<T> {
const previous = this.mutations
const finished = Promise.withResolvers<void>()
this.mutations = finished.promise
await previous
try {
if (adding && !(await this.lease.tryUpgrade())) {
throw new HTTPException(409, { message: SINGLE_SERVER_REQUIRED })
}
return await operation()
} finally {
try {
if (this.lease.exclusive && !(await hasConfiguredRunners())) await this.lease.downgrade()
} catch (error) {
logger.warn({ err: error }, 'Failed to downgrade the runner server lease')
} finally {
finished.resolve()
}
}
}
async close() {
await this.mutations
await this.lease.release()
}
}
+44
View File
@@ -717,6 +717,50 @@ describe('S3Service implementations', () => {
expect(keysAll.length).toBe(3)
})
it('lists a nested prefix without scanning unrelated directories', async () => {
await localS3.putObject('test-bucket', 'temp/generated/one.jpg', 'one', 3)
await localS3.putObject('test-bucket', 'files/unrelated.txt', 'abc', 3)
const readDirectory = vi.spyOn(fs.promises, 'readdir')
try {
await expect(localS3.listObjects('test-bucket', 'temp/generated/')).resolves.toEqual([
'temp/generated/one.jpg',
])
expect(readDirectory.mock.calls.map(([directory]) => directory)).toEqual([
path.join(TEST_BASE_PATH, 'test-bucket', 'temp', 'generated'),
])
readDirectory.mockClear()
await expect(localS3.listObjects('test-bucket', 'temp/missing/')).resolves.toEqual([])
expect(readDirectory.mock.calls.map(([directory]) => directory)).not.toContain(
path.join(TEST_BASE_PATH, 'test-bucket'),
)
await expect(localS3.listObjects('test-bucket', 'temp/generated/on')).resolves.toEqual([
'temp/generated/one.jpg',
])
} finally {
readDirectory.mockRestore()
}
})
it('continues listing when a file disappears during a local prefix scan', async () => {
const removedKey = 'temp/generated/removed.jpg'
const remainingKey = 'temp/generated/remaining.jpg'
await localS3.putObject('test-bucket', removedKey, 'old', 3)
await localS3.putObject('test-bucket', remainingKey, 'new', 3)
const stat = fs.promises.stat
const removedPath = path.join(TEST_BASE_PATH, 'test-bucket', removedKey)
const statSpy = vi.spyOn(fs.promises, 'stat').mockImplementation(async (target, options) => {
if (target === removedPath) await fs.promises.unlink(removedPath)
return stat(target, options)
})
try {
await expect(localS3.listObjects('test-bucket', 'temp/generated/')).resolves.toEqual([
remainingKey,
])
} finally {
statSpy.mockRestore()
}
})
it('should handle headObject correctly', async () => {
await localS3.putObject('test-bucket', 'test.txt', 'hello world', 11)
const head = await localS3.headObject('test-bucket', 'test.txt')
+23 -6
View File
@@ -1012,16 +1012,33 @@ export class LocalStorageService implements S3Service {
async listObjects(bucket: string, prefix: string): Promise<string[]> {
const bucketDir = path.join(this.basePath, bucket)
const keys: string[] = []
if (!fs.existsSync(bucketDir)) {
const directoryPrefix = prefix.slice(0, prefix.lastIndexOf('/') + 1)
const directory = path.join(bucketDir, directoryPrefix.slice(0, -1))
const relativeDirectory = path.relative(bucketDir, directory).split(path.sep).join('/')
if (
directoryPrefix &&
(!relativeDirectory ||
relativeDirectory === '..' ||
relativeDirectory.startsWith('../') ||
relativeDirectory + '/' !== directoryPrefix)
)
return keys
}
const ignoreMissing = (error: unknown) => {
if (
error instanceof Error &&
'code' in error &&
(error.code === 'ENOENT' || error.code === 'ENOTDIR')
)
return undefined
throw error
}
const walkDir = async (currentDir: string) => {
const files = await fs.promises.readdir(currentDir)
const files = (await fs.promises.readdir(currentDir).catch(ignoreMissing)) ?? []
for (const file of files) {
const fullPath = path.join(currentDir, file)
const stat = await fs.promises.stat(fullPath)
const stat = await fs.promises.stat(fullPath).catch(ignoreMissing)
if (!stat) continue
if (stat.isDirectory()) {
await walkDir(fullPath)
@@ -1037,7 +1054,7 @@ export class LocalStorageService implements S3Service {
}
}
await walkDir(bucketDir)
await walkDir(directory)
return keys
}