Skip to content

Commit 7db1461

Browse files
committed
feat(opencode): ship plugin as agent-cya/opencode subpath export
New src/opencode-plugin.ts exports: - createAgentCyaPlugin({ reviewer? }): factory typed against @opencode-ai/plugin's Plugin (type-only import, no runtime dep) - AgentCya: default-options instance for zero-config consumers - default = AgentCya, so { plugin: ['agent-cya/opencode'] } in opencode.json works with no plugin file written by the consumer Plugin runs the same in-process review pipeline (evaluate from pipeline.ts) so the OpenCode path and the Claude Code hook path share rule-deny, file enrichment, LLM review, and the audit log. The permission.ask handler emits AgentCYA's decision directly into OpenCode's output.status — no spawn, no JSON round-trip. knip.json gains a per-file 'duplicates' ignore for opencode-plugin.ts: the AgentCya named export + default export of the same identity is the documented OpenCode plugin idiom, but knip flags it as duplicate. Scoped to the one file so the rule still fires elsewhere. (Note: upstream OpenCode currently gates permission.ask for first- encounter commands — anomalyco/opencode#19927 — so the hook may not fire today even though the plugin is correct. Documented in README.)
1 parent a415445 commit 7db1461

3 files changed

Lines changed: 177 additions & 1 deletion

File tree

‎knip.json‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"entry": ["src/main.ts"]
2+
"entry": ["src/main.ts"],
3+
"ignoreIssues": {
4+
"src/opencode-plugin.ts": ["duplicates"]
5+
}
36
}

‎src/opencode-plugin.test.ts‎

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import { describe, it, expect, vi, beforeEach } from "vitest";
2+
3+
vi.mock("./pipeline.ts", () => ({
4+
evaluate: vi.fn(),
5+
}));
6+
7+
import {
8+
createAgentCyaPlugin,
9+
AgentCya,
10+
default as DefaultExport,
11+
} from "./opencode-plugin.ts";
12+
import { evaluate } from "./pipeline.ts";
13+
14+
const evaluateMock = vi.mocked(evaluate);
15+
16+
const invokePlugin = async (
17+
plugin: ReturnType<typeof createAgentCyaPlugin>,
18+
): Promise<Record<string, (...args: never[]) => Promise<void>>> => {
19+
return (
20+
plugin as unknown as () => Promise<
21+
Record<string, (...args: never[]) => Promise<void>>
22+
>
23+
)();
24+
};
25+
26+
/* eslint-disable functional/prefer-immutable-types -- `output` is intentionally mutable; the plugin's contract is to mutate output.status, and the test observes that mutation */
27+
const callPermissionAsk = (
28+
hooks: Readonly<Record<string, (...args: never[]) => Promise<void>>>,
29+
input: Readonly<{ type?: string; pattern?: string }>,
30+
output: { status: "ask" | "deny" | "allow" },
31+
): Promise<void> =>
32+
(
33+
hooks["permission.ask"] as unknown as (
34+
i: typeof input,
35+
o: typeof output,
36+
) => Promise<void>
37+
)(input, output);
38+
/* eslint-enable functional/prefer-immutable-types */
39+
40+
describe("createAgentCyaPlugin", () => {
41+
beforeEach(() => {
42+
evaluateMock.mockReset();
43+
evaluateMock.mockResolvedValue({
44+
decision: { decision: "allow", reason: "safe" },
45+
source: "llm",
46+
});
47+
});
48+
49+
it("invoking the factory yields a hooks object with permission.ask", async () => {
50+
const hooks = await invokePlugin(createAgentCyaPlugin());
51+
expect(typeof hooks["permission.ask"]).toBe("function");
52+
});
53+
54+
it("forwards input.type/input.pattern to evaluate and sets output.status", async () => {
55+
const hooks = await invokePlugin(createAgentCyaPlugin());
56+
const output = { status: "ask" as "ask" | "deny" | "allow" };
57+
58+
await callPermissionAsk(hooks, { type: "Bash", pattern: "ls -la" }, output);
59+
60+
expect(evaluateMock).toHaveBeenCalledTimes(1);
61+
expect(evaluateMock).toHaveBeenCalledWith(
62+
{ toolType: "Bash", command: "ls -la", fileContent: null },
63+
"claude",
64+
);
65+
expect(output.status).toBe("allow");
66+
});
67+
68+
it("defaults command to '' when input.pattern is missing", async () => {
69+
const hooks = await invokePlugin(createAgentCyaPlugin());
70+
const output = { status: "ask" as "ask" | "deny" | "allow" };
71+
72+
await callPermissionAsk(hooks, { type: "Bash" }, output);
73+
74+
expect(evaluateMock).toHaveBeenCalledWith(
75+
{ toolType: "Bash", command: "", fileContent: null },
76+
"claude",
77+
);
78+
});
79+
80+
it("defaults command to '' when input.pattern is a string array", async () => {
81+
const hooks = await invokePlugin(createAgentCyaPlugin());
82+
const output = { status: "ask" as "ask" | "deny" | "allow" };
83+
84+
await callPermissionAsk(
85+
hooks,
86+
{ type: "Bash", pattern: ["ls", "-la"] } as unknown as Readonly<{
87+
type: string;
88+
pattern: string;
89+
}>,
90+
output,
91+
);
92+
93+
expect(evaluateMock).toHaveBeenCalledWith(
94+
{ toolType: "Bash", command: "", fileContent: null },
95+
"claude",
96+
);
97+
});
98+
99+
it("writes the LLM decision through to output.status", async () => {
100+
evaluateMock.mockResolvedValueOnce({
101+
decision: { decision: "deny", reason: "nope" },
102+
source: "rule",
103+
});
104+
105+
const hooks = await invokePlugin(createAgentCyaPlugin());
106+
const output = { status: "ask" as "ask" | "deny" | "allow" };
107+
108+
await callPermissionAsk(
109+
hooks,
110+
{ type: "Bash", pattern: "rm -rf /" },
111+
output,
112+
);
113+
114+
expect(output.status).toBe("deny");
115+
});
116+
117+
it("passes 'opencode' to evaluate when configured with reviewer: 'opencode'", async () => {
118+
const hooks = await invokePlugin(
119+
createAgentCyaPlugin({ reviewer: "opencode" }),
120+
);
121+
const output = { status: "ask" as "ask" | "deny" | "allow" };
122+
123+
await callPermissionAsk(
124+
hooks,
125+
{ type: "Write", pattern: "foo.ts" },
126+
output,
127+
);
128+
129+
expect(evaluateMock).toHaveBeenCalledWith(
130+
{ toolType: "Write", command: "foo.ts", fileContent: null },
131+
"opencode",
132+
);
133+
});
134+
});
135+
136+
describe("AgentCya default instance", () => {
137+
beforeEach(() => {
138+
evaluateMock.mockReset();
139+
evaluateMock.mockResolvedValue({
140+
decision: { decision: "allow", reason: "safe" },
141+
source: "llm",
142+
});
143+
});
144+
145+
it("default export equals the AgentCya named export", () => {
146+
expect(DefaultExport).toBe(AgentCya);
147+
});
148+
});

‎src/opencode-plugin.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { Plugin } from "@opencode-ai/plugin";
2+
import { evaluate, type Reviewer } from "./pipeline.ts";
3+
4+
type Options = Readonly<{ reviewer?: Reviewer }>;
5+
6+
export const createAgentCyaPlugin =
7+
({ reviewer = "claude" }: Options = {}): Plugin =>
8+
async () => ({
9+
"permission.ask": async (input, output) => {
10+
const pattern = input.pattern;
11+
const { decision } = await evaluate(
12+
{
13+
toolType: input.type,
14+
command: typeof pattern === "string" ? pattern : "",
15+
fileContent: null,
16+
},
17+
reviewer,
18+
);
19+
// eslint-disable-next-line functional/immutable-data -- OpenCode's permission.ask contract requires mutating output.status
20+
output.status = decision.decision;
21+
},
22+
});
23+
24+
export const AgentCya = createAgentCyaPlugin();
25+
export default AgentCya;

0 commit comments

Comments
 (0)