Public: anyone can read this; the crew can change it.

The forge serves its OpenCode plugin at one stable address, so it installs with one download and no clone #9950

Parked opened this
Closed without merging

scout/forge:scout/9936-the-forge-serves-its-opencode-plugin-at → main 7 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
8 files changed +77 −3
Dockerfile +2
@@ -21,6 +21,8 @@ COPY src ./src
2121 COPY docs/PRINCIPLES.md ./docs/PRINCIPLES.md
2222 # The design principles a src/web/ review is held to (src/crew/design_principles.ts).
2323 COPY docs/DESIGN.md ./docs/DESIGN.md
24 # The OpenCode plugin the forge serves at /plugins/opencode/forge.ts (src/http/plugin_routes.ts reads it at boot).
25 COPY plugins/opencode/forge.ts ./plugins/opencode/forge.ts
2426 # src/ alone: nothing under it imports scripts/ or test/ (test/src-imports.test.ts),
2527 # and the boot proof below fails the build if that ever stops being so (#2652).
2628
plugins/opencode/README.md +4 −2
@@ -1,8 +1,10 @@
11 # forge for OpenCode
22
3 Copy `forge.ts` into `.opencode/plugin/` (or list this package under `plugin`
3 Download `forge.ts` into `.opencode/plugin/` (or list this package under `plugin`
44 in `opencode.json`), and set `FORGE_URL` and `FORGE_TOKEN` (make the token at `/settings/tokens` on the forge, which shows the line with its own address filled in):
5 `mkdir -p .opencode/plugin && cp plugins/opencode/forge.ts .opencode/plugin/ && FORGE_URL=<url> FORGE_TOKEN=<token> opencode`. It registers the
5 `mkdir -p .opencode/plugin && curl -fsSo .opencode/plugin/forge.ts <url>/plugins/opencode/forge.ts && FORGE_URL=<url> FORGE_TOKEN=<token> opencode`.
6 The forge serves the same file at that address, no clone needed. With a clone, the second way:
7 `mkdir -p .opencode/plugin && cp plugins/opencode/forge.ts .opencode/plugin/`. It registers the
68 forge's MCP server and the `forge` command: `new`, `tell`, `status`, `decide`, `plan`,
79 `preview`, `pulls`, `runs`, `log`, `asked`.
810 `/forge plan` shows the planner's next wave (the first wave-proposal row of `asks`);
src/core/reserved.ts +1
@@ -67,6 +67,7 @@ export const SYSTEM_NAMES: readonly string[] = [
6767 "contribute",
6868 "deploy-to",
6969 "pipeline",
70 "plugins",
7071 "events",
7172 "bring",
7273 "explore",
src/http/plugin_routes.ts added +27
@@ -0,0 +1,27 @@
1 /**
2 * `GET /plugins/opencode/forge.ts` (#9936): the OpenCode plugin at one stable
3 * address, so it installs with one download and no clone. The bytes are the
4 * file in plugins/opencode, read once at boot: the same file, not a copy.
5 */
6
7 import { readFileSync } from "node:fs";
8 import { weakEtag, holds } from "./etag.ts";
9 import type { Handler } from "./context.ts";
10 import type { Router } from "./router.ts";
11
12 const bytes = readFileSync(new URL("../../plugins/opencode/forge.ts", import.meta.url));
13 const etag = weakEtag(bytes.toString("utf8"));
14 const headers = {
15 "content-type": "text/plain; charset=utf-8",
16 etag,
17 "cache-control": "public, max-age=300",
18 };
19
20 const opencodePlugin: Handler = (ctx) => {
21 if (holds(ctx.request, etag)) return new Response(null, { status: 304, headers });
22 return new Response(bytes, { headers });
23 };
24
25 export function pluginRoutes(router: Router<Handler>): void {
26 router.add("GET", "/plugins/opencode/forge.ts", opencodePlugin);
27 }
src/http/server.ts +3
@@ -33,6 +33,7 @@ import { allFlowsPage } from "../web/pages/flows-all.ts";
3333 import { measureSubmit } from "./measure.ts";
3434 import { settingsRoutes } from "./settings_routes.ts";
3535 import { pinnedRoutes } from "./pinned_routes.ts";
36 import { pluginRoutes } from "./plugin_routes.ts";
3637 import { boardsAsk, boardsPage } from "../web/pages/boards.ts";
3738 import { boardNew, boardPin, boardPost, pinPost } from "../web/pages/boards-edit.ts";
3839 import { boardsHoldPost } from "../web/pages/boards-holds.ts";
@@ -902,6 +903,8 @@ export function buildRouter(): Router<Handler> {
902903 settingsRoutes(router, knownPatterns);
903904 // The Deploy to… page for a pinned forge (#8744); literal, above /:owner.
904905 pinnedRoutes(router);
906 // The OpenCode plugin file, one download (#9936); literal, above /:owner.
907 pluginRoutes(router);
905908 // Each THREATS.md protection against the last gate; literal, above /:owner.
906909 router.add("GET", "/resilience", resiliencePage);
907910 router.add("GET", "/maintenance", maintenancePage);
src/web/pages/account.ts +1 −1
@@ -594,7 +594,7 @@ export function connectCard(ctx: Ctx, token: string | undefined): Html {
594594 <p class="small">OpenCode</p>
595595 ${copyField(
596596 "connect-opencode",
597 `mkdir -p .opencode/plugin && cp plugins/opencode/forge.ts .opencode/plugin/ && ${env} opencode`,
597 `mkdir -p .opencode/plugin && curl -fsSo .opencode/plugin/forge.ts ${url}/plugins/opencode/forge.ts && ${env} opencode`,
598598 "OpenCode command",
599599 "Copy command",
600600 )}`,
test/plugin-opencode-route.test.ts added +37
@@ -0,0 +1,37 @@
1 /** The forge serves its OpenCode plugin at one stable address (#9936). */
2 import { afterAll, beforeAll, expect, test } from "bun:test";
3 import { mkdtemp, rm } from "node:fs/promises";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { createApp } from "../src/app.ts";
7 import { serve, type Servers } from "../src/http/server.ts";
8 import { quietLogs } from "./helpers/globals.ts";
9
10 quietLogs();
11
12 let dataDir = "";
13 let servers: Servers;
14 beforeAll(async () => {
15 dataDir = await mkdtemp(join(tmpdir(), "forge-plugin-route-"));
16 servers = await serve(createApp({ dataDir, port: 0 }));
17 });
18 afterAll(async () => {
19 await servers.stop();
20 await rm(dataDir, { recursive: true, force: true });
21 });
22
23 test("serves the plugin file's bytes with an ETag and the security floor, and 304s a match", async () => {
24 const path = "/plugins/opencode/forge.ts";
25 const res = await fetch(servers.url + path);
26 expect(res.status).toBe(200);
27 expect(res.headers.get("content-type")).toBe("text/plain; charset=utf-8");
28 expect(res.headers.get("x-content-type-options")).toBe("nosniff");
29 expect(res.headers.get("referrer-policy")).toBeTruthy();
30 expect(res.headers.get("content-security-policy")).toBeTruthy();
31 const etag = res.headers.get("etag")!;
32 expect(etag).toBeTruthy();
33 const want = await Bun.file(join(import.meta.dir, "../plugins/opencode/forge.ts")).bytes();
34 expect(new Uint8Array(await res.arrayBuffer())).toEqual(want);
35 const again = await fetch(servers.url + path, { headers: { "if-none-match": etag } });
36 expect(again.status).toBe(304);
37 });
test/release-boot.test.ts +2
@@ -96,6 +96,8 @@ test("this tree, shipped as the image ships it (src/ alone), boots", async () =>
9696 const repo = join(import.meta.dir, "..");
9797 cpSync(join(repo, "src"), join(image, "src"), { recursive: true });
9898 for (const file of ["package.json", "tsconfig.json"]) cpSync(join(repo, file), join(image, file));
99 // The one file outside src/ the Dockerfile copies for the plugin route (#9936).
100 cpSync(join(repo, "plugins/opencode/forge.ts"), join(image, "plugins/opencode/forge.ts"));
99101 expect(existsSync(join(image, "scripts"))).toBe(false);
100102 const result = await proveBoot({ tree: image, version: "9.9.9", budgetMs: 60_000 });
101103 expect(result).toMatchObject({ ok: true });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs