Files
ycode/src/main/sandbox/execution.ts
T
xiaohan.yuan b2d37472f0 fix(sandbox): support real development workspaces
- raise finite development defaults/ceilings and queue capacities
- migrate untouched v1 default policy
- exclude generated output and shipped runtime paths from snapshots
- report snapshot limit errors with the offending path
2026-10-18 18:08:20 +08:00

39 lines
1.8 KiB
TypeScript

import { promises as fs } from 'node:fs'
import { assertActive } from './policy'
interface Waiting { workspace: string; start(): void; cancel(): void }
const queue: Waiting[] = [], busy = new Set<string>()
const maximumActive = 4, maximumQueued = 64
function drain(): void {
for (let index = 0; index < queue.length && busy.size < maximumActive;) {
const item = queue[index]
if (busy.has(item.workspace)) { index++; continue }
queue.splice(index, 1); busy.add(item.workspace); item.start()
}
}
/** A workspace snapshot observes the previous approved commit, never a concurrent stale copy. */
export async function withSandboxExecution<T>(root: string, signal: AbortSignal | undefined, operation: (canonicalRoot: string) => Promise<T>): Promise<T> {
assertActive(signal)
const real = await fs.realpath(root), workspace = process.platform === 'win32' ? real.toLowerCase() : real
if (queue.length >= maximumQueued || queue.filter(item => item.workspace === workspace).length >= 12) throw new Error('沙盒执行队列已满,请稍后重试')
return new Promise<T>((done, reject) => {
const item: Waiting = {
workspace,
start() {
signal?.removeEventListener('abort', item.cancel)
Promise.resolve().then(() => { assertActive(signal); return operation(real) }).then(done, reject).finally(() => { busy.delete(workspace); drain() })
},
cancel() {
const index = queue.indexOf(item)
if (index < 0) return
queue.splice(index, 1); signal?.removeEventListener('abort', item.cancel)
reject(Object.assign(new Error('排队中的沙盒执行已取消'), { name: 'AbortError' }))
}
}
queue.push(item)
signal?.addEventListener('abort', item.cancel, { once: true })
if (signal?.aborted) item.cancel()
drain()
})
}