Public: anyone can read this; the crew can change it.
A person shares one imported session by a link that hides what the forge redacts #18391
Merged 3h 21m ago, live in production
mason/forge:mason/18377-a-person-shares-one-imported-session-by → main 0 commits fork 1 thread · 1 unresolved
11 files
changed +411
−19
docs/features/traces.md +4
| @@ -76,3 +76,7 @@ A search you repeat (a repository, a model, a failed outcome, a tool) is kept un | ||
| 76 | 76 | - **MCP:** the `trace` tool's `action: "saved"` lists, with `name` and `query` keeps, with `remove` (an id) ends one. |
| 77 | 77 | - **Terminal:** in Traces, `b` types the start of a saved name and Enter opens it, `w` names the filters on screen and keeps them, `x` removes the search last opened. |
| 78 | 78 | - **Opening re-reads the query** through the list's own validator: a facet that no longer exists (an outcome the forge dropped) is left out and the page, the API (`dropped`) and the terminal say which. Names are escaped everywhere. Code: `src/trace/saved.ts`, `src/web/pages/traces_saved.ts`. |
| 79 | ||
| 80 | ## Share an imported session (#18377) | |
| 81 | ||
| 82 | On `/traces/<id>` of an imported session its importer sees **Make a share link** (`POST /traces/<id>/share`, same-origin) and, while one is live, **Stop sharing** (`/unshare`). It is the run's link (`src/trace/share.ts`, gate `shareImported` and `unshareImported` in `src/actions/trace_share.ts`, reader `src/trace/shared.ts`, the same `/traces/shared/<secret>` page): shown once, only its hash kept, one live link, off until made. Anyone else, signed in or not, gets the not-found, as for the session's own page. The shared page passes every step line and raw line through `redactHeld` (credential shapes and granted values become `[redacted]`) and drops who and where from the raw records (`src/trace/who.ts`: the keys `cwd`, `gitBranch`, `email`, `user`, `host`, `git` and the like, read only at a record's top level, in `payload` and in `message.metadata`; a tool's input is kept whole, even one with a `user` key), so no person and no path of the importer's machine shows; it says the forge cannot know every secret, and the form on the session says to share only what you have read. A stopped link, a deleted session or a link never made is the plain not-found, and one address opens 60 shared pages a minute. Terminal: in the Traces list (`src/tui/screens/traces.ts`) `s` on an imported row makes the link (shown to copy; a second press confirms when one is live) and `z` ends it. |
src/trace/imported_read.ts +25
| @@ -90,3 +90,28 @@ export function rawLinesOf(lines: Iterable<string>, step: Pick<Step, "id">): str | ||
| 90 | 90 | } |
| 91 | 91 | return out; |
| 92 | 92 | } |
| 93 | ||
| 94 | /** | |
| 95 | * The lines of the file that name each step's id, found in one pass: a quoted string of a line is | |
| 96 | * looked up among the ids, so a long session is read once, not once a step. `keep` is applied to | |
| 97 | * a line before it is cut (the shared page redacts there), at most `RAW_LINES` a step. | |
| 98 | */ | |
| 99 | export function rawLinesByStep(lines: Iterable<string>, steps: readonly Pick<Step, "id">[], keep: (line: string) => string = (l) => l): string[][] { | |
| 100 | const at = new Map<string, number>(); | |
| 101 | steps.forEach((s, i) => { | |
| 102 | if (!at.has(s.id)) at.set(s.id, i); | |
| 103 | }); | |
| 104 | const out: string[][] = steps.map(() => []); | |
| 105 | for (const line of lines) { | |
| 106 | const hit = new Set<number>(); | |
| 107 | for (const m of line.matchAll(/"([^"\\]{1,200})"/g)) { | |
| 108 | const i = at.get(m[1]!); | |
| 109 | if (i !== undefined && out[i]!.length < RAW_LINES) hit.add(i); | |
| 110 | } | |
| 111 | if (hit.size === 0) continue; | |
| 112 | const kept = keep(line); | |
| 113 | const cut = kept.length > RAW_CHARS ? `${kept.slice(0, RAW_CHARS)}…` : kept; | |
| 114 | for (const i of hit) out[i]!.push(cut); | |
| 115 | } | |
| 116 | return out; | |
| 117 | } |
src/trace/who.ts added +35
| @@ -0,0 +1,35 @@ | ||
| 1 | /** | |
| 2 | * What a shared session leaves out of each raw record (#18377): who made it and where, never what | |
| 3 | * it did. Only the record's own level is read (its top level, `payload` as a Codex record keeps its | |
| 4 | * session header there, and `message.metadata`), so a tool's input that happens to hold a `user`, | |
| 5 | * `host` or `git` key is kept whole. Plain data in, plain data out. | |
| 6 | */ | |
| 7 | ||
| 8 | /** Keys that say who or where the session was made. */ | |
| 9 | const WHO_KEYS = new Set(["cwd", "workdir", "working_directory", "gitBranch", "userEmail", "email", "user", "username", "hostname", "host", "originator", "git"]); | |
| 10 | ||
| 11 | type Obj = { [k: string]: unknown }; | |
| 12 | const isObj = (v: unknown): v is Obj => v !== null && typeof v === "object" && !Array.isArray(v); | |
| 13 | ||
| 14 | /** `o` without its who-keys; any of `into` that is an object is cleaned the same way. */ | |
| 15 | function cleaned(o: Obj, into: readonly string[]): Obj { | |
| 16 | const out: Obj = {}; | |
| 17 | for (const [k, v] of Object.entries(o)) { | |
| 18 | if (WHO_KEYS.has(k)) continue; | |
| 19 | out[k] = into.includes(k) && isObj(v) ? cleaned(v, k === "message" ? ["metadata"] : []) : v; | |
| 20 | } | |
| 21 | return out; | |
| 22 | } | |
| 23 | ||
| 24 | /** | |
| 25 | * A record without the person and the machine around it; a line that is not JSON is kept as it is. | |
| 26 | */ | |
| 27 | export function withoutWho(line: string): string { | |
| 28 | let rec: unknown; | |
| 29 | try { | |
| 30 | rec = JSON.parse(line); | |
| 31 | } catch { | |
| 32 | return line; | |
| 33 | } | |
| 34 | return isObj(rec) ? JSON.stringify(cleaned(rec, ["payload", "message"])) : line; | |
| 35 | } |
src/tui/screens/traces.ts +22 −3
| @@ -10,7 +10,9 @@ | ||
| 10 | 10 | * page's own `readFilter`. `b` types the name of a saved search (the page's links) and opens the |
| 11 | 11 | * first that starts with it, `w` names the filters on screen and keeps them, `x` removes the saved |
| 12 | 12 | * search last opened (the search only: no trace is touched); a facet a saved search holds that no |
| 13 | * longer exists is dropped and said. Esc closes. The lines are `traceLine`, the page's own row. | |
| 13 | * longer exists is dropped and said. On an imported session `s` makes its share link (shown once; | |
| 14 | * a second press confirms when one is live, as it ends the old) and `z` ends it (#18377). Esc | |
| 15 | * closes. The lines are `traceLine`, the page's own row. | |
| 14 | 16 | */ |
| 15 | 17 | import { binding } from "../app/keys.ts"; |
| 16 | 18 | import type { Ctx, Screen } from "../app/app.ts"; |
| @@ -20,6 +22,7 @@ import { getRun } from "../../core/runs.ts"; | ||
| 20 | 22 | import { openedSaved, removeSaved, SAVE_WORDS, saveSearch, savedWords, NAME_MAX, type OpenedSearch } from "../../trace/saved.ts"; |
| 21 | 23 | import { sameIssue } from "../../trace/compare.ts"; |
| 22 | 24 | import { traceScreen } from "./trace.ts"; |
| 25 | import { importedSharedNow, sharedHref, shareImported, unshareImported } from "../../actions/trace_share.ts"; | |
| 23 | 26 | import { Scroll, openOnWeb } from "./common.ts"; |
| 24 | 27 | import { style } from "../term/style.ts"; |
| 25 | 28 | import { truncate } from "../term/width.ts"; |
| @@ -58,6 +61,7 @@ function tracesScreen(f: Session, ctx: Ctx): Screen { | ||
| 58 | 61 | const made = saveSearch(f.app.db, f.user!.id, name, q.toString()); |
| 59 | 62 | ctx.toast(made.ok ? `saved “${made.value.name}”` : SAVE_WORDS[made.error], made.ok ? undefined : "attn"); |
| 60 | 63 | }; |
| 64 | let confirmShare: string | undefined; | |
| 61 | 65 | let typing: "q" | "tool" | "file" | "from" | "to" | undefined; |
| 62 | 66 | const read = (): void => { |
| 63 | 67 | list = listTraces(f.app, f.user, filter()); |
| @@ -81,7 +85,7 @@ function tracesScreen(f: Session, ctx: Ctx): Screen { | ||
| 81 | 85 | }; |
| 82 | 86 | return { |
| 83 | 87 | title: "Traces", |
| 84 | keys: [binding(["j", "k"], "move"), binding(["enter"], "open the trace"), binding(["v"], "mark"), binding(["d"], "compare marked"), binding(["n"], "older"), binding(["/"], "search"), binding(["i", "r", "m", "o", "p"], "kind, repo, model, outcome, person"), binding(["u", "e"], "tool used, file touched"), binding(["f", "t"], "from, to day"), binding(["b", "w", "x"], "open, save, remove a saved search"), binding(["y"], "import a session on the web"), binding(["c"], "clear filters"), binding(["esc"], "close")], | |
| 88 | keys: [binding(["j", "k"], "move"), binding(["enter"], "open the trace"), binding(["v"], "mark"), binding(["d"], "compare marked"), binding(["n"], "older"), binding(["/"], "search"), binding(["i", "r", "m", "o", "p"], "kind, repo, model, outcome, person"), binding(["u", "e"], "tool used, file touched"), binding(["f", "t"], "from, to day"), binding(["b", "w", "x"], "open, save, remove a saved search"), binding(["y"], "import a session on the web"), binding(["s", "z"], "share an imported session, stop sharing"), binding(["c"], "clear filters"), binding(["esc"], "close")], | |
| 85 | 89 | render(area, s) { |
| 86 | 90 | const c = ctx.theme.c; |
| 87 | 91 | s.text(area.x, area.y, truncate(`${typing === undefined ? "/ to search" : typing === "q" ? "Search" : typing === "from" ? "From (YYYY-MM-DD)" : typing === "to" ? "To (YYYY-MM-DD)" : typing === "tool" ? "Tool (Edit, Bash…)" : "File (path)"}: ${typing === undefined || typing === "q" ? (facet.q ?? "") : (facet[typing] ?? "")}${typing === undefined ? "" : "_"}${summary()}`, area.w), style(c.fg, c.bg), area.x + area.w); |
| @@ -157,7 +161,22 @@ function tracesScreen(f: Session, ctx: Ctx): Screen { | ||
| 157 | 161 | else ctx.push(traceScreen(f, ctx, repo, ra.id, rb.id)); |
| 158 | 162 | } |
| 159 | 163 | } else if (e.key === "y") openOnWeb(f, ctx, "/traces#traces-import", "the import form (a file input)"); |
| 160 | else if (e.key === "c") { | |
| 164 | else if (e.key === "s" || e.key === "z") { | |
| 165 | const r = list.rows[at]; | |
| 166 | const live = r?.kind === "imported" ? importedSharedNow(f.app, f.user, r.id) : undefined; | |
| 167 | if (r === undefined || live === undefined) ctx.toast("share is for an imported session of yours: move to one", "attn"); | |
| 168 | else if (e.key === "s" && live && confirmShare !== r.id) { | |
| 169 | confirmShare = r.id; | |
| 170 | ctx.toast("a share link is live: press s again to replace it, and end the old one", "attn"); | |
| 171 | } else { | |
| 172 | confirmShare = undefined; | |
| 173 | const done = e.key === "s" ? shareImported(f.app, f.user, r.id) : unshareImported(f.app, f.user, r.id); | |
| 174 | if (!done.ok) ctx.toast("that could not be done", "danger"); | |
| 175 | else if ("token" in done.value) openOnWeb(f, ctx, sharedHref(done.value.token), "the share link (shown once)"); | |
| 176 | else ctx.toast(done.value.was ? "sharing stopped: the link no longer works" : "this session was not shared", "success"); | |
| 177 | } | |
| 178 | if (e.key === "z") confirmShare = undefined; | |
| 179 | } else if (e.key === "c") { | |
| 161 | 180 | for (const k of Object.keys(facet)) delete facet[k as keyof typeof facet]; |
| 162 | 181 | before = undefined; |
| 163 | 182 | read(); |
src/web/pages/trace_imported.ts +6
| @@ -4,6 +4,7 @@ | ||
| 4 | 4 | * not-found an unknown id gets, `seenTraceRow`). The file holds no cost and often no tokens; the |
| 5 | 5 | * page says so rather than showing zero. Raw lines load per step, as on a run's page. |
| 6 | 6 | */ |
| 7 | import { importedSharedNow } from "../../actions/trace_share.ts"; | |
| 7 | 8 | import type { Ctx } from "../../http/context.ts"; |
| 8 | 9 | import { securityFloor } from "../../http/respond.ts"; |
| 9 | 10 | import { importedLines } from "../../trace/import.ts"; |
| @@ -35,6 +36,7 @@ export function importedTracePage(ctx: Ctx, row: TraceRow): Response { | ||
| 35 | 36 | const base = `/traces/${row.id}`; |
| 36 | 37 | const timed = read.timed; |
| 37 | 38 | const failed = steps.filter((s) => s.outcome === "error").length; |
| 39 | const shared = importedSharedNow(ctx.app, ctx.auth.user, row.id); | |
| 38 | 40 | const tokens = row.tokensIn + row.tokensCached + row.tokensOut; |
| 39 | 41 | const content = html`${pageHead(html`Trace: ${row.title}`, { sub: `An imported ${read.trace.source} session, step by step. Only you can read it.` })} |
| 40 | 42 | <dl class="row-facts" id="trace-total"> |
| @@ -45,6 +47,10 @@ export function importedTracePage(ctx: Ctx, row: TraceRow): Response { | ||
| 45 | 47 | <div><dt>Cost</dt><dd id="trace-cost">${row.costUsd > 0 ? `$${row.costUsd.toFixed(2)}` : "not recorded in this file"}</dd></div> |
| 46 | 48 | </dl> |
| 47 | 49 | <nav class="bar small" id="trace-links" aria-label="Where this trace is"><a href="/traces">All traces</a></nav> |
| 50 | ${shared === undefined ? null : html`<form method="post" class="bar small" id="trace-share" action="${base}/${shared ? "unshare" : "share"}"> | |
| 51 | <span class="muted">${shared ? "A share link is live: anyone holding it reads this session, secrets redacted." : "Not shared. A link lets anyone who holds it read this session. The forge hides the credentials it knows, but a file can hold others, so share only what you have read."}</span> | |
| 52 | <button type="submit">${shared ? "Stop sharing" : "Make a share link"}</button> | |
| 53 | </form>`} | |
| 48 | 54 | ${read.truncated ? html`<p class="small muted" id="trace-cut">The session is longer than the page reads; only its first steps are shown.</p>` : null} |
| 49 | 55 | ${total === 0 ? html`<p class="muted" id="trace-empty">This session holds no steps.</p>` : html`${strip(steps, timed)} |
| 50 | 56 | <div class="trace-tree" id="trace-tree" data-base="${base}" role="group" aria-label="Steps">${treeRows(steps, base, timed)}</div> |
test/trace-who.test.ts added +25
| @@ -0,0 +1,25 @@ | ||
| 1 | /** | |
| 2 | * What a shared session drops of who and where (#18377): the record's own level, nothing of a | |
| 3 | * tool's input. | |
| 4 | */ | |
| 5 | import { expect, test } from "bun:test"; | |
| 6 | import { withoutWho } from "../src/trace/who.ts"; | |
| 7 | ||
| 8 | const back = (o: object): unknown => JSON.parse(withoutWho(JSON.stringify(o))); | |
| 9 | ||
| 10 | test("top-level, payload and message.metadata who-keys go; the rest stays", () => { | |
| 11 | expect(back({ type: "x", cwd: "/home/a", gitBranch: "m", userEmail: "[email protected]", n: 1 })).toEqual({ type: "x", n: 1 }); | |
| 12 | expect(back({ type: "session_meta", payload: { id: "s", cwd: "/home/a", git: { repository_url: "https://h/r" }, originator: "cli", keep: [1] } })).toEqual({ type: "session_meta", payload: { id: "s", keep: [1] } }); | |
| 13 | expect(back({ message: { role: "user", metadata: { email: "[email protected]", user: "al", model: "m" }, content: "hi" } })).toEqual({ message: { role: "user", metadata: { model: "m" }, content: "hi" } }); | |
| 14 | }); | |
| 15 | ||
| 16 | test("a tool's input keeps its own user, host and git keys", () => { | |
| 17 | const rec = { cwd: "/w", message: { content: [{ type: "tool_use", input: { user: "deploy", host: "db1", git: "status", list: [{ email: "[email protected]" }] } }] } }; | |
| 18 | expect(back(rec)).toEqual({ message: rec.message }); | |
| 19 | }); | |
| 20 | ||
| 21 | test("a line that is not a JSON object is kept as it is", () => { | |
| 22 | expect(withoutWho("not json cwd")).toBe("not json cwd"); | |
| 23 | expect(withoutWho("[1,2]")).toBe("[1,2]"); | |
| 24 | expect(withoutWho("\"cwd\"")).toBe("\"cwd\""); | |
| 25 | }); |
new above
offline
Add tests for the terminal
s/zkeys, forwithoutWhoon nested keys and non-JSON lines, and one request-level check of the 60-a-minute limit.