Skip to content

fix(tui): scope the memory capability to the agent that has the route - #3988

Merged
itomek merged 2 commits into
mainfrom
tui/scope-memory-capability-to-gaia
Sep 18, 2026
Merged

itomek merged 2 commits into
mainfrom
tui/scope-memory-capability-to-gaia

Conversation

@itomek

@itomek itomek commented Sep 18, 2026

Copy link
Copy Markdown
Collaborator

/memory is currently offered on the email agent and then 404s — the exact advertised-then-refused defect #3978 removed, reintroduced on a different agent by #3987's own fix. This scopes the capability to the agent that actually has the route.

Found by an adversarial review pass whose report arrived after #3987 merged.

Why it slipped through

The gate asked one question: is the peer's contract ≥ 2.13? But every sidecar numbers its own contract independently, so the same number means different things across them — email is already at 2.14 for unrelated reasons and has no memory route at all.

Verified on a live machine, not just read:

$ grep "email sidecar contract" ~/.gaia/host/daemon.log | tail -1
email sidecar contract: apiVersion=2.14 agentVersion=0.6.0

$ grep -c "memory" hub/agents/email/python/gaia_agent_email/server.py   # routes
0

So Supports(CapabilityMemory) returned (true, true) for email, /memory appeared in its palette and /help, and FetchMemory sailed through the contract gate to GET a route that does not exist.

Version answers "is this build new enough", never "does this agent have the route" — both have to hold. The gate now pairs the floor with the agent that owns the memory store, the same way /model is already scoped in modelcmd.go. Asking for memory on an agent that keeps none now says exactly that, instead of telling the user their perfectly current agent is out of date.

🔍 Why no test caught it, and three smaller repairs

The test suite was aimed at the wrong agent. fakeRelay.client() builds an SSEClient named email (sse_test.go), so every memory test was exercising an agent with no memory route and passing anyway, because the gate never looked at the id. Those tests now use the flagship id via a new clientFor(t, id), and the email case is pinned by a test that fails against the shipped gate.

  • Supports claimed to know about capabilities it had never heard of. The default: branch returned (false, true) — a confident "unsupported" — so the next capability added would be hidden by whichever transport was not taught about it, with nothing on screen saying why. That is precisely what the tri-state exists to prevent. Unknown is the honest answer.
  • AgentVersion decoded only version. That is the flagship's spelling; email's /v1/email/version and both agents' top-level /version say agentVersion, so the release version was permanently blank for email and fix(tui): the chat never says which agent is answering — the flagship's name is suppressed as a brand duplicate #3979's live-refinement never fired there. It accepts both spellings now, preferring version.
  • The agents panel matched a late load result by comparing interface values, which panics at runtime if the dynamic type is uncomparable (a HubAgentLister passed by value with a slice or map field). A generation counter does the same job with no such edge.

Also folded in: a gofmt misalignment in chat/model.go that shipped in #3987, and a test pinning the /help Commands block at exactly two lines — renderCommandsSection skips the continuation by position, so a third line would silently leak into every filtered render.

Test plan

  • Mutation-verified — reverting the gate to the shipped version-only form turns the new test red, and restoring it turns it green:
    === with the fix reverted === --- FAIL: TestSupportsMemoryIsScopedToTheAgentThatHasTheRoute capability_test.go:254: an agent with no memory route must not report the memory capability, whatever its contract number === restored === ok github.com/amd/gaia/tui/internal/client 0.007s
  • go build ./..., go vet ./..., gofmt -l ./internal all clean (Go 1.26.3, Linux)
  • go test ./... -count=1 green — 22 packages, 0 failures (-skip TestCtrlVReadsTheClipboard, which reads the machine's real clipboard)
  • New regression tests: email refused despite 2.14; the refusal is not an ErrMemoryContractTooOld; an unknown capability reports unknown on both transports; AgentVersion accepts both wire spellings; an abandoned load cannot overwrite the current list

No Python or docs changes — the sidecar route and its contract are unchanged.

`/memory` was offered on the `email` agent and then 404'd — the exact
advertised-then-refused defect #3978 removed, reintroduced on a different
agent by its own fix.

The gate asked only "is the peer's contract >= 2.13". But every sidecar
numbers its own contract, so the same number means different things across
them: `email` is already at 2.14 for unrelated reasons and has no memory route
at all. Version answers "is this build new enough", never "does this agent have
the route" — both have to hold, so the gate now pairs the floor with the agent
that owns the memory store, the way `/model` is already scoped in
ui/chat/modelcmd.go. Asking for memory on an agent that keeps none says so,
rather than telling the user their current agent is out of date.

The whole memory test suite was running against a relay client named `email`,
which is why nothing caught it; those tests now use the flagship id, and the
`email` case is pinned by a test that fails against the shipped gate.

Three smaller repairs alongside it:

`Supports` answered a confident "known, unsupported" for any capability it had
never heard of, so the next one added would be hidden by whichever transport
was not taught about it, silently. Unknown is the honest answer.

`AgentVersion` decoded only `version`, the flagship's spelling; `email` and both
agents' top-level `/version` say `agentVersion`, so the release version was
permanently blank there. It accepts both.

The agents panel matched a late load result by comparing interface values,
which panics at runtime if the dynamic type is uncomparable. A generation
counter does the same job with no such edge.
@github-actions github-actions Bot added the tui Go terminal UI (gaia-tui) label Sep 18, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Request changes

The main fix is right: memory is a gaia-only route, so gating it on the contract number alone was always going to advertise /memory on email and then 404. Scoping it to the agent that owns the store — and making the refusal say "this agent keeps no memory" instead of "your agent is out of date" — is the correct shape, and the test suite fix (memory tests were aimed at email the whole time) is the real find here.

One of the folded-in repairs regresses something the code it replaced was protecting. Swapping the agents panel's stale-result guard for a plain counter loses the case where a previous panel's abandoned fetch lands on a newly opened one: both start counting from zero, so they match. Close /agents and reopen it while the first list fetch is still in flight, and the fresh panel replaces its list with "Could not list installed agents: context canceled". The old context comparison caught this, and the test that covered it was rewritten into a different scenario — so nothing catches it now. I reproduced it; fix is a few lines (seed each panel's generation from a package-level counter so two panels can never share one).

Smaller: the "this agent keeps no memory" refusal is checked after the sidecar is started and probed, so typing /memory on email spawns a sidecar just to say no — and if that probe fails, the user gets the wrong reason ("could not confirm the contract version") for a situation that has nothing to do with versions. The check needs nothing from the network; move it first.

Real-world evidence

No evidence-bundle.md in this checkout, so nothing was captured by the evidence stage. The PR description carries real proof for the bug itself — the live daemon.log line showing email at apiVersion 2.14, the zero memory routes in its server, and a mutation-verified test that goes red against the shipped gate. I ran the Go suite here myself: go build, go vet, gofmt -l ./internal clean, and go test ./internal/client/... ./internal/ui/{agents,components,chat}/... -count=1 all green. What isn't shown anywhere is the TUI itself — a live session proving /memory no longer appears in email's palette or /help. That's a nudge, not a blocker; the verdict rests on the panel regression below, which I reproduced directly.

🔍 Technical details

🟡 A previous panel's abandoned load can land on a newly opened one (tui/internal/ui/agents/model.go:171)

gen starts at 0 for every Model, and agents.New is called fresh on each /agents (ui/chat/model.go:1558). Sequence: panel A opens (gen 0, Catalog in flight) → Esc calls abandonLoad, cancelling A's context and bumping A's own gen → A's fetch returns loadedMsg{gen: 0, err: context canceled} → user reopens /agents, creating panel B at gen 0 → v.gen != m.gen is false, so the stale error lands.

The replaced guard covered this: panel B holds a brand-new context.Context, so v.ctx != m.ctx dropped it. TestOldPanelResultsCannotAffectNewPanel was pinning exactly that and was rewritten into the same-instance case, which the counter does handle.

Reproduced against this branch (temporary test, since removed):

=== RUN   TestStaleResultFromAPreviousPanelInstance
    zz_tmp_test.go:21: panel A's abandoned load landed on panel B:
    loadErr="Could not list installed agents: context canceled" rows=0
--- FAIL

The uncomparable-interface reasoning for dropping source/ctx is sound — the fix is just to make generations unique across instances rather than per-instance:

// panelGen hands every panel its own generation, so a late result from a
// panel the user already closed cannot match a freshly opened one.
var panelGen atomic.Int64

func New(client HubAgentLister, width, height int) Model {
	ctx, cancel := context.WithCancel(context.Background())
	return Model{ctx: ctx, cancel: cancel, client: client, loading: true,
		width: width, height: height, gen: panelGen.Add(1)}
}

func (m *Model) abandonLoad() {
	m.cancel()
	m.ctx, m.cancel = context.WithCancel(context.Background())
	m.gen = panelGen.Add(1)
}

(with gen int64 on Model and loadedMsg). Worth restoring the cross-instance assertion alongside the new same-instance one — both fit in the existing test.

🟡 The no-memory-store refusal runs after EnsureAgent + negotiate (tui/internal/client/memory.go:159)

/memory is hidden from email's palette now, but the command dispatcher still runs it when typed (ui/chat/model.go:1577 → startMemoryFetch). On that path the agent-id check sits behind the sidecar spawn and the version probe, both of which it doesn't need — and when peer.answered is false the earlier branch wins, telling the user to "check gaia daemon status" about an agent whose contract is irrelevant. Moving the check to the top of FetchMemory, before EnsureAgent, makes the answer immediate and always the true one.

🟢 The refusal points out of the TUI (tui/internal/client/memory.go:165)

The reader is already inside the TUI; #3987 gave them an in-session switcher.

			"the '%s' agent does not keep a memory store — only '%s' does. "+
				"Type /agents to switch, or run `gaia tui status` to see what is installed",

🟢 Third hardcoded "gaia" (tui/internal/client/negotiate.go:293)

memoryAgentID, modelSwitchAgentID (ui/chat/modelcmd.go:29) and catalog.FlagshipID are now the same literal in three places. The client package already imports catalog (client/factory.go), so const memoryAgentID = catalog.FlagshipID keeps the named intent while removing one copy. Fine to leave if you consider "owns the memory store" a fact that could drift from "is the flagship".

Strengths

  • The root-cause framing is exactly right, and the comments say why rather than what: "version answers 'is this build new enough', never 'does this agent have the route'" is the sentence that stops this being reintroduced a third time.
  • Catching that fakeRelay.client() built an email client, so every memory test passed while exercising an agent with no memory route, is the finding that made the bug invisible. Pinning the email case with a test that fails against the shipped gate is the right repair.
  • (false, false) for an unrecognized capability, and accepting both version/agentVersion spellings, are both fixes for real future-silence failures — the second verified against the two sidecars' actual handlers (gaia_agent/server.py:909 vs gaia_agent_email/server.py:219).

Two review findings on the previous commit.

Swapping the agents panel's stale-result guard for a per-panel counter lost the
case the context comparison was catching: both panels start at zero, so a panel
closed while its list fetch was in flight could land that fetch's "context
canceled" on the panel opened next. Generations now come from a process-wide
counter, so two panels can never share one, and the test covers the cross-panel
sequence rather than only the same-instance one.

The "this agent keeps no memory store" refusal sat after EnsureAgent and the
contract probe, so asking for memory on an agent that has none spawned a sidecar
purely to say no — and if that probe then failed, the user was told the version
could not be confirmed, for a situation with no version in it. The check needs
nothing from the network, so it runs first.
@itomek

itomek commented Sep 18, 2026

Copy link
Copy Markdown
Collaborator Author

Both fixed in 16872b9 — and the panel one was a real regression I introduced, not a judgement call. Thanks for reproducing it.

Swapping the context comparison for a per-panel counter looked like a pure cleanup and quietly dropped the cross-panel case: both panels start at zero, so a fetch abandoned by panel A lands on panel B. Generations now come from a process-wide counter, so two panels can never share one.

🔍 Verification, and the second fix

Mutation-verified against the exact failure you described. Reverting the constructor to a per-panel counter turns the new test red with your string, and restoring it turns it green:

=== with the regressed per-panel counter ===
--- FAIL: TestAStalePanelsLoadCannotLandOnTheNextPanel
    model_test.go:275: a closed panel's abandoned load reached the next panel:
    "Could not list installed agents: context canceled"
=== restored ===
ok  	github.com/amd/gaia/tui/internal/ui/agents	0.002s

The test drives the sequence rather than the same-instance case the rewritten one covered: open A → abandon its in-flight load → open B → deliver A's late result, and assert B still shows its own load as in progress.

Worth noting the first mutation attempt passed — my sed pattern missed after gofmt realigned the struct literal, so the mutation never applied. Re-ran it with an anchored regex that asserts it applied before testing.

Second finding also fixed: the "this agent keeps no memory store" check needs nothing from the peer, so it now runs before EnsureAgent and the contract probe. Asking for memory on email no longer spawns a sidecar purely to refuse, and a failed probe can no longer report a version problem for a situation with no version in it. Pinned by TestFetchMemoryOnAnAgentWithoutAStoreCostsNoRoundTrip, which asserts zero /version probes.

Full Go suite green after both (22 packages), gofmt -l ./internal clean, Go 1.26.3 on Linux.

On the live-TUI nudge: fair. The /memory-absent-from-email's-palette frame isn't captured here because reaching an email session needs a mailbox connector on the box, which the isolated drive home didn't have. The path is covered by unit tests at both layers — Supports and FetchMemory — and the mutation above shows they bite.

@itomek
itomek added this pull request to the merge queue Sep 18, 2026
Merged via the queue into main with commit 3af8615 Sep 18, 2026
45 checks passed
@itomek
itomek deleted the tui/scope-memory-capability-to-gaia branch September 18, 2026 05:01
kovtcharov-amd added a commit to kovtcharov/gaia that referenced this pull request Sep 22, 2026
Adding /slack pushed the block to three lines, which the help overlay
cannot render correctly: renderCommandsSection replaces the block by
position — the label line plus exactly one continuation — so the third
line leaked into every filtered render.

Nothing conflicted textually. This branch grew the block while amd#3988
independently landed the test that pins its shape, so the defect only
appears once the two meet. Reflowed to two lines, both within the
panel's 53-column maximum.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tui Go terminal UI (gaia-tui)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant