Skip to content

feat(agents): serve agent init from a declaration, not hand-written routes - #2491

Merged
kovtcharov-amd merged 7 commits into
claudia/task-a140d608from
claudia/task-1393ee59
Jul 27, 2026
Merged

kovtcharov-amd merged 7 commits into
claudia/task-a140d608from
claudia/task-1393ee59

Conversation

@kovtcharov

@kovtcharov kovtcharov commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Only one agent in the repo could tell you whether it was actually ready to work. The email sidecar hand-wrote GET/POST /v1/email/init; the other 18 hub agents had nothing, so a consumer could see the process was up but had no way to learn the model it needs was never downloaded — the failure surfaced later, mid-task, as a confusing error. Now an agent declares what it needs (a model id, a minimum backend version) and AgentServer serves both verbs for it. The response is byte-compatible with email's existing endpoint, so the TUI preflight gate parses both without a branch and email keeps serving unchanged until its swap-over lands separately.

The boundary is enforced, not just documented: inherited init makes this agent's requirements ready against a backend that is already running. Installing the backend stays with gaia init — an agent process cannot bootstrap the server it depends on, so an unreachable backend fails loudly and names whose job the fix is instead of hanging or half-succeeding.

The failure this is designed against

An agent that declares nothing gets no /init at all — not a cheerful one.

That is the whole design rule, and it is worth stating because the cheerful version is the one that ships and then wastes somebody's afternoon. A readiness endpoint built from an empty declaration would answer ready: true the moment Lemonade happened to be up, having verified nothing about the agent — and a consumer would believe it. So the routes are only mounted when the agent actually declared something, whether it passed an empty declaration object or a manifest with no models:.

This is the same shape as several bugs already found in this project: something reports ready, and only fails when the user tries to use it. A mailbox row green over dead credentials; a transport reporting "Connected" before executable file not found; a control file naming a dead pid. A green light that was never wired to anything is worse than a red one, because it costs a debugging session to disbelieve.

Three related distinctions the contract preserves for the same reason, each with a different remedy: an unreadable model list is "could not tell", never "missing" (reporting it as missing sends the user to re-download a model they may already have); a backend advertising no version is indeterminate, never a pass; and a version-too-old is reported before a missing model, so nobody downloads gigabytes an old server may then refuse to serve.

Test plan

  • python -m pytest tests/unit/test_agent_readiness.py tests/unit/test_agent_server.py — 75 pass (48 new, 27 pre-existing, no regressions)
  • python util/lint.py --all — exit 0
  • Live against lemond v10.10.0 on port 13305, serving a test agent through AgentServer:
    • GET with model present → 200 {"ready":true,...,"ctx_size":65536}, byte-identical to email's response for the same state
    • GET with backend unreachable → 503, hint names lemonade-server serve and "host prerequisite"
    • POST with model present → 200, correctly no-ops instead of re-pulling, final line ✓
    • POST with an unknown model → committed 200 with final line ✗ Provisioning failed: HTTPError: 400 — proves the final line, not the status, carries the verdict
    • POST with backend unreachable → 503, pulls nothing
  • A test fails the build if the model probe is ever reached on a dead backend, so an installer cannot quietly grow here later

Follow-ups (deliberately not in this PR)

Every way the daemon path can fail — daemon down, token rotated, version
gate, model missing, mailbox not connected — reached the user as a raw HTTP
status, discovered one wall at a time. The gate now reports all of it as
checkable rows with a remedy per failure.

Its own review caught remedies that damaged what they claimed to fix: the
mailbox fixes named commands that OVERWRITE existing scopes, turning
'can read, cannot send' into 'can send, cannot read' — and the row then went
green over a mailbox that was worse off. A relay 503 also decoded as
'Lemonade unreachable', sending users to the wrong component entirely.
The preflight gate was built, tested and merged, but nothing imported it, so
no user could reach it: pressing enter on an installed agent went straight to
chat and the first sign of a missing model or a disconnected mailbox was a
failed turn mid-conversation. The gate now sits on the launch path — the four
generic checks plus the agent's extras run, an all-green screen holds ~800ms
and hands off to the same launch path the hub used to call directly, and a
proved-broken row keeps the user on a screen with a command they can run.

Two decisions worth naming:

The gate runs on EVERY launch and nothing is cached. Every row is a fact with
no expiry notice — the daemon can be killed, its token rotates on restart,
Lemonade can unload the model, a mailbox grant can be revoked from the web — so
a remembered pass would open a chat that cannot answer while claiming it had
been verified. A pass costs one attach plus two relayed GETs.

ConnectMailboxMsg becomes an instruction screen, not a connector flow. The TUI
has no OAuth screen and half of one would fork the flow that owns tokens,
scopes and grants (`gaia connectors`), so the host shows the exact command the
mailbox row already computed — scope union and agent grant included — and
re-checks on `r`. With no provider connected the choice stays the user's: the
Gmail command is copy-pasteable and Outlook points at its one-time Microsoft
setup, because that provider needs an app registration first and the Gmail
scopes are wrong for it.

Subprocess-transport agents are not gated: every precondition is probed through
the daemon relay, so the gate could only answer "not installed" — four wrong
rows with four wrong remedies over a launch that works.

The control snapshot reports the new view so automation can wait on it instead
of racing the render.
… agent

Review found the launch this seam was supposed to make impossible. The gate's
async results are unexported types, so root forwards them blind to whichever
gate is mounted: back out of a slow check on Email, launch a second hub agent,
and Email's report lands on that gate — five rows for the wrong agent, and if
that report is all-green the gate hands off and opens a chat for an agent
nothing ever probed. The report is exported and names its agent, so root now
drops any result that belongs to a gate the user already left.

The regression test reproduces it end to end (it fails with "a stale report
launched chat" without the guard), and replaces a test that could not see the
bug: after esc the view is already the hub, so its guard short-circuited before
the agent id was ever compared.

Also from the review:

The mailbox hand-off gave up the wrong lines when the terminal was too short.
The command sits in the middle of the screen, so trimming by position — from
either end — could keep the paragraph explaining the problem and drop the one
command that fixes it. Lines are now dropped by what they are: explanation
first, then the alternative path, never the command or the way out. Verified at
24, 16, 12 and 10 rows.

`closeGate`'s comment claimed it cancelled everything the screen started. It
does not: preflight.Model.Init has a value receiver, so the first probe's cancel
func dies with the call and only a pointer-receiver initialiser in that package
can fix it. The comment now says so instead of vouching for a guarantee that
isn't there.

Smaller: the fits test measured the chat view for its own "all ready" case and
asserted only a row count both views satisfy by self-truncating, so it could
pass while dropping the content it existed to protect — every case now names
what must survive at 80x24. A reconnect command that names a different provider
than the mailbox it is for is flagged rather than presented under a title that
vouches for it. ctrl+c out of the hand-off no longer leaves the view pointing at
a torn-down model. And the fake transport's start/ensure counters are asserted:
a blocked gate must start nothing, and `f` on a stopped sidecar must start it
exactly once.
The gate was built and merged but nothing imported it, so pressing enter on an
installed agent still went straight to chat and the first sign of a missing
model or a disconnected mailbox was a failed turn mid-conversation. It now sits
on the launch path.

It runs every launch and caches nothing: the daemon can be killed, its token
rotates on restart, Lemonade can unload the model and a grant can be revoked
from the web, so a remembered pass would open a chat that cannot answer while
claiming it had been verified.

Review caught the launch this seam exists to prevent — backing out of a slow
check and launching a second agent let the first agent's all-green report hand
off and open a chat for an agent nothing had probed. Reports now name their
agent and stale ones are dropped.
`gaia tui run <id> --query "…"` is the path that exists for scripts and CI,
and with the local model server down it hung indefinitely printing nothing at
all — no stdout, no stderr, no exit. A CI job wedged until its own timeout with
nothing in the log to say why. It now refuses in about a second, naming the
unmet precondition and the command that fixes it.

Two changes, and the second matters even with the first:

- The one-shot runs the existing headless readiness check before sending
  anything. The interactive gate is deliberately not rendered — a script has
  nobody to press a key — but the blocked report is rendered from the same
  preflight.Report the gate draws, so the two paths cannot drift. It refuses
  only on a proved failure; an indeterminate row is named and the run
  continues, matching the gate. The daemon and sidecar are still started first,
  so a cold machine keeps working the way it did.
- The turn is bounded (15m default, `--timeout`), and RunOneShot honours the
  deadline. A readiness check cannot catch a dependency that accepts the query
  and then goes quiet: the relay's read watchdog is reset by every heartbeat,
  so a wedged sidecar can hold the stream open forever. Events already buffered
  when the deadline fires are still taken, so an answer that lands in the same
  instant is not thrown away.

Only daemon-transport agents are checked — a subprocess agent has no
/v1/<agent>/init route, so the rows could only say "not installed" over a
launch that works. `gaia tui chat --agent <id> --query` gets both changes; it
runs through the same entry point.
`gaia tui run <id> --query` is the scriptable path, and with the local model
server down it hung indefinitely printing nothing at all — a CI job wedged
until its own timeout with an empty log. It now fails in under a second,
naming the unmet row and a runnable remedy on stderr, exit 1, stdout untouched
so redirecting still captures only the answer.

Two parts, because a readiness check alone is not enough: the one-shot runs
the same headless check the interactive gate uses (so the two cannot drift),
and every phase gets a bounded context — a dependency that accepts the
connection and then stalls has something to fail against.
…outes

An agent that needs a model before it can answer anything had to hand-write two
endpoints to say so. Only the email sidecar ever did, so every other agent had
no way to report whether it was ready — a consumer could tell the process was
up, but not that it could actually work.

An agent now declares what it needs — a model id, a minimum backend version —
and AgentServer serves GET/POST /v1/<id>/init for it. GET is a read-only
readiness probe returning 200 ready / 503 not-ready with the same body and an
actionable hint per failure; POST tells an already-running backend to download
the declared model, streaming progress. The response is byte-compatible with
the email sidecar's existing /v1/email/init, so the TUI preflight gate parses
both without a branch.

The boundary is deliberate and enforced: inherited init makes this agent's
requirements ready, against a backend that is already up. Installing the
backend stays with `gaia init` — an agent process cannot bootstrap the server
it depends on, so an unreachable backend fails loudly and names whose job the
fix is rather than hanging or half-succeeding.

Three distinctions the contract preserves, each with a different remedy:

- An unreadable model list is "could not tell", never "missing" — reporting it
  as missing sends the user to re-download a model they may already have.
- A backend advertising no version is indeterminate, never a pass.
- A version too old is reported before a missing model, so the user is not sent
  to download gigabytes an old server may then refuse to serve.

An agent that declares nothing gets no init routes at all, whether it passed an
empty declaration or a manifest with no models: an /init that answers "ready"
without having checked anything is worse than a 404, because a consumer would
believe it.
@github-actions github-actions Bot added documentation Documentation changes devops DevOps/infrastructure changes mcp MCP integration changes cli CLI changes tests Test changes agents agent::email Email agent changes tui Go terminal UI (gaia-tui) daemon Daemon supervisor / sidecar control plane sidecar Agent sidecar contract / harness labels Jul 25, 2026
@kovtcharov
kovtcharov changed the base branch from main to claudia/task-5fcce79e July 25, 2026 12:19

@kovtcharov-amd kovtcharov-amd left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving — serving agent init from the declaration is a faithful, additive generalization of the email sidecar's hand-written /init routes (byte-checked against the real _compute_init_status/_provision_progress), with no parity-loss risk since the email routes stay in place and the swap-over is an explicit follow-up. Fail-loudly is enforced, not just claimed: an empty/missing declaration mounts no route (404), an unreachable backend never probes or pulls, and an indeterminate version is null rather than a false pass. pull_model sends only model_name, avoiding the #1655 cold-cache 400.

Nit: drop the min_lemonade_version NOTE once #2493 lands.

Base automatically changed from claudia/task-5fcce79e to claudia/task-a140d608 July 27, 2026 09:03
@kovtcharov-amd
kovtcharov-amd merged commit 9f3a61e into claudia/task-a140d608 Jul 27, 2026
77 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the claudia/task-1393ee59 branch July 27, 2026 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent::email Email agent changes agents cli CLI changes daemon Daemon supervisor / sidecar control plane devops DevOps/infrastructure changes documentation Documentation changes mcp MCP integration changes sidecar Agent sidecar contract / harness tests Test changes tui Go terminal UI (gaia-tui)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants