Skip to content

Commit 03b4f29

Browse files
committed
fix(ui): show frozen elapsed time for stopped goals
1 parent 25a16dd commit 03b4f29

3 files changed

Lines changed: 188 additions & 2 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Real-browser proof of frozen goal timing on the chat composer.
2+
import { mkdir } from "node:fs/promises";
3+
import path from "node:path";
4+
import { expect, it } from "vitest";
5+
import { installMockGateway } from "../test-helpers/control-ui-e2e.ts";
6+
import { createControlUiE2eSuite } from "./control-ui-e2e-suite.test-support.ts";
7+
8+
const suite = createControlUiE2eSuite({ name: "Control UI goal elapsed time" });
9+
const captureProof = process.env.OPENCLAW_CAPTURE_UI_PROOF === "1";
10+
const artifactDir = path.resolve(".artifacts/control-ui-e2e/goal-elapsed");
11+
12+
suite.define(() => {
13+
it("shows a paused goal's frozen duration before and after opening details and reloading", async () => {
14+
if (captureProof) {
15+
await mkdir(artifactDir, { recursive: true });
16+
}
17+
await suite.withPage(
18+
{
19+
colorScheme: "light",
20+
locale: "en-US",
21+
serviceWorkers: "block",
22+
viewport: { width: 1440, height: 900 },
23+
...(captureProof
24+
? { recordVideo: { dir: artifactDir, size: { width: 1440, height: 900 } } }
25+
: {}),
26+
},
27+
async ({ page }) => {
28+
await installMockGateway(page, {
29+
sessionKey: "agent:main:main",
30+
historyMessages: [
31+
{
32+
role: "assistant",
33+
content: [{ type: "text", text: "Deployment checks are ready." }],
34+
},
35+
],
36+
methodResponses: {
37+
"sessions.list": {
38+
ts: 61_000,
39+
path: "",
40+
count: 1,
41+
defaults: { model: "gpt-5.6-luna", modelProvider: "openai", contextTokens: 128_000 },
42+
sessions: [
43+
{
44+
key: "agent:main:main",
45+
kind: "direct",
46+
displayName: "Deployment checks",
47+
updatedAt: 61_000,
48+
goal: {
49+
schemaVersion: 1,
50+
id: "goal-timing",
51+
objective: "Verify the deployment",
52+
status: "paused",
53+
createdAt: 1_000,
54+
updatedAt: 61_000,
55+
pausedAt: 61_000,
56+
tokenStart: 0,
57+
tokensUsed: 100,
58+
continuationTurns: 0,
59+
},
60+
},
61+
],
62+
},
63+
},
64+
});
65+
await page.goto(`${suite.server.baseUrl}chat/main`);
66+
const goal = page.locator(".agent-chat__goal");
67+
await goal.getByText("Goal paused", { exact: true }).waitFor();
68+
if (captureProof) {
69+
await page.screenshot({ path: path.join(artifactDir, "paused-goal.png") });
70+
}
71+
expect(await goal.locator(".agent-chat__goal-elapsed").textContent()).toBe("1m");
72+
await goal.getByRole("button", { name: "Show goal details" }).click();
73+
expect(await goal.locator(".agent-chat__goal-detail-meta").textContent()).toContain("1m");
74+
await goal.getByRole("button", { name: "Hide goal details" }).click();
75+
expect(await goal.locator(".agent-chat__goal-elapsed").textContent()).toBe("1m");
76+
await page.reload();
77+
await goal.getByText("Goal paused", { exact: true }).waitFor();
78+
expect(await goal.locator(".agent-chat__goal-elapsed").textContent()).toBe("1m");
79+
},
80+
);
81+
});
82+
});
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/* @vitest-environment jsdom */
2+
3+
import { render } from "lit";
4+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
5+
import type { SessionGoal } from "../../api/types.ts";
6+
import { i18n } from "../../i18n/index.ts";
7+
import { renderChatGoal, clearGoalElapsedTimers } from "./components/chat-composer-goal.ts";
8+
import { getChatComposerState, resetChatComposerState } from "./components/chat-composer-state.ts";
9+
10+
const goal: SessionGoal = {
11+
schemaVersion: 1,
12+
id: "goal-timing",
13+
objective: "Verify the deployment",
14+
status: "active",
15+
createdAt: 1_000,
16+
updatedAt: 61_000,
17+
tokenStart: 0,
18+
tokensUsed: 100,
19+
continuationTurns: 0,
20+
};
21+
22+
function mountGoal(initial: SessionGoal) {
23+
const container = document.createElement("div");
24+
document.body.append(container);
25+
const state = getChatComposerState("goal-timing");
26+
const draw = (value: SessionGoal | undefined) =>
27+
render(renderChatGoal(state, value, { canAct: false, requestUpdate: () => {} }), container);
28+
const part = draw(initial);
29+
return {
30+
container,
31+
draw,
32+
part,
33+
elapsed: () => container.querySelector(".agent-chat__goal-elapsed")?.textContent,
34+
};
35+
}
36+
37+
describe("goal elapsed presentation", () => {
38+
beforeEach(async () => {
39+
await i18n.setLocale("en");
40+
vi.useFakeTimers();
41+
vi.setSystemTime(121_000);
42+
});
43+
44+
afterEach(() => {
45+
clearGoalElapsedTimers();
46+
resetChatComposerState();
47+
document.body.replaceChildren();
48+
vi.useRealTimers();
49+
});
50+
51+
it.each([
52+
["active", "2m"],
53+
["paused", "1m"],
54+
["blocked", "1m"],
55+
["usage_limited", "1m"],
56+
["budget_limited", "1m"],
57+
["complete", "1m"],
58+
] as const)("renders elapsed time immediately for %s goals", (status, elapsed) => {
59+
const view = mountGoal({ ...goal, status });
60+
expect(view.elapsed()).toBe(elapsed);
61+
});
62+
63+
it("replaces a live tick with the authoritative stop time and resumes ticking", () => {
64+
const view = mountGoal(goal);
65+
vi.advanceTimersByTime(1_000);
66+
expect(view.elapsed()).toBe("2m");
67+
68+
view.draw({ ...goal, status: "paused", pausedAt: 46_000 });
69+
expect(view.elapsed()).toBe("45s");
70+
vi.advanceTimersByTime(60_000);
71+
expect(view.elapsed()).toBe("45s");
72+
73+
view.draw(goal);
74+
expect(view.elapsed()).toBe("3m");
75+
vi.advanceTimersByTime(60_000);
76+
expect(view.elapsed()).toBe("4m");
77+
78+
view.draw({ ...goal, status: "complete", completedAt: 151_000 });
79+
expect(view.elapsed()).toBe("2m");
80+
vi.advanceTimersByTime(60_000);
81+
expect(view.elapsed()).toBe("2m");
82+
});
83+
84+
it("retires the active timer when the goal is removed", () => {
85+
const view = mountGoal(goal);
86+
expect(vi.getTimerCount()).toBe(1);
87+
view.draw(undefined);
88+
expect(view.container.querySelector(".agent-chat__goal")).toBeNull();
89+
expect(vi.getTimerCount()).toBe(0);
90+
});
91+
92+
it("retires and resumes the active timer with the rendered host connection", () => {
93+
const view = mountGoal(goal);
94+
view.part.setConnected(false);
95+
expect(vi.getTimerCount()).toBe(0);
96+
vi.advanceTimersByTime(60_000);
97+
view.part.setConnected(true);
98+
expect(view.elapsed()).toBe("3m");
99+
expect(vi.getTimerCount()).toBe(1);
100+
});
101+
});

‎ui/src/pages/chat/components/chat-composer-goal.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ function createGoalElapsedRef(goal: SessionGoal) {
3535
clearGoalElapsedTimer(bound);
3636
bound = null;
3737
}
38-
if (!(element instanceof HTMLElement) || goal.status !== "active") {
38+
if (!(element instanceof HTMLElement)) {
3939
return;
4040
}
41-
bound = element;
4241
element.textContent = formatGoalElapsed(goalElapsedMs(goal, Date.now()));
42+
if (goal.status !== "active") {
43+
return;
44+
}
45+
bound = element;
4346
const timer = setInterval(() => {
4447
// Tests and detached renders can drop the pill without a final ref call.
4548
if (!element.isConnected) {

0 commit comments

Comments
 (0)