Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/opencode-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Plannotator builds against the stable `@opencode/plugin` API and checks its inst
- **Agent switching.** `ctx.session.switchAgent` arrived with the same plugin API generation. On a host that exposes it, an agent switch chosen in the review UI is applied to the session. On an older host the plan is still approved and a warning is written to the server log; switch to `build` manually before implementation.
- **Ask this session.** In code review, annotate and `/plannotator-last`, Ask AI is answered by your OpenCode 2 session, and it is the only Ask AI option there. In plan review the session is waiting for your decision, so it gives a quick answer from its context only. Remote sessions and OpenCode 1 use a separate provider you pick.
- **Cancellation.** Stable V2 tool execution exposes an abort signal, but Plannotator's V2 adapter does not yet forward it to the review server or CLI child. Cancelling a turn cannot stop that review immediately.
- **Session URLs.** OpenCode 2 has a TUI plugin entry point, but it is separate from the server plugin Plannotator registers, so there is no toast to show and the plugin's own console output is discarded by the host unless you start it with `OPENCODE_PRINT_LOGS=1`. Instead, on a host whose plugin API exposes `session.synthetic`, Plannotator posts the URL into the session transcript as a `Plannotator session ready: <url>` notice, injected with `resume: false` so it appears without waking a model turn. This covers every way a session opens: the three slash commands and the `submit_plan` plan review, whether the review runs on the embedded runtime or the CLI. That is the link to open for a remote session, which gets no browser opened for it. On an older host without `session.synthetic` the URL only reaches that discarded console output, so run with `OPENCODE_PRINT_LOGS=1` there.
- **Session URLs.** OpenCode 2 has a TUI plugin entry point, but it is separate from the server plugin Plannotator registers, so there is no toast to show and the plugin's own console output is discarded by the host unless you start it with `OPENCODE_PRINT_LOGS=1`. Instead, on a host whose plugin API exposes `session.synthetic`, Plannotator posts the URL into the session transcript as a `Plannotator session ready: <url>` notice, injected with `resume: false` so it appears without waking a model turn. The notice is for you, not the model: Plannotator removes it from every model request, so after a plan decision the model reads the decision, not the URL line. This covers every way a session opens: the three slash commands and the `submit_plan` plan review, whether the review runs on the embedded runtime or the CLI. That is the link to open for a remote session, which gets no browser opened for it. On an older host without `session.synthetic` the URL only reaches that discarded console output, so run with `OPENCODE_PRINT_LOGS=1` there.

### OpenCode 1

Expand Down
84 changes: 84 additions & 0 deletions apps/opencode-plugin/command-interception.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { afterEach, describe, expect, test } from "bun:test";
import { writeFileSync } from "node:fs";
import path from "node:path";
import { createTestEnvironment } from "../../tests/helpers/environment";
import PlannotatorPlugin from "./index";

Expand Down Expand Up @@ -77,6 +79,88 @@ describe("OpenCode 1 command interception", () => {
}
}

// OpenCode runs a model turn for the command's own message whatever the hook
// does, so feedback sent as a separate prompt ran a SECOND turn that answered
// the same review again. Failure caught: the feedback leaving the command's
// own message, or that message being answered by a different agent, model
// or variant than the separate prompt was.
const routing = [
{
name: "a review agent switch answers on that agent's model and variant",
outcome: { decision: "annotated", feedback: "Rename the helper.", agentSwitch: "reviewer" },
expected: { agent: "reviewer", model: { providerID: "acme", modelID: "careful-1", variant: "max" } },
},
{
// The old prompt named no agent, so OpenCode's default agent answered on
// the session's model, whatever the TUI had picked for the command.
name: "no agent named: OpenCode's default agent on the session's model",
outcome: { decision: "annotated", feedback: "Rename the helper." },
expected: { agent: "build", model: { providerID: "acme", modelID: "everyday-1" } },
},
];
for (const { name, outcome, expected } of routing) {
test.skipIf(process.platform === "win32")(`feedback becomes the command's own message: ${name}`, async () => {
environment.reset();
const root = environment.makeTempDir();
process.env.PLANNOTATOR_DATA_DIR = root;
const binary = path.join(root, "fake-cli.ts");
writeFileSync(binary, `#!/usr/bin/env bun
await Bun.stdin.text();
console.log(${JSON.stringify(JSON.stringify(outcome))});
`, { mode: 0o755 });
process.env.PLANNOTATOR_BIN = binary;

const client: any = makeClient();
const sent: unknown[] = [];
client.session.prompt = async (request: unknown) => {
sent.push(request);
return {};
};
client.session.get = async () => ({ data: { id: "ses_1", model: { id: "everyday-1", providerID: "acme", variant: "default" } } });
client.app.agents = async () => ({
data: [
{ name: "build", mode: "primary" },
{ name: "reviewer", mode: "primary", model: { providerID: "acme", modelID: "careful-1" }, variant: "max" },
],
});
client.config.providers = async () => ({
data: { providers: [{ id: "acme", models: { "careful-1": { variants: { max: {} } }, "everyday-1": {} } }] },
});
const plugin = await PlannotatorPlugin(
{ client, directory: root } as never,
{ runtime: "cli" } as never,
) as Record<string, any>;

const parts: any[] = [{ type: "text", text: "stub body" }];
await plugin["command.execute.before"](
{ command: "plannotator-review", sessionID: "ses_1", arguments: "" },
{ parts },
);

expect(sent).toHaveLength(0);
expect(parts).toHaveLength(1);
expect(parts[0].text).toContain("Rename the helper.");

// OpenCode then builds the command's message from those parts, with the
// agent and model the TUI picked for the command.
const tuiPick = { agent: "plan", model: { providerID: "acme", modelID: "tui-pick", variant: "low" } };
const message = structuredClone(tuiPick);
await plugin["chat.message"](
{ sessionID: "ses_1", agent: "plan" },
{ message, parts: [{ ...parts[0], id: "prt_1" }] },
);
expect(message).toEqual(expected as never);

// One-shot: the next message in the session is the user's own.
const next = structuredClone(tuiPick);
await plugin["chat.message"](
{ sessionID: "ses_1", agent: "plan" },
{ message: next, parts: [{ ...parts[0], id: "prt_2" }] },
);
expect(next).toEqual(tuiPick);
}, 20_000);
}

test("an unrelated command keeps its parts untouched", async () => {
environment.reset();
process.env.PLANNOTATOR_BIN = "/nonexistent/plannotator-interception-test";
Expand Down
Loading
Loading