session-execution

작성자: cloudflare

세션 실행, 명령 처리, 셸 상태, FIFO 기반 스트리밍, 또는 stdout/stderr 분리를 작업하거나 검토할 때 사용합니다. 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

cloudflare의 다른 스킬

workerd-api-review
cloudflare
workerd 코드 리뷰를 위한 성능 최적화, API 설계 및 호환성, 보안 취약점, 표준 사양 준수. tcmalloc 인식…
official
workerd-safety-review
cloudflare
메모리 안전성, 스레드 안전성, 동시성, 그리고 workerd 코드 리뷰를 위한 중요 탐지 패턴. V8/KJ 경계 위험 요소, 수명 관리 등을 다룹니다.
official
module-registry
cloudflare
workerd에서 모듈 레지스트리를 작업할 때 로드 — 모듈 해석, 컴파일, 평가, 등록을 읽기, 수정, 디버깅, 검토하는 경우…
official
reproduce
cloudflare
cloudflare/agents GitHub 이슈를 재현하기 위해 최소한의 Agents/Worker 프로젝트를 스캐폴딩하고 임시 Cloudflare 계정에 배포한 후 보고합니다…
official
local-explorer
cloudflare
로컬 탐색기 또는 로컬 API에 제품/리소스를 추가하는 방법. 새로운 로컬 API나 UI 라우트를 구현할 때 사용합니다.
official
commit-categories
cloudflare
커밋을 체인지로그와 "새로운 기능" 요약으로 분류하는 규칙입니다. 체인지로그 또는 whats-new 명령에서 커밋을 분류하기 전에 반드시 로드되어야 합니다. 제공하는 기능:
official
architecture
cloudflare
코드베이스를 처음 탐색할 때, 새 클라이언트 메서드를 추가할 때, 새 컨테이너 핸들러/서비스를 추가할 때, 또는 요청 흐름을 이해할 때 사용합니다.
official
changesets
cloudflare
변경셋을 생성하거나, 릴리즈를 준비하거나, 버전을 올릴 때 사용합니다. 참조할 패키지, 사용자 대상 변경셋 설명 작성 방법 등을 다룹니다.
official