Skip to content

Commit 87156ba

Browse files
committed
fix(typing): keep long active turns visible
1 parent 32361e7 commit 87156ba

5 files changed

Lines changed: 88 additions & 9 deletions

File tree

‎docs/channels/whatsapp.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,21 @@ Set `messages.statusReactions.enabled: true` to let WhatsApp replace the ack rea
493493

494494
Notes: `channels.whatsapp.ackReaction` still controls eligibility for direct messages and groups; the queued state uses the same effective emoji as plain ack reactions; WhatsApp has one bot reaction slot per message, so lifecycle updates replace the current reaction in place and restore the ack after the final done/error state.
495495

496+
## Active-turn typing
497+
498+
For admitted automatic turns where typing is allowed, WhatsApp sends a
499+
`composing` presence update when agent execution begins and refreshes it while
500+
the turn remains active. Refreshing stops when the run completes, including
501+
terminal failure or cancellation; the controller seals and cleans up after its
502+
reply dispatch settles. Turns for which the existing typing and suppression
503+
policy disables typing do not start this activity.
504+
505+
Typing presence is ephemeral, best-effort activity feedback. It is not a
506+
persisted message, delivery receipt, or guarantee that every WhatsApp client
507+
will display continuous activity; reconnects and client behavior can make the
508+
indicator disappear. Lifecycle status reactions remain the persistent-looking
509+
opt-in status surface described above.
510+
496511
## Multi-account and credentials
497512

498513
<AccordionGroup>

‎extensions/whatsapp/src/monitor-inbox.delivery-and-dedupe.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe("web monitor inbox delivery and dedupe", () => {
2424

2525
it("delivery coordinator streams inbound messages", async () => {
2626
const onMessage = vi.fn(async (msg) => {
27-
await msg.sendComposing();
27+
await msg.platform.sendComposing();
2828
await msg.reply("flat reply works");
2929
await msg.sendMedia({ text: "flat media works" });
3030
});

‎src/auto-reply/reply/reply-utils.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { createBlockReplyCoalescer } from "./block-reply-coalescer.js";
77
import { matchesMentionWithExplicit } from "./mentions.js";
88
import { normalizeReplyPayload } from "./normalize-reply.js";
99
import { parseReplyDirectives } from "./reply-directives.js";
10+
import { createReplyDispatcherWithTyping } from "./reply-dispatcher.js";
1011
import { createReplyReferencePlanner, isSingleUseReplyToMode } from "./reply-reference.js";
1112
import {
1213
extractShortModelName,
@@ -472,6 +473,41 @@ describe("typing controller", () => {
472473
expect(onReplyStart).toHaveBeenCalledTimes(1);
473474
});
474475

476+
it("keeps execution typing alive past its base TTL until the dispatcher settles", async () => {
477+
vi.useFakeTimers();
478+
const onReplyStart = vi.fn(async () => undefined);
479+
const onCleanup = vi.fn();
480+
const typing = createTypingController({
481+
onReplyStart,
482+
onCleanup,
483+
typingIntervalSeconds: 121,
484+
});
485+
const lifecycle = createReplyDispatcherWithTyping({
486+
deliver: async () => undefined,
487+
});
488+
lifecycle.replyOptions.onTypingController?.(typing);
489+
const signaler = createTypingSignaler({
490+
typing,
491+
mode: "message",
492+
isHeartbeat: false,
493+
});
494+
495+
await signaler.signalExecutionActivity?.();
496+
expect(onReplyStart).toHaveBeenCalledTimes(1);
497+
498+
await vi.advanceTimersByTimeAsync(243_000);
499+
expect(onReplyStart).toHaveBeenCalledTimes(3);
500+
expect(onCleanup).not.toHaveBeenCalled();
501+
502+
lifecycle.markRunComplete();
503+
lifecycle.dispatcher.markComplete();
504+
await lifecycle.dispatcher.waitForIdle();
505+
expect(onCleanup).toHaveBeenCalledTimes(1);
506+
507+
await vi.advanceTimersByTimeAsync(243_000);
508+
expect(onReplyStart).toHaveBeenCalledTimes(3);
509+
});
510+
475511
it("can send the first typing signal without periodic keepalive refreshes", async () => {
476512
vi.useFakeTimers();
477513
const onReplyStart = vi.fn();

‎src/auto-reply/reply/typing-persistence.test.ts‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,38 @@ describe("typing persistence bug fix", () => {
121121
expect(vi.getTimerCount()).toBe(0);
122122
});
123123

124-
it("clamps oversized typing interval and TTL timers", async () => {
124+
it("preserves the explicit zero TTL disable sentinel", async () => {
125+
const setTimeoutSpy = vi.spyOn(globalThis, "setTimeout");
126+
const unboundedController = createTypingController({
127+
onReplyStart: onReplyStartSpy,
128+
onCleanup: onCleanupSpy,
129+
typingIntervalSeconds: 121,
130+
typingTtlMs: 0,
131+
log: vi.fn(),
132+
});
133+
134+
await unboundedController.startTypingLoop();
135+
unboundedController.refreshTypingTtl();
136+
137+
expect(setTimeoutSpy).not.toHaveBeenCalled();
138+
unboundedController.cleanup();
139+
});
140+
141+
it("clamps an oversized typing interval and derives a longer TTL", async () => {
125142
const setIntervalSpy = vi.spyOn(globalThis, "setInterval");
126143
const setTimeoutSpy = vi.spyOn(globalThis, "setTimeout");
127144
const boundedController = createTypingController({
128145
onReplyStart: onReplyStartSpy,
129146
onCleanup: onCleanupSpy,
130147
typingIntervalSeconds: Number.MAX_SAFE_INTEGER,
131-
typingTtlMs: Number.MAX_SAFE_INTEGER,
132148
log: vi.fn(),
133149
});
134150

135151
await boundedController.startTypingLoop();
136152

137-
expect(setIntervalSpy).toHaveBeenCalledWith(expect.any(Function), MAX_TIMER_TIMEOUT_MS);
138-
expect(setTimeoutSpy).toHaveBeenCalledWith(expect.any(Function), MAX_TIMER_TIMEOUT_MS);
153+
const maxTypingIntervalMs = Math.floor(MAX_TIMER_TIMEOUT_MS / 2);
154+
expect(setIntervalSpy).toHaveBeenCalledWith(expect.any(Function), maxTypingIntervalMs);
155+
expect(setTimeoutSpy).toHaveBeenCalledWith(expect.any(Function), maxTypingIntervalMs * 2);
139156
boundedController.cleanup();
140157
});
141158
});

‎src/auto-reply/reply/typing.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/** Typing indicator lifecycle controller for reply runs. */
22
import {
33
finiteSecondsToTimerSafeMilliseconds,
4+
MAX_TIMER_TIMEOUT_MS,
45
resolveTimerTimeoutMs,
56
} from "@openclaw/normalization-core/number-coercion";
67
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
@@ -10,15 +11,25 @@ import { isSilentReplyPrefixText, isSilentReplyText, SILENT_REPLY_TOKEN } from "
1011

1112
const DEFAULT_TYPING_INTERVAL_SECONDS = 6;
1213
const DEFAULT_TYPING_TTL_MS = 2 * 60_000;
14+
const MAX_TYPING_INTERVAL_MS = Math.floor(MAX_TIMER_TIMEOUT_MS / 2);
1315

1416
function resolveTypingIntervalMs(seconds: number | undefined): number {
1517
if (Number.isFinite(seconds) && (seconds ?? 0) <= 0) {
1618
return 0;
1719
}
18-
return (
20+
const intervalMs =
1921
finiteSecondsToTimerSafeMilliseconds(seconds ?? DEFAULT_TYPING_INTERVAL_SECONDS) ??
20-
DEFAULT_TYPING_INTERVAL_SECONDS * 1000
21-
);
22+
DEFAULT_TYPING_INTERVAL_SECONDS * 1000;
23+
return Math.min(intervalMs, MAX_TYPING_INTERVAL_MS);
24+
}
25+
26+
function resolveTypingTtlMs(requestedTtlMs: number | undefined, intervalMs: number): number {
27+
const requested = resolveTimerTimeoutMs(requestedTtlMs, DEFAULT_TYPING_TTL_MS, 0);
28+
if (requested === 0) {
29+
return 0;
30+
}
31+
// Leave one full cadence for a keepalive call to settle before safety cleanup.
32+
return Math.max(requested, intervalMs * 2);
2233
}
2334

2435
/** Controller for channel typing indicator lifecycle during a reply run. */
@@ -73,7 +84,7 @@ export function createTypingController(params: {
7384
let sealed = false;
7485
let typingTtlTimer: NodeJS.Timeout | undefined;
7586
const typingIntervalMs = resolveTypingIntervalMs(params.typingIntervalSeconds);
76-
const typingTtlMs = resolveTimerTimeoutMs(params.typingTtlMs, DEFAULT_TYPING_TTL_MS, 0);
87+
const typingTtlMs = resolveTypingTtlMs(params.typingTtlMs, typingIntervalMs);
7788

7889
const formatTypingTtl = (ms: number) => {
7990
if (ms % 60_000 === 0) {

0 commit comments

Comments
 (0)