Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
aa978d6
Scaffold, core contracts and run pipeline
mths-mcfs Nov 15, 2026
b230183
Fix four seam defects found in adversarial review
mths-mcfs Nov 15, 2026
c7a6e20
Tools runtime: jail, tolerant edit, transactional rollback, contained…
mths-mcfs Nov 15, 2026
8e8a3f0
Match protected patterns written through an alias of the workspace
mths-mcfs Nov 15, 2026
204b823
Expect forward slashes in changedFiles on every platform
mths-mcfs Nov 15, 2026
20318a5
Close races and path gaps found by the correctness review
mths-mcfs Nov 15, 2026
9a1789d
Let the kernel decide whether an oracle path resolves
mths-mcfs Nov 15, 2026
1cd0a25
Merge remote-tracking branch 'origin/feat/scaffold' into feat/tools
mths-mcfs Nov 15, 2026
28f276b
Key prepare metadata by content, not by object identity
mths-mcfs Nov 15, 2026
2dec002
CI: run once per push and cancel superseded PR runs
mths-mcfs Nov 19, 2026
581d409
Merge ci-speed/scaffold into ci-speed/tools
mths-mcfs Nov 19, 2026
535304b
fix(review): round 1
mths-mcfs Nov 19, 2026
3cba6ae
fix(review): round 2
mths-mcfs Nov 19, 2026
9824d9d
Refuse UNC workspace roots and bound edit preparation in the memory r…
mths-mcfs Nov 19, 2026
c61bedd
Refuse UNC session roots under a fixed root and count diff rows exactly
mths-mcfs Nov 19, 2026
7396009
fix(review): round 1
mths-mcfs Nov 19, 2026
fc6026b
fix(review): round 2
mths-mcfs Nov 19, 2026
370c0f1
Merge feat/scaffold (reviewed #1) into feat/tools
mths-mcfs Nov 19, 2026
4ffdf60
Merge main (squashed #1) into feat/tools
mths-mcfs Nov 19, 2026
d1e09f5
Finalize the transaction journal before reporting success
mths-mcfs Nov 19, 2026
03d8f90
fix(review): round 1
mths-mcfs Nov 20, 2026
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
17 changes: 17 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,20 @@ Third-party code
----------------
Portions of this product that are derived from third-party projects are
listed below together with their copyright notices and licenses.

Turnpack
Files: src/tools/edit-match.ts, src/tools/edit-match.test.ts (ported),
src/tools/journal.ts and src/tools/jail.ts (design and parts adapted)
Source: https://github.com/OnlyTerp/turnpack (commit b14330d)
License: Apache License, Version 2.0
Modified by the agent-code-mode authors (see the header of each file).
Turnpack's NOTICE:

Turnpack
Copyright 2026 OnlyTerp (https://github.com/OnlyTerp)

This product is licensed under the Apache License, Version 2.0.
You may obtain a copy of the License in the LICENSE file, or at
http://www.apache.org/licenses/LICENSE-2.0

Project home: https://github.com/OnlyTerp/turnpack
54 changes: 53 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"NOTICE"
],
"engines": {
"node": ">=22"
"node": ">=22.12.0"
},
"repository": {
"type": "git",
Expand All @@ -27,6 +27,7 @@
"bench": "node -e \"console.log('bench: not implemented yet')\""
},
"dependencies": {
"@anthropic-ai/sandbox-runtime": "^0.0.81",
"@modelcontextprotocol/sdk": "^1.32.2",
"zod": "^4.6.7"
},
Expand Down
16 changes: 16 additions & 0 deletions src/core/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ export class ToolError extends Error {
}
}

/**
* Thrown by `Transaction.dispose()` when the journal could not be finalized.
* `committed: false`: the journal is intact and can still be rolled back.
* `committed: true`: the journal is out of recovery's reach (the changes are
* kept), but the step that makes that durable failed.
*/
export class TransactionFinalizeError extends Error {
override readonly name = 'TransactionFinalizeError';
readonly committed: boolean;

constructor(committed: boolean, message: string) {
super(message);
this.committed = committed;
}
}

/** Message of any thrown value, without trusting its shape. */
export function errorMessage(err: unknown): string {
if (err instanceof Error) return err.message;
Expand Down
167 changes: 167 additions & 0 deletions src/core/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { makeContext } from '../testing/context.js';
import { FakeSandbox, type FakeStep } from '../testing/fake-sandbox.js';
import { MemoryToolRuntime } from '../testing/memory-tool-runtime.js';
import { StaticHostAdapter, type StaticHostAdapterOptions } from '../testing/static-host-adapter.js';
import { TransactionFinalizeError } from './errors.js';
import { resolveTimeoutMs, runScript, toRunRequest } from './run.js';
import type { Sandbox } from './types.js';

Expand Down Expand Up @@ -281,6 +282,172 @@ describe('runScript', () => {
await vi.waitFor(() => expect(events).toEqual(['rollback', 'dispose']));
});

it('reports success only after the journal is finalized', async () => {
const d = deps();
let finalize = (): void => undefined;
const gate = new Promise<void>((r) => (finalize = r));
let disposeCalls = 0;
const begin = d.tools.beginTransaction.bind(d.tools);
d.tools.beginTransaction = () => {
const tx = begin();
const dispose = tx.dispose.bind(tx);
tx.dispose = async () => {
disposeCalls++;
await gate;
await dispose();
};
return tx;
};
let settled = false;
const running = runScript(
{ code: script([{ tool: 'write', args: { path: 'a.txt', content: 'x' } }]), transaction: true, timeoutMs: 5_000 },
d,
).then((r) => ((settled = true), r));
await vi.waitFor(() => expect(disposeCalls).toBe(1));
await new Promise((r) => setTimeout(r, 20));
expect(settled).toBe(false);
finalize();
const result = await running;
expect(result.ok).toBe(true);
expect(d.tools.read('a.txt')).toBe('x');
await new Promise((r) => setTimeout(r, 10));
expect(disposeCalls).toBe(1);
});

it('fails and rolls back a successful script when the journal cannot be finalized', async () => {
const d = deps({}, { 'a.txt': 'original' });
const begin = d.tools.beginTransaction.bind(d.tools);
const rollbacks = vi.fn();
d.tools.beginTransaction = () => {
const tx = begin();
const rollback = tx.rollback.bind(tx);
tx.dispose = async () => {
throw new TransactionFinalizeError(false, 'rename failed');
};
tx.rollback = () => (rollbacks(), rollback());
return tx;
};
const result = await runScript(
{ code: script([{ tool: 'write', args: { path: 'a.txt', content: 'changed' } }]), transaction: true, timeoutMs: 5_000 },
d,
);
expect(result.ok).toBe(false);
expect(result.error).toMatchObject({ kind: 'internal', name: 'TransactionNotFinalized' });
expect(result.error?.message).toContain('rename failed');
expect(rollbacks).toHaveBeenCalledTimes(1);
expect(result.rolledBack?.restored).toEqual(['/ws/a.txt']);
expect(d.tools.read('a.txt')).toBe('original');
});

it('reports an indeterminate outcome when the journal was committed but not flushed, without rolling back', async () => {
const d = deps({}, { 'a.txt': 'original' });
const begin = d.tools.beginTransaction.bind(d.tools);
const rollbacks = vi.fn();
let disposeCalls = 0;
d.tools.beginTransaction = () => {
const tx = begin();
const rollback = tx.rollback.bind(tx);
tx.dispose = async () => {
disposeCalls++;
throw new TransactionFinalizeError(true, 'the transaction journal was finalized, but flushing it to disk failed');
};
tx.rollback = () => (rollbacks(), rollback());
return tx;
};
const result = await runScript(
{ code: script([{ tool: 'write', args: { path: 'a.txt', content: 'changed' } }]), transaction: true, timeoutMs: 5_000 },
d,
);
expect(result.ok).toBe(false);
expect(result.error).toMatchObject({ kind: 'internal', name: 'TransactionIndeterminate' });
expect(result.rolledBack).toBeUndefined();
expect(result.warnings.join('\n')).toContain('are on disk and were not rolled back');
await new Promise((r) => setTimeout(r, 20));
expect(rollbacks).not.toHaveBeenCalled();
expect(disposeCalls).toBe(1);
expect(d.tools.read('a.txt')).toBe('changed');
});

/** A transaction whose finalization waits for `gate` and then ends as `end` says. */
function slowFinalization(d: ReturnType<typeof deps>, gate: Promise<void>, end: () => Promise<void>, onDispose = (): void => undefined) {
const rollbacks = vi.fn();
const begin = d.tools.beginTransaction.bind(d.tools);
d.tools.beginTransaction = () => {
const tx = begin();
const rollback = tx.rollback.bind(tx);
const dispose = tx.dispose.bind(tx);
tx.dispose = async () => {
onDispose();
await gate;
await end();
await dispose();
};
tx.rollback = () => (rollbacks(), rollback());
return tx;
};
return rollbacks;
}

it('stops waiting for a finalization that outlasts the run, and reports neither success nor a rollback', async () => {
const d = { ...deps({}, { 'a.txt': 'original' }), stopGraceMs: 20 };
let release = (): void => undefined;
const rollbacks = slowFinalization(d, new Promise<void>((r) => (release = r)), async () => undefined);
const result = await runScript({ code: script([{ tool: 'write', args: { path: 'a.txt', content: 'changed' } }]), transaction: true, timeoutMs: 50 }, d);
expect(result.ok).toBe(false);
expect(result.error).toMatchObject({ kind: 'internal', name: 'TransactionIndeterminate' });
expect(result.error?.message).toContain('time limit');
expect(result.rolledBack).toBeUndefined();
release();
await new Promise((r) => setTimeout(r, 20));
expect(rollbacks).not.toHaveBeenCalled();
expect(d.tools.read('a.txt')).toBe('changed');
});

it('rolls back once a finalization that outlasted the run fails to commit', async () => {
const d = { ...deps({}, { 'a.txt': 'original' }), stopGraceMs: 20 };
let release = (): void => undefined;
const rollbacks = slowFinalization(d, new Promise<void>((r) => (release = r)), async () => {
throw new TransactionFinalizeError(false, 'rename failed');
});
const result = await runScript({ code: script([{ tool: 'write', args: { path: 'a.txt', content: 'changed' } }]), transaction: true, timeoutMs: 50 }, d);
expect(result.error).toMatchObject({ kind: 'internal', name: 'TransactionIndeterminate' });
release();
await vi.waitFor(() => expect(rollbacks).toHaveBeenCalledTimes(1));
await vi.waitFor(() => expect(d.tools.read('a.txt')).toBe('original'));
});

it('does not report success when the client cancels while the journal is being finalized', async () => {
const controller = new AbortController();
const d = deps({}, { 'a.txt': 'original' });
let release = (): void => undefined;
slowFinalization(d, new Promise<void>((r) => (release = r)), async () => undefined, () => controller.abort());
const result = await runScript(
{ code: script([{ tool: 'write', args: { path: 'a.txt', content: 'changed' } }]), transaction: true, timeoutMs: 5_000 },
{ ...d, signal: controller.signal },
);
release();
expect(result.ok).toBe(false);
expect(result.error).toMatchObject({ kind: 'internal', name: 'TransactionIndeterminate' });
expect(result.error?.message).toContain('cancelled');
});

it('fails and rolls back a successful script whose write is still running after the grace', async () => {
const tools = new MemoryToolRuntime({ root: '/ws', commitDelayMs: 60, honorAbortInCommit: false });
const sandbox: Sandbox = {
run: async (_code, opts) => {
void opts.onCall('write', { path: 'late.txt', content: 'late' });
await new Promise((r) => setTimeout(r, 5));
return { ok: true, value: 'done' };
},
};
const result = await runScript({ code: 'x', transaction: true, timeoutMs: 5_000 }, { ...deps(), tools, sandbox, stopGraceMs: 10 });
expect(result.ok).toBe(false);
expect(result.error).toMatchObject({ kind: 'internal', name: 'TransactionNotFinalized' });
expect(result.warnings.join('\n')).toContain('the rollback is pending');
await vi.waitFor(() => expect(tools.commits).toHaveLength(1));
await vi.waitFor(() => expect(tools.read('late.txt')).toBeUndefined());
});

it('counts policy loading against the deadline and gives the sandbox only the time left', async () => {
let clock = 0;
const budgets: number[] = [];
Expand Down
Loading
Loading