Repository navigation
Keep Pi hooks and unread updates off UI-critical paths #9289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 17 commits
41a5ac1
b2e2dee
d5303e2
0994ab7
713bd78
92cba43
6fed64d
4ed2cfb
f9184b3
baaa5a1
4d95092
3c4b3b7
cf3dc3c
3c63d53
e2f7aeb
d5d163f
4f18eec
1c212ee
f3d1df0
ea3d89e
c39d076
34ea84d
39cc5b9
de98d6b
942888b
4d65663
6ad517f
2ed483f
a06da1d
67246d3
6a3e3f9
3f26fb4
00d661c
e4bab82
02c4387
bd437b9
05f5322
21e48f2
0c7d27d
480e6fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35274,6 +35274,9 @@ export default CMUXSessionRestore; | |
| print(subcommandUsage("hooks") ?? "Usage: cmux hooks <setup|uninstall|agent>") | ||
| return true | ||
| } | ||
| if def.name == "pi", action == "session-start" { | ||
| refreshManagedPiExtensionIfNeeded(def) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: The added session-start refresh performs synchronous disk I/O — a full file read on every call and a full atomic rewrite whenever stale — on the Pi session-start hook dispatch path. It runs in the cmux CLI subprocess so it doesn't block the app UI directly, but it does add a blocking disk operation to a lifecycle path this PR is meant to keep lightweight, and the rewrite only takes effect for the next session (the current one already loaded the old extension). Consider deferring the write off the synchronous dispatch path (e.g., a fire-and-forget refresh) and short-circuiting on a cheap stat/size check before reading the whole file, since the per-session cost otherwise isn't free on upgrade sessions. Prompt for AI agents
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The refresh now skips a contended lock, and Pi's handler returns before the serialized CLI child completes. The remaining steady-state file verification is bounded to the generated managed extension; an atomic rewrite occurs only after an upgrade. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: The new pi session-start refresh advances the extension if the existing file differs, but every failure (unreadable/stale-extension path, permission/disk write errors) is swallowed silently with no log or telemetry in Prompt for AI agents
lawrencecchen marked this conversation as resolved.
Resolved
|
||
| } | ||
| let actionArgs = Array(rest.dropFirst()) | ||
| switch action { | ||
| case "inject-args" where def.name == "codex": | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,228 @@ | ||
| public import Foundation | ||
| import Observation | ||
|
|
||
| /// The unread values rendered for one workspace row. | ||
| public struct SidebarWorkspaceUnreadSummary: Equatable, Sendable { | ||
| /// The workspace's displayed unread count. | ||
| public var unreadCount: Int | ||
| /// The trimmed body or title of the latest notification, when present. | ||
| public var latestNotificationText: String? | ||
| /// Whether the workspace has a latest notification that can be cleared. | ||
| public var hasLatestNotification: Bool | ||
|
|
||
| /// Creates one workspace unread summary. | ||
| public init( | ||
| unreadCount: Int, | ||
| latestNotificationText: String?, | ||
| hasLatestNotification: Bool = false | ||
| ) { | ||
| self.unreadCount = unreadCount | ||
| self.latestNotificationText = latestNotificationText | ||
| self.hasLatestNotification = hasLatestNotification | ||
| } | ||
| } | ||
|
|
||
| /// A workspace and optional surface pair in the unread set. | ||
| public struct SidebarSurfaceUnreadKey: Hashable, Sendable { | ||
| /// The owning workspace identifier. | ||
| public var workspaceId: UUID | ||
| /// The surface identifier, or `nil` for a workspace-wide notification. | ||
| public var surfaceId: UUID? | ||
|
|
||
| /// Creates one workspace or surface unread key. | ||
| public init(workspaceId: UUID, surfaceId: UUID?) { | ||
| self.workspaceId = workspaceId | ||
| self.surfaceId = surfaceId | ||
| } | ||
| } | ||
|
|
||
| /// One atomic unread-state publication shared by all presentation surfaces. | ||
| public struct SidebarUnreadSnapshot: Equatable, Sendable { | ||
| /// Total unread count rendered by global badges. | ||
| public var totalUnreadCount: Int | ||
| /// Per-workspace row summaries, omitting default empty summaries. | ||
| public var summaryByWorkspaceId: [UUID: SidebarWorkspaceUnreadSummary] | ||
| /// Workspace and surface pairs with unread notifications. | ||
| public var unreadSurfaceKeys: Set<SidebarSurfaceUnreadKey> | ||
| /// Focused surfaces whose read indicator remains visible. | ||
| public var focusedReadIndicatorByWorkspaceId: [UUID: UUID] | ||
| /// Workspaces explicitly marked unread by the user. | ||
| public var manualUnreadWorkspaceIds: Set<UUID> | ||
|
|
||
| /// Creates one complete unread snapshot. | ||
| public init( | ||
| totalUnreadCount: Int = 0, | ||
| summaryByWorkspaceId: [UUID: SidebarWorkspaceUnreadSummary] = [:], | ||
| unreadSurfaceKeys: Set<SidebarSurfaceUnreadKey> = [], | ||
| focusedReadIndicatorByWorkspaceId: [UUID: UUID] = [:], | ||
| manualUnreadWorkspaceIds: Set<UUID> = [] | ||
| ) { | ||
| self.totalUnreadCount = totalUnreadCount | ||
| self.summaryByWorkspaceId = summaryByWorkspaceId | ||
| self.unreadSurfaceKeys = unreadSurfaceKeys | ||
| self.focusedReadIndicatorByWorkspaceId = focusedReadIndicatorByWorkspaceId | ||
| self.manualUnreadWorkspaceIds = manualUnreadWorkspaceIds | ||
| } | ||
|
|
||
| /// Returns the workspace summary, or an empty summary when absent. | ||
| public func summary(forWorkspaceId id: UUID) -> SidebarWorkspaceUnreadSummary { | ||
| summaryByWorkspaceId[id] ?? SidebarWorkspaceUnreadSummary( | ||
| unreadCount: 0, | ||
| latestNotificationText: nil | ||
| ) | ||
| } | ||
|
|
||
| /// Returns the workspace's displayed unread count. | ||
| public func unreadCount(forWorkspaceId id: UUID) -> Int { | ||
| summary(forWorkspaceId: id).unreadCount | ||
| } | ||
|
|
||
| /// Returns the workspace's latest notification text. | ||
| public func latestNotificationText(forWorkspaceId id: UUID) -> String? { | ||
| summary(forWorkspaceId: id).latestNotificationText | ||
| } | ||
|
|
||
| /// Returns whether the workspace has any displayed unread state. | ||
| public func workspaceIsUnread(forWorkspaceId id: UUID) -> Bool { | ||
| unreadCount(forWorkspaceId: id) > 0 | ||
| } | ||
|
|
||
| /// Returns whether the workspace was explicitly marked unread. | ||
| public func hasManualUnread(forWorkspaceId id: UUID) -> Bool { | ||
| manualUnreadWorkspaceIds.contains(id) | ||
| } | ||
|
|
||
| /// Returns whether the workspace or surface has an unread notification. | ||
| public func hasUnreadNotification(forWorkspaceId id: UUID, surfaceId: UUID?) -> Bool { | ||
| unreadSurfaceKeys.contains(SidebarSurfaceUnreadKey(workspaceId: id, surfaceId: surfaceId)) | ||
| } | ||
|
|
||
| /// Returns whether the surface should render its notification indicator. | ||
| public func hasVisibleNotificationIndicator(forWorkspaceId id: UUID, surfaceId: UUID?) -> Bool { | ||
| hasUnreadNotification(forWorkspaceId: id, surfaceId: surfaceId) || | ||
| (focusedReadIndicatorByWorkspaceId[id].map { $0 == surfaceId } ?? false) | ||
| } | ||
|
|
||
| /// Returns whether any supplied workspace can be marked read. | ||
| public func canMarkWorkspaceRead(forWorkspaceIds ids: [UUID]) -> Bool { | ||
| ids.contains { workspaceIsUnread(forWorkspaceId: $0) } | ||
|
lawrencecchen marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| /// Returns whether any supplied workspace can be marked unread. | ||
| public func canMarkWorkspaceUnread(forWorkspaceIds ids: [UUID]) -> Bool { | ||
| ids.contains { !workspaceIsUnread(forWorkspaceId: $0) } | ||
| } | ||
| } | ||
|
|
||
| /// Main-actor unread source of truth for leaf UI projections. | ||
| /// | ||
| /// Mutations publish one equality-guarded snapshot. SwiftUI tracks ``snapshot`` | ||
| /// through Observation, while AppKit consumers use ``snapshotChanges()`` so the | ||
| /// app does not add Combine state or broad root-view subscriptions. | ||
| @MainActor | ||
| @Observable | ||
| public final class SidebarUnreadModel { | ||
| /// The latest complete unread state. | ||
| public private(set) var snapshot = SidebarUnreadSnapshot() | ||
|
|
||
| /// Total unread count rendered by global badges. | ||
| public var totalUnreadCount: Int { snapshot.totalUnreadCount } | ||
| /// Current per-workspace unread summaries. | ||
| public var summaryByWorkspaceId: [UUID: SidebarWorkspaceUnreadSummary] { | ||
| snapshot.summaryByWorkspaceId | ||
| } | ||
| /// Current workspace and surface unread keys. | ||
| public var unreadSurfaceKeys: Set<SidebarSurfaceUnreadKey> { snapshot.unreadSurfaceKeys } | ||
| /// Current focused read-indicator surfaces. | ||
| public var focusedReadIndicatorByWorkspaceId: [UUID: UUID] { | ||
| snapshot.focusedReadIndicatorByWorkspaceId | ||
| } | ||
| /// Workspaces explicitly marked unread. | ||
| public var manualUnreadWorkspaceIds: Set<UUID> { snapshot.manualUnreadWorkspaceIds } | ||
|
|
||
| @ObservationIgnored | ||
| private var snapshotObservers: [UUID: AsyncStream<SidebarUnreadSnapshot>.Continuation] = [:] | ||
|
|
||
| /// Creates an empty unread model. | ||
| public init() {} | ||
|
|
||
| /// Returns a stream of changed snapshots, buffering only the newest value. | ||
| public func snapshotChanges() -> AsyncStream<SidebarUnreadSnapshot> { | ||
| AsyncStream(bufferingPolicy: .bufferingNewest(1)) { continuation in | ||
| let id = UUID() | ||
| snapshotObservers[id] = continuation | ||
|
lawrencecchen marked this conversation as resolved.
Resolved
|
||
| continuation.onTermination = { [weak self] _ in | ||
| Task { @MainActor in | ||
| self?.snapshotObservers[id] = nil | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Atomically applies one complete unread state and publishes only changes. | ||
| public func apply( | ||
| totalUnreadCount: Int, | ||
| summaries: [UUID: SidebarWorkspaceUnreadSummary], | ||
| unreadSurfaceKeys: Set<SidebarSurfaceUnreadKey>, | ||
| focusedReadIndicatorByWorkspaceId: [UUID: UUID], | ||
| manualUnreadWorkspaceIds: Set<UUID> | ||
| ) { | ||
| let next = SidebarUnreadSnapshot( | ||
| totalUnreadCount: totalUnreadCount, | ||
| summaryByWorkspaceId: summaries, | ||
| unreadSurfaceKeys: unreadSurfaceKeys, | ||
| focusedReadIndicatorByWorkspaceId: focusedReadIndicatorByWorkspaceId, | ||
| manualUnreadWorkspaceIds: manualUnreadWorkspaceIds | ||
| ) | ||
| guard snapshot != next else { return } | ||
| snapshot = next | ||
| for observer in snapshotObservers.values { | ||
| observer.yield(next) | ||
| } | ||
| } | ||
|
|
||
| /// Returns the summary for one workspace. | ||
| public func summary(forWorkspaceId id: UUID) -> SidebarWorkspaceUnreadSummary { | ||
| snapshot.summary(forWorkspaceId: id) | ||
| } | ||
|
|
||
| /// Returns the unread count for one workspace. | ||
| public func unreadCount(forWorkspaceId id: UUID) -> Int { | ||
| snapshot.unreadCount(forWorkspaceId: id) | ||
| } | ||
|
|
||
| /// Returns the latest notification text for one workspace. | ||
| public func latestNotificationText(forWorkspaceId id: UUID) -> String? { | ||
| snapshot.latestNotificationText(forWorkspaceId: id) | ||
| } | ||
|
|
||
| /// Returns whether one workspace is unread. | ||
| public func workspaceIsUnread(forWorkspaceId id: UUID) -> Bool { | ||
| snapshot.workspaceIsUnread(forWorkspaceId: id) | ||
| } | ||
|
|
||
| /// Returns whether one workspace was manually marked unread. | ||
| public func hasManualUnread(forWorkspaceId id: UUID) -> Bool { | ||
| snapshot.hasManualUnread(forWorkspaceId: id) | ||
| } | ||
|
|
||
| /// Returns whether a workspace or surface has an unread notification. | ||
| public func hasUnreadNotification(forWorkspaceId id: UUID, surfaceId: UUID?) -> Bool { | ||
| snapshot.hasUnreadNotification(forWorkspaceId: id, surfaceId: surfaceId) | ||
| } | ||
|
|
||
| /// Returns whether a surface should show its notification indicator. | ||
| public func hasVisibleNotificationIndicator(forWorkspaceId id: UUID, surfaceId: UUID?) -> Bool { | ||
| snapshot.hasVisibleNotificationIndicator(forWorkspaceId: id, surfaceId: surfaceId) | ||
| } | ||
|
|
||
| /// Returns whether any supplied workspace can be marked read. | ||
| public func canMarkWorkspaceRead(forWorkspaceIds ids: [UUID]) -> Bool { | ||
| snapshot.canMarkWorkspaceRead(forWorkspaceIds: ids) | ||
| } | ||
|
|
||
| /// Returns whether any supplied workspace can be marked unread. | ||
| public func canMarkWorkspaceUnread(forWorkspaceIds ids: [UUID]) -> Bool { | ||
| snapshot.canMarkWorkspaceUnread(forWorkspaceIds: ids) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.