Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Turn auto sleep on for agent37 computers

Vishnu43d6f13c481e
diff --git a/adrs/agent37-sandbox-backend.md b/adrs/agent37-sandbox-backend.md
index 94bb9f4ca..7237a00dd 100644
--- a/adrs/agent37-sandbox-backend.md
+++ b/adrs/agent37-sandbox-backend.md
@@ -9,7 +9,7 @@ What the PR adds
- `SANDBOX_BACKEND=agent37` with `AGENT37_API_KEY`. Optional knobs: `AGENT37_API_BASE_URL`, `AGENT37_NAME_PREFIX`, `AGENT37_TEMPLATE`, `AGENT37_CPUS`, `AGENT37_MEMORY_GB`, `AGENT37_DISK_GB`, `AGENT37_EGRESS_PROXY_URL`, and the shared `SANDBOX_TIMEOUT_SEC`.
- `src/sandbox/agent37-sandbox.ts`, modeled on the smolmachines backend. Provision creates the instance and waits for it to run. Run is our exec endpoint. Teardown with destroy deletes the instance. Files move over the same exec endpoint in base64 chunks, the way the Porter backend does it, because our API has no separate file transfer. Process sessions, home backup, ro layers and blob staging come from the shared exec helpers unchanged.
- Exec on our side returns within 280 seconds and caps each stream at 512 KiB. Longer commands run detached and are polled; larger output is spooled to disk and read back in chunks, so the agent never sees a truncated result.
-- Auto sleep is off. Agent37 counts only traffic through the instance's routed port as activity, and qm drives the machine through exec alone, so an idle timer would freeze it mid-command. A command sent to a stopped or sleeping instance (an operator stop, a past-due park) starts it first, then runs, and retries while a checkpoint is still in flight. We are making exec count as activity on our side; once that ships, sleeping when idle is a one-line change here.
+- Auto sleep is on. Agent37 checkpoints an idle computer after five minutes and bills disk alone while it sleeps; a command sent to a sleeping computer wakes it first, and every command counts as activity on the Agent37 side (stamped while it runs), so a long command is never frozen mid-flight. A command sent to a stopped instance (an operator stop, a past-due park) starts it first, then runs; while a sleep checkpoint is still in flight the API answers 409 and the backend retries until it clears.
- Profile: `writablePersistence: "resident_disk"`, `processSessions: true`, `egressEnforcement: "none"`. Our sandboxes reach the public internet and nothing private, but there is no per-domain allowlist, so the backend declares none rather than pretend.
What it does not do
diff --git a/src/sandbox/agent37-sandbox.ts b/src/sandbox/agent37-sandbox.ts
index 05f67829d..24b231f6c 100644
--- a/src/sandbox/agent37-sandbox.ts
+++ b/src/sandbox/agent37-sandbox.ts
@@ -165,7 +165,7 @@ export function createAgent37Sandbox(workspace: WorkspaceStore, opts: Agent37San
const info = await apiJson<InstanceInfo>(
"POST",
"/v1/instances",
- { template, name, resources, auto_sleep: false },
+ { template, name, resources, auto_sleep: true },
CREATE_TIMEOUT_MS,
);
await ensureRunning(info.id);
@@ -201,7 +201,8 @@ export function createAgent37Sandbox(workspace: WorkspaceStore, opts: Agent37San
const detail = (await first.text()).slice(0, 200);
if (first.status === 404) idByName.delete(name);
const notRunning = first.status === 400 && /running instances/i.test(detail);
- if (first.status !== 404 && !notRunning) {
+ const freezing = first.status === 409;
+ if (first.status !== 404 && !notRunning && !freezing) {
throw new Error(`agent37 exec ${name}: http ${first.status} ${detail}`);
}
const retryId = await instanceIdFor(name);
diff --git a/test/agent37-sandbox.test.ts b/test/agent37-sandbox.test.ts
index b6a9afe4f..4ff051088 100644
--- a/test/agent37-sandbox.test.ts
+++ b/test/agent37-sandbox.test.ts
@@ -134,7 +134,7 @@ test("a shared advisory lock prevents duplicate instances across core replicas",
assert.equal(fake.names().filter((name) => name === a.id).length, 1);
});
-test("exec on a sleeping instance wakes it and retries", async () => {
+test("exec on a sleeping instance wakes it without a separate start", async () => {
const h = await sandbox.provision(layers);
await sandbox.writeFile(h, "keep.txt", "still here\n");
fake.sleep(h.id);
@@ -142,15 +142,15 @@ test("exec on a sleeping instance wakes it and retries", async () => {
assert.equal(r.code, 0);
assert.equal(r.stdout, "still here\n");
assert.equal(fake.instance(h.id)?.status, "running");
- assert.ok(fake.calls.some((c) => c.method === "POST" && c.path.endsWith("/start")));
+ assert.ok(!fake.calls.some((c) => c.method === "POST" && c.path.endsWith("/start")));
});
-test("exec during the sleep checkpoint retries start until the freeze clears", async () => {
+test("exec during the sleep checkpoint retries until the freeze clears", async () => {
const h = await sandbox.provision(layers);
fake.sleep(h.id, { freezing: 2 });
const r = await sandbox.run(h, "echo woke");
assert.equal(r.stdout.trim(), "woke");
- assert.equal(fake.calls.filter((c) => c.method === "POST" && c.path.endsWith("/start")).length, 3);
+ assert.equal(fake.calls.filter((c) => c.method === "POST" && c.path.endsWith("/start")).length, 2);
});
test("exec on a stopping instance waits for it to stop, then starts it", async () => {
@@ -247,11 +247,11 @@ test("timeouts beyond the API's sync exec ceiling run detached and poll to compl
assert.equal(leftovers.stdout.trim(), "0");
});
-test("create requests the template, the default shape and no auto sleep", async () => {
+test("create requests the template, the default shape and auto sleep", async () => {
const h = await sandbox.provision(layers);
const created = fake.instance(h.id);
assert.equal(created?.template, "agent37-codex");
- assert.equal(created?.autoSleep, false);
+ assert.equal(created?.autoSleep, true);
assert.deepEqual(created?.resources, { cpu: 2, memory: 4, disk: 8 });
});
diff --git a/test/support/fake-agent37.ts b/test/support/fake-agent37.ts
index df4e47ebb..0ff06e1cf 100644
--- a/test/support/fake-agent37.ts
+++ b/test/support/fake-agent37.ts
@@ -143,8 +143,19 @@ export function installFakeAgent37(): FakeAgent37 {
const m = instances.get(decodeURIComponent(sub[1]!));
if (!m || m.status === "deleted") return error(404, "not_found", "Instance not found.");
if (sub[2] === "exec") {
+ if (m.status === "sleeping") {
+ if (m.freezing) {
+ m.freezing--;
+ return error(409, "try_again", "A sleep checkpoint is in flight. Retry in a few seconds.");
+ }
+ m.status = "running";
+ }
if (m.status !== "running") {
- return error(400, "invalid_request", "Only running instances can execute commands.");
+ return error(
+ 400,
+ "invalid_request",
+ "Only running instances can execute commands (a sleeping instance is woken first).",
+ );
}
const body = JSON.parse(toBuf(init?.body).toString() || "{}") as { command?: string };
const script = body.command ?? "";