Skip to content

Revert "chore(ai-gateway): remove Cloudflare analytics (#6084)" - #7071

Merged
chrarnoldus merged 4 commits into
mainfrom
revert/6084-ai-gateway-observability
Oct 1, 2026
Merged

chrarnoldus merged 4 commits into
mainfrom
revert/6084-ai-gateway-observability

Conversation

@chrarnoldus

@chrarnoldus chrarnoldus commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Reverts #6084.

Notes

git revert did not apply cleanly: since #6084 the gateway routes became thin facades (#6737) and the handler logic moved to packages/web-shared (#6806). This restores the same behavior in the new layout:

  • Restores api-metrics.server.ts and its test under packages/web-shared/src/lib/ai-gateway/o11y/ (web-shared runtime code may only import from web-shared).
  • Re-adds the emitApiMetricsForResponse calls in the shared llm-proxy, embeddings, and audio-transcriptions handlers.
  • Restores toolsAvailable / toolsUsed on the successful sendUpstreamAttempt result.
  • Security Agent triage/extraction go back to the shared emitApiMetrics; the local copy is removed.
  • Route files under apps/web/src/app/api/openrouter/ stay unchanged.

Additions beyond the revert

  • System One (/api/gateway/typesafe/v1/systemone) now emits API metrics too. Its body is read as JSON, so it reports known values through a new emitGatewayApiMetrics helper instead of cloning and draining the response (a clone would keep the upstream open after the 402 path cancels it).
  • FIM (/api/fim/completions) and edit (/api/edit/completions) completions now emit API metrics. They call Mistral and Inception directly, so these metrics are the only provider health signal for those paths. userByok reflects whether a BYOK key was actually used.
  • The response drain uses one deadline timer instead of racing a new setTimeout per chunk (review feedback).

Verification

  • pnpm --filter @kilocode/web-shared typecheck passes.
  • api-metrics.server.test.ts, typesafe/handler.test.ts, fim-completions.test.ts, and edit-completions.test.ts pass locally; remaining tests left to CI.

This reverts commit 3fbc18a.

The gateway handlers have since moved to packages/web-shared, so the
restored API metrics module lives in
packages/web-shared/src/lib/ai-gateway/o11y and the emit calls are
re-applied in the shared llm-proxy, embeddings, and audio-transcriptions
handlers. The route files stay thin facades.
@chrarnoldus chrarnoldus self-assigned this Oct 1, 2026
@kilo-code-bot

kilo-code-bot Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (4 files)
  • packages/web-shared/src/lib/ai-gateway/handlers/edit-completions.ts
  • packages/web-shared/src/lib/ai-gateway/handlers/edit-completions.test.ts
  • packages/web-shared/src/lib/ai-gateway/handlers/fim-completions.ts
  • packages/web-shared/src/lib/ai-gateway/handlers/fim-completions.test.ts

Note: the previously reported un-cleared drain-timer finding in o11y/api-metrics.server.ts is resolved on current HEAD (single deadline timer cleared in finally).

Previous Review Summaries (2 snapshots, latest commit 576d403)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit 576d403)

Status: No Issues Found | Recommendation: Merge

Files Reviewed (4 files)
  • packages/web-shared/src/lib/ai-gateway/o11y/api-metrics.server.ts
  • packages/web-shared/src/lib/ai-gateway/o11y/api-metrics.server.test.ts
  • packages/web-shared/src/lib/ai-gateway/typesafe/handler.ts
  • packages/web-shared/src/lib/ai-gateway/typesafe/handler.test.ts

Previous review (commit f9d27e6)

Status: 1 Issue Found | Recommendation: Address before merge

Executive Summary

This revert faithfully restores the removed API-metrics path (the new api-metrics.server.ts and its test are byte-identical to the pre-#6084 originals, and the emitApiMetricsForResponse calls and toolsAvailable/toolsUsed fields match the old route code); the only new finding is an un-cleared timeout timer in the response-drain loop that can retain pending timers under sustained streaming.

Overview

Severity Count
CRITICAL 0
WARNING 0
SUGGESTION 1
Issue Details (click to expand)

SUGGESTION

File Line Issue
packages/web-shared/src/lib/ai-gateway/o11y/api-metrics.server.ts 270 Promise.race never clears the losing sleep() timeout, so a sustained stream accumulates pending timers for up to the 60s drain window.
Files Reviewed (11 files)
  • apps/web/src/lib/security-agent/services/api-metrics.server.ts (deleted)
  • apps/web/src/lib/security-agent/services/extraction-service.ts
  • apps/web/src/lib/security-agent/services/triage-service.ts
  • packages/web-shared/src/lib/ai-gateway/handlers/audio-transcriptions.test.ts
  • packages/web-shared/src/lib/ai-gateway/handlers/audio-transcriptions.ts
  • packages/web-shared/src/lib/ai-gateway/handlers/embeddings.ts
  • packages/web-shared/src/lib/ai-gateway/handlers/llm-proxy.test.ts
  • packages/web-shared/src/lib/ai-gateway/handlers/llm-proxy.ts
  • packages/web-shared/src/lib/ai-gateway/o11y/api-metrics.server.test.ts - 1 issue (via dependency api-metrics.server.ts)
  • packages/web-shared/src/lib/ai-gateway/o11y/api-metrics.server.ts - 1 issue
  • packages/web-shared/src/lib/ai-gateway/providers/upstream-attempt.ts

Fix these issues in Kilo Cloud


Reviewed by deepseek-v4.1-flash · Input: 0 · Output: 0 · Cached: 0

Review guidance: REVIEW.md from base branch main

@chrarnoldus
chrarnoldus merged commit e00d1bb into main Oct 1, 2026
27 checks passed
@chrarnoldus
chrarnoldus deleted the revert/6084-ai-gateway-observability branch October 1, 2026 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants