hindsight icon indicating copy to clipboard operation
hindsight copied to clipboard

Reflect finalization continues LLM retries after client disconnect, running until the 300s wall timeout

Open rtlsilva opened this issue 2 months ago • 0 comments

Bug Description

A /reflect request triggered by the coding-agents Codex hook continued making LLM calls after the hook aborted and disconnected.

In the observed run:

  • The Codex hook aborted after 25.007 seconds.
  • Hindsight continued for another ~270 seconds.
  • It ran four tool-call attempts, then four standalone fallback attempts.
  • It finally stopped at the server’s 300-second reflect wall timeout.
  • No [REFLECT CANCELLED] event was emitted for this request.

This creates a runaway-cost risk: the result can no longer be delivered, but provider requests continue and may still be billed.

Current main appears to explain the behavior:

  • Disconnect cancellation is cooperative and checked between reflect-agent iterations.
  • _ask_for_done() can enter the provider’s internal retry loop.
  • If that fails, _finish() immediately starts _forced_final_synthesis().
  • Neither transition performs another cancellation check.
  • The LiteLLM retry loops also do not receive/check the request cancellation token.

Therefore, a disconnect during finalization may not be noticed until all provider retries and fallback work have completed.

This is distinct from:

  • #2122: introduced disconnect cancellation for recall/reflect.
  • #3443: covers the coding-agent hook timeout and user-visible fallback, not server work continuing after the timeout.
  • #4526: the equivalent missing cancellation for synchronous retain.
  • #2668: related observability gap for token usage from failed provider attempts.

Suggested Fix

  • Check the reflect cancellation token before every provider retry.
  • Check it before falling back from _ask_for_done() to standalone synthesis.
  • Add checkpoints at the start of _finish() and _forced_final_synthesis().
  • Add a regression test that disconnects during the closing call and asserts that no retry or standalone fallback begins.

Workaround

I have temporarily:

  • Disabled automatic Codex reflect with harnesses.codex.autoReflect=false.
  • Set HINDSIGHT_API_REFLECT_LLM_MAX_RETRIES=0.

Environment

  • hindsight-api: 0.10.2, self-hosted
  • @vectorize-io/hindsight-coding-agents: 0.4.1
  • OS: macOS 26.7.1, arm64
  • Python: 3.14.7
  • Node: 26.5.1
  • AWS Bedrock — Anthropic Claude Sonnet 5

Steps to Reproduce

  1. Run self-hosted Hindsight with reflect provider timeouts and retries enabled.
  2. Configure a provider that stalls longer than the per-attempt timeout. The observed case used Bedrock with Claude Sonnet 5 and a 30-second provider timeout.
  3. Trigger automatic reflect from the coding-agents Codex hook.
  4. Allow reflect to reach its closing done/final-synthesis phase.
  5. Abort the client request after 25 seconds, as the hook does.
  6. Inspect the API and provider logs.

A deterministic regression test could use a provider stub that permits the initial reflect work, then stalls during the closing done call.

Expected Behavior

Once the client disconnects:

  • Hindsight should cancel reflect at the next cancellation boundary.
  • No new provider retry or standalone fallback should begin.
  • If an in-flight provider call cannot be interrupted, Hindsight should stop before retrying it.
  • The server should log [REFLECT CANCELLED] ... reason=client disconnected.

Actual Behavior

Plugin diagnostic, in UTC:

2026-10-02T10:14:00.650Z
harness=codex
event=reflect_failed
ms=25007
error="This operation was aborted (20)"

Server log timestamps below are local time, UTC+1:

11:14:16  closing tool call begins after the client abort
11:14:46  tool call exceeded timeout=30.0s, retrying
11:15:17  tool call exceeded timeout=30.0s, retrying
11:15:49  tool call exceeded timeout=30.0s, retrying
11:16:23  tool call timed out after 4 attempts
11:16:23  closing done call failed, using standalone prompt
11:16:53  standalone call exceeded timeout=30.0s, retrying
11:17:24  standalone call exceeded timeout=30.0s, retrying
11:17:56  standalone call exceeded timeout=30.0s, retrying
11:18:30  standalone call timed out after 4 attempts
11:18:30  reflect wall-clock timeout after 295.1s

The request therefore continued for approximately 4½ minutes after its client had aborted.

Cancellation is intermittent: another request on the same installation later produced:

[REFLECT CANCELLED] bank=<bank> reason=client disconnected

Failed timeout attempts also return no provider usage response, so Hindsight’s token totals can understate the cost exposure.

Version

0.10.2

LLM Provider

Other

rtlsilva avatar Oct 02 '26 15:10 rtlsilva