agent-hub-runtime 0.6.0

AgentHub desktop and companion CLI runtime for Tauri applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
"""Herdr-specific managed execution: ownership, layout, observation and recovery.

The shared Agent executor supplies checkpoint storage and command transport.
Startup is delegated to the same configured Rust launcher used by interactive entry points.
"""
from pathlib import Path
import hashlib
import math
import subprocess
import time
import uuid
import json
import re


def prompt_digest(text):
    # Terminal wrapping and NBSP change whitespace, but never the input's words.
    return hashlib.sha256(" ".join(text.split()).encode()).hexdigest()


def confirm_pending_input(invoke, state):
    """Submit a verified Claude draft once; never replay its text or guess at input."""
    delivery = state.get("delivery", {})
    if not delivery.get("digest") or delivery.get("enter_attempted"):
        return False
    target = state["pane"]
    agent = invoke(["agent", "get", target])["agent"]
    if (agent.get("agent") != "claude" or agent.get("agent_status") != "idle" or not agent.get("agent_session")
            or (agent.get("completion_seq") or 0) > state.get("completion_seq", 0)):
        return False
    verify_agent_identity(agent, state, Path(state["cwd"]))
    text = invoke(["agent", "read", target, "--source", "visible"], text_output=True)
    blocks = re.split(r"(?m)^[─━]{8,}[^\S\n]*$", text)
    if len(blocks) < 3:
        return False
    draft = blocks[-2].strip()
    if not draft.startswith("❯") or prompt_digest(draft[1:]) != delivery["digest"]:
        return False
    current = invoke(["agent", "get", target])["agent"]
    verify_agent_identity(current, state, Path(state["cwd"]))
    if (current.get("agent") != "claude" or current.get("agent_status") != "idle" or current.get("agent_session") != agent.get("agent_session")
            or current.get("revision") != agent.get("revision")
            or (current.get("completion_seq") or 0) > state.get("completion_seq", 0)):
        return False
    # Persist before the key: an uncertain reply cannot authorize a second Enter.
    delivery["enter_attempted"] = True
    save_state(state)
    invoke(["agent", "send-keys", target, "enter"])
    print("已核对原输入框完整消息,补交一次确认键;没有重发提示词,继续核验 Agent 活动。", flush=True)
    return True


def submit_prompt(invoke, state, prompt):
    state["delivery"] = {"digest": prompt_digest(prompt), "enter_attempted": False}
    save_state(state)
    try:
        invoke(["agent", "prompt", state.get("agent_name") or state["pane"], prompt,
                "--wait", "--until", "working", "--until", "blocked", "--until", "done", "--timeout", "7000"])
        return True
    except HerdrError as error:
        if error.code not in ("agent_prompt_stalled", "timeout"):
            raise
        if confirm_pending_input(invoke, state):
            # The key receipt alone is not delivery evidence.
            deadline = time.monotonic() + 7
            while time.monotonic() < deadline:
                agent = invoke(["agent", "get", state["pane"]])["agent"]
                verify_agent_identity(agent, state, Path(state["cwd"]))
                if agent.get("agent_status") in ("working", "blocked") or (
                        (agent.get("completion_seq") or 0) > state.get("completion_seq", 0)):
                    return True
                time.sleep(1)
        if error.code == "timeout":
            raise
        print("提示词提交尚未确认,保留原会话继续观察;不会重复投递。", flush=True)
        return False

def previous_agent(invoke, previous, session, cwd):
    if not previous:
        return None, None
    if previous.get("session") != session or previous.get("cwd") != str(cwd):
        if previous.get("phase") != "completed":
            raise HerdrError("上次 Herdr 任务未确认完成,请先处理原会话后再更换目标。")
        return None, None
    if not previous.get("pane"):
        raise HerdrError("上次 Herdr 布局创建结果未确认,请在 Herdr 检查后处理 .herdr-run.json。")
    try:
        return invoke(["agent", "get", previous["pane"]])["agent"], None
    except HerdrError as error:
        if error.code != "agent_not_found":
            raise
    # An absent agent can also mean the pane now hosts the user's shell or editor.
    # Only a confirmed closed pane permits automatic layout recreation.
    try:
        pane = invoke(["pane", "get", previous["pane"]])["pane"]
    except HerdrError as error:
        if error.code != "pane_not_found":
            raise
        print(f"上次 Herdr 面板 {previous['pane']} 已关闭,将重新创建任务标签页。", flush=True)
        return None, None
    may_start = not previous.get("replacement_pending") and (
        previous.get("phase") in ("starting", "completed") or previous.get("prompt_attempted") is False
    )
    if may_start and not pane.get("agent"):
        actual_cwd = pane.get("foreground_cwd") or pane.get("cwd")
        if not actual_cwd or Path(actual_cwd).resolve() != cwd:
            raise HerdrError("原 Herdr 面板的项目目录已更换,保留面板且不启动任务。")
        info = invoke(["pane", "process-info", "--pane", previous["pane"]])["process_info"]
        foreground = info.get("foreground_processes", [])
        shell_pid = info.get("shell_pid")
        if shell_pid and len(foreground) == 1 and foreground[0].get("pid") == shell_pid:
            print(f"原 Herdr 面板 {previous['pane']} 已回到 shell,将在原面板启动新 Agent。", flush=True)
            return None, pane
        raise HerdrError("原面板仍在执行启动或其他命令,跳过本次触发。", "pane_busy")
    raise HerdrError("原 Herdr 面板仍存在但 Agent 已退出,请检查该面板后处理 .herdr-run.json。")


def verify_agent_identity(agent, previous, cwd):
    prefix = "ah-" + hashlib.sha256(str(HERE).encode()).hexdigest()[:8] + "-"
    name = agent.get("name", "")
    expected = previous.get("agent_name")
    owned = name == expected if expected else name.startswith(prefix)
    actual_cwd = agent.get("foreground_cwd") or agent.get("cwd")
    if not owned or (actual_cwd and Path(actual_cwd).resolve() != cwd):
        raise HerdrError("原面板的 Agent 或项目已更换,请检查后处理 .herdr-run.json。")


def verify_previous_agent(agent, previous, cwd):
    verify_agent_identity(agent, previous, cwd)
    if agent["agent_status"] not in ("idle", "done"):
        raise HerdrError("原 Agent 状态已变化,保留面板且不发起新任务。")


def create_task_pane(invoke, previous, agent, session, cwd, title, available_pane=None):
    if available_pane:
        print(f"沿用原面板 {available_pane['pane_id']};标签页不变,未新建标签页。", flush=True)
        return available_pane
    if agent:
        verify_previous_agent(agent, previous, cwd)
        save_state({**previous, "phase": "attention"})
        # Replace only this task's finished agent pane, keeping its tab and sidecars.
        pane = invoke(["pane", "split", previous["pane"], "--direction", "right",
                       "--cwd", str(cwd), "--no-focus"])["pane"]
        print(f"换新布局:在原标签页 {pane.get('tab_id') or previous.get('tab') or '未记录'} 新建面板 "
              f"{pane['pane_id']},关闭旧面板 {previous['pane']};未新建标签页。", flush=True)
        return pane
    workspaces = invoke(["workspace", "list"])["workspaces"]
    matches = [item for item in workspaces if (
        item.get("worktree") and Path(item["worktree"]["checkout_path"]).resolve() == cwd
    ) or (
        not item.get("worktree") and previous and previous.get("cwd") == str(cwd)
        and previous.get("session") == session and previous.get("workspace") == item["workspace_id"]
    )]
    if len(matches) > 1:
        raise HerdrError("多个 Herdr 工作区对应此项目,请保留一个目标工作区后重试。")
    save_state({"session": session, "cwd": str(cwd), "phase": "attention"})
    if matches:
        created = invoke(["tab", "create", "--workspace", matches[0]["workspace_id"],
                          "--cwd", str(cwd), "--label", title, "--no-focus"])
        pane = created["root_pane"]
        print(f"新建标签页 {pane.get('tab_id') or '未记录'}(工作区 {pane['workspace_id']}),"
              f"面板 {pane['pane_id']};本轮新建标签页。", flush=True)
        return pane
    created = invoke(["workspace", "create", "--cwd", str(cwd),
                      "--label", cwd.name, "--no-focus"])
    pane = created["root_pane"]
    print(f"新建工作区 {pane['workspace_id']} 与标签页 {pane.get('tab_id') or '未记录'},"
          f"面板 {pane['pane_id']};本轮新建标签页。", flush=True)
    return pane


def wait_for_result(invoke, name, baseline_seq, timeout_seconds=1800):
    deadline = time.monotonic() + timeout_seconds
    observed_activity = False
    last_status = None
    while time.monotonic() < deadline:
        agent = invoke(["agent", "get", name])["agent"]
        status = agent["agent_status"]
        observed_activity = observed_activity or status in ("working", "blocked")
        completed = (agent.get("completion_seq") or 0) > baseline_seq
        if status != last_status:
            description = {"unknown": "状态识别中", "working": "正在执行",
                           "blocked": "等待输入", "idle": "等待执行结果", "done": "已结束"}.get(status, status)
            print(f"Herdr 任务进行中:{description}。", flush=True)
            last_status = status
        if status == "blocked" or (status in ("idle", "done") and (observed_activity or completed)):
            return {"agent": agent}
        time.sleep(2)
    raise HerdrError("等待 Herdr 任务结果超时;会话已保留,请在 Herdr 查看进展。", "timeout")


# Claude Code marks responses with ⏺, Codex with • and its errors with ■.
RESPONSE_MARKERS = ("⏺", "•", "■")
PROVIDER_ERROR = re.compile(r"API Error:|This content was flagged for possible", re.IGNORECASE)


def ended_with_provider_error(output):
    """Judge only the last top-level response; errors quoted earlier or in sub-items never count."""
    heads = [line[1:].strip() for line in output.splitlines() if line[:1] in RESPONSE_MARKERS]
    return bool(heads) and PROVIDER_ERROR.match(heads[-1]) is not None


def finish_result(invoke, state, result, target):
    status = result["agent"]["agent_status"]
    state["phase"] = "completed" if status in ("idle", "done") else "attention"
    state.pop("provider_error", None)
    save_state(state)
    # Scrolling application history is only safe once the agent is idle.
    source = ["--source", "recent-unwrapped", "--lines", "160"] if state["phase"] == "completed" else ["--source", "visible"]
    output = invoke(["agent", "read", target, *source], text_output=True)
    print(output, flush=True)
    if state["phase"] == "completed" and ended_with_provider_error(output):
        # This turn's completion becomes the baseline, so the next round replaces
        # the session instead of collecting the same refusal again.
        state.update(phase="attention", provider_error=True,
                     completion_seq=result["agent"].get("completion_seq") or state.get("completion_seq", 0))
        save_state(state)
        print("Agent 回合以提供方报错结束,本轮不计为完成;下一轮在原标签页换新 Agent,不向该会话重发。", flush=True)
    print(f"Herdr 会话状态:{status}。请在 Herdr 查看任务结果;会话就绪不代表验证已通过。")
    return 0 if state["phase"] == "completed" else 1


def submit_and_collect(invoke, state, prompt, wait_timeout_seconds, recovery=None):
    # Persist before submission: timeout does not prove the prompt was not delivered.
    save_state(state)
    try:
        delivered = submit_prompt(invoke, state, prompt)
        if recovery:
            if not delivered:
                return 1
            state["recovery"]["status"] = "submitted"
            state["continuation"]["status"] = "submitted"
            save_state(state)
            return 0
        result = wait_for_result(invoke, state["agent_name"], state["completion_seq"], wait_timeout_seconds)
    except (HerdrError, subprocess.TimeoutExpired):
        state["phase"] = "attention"
        save_state(state)
        raise
    return finish_result(invoke, state, result, state["agent_name"])


def session_age(previous, now):
    """Seconds since this task started the Agent; records from before the start time existed count from now."""
    if "agent_started_at" not in previous:
        return 0
    started = previous["agent_started_at"]
    if isinstance(started, bool) or not isinstance(started, (int, float)):
        return None
    age = now - started
    return age if math.isfinite(age) and age >= 0 else None


def describe_duration(seconds):
    minutes = int(seconds // 60)
    return f"{minutes // 60} 小时 {minutes % 60} 分钟" if minutes >= 60 else f"{minutes} 分钟"


def describe_moment(epoch):
    """Absolute local time; a session can outlive the day it started on."""
    return time.strftime("%Y-%m-%d %H:%M", time.localtime(epoch))


def describe_rotation(previous, limit_seconds, now):
    """Say when this conversation will be replaced, so the run log answers it directly."""
    started = previous.get("agent_started_at")
    if not isinstance(started, (int, float)) or isinstance(started, bool):
        return "会话开始时间未知,无法给出换新时间"
    due = started + limit_seconds
    if due <= now:
        return f"换新已到期({describe_moment(due)})"
    return f"换新时间 {describe_moment(due)}({describe_duration(due - now)}后)"


def describe_start(previous):
    started = previous.get("agent_started_at")
    if not isinstance(started, (int, float)) or isinstance(started, bool):
        return "开始时间未记录"
    return f"{describe_moment(started)} 开始"


def reuse_agent(invoke, previous, agent, cwd, prompt, wait_timeout_seconds, started):
    """Continue a finished round in the same conversation; identity and layout stay unchanged."""
    verify_previous_agent(agent, previous, cwd)
    state = {**previous, "phase": "submitted", "prompt_attempted": True,
             "completion_seq": agent.get("completion_seq") or 0, "agent_started_at": started}
    return submit_and_collect(invoke, state, prompt, wait_timeout_seconds)


def continue_existing(invoke, previous, agent, cwd, prompt, continuation_id):
    verify_previous_agent(agent, previous, cwd)
    prior = previous.get("continuation", {})
    if prior.get("id") == continuation_id:
        print("同一续作已尝试投递,保留结果且不重复发送。", flush=True)
        return 0
    if prior.get("status") == "uncertain" and (agent.get("completion_seq") or 0) <= (previous.get("completion_seq") or 0):
        raise HerdrError("上次续作投递结果仍不明确,先核对原会话,不重复发送。")
    state = {**previous, "phase": "submitted", "completion_seq": agent.get("completion_seq") or 0,
             "continuation": {"id": continuation_id, "status": "uncertain"}}
    save_state(state)
    if not submit_prompt(invoke, state, prompt):
        return 1
    state["continuation"]["status"] = "submitted"
    save_state(state)
    print("续作已投递到原 Agent,会话和业务身份保持不变;未创建新 Agent。", flush=True)
    return 0


def run(binary, config, session, kind, extra, prompt, cwd, bridge, title, wait_timeout_seconds=1800,
        reconcile_only=False, continuation_prompt="", continuation_id="", recover_closed=False, recovery_pane=""):
    sessions = command(binary, ["session", "list", "--json"])["sessions"]
    if not any(item.get("name") == session and item.get("running") for item in sessions):
        raise HerdrError(f"Herdr 会话 {session} 未运行,请先在 Herdr 中启动该会话。")
    prefix = ["--session", session]
    invoke = lambda args, timeout=40, text_output=False: command(binary, prefix + args, timeout, text_output)
    previous = None
    if STATE.exists():
        previous = json.loads(STATE.read_text(encoding="utf-8"))
    try:
        agent, available_pane = previous_agent(invoke, previous, session, cwd)
    except HerdrError as error:
        if error.code != "pane_busy":
            raise
        print(error, flush=True)
        return 0
    if agent:
        verify_agent_identity(agent, previous, cwd)
        status = agent["agent_status"]
        if status == "working":
            if reconcile_only and previous.get("phase") == "completed":
                save_state({**previous, "phase": "attention"})
            print(f"上次 Herdr 任务仍在运行:{previous['pane']};跳过本次触发,不排队或重复发起。", flush=True)
            return 0
        if status == "unknown":
            if reconcile_only:
                if previous.get("phase") == "completed":
                    save_state({**previous, "phase": "attention"})
                print("原 Agent 状态尚未确认,仅保留已有执行,不投递任务。", flush=True)
                return 0
            if previous.get("phase") == "starting" and previous.get("prompt_attempted") is False:
                print("上次 Herdr Agent 仍在启动,跳过本次触发;尚未投递提示词。", flush=True)
                return 0
            target = previous.get("agent_name") or previous["pane"]
            print(f"保留上次 Herdr 会话 {previous['pane']},继续等待状态与结果;不重复投递。", flush=True)
            result = wait_for_result(invoke, target, previous.get("completion_seq") or 0,
                                     wait_timeout_seconds)
            return finish_result(invoke, previous, result, target)
        if continuation_prompt:
            return continue_existing(invoke, previous, agent, cwd, continuation_prompt, continuation_id)
        if previous.get("phase") in ("submitted", "attention") and status in ("idle", "done") and (
            (agent.get("completion_seq") or 0) > (previous.get("completion_seq") or 0)
        ):
            verify_previous_agent(agent, previous, cwd)
            print("上次 Herdr Agent 已完成,仅收回已有结果;本次不重复投递。", flush=True)
            return finish_result(invoke, previous, {"agent": agent}, previous["pane"])
        if previous.get("phase") == "submitted" and status in ("idle", "done"):
            print('旧提示词已尝试投递,但还没有可核实的完成记录;保留原 Agent,不重复发起。', flush=True)
            return 1
        if previous.get("provider_error") and previous.get("phase") == "attention" and status in ("idle", "done"):
            if reconcile_only:
                print("上轮回合以提供方报错结束,保留原会话;下一次执行在原标签页换新 Agent,不向该会话重发。", flush=True)
                return 1
            print("上轮回合以提供方报错结束:本轮在原标签页换新 Agent,不复用该会话。", flush=True)
            # A failed replacement leaves an ordinary attention record and is not retried every round.
            previous = {key: value for key, value in previous.items() if key != "provider_error"}
        elif status not in ("idle", "done") or previous.get("phase") == "attention":
            print(f"保留上次会话:{previous['pane']},状态 {status};不重复发起。", flush=True)
            return 1
    reuse_seconds = config["execution"].get("session_reuse_seconds", 0)
    if agent and reuse_seconds and not reconcile_only and previous.get("phase") == "completed":
        now = time.time()
        # Only a named Agent can be addressed again; older records are replaced instead.
        age = session_age(previous, now) if previous.get("agent_name") else None
        limit = describe_duration(reuse_seconds)
        tab = previous.get("tab") or "未记录"
        if age is not None and age < reuse_seconds:
            note = "" if "agent_started_at" in previous else "(旧记录没有启动时间,从本轮开始计时)"
            print(f"复用原 Agent {previous['agent_name']}(标签页 {tab},面板 {previous['pane']}):"
                  f"会话{describe_moment(previous.get('agent_started_at', now))}开始,已运行 "
                  f"{describe_duration(age)}{note},复用上限 {limit};"
                  f"{describe_rotation(previous, reuse_seconds, now)}。"
                  "本轮在原会话继续,未新建标签页或面板。", flush=True)
            return reuse_agent(invoke, previous, agent, cwd, prompt, wait_timeout_seconds,
                               previous.get("agent_started_at", int(now)))
        if age is None:
            print(f"原 Agent 记录无法确认运行时长(标签页 {tab},面板 {previous['pane']});"
                  "本轮在原标签页换新 Agent,未新建标签页。", flush=True)
        else:
            print(f"原 Agent 会话{describe_start(previous)}开始,已运行 "
                  f"{describe_duration(age)},达到复用上限 {limit};"
                  "本轮在原标签页换新 Agent,未新建标签页。", flush=True)
    recovery = None
    if recover_closed and not agent:
        if not previous or available_pane or previous.get("session") != session or previous.get("cwd") != str(cwd):
            raise HerdrError("恢复须核实原任务已关闭的面板及项目;不接管现有 Shell。")
        prior = previous.get("continuation", {})
        if prior.get("status") in ("requested", "uncertain") or previous.get("phase") not in ("completed", "submitted"):
            raise HerdrError("原执行或提示词送达未决,不能创建恢复 Agent。")
        if previous.get("recovery", {}).get("id") == continuation_id:
            raise HerdrError("同一恢复已尝试,不重复创建 Agent;先核验原回执。")
        if not continuation_prompt or not continuation_id:
            raise HerdrError("恢复须提供明确提示词与唯一 ID。")
        try:
            invoke(["agent", "get", previous["agent_name"]])
        except HerdrError as error:
            if error.code != "agent_not_found":
                raise
        else:
            raise HerdrError("原 Agent 仍存在,不能创建第二个执行者。")
        archive = STATE_DIR / ("closed-agent-" + uuid.uuid4().hex + ".json")
        # The shared caller lock is held; preserve the original receipt before layout changes.
        save_state(previous, archive)
        recovery = {"id": continuation_id, "previous_checkpoint": str(archive), "status": "requested"}
        prompt = continuation_prompt
        if recovery_pane:
            available_pane = invoke(["pane", "get", recovery_pane])["pane"]
            actual = available_pane.get("foreground_cwd") or available_pane.get("cwd")
            if available_pane.get("agent") or not actual or Path(actual).resolve() != cwd:
                raise HerdrError("恢复目标面板非空或项目不匹配,未启动。")
    if reconcile_only and not recovery:
        print("本次仅核对已有执行,不创建面板、启动 Agent 或投递提示词。", flush=True)
        return 1 if previous and previous.get("phase") != "completed" else 0
    created = create_task_pane(invoke, previous, agent, session, cwd, title, available_pane)
    pane = created["pane_id"]
    name = "ah-" + hashlib.sha256(str(HERE).encode()).hexdigest()[:8] + "-" + uuid.uuid4().hex[:6]
    state = {"session": session, "pane": pane, "cwd": str(cwd), "workspace": created["workspace_id"],
             "tab": created.get("tab_id"), "agent_name": name, "phase": "starting", "prompt_attempted": False,
             "agent_started_at": int(time.time())}
    if recovery:
        state["recovery"] = recovery
        state["continuation"] = {"id": continuation_id, "status": "uncertain"}
    if agent:
        state["replacement_pending"] = previous["pane"]
    save_state(state)
    if agent:
        verify_previous_agent(invoke(["agent", "get", previous["pane"]])["agent"], previous, cwd)
        invoke(["pane", "close", previous["pane"]])
        state.pop("replacement_pending")
        save_state(state)
    print(f"Herdr 会话 {session},Agent {name},面板 {pane}。", flush=True)
    # Every executable uses core/herdr/agents, just like the sidebar and terminal.
    # Its startup rules (including shell readiness) have one authoritative implementation.
    started = command(bridge, ["--json", "agents", "launch", "start", kind,
                              "--session", session, "--pane", pane, "--name", name,
                              "--arguments-json", json.dumps(extra)] + (["--scheme", config["launch"]["scheme_id"]] if config["launch"].get("scheme_id") else []) + (["--expected-plan", config["launch"]["_expected_plan"]] if config["launch"].get("_expected_plan") else []), timeout=80)
    state["phase"] = "submitted"
    state["prompt_attempted"] = True
    state["completion_seq"] = started.get("agent", {}).get("completion_seq") or 0
    return submit_and_collect(invoke, state, prompt, wait_timeout_seconds, recovery)