Skip to content

Commit 11cd2cb

Browse files
authored
fix(computer-use): tighten app-command update confirmations (#10236)
* fix(computer-use): check both event boundaries, show offset times locally, refuse mismatched date forms before asking * fix(computer-use): keep a new time's offset beside its local time, filter all bidi controls, bound create titles
1 parent 5929764 commit 11cd2cb

4 files changed

Lines changed: 64 additions & 15 deletions

File tree

‎apps/desktop/native/computer-use/ComputerUse.swift‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,8 @@ func runAppCommand(_ command: AppCommand, expected: Expectation? = nil) throws -
419419
}
420420
let now = eventItem(event)
421421
let moved = (title != nil && now["title"] as? String != expected.title)
422-
|| (command.start != nil && now["start"] as? String != expected.start)
423-
|| (command.end != nil && now["end"] as? String != expected.end)
422+
// Either boundary is checked when either changes: the one kept is saved beside the one moved.
423+
|| ((command.start != nil || command.end != nil) && (now["start"] as? String != expected.start || now["end"] as? String != expected.end))
424424
|| (command.location != nil && now["location"] as? String != expected.location)
425425
|| (command.notes != nil && now["notes"] as? String != expected.notes)
426426
guard !moved else {
@@ -505,7 +505,7 @@ func runAppCommand(_ command: AppCommand, expected: Expectation? = nil) throws -
505505
try checkCancellation()
506506
inputDispatched = true
507507
try store.save(reminder, commit: true)
508-
return ["kind": "command", "command": command.name, "created": ["id": reminder.calendarItemIdentifier, "title": title, "list": list.title]]
508+
return ["kind": "command", "command": command.name, "created": ["id": reminder.calendarItemIdentifier, "title": bounded(title, 1024), "list": bounded(list.title, 1024)]]
509509
case "list_calendars":
510510
let allCalendars = try calendarStore().calendars(for: .event)
511511
let calendars = allCalendars.prefix(200).map { calendar -> [String: Any] in
@@ -540,7 +540,7 @@ func runAppCommand(_ command: AppCommand, expected: Expectation? = nil) throws -
540540
try checkCancellation()
541541
inputDispatched = true
542542
try store.save(event, span: .thisEvent, commit: true)
543-
return ["kind": "command", "command": command.name, "created": ["id": event.eventIdentifier ?? event.calendarItemIdentifier, "title": title, "list": calendar.title]]
543+
return ["kind": "command", "command": command.name, "created": ["id": event.eventIdentifier ?? event.calendarItemIdentifier, "title": bounded(title, 1024), "list": bounded(calendar.title, 1024)]]
544544
case "list_contact_groups":
545545
let allGroups = try contactStore().groups(matching: nil)
546546
let groups = allGroups.prefix(200).map { group -> [String: Any] in

‎apps/desktop/src/main/computer-use/service.test.ts‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,11 @@ describe('native computer use authority and lifecycle', () => {
318318
events: [
319319
{
320320
id: command.id,
321-
title: 'Flight\nCalendar: Work',
321+
title: 'Flight\u0085Calendar: Work',
322322
list: 'Travel',
323-
start: '2027-06-09T07:15:00',
324-
end: '2027-06-09T08:10:00',
325-
allDay: false,
323+
start: command.id === 'trip' ? '2027-06-09' : '2027-06-09T07:15:00',
324+
end: command.id === 'trip' ? '2027-06-12' : '2027-06-09T08:10:00',
325+
allDay: command.id === 'trip',
326326
recurring: command.id === 'weekly',
327327
},
328328
],
@@ -344,6 +344,11 @@ describe('native computer use authority and lifecycle', () => {
344344
})
345345
expect(confirmAppCommand).not.toHaveBeenCalled()
346346

347+
await expect(service.execute('trip', 'chat', update('trip'))).rejects.toMatchObject({
348+
details: { code: 'invalid_arguments', dispatchState: 'not_started' },
349+
})
350+
expect(confirmAppCommand).not.toHaveBeenCalled()
351+
347352
await service.execute('moved', 'chat', update('e1'))
348353
expect(confirmAppCommand.mock.calls[0]?.[0]).toMatchObject({
349354
change: 'update',
@@ -353,11 +358,21 @@ describe('native computer use authority and lifecycle', () => {
353358
expect(native.request.mock.lastCall?.[1]).toEqual({
354359
command: update('e1').command,
355360
expected: {
356-
title: 'Flight\nCalendar: Work',
361+
title: 'Flight\u0085Calendar: Work',
357362
start: '2027-06-09T07:15:00',
358363
end: '2027-06-09T08:10:00',
359364
},
360365
})
366+
367+
// The current start written with an offset keeps the offset and shows the same local time.
368+
const sameMoment = new Date(2027, 5, 9, 7, 15).toISOString()
369+
await service.execute('same', 'chat', {
370+
action: 'app_command',
371+
command: { name: 'update_event', id: 'e1', start: sameMoment },
372+
})
373+
expect(confirmAppCommand.mock.lastCall?.[0].summary).toContain(
374+
`Starts: 2027-06-09T07:15:00 → ${sameMoment} (2027-06-09T07:15:00 here)`
375+
)
361376
})
362377

363378
it('confirms what a contact update adds, by name and group, and refuses an unknown id first', async () => {

‎apps/desktop/src/main/computer-use/service.ts‎

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,28 @@ interface UpdateApproval {
106106
* like extra fields in the confirmation.
107107
*/
108108
function oneLine(value: string): string {
109-
return value.replace(/[\u0000-\u001f\u007f\u2028\u2029]+/g, ' ').trim()
109+
// C0/C1 controls (NEL breaks a line too), line and paragraph separators, and bidi controls,
110+
// which could reorder "current → new".
111+
return value
112+
.replace(
113+
/[\u0000-\u001f\u007f-\u009f\u061c\u200e\u200f\u2028\u2029\u202a-\u202e\u2066-\u2069]+/g,
114+
' '
115+
)
116+
.trim()
117+
}
118+
119+
/**
120+
* A new date or time as the helper will save it. A time with an offset keeps that offset, which names
121+
* exactly one moment even in an hour a daylight-saving change repeats, and adds this Mac's local time,
122+
* the form the current value is shown in.
123+
*/
124+
function localTime(value: string): string {
125+
if (!/T.*(Z|[+-]\d{2}:\d{2})$/.test(value)) return value
126+
const date = new Date(value)
127+
if (Number.isNaN(date.getTime())) return value
128+
const pad = (n: number) => String(n).padStart(2, '0')
129+
const local = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}T${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`
130+
return `${value} (${local} here)`
110131
}
111132

112133
/**
@@ -136,7 +157,7 @@ function reminderChanges(
136157
const changes: Change[] = []
137158
const title = command.title?.trim()
138159
if (title !== undefined && title !== current.title) changes.push(['Title', current.title, title])
139-
if (command.due !== undefined) changes.push(['Due', current.due, command.due])
160+
if (command.due !== undefined) changes.push(['Due', current.due, localTime(command.due)])
140161
if (command.clearDue) changes.push(['Due', current.due, 'none'])
141162
if (command.priority !== undefined)
142163
changes.push(['Priority', priority(current.priority), priority(command.priority)])
@@ -158,8 +179,8 @@ function eventChanges(
158179
const changes: Change[] = []
159180
const title = command.title?.trim()
160181
if (title !== undefined && title !== current.title) changes.push(['Title', current.title, title])
161-
if (command.start !== undefined) changes.push(['Starts', current.start, command.start])
162-
if (command.end !== undefined) changes.push(['Ends', current.end, command.end])
182+
if (command.start !== undefined) changes.push(['Starts', current.start, localTime(command.start)])
183+
if (command.end !== undefined) changes.push(['Ends', current.end, localTime(command.end)])
163184
if (command.location !== undefined)
164185
changes.push(['Location', current.location, command.location.trim() || 'none'])
165186
if (command.notes !== undefined)
@@ -618,6 +639,16 @@ export class ComputerUseService {
618639
message: 'This event repeats; change it in Calendar so the user picks which occurrences.',
619640
dispatchState: 'not_started',
620641
})
642+
// An all-day event takes dates and a timed one takes times; a mismatch is refused before asking.
643+
for (const boundary of [command.start, command.end])
644+
if (boundary !== undefined && boundary.includes('T') === current.allDay)
645+
throw new ComputerUseError({
646+
code: 'invalid_arguments',
647+
message: current.allDay
648+
? 'This is an all-day event; pass start and end as YYYY-MM-DD.'
649+
: 'This event has times; pass start and end as ISO 8601 date-times.',
650+
dispatchState: 'not_started',
651+
})
621652
return {
622653
summary: changeSummary(
623654
[

‎packages/desktop-bridge/src/computer-use.generated.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ const DateOrDateTime = z
251251
"Use a real date and time",
252252
);
253253
const CommandNotes = z.string().max(8000).optional();
254+
const UpdatedEventBoundary = DateOrDateTime.describe(
255+
"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.",
256+
);
254257
const EventBoundary = DateOrDateTime.describe(
255258
"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.",
256259
);
@@ -359,8 +362,8 @@ export const AppCommandSchema = z.discriminatedUnion("name", [
359362
.max(256)
360363
.describe("An id from find_events or create_event; repeating events are refused."),
361364
title: CommandTitle.optional(),
362-
start: EventBoundary.optional(),
363-
end: EventBoundary.optional(),
365+
start: UpdatedEventBoundary.optional(),
366+
end: UpdatedEventBoundary.optional(),
364367
location: z.string().max(1024).optional().describe("Replaces the location; an empty string clears it."),
365368
notes: CommandNotes.describe("Replaces the notes; an empty string clears them."),
366369
})

0 commit comments

Comments
 (0)