π€― 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import type { AgentState } from '@lobechat/agent-runtime';
import { isDesktop } from '@lobechat/const';
import type { ConversationContext, UIChatMessage } from '@lobechat/types';
import debug from 'debug';
import { t } from 'i18next';
import { LOADING_FLAT } from '@/const/message';
import { workService } from '@/services/work';
import type { AgentRuntimeType } from '@/store/chat/slices/agentRun/actions/dispatch/agentDispatcher';
import { emitClientAgentSignalSourceEvent } from '@/store/chat/slices/agentRun/actions/lifecycle/agentSignalBridge';
import { linkOwnBranchPullRequest } from '@/store/chat/slices/agentRun/actions/lifecycle/linkOwnBranchPullRequest';
import { snapshotTopicWorkingDirGit } from '@/store/chat/slices/agentRun/actions/lifecycle/snapshotWorkingDirGit';
import type { ChatStore } from '@/store/chat/store';
import {
notifyDesktopAgentCompleted,
notifyDesktopAgentFailed,
} from '@/store/chat/utils/desktopNotification';
import {
hasCompletedAssistantText,
isAudioOnlyFirstUserMessage,
} from '@/store/chat/utils/topicTitle';
import { toTitleText } from '@/utils/markdownToTxt';
import { messageMapKey } from '../../../../utils/messageMapKey';
import { displayMessageSelectors } from '../../../message/selectors/displayMessage';
import type { OperationStatus } from '../../../operation/types';
import { AI_RUNTIME_OPERATION_TYPES, isClientHeldQueuedMessage } from '../../../operation/types';
import { topicSelectors } from '../../../topic/selectors';
import { scheduleQueuedFollowUp } from './queuedFollowUp';
import type {
AgentRunLifecycle,
RunCompleteEvent,
RunCompleteResult,
RunParkedEvent,
RunResumedEvent,
RunScope,
UserMessagePersistedEvent,
} from './types';
const log = debug('lobe-store:run-lifecycle');
const MAX_NOTIFIED_SERVER_OPERATIONS = 200;
const notifiedServerOperationIds = new Set<string>();
/**
* Claim the single desktop notification of a server run. One server run can
* reach this renderer through several local operations: a hetero run's in-stream
* end arrives before heteroFinish clears the topic marker, so a reconnect can
* attach and see that run end again. Local-only runs have no server id to claim.
*/
const claimServerRunNotification = (serverOperationId: string | undefined): boolean => {
if (!serverOperationId) return true;
if (notifiedServerOperationIds.has(serverOperationId)) return false;
notifiedServerOperationIds.add(serverOperationId);
if (notifiedServerOperationIds.size > MAX_NOTIFIED_SERVER_OPERATIONS) {
const [oldest] = notifiedServerOperationIds;
notifiedServerOperationIds.delete(oldest);
}
return true;
};
/**
* Normalize the runtime/operation status into the cross-runtime
* `client.runtime.complete` signal status.
*
* Only TERMINAL states reach here: parked states (`waiting_for_human` /
* `waiting_for_async_tool`) are routed to `onRunParked` by the executor and
* never emit a completion signal β a run is not complete while it is parked.
*/
const normalizeClientRuntimeCompleteStatus = (
runtimeStatus: AgentState['status'] | undefined,
operationStatus?: OperationStatus,
): 'cancelled' | 'completed' | 'failed' | undefined => {
if (operationStatus === 'cancelled') return 'cancelled';
if (operationStatus === 'failed') return 'failed';
if (operationStatus === 'completed') return 'completed';
if (runtimeStatus === 'done') return 'completed';
if (runtimeStatus === 'error' || runtimeStatus === 'interrupted') return 'failed';
return undefined;
};
/** The effective terminal disposition a run ended on, transport-agnostic. */
type TerminalDisposition = 'cancelled' | 'failed' | 'success';
/**
* Resolve the terminal disposition from EITHER the client's raw `runtimeStatus`
* (`AgentState['status']`) OR the normalized cross-runtime `status` that gateway
* / hetero supply. This lets `completeRun` drive the same store/UI side effects
* regardless of which transport reached the terminal boundary.
*
* `cancelled` completes the operation for gateway/hetero (their cancel reaches
* this boundary with the op still `running`, so it must be moved to a terminal
* state here) but NOT for the client (its cancel path already moved the op out
* of band before reaching `completeRun`). `undefined` never completes β it means
* the transport reached an unrecognized status and falls through untouched.
*/
const resolveTerminalDisposition = (
event: Pick<RunCompleteEvent, 'runtimeStatus' | 'status'>,
): TerminalDisposition | undefined => {
const { runtimeStatus, status } = event;
// Client drives off the raw runtime status.
if (runtimeStatus === 'done') return 'success';
if (runtimeStatus === 'error') return 'failed';
if (runtimeStatus === 'interrupted') return 'cancelled';
// Gateway / hetero drive off the normalized terminal status.
if (status === 'completed') return 'success';
if (status === 'failed') return 'failed';
if (status === 'cancelled') return 'cancelled';
return undefined;
};
const findCompletionAssistantMessageId = (
messages: UIChatMessage[],
parentMessageId: string,
parentMessageType: 'user' | 'assistant' | 'tool',
) => {
const messagesById = new Map(messages.map((message) => [message.id, message]));
const parentMessage = messagesById.get(parentMessageId);
const isDescendantOfParent = (message: UIChatMessage) => {
let currentParentId = message.parentId;
const visited = new Set<string>();
while (currentParentId && !visited.has(currentParentId)) {
if (currentParentId === parentMessageId) return true;
visited.add(currentParentId);
currentParentId = messagesById.get(currentParentId)?.parentId;
}
return false;
};
return (
messages.findLast((message) => message.role === 'assistant' && isDescendantOfParent(message))
?.id ??
(parentMessageType === 'assistant' && parentMessage?.role === 'assistant'
? parentMessage.id
: undefined)
);
};
/**
* Per-run context resolved at the dispatch seam (or, transitionally, inside the
* executor). Carries everything the lifecycle hooks need beyond the per-call
* event so they can be assembled ONCE and called at runtime boundaries.
*/
export interface RunAdapterContext {
context: ConversationContext;
parentMessageId: string;
parentMessageType: 'user' | 'assistant' | 'tool';
runId: string;
runScope: RunScope;
runtimeType: AgentRuntimeType;
}
const NOOP = async () => {};
/**
* Assemble the store/UI run-lifecycle hooks for a single run.
*
* The hook bodies are the CLIENT completion effects (the fullest set today), to
* be reused as the shared implementation when gateway/hetero are wired in the
* follow-up entry convergence. `completeRun` handles only TERMINAL states;
* parked states route to `onRunParked` and fire no terminal side effects β a
* run is not complete while it is parked β parked states are non-terminal and
* must not emit terminal side effects (title, queue drain, notification, unread).
*/
export const buildRunLifecycle = (
get: () => ChatStore,
adapter: RunAdapterContext,
): AgentRunLifecycle => {
const { context, parentMessageId, parentMessageType } = adapter;
const { agentId, topicId, threadId, groupId, workspaceSlug } = context;
const messageKey = messageMapKey(context);
const contextKey = messageKey;
let voiceTopicTitleSummaryRequested = false;
const summarizeVoiceTopicTitleAfterCompletion = () => {
if (voiceTopicTitleSummaryRequested || adapter.runScope === 'sub_agent' || !topicId) return;
const topic = topicSelectors.getTopicById(topicId)(get());
const messages = displayMessageSelectors.getDisplayMessagesByKey(messageKey)(get());
const isUntitled =
!topic?.title ||
topic.title === LOADING_FLAT ||
topic.title === t('defaultTitle', { ns: 'topic' });
if (
topic &&
isUntitled &&
isAudioOnlyFirstUserMessage(messages) &&
hasCompletedAssistantText(messages)
) {
voiceTopicTitleSummaryRequested = true;
void get()
.summaryTopicTitle(topicId, messages)
.catch((error) => {
voiceTopicTitleSummaryRequested = false;
log('Failed to summarize voice topic title: %O', error);
});
}
};
const emitComplete = (operationId: string, runtimeStatus: AgentState['status'] | undefined) => {
// `client.runtime.complete` is a CLIENT-only source event (browser β server
// policy pipeline). Gateway / hetero emit their own `client.gateway.*` events
// at their transport boundaries, so the shared lifecycle must not emit it for
// them.
if (adapter.runtimeType !== 'client') return;
const finalMessages = get().messagesMap[messageKey] || [];
const assistantMessageId =
findCompletionAssistantMessageId(finalMessages, parentMessageId, parentMessageType) ??
findCompletionAssistantMessageId(
get().dbMessagesMap[messageKey] || [],
parentMessageId,
parentMessageType,
);
const operationStatus = get().operations[operationId]?.status;
void emitClientAgentSignalSourceEvent({
payload: {
agentId,
...(assistantMessageId ? { anchorMessageId: assistantMessageId } : {}),
assistantMessageId,
operationId,
status: normalizeClientRuntimeCompleteStatus(runtimeStatus, operationStatus),
threadId: threadId ?? undefined,
topicId: topicId ?? undefined,
...(parentMessageType === 'user' ? { triggerMessageId: parentMessageId } : {}),
},
sourceId: `${operationId}:client:complete`,
sourceType: 'client.runtime.complete',
});
};
return {
afterUserMessagePersisted: async (event: UserMessagePersistedEvent) => {
// Topic title auto-generation. Single home for all three runtimes β
// the client used to do this inline in sendMessage and gateway/hetero
// had no LLM-summarized title at all before the unified lifecycle.
// Top-level only β a nested sub-agent / `/compact` run must not retitle the
// user's topic. See RunScope.
if (adapter.runScope !== 'top_level') return;
const { isCreateNewTopic, topicId, assistantMessageId } = event;
if (!topicId) return;
// Snapshot the working directory's live branch + linked PR onto the topic.
// Anchored HERE (send) rather than in the ControlBar's mount effect so that
// merely opening a topic never re-stamps its historical branch/PR. Slow gh
// leg β fire-and-forget; it only patches topic metadata, idempotently.
void snapshotTopicWorkingDirGit(get, { agentId, topicId }).catch(() => {});
// Dev-only fast path: slice the first user message instead of calling the
// LLM. Only honored in non-production builds. Relocated verbatim.
const shouldSliceTopicTitle =
__DEV__ && process.env.NEXT_PUBLIC_DEV_DISABLE_AUTO_TOPIC === '1';
const applyTopicTitle = async (tid: string, messages: UIChatMessage[]) => {
if (!shouldSliceTopicTitle) {
await get().summaryTopicTitle(tid, messages);
return;
}
const firstUserText = messages.find((m) => m.role === 'user')?.content?.trim() ?? '';
const title = toTitleText(firstUserText).slice(0, 80) || 'New Topic';
// `internal_updateTopic` already balances its own loading owner. For a
// new client-runtime topic like "ι
θ―»δΈι’...", an extra `false` here would
// consume the runtime's loading owner and hide the sidebar spinner early.
await get().internal_updateTopic(tid, { title });
console.info('[dev] sliced topic title (NEXT_PUBLIC_DEV_DISABLE_AUTO_TOPIC=1):', title);
};
// Key off the EVENT's context, not the adapter's send-time `context`.
// `context` (= operationContext captured at dispatch) still carries a null
// topicId for a just-created topic, so its key points at the empty
// main-scope bucket; gateway/hetero persist the conversation under the real
// topicId (`event.context` β `{ ...operationContext, topicId }`). Reading
// the stale key summarizes nothing and emits a degenerate "η©Ίε―Ήθ―ζ ι’"
// title. `event.context` also carries groupId/threadId, so group topics
// keep reading their real messages (the #16289 fix).
const readStoreChats = () =>
displayMessageSelectors.getDisplayMessagesByKey(messageMapKey(event.context))(get());
// New topic β always title. Use caller-provided messages when present
// (client's freshly-created rows aren't in the store under topicId yet);
// otherwise read the persisted conversation from the store (gateway/hetero).
if (isCreateNewTopic) {
// The gateway path adds the new topic via a FIRE-AND-FORGET refreshTopic
// (gateway.ts), so it may not be in the store yet β and `summaryTopicTitle`
// bails on a missing topic. Load it first when absent (client / hetero
// already inserted it synchronously, so this is a no-op for them).
if (!topicSelectors.getTopicById(topicId)(get())) {
await get()
.refreshTopic()
.catch(() => {});
}
await applyTopicTitle(topicId, event.messages ?? readStoreChats());
return;
}
// Existing topic β title only when it still has none. Read from the store,
// excluding the just-created assistant placeholder.
const topic = topicSelectors.getTopicById(topicId)(get());
if (topic && !topic.title) {
const chats = readStoreChats().filter((item) => item.id !== assistantMessageId);
await applyTopicTitle(topicId, chats);
}
},
afterRunComplete: async (event: RunCompleteEvent) => {
if (get().operations[event.operationId]?.metadata.terminalReconciled) return;
if (adapter.runScope === 'sub_agent') return;
const serverOperationId = get().operations[event.operationId]?.metadata.serverOperationId;
const disposition = resolveTerminalDisposition(event);
if (disposition === 'failed' && event.notification) {
if (!claimServerRunNotification(serverOperationId)) return;
await notifyDesktopAgentFailed(get, {
context: { agentId, groupId, topicId, workspaceSlug },
error: event.notification.error,
});
return;
}
if (disposition !== 'success') return;
// A voice-only first message cannot be summarized at the post-persist seam:
// its content is empty and the assistant row is still a loading placeholder.
// Once the reply completes, use the now-textual conversation to generate a
// meaningful title. This also leaves the visible default title intact if the
// title request fails instead of turning the sidebar row blank.
summarizeVoiceTopicTitleAfterCompletion();
// Desktop notification + dock badge. Single home for all runtimes'
// completion notification β every transport funnels through the shared
// `notifyDesktopAgentCompleted` helper, so title (topic/agent name), body
// (the actual reply) and click-to-deep-link stay identical across them.
// Top-level-only: a nested sub-agent finishing is not a user-facing run
// completion β the parent run is still going, so it must not fire a
// "generation finished" notification / badge. See RunScope.
if (!isDesktop) return;
if (!claimServerRunNotification(serverOperationId)) return;
const notificationContext = { agentId, groupId, topicId, workspaceSlug };
if (adapter.runtimeType === 'client') {
// CLIENT: notify only OUTSIDE tool-calling mode; content comes from the
// in-memory store. No badge (preserves the prior client behavior).
//
// Anchor to the assistant message THIS run produced (walk from
// parentMessageId), NOT a positional findLast on the topic. On a later
// turn the fresh assistant can still be settling into messagesMap while
// the previous turn's assistant is the last populated one β a naive
// findLast then surfaces the PRIOR turn's reply as the notification body.
// Mirror emitComplete's dual-map (messagesMap β dbMessagesMap) lookup so
// the body is pinned to this run's freshest persisted content.
const finalMessages = get().messagesMap[messageKey] || [];
const dbMessages = get().dbMessagesMap[messageKey] || [];
const assistantId =
findCompletionAssistantMessageId(finalMessages, parentMessageId, parentMessageType) ??
findCompletionAssistantMessageId(dbMessages, parentMessageId, parentMessageType);
const lastAssistant = assistantId
? (finalMessages.find((m) => m.id === assistantId) ??
dbMessages.find((m) => m.id === assistantId))
: undefined;
if (!lastAssistant?.content || lastAssistant?.tools) return;
await notifyDesktopAgentCompleted(get, {
content: lastAssistant.content,
context: notificationContext,
});
return;
}
// GATEWAY / HETERO: the body content is executor-resolved (hetero's
// in-memory `accContent`) when supplied, else derived from the store's
// final assistant content (gateway, after its terminal DB reconciliation).
// Dock badge is set so a backgrounded app still signals completion.
const fallbackContent = (
get().messagesMap?.[messageKey] ||
get().dbMessagesMap?.[messageKey] ||
[]
).findLast((m) => m.role === 'assistant')?.content;
await notifyDesktopAgentCompleted(get, {
badge: true,
content: event.notification?.content || fallbackContent,
context: notificationContext,
});
},
beforeRunComplete: NOOP,
completeRun: async (event: RunCompleteEvent): Promise<RunCompleteResult> => {
const { operationId, runtimeStatus } = event;
if (get().operations[operationId]?.metadata.terminalReconciled) return { requeued: false };
// Effective terminal disposition, resolved from the client `runtimeStatus`
// OR the normalized `status` gateway/hetero pass β so the same side effects
// fire regardless of which transport reached this boundary.
const disposition = resolveTerminalDisposition(event);
// Title recovery is a successful-completion side effect, not a notification
// side effect. Run it before the queued-message early return so a follow-up
// cannot strand an audio-first topic without a title.
if (disposition === 'success') summarizeVoiceTopicTitleAfterCompletion();
// Same for the topic's linked PRs: a queued follow-up skips the
// completion notification, not these. The run's gh / push commands were
// linked by the completion Work scan before the run reported its end; only
// a mounted list (the PR chip / sidebar) refetches.
if (adapter.runScope !== 'sub_agent') {
void workService.refreshTopicPullRequests(topicId).catch(() => {});
// A PR someone opened on the topic's own branch β looked up before a
// queued turn can move the topic off that branch.
if (disposition === 'success' && topicId && agentId)
void linkOwnBranchPullRequest(get, { agentId, topicId });
}
const completeSuccess = () => {
get().completeOperation(operationId);
const completedOp = get().operations[operationId];
if (completedOp?.context.agentId) {
get().markTopicUnread({
agentId: completedOp.context.agentId,
groupId: completedOp.context.groupId,
topicId: completedOp.context.topicId,
});
}
};
// The client transport persists `status: 'running'` at run start
// (streamingExecutor) but, unlike gateway (see gateway.ts onSessionComplete),
// had no terminal write that flips it back for the topic the user is
// watching β `markTopicUnread` early-returns on the active topic, so the
// persisted status stayed `running` forever and stuck both the sidebar
// spinner and the home "δ»»ε‘ζ£ε¨ζ§θ‘" card. Mirror gateway's rule here: a
// clean completion the user isn't watching is owned by `markTopicUnread`
// (status: 'unread'); every OTHER case (viewing, error, abort) force-resets
// to 'active'. Client + top-level + real topic only β sub-agents never wrote
// 'running', and gateway/hetero own their own reset.
const resetActiveTopicRunningStatus = () => {
if (adapter.runtimeType !== 'client') return;
if (adapter.runScope === 'sub_agent') return;
if (!topicId) return;
const hasNewerRuntime = Object.values(get().operations).some(
(candidate) =>
candidate.id !== operationId &&
candidate.status === 'running' &&
AI_RUNTIME_OPERATION_TYPES.includes(candidate.type) &&
candidate.context.topicId === topicId &&
candidate.context.agentId === agentId &&
candidate.context.groupId === groupId &&
!candidate.parentOperationId,
);
if (hasNewerRuntime) return;
const viewing = get().activeTopicId === topicId;
// Not-viewing clean success is owned by `markTopicUnread` (β 'unread');
// skip so the two never race over the status field.
if (!viewing && disposition === 'success') return;
// Carry the group scope through, exactly like the start-write does. Without
// it `updateTopicStatus` auto-derives `group_agent` from agentId+groupId and
// the optimistic in-memory patch lands in the wrong bucket, leaving the
// VISIBLE group topic's sidebar spinner stuck until the next refetch.
void get().updateTopicStatus?.({
agentId,
groupId,
...(context.scope === 'group' || context.scope === 'group_agent'
? { scope: context.scope }
: {}),
status: 'active',
topicId,
});
};
// 1. afterCompletion callbacks β fire on ALL terminal states (tools that
// registered post-run actions: speak / broadcast / delegate).
const operation = get().operations[operationId];
const afterCompletionCallbacks = operation?.metadata?.runtimeHooks?.afterCompletionCallbacks;
if (afterCompletionCallbacks && afterCompletionCallbacks.length > 0) {
for (const callback of afterCompletionCallbacks) {
try {
await callback();
} catch (error) {
// Keep the original log prefix β characterization tests lock it (behavior-preserving).
console.error('[executeClientAgent] afterCompletion callback error:', error);
}
}
}
// A server snapshot may have settled the run while a callback awaited IO.
if (get().operations[operationId]?.metadata.terminalReconciled) return { requeued: false };
// 2. On success with queued messages: drain, complete, and re-trigger a new
// sendMessage. Only drain on success β on error the queue is preserved.
// Gated to TOP-LEVEL runs only: the input queue belongs to the parent
// run, so a nested sub-agent completion must never drain it (it would
// re-trigger the user's queued message mid-parent-run). See RunScope.
if (
disposition === 'success' &&
adapter.runScope !== 'sub_agent' &&
// The server delivers the follow-ups it holds itself.
get().queuedMessages?.[contextKey]?.some(isClientHeldQueuedMessage)
) {
completeSuccess();
emitComplete(operationId, runtimeStatus);
scheduleQueuedFollowUp(get, context, operationId, resetActiveTopicRunningStatus);
return { requeued: true };
}
// 3. Complete the operation based on the terminal disposition.
switch (disposition) {
case 'success': {
completeSuccess();
break;
}
case 'failed': {
get().failOperation(operationId, {
type: 'runtime_error',
message: 'Agent runtime execution failed',
});
break;
}
case 'cancelled': {
// Gateway / hetero reach this boundary with the op still `running`
// (their interrupt ends the run segment server- / CLI-side), so the op
// must be moved to terminal here. The client is exempt: its cancel
// path already set the op to `cancelled` out of band, and
// `completeOperation` deliberately preserves a `cancelled` status.
if (adapter.runtimeType !== 'client') {
get().completeOperation(operationId);
// A stop from elsewhere settles as `completed`; nothing drains the
// queue now, so the input must not wait for that hand-off.
get().updateOperationMetadata(operationId, { endedWithoutCompletion: true });
}
break;
}
// `undefined`: unrecognized terminal status β fall through untouched.
// Parked states never reach `completeRun` β the executor routes them to
// `onRunParked`.
}
// Runs past the requeue early-return, so a run that continues into a queued
// follow-up (which writes 'running' again) is never reset mid-flight.
resetActiveTopicRunningStatus();
emitComplete(operationId, runtimeStatus);
return { requeued: false };
},
onRunError: NOOP,
onRunParked: async ({ operationId, reason }: RunParkedEvent) => {
// Parked is NOT terminal: fire NO terminal side effects (title / queue
// drain / notification / markUnread) and emit NO `client.runtime.complete`
// β the run has not ended, it is waiting out-of-band.
//
// - `waiting_for_human`: complete this operation so the loading spinner
// clears for the approval UI; a NEW operation resumes the run when the
// user approves / rejects / submits / skips.
// - `waiting_for_async_tool`: keep the operation running until the async
// tool / sub-agent result resolves and drives the run forward.
if (reason === 'waiting_for_human') {
get().completeOperation(operationId);
}
},
onRunResumed: async ({ operationId, resumedOperationId, runId }: RunResumedEvent) => {
// A NEW operation resumes the SAME logical run after a park
// (`waiting_for_human` β approve / reject / reject-continue / submit / skip).
// This is the single broadcast seam for that transition: it fires NO terminal
// side effects (the run is continuing, not completing) and mutates NO store
// state β the resume operation is already started by its entry. Behavior-
// neutral today; the structural marker is what `[6]` AgentRunner builds on to
// thread a stable cross-operation `runId`. Top-level only, mirroring the other
// run-scoped hooks (a parked sub-agent is not a user-facing resume).
if (adapter.runScope === 'sub_agent') return;
log(
'run resumed (parkedOp=%s β resumeOp=%s, runId=%s)',
operationId,
resumedOperationId,
runId,
);
},
onRunStarted: NOOP,
onTerminalPersisted: NOOP,
};
};