Skip to content

Open-source SEO, GEO, and marketing skills for AI agents.

fix(tasks): keep auto-kickoff + extend polling grace after task terminates

Yuting Zhong9e83ec5885b8
diff --git a/src/components/agent-task-workspace.tsx b/src/components/agent-task-workspace.tsx
index d0065698..d919bf0c 100644
--- a/src/components/agent-task-workspace.tsx
+++ b/src/components/agent-task-workspace.tsx
@@ -1,6 +1,6 @@
"use client";
-import { useCallback, useMemo, useState, useTransition } from "react";
+import { useCallback, useMemo, useRef, useState, useTransition } from "react";
import Link from "next/link";
import { useRouter, useSearchParams } from "next/navigation";
import {
@@ -314,22 +314,33 @@ function SelectedTaskPanel({
//
// Why: OpenClaw doesn't flush the JSONL file until session.ended —
// a long-running audit can spend minutes calling MCP tools (which
- // update the DB synchronously) while the JSONL stays empty. If we
- // only refresh on `newEvents > 0`, the badge stays stuck on
- // "Working" until the very end of the session even though the
- // agent already called submit_task_status seconds earlier. Polling
- // is cheap (every 800ms in-flight, 2s otherwise) and the refresh
- // is RSC-only.
+ // update the DB synchronously) while the JSONL stays empty.
//
- // When the task is terminal AND the poll returned no new events,
- // we stop polling — the transcript is fully drained.
+ // Stop-polling rule: we hang on for a few empty ticks AFTER the task
+ // hits a terminal status. The JSONL flush can land AFTER the
+ // status-flip — if we stopped immediately, the transcript would stay
+ // blank until the next manual refresh. Counting empty terminal ticks
+ // and stopping only after a small grace window (~5 s at 2 s cadence)
+ // gives the flush a chance to catch up.
+ const emptyTerminalTicksRef = useRef(0);
+ const GRACE_TICKS_AFTER_TERMINAL = 3;
const onPolled = useCallback(
({ newEvents }: { newEvents: number; fileSize: number }) => {
if (newEvents > 0 || isInFlight) {
router.refresh();
}
- if (!isInFlight && newEvents === 0) return true;
- return false;
+ if (isInFlight) {
+ emptyTerminalTicksRef.current = 0;
+ return false;
+ }
+ if (newEvents > 0) {
+ // Flush landed something. Reset the grace counter — there might
+ // be more bytes coming as OpenClaw drains.
+ emptyTerminalTicksRef.current = 0;
+ return false;
+ }
+ emptyTerminalTicksRef.current += 1;
+ return emptyTerminalTicksRef.current >= GRACE_TICKS_AFTER_TERMINAL;
},
[isInFlight, router],
);
diff --git a/src/server/orchestration/handlers.ts b/src/server/orchestration/handlers.ts
index 53be3301..ce131574 100644
--- a/src/server/orchestration/handlers.ts
+++ b/src/server/orchestration/handlers.ts
@@ -236,17 +236,33 @@ export function handleTaskStatus(
// signal to the user, not something to silently route around. They can
// cancel the dependent or rerun the blocker.
if (newStatus === "done") {
- // Promote dependents from `blocked` to `proposed` so they're eligible
- // to start. We DO NOT fire the kickoff here — server-side kickoffs
- // (runTaskKickoffServerSide) bypass /api/chat's SSE channel, so the
- // user's browser sees nothing live and OpenClaw's buffered JSONL
- // only flushes at session end. Leaving the task in `proposed` lets
- // the task workspace page run its own client-side auto-kickoff via
- // /api/chat the moment the user opens it — that DOES stream events
- // back to the browser. The "Start all" button on the agent's tasks
- // tab is also still available for batch start.
- for (const dep of listTasksBlockedBy(task.id)) {
- clearBlockerAndPromote(dep.id);
+ // Promote dependents from `blocked → proposed` and auto-fire each
+ // kickoff so the audit runs in the background even when the user is
+ // on another page. Visibility comes from the SHADOW TRANSCRIPT that
+ // runTaskKickoffServerSide writes — readTranscriptTail merges it
+ // with OpenClaw's buffered JSONL so polling sees events live.
+ const dependents = listTasksBlockedBy(task.id);
+ const promoted: Task[] = [];
+ for (const dep of dependents) {
+ const p = clearBlockerAndPromote(dep.id);
+ if (p) promoted.push(p);
+ }
+ if (promoted.length > 0) {
+ void (async () => {
+ const { startTaskIfProposed } = await import("./run-task");
+ for (const p of promoted) {
+ try {
+ startTaskIfProposed(p);
+ } catch (err) {
+ console.error(
+ `[block-prop] kickoff failed for ${p.display_id}:`,
+ err,
+ );
+ }
+ }
+ })().catch((err) => {
+ console.error("[block-prop] propagation IIFE failed:", err);
+ });
}
}