Public: anyone can read this; the crew can change it.
A talk answer's cited issue and pull numbers link to their pages, and only to pages the viewer may read #13438
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13405-a-talk-answer-s-cited-issue-and-pull-num → main 7 commits fork 1 thread · 1 unresolved
11 files
changed +247
−25
docs/features/README.md +1
| @@ -142,6 +142,7 @@ Read with the guide page [Tell the crew](../guide-tell-the-crew.md). | ||
| 142 | 142 | - [](talk-attachments.md) |
| 143 | 143 | - [](talk-captain.md) |
| 144 | 144 | - [](talk.md) |
| 145 | - [](talk-links.md) | |
| 145 | 146 | - [](share-tell.md) |
| 146 | 147 | - [](crew-line.md) |
| 147 | 148 | - [](crew-list.md) |
docs/features/talk-links.md added +10
| @@ -0,0 +1,10 @@ | ||
| 1 | # Talk answers link their cited numbers | |
| 2 | ||
| 3 | When the forge's agent answers with `#12`, `issue 12` or `pull 12`, the number leads to that issue or pull in the repository the conversation is working on. A number you may not read, or one that does not exist there, stays plain text. | |
| 4 | ||
| 5 | - **On /talk** the number is a link, to `/owner/name/issues/N` or `/owner/name/pulls/N` by what it names. Text in code spans, code blocks and existing links is left alone. | |
| 6 | - **In the terminal** (Conversations, open one) a link cannot be clicked, so a readable number is followed by its path: `#12 (/owner/name/issues/12)`. An unreadable number prints nothing extra. | |
| 7 | ||
| 8 | Both doors decide through `refTarget` in `src/text/talk_links.ts`, so what one shows the other shows. The repository is the one the chat's latest tool call named; with none, nothing is linked. | |
| 9 | ||
| 10 | Not covered: the one-line `ssh <forge> talk <words>` answer prints as before. |
src/actions/runs.ts +17 −9
| @@ -1,11 +1,10 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * Run actions: look at a repository's runs and their logs, cancel one, |
| 3 | * queue a finished agent run's task again, and — for the workers that produce runs — announce a run's state on the | |
| 4 | * outbox. Reading follows the repository's visibility (a run's log is as | |
| 5 | * private as the code it ran), and a private app's runs follow the app: a | |
| 6 | * deploy log carries the app's own output, so only someone who may enter | |
| 7 | * the app reads them (#620). Cancelling takes write access; announcing is | |
| 8 | * the system's own voice and takes no actor. | |
| 3 | * queue a finished agent run's task again, and — for the workers that produce runs — announce a | |
| 4 | * run's state on the outbox. Reading follows the repository's visibility (a run's log is as private | |
| 5 | * as the code it ran), and a private app's runs follow the app: a deploy log carries the app's own | |
| 6 | * output, so only someone who may enter the app reads them (#620). Cancelling takes write access; | |
| 7 | * announcing is the system's own voice and takes no actor. | |
| 9 | 8 | */ |
| 10 | 9 | |
| 11 | 10 | import { commitVerdicts, isCommitId, releaseCheck, type CommitCheck } from "../checks/commit_check.ts"; |
| @@ -268,12 +267,18 @@ export async function checkOfCommit( | ||
| 268 | 267 | ); |
| 269 | 268 | } |
| 270 | 269 | |
| 271 | /** One look of a check as the API words it: how many screens tall, and whether a phone look scrolls sideways. */ | |
| 270 | /** | |
| 271 | * One look of a check as the API words it: how many screens tall, and whether a phone look scrolls | |
| 272 | * sideways. | |
| 273 | */ | |
| 272 | 274 | export interface LookFigures { |
| 273 | 275 | readonly look: string; |
| 274 | 276 | readonly screens: string; |
| 275 | 277 | readonly sideways: boolean; |
| 276 | /** The window and page as measured (`scrollWidth` vs `innerWidth` says whether it fits); absent when none was recorded. */ | |
| 278 | /** | |
| 279 | * The window and page as measured (`scrollWidth` vs `innerWidth` says whether it fits); absent | |
| 280 | * when none was recorded. | |
| 281 | */ | |
| 277 | 282 | readonly size?: PageSize; |
| 278 | 283 | } |
| 279 | 284 | |
| @@ -297,7 +302,10 @@ export function looksOfCheck( | ||
| 297 | 302 | })); |
| 298 | 303 | } |
| 299 | 304 | |
| 300 | /** The own green or red verdicts of a page of commits, for whoever may read the runs; empty otherwise. */ | |
| 305 | /** | |
| 306 | * The own green or red verdicts of a page of commits, for whoever may read the runs; empty | |
| 307 | * otherwise. | |
| 308 | */ | |
| 301 | 309 | export function verdictsOfCommits( |
| 302 | 310 | app: App, |
| 303 | 311 | actor: User | undefined, |
src/actions/talk.ts +4 −1
| @@ -68,7 +68,10 @@ export function talk(app: App, user: User | undefined, text: string, files: read | ||
| 68 | 68 | return said; |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | /** Edit the last message and resend it, regenerate the last reply, or retry a failed turn (#8817), in the named conversation or the latest. */ | |
| 71 | /** | |
| 72 | * Edit the last message and resend it, regenerate the last reply, or retry a failed turn (#8817), | |
| 73 | * in the named conversation or the latest. | |
| 74 | */ | |
| 72 | 75 | export function talkAgain(app: App, user: User | undefined, how: RedoHow, text = "", conversationId?: string): Result<Said, TalkRefusal> { |
| 73 | 76 | if (user === undefined) return err("unauthorized"); |
| 74 | 77 | const options = conversationOptions(app); |
src/core/conversation_markdown.ts +4 −3
| @@ -2,7 +2,8 @@ | ||
| 2 | 2 | * A talk conversation as one Markdown document (#13307): who, when, the text, |
| 3 | 3 | * and each attachment as a link, turns in the order they were written. The |
| 4 | 4 | * download route, the MCP `say` tool (`action: download`) and the terminal's talk screen all |
| 5 | * return this text; each turn's words sit in a fenced block, and only the conversation's owner is ever served it. | |
| 5 | * return this text; each turn's words sit in a fenced block, and only the conversation's owner is | |
| 6 | * ever served it. | |
| 6 | 7 | */ |
| 7 | 8 | import type { Database } from "bun:sqlite"; |
| 8 | 9 | import { err, ok, type Result } from "../kernel/result.ts"; |
| @@ -18,8 +19,8 @@ const plain = (s: string): string => s.replace(/[\r\n]+/g, " ").replace(/[[\]()< | ||
| 18 | 19 | export const MAX_TEXT = 1_000_000; |
| 19 | 20 | |
| 20 | 21 | /** |
| 21 | * The newest turns that still count (an edit's replaced ones are left out), oldest first, read a page | |
| 22 | * at a time until `MAX_TEXT` is reached. `cut` is true when older turns were left unread. | |
| 22 | * The newest turns that still count (an edit's replaced ones are left out), oldest first, read a | |
| 23 | * page at a time until `MAX_TEXT` is reached. `cut` is true when older turns were left unread. | |
| 23 | 24 | */ |
| 24 | 25 | function recentTurns(db: Database, userId: string, conversationId: string): { turns: Turn[]; cut: boolean } { |
| 25 | 26 | const out: Turn[] = []; |
src/text/talk_links.ts added +58
| @@ -0,0 +1,58 @@ | ||
| 1 | /** | |
| 2 | * A talk answer's "#12", "pull 12" and "issue #12" as links (#13405). It works on HTML the | |
| 3 | * markdown renderer already escaped: only text outside tags, links, code and pre is touched, and | |
| 4 | * the only thing put in is a digit run inside a fixed path, so no string of the answer becomes | |
| 5 | * markup. `href` says where a number goes, or `undefined` when the viewer may not read it, and | |
| 6 | * then the words stay plain text. | |
| 7 | */ | |
| 8 | import type { Database } from "bun:sqlite"; | |
| 9 | import { getIssue } from "../core/issues.ts"; | |
| 10 | import { canRead, getRepo } from "../core/repos.ts"; | |
| 11 | import type { User } from "../core/users.ts"; | |
| 12 | import { Html, raw } from "../web/html.ts"; | |
| 13 | ||
| 14 | const REF = /(?<![\w&#/])(?:(?:pulls?|issues?)\s+|#)(\d{1,7})(?![\w;])/gi; | |
| 15 | const SKIP = new Set(["a", "code", "pre"]); | |
| 16 | ||
| 17 | /** Link the cited numbers in escaped `rendered`; `href(n)` is where number n lives, if readable. */ | |
| 18 | export function linkRefs(rendered: Html, href: (n: number) => string | undefined): Html { | |
| 19 | const parts = rendered.text.split(/(<[^>]*>)/); | |
| 20 | let skipping = 0; | |
| 21 | for (let i = 0; i < parts.length; i += 1) { | |
| 22 | const part = parts[i]!; | |
| 23 | if (part.startsWith("<")) { | |
| 24 | const tag = /^<(\/?)([a-z0-9]+)/i.exec(part); | |
| 25 | if (tag !== null && SKIP.has(tag[2]!.toLowerCase())) skipping = Math.max(0, skipping + (tag[1] === "/" ? -1 : 1)); | |
| 26 | continue; | |
| 27 | } | |
| 28 | if (skipping > 0 || part === "") continue; | |
| 29 | parts[i] = part.replace(REF, (whole, digits: string) => { | |
| 30 | const to = href(Number(digits)); | |
| 31 | return to === undefined ? whole : `<a href="${to}">${whole}</a>`; | |
| 32 | }); | |
| 33 | } | |
| 34 | return raw(parts.join("")); | |
| 35 | } | |
| 36 | ||
| 37 | /** | |
| 38 | * The same references in plain text, for a screen with nothing to click: a readable number is | |
| 39 | * followed by its path, an unreadable one is left as it was (#13405). | |
| 40 | */ | |
| 41 | export function pathRefs(text: string, href: (n: number) => string | undefined): string { | |
| 42 | return text.replace(REF, (whole, digits: string) => { | |
| 43 | const to = href(Number(digits)); | |
| 44 | return to === undefined ? whole : `${whole} (${to})`; | |
| 45 | }); | |
| 46 | } | |
| 47 | ||
| 48 | /** Where `#n` goes in `repo` ("owner/name") for `viewer`: a page they may read, else nothing. */ | |
| 49 | export function refTarget(db: Database, viewer: User | undefined, repo: string | undefined): (n: number) => string | undefined { | |
| 50 | if (repo === undefined) return () => undefined; | |
| 51 | const [owner, name] = repo.split("/"); | |
| 52 | const found = owner === undefined || name === undefined ? undefined : getRepo(db, owner, name); | |
| 53 | if (found === undefined || !canRead(db, found, viewer)) return () => undefined; | |
| 54 | return (n) => { | |
| 55 | const issue = getIssue(db, found.id, n); | |
| 56 | return issue === undefined ? undefined : `/${found.ownerName}/${found.name}/${issue.kind === "pull" ? "pulls" : "issues"}/${String(n)}`; | |
| 57 | }; | |
| 58 | } |
src/tui/screens/talk_chats.ts +8 −2
| @@ -9,6 +9,7 @@ | ||
| 9 | 9 | * sends that one through the same `talk()`. |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | import { pathRefs, refTarget } from "../../text/talk_links.ts"; | |
| 12 | 13 | import { STARTER_PROMPTS, TALK_WORDS, talk } from "../../actions/talk.ts"; |
| 13 | 14 | import { |
| 14 | 15 | archiveConversation, |
| @@ -39,7 +40,10 @@ const BAD_TITLE = `A title is up to ${TITLE_MAX} characters, on one line.`; | ||
| 39 | 40 | |
| 40 | 41 | const WHO: Readonly<Record<string, string>> = { person: "You", agent: "Forge", tool: "Tool", confirm: "Asks" }; |
| 41 | 42 | |
| 42 | /** The conversation as the Markdown download holds it, to read and scroll; the file itself is the web's /talk/:id/download. */ | |
| 43 | /** | |
| 44 | * The conversation as the Markdown download holds it, to read and scroll; the file itself is the | |
| 45 | * web's /talk/:id/download. | |
| 46 | */ | |
| 43 | 47 | function downloadScreen(f: Session, ctx: Ctx, chat: Conversation): Screen { |
| 44 | 48 | const scroll = new Scroll(); |
| 45 | 49 | const made = f.user === undefined ? undefined : conversationMarkdown(f.app.db, f.user.id, chat.id, publicOrigin(f.app.config.url, `http://localhost:${f.app.config.port}`)); |
| @@ -119,11 +123,13 @@ function chatScreen(f: Session, ctx: Ctx, chat: Conversation, hit?: string): Scr | ||
| 119 | 123 | [{ text: "Nothing said yet; s says something, or a number sends one of these:", style: style(c.mutedFg, c.bg) }], |
| 120 | 124 | ...STARTER_PROMPTS.map((p, i) => [{ text: ` ${String(i + 1)} ${p}`, style: style(c.fg, c.bg) }]), |
| 121 | 125 | ]; |
| 126 | const refs = refTarget(f.app.db, f.user, repo); | |
| 122 | 127 | let n = 0; |
| 123 | 128 | return turns.flatMap((t) => { |
| 124 | 129 | if (t.id === around) mark = n; |
| 125 | 130 | const step = t.role === "tool" ? stepOf(t.text) : undefined; |
| 126 | const own = (step === undefined ? t.text : [step.label, ...step.details.map((d) => ` ${d}`)].join("\n")).split("\n").flatMap((l) => wrap(l, Math.max(10, width - 2))); | |
| 131 | const said = t.role === "agent" ? pathRefs(t.text, refs) : t.text; | |
| 132 | const own = (step === undefined ? said : [step.label, ...step.details.map((d) => ` ${d}`)].join("\n")).split("\n").flatMap((l) => wrap(l, Math.max(10, width - 2))); | |
| 127 | 133 | n += own.length + 2; |
| 128 | 134 | return [ |
| 129 | 135 | [{ text: `${t.id === around ? "▶ " : ""}${WHO[t.role] ?? t.role}${t.replaced ? " · replaced" : ""} · ${ago(t.createdAt)}`, style: style(c.mutedFg, c.bg) }], |
src/web/pages/talk.ts +14 −5
| @@ -56,6 +56,7 @@ import { signInFirst, forbiddenPage, notFoundPage } from "../chrome.ts"; | ||
| 56 | 56 | import { readForm } from "../form.ts"; |
| 57 | 57 | import { html, raw, type Html } from "../html.ts"; |
| 58 | 58 | import { flashTo, page, redirect } from "../layout.ts"; |
| 59 | import { linkRefs, refTarget } from "../../text/talk_links.ts"; | |
| 59 | 60 | import { renderMarkdown } from "../markdown.ts"; |
| 60 | 61 | import { badge, btn, row, time } from "../ui.ts"; |
| 61 | 62 | import { buildBox, buildCss, chosenTemplate, panelFor } from "../talk_build.ts"; |
| @@ -215,7 +216,7 @@ const totalLine = (usd: number | undefined): Html => | ||
| 215 | 216 | const costLine = (usd: number): Html => html`<p class="talk-cost small muted">${dollars(usd)} this turn</p>`; |
| 216 | 217 | |
| 217 | 218 | /** One turn as the page and the stream both draw it; `waiting` is the confirms still open. */ |
| 218 | export function turnHtml(turn: Turn, waiting: ReadonlySet<string>, cost?: number, act?: Html, work?: Work, files: readonly ConversationFile[] = [], expired: ReadonlySet<string> = new Set(), undone: ReadonlySet<string> = new Set()): Html { | |
| 219 | export function turnHtml(turn: Turn, waiting: ReadonlySet<string>, cost?: number, act?: Html, work?: Work, files: readonly ConversationFile[] = [], expired: ReadonlySet<string> = new Set(), undone: ReadonlySet<string> = new Set(), refs?: (n: number) => string | undefined): Html { | |
| 219 | 220 | const id = `t-${turn.id}`; |
| 220 | 221 | const set = turn.replaced ? " talk-replaced" : ""; |
| 221 | 222 | const acts = act === undefined ? null : html`<div class="talk-acts">${act}</div>`; |
| @@ -233,7 +234,7 @@ export function turnHtml(turn: Turn, waiting: ReadonlySet<string>, cost?: number | ||
| 233 | 234 | </li>`; |
| 234 | 235 | if (turn.role === "agent") |
| 235 | 236 | return html`<li class="talk-turn talk-agent${set}" id="${id}"> |
| 236 | <div class="talk-body prose">${renderMarkdown(turn.text)}</div> | |
| 237 | <div class="talk-body prose">${refs === undefined ? renderMarkdown(turn.text) : linkRefs(renderMarkdown(turn.text), refs)}</div> | |
| 237 | 238 | ${work === undefined ? null : workHtml(work)} |
| 238 | 239 | ${label("Forge")} |
| 239 | 240 | ${cost === undefined ? null : costLine(cost)}${acts} |
| @@ -372,7 +373,9 @@ async function talkPage(ctx: Ctx, conversation: Conversation, before0: string | | ||
| 372 | 373 | const asked = ctx.url.searchParams.get("pane"); |
| 373 | 374 | const view: View | undefined = VIEWS.find((p) => p === asked); |
| 374 | 375 | const shown = view ?? "chat"; |
| 375 | const pane = talkPane(ctx, user, repoOf(turns), works, view); | |
| 376 | const cited = repoOf(turns); | |
| 377 | const refs = refTarget(ctx.app.db, user, cited); | |
| 378 | const pane = talkPane(ctx, user, cited, works, view); | |
| 376 | 379 | const body = html`<h1 class="sr-only">Talk</h1> |
| 377 | 380 | ${box} |
| 378 | 381 | <div class="talk-shell" data-shell data-pane="${shown}"> |
| @@ -390,7 +393,7 @@ async function talkPage(ctx: Ctx, conversation: Conversation, before0: string | | ||
| 390 | 393 | ${turns.length === 0 |
| 391 | 394 | ? html`<li class="muted talk-turn">Ask the forge anything, or tell it what to change. It acts as you, with your permissions, and asks before anything hard to undo. |
| 392 | 395 | <div class="talk-suggest">${STARTER_PROMPTS.map((q) => html`<form method="post" action="${here}" data-starter><input type="hidden" name="text" value="${q}" />${btn(q)}</form>`)}</div></li>` |
| 393 | : turns.map((t) => turnHtml(t, waiting, costs.get(t.id), acts.get(t.id), works.get(t.id), sent.get(t.id), expired, undone))} | |
| 396 | : turns.map((t) => turnHtml(t, waiting, costs.get(t.id), acts.get(t.id), works.get(t.id), sent.get(t.id), expired, undone, refs))} | |
| 394 | 397 | ${!paged && running ? queuedTalk(ctx.app, user, conversation.id).map(queuedHtml) : null} |
| 395 | 398 | </ol> |
| 396 | 399 | <div id="end"></div> |
| @@ -608,6 +611,9 @@ export function talkStream(ctx: Ctx): Response { | ||
| 608 | 611 | const started = Date.now(); |
| 609 | 612 | let told = false; |
| 610 | 613 | let shown = ""; |
| 614 | // The repository the chat works on, kept across batches instead of re-reading the turns. | |
| 615 | const before = listTurns(app.db, user.id, conversation.id, { limit: DEFAULT_PAGE }); | |
| 616 | let cited: string | undefined = before.ok ? repoOf(before.value) : undefined; | |
| 611 | 617 | const tick = (): void => { |
| 612 | 618 | // Asked before the read: a turn that ends after this look is read whole next time. |
| 613 | 619 | const running = clock.running(app, user.id, conversation.id); |
| @@ -618,8 +624,11 @@ export function talkStream(ctx: Ctx): Response { | ||
| 618 | 624 | const undone = undoneIn(fresh); |
| 619 | 625 | const works = worksOf(app.db, user.id, conversation.id, fresh); |
| 620 | 626 | const sent = filesOfTurns(app.db, user.id, fresh.filter((t) => t.role === "person").map((t) => t.id)); |
| 627 | const named = repoOf(fresh); | |
| 628 | if (named !== undefined) cited = named; | |
| 629 | const refs = refTarget(app.db, user, cited); | |
| 621 | 630 | for (const turn of fresh) { |
| 622 | emit("turn", { id: turn.id, role: turn.role, html: turnHtml(turn, waiting, undefined, undefined, works.get(turn.id), sent.get(turn.id), expired, undone).text }, turn.id); | |
| 631 | emit("turn", { id: turn.id, role: turn.role, html: turnHtml(turn, waiting, undefined, undefined, works.get(turn.id), sent.get(turn.id), expired, undone, refs).text }, turn.id); | |
| 623 | 632 | cursor = turn.id; |
| 624 | 633 | } |
| 625 | 634 | } |
test/no-polling.test.ts +19 −5
| @@ -36,10 +36,15 @@ const NO_TIMER_PAGES = ["/system", "/flow"]; | ||
| 36 | 36 | * that only the owner's own submit changes; a pushed patch would move a handle under a drag. */ |
| 37 | 37 | const STREAM_PAGES = ["/system"]; |
| 38 | 38 | |
| 39 | /** A page as the browser gets it: its markup, and every script it runs (fetched files and inline bodies), apart. */ | |
| 39 | /** | |
| 40 | * A page as the browser gets it: its markup, and every script it runs (fetched files and inline | |
| 41 | * bodies), apart. | |
| 42 | */ | |
| 40 | 43 | export interface RenderedPage { html: string; scripts: string[] } |
| 41 | 44 | |
| 42 | /** A stream a script opens. `data-live` alone, never `data-live-every`, the beat this guard forbids. */ | |
| 45 | /** | |
| 46 | * A stream a script opens. `data-live` alone, never `data-live-every`, the beat this guard forbids. | |
| 47 | */ | |
| 43 | 48 | const STREAM = /EventSource\s*\(|data-live(?![-\w])/; |
| 44 | 49 | |
| 45 | 50 | /** Script types that hold data, not code. */ |
| @@ -230,7 +235,10 @@ describe("no rendered page polls", () => { | ||
| 230 | 235 | * reads each page's markup and the scripts it loads. |
| 231 | 236 | */ |
| 232 | 237 | |
| 233 | /** The text of the balanced `(…)` or `{…}` whose opener is at `open`. Strings and comments are not understood: a bracket inside one skews it. */ | |
| 238 | /** | |
| 239 | * The text of the balanced `(…)` or `{…}` whose opener is at `open`. Strings and comments are not | |
| 240 | * understood: a bracket inside one skews it. | |
| 241 | */ | |
| 234 | 242 | function balanced(src: string, open: number): string { |
| 235 | 243 | const pairs: Record<string, string> = { "(": ")", "{": "}" }; |
| 236 | 244 | const o = src[open]!, c = pairs[o]!; |
| @@ -242,7 +250,10 @@ function balanced(src: string, open: number): string { | ||
| 242 | 250 | return src.slice(open + 1); |
| 243 | 251 | } |
| 244 | 252 | |
| 245 | /** The body of a function a script names: `function name(…) {…}` or `name = (…) => {…}` / `name = function (…) {…}`. */ | |
| 253 | /** | |
| 254 | * The body of a function a script names: `function name(…) {…}` or `name = (…) => {…}` / | |
| 255 | * `name = function (…) {…}`. | |
| 256 | */ | |
| 246 | 257 | function bodyOf(src: string, name: string): string | undefined { |
| 247 | 258 | const m = new RegExp(`(?:function\\s+${name}\\s*\\(|\\b${name}\\s*=\\s*(?:async\\s*)?(?:function\\s*\\(|\\([^)]*\\)\\s*=>|\\w+\\s*=>))`).exec(src); |
| 248 | 259 | if (!m) return undefined; |
| @@ -413,7 +424,10 @@ describe("every page the router lists", () => { | ||
| 413 | 424 | }); |
| 414 | 425 | }); |
| 415 | 426 | |
| 416 | /** Fewest HTML pages the admin walk reaches; observed when written. A page lost from the walk drops under it. */ | |
| 427 | /** | |
| 428 | * Fewest HTML pages the admin walk reaches; observed when written. A page lost from the walk drops | |
| 429 | * under it. | |
| 430 | */ | |
| 417 | 431 | const WALKED_FLOOR = 130; |
| 418 | 432 | /** The same, signed out (observed 44). */ |
| 419 | 433 | const ANON_FLOOR = 40; |
test/talk-links.test.ts added +95
| @@ -0,0 +1,95 @@ | ||
| 1 | /** A talk answer's cited numbers link to pages the viewer may read, and only those (#13405). */ | |
| 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, type App } from "../src/app.ts"; | |
| 7 | import { createRepo } from "../src/actions/repos.ts"; | |
| 8 | import { openIssue } from "../src/actions/issues.ts"; | |
| 9 | import { createIssue } from "../src/core/issues.ts"; | |
| 10 | import { linkRefs, pathRefs, refTarget } from "../src/text/talk_links.ts"; | |
| 11 | import { renderMarkdown } from "../src/web/markdown.ts"; | |
| 12 | import { createUser, type User } from "../src/core/users.ts"; | |
| 13 | import { promote } from "./helpers/members.ts"; | |
| 14 | ||
| 15 | let dir: string; | |
| 16 | let app: App; | |
| 17 | let alice: User; | |
| 18 | let bob: User; | |
| 19 | ||
| 20 | beforeAll(async () => { | |
| 21 | dir = await mkdtemp(join(tmpdir(), "forge-talk-links-")); | |
| 22 | app = createApp({ dataDir: dir, port: 0 }); | |
| 23 | const mk = async (username: string, first = false) => { | |
| 24 | const u = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst: first }); | |
| 25 | if (!u.ok) throw new Error(u.error); | |
| 26 | promote(app.db, u.value); | |
| 27 | return u.value; | |
| 28 | }; | |
| 29 | alice = await mk("alice", true); | |
| 30 | bob = await mk("bob"); | |
| 31 | }, 30_000); | |
| 32 | afterAll(async () => { | |
| 33 | app.close(); | |
| 34 | await rm(dir, { recursive: true, force: true }); | |
| 35 | }); | |
| 36 | ||
| 37 | const draw = (text: string, to: (n: number) => string | undefined) => linkRefs(renderMarkdown(text), to).text; | |
| 38 | ||
| 39 | test("readable: #N, issue N and pull N link to the same forge; a missing number stays text", async () => { | |
| 40 | const made = await createRepo(app, alice, { name: "pub-links", visibility: "public" }); | |
| 41 | if (!made.ok) throw new Error(String(made.error)); | |
| 42 | const issue = openIssue(app, alice, made.value, { title: "t", body: "x" }); | |
| 43 | if (!issue.ok) throw new Error(String(issue.error)); | |
| 44 | const n = issue.value.number; | |
| 45 | const to = refTarget(app.db, bob, "alice/pub-links"); | |
| 46 | const out = draw(`see #${n}, issue ${n} and \`#${n}\` but not #999`, to); | |
| 47 | expect(out).toContain(`<a href="/alice/pub-links/issues/${n}">#${n}</a>`); | |
| 48 | expect(out).toContain(`<a href="/alice/pub-links/issues/${n}">issue ${n}</a>`); | |
| 49 | expect(out).toContain(`<code>#${n}</code>`); | |
| 50 | expect(out).toContain("not #999"); | |
| 51 | expect(out).not.toContain("/999"); | |
| 52 | }); | |
| 53 | ||
| 54 | test("unreadable: a private repository's numbers stay plain text for someone who may not read it", async () => { | |
| 55 | const made = await createRepo(app, alice, { name: "priv-links", visibility: "private" }); | |
| 56 | if (!made.ok) throw new Error(String(made.error)); | |
| 57 | const issue = openIssue(app, alice, made.value, { title: "t", body: "x" }); | |
| 58 | if (!issue.ok) throw new Error(String(issue.error)); | |
| 59 | const n = issue.value.number; | |
| 60 | expect(draw(`see #${n}`, refTarget(app.db, bob, "alice/priv-links"))).not.toContain("<a "); | |
| 61 | expect(draw(`see #${n}`, refTarget(app.db, undefined, "alice/priv-links"))).not.toContain("<a "); | |
| 62 | expect(draw(`see #${n}`, refTarget(app.db, alice, "alice/priv-links"))).toContain(`href="/alice/priv-links/issues/${n}"`); | |
| 63 | expect(draw("see #1", refTarget(app.db, alice, undefined))).not.toContain("<a "); | |
| 64 | }); | |
| 65 | ||
| 66 | test("hostile: markup in the answer stays escaped and only digits reach a link", () => { | |
| 67 | const out = draw(`<script>alert(1)</script> #1 "><img src=x onerror=y> pull #2 ' [x](http://e.x/#3)`, (n) => `/o/r/issues/${String(n)}`); | |
| 68 | expect(out).not.toContain("<script"); | |
| 69 | expect(out).not.toContain("<img"); | |
| 70 | expect(out).toContain('<a href="/o/r/issues/1">#1</a>'); | |
| 71 | expect(out).toContain('<a href="/o/r/issues/2">pull #2</a>'); | |
| 72 | expect(out).toContain("&#39;"); | |
| 73 | expect(out).not.toContain("/issues/3"); | |
| 74 | }); | |
| 75 | ||
| 76 | test("a number that names a pull links to /pulls/N, and the terminal prints that path", async () => { | |
| 77 | const made = await createRepo(app, alice, { name: "pull-links", visibility: "public" }); | |
| 78 | if (!made.ok) throw new Error(String(made.error)); | |
| 79 | const pull = createIssue(app.db, { repoId: made.value.id, authorId: alice.id, title: "p", kind: "pull" }); | |
| 80 | if (!pull.ok) throw new Error(pull.error); | |
| 81 | const n = pull.value.number; | |
| 82 | const to = refTarget(app.db, bob, "alice/pull-links"); | |
| 83 | expect(draw(`see pull ${n}`, to)).toContain(`<a href="/alice/pull-links/pulls/${n}">pull ${n}</a>`); | |
| 84 | expect(pathRefs(`see pull #${n} and #999`, to)).toBe(`see pull #${n} (/alice/pull-links/pulls/${n}) and #999`); | |
| 85 | }); | |
| 86 | ||
| 87 | test("terminal text: an unreadable number prints nothing extra", async () => { | |
| 88 | const made = await createRepo(app, alice, { name: "priv-text", visibility: "private" }); | |
| 89 | if (!made.ok) throw new Error(String(made.error)); | |
| 90 | const issue = openIssue(app, alice, made.value, { title: "t", body: "x" }); | |
| 91 | if (!issue.ok) throw new Error(String(issue.error)); | |
| 92 | const n = issue.value.number; | |
| 93 | expect(pathRefs(`see #${n}`, refTarget(app.db, bob, "alice/priv-text"))).toBe(`see #${n}`); | |
| 94 | expect(pathRefs(`see #${n}`, refTarget(app.db, alice, "alice/priv-text"))).toBe(`see #${n} (/alice/priv-text/issues/${n})`); | |
| 95 | }); |
test/tui-talk-chats.test.ts +17
| @@ -9,6 +9,7 @@ import { tmpdir } from "node:os"; | ||
| 9 | 9 | import { join } from "node:path"; |
| 10 | 10 | import { createApp, type App } from "../src/app.ts"; |
| 11 | 11 | import { appendTurn, archiveConversation, CONVERSATIONS_MAX, createConversation, listConversations, listTurns, renameConversation } from "../src/core/conversations.ts"; |
| 12 | import { createIssue } from "../src/core/issues.ts"; | |
| 12 | 13 | import { createUser, type User } from "../src/core/users.ts"; |
| 13 | 14 | import { createRepo } from "../src/actions/repos.ts"; |
| 14 | 15 | import { STARTER_PROMPTS } from "../src/actions/talk.ts"; |
| @@ -101,6 +102,22 @@ describe("the terminal's conversations screen", () => { | ||
| 101 | 102 | expect(turns.ok && turns.value.length).toBe(2); |
| 102 | 103 | }); |
| 103 | 104 | |
| 105 | test("an answer's cited number prints its path when readable and nothing when not (#13405)", async () => { | |
| 106 | for (const [name, visibility, shows] of [["pubtalk", "public", true], ["privtalk", "private", false]] as const) { | |
| 107 | const repo = await createRepo(app, alice, { name, visibility }); | |
| 108 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 109 | const issue = createIssue(app.db, { repoId: repo.value.id, authorId: alice.id, title: "t" }); | |
| 110 | if (!issue.ok) throw new Error(issue.error); | |
| 111 | const made = createConversation(app.db, bob.id); | |
| 112 | if (!made.ok) throw new Error(made.error); | |
| 113 | appendTurn(app.db, bob.id, made.value.id, { role: "tool", text: `mcp__forge__read_file {"repo":"alice/${name}","path":"a.ts"}` }); | |
| 114 | appendTurn(app.db, bob.id, made.value.id, { role: "agent", text: `it is tracked in #${issue.value.number}` }); | |
| 115 | const text = await drive(bob, [...open, "\r"]); | |
| 116 | expect(text).toContain(`tracked in #${issue.value.number}`); | |
| 117 | expect(text.includes(`/alice/${name}/issues/${issue.value.number}`)).toBe(shows); | |
| 118 | } | |
| 119 | }); | |
| 120 | ||
| 104 | 121 | test("an empty conversation numbers the starter prompts, and a number sends that one through talk()", async () => { |
| 105 | 122 | const carol = await createUser(app.db, { origin: "seed", trusted: true, username: "carol", email: "[email protected]", password: "a-good-passphrase" }); |
| 106 | 123 | if (!carol.ok) throw new Error(carol.error); |
new above
offline
Both doors: this helper is used only by the web page. src/tui/ talk needs the same refTarget gating, with a printed path in place of a link, plus a test and docs/features/talk-links.md.