File size: 37,505 Bytes
5cb63c1 | 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 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 | // Process supervisor tests cover lifecycle, restart, and termination behavior.
import { performance } from "node:perf_hooks";
import { expectDefined } from "@openclaw/normalization-core";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { createDeferred } from "../../../test/helpers/promise.js";
import { mockProcessPlatform } from "../../test-utils/vitest-spies.js";
import {
createSilentIdleArgv,
createStubChildAdapter,
createWriteStdoutArgv,
spawnChild,
type StubChildAdapter,
} from "./supervisor.test-support.js";
import type { ManagedRun } from "./types.js";
const { createChildAdapterMock, createPtyAdapterMock } = vi.hoisted(() => ({
createChildAdapterMock: vi.fn(),
createPtyAdapterMock: vi.fn(),
}));
vi.mock("./adapters/child.js", () => ({
createChildAdapter: async (
...args: Parameters<typeof import("./adapters/child.js").createChildAdapter>
) => ({
adapter: await createChildAdapterMock(...args),
ready: Promise.resolve(),
}),
}));
vi.mock("./adapters/pty.js", () => ({
createPtyAdapter: createPtyAdapterMock,
}));
let createProcessSupervisor: typeof import("./supervisor.js").createProcessSupervisor;
describe("process supervisor", () => {
beforeAll(async () => {
vi.resetModules();
({ createProcessSupervisor } = await import("./supervisor.js"));
});
beforeEach(() => {
createChildAdapterMock.mockReset();
createPtyAdapterMock.mockReset();
vi.useRealTimers();
});
afterEach(() => {
vi.useRealTimers();
vi.restoreAllMocks();
});
it("passes private secret input and exact environment to the child adapter", async () => {
const adapter = createStubChildAdapter();
createChildAdapterMock.mockResolvedValue(adapter);
const secretInput = { fd: 3, createData: () => Buffer.from("secret") };
const supervisor = createProcessSupervisor();
const run = await spawnChild(supervisor, {
argv: createWriteStdoutArgv("ok"),
exactEnv: true,
secretInput,
});
adapter.settle(0);
await run.wait();
expect(createChildAdapterMock).toHaveBeenCalledWith(
expect.objectContaining({ exactEnv: true, secretInput }),
);
});
it("enforces no-output timeout for silent processes", async () => {
vi.useFakeTimers();
const adapter = createStubChildAdapter({
onKill: (signal, current) => {
current.settle(null, signal ?? "SIGKILL");
},
});
createChildAdapterMock.mockResolvedValue(adapter);
const supervisor = createProcessSupervisor();
const run = await spawnChild(supervisor, {
argv: createSilentIdleArgv(),
timeoutMs: 300,
noOutputTimeoutMs: 5,
stdinMode: "pipe-closed",
});
const exitPromise = run.wait();
await vi.advanceTimersByTimeAsync(5);
const exit = await exitPromise;
const expectedTimeoutSignal = process.platform === "win32" ? "SIGKILL" : "SIGTERM";
expect(adapter.killMock).toHaveBeenCalledWith(expectedTimeoutSignal);
if (process.platform !== "win32") {
await vi.advanceTimersByTimeAsync(5_000);
expect(adapter.killMock).not.toHaveBeenCalledWith("SIGKILL");
}
expect(exit.reason).toBe("no-output-timeout");
expect(exit.noOutputTimedOut).toBe(true);
expect(exit.timedOut).toBe(true);
});
it("coalesces overlapping Windows deadline cancellation while hard kill is pending", async () => {
vi.useFakeTimers();
mockProcessPlatform("win32");
const adapter = createStubChildAdapter();
createChildAdapterMock.mockResolvedValue(adapter);
const supervisor = createProcessSupervisor();
const run = await spawnChild(supervisor, {
argv: createSilentIdleArgv(),
timeoutMs: 20,
noOutputTimeoutMs: 5,
stdinMode: "pipe-closed",
});
const exitPromise = run.wait();
await vi.advanceTimersByTimeAsync(5);
expect(adapter.killMock).toHaveBeenCalledTimes(1);
expect(adapter.killMock).toHaveBeenCalledWith("SIGKILL");
await vi.advanceTimersByTimeAsync(15);
expect(adapter.killMock).toHaveBeenCalledTimes(1);
adapter.settle(null, "SIGKILL");
const exit = await exitPromise;
expect(exit.reason).toBe("no-output-timeout");
});
it("escalates cancellation to SIGKILL when graceful shutdown does not settle", async () => {
vi.useFakeTimers();
const adapter = createStubChildAdapter({
onKill: (signal, current) => {
if (signal === "SIGKILL") {
current.settle(null, signal);
}
},
});
createChildAdapterMock.mockResolvedValue(adapter);
const supervisor = createProcessSupervisor();
const run = await spawnChild(supervisor, {
argv: createSilentIdleArgv(),
timeoutMs: 1_000,
stdinMode: "pipe-closed",
});
const exitPromise = run.wait();
run.cancel("manual-cancel");
expect(adapter.killMock).toHaveBeenCalledWith("SIGTERM");
expect(adapter.killMock).not.toHaveBeenCalledWith("SIGKILL");
await vi.advanceTimersByTimeAsync(4_999);
expect(adapter.killMock).not.toHaveBeenCalledWith("SIGKILL");
await vi.advanceTimersByTimeAsync(1);
const exit = await exitPromise;
expect(adapter.killMock).toHaveBeenCalledWith("SIGKILL");
expect(exit.reason).toBe("manual-cancel");
expect(exit.exitSignal).toBe("SIGKILL");
});
it.each(["child", "pty"] as const)(
"cancels a %s process by run ID while its adapter is starting",
async (mode) => {
const adapter = createStubChildAdapter({
onKill: (signal, current) => {
current.settle(null, signal ?? "SIGTERM");
},
});
const startup = createDeferred<StubChildAdapter>();
if (mode === "pty") {
createPtyAdapterMock.mockReturnValueOnce(startup.promise);
} else {
createChildAdapterMock.mockReturnValueOnce(startup.promise);
}
const supervisor = createProcessSupervisor();
const runId = `cancel-starting-${mode}`;
const pendingRun =
mode === "pty"
? supervisor.spawn({
runId,
mode: "pty",
argv: ["/bin/sh", "-c", "printf cancelled"],
scopeKey: "scope:cancel-starting",
})
: spawnChild(supervisor, {
runId,
scopeKey: "scope:cancel-starting",
argv: createSilentIdleArgv(),
});
expect(mode === "pty" ? createPtyAdapterMock : createChildAdapterMock).toHaveBeenCalledOnce();
supervisor.cancel(runId, "manual-cancel");
expect(adapter.killMock).not.toHaveBeenCalled();
startup.resolve(adapter);
const run = await pendingRun;
expect(adapter.killMock).toHaveBeenCalledWith("SIGKILL");
await run.waitForExtinction?.();
expect(adapter.disposeMock).toHaveBeenCalled();
await expect(run.wait()).resolves.toMatchObject({ reason: "manual-cancel" });
expect(run.activity.resultSettled).toBe(true);
},
);
it.each([
{
timeoutField: "timeoutMs" as const,
reason: "overall-timeout" as const,
},
{
timeoutField: "noOutputTimeoutMs" as const,
reason: "no-output-timeout" as const,
},
])("bounds a hung child adapter construction with $reason", async ({ timeoutField, reason }) => {
vi.useFakeTimers();
const startup = createDeferred<StubChildAdapter>();
createChildAdapterMock.mockReturnValueOnce(startup.promise);
const supervisor = createProcessSupervisor();
const runId = `hung-adapter-${reason}`;
const pendingRun = spawnChild(supervisor, {
runId,
argv: createSilentIdleArgv(),
[timeoutField]: 25,
stdinMode: "pipe-closed",
});
expect(createChildAdapterMock).toHaveBeenCalledOnce();
await vi.advanceTimersByTimeAsync(25);
const constructionState = await Promise.race([
pendingRun.then(() => "settled" as const),
Promise.resolve().then(() => "pending" as const),
]);
expect(constructionState).toBe("settled");
const run = await pendingRun;
await expect(run.wait()).resolves.toMatchObject({
reason,
timedOut: true,
noOutputTimedOut: reason === "no-output-timeout",
});
expect(run.activity.resultSettled).toBe(true);
const killed = createDeferred();
const lateAdapter = createStubChildAdapter({ onKill: () => killed.resolve() });
startup.resolve(lateAdapter);
await killed.promise;
expect(lateAdapter.killMock).toHaveBeenCalledWith("SIGKILL");
expect(lateAdapter.disposeMock).not.toHaveBeenCalled();
lateAdapter.settle(null, "SIGKILL");
await run.waitForExtinction?.();
expect(lateAdapter.disposeMock).toHaveBeenCalled();
});
it("fences new runs and drains an unscoped startup during shutdown", async () => {
const adapter = createStubChildAdapter({
onKill: (signal, current) => {
current.settle(null, signal ?? "SIGTERM");
},
});
const startup = createDeferred<StubChildAdapter>();
createChildAdapterMock.mockReturnValueOnce(startup.promise);
const supervisor = createProcessSupervisor();
const pendingRun = spawnChild(supervisor, {
runId: "shutdown-starting",
argv: createSilentIdleArgv(),
});
const shutdown = supervisor.shutdown();
await expect(
spawnChild(supervisor, {
runId: "shutdown-late",
argv: createSilentIdleArgv(),
}),
).rejects.toThrow("process supervisor is shut down");
expect(createChildAdapterMock).toHaveBeenCalledTimes(1);
startup.resolve(adapter);
const run = await pendingRun;
await expect(shutdown).resolves.toBeUndefined();
expect(adapter.killMock).toHaveBeenCalledWith("SIGKILL");
expect(adapter.disposeMock).toHaveBeenCalled();
await expect(run.wait()).resolves.toMatchObject({ reason: "manual-cancel" });
});
it("keeps shutdown fenced when live ownership extinction fails", async () => {
const extinction = createDeferred();
const adapter = createStubChildAdapter({
onKill: (signal, current) => {
current.settle(null, signal ?? "SIGTERM");
},
});
adapter.waitForExtinction = () => extinction.promise;
createChildAdapterMock.mockResolvedValueOnce(adapter);
const supervisor = createProcessSupervisor();
await spawnChild(supervisor, {
runId: "shutdown-failed-extinction",
argv: createSilentIdleArgv(),
});
const shutdown = supervisor.shutdown();
extinction.reject(new Error("owner extinction failed"));
await expect(shutdown).rejects.toThrow("owner extinction failed");
await expect(
spawnChild(supervisor, {
runId: "shutdown-after-failed-extinction",
argv: createSilentIdleArgv(),
}),
).rejects.toThrow("process supervisor is shut down");
expect(createChildAdapterMock).toHaveBeenCalledOnce();
});
it("cancels every starting scoped process without canceling a later arrival", async () => {
const runCount = 16;
const startups = Array.from({ length: runCount }, () => createDeferred<StubChildAdapter>());
const adapters = Array.from({ length: runCount }, (_unused, index) =>
createStubChildAdapter({
pid: 7_000 + index,
onKill: (signal, current) => {
current.settle(null, signal ?? "SIGTERM");
},
}),
);
const laterAdapter = createStubChildAdapter({ pid: 8_000 });
let adapterIndex = 0;
createChildAdapterMock.mockImplementation(() => {
const index = adapterIndex++;
if (index === runCount) {
return Promise.resolve(laterAdapter);
}
const startup = startups[index];
if (!startup) {
throw new Error(`unexpected scope cancellation startup ${index}`);
}
return startup.promise;
});
const supervisor = createProcessSupervisor();
const pendingRuns = Array.from({ length: runCount }, (_unused, index) =>
spawnChild(supervisor, {
runId: `cancel-scope-starting-${index}`,
scopeKey: "scope:cancel-every-start",
argv: createSilentIdleArgv(),
}),
);
expect(createChildAdapterMock).toHaveBeenCalledTimes(runCount);
supervisor.cancelScope("scope:cancel-every-start", "manual-cancel");
for (const adapter of adapters) {
expect(adapter.killMock).not.toHaveBeenCalled();
}
const laterRun = await spawnChild(supervisor, {
runId: "cancel-scope-later-arrival",
scopeKey: "scope:cancel-every-start",
argv: createSilentIdleArgv(),
});
expect(laterAdapter.killMock).not.toHaveBeenCalled();
for (let index = runCount - 1; index >= 0; index -= 1) {
const startup = startups[index];
const adapter = adapters[index];
if (!startup || !adapter) {
throw new Error(`missing scope cancellation startup ${index}`);
}
startup.resolve(adapter);
}
const runs = await Promise.all(pendingRuns);
await Promise.all(
runs.map((run) => expectDefined(run.waitForExtinction, "cancelled construction cleanup")()),
);
for (const adapter of adapters) {
expect(adapter.killMock).toHaveBeenCalledWith("SIGKILL");
expect(adapter.disposeMock).toHaveBeenCalled();
}
await expect(Promise.all(runs.map((run) => run.wait()))).resolves.toEqual(
Array.from({ length: runCount }, () => expect.objectContaining({ reason: "manual-cancel" })),
);
expect(laterAdapter.killMock).not.toHaveBeenCalled();
laterAdapter.settle(0);
await expect(laterRun.wait()).resolves.toMatchObject({ reason: "exit" });
});
it("cancels a replacement that is fenced behind an earlier startup", async () => {
const first = createStubChildAdapter({
onKill: (signal, current) => {
current.settle(null, signal ?? "SIGTERM");
},
});
const later = createStubChildAdapter();
const firstStartup = createDeferred<StubChildAdapter>();
createChildAdapterMock.mockReturnValueOnce(firstStartup.promise).mockResolvedValueOnce(later);
const supervisor = createProcessSupervisor();
const firstRunPromise = spawnChild(supervisor, {
runId: "cancel-fenced-first",
scopeKey: "scope:cancel-fenced",
argv: createSilentIdleArgv(),
});
let replacementCurrent = true;
const replacementPromise = spawnChild(supervisor, {
runId: "cancel-fenced-replacement",
scopeKey: "scope:cancel-fenced",
replaceExistingScope: true,
argv: createSilentIdleArgv(),
onCancel: () => {
replacementCurrent = false;
},
});
expect(createChildAdapterMock).toHaveBeenCalledTimes(1);
supervisor.cancelScope("scope:cancel-fenced", "manual-cancel");
expect(replacementCurrent).toBe(false);
const laterPromise = spawnChild(supervisor, {
runId: "cancel-fenced-later",
scopeKey: "scope:cancel-fenced",
argv: createSilentIdleArgv(),
});
firstStartup.resolve(first);
const [firstRun, replacementRun, laterRun] = await Promise.all([
firstRunPromise,
replacementPromise,
laterPromise,
]);
expect(createChildAdapterMock).toHaveBeenCalledTimes(2);
expect(first.killMock).toHaveBeenCalledWith("SIGKILL");
expect(first.disposeMock).toHaveBeenCalled();
expect(replacementRun.pid).toBeUndefined();
expect(replacementRun.waitForExtinction).toBeUndefined();
expect(later.killMock).not.toHaveBeenCalled();
later.settle(0);
await expect(
Promise.all([firstRun.wait(), replacementRun.wait(), laterRun.wait()]),
).resolves.toEqual([
expect.objectContaining({ reason: "manual-cancel" }),
expect.objectContaining({ reason: "manual-cancel" }),
expect.objectContaining({ reason: "exit" }),
]);
});
it("cancels prior scoped run when replaceExistingScope is enabled", async () => {
const first = createStubChildAdapter({
onKill: (signal, current) => {
current.settle(null, signal ?? "SIGKILL");
},
});
const second = createStubChildAdapter();
createChildAdapterMock.mockResolvedValueOnce(first).mockResolvedValueOnce(second);
const supervisor = createProcessSupervisor();
const firstRun = await spawnChild(supervisor, {
scopeKey: "scope:a",
argv: [process.execPath, "-e", "setTimeout(() => {}, 80)"],
timeoutMs: 1_000,
stdinMode: "pipe-open",
});
const secondRun = await spawnChild(supervisor, {
scopeKey: "scope:a",
replaceExistingScope: true,
argv: createWriteStdoutArgv("new"),
timeoutMs: 1_000,
stdinMode: "pipe-closed",
});
second.emitStdout("new");
second.settle(0);
const firstExit = await firstRun.wait();
const secondExit = await secondRun.wait();
expect(first.killMock).toHaveBeenCalledWith("SIGTERM");
expect(["manual-cancel", "signal"]).toContain(firstExit.reason);
expect(secondExit.reason).toBe("exit");
expect(secondExit.stdout).toBe("new");
});
it("waits for an in-flight scoped startup before replacing its run", async () => {
const first = createStubChildAdapter({
onKill: (signal, current) => {
current.settle(null, signal ?? "SIGTERM");
},
});
const second = createStubChildAdapter();
const firstStartup = createDeferred<StubChildAdapter>();
createChildAdapterMock.mockReturnValueOnce(firstStartup.promise).mockResolvedValueOnce(second);
const supervisor = createProcessSupervisor();
const firstRunPromise = spawnChild(supervisor, {
runId: "scoped-start-first",
scopeKey: "scope:overlap",
argv: createSilentIdleArgv(),
});
const replacementPromise = spawnChild(supervisor, {
runId: "scoped-start-replacement",
scopeKey: "scope:overlap",
replaceExistingScope: true,
argv: createWriteStdoutArgv("replacement"),
});
expect(createChildAdapterMock).toHaveBeenCalledTimes(1);
firstStartup.resolve(first);
const [firstRun, replacement] = await Promise.all([firstRunPromise, replacementPromise]);
expect(createChildAdapterMock).toHaveBeenCalledTimes(2);
expect(first.killMock).toHaveBeenCalledWith("SIGTERM");
await expect(firstRun.wait()).resolves.toMatchObject({ reason: "manual-cancel" });
second.emitStdout("replacement");
second.settle(0);
await expect(replacement.wait()).resolves.toMatchObject({
reason: "exit",
stdout: "replacement",
});
});
it("starts same-scope runs concurrently when replacement is not requested", async () => {
const first = createStubChildAdapter();
const second = createStubChildAdapter();
const firstStartup = createDeferred<StubChildAdapter>();
const secondStartup = createDeferred<StubChildAdapter>();
createChildAdapterMock
.mockReturnValueOnce(firstStartup.promise)
.mockReturnValueOnce(secondStartup.promise);
const supervisor = createProcessSupervisor();
const firstRunPromise = spawnChild(supervisor, {
scopeKey: "scope:shared-concurrent",
argv: createSilentIdleArgv(),
});
const secondRunPromise = spawnChild(supervisor, {
scopeKey: "scope:shared-concurrent",
argv: createSilentIdleArgv(),
});
expect(createChildAdapterMock).toHaveBeenCalledTimes(2);
secondStartup.resolve(second);
const secondRun = await secondRunPromise;
firstStartup.resolve(first);
const firstRun = await firstRunPromise;
expect(first.killMock).not.toHaveBeenCalled();
expect(second.killMock).not.toHaveBeenCalled();
first.settle(0);
second.settle(0);
await expect(Promise.all([firstRun.wait(), secondRun.wait()])).resolves.toEqual([
expect.objectContaining({ reason: "exit" }),
expect.objectContaining({ reason: "exit" }),
]);
});
it("does not cancel a newer run while an earlier scoped replacement is pending", async () => {
const first = createStubChildAdapter({
onKill: (signal, current) => {
current.settle(null, signal ?? "SIGTERM");
},
});
const replacementAdapter = createStubChildAdapter();
const newerAdapter = createStubChildAdapter();
const firstStartup = createDeferred<StubChildAdapter>();
const replacementStartup = createDeferred<StubChildAdapter>();
createChildAdapterMock
.mockReturnValueOnce(firstStartup.promise)
.mockReturnValueOnce(replacementStartup.promise)
.mockResolvedValueOnce(newerAdapter);
const supervisor = createProcessSupervisor();
const firstRunPromise = spawnChild(supervisor, {
runId: "replacement-fence-first",
scopeKey: "scope:replacement-fence",
argv: createSilentIdleArgv(),
});
const replacementPromise = spawnChild(supervisor, {
runId: "replacement-fence-replacement",
scopeKey: "scope:replacement-fence",
replaceExistingScope: true,
argv: createSilentIdleArgv(),
});
const newerRunPromise = spawnChild(supervisor, {
runId: "replacement-fence-newer",
scopeKey: "scope:replacement-fence",
argv: createSilentIdleArgv(),
});
expect(createChildAdapterMock).toHaveBeenCalledTimes(1);
firstStartup.resolve(first);
const firstRun = await firstRunPromise;
await vi.waitFor(() => {
expect(createChildAdapterMock).toHaveBeenCalledTimes(2);
});
expect(first.killMock).toHaveBeenCalledWith("SIGTERM");
expect(newerAdapter.killMock).not.toHaveBeenCalled();
replacementStartup.resolve(replacementAdapter);
const [replacement, newerRun] = await Promise.all([replacementPromise, newerRunPromise]);
expect(createChildAdapterMock).toHaveBeenCalledTimes(3);
expect(replacementAdapter.killMock).not.toHaveBeenCalled();
expect(newerAdapter.killMock).not.toHaveBeenCalled();
replacementAdapter.settle(0);
newerAdapter.settle(0);
await expect(
Promise.all([firstRun.wait(), replacement.wait(), newerRun.wait()]),
).resolves.toEqual([
expect.objectContaining({ reason: "manual-cancel" }),
expect.objectContaining({ reason: "exit" }),
expect.objectContaining({ reason: "exit" }),
]);
});
it("waits for every concurrent same-scope startup before replacing the scope", async () => {
const runCount = 8;
const startups = Array.from({ length: runCount }, () => createDeferred<StubChildAdapter>());
const adapters = Array.from({ length: runCount }, (_unused, index) =>
createStubChildAdapter({
pid: 5_000 + index,
onKill: (signal, current) => {
current.settle(null, signal ?? "SIGTERM");
},
}),
);
const replacementAdapter = createStubChildAdapter({ pid: 6_000 });
let adapterIndex = 0;
createChildAdapterMock.mockImplementation(() => {
const index = adapterIndex++;
if (index === runCount) {
return Promise.resolve(replacementAdapter);
}
const startup = startups[index];
if (!startup) {
throw new Error(`unexpected shared-scope startup ${index}`);
}
return startup.promise;
});
const supervisor = createProcessSupervisor();
const pendingRuns = Array.from({ length: runCount }, (_unused, index) =>
spawnChild(supervisor, {
runId: `shared-scope-${index}`,
scopeKey: "scope:shared-replacement",
argv: createSilentIdleArgv(),
}),
);
const replacementPromise = spawnChild(supervisor, {
runId: "shared-scope-replacement",
scopeKey: "scope:shared-replacement",
replaceExistingScope: true,
argv: createWriteStdoutArgv("replacement"),
});
expect(createChildAdapterMock).toHaveBeenCalledTimes(runCount);
for (let index = runCount - 1; index >= 0; index -= 1) {
const startup = startups[index];
const adapter = adapters[index];
if (!startup || !adapter) {
throw new Error(`missing shared-scope startup ${index}`);
}
startup.resolve(adapter);
}
const runs = await Promise.all(pendingRuns);
const replacement = await replacementPromise;
expect(createChildAdapterMock).toHaveBeenCalledTimes(runCount + 1);
for (const adapter of adapters) {
expect(adapter.killMock).toHaveBeenCalledWith("SIGTERM");
}
await expect(Promise.all(runs.map((run) => run.wait()))).resolves.toEqual(
Array.from({ length: runCount }, () => expect.objectContaining({ reason: "manual-cancel" })),
);
replacementAdapter.emitStdout("replacement");
replacementAdapter.settle(0);
await expect(replacement.wait()).resolves.toMatchObject({
reason: "exit",
stdout: "replacement",
});
});
it("keeps startup independent for different replacement scopes", async () => {
const first = createStubChildAdapter();
const second = createStubChildAdapter();
const firstStartup = createDeferred<StubChildAdapter>();
const secondStartup = createDeferred<StubChildAdapter>();
createChildAdapterMock
.mockReturnValueOnce(firstStartup.promise)
.mockReturnValueOnce(secondStartup.promise);
const supervisor = createProcessSupervisor();
const firstRunPromise = spawnChild(supervisor, {
scopeKey: "scope:independent-a",
replaceExistingScope: true,
argv: createSilentIdleArgv(),
});
const secondRunPromise = spawnChild(supervisor, {
scopeKey: "scope:independent-b",
replaceExistingScope: true,
argv: createSilentIdleArgv(),
});
expect(createChildAdapterMock).toHaveBeenCalledTimes(2);
secondStartup.resolve(second);
const secondRun = await secondRunPromise;
firstStartup.resolve(first);
const firstRun = await firstRunPromise;
expect(first.killMock).not.toHaveBeenCalled();
expect(second.killMock).not.toHaveBeenCalled();
first.settle(0);
second.settle(0);
await expect(Promise.all([firstRun.wait(), secondRun.wait()])).resolves.toEqual([
expect.objectContaining({ reason: "exit" }),
expect.objectContaining({ reason: "exit" }),
]);
});
it("starts a scoped replacement after the previous adapter fails to start", async () => {
const second = createStubChildAdapter();
createChildAdapterMock
.mockRejectedValueOnce(new Error("first adapter could not start"))
.mockResolvedValueOnce(second);
const supervisor = createProcessSupervisor();
const firstRunPromise = spawnChild(supervisor, {
runId: "failed-scoped-start",
scopeKey: "scope:recover-start",
argv: createSilentIdleArgv(),
});
const replacementPromise = spawnChild(supervisor, {
runId: "recovered-scoped-start",
scopeKey: "scope:recover-start",
replaceExistingScope: true,
argv: createWriteStdoutArgv("recovered"),
});
await expect(firstRunPromise).rejects.toThrow("first adapter could not start");
const replacement = await replacementPromise;
second.emitStdout("recovered");
second.settle(0);
await expect(replacement.wait()).resolves.toMatchObject({
reason: "exit",
stdout: "recovered",
});
});
it("keeps only the newest run across 64 concurrent same-scope replacements", async () => {
const runCount = 64;
const adapters = Array.from({ length: runCount }, (_unused, index) =>
createStubChildAdapter({
pid: 2_000 + index,
onKill: (signal, current) => {
current.settle(null, signal ?? "SIGTERM");
},
}),
);
let adapterIndex = 0;
createChildAdapterMock.mockImplementation(async () => {
const adapter = adapters[adapterIndex++];
if (!adapter) {
throw new Error("unexpected concurrent supervisor startup");
}
return adapter;
});
const supervisor = createProcessSupervisor();
const pendingRuns = Array.from({ length: runCount }, (_unused, index) =>
spawnChild(supervisor, {
runId: `scope-stress-${index}`,
scopeKey: "scope:stress",
replaceExistingScope: true,
argv: createSilentIdleArgv(),
}),
);
const runs = await Promise.all(pendingRuns);
for (const [index, adapter] of adapters.entries()) {
if (index === runCount - 1) {
expect(adapter.killMock, `newest scope owner ${index}`).not.toHaveBeenCalled();
} else {
expect(adapter.killMock, `superseded scope owner ${index}`).toHaveBeenCalledWith("SIGTERM");
}
}
const newestAdapter = adapters[runCount - 1];
const newestRun = runs[runCount - 1];
if (!newestAdapter || !newestRun) {
throw new Error("expected the newest scoped process");
}
newestAdapter.settle(0);
const exits = await Promise.all(runs.map((run) => run.wait()));
for (const [index, exit] of exits.entries()) {
expect(exit.reason, `scoped run ${index}`).toBe(
index === runCount - 1 ? "exit" : "manual-cancel",
);
}
});
it("continues replacing scoped runs across interleaved startup failures", async () => {
const runCount = 48;
const adapters = new Map<number, StubChildAdapter>();
let adapterIndex = 0;
createChildAdapterMock.mockImplementation(async () => {
const index = adapterIndex++;
if (index % 7 === 3) {
throw new Error(`adapter ${index} could not start`);
}
const adapter = createStubChildAdapter({
pid: 3_000 + index,
onKill: (signal, current) => {
current.settle(null, signal ?? "SIGTERM");
},
});
adapters.set(index, adapter);
return adapter;
});
const supervisor = createProcessSupervisor();
const pendingRuns = Array.from({ length: runCount }, (_unused, index) =>
spawnChild(supervisor, {
runId: `scope-recovery-${index}`,
scopeKey: "scope:interleaved-recovery",
replaceExistingScope: true,
argv: createSilentIdleArgv(),
}),
);
const results = await Promise.allSettled(pendingRuns);
const running: ManagedRun[] = [];
for (const [index, result] of results.entries()) {
if (index % 7 === 3) {
expect(result.status, `failed adapter ${index}`).toBe("rejected");
if (result.status === "rejected") {
expect(result.reason).toMatchObject({ message: `adapter ${index} could not start` });
}
continue;
}
expect(result.status, `started adapter ${index}`).toBe("fulfilled");
if (result.status === "fulfilled") {
running.push(result.value);
}
}
const newestAdapter = adapters.get(runCount - 1);
if (!newestAdapter) {
throw new Error("expected the final replacement adapter");
}
for (const [index, adapter] of adapters) {
if (index === runCount - 1) {
expect(adapter.killMock, `newest adapter ${index}`).not.toHaveBeenCalled();
} else {
expect(adapter.killMock, `superseded adapter ${index}`).toHaveBeenCalledWith("SIGTERM");
}
}
newestAdapter.settle(0);
const exits = await Promise.all(running.map((run) => run.wait()));
expect(exits.filter((exit) => exit.reason === "exit")).toHaveLength(1);
expect(exits.filter((exit) => exit.reason === "manual-cancel")).toHaveLength(
running.length - 1,
);
});
it("starts 24 independent scoped processes without a global startup bottleneck", async () => {
const scopeCount = 24;
const startups = Array.from({ length: scopeCount }, () => createDeferred<StubChildAdapter>());
const adapters = Array.from({ length: scopeCount }, (_unused, index) =>
createStubChildAdapter({ pid: 4_000 + index }),
);
let adapterIndex = 0;
createChildAdapterMock.mockImplementation(() => {
const startup = startups[adapterIndex++];
if (!startup) {
throw new Error("unexpected independent supervisor startup");
}
return startup.promise;
});
const supervisor = createProcessSupervisor();
const pendingRuns = Array.from({ length: scopeCount }, (_unused, index) =>
spawnChild(supervisor, {
runId: `independent-scope-${index}`,
scopeKey: `scope:independent-${index}`,
replaceExistingScope: true,
argv: createSilentIdleArgv(),
}),
);
expect(createChildAdapterMock).toHaveBeenCalledTimes(scopeCount);
for (let index = scopeCount - 1; index >= 0; index -= 1) {
const startup = startups[index];
const adapter = adapters[index];
if (!startup || !adapter) {
throw new Error(`missing independent scope ${index}`);
}
startup.resolve(adapter);
}
const runs = await Promise.all(pendingRuns);
for (const adapter of adapters) {
expect(adapter.killMock).not.toHaveBeenCalled();
adapter.settle(0);
}
const exits = await Promise.all(runs.map((run) => run.wait()));
expect(exits.every((exit) => exit.reason === "exit")).toBe(true);
});
it("applies overall timeout even for near-immediate timer firing", async () => {
vi.useFakeTimers();
const adapter = createStubChildAdapter({
onKill: (signal, current) => {
current.settle(null, signal ?? "SIGKILL");
},
});
createChildAdapterMock.mockResolvedValue(adapter);
const supervisor = createProcessSupervisor();
const run = await spawnChild(supervisor, {
argv: createSilentIdleArgv(),
timeoutMs: 1,
stdinMode: "pipe-closed",
});
const exitPromise = run.wait();
await vi.advanceTimersByTimeAsync(1);
const exit = await exitPromise;
expect(adapter.killMock).toHaveBeenCalledWith(
process.platform === "win32" ? "SIGKILL" : "SIGTERM",
);
expect(exit.reason).toBe("overall-timeout");
expect(exit.timedOut).toBe(true);
});
it("classifies a natural close after a missed overall deadline as timed out", async () => {
vi.useFakeTimers();
const nowSpy = vi.spyOn(performance, "now").mockReturnValue(1_000);
const adapter = createStubChildAdapter();
createChildAdapterMock.mockResolvedValue(adapter);
const supervisor = createProcessSupervisor();
const run = await spawnChild(supervisor, {
argv: createSilentIdleArgv(),
timeoutMs: 10,
stdinMode: "pipe-closed",
});
const exitPromise = run.wait();
nowSpy.mockReturnValue(1_011);
adapter.settle(0);
const exit = await exitPromise;
expect(adapter.killMock).not.toHaveBeenCalled();
expect(exit.reason).toBe("overall-timeout");
expect(exit.timedOut).toBe(true);
});
it("uses the refreshed no-output deadline when a missed timer races natural close", async () => {
vi.useFakeTimers();
const nowSpy = vi.spyOn(performance, "now").mockReturnValue(1_000);
const adapter = createStubChildAdapter();
createChildAdapterMock.mockResolvedValue(adapter);
const supervisor = createProcessSupervisor();
const run = await spawnChild(supervisor, {
argv: createSilentIdleArgv(),
timeoutMs: 100,
noOutputTimeoutMs: 10,
stdinMode: "pipe-closed",
});
const exitPromise = run.wait();
nowSpy.mockReturnValue(1_005);
adapter.emitStdout("progress");
nowSpy.mockReturnValue(1_016);
adapter.settle(0);
const exit = await exitPromise;
expect(adapter.killMock).not.toHaveBeenCalled();
expect(exit.reason).toBe("no-output-timeout");
expect(exit.noOutputTimedOut).toBe(true);
expect(exit.timedOut).toBe(true);
});
it("can stream output without retaining it in RunExit payload", async () => {
const adapter = createStubChildAdapter();
createChildAdapterMock.mockResolvedValue(adapter);
const supervisor = createProcessSupervisor();
let streamed = "";
const run = await spawnChild(supervisor, {
argv: createWriteStdoutArgv("streamed"),
timeoutMs: 1_000,
stdinMode: "pipe-closed",
captureOutput: false,
onStdout: (chunk) => {
streamed += chunk;
},
});
adapter.emitStdout("streamed");
adapter.settle(0);
const exit = await run.wait();
expect(streamed).toBe("streamed");
expect(exit.stdout).toBe("");
});
it("bounds retained output on UTF-16 boundaries while streaming full chunks", async () => {
const adapter = createStubChildAdapter();
createChildAdapterMock.mockResolvedValue(adapter);
const supervisor = createProcessSupervisor();
let streamedStdout = "";
let streamedStderr = "";
const maxCapturedOutputChars = 256;
const stdoutMarker = `[openclaw: captured stdout truncated to last ${maxCapturedOutputChars} chars]\n`;
const stderrMarker = `[openclaw: captured stderr truncated to last ${maxCapturedOutputChars} chars]\n`;
const retainedChars = maxCapturedOutputChars - stdoutMarker.length - 1;
const stdoutChunk = `${"a".repeat(stdoutMarker.length)}😀${"s".repeat(retainedChars)}`;
const stderrChunk = `${"b".repeat(stderrMarker.length)}😀${"e".repeat(retainedChars)}`;
const run = await spawnChild(supervisor, {
argv: createWriteStdoutArgv(stdoutChunk),
timeoutMs: 1_000,
stdinMode: "pipe-closed",
maxCapturedOutputChars,
onStdout: (chunk) => {
streamedStdout += chunk;
},
onStderr: (chunk) => {
streamedStderr += chunk;
},
});
adapter.emitStdout(stdoutChunk);
adapter.emitStderr(stderrChunk);
adapter.settle(0);
const exit = await run.wait();
expect(streamedStdout).toBe(stdoutChunk);
expect(streamedStderr).toBe(stderrChunk);
expect(exit.stdout).toBe(`${stdoutMarker}${"s".repeat(retainedChars)}`);
expect(exit.stderr).toBe(`${stderrMarker}${"e".repeat(retainedChars)}`);
});
});
|