Skip to content

fix: make agent_end auto-capture hook non-blocking (fire-and-forget)#278

Merged
rwmjhb merged 2 commits intoCortexReach:masterfrom
qingshuicn:fix/agent-end-hook-blocking-telegram
Mar 20, 2026
Merged

fix: make agent_end auto-capture hook non-blocking (fire-and-forget)#278
rwmjhb merged 2 commits intoCortexReach:masterfrom
qingshuicn:fix/agent-end-hook-blocking-telegram

Conversation

@qingshuicn
Copy link

Problem

The agent_end hook for auto-capture (index.ts:2191) is async and awaits multiple slow operations:

  • smartExtractor.filterNoiseByEmbedding() — embedding API call
  • smartExtractor.extractAndPersist() — LLM call (6-category extraction + dedup)
  • embedder.embedPassage() — embedding for regex fallback
  • store.vectorSearch() — vector similarity search for dedup

When OpenClaw's hook system awaits the returned promise, it holds the session lock for the entire duration of these API calls (potentially 30+ seconds with default LLM timeout). This causes downstream channel deliveries (particularly Telegram) to be silently dropped when subsequent messages hit session store lock timeouts.

Symptoms

  • First message in a Telegram session gets a response
  • All subsequent messages are received and processed (tools.profile loads, memory plugin runs) but sendMessage never fires
  • No errors logged anywhere — completely silent failure
  • Disabling autoCapture and smartExtraction immediately resolves the issue

Fix

Wrap the async capture work inside the agent_end handler in a void IIFE so the hook returns immediately. The capture work continues in the background without blocking the session.

// Before: blocks session until all API calls complete
api.on("agent_end", async (event, ctx) => {
  // ... slow embedding/LLM/vector calls ...
});

// After: returns immediately, work runs in background
api.on("agent_end", (event, ctx) => {
  void (async () => {
    // ... same work, but non-blocking ...
  })();
});

Change: 8 lines added, 1 line modified. The early-return guard (!event.success) stays synchronous for efficiency.

Testing

Verified on OpenClaw v2026.3.13 with Telegram channel (long-polling, streaming: "partial"):

  • Before fix: Bot responds to first message only; all follow-ups silently dropped
  • After disabling autoCapture/smartExtraction: All messages get responses (confirmed workaround)
  • Fix approach: Same behavioral result as the workaround, but with auto-capture still active

Related

liuqi added 2 commits March 20, 2026 01:21
The agent_end hook for auto-capture was async and awaited multiple slow
operations (embedding, LLM extraction, vector search). When OpenClaw's
hook system awaits the returned promise, it holds the session lock for
the entire duration of these API calls (potentially 30+ seconds).

This causes downstream channel deliveries (particularly Telegram) to be
silently dropped when subsequent messages hit session store lock timeouts.

The fix wraps the async work in a void IIFE so the hook handler returns
immediately. The capture work continues in the background without
blocking the session.

Symptoms before fix:
- First message in a session gets a response
- All subsequent messages are received and processed (tools.profile loads,
  memory plugin runs) but sendMessage never fires
- No errors logged anywhere — completely silent failure

Fixes CortexReach#260
The before_agent_start hook for auto-recall awaits the full retrieval
chain (embedding → rerank → lifecycle maintenance) without any timeout.
When the embedding or rerank API is slow or unresponsive, this holds the
session lock indefinitely, stalling agent startup and causing downstream
channel deliveries (e.g. Telegram) to silently drop messages.

The fix wraps the recall pipeline in a Promise.race with a 3-second
timeout. If the retrieval chain does not complete in time, the hook
returns undefined (skipping memory injection) and logs a warning. The
agent proceeds normally without injected memories rather than hanging.

This is the before_agent_start counterpart to the agent_end fix in the
previous commit; together they ensure neither hook can block the session.

Fixes CortexReach#253
@rwmjhb rwmjhb merged commit 5545338 into CortexReach:master Mar 20, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

autoCapture + smartExtraction agent_end hook causes Telegram messages to be silently dropped

2 participants