@agentrq/acp-gateway 0.2.15 → 0.2.16

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