diff --git a/internal/ui/model.go b/internal/ui/model.go index 84315e8..7f6ddbb 100644 --- a/internal/ui/model.go +++ b/internal/ui/model.go @@ -1634,6 +1634,17 @@ type dashboardGeometry struct { } func (m Model) dashboardLayout() dashboardGeometry { + if m.geometry == nil { + return m.computeDashboardLayout() + } + if m.geometry.baseDashboard == nil { + layout := m.computeDashboardLayout() + m.geometry.baseDashboard = &layout + } + return *m.geometry.baseDashboard +} + +func (m Model) computeDashboardLayout() dashboardGeometry { width := m.width if width == 0 { width = 80 diff --git a/internal/ui/monitor_approval_test.go b/internal/ui/monitor_approval_test.go index b1b8cd6..79ce92d 100644 --- a/internal/ui/monitor_approval_test.go +++ b/internal/ui/monitor_approval_test.go @@ -120,6 +120,7 @@ func TestMonitorApprovalRenderedTargets(t *testing.T) { } g := m.dashboardLayout() shown := m.monitorApprovalControls(g.contentWidth, g.meterHeight) + hit := m.monitorContextHitTester() for i, option := range m.monitorSessionData[0].preview.ApprovalOptions { if option.Kind == "" { continue @@ -135,7 +136,7 @@ func TestMonitorApprovalRenderedTargets(t *testing.T) { found = true x := lipgloss.Width(line[:pos]) for dx := 0; dx < lipgloss.Width(label); dx++ { - if got := m.monitorContextAt(x+dx, y); got != action { + if got := hit(x+dx, y); got != action { t.Fatalf("%s miss at %d,%d got %q\n%s", action, x+dx, y, got, ansi.Strip(out)) } } diff --git a/internal/ui/monitor_context.go b/internal/ui/monitor_context.go index 2fae042..1fc7228 100644 --- a/internal/ui/monitor_context.go +++ b/internal/ui/monitor_context.go @@ -401,7 +401,17 @@ func (m Model) updateMonitorContextKey(key string) (Model, tea.Cmd, bool) { } func (m Model) monitorContextAt(x, y int) string { + return m.monitorContextHitTester()(x, y) +} + +// Reuse geometry only while probing one unchanged screen. Live input starts +// a new pass so a resize, telemetry update or approval expiry cannot reuse it. +func (m Model) monitorContextHitTester() func(x, y int) string { m.geometry = &monitorGeometryCache{} + return m.monitorContextAtInPass +} + +func (m Model) monitorContextAtInPass(x, y int) string { if m.meterView != viewMonitor || m.loading && len(m.snapshot.Meters()) == 0 { return "" } @@ -449,7 +459,8 @@ func (m Model) monitorContextAt(x, y int) string { return hit } } - if hit := monitorButtonAt(m.monitorApprovalButtons(g.contentWidth, g.meterHeight), g.meterHeight, x, y); hit != "" { + approvalButtons := m.geometry.approvalButtons.at(g.contentWidth, g.meterHeight, m.monitorApprovalButtons) + if hit := monitorButtonAt(approvalButtons, g.meterHeight, x, y); hit != "" { return hit } if r, ok := contextActionRect(g.contentWidth, 0, monitorDismissLabel); ok && r.contains(x, y) { diff --git a/internal/ui/monitor_detail_activity.go b/internal/ui/monitor_detail_activity.go index 49d94ce..e4cae86 100644 --- a/internal/ui/monitor_detail_activity.go +++ b/internal/ui/monitor_detail_activity.go @@ -107,6 +107,13 @@ func (m Model) layoutDetailControls(width, height int) detailControlLayout { } func (m Model) layoutDetailControlsBase(width, height int) detailControlLayout { + if m.geometry != nil { + return m.geometry.detailControls.at(width, height, m.computeDetailControlsBase) + } + return m.computeDetailControlsBase(width, height) +} + +func (m Model) computeDetailControlsBase(width, height int) detailControlLayout { if s, ok := m.contextDetailSession(); ok { if buttons := m.profileButtons(width, height, s); len(buttons) > 0 { return detailControlLayout{kind: "profile", rows: buttons[len(buttons)-1].y + 1} diff --git a/internal/ui/monitor_efficiency_test.go b/internal/ui/monitor_efficiency_test.go index 2dd27ff..f6f276e 100644 --- a/internal/ui/monitor_efficiency_test.go +++ b/internal/ui/monitor_efficiency_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + tea "charm.land/bubbletea/v2" "github.com/charmbracelet/x/ansi" "github.com/merefield/codexometer/internal/codex" "github.com/merefield/codexometer/internal/i18n" @@ -70,6 +71,28 @@ func TestMonitorGeometryPassMatchesUncached(t *testing.T) { } } +func TestMonitorHitTestPassDoesNotKeepExpiredApprovalClickable(t *testing.T) { + m := approvalTestModel() + label := m.approvalButtonLabel("accept", false, 0) + x, y := renderedTextStart(t, m, label) + hit := m.monitorContextHitTester() + if hit(x, y) != "decision:0" || hit(x+1, y) != "decision:0" { + t.Fatal("active approval did not cover its rendered button") + } + + // Approval state may change independently of the UI model when the + // app-server consumes a request. Live clicks must start a new pass. + provider := m.fetcher.(*approvalTestClient) + provider.token = "replacement" + if got := m.monitorContextAt(x, y); got == "decision:0" { + t.Fatal("fresh hit test retained the obsolete approval") + } + updated, cmd := m.Update(tea.MouseClickMsg{X: x, Y: y, Button: tea.MouseLeft}) + if updated.(Model).monitorApprovalBusy || cmd != nil || m.geometry != nil { + t.Fatal("live click used stale approval geometry or leaked the read-only cache") + } +} + func TestMonitorEditorConfigurationStableAndResponsive(t *testing.T) { e := newMonitorEditor() e.SetValue(strings.Repeat("wrapped text ", 30)) diff --git a/internal/ui/monitor_prompt_test.go b/internal/ui/monitor_prompt_test.go index f26deae..4d1d341 100644 --- a/internal/ui/monitor_prompt_test.go +++ b/internal/ui/monitor_prompt_test.go @@ -274,14 +274,15 @@ func TestMonitorPromptWrapsAndGrowsUpward(t *testing.T) { if lipgloss.Width(out) > m.width || lipgloss.Height(out) > m.height { t.Fatalf("overflow %v\n%s", size, ansi.Strip(out)) } + hit := m.monitorContextHitTester() for row := y; row < y+rows-1; row++ { for x := 4; x < g.contentWidth; x++ { - if got := m.monitorContextAt(x, g.meterY+row); got != "prompt" { + if got := hit(x, g.meterY+row); got != "prompt" { t.Fatalf("wrapped input miss %v %d,%d: %s", size, x, row, got) } } } - if m.monitorContextAt(4, g.meterY+y+rows-1) != "prompt" { + if hit(4, g.meterY+y+rows-1) != "prompt" { t.Fatal("bottom hint line should focus the composer") } if m.monitorPrompt.input.Value() != value { diff --git a/internal/ui/monitor_summary.go b/internal/ui/monitor_summary.go index 0134698..bb90193 100644 --- a/internal/ui/monitor_summary.go +++ b/internal/ui/monitor_summary.go @@ -398,8 +398,26 @@ func (m Model) renderMonitorAttention(width, rows int, buttons []monitorNavigati // Overview attention fits between the summary and session rows; retain at least // one useful row. The full detail budget additionally protects controls/text. type monitorGeometryCache struct { - areas map[[2]int]monitorGeometry - dashboard *dashboardGeometry + areas map[[2]int]monitorGeometry + baseDashboard *dashboardGeometry + dashboard *dashboardGeometry + detailControls monitorSizedCache[detailControlLayout] + approvalButtons monitorSizedCache[[]monitorApprovalButton] +} + +type monitorSizedCache[T any] map[[2]int]T + +func (c *monitorSizedCache[T]) at(width, height int, compute func(int, int) T) T { + key := [2]int{width, height} + if v, ok := (*c)[key]; ok { + return v + } + v := compute(width, height) + if *c == nil { + *c = make(monitorSizedCache[T]) + } + (*c)[key] = v + return v } func (m Model) monitorArea(width, height int) monitorGeometry {