Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mobile/src/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,9 @@
"sound": "Notification sound",
"suppressWhileFocused": "Suppress while focused",
"suppressWhileFocusedHint": "Skip alerts for the workspace open on this phone.",
"footer": "Alert types follow each paired desktop’s notification settings. Notifications pause after 7 days without using this app; open it and reconnect to resume."
"footer": "Alert types follow each paired desktop’s notification settings. Notifications pause after 7 days without using this app; open it and reconnect to resume.",
"onlyWhenAgentNeedsInput": "Only when an agent needs me",
"onlyWhenAgentNeedsInputHint": "Permission requests and questions. Skips finished agents."
},
"desktopChannel": {
"silentName": "Orca silent notifications",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ it('shows only phone-specific controls while desktop owns category eligibility',
const switches = () => findAllByHostType(renderer.root, 'Switch')
expect(switches().map((node) => node.props.accessibilityLabel)).toEqual([
'Only when away from desktop',
'Only when an agent needs me',
'Notification sound',
'Suppress while focused'
])
Expand Down
11 changes: 11 additions & 0 deletions mobile/src/notifications/NotificationDeliverySection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ export function NotificationDeliverySection({ value, disabled, onChange }: Props
'After 3 minutes without keyboard or mouse activity, or when locked.'
)
)}
{row(
'onlyWhenAgentNeedsInput',
t(
'notifications.notificationDeliverySection.onlyWhenAgentNeedsInput',
'Only when an agent needs me'
),
t(
'notifications.notificationDeliverySection.onlyWhenAgentNeedsInputHint',
'Permission requests and questions. Skips finished agents.'
)
)}
{row('sound', t('notifications.notificationDeliverySection.sound', 'Notification sound'))}
{row(
'suppressWhileViewing',
Expand Down
26 changes: 24 additions & 2 deletions mobile/src/notifications/mobile-notifications.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ const fallback = vi.hoisted(() => ({
completion: { completed: Promise.resolve(true), signal: new AbortController().signal },
eligible: vi.fn(),
schedule: vi.fn(),
preferences: { onlyWhenDesktopAway: true, sound: true, suppressWhileViewing: true }
preferences: {
onlyWhenDesktopAway: true,
onlyWhenAgentNeedsInput: false,
sound: true,
suppressWhileViewing: true
}
}))
vi.mock('./push-token', () => ({ getDevicePushToken: fallback.token }))
vi.mock('./push-registration', () => ({
Expand Down Expand Up @@ -167,7 +172,12 @@ beforeEach(() => {
fallback.supported.mockReturnValue(true)
fallback.eligible.mockResolvedValue(true)
fallback.schedule.mockResolvedValue('local')
fallback.preferences = { onlyWhenDesktopAway: true, sound: true, suppressWhileViewing: true }
fallback.preferences = {
onlyWhenDesktopAway: true,
onlyWhenAgentNeedsInput: false,
sound: true,
suppressWhileViewing: true
}
})

const liveAlert = {
Expand Down Expand Up @@ -255,6 +265,18 @@ it.each([
expect(fallback.schedule).not.toHaveBeenCalled()
})

it('presents only agent questions when the phone wants nothing else, on any host', async () => {
fallback.token.mockResolvedValue(null)
fallback.preferences = { ...fallback.preferences, onlyWhenAgentNeedsInput: true }
emitAlert({ ...liveAlert, agentState: 'done' })
emitAlert({ ...liveAlert, agentState: 'blocked', notificationSeq: 5 })
await vi.waitFor(() => expect(fallback.schedule).toHaveBeenCalledOnce())
// Foreground eligibility re-reads the kind from this data, so it must carry it.
expect(fallback.schedule.mock.calls[0][0]).toMatchObject({
content: { data: { source: 'agent-task-complete', agentState: 'needs-input' } }
})
})

it('delegates foreground eligibility before scheduling', async () => {
fallback.token.mockResolvedValue(null)
fallback.eligible.mockResolvedValue(false)
Expand Down
2 changes: 2 additions & 0 deletions mobile/src/notifications/mobile-notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ export function subscribeToDesktopNotifications(client: RpcClient, hostId: strin
worktreeId: event.worktreeId,
paneKey: event.paneKey,
executionHost: event.executionHost,
source: content.source,
...(content.agentState ? { agentState: content.agentState } : {}),
channelId
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ it('persists only phone-specific delivery preferences', async () => {
expect(await loadNotificationDeliveryPreferences()).toEqual(value)
expect(notificationPreferencesFilter(value)).toEqual({
onlyWhenDesktopAway: false,
onlyWhenAgentNeedsInput: false,
sound: false
})
})
Expand All @@ -54,11 +55,13 @@ it('ignores unrelated stored preferences', async () => {
)
expect(await loadNotificationDeliveryPreferences()).toEqual({
onlyWhenDesktopAway: false,
onlyWhenAgentNeedsInput: false,
sound: false,
suppressWhileViewing: false
})
expect(notificationPreferencesFilter(await loadNotificationDeliveryPreferences())).toEqual({
onlyWhenDesktopAway: false,
onlyWhenAgentNeedsInput: false,
sound: false
})
})
Expand Down
3 changes: 3 additions & 0 deletions mobile/src/notifications/notification-delivery-preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import type { MobilePushFilter } from '../../../src/shared/mobile-push-contract'
const KEY = 'orca:notificationDeliveryPreferences'
export type NotificationDeliveryPreferences = {
onlyWhenDesktopAway: boolean
onlyWhenAgentNeedsInput: boolean
sound: boolean
suppressWhileViewing: boolean
}

export const DEFAULT_NOTIFICATION_DELIVERY: NotificationDeliveryPreferences = {
onlyWhenDesktopAway: true,
onlyWhenAgentNeedsInput: false,
sound: true,
suppressWhileViewing: true
}
Expand Down Expand Up @@ -44,6 +46,7 @@ export function notificationPreferencesFilter(
): MobilePushFilter {
return {
onlyWhenDesktopAway: value.onlyWhenDesktopAway,
onlyWhenAgentNeedsInput: value.onlyWhenAgentNeedsInput,
sound: value.sound
}
}
6 changes: 5 additions & 1 deletion mobile/src/notifications/push-payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export type OrcaPushPayload = {
readonly paneKey?: string
readonly worktreeId?: string
readonly executionHost?: string
readonly source?: string
readonly agentState?: string
}

function readString(value: unknown): string | undefined {
Expand Down Expand Up @@ -38,6 +40,8 @@ export function readOrcaPushPayload(data: unknown): OrcaPushPayload | null {
notificationEpoch: readString(record.notificationEpoch),
paneKey: readString(record.paneKey),
worktreeId: readString(record.worktreeId),
executionHost: readString(record.executionHost)
executionHost: readString(record.executionHost),
source: readString(record.source),
agentState: readString(record.agentState)
}
}
27 changes: 27 additions & 0 deletions mobile/src/notifications/push-receive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,33 @@ describe('shouldSuppressForegroundPush', () => {
notificationEpoch: 'epoch-1'
})

it('drops an older host’s finished-agent push but keeps questions, bells and the push test', async () => {
storage.set(
'orca:notificationDeliveryPreferences',
JSON.stringify({ onlyWhenAgentNeedsInput: true })
)
const alert = (seq: number, agentState: string) =>
apnsData({
hostFingerprint,
notificationSeq: seq,
notificationEpoch: 'epoch-1',
source: 'agent-task-complete',
agentState
})
await expect(shouldSuppressForegroundPush(alert(1, 'finished'))).resolves.toBe(true)
await expect(shouldSuppressForegroundPush(alert(2, 'needs-input'))).resolves.toBe(false)
await expect(
shouldSuppressForegroundPush(
fcmData({
hostFingerprint,
notificationSeq: 3,
notificationEpoch: 'epoch-1',
source: 'terminal-bell'
})
)
).resolves.toBe(false)
})

it('allows one eligible native push and suppresses an in-process duplicate', async () => {
await expect(shouldSuppressForegroundPush(push())).resolves.toBe(false)
await expect(shouldSuppressForegroundPush(push())).resolves.toBe(true)
Expand Down
23 changes: 15 additions & 8 deletions mobile/src/notifications/push-receive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { resolveHostIdForFingerprint } from './push-host-fingerprint'
import { readOrcaPushPayload, type OrcaPushPayload } from './push-payload'
import type { NotificationBehavior } from 'expo-notifications'
import { readNativeNotificationData } from './native-notification-data'
import { loadNotificationDeliveryPreferences } from './notification-delivery-preferences'
import {
loadNotificationDeliveryPreferences,
type NotificationDeliveryPreferences
} from './notification-delivery-preferences'
import { allowsMobilePushAlertKind } from '../../../src/shared/mobile-push-alert'

const RECENT_FOREGROUND_PUSH_CAP = 512
const recentForegroundPushes = new Set<string>()
Expand Down Expand Up @@ -52,10 +56,9 @@ export async function foregroundNotificationBehavior(notification: {
const payload = readOrcaPushPayload(data)
const preferences = await loadNotificationDeliveryPreferences()
// Preserve unknown notifications; recognized pushes fail closed when eligibility/dismissal checks cannot complete.
const ineligible = await shouldSuppressForegroundPush(
payload,
preferences.suppressWhileViewing
).catch(() => payload !== null)
const ineligible = await shouldSuppressForegroundPush(payload, preferences).catch(
() => payload !== null
)
const suppressed = ineligible || (payload !== null && !claimForegroundPush(payload))
return {
shouldShowBanner: !suppressed,
Expand All @@ -67,7 +70,7 @@ export async function foregroundNotificationBehavior(notification: {

export async function canPresentForegroundPush(payload: OrcaPushPayload): Promise<boolean> {
const preferences = await loadNotificationDeliveryPreferences()
return !(await shouldSuppressForegroundPush(payload, preferences.suppressWhileViewing))
return !(await shouldSuppressForegroundPush(payload, preferences))
}

async function resolvePushHostId(payload: OrcaPushPayload): Promise<string | null> {
Expand All @@ -77,7 +80,7 @@ async function resolvePushHostId(payload: OrcaPushPayload): Promise<string | nul

async function shouldSuppressForegroundPush(
payload: OrcaPushPayload | null,
suppressWhileViewing: boolean
preferences: NotificationDeliveryPreferences
): Promise<boolean> {
if (!payload) {
return false
Expand All @@ -92,6 +95,10 @@ async function shouldSuppressForegroundPush(
}
return true
}
// An older host ignores this registration filter, so its pushes are filtered here too.
if (!allowsMobilePushAlertKind(preferences, payload.source, payload.agentState)) {
return true
}
const hostId = await resolvePushHostId(payload)
// Suppress removed-host pushes: stale gateway registrations cannot route a useful tap and the phone can no longer disable them per host.
if (!hostId) {
Expand All @@ -100,7 +107,7 @@ async function shouldSuppressForegroundPush(
if (!(await loadPushNotificationsEnabled())) {
return true
}
if (shouldSuppressNotificationWhileViewing(payload, hostId, suppressWhileViewing)) {
if (shouldSuppressNotificationWhileViewing(payload, hostId, preferences.suppressWhileViewing)) {
return true
}
// Keep this last: a socket/native dismissal may land during any preference or host read.
Expand Down
2 changes: 1 addition & 1 deletion mobile/src/notifications/push-registration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe('push registration capability gating', () => {
platform: 'ios',
token: IOS_TOKEN.token,
apnsEnvironment: 'production',
filter: { onlyWhenDesktopAway: true, sound: true }
filter: { onlyWhenDesktopAway: true, onlyWhenAgentNeedsInput: false, sound: true }
})
expect(stored.registeredHostIds).toEqual(['host-1'])
})
Expand Down
1 change: 1 addition & 0 deletions src/main/ipc/notifications-macos-removal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function register(): void {
notifications: {
enabled: true,
agentTaskComplete: true,
agentNeedsInput: true,
terminalBell: true,
suppressWhenFocused: false
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/ipc/notifications-message-formatting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ describe('registerNotificationHandlers', () => {
})

it('formats blocked and interrupted agent snapshots distinctly', async () => {
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the handlers read only getSettings().notifications from the store.
registerNotificationHandlers({
getSettings: () => ({
notifications: {
enabled: true,
agentTaskComplete: true,
agentNeedsInput: true,
terminalBell: false,
suppressWhenFocused: false
}
Expand Down
1 change: 1 addition & 0 deletions src/main/ipc/notifications-permission-onboarding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe('notifications:probeDelivery', () => {
notifications: {
enabled: true,
agentTaskComplete: true,
agentNeedsInput: true,
terminalBell: true,
suppressWhenFocused: false
}
Expand Down
1 change: 1 addition & 0 deletions src/main/ipc/notifications-subject-retirement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function register(
notifications: {
enabled: true,
agentTaskComplete: true,
agentNeedsInput: true,
terminalBell: true,
suppressWhenFocused: options.suppressWhenFocused
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/notifications/notification-delivery-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function makeSettings(overrides: Partial<NotificationSettings> = {}): Notificati
return {
enabled: true,
agentTaskComplete: true,
agentNeedsInput: true,
terminalBell: true,
suppressWhenFocused: false,
customSoundId: 'system',
Expand Down Expand Up @@ -117,6 +118,25 @@ describe('createNotificationDeliveryService', () => {
expect(harness.order).toEqual(['tray', 'mobile'])
})

it('gates agent questions on needs-input and finished turns on task-complete', () => {
const harness = makeHarness(makeSettings({ agentTaskComplete: false }))
const service = createNotificationDeliveryService(harness.deps)
expect(service.dispatch(makeRequest({ agentState: 'blocked' }))).toEqual({ delivered: true })
expect(
service.dispatch(
makeRequest({ agentState: 'done', worktreeId: 'wt-2', worktreeLabel: 'wt-2' })
)
).toEqual({ delivered: false, reason: 'source-disabled' })

const quiet = makeHarness(makeSettings({ agentNeedsInput: false }))
expect(
createNotificationDeliveryService(quiet.deps).dispatch(makeRequest({ agentState: 'waiting' }))
).toEqual({ delivered: false, reason: 'source-disabled' })
expect(quiet.dispatchMobileNotification).toHaveBeenCalledWith(
expect.objectContaining({ desktopAllowed: false, agentState: 'waiting' })
)
})

it('keeps the desktop source gates distinct per source', () => {
const harness = makeHarness(makeSettings({ terminalBell: false }))
const service = createNotificationDeliveryService(harness.deps)
Expand Down
10 changes: 6 additions & 4 deletions src/main/notifications/notification-delivery-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { buildNotificationOptions } from '../ipc/notification-options'
import { translateMain } from '../i18n/main-i18n'
import { reserveNotificationCooldown } from '../../shared/notification-burst-cooldown'
import { toHostSessionPaneKey } from '../../shared/terminal-surface-id'
import { isNotificationSourceEnabled } from '../../shared/notification-source-gate'

export type NotificationDeliveryDependencies = {
readNotificationSettings: () => NotificationSettings
Expand Down Expand Up @@ -78,10 +79,11 @@ export function createNotificationDeliveryService(
request.notificationSourceId !== undefined &&
settings.mutedNotificationSourceIds.includes(request.notificationSourceId)
// Machine mutes leave mobile eligibility and its cooldown unchanged.
const desktopAllowed =
settings.enabled &&
(request.source !== 'agent-task-complete' || settings.agentTaskComplete) &&
(request.source !== 'terminal-bell' || settings.terminalBell)
const desktopAllowed = isNotificationSourceEnabled(
settings,
request.source,
request.agentState
)

const notificationOptions = buildNotificationOptions(request, translateMain)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,18 @@ describe('muted notification machines', () => {
expect(persistedNotificationSettingsRepaired(persisted, normalized)).toBe(true)
})
})

describe('agent needs-input notifications', () => {
it('inherits the old shared toggle on upgrade, then stands on its own', () => {
const { agentNeedsInput: _absentBeforeUpgrade, ...upgraded } = {
...getDefaultNotificationSettings(),
agentTaskComplete: false
}
expect(normalizeNotificationSettings(upgraded).agentNeedsInput).toBe(false)
expect(normalizeNotificationSettings({}).agentNeedsInput).toBe(true)
expect(
normalizeNotificationSettings({ agentTaskComplete: false, agentNeedsInput: true })
.agentNeedsInput
).toBe(true)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export function normalizeNotificationSettings(value: unknown): NotificationSetti
return {
enabled: booleanOr(candidate.enabled, defaults.enabled),
agentTaskComplete: booleanOr(candidate.agentTaskComplete, defaults.agentTaskComplete),
// Upgrades inherit the old shared toggle so turning completions off never starts new alerts.
agentNeedsInput: booleanOr(
candidate.agentNeedsInput,
booleanOr(candidate.agentTaskComplete, defaults.agentNeedsInput)
),
terminalBell: booleanOr(candidate.terminalBell, defaults.terminalBell),
suppressWhenFocused: booleanOr(candidate.suppressWhenFocused, defaults.suppressWhenFocused),
customSoundId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ describe('Store', () => {
expect(store.getSettings().notifications).toMatchObject({
enabled: true,
agentTaskComplete: true,
agentNeedsInput: true,
terminalBell: false,
suppressWhenFocused: true,
customSoundPath: '/Users/kaylee/Downloads/Note_block_pling.ogg',
Expand Down
Loading
Loading