Public: anyone can read this; the crew can change it.
Traces 1b: GET /api/v1/traces lists traces newest first, gated as the run's log is #18077
Merged 10h 5m ago, live in production
scout/forge:scout/18021-traces-1b-get-api-v1-traces-lists-traces → main 0 commits fork 1 thread · 1 unresolved
11 files
changed +521
−210
docs/features/traces.md +4
| @@ -25,3 +25,7 @@ Not yet: Talk conversations and outside imports as rows (they wait for the trace | ||
| 25 | 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. |
| 26 | 26 | |
| 27 | 27 | 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. |
| 28 | ||
| 29 | ## Listing traces (#18021) | |
| 30 | ||
| 31 | `GET /api/v1/traces` lists the trace index newest first as `{items, next_cursor}`: filters `repository` (an id or `owner/name`), `kind` (run, talk), `model`, `outcome` (ok, failed, cancelled, stopped), `since` and `until` (a date or an ISO instant with a zone); `limit` 1 to 200 (default 50) and `cursor`. A run is listed only to someone who may read its log (the rule the run page uses), a Talk turn to its person alone; any other row is absent, never a hint. Only the index is read, never a log. A bad filter value is a 400 (`invalid_since`, …); a bad limit or cursor, or an unknown parameter, a 422. Code: `src/http/api/traces.ts` over `listTraceIndex`. |
src/contract/openapi/routes/getApiV1Traces.ts added +25
| @@ -0,0 +1,25 @@ | ||
| 1 | import { int, num, obj, page, str } from "../schema_kit.ts"; | |
| 2 | import { limitTo, type Route } from "../shared.ts"; | |
| 3 | ||
| 4 | const ITEM_KEYS = ["id", "kind", "repository", "title", "model", "account", "steps", "errors", "tokensIn", "tokensCached", "tokensOut", "costUsd", "outcome", "started", "ended"] as const; | |
| 5 | ||
| 6 | export const route: Route = { | |
| 7 | method: "get", | |
| 8 | path: "/api/v1/traces", | |
| 9 | summary: "List traces newest first as `{ items, next_cursor }`", | |
| 10 | detail: | |
| 11 | "List the trace index, newest first (started, then id), as `{ items, next_cursor }` of { id, kind (run, talk, review), repository (an id; empty for a Talk turn), title, model, account, steps, errors, tokensIn, tokensCached, tokensOut, costUsd, outcome (ok, failed, cancelled, stopped), started, ended }; filters repository (an id or owner/name), kind, model, outcome, since (started at or after) and until (started before), the last two a date or an ISO 8601 instant with a zone; limit 1 to 200 (default 50) and a cursor (the next_cursor of the page before). A trace the caller could not read the log of is absent, never a hint it exists: a run is listed to those who may read its log, a Talk turn to its person alone; only the index is read, never a log. A bad filter value is a 400 (invalid_kind, invalid_outcome, invalid_model, invalid_repository, invalid_since, invalid_until); a bad limit or cursor, or a parameter this list does not read, is a 422", | |
| 12 | auth: true, | |
| 13 | role: "member", | |
| 14 | query: { repository: "string", kind: "string", model: "string", outcome: "string", since: "string", until: "string", cursor: "string", limit: limitTo(200) }, | |
| 15 | itemKeys: ITEM_KEYS, | |
| 16 | response: page( | |
| 17 | obj({ | |
| 18 | id: str, kind: str, repository: str, title: str, model: str, account: str, | |
| 19 | steps: int, errors: int, tokensIn: int, tokensCached: int, tokensOut: int, costUsd: num, | |
| 20 | outcome: str, started: str, ended: str, | |
| 21 | }), | |
| 22 | ), | |
| 23 | errorStatus: { 400: "a filter value that is not one it reads (invalid_kind, invalid_outcome, invalid_model, invalid_repository, invalid_since, invalid_until)", 422: "a bad limit or cursor, or a parameter this list does not read" }, | |
| 24 | refuses: [401], | |
| 25 | }; |
src/contract/openapi/routes/index.ts +1
| @@ -265,6 +265,7 @@ export { route as getApiV1TelemetrySources } from "./getApiV1TelemetrySources.ts | ||
| 265 | 265 | export { route as getApiV1TelemetrySystem } from "./getApiV1TelemetrySystem.ts"; |
| 266 | 266 | export { route as getApiV1Templates } from "./getApiV1Templates.ts"; |
| 267 | 267 | export { route as getApiV1Tokens } from "./getApiV1Tokens.ts"; |
| 268 | export { route as getApiV1Traces } from "./getApiV1Traces.ts"; | |
| 268 | 269 | export { route as getApiV1Usage } from "./getApiV1Usage.ts"; |
| 269 | 270 | export { route as getApiV1User } from "./getApiV1User.ts"; |
| 270 | 271 | export { route as getApiV1UserAssigned } from "./getApiV1UserAssigned.ts"; |
src/contract/tags.ts +1
| @@ -153,6 +153,7 @@ export const TAG_WORDS: Readonly<Record<string, string>> = { | ||
| 153 | 153 | reports: "Reports filed on a repository and their triage", |
| 154 | 154 | runs: "Runs of the crew: logs, steering, cancelling, decisions and events", |
| 155 | 155 | tokens: "API tokens, sessions and secrets", |
| 156 | traces: "The trace index: one line for each run or Talk turn, newest first", | |
| 156 | 157 | user: "The signed-in person and people: profile, keys, settings, notifications", |
| 157 | 158 | }; |
| 158 | 159 |
src/generated/marks.ts +209 −207
| @@ -6,213 +6,214 @@ import k1 from "../http/marks/advisories.json"; | ||
| 6 | 6 | import k2 from "../http/marks/agents.json"; |
| 7 | 7 | import k3 from "../http/marks/api-project-envelope.json"; |
| 8 | 8 | import k4 from "../http/marks/api-project-otlp.json"; |
| 9 | import k5 from "../http/marks/api-v1-admin.json"; | |
| 10 | import k6 from "../http/marks/api-v1-agents.json"; | |
| 11 | import k7 from "../http/marks/api-v1-app-templates.json"; | |
| 12 | import k8 from "../http/marks/api-v1-apps.json"; | |
| 13 | import k9 from "../http/marks/api-v1-arcade.json"; | |
| 14 | import k10 from "../http/marks/api-v1-asks.json"; | |
| 15 | import k11 from "../http/marks/api-v1-boards.json"; | |
| 16 | import k12 from "../http/marks/api-v1-broker.json"; | |
| 17 | import k13 from "../http/marks/api-v1-builds.json"; | |
| 18 | import k14 from "../http/marks/api-v1-cache.json"; | |
| 19 | import k15 from "../http/marks/api-v1-changelog.json"; | |
| 20 | import k16 from "../http/marks/api-v1-cloud.json"; | |
| 21 | import k17 from "../http/marks/api-v1-codes.json"; | |
| 22 | import k18 from "../http/marks/api-v1-config.json"; | |
| 23 | import k19 from "../http/marks/api-v1-contribute.json"; | |
| 24 | import k20 from "../http/marks/api-v1-conversation-confirms.json"; | |
| 25 | import k21 from "../http/marks/api-v1-conversation-tokens.json"; | |
| 26 | import k22 from "../http/marks/api-v1-conversation.json"; | |
| 27 | import k23 from "../http/marks/api-v1-conversations.json"; | |
| 28 | import k24 from "../http/marks/api-v1-credit.json"; | |
| 29 | import k25 from "../http/marks/api-v1-crew.json"; | |
| 30 | import k26 from "../http/marks/api-v1-devices.json"; | |
| 31 | import k27 from "../http/marks/api-v1-drills.json"; | |
| 32 | import k28 from "../http/marks/api-v1-explore.json"; | |
| 33 | import k29 from "../http/marks/api-v1-family.json"; | |
| 34 | import k30 from "../http/marks/api-v1-features.json"; | |
| 35 | import k31 from "../http/marks/api-v1-feed.json"; | |
| 36 | import k32 from "../http/marks/api-v1-first-admin.json"; | |
| 37 | import k33 from "../http/marks/api-v1-flags.json"; | |
| 38 | import k34 from "../http/marks/api-v1-fleet.json"; | |
| 39 | import k35 from "../http/marks/api-v1-flow.json"; | |
| 40 | import k36 from "../http/marks/api-v1-forge-templates.json"; | |
| 41 | import k37 from "../http/marks/api-v1-forge.json"; | |
| 42 | import k38 from "../http/marks/api-v1-forges.json"; | |
| 43 | import k39 from "../http/marks/api-v1-forks.json"; | |
| 44 | import k40 from "../http/marks/api-v1-front.json"; | |
| 45 | import k41 from "../http/marks/api-v1-games.json"; | |
| 46 | import k42 from "../http/marks/api-v1-health.json"; | |
| 47 | import k43 from "../http/marks/api-v1-heartbeat.json"; | |
| 48 | import k44 from "../http/marks/api-v1-here.json"; | |
| 49 | import k45 from "../http/marks/api-v1-home.json"; | |
| 50 | import k46 from "../http/marks/api-v1-keeper.json"; | |
| 51 | import k47 from "../http/marks/api-v1-mail-key.json"; | |
| 52 | import k48 from "../http/marks/api-v1-maintenance.json"; | |
| 53 | import k49 from "../http/marks/api-v1-map.json"; | |
| 54 | import k50 from "../http/marks/api-v1-migrate.json"; | |
| 55 | import k51 from "../http/marks/api-v1-models.json"; | |
| 56 | import k52 from "../http/marks/api-v1-offers.json"; | |
| 57 | import k53 from "../http/marks/api-v1-openapi.json.json"; | |
| 58 | import k54 from "../http/marks/api-v1-orgs.json"; | |
| 59 | import k55 from "../http/marks/api-v1-packs.json"; | |
| 60 | import k56 from "../http/marks/api-v1-passport.json"; | |
| 61 | import k57 from "../http/marks/api-v1-places.json"; | |
| 62 | import k58 from "../http/marks/api-v1-plans.json"; | |
| 63 | import k59 from "../http/marks/api-v1-proof.json"; | |
| 64 | import k60 from "../http/marks/api-v1-reach.json"; | |
| 65 | import k61 from "../http/marks/api-v1-release.json"; | |
| 66 | import k62 from "../http/marks/api-v1-releases.json"; | |
| 67 | import k63 from "../http/marks/api-v1-repos.json"; | |
| 68 | import k64 from "../http/marks/api-v1-returns.json"; | |
| 69 | import k65 from "../http/marks/api-v1-runner.json"; | |
| 70 | import k66 from "../http/marks/api-v1-search.json"; | |
| 71 | import k67 from "../http/marks/api-v1-secrets.json"; | |
| 72 | import k68 from "../http/marks/api-v1-sessions.json"; | |
| 73 | import k69 from "../http/marks/api-v1-share.json"; | |
| 74 | import k70 from "../http/marks/api-v1-slash.json"; | |
| 75 | import k71 from "../http/marks/api-v1-start.json"; | |
| 76 | import k72 from "../http/marks/api-v1-status.json"; | |
| 77 | import k73 from "../http/marks/api-v1-telemetry.json"; | |
| 78 | import k74 from "../http/marks/api-v1-templates.json"; | |
| 79 | import k75 from "../http/marks/api-v1-thoughts.json"; | |
| 80 | import k76 from "../http/marks/api-v1-timers.json"; | |
| 81 | import k77 from "../http/marks/api-v1-tokens.json"; | |
| 82 | import k78 from "../http/marks/api-v1-usage.json"; | |
| 83 | import k79 from "../http/marks/api-v1-user.json"; | |
| 84 | import k80 from "../http/marks/api-v1-users.json"; | |
| 85 | import k81 from "../http/marks/api-v1-version.json"; | |
| 86 | import k82 from "../http/marks/api-v1.json"; | |
| 87 | import k83 from "../http/marks/api.json"; | |
| 88 | import k84 from "../http/marks/apple-touch-icon-precomposed.png.json"; | |
| 89 | import k85 from "../http/marks/apple-touch-icon.png.json"; | |
| 90 | import k86 from "../http/marks/apps.json"; | |
| 91 | import k87 from "../http/marks/archive.json"; | |
| 92 | import k88 from "../http/marks/asked.json"; | |
| 93 | import k89 from "../http/marks/asks.json"; | |
| 94 | import k90 from "../http/marks/assets.json"; | |
| 95 | import k91 from "../http/marks/audit.csv.json"; | |
| 96 | import k92 from "../http/marks/audit.json"; | |
| 97 | import k93 from "../http/marks/big.json"; | |
| 98 | import k94 from "../http/marks/board.json"; | |
| 99 | import k95 from "../http/marks/boards.json"; | |
| 100 | import k96 from "../http/marks/bring.json"; | |
| 101 | import k97 from "../http/marks/broker.json"; | |
| 102 | import k98 from "../http/marks/changelog.atom.json"; | |
| 103 | import k99 from "../http/marks/changelog.json"; | |
| 104 | import k100 from "../http/marks/claim.json"; | |
| 105 | import k101 from "../http/marks/client-clouds.json"; | |
| 106 | import k102 from "../http/marks/cloud.json"; | |
| 107 | import k103 from "../http/marks/cockpit.json"; | |
| 108 | import k104 from "../http/marks/contract.json"; | |
| 109 | import k105 from "../http/marks/contribute.json"; | |
| 110 | import k106 from "../http/marks/csp-report.json"; | |
| 111 | import k107 from "../http/marks/dash.json"; | |
| 112 | import k108 from "../http/marks/data.json"; | |
| 113 | import k109 from "../http/marks/dataset.json"; | |
| 114 | import k110 from "../http/marks/deploy-to.json"; | |
| 115 | import k111 from "../http/marks/design.json"; | |
| 116 | import k112 from "../http/marks/desk.json"; | |
| 117 | import k113 from "../http/marks/displays.json"; | |
| 118 | import k114 from "../http/marks/docs.json"; | |
| 119 | import k115 from "../http/marks/edge.json"; | |
| 120 | import k116 from "../http/marks/edition.json"; | |
| 121 | import k117 from "../http/marks/events.json"; | |
| 122 | import k118 from "../http/marks/explore.atom.json"; | |
| 123 | import k119 from "../http/marks/explore.json"; | |
| 124 | import k120 from "../http/marks/explore.json.json"; | |
| 125 | import k121 from "../http/marks/family.json"; | |
| 126 | import k122 from "../http/marks/favicon.ico.json"; | |
| 127 | import k123 from "../http/marks/feed.json"; | |
| 128 | import k124 from "../http/marks/feed.json.json"; | |
| 129 | import k125 from "../http/marks/first-admin.json"; | |
| 130 | import k126 from "../http/marks/fleet.json"; | |
| 131 | import k127 from "../http/marks/flow.json"; | |
| 132 | import k128 from "../http/marks/forge.json"; | |
| 133 | import k129 from "../http/marks/forges.json"; | |
| 134 | import k130 from "../http/marks/forks.json"; | |
| 135 | import k131 from "../http/marks/front.json"; | |
| 136 | import k132 from "../http/marks/funnel.json"; | |
| 137 | import k133 from "../http/marks/games.json"; | |
| 138 | import k134 from "../http/marks/go.json"; | |
| 139 | import k135 from "../http/marks/health.json"; | |
| 140 | import k136 from "../http/marks/home.json"; | |
| 141 | import k137 from "../http/marks/hooks.json"; | |
| 142 | import k138 from "../http/marks/icons.json"; | |
| 143 | import k139 from "../http/marks/infra.json"; | |
| 144 | import k140 from "../http/marks/join.json"; | |
| 145 | import k141 from "../http/marks/live.json"; | |
| 146 | import k142 from "../http/marks/llms-full.txt.json"; | |
| 147 | import k143 from "../http/marks/llms.txt.json"; | |
| 148 | import k144 from "../http/marks/login.json"; | |
| 149 | import k145 from "../http/marks/logout.json"; | |
| 150 | import k146 from "../http/marks/maintenance-json.json"; | |
| 151 | import k147 from "../http/marks/maintenance.json"; | |
| 152 | import k148 from "../http/marks/manifest.webmanifest.json"; | |
| 153 | import k149 from "../http/marks/map.json"; | |
| 154 | import k150 from "../http/marks/mcp.json"; | |
| 155 | import k151 from "../http/marks/measure.json"; | |
| 156 | import k152 from "../http/marks/metrics.json"; | |
| 157 | import k153 from "../http/marks/migrate.json"; | |
| 158 | import k154 from "../http/marks/new.json"; | |
| 159 | import k155 from "../http/marks/next.json"; | |
| 160 | import k156 from "../http/marks/notes.json"; | |
| 161 | import k157 from "../http/marks/now.json"; | |
| 162 | import k158 from "../http/marks/oauth.json"; | |
| 163 | import k159 from "../http/marks/oembed.json"; | |
| 164 | import k160 from "../http/marks/offers.json"; | |
| 165 | import k161 from "../http/marks/offline.json"; | |
| 166 | import k162 from "../http/marks/og.json"; | |
| 167 | import k163 from "../http/marks/openapi.json.json"; | |
| 168 | import k164 from "../http/marks/opensearch.xml.json"; | |
| 169 | import k165 from "../http/marks/organizations.json"; | |
| 170 | import k166 from "../http/marks/origins.json"; | |
| 171 | import k167 from "../http/marks/owner.atom.json"; | |
| 172 | import k168 from "../http/marks/owner.json"; | |
| 173 | import k169 from "../http/marks/packs.json"; | |
| 174 | import k170 from "../http/marks/peek.json"; | |
| 175 | import k171 from "../http/marks/pinned.json"; | |
| 176 | import k172 from "../http/marks/pipeline.json"; | |
| 177 | import k173 from "../http/marks/plugins.json"; | |
| 178 | import k174 from "../http/marks/pricing.json"; | |
| 179 | import k175 from "../http/marks/privacy.json"; | |
| 180 | import k176 from "../http/marks/r.json"; | |
| 181 | import k177 from "../http/marks/recover.json"; | |
| 182 | import k178 from "../http/marks/releases.json"; | |
| 183 | import k179 from "../http/marks/releases.json.json"; | |
| 184 | import k180 from "../http/marks/repos.json"; | |
| 185 | import k181 from "../http/marks/resilience.json"; | |
| 186 | import k182 from "../http/marks/review.json"; | |
| 187 | import k183 from "../http/marks/robots.txt.json"; | |
| 188 | import k184 from "../http/marks/root.json"; | |
| 189 | import k185 from "../http/marks/runs.json"; | |
| 190 | import k186 from "../http/marks/script-error.json"; | |
| 191 | import k187 from "../http/marks/search.json"; | |
| 192 | import k188 from "../http/marks/security.json"; | |
| 193 | import k189 from "../http/marks/settings.json"; | |
| 194 | import k190 from "../http/marks/sign-in.json"; | |
| 195 | import k191 from "../http/marks/signin.json"; | |
| 196 | import k192 from "../http/marks/signup.json"; | |
| 197 | import k193 from "../http/marks/sitemap.xml.json"; | |
| 198 | import k194 from "../http/marks/status.json"; | |
| 199 | import k195 from "../http/marks/support.json"; | |
| 200 | import k196 from "../http/marks/sw.js.json"; | |
| 201 | import k197 from "../http/marks/system.json"; | |
| 202 | import k198 from "../http/marks/tab.json"; | |
| 203 | import k199 from "../http/marks/tabforge.json"; | |
| 204 | import k200 from "../http/marks/talk.json"; | |
| 205 | import k201 from "../http/marks/tell.json"; | |
| 206 | import k202 from "../http/marks/templates.json"; | |
| 207 | import k203 from "../http/marks/templates.json.json"; | |
| 208 | import k204 from "../http/marks/terms.json"; | |
| 209 | import k205 from "../http/marks/theme.json"; | |
| 210 | import k206 from "../http/marks/toolbar-lab.js.json"; | |
| 211 | import k207 from "../http/marks/toolbar-lab.json"; | |
| 212 | import k208 from "../http/marks/traces.json"; | |
| 213 | import k209 from "../http/marks/verify.json"; | |
| 214 | import k210 from "../http/marks/welcome.json"; | |
| 215 | import k211 from "../http/marks/well-known.json"; | |
| 9 | import k5 from "../http/marks/api-traces.json"; | |
| 10 | import k6 from "../http/marks/api-v1-admin.json"; | |
| 11 | import k7 from "../http/marks/api-v1-agents.json"; | |
| 12 | import k8 from "../http/marks/api-v1-app-templates.json"; | |
| 13 | import k9 from "../http/marks/api-v1-apps.json"; | |
| 14 | import k10 from "../http/marks/api-v1-arcade.json"; | |
| 15 | import k11 from "../http/marks/api-v1-asks.json"; | |
| 16 | import k12 from "../http/marks/api-v1-boards.json"; | |
| 17 | import k13 from "../http/marks/api-v1-broker.json"; | |
| 18 | import k14 from "../http/marks/api-v1-builds.json"; | |
| 19 | import k15 from "../http/marks/api-v1-cache.json"; | |
| 20 | import k16 from "../http/marks/api-v1-changelog.json"; | |
| 21 | import k17 from "../http/marks/api-v1-cloud.json"; | |
| 22 | import k18 from "../http/marks/api-v1-codes.json"; | |
| 23 | import k19 from "../http/marks/api-v1-config.json"; | |
| 24 | import k20 from "../http/marks/api-v1-contribute.json"; | |
| 25 | import k21 from "../http/marks/api-v1-conversation-confirms.json"; | |
| 26 | import k22 from "../http/marks/api-v1-conversation-tokens.json"; | |
| 27 | import k23 from "../http/marks/api-v1-conversation.json"; | |
| 28 | import k24 from "../http/marks/api-v1-conversations.json"; | |
| 29 | import k25 from "../http/marks/api-v1-credit.json"; | |
| 30 | import k26 from "../http/marks/api-v1-crew.json"; | |
| 31 | import k27 from "../http/marks/api-v1-devices.json"; | |
| 32 | import k28 from "../http/marks/api-v1-drills.json"; | |
| 33 | import k29 from "../http/marks/api-v1-explore.json"; | |
| 34 | import k30 from "../http/marks/api-v1-family.json"; | |
| 35 | import k31 from "../http/marks/api-v1-features.json"; | |
| 36 | import k32 from "../http/marks/api-v1-feed.json"; | |
| 37 | import k33 from "../http/marks/api-v1-first-admin.json"; | |
| 38 | import k34 from "../http/marks/api-v1-flags.json"; | |
| 39 | import k35 from "../http/marks/api-v1-fleet.json"; | |
| 40 | import k36 from "../http/marks/api-v1-flow.json"; | |
| 41 | import k37 from "../http/marks/api-v1-forge-templates.json"; | |
| 42 | import k38 from "../http/marks/api-v1-forge.json"; | |
| 43 | import k39 from "../http/marks/api-v1-forges.json"; | |
| 44 | import k40 from "../http/marks/api-v1-forks.json"; | |
| 45 | import k41 from "../http/marks/api-v1-front.json"; | |
| 46 | import k42 from "../http/marks/api-v1-games.json"; | |
| 47 | import k43 from "../http/marks/api-v1-health.json"; | |
| 48 | import k44 from "../http/marks/api-v1-heartbeat.json"; | |
| 49 | import k45 from "../http/marks/api-v1-here.json"; | |
| 50 | import k46 from "../http/marks/api-v1-home.json"; | |
| 51 | import k47 from "../http/marks/api-v1-keeper.json"; | |
| 52 | import k48 from "../http/marks/api-v1-mail-key.json"; | |
| 53 | import k49 from "../http/marks/api-v1-maintenance.json"; | |
| 54 | import k50 from "../http/marks/api-v1-map.json"; | |
| 55 | import k51 from "../http/marks/api-v1-migrate.json"; | |
| 56 | import k52 from "../http/marks/api-v1-models.json"; | |
| 57 | import k53 from "../http/marks/api-v1-offers.json"; | |
| 58 | import k54 from "../http/marks/api-v1-openapi.json.json"; | |
| 59 | import k55 from "../http/marks/api-v1-orgs.json"; | |
| 60 | import k56 from "../http/marks/api-v1-packs.json"; | |
| 61 | import k57 from "../http/marks/api-v1-passport.json"; | |
| 62 | import k58 from "../http/marks/api-v1-places.json"; | |
| 63 | import k59 from "../http/marks/api-v1-plans.json"; | |
| 64 | import k60 from "../http/marks/api-v1-proof.json"; | |
| 65 | import k61 from "../http/marks/api-v1-reach.json"; | |
| 66 | import k62 from "../http/marks/api-v1-release.json"; | |
| 67 | import k63 from "../http/marks/api-v1-releases.json"; | |
| 68 | import k64 from "../http/marks/api-v1-repos.json"; | |
| 69 | import k65 from "../http/marks/api-v1-returns.json"; | |
| 70 | import k66 from "../http/marks/api-v1-runner.json"; | |
| 71 | import k67 from "../http/marks/api-v1-search.json"; | |
| 72 | import k68 from "../http/marks/api-v1-secrets.json"; | |
| 73 | import k69 from "../http/marks/api-v1-sessions.json"; | |
| 74 | import k70 from "../http/marks/api-v1-share.json"; | |
| 75 | import k71 from "../http/marks/api-v1-slash.json"; | |
| 76 | import k72 from "../http/marks/api-v1-start.json"; | |
| 77 | import k73 from "../http/marks/api-v1-status.json"; | |
| 78 | import k74 from "../http/marks/api-v1-telemetry.json"; | |
| 79 | import k75 from "../http/marks/api-v1-templates.json"; | |
| 80 | import k76 from "../http/marks/api-v1-thoughts.json"; | |
| 81 | import k77 from "../http/marks/api-v1-timers.json"; | |
| 82 | import k78 from "../http/marks/api-v1-tokens.json"; | |
| 83 | import k79 from "../http/marks/api-v1-usage.json"; | |
| 84 | import k80 from "../http/marks/api-v1-user.json"; | |
| 85 | import k81 from "../http/marks/api-v1-users.json"; | |
| 86 | import k82 from "../http/marks/api-v1-version.json"; | |
| 87 | import k83 from "../http/marks/api-v1.json"; | |
| 88 | import k84 from "../http/marks/api.json"; | |
| 89 | import k85 from "../http/marks/apple-touch-icon-precomposed.png.json"; | |
| 90 | import k86 from "../http/marks/apple-touch-icon.png.json"; | |
| 91 | import k87 from "../http/marks/apps.json"; | |
| 92 | import k88 from "../http/marks/archive.json"; | |
| 93 | import k89 from "../http/marks/asked.json"; | |
| 94 | import k90 from "../http/marks/asks.json"; | |
| 95 | import k91 from "../http/marks/assets.json"; | |
| 96 | import k92 from "../http/marks/audit.csv.json"; | |
| 97 | import k93 from "../http/marks/audit.json"; | |
| 98 | import k94 from "../http/marks/big.json"; | |
| 99 | import k95 from "../http/marks/board.json"; | |
| 100 | import k96 from "../http/marks/boards.json"; | |
| 101 | import k97 from "../http/marks/bring.json"; | |
| 102 | import k98 from "../http/marks/broker.json"; | |
| 103 | import k99 from "../http/marks/changelog.atom.json"; | |
| 104 | import k100 from "../http/marks/changelog.json"; | |
| 105 | import k101 from "../http/marks/claim.json"; | |
| 106 | import k102 from "../http/marks/client-clouds.json"; | |
| 107 | import k103 from "../http/marks/cloud.json"; | |
| 108 | import k104 from "../http/marks/cockpit.json"; | |
| 109 | import k105 from "../http/marks/contract.json"; | |
| 110 | import k106 from "../http/marks/contribute.json"; | |
| 111 | import k107 from "../http/marks/csp-report.json"; | |
| 112 | import k108 from "../http/marks/dash.json"; | |
| 113 | import k109 from "../http/marks/data.json"; | |
| 114 | import k110 from "../http/marks/dataset.json"; | |
| 115 | import k111 from "../http/marks/deploy-to.json"; | |
| 116 | import k112 from "../http/marks/design.json"; | |
| 117 | import k113 from "../http/marks/desk.json"; | |
| 118 | import k114 from "../http/marks/displays.json"; | |
| 119 | import k115 from "../http/marks/docs.json"; | |
| 120 | import k116 from "../http/marks/edge.json"; | |
| 121 | import k117 from "../http/marks/edition.json"; | |
| 122 | import k118 from "../http/marks/events.json"; | |
| 123 | import k119 from "../http/marks/explore.atom.json"; | |
| 124 | import k120 from "../http/marks/explore.json"; | |
| 125 | import k121 from "../http/marks/explore.json.json"; | |
| 126 | import k122 from "../http/marks/family.json"; | |
| 127 | import k123 from "../http/marks/favicon.ico.json"; | |
| 128 | import k124 from "../http/marks/feed.json"; | |
| 129 | import k125 from "../http/marks/feed.json.json"; | |
| 130 | import k126 from "../http/marks/first-admin.json"; | |
| 131 | import k127 from "../http/marks/fleet.json"; | |
| 132 | import k128 from "../http/marks/flow.json"; | |
| 133 | import k129 from "../http/marks/forge.json"; | |
| 134 | import k130 from "../http/marks/forges.json"; | |
| 135 | import k131 from "../http/marks/forks.json"; | |
| 136 | import k132 from "../http/marks/front.json"; | |
| 137 | import k133 from "../http/marks/funnel.json"; | |
| 138 | import k134 from "../http/marks/games.json"; | |
| 139 | import k135 from "../http/marks/go.json"; | |
| 140 | import k136 from "../http/marks/health.json"; | |
| 141 | import k137 from "../http/marks/home.json"; | |
| 142 | import k138 from "../http/marks/hooks.json"; | |
| 143 | import k139 from "../http/marks/icons.json"; | |
| 144 | import k140 from "../http/marks/infra.json"; | |
| 145 | import k141 from "../http/marks/join.json"; | |
| 146 | import k142 from "../http/marks/live.json"; | |
| 147 | import k143 from "../http/marks/llms-full.txt.json"; | |
| 148 | import k144 from "../http/marks/llms.txt.json"; | |
| 149 | import k145 from "../http/marks/login.json"; | |
| 150 | import k146 from "../http/marks/logout.json"; | |
| 151 | import k147 from "../http/marks/maintenance-json.json"; | |
| 152 | import k148 from "../http/marks/maintenance.json"; | |
| 153 | import k149 from "../http/marks/manifest.webmanifest.json"; | |
| 154 | import k150 from "../http/marks/map.json"; | |
| 155 | import k151 from "../http/marks/mcp.json"; | |
| 156 | import k152 from "../http/marks/measure.json"; | |
| 157 | import k153 from "../http/marks/metrics.json"; | |
| 158 | import k154 from "../http/marks/migrate.json"; | |
| 159 | import k155 from "../http/marks/new.json"; | |
| 160 | import k156 from "../http/marks/next.json"; | |
| 161 | import k157 from "../http/marks/notes.json"; | |
| 162 | import k158 from "../http/marks/now.json"; | |
| 163 | import k159 from "../http/marks/oauth.json"; | |
| 164 | import k160 from "../http/marks/oembed.json"; | |
| 165 | import k161 from "../http/marks/offers.json"; | |
| 166 | import k162 from "../http/marks/offline.json"; | |
| 167 | import k163 from "../http/marks/og.json"; | |
| 168 | import k164 from "../http/marks/openapi.json.json"; | |
| 169 | import k165 from "../http/marks/opensearch.xml.json"; | |
| 170 | import k166 from "../http/marks/organizations.json"; | |
| 171 | import k167 from "../http/marks/origins.json"; | |
| 172 | import k168 from "../http/marks/owner.atom.json"; | |
| 173 | import k169 from "../http/marks/owner.json"; | |
| 174 | import k170 from "../http/marks/packs.json"; | |
| 175 | import k171 from "../http/marks/peek.json"; | |
| 176 | import k172 from "../http/marks/pinned.json"; | |
| 177 | import k173 from "../http/marks/pipeline.json"; | |
| 178 | import k174 from "../http/marks/plugins.json"; | |
| 179 | import k175 from "../http/marks/pricing.json"; | |
| 180 | import k176 from "../http/marks/privacy.json"; | |
| 181 | import k177 from "../http/marks/r.json"; | |
| 182 | import k178 from "../http/marks/recover.json"; | |
| 183 | import k179 from "../http/marks/releases.json"; | |
| 184 | import k180 from "../http/marks/releases.json.json"; | |
| 185 | import k181 from "../http/marks/repos.json"; | |
| 186 | import k182 from "../http/marks/resilience.json"; | |
| 187 | import k183 from "../http/marks/review.json"; | |
| 188 | import k184 from "../http/marks/robots.txt.json"; | |
| 189 | import k185 from "../http/marks/root.json"; | |
| 190 | import k186 from "../http/marks/runs.json"; | |
| 191 | import k187 from "../http/marks/script-error.json"; | |
| 192 | import k188 from "../http/marks/search.json"; | |
| 193 | import k189 from "../http/marks/security.json"; | |
| 194 | import k190 from "../http/marks/settings.json"; | |
| 195 | import k191 from "../http/marks/sign-in.json"; | |
| 196 | import k192 from "../http/marks/signin.json"; | |
| 197 | import k193 from "../http/marks/signup.json"; | |
| 198 | import k194 from "../http/marks/sitemap.xml.json"; | |
| 199 | import k195 from "../http/marks/status.json"; | |
| 200 | import k196 from "../http/marks/support.json"; | |
| 201 | import k197 from "../http/marks/sw.js.json"; | |
| 202 | import k198 from "../http/marks/system.json"; | |
| 203 | import k199 from "../http/marks/tab.json"; | |
| 204 | import k200 from "../http/marks/tabforge.json"; | |
| 205 | import k201 from "../http/marks/talk.json"; | |
| 206 | import k202 from "../http/marks/tell.json"; | |
| 207 | import k203 from "../http/marks/templates.json"; | |
| 208 | import k204 from "../http/marks/templates.json.json"; | |
| 209 | import k205 from "../http/marks/terms.json"; | |
| 210 | import k206 from "../http/marks/theme.json"; | |
| 211 | import k207 from "../http/marks/toolbar-lab.js.json"; | |
| 212 | import k208 from "../http/marks/toolbar-lab.json"; | |
| 213 | import k209 from "../http/marks/traces.json"; | |
| 214 | import k210 from "../http/marks/verify.json"; | |
| 215 | import k211 from "../http/marks/welcome.json"; | |
| 216 | import k212 from "../http/marks/well-known.json"; | |
| 216 | 217 | |
| 217 | 218 | /** Each file of `http/marks/`, parsed, in name order. */ |
| 218 | 219 | export const MARK_FILES: readonly unknown[] = [ |
| @@ -428,4 +429,5 @@ export const MARK_FILES: readonly unknown[] = [ | ||
| 428 | 429 | k209, |
| 429 | 430 | k210, |
| 430 | 431 | k211, |
| 432 | k212, | |
| 431 | 433 | ]; |
src/http/api.ts +2
| @@ -382,6 +382,7 @@ import { rollbackRoutes } from "./api/rollback.ts"; | ||
| 382 | 382 | import { takeBackRoutes } from "./api/take_back.ts"; |
| 383 | 383 | import { conversationConfirmRoutes } from "./api/conversation_confirms.ts"; |
| 384 | 384 | import { conversationTalkRoutes } from "./api/conversation_talk.ts"; |
| 385 | import { traceRoutes } from "./api/traces.ts"; | |
| 385 | 386 | import { conversationBuildRoutes } from "./api/conversation_build.ts"; |
| 386 | 387 | import { orgRoutes } from "./api/orgs.ts"; |
| 387 | 388 | import { promptRoutes } from "./api/prompts.ts"; |
| @@ -2404,6 +2405,7 @@ export function apiRoutes(router: Router<Handler>, isPage: (path: string) => boo | ||
| 2404 | 2405 | takeBackRoutes(router); |
| 2405 | 2406 | conversationConfirmRoutes(router); |
| 2406 | 2407 | conversationTalkRoutes(router); |
| 2408 | traceRoutes(router); | |
| 2407 | 2409 | conversationBuildRoutes(router); |
| 2408 | 2410 | |
| 2409 | 2411 | // Which stage of the one flow each open issue is in, and what went live in |
src/http/api/traces.ts added +131
| @@ -0,0 +1,131 @@ | ||
| 1 | /** | |
| 2 | * GET /api/v1/traces (#18021): the trace index newest first, a page at a time, as `{ items, | |
| 3 | * next_cursor }`. The index (`trace/index_table.ts`) knows nothing of who may see a row, so this is | |
| 4 | * where it is decided, by the rule that gates the run's log (`mayReadLogOfRun`): a run whose log | |
| 5 | * the caller may not read is absent, never a hint it exists; a Talk turn is its person's alone. | |
| 6 | * Only the index is read, never a raw log. | |
| 7 | */ | |
| 8 | ||
| 9 | import { mayReadLogOfRun } from "../../actions/runs.ts"; | |
| 10 | import { cursorRefusal, limitRefusal, parseLimit } from "../../contract/page_query.ts"; | |
| 11 | import type { App } from "../../app.ts"; | |
| 12 | import { getRepo, getRepoById, type Repo } from "../../core/repos.ts"; | |
| 13 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; | |
| 14 | import type { User } from "../../core/users.ts"; | |
| 15 | import { cursorOf, DEFAULT_LIMIT, listTraceIndex, MAX_LIMIT, TRACE_CURSOR, type TraceKind, type TraceQuery, type TraceRow } from "../../trace/index_table.ts"; | |
| 16 | import type { Handler } from "../context.ts"; | |
| 17 | import { apiError, json, requireScope } from "../respond.ts"; | |
| 18 | import type { Router } from "../router.ts"; | |
| 19 | ||
| 20 | const ACCEPTED = ["repository", "kind", "model", "outcome", "since", "until", "cursor", "limit"] as const; | |
| 21 | const KINDS: readonly string[] = ["run", "talk", "review"]; | |
| 22 | const OUTCOMES: readonly string[] = ["ok", "failed", "cancelled", "stopped"]; | |
| 23 | /** An instant: a date, or a date and time with a zone. */ | |
| 24 | const INSTANT = /^\d{4}-\d\d-\d\d(T\d\d:\d\d(:\d\d(\.\d{1,3})?)?(Z|[+-]\d\d:\d\d))?$/; | |
| 25 | const WORD = /^[\w./:-]{1,100}$/; | |
| 26 | /** A repository id no row has (a Talk turn's is empty, which a filter must not match). */ | |
| 27 | const NO_REPOSITORY = "-"; | |
| 28 | /** Rows looked at (in index pages) before a page is answered short with a cursor to go on from. */ | |
| 29 | const SCAN_PAGES = 10; | |
| 30 | ||
| 31 | const instant = (raw: string): string | undefined => { | |
| 32 | if (!INSTANT.test(raw)) return undefined; | |
| 33 | const at = new Date(raw); | |
| 34 | return Number.isNaN(at.getTime()) ? undefined : at.toISOString(); | |
| 35 | }; | |
| 36 | ||
| 37 | /** Whether `actor` may see this row: the answer for a repository is asked once per request. */ | |
| 38 | function seeing(app: App, actor: User): (row: TraceRow) => boolean { | |
| 39 | const repos = new Map<string, Repo | undefined>(); | |
| 40 | return (row) => { | |
| 41 | if (row.kind === "talk") { | |
| 42 | const owner = app.db.query<{ user_id: string }, [string]>("SELECT user_id FROM conversation_run WHERE id = ?1").get(row.id); | |
| 43 | return owner?.user_id === actor.id; | |
| 44 | } | |
| 45 | if (row.kind !== "run") return false; | |
| 46 | if (!repos.has(row.repository)) repos.set(row.repository, getRepoById(app.db, row.repository)); | |
| 47 | const repo = repos.get(row.repository); | |
| 48 | return repo !== undefined && mayReadLogOfRun(app, actor, repo, row.id); | |
| 49 | }; | |
| 50 | } | |
| 51 | ||
| 52 | export const listTraces: Handler = (ctx) => { | |
| 53 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 54 | const denied = requireScope(ctx, "ro"); | |
| 55 | if (denied) return denied; | |
| 56 | const actor = ctx.auth.user!; | |
| 57 | const q = ctx.url.searchParams; | |
| 58 | const unknown = [...new Set(q.keys())].filter((k) => !(ACCEPTED as readonly string[]).includes(k)); | |
| 59 | if (unknown.length > 0) | |
| 60 | return json( | |
| 61 | { | |
| 62 | error: "unknown_parameter", | |
| 63 | message: `this list does not read ${unknown.map((k) => JSON.stringify(k.slice(0, 40))).join(", ")}; it reads ${ACCEPTED.join(", ")}`, | |
| 64 | accepted: ACCEPTED, | |
| 65 | }, | |
| 66 | 422, | |
| 67 | ); | |
| 68 | const limit = parseLimit(q.get("limit"), MAX_LIMIT, DEFAULT_LIMIT); | |
| 69 | if (limit === undefined) return limitRefusal(MAX_LIMIT); | |
| 70 | const cursor = q.get("cursor"); | |
| 71 | if (cursor !== null && !TRACE_CURSOR.test(cursor)) return cursorRefusal(); | |
| 72 | const query: TraceQuery = {}; | |
| 73 | const kind = q.get("kind"); | |
| 74 | if (kind !== null) { | |
| 75 | if (!KINDS.includes(kind)) return apiError("invalid_kind", 400, `kind is ${KINDS.join(", ")}`); | |
| 76 | query.kind = kind as TraceKind; | |
| 77 | } | |
| 78 | const outcome = q.get("outcome"); | |
| 79 | if (outcome !== null) { | |
| 80 | if (!OUTCOMES.includes(outcome)) return apiError("invalid_outcome", 400, `outcome is ${OUTCOMES.join(", ")}`); | |
| 81 | query.outcome = outcome; | |
| 82 | } | |
| 83 | const model = q.get("model"); | |
| 84 | if (model !== null) { | |
| 85 | if (!WORD.test(model)) return apiError("invalid_model", 400, "model is a model name of up to 100 characters"); | |
| 86 | query.model = model; | |
| 87 | } | |
| 88 | for (const name of ["since", "until"] as const) { | |
| 89 | const raw = q.get(name); | |
| 90 | if (raw === null) continue; | |
| 91 | const at = instant(raw); | |
| 92 | if (at === undefined) return apiError(`invalid_${name}`, 400, `${name} is a date or an ISO 8601 instant with a zone`); | |
| 93 | query[name] = at; | |
| 94 | } | |
| 95 | const repository = q.get("repository"); | |
| 96 | if (repository !== null) { | |
| 97 | if (!WORD.test(repository)) return apiError("invalid_repository", 400, "repository is a repository id or owner/name"); | |
| 98 | const [owner, name, ...more] = repository.split("/"); | |
| 99 | // An owner/name that names nothing matches no row, the same empty list a hidden one gives. | |
| 100 | query.repository = name === undefined ? repository : more.length > 0 ? NO_REPOSITORY : (getRepo(ctx.app.db, owner!, name)?.id ?? NO_REPOSITORY); | |
| 101 | } | |
| 102 | ||
| 103 | const may = seeing(ctx.app, actor); | |
| 104 | const items: TraceRow[] = []; | |
| 105 | let next: string | null = null; | |
| 106 | timePhase(ctx.request, "traces", () => { | |
| 107 | let from = cursor ?? undefined; | |
| 108 | for (let pages = 0; pages < SCAN_PAGES; pages += 1) { | |
| 109 | const got = listTraceIndex(ctx.app.db, { ...query, ...(from === undefined ? {} : { cursor: from }), limit: MAX_LIMIT }); | |
| 110 | for (const row of got.rows) { | |
| 111 | if (!may(row)) continue; | |
| 112 | if (items.length === limit) { | |
| 113 | next = cursorOf(items[items.length - 1]!); | |
| 114 | return; | |
| 115 | } | |
| 116 | items.push(row); | |
| 117 | } | |
| 118 | if (got.next === undefined) return; | |
| 119 | from = got.next; | |
| 120 | } | |
| 121 | // Looked at enough without filling the page: go on from where the look stopped. | |
| 122 | next = from ?? null; | |
| 123 | }); | |
| 124 | return new Response(JSON.stringify({ items, next_cursor: next }), { | |
| 125 | headers: { "content-type": "application/json", "cache-control": "private, no-store", vary: "authorization, cookie" }, | |
| 126 | }); | |
| 127 | }; | |
| 128 | ||
| 129 | export function traceRoutes(router: Router<Handler>): void { | |
| 130 | router.add("GET", "/api/v1/traces", listTraces); | |
| 131 | } |
src/http/lazy_lists.ts +1
| @@ -1484,6 +1484,7 @@ export const API_ROUTES = [ | ||
| 1484 | 1484 | ["GET", "/api/v1/models"], |
| 1485 | 1485 | ["POST", "/api/v1/conversations"], |
| 1486 | 1486 | ["PATCH", "/api/v1/conversations/:id"], |
| 1487 | ["GET", "/api/v1/traces"], | |
| 1487 | 1488 | ["POST", "/api/v1/builds"], |
| 1488 | 1489 | ["GET", "/api/v1/conversations/:id/build"], |
| 1489 | 1490 | ["GET", "/api/v1/conversations/:id/build/versions/:n/diff"], |
src/http/marks/api-traces.json added +4
src/trace/index_table.ts +6 −3
| @@ -268,7 +268,10 @@ export const DEFAULT_LIMIT = 50; | ||
| 268 | 268 | export const MAX_LIMIT = 200; |
| 269 | 269 | |
| 270 | 270 | /** `<started>|<id>`: where the last row of a page was; anything else is no cursor. */ |
| 271 | const CURSOR = /^(\d{4}-\d\d-\d\dT[0-9:.]{8,12}Z)\|([0-9a-z]{1,40})$/; | |
| 271 | export const TRACE_CURSOR = /^(\d{4}-\d\d-\d\dT[0-9:.]{8,12}Z)\|([0-9a-z]{1,40})$/; | |
| 272 | ||
| 273 | /** Where a row sits in the listing: what a page's `next` and a `cursor` say. */ | |
| 274 | export const cursorOf = (row: { readonly started: string; readonly id: string }): string => `${row.started}|${row.id}`; | |
| 272 | 275 | |
| 273 | 276 | interface Stored { |
| 274 | 277 | id: string; |
| @@ -307,7 +310,7 @@ export function listTraceIndex(db: Database, query: TraceQuery = {}): TracePage | ||
| 307 | 310 | add("outcome = ?", query.outcome); |
| 308 | 311 | add("started >= ?", query.since); |
| 309 | 312 | add("started < ?", query.until); |
| 310 | const at = query.cursor === undefined ? null : CURSOR.exec(query.cursor); | |
| 313 | const at = query.cursor === undefined ? null : TRACE_CURSOR.exec(query.cursor); | |
| 311 | 314 | if (at !== null) { |
| 312 | 315 | args.push(at[1]!, at[2]!); |
| 313 | 316 | where.push(`(started < ?${args.length - 1} OR (started = ?${args.length - 1} AND id < ?${args.length}))`); |
| @@ -339,6 +342,6 @@ export function listTraceIndex(db: Database, query: TraceQuery = {}): TracePage | ||
| 339 | 342 | started: r.started, |
| 340 | 343 | ended: r.ended, |
| 341 | 344 | })), |
| 342 | next: more && last !== undefined ? `${last.started}|${last.id}` : undefined, | |
| 345 | next: more && last !== undefined ? cursorOf(last) : undefined, | |
| 343 | 346 | }; |
| 344 | 347 | } |
test/traces-api.test.ts added +137
| @@ -0,0 +1,137 @@ | ||
| 1 | /** | |
| 2 | * GET /api/v1/traces (#18021): the index newest first, filtered, paged by cursor, and gated as a | |
| 3 | * run's log is. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { createRepo } from "../src/actions/repos.ts"; | |
| 11 | import { createToken } from "../src/core/tokens.ts"; | |
| 12 | import { createUser } from "../src/core/users.ts"; | |
| 13 | import { claimRun, createRun, finishRun } from "../src/core/runs.ts"; | |
| 14 | import { newConversation } from "../src/core/conversations.ts"; | |
| 15 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 16 | import { indexRun, indexTalkTurn } from "../src/trace/index_table.ts"; | |
| 17 | import { seedAdmin } from "./helpers/admin.ts"; | |
| 18 | ||
| 19 | let dir = ""; | |
| 20 | let app: App; | |
| 21 | let servers: Servers; | |
| 22 | let alice = ""; | |
| 23 | let bob = ""; | |
| 24 | let notesId = ""; | |
| 25 | let otherId = ""; | |
| 26 | const ids: string[] = []; | |
| 27 | ||
| 28 | const get = async (token: string, query = ""): Promise<{ status: number; body: any }> => { | |
| 29 | const res = await fetch(`${servers.url}/api/v1/traces${query}`, { headers: { authorization: `Bearer ${token}` } }); | |
| 30 | return { status: res.status, body: await res.json() }; | |
| 31 | }; | |
| 32 | ||
| 33 | /** A run that ended and was indexed, then dated: the index row is what the route lists. */ | |
| 34 | const ended = (repoId: string, actorId: string, summary: string, started: string, state: "ok" | "failed"): string => { | |
| 35 | const run = createRun(app.db, { repoId, kind: "agent", actorId }); | |
| 36 | for (const step of [() => claimRun(app.db, run.id), () => finishRun(app.db, run.id, { state, summary, tokensIn: 10, tokensOut: 5 }), () => indexRun(app.db, dir, run.id)]) step(); | |
| 37 | app.db.query("UPDATE trace_index SET started = ?2, ended = ?2 WHERE id = ?1").run(run.id, started); | |
| 38 | return run.id; | |
| 39 | }; | |
| 40 | ||
| 41 | beforeAll(async () => { | |
| 42 | dir = await mkdtemp(join(tmpdir(), "forge-traces-api-")); | |
| 43 | app = createApp({ dataDir: dir, port: 0 }); | |
| 44 | servers = await serve(app); | |
| 45 | const a = await seedAdmin(app, { username: "alice", email: "[email protected]", password: "correct-horse-battery" }); | |
| 46 | if (!a.ok) throw new Error(a.error); | |
| 47 | const b = await createUser(app.db, { origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-good-passphrase" }); | |
| 48 | if (!b.ok) throw new Error(b.error); | |
| 49 | alice = createToken(app.db, a.value.id, "t", "ro").token; | |
| 50 | bob = createToken(app.db, b.value.id, "t", "ro").token; | |
| 51 | const notes = await createRepo(app, a.value, { name: "notes", visibility: "public", description: "" }); | |
| 52 | const other = await createRepo(app, a.value, { name: "other", visibility: "public", description: "" }); | |
| 53 | if (!notes.ok || !other.ok) throw new Error("repo"); | |
| 54 | notesId = notes.value.id; | |
| 55 | otherId = other.value.id; | |
| 56 | // Five on notes, newest last written, and one on other. | |
| 57 | for (let i = 1; i <= 5; i += 1) ids.push(ended(notesId, a.value.id, `run ${i}`, `2026-12-0${i}T10:00:00.000Z`, i === 2 ? "failed" : "ok")); | |
| 58 | ended(otherId, a.value.id, "elsewhere", "2026-12-04T12:00:00.000Z", "ok"); | |
| 59 | }, 30_000); | |
| 60 | ||
| 61 | afterAll(async () => { | |
| 62 | servers.stop(); | |
| 63 | app.close(); | |
| 64 | await rm(dir, { recursive: true, force: true }); | |
| 65 | }); | |
| 66 | ||
| 67 | test("a repository filter lists that repository's traces newest first", async () => { | |
| 68 | const { status, body } = await get(alice, "?repository=" + notesId); | |
| 69 | expect(status).toBe(200); | |
| 70 | expect(body.items.map((r: { title: string }) => r.title)).toEqual(["run 5", "run 4", "run 3", "run 2", "run 1"]); | |
| 71 | expect(body.next_cursor).toBeNull(); | |
| 72 | const byName = await get(alice, "?repository=alice/other"); | |
| 73 | expect(byName.body.items.map((r: { title: string }) => r.title)).toEqual(["elsewhere"]); | |
| 74 | expect((await get(alice, "?repository=alice/nothing")).body.items).toEqual([]); | |
| 75 | }); | |
| 76 | ||
| 77 | test("kind, outcome, since and until narrow the list", async () => { | |
| 78 | expect((await get(alice, "?outcome=failed")).body.items.map((r: { title: string }) => r.title)).toEqual(["run 2"]); | |
| 79 | expect((await get(alice, "?kind=talk")).body.items).toEqual([]); | |
| 80 | const window = await get(alice, `?repository=${notesId}&since=2026-12-02&until=2026-12-04T10:00:00Z`); | |
| 81 | expect(window.body.items.map((r: { title: string }) => r.title)).toEqual(["run 3", "run 2"]); | |
| 82 | }); | |
| 83 | ||
| 84 | test("a page of 2 over 5 rows walks by cursor to the end", async () => { | |
| 85 | const seen: string[] = []; | |
| 86 | let cursor = ""; | |
| 87 | for (let page = 0; page < 5; page += 1) { | |
| 88 | const { body } = await get(alice, `?repository=${notesId}&limit=2${cursor}`); | |
| 89 | seen.push(...body.items.map((r: { title: string }) => r.title)); | |
| 90 | if (body.next_cursor === null) { | |
| 91 | expect(body.items.length).toBe(1); | |
| 92 | break; | |
| 93 | } | |
| 94 | expect(body.items.length).toBe(2); | |
| 95 | cursor = `&cursor=${encodeURIComponent(body.next_cursor)}`; | |
| 96 | } | |
| 97 | expect(seen).toEqual(["run 5", "run 4", "run 3", "run 2", "run 1"]); | |
| 98 | }); | |
| 99 | ||
| 100 | test("a run whose log the caller may not read is absent, and a page still fills", async () => { | |
| 101 | const mine = await get(alice, `?repository=${notesId}`); | |
| 102 | expect(mine.body.items.length).toBe(5); | |
| 103 | // Bob may read the public repository's runs but not their logs: no row, no cursor, no count. | |
| 104 | const theirs = await get(bob, ""); | |
| 105 | expect(theirs.status).toBe(200); | |
| 106 | expect(theirs.body).toEqual({ items: [], next_cursor: null }); | |
| 107 | // A Talk turn is its person's alone. | |
| 108 | const convo = newConversation(app.db, (app.db.query("SELECT id FROM user WHERE username = 'alice'").get() as { id: string }).id); | |
| 109 | app.db | |
| 110 | .query( | |
| 111 | "INSERT INTO conversation_run (id, conversation_id, user_id, state, prompt, started_at, finished_at, model) VALUES ('talk1', ?1, (SELECT id FROM user WHERE username = 'alice'), 'ok', 'hello', '2026-12-04T13:00:00.000Z', '2026-12-04T13:00:01.000Z', 'm')", | |
| 112 | ) | |
| 113 | .run(convo.id); | |
| 114 | expect(indexTalkTurn(app.db, dir, "talk1")).toBe(true); | |
| 115 | expect((await get(alice, "?kind=talk")).body.items.map((r: { id: string }) => r.id)).toEqual(["talk1"]); | |
| 116 | expect((await get(bob, "?kind=talk")).body.items).toEqual([]); | |
| 117 | }); | |
| 118 | ||
| 119 | test("a bad value is refused at the boundary", async () => { | |
| 120 | expect((await get(alice, "?since=yesterday")).status).toBe(400); | |
| 121 | expect((await get(alice, "?since=yesterday")).body.error).toBe("invalid_since"); | |
| 122 | expect((await get(alice, "?until=2026-13-45")).body.error).toBe("invalid_until"); | |
| 123 | expect((await get(alice, "?kind=bogus")).status).toBe(400); | |
| 124 | expect((await get(alice, "?outcome=bogus")).status).toBe(400); | |
| 125 | expect((await get(alice, "?limit=0")).status).toBe(422); | |
| 126 | expect((await get(alice, "?limit=201")).body.error).toBe("invalid_limit"); | |
| 127 | expect((await get(alice, "?cursor=junk")).body.error).toBe("invalid_cursor"); | |
| 128 | const unknown = await get(alice, "?colour=red"); | |
| 129 | expect(unknown.status).toBe(422); | |
| 130 | expect(unknown.body.error).toBe("unknown_parameter"); | |
| 131 | }); | |
| 132 | ||
| 133 | test("it answers a signed-in caller only, and names its session phase", async () => { | |
| 134 | expect((await fetch(`${servers.url}/api/v1/traces`)).status).toBe(401); | |
| 135 | const res = await fetch(`${servers.url}/api/v1/traces`, { headers: { authorization: `Bearer ${alice}` } }); | |
| 136 | expect(res.headers.get("server-timing") ?? "").toContain("session"); | |
| 137 | }); |
new above
offline
This comment describes SCAN_PAGES but sits above NO_REPOSITORY, so NO_REPOSITORY has two doc comments and SCAN_PAGES has none. Move it to the line above
const SCAN_PAGES = 10;.