fix: dispatch timeout causing missed Telegram notifications (#153) (#154)

## Problem

`dispatchTask()` shells out to `openclaw gateway call sessions.patch` which
times out when the gateway is busy, causing:
1. Notifications never fire (they're at the end of dispatchTask)
2. Worker state may not be recorded
3. Workers run silently

## Solution (3 changes)

### 1. Make `ensureSession` fire-and-forget
Session key is deterministic, so we don't need to wait for confirmation.
Health check catches orphaned state later.

### 2. Use runtime API for notifications instead of CLI
Pass `runtime` through opts and use direct API calls:
- `runtime.channel.telegram.sendMessageTelegram()`
- `runtime.channel.whatsapp.sendMessageWhatsApp()`
- etc.

### 3. Move notification before session dispatch
Fire workerStart/workerComplete notifications early (after label transition)
before the session calls that can timeout.

## Files Changed

- lib/dispatch.ts — fire-and-forget ensureSession, early notification, accept runtime
- lib/notify.ts — use runtime API for direct channel sends
- lib/services/pipeline.ts — early notification, accept runtime
- lib/services/tick.ts — pass runtime through to dispatchTask
- lib/tool-helpers.ts — accept runtime in tickAndNotify
- lib/tools/work-start.ts — pass api.runtime to dispatchTask
- lib/tools/work-finish.ts — pass api.runtime to executeCompletion/tickAndNotify
This commit is contained in:
Lauren ten Hoor
2026-02-13 17:29:25 +08:00
committed by GitHub
parent d1106fc0bb
commit 24b35b3a3e
7 changed files with 140 additions and 106 deletions

View File

@@ -8,7 +8,7 @@
* - workerComplete: Worker completed task (→ project group)
*/
import { log as auditLog } from "./audit.js";
import { runCommand } from "./run-command.js";
import type { PluginRuntime } from "openclaw/plugin-sdk";
/** Per-event-type toggle. All default to true — set to false to suppress. */
export type NotificationConfig = Partial<Record<NotifyEvent["type"], boolean>>;
@@ -78,19 +78,46 @@ function buildMessage(event: NotifyEvent): string {
}
/**
* Send a notification message via the native OpenClaw messaging CLI.
* Send a notification message via the plugin runtime API.
*
* Uses `openclaw message send` which handles target resolution, chunking,
* retries, and error reporting for all supported channels.
* Fails silently (logs error but doesn't throw) to avoid breaking the main flow.
* Uses the runtime's native send functions to bypass CLI → WebSocket timeouts.
* Falls back gracefully on error (notifications shouldn't break the main flow).
*/
async function sendMessage(
target: string,
message: string,
channel: string,
workspaceDir: string,
runtime?: PluginRuntime,
): Promise<boolean> {
try {
// Use runtime API when available (avoids CLI subprocess timeouts)
if (runtime) {
if (channel === "telegram") {
await runtime.channel.telegram.sendMessageTelegram(target, message, { silent: true });
return true;
}
if (channel === "whatsapp") {
await runtime.channel.whatsapp.sendMessageWhatsApp(target, message, { verbose: false });
return true;
}
if (channel === "discord") {
await runtime.channel.discord.sendMessageDiscord(target, message);
return true;
}
if (channel === "slack") {
await runtime.channel.slack.sendMessageSlack(target, message);
return true;
}
if (channel === "signal") {
await runtime.channel.signal.sendMessageSignal(target, message);
return true;
}
}
// Fallback: use CLI (for unsupported channels or when runtime isn't available)
// Import lazily to avoid circular dependency issues
const { runCommand } = await import("./run-command.js");
await runCommand(
[
"openclaw",
@@ -132,6 +159,8 @@ export async function notify(
groupId?: string;
/** Channel type for routing (e.g. "telegram", "whatsapp", "discord", "slack") */
channel?: string;
/** Plugin runtime for direct API access (avoids CLI subprocess timeouts) */
runtime?: PluginRuntime;
},
): Promise<boolean> {
if (opts.config?.[event.type] === false) return true;
@@ -155,7 +184,7 @@ export async function notify(
message,
});
return sendMessage(target, message, channel, opts.workspaceDir);
return sendMessage(target, message, channel, opts.workspaceDir, opts.runtime);
}
/**