From be5dda361017cd764f811c97fcfb387d11f0c0a0 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Thu, 19 Nov 2026 22:25:04 -0800 Subject: [PATCH 1/2] fix(computer-use): check both event boundaries, show offset times locally, refuse mismatched date forms before asking --- .../native/computer-use/ComputerUse.swift | 8 ++--- .../src/main/computer-use/service.test.ts | 25 +++++++++++--- apps/desktop/src/main/computer-use/service.ts | 34 ++++++++++++++++--- .../src/computer-use.generated.ts | 7 ++-- 4 files changed, 59 insertions(+), 15 deletions(-) diff --git a/apps/desktop/native/computer-use/ComputerUse.swift b/apps/desktop/native/computer-use/ComputerUse.swift index 9f00ae0a1e1..cd70c6639f8 100644 --- a/apps/desktop/native/computer-use/ComputerUse.swift +++ b/apps/desktop/native/computer-use/ComputerUse.swift @@ -419,8 +419,8 @@ func runAppCommand(_ command: AppCommand, expected: Expectation? = nil) throws - } let now = eventItem(event) let moved = (title != nil && now["title"] as? String != expected.title) - || (command.start != nil && now["start"] as? String != expected.start) - || (command.end != nil && now["end"] as? String != expected.end) + // Either boundary is checked when either changes: the one kept is saved beside the one moved. + || ((command.start != nil || command.end != nil) && (now["start"] as? String != expected.start || now["end"] as? String != expected.end)) || (command.location != nil && now["location"] as? String != expected.location) || (command.notes != nil && now["notes"] as? String != expected.notes) guard !moved else { @@ -505,7 +505,7 @@ func runAppCommand(_ command: AppCommand, expected: Expectation? = nil) throws - try checkCancellation() inputDispatched = true try store.save(reminder, commit: true) - return ["kind": "command", "command": command.name, "created": ["id": reminder.calendarItemIdentifier, "title": title, "list": list.title]] + return ["kind": "command", "command": command.name, "created": ["id": reminder.calendarItemIdentifier, "title": title, "list": bounded(list.title, 1024)]] case "list_calendars": let allCalendars = try calendarStore().calendars(for: .event) let calendars = allCalendars.prefix(200).map { calendar -> [String: Any] in @@ -540,7 +540,7 @@ func runAppCommand(_ command: AppCommand, expected: Expectation? = nil) throws - try checkCancellation() inputDispatched = true try store.save(event, span: .thisEvent, commit: true) - return ["kind": "command", "command": command.name, "created": ["id": event.eventIdentifier ?? event.calendarItemIdentifier, "title": title, "list": calendar.title]] + return ["kind": "command", "command": command.name, "created": ["id": event.eventIdentifier ?? event.calendarItemIdentifier, "title": title, "list": bounded(calendar.title, 1024)]] case "list_contact_groups": let allGroups = try contactStore().groups(matching: nil) let groups = allGroups.prefix(200).map { group -> [String: Any] in diff --git a/apps/desktop/src/main/computer-use/service.test.ts b/apps/desktop/src/main/computer-use/service.test.ts index a7fe0777e72..8964fc42450 100644 --- a/apps/desktop/src/main/computer-use/service.test.ts +++ b/apps/desktop/src/main/computer-use/service.test.ts @@ -318,11 +318,11 @@ describe('native computer use authority and lifecycle', () => { events: [ { id: command.id, - title: 'Flight\nCalendar: Work', + title: 'Flight\u0085Calendar: Work', list: 'Travel', - start: '2027-06-09T07:15:00', - end: '2027-06-09T08:10:00', - allDay: false, + start: command.id === 'trip' ? '2027-06-09' : '2027-06-09T07:15:00', + end: command.id === 'trip' ? '2027-06-12' : '2027-06-09T08:10:00', + allDay: command.id === 'trip', recurring: command.id === 'weekly', }, ], @@ -344,6 +344,11 @@ describe('native computer use authority and lifecycle', () => { }) expect(confirmAppCommand).not.toHaveBeenCalled() + await expect(service.execute('trip', 'chat', update('trip'))).rejects.toMatchObject({ + details: { code: 'invalid_arguments', dispatchState: 'not_started' }, + }) + expect(confirmAppCommand).not.toHaveBeenCalled() + await service.execute('moved', 'chat', update('e1')) expect(confirmAppCommand.mock.calls[0]?.[0]).toMatchObject({ change: 'update', @@ -353,11 +358,21 @@ describe('native computer use authority and lifecycle', () => { expect(native.request.mock.lastCall?.[1]).toEqual({ command: update('e1').command, expected: { - title: 'Flight\nCalendar: Work', + title: 'Flight\u0085Calendar: Work', start: '2027-06-09T07:15:00', end: '2027-06-09T08:10:00', }, }) + + // The current start written with an offset is the same moment, so it must not read as a move. + const sameMoment = new Date(2027, 5, 9, 7, 15).toISOString() + await service.execute('same', 'chat', { + action: 'app_command', + command: { name: 'update_event', id: 'e1', start: sameMoment }, + }) + expect(confirmAppCommand.mock.lastCall?.[0].summary).toContain( + 'Starts: 2027-06-09T07:15:00 → 2027-06-09T07:15:00' + ) }) it('confirms what a contact update adds, by name and group, and refuses an unknown id first', async () => { diff --git a/apps/desktop/src/main/computer-use/service.ts b/apps/desktop/src/main/computer-use/service.ts index ab9ebf81ab1..a4cbe0d7975 100644 --- a/apps/desktop/src/main/computer-use/service.ts +++ b/apps/desktop/src/main/computer-use/service.ts @@ -106,7 +106,23 @@ interface UpdateApproval { * like extra fields in the confirmation. */ function oneLine(value: string): string { - return value.replace(/[\u0000-\u001f\u007f\u2028\u2029]+/g, ' ').trim() + // C0/C1 controls (NEL breaks a line too), line and paragraph separators, and bidi controls, + // which could reorder "current → new". + return value + .replace(/[\u0000-\u001f\u007f-\u009f\u2028\u2029\u202a-\u202e\u2066-\u2069]+/g, ' ') + .trim() +} + +/** + * A new date or time as the helper will save it: a time with an offset is shown in this Mac's + * local time, the form the current value is shown in, so a same-time change doesn't read as a move. + */ +function localTime(value: string): string { + if (!/T.*(Z|[+-]\d{2}:\d{2})$/.test(value)) return value + const date = new Date(value) + if (Number.isNaN(date.getTime())) return value + const pad = (n: number) => String(n).padStart(2, '0') + return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}T${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}` } /** @@ -136,7 +152,7 @@ function reminderChanges( const changes: Change[] = [] const title = command.title?.trim() if (title !== undefined && title !== current.title) changes.push(['Title', current.title, title]) - if (command.due !== undefined) changes.push(['Due', current.due, command.due]) + if (command.due !== undefined) changes.push(['Due', current.due, localTime(command.due)]) if (command.clearDue) changes.push(['Due', current.due, 'none']) if (command.priority !== undefined) changes.push(['Priority', priority(current.priority), priority(command.priority)]) @@ -158,8 +174,8 @@ function eventChanges( const changes: Change[] = [] const title = command.title?.trim() if (title !== undefined && title !== current.title) changes.push(['Title', current.title, title]) - if (command.start !== undefined) changes.push(['Starts', current.start, command.start]) - if (command.end !== undefined) changes.push(['Ends', current.end, command.end]) + if (command.start !== undefined) changes.push(['Starts', current.start, localTime(command.start)]) + if (command.end !== undefined) changes.push(['Ends', current.end, localTime(command.end)]) if (command.location !== undefined) changes.push(['Location', current.location, command.location.trim() || 'none']) if (command.notes !== undefined) @@ -618,6 +634,16 @@ export class ComputerUseService { message: 'This event repeats; change it in Calendar so the user picks which occurrences.', dispatchState: 'not_started', }) + // An all-day event takes dates and a timed one takes times; a mismatch is refused before asking. + for (const boundary of [command.start, command.end]) + if (boundary !== undefined && boundary.includes('T') === current.allDay) + throw new ComputerUseError({ + code: 'invalid_arguments', + message: current.allDay + ? 'This is an all-day event; pass start and end as YYYY-MM-DD.' + : 'This event has times; pass start and end as ISO 8601 date-times.', + dispatchState: 'not_started', + }) return { summary: changeSummary( [ diff --git a/packages/desktop-bridge/src/computer-use.generated.ts b/packages/desktop-bridge/src/computer-use.generated.ts index 29908ea9ba6..41e1afc85bc 100644 --- a/packages/desktop-bridge/src/computer-use.generated.ts +++ b/packages/desktop-bridge/src/computer-use.generated.ts @@ -251,6 +251,9 @@ const DateOrDateTime = z "Use a real date and time", ); const CommandNotes = z.string().max(8000).optional(); +const UpdatedEventBoundary = DateOrDateTime.describe( + "In the event's own form: YYYY-MM-DD for an all-day event, else an ISO 8601 date-time (this Mac's local time without an offset).", +); const EventBoundary = DateOrDateTime.describe( "ISO 8601 date-time, or YYYY-MM-DD when allDay is true. Without an offset it is this Mac's local time, as typing it into Calendar would be.", ); @@ -359,8 +362,8 @@ export const AppCommandSchema = z.discriminatedUnion("name", [ .max(256) .describe("An id from find_events or create_event; repeating events are refused."), title: CommandTitle.optional(), - start: EventBoundary.optional(), - end: EventBoundary.optional(), + start: UpdatedEventBoundary.optional(), + end: UpdatedEventBoundary.optional(), location: z.string().max(1024).optional().describe("Replaces the location; an empty string clears it."), notes: CommandNotes.describe("Replaces the notes; an empty string clears them."), }) From c1e821e2d411f85a3a337d5180756279bc1a4415 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Thu, 19 Nov 2026 22:38:05 -0800 Subject: [PATCH 2/2] fix(computer-use): keep a new time's offset beside its local time, filter all bidi controls, bound create titles --- apps/desktop/native/computer-use/ComputerUse.swift | 4 ++-- apps/desktop/src/main/computer-use/service.test.ts | 4 ++-- apps/desktop/src/main/computer-use/service.ts | 13 +++++++++---- .../desktop-bridge/src/computer-use.generated.ts | 2 +- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/apps/desktop/native/computer-use/ComputerUse.swift b/apps/desktop/native/computer-use/ComputerUse.swift index cd70c6639f8..46cb1f1b758 100644 --- a/apps/desktop/native/computer-use/ComputerUse.swift +++ b/apps/desktop/native/computer-use/ComputerUse.swift @@ -505,7 +505,7 @@ func runAppCommand(_ command: AppCommand, expected: Expectation? = nil) throws - try checkCancellation() inputDispatched = true try store.save(reminder, commit: true) - return ["kind": "command", "command": command.name, "created": ["id": reminder.calendarItemIdentifier, "title": title, "list": bounded(list.title, 1024)]] + return ["kind": "command", "command": command.name, "created": ["id": reminder.calendarItemIdentifier, "title": bounded(title, 1024), "list": bounded(list.title, 1024)]] case "list_calendars": let allCalendars = try calendarStore().calendars(for: .event) let calendars = allCalendars.prefix(200).map { calendar -> [String: Any] in @@ -540,7 +540,7 @@ func runAppCommand(_ command: AppCommand, expected: Expectation? = nil) throws - try checkCancellation() inputDispatched = true try store.save(event, span: .thisEvent, commit: true) - return ["kind": "command", "command": command.name, "created": ["id": event.eventIdentifier ?? event.calendarItemIdentifier, "title": title, "list": bounded(calendar.title, 1024)]] + return ["kind": "command", "command": command.name, "created": ["id": event.eventIdentifier ?? event.calendarItemIdentifier, "title": bounded(title, 1024), "list": bounded(calendar.title, 1024)]] case "list_contact_groups": let allGroups = try contactStore().groups(matching: nil) let groups = allGroups.prefix(200).map { group -> [String: Any] in diff --git a/apps/desktop/src/main/computer-use/service.test.ts b/apps/desktop/src/main/computer-use/service.test.ts index 8964fc42450..01c1d41c4a0 100644 --- a/apps/desktop/src/main/computer-use/service.test.ts +++ b/apps/desktop/src/main/computer-use/service.test.ts @@ -364,14 +364,14 @@ describe('native computer use authority and lifecycle', () => { }, }) - // The current start written with an offset is the same moment, so it must not read as a move. + // The current start written with an offset keeps the offset and shows the same local time. const sameMoment = new Date(2027, 5, 9, 7, 15).toISOString() await service.execute('same', 'chat', { action: 'app_command', command: { name: 'update_event', id: 'e1', start: sameMoment }, }) expect(confirmAppCommand.mock.lastCall?.[0].summary).toContain( - 'Starts: 2027-06-09T07:15:00 → 2027-06-09T07:15:00' + `Starts: 2027-06-09T07:15:00 → ${sameMoment} (2027-06-09T07:15:00 here)` ) }) diff --git a/apps/desktop/src/main/computer-use/service.ts b/apps/desktop/src/main/computer-use/service.ts index a4cbe0d7975..189759f3c25 100644 --- a/apps/desktop/src/main/computer-use/service.ts +++ b/apps/desktop/src/main/computer-use/service.ts @@ -109,20 +109,25 @@ function oneLine(value: string): string { // C0/C1 controls (NEL breaks a line too), line and paragraph separators, and bidi controls, // which could reorder "current → new". return value - .replace(/[\u0000-\u001f\u007f-\u009f\u2028\u2029\u202a-\u202e\u2066-\u2069]+/g, ' ') + .replace( + /[\u0000-\u001f\u007f-\u009f\u061c\u200e\u200f\u2028\u2029\u202a-\u202e\u2066-\u2069]+/g, + ' ' + ) .trim() } /** - * A new date or time as the helper will save it: a time with an offset is shown in this Mac's - * local time, the form the current value is shown in, so a same-time change doesn't read as a move. + * A new date or time as the helper will save it. A time with an offset keeps that offset, which names + * exactly one moment even in an hour a daylight-saving change repeats, and adds this Mac's local time, + * the form the current value is shown in. */ function localTime(value: string): string { if (!/T.*(Z|[+-]\d{2}:\d{2})$/.test(value)) return value const date = new Date(value) if (Number.isNaN(date.getTime())) return value const pad = (n: number) => String(n).padStart(2, '0') - return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}T${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}` + const local = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}T${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}` + return `${value} (${local} here)` } /** diff --git a/packages/desktop-bridge/src/computer-use.generated.ts b/packages/desktop-bridge/src/computer-use.generated.ts index 41e1afc85bc..5901ffc1d9e 100644 --- a/packages/desktop-bridge/src/computer-use.generated.ts +++ b/packages/desktop-bridge/src/computer-use.generated.ts @@ -252,7 +252,7 @@ const DateOrDateTime = z ); const CommandNotes = z.string().max(8000).optional(); const UpdatedEventBoundary = DateOrDateTime.describe( - "In the event's own form: YYYY-MM-DD for an all-day event, else an ISO 8601 date-time (this Mac's local time without an offset).", + "In the event's own form: YYYY-MM-DD for an all-day event, else an ISO 8601 date-time. Keep an offset the source states; without one it is this Mac's local time.", ); const EventBoundary = DateOrDateTime.describe( "ISO 8601 date-time, or YYYY-MM-DD when allDay is true. Without an offset it is this Mac's local time, as typing it into Calendar would be.",