Skip to content

fix(telegram): deliver rich-message authoring contract to every runtime via inbound formatting hints - #108264

Merged
obviyus merged 2 commits into
mainfrom
feat/telegram-rich-inbound-hints
Jul 15, 2026
Merged

obviyus merged 2 commits into
mainfrom
feat/telegram-rich-inbound-hints

Conversation

@obviyus

@obviyus obviyus commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

What Problem This Solves

The Telegram rich-message authoring contract (Bot API 10.2 blocks + HTML islands, added in #107986) lived as a telegram-specific string inside core buildAgentSystemPrompt's Messaging section. Only embedded runtimes that consume that builder (e.g. claude-cli) ever saw it. The native Codex runtime assembles its own developer prompt and never received the contract — verified live on a Codex rollout: the developer message contained no ## Messaging section, no rich ON line, not even the rich OFF line. Codex-backed agents on rich-enabled accounts authored from stale habits (hand-drawn ASCII tables in code fences, legacy <blockquote expandable>, literal ☐ checkboxes) instead of emitting the markdown/island contract that renders as native blocks.

Why This Change Was Made

There is an existing channel-plugin seam built for exactly this: agentPrompt.inboundFormattingHints, rendered as response_format inside the trusted inbound-meta JSON — which reaches every runtime (it flows through extraSystemPrompt into Codex's buildDeveloperInstructions, confirmed against sibling codex-rs source: developer instructions land as developer-role model context). Slack already uses this hook.

This PR moves the contract to the right owner boundary:

  • extensions/telegram: implements inboundFormattingHints, account-aware (richMessages per account) — rich ON returns markdown_telegram_rich + the full island contract; OFF returns markdown + a short "rich OFF" note.
  • Seam: inboundFormattingHints params gain cfg (matching sibling messageToolHints); the loaded config is threaded from runPreparedReply — no loadConfig() in the render path.
  • Core: the telegram-specific ternary in buildMessagingSection is deleted, along with the now-dead richTextEnabled param and the richText capability plumbing (single producer/consumer pair).
  • System-event turns (heartbeat/cron): hints resolve from the hydrated promptSessionCtx (which restores the persisted channel/account), so scheduled replies delivered to Telegram get the contract too, while trusted metadata still identifies the system event.
  • Docs: docs/channels/telegram.md rich-message section refreshed from the stale 10.1 rich-HTML wording to the 10.2 typed-blocks pipeline.

One canonical source, plugin-owned, delivered uniformly. Core stays plugin-agnostic.

User Impact

Agents on native Codex (and any other runtime that doesn't consume the core system prompt) now reliably emit rich Telegram formatting when richMessages is enabled: native tables, collapsible details, checklists, math, maps, media islands. Rich-OFF accounts get an explicit "rich OFF" hint everywhere too. No config changes required.

Evidence

  • Root cause: live Codex rollout on a production box showed the developer prompt lacked all channel formatting guidance while sendRichMessage delivered a hand-drawn ASCII pre table.
  • Delivery path proven in source: resolveInboundFormattingHints → response_format (src/auto-reply/reply/inbound-meta.ts) → extraSystemPromptParts (get-reply-run.ts) → extensions/codex/src/app-server/thread-lifecycle.ts buildDeveloperInstructions → codex-rs developer_sections → developer-role context.
  • Testbox (Blacksmith): pnpm check:changed green; affected suites green — src/auto-reply/reply (full dir), src/agents/system-prompt.test.ts, src/agents/cli-runner (207 tests), src/agents/prompt-composition.test.ts, extensions/telegram contract/gateway tests (33 tests).
  • New tests: telegram inboundFormattingHints ON/OFF/per-account/default-account; inbound-meta formattingHintsCtx system-event resolution; core system prompt asserts the guidance is gone.
  • Autoreview (Codex gpt-5.6-sol, high): one finding (system-event hint resolution) — fixed with a regression test; rerun clean, "patch is correct (0.91)".
  • Note: the first commit mechanically renames token-shaped test fixtures to allowlisted placeholders (test-token-placeholder) so review tooling can scan the bundle; manually audited, test-only.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation channel: telegram Channel integration: telegram agents Agent runtime and tooling size: L maintainer Maintainer-authored PR labels Jul 15, 2026
@obviyus
obviyus merged commit 8160bbd into main Jul 15, 2026
147 of 151 checks passed
@obviyus
obviyus deleted the feat/telegram-rich-inbound-hints branch July 15, 2026 11:12
@obviyus obviyus self-assigned this Jul 15, 2026
obviyus added a commit that referenced this pull request Sep 25, 2026
Related: #108264

## What Problem This Solves

Fixes: scheduled (cron) runs that announce to a channel get none of that channel's formatting rules. For example, a daily job that posts to a Telegram chat with `richMessages: true` never sees the rich-message contract. The model guesses the markup, so the same job sends a `<table>` on one day and a `<pre>` block on another.

#108264 moved the Telegram rich contract out of the core system prompt and into the channel's inbound formatting hints. Those hints are only built for turns that start from an incoming message (and for heartbeats, which carry their delivery channel in the same context). Isolated cron runs never build them, so they lost the contract.

## User Impact

User impact: a cron job that announces to a channel now gets the same formatting rules as a normal reply on that channel and account. For Telegram, this is the rich contract when `richMessages` is on and the "rich OFF" rules when it is off. Jobs with `delivery.mode: "none"` or a webhook get no channel rules. Inbound replies do not change.

## Why This Change Was Made

- The channel plugin's `inboundFormattingHints` stays the only source of the rule text.
- Cron delivery resolution now also resolves the plugin that will send the output, with the same outbound lookup that delivery uses. This includes a plugin that is loaded on demand for the first announcement after startup. The hints come from that plugin for the delivering account.
- An announce run adds a short trusted `### Delivery Context` block (`openclaw.delivery_meta.v1`) to its extra system prompt. This is the same path that #108264 uses to reach every runtime (embedded, Codex, CLI). The block stays in system metadata, not in the transcript.
- The block only exists when the job requests delivery, the target resolves to a chat, and the plugin has hints. For each job it is the same on every run, so the prompt prefix stays cache-friendly. CLI session binding hashes the same block, so a CLI session and its media-wake continuation keep resuming.

## Evidence

- New `src/cron/isolated-agent/run.delivery-formatting.test.ts` runs the real cron preparation, delivery resolution and executor. The Telegram plugin is not active and is loaded on demand, as on a cold Gateway. Rich ON adds the rich contract for the delivering account, rich OFF adds the OFF rules, and `mode: "none"` adds nothing. The ON and OFF cases fail on `main`. They also fail with a lookup that reads only the active plugin registry.
- Neighboring cron suites pass: message-tool policy and source-delivery guard.
- Cost of the new test file: `pnpm test src/cron/isolated-agent/run.delivery-formatting.test.ts --maxWorkers=1` measures about 4.7 s of test time (33 s wall, most of it cold transform).
- Live check on the Telegram Test Server with a real-user recorder: an isolated cron job announces to the test chat with `richMessages: true` and a mock provider that records requests.
  - `main`: the model request has no delivery context and no `markdown_telegram_rich`.
  - This branch (current head): the developer-role system context of the model request contains the `openclaw.delivery_meta.v1` block with `text_markup: "markdown_telegram_rich"` and the full rule list.
  - On both, the Telegram user received the reply as a native rich message with a table block. The mock reply is fixed, so this shows delivery still works. It does not show a change in model behavior.
- Not live-tested: the CLI and Codex runtimes. They get the same extra-system-prompt input as the embedded runtime that was tested.

Co-authored-by: Ayaan Zaidi <[email protected]>
obviyus added a commit that referenced this pull request Sep 25, 2026
…158028)

Related: #157877
Related: #108264

## What Problem This Solves

Fixes: a turn delivered to Telegram gets the channel's formatting rules in a different shape, twice, or not at all, depending on how it started.

- Reply turns carry the rules inside the inbound metadata. On an account with `richMessages: true`, they also get a separate "Collapsible Details" section, so the `<details>` rule arrives twice.
- Cron announce turns (#157877) carry the same rules in a second block with a different schema.
- Subagent and requester announce turns, and other delivered agent turns (inter-session steps, `openclaw agent --deliver`), get no formatting rules.

## User Impact

User impact: every turn whose visible output is delivered to a channel gets that channel's formatting rules once, in one block. This covers replies, heartbeats, cron announces and subagent announces. For Telegram, the rich-message rules apply only when the delivering account has `richMessages: true`. Other accounts get the standard-formatting rules. Turns with no chat delivery get no rules: no delivery, webhook delivery, subagent children, and Control UI chat. Slack announces now get Slack's rules too. No config change is required.

## Why This Change Was Made

- One core function builds the block: `buildDeliveryFormatPrompt`. Reply turns read the loaded plugin that received the message, as before. Cron and agent-command turns resolve the delivering plugin with on-demand loading, for example for the first cron announce after startup. It asks the plugin for the delivering account's rules and renders one trusted `### Delivery Format` block (`openclaw.delivery_format.v1`) into the extra system prompt. This is the path every runtime (embedded, Codex, CLI) and its compaction already reads.
- Three entry points call this function, each only when its turn has a channel delivery target:
  - Reply metadata, which also covers heartbeats and system events.
  - Cron delivery resolution.
  - Agent-command session preparation, for turns that deliver or use message-tool delivery. It uses the outbound channel and account chosen by delivery preflight, so a turn that starts on one account and delivers through another gets the delivering account's rules.
- Removed:
  - The cron-only builder and its `openclaw.delivery_meta.v1` schema.
  - The `response_format` field in the inbound metadata.
  - Telegram's `markdownDetails` capability. The Telegram contract already contains the `<details>` rule.
- The block has no per-turn fields, so its bytes are the same for every turn kind on the same channel and contract. It stays in trusted system metadata and never enters the transcript. The CLI session-binding inputs are unchanged: replies still keep this block out of the static binding text, and cron still hashes it.
- External ACP agents do not receive OpenClaw's extra system prompt, so they do not get this block. The docs say "OpenClaw agent turn" for that reason.
- Upgrade: the old cron `Delivery Context` block (#157877) is not in any release. Reply turns keep this block out of the CLI session-binding text, as before. A CLI-backed cron session on `main` whose bound prompt text changes resumes with `system-prompt` content drift, not a reset (covered by `src/agents/cli-session.test.ts`, "resumes on content drift").
- Plugin SDK: `inboundFormattingHints` is a shipped field, so this change keeps its name and signature. It is an intentional scope extension: core now calls it for every OpenClaw agent turn whose delivery target is the channel, not only inbound replies. The hook only receives `cfg` and `accountId`, with no inbound message data, so an existing hook returns the same rules it returns today. The SDK docs and type comment describe the new scope.
- Control UI keeps its core-owned `markdownDetails` capability, because it has no channel plugin.
- Follow-ups, not in this PR:
  - A `message` tool send to a different channel than the turn's own still gets only the current channel's rules.
  - `/btw` side answers do not get the block yet.

## Evidence

- Focused tests at the real entry points, with a Telegram stub whose rich and plain accounts return different contracts:
  - `src/auto-reply/reply/get-reply.delivery-format.test.ts`: `getReplyFromConfig` replies on a rich account and on a plain account each get exactly one block with their own contract. A Telegram heartbeat through `runHeartbeatOnce` gets exactly one block. A Control UI reply gets none.
  - `src/commands/agent.delivery-format.test.ts`: `agentCommandFromIngress` with `deliver: true` (rich account) and with message-tool delivery (plain account) each append exactly one block after the caller's prompt. A turn from the rich account that delivers through the plain account gets the plain rules. An undelivered turn keeps the caller's prompt unchanged.
  - `src/cron/isolated-agent/run.delivery-formatting.test.ts`: cron announces load the Telegram plugin on demand, as on a cold Gateway. They get exactly one block for rich and plain accounts, and none with `mode: "none"`.
  - `extensions/telegram/src/channel-actions.contract.test.ts`: in the real Telegram plugin, a rich account and a plain account on the same bot get different contracts, and neither advertises `markdownDetails`.
- The tests fail with the old production code restored: both delivered agent-command cases get no block, and the Telegram test finds `markdownDetails`. With the source account used instead of the outbound account, the cross-account case fails.
- Test cost with `pnpm test <file> --maxWorkers=1`: about 7.5 s of test time for the agent-command file, 9 s for the reply and heartbeat file, and 4.3 s for the cron file. Most of the wall time is cold transform.
- Neighboring suites pass: inbound metadata, prompt session context, reply media-only, agent command, session preparation and the system prompt. Changed-file typecheck, format and line-cap checks pass.
- Live Telegram Test Server run with a real user recorder and a recording mock provider, on `main` and on this branch (reply and cron code as in the current head):

  | Run | Model request system prompt | Telegram result |
  | --- | --- | --- |
  | `main`, reply, rich on | rules inside inbound metadata `response_format`, plus a separate `## Collapsible Details` section (the `<details>` rule twice) | rich message |
  | `main`, cron announce, rich on | rules in `### Delivery Context` (`openclaw.delivery_meta.v1`) | rich message |
  | this branch, reply, rich on | one `### Delivery Format` block (`openclaw.delivery_format.v1`, `markdown_telegram_rich`), `<details>` rule once, no `Collapsible Details` section, no `response_format` in inbound metadata | rich message |
  | this branch, cron announce, rich on | the same `### Delivery Format` block, byte for byte; no `Delivery Context` block | rich message |
  | this branch, reply, rich off | one `### Delivery Format` block with the "Telegram rich OFF" rules | plain text message |

  The mock reply is fixed, so the Telegram message does not change between `main` and this branch. No screenshots are attached for that reason.
- Not live-tested: subagent announces, heartbeats, and the Codex and CLI runtimes. They read the same extra-system-prompt input that the focused tests cover.

Co-authored-by: Ayaan Zaidi <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 26, 2026
…w#157877)

Related: openclaw#108264

## What Problem This Solves

Fixes: scheduled (cron) runs that announce to a channel get none of that channel's formatting rules. For example, a daily job that posts to a Telegram chat with `richMessages: true` never sees the rich-message contract. The model guesses the markup, so the same job sends a `<table>` on one day and a `<pre>` block on another.

openclaw#108264 moved the Telegram rich contract out of the core system prompt and into the channel's inbound formatting hints. Those hints are only built for turns that start from an incoming message (and for heartbeats, which carry their delivery channel in the same context). Isolated cron runs never build them, so they lost the contract.

## User Impact

User impact: a cron job that announces to a channel now gets the same formatting rules as a normal reply on that channel and account. For Telegram, this is the rich contract when `richMessages` is on and the "rich OFF" rules when it is off. Jobs with `delivery.mode: "none"` or a webhook get no channel rules. Inbound replies do not change.

## Why This Change Was Made

- The channel plugin's `inboundFormattingHints` stays the only source of the rule text.
- Cron delivery resolution now also resolves the plugin that will send the output, with the same outbound lookup that delivery uses. This includes a plugin that is loaded on demand for the first announcement after startup. The hints come from that plugin for the delivering account.
- An announce run adds a short trusted `### Delivery Context` block (`openclaw.delivery_meta.v1`) to its extra system prompt. This is the same path that openclaw#108264 uses to reach every runtime (embedded, Codex, CLI). The block stays in system metadata, not in the transcript.
- The block only exists when the job requests delivery, the target resolves to a chat, and the plugin has hints. For each job it is the same on every run, so the prompt prefix stays cache-friendly. CLI session binding hashes the same block, so a CLI session and its media-wake continuation keep resuming.

## Evidence

- New `src/cron/isolated-agent/run.delivery-formatting.test.ts` runs the real cron preparation, delivery resolution and executor. The Telegram plugin is not active and is loaded on demand, as on a cold Gateway. Rich ON adds the rich contract for the delivering account, rich OFF adds the OFF rules, and `mode: "none"` adds nothing. The ON and OFF cases fail on `main`. They also fail with a lookup that reads only the active plugin registry.
- Neighboring cron suites pass: message-tool policy and source-delivery guard.
- Cost of the new test file: `pnpm test src/cron/isolated-agent/run.delivery-formatting.test.ts --maxWorkers=1` measures about 4.7 s of test time (33 s wall, most of it cold transform).
- Live check on the Telegram Test Server with a real-user recorder: an isolated cron job announces to the test chat with `richMessages: true` and a mock provider that records requests.
  - `main`: the model request has no delivery context and no `markdown_telegram_rich`.
  - This branch (current head): the developer-role system context of the model request contains the `openclaw.delivery_meta.v1` block with `text_markup: "markdown_telegram_rich"` and the full rule list.
  - On both, the Telegram user received the reply as a native rich message with a table block. The mock reply is fixed, so this shows delivery still works. It does not show a change in model behavior.
- Not live-tested: the CLI and Codex runtimes. They get the same extra-system-prompt input as the embedded runtime that was tested.

Co-authored-by: Ayaan Zaidi <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 26, 2026
…penclaw#158028)

Related: openclaw#157877
Related: openclaw#108264

## What Problem This Solves

Fixes: a turn delivered to Telegram gets the channel's formatting rules in a different shape, twice, or not at all, depending on how it started.

- Reply turns carry the rules inside the inbound metadata. On an account with `richMessages: true`, they also get a separate "Collapsible Details" section, so the `<details>` rule arrives twice.
- Cron announce turns (openclaw#157877) carry the same rules in a second block with a different schema.
- Subagent and requester announce turns, and other delivered agent turns (inter-session steps, `openclaw agent --deliver`), get no formatting rules.

## User Impact

User impact: every turn whose visible output is delivered to a channel gets that channel's formatting rules once, in one block. This covers replies, heartbeats, cron announces and subagent announces. For Telegram, the rich-message rules apply only when the delivering account has `richMessages: true`. Other accounts get the standard-formatting rules. Turns with no chat delivery get no rules: no delivery, webhook delivery, subagent children, and Control UI chat. Slack announces now get Slack's rules too. No config change is required.

## Why This Change Was Made

- One core function builds the block: `buildDeliveryFormatPrompt`. Reply turns read the loaded plugin that received the message, as before. Cron and agent-command turns resolve the delivering plugin with on-demand loading, for example for the first cron announce after startup. It asks the plugin for the delivering account's rules and renders one trusted `### Delivery Format` block (`openclaw.delivery_format.v1`) into the extra system prompt. This is the path every runtime (embedded, Codex, CLI) and its compaction already reads.
- Three entry points call this function, each only when its turn has a channel delivery target:
  - Reply metadata, which also covers heartbeats and system events.
  - Cron delivery resolution.
  - Agent-command session preparation, for turns that deliver or use message-tool delivery. It uses the outbound channel and account chosen by delivery preflight, so a turn that starts on one account and delivers through another gets the delivering account's rules.
- Removed:
  - The cron-only builder and its `openclaw.delivery_meta.v1` schema.
  - The `response_format` field in the inbound metadata.
  - Telegram's `markdownDetails` capability. The Telegram contract already contains the `<details>` rule.
- The block has no per-turn fields, so its bytes are the same for every turn kind on the same channel and contract. It stays in trusted system metadata and never enters the transcript. The CLI session-binding inputs are unchanged: replies still keep this block out of the static binding text, and cron still hashes it.
- External ACP agents do not receive OpenClaw's extra system prompt, so they do not get this block. The docs say "OpenClaw agent turn" for that reason.
- Upgrade: the old cron `Delivery Context` block (openclaw#157877) is not in any release. Reply turns keep this block out of the CLI session-binding text, as before. A CLI-backed cron session on `main` whose bound prompt text changes resumes with `system-prompt` content drift, not a reset (covered by `src/agents/cli-session.test.ts`, "resumes on content drift").
- Plugin SDK: `inboundFormattingHints` is a shipped field, so this change keeps its name and signature. It is an intentional scope extension: core now calls it for every OpenClaw agent turn whose delivery target is the channel, not only inbound replies. The hook only receives `cfg` and `accountId`, with no inbound message data, so an existing hook returns the same rules it returns today. The SDK docs and type comment describe the new scope.
- Control UI keeps its core-owned `markdownDetails` capability, because it has no channel plugin.
- Follow-ups, not in this PR:
  - A `message` tool send to a different channel than the turn's own still gets only the current channel's rules.
  - `/btw` side answers do not get the block yet.

## Evidence

- Focused tests at the real entry points, with a Telegram stub whose rich and plain accounts return different contracts:
  - `src/auto-reply/reply/get-reply.delivery-format.test.ts`: `getReplyFromConfig` replies on a rich account and on a plain account each get exactly one block with their own contract. A Telegram heartbeat through `runHeartbeatOnce` gets exactly one block. A Control UI reply gets none.
  - `src/commands/agent.delivery-format.test.ts`: `agentCommandFromIngress` with `deliver: true` (rich account) and with message-tool delivery (plain account) each append exactly one block after the caller's prompt. A turn from the rich account that delivers through the plain account gets the plain rules. An undelivered turn keeps the caller's prompt unchanged.
  - `src/cron/isolated-agent/run.delivery-formatting.test.ts`: cron announces load the Telegram plugin on demand, as on a cold Gateway. They get exactly one block for rich and plain accounts, and none with `mode: "none"`.
  - `extensions/telegram/src/channel-actions.contract.test.ts`: in the real Telegram plugin, a rich account and a plain account on the same bot get different contracts, and neither advertises `markdownDetails`.
- The tests fail with the old production code restored: both delivered agent-command cases get no block, and the Telegram test finds `markdownDetails`. With the source account used instead of the outbound account, the cross-account case fails.
- Test cost with `pnpm test <file> --maxWorkers=1`: about 7.5 s of test time for the agent-command file, 9 s for the reply and heartbeat file, and 4.3 s for the cron file. Most of the wall time is cold transform.
- Neighboring suites pass: inbound metadata, prompt session context, reply media-only, agent command, session preparation and the system prompt. Changed-file typecheck, format and line-cap checks pass.
- Live Telegram Test Server run with a real user recorder and a recording mock provider, on `main` and on this branch (reply and cron code as in the current head):

  | Run | Model request system prompt | Telegram result |
  | --- | --- | --- |
  | `main`, reply, rich on | rules inside inbound metadata `response_format`, plus a separate `## Collapsible Details` section (the `<details>` rule twice) | rich message |
  | `main`, cron announce, rich on | rules in `### Delivery Context` (`openclaw.delivery_meta.v1`) | rich message |
  | this branch, reply, rich on | one `### Delivery Format` block (`openclaw.delivery_format.v1`, `markdown_telegram_rich`), `<details>` rule once, no `Collapsible Details` section, no `response_format` in inbound metadata | rich message |
  | this branch, cron announce, rich on | the same `### Delivery Format` block, byte for byte; no `Delivery Context` block | rich message |
  | this branch, reply, rich off | one `### Delivery Format` block with the "Telegram rich OFF" rules | plain text message |

  The mock reply is fixed, so the Telegram message does not change between `main` and this branch. No screenshots are attached for that reason.
- Not live-tested: subagent announces, heartbeats, and the Codex and CLI runtimes. They read the same extra-system-prompt input that the focused tests cover.

Co-authored-by: Ayaan Zaidi <[email protected]>
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: telegram Channel integration: telegram docs Improvements or additions to documentation maintainer Maintainer-authored PR size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant