-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add OpenCode execute regression test #1903
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
Closed
Utkarshbhimte
wants to merge
2
commits into
paperclipai:master
from
makerdock:fix/opencode-execute-regression
+182
−0
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import fs from "node:fs/promises"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
| import { execute } from "@paperclipai/adapter-opencode-local/server"; | ||
|
|
||
| async function writeFakeOpenCodeCommand(commandPath: string): Promise<void> { | ||
| const script = `#!/usr/bin/env node | ||
| const fs = require("node:fs"); | ||
| const path = require("node:path"); | ||
|
|
||
| const args = process.argv.slice(2); | ||
| const capturePath = process.env.PAPERCLIP_TEST_CAPTURE_PATH; | ||
|
|
||
| if (args[0] === "models") { | ||
| process.stdout.write("anthropic/claude-opus-4-6\\n"); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| let runtimeConfig = null; | ||
| const xdgConfigHome = process.env.XDG_CONFIG_HOME || null; | ||
| if (xdgConfigHome) { | ||
| const runtimeConfigPath = path.join(xdgConfigHome, "opencode", "opencode.json"); | ||
| if (fs.existsSync(runtimeConfigPath)) { | ||
| runtimeConfig = JSON.parse(fs.readFileSync(runtimeConfigPath, "utf8")); | ||
| } | ||
| } | ||
|
|
||
| if (capturePath) { | ||
| fs.writeFileSync(capturePath, JSON.stringify({ | ||
| argv: args, | ||
| prompt: fs.readFileSync(0, "utf8"), | ||
| xdgConfigHome, | ||
| runtimeConfig, | ||
| paperclipEnvKeys: Object.keys(process.env) | ||
| .filter((key) => key.startsWith("PAPERCLIP_")) | ||
| .sort(), | ||
| }), "utf8"); | ||
| } | ||
|
|
||
| process.stdout.write(JSON.stringify({ type: "step_start", sessionID: "ses_opencode_1" }) + "\\n"); | ||
| process.stdout.write(JSON.stringify({ type: "text", part: { type: "text", text: "hello" } }) + "\\n"); | ||
| process.stdout.write(JSON.stringify({ | ||
| type: "step_finish", | ||
| part: { | ||
| reason: "stop", | ||
| cost: 0, | ||
| tokens: { | ||
| input: 10, | ||
| output: 5, | ||
| cache: { read: 0, write: 0 }, | ||
| }, | ||
| }, | ||
| }) + "\\n"); | ||
| `; | ||
| await fs.writeFile(commandPath, script, "utf8"); | ||
| await fs.chmod(commandPath, 0o755); | ||
| } | ||
|
|
||
| type CapturePayload = { | ||
| argv: string[]; | ||
| prompt: string; | ||
| xdgConfigHome: string | null; | ||
| runtimeConfig: { | ||
| permission?: { | ||
| external_directory?: string; | ||
| read?: string; | ||
| }; | ||
| theme?: string; | ||
| } | null; | ||
| paperclipEnvKeys: string[]; | ||
| }; | ||
|
|
||
| describe("opencode execute", () => { | ||
| it("injects external_directory allow config for headless runs", async () => { | ||
| const root = await fs.mkdtemp(path.join(os.tmpdir(), "paperclip-opencode-execute-")); | ||
| const workspace = path.join(root, "workspace"); | ||
| const agentHome = path.join(root, "agent-home"); | ||
| const commandPath = path.join(root, "opencode"); | ||
| const capturePath = path.join(root, "capture.json"); | ||
| const sourceConfigHome = path.join(root, "source-config"); | ||
| await fs.mkdir(workspace, { recursive: true }); | ||
| await fs.mkdir(agentHome, { recursive: true }); | ||
| await fs.mkdir(path.join(sourceConfigHome, "opencode"), { recursive: true }); | ||
| await fs.writeFile( | ||
| path.join(sourceConfigHome, "opencode", "opencode.json"), | ||
| `${JSON.stringify({ theme: "system", permission: { read: "allow" } }, null, 2)}\n`, | ||
| "utf8", | ||
| ); | ||
| await writeFakeOpenCodeCommand(commandPath); | ||
|
|
||
| const previousHome = process.env.HOME; | ||
| process.env.HOME = root; | ||
|
|
||
| let commandNotes: string[] = []; | ||
| try { | ||
| const result = await execute({ | ||
| runId: "run-1", | ||
| agent: { | ||
| id: "agent-1", | ||
| companyId: "company-1", | ||
| name: "OpenCode Coder", | ||
| adapterType: "opencode_local", | ||
| adapterConfig: {}, | ||
| }, | ||
| runtime: { | ||
| sessionId: null, | ||
| sessionParams: null, | ||
| sessionDisplayId: null, | ||
| taskKey: null, | ||
| }, | ||
| config: { | ||
| command: commandPath, | ||
| cwd: workspace, | ||
| model: "anthropic/claude-opus-4-6", | ||
| env: { | ||
| PAPERCLIP_TEST_CAPTURE_PATH: capturePath, | ||
| XDG_CONFIG_HOME: sourceConfigHome, | ||
| }, | ||
| promptTemplate: "Follow the paperclip heartbeat.", | ||
| }, | ||
| context: { | ||
| paperclipWorkspace: { | ||
| cwd: workspace, | ||
| source: "project_primary", | ||
| agentHome, | ||
| }, | ||
| }, | ||
| authToken: "run-jwt-token", | ||
| onLog: async () => {}, | ||
| onMeta: async (meta) => { | ||
| commandNotes = meta.commandNotes ?? []; | ||
| }, | ||
| }); | ||
|
|
||
| expect(result.exitCode).toBe(0); | ||
| expect(result.errorMessage).toBeNull(); | ||
|
|
||
| const capture = JSON.parse(await fs.readFile(capturePath, "utf8")) as CapturePayload; | ||
| expect(capture.argv).toEqual([ | ||
| "run", | ||
| "--format", | ||
| "json", | ||
| "--model", | ||
| "anthropic/claude-opus-4-6", | ||
| ]); | ||
| expect(capture.prompt).toContain("Follow the paperclip heartbeat."); | ||
| expect(capture.paperclipEnvKeys).toEqual( | ||
| expect.arrayContaining([ | ||
| "PAPERCLIP_AGENT_ID", | ||
| "PAPERCLIP_API_KEY", | ||
| "PAPERCLIP_API_URL", | ||
| "PAPERCLIP_COMPANY_ID", | ||
| "PAPERCLIP_RUN_ID", | ||
| "PAPERCLIP_WORKSPACE_CWD", | ||
| ]), | ||
| ); | ||
| expect(capture.runtimeConfig).toMatchObject({ | ||
| theme: "system", | ||
| permission: { | ||
| read: "allow", | ||
| external_directory: "allow", | ||
| }, | ||
| }); | ||
| expect(commandNotes).toEqual( | ||
| expect.arrayContaining([ | ||
| "Injected runtime OpenCode config with permission.external_directory=allow to avoid headless approval prompts.", | ||
| ]), | ||
| ); | ||
| expect(capture.xdgConfigHome).not.toBe(sourceConfigHome); | ||
| await expect(fs.access(capture.xdgConfigHome ?? "")).rejects.toThrow(); | ||
| } finally { | ||
| if (previousHome === undefined) { | ||
| delete process.env.HOME; | ||
| } else { | ||
| process.env.HOME = previousHome; | ||
| } | ||
| await fs.rm(root, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.