-
Notifications
You must be signed in to change notification settings - Fork 0
feat: scripted install for npm-shipped agents #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6513399
feat(agents): scripted install for npm-shipped agents
joch 6987e63
fix(agents): distinguish spawn failures from Ctrl-C in npm install
joch 8e3b0e3
fix(agents): use shell on Windows so npm.cmd shim resolves
joch d90a215
Merge remote-tracking branch 'origin/main' into opencode-install
joch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { run } from "../util/run.js"; | ||
| import { which } from "../util/which.js"; | ||
| import { OpperError } from "../errors.js"; | ||
|
|
||
| // On Windows, npm ships as a `.cmd` shim. Node refuses to execute .cmd | ||
| // files via spawnSync without a shell — even when given the explicit | ||
| // extension — so we have to route through cmd.exe (shell: true) on | ||
| // Windows. POSIX still runs npm directly, no shell. | ||
| const USE_SHELL = process.platform === "win32"; | ||
|
|
||
| /** | ||
| * Run `npm install -g <pkg>` and surface a useful error if it fails. | ||
| * Used by every adapter whose upstream agent ships as a global npm package. | ||
| */ | ||
| export async function npmInstallGlobal( | ||
| packageName: string, | ||
| docsUrl: string, | ||
| ): Promise<void> { | ||
| if (!(await which("npm"))) { | ||
| throw new OpperError( | ||
| "AGENT_NOT_FOUND", | ||
| "npm is required to install this agent but was not found on PATH.", | ||
| `Install Node.js (which ships npm) from https://nodejs.org, or install ${packageName} manually from ${docsUrl}.`, | ||
| ); | ||
| } | ||
|
|
||
| const result = run("npm", ["install", "-g", packageName], { | ||
| inherit: true, | ||
| shell: USE_SHELL, | ||
| }); | ||
| if (result.code === 0) return; | ||
|
|
||
| // run() collapses both signal-killed children and spawn-time errors | ||
| // (EACCES on the npm binary, transient ENOENT, etc.) to code: -1. | ||
| // We tell them apart by stderr: with `inherit: true` run() only | ||
| // populates stderr when it has captured an Error.message from the | ||
| // spawn-failure path. Empty stderr ⇒ the child started and was killed | ||
| // by a signal (almost always Ctrl-C). | ||
| if (result.code === -1) { | ||
| if (result.stderr.trim().length > 0) { | ||
| throw new OpperError( | ||
| "AGENT_NOT_FOUND", | ||
| `npm install -g ${packageName} failed to start: ${result.stderr.trim()}`, | ||
| `Resolve the underlying error, or install ${packageName} manually from ${docsUrl}.`, | ||
| ); | ||
| } | ||
| throw new OpperError( | ||
| "AGENT_NOT_FOUND", | ||
| `npm install -g ${packageName} was interrupted before completion.`, | ||
| `Re-run to retry, or install manually from ${docsUrl}.`, | ||
| ); | ||
| } | ||
|
|
||
| throw new OpperError( | ||
| "AGENT_NOT_FOUND", | ||
| `npm install -g ${packageName} exited with code ${result.code}`, | ||
| `Check your network connection and npm permissions, or install manually from ${docsUrl}.`, | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
|
|
||
| const whichMock = vi.fn(); | ||
| vi.mock("../../src/util/which.js", () => ({ which: whichMock })); | ||
|
|
||
| const runMock = vi.fn(); | ||
| vi.mock("../../src/util/run.js", () => ({ run: runMock })); | ||
|
|
||
| const { npmInstallGlobal } = await import("../../src/agents/npm-install.js"); | ||
|
|
||
| describe("npmInstallGlobal", () => { | ||
| beforeEach(() => { | ||
| whichMock.mockReset(); | ||
| runMock.mockReset(); | ||
| }); | ||
|
|
||
| it("invokes npm with `install -g <pkg>` and inherited stdio", async () => { | ||
| whichMock.mockResolvedValue("/usr/bin/npm"); | ||
| runMock.mockReturnValue({ code: 0, stdout: "", stderr: "" }); | ||
|
|
||
| await expect( | ||
| npmInstallGlobal("some-pkg", "https://example.test"), | ||
| ).resolves.toBeUndefined(); | ||
|
|
||
| const [cmd, args, opts] = runMock.mock.calls[0]!; | ||
| expect(cmd).toBe("npm"); | ||
| expect(args).toEqual(["install", "-g", "some-pkg"]); | ||
| expect(opts).toMatchObject({ inherit: true }); | ||
| }); | ||
|
|
||
| it("routes through a shell on Windows so cmd.exe can resolve the npm.cmd shim", async () => { | ||
| whichMock.mockResolvedValue("/usr/bin/npm"); | ||
| runMock.mockReturnValue({ code: 0, stdout: "", stderr: "" }); | ||
|
|
||
| await npmInstallGlobal("some-pkg", "https://example.test"); | ||
|
|
||
| const [, , opts] = runMock.mock.calls[0]!; | ||
| // Node's spawnSync refuses to execute .cmd shims directly — without | ||
| // shell:true on Windows, every install would fail with code -1. | ||
| expect((opts as { shell?: boolean }).shell).toBe( | ||
| process.platform === "win32", | ||
| ); | ||
| }); | ||
|
|
||
| it("throws AGENT_NOT_FOUND with a Node.js install hint when npm isn't on PATH", async () => { | ||
| whichMock.mockResolvedValue(null); | ||
|
|
||
| await expect( | ||
| npmInstallGlobal("some-pkg", "https://example.test"), | ||
| ).rejects.toMatchObject({ | ||
| code: "AGENT_NOT_FOUND", | ||
| message: expect.stringContaining("npm is required"), | ||
| }); | ||
| // Bailed out before invoking npm. | ||
| expect(runMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("throws a graceful 'interrupted' error when code is -1 with empty stderr (signal kill)", async () => { | ||
| whichMock.mockResolvedValue("/usr/bin/npm"); | ||
| runMock.mockReturnValue({ code: -1, stdout: "", stderr: "" }); | ||
|
|
||
| await expect( | ||
| npmInstallGlobal("some-pkg", "https://example.test"), | ||
| ).rejects.toMatchObject({ | ||
| code: "AGENT_NOT_FOUND", | ||
| message: expect.stringContaining("interrupted"), | ||
| }); | ||
| }); | ||
|
|
||
| it("surfaces the spawn error when code is -1 with stderr (e.g. EACCES on npm)", async () => { | ||
| whichMock.mockResolvedValue("/usr/bin/npm"); | ||
| runMock.mockReturnValue({ | ||
| code: -1, | ||
| stdout: "", | ||
| stderr: "EACCES: permission denied", | ||
| }); | ||
|
|
||
| await expect( | ||
| npmInstallGlobal("some-pkg", "https://example.test"), | ||
| ).rejects.toMatchObject({ | ||
| code: "AGENT_NOT_FOUND", | ||
| message: expect.stringContaining("EACCES: permission denied"), | ||
| }); | ||
| }); | ||
|
|
||
| it("throws AGENT_NOT_FOUND with the exit code when npm exits non-zero", async () => { | ||
| whichMock.mockResolvedValue("/usr/bin/npm"); | ||
| runMock.mockReturnValue({ code: 13, stdout: "", stderr: "" }); | ||
|
|
||
| await expect( | ||
| npmInstallGlobal("some-pkg", "https://example.test"), | ||
| ).rejects.toMatchObject({ | ||
| code: "AGENT_NOT_FOUND", | ||
| message: expect.stringContaining("exited with code 13"), | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
run()usescode: -1for both signal interruption and process-spawn errors, but this branch treats every-1as a Ctrl-C cancellation. In environments wherenpmexists on PATH but cannot be executed (for example permission-denied or transient command resolution failures), users will get a misleading “interrupted” message and lose the actionable error context. Handle-1by inspecting the underlying error/stderr (or propagate it) so real execution failures are not misreported as user cancellation.Useful? React with 👍 / 👎.