Skip to content

Fix: move bootstrap session grammar into plugin-owned session-key surfaces - #58400

Merged
gumadeiras merged 10 commits into
mainfrom
codex/plugin-owned-session-routing
Mar 31, 2026
Merged

gumadeiras merged 10 commits into
mainfrom
codex/plugin-owned-session-routing

Conversation

@gumadeiras

@gumadeiras gumadeiras commented Mar 31, 2026 •

Copy link
Copy Markdown
Member

Summary

This PR moves provider-specific session conversation grammar out of core and into plugin-owned session-key surfaces.

Core now owns:

  • the outer session-key envelope
  • generic :thread: parsing
  • dispatch across runtime plugin hooks and bundled bootstrap surfaces

Channel plugins now own:

  • provider-specific conversation parsing
  • thread/topic extraction when encoded inside conversation ids
  • parent conversation inheritance for scoped conversations
  • explicit base conversation identity for policy/model fallback routing

End State

  • src/channels/plugins/session-conversation.ts is the shared resolver for persisted session keys.
  • Runtime paths use messaging.resolveSessionConversation(...) from the active channel plugin.
  • Pre-bootstrap paths use bundled plugin session-key-api.ts surfaces when the runtime registry is not available.
  • Generic :thread: remains core-owned.
  • Telegram topic parsing is plugin-owned and still works before registry bootstrap.
  • Feishu topic/sender inheritance is plugin-owned and still works before registry bootstrap.
  • Tool policy inheritance uses explicit baseConversationId instead of array-tail assumptions.
  • Model override matching preserves parent conversation ID priority ahead of channel-name/subject fallbacks.

Plugin-Owned Surfaces

  • extensions/telegram/session-key-api.ts
  • extensions/telegram/src/session-conversation.ts
  • extensions/feishu/session-key-api.ts
  • extensions/feishu/src/session-conversation.ts

Contract

The canonical channel-plugin hook is:

  • messaging.resolveSessionConversation({ kind, rawId })

It returns:

  • id
  • threadId?
  • baseConversationId?
  • parentConversationCandidates?

parentConversationCandidates, when present, are ordered from narrowest parent to broadest/base conversation.

The legacy compatibility hook remains supported:

  • messaging.resolveParentConversationCandidates(...)

Core only uses the legacy hook when the canonical hook omits parentConversationCandidates.

Behavior

  • No config migration.
  • No intended breaking change for existing session keys.
  • Mixed-case Telegram :Topic: parsing remains accepted.
  • Mixed-case Feishu :Topic: / :Sender: parsing remains accepted.
  • Scoped Feishu sessions inherit base group tool policy correctly.
  • Channel-vs-group model override routing respects plugin-owned conversation kind.

Verification

Passed:

  • pnpm test -- src/channels/plugins/session-conversation.test.ts extensions/telegram/src/channel.test.ts extensions/feishu/src/channel.test.ts src/agents/pi-tools-agent-config.test.ts src/agents/pi-tools.policy.test.ts
  • pnpm test -- src/channels/model-overrides.test.ts src/auto-reply/reply/model-selection.test.ts
  • pnpm test -- src/agents/tools/sessions-send-helpers.test.ts src/channels/plugins/session-conversation.test.ts src/channels/model-overrides.test.ts src/agents/pi-tools-agent-config.test.ts src/auto-reply/reply/model-selection.test.ts src/config/sessions/delivery-info.test.ts src/config/sessions/reset.test.ts
  • pnpm plugin-sdk:api:check

Known unrelated repo failures on current mainline / tree:

  • pnpm build
  • pnpm check

These still fail outside this PR in existing ModelRegistry/provider test surfaces.

Copilot AI review requested due to automatic review settings March 31, 2026 14:04
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation channel: telegram Channel integration: telegram gateway Gateway runtime agents Agent runtime and tooling channel: feishu Channel integration: feishu size: L maintainer Maintainer-authored PR labels Mar 31, 2026
@greptile-apps

greptile-apps Bot commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR completes the channel plugin boundary established in #58100 by moving Telegram :topic: and Feishu :sender:/topic inheritance rules out of core session helpers and into their respective channel plugins. A new session-conversation.ts dispatcher provides the core outer loop (generic :thread: parsing + plugin delegation), while session-key-utils.ts is narrowed to pure data structures. All shared callers (delivery-info, reset, model-overrides, model-selection, sessions-send-helpers, pi-tools.policy) are updated to route through the new plugin-aware dispatcher.

Key changes:

  • New ChannelMessagingAdapter hooks (resolveSessionConversation, resolveParentConversationCandidates) allow plugins to own their session grammar
  • parseThreadSessionSuffix is narrowed to :thread: only (:topic: removed from core); its now-unused channelHint / options parameter is dropped
  • parseSessionConversationRef is renamed to parseRawSessionConversationRef and returns raw data (rawId, prefix) instead of pre-parsed id/threadId
  • model-overrides.ts cleanly separates keys (direct match candidates) from parentKeys (parent fallback candidates), with matchSource shifting from \"direct\" to \"parent\" for parent-derived keys — callers only use .model, so no behavioral regression
  • New createSessionConversationTestRegistry consolidates test stubs that previously were scattered and duplicated across multiple test files

Confidence Score: 5/5

Safe to merge — no P0 or P1 issues found; all behavioral changes are intentional and covered by tests.

The refactor is well-scoped and well-tested. All shared callers that needed updating were updated, the plugin seam is clearly defined, the new dispatcher correctly handles both the generic 🧵 case and plugin-delegated session grammars, and the removal of the Telegram-specific channelHint path from core is clean. The only findings are P2 style observations that do not affect correctness for any current or planned use.

No files require special attention. src/channels/plugins/session-conversation.ts is the new core dispatcher and worth a quick read before any future plugin adds both hooks.

Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/channels/plugins/session-conversation.ts
Line: 112-117

Comment:
**`resolveParentConversationCandidates` silently wins over `resolveSessionConversation`'s candidates**

When a plugin implements both hooks (as Telegram and Feishu do), any `parentConversationCandidates` returned inside `resolveSessionConversation` are discarded if `resolveParentConversationCandidates` returns a non-null value — including an empty array `[]`, which would wipe out the candidates from the first hook.

Both current plugins return consistent values from both hooks, so there's no bug today. However, a future plugin author who puts candidates inside `resolveSessionConversation` and expects them to be honored (perhaps as a baseline merged with `resolveParentConversationCandidates`) would be surprised that the latter completely overrides rather than complements the former.

Consider a short doc comment above this block clarifying the intentional override semantics:

```typescript
// resolveParentConversationCandidates, when present, is the canonical source for
// parent candidates and fully overrides any candidates returned by resolveSessionConversation.
const parentConversationCandidates = dedupeConversationIds(
  messaging?.resolveParentConversationCandidates?.({
    kind: params.kind,
    rawId,
  }) ?? resolved.parentConversationCandidates,
);
```

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: src/channels/model-overrides.ts
Line: 76-82

Comment:
**`kind: "group"` hard-coded for channel-type conversations**

`groupId` can represent either a group or a channel-type conversation (e.g., a Slack channel), but `resolveParentConversationCandidates` always receives `kind: "group"`. Both current plugins (Telegram, Feishu) ignore `kind` in their parsing, so there's no practical issue today. If a future plugin varies behavior by `kind`, it would silently receive the wrong value here.

Consider deriving `kind` from the session key shape or adding a `kind` field to `ChannelModelOverrideParams`.

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "Refactor: move session conversation gram..." | Re-trigger Greptile

Comment thread src/channels/model-overrides.ts Resolved
@aisle-research-bot

aisle-research-bot Bot commented Mar 31, 2026 •

Copy link
Copy Markdown

🔒 Aisle Security Analysis

We found 1 potential security issue(s) in this PR:

# Severity Title
1 🔵 Low Potential event-loop DoS via synchronous filesystem checks/module loading on sessionKey parsing path
1. 🔵 Potential event-loop DoS via synchronous filesystem checks/module loading on sessionKey parsing path
Property Value
Severity Low
CWE CWE-400
Location src/channels/plugins/session-conversation.ts:129-160

Description

resolveSessionConversationRef() and resolveSessionThreadInfo() parse a caller-provided sessionKey and may fall back to loading a bundled plugin "public surface" using synchronous filesystem checks and a synchronous module loader.

  • Input: sessionKey (string) is accepted from higher-level callers (e.g., gateway/config/session helpers) and is parsed into {channel, kind, rawId}.
  • Hot path / sink: when the runtime plugin registry is unavailable or does not resolve messaging.resolveSessionConversation, the code calls:
    • resolveBundledPluginPublicSurfacePath(...) which performs multiple fs.existsSync(...) checks
    • loadBundledPluginPublicSurfaceModuleSync(...) which performs sync boundary file opening and loads the module via Jiti synchronously
  • Because this happens on the request thread, an attacker who can repeatedly trigger code paths that call resolveSessionThreadInfo/resolveSessionConversationRef with crafted sessionKey values can cause blocking I/O on the Node.js event loop, degrading throughput.

Although loadBundledPluginPublicSurfaceModuleSync caches loaded modules by resolved module path, the existence checks are still synchronous on every call, and varying channel values can increase filesystem work.

Vulnerable code:

if (!resolveBundledPluginPublicSurfacePath({
  rootDir: OPENCLAW_PACKAGE_ROOT,
  bundledPluginsDir: resolveBundledPluginsDir(),
  dirName,
  artifactBasename: SESSION_KEY_API_ARTIFACT_BASENAME,
})) {
  return null;
}

const resolveSessionConversation =
  loadBundledPluginPublicSurfaceModuleSync({ dirName, artifactBasename: SESSION_KEY_API_ARTIFACT_BASENAME })
    .resolveSessionConversation;

Recommendation

Avoid synchronous filesystem and module-loading work on paths reachable with attacker-controlled sessionKey values.

Suggested mitigations (choose applicable):

  1. Cache negative/positive resolution results for (dirName, artifactBasename) so repeated calls don't hit fs.existsSync:
const publicSurfaceExistsCache = new Map<string, boolean>();

function bundledSessionKeyApiExists(dirName: string): boolean {
  const key = `${dirName}:${SESSION_KEY_API_ARTIFACT_BASENAME}`;
  const cached = publicSurfaceExistsCache.get(key);
  if (cached != null) return cached;
  const exists = Boolean(resolveBundledPluginPublicSurfacePath({
    rootDir: OPENCLAW_PACKAGE_ROOT,
    bundledPluginsDir: resolveBundledPluginsDir(),
    dirName,
    artifactBasename: SESSION_KEY_API_ARTIFACT_BASENAME,
  }));
  publicSurfaceExistsCache.set(key, exists);
  return exists;
}
  1. Preload known bundled session-key modules during startup, or move to async loading (import()/async loader) and ensure the hot path uses only in-memory lookups.

  2. Validate/allowlist channel identifiers before attempting bundled resolution, so arbitrary user-provided channel values cannot trigger repeated filesystem probing.


Analyzed PR: #58400 at commit 0f3a457

Last updated on: 2026-03-31T17:53:59Z

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Refactors session conversation parsing so that provider-specific session-id grammar (Telegram topics, Feishu sender/topic scoping + inheritance) is owned by each channel plugin instead of being hard-coded in core session helpers. This tightens the core/plugin boundary while keeping generic :thread: parsing in core.

Changes:

  • Introduces plugin-owned session conversation hooks (resolveSessionConversation, resolveParentConversationCandidates) on the channel messaging adapter, plus a core dispatcher (src/channels/plugins/session-conversation.ts).
  • Moves Telegram :topic: thread parsing and Feishu parent-candidate inheritance logic into their respective channel plugins.
  • Updates core consumers (delivery/reset/model override/routing tools) and tests to route through the new dispatcher; updates docs to reflect the boundary.

Reviewed changes

Copilot reviewed 26 out of 26 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/test-utils/session-conversation-registry.ts Adds a dedicated test plugin registry that implements session-grammar hooks for seam tests.
src/sessions/session-key-utils.ts Removes Telegram-specific :topic: handling from generic thread parsing; introduces raw session conversation ref parsing.
src/routing/session-key.test.ts Updates expectations so :topic: is no longer treated as a generic thread suffix.
src/gateway/server-methods/config.ts Updates restart-routing comment to reflect plugin-owned session grammar.
src/config/sessions/reset.ts Switches thread-session detection to the new session conversation resolver.
src/config/sessions/reset.test.ts Activates a plugin registry in tests to exercise plugin-owned session grammar.
src/config/sessions/delivery-info.ts Routes delivery thread extraction through the new resolver.
src/config/sessions/delivery-info.test.ts Activates a plugin registry in tests to exercise plugin-owned session grammar.
src/channels/plugins/types.core.ts Extends ChannelMessagingAdapter with session-grammar and parent-candidate hooks.
src/channels/plugins/session-conversation.ts New core dispatcher that combines generic :thread: parsing with plugin-owned grammar + parent candidates.
src/channels/plugins/session-conversation.test.ts New seam tests validating generic vs plugin-owned session grammar behavior.
src/channels/model-overrides.ts Reworks model-override candidate generation to use plugin-owned parent conversation candidates.
src/channels/model-overrides.test.ts Activates a plugin registry in tests to exercise plugin-owned parent-candidate logic.
src/auto-reply/reply/model-selection.ts Switches parent session derivation to the new resolver.
src/auto-reply/reply/model-selection.test.ts Activates a plugin registry in tests to exercise plugin-owned session grammar.
src/agents/tools/sessions-send-helpers.ts Switches announce-target parsing to the new resolver.
src/agents/tools/sessions-send-helpers.test.ts Uses the new session-conversation test registry (reduces duplicated test stubs).
src/agents/tools/gateway-tool.ts Updates restart-routing comment to reflect plugin-owned session grammar.
src/agents/pi-tools.policy.ts Switches thread parent session derivation to the new resolver.
src/agents/pi-tools-agent-config.test.ts Activates a plugin registry in tests to exercise plugin-owned session grammar.
extensions/telegram/src/channel.ts Implements plugin-owned Telegram topic session grammar via messaging hook.
extensions/telegram/src/channel.test.ts Adds tests asserting Telegram plugin owns topic parsing + parent candidates.
extensions/feishu/src/channel.ts Implements plugin-owned Feishu session inheritance candidates via messaging hook.
extensions/feishu/src/channel.test.ts Adds tests asserting Feishu plugin owns sender/topic inheritance candidates.
docs/plugins/sdk-channel-plugins.md Documents that session grammar belongs in channel plugins and points to the new hooks.
docs/plugins/architecture.md Updates architecture boundary documentation to include plugin-owned session conversation grammar.

Comment thread extensions/telegram/src/channel.ts Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 99e5c4382c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/channels/model-overrides.ts Resolved
@gumadeiras gumadeiras self-assigned this Mar 31, 2026
@openclaw-barnacle openclaw-barnacle Bot added channel: discord Channel integration: discord size: XL and removed size: L labels Mar 31, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: da8337e4bb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@gumadeiras gumadeiras changed the title Refactor: move session conversation grammar into channel plugins Fix: move bootstrap session grammar into plugin-owned session-key surfaces Mar 31, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b3c3b29644

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@gumadeiras
gumadeiras force-pushed the codex/plugin-owned-session-routing branch from 279e675 to efa8632 Compare March 31, 2026 22:50

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d3ee04de63

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/channels/model-overrides.ts Resolved
@gumadeiras
gumadeiras merged commit bea53d7 into main Mar 31, 2026
9 checks passed
@gumadeiras
gumadeiras deleted the codex/plugin-owned-session-routing branch March 31, 2026 23:41
@gumadeiras

Copy link
Copy Markdown
Member Author

Merged via squash.

Thanks @gumadeiras!

mcaxtr pushed a commit that referenced this pull request Apr 1, 2026
…faces (#58400)

Merged via squash.

Prepared head SHA: b062b18
Co-authored-by: gumadeiras <[email protected]>
Co-authored-by: gumadeiras <[email protected]>
Reviewed-by: @gumadeiras
steipete pushed a commit to Mlightsnow/openclaw that referenced this pull request Apr 1, 2026
…faces (openclaw#58400)

Merged via squash.

Prepared head SHA: b062b18
Co-authored-by: gumadeiras <[email protected]>
Co-authored-by: gumadeiras <[email protected]>
Reviewed-by: @gumadeiras
lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
…faces (openclaw#58400)

Merged via squash.

Prepared head SHA: b062b18
Co-authored-by: gumadeiras <[email protected]>
Co-authored-by: gumadeiras <[email protected]>
Reviewed-by: @gumadeiras
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
…faces (openclaw#58400)

Merged via squash.

Prepared head SHA: b062b18
Co-authored-by: gumadeiras <[email protected]>
Co-authored-by: gumadeiras <[email protected]>
Reviewed-by: @gumadeiras
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
…faces (openclaw#58400)

Merged via squash.

Prepared head SHA: b062b18
Co-authored-by: gumadeiras <[email protected]>
Co-authored-by: gumadeiras <[email protected]>
Reviewed-by: @gumadeiras
Nachx639 pushed a commit to Nachx639/clawdbot that referenced this pull request Jun 17, 2026
…faces (openclaw#58400)

Merged via squash.

Prepared head SHA: b062b18
Co-authored-by: gumadeiras <[email protected]>
Co-authored-by: gumadeiras <[email protected]>
Reviewed-by: @gumadeiras
vixco pushed a commit to vixco/openclaw that referenced this pull request Aug 16, 2026
…faces (openclaw#58400)

Merged via squash.

Prepared head SHA: b062b18
Co-authored-by: gumadeiras <[email protected]>
Co-authored-by: gumadeiras <[email protected]>
Reviewed-by: @gumadeiras
globalcaos pushed a commit to globalcaos/tinkerclaw that referenced this pull request Sep 29, 2026
…faces (openclaw#58400)

Merged via squash.

Prepared head SHA: b062b18
Co-authored-by: gumadeiras <[email protected]>
Co-authored-by: gumadeiras <[email protected]>
Reviewed-by: @gumadeiras
lhfer pushed a commit to lhfer/openclaw that referenced this pull request Oct 11, 2026
…faces (openclaw#58400)

Merged via squash.

Prepared head SHA: b062b18
Co-authored-by: gumadeiras <[email protected]>
Co-authored-by: gumadeiras <[email protected]>
Reviewed-by: @gumadeiras
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling channel: discord Channel integration: discord channel: feishu Channel integration: feishu channel: telegram Channel integration: telegram docs Improvements or additions to documentation gateway Gateway runtime maintainer Maintainer-authored PR size: XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants