release: test → main — POST /api/chat/{chatId}/stop (#590)

Merged
SW
4mo ago
+2064-22

Promotes test to main (1 commit): adds POST /api/chat/{chatId}/stop to halt an in-flight chat workflow (#590) — cancels the run, waits for terminal status, releases active_stream_id via CAS. Verified end-to-end on preview (stop cancels the model mid-tool-call, slot released, sub-second response).

🤖 Generated with Claude Code


Summary by cubic

Adds POST /api/chat/{chatId}/stop to cancel an in-flight chat workflow. Stop now aborts the current step fast, waits for terminal status, closes SSE promptly, and frees the chat slot.

  • New Features

    • New route to stop a chat: validates auth/ownership, calls getRun().cancel(), waits via waitForTerminalRunStatus, then clears active_stream_id with CAS; handles pending-* placeholders; returns { success, stopped }.
    • Preemption in runAgentStep: polls getRun(runId).status, passes an abortSignal to streamText, detects user-stop via pipeWorkflowStreamWithStopDetection, and re-persists closed tool parts on abort; returns an aborted flag.
    • Tool backstop: wrapToolsWithAbort races tool execute against the abort signal and rejects hung tools with ToolAbortedError.
    • SSE watcher: wrapWorkflowStreamWatcher closes the response on terminal status, synthesizes tool-output-error for unclosed tool-calls, and forwards consumer cancel to getRun().cancel(); wired into handleChatWorkflowStream.
    • Post-step behavior: credits are charged when a responseMessage exists; auto-commit is skipped on user-stop.
  • Bug Fixes

    • Stop no longer leaves the UI “streaming”; SSE closes quickly after cancellation.
    • Natural completions aren’t misclassified as stops; avoids late finishReason rejections.
    • On cancel timeout, returns 504 and keeps active_stream_id held to avoid races.
    • Client disconnect now cancels the run to reduce drift and tool leaks.

Summary by CodeRabbit

Release Notes

  • New Features

    • Users can now stop in-progress chat workflows through a new stop endpoint.
  • Bug Fixes

    • Tool calls now properly finalize with error status when a workflow is stopped, preventing incomplete tool states from persisting across sessions.