← Back to Pull requests

fix(cli): share telemetry shutdown deadline #2333

Open
unknown opened this pull request with 545 lines diff across 6 files
View on GitHub
unknown unknown commented on Jul 29, 2026 01:53 UTC
Author

Requirement or Bug

Resolve #2248. CLI shutdown previously gave each telemetry owner a separate timeout, so a stalled engine pipeline followed by a stalled host pipeline could delay exit beyond the configured budget. The same budget must also cover cleanup after web startup fails.

Bug Reproduction Steps

The regression tests run two stalled telemetry pipelines under fake time. Before this change, their cumulative shutdown time exceeded one budget; the startup-failure cleanup path had the same problem. See apps/kimi-code/test/cli/telemetry.test.ts, apps/kimi-code/test/cli/web/web.test.ts, and packages/kap-server/test/boot.test.ts.

Root Cause

The print and web entrypoints passed a fresh timeout to each independently owned telemetry pipeline. RunningServer.close() had no way to receive the absolute deadline from the web host, and the web onReady rejection path bypassed the normal shutdown policy. This is a shared-deadline fix, not a change to telemetry transport or stored data.

Code Changes

One absolute deadline is created at each CLI shutdown boundary. Each pipeline receives only the remaining time, and the caller also bounds the await so a stalled token lookup cannot outlive the deadline. A rejection or timeout is reported without blocking the next owner. Web startup failure now uses the same close helper as normal shutdown.

print/headless exit
  CLI deadline → engine telemetry → host telemetry
web exit or startup failure
  CLI deadline → RunningServer.close(deadline) → engine telemetry
               → host telemetry

The server-close error boundary remains distinct from telemetry failures, and print exit results are preserved. Start with apps/kimi-code/src/cli/telemetry-shutdown.ts, then the print/web call sites and packages/kap-server/src/start.ts.

Behavior Changes and Affected Users

| Behavior | Before | After | Who relies on the old behavior | Escape hatch |
|---|---|---|---|---|
| Telemetry flush on print/headless CLI exit | Each pipeline could use a full timeout, extending exit latency | All pipelines share one absolute budget; failures are reported and later cleanup is still attempted | Headless CLI users, scripts that parse print output or exit status | No output format, exit code, flag, or telemetry setting changes; the existing telemetry toggle still applies |
| Telemetry flush on kimi web exit or startup failure | Engine and host flushes could each wait a full timeout, including after onReady failed | Both use the remaining shared budget; server close errors remain visible | Code-app web/desktop hosts and remote-control clients using the local server | No REST, WebSocket, remote-control protocol, auth, or persisted session shape changes |

Previous-release clients and data remain compatible: this PR changes shutdown timing and warning behavior only. Existing CLI arguments, printed result formats, exit outcomes, REST/WebSocket contracts, remote-control messages, and stored sessions remain readable and unchanged. A stalled telemetry owner may receive less time than before because it shares the process exit budget; the configured budget itself and the telemetry setting stay the same.

Affected modules and evidence: print lifecycle tests cover failure, reporter failure, expired budget, and preserved exit result; web lifecycle tests cover normal shutdown, startup failure, and server-close error; kap-server tests cover the propagated deadline. On the rebased branch, 128 focused tests pass across four suites (including a RED → GREEN stalled-host-lookup regression), apps/kimi-code and packages/kap-server typecheck, and the CLI production build passes. Full Vitest matrix was not rerun for this rebase.

Checklist

  • I have read the CONTRIBUTING document.

  • I have linked a related issue, or explained the problem above.

  • I have added tests that prove my feature works.

  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.

  • Ran gen-changesets skill; added a patch changeset for the CLI package.

  • Ran gen-docs skill; no documentation update is needed for this internal shutdown policy.
  • Diff: 545 lines (+408 / -137) across 6 files Reviewer: ousamabenyounes Review duration: 179855m
    All Vouch review confidence checks passed
    Residual risk 0.33 = Change Risk (0.55) × (1 − Review Confidence (0.41)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.55 · 545 lines diff across 6 files
    0.55
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    40%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.377
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #2333"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 40.9% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 55% (MEDIUM) · Higher = larger blast radius
    55%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 40.9% · Lower = rushed or shallow review
    40.9%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 32.5% · Safe to merge
    32.5%