Python
Continue the turn when a reply is cut off at the output limit with no action (OPE-171)
Devika Verma786ba3022518
diff --git a/coworker/engine.py b/coworker/engine.pyindex 24b26be2..92ea9dc2 100644--- a/coworker/engine.py+++ b/coworker/engine.py@@ -30,6 +30,22 @@ from .events import Event, EventType# §8.4 retry guard: the reviewer pauses for the rest of the turn after this many denials# IN A ROW (2→5 + streak semantics, owner ruling 2026-08-24 — a cumulative 2 silently# downgraded long agentic turns to hand-approval after one over-strict pair).+# OPE-171: a reply cut off at the output-token limit (finish_reason "length") that+# carries no tool call is not an answer — typically thinking consumed the whole budget+# and nothing else came back. The engine nudges the model to act instead of ending the+# turn as "completed", at most this many times in a row; then the turn ends as+# "truncated" so callers can tell "finished" from "gave up".+MAX_TRUNCATION_CONTINUATIONS = 2+TRUNCATION_NUDGE = (+ "Your previous reply hit the output-token limit before you took an action or "+ "finished. Do not repeat the long reasoning. Decide the next concrete step and "+ "call a tool now, or give the final answer briefly."+)+# What the provider sees in place of the cut-off reply: its partial thinking block has+# no signature (Anthropic rejects it on replay) and its content is empty or a fragment;+# the transcript keeps the original, the outbound view sends this.+TRUNCATION_STUB = "(reply cut off at the output-token limit before any action)"+_REVIEWER_TRIP = 5_REVIEWER_PAUSED_TEXT = ("Auto-approve is paused for the rest of this turn — the reviewer blocked "@@ -247,6 +263,8 @@ class TurnEngine:# Whether the latest assistant turn hit the output-token limit — decides which# diagnosis a mangled (unparseable-args) tool call gets answered with.self._turn_truncated = False+ # Consecutive length-truncated, action-free replies nudged this turn (OPE-171).+ self._continuations = 0self._warned_context_fallback = False# Each pending steering message: (text, optional MessageSource sidecar dict).self._steering: list[tuple[str, Optional[dict[str, Any]]]] = []@@ -466,6 +484,7 @@ class TurnEngine:async def _loop(self) -> AsyncIterator[Event]:iterations = 0+ self._continuations = 0while True:if iterations >= self.max_iterations:yield Event(@@ -554,6 +573,8 @@ class TurnEngine:self._last_context_tokens = turn.usage.context_tokensself._turn_truncated = turn.finish_reason == "length"+ if not self._turn_truncated:+ self._continuations = 0_sanitize_mangled_calls(turn)self.messages.append(_assistant_message(turn, model=self.model))payload: dict[str, Any] = {@@ -576,6 +597,64 @@ class TurnEngine:if self._steering:self._inject_steering()continue+ if self._turn_truncated:+ # OPE-171: cut off at the output limit with nothing actionable. The+ # reply just persisted is marked for stub replay (see+ # `_outbound_messages`); the model is asked to act, and the turn+ # goes round again — unless it has already happened too often.+ cut = self.messages[-1]+ if cut.get("role") == "assistant":+ cut["replay"] = "stub"+ if self._continuations < MAX_TRUNCATION_CONTINUATIONS:+ self._continuations += 1+ self.messages.append(+ {+ "role": "user",+ "content": TRUNCATION_NUDGE,+ "ts": time.time(),+ "_display": {+ "kind": "continuation",+ "reason": "length",+ "attempt": self._continuations,+ },+ }+ )+ yield Event(+ EventType.CONTINUATION,+ {+ "reason": "length",+ "attempt": self._continuations,+ "remaining": MAX_TRUNCATION_CONTINUATIONS+ - self._continuations,+ "iterations": iterations,+ "text": (+ "Reply cut off at the output-token limit with no "+ f"action; asking the model to continue "+ f"({self._continuations} of "+ f"{MAX_TRUNCATION_CONTINUATIONS})."+ ),+ },+ )+ continue+ text = (+ f"{self.model}'s reply was cut off at the output-token limit with "+ f"no action {self._continuations + 1} times in a row, so the turn "+ "was stopped rather than reported as complete. Raise "+ "max_output_tokens, lower the reasoning effort, or retry."+ )+ self._append_notice(+ "truncated", text, continuations=self._continuations+ )+ yield Event(+ EventType.TURN_END,+ {+ "status": "truncated",+ "iterations": iterations,+ "continuations": self._continuations,+ "text": text,+ },+ )+ return# The model tried to call a tool and the syntax never parsed — salvage already# had its go. Ending as "completed" here would present a half-written call as# the answer, which is indistinguishable from the model deciding it was done;@@ -2018,6 +2097,7 @@ class TurnEngine:"usage","finish_reason","max_output_tokens",+ "replay",)# Auto-compaction (OPE-27): everything before the boundary is represented by the# compacted block. Outbound-only — the canonical history stays intact — and the@@ -2027,7 +2107,10 @@ class TurnEngine:)out = [(- {k: v for k, v in msg.items() if k not in _SIDECARS}+ # OPE-171: a length-truncated, action-free reply is replayed as a stub.+ {"role": "assistant", "content": TRUNCATION_STUB}+ if msg.get("replay") == "stub"+ else {k: v for k, v in msg.items() if k not in _SIDECARS}if any(s in msg for s in _SIDECARS)else msg)diff --git a/coworker/events.py b/coworker/events.pyindex bc01ea73..323c789f 100644--- a/coworker/events.py+++ b/coworker/events.py@@ -41,6 +41,9 @@ class EventType(str, Enum):INTERRUPTED = "interrupted"COMPACTING = "compacting" # compaction started — surfaces show a transient signalCOMPACTED = "compacted" # outbound history was compacted (summary or trim)+ # OPE-171: the reply was cut off at the output-token limit with no tool call; the+ # engine nudged the model to act and is going round the loop again.+ CONTINUATION = "continuation"@dataclassdiff --git a/surfaces/gui/src/App.tsx b/surfaces/gui/src/App.tsxindex 67cff675..8a9d58b8 100644--- a/surfaces/gui/src/App.tsx+++ b/surfaces/gui/src/App.tsx@@ -891,6 +891,10 @@ export function App() {case "turn_end":if (d.status === "max_iterations_exceeded")setItems((p) => [...p, { kind: "notice", tone: "warn", text: t("app.notice.max_iterations") }]);+ // OPE-171: the reply kept hitting the output-token limit with no action; the+ // engine stopped rather than report a cut-off reply as the answer.+ else if (d.status === "truncated")+ setItems((p) => [...p, { kind: "notice", tone: "warn", text: d.text || t("app.notice.truncated") }]);break;case "mode_notice":// Server-authored + persisted (owner ruling 2026-08-24): the Auto-Approve@@ -924,6 +928,12 @@ export function App() {]);announceMemoryChanged(); // Settings ▸ Memory, if open, is now stalebreak;+ case "continuation":+ // OPE-171: a reply cut off at the output limit with no tool call — the engine+ // nudged the model and is going round again; one quiet line so the pause and+ // the extra model call are explained.+ setItems((p) => [...p, { kind: "notice", tone: "info", text: d.text || t("app.notice.continuation") }]);+ break;case "compacting":setCompacting(true);break;diff --git a/surfaces/gui/src/itemsFromMessages.test.ts b/surfaces/gui/src/itemsFromMessages.test.tsindex 2b296b37..666d9aee 100644--- a/surfaces/gui/src/itemsFromMessages.test.ts+++ b/surfaces/gui/src/itemsFromMessages.test.ts@@ -70,6 +70,19 @@ describe("itemsFromMessages notices", () => {});describe("itemsFromMessages model switch", () => {+ it("replays the persisted truncated marker as a retriable warning (OPE-171)", () => {+ const items = itemsFromMessages([+ { role: "user", content: "go", ts: 1752969700 },+ { role: "assistant", content: "", finish_reason: "length", replay: "stub", ts: 1752969710 },+ { role: "notice", kind: "truncated", text: "cut off 3 times", continuations: 2, ts: 1752969720 },+ ] as any);+ const notice = items[items.length - 1] as any;+ expect(notice.kind).toBe("notice");+ expect(notice.tone).toBe("warn");+ expect(notice.text).toBe("cut off 3 times");+ expect(notice.retriable).toBe(true);+ });+it("replays the persisted model_switch marker as an info notice", () => {const items = itemsFromMessages([{ role: "user", content: "hi" },diff --git a/surfaces/gui/src/itemsFromMessages.ts b/surfaces/gui/src/itemsFromMessages.tsindex 1917aa05..bd43be47 100644--- a/surfaces/gui/src/itemsFromMessages.ts+++ b/surfaces/gui/src/itemsFromMessages.ts@@ -117,7 +117,11 @@ export function itemsFromMessages(messages: ConversationMessage[]): Item[] {{ kind: "notice", tone: "info", title: (m as any).title || "Auto-approve is on.", text: m.text || "" }: m.kind === "mode_switch"? { kind: "notice", tone: "info", text: m.text || "" }- : { kind: "notice", tone: "warn", text: "Error: " + (m.text || "unknown"), retriable: true },+ : m.kind === "truncated"+ ? // OPE-171: the turn stopped after repeated output-limit cut-offs with no+ // action. Retriable like an error: a fresh attempt usually acts.+ { kind: "notice", tone: "warn", text: m.text || "Stopped: reply cut off at the output-token limit.", retriable: true }+ : { kind: "notice", tone: "warn", text: "Error: " + (m.text || "unknown"), retriable: true },);}// system messages are omitted; tool-result messages are folded into the tool row abovediff --git a/surfaces/gui/src/locales/en.json b/surfaces/gui/src/locales/en.jsonindex 869ea739..46695337 100644--- a/surfaces/gui/src/locales/en.json+++ b/surfaces/gui/src/locales/en.json@@ -423,6 +423,8 @@"interrupted": "Interrupted.","error": "Error: ","context_compacted": "Context compacted",+ "continuation": "Reply cut off at the output-token limit; asking the model to continue.",+ "truncated": "Stopped: reply cut off at the output-token limit with no action.","input_rejected": "That message was rejected.","unknown": "unknown"},diff --git a/surfaces/gui/src/locales/zh.json b/surfaces/gui/src/locales/zh.jsonindex 6ac0f33c..be6bc807 100644--- a/surfaces/gui/src/locales/zh.json+++ b/surfaces/gui/src/locales/zh.json@@ -415,6 +415,8 @@"interrupted": "已中断。","error": "错误:","context_compacted": "上下文已压缩",+ "continuation": "回复在输出上限处被截断,正在让模型继续。",+ "truncated": "已停止:回复在输出上限处被截断且未执行任何操作。","input_rejected": "该消息已被拒绝。","unknown": "未知"},diff --git a/surfaces/gui/src/types.ts b/surfaces/gui/src/types.tsindex 9168995b..160125d3 100644--- a/surfaces/gui/src/types.ts+++ b/surfaces/gui/src/types.ts@@ -25,6 +25,7 @@ export type EventType =| "memory_saved"| "compacting"| "compacted"+ | "continuation"| "turn_done";export interface WsEvent {diff --git a/tests/test_truncation_continuation.py b/tests/test_truncation_continuation.pynew file mode 100644index 00000000..6a463a42--- /dev/null+++ b/tests/test_truncation_continuation.py@@ -0,0 +1,223 @@+"""OPE-171: a reply cut off at the output limit with no action continues the turn.++Before: thinking filled the whole output budget, the provider returned no text and no+tool call with finish_reason "length", and the engine ended the turn as "completed" — the+task was silently abandoned (two of three Fable 5.1 failures in a ten-task trial). Now the+engine nudges the model to act, at most MAX_TRUNCATION_CONTINUATIONS times in a row, and+otherwise ends the turn with the distinct status "truncated". The cut-off reply is kept in+the transcript but replayed to the provider as a short stub (an unsigned partial thinking+block would be rejected by Anthropic; replaying it is useless everywhere else).+"""++from __future__ import annotations++import asyncio++import aisuite as ai+from coworker.engine import (+ MAX_TRUNCATION_CONTINUATIONS,+ TRUNCATION_NUDGE,+ TRUNCATION_STUB,+ TurnEngine,+)+from coworker.events import EventType+from coworker.permissions import PermissionEngine+from coworker.providers import (+ AssistantTurn,+ ModelCapabilities,+ ProviderClient,+ ToolCall,+)+from coworker.tools import ToolRegistry+++def _cut(text=None):+ """A length-truncated, action-free reply (thinking ate the budget)."""+ return AssistantTurn(text=text, finish_reason="length", reasoning="Let me think ab")+++def _stop(text="done"):+ return AssistantTurn(text=text, finish_reason="stop")+++def _tool(name, args, call_id="c1"):+ return AssistantTurn(+ tool_calls=[ToolCall(id=call_id, name=name, arguments=args)],+ finish_reason="tool_calls",+ )+++class Scripted(ProviderClient):+ def __init__(self, turns):+ self._turns = list(turns)+ self.calls = 0+ self.seen: list[list[dict]] = [] # outbound history per call++ def complete(self, *, model, messages, tools=None, **settings):+ self.calls += 1+ self.seen.append(messages)+ return self._turns.pop(0)++ def capabilities(self, model):+ return ModelCapabilities()+++def _engine(tmp_path, turns, **kwargs):+ provider = Scripted(turns)+ registry = ToolRegistry()+ registry.register_all(ai.toolkits.files(root=str(tmp_path), allow_write=True))+ engine = TurnEngine(+ provider=provider,+ registry=registry,+ permissions=PermissionEngine(workspace_root=tmp_path),+ model="gpt-5.5",+ **kwargs,+ )+ return engine, provider+++def _run(engine, text="hello"):+ async def _collect():+ return [ev async for ev in engine.run(text)]++ return asyncio.run(_collect())+++def _types(events):+ return [ev.type for ev in events]+++def _assistants(engine):+ return [m for m in engine.messages if m.get("role") == "assistant"]+++# -- continuation -------------------------------------------------------------------+++def test_cut_off_reply_is_nudged_then_the_turn_completes(tmp_path):+ engine, provider = _engine(tmp_path, [_cut(), _stop("the answer")])+ events = _run(engine)++ assert provider.calls == 2+ assert _types(events) == [+ EventType.TURN_START,+ EventType.ASSISTANT_MESSAGE,+ EventType.CONTINUATION,+ EventType.ASSISTANT_MESSAGE,+ EventType.TURN_END,+ ]+ assert events[-1].data["status"] == "completed"+ cont = events[2].data+ assert cont["reason"] == "length" and cont["attempt"] == 1+ assert cont["remaining"] == MAX_TRUNCATION_CONTINUATIONS - 1++ # Transcript: the cut-off reply, the nudge, the real answer — in that order.+ roles = [(m["role"], m.get("finish_reason")) for m in engine.messages if m["role"] != "system"]+ assert roles == [("user", None), ("assistant", "length"), ("user", None), ("assistant", "stop")]+ nudge = engine.messages[-2]+ assert nudge["content"] == TRUNCATION_NUDGE+ assert nudge["_display"] == {"kind": "continuation", "reason": "length", "attempt": 1}+ truncated = _assistants(engine)[0]+ assert truncated["replay"] == "stub"+ assert truncated["reasoning"] == "Let me think ab" # kept for the record+++def test_cut_off_reply_is_replayed_as_a_stub_not_verbatim(tmp_path):+ engine, provider = _engine(tmp_path, [_cut(), _stop()])+ _run(engine)+ second_call = provider.seen[1]+ assistant = [m for m in second_call if m["role"] == "assistant"]+ assert assistant == [{"role": "assistant", "content": TRUNCATION_STUB}]+ assert "replay" not in assistant[0] and "reasoning" not in assistant[0]+ # The nudge itself travels as a plain user message (sidecars stripped).+ assert second_call[-1] == {"role": "user", "content": TRUNCATION_NUDGE}+++def test_cut_off_with_partial_text_is_nudged_too(tmp_path):+ """Text that stops mid-sentence with no action is still not an answer."""+ engine, provider = _engine(tmp_path, [_cut("The plan is to first"), _stop("full")])+ events = _run(engine)+ assert provider.calls == 2+ assert EventType.CONTINUATION in _types(events)+ assert events[-1].data["status"] == "completed"+++def test_cap_ends_the_turn_as_truncated_with_a_notice(tmp_path):+ turns = [_cut() for _ in range(MAX_TRUNCATION_CONTINUATIONS + 1)]+ engine, provider = _engine(tmp_path, turns)+ events = _run(engine)++ assert provider.calls == MAX_TRUNCATION_CONTINUATIONS + 1+ assert _types(events).count(EventType.CONTINUATION) == MAX_TRUNCATION_CONTINUATIONS+ end = events[-1]+ assert end.type == EventType.TURN_END+ assert end.data["status"] == "truncated"+ assert end.data["continuations"] == MAX_TRUNCATION_CONTINUATIONS+ notice = engine.messages[-1]+ assert notice["role"] == "notice" and notice["kind"] == "truncated"+ assert notice["continuations"] == MAX_TRUNCATION_CONTINUATIONS+ assert "output" in notice["text"]+++def test_cut_off_reply_with_a_tool_call_is_not_nudged(tmp_path):+ """A truncated reply that still carries a parseable tool call runs the tool as+ before; the mangled-call path already handles the broken-arguments case."""+ (tmp_path / "a.txt").write_text("hello", encoding="utf-8")+ cut_with_call = AssistantTurn(+ tool_calls=[ToolCall(id="c1", name="read_file", arguments={"path": "a.txt"})],+ finish_reason="length",+ )+ engine, provider = _engine(tmp_path, [cut_with_call, _stop("it says hello")])+ events = _run(engine)+ assert EventType.CONTINUATION not in _types(events)+ assert EventType.TOOL_FINISHED in _types(events)+ assert events[-1].data["status"] == "completed"+ assert "replay" not in _assistants(engine)[0]+++def test_counter_resets_after_a_good_reply(tmp_path):+ """cut, act, cut, cut, answer: never more than MAX consecutive, so it completes."""+ (tmp_path / "a.txt").write_text("hello", encoding="utf-8")+ turns = [+ _cut(),+ _tool("read_file", {"path": "a.txt"}),+ _cut(),+ _cut(),+ _stop("finally"),+ ]+ engine, provider = _engine(tmp_path, turns)+ events = _run(engine)+ assert provider.calls == 5+ assert _types(events).count(EventType.CONTINUATION) == 3+ assert events[-1].data["status"] == "completed"+++def test_counter_resets_between_turns(tmp_path):+ engine, provider = _engine(tmp_path, [_cut(), _stop(), _cut(), _cut(), _stop()])+ assert _run(engine, "one")[-1].data["status"] == "completed"+ assert _run(engine, "two")[-1].data["status"] == "completed"+ assert provider.calls == 5+++def test_normal_completion_is_untouched(tmp_path):+ engine, provider = _engine(tmp_path, [_stop("all done")])+ events = _run(engine)+ assert _types(events) == [+ EventType.TURN_START,+ EventType.ASSISTANT_MESSAGE,+ EventType.TURN_END,+ ]+ assert all("replay" not in m for m in engine.messages)+++def test_stub_marker_never_reaches_a_provider_and_survives_persistence(tmp_path):+ engine, provider = _engine(tmp_path, [_cut(), _stop()])+ _run(engine)+ assert any(m.get("replay") == "stub" for m in engine.messages)+ for call in provider.seen:+ assert all("replay" not in m for m in call)+ # A fresh engine over the same persisted history replays the stub the same way.+ engine2, provider2 = _engine(tmp_path, [_stop("again")], messages=list(engine.messages))+ _run(engine2, "next")+ assistants = [m for m in provider2.seen[0] if m["role"] == "assistant"]+ assert assistants[0] == {"role": "assistant", "content": TRUNCATION_STUB}