Skip to content

fix(cli): recover managed daemon requests after connection refusal - #375

Open
ashwinkrkumar wants to merge 1 commit into
Kaelio:mainfrom
ashwinkrkumar:fix/338-managed-daemon-recovery
Open

ashwinkrkumar wants to merge 1 commit into
Kaelio:mainfrom
ashwinkrkumar:fix/338-managed-daemon-recovery

Conversation

@ashwinkrkumar

Copy link
Copy Markdown

fix(cli): recover managed daemon requests after connection refusal

Summary

Long-running ktx processes (notably ktx mcp) cached the managed daemon's base URL for their lifetime.

After the daemon was replaced or stopped, every daemon-backed HTTP request (for example sql_execution read-only validation) failed with connect ECONNREFUSED 127.0.0.1: until restart.

Managed requests now re-resolve the daemon and retry once, only after a structured ECONNREFUSED on connect, which proves the request was never delivered.

Daemon startup is hardened: spawn errors and early exits are reported instead of crashing or waiting out the deadline, health probes are bounded, a version mismatch fails fast, and a failed or stale start never removes or stops a daemon recorded by another attempt.

Changes

managed-python-http.ts: per-daemon endpoint coordinator (single-flight acquisition with generations, shared across runners in a process), refused-connect classifier, single retry, stderr diagnostics.

managed-python-daemon.ts: child error/exit watch, bounded readiness probes, classified start errors, ownership-safe cleanup, and publish-or-yield before recording a new daemon.

Never retried: resets, timeouts, aborts, HTTP status errors, invalid JSON, permission errors, a denied runtime install, and semantic-query rejections.

Tests

Real-socket and unit coverage for stale endpoints, replacement reuse, same-port recovery, concurrent starts and recoveries, late failures, bounded failure with later recovery, error classification, readiness bounds, ownership, and diagnostics.

Integration test through createKtxMcpServerFactory with an in-memory MCP client: sql_execution recovers after daemon replacement, a persistent failure is reported as a tool fault, and sl_query stays on the subprocess path with its rejections still treated as expected errors.

Against the previous implementation, 28 of these tests fail; the production-wiring test returns connect ECONNREFUSED 127.0.0.1:.

Known Limits

No cross-process lock exists; separate processes starting at the same moment can still both spawn a daemon.

sl_query does not use daemon HTTP, so this does not explain every sl_query report in the issue. The managed embedding provider keeps a fixed daemon URL and is a likely source of the fetch failed variant (follow-up).

Refs #338

@vercel

vercel Bot commented Oct 18, 2026

Copy link
Copy Markdown

@ashwinkrkumar is attempting to deploy a commit to the Kaelio Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

No deployments
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.

1 participant