Skip to content

Commit 4add179

Browse files
committed
fix: harden cron and Workboard scheduling under load
1 parent a62abee commit 4add179

19 files changed

Lines changed: 1109 additions & 89 deletions

‎extensions/workboard/src/dispatcher.test.ts‎

Lines changed: 298 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,132 @@ describe("dispatchAndStartWorkboardCards", () => {
909909
await expect(store.get(ready.id)).resolves.toMatchObject({ status: "ready" });
910910
});
911911

912+
it("does not let an expired review claim consume worker capacity", async () => {
913+
const store = new WorkboardStore(createMemoryStore());
914+
const now = Date.now();
915+
await store.create({
916+
title: "Expired review claim",
917+
status: "review",
918+
agentId: "shared-worker",
919+
metadata: {
920+
claim: {
921+
ownerId: "shared-worker",
922+
token: "expired-token",
923+
claimedAt: now - 60_000,
924+
lastHeartbeatAt: now - 60_000,
925+
expiresAt: now - 1_000,
926+
},
927+
},
928+
});
929+
const ready = await store.create({
930+
title: "Ready after expired review claim",
931+
status: "ready",
932+
agentId: "shared-worker",
933+
workspaceAccess: { unrestricted: true },
934+
});
935+
const run = vi.fn().mockResolvedValue({ runId: "run-after-expiry" });
936+
937+
const result = await dispatchAndStartWorkboardCards({
938+
store,
939+
subagent: { run },
940+
options: { maxStarts: 1 },
941+
});
942+
943+
expect(result.started).toEqual([
944+
expect.objectContaining({ cardId: ready.id, runId: "run-after-expiry" }),
945+
]);
946+
expect(run).toHaveBeenCalledOnce();
947+
});
948+
949+
it.each([
950+
{ wallClock: 10_000, dispatchNow: 50_000, expectedStarts: 1 },
951+
{ wallClock: 50_000, dispatchNow: 10_000, expectedStarts: 0 },
952+
])(
953+
"evaluates claim capacity at dispatch time instead of wall time ($wallClock, $dispatchNow)",
954+
async ({ wallClock, dispatchNow, expectedStarts }) => {
955+
vi.useFakeTimers();
956+
try {
957+
vi.setSystemTime(wallClock);
958+
const store = new WorkboardStore(createMemoryStore());
959+
await store.create({
960+
title: "Review claim with a deterministic expiry",
961+
status: "review",
962+
agentId: "shared-worker",
963+
metadata: {
964+
claim: {
965+
ownerId: "shared-worker",
966+
token: "timed-token",
967+
claimedAt: 1_000,
968+
lastHeartbeatAt: 1_000,
969+
expiresAt: 20_000,
970+
},
971+
},
972+
});
973+
const ready = await store.create({
974+
title: "Worker using dispatch-time capacity",
975+
status: "ready",
976+
agentId: "shared-worker",
977+
workspaceAccess: { unrestricted: true },
978+
});
979+
const run = vi.fn().mockResolvedValue({ runId: "run-at-dispatch-time" });
980+
981+
const result = await dispatchAndStartWorkboardCards({
982+
store,
983+
subagent: { run },
984+
options: { now: dispatchNow, maxStarts: 1 },
985+
});
986+
987+
expect(run).toHaveBeenCalledTimes(expectedStarts);
988+
expect(result.started).toHaveLength(expectedStarts);
989+
if (expectedStarts === 1) {
990+
expect(result.started[0]).toMatchObject({
991+
cardId: ready.id,
992+
runId: "run-at-dispatch-time",
993+
});
994+
}
995+
} finally {
996+
vi.useRealTimers();
997+
}
998+
},
999+
);
1000+
1001+
it("replaces an expired ready-card claim without waiting for stale-claim cleanup", async () => {
1002+
const store = new WorkboardStore(createMemoryStore());
1003+
const now = Date.now();
1004+
const card = await store.create({
1005+
title: "Ready with an expired lease",
1006+
status: "ready",
1007+
agentId: "shared-worker",
1008+
workspaceAccess: { unrestricted: true },
1009+
metadata: {
1010+
claim: {
1011+
ownerId: "retired-worker",
1012+
token: "expired-token",
1013+
claimedAt: now - 60_000,
1014+
lastHeartbeatAt: now - 60_000,
1015+
expiresAt: now - 1_000,
1016+
},
1017+
},
1018+
});
1019+
const run = vi.fn().mockResolvedValue({ runId: "run-reclaimed" });
1020+
1021+
const result = await dispatchAndStartWorkboardCards({
1022+
store,
1023+
subagent: { run },
1024+
options: { maxStarts: 1 },
1025+
});
1026+
1027+
expect(result.started).toEqual([
1028+
expect.objectContaining({ cardId: card.id, runId: "run-reclaimed" }),
1029+
]);
1030+
expect(run).toHaveBeenCalledOnce();
1031+
await expect(store.get(card.id)).resolves.toMatchObject({
1032+
status: "running",
1033+
metadata: { claim: { ownerId: "shared-worker" } },
1034+
});
1035+
expect((await store.get(card.id))?.metadata?.claim?.token).not.toBe("expired-token");
1036+
});
1037+
9121038
it.each(["worker", "other-worker"])(
9131039
"starts recoverable work after a higher-priority worker fails (next owner: %s)",
9141040
async (nextOwner) => {
@@ -1017,6 +1143,103 @@ describe("dispatchAndStartWorkboardCards", () => {
10171143
});
10181144
});
10191145

1146+
it("serializes concurrent board dispatches for the same worker", async () => {
1147+
const store = new WorkboardStore(createMemoryStore());
1148+
const ops = await store.create({
1149+
title: "Ops shared worker",
1150+
status: "ready",
1151+
boardId: "ops",
1152+
agentId: "shared-worker",
1153+
workspaceAccess: { unrestricted: true },
1154+
});
1155+
const product = await store.create({
1156+
title: "Product shared worker",
1157+
status: "ready",
1158+
boardId: "product",
1159+
agentId: "shared-worker",
1160+
workspaceAccess: { unrestricted: true },
1161+
});
1162+
const originalList = store.list.bind(store);
1163+
let snapshotCount = 0;
1164+
let releaseSnapshots: (() => void) | undefined;
1165+
const snapshotsReleased = new Promise<void>((resolve) => {
1166+
releaseSnapshots = resolve;
1167+
});
1168+
let releaseTimer: ReturnType<typeof setTimeout> | undefined;
1169+
vi.spyOn(store, "list").mockImplementation(async (options) => {
1170+
const cards = await originalList(options);
1171+
if (options === undefined && snapshotCount < 2) {
1172+
snapshotCount += 1;
1173+
if (snapshotCount === 1) {
1174+
releaseTimer = setTimeout(() => releaseSnapshots?.(), 0);
1175+
} else {
1176+
if (releaseTimer !== undefined) {
1177+
clearTimeout(releaseTimer);
1178+
}
1179+
releaseSnapshots?.();
1180+
}
1181+
await snapshotsReleased;
1182+
}
1183+
return cards;
1184+
});
1185+
const run = vi.fn().mockResolvedValue({ runId: "run-shared-board" });
1186+
1187+
const results = await Promise.all([
1188+
dispatchAndStartWorkboardCards({
1189+
store,
1190+
subagent: { run },
1191+
options: { boardId: "ops", maxStarts: 1 },
1192+
}),
1193+
dispatchAndStartWorkboardCards({
1194+
store,
1195+
subagent: { run },
1196+
options: { boardId: "product", maxStarts: 1 },
1197+
}),
1198+
]);
1199+
1200+
expect(run).toHaveBeenCalledOnce();
1201+
expect(results.flatMap((result) => result.started)).toEqual([
1202+
expect.objectContaining({ cardId: ops.id, runId: "run-shared-board" }),
1203+
]);
1204+
await expect(store.get(ops.id)).resolves.toMatchObject({
1205+
status: "running",
1206+
metadata: { claim: { ownerId: "shared-worker" } },
1207+
});
1208+
await expect(store.get(product.id)).resolves.toMatchObject({ status: "ready" });
1209+
});
1210+
1211+
it("keeps one worker slot across 25 concurrent board dispatches", async () => {
1212+
const store = new WorkboardStore(createMemoryStore());
1213+
const cards = await Promise.all(
1214+
Array.from({ length: 25 }, (_, index) =>
1215+
store.create({
1216+
title: `Concurrent worker ${index}`,
1217+
status: "ready",
1218+
boardId: `board-${index}`,
1219+
agentId: "shared-worker",
1220+
workspaceAccess: { unrestricted: true },
1221+
}),
1222+
),
1223+
);
1224+
const run = vi.fn().mockResolvedValue({ runId: "run-concurrent" });
1225+
1226+
const results = await Promise.all(
1227+
cards.map((card) =>
1228+
dispatchAndStartWorkboardCards({
1229+
store,
1230+
subagent: { run },
1231+
options: { boardId: card.metadata?.automation?.boardId, maxStarts: 1 },
1232+
}),
1233+
),
1234+
);
1235+
1236+
expect(run).toHaveBeenCalledOnce();
1237+
expect(results.flatMap((result) => result.started)).toHaveLength(1);
1238+
const persisted = await store.list();
1239+
expect(persisted.filter((card) => card.status === "running")).toHaveLength(1);
1240+
expect(persisted.filter((card) => card.status === "ready")).toHaveLength(24);
1241+
});
1242+
10201243
it("keeps claimed review cards in the owner running slot", async () => {
10211244
const store = new WorkboardStore(createMemoryStore());
10221245
const review = await store.create({
@@ -1044,6 +1267,81 @@ describe("dispatchAndStartWorkboardCards", () => {
10441267
expect(run).not.toHaveBeenCalled();
10451268
});
10461269

1270+
it("preserves an accepted worker claim when execution persistence fails", async () => {
1271+
const store = new WorkboardStore(createMemoryStore());
1272+
const card = await store.create({
1273+
title: "Worker with unavailable execution persistence",
1274+
status: "ready",
1275+
workspaceAccess: { unrestricted: true },
1276+
});
1277+
const run = vi.fn().mockResolvedValue({ runId: "run-awaiting-persistence" });
1278+
vi.spyOn(store, "update").mockRejectedValueOnce(new Error("execution persistence unavailable"));
1279+
1280+
const result = await dispatchAndStartWorkboardCards({
1281+
store,
1282+
subagent: { run },
1283+
options: { maxStarts: 1 },
1284+
});
1285+
1286+
expect(run).toHaveBeenCalledOnce();
1287+
expect(result.started).toEqual([]);
1288+
expect(result.startFailures).toEqual([
1289+
expect.objectContaining({
1290+
cardId: card.id,
1291+
error: "execution persistence unavailable",
1292+
}),
1293+
]);
1294+
await expect(store.get(card.id)).resolves.toMatchObject({
1295+
status: "running",
1296+
metadata: { claim: { ownerId: "workboard-dispatcher" } },
1297+
});
1298+
await expect(
1299+
store.heartbeat(card.id, { ownerId: "workboard-dispatcher" }),
1300+
).resolves.toMatchObject({
1301+
status: "running",
1302+
metadata: { claim: { ownerId: "workboard-dispatcher" } },
1303+
});
1304+
1305+
const retry = await dispatchAndStartWorkboardCards({
1306+
store,
1307+
subagent: { run },
1308+
options: { maxStarts: 1 },
1309+
});
1310+
1311+
expect(retry.started).toEqual([]);
1312+
expect(retry.startFailures).toEqual([]);
1313+
expect(run).toHaveBeenCalledOnce();
1314+
});
1315+
1316+
it("keeps an accepted worker running when its worker log cannot be recorded", async () => {
1317+
const store = new WorkboardStore(createMemoryStore());
1318+
const card = await store.create({
1319+
title: "Worker with unavailable logging",
1320+
status: "ready",
1321+
workspaceAccess: { unrestricted: true },
1322+
});
1323+
const run = vi.fn().mockResolvedValue({ runId: "run-without-log" });
1324+
vi.spyOn(store, "addWorkerLog").mockRejectedValueOnce(new Error("logger unavailable"));
1325+
1326+
const result = await dispatchAndStartWorkboardCards({
1327+
store,
1328+
subagent: { run },
1329+
options: { maxStarts: 1 },
1330+
});
1331+
1332+
expect(run).toHaveBeenCalledOnce();
1333+
expect(result.started).toEqual([
1334+
expect.objectContaining({ cardId: card.id, runId: "run-without-log" }),
1335+
]);
1336+
expect(result.startFailures).toEqual([]);
1337+
await expect(store.get(card.id)).resolves.toMatchObject({
1338+
status: "running",
1339+
runId: "run-without-log",
1340+
execution: { status: "running", runId: "run-without-log" },
1341+
metadata: { claim: { ownerId: "workboard-dispatcher" } },
1342+
});
1343+
});
1344+
10471345
it("blocks a card when worker start fails after claim", async () => {
10481346
const store = new WorkboardStore(createMemoryStore());
10491347
const card = await store.create({

0 commit comments

Comments
 (0)