{"record":{"id":"5ba92e729df2f6cd","repo":"JuliusBrussee/caveman","slug":"deadline","errorCode":"deadline","errorMessage":"deadline","messagePattern":"deadline","errorType":"error_code","errorClass":"MiddlewareError","httpStatus":null,"severity":"error","filePath":"packages/sdk/typescript/src/middleware/runtime.ts","lineNumber":198,"sourceCode":"      attempt_id: token(context.attemptId ?? optimization?.request?.attempt_id),\n    });\n    this.reported = event;\n    try {\n      const completion = this.options.onReport?.(event);\n      if (completion) void Promise.resolve(completion).catch(() => {});\n    } catch { /* A reporting sink cannot change native behavior. */ }\n    return event;\n  }\n\n  async optimize(options: OptimizeOptions): Promise<Optimization> {\n    options.signal?.throwIfAborted();\n    if (this.mode === 'off') return { status: 'off', reason: 'off', replacements: [], plan: null, request: null, cacheContinuity: 'off' };\n    if (Date.now() < this.openUntil) return this.bypass('circuit_open');\n    if (this.pending >= 16) return this.bypass('capacity');\n    this.pending++;\n    const start = performance.now();\n    const deadline = this.options.deadlineMs ?? 100;\n    const remaining = () => { const ms = deadline - (performance.now()-start); if (ms <= 0) throw new MiddlewareError('deadline'); return Math.ceil(ms); };\n    try {\n      scopeKey(options.scope);\n      const caps = this.capsCache.value ?? validateCapabilities(await this.http('capabilities', undefined, remaining(), options.signal));\n      this.capsCache.value = caps;\n      if (options.candidates.length > 256 || options.manifest.length > 4096) return this.bypass('payload_limit');\n      const ids = new Set<string>();\n      const segments = [];\n      for (const candidate of options.candidates) {\n        if (!isToken(candidate.id) || ids.has(candidate.id) || !candidate.content.isWellFormed()) return this.bypass('unsupported_shape');\n        ids.add(candidate.id);\n        // Never transfer a protected or oversized leaf merely to get a skip.\n        if (candidate.protected || candidate.opaque || byteLength(candidate.content) > caps.limits.segment_bytes) continue;\n        segments.push({ id: candidate.id, source_id: candidate.sourceId ?? candidate.id, kind: candidate.kind ?? 'tool_result' as const,\n          cache_region: candidate.cacheRegion ?? 'live_zone' as const, content: candidate.content, sha256: await sha256(candidate.content), protected: false, opaque: false });\n      }\n      if (segments.length === 0) return this.bypass('no_candidate', false);\n      let binding: BindingWire | null = null;\n      if (this.ownsBinding(options.binding, options.scope)) {","sourceCodeStart":180,"sourceCodeEnd":216,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/sdk/typescript/src/middleware/runtime.ts#L180-L216","documentation":"MiddlewareError('deadline') is thrown inside optimize() by the remaining() helper once the configured deadlineMs budget (default 100ms) has elapsed. The runtime aborts further optimization work — including capability discovery — rather than adding latency to the model call. If strict mode is on this error propagates; otherwise it surfaces as a bypass diagnostic.","triggerScenarios":"performance.now() minus start exceeds deadlineMs during optimize(); capability discovery via this.http('capabilities') has to happen inside optimize() because the caps cache is cold and the fetch exhausts the remaining budget.","commonSituations":"First request after startup without a prior ready() call, so capability discovery eats the whole budget; a slow or remote runtime endpoint exceeding the tight 100ms default; calling optimize() on a stalled event loop.","solutions":["Call await runtime.ready() (or the nonthrowing variant) at startup to warm the caps cache","Raise options.deadlineMs to a realistic budget for your network","Ensure the runtime endpoint is local and fast; remote endpoints should be avoided or well provisioned","Catch MiddlewareError with code 'deadline' and fall back to sending the unoptimized request"],"exampleFix":"// before\nawait runtime.ready(); // missing; first optimize() pays discovery cost\n// after\nconst runtime = createMiddlewareRuntime({ deadlineMs: 250 });\nawait runtime.ready(); // warm caps cache outside the model call","handlingStrategy":"try-catch","validationCode":"if (!runtime.capsCache?.value) await runtime.ready(); // warm before model calls","typeGuard":null,"tryCatchPattern":"try { const plan = await runtime.optimize(req); } catch (e) { if (e instanceof MiddlewareError && e.code === 'deadline') { /* send request unoptimized or retry */ } else throw e; }","preventionTips":["Always call ready() at app startup to warm the caps cache","Set deadlineMs generously enough for your network (remote endpoints need more than 100ms)","Monitor onDiagnostic for repeated deadline bypasses"],"tags":["timeout","performance","network"],"backgroundTag":"request-timeout","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}