feat(hooks): add context_assembled, loop_iteration_start/end observability hooks#16
Closed
zeroaltitude wants to merge 21 commits intomainfrom
Closed
feat(hooks): add context_assembled, loop_iteration_start/end observability hooks#16zeroaltitude wants to merge 21 commits intomainfrom
zeroaltitude wants to merge 21 commits intomainfrom
Conversation
18 tasks
bcd7f12 to
c72adc4
Compare
…ility hooks Three new void/parallel plugin hooks for agent loop observability: - context_assembled: fires when the full context (system prompt + messages) is assembled before the first LLM call. Gives plugins a census of what the model will see. - loop_iteration_start: fires at the start of each agent loop iteration. Enables recursion depth tracking. - loop_iteration_end: fires at the end of each iteration with tool call count and whether the loop will continue. All three are fire-and-forget (void return, parallel execution), making them zero-impact on the critical path. No existing hooks cover these observation points — context_assembled and loop iteration tracking have zero overlap with current hook surface. Wired into the embedded runner via event subscription for iteration hooks and inline call for context_assembled. 9 unit tests covering event shape, parallel execution, and no-op when no handlers are registered. [claude, human developer oversight]
…e as approximate - context_assembled now includes effectivePrompt so plugins see the full model input (system prompt + user prompt + messages) - willContinue documented as heuristic (doesn't account for abort/timeout) Addresses greptile + codex-connector review on PR openclaw#33915.
The previous name implied the loop would continue, but the field only reflects whether tool results were present — it cannot account for abort signals, timeouts, or max-iteration limits evaluated downstream. Rename to hasToolResults for accuracy. Strengthened JSDoc to direct plugin authors to llm_output for definitive terminal detection. Addresses greptile review on PR openclaw#33915.
…led single-fire - Move hookCtx declaration before the subscription callback that references it, eliminating the forward-reference fragility flagged by greptile (was safe due to async events, now safe unconditionally) - Document that context_assembled fires once per run (iteration: 1) and direct plugin authors to loop_iteration_start for per-turn tracking - Updated type-level JSDoc on PluginHookContextAssembledEvent Addresses remaining greptile 4/5 → 5/5 concerns on PR openclaw#33915.
…ed iteration - Populate newMessagesAdded in loop_iteration_end (tracks messages added per turn) - Add JSDoc to context_assembled.iteration clarifying it's always 1 - Addresses Greptile review feedback
- Add imageCount to context_assembled event (Codex P2) - Snapshot messages array to avoid mutation during async handlers (Codex P2) - Implement pendingToolResults in loop_iteration_start (Greptile suggestion) Tracks tool results from previous turn_end and exposes at next turn_start
…empt context_assembled fired inside runEmbeddedAttempt with iteration: 1, but the outer runner can re-enter runEmbeddedAttempt for the same user-visible run (overflow compaction retry, auth retry). Plugins doing audit/census would double-count runs and see multiple context_assembled events all claiming to be the first. Fix: pass attemptIndex from the outer run loop and guard emission to only fire on attemptIndex === 0. Retries skip the hook entirely. Addresses review feedback from bmendonca3.
context_assembled should fire on every attempt, not just the first. The outer run loop may retry after overflow compaction, auth refresh, or tool result truncation — each producing different context (fewer messages post-compaction, different auth profile, etc.). Plugins need to see the actual context that will feed the LLM on each attempt. Changes: - Remove attemptIndex === 0 guard (hook fires every attempt) - Add attemptIndex to PluginHookContextAssembledEvent type - Pass attemptIndex through to the event - Update docs: 'once per attempt' not 'once per run' - Plugins needing run-level dedup should key on runId Reviewed openclaw-provenance plugin: safe under multiple firings. store.startTurn() seals the previous attempt's graph (correct for interrupted-by-compaction). All other operations are idempotent.
…HOOK_NAMES These hook names were in the PluginHookName union but missing from PLUGIN_HOOK_NAMES, which powers isPluginHookName and registerTypedHook. Without this, plugins could not register these hooks — registerTypedHook treats missing names as unknown and silently ignores them. The compile-time assertion (MissingPluginHookNames extends never) was already failing, confirming the gap.
…semantics The type comment and hook runner JSDoc still said 'once per run' and 'before the first LLM call'. Updated to reflect the actual behavior: fires once per attempt, may fire multiple times per run on retries.
The docs referenced runId for run-level deduplication but plugins couldn't access it — it wasn't in the event or agent context. Add runId to PluginHookContextAssembledEvent and pass it through.
- Remove misleading iteration field from context_assembled (always 1, confusing) - Make attemptIndex required through full call chain (types.ts → params → emission) - Add shallow-copy safety JSDoc on messages array - Enhance error logging with runId/iteration/attempt context - Improve context_assembled emission comment for scannability
- Make runId required in PluginHookContextAssembledEvent (always provided, JSDoc advises plugins to key on it — optional was misleading) - Fix runLoopIterationEnd JSDoc: 'includes whether loop will continue' → 'includes hasToolResults heuristic hint' (not a definitive signal) - Move module docstring above all imports in test file - (pendingToolResults/newMessagesAdded already populated — stale comment)
…ription Counter increments (hookTurnIteration, hookTurnMessageCount) remain unconditional since loop_iteration_end depends on state set during turn_start. Only the hookRunner calls are guarded, consistent with the hasHooks opt-in philosophy used elsewhere.
…case Mid-turn compaction can reduce messages.length below the turn_start snapshot, yielding a negative delta. Clamp to 0 since negative message counts are nonsensical for plugin consumers.
…rovided Both fields are always populated by the embedded runner but typed optional to permit future/custom call sites to omit them.
c72adc4 to
eb606d2
Compare
zeroaltitude
pushed a commit
that referenced
this pull request
Apr 2, 2026
* feat: add QQ Bot channel extension * fix(qqbot): add setupWizard to runtime plugin for onboard re-entry * fix: fix review * fix: fix review * chore: sync lockfile and config-docs baseline for qqbot extension * refactor: 移除图床服务器相关代码 * fix * docs: 新增 QQ Bot 插件文档并修正链接路径 * refactor: remove credential backup functionality and update setup logic - Deleted the credential backup module to streamline the codebase. - Updated the setup surface to handle client secrets more robustly, allowing for configured secret inputs. - Simplified slash commands by removing unused hot upgrade compatibility checks and related functions. - Adjusted types to use SecretInput for client secrets in QQBot configuration. - Modified bundled plugin metadata to allow additional properties in the config schema. * feat: 添加本地媒体路径解析功能,修正 QQBot 媒体路径处理 * feat: 添加本地媒体路径解析功能,修正 QQBot 媒体路径处理 * feat: remove qqbot-media and qqbot-remind skills, add tests for config and setup - Deleted the qqbot-media and qqbot-remind skills documentation files. - Added unit tests for qqbot configuration and setup processes, ensuring proper handling of SecretRef-backed credentials and account configurations. - Implemented tests for local media path remapping, verifying correct resolution of media file paths. - Removed obsolete channel and remind tools, streamlining the codebase. * feat: 更新 QQBot 配置模式,添加音频格式和账户定义 * feat: 添加 QQBot 频道管理和定时提醒技能,更新媒体路径解析功能 * fix * feat: 添加 /bot-upgrade 指令以查看 QQBot 插件升级指引 * feat: update reminder and qq channel skills * feat: 更新remind工具投递目标地址格式 * feat: Refactor QQBot payload handling and improve code documentation - Simplified and clarified the structure of payload interfaces for Cron reminders and media messages. - Enhanced the parsing function to provide clearer error messages and improved validation. - Updated platform utility functions for better cross-platform compatibility and clearer documentation. - Improved text parsing utilities for better readability and consistency in emoji representation. - Optimized upload cache management with clearer comments and reduced redundancy. - Integrated QQBot plugin into the bundled channel plugins and updated metadata for installation. * OK apps/macos/Sources/OpenClaw/HostEnvSecurityPolicy.generated.swift > openclaw@2026.3.26 check:bundled-channel-config-metadata /Users/yuehuali/code/PR/openclaw > node --import tsx scripts/generate-bundled-channel-config-metadata.ts --check [bundled-channel-config-metadata] stale generated output at src/config/bundled-channel-config-metadata.generated.ts ELIFECYCLE Command failed with exit code 1. ELIFECYCLE Command failed with exit code 1. * feat: 添加 QQBot 渠道配置及相关账户设置 * fix(qqbot): resolve 14 high-priority bugs from PR openclaw#52986 review DM routing (7 fixes): - #1: DM slash-command replies use sendDmMessage(guildId) instead of sendC2CMessage(senderId) - #2: DM qualifiedTarget uses qqbot:dm:${guildId} instead of qqbot:c2c:${senderId} - #3: sendTextChunks adds DM branch - #4: sendMarkdownReply adds DM branch for text and Base64 images - #5: parseAndSendMediaTags maps DM to targetType:dm + guildId - #6: sendTextToTarget DM branch uses sendDmMessage; MessageTarget adds guildId field - #7: handleImage/Audio/Video/FilePayload add DM branches Other high-priority fixes: - #8: Fix sendC2CVoiceMessage/sendGroupVoiceMessage parameter misalignment - #9: broadcastMessage uses groupOpenid instead of member_openid for group users - #10: Unify KnownUser storage - proactive.ts delegates to known-users.ts - #11: Remove invalid recordKnownUser calls for guild/DM users - #12: sendGroupMessage uses sendAndNotify to trigger onMessageSent hook - #13: sendPhoto channel unsupported returns error field - #14: sendTextAfterMedia adds channel and dm branches Type fixes: - DeliverEventContext adds guildId field - MediaTargetContext.targetType adds dm variant - sendPlainTextReply imgMediaTarget adds DM branch * fix(qqbot): resolve 2 blockers + 7 medium-priority bugs from PR openclaw#52986 review Blocker-1: Remove unused dmPolicy config knob - dmPolicy was declared in schema/types/plugin.json but never consumed at runtime - Removed from config-schema.ts, types.ts, and openclaw.plugin.json - allowFrom remains active (already wired into framework command-auth) Blocker-2: Gate sensitive slash commands with allowFrom authorization - SlashCommand interface adds requireAuth?: boolean - SlashCommandContext adds commandAuthorized: boolean - /bot-logs set to requireAuth: true (reads local log files) - matchSlashCommand rejects unauthorized senders for requireAuth commands - trySlashCommandOrEnqueue computes commandAuthorized from allowFrom config Medium-priority fixes: - #15: Strip non-HTTP/non-local markdown image tags to prevent path leakage - #16: applyQQBotAccountConfig clears clientSecret when setting clientSecretFile and vice versa - #17: getAdminMarkerFile sanitizes accountId to prevent path traversal - #18: URGENT_COMMANDS uses exact match instead of startsWith prefix match - #19: isCronExpression validates each token starts with a cron-valid character - openclaw#20: --token format validation rejects malformed input without colon separator - openclaw#21: resolveDefaultQQBotAccountId checks QQBOT_APP_ID environment variable * test(qqbot): add focused tests for slash command authorization path - Unauthorized sender rejected for /bot-logs (requireAuth: true) - Authorized sender allowed for /bot-logs - Non-requireAuth commands (/bot-ping, /bot-help, /bot-version) work for all senders - Unknown slash commands return null (passthrough) - Non-slash messages return null - Usage query (/bot-logs ?) also gated by auth check * fix(qqbot): align global TTS fallback with framework config resolution - Extract isGlobalTTSAvailable to utils/audio-convert.ts, mirroring core resolveTtsConfig logic: check auto !== 'off', fall back to legacy enabled boolean, default to off when neither is set. - Add pre-check in reply-dispatcher before calling globalTextToSpeech to avoid unnecessary TTS calls and noisy error logs when TTS is not configured. - Remove inline as any casts; use OpenClawConfig type throughout. - Refactor handleAudioPayload into flat early-return structure with unified send path (plugin TTS → global fallback → send). * fix(qqbot): break ESM circular dependency causing multi-account startup crash The bundled gateway chunk had a circular static import on the channel chunk (gateway -> outbound-deliver -> channel, while channel dynamically imports gateway). When two accounts start concurrently via Promise.all, the first dynamic import triggers module graph evaluation; the circular reference causes api exports (including runDiagnostics) to resolve as undefined before the module finishes evaluating. Fix: extract chunkText and TEXT_CHUNK_LIMIT from channel.ts into a new text-utils.ts leaf module. outbound-deliver.ts now imports from text-utils.ts, breaking the cycle. channel.ts re-exports for backward compatibility. * fix(qqbot): serialize gateway module import to prevent multi-account startup race When multiple accounts start concurrently via Promise.all, each calls await import('./gateway.js') independently. Due to ESM circular dependencies in the bundled output, the first import can resolve transitive exports as undefined before module evaluation completes. Fix: cache the dynamic import promise in a module-level variable so all concurrent startAccount calls share the same import, ensuring the gateway module is fully evaluated before any account uses it. * refactor(qqbot): remove startup greeting logic Remove getStartupGreetingPlan and related startup greeting delivery: - Delete startup-greeting.ts (greeting plan, marker persistence) - Delete admin-resolver.ts (admin resolution, greeting dispatch) - Remove startup greeting calls from gateway READY/RESUMED handlers - Remove isFirstReadyGlobal flag and adminCtx * fix(qqbot): skip octal escape decoding for Windows local paths Windows paths like C:\Users\1\file.txt contain backslash-digit sequences that were incorrectly matched as octal escape sequences and decoded, corrupting the file path. Detect Windows local paths (drive letter or UNC prefix) and skip the octal decoding step for them. * fix bot issue * feat: 支持 TTS 自动开关并清理配置中的 clientSecretFile * docs: 添加 QQBot 配置和消息处理的设计说明 * rebase * fix(qqbot): align slash-command auth with shared command-auth model Route requireAuth:true slash commands (e.g. /bot-logs) through the framework's api.registerCommand() so resolveCommandAuthorization() applies commands.allowFrom.qqbot precedence and qqbot: prefix normalization before any handler runs. - slash-commands.ts: registerCommand() now auto-routes by requireAuth into two maps (commands / frameworkCommands); getFrameworkCommands() exports the auth-required set for framework registration; bot-help lists both maps - index.ts: registerFull() iterates getFrameworkCommands() and calls api.registerCommand() for each; handler derives msgType from ctx.from, sends file attachments via sendDocument, supports multi-account via ctx.accountId - gateway.ts (inbound): replace raw allowFrom string comparison with qqbotPlugin.config.formatAllowFrom() to strip qqbot: prefix and uppercase before matching event.senderId - gateway.ts (pre-dispatch): remove stale auth computation; commandAuthorized is true (requireAuth:true commands never reach matchSlashCommand) - command-auth.test.ts: add regression tests for qqbot: prefix normalization in the inbound commandAuthorized computation - slash-commands.test.ts: update /bot-logs tests to expect null (command routed to framework, not in local registry) * rebase and solve conflict * fix(qqbot): preserve mixed env setup credentials --------- Co-authored-by: yuehuali <yuehuali@tencent.com> Co-authored-by: walli <walli@tencent.com> Co-authored-by: WideLee <limkuan24@gmail.com> Co-authored-by: Frank Yang <frank.ekn@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Upstream: openclaw#33915
Part 2/3 of hooks split. See upstream PR for full description.
[claude, human developer oversight]