Files
autocraft/test/remote-picks.test.ts
T

320 lines
23 KiB
TypeScript

// "Run on my Task Runner" picks and Manual only: a pick goes to the picker's runners first and no one else's while it counts;
// it lapses with its time or its picker's runners and does not count while its picker cannot take it; a Manual only user's
// runners take only their picks and are no room; the queue view hides picker logins and errors from viewers.
import assert from "node:assert/strict";
import { mkdirSync, mkdtempSync } from "node:fs";
import { join } from "node:path";
import { describe, test } from "node:test";
import * as modelQueue from "../src/dash/api/model-queue.ts";
import { handleMcp } from "../src/dash/mcp/index.ts";
import * as userSettings from "../src/dash/api/user-settings.ts";
import { createToken } from "../src/dash/mcp/tokens.ts";
import type { Req, Role } from "../src/dash/types.ts";
import { claimCall, dropQueueCache, enqueueCall, type Caller } from "../src/remote/calls.ts";
import { parseStatus, STATUS_FRESH_MS, storeStatus } from "../src/remote/helpers.ts";
import { RateLimiter } from "../src/dash/server/util.ts";
import { setUserMaxCalls } from "../src/remote/max-calls.ts";
import { setInferenceMode } from "../src/remote/mode.ts";
import { pickCall, setPickMinutes, unpickCall } from "../src/remote/picks.ts";
import { PROTOCOL_VERSION, type Claim, type Tier } from "../src/remote/protocol.ts";
import { whyWaiting, type ReadyRunner } from "../src/remote/queue-view.ts";
import { remoteRoom } from "../src/remote/room.ts";
import { setUserRepos } from "../src/remote/my-repos.ts";
import { takerTest } from "../src/remote/takers.ts";
import { addFastTrack } from "../src/fasttrack.ts";
import { poolRoom, repoBatches } from "../src/agents/call.ts";
import { pool } from "../src/classify/run.ts";
import { SCRATCH } from "./scratch.ts";
import { connectDb } from "../src/db/open.ts";
mkdirSync(SCRATCH, { recursive: true });
const dir = mkdtempSync(join(SCRATCH, "remote-picks-"));
describe("picks and Manual only", async () => {
const db = await connectDb(join(dir, "t.db"));
await setInferenceMode(db, "user-local", "test");
await db.prepare("INSERT OR IGNORE INTO tracked_repos (name) VALUES ('photocraft')").run();
await db.prepare("INSERT INTO repos (name, visibility) VALUES ('photocraft', 'public') ON CONFLICT (name) DO UPDATE SET visibility = 'public'").run();
const callers: Record<string, Caller> = {};
for (const [login, role] of [["alice", "maintainer"], ["bob", "maintainer"], ["olga", "operator"], ["eve", "viewer"]] as const) {
await db.prepare("INSERT INTO access (login, role) VALUES (?, ?)").run(login, role);
if (role !== "viewer") callers[login] = { tokenId: (await createToken(db, login, "runner", { kind: "runner" })).row.id, login, role };
}
/** The user's runner reports ready now: two slots and the fast lane. */
const ready = async (login: string) => await storeStatus(db, callers[login]!.tokenId, login, parseStatus({ slots: 3, busy: 0, readiness: "ready", harness: "claude", models: { top: "opus", small: "haiku" }, reserve: { fiveHour: 0, weekly: 0 } }));
for (const login of ["alice", "bob", "olga"]) await ready(login);
let claims = 0;
const claim = (extra: Partial<Claim> = {}): Claim => ({ requestId: `picks-claim-${String(++claims).padStart(4, "0")}`, protocol: PROTOCOL_VERSION, helperVersion: "1.2.0", harness: "claude", harnessVersion: "2.1", tiers: { top: "opus", small: "haiku" }, ...extra });
const yes = async () => true;
const leased = async (login: string, extra: Partial<Claim> = {}, canWrite = yes) => (await claimCall(db, callers[login]!, claim(extra), canWrite)).lease?.id ?? null;
const user = (login: string, role: Role = "maintainer") => ({ login, role, canWrite: yes, canRead: yes });
const req = (login: string, role: Role, extra: Partial<Req> = {}) => ({ user: user(login, role), params: {}, ...extra }) as unknown as Req;
const q = async (opts: { tier?: Tier; ageMs?: number; items?: { repo: string; number: number }[]; promptId?: string } = {}) => {
const tier = opts.tier ?? "top", promptId = opts.promptId ?? "review.opus";
const id = await enqueueCall(db, { promptId, promptVersion: "1", tier, effort: "medium", askedModel: tier === "top" ? "opus" : "haiku", items: opts.items ?? [{ repo: "photocraft", number: 2 }], level: 0, maxAttempts: 2,
bundle: { promptId, promptVersion: "1", tier, effort: "medium", timeoutSeconds: 60, maxTurns: null, stdin: "x", system: null, mounts: [], mcpServers: [], keepSession: true, hints: {} } });
if (opts.ageMs) await db.prepare("UPDATE remote_calls SET since = since - ? WHERE id = ?").run(opts.ageMs, id);
return id;
};
const clear = async () => await db.prepare("UPDATE remote_calls SET state = 'failed' WHERE state IN ('queued', 'leased')").run();
const pickedBy = async (id: number) => (await db.prepare("SELECT picked_by FROM remote_calls WHERE id = ?").get(id) as { picked_by: string | null }).picked_by;
test("the picker's runners take a pick first; others pass it over; the fast lane takes only picks that fit it", async () => {
const older = await q({ ageMs: 10 * 60_000 });
const newer = await q();
await pickCall(db, user("bob"), newer);
assert.equal(await leased("bob"), newer, "bob's pick before the older call");
assert.equal(await pickedBy(newer), null, "the lease ends the pick");
const third = await q();
await pickCall(db, user("bob"), third);
assert.equal(await leased("alice"), older, "alice takes the unpicked call");
assert.equal(await leased("alice"), null, "and not bob's pick");
assert.equal(await leased("bob", { fastLane: true }), null, "a top-tier review does not fit the fast lane");
assert.equal(await leased("bob"), third);
assert.ok(await db.prepare("SELECT 1 FROM audit WHERE action = 'remote-pick' AND login = 'bob' AND target = 'photocraft#2'").get());
await clear();
});
test("a pick lapses with its time or its picker's runners; with no time limit it lasts", async () => {
await setPickMinutes(db, 0, "test");
const a = await q();
await pickCall(db, user("bob"), a);
assert.equal((await db.prepare("SELECT pick_until FROM remote_calls WHERE id = ?").get(a) as { pick_until: number | null }).pick_until, null);
assert.equal(await leased("alice"), null, "no time limit: still bob's");
await db.run(db.k.updateTable("remote_helpers").set({ reported_at: Date.now() - STATUS_FRESH_MS - 1000 }).where("login", "=", "bob"));
assert.equal(await leased("alice"), a, "none of bob's runners is fresh: the pick no longer counts");
await ready("bob");
await setPickMinutes(db, 15, "test");
const b = await q();
await pickCall(db, user("bob"), b);
assert.equal(await leased("alice"), null);
await db.prepare("UPDATE remote_calls SET pick_until = ? WHERE id = ?").run(Date.now() - 1, b);
dropQueueCache(db);
assert.equal(await leased("alice"), b, "the pick's time is up");
await clear();
});
test("a pick does not count while its picker is cooling off or at their Max calls at once", async () => {
const a = await q();
await pickCall(db, user("bob"), a);
await db.prepare("INSERT INTO remote_cooloffs (login, until, reason) VALUES ('Bob', ?, 'testing cool-off')").run(Date.now() + 60_000);
assert.equal(await leased("alice"), a, "bob is cooling off: alice's runner takes his pick");
await db.prepare("DELETE FROM remote_cooloffs WHERE login = 'bob'").run();
await setUserMaxCalls(db, "bob", 1);
const b = await q();
await pickCall(db, user("bob"), b);
assert.equal(await leased("alice"), null, "bob may still take it");
const held = await q({ ageMs: 60_000 });
await db.prepare("UPDATE remote_calls SET state = 'leased', user_login = 'bob', leased_at = ?, heartbeat_at = ? WHERE id = ?").run(Date.now(), Date.now(), held);
dropQueueCache(db);
assert.equal(await leased("alice"), b, "bob runs his Max calls at once: alice's runner takes his pick");
await setUserMaxCalls(db, "bob", null);
await clear();
});
test("a pick the picker may no longer take or released ends at their claim; two users picking at once: one wins", async () => {
const a = await q();
await pickCall(db, user("bob"), a);
assert.equal(await leased("bob", {}, async () => false), null, "bob lost write permission");
assert.equal(await pickedBy(a), null, "and with it the pick");
await pickCall(db, user("bob"), a);
await db.prepare("UPDATE remote_calls SET released_by = 'bob', released_at = ? WHERE id = ?").run(Date.now(), a);
dropQueueCache(db);
assert.equal(await leased("bob"), null);
assert.equal(await pickedBy(a), null, "a call bob's runner released loses bob's pick");
const results = await Promise.allSettled([pickCall(db, user("bob"), a), pickCall(db, user("alice"), a)]);
assert.deepEqual(results.map((r) => r.status).sort(), ["fulfilled", "rejected"]);
const winner = (await pickedBy(a))!;
const loser = winner === "bob" ? "alice" : "bob";
await assert.rejects(async () => await unpickCall(db, user(loser), a), /only operators/);
await unpickCall(db, user("olga", "operator"), a);
assert.equal(await pickedBy(a), null);
assert.ok(await db.prepare("SELECT 1 FROM audit WHERE action = 'remote-unpick' AND login = 'olga' AND details LIKE ?").get(`%"picker":"${winner}"%`));
await clear();
});
test("a Manual only user's runners take only their picks and are no room", async () => {
const a = await q();
// Both runners asked for work a moment ago (active), so both are room.
for (const login of ["alice", "bob", "olga"]) await db.run(db.k.updateTable("mcp_tokens").set({ idle_claim_at: Date.now() }).where("login", "=", login));
const room = await remoteRoom(db, 0);
await userSettings.setSettings(db, req("alice", "maintainer", { json: { manualOnly: true } }));
assert.equal((await userSettings.settings(db, req("alice", "maintainer"))).manualOnly, true);
assert.equal(await remoteRoom(db, 0), room - 2, "alice's two slots are no room");
assert.equal(await leased("alice"), null, "an unpicked call");
await pickCall(db, user("alice"), a);
assert.equal(await leased("alice"), a, "her own pick");
await userSettings.setSettings(db, req("alice", "maintainer", { json: { manualOnly: false } }));
assert.ok(await db.prepare("SELECT 1 FROM audit WHERE action = 'manual-only' AND login = 'alice'").get());
await clear();
});
test("the queue view: viewers see no picker, error or repo-less call; maintainers see them and what they may pick", async () => {
const a = await q();
const b = await q();
const loose = await q({ items: [] });
await pickCall(db, user("bob"), a);
await db.prepare("UPDATE remote_calls SET error = 'lease expired' WHERE id = ?").run(b);
const viewer = await modelQueue.get(db, req("eve", "viewer"));
assert.deepEqual(viewer.calls.map((c) => c.id), [a, b]);
assert.deepEqual(viewer.calls[0]!.picked, {});
assert.equal(viewer.calls[0]!.why.text, "Picked by a user");
assert.equal(viewer.calls[1]!.lastError, undefined);
assert.equal(viewer.runners.manualLogins, undefined);
assert.deepEqual(viewer.me, { canPick: false, manualOnly: false, picks: 0, pickMax: 0 });
const alice = await modelQueue.get(db, req("alice", "maintainer"));
assert.deepEqual(alice.calls.map((c) => [c.id, c.picked, c.pickable]), [[a, { login: "bob" }, false], [b, null, true], [loose, null, false]]);
assert.equal(alice.calls[1]!.lastError, "lease expired");
assert.deepEqual(alice.me, { canPick: true, manualOnly: false, picks: 0, pickMax: 2 });
const bob = await modelQueue.unpick(db, req("bob", "maintainer", { params: { id: String(a) } }));
assert.equal(bob.calls[0]!.picked, null);
await clear();
});
test("the MCP model call tools apply the queue page's rules: viewers list without pickers and cannot pick; a pick is audited via mcp", async () => {
const a = await q();
await pickCall(db, user("bob"), a);
const b = await q();
const env = (login: string, role: Role) => ({ db, user: () => user(login, role) as any, baseUrl: null, limiter: new RateLimiter(10, 60_000), canAnswer: false, expensive: () => 0, userLocal: true });
const rpc = async (login: string, role: Role, method: string, params?: Record<string, unknown>) => (await handleMcp(env(login, role), { jsonrpc: "2.0", id: 1, method, ...(params && { params }) }))!.result as any;
const names = async (login: string, role: Role) => (await rpc(login, role, "tools/list")).tools.map((t: { name: string }) => t.name).filter((n: string) => n.includes("model_call"));
assert.deepEqual(await names("eve", "viewer"), ["list_model_calls"]);
assert.deepEqual(await names("alice", "maintainer"), ["list_model_calls", "pick_model_call", "unpick_model_call"]);
const listed = (await rpc("eve", "viewer", "tools/call", { name: "list_model_calls", arguments: {} })).content[0].text as string;
assert.match(listed, /"picked": "a user"/);
assert.doesNotMatch(listed, /bob/);
const refused = await rpc("eve", "viewer", "tools/call", { name: "pick_model_call", arguments: { id: String(b) } });
assert.equal(refused.isError, true);
assert.match(refused.content[0].text, /only maintainers and operators/);
const taken = await rpc("alice", "maintainer", "tools/call", { name: "pick_model_call", arguments: { id: String(a) } });
assert.equal(taken.isError, true);
assert.match(taken.content[0].text, /another user picked it/);
assert.equal((await rpc("alice", "maintainer", "tools/call", { name: "pick_model_call", arguments: { id: String(b) } })).isError, undefined);
assert.equal(await pickedBy(b), "alice");
assert.ok(await db.prepare("SELECT 1 FROM audit WHERE action = 'remote-pick' AND login = 'alice' AND details LIKE '%\"via\":\"mcp\"%'").get());
await rpc("alice", "maintainer", "tools/call", { name: "unpick_model_call", arguments: { id: String(b) } });
assert.equal(await pickedBy(b), null);
await clear();
});
test("My tasks: unticked tasks are left in the queue for others; a pick and every task again still run", async () => {
const all = (await userSettings.settings(db, req("alice", "maintainer"))).taskChoices.map((t) => t.id);
assert.deepEqual((await userSettings.settings(db, req("alice", "maintainer"))).tasks, all, "every task is ticked by default");
const put = (tasks: unknown) => userSettings.setSettings(db, req("alice", "maintainer", { json: { tasks } }));
await assert.rejects(put(["review", "nope"]), /unknown task: nope/);
assert.deepEqual((await put(all.filter((t) => t !== "review"))).tasks, all.filter((t) => t !== "review"));
const step = async (id: number, s: string) => await db.prepare("UPDATE remote_calls SET step = ? WHERE id = ?").run(s, id);
const review = await q({ ageMs: 60_000 }), fix = await q();
await step(review, "opus-review");
await step(fix, "fixissue");
dropQueueCache(db);
assert.equal(await leased("alice"), fix, "the older review is passed over");
assert.equal(await leased("alice"), null);
assert.equal(await leased("bob"), review, "another user who takes reviews gets it");
const picked = await q();
await step(picked, "opus-review");
dropQueueCache(db);
await pickCall(db, user("alice"), picked);
assert.equal(await leased("alice"), picked, "a call alice picked runs although she unticked its task");
const later = await q();
await step(later, "opus-review");
dropQueueCache(db);
await put(all);
assert.equal(await db.get(db.k.selectFrom("user_settings").select((eb) => eb.lit(1).as("one")).where("login", "=", "alice").where("key", "=", "skip_tasks")), undefined, "every task ticked again stores nothing");
assert.equal(await leased("alice"), later);
await clear();
});
test("My tasks items: fast-tracked only takes calls on fast-tracked items; by me only on the user's own; a repo counts only with such an item", async () => {
const ins = db.prepare("INSERT OR IGNORE INTO items (repo, number, kind, state, title, author, created_at) VALUES ('photocraft', ?, 'pr', 'OPEN', 'x', 'dev', '2026-01-01T00:00:00Z')");
for (const n of [10, 11, 12]) await ins.run(n);
const put = (taskItems: unknown) => userSettings.setSettings(db, req("alice", "maintainer", { json: { taskItems } }));
assert.equal((await userSettings.settings(db, req("alice", "maintainer"))).taskItems, "all", "every item by default");
await assert.rejects(put("some"), /taskItems must be one of/);
await put("fast");
const at = (n: number) => [{ repo: "photocraft", number: n }];
const takes = () => takerTest(db, Date.now());
// Only alice's runner is fresh, so the taker rule reads her choice alone.
await db.run(db.k.updateTable("remote_helpers").set({ reported_at: Date.now() - STATUS_FRESH_MS - 1000 }).where("login", "<>", "alice"));
assert.equal(await takes()({ prompt_id: "review.opus", repos: ["photocraft"], step: "opus-review" }), false, "no fast-tracked item on the repo: no taker before the items are known");
assert.equal(await leased("alice"), null, "an idle claim: alice's runner is active");
assert.equal(await remoteRoom(db, 0), 0, "no open fast-tracked item at all: no room, even for a question about no repo or task");
await addFastTrack(db, "bob", "photocraft#11");
await addFastTrack(db, "alice", "photocraft#12");
assert.ok(await remoteRoom(db, 0, Date.now(), undefined, "opus-review") > 0, "room for the task: the fast-tracked items are on a repo alice takes");
await setUserRepos(db, "alice", ["deckcraft"]);
assert.equal(await remoteRoom(db, 0, Date.now(), undefined, "opus-review"), 0, "no room once alice takes only a repo without fast-tracked items");
await setUserRepos(db, "alice", []);
await q({ ageMs: 60_000, items: at(10) });
const bobs = await q({ items: at(11) }), mixed = await q({ items: [...at(10), ...at(12)] });
dropQueueCache(db);
assert.equal(await takes()({ prompt_id: "review.opus", repos: ["photocraft"], step: "opus-review" }), true, "the repo now has fast-tracked items");
assert.equal(await takes()({ prompt_id: "review.opus", repos: ["photocraft"], step: "opus-review", refs: at(10) }), false, "but not this item");
assert.equal(await leased("alice"), bobs, "the older call on an item nobody fast-tracked is passed over");
assert.equal(await leased("alice"), mixed, "one fast-tracked item of a call is enough");
assert.equal(await leased("alice"), null);
await clear();
await put("mine");
const theirs = await q({ ageMs: 60_000, items: at(11) }), own = await q({ items: at(12) });
dropQueueCache(db);
assert.equal(await leased("alice"), own, "only the item alice fast-tracked");
for (const login of ["bob", "olga"]) await ready(login);
assert.equal(await leased("bob"), theirs, "a runner that takes every item gets the rest");
assert.equal(await leased("alice"), null, "nothing else alice fast-tracked");
const r: ReadyRunner = { login: "alice", role: "maintainer", manual: false, tiers: new Set<Tier>(["top"]), free: 1, repos: null, skip: new Set<string>(), items: { refs: new Set(["photocraft#12"]), repos: new Set(["photocraft"]) } };
const c = { tier: "top" as Tier, promptId: "review.opus", step: "opus-review", repos: ["photocraft"], followUp: false, picked: null };
assert.equal(whyWaiting({ ...c, refs: at(10) }, [r], 0, () => true).code, "items-off");
assert.equal(whyWaiting({ ...c, refs: at(12) }, [r], 0, () => true).code, "behind");
assert.equal(whyWaiting({ ...c, refs: at(10) }, [{ ...r, items: { refs: new Set(), repos: new Set() } }], 0, () => true).code, "items-off", "no fast-tracked item on the repo is still about items, not repo access");
await put("all");
assert.equal(await db.get(db.k.selectFrom("user_settings").select((eb) => eb.lit(1).as("one")).where("login", "=", "alice").where("key", "=", "task_items")), undefined, "every item again stores nothing");
await clear();
});
test("My tasks items in a pool: a fast-only runner gets the fast item's unit at once and no unit on other items, even with the other runners busy", async () => {
const ins = db.prepare("INSERT OR IGNORE INTO items (repo, number, kind, state, title, author, created_at) VALUES ('photocraft', ?, 'issue', 'OPEN', 'x', 'dev', '2026-01-01T00:00:00Z')");
const units = [21, 22, 23, 20];
for (const n of units) await ins.run(n);
await addFastTrack(db, "bob", "photocraft#20");
await userSettings.setSettings(db, req("alice", "maintainer", { json: { taskItems: "fast" } }));
await db.run(db.k.updateTable("remote_helpers").set({ reported_at: Date.now() - STATUS_FRESH_MS - 1000 }).where("login", "<>", "alice"));
assert.equal(await leased("alice"), null, "an idle claim: alice's runner is active");
const refs = (i: number) => [{ repo: "photocraft", number: units[i]! }];
const started: number[] = [];
await pool(units.map((_, i) => async () => { started.push(i); }), poolRoom(db, () => ["photocraft"], refs, { promptId: "review.opus" }), { pollMs: 5 });
assert.deepEqual(started, [3], "only the fast-tracked item's unit started, though it came last");
assert.deepEqual(await repoBatches(db, units, 3, () => "photocraft", (n) => [{ repo: "photocraft", number: n }]), [[21, 22, 23], [20]], "the fast item is batched apart");
// Bob takes every item, and the calls ahead fill his slots: they take none of alice's.
await ready("bob");
assert.equal(await leased("bob"), null);
for (let k = 0; k < 4; k++) await q({ items: refs(0) });
dropQueueCache(db);
const room = poolRoom(db, () => ["photocraft"], refs, { promptId: "review.opus" });
assert.equal(await room.room(0), 0, "no room for an unfast unit: bob is full");
assert.ok(await room.room(3) > 0, "room for the fast unit on alice's runner");
await userSettings.setSettings(db, req("alice", "maintainer", { json: { taskItems: "all" } }));
for (const login of ["alice", "bob", "olga"]) await ready(login);
await clear();
});
test("why a call waits", () => {
const r = (o: Partial<ReadyRunner> = {}): ReadyRunner => ({ login: "bob", role: "maintainer", manual: false, tiers: new Set<Tier>(["top"]), free: 1, repos: null, skip: new Set<string>(), ...o });
const c = { tier: "top" as Tier, promptId: "review.opus", step: "opus-review", repos: ["photocraft"], followUp: false, picked: null };
const write = () => true;
assert.equal(whyWaiting({ ...c, picked: { login: "bob" } }, [r()], 0, write).text, "Picked by bob");
assert.equal(whyWaiting({ ...c, followUp: true }, [r()], 0, write).code, "session");
assert.equal(whyWaiting({ ...c, tier: "mid" }, [r()], 0, write).code, "no-runner");
assert.equal(whyWaiting(c, [r({ manual: true })], 0, write).code, "manual-only");
assert.equal(whyWaiting(c, [r()], 0, () => false).code, "no-repo-access");
assert.equal(whyWaiting({ ...c, repos: [] }, [r()], 0, write).code, "no-repo-access", "a repo-less call goes to operators only");
assert.equal(whyWaiting(c, [r({ skip: new Set(["review"]) })], 0, write).code, "task-off");
assert.equal(whyWaiting(c, [r({ skip: new Set(["review"]) }), r({ free: 0 })], 0, write).code, "busy", "the free runner does not take reviews");
assert.equal(whyWaiting(c, [r({ free: 0 }), r({ manual: true })], 0, write).code, "busy");
assert.deepEqual(whyWaiting(c, [r()], 3, write), { code: "behind", text: "3 calls ahead" });
});
});