everruns-integrations-deno 0.17.16

Deno Sandbox integration for Everruns agents
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
//! Tool implementations for Deno sandbox operations.
//!
//! Decision: keep the surface small for the first version: create, exec, read,
//! write, list, delete.

use async_trait::async_trait;
use everruns_core::ToolHints;
use everruns_core::tool_output_sanitizer::{
    READ_FILE_DEFAULT_LIMIT, build_text_read_file_result, parse_read_file_window_args,
};
use everruns_core::tools::{Tool, ToolExecutionResult};
use everruns_core::traits::ToolContext;
use serde_json::{Value, json};
use tracing::debug;

use crate::client::{CreateSandboxRequest, DenoClient};
use crate::state::{
    SandboxState, delete_sandbox_state, get_credentials, get_sandbox_state, list_sandbox_states,
    release_sandbox_lease, required_str, save_sandbox_state, touch_sandbox_lease,
};
use crate::{DENO_DEFAULT_MEMORY_MB, DENO_MAX_MEMORY_MB, DENO_SANDBOX_TIMEOUT};

fn parse_memory_mb(arguments: &Value) -> Result<Option<u64>, String> {
    let Some(value) = arguments.get("memory_mb") else {
        return Ok(None);
    };
    if value.is_null() {
        return Ok(None);
    }
    let memory_mb = value
        .as_u64()
        .ok_or_else(|| "Invalid 'memory_mb': must be a positive integer".to_string())?;
    if memory_mb == 0 || memory_mb > DENO_MAX_MEMORY_MB {
        return Err(format!(
            "Invalid 'memory_mb': must be between 1 and {DENO_MAX_MEMORY_MB}"
        ));
    }
    Ok(Some(memory_mb))
}

fn parse_timeout_seconds(timeout: &str) -> Result<u64, String> {
    if timeout == "session" {
        return Err("Deno sandboxes cannot use timeout='session' because Everruns closes the creator websocket after each tool. Use a concrete duration like '20m'.".to_string());
    }
    if let Some(minutes) = timeout.strip_suffix('m') {
        let minutes = minutes
            .parse::<u64>()
            .map_err(|_| "Invalid timeout: expected e.g. '20m' or '600s'".to_string())?;
        return minutes
            .checked_mul(60)
            .ok_or_else(|| "Timeout too large".to_string());
    }
    if let Some(seconds) = timeout.strip_suffix('s') {
        return seconds
            .parse::<u64>()
            .map_err(|_| "Invalid timeout: expected e.g. '20m' or '600s'".to_string());
    }
    Err("Invalid timeout: expected e.g. '20m' or '600s'".to_string())
}

pub struct DenoCreateSandboxTool;

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

    fn description(&self) -> &str {
        "Create a new Deno sandbox. Returns the sandbox id and workspace path."
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "title": { "type": "string", "description": "Sandbox label shown in Deno" },
                "region": { "type": "string", "description": "Region (for example 'ord' or 'ams')" },
                "timeout": { "type": "string", "description": "Sandbox lifetime, e.g. '20m' or '600s'", "default": DENO_SANDBOX_TIMEOUT },
                "memory_mb": { "type": "integer", "description": "Sandbox memory in MiB (1-16384)", "minimum": 1, "maximum": DENO_MAX_MEMORY_MB, "default": DENO_DEFAULT_MEMORY_MB },
                "allow_net": {
                    "type": "array",
                    "items": { "type": "string" },
                    "description": "Optional outbound network allowlist hosts/IPs"
                }
            },
            "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 {
        ToolExecutionResult::tool_error(
            "deno_create_sandbox requires context. This tool must be executed with session context.",
        )
    }

    async fn execute_with_context(
        &self,
        arguments: Value,
        context: &ToolContext,
    ) -> ToolExecutionResult {
        let credentials = match get_credentials(context).await {
            Ok(credentials) => credentials,
            Err(error) => return error,
        };

        let timeout = arguments
            .get("timeout")
            .and_then(Value::as_str)
            .unwrap_or(DENO_SANDBOX_TIMEOUT);
        let timeout_seconds = match parse_timeout_seconds(timeout) {
            Ok(timeout) => timeout,
            Err(error) => return ToolExecutionResult::tool_error(error),
        };
        let memory_mb = match parse_memory_mb(&arguments) {
            Ok(memory_mb) => memory_mb,
            Err(error) => return ToolExecutionResult::tool_error(error),
        };
        let region = arguments
            .get("region")
            .and_then(Value::as_str)
            .map(str::to_string);
        let title = arguments
            .get("title")
            .and_then(Value::as_str)
            .unwrap_or("Everruns Deno Sandbox");
        let allow_net = arguments
            .get("allow_net")
            .and_then(Value::as_array)
            .map(|items| {
                items
                    .iter()
                    .filter_map(Value::as_str)
                    .map(str::to_string)
                    .collect::<Vec<_>>()
            })
            .unwrap_or_default();

        let mut labels = serde_json::Map::new();
        labels.insert("everruns".to_string(), json!("true"));
        labels.insert(
            "everruns.session_id".to_string(),
            json!(context.session_id.to_string()),
        );
        labels.insert("everruns.title".to_string(), json!(title));
        if let Some(session_store) = &context.session_store
            && let Ok(Some(session)) = session_store.get_session(context.session_id).await
        {
            labels.insert(
                "everruns.harness_id".to_string(),
                json!(session.harness_id.to_string()),
            );
            labels.insert(
                "everruns.org_id".to_string(),
                json!(session.organization_id.to_string()),
            );
            if let Some(agent_id) = &session.agent_id {
                labels.insert("everruns.agent_id".to_string(), json!(agent_id.to_string()));
            }
        }

        let client = DenoClient::new(credentials.token, credentials.org);
        let created = match client
            .create_sandbox(CreateSandboxRequest {
                region,
                timeout_seconds: Some(timeout_seconds),
                memory_mb,
                labels,
                allow_net,
            })
            .await
        {
            Ok(created) => created,
            Err(error) => return ToolExecutionResult::tool_error(error),
        };

        let state = SandboxState {
            sandbox_id: created.sandbox_id.clone(),
            region: created.region.clone(),
            org: client.org().map(str::to_string),
            workspace_path: created.workspace_path.clone(),
            started_at: chrono::Utc::now().to_rfc3339(),
        };
        if let Err(error) = save_sandbox_state(context, &state).await {
            return error;
        }
        if let Err(error) = touch_sandbox_lease(context, &state, Some(title.to_string())).await {
            return error;
        }

        ToolExecutionResult::success(json!({
            "sandbox_id": created.sandbox_id,
            "region": created.region,
            "workspace_path": created.workspace_path,
            "status": "running",
            "timeout": timeout,
        }))
    }

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

pub struct DenoExecTool;

#[async_trait]
impl Tool for DenoExecTool {
    fn narrate(
        &self,
        tool_call: &everruns_core::tool_types::ToolCall,
        phase: everruns_core::tool_narration::ToolNarrationPhase,
        locale: Option<&str>,
        _ctx: everruns_core::tool_narration::ToolNarrationContext<'_>,
    ) -> Option<String> {
        let fallback = self.display_name().unwrap_or("Deno");
        Some(everruns_core::tool_narration::narrate_shell_exec(
            &tool_call.arguments,
            fallback,
            phase,
            locale,
        ))
    }

    fn name(&self) -> &str {
        "deno_exec"
    }

    fn description(&self) -> &str {
        "Execute a shell command in a Deno sandbox. Returns stdout, stderr, and exit code."
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "sandbox_id": { "type": "string", "description": "Sandbox ID" },
                "command": { "type": "string", "description": "Shell command to run" },
                "cwd": { "type": "string", "description": "Optional working directory" },
                "output": everruns_core::tool_output_sanitizer::output_verbosity_schema()
            },
            "required": ["sandbox_id", "command"],
            "additionalProperties": false
        })
    }

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

    async fn execute(&self, _arguments: Value) -> ToolExecutionResult {
        ToolExecutionResult::tool_error(
            "deno_exec requires context. This tool must be executed with session context.",
        )
    }

    async fn execute_with_context(
        &self,
        arguments: Value,
        context: &ToolContext,
    ) -> ToolExecutionResult {
        let sandbox_id = match required_str(&arguments, "sandbox_id") {
            Ok(value) => value,
            Err(error) => return error,
        };
        let command = match required_str(&arguments, "command") {
            Ok(value) => value,
            Err(error) => return error,
        };
        let cwd = arguments.get("cwd").and_then(Value::as_str);
        let output_mode = arguments
            .get("output")
            .and_then(|v| v.as_str())
            .unwrap_or("auto");

        let credentials = match get_credentials(context).await {
            Ok(credentials) => credentials,
            Err(error) => return error,
        };
        let state = match get_sandbox_state(context, sandbox_id).await {
            Ok(state) => state,
            Err(error) => return error,
        };

        let client = DenoClient::new(credentials.token, credentials.org);
        debug!(sandbox_id, command, "Executing command in Deno sandbox");
        let exec = match client.exec(sandbox_id, &state.region, command, cwd).await {
            Ok(exec) => exec,
            Err(error) => return ToolExecutionResult::tool_error(error),
        };

        if let Err(error) = touch_sandbox_lease(context, &state, None).await {
            return error;
        }

        {
            use everruns_core::tool_output_sanitizer::{
                clean_exec_output, output_verbosity_budget, priority_aware_truncate,
                resolve_auto_mode,
            };
            let clean_stdout = clean_exec_output(&exec.stdout);
            let clean_stderr = clean_exec_output(&exec.stderr);
            let effective_mode = resolve_auto_mode(output_mode, exec.exit_code);
            let (stdout, stderr) = if let Some(budget) = output_verbosity_budget(effective_mode) {
                (
                    priority_aware_truncate(&clean_stdout, budget),
                    priority_aware_truncate(&clean_stderr, budget.min(4096)),
                )
            } else {
                (clean_stdout.clone(), clean_stderr.clone())
            };
            let mut raw = clean_stdout;
            if !clean_stderr.is_empty() {
                raw.push_str("\n--- stderr ---\n");
                raw.push_str(&clean_stderr);
            }
            ToolExecutionResult::success_with_raw_output(
                json!({
                    "exit_code": exec.exit_code,
                    "stdout": stdout,
                    "stderr": stderr,
                }),
                raw,
            )
        }
    }

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

pub struct DenoReadFileTool;

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

    fn description(&self) -> &str {
        "Read a text file from a remote Deno sandbox filesystem (NOT the session /workspace). Requires sandbox_id."
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "sandbox_id": { "type": "string", "description": "Sandbox ID" },
                "path": { "type": "string", "description": "Path to read" },
                "offset": {
                    "type": "integer",
                    "minimum": 0,
                    "default": 0,
                    "description": "Zero-based line offset to start reading from"
                },
                "limit": {
                    "type": "integer",
                    "minimum": 1,
                    "default": READ_FILE_DEFAULT_LIMIT,
                    "description": "Maximum number of lines to return"
                }
            },
            "required": ["sandbox_id", "path"],
            "additionalProperties": false
        })
    }

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

    async fn execute(&self, _arguments: Value) -> ToolExecutionResult {
        ToolExecutionResult::tool_error(
            "deno_read_file requires context. This tool must be executed with session context.",
        )
    }

    async fn execute_with_context(
        &self,
        arguments: Value,
        context: &ToolContext,
    ) -> ToolExecutionResult {
        let sandbox_id = match required_str(&arguments, "sandbox_id") {
            Ok(value) => value,
            Err(error) => return error,
        };
        let path = match required_str(&arguments, "path") {
            Ok(value) => value,
            Err(error) => return error,
        };
        let (offset, limit) = match parse_read_file_window_args(&arguments) {
            Ok(window) => window,
            Err(err) => return ToolExecutionResult::tool_error(err),
        };

        let credentials = match get_credentials(context).await {
            Ok(credentials) => credentials,
            Err(error) => return error,
        };
        let state = match get_sandbox_state(context, sandbox_id).await {
            Ok(state) => state,
            Err(error) => return error,
        };

        let client = DenoClient::new(credentials.token, credentials.org);
        let content = match client.read_text_file(sandbox_id, &state.region, path).await {
            Ok(content) => content,
            Err(error) => return ToolExecutionResult::tool_error(error),
        };
        if let Err(error) = touch_sandbox_lease(context, &state, None).await {
            return error;
        }

        let mut result =
            build_text_read_file_result("deno_read_file", path, &content, "text", offset, limit);
        result["sandbox_id"] = json!(sandbox_id);
        ToolExecutionResult::success(result)
    }

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

pub struct DenoWriteFileTool;

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

    fn description(&self) -> &str {
        "Write a text file into a Deno sandbox filesystem."
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "sandbox_id": { "type": "string", "description": "Sandbox ID" },
                "path": { "type": "string", "description": "Path to write" },
                "content": { "type": "string", "description": "File content" }
            },
            "required": ["sandbox_id", "path", "content"],
            "additionalProperties": false
        })
    }

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

    async fn execute(&self, _arguments: Value) -> ToolExecutionResult {
        ToolExecutionResult::tool_error(
            "deno_write_file requires context. This tool must be executed with session context.",
        )
    }

    async fn execute_with_context(
        &self,
        arguments: Value,
        context: &ToolContext,
    ) -> ToolExecutionResult {
        let sandbox_id = match required_str(&arguments, "sandbox_id") {
            Ok(value) => value,
            Err(error) => return error,
        };
        let path = match required_str(&arguments, "path") {
            Ok(value) => value,
            Err(error) => return error,
        };
        let content = match required_str(&arguments, "content") {
            Ok(value) => value,
            Err(error) => return error,
        };

        let credentials = match get_credentials(context).await {
            Ok(credentials) => credentials,
            Err(error) => return error,
        };
        let state = match get_sandbox_state(context, sandbox_id).await {
            Ok(state) => state,
            Err(error) => return error,
        };

        let client = DenoClient::new(credentials.token, credentials.org);
        match client
            .write_text_file(sandbox_id, &state.region, path, content)
            .await
        {
            Ok(()) => {}
            Err(error) => return ToolExecutionResult::tool_error(error),
        }
        if let Err(error) = touch_sandbox_lease(context, &state, None).await {
            return error;
        }

        ToolExecutionResult::success(json!({
            "path": path,
            "success": true,
        }))
    }

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

pub struct DenoListSandboxesTool;

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

    fn description(&self) -> &str {
        "List all Deno sandboxes created in this session."
    }

    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 {
        ToolExecutionResult::tool_error(
            "deno_list_sandboxes requires context. This tool must be executed with session context.",
        )
    }

    async fn execute_with_context(
        &self,
        _arguments: Value,
        context: &ToolContext,
    ) -> ToolExecutionResult {
        let sandboxes = match list_sandbox_states(context).await {
            Ok(sandboxes) => sandboxes,
            Err(error) => return error,
        };

        ToolExecutionResult::success(json!({
            "sandboxes": sandboxes.iter().map(|state| {
                json!({
                    "sandbox_id": state.sandbox_id,
                    "region": state.region,
                    "workspace_path": state.workspace_path,
                    "started_at": state.started_at,
                })
            }).collect::<Vec<_>>(),
            "count": sandboxes.len(),
        }))
    }

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

pub struct DenoManageSandboxTool;

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

    fn description(&self) -> &str {
        "Delete a Deno sandbox."
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "sandbox_id": { "type": "string", "description": "Sandbox ID" },
                "action": { "type": "string", "enum": ["delete"], "description": "Lifecycle action" }
            },
            "required": ["sandbox_id", "action"],
            "additionalProperties": false
        })
    }

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

    async fn execute(&self, _arguments: Value) -> ToolExecutionResult {
        ToolExecutionResult::tool_error(
            "deno_manage_sandbox requires context. This tool must be executed with session context.",
        )
    }

    async fn execute_with_context(
        &self,
        arguments: Value,
        context: &ToolContext,
    ) -> ToolExecutionResult {
        let sandbox_id = match required_str(&arguments, "sandbox_id") {
            Ok(value) => value,
            Err(error) => return error,
        };
        let action = match required_str(&arguments, "action") {
            Ok(value) => value,
            Err(error) => return error,
        };
        if action != "delete" {
            return ToolExecutionResult::tool_error(
                "Unsupported action. Deno sandboxes currently support only action='delete'.",
            );
        }

        let credentials = match get_credentials(context).await {
            Ok(credentials) => credentials,
            Err(error) => return error,
        };
        let state = match get_sandbox_state(context, sandbox_id).await {
            Ok(state) => state,
            Err(error) => return error,
        };

        let client = DenoClient::new(credentials.token, credentials.org);
        if let Err(error) = client.delete_sandbox(sandbox_id, &state.region).await {
            return ToolExecutionResult::tool_error(error);
        }
        if let Err(error) = delete_sandbox_state(context, sandbox_id).await {
            return error;
        }
        if let Err(error) = release_sandbox_lease(context, sandbox_id).await {
            return error;
        }

        ToolExecutionResult::success(json!({
            "sandbox_id": sandbox_id,
            "action": action,
            "success": true,
        }))
    }

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

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_timeout_rejects_session() {
        let err = parse_timeout_seconds("session").unwrap_err();
        assert!(err.contains("session"));
    }

    #[test]
    fn parse_memory_validates_bounds() {
        assert!(parse_memory_mb(&json!({"memory_mb": 0})).is_err());
        assert!(
            parse_memory_mb(&json!({"memory_mb": 1280}))
                .unwrap()
                .is_some()
        );
    }

    #[test]
    fn create_tool_mentions_fixed_timeout() {
        let tool = DenoCreateSandboxTool;
        let schema = tool.parameters_schema();
        assert_eq!(
            schema["properties"]["timeout"]["default"],
            DENO_SANDBOX_TIMEOUT
        );
        assert_eq!(crate::DENO_WORKSPACE_PATH, "/home/app");
    }
}