mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-26 02:31:40 +00:00
perf(plugins): short-circuit PluginTerminalProviderRegistry cloning and IPC when plugin host is disabled (#4380)
This commit is contained in:
2 files changed
+209
-3
No files matched your search
@@ -3,10 +3,15 @@ import test from 'node:test';
|
||||
|
||||
import {
|
||||
collectPluginTerminalProviderKinds,
|
||||
createWindowPluginTerminalProviderRegistry,
|
||||
isPluginTerminalProviderKindAvailable,
|
||||
PluginTerminalProviderAvailability,
|
||||
PluginTerminalProviderRegistry,
|
||||
} from './pluginTerminalProviderRegistry.ts';
|
||||
import {
|
||||
_resetSharedPluginRuntimeStatusForTests,
|
||||
getSharedPluginRuntimeStatus,
|
||||
} from './pluginRuntimeStatusCache.ts';
|
||||
|
||||
const session: NetcattyTerminalSessionSnapshot = {
|
||||
sessionId: 'session-1',
|
||||
@@ -430,3 +435,142 @@ test('terminal Provider registry forwards contribution lifecycle invalidation',
|
||||
registry.dispose();
|
||||
assert.equal(disposed, true);
|
||||
});
|
||||
|
||||
test('terminal Provider registry short-circuits cloning, snapshots, and IPC when plugin runtime is disabled, and re-probes on contribution change', async (t) => {
|
||||
_resetSharedPluginRuntimeStatusForTests();
|
||||
let changed: (() => void) | undefined;
|
||||
let available = false;
|
||||
let statusCalls = 0;
|
||||
let listCalls = 0;
|
||||
let lifecycleCalls = 0;
|
||||
const requests: NetcattyTerminalProviderRequest[] = [];
|
||||
const events: NetcattyTerminalSessionEvent[] = [];
|
||||
|
||||
const registry = new PluginTerminalProviderRegistry({
|
||||
async getPluginRuntimeStatus() {
|
||||
statusCalls += 1;
|
||||
return { available } as NetcattyPluginRuntimeStatus;
|
||||
},
|
||||
async listPluginTerminalProviders() {
|
||||
listCalls += 1;
|
||||
return [{
|
||||
pluginId: 'com.example.matcher',
|
||||
pluginVersion: '1.0.0',
|
||||
pluginDisplayName: 'Matcher',
|
||||
provider: {
|
||||
id: 'com.example.matcher.output',
|
||||
label: 'Output matcher',
|
||||
kind: 'terminal.matcher',
|
||||
},
|
||||
}];
|
||||
},
|
||||
async providePluginTerminal(request) {
|
||||
requests.push(request);
|
||||
return [];
|
||||
},
|
||||
async cancelPluginTerminalRequest() { return false; },
|
||||
async publishPluginTerminalSessionEvent(event) {
|
||||
lifecycleCalls += 1;
|
||||
events.push(event);
|
||||
return [];
|
||||
},
|
||||
onPluginContributionsChanged(listener) {
|
||||
changed = listener;
|
||||
return () => {};
|
||||
},
|
||||
});
|
||||
|
||||
const cloneSpy = t.mock.method(globalThis, 'structuredClone');
|
||||
try {
|
||||
// Parallel provider enumerations coalesce onto one runtime status check and make 0 provider IPCs.
|
||||
const [matchers, links] = await Promise.all([
|
||||
registry.listProviders({ kind: 'terminal.matcher' }),
|
||||
registry.listProviders({ kind: 'terminal.link' }),
|
||||
]);
|
||||
assert.deepEqual(matchers, []);
|
||||
assert.deepEqual(links, []);
|
||||
assert.equal(statusCalls, 1);
|
||||
assert.equal(listCalls, 0);
|
||||
assert.equal(cloneSpy.mock.callCount(), 0);
|
||||
|
||||
// Lifecycle events while disabled must not clone, snapshot, or call IPC.
|
||||
await registry.publishSessionEvent({
|
||||
type: 'created',
|
||||
session: { ...session, cwd: '/must-not-be-retained', title: 'Hidden title' },
|
||||
});
|
||||
await registry.publishSessionEvent({
|
||||
type: 'cwdChanged',
|
||||
session: { ...session, cwd: '/also-not-retained' },
|
||||
});
|
||||
await registry.publishSessionEvent({
|
||||
type: 'resized',
|
||||
session: { ...session, cols: 120, rows: 40 },
|
||||
});
|
||||
assert.equal(statusCalls, 1);
|
||||
assert.equal(lifecycleCalls, 0);
|
||||
assert.equal(cloneSpy.mock.callCount(), 0, '0 structuredClone calls when plugin host is disabled');
|
||||
|
||||
// Dev-mode hot-enable: onPluginContributionsChanged invalidates cached status and re-probes.
|
||||
available = true;
|
||||
changed?.();
|
||||
|
||||
// Verify no stale session snapshot was retained while disabled.
|
||||
await registry.request({ kind: 'terminal.completion', operation: 'provide', session });
|
||||
assert.equal(Object.hasOwn(requests[0].session, 'cwd'), false, 'disabled publishSessionEvent must not grow #sessionSnapshots');
|
||||
assert.equal(Object.hasOwn(requests[0].session, 'title'), false);
|
||||
|
||||
const enabledProviders = await registry.listProviders({ kind: 'terminal.matcher' });
|
||||
assert.equal(statusCalls, 2, 'onPluginContributionsChanged invalidates shared runtime status cache');
|
||||
assert.equal(listCalls, 1);
|
||||
assert.equal(enabledProviders.length, 1);
|
||||
|
||||
await registry.publishSessionEvent({ type: 'connected', session: { ...session, cwd: '/live' } });
|
||||
assert.equal(lifecycleCalls, 1);
|
||||
assert.equal(events[0].session.cwd, '/live');
|
||||
} finally {
|
||||
registry.dispose();
|
||||
_resetSharedPluginRuntimeStatusForTests();
|
||||
}
|
||||
});
|
||||
|
||||
test('createWindowPluginTerminalProviderRegistry shares runtime status cache with the window bridge', async (t) => {
|
||||
_resetSharedPluginRuntimeStatusForTests();
|
||||
let statusCalls = 0;
|
||||
let listCalls = 0;
|
||||
let lifecycleCalls = 0;
|
||||
const mockBridge = {
|
||||
async getPluginRuntimeStatus() {
|
||||
statusCalls += 1;
|
||||
return { available: false };
|
||||
},
|
||||
async listPluginTerminalProviders() {
|
||||
listCalls += 1;
|
||||
return [];
|
||||
},
|
||||
async providePluginTerminal() { return []; },
|
||||
async cancelPluginTerminalRequest() { return false; },
|
||||
async publishPluginTerminalSessionEvent() {
|
||||
lifecycleCalls += 1;
|
||||
return [];
|
||||
},
|
||||
} as unknown as NetcattyBridge;
|
||||
|
||||
// Pre-populate shared runtime status cache (as usePluginContributions does on mount).
|
||||
assert.deepEqual(await getSharedPluginRuntimeStatus(mockBridge), { available: false });
|
||||
assert.equal(statusCalls, 1);
|
||||
|
||||
const cloneSpy = t.mock.method(globalThis, 'structuredClone');
|
||||
const registry = createWindowPluginTerminalProviderRegistry(mockBridge);
|
||||
assert.ok(registry);
|
||||
try {
|
||||
assert.deepEqual(await registry.listProviders({ kind: 'terminal.matcher' }), []);
|
||||
await registry.publishSessionEvent({ type: 'created', session });
|
||||
assert.equal(statusCalls, 1, 'reuses cached runtime status from window bridge without extra IPC');
|
||||
assert.equal(listCalls, 0);
|
||||
assert.equal(lifecycleCalls, 0);
|
||||
assert.equal(cloneSpy.mock.callCount(), 0);
|
||||
} finally {
|
||||
registry.dispose();
|
||||
_resetSharedPluginRuntimeStatusForTests();
|
||||
}
|
||||
});
|
||||
@@ -1,8 +1,14 @@
|
||||
import {
|
||||
getSharedPluginRuntimeStatus,
|
||||
invalidateSharedPluginRuntimeStatus,
|
||||
} from './pluginRuntimeStatusCache';
|
||||
|
||||
export interface PluginTerminalProviderBridge {
|
||||
listPluginTerminalProviders(options: NetcattyTerminalProviderQuery): Promise<ReadonlyArray<NetcattyTerminalProviderContribution>>;
|
||||
providePluginTerminal(request: NetcattyTerminalProviderRequest): Promise<ReadonlyArray<NetcattyTerminalProviderResult>>;
|
||||
cancelPluginTerminalRequest(requestId: string): Promise<boolean>;
|
||||
publishPluginTerminalSessionEvent(event: NetcattyTerminalSessionEvent): Promise<ReadonlyArray<{ pluginId: string; delivered: boolean }>>;
|
||||
getPluginRuntimeStatus?(): Promise<NetcattyPluginRuntimeStatus>;
|
||||
onPluginContributionsChanged?(callback: () => void): () => void;
|
||||
}
|
||||
|
||||
@@ -52,6 +58,7 @@ function mergeLifecycleSessionSnapshot(
|
||||
|
||||
export class PluginTerminalProviderRegistry {
|
||||
readonly #bridge: PluginTerminalProviderBridge;
|
||||
readonly #statusBridge: Pick<NetcattyBridge, 'getPluginRuntimeStatus'>;
|
||||
readonly #activeRequests = new Map<string, string>();
|
||||
readonly #sessionSnapshots = new Map<string, NetcattyTerminalSessionSnapshot>();
|
||||
readonly #providerListeners = new Set<() => void>();
|
||||
@@ -62,11 +69,16 @@ export class PluginTerminalProviderRegistry {
|
||||
#bridgeAvailability: 'unknown' | 'available' | 'unavailable' = 'unknown';
|
||||
#disposed = false;
|
||||
|
||||
constructor(bridge: PluginTerminalProviderBridge) {
|
||||
constructor(
|
||||
bridge: PluginTerminalProviderBridge,
|
||||
statusBridge: Pick<NetcattyBridge, 'getPluginRuntimeStatus'> = bridge,
|
||||
) {
|
||||
this.#bridge = bridge;
|
||||
this.#statusBridge = statusBridge;
|
||||
this.#disposeContributionListener = bridge.onPluginContributionsChanged?.(() => {
|
||||
this.#providerListGeneration += 1;
|
||||
this.#bridgeAvailability = 'unknown';
|
||||
invalidateSharedPluginRuntimeStatus(this.#statusBridge);
|
||||
this.#providerListCache.clear();
|
||||
this.#pendingProviderLists.clear();
|
||||
for (const requestId of this.#activeRequests.values()) {
|
||||
@@ -79,6 +91,33 @@ export class PluginTerminalProviderRegistry {
|
||||
});
|
||||
}
|
||||
|
||||
#isUnavailable(): boolean {
|
||||
return this.#disposed || this.#bridgeAvailability === 'unavailable';
|
||||
}
|
||||
|
||||
async #probeRuntimeStatus(generation: number): Promise<boolean> {
|
||||
try {
|
||||
const status = await getSharedPluginRuntimeStatus(this.#statusBridge);
|
||||
if (this.#disposed) return false;
|
||||
if (generation !== this.#providerListGeneration) {
|
||||
return this.#bridgeAvailability !== 'unavailable';
|
||||
}
|
||||
if (!status.available) {
|
||||
this.#bridgeAvailability = 'unavailable';
|
||||
this.#providerListCache.clear();
|
||||
return false;
|
||||
}
|
||||
this.#bridgeAvailability = 'available';
|
||||
return true;
|
||||
} catch {
|
||||
if (!this.#disposed && generation === this.#providerListGeneration) {
|
||||
this.#bridgeAvailability = 'unavailable';
|
||||
this.#providerListCache.clear();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
onDidChangeProviders(listener: () => void): () => void {
|
||||
if (this.#disposed) return () => {};
|
||||
this.#providerListeners.add(listener);
|
||||
@@ -96,6 +135,16 @@ export class PluginTerminalProviderRegistry {
|
||||
const generation = this.#providerListGeneration;
|
||||
const request = (async () => {
|
||||
try {
|
||||
if (
|
||||
this.#bridgeAvailability === 'unknown'
|
||||
&& typeof this.#statusBridge.getPluginRuntimeStatus === 'function'
|
||||
) {
|
||||
const available = await this.#probeRuntimeStatus(generation);
|
||||
if (!available) return Object.freeze([]);
|
||||
}
|
||||
if (this.#isUnavailable()) {
|
||||
return Object.freeze([]);
|
||||
}
|
||||
const providers = freezeValue(await this.#bridge.listPluginTerminalProviders(query));
|
||||
if (generation === this.#providerListGeneration) {
|
||||
if (this.#bridgeAvailability !== 'unavailable') this.#bridgeAvailability = 'available';
|
||||
@@ -206,11 +255,21 @@ export class PluginTerminalProviderRegistry {
|
||||
|
||||
async publishSessionEvent(event: NetcattyTerminalSessionEvent): Promise<void> {
|
||||
if (this.#disposed) return;
|
||||
// Short-circuit BEFORE mergeLifecycleSessionSnapshot so disabled plugin
|
||||
// hosts never run structuredClone/Object.freeze or retain session snapshots.
|
||||
if (this.#bridgeAvailability === 'unavailable') return;
|
||||
if (
|
||||
this.#bridgeAvailability === 'unknown'
|
||||
&& typeof this.#statusBridge.getPluginRuntimeStatus === 'function'
|
||||
) {
|
||||
const available = await this.#probeRuntimeStatus(this.#providerListGeneration);
|
||||
if (!available) return;
|
||||
}
|
||||
if (this.#isUnavailable()) return;
|
||||
const previous = this.#sessionSnapshots.get(event.session.sessionId);
|
||||
const session = mergeLifecycleSessionSnapshot(previous, event);
|
||||
if (event.type === 'disposed') this.#sessionSnapshots.delete(session.sessionId);
|
||||
else this.#sessionSnapshots.set(session.sessionId, session);
|
||||
if (this.#bridgeAvailability === 'unavailable') return;
|
||||
try {
|
||||
await this.#bridge.publishPluginTerminalSessionEvent(freezeValue({ ...event, session }));
|
||||
this.#bridgeAvailability = 'available';
|
||||
@@ -302,10 +361,13 @@ export function createWindowPluginTerminalProviderRegistry(
|
||||
providePluginTerminal: (request) => bridge.providePluginTerminal!(request),
|
||||
cancelPluginTerminalRequest: (requestId) => bridge.cancelPluginTerminalRequest!(requestId),
|
||||
publishPluginTerminalSessionEvent: (event) => bridge.publishPluginTerminalSessionEvent!(event),
|
||||
getPluginRuntimeStatus: bridge.getPluginRuntimeStatus
|
||||
? () => bridge.getPluginRuntimeStatus!()
|
||||
: undefined,
|
||||
onPluginContributionsChanged: bridge.onPluginContributionsChanged
|
||||
? (callback) => bridge.onPluginContributionsChanged!(callback)
|
||||
: undefined,
|
||||
});
|
||||
}, bridge);
|
||||
}
|
||||
|
||||
let windowRegistry: PluginTerminalProviderRegistry | null | undefined;
|
||||
|
||||
Reference in new issue
Block a user