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
scout/forge:scout/9936-the-forge-serves-its-opencode-plugin-at → main 7 commits fork
8 files
changed +77
−3
Dockerfile +2
| @@ -21,6 +21,8 @@ COPY src ./src | ||
| 21 | 21 | COPY docs/PRINCIPLES.md ./docs/PRINCIPLES.md |
| 22 | 22 | # The design principles a src/web/ review is held to (src/crew/design_principles.ts). |
| 23 | 23 | 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 | |
| 24 | 26 | # src/ alone: nothing under it imports scripts/ or test/ (test/src-imports.test.ts), |
| 25 | 27 | # and the boot proof below fails the build if that ever stops being so (#2652). |
| 26 | 28 |
plugins/opencode/README.md +4 −2
| @@ -1,8 +1,10 @@ | ||
| 1 | 1 | # forge for OpenCode |
| 2 | 2 | |
| 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` | |
| 4 | 4 | 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 | |
| 6 | 8 | forge's MCP server and the `forge` command: `new`, `tell`, `status`, `decide`, `plan`, |
| 7 | 9 | `preview`, `pulls`, `runs`, `log`, `asked`. |
| 8 | 10 | `/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[] = [ | ||
| 67 | 67 | "contribute", |
| 68 | 68 | "deploy-to", |
| 69 | 69 | "pipeline", |
| 70 | "plugins", | |
| 70 | 71 | "events", |
| 71 | 72 | "bring", |
| 72 | 73 | "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"; | ||
| 33 | 33 | import { measureSubmit } from "./measure.ts"; |
| 34 | 34 | import { settingsRoutes } from "./settings_routes.ts"; |
| 35 | 35 | import { pinnedRoutes } from "./pinned_routes.ts"; |
| 36 | import { pluginRoutes } from "./plugin_routes.ts"; | |
| 36 | 37 | import { boardsAsk, boardsPage } from "../web/pages/boards.ts"; |
| 37 | 38 | import { boardNew, boardPin, boardPost, pinPost } from "../web/pages/boards-edit.ts"; |
| 38 | 39 | import { boardsHoldPost } from "../web/pages/boards-holds.ts"; |
| @@ -902,6 +903,8 @@ export function buildRouter(): Router<Handler> { | ||
| 902 | 903 | settingsRoutes(router, knownPatterns); |
| 903 | 904 | // The Deploy to… page for a pinned forge (#8744); literal, above /:owner. |
| 904 | 905 | pinnedRoutes(router); |
| 906 | // The OpenCode plugin file, one download (#9936); literal, above /:owner. | |
| 907 | pluginRoutes(router); | |
| 905 | 908 | // Each THREATS.md protection against the last gate; literal, above /:owner. |
| 906 | 909 | router.add("GET", "/resilience", resiliencePage); |
| 907 | 910 | 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 { | ||
| 594 | 594 | <p class="small">OpenCode</p> |
| 595 | 595 | ${copyField( |
| 596 | 596 | "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`, | |
| 598 | 598 | "OpenCode command", |
| 599 | 599 | "Copy command", |
| 600 | 600 | )}`, |
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 () => | ||
| 96 | 96 | const repo = join(import.meta.dir, ".."); |
| 97 | 97 | cpSync(join(repo, "src"), join(image, "src"), { recursive: true }); |
| 98 | 98 | 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")); | |
| 99 | 101 | expect(existsSync(join(image, "scripts"))).toBe(false); |
| 100 | 102 | const result = await proveBoot({ tree: image, version: "9.9.9", budgetMs: 60_000 }); |
| 101 | 103 | expect(result).toMatchObject({ ok: true }); |
new above
offline