Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
# Repository Guideline

## Project Overview

Piston is a high-performance, high-security code execution engine supporting 90+ programming languages. It enables safe code execution via an API server and operates in a sandboxed environment using Linux Isolate (namespaces + chroot + cgroup).

## Development Commands

### Starting and Stopping the Environment

```bash
./piston select dev # Select development environment (first time only)
./piston start # Start
./piston stop # Stop
./piston restart # Restart
./piston logs # Show logs
./piston bash # Open container shell
./piston rebuild # Build and restart
```

### Code Formatting (Lint)

```bash
./piston lint # Format all files with Prettier
npx prettier --write <path> # Format specific files only
```

### Package Management

```bash
./piston list-pkgs # List available packages
./piston build-pkg <pkg> <ver> # Build package
./piston clean-pkgs # Clean build artifacts
```

### CLI Setup

```bash
cd cli && npm i && cd -
```

## Architecture

```
┌─────────────────────────────────────────────────────────────┐
│ Docker Container │
├─────────────────────────────────────────────────────────────┤
│ api/ cli/ │
│ ├─ Express Server ├─ yargs CLI │
│ ├─ Routes (api/v2.js) └─ commands/ │
│ ├─ Job Manager (job.js) ├─ execute.js │
│ ├─ Runtime Manager └─ ppman.js │
│ └─ Package Manager │
├─────────────────────────────────────────────────────────────┤
│ Isolate Sandbox │
│ (Linux namespaces + chroot + cgroup) │
├─────────────────────────────────────────────────────────────┤
│ packages/ │
│ └─ <lang>/<version>/ │
│ ├─ metadata.json (language info, aliases) │
│ ├─ build.sh (build script) │
│ ├─ run (execution script) │
│ └─ environment (environment variables) │
└─────────────────────────────────────────────────────────────┘
```

### Main Components

| File | Role |
|---------|------|
| `api/src/index.js` | Express server initialization |
| `api/src/api/v2.js` | API endpoint definitions |
| `api/src/job.js` | Job execution management (READY → PRIMED → EXECUTED) |
| `api/src/runtime.js` | Language runtime management |
| `api/src/package.js` | Package installation and management |
| `api/src/config.js` | Configuration management via environment variables |

### API Endpoints

| Method | Path | Purpose |
|---------|------|------|
| GET | `/api/v2/runtimes` | List installed languages |
| POST | `/api/v2/execute` | Execute code |
| WebSocket | `/api/v2/connect` | Interactive execution |

## Configuration (Environment Variables)

Main environment variables (`PISTON_` prefix):

| Variable | Default | Description |
|-----|---------|------|
| `PISTON_LOG_LEVEL` | INFO | Log level |
| `PISTON_BIND_ADDRESS` | 0.0.0.0:2000 | API bind address |
| `PISTON_DISABLE_NETWORKING` | true | Disable networking |
| `PISTON_COMPILE_TIMEOUT` | 10000 | Compile timeout (ms) |
| `PISTON_RUN_TIMEOUT` | 3000 | Execution timeout (ms) |
| `PISTON_MAX_PROCESS_COUNT` | 64 | Maximum process count |
| `PISTON_OUTPUT_MAX_SIZE` | 1024 | Maximum output size |

See `docs/configuration.md` for details.

## Testing

Security tests are located in `/tests/`:

```bash
python3 tests/fork.py # Fork bomb test
python3 tests/fallocate.py # Disk fill attack test
python3 tests/network.py # Network access test
```

Package tests are automatically executed via GitHub Actions (`package-pr.yaml`).

## Adding Language Packages

1. Create `packages/<lang>/<version>/` directory
2. Create required files:
- `metadata.json` - Language name, version, aliases
- `build.sh` - Build script
- `run` - Execution script
3. Build with `./piston build-pkg <lang> <version>`
4. Add badge to README.md

## Code Style

Prettier configuration (`.prettierrc.yaml`):
- Use single quotes
- Tab width: 4
- Omit arrow function parentheses

### Commit Messages

- **Do not use Conventional Commits** (no need for prefixes like `fix:`, `feat:`)
- Write in normal format with concise description of changes

## GitHub Actions

### permissions Configuration

Workflows using ghcr.io or GitHub Releases require explicit permissions configuration:

```yaml
jobs:
job_name:
runs-on: ubuntu-latest
permissions:
contents: write # When uploading to releases
packages: write # When pushing to ghcr.io
packages: read # When pulling from ghcr.io
```

### Docker Image Workflows

| Workflow | Purpose | Trigger Path |
|-------------|------|-------------|
| `api-push.yaml` | API image | `api/**` |
| `repo-push.yaml` | Repo Builder image | `repo/**` |
| `package-push.yaml` | Package build | `packages/**` |

## Prerequisites

- Docker & Docker Compose
- cgroup v2 enabled (cgroup v1 disabled)
- Node.js >= 15 (for CLI development)
167 changes: 1 addition & 166 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -1,166 +1 @@
# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## プロジェクト概要

Piston は、90以上のプログラミング言語をサポートする高性能・高セキュリティなコード実行エンジン。API サーバー経由でコードを安全に実行でき、Linux の Isolate (namespaces + chroot + cgroup) を使用したサンドボックス環境で動作する。

## 開発コマンド

### 環境の起動・停止

```bash
./piston select dev # 開発環境を選択 (初回のみ)
./piston start # 起動
./piston stop # 停止
./piston restart # 再起動
./piston logs # ログ表示
./piston bash # コンテナシェルを開く
./piston rebuild # ビルドして再起動
```

### コード整形 (Lint)

```bash
./piston lint # Prettier で全ファイルをフォーマット
npx prettier --write <path> # 特定ファイルのみフォーマット
```

### パッケージ管理

```bash
./piston list-pkgs # 利用可能なパッケージ一覧
./piston build-pkg <pkg> <ver> # パッケージをビルド
./piston clean-pkgs # ビルド成果物をクリーン
```

### CLI のセットアップ

```bash
cd cli && npm i && cd -
```

## アーキテクチャ

```
┌─────────────────────────────────────────────────────────────┐
│ Docker Container │
├─────────────────────────────────────────────────────────────┤
│ api/ cli/ │
│ ├─ Express Server ├─ yargs CLI │
│ ├─ Routes (api/v2.js) └─ commands/ │
│ ├─ Job Manager (job.js) ├─ execute.js │
│ ├─ Runtime Manager └─ ppman.js │
│ └─ Package Manager │
├─────────────────────────────────────────────────────────────┤
│ Isolate Sandbox │
│ (Linux namespaces + chroot + cgroup) │
├─────────────────────────────────────────────────────────────┤
│ packages/ │
│ └─ <lang>/<version>/ │
│ ├─ metadata.json (言語情報、エイリアス) │
│ ├─ build.sh (ビルドスクリプト) │
│ ├─ run (実行スクリプト) │
│ └─ environment (環境変数) │
└─────────────────────────────────────────────────────────────┘
```

### 主要コンポーネント

| ファイル | 役割 |
|---------|------|
| `api/src/index.js` | Express サーバー初期化 |
| `api/src/api/v2.js` | API エンドポイント定義 |
| `api/src/job.js` | ジョブ実行管理 (READY → PRIMED → EXECUTED) |
| `api/src/runtime.js` | 言語ランタイム管理 |
| `api/src/package.js` | パッケージのインストール・管理 |
| `api/src/config.js` | 環境変数による設定管理 |

### API エンドポイント

| メソッド | パス | 目的 |
|---------|------|------|
| GET | `/api/v2/runtimes` | インストール済み言語一覧 |
| POST | `/api/v2/execute` | コード実行 |
| WebSocket | `/api/v2/connect` | インタラクティブ実行 |

## 設定 (環境変数)

主要な環境変数 (`PISTON_` プレフィックス):

| 変数 | デフォルト | 説明 |
|-----|---------|------|
| `PISTON_LOG_LEVEL` | INFO | ログレベル |
| `PISTON_BIND_ADDRESS` | 0.0.0.0:2000 | API バインドアドレス |
| `PISTON_DISABLE_NETWORKING` | true | ネットワーク無効化 |
| `PISTON_COMPILE_TIMEOUT` | 10000 | コンパイルタイムアウト (ms) |
| `PISTON_RUN_TIMEOUT` | 3000 | 実行タイムアウト (ms) |
| `PISTON_MAX_PROCESS_COUNT` | 64 | 最大プロセス数 |
| `PISTON_OUTPUT_MAX_SIZE` | 1024 | 出力最大サイズ |

詳細は `docs/configuration.md` を参照。

## テスト

セキュリティテストは `/tests/` にある:

```bash
python3 tests/fork.py # フォーク爆弾テスト
python3 tests/fallocate.py # ディスク満杯攻撃テスト
python3 tests/network.py # ネットワークアクセステスト
```

パッケージのテストは GitHub Actions (`package-pr.yaml`) で自動実行される。

## 言語パッケージの追加

1. `packages/<lang>/<version>/` ディレクトリを作成
2. 必須ファイルを作成:
- `metadata.json` - 言語名、バージョン、エイリアス
- `build.sh` - ビルドスクリプト
- `run` - 実行スクリプト
3. `./piston build-pkg <lang> <version>` でビルド
4. README.md にバッジを追加

## コードスタイル

Prettier 設定 (`.prettierrc.yaml`):
- シングルクォート使用
- タブ幅: 4
- Arrow関数の括弧: 省略

### コミットメッセージ

- **Conventional Commits は使用しない** (`fix:`, `feat:` などのプレフィックスは不要)
- 変更内容を簡潔に説明する通常の形式で記述

## GitHub Actions

### permissions 設定

ghcr.io や GitHub Releases を使用するワークフローでは、明示的な permissions 設定が必要:

```yaml
jobs:
job_name:
runs-on: ubuntu-latest
permissions:
contents: write # リリースへのアップロード時
packages: write # ghcr.io へのプッシュ時
packages: read # ghcr.io からのプル時
```

### Docker イメージワークフロー

| ワークフロー | 用途 | トリガーパス |
|-------------|------|-------------|
| `api-push.yaml` | API イメージ | `api/**` |
| `repo-push.yaml` | Repo Builder イメージ | `repo/**` |
| `package-push.yaml` | パッケージビルド | `packages/**` |

## 前提条件

- Docker & Docker Compose
- cgroup v2 有効化 (cgroup v1 は無効化)
- Node.js >= 15 (CLI 開発時)
@AGENTS.md
5 changes: 5 additions & 0 deletions packages/rust/1.93.1/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

curl -OL "https://static.rust-lang.org/dist/rust-1.93.1-x86_64-unknown-linux-gnu.tar.gz"
tar xzvf rust-1.93.1-x86_64-unknown-linux-gnu.tar.gz
rm rust-1.93.1-x86_64-unknown-linux-gnu.tar.gz
6 changes: 6 additions & 0 deletions packages/rust/1.93.1/compile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

# https://stackoverflow.com/questions/38041331/rust-compiler-cant-find-crate-for-std
# Rust compiler needs to find the stdlib to link against
rustc -o binary -L ${RUST_INSTALL_LOC}/rustc/lib -L ${RUST_INSTALL_LOC}/rust-std-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib "$@"
chmod +x binary
5 changes: 5 additions & 0 deletions packages/rust/1.93.1/environment
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

# Put 'export' statements here for environment variables
export PATH=$PWD/rust-1.93.1-x86_64-unknown-linux-gnu/rustc/bin/:$PATH
export RUST_INSTALL_LOC=$PWD/rust-1.93.1-x86_64-unknown-linux-gnu
7 changes: 7 additions & 0 deletions packages/rust/1.93.1/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"language": "rust",
"version": "1.93.1",
"aliases": [
"rs"
]
}
4 changes: 4 additions & 0 deletions packages/rust/1.93.1/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

shift
./binary "$@"
3 changes: 3 additions & 0 deletions packages/rust/1.93.1/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("OK");
}