Repository navigation
test(webui): fail CI when the Agent UI's API types drift from the backend - #4345
Conversation
…kend The Agent UI's TypeScript API types were hand-written against the backend's pydantic models with nothing checking them, and five had already drifted: Settings lacked agent_mode, SystemStatus lacked active_profile, Schedule lacked schedule_config, and Session.mail_provider / FileEntry.extension+modified were typed non-null although the backend sends null. - python -m gaia.ui.export_openapi snapshots the backend's component schemas (built in-process, no server) to src/types/api.schemas.json; a unit test fails when the snapshot is stale. - npm run gen:api-types generates src/types/api.gen.ts from that snapshot with json-schema-to-typescript (pinned); a Vitest test fails when it is stale. openapi-typescript was not usable: it needs the TypeScript 5 JS API, and the Agent UI is on TypeScript 7. - src/types/apiContract.ts makes tsc fail, naming the field, when a hand-written wire type misses a backend field, declares one the backend never sends, or disagrees on its kind. The Agent UI Vitest job now runs tsc. - The drifted types are reconciled; frontend behaviour is unchanged. Part of amd#4246
Approve with suggestionsThis wires a real drift guard between the Agent UI's hand-written API types and the backend's pydantic models — a snapshot of the backend schemas, a generated TypeScript mirror, a compile-time comparison of 18 wire types, staleness checks on both generated artifacts, and a self-test so the guard can't pass vacuously. It's careful work and the five pre-existing drifts it uncovered justify it on their own. One thing worth fixing before merge: the type comparison only catches drift in one direction. It flags a hand-written type that's missing a field or claims a value is never null when the backend does send null — which is what this PR set out to catch. It does not flag the mirror case, where the hand-written type says a value can be null but the backend never sends null. Two fields in this very PR are in that state, so the guard ships with a known blind spot while its own comment claims it catches any kind disagreement. Either make the comparison symmetric (two small type fixes fall out of it, plus a few test fixtures) or narrow the comment to say what it actually checks. Two smaller things: a Windows checkout can make the local staleness check for the generated TypeScript file report a false failure, because the newline handling there isn't normalized the way the test's is; and because the backend's web framework dependencies have no upper version bound, a future release that changes schema output will fail the snapshot test on every unrelated PR until someone regenerates. Both are documented-and-acknowledged territory rather than blockers. Real-world evidenceN/A — no evidence bundle was produced for this PR, and the change touches no user-facing surface: it adds generated types, a schema snapshot, tests, and a CI step, with no runtime behaviour change (the five corrected types are fields no UI code reads yet). The PR description already carries the relevant command output — the new pytest module, 🔍 Technical details🟡 ImportantKind comparison is one-directional; the file's own contract overstates it (
Both are The file already has the
🟢 MinorA CRLF checkout breaks
Unpinned
Nothing checks the hand-written declaration against the module's real exports — renaming Strengths
|
KindMismatch compared kinds with assignability, so it only caught a TS type too NARROW for the wire. A hand-written type whose kind set was a superset passed silently -- SystemStatus.lemonade_url and default_model_name were both declared string | null for fields the backend always sends as str, leaving dead null handling in the UI and a guard whose own header claimed it caught any kind disagreement. Switch to Equals, fix the two types and the fixtures that set them null, and add a self-test that pins BOTH directions so a regression to assignability fails the build rather than quietly reopening the blind spot.
|
Confirmed the blind spot and made the kind check symmetric — the two fields are Both were hand-written as Rather than just fix the two types, I switched I could not run the project's Python side post-merge: 🔍 Technical detailsThe blind spot, demonstrated four ways (all against the real
Backend proof the fields are never null: Fixtures updated: Left alone: the Not addressed (🟢): the |
# Conflicts: # .gitattributes
Searching files in the Agent UI could walk the entire home directory with no limit. On a real machine one search took minutes, and it never returned at all if one directory read blocked, for example on a cloud mount or a macOS privacy prompt. Now a search stops after 20,000 entries or about 10 seconds and returns what it found so far. The file browser says so when a search stopped early. ## Test plan - [x] `python -m pytest tests/integration/test_files_search_budget.py -q`: 5 passed. All 5 fail on `main`. - [x] `python -m pytest tests/integration/test_files_search_budget.py tests/integration/test_files_router.py tests/unit/chat/ui/test_models.py -q`: 111 passed, 1 skipped. On this Mac, `test_files_router.py`'s search tests hung past 10 minutes against the real home dir before this change; each now returns at the ~11 s cap. - [x] `cd src/gaia/apps/webui && npx vitest run`: 400 passed. The new truncation-note test fails without the component change. - [x] `cd src/gaia/apps/webui && npx tsc --noEmit`: clean - [x] black, isort, pylint clean on the changed Python files - [ ] Manual: Agent UI file browser, search a common name on a large home dir, and see the "Search stopped early" note - [ ] `gaia eval agent`: the Agent UI MCP server's `search_files` tool result gains a `truncated` field <details> <summary>🔍 Technical details</summary> - The walk now runs on a dedicated daemon thread instead of the default executor. A deadline checked between entries can't interrupt a blocked `opendir`, so the endpoint also stops waiting at budget + 1 s grace. A stuck thread exits once its read returns, and it never holds a shared executor slot. - The `$HOME` pass skips the four priority folders it already walked at greater depth. - `truncated` is true only when the entry cap or time budget cut the walk short, not when `max_results` was reached. - Overlaps: amd#4345 adds a generated-types drift check. If it merges first, `api.gen.ts` needs regenerating for `FileSearchResponse.truncated`. amd#4348 edits other parts of `routers/files.py` (the upload dir). New tests are in their own file to stay clear of amd#4344/amd#4348's edits to `test_files_router.py`. The docs edit is outside amd#4340's hunks. </details> Fixes amd#4352 Co-authored-by: Kalin Ovtcharov <[email protected]> Co-authored-by: Kalin Ovtcharov <[email protected]>
amd#4354 added `truncated` to FileSearchResponse after this branch last regenerated, so the committed artifacts were stale and this PR's own drift check failed against them.
Merging main's apiGen.test.ts (which opts into @vitest-environment node via #4345) with this branch's scrollIntoView polyfill in setup.ts broke that suite: HTMLElement doesn't exist in the node environment, so the global setup file threw before any test ran. Guard the polyfill so both coexist.
The Agent UI's TypeScript API types are written by hand, and nothing checked them against what the backend actually sends. Five had already drifted: settings were missing the agent mode, system status was missing the active profile, schedules were missing their parsed config, and three fields were typed as never-null although the backend sends null. Now CI fails and names the field whenever a hand-written type and the backend disagree, and those five types are fixed. Nothing changes for users; no UI code read the missing fields yet.
This is the type-generation half of #4246. It also covers #3327's "snapshot the schema" and "check the TS types agree" items, but not its route
response_modeland conformance-test items.Test plan
python -m pytest tests/unit/chat/ui/test_export_openapi.py tests/unit/chat/ui/test_models.py tests/unit/chat/ui/test_server.py tests/unit/test_webui_agent_info_contract.py -q: 170 passedpython -m gaia.ui.export_openapi --checkexits 0, also from another working directory with different envcd src/gaia/apps/webui && npx tsc --noEmit: clean. Withmain'stypes/index.tsrestored it fails five times, namingagent_mode,active_profile,schedule_config,mail_provider,extensioncd src/gaia/apps/webui && npm test: 400 passed. Deleting one line fromapi.gen.tsfails the new generation test andnode scripts/gen-api-types.mjs --checknpm run buildsucceeds🔍 Technical details
The pipeline:
python -m gaia.ui.export_openapiwritessrc/types/api.schemas.jsonfromcreate_app(":memory:").openapi(), thennpm run gen:api-typeswritessrc/types/api.gen.ts. Each step has a stale check: a pytest test for the JSON and a Vitest test for the TS.src/types/apiContract.tsthen compares 18 hand-written wire types with their generated counterparts at compile time. It flags backend fields missing from TS, TS fields never sent, and kind mismatches (string/number/boolean/null/array/object). It also has a self-test so it can't pass vacuously.tscruns in the PyPI build on every PR, and now in the Agent UI Vitest job too.Why not openapi-typescript: it has a peer dependency on
typescript@^5and uses the TS 5 compiler API. The Agent UI is on TypeScript 7, the native port, which has no JS API, so npm refuses the install. Its 6.x line has no TS dependency but pulls a vulnerableundici.[email protected]is pinned exact and adds no newnpm auditfindings (still 6, all pre-existing). The lockfile was written with npm 11 to keep itslibcfields.Only the component schemas are snapshotted, not the full OpenAPI doc. Route docstring edits don't cause regenerations, since the types don't depend on them.
AgentInfo's 14 "TS-only" fields aren't drift. They're Hub catalog fields from
merge_with_registry, andtests/unit/test_webui_agent_info_contract.py(#3846) already checks each one against its emitter. The contract allowlists exactly those 14.Not covered: camelCase view models (
Message,AgentStep,CommandOutput), and TS types whose routes have noresponse_model(tunnel, MCP, connectors). The latter are #3327's remaining work.Things to know:
fastapi/pydantic, as the email agent'sexport_openapi --checkalready does. The committed snapshot matches the current latest (fastapi 0.141.1, pydantic 2.13.5), but a future release that changes schema output would need a regenerate.json-schema-to-typescriptor itsprettierdependency may neednpm run gen:api-typesrun on its PR.Overlap:
SystemStatus.device_supportedfrom the backend and TS. Whichever of the two PRs merges second must rerun the two regenerate commands. The fixture edits touch separate hunks.src/gaia/ui/need the same regenerate after this lands.Part of #4246