A local-first multi-agent app development platform, built on PenguinHarness
End a bare URL at the URL, not at the end of the sentence (#437)
Yaowei Zhengb5c5cdf89770
diff --git a/changelog/unreleased/2026-08-24-autolink-ends-at-ascii.md b/changelog/unreleased/2026-08-24-autolink-ends-at-ascii.mdnew file mode 100644index 00000000..f6ba46bb--- /dev/null+++ b/changelog/unreleased/2026-08-24-autolink-ends-at-ascii.md@@ -0,0 +1,29 @@+# A bare URL ends where the URL ends, not where the sentence does++- **Date:** 2026-08-24+- **Type:** fix+- **Scope:** `web`+- **PR:** [#437](https://github.com/Prism-Shadow/penguin-harness/pull/437)++[中文版](2026-08-24-autolink-ends-at-ascii.zh.md)++A URL typed into Chinese prose swallowed whatever followed it. `见 https://penguin.ooo,然后继续`+linked the comma and the rest of the clause along with the address, so the link 404ed and the+sentence lost its punctuation to link styling. Markdown surfaces in the Web App now end a bare URL+at its last ASCII character.++## Details++- GFM's autolink literal ends a bare URL at whitespace, then trims a short list of trailing ASCII+ punctuation. Neither rule sees CJK, so `,`, `。`, `()` and plain Chinese text run up against+ the host were all inside the href. English was never affected — `see https://penguin.ooo, then`+ already ended correctly.+- The boundary is the last ASCII character, because a URL is ASCII by RFC 3986; a trailing+ non-ASCII run is handed back to the paragraph as text.+- Explicit `[文档](https://…)` links are untouched: the trim applies only to autolinks, where the+ link text is the URL itself.+- One shared plugin list now backs all five Markdown renderers — chat messages, both Trace event+ views, the benchmark case browser and the workspace file preview — so a URL ends in the same+ place on every surface.+- A URL carrying *unencoded* CJK in its path is trimmed at the last ASCII character. That form is+ invalid per the RFC; the same link percent-encoded is untouched.diff --git a/changelog/unreleased/2026-08-24-autolink-ends-at-ascii.zh.md b/changelog/unreleased/2026-08-24-autolink-ends-at-ascii.zh.mdnew file mode 100644index 00000000..b17e4881--- /dev/null+++ b/changelog/unreleased/2026-08-24-autolink-ends-at-ascii.zh.md@@ -0,0 +1,24 @@+# 裸链接在 URL 结束处终止,而不是在句子结束处++- **Date:** 2026-08-24+- **Type:** fix+- **Scope:** `web`+- **PR:** [#437](https://github.com/Prism-Shadow/penguin-harness/pull/437)++[English](2026-08-24-autolink-ends-at-ascii.md)++写在中文行文里的 URL 会把后面的内容一并吞掉:`见 https://penguin.ooo,然后继续` 会把逗号及其后的+半句话一起链接进去,于是链接 404,句子的标点也被套上了链接样式。Web App 的 Markdown 渲染现在让裸+链接在最后一个 ASCII 字符处终止。++## 细节++- GFM 的 autolink literal 以空白作为裸链接的终止,再裁掉少量结尾的 ASCII 标点。两条规则都不认识+ CJK,因此 `,`、`。`、`()` 以及紧贴主机名的中文文字都留在了 href 里。英文从来不受影响——+ `see https://penguin.ooo, then` 本就正确终止。+- 边界取最后一个 ASCII 字符:按 RFC 3986,URL 本就是 ASCII;结尾的非 ASCII 片段会作为文本交还给段落。+- 显式的 `[文档](https://…)` 链接不受影响:裁剪只作用于链接文字即 URL 本身的 autolink。+- 五个 Markdown 渲染入口——对话消息、两处 Trace 事件视图、Benchmark 用例浏览器与工作区文件预览——+ 现在共用同一份插件列表,因此裸链接在每个界面上的终止位置一致。+- 路径中携带**未编码** CJK 的 URL 会在最后一个 ASCII 字符处被截断。该写法本就不符合 RFC;同一链接+ 经百分号编码后不受影响。diff --git a/packages/web/src/features/benchmark/benchmark-case-browser.tsx b/packages/web/src/features/benchmark/benchmark-case-browser.tsxindex 0538f82a..4905968a 100644--- a/packages/web/src/features/benchmark/benchmark-case-browser.tsx+++ b/packages/web/src/features/benchmark/benchmark-case-browser.tsx@@ -7,7 +7,7 @@ import type {} from "@prismshadow/penguin-server/api";import ReactMarkdown from "react-markdown";import type { Components } from "react-markdown";-import remarkGfm from "remark-gfm";+import { REMARK_PLUGINS } from "../../lib/remark-autolink-boundary";import * as api from "../../api/endpoints";import { apiErrorText } from "../../lib/api-error";import { joinWorkspacePath } from "../../lib/file-path";@@ -455,7 +455,7 @@ export function BenchmarkCaseBrowser({ projectId, agentId, benchmarkId, caseSumm) : preview.kind === "md" ? (<><div className="md-body text-sm text-gray-800 dark:text-gray-100">- <ReactMarkdown remarkPlugins={[remarkGfm]} components={markdownComponents}>+ <ReactMarkdown remarkPlugins={REMARK_PLUGINS} components={markdownComponents}>{preview.content ?? ""}</ReactMarkdown></div>diff --git a/packages/web/src/features/chat/md.tsx b/packages/web/src/features/chat/md.tsxindex c9845a10..67361816 100644--- a/packages/web/src/features/chat/md.tsx+++ b/packages/web/src/features/chat/md.tsx@@ -16,7 +16,7 @@ import { isValidElement, memo } from "react";import type { ComponentPropsWithoutRef, ReactNode } from "react";import ReactMarkdown from "react-markdown";import type { Components, ExtraProps } from "react-markdown";-import remarkGfm from "remark-gfm";+import { REMARK_PLUGINS } from "../../lib/remark-autolink-boundary";import { CodeBlock } from "./code-block";/** Flatten a react-markdown code element's children to plain text (string or string array in practice). */@@ -91,7 +91,7 @@ export const Md = memo(function Md({}) {return (<ReactMarkdown- remarkPlugins={[remarkGfm]}+ remarkPlugins={REMARK_PLUGINS}components={streaming ? STREAMING_COMPONENTS : SETTLED_COMPONENTS}>{text}diff --git a/packages/web/src/features/chat/workspace-browser.tsx b/packages/web/src/features/chat/workspace-browser.tsxindex c112590d..d23df1f2 100644--- a/packages/web/src/features/chat/workspace-browser.tsx+++ b/packages/web/src/features/chat/workspace-browser.tsx@@ -15,7 +15,7 @@import { useCallback, useEffect, useRef, useState } from "react";import type { ChangeEvent } from "react";import ReactMarkdown from "react-markdown";-import remarkGfm from "remark-gfm";+import { REMARK_PLUGINS } from "../../lib/remark-autolink-boundary";import type { SessionInfo, WorkspaceFilesResponse } from "@prismshadow/penguin-server/api";import * as api from "../../api/endpoints";import { ApiError } from "../../api/client";@@ -566,7 +566,7 @@ export function WorkspaceBrowser({<><div className="md-body text-base leading-relaxed text-gray-800 dark:text-gray-100"><ReactMarkdown- remarkPlugins={[remarkGfm]}+ remarkPlugins={REMARK_PLUGINS}components={{// Relative images are resolved against the md file's directory into the file API (otherwise resolving against the app's origin would always 404).img: ({ src, alt }) => (diff --git a/packages/web/src/features/traces/trace-event-row.tsx b/packages/web/src/features/traces/trace-event-row.tsxindex ce7e76c5..3737a399 100644--- a/packages/web/src/features/traces/trace-event-row.tsx+++ b/packages/web/src/features/traces/trace-event-row.tsx@@ -13,7 +13,7 @@*/import { Fragment, useState } from "react";import ReactMarkdown from "react-markdown";-import remarkGfm from "remark-gfm";+import { REMARK_PLUGINS } from "../../lib/remark-autolink-boundary";import { S } from "../../lib/strings";import type { OmniMessage } from "@prismshadow/penguin-core/omnimessage";import { formatTime, humanizeTokens } from "../../lib/format";@@ -213,7 +213,7 @@ function SessionMetaBody({ p }: { p: Record<string, unknown> }) {<details><summary className={summaryClass}>{S.traces.systemPrompt}</summary><div className="md-body mt-1.5 max-h-96 overflow-auto rounded bg-gray-100 px-2.5 py-2 text-sm leading-relaxed text-gray-700 dark:bg-gray-800/70 dark:text-gray-300">- <ReactMarkdown remarkPlugins={[remarkGfm]}>{prompt}</ReactMarkdown>+ <ReactMarkdown remarkPlugins={REMARK_PLUGINS}>{prompt}</ReactMarkdown></div></details>)}@@ -316,7 +316,7 @@ function EventBody({ msg }: { msg: OmniMessage }) {if (!md.trim()) return <p className="text-xs text-gray-400">—</p>;return (<div className="md-body text-sm leading-relaxed text-gray-700 dark:text-gray-300">- <ReactMarkdown remarkPlugins={[remarkGfm]}>{md}</ReactMarkdown>+ <ReactMarkdown remarkPlugins={REMARK_PLUGINS}>{md}</ReactMarkdown></div>);}diff --git a/packages/web/src/lib/remark-autolink-boundary.ts b/packages/web/src/lib/remark-autolink-boundary.tsnew file mode 100644index 00000000..04f8c149--- /dev/null+++ b/packages/web/src/lib/remark-autolink-boundary.ts@@ -0,0 +1,88 @@+/**+ * Ends a bare URL at the last ASCII character.+ *+ * GFM's autolink-literal extension ends a bare URL at whitespace, then trims a short list of+ * trailing ASCII punctuation. Neither rule sees CJK: `见 https://penguin.ooo,然后继续` is not+ * whitespace-separated after the host, so the comma and the clause after it are swallowed into the+ * href, which then 404s, and the sentence loses its punctuation to link styling. English is+ * unaffected — `see https://penguin.ooo, then` already trims correctly — so this only ever fires on+ * text GFM was not written for.+ *+ * A URL is ASCII by RFC 3986; anything outside it belongs percent-encoded. So the boundary is the+ * last ASCII character, and a trailing non-ASCII run is handed back to the paragraph as text.+ *+ * The trade this makes: a URL carrying *unencoded* CJK in its path — `…/wiki/中文` pasted raw+ * rather than percent-encoded — is trimmed at the last ASCII character. That form is invalid per+ * the RFC and browsers only accept it by encoding it for you; the same link pasted in its encoded+ * form is untouched. Explicit `[text](url)` links are never touched at all.+ */+import remarkGfm from "remark-gfm";++/**+ * The mdast shapes this touches, declared structurally rather than pulling `@types/mdast` in as a+ * dependency for three of them. Anything else on a node passes through untouched.+ */+interface TextNode {+ type: "text";+ value: string;+}+interface LinkNode {+ type: "link";+ url: string;+ children: Node[];+}+type Node = TextNode | LinkNode | { type: string; children?: Node[] };+interface Parent {+ children: Node[];+}++/** Trailing characters outside ASCII: never part of a well-formed URL. */+const TRAILING_NON_ASCII = /[^\x00-\x7F]+$/;++/** True when this link came from autolinking rather than `[text](url)` syntax. */+function isAutolinkLiteral(node: LinkNode): boolean {+ if (node.children.length !== 1) return false;+ const child = node.children[0];+ if (!child || child.type !== "text") return false;+ const { value } = child as TextNode;+ // GFM sets url to the matched text, prefixing `http://` for a bare `www.` match.+ return node.url === value || node.url === `http://${value}`;+}++function trimNode(node: LinkNode): TextNode | null {+ if (!isAutolinkLiteral(node)) return null;+ const child = node.children[0] as TextNode;+ const match = TRAILING_NON_ASCII.exec(child.value);+ if (!match) return null;+ const kept = child.value.slice(0, match.index);+ // A "URL" that is non-ASCII all the way to its scheme is not one this should be splitting.+ if (!kept.includes("://") && !kept.startsWith("www.")) return null;+ child.value = kept;+ node.url = node.url.startsWith("http://") && !kept.startsWith("http") ? `http://${kept}` : kept;+ return { type: "text", value: match[0] };+}++/** Walks every parent, so a link nested in emphasis or a list item is covered too. */+function walk(parent: Parent): void {+ for (let i = 0; i < parent.children.length; i += 1) {+ const node = parent.children[i]!;+ if (node.type === "link") {+ const spill = trimNode(node as LinkNode);+ if (spill) parent.children.splice(i + 1, 0, spill);+ }+ const children = (node as { children?: Node[] }).children;+ if (Array.isArray(children)) walk(node as Parent);+ }+}++/** Runs after remark-gfm, on the tree it produced. */+export function remarkAutolinkBoundary() {+ return (tree: Parent): void => walk(tree);+}++/**+ * The remark plugin list every Markdown surface uses. Shared so the five renderers cannot drift:+ * a bare URL has to end at the same place in a chat message, a Trace event, a benchmark case and+ * a workspace file preview.+ */+export const REMARK_PLUGINS = [remarkGfm, remarkAutolinkBoundary];diff --git a/packages/web/test/remark-autolink-boundary.test.ts b/packages/web/test/remark-autolink-boundary.test.tsnew file mode 100644index 00000000..45317f56--- /dev/null+++ b/packages/web/test/remark-autolink-boundary.test.ts@@ -0,0 +1,94 @@+/**+ * Where a bare URL ends. GFM's autolink literal stops at whitespace and trims a little ASCII+ * punctuation, which leaves CJK — the comma, the clause after it, or plain text run up against the+ * host — inside the href. These pin the boundary at the last ASCII character, and pin the two+ * things that must not change: explicit links, and URLs that are already fine.+ */+import { describe, expect, it } from "vitest";+import React from "react";+import { renderToStaticMarkup } from "react-dom/server";+import ReactMarkdown from "react-markdown";+import { REMARK_PLUGINS } from "../src/lib/remark-autolink-boundary";++/** The rendered `[href, text]` of the first link, or null. */+function firstLink(markdown: string): [string, string] | null {+ const html = renderToStaticMarkup(+ React.createElement(ReactMarkdown, { remarkPlugins: REMARK_PLUGINS }, markdown),+ );+ const m = /<a href="([^"]*)"[^>]*>([^<]*)<\/a>/.exec(html);+ return m ? [m[1]!, m[2]!] : null;+}++/** The rendered text of the whole thing, tags stripped — what the reader actually sees. */+function plainText(markdown: string): string {+ return renderToStaticMarkup(+ React.createElement(ReactMarkdown, { remarkPlugins: REMARK_PLUGINS }, markdown),+ ).replace(/<[^>]*>/g, "");+}++describe("a bare URL ends at the last ASCII character", () => {+ it("stops before CJK punctuation, and the punctuation stays in the sentence", () => {+ expect(firstLink("见 https://penguin.ooo,然后继续")).toEqual([+ "https://penguin.ooo",+ "https://penguin.ooo",+ ]);+ expect(plainText("见 https://penguin.ooo,然后继续")).toContain(",然后继续");+ });++ it("stops before CJK text with no punctuation between", () => {+ expect(firstLink("中文https://penguin.ooo英文")).toEqual([+ "https://penguin.ooo",+ "https://penguin.ooo",+ ]);+ expect(plainText("中文https://penguin.ooo英文")).toContain("英文");+ });++ it("stops before a full-width bracket after a path with a query", () => {+ expect(firstLink("参考 https://penguin.ooo/a/b?c=1(备注)")).toEqual([+ "https://penguin.ooo/a/b?c=1",+ "https://penguin.ooo/a/b?c=1",+ ]);+ });++ it("stops before a full stop at the end of a sentence", () => {+ expect(firstLink("见 https://penguin.ooo。")).toEqual([+ "https://penguin.ooo",+ "https://penguin.ooo",+ ]);+ });+});++describe("what must not change", () => {+ it("leaves English alone — GFM already ends those correctly", () => {+ expect(firstLink("see https://penguin.ooo, then")).toEqual([+ "https://penguin.ooo",+ "https://penguin.ooo",+ ]);+ expect(firstLink("docs at https://penguin.ooo/docs work")).toEqual([+ "https://penguin.ooo/docs",+ "https://penguin.ooo/docs",+ ]);+ });++ it("leaves an explicit link's CJK text alone", () => {+ // The trim applies to autolinks only; here the CJK *is* the link text the author wrote.+ expect(firstLink("[中文文档](https://penguin.ooo/docs)")).toEqual([+ "https://penguin.ooo/docs",+ "中文文档",+ ]);+ });++ it("leaves a percent-encoded path alone, CJK or not", () => {+ expect(firstLink("见 https://penguin.ooo/wiki/%E4%B8%AD%E6%96%87 完")).toEqual([+ "https://penguin.ooo/wiki/%E4%B8%AD%E6%96%87",+ "https://penguin.ooo/wiki/%E4%B8%AD%E6%96%87",+ ]);+ });++ it("keeps a bare www. autolink working, prefix and all", () => {+ expect(firstLink("见 www.penguin.ooo,好")).toEqual([+ "http://www.penguin.ooo",+ "www.penguin.ooo",+ ]);+ });+});