Spaces:
Paused
Paused
File size: 19,287 Bytes
2df2e7e 77374bd 2df2e7e 77374bd 2df2e7e 77374bd 2df2e7e 77374bd 2df2e7e 77374bd 2df2e7e 77374bd 2df2e7e 77374bd 2df2e7e 77374bd 2df2e7e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 | import { randomUUID } from "node:crypto";
import { afterEach, beforeAll, describe, expect, it } from "vitest";
import { ObjectId } from "mongodb";
import { collections, ready } from "$lib/server/database";
import { createTestLocals } from "$lib/server/api/__tests__/testHelpers";
import type {
AgentControlMailboxEntry,
AgentControlPayload,
Generation,
} from "$lib/types/Generation";
import { AGENT_APPROVAL_NOT_ACTIVE_RESPONSE } from "$lib/types/AgentControl";
import { MessageUpdateType } from "$lib/types/MessageUpdate";
import {
agentControlBridgeId,
agentControlRpcEnvelope,
claimAgentControl,
completeAgentControl,
dispatchAgentControl,
enqueueAgentControl,
fingerprintAgentControlPayload,
sealAgentControls,
waitForAgentControlCompletion,
type AgentControlTarget,
} from "./AgentControlMailbox";
import { POST as agentControlPOST } from "../../../routes/conversation/[id]/agent/control/+server";
beforeAll(async () => {
await ready;
});
afterEach(async () => {
await Promise.all([
collections.conversations.deleteMany({ sessionId: /^agent-control-/ }),
collections.workspaces.deleteMany({ sessionId: /^agent-control-/ }),
collections.generations.deleteMany({ sessionId: /^agent-control-/ }),
]);
});
describe.sequential("AgentControlMailbox", () => {
it("preserves synchronous steer then follow-up semantics with one bounded slot", async () => {
const seeded = await seedAgentControlRun();
const steer = await enqueueAgentControl({
target: seeded.target,
id: "steer-1",
payload: { kind: "steer", message: "change course" },
});
expect(steer.status).toBe("queued");
if (steer.status !== "queued") throw new Error("expected queued steer");
const waiting = waitForAgentControlCompletion({
generationId: seeded.generationId,
controlId: steer.control.id,
fingerprint: steer.control.fingerprint,
pollIntervalMs: 1,
timeoutMs: 2_000,
});
const claimedSteer = await claimAgentControl({
generationId: seeded.generationId,
relay: seeded.relay,
});
expect(claimedSteer?.payload).toEqual({ kind: "steer", message: "change course" });
expect(
await completeAgentControl({
generationId: seeded.generationId,
relay: seeded.relay,
control: requireControl(claimedSteer),
})
).toBe(true);
expect((await waiting).status).toBe("completed");
// The first HTTP request waits for its receipt, so the immediately
// sequential second request sees an empty mailbox rather than a 409.
const followUp = await enqueueAgentControl({
target: seeded.target,
id: "follow-up-1",
payload: { kind: "follow-up", message: "then run the tests" },
});
expect(followUp.status).toBe("queued");
const claimedFollowUp = await claimAgentControl({
generationId: seeded.generationId,
relay: seeded.relay,
});
expect(claimedFollowUp?.payload).toEqual({
kind: "follow-up",
message: "then run the tests",
});
});
it("lets a replacement fence reclaim a crash-partial dispatch with the same bridge id", async () => {
const seeded = await seedAgentControlRun();
const queued = await enqueueAgentControl({
target: seeded.target,
id: "reclaim-me",
payload: { kind: "steer", message: "safe retry" },
});
if (queued.status !== "queued") throw new Error("expected queued control");
const first = requireControl(
await claimAgentControl({ generationId: seeded.generationId, relay: seeded.relay })
);
const firstEnvelope = agentControlRpcEnvelope(first);
const replacement = { owner: "relay-replacement", fence: seeded.relay.fence + 1 };
await collections.generations.updateOne(
{ generationId: seeded.generationId },
{
$set: {
"agent.relay.owner": replacement.owner,
"agent.relay.fence": replacement.fence,
"agent.relay.expiresAt": new Date(Date.now() + 60_000),
},
}
);
const reclaimed = requireControl(
await claimAgentControl({ generationId: seeded.generationId, relay: replacement })
);
expect(reclaimed.dispatch).toMatchObject(replacement);
expect(agentControlRpcEnvelope(reclaimed)).toEqual(firstEnvelope);
expect(firstEnvelope).toMatchObject({
id: agentControlBridgeId("reclaim-me"),
type: "steer",
_chatUiExpectedPromptId: seeded.promptId,
message: "safe retry",
});
const dispatched: Array<{ command: Record<string, unknown>; timeoutMs?: number }> = [];
await dispatchAgentControl(
{
command: async (command, options) => {
dispatched.push({ command, timeoutMs: options?.timeoutMs });
return { success: true };
},
},
reclaimed,
{ timeoutMs: 1_234 }
);
expect(dispatched).toEqual([{ command: firstEnvelope, timeoutMs: 1_234 }]);
expect(
await completeAgentControl({
generationId: seeded.generationId,
relay: seeded.relay,
control: first,
})
).toBe(false);
expect(
await completeAgentControl({
generationId: seeded.generationId,
relay: replacement,
control: reclaimed,
})
).toBe(true);
});
it("atomically orders enqueue against terminal sealing", async () => {
const enqueueWinner = await seedAgentControlRun();
const queued = await enqueueAgentControl({
target: enqueueWinner.target,
id: "before-seal",
payload: { kind: "compact" },
});
expect(queued.status).toBe("queued");
expect(
await sealAgentControls({
generationId: enqueueWinner.generationId,
relay: enqueueWinner.relay,
})
).toBe("pending");
const sealWinner = await seedAgentControlRun();
expect(
await sealAgentControls({
generationId: sealWinner.generationId,
relay: sealWinner.relay,
})
).toBe("sealed");
expect(
await enqueueAgentControl({
target: sealWinner.target,
id: "after-seal",
payload: { kind: "steer", message: "too late" },
})
).toEqual({ status: "gone" });
});
it("binds approval to its persisted request and makes the first response idempotent", async () => {
const seeded = await seedAgentControlRun({ approvalRequestId: "approval-1" });
const approve: AgentControlPayload = {
kind: "approval",
requestId: "approval-1",
response: { confirmed: true, always: true },
};
const first = await enqueueAgentControl({
target: seeded.target,
id: "approval-command-a",
payload: approve,
});
expect(first.status).toBe("queued");
const duplicate = await enqueueAgentControl({
target: seeded.target,
id: "approval-command-b",
payload: approve,
});
expect(duplicate.status).toBe("duplicate");
const conflicting = await enqueueAgentControl({
target: seeded.target,
id: "approval-command-c",
payload: {
kind: "approval",
requestId: "approval-1",
response: { cancelled: true },
},
});
expect(conflicting).toEqual({ status: "conflict" });
const claimed = requireControl(
await claimAgentControl({ generationId: seeded.generationId, relay: seeded.relay })
);
expect(agentControlRpcEnvelope(claimed)).toEqual({
id: agentControlBridgeId("approval-command-a"),
type: "extension_ui_response",
_chatUiExpectedPromptId: seeded.promptId,
requestId: "approval-1",
confirmed: true,
});
expect(
await completeAgentControl({
generationId: seeded.generationId,
relay: seeded.relay,
control: claimed,
})
).toBe(true);
expect(
await enqueueAgentControl({
target: seeded.target,
id: "missing-approval",
payload: {
kind: "approval",
requestId: "approval-does-not-exist",
response: { confirmed: true },
},
})
).toEqual({ status: "invalid-approval" });
expect(
await sealAgentControls({
generationId: seeded.generationId,
relay: seeded.relay,
})
).toBe("sealed");
const completedRetry = await enqueueAgentControl({
target: seeded.target,
id: "approval-command-d",
payload: approve,
});
expect(completedRetry.status).toBe("completed");
});
it("waits on the canonical mailbox id for a duplicate approval from another tab", async () => {
const seeded = await seedAgentControlRun({ approvalRequestId: "approval-two-tabs" });
const payload: AgentControlPayload = {
kind: "approval",
requestId: "approval-two-tabs",
response: { confirmed: true },
};
const originalId = randomUUID();
const original = await enqueueAgentControl({
target: seeded.target,
id: originalId,
payload,
});
expect(original.status).toBe("queued");
const duplicateResponse = postAgentControlRoute(
seeded,
{
action: "approval",
controlId: randomUUID(),
generationId: seeded.generationId,
messageId: seeded.target.messageId,
requestId: payload.requestId,
response: payload.response,
},
AbortSignal.timeout(2_000)
);
// Leave the original in the mailbox long enough for the second request to
// classify it as a duplicate before publishing its canonical receipt.
await new Promise((resolve) => setTimeout(resolve, 100));
const claimed = requireControl(
await claimAgentControl({ generationId: seeded.generationId, relay: seeded.relay })
);
expect(claimed.id).toBe(originalId);
expect(
await completeAgentControl({
generationId: seeded.generationId,
relay: seeded.relay,
control: claimed,
})
).toBe(true);
expect((await duplicateResponse).status).toBe(200);
});
it("returns an exact completed queue retry before rejecting its stale snapshot", async () => {
const seeded = await seedAgentControlRun();
const expected = { steering: ["old steer"], followUp: ["old follow-up"] };
const replacement = { steering: ["new steer"], followUp: [] };
await setAgentQueue(seeded, expected);
const controlId = randomUUID();
const payload: AgentControlPayload = {
kind: "queue-replace",
expected,
replacement,
};
const queued = await enqueueAgentControl({ target: seeded.target, id: controlId, payload });
expect(queued.status).toBe("queued");
const claimed = requireControl(
await claimAgentControl({ generationId: seeded.generationId, relay: seeded.relay })
);
expect(
await completeAgentControl({
generationId: seeded.generationId,
relay: seeded.relay,
control: claimed,
})
).toBe(true);
await setAgentQueue(seeded, replacement);
const completedRetry = await postAgentControlRoute(seeded, {
action: "queue-replace",
controlId,
generationId: seeded.generationId,
messageId: seeded.target.messageId,
expected,
replacement,
});
expect(completedRetry.status).toBe(200);
await expect(
postAgentControlRoute(seeded, {
action: "queue-replace",
controlId,
generationId: seeded.generationId,
messageId: seeded.target.messageId,
expected,
replacement: { steering: [], followUp: ["different"] },
})
).rejects.toMatchObject({
status: 409,
body: { message: "Agent control id was reused" },
});
await expect(
postAgentControlRoute(seeded, {
action: "queue-replace",
controlId: randomUUID(),
generationId: seeded.generationId,
messageId: seeded.target.messageId,
expected,
replacement,
})
).rejects.toMatchObject({
status: 409,
body: { message: "The Agent queue changed; refresh before editing it" },
});
});
it("rejects a consumed historical approval even after its bounded receipt is evicted", async () => {
const seeded = await seedAgentControlRun({ approvalRequestId: "approval-stale" });
const payload: AgentControlPayload = {
kind: "approval",
requestId: "approval-stale",
response: { confirmed: true, always: true },
};
const queued = await enqueueAgentControl({
target: seeded.target,
id: "approval-first",
payload,
});
expect(queued.status).toBe("queued");
const claimed = requireControl(
await claimAgentControl({ generationId: seeded.generationId, relay: seeded.relay })
);
expect(
await completeAgentControl({
generationId: seeded.generationId,
relay: seeded.relay,
control: claimed,
})
).toBe(true);
await collections.generations.updateOne(
{ generationId: seeded.generationId },
{ $unset: { "agent.controlReceipts": "" } }
);
expect(
await enqueueAgentControl({
target: seeded.target,
id: "approval-replay-after-eviction",
payload,
})
).toEqual({ status: "invalid-approval" });
});
it("returns a typed public stale response when an approval expires before submission", async () => {
const seeded = await seedAgentControlRun({ approvalRequestId: "approval-expired" });
await collections.generations.updateOne(
{ generationId: seeded.generationId },
{
$pull: { "agent.pendingApprovalIds": "approval-expired" },
$set: { updatedAt: new Date(Date.now() + 1_000) },
}
);
const response = await postAgentControlRoute(seeded, {
action: "approval",
controlId: randomUUID(),
generationId: seeded.generationId,
messageId: seeded.target.messageId,
requestId: "approval-expired",
response: { confirmed: true },
});
expect(response.status).toBe(409);
expect(response.headers.get("cache-control")).toBe("no-store");
expect(await response.json()).toEqual(AGENT_APPROVAL_NOT_ACTIVE_RESPONSE);
expect(
await collections.generations.findOne(
{ generationId: seeded.generationId },
{ projection: { "agent.control": 1 } }
)
).not.toHaveProperty("agent.control");
});
it("fingerprints equivalent payloads deterministically and conflicting approvals differently", () => {
expect(
fingerprintAgentControlPayload({
kind: "approval",
requestId: "same",
response: { always: true, confirmed: true },
})
).toBe(
fingerprintAgentControlPayload({
kind: "approval",
requestId: "same",
response: { confirmed: true, always: true },
})
);
expect(
fingerprintAgentControlPayload({
kind: "approval",
requestId: "same",
response: { confirmed: true },
})
).not.toBe(
fingerprintAgentControlPayload({
kind: "approval",
requestId: "same",
response: { cancelled: true },
})
);
});
it("dispatches queue replacement as a direct, semantic compare-and-swap command", async () => {
const seeded = await seedAgentControlRun();
const queued = await enqueueAgentControl({
target: seeded.target,
id: "replace-queue-1",
payload: {
kind: "queue-replace",
expected: { steering: ["old steer"], followUp: ["old follow-up"] },
replacement: { steering: ["new steer"], followUp: [] },
},
});
if (queued.status !== "queued") throw new Error("expected queued control");
const claimed = requireControl(
await claimAgentControl({ generationId: seeded.generationId, relay: seeded.relay })
);
const envelope = agentControlRpcEnvelope(claimed);
expect(envelope).toEqual({
id: agentControlBridgeId("replace-queue-1"),
type: "replace_queue",
_chatUiExpectedPromptId: seeded.promptId,
expected: { steering: ["old steer"], followUp: ["old follow-up"] },
replacement: { steering: ["new steer"], followUp: [] },
});
const dispatched: Record<string, unknown>[] = [];
await dispatchAgentControl(
{
command: async (command) => {
dispatched.push(command);
return { success: true };
},
},
claimed
);
expect(dispatched).toEqual([envelope]);
});
it("prefers durable user ownership when a logged-in request also has a session id", async () => {
const seeded = await seedAgentControlRun({ includeUserAndSession: true });
expect(
await enqueueAgentControl({
target: seeded.target,
id: "logged-in-control",
payload: { kind: "steer", message: "keep the user owner" },
})
).toMatchObject({ status: "queued" });
});
});
function requireControl(control: AgentControlMailboxEntry | null): AgentControlMailboxEntry {
if (!control) throw new Error("expected a claimed Agent control");
return control;
}
async function seedAgentControlRun(
options: { approvalRequestId?: string; includeUserAndSession?: boolean } = {}
) {
const now = new Date();
const conversationId = new ObjectId();
const workspaceId = new ObjectId();
const generationId = randomUUID();
const messageId = randomUUID();
const promptId = randomUUID();
const sessionId = `agent-control-${randomUUID()}`;
const userId = options.includeUserAndSession ? new ObjectId() : undefined;
const relay = { owner: randomUUID(), fence: 7 };
const target: AgentControlTarget = {
generationId,
conversationId,
workspaceId,
messageId,
owner: { ...(userId ? { userId } : {}), sessionId },
};
await collections.conversations.insertOne({
_id: conversationId,
...(userId ? { userId } : {}),
sessionId,
mode: "agent",
workspaceId,
messages: [
{
id: messageId,
from: "assistant",
content: "",
generationId,
updates: options.approvalRequestId
? [
{
type: MessageUpdateType.AgentApproval,
requestId: options.approvalRequestId,
method: "confirm",
title: "Allow command?",
},
]
: [],
createdAt: now,
updatedAt: now,
},
],
createdAt: now,
updatedAt: now,
} as never);
await collections.workspaces.insertOne({
_id: workspaceId,
conversationId,
...(userId ? { userId } : {}),
sessionId,
repo: { kind: "blank" },
flavor: "cpu-basic",
model: "fake/model",
thinkingLevel: "medium",
policy: "ask",
sandbox: {
status: "ready",
id: `sandbox-${workspaceId.toString()}`,
generation: `sandbox-generation-${workspaceId.toString()}`,
namespace: "tester",
baseUrl: `https://sandbox-${workspaceId.toString()}.example.test`,
nonce: "nonce",
image: "node:22",
flavor: "cpu-basic",
createdAt: now,
},
spend: { accruedUsd: 0 },
createdAt: now,
updatedAt: now,
});
await collections.generations.insertOne({
_id: new ObjectId(),
generationId,
conversationId,
workspaceId,
messageId,
...(userId ? { userId } : {}),
sessionId,
mode: "agent",
status: "running",
seq: 0,
lastHeartbeatAt: now,
startedAt: now,
createdAt: now,
updatedAt: now,
agent: {
sourceSequence: 0,
prompt: { id: promptId, message: "do the work" },
relay: { ...relay, expiresAt: new Date(Date.now() + 60_000) },
...(options.approvalRequestId ? { pendingApprovalIds: [options.approvalRequestId] } : {}),
},
} satisfies Generation);
return { generationId, promptId, relay, target, sessionId };
}
function postAgentControlRoute(
seeded: Awaited<ReturnType<typeof seedAgentControlRun>>,
body: Record<string, unknown>,
signal?: AbortSignal
): Promise<Response> {
return agentControlPOST({
params: { id: seeded.target.conversationId.toString() },
locals: createTestLocals({ sessionId: seeded.sessionId }),
request: new Request("http://localhost/agent/control", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
signal,
}),
} as never) as Promise<Response>;
}
async function setAgentQueue(
seeded: Awaited<ReturnType<typeof seedAgentControlRun>>,
queue: { steering: string[]; followUp: string[] }
): Promise<void> {
const updated = await collections.conversations.updateOne(
{ _id: seeded.target.conversationId, "messages.id": seeded.target.messageId },
{
$set: {
"messages.$.updates": [{ type: MessageUpdateType.AgentQueue, ...queue }],
},
}
);
if (updated.matchedCount !== 1) throw new Error("Expected to update the Agent queue fixture");
}
|