jcode icon indicating copy to clipboard operation
jcode copied to clipboard

Streaming turn loop never retries empty provider responses, then blames a guardrail (openai-compatible)

Open sprayogo opened this issue 4 months ago • 2 comments

Summary

The [provider guardrail] The model ended its turn without any visible output (stop_reason: stop) notice fires regularly on an OpenAI-compatible provider (local 9router proxy). Root cause is not a real provider guardrail: the upstream stream genuinely returns zero content, and jcode's streaming turn loop has no retry for that case, whereas the non-streaming loop does. The notice text is also misleading, since it blames a content filter for what is almost always a dropped/aborted upstream stream.

Environment

  • jcode v0.64.2 (6c6fbba1c), linux x86_64
  • provider: openai-compatible, base_url = http://localhost:20128/v1 (9router v0.5.45 relay)
  • models seen: cc/claude-opus-5, cx/gpt-5.6-sol, ag/gemini-3-flash-agent
  • mode: mpsc streaming (AGENT_PROVIDER_STREAM_LIFECYCLE ... mode=mpsc)

Evidence

jcode side, from ~/.jcode/logs/jcode-2026-07-30.log (13 occurrences in one morning, across 4 sessions and 3 different upstream models):

[WARN] [ses:session_tulip_178540|prv:OpenRouter|mod:cx/gpt] PROVIDER_GUARDRAIL: turn ended with no visible output (stop_reason=Some("stop"), reasoning_chars=0)
[WARN] [ses:session_vole_1785142|prv:OpenRouter|mod:ag/gemini] PROVIDER_GUARDRAIL: turn ended with no visible output (stop_reason=None, reasoning_chars=0)
[WARN] [ses:session_blowfish_178|prv:OpenRouter|mod:cc/claude] PROVIDER_GUARDRAIL: turn ended with no visible output (stop_reason=None, reasoning_chars=0)

The matching stream lifecycle line shows a clean finish, not a refusal:

event=AGENT_PROVIDER_STREAM_LIFECYCLE ... elapsed_ms=5280 mode=mpsc model=cx/gpt-5.6-sol
  phase=stream_complete provider=OpenRouter saw_message_end=true stop_reason=stop

Proxy side, same request id/timestamp (proxy request log, 2026-07-30T10:37:59.708Z, matching the warning above to the millisecond):

{
  "status": "success",
  "latency": {"ttft": 3684, "total": 5261},
  "tokens": {"prompt_tokens": 144546, "completion_tokens": 4,
             "prompt_tokens_details": {"cached_tokens": 142848}},
  "providerResponse": "[Empty streaming response]",
  "response": {"content": "[Empty streaming response]", "thinking": null, "type": "streaming"}
}

Several other occurrences are even more degenerate — prompt_tokens: 0, completion_tokens: 0, ttft 1–5 s, still status: success, i.e. the upstream SSE stream opened and closed with no usable event at all.

Frequency over the proxy's last 1000 requests: 13 turns with completion_tokens <= 10 and no tool calls (~1.3% of requests, 11× claude-opus-5, 2× gpt-5.6-sol). Those 13 map 1:1 onto the 13 PROVIDER_GUARDRAIL warnings. So the notice is accurately detecting an empty turn; it is simply not recovering from it, and it mislabels the cause.

I also replayed one of the failing payloads through the same proxy three times (full 175 tools / 20 tools / no tools) and every replay returned content normally. The emptiness is transient, not payload-shaped — exactly the class of failure that should be retried.

Root cause

Two independent gaps.

1. The empty-response retry only exists in the non-streaming loop.

crates/jcode-app-core/src/agent/turn_loops.rs retries a whitespace-only provider response:

// turn_loops.rs:~796
&& empty_post_tool_continuations < Self::MAX_EMPTY_POST_TOOL_CONTINUATION_ATTEMPTS

grep -rn "empty_post_tool_continuation\|MAX_EMPTY_POST_TOOL" crates/jcode-app-core/src/agent/ matches only turn_loops.rs. The streaming path (turn_streaming_mpsc.rs, the one actually used, mode=mpsc) has no equivalent. Its only recovery hook is handle_streaming_no_tool_calls → maybe_continue_incomplete_response, and that bails immediately for exactly the stop reasons observed here:

// response_recovery.rs:103
if matches!(reason.as_str(), "stop" | "end_turn" | "tool_use") {
    return false;
}

stop_reason=None also returns false earlier (empty reason). So on the streaming path an empty turn is terminal: NoToolCallOutcome::Break, notice emitted, turn over. On the non-streaming path the identical situation would have been retried up to MAX_EMPTY_POST_TOOL_CONTINUATION_ATTEMPTS. Same provider, same failure, two different behaviors purely by codepath.

2. The message asserts a cause that the code has not established.

provider_guardrail_notice (response_recovery.rs:137) correctly distinguishes real guardrail stops via is_guardrail_stop_reason (refusal / content_filter / safety / guardrail / policy_violation). When that check is false, it still says:

"This is usually a provider-side guardrail or filter silently dropping the response."

For stop_reason: stop with completion_tokens: 4 that is a misdiagnosis. It sent me looking at swarm mode and compaction settings for an hour before I checked the proxy's request log. The event is also logged under the name PROVIDER_GUARDRAIL, which makes the log searchable only under the wrong hypothesis.

Suggested fix

  1. Port the empty-response continuation from turn_loops.rs into turn_streaming_mpsc.rs: when saw_message_end && tool_calls.is_empty() && text_content.trim().is_empty() && !is_guardrail_stop_reason(...), retry the turn up to MAX_EMPTY_POST_TOOL_CONTINUATION_ATTEMPTS before surfacing anything. Ideally share one helper so the two loops cannot drift again.
  2. Split the notice into two messages, and only surface it after retries are exhausted:
    • real guardrail stop reason → current wording;
    • empty turn with a normal stop reason → say the provider returned an empty response and jcode retried N times, without asserting a content filter. Rename the non-guardrail log event (e.g. PROVIDER_EMPTY_RESPONSE) so the two are separable in logs.
  3. Optionally include completion_tokens in the warning when usage is available. completion_tokens: 0 vs a large value cleanly separates "upstream dropped the stream" from "model really chose to emit nothing", and would have made this diagnosable from the jcode log alone.

Happy to send a PR for (1) and (2) if the approach looks right.

Unrelated observation while debugging

The bash destructive-command guard has false positives on redirections and glob-free strings. All three of these were refused during this investigation:

grep -rl "ended its turn without any visible output" .   # parsed as target /home/<user>/ended its turn without...
sed -n '95,145p' config.toml | ... ; ss -ltnp 2>/dev/null | grep 20128   # "protected system path (target: /dev/null)"
grep -c "PROVIDER_GUARDRAIL" logs/jcode-*.log 2>/dev/null | grep -v ":0"

2>/dev/null being read as a destructive target for /dev/null, and a quoted grep pattern being read as a path, both look like tokenizer bugs in the guard. Filing separately if you prefer.

sprayogo avatar Jul 30 '26 10:07 sprayogo

Fixed in 480d395e1's agent changes on master (suggestions 1 and 2 from your report):

  • The whitespace-only post-tool-result continuation is now a shared helper (maybe_continue_empty_post_tool_response) used by both the non-streaming and streaming (mpsc) loops, so an empty stream after tool results is retried up to 5 times on the streaming path too. Guardrail stop reasons (refusal/content_filter/safety) are never retried.
  • The log event is split: real guardrail stops keep PROVIDER_GUARDRAIL, transient empties now log as PROVIDER_EMPTY_RESPONSE, and the user-facing notice no longer asserts a content filter for a normal stop reason.

Suggestion 3 (include completion_tokens in the warning) was not done in this pass. The bash-guard false positives you noted at the end are a known rough edge; please do file that separately if you have the repro handy.

Verified with new unit tests covering retry gating, budget, and notice wording.


— Jcode agent (automated triage), on behalf of @1jehuang

1jehuang avatar Jul 30 '26 22:07 1jehuang

Released in v0.75.5. Run jcode update to get it.

github-actions[bot] avatar Aug 12 '26 19:08 github-actions[bot]