@agentrq/acp-gateway 0.2.26 → 0.2.27

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 CHANGED
@@ -37,7 +37,7 @@ npm install -g @agentrq/acp-gateway
37
37
 
38
38
  ## Current Version
39
39
 
40
- `0.2.26`
40
+ `0.2.27`
41
41
 
42
42
  ## Usage
43
43
 
@@ -173,7 +173,7 @@ Credentials are never stored by the gateway; the agent keeps its own, exactly as
173
173
 
174
174
  `acp-gateway` searches for `.mcp.json` starting in the current working directory and up to 3 parent directories.
175
175
 
176
- `--list-models`, `--agent-info` and `--list-auth-methods` only ask the agent about itself, so they do not need a workspace: they skip the search and run without a `.mcp.json`. They still read a config named with `--mcp-json`.
176
+ `--list-models`, `--agent-info` and `--list-auth-methods` only ask the agent about itself, so they do not need a workspace: they skip the search and run without a `.mcp.json`. They still read a config named with `--mcp-json`, but `--list-models` never connects to the workspace: it runs the agent in an empty temporary directory, so the agent does not pick up the workspace's `.mcp.json` and get taken for the workspace's agent.
177
177
 
178
178
  `--mcp-json <path>` names a config directly, wherever it lives and whatever it is called. It is read *as well as* whatever the search finds, with the named file winning any name collision — so a gateway started from somewhere other than its workspace still connects to the right one.
179
179
 
@@ -1,11 +1,11 @@
1
1
  import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2
2
  import { setVerbose } from "../log.js";
3
- import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
3
+ import { existsSync, mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
4
4
  import { tmpdir } from "node:os";
5
5
  import { join } from "node:path";
6
6
  import { EventEmitter } from "node:events";
7
7
  import * as acp from "@agentclientprotocol/sdk";
8
- import { createAcpSessionSwitcher, fetchNextTask, startPendingTask, drainPendingTasks, createSelfFillingTaskQueue, MAX_SKIPPED_FETCHES, IDLE_TASK_POLL_INTERVAL_MS, freeSlots, resetPendingTaskDrain, mapMcpServers, TaskQueue, createTaskQueue, resetConcurrencyReports, getOrCreateSession, openIdleSession, resetIdleSession, activeSessions, AuthenticationFailed, IDLE_SESSION_KEY, loginCommandFor, authConfig, modelConfig, createSessionWithAuth, isInteractiveTerminal, openAgentConnection, agentExitFailed, showAgentStderr, shouldPrintBanner, parseGatewayArgs, assertAgentRunnable, helpText, isRunnable, resolveOnPath, spawnTarget, needsShell, escapeCmdCommand, quoteForCmd, spawnArgsFor, terminateAgentProcess, printHelp, resolveAgentCommand, runListAgents, pickHumanApprovalMode, enforceHumanApprovalMode, handleAgentModeChange, runAgentCommand, loadWorkspace, createDetachedBridge, WORKSPACE_OPTIONAL_COMMANDS, findActiveSession, handleTaskCancellation, handleSetModel, handleSetConcurrency, runTurnForTask, resetTaskTurns, registerConcurrencyListeners, reportConcurrency, applyModelSelection, cancelledTaskSeq, markTaskCancelled, isTaskCancelled, nextTaskSeq, supportsCloseSession, closeSession, closeAllSessions, setupSignalHandlers, } from "../index.js";
8
+ import { createAcpSessionSwitcher, fetchNextTask, startPendingTask, drainPendingTasks, createSelfFillingTaskQueue, MAX_SKIPPED_FETCHES, IDLE_TASK_POLL_INTERVAL_MS, freeSlots, resetPendingTaskDrain, mapMcpServers, TaskQueue, createTaskQueue, resetConcurrencyReports, getOrCreateSession, openIdleSession, resetIdleSession, activeSessions, AuthenticationFailed, IDLE_SESSION_KEY, loginCommandFor, authConfig, modelConfig, createSessionWithAuth, isInteractiveTerminal, openAgentConnection, agentExitFailed, showAgentStderr, shouldPrintBanner, parseGatewayArgs, assertAgentRunnable, helpText, isRunnable, resolveOnPath, spawnTarget, needsShell, escapeCmdCommand, quoteForCmd, spawnArgsFor, terminateAgentProcess, printHelp, resolveAgentCommand, runListAgents, pickHumanApprovalMode, enforceHumanApprovalMode, handleAgentModeChange, runAgentCommand, loadWorkspace, createDetachedBridge, makeIsolatedDir, WORKSPACE_OPTIONAL_COMMANDS, findActiveSession, handleTaskCancellation, handleSetModel, handleSetConcurrency, runTurnForTask, resetTaskTurns, registerConcurrencyListeners, reportConcurrency, applyModelSelection, cancelledTaskSeq, markTaskCancelled, isTaskCancelled, nextTaskSeq, supportsCloseSession, closeSession, closeAllSessions, setupSignalHandlers, } from "../index.js";
9
9
  import { sendModelsNotification } from "../acpClient.js";
10
10
  import { AUTH_REQUIRED_CODE } from "../auth.js";
11
11
  /**
@@ -2678,12 +2678,19 @@ describe("index", () => {
2678
2678
  writeFileSync(join(dir, ".mcp.json"), "{ not json");
2679
2679
  expect(loadWorkspace("list-models", undefined, dir).configs).toEqual([]);
2680
2680
  });
2681
- it("should still use a config named with --mcp-json", () => {
2681
+ it.each(["agent-info", "list-auth-methods"])("should still use a config named with --mcp-json for --%s", (command) => {
2682
2682
  const file = writeMcpJson(emptyWorkspaceDir(), "servers.json");
2683
- const workspace = loadWorkspace("list-models", file, emptyWorkspaceDir());
2683
+ const workspace = loadWorkspace(command, file, emptyWorkspaceDir());
2684
2684
  expect(workspace.agentrqConfig.name).toBe("agentrq-ws");
2685
2685
  expect(workspace.mcpBridge.getServerName()).toBe("agentrq-ws");
2686
2686
  });
2687
+ it("should never connect --list-models to the workspace, even one named with --mcp-json", async () => {
2688
+ const file = writeMcpJson(emptyWorkspaceDir(), "servers.json");
2689
+ const workspace = loadWorkspace("list-models", file, emptyWorkspaceDir());
2690
+ expect(workspace.agentrqConfig.name).toBe("agentrq-ws");
2691
+ expect(workspace.mcpBridge.getServerName()).toBeUndefined();
2692
+ await expect(workspace.mcpBridge.sendNotification("notifications/claude/channel/models", {})).rejects.toThrow("No agentrq workspace is configured");
2693
+ });
2687
2694
  it("should still report a --mcp-json path that is wrong", () => {
2688
2695
  expect(() => loadWorkspace("list-models", "/nowhere/servers.json", emptyWorkspaceDir())).toThrow();
2689
2696
  });
@@ -2698,6 +2705,17 @@ describe("index", () => {
2698
2705
  expect(workspace.mcpBridge.getServerName()).toBe("agentrq-ws");
2699
2706
  });
2700
2707
  });
2708
+ describe("makeIsolatedDir", () => {
2709
+ it("should make an empty directory and remove it again", () => {
2710
+ const isolated = makeIsolatedDir();
2711
+ expect(existsSync(isolated.dir)).toBe(true);
2712
+ expect(isolated.dir.startsWith(tmpdir())).toBe(true);
2713
+ isolated.remove();
2714
+ expect(existsSync(isolated.dir)).toBe(false);
2715
+ // Removing twice, or after something else did, is harmless.
2716
+ expect(() => isolated.remove()).not.toThrow();
2717
+ });
2718
+ });
2701
2719
  describe("createDetachedBridge", () => {
2702
2720
  it("should fail whatever is asked of the workspace, and close quietly", async () => {
2703
2721
  const bridge = createDetachedBridge();
@@ -2739,6 +2757,35 @@ describe("index", () => {
2739
2757
  expect(printed).toContain("--logout");
2740
2758
  expect(spawnedAgents[0].kill).toHaveBeenCalled();
2741
2759
  });
2760
+ it("should run --list-models in an empty directory of its own, and remove it after", async () => {
2761
+ // Run from the workspace's own directory, the agent would find the
2762
+ // workspace's .mcp.json there and connect to it as though it were the
2763
+ // workspace's agent.
2764
+ const { spawn } = await import("node:child_process");
2765
+ const newSession = vi.fn().mockResolvedValue({ sessionId: "m-sess", configOptions: [] });
2766
+ mockConnection({ newSession });
2767
+ await runAgentCommand("list-models", ["gemini", "--acp"], agentrqConfig, fakeBridge());
2768
+ const sessionCwd = newSession.mock.calls[0][0].cwd;
2769
+ expect(sessionCwd).not.toBe(process.cwd());
2770
+ expect(sessionCwd.startsWith(tmpdir())).toBe(true);
2771
+ expect(newSession.mock.calls[0][0].mcpServers).toEqual([]);
2772
+ expect(vi.mocked(spawn)).toHaveBeenLastCalledWith("gemini", ["--acp"], expect.objectContaining({ cwd: sessionCwd }));
2773
+ expect(existsSync(sessionCwd)).toBe(false);
2774
+ });
2775
+ it("should remove the --list-models directory when the agent fails to start", async () => {
2776
+ const { spawn } = await import("node:child_process");
2777
+ mockConnection({ initialize: vi.fn().mockRejectedValue(new Error("boom")) });
2778
+ await expect(runAgentCommand("list-models", ["gemini", "--acp"], agentrqConfig, fakeBridge())).rejects.toThrow();
2779
+ const cwd = vi.mocked(spawn).mock.calls.at(-1)[2].cwd;
2780
+ expect(cwd.startsWith(tmpdir())).toBe(true);
2781
+ expect(existsSync(cwd)).toBe(false);
2782
+ });
2783
+ it("should leave the other commands running where the gateway runs", async () => {
2784
+ const { spawn } = await import("node:child_process");
2785
+ mockConnection({});
2786
+ await runAgentCommand("agent-info", ["gemini", "--acp"], agentrqConfig, fakeBridge());
2787
+ expect(vi.mocked(spawn).mock.calls.at(-1)[2]).not.toHaveProperty("cwd");
2788
+ });
2742
2789
  it("should list models from configOptions and cleanly close session", async () => {
2743
2790
  mockConnection({
2744
2791
  newSession: vi.fn().mockResolvedValue({