Skip to content

refactor(session): split boundary owners - #1957

Merged
zerob13 merged 3 commits into
devfrom
codex/session-boundary-cleanup
Jul 13, 2026
Merged

refactor(session): split boundary owners#1957
zerob13 merged 3 commits into
devfrom
codex/session-boundary-cleanup

Conversation

@zerob13

@zerob13 zerob13 commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • move history search, translation, current-session export, usage statistics, startup migrations, RTK health, and available-agent catalog responsibilities out of AgentSessionPresenter
  • remove the migrated presenter methods and shared interface surface, and wire typed routes and lifecycle hooks to explicit narrow owners
  • add characterization tests, architecture guards, SDD records, maintained architecture documentation, and refreshed baselines

Why

AgentSessionPresenter remained a God Object after the layered runtime refactor. Unrelated route, migration, maintenance, export, and catalog policies still accumulated behind its public interface and startup hooks depended on unsafe presenter casts. This change establishes explicit ownership boundaries without changing route contracts or session behavior.

Impact

  • AgentSessionPresenter is reduced from 4077 to 2636 lines
  • 20 foreign presenter methods are removed
  • five startup hooks use typed owners without unsafe casts or optional task probes
  • renderer route contracts and persisted migration keys remain unchanged
  • no schema migration or user-visible UI change

Validation

  • pnpm run format / pnpm run format:check
  • pnpm run i18n
  • pnpm run lint
  • pnpm run typecheck
  • pnpm run test:main — 3753 passed, 194 skipped
  • focused owner, route, lifecycle, floating-button, and composition tests — 146 passed
  • pnpm run lint:architecture
  • pnpm run architecture:baseline
  • git diff --check

Architecture cycle count remains 36 and DeepChat loop dependency metrics remain zero.

Summary by CodeRabbit

  • New Features
    • Enhanced session history search with FTS + LIKE and a legacy fallback, powering Spotlight results.
    • Added dedicated session translation and conversation export capabilities.
    • Added usage dashboard generation and async usage backfill support.
    • Improved available-agent listing with ACP-aware filtering.
  • Bug Fixes
    • Improved legacy chat import, mainline normalization, and disabled-search-tool cleanup during startup.
  • Documentation
    • Updated architecture, flows, navigation, and migration docs to match the current session routing/ownership model.

@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: db45f087-b45f-4446-a5d4-b29fbe1ea7e1

📥 Commits

Reviewing files that changed from the base of the PR and between de6f67a and 4be0498.

📒 Files selected for processing (2)
  • src/main/agent/shared/assistantModelSelection.ts
  • test/main/routes/sessions/sessionTranslation.test.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/main/agent/shared/assistantModelSelection.ts
  • test/main/routes/sessions/sessionTranslation.test.ts

📝 Walkthrough

Walkthrough

The PR narrows AgentSessionPresenter to core session orchestration and moves search, translation, export, usage, RTK, catalog, import, and migration responsibilities into dedicated services, typed routes, lifecycle hooks, and shared policies. Architecture documentation, contracts, wiring, and tests are updated accordingly.

Changes

Session boundary cleanup

Layer / File(s) Summary
Architecture contracts and enforcement
docs/architecture/*, scripts/architecture-guard.mjs, docs/guides/*
Documentation, contracts, baselines, and guard rules define dedicated session owners, narrowed presenter responsibilities, and prohibited presenter or lifecycle-hook regressions.
Shared policies and presenter surface cleanup
src/main/agent/shared/*, src/main/presenter/agentSessionPresenter/*, src/shared/contracts/routes/sessions.routes.ts, src/shared/types/presenters/*
Assistant model selection and available-agent filtering become shared helpers; moved presenter methods and history-search types are removed from the presenter surface.
Startup migration and maintenance owners
src/main/presenter/startupMigrations/*, src/main/presenter/usageStatsService.ts, src/main/presenter/lifecyclePresenter/hooks/after-start/*
Legacy import, session-data migrations, usage backfill, and RTK health startup are implemented or invoked directly by dedicated owners.
Typed session route services
src/main/routes/*, src/main/presenter/exporter/agentSessionExporter.ts
History search, translation, export, usage dashboard, RTK retry, and agent listing use explicit route dependencies and service implementations.
Composition and validation
src/main/presenter/index.ts, src/main/presenter/floatingButtonPresenter/*, test/main/**/*
Presenter construction and floating-button loading use the new owners, while route, migration, exporter, lifecycle, composition, and architecture tests validate the rewiring.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Renderer
  participant MainKernelRouteRuntime
  participant SessionOwner
  participant SQLitePresenter

  Renderer->>MainKernelRouteRuntime: invoke typed session route
  MainKernelRouteRuntime->>SessionOwner: delegate search, translation, export, or dashboard
  SessionOwner->>SQLitePresenter: read session, transcript, or usage data
  SQLitePresenter-->>SessionOwner: return persisted data
  SessionOwner-->>Renderer: return typed result
Loading

Possibly related PRs

Suggested reviewers: zhangmo8

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately captures the main refactor: splitting session boundary ownership into dedicated owners.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/session-boundary-cleanup

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

Comment @coderabbitai help to get the list of available commands.

@zerob13
zerob13 marked this pull request as ready for review July 13, 2026 09:05

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/main/presenter/usageStatsService.ts (1)

210-220: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Fallback path re-fetches and re-sorts the full candidate set on every page.

When listAssistantUsageCandidatesPage is absent, each iteration of the runBackfill while loop calls listAssistantUsageCandidates() (all rows) and re-sorts them, giving roughly O(n²·log n) work over the backfill. Sort once outside the loop (or cache the sorted array) and page against it.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/main/presenter/usageStatsService.ts` around lines 210 - 220, Update the
fallback pagination flow used by runBackfill so listAssistantUsageCandidates()
is fetched and sorted only once outside the while loop, then reuse the cached
sorted candidates for each cursor page. Preserve the existing created_at/id
ordering, cursor filtering, and limit behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/main/agent/shared/assistantModelSelection.ts`:
- Line 13: Update the backend check in the assistant model selection flow to
safely handle optional or unknown agentId values before accessing .kind on
resolveBackend(). Fall back to the default translation model when
agentManager.resolveBackend(agentId) cannot resolve a backend, while preserving
deepchat-specific model selection for valid backends.

---

Nitpick comments:
In `@src/main/presenter/usageStatsService.ts`:
- Around line 210-220: Update the fallback pagination flow used by runBackfill
so listAssistantUsageCandidates() is fetched and sorted only once outside the
while loop, then reuse the cached sorted candidates for each cursor page.
Preserve the existing created_at/id ordering, cursor filtering, and limit
behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 89aa0e2a-db8f-4211-9e40-39e538640e6a

📥 Commits

Reviewing files that changed from the base of the PR and between 28e2a0e and de6f67a.

📒 Files selected for processing (51)
  • docs/ARCHITECTURE.md
  • docs/FLOWS.md
  • docs/architecture/agent-system-layered-runtime/README.md
  • docs/architecture/agent-system.md
  • docs/architecture/baselines/agent-system-layered-runtime-baseline.json
  • docs/architecture/baselines/dependency-report.md
  • docs/architecture/baselines/main-kernel-boundary-baseline.md
  • docs/architecture/baselines/main-kernel-migration-scoreboard.json
  • docs/architecture/baselines/main-kernel-migration-scoreboard.md
  • docs/architecture/session-boundary-cleanup/plan.md
  • docs/architecture/session-boundary-cleanup/spec.md
  • docs/architecture/session-boundary-cleanup/tasks.md
  • docs/architecture/session-management.md
  • docs/guides/code-navigation.md
  • docs/guides/getting-started.md
  • scripts/architecture-guard.mjs
  • src/main/agent/shared/assistantModelSelection.ts
  • src/main/agent/shared/availableAgentCatalog.ts
  • src/main/presenter/agentSessionPresenter/index.ts
  • src/main/presenter/exporter/agentSessionExporter.ts
  • src/main/presenter/floatingButtonPresenter/index.ts
  • src/main/presenter/index.ts
  • src/main/presenter/lifecyclePresenter/hooks/after-start/disabledSearchToolCleanupHook.ts
  • src/main/presenter/lifecyclePresenter/hooks/after-start/legacyImportHook.ts
  • src/main/presenter/lifecyclePresenter/hooks/after-start/rtkHealthCheckHook.ts
  • src/main/presenter/lifecyclePresenter/hooks/after-start/sqliteMainlineNormalizationHook.ts
  • src/main/presenter/lifecyclePresenter/hooks/after-start/usageStatsBackfillHook.ts
  • src/main/presenter/sqlitePresenter/index.ts
  • src/main/presenter/startupMigrations/legacyChatImportService.ts
  • src/main/presenter/startupMigrations/sessionDataMigrations.ts
  • src/main/presenter/usageStatsService.ts
  • src/main/routes/index.ts
  • src/main/routes/sessions/sessionHistorySearch.ts
  • src/main/routes/sessions/sessionTranslation.ts
  • src/renderer/src/stores/ui/spotlight.ts
  • src/shared/contracts/routes/sessions.routes.ts
  • src/shared/types/presenters/agent-session.presenter.d.ts
  • src/shared/types/presenters/index.d.ts
  • test/main/agent/shared/availableAgentCatalog.test.ts
  • test/main/presenter/agentSessionPresenter/agentSessionPresenter.test.ts
  • test/main/presenter/exporter/agentSessionExporter.test.ts
  • test/main/presenter/floatingButtonPresenter/index.test.ts
  • test/main/presenter/lifecyclePresenter/startupMaintenanceHooks.test.ts
  • test/main/presenter/sessionBoundaryComposition.test.ts
  • test/main/presenter/startupMigrations/legacyChatImportService.test.ts
  • test/main/presenter/startupMigrations/sessionDataMigrations.test.ts
  • test/main/presenter/usageStatsService.test.ts
  • test/main/routes/dispatcher.test.ts
  • test/main/routes/sessions/sessionHistorySearch.test.ts
  • test/main/routes/sessions/sessionTranslation.test.ts
  • test/main/scripts/architectureGuard.test.ts
💤 Files with no reviewable changes (2)
  • src/shared/types/presenters/agent-session.presenter.d.ts
  • test/main/presenter/agentSessionPresenter/agentSessionPresenter.test.ts

Comment thread src/main/agent/shared/assistantModelSelection.ts Outdated
@zerob13
zerob13 merged commit a93d573 into dev Jul 13, 2026
4 checks passed
@zhangmo8
zhangmo8 deleted the codex/session-boundary-cleanup branch July 14, 2026 05:24
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.

1 participant