Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(channels): correct reply routing and ack lifecycle for rapid-fire messages

Naiyuan Qingf9d1e5c8096a
diff --git a/src/channels/inbound-debouncer.ts b/src/channels/inbound-debouncer.ts
index 0ff9213ffa..3a8ce0756b 100644
--- a/src/channels/inbound-debouncer.ts
+++ b/src/channels/inbound-debouncer.ts
@@ -10,12 +10,9 @@
* fire immediately regardless of timer
* 4. When timer fires, call the flush callback with all accumulated text
*
- * This prevents rapid-fire messages from triggering multiple separate Agent
- * runs. Instead, messages sent within a short window are concatenated with
- * newlines and dispatched as one combined prompt.
- *
- * Inspired by OpenClaw's createInboundDebouncer pattern.
- * @see docs/channel/openclaw-research.md — Section 7.3 message preprocessing
+ * Each flush produces an independent agent.write() call with its own reply
+ * target. Messages arriving while the agent is busy are NOT accumulated —
+ * they get their own debounce window and independent processing.
*/
interface PendingBatch {
@@ -24,7 +21,7 @@ interface PendingBatch {
/** Timestamp of the first message in this batch */
firstArrival: number;
/** Idle timer — fires when no new message arrives within delayMs */
- timer: ReturnType<typeof setTimeout>;
+ timer: ReturnType<typeof setTimeout> | null;
}
export class InboundDebouncer {
@@ -44,11 +41,12 @@ export class InboundDebouncer {
/** Add a message to the buffer. May trigger an immediate flush if maxWaitMs exceeded. */
push(conversationId: string, text: string): void {
const existing = this.pending.get(conversationId);
+ const preview = text.slice(0, 40) + (text.length > 40 ? "..." : "");
if (existing) {
- // Append to existing batch, reset idle timer
+ // Append to existing batch, clear any running timer
existing.texts.push(text);
- clearTimeout(existing.timer);
+ if (existing.timer !== null) clearTimeout(existing.timer);
// Check hard cap: if we've been buffering too long, flush now
const elapsed = Date.now() - existing.firstArrival;
@@ -58,9 +56,11 @@ export class InboundDebouncer {
}
// Reset idle timer
+ console.log(`[Debouncer] push (appending #${existing.texts.length}): "${preview}"`);
existing.timer = setTimeout(() => this.flush(conversationId), this.delayMs);
} else {
- // Start a new batch
+ // Start a new batch with idle timer
+ console.log(`[Debouncer] push (new batch, timer=${this.delayMs}ms): "${preview}"`);
const timer = setTimeout(() => this.flush(conversationId), this.delayMs);
this.pending.set(conversationId, {
texts: [text],
@@ -75,18 +75,19 @@ export class InboundDebouncer {
const batch = this.pending.get(conversationId);
if (!batch) return;
- clearTimeout(batch.timer);
+ if (batch.timer !== null) clearTimeout(batch.timer);
this.pending.delete(conversationId);
// Join multiple messages with newlines so the Agent sees them as one prompt
const combined = batch.texts.join("\n");
+ console.log(`[Debouncer] flush: ${batch.texts.length} message(s), ${combined.length} chars`);
this.flushFn(conversationId, combined);
}
/** Clean up all pending timers (call on shutdown) */
dispose(): void {
for (const batch of this.pending.values()) {
- clearTimeout(batch.timer);
+ if (batch.timer !== null) clearTimeout(batch.timer);
}
this.pending.clear();
}
diff --git a/src/channels/manager.ts b/src/channels/manager.ts
index 7b0cea7c35..5cbe7425ce 100644
--- a/src/channels/manager.ts
+++ b/src/channels/manager.ts
@@ -46,8 +46,19 @@ export class ChannelManager {
private readonly hub: Hub;
/** Running accounts keyed by "channelId:accountId" */
private readonly accounts = new Map<string, AccountHandle>();
- /** Where the last channel message came from (reply target) */
+ /** Where the last channel message came from (used for typing/reactions/errors) */
private lastRoute: LastRoute | null = null;
+ /**
+ * FIFO queue of route snapshots + their ack targets, captured at each debouncer flush.
+ * Each agent.write() gets its own entry; dequeued on agent_start.
+ */
+ private pendingRoutes: { route: LastRoute; acks: LastRoute[] }[] = [];
+ /** Route for the currently active agent run (set on agent_start, cleared on agent_end). */
+ private activeRoute: LastRoute | null = null;
+ /** All messages in the current run's batch that have 👀 (cleared on agent_end). */
+ private activeAcks: LastRoute[] = [];
+ /** Accumulates message routes for 👀 removal between debouncer flushes. */
+ private ackBuffer: LastRoute[] = [];
/** Unsubscribe function for the agent subscriber */
private agentUnsubscribe: (() => void) | null = null;
/** Session ID of the currently subscribed agent (for stale detection) */
@@ -188,13 +199,50 @@ export class ChannelManager {
this.subscribedAgentId = agent.sessionId;
this.agentUnsubscribe = agent.subscribe((event) => {
+ const maybeMessage = (event as { message?: { role?: string } }).message;
+ const role = maybeMessage?.role;
+
+ // Activate the next pending route + acks when a new agent run starts.
+ if (event.type === "agent_start") {
+ const entry = this.pendingRoutes.shift();
+ if (entry) {
+ this.activeRoute = entry.route;
+ this.activeAcks = entry.acks;
+ console.log(`[Channels] agent_start: activeRoute replyTo=${entry.route.deliveryCtx.replyToMessageId}, acks=${entry.acks.length}`);
+ }
+ }
+
+ // Agent run complete — remove 👀 from all batch messages, conditionally stop typing.
+ if (event.type === "agent_end") {
+ for (const ack of this.activeAcks) {
+ if (ack.plugin.outbound.removeReaction) {
+ console.log(`[Channels] agent_end: removing 👀 from replyTo=${ack.deliveryCtx.replyToMessageId}`);
+ void ack.plugin.outbound.removeReaction(ack.deliveryCtx).catch(() => {});
+ }
+ }
+ this.activeRoute = null;
+ this.activeAcks = [];
+ if (this.pendingRoutes.length === 0) {
+ console.log("[Channels] agent_end: no more pending, stopping typing");
+ this.stopTyping();
+ } else {
+ console.log(`[Channels] agent_end: ${this.pendingRoutes.length} pending run(s), keeping typing`);
+ }
+ }
+
// No active channel route — skip (reply goes to desktop/gateway only)
if (!this.lastRoute) return;
// Handle agent errors — notify the channel user
if (event.type === "agent_error") {
this.stopTyping();
- this.removeAckReaction();
+ for (const ack of this.activeAcks) {
+ if (ack.plugin.outbound.removeReaction) {
+ void ack.plugin.outbound.removeReaction(ack.deliveryCtx).catch(() => {});
+ }
+ }
+ this.activeRoute = null;
+ this.activeAcks = [];
const errorMsg = (event as { message?: string }).message ?? "Unknown error";
console.error(`[Channels] Agent error: ${errorMsg}`);
const route = this.lastRoute;
@@ -206,9 +254,6 @@ export class ChannelManager {
return;
}
- const maybeMessage = (event as { message?: { role?: string } }).message;
- const role = maybeMessage?.role;
-
// Only forward assistant message events
if (event.type === "message_start" || event.type === "message_update" || event.type === "message_end") {
if (role !== "assistant") return;
@@ -236,28 +281,31 @@ export class ChannelManager {
this.aggregator.handleEvent(event);
}
- // Clean up after response complete
+ // Finalize aggregator per assistant message (may fire multiple times in multi-turn runs).
+ // Typing and ack removal are handled at agent_end, not here.
if (event.type === "message_end" && role === "assistant") {
- this.stopTyping();
- this.removeAckReaction();
this.aggregator = null;
}
});
}
- /** Create a fresh aggregator wired to the current lastRoute */
+ /**
+ * Create a fresh aggregator wired to the activeRoute (snapshotted at flush time).
+ * Falls back to lastRoute for non-debounced paths (e.g. direct writes).
+ */
private createAggregator(): void {
- const route = this.lastRoute;
+ const route = this.activeRoute ?? this.lastRoute;
if (!route) return;
const { plugin, deliveryCtx } = route;
+ console.log(`[Channels] createAggregator: replyTo=${deliveryCtx.replyToMessageId} (source=${this.activeRoute ? "activeRoute" : "lastRoute"})`);
const chunkerConfig = plugin.chunkerConfig ?? DEFAULT_CHUNKER_CONFIG;
this.aggregator = new MessageAggregator(
chunkerConfig,
async (block) => {
try {
- console.log(`[Channels] Sending block ${block.index} (${block.text.length} chars${block.isFinal ? ", final" : ""}) → ${deliveryCtx.channel}:${deliveryCtx.conversationId}`);
+ console.log(`[Channels] Sending block ${block.index} (${block.text.length} chars${block.isFinal ? ", final" : ""}) → ${deliveryCtx.channel}:${deliveryCtx.conversationId} replyTo=${deliveryCtx.replyToMessageId}`);
if (block.index === 0) {
await plugin.outbound.replyText(deliveryCtx, block.text);
} else {
@@ -303,12 +351,17 @@ export class ChannelManager {
replyToMessageId: messageId,
},
};
- console.log(`[Channels] lastRoute updated → ${plugin.id}:${conversationId}`);
+ console.log(`[Channels] lastRoute updated → ${plugin.id}:${conversationId} replyTo=${messageId}`);
console.log(`[Channels] Forwarding to agent ${agent.sessionId}`);
- // Show typing indicator and ACK reaction while agent processes
+ // Show typing indicator and 👀 ack on this message
this.startTyping();
- this.addAckReaction();
+ const ackRoute: LastRoute = { ...this.lastRoute };
+ if (ackRoute.plugin.outbound.addReaction) {
+ console.log(`[Channels] Adding 👀 to replyTo=${messageId}`);
+ void ackRoute.plugin.outbound.addReaction(ackRoute.deliveryCtx, "👀").catch(() => {});
+ }
+ this.ackBuffer.push(ackRoute);
// Handle media messages (processed async, then fed through debouncer)
if (message.media && plugin.downloadMedia) {
@@ -406,7 +459,15 @@ export class ChannelManager {
if (!this.debouncer) {
this.debouncer = new InboundDebouncer(
(_conversationId, combinedText) => {
- console.log(`[Channels] Debouncer flushing ${combinedText.length} chars to agent`);
+ // Snapshot the current route + pending acks for this batch.
+ const route = this.lastRoute ? { ...this.lastRoute } : null;
+ const acks = [...this.ackBuffer];
+ this.ackBuffer = [];
+ if (route) {
+ this.pendingRoutes.push({ route, acks });
+ }
+ const replyTo = route?.deliveryCtx.replyToMessageId ?? "?";
+ console.log(`[Channels] Debouncer flushing ${combinedText.length} chars to agent (queued route replyTo=${replyTo}, acks=${acks.length})`);
agent.write(combinedText);
},
);
@@ -414,20 +475,6 @@ export class ChannelManager {
return this.debouncer;
}
- /** Add 👀 reaction to acknowledge message receipt */
- private addAckReaction(): void {
- const route = this.lastRoute;
- if (!route?.plugin.outbound.addReaction) return;
- void route.plugin.outbound.addReaction(route.deliveryCtx, "👀").catch(() => {});
- }
-
- /** Remove ACK reaction when processing completes */
- private removeAckReaction(): void {
- const route = this.lastRoute;
- if (!route?.plugin.outbound.removeReaction) return;
- void route.plugin.outbound.removeReaction(route.deliveryCtx).catch(() => {});
- }
-
/** Start sending typing indicators (repeats every 5s until stopped) */
private startTyping(): void {
this.stopTyping();
@@ -462,6 +509,10 @@ export class ChannelManager {
if (this.lastRoute && this.lastRoute.plugin.id === channelId && this.lastRoute.deliveryCtx.accountId === accountId) {
this.stopTyping();
this.lastRoute = null;
+ this.activeRoute = null;
+ this.activeAcks = [];
+ this.ackBuffer = [];
+ this.pendingRoutes = [];
this.aggregator = null;
}
@@ -495,6 +546,10 @@ export class ChannelManager {
}
this.accounts.clear();
this.lastRoute = null;
+ this.activeRoute = null;
+ this.activeAcks = [];
+ this.ackBuffer = [];
+ this.pendingRoutes = [];
this.aggregator = null;
}