diff --git a/apps/desktop/native/computer-use/ComputerUse.swift b/apps/desktop/native/computer-use/ComputerUse.swift index 9f00ae0a1e1..46cb1f1b758 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": 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": calendar.title]] + 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 a7fe0777e72..01c1d41c4a0 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 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 → ${sameMoment} (2027-06-09T07:15:00 here)` + ) }) 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..189759f3c25 100644 --- a/apps/desktop/src/main/computer-use/service.ts +++ b/apps/desktop/src/main/computer-use/service.ts @@ -106,7 +106,28 @@ 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\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 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') + 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)` } /** @@ -136,7 +157,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 +179,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 +639,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..5901ffc1d9e 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. 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.", ); @@ -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."), })