Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions apps/desktop/docs/MEMORY_AND_WORKERS_REVIEW.md

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions apps/desktop/src/lib/trpc/routers/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ import {
DEFAULT_SHOW_PRESETS_BAR,
DEFAULT_SHOW_RESOURCE_MONITOR,
DEFAULT_TERMINAL_LINK_BEHAVIOR,
DEFAULT_TERMINAL_PARKED_RUNTIME_CAP,
DEFAULT_USE_COMPACT_TERMINAL_ADD_BUTTON,
MAX_TERMINAL_PARKED_RUNTIME_CAP,
MIN_TERMINAL_PARKED_RUNTIME_CAP,
} from "shared/constants";
import { normalizePresetProjectIds } from "shared/preset-project-targeting";
import {
Expand Down Expand Up @@ -920,6 +923,36 @@ export const createSettingsRouter = () => {
return { success: true };
}),

getTerminalParkedRuntimeCap: publicProcedure.query(() => {
const row = getSettings();
return (
row.terminalParkedRuntimeCap ?? DEFAULT_TERMINAL_PARKED_RUNTIME_CAP
);
}),

setTerminalParkedRuntimeCap: publicProcedure
.input(
z.object({
cap: z
.number()
.int()
.min(MIN_TERMINAL_PARKED_RUNTIME_CAP)
.max(MAX_TERMINAL_PARKED_RUNTIME_CAP),
}),
)
.mutation(({ input }) => {
localDb
.insert(settings)
.values({ id: 1, terminalParkedRuntimeCap: input.cap })
.onConflictDoUpdate({
target: settings.id,
set: { terminalParkedRuntimeCap: input.cap },
})
.run();

return { success: true };
}),

getShowResourceMonitor: publicProcedure.query(() => {
const row = getSettings();
return row.showResourceMonitor ?? DEFAULT_SHOW_RESOURCE_MONITOR;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { describe, expect, it } from "bun:test";
import {
normalizeParkedRuntimeCap,
selectRuntimesToEvict,
} from "./terminal-runtime-eviction";

const attachedMarker = {};

function parked(id: string, lastUsedAt: number) {
return { id, runtime: { container: null }, lastUsedAt };
}

function attached(id: string, lastUsedAt: number) {
return { id, runtime: { container: attachedMarker }, lastUsedAt };
}

function bare(id: string) {
return { id, runtime: null, lastUsedAt: 0 };
}

function ids(entries: { id: string }[]): string[] {
return entries.map((entry) => entry.id);
}

describe("selectRuntimesToEvict", () => {
it("evicts by recency, not insertion order", () => {
// "a" was inserted first but re-used most recently
const entries = [parked("a", 9), parked("b", 1), parked("c", 5)];
expect(ids(selectRuntimesToEvict(entries, 2))).toEqual(["b"]);
expect(ids(selectRuntimesToEvict(entries, 1))).toEqual(["b", "c"]);
});

it("returns nothing at or under the cap", () => {
const entries = [parked("a", 1), parked("b", 2)];
expect(selectRuntimesToEvict(entries, 2)).toEqual([]);
expect(selectRuntimesToEvict(entries, 3)).toEqual([]);
expect(selectRuntimesToEvict([], 1)).toEqual([]);
});

it("never selects attached runtimes, even when they exceed the cap", () => {
const entries = [
attached("a", 1),
attached("b", 2),
attached("c", 3),
parked("p", 4),
];
expect(selectRuntimesToEvict(entries, 2)).toEqual([]);
});

it("attached entries do not shield parked ones past the cap", () => {
const entries = [attached("a", 9), parked("old", 1), parked("new", 2)];
expect(ids(selectRuntimesToEvict(entries, 1))).toEqual(["old"]);
});

it("ignores runtime-less entries entirely", () => {
const entries = [bare("x"), bare("y"), parked("a", 1), parked("b", 2)];
expect(selectRuntimesToEvict(entries, 2)).toEqual([]);
expect(ids(selectRuntimesToEvict(entries, 1))).toEqual(["a"]);
});
});

describe("normalizeParkedRuntimeCap", () => {
it("floors valid values", () => {
expect(normalizeParkedRuntimeCap(12)).toBe(12);
expect(normalizeParkedRuntimeCap(6.9)).toBe(6);
expect(normalizeParkedRuntimeCap(1)).toBe(1);
});

it("rejects non-finite and sub-1 values", () => {
expect(normalizeParkedRuntimeCap(0)).toBeNull();
expect(normalizeParkedRuntimeCap(-3)).toBeNull();
expect(normalizeParkedRuntimeCap(Number.NaN)).toBeNull();
expect(normalizeParkedRuntimeCap(Number.POSITIVE_INFINITY)).toBeNull();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Pure selection policy for parked-runtime LRU eviction (SUPER-1545).
* Kept dependency-free so it can be unit-tested without pulling the
* xterm/transport import graph into the test process.
*/

interface EvictionCandidate {
runtime: { container: unknown } | null;
lastUsedAt: number;
}

/** Returns null for values that must not change the cap (non-finite or < 1). */
export function normalizeParkedRuntimeCap(cap: number): number | null {
if (!Number.isFinite(cap) || cap < 1) return null;
return Math.floor(cap);
}

/**
* Pick the parked (live runtime, no container) entries to release, oldest
* `lastUsedAt` first, so at most `cap` parked runtimes remain. Attached
* runtimes and runtime-less entries are never candidates.
*/
export function selectRuntimesToEvict<T extends EvictionCandidate>(
entries: Iterable<T>,
cap: number,
): T[] {
const parked = Array.from(entries).filter(
(entry) => entry.runtime !== null && entry.runtime.container === null,
);
if (parked.length <= cap) return [];
parked.sort((a, b) => a.lastUsedAt - b.lastUsedAt);
return parked.slice(0, parked.length - cap);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ProgressAddon } from "@xterm/addon-progress";
import type { SearchAddon } from "@xterm/addon-search";
import { DEFAULT_TERMINAL_PARKED_RUNTIME_CAP } from "shared/constants";
import type { TerminalAppearance } from "./appearance";
import {
type LinkHoverInfo,
Expand All @@ -14,6 +15,10 @@ import {
type TerminalRuntime,
updateRuntimeAppearance,
} from "./terminal-runtime";
import {
normalizeParkedRuntimeCap,
selectRuntimesToEvict,
} from "./terminal-runtime-eviction";
import {
type ConnectionState,
clearLogs,
Expand All @@ -37,11 +42,30 @@ interface RegistryEntry {
linkManager: TerminalLinkManager | null;
/** Stored until linkManager is created (mount called after setLinkHandlers). */
pendingLinkHandlers: TerminalLinkHandlers | null;
/** Monotonic use counter; bumped on mount/detach, drives parked-LRU eviction. */
lastUsedAt: number;
}

class TerminalRuntimeRegistryImpl {
private entries = new Map<string, RegistryEntry>();
private entryKeysByTerminalId = new Map<string, Set<string>>();
private useSeq = 0;
private pendingEviction: ReturnType<typeof setTimeout> | null = null;
/**
* Cap on parked (hidden) xterm runtimes. Each live runtime holds its full
* scrollback and a WebGL context (~55–70 MB RSS measured), so parked
* instances beyond this are released — buffer persisted to localStorage,
* PTY untouched — and rebuilt from the persisted buffer on next mount.
* User-configurable via settings.setTerminalParkedRuntimeCap. (SUPER-1545)
*/
private parkedRuntimeCap = DEFAULT_TERMINAL_PARKED_RUNTIME_CAP;

setParkedRuntimeCap(cap: number) {
const normalized = normalizeParkedRuntimeCap(cap);
if (normalized === null) return;
this.parkedRuntimeCap = normalized;
this.scheduleParkedEviction();
}

private getEntryKey(terminalId: string, instanceId = terminalId): string {
return `${terminalId}\u0000${instanceId}`;
Expand All @@ -62,6 +86,7 @@ class TerminalRuntimeRegistryImpl {
transport: createTransport(),
linkManager: null,
pendingLinkHandlers: null,
lastUsedAt: 0,
};

this.entries.set(key, entry);
Expand Down Expand Up @@ -146,6 +171,7 @@ class TerminalRuntimeRegistryImpl {
instanceId = terminalId,
) {
const entry = this.getOrCreateEntry(terminalId, instanceId);
entry.lastUsedAt = ++this.useSeq;

if (!entry.runtime) {
entry.runtime = createRuntime(terminalId, appearance, {
Expand Down Expand Up @@ -241,7 +267,31 @@ class TerminalRuntimeRegistryImpl {
const entry = this.getEntry(terminalId, instanceId);
if (!entry?.runtime) return;

entry.lastUsedAt = ++this.useSeq;
detachFromContainer(entry.runtime);
this.scheduleParkedEviction();
}

/**
* Deferred so a workspace switch (detach batch, then mount batch in the
* next effect pass) re-adopts parked runtimes before eviction counts them.
*/
private scheduleParkedEviction() {
if (this.pendingEviction !== null) return;
this.pendingEviction = setTimeout(() => {
this.pendingEviction = null;
this.evictExcessParkedRuntimes();
}, 0);
}

private evictExcessParkedRuntimes() {
const victims = selectRuntimesToEvict(
this.entries.values(),
this.parkedRuntimeCap,
);
for (const entry of victims) {
this.disposeEntry(entry, { clearPersistedState: false });
}
}

updateAppearance(
Expand Down
10 changes: 10 additions & 0 deletions apps/desktop/src/renderer/routes/_authenticated/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { useSignOut } from "renderer/hooks/useSignOut";
import { authClient, getAuthToken } from "renderer/lib/auth-client";
import { dragDropManager } from "renderer/lib/dnd";
import { electronTrpc } from "renderer/lib/electron-trpc";
import { terminalRuntimeRegistry } from "renderer/lib/terminal/terminal-runtime-registry";
import { showWorkspaceAutoNameWarningToast } from "renderer/lib/workspaces/showWorkspaceAutoNameWarningToast";
import { InitGitDialog } from "renderer/react-query/projects/InitGitDialog";
import { DaemonAutoUpdateFailureDialog } from "renderer/routes/_authenticated/components/DaemonAutoUpdateFailureDialog";
Expand Down Expand Up @@ -92,6 +93,15 @@ function AuthenticatedLayout() {

useAgentHookListener();

// Seed the parked-terminal eviction cap from settings (SUPER-1545).
const { data: parkedRuntimeCap } =
electronTrpc.settings.getTerminalParkedRuntimeCap.useQuery();
useEffect(() => {
if (parkedRuntimeCap !== undefined) {
terminalRuntimeRegistry.setParkedRuntimeCap(parkedRuntimeCap);
}
}, [parkedRuntimeCap]);

// Update workspace-run pane state on terminal exit
electronTrpc.notifications.subscribe.useSubscription(undefined, {
onData: (event) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
SETTING_ITEM_ID,
type SettingItemId,
} from "../../../utils/settings-search";
import { BackgroundTerminalsSetting } from "./components/BackgroundTerminalsSetting";
import { LinkBehaviorSetting } from "./components/LinkBehaviorSetting";
import { PresetsSection } from "./components/PresetsSection";
import { SessionsSection } from "./components/SessionsSection";
Expand Down Expand Up @@ -64,6 +65,10 @@ export function TerminalSettings({
SETTING_ITEM_ID.TERMINAL_SESSIONS,
visibleItems,
);
const showBackgroundLimit = isItemVisible(
SETTING_ITEM_ID.TERMINAL_BACKGROUND_LIMIT,
visibleItems,
);

return (
<div className="p-6 max-w-6xl w-full">
Expand Down Expand Up @@ -98,6 +103,9 @@ export function TerminalSettings({
/>
))}
{showLinkBehavior && <LinkBehaviorSetting key="link-behavior" />}
{showBackgroundLimit && (
<BackgroundTerminalsSetting key="background-limit" />
)}
{showSessions &&
(isV2CloudEnabled ? (
<V2SessionsSection key="sessions" />
Expand Down
Loading
Loading