Skip to content

fix(live-activity): end a finished run's activity when the app returns to the foreground - #1802

Merged
uzairansaruzi merged 2 commits into
masterfrom
issue/1801-foreground-live-activity
Nov 24, 2026
Merged

uzairansaruzi merged 2 commits into
masterfrom
issue/1801-foreground-live-activity

Conversation

@uzairansaruzi

@uzairansaruzi uzairansaruzi commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Linked issue

Fixes #1801

What changed

If the app sat suspended overnight with the chat closed and the relay's final push was lost, the run's Live Activity kept running after the run ended. #1800 fixed this only for a cold launch.

When the app returns to the foreground, AgentLiveActivityManager.endFinishedRuns now makes the same per-Profile session.active_list check, but it only ends runs the host says are over. It never adopts, retires or ends anything else, because in a running process the manager may be driving another activity. The pass covers the activity the manager holds when no stream is live for it. That one ends through end(status:) only if nothing wrote to it during the read: same lifecycle and update generation, no reattached stream. A leftover the manager doesn't hold is checked again after its relay route is retired.

Review also found a bug in the existing endActivity path. It compared the manager's state against the relay-enriched final state, so the manager didn't reset after ending an activity that had relay tool counts. It now compares against the manager's own state.

How it was tested

  • scripts/test-sim <udid> --only LiveActivityTests, PushRegistrationTests, HermesChatTurnTests, ChatStreamCoordinatorTests, BotChatPresentationTests: 304 passed.
  • Full local suite before the review fixes: 4218 passed, 1 failed. The failure was BotChatPresentationTests.testThreadSearchOpensChronologicalDetailAndItsOwnComposer, which this diff doesn't touch; it passed when re-run on the final code.
  • The new testForegroundEndsOnlyFinishedHermesRunsIncludingTheHeldOne fails if the pass applies anything besides ending a finished run (for example, a bot leftover's .end), or ends the held activity by the wrong path.
  • The guards on the held activity need a real ActivityKit Activity and can't be unit-tested. Live Activities don't run on the simulator, so they still need a check on a phone.

Proved with a focused test that fails without the change, plus a full local suite run.

Reviewed with sol-review: 1 round with gpt-6.2-sol. Fixed S1 (the reset comparison), S2 (the recheck after retiring the route) and S3 (the update-generation guard). Declined S4: a foreground end reads "Response complete" whatever the outcome, the same as cold launch, because session.active_list doesn't carry the outcome and the open chat shows it. Declined S5, a server switch between Profile reads, as a nit.

Checklist

  • Local validation passes using scripts/test-sim <assigned-simulator-udid> with the tests required by AGENTS.md § Verifying; the selection and results are recorded above
  • New/changed Codable models decode tolerantly (optionals for fields the server might add or rename)
  • No new third-party dependencies (the list in AGENTS.md is locked)
  • No invented API endpoints or JSON shapes (verified against upstream source or a running server)

Created with Claude Opus 5.5 in Claude Code (T3 Code), with gpt-6.2-sol (review).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • When the app returns to the foreground, live activities for Hermes runs confirmed as finished are now cleared, including an activity still held by the app.
    • Activities for runs that are still in progress remain active, and unrelated bot activities are unaffected.
    • Activities ended by this check display “Response complete.”

…s to the foreground

A Live Activity whose run ended while the app sat suspended stayed running
until the next cold launch when its last push was lost, because only cold
launch checked the host. Foreground now runs the same per-Profile
session.active_list check but only ends runs the host says are over,
including the activity the manager holds when no stream is live for it.

Fixes #1801

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

When the scene becomes active, the app checks Hermes run state and ends eligible finished Live Activities. The manager rechecks that held activities remain unchanged and disconnected before ending them.

Changes: Foreground Live Activity settlement

Layer / File(s) Summary
Foreground check and completion
HermesMobile/ContentView.swift, HermesMobile/LiveActivities/AgentLiveActivityManager.swift, HermesMobileTests/LiveActivityTests.swift, docs/adr/0070-live-activity-orphan-reconciliation.md
The active-scene task invokes endFinishedRuns. The manager ends activities marked .endComplete and checks ownership and generation conditions before completion. The final activity state preserves the relay count. The test covers held, finished, running, and bot activities. The ADR documents the foreground check and safeguards.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant ContentView
  participant AgentLiveActivityManager
  participant HermesSession
  participant LiveActivity
  ContentView->>AgentLiveActivityManager: Call endFinishedRuns(checking:)
  AgentLiveActivityManager->>HermesSession: Check session.active_list per Profile
  HermesSession-->>AgentLiveActivityManager: Return active run keys
  AgentLiveActivityManager->>LiveActivity: End eligible activity
Loading

Possibly related issues

  • uzairansaruzi/hermex#1723 — Describes finished runs that remain active when a terminal push is lost; this change checks for finished runs when the app returns to the foreground.

Possibly related PRs

  • uzairansaruzi/hermex#1800 — Adds cold-launch reconciliation using session.active_list; this PR adds foreground reconciliation, including the manager-held activity.
Merge Risk: Minimal · up to c0e33

The change ends finished Live Activities when the app returns to the foreground, and it only ends runs the host reports as over. The earlier concern about ending a held activity whose push key changed is fixed. No known merge-blocking risk remains, but the author's planned on-device check is still worth completing.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: a finished run's Live Activity is ended when the app returns to the foreground. It also uses the fix(live-activity) scope, which fits the changeset. The summary and ob…
Linked Issues check Passed Issue #1801 requires a foreground, end-only pass that applies only .endComplete. In AgentLiveActivityManager.endFinishedRuns(checking:), the held activity is selected only when `activeConnectedStr…
Out of Scope Changes check Passed The changes connect to #1801. The endFinishedRuns and endFinished functions implement the foreground pass. The endActivity rename from finalState to shownState in the update and end calls fi…
Full details: Title check

Explanation

The title names the main change: a finished run's Live Activity is ended when the app returns to the foreground. It also uses the fix(live-activity) scope, which fits the changeset. The summary and objectives both describe a foreground, end-only pass through endFinishedRuns, and the title matches that.

Full details: Linked Issues check

Explanation

Issue #1801 requires a foreground, end-only pass that applies only .endComplete. In AgentLiveActivityManager.endFinishedRuns(checking:), the held activity is selected only when activeConnectedStreamID == nil and currentState?.isFinal == false. endFinished applies only actions equal to .endComplete. The held path ends through end(status:), which resets the manager. Before ending, endHeld rechecks activity?.id, lifecycleGeneration, updateGeneration, drivenPushSessionID, and activeConnectedStreamID. This covers the reattach check after the session.active_list read. Unheld leftovers are checked again after retirePush (guard unowned() != nil) before finishPersisted. The ContentView change wires the pass into the scene-active task, and the endActivity change compares against the manager's own state (resetIfStillCurrent still uses finalState). The no-progress watchdog is not implemented. The new test testForegroundEndsOnlyFinishedHermesRunsIncludingTheHeldOne covers the held path and the running and bot exclusions. The on-device behavior of the held-activity guards needs a phone check. The simulator cannot run Live Activities, so this part stays unverified in automation.

Full details: Out of Scope Changes check

Explanation

The changes connect to #1801. The endFinishedRuns and endFinished functions implement the foreground pass. The endActivity rename from finalState to shownState in the update and end calls fixes the reset comparison that the issue needs for end(status:) to reset the manager. The ContentView change wires the pass into the scene-active task. The unit test verifies the required behavior. The ADR section documents the same behavior. The retirement of a leftover's push route before ending it is part of ending that leftover, not a separate change. No unrelated change was found in the reviewed diff.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 4c900a2 and c0e334f.

Files selected for processing: 2

  • HermesMobile/LiveActivities/AgentLiveActivityManager.swift
  • docs/adr/0070-live-activity-orphan-reconciliation.md
Files skipped from review as they are similar to previous changes: 1

  • docs/adr/0070-live-activity-orphan-reconciliation.md

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.

@greptile-apps

greptile-apps Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable issues were found.

Summary

The PR ends finished Hermes Live Activities when the app returns to the foreground. The latest change also skips an old result if the held activity’s push key changed during the check.

  • Foreground checks end Hermes runs the host no longer lists.
  • Finished runs clear the manager even when relay counts are shown.

Reviews (2) · Last reviewed commit: "fix(live-activity): skip the held end wh..." · Reviewed by Greptile

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 7429b89 and 4c900a2.

Files selected for processing: 4

  • HermesMobile/ContentView.swift
  • HermesMobile/LiveActivities/AgentLiveActivityManager.swift
  • HermesMobileTests/LiveActivityTests.swift
  • docs/adr/0070-live-activity-orphan-reconciliation.md

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

…the read

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@uzairansaruzi
uzairansaruzi merged commit ac9b522 into master Nov 24, 2026
5 checks passed
@uzairansaruzi
uzairansaruzi deleted the issue/1801-foreground-live-activity branch November 24, 2026 22:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(live-activity): recheck the held Hermes activity when the app returns to the foreground

1 participant