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
64 changes: 59 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,15 @@ not learned from, measured against a control.
draft pull request ───► a person reviews it; Insi never merges
```

Four rules keep the measurement honest:

1. **The agent never sees the check.** Hidden tests live outside every
checkout the agent can read. They are copied in after the agent stops,
replacing anything it wrote at those paths.
Four rules keep the measurement honest (all eleven, with how each is enforced, are in [docs/MEASUREMENT.md](docs/MEASUREMENT.md)):

1. **The agent never sees the check.** Hidden tests live outside the
attempt's folder and are copied in after the agent stops, replacing
anything it wrote at those paths. The attempt's repository holds the base
commit and nothing after it, so no git command shows the fix. What is on
disk (the source checkout, the hidden tests) is kept from the agent by
[`[worker] sandbox`](#keeping-the-worker-from-the-answer); `insi report`
counts the attempts that ran without one.
2. **Only fair tasks count.** A task whose check already passes at the start,
or fails even with the real fix, is dropped by `insi validate`.
3. **Learn on the past, measure on the future.** Tasks are split by commit
Expand Down Expand Up @@ -389,6 +393,25 @@ insi issue --number 42 # from the repository's folder
network. Committing, the scan and the push stay outside it, on this
machine. A sandbox with no `{tree}` is refused.

Without a container,
[`sbx`](https://github.com/incordai/Incode/tree/main/crates/sandbox) does
the same with the OS's own sandbox (Landlock on Linux, Seatbelt on
macOS). In the example below:
- writes are confined to the worktree and the agent's own state;
- the network only reaches the hosts listed;
- the session and `GIT_DIR` variables are stripped;
- the step is killed after the wall limit.

It refuses to run when the machine cannot enforce the policy, instead of
running unconfined:

```toml
[sandbox]
command = ["sbx", "run", "-w", "{tree}", "-w", "~/.claude", "-w", "~/.claude.json",
"--allow-host", "api.anthropic.com", "--allow-host", "registry.npmjs.org",
"--wall", "1800", "--"]
```

GitHub is reached through the `gh` CLI, which holds the login: Insi reads,
holds and sends no token.

Expand Down Expand Up @@ -622,6 +645,37 @@ are timed but not counted in tokens. `--spec-gate=off` keeps incode from
writing its spec documents before each already-scoped task (it needs an incode
that has the flag; an older one refuses it by name).

### Keeping the worker from the answer

The answer to a task is in three places: the fix commit, the source
repository's checkout (usually at a commit after the fix), and the hidden
tests. A worker attempt's repository is a shallow fetch of the base commit
alone, so git can't reach the first one. The other two are on disk, and any
worker with a shell can read them. Measured: a worker that only ran
`cp ../../../src/lib.rs src/lib.rs` passed its task, and the reward-hacking
check didn't catch it, because it passed the hidden tests for real.

`[worker] sandbox` runs the worker step inside a command and fills in its
places:
- `{tree}`: the attempt's folder (required);
- `{private}`: the hidden tests;
- `{work}`: Insi's work folder;
- `{source}`: the task's source, which must be a local folder.

With [`sbx`](https://github.com/incordai/Incode/tree/main/crates/sandbox):

```toml
[worker]
command = ["incode", "-t", "{prompt}", "--output-format", "stream-json", "--spec-gate=off"]
sandbox = ["sbx", "run", "-q", "-w", "{tree}", "-w", "~/.claude", "-w", "~/.claude.json",
"-w", "~/.incode", "--deny-read", "{source}", "--deny-read", "{private}", "--"]
```

The same cheating worker then fails, as it should: the copy is refused.
Each result records `isolation` (`history: "base-only"`, `sandboxed`).
`insi report` adds `N attempt(s) unsandboxed` to a condition whose passes are
not proof of the work.

Every command Insi starts — the worker, the checks, `gh` — is started without
the variables of the agent session Insi itself runs in (`CLAUDECODE`,
`CLAUDE_CODE_SESSION_ID`, …), so concurrent attempts never share, or report,
Expand Down
127 changes: 127 additions & 0 deletions crates/insi-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ fn untrusted() -> crate::Trust {

/// What a sandbox command must contain: where the work's folder goes.
pub const TREE: &str = "{tree}";
/// In `[worker] sandbox`: where the hidden tests are kept.
pub const PRIVATE: &str = "{private}";
/// In `[worker] sandbox`: Insi's work folder.
pub const WORK: &str = "{work}";
/// In `[worker] sandbox`: the task's source repository.
pub const SOURCE: &str = "{source}";

/// A command every step of issue work runs inside, e.g. a container:
///
Expand Down Expand Up @@ -230,6 +236,31 @@ pub struct Worker {
/// code. The check still runs on the whole worktree.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub own_files: Vec<String>,
/// A command the worker step of an eval attempt runs inside, with the
/// worker's own command appended, e.g. `sbx`:
///
/// ```toml
/// [worker]
/// sandbox = ["sbx", "run", "-w", "{tree}", "-w", "~/.claude", "-w", "~/.claude.json",
/// "--deny-read", "{source}", "--deny-read", "{private}", "--"]
/// ```
///
/// `{tree}` is the attempt's folder (required), `{private}` where the
/// hidden tests are kept, `{work}` Insi's work folder, and `{source}`
/// the task's source repository (a local folder). Without it a worker can
/// read the source checkout, usually at a commit after the fix, and the
/// hidden tests on disk; the report counts such attempts.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub sandbox: Vec<String>,
}

/// Where an eval attempt's places are, for [`Worker::sandboxed`].
#[derive(Debug, Clone, Copy)]
pub struct Places<'a> {
pub tree: &'a Path,
pub private: &'a Path,
pub work: &'a Path,
pub source: &'a str,
}

impl Worker {
Expand Down Expand Up @@ -257,6 +288,7 @@ impl Worker {
// incode's state, the spec set its spec step writes when the gate
// is on (incode src/spec.rs SPEC_SET), and the backups its edit
// hook keeps beside each edited file.
sandbox: Vec::new(),
own_files: [
".incode/**",
".incord/**",
Expand All @@ -276,7 +308,49 @@ impl Worker {
Worker {
command,
own_files: Vec::new(),
sandbox: Vec::new(),
}
}

/// Why `sandbox` cannot be used, if it cannot.
pub fn sandbox_problem(&self) -> Option<String> {
if self.sandbox.is_empty() {
return None;
}
if self.sandbox[0].trim().is_empty() {
return Some("worker.sandbox: name the program to run".to_string());
}
if !self.sandbox.iter().any(|a| a.contains(TREE)) {
return Some(format!(
"worker.sandbox has no {TREE} argument, so the attempt's folder would never be in it"
));
}
None
}

/// `argv` wrapped in `sandbox`, with its places filled in; `argv`
/// unchanged when there is no sandbox.
///
/// # Errors
/// When the sandbox names `{source}` and the task's source is not a
/// local folder: a path that does not exist could not be hidden.
pub fn sandboxed(&self, argv: Vec<String>, places: &Places) -> Result<Vec<String>, String> {
if self.sandbox.is_empty() {
return Ok(argv);
}
if self.sandbox.iter().any(|a| a.contains(SOURCE)) && !Path::new(places.source).is_dir() {
return Err(format!(
"worker.sandbox names {SOURCE}, but the task's source `{}` is not a local folder",
places.source
));
}
let fill = |a: &String| {
a.replace(TREE, &places.tree.display().to_string())
.replace(PRIVATE, &places.private.display().to_string())
.replace(WORK, &places.work.display().to_string())
.replace(SOURCE, places.source)
};
Ok(self.sandbox.iter().map(fill).chain(argv).collect())
}

/// The command for one prompt. Refused when the program is missing or no
Expand Down Expand Up @@ -363,6 +437,9 @@ impl Config {
if let Some(why) = config.sandbox.as_ref().and_then(Sandbox::problem) {
return Err(why);
}
if let Some(why) = config.worker.as_ref().and_then(Worker::sandbox_problem) {
return Err(why);
}
let mut sources = std::collections::BTreeSet::new();
for c in &config.connectors {
if c.name.trim().is_empty() || c.command.first().is_none_or(|p| p.trim().is_empty()) {
Expand Down Expand Up @@ -472,6 +549,56 @@ mod tests {

/// **A SANDBOX HOLDS THE WORK'S FOLDER, OR IS REFUSED.** The step's
/// command follows the sandbox's, with `{tree}` filled in.
#[test]
fn a_worker_sandbox_needs_the_tree_and_fills_its_places() {
let err = Config::parse(
"[worker]\ncommand = [\"w\", \"{prompt}\"]\nsandbox = [\"sbx\", \"run\", \"--\"]",
)
.expect_err("no {tree}");
assert!(err.contains("{tree}"), "{err}");
let cfg = Config::parse(
"[worker]\ncommand = [\"w\", \"{prompt}\"]\n\
sandbox = [\"sbx\", \"-w\", \"{tree}\", \"--deny-read\", \"{private}\", \"--deny-read\", \"{work}/attempts\", \"--\"]",
)
.expect("parses");
let worker = cfg.worker();
let places = Places {
tree: Path::new("/w/runs/t"),
private: Path::new("/p"),
work: Path::new("/w"),
source: "https://example.invalid/repo.git",
};
let argv = worker
.sandboxed(vec!["w".into(), "go".into()], &places)
.expect("wraps");
assert_eq!(
argv,
[
"sbx",
"-w",
"/w/runs/t",
"--deny-read",
"/p",
"--deny-read",
"/w/attempts",
"--",
"w",
"go"
]
);
// {source} must be a folder that exists, or it could not be hidden.
let mut with_source = worker.clone();
with_source.sandbox.push("{source}".into());
assert!(with_source.sandboxed(vec![], &places).is_err());
// No sandbox: the command as it was.
assert_eq!(
Worker::incode()
.sandboxed(vec!["x".into()], &places)
.expect("plain"),
["x"]
);
}

#[test]
fn a_sandbox_wraps_each_command_around_the_work_folder() {
let config = Config::parse(
Expand Down
4 changes: 2 additions & 2 deletions crates/insi-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ pub mod task;
pub mod topic;
pub mod trust;

pub use config::{Config, Price, Worker};
pub use config::{Config, Places, Price, Worker};
pub use lesson::{Lesson, Verification};
pub use signature::normalize;
pub use task::{Budget, EvalResult, Task, Usage};
pub use task::{Budget, EvalResult, Isolation, Task, Usage};
pub use trust::Trust;
20 changes: 20 additions & 0 deletions crates/insi-core/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,26 @@ pub struct EvalResult {
/// verdict: why setup's output was not reused or not kept, for one.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub notes: Vec<String>,
/// How far a worker attempt was kept from the answer ([`Isolation`]).
/// Absent on controls, and on results written before it was recorded,
/// which were not isolated.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub isolation: Option<Isolation>,
}

/// What stood between a worker and the task's answer.
///
/// The answer exists in three places a worker could reach: the fix commit
/// (in any git history that includes it), the source repository's own
/// checkout (usually at a commit after the fix), and the hidden tests.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Isolation {
/// `"base-only"`: the worker's repository holds the base commit and
/// nothing after it, so no git command in it can show the fix.
pub history: String,
/// Whether the worker ran inside `[worker] sandbox`, which is what can
/// keep it out of the source checkout and the hidden tests on disk.
pub sandboxed: bool,
}

/// Tokens a worker reported, by kind.
Expand Down
4 changes: 3 additions & 1 deletion crates/insi-eval/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ pub fn git(dir: &Path, args: &[&str]) -> Result<String, String> {

/// Run git in `dir` and return its stdout untouched (a file's contents).
pub fn git_bytes(dir: &Path, args: &[&str]) -> Result<Vec<u8>, String> {
let output = Command::new("git")
let mut command = Command::new("git");
crate::process::detach_from_caller_repository(&mut command);
let output = command
.args(args)
.current_dir(dir)
.output()
Expand Down
41 changes: 41 additions & 0 deletions crates/insi-eval/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub fn run_with(
let mut command = Command::new(&program);
// Before `env`, so a variable the caller sets on purpose still wins.
detach_from_parent_session(&mut command, std::env::vars_os().map(|(name, _)| name));
detach_from_caller_repository(&mut command);
command
.args(args)
.envs(env.iter().copied())
Expand Down Expand Up @@ -196,6 +197,31 @@ fn detach_from_parent_session(
}
}

/// Variables that point git at a repository other than the one in the folder
/// it runs in. A process started from a git hook inherits `GIT_DIR` (and
/// often `GIT_INDEX_FILE`), and Insi installs hooks: every `git` a worker, a
/// check or Insi itself then ran would act on the hook's repository instead
/// of the attempt's worktree.
pub const GIT_REDIRECTS: [&str; 9] = [
"GIT_DIR",
"GIT_WORK_TREE",
"GIT_INDEX_FILE",
"GIT_OBJECT_DIRECTORY",
"GIT_ALTERNATE_OBJECT_DIRECTORIES",
"GIT_COMMON_DIR",
"GIT_NAMESPACE",
"GIT_CEILING_DIRECTORIES",
"GIT_DISCOVERY_ACROSS_FILESYSTEM",
];

/// Start `command` in the repository of the folder it runs in, whatever the
/// caller's environment points git at ([`GIT_REDIRECTS`]).
pub fn detach_from_caller_repository(command: &mut Command) {
for name in GIT_REDIRECTS {
command.env_remove(name);
}
}

/// The program to start. On Windows a bare name like `npx` is a `.cmd`
/// script, which `Command` does not look for on its own: PATH is searched
/// for each executable extension, as a shell would.
Expand Down Expand Up @@ -271,6 +297,21 @@ mod tests {

/// **A COMMAND NEVER INHERITS THE AGENT SESSION INSI RUNS IN** — and keeps
/// the credentials it needs to start one of its own.
#[test]
fn a_command_ignores_git_redirects_from_its_caller() {
let mut command = Command::new("worker");
detach_from_caller_repository(&mut command);
for name in GIT_REDIRECTS {
assert_eq!(
fate(&command, name),
Some(None),
"{name} reached the command"
);
}
// Configuration through the environment is not a redirect.
assert_eq!(fate(&command, "GIT_CONFIG_COUNT"), None);
}

#[test]
fn a_command_is_detached_from_the_parent_session_and_keeps_its_credentials() {
let parent = [
Expand Down
Loading
Loading