Skip to content

fix(ui): drop a withheld file's approved flag when its session rebuilds - #8398

Merged
kovtcharov-amd merged 5 commits into
amd:mainfrom
iam-hk:fix/8083-stale-approved-file
Nov 22, 2026
Merged

kovtcharov-amd merged 5 commits into
amd:mainfrom
iam-hk:fix/8083-stale-approved-file

Conversation

@iam-hk

@iam-hk iam-hk commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Summary

The relayed session panel ("Files GAIA can reach") kept listing a file as approved after the agent session that held that approval was gone — eviction, a sidecar restart, or just the chat's underlying session being rebuilt.

Why

_approved_paths in src/gaia/ui/session_environment.py is only cleared by forget_session, which is only called from the chat-delete path in routers/sessions.py. It is never cleared on eviction or a sidecar restart, so a file approved at a prompt kept showing as reachable even after the agent instance that actually held the grant no longer existed.

The existing reclaimed_after_eviction flag on _AgentSession only fires when the in-memory SessionRegistry._evicted_ids recognizes the session_id as a previous eviction — which a sidecar restart (a fresh process, so that dict is empty) can never satisfy, even though a brand-new agent was just built for a session_id the UI still thinks is live.

Linked issue

Closes #8083

Changes

  • hub/agents/gaia/python/gaia_agent/session_registry.py: new freshly_built flag on _AgentSession, true on every path that constructs a fresh agent for a session_id (eviction-reclaim, restart, or first use) — not just the already-covered eviction case.
  • hub/agents/gaia/python/gaia_agent/server.py: the /query handler consumes freshly_built once per turn and merges a session_created: true field onto the turn's terminal SSE event (final or error), regardless of how the turn ended.
  • src/gaia/ui/email_sidecar/relay.py: _dispatch_one takes the chat's session_id and, on seeing session_created, calls session_environment.forget_session(session_id) to drop the chat's stale approved-paths cache.
  • Added regression tests in hub/agents/gaia/python/tests/test_session_registry.py and tests/unit/test_sidecar_relay_fidelity.py.

Test plan

  • pytest hub/agents/gaia/python/tests/test_session_registry.py -v — 28 passed, including 3 new tests for freshly_built (first use, eviction-reclaim, consume-once).
  • pytest tests/unit/test_sidecar_relay_fidelity.py -v — 83 passed, including 3 new tests for the relay's session_created handling (final, no-flag, error terminal).
  • pytest hub/agents/gaia/python/tests/ (full package) — 1548 passed; the 6 failures/errors present are pre-existing in this sandbox (missing gh CLI, insufficient memory to load a local model, and one unrelated tool-registry name drift) and reproduce identically on main with the same install.
  • python util/lint.py --all on the changed files: black, isort, flake8 all clean. The pylint run reports 23 pre-existing errors, none in any file this PR touches.

Evidence

  • [N/A] Agent exposed in the Agent UI — this fix is in the session-rebuild/relay plumbing behind the panel, not a new UI surface. Reproducing it live needs a running sidecar plus a forced eviction or process restart mid-chat, which isn't available in my build setup. The regression tests exercise the exact mechanism directly: a session built fresh after simulated eviction, and the relay's _dispatch_one dropping the chat's approved-paths cache on session_created.
  • [N/A] MCP tools / servers — not touched.
  • [N/A] CLI — not touched.
  • [N/A] HTTP API / REST — the /query SSE contract gains one additional field (session_created) on its terminal event; covered by the server-side test suite, not a new endpoint.

Checklist

  • I have linked a GitHub issue above (Closes #8083).
  • I have described why this change is being made, not just what changed.
  • I have run linting and tests locally.
  • Evidence matched to the surface — marked N/A above with the reason; no live screenshot was available for this plumbing-level fix.
  • No user-visible documentation describes this cache's lifetime, so nothing needed updating.

…ebuilt

The relayed session panel kept listing a file as approved after the
agent that held the approval was gone, because _approved_paths in
session_environment.py was only cleared by forget_session on chat
delete, never on eviction or a sidecar restart.

The sidecar now flags session_created on a turn's terminal event
whenever it built a fresh agent for that session_id -- eviction,
restart, or first use, not just the already-flagged eviction case.
The relay drops the chat's approved-paths cache whenever it sees that
flag, regardless of how the turn ended.

Closes amd#8083
@github-actions github-actions Bot added the tests Test changes label Nov 17, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Request changes

This PR makes the Agent UI forget files a chat approved whenever the gaia sidecar rebuilds that chat's agent, which is the right idea for #8083. As written, though, it clears the list at the end of the turn, so it also wipes approvals the user made during that turn.

  • Approvals from the first turn of every chat (and the first turn after a restart or eviction) vanish from the session panel. The approval is recorded mid-turn, then the turn ends with the "freshly built" flag, and the relay erases it. The agent still holds the grant, so the panel ("Files GAIA can reach") now under-reports what GAIA can open. That's the reverse of the original bug, and it hits a more common path. Clear the list when the turn starts on a rebuilt agent, not when it ends.
  • The new session_created field is undocumented. The agent package's SPEC, SKILL and CHANGELOG list every field of the terminal event against a contract version. This one is missing from all three, with no contract bump.

Real-world evidence

N/A: no evidence bundle was produced for this run. The verdict rests on static review. The session panel and the sidecar restart/eviction paths weren't exercised live, and the PR's own Evidence section marks the Agent UI as N/A.

🔍 Technical details

Issues

🔴 Mid-turn approvals are erased on every freshly built turn (src/gaia/ui/email_sidecar/relay.py:296, hub/agents/gaia/python/gaia_agent/session_registry.py:127)
freshly_built defaults to True, so the first turn of every chat carries session_created. Here is the sequence on that turn:

  1. The agent hits a path prompt, and the user approves it.
  2. routers/chat.py:452 calls note_approved_path(session_id, path).
  3. The sidecar finishes and sends final with session_created: true, which _with_context adds to every terminal type.
  4. _dispatch_one calls forget_session(session_id), which pops the path recorded in step 2.

The sidecar's agent keeps the grant for the rest of the session, but the panel no longer lists it. The same thing happens on the first turn after a restart or eviction, which is the exact case this PR targets.

The new tests miss this because they call _dispatch_one on a bare final and never record an approval during the turn. Two possible fixes:

  • Clear at turn start. relay_query already knows the session_id, so have the sidecar emit the flag on an early, non-terminal event (it already sends context_usage before the answer), and call forget_session the moment that event arrives, before any prompt can be answered.
  • Or snapshot _approved_paths when the turn starts, and on session_created drop only the paths recorded before the turn.

Either way, add a test that records an approval mid-turn on a session_created turn and asserts it survives.

🟡 New SSE field with no contract bump or docs (hub/agents/gaia/python/gaia_agent/server.py:2170)
hub/agents/gaia/npm/SKILL.md:356 lists the full final shape ({ type, answer, usage?, failed?, failure?, context?, … route? }), and each field there is gated by a contract version (latest in CHANGELOG [Unreleased] is 2.30). CLAUDE.md requires README/SPEC/SKILL/CHANGELOG to move together for a contract change. Fix:

  • Add session_created? (on final and error) to SPEC §5.1 and SKILL.md.
  • Bump the contract minor and add a CHANGELOG entry.
  • If you move the flag to an early event, as suggested above, document that event instead.

🟢 First use is indistinguishable from a rebuild. Even after the 🔴 fix, a brand-new chat always triggers forget_session. That's harmless while the list is empty, but the comment in session_registry.py should say first use is deliberately included. Otherwise a later reader may "optimise" it away, or rely on session_created to mean "something was lost".

Strengths

  • The diagnosis is right: reclaimed_after_eviction can never fire after a process restart, because _evicted_ids lives in memory. A flag set at construction covers restart, eviction and first use with one rule.
  • The consume-once handling mirrors the existing reclaimed_after_eviction contract, and the server-side tests pin it.
  • Tagging every terminal type, not just a successful final, is the correct call: the agent was rebuilt whether or not the turn answered.

iam-hk added a commit to iam-hk/iam-hk that referenced this pull request Nov 17, 2026
…rase

The previous commit cleared a chat's whole approved-paths cache whenever
session_created arrived, which also wiped out an approval the user made
during that very turn: note_approved_path runs mid-turn, forget_session
ran at the end, and the two raced on the same turn every time a session
was rebuilt -- including the common case of a chat's very first turn.

Snapshot the chat's approved paths when the turn starts (relay_query
already has the session_id) and, on session_created, drop only what was
in that snapshot. Anything approved during the same turn is approved
after the snapshot, so it survives.

Bumps the sidecar contract to 2.31 and documents session_created on
final/error in the SPEC, SKILL.md and CHANGELOG, which the first commit
left undocumented.

Addresses review feedback on amd#8398.
@github-actions github-actions Bot added the documentation Documentation changes label Nov 17, 2026
@iam-hk

iam-hk commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

Good catch on the mid-turn erasure — you're right, that was a real regression and on the common path too.

Pushed a fix: relay_query now snapshots the chat's approved paths before the streaming loop starts, and session_created only drops what's in that snapshot, not whatever the turn itself just approved. Added a test that records a stale approval, snapshots, approves a new path mid-turn, fires session_created, and asserts only the new path survives (it fails against the old code).

Also bumped the contract to 2.31 and documented session_created on final/error in the SPEC, SKILL.md and CHANGELOG, and clarified the freshly_built comment per your third point.

@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Request changes

Both earlier findings are fixed: approvals made during a rebuilt turn now survive, and session_created is documented under contract 2.31. One case still slips through.

🟡 After a restart or eviction, approving the same file again still wipes it from the panel. The new agent has lost its grant, so it asks again for a file the chat already approved. The user approves it, but the chat's list already holds that path, so nothing new is recorded. The end-of-turn snapshot diff then treats it as stale and drops it. The agent can open the file and the panel doesn't list it. This is probably the most common restart case, because users tend to go back to the file they were working on. Track what this turn approved separately from the start-of-turn list.

🔍 Technical details

src/gaia/ui/session_environment.py:249: note_approved_path skips a path that is already listed, and forget_approved_before (line 273) removes every path in before. So a re-approval of a snapshot path in the same turn gets removed too.

Possible fix: record this turn's approvals in their own per-session set. Clear it when relay_query takes the snapshot, and subtract it from before inside forget_approved_before. Add a test that snapshots ["/a"], approves /a again mid-turn, sends session_created, and asserts /a is still listed.

@kovtcharov-amd kovtcharov-amd added bug Something isn't working p2 low priority agent-ui Agent UI changes security Security-sensitive changes agents sidecar Agent sidecar contract / harness and removed documentation Documentation changes labels Nov 19, 2026
@github-actions github-actions Bot added the documentation Documentation changes label Nov 19, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Request changes

The latest push only merges main, so the problem from the last review is still there.

🟡 If the user approves the same file again after a restart or eviction, it still drops off the panel. The rebuilt agent asks again for a file the chat already approved. The user approves it, but the path is already in the chat's list, so nothing new gets recorded. When the turn ends, the snapshot diff treats the path as stale and removes it. The agent can still open the file, but the panel no longer shows it. Track this turn's approvals separately from the list taken at the start of the turn.

🔍 Technical details
  • src/gaia/ui/session_environment.py: note_approved_path still returns early when the path is already listed. forget_approved_before still removes every path in before.
  • Suggested fix: keep a per-session set of paths approved this turn. Clear it when relay_query takes the snapshot (around relay.py:789), add to it in note_approved_path whether or not the path is new, and subtract it from before in forget_approved_before.
  • Add a test: snapshot ["/a"], approve /a again mid-turn, send session_created, then assert /a is still listed.

@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

I merged current main into the branch so it no longer conflicts; no change to your fix. The one open item is still the last review's: re-approving a file that was already listed (after a restart or eviction) drops it from the panel.

🔍 Technical details
  • main moved the relay from src/gaia/ui/email_sidecar/relay.py to src/gaia/ui/sidecar/relay.py; git carried your change into the new path with no manual edits.
  • Locally on the merge (d12a9915e): test_sidecar_relay_fidelity.py, test_session_registry.py and the relay/session/sidecar unit tests pass (1261 passed). One order-dependent failure in test_sidecar_relay_profiles.py passes on its own and isn't touched by this PR.

@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Request changes

This push only merges main again. The problem from the last two reviews is still unfixed.

🟡 If the user approves the same file again after a restart or eviction, it still drops off the panel. The rebuilt agent asks for a file the chat already approved. The user approves it, but the path is already in the list, so nothing new is recorded. When the turn ends, the cleanup treats the path as stale and removes it. The agent can still open the file, but the panel no longer shows it. Track each turn's approvals apart from the list taken when the turn starts.

🔍 Technical details
  • src/gaia/ui/session_environment.py:262: note_approved_path still skips a path that is already listed. forget_approved_before (line 282) still removes every path in before.
  • Fix: keep a per-session set of paths approved this turn. Reset it where relay_query takes the snapshot (src/gaia/ui/sidecar/relay.py:800), add to it in note_approved_path whether or not the path is new, and subtract it from stale in forget_approved_before.
  • Test: snapshot ["/a"], approve /a again mid-turn, send session_created, then assert /a is still listed.

…the sidecar

The sidecar now reports contract 2.31 for session_created, but the npm
package still declared 2.30, so its contract-constants test failed.
@kovtcharov-amd
kovtcharov-amd merged commit af42dcb into amd:main Nov 22, 2026
50 of 55 checks passed
@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

Thanks @iam-hk — the panel finally tells the truth after a sidecar restart, and spotting that the eviction flag could never fire across a fresh process was a sharp catch. I'll pick up the re-approval edge case in a small follow-up so this didn't have to wait.

kovtcharov-amd added a commit that referenced this pull request Nov 22, 2026
…8816)

After a sidecar restart or eviction, the rebuilt agent asks again for a
file the chat had already approved. Approving it again made the file
disappear from "Files GAIA can reach", even though the agent could still
open it. Now a file approved again during the rebuilt turn stays listed;
files nobody re-approved are still dropped.

Follow-up to #8398 (the last open review point there); refs #8083.

## Test plan

- [x] `python -m pytest tests/unit/test_sidecar_relay_fidelity.py`: the
re-approval tests fail without the fix and pass with it
- [x] `python -m pytest tests/unit/test_sidecar_relay.py
hub/agents/gaia/python/tests/test_session_registry.py` passes
- [x] `python util/lint.py --black --isort --flake8` passes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-ui Agent UI changes agents bug Something isn't working documentation Documentation changes p2 low priority security Security-sensitive changes sidecar Agent sidecar contract / harness tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Files GAIA can reach" keeps listing an approved file after the agent drops the chat's session

2 participants