Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix: keep Activity Log in sync while a run is Pending/Running (#824)

Hiep Lea3feade99f9b
diff --git a/__tests__/components/automations/detail/activity-log-item.test.tsx b/__tests__/components/automations/detail/activity-log-item.test.tsx
index 663f983211..d417d0948d 100644
--- a/__tests__/components/automations/detail/activity-log-item.test.tsx
+++ b/__tests__/components/automations/detail/activity-log-item.test.tsx
@@ -135,6 +135,57 @@ describe("ActivityLogItem β€” logs button", () => {
});
});
+describe("ActivityLogItem β€” Conversation not created label", () => {
+ beforeEach(() => {
+ __resetActiveStoreForTests();
+ setRegisteredBackends([localBackend]);
+ setActiveSelection({ backendId: localBackend.id });
+ });
+
+ afterEach(() => {
+ __resetActiveStoreForTests();
+ });
+
+ it("hides the 'Conversation not created' label while the run is Pending without a conversation", () => {
+ // Arrange: a freshly-dispatched run that hasn't yet been linked to a
+ // conversation by the backend. The label would falsely imply terminal
+ // failure during this transient window.
+ const run = makeRun({
+ status: AutomationRunStatus.PENDING,
+ conversation_id: null,
+ bash_command_id: null,
+ });
+
+ // Act
+ renderItem(run);
+
+ // Assert
+ expect(
+ screen.queryByText((content) => content.includes("NO_CONVERSATION")),
+ ).not.toBeInTheDocument();
+ });
+
+ it("shows the 'Conversation not created' label when the run has Failed without a conversation", () => {
+ // Arrange: a run that reached a terminal state without ever creating a
+ // conversation (e.g. sandbox provisioning error) β€” here the label is
+ // accurate and useful.
+ const run = makeRun({
+ status: AutomationRunStatus.FAILED,
+ conversation_id: null,
+ bash_command_id: null,
+ completed_at: "2026-01-01T10:00:30Z",
+ });
+
+ // Act
+ renderItem(run);
+
+ // Assert
+ expect(
+ screen.queryByText((content) => content.includes("NO_CONVERSATION")),
+ ).toBeInTheDocument();
+ });
+});
+
describe("ActivityLogItem β€” timestamp fallback", () => {
beforeEach(() => {
__resetActiveStoreForTests();
diff --git a/__tests__/hooks/query/use-automations-backend-switch.test.tsx b/__tests__/hooks/query/use-automations-backend-switch.test.tsx
index 7dd5d8e968..83b44fd9bc 100644
--- a/__tests__/hooks/query/use-automations-backend-switch.test.tsx
+++ b/__tests__/hooks/query/use-automations-backend-switch.test.tsx
@@ -14,19 +14,24 @@ import {
useAutomations,
useDispatchAutomation,
} from "#/hooks/query/use-automations";
-import { useAutomationDetail } from "#/hooks/query/use-automation-detail";
+import {
+ useAutomationDetail,
+ useAutomationRuns,
+} from "#/hooks/query/use-automation-detail";
import type { Backend } from "#/api/backend-registry/types";
import { AutomationRunStatus } from "#/types/automation";
import type {
Automation,
AutomationRun,
AutomationsResponse,
+ AutomationRunsResponse,
} from "#/types/automation";
vi.mock("#/api/automation-service/automation-service.api", () => ({
default: {
getAutomations: vi.fn(),
getAutomation: vi.fn(),
+ getAutomationRuns: vi.fn(),
dispatchAutomation: vi.fn(),
},
}));
@@ -92,6 +97,7 @@ beforeEach(() => {
__resetActiveStoreForTests();
vi.mocked(AutomationService.getAutomations).mockReset();
vi.mocked(AutomationService.getAutomation).mockReset();
+ vi.mocked(AutomationService.getAutomationRuns).mockReset();
vi.mocked(AutomationService.dispatchAutomation).mockReset();
vi.mocked(AutomationService.dispatchAutomation).mockResolvedValue(
automationRun,
@@ -155,3 +161,72 @@ describe("automation hooks β€” backend switch", () => {
expect(AutomationService.dispatchAutomation).toHaveBeenCalledWith("auto-1");
});
});
+
+describe("useAutomationRuns β€” polling", () => {
+ const pendingRun: AutomationRun = {
+ id: "run-pending",
+ status: AutomationRunStatus.PENDING,
+ conversation_id: null,
+ bash_command_id: null,
+ error_detail: null,
+ started_at: "2026-01-02T00:00:00Z",
+ completed_at: null,
+ };
+ const completedRun: AutomationRun = {
+ id: "run-pending",
+ status: AutomationRunStatus.COMPLETED,
+ conversation_id: "conv-1",
+ bash_command_id: "cmd-1",
+ error_detail: null,
+ started_at: "2026-01-02T00:00:00Z",
+ completed_at: "2026-01-02T00:00:30Z",
+ };
+
+ it(
+ "re-fetches while a run is non-terminal, and stops once all runs are terminal",
+ async () => {
+ // Arrange: first fetch returns a PENDING run (polling should engage);
+ // subsequent fetches return a COMPLETED run (polling should then stop).
+ const pendingResponse: AutomationRunsResponse = {
+ runs: [pendingRun],
+ total: 1,
+ };
+ const completedResponse: AutomationRunsResponse = {
+ runs: [completedRun],
+ total: 1,
+ };
+ vi.mocked(AutomationService.getAutomationRuns)
+ .mockResolvedValueOnce(pendingResponse)
+ .mockResolvedValue(completedResponse);
+
+ // Act
+ renderHook(
+ () => useAutomationRuns({ id: "auto-1", limit: 20, offset: 0 }),
+ { wrapper: makeWrapper() },
+ );
+
+ // Assert: the initial fetch fires once.
+ await waitFor(() => {
+ expect(AutomationService.getAutomationRuns).toHaveBeenCalledTimes(1);
+ });
+
+ // The cached data still contains a PENDING run, so refetchInterval
+ // engages and a second fetch arrives within the poll window.
+ await waitFor(
+ () => {
+ expect(AutomationService.getAutomationRuns).toHaveBeenCalledTimes(2);
+ },
+ { timeout: 5000 },
+ );
+
+ // The second fetch returned a COMPLETED run, so polling should stop.
+ // Give the would-be next poll window plenty of slack and assert no
+ // further calls happen.
+ await new Promise((resolve) => {
+ setTimeout(resolve, 4000);
+ });
+ expect(AutomationService.getAutomationRuns).toHaveBeenCalledTimes(2);
+ },
+ 15000,
+ );
+});
diff --git a/src/components/features/automations/detail/activity-log-item.tsx b/src/components/features/automations/detail/activity-log-item.tsx
index 73d30438c4..715e31ed46 100644
--- a/src/components/features/automations/detail/activity-log-item.tsx
+++ b/src/components/features/automations/detail/activity-log-item.tsx
@@ -2,7 +2,7 @@ import { useState } from "react";
import { useTranslation } from "react-i18next";
import { I18nKey } from "#/i18n/declaration";
import TerminalIcon from "#/icons/terminal.svg?react";
-import type { AutomationRun } from "#/types/automation";
+import { AutomationRunStatus, type AutomationRun } from "#/types/automation";
import { RunStatusBadge } from "./run-status-badge";
import { RunLogsModal } from "./run-logs-modal";
@@ -37,6 +37,16 @@ export function ActivityLogItem({ run }: ActivityLogItemProps) {
const { t, i18n } = useTranslation("openhands");
const hasConversation = !!run.conversation_id;
const hasBashCommand = !!run.bash_command_id;
+ // Only surface "Conversation not created" when the run has reached a
+ // terminal status without a conversation β€” i.e. the conversation truly
+ // will not be created (e.g. sandbox provisioning failed). While
+ // PENDING/RUNNING the conversation may still be in the process of being
+ // created, and the status badge already communicates the in-progress
+ // state.
+ const isTerminal =
+ run.status === AutomationRunStatus.COMPLETED ||
+ run.status === AutomationRunStatus.FAILED;
+ const showNoConversationLabel = !hasConversation && isTerminal;
const [logsOpen, setLogsOpen] = useState(false);
// The backend leaves started_at unset (epoch/zero) while a run is Pending
// and only populates it once execution begins. Show the user's local time
@@ -81,7 +91,7 @@ export function ActivityLogItem({ run }: ActivityLogItemProps) {
<>
<div className="flex items-center gap-3">
<span className="text-sm text-content">{formattedTimestamp}</span>
- {!hasConversation && (
+ {showNoConversationLabel && (
<span className="text-xs text-muted italic">
({t(I18nKey.AUTOMATIONS$DETAIL$NO_CONVERSATION)})
</span>
diff --git a/src/hooks/query/use-automation-detail.ts b/src/hooks/query/use-automation-detail.ts
index 8ff1d8ff5d..0174815299 100644
--- a/src/hooks/query/use-automation-detail.ts
+++ b/src/hooks/query/use-automation-detail.ts
@@ -1,6 +1,10 @@
import { useQuery } from "@tanstack/react-query";
import AutomationService from "#/api/automation-service/automation-service.api";
import { useActiveBackend } from "#/contexts/active-backend-context";
+import {
+ AutomationRunStatus,
+ type AutomationRunsResponse,
+} from "#/types/automation";
export const AUTOMATION_DETAIL_QUERY_KEY = ["automation-detail"] as const;
export const AUTOMATION_RUNS_QUERY_KEY = ["automation-runs"] as const;
@@ -47,5 +51,17 @@ export function useAutomationRuns(options: UseAutomationRunsOptions) {
queryFn: () => AutomationService.getAutomationRuns(id, limit, offset),
staleTime: 60 * 1000,
enabled: !!id && enabled,
+ // Poll while any run is non-terminal so status and conversation_id
+ // transitions appear without a manual refresh.
+ refetchInterval: (query) => {
+ const data = query.state.data as AutomationRunsResponse | undefined;
+ if (!data) return false;
+ const hasInFlightRun = data.runs.some(
+ (run) =>
+ run.status === AutomationRunStatus.PENDING ||
+ run.status === AutomationRunStatus.RUNNING,
+ );
+ return hasInFlightRun ? 3000 : false;
+ },
});
}