Skip to content

Conversation

@Kitenite
Copy link
Collaborator

@Kitenite Kitenite commented Jan 18, 2026

Summary

  • When terminal persistence is enabled in settings, the daemon now starts immediately without requiring app restart
  • When terminal persistence is disabled, the daemon shuts down and terminates all sessions
  • Extracts shared initializeDaemon() and shutdownDaemon() helpers used by both startup and runtime toggle paths

Test plan

  • Enable terminal persistence in settings → daemon should start, new terminals should persist
  • Disable terminal persistence in settings → daemon should shutdown, existing persisted terminals should be terminated
  • App restart with persistence enabled → should still work as before
  • App restart with persistence disabled → orphaned daemon cleanup should still work

Summary by CodeRabbit

  • New Features

    • Enabled runtime toggling of terminal daemon mode (persistent background processes) with automatic lifecycle management.
  • Improvements

    • Enhanced terminal persistence settings to properly handle background process initialization and shutdown.
    • Workspace runtime registry now automatically resets when daemon mode is toggled.

✏️ Tip: You can customize this high-level summary in your review settings.

…e is toggled

When users enable/disable terminal persistence in settings, the daemon
now starts/stops immediately without requiring an app restart. Extracts
shared daemon initialization and shutdown logic for use by both startup
and runtime toggle paths.
@coderabbitai
Copy link

coderabbitai bot commented Jan 18, 2026

📝 Walkthrough

Walkthrough

This PR adds runtime daemon mode toggling functionality to the terminal subsystem. When terminal persistence settings are updated through the settings API, the system now enables or disables daemon mode and resets the workspace runtime registry accordingly. The daemon management module gains new lifecycle functions for initialization, shutdown, and session reconciliation.

Changes

Cohort / File(s) Summary
Settings Router Integration
apps/desktop/src/lib/trpc/routers/settings/index.ts
Made setTerminalPersistence mutation async to orchestrate daemon mode toggling and workspace runtime registry reset as side effects when terminal persistence changes.
Daemon Lifecycle Management
apps/desktop/src/main/lib/terminal/index.ts
Introduced initializeDaemon(), shutdownDaemon(), and reconcileDaemonSessions() core functions. Added enableDaemonMode() and disableDaemonMode() for runtime toggling with state caching and session handling. Refactored daemon mode detection with caching and environment variable override support. Enhanced error handling with consistent logging patterns.

Sequence Diagram

sequenceDiagram
    participant Settings Router
    participant Terminal Manager
    participant Daemon
    participant Workspace Registry

    Settings Router->>Terminal Manager: enableDaemonMode() or disableDaemonMode()
    
    alt Enable Path
        Terminal Manager->>Daemon: initializeDaemon()
        Daemon->>Terminal Manager: daemon ready
        Terminal Manager->>Terminal Manager: reconcileDaemonSessions()
        Terminal Manager->>Terminal Manager: cache state
    else Disable Path
        Terminal Manager->>Daemon: shutdownDaemon()
        Daemon->>Terminal Manager: shutdown complete
        Terminal Manager->>Terminal Manager: cache state
    end
    
    Terminal Manager->>Workspace Registry: resetWorkspaceRuntimeRegistry()
    Workspace Registry->>Settings Router: reset complete
    Settings Router->>Settings Router: return success
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • AviPeltz

Poem

🐰 A daemon awakens at rabbit's command,
With toggles and resets across the land,
Now settings control this mystical beast,
From startup to shutdown—a feature-rich feast! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description is incomplete and does not follow the required template structure, missing required sections like Related Issues and Type of Change. Add all required template sections: Related Issues (with GitHub keywords), Type of Change checkbox (mark New feature), Testing (detailed verification steps), and Additional Notes if applicable.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: enabling daemon mode at runtime when terminal persistence is toggled, which directly matches the PR objectives and code modifications.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

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 and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@apps/desktop/src/main/lib/terminal/index.ts`:
- Around line 147-161: The cachedDaemonMode is set to true before
initializeDaemon() completes, so on failure the cache incorrectly indicates the
daemon is active; update enableDaemonMode to only set cachedDaemonMode = true
after await initializeDaemon() succeeds, or alternatively catch the error and
reset cachedDaemonMode = false before rethrowing; reference enableDaemonMode,
initializeDaemon, cachedDaemonMode and getActiveTerminalManager when making the
change so subsequent calls reflect the real daemon state.

Comment on lines +147 to +161
export async function enableDaemonMode(): Promise<void> {
if (cachedDaemonMode === true) {
return;
}

console.log("[TerminalManager] Enabling daemon mode");
cachedDaemonMode = true;

try {
await initializeDaemon();
} catch (error) {
console.error("[TerminalManager] Failed to enable daemon mode:", error);
throw error;
}
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Cache is updated before async operation completes.

If initializeDaemon() fails, the cache remains set to true while the daemon isn't actually running. This could cause subsequent calls to getActiveTerminalManager() to return the daemon manager when the daemon isn't initialized.

Consider rolling back the cache on failure:

Proposed fix
 export async function enableDaemonMode(): Promise<void> {
 	if (cachedDaemonMode === true) {
 		return;
 	}

 	console.log("[TerminalManager] Enabling daemon mode");
 	cachedDaemonMode = true;

 	try {
 		await initializeDaemon();
 	} catch (error) {
+		cachedDaemonMode = false;
 		console.error("[TerminalManager] Failed to enable daemon mode:", error);
 		throw error;
 	}
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export async function enableDaemonMode(): Promise<void> {
if (cachedDaemonMode === true) {
return;
}
console.log("[TerminalManager] Enabling daemon mode");
cachedDaemonMode = true;
try {
await initializeDaemon();
} catch (error) {
console.error("[TerminalManager] Failed to enable daemon mode:", error);
throw error;
}
}
export async function enableDaemonMode(): Promise<void> {
if (cachedDaemonMode === true) {
return;
}
console.log("[TerminalManager] Enabling daemon mode");
cachedDaemonMode = true;
try {
await initializeDaemon();
} catch (error) {
cachedDaemonMode = false;
console.error("[TerminalManager] Failed to enable daemon mode:", error);
throw error;
}
}
🤖 Prompt for AI Agents
In `@apps/desktop/src/main/lib/terminal/index.ts` around lines 147 - 161, The
cachedDaemonMode is set to true before initializeDaemon() completes, so on
failure the cache incorrectly indicates the daemon is active; update
enableDaemonMode to only set cachedDaemonMode = true after await
initializeDaemon() succeeds, or alternatively catch the error and reset
cachedDaemonMode = false before rethrowing; reference enableDaemonMode,
initializeDaemon, cachedDaemonMode and getActiveTerminalManager when making the
change so subsequent calls reflect the real daemon state.

@Kitenite Kitenite closed this Jan 18, 2026
@github-actions
Copy link

🧹 Preview Cleanup Complete

The following preview resources have been cleaned up:

  • ⚠️ Neon database branch
  • ⚠️ Electric Fly.io app

Thank you for your contribution! 🎉

@Kitenite Kitenite deleted the daemon-from-setting branch January 18, 2026 22:15
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.

2 participants