Skip to content

[Bug] agentmain --task: unconditional 600s reply.txt wait after round 1 keeps unattended tasks alive until the runner kills them (rc=-999) #866

Open

Description

@leonsong09

中文版

摘要

agentmain.py --task 模式在第一轮完成后无条件等待 reply.txt 最长 600 秒(300×2s)。对无人值守的一次性任务(如定时调度)而言,这 600 秒是纯空转,且会让进程在报告早已写完之后继续占着,直到外层 runner 的硬超时把整棵进程树杀掉。

复现

无交互地跑任意 --task 任务:

python agentmain.py --task mytask --input "做一件事" --nobg

第 1 轮结束、output.txt 已写入 [ROUND END] 后,进程不会退出,而是继续存活最长 600 秒等 reply.txt。

根因

agentmain.py(约 423-439 行):

if args.func or args.task:
    agent.peer_hint = False
    with open(infile, encoding='utf-8') as f: raw = f.read()
    while True:
        dq = agent.put_task(raw, source='func' if args.func else 'task')
        while 'done' not in (item := dq.get(timeout=2200)):
            if 'next' in item:
                with open(outfile, 'w', encoding='utf-8') as f: f.write(item.get('next', ''))
        with open(outfile, 'w', encoding='utf-8') as f: f.write(item['done'] + '\n\n[ROUND END]\n')
        if not args.task: break
        consume_file(d, '_stop')  # 已经成功停下来了,避免打断下次reply
        for _ in range(300):  # 等reply.txt,10分钟超时
            time.sleep(2)
            if (raw := consume_file(d, 'reply.txt')): break
        else: break
        nround = nround + 1 if isinstance(nround, int) else 1
        outfile = f'{d}/output{nround}.txt'

reply.txt 走的是外部监督者写文件的方式(见 memory/subagent.md 与 memory/supervisor_sop.md),仓库内无任何代码写入它:

$ git grep -n "reply.txt"
agentmain.py:434:            for _ in range(300):  # 等reply.txt,10分钟超时
agentmain.py:436:                if (raw := consume_file(d, 'reply.txt')): break
memory/subagent.md:15:- 通信:output.txt(`[ROUND END]`=轮完成) → 写reply.txt继续 → 不写10min退出。reply后输出为output1/2/3.txt
memory/supervisor_sop.md:18:- **训练数据变体**:...

所以在没有监督者的场景里,for _ in range(300) 必然跑满 600 秒。

值得注意的是 memory/subagent.md:14 已经记录了这个陷阱:

不要--nobg(会卡在等reply循环),只能后台启动

但 sche_tasks/agent_task_runner.py 恰恰用的是前台 --nobg:

cmd = [sys.executable, _AGENTMAIN, "--task", name,
       "--input", prompt, "--llm_no", str(llm_no), "--nobg"]

影响

以定时任务为例,两条 agent 任务(agent_runs.jsonl 实测):

任务 timeout_s 样本 p50 rc=-999 撞顶
baseline_progress_weekday_1900 1800 25 1020s 3/25(1800.7 / 1800.9 / 1800.8)
upstream_check_weekly_monday_1030 2400 9 1377s 2/9(均 2400.0)

撞顶那三次,报告实际写入时间比被杀早 10 分钟以上:

2026-11-09 20:51:07  rc=-999  1800.7s   report mtime 20:41:06
2026-11-10 20:51:50  rc=-999  1800.9s   report mtime 20:45:44
2026-11-11 20:51:30  rc=-999  1800.8s   report mtime 20:40:59

即:活早干完了,进程还在空转,最后被 taskkill /F /T 杀掉,rc=-999。调用方不得不写"报告已产出 → 判为已交付"的特殊逻辑来兜底:

TimeoutExpired after 1800s (进程树已终止) | 但报告已在本次尝试内产出(21475B,完整性=nl),判为已交付,不重跑

期望行为

--task 模式应当能区分两种调用意图:

  • 有人守着续轮(交互/监督者场景)→ 行为不变,等 reply
  • 无人值守一次性任务 → 第一轮完成即退出

建议方案

方案 A(推荐,改动最小、向后兼容)

给 --task 增加显式开关,默认保持现状(不破坏既有监督者工作流):

parser.add_argument('--no-reply-wait', action='store_true',
                    help='一次性任务:第一轮完成后立即退出,不等 reply.txt')
if not args.task: break
if getattr(args, 'no_reply_wait', False):
    break                      # 无人值守:本轮即终态
consume_file(d, '_stop')
for _ in range(300):
    ...

调用方(如 agent_task_runner.py)显式传 --no-reply-wait。

方案 B(若不想加参数:用 env 探测)

--nobg 语义上已经表示"前台同步等结果",与"允许外部续轮"天然矛盾。可在 --nobg 时默认跳过等待,用 --allow-reply 反向开启。但这会改变现有 --nobg 语义,属于破坏性变更。

方案 C(最小侵入,仅治标)

把等待时长做成可配置(--reply-wait-seconds,默认 600),让调用方设 0:

for _ in range(int(args.reply_wait_seconds) // 2):

附加建议(与主问题独立)

--nobg 的注释"等reply.txt,10分钟超时"容易被读成"等 10 分钟是正常设计"。建议在注释里点明:无监督者时这 10 分钟纯属空转,并交叉引用 memory/subagent.md:14 的警告。


English version

Summary

In agentmain.py, the --task mode blocks for up to 600 seconds (300 × 2s) waiting for a reply.txt after the first round already finished. For unattended one-shot tasks (e.g. a scheduler), this is pure idle time: the process keeps running long after its report has been written, until the outer runner's hard timeout kills the whole process tree.

Root cause

if not args.task: break
consume_file(d, '_stop')
for _ in range(300):  # wait for reply.txt, 10 min timeout
    time.sleep(2)
    if (raw := consume_file(d, 'reply.txt')): break
else: break

reply.txt is designed to be written by an external supervisor (memory/subagent.md, memory/supervisor_sop.md). Nothing in the repo ever writes it — git grep "reply.txt" only finds the read side plus docs.

So without a supervisor, the loop always burns the full 600s.

memory/subagent.md:14 already documents the pitfall — "don't use --nobg, it will hang in the reply-waiting loop" — yet sche_tasks/agent_task_runner.py invokes exactly --task ... --nobg.

Impact (measured)

Task timeout_s n p50 hit limit (rc=-999)
baseline_progress_weekday_1900 1800 25 1020s 3/25
upstream_check_weekly_monday_1030 2400 9 1377s 2/9

In all hit cases the report file was written more than 10 minutes before the kill.

Proposed fix (backward compatible)

Add an explicit opt-in flag, default preserving current behavior:

parser.add_argument('--no-reply-wait', action='store_true',
                    help='one-shot: exit right after round 1, do not wait for reply.txt')

Callers that never intend to feed replies (schedulers, batch runners) pass it. Supervisors keep the current behavior untouched.

Activity

  1. leonsong09 commented on Nov 14, 2026

    @leonsong09
    Author

    Folded into #880 (commit 6 of 6, agentmain.py +9). The three-day rc=-999 table is quoted in full in #880's description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions