session-execution

Sử dụng khi làm việc hoặc xem xét việc thực thi phiên, xử lý lệnh, trạng thái shell, truyền phát dựa trên FIFO hoặc tách biệt stdout/stderr. Liên quan đến session.ts,…

npx skills add https://github.com/cloudflare/sandbox-sdk --skill session-execution

Session Execution

Read docs/SESSION_EXECUTION.md before working in this area. It explains the architecture for reliable command execution with stdout/stderr separation.

Key Concepts

Two execution modes:

  • Foreground (exec): Runs in main shell, state persists. Uses temp files for output capture.
  • Background (execStream/startProcess): Runs in subshell via FIFOs. Labelers prefix output in background.

Binary prefix contract:

  • Stdout: \x01\x01\x01 prefix per line
  • Stderr: \x02\x02\x02 prefix per line
  • Log parser reconstructs streams from these prefixes

Completion signaling:

  • Exit code written to <id>.exit file via atomic tmp + mv
  • Hybrid fs.watch + polling detects completion (robust on tmpfs/overlayfs)
  • Background mode uses labelers.done marker to ensure output is fully captured

When Developing

  • Understand why foreground uses temp files (bash waits for redirects to complete)
  • Understand why background uses FIFOs (concurrent streaming without blocking shell)
  • Test silent commands (cd, variable assignment) - these historically caused hangs
  • Test large output - buffering issues can cause incomplete logs

When Reviewing

Correctness checks:

  • Verify exit code handling is atomic (write to .tmp then mv)
  • Check FIFO cleanup in error paths
  • Ensure labelers.done is awaited before reading final output (background mode)

Race condition analysis:

Session execution has a mutex that serializes command execution per session. Before flagging race conditions:

  1. Check if operations happen within the same session (mutex protects)
  2. Check if operations are per-session vs cross-session (cross-session races are real)
  3. Refer to docs/CONCURRENCY.md for the full concurrency model

Common false positives:

  • "Concurrent reads/writes to session state" - mutex serializes these
  • "FIFO operations might race" - labelers are per-command, not shared

Actual concerns to watch for:

  • Cross-session operations without proper isolation
  • Cleanup operations that might affect still-running commands
  • File operations outside the mutex-protected section

Key Files

  • packages/sandbox-container/src/session.ts - Session class with exec/execStream
  • packages/sandbox-container/src/managers/SessionManager.ts - Mutex and lifecycle
  • packages/sandbox/src/clients/CommandClient.ts - SDK interface to session commands

Thêm skills từ cloudflare

workerd-api-review
cloudflare
Tối ưu hóa hiệu suất, thiết kế & tương thích API, lỗ hổng bảo mật và tuân thủ đặc tả tiêu chuẩn cho việc xem xét mã workerd. Bao gồm nhận biết tcmalloc…
official
workerd-safety-review
cloudflare
Các mẫu phát hiện về an toàn bộ nhớ, an toàn luồng, đồng thời và quan trọng cho việc xem xét mã workerd. Bao gồm các rủi ro ranh giới V8/KJ, quản lý vòng đời,…
official
module-registry
cloudflare
Tải khi làm việc với module registry trong workerd — đọc, sửa đổi, gỡ lỗi hoặc xem xét việc phân giải, biên dịch, đánh giá hoặc đăng ký module…
official
reproduce
cloudflare
Tái tạo một vấn đề GitHub của cloudflare/agents bằng cách tạo khung một dự án Agents/Worker tối thiểu và triển khai nó lên một tài khoản Cloudflare tạm thời, sau đó báo cáo…
official
local-explorer
cloudflare
Cách thêm sản phẩm/tài nguyên vào local explorer hoặc local API. Sử dụng khi triển khai các local API mới hoặc các route UI trong…
official
commit-categories
cloudflare
Quy tắc phân loại commit cho changelog và tóm tắt "có gì mới". PHẢI được tải trước khi phân loại commit trong changelog hoặc lệnh whats-new. Cung cấp…
official
architecture
cloudflare
Sử dụng khi lần đầu điều hướng cơ sở mã, thêm một phương thức client mới, thêm một trình xử lý/dịch vụ container mới, hoặc hiểu cách một yêu cầu luân chuyển…
official
changesets
cloudflare
Sử dụng khi tạo changeset, chuẩn bị phát hành hoặc nâng phiên bản. Bao gồm các gói cần tham chiếu, cách viết mô tả changeset hướng đến người dùng,…
official