Skip to content

fix(telegram): /codex detach and rebind fail with 'Telegram runtime not initialized' - #175379

Merged
obviyus merged 4 commits into
openclaw:mainfrom
Satoshi-Kusumoto:fix/session-binding-adapter-owner
Oct 18, 2026
Merged

obviyus merged 4 commits into
openclaw:mainfrom
Satoshi-Kusumoto:fix/session-binding-adapter-owner

Conversation

@Satoshi-Kusumoto

@Satoshi-Kusumoto Satoshi-Kusumoto commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Closes #175372
Related: #173667

What Problem This Solves

Fixes: /codex detach and re-running /codex bind fail with Codex command failed: Telegram runtime not initialized in a Telegram conversation bound to Codex, and the conversation stays bound.

User Impact

User impact: in Telegram conversations bound to Codex, /codex detach and a repeated /codex bind succeed, so /session unbind is no longer needed as a workaround. Other channels and the core session-binding service keep their current behavior, including during plugin reloads.

Why This Change Was Made

/codex handlers run in the Codex plugin instance. When they change a Telegram binding through the core session-binding service, Telegram's adapter runs in that caller's context. Telegram's binding persistence looked up its plugin runtime at write time, and that lookup reads the slot of the plugin instance that is currently running, so it found the Codex instance's empty slot. /session unbind worked because it runs in Telegram's own inbound scope.

The Telegram thread-binding manager is created inside the Telegram plugin instance when the channel starts. It now keeps that runtime and persists through it, no matter which plugin calls its adapter. The runtime's registry checks still apply, so a retired Telegram registry still refuses the write.

An earlier revision of this PR wrapped every session-binding adapter in its registering plugin instance in core. That also fixed the bug, but it made every adapter call (including reads and unscoped fan-out such as session deletion) fail while a channel plugin is quiesced during a reload. This revision keeps the fix inside Telegram's binding owner, so the core call semantics do not change and no new reload-window failure is introduced.

Thanks @Satoshi-Kusumoto for the report, the root-cause analysis and the cross-instance tests, which this revision keeps in a smaller form.

Evidence

Real Telegram (Test Server, leased QA account) driving the bundled Codex plugin through a fresh Gateway. Scenario: /codex bind --cwd /tmp, tap Allow once, /codex binding, /codex bind --cwd /tmp again, /codex detach, /codex binding.

  • main (d3dc09e): the repeated bind and the detach both reply Codex command failed: Telegram runtime not initialized; the final /codex binding still shows the binding.
  • This PR: the repeated bind replies Bound this conversation to a new thread in /tmp. The next message will initialize it., detach replies Detached this conversation from Codex., and the final /codex binding replies No Codex conversation binding is attached.

Focused tests (src/plugins/conversation-binding.cross-instance.test.ts, extensions/telegram/src/thread-bindings.test.ts, extensions/telegram/src/thread-bindings-reconcile.test.ts): 27 passed. With main's Telegram binding files swapped back in, all three cross-instance cases fail, so the tests catch the defect.

Merge danger

Two-way door: no stored state, config or schema changes, and a revert restores the old lookup. Blast radius is Telegram thread-binding persistence only; Discord, Matrix, Feishu and the core adapter registry are untouched.

…n instance

Session binding adapters were registered without their owning plugin
instance. When another plugin reached a channel adapter through the core
service (for example `/codex detach` in a Telegram conversation), the
adapter ran in the caller's instance, so the channel's runtime lookup hit
an empty slot and failed with "Telegram runtime not initialized".

Wrap the normalized adapter at registration, as core already does for
command handlers and channel plugins. Unregistration still matches the
original adapter object, and adapters registered outside any plugin
instance keep their previous behavior.
@clawsweeper

clawsweeper Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@clawsweeper clawsweeper Bot added P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Oct 17, 2026
@clawsweeper

clawsweeper Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

Telegram’s binding manager retains its owning runtime so Codex commands can persist rebind and detach operations across plugin instances.

Example: Run /codex bind --cwd /tmp again in an already-bound Telegram conversation, then run /codex detach.

  • Before: Both commands report “Telegram runtime not initialized,” and the conversation remains bound.
  • After: Rebind succeeds, detach succeeds, and /codex binding reports no attached binding.

Review scores

Measure Result What it means
Overall readiness 🦞 diamond lobster (5/6) Focused owner repair with recorded shipped-command proof, distinct regression coverage, and no actionable findings; hosted CI is still pending.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): The PR records real Telegram Test Server commands through a fresh Gateway and bundled Codex plugin: repeated bind succeeds, detach succeeds, and final inspection reports no binding. The supplied screenshot confirms the original failure; reported production-owner tests additionally exercise refusal after registry retirement with persisted state unchanged.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Telegram users cannot detach or rebind an existing Codex conversation through the documented commands.
Reason: Restores documented behavior through the existing owner, with no new option, public API, or competing state path.

Merge readiness

✅ Ready for maintainer review

Keep open: this PR fixes a demonstrated bug still present on main, and its current Telegram-local implementation resolves the earlier reload concern without an actionable finding.

Priority: P1
Reviewed head: 6528d19cba359d50cda21f905f87d865ce9a3bfd

Before merge

None.

Findings

None.

Agent review details

How this fits together

Codex commands call the shared conversation-binding service, whose Telegram adapter updates live routing and persists binding records through Telegram’s registry-owned SQLite worker stores.

flowchart LR
  A[Telegram command] --> B[Codex plugin instance]
  B --> C[Conversation binding service]
  C --> D[Telegram binding manager]
  D --> E[Captured Telegram runtime]
  E --> F[SQLite worker store]
Loading

Technical review

Best possible solution:

Keep runtime selection inside Telegram’s existing binding manager while preserving shared adapter semantics and worker-held authority.

Do we have a high-confidence way to reproduce the issue?

Current-main source exposes the cross-instance runtime-slot failure, corroborated by the reporter screenshot and the PR’s recorded live base reproduction; no target code was executed during this read-only review.

Is this the best way to solve the issue?

The current revision is the smallest complete repair: capture the manager’s runtime once and reuse it across every persistence path, without changing core adapter invocation or reload admission.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against c23f752a67e6.

Provenance checked

  • Telegram persistence runtime selection changes intended behavior with a stated reason (71bb516: The worker migration retained ordered binding persistence, commit acknowledgement, and shutdown drainage; its persistence helper resolved the runtime when used.)
  • Async and synchronous store contracts keeps the original intent (a157713: Separate current asynchronous receivers from released synchronous compatibility adapters while preserving their respective semantics.)
  • Prepared startup reconciliation keeps the original intent (1f8e598: Retain prepared session ownership through binding cleanup and propagate authority refusals.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

  • Current-main failure mechanism: Current main still resolves getTelegramRuntime when opening the binding store; runtime slots belong to the invoking plugin instance, explaining the empty Telegram slot during Codex calls. (extensions/telegram/src/thread-bindings-persistence.ts:15, c23f752a67e6)
  • Focused owner repair: The manager captures Telegram’s runtime during initialization and passes it through loading, reconciliation, asynchronous mutations, and synchronous compatibility writes; every persistence caller was migrated. (extensions/telegram/src/thread-bindings.ts:89, 6528d19cba35)
  • Live behavior evidence: The PR body records a fresh Gateway and real Telegram Test Server sequence covering initial approval, repeated bind, detach, and final binding inspection, with explicit failing base and successful patched responses. The downloaded reporter screenshot independently confirms the original error and successful /session unbind workaround. (6528d19cba35)
  • Retirement refusal at persistence boundary: The new production-owner integration tests cover cross-instance refresh and detach, plus retirement while registration is queued; the latter requires refusal and an unchanged stored record. The body reports all three fail with the base Telegram files restored and the focused suites pass after the fix. (src/plugins/conversation-binding.cross-instance.test.ts:239, 6528d19cba35)
  • Authority remains owner-bound: Registry runtime stores capture assertRuntimeCurrent, and worker write admission invokes both registry and operation authority before transaction and commit grants; retaining the runtime does not replace those checks. (src/plugin-state/plugin-state-worker-client.ts:330, 6528d19cba35)
  • History and intended persistence contract: The worker migration established FIFO mutations, acknowledged publication, shutdown drainage, and synchronous SDK compatibility. This PR preserves those mechanics. Some older blob searches reached the local lazy-fetch limit; available history, base blobs, and current source establish the relevant contract. (extensions/telegram/src/thread-bindings-persistence.ts:14, 71bb516084f5)

PR surface

Source +20, Tests +271. Total +291 across 5 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 4 32 12 +20
Tests 1 271 0 +271
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 5 303 12 +291

Review metrics

Metric Value Why it matters
Production versus test changes Production +32/-12 lines; tests +271/-0 lines Production growth passes the existing runtime through its owner’s persistence paths; test scaffolding exercises real cross-instance operations and queued retirement without adding production test seams.

Labels

Label changes:

  • add rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • remove rating: 🦐 gold shrimp: Current PR rating is rating: 🦞 diamond lobster, so this older rating label is no longer current.
  • remove merge-risk: 🚨 compatibility: Current PR review selected no merge-risk labels.

Label justifications:

  • P1: An existing Telegram Codex workflow cannot detach or replace its binding, leaving conversation state unchanged until the user uses a separate command.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

History

Review history (3 earlier review cycles)
  • reviewed 2026-10-17T09:37:04.883Z sha 5b38bf2 :: needs real behavior proof before merge. :: none
  • reviewed 2026-10-17T18:04:27.149Z sha 8607d6d :: needs maintainer review before merge. :: none
  • reviewed 2026-10-17T18:13:37.566Z sha 8607d6d :: blocked before merge. :: none

Reviewed October 18, 2026, 1:10 AM ET / 05:10 UTC (Revision 4).

Add a core test that drives Telegram's real thread-binding manager,
with its session binding adapter registered inside a Telegram plugin
instance, through requestPluginConversationBinding and
detachPluginConversationBinding from other plugin instances. It covers
refresh and detach by the owning plugin root, a different plugin root,
a quiesced Telegram instance, a replaced session binding adapter, and
a retired plugin registry, and checks the stored Telegram entry after
each refusal.

Rename the outside-instance guard test in session-binding-service.test.ts
to say which instance the adapter runs in, rename its runtime-store key
and error message to match, and record the observed owner in a local
variable.
@Satoshi-Kusumoto

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Oct 17, 2026
@Satoshi-Kusumoto

Copy link
Copy Markdown
Contributor Author

Updated the PR body after the latest ClawSweeper review; the code is unchanged and the head is still 8607d6d. The Risk item now describes the reload interval (between quiesce and channel stop, while the adapter is still registered) and its consequences from source reading, including unscoped fan-out that stops partway and a sessions.delete that can fail after deleting the session and leave bindings that point to it. The remaining review item is the owner decision that review raised: whether session-wide binding operations may fail in that interval as part of this fix.

Since #175769 landed, this PR conflicts in registerAdapter; I plan to resolve it after that decision by keeping the registered original for unregistration, storing a separate instance view for calls, and letting the current-registration check accept either (about +6/-4 production lines against current main), or whoever lands this PR can resolve it instead.

Session-binding adapter calls can run in another plugin's instance, for
example /codex detach or a repeated /codex bind. Telegram resolved its
plugin runtime from the caller's instance slot at write time, so those
commands failed with "Telegram runtime not initialized".

The thread binding manager now keeps the runtime of the Telegram instance
that created it and persists through it. The core adapter registry keeps
its existing call semantics, so other channels' adapters are not fenced
during a plugin reload.
@openclaw-barnacle openclaw-barnacle Bot added channel: telegram Channel integration: telegram size: M and removed size: L labels Oct 18, 2026
@obviyus obviyus changed the title fix(session-binding): /codex detach and rebind fail with "Telegram runtime not initialized" fix(telegram): /codex detach and rebind fail with 'Telegram runtime not initialized' Oct 18, 2026
@obviyus

obviyus commented Oct 18, 2026

Copy link
Copy Markdown
Collaborator

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Oct 18, 2026
@Satoshi-Kusumoto

Copy link
Copy Markdown
Contributor Author

@obviyus Thanks for picking this up and reworking the fix.

@obviyus
obviyus merged commit 2a30b52 into openclaw:main Oct 18, 2026
139 of 142 checks passed
@obviyus

obviyus commented Oct 18, 2026

Copy link
Copy Markdown
Collaborator

Landed in 2a30b52. Thanks @Satoshi-Kusumoto for the report, the root-cause analysis and the cross-instance tests, and for sharing it with the community.

Maintainer follow-up: the fix now lives in Telegram's binding owner instead of wrapping every core session-binding adapter, so plugin reloads keep their current behavior. Proven end to end on real Telegram against main.

steipete added a commit that referenced this pull request Oct 18, 2026
The registry-backed fixture registers Telegram's channel directly and
omitted the runtime initialization performed by the production loader.
Since #175379 captures that runtime when creating the binding manager,
the contract failed with "Telegram runtime not initialized".

Initialize the fixture through Telegram's narrow runtime setter and back
its state API with the existing isolated plugin-state test store. Preserve
all capability, bind, unbind and cleanup assertions. Production startup,
reload, cross-plugin runtime ownership and Telegram transport are unchanged.

Current-main baseline reproduced the reported failure, with four other
channel cases passing. The repaired contract passes all five cases under
pinned Bun. Owning core/plugin-SDK test types, scoped lint, formatting and
independent P2 autoreview pass. No live Telegram behavior is claimed.
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 19, 2026
…ot initialized' (openclaw#175379)

`/codex detach` and a repeated `/codex bind` in a Telegram conversation no longer fail with "Telegram runtime not initialized". The Telegram thread-binding manager keeps the runtime it was created with and persists through it, whichever plugin calls its adapter. Registry checks still apply, so a retired Telegram registry still refuses the write. Core session-binding semantics are unchanged.

Proof: real Telegram (Test Server) driving the bundled Codex plugin through a fresh Gateway. On main 72a9fbb the repeated bind and the detach both fail and the binding stays; the candidate binds, detaches and reports no binding. Live gate passed with openai/gpt-5-mini.

Thanks @Satoshi-Kusumoto.

Co-authored-by: Ayaan Zaidi <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 19, 2026
The registry-backed fixture registers Telegram's channel directly and
omitted the runtime initialization performed by the production loader.
Since openclaw#175379 captures that runtime when creating the binding manager,
the contract failed with "Telegram runtime not initialized".

Initialize the fixture through Telegram's narrow runtime setter and back
its state API with the existing isolated plugin-state test store. Preserve
all capability, bind, unbind and cleanup assertions. Production startup,
reload, cross-plugin runtime ownership and Telegram transport are unchanged.

Current-main baseline reproduced the reported failure, with four other
channel cases passing. The repaired contract passes all five cases under
pinned Bun. Owning core/plugin-SDK test types, scoped lint, formatting and
independent P2 autoreview pass. No live Telegram behavior is claimed.
iFwu added a commit to iFwu/openclaw that referenced this pull request Nov 4, 2026
Backport the fix from upstream PR openclaw#175379 (2a30b52) to the 2026.9 plugin-state API. Capture the owning Telegram runtime for every persisted binding mutation, including synchronous lifecycle updates. Add a regression covering cross-plugin bind, refresh, lifecycle update and detach.

Co-authored-by: BTofu <[email protected]>

Co-authored-by: Ayaan Zaidi <[email protected]>
tideclaw-bot pushed a commit that referenced this pull request Nov 6, 2026
…ot initialized' (#175379)

`/codex detach` and a repeated `/codex bind` in a Telegram conversation no longer fail with "Telegram runtime not initialized". The Telegram thread-binding manager keeps the runtime it was created with and persists through it, whichever plugin calls its adapter. Registry checks still apply, so a retired Telegram registry still refuses the write. Core session-binding semantics are unchanged.

Proof: real Telegram (Test Server) driving the bundled Codex plugin through a fresh Gateway. On main d3dc09e the repeated bind and the detach both fail and the binding stays; the candidate binds, detaches and reports no binding. Live gate passed with openai/gpt-5-mini.

Thanks @Satoshi-Kusumoto.

Co-authored-by: Ayaan Zaidi <[email protected]>

(cherry picked from commit 2a30b52)

Co-authored-by: RomneyDa <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: telegram Channel integration: telegram P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: M status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: /codex detach (and re-running /codex bind) fails with "Telegram runtime not initialized" while /session unbind succeeds

2 participants