Skip to content

Latest commit

 

History

History
87 lines (60 loc) · 2.4 KB

code-execution.md

File metadata and controls

87 lines (60 loc) · 2.4 KB

Run model code safely

The problem

Four people share a house. Some bills are split by days at home, and some by name. Leftover cents follow a rule.

The first try asks the model to work out the shares itself. It shows its working and sounds sure. Several figures still come out wrong.

How it works

Run model code safely.

The model writes a short program. Your code runs it in a sandbox and checks the result.

  1. The model writes the program.
  2. The sandbox runs it with strict limits.
  3. A guard checks the names and that the shares add up to the bills.

For example, a program that loses one cent is sent back to be fixed.

The flow

sequenceDiagram
    participant Code as Your code
    participant Model
    participant Runner as Sandbox
    Code->>Model: write a program for these bills
    Model-->>Code: program
    Code->>Runner: run it
    Runner-->>Code: shares
    alt the shares add up
        Code->>Code: use them
    else they do not
        Code->>Model: say what failed
    end
Loading

Concepts

  • The closed place that runs the program is a sandbox.
  • The sum check is a guard.

Run it

uv run lab code-execution naive
uv run lab code-execution

Add --case 2 or --case 3 to try the other cases.

Prove it

There are three months of bills. Code works out every true share. It counts shares that are right to the cent, out of 12. More is better.

Version Right shares
Model works it out 8
Model writes code 12

Now break it. Open the pattern file and find check_shares. Make it return an empty list. Then run the test:

uv run pytest tests/promises/test_code_execution.py

The test fails, because shares that do not add up are now accepted.

Stuck points

  • A refused import means the program used something outside the safe list.
  • The check proves the total, not the split. A program can add up and still be unfair.

What to watch for

This sandbox stops mistakes, not attackers. Use a container or a virtual machine for code you do not trust. Never give the program your keys or files.

Where to go next

Go to Load skills on demand. It keeps long rules out of the prompt.