justintsus-experiment Public
TSUS experiment: briefs, rubric, hidden tests, both builds' search modules, the judge's report and probes, usage
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Changed files
52 files changed6,451 additions0 deletionsin the first commit
- AddedLICENSE+25−0
- AddedREADME.md+98−0
- Addedbrief/BRIEF-ts.md+328−0
- Addedbrief/BRIEF-tsus.md+346−0
- Addedbrief/BRIEF.md+369−0
- Addedbrief/RUBRIC.md+110−0
- Addedbuild-a-ts/apps/web/src/search/api.ts+15−0
- Addedbuild-a-ts/apps/web/src/search/ast.ts+57−0
- Addedbuild-a-ts/apps/web/src/search/edit.ts+49−0
- Addedbuild-a-ts/apps/web/src/search/parse.ts+309−0
- Addedbuild-a-ts/apps/web/src/search/serialize.ts+56−0
- Addedbuild-a-ts/apps/web/src/search/sql.ts+140−0
- Addedbuild-a-ts/apps/web/src/ui/search.ts+97−0
- Addedbuild-a-ts/test/unit/search/pages.test.ts+113−0
- Addedbuild-a-ts/test/unit/search/query.test.ts+178−0
- Addedbuild-a-ts/test/unit/search/sql.test.ts+203−0
- Addedbuild-b-tsus/apps/web/src/search/api.ts+51−0
- Addedbuild-b-tsus/apps/web/src/search/ast.tsus+62−0
- Addedbuild-b-tsus/apps/web/src/search/edit.tsus+55−0
- Addedbuild-b-tsus/apps/web/src/search/lex.tsus+104−0
- Addedbuild-b-tsus/apps/web/src/search/lists.ts+140−0
- Addedbuild-b-tsus/apps/web/src/search/parse.tsus+223−0
- Addedbuild-b-tsus/apps/web/src/search/serialize.tsus+55−0
- Addedbuild-b-tsus/apps/web/src/search/sql.tsus+200−0
- Addedbuild-b-tsus/apps/web/src/search/values.tsus+143−0
- Addedbuild-b-tsus/apps/web/src/ui/search.ts+133−0
- Addedbuild-b-tsus/test/unit/search/pages.test.ts+124−0
- Addedbuild-b-tsus/test/unit/search/parse.test.ts+184−0
- Addedbuild-b-tsus/test/unit/search/sql.test.ts+257−0
- Addedhidden-tests/dates.test.mjs+107−0
- Addedhidden-tests/helpers.mjs+212−0
- Addedhidden-tests/parse.test.mjs+239−0
- Addedhidden-tests/permission.test.mjs+103−0
- Addedhidden-tests/results/ts.out+164−0
- Addedhidden-tests/results/tsus.out+164−0
- Addedhidden-tests/run.sh+61−0
- Addedhidden-tests/semantics.test.mjs+179−0
- Addedhidden-tests/serialize.test.mjs+96−0
- Addedhidden-tests/sql-safety.test.mjs+114−0
- Addedjudge/JUDGE.md+287−0
- Addedjudge/probes/probe.mjs+189−0
- Addedjudge/probes/probe.out+82−0
- Addedjudge/probes/render.mjs+137−0
- Addedjudge/probes/shots.mjs+56−0
- Addedjudge/probes/show.mjs+21−0
- Addedjudge/shots/A-issues-invalid-1280-light.png
- Addedjudge/shots/A-issues-query-1280-light.png
- Addedjudge/shots/A-pulls-default-help-390-dark.png
- Addedjudge/shots/B-issues-invalid-1280-light.png
- Addedjudge/shots/B-issues-query-1280-light.png
- Addedjudge/shots/B-pulls-default-help-390-dark.png
- Addedusage.json+16−0
AddedLICENSE+25−0
| @@ -0,0 +1,25 @@ | ||
| +MIT License | ||
| + | ||
| +Copyright (c) 2026 Justin Torre | ||
| + | ||
| +Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| +of this software and associated documentation files (the "Software"), to deal | ||
| +in the Software without restriction, including without limitation the rights | ||
| +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| +copies of the Software, and to permit persons to whom the Software is | ||
| +furnished to do so, subject to the following conditions: | ||
| + | ||
| +The above copyright notice and this permission notice shall be included in all | ||
| +copies or substantial portions of the Software. | ||
| + | ||
| +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| +SOFTWARE. | ||
| + | ||
| +Note: build-a-ts/ and build-b-tsus/ are excerpts of Rebasely source code, | ||
| +published under this license by Rebasely's owner for this experiment. The rest | ||
| +of Rebasely is not covered by this license. | ||
AddedREADME.md+98−0
| @@ -0,0 +1,98 @@ | ||
| +# TSUS experiment: plain TypeScript against TypeScript-Ultra-Strict | ||
| + | ||
| +Two Claude agents built the same Rebasely feature from the same brief. Agent A wrote plain strict | ||
| +TypeScript. Agent B wrote [TypeScript-Ultra-Strict](https://rebasely.com/justin/typescript-ultra-strict) | ||
| +(TSUS), a stricter dialect with no `null`, `undefined`, `as`, `!`, `any`, `class` or `throw`, plus | ||
| +`Option`, `Result` and an exhaustive `match`. A judge agent then scored both builds against a rubric | ||
| +written before either agent started. | ||
| + | ||
| +The feature: a GitHub-style search query language (`is:open label:bug author:@me sort:updated`) for | ||
| +Rebasely's Issues and Pull requests lists. It parses the query, prints a canonical form, and turns it | ||
| +into parameterized SQL for D1, scoped to the repository and organization. | ||
| + | ||
| +The write-up is on the Rebasely blog: https://rebasely.com/blog/strict-typescript-ai-code | ||
| + | ||
| +## How it was run | ||
| + | ||
| +1. One brief, two copies. [`brief/BRIEF-ts.md`](brief/BRIEF-ts.md) and | ||
| + [`brief/BRIEF-tsus.md`](brief/BRIEF-tsus.md) differ only in their "Language" section | ||
| + ([`brief/BRIEF.md`](brief/BRIEF.md) is the template). Each agent worked alone in its own git | ||
| + worktree of Rebasely at commit `903f762f`, on the same model (Claude Opus 5.5), with no access to the other's work | ||
| + or to the tests. | ||
| +2. 118 hidden tests ([`hidden-tests/`](hidden-tests)), written before the builds from the brief | ||
| + alone: parsing, canonical form and round trips, semantics on SQLite, dates, SQL injection and | ||
| + scope. `run.sh <worktree>` runs them against a build. | ||
| +3. A judge agent scored each build against [`brief/RUBRIC.md`](brief/RUBRIC.md): hidden tests (40), | ||
| + code review (30), UI (20), cost (10), minus security penalties. A gap under 5 points is a tie. | ||
| + The judge also fuzzed both builds against each other and reviewed the page wiring for security | ||
| + ([`judge/JUDGE.md`](judge/JUDGE.md), probes in [`judge/probes/`](judge/probes)). | ||
| +4. The harness recorded each agent's tokens, tool calls and wall time ([`usage.json`](usage.json)). | ||
| + | ||
| +The judge knew which build was which: `.tsus` files cannot be hidden. The rubric tells it not to | ||
| +reward or penalize the language itself. | ||
| + | ||
| +## Results | ||
| + | ||
| +| | A, plain TypeScript | B, TSUS | | ||
| +|---|---|---| | ||
| +| Hidden tests passed | 118 / 118 | 118 / 118 | | ||
| +| Judge's total (of 100) | 98.0 | 95.9 | | ||
| +| Code review (of 30) | 28.5 | 27.0 | | ||
| +| UI (of 20) | 19.5 | 20.0 | | ||
| +| Cost (of 10) | 10.0 | 8.86 | | ||
| +| Security findings | 0 | 0 | | ||
| +| Tokens | 277,747 | 290,102 (+4.4%) | | ||
| +| Output tokens | 35.7k | 57.7k (+61%) | | ||
| +| Tool calls | 117 | 150 (+28%) | | ||
| +| Wall time | 25.9 min | 25.1 min | | ||
| +| Search module lines | 626 | 1,033 (+65%) | | ||
| +| `as` casts | 11 | 0 | | ||
| +| New unit tests | 17 | 28 | | ||
| +| TSUS diagnostics during the build | n/a | 0 in 8 check runs | | ||
| + | ||
| +On 20,400 random inputs plus about 300 targeted probes the two builds returned the same results, | ||
| +errors, canonical forms and SQLite rows. The only differences were outside the brief: B fails on a | ||
| +row whose `labels` column is not valid JSON (A guards it with `json_valid`), and B throws on | ||
| +`parseQuery(null)`. | ||
| + | ||
| +## Verdict | ||
| + | ||
| +A tie on quality, and plain TypeScript was cheaper. The TSUS build had no casts where the plain | ||
| +build had 11, all of them safe. TSUS caught nothing during the build: every check run came back | ||
| +clean, because the agent read the rules first and wrote inside them. In exchange it took 33 more tool | ||
| +calls, 61% more output tokens and 65% more code, including a 51-line adapter between `Option` and | ||
| +`Result` and the plain-TypeScript page code. The build that shipped in Rebasely is A, with B's | ||
| +full-width search box. | ||
| + | ||
| +On this feature, TSUS did not measurably beat strict TypeScript for an AI builder. Where it did help | ||
| +was an earlier pilot that rewrote existing code (Rebasely's on-call watcher) in TSUS: following the | ||
| +`Option` and `match` rewrite turned up two real production bugs, described in the blog post. | ||
| + | ||
| +## What is here | ||
| + | ||
| +| Path | What | | ||
| +|---|---| | ||
| +| `brief/` | The template brief, the two briefs the agents got, and the judge's rubric | | ||
| +| `hidden-tests/` | The 118 hidden tests, their SQLite fixture (`helpers.mjs`) and `run.sh`; `results/` has both runs | | ||
| +| `build-a-ts/` | Build A's new files: `apps/web/src/search/*.ts`, `apps/web/src/ui/search.ts`, `test/unit/search/` | | ||
| +| `build-b-tsus/` | Build B's new files: `apps/web/src/search/*.tsus` and `api.ts`, `lists.ts`, `ui/search.ts`, `test/unit/search/` | | ||
| +| `judge/JUDGE.md` | The judge's full report and scores | | ||
| +| `judge/probes/` | The judge's differential probes, page renderer and screenshot script, and the probe output | | ||
| +| `judge/shots/` | Six of the judge's 72 screenshots | | ||
| +| `usage.json` | Tokens, tool calls and milliseconds per agent, from the harness | | ||
| + | ||
| +The builds are excerpts: only the feature's new files, not the routes, pages and styles they changed | ||
| +in the rest of Rebasely, which is not open source. So they do not run on their own here. The hidden | ||
| +tests only need `apps/web/src/search/api.ts` of a build, and import it from `SEARCH_WORKTREE`; | ||
| +B's `.tsus` files need the TSUS compiler first. The judge's scripts take the two checkouts from | ||
| +`WORKTREE_A` and `WORKTREE_B`. | ||
| + | ||
| +## Links | ||
| + | ||
| +- TypeScript-Ultra-Strict: https://rebasely.com/justin/typescript-ultra-strict | ||
| +- The blog post: https://rebasely.com/blog/strict-typescript-ai-code | ||
| + | ||
| +## License | ||
| + | ||
| +MIT, see [LICENSE](LICENSE). The two builds are excerpts of Rebasely source, published by its owner | ||
| +for this experiment. | ||
Addedbrief/BRIEF-ts.md+328−0
| @@ -0,0 +1,328 @@ | ||
| +# Brief: a search query language for the Issues and Pull requests lists | ||
| + | ||
| +You are building one feature in the Rebasely codebase: a GitHub-style search query language for the | ||
| +repository Issues list and the repository Pull requests list. Another engineer is building the same | ||
| +feature from the same brief in parallel; the two results are compared on hidden tests, code review | ||
| +and a UI check. Work alone, inside your own worktree, and do not look at other worktrees. | ||
| + | ||
| +## Setup | ||
| + | ||
| +- The repository is at the worktree path you were given (a git worktree of | ||
| + the Rebasely checkout). Work and commit only there, on your worktree branch. | ||
| +- **Do not push, deploy, or run any `wrangler deploy`, migration against a remote database, or | ||
| + release script.** Commit your work on your worktree branch; the judge reads the branch. | ||
| +- Node resolves dependencies from the main checkout's `node_modules`, so a worktree needs its own | ||
| + links to the workspace packages before anything runs. From the worktree root: | ||
| + | ||
| + ```sh | ||
| + mkdir -p node_modules/@duragit node_modules/@tsus | ||
| + for p in actions:apps/actions cli:packages/cli deadman:apps/deadman shared:packages/shared ssh:apps/ssh web:apps/web; do | ||
| + ln -sfn "../../${p#*:}" "node_modules/@duragit/${p%%:*}" | ||
| + done | ||
| + ln -sfn ../../packages/git node_modules/@duragit/git | ||
| + ln -sfn ../../tools/tsus/packages/runtime node_modules/@tsus/runtime | ||
| + ``` | ||
| + | ||
| + (If your shell tool refuses a command, run the `ln` lines one at a time.) | ||
| + | ||
| +- Read `DESIGN.md` (UI rules: one accent color, Inter 450, no decorative stripes, gradients, emoji, | ||
| + uppercase kickers or em dashes in UI copy) before touching pages. | ||
| +- Checks that must pass before you finish: `npm run typecheck`, `npm run lint`, and the unit tests you | ||
| + add (`node --test test/unit/search/*.test.ts` or wherever you put them). The full `npm run test:unit` | ||
| + is slow; run at least the suites near what you change. | ||
| +- Do not read anything outside your worktree and your own scratchpad directory. In particular do not | ||
| + read anything else under `/private/tmp` (other sessions' scratchpads), other worktrees under | ||
| + `.claude/worktrees`, the main checkout's working tree, or other copies of any tool. | ||
| + Everything you need is in the worktree. | ||
| + | ||
| +## What exists today | ||
| + | ||
| +- Issues live in D1, table `issues` (`apps/web/migrations/0020_issues.sql`): `repo_id, number, | ||
| + org_id, author_id, title, body, state ('open'|'closed'), labels (a JSON array of lowercase | ||
| + strings, default '[]'), comments, created_at, updated_at, closed_at, closed_by` (times are epoch | ||
| + milliseconds). The list query is `listIssueViews` in `apps/web/src/issues/service.ts`; the page is | ||
| + `issuesPage` in `apps/web/src/repo/issues-page.ts`, rendered by `issuesListBody` in | ||
| + `apps/web/src/ui/issues.ts`, which already has a small search form (`q`, titles or `#number`). | ||
| +- Pull requests live in each repository's RepoDO (`apps/web/src/do/repo/pulls.ts`, `prList`), with a | ||
| + D1 copy kept by the DO's outbox: table `pull_index` (`apps/web/migrations/0013_pulls.sql`): | ||
| + `repo_id, number, org_id, author_id, owner_id (the agent's owner for an agent's PR, else NULL), | ||
| + state ('open'|'closed'|'merged'), draft (0|1), title, base_ref, head_ref, created_at, updated_at, | ||
| + merged_at, closed_at`, and `pull_review_requests (repo_id, number, user_id, requested_at)`. The | ||
| + page is `pullsPage` in `apps/web/src/repo/pulls-page.ts`, rendered by `pullsListBody` in | ||
| + `apps/web/src/ui/pulls.ts`. | ||
| +- People have no login handle: a user is a UUID (`users.id`). Agents are users too, with a slug per | ||
| + organization (`agents (user_id, org_id, slug, owner_user_id, ...)`, slug `^[a-z0-9][a-z0-9-]{0,38}$`, | ||
| + `AGENT_SLUG` in `packages/shared/agents.ts`). Labels follow `cleanLabels` in | ||
| + `packages/shared/issues.ts`: lowercase, `^[a-z0-9][a-z0-9 ._-]*$`, at most 32 characters. | ||
| +- There are no assignees, no milestones and no labels on pull requests. | ||
| +- The routes already decide `repo.read` before the list runs (`prContext`), so a repository the | ||
| + viewer cannot read never reaches your code. Your SQL still scopes every row to the repository and | ||
| + its organization (see "SQL contract"), so nothing a person types can widen it. | ||
| + | ||
| +## Language | ||
| + | ||
| +Write everything in plain TypeScript (`.ts`), following the repository's conventions: `tsconfig.v2.json` | ||
| +(strict, `erasableSyntaxOnly`, `verbatimModuleSyntax`, imports with explicit `.ts` extensions, so Node | ||
| +runs the files directly with type stripping). Do not use TypeScript-Ultra-Strict (`.tsus` files or | ||
| +`@tsus/runtime`), even though the repository has it wired up for other work. | ||
| + | ||
| +Put the parser, the canonical serializer and the SQL evaluator in `apps/web/src/search/` (as many | ||
| +files as you like). `apps/web/src/search/api.ts` exports the interface below, directly or by | ||
| +re-exporting. Treat a bad query as data, not an exception: `parseQuery` never throws for any string | ||
| +input, and `toSql` never throws for a query `parseQuery` accepted. The route and page code is `.ts` | ||
| +as everywhere else in `apps/web/src`. | ||
| + | ||
| +`npm run typecheck` is your type check; it must pass. | ||
| + | ||
| +## The module interface (what the hidden tests import) | ||
| + | ||
| +Create `apps/web/src/search/api.ts`. The hidden tests import exactly this file, under Node 24 with | ||
| +type stripping (`node --test`, no bundler, no wrangler), after running `npm run tsus:web`. So | ||
| +`api.ts` and everything it imports must load in plain Node: relative imports and `@duragit/shared/*` | ||
| +are fine; no `cloudflare:*` modules, no `env` bindings, no D1 at import time. | ||
| + | ||
| +```ts | ||
| +export type Target = "issues" | "pulls"; | ||
| +export type SqlValue = string | number | null; | ||
| + | ||
| +export type QueryErrorCode = | ||
| + | "too_long" | "too_many_terms" | "unterminated_quote" | "unexpected_quote" | ||
| + | "unknown_qualifier" | "unsupported_qualifier" | "empty_value" | "negation_not_allowed" | ||
| + | "bad_value" | "bad_date" | "conflict"; | ||
| + | ||
| +// start/end: UTF-16 code unit offsets into the input, end exclusive. | ||
| +export type QueryError = { code: QueryErrorCode; message: string; start: number; end: number }; | ||
| + | ||
| +// The parsed query. Its shape is yours (an AST); the tests treat it as opaque and only pass it back | ||
| +// to serializeQuery and toSql. | ||
| +export type SearchQuery = /* your AST type */; | ||
| + | ||
| +export function parseQuery(input: string, target: Target): | ||
| + { ok: true; query: SearchQuery } | { ok: false; errors: QueryError[] }; | ||
| + | ||
| +export function serializeQuery(query: SearchQuery): string; | ||
| + | ||
| +export type SqlCtx = { | ||
| + repoId: string; // the repository being listed | ||
| + orgId: string; // its organization | ||
| + viewerId: string | null; // the signed-in user's id, null when signed out | ||
| + now: number; // epoch milliseconds, for relative dates | ||
| +}; | ||
| + | ||
| +// A boolean SQL expression and an ORDER BY list for the target's table, with every value bound. | ||
| +export function toSql(query: SearchQuery, ctx: SqlCtx): { where: string; params: SqlValue[]; orderBy: string }; | ||
| +``` | ||
| + | ||
| +These are plain JavaScript values at the boundary (`ok` booleans, arrays, strings, numbers, `null` | ||
| +for a signed-out viewer) whatever you use inside. `message` is a short sentence for people (shown | ||
| +in the UI), never empty. | ||
| + | ||
| +## The query language | ||
| + | ||
| +### Terms | ||
| + | ||
| +An input is a sequence of terms separated by runs of whitespace (any character JavaScript's `\s` | ||
| +matches, so a no-break space or an ideographic space separates terms too). Every term is ANDed with | ||
| +the others. There is no OR, no parentheses, no NOT keyword: `OR` is just a word. | ||
| + | ||
| +Reading one term, starting at its first non-whitespace character: | ||
| + | ||
| +1. **Negation.** A `-` immediately followed by a non-whitespace character negates the term and is | ||
| + not part of it. A `-` alone (followed by whitespace or the end) is the free-text word `-`. | ||
| +2. **Quoted value.** A `"` starts a quoted value. Inside, `\"` is a `"` and `\\` is a `\`; any other | ||
| + backslash is kept as it is. The next unescaped `"` ends it. No closing quote: error | ||
| + `unterminated_quote` spanning from the term's start to the end of the input, and parsing stops | ||
| + there. After the closing quote the term must end (whitespace or end of input), otherwise | ||
| + `unexpected_quote`. | ||
| +3. **Bare text** runs to the next whitespace. If it reaches a `"`, that is only allowed when the bare | ||
| + text so far is exactly a qualifier key and its colon (`label:"needs review"`): the quoted value is | ||
| + the qualifier's value. Any other `"` inside bare text is `unexpected_quote`. | ||
| + An `unexpected_quote` error spans from the term's start to the next whitespace (or the end), and | ||
| + parsing goes on after it. | ||
| +4. **Kinds.** A bare term matching `^[A-Za-z]+(-[A-Za-z]+)*:` is a qualifier: the key (lowercased) is | ||
| + the part before the first colon, the value is everything after it (or the quoted value). A bare | ||
| + term matching `^#[1-9][0-9]{0,8}$` is a number. Anything else, and every quoted term, is free text. | ||
| + So `12:30`, `a1:b` and `:x` are free text, `http://x` is the qualifier `http` (unknown), and | ||
| + `"label:bug"` is the phrase `label:bug`. | ||
| + | ||
| +### Qualifiers and values | ||
| + | ||
| +| Qualifier | Targets | Values | Negation | | ||
| +|---|---|---|---| | ||
| +| `is:` | both | `open`, `closed`; for pulls also `merged`, `draft` | allowed | | ||
| +| `label:` | issues | a label name | allowed | | ||
| +| `no:` | issues | `label` | not allowed | | ||
| +| `author:` | both | a user reference | allowed | | ||
| +| `review-requested:` | pulls | a user reference | allowed | | ||
| +| `sort:` | both | `created-desc`, `created-asc`, `updated-desc`, `updated-asc`; for issues also `comments-desc`, `comments-asc` | not allowed | | ||
| +| `created:` `updated:` | both | a date filter | not allowed | | ||
| +| `#123` | both | the number | not allowed | | ||
| +| free text, "phrases" | both | any non-empty text | allowed | | ||
| + | ||
| +Values are case-insensitive: `is:`, `no:`, `sort:`, `author:` and `review-requested:` values are | ||
| +lowercased. A **label** value is trimmed, lowercased and has each internal whitespace run collapsed | ||
| +to one space; the result must match `^[a-z0-9][a-z0-9 ._-]*$` and be at most 32 characters. A | ||
| +**user reference** is `@me`, a UUID (`^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$` | ||
| +after lowercasing; checked before the slug rule) or an agent slug (`^[a-z0-9][a-z0-9-]{0,38}$`). | ||
| +Anything else after `@` is a `bad_value`. | ||
| + | ||
| +### Date filters | ||
| + | ||
| +All dates are UTC calendar days, `YYYY-MM-DD` exactly (zero-padded, a real date, year 1970 to 9999). | ||
| +Let `start(D)` be D at 00:00:00.000Z in epoch ms and `DAY` = 86,400,000. | ||
| + | ||
| +| Value | Matches a timestamp `t` when | | ||
| +|---|---| | ||
| +| `D` | `start(D) <= t < start(D) + DAY` | | ||
| +| `>D` | `t >= start(D) + DAY` | | ||
| +| `>=D` | `t >= start(D)` | | ||
| +| `<D` | `t < start(D)` | | ||
| +| `<=D` | `t < start(D) + DAY` | | ||
| +| `A..B` | `start(A) <= t < start(B) + DAY`; A after B is `bad_date` | | ||
| +| `A..*` | `t >= start(A)` | | ||
| +| `*..B` | `t < start(B) + DAY` | | ||
| + | ||
| +Relative values compare **age** (`now - t`) with a duration `N` units, where N is 1 to 9999 (leading | ||
| +zeros allowed, dropped in the canonical form) and the unit is `h` (3,600,000 ms), `d` (86,400,000) | ||
| +or `w` (604,800,000), case-insensitive. With `T = now - N * unit`: | ||
| + | ||
| +| Value | Reads as | Matches when | | ||
| +|---|---|---| | ||
| +| `<7d` | younger than 7 days | `t > T` | | ||
| +| `<=7d` | at most 7 days old | `t >= T` | | ||
| +| `>7d` | older than 7 days | `t < T` | | ||
| +| `>=7d` | at least 7 days old | `t <= T` | | ||
| + | ||
| +A relative value needs an operator (`updated:7d` is `bad_date`); ranges take absolute dates or `*` | ||
| +only (`*..*` is `bad_date`); `=` is not an operator. Anything else is `bad_date`. | ||
| + | ||
| +### Errors | ||
| + | ||
| +Every invalid term produces exactly one error spanning the whole term, from its first character | ||
| +(including a leading `-`) to its last (including quotes); errors are listed in input order. When | ||
| +any error exists, `parseQuery` returns `ok: false` with all of them. For a qualifier term the checks | ||
| +run in this order and the first failure is the error: | ||
| + | ||
| +1. `unknown_qualifier`: the key is not in the table (for example `assignee:`, `repo:`, `milestone:`). | ||
| +2. `unsupported_qualifier`: the key exists but not for this target (`label:` and `no:` on pulls, | ||
| + `review-requested:` on issues). | ||
| +3. `empty_value`: nothing after the colon, or `""`. | ||
| +4. `negation_not_allowed`: a `-` on `no:`, `sort:`, `created:` or `updated:`. | ||
| +5. `bad_value` (or `bad_date` for `created:` and `updated:`): the value is not valid for the key and | ||
| + target, for example `is:merged` on issues or `sort:comments-desc` on pulls. | ||
| + | ||
| +Other errors: a negated number (`-#5`) is `negation_not_allowed`; a free-text quoted value with no | ||
| +non-whitespace character (`""`, `" "`) is `empty_value`; a second `sort:` term with a different | ||
| +value is `conflict` (on the second term). | ||
| + | ||
| +Limits, checked before anything else and reported alone (a single error, nothing else): | ||
| + | ||
| +- `too_long`: the input is longer than 1024 UTF-16 code units; the error spans the whole input | ||
| + (`start` 0, `end` the input's length). | ||
| +- `too_many_terms`: more than 32 terms, counted as written (before duplicates are removed, invalid | ||
| + terms included); the error spans from the 33rd term's start to the end of the input. | ||
| + | ||
| +An empty or whitespace-only input is a valid, empty query. | ||
| + | ||
| +### Canonical form (`serializeQuery`) | ||
| + | ||
| +The canonical form is what goes in the page URL (`?q=`), so the URL round-trips: for any valid | ||
| +input `x`, with `c = serializeQuery(parseQuery(x).query)`, `parseQuery(c)` succeeds, | ||
| +`serializeQuery` of it is `c` again, and `toSql` gives the same `where`, `params` and `orderBy` for | ||
| +both. Rules: | ||
| + | ||
| +- Terms keep their input order, joined by single spaces. A term whose canonical text equals an | ||
| + earlier term's is dropped. | ||
| +- Keys are lowercase. Values are normalized as above (`IS:Open` is `is:open`, `label:" Needs | ||
| + Review "` is `label:"needs review"`, `author:@ME` is `author:@me`, `updated:<007D` is | ||
| + `updated:<7d`). Dates are written as given (`created:>=2026-10-01`, `created:2026-10-01..*`). | ||
| +- Negated terms start with `-`. | ||
| +- A qualifier value is written bare when it matches `^[^\s"]+$`, otherwise quoted. | ||
| +- A free-text value is written bare when it matches `^[^\s"]+$`, does not start with `-`, does not | ||
| + match the qualifier pattern `^[A-Za-z]+(-[A-Za-z]+)*:` and does not match the number pattern | ||
| + `^#[1-9][0-9]{0,8}$`; otherwise quoted. So `"bug"` is written `bug`, `"label:x"` stays `"label:x"`, | ||
| + and a lone `-` is written `"-"`. | ||
| +- Quoting writes `"`, the value with each `\` as `\\` and each `"` as `\"`, then `"`. Phrases keep | ||
| + their inner whitespace exactly. | ||
| +- The empty query is the empty string. | ||
| + | ||
| +### Semantics | ||
| + | ||
| +| Term | Issues (`issues`) | Pulls (`pull_index`) | | ||
| +|---|---|---| | ||
| +| `is:open` | `state = 'open'` | `state = 'open'` (drafts included) | | ||
| +| `is:closed` | `state = 'closed'` | `state` is `'closed'` or `'merged'` (as on GitHub) | | ||
| +| `is:merged` | n/a | `state = 'merged'` | | ||
| +| `is:draft` | n/a | `draft = 1`, in any state | | ||
| +| `label:x` | `x` is in the `labels` JSON array | n/a | | ||
| +| `no:label` | `labels` is an empty array | n/a | | ||
| +| `author:u` | `author_id` is u | `author_id` is u, or `owner_id` is u (an agent's PR shows under its owner, as the list does today) | | ||
| +| `review-requested:u` | n/a | a `pull_review_requests` row for this PR with `user_id` u | | ||
| +| `#n` | `number = n` | `number = n` | | ||
| +| free text `w` | `w` occurs in `title` or in `body` | `w` occurs in `title` | | ||
| +| `created:` `updated:` | `created_at` / `updated_at` per the date tables | same | | ||
| + | ||
| +- A negated term matches exactly the rows (in scope) its positive form does not match. Watch NULLs: | ||
| + `-author:u` on pulls must match a PR whose `owner_id` is NULL and whose author is someone else. | ||
| +- User references: `@me` is `ctx.viewerId`; with no viewer, `author:@me` and | ||
| + `review-requested:@me` match nothing and their negations match everything. A UUID is that user id. | ||
| + A slug is the agent with that slug **in `ctx.orgId`** (`agents.org_id`); a slug of another | ||
| + organization's agent, or of no agent, matches nothing. | ||
| +- Free text and phrases match as substrings with SQLite `LIKE` semantics (ASCII case-insensitive, | ||
| + other characters exactly), with `%`, `_` and `\` in the text matched literally (escape them; use | ||
| + `ESCAPE '\'`). A phrase is one substring, inner spaces included; each bare word is its own term. | ||
| +- Two `is:` terms are ANDed like everything else: `is:open is:closed` matches nothing. No term is | ||
| + implied: the empty query matches every row in scope, in every state. (The page supplies `is:open` | ||
| + as its default; the evaluator does not.) | ||
| +- `sort:` picks the order; the default is `created-desc`. `orderBy` is `<column> <ASC|DESC>, number | ||
| + <ASC|DESC>` with the tie-break in the same direction: `created_at`, `updated_at` or `comments`. | ||
| + | ||
| +### SQL contract (`toSql`) | ||
| + | ||
| +- The result is used as `SELECT ... FROM <table> WHERE ${where} AND <more conditions> ORDER BY | ||
| + ${orderBy} LIMIT ?`, where `<table>` is `issues` for an issues query and `pull_index` for a pulls | ||
| + query, without an alias. `where` must keep its meaning when more conditions are ANDed after it | ||
| + (parenthesize any top-level OR). It may reference the table by name and use subqueries | ||
| + (`json_each`, `pull_review_requests`, `agents`). | ||
| +- **Every value is bound.** Nothing from the input or from `ctx` (ids included) is ever written into | ||
| + the SQL text; `params` carries them, in placeholder order. Use `?` placeholders (`?NNN` numbered | ||
| + ones are allowed if you use them for every placeholder). The SQL text may contain only constant | ||
| + literals you wrote yourself (`'open'`, `'\'` in `ESCAPE '\'`, `0`, `1`). | ||
| +- **Scope cannot be dropped.** `where` always requires `repo_id = ctx.repoId` and `org_id = | ||
| + ctx.orgId` at its top level, ANDed with the query's conditions, for every query including the | ||
| + empty one. No term can widen the scope. | ||
| +- `orderBy` is built only from the fixed column names and directions above. | ||
| +- `toSql` itself never fails for a query `parseQuery` accepted for the same target. | ||
| + | ||
| +## The pages | ||
| + | ||
| +- **Issues** (`/:org/:repo/issues`) and **Pull requests** (`/:org/:repo/pulls`): the list is driven by | ||
| + `?q=` in the query language. With no `q`, the page uses `is:open` (and still honors the old | ||
| + `state=` and `label=` links by turning them into terms). Run the search through `parseQuery` and | ||
| + `toSql` against D1 for both lists: issues from `issues`, pull requests from `pull_index`. (The | ||
| + RepoDO stays the source of truth for a PR's own page; `pull_index` may lag a few seconds, which is | ||
| + fine for a list. You may hydrate rows from the RepoDO if the list needs fields `pull_index` lacks, | ||
| + or render what `pull_index` has.) Keep 25 per page with a way to the next page that keeps `q`. | ||
| +- The search box shows the canonical form of the current query. Links the page makes (state tabs, | ||
| + next page, a label chip, "Clear") use canonical `q` values. The Open / Closed (and Merged) tabs | ||
| + replace the query's `is:` state term. | ||
| +- **Errors inline:** a query with errors renders the page (status 200) with the input kept as typed, | ||
| + each error's message under the box, and the offending spans marked in a way that does not rely on | ||
| + color alone. No results are listed for an invalid query. | ||
| +- **Syntax help:** a small popover or disclosure next to the box listing the qualifiers that apply to | ||
| + that list, with one example each, usable by keyboard and screen readers and without JavaScript | ||
| + (for example a `<details>` element). | ||
| +- Follow `DESIGN.md` and the existing list styles. No new JavaScript framework; a little progressive | ||
| + enhancement is fine. | ||
| + | ||
| +## Out of scope | ||
| + | ||
| +- `/inbox`, the `/api/v1` list endpoints (keep their current `q` behavior), the MCP tools, saved | ||
| + searches, autocomplete, assignees and milestones, labels on pull requests. | ||
| +- Migrations: none are needed. Do not add one. | ||
| + | ||
| +## When you are done | ||
| + | ||
| +Commit on your worktree branch with a message in the repository's style, and reply with: what you | ||
| +built, the files, how you checked it (commands and results), and anything you know is missing or | ||
| +wrong. | ||
Addedbrief/BRIEF-tsus.md+346−0
| @@ -0,0 +1,346 @@ | ||
| +# Brief: a search query language for the Issues and Pull requests lists | ||
| + | ||
| +You are building one feature in the Rebasely codebase: a GitHub-style search query language for the | ||
| +repository Issues list and the repository Pull requests list. Another engineer is building the same | ||
| +feature from the same brief in parallel; the two results are compared on hidden tests, code review | ||
| +and a UI check. Work alone, inside your own worktree, and do not look at other worktrees. | ||
| + | ||
| +## Setup | ||
| + | ||
| +- The repository is at the worktree path you were given (a git worktree of | ||
| + the Rebasely checkout). Work and commit only there, on your worktree branch. | ||
| +- **Do not push, deploy, or run any `wrangler deploy`, migration against a remote database, or | ||
| + release script.** Commit your work on your worktree branch; the judge reads the branch. | ||
| +- Node resolves dependencies from the main checkout's `node_modules`, so a worktree needs its own | ||
| + links to the workspace packages before anything runs. From the worktree root: | ||
| + | ||
| + ```sh | ||
| + mkdir -p node_modules/@duragit node_modules/@tsus | ||
| + for p in actions:apps/actions cli:packages/cli deadman:apps/deadman shared:packages/shared ssh:apps/ssh web:apps/web; do | ||
| + ln -sfn "../../${p#*:}" "node_modules/@duragit/${p%%:*}" | ||
| + done | ||
| + ln -sfn ../../packages/git node_modules/@duragit/git | ||
| + ln -sfn ../../tools/tsus/packages/runtime node_modules/@tsus/runtime | ||
| + ``` | ||
| + | ||
| + (If your shell tool refuses a command, run the `ln` lines one at a time.) | ||
| + | ||
| +- Read `DESIGN.md` (UI rules: one accent color, Inter 450, no decorative stripes, gradients, emoji, | ||
| + uppercase kickers or em dashes in UI copy) before touching pages. | ||
| +- Checks that must pass before you finish: `npm run typecheck`, `npm run lint`, and the unit tests you | ||
| + add (`node --test test/unit/search/*.test.ts` or wherever you put them). The full `npm run test:unit` | ||
| + is slow; run at least the suites near what you change. | ||
| +- Do not read anything outside your worktree and your own scratchpad directory. In particular do not | ||
| + read anything else under `/private/tmp` (other sessions' scratchpads), other worktrees under | ||
| + `.claude/worktrees`, the main checkout's working tree, or other copies of any tool. | ||
| + Everything you need is in the worktree. | ||
| + | ||
| +## What exists today | ||
| + | ||
| +- Issues live in D1, table `issues` (`apps/web/migrations/0020_issues.sql`): `repo_id, number, | ||
| + org_id, author_id, title, body, state ('open'|'closed'), labels (a JSON array of lowercase | ||
| + strings, default '[]'), comments, created_at, updated_at, closed_at, closed_by` (times are epoch | ||
| + milliseconds). The list query is `listIssueViews` in `apps/web/src/issues/service.ts`; the page is | ||
| + `issuesPage` in `apps/web/src/repo/issues-page.ts`, rendered by `issuesListBody` in | ||
| + `apps/web/src/ui/issues.ts`, which already has a small search form (`q`, titles or `#number`). | ||
| +- Pull requests live in each repository's RepoDO (`apps/web/src/do/repo/pulls.ts`, `prList`), with a | ||
| + D1 copy kept by the DO's outbox: table `pull_index` (`apps/web/migrations/0013_pulls.sql`): | ||
| + `repo_id, number, org_id, author_id, owner_id (the agent's owner for an agent's PR, else NULL), | ||
| + state ('open'|'closed'|'merged'), draft (0|1), title, base_ref, head_ref, created_at, updated_at, | ||
| + merged_at, closed_at`, and `pull_review_requests (repo_id, number, user_id, requested_at)`. The | ||
| + page is `pullsPage` in `apps/web/src/repo/pulls-page.ts`, rendered by `pullsListBody` in | ||
| + `apps/web/src/ui/pulls.ts`. | ||
| +- People have no login handle: a user is a UUID (`users.id`). Agents are users too, with a slug per | ||
| + organization (`agents (user_id, org_id, slug, owner_user_id, ...)`, slug `^[a-z0-9][a-z0-9-]{0,38}$`, | ||
| + `AGENT_SLUG` in `packages/shared/agents.ts`). Labels follow `cleanLabels` in | ||
| + `packages/shared/issues.ts`: lowercase, `^[a-z0-9][a-z0-9 ._-]*$`, at most 32 characters. | ||
| +- There are no assignees, no milestones and no labels on pull requests. | ||
| +- The routes already decide `repo.read` before the list runs (`prContext`), so a repository the | ||
| + viewer cannot read never reaches your code. Your SQL still scopes every row to the repository and | ||
| + its organization (see "SQL contract"), so nothing a person types can widen it. | ||
| + | ||
| +## Language | ||
| + | ||
| +Write the parser, the canonical serializer and the SQL evaluator in TypeScript-Ultra-Strict | ||
| +(`.tsus` files) in `apps/web/src/search/` (as many files as you like). tsus is TypeScript with the | ||
| +escape hatches removed: no `null`, `undefined`, `class`, `var` or `throw`; `Option` and `Result` | ||
| +from `@tsus/runtime`; a typed `match` macro checked for exhaustiveness at compile time; a `?` | ||
| +operator for early return; and 13 further bans (`any`, `as`, `!`, `enum`, `==`, default exports, | ||
| +parameter mutation, floating promises, unused `Result`, untagged error types and more). **Read | ||
| +`tools/tsus/AGENTS.md` first**; it is the language reference (the vendored compiler and runtime are | ||
| +in `tools/tsus`, pinned in `tools/tsus/VERSION`). | ||
| + | ||
| +How it builds here (already wired, see `scripts/tsus-web.mjs`): | ||
| + | ||
| +- Every `apps/web/src/**/x.tsus` compiles to `x.js`, `x.js.map` and `x.d.ts` next to it | ||
| + (gitignored; never commit or edit them). The wrangler build, `npm run typecheck` and | ||
| + `npm run test:unit` run the compile first; run it yourself with `npm run tsus:web`. | ||
| +- One `.tsus` file imports another as `"./y.tsus"`, and the runtime as `"@tsus/runtime"` and | ||
| + `"@tsus/runtime/boundary"`. Plain `.ts` code imports a `.tsus` module as `"./x.js"` (the compiled | ||
| + file; tsc reads `x.d.ts`). Do not put `x.ts` next to `x.tsus`. | ||
| +- Your loop: `node scripts/tsus-web.mjs check --json` (the tsus rules plus a strict type check of the | ||
| + `.tsus` files; a TypeScript error comes back as `TSUS200` with `tsCode`), `node scripts/tsus-web.mjs | ||
| + fix` (and `fix --unsafe`) for machine fixes, and `node tools/tsus/packages/cli/bin/tsus.js explain | ||
| + <CODE>` for any rule. Use these instead of `tsus check apps/web` or `tsus fix apps/web`: the stock | ||
| + command type-checks every `.ts` file under `src/` without the Worker types and drowns your | ||
| + diagnostics in hundreds of false ones. `npm run typecheck` must pass too. | ||
| + | ||
| +`apps/web/src/search/api.ts` is a thin plain `.ts` adapter over your `.tsus` modules: it converts | ||
| +`Result` and `Option` into the plain objects of the interface below (`{ ok: true, query }` or | ||
| +`{ ok: false, errors }`, `viewerId: string | null` into whatever you use) and nothing else; the | ||
| +logic lives in `.tsus`. The routes and pages stay `.ts` and call the module through `api.ts` (or | ||
| +through the compiled `.js` modules directly). Treat a bad query as data: no exceptions, and the | ||
| +adapter never throws for any string input. | ||
| + | ||
| +## The module interface (what the hidden tests import) | ||
| + | ||
| +Create `apps/web/src/search/api.ts`. The hidden tests import exactly this file, under Node 24 with | ||
| +type stripping (`node --test`, no bundler, no wrangler), after running `npm run tsus:web`. So | ||
| +`api.ts` and everything it imports must load in plain Node: relative imports and `@duragit/shared/*` | ||
| +are fine; no `cloudflare:*` modules, no `env` bindings, no D1 at import time. | ||
| + | ||
| +```ts | ||
| +export type Target = "issues" | "pulls"; | ||
| +export type SqlValue = string | number | null; | ||
| + | ||
| +export type QueryErrorCode = | ||
| + | "too_long" | "too_many_terms" | "unterminated_quote" | "unexpected_quote" | ||
| + | "unknown_qualifier" | "unsupported_qualifier" | "empty_value" | "negation_not_allowed" | ||
| + | "bad_value" | "bad_date" | "conflict"; | ||
| + | ||
| +// start/end: UTF-16 code unit offsets into the input, end exclusive. | ||
| +export type QueryError = { code: QueryErrorCode; message: string; start: number; end: number }; | ||
| + | ||
| +// The parsed query. Its shape is yours (an AST); the tests treat it as opaque and only pass it back | ||
| +// to serializeQuery and toSql. | ||
| +export type SearchQuery = /* your AST type */; | ||
| + | ||
| +export function parseQuery(input: string, target: Target): | ||
| + { ok: true; query: SearchQuery } | { ok: false; errors: QueryError[] }; | ||
| + | ||
| +export function serializeQuery(query: SearchQuery): string; | ||
| + | ||
| +export type SqlCtx = { | ||
| + repoId: string; // the repository being listed | ||
| + orgId: string; // its organization | ||
| + viewerId: string | null; // the signed-in user's id, null when signed out | ||
| + now: number; // epoch milliseconds, for relative dates | ||
| +}; | ||
| + | ||
| +// A boolean SQL expression and an ORDER BY list for the target's table, with every value bound. | ||
| +export function toSql(query: SearchQuery, ctx: SqlCtx): { where: string; params: SqlValue[]; orderBy: string }; | ||
| +``` | ||
| + | ||
| +These are plain JavaScript values at the boundary (`ok` booleans, arrays, strings, numbers, `null` | ||
| +for a signed-out viewer) whatever you use inside. `message` is a short sentence for people (shown | ||
| +in the UI), never empty. | ||
| + | ||
| +## The query language | ||
| + | ||
| +### Terms | ||
| + | ||
| +An input is a sequence of terms separated by runs of whitespace (any character JavaScript's `\s` | ||
| +matches, so a no-break space or an ideographic space separates terms too). Every term is ANDed with | ||
| +the others. There is no OR, no parentheses, no NOT keyword: `OR` is just a word. | ||
| + | ||
| +Reading one term, starting at its first non-whitespace character: | ||
| + | ||
| +1. **Negation.** A `-` immediately followed by a non-whitespace character negates the term and is | ||
| + not part of it. A `-` alone (followed by whitespace or the end) is the free-text word `-`. | ||
| +2. **Quoted value.** A `"` starts a quoted value. Inside, `\"` is a `"` and `\\` is a `\`; any other | ||
| + backslash is kept as it is. The next unescaped `"` ends it. No closing quote: error | ||
| + `unterminated_quote` spanning from the term's start to the end of the input, and parsing stops | ||
| + there. After the closing quote the term must end (whitespace or end of input), otherwise | ||
| + `unexpected_quote`. | ||
| +3. **Bare text** runs to the next whitespace. If it reaches a `"`, that is only allowed when the bare | ||
| + text so far is exactly a qualifier key and its colon (`label:"needs review"`): the quoted value is | ||
| + the qualifier's value. Any other `"` inside bare text is `unexpected_quote`. | ||
| + An `unexpected_quote` error spans from the term's start to the next whitespace (or the end), and | ||
| + parsing goes on after it. | ||
| +4. **Kinds.** A bare term matching `^[A-Za-z]+(-[A-Za-z]+)*:` is a qualifier: the key (lowercased) is | ||
| + the part before the first colon, the value is everything after it (or the quoted value). A bare | ||
| + term matching `^#[1-9][0-9]{0,8}$` is a number. Anything else, and every quoted term, is free text. | ||
| + So `12:30`, `a1:b` and `:x` are free text, `http://x` is the qualifier `http` (unknown), and | ||
| + `"label:bug"` is the phrase `label:bug`. | ||
| + | ||
| +### Qualifiers and values | ||
| + | ||
| +| Qualifier | Targets | Values | Negation | | ||
| +|---|---|---|---| | ||
| +| `is:` | both | `open`, `closed`; for pulls also `merged`, `draft` | allowed | | ||
| +| `label:` | issues | a label name | allowed | | ||
| +| `no:` | issues | `label` | not allowed | | ||
| +| `author:` | both | a user reference | allowed | | ||
| +| `review-requested:` | pulls | a user reference | allowed | | ||
| +| `sort:` | both | `created-desc`, `created-asc`, `updated-desc`, `updated-asc`; for issues also `comments-desc`, `comments-asc` | not allowed | | ||
| +| `created:` `updated:` | both | a date filter | not allowed | | ||
| +| `#123` | both | the number | not allowed | | ||
| +| free text, "phrases" | both | any non-empty text | allowed | | ||
| + | ||
| +Values are case-insensitive: `is:`, `no:`, `sort:`, `author:` and `review-requested:` values are | ||
| +lowercased. A **label** value is trimmed, lowercased and has each internal whitespace run collapsed | ||
| +to one space; the result must match `^[a-z0-9][a-z0-9 ._-]*$` and be at most 32 characters. A | ||
| +**user reference** is `@me`, a UUID (`^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$` | ||
| +after lowercasing; checked before the slug rule) or an agent slug (`^[a-z0-9][a-z0-9-]{0,38}$`). | ||
| +Anything else after `@` is a `bad_value`. | ||
| + | ||
| +### Date filters | ||
| + | ||
| +All dates are UTC calendar days, `YYYY-MM-DD` exactly (zero-padded, a real date, year 1970 to 9999). | ||
| +Let `start(D)` be D at 00:00:00.000Z in epoch ms and `DAY` = 86,400,000. | ||
| + | ||
| +| Value | Matches a timestamp `t` when | | ||
| +|---|---| | ||
| +| `D` | `start(D) <= t < start(D) + DAY` | | ||
| +| `>D` | `t >= start(D) + DAY` | | ||
| +| `>=D` | `t >= start(D)` | | ||
| +| `<D` | `t < start(D)` | | ||
| +| `<=D` | `t < start(D) + DAY` | | ||
| +| `A..B` | `start(A) <= t < start(B) + DAY`; A after B is `bad_date` | | ||
| +| `A..*` | `t >= start(A)` | | ||
| +| `*..B` | `t < start(B) + DAY` | | ||
| + | ||
| +Relative values compare **age** (`now - t`) with a duration `N` units, where N is 1 to 9999 (leading | ||
| +zeros allowed, dropped in the canonical form) and the unit is `h` (3,600,000 ms), `d` (86,400,000) | ||
| +or `w` (604,800,000), case-insensitive. With `T = now - N * unit`: | ||
| + | ||
| +| Value | Reads as | Matches when | | ||
| +|---|---|---| | ||
| +| `<7d` | younger than 7 days | `t > T` | | ||
| +| `<=7d` | at most 7 days old | `t >= T` | | ||
| +| `>7d` | older than 7 days | `t < T` | | ||
| +| `>=7d` | at least 7 days old | `t <= T` | | ||
| + | ||
| +A relative value needs an operator (`updated:7d` is `bad_date`); ranges take absolute dates or `*` | ||
| +only (`*..*` is `bad_date`); `=` is not an operator. Anything else is `bad_date`. | ||
| + | ||
| +### Errors | ||
| + | ||
| +Every invalid term produces exactly one error spanning the whole term, from its first character | ||
| +(including a leading `-`) to its last (including quotes); errors are listed in input order. When | ||
| +any error exists, `parseQuery` returns `ok: false` with all of them. For a qualifier term the checks | ||
| +run in this order and the first failure is the error: | ||
| + | ||
| +1. `unknown_qualifier`: the key is not in the table (for example `assignee:`, `repo:`, `milestone:`). | ||
| +2. `unsupported_qualifier`: the key exists but not for this target (`label:` and `no:` on pulls, | ||
| + `review-requested:` on issues). | ||
| +3. `empty_value`: nothing after the colon, or `""`. | ||
| +4. `negation_not_allowed`: a `-` on `no:`, `sort:`, `created:` or `updated:`. | ||
| +5. `bad_value` (or `bad_date` for `created:` and `updated:`): the value is not valid for the key and | ||
| + target, for example `is:merged` on issues or `sort:comments-desc` on pulls. | ||
| + | ||
| +Other errors: a negated number (`-#5`) is `negation_not_allowed`; a free-text quoted value with no | ||
| +non-whitespace character (`""`, `" "`) is `empty_value`; a second `sort:` term with a different | ||
| +value is `conflict` (on the second term). | ||
| + | ||
| +Limits, checked before anything else and reported alone (a single error, nothing else): | ||
| + | ||
| +- `too_long`: the input is longer than 1024 UTF-16 code units; the error spans the whole input | ||
| + (`start` 0, `end` the input's length). | ||
| +- `too_many_terms`: more than 32 terms, counted as written (before duplicates are removed, invalid | ||
| + terms included); the error spans from the 33rd term's start to the end of the input. | ||
| + | ||
| +An empty or whitespace-only input is a valid, empty query. | ||
| + | ||
| +### Canonical form (`serializeQuery`) | ||
| + | ||
| +The canonical form is what goes in the page URL (`?q=`), so the URL round-trips: for any valid | ||
| +input `x`, with `c = serializeQuery(parseQuery(x).query)`, `parseQuery(c)` succeeds, | ||
| +`serializeQuery` of it is `c` again, and `toSql` gives the same `where`, `params` and `orderBy` for | ||
| +both. Rules: | ||
| + | ||
| +- Terms keep their input order, joined by single spaces. A term whose canonical text equals an | ||
| + earlier term's is dropped. | ||
| +- Keys are lowercase. Values are normalized as above (`IS:Open` is `is:open`, `label:" Needs | ||
| + Review "` is `label:"needs review"`, `author:@ME` is `author:@me`, `updated:<007D` is | ||
| + `updated:<7d`). Dates are written as given (`created:>=2026-10-01`, `created:2026-10-01..*`). | ||
| +- Negated terms start with `-`. | ||
| +- A qualifier value is written bare when it matches `^[^\s"]+$`, otherwise quoted. | ||
| +- A free-text value is written bare when it matches `^[^\s"]+$`, does not start with `-`, does not | ||
| + match the qualifier pattern `^[A-Za-z]+(-[A-Za-z]+)*:` and does not match the number pattern | ||
| + `^#[1-9][0-9]{0,8}$`; otherwise quoted. So `"bug"` is written `bug`, `"label:x"` stays `"label:x"`, | ||
| + and a lone `-` is written `"-"`. | ||
| +- Quoting writes `"`, the value with each `\` as `\\` and each `"` as `\"`, then `"`. Phrases keep | ||
| + their inner whitespace exactly. | ||
| +- The empty query is the empty string. | ||
| + | ||
| +### Semantics | ||
| + | ||
| +| Term | Issues (`issues`) | Pulls (`pull_index`) | | ||
| +|---|---|---| | ||
| +| `is:open` | `state = 'open'` | `state = 'open'` (drafts included) | | ||
| +| `is:closed` | `state = 'closed'` | `state` is `'closed'` or `'merged'` (as on GitHub) | | ||
| +| `is:merged` | n/a | `state = 'merged'` | | ||
| +| `is:draft` | n/a | `draft = 1`, in any state | | ||
| +| `label:x` | `x` is in the `labels` JSON array | n/a | | ||
| +| `no:label` | `labels` is an empty array | n/a | | ||
| +| `author:u` | `author_id` is u | `author_id` is u, or `owner_id` is u (an agent's PR shows under its owner, as the list does today) | | ||
| +| `review-requested:u` | n/a | a `pull_review_requests` row for this PR with `user_id` u | | ||
| +| `#n` | `number = n` | `number = n` | | ||
| +| free text `w` | `w` occurs in `title` or in `body` | `w` occurs in `title` | | ||
| +| `created:` `updated:` | `created_at` / `updated_at` per the date tables | same | | ||
| + | ||
| +- A negated term matches exactly the rows (in scope) its positive form does not match. Watch NULLs: | ||
| + `-author:u` on pulls must match a PR whose `owner_id` is NULL and whose author is someone else. | ||
| +- User references: `@me` is `ctx.viewerId`; with no viewer, `author:@me` and | ||
| + `review-requested:@me` match nothing and their negations match everything. A UUID is that user id. | ||
| + A slug is the agent with that slug **in `ctx.orgId`** (`agents.org_id`); a slug of another | ||
| + organization's agent, or of no agent, matches nothing. | ||
| +- Free text and phrases match as substrings with SQLite `LIKE` semantics (ASCII case-insensitive, | ||
| + other characters exactly), with `%`, `_` and `\` in the text matched literally (escape them; use | ||
| + `ESCAPE '\'`). A phrase is one substring, inner spaces included; each bare word is its own term. | ||
| +- Two `is:` terms are ANDed like everything else: `is:open is:closed` matches nothing. No term is | ||
| + implied: the empty query matches every row in scope, in every state. (The page supplies `is:open` | ||
| + as its default; the evaluator does not.) | ||
| +- `sort:` picks the order; the default is `created-desc`. `orderBy` is `<column> <ASC|DESC>, number | ||
| + <ASC|DESC>` with the tie-break in the same direction: `created_at`, `updated_at` or `comments`. | ||
| + | ||
| +### SQL contract (`toSql`) | ||
| + | ||
| +- The result is used as `SELECT ... FROM <table> WHERE ${where} AND <more conditions> ORDER BY | ||
| + ${orderBy} LIMIT ?`, where `<table>` is `issues` for an issues query and `pull_index` for a pulls | ||
| + query, without an alias. `where` must keep its meaning when more conditions are ANDed after it | ||
| + (parenthesize any top-level OR). It may reference the table by name and use subqueries | ||
| + (`json_each`, `pull_review_requests`, `agents`). | ||
| +- **Every value is bound.** Nothing from the input or from `ctx` (ids included) is ever written into | ||
| + the SQL text; `params` carries them, in placeholder order. Use `?` placeholders (`?NNN` numbered | ||
| + ones are allowed if you use them for every placeholder). The SQL text may contain only constant | ||
| + literals you wrote yourself (`'open'`, `'\'` in `ESCAPE '\'`, `0`, `1`). | ||
| +- **Scope cannot be dropped.** `where` always requires `repo_id = ctx.repoId` and `org_id = | ||
| + ctx.orgId` at its top level, ANDed with the query's conditions, for every query including the | ||
| + empty one. No term can widen the scope. | ||
| +- `orderBy` is built only from the fixed column names and directions above. | ||
| +- `toSql` itself never fails for a query `parseQuery` accepted for the same target. | ||
| + | ||
| +## The pages | ||
| + | ||
| +- **Issues** (`/:org/:repo/issues`) and **Pull requests** (`/:org/:repo/pulls`): the list is driven by | ||
| + `?q=` in the query language. With no `q`, the page uses `is:open` (and still honors the old | ||
| + `state=` and `label=` links by turning them into terms). Run the search through `parseQuery` and | ||
| + `toSql` against D1 for both lists: issues from `issues`, pull requests from `pull_index`. (The | ||
| + RepoDO stays the source of truth for a PR's own page; `pull_index` may lag a few seconds, which is | ||
| + fine for a list. You may hydrate rows from the RepoDO if the list needs fields `pull_index` lacks, | ||
| + or render what `pull_index` has.) Keep 25 per page with a way to the next page that keeps `q`. | ||
| +- The search box shows the canonical form of the current query. Links the page makes (state tabs, | ||
| + next page, a label chip, "Clear") use canonical `q` values. The Open / Closed (and Merged) tabs | ||
| + replace the query's `is:` state term. | ||
| +- **Errors inline:** a query with errors renders the page (status 200) with the input kept as typed, | ||
| + each error's message under the box, and the offending spans marked in a way that does not rely on | ||
| + color alone. No results are listed for an invalid query. | ||
| +- **Syntax help:** a small popover or disclosure next to the box listing the qualifiers that apply to | ||
| + that list, with one example each, usable by keyboard and screen readers and without JavaScript | ||
| + (for example a `<details>` element). | ||
| +- Follow `DESIGN.md` and the existing list styles. No new JavaScript framework; a little progressive | ||
| + enhancement is fine. | ||
| + | ||
| +## Out of scope | ||
| + | ||
| +- `/inbox`, the `/api/v1` list endpoints (keep their current `q` behavior), the MCP tools, saved | ||
| + searches, autocomplete, assignees and milestones, labels on pull requests. | ||
| +- Migrations: none are needed. Do not add one. | ||
| + | ||
| +## When you are done | ||
| + | ||
| +Commit on your worktree branch with a message in the repository's style, and reply with: what you | ||
| +built, the files, how you checked it (commands and results), and anything you know is missing or | ||
| +wrong. | ||
Addedbrief/BRIEF.md+369−0
| @@ -0,0 +1,369 @@ | ||
| +<!-- | ||
| +Orchestrator note (remove before sending). This is the shared brief with both variants of its one | ||
| +differing section. Send each builder its own file, BRIEF-ts.md or BRIEF-tsus.md (generated from this | ||
| +template by make-briefs.mjs; diff them to see that only "## Language" differs), pasted into the | ||
| +builder's prompt or copied into its worktree. Never give a builder a path into this ab/ directory: | ||
| +hidden-tests/ and reference/ sit next to it. | ||
| +--> | ||
| + | ||
| +# Brief: a search query language for the Issues and Pull requests lists | ||
| + | ||
| +You are building one feature in the Rebasely codebase: a GitHub-style search query language for the | ||
| +repository Issues list and the repository Pull requests list. Another engineer is building the same | ||
| +feature from the same brief in parallel; the two results are compared on hidden tests, code review | ||
| +and a UI check. Work alone, inside your own worktree, and do not look at other worktrees. | ||
| + | ||
| +## Setup | ||
| + | ||
| +- The repository is at the worktree path you were given (a git worktree of | ||
| + the Rebasely checkout). Work and commit only there, on your worktree branch. | ||
| +- **Do not push, deploy, or run any `wrangler deploy`, migration against a remote database, or | ||
| + release script.** Commit your work on your worktree branch; the judge reads the branch. | ||
| +- Node resolves dependencies from the main checkout's `node_modules`, so a worktree needs its own | ||
| + links to the workspace packages before anything runs. From the worktree root: | ||
| + | ||
| + ```sh | ||
| + mkdir -p node_modules/@duragit node_modules/@tsus | ||
| + for p in actions:apps/actions cli:packages/cli deadman:apps/deadman shared:packages/shared ssh:apps/ssh web:apps/web; do | ||
| + ln -sfn "../../${p#*:}" "node_modules/@duragit/${p%%:*}" | ||
| + done | ||
| + ln -sfn ../../packages/git node_modules/@duragit/git | ||
| + ln -sfn ../../tools/tsus/packages/runtime node_modules/@tsus/runtime | ||
| + ``` | ||
| + | ||
| + (If your shell tool refuses a command, run the `ln` lines one at a time.) | ||
| + | ||
| +- Read `DESIGN.md` (UI rules: one accent color, Inter 450, no decorative stripes, gradients, emoji, | ||
| + uppercase kickers or em dashes in UI copy) before touching pages. | ||
| +- Checks that must pass before you finish: `npm run typecheck`, `npm run lint`, and the unit tests you | ||
| + add (`node --test test/unit/search/*.test.ts` or wherever you put them). The full `npm run test:unit` | ||
| + is slow; run at least the suites near what you change. | ||
| +- Do not read anything outside your worktree and your own scratchpad directory. In particular do not | ||
| + read anything else under `/private/tmp` (other sessions' scratchpads), other worktrees under | ||
| + `.claude/worktrees`, the main checkout's working tree, or other copies of any tool. | ||
| + Everything you need is in the worktree. | ||
| + | ||
| +## What exists today | ||
| + | ||
| +- Issues live in D1, table `issues` (`apps/web/migrations/0020_issues.sql`): `repo_id, number, | ||
| + org_id, author_id, title, body, state ('open'|'closed'), labels (a JSON array of lowercase | ||
| + strings, default '[]'), comments, created_at, updated_at, closed_at, closed_by` (times are epoch | ||
| + milliseconds). The list query is `listIssueViews` in `apps/web/src/issues/service.ts`; the page is | ||
| + `issuesPage` in `apps/web/src/repo/issues-page.ts`, rendered by `issuesListBody` in | ||
| + `apps/web/src/ui/issues.ts`, which already has a small search form (`q`, titles or `#number`). | ||
| +- Pull requests live in each repository's RepoDO (`apps/web/src/do/repo/pulls.ts`, `prList`), with a | ||
| + D1 copy kept by the DO's outbox: table `pull_index` (`apps/web/migrations/0013_pulls.sql`): | ||
| + `repo_id, number, org_id, author_id, owner_id (the agent's owner for an agent's PR, else NULL), | ||
| + state ('open'|'closed'|'merged'), draft (0|1), title, base_ref, head_ref, created_at, updated_at, | ||
| + merged_at, closed_at`, and `pull_review_requests (repo_id, number, user_id, requested_at)`. The | ||
| + page is `pullsPage` in `apps/web/src/repo/pulls-page.ts`, rendered by `pullsListBody` in | ||
| + `apps/web/src/ui/pulls.ts`. | ||
| +- People have no login handle: a user is a UUID (`users.id`). Agents are users too, with a slug per | ||
| + organization (`agents (user_id, org_id, slug, owner_user_id, ...)`, slug `^[a-z0-9][a-z0-9-]{0,38}$`, | ||
| + `AGENT_SLUG` in `packages/shared/agents.ts`). Labels follow `cleanLabels` in | ||
| + `packages/shared/issues.ts`: lowercase, `^[a-z0-9][a-z0-9 ._-]*$`, at most 32 characters. | ||
| +- There are no assignees, no milestones and no labels on pull requests. | ||
| +- The routes already decide `repo.read` before the list runs (`prContext`), so a repository the | ||
| + viewer cannot read never reaches your code. Your SQL still scopes every row to the repository and | ||
| + its organization (see "SQL contract"), so nothing a person types can widen it. | ||
| + | ||
| +## Language (variant A: plain TypeScript, for the TS builder) | ||
| + | ||
| +Write everything in plain TypeScript (`.ts`), following the repository's conventions: `tsconfig.v2.json` | ||
| +(strict, `erasableSyntaxOnly`, `verbatimModuleSyntax`, imports with explicit `.ts` extensions, so Node | ||
| +runs the files directly with type stripping). Do not use TypeScript-Ultra-Strict (`.tsus` files or | ||
| +`@tsus/runtime`), even though the repository has it wired up for other work. | ||
| + | ||
| +Put the parser, the canonical serializer and the SQL evaluator in `apps/web/src/search/` (as many | ||
| +files as you like). `apps/web/src/search/api.ts` exports the interface below, directly or by | ||
| +re-exporting. Treat a bad query as data, not an exception: `parseQuery` never throws for any string | ||
| +input, and `toSql` never throws for a query `parseQuery` accepted. The route and page code is `.ts` | ||
| +as everywhere else in `apps/web/src`. | ||
| + | ||
| +`npm run typecheck` is your type check; it must pass. | ||
| + | ||
| +## Language (variant B: TypeScript-Ultra-Strict, for the tsus builder) | ||
| + | ||
| +Write the parser, the canonical serializer and the SQL evaluator in TypeScript-Ultra-Strict | ||
| +(`.tsus` files) in `apps/web/src/search/` (as many files as you like). tsus is TypeScript with the | ||
| +escape hatches removed: no `null`, `undefined`, `class`, `var` or `throw`; `Option` and `Result` | ||
| +from `@tsus/runtime`; a typed `match` macro checked for exhaustiveness at compile time; a `?` | ||
| +operator for early return; and 13 further bans (`any`, `as`, `!`, `enum`, `==`, default exports, | ||
| +parameter mutation, floating promises, unused `Result`, untagged error types and more). **Read | ||
| +`tools/tsus/AGENTS.md` first**; it is the language reference (the vendored compiler and runtime are | ||
| +in `tools/tsus`, pinned in `tools/tsus/VERSION`). | ||
| + | ||
| +How it builds here (already wired, see `scripts/tsus-web.mjs`): | ||
| + | ||
| +- Every `apps/web/src/**/x.tsus` compiles to `x.js`, `x.js.map` and `x.d.ts` next to it | ||
| + (gitignored; never commit or edit them). The wrangler build, `npm run typecheck` and | ||
| + `npm run test:unit` run the compile first; run it yourself with `npm run tsus:web`. | ||
| +- One `.tsus` file imports another as `"./y.tsus"`, and the runtime as `"@tsus/runtime"` and | ||
| + `"@tsus/runtime/boundary"`. Plain `.ts` code imports a `.tsus` module as `"./x.js"` (the compiled | ||
| + file; tsc reads `x.d.ts`). Do not put `x.ts` next to `x.tsus`. | ||
| +- Your loop: `node scripts/tsus-web.mjs check --json` (the tsus rules plus a strict type check of the | ||
| + `.tsus` files; a TypeScript error comes back as `TSUS200` with `tsCode`), `node scripts/tsus-web.mjs | ||
| + fix` (and `fix --unsafe`) for machine fixes, and `node tools/tsus/packages/cli/bin/tsus.js explain | ||
| + <CODE>` for any rule. Use these instead of `tsus check apps/web` or `tsus fix apps/web`: the stock | ||
| + command type-checks every `.ts` file under `src/` without the Worker types and drowns your | ||
| + diagnostics in hundreds of false ones. `npm run typecheck` must pass too. | ||
| + | ||
| +`apps/web/src/search/api.ts` is a thin plain `.ts` adapter over your `.tsus` modules: it converts | ||
| +`Result` and `Option` into the plain objects of the interface below (`{ ok: true, query }` or | ||
| +`{ ok: false, errors }`, `viewerId: string | null` into whatever you use) and nothing else; the | ||
| +logic lives in `.tsus`. The routes and pages stay `.ts` and call the module through `api.ts` (or | ||
| +through the compiled `.js` modules directly). Treat a bad query as data: no exceptions, and the | ||
| +adapter never throws for any string input. | ||
| + | ||
| +## The module interface (what the hidden tests import) | ||
| + | ||
| +Create `apps/web/src/search/api.ts`. The hidden tests import exactly this file, under Node 24 with | ||
| +type stripping (`node --test`, no bundler, no wrangler), after running `npm run tsus:web`. So | ||
| +`api.ts` and everything it imports must load in plain Node: relative imports and `@duragit/shared/*` | ||
| +are fine; no `cloudflare:*` modules, no `env` bindings, no D1 at import time. | ||
| + | ||
| +```ts | ||
| +export type Target = "issues" | "pulls"; | ||
| +export type SqlValue = string | number | null; | ||
| + | ||
| +export type QueryErrorCode = | ||
| + | "too_long" | "too_many_terms" | "unterminated_quote" | "unexpected_quote" | ||
| + | "unknown_qualifier" | "unsupported_qualifier" | "empty_value" | "negation_not_allowed" | ||
| + | "bad_value" | "bad_date" | "conflict"; | ||
| + | ||
| +// start/end: UTF-16 code unit offsets into the input, end exclusive. | ||
| +export type QueryError = { code: QueryErrorCode; message: string; start: number; end: number }; | ||
| + | ||
| +// The parsed query. Its shape is yours (an AST); the tests treat it as opaque and only pass it back | ||
| +// to serializeQuery and toSql. | ||
| +export type SearchQuery = /* your AST type */; | ||
| + | ||
| +export function parseQuery(input: string, target: Target): | ||
| + { ok: true; query: SearchQuery } | { ok: false; errors: QueryError[] }; | ||
| + | ||
| +export function serializeQuery(query: SearchQuery): string; | ||
| + | ||
| +export type SqlCtx = { | ||
| + repoId: string; // the repository being listed | ||
| + orgId: string; // its organization | ||
| + viewerId: string | null; // the signed-in user's id, null when signed out | ||
| + now: number; // epoch milliseconds, for relative dates | ||
| +}; | ||
| + | ||
| +// A boolean SQL expression and an ORDER BY list for the target's table, with every value bound. | ||
| +export function toSql(query: SearchQuery, ctx: SqlCtx): { where: string; params: SqlValue[]; orderBy: string }; | ||
| +``` | ||
| + | ||
| +These are plain JavaScript values at the boundary (`ok` booleans, arrays, strings, numbers, `null` | ||
| +for a signed-out viewer) whatever you use inside. `message` is a short sentence for people (shown | ||
| +in the UI), never empty. | ||
| + | ||
| +## The query language | ||
| + | ||
| +### Terms | ||
| + | ||
| +An input is a sequence of terms separated by runs of whitespace (any character JavaScript's `\s` | ||
| +matches, so a no-break space or an ideographic space separates terms too). Every term is ANDed with | ||
| +the others. There is no OR, no parentheses, no NOT keyword: `OR` is just a word. | ||
| + | ||
| +Reading one term, starting at its first non-whitespace character: | ||
| + | ||
| +1. **Negation.** A `-` immediately followed by a non-whitespace character negates the term and is | ||
| + not part of it. A `-` alone (followed by whitespace or the end) is the free-text word `-`. | ||
| +2. **Quoted value.** A `"` starts a quoted value. Inside, `\"` is a `"` and `\\` is a `\`; any other | ||
| + backslash is kept as it is. The next unescaped `"` ends it. No closing quote: error | ||
| + `unterminated_quote` spanning from the term's start to the end of the input, and parsing stops | ||
| + there. After the closing quote the term must end (whitespace or end of input), otherwise | ||
| + `unexpected_quote`. | ||
| +3. **Bare text** runs to the next whitespace. If it reaches a `"`, that is only allowed when the bare | ||
| + text so far is exactly a qualifier key and its colon (`label:"needs review"`): the quoted value is | ||
| + the qualifier's value. Any other `"` inside bare text is `unexpected_quote`. | ||
| + An `unexpected_quote` error spans from the term's start to the next whitespace (or the end), and | ||
| + parsing goes on after it. | ||
| +4. **Kinds.** A bare term matching `^[A-Za-z]+(-[A-Za-z]+)*:` is a qualifier: the key (lowercased) is | ||
| + the part before the first colon, the value is everything after it (or the quoted value). A bare | ||
| + term matching `^#[1-9][0-9]{0,8}$` is a number. Anything else, and every quoted term, is free text. | ||
| + So `12:30`, `a1:b` and `:x` are free text, `http://x` is the qualifier `http` (unknown), and | ||
| + `"label:bug"` is the phrase `label:bug`. | ||
| + | ||
| +### Qualifiers and values | ||
| + | ||
| +| Qualifier | Targets | Values | Negation | | ||
| +|---|---|---|---| | ||
| +| `is:` | both | `open`, `closed`; for pulls also `merged`, `draft` | allowed | | ||
| +| `label:` | issues | a label name | allowed | | ||
| +| `no:` | issues | `label` | not allowed | | ||
| +| `author:` | both | a user reference | allowed | | ||
| +| `review-requested:` | pulls | a user reference | allowed | | ||
| +| `sort:` | both | `created-desc`, `created-asc`, `updated-desc`, `updated-asc`; for issues also `comments-desc`, `comments-asc` | not allowed | | ||
| +| `created:` `updated:` | both | a date filter | not allowed | | ||
| +| `#123` | both | the number | not allowed | | ||
| +| free text, "phrases" | both | any non-empty text | allowed | | ||
| + | ||
| +Values are case-insensitive: `is:`, `no:`, `sort:`, `author:` and `review-requested:` values are | ||
| +lowercased. A **label** value is trimmed, lowercased and has each internal whitespace run collapsed | ||
| +to one space; the result must match `^[a-z0-9][a-z0-9 ._-]*$` and be at most 32 characters. A | ||
| +**user reference** is `@me`, a UUID (`^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$` | ||
| +after lowercasing; checked before the slug rule) or an agent slug (`^[a-z0-9][a-z0-9-]{0,38}$`). | ||
| +Anything else after `@` is a `bad_value`. | ||
| + | ||
| +### Date filters | ||
| + | ||
| +All dates are UTC calendar days, `YYYY-MM-DD` exactly (zero-padded, a real date, year 1970 to 9999). | ||
| +Let `start(D)` be D at 00:00:00.000Z in epoch ms and `DAY` = 86,400,000. | ||
| + | ||
| +| Value | Matches a timestamp `t` when | | ||
| +|---|---| | ||
| +| `D` | `start(D) <= t < start(D) + DAY` | | ||
| +| `>D` | `t >= start(D) + DAY` | | ||
| +| `>=D` | `t >= start(D)` | | ||
| +| `<D` | `t < start(D)` | | ||
| +| `<=D` | `t < start(D) + DAY` | | ||
| +| `A..B` | `start(A) <= t < start(B) + DAY`; A after B is `bad_date` | | ||
| +| `A..*` | `t >= start(A)` | | ||
| +| `*..B` | `t < start(B) + DAY` | | ||
| + | ||
| +Relative values compare **age** (`now - t`) with a duration `N` units, where N is 1 to 9999 (leading | ||
| +zeros allowed, dropped in the canonical form) and the unit is `h` (3,600,000 ms), `d` (86,400,000) | ||
| +or `w` (604,800,000), case-insensitive. With `T = now - N * unit`: | ||
| + | ||
| +| Value | Reads as | Matches when | | ||
| +|---|---|---| | ||
| +| `<7d` | younger than 7 days | `t > T` | | ||
| +| `<=7d` | at most 7 days old | `t >= T` | | ||
| +| `>7d` | older than 7 days | `t < T` | | ||
| +| `>=7d` | at least 7 days old | `t <= T` | | ||
| + | ||
| +A relative value needs an operator (`updated:7d` is `bad_date`); ranges take absolute dates or `*` | ||
| +only (`*..*` is `bad_date`); `=` is not an operator. Anything else is `bad_date`. | ||
| + | ||
| +### Errors | ||
| + | ||
| +Every invalid term produces exactly one error spanning the whole term, from its first character | ||
| +(including a leading `-`) to its last (including quotes); errors are listed in input order. When | ||
| +any error exists, `parseQuery` returns `ok: false` with all of them. For a qualifier term the checks | ||
| +run in this order and the first failure is the error: | ||
| + | ||
| +1. `unknown_qualifier`: the key is not in the table (for example `assignee:`, `repo:`, `milestone:`). | ||
| +2. `unsupported_qualifier`: the key exists but not for this target (`label:` and `no:` on pulls, | ||
| + `review-requested:` on issues). | ||
| +3. `empty_value`: nothing after the colon, or `""`. | ||
| +4. `negation_not_allowed`: a `-` on `no:`, `sort:`, `created:` or `updated:`. | ||
| +5. `bad_value` (or `bad_date` for `created:` and `updated:`): the value is not valid for the key and | ||
| + target, for example `is:merged` on issues or `sort:comments-desc` on pulls. | ||
| + | ||
| +Other errors: a negated number (`-#5`) is `negation_not_allowed`; a free-text quoted value with no | ||
| +non-whitespace character (`""`, `" "`) is `empty_value`; a second `sort:` term with a different | ||
| +value is `conflict` (on the second term). | ||
| + | ||
| +Limits, checked before anything else and reported alone (a single error, nothing else): | ||
| + | ||
| +- `too_long`: the input is longer than 1024 UTF-16 code units; the error spans the whole input | ||
| + (`start` 0, `end` the input's length). | ||
| +- `too_many_terms`: more than 32 terms, counted as written (before duplicates are removed, invalid | ||
| + terms included); the error spans from the 33rd term's start to the end of the input. | ||
| + | ||
| +An empty or whitespace-only input is a valid, empty query. | ||
| + | ||
| +### Canonical form (`serializeQuery`) | ||
| + | ||
| +The canonical form is what goes in the page URL (`?q=`), so the URL round-trips: for any valid | ||
| +input `x`, with `c = serializeQuery(parseQuery(x).query)`, `parseQuery(c)` succeeds, | ||
| +`serializeQuery` of it is `c` again, and `toSql` gives the same `where`, `params` and `orderBy` for | ||
| +both. Rules: | ||
| + | ||
| +- Terms keep their input order, joined by single spaces. A term whose canonical text equals an | ||
| + earlier term's is dropped. | ||
| +- Keys are lowercase. Values are normalized as above (`IS:Open` is `is:open`, `label:" Needs | ||
| + Review "` is `label:"needs review"`, `author:@ME` is `author:@me`, `updated:<007D` is | ||
| + `updated:<7d`). Dates are written as given (`created:>=2026-10-01`, `created:2026-10-01..*`). | ||
| +- Negated terms start with `-`. | ||
| +- A qualifier value is written bare when it matches `^[^\s"]+$`, otherwise quoted. | ||
| +- A free-text value is written bare when it matches `^[^\s"]+$`, does not start with `-`, does not | ||
| + match the qualifier pattern `^[A-Za-z]+(-[A-Za-z]+)*:` and does not match the number pattern | ||
| + `^#[1-9][0-9]{0,8}$`; otherwise quoted. So `"bug"` is written `bug`, `"label:x"` stays `"label:x"`, | ||
| + and a lone `-` is written `"-"`. | ||
| +- Quoting writes `"`, the value with each `\` as `\\` and each `"` as `\"`, then `"`. Phrases keep | ||
| + their inner whitespace exactly. | ||
| +- The empty query is the empty string. | ||
| + | ||
| +### Semantics | ||
| + | ||
| +| Term | Issues (`issues`) | Pulls (`pull_index`) | | ||
| +|---|---|---| | ||
| +| `is:open` | `state = 'open'` | `state = 'open'` (drafts included) | | ||
| +| `is:closed` | `state = 'closed'` | `state` is `'closed'` or `'merged'` (as on GitHub) | | ||
| +| `is:merged` | n/a | `state = 'merged'` | | ||
| +| `is:draft` | n/a | `draft = 1`, in any state | | ||
| +| `label:x` | `x` is in the `labels` JSON array | n/a | | ||
| +| `no:label` | `labels` is an empty array | n/a | | ||
| +| `author:u` | `author_id` is u | `author_id` is u, or `owner_id` is u (an agent's PR shows under its owner, as the list does today) | | ||
| +| `review-requested:u` | n/a | a `pull_review_requests` row for this PR with `user_id` u | | ||
| +| `#n` | `number = n` | `number = n` | | ||
| +| free text `w` | `w` occurs in `title` or in `body` | `w` occurs in `title` | | ||
| +| `created:` `updated:` | `created_at` / `updated_at` per the date tables | same | | ||
| + | ||
| +- A negated term matches exactly the rows (in scope) its positive form does not match. Watch NULLs: | ||
| + `-author:u` on pulls must match a PR whose `owner_id` is NULL and whose author is someone else. | ||
| +- User references: `@me` is `ctx.viewerId`; with no viewer, `author:@me` and | ||
| + `review-requested:@me` match nothing and their negations match everything. A UUID is that user id. | ||
| + A slug is the agent with that slug **in `ctx.orgId`** (`agents.org_id`); a slug of another | ||
| + organization's agent, or of no agent, matches nothing. | ||
| +- Free text and phrases match as substrings with SQLite `LIKE` semantics (ASCII case-insensitive, | ||
| + other characters exactly), with `%`, `_` and `\` in the text matched literally (escape them; use | ||
| + `ESCAPE '\'`). A phrase is one substring, inner spaces included; each bare word is its own term. | ||
| +- Two `is:` terms are ANDed like everything else: `is:open is:closed` matches nothing. No term is | ||
| + implied: the empty query matches every row in scope, in every state. (The page supplies `is:open` | ||
| + as its default; the evaluator does not.) | ||
| +- `sort:` picks the order; the default is `created-desc`. `orderBy` is `<column> <ASC|DESC>, number | ||
| + <ASC|DESC>` with the tie-break in the same direction: `created_at`, `updated_at` or `comments`. | ||
| + | ||
| +### SQL contract (`toSql`) | ||
| + | ||
| +- The result is used as `SELECT ... FROM <table> WHERE ${where} AND <more conditions> ORDER BY | ||
| + ${orderBy} LIMIT ?`, where `<table>` is `issues` for an issues query and `pull_index` for a pulls | ||
| + query, without an alias. `where` must keep its meaning when more conditions are ANDed after it | ||
| + (parenthesize any top-level OR). It may reference the table by name and use subqueries | ||
| + (`json_each`, `pull_review_requests`, `agents`). | ||
| +- **Every value is bound.** Nothing from the input or from `ctx` (ids included) is ever written into | ||
| + the SQL text; `params` carries them, in placeholder order. Use `?` placeholders (`?NNN` numbered | ||
| + ones are allowed if you use them for every placeholder). The SQL text may contain only constant | ||
| + literals you wrote yourself (`'open'`, `'\'` in `ESCAPE '\'`, `0`, `1`). | ||
| +- **Scope cannot be dropped.** `where` always requires `repo_id = ctx.repoId` and `org_id = | ||
| + ctx.orgId` at its top level, ANDed with the query's conditions, for every query including the | ||
| + empty one. No term can widen the scope. | ||
| +- `orderBy` is built only from the fixed column names and directions above. | ||
| +- `toSql` itself never fails for a query `parseQuery` accepted for the same target. | ||
| + | ||
| +## The pages | ||
| + | ||
| +- **Issues** (`/:org/:repo/issues`) and **Pull requests** (`/:org/:repo/pulls`): the list is driven by | ||
| + `?q=` in the query language. With no `q`, the page uses `is:open` (and still honors the old | ||
| + `state=` and `label=` links by turning them into terms). Run the search through `parseQuery` and | ||
| + `toSql` against D1 for both lists: issues from `issues`, pull requests from `pull_index`. (The | ||
| + RepoDO stays the source of truth for a PR's own page; `pull_index` may lag a few seconds, which is | ||
| + fine for a list. You may hydrate rows from the RepoDO if the list needs fields `pull_index` lacks, | ||
| + or render what `pull_index` has.) Keep 25 per page with a way to the next page that keeps `q`. | ||
| +- The search box shows the canonical form of the current query. Links the page makes (state tabs, | ||
| + next page, a label chip, "Clear") use canonical `q` values. The Open / Closed (and Merged) tabs | ||
| + replace the query's `is:` state term. | ||
| +- **Errors inline:** a query with errors renders the page (status 200) with the input kept as typed, | ||
| + each error's message under the box, and the offending spans marked in a way that does not rely on | ||
| + color alone. No results are listed for an invalid query. | ||
| +- **Syntax help:** a small popover or disclosure next to the box listing the qualifiers that apply to | ||
| + that list, with one example each, usable by keyboard and screen readers and without JavaScript | ||
| + (for example a `<details>` element). | ||
| +- Follow `DESIGN.md` and the existing list styles. No new JavaScript framework; a little progressive | ||
| + enhancement is fine. | ||
| + | ||
| +## Out of scope | ||
| + | ||
| +- `/inbox`, the `/api/v1` list endpoints (keep their current `q` behavior), the MCP tools, saved | ||
| + searches, autocomplete, assignees and milestones, labels on pull requests. | ||
| +- Migrations: none are needed. Do not add one. | ||
| + | ||
| +## When you are done | ||
| + | ||
| +Commit on your worktree branch with a message in the repository's style, and reply with: what you | ||
| +built, the files, how you checked it (commands and results), and anything you know is missing or | ||
| +wrong. | ||
Addedbrief/RUBRIC.md+110−0
| @@ -0,0 +1,110 @@ | ||
| +# Rubric: TypeScript vs TypeScript-Ultra-Strict, search query language | ||
| + | ||
| +You are the judge. Two builders implemented the same brief (`BRIEF.md` in this directory; builder A | ||
| +got `BRIEF-ts.md`, builder B got `BRIEF-tsus.md`, identical except the "Language" section). Score | ||
| +each build on its own against this rubric, then compare. Do not reward or penalize the language | ||
| +itself: judge what each builder produced under its constraints. Give every score with the evidence | ||
| +(file and line, test name, command output). | ||
| + | ||
| +You are given, per builder: the worktree path and branch, and from the harness the token count and | ||
| +tool-call count. Run everything yourself; do not trust a builder's own report. | ||
| + | ||
| +## 0. Preparation (no points) | ||
| + | ||
| +1. In each worktree: `git log --oneline main..HEAD` and `git diff main...HEAD --stat`. Note | ||
| + anything committed that should not be (generated `.js` or `.d.ts` under `apps/web/src`, a | ||
| + migration, edits to `scripts/tsus-web.mjs`, `tools/tsus`, the wrangler config or CI, edits to | ||
| + unrelated features). Changing the build wiring or the vendored compiler is a red flag for B and | ||
| + a scope violation for either. | ||
| +2. `npm run typecheck` and `npm run lint` in each worktree (record pass or fail and the output). | ||
| +3. Run the builder's own tests (whatever they added under `test/unit/`), and record the count. | ||
| + | ||
| +## 1. Hidden tests: 40 points | ||
| + | ||
| +Run `hidden-tests/run.sh <worktree>` (it links the worktree's `node_modules`, runs | ||
| +`scripts/tsus-web.mjs build`, then all `hidden-tests/*.test.mjs`). The last line is | ||
| +`hidden tests: pass N / total M`. Score = 40 x N / M, rounded to one decimal. | ||
| + | ||
| +- If `run.sh` exits 2 (missing `api.ts`, or the `.tsus` build fails), the score is 0. | ||
| +- Do not edit the tests or the build to make them run. If a test fails because of an ambiguity in | ||
| + the brief (not a bug), note it, but keep the score mechanical; the same tests run for both. | ||
| +- 118 tests: parsing (42), canonical form and round trips (9), semantics on SQLite (30), dates (18), | ||
| + SQL safety (9, three of them covering 15 injection payloads), scope and generated inputs (10), plus the structural | ||
| + checks every SQL-running test makes (placeholders equal params, fixed `orderBy`, every returned | ||
| + row in ctx's repository and organization). | ||
| +- `reference/` holds a quick reference implementation that passes all 118; it exists only to show the | ||
| + tests are satisfiable. Do not use it to grade code style. | ||
| + | ||
| +## 2. Security: automatic severe penalty (up to minus 40, applied after the total) | ||
| + | ||
| +Look for these yourself in the code (the evaluator and the page glue), not only in test failures. | ||
| +Each confirmed finding: | ||
| + | ||
| +| Finding | Penalty | | ||
| +|---|---| | ||
| +| A value from the query text or from ctx is written into SQL text (string concatenation or a template literal with a value), anywhere: evaluator, page glue, count queries, pagination | -40, and the build cannot win | | ||
| +| A query (any input) can return rows outside the repository or organization, or the page runs a list query without the scope (for example the counts or "next page" query) | -40, and the build cannot win | | ||
| +| The page lists rows without the route's existing `repo.read` decision (`prContext`/`pageOf`), or the search reaches private data another way (for example resolving an agent slug or user across organizations) | -30 | | ||
| +| An unescaped `LIKE` pattern (`%`, `_`, `\` from the input act as wildcards) | -5 | | ||
| +| Error messages or the page echo the raw query into HTML without escaping (XSS) | -20 | | ||
| +| `parseQuery`, `toSql` or the page throws (a 500) on some input | -10 per distinct crash | | ||
| + | ||
| +Hidden-test failures in the SQL safety or scope groups count here too: any failure in | ||
| +`sql-safety.test.mjs` or `permission.test.mjs` is investigated, and if it is a real injection or | ||
| +scope bug it takes the penalty above in addition to the lost test points. | ||
| + | ||
| +## 3. Code review: 30 points | ||
| + | ||
| +Read the diff as a senior reviewer on this codebase would. Score each line item and justify it. | ||
| + | ||
| +| Item | Points | What good looks like | | ||
| +|---|---|---| | ||
| +| Correctness beyond the tests | 8 | Grammar and semantics match the brief where the tests do not reach (error positions for odd input, `-author:` NULL handling, `is:closed` on pulls including merged, slug resolution scoped to the org, the page's default `is:open`, legacy `state=`/`label=` links). Spot-check with your own inputs. | | ||
| +| Error handling | 6 | Bad input is data end to end: typed error codes, spans, one error per bad term, no exceptions in the parser or evaluator, D1 failures on the page handled like the rest of the codebase. | | ||
| +| Readability and structure | 6 | A reader can follow tokenizer, parser, serializer and evaluator; clear types (a real AST, a closed error union); no dead code; names and comments in the repository's style (see neighbouring files). | | ||
| +| Edge cases | 5 | Limits, unicode offsets, whitespace kinds, empty input, quoting and escaping in both directions, pagination with a sort, counts for the tabs. | | ||
| +| Tests the builder wrote | 3 | Useful unit tests of their own, covering what they changed. | | ||
| +| Fit with the codebase | 2 | Follows existing patterns (D1 access, `Out`/page helpers, `DESIGN.md`), touches only what the feature needs, no new dependencies. | | ||
| + | ||
| +For builder B also check the tsus constraint was honored: parser, serializer and evaluator in | ||
| +`.tsus`, `api.ts` a thin adapter with no logic of its own. Logic moved into `.ts` to dodge the | ||
| +checker loses up to 6 points from "Readability and structure" and "Fit". For builder A check that no | ||
| +`.tsus` was used. | ||
| + | ||
| +## 4. UI check: 20 points | ||
| + | ||
| +Start the stack in each worktree (`npm run dev:v2`, port 8790; seed a repository with a few issues, | ||
| +labels and pull requests in several states, signed in and signed out; the repository's test helpers | ||
| +and `test/helpers/seed.ts` show how). Check desktop and a 375 px phone width, light and dark. | ||
| + | ||
| +| Item | Points | | ||
| +|---|---| | ||
| +| The search box on both lists runs the language; results are right for a handful of queries you try (state, label, author, text, date, sort) | 6 | | ||
| +| Errors are shown inline: message per error under the box, offending span marked without relying on color alone, input kept as typed, status 200, no results listed | 5 | | ||
| +| Syntax help: present on both lists, lists only the qualifiers of that list, works by keyboard and without JavaScript | 3 | | ||
| +| URL round trip: `?q=` holds the canonical form; reload, back, the state tabs, a label link and next page all keep the query | 3 | | ||
| +| Design: follows `DESIGN.md` and the existing list styles (one accent, Inter 450, no stripes, gradients, emoji, uppercase kickers, em dashes in copy); fits on a phone with no horizontal scroll | 3 | | ||
| + | ||
| +## 5. Cost: 10 points | ||
| + | ||
| +The harness supplies, per builder, total tokens (input plus output, cache reads counted as the | ||
| +harness reports them) and tool calls. Score relative to the cheaper build: | ||
| + | ||
| +- tokens: 6 x (fewer tokens / this build's tokens) | ||
| +- tool calls: 4 x (fewer calls / this build's calls) | ||
| + | ||
| +So the cheaper build gets the full 10 and the other gets proportionally less. Report the raw numbers | ||
| +too, and the cost per hidden test passed (tokens / N). | ||
| + | ||
| +## Total and verdict | ||
| + | ||
| +Total = hidden tests (40) + code review (30) + UI (20) + cost (10) - security penalties, out of 100. | ||
| + | ||
| +Report a table with every line item for both builds, the totals, and a verdict: | ||
| + | ||
| +- The winner is the higher total, unless it carries a "cannot win" finding. | ||
| +- If the totals are within 5 points, say it is a tie on quality and name the cheaper build. | ||
| +- Then say plainly which build to ship (or that neither is shippable and why), and list what must be | ||
| + fixed before shipping it. | ||
| +- Note anything about the comparison itself that was unfair or noisy (a hidden test the brief left | ||
| + ambiguous, an environment problem one builder hit and the other did not, a tsus toolchain bug). | ||
Addedbuild-a-ts/apps/web/src/search/api.ts+15−0
| @@ -0,0 +1,15 @@ | ||
| +// The search query language of the repository Issues and Pull requests lists: a GitHub-style query | ||
| +// (is:open label:bug author:@me "a phrase" -word sort:updated-desc created:>=2026-10-01) parsed to a | ||
| +// SearchQuery, written back in its canonical form (what ?q= carries), and evaluated as SQL over the | ||
| +// target's D1 table (issues; pull_index for pull requests). | ||
| +// | ||
| +// This module loads in plain Node (no bindings, no cloudflare: modules): the pages call it, and so do | ||
| +// the unit tests (test/unit/search). A bad query is data, not an exception: parseQuery never throws, | ||
| +// and toSql never throws for a query parseQuery accepted. | ||
| + | ||
| +export type { | ||
| + QueryError, QueryErrorCode, SearchQuery, SqlCtx, SqlResult, SqlValue, Target, | ||
| +} from "./ast.ts"; | ||
| +export { parseQuery } from "./parse.ts"; | ||
| +export { serializeQuery } from "./serialize.ts"; | ||
| +export { toSql } from "./sql.ts"; | ||
Addedbuild-a-ts/apps/web/src/search/ast.ts+57−0
| @@ -0,0 +1,57 @@ | ||
| +// The search query language's types (search/api.ts is the module's door): what a query parses to, | ||
| +// what an invalid one reports, and what toSql binds. Plain data, so a parsed query can be passed back | ||
| +// to serializeQuery and toSql, compared, and edited by the pages (search/edit.ts). | ||
| + | ||
| +export type Target = "issues" | "pulls"; | ||
| +export type SqlValue = string | number | null; | ||
| + | ||
| +export type QueryErrorCode = | ||
| + | "too_long" | "too_many_terms" | "unterminated_quote" | "unexpected_quote" | ||
| + | "unknown_qualifier" | "unsupported_qualifier" | "empty_value" | "negation_not_allowed" | ||
| + | "bad_value" | "bad_date" | "conflict"; | ||
| + | ||
| +// start/end: UTF-16 code unit offsets into the input, end exclusive. | ||
| +export type QueryError = { code: QueryErrorCode; message: string; start: number; end: number }; | ||
| + | ||
| +export type StateValue = "open" | "closed" | "merged" | "draft"; | ||
| +export type SortValue = "created-desc" | "created-asc" | "updated-desc" | "updated-asc" | "comments-desc" | "comments-asc"; | ||
| + | ||
| +// A person named in a query: the viewer, a user id, or an agent of the repository's organization. | ||
| +export type UserRef = { kind: "me" } | { kind: "id"; id: string } | { kind: "slug"; slug: string }; | ||
| + | ||
| +// A calendar day: as written (YYYY-MM-DD) and its start in epoch ms (00:00:00.000Z). | ||
| +export type Day = { text: string; start: number }; | ||
| +export type DateOp = "<" | "<=" | ">" | ">="; | ||
| +export type RelUnit = "h" | "d" | "w"; | ||
| + | ||
| +export type DateFilter = | ||
| + | { kind: "day"; day: Day } | ||
| + | { kind: "cmp"; op: DateOp; day: Day } | ||
| + | { kind: "range"; from: Day | null; to: Day | null } | ||
| + | { kind: "age"; op: DateOp; n: number; unit: RelUnit }; | ||
| + | ||
| +export type Term = | ||
| + | { kind: "is"; neg: boolean; value: StateValue } | ||
| + | { kind: "label"; neg: boolean; value: string } | ||
| + | { kind: "no"; value: "label" } | ||
| + | { kind: "author"; neg: boolean; user: UserRef } | ||
| + | { kind: "review-requested"; neg: boolean; user: UserRef } | ||
| + | { kind: "sort"; value: SortValue } | ||
| + | { kind: "date"; field: "created" | "updated"; filter: DateFilter } | ||
| + | { kind: "number"; value: number } | ||
| + | { kind: "text"; neg: boolean; value: string }; | ||
| + | ||
| +// A parsed, valid query: its target and its terms in input order, duplicates (same canonical text) | ||
| +// removed. At most one sort term. | ||
| +export type SearchQuery = { readonly target: Target; readonly terms: readonly Term[] }; | ||
| + | ||
| +export type ParseResult = { ok: true; query: SearchQuery } | { ok: false; errors: QueryError[] }; | ||
| + | ||
| +export type SqlCtx = { | ||
| + repoId: string; | ||
| + orgId: string; | ||
| + viewerId: string | null; | ||
| + now: number; | ||
| +}; | ||
| + | ||
| +export type SqlResult = { where: string; params: SqlValue[]; orderBy: string }; | ||
Addedbuild-a-ts/apps/web/src/search/edit.ts+49−0
| @@ -0,0 +1,49 @@ | ||
| +// What the list pages do with a parsed query (repo/issues-page.ts, repo/pulls-page.ts): which state | ||
| +// tab it is on, the same query on another tab, the query without its state for the tab counts, and | ||
| +// the query with one more term (a label chip). Everything stays a SearchQuery, so every link is | ||
| +// written by serializeQuery and is canonical. | ||
| + | ||
| +import type { SearchQuery, StateValue, Target, Term } from "./ast.ts"; | ||
| +import { MAX_TERMS } from "./parse.ts"; | ||
| +import { termText } from "./serialize.ts"; | ||
| + | ||
| +export type StateTab = "open" | "closed" | "merged" | "all"; | ||
| + | ||
| +const isStateTerm = (t: Term) => t.kind === "is" && t.value !== "draft"; | ||
| + | ||
| +export function emptyQuery(target: Target): SearchQuery { | ||
| + return { target, terms: [] }; | ||
| +} | ||
| + | ||
| +// The tab the query is on: no open/closed/merged term is All, one positive term is its tab, anything | ||
| +// else (two of them, a negated one) is none of the tabs. | ||
| +export function stateTab(q: SearchQuery): StateTab | null { | ||
| + const s = q.terms.filter(isStateTerm); | ||
| + if (!s.length) return "all"; | ||
| + const [t] = s; | ||
| + return s.length === 1 && t.kind === "is" && !t.neg ? (t.value as StateTab) : null; | ||
| +} | ||
| + | ||
| +// The query on another tab: its open/closed/merged terms replaced by the tab's (first), or removed | ||
| +// for All. is:draft and everything else stay where they are. | ||
| +export function withState(q: SearchQuery, tab: StateTab): SearchQuery { | ||
| + const rest = q.terms.filter((t) => !isStateTerm(t)); | ||
| + const terms: Term[] = tab === "all" ? rest : [{ kind: "is", neg: false, value: tab as StateValue }, ...rest]; | ||
| + return { target: q.target, terms }; | ||
| +} | ||
| + | ||
| +// The query with one more term at the end (unchanged when it already has it), or null when that would | ||
| +// pass the term limit. | ||
| +export function withTerm(q: SearchQuery, term: Term): SearchQuery | null { | ||
| + const text = termText(term); | ||
| + if (q.terms.some((t) => termText(t) === text)) return q; | ||
| + if (term.kind === "sort") return { target: q.target, terms: [...q.terms.filter((t) => t.kind !== "sort"), term] }; | ||
| + if (q.terms.length >= MAX_TERMS) return null; | ||
| + return { target: q.target, terms: [...q.terms, term] }; | ||
| +} | ||
| + | ||
| +// Whether the query narrows the list beyond a state tab and an order (the empty state then offers to | ||
| +// clear it). | ||
| +export function hasFilters(q: SearchQuery): boolean { | ||
| + return q.terms.some((t) => !isStateTerm(t) && t.kind !== "sort"); | ||
| +} | ||
Addedbuild-a-ts/apps/web/src/search/parse.ts+309−0
| @@ -0,0 +1,309 @@ | ||
| +// The search query parser (search/api.ts): an input string to a SearchQuery, or every error in it. | ||
| +// Never throws. Two passes: | ||
| +// 1. lex: split the input into terms as written (quotes, a leading -, a qualifier's quoted value), | ||
| +// so the term limit counts what was typed and quote errors are found where they are; | ||
| +// 2. read each term: a qualifier (key:value), a number (#12) or free text, checked against the | ||
| +// target's table; then duplicates (the same canonical text) are dropped and a second sort: with | ||
| +// another value is a conflict. | ||
| + | ||
| +import type { | ||
| + DateFilter, DateOp, Day, ParseResult, QueryError, QueryErrorCode, RelUnit, SearchQuery, SortValue, StateValue, Target, Term, UserRef, | ||
| +} from "./ast.ts"; | ||
| +import { NUMBER_RE, QUALIFIER_RE, termText } from "./serialize.ts"; | ||
| + | ||
| +export const MAX_INPUT = 1024; | ||
| +export const MAX_TERMS = 32; | ||
| +const LABEL_MAX = 32; | ||
| + | ||
| +const WS = /\s/; | ||
| +const KEY_COLON = /^[A-Za-z]+(-[A-Za-z]+)*:$/; | ||
| +const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/; | ||
| +const SLUG = /^[a-z0-9][a-z0-9-]{0,38}$/; | ||
| +const LABEL = /^[a-z0-9][a-z0-9 ._-]*$/; | ||
| + | ||
| +type Key = "is" | "label" | "no" | "author" | "review-requested" | "sort" | "created" | "updated"; | ||
| + | ||
| +// Which qualifiers each list has. | ||
| +const KEYS: Record<Key, readonly Target[]> = { | ||
| + is: ["issues", "pulls"], | ||
| + label: ["issues"], | ||
| + no: ["issues"], | ||
| + author: ["issues", "pulls"], | ||
| + "review-requested": ["pulls"], | ||
| + sort: ["issues", "pulls"], | ||
| + created: ["issues", "pulls"], | ||
| + updated: ["issues", "pulls"], | ||
| +}; | ||
| + | ||
| +const NO_NEGATION: ReadonlySet<Key> = new Set(["no", "sort", "created", "updated"]); | ||
| + | ||
| +export const STATES: Record<Target, readonly StateValue[]> = { issues: ["open", "closed"], pulls: ["open", "closed", "merged", "draft"] }; | ||
| +export const SORTS: Record<Target, readonly SortValue[]> = { | ||
| + issues: ["created-desc", "created-asc", "updated-desc", "updated-asc", "comments-desc", "comments-asc"], | ||
| + pulls: ["created-desc", "created-asc", "updated-desc", "updated-asc"], | ||
| +}; | ||
| + | ||
| +const LIST: Record<Target, string> = { issues: "issues", pulls: "pull requests" }; | ||
| +const EXAMPLE: Record<Key, string> = { | ||
| + is: "is:open", label: "label:bug", no: "no:label", author: "author:@me", "review-requested": "review-requested:@me", sort: "sort:updated-desc", | ||
| + created: "created:>=2026-10-01", updated: "updated:<7d", | ||
| +}; | ||
| + | ||
| +// --- lexing | ||
| + | ||
| +type Lexed = | ||
| + | { kind: "error"; start: number; end: number; code: "unterminated_quote" | "unexpected_quote" } | ||
| + // free text in quotes | ||
| + | { kind: "quoted"; start: number; end: number; neg: boolean; value: string } | ||
| + // a bare term (after the -) | ||
| + | { kind: "bare"; start: number; end: number; neg: boolean; text: string } | ||
| + // key:"value" | ||
| + | { kind: "qualifier"; start: number; end: number; neg: boolean; key: string; value: string }; | ||
| + | ||
| +const isWs = (c: string) => WS.test(c); | ||
| + | ||
| +function nextWs(s: string, i: number): number { | ||
| + while (i < s.length && !isWs(s[i])) i++; | ||
| + return i; | ||
| +} | ||
| + | ||
| +// The quoted value opening at s[i] ('"'): its text and the index after the closing quote, or null when | ||
| +// it never closes. \" is a quote and \\ a backslash; any other backslash stays. | ||
| +function readQuoted(s: string, i: number): { value: string; end: number } | null { | ||
| + let out = ""; | ||
| + let j = i + 1; | ||
| + while (j < s.length) { | ||
| + const c = s[j]; | ||
| + if (c === "\\" && (s[j + 1] === '"' || s[j + 1] === "\\")) { | ||
| + out += s[j + 1]; | ||
| + j += 2; | ||
| + } else if (c === '"') return { value: out, end: j + 1 }; | ||
| + else { | ||
| + out += c; | ||
| + j++; | ||
| + } | ||
| + } | ||
| + return null; | ||
| +} | ||
| + | ||
| +function lex(s: string): Lexed[] { | ||
| + const out: Lexed[] = []; | ||
| + let i = 0; | ||
| + while (i < s.length) { | ||
| + while (i < s.length && isWs(s[i])) i++; | ||
| + if (i >= s.length) break; | ||
| + const start = i; | ||
| + let neg = false; | ||
| + if (s[i] === "-" && i + 1 < s.length && !isWs(s[i + 1])) { | ||
| + neg = true; | ||
| + i++; | ||
| + } | ||
| + // a quoted value (free text, or a qualifier's when key is set) from s[at] | ||
| + const quoted = (at: number, key: string | null): boolean => { | ||
| + const q = readQuoted(s, at); | ||
| + if (!q) { | ||
| + out.push({ kind: "error", start, end: s.length, code: "unterminated_quote" }); | ||
| + i = s.length; | ||
| + return false; | ||
| + } | ||
| + if (q.end < s.length && !isWs(s[q.end])) { | ||
| + const end = nextWs(s, q.end); | ||
| + out.push({ kind: "error", start, end, code: "unexpected_quote" }); | ||
| + i = end; | ||
| + return true; | ||
| + } | ||
| + out.push(key === null ? { kind: "quoted", start, end: q.end, neg, value: q.value } : { kind: "qualifier", start, end: q.end, neg, key, value: q.value }); | ||
| + i = q.end; | ||
| + return true; | ||
| + }; | ||
| + if (s[i] === '"') { | ||
| + if (!quoted(i, null)) break; | ||
| + continue; | ||
| + } | ||
| + let j = i; | ||
| + while (j < s.length && !isWs(s[j]) && s[j] !== '"') j++; | ||
| + const text = s.slice(i, j); | ||
| + if (j < s.length && s[j] === '"') { | ||
| + if (KEY_COLON.test(text)) { | ||
| + if (!quoted(j, text.slice(0, -1))) break; | ||
| + continue; | ||
| + } | ||
| + const end = nextWs(s, j); | ||
| + out.push({ kind: "error", start, end, code: "unexpected_quote" }); | ||
| + i = end; | ||
| + continue; | ||
| + } | ||
| + out.push({ kind: "bare", start, end: j, neg, text }); | ||
| + i = j; | ||
| + } | ||
| + return out; | ||
| +} | ||
| + | ||
| +// --- values | ||
| + | ||
| +function dayOf(s: string): Day | null { | ||
| + const m = /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/.exec(s); | ||
| + if (!m) return null; | ||
| + const y = Number(m[1]); | ||
| + const mo = Number(m[2]); | ||
| + const d = Number(m[3]); | ||
| + if (y < 1970 || y > 9999 || mo < 1 || mo > 12 || d < 1) return null; | ||
| + const start = Date.UTC(y, mo - 1, d); | ||
| + if (new Date(start).getUTCDate() !== d) return null; | ||
| + return { text: s, start }; | ||
| +} | ||
| + | ||
| +export function dateFilterOf(v: string): DateFilter | null { | ||
| + const dots = v.indexOf(".."); | ||
| + if (dots >= 0) { | ||
| + const a = v.slice(0, dots); | ||
| + const b = v.slice(dots + 2); | ||
| + const from = a === "*" ? null : dayOf(a); | ||
| + const to = b === "*" ? null : dayOf(b); | ||
| + if ((a !== "*" && !from) || (b !== "*" && !to) || (!from && !to)) return null; | ||
| + if (from && to && from.start > to.start) return null; | ||
| + return { kind: "range", from, to }; | ||
| + } | ||
| + const m = /^(<=|>=|<|>)(.*)$/s.exec(v); | ||
| + if (m) { | ||
| + const op = m[1] as DateOp; | ||
| + const day = dayOf(m[2]); | ||
| + if (day) return { kind: "cmp", op, day }; | ||
| + const rel = /^([0-9]+)([hdwHDW])$/.exec(m[2]); | ||
| + if (!rel) return null; | ||
| + const n = Number(rel[1]); | ||
| + if (!Number.isSafeInteger(n) || n < 1 || n > 9999) return null; | ||
| + return { kind: "age", op, n, unit: rel[2].toLowerCase() as RelUnit }; | ||
| + } | ||
| + const day = dayOf(v); | ||
| + return day ? { kind: "day", day } : null; | ||
| +} | ||
| + | ||
| +export function labelOf(v: string): string | null { | ||
| + const l = v.trim().toLowerCase().replace(/\s+/g, " "); | ||
| + return l.length <= LABEL_MAX && LABEL.test(l) ? l : null; | ||
| +} | ||
| + | ||
| +function userOf(v: string): UserRef | null { | ||
| + const l = v.toLowerCase(); | ||
| + if (l === "@me") return { kind: "me" }; | ||
| + if (UUID.test(l)) return { kind: "id", id: l }; | ||
| + if (SLUG.test(l)) return { kind: "slug", slug: l }; | ||
| + return null; | ||
| +} | ||
| + | ||
| +// --- terms | ||
| + | ||
| +type Failure = { code: QueryErrorCode; message: string }; | ||
| +const fail = (code: QueryErrorCode, message: string): Failure => ({ code, message }); | ||
| + | ||
| +function qualifier(target: Target, rawKey: string, value: string, neg: boolean): Term | Failure { | ||
| + const key = rawKey.toLowerCase(); | ||
| + if (!Object.hasOwn(KEYS, key)) { | ||
| + return fail("unknown_qualifier", `${key}: is not something ${LIST[target]} can be searched by. Remove it, or put it in quotes to search for the text.`); | ||
| + } | ||
| + const k = key as Key; | ||
| + if (!KEYS[k].includes(target)) return fail("unsupported_qualifier", `${k}: does not apply to ${LIST[target]}.`); | ||
| + if (value === "") return fail("empty_value", `${k}: needs a value, for example ${EXAMPLE[k]}.`); | ||
| + if (neg && NO_NEGATION.has(k)) return fail("negation_not_allowed", `${k}: cannot be negated. Remove the - before it.`); | ||
| + const lower = value.toLowerCase(); | ||
| + switch (k) { | ||
| + case "is": { | ||
| + const s = STATES[target].find((x) => x === lower); | ||
| + return s ? { kind: "is", neg, value: s } : fail("bad_value", `Use ${or(STATES[target].map((x) => `is:${x}`))} for ${LIST[target]}.`); | ||
| + } | ||
| + case "label": { | ||
| + const l = labelOf(value); | ||
| + return l !== null ? { kind: "label", neg, value: l } : fail("bad_value", `A label is up to ${LABEL_MAX} letters, digits, spaces, dots, dashes or underscores, starting with a letter or digit.`); | ||
| + } | ||
| + case "no": | ||
| + return lower === "label" ? { kind: "no", value: "label" } : fail("bad_value", "Use no:label for issues without labels."); | ||
| + case "author": | ||
| + case "review-requested": { | ||
| + const u = userOf(value); | ||
| + if (!u) return fail("bad_value", `Use ${k}:@me, a user id, or the name of one of the organization's agents.`); | ||
| + return k === "author" ? { kind: "author", neg, user: u } : { kind: "review-requested", neg, user: u }; | ||
| + } | ||
| + case "sort": { | ||
| + const s = SORTS[target].find((x) => x === lower); | ||
| + return s ? { kind: "sort", value: s } : fail("bad_value", `Use ${or(SORTS[target].map((x) => `sort:${x}`))} for ${LIST[target]}.`); | ||
| + } | ||
| + case "created": | ||
| + case "updated": { | ||
| + const f = dateFilterOf(value); | ||
| + return f ? { kind: "date", field: k, filter: f } : fail("bad_date", `Use a day like ${k}:2026-10-01 (also with >, >=, < or <=), a range like ${k}:2026-10-01..2026-10-31, or an age like ${k}:<7d.`); | ||
| + } | ||
| + } | ||
| +} | ||
| + | ||
| +function or(list: readonly string[]): string { | ||
| + return list.length < 2 ? list.join("") : `${list.slice(0, -1).join(", ")} or ${list[list.length - 1]}`; | ||
| +} | ||
| + | ||
| +function termOf(target: Target, t: Exclude<Lexed, { kind: "error" }>): Term | Failure { | ||
| + if (t.kind === "quoted") { | ||
| + return /\S/.test(t.value) ? { kind: "text", neg: t.neg, value: t.value } : fail("empty_value", "These quotes hold no text. Write something between them or remove them."); | ||
| + } | ||
| + if (t.kind === "qualifier") return qualifier(target, t.key, t.value, t.neg); | ||
| + const m = QUALIFIER_RE.exec(t.text); | ||
| + if (m) return qualifier(target, m[0].slice(0, -1), t.text.slice(m[0].length), t.neg); | ||
| + if (NUMBER_RE.test(t.text)) { | ||
| + return t.neg ? fail("negation_not_allowed", "A number cannot be negated. Remove the - before it.") : { kind: "number", value: Number(t.text.slice(1)) }; | ||
| + } | ||
| + return { kind: "text", neg: t.neg, value: t.text }; | ||
| +} | ||
| + | ||
| +const LEX_MESSAGE = { | ||
| + unterminated_quote: 'This quote is never closed. Add a " at the end of the phrase.', | ||
| + unexpected_quote: 'A quote starts a phrase or a qualifier\'s value and ends at a space. Add a space next to it, or remove it.', | ||
| +}; | ||
| + | ||
| +export function parseQuery(input: string, target: Target): ParseResult { | ||
| + try { | ||
| + return parse(String(input), target === "pulls" ? "pulls" : "issues"); | ||
| + } catch { | ||
| + // not reached: the parser handles every string; a query is data, never an exception | ||
| + const s = String(input ?? ""); | ||
| + return { ok: false, errors: [{ code: "bad_value", message: "This search could not be read. Change it and try again.", start: 0, end: s.length }] }; | ||
| + } | ||
| +} | ||
| + | ||
| +function parse(input: string, target: Target): ParseResult { | ||
| + if (input.length > MAX_INPUT) { | ||
| + return { ok: false, errors: [{ code: "too_long", message: `A search is at most ${MAX_INPUT.toLocaleString("en-US")} characters. Shorten it.`, start: 0, end: input.length }] }; | ||
| + } | ||
| + const lexed = lex(input); | ||
| + if (lexed.length > MAX_TERMS) { | ||
| + return { ok: false, errors: [{ code: "too_many_terms", message: `A search has at most ${MAX_TERMS} terms. Remove some.`, start: lexed[MAX_TERMS].start, end: input.length }] }; | ||
| + } | ||
| + const errors: QueryError[] = []; | ||
| + const terms: Term[] = []; | ||
| + const seen = new Set<string>(); | ||
| + let sort: string | null = null; | ||
| + for (const t of lexed) { | ||
| + if (t.kind === "error") { | ||
| + errors.push({ code: t.code, message: LEX_MESSAGE[t.code], start: t.start, end: t.end }); | ||
| + continue; | ||
| + } | ||
| + const r = termOf(target, t); | ||
| + if ("code" in r) { | ||
| + errors.push({ code: r.code, message: r.message, start: t.start, end: t.end }); | ||
| + continue; | ||
| + } | ||
| + if (r.kind === "sort") { | ||
| + if (sort !== null && sort !== r.value) { | ||
| + errors.push({ code: "conflict", message: `This search is already sorted by ${sort}. Keep one sort: term.`, start: t.start, end: t.end }); | ||
| + continue; | ||
| + } | ||
| + sort = r.value; | ||
| + } | ||
| + const text = termText(r); | ||
| + if (seen.has(text)) continue; | ||
| + seen.add(text); | ||
| + terms.push(r); | ||
| + } | ||
| + if (errors.length) return { ok: false, errors }; | ||
| + const query: SearchQuery = { target, terms }; | ||
| + return { ok: true, query }; | ||
| +} | ||
| + | ||
Addedbuild-a-ts/apps/web/src/search/serialize.ts+56−0
| @@ -0,0 +1,56 @@ | ||
| +// The canonical form of a search query (search/api.ts): what the pages put in ?q=, so a URL | ||
| +// round-trips. Terms in input order joined by single spaces, keys lowercase, values normalized, a | ||
| +// value bare when nothing in it would read differently bare, otherwise quoted with \ and " escaped. | ||
| + | ||
| +import type { DateFilter, SearchQuery, Term, UserRef } from "./ast.ts"; | ||
| + | ||
| +// The patterns the parser reads a bare term with: free text that matches one must be quoted to stay | ||
| +// free text. | ||
| +export const QUALIFIER_RE = /^[A-Za-z]+(-[A-Za-z]+)*:/; | ||
| +export const NUMBER_RE = /^#[1-9][0-9]{0,8}$/; | ||
| +const BARE_RE = /^[^\s"]+$/; | ||
| + | ||
| +export function quote(v: string): string { | ||
| + return `"${v.replace(/[\\"]/g, (c) => `\\${c}`)}"`; | ||
| +} | ||
| + | ||
| +export function qualifierValue(v: string): string { | ||
| + return BARE_RE.test(v) ? v : quote(v); | ||
| +} | ||
| + | ||
| +export function textValue(v: string): string { | ||
| + return BARE_RE.test(v) && !v.startsWith("-") && !QUALIFIER_RE.test(v) && !NUMBER_RE.test(v) ? v : quote(v); | ||
| +} | ||
| + | ||
| +export function userText(u: UserRef): string { | ||
| + return u.kind === "me" ? "@me" : u.kind === "id" ? u.id : u.slug; | ||
| +} | ||
| + | ||
| +export function dateText(f: DateFilter): string { | ||
| + switch (f.kind) { | ||
| + case "day": return f.day.text; | ||
| + case "cmp": return `${f.op}${f.day.text}`; | ||
| + case "range": return `${f.from ? f.from.text : "*"}..${f.to ? f.to.text : "*"}`; | ||
| + case "age": return `${f.op}${f.n}${f.unit}`; | ||
| + } | ||
| +} | ||
| + | ||
| +// One term's canonical text: also what duplicates are recognized by. | ||
| +export function termText(t: Term): string { | ||
| + const neg = "neg" in t && t.neg ? "-" : ""; | ||
| + switch (t.kind) { | ||
| + case "is": return `${neg}is:${t.value}`; | ||
| + case "label": return `${neg}label:${qualifierValue(t.value)}`; | ||
| + case "no": return `no:${t.value}`; | ||
| + case "author": return `${neg}author:${qualifierValue(userText(t.user))}`; | ||
| + case "review-requested": return `${neg}review-requested:${qualifierValue(userText(t.user))}`; | ||
| + case "sort": return `sort:${t.value}`; | ||
| + case "date": return `${t.field}:${qualifierValue(dateText(t.filter))}`; | ||
| + case "number": return `#${t.value}`; | ||
| + case "text": return `${neg}${textValue(t.value)}`; | ||
| + } | ||
| +} | ||
| + | ||
| +export function serializeQuery(query: SearchQuery): string { | ||
| + return query.terms.map(termText).join(" "); | ||
| +} | ||
Addedbuild-a-ts/apps/web/src/search/sql.ts+140−0
| @@ -0,0 +1,140 @@ | ||
| +// A parsed search query as SQL (search/api.ts): a WHERE expression and an ORDER BY list for the | ||
| +// target's D1 table (issues, or pull_index for pull requests), used as | ||
| +// SELECT ... FROM <table> WHERE ${where} AND <more> ORDER BY ${orderBy} LIMIT ? | ||
| +// | ||
| +// The contract, which nothing a person types can change: | ||
| +// - every value is bound: the text holds only the constants written here, each value (ids from ctx | ||
| +// included) is a ? placeholder, and params carries them in placeholder order; | ||
| +// - the scope comes first and stays: repo_id = ? AND org_id = ? at the top level, every term ANDed | ||
| +// after it in its own parentheses, so no OR reaches the top; | ||
| +// - a negated term matches exactly the rows its positive form does not: NOT IFNULL((...), 0), so a | ||
| +// NULL (an agent-less pull request's owner_id) counts as "does not match"; | ||
| +// - ORDER BY comes from fixed column names and directions. | ||
| + | ||
| +import type { DateFilter, SearchQuery, SortValue, SqlCtx, SqlResult, SqlValue, Term, UserRef } from "./ast.ts"; | ||
| + | ||
| +const HOUR_MS = 3_600_000; | ||
| +const DAY_MS = 86_400_000; | ||
| +const UNIT_MS = { h: HOUR_MS, d: DAY_MS, w: 7 * DAY_MS } as const; | ||
| + | ||
| +type Cond = { sql: string; params: SqlValue[] }; | ||
| +const cond = (sql: string, ...params: SqlValue[]): Cond => ({ sql, params }); | ||
| +const NOTHING = cond("0"); | ||
| + | ||
| +// An issue's labels as a JSON array whatever is stored (json_each and json_array_length fail on text | ||
| +// that is not JSON; parseLabels reads such a row as no labels, and so does the search). | ||
| +const LABELS = "(CASE WHEN json_valid(issues.labels) THEN CASE json_type(issues.labels) WHEN 'array' THEN issues.labels ELSE '[]' END ELSE '[]' END)"; | ||
| + | ||
| +export function likePattern(text: string): string { | ||
| + return `%${text.replace(/[\\%_]/g, (c) => `\\${c}`)}%`; | ||
| +} | ||
| + | ||
| +// Rows whose columns name this person. An agent's slug names the agent of the repository's own | ||
| +// organization only; an unknown slug, or @me signed out, names nobody. | ||
| +function userCond(u: UserRef, ctx: SqlCtx, table: "issues" | "pull_index", columns: readonly string[]): Cond { | ||
| + if (u.kind === "slug") { | ||
| + const cols = columns.map((c) => `${table}.${c}`).join(", "); | ||
| + return cond(`EXISTS (SELECT 1 FROM agents a WHERE a.org_id = ? AND a.slug = ? AND a.user_id IN (${cols}))`, ctx.orgId, u.slug); | ||
| + } | ||
| + const id = u.kind === "me" ? ctx.viewerId : u.id; | ||
| + if (typeof id !== "string") return NOTHING; | ||
| + return cond(columns.map((c) => `${table}.${c} = ?`).join(" OR "), ...columns.map(() => id)); | ||
| +} | ||
| + | ||
| +function reviewCond(u: UserRef, ctx: SqlCtx): Cond { | ||
| + const base = "SELECT 1 FROM pull_review_requests r WHERE r.repo_id = pull_index.repo_id AND r.number = pull_index.number"; | ||
| + if (u.kind === "slug") return cond(`EXISTS (${base} AND r.user_id IN (SELECT a.user_id FROM agents a WHERE a.org_id = ? AND a.slug = ?))`, ctx.orgId, u.slug); | ||
| + const id = u.kind === "me" ? ctx.viewerId : u.id; | ||
| + if (typeof id !== "string") return NOTHING; | ||
| + return cond(`EXISTS (${base} AND r.user_id = ?)`, id); | ||
| +} | ||
| + | ||
| +function dateCond(column: string, f: DateFilter, now: number): Cond { | ||
| + switch (f.kind) { | ||
| + case "day": return cond(`${column} >= ? AND ${column} < ?`, f.day.start, f.day.start + DAY_MS); | ||
| + case "cmp": | ||
| + switch (f.op) { | ||
| + case ">": return cond(`${column} >= ?`, f.day.start + DAY_MS); | ||
| + case ">=": return cond(`${column} >= ?`, f.day.start); | ||
| + case "<": return cond(`${column} < ?`, f.day.start); | ||
| + case "<=": return cond(`${column} < ?`, f.day.start + DAY_MS); | ||
| + } | ||
| + break; | ||
| + case "range": { | ||
| + const parts: Cond[] = []; | ||
| + if (f.from) parts.push(cond(`${column} >= ?`, f.from.start)); | ||
| + if (f.to) parts.push(cond(`${column} < ?`, f.to.start + DAY_MS)); | ||
| + return cond(parts.map((p) => p.sql).join(" AND "), ...parts.flatMap((p) => p.params)); | ||
| + } | ||
| + case "age": { | ||
| + // the age (now - t) against N units: t against T = now - N units | ||
| + const t = now - f.n * UNIT_MS[f.unit]; | ||
| + const op = f.op === "<" ? ">" : f.op === "<=" ? ">=" : f.op === ">" ? "<" : "<="; | ||
| + return cond(`${column} ${op} ?`, t); | ||
| + } | ||
| + } | ||
| + return NOTHING; | ||
| +} | ||
| + | ||
| +function termCond(t: Term, q: SearchQuery, ctx: SqlCtx): Cond | null { | ||
| + const issues = q.target === "issues"; | ||
| + const table = issues ? "issues" : "pull_index"; | ||
| + switch (t.kind) { | ||
| + case "is": | ||
| + switch (t.value) { | ||
| + case "open": return cond("state = 'open'"); | ||
| + case "closed": return cond(issues ? "state = 'closed'" : "state IN ('closed', 'merged')"); | ||
| + case "merged": return issues ? NOTHING : cond("state = 'merged'"); | ||
| + case "draft": return issues ? NOTHING : cond("draft = 1"); | ||
| + } | ||
| + break; | ||
| + case "label": | ||
| + return issues ? cond(`EXISTS (SELECT 1 FROM json_each(${LABELS}) j WHERE j.value = ?)`, t.value) : NOTHING; | ||
| + case "no": | ||
| + return issues ? cond(`json_array_length(${LABELS}) = 0`) : NOTHING; | ||
| + case "author": | ||
| + return userCond(t.user, ctx, table, issues ? ["author_id"] : ["author_id", "owner_id"]); | ||
| + case "review-requested": | ||
| + return issues ? NOTHING : reviewCond(t.user, ctx); | ||
| + case "number": | ||
| + return cond("number = ?", t.value); | ||
| + case "text": { | ||
| + const p = likePattern(t.value); | ||
| + return issues ? cond("title LIKE ? ESCAPE '\\' OR body LIKE ? ESCAPE '\\'", p, p) : cond("title LIKE ? ESCAPE '\\'", p); | ||
| + } | ||
| + case "date": | ||
| + return dateCond(t.field === "created" ? "created_at" : "updated_at", t.filter, ctx.now); | ||
| + case "sort": | ||
| + return null; | ||
| + } | ||
| + return NOTHING; | ||
| +} | ||
| + | ||
| +const ORDER: Record<SortValue, string> = { | ||
| + "created-desc": "created_at DESC, number DESC", | ||
| + "created-asc": "created_at ASC, number ASC", | ||
| + "updated-desc": "updated_at DESC, number DESC", | ||
| + "updated-asc": "updated_at ASC, number ASC", | ||
| + "comments-desc": "comments DESC, number DESC", | ||
| + "comments-asc": "comments ASC, number ASC", | ||
| +}; | ||
| + | ||
| +export function sortOf(q: SearchQuery): SortValue { | ||
| + for (const t of q.terms) if (t.kind === "sort") return t.value; | ||
| + return "created-desc"; | ||
| +} | ||
| + | ||
| +export function toSql(query: SearchQuery, ctx: SqlCtx): SqlResult { | ||
| + const parts = ["repo_id = ?", "org_id = ?"]; | ||
| + const params: SqlValue[] = [String(ctx.repoId), String(ctx.orgId)]; | ||
| + for (const t of query.terms) { | ||
| + const c = termCond(t, query, ctx); | ||
| + if (!c) continue; | ||
| + parts.push("neg" in t && t.neg ? `NOT IFNULL((${c.sql}), 0)` : `(${c.sql})`); | ||
| + params.push(...c.params); | ||
| + } | ||
| + let order = ORDER[sortOf(query)]; | ||
| + // comments is an issues column; a pulls query never parses with it, and never gets it here | ||
| + if (query.target === "pulls" && order.startsWith("comments")) order = ORDER["created-desc"]; | ||
| + return { where: parts.join(" AND "), params, orderBy: order }; | ||
| +} | ||
Addedbuild-a-ts/apps/web/src/ui/search.ts+97−0
| @@ -0,0 +1,97 @@ | ||
| +// The search box of the Issues and Pull requests lists (ui/issues.ts, ui/pulls.ts; the language is | ||
| +// search/api.ts): the box with the canonical query (or the input as typed when it has errors), the | ||
| +// syntax help as a disclosure that works without script, and the errors under the box, each one's | ||
| +// span of the input marked with a wavy underline and its number, so they never rely on color alone. | ||
| + | ||
| +import type { QueryError, Target } from "../search/api.ts"; | ||
| +import { button } from "./components.ts"; | ||
| +import { html, type Html } from "./html.ts"; | ||
| + | ||
| +export type SearchView = { | ||
| + // what the box shows | ||
| + input: string; | ||
| + errors: readonly QueryError[]; | ||
| +}; | ||
| + | ||
| +const HELP: Record<Target, readonly [string, string][]> = { | ||
| + issues: [ | ||
| + ["is:open", "Open issues; is:closed for closed ones"], | ||
| + ["label:bug", 'Has this label; label:"needs review" for a name with spaces'], | ||
| + ["no:label", "Has no labels"], | ||
| + ["author:@me", "Opened by you, by a user id, or by an agent of the organization (author:deploy-bot)"], | ||
| + ["#12", "The issue with this number"], | ||
| + ['"login fails"', "This text in the title or the description; a bare word is searched for on its own"], | ||
| + ["-label:wontfix", "A - before a term leaves out what it matches"], | ||
| + ["created:>=2026-10-01", "Opened on or after a day (UTC); also >, <, <=, a day, or a range 2026-10-01..2026-10-31"], | ||
| + ["updated:<7d", "Updated in the last 7 days; ages in h, d or w, and >7d for older ones"], | ||
| + ["sort:updated-desc", "Order: created, updated or comments, each -desc or -asc (newest first by default)"], | ||
| + ], | ||
| + pulls: [ | ||
| + ["is:open", "Open pull requests, drafts included; is:merged, is:closed (closed or merged), is:draft"], | ||
| + ["author:@me", "Opened by you, by a user id, or by an agent of the organization (with its owner's pull requests)"], | ||
| + ["review-requested:@me", "Your review was requested"], | ||
| + ["#12", "The pull request with this number"], | ||
| + ['"fix login"', "This text in the title; a bare word is searched for on its own"], | ||
| + ["-is:draft", "A - before a term leaves out what it matches"], | ||
| + ["created:>=2026-10-01", "Opened on or after a day (UTC); also >, <, <=, a day, or a range 2026-10-01..2026-10-31"], | ||
| + ["updated:<7d", "Updated in the last 7 days; ages in h, d or w, and >7d for older ones"], | ||
| + ["sort:updated-desc", "Order: created or updated, each -desc or -asc (newest first by default)"], | ||
| + ], | ||
| +}; | ||
| + | ||
| +export function syntaxHelp(target: Target, id: string): Html { | ||
| + const what = target === "issues" ? "issues" : "pull requests"; | ||
| + return html`<details class="search-help"> | ||
| +<summary>Search syntax</summary> | ||
| +<div class="search-help-panel" id="${id}-help"> | ||
| +<p class="small">Every term must match. Search ${what} with:</p> | ||
| +<table class="search-help-table"> | ||
| +<caption class="visually-hidden">Search qualifiers for ${what}, with an example each</caption> | ||
| +<thead class="visually-hidden"><tr><th scope="col">Example</th><th scope="col">Finds</th></tr></thead> | ||
| +<tbody>${HELP[target].map(([ex, what]) => html`<tr><th scope="row"><code>${ex}</code></th><td>${what}</td></tr>`)}</tbody> | ||
| +</table> | ||
| +</div> | ||
| +</details>`; | ||
| +} | ||
| + | ||
| +export function searchForm(o: { action: string; id: string; label: string; target: Target; search: SearchView }): Html { | ||
| + const bad = o.search.errors.length > 0; | ||
| + return html`<div class="list-search"> | ||
| +<form method="get" action="${o.action}" class="pr-search" role="search"> | ||
| +<label class="visually-hidden" for="${o.id}">${o.label}</label> | ||
| +<input id="${o.id}" name="q" type="search" value="${o.search.input}" placeholder="${o.target === "issues" ? "is:open label:bug" : "is:open review-requested:@me"}" maxlength="1024" autocomplete="off" spellcheck="false" autocapitalize="off"${bad ? html` aria-invalid="true" aria-describedby="${o.id}-errors"` : ""}> | ||
| +${button("Search", { size: "sm" })} | ||
| +</form> | ||
| +${syntaxHelp(o.target, o.id)} | ||
| +</div>`; | ||
| +} | ||
| + | ||
| +// Previous and next page of a list, both keeping the query. | ||
| +export function listPager(prev: string | null, next: string | null, what: string): Html { | ||
| + if (!prev && !next) return html``; | ||
| + return html`<div class="card-foot"><nav class="list-pager" aria-label="Pages of ${what}">${prev ? html`<a href="${prev}" rel="prev">Previous page</a>` : ""}${next ? html`<a href="${next}" rel="next">Next page</a>` : ""}</nav></div>`; | ||
| +} | ||
| + | ||
| +// The errors under the box: the input with each error's span marked and numbered, then the messages | ||
| +// in the same order. | ||
| +export function searchErrors(id: string, search: SearchView): Html { | ||
| + const errs = search.errors; | ||
| + if (!errs.length) return html``; | ||
| + const input = search.input; | ||
| + const parts: Html[] = []; | ||
| + let at = 0; | ||
| + errs.forEach((e, i) => { | ||
| + const start = Math.max(at, Math.min(e.start, input.length)); | ||
| + const end = Math.max(start, Math.min(e.end, input.length)); | ||
| + if (start > at) parts.push(html`${input.slice(at, start)}`); | ||
| + parts.push(html`<mark class="search-bad">${input.slice(start, end) || " "}<sup class="search-bad-n" aria-hidden="true">${i + 1}</sup><span class="visually-hidden"> (problem ${i + 1})</span></mark>`); | ||
| + at = end; | ||
| + }); | ||
| + if (at < input.length) parts.push(html`${input.slice(at)}`); | ||
| + const n = errs.length; | ||
| + return html`<div class="search-errors" id="${id}-errors"> | ||
| +<p class="search-errors-head">${n === 1 ? "This search has a problem" : `This search has ${n} problems`}, so nothing is listed. Fix ${n === 1 ? "it" : "them"} and search again.</p> | ||
| +<p class="search-echo mono">${parts}</p> | ||
| +<ol class="search-error-list">${errs.map((e) => html`<li><code>${input.slice(e.start, e.end).slice(0, 80) || "(empty)"}</code> ${e.message}</li>`)}</ol> | ||
| +</div>`; | ||
| +} | ||
Addedbuild-a-ts/test/unit/search/pages.test.ts+113−0
| @@ -0,0 +1,113 @@ | ||
| +// unit/search/pages (repo/list-search.ts, search/edit.ts, ui/search.ts): how the Issues and Pull | ||
| +// requests lists read ?q= (the default is:open, the old state= and label= links), the canonical links | ||
| +// they make (tabs, label chips, pages), and the page with an invalid query: the input kept as typed, | ||
| +// each error under the box with its span marked by more than color, no rows, the syntax help. | ||
| + | ||
| +import { test } from "node:test"; | ||
| +import assert from "node:assert/strict"; | ||
| +import { listSearchOf, searchHref } from "../../../apps/web/src/repo/list-search.ts"; | ||
| +import { parseQuery, serializeQuery } from "../../../apps/web/src/search/api.ts"; | ||
| +import { hasFilters, stateTab, withState, withTerm } from "../../../apps/web/src/search/edit.ts"; | ||
| +import { issuesListBody } from "../../../apps/web/src/ui/issues.ts"; | ||
| +import { pullsListBody } from "../../../apps/web/src/ui/pulls.ts"; | ||
| + | ||
| +const repo = { org: "rebasely", repo: "docs", visibility: "public" as const }; | ||
| +const P = (s: string) => new URLSearchParams(s); | ||
| + | ||
| +test("?q= as typed, the default is:open, and the old links' state= and label=", () => { | ||
| + assert.equal(listSearchOf(P(""), "issues").input, "is:open"); | ||
| + assert.equal(listSearchOf(P("state=closed&label=Needs%20Review"), "issues").input, 'is:closed label:"needs review"'); | ||
| + assert.equal(listSearchOf(P("state=all&label=bug"), "issues").input, "label:bug"); | ||
| + assert.equal(listSearchOf(P("state=merged"), "pulls").input, "is:merged"); | ||
| + assert.equal(listSearchOf(P("state=merged"), "issues").input, "is:open"); | ||
| + assert.equal(listSearchOf(P("label=bug"), "pulls").input, "is:open"); | ||
| + assert.equal(listSearchOf(P("q="), "issues").input, ""); | ||
| + assert.equal(listSearchOf(P("q=IS:OPEN%20%20bug&state=closed&page=3"), "issues").input, "is:open bug"); | ||
| + assert.equal(listSearchOf(P("q=bug&page=3"), "issues").page, 3); | ||
| + assert.equal(listSearchOf(P("q=bug&page=-1"), "issues").page, 1); | ||
| + const bad = listSearchOf(P("q=assignee:me%20%20bug"), "issues"); | ||
| + assert.equal(bad.input, "assignee:me bug"); | ||
| + assert.equal(bad.query, null); | ||
| + assert.deepEqual(bad.errors.map((e) => e.code), ["unknown_qualifier"]); | ||
| +}); | ||
| + | ||
| +test("tabs replace the state term, chips add a label, links are canonical", () => { | ||
| + const r = parseQuery("bug is:closed -is:draft label:x", "pulls"); | ||
| + assert.ok(!r.ok); | ||
| + const q = parseQuery("bug is:closed -is:draft sort:updated-asc", "pulls"); | ||
| + assert.ok(q.ok); | ||
| + assert.equal(stateTab(q.query), "closed"); | ||
| + assert.equal(serializeQuery(withState(q.query, "merged")), "is:merged bug -is:draft sort:updated-asc"); | ||
| + assert.equal(serializeQuery(withState(q.query, "all")), "bug -is:draft sort:updated-asc"); | ||
| + assert.equal(stateTab(withState(q.query, "all")), "all"); | ||
| + assert.equal(hasFilters(q.query), true); | ||
| + const two = parseQuery("is:open is:closed", "issues"); | ||
| + assert.ok(two.ok); | ||
| + assert.equal(stateTab(two.query), null); | ||
| + assert.equal(serializeQuery(withState(two.query, "open")), "is:open"); | ||
| + const plain = parseQuery("is:open sort:created-asc", "issues"); | ||
| + assert.ok(plain.ok); | ||
| + assert.equal(hasFilters(plain.query), false); | ||
| + const i = parseQuery("is:open", "issues"); | ||
| + assert.ok(i.ok); | ||
| + const chip = withTerm(i.query, { kind: "label", neg: false, value: "needs review" }); | ||
| + assert.ok(chip); | ||
| + assert.equal(searchHref("/o/r/issues", chip), "/o/r/issues?q=is%3Aopen+label%3A%22needs+review%22"); | ||
| + assert.equal(searchHref("/o/r/issues", { target: "issues", terms: [] }), "/o/r/issues?q="); | ||
| + assert.equal(searchHref("/o/r/issues", i.query, 2), "/o/r/issues?q=is%3Aopen&page=2"); | ||
| +}); | ||
| + | ||
| +const issueData = (search: { input: string; errors: ReturnType<typeof listSearchOf>["errors"] }) => ({ | ||
| + repo, nav: { member: false }, now: 1000, search, tab: search.errors.length ? null : ("open" as const), | ||
| + tabHrefs: { open: "/rebasely/docs/issues?q=is%3Aopen", closed: "/rebasely/docs/issues?q=is%3Aclosed", all: "/rebasely/docs/issues?q=" }, | ||
| + filtered: false, clearHref: "/rebasely/docs/issues?q=is%3Aopen", labelHref: (l: string) => `/rebasely/docs/issues?q=label%3A${l}`, prevHref: null, | ||
| + nextHref: "/rebasely/docs/issues?q=is%3Aopen&page=2", | ||
| + items: [{ number: 3, title: "A title", body: "", state: "open" as const, labels: ["docs"], authorId: "u1", comments: 1, createdAt: 0, updatedAt: 0, closedAt: null, closedBy: null }], | ||
| + counts: { open: 1, closed: 0 }, labels: new Map(), canOpen: false, signedIn: false, membersOnly: false, paused: false, | ||
| +}); | ||
| + | ||
| +test("the issues list: canonical query in the box, syntax help without script, links that keep q", () => { | ||
| + const s = issuesListBody(issueData({ input: "is:open", errors: [] })).s; | ||
| + assert.match(s, /name="q" type="search" value="is:open"/); | ||
| + assert.match(s, /<details class="search-help">\s*<summary>Search syntax<\/summary>/); | ||
| + assert.match(s, /<code>no:label<\/code>/); | ||
| + assert.doesNotMatch(s, /review-requested/); | ||
| + assert.match(s, /href="\/rebasely\/docs\/issues\?q=label%3Adocs">docs<\/a>/); | ||
| + assert.match(s, /href="\/rebasely\/docs\/issues\?q=is%3Aopen&page=2" rel="next"/); | ||
| + assert.match(s, /href="\/rebasely\/docs\/issues\?q=is%3Aclosed"/); | ||
| + assert.match(s, /aria-current="page">Open/); | ||
| + assert.doesNotMatch(s, /aria-invalid/); | ||
| +}); | ||
| + | ||
| +test("an invalid query: kept as typed, each error under the box with its span marked and numbered, nothing listed", () => { | ||
| + const input = 'is:open assignee:me "<b>x'; | ||
| + const r = parseQuery(input, "issues"); | ||
| + assert.ok(!r.ok); | ||
| + const s = issuesListBody(issueData({ input, errors: r.errors })).s; | ||
| + assert.match(s, /value="is:open assignee:me "<b>x"/); | ||
| + assert.match(s, /aria-invalid="true" aria-describedby="issue-q-errors"/); | ||
| + assert.match(s, /id="issue-q-errors"/); | ||
| + assert.match(s, /This search has 2 problems/); | ||
| + assert.match(s, /<mark class="search-bad">assignee:me<sup class="search-bad-n" aria-hidden="true">1<\/sup><span class="visually-hidden"> \(problem 1\)<\/span><\/mark>/); | ||
| + assert.match(s, /<mark class="search-bad">"<b>x<sup/); | ||
| + assert.match(s, /<li><code>assignee:me<\/code> assignee: is not something issues can be searched by/); | ||
| + assert.doesNotMatch(s, /<b>x/); | ||
| + assert.doesNotMatch(s, /pr-row/); | ||
| + assert.doesNotMatch(s, /rel="next"/); | ||
| +}); | ||
| + | ||
| +test("the pull requests list: its own help and tabs", () => { | ||
| + const s = pullsListBody({ | ||
| + repo, nav: { member: false }, now: 1000, search: { input: "is:open", errors: [] }, tab: "open", | ||
| + tabHrefs: { open: "/o?q=is%3Aopen", merged: "/o?q=is%3Amerged", closed: "/o?q=is%3Aclosed", all: "/o?q=" }, filtered: false, clearHref: "/o?q=is%3Aopen", | ||
| + prevHref: "/o?q=is%3Aopen", nextHref: null, counts: { open: 1, closed: 2, merged: 3 }, labels: new Map(), canOpen: false, paused: false, | ||
| + defaultBranch: "main", recent: [], | ||
| + items: [{ number: 4, title: "Fix", state: "open", draft: true, authorKind: "agent", authorId: "u", baseRef: "refs/heads/main", headRef: "refs/heads/x", createdAt: 0, mergedAt: null, closedAt: null, comments: 2 }], | ||
| + }).s; | ||
| + assert.match(s, /<code>review-requested:@me<\/code>/); | ||
| + assert.doesNotMatch(s, /<code>label:bug<\/code>/); | ||
| + assert.match(s, /Merged <span class="count num">3<\/span>/); | ||
| + assert.match(s, /Closed <span class="count num">5<\/span>/); | ||
| + assert.match(s, /rel="prev"/); | ||
| + assert.match(s, /<span class="tag">Draft<\/span>/); | ||
| +}); | ||
Addedbuild-a-ts/test/unit/search/query.test.ts+178−0
| @@ -0,0 +1,178 @@ | ||
| +// unit/search/query (apps/web/src/search/api.ts): the query language's parser and canonical form: | ||
| +// terms, quotes, negation, qualifiers per target, values, dates, errors with their spans and their | ||
| +// order, the limits, and that every valid input's canonical form round-trips. | ||
| + | ||
| +import { test } from "node:test"; | ||
| +import assert from "node:assert/strict"; | ||
| +import { parseQuery, serializeQuery, toSql, type QueryError, type Target } from "../../../apps/web/src/search/api.ts"; | ||
| + | ||
| +const canon = (x: string, t: Target = "issues"): string => { | ||
| + const r = parseQuery(x, t); | ||
| + assert.ok(r.ok, `${JSON.stringify(x)} should parse: ${r.ok ? "" : JSON.stringify(r.errors)}`); | ||
| + return serializeQuery(r.query); | ||
| +}; | ||
| + | ||
| +const errs = (x: string, t: Target = "issues"): Array<[string, number, number]> => { | ||
| + const r = parseQuery(x, t); | ||
| + assert.ok(!r.ok, `${JSON.stringify(x)} should not parse`); | ||
| + for (const e of r.errors) assert.ok(e.message.length > 0); | ||
| + return r.errors.map((e: QueryError) => [e.code, e.start, e.end]); | ||
| +}; | ||
| + | ||
| +const CTX = { repoId: "r1", orgId: "o1", viewerId: "11111111-1111-4111-8111-111111111111", now: 1_800_000_000_000 }; | ||
| + | ||
| +test("terms: whitespace of every kind separates, free text, phrases, numbers and qualifiers", () => { | ||
| + assert.equal(canon(""), ""); | ||
| + assert.equal(canon(" \t\n "), ""); | ||
| + assert.equal(canon("a b c"), "a b c"); | ||
| + assert.equal(canon('"bug"'), "bug"); | ||
| + assert.equal(canon('"label:bug"'), '"label:bug"'); | ||
| + assert.equal(canon('"#12"'), '"#12"'); | ||
| + assert.equal(canon("#12"), "#12"); | ||
| + assert.equal(canon("#0 #1234567890"), "#0 #1234567890"); | ||
| + assert.equal(canon("12:30 a1:b :x"), "12:30 a1:b :x"); | ||
| + assert.equal(canon("OR"), "OR"); | ||
| + assert.equal(canon("-"), '"-"'); | ||
| + assert.equal(canon("- -"), '"-"'); | ||
| + assert.equal(canon("-foo --bar"), '-foo -"-bar"'); | ||
| + assert.equal(canon('"two spaces"'), '"two spaces"'); | ||
| + assert.equal(canon('"a \\"q\\" b\\\\c \\d"'), '"a \\"q\\" b\\\\c \\\\d"'); | ||
| + assert.equal(canon('"x\\y"'), "x\\y"); | ||
| + assert.equal(canon("x\\y"), "x\\y"); | ||
| + assert.equal(canon('-"some phrase"'), '-"some phrase"'); | ||
| +}); | ||
| + | ||
| +test("qualifiers: keys and values are normalized, duplicates dropped, order kept", () => { | ||
| + assert.equal(canon('IS:Open label:" Needs Review " author:@ME updated:<007D'), 'is:open label:"needs review" author:@me updated:<7d'); | ||
| + assert.equal(canon("is:open bug is:open BUG"), "is:open bug BUG"); | ||
| + assert.equal(canon("-label:x -label:X label:x"), "-label:x label:x"); | ||
| + assert.equal(canon("no:LABEL"), "no:label"); | ||
| + assert.equal(canon("author:AAAAAAAA-BBBB-4CCC-8DDD-EEEEEEEEEEEE"), "author:aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee"); | ||
| + assert.equal(canon("author:Deploy-Bot"), "author:deploy-bot"); | ||
| + assert.equal(canon("sort:Updated-Asc sort:updated-asc"), "sort:updated-asc"); | ||
| + assert.equal(canon("is:merged is:draft review-requested:@me -author:bot", "pulls"), "is:merged is:draft review-requested:@me -author:bot"); | ||
| + assert.equal(canon('label:"bug"'), "label:bug"); | ||
| + assert.equal(canon("created:>=2026-10-01 created:2026-10-01..* updated:*..2026-01-31 created:2026-02-28..2026-02-28"), "created:>=2026-10-01 created:2026-10-01..* updated:*..2026-01-31 created:2026-02-28..2026-02-28"); | ||
| + assert.equal(canon("updated:>=0001W created:<=12H"), "updated:>=1w created:<=12h"); | ||
| +}); | ||
| + | ||
| +test("errors: one per invalid term, spanning it, in input order, in the documented check order", () => { | ||
| + assert.deepEqual(errs("assignee:me repo:x milestone:1"), [["unknown_qualifier", 0, 11], ["unknown_qualifier", 12, 18], ["unknown_qualifier", 19, 30]]); | ||
| + assert.deepEqual(errs("http://x"), [["unknown_qualifier", 0, 8]]); | ||
| + assert.deepEqual(errs("label:bug no:label", "pulls"), [["unsupported_qualifier", 0, 9], ["unsupported_qualifier", 10, 18]]); | ||
| + assert.deepEqual(errs("review-requested:@me"), [["unsupported_qualifier", 0, 20]]); | ||
| + assert.deepEqual(errs('label: author:""'), [["empty_value", 0, 6], ["empty_value", 7, 16]]); | ||
| + assert.deepEqual(errs('"" " "'), [["empty_value", 0, 2], ["empty_value", 3, 7]]); | ||
| + assert.deepEqual(errs("-no:label -sort:created-asc -created:2026-01-01 -updated:<7d"), [ | ||
| + ["negation_not_allowed", 0, 9], ["negation_not_allowed", 10, 27], ["negation_not_allowed", 28, 47], ["negation_not_allowed", 48, 60], | ||
| + ]); | ||
| + assert.deepEqual(errs("-#5"), [["negation_not_allowed", 0, 3]]); | ||
| + // the order: unknown before negation, unsupported before empty, empty before negation | ||
| + assert.deepEqual(errs("-assignee:"), [["unknown_qualifier", 0, 10]]); | ||
| + assert.deepEqual(errs("-label:", "pulls"), [["unsupported_qualifier", 0, 7]]); | ||
| + assert.deepEqual(errs("-sort:"), [["empty_value", 0, 6]]); | ||
| + assert.deepEqual(errs("is:merged is:draft sort:comments-desc"), [["bad_value", 0, 9], ["bad_value", 10, 18]]); | ||
| + assert.deepEqual(errs("sort:comments-desc", "pulls"), [["bad_value", 0, 18]]); | ||
| + assert.deepEqual(errs("is:all no:labels author:@someone author:a_b label:-x"), [ | ||
| + ["bad_value", 0, 6], ["bad_value", 7, 16], ["bad_value", 17, 32], ["bad_value", 33, 43], ["bad_value", 44, 52], | ||
| + ]); | ||
| + assert.deepEqual(errs(`label:${"a".repeat(33)}`), [["bad_value", 0, 39]]); | ||
| + assert.deepEqual(errs("sort:created-asc sort:created-desc sort:created-asc"), [["conflict", 17, 34]]); | ||
| +}); | ||
| + | ||
| +test("dates: real UTC days 1970 to 9999, operators, ranges and ages", () => { | ||
| + for (const bad of [ | ||
| + "7d", "=2026-01-01", "2026-1-01", "2026-02-30", "2025-02-29", "1969-12-31", "10000-01-01", "*..*", "2026-02-01..2026-01-31", "<0d", "<10000d", "<7m", | ||
| + "<7d..*", "2026-01-01..", "..2026-01-01", "<", ">=", "<=7d7d", "2026-01-01T00:00", " 2026-01-01", | ||
| + ]) { | ||
| + assert.deepEqual(errs(`created:"${bad}"`).map((e) => e[0]), ["bad_date"], bad); | ||
| + } | ||
| + assert.equal(canon("created:2024-02-29 updated:9999-12-31 created:1970-01-01"), "created:2024-02-29 updated:9999-12-31 created:1970-01-01"); | ||
| + const day = Date.UTC(2026, 9, 1); | ||
| + const D = 86_400_000; | ||
| + const w = (x: string) => { | ||
| + const r = parseQuery(x, "issues"); | ||
| + assert.ok(r.ok); | ||
| + return toSql(r.query, CTX); | ||
| + }; | ||
| + assert.deepEqual(w("created:2026-10-01").params.slice(2), [day, day + D]); | ||
| + assert.deepEqual(w("created:>2026-10-01").params.slice(2), [day + D]); | ||
| + assert.deepEqual(w("created:>=2026-10-01").params.slice(2), [day]); | ||
| + assert.deepEqual(w("created:<2026-10-01").params.slice(2), [day]); | ||
| + assert.deepEqual(w("created:<=2026-10-01").params.slice(2), [day + D]); | ||
| + assert.deepEqual(w("created:2026-10-01..2026-10-03").params.slice(2), [day, day + 3 * D]); | ||
| + assert.deepEqual(w("updated:<7d").params.slice(2), [CTX.now - 7 * D]); | ||
| + assert.deepEqual(w("updated:<2w").params.slice(2), [CTX.now - 14 * D]); | ||
| + assert.deepEqual(w("updated:>=3h").params.slice(2), [CTX.now - 3 * 3_600_000]); | ||
| + assert.match(w("updated:<7d").where, /updated_at > \?\)$/); | ||
| + assert.match(w("updated:<=7d").where, /updated_at >= \?\)$/); | ||
| + assert.match(w("updated:>7d").where, /updated_at < \?\)$/); | ||
| + assert.match(w("updated:>=7d").where, /updated_at <= \?\)$/); | ||
| +}); | ||
| + | ||
| +test("quotes: an unterminated quote stops parsing, an unexpected one spans to the next space", () => { | ||
| + assert.deepEqual(errs('is:open "abc def'), [["unterminated_quote", 8, 16]]); | ||
| + assert.deepEqual(errs('assignee:x -"abc label:'), [["unknown_qualifier", 0, 10], ["unterminated_quote", 11, 23]]); | ||
| + assert.deepEqual(errs('label:"abc'), [["unterminated_quote", 0, 10]]); | ||
| + assert.deepEqual(errs('"a\\"'), [["unterminated_quote", 0, 4]]); | ||
| + assert.deepEqual(errs('ab"c d" x'), [["unexpected_quote", 0, 4], ["unexpected_quote", 5, 7]]); | ||
| + assert.deepEqual(errs('"a"b c'), [["unexpected_quote", 0, 4]]); | ||
| + assert.deepEqual(errs('label:"x"y is:bad'), [["unexpected_quote", 0, 10], ["bad_value", 11, 17]]); | ||
| + assert.deepEqual(errs('lab el:"x"'), [["unknown_qualifier", 4, 10]]); | ||
| + assert.deepEqual(errs('lab e1:"x"'), [["unexpected_quote", 4, 10]]); | ||
| + assert.deepEqual(errs('-label:x"'), [["unexpected_quote", 0, 9]]); | ||
| + assert.equal(canon('label:"needs review" -label:"won t fix"'), 'label:"needs review" -label:"won t fix"'); | ||
| +}); | ||
| + | ||
| +test("limits: too long and too many terms are reported alone", () => { | ||
| + const long = "a".repeat(1025); | ||
| + const r = parseQuery(long, "issues"); | ||
| + assert.ok(!r.ok); | ||
| + assert.deepEqual(r.errors.map((e) => [e.code, e.start, e.end]), [["too_long", 0, 1025]]); | ||
| + assert.ok(parseQuery("a".repeat(1024), "issues").ok); | ||
| + const terms = Array.from({ length: 32 }, () => "assignee:x"); | ||
| + assert.equal(errs(terms.join(" ")).length, 32); | ||
| + const many = [...terms, "x", "y"].join(" "); | ||
| + assert.deepEqual(errs(many), [["too_many_terms", many.indexOf(" x ") + 1, many.length]]); | ||
| + // counted as written, duplicates included | ||
| + assert.equal(errs(Array.from({ length: 33 }, () => "bug").join(" "))[0][0], "too_many_terms"); | ||
| + assert.ok(parseQuery(Array.from({ length: 32 }, () => "bug").join(" "), "issues").ok); | ||
| +}); | ||
| + | ||
| +test("parseQuery never throws and every accepted input round-trips through its canonical form", () => { | ||
| + const pieces = [ | ||
| + "is:open", "IS:CLOSED", "is:merged", "is:draft", "label:Bug", 'label:"a b"', "no:label", "author:@me", "author:bot", "review-requested:@ME", "sort:updated-asc", | ||
| + "created:>=2026-01-01", "updated:<07d", "created:2026-01-01..*", "#7", "word", '"a phrase"', '"-x"', '"x:y"', "-", "-w", '-"p q"', '"\\\\"', '"\\""', "a\\b", | ||
| + '"', "x\"", ":", "-:", "a:", '""', " ", " ", "\t", "é", "ß", "K", "#", "-#1", "--", '"is:open"', "OR", | ||
| + ]; | ||
| + let seed = 7; | ||
| + const rnd = (n: number) => { | ||
| + seed = (seed * 1103515245 + 12345) % 2147483648; | ||
| + return seed % n; | ||
| + }; | ||
| + for (let i = 0; i < 4000; i++) { | ||
| + const n = 1 + rnd(6); | ||
| + let s = ""; | ||
| + for (let j = 0; j < n; j++) s += pieces[rnd(pieces.length)] + [" ", "", " ", " "][rnd(4)]; | ||
| + for (const t of ["issues", "pulls"] as const) { | ||
| + const r = parseQuery(s, t); | ||
| + if (!r.ok) { | ||
| + assert.ok(r.errors.length > 0); | ||
| + for (let k = 0; k < r.errors.length; k++) { | ||
| + const e: QueryError = r.errors[k]; | ||
| + assert.ok(e.start >= 0 && e.start < e.end && e.end <= s.length, JSON.stringify([s, e])); | ||
| + if (k) assert.ok(e.start >= r.errors[k - 1].end); | ||
| + } | ||
| + continue; | ||
| + } | ||
| + const c = serializeQuery(r.query); | ||
| + const r2 = parseQuery(c, t); | ||
| + assert.ok(r2.ok, JSON.stringify([s, c])); | ||
| + assert.equal(serializeQuery(r2.query), c); | ||
| + assert.deepEqual(toSql(r2.query, CTX), toSql(r.query, CTX)); | ||
| + } | ||
| + } | ||
| + for (const weird of [null, undefined, 12, {}, "\ud800", "\"\\", "\\"]) { | ||
| + assert.doesNotThrow(() => parseQuery(weird as unknown as string, "issues")); | ||
| + } | ||
| +}); | ||
Addedbuild-a-ts/test/unit/search/sql.test.ts+203−0
| @@ -0,0 +1,203 @@ | ||
| +// unit/search/sql (apps/web/src/search/sql.ts): toSql run against SQLite (node:sqlite) with the D1 | ||
| +// tables it reads (issues, pull_index, pull_review_requests, agents, as the migrations make them): | ||
| +// what each term matches on each list, that a negated term matches exactly the rest (NULL owners | ||
| +// included), the scope that no query widens, values always bound, and the order. | ||
| + | ||
| +import { test } from "node:test"; | ||
| +import assert from "node:assert/strict"; | ||
| +import { DatabaseSync } from "node:sqlite"; | ||
| +import { parseQuery, toSql, type SqlCtx, type Target } from "../../../apps/web/src/search/api.ts"; | ||
| + | ||
| +const H1 = "11111111-1111-4111-8111-111111111111"; | ||
| +const H2 = "22222222-2222-4222-8222-222222222222"; | ||
| +const BOT = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa"; | ||
| +const OTHER_BOT = "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb"; | ||
| +const DAY = 86_400_000; | ||
| +const T0 = Date.UTC(2026, 9, 1); | ||
| +const NOW = T0 + 30 * DAY; | ||
| + | ||
| +function db(): DatabaseSync { | ||
| + const d = new DatabaseSync(":memory:"); | ||
| + d.exec(` | ||
| + CREATE TABLE issues (repo_id TEXT NOT NULL, number INTEGER NOT NULL, org_id TEXT NOT NULL, author_id TEXT NOT NULL, title TEXT NOT NULL, | ||
| + body TEXT NOT NULL DEFAULT '', state TEXT NOT NULL DEFAULT 'open', labels TEXT NOT NULL DEFAULT '[]', comments INTEGER NOT NULL DEFAULT 0, | ||
| + created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL, closed_at INTEGER, closed_by TEXT, ip_key TEXT, notified INTEGER NOT NULL DEFAULT 0, | ||
| + PRIMARY KEY (repo_id, number)); | ||
| + CREATE TABLE pull_index (repo_id TEXT NOT NULL, number INTEGER NOT NULL, org_id TEXT NOT NULL, author_id TEXT NOT NULL, owner_id TEXT, | ||
| + state TEXT NOT NULL, draft INTEGER NOT NULL DEFAULT 0, title TEXT NOT NULL, base_ref TEXT NOT NULL, head_ref TEXT NOT NULL, | ||
| + created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL, merged_at INTEGER, closed_at INTEGER, PRIMARY KEY (repo_id, number)); | ||
| + CREATE TABLE pull_review_requests (repo_id TEXT NOT NULL, number INTEGER NOT NULL, user_id TEXT NOT NULL, requested_at INTEGER NOT NULL, | ||
| + PRIMARY KEY (repo_id, number, user_id)); | ||
| + CREATE TABLE agents (user_id TEXT PRIMARY KEY, org_id TEXT NOT NULL, slug TEXT NOT NULL, owner_user_id TEXT NOT NULL, UNIQUE (org_id, slug)); | ||
| + `); | ||
| + d.prepare("INSERT INTO agents VALUES (?, ?, ?, ?)").run(BOT, "o1", "bot", H1); | ||
| + d.prepare("INSERT INTO agents VALUES (?, ?, ?, ?)").run(OTHER_BOT, "o2", "other", H2); | ||
| + const issue = d.prepare("INSERT INTO issues (repo_id, number, org_id, author_id, title, body, state, labels, comments, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); | ||
| + issue.run("r1", 1, "o1", H1, "Login fails", "on Safari 100% of the time", "open", '["bug"]', 3, T0, T0 + 20 * DAY); | ||
| + issue.run("r1", 2, "o1", BOT, "Docs typo", "", "closed", '["docs","needs review"]', 0, T0 + DAY, T0 + 2 * DAY); | ||
| + issue.run("r1", 3, "o1", H2, "a_b in names", "path x\\y", "open", "[]", 10, T0 + 2 * DAY + 5, T0 + 29 * DAY); | ||
| + issue.run("r1", 4, "o1", OTHER_BOT, "Stored labels are not JSON", "LOGIN", "open", "not json", 1, T0 + 3 * DAY, T0 + 3 * DAY); | ||
| + issue.run("r1", 5, "o1", H1, "100% broken", "", "closed", '"bug"', 1, T0 + 4 * DAY, T0 + 4 * DAY); | ||
| + // other repositories and another organization: never listed for r1 in o1 | ||
| + issue.run("r2", 1, "o1", H1, "Login fails", "", "open", '["bug"]', 0, T0, T0); | ||
| + issue.run("r1x", 1, "o2", H1, "Login fails", "", "open", '["bug"]', 0, T0, T0); | ||
| + const pull = d.prepare("INSERT INTO pull_index (repo_id, number, org_id, author_id, owner_id, state, draft, title, base_ref, head_ref, created_at, updated_at, merged_at, closed_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'refs/heads/main', 'refs/heads/x', ?, ?, ?, ?)"); | ||
| + pull.run("r1", 1, "o1", H1, null, "open", 0, "Fix login", T0, T0 + 5 * DAY, null, null); | ||
| + pull.run("r1", 2, "o1", BOT, H1, "open", 1, "Bot change", T0 + DAY, T0 + 28 * DAY, null, null); | ||
| + pull.run("r1", 3, "o1", H2, null, "merged", 0, "Merge me", T0 + 2 * DAY, T0 + 2 * DAY, T0 + 2 * DAY, null); | ||
| + pull.run("r1", 4, "o1", H2, null, "closed", 1, "Abandoned draft", T0 + 3 * DAY, T0 + 3 * DAY, null, T0 + 3 * DAY); | ||
| + pull.run("r1", 5, "o1", OTHER_BOT, null, "open", 0, "Other org's bot", T0 + 4 * DAY, T0 + 4 * DAY, null, null); | ||
| + pull.run("r2", 1, "o1", H1, null, "open", 0, "Fix login", T0, T0, null, null); | ||
| + const rr = d.prepare("INSERT INTO pull_review_requests VALUES (?, ?, ?, 0)"); | ||
| + rr.run("r1", 1, H2); | ||
| + rr.run("r1", 3, BOT); | ||
| + rr.run("r2", 2, H2); | ||
| + rr.run("r2", 4, H1); | ||
| + return d; | ||
| +} | ||
| + | ||
| +const DB = db(); | ||
| +const ctx = (viewerId: string | null = H1): SqlCtx => ({ repoId: "r1", orgId: "o1", viewerId, now: NOW }); | ||
| + | ||
| +function run(q: string, target: Target, c: SqlCtx = ctx()): number[] { | ||
| + const r = parseQuery(q, target); | ||
| + assert.ok(r.ok, `${q}: ${r.ok ? "" : JSON.stringify(r.errors)}`); | ||
| + const s = toSql(r.query, c); | ||
| + const table = target === "issues" ? "issues" : "pull_index"; | ||
| + const rows = DB.prepare(`SELECT repo_id, org_id, number FROM ${table} WHERE ${s.where} AND 1 = 1 ORDER BY ${s.orderBy} LIMIT ?`).all(...s.params, 100) as { repo_id: string; org_id: string; number: number }[]; | ||
| + for (const x of rows) assert.deepEqual([x.repo_id, x.org_id], ["r1", "o1"], `${q} left its scope`); | ||
| + return rows.map((x) => x.number); | ||
| +} | ||
| + | ||
| +const sorted = (a: number[]) => [...a].sort((x, y) => x - y); | ||
| +const ALL_ISSUES = [1, 2, 3, 4, 5]; | ||
| +const ALL_PULLS = [1, 2, 3, 4, 5]; | ||
| + | ||
| +test("issues: each term", () => { | ||
| + const i = (q: string, c?: SqlCtx) => sorted(run(q, "issues", c)); | ||
| + assert.deepEqual(i(""), ALL_ISSUES); | ||
| + assert.deepEqual(i("is:open"), [1, 3, 4]); | ||
| + assert.deepEqual(i("is:closed"), [2, 5]); | ||
| + assert.deepEqual(i("is:open is:closed"), []); | ||
| + assert.deepEqual(i("label:bug"), [1]); | ||
| + assert.deepEqual(i('label:"Needs Review"'), [2]); | ||
| + assert.deepEqual(i("no:label"), [3, 4, 5]); | ||
| + assert.deepEqual(i("author:@me"), [1, 5]); | ||
| + assert.deepEqual(i("author:@me", ctx(null)), []); | ||
| + assert.deepEqual(i("-author:@me", ctx(null)), ALL_ISSUES); | ||
| + assert.deepEqual(i(`author:${H2.toUpperCase()}`), [3]); | ||
| + assert.deepEqual(i("author:bot"), [2]); | ||
| + assert.deepEqual(i("author:other"), []); | ||
| + assert.deepEqual(i("author:nobody"), []); | ||
| + assert.deepEqual(i("#3"), [3]); | ||
| + assert.deepEqual(i("#99"), []); | ||
| + assert.deepEqual(i("login"), [1, 4]); | ||
| + assert.deepEqual(i("LOGIN fails"), [1]); | ||
| + assert.deepEqual(i('"login fails"'), [1]); | ||
| + assert.deepEqual(i('"fails login"'), []); | ||
| + assert.deepEqual(i("100%"), [1, 5]); | ||
| + assert.deepEqual(i("%"), [1, 5]); | ||
| + assert.deepEqual(i("_"), [3]); | ||
| + assert.deepEqual(i("x\\y"), [3]); | ||
| + assert.deepEqual(i("\\"), [3]); | ||
| + assert.deepEqual(i("created:2026-10-01"), [1]); | ||
| + assert.deepEqual(i("created:>2026-10-02"), [3, 4, 5]); | ||
| + assert.deepEqual(i("created:>=2026-10-02"), [2, 3, 4, 5]); | ||
| + assert.deepEqual(i("created:<2026-10-02"), [1]); | ||
| + assert.deepEqual(i("created:<=2026-10-02"), [1, 2]); | ||
| + assert.deepEqual(i("created:2026-10-02..2026-10-03"), [2, 3]); | ||
| + assert.deepEqual(i("created:2026-10-04..*"), [4, 5]); | ||
| + assert.deepEqual(i("created:*..2026-10-01"), [1]); | ||
| + assert.deepEqual(i("updated:<2d"), [3]); | ||
| + assert.deepEqual(i("updated:>=10d"), [1, 2, 4, 5]); | ||
| + assert.deepEqual(i("updated:<=1d"), [3]); | ||
| + assert.deepEqual(i("updated:>1d"), [1, 2, 4, 5]); | ||
| + assert.deepEqual(i("updated:<25h"), [3]); | ||
| +}); | ||
| + | ||
| +test("pull requests: each term", () => { | ||
| + const p = (q: string, c?: SqlCtx) => sorted(run(q, "pulls", c)); | ||
| + assert.deepEqual(p(""), ALL_PULLS); | ||
| + assert.deepEqual(p("is:open"), [1, 2, 5]); | ||
| + assert.deepEqual(p("is:closed"), [3, 4]); | ||
| + assert.deepEqual(p("is:merged"), [3]); | ||
| + assert.deepEqual(p("is:draft"), [2, 4]); | ||
| + assert.deepEqual(p("is:open -is:draft"), [1, 5]); | ||
| + assert.deepEqual(p("author:@me"), [1, 2]); | ||
| + assert.deepEqual(p("-author:@me"), [3, 4, 5]); | ||
| + assert.deepEqual(p("author:bot"), [2]); | ||
| + assert.deepEqual(p("-author:bot"), [1, 3, 4, 5]); | ||
| + assert.deepEqual(p("author:other"), []); | ||
| + assert.deepEqual(p(`author:${H2}`), [3, 4]); | ||
| + assert.deepEqual(p("review-requested:@me"), []); | ||
| + assert.deepEqual(p(`review-requested:${H2}`), [1]); | ||
| + assert.deepEqual(p(`-review-requested:${H2}`), [2, 3, 4, 5]); | ||
| + assert.deepEqual(p("review-requested:bot"), [3]); | ||
| + assert.deepEqual(p("review-requested:@me", ctx(H2)), [1]); | ||
| + assert.deepEqual(p("review-requested:@me", ctx(null)), []); | ||
| + assert.deepEqual(p("-review-requested:@me", ctx(null)), ALL_PULLS); | ||
| + assert.deepEqual(p("login"), [1]); | ||
| + assert.deepEqual(p("#4"), [4]); | ||
| + assert.deepEqual(p("created:>=2026-10-03 updated:<2026-10-05"), [3, 4]); | ||
| + assert.deepEqual(p("created:>=2026-10-03 updated:<=2026-10-05"), [3, 4, 5]); | ||
| +}); | ||
| + | ||
| +test("a negated term matches exactly the rows its positive form does not", () => { | ||
| + const terms: Record<Target, string[]> = { | ||
| + issues: ["is:open", "is:closed", "label:bug", "label:docs", "author:@me", "author:bot", "author:other", `author:${H2}`, "login", '"login fails"', "%", "zzz"], | ||
| + pulls: ["is:open", "is:closed", "is:merged", "is:draft", "author:@me", "author:bot", "author:other", `author:${H2}`, "review-requested:@me", `review-requested:${H2}`, "review-requested:bot", "login", "zzz"], | ||
| + }; | ||
| + for (const target of ["issues", "pulls"] as const) { | ||
| + for (const viewer of [H1, H2, null]) { | ||
| + for (const t of terms[target]) { | ||
| + const pos = run(t, target, ctx(viewer)); | ||
| + const neg = run(`-${t}`, target, ctx(viewer)); | ||
| + assert.deepEqual(sorted([...pos, ...neg]), target === "issues" ? ALL_ISSUES : ALL_PULLS, `${target} ${t} viewer ${viewer}`); | ||
| + } | ||
| + } | ||
| + } | ||
| +}); | ||
| + | ||
| +test("sort: the column and the tie-break in the same direction", () => { | ||
| + assert.deepEqual(run("", "issues"), [5, 4, 3, 2, 1]); | ||
| + assert.deepEqual(run("sort:created-asc", "issues"), [1, 2, 3, 4, 5]); | ||
| + assert.deepEqual(run("sort:updated-desc", "issues"), [3, 1, 5, 4, 2]); | ||
| + assert.deepEqual(run("sort:comments-desc", "issues"), [3, 1, 5, 4, 2]); | ||
| + assert.deepEqual(run("sort:comments-asc", "issues"), [2, 4, 5, 1, 3]); | ||
| + assert.deepEqual(run("sort:updated-asc", "pulls"), [3, 4, 5, 1, 2]); | ||
| + const order = (q: string, t: Target) => { | ||
| + const r = parseQuery(q, t); | ||
| + assert.ok(r.ok); | ||
| + return toSql(r.query, ctx()).orderBy; | ||
| + }; | ||
| + assert.equal(order("", "pulls"), "created_at DESC, number DESC"); | ||
| + assert.equal(order("sort:comments-asc", "issues"), "comments ASC, number ASC"); | ||
| + assert.equal(order("sort:updated-desc", "pulls"), "updated_at DESC, number DESC"); | ||
| +}); | ||
| + | ||
| +test("every value is bound: the SQL text holds only its own constants, the scope comes first", () => { | ||
| + const nasty = [`"'; DROP TABLE issues; --"`, `"x' OR '1'='1"`, "label:\"a'b\"", "author:x-y", `"?" "?1" "$a" ":a" "@a"`, `"\\\\'"`, "is:open"]; | ||
| + for (const target of ["issues", "pulls"] as const) { | ||
| + for (const q of nasty) { | ||
| + const r = parseQuery(q, target); | ||
| + if (!r.ok) continue; | ||
| + const evil: SqlCtx = { repoId: "r1' OR 1=1 --", orgId: "o1' --", viewerId: "v' --", now: NOW }; | ||
| + const s = toSql(r.query, evil); | ||
| + assert.ok(s.where.startsWith("repo_id = ? AND org_id = ?"), s.where); | ||
| + const literals = s.where.replace(/'(open|closed|merged|array|\[\]|\\)'/g, ""); | ||
| + assert.doesNotMatch(literals, /'/, `${q}: ${s.where}`); | ||
| + assert.doesNotMatch(s.where, /DROP|1=1|\$a|:a\b|@a|\?1/); | ||
| + assert.equal((s.where.match(/\?/g) ?? []).length, s.params.length); | ||
| + assert.deepEqual(s.params.slice(0, 2), [evil.repoId, evil.orgId]); | ||
| + assert.match(s.orderBy, /^(created_at|updated_at|comments) (ASC|DESC), number (ASC|DESC)$/); | ||
| + // runs, and finds nothing outside a repository that does not exist | ||
| + const table = target === "issues" ? "issues" : "pull_index"; | ||
| + assert.deepEqual(DB.prepare(`SELECT number FROM ${table} WHERE ${s.where} ORDER BY ${s.orderBy} LIMIT ?`).all(...s.params, 10), []); | ||
| + } | ||
| + } | ||
| + // the most bound values a query can need stays under D1's 100 a statement | ||
| + const big = Array.from({ length: 32 }, (_, i) => `w${i}`).join(" "); | ||
| + const r = parseQuery(big, "issues"); | ||
| + assert.ok(r.ok); | ||
| + assert.ok(toSql(r.query, ctx()).params.length + 3 <= 100); | ||
| +}); | ||
Addedbuild-b-tsus/apps/web/src/search/api.ts+51−0
| @@ -0,0 +1,51 @@ | ||
| +// The search query language of the Issues and Pull requests lists, for plain .ts code: a thin adapter | ||
| +// over the .tsus modules next to it (parse, serialize, sql, edit), which hold all the logic. It turns | ||
| +// their Result and Option values into plain objects and nothing else. Loads in plain Node (no | ||
| +// cloudflare:* modules, no bindings), so unit tests import it directly after npm run tsus:web. | ||
| + | ||
| +import { None, Some } from "@tsus/runtime"; | ||
| +import type { ListState, QueryError, QueryErrorCode, SearchQuery, Target } from "./ast.js"; | ||
| +import { tabOf as tabOfQuery, withLabel as withLabelQuery, withState as withStateQuery } from "./edit.js"; | ||
| +import { parseQuery as parse } from "./parse.js"; | ||
| +import { serializeQuery as serialize } from "./serialize.js"; | ||
| +import { toSql as sql } from "./sql.js"; | ||
| + | ||
| +export type { ListState, QueryError, QueryErrorCode, SearchQuery, Target }; | ||
| +export type SqlValue = string | number | null; | ||
| + | ||
| +export type SqlCtx = { | ||
| + repoId: string; | ||
| + orgId: string; | ||
| + viewerId: string | null; | ||
| + now: number; | ||
| +}; | ||
| + | ||
| +export function parseQuery(input: string, target: Target): { ok: true; query: SearchQuery } | { ok: false; errors: QueryError[] } { | ||
| + const r = parse(input, target); | ||
| + return r.isOk() ? { ok: true, query: r.value } : { ok: false, errors: [...r.error.errors] }; | ||
| +} | ||
| + | ||
| +export function serializeQuery(query: SearchQuery): string { | ||
| + return serialize(query); | ||
| +} | ||
| + | ||
| +export function toSql(query: SearchQuery, ctx: SqlCtx): { where: string; params: SqlValue[]; orderBy: string } { | ||
| + const r = sql(query, { repoId: ctx.repoId, orgId: ctx.orgId, viewer: ctx.viewerId === null ? None : Some(ctx.viewerId), now: ctx.now }); | ||
| + return { where: r.where, params: [...r.params], orderBy: r.orderBy }; | ||
| +} | ||
| + | ||
| +// For the pages' links: the state tab a query is on (null: none of them), the query on another tab | ||
| +// (null: All), and the query with a label added (null when the name is no label). | ||
| +export function tabOf(query: SearchQuery): ListState | null { | ||
| + const t = tabOfQuery(query); | ||
| + return t.isSome() ? t.value : null; | ||
| +} | ||
| + | ||
| +export function withState(query: SearchQuery, state: ListState | null): SearchQuery { | ||
| + return withStateQuery(query, state === null ? None : Some(state)); | ||
| +} | ||
| + | ||
| +export function withLabel(query: SearchQuery, label: string): SearchQuery | null { | ||
| + const q = withLabelQuery(query, label); | ||
| + return q.isSome() ? q.value : null; | ||
| +} | ||
Addedbuild-b-tsus/apps/web/src/search/ast.tsus+62−0
| @@ -0,0 +1,62 @@ | ||
| +// The search query language of the Issues and Pull requests lists: the parsed query (an AST) and its | ||
| +// errors. parse.tsus reads a query into this shape, serialize.tsus writes its canonical form and | ||
| +// sql.tsus turns it into a WHERE clause and an ORDER BY list. api.ts is the plain .ts face of it. | ||
| +// | ||
| +// A query is a list of terms, all ANDed, in input order with duplicates removed. Every value in a term | ||
| +// is already normalized (lowercased, trimmed, dates checked), so the serializer and the evaluator | ||
| +// never fail for a query the parser accepted. | ||
| + | ||
| +import type { Option } from "@tsus/runtime"; | ||
| + | ||
| +export type Target = "issues" | "pulls"; | ||
| + | ||
| +export type QueryErrorCode = | ||
| + | "too_long" | "too_many_terms" | "unterminated_quote" | "unexpected_quote" | ||
| + | "unknown_qualifier" | "unsupported_qualifier" | "empty_value" | "negation_not_allowed" | ||
| + | "bad_value" | "bad_date" | "conflict"; | ||
| + | ||
| +// start/end: UTF-16 offsets into the input, end exclusive. | ||
| +export type QueryError = { readonly code: QueryErrorCode; readonly message: string; readonly start: number; readonly end: number }; | ||
| + | ||
| +// What parseQuery returns for a query with errors: every error, in input order. | ||
| +export type ParseFailure = { readonly tag: "Invalid"; readonly errors: readonly QueryError[] }; | ||
| + | ||
| +// A person a term names: the viewer (@me), a user id, or an agent of the repository's organization. | ||
| +export type UserRef = | ||
| + | { readonly tag: "Me" } | ||
| + | { readonly tag: "User"; readonly id: string } | ||
| + | { readonly tag: "Agent"; readonly slug: string }; | ||
| + | ||
| +export type StateValue = "open" | "closed" | "merged" | "draft"; | ||
| +export type ListState = "open" | "closed" | "merged"; | ||
| + | ||
| +export type SortValue = "created-desc" | "created-asc" | "updated-desc" | "updated-asc" | "comments-desc" | "comments-asc"; | ||
| + | ||
| +export type DateOp = "<" | "<=" | ">" | ">="; | ||
| +export type Unit = "h" | "d" | "w"; | ||
| + | ||
| +// A UTC calendar day: its text as written (YYYY-MM-DD) and its first millisecond. | ||
| +export type Day = { readonly text: string; readonly start: number }; | ||
| + | ||
| +export type DateFilter = | ||
| + | { readonly tag: "On"; readonly day: Day } | ||
| + | { readonly tag: "Compare"; readonly op: DateOp; readonly day: Day } | ||
| + | { readonly tag: "Between"; readonly from: Option<Day>; readonly to: Option<Day> } | ||
| + | { readonly tag: "Age"; readonly op: DateOp; readonly count: number; readonly unit: Unit }; | ||
| + | ||
| +export type DateField = "created" | "updated"; | ||
| + | ||
| +export type Term = | ||
| + | { readonly tag: "Is"; readonly negated: boolean; readonly state: StateValue } | ||
| + | { readonly tag: "Label"; readonly negated: boolean; readonly label: string } | ||
| + | { readonly tag: "NoLabel" } | ||
| + | { readonly tag: "Author"; readonly negated: boolean; readonly user: UserRef } | ||
| + | { readonly tag: "ReviewRequested"; readonly negated: boolean; readonly user: UserRef } | ||
| + | { readonly tag: "Sort"; readonly sort: SortValue } | ||
| + | { readonly tag: "Date"; readonly field: DateField; readonly filter: DateFilter } | ||
| + | { readonly tag: "Number"; readonly number: number } | ||
| + | { readonly tag: "Text"; readonly negated: boolean; readonly text: string }; | ||
| + | ||
| +export type SearchQuery = { readonly target: Target; readonly terms: readonly Term[] }; | ||
| + | ||
| +export const LIMITS = { length: 1024, terms: 32 } as const; | ||
Addedbuild-b-tsus/apps/web/src/search/edit.tsus+55−0
| @@ -0,0 +1,55 @@ | ||
| +// Changes the list pages make to a parsed query for their links: the state tabs (Open, Closed, | ||
| +// Merged, All) replace the query's is:open/closed/merged terms, and a label chip adds label:x. | ||
| + | ||
| +import { Some, None, type Option } from "@tsus/runtime"; | ||
| +import type { ListState, SearchQuery, StateValue, Term } from "./ast.tsus"; | ||
| +import { serializeTerm } from "./serialize.tsus"; | ||
| +import { labelValue } from "./values.tsus"; | ||
| + | ||
| +const listStateOf = (s: StateValue): Option<ListState> => | ||
| + match (s) { | ||
| + "open" => Some<ListState>("open"), | ||
| + "closed" => Some<ListState>("closed"), | ||
| + "merged" => Some<ListState>("merged"), | ||
| + "draft" => None, | ||
| + }; | ||
| + | ||
| +// The is:open/closed/merged term t is, with whether it is negated. | ||
| +type StateTerm = { readonly state: ListState; readonly negated: boolean }; | ||
| + | ||
| +const stateTerm = (t: Term): Option<StateTerm> => | ||
| + match (t) { | ||
| + Is(x) => listStateOf(x.state).map((state) => ({ state, negated: x.negated })), | ||
| + _ => None, | ||
| + }; | ||
| + | ||
| +// The tab a query is on: its one is:open, is:closed or is:merged, when it has exactly one and no | ||
| +// negated one. None: All, or a mix no tab stands for. | ||
| +export const tabOf = (q: SearchQuery): Option<ListState> => { | ||
| + const states: StateTerm[] = []; | ||
| + for (const t of q.terms) { | ||
| + const s = stateTerm(t); | ||
| + if (s.isSome()) states.push(s.value); | ||
| + } | ||
| + const only = states.length === 1 ? states.slice(0, 1) : []; | ||
| + for (const s of only) if (!s.negated) return Some(s.state); | ||
| + return None; | ||
| +}; | ||
| + | ||
| +// The query with its state terms replaced by is:<state> in front (or removed, for None). | ||
| +export const withState = (q: SearchQuery, state: Option<ListState>): SearchQuery => { | ||
| + const rest = q.terms.filter((t) => stateTerm(t).isNone()); | ||
| + const lead: readonly Term[] = state.isSome() ? [{ tag: "Is", negated: false, state: state.value }] : []; | ||
| + return { target: q.target, terms: [...lead, ...rest] }; | ||
| +}; | ||
| + | ||
| +// The query with label:<label> added at the end, unless it has it already. None for a name that is | ||
| +// no label (or on pulls, which have none). | ||
| +export const withLabel = (q: SearchQuery, label: string): Option<SearchQuery> => { | ||
| + if (q.target !== "issues") return None; | ||
| + return labelValue(label).map((l): SearchQuery => { | ||
| + const t: Term = { tag: "Label", negated: false, label: l }; | ||
| + const have = q.terms.some((x) => serializeTerm(x) === serializeTerm(t)); | ||
| + return have ? q : { target: q.target, terms: [...q.terms, t] }; | ||
| + }); | ||
| +}; | ||
Addedbuild-b-tsus/apps/web/src/search/lex.tsus+104−0
| @@ -0,0 +1,104 @@ | ||
| +// Splitting a query into its terms, as written: where each one starts and ends, whether it is | ||
| +// negated, and whether it is bare text, a quoted phrase or a qualifier with a quoted value. Nothing | ||
| +// is interpreted here (parse.tsus decides what a term means); a term whose quotes are wrong comes | ||
| +// back as Broken with its span. | ||
| + | ||
| +import { Some, None, type Option } from "@tsus/runtime"; | ||
| + | ||
| +export type Raw = | ||
| + // bare text up to the next whitespace, without the leading "-" of a negated term | ||
| + | { readonly tag: "Word"; readonly start: number; readonly end: number; readonly negated: boolean; readonly text: string } | ||
| + // a quoted value standing alone: free text | ||
| + | { readonly tag: "Phrase"; readonly start: number; readonly end: number; readonly negated: boolean; readonly text: string } | ||
| + // key:"quoted value"; prefix is the key as written, without its colon | ||
| + | { readonly tag: "Keyed"; readonly start: number; readonly end: number; readonly negated: boolean; readonly prefix: string; readonly text: string } | ||
| + | { readonly tag: "Broken"; readonly start: number; readonly end: number; readonly code: "unterminated_quote" | "unexpected_quote" }; | ||
| + | ||
| +const WS = /\s/; | ||
| +const KEY_COLON = /^[A-Za-z]+(-[A-Za-z]+)*:$/; | ||
| + | ||
| +const isWs = (c: string): boolean => c !== "" && WS.test(c); | ||
| + | ||
| +// The first index at or after `from` that is whitespace, or the end of the input. | ||
| +const nextWs = (s: string, from: number): number => { | ||
| + let i = from; | ||
| + while (i < s.length && !isWs(s.charAt(i))) i += 1; | ||
| + return i; | ||
| +}; | ||
| + | ||
| +// The first index at or after `from` that is whitespace or a double quote, or the end. | ||
| +const nextStop = (s: string, from: number): number => { | ||
| + let i = from; | ||
| + while (i < s.length && !isWs(s.charAt(i)) && s.charAt(i) !== "\"") i += 1; | ||
| + return i; | ||
| +}; | ||
| + | ||
| +type Quoted = { readonly text: string; readonly next: number }; | ||
| + | ||
| +// A quoted value whose opening quote is just before `from`: \" is a quote, \\ a backslash, any other | ||
| +// backslash stays. None when no unescaped quote closes it. | ||
| +const readQuoted = (s: string, from: number): Option<Quoted> => { | ||
| + let i = from; | ||
| + let text = ""; | ||
| + while (i < s.length) { | ||
| + const c = s.charAt(i); | ||
| + if (c === "\"") return Some({ text, next: i + 1 }); | ||
| + const d = s.charAt(i + 1); | ||
| + if (c === "\\" && (d === "\"" || d === "\\")) { | ||
| + text += d; | ||
| + i += 2; | ||
| + } else { | ||
| + text += c; | ||
| + i += 1; | ||
| + } | ||
| + } | ||
| + return None; | ||
| +}; | ||
| + | ||
| +type Step = { readonly raw: Raw; readonly next: number }; | ||
| + | ||
| +const broken = (start: number, end: number, code: "unterminated_quote" | "unexpected_quote"): Step => | ||
| + ({ raw: { tag: "Broken", start, end, code }, next: end }); | ||
| + | ||
| +// After a closing quote the term must end; otherwise the whole term, to the next whitespace, is an | ||
| +// unexpected quote. | ||
| +const closed = (s: string, start: number, q: Quoted, make: (text: string, end: number) => Raw): Step => | ||
| + q.next < s.length && !isWs(s.charAt(q.next)) | ||
| + ? broken(start, nextWs(s, q.next), "unexpected_quote") | ||
| + : { raw: make(q.text, q.next), next: q.next }; | ||
| + | ||
| +const quoted = (s: string, start: number, from: number, make: (text: string, end: number) => Raw): Step => | ||
| + match (readQuoted(s, from)) { | ||
| + Some(q) => closed(s, start, q, make), | ||
| + None => broken(start, s.length, "unterminated_quote"), | ||
| + }; | ||
| + | ||
| +const term = (s: string, start: number): Step => { | ||
| + const negated = s.charAt(start) === "-" && start + 1 < s.length && !isWs(s.charAt(start + 1)); | ||
| + const body = negated ? start + 1 : start; | ||
| + if (s.charAt(body) === "\"") { | ||
| + return quoted(s, start, body + 1, (text, end): Raw => ({ tag: "Phrase", start, end, negated, text })); | ||
| + } | ||
| + const stop = nextStop(s, body); | ||
| + if (s.charAt(stop) !== "\"") return { raw: { tag: "Word", start, end: stop, negated, text: s.slice(body, stop) }, next: stop }; | ||
| + const prefix = s.slice(body, stop); | ||
| + if (!KEY_COLON.test(prefix)) return broken(start, nextWs(s, stop), "unexpected_quote"); | ||
| + return quoted(s, start, stop + 1, (text, end): Raw => ({ tag: "Keyed", start, end, negated, prefix: prefix.slice(0, -1), text })); | ||
| +}; | ||
| + | ||
| +// Every term of the input, in order. An unterminated quote runs to the end of the input, so it is | ||
| +// always the last term. | ||
| +export const lex = (s: string): readonly Raw[] => { | ||
| + const out: Raw[] = []; | ||
| + let i = 0; | ||
| + while (i < s.length) { | ||
| + if (isWs(s.charAt(i))) { | ||
| + i += 1; | ||
| + } else { | ||
| + const step = term(s, i); | ||
| + out.push(step.raw); | ||
| + i = step.next; | ||
| + } | ||
| + } | ||
| + return out; | ||
| +}; | ||
Addedbuild-b-tsus/apps/web/src/search/lists.ts+140−0
| @@ -0,0 +1,140 @@ | ||
| +// Running a list search (api.ts) against D1 for the Issues and Pull requests pages: one page of rows | ||
| +// in the query's order, and the counts the state tabs show (the query with its state terms removed, | ||
| +// by state). Issues come from `issues`; pull requests from `pull_index`, the RepoDO's D1 copy, which | ||
| +// may lag a few seconds; each row's details (comments, the author's kind) then come from the RepoDO, | ||
| +// which stays the source of truth, falling back to what pull_index has. | ||
| +// | ||
| +// Pages are numbered (?page=): an order other than by number has no cursor that a number could be. | ||
| + | ||
| +import { log } from "@duragit/shared/log.ts"; | ||
| +import type { PrState } from "@duragit/shared/pulls.ts"; | ||
| +import type { PrListItem } from "../do/repo/pulls.ts"; | ||
| +import type { WebEnv } from "../env.ts"; | ||
| +import { viewOf, type IssueRow, type IssueView } from "../issues/service.ts"; | ||
| +import { repoStub } from "../repo/do-client.ts"; | ||
| +import type { PullListRow } from "../ui/pulls.ts"; | ||
| +import { searchHref, type Pager } from "../ui/search.ts"; | ||
| +import { serializeQuery, tabOf, toSql, withState, type ListState, type SearchQuery, type SqlCtx, type Target } from "./api.ts"; | ||
| + | ||
| +export const PAGE_SIZE = 25; | ||
| +export const MAX_PAGE = 400; | ||
| + | ||
| +export function pageOf(raw: string | null): number { | ||
| + const n = Number(raw ?? ""); | ||
| + return Number.isSafeInteger(n) && n >= 1 ? Math.min(n, MAX_PAGE) : 1; | ||
| +} | ||
| + | ||
| +// What the list searches for: ?q= when the URL has it (an empty q is the empty query: everything), | ||
| +// otherwise the default is:open, with the old links' state= and label= turned into terms. | ||
| +export function inputOf(p: URLSearchParams, target: Target): string { | ||
| + const q = p.get("q"); | ||
| + if (q !== null) return q; | ||
| + const s = p.get("state"); | ||
| + const terms: string[] = []; | ||
| + if (s === "closed" || (s === "merged" && target === "pulls")) terms.push(`is:${s}`); | ||
| + else if (s !== "all") terms.push("is:open"); | ||
| + const label = p.get("label"); | ||
| + if (target === "issues" && label) terms.push(`label:"${label.replace(/[\\"]/g, (c) => `\\${c}`)}"`); | ||
| + return terms.join(" "); | ||
| +} | ||
| + | ||
| +// The links a list page makes from its query, all with canonical q values. | ||
| +export type SearchLinks = { | ||
| + canonical: string; | ||
| + // the tab the query is on: one is: state, "all" (no state term), or null (a mix no tab stands for) | ||
| + tab: ListState | "all" | null; | ||
| + // the query has terms besides its state | ||
| + filtered: boolean; | ||
| + tabHref: (state: ListState | null) => string; | ||
| + pages: Pager; | ||
| + // the default list, when the query is something else | ||
| + clear: string | null; | ||
| +}; | ||
| + | ||
| +export const DEFAULT_QUERY = "is:open"; | ||
| + | ||
| +export function linksOf(base: string, query: SearchQuery, n: number, more: boolean): SearchLinks { | ||
| + const canonical = serializeQuery(query); | ||
| + const stateless = serializeQuery(withState(query, null)); | ||
| + const tab = tabOf(query); | ||
| + return { | ||
| + canonical, | ||
| + tab: tab ?? (stateless === canonical ? "all" : null), | ||
| + filtered: stateless !== "", | ||
| + tabHref: (s) => searchHref(base, serializeQuery(withState(query, s))), | ||
| + pages: { prev: n > 1 ? searchHref(base, canonical, n - 1) : null, next: more ? searchHref(base, canonical, n + 1) : null }, | ||
| + clear: canonical === DEFAULT_QUERY ? null : searchHref(base, DEFAULT_QUERY), | ||
| + }; | ||
| +} | ||
| + | ||
| +// The links of a page whose query has errors: the tabs and Clear lead to plain state queries. | ||
| +export function linksOfInvalid(base: string): SearchLinks { | ||
| + return { | ||
| + canonical: "", tab: null, filtered: true, tabHref: (s) => searchHref(base, s ? `is:${s}` : ""), pages: { prev: null, next: null }, clear: searchHref(base, DEFAULT_QUERY), | ||
| + }; | ||
| +} | ||
| + | ||
| +export type ListPage<T> = { items: T[]; more: boolean }; | ||
| + | ||
| +async function page<R>(env: Pick<WebEnv, "DB">, table: "issues" | "pull_index", cols: string, query: SearchQuery, ctx: SqlCtx, n: number): Promise<ListPage<R>> { | ||
| + const s = toSql(query, ctx); | ||
| + const rows = await env.DB.prepare(`SELECT ${cols} FROM ${table} WHERE ${s.where} ORDER BY ${s.orderBy} LIMIT ? OFFSET ?`) | ||
| + .bind(...s.params, PAGE_SIZE + 1, (n - 1) * PAGE_SIZE).all<R>(); | ||
| + const list = rows.results ?? []; | ||
| + return { items: list.slice(0, PAGE_SIZE), more: list.length > PAGE_SIZE }; | ||
| +} | ||
| + | ||
| +async function countsOf(env: Pick<WebEnv, "DB">, table: "issues" | "pull_index", query: SearchQuery, ctx: SqlCtx): Promise<Map<string, number>> { | ||
| + const s = toSql(withState(query, null), ctx); | ||
| + const rows = await env.DB.prepare(`SELECT state, COUNT(*) AS n FROM ${table} WHERE ${s.where} GROUP BY state`).bind(...s.params).all<{ state: string; n: number }>(); | ||
| + return new Map((rows.results ?? []).map((r) => [r.state, r.n])); | ||
| +} | ||
| + | ||
| +const ISSUE_COLS = "repo_id, number, org_id, author_id, title, body, state, labels, comments, created_at, updated_at, closed_at, closed_by"; | ||
| + | ||
| +export async function searchIssues(env: Pick<WebEnv, "DB">, query: SearchQuery, ctx: SqlCtx, n: number): Promise<ListPage<IssueView> & { counts: { open: number; closed: number } }> { | ||
| + const [rows, counts] = await Promise.all([page<IssueRow>(env, "issues", ISSUE_COLS, query, ctx, n), countsOf(env, "issues", query, ctx)]); | ||
| + return { items: rows.items.map(viewOf), more: rows.more, counts: { open: counts.get("open") ?? 0, closed: counts.get("closed") ?? 0 } }; | ||
| +} | ||
| + | ||
| +type PullIndexRow = { | ||
| + number: number; author_id: string; owner_id: string | null; state: string; draft: number; title: string; base_ref: string; head_ref: string; | ||
| + created_at: number; updated_at: number; merged_at: number | null; closed_at: number | null; | ||
| +}; | ||
| + | ||
| +const PULL_COLS = "number, author_id, owner_id, state, draft, title, base_ref, head_ref, created_at, updated_at, merged_at, closed_at"; | ||
| + | ||
| + | ||
| +function stateOf(s: string): PrState { | ||
| + return s === "merged" || s === "closed" ? s : "open"; | ||
| +} | ||
| + | ||
| +function rowOf(r: PullIndexRow, live: PrListItem | undefined): PullListRow { | ||
| + if (live) { | ||
| + return { | ||
| + number: live.number, title: live.title, state: live.state, draft: live.draft, authorId: live.authorId, ownerId: live.ownerId, baseRef: live.baseRef, headRef: live.headRef, | ||
| + createdAt: live.createdAt, mergedAt: live.mergedAt, closedAt: live.closedAt, authorKind: live.authorKind, comments: live.comments, | ||
| + }; | ||
| + } | ||
| + return { | ||
| + number: r.number, title: r.title, state: stateOf(r.state), draft: r.draft === 1, authorId: r.author_id, ownerId: r.owner_id, baseRef: r.base_ref, headRef: r.head_ref, | ||
| + createdAt: r.created_at, mergedAt: r.merged_at, closedAt: r.closed_at, authorKind: r.owner_id ? "agent" : "user", comments: 0, | ||
| + }; | ||
| +} | ||
| + | ||
| +export async function searchPulls(env: Pick<WebEnv, "DB" | "REPO">, query: SearchQuery, ctx: SqlCtx, n: number): Promise<ListPage<PullListRow> & { counts: Record<PrState, number> }> { | ||
| + const [rows, counts] = await Promise.all([page<PullIndexRow>(env, "pull_index", PULL_COLS, query, ctx, n), countsOf(env, "pull_index", query, ctx)]); | ||
| + let live = new Map<number, PrListItem>(); | ||
| + if (rows.items.length) { | ||
| + try { | ||
| + const got = await repoStub(env, ctx.repoId).prList({ state: "all", numbers: rows.items.map((r) => r.number), limit: 100 }); | ||
| + live = new Map(got.items.map((x) => [x.number, x])); | ||
| + } catch (e) { | ||
| + log("search.pulls_hydrate_failed", { error: e }, "warn"); | ||
| + } | ||
| + } | ||
| + return { | ||
| + items: rows.items.map((r) => rowOf(r, live.get(r.number))), more: rows.more, | ||
| + counts: { open: counts.get("open") ?? 0, closed: counts.get("closed") ?? 0, merged: counts.get("merged") ?? 0 }, | ||
| + }; | ||
| +} | ||
Addedbuild-b-tsus/apps/web/src/search/parse.tsus+223−0
This diff has 224 lines, more than this page shows. View the file or the raw file.
Addedbuild-b-tsus/apps/web/src/search/serialize.tsus+55−0
| @@ -0,0 +1,55 @@ | ||
| +// The canonical form of a query: what the page puts in ?q= and shows in the search box. Parsing the | ||
| +// canonical form gives the same query back, so a URL round-trips. | ||
| + | ||
| +import type { DateFilter, Day, SearchQuery, Term, UserRef } from "./ast.tsus"; | ||
| +import type { Option } from "@tsus/runtime"; | ||
| + | ||
| +const BARE = /^[^\s"]+$/; | ||
| +const QUALIFIER = /^[A-Za-z]+(-[A-Za-z]+)*:/; | ||
| +const NUMBER = /^#[1-9][0-9]{0,8}$/; | ||
| + | ||
| +export const quote = (v: string): string => `"${v.replace(/[\\"]/g, (c) => `\\${c}`)}"`; | ||
| + | ||
| +const qualifierValue = (v: string): string => (BARE.test(v) ? v : quote(v)); | ||
| + | ||
| +// Free text is bare only when reading it back gives free text again. | ||
| +const textValue = (v: string): string => | ||
| + BARE.test(v) && !v.startsWith("-") && !QUALIFIER.test(v) && !NUMBER.test(v) ? v : quote(v); | ||
| + | ||
| +const sign = (negated: boolean): string => (negated ? "-" : ""); | ||
| + | ||
| +const user = (u: UserRef): string => | ||
| + match (u) { | ||
| + Me(_m) => "@me", | ||
| + User(x) => x.id, | ||
| + Agent(a) => a.slug, | ||
| + }; | ||
| + | ||
| +const dayText = (d: Option<Day>): string => | ||
| + match (d) { | ||
| + Some(x) => x.text, | ||
| + None => "*", | ||
| + }; | ||
| + | ||
| +const date = (f: DateFilter): string => | ||
| + match (f) { | ||
| + On(x) => x.day.text, | ||
| + Compare(x) => `${x.op}${x.day.text}`, | ||
| + Between(x) => `${dayText(x.from)}..${dayText(x.to)}`, | ||
| + Age(x) => `${x.op}${x.count}${x.unit}`, | ||
| + }; | ||
| + | ||
| +export const serializeTerm = (t: Term): string => | ||
| + match (t) { | ||
| + Is(x) => `${sign(x.negated)}is:${x.state}`, | ||
| + Label(x) => `${sign(x.negated)}label:${qualifierValue(x.label)}`, | ||
| + NoLabel(_x) => "no:label", | ||
| + Author(x) => `${sign(x.negated)}author:${qualifierValue(user(x.user))}`, | ||
| + ReviewRequested(x) => `${sign(x.negated)}review-requested:${qualifierValue(user(x.user))}`, | ||
| + Sort(x) => `sort:${x.sort}`, | ||
| + Date(x) => `${x.field}:${qualifierValue(date(x.filter))}`, | ||
| + Number(x) => `#${x.number}`, | ||
| + Text(x) => `${sign(x.negated)}${textValue(x.text)}`, | ||
| + }; | ||
| + | ||
| +export const serializeQuery = (q: SearchQuery): string => q.terms.map(serializeTerm).join(" "); | ||
Addedbuild-b-tsus/apps/web/src/search/sql.tsus+200−0
This diff has 201 lines, more than this page shows. View the file or the raw file.
Addedbuild-b-tsus/apps/web/src/search/values.tsus+143−0
This diff has 144 lines, more than this page shows. View the file or the raw file.
Addedbuild-b-tsus/apps/web/src/ui/search.ts+133−0
This diff has 134 lines, more than this page shows. View the file or the raw file.
Addedbuild-b-tsus/test/unit/search/pages.test.ts+124−0
This diff has 125 lines, more than this page shows. View the file or the raw file.
Addedbuild-b-tsus/test/unit/search/parse.test.ts+184−0
This diff has 185 lines, more than this page shows. View the file or the raw file.
Addedbuild-b-tsus/test/unit/search/sql.test.ts+257−0
This diff has 258 lines, more than this page shows. View the file or the raw file.
Addedhidden-tests/dates.test.mjs+107−0
This diff has 108 lines, more than this page shows. View the file or the raw file.
Addedhidden-tests/helpers.mjs+212−0
This diff has 213 lines, more than this page shows. View the file or the raw file.
Addedhidden-tests/parse.test.mjs+239−0
This diff has 240 lines, more than this page shows. View the file or the raw file.
Addedhidden-tests/permission.test.mjs+103−0
This diff has 104 lines, more than this page shows. View the file or the raw file.
Addedhidden-tests/results/ts.out+164−0
This diff has 165 lines, more than this page shows. View the file or the raw file.
Addedhidden-tests/results/tsus.out+164−0
This diff has 165 lines, more than this page shows. View the file or the raw file.
Addedhidden-tests/run.sh+61−0
This diff has 62 lines, more than this page shows. View the file or the raw file.
Addedhidden-tests/semantics.test.mjs+179−0
This diff has 180 lines, more than this page shows. View the file or the raw file.
Addedhidden-tests/serialize.test.mjs+96−0
This diff has 97 lines, more than this page shows. View the file or the raw file.
Addedhidden-tests/sql-safety.test.mjs+114−0
This diff has 115 lines, more than this page shows. View the file or the raw file.
Addedjudge/JUDGE.md+287−0
This diff has 288 lines, more than this page shows. View the file or the raw file.
Addedjudge/probes/probe.mjs+189−0
This diff has 190 lines, more than this page shows. View the file or the raw file.
Addedjudge/probes/probe.out+82−0
This diff has 83 lines, more than this page shows. View the file or the raw file.
Addedjudge/probes/render.mjs+137−0
This diff has 138 lines, more than this page shows. View the file or the raw file.
Addedjudge/probes/shots.mjs+56−0
This diff has 57 lines, more than this page shows. View the file or the raw file.
Addedjudge/probes/show.mjs+21−0
This diff has 22 lines, more than this page shows. View the file or the raw file.
Addedjudge/shots/A-issues-invalid-1280-light.png
Binary file, not shown. View the file or the raw file.
Addedjudge/shots/A-issues-query-1280-light.png
Binary file, not shown. View the file or the raw file.
Addedjudge/shots/A-pulls-default-help-390-dark.png
Binary file, not shown. View the file or the raw file.
Addedjudge/shots/B-issues-invalid-1280-light.png
Binary file, not shown. View the file or the raw file.
Addedjudge/shots/B-issues-query-1280-light.png
Binary file, not shown. View the file or the raw file.
Addedjudge/shots/B-pulls-default-help-390-dark.png
Binary file, not shown. View the file or the raw file.
Addedusage.json+16−0
| @@ -0,0 +1,16 @@ | ||
| +{ | ||
| + "tsus": { | ||
| + "tokens": 290102, | ||
| + "toolCalls": 150, | ||
| + "ms": 1507017, | ||
| + "branch": "worktree-agent-a66151373793240e4", | ||
| + "commit": "a69ca436" | ||
| + }, | ||
| + "ts": { | ||
| + "tokens": 277747, | ||
| + "toolCalls": 117, | ||
| + "ms": 1555957, | ||
| + "branch": "worktree-agent-a7895120081998e14", | ||
| + "commit": "40d9e6fd" | ||
| + } | ||
| +} | ||
| \ No newline at end of file | ||
Checks
No checks reported for this commit.
Runs
No runs for this commit.