The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.
import { Hono } from 'hono'
import type { LogLine } from './db'
import { admitLogs, apiRateLimit, limitsOf, setLimits, sseBusy, sseSlot, uploadSlot } from './limits'
describe('App request limits', () => {
afterEach(() => jest.useRealTimers())
test('budgets follow the credential, refill over time and exempt snapshot file transfers', async () => {
let time = 0
const ctx = {}
const server = new Hono().use('*', apiRateLimit(ctx)).all('*', (c) => c.text('ok'))
const request = (token?: string, path = '/v1/apps', method = 'GET') =>
server.request(path, { method, headers: token ? { authorization: 'Bearer ' + token } : {} })
setLimits(ctx, { apiRequestsPerSecond: 0.5, apiBurst: 1 }, () => time)
expect((await request('first')).status).toBe(200)
const full = await request('first')
expect(full.status).toBe(429)
expect(full.headers.get('retry-after')).toBe('2')
expect(await full.json()).toMatchObject({
error: { code: 'rate_limited', retry_after: 2, next: 'wait 2 s and retry' },
})
expect((await request('second')).status).toBe(200)
expect((await request()).status).toBe(200)
expect((await request('first', '/v1/apps/demo/uploads/u1/files/abc', 'PUT')).status).toBe(200)
time = 1999
expect((await request('first')).status).toBe(429)
time++
expect((await request('first')).status).toBe(200)
})
test('a full environment or key rejects the whole subscription and one close releases exactly one slot', async () => {
const ctx = {}
const state = setLimits(ctx, { sseSubscribersPerEnv: 2, sseSubscribersPerKey: 1, uploadsInFlightPerApp: 1 })
const first = sseSlot(ctx, 'main', 'a')!
expect(sseSlot(ctx, 'staging', 'a')).toBeNull()
expect(state.gate.count('sse:env:staging')).toBe(0)
const second = sseSlot(ctx, 'main', null)!
expect(sseSlot(ctx, 'main', 'b')).toBeNull()
const server = new Hono().get('/', (c) => sseBusy(c, ctx))
const busy = await server.request('/')
expect(busy.status).toBe(429)
expect(busy.headers.get('retry-after')).toBe('5')
first()
first()
expect(state.gate.count('sse:env:main')).toBe(1)
second()
expect(state.gate.count('sse:env:main')).toBe(0)
const release = uploadSlot(ctx, 'app')!
expect(uploadSlot(ctx, 'app')).toBeNull()
expect(uploadSlot(ctx, 'other')).toEqual(expect.any(Function))
release()
expect(uploadSlot(ctx, 'app')).toEqual(expect.any(Function))
expect(limitsOf({}).limits.sseSubscribersPerEnv).toBeGreaterThan(2)
})
test('evicts idle credentials at capacity while preserving the budget of an active key', () => {
const state = setLimits({}, { apiBurst: 1, apiRequestsPerSecond: 1 }, () => 0)
const idle = state.apiBucket('idle')
expect(state.apiBucket('active').take()).toBe(1)
for (let i = 0; i < 9_999; i++) state.apiBucket('client-' + i)
expect(state.apiBucket('active').take()).toBe(0)
expect(state.apiBucket('idle')).not.toBe(idle)
expect(state.apiBucket('idle').take()).toBe(1)
})
test('log ingestion bounds batches and UTF-8 bytes, and flushes suppressed drops after the burst', async () => {
jest.useFakeTimers({ now: 0 })
const ctx = {}
setLimits(ctx, { logLinesPerRequest: 2, logBurst: 3, logLinesPerSecond: 1, logLineBytes: 3, logDropWarnMs: 500 })
const stored: LogLine[] = []
const record = async (lines: LogLine[]) => {
stored.push(...lines)
}
const line = (line: string): LogLine => ({ ts: '2026-01-01T00:00:00Z', source: 'web', level: 'info', line })
const push = (env: string, ...lines: string[]) => admitLogs(ctx, env, lines.map(line), record)
expect(await push('main', 'éé', 'abc', 'four')).toMatchObject({
accepted: [{ line: 'é … [truncated 2 bytes]' }, { line: 'abc' }],
dropped: 1,
})
expect(await push('main', 'one', 'two')).toMatchObject({ accepted: [{ line: 'one' }], dropped: 1 })
expect(await push('main', 'a', 'b')).toEqual({ accepted: [], dropped: 2 })
expect((await push('dev', 'one')).dropped).toBe(0)
expect(stored.filter((l) => l.source === 'system')).toHaveLength(1)
await jest.advanceTimersByTimeAsync(500)
expect(stored.filter((l) => l.source === 'system').map((l) => l.line)).toEqual([
expect.stringContaining('dropped 1 line'),
expect.stringContaining('dropped 3 line'),
])
await jest.advanceTimersByTimeAsync(500)
expect((await push('main', 'ok')).dropped).toBe(0)
})
})