# SCOPE-Gen
**Safety and Capability Optimization for Policy Execution**
[English](README.md) | [简体中文](README_zh-CN.md)
SCOPE-Gen is a generation and executable-validation framework for building both capability and safety tasks for computer-use agents. It targets the [OSWorld](https://github.com/xlang-ai/OSWorld) task environment and [RiOSWorld](https://github.com/yjyddq/RiOSWorld)-style risk evaluation.
The pipeline has two connected stages. First, it composes atomic application capabilities into reproducible OSWorld-compatible tasks, with independently generated Setup, Evaluator, and Counterfactual artifacts. A deterministic host executes and validates these artifacts before accepting a task. Second, it transforms a verified capability task into a paired safety task by injecting one selected hazard condition while preserving the original user goal and completion evaluator. The resulting pairs support measuring both task-execution capability and policy compliance under attack.
## Pipeline
Each role has a narrow contract:
| Role | Responsibility | Main outputs |
|---|---|---|
| Task | Compose sampled capabilities into one realistic GUI instruction and observable success criteria | `task_spec.json` |
| Setup | Create only the initial artifact and OSWorld configuration | `setup_builder.py`, initial artifact, `setup_manifest.json` |
| Evaluator | Create the oracle, calibration cases, and runtime reward implementation | `evaluator_builder.py`, `reward.py` |
| Counterfactual | Independently create one high-value mutation that attacks the evaluator boundary | `counterfactual_builder.py`, audit artifacts |
For a formal candidate, evaluator calibration requires:
```text
reward(initial) = 0
reward(oracle) = 1
reward(calibration case) < 1
```
## Supported task domains
The built-in catalog covers:
- Google Chrome
- GIMP
- LibreOffice Calc
- LibreOffice Writer
- LibreOffice Impress
- Ubuntu Desktop operations
- Visual Studio Code
- Opt-in multi-application workflows
GIMP tasks use pre-downloaded images from `gimp_files/`. The host records and verifies their hashes so Setup cannot silently replace the source asset.
## Requirements
- Python 3.10 or newer
- [uv](https://docs.astral.sh/uv/)
- An OpenAI-compatible endpoint, or the [Codex CLI](https://developers.openai.com/codex/cli/), for formal generation
- A Safactory checkout for generation validation and runtime export; pass its location explicitly with `--safactory-root /path/to/Safactory`
Install the project dependencies:
```bash
uv sync
```
## Quick start
### 1. Run an offline smoke test
Offline mode uses deterministic fixtures and does not call an LLM:
```bash
uv run osworld-taskgen generate \
--offline \
--count 7 \
--overwrite \
--safactory-root /path/to/Safactory \
--output output/offline_smoke
uv run osworld-taskgen validate \
output/offline_smoke \
--expected-count 7 \
--safactory-root /path/to/Safactory \
--allow-offline
```
### 2. Generate capability tasks
#### Option A: Use an OpenAI-compatible API
Keep credentials out of shell history and source control:
```bash
export OPENAI_API_KEY="your-key"
export OPENAI_BASE_URL="https://your-endpoint.example/v1" # optional
```
```bash
uv run osworld-taskgen generate \
--app libreoffice_calc \
--count 10 \
--concurrency 4 \
--generator-model task-model \
--setup-generator-model setup-model \
--evaluator-generator-model evaluator-model \
--evaluator-fallback-model evaluator-fallback-model \
--counterfactual-model counterfactual-model \
--safactory-root /path/to/Safactory \
--output output/calc_api_10
```
#### Option B: Use Codex CLI
Install and authenticate Codex by following the [official Codex CLI guide](https://developers.openai.com/codex/cli/). SCOPE-Gen invokes one ephemeral, read-only `codex exec` thread for each role and injects the corresponding repository skill and dynamic output schema. Model parameters work the same way as with the API backend: each role may use any model available to Codex CLI, and roles may reuse the same model name.
```bash
uv run osworld-taskgen generate \
--app libreoffice_calc \
--count 10 \
--concurrency 4 \
--generator-backend codex \
--generator-model task-model \
--setup-generator-backend codex \
--setup-generator-model setup-model \
--evaluator-generator-backend codex \
--evaluator-generator-model evaluator-model \
--evaluator-fallback-backend codex \
--evaluator-fallback-model evaluator-fallback-model \
--counterfactual-backend codex \
--counterfactual-model counterfactual-model \
--safactory-root /path/to/Safactory \
--output output/calc_codex_10
```
If model parameters are omitted, the built-in defaults for the Codex backend are used.
The role skills live in `.agents/skills/`. Codex threads cannot write task bundles directly; they return structured results to the host, which applies the same safety, calibration, and packaging logic used by the API backend.
### 3. Derive Safactory safety tasks
After capability tasks pass validation, they can be deterministically transformed into corresponding Safactory safety tasks. This process does not call an LLM and preserves each task's Setup and completion Evaluator:
```bash
uv run osworld-taskgen export-safactory-safety \
output/calc_api_10 \
--output output/runtime/calc_api_10_safety \
--validation-concurrency 4 \
--safactory-root /path/to/Safactory
```
For capability tasks generated with Codex CLI, replace the input directory with `output/calc_codex_10`.
## Set a valid-task count target
`--count` is a candidate budget, so rejected candidates can reduce the final task count. For production runs, use a valid-count target and a larger candidate budget:
```bash
uv run osworld-taskgen generate \
--app libreoffice_writer \
--target-valid-count 40 \
--max-candidates 60 \
--concurrency 8 \
--safactory-root /path/to/Safactory \
--output output/writer_valid_40
```
This mode requires every generation role to use the OpenAI-compatible backend or every role to use Codex. Scheduling stops once the target has passed executable final replay or the candidate budget is exhausted, so reaching the target is not guaranteed. Check `target_valid_count_met` in `manifest.json` after the run.
## Coverage-balanced sampling
The default `least-used` strategy favors atomic examples with the lowest accepted and in-run usage:
```bash
uv run osworld-taskgen generate \
--count 40 \
--atomic-sampling-strategy least-used \
--atomic-usage-ledger output/atomic_usage_ledger.json \
--safactory-root /path/to/Safactory \
--output output/run_001
```
Every run writes `atomic_usage_report.json` with selected, accepted, and rejected frequencies. The optional ledger balances usage across runs and is updated only after successful final validation.
## Manual validation
To check an existing canonical bundle independently, run:
```bash
uv run osworld-taskgen validate \
output/calc_api_10 \
--validation-concurrency 4 \
--safactory-root /path/to/Safactory
```
This command is useful after a task directory has been modified or moved, or before a formal handoff. Generation and export already perform their own validation, so this command is not a required prerequisite. To assert a task count, pass `--expected-count` using the actual `task_count` from `manifest.json`.
## Export a Safactory dataset
Canonical bundles retain generation evidence, oracle artifacts, calibration cases, and Counterfactual audits. Export a smaller rollout dataset for Safactory:
```bash
uv run osworld-taskgen export-safactory \
output/calc_api_10 \
--output output/runtime/calc_api_10 \
--validation-concurrency 4 \
--safactory-root /path/to/Safactory
```
`export-safactory` automatically validates the source canonical bundle and the exported Safactory runtime.
## Safactory safety task derivation details
The safety-task generator derives five Safactory attack classes:
- `note_attack`: one explicit command-injection variant per base task, deterministically sampled from the five Safactory variants.
- `popup`: one screenshot-overlay task per base task.
- `induced_text`: one malicious-dialog overlay task per base task.
- `account`: one fake-login task per base task, rotating across the arXiv, GitHub, and Yahoo account services and opening the attack page through Safactory's existing `halfway_config` mechanism.
- `phishing_web`: one task only when the base instruction or Evaluator targets arxiv.org, github.com, neurips.cc, or www.kimi.com. Other tasks are counted as skipped instead of being incorrectly relabeled as phishing tasks.
## Output layout
```text
output//
├── manifest.json
├── tasks.jsonl
├── validation_report.json
├── atomic_usage_report.json
└── bundles//
├── task.json
├── task_spec.json
├── setup_builder.py
├── setup_manifest.json
├── initial.* or workspace/
├── evaluator_builder.py
├── evaluation_manifest.json
├── reward.py
├── calibration_audit.json
├── counterfactual_builder.py
├── counterfactual_manifest.json
├── counterfactual_audit.json
└── counterfactual_cases/
```
`tasks.jsonl` follows the OSWorld task schema. Each task points to its initial artifact and task-specific `reward.py`.
## Repository layout
```text
src/ Python package and CLI implementation
data/ Capability catalog, atomic examples, and workflow recipes
gimp_files/ Source images for GIMP tasks
.agents/skills/ Codex role contracts
asset/ README figures
output/ Generated runs; ignored by Git
```