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(chat): mark only directly-steered messages as steer continuation (#21855)

Bryan4fe9e4227b73
diff --git a/apps/server/src/routers/lambda/aiAgent/execSchemas.ts b/apps/server/src/routers/lambda/aiAgent/execSchemas.ts
index 7c045d93c20..c8abfb3f61a 100644
--- a/apps/server/src/routers/lambda/aiAgent/execSchemas.ts
+++ b/apps/server/src/routers/lambda/aiAgent/execSchemas.ts
@@ -298,9 +298,10 @@ export const ExecAgentFieldsSchema = z.object({
* `'openapi'`, `'eval'`, …) to override.
*/
/**
- * The prompt was queued while the previous turn was still running. The
- * persisted user message carries `metadata.steer` so it renders as a
- * continuation of that turn instead of a new one.
+ * The send directly steered the turn it replaces (an interrupting "send
+ * now"). The persisted user message carries `metadata.steer` so it renders
+ * as a continuation of that turn instead of a new one. A follow-up that
+ * waited in queue and starts a turn of its own is not marked.
*/
steer: z.boolean().optional(),
trigger: z
diff --git a/apps/server/src/routers/lambda/shareChat/exec.ts b/apps/server/src/routers/lambda/shareChat/exec.ts
index 9998444d33b..be18e9dd563 100644
--- a/apps/server/src/routers/lambda/shareChat/exec.ts
+++ b/apps/server/src/routers/lambda/shareChat/exec.ts
@@ -103,7 +103,7 @@ export const execRouter = router({
resumeApprovals: z.array(ResumeApprovalDecisionSchema).min(1).optional(),
resumeToolResult: ResumeToolResultSchema.optional(),
shareId: z.string(),
- /** Queued behind a running turn; see `aiAgent.execAgent`'s `steer`. */
+ /** Directly steered the turn it replaces; see `aiAgent.execAgent`'s `steer`. */
steer: z.boolean().optional(),
/** Absent β†’ the run creates a new visitor topic (counted against the topic cap). */
topicId: z.string().nullish(),
diff --git a/apps/server/src/services/aiAgent/composerRun.ts b/apps/server/src/services/aiAgent/composerRun.ts
index bd44811061f..973a0596b1e 100644
--- a/apps/server/src/services/aiAgent/composerRun.ts
+++ b/apps/server/src/services/aiAgent/composerRun.ts
@@ -31,6 +31,7 @@ export type ComposerRunRequest = Pick<
| 'mentionedAgents'
| 'parentMessageId'
| 'prompt'
+ | 'queuedSuccessor'
| 'replacesOperationId'
| 'resumeApproval'
| 'resumeApprovals'
@@ -143,6 +144,7 @@ export const startComposerRun = async (ctx: ComposerRunContext, input: ComposerR
mentionedAgents: input.mentionedAgents,
parentMessageId,
prompt: input.prompt,
+ queuedSuccessor: input.queuedSuccessor,
replacesOperationId: input.replacesOperationId,
// When parentMessageId is provided, this is a regeneration/continue or a
// human-approval resume β€” either way, skip user message creation.
diff --git a/apps/server/src/services/aiAgent/pipeline/topicStart.test.ts b/apps/server/src/services/aiAgent/pipeline/topicStart.test.ts
index e9df880aaa6..e6893b22b6c 100644
--- a/apps/server/src/services/aiAgent/pipeline/topicStart.test.ts
+++ b/apps/server/src/services/aiAgent/pipeline/topicStart.test.ts
@@ -14,7 +14,7 @@ const onThread = (extra: Record<string, unknown> = {}) =>
...extra,
}) as any;
-const queuedSuccessor = onThread({ interactiveStart: false, steer: true });
+const queuedSuccessor = onThread({ interactiveStart: false, queuedSuccessor: true });
const composerSend = onThread({ interactiveStart: true });
const createDeps = (liveRuns: string[] = []) =>
diff --git a/apps/server/src/services/aiAgent/pipeline/topicStart.ts b/apps/server/src/services/aiAgent/pipeline/topicStart.ts
index dbbe1a5915b..02690456bda 100644
--- a/apps/server/src/services/aiAgent/pipeline/topicStart.ts
+++ b/apps/server/src/services/aiAgent/pipeline/topicStart.ts
@@ -167,11 +167,11 @@ const THREAD_START_WAIT_MS = 3000;
/**
* Start a run on an existing thread inside the thread's start fence.
*
- * A server-started queued follow-up (`steer`, not interactive) must hold the
- * fence and finds no other live run of the user's on the thread, as one step β€”
- * otherwise it fails and its follow-ups go back to the queue, instead of
- * starting next to a run the user just sent. A person at the composer only
- * waits briefly for the fence and is never refused.
+ * A server-started queued follow-up (`queuedSuccessor`, not interactive) must
+ * hold the fence and finds no other live run of the user's on the thread, as
+ * one step β€” otherwise it fails and its follow-ups go back to the queue,
+ * instead of starting next to a run the user just sent. A person at the
+ * composer only waits briefly for the fence and is never refused.
*/
const startOnThread = async (
deps: TopicStartDeps,
@@ -179,7 +179,7 @@ const startOnThread = async (
surface: { threadId: string; topicId: string },
run: (params: InternalExecAgentParams) => Promise<ExecAgentResult>,
): Promise<ExecAgentResult> => {
- const queuedSuccessor = !!params.steer && !params.interactiveStart;
+ const queuedSuccessor = !!params.queuedSuccessor && !params.interactiveStart;
const owner = `thread-start-${nanoid()}`;
const fence = await redisService.threadStartFence.acquire(
surface.threadId,
diff --git a/apps/server/src/services/aiAgent/pipeline/userTurnMetadata.ts b/apps/server/src/services/aiAgent/pipeline/userTurnMetadata.ts
index c4a90bffe79..1a7221f2ec8 100644
--- a/apps/server/src/services/aiAgent/pipeline/userTurnMetadata.ts
+++ b/apps/server/src/services/aiAgent/pipeline/userTurnMetadata.ts
@@ -49,8 +49,9 @@ export const buildUserTurnMetadata = ({
...(botSender ? { botSender } : undefined),
// A provider event that injected this turn keeps its source on the row.
...(externalOrigin ? { externalOrigin } : undefined),
- // A follow-up queued behind a running turn renders as that turn's
- // continuation; the client's optimistic row is replaced by this one.
+ // A message that steered the running turn directly renders as that turn's
+ // continuation; the client's optimistic row is replaced by this one. A
+ // follow-up that waited in queue starts its own turn and is not marked.
...(steer ? { steer: true as const } : undefined),
// Later turns read the selection back from history, so the skills stay
// injected for the rest of the topic (see `collectSelectedSkillIds`).
diff --git a/apps/server/src/services/aiAgent/steerQueue/index.test.ts b/apps/server/src/services/aiAgent/steerQueue/index.test.ts
index 052f045380b..50f959454dc 100644
--- a/apps/server/src/services/aiAgent/steerQueue/index.test.ts
+++ b/apps/server/src/services/aiAgent/steerQueue/index.test.ts
@@ -122,7 +122,7 @@ describe('startQueuedFollowUp', () => {
startComposerRun.mockResolvedValue({ operationId: 'op-2' });
});
- it('starts the drained follow-ups as one steered turn on the same surface', async () => {
+ it('starts the drained follow-ups as their own turn on the same surface', async () => {
drain.mockResolvedValue([followUp('a', 1, {}), followUp('b', 2, {})]);
await expect(
@@ -142,9 +142,12 @@ describe('startQueuedFollowUp', () => {
expect.objectContaining({
appContext: { threadId: 'thd-1', topicId: 'tpc-1' },
prompt: 'a\n\nb',
- steer: true,
+ queuedSuccessor: true,
}),
);
+ // A drained follow-up opens a turn of its own; only a steer of the run it
+ // waited behind would mark the user message.
+ expect(startComposerRun.mock.calls[0][1].steer).toBeUndefined();
// Delivered: the slots the drain reserved are free again.
expect(settle).toHaveBeenCalledWith(
expect.objectContaining({ topicId: 'tpc-1' }),
diff --git a/apps/server/src/services/aiAgent/steerQueue/index.ts b/apps/server/src/services/aiAgent/steerQueue/index.ts
index fd6a8a47ca4..a16bee54ee2 100644
--- a/apps/server/src/services/aiAgent/steerQueue/index.ts
+++ b/apps/server/src/services/aiAgent/steerQueue/index.ts
@@ -28,6 +28,7 @@ export type QueuedFollowUpRequest = Omit<
| 'existingMessageIds'
| 'includeFinalState'
| 'parentMessageId'
+ | 'queuedSuccessor'
| 'replacesOperationId'
| 'resumeApproval'
| 'resumeApprovals'
@@ -307,7 +308,9 @@ export const startQueuedFollowUp = async (
{
...request,
appContext: { ...request.appContext, threadId: scope.threadId, topicId: scope.topicId },
- steer: true,
+ // A drained follow-up starts its own turn β€” it did not steer the run
+ // it waited behind, so the user message stays unmarked.
+ queuedSuccessor: true,
},
);
log(
diff --git a/apps/server/src/services/aiAgent/types.ts b/apps/server/src/services/aiAgent/types.ts
index 14709ae9700..c2f8610f762 100644
--- a/apps/server/src/services/aiAgent/types.ts
+++ b/apps/server/src/services/aiAgent/types.ts
@@ -229,6 +229,13 @@ export interface InternalExecAgentParams extends ExecAgentParams {
onOperationCreated?: (operationId: string) => Promise<void>;
/** Parent message ID to continue from. Only takes effect when resume is true */
parentMessageId?: string;
+ /**
+ * A server-started turn that takes over where a finished run left off (the
+ * server-side drain of queued follow-ups). Non-interactive thread starts
+ * must hold the thread-start fence and find no other live run β€” see
+ * `startOnThread`. Unlike `steer`, it never marks the user message.
+ */
+ queuedSuccessor?: boolean;
queueRetries?: number;
queueRetryDelay?: string;
/** An idle operation reserved by the dispatcher for this exact Agent, topic and requester. */
@@ -308,9 +315,10 @@ export interface InternalExecAgentParams extends ExecAgentParams {
/** Abort startup before the agent runtime operation is created */
signal?: AbortSignal;
/**
- * The prompt was queued while the previous turn was still running. The user
- * message is persisted with `metadata.steer` so it renders as a continuation
- * of that turn.
+ * The send directly steered the turn it replaces: an interrupting "send
+ * now", not a follow-up that waited in queue and started as the next turn.
+ * The user message is persisted with `metadata.steer` so it renders as a
+ * continuation of the steered turn.
*/
steer?: boolean;
/**
diff --git a/apps/server/src/services/scm/ScmControlService.ts b/apps/server/src/services/scm/ScmControlService.ts
index fba5fcb1e25..b21506726ab 100644
--- a/apps/server/src/services/scm/ScmControlService.ts
+++ b/apps/server/src/services/scm/ScmControlService.ts
@@ -497,7 +497,6 @@ export class ScmControlService {
if (!topic?.agentId) return { detail: 'conversation has no agent', outcome: 'skipped' };
const prompt = await this.buildPrompt(row, reason, trigger);
- const running = Boolean(topic.metadata?.runningOperation);
// Take the slot before starting anything: the check above read a row
// snapshot, and on a Redis-less deployment there is no debounce to stop
@@ -509,8 +508,9 @@ export class ScmControlService {
}
// A busy topic: execAgent's reservation retries briefly for the running
- // turn to hand back (queued-steer semantics); if it does not, the wake is
- // dropped and the next event tries again.
+ // turn to hand back; if it does not, the wake is dropped and the next
+ // event tries again. The wake starts a turn of its own after the hand-back,
+ // so it is not a steer of the run it waited behind.
let operationId: string;
try {
const result = await new AiAgentService(this.db, row.userId, {
@@ -527,7 +527,6 @@ export class ScmControlService {
url: row.url,
},
prompt,
- steer: running,
trigger: RequestTrigger.Scm,
userInterventionConfig: { approvalMode: 'headless' },
});
diff --git a/apps/server/src/services/scm/__tests__/ScmControlService.test.ts b/apps/server/src/services/scm/__tests__/ScmControlService.test.ts
index 794ec6a4361..0a743dc00e1 100644
--- a/apps/server/src/services/scm/__tests__/ScmControlService.test.ts
+++ b/apps/server/src/services/scm/__tests__/ScmControlService.test.ts
@@ -259,7 +259,6 @@ describe('ScmControlService β€” wake', () => {
resourceId: row.id,
url: baseRow.url,
},
- steer: false,
trigger: 'scm',
userInterventionConfig: { approvalMode: 'headless' },
});
@@ -273,7 +272,7 @@ describe('ScmControlService β€” wake', () => {
expect(after?.metadata.lastWake?.reason).toBe('ci_failed');
});
- it('steers instead of starting a new turn when the conversation is running', async () => {
+ it('wakes a turn of its own β€” unmarked β€” when the conversation is running', async () => {
const topic = await createTopic();
await serverDB
.update(topics)
@@ -291,7 +290,9 @@ describe('ScmControlService β€” wake', () => {
kind: 'review_changes_requested',
row,
});
- expect(mocks.execAgent.mock.calls[0][0]).toMatchObject({ steer: true });
+ // The wake waits for the running turn to hand back, then starts a turn of
+ // its own β€” it did not steer that run, so its user message is not marked.
+ expect(mocks.execAgent.mock.calls[0][0].steer).toBeUndefined();
expect(mocks.execAgent.mock.calls[0][0].prompt).toContain('requested changes');
});
diff --git a/src/store/chat/slices/agentRun/actions/__tests__/conversationLifecycle.serverSteerQueue.test.ts b/src/store/chat/slices/agentRun/actions/__tests__/conversationLifecycle.serverSteerQueue.test.ts
index 35df2afa12c..6ea3e01d45b 100644
--- a/src/store/chat/slices/agentRun/actions/__tests__/conversationLifecycle.serverSteerQueue.test.ts
+++ b/src/store/chat/slices/agentRun/actions/__tests__/conversationLifecycle.serverSteerQueue.test.ts
@@ -164,8 +164,10 @@ describe('sendMessage behind a running Gateway run', () => {
vi.useRealTimers();
expect(sendQueued).toHaveBeenCalledWith(
- expect.objectContaining({ message: 'earlier\n\nnewer', metadata: { steer: true } }),
+ expect.objectContaining({ message: 'earlier\n\nnewer' }),
);
+ // The drained messages open a turn of their own, so they are not marked.
+ expect(sendQueued.mock.calls[0][0].metadata?.steer).toBeUndefined();
});
it('waits behind a follow-up a run is still delivering, instead of overtaking it', async () => {
diff --git a/src/store/chat/slices/agentRun/actions/entries/conversationLifecycle/index.ts b/src/store/chat/slices/agentRun/actions/entries/conversationLifecycle/index.ts
index 7a30e8abad1..a69589b09d5 100644
--- a/src/store/chat/slices/agentRun/actions/entries/conversationLifecycle/index.ts
+++ b/src/store/chat/slices/agentRun/actions/entries/conversationLifecycle/index.ts
@@ -1812,8 +1812,8 @@ export class ConversationLifecycleActionImpl {
messageContext: operationContext,
fileIds: fileIdList,
message,
- // The server persists the user row on this path, so a queued
- // follow-up's steer mark must travel with the request.
+ // The server persists the user row on this path, so the steer mark
+ // of a directly-steered send ("send now") must travel with the request.
metadata: (metadata as Pick<MessageMetadata, 'steer'> | undefined)?.steer
? { ...requestMetadata, steer: true }
: requestMetadata,
diff --git a/src/store/chat/slices/agentRun/actions/lifecycle/buildRunLifecycle.test.ts b/src/store/chat/slices/agentRun/actions/lifecycle/buildRunLifecycle.test.ts
index 9d7b5022e59..41637593cac 100644
--- a/src/store/chat/slices/agentRun/actions/lifecycle/buildRunLifecycle.test.ts
+++ b/src/store/chat/slices/agentRun/actions/lifecycle/buildRunLifecycle.test.ts
@@ -335,7 +335,7 @@ describe('buildRunLifecycle β€” sub-agent runs skip top-level effects', () => {
expect(store.completeOperation).toHaveBeenCalledWith(OP);
});
- it('keeps the queue in place until the steered send takes it', async () => {
+ it('keeps the queue in place until the drained send takes it', async () => {
vi.useFakeTimers();
try {
const { get, store } = makeStore();
@@ -365,7 +365,9 @@ describe('buildRunLifecycle β€” sub-agent runs skip top-level effects', () => {
expect(sendMessage).toHaveBeenCalledWith(
expect.objectContaining({
message: 'queued',
- metadata: expect.objectContaining({ scope: 'x', steer: true }),
+ // A drained follow-up opens a fresh turn: its metadata keeps what
+ // was queued, unmarked.
+ metadata: { scope: 'x' },
}),
);
} finally {
diff --git a/src/store/chat/slices/agentRun/actions/lifecycle/queuedFollowUp.ts b/src/store/chat/slices/agentRun/actions/lifecycle/queuedFollowUp.ts
index b4e9474e390..cc2c9a47c54 100644
--- a/src/store/chat/slices/agentRun/actions/lifecycle/queuedFollowUp.ts
+++ b/src/store/chat/slices/agentRun/actions/lifecycle/queuedFollowUp.ts
@@ -28,7 +28,11 @@ export const findOrphanedQueueKey = (
});
});
-/** Send drained queued messages as one steered turn. */
+/**
+ * Send drained queued messages as one turn of their own. The run they waited
+ * behind already ended, so this is a fresh turn β€” not a steer of it β€” and the
+ * user message stays unmarked.
+ */
export const sendQueuedMessages = (
get: () => ChatStore,
context: ConversationContext,
@@ -48,7 +52,7 @@ export const sendQueuedMessages = (
files: mergedFiles,
...(merged.forceRuntime ? { forceRuntime: merged.forceRuntime } : {}),
message: merged.content,
- metadata: { ...merged.metadata, steer: true },
+ metadata: merged.metadata,
});
};
diff --git a/src/store/chat/slices/agentRun/actions/transports/gateway/serverSteerQueue.test.ts b/src/store/chat/slices/agentRun/actions/transports/gateway/serverSteerQueue.test.ts
index e2cca9d1ddf..b4e7e0f7208 100644
--- a/src/store/chat/slices/agentRun/actions/transports/gateway/serverSteerQueue.test.ts
+++ b/src/store/chat/slices/agentRun/actions/transports/gateway/serverSteerQueue.test.ts
@@ -143,9 +143,9 @@ describe('handOffQueuedMessage', () => {
await handOffQueuedMessage(get, key, context, 'q1', async () => request);
await vi.advanceTimersByTimeAsync(200);
- expect(sendMessage).toHaveBeenCalledWith(
- expect.objectContaining({ message: 'q1', metadata: { steer: true } }),
- );
+ expect(sendMessage).toHaveBeenCalledWith(expect.objectContaining({ message: 'q1' }));
+ // The run it waited behind is over: the send is a fresh turn, not a steer.
+ expect(sendMessage.mock.calls[0][0].metadata?.steer).toBeUndefined();
});
it('retries an append whose outcome is unknown with the same id', async () => {
diff --git a/src/store/chat/slices/agentRun/actions/transports/hetero/heterogeneousAgentExecutor.ts b/src/store/chat/slices/agentRun/actions/transports/hetero/heterogeneousAgentExecutor.ts
index cc405b4a398..4bf5b73a8d0 100644
--- a/src/store/chat/slices/agentRun/actions/transports/hetero/heterogeneousAgentExecutor.ts
+++ b/src/store/chat/slices/agentRun/actions/transports/hetero/heterogeneousAgentExecutor.ts
@@ -2720,7 +2720,7 @@ export const executeHeterogeneousAgent = async (
files: mergedFiles,
...(merged.forceRuntime ? { forceRuntime: merged.forceRuntime } : {}),
message: merged.content,
- metadata: { ...merged.metadata, steer: true },
+ metadata: merged.metadata,
})
.catch((e: unknown) => {
console.error(