Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(home): run the elapsed clock for client-executed runs (#19701)

Arvin Xu276a284ed5fd
diff --git a/packages/database/src/models/__tests__/topic.test.ts b/packages/database/src/models/__tests__/topic.test.ts
index 09e2a97b80f..85e74d562ba 100644
--- a/packages/database/src/models/__tests__/topic.test.ts
+++ b/packages/database/src/models/__tests__/topic.test.ts
@@ -755,10 +755,45 @@ describe('TopicModel', () => {
const byId = Object.fromEntries(result.map((t) => [t.id, t]));
expect(byId['t-run'].runStartedAt).toEqual(new Date('2026-01-02T00:00:00Z'));
- // A run that never wrote an operation row (e.g. client-mode) stays null.
+ // A run with neither an operation row nor a topic stamp stays null.
expect(byId['t-no-op'].runStartedAt).toBeNull();
});
+ // Regression: client-executed runs (desktop CC / in-browser runtime) create
+ // no operation row, so the topic's own stamp is the only start time there is.
+ it('falls back to the topic stamp when the run left no operation row', async () => {
+ await serverDB.insert(topics).values([
+ {
+ id: 't-local',
+ metadata: { runStartedAt: '2026-01-05T00:00:00Z' },
+ status: 'running',
+ title: 'local',
+ userId,
+ },
+ {
+ id: 't-both',
+ metadata: { runStartedAt: '2026-01-06T00:00:00Z' },
+ status: 'running',
+ title: 'both',
+ userId,
+ },
+ ]);
+ await serverDB.insert(agentOperations).values({
+ id: 'op-both',
+ startedAt: new Date('2026-01-07T00:00:00Z'),
+ status: 'running',
+ topicId: 't-both',
+ userId,
+ });
+
+ const result = await topicModel.queryTopics({ statuses: ['running'] });
+ const byId = Object.fromEntries(result.map((t) => [t.id, t]));
+
+ expect(byId['t-local'].runStartedAt).toEqual(new Date('2026-01-05T00:00:00Z'));
+ // Server's own record of the run beats the client-reported stamp.
+ expect(byId['t-both'].runStartedAt).toEqual(new Date('2026-01-07T00:00:00Z'));
+ });
+
it('never resurrects a timer for a non-running topic with a stale running op', async () => {
await serverDB
.insert(topics)
@@ -951,6 +986,41 @@ describe('TopicModel', () => {
expect(cleared.status).toBe('active');
});
+ // Regression: a desktop CC / in-browser run persists nothing but this status
+ // write, so without the stamp the home inbox had no start time for it and
+ // rendered no elapsed clock at all.
+ it('stamps when a client-executed run claimed the topic', async () => {
+ const topic = await topicModel.create({ metadata: { workingDirectory: '/w' }, title: 'run' });
+
+ const [running] = await topicModel.update(topic.id, { status: 'running' });
+
+ expect(running.metadata?.workingDirectory).toBe('/w');
+ expect(new Date(running.metadata!.runStartedAt!).getTime()).toBeGreaterThan(
+ Date.now() - 60_000,
+ );
+ });
+
+ it('keeps the original start when a run resumes from an approval', async () => {
+ const topic = await topicModel.create({ title: 'approval' });
+ const [started] = await topicModel.update(topic.id, { status: 'running' });
+ await topicModel.update(topic.id, { status: 'waitingForHuman' });
+
+ const [resumed] = await topicModel.update(topic.id, { status: 'running' });
+
+ expect(started.metadata?.runStartedAt).toBeDefined();
+ expect(resumed.metadata?.runStartedAt).toBe(started.metadata?.runStartedAt);
+ });
+
+ it('restamps when a new run starts on a settled topic', async () => {
+ const topic = await topicModel.create({ title: 'second turn' });
+ const [first] = await topicModel.update(topic.id, { status: 'running' });
+ await topicModel.update(topic.id, { status: 'unread' });
+
+ const [second] = await topicModel.update(topic.id, { status: 'running' });
+
+ expect(second.metadata?.runStartedAt).not.toBe(first.metadata?.runStartedAt);
+ });
+
it('does not update a topic owned by another user', async () => {
await serverDB
.insert(topics)
diff --git a/packages/database/src/models/topic.ts b/packages/database/src/models/topic.ts
index d2a12c9668b..98d85429701 100644
--- a/packages/database/src/models/topic.ts
+++ b/packages/database/src/models/topic.ts
@@ -968,10 +968,19 @@ export class TopicModel {
.orderBy(desc(agentOperations.startedAt))
.limit(1);
+ // Client-executed runs (desktop heterogeneous CLI, in-browser runtime) never
+ // reach `agent_operations` β€” nothing server-side creates the operation β€” so
+ // their start is stamped onto the topic by the status write that claims it
+ // (see {@link TopicModel.update}). The operation row still wins when both
+ // exist: it is the server's own record of the run, while the stamp is a
+ // client-reported time.
+ const localRunStartedAt = sql`(${topics.metadata} ->> 'runStartedAt')::timestamptz`;
+
// CASE-gated so only rows that are actually running pay for the lookup β€”
- // and a stale running op under a finished topic can't resurrect a timer.
+ // and a stale running op (or stamp) under a finished topic can't resurrect
+ // a timer.
const runStartedAtColumn =
- sql<Date | null>`CASE WHEN ${topics.status} = 'running' THEN (${runStartedAtSubquery}) ELSE NULL END`
+ sql<Date | null>`CASE WHEN ${topics.status} = 'running' THEN COALESCE((${runStartedAtSubquery}), ${localRunStartedAt}) ELSE NULL END`
.mapWith(agentOperations.startedAt)
.as('run_started_at');
@@ -1728,10 +1737,32 @@ export class TopicModel {
? sql`${topics.provider} is distinct from ${data.provider}`
: undefined,
);
+ const persistedMetadata = modelChanged
+ ? sql`case when ${modelChanged} then coalesce(${topics.metadata}, '{}'::jsonb) - 'reasoningConfig' else coalesce(${topics.metadata}, '{}'::jsonb) end`
+ : sql`coalesce(${topics.metadata}, '{}'::jsonb)`;
+
+ /**
+ * A locally executed run β€” desktop heterogeneous CLI, in-browser runtime β€”
+ * has no `agent_operations` row: its runtime lives in the client, and the
+ * only thing it tells the server is this status write. Stamp when the run
+ * claimed the topic in the same statement, so a list can show a live
+ * elapsed clock for those runs the same way it does for server-side ones
+ * (see `runStartedAtColumn` in {@link TopicModel.queryTopics}).
+ *
+ * Compared against the PERSISTED status so a resume out of
+ * `waitingForHuman` β€” the same run, continuing after an approval β€” keeps
+ * its original start instead of restarting the clock. The stamp is left
+ * behind on terminal statuses: every reader gates on `status = 'running'`,
+ * and the next run overwrites it.
+ */
const metadata =
- data.metadata === undefined && modelChanged
- ? sql`case when ${modelChanged} then coalesce(${topics.metadata}, '{}'::jsonb) - 'reasoningConfig' else ${topics.metadata} end`
- : data.metadata;
+ data.metadata !== undefined
+ ? data.metadata
+ : data.status === 'running'
+ ? sql`case when ${topics.status} in ('running', 'waitingForHuman') then ${persistedMetadata} else jsonb_set(${persistedMetadata}, '{runStartedAt}', to_jsonb(now())) end`
+ : modelChanged
+ ? persistedMetadata
+ : undefined;
return this.db
.update(topics)
diff --git a/packages/types/src/topic/topic.ts b/packages/types/src/topic/topic.ts
index 6169345a9d2..2c2cad45f0c 100644
--- a/packages/types/src/topic/topic.ts
+++ b/packages/types/src/topic/topic.ts
@@ -303,6 +303,18 @@ export interface ChatTopicMetadata {
startedAt?: string;
threadId?: string | null;
} | null;
+ /**
+ * When the current run claimed this topic, as an ISO string. Stamped by the
+ * server whenever a status write moves the topic into `running` (see
+ * `TopicModel.update`), and read back only while the topic still is β€” a
+ * leftover stamp under a finished topic means nothing.
+ *
+ * Exists for runs the server doesn't execute: a desktop heterogeneous CLI or
+ * in-browser runtime writes no `agent_operations` row, so without this the
+ * topic list has no start time to run an elapsed clock from. Server-executed
+ * runs keep using their operation row, which is the more faithful record.
+ */
+ runStartedAt?: string;
/**
* A deferred agent run on this topic. Present iff the topic status is
* `scheduled`. Set to `null` to clear it (same clear-convention as