car-server-core 0.55.0

Transport-neutral library for the CAR daemon JSON-RPC dispatcher (used by car-server and tokhn-daemon)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//! Local macOS Calendar tools for the built-in assistant.
//!
//! These tools use the existing EventKit integration. They do not depend on a
//! Parslee session or a connected Microsoft account. Runtime advertisement is
//! restricted to macOS with an already-granted full Calendar permission; a
//! stale transcript that calls one after permission changes gets an actionable
//! error instead of accidentally prompting or falling through to another tool.

use std::sync::Arc;

use async_trait::async_trait;
use car_engine::ToolExecutor;
use serde_json::{json, Value};

const MUTATION_TIER: &str = "full_access";
const CALENDAR_PERMISSION_FIX: &str =
    "Open System Settings > Privacy & Security > Calendars, allow CAR/CarHost full access, then retry.";

trait CalendarBackend: Send + Sync {
    fn permission_status(&self) -> Result<String, String>;
    fn calendars(&self) -> Result<Value, String>;
    fn events(
        &self,
        start: chrono::DateTime<chrono::Utc>,
        end: chrono::DateTime<chrono::Utc>,
        calendar_ids: &[String],
    ) -> Result<Value, String>;
    fn create_event(&self, input: &Value) -> Result<Value, String>;
    fn update_event(&self, input: &Value) -> Result<Value, String>;
    fn delete_event(&self, event_id: &str) -> Result<Value, String>;
}

struct EventKitBackend;

impl CalendarBackend for EventKitBackend {
    fn permission_status(&self) -> Result<String, String> {
        let status = car_ffi_common::permissions::status("calendar", None)?;
        status
            .get("status")
            .and_then(Value::as_str)
            .map(str::to_string)
            .ok_or_else(|| "Calendar permission probe returned no status".to_string())
    }

    fn calendars(&self) -> Result<Value, String> {
        car_ffi_common::integrations::calendar_list()
    }

    fn events(
        &self,
        start: chrono::DateTime<chrono::Utc>,
        end: chrono::DateTime<chrono::Utc>,
        calendar_ids: &[String],
    ) -> Result<Value, String> {
        car_ffi_common::integrations::calendar_events(start, end, calendar_ids)
    }

    fn create_event(&self, input: &Value) -> Result<Value, String> {
        car_ffi_common::integrations::calendar_create_event(&input.to_string())
    }

    fn update_event(&self, input: &Value) -> Result<Value, String> {
        car_ffi_common::integrations::calendar_update_event(&input.to_string())
    }

    fn delete_event(&self, event_id: &str) -> Result<Value, String> {
        car_ffi_common::integrations::calendar_delete_event(event_id)
    }
}

/// Model-facing access to the current Mac user's local Calendar.app data.
pub struct CalendarTools {
    backend: Arc<dyn CalendarBackend>,
    macos: bool,
}

impl CalendarTools {
    pub fn new() -> Self {
        Self {
            backend: Arc::new(EventKitBackend),
            macos: cfg!(target_os = "macos"),
        }
    }

    #[cfg(test)]
    fn with_backend(backend: Arc<dyn CalendarBackend>, macos: bool) -> Self {
        Self { backend, macos }
    }

    /// Advertise only when the host and its non-prompting TCC probe say the
    /// local EventKit backend is ready.
    pub fn tool_defs(&self) -> Vec<Value> {
        if self.macos
            && self
                .backend
                .permission_status()
                .is_ok_and(|status| status == "granted")
        {
            calendar_tool_defs()
        } else {
            Vec::new()
        }
    }

    fn require_permission(&self) -> Result<(), String> {
        if !self.macos {
            return Err("local Calendar.app tools are available only on macOS".to_string());
        }
        let status = self
            .backend
            .permission_status()
            .unwrap_or_else(|_| "unknown".to_string());
        if status == "granted" {
            return Ok(());
        }
        Err(format!(
            "Calendar access is {status}; local calendar tools cannot run. {CALENDAR_PERMISSION_FIX}"
        ))
    }

    fn events(&self, params: &Value) -> Result<Value, String> {
        let start = parse_rfc3339(params, "start")?;
        let end = parse_rfc3339(params, "end")?;
        if end <= start {
            return Err("calendar_events requires `end` after `start`".to_string());
        }
        let calendar_ids = optional_strings(params, "calendar_ids")?;
        self.backend.events(start, end, &calendar_ids)
    }

    fn create_event(&self, params: &Value) -> Result<Value, String> {
        let mut input = params.clone();
        let object = input
            .as_object_mut()
            .ok_or_else(|| "calendar_create_event parameters must be an object".to_string())?;
        let needs_calendar = object
            .get("calendar_id")
            .and_then(Value::as_str)
            .map(str::trim)
            .is_none_or(str::is_empty);
        if needs_calendar {
            let listing = self.backend.calendars()?;
            let calendar_id = listing
                .get("calendars")
                .and_then(Value::as_array)
                .and_then(|calendars| {
                    calendars.iter().find_map(|calendar| {
                        (calendar.get("writable").and_then(Value::as_bool) == Some(true))
                            .then(|| calendar.get("id").and_then(Value::as_str))
                            .flatten()
                    })
                })
                .ok_or_else(|| {
                    "no writable local calendar is available; create or enable one in Calendar.app"
                        .to_string()
                })?;
            object.insert("calendar_id".to_string(), json!(calendar_id));
        }
        let mut result = self.backend.create_event(&input)?;
        ensure_event_identifier(&mut result, "calendar_create_event")?;
        Ok(result)
    }

    fn update_event(&self, params: &Value) -> Result<Value, String> {
        let mut result = self.backend.update_event(params)?;
        ensure_event_identifier(&mut result, "calendar_update_event")?;
        Ok(result)
    }

    fn delete_event(&self, params: &Value) -> Result<Value, String> {
        let event_id = required_string(params, "event_id")?;
        let mut result = self.backend.delete_event(event_id)?;
        if result.get("ok").and_then(Value::as_bool) == Some(true) {
            let object = result.as_object_mut().ok_or_else(|| {
                "calendar_delete_event backend returned a non-object result".to_string()
            })?;
            object.insert("event_id".to_string(), json!(event_id));
        }
        Ok(result)
    }
}

impl Default for CalendarTools {
    fn default() -> Self {
        Self::new()
    }
}

/// All local-calendar schemas, including those unavailable on this host.
pub(super) fn calendar_tool_defs() -> Vec<Value> {
    vec![
        json!({
            "name": "calendar_events",
            "description": "Read events from the current Mac user's local Calendar.app through EventKit. This is the private on-device path: it does not use Parslee or require a Microsoft 365 connection. Pass an RFC3339 range; results include stable event and calendar ids for later updates or deletion. After answering a guided schedule check, end with a concrete offer to add or move an event; do not make the change until the user accepts the approval-gated action.",
            "parameters": {
                "type": "object",
                "properties": {
                    "start": { "type": "string", "description": "Inclusive range start as RFC3339 with an offset." },
                    "end": { "type": "string", "description": "Exclusive range end as RFC3339 with an offset." },
                    "calendar_ids": { "type": "array", "items": { "type": "string" }, "description": "Optional calendar ids to include; omit for every accessible calendar." }
                },
                "required": ["start", "end"],
                "additionalProperties": false
            }
        }),
        json!({
            "name": "calendar_create_event",
            "description": "Create an event in the current Mac user's local Calendar.app through EventKit. This changes on-device calendar data and requires approval unless the session has full access. Omit calendar_id to use the first writable local calendar. Returns the created event with its stable id.",
            "parameters": {
                "type": "object",
                "properties": {
                    "calendar_id": { "type": "string", "description": "Writable local calendar id. Optional; omit to choose the first writable calendar." },
                    "title": { "type": "string" },
                    "start": { "type": "string", "description": "RFC3339 start time with an offset." },
                    "end": { "type": "string", "description": "RFC3339 end time with an offset." },
                    "all_day": { "type": "boolean" },
                    "notes": { "type": "string" },
                    "location": { "type": "string" },
                    "url": { "type": "string" }
                },
                "required": ["title", "start", "end"],
                "additionalProperties": false
            },
            "mutating": true,
            "tier": MUTATION_TIER
        }),
        json!({
            "name": "calendar_update_event",
            "description": "Update an event in the current Mac user's local Calendar.app through EventKit. Use the stable event_id returned by calendar_events or calendar_create_event. This changes on-device calendar data and requires approval unless the session has full access.",
            "parameters": {
                "type": "object",
                "properties": {
                    "event_id": { "type": "string" },
                    "title": { "type": "string" },
                    "start": { "type": "string", "description": "RFC3339 start time with an offset." },
                    "end": { "type": "string", "description": "RFC3339 end time with an offset." },
                    "all_day": { "type": "boolean" },
                    "notes": { "type": "string", "description": "Empty string clears the field." },
                    "location": { "type": "string", "description": "Empty string clears the field." },
                    "url": { "type": "string", "description": "Empty string clears the field." }
                },
                "required": ["event_id"],
                "additionalProperties": false
            },
            "mutating": true,
            "tier": MUTATION_TIER
        }),
        json!({
            "name": "calendar_delete_event",
            "description": "Delete an event from the current Mac user's local Calendar.app through EventKit. Use the stable event_id returned by calendar_events. This changes on-device calendar data and requires approval unless the session has full access; success echoes event_id.",
            "parameters": {
                "type": "object",
                "properties": { "event_id": { "type": "string" } },
                "required": ["event_id"],
                "additionalProperties": false
            },
            "mutating": true,
            "tier": MUTATION_TIER
        }),
    ]
}

#[async_trait]
impl ToolExecutor for CalendarTools {
    async fn execute(&self, tool: &str, params: &Value) -> Result<Value, String> {
        match tool {
            "calendar_events" => {
                self.require_permission()?;
                self.events(params)
            }
            "calendar_create_event" => {
                self.require_permission()?;
                self.create_event(params)
            }
            "calendar_update_event" => {
                self.require_permission()?;
                self.update_event(params)
            }
            "calendar_delete_event" => {
                self.require_permission()?;
                self.delete_event(params)
            }
            other => Err(format!("unknown tool: '{other}'")),
        }
    }
}

fn parse_rfc3339(params: &Value, field: &str) -> Result<chrono::DateTime<chrono::Utc>, String> {
    let raw = required_string(params, field)?;
    chrono::DateTime::parse_from_rfc3339(raw)
        .map(|value| value.with_timezone(&chrono::Utc))
        .map_err(|error| format!("calendar {field} must be RFC3339: {error}"))
}

fn required_string<'a>(params: &'a Value, field: &str) -> Result<&'a str, String> {
    params
        .get(field)
        .and_then(Value::as_str)
        .map(str::trim)
        .filter(|value| !value.is_empty())
        .ok_or_else(|| format!("calendar tool requires non-empty `{field}`"))
}

fn optional_strings(params: &Value, field: &str) -> Result<Vec<String>, String> {
    match params.get(field) {
        None => Ok(Vec::new()),
        Some(Value::Array(values)) => values
            .iter()
            .map(|value| {
                value
                    .as_str()
                    .map(str::trim)
                    .filter(|value| !value.is_empty())
                    .map(str::to_string)
                    .ok_or_else(|| format!("calendar `{field}` must contain non-empty strings"))
            })
            .collect(),
        Some(_) => Err(format!("calendar `{field}` must be an array of strings")),
    }
}

fn ensure_event_identifier(result: &mut Value, tool: &str) -> Result<(), String> {
    if result.get("ok").and_then(Value::as_bool) != Some(true) {
        return Ok(());
    }
    if result
        .get("event")
        .and_then(|event| event.get("id"))
        .and_then(Value::as_str)
        .is_some_and(|id| !id.is_empty())
    {
        return Ok(());
    }
    Err(format!(
        "{tool} succeeded without the stable event id required for follow-up actions"
    ))
}

#[cfg(test)]
mod tests {
    use std::sync::Mutex;

    use super::*;

    struct MockCalendarBackend {
        status: Result<String, String>,
        calls: Mutex<Vec<(String, Value)>>,
    }

    impl MockCalendarBackend {
        fn granted() -> Arc<Self> {
            Arc::new(Self {
                status: Ok("granted".to_string()),
                calls: Mutex::new(Vec::new()),
            })
        }

        fn with_status(status: &str) -> Arc<Self> {
            Arc::new(Self {
                status: Ok(status.to_string()),
                calls: Mutex::new(Vec::new()),
            })
        }
    }

    impl CalendarBackend for MockCalendarBackend {
        fn permission_status(&self) -> Result<String, String> {
            self.status.clone()
        }

        fn calendars(&self) -> Result<Value, String> {
            Ok(json!({
                "available": true,
                "backend": "mock_eventkit",
                "calendars": [
                    {"id": "read-only", "title": "Holidays", "writable": false},
                    {"id": "cal-local", "title": "Personal", "writable": true}
                ]
            }))
        }

        fn events(
            &self,
            start: chrono::DateTime<chrono::Utc>,
            end: chrono::DateTime<chrono::Utc>,
            calendar_ids: &[String],
        ) -> Result<Value, String> {
            self.calls.lock().unwrap().push((
                "events".to_string(),
                json!({"start": start, "end": end, "calendar_ids": calendar_ids}),
            ));
            Ok(json!({
                "available": true,
                "backend": "mock_eventkit",
                "events": [{"id": "event-7", "calendar_id": "cal-local", "title": "Review"}]
            }))
        }

        fn create_event(&self, input: &Value) -> Result<Value, String> {
            self.calls
                .lock()
                .unwrap()
                .push(("create".to_string(), input.clone()));
            Ok(
                json!({"ok": true, "event": {"id": "event-created", "calendar_id": input["calendar_id"]}}),
            )
        }

        fn update_event(&self, input: &Value) -> Result<Value, String> {
            self.calls
                .lock()
                .unwrap()
                .push(("update".to_string(), input.clone()));
            Ok(json!({"ok": true, "event": {"id": input["event_id"]}}))
        }

        fn delete_event(&self, event_id: &str) -> Result<Value, String> {
            self.calls
                .lock()
                .unwrap()
                .push(("delete".to_string(), json!({"event_id": event_id})));
            Ok(json!({"ok": true}))
        }
    }

    #[test]
    fn schemas_assign_read_and_approval_tiers() {
        let defs = calendar_tool_defs();
        let names: Vec<&str> = defs.iter().filter_map(|def| def["name"].as_str()).collect();
        assert_eq!(
            names,
            [
                "calendar_events",
                "calendar_create_event",
                "calendar_update_event",
                "calendar_delete_event"
            ]
        );
        assert!(defs[0].get("tier").is_none(), "reads are ungated");
        assert!(defs[0].get("mutating").is_none());
        assert!(defs[0]["description"]
            .as_str()
            .is_some_and(|description| description.contains("offer to add or move an event")));
        for def in &defs[1..] {
            assert_eq!(def["tier"], MUTATION_TIER);
            assert_eq!(def["mutating"], true);
        }
        assert!(defs.iter().all(|def| def["description"]
            .as_str()
            .is_some_and(|description| description.contains("local Calendar.app"))));
    }

    #[test]
    fn calendar_mutations_gate_until_the_session_has_full_access() {
        let defs = calendar_tool_defs();
        let gated = super::super::tier_gated_tool_names(
            &defs,
            car_policy::permission::PermissionTier::ReadOnly,
        );
        assert_eq!(
            gated,
            [
                "calendar_create_event",
                "calendar_update_event",
                "calendar_delete_event"
            ]
        );
        assert!(super::super::tier_gated_tool_names(
            &defs,
            car_policy::permission::PermissionTier::FullAccess,
        )
        .is_empty());
    }

    #[test]
    fn advertisement_requires_macos_and_granted_permission() {
        let granted: Arc<dyn CalendarBackend> = MockCalendarBackend::granted();
        assert_eq!(
            CalendarTools::with_backend(granted, true).tool_defs().len(),
            4
        );

        let denied: Arc<dyn CalendarBackend> = MockCalendarBackend::with_status("denied");
        assert!(CalendarTools::with_backend(denied, true)
            .tool_defs()
            .is_empty());

        let off_platform: Arc<dyn CalendarBackend> = MockCalendarBackend::granted();
        assert!(CalendarTools::with_backend(off_platform, false)
            .tool_defs()
            .is_empty());
    }

    #[tokio::test]
    async fn unavailable_permission_returns_actionable_error_for_stale_calls() {
        let backend: Arc<dyn CalendarBackend> = MockCalendarBackend::with_status("denied");
        let tools = CalendarTools::with_backend(backend, true);
        let error = tools
            .execute(
                "calendar_events",
                &json!({"start": "2026-09-18T09:00:00Z", "end": "2026-09-18T10:00:00Z"}),
            )
            .await
            .unwrap_err();
        assert!(error.contains("Calendar access is denied"), "{error}");
        assert!(error.contains("System Settings"), "{error}");
        assert!(error.contains("Calendars"), "{error}");
    }

    #[tokio::test]
    async fn read_uses_backend_and_preserves_stable_event_id() {
        let backend = MockCalendarBackend::granted();
        let tools = CalendarTools::with_backend(backend.clone(), true);
        let result = tools
            .execute(
                "calendar_events",
                &json!({
                    "start": "2026-09-18T09:00:00Z",
                    "end": "2026-09-18T10:00:00Z",
                    "calendar_ids": ["cal-local"]
                }),
            )
            .await
            .unwrap();
        assert_eq!(result["events"][0]["id"], "event-7");
        assert_eq!(backend.calls.lock().unwrap()[0].0, "events");
    }

    #[tokio::test]
    async fn create_selects_writable_calendar_and_returns_event_id() {
        let backend = MockCalendarBackend::granted();
        let tools = CalendarTools::with_backend(backend.clone(), true);
        let result = tools
            .execute(
                "calendar_create_event",
                &json!({
                    "title": "Planning",
                    "start": "2026-09-18T09:00:00Z",
                    "end": "2026-09-18T09:30:00Z"
                }),
            )
            .await
            .unwrap();
        assert_eq!(result["event"]["id"], "event-created");
        let calls = backend.calls.lock().unwrap();
        assert_eq!(calls[0].0, "create");
        assert_eq!(calls[0].1["calendar_id"], "cal-local");
    }

    #[tokio::test]
    async fn update_and_delete_echo_stable_identifiers() {
        let backend = MockCalendarBackend::granted();
        let tools = CalendarTools::with_backend(backend, true);
        let updated = tools
            .execute(
                "calendar_update_event",
                &json!({"event_id": "event-7", "title": "Renamed"}),
            )
            .await
            .unwrap();
        assert_eq!(updated["event"]["id"], "event-7");
        let deleted = tools
            .execute("calendar_delete_event", &json!({"event_id": "event-7"}))
            .await
            .unwrap();
        assert_eq!(deleted["event_id"], "event-7");
    }

    #[tokio::test]
    async fn unknown_tool_falls_through() {
        let backend: Arc<dyn CalendarBackend> = MockCalendarBackend::granted();
        let error = CalendarTools::with_backend(backend, true)
            .execute("mail_inbox", &json!({}))
            .await
            .unwrap_err();
        assert!(error.starts_with("unknown tool"), "{error}");
    }
}