Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(calendar): check public MCP code-mode runs for the copy they change (#8756)

gbirman3e5b59fe9c50
diff --git a/crates/calendar_events/AGENTS.md b/crates/calendar_events/AGENTS.md
index f14d2be32b..772330edc1 100644
--- a/crates/calendar_events/AGENTS.md
+++ b/crates/calendar_events/AGENTS.md
@@ -168,13 +168,15 @@ reminder dispatch, team availability, and provider/Postgres persistence.
(`meeting_copies`, matched by iCalendar UID) is refused, listing each copy,
until `inbox` names the addressed copy's inbox. UpdateCalendarEvent and
DeleteCalendarEvent check every call; the update, invite-guests and delete
- routes check agent runs (a token with `agent_id`, as code mode mints) and any
- request naming `inbox` through `CalendarCopyConfirmation`, answering 409
- `copy_choice_required`. Code-mode agents never see the calendar tools
- (`agent_code_mode::covered_by_code_sdk`), so the routes and the SDK docs are
- what reach them; the web, which opens one copy, sends no `inbox` and is not
- checked. An Outlook copy of a Google meeting carries Exchange's UID, so it is
- not matched.
+ routes check agent runs and any request naming `inbox` through
+ `CalendarCopyConfirmation`, answering 409 `copy_choice_required`. An agent
+ run is a token with `agent_id` (in-app code mode) or a request carrying
+ `x-macro-code-execution`, which `code_execution`'s bootstrap sets on every
+ code-mode program's SDK requests, public MCP clients' included. Code-mode
+ agents never see the calendar tools (`agent_code_mode::covered_by_code_sdk`),
+ so the routes and the SDK docs are what reach them; the web, which opens one
+ copy, sends no `inbox` and is not checked. An Outlook copy of a Google
+ meeting carries Exchange's UID, so it is not matched.
- Preserve the conservative OOO refresh hint on write outcomes and sync reports;
its contract is in [team sharing](../../docs/design/calendar-team-sharing.md).
- The team out-of-office overlay (`src/outbound/pg_team/ooo.rs`) starts from
diff --git a/crates/calendar_events/src/inbound/mutation_router.rs b/crates/calendar_events/src/inbound/mutation_router.rs
index 69bac745d3..0c74574b74 100644
--- a/crates/calendar_events/src/inbound/mutation_router.rs
+++ b/crates/calendar_events/src/inbound/mutation_router.rs
@@ -12,7 +12,7 @@ use std::sync::Arc;
use axum::{
Json, Router,
extract::{FromRef, Path, Query, State},
- http::StatusCode,
+ http::{HeaderMap, StatusCode},
response::{IntoResponse, Response},
routing::{get, patch, post, put},
};
@@ -459,9 +459,20 @@ impl From<CopyRefusal> for CalendarMutationApiError {
}
}
+/// Set by the code-mode runtime (`code_execution`'s `bootstrap.mjs`) on every
+/// authenticated SDK request a program makes, in agent sessions and for
+/// public MCP clients alike.
+const CODE_EXECUTION_HEADER: &str = "x-macro-code-execution";
+
+/// Whether a request comes from an agent's program rather than a person in
+/// the app: a code-mode run, or a token carrying an agent's attribution.
+fn is_agent_run(agent_attributed: bool, headers: &HeaderMap) -> bool {
+ agent_attributed || headers.contains_key(CODE_EXECUTION_HEADER)
+}
+
/// Make an agent run name the copy it changes when the meeting is on several
/// of the requester's inboxes. The web picks a copy by opening it, so a
-/// request without an agent identity and without `inbox` is not checked; a
+/// request that is not an agent run and names no `inbox` is not checked; a
/// request naming `inbox` is checked whoever sends it.
async fn confirm_copy<S: CalendarCopyConfirmation>(
service: &S,
@@ -769,6 +780,7 @@ fn update_scope(
pub async fn update_calendar_event<S, L, Auth>(
State(state): State<CalendarMutationRouterState<S, L, Auth>>,
user: MacroAuthorizationExtractor<Auth, UserOrInternal>,
+ headers: HeaderMap,
Path(event_id): Path<Uuid>,
Json(request): Json<UpdateCalendarEventRequest>,
) -> Result<Json<CalendarEvent>, CalendarMutationApiError>
@@ -801,7 +813,7 @@ where
confirm_copy(
&*state.service,
requester_id,
- user.authorization.user.agent_id.is_some(),
+ is_agent_run(user.authorization.user.agent_id.is_some(), &headers),
event_id,
request.inbox.as_deref(),
CopyChange::Update {
@@ -839,6 +851,7 @@ where
pub async fn delete_calendar_event<S, L, Auth>(
State(state): State<CalendarMutationRouterState<S, L, Auth>>,
user: MacroAuthorizationExtractor<Auth, UserOrInternal>,
+ headers: HeaderMap,
Path(event_id): Path<Uuid>,
Query(query): Query<DeleteCalendarEventQuery>,
) -> Result<StatusCode, CalendarMutationApiError>
@@ -867,7 +880,7 @@ where
confirm_copy(
&*state.service,
requester_id,
- user.authorization.user.agent_id.is_some(),
+ is_agent_run(user.authorization.user.agent_id.is_some(), &headers),
event_id,
query.inbox.as_deref(),
CopyChange::Delete,
@@ -957,6 +970,7 @@ where
pub async fn invite_calendar_event_guests<S, L, Auth>(
State(state): State<CalendarMutationRouterState<S, L, Auth>>,
user: MacroAuthorizationExtractor<Auth, UserOrInternal>,
+ headers: HeaderMap,
Path(event_id): Path<Uuid>,
Json(request): Json<InviteCalendarEventGuestsRequest>,
) -> Result<Json<CalendarEvent>, CalendarMutationApiError>
@@ -981,7 +995,7 @@ where
confirm_copy(
&*state.service,
requester_id,
- user.authorization.user.agent_id.is_some(),
+ is_agent_run(user.authorization.user.agent_id.is_some(), &headers),
event_id,
request.inbox.as_deref(),
CopyChange::Update { adds_guests: true },
diff --git a/crates/calendar_events/src/inbound/mutation_router/test.rs b/crates/calendar_events/src/inbound/mutation_router/test.rs
index f19dde4e17..d7ee0e0c37 100644
--- a/crates/calendar_events/src/inbound/mutation_router/test.rs
+++ b/crates/calendar_events/src/inbound/mutation_router/test.rs
@@ -225,3 +225,14 @@ fn a_copy_refusal_asks_the_caller_to_name_the_inbox() {
assert_eq!(error.code, CalendarMutationErrorCode::CopyChoiceRequired);
assert_eq!(error.message, refusal.message());
}
+
+#[test]
+fn a_code_mode_program_or_an_agent_token_counts_as_an_agent_run() {
+ let mut program = axum::http::HeaderMap::new();
+ program.insert(CODE_EXECUTION_HEADER, "1".parse().unwrap());
+ let browser = axum::http::HeaderMap::new();
+
+ assert!(is_agent_run(false, &program), "a public MCP code-mode run");
+ assert!(is_agent_run(true, &browser), "an agent-attributed token");
+ assert!(!is_agent_run(false, &browser), "a person in the app");
+}
diff --git a/crates/code_execution/AGENTS.md b/crates/code_execution/AGENTS.md
index 9f863ec507..d881f91faf 100644
--- a/crates/code_execution/AGENTS.md
+++ b/crates/code_execution/AGENTS.md
@@ -20,6 +20,11 @@ proxy or host-call bridge. Cancellation stops the process; it does not roll back
completed API writes. SDK and Handlebars bundles are generated by
`packages/code-mode-sdk`, never edited by hand.
+Authenticated SDK requests carry `x-macro-code-execution: 1`, the only mark a
+run under a plain user token (public MCP) has. APIs that ask a program to
+confirm what a person picks by opening something read it; the calendar
+mutation routes demand the copy's `inbox` from such requests.
+
## Checks
- `cargo test -p code_execution --lib -- --include-ignored` from the repository
diff --git a/crates/code_execution/README.md b/crates/code_execution/README.md
index 4aac6b1b04..9bf159f99f 100644
--- a/crates/code_execution/README.md
+++ b/crates/code_execution/README.md
@@ -72,8 +72,9 @@ Timeout, cancellation, a dropped caller, and harness shutdown kill and reap the
child before deleting scratch. API writes already accepted by a service may
complete afterward and must not be blindly retried. SDK operation events carry only an operation name, request identity and status.
They are bounded diagnostics, not proof of a committed action. Console output
-and progress are program-authored and may be incomplete. W3C trace context is
-forwarded to configured service origins on authenticated requests only.
+and progress are program-authored and may be incomplete. W3C trace context and
+`x-macro-code-execution: 1` are sent to configured service origins on
+authenticated requests only.
Deno shares the harness container. The heap setting does not hard-limit native
memory, ArrayBuffers, or CPU; a container OOM can affect the harness and other
diff --git a/crates/code_execution/src/outbound/deno/bootstrap.mjs b/crates/code_execution/src/outbound/deno/bootstrap.mjs
index 4f009a5a37..0d282c6fde 100644
--- a/crates/code_execution/src/outbound/deno/bootstrap.mjs
+++ b/crates/code_execution/src/outbound/deno/bootstrap.mjs
@@ -32,6 +32,9 @@ try {
for (const [name, value] of Object.entries(context.traceHeaders ?? {})) {
if (name === "traceparent" || name === "tracestate") request.headers.set(name, value);
}
+ // Marks the request as a program's, not a person's in the app, so an API
+ // can ask it to confirm what a person would have chosen by opening it.
+ request.headers.set("x-macro-code-execution", "1");
}
return originalFetch(request);
};
diff --git a/crates/code_execution/src/outbound/deno/test.rs b/crates/code_execution/src/outbound/deno/test.rs
index af8f68f2ed..d4b8ba59de 100644
--- a/crates/code_execution/src/outbound/deno/test.rs
+++ b/crates/code_execution/src/outbound/deno/test.rs
@@ -109,6 +109,12 @@ async fn real_sdk_nested_calls_are_authenticated_and_concurrent() {
"traceparent: 00-01234567890123456789012345678901-0123456789012345-01"
));
assert!(request.starts_with("POST /decisions "), "{request}");
+ assert!(
+ request
+ .to_ascii_lowercase()
+ .contains("x-macro-code-execution: 1\r\n"),
+ "{request}"
+ );
assert!(
request
.to_ascii_lowercase()