Public: anyone can read this; the crew can change it.
Main green: tool docs in /llms.txt cap at 800 characters (find_tool has the rest); chart tooltip clamps in CSS; doc link #16814
Check · Review · Land · Live done — Landed and live in production.
main-red-2 → main 0 commits
11 files
changed +77
−24
docs/features/data-browser.md +1 −1
| @@ -30,6 +30,6 @@ Cost of a query: `query` also returns `rowsRead` (rows the query produced, the o | ||
| 30 | 30 | |
| 31 | 31 | ## The API spec as a file (#7578) |
| 32 | 32 | |
| 33 | MCP `data` with `spec: true` lists operations (`area`, `method`, `path`, `summary`; 300 at most, with `total` saying how many matched) for what the forge's contract says as your role may call it; add `q` to narrow by words, or `repo` for the document that app serves at `/openapi.json` (the same door as its API tab: a 404 where you may not enter the app, "serves no /openapi.json" when it has none). `src/data/spec_scope.ts` reads it through `reference` (`src/contract/reference.ts`), the reading the web `/api` page and the terminal API screen (`src/tui/screens/api.ts`) already use. | |
| 33 | MCP `data` with `spec: true` lists operations (`area`, `method`, `path`, `summary`; 300 at most, with `total` saying how many matched) for what the forge's contract says as your role may call it; add `q` to narrow by words, or `repo` for that app's own OpenAPI document (docs/features/api-reference.md; the same door as its API tab: a 404 where you may not enter the app, and a plain answer that it serves none when it has none). `src/data/spec_scope.ts` reads it through `reference` (`src/contract/reference.ts`), the reading the web `/api` page and the terminal API screen (`src/tui/screens/api.ts`) already use. | |
| 34 | 34 | |
| 35 | 35 | Three doors, one reading. In the data tree the spec is a file named `openapi.json`: the forge's own at the bottom of `/data` and of the terminal's Data screen (only for someone who sees any file there), and each app's after its environments (the app's Data tab on the web, the app node in the terminal; Enter or `l` opens it, `y` yanks `METHOD /path`). On the web it is `?spec=1` on `/data` and on `/:owner/:name/data`; a reader and a guest get the same 404 or sign-in as for the databases, and an app's document is asked through `appSpec`, which still checks `canEnterApp`. It reads the app's production environment only; a preview's document is not read here. The canonical place to read and try operations stays `/api` and `/:owner/:name/api` (and the terminal's API screen): the `openapi.json` nodes and the MCP tool link to them and add no second listing, only the same `reference` rows. |
src/http/mcp.ts +5
| @@ -106,6 +106,11 @@ function forTalk<T extends { inputSchema?: unknown }>(entry: T): T { | ||
| 106 | 106 | /** The entries this caller may call, `report` and `start` included: what find_tool searches. */ |
| 107 | 107 | export const entriesFor = (ctx: Ctx, user: User) => toolEntries(TOOLS.filter((t) => listable(t, ctx, user))); |
| 108 | 108 | |
| 109 | /** | |
| 110 | * A tool's whole `doc`, for a caller who may call it: what /llms.txt cuts short, find_tool returns. | |
| 111 | */ | |
| 112 | export const docFor = (ctx: Ctx, user: User, name: string): string | undefined => TOOLS.find((t) => t.name === name && listable(t, ctx, user))?.doc; | |
| 113 | ||
| 109 | 114 | // One page of tools/list stays within this many bytes of tools; `nextCursor` |
| 110 | 115 | // names the rest, so adding a tool never busts one response. |
| 111 | 116 | export const TOOLS_PAGE_BYTES = 20_000; |
src/http/mcp/tools/find_tool.ts +8 −3
| @@ -3,7 +3,7 @@ import { FIND_MAX, matching, QUERY_MAX, talkLists } from "../catalog.ts"; | ||
| 3 | 3 | |
| 4 | 4 | export const tool: Tool = { |
| 5 | 5 | name: "find_tool", |
| 6 | doc: "`{ query }`: the tools whose name, description or plain-word keywords match every word (else the most words); filler words are ignored, with their one-line description and schema; a tool found is callable at once, listed or not; Talk's own agent, which can call only what it is listed, is found only those. `tools/list` with `{\"all\": true}` is the full list", | |
| 6 | doc: "`{ query }`: the tools whose name, description or plain-word keywords match every word (else the most words); filler words are ignored, with their one-line description and schema (and, asked by its exact name, its whole doc); a tool found is callable at once, listed or not; Talk's own agent, which can call only what it is listed, is found only those. `tools/list` with `{\"all\": true}` is the full list", | |
| 7 | 7 | description: "Find a tool by what it does: name, description, schema. Any tool is callable by name.", |
| 8 | 8 | inputSchema: { |
| 9 | 9 | type: "object", |
| @@ -15,7 +15,7 @@ export const tool: Tool = { | ||
| 15 | 15 | const query = str(args["query"]).trim(); |
| 16 | 16 | if (query === "") return bad("invalid_query", "query is a word or two about what you want to do"); |
| 17 | 17 | // Lazy: mcp.ts imports the tool table, which holds this tool. |
| 18 | const { entriesFor } = await import("../../mcp.ts"); | |
| 18 | const { docFor, entriesFor } = await import("../../mcp.ts"); | |
| 19 | 19 | const found = matching( |
| 20 | 20 | // Talk's agent calls only what it is listed: it is never offered a tool it cannot call. |
| 21 | 21 | entriesFor(ctx, user) |
| @@ -23,8 +23,13 @@ export const tool: Tool = { | ||
| 23 | 23 | .map(({ name, description, inputSchema }) => ({ name, description, inputSchema })), |
| 24 | 24 | query, |
| 25 | 25 | ); |
| 26 | // A tool asked for by its exact name comes first with its whole doc (/llms.txt cuts a long one | |
| 27 | // short). | |
| 28 | const exact = found.find((t) => t.name === query); | |
| 29 | const doc = exact === undefined ? undefined : docFor(ctx, user, query); | |
| 30 | const ranked = exact === undefined ? found : [doc === undefined ? exact : { ...exact, doc }, ...found.filter((t) => t !== exact)]; | |
| 26 | 31 | return good({ |
| 27 | tools: found.slice(0, FIND_MAX), | |
| 32 | tools: ranked.slice(0, FIND_MAX), | |
| 28 | 33 | ...(found.length > FIND_MAX ? { more: found.length - FIND_MAX } : {}), |
| 29 | 34 | }); |
| 30 | 35 | }, |
src/web/chart_hover.ts +3 −3
| @@ -28,10 +28,10 @@ export const chartHoverJs = /* js */ ` | ||
| 28 | 28 | } |
| 29 | 29 | var shown = null, pending = false; |
| 30 | 30 | function place(pt) { |
| 31 | var w = tip.offsetWidth, h = tip.offsetHeight; | |
| 32 | var left = Math.min(Math.max(8, pt.x + 12), window.innerWidth - w - 8); | |
| 31 | // Across, CSS keeps the tip inside the viewport (.chart-tip); only its height is read here. | |
| 32 | var h = tip.offsetHeight; | |
| 33 | 33 | var top = pt.y - h - 12 < 8 ? pt.y + 16 : pt.y - h - 12; |
| 34 | tip.style.left = left + 'px'; tip.style.top = top + 'px'; | |
| 34 | tip.style.setProperty('--x', pt.x + 'px'); tip.style.top = top + 'px'; | |
| 35 | 35 | } |
| 36 | 36 | function open(head, rows, pt) { |
| 37 | 37 | tip.textContent = ''; tip.appendChild(head); if (rows) tip.appendChild(rows); |
src/web/llms_tools.ts +14 −1
| @@ -15,12 +15,25 @@ export type Listed = Pick<Tool, "name" | "description" | "doc" | "writes" | "adm | ||
| 15 | 15 | |
| 16 | 16 | const oneLine = (s: string): string => s.replace(/\s+/g, " ").trim(); |
| 17 | 17 | |
| 18 | /** | |
| 19 | * The most of a tool's doc one line inlines. Every tool adds its doc to this text, which has a byte | |
| 20 | * budget per role (test/llms-budget.test.ts), so a long doc is cut at a word and the line names the | |
| 21 | * door to the whole of it: `find_tool` with the tool's exact name returns its `doc`. | |
| 22 | */ | |
| 23 | export const LINE_DOC_MAX = 800; | |
| 24 | ||
| 25 | const bounded = (name: string, doc: string): string => { | |
| 26 | if (doc.length <= LINE_DOC_MAX) return doc; | |
| 27 | const cut = doc.slice(0, doc.lastIndexOf(" ", LINE_DOC_MAX)).replace(/[\s,;:(]+$/, ""); | |
| 28 | return `${cut} … (all of it: find_tool \`{ query: "${name}" }\`)`; | |
| 29 | }; | |
| 30 | ||
| 18 | 31 | export function toolLine(t: Listed): string { |
| 19 | 32 | const admin = t.adminOnly === true || t.needsAdminScope === true ? " (admin)" : ""; |
| 20 | 33 | const writes = t.writes ? " (writes)" : ""; |
| 21 | 34 | // The same list the MCP's anonymous gate reads (core/mcp_card.ts). |
| 22 | 35 | const open = TOKEN_FREE_TOOLS.includes(t.name) ? " (no account)" : ""; |
| 23 | return `- ${t.name}: ${oneLine(t.doc ?? t.description)}${admin}${writes}${open}`; | |
| 36 | return `- ${t.name}: ${bounded(t.name, oneLine(t.doc ?? t.description))}${admin}${writes}${open}`; | |
| 24 | 37 | } |
| 25 | 38 | |
| 26 | 39 | /** What every "planned first" in a tool line means, said once at the head of the list. */ |
src/web/style/content.ts +1 −1
| @@ -234,7 +234,7 @@ a.where-place, button.where-place { cursor: pointer; } | ||
| 234 | 234 | .chart-mark-hit { stroke: transparent; stroke-width: 8; vector-effect: non-scaling-stroke; pointer-events: stroke; } |
| 235 | 235 | .chart-mark { pointer-events: none; stroke: var(--muted-fg); stroke-width: 1; stroke-dasharray: 3 3; vector-effect: non-scaling-stroke; } |
| 236 | 236 | .chart-cross { stroke: var(--fg); stroke-width: 1; vector-effect: non-scaling-stroke; pointer-events: none; } |
| 237 | .chart-tip { margin: 0; inset: auto; overflow: visible; max-width: min(320px, calc(100vw - 16px)); padding: 6px 8px; font-size: var(--fs-min); line-height: 16px; font-variant-numeric: tabular-nums; color: var(--fg); background: var(--muted); border: 0; border-radius: var(--radius); pointer-events: none; } | |
| 237 | .chart-tip { margin: 0; inset: auto; overflow: visible; max-width: min(320px, calc(100vw - 16px)); padding: 6px 8px; font-size: var(--fs-min); line-height: 16px; font-variant-numeric: tabular-nums; color: var(--fg); background: var(--muted); border: 0; border-radius: var(--radius); pointer-events: none; left: max(8px, calc(var(--x, 0px) + 12px)); translate: min(0px, calc(100vw - 8px - 100% - max(8px, calc(var(--x, 0px) + 12px)))) 0; } | |
| 238 | 238 | .chart-tip > div > div { display: flex; justify-content: space-between; gap: 16px; } |
| 239 | 239 | .chart-tip > div > div > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted-fg); } |
| 240 | 240 | .chart .bar, .chart .cell { fill: currentColor; } |
test/chart-hover-script.test.ts +3 −2
| @@ -5,7 +5,8 @@ import { chartHoverJs } from "../src/web/chart_hover.ts"; | ||
| 5 | 5 | type El = any; |
| 6 | 6 | function dom(chartCount = 10) { |
| 7 | 7 | const mk = (tag = "div", attrs: Record<string, string> = {}): El => { |
| 8 | const e: El = { tag, attrs: { ...attrs }, kids: [], text: "", style: {}, parent: null, open: false, offsetWidth: 100, offsetHeight: 40 }; | |
| 8 | const e: El = { tag, attrs: { ...attrs }, kids: [], text: "", style: {}, parent: null, open: false, offsetHeight: 40 }; | |
| 9 | e.style.setProperty = (k: string, v: string) => (e.style[k] = v); | |
| 9 | 10 | e.setAttribute = (k: string, v: unknown) => (e.attrs[k] = String(v)); |
| 10 | 11 | e.getAttribute = (k: string) => e.attrs[k] ?? null; |
| 11 | 12 | e.appendChild = (c: El) => (c.parent = e, e.kids.push(c), c); |
| @@ -48,7 +49,7 @@ function dom(chartCount = 10) { | ||
| 48 | 49 | querySelectorAll: () => charts, |
| 49 | 50 | addEventListener: (n: string, f: (e: El) => void) => (listeners[n] ??= []).push(f), |
| 50 | 51 | }; |
| 51 | new Function("document", "window", chartHoverJs)(document, { innerWidth: 800 }); | |
| 52 | new Function("document", "window", chartHoverJs)(document, {}); | |
| 52 | 53 | const tip = body.kids[0]!; |
| 53 | 54 | const fire = (n: string, e: El) => listeners[n]?.forEach((f) => f({ preventDefault() { e.prevented = true; }, ...e })); |
| 54 | 55 | return { tip, hits, mark, charts, fire }; |
test/held.test.ts +4 −1
| @@ -89,7 +89,10 @@ describe("the memory sampler", () => { | ||
| 89 | 89 | try { |
| 90 | 90 | probe.add(4); |
| 91 | 91 | sampler.tick(); |
| 92 | const lines = info.mock.calls.map((call) => String(call[0])); | |
| 92 | // Only the memory and held lines: a job line joins them when any job on the thread (another | |
| 93 | // test file's, in a shared process) grew by 1 MiB since the last read | |
| 94 | // (test/job-alloc.test.ts). | |
| 95 | const lines = info.mock.calls.map((call) => String(call[0])).filter((l) => !l.startsWith("memory allocated by jobs")); | |
| 93 | 96 | expect(lines.map((l) => l.slice(0, l.indexOf(":")))).toEqual(["memory main", "memory holds", "memory main", "memory holds"]); |
| 94 | 97 | expect(lines[1]).toStartWith("memory holds: "); |
| 95 | 98 | expect(lines[1]!.split(": ")[1]!.split(", ")).toContain("probe 3"); |
test/llms-budget.test.ts +3 −2
| @@ -59,8 +59,9 @@ const get = async (token?: string): Promise<string> => | ||
| 59 | 59 | * add about 40 to 330 bytes to it. Only an agent with an admin token reads this text, and few do, |
| 60 | 60 | * so the cost of the room is small. Raised to 98,500 for cut_wave (admin 98,345), then to 98,700 |
| 61 | 61 | * for the allocation tool (#8718; admin 98,516), then to 99,200 for the app_flags tool (#1830; |
| 62 | * admin 98,939). Raised to 100,700 for the profile tool's `gift` action (admin 99,756; 100,036 with the changes | |
| 63 | * tool of #10839). | |
| 62 | * admin 98,939). Raised to 100,700 for the profile tool's `gift` action (admin 99,756; 100,036 with | |
| 63 | * the changes tool of #10839). Then a tool's line inlines at most `LINE_DOC_MAX` (800) characters | |
| 64 | * of its doc and names find_tool, which returns the whole of it: admin 92,689, signed-in 70,251. | |
| 64 | 65 | */ |
| 65 | 66 | const BUDGET = { anonymous: 9_250, signedIn: 84_100, admin: 100_700 } as const; |
| 66 | 67 |
test/llms-tools.test.ts +25 −6
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * /llms.txt's `## MCP tools` section is the generated tool index and `report`, line for line, each |
| 3 | * by its `doc`. | |
| 3 | * by its `doc`, a long one cut at a word with the door to the rest. | |
| 4 | 4 | */ |
| 5 | 5 | import { TOKEN_FREE_TOOLS } from "../src/core/mcp_card.ts"; |
| 6 | 6 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| @@ -14,7 +14,7 @@ import { OFFER_TOOL, REPORT_TOOL, START_TOOL } from "../src/http/mcp.ts"; | ||
| 14 | 14 | import { TOOLS } from "../src/http/mcp/tools.ts"; |
| 15 | 15 | import { createToken } from "../src/core/tokens.ts"; |
| 16 | 16 | import { createUser } from "../src/core/users.ts"; |
| 17 | import { PLANNED_LINE, type Listed } from "../src/web/llms_tools.ts"; | |
| 17 | import { LINE_DOC_MAX, PLANNED_LINE, type Listed } from "../src/web/llms_tools.ts"; | |
| 18 | 18 | |
| 19 | 19 | describe("/llms.txt MCP tools", () => { |
| 20 | 20 | let dataDir: string; |
| @@ -55,11 +55,30 @@ describe("/llms.txt MCP tools", () => { | ||
| 55 | 55 | expect(start).toBeGreaterThan(-1); |
| 56 | 56 | const end = lines.findIndex((l, i) => i > start && l.startsWith("## ")); |
| 57 | 57 | const listed = lines.slice(start + 1, end).filter((l) => l.startsWith("- ")); |
| 58 | const expected = [...TOOLS, REPORT_TOOL, START_TOOL, OFFER_TOOL].map((t: Listed) => { | |
| 58 | const all: Listed[] = [...TOOLS, REPORT_TOOL, START_TOOL, OFFER_TOOL]; | |
| 59 | expect(listed.map((l) => l.slice(2, l.indexOf(":")))).toEqual(all.map((t) => t.name)); | |
| 60 | for (const [i, t] of all.entries()) { | |
| 59 | 61 | const admin = t.adminOnly === true || t.needsAdminScope === true ? " (admin)" : ""; |
| 60 | return `- ${t.name}: ${(t.doc ?? t.description).replace(/\s+/g, " ").trim()}${admin}${t.writes ? " (writes)" : ""}${TOKEN_FREE_TOOLS.includes(t.name) ? " (no account)" : ""}`; | |
| 61 | }); | |
| 62 | expect(listed).toEqual(expected); | |
| 62 | const flags = `${admin}${t.writes ? " (writes)" : ""}${TOKEN_FREE_TOOLS.includes(t.name) ? " (no account)" : ""}`; | |
| 63 | const doc = (t.doc ?? t.description).replace(/\s+/g, " ").trim(); | |
| 64 | // A doc within the bound is inlined whole; a longer one is cut at a word and names the door. | |
| 65 | if (doc.length <= LINE_DOC_MAX) expect(listed[i]).toBe(`- ${t.name}: ${doc}${flags}`); | |
| 66 | else { | |
| 67 | const pointer = ` … (all of it: find_tool \`{ query: "${t.name}" }\`)${flags}`; | |
| 68 | expect(listed[i]!.endsWith(pointer), t.name).toBe(true); | |
| 69 | const kept = listed[i]!.slice(`- ${t.name}: `.length, -pointer.length); | |
| 70 | expect(doc.startsWith(kept) && kept.length <= LINE_DOC_MAX && kept.length > LINE_DOC_MAX / 2, t.name).toBe(true); | |
| 71 | } | |
| 72 | } | |
| 73 | // The door gives the whole doc of a tool whose line is cut. | |
| 74 | const long = TOOLS.find((t) => (t.doc ?? "").length > LINE_DOC_MAX * 2)!; | |
| 75 | const rpc = await fetch(`${servers.url}/mcp`, { | |
| 76 | method: "POST", | |
| 77 | headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${token}` }, | |
| 78 | body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "find_tool", arguments: { query: long.name } } }), | |
| 79 | }).then((r) => r.json() as Promise<{ result: { content: { text: string }[] } }>); | |
| 80 | const found = JSON.parse(rpc.result.content[0]!.text) as { tools: { name: string; doc?: string }[] }; | |
| 81 | expect(found.tools[0]).toMatchObject({ name: long.name, doc: long.doc }); | |
| 63 | 82 | // What every "planned first" means is said once, at the head of the list, not in each line. |
| 64 | 83 | expect(lines[start + 2]).toBe("A tool with `dryRun` only plans unless it is `false`; \"planned first\" below means that."); |
| 65 | 84 | expect(listed.filter((l) => l.includes("unless `false`"))).toEqual([]); |
test/mcp-tools.test.ts +10 −4
| @@ -13,7 +13,7 @@ import { indexText, toolFiles } from "../scripts/mcp-tools.ts"; | ||
| 13 | 13 | import { OFFER_TOOL, REPORT_TOOL, START_TOOL, toolNames } from "../src/http/mcp.ts"; |
| 14 | 14 | import { byName } from "../src/http/mcp/shared.ts"; |
| 15 | 15 | import { TOOLS } from "../src/http/mcp/tools.ts"; |
| 16 | import { toolsSection } from "../src/web/llms_tools.ts"; | |
| 16 | import { LINE_DOC_MAX, toolsSection } from "../src/web/llms_tools.ts"; | |
| 17 | 17 | |
| 18 | 18 | const ROOT = join(import.meta.dir, ".."); |
| 19 | 19 | const read = (path: string) => readFileSync(join(ROOT, path), "utf8"); |
| @@ -66,11 +66,17 @@ describe("the gathered tools", () => { | ||
| 66 | 66 | }); |
| 67 | 67 | |
| 68 | 68 | describe("the live tool list", () => { |
| 69 | test("every tool's `doc` is its line in /llms.txt's tools section", () => { | |
| 69 | // Up to LINE_DOC_MAX characters, the doc is the line; past it, the line is the doc cut at a word | |
| 70 | // and the find_tool pointer, and find_tool returns the whole doc (test/llms-tools.test.ts). | |
| 71 | test("every tool's `doc` is its line in /llms.txt's tools section, up to the cap and then the find_tool pointer", () => { | |
| 70 | 72 | const section = toolsSection([...TOOLS, REPORT_TOOL, START_TOOL, OFFER_TOOL]); |
| 71 | 73 | for (const t of [...TOOLS, REPORT_TOOL, START_TOOL, OFFER_TOOL]) { |
| 72 | const line = section.find((l) => l.startsWith(`- ${t.name}: `)); | |
| 73 | expect([t.name, line?.includes(t.doc ?? t.description) ?? false]).toEqual([t.name, true]); | |
| 74 | const line = section.find((l) => l.startsWith(`- ${t.name}: `)) ?? ""; | |
| 75 | const doc = (t.doc ?? t.description).replace(/\s+/g, " ").trim(); | |
| 76 | const pointer = ` … (all of it: find_tool \`{ query: "${t.name}" }\`)`; | |
| 77 | const kept = doc.length <= LINE_DOC_MAX ? doc : line.slice(`- ${t.name}: `.length, line.indexOf(pointer)); | |
| 78 | const holds = doc.length <= LINE_DOC_MAX ? line.includes(doc) : line.includes(pointer) && kept.length > 0 && kept.length <= LINE_DOC_MAX && doc.startsWith(kept); | |
| 79 | expect([t.name, holds]).toEqual([t.name, true]); | |
| 74 | 80 | } |
| 75 | 81 | expect(section.filter((l) => l.startsWith("- ")).length).toBe(TOOLS.length + 3); |
| 76 | 82 | }); |
new above
offline