Public: anyone can read this; the crew can change it.
Traces 5b: a trace has a share link, off by default, revocable, served with secrets redacted #18162
Closed 10h 21m ago
scout/forge:scout/18084-traces-5b-a-trace-has-a-share-link-off-b → main 2 commits fork
9 files
changed +328
−5
docs/features/traces.md +8
| @@ -33,3 +33,11 @@ The file is read whole, then parsed, so the 256 MB limit is memory-bound: a sess | ||
| 33 | 33 | ## One trace as a tree (#18022) |
| 34 | 34 | |
| 35 | 35 | `GET /api/v1/traces/:id` answers the thread, its turns and their steps as one tree, rebuilt from the raw log with `parseRunLog` (a Talk turn's log is a run log): the thread's figures are the list's row (model, account, tokens in, cached and out, cost, outcome, started, ended, steps, errors), each step has its start and end in ms, kind, name, line, model, tokens and outcome (a run log holds no tokens per step, so those are 0). `timed` is false where the log kept no times. `truncated` is true when the log's start was cut (it is read from its last 512 KB) or it holds more than `MAX_STEPS` (20,000) steps. A trace the caller may not see answers exactly as an unknown id: 404 `not_found`. The tree is answered whole (about 15 ms for 5,000 steps). Code: `readTraceTree` in `src/http/api/traces.ts`. |
| 36 | ||
| 37 | ## A share link (#18084) | |
| 38 | ||
| 39 | The owner of a crew run's trace (whoever sees the run's cost: the repository's owner or writers) can show it to someone without an account, and take it back. Off until made. On `/traces/<run id>` a **Share** section has **Make a link** (`POST /traces/<run id>/share`) and **Revoke every link** (`POST …/unshare`); the link `/t/<token>` is shown once on the answer to making it, and only its SHA-256 is kept (`trace_share`). Both are same-origin only; a reader who is not the owner gets the 404 a missing trace gets (403 for a writer-less reader of the log). No grant or role of its own: it is the run log's owner rule. | |
| 40 | ||
| 41 | `/t/<token>` serves the steps and their raw lines read-only, every line passed through `redactHeld` again (a log written before a value was held is masked now), with no repository, cost or links, `no-store`, `no-referrer` and `noindex`. A revoked, unknown or malformed token is 404. Terminal door: on a trace screen `s` makes a link (shown once) and `x` revokes them. Code: `src/trace/share.ts`, `src/web/pages/trace.ts`. | |
| 42 | ||
| 43 | Not yet: expiry, naming a link, revoking one of several, a JSON twin. |
src/http/lazy_lists.ts +3
| @@ -703,6 +703,9 @@ export const OPERATIONS_ROUTES = [ | ||
| 703 | 703 | ["GET", "/traces"], |
| 704 | 704 | ["GET", "/traces/:id"], |
| 705 | 705 | ["GET", "/traces/:id/steps/:n"], |
| 706 | ["POST", "/traces/:id/share"], | |
| 707 | ["POST", "/traces/:id/unshare"], | |
| 708 | ["GET", "/t/:token"], | |
| 706 | 709 | ] as const; |
| 707 | 710 | |
| 708 | 711 | /** `originsRoutes` (`routes/origins.ts`). */ |
src/http/routes/operations.ts +5 −1
| @@ -22,7 +22,7 @@ import { pipelinePage } from "../../web/pages/pipeline.ts"; | ||
| 22 | 22 | import { advisoriesIngest, advisoriesPage } from "../../web/pages/advisories.ts"; |
| 23 | 23 | import { resiliencePage, nightlyOutputPage } from "../../web/pages/resilience.ts"; |
| 24 | 24 | import { runRedirect } from "../../web/pages/runs.ts"; |
| 25 | import { tracePage, traceStep } from "../../web/pages/trace.ts"; | |
| 25 | import { sharedPage, tracePage, traceShare, traceStep, traceUnshare } from "../../web/pages/trace.ts"; | |
| 26 | 26 | import { tracesPage } from "../../web/pages/traces.ts"; |
| 27 | 27 | import { oauthCallbackUri } from "../api.ts"; |
| 28 | 28 | import { gamesRoutes } from "../games_routes.ts"; |
| @@ -124,4 +124,8 @@ export function operationsRoutes(router: Router<Handler>, knownPatterns: () => s | ||
| 124 | 124 | router.add("GET", "/traces", tracesPage); |
| 125 | 125 | router.add("GET", "/traces/:id", tracePage); |
| 126 | 126 | router.add("GET", "/traces/:id/steps/:n", traceStep); |
| 127 | router.add("POST", "/traces/:id/share", traceShare); | |
| 128 | router.add("POST", "/traces/:id/unshare", traceUnshare); | |
| 129 | // A shared trace (#18084), open to anyone holding a live link. | |
| 130 | router.add("GET", "/t/:token", sharedPage); | |
| 127 | 131 | } |
src/tui/screens/trace.ts +28 −2
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | |
| 7 | 7 | import type { Repo } from "../../core/repos.ts"; |
| 8 | 8 | import { readTrace, traceHref, traceRows } from "../../trace/read.ts"; |
| 9 | import { createShare, liveShares, revokeShares, shareHref } from "../../trace/share.ts"; | |
| 9 | 10 | import type { Ctx, Screen } from "../app/app.ts"; |
| 10 | 11 | import { binding } from "../app/keys.ts"; |
| 11 | 12 | import type { Session } from "../session.ts"; |
| @@ -17,14 +18,26 @@ import { Scroll, openOnWeb } from "./common.ts"; | ||
| 17 | 18 | export function traceScreen(f: Session, ctx: Ctx, repo: Repo, runId: string): Screen { |
| 18 | 19 | const scroll = new Scroll(); |
| 19 | 20 | let rows: string[] | undefined; |
| 21 | let base: string[] | undefined; | |
| 20 | 22 | let loaded = false; |
| 23 | /** What sharing last said: a link is shown here once, as on the page. */ | |
| 24 | let said: string | undefined; | |
| 25 | const shareRows = (): string[] => { | |
| 26 | const live = liveShares(f.app, f.user, repo, runId); | |
| 27 | if (live === undefined) return []; | |
| 28 | return [ | |
| 29 | "", | |
| 30 | said ?? (live === 0 ? "Sharing is off. s makes a link." : `${live} link${live === 1 ? " is" : "s are"} live. s makes another, x revokes them.`), | |
| 31 | ]; | |
| 32 | }; | |
| 21 | 33 | return { |
| 22 | 34 | title: "Trace", |
| 23 | 35 | here: () => ({ repo, run: runId }), |
| 24 | keys: [binding(["j", "k"], "scroll"), binding(["pgup", "pgdn"], "page"), binding(["o"], "open on the web")], | |
| 36 | keys: [binding(["j", "k"], "scroll"), binding(["pgup", "pgdn"], "page"), binding(["s"], "share link"), binding(["x"], "revoke links"), binding(["o"], "open on the web")], | |
| 25 | 37 | async load() { |
| 26 | 38 | const view = await readTrace(f.app, f.user, repo, runId); |
| 27 | rows = view === undefined ? undefined : traceRows(view); | |
| 39 | base = view === undefined ? undefined : traceRows(view); | |
| 40 | rows = base === undefined ? undefined : [...base, ...shareRows()]; | |
| 28 | 41 | loaded = true; |
| 29 | 42 | ctx.invalidate(); |
| 30 | 43 | }, |
| @@ -47,6 +60,19 @@ export function traceScreen(f: Session, ctx: Ctx, repo: Repo, runId: string): Sc | ||
| 47 | 60 | return true; |
| 48 | 61 | } |
| 49 | 62 | if (rows === undefined) return false; |
| 63 | if ((e.key === "s" || e.key === "x") && !e.ctrl && !e.alt) { | |
| 64 | if (e.key === "s") { | |
| 65 | const made = createShare(f.app, f.user, repo, runId); | |
| 66 | said = made.ok ? `Link, shown once: ${f.app.config.url}${shareHref(made.value.token)}` : "Only the trace's owner shares it."; | |
| 67 | } else { | |
| 68 | const done = revokeShares(f.app, f.user, repo, runId); | |
| 69 | said = !done.ok ? "Only the trace's owner shares it." : done.value === 0 ? "There was no link to revoke." : "Every link is revoked."; | |
| 70 | } | |
| 71 | rows = [...(base ?? []), ...shareRows()]; | |
| 72 | if (said !== undefined && !rows.includes(said)) rows.push(said); | |
| 73 | ctx.invalidate(); | |
| 74 | return true; | |
| 75 | } | |
| 50 | 76 | const took = scroll.key(e, rows.length); |
| 51 | 77 | if (took) ctx.invalidate(); |
| 52 | 78 | return took; |
src/web/pages/trace.ts +78 −2
| @@ -17,6 +17,11 @@ import { crumbsFor, notFoundPage, pageHead, repoPalette } from "../chrome.ts"; | ||
| 17 | 17 | import { html, type Html } from "../html.ts"; |
| 18 | 18 | import { page } from "../layout.ts"; |
| 19 | 19 | import { behaviorsJs } from "../behaviors.ts"; |
| 20 | import { btn } from "../ui.ts"; | |
| 21 | import { readForm } from "../form.ts"; | |
| 22 | import { flashTo } from "../layout.ts"; | |
| 23 | import { createShare, liveShares, revokeShares, shareHref, SHARE_TOKEN, readShared, type ShareError, type SharedTrace } from "../../trace/share.ts"; | |
| 24 | import { signInFirst } from "../chrome.ts"; | |
| 20 | 25 | |
| 21 | 26 | /** A trace with this many steps or fewer opens its branches; a longer one opens none. */ |
| 22 | 27 | export const OPEN_BELOW = 100; |
| @@ -92,7 +97,19 @@ function strip(steps: readonly Step[], timed: boolean): Html { | ||
| 92 | 97 | return html`<svg class="trace-strip" viewBox="0 0 1000 28" preserveAspectRatio="none" role="group" aria-label="${timed ? `Steps over ${spanWords(whole)}` : "Steps in order"}">${out}</svg>`; |
| 93 | 98 | } |
| 94 | 99 | |
| 95 | function body(view: TraceView): Html { | |
| 100 | /** The owner's share controls (#18084); `token` is the link just made, shown this once. */ | |
| 101 | function shareBlock(runId: string, live: number, token: string | undefined, origin: string): Html { | |
| 102 | const base = traceHref(runId); | |
| 103 | return html`<section id="trace-share" aria-label="Share link"> | |
| 104 | <h2>Share</h2> | |
| 105 | ${token === undefined ? null : html`<p id="trace-share-token"><strong>Link, shown once:</strong> <code class="mono">${origin}${shareHref(token)}</code></p>`} | |
| 106 | <p class="small muted">${live === 0 ? "Sharing is off." : `${String(live)} link${live === 1 ? " is" : "s are"} live.`} Anyone with a link reads this trace without an account, secrets masked.</p> | |
| 107 | <form method="post" action="${base}/share" class="bar">${btn("Make a link", { size: "sm" })}</form> | |
| 108 | ${live === 0 ? null : html`<form method="post" action="${base}/unshare" class="bar">${btn("Revoke every link", { size: "sm", variant: "danger" })}</form>`} | |
| 109 | </section>`; | |
| 110 | } | |
| 111 | ||
| 112 | function body(view: TraceView, share: Html | null = null): Html { | |
| 96 | 113 | const steps = stepsInView(view); |
| 97 | 114 | const total = steps.length; |
| 98 | 115 | const base = traceHref(view.run.id); |
| @@ -119,6 +136,7 @@ function body(view: TraceView): Html { | ||
| 119 | 136 | <nav class="bar small" id="trace-links" aria-label="Where this run went">${view.links.map((l) => html`<a href="${l.href}">${l.label}</a>`)}<a href="/traces">All traces</a></nav> |
| 120 | 137 | ${changed === undefined ? null : html`<p id="trace-changed"><strong>What it changed:</strong> ${changed}. <a href="${view.links.find((l) => l.label.startsWith("Pull"))?.href ?? "#"}">Read the diff on the pull</a>.</p>`} |
| 121 | 138 | ${view.truncated ? html`<p class="small muted" id="trace-cut">The start of the log was cut, so steps count from the cut.</p>` : null} |
| 139 | ${share} | |
| 122 | 140 | ${total === 0 ? html`<p class="muted" id="trace-empty">This run wrote no steps.</p>` : html`${strip(steps, timed)} |
| 123 | 141 | <div class="trace-tree" id="trace-tree" data-base="${base}" role="group" aria-label="Steps">${rows}</div> |
| 124 | 142 | ${timed ? null : html`<p class="small muted" id="trace-untimed">This run's log kept no times, so steps show none; it holds no tokens or cost per step.</p>`}`}`; |
| @@ -135,21 +153,79 @@ function repoOfRun(ctx: Ctx): { repo: Repo; id: string } | undefined { | ||
| 135 | 153 | // GET /traces/:id |
| 136 | 154 | export async function tracePage(ctx: Ctx): Promise<Response> { |
| 137 | 155 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); |
| 156 | return traceAnswer(ctx); | |
| 157 | } | |
| 158 | ||
| 159 | async function traceAnswer(ctx: Ctx, token?: string): Promise<Response> { | |
| 138 | 160 | const found = repoOfRun(ctx); |
| 139 | 161 | if (found === undefined) return notFoundPage(ctx); |
| 140 | 162 | const view = await readTrace(ctx.app, ctx.auth.user, found.repo, found.id); |
| 141 | 163 | if (view === undefined) return notFoundPage(ctx); |
| 164 | const live = liveShares(ctx.app, ctx.auth.user, found.repo, found.id); | |
| 142 | 165 | return page({ |
| 143 | 166 | ctx, |
| 144 | 167 | title: `Trace · ${view.title}`, |
| 145 | 168 | crumbs: crumbsFor(found.repo), |
| 146 | 169 | palette: repoPalette(ctx, found.repo), |
| 147 | body: body(view), | |
| 170 | body: body(view, live === undefined ? null : shareBlock(found.id, live, token, ctx.url.origin)), | |
| 148 | 171 | styleCss: TRACE_CSS, |
| 149 | 172 | scriptJs: `${behaviorsJs}\n${TRACE_JS}`, |
| 150 | 173 | }); |
| 151 | 174 | } |
| 152 | 175 | |
| 176 | const refused = (ctx: Ctx, why: ShareError): Response => | |
| 177 | why === "owner_only" ? new Response("only the trace's owner shares it", { status: 403, headers: { "cache-control": "no-store" } }) : notFoundPage(ctx); | |
| 178 | ||
| 179 | /** The form door both owner acts share: signed in, same-origin, a run the viewer may read. */ | |
| 180 | async function ownerAct(ctx: Ctx, act: (found: { repo: Repo; id: string }) => Promise<Response> | Response): Promise<Response> { | |
| 181 | if (!ctx.auth.user) return signInFirst(ctx); | |
| 182 | const form = await readForm(ctx, "rw"); | |
| 183 | if (form instanceof Response) return form; | |
| 184 | const found = repoOfRun(ctx); | |
| 185 | return found === undefined ? notFoundPage(ctx) : act(found); | |
| 186 | } | |
| 187 | ||
| 188 | // POST /traces/:id/share — the owner makes a link; the answer is the trace page showing it once. | |
| 189 | export const traceShare = (ctx: Ctx): Promise<Response> => | |
| 190 | ownerAct(ctx, async (found) => { | |
| 191 | const made = createShare(ctx.app, ctx.auth.user, found.repo, found.id); | |
| 192 | if (!made.ok) return refused(ctx, made.error); | |
| 193 | const res = await traceAnswer(ctx, made.value.token); | |
| 194 | res.headers.set("cache-control", "private, no-store"); | |
| 195 | return res; | |
| 196 | }); | |
| 197 | ||
| 198 | // POST /traces/:id/unshare — the owner takes every link back. | |
| 199 | export const traceUnshare = (ctx: Ctx): Promise<Response> => | |
| 200 | ownerAct(ctx, (found) => { | |
| 201 | const done = revokeShares(ctx.app, ctx.auth.user, found.repo, found.id); | |
| 202 | if (!done.ok) return refused(ctx, done.error); | |
| 203 | return flashTo(ctx, traceHref(found.id), { message: done.value === 0 ? "There was no link to revoke." : "Every link is revoked." }); | |
| 204 | }); | |
| 205 | ||
| 206 | function sharedBody(t: SharedTrace): Html { | |
| 207 | const total = t.steps.length; | |
| 208 | return html`${pageHead(html`Trace: ${t.title}`, { sub: "A crew run, step by step. Shared read-only; secrets are masked." })} | |
| 209 | ${t.truncated ? html`<p class="small muted">The start of the log was cut, so steps count from the cut.</p>` : null} | |
| 210 | ${total === 0 ? html`<p class="muted">This run wrote no steps.</p>` : html`<div id="shared-steps">${t.steps.map(({ step, lines }, i) => html`<details class="trace-step trace-${step.kind}${step.outcome === "error" ? " trace-bad" : ""}" id="${step.id}"> | |
| 211 | <summary><span class="trace-n mono">${String(i + 1)}/${String(total)}</span>${step.outcome === "error" ? "✗ " : ""}${step.line}</summary> | |
| 212 | <pre class="trace-raw mono">${lines.join("\n")}</pre> | |
| 213 | </details>`)}</div>`}`; | |
| 214 | } | |
| 215 | ||
| 216 | // GET /t/:token — a shared trace, for anyone holding a live link; revoked, unknown or malformed | |
| 217 | // is 404. | |
| 218 | export async function sharedPage(ctx: Ctx): Promise<Response> { | |
| 219 | const token = ctx.params.token ?? ""; | |
| 220 | const shared = SHARE_TOKEN.test(token) ? await readShared(ctx.app, token) : undefined; | |
| 221 | if (shared === undefined) return notFoundPage(ctx, { noEcho: true, signedOut: true }); | |
| 222 | const res = page({ ctx, title: `Trace · ${shared.title}`, body: sharedBody(shared), styleCss: TRACE_CSS }); | |
| 223 | res.headers.set("cache-control", "private, no-store"); | |
| 224 | res.headers.set("referrer-policy", "no-referrer"); | |
| 225 | res.headers.set("x-robots-tag", "noindex, nofollow"); | |
| 226 | return res; | |
| 227 | } | |
| 228 | ||
| 153 | 229 | // GET /traces/:id/steps/:n — the raw lines of one step, as text, read when a step is opened. |
| 154 | 230 | export async function traceStep(ctx: Ctx): Promise<Response> { |
| 155 | 231 | const found = repoOfRun(ctx); |
new above
offline