diff --git a/Tests/StackNudgePanelCoreTests/CompanionTests.swift b/Tests/StackNudgePanelCoreTests/CompanionTests.swift index 05f234c..96a6778 100644 --- a/Tests/StackNudgePanelCoreTests/CompanionTests.swift +++ b/Tests/StackNudgePanelCoreTests/CompanionTests.swift @@ -41,6 +41,15 @@ final class CompanionModeTests: XCTestCase { s.permissionWaiting = false XCTAssertEqual(CompanionMode.mode(for: s), .thinking) } + + func testAnUnreadAlertEventIsTheAlert() { + var s = CompanionSignals(permissionWaiting: true, eventAlerting: true, eventWaiting: true, sessionBusy: true) + XCTAssertEqual(CompanionMode.mode(for: s), .alert) + s.eventAlerting = false + XCTAssertEqual(CompanionMode.mode(for: s), .pending) + s = CompanionSignals(muted: true, eventAlerting: true) + XCTAssertEqual(CompanionMode.mode(for: s), .muted, "mute still outranks an alert") + } } final class CompanionPlacementTests: XCTestCase { diff --git a/Tests/StackNudgePanelCoreTests/GitHubFeedTests.swift b/Tests/StackNudgePanelCoreTests/GitHubFeedTests.swift index fa2fc6d..5df45e2 100644 --- a/Tests/StackNudgePanelCoreTests/GitHubFeedTests.swift +++ b/Tests/StackNudgePanelCoreTests/GitHubFeedTests.swift @@ -163,6 +163,52 @@ final class GitHubFeedTests: XCTestCase { XCTAssertEqual(recovered.map(\.title), ["CI passing again on app#1"], "no separate checks-passed row") } + func testAFixPushedBetweenFailureAndSuccessStillReplacesTheAlert() { + let seeded = GitHubFeed.diff(snapshot(mine: [pr(ci: "PENDING")]), previous: nil, login: login, now: t0) + let failed = GitHubFeed.diff(snapshot(mine: [pr(ci: "FAILURE")]), previous: seeded.state, login: login, now: later(3)) + let pending = GitHubFeed.diff(snapshot(mine: [pr(ci: "PENDING", sha: "a2")]), previous: failed.state, login: login, now: later(6)) + XCTAssertTrue(pending.events.isEmpty) + let path = NSTemporaryDirectory() + "github-feed-\(UUID().uuidString).json" + defer { try? FileManager.default.removeItem(atPath: path) } + GitHubFeedPoller.saveState(pending.state, path: path) + let relaunched = GitHubFeedPoller.loadState(path: path) + let green = GitHubFeed.diff(snapshot(mine: [pr(ci: "SUCCESS", sha: "a2")]), previous: relaunched, login: login, now: later(9)) + XCTAssertEqual(green.events.map(\.title), ["CI passing again on app#1"]) + XCTAssertEqual(green.events.first?.key, "ci:acme/app#1") + XCTAssertNotEqual(green.events.first?.severity, .alert) + let after = GitHubFeed.diff(snapshot(mine: [pr(ci: "SUCCESS", sha: "a2")]), previous: green.state, login: login, now: later(12)) + XCTAssertTrue(after.events.isEmpty) + } + + func testResolvingAConflictReplacesTheAlert() { + let got = events(from: snapshot(mine: [pr(mergeable: "CONFLICTING")]), to: snapshot(mine: [pr(mergeable: "MERGEABLE")])) + XCTAssertEqual(got.map(\.title), ["Conflict resolved on app#1"]) + XCTAssertEqual(got.first?.key, "conflict:acme/app#1") + XCTAssertNotEqual(got.first?.severity, .alert) + } + + func testTheCompanionStopsAlertingOnceTheFailureIsReplaced() { + let store = EventStore() + let seeded = GitHubFeed.diff(snapshot(mine: [pr(ci: "PENDING")]), previous: nil, login: login, now: t0) + let failed = GitHubFeed.diff(snapshot(mine: [pr(ci: "FAILURE", mergeable: "CONFLICTING")]), previous: seeded.state, login: login, now: later(3)) + failed.events.forEach(store.append) + let alerting = { store.events.contains { $0.isUnread && $0.severity == .alert } } + XCTAssertTrue(alerting()) + XCTAssertEqual(CompanionMode.mode(for: CompanionSignals(eventAlerting: alerting(), eventWaiting: true)), .alert) + let pending = GitHubFeed.diff(snapshot(mine: [pr(ci: "PENDING", sha: "a2", mergeable: "CONFLICTING")]), previous: failed.state, login: login, now: later(6)) + let green = GitHubFeed.diff(snapshot(mine: [pr(ci: "SUCCESS", sha: "a2")]), previous: pending.state, login: login, now: later(9)) + green.events.forEach(store.append) + XCTAssertFalse(alerting()) + XCTAssertNotEqual(CompanionMode.mode(for: CompanionSignals(eventAlerting: alerting(), eventWaiting: true)), .alert) + } + + func testStateFromBeforeCIFailingWasTrackedStillDecodes() throws { + let old = #"{"prs":{"acme/app#1":{"ci":"FAILURE","ready":false}},"seen":{}}"# + let state = try JSONDecoder().decode(GitHubFeed.State.self, from: Data(old.utf8)) + let green = GitHubFeed.diff(snapshot(mine: [pr(ci: "SUCCESS")]), previous: state, login: login, now: t0) + XCTAssertEqual(green.events.map(\.title), ["CI passing again on app#1"]) + } + func testChecksPassedOncePerCommit() { let seeded = GitHubFeed.diff(snapshot(mine: [pr(ci: "PENDING")]), previous: nil, login: login, now: t0) let green = GitHubFeed.diff(snapshot(mine: [pr(ci: "SUCCESS")]), previous: seeded.state, login: login, now: later(3)) diff --git a/docs/notifications.md b/docs/notifications.md index df09b1a..74cbe0e 100644 --- a/docs/notifications.md +++ b/docs/notifications.md @@ -60,7 +60,7 @@ printf '%s\n' '{"agent":"deploy-bot","event":"other","title":"Deployed","message |-------|---------| | `source` | `script`, `github` or `calendar`. Anything else, or `agent`, makes it an ordinary agent event. `extension` is read as `script`: only a service extension's own process posts as one. A feed event is never a permission prompt: it ignores `event`, `fifo_path`, `hook_pid` and `has_action_button`. | | `tag` | Free-form label that picks the row's icon: `review`, `review-request`, `ci`, `ready`, `deploy`, `pipeline` or `calendar`. Any other tag gets the source's icon. Searchable in history. | -| `severity` | `info` (default), `action` or `alert`. Colours the icon grey, orange or red. | +| `severity` | `info` (default), `action` or `alert`. Colours the icon grey, orange or red. `alert` also puts the companion in its alert pose until the event is read. | | `url` | Opened by `⏎` or `O` (the primary action reads **Open**). Only `http` and `https` links are kept. Without one, `⏎` just dismisses the row. | | `repo`, `pr` | Shown in the row's chip, for example `GitHub · api#9`. | | `key` | An event with the same `key` replaces the one already queued, so a build that fails and then passes is one row. | diff --git a/docs/panel.md b/docs/panel.md index fa9d8ec..7170996 100644 --- a/docs/panel.md +++ b/docs/panel.md @@ -273,7 +273,7 @@ The companion is a small animated character that floats on your desktop and take - **Drag** it anywhere; it stays where you drop it, and comes back on screen if a display it was on goes away. - **Right-click** for the menu, including a **Companion** submenu with its size and **Reset position**, which puts it back where a fresh install starts it. -Its pose follows what stack-nudge knows: thinking while an agent is busy, watching while agent sessions are live, waiting when an event or a permission prompt is queued, alert when a quota is past your alert threshold, muted while notifications are muted, and asleep after five quiet minutes. The waiting pose is a wave: it means something wants you to act, and a finished agent turn is one of those, so it still waves. A new finished turn also gets a single small nod on arrival, when the character is otherwise calm (idle or asleep), not muted, and Reduce Motion is off. A count badge at its corner shows how many events are waiting, the Events tab count less snoozed events (a notification stops counting once read), grey while muted. It animates on the render server and pauses while it is covered, the display sleeps or the screen is locked, so it stays near 0% CPU. With Reduce Motion on, it holds still. +Its pose follows what stack-nudge knows: thinking while an agent is busy, watching while agent sessions are live, waiting when an event or a permission prompt is queued, alert when a quota is past your alert threshold or an unread event has `severity: "alert"` (CI failing, a merge conflict, a meeting about to start, or anything a script or extension sends that way; a later event under the same key, like CI passing again, clears it), muted while notifications are muted, and asleep after five quiet minutes. The waiting pose is a wave: it means something wants you to act, and a finished agent turn is one of those, so it still waves. A new finished turn also gets a single small nod on arrival, when the character is otherwise calm (idle or asleep), not muted, and Reduce Motion is off. A count badge at its corner shows how many events are waiting, the Events tab count less snoozed events (a notification stops counting once read), grey while muted. It animates on the render server and pauses while it is covered, the display sleeps or the screen is locked, so it stays near 0% CPU. With Reduce Motion on, it holds still. | Setting | Key | Values | |---------|-----|--------| diff --git a/panel/Companion/CompanionController.swift b/panel/Companion/CompanionController.swift index b0f1336..d8861c5 100644 --- a/panel/Companion/CompanionController.swift +++ b/panel/Companion/CompanionController.swift @@ -100,6 +100,7 @@ final class CompanionController { muted: nav.isMuted, permissionWaiting: waiting.contains { $0.kind == .permission }, quotaStressed: nav.stressPeakUtilization >= Double(nav.quotaAlertThreshold), + eventAlerting: waiting.contains { $0.isUnread && $0.severity == .alert }, eventWaiting: !waiting.isEmpty, sessionBusy: live.contains { $0.liveStatus == "busy" }, quietFor: now.timeIntervalSince(quietSince)) diff --git a/panel/Companion/CompanionMode.swift b/panel/Companion/CompanionMode.swift index fbfcb72..b062344 100644 --- a/panel/Companion/CompanionMode.swift +++ b/panel/Companion/CompanionMode.swift @@ -9,6 +9,8 @@ struct CompanionSignals: Equatable { var muted = false var permissionWaiting = false var quotaStressed = false + // An unread alert: CI failing, a merge conflict, a meeting about to start. + var eventAlerting = false var eventWaiting = false var sessionBusy = false // Seconds since there was last anything to show; drives the nap. @@ -21,12 +23,13 @@ enum CompanionMode { // Highest priority first: a preview, then mute, which outranks the // activity it is hiding. - // Alert is for trouble (quota past the threshold); a permission prompt is - // routine, so it waits like any other event instead of alarming. + // Alert is for trouble (quota past the threshold, an unread alert event); + // a permission prompt is routine, so it waits like any other event + // instead of alarming. static func mode(for s: CompanionSignals) -> SkinMode { if let preview = s.preview { return preview } if s.muted { return .muted } - if s.quotaStressed { return .alert } + if s.quotaStressed || s.eventAlerting { return .alert } if s.permissionWaiting || s.eventWaiting { return .pending } if s.sessionBusy { return .thinking } // Live but idle sessions leave the character idle; they only keep it diff --git a/panel/GitHubFeed.swift b/panel/GitHubFeed.swift index e18cf52..8a96a63 100644 --- a/panel/GitHubFeed.swift +++ b/panel/GitHubFeed.swift @@ -175,6 +175,10 @@ enum GitHubFeed { // The commit whose green checks were last announced, so a commit is // announced once however many polls see it green. var checksSha: String? + // True from a failing run until a passing one, across the pending runs + // a fix push starts, so the green run can still replace the failure row. + // Optional so state files from before it existed still decode. + var ciFailing: Bool? } struct State: Codable, Equatable { @@ -270,7 +274,9 @@ enum GitHubFeed { // UNKNOWN is GitHub still working it out; keep the last answer rather than flap. let mergeable = pr.mergeable == "UNKNOWN" ? prev?.mergeable : pr.mergeable var next = PRState(decision: pr.reviewDecision, ci: ci, mergeable: mergeable, - ready: prev?.ready ?? false, checksSha: prev?.checksSha) + ready: prev?.ready ?? false, checksSha: prev?.checksSha, + ciFailing: isFailing(ci) ? true : ci == "SUCCESS" ? false + : prev.map { $0.ciFailing ?? isFailing($0.ci) }) var coveredGreen = false if let prev, !seeding { @@ -281,16 +287,17 @@ enum GitHubFeed { approved ? "\(label) approved" : "\(label) has changes requested", pr.title, url: pr.url, key: "decision:\(key)")) } - if ci != prev.ci { - if isFailing(ci) { - events.append(feed("ci", .alert, "CI failing on \(label)", pr.title, url: pr.url, key: "ci:\(key)")) - } else if ci == "SUCCESS", isFailing(prev.ci) { - events.append(feed("ci", .info, "CI passing again on \(label)", pr.title, url: pr.url, key: "ci:\(key)")) - coveredGreen = true - } + if ci != prev.ci, isFailing(ci) { + events.append(feed("ci", .alert, "CI failing on \(label)", pr.title, url: pr.url, key: "ci:\(key)")) + } else if ci == "SUCCESS", prev.ciFailing ?? isFailing(prev.ci) { + events.append(feed("ci", .info, "CI passing again on \(label)", pr.title, url: pr.url, key: "ci:\(key)")) + coveredGreen = true } + // Both conflict events share a key so the resolution clears the alert. if mergeable == "CONFLICTING", prev.mergeable != "CONFLICTING" { events.append(feed("ci", .alert, "Merge conflict on \(label)", pr.title, url: pr.url, key: "conflict:\(key)")) + } else if mergeable == "MERGEABLE", prev.mergeable == "CONFLICTING" { + events.append(feed("ci", .info, "Conflict resolved on \(label)", pr.title, url: pr.url, key: "conflict:\(key)")) } }