# botjail

A policy jail and flight recorder for AI agents.

Give every agent its own identity and a written policy — which files, which hosts, which programs, which desktop
rights. Check each action against it. Keep a hash-chained record of everything. Scan what the agent reads for prompt
injection, and when a page tries to hijack it, cut off that one agent without locking out the person it works for.
One JavaScript file, no dependencies, Node 18+ and browsers.

> botjail **decides and records**. It is not an operating-system sandbox and does not contain a process by itself.
> Wire `request()` into your tool layer and pair it with real containment (a VM, a container, or an OS feature such
> as Windows' Microsoft Execution Containers). botjail is the policy, the identity and the audit trail around it.

```
npm test                       # 18 checks, well under a second
node bin/botjail.js run trap   # watch a hijack get caught
```

## Why it exists

On 7 October 2026 Microsoft shipped Microsoft Execution Containers (MXC) on Windows 11: each agent gets its own
identity, runs in a sandbox, keeps its own audit trail, and a hijacked agent can be cut off without locking the user
out of the same files. The code is MIT-licensed and open. It answers a real problem — an agent that can read the web
and touch your disk is one poisoned page away from acting against you — but the enforcement lives deep in the OS, and
the management pieces (Intune, Entra) are still "coming soon".

botjail is the same idea as a small, readable library you can run anywhere today: the **policy**, the **identity**,
the **injection check** and the **audit trail**, decided in user space so you can test them, replay them and reason
about them. The containment underneath is yours to choose.

## What it does

| Part | Function | What it gives you |
|---|---|---|
| Policy | `policy(p)`, `decide(pol, action)` | a written allow-list of files (glob), hosts, programs and desktop rights, with a built-in secrets denylist |
| Identity | `createJail().admit(policy)` | each agent gets its own identity (`agent:coder#1`) and policy fingerprint; the user is never jailed |
| Decision | `jail.request(agent, action)` | allow or deny by policy, in `enforce`, `learn` or `permissive` mode |
| Injection | `scanInjection(text)`, `jail.ingest(agent, text)` | signs of prompt injection in a page the agent read, and a taint level per agent |
| Hijack | (automatic) | a tainted agent that then reaches for a secret, a new host or a program is quarantined on that turn |
| Cut-off | `quarantine`, `revoke`, `release` | stop one agent; the user and every other agent keep working |
| Record | `jail.log()`, `verify(log)` | a hash-chained flight recorder; any edit is caught at the first changed entry |
| Learn | `suggest(log, agent)`, `replay(log, policy)` | a least-privilege policy from a trusted run, and what any policy would have done to a recorded log |
| Lint | `lint(policy)` | warnings about wide grants (whole disk, `*` hosts, a shell program) with a risk score |

## Jail an agent

```js
const botjail = require("botjail");

const jail = botjail.createJail();                 // the user is never jailed
const coder = jail.admit({
  agent: "coder",
  mode: "enforce",
  files: { read: ["~/projects/app/**"], write: ["~/projects/app/dist/**"] },
  network: { allow: ["registry.npmjs.org", "*.github.com"] },
  programs: ["node"]
});

jail.request(coder, { kind: "file.read", target: "~/projects/app/src/index.js" }); // { allow: true, … }
jail.request(coder, { kind: "file.read", target: "~/.ssh/id_ed25519" });           // { allow: false, reason: "secret files are never granted" }
jail.request(coder, { kind: "net",       target: "https://pastebin.example/up" }); // { allow: false, reason: "host … not on the list" }
```

Action kinds: `file.read`, `file.write`, `file.delete`, `net`, `exec`, `clipboard`, `input`, `screen`.
Secrets (`~/.ssh/**`, `**/.env`, `**/*.pem`, `~/.aws/**`, …) are denied even if a file rule would match them.

## Catch a hijack

A page the agent reads carries a hidden instruction. `ingest` scans it and taints the agent; the next time the agent
acts on it — reaching for a secret, a new host or a program — the jail catches that turn and quarantines it.

```js
jail.ingest(coder, pageText, "https://docs.example.com/notes");  // risk 0.92: override, secrets bait, send-it-out
jail.request(coder, { kind: "file.read", target: "~/.ssh/id_ed25519" });
// { allow: false, hijack: true }  — and coder is now quarantined
jail.request(otherAgent, …);  // still works
jail.request(jail.user, …);   // the user was never jailed
```

Quarantine is reversible (`release`); `revoke` is permanent. Either way the log keeps the whole story.

## Learn a least-privilege policy

Run an agent once on a trusted task, then ask for the smallest policy that would have allowed it:

```js
const run = botjail.run("normal");
const policy = botjail.suggest(run.log, run.agent);   // folders, hosts and programs it actually used
botjail.lint(policy);                                 // { score: 0, warnings: [{ text: "no wide grants found" }] }
botjail.replay(run.log, policy);                      // { total: 5, allowed: 5, changed: [] }
```

`suggest` stops at a quarantine, so learning from a run that was hijacked never widens the policy to the attack.

## Command line

```
node bin/botjail.js run trap --log run.jsonl    # a scripted hijack, recorded
node bin/botjail.js verify run.jsonl            # check the hash chain
node bin/botjail.js check policy.json net https://evil.test   # decide one action (exit 2 = deny)
node bin/botjail.js lint policy.json            # wide-grant warnings and a risk score
node bin/botjail.js scan page.txt               # injection signs in a page
node bin/botjail.js suggest run.jsonl --agent agent:coder#1
node bin/botjail.js replay run.jsonl policy.json
```

## The model

An action is `{kind, target}`. A policy is an allow-list; `decide()` returns the first matching rule or a denial.
A jail wraps policies with identities, modes and a hash-chained log. `ingest()` raises an agent's **taint** when a
page looks like an injection; a denied action by a tainted agent against a secret, a new host or a program is a
**hijack**, and the agent is quarantined on that turn. The log chains each entry's hash into the next, so the record
is tamper-evident: `verify()` names the first entry that does not match. `suggest()` reads a trusted run back into the
smallest policy that fits it; `replay()` shows what any policy would have changed.

## Limits

- botjail is a decision-and-record layer, not containment. If you never route an agent's real file and network calls
  through `request()`, nothing is enforced. Pair it with an OS sandbox or a VM.
- Injection scanning is pattern-based and English-centred: it catches common shapes (override lines, secrets bait,
  send-it-out lines), not every attack. Treat a low score as "no obvious signs", not "safe".
- A policy is only as good as its globs. Use `lint()` and prefer a `suggest()`-ed policy from a trusted run over a
  hand-written `**`.
- The clock and identities are in-process; persist the log yourself (it is plain JSON lines) if you need it to outlive
  the run.

## References

- Microsoft. Microsoft Execution Containers: policy-driven containment for AI agents. Windows Developer Blog,
  7 October 2026; repository github.com/microsoft/mxc (MIT).
- OWASP. Top 10 for Large Language Model Applications — LLM01: Prompt Injection (2025).
- J. H. Saltzer, M. D. Schroeder. The Protection of Information in Computer Systems. Proceedings of the IEEE 63(9)
  (1975) — least privilege.
- B. Lampson. Protection. ACM Operating Systems Review 8(1) (1974) — the access matrix.
- R. C. Merkle. Protocols for Public Key Cryptosystems. IEEE S&P (1980) — hash chains.

## Licence

MIT. Not connected to Microsoft or any agent vendor; the scenarios and pages in the demos are inert fixtures.
