From dbb98cd8dbd70dd92b8a541653e64c45b18348fa Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 18 Mar 2026 09:24:22 +0200 Subject: refactor: split TraceRuntimeBindings into RuntimePublisher and RuntimeState (task 427/ISP) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TraceRuntimeBindings mixed 4 setter methods (injecting data into TUI) with 4 getter methods (reading persistent TUI-owned state), violating ISP. Split into two focused interfaces: - RuntimePublisher: SetDashboardSnapshotSource, SetEventStreamSource, SetLiveTrie, SetProbeManager — the write/inject side - RuntimeState: StreamBuffer, Recorder, StreamSequencer, FilterEpoch — the read/persistent-state side TraceRuntimeBindings now embeds both, preserving all existing call sites. RuntimePublisherFromContext() added so callers that only inject data do not see the getter surface. Three such callers are narrowed: setupBPFModule, tuiTestFlamesStarter, tuiTestLiveFlamesStarter. Co-Authored-By: Claude Sonnet 4.6 (1M context) --- internal/tui/tui.go | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) (limited to 'internal/tui') diff --git a/internal/tui/tui.go b/internal/tui/tui.go index 432fc14..252e722 100644 --- a/internal/tui/tui.go +++ b/internal/tui/tui.go @@ -58,19 +58,31 @@ type ProbeManager interface { ActiveCount() (int, int) } -// TraceRuntimeBindings allows a trace starter to publish runtime dependencies -// (snapshot source, stream source, probe manager) into the active TUI model. -type TraceRuntimeBindings interface { +// RuntimePublisher is the write side of the TUI runtime contract. +// A trace starter calls these methods to inject live data into the active TUI. +type RuntimePublisher interface { SetDashboardSnapshotSource(source SnapshotSource) SetEventStreamSource(source eventstream.Source) SetLiveTrie(liveTrie flamegraphtui.LiveTrieSource) SetProbeManager(manager ProbeManager) +} + +// RuntimeState is the read side of the TUI runtime contract. +// A trace starter calls these methods to obtain persistent state owned by the TUI. +type RuntimeState interface { StreamBuffer() eventstream.Source Recorder() *parquet.Recorder StreamSequencer() *eventstream.Sequencer FilterEpoch() uint64 } +// TraceRuntimeBindings composes RuntimePublisher and RuntimeState so a trace +// starter can both inject live data and read persistent TUI-owned state. +type TraceRuntimeBindings interface { + RuntimePublisher + RuntimeState +} + type runtimeBindingsContextKey struct{} type traceFiltersContextKey struct{} @@ -200,8 +212,9 @@ func (r *runtimeBindings) resetDashboardSnapshotSource() *statsengine.Snapshot { return nil } -// RuntimeBindingsFromContext returns model-scoped trace bindings when the -// context was created by the TUI. +// RuntimeBindingsFromContext returns the full TraceRuntimeBindings when the +// context was created by the TUI. Use RuntimePublisherFromContext when only +// write access is needed. func RuntimeBindingsFromContext(ctx context.Context) (TraceRuntimeBindings, bool) { bindings, ok := ctx.Value(runtimeBindingsContextKey{}).(TraceRuntimeBindings) if !ok || bindings == nil { @@ -210,6 +223,17 @@ func RuntimeBindingsFromContext(ctx context.Context) (TraceRuntimeBindings, bool return bindings, true } +// RuntimePublisherFromContext returns only the RuntimePublisher side of the +// TUI bindings. Use this when the caller only injects data and does not need +// to read persistent TUI state. +func RuntimePublisherFromContext(ctx context.Context) (RuntimePublisher, bool) { + bindings, ok := ctx.Value(runtimeBindingsContextKey{}).(RuntimePublisher) + if !ok || bindings == nil { + return nil, false + } + return bindings, true +} + // ContextWithRuntimeBindings stores trace runtime bindings on the context. func ContextWithRuntimeBindings(ctx context.Context, bindings TraceRuntimeBindings) context.Context { return context.WithValue(ctx, runtimeBindingsContextKey{}, bindings) -- cgit v1.2.3