{"record":{"id":"7a101ba09e27ae64","repo":"JuliusBrussee/caveman","slug":"code-runtime","errorCode":null,"errorMessage":"code","messagePattern":"code","errorType":"error_code","errorClass":"MiddlewareError","httpStatus":null,"severity":"warning","filePath":"packages/sdk/python/caveman_cloud/middleware/runtime.py","lineNumber":352,"sourceCode":"    def __exit__(self, *_):\n        self.close()\n\n    def decline(self, reason: str) -> Optimization:\n        \"\"\"Report an untested native framework without sending content or I/O.\"\"\"\n        if reason != \"unsupported_version\":\n            raise ValueError(\"Unsupported adapter diagnostic\")\n        if self.mode == \"off\":\n            return Optimization(\"off\", \"disabled\", (), None, None, \"off\")\n        return self._bypass(reason)\n\n    def _bypass(self, code: str, diagnostic: bool = True) -> Optimization:\n        if diagnostic and self._diagnostic:\n            try:\n                self._diagnostic({\"code\": code, \"cache_continuity\": \"unavailable\"})\n            except Exception:\n                pass\n        if self.strict and diagnostic:\n            raise MiddlewareError(code)\n        return Optimization(\"bypassed\", code)\n\n    def _http(self, path: str, body: str | None, timeout: float) -> Any:\n        if self._closed:\n            raise MiddlewareError(\"closed\")\n        headers = {\"Content-Type\": \"application/json\"}\n        if self._token:\n            headers[\"Authorization\"] = f\"Bearer {self._token}\"\n        started = time.monotonic()\n        cls = http.client.HTTPSConnection if self._url.scheme == \"https\" else http.client.HTTPConnection\n        connection = cls(self._url.hostname, self._url.port, timeout=max(0.001, timeout))\n        with self._lock:\n            if self._closed:\n                raise MiddlewareError(\"closed\")\n            self._connections.add(connection)\n        try:\n            connection.connect()\n            sock = connection.sock","sourceCodeStart":334,"sourceCodeEnd":370,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/sdk/python/caveman_cloud/middleware/runtime.py#L334-L370","documentation":"In strict mode, _bypass() re-raises the bypass code as MiddlewareError(code) after invoking the diagnostic callback, instead of returning a bypassed Optimization. Callers of optimize()/decline() configured with strict=True get an exception for any bypass condition (capacity, off, unsupported_version, etc.).","triggerScenarios":"Running with strict=True and hitting a bypass path: capacity exhausted, mode off, or decline(\"unsupported_version\") with diagnostic=True; also when the diagnostic callback itself raises (it is swallowed) but strict still escalates.","commonSituations":"Production strict deployments where the middleware service reports no capacity; teams enabling strict mode without monitoring the diagnostic hook.","solutions":["Catch MiddlewareError from optimize()/decline() and fall back to unoptimized behavior.","Set strict=False if bypasses should be returned as Optimization(\"bypassed\", ...) rather than raised.","Fix the underlying cause indicated by the code attribute (e.g. restore service capacity, enable the middleware)."],"exampleFix":"// before\nresult = runtime.optimize(scope, prompt)  # strict mode -> raises\n// after\ntry:\n    result = runtime.optimize(scope, prompt)\nexcept MiddlewareError as e:\n    result = None  # proceed without optimization","handlingStrategy":"try-catch","validationCode":"def strict_enabled(rt) -> bool:\n    return bool(getattr(rt, \"strict\", False))","typeGuard":"None","tryCatchPattern":"try:\n    result = rt.optimize(scope, prompt)\nexcept MiddlewareError as e:\n    result = None  # strict-mode bypass surfaced as error; proceed unoptimized","preventionTips":["Only enable strict=True when callers are prepared to handle MiddlewareError on every bypass.","Monitor the diagnostic hook to detect frequent bypasses before they become exceptions.","Document per-code handling for capacity/off/unsupported_version in strict deployments."],"tags":["strict-mode","bypass","python"],"backgroundTag":"feature-not-enabled","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"}