Metadata-Version: 2.4
Name: samnour-driver
Version: 0.4.0
Summary: Local terminal and workspace driver for Samnour coding labs
Project-URL: Homepage, https://github.com/nattkorat/quiz_system
Project-URL: Repository, https://github.com/nattkorat/quiz_system
Project-URL: Documentation, https://github.com/nattkorat/quiz_system/tree/main/local_agent
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Framework :: FastAPI
Classifier: Intended Audience :: Education
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Python: >=3.11
Description-Content-Type: text/markdown
Requires-Dist: fastapi<1,>=0.162
Requires-Dist: uvicorn[standard]<1,>=0.30
Requires-Dist: pywinpty<4,>=3.0.4; sys_platform == "win32"
Provides-Extra: dev
Requires-Dist: httpx<1,>=0.27; extra == "dev"
Requires-Dist: pytest<10,>=8; extra == "dev"
Provides-Extra: build
Requires-Dist: pyinstaller<7,>=6.10; extra == "build"

# Samnour Driver

Samnour Driver connects the browser coding workspace to a real terminal and project folder on the student's computer. It does not send commands to the central Samnour backend and does not provide centralized compilation. Students can choose a native installation or a more isolated Docker container.

Implemented in this milestone:

- loopback-only native service and explicit container mode;
- short-lived, single-use console pairing code;
- origin-bound bearer and WebSocket credentials;
- real Unix PTY support and a Windows ConPTY adapter through `pywinpty`;
- streaming input/output, ANSI data, resize, exit, timeout, output limits, and process-group cleanup;
- multiple independent terminal sessions;
- workspace-rooted starting directories;
- bounded project synchronization between the browser editor and terminal workspace;
- tool path/version discovery;
- an xterm.js browser test page;
- API, security, PTY, and local toolchain tests.

This milestone has been exercised on macOS and Ubuntu. The Windows code path still requires validation before it is described as supported. A normal user shell is not a sandbox: it can access everything available to that OS account.

## Basic option: native installation

This mode uses the student's installed tools and has the same file access as their OS account.

```bash
python3 -m pip install --upgrade samnour-driver
samnour-driver
```

On Windows PowerShell, use `py` instead of `python3`.

For local source development:

```bash
cd local_agent
python3 -m venv .venv
source .venv/bin/activate             # Windows: .venv\Scripts\activate
python -m pip install -e .
samnour-driver
```

## Container-isolated option: Docker

The published image includes Python, Java, GCC, and G++. It runs as a non-root user with a read-only container filesystem, dropped Linux capabilities, resource limits, and a dedicated workspace volume. It cannot see the student's personal files unless additional host paths are mounted. Docker isolation reduces risk but is not a perfect hostile-code sandbox.

```bash
docker pull ghcr.io/nattkorat/samnour-driver:latest
docker run --rm -it \
  -p 127.0.0.1:8765:8765 \
  --read-only \
  --tmpfs /tmp:rw,nosuid,nodev,size=128m \
  --cap-drop=ALL \
  --security-opt=no-new-privileges \
  --pids-limit=256 \
  --memory=1g \
  --cpus=2 \
  -v samnour-workspaces:/workspaces \
  ghcr.io/nattkorat/samnour-driver:latest
```

The GitHub Actions workflow publishes `latest`, release, and commit-specific images after Samnour Driver changes reach `main`. The GHCR package must be **Public** so students can pull without signing in.

Open <http://127.0.0.1:8765>, enter the pairing code printed in the driver console, and use the terminal. The test page loads xterm.js from jsDelivr, so it needs network access. Samnour Driver and its PTY do not depend on the central Samnour server.

To test inside Samnour, run the driver before opening a coding exam or lab. The coding workspace detects the driver, accepts the one-time pairing code, synchronizes editor files into its assigned local workspace, and imports terminal file changes back into the browser submission.

Configuration:

| Environment variable | Default |
|---|---|
| `SAMNOUR_DRIVER_HOST` | `127.0.0.1`; the container uses `0.0.0.0` internally |
| `SAMNOUR_DRIVER_PORT` | `8765` |
| `SAMNOUR_ALLOWED_ORIGINS` | local test page and Vite development origins |
| `SAMNOUR_WORKSPACE_ROOT` | `~/SamnourWorkspaces` |
| `SAMNOUR_DRIVER_CONTAINER_MODE` | `false`; the image sets it to `true` |
| `SAMNOUR_PAIRING_TTL_SECONDS` | `300` |
| `SAMNOUR_TERMINAL_TIMEOUT_SECONDS` | `14400` |
| `SAMNOUR_MAX_TERMINAL_OUTPUT_BYTES` | `52428800` |

For a deployed Samnour frontend, set `SAMNOUR_ALLOWED_ORIGINS` to its exact HTTPS origin plus any intentionally enabled development origin.

The previous `SAMNOUR_AGENT_HOST`, `SAMNOUR_AGENT_PORT`, and `SAMNOUR_AGENT_CONTAINER_MODE` names remain accepted for backward compatibility.

## Test

```bash
cd local_agent
python -m pip install -e '.[dev]'
pytest
python scripts/smoke_toolchains.py
```

The smoke test compiles and runs temporary Python, Java, C, and C++ programs through the native PTY. Missing toolchains are reported as skipped rather than installed.

See [PROTOCOL.md](PROTOCOL.md) for the complete wire protocol.
