Public: anyone can read this; the crew can change it.
Traces 5: compare two traces, and share one with secrets redacted #18017
Merged 6h 41m ago, live in production
torque/forge:torque/17995-traces-5-compare-two-traces-and-share-on → main 0 commits fork 5 threads · 5 unresolved
30 files
changed +857
−9
- docs/design/areas/traces.md
- docs/features/traces.md
- src/actions/trace_share.ts
- src/core/account_purge.ts
- src/core/migration-pins/20261204025000-trace-share.pin
- src/core/migrations/20261204025000-trace-share.ts
- src/core/runs.ts
- src/crew/conversation_ask.ts
- src/crew/conversation_confirm.ts
- src/http/lazy_lists.ts
- src/http/mcp/tools/index.ts
- src/http/mcp/tools/trace.ts
- src/http/routes/operations.ts
- src/trace/compare.ts
- src/trace/share.ts
- src/trace/shared.ts
- src/tui/screens/trace.ts
- src/web/pages/trace.ts
- src/web/pages/trace_compare.ts
- src/web/pages/trace_share.ts
- test/budgets/unfixtured/traces-id-compare-other.json
- test/budgets/unfixtured/traces-shared-token.json
- test/conversation-ask.test.ts
- test/hot-reads-indexed.test.ts
- test/mcp-forge-protocol.test.ts
- test/mixed-actions-held.test.ts
- test/no-polling.test.ts
- test/open-routes-floor.test.ts
- test/security-floor-routes.test.ts
- test/trace-share.test.ts
docs/design/areas/traces.md +2
| @@ -3,3 +3,5 @@ | ||
| 3 | 3 | `/traces/:id` (`src/web/pages/trace.ts`), one crew run as a tree of steps beside a strip of them. Page-local classes, drawn from existing tokens: `.trace-strip` (the inline SVG, fixed 28px tall so nothing shifts; `.trace-tick-model`, `.trace-tick-tool` and `.trace-tick-bad` colour a tick by what the step was), `.trace-tree` (the collapsible tree, a group of `details`), `.trace-calls` (a step's tool calls, indented), `.trace-step` (one step's `details`; `.trace-bad` turns a failed one `--danger`, `.trace-here` rings the step a `#step-N` address names), `.trace-raw` (a step's raw lines, loaded when it opens, scrolls inside 24rem) and `.trace-n` (the step's number, quiet). |
| 4 | 4 | |
| 5 | 5 | `/traces` (`src/web/pages/traces.ts`), the list. Page-local classes, drawn from existing tokens: `.traces-search` (the search form: the input grows, the button and the Filters disclosure wrap under it on a phone) and `.traces-facets` (the facet selects and dates, an auto-fit grid of `.field`s). `#traces-list` only ellipsises a row's title and meta to one line each. A removable active filter is the shared `.badge` with the `x` icon inside its link. |
| 6 | ||
| 7 | `/traces/:id/compare/:other` (`src/web/pages/trace_compare.ts`) is one `table` of the shared table class, A and B as column heads and B − A last; `/traces/shared/:token` (`src/web/pages/trace_share.ts`) is the same `details` rows with the raw lines inline and no script. No new classes. |
docs/features/traces.md +9 −1
| @@ -22,6 +22,14 @@ Each line a run appends is stamped in `<run id>.times` beside its log (`appendRu | ||
| 22 | 22 | |
| 23 | 23 | Not yet: Talk conversations and outside imports as rows (they wait for the trace index of #17991), a kind facet (every row is a crew run), archived turns in the search, and a JSON twin of the list. |
| 24 | 24 | |
| 25 | Modules: `src/trace/model.ts` (the shape and totals), `src/trace/claude_code.ts` and `src/trace/codex.ts` (the readers), `src/trace/session.ts` (which one), `src/trace/otlp.ts` (the export). Not yet: storing an import, tokens and cost per step (the file holds none), compare, share. | |
| 25 | ## Compare two runs, and share one (#17995) | |
| 26 | ||
| 27 | `/traces/<run id>/compare/<other run id>` sets two runs of one issue side by side: steps, failed steps, time (each run's own clock, started to finished), tokens (said "not recorded": a run's log holds none), cost (for those who may see the owner's facts) and outcome, with B − A beside each. Runs of two issues, or one the viewer cannot read, are the not-found a missing run gets. A trace page lists the issue's other runs to compare with. Reader: `src/trace/compare.ts` (`compareViews`, `compareRows`); page: `src/web/pages/trace_compare.ts`. | |
| 28 | ||
| 29 | A share link is off until the repository's owner makes one (**Make a share link** on the trace; `POST /traces/<run id>/share`, same-origin). It is `/traces/shared/<secret>`, shown once (only its hash is kept, `src/trace/share.ts`), and read by anyone holding it with no sign-in: the steps and their raw lines, every one passed through the forge's redaction (`redactHeld`) on the way out, so a token the log's writer missed is `[redacted]`; no cost, no people. One live link a run: a new one ends the old, **Stop sharing** ends it, and the link is then the not-found any page gets. The gate is `src/actions/trace_share.ts`; pages `src/web/pages/trace_share.ts`, reader `src/trace/shared.ts`, which keeps the redacted trace for 30 seconds per link and allows one address 60 opens a minute (then a 429 with `Retry-After`). | |
| 30 | ||
| 31 | Terminal: on a run's trace (`src/tui/screens/trace.ts`) `c` reads it against the issue's newest other run, `s` makes the link (shown to copy; when one is live a second press confirms, as it ends the old one), `x` ends it; both are on the single-trace screen only, and a non-owner is told only the owner shares. MCP: the `trace` tool, `action` `list`, `read`, `compare` (`with`), `share`, `unshare`. | |
| 32 | ||
| 33 | Modules: `src/trace/model.ts` (the shape and totals), `src/trace/claude_code.ts` and `src/trace/codex.ts` (the readers), `src/trace/session.ts` (which one), `src/trace/otlp.ts` (the export). Not yet: storing an import, tokens and cost per step (the file holds none). | |
| 26 | 34 | |
| 27 | 35 | The file is read whole, then parsed, so the 256 MB limit is memory-bound: a session near it holds a few copies at once. Streaming the read is a later step. |
src/core/account_purge.ts +2
| @@ -52,6 +52,8 @@ export const COVERED: Readonly<Record<string, Coverage>> = { | ||
| 52 | 52 | "access_link.created_by": FORMER, |
| 53 | 53 | // Who asked a game box to run a command: the record keeps it, as the former account. |
| 54 | 54 | "game_command.by": FORMER, |
| 55 | // A trace share link is its maker's to give, like a client pack link: it ends with the person. | |
| 56 | "trace_share.created_by": DELETE, | |
| 55 | 57 | // The link a person made to a game box's client pack ends with the person: it is theirs to give, and the row goes. |
| 56 | 58 | "game_client_link.created_by": DELETE, |
| 57 | 59 | // The !forge audit keeps what was said and what came of it; who said it becomes a former member. |
src/core/runs.ts +13
| @@ -2198,3 +2198,16 @@ export function lastPullAt(db: Database, appId: string): number | undefined { | ||
| 2198 | 2198 | const at = Date.parse(row.created_at); |
| 2199 | 2199 | return Number.isFinite(at) ? at : undefined; |
| 2200 | 2200 | } |
| 2201 | ||
| 2202 | /** | |
| 2203 | * The other agent runs of one issue or pull, newest first, at most `limit` (#17995): what a trace | |
| 2204 | * is compared with. Walks `run_issue (issue_id, id DESC)`. | |
| 2205 | */ | |
| 2206 | export function otherRunsOfIssue(db: Database, issueId: string, exceptRunId: string, limit = 20): Run[] { | |
| 2207 | return db | |
| 2208 | .query<RunRow, [string, string, number]>( | |
| 2209 | "SELECT * FROM run WHERE issue_id = ?1 AND id <> ?2 AND kind = 'agent' ORDER BY id DESC LIMIT ?3", | |
| 2210 | ) | |
| 2211 | .all(issueId, exceptRunId, limit) | |
| 2212 | .map(toRun); | |
| 2213 | } |
src/crew/conversation_ask.ts +4
| @@ -188,6 +188,10 @@ export const ASKED_AS: Readonly<Record<string, Form>> = { | ||
| 188 | 188 | close: (a) => |
| 189 | 189 | a["state"] === "open" ? `Reopen ${item(a)}` : `Close ${item(a)}${clause(" as ", verb(a, "reason", { done: "done", dropped: "dropped", split: "split up" }, ""))}`, |
| 190 | 190 | merge: (a) => `Merge ${pull(a)}`, |
| 191 | trace: (a) => | |
| 192 | a["action"] === "unshare" | |
| 193 | ? `Stop sharing the trace of run ${val(a, "run")} in ${repo(a)}` | |
| 194 | : `Make a redacted share link for the trace of run ${val(a, "run")} in ${repo(a)}`, | |
| 191 | 195 | cut_wave: (a) => `Cut the next wave of ${repo(a, "the app")} as a draft pull`, |
| 192 | 196 | wall: (a) => (a["answer"] === "send_back" ? `Send ${pull(a)} back at the access wall` : `Allow ${pull(a)} at the access wall and merge it`), |
| 193 | 197 | edit: (a) => |
src/crew/conversation_confirm.ts +3
| @@ -63,6 +63,9 @@ export const ASKING: Readonly<Record<string, Asking>> = { | ||
| 63 | 63 | undo: (a) => `\`archive\` with \`archived: ${a["archived"] === false}\` turns it back`, |
| 64 | 64 | }, |
| 65 | 65 | board_delete: {}, |
| 66 | // Making or ending a redacted share link for a run trace (#17995); reading and comparing never | |
| 67 | // ask. | |
| 68 | trace: { asks: (a) => a["action"] === "share" || a["action"] === "unshare" }, | |
| 66 | 69 | // One action over every row a fleet query selects (#450); only `apply: true` writes. |
| 67 | 70 | flow: { |
| 68 | 71 | undo: () => "`limb` with `action: undo` and the receipt's id reverts it for 24 hours", |
src/http/lazy_lists.ts +4
| @@ -698,6 +698,10 @@ export const OPERATIONS_ROUTES = [ | ||
| 698 | 698 | ["GET", "/traces"], |
| 699 | 699 | ["GET", "/traces/:id"], |
| 700 | 700 | ["GET", "/traces/:id/steps/:n"], |
| 701 | ["GET", "/traces/:id/compare/:other"], | |
| 702 | ["POST", "/traces/:id/share"], | |
| 703 | ["POST", "/traces/:id/unshare"], | |
| 704 | ["GET", "/traces/shared/:token"], | |
| 701 | 705 | ] as const; |
| 702 | 706 | |
| 703 | 707 | /** `originsRoutes` (`routes/origins.ts`). */ |
src/http/mcp/tools/index.ts +1
| @@ -209,6 +209,7 @@ export { tool as templates } from "./templates.ts"; | ||
| 209 | 209 | export { tool as thought } from "./thought.ts"; |
| 210 | 210 | export { tool as threads } from "./threads.ts"; |
| 211 | 211 | export { tool as tokens } from "./tokens.ts"; |
| 212 | export { tool as trace } from "./trace.ts"; | |
| 212 | 213 | export { tool as trail } from "./trail.ts"; |
| 213 | 214 | export { tool as upstream } from "./upstream.ts"; |
| 214 | 215 | export { tool as usage } from "./usage.ts"; |
src/http/mcp/tools/trace.ts added +60
| @@ -0,0 +1,60 @@ | ||
| 1 | import { listRepoRuns } from "../../../actions/runs.ts"; | |
| 2 | import { sharedHref, shareRun, unshareRun } from "../../../actions/trace_share.ts"; | |
| 3 | import { compareRows, compareViews, readCompared } from "../../../trace/compare.ts"; | |
| 4 | import { readTrace, traceHref, traceRows } from "../../../trace/read.ts"; | |
| 5 | import { good, bad, type Tool, REPO_ARG, repoArg, isAnswer } from "../shared.ts"; | |
| 6 | ||
| 7 | const ID = /^[0-9A-Za-z_-]{1,64}$/; | |
| 8 | const ACTIONS = ["read", "list", "compare", "share", "unshare"] as const; | |
| 9 | ||
| 10 | export const tool: Tool = { | |
| 11 | name: "trace", | |
| 12 | doc: "`{ repo, action?, run?, with? }`: a crew run read as a trace (#17995). `list` (default without `run`): the repository's recent agent runs, each with its `trace` address; `read` (default with `run`): the trace as lines, one a step, as the page and terminal show; `compare` with `with`: two runs of one issue side by side, steps, failed, time, tokens, cost and outcome with B − A; `share`: the repository owner's share link for the run (secrets redacted, off until made, shown once); `unshare`: end it", | |
| 13 | description: "Read, list or compare crew-run traces; an owner makes or ends a redacted share link.", | |
| 14 | inputSchema: { | |
| 15 | type: "object", | |
| 16 | properties: { | |
| 17 | repo: REPO_ARG, | |
| 18 | action: { type: "string", enum: [...ACTIONS] }, | |
| 19 | run: { type: "string" }, | |
| 20 | with: { type: "string", description: "compare: the other run of the same issue" }, | |
| 21 | }, | |
| 22 | required: ["repo"], | |
| 23 | }, | |
| 24 | writes: true, | |
| 25 | heldNotWhen: (args) => args["action"] !== "share" && args["action"] !== "unshare", | |
| 26 | async run(ctx, user, args) { | |
| 27 | const repo = repoArg(ctx, user, args); | |
| 28 | if (isAnswer(repo)) return repo; | |
| 29 | const run = args["run"]; | |
| 30 | if (run !== undefined && (typeof run !== "string" || !ID.test(run))) return bad("bad_run", "run is a run id"); | |
| 31 | const action = args["action"] ?? (run === undefined ? "list" : "read"); | |
| 32 | if (typeof action !== "string" || !ACTIONS.some((a) => a === action)) return bad("bad_action", `action is one of ${ACTIONS.join(", ")}`); | |
| 33 | if (action === "list") { | |
| 34 | const runs = listRepoRuns(ctx.app, user, repo, { limit: 20, kind: "agent" }); | |
| 35 | if (!runs.ok) return bad("not_found", "no such app or repository (owner/name)"); | |
| 36 | return good({ runs: runs.value.map((r) => ({ id: r.id, state: r.state, trace: traceHref(r.id) })) }); | |
| 37 | } | |
| 38 | if (run === undefined) return bad("bad_run", "name the `run`"); | |
| 39 | if (action === "read") { | |
| 40 | const view = await readTrace(ctx.app, user, repo, run); | |
| 41 | return view === undefined ? bad("not_found", "no such run here") : good({ trace: traceHref(run), rows: traceRows(view) }); | |
| 42 | } | |
| 43 | if (action === "compare") { | |
| 44 | const other = args["with"]; | |
| 45 | if (typeof other !== "string" || !ID.test(other)) return bad("bad_with", "name `with`: the other run of the same issue"); | |
| 46 | const both = await readCompared(ctx.app, user, repo, run, other); | |
| 47 | if (both === undefined) return bad("not_found", "no such pair of runs: both must be readable runs of one issue"); | |
| 48 | const c = compareViews(both.a, both.b); | |
| 49 | return good({ rows: compareRows(both.a, both.b), table: c.rows, timeDiffMs: c.timeDiffMs ?? null, costDiffUsd: c.costDiffUsd ?? null }); | |
| 50 | } | |
| 51 | if (action === "share") { | |
| 52 | const made = shareRun(ctx.app, user, repo, run); | |
| 53 | if (!made.ok) return bad(made.error, made.error === "forbidden" ? "only the repository's owner makes a share link" : "no such run here"); | |
| 54 | return good({ says: "Share link made: shown once, secrets redacted.", link: `${ctx.url.origin}${sharedHref(made.value.token)}` }); | |
| 55 | } | |
| 56 | const ended = unshareRun(ctx.app, user, repo, run); | |
| 57 | if (!ended.ok) return bad(ended.error, ended.error === "forbidden" ? "only the repository's owner ends a share link" : "no such run here"); | |
| 58 | return good({ says: ended.value.was ? "Sharing stopped: the link no longer works." : "This trace was not shared." }); | |
| 59 | }, | |
| 60 | }; |
src/http/routes/operations.ts +7
| @@ -23,6 +23,8 @@ 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 | 25 | import { tracePage, traceStep } from "../../web/pages/trace.ts"; |
| 26 | import { traceComparePage } from "../../web/pages/trace_compare.ts"; | |
| 27 | import { traceShare, traceShared, traceUnshare } from "../../web/pages/trace_share.ts"; | |
| 26 | 28 | import { tracesPage } from "../../web/pages/traces.ts"; |
| 27 | 29 | import { oauthCallbackUri } from "../api.ts"; |
| 28 | 30 | import { gamesRoutes } from "../games_routes.ts"; |
| @@ -124,4 +126,9 @@ export function operationsRoutes(router: Router<Handler>, knownPatterns: () => s | ||
| 124 | 126 | router.add("GET", "/traces", tracesPage); |
| 125 | 127 | router.add("GET", "/traces/:id", tracePage); |
| 126 | 128 | router.add("GET", "/traces/:id/steps/:n", traceStep); |
| 129 | // Two runs of one issue side by side, and a trace shared by a link its owner made (#17995). | |
| 130 | router.add("GET", "/traces/:id/compare/:other", traceComparePage); | |
| 131 | router.add("POST", "/traces/:id/share", traceShare); | |
| 132 | router.add("POST", "/traces/:id/unshare", traceUnshare); | |
| 133 | router.add("GET", "/traces/shared/:token", traceShared); | |
| 127 | 134 | } |
src/trace/compare.ts added +104
| @@ -0,0 +1,104 @@ | ||
| 1 | /** | |
| 2 | * Two runs of one issue side by side (#17995), as plain rows: steps, failed steps, time, tokens, | |
| 3 | * cost, outcome, each with the difference (B − A). Time is the run's own wall clock (started to | |
| 4 | * finished); the cost is shown only where the viewer may see the owner's facts (`costUsd`, as the | |
| 5 | * trace page's). A run's log holds no tokens, so a run with none says "not recorded" rather than 0. | |
| 6 | */ | |
| 7 | import { stepsOf, type Step } from "./model.ts"; | |
| 8 | import type { App } from "../app.ts"; | |
| 9 | import type { Repo } from "../core/repos.ts"; | |
| 10 | import type { User } from "../core/users.ts"; | |
| 11 | import { readTrace, spanWords, type TraceView } from "./read.ts"; | |
| 12 | ||
| 13 | export interface CompareRow { | |
| 14 | readonly label: string; | |
| 15 | readonly a: string; | |
| 16 | readonly b: string; | |
| 17 | /** B − A in words, where both sides have the figure; otherwise empty. */ | |
| 18 | readonly diff: string; | |
| 19 | } | |
| 20 | ||
| 21 | export interface Comparison { | |
| 22 | readonly rows: readonly CompareRow[]; | |
| 23 | /** B − A in numbers, undefined where a side lacks the figure (units: ms, USD). */ | |
| 24 | readonly timeDiffMs: number | undefined; | |
| 25 | readonly costDiffUsd: number | undefined; | |
| 26 | } | |
| 27 | ||
| 28 | /** The run's wall time in ms, from its own record; undefined while it has not both ends. */ | |
| 29 | export function wallMs(run: { startedAt: string | null; finishedAt: string | null }): number | undefined { | |
| 30 | if (run.startedAt === null || run.finishedAt === null) return undefined; | |
| 31 | const ms = Date.parse(run.finishedAt) - Date.parse(run.startedAt); | |
| 32 | return Number.isFinite(ms) && ms >= 0 ? ms : undefined; | |
| 33 | } | |
| 34 | ||
| 35 | const signed = (n: number, unit: (abs: number) => string): string => (n === 0 ? "same" : `${n > 0 ? "+" : "−"}${unit(Math.abs(n))}`); | |
| 36 | const money = (usd: number): string => `$${usd.toFixed(2)}`; | |
| 37 | const failed = (s: readonly Step[]): number => s.filter((x) => x.outcome === "error").length; | |
| 38 | const tokensOf = (s: readonly Step[]): number => s.reduce((n, x) => n + x.tokensIn + x.tokensOut, 0); | |
| 39 | ||
| 40 | export function compareViews(a: TraceView, b: TraceView): Comparison { | |
| 41 | const sa = stepsOf(a.trace); | |
| 42 | const sb = stepsOf(b.trace); | |
| 43 | const ta = wallMs(a.run); | |
| 44 | const tb = wallMs(b.run); | |
| 45 | const ka = tokensOf(sa); | |
| 46 | const kb = tokensOf(sb); | |
| 47 | const timeDiffMs = ta === undefined || tb === undefined ? undefined : tb - ta; | |
| 48 | const costDiffUsd = a.costUsd === undefined || b.costUsd === undefined ? undefined : Math.round((b.costUsd - a.costUsd) * 1e6) / 1e6; | |
| 49 | const rows: CompareRow[] = [ | |
| 50 | { label: "Steps", a: String(sa.length), b: String(sb.length), diff: signed(sb.length - sa.length, String) }, | |
| 51 | { label: "Failed steps", a: String(failed(sa)), b: String(failed(sb)), diff: signed(failed(sb) - failed(sa), String) }, | |
| 52 | { | |
| 53 | label: "Time", | |
| 54 | a: ta === undefined ? "not finished" : spanWords(ta), | |
| 55 | b: tb === undefined ? "not finished" : spanWords(tb), | |
| 56 | diff: timeDiffMs === undefined ? "" : signed(timeDiffMs, spanWords), | |
| 57 | }, | |
| 58 | { | |
| 59 | label: "Tokens", | |
| 60 | a: ka === 0 ? "not recorded" : String(ka), | |
| 61 | b: kb === 0 ? "not recorded" : String(kb), | |
| 62 | diff: ka === 0 || kb === 0 ? "" : signed(kb - ka, String), | |
| 63 | }, | |
| 64 | { | |
| 65 | label: "Cost", | |
| 66 | a: a.costUsd === undefined ? "not shown" : money(a.costUsd), | |
| 67 | b: b.costUsd === undefined ? "not shown" : money(b.costUsd), | |
| 68 | diff: costDiffUsd === undefined ? "" : signed(costDiffUsd, money), | |
| 69 | }, | |
| 70 | { label: "Outcome", a: a.run.state, b: b.run.state, diff: a.run.state === b.run.state ? "same" : "differs" }, | |
| 71 | ]; | |
| 72 | return { rows, timeDiffMs, costDiffUsd }; | |
| 73 | } | |
| 74 | ||
| 75 | /** The comparison as terminal lines: the two runs, then `label: A | B (diff)` a row. */ | |
| 76 | export function compareRows(a: TraceView, b: TraceView): string[] { | |
| 77 | const c = compareViews(a, b); | |
| 78 | return [ | |
| 79 | `A: ${a.title} (${a.run.id})`, | |
| 80 | `B: ${b.title} (${b.run.id})`, | |
| 81 | ...c.rows.map((r) => `${r.label}: ${r.a} | ${r.b}${r.diff === "" ? "" : ` (${r.diff})`}`), | |
| 82 | ]; | |
| 83 | } | |
| 84 | ||
| 85 | /** Two runs may be compared when they are two different runs of one issue or pull. */ | |
| 86 | export function sameIssue(a: Pick<TraceView["run"], "id" | "issueId">, b: Pick<TraceView["run"], "id" | "issueId">): boolean { | |
| 87 | return a.id !== b.id && a.issueId !== null && a.issueId === b.issueId; | |
| 88 | } | |
| 89 | ||
| 90 | export interface Compared { | |
| 91 | readonly a: TraceView; | |
| 92 | readonly b: TraceView; | |
| 93 | readonly comparison: Comparison; | |
| 94 | } | |
| 95 | ||
| 96 | /** | |
| 97 | * Both runs' traces through the one gate (`readTrace`) and their difference: `undefined` where | |
| 98 | * either cannot be read or the two are not runs of one issue, so a stranger learns nothing. | |
| 99 | */ | |
| 100 | export async function readCompared(app: App, actor: User | undefined, repo: Repo, aId: string, bId: string): Promise<Compared | undefined> { | |
| 101 | const [a, b] = await Promise.all([readTrace(app, actor, repo, aId), readTrace(app, actor, repo, bId)]); | |
| 102 | if (a === undefined || b === undefined || !sameIssue(a.run, b.run)) return undefined; | |
| 103 | return { a, b, comparison: compareViews(a, b) }; | |
| 104 | } |
src/tui/screens/trace.ts +50 −2
| @@ -5,6 +5,10 @@ | ||
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | import type { Repo } from "../../core/repos.ts"; |
| 8 | import { isShared } from "../../trace/share.ts"; | |
| 9 | import { sharedHref, shareRun, unshareRun } from "../../actions/trace_share.ts"; | |
| 10 | import { otherRunsOfIssue } from "../../core/runs.ts"; | |
| 11 | import { compareRows, readCompared } from "../../trace/compare.ts"; | |
| 8 | 12 | import { readTrace, traceHref, traceRows } from "../../trace/read.ts"; |
| 9 | 13 | import type { Ctx, Screen } from "../app/app.ts"; |
| 10 | 14 | import { binding } from "../app/keys.ts"; |
| @@ -14,17 +18,34 @@ import { truncate } from "../term/width.ts"; | ||
| 14 | 18 | import { emptyState } from "../ui/widgets.ts"; |
| 15 | 19 | import { Scroll, openOnWeb } from "./common.ts"; |
| 16 | 20 | |
| 17 | export function traceScreen(f: Session, ctx: Ctx, repo: Repo, runId: string): Screen { | |
| 21 | /** | |
| 22 | * `versus`: the other run of the issue to read against, as `compareRows` (the page's table); `c` | |
| 23 | * opens the newest other run of the issue. `s` makes the share link (owner only, shown once), `x` | |
| 24 | * ends it. | |
| 25 | */ | |
| 26 | export function traceScreen(f: Session, ctx: Ctx, repo: Repo, runId: string, versus?: string): Screen { | |
| 18 | 27 | const scroll = new Scroll(); |
| 19 | 28 | let rows: string[] | undefined; |
| 20 | 29 | let loaded = false; |
| 30 | let issueId: string | null = null; | |
| 31 | let shared = false; | |
| 32 | let confirm = false; | |
| 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(["o"], "open on the web"), binding(["c"], "compare"), binding(["s"], "share"), binding(["x"], "stop sharing")], | |
| 25 | 37 | async load() { |
| 38 | if (versus !== undefined) { | |
| 39 | const both = await readCompared(f.app, f.user, repo, runId, versus); | |
| 40 | rows = both === undefined ? undefined : compareRows(both.a, both.b); | |
| 41 | loaded = true; | |
| 42 | ctx.invalidate(); | |
| 43 | return; | |
| 44 | } | |
| 26 | 45 | const view = await readTrace(f.app, f.user, repo, runId); |
| 27 | 46 | rows = view === undefined ? undefined : traceRows(view); |
| 47 | issueId = view?.run.issueId ?? null; | |
| 48 | shared = isShared(f.app.db, runId); | |
| 28 | 49 | loaded = true; |
| 29 | 50 | ctx.invalidate(); |
| 30 | 51 | }, |
| @@ -46,6 +67,33 @@ export function traceScreen(f: Session, ctx: Ctx, repo: Repo, runId: string): Sc | ||
| 46 | 67 | openOnWeb(f, ctx, traceHref(runId), "the run's trace"); |
| 47 | 68 | return true; |
| 48 | 69 | } |
| 70 | if (e.ctrl || e.alt) return false; | |
| 71 | if (e.key !== "s") confirm = false; | |
| 72 | if (e.key === "c" && versus === undefined && issueId !== null) { | |
| 73 | const other = otherRunsOfIssue(f.app.db, issueId, runId, 1)[0]; | |
Medium. |
||
| 74 | if (other === undefined) ctx.toast("no other run of this issue to compare with", "attn"); | |
| 75 | else ctx.push(traceScreen(f, ctx, repo, runId, other.id)); | |
| 76 | return true; | |
| 77 | } | |
| 78 | if ((e.key === "s" || e.key === "x") && versus === undefined) { | |
| 79 | // A live link already handed out ends when another is made: the second press says yes. | |
| 80 | if (e.key === "s" && shared && !confirm) { | |
| 81 | confirm = true; | |
| 82 | ctx.toast("a share link is live: press s again to replace it, and end the old one", "attn"); | |
| 83 | return true; | |
| 84 | } | |
| 85 | confirm = false; | |
| 86 | const done = e.key === "s" ? shareRun(f.app, f.user, repo, runId) : unshareRun(f.app, f.user, repo, runId); | |
| 87 | if (!done.ok) ctx.toast(done.error === "forbidden" ? "only the repository's owner shares a trace" : "that could not be done", "danger"); | |
| 88 | else if ("token" in done.value) { | |
| 89 | shared = true; | |
| 90 | openOnWeb(f, ctx, sharedHref(done.value.token), "the share link (shown once)"); | |
| 91 | } else { | |
| 92 | shared = false; | |
| 93 | ctx.toast(done.value.was ? "sharing stopped: the link no longer works" : "this trace was not shared", "success"); | |
| 94 | } | |
| 95 | return true; | |
| 96 | } | |
| 49 | 97 | if (rows === undefined) return false; |
| 50 | 98 | const took = scroll.key(e, rows.length); |
| 51 | 99 | if (took) ctx.invalidate(); |
src/web/pages/trace.ts +12 −4
| @@ -7,7 +7,8 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | import { notePhase, takeSessionTime } from "../../core/route_timing.ts"; |
| 9 | 9 | import { canRead, getRepoById, type Repo } from "../../core/repos.ts"; |
| 10 | import { getRun } from "../../core/runs.ts"; | |
| 10 | import { getRun, otherRunsOfIssue } from "../../core/runs.ts"; | |
| 11 | import { sharedNow } from "../../actions/trace_share.ts"; | |
| 11 | 12 | import type { Ctx } from "../../http/context.ts"; |
| 12 | 13 | import { securityFloor } from "../../http/respond.ts"; |
| 13 | 14 | import { branchesOf, stepNumber } from "../../trace/run_log.ts"; |
| @@ -92,7 +93,7 @@ function strip(steps: readonly Step[], timed: boolean): Html { | ||
| 92 | 93 | 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 | 94 | } |
| 94 | 95 | |
| 95 | function body(view: TraceView): Html { | |
| 96 | function body(view: TraceView, others: readonly { id: string; title: string }[], shared: boolean | undefined): Html { | |
| 96 | 97 | const steps = stepsInView(view); |
| 97 | 98 | const total = steps.length; |
| 98 | 99 | const base = traceHref(view.run.id); |
| @@ -118,6 +119,11 @@ function body(view: TraceView): Html { | ||
| 118 | 119 | </dl> |
| 119 | 120 | <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 | 121 | ${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>`} |
| 122 | ${others.length === 0 ? null : html`<nav class="bar small" id="trace-compare" aria-label="Compare with another run of this issue"><span class="muted">Compare with</span>${others.map((o) => html`<a href="${traceHref(view.run.id)}/compare/${o.id}">${o.title}</a>`)}</nav>`} | |
| 123 | ${shared === undefined ? null : html`<form method="post" class="bar small" id="trace-share" action="${traceHref(view.run.id)}/${shared ? "unshare" : "share"}"> | |
| 124 | <span class="muted">${shared ? "A share link is live: anyone holding it reads this trace, secrets redacted." : "Not shared. A share link lets anyone holding it read this trace, secrets redacted."}</span> | |
| 125 | <button type="submit">${shared ? "Stop sharing" : "Make a share link"}</button> | |
| 126 | </form>`} | |
| 121 | 127 | ${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} |
| 122 | 128 | ${total === 0 ? html`<p class="muted" id="trace-empty">This run wrote no steps.</p>` : html`${strip(steps, timed)} |
| 123 | 129 | <div class="trace-tree" id="trace-tree" data-base="${base}" role="group" aria-label="Steps">${rows}</div> |
| @@ -125,7 +131,7 @@ function body(view: TraceView): Html { | ||
| 125 | 131 | } |
| 126 | 132 | |
| 127 | 133 | /** The repository and run `:id` names, when the viewer may read the repository. */ |
| 128 | function repoOfRun(ctx: Ctx): { repo: Repo; id: string } | undefined { | |
| 134 | export function repoOfRun(ctx: Ctx): { repo: Repo; id: string } | undefined { | |
| 129 | 135 | const id = ctx.params.id ?? ""; |
| 130 | 136 | const run = /^[0-9A-Za-z_-]{1,64}$/.test(id) ? getRun(ctx.app.db, id) : undefined; |
| 131 | 137 | const repo = run === undefined ? undefined : getRepoById(ctx.app.db, run.repoId); |
| @@ -139,12 +145,14 @@ export async function tracePage(ctx: Ctx): Promise<Response> { | ||
| 139 | 145 | if (found === undefined) return notFoundPage(ctx); |
| 140 | 146 | const view = await readTrace(ctx.app, ctx.auth.user, found.repo, found.id); |
| 141 | 147 | if (view === undefined) return notFoundPage(ctx); |
| 148 | const others = view.run.issueId === null ? [] : otherRunsOfIssue(ctx.app.db, view.run.issueId, view.run.id, 10).map((r) => ({ id: r.id, title: `${r.model === "" ? r.kind : r.model} · ${r.state} · ${r.createdAt.slice(0, 16).replace("T", " ")} UTC` })); | |
| 149 | const shared = sharedNow(ctx.app, ctx.auth.user, found.repo, found.id); | |
| 142 | 150 | return page({ |
| 143 | 151 | ctx, |
| 144 | 152 | title: `Trace · ${view.title}`, |
| 145 | 153 | crumbs: crumbsFor(found.repo), |
| 146 | 154 | palette: repoPalette(ctx, found.repo), |
| 147 | body: body(view), | |
| 155 | body: body(view, others, shared), | |
| 148 | 156 | styleCss: TRACE_CSS, |
| 149 | 157 | scriptJs: `${behaviorsJs}\n${TRACE_JS}`, |
| 150 | 158 | }); |
src/web/pages/trace_compare.ts added +39
| @@ -0,0 +1,39 @@ | ||
| 1 | /** | |
| 2 | * `/traces/:id/compare/:other` (#17995): two runs of one issue side by side, as a table: steps, | |
| 3 | * failed steps, time, tokens, cost and outcome, with B − A beside each. Read through the trace's | |
| 4 | * own gate (`readCompared`); runs of two different issues, or one the viewer cannot read, are the | |
| 5 | * not-found a missing run gets. The terminal reads the same rows (`compareRows`). | |
| 6 | */ | |
| 7 | import { notePhase, takeSessionTime } from "../../core/route_timing.ts"; | |
| 8 | import type { Ctx } from "../../http/context.ts"; | |
| 9 | import { readCompared } from "../../trace/compare.ts"; | |
| 10 | import { traceHref } from "../../trace/read.ts"; | |
| 11 | import { crumbsFor, notFoundPage, pageHead, repoPalette } from "../chrome.ts"; | |
| 12 | import { html } from "../html.ts"; | |
| 13 | import { page } from "../layout.ts"; | |
| 14 | import { repoOfRun } from "./trace.ts"; | |
| 15 | ||
| 16 | const ID = /^[0-9A-Za-z_-]{1,64}$/; | |
| 17 | ||
| 18 | // GET /traces/:id/compare/:other | |
| 19 | export async function traceComparePage(ctx: Ctx): Promise<Response> { | |
| 20 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 21 | const found = repoOfRun(ctx); | |
| 22 | const other = ctx.params.other ?? ""; | |
| 23 | if (found === undefined || !ID.test(other)) return notFoundPage(ctx); | |
| 24 | const both = await readCompared(ctx.app, ctx.auth.user, found.repo, found.id, other); | |
| 25 | if (both === undefined) return notFoundPage(ctx); | |
| 26 | const { a, b, comparison } = both; | |
| 27 | return page({ | |
| 28 | ctx, | |
| 29 | title: `Compare · ${a.title}`, | |
| 30 | crumbs: crumbsFor(found.repo), | |
| 31 | palette: repoPalette(ctx, found.repo), | |
| 32 | body: html`${pageHead(html`Compare: ${a.title}`, { sub: "Two runs of one issue side by side. Difference is B − A." })} | |
| 33 | <table class="table" id="trace-compare-table"> | |
| 34 | <caption class="visually-hidden">Run A and run B</caption> | |
| 35 | <thead><tr><th scope="col"></th><th scope="col"><a href="${traceHref(a.run.id)}">A: ${a.run.model === "" ? a.run.kind : a.run.model}</a></th><th scope="col"><a href="${traceHref(b.run.id)}">B: ${b.run.model === "" ? b.run.kind : b.run.model}</a></th><th scope="col">B − A</th></tr></thead> | |
| 36 | <tbody>${comparison.rows.map((r) => html`<tr><th scope="row">${r.label}</th><td>${r.a}</td><td>${r.b}</td><td>${r.diff}</td></tr>`)}</tbody> | |
| 37 | </table>`, | |
| 38 | }); | |
| 39 | } |
test/budgets/unfixtured/traces-id-compare-other.json added +4
test/conversation-ask.test.ts +2
| @@ -13,6 +13,8 @@ import { TOOLS } from "../src/http/mcp/tools.ts"; | ||
| 13 | 13 | |
| 14 | 14 | /** Every form, said: a tool, a call of it, and the sentence the person reads. */ |
| 15 | 15 | const SAID: readonly (readonly [string, Record<string, unknown>, string])[] = [ |
| 16 | ["trace", { repo: "marin/cakes", run: "r1", action: "share" }, "Make a redacted share link for the trace of run r1 in marin/cakes."], | |
| 17 | ["trace", { repo: "marin/cakes", run: "r1", action: "unshare" }, "Stop sharing the trace of run r1 in marin/cakes."], | |
| 16 | 18 | ["cut_wave", { repo: "marin/cakes" }, "Cut the next wave of marin/cakes as a draft pull."], |
| 17 | 19 | [ |
| 18 | 20 | "tell", |
test/hot-reads-indexed.test.ts +5 −1
| @@ -5,7 +5,7 @@ import { tmpdir } from "node:os"; | ||
| 5 | 5 | import { join } from "node:path"; |
| 6 | 6 | import { createApp, type App } from "../src/app.ts"; |
| 7 | 7 | import { listEvents } from "../src/core/events.ts"; |
| 8 | import { lastPullCursor, listPullRuns, queuedApart } from "../src/core/runs.ts"; | |
| 8 | import { lastPullCursor, listPullRuns, otherRunsOfIssue, queuedApart } from "../src/core/runs.ts"; | |
| 9 | 9 | import { closedByReview, reviewedClosed } from "../src/core/gate_hold.ts"; |
| 10 | 10 | import { askToLiveByPillar } from "../src/core/pillars.ts"; |
| 11 | 11 | |
| @@ -115,3 +115,7 @@ test("the crew's lessons read the week's failed checks by finish time from an in | ||
| 115 | 115 | expect(lines.join("\n")).toMatch(/run_(kind|failed)_finished/); |
| 116 | 116 | noScan(lines); |
| 117 | 117 | }); |
| 118 | ||
| 119 | test("the other runs of an issue (a trace's comparison) walk the issue's run index (#17995)", () => { | |
| 120 | noScan(plans(() => otherRunsOfIssue(app.db, "x", "y"))); | |
| 121 | }); |
test/mcp-forge-protocol.test.ts +1
test/mixed-actions-held.test.ts +1
| @@ -63,6 +63,7 @@ const MIXED: Readonly< | ||
| 63 | 63 | .map((action) => ({ action, id: "o1" })) |
| 64 | 64 | .concat([{ action: "hosts", id: "o1" }]), |
| 65 | 65 | }, |
| 66 | trace: { key: "action", reads: [{ repo: "o/r", action: "list" }, { repo: "o/r", run: "r1", action: "read" }, { repo: "o/r", run: "r1", with: "r2", action: "compare" }] }, | |
| 66 | 67 | sessions: { key: "action", reads: [{ action: "list" }] }, |
| 67 | 68 | ssh_keys: { key: "action", reads: [{ action: "list" }] }, |
| 68 | 69 | }; |
test/no-polling.test.ts +2
| @@ -356,6 +356,8 @@ const NOT_WALKED: Record<string, string> = { | ||
| 356 | 356 | "/runs/:id": "the seed creates no run", |
| 357 | 357 | "/traces/:id": "the seed creates no crew run with a log", |
| 358 | 358 | "/traces/:id/steps/:n": "the seed creates no crew run with a log", |
| 359 | "/traces/:id/compare/:other": "the seed creates no crew run with a log", | |
| 360 | "/traces/shared/:token": "the seed creates no crew run with a log", | |
| 359 | 361 | "/signin/access/:token": "needs a one-time access link an admin minted", |
| 360 | 362 | "/tell/share/:id": "needs a receipt for a share sent by the signed-in person, which the seed has none of", |
| 361 | 363 | "/status/:token": "needs a status token an outsider was handed on filing, which the seed has none of", |
test/open-routes-floor.test.ts +1 −1
| @@ -36,7 +36,7 @@ const SLOW_MS = 200 * 5; | ||
| 36 | 36 | * issue, or a well-formed value nothing seeds. |
| 37 | 37 | */ |
| 38 | 38 | const FILL: Readonly<Record<string, string>> = { |
| 39 | owner: "alice", username: "alice", user: "alice", org: "alice", scope: "alice", | |
| 39 | owner: "alice", other: "2", username: "alice", user: "alice", org: "alice", scope: "alice", | |
| 40 | 40 | name: "notes", repo: "notes", number: "1", n: "1", id: "1", job: "1", |
| 41 | 41 | ref: "main", branch: "main", oid: "0".repeat(40), sha: "0".repeat(40), commit: "0".repeat(40), |
| 42 | 42 | // Not seeded: each still has to answer its not-found with the floor. |
test/security-floor-routes.test.ts +2
| @@ -95,6 +95,8 @@ const UNSEEDED: Record<string, string> = { | ||
| 95 | 95 | "/tell/share/:id": "needs a receipt for a share sent by the signed-in person, which the seed has none of", |
| 96 | 96 | "/traces/:id": "needs a crew run with a log, which the seed has none of", |
| 97 | 97 | "/traces/:id/steps/:n": "needs a crew run with a log, which the seed has none of", |
| 98 | "/traces/:id/compare/:other": "needs a crew run with a log, which the seed has none of", | |
| 99 | "/traces/shared/:token": "needs a crew run with a log, which the seed has none of", | |
| 98 | 100 | "/pricing/orders/:id": "needs an order id the buyer's cookie names; the seed has no order (test/billing-order-receipt.test.ts covers it)", |
| 99 | 101 | "/pricing/orders/:id/receipt.html": "needs an order id the buyer's cookie names; the seed has no order (test/billing-order-receipt.test.ts covers it)", |
| 100 | 102 | "/status/:token": "needs a status token an outsider was handed on filing, which the seed has none of", |
new above
offline
Medium, denial of service. Any holder of a link (no sign-in) makes the forge restore, read and parse the whole run log and redact every line on every request, and there is no rate limit by address. Cache the redacted SharedTrace by run id (drop it on revoke or a new link), and rate-limit this route by socket address.