Skip to content

fix: capture channel delta messages#103

Open
Seekerzero wants to merge 1 commit into
plastic-labs:mainfrom
Seekerzero:fix-channel-delta-capture
Open

fix: capture channel delta messages#103
Seekerzero wants to merge 1 commit into
plastic-labs:mainfrom
Seekerzero:fix-channel-delta-capture

Conversation

@Seekerzero

@Seekerzero Seekerzero commented May 22, 2026

Copy link
Copy Markdown

Summary

  • handle wrapped OpenClaw message records when extracting roles, timestamps, sender metadata, and content
  • treat agent_end batches as already sliced when before_prompt_build recorded a full-transcript cursor
  • persist visible outbound channel replies from message tool calls while filtering channel delivery placeholders
  • classify private LAN and 100.64.0.0/10 self-host URLs as local Honcho deployments

Testing

  • ./node_modules/.bin/vitest --run
  • ./node_modules/.bin/tsc --noEmit

Summary by CodeRabbit

  • Bug Fixes

    • Improved message content extraction and metadata handling
    • Enhanced detection of self-hosted Honcho instances with broader private network support
    • Fixed delta batch message persistence handling
  • Chores

    • Added comprehensive test coverage for message extraction, batch handling, and self-hosted detection
    • Added support for additional Chinese noise pattern defaults

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 22, 2026

Copy link
Copy Markdown

Walkthrough

The PR updates message handling and Honcho session management across the plugin. It introduces helper functions to unwrap OpenClaw-wrapped messages and extract metadata, updates message capture to handle delta batches with refined index tracking, improves turn-boundary detection in the context hook, refactors session retrieval to remove metadata overrides, and expands local host detection for self-hosted Honcho setups.

Changes

Message Capture & Session Management

Layer / File(s) Summary
Message Unwrapping Helpers
config.ts, helpers.ts, test/helpers.test.ts
New getMessageRole(), getMessageTimestamp(), and unwrapMessage() centralize metadata extraction from raw and wrapped message objects. getRawContent() unwraps and extracts text from content or toolCall blocks. extractMessages() refactored to use these helpers. Chinese noise patterns "已回复。" and "已回复" added to default configuration.
Delta-Batch Message Capture
hooks/capture.ts, test/capture.test.ts
flushMessages now detects delta-batch scenarios when turnStartIndex exceeds message count, adjusts index clamping, and computes lastSavedIndex conditionally. User-message detection uses getMessageRole(). Tests verify delta-batch metadata updates and extraction of sender_id from wrapped message structures.
Turn-Start Index Resolution & Session Fetch
hooks/context.ts
New resolveTurnStartIndex() helper analyzes the last message to determine whether the turn started with a user message. Honcho session retrieval refactored to call state.honcho.session(sessionKey) without explicit agentId metadata.
Transcript Session Fetch Refactoring
runtime.ts
buildSessionTranscript updated to retrieve Honcho session without agentId metadata override, consistent with context hook changes.
Network Configuration
state.ts, test/state.test.ts
isLocalHonchoBaseUrl() expanded to recognize loopback hosts and RFC1918/CGNAT private IPv4 ranges for self-hosted detection. Tests verify local, private, and tailnet addresses classify as local, while public URLs do not.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

  • plastic-labs/openclaw-honcho#96: Both PRs modify hooks/capture.ts's flushMessages to refactor session/metadata management; this PR introduces delta-batch index tracking and metadata loading refactoring, while the other PR addresses session-key derivation and stored metadata fields.

Poem

🐰 Honcho hops through wrapped-up messages bright,
Unwraps the truth with helpers' delight,
Turn-starts and batches, delta and neat,
Session refactoring makes logic complete! 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: capture channel delta messages' directly addresses the main purpose of the PR: handling delta batch messages in the channel capture flow, which is evident from changes to hooks/capture.ts and the delta batch handling logic.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (1)
hooks/context.ts (1)

6-19: ⚡ Quick win

Reuse getMessageRole here to avoid wrapper-parsing drift.

This reimplements message unwrapping/role detection that now exists in helpers.ts. Using the shared helper keeps turn-boundary logic aligned with capture/extraction paths.

Refactor sketch
-import { buildSessionKey, extractSenderId, isSubagentSession } from "../helpers.js";
+import { buildSessionKey, extractSenderId, getMessageRole, isSubagentSession } from "../helpers.js";

 function resolveTurnStartIndex(messages: unknown[]): number {
   if (!Array.isArray(messages) || messages.length === 0) return 0;
   const lastIndex = messages.length - 1;
-  const rawLast = messages[lastIndex];
-  const last =
-    rawLast && typeof rawLast === "object" &&
-    "message" in rawLast && typeof (rawLast as { message?: unknown }).message === "object"
-      ? (rawLast as { message: unknown }).message
-      : rawLast;
-  if (last && typeof last === "object" && (last as { role?: unknown }).role === "user") {
-    return lastIndex;
-  }
-  return messages.length;
+  return getMessageRole(messages[lastIndex]) === "user" ? lastIndex : messages.length;
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@hooks/context.ts` around lines 6 - 19, resolveTurnStartIndex reimplements
message unwrapping and role detection; replace that logic by importing and using
the shared helper getMessageRole (from helpers.ts) instead of manually
unwrapping rawLast. Update resolveTurnStartIndex to call getMessageRole(rawLast)
and compare the returned role to "user" to decide the turn start, keep the same
return values (lastIndex or messages.length), and ensure proper typing/imports
so compilation/type-narrowing still works.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@helpers.ts`:
- Around line 385-387: The code currently treats any typeof timestamp ===
"number" as valid, which lets NaN/Infinity produce invalid Date objects; update
the guard around getMessageTimestamp(msg) so you only construct a Date when the
numeric timestamp is finite (e.g. use Number.isFinite(timestamp) or a similar
check) before creating ts and passing { createdAt: ts } into peer.message,
ensuring invalid numeric timestamps are rejected/handled instead of turned into
bad Date values.

In `@state.ts`:
- Around line 33-35: The octet parsing in the normalizedHost check currently
uses Number(part) which accepts non-decimal forms like "1e1" or "0x0a"; update
the logic around the octets variable so each part is first validated against a
decimal-digit regex (e.g., /^\d+$/) before numeric conversion, then parse with
parseInt(part, 10) and enforce the existing length/range checks (octets.length
=== 4 and each 0<=value<=255). Modify the mapping/validation that produces
octets in the function that uses normalizedHost to reject any non-digit octet
and only accept strictly decimal values.

In `@test/state.test.ts`:
- Around line 14-17: The test currently checks that public Honcho URLs are not
classified as self-hosted but misses non-decimal dotted host tokens; update the
test that uses isLocalHonchoBaseUrl to add two assertions ensuring that
"http://1e1.0.0.1" and "http://0x0a.0.0.1" both return false
(expect(...).toBe(false)), preventing numeric-coercion regressions in
isLocalHonchoBaseUrl.

---

Nitpick comments:
In `@hooks/context.ts`:
- Around line 6-19: resolveTurnStartIndex reimplements message unwrapping and
role detection; replace that logic by importing and using the shared helper
getMessageRole (from helpers.ts) instead of manually unwrapping rawLast. Update
resolveTurnStartIndex to call getMessageRole(rawLast) and compare the returned
role to "user" to decide the turn start, keep the same return values (lastIndex
or messages.length), and ensure proper typing/imports so
compilation/type-narrowing still works.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a191d3e5-2a07-41bb-8b80-0a48fc0f0295

📥 Commits

Reviewing files that changed from the base of the PR and between f35f9d4 and f9c4c2b.

📒 Files selected for processing (9)
  • config.ts
  • helpers.ts
  • hooks/capture.ts
  • hooks/context.ts
  • runtime.ts
  • state.ts
  • test/capture.test.ts
  • test/helpers.test.ts
  • test/state.test.ts

Comment thread helpers.ts
Comment on lines +385 to 387
const timestamp = getMessageTimestamp(msg);
const ts = typeof timestamp === "number" ? new Date(timestamp) : undefined;
result.push(peer.message(content, ts ? { createdAt: ts } : undefined));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Guard against invalid numeric timestamps before creating Date.

typeof timestamp === "number" also passes NaN/Infinity, which can produce invalid createdAt values downstream.

Suggested fix
-    const ts = typeof timestamp === "number" ? new Date(timestamp) : undefined;
+    const ts =
+      typeof timestamp === "number" && Number.isFinite(timestamp)
+        ? new Date(timestamp)
+        : undefined;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const timestamp = getMessageTimestamp(msg);
const ts = typeof timestamp === "number" ? new Date(timestamp) : undefined;
result.push(peer.message(content, ts ? { createdAt: ts } : undefined));
const timestamp = getMessageTimestamp(msg);
const ts =
typeof timestamp === "number" && Number.isFinite(timestamp)
? new Date(timestamp)
: undefined;
result.push(peer.message(content, ts ? { createdAt: ts } : undefined));
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@helpers.ts` around lines 385 - 387, The code currently treats any typeof
timestamp === "number" as valid, which lets NaN/Infinity produce invalid Date
objects; update the guard around getMessageTimestamp(msg) so you only construct
a Date when the numeric timestamp is finite (e.g. use Number.isFinite(timestamp)
or a similar check) before creating ts and passing { createdAt: ts } into
peer.message, ensuring invalid numeric timestamps are rejected/handled instead
of turned into bad Date values.

Comment thread state.ts
Comment on lines +33 to +35
const octets = normalizedHost.split(".").map((part) => Number(part));
if (octets.length !== 4 || octets.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) {
return false;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Require strict decimal IPv4 octets before numeric conversion.

On Line 33, using Number(part) allows non-decimal forms (e.g., 1e1, 0x0a) to pass validation, which can incorrectly classify non-IPv4 hostnames as local/private. Validate octets as digits first, then parse.

Proposed fix
-    const octets = normalizedHost.split(".").map((part) => Number(part));
-    if (octets.length !== 4 || octets.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) {
+    const parts = normalizedHost.split(".");
+    if (parts.length !== 4 || parts.some((part) => !/^\d{1,3}$/.test(part))) {
+      return false;
+    }
+    const octets = parts.map((part) => Number.parseInt(part, 10));
+    if (octets.some((part) => part < 0 || part > 255)) {
       return false;
     }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@state.ts` around lines 33 - 35, The octet parsing in the normalizedHost check
currently uses Number(part) which accepts non-decimal forms like "1e1" or
"0x0a"; update the logic around the octets variable so each part is first
validated against a decimal-digit regex (e.g., /^\d+$/) before numeric
conversion, then parse with parseInt(part, 10) and enforce the existing
length/range checks (octets.length === 4 and each 0<=value<=255). Modify the
mapping/validation that produces octets in the function that uses normalizedHost
to reject any non-digit octet and only accept strictly decimal values.

Comment thread test/state.test.ts
Comment on lines +14 to +17
it("does not classify public Honcho URLs as self-hosted", () => {
expect(isLocalHonchoBaseUrl("https://api.honcho.dev")).toBe(false);
expect(isLocalHonchoBaseUrl("https://8.8.8.8")).toBe(false);
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Add regression cases for non-decimal dotted host tokens.

Please add assertions that values like http://1e1.0.0.1 and http://0x0a.0.0.1 are false so local-host detection doesn’t regress through numeric coercion.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/state.test.ts` around lines 14 - 17, The test currently checks that
public Honcho URLs are not classified as self-hosted but misses non-decimal
dotted host tokens; update the test that uses isLocalHonchoBaseUrl to add two
assertions ensuring that "http://1e1.0.0.1" and "http://0x0a.0.0.1" both return
false (expect(...).toBe(false)), preventing numeric-coercion regressions in
isLocalHonchoBaseUrl.

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.

1 participant