refactor(app): type event fan-out with sealed Event union (#101) - #103
Conversation
The app layer described its own events with Bubble Tea's untyped tea.Msg, forcing internal/app to speak the TUI's message vocabulary and letting any value flow through the event-producing paths unchecked. Introduce a sealed app.Event interface (unexported isAppEvent marker) implemented by every event type in events.go, and retype all app-internal fan-out — RunOnceWithDisplay(AndFiles), executeStep, executeBatch, runQueueBatch, subscribeSDKEvents, handleTurnEnd, recordStepUsage and the sendEvent helper — from func(tea.Msg) to func(Event). The CLI handler's Handle now takes app.Event, dropping its bubbletea import entirely. Only the concrete transport (prog.Send) adapts Event at the boundary, so the event-producing code is now transport-agnostic. Rename the UI-transport escape hatch AppController.SendEvent(tea.Msg) to SendUIMessage(tea.Msg) so it is clearly distinct from app events: it re-injects UI-internal messages (async goroutine results) into the Update loop and stays opaque to the app layer, which never interprets it. Response-channel events (password/prompt/overlay/new-session) keep their channels for now; a request-id correlation scheme for a real out-of-process transport is deferred until such a transport exists. Refs #101
|
Connected to Huly®: KIT-104 |
📝 WalkthroughWalkthroughThe app now uses a sealed ChangesEvent and UI transport separation
Estimated code review effort: 3 (Moderate) | ~20 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
|
@CodeRabbit review |
✅ Action performedReview finished.
|
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 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 `@internal/app/events.go`:
- Around line 5-24: Revise the Event interface documentation to remove the claim
that type switches over Event are compiler-checked for exhaustiveness. Keep the
sealed-union and app-owned-type explanation, but accurately state that Go’s
unexported marker prevents external implementations without implying unhandled
variants produce compile errors.
In `@internal/ui/model.go`:
- Line 3943: Update the concurrency rationale comment immediately above the
extension command result goroutine to reference SendUIMessage instead of the
stale SendEvent API, matching the call in the goroutine.
🪄 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: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: bbac234d-1115-417b-8687-53f08c07becf
📒 Files selected for processing (6)
internal/app/app.gointernal/app/app_test.gointernal/app/events.gointernal/ui/event_handler.gointernal/ui/model.gointernal/ui/model_test.go
- events.go: drop the inaccurate claim that Go compiler-checks type-switch exhaustiveness over the sealed Event interface; state that the unexported marker only closes the union to external implementations and that downstream switches should keep a default case. - model.go / model_test.go: update stale SendEvent references in the extension-command goroutine rationale and the session-switch test comment to the renamed SendUIMessage. Refs #101
|
@CodeRabbit review |
✅ Action performedReview finished.
|
Summary
Step (a) of the #101 decoupling: give the app layer a typed vocabulary for the events it emits toward a display, so
internal/appno longer has to speak Bubble Tea's untypedtea.Msgto describe its own events.This is behaviour-preserving and the second of the sequenced PRs for #101 (after #102). It does not close the issue.
What changed
app.Eventunion (internal/app/events.go): an interface with an unexportedisAppEvent()marker, implemented by every event type in the file. The set is closed — only app-owned types can satisfy it, and a new event type is a compile error until it is added to the marker block.func(tea.Msg)tofunc(Event)—RunOnceWithDisplay/RunOnceWithDisplayAndFiles,executeStep,executeBatch,runQueueBatch,subscribeSDKEvents,handleTurnEnd,recordStepUsage, and thesendEventhelper. Only the concrete transport (prog.Send) adaptsEventat the boundary now.internal/ui/event_handler.go):Handlenow takesapp.Event, which lets it drop itscharm.land/bubbletea/v2import entirely.AppController.SendEvent(tea.Msg)→SendUIMessage(tea.Msg)(and theAppmethod to match). This method is not an app event — it re-injects UI-internal messages (async goroutine results likebeforeForkResultMsg,extensionCmdResultMsg) into the Bubble TeaUpdateloop and is opaque to the app layer. Renaming disambiguates it from the typedEventfan-out.Why
SendEvent(tea.Msg)and thefunc(tea.Msg)fan-out meantinternal/appdepended on the TUI's message type to model its own events, and any value could flow through those paths unchecked. A sealedEventunion makes the event surface transport-agnostic (a future non-Bubble Tea transport only adapts atprog.Send), self-documenting, and type-checked.Scope / deferred
PasswordPromptEvent,PromptRequestEvent,OverlayRequestEvent,NewSessionRequestEvent) keep their channels. Converting them to a request-id correlation scheme (and error fields to strings) only matters for a real out-of-process boundary, which does not exist yet — deferred to avoid speculative churn. Noted in theEventdoc comment.*tea.Programfield andSendUIMessage'stea.Msgtransport remain: fully removing Bubble Tea frominternal/appis out of scope for this step.Testing
go build ./...,go vet ./...— clean (the pre-existingexamples/extensions"function main is undeclared" error is unrelated and present onmaster).go test -race ./...— all pass.gofmt -lclean;golangci-lint run— 0 issues./helprenders, empty stderr — event round-trip through real Bubble Tea confirmed.Refs #101
Summary by CodeRabbit
SendUIMessagefor UI-only message reinjection.