卧槽,我真解决了让 Codex 连续工作 8 小时的问题,上下文都不会爆掉! 方案就是让 Claude Code 去当监工监督...

方案就是让 Claude Code 去当监工监督 Codex 干活,大概的步骤如下:
1. 首先要让 Codex 生成一个任务的 TODO List,就是那种能一步步完成的
2. 然后让 Codex 更新 Agents md 文件,加上说明,如果输入 continue,要读取 TODO 文件,去选取任务,执行后更新 TODO
3. 让 Claude Code 去执行命令:
> export TERM=xterm && codex exec "continue to next task" --full-auto
也就是 Claude Code 去启动 codex 并传入提示词 "continue to next task"
并且监控 codex 的执行,如果当前任务完成了,就杀掉进程,重新执行上面的指令下一个任务。
由于每次都是新的 session,所以 codex 的上下文每次用的不多,不会爆掉。
那么怎么保证 Claude Code 的 Context 不爆掉呢?毕竟codex输出的信息也不少
答案就是让 Claude Code 每次去启动 codex 和监控 codex 执行的时候,都起一个子 Agent,这样每个子 Agent 都有独立的上下文,主 Agent 只有子Agent完成的上下文,占用空间极小。
完整的提示词和运行效果在图1可以看到:
> 帮我在当前目录下,新开一个agent,使用 export TERM=xterm && codex exec "continue to next task" --full-auto 命令开启一个 codex 进程, 注意观察任务执行情况,如果当前任务完成(任务运行时间较长,可以多等一会),就结束进程,然后重新开个agent运行相同指令让它继续
> 注意每次打开codex和监控它运行都调用一个新agent (Task Tool)来执行这个操作以避免主agent上下文太长
BTW: 监控 codex 执行这任务理论上来说 Gemini cli和 Codex cli 也能做,但是我没成功。
要点:
1. Worker 要有 TODO List,并且 Agents/Claude Code MD 要有引导,这样每次固定提示词(continue)能继续任务
2. Worker 要开子进程避免上下文爆掉
3. Manager 去管理 Worker 干活要开子 Agent,避免 Manager 的上下文爆掉
x.com/zhangjintao902…
1. 探索各种可能性
2. 这样用 AI 监测,比脚本的好处是:
- 简单易行(但是费 Tokens)
- 可以根据任务执行的结果动态处理, Prompt 可以不是固定的
x.com/geniusvczh/sta…
#!/usr/bin/env bash
#
# codex_task_monitor.sh — minimal scheduler that runs
# `export TERM=xterm && codex exec "continue to next task" --full-auto`
# every five minutes and reports each run's outcome.
set -euo pipefail
cmd=(codex exec "continue to next task" --full-auto)
while true; do
echo "[codex-monitor] starting run at $(date -u '+%Y-%m-%dT%H:%M:%SZ')"
if TERM=xterm "${cmd[@]}"; then
echo "[codex-monitor] codex exec completed successfully (exit 0)."
else
exit_code=$?
echo "[codex-monitor] codex exec exited with errors (exit $exit_code)."
fi
echo "[codex-monitor] waiting five minutes before the next run..."
sleep 300
done
x.com/Stephen4171127…
#!/usr/bin/env bash
#
# codex_task_monitor.sh — minimal scheduler that runs
# `export TERM=xterm && codex exec "continue to next task" --full-auto`
# on a loop, restarting immediately if the codex process stays silent for
# longer than the configured inactivity timeout.
set -euo pipefail
cmd=(codex exec "continue to next task" --full-auto)
if [[ -n "${CODEX_MONITOR_CMD:-}" ]]; then
cmd=(bash -lc "${CODEX_MONITOR_CMD}")
fi
terminate_codex_process() {
local pid=$1
local grace_seconds=${2:-5}
if ! kill -TERM "$pid" 2>/dev/null; then
return 0
fi
for (( i = 0; i < grace_seconds; i++ )); do
if ! kill -0 "$pid" 2>/dev/null; then
return 0
fi
sleep 1
done
kill -KILL "$pid" 2>/dev/null || true
}
# Allow overrides via env vars: CODEX_MONITOR_INACTIVITY_TIMEOUT (seconds)
# and CODEX_MONITOR_INTERVAL_SECONDS (pause between completed runs).
inactivity_timeout=${CODEX_MONITOR_INACTIVITY_TIMEOUT:-60}
pause_seconds=${CODEX_MONITOR_INTERVAL_SECONDS:-60}
run_codex_with_watchdog() {
local inactivity_limit=$1
local last_output
last_output=$(date +%s)
local exit_code=0
local inactivity_triggered=0
local line
local output_dir
output_dir=$(mktemp -d -t codex-monitor-XXXXXX)
local output_fifo="$output_dir/codex-output"
mkfifo "$output_fifo"
TERM=xterm "${cmd[@]}" 2>&1 >"$output_fifo" &
local codex_pid=$!
exec 3<"$output_fifo"
rm -f "$output_fifo"
while true; do
local read_something=0
if IFS= read -r -t 1 -u 3 line; then
read_something=1
last_output=$(date +%s)
printf '%s\n' "$line"
fi
if (( read_something )); then
continue
fi
if ! kill -0 "$codex_pid" 2>/dev/null; then
# Flush any remaining output before capturing the exit code.
while IFS= read -r -u 3 line; do
printf '%s\n' "$line"
done
if ! wait "$codex_pid"; then
exit_code=$?
else
exit_code=0
fi
break
fi
local now
now=$(date +%s)
if (( now - last_output >= inactivity_limit )); then
echo "[codex-monitor] no output from codex for ${inactivity_limit}s; terminating run..." >&2
terminate_codex_process "$codex_pid"
if ! wait "$codex_pid"; then
exit_code=$?
else
exit_code=0
fi
inactivity_triggered=1
break
fi
done
exec 3<&-
rm -rf "$output_dir"
if (( inactivity_triggered )); then
return 124
fi
return "$exit_code"
}
while true; do
echo "[codex-monitor] starting run at $(date -u '+%Y-%m-%dT%H:%M:%SZ')"
if run_codex_with_watchdog "$inactivity_timeout"; then
echo "[codex-monitor] codex exec completed successfully (exit 0)."
echo "[codex-monitor] waiting ${pause_seconds}s before the next run..."
sleep "$pause_seconds"
continue
else
exit_code=$?
if [[ $exit_code -eq 124 ]]; then
echo "[codex-monitor] codex exec produced no output for ${inactivity_timeout}s. Restarting immediately."
continue
fi
echo "[codex-monitor] codex exec exited with errors (exit $exit_code)."
echo "[codex-monitor] waiting ${pause_seconds}s before the next run..."
sleep "$pause_seconds"
fi
done
x.com/marsonshine/st…



