@agentrq/acp-gateway 0.2.18 → 0.2.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/__tests__/index.test.js +163 -1
- package/dist/__tests__/index.test.js.map +1 -1
- package/dist/index.js +137 -9
- package/dist/index.js.map +1 -1
- package/dist/mcpClient.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@ import { tmpdir } from "node:os";
|
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { EventEmitter } from "node:events";
|
|
6
6
|
import * as acp from "@agentclientprotocol/sdk";
|
|
7
|
-
import { createAcpSessionSwitcher, checkForNextTask, mapMcpServers, TaskQueue, getOrCreateSession, openIdleSession, resetIdleSession, activeSessions, AuthenticationFailed, IDLE_SESSION_KEY, loginCommandFor, authConfig, modelConfig, createSessionWithAuth, isInteractiveTerminal, openAgentConnection, parseGatewayArgs, assertAgentRunnable, helpText, isRunnable, resolveOnPath, spawnTarget, needsShell, escapeCmdCommand, quoteForCmd, spawnArgsFor, terminateAgentProcess, printHelp, resolveAgentCommand, runListAgents, pickHumanApprovalMode, enforceHumanApprovalMode, handleAgentModeChange, runAgentCommand, findActiveSession, handleTaskCancellation, handleSetModel, handleSetConcurrency, runTurnForTask, resetTaskTurns, registerConcurrencyListeners, reportConcurrency, applyModelSelection, cancelledTaskSeq, markTaskCancelled, isTaskCancelled, nextTaskSeq, supportsCloseSession, closeSession, closeAllSessions, setupSignalHandlers, } from "../index.js";
|
|
7
|
+
import { createAcpSessionSwitcher, checkForNextTask, mapMcpServers, TaskQueue, createTaskQueue, resetConcurrencyReports, getOrCreateSession, openIdleSession, resetIdleSession, activeSessions, AuthenticationFailed, IDLE_SESSION_KEY, loginCommandFor, authConfig, modelConfig, createSessionWithAuth, isInteractiveTerminal, openAgentConnection, parseGatewayArgs, assertAgentRunnable, helpText, isRunnable, resolveOnPath, spawnTarget, needsShell, escapeCmdCommand, quoteForCmd, spawnArgsFor, terminateAgentProcess, printHelp, resolveAgentCommand, runListAgents, pickHumanApprovalMode, enforceHumanApprovalMode, handleAgentModeChange, runAgentCommand, findActiveSession, handleTaskCancellation, handleSetModel, handleSetConcurrency, runTurnForTask, resetTaskTurns, registerConcurrencyListeners, reportConcurrency, applyModelSelection, cancelledTaskSeq, markTaskCancelled, isTaskCancelled, nextTaskSeq, supportsCloseSession, closeSession, closeAllSessions, setupSignalHandlers, } from "../index.js";
|
|
8
8
|
import { sendModelsNotification } from "../acpClient.js";
|
|
9
9
|
import { AUTH_REQUIRED_CODE } from "../auth.js";
|
|
10
10
|
/**
|
|
@@ -549,6 +549,99 @@ describe("index", () => {
|
|
|
549
549
|
expect(errorSpy).not.toHaveBeenCalled();
|
|
550
550
|
errorSpy.mockRestore();
|
|
551
551
|
});
|
|
552
|
+
describe("saying when its numbers move", () => {
|
|
553
|
+
/** Records what the queue looked like at each announcement. */
|
|
554
|
+
const watch = (queue) => {
|
|
555
|
+
const seen = [];
|
|
556
|
+
return {
|
|
557
|
+
seen,
|
|
558
|
+
onChange: () => seen.push({
|
|
559
|
+
active: queue().getActiveCount(),
|
|
560
|
+
queued: queue().getQueueLength(),
|
|
561
|
+
}),
|
|
562
|
+
};
|
|
563
|
+
};
|
|
564
|
+
it("says so when a task starts, and again when it finishes", async () => {
|
|
565
|
+
// Without this the workspace hears the count once, while the gateway is
|
|
566
|
+
// idle, and then shows 0 running for the rest of its life.
|
|
567
|
+
let queue;
|
|
568
|
+
const { seen, onChange } = watch(() => queue);
|
|
569
|
+
queue = new TaskQueue(2, onChange);
|
|
570
|
+
let release;
|
|
571
|
+
const held = new Promise((resolve) => { release = resolve; });
|
|
572
|
+
const running = queue.run(() => held);
|
|
573
|
+
expect(seen).toEqual([{ active: 1, queued: 0 }]);
|
|
574
|
+
release();
|
|
575
|
+
await running;
|
|
576
|
+
expect(seen).toEqual([
|
|
577
|
+
{ active: 1, queued: 0 },
|
|
578
|
+
{ active: 0, queued: 0 },
|
|
579
|
+
]);
|
|
580
|
+
});
|
|
581
|
+
it("says so when a task is made to wait, with nothing newly running", async () => {
|
|
582
|
+
let queue;
|
|
583
|
+
const { seen, onChange } = watch(() => queue);
|
|
584
|
+
queue = new TaskQueue(1, onChange);
|
|
585
|
+
let release;
|
|
586
|
+
const held = new Promise((resolve) => { release = resolve; });
|
|
587
|
+
const first = queue.run(() => held);
|
|
588
|
+
const second = queue.run(async () => { });
|
|
589
|
+
// The second changed what is waiting, not what is running.
|
|
590
|
+
expect(seen).toEqual([
|
|
591
|
+
{ active: 1, queued: 0 },
|
|
592
|
+
{ active: 1, queued: 1 },
|
|
593
|
+
]);
|
|
594
|
+
release();
|
|
595
|
+
await Promise.all([first, second]);
|
|
596
|
+
// A slot came free, then the waiting task took it, then it finished.
|
|
597
|
+
expect(seen.slice(2)).toEqual([
|
|
598
|
+
{ active: 0, queued: 1 },
|
|
599
|
+
{ active: 1, queued: 0 },
|
|
600
|
+
{ active: 0, queued: 0 },
|
|
601
|
+
]);
|
|
602
|
+
});
|
|
603
|
+
it("runs tasks normally when nobody is listening", async () => {
|
|
604
|
+
// The queue is built without a listener throughout these tests and in
|
|
605
|
+
// --list-models; an optional callback must stay optional.
|
|
606
|
+
const queue = new TaskQueue(1);
|
|
607
|
+
let ran = false;
|
|
608
|
+
await expect(queue.run(async () => { ran = true; })).resolves.toBeUndefined();
|
|
609
|
+
expect(ran).toBe(true);
|
|
610
|
+
expect(queue.getActiveCount()).toBe(0);
|
|
611
|
+
});
|
|
612
|
+
it("tells the workspace, end to end, when a task starts and finishes", async () => {
|
|
613
|
+
const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
614
|
+
// The callback is only worth anything if it reaches the wire, so this
|
|
615
|
+
// asserts on the notification rather than on the callback firing.
|
|
616
|
+
const bridge = { sendNotification: vi.fn().mockResolvedValue(undefined) };
|
|
617
|
+
const queue = createTaskQueue(2, bridge);
|
|
618
|
+
let release;
|
|
619
|
+
const held = new Promise((resolve) => { release = resolve; });
|
|
620
|
+
const running = queue.run(() => held);
|
|
621
|
+
await flush();
|
|
622
|
+
expect(bridge.sendNotification).toHaveBeenCalledWith("notifications/claude/channel/concurrency", expect.objectContaining({ maxConcurrency: 2, active: 1, queued: 0 }));
|
|
623
|
+
release();
|
|
624
|
+
await running;
|
|
625
|
+
await flush();
|
|
626
|
+
expect(bridge.sendNotification).toHaveBeenLastCalledWith("notifications/claude/channel/concurrency", expect.objectContaining({ maxConcurrency: 2, active: 0, queued: 0 }));
|
|
627
|
+
});
|
|
628
|
+
it("keeps running tasks when the listener throws", async () => {
|
|
629
|
+
// Announcing happens inside execute, including in its finally — a
|
|
630
|
+
// listener that threw there would replace the task's own error with its
|
|
631
|
+
// own. Running tasks is the job; telling an interface is a courtesy.
|
|
632
|
+
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => { });
|
|
633
|
+
const queue = new TaskQueue(1, () => {
|
|
634
|
+
throw new Error("the workspace is on fire");
|
|
635
|
+
});
|
|
636
|
+
let ran = false;
|
|
637
|
+
await expect(queue.run(async () => { ran = true; })).resolves.toBeUndefined();
|
|
638
|
+
expect(ran).toBe(true);
|
|
639
|
+
// And a task's own failure still reaches its caller unchanged.
|
|
640
|
+
await expect(queue.run(async () => { throw new Error("the task itself failed"); })).rejects.toThrow("the task itself failed");
|
|
641
|
+
expect(errorSpy).toHaveBeenCalledWith("[queue] Error reporting the queue's state:", expect.any(Error));
|
|
642
|
+
errorSpy.mockRestore();
|
|
643
|
+
});
|
|
644
|
+
});
|
|
552
645
|
});
|
|
553
646
|
describe("handleSetConcurrency", () => {
|
|
554
647
|
let errorSpy;
|
|
@@ -757,6 +850,75 @@ describe("index", () => {
|
|
|
757
850
|
});
|
|
758
851
|
});
|
|
759
852
|
describe("reportConcurrency", () => {
|
|
853
|
+
beforeEach(() => resetConcurrencyReports());
|
|
854
|
+
afterEach(() => resetConcurrencyReports());
|
|
855
|
+
/** A bridge whose sends finish only when the test says so. */
|
|
856
|
+
const heldBridge = () => {
|
|
857
|
+
const releases = [];
|
|
858
|
+
const sendNotification = vi.fn(() => new Promise((resolve) => { releases.push(resolve); }));
|
|
859
|
+
return { bridge: { sendNotification }, sendNotification, releases };
|
|
860
|
+
};
|
|
861
|
+
it("never puts two reports on the wire at once", async () => {
|
|
862
|
+
// Each report is its own POST and nothing sequences them, so two in
|
|
863
|
+
// flight can be processed in either order — and the workspace would then
|
|
864
|
+
// show whichever landed last rather than whichever is true.
|
|
865
|
+
const { bridge, sendNotification, releases } = heldBridge();
|
|
866
|
+
const queue = new TaskQueue(4);
|
|
867
|
+
void reportConcurrency(bridge, queue);
|
|
868
|
+
void reportConcurrency(bridge, queue);
|
|
869
|
+
void reportConcurrency(bridge, queue);
|
|
870
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
871
|
+
// The second and third waited for the first rather than racing it.
|
|
872
|
+
expect(sendNotification).toHaveBeenCalledTimes(1);
|
|
873
|
+
releases[0]();
|
|
874
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
875
|
+
expect(sendNotification).toHaveBeenCalledTimes(2);
|
|
876
|
+
releases[1]();
|
|
877
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
878
|
+
// Three asks, two reports: the ones that piled up became one.
|
|
879
|
+
expect(sendNotification).toHaveBeenCalledTimes(2);
|
|
880
|
+
});
|
|
881
|
+
it("makes the report that follows a burst describe the queue now", async () => {
|
|
882
|
+
// A collapsed report is worth sending only because it is current; one
|
|
883
|
+
// carrying the state at the moment it was asked for would be a stale
|
|
884
|
+
// number arriving late, which is the thing being fixed.
|
|
885
|
+
const { bridge, sendNotification, releases } = heldBridge();
|
|
886
|
+
// Wired to the bridge, so a task starting is what asks for the report.
|
|
887
|
+
const queue = createTaskQueue(4, bridge);
|
|
888
|
+
void reportConcurrency(bridge, queue);
|
|
889
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
890
|
+
expect(sendNotification).toHaveBeenLastCalledWith("notifications/claude/channel/concurrency", expect.objectContaining({ active: 0, queued: 0 }));
|
|
891
|
+
// The numbers move while the first report is still flying.
|
|
892
|
+
let release;
|
|
893
|
+
const held = new Promise((resolve) => { release = resolve; });
|
|
894
|
+
const running = queue.run(() => held);
|
|
895
|
+
releases[0]();
|
|
896
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
897
|
+
expect(sendNotification).toHaveBeenLastCalledWith("notifications/claude/channel/concurrency", expect.objectContaining({ active: 1, queued: 0 }));
|
|
898
|
+
release();
|
|
899
|
+
releases.slice(1).forEach((r) => r());
|
|
900
|
+
await running;
|
|
901
|
+
});
|
|
902
|
+
it("resolves only once the workspace has been told", async () => {
|
|
903
|
+
// handleSetConcurrency awaits this to make "always answered" true, so it
|
|
904
|
+
// must not resolve on the strength of somebody else's report.
|
|
905
|
+
const { bridge, sendNotification, releases } = heldBridge();
|
|
906
|
+
const queue = new TaskQueue(4);
|
|
907
|
+
void reportConcurrency(bridge, queue);
|
|
908
|
+
let answered = false;
|
|
909
|
+
void reportConcurrency(bridge, queue).then(() => { answered = true; });
|
|
910
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
911
|
+
// The first report has not even landed yet.
|
|
912
|
+
expect(answered).toBe(false);
|
|
913
|
+
releases[0]();
|
|
914
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
915
|
+
// Nor on the first one landing — the second is what it is owed.
|
|
916
|
+
expect(answered).toBe(false);
|
|
917
|
+
releases[1]();
|
|
918
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
919
|
+
expect(answered).toBe(true);
|
|
920
|
+
expect(sendNotification).toHaveBeenCalledTimes(2);
|
|
921
|
+
});
|
|
760
922
|
it("describes the queue as it stands", async () => {
|
|
761
923
|
const bridge = { sendNotification: vi.fn().mockResolvedValue(undefined) };
|
|
762
924
|
const queue = new TaskQueue(2);
|