mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
Merge pull request #787 from nwparker/cloud/create-step-watch
chore(sandbox-control): name the step a stuck create waits in
This commit is contained in:
@@ -891,6 +891,34 @@ function fail(error: string, status: number, reason?: string): Response {
|
||||
return json(reason ? { error, reason } : { error }, status)
|
||||
}
|
||||
|
||||
/** CREATE-STEP-WATCH: logs a step still pending after 10 s and, once the create starts, every step's time. */
|
||||
export function createStepWatch(enabled: boolean, now: () => number = Date.now) {
|
||||
const started = now()
|
||||
const steps: Record<string, number> = {}
|
||||
let reported = false
|
||||
return {
|
||||
async step<T>(name: string, work: Promise<T>): Promise<T> {
|
||||
if (!enabled) {
|
||||
return work
|
||||
}
|
||||
const at = now()
|
||||
const slow = setTimeout(() => console.log(JSON.stringify({ event: 'create_step_slow', step: name, ms: now() - at })), 10_000)
|
||||
try {
|
||||
return await work
|
||||
} finally {
|
||||
clearTimeout(slow)
|
||||
steps[name] = now() - at
|
||||
}
|
||||
},
|
||||
done() {
|
||||
if (enabled && !reported) {
|
||||
reported = true
|
||||
console.log(JSON.stringify({ event: 'create_steps', ms: now() - started, steps }))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function readJson(request: Request, maxBytes = MAX_JSON_BYTES): Promise<unknown> {
|
||||
const text = await request.text()
|
||||
if (text.length > maxBytes) {
|
||||
@@ -1809,11 +1837,13 @@ async function routeRequest(request: Request, env: Env, deps: Deps): Promise<Res
|
||||
if (!match) {
|
||||
return fail('not_found', 404)
|
||||
}
|
||||
const caller = await authenticateDesktop(env, request, (deps.now ?? Date.now)())
|
||||
// CREATE-STEP-WATCH: creates hung 180 s with no log and no row (n73). Name the step a create waits in.
|
||||
const watch = createStepWatch(request.method === 'POST' && !match[1])
|
||||
const caller = await watch.step('auth', authenticateDesktop(env, request, (deps.now ?? Date.now)()))
|
||||
if (!caller) {
|
||||
return fail('unauthorized', 401)
|
||||
}
|
||||
const life = await lifecycleDeps(env, deps)
|
||||
const life = await watch.step('deps', lifecycleDeps(env, deps))
|
||||
if (!life) {
|
||||
return fail('not_configured', 503)
|
||||
}
|
||||
@@ -1836,12 +1866,12 @@ async function routeRequest(request: Request, env: Env, deps: Deps): Promise<Res
|
||||
if (request.method !== 'POST') {
|
||||
return fail('not_found', 404)
|
||||
}
|
||||
if (await tokenMintedBeforeMemberChange(life.db, caller)) {
|
||||
if (await watch.step('stale_token', tokenMintedBeforeMemberChange(life.db, caller))) {
|
||||
// ORG-TOKEN-STALE: never create (and bill) with a token minted before a role change.
|
||||
return fail('unauthorized', 401, 'stale_token')
|
||||
}
|
||||
await consumeCreateToken(life.db, life.config.guardrails!, caller.accountId, life.now())
|
||||
const raw = await readJson(request)
|
||||
await watch.step('create_token', consumeCreateToken(life.db, life.config.guardrails!, caller.accountId, life.now()))
|
||||
const raw = await watch.step('body', readJson(request))
|
||||
const body = CreateRequestSchema.safeParse(raw)
|
||||
if (!body.success) {
|
||||
return fail('invalid_request', 400)
|
||||
@@ -1859,6 +1889,7 @@ async function routeRequest(request: Request, env: Env, deps: Deps): Promise<Res
|
||||
life.db,
|
||||
{ accountId: caller.accountId, scope: `sandbox.create:${caller.userId}`, key: readIdempotencyKey(request), body: raw, now: life.now() },
|
||||
async () => {
|
||||
watch.done()
|
||||
// Desktops that show the clone as its own step ask for it in the background (GET .../clone).
|
||||
// Member identities install after the response; the VM's git holds commits and pushes until then.
|
||||
let ready: Ready
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
// CREATE-STEP-WATCH: creates hung 180 s with no log and no row (n73); the steps before the create log.
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { createStepWatch } from '../src/index'
|
||||
|
||||
describe('CREATE-STEP-WATCH', () => {
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
it('names a step still pending after 10 s, then reports every step once the create starts', async () => {
|
||||
vi.useFakeTimers()
|
||||
const log = vi.spyOn(console, 'log').mockImplementation(() => undefined)
|
||||
const watch = createStepWatch(true, () => Date.now())
|
||||
let finish: (value: string) => void = () => undefined
|
||||
const body = watch.step('body', new Promise<string>((resolve) => (finish = resolve)))
|
||||
await vi.advanceTimersByTimeAsync(10_000)
|
||||
expect(log).toHaveBeenCalledWith(expect.stringContaining('"step":"body"'))
|
||||
finish('{}')
|
||||
await expect(body).resolves.toBe('{}')
|
||||
watch.done()
|
||||
watch.done()
|
||||
const reports = log.mock.calls.filter(([line]) => String(line).includes('"create_steps"'))
|
||||
expect(reports).toHaveLength(1)
|
||||
expect(String(reports[0]![0])).toContain('"body":10000')
|
||||
})
|
||||
|
||||
it('stays quiet for other routes', async () => {
|
||||
vi.useFakeTimers()
|
||||
const log = vi.spyOn(console, 'log').mockImplementation(() => undefined)
|
||||
const watch = createStepWatch(false)
|
||||
const pending = watch.step('auth', new Promise(() => undefined))
|
||||
await vi.advanceTimersByTimeAsync(20_000)
|
||||
watch.done()
|
||||
expect(log).not.toHaveBeenCalled()
|
||||
void pending
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user