Skip to content

refactor(telegram): merge dispatch controllers into one turn module - #122091

Merged
obviyus merged 1 commit into
mainfrom
refactor/telegram-turn-dispatch
Aug 11, 2026
Merged

obviyus merged 1 commit into
mainfrom
refactor/telegram-turn-dispatch

Conversation

@obviyus

@obviyus obviyus commented Aug 11, 2026 •

Copy link
Copy Markdown
Collaborator

What Problem This Solves

The four Telegram dispatch "controllers" (bot-message-dispatch-{draft,progress,delivery,reply}) were partitions of one closure pretending to be modules: ~75 parameter slots across five factory signatures, ~12 config fields copied into multiple constructors, a shared mutable TelegramDispatchTurnState, and seven post-construction back-edge setters whose only reason to exist was the module boundary. Construction order was load-bearing (draft's no-op defaults silently absorbed misordering) with nothing asserting it. dispatchTelegramMessage was already the only production entry point; the internal shape just didn't match that fact.

Why This Change Was Made

One turn record now carries the once-resolved config and all turn state; the four files remain as implementation of that single module (plain functions over the turn record), with the factories, option bags, and every setter deleted. Three latent hazards found during the mapping are fixed in the same change:

  • H1: the generation fence was dead code — dispatchGeneration was introduced as a constant 0 (verified via history), so the buffered-final generation guard could never reject; the whole fence is deleted.
  • H2: runTelegramDispatchTurn unconditionally overwrote state.queuedFinal, clobbering the reply-side write for suppressed exec-approval prompts; now ||=, with a regression test proven to fail on the pre-fix code (one unwanted fallback delivery).
  • H3: resolveCollapseSummaryLine mutated summaryDelivered as a side effect of a resolver; split into a pure resolver plus explicit marks at both consumption sites.

User Impact

None intended: behavior-neutral restructure. The acceptance gate makes that checkable — all 17 bot-message-dispatch* test files and the 745-line test harness are byte-identical to main and pass unchanged (247/247). H2 is the one deliberate behavior fix (suppressed exec-approval turns no longer trigger a spurious "no response" fallback).

Evidence

  • Dispatch suite + bot.test.ts: 18 files, 387 tests green on the exact head; dispatch tests/harness zero-diff vs main.
  • bot.test.ts's three reply-controller tests rewritten through the dispatchTelegramMessage front door (the deleted factory was their only consumer).
  • H2 negative control: reverting ||= to assignment fails exactly the new regression.
  • pnpm tsgo:extensions, pnpm check:test-types, oxlint (all touched files incl. tests), git diff --check: clean.
  • Production LOC net +93 (+2076/−1983); tests +55. Named tradeoff, accepted deliberately: ~100 of those lines are the hand-written turn contract — four explicit state-slice types (TelegramDraftStateSlice 16 members, progress 11, delivery 8, reply 4) in bot-message-dispatch.types.ts, now a leaf module with zero imports from implementations (repo madge leaf-contract rule). This replaces ReturnType<typeof create*> derived types — the exact interface-is-whatever-the-implementation-returns anti-pattern this refactor exists to kill. What got deleted is interface, not lines: 4 factories, ~75 parameter slots, 7 back-edge setters, the shared mutable state bag, and the dead generation fence.
  • Rebased over refactor(telegram): centralize text delivery #117327; bot-message-dispatch-progress.ts keeps main's centralized text-delivery behavior with the turn-record structure re-applied.

@clawsweeper

clawsweeper Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@obviyus
obviyus marked this pull request as ready for review August 11, 2026 15:10
@openclaw-barnacle openclaw-barnacle Bot added channel: telegram Channel integration: telegram size: XL maintainer Maintainer-authored PR labels Aug 11, 2026
@obviyus
obviyus force-pushed the refactor/telegram-turn-dispatch branch from 438e656 to cc76476 Compare August 11, 2026 15:25
@obviyus

obviyus commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator Author

Telegram E2E proof (exact head cc7647618afdd06552b1cf9a01e662bcf83e0762)

Live drive via the QA userbot lane (TDLib user observer, completions-fixture mock provider, ephemeral gateway from this head; DM; streaming.mode: progress with commentary). Recorded timeline of the OPENCLAW_E2E_DRAFTPROOF scenario through the merged turn module:

t event msg content
2.4s message 872 progress window: Working + 💬 commentary line
2.8s edit 872 + 🛠️ Exec tool line (same-id in-place growth)
4.9s/7.6s typing ×2 — chat actions during delivery
7.6s message 873 separate persistent final: OPENCLAW_E2E_DRAFTPROOF
8.6s edit 872 window collapses in place to activity receipt 💬 1 note · 🛠️ 1 tool call · ⏱️ 7s

No delete events — the collapse is the required finalizeToPreview edit, not delete-and-repost. This exercises the merged draft→progress→delivery→reply machinery end to end: preview creation, progress rotation/interleaving, final delivery on a fresh message, and receipt finalization — all behavior-identical to main's recorded contract (the 17 dispatch test files pass byte-unchanged).

Recorded gap, per the review's next-step: the suppressed exec-approval path (H2) has no live mock fixture, so it is pinned by the front-door regression in bot.test.ts ("suppressed exec-approval turn still counts as queuedFinal"), proven to fail on the pre-fix code (one unwanted fallback delivery). Live-driving that path would need an approval-capable fixture — happy to add one as a follow-up if wanted, but not inventing it inside this PR.

Artifacts (events NDJSON with raw TDLib updates, summary, scoped gateway log) retained locally.

Unify draft, progress, delivery, and reply ownership around one dispatch turn record.

Remove construction back-edges and fix generation, queued-final, and collapse-summary hazards.
@obviyus
obviyus force-pushed the refactor/telegram-turn-dispatch branch from cc76476 to d2307b4 Compare August 11, 2026 15:35
@obviyus
obviyus enabled auto-merge (squash) August 11, 2026 15:38
@obviyus
obviyus merged commit e31a6e2 into main Aug 11, 2026
112 checks passed
@obviyus
obviyus deleted the refactor/telegram-turn-dispatch branch August 11, 2026 15:45
obviyus added a commit that referenced this pull request Aug 11, 2026
The command-menu locale ledger reads state.openKeyedStore during
hydration; the fixture's partial runtime stub lacked it, so the
degrade warning broke both claim-watchdog cancellation tests on main.
Provision it like the dispatch harness does. Surfaced by #122091's
follow-up work; suite runs locally only (see #122029 decision).

Co-Authored-By: Claude Fable 5 <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 12, 2026
…penclaw#122091)

The four Telegram dispatch controllers were partitions of one closure: ~75 factory parameter slots, 7 post-construction back-edge setters, a shared mutable state bag, and load-bearing construction order. One turn record now carries the once-resolved config and all state; the four files remain as implementation with a hand-written leaf type contract (four state-slice types). Rides along: dead generation fence deleted (constant-0 from birth), queuedFinal ||= fix with regression (suppressed exec-approval turns no longer trigger a spurious fallback), collapse resolver/mutator split. Dispatch tests and harness byte-identical to main; live E2E lifecycle proof on the PR.

Co-authored-by: Ayaan Zaidi <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 12, 2026
The command-menu locale ledger reads state.openKeyedStore during
hydration; the fixture's partial runtime stub lacked it, so the
degrade warning broke both claim-watchdog cancellation tests on main.
Provision it like the dispatch harness does. Surfaced by openclaw#122091's
follow-up work; suite runs locally only (see openclaw#122029 decision).

Co-Authored-By: Claude Fable 5 <[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 maintainer Maintainer-authored PR size: XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant