Public: anyone can read this; the crew can change it.
A tell carries the page it was told from: `from` on the tell action, the API and the MCP, shown on the issue #1581
scout/forge:scout/1463-a-tell-carries-the-page-it-was-told-from → main 4 commits fork
9 files
changed +245
−8
README.md +7
| @@ -392,6 +392,13 @@ in production, over the last 7 days (also in the map and milestones API and MCP) | ||
| 392 | 392 | words and where each is decided, the box, the three lists, a tell, a run |
| 393 | 393 | and a change each on its own page, and the board. |
| 394 | 394 | |
| 395 | A tell can say the page it was told from: `from`, a path on this forge such as | |
| 396 | `/owner/app/issues/3` (up to 300 characters, nothing that leaves the origin), | |
| 397 | on `POST /api/v1/feed/tells` and the MCP `tell` tool. The issue then ends with | |
| 398 | "told from" and a link. The studio's tell forms and the feed's outbox send | |
| 399 | their own page; a bad `from` is a 422 `invalid_from` on the API and MCP, and | |
| 400 | is dropped on the page's form. The terminal does not tell, so it names no page. | |
| 401 | ||
| 395 | 402 | `docs/CONTRACT.md` is who may do what, as a table: an app's `contract.json`, |
| 396 | 403 | the one evaluator every app decides with, the audit that reads it from git, |
| 397 | 404 | the probe that knocks on the running app, and the wall a person alone |
scripts/gate.ts +1
| @@ -167,6 +167,7 @@ export const GATE: readonly string[] = [ | ||
| 167 | 167 | "test/fleet-ops.test.ts", |
| 168 | 168 | "test/meter.test.ts", |
| 169 | 169 | "test/told-by.test.ts", |
| 170 | "test/tell-from.test.ts", | |
| 170 | 171 | "test/highlight.test.ts", |
| 171 | 172 | "test/diff-edge-cases.test.ts", |
| 172 | 173 | "test/diff.test.ts", |
src/actions/tell.ts +35 −1
| @@ -37,13 +37,40 @@ export type TellError = | ||
| 37 | 37 | | "unverified" |
| 38 | 38 | | TitleError |
| 39 | 39 | | "body_too_long" |
| 40 | /** A `from` that is not a path on this forge (`fromPath`). */ | |
| 41 | | "invalid_from" | |
| 40 | 42 | | MirrorError; |
| 41 | 43 | |
| 44 | /** The longest page path a tell may say it was told from. */ | |
| 45 | export const FROM_MAX = 300; | |
| 46 | ||
| 47 | /** What every door says when `from` is refused. */ | |
| 48 | export const FROM_WORDS = `from is a path on this forge, like /owner/app/issues/3, up to ${FROM_MAX} characters`; | |
| 49 | ||
| 50 | // A rooted path of plain URL characters. No scheme, no "//", no quotes, | |
| 51 | // brackets, backticks or stars: nothing that leaves this origin or that the | |
| 52 | // issue's markdown could read as anything but a link. | |
| 53 | const FROM_PATH = /^\/(?!\/)[A-Za-z0-9\-._~%:@!$&+,;=?#/]*$/; | |
| 54 | ||
| 55 | /** | |
| 56 | * The page a tell was told from, or undefined for none: empty and absent are | |
| 57 | * the same; a full URL, a path off this forge or an over-long one is refused. | |
| 58 | */ | |
| 59 | export function fromPath(raw: unknown): Result<string | undefined, "invalid_from"> { | |
| 60 | if (raw === undefined || raw === null || raw === "") return ok(undefined); | |
| 61 | if (typeof raw !== "string") return err("invalid_from"); | |
| 62 | const path = raw.trim(); | |
| 63 | if (path === "") return ok(undefined); | |
| 64 | if (path.length > FROM_MAX || !FROM_PATH.test(path)) return err("invalid_from"); | |
| 65 | return ok(path); | |
| 66 | } | |
| 67 | ||
| 42 | 68 | export async function tell( |
| 43 | 69 | app: App, |
| 44 | 70 | actor: User | undefined, |
| 45 | 71 | repo: Repo, |
| 46 | 72 | said: string, |
| 73 | from?: string, | |
| 47 | 74 | ): Promise<Result<Told, TellError>> { |
| 48 | 75 | // A tell is a run: only someone who may direct the crew here says one, |
| 49 | 76 | // and a refused tell opens nothing (`crew/direct.ts`). |
| @@ -53,6 +80,8 @@ export async function tell( | ||
| 53 | 80 | // A tell sends the crew to work in this person's name: an address not yet |
| 54 | 81 | // shown to be theirs tells nothing (`verify.ts`). |
| 55 | 82 | if (!requireVerified(app, participant.value).ok) return err("unverified"); |
| 83 | const page = fromPath(from); | |
| 84 | if (!page.ok) return page; | |
| 56 | 85 | const text = said.trim(); |
| 57 | 86 | if (text === "") return err("empty"); |
| 58 | 87 | const firstLine = text.split(/\n/)[0]!.trim(); |
| @@ -60,7 +89,12 @@ export async function tell( | ||
| 60 | 89 | firstLine.length <= 80 ? firstLine : `${firstLine.slice(0, 77).trimEnd()}…`; |
| 61 | 90 | const opened = openIssue(app, actor, repo, { |
| 62 | 91 | title, |
| 63 | body: text === firstLine ? "" : text, | |
| 92 | body: [ | |
| 93 | text === firstLine ? "" : text, | |
| 94 | page.value === undefined ? "" : `told from [${page.value}](${page.value})`, | |
| 95 | ] | |
| 96 | .filter((part) => part !== "") | |
| 97 | .join("\n\n"), | |
| 64 | 98 | }); |
| 65 | 99 | if (!opened.ok) return opened; |
| 66 | 100 | const crew = listCrewRoles(app.db, repo.id); |
src/http/feed.ts +4 −2
| @@ -3,7 +3,7 @@ | ||
| 3 | 3 | * are the signed-in viewer's own — there is no way to ask for another's. |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | import { tell, UNVERIFIED_WORDS } from "../actions/tell.ts"; | |
| 6 | import { FROM_WORDS, fromPath, tell, UNVERIFIED_WORDS } from "../actions/tell.ts"; | |
| 7 | 7 | import { DIRECTION_NOTE } from "../crew/direct.ts"; |
| 8 | 8 | import { audienceFilter } from "../core/event_audience.ts"; |
| 9 | 9 | import { getEvent } from "../core/events.ts"; |
| @@ -154,13 +154,15 @@ export function feedRoutes(router: Router<Handler>): void { | ||
| 154 | 154 | const text = str(input.text).trim(); |
| 155 | 155 | if (text === "" || text.length > 2000) |
| 156 | 156 | return apiError("invalid_text", 422, "text is 1 to 2000 characters"); |
| 157 | const from = fromPath(input.from); | |
| 158 | if (!from.ok) return apiError("invalid_from", 422, FROM_WORDS); | |
| 157 | 159 | const [owner, name, extra] = str(input.app).split("/"); |
| 158 | 160 | const repo = |
| 159 | 161 | owner && name && extra === undefined |
| 160 | 162 | ? getRepo(ctx.app.db, owner, name) |
| 161 | 163 | : undefined; |
| 162 | 164 | if (!repo || !canRead(ctx.app.db, repo, viewer)) return notFound(); |
| 163 | const told = await tell(ctx.app, viewer, repo, text); | |
| 165 | const told = await tell(ctx.app, viewer, repo, text, from.value); | |
| 164 | 166 | if (!told.ok) |
| 165 | 167 | return told.error === "forbidden" |
| 166 | 168 | ? apiError("forbidden", 403, DIRECTION_NOTE) |
src/http/mcp.ts +10 −1
| @@ -46,7 +46,7 @@ import { | ||
| 46 | 46 | CARD_URI, |
| 47 | 47 | cardFor, |
| 48 | 48 | } from "../web/studio/cards.ts"; |
| 49 | import { tell, toldWords, UNVERIFIED_WORDS } from "../actions/tell.ts"; | |
| 49 | import { FROM_MAX, FROM_WORDS, fromPath, tell, toldWords, UNVERIFIED_WORDS } from "../actions/tell.ts"; | |
| 50 | 50 | import { readReturns, reportSpend } from "../actions/returns.ts"; |
| 51 | 51 | import { |
| 52 | 52 | readUsage, |
| @@ -299,6 +299,12 @@ const TOOLS: readonly Tool[] = [ | ||
| 299 | 299 | properties: { |
| 300 | 300 | repo: REPO_ARG, |
| 301 | 301 | text: { type: "string", maxLength: 8000 }, |
| 302 | from: { | |
| 303 | type: "string", | |
| 304 | maxLength: FROM_MAX, | |
| 305 | description: | |
| 306 | "The page on this forge the tell was told from, as a path like /owner/app/issues/3. The issue says \"told from\" and links it.", | |
| 307 | }, | |
| 302 | 308 | context: { |
| 303 | 309 | type: "object", |
| 304 | 310 | description: |
| @@ -317,6 +323,8 @@ const TOOLS: readonly Tool[] = [ | ||
| 317 | 323 | async run(ctx, user, args) { |
| 318 | 324 | const repo = repoArg(ctx, user, args); |
| 319 | 325 | if (isAnswer(repo)) return repo; |
| 326 | const from = fromPath(args["from"]); | |
| 327 | if (!from.ok) return bad("invalid_from", FROM_WORDS); | |
| 320 | 328 | const where = parseContext(args["context"]); |
| 321 | 329 | if (where === null) |
| 322 | 330 | return bad( |
| @@ -330,6 +338,7 @@ const TOOLS: readonly Tool[] = [ | ||
| 330 | 338 | str(args["text"]) === "" |
| 331 | 339 | ? "" |
| 332 | 340 | : `${str(args["text"])}${contextNote(where)}`, |
| 341 | from.value, | |
| 333 | 342 | ); |
| 334 | 343 | if (!told.ok) |
| 335 | 344 | return bad( |
src/web/studio/actions.ts +10 −2
| @@ -11,7 +11,7 @@ | ||
| 11 | 11 | import { mayView } from "../../core/sealed.ts"; |
| 12 | 12 | import { holdPull, releasePull } from "../../actions/holds.ts"; |
| 13 | 13 | import { addComment } from "../../actions/issues.ts"; |
| 14 | import { tell } from "../../actions/tell.ts"; | |
| 14 | import { fromPath, tell } from "../../actions/tell.ts"; | |
| 15 | 15 | import { DIRECTION_NOTE } from "../../crew/direct.ts"; |
| 16 | 16 | import { mergePull } from "../../actions/pulls.ts"; |
| 17 | 17 | import { revertPull, type RevertPullError } from "../../actions/revert.ts"; |
| @@ -52,7 +52,15 @@ async function tellFrom( | ||
| 52 | 52 | tone: "danger", |
| 53 | 53 | message: "Say something first.", |
| 54 | 54 | }); |
| 55 | const told = await tell(ctx.app, ctx.auth.user, repo, text); | |
| 55 | const page = fromPath(field(form, "from")); | |
| 56 | const told = await tell( | |
| 57 | ctx.app, | |
| 58 | ctx.auth.user, | |
| 59 | repo, | |
| 60 | text, | |
| 61 | // The page the form sat on; a value that is not a path here is no page. | |
| 62 | page.ok ? page.value : undefined, | |
| 63 | ); | |
| 56 | 64 | if (!told.ok) |
| 57 | 65 | return flashTo(ctx, back, { |
| 58 | 66 | tone: "danger", |
src/web/studio/app.ts +1
| @@ -278,6 +278,7 @@ export async function studioApp( | ||
| 278 | 278 | method="post" |
| 279 | 279 | action="${base}/tell" |
| 280 | 280 | > |
| 281 | <input type="hidden" name="from" value="${ctx.url.pathname}" /> | |
| 281 | 282 | <label class="sr-only" for="tell">Tell the crew</label> |
| 282 | 283 | <textarea |
| 283 | 284 | class="input" |
src/web/studio/feed.ts +4 −2
| @@ -82,6 +82,7 @@ export function feedCard( | ||
| 82 | 82 | <summary class="small muted" aria-label="${target ? "Steer, dismiss or mute" : "Tell the crew, dismiss or mute"}">${target ? "Steer" : "Tell"} · more</summary> |
| 83 | 83 | <form method="post" action="${target ?? `/${item.app}/tell`}" class="fcard-form"> |
| 84 | 84 | <textarea class="input" name="text" rows="2" maxlength="2000" required aria-label="${target ? "Steer this run" : "Tell the crew"}"></textarea> |
| 85 | ${target ? null : html`<input type="hidden" name="from" value="/" />`} | |
| 85 | 86 | ${btn(target ? "Steer" : "Tell", { size: "sm", variant: "primary" })} |
| 86 | 87 | </form> |
| 87 | 88 | <span class="fcard-acts"> |
| @@ -132,6 +133,7 @@ export function composer(ctx: Ctx, user: User): Html { | ||
| 132 | 133 | const first = slug(repos[0]!); |
| 133 | 134 | return html`<form id="composer" class="composer" method="post" action="/tell"> |
| 134 | 135 | <textarea class="input" name="text" rows="2" maxlength="2000" required placeholder="Tell the crew what should change" aria-label="Tell the crew">${sharedDraft(ctx.url.searchParams)}</textarea> |
| 136 | <input type="hidden" name="from" value="/" /> | |
| 135 | 137 | <div class="composer-bar"> |
| 136 | 138 | ${repos.length === 1 |
| 137 | 139 | ? html`<input type="hidden" name="app" value="${first}" /><span class="muted small">${first}</span>` |
| @@ -264,7 +266,7 @@ ${outboxJs} | ||
| 264 | 266 | if (!card || !box || !/\\/tell$/.test(form.getAttribute('action') || '')) return; |
| 265 | 267 | var text = box.value.trim(); |
| 266 | 268 | if (!text) return; |
| 267 | if (!queue('/api/v1/feed/tells', { app: card.getAttribute('data-app'), text: text })) return; | |
| 269 | if (!queue('/api/v1/feed/tells', { app: card.getAttribute('data-app'), text: text, from: location.pathname + location.search })) return; | |
| 268 | 270 | e.preventDefault(); |
| 269 | 271 | box.value = ''; |
| 270 | 272 | var d = form.closest('details'); |
| @@ -278,7 +280,7 @@ ${outboxJs} | ||
| 278 | 280 | var app = composer.elements.app; |
| 279 | 281 | var text = box ? box.value.trim() : ''; |
| 280 | 282 | if (!text || !app) return; |
| 281 | if (!queue('/api/v1/feed/tells', { app: app.value, text: text })) return; | |
| 283 | if (!queue('/api/v1/feed/tells', { app: app.value, text: text, from: location.pathname + location.search })) return; | |
| 282 | 284 | e.preventDefault(); |
| 283 | 285 | box.value = ''; |
| 284 | 286 | box.focus(); |
test/tell-from.test.ts added +173
| @@ -0,0 +1,173 @@ | ||
| 1 | /** | |
| 2 | * A tell carries the page it was told from: a same-forge path, validated at | |
| 3 | * the boundary, said on the issue it opens and escaped there, through the | |
| 4 | * action, the JSON API and the MCP `tell` tool. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import { tell, fromPath, FROM_MAX } from "../src/actions/tell.ts"; | |
| 12 | import { setIssueState } from "../src/actions/issues.ts"; | |
| 13 | import { createRepo } from "../src/actions/repos.ts"; | |
| 14 | import { getIssue, listIssues } from "../src/core/issues.ts"; | |
| 15 | import type { Repo } from "../src/core/repos.ts"; | |
| 16 | import { createSession } from "../src/core/sessions.ts"; | |
| 17 | import { createToken } from "../src/core/tokens.ts"; | |
| 18 | import { createUser, type User } from "../src/core/users.ts"; | |
| 19 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 20 | ||
| 21 | describe("fromPath", () => { | |
| 22 | test("a path on this forge is kept; nothing is none", () => { | |
| 23 | for (const p of ["/", "/alice/notes/issues/3", "/a/b?x=1&y=2#top"]) | |
| 24 | expect(fromPath(p)).toEqual({ ok: true, value: p }); | |
| 25 | for (const none of [undefined, null, "", " "]) | |
| 26 | expect(fromPath(none)).toEqual({ ok: true, value: undefined }); | |
| 27 | }); | |
| 28 | test("anything that could leave the origin or break the page is refused", () => { | |
| 29 | for (const bad of [ | |
| 30 | "https://evil.example/x", | |
| 31 | "//evil.example", | |
| 32 | "/\\evil.example", | |
| 33 | "javascript:alert(1)", | |
| 34 | "alice/notes", | |
| 35 | "/a b", | |
| 36 | "/a\nb", | |
| 37 | '/a"onmouseover="x', | |
| 38 | "/<script>", | |
| 39 | "/a](javascript:x)", | |
| 40 | "/a`b", | |
| 41 | `/${"a".repeat(FROM_MAX)}`, | |
| 42 | 42, | |
| 43 | {}, | |
| 44 | ]) | |
| 45 | expect(fromPath(bad)).toEqual({ ok: false, error: "invalid_from" }); | |
| 46 | }); | |
| 47 | }); | |
| 48 | ||
| 49 | describe("tell from a page", () => { | |
| 50 | let dataDir: string; | |
| 51 | let app: App; | |
| 52 | let servers: Servers; | |
| 53 | let alice: User; | |
| 54 | let repo: Repo; | |
| 55 | let cookie: string; | |
| 56 | const opened: number[] = []; | |
| 57 | ||
| 58 | const api = (body: unknown) => | |
| 59 | fetch(`${servers.url}/api/v1/feed/tells`, { | |
| 60 | method: "POST", | |
| 61 | headers: { cookie, origin: servers.url, "content-type": "application/json" }, | |
| 62 | body: JSON.stringify(body), | |
| 63 | }); | |
| 64 | ||
| 65 | beforeAll(async () => { | |
| 66 | dataDir = await mkdtemp(join(tmpdir(), "forge-tellfrom-")); | |
| 67 | app = createApp({ dataDir, port: 0 }); | |
| 68 | servers = await serve(app); | |
| 69 | const u = await createUser(app.db, { | |
| 70 | trusted: true, | |
| 71 | username: "alice", | |
| 72 | email: "[email protected]", | |
| 73 | password: "a-good-passphrase", | |
| 74 | adminIfFirst: true, | |
| 75 | }); | |
| 76 | if (!u.ok) throw new Error(u.error); | |
| 77 | alice = u.value; | |
| 78 | cookie = `forge_session=${createSession(app.db, alice.id).token}`; | |
| 79 | const made = await createRepo(app, alice, { name: "notes", visibility: "public" }); | |
| 80 | if (!made.ok) throw new Error("repo"); | |
| 81 | repo = made.value; | |
| 82 | }); | |
| 83 | afterAll(async () => { | |
| 84 | // A tell left open would show in the studio's Now panel. | |
| 85 | for (const n of opened) | |
| 86 | setIssueState(app, alice, repo, getIssue(app.db, repo.id, n)!, "closed"); | |
| 87 | servers.stop(); | |
| 88 | app.close(); | |
| 89 | await rm(dataDir, { recursive: true, force: true }); | |
| 90 | }); | |
| 91 | ||
| 92 | test("the issue says where it was told from, as a link", async () => { | |
| 93 | const told = await tell(app, alice, repo, "Make it purple\nand quicker", "/alice/notes/issues/1"); | |
| 94 | if (!told.ok) throw new Error(told.error); | |
| 95 | opened.push(told.value.issue.number); | |
| 96 | expect(told.value.issue.body).toBe( | |
| 97 | "Make it purple\nand quicker\n\ntold from [/alice/notes/issues/1](/alice/notes/issues/1)", | |
| 98 | ); | |
| 99 | const page = await ( | |
| 100 | await fetch(`${servers.url}/alice/notes/issues/${told.value.issue.number}`, { headers: { cookie } }) | |
| 101 | ).text(); | |
| 102 | expect(page).toContain('href="/alice/notes/issues/1"'); | |
| 103 | }); | |
| 104 | ||
| 105 | test("a single-line tell with a from has the from as its whole body; without one, none", async () => { | |
| 106 | const a = await tell(app, alice, repo, "One line", "/x"); | |
| 107 | const b = await tell(app, alice, repo, "Other line"); | |
| 108 | if (!a.ok || !b.ok) throw new Error("tell"); | |
| 109 | opened.push(a.value.issue.number, b.value.issue.number); | |
| 110 | expect(a.value.issue.body).toBe("told from [/x](/x)"); | |
| 111 | expect(b.value.issue.body).toBe(""); | |
| 112 | }); | |
| 113 | ||
| 114 | test("the studio's tell form records its page; a bad one is dropped, the tell still goes", async () => { | |
| 115 | const post = (from: string, text: string) => | |
| 116 | fetch(`${servers.url}/alice/notes/tell`, { | |
| 117 | method: "POST", | |
| 118 | redirect: "manual", | |
| 119 | headers: { cookie, origin: servers.url, "content-type": "application/x-www-form-urlencoded" }, | |
| 120 | body: new URLSearchParams({ text, from }), | |
| 121 | }); | |
| 122 | const before = new Set(listIssues(app.db, repo.id, {}).map((i) => i.number)); | |
| 123 | await post("/alice/notes", "from the page"); | |
| 124 | await post("https://evil.example", "from nowhere"); | |
| 125 | const fresh = listIssues(app.db, repo.id, {}).filter((i) => !before.has(i.number)); | |
| 126 | for (const i of fresh) opened.push(i.number); | |
| 127 | expect(fresh.find((i) => i.title === "from the page")!.body).toBe("told from [/alice/notes](/alice/notes)"); | |
| 128 | expect(fresh.find((i) => i.title === "from nowhere")!.body).toBe(""); | |
| 129 | }); | |
| 130 | ||
| 131 | test("a bad from opens nothing", async () => { | |
| 132 | const r = await tell(app, alice, repo, "Nope", "https://evil.example"); | |
| 133 | expect(r).toEqual({ ok: false, error: "invalid_from" }); | |
| 134 | }); | |
| 135 | ||
| 136 | test("the JSON API passes from on and refuses a bad one in a sentence", async () => { | |
| 137 | const good = await api({ app: "alice/notes", text: "via api", from: "/alice/notes" }); | |
| 138 | expect(good.status).toBe(201); | |
| 139 | const n = ((await good.json()) as { issue: number }).issue; | |
| 140 | opened.push(n); | |
| 141 | expect(getIssue(app.db, repo.id, n)!.body).toContain("told from [/alice/notes]"); | |
| 142 | const bad = await api({ app: "alice/notes", text: "via api", from: "//evil.example" }); | |
| 143 | expect(bad.status).toBe(422); | |
| 144 | const j = (await bad.json()) as { error: { code?: string; message?: string } | string }; | |
| 145 | expect(JSON.stringify(j)).toContain("path on this forge"); | |
| 146 | }); | |
| 147 | ||
| 148 | test("the MCP tell tool accepts from and refuses a bad one", async () => { | |
| 149 | const call = async (args: Record<string, unknown>) => { | |
| 150 | const res = await fetch(`${servers.url}/mcp`, { | |
| 151 | method: "POST", | |
| 152 | headers: { | |
| 153 | authorization: `Bearer ${createToken(app.db, alice.id, "rw", "rw").token}`, | |
| 154 | "content-type": "application/json", | |
| 155 | accept: "application/json, text/event-stream", | |
| 156 | }, | |
| 157 | body: JSON.stringify({ | |
| 158 | jsonrpc: "2.0", | |
| 159 | id: 1, | |
| 160 | method: "tools/call", | |
| 161 | params: { name: "tell", arguments: args }, | |
| 162 | }), | |
| 163 | }); | |
| 164 | return res.text(); | |
| 165 | }; | |
| 166 | const good = await call({ repo: "alice/notes", text: "via mcp", from: "/alice/notes/tells" }); | |
| 167 | const m = /issue\\?":(\d+)/.exec(good); | |
| 168 | if (m) opened.push(Number(m[1])); | |
| 169 | expect(good).toContain("issue"); | |
| 170 | const bad = await call({ repo: "alice/notes", text: "via mcp", from: "http://evil.example" }); | |
| 171 | expect(bad).toContain("path on this forge"); | |
| 172 | }); | |
| 173 | }); |
new above
offline