@agentrq/acp-gateway 0.2.15 → 0.2.17

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.
@@ -4,7 +4,8 @@ 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, cancelledTaskSeq, markTaskCancelled, isTaskCancelled, nextTaskSeq, supportsCloseSession, closeSession, closeAllSessions, setupSignalHandlers, } from "../index.js";
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, applyModelSelection, cancelledTaskSeq, markTaskCancelled, isTaskCancelled, nextTaskSeq, supportsCloseSession, closeSession, closeAllSessions, setupSignalHandlers, } from "../index.js";
8
+ import { sendModelsNotification } from "../acpClient.js";
8
9
  import { AUTH_REQUIRED_CODE } from "../auth.js";
9
10
  /**
10
11
  * A stand-in for the workspace bridge.
@@ -449,6 +450,22 @@ describe("index", () => {
449
450
  await idle.acpClient.sendCommandsToWorkspace(idle.sessionId, [{ name: "init", description: "d" }]);
450
451
  expect(mockBridge.sendNotification).toHaveBeenCalledWith("notifications/claude/channel/commands", expect.objectContaining({ task_id: "T-Owner" }));
451
452
  });
453
+ it("re-advertises known models with the adopted task id when taken over", async () => {
454
+ const mockBridge = fakeBridge();
455
+ const idle = await getOrCreateSession(undefined, ["node", "agent.js"], [], { env: {} }, mockBridge);
456
+ await idle.acpClient.sendModelsToWorkspace(idle.sessionId, {
457
+ configId: "model",
458
+ currentModelId: "m1",
459
+ models: [{ id: "m1", name: "Model 1", current: true }],
460
+ });
461
+ mockBridge.sendNotification.mockClear();
462
+ await getOrCreateSession("T-Adopt", ["node", "agent.js"], [], { env: {} }, mockBridge);
463
+ expect(mockBridge.sendNotification).toHaveBeenCalledWith("notifications/claude/channel/models", expect.objectContaining({
464
+ task_id: "T-Adopt",
465
+ session_id: idle.sessionId,
466
+ current_model: "m1",
467
+ }));
468
+ });
452
469
  it("gives a second task its own session, having only one to hand over", async () => {
453
470
  const mockBridge = fakeBridge();
454
471
  await getOrCreateSession(undefined, ["node", "agent.js"], [], { env: {} }, mockBridge);
@@ -1647,6 +1664,209 @@ describe("index", () => {
1647
1664
  });
1648
1665
  });
1649
1666
  });
1667
+ describe("choosing a model at runtime", () => {
1668
+ // Everything here rests on one contract: the interface marks a chosen model
1669
+ // pending and only a models notification releases it. So every path has to
1670
+ // end in a report — including the ones that fail — or the picker is left
1671
+ // claiming a model the agent never adopted, with nothing to correct it.
1672
+ beforeEach(() => {
1673
+ activeSessions.clear();
1674
+ });
1675
+ afterEach(() => {
1676
+ activeSessions.clear();
1677
+ });
1678
+ const known = {
1679
+ configId: "model",
1680
+ currentModelId: "a",
1681
+ models: [
1682
+ { id: "a", name: "Model A", current: true },
1683
+ { id: "b", name: "Model B", current: false },
1684
+ ],
1685
+ };
1686
+ /** A session whose ACP set succeeds, reporting the new list back. */
1687
+ const sessionThatSwitches = () => {
1688
+ const sendModelsToWorkspace = vi.fn();
1689
+ const setSessionConfigOption = vi.fn().mockResolvedValue({
1690
+ configOptions: [
1691
+ {
1692
+ id: "model",
1693
+ type: "select",
1694
+ category: "model",
1695
+ currentValue: "b",
1696
+ options: [
1697
+ { value: "a", name: "Model A" },
1698
+ { value: "b", name: "Model B" },
1699
+ ],
1700
+ },
1701
+ ],
1702
+ });
1703
+ const session = {
1704
+ sessionId: "sess-1",
1705
+ connection: { setSessionConfigOption },
1706
+ acpClient: { sendModelsToWorkspace, lastModelsFor: () => known },
1707
+ };
1708
+ return { session, sendModelsToWorkspace, setSessionConfigOption };
1709
+ };
1710
+ it("switches the named session and reports what the agent ended up on", async () => {
1711
+ const { session, sendModelsToWorkspace, setSessionConfigOption } = sessionThatSwitches();
1712
+ activeSessions.set("task-1", session);
1713
+ await handleSetModel({ sessionId: "sess-1", configId: "model", modelId: "b" });
1714
+ expect(setSessionConfigOption).toHaveBeenCalledWith({
1715
+ sessionId: "sess-1",
1716
+ configId: "model",
1717
+ value: "b",
1718
+ });
1719
+ // The agent's own answer, not the id that was asked for.
1720
+ expect(sendModelsToWorkspace).toHaveBeenCalledWith("sess-1", expect.objectContaining({ currentModelId: "b" }));
1721
+ });
1722
+ it("puts back what was true when the agent refuses the switch", async () => {
1723
+ const sendModelsToWorkspace = vi.fn();
1724
+ const session = {
1725
+ sessionId: "sess-1",
1726
+ connection: {
1727
+ setSessionConfigOption: vi.fn().mockRejectedValue(new Error("nope")),
1728
+ },
1729
+ acpClient: { sendModelsToWorkspace, lastModelsFor: () => known },
1730
+ };
1731
+ activeSessions.set("task-1", session);
1732
+ await handleSetModel({ sessionId: "sess-1", configId: "model", modelId: "b" });
1733
+ // Still on "a": the picker reverts instead of hanging on "b" forever.
1734
+ expect(sendModelsToWorkspace).toHaveBeenCalledWith("sess-1", expect.objectContaining({ currentModelId: "a" }));
1735
+ });
1736
+ it("does not throw when no session matches", async () => {
1737
+ // The session went away between the human clicking and this arriving.
1738
+ await expect(handleSetModel({ sessionId: "sess-gone", configId: "model", modelId: "b" })).resolves.toBeUndefined();
1739
+ });
1740
+ it("answers for a session that has already ended", async () => {
1741
+ // The agent exited, taking its client with it, so there is nothing left to
1742
+ // set. The picker is still holding a pending model, so what was last true
1743
+ // for that session goes out over the bridge to release it.
1744
+ const sendNotification = vi.fn().mockResolvedValue(undefined);
1745
+ await sendModelsNotification({ sendNotification }, "sess-ended", known);
1746
+ sendNotification.mockClear();
1747
+ await handleSetModel({ sessionId: "sess-ended", configId: "model", modelId: "b" }, { sendNotification });
1748
+ expect(sendNotification).toHaveBeenCalledWith("notifications/claude/channel/models", expect.objectContaining({ session_id: "sess-ended", current_model: "a" }));
1749
+ });
1750
+ it("stays quiet about a set that names no session and matches none", async () => {
1751
+ // With no id and nothing running there is nothing to identify, let alone
1752
+ // report on.
1753
+ const sendNotification = vi.fn().mockResolvedValue(undefined);
1754
+ await handleSetModel({ configId: "model", modelId: "b" }, { sendNotification });
1755
+ expect(sendNotification).not.toHaveBeenCalled();
1756
+ });
1757
+ it("stays quiet about a session it has never heard of", async () => {
1758
+ // Nothing was ever reported for it, so there is no truth to put back and
1759
+ // inventing one would be worse than the silence.
1760
+ const sendNotification = vi.fn().mockResolvedValue(undefined);
1761
+ await handleSetModel({ sessionId: "sess-never-seen", configId: "model", modelId: "b" }, { sendNotification });
1762
+ expect(sendNotification).not.toHaveBeenCalled();
1763
+ });
1764
+ it("answers a notification naming no model instead of ignoring it", async () => {
1765
+ const { session, sendModelsToWorkspace, setSessionConfigOption } = sessionThatSwitches();
1766
+ activeSessions.set("task-1", session);
1767
+ await handleSetModel({ sessionId: "sess-1", configId: "model" });
1768
+ expect(setSessionConfigOption).not.toHaveBeenCalled();
1769
+ // A malformed notification is still a pending picker on the other end.
1770
+ expect(sendModelsToWorkspace).toHaveBeenCalledWith("sess-1", known);
1771
+ });
1772
+ it("refuses a model the session never advertised, and says what it has", async () => {
1773
+ const { session, sendModelsToWorkspace, setSessionConfigOption } = sessionThatSwitches();
1774
+ activeSessions.set("task-1", session);
1775
+ await handleSetModel({ sessionId: "sess-1", configId: "model", modelId: "not-a-model" });
1776
+ expect(setSessionConfigOption).not.toHaveBeenCalled();
1777
+ expect(sendModelsToWorkspace).toHaveBeenCalledWith("sess-1", known);
1778
+ });
1779
+ it("reports without asking the agent when the model is already current", async () => {
1780
+ const { session, sendModelsToWorkspace, setSessionConfigOption } = sessionThatSwitches();
1781
+ activeSessions.set("task-1", session);
1782
+ await handleSetModel({ sessionId: "sess-1", configId: "model", modelId: "a" });
1783
+ expect(setSessionConfigOption).not.toHaveBeenCalled();
1784
+ expect(sendModelsToWorkspace).toHaveBeenCalledWith("sess-1", known);
1785
+ });
1786
+ it("resolves a model by display name, for a --model typed by a human", async () => {
1787
+ const { session, setSessionConfigOption } = sessionThatSwitches();
1788
+ await applyModelSelection({
1789
+ connection: session.connection,
1790
+ acpClient: session.acpClient,
1791
+ sessionId: "sess-1",
1792
+ known,
1793
+ requested: "Model B",
1794
+ });
1795
+ expect(setSessionConfigOption).toHaveBeenCalledWith({
1796
+ sessionId: "sess-1",
1797
+ configId: "model",
1798
+ value: "b",
1799
+ });
1800
+ });
1801
+ it("marks the chosen model current when the agent answers without its config", async () => {
1802
+ // Some agents acknowledge the write without repeating their options. The
1803
+ // interface still has to end up with something selected.
1804
+ const sendModelsToWorkspace = vi.fn();
1805
+ const acpClient = { sendModelsToWorkspace, lastModelsFor: () => known };
1806
+ await applyModelSelection({
1807
+ connection: { setSessionConfigOption: vi.fn().mockResolvedValue({}) },
1808
+ acpClient,
1809
+ sessionId: "sess-1",
1810
+ known,
1811
+ requested: "b",
1812
+ });
1813
+ expect(sendModelsToWorkspace).toHaveBeenCalledWith("sess-1", expect.objectContaining({
1814
+ currentModelId: "b",
1815
+ models: [
1816
+ expect.objectContaining({ id: "a", current: false }),
1817
+ expect.objectContaining({ id: "b", current: true }),
1818
+ ],
1819
+ }));
1820
+ });
1821
+ it("puts back the newest report, not the snapshot taken before the set", async () => {
1822
+ // The agent moved to "c" on its own while the set was in flight, and then
1823
+ // refused the set. Reverting to the pre-set snapshot would claim the
1824
+ // session is on "a" — a model it has already left — and, since every
1825
+ // report is recorded, would write that stale list back over the newer one.
1826
+ const fresher = {
1827
+ configId: "model",
1828
+ currentModelId: "c",
1829
+ models: [{ id: "c", name: "Model C", current: true }],
1830
+ };
1831
+ const sendModelsToWorkspace = vi.fn();
1832
+ await applyModelSelection({
1833
+ connection: {
1834
+ setSessionConfigOption: vi.fn().mockRejectedValue(new Error("nope")),
1835
+ },
1836
+ acpClient: { sendModelsToWorkspace, lastModelsFor: () => fresher },
1837
+ sessionId: "sess-1",
1838
+ known,
1839
+ requested: "b",
1840
+ });
1841
+ expect(sendModelsToWorkspace).toHaveBeenCalledWith("sess-1", fresher);
1842
+ });
1843
+ it("falls back to the caller's snapshot when nothing has been reported yet", async () => {
1844
+ // The startup path: --model runs against a session that has not sent a
1845
+ // models notification, so the list from session/new is all there is.
1846
+ const sendModelsToWorkspace = vi.fn();
1847
+ await applyModelSelection({
1848
+ connection: {
1849
+ setSessionConfigOption: vi.fn().mockRejectedValue(new Error("nope")),
1850
+ },
1851
+ acpClient: { sendModelsToWorkspace, lastModelsFor: () => undefined },
1852
+ sessionId: "sess-1",
1853
+ known,
1854
+ requested: "b",
1855
+ });
1856
+ expect(sendModelsToWorkspace).toHaveBeenCalledWith("sess-1", known);
1857
+ });
1858
+ it("says so rather than guessing when there is no config option to write to", async () => {
1859
+ const sendModelsToWorkspace = vi.fn();
1860
+ const setSessionConfigOption = vi.fn();
1861
+ await applyModelSelection({
1862
+ connection: { setSessionConfigOption },
1863
+ acpClient: { sendModelsToWorkspace, lastModelsFor: () => undefined },
1864
+ sessionId: "sess-1",
1865
+ requested: "b",
1866
+ });
1867
+ expect(setSessionConfigOption).not.toHaveBeenCalled();
1868
+ });
1869
+ });
1650
1870
  describe("handleAgentModeChange", () => {
1651
1871
  const modes = {
1652
1872
  currentModeId: "ask",