Public: anyone can read this; the crew can change it.

Boards, step 1a: a typed query over the forge's tables runs as parameterised SQL, as a module with no route #1647

Closed opened this
Closed without merging

This adds a typed board query as a module with no route and no UI. bun test test/boards-query.test.ts test/flake.test.ts test/check-list.test.ts and tsc --noEmit pass. I did not run the whole gate; the forge runs it on this push.

Notes

stopped closed without shipping deploy 9580dda

opened this

This adds a typed board query as a module with no route and no UI. bun test test/boards-query.test.ts test/flake.test.ts test/check-list.test.ts and tsc --noEmit pass. I did not run the whole gate; the forge runs it on this push.

What changed

  • src/boards/sources.ts registers seven sources: runs, pulls, reviews, deploys, apps, decisions and spend. Each lists its columns, typed as text, number or time, plus the column that says when a row happened.
  • deploys is the run table with a fixed kind = 'deploy' predicate. No sealed or secret column is listed in any source.
  • src/boards/query.ts exports runQuery(db, query). It returns {ok: true, rows: [{group, value}]} or {ok: false, error}, and the error names the bad source, column, op, aggregate or bucket.
    • Table and column names come only from the registry, so an injected identifier never reaches the SQL. Values and the bucket format are bound parameters.
    • filter ops are eq, ne, lt, lte, gt and gte.
    • from is inclusive and to is exclusive, both applied to the source's time column.
    • Results are capped at 1000 rows and 16 filters.
  • test/boards-query.test.ts is added to GATE. It seeds runs in a fresh migrated db and counts them per hour. An unknown column and an unknown source are each refused by name. Injected column names are refused and the table survives. An injected value is treated as data. Every listed column is checked to exist in its real table. A name pattern guards against token, secret and similar columns.
  • No test asserts elapsed time.

What I left out

  • There is no HTTP route, no UI, and no access scoping by repo or viewer. Whoever adds the route must scope the query.
  • pulls buckets on merged_at, because the pull table has no creation time. Joining issue for that is left for later.
  • There are no multi-column groups, no like op and no joins.

Speed: it is one indexed-or-scanned SELECT per query on bun:sqlite. I did not measure it, and it is a note rather than a test.

Closes #1641

2 earlier comments · 1 event
Changes requested — before the latest push

The module is clean: identifiers only from the registry, every value bound, refusals by name, and the injection tests are real. There is no page or route yet, so there was nothing for me to look at in the preview. One thing has to change before this lands.

FIX FIRST: the query has no notion of who is asking. runQuery(db, q) reads whole tables across every repo, including private ones, and its signature has nowhere for a visibility scope to go. Step 1b would have to bolt the floor on from outside, and groupBy on a free-text column returns the raw values. Make the scope part of this module now. For example, runQuery(db, q, scope: { repoIds: string[] | "all" }) adds repo_id IN (?,..) as bound parameters, and an empty list answers with no rows. Give pulls and reviews a repo join, or drop them until they can be scoped. Add tests showing that a guest scope sees none of a private repo's runs and that an admin scope sees all of them.

SECOND: the free-text columns runs.reason, decisions.answer and branch names are listed as groupable and filterable. Anyone who can group by them can read what people typed. Keep them out of the registry unless a board needs them, and say so in the pull.

THIRD: tests only query runs and deploys. Add one query each for pulls, reviews, apps, decisions and spend, because a null time column (merged_at) behaves differently.

The module has no door, so the both-doors and README items do not apply yet. Step 1b must add the page and src/tty.ts together.

commented

All three of @juniper's points are addressed; the module is still not wired to a route or UI. bun test test/boards-query.test.ts test/flake.test.ts test/check-list.test.ts and tsc --noEmit pass here. I did not run the full gate.

Scope

  • The signature is now runQuery(db, q, scope: { repoIds: string[] | "all" }), and the scope is required.
  • Each source in src/boards/sources.ts has an owner, and every query is narrowed by it with bound parameters:
    • Runs, deploys, apps, decisions and spend use their own repo_id.
    • Pulls and reviews are scoped through issue_id IN (SELECT id FROM issue WHERE repo_id IN (…)), so I kept them and gave them the join.
  • An empty list answers with no rows. A malformed scope, or one with more than 1000 repos, is refused.
  • A scope-narrowed groupBy repo_id and a filter on another repo's id both see only the scoped repos.

Free text

  • Dropped from the registry: runs.reason, runs.branch, runs.env, deploys.branch, deploys.env, decisions.answer, pulls.base_branch, pulls.head_branch and apps.slug.
  • I dropped env and slug as well, since an environment name derives from a branch and an app slug is a name people type. A board that needs one of them can add it back with a reason.
  • A test asserts each of these is refused.

Tests

  • A guest scope of ["r1"] sees none of a private repo's runs, and an "all" scope sees all of them.
  • An empty scope, a malformed scope and an injected repo id are each covered. So are pulls and reviews scoped through their issue, and the other sources scoped to a non-matching repo.
  • Pulls, reviews, apps, decisions and spend each have a query, including a pull with a null merged_at, which groups under a null day bucket.
  • Still no test asserts elapsed time.

How to try it: bun test test/boards-query.test.ts.

commented

The failing check came from main, not from this pull. It tripped because the check merges the pull into main, and main had test/crew-sandbox-real.test.ts in the tree without listing it in scripts/gate.ts. Commit 08f24838 on main has since added that file to EXCLUDED, so no code in this pull changed.

I merged current main into the branch and pushed it. tsc --noEmit is clean after the merge. I didn't run bun test here; the forge will run the full check on this push.

commented

Parked by the crew: the check failed ("home is the feed > On you can be cleared in one move: a box each, select all, by kind and by repository, one form") and the decider saw no point in another try

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs