everruns-integrations-cursor 0.17.4

Cursor Cloud Agents integration for Everruns
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
//! Cursor Cloud Agents tool implementations.

use async_trait::async_trait;
use everruns_core::ToolHints;
use everruns_core::tools::{Tool, ToolExecutionResult};
use everruns_core::traits::ToolContext;
use serde_json::{Value, json};
use tracing::{debug, error};

use crate::client::{CursorClient, LaunchAgentRequest};
use crate::{CURSOR_API_BASE_ENV, CURSOR_API_KEY_SECRET, CURSOR_CONNECTION_PROVIDER};

const MAX_PROMPT_CHARS: usize = 20_000;
const MAX_AGENT_ID_CHARS: usize = 128;
const MAX_REF_CHARS: usize = 255;
const MAX_BRANCH_CHARS: usize = 255;
const MAX_MODEL_CHARS: usize = 128;

async fn get_api_key(context: &ToolContext) -> Result<String, ToolExecutionResult> {
    if let Some(resolver) = context.connection_resolver.as_ref() {
        match resolver
            .get_connection_token(context.session_id, CURSOR_CONNECTION_PROVIDER)
            .await
        {
            Ok(Some(token)) if !token.trim().is_empty() => return Ok(token),
            Ok(_) => {}
            Err(e) => debug!("Cursor connection resolver failed: {e}"),
        }
    }

    if let Some(storage) = context.storage_store.as_ref() {
        match storage
            .get_secret(context.session_id, CURSOR_API_KEY_SECRET)
            .await
        {
            Ok(Some(key)) if !key.trim().is_empty() => return Ok(key),
            Ok(_) => {}
            Err(e) => {
                error!("Failed to read {CURSOR_API_KEY_SECRET} session secret: {e}");
                return Err(ToolExecutionResult::internal_error_msg(
                    "Failed to read Cursor API key",
                ));
            }
        }
    }

    // THREAT[TM-CURSOR-001]: Avoid asking for API keys in chat where they are
    // stored in plaintext messages. Signal the connection dialog instead.
    Err(ToolExecutionResult::connection_required(
        CURSOR_CONNECTION_PROVIDER,
    ))
}

fn cursor_client(api_key: String) -> CursorClient {
    match std::env::var(CURSOR_API_BASE_ENV) {
        Ok(api_base) if !api_base.trim().is_empty() => {
            CursorClient::with_base_url(api_key, api_base)
        }
        _ => CursorClient::new(api_key),
    }
}

fn required_str<'a>(arguments: &'a Value, name: &str) -> Result<&'a str, ToolExecutionResult> {
    arguments
        .get(name)
        .and_then(|v| v.as_str())
        .map(str::trim)
        .filter(|v| !v.is_empty())
        .ok_or_else(|| {
            ToolExecutionResult::tool_error(format!("Missing required parameter: {name}"))
        })
}

fn optional_str<'a>(
    arguments: &'a Value,
    name: &str,
    max_chars: usize,
) -> Result<Option<&'a str>, ToolExecutionResult> {
    match arguments.get(name) {
        Some(Value::String(value)) => {
            let trimmed = value.trim();
            if trimmed.is_empty() {
                Ok(None)
            } else if trimmed.chars().count() > max_chars {
                Err(ToolExecutionResult::tool_error(format!(
                    "Invalid '{name}': maximum length is {max_chars} characters"
                )))
            } else {
                Ok(Some(trimmed))
            }
        }
        Some(Value::Null) | None => Ok(None),
        Some(_) => Err(ToolExecutionResult::tool_error(format!(
            "Invalid '{name}': must be a string"
        ))),
    }
}

fn required_limited_str<'a>(
    arguments: &'a Value,
    name: &str,
    max_chars: usize,
) -> Result<&'a str, ToolExecutionResult> {
    let value = required_str(arguments, name)?;
    if value.chars().count() > max_chars {
        return Err(ToolExecutionResult::tool_error(format!(
            "Invalid '{name}': maximum length is {max_chars} characters"
        )));
    }
    Ok(value)
}

fn optional_bool(arguments: &Value, name: &str) -> Result<Option<bool>, ToolExecutionResult> {
    match arguments.get(name) {
        Some(Value::Bool(value)) => Ok(Some(*value)),
        Some(Value::Null) | None => Ok(None),
        Some(_) => Err(ToolExecutionResult::tool_error(format!(
            "Invalid '{name}': must be a boolean"
        ))),
    }
}

fn optional_limit(arguments: &Value) -> Result<Option<u32>, ToolExecutionResult> {
    match arguments.get("limit") {
        Some(Value::Number(n)) => {
            let Some(value) = n.as_u64() else {
                return Err(ToolExecutionResult::tool_error(
                    "Invalid 'limit': must be an integer between 1 and 100",
                ));
            };
            if !(1..=100).contains(&value) {
                return Err(ToolExecutionResult::tool_error(
                    "Invalid 'limit': must be between 1 and 100",
                ));
            }
            Ok(Some(value as u32))
        }
        Some(Value::Null) | None => Ok(None),
        Some(_) => Err(ToolExecutionResult::tool_error(
            "Invalid 'limit': must be an integer between 1 and 100",
        )),
    }
}

fn context_required_error(name: &str) -> ToolExecutionResult {
    ToolExecutionResult::tool_error(format!(
        "{name} requires context. This tool must be executed with session context."
    ))
}

pub struct CursorLaunchAgentTool;

#[async_trait]
impl Tool for CursorLaunchAgentTool {
    fn name(&self) -> &str {
        "cursor_launch_agent"
    }

    fn display_name(&self) -> Option<&str> {
        Some("Launch Cursor Agent")
    }

    fn description(&self) -> &str {
        "Start a Cursor Cloud Agent to work asynchronously on a GitHub repository."
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "prompt": { "type": "string", "description": "Precise task instructions for the Cursor agent.", "minLength": 1, "maxLength": MAX_PROMPT_CHARS },
                "repository": { "type": "string", "description": "GitHub repository URL, e.g. https://github.com/org/repo.", "minLength": 1 },
                "ref": { "type": "string", "description": "Base branch, tag, or ref. Default is Cursor's repository default branch." },
                "model": { "type": "string", "description": "Optional Cursor model id. Omit for Cursor Auto." },
                "auto_create_pr": { "type": "boolean", "description": "Whether Cursor should create a pull request when the agent finishes. Default false." },
                "branch_name": { "type": "string", "description": "Optional custom branch name for Cursor to create." },
                "name": { "type": "string", "description": "Optional short human-readable task name used only for UI narration." }
            },
            "required": ["prompt", "repository"],
            "additionalProperties": false
        })
    }

    fn hints(&self) -> ToolHints {
        ToolHints::default()
            .with_open_world(true)
            .with_requires_secrets(true)
            .with_long_running(true)
    }

    async fn execute(&self, _arguments: Value) -> ToolExecutionResult {
        context_required_error("cursor_launch_agent")
    }

    fn requires_context(&self) -> bool {
        true
    }

    async fn execute_with_context(
        &self,
        arguments: Value,
        context: &ToolContext,
    ) -> ToolExecutionResult {
        let prompt = match required_limited_str(&arguments, "prompt", MAX_PROMPT_CHARS) {
            Ok(v) => v,
            Err(e) => return e,
        };
        let repository = match required_str(&arguments, "repository") {
            Ok(v) => v,
            Err(e) => return e,
        };
        let ref_ = match optional_str(&arguments, "ref", MAX_REF_CHARS) {
            Ok(v) => v.map(str::to_string),
            Err(e) => return e,
        };
        let model = match optional_str(&arguments, "model", MAX_MODEL_CHARS) {
            Ok(v) => v.map(str::to_string),
            Err(e) => return e,
        };
        let branch_name = match optional_str(&arguments, "branch_name", MAX_BRANCH_CHARS) {
            Ok(v) => v.map(str::to_string),
            Err(e) => return e,
        };
        let auto_create_pr = match optional_bool(&arguments, "auto_create_pr") {
            Ok(v) => v,
            Err(e) => return e,
        };
        let api_key = match get_api_key(context).await {
            Ok(v) => v,
            Err(e) => return e,
        };

        match cursor_client(api_key)
            .launch_agent(LaunchAgentRequest {
                prompt: prompt.to_string(),
                repository: repository.to_string(),
                ref_,
                model,
                auto_create_pr,
                branch_name,
            })
            .await
        {
            Ok(agent) => ToolExecutionResult::success(json!(agent)),
            Err(e) => ToolExecutionResult::tool_error(e),
        }
    }
}

pub struct CursorGetAgentTool;

#[async_trait]
impl Tool for CursorGetAgentTool {
    fn name(&self) -> &str {
        "cursor_get_agent"
    }

    fn display_name(&self) -> Option<&str> {
        Some("Get Cursor Agent")
    }

    fn description(&self) -> &str {
        "Get status and result metadata for a Cursor Cloud Agent."
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "agent_id": { "type": "string", "description": "Cursor agent id, e.g. bc_abc123.", "minLength": 1, "maxLength": MAX_AGENT_ID_CHARS }
            },
            "required": ["agent_id"],
            "additionalProperties": false
        })
    }

    fn hints(&self) -> ToolHints {
        ToolHints::default()
            .with_readonly(true)
            .with_idempotent(true)
            .with_open_world(true)
            .with_requires_secrets(true)
    }

    async fn execute(&self, _arguments: Value) -> ToolExecutionResult {
        context_required_error("cursor_get_agent")
    }

    fn requires_context(&self) -> bool {
        true
    }

    async fn execute_with_context(
        &self,
        arguments: Value,
        context: &ToolContext,
    ) -> ToolExecutionResult {
        let agent_id = match required_limited_str(&arguments, "agent_id", MAX_AGENT_ID_CHARS) {
            Ok(v) => v,
            Err(e) => return e,
        };
        let api_key = match get_api_key(context).await {
            Ok(v) => v,
            Err(e) => return e,
        };
        match cursor_client(api_key).get_agent(agent_id).await {
            Ok(agent) => ToolExecutionResult::success(json!(agent)),
            Err(e) => ToolExecutionResult::tool_error(e),
        }
    }
}

pub struct CursorListAgentsTool;

#[async_trait]
impl Tool for CursorListAgentsTool {
    fn name(&self) -> &str {
        "cursor_list_agents"
    }

    fn display_name(&self) -> Option<&str> {
        Some("List Cursor Agents")
    }

    fn description(&self) -> &str {
        "List Cursor Cloud Agents for the authenticated Cursor account."
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "limit": { "type": "integer", "description": "Number of agents to return, 1-100. Default 20.", "minimum": 1, "maximum": 100 },
                "cursor": { "type": "string", "description": "Pagination cursor from a previous response." }
            },
            "additionalProperties": false
        })
    }

    fn hints(&self) -> ToolHints {
        ToolHints::default()
            .with_readonly(true)
            .with_idempotent(true)
            .with_open_world(true)
            .with_requires_secrets(true)
    }

    async fn execute(&self, _arguments: Value) -> ToolExecutionResult {
        context_required_error("cursor_list_agents")
    }

    fn requires_context(&self) -> bool {
        true
    }

    async fn execute_with_context(
        &self,
        arguments: Value,
        context: &ToolContext,
    ) -> ToolExecutionResult {
        let limit = match optional_limit(&arguments) {
            Ok(v) => v,
            Err(e) => return e,
        };
        let cursor = match optional_str(&arguments, "cursor", MAX_AGENT_ID_CHARS) {
            Ok(v) => v,
            Err(e) => return e,
        };
        let api_key = match get_api_key(context).await {
            Ok(v) => v,
            Err(e) => return e,
        };
        match cursor_client(api_key).list_agents(limit, cursor).await {
            Ok(response) => ToolExecutionResult::success(json!(response)),
            Err(e) => ToolExecutionResult::tool_error(e),
        }
    }
}

pub struct CursorAddFollowupTool;

#[async_trait]
impl Tool for CursorAddFollowupTool {
    fn name(&self) -> &str {
        "cursor_add_followup"
    }

    fn display_name(&self) -> Option<&str> {
        Some("Add Cursor Follow-up")
    }

    fn description(&self) -> &str {
        "Send additional instructions to a running Cursor Cloud Agent."
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "agent_id": { "type": "string", "description": "Cursor agent id.", "minLength": 1, "maxLength": MAX_AGENT_ID_CHARS },
                "prompt": { "type": "string", "description": "Follow-up instruction.", "minLength": 1, "maxLength": MAX_PROMPT_CHARS }
            },
            "required": ["agent_id", "prompt"],
            "additionalProperties": false
        })
    }

    fn hints(&self) -> ToolHints {
        ToolHints::default()
            .with_open_world(true)
            .with_requires_secrets(true)
    }

    async fn execute(&self, _arguments: Value) -> ToolExecutionResult {
        context_required_error("cursor_add_followup")
    }

    fn requires_context(&self) -> bool {
        true
    }

    async fn execute_with_context(
        &self,
        arguments: Value,
        context: &ToolContext,
    ) -> ToolExecutionResult {
        let agent_id = match required_limited_str(&arguments, "agent_id", MAX_AGENT_ID_CHARS) {
            Ok(v) => v,
            Err(e) => return e,
        };
        let prompt = match required_limited_str(&arguments, "prompt", MAX_PROMPT_CHARS) {
            Ok(v) => v,
            Err(e) => return e,
        };
        let api_key = match get_api_key(context).await {
            Ok(v) => v,
            Err(e) => return e,
        };
        match cursor_client(api_key).add_followup(agent_id, prompt).await {
            Ok(response) => ToolExecutionResult::success(response),
            Err(e) => ToolExecutionResult::tool_error(e),
        }
    }
}

pub struct CursorGetConversationTool;

#[async_trait]
impl Tool for CursorGetConversationTool {
    fn name(&self) -> &str {
        "cursor_get_conversation"
    }

    fn display_name(&self) -> Option<&str> {
        Some("Get Cursor Conversation")
    }

    fn description(&self) -> &str {
        "Retrieve the conversation transcript for a Cursor Cloud Agent."
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "agent_id": { "type": "string", "description": "Cursor agent id.", "minLength": 1, "maxLength": MAX_AGENT_ID_CHARS }
            },
            "required": ["agent_id"],
            "additionalProperties": false
        })
    }

    fn hints(&self) -> ToolHints {
        ToolHints::default()
            .with_readonly(true)
            .with_idempotent(true)
            .with_open_world(true)
            .with_requires_secrets(true)
    }

    async fn execute(&self, _arguments: Value) -> ToolExecutionResult {
        context_required_error("cursor_get_conversation")
    }

    fn requires_context(&self) -> bool {
        true
    }

    async fn execute_with_context(
        &self,
        arguments: Value,
        context: &ToolContext,
    ) -> ToolExecutionResult {
        let agent_id = match required_limited_str(&arguments, "agent_id", MAX_AGENT_ID_CHARS) {
            Ok(v) => v,
            Err(e) => return e,
        };
        let api_key = match get_api_key(context).await {
            Ok(v) => v,
            Err(e) => return e,
        };
        match cursor_client(api_key).get_conversation(agent_id).await {
            Ok(response) => ToolExecutionResult::success(json!(response)),
            Err(e) => ToolExecutionResult::tool_error(e),
        }
    }
}

pub struct CursorDeleteAgentTool;

#[async_trait]
impl Tool for CursorDeleteAgentTool {
    fn name(&self) -> &str {
        "cursor_delete_agent"
    }

    fn display_name(&self) -> Option<&str> {
        Some("Delete Cursor Agent")
    }

    fn description(&self) -> &str {
        "Permanently delete a Cursor Cloud Agent record and associated resources."
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "agent_id": { "type": "string", "description": "Cursor agent id.", "minLength": 1, "maxLength": MAX_AGENT_ID_CHARS }
            },
            "required": ["agent_id"],
            "additionalProperties": false
        })
    }

    fn hints(&self) -> ToolHints {
        ToolHints::default()
            .with_destructive(true)
            .with_open_world(true)
            .with_requires_secrets(true)
    }

    async fn execute(&self, _arguments: Value) -> ToolExecutionResult {
        context_required_error("cursor_delete_agent")
    }

    fn requires_context(&self) -> bool {
        true
    }

    async fn execute_with_context(
        &self,
        arguments: Value,
        context: &ToolContext,
    ) -> ToolExecutionResult {
        let agent_id = match required_limited_str(&arguments, "agent_id", MAX_AGENT_ID_CHARS) {
            Ok(v) => v,
            Err(e) => return e,
        };
        let api_key = match get_api_key(context).await {
            Ok(v) => v,
            Err(e) => return e,
        };
        match cursor_client(api_key).delete_agent(agent_id).await {
            Ok(response) => ToolExecutionResult::success(response),
            Err(e) => ToolExecutionResult::tool_error(e),
        }
    }
}

pub struct CursorListModelsTool;

#[async_trait]
impl Tool for CursorListModelsTool {
    fn name(&self) -> &str {
        "cursor_list_models"
    }

    fn display_name(&self) -> Option<&str> {
        Some("List Cursor Models")
    }

    fn description(&self) -> &str {
        "List model ids recommended by Cursor for Cloud Agents."
    }

    fn parameters_schema(&self) -> Value {
        json!({ "type": "object", "properties": {}, "additionalProperties": false })
    }

    fn hints(&self) -> ToolHints {
        ToolHints::default()
            .with_readonly(true)
            .with_idempotent(true)
            .with_open_world(true)
            .with_requires_secrets(true)
    }

    async fn execute(&self, _arguments: Value) -> ToolExecutionResult {
        context_required_error("cursor_list_models")
    }

    fn requires_context(&self) -> bool {
        true
    }

    async fn execute_with_context(
        &self,
        _arguments: Value,
        context: &ToolContext,
    ) -> ToolExecutionResult {
        let api_key = match get_api_key(context).await {
            Ok(v) => v,
            Err(e) => return e,
        };
        match cursor_client(api_key).list_models().await {
            Ok(response) => ToolExecutionResult::success(json!(response)),
            Err(e) => ToolExecutionResult::tool_error(e),
        }
    }
}

pub struct CursorListRepositoriesTool;

#[async_trait]
impl Tool for CursorListRepositoriesTool {
    fn name(&self) -> &str {
        "cursor_list_repositories"
    }

    fn display_name(&self) -> Option<&str> {
        Some("List Cursor Repositories")
    }

    fn description(&self) -> &str {
        "List GitHub repositories accessible to Cursor. This Cursor endpoint is heavily rate-limited; use sparingly."
    }

    fn parameters_schema(&self) -> Value {
        json!({ "type": "object", "properties": {}, "additionalProperties": false })
    }

    fn hints(&self) -> ToolHints {
        ToolHints::default()
            .with_readonly(true)
            .with_open_world(true)
            .with_requires_secrets(true)
            .with_long_running(true)
    }

    async fn execute(&self, _arguments: Value) -> ToolExecutionResult {
        context_required_error("cursor_list_repositories")
    }

    fn requires_context(&self) -> bool {
        true
    }

    async fn execute_with_context(
        &self,
        _arguments: Value,
        context: &ToolContext,
    ) -> ToolExecutionResult {
        let api_key = match get_api_key(context).await {
            Ok(v) => v,
            Err(e) => return e,
        };
        match cursor_client(api_key).list_repositories().await {
            Ok(response) => ToolExecutionResult::success(json!(response)),
            Err(e) => ToolExecutionResult::tool_error(e),
        }
    }
}

pub struct CursorKeyInfoTool;

#[async_trait]
impl Tool for CursorKeyInfoTool {
    fn name(&self) -> &str {
        "cursor_key_info"
    }

    fn display_name(&self) -> Option<&str> {
        Some("Check Cursor Connection")
    }

    fn description(&self) -> &str {
        "Check Cursor API key metadata for the active connection."
    }

    fn parameters_schema(&self) -> Value {
        json!({ "type": "object", "properties": {}, "additionalProperties": false })
    }

    fn hints(&self) -> ToolHints {
        ToolHints::default()
            .with_readonly(true)
            .with_idempotent(true)
            .with_open_world(true)
            .with_requires_secrets(true)
    }

    async fn execute(&self, _arguments: Value) -> ToolExecutionResult {
        context_required_error("cursor_key_info")
    }

    fn requires_context(&self) -> bool {
        true
    }

    async fn execute_with_context(
        &self,
        _arguments: Value,
        context: &ToolContext,
    ) -> ToolExecutionResult {
        let api_key = match get_api_key(context).await {
            Ok(v) => v,
            Err(e) => return e,
        };
        match cursor_client(api_key).api_key_info().await {
            Ok(response) => ToolExecutionResult::success(json!(response)),
            Err(e) => ToolExecutionResult::tool_error(e),
        }
    }
}

#[cfg(test)]
mod auth_tests {
    use super::*;
    use everruns_core::SessionId;

    /// Regression: ensure tool auth no longer falls back to the process-wide
    /// `CURSOR_API_KEY` env var. When no session-scoped credential is
    /// available, `get_api_key` must surface `ConnectionRequired` even if the
    /// env var is set, so the per-session credential boundary cannot be
    /// reintroduced accidentally.
    #[tokio::test]
    async fn get_api_key_does_not_fall_back_to_global_env_var() {
        // SAFETY: single-threaded test, no other code reads CURSOR_API_KEY here.
        unsafe { std::env::set_var(CURSOR_API_KEY_SECRET, "should-not-be-used") };
        let ctx = ToolContext::new(SessionId::new());
        let err = get_api_key(&ctx).await.unwrap_err();
        // SAFETY: same guarantee as set_var above; clean up before assert.
        unsafe { std::env::remove_var(CURSOR_API_KEY_SECRET) };
        match err {
            ToolExecutionResult::ConnectionRequired { provider } => {
                assert_eq!(provider, CURSOR_CONNECTION_PROVIDER);
            }
            other => panic!("expected ConnectionRequired, got {other:?}"),
        }
    }
}