| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289 |
- import type {
- PluginHookCommandHandler,
- PluginHookDecision,
- PluginHookEventInput,
- PluginHookRunResult,
- PluginHookLogger,
- PluginHookObserver,
- PluginHookSessionStartSource,
- PluginSubagentRegistration,
- } from './contracts.js';
- import { cleanupPluginHookSessionArtifacts } from './output-artifacts.js';
- import { mergePluginHookDecisions, PluginHookRunner } from './runner.js';
- const SESSION_IDLE_MS = 30 * 60 * 1_000;
- const SESSION_END_BUDGET_MS = 3_000;
- const SESSION_END_REPORT_BUDGET_MS = 1_000;
- const SESSION_END_CONCURRENCY = 4;
- const SESSION_END_FENCE_RETRY_MS = 1_000;
- const SESSION_END_RESUME_DRAIN_BUDGET_MS = 2_000;
- const MAX_RESUMABLE_SESSION_MARKERS = 1_024;
- type PluginHookSessionEndReason = 'archive' | 'clear' | 'logout' | 'resume_other' | 'idle_timeout';
- export interface PluginHookSessionEndFence {
- run(input: {
- readonly sessionId: string;
- readonly reason: PluginHookSessionEndReason;
- readonly idleSinceMs?: number;
- readonly sessionOwnershipClaim?: string;
- readonly operation: (signal?: AbortSignal) => Promise<PluginHookRunResult | undefined>;
- }): Promise<
- | { readonly status: 'executed'; readonly result?: PluginHookRunResult }
- | { readonly status: 'deferred'; readonly latestActivityAtMs?: number }
- | { readonly status: 'superseded' }
- >;
- }
- export interface PluginHookAdmissionTransaction {
- commit(): Promise<void>;
- rollback(): Promise<void>;
- }
- interface ActivePlugin {
- readonly pluginName: string;
- readonly handlers: readonly PluginHookCommandHandler[];
- readonly cwd: string;
- readonly transcriptPath?: string | null;
- readonly codexTranscriptPath?: string | null;
- readonly model?: string;
- readonly permissionMode?: string;
- readonly effort?: PluginHookEventInput['effort'];
- readonly promptId?: string;
- }
- interface SessionState {
- readonly plugins: Map<string, ActivePlugin>;
- sessionOwnershipClaim?: string;
- pendingSessionEnd?: {
- readonly reason: Exclude<PluginHookSessionEndReason, 'idle_timeout'>;
- readonly enabledPluginNames?: ReadonlySet<string>;
- };
- transcriptCleanup?: () => Promise<void>;
- sessionEndCleanup?: () => Promise<void>;
- pendingCompactPluginIdentities?: ReadonlySet<string>;
- idleSinceMs?: number;
- suspendedIdleSinceMs?: number;
- turnActive: boolean;
- idleTimer?: ReturnType<typeof setTimeout>;
- idleEpoch: number;
- idleEndController?: AbortController;
- }
- export interface PluginHookCoordinatorOptions {
- /**
- * Desktop owns its 30-minute SessionEnd timer in-process. Cloud disables
- * this timer and runs SessionEnd from the sandbox idle-release fence so the
- * Hook still has a live sandbox in which to execute.
- */
- readonly automaticIdleSessionEnd?: boolean;
- /**
- * Best-effort host projection for SessionEnd output. Delivery is awaited
- * within a short independent budget so Cloud can persist the warning before
- * sandbox release without making Desktop lifecycle actions wait forever.
- */
- readonly onSessionEndResult?: (input: {
- readonly sessionId: string;
- readonly result?: PluginHookRunResult;
- }) => void | Promise<void>;
- }
- /**
- * Owns Plugin activation and the 30-minute idle SessionEnd rule. Turn callers
- * still pass their immutable handler snapshot to every event; this class never
- * re-reads mutable Plugin state in the middle of a turn.
- */
- export class PluginHookCoordinator {
- private readonly sessions = new Map<string, SessionState>();
- private readonly endedSessions = new Set<string>();
- private readonly subagents = new Map<string, PluginSubagentRegistration>();
- private readonly idleEndControllers = new Map<string, AbortController>();
- private readonly idleEndTasks = new Map<string, Promise<void>>();
- private readonly explicitEndControllers = new Map<string, AbortController>();
- private readonly explicitEndTasks = new Map<string, Promise<PluginHookRunResult | undefined>>();
- private readonly forcedAutomaticCompactions = new Set<string>();
- private readonly pendingSessionStartSources = new Map<
- string,
- Exclude<PluginHookSessionStartSource, 'compact' | 'plugin_activation'>
- >();
- private readonly automaticIdleSessionEnd: boolean;
- private readonly onSessionEndResult?: PluginHookCoordinatorOptions['onSessionEndResult'];
- private resolveEnabledPluginNames?: () => ReadonlySet<string>;
- private sessionEndFence?: PluginHookSessionEndFence;
- constructor(
- private readonly runner: PluginHookRunner = new PluginHookRunner(),
- options: PluginHookCoordinatorOptions = {},
- ) {
- this.automaticIdleSessionEnd = options.automaticIdleSessionEnd ?? true;
- this.onSessionEndResult = options.onSessionEndResult;
- }
- configureObservability(input: {
- readonly logger?: PluginHookLogger;
- readonly observer?: PluginHookObserver;
- }): void {
- this.runner.configure(input);
- }
- setEnabledPluginResolver(resolve: (() => ReadonlySet<string>) | undefined): void {
- this.resolveEnabledPluginNames = resolve;
- }
- setSessionEndFence(fence: PluginHookSessionEndFence | undefined): void {
- this.sessionEndFence = fence;
- }
- /** Binds a prepared durable owner before its activation CAS is committed. */
- bindSessionOwnershipClaim(sessionId: string, ownershipClaimId: string): string | undefined {
- const state = this.sessions.get(sessionId);
- if (!state) return undefined;
- const previousClaim = state.sessionOwnershipClaim;
- state.sessionOwnershipClaim = ownershipClaimId;
- return previousClaim;
- }
- /** Restores the prior owner only when the failed candidate is still bound. */
- rollbackSessionOwnershipClaim(
- sessionId: string,
- failedOwnershipClaimId: string,
- previousOwnershipClaimId?: string,
- ): void {
- const state = this.sessions.get(sessionId);
- if (!state || state.sessionOwnershipClaim !== failedOwnershipClaimId) return;
- if (previousOwnershipClaimId) state.sessionOwnershipClaim = previousOwnershipClaimId;
- else delete state.sessionOwnershipClaim;
- }
- activeSessionIds(): readonly string[] {
- return [...this.sessions.keys()];
- }
- /** Returns root sessions plus every linked child execution that must be fenced with them. */
- activeExecutionSessionIds(rootSessionId?: string): readonly string[] {
- if (!rootSessionId) {
- return [...new Set([...this.sessions.keys(), ...this.subagents.keys()])];
- }
- const result = new Set<string>([rootSessionId]);
- let changed = true;
- while (changed) {
- changed = false;
- for (const registration of this.subagents.values()) {
- if (!result.has(registration.parentSessionId) || result.has(registration.childSessionId)) {
- continue;
- }
- result.add(registration.childSessionId);
- changed = true;
- }
- }
- return [...result];
- }
- hasActiveDescendantExecutions(rootSessionId: string): boolean {
- return this.activeExecutionSessionIds(rootSessionId).length > 1;
- }
- isSubagentSession(sessionId: string): boolean {
- return this.subagents.has(sessionId);
- }
- markNextSessionStart(
- sessionId: string,
- source: Exclude<PluginHookSessionStartSource, 'compact' | 'plugin_activation'>,
- ): void {
- this.pendingSessionStartSources.delete(sessionId);
- this.pendingSessionStartSources.set(sessionId, source);
- if (this.pendingSessionStartSources.size <= MAX_RESUMABLE_SESSION_MARKERS) return;
- const oldest = this.pendingSessionStartSources.keys().next().value as string | undefined;
- if (oldest) this.pendingSessionStartSources.delete(oldest);
- }
- deactivatePlugin(pluginName: string): void {
- for (const [sessionId, state] of this.sessions) {
- const removed = new Set<string>();
- for (const [identity, plugin] of state.plugins) {
- if (plugin.pluginName === pluginName) {
- void cleanupPluginHookSessionArtifacts(plugin.handlers, sessionId);
- state.plugins.delete(identity);
- removed.add(identity);
- }
- }
- if (state.pendingCompactPluginIdentities && removed.size > 0) {
- state.pendingCompactPluginIdentities = new Set(
- [...state.pendingCompactPluginIdentities].filter((identity) => !removed.has(identity)),
- );
- if (state.pendingCompactPluginIdentities.size === 0) {
- state.pendingCompactPluginIdentities = undefined;
- }
- }
- }
- }
- async beginTurn(input: {
- readonly handlers: readonly PluginHookCommandHandler[];
- readonly sessionId: string;
- readonly turnId: string;
- readonly cwd: string;
- readonly prompt: string;
- readonly transcriptPath?: string | null;
- readonly codexTranscriptPath?: string | null;
- readonly model?: string;
- readonly permissionMode?: string;
- readonly effort?: PluginHookEventInput['effort'];
- readonly promptId?: string;
- readonly resumeExistingSession?: boolean;
- readonly sessionStartSource?: Exclude<
- PluginHookSessionStartSource,
- 'compact' | 'plugin_activation'
- >;
- readonly transcriptCleanup?: () => Promise<void>;
- /** Host-owned Session state cleared only after the logical SessionEnd Hook finishes. */
- readonly sessionEndCleanup?: () => Promise<void>;
- readonly sessionOwnershipClaim?: string;
- readonly captureAdmissionTransaction?: (transaction: PluginHookAdmissionTransaction) => void;
- readonly signal?: AbortSignal;
- }): Promise<PluginHookRunResult> {
- await this.drainExplicitSessionEndBeforeTurn(input.sessionId);
- const inherited = this.subagents.get(input.sessionId);
- if (inherited) return { decision: { decision: 'allow' }, diagnostics: [] };
- const idleEnd = this.idleEndTasks.get(input.sessionId);
- this.idleEndControllers.get(input.sessionId)?.abort('A new turn superseded idle SessionEnd');
- if (idleEnd) await idleEnd;
- const previousState = this.sessions.get(input.sessionId);
- const previousSnapshot = previousState ? snapshotSessionState(previousState) : undefined;
- const previousPendingStartSource = this.pendingSessionStartSources.get(input.sessionId);
- const wasEnded = this.endedSessions.has(input.sessionId);
- const wasKnownSession = previousState !== undefined;
- const isResume = this.endedSessions.delete(input.sessionId);
- const state = this.session(input.sessionId);
- let removedHandlersForCommit: readonly PluginHookCommandHandler[] = [];
- let admissionSettled = false;
- input.captureAdmissionTransaction?.({
- commit: async () => {
- if (admissionSettled) return;
- admissionSettled = true;
- if (removedHandlersForCommit.length > 0) {
- await cleanupPluginHookSessionArtifacts(removedHandlersForCommit, input.sessionId);
- }
- },
- rollback: async () => {
- if (admissionSettled) return;
- admissionSettled = true;
- await this.rollbackTurnAdmission({
- sessionId: input.sessionId,
- state,
- previousSnapshot,
- previousPendingStartSource,
- wasEnded,
- });
- },
- });
- if (input.sessionOwnershipClaim) state.sessionOwnershipClaim = input.sessionOwnershipClaim;
- if (input.transcriptCleanup) state.transcriptCleanup = input.transcriptCleanup;
- if (input.sessionEndCleanup) state.sessionEndCleanup = input.sessionEndCleanup;
- state.idleEpoch += 1;
- state.suspendedIdleSinceMs = state.idleSinceMs;
- state.idleSinceMs = undefined;
- state.turnActive = true;
- if (state.idleTimer) clearTimeout(state.idleTimer);
- state.idleTimer = undefined;
- this.idleEndControllers.delete(input.sessionId);
- state.idleEndController?.abort('A new turn superseded idle SessionEnd');
- state.idleEndController = undefined;
- const currentPlugins = groupByPlugin(input.handlers, input);
- const removedHandlers: PluginHookCommandHandler[] = [];
- for (const identity of [...state.plugins.keys()]) {
- if (currentPlugins.has(identity)) continue;
- const removed = state.plugins.get(identity);
- if (removed) removedHandlers.push(...removed.handlers);
- state.plugins.delete(identity);
- }
- if (removedHandlers.length > 0 && !input.captureAdmissionTransaction) {
- await cleanupPluginHookSessionArtifacts(removedHandlers, input.sessionId);
- } else {
- removedHandlersForCommit = removedHandlers;
- }
- const compactIdentities = state.pendingCompactPluginIdentities;
- const compacting = compactIdentities
- ? [...currentPlugins.entries()].filter(([identity]) => compactIdentities.has(identity))
- : [];
- const starting = [...currentPlugins.entries()].filter(
- ([identity]) => !state.plugins.has(identity) && !compactIdentities?.has(identity),
- );
- const pendingStartSource = this.pendingSessionStartSources.get(input.sessionId);
- this.pendingSessionStartSources.delete(input.sessionId);
- const startSource = resolveSessionStartSource(
- wasKnownSession,
- isResume || input.resumeExistingSession === true,
- pendingStartSource ?? (wasKnownSession ? undefined : input.sessionStartSource),
- );
- const startResults: PluginHookRunResult[] = [];
- let compactResult: PluginHookRunResult | undefined;
- if (compactIdentities) {
- state.pendingCompactPluginIdentities = undefined;
- if (compacting.length > 0) {
- compactResult = await this.runner.run(
- compacting.flatMap(([, plugin]) => plugin.handlers),
- eventInput('SessionStart', input, { source: 'compact' }, 'compact'),
- input.signal,
- );
- startResults.push(compactResult);
- if (stopsTurnLifecycle(compactResult)) {
- return mergeRunResults(startResults);
- }
- }
- }
- let activationResult: PluginHookRunResult | undefined;
- if (starting.length > 0) {
- activationResult = await this.runner.run(
- starting.flatMap(([, plugin]) => plugin.handlers),
- eventInput('SessionStart', input, { source: startSource }, startSource),
- input.signal,
- );
- startResults.push(activationResult);
- }
- const compactAborted = compactResult?.diagnostics.some(
- (diagnostic) => diagnostic.code === 'HOOK_ABORTED',
- );
- if (compactAborted && compactIdentities) {
- state.pendingCompactPluginIdentities = compactIdentities;
- }
- const activationAborted = activationResult?.diagnostics.some(
- (diagnostic) => diagnostic.code === 'HOOK_ABORTED',
- );
- for (const [identity, plugin] of currentPlugins) {
- if (state.plugins.has(identity) || !activationAborted) state.plugins.set(identity, plugin);
- }
- if (activationResult && stopsTurnLifecycle(activationResult)) {
- return mergeRunResults(startResults);
- }
- const promptResult = await this.runner.run(
- input.handlers,
- eventInput('UserPromptSubmit', input, { prompt: input.prompt }),
- input.signal,
- );
- return mergeRunResults([...startResults, promptResult]);
- }
- runEvent(
- handlers: readonly PluginHookCommandHandler[],
- input: PluginHookEventInput,
- signal?: AbortSignal,
- ): Promise<PluginHookRunResult> {
- const inherited = this.subagents.get(input.sessionId);
- if (!inherited) return this.runner.run(handlers, input, signal);
- return this.runner.run(
- inherited.handlers,
- {
- ...input,
- promptId: inherited.parentPromptId ?? inherited.parentTurnId,
- model: input.model ?? inherited.model,
- permissionMode: input.permissionMode ?? inherited.permissionMode,
- effort: input.effort ?? inherited.effort,
- subagentContext: registrationSubagentContext(inherited),
- payload: {
- ...(input.payload ?? {}),
- agent_id: inherited.agentId,
- agent_type: inherited.agentType,
- },
- },
- signal,
- );
- }
- async finishTurn(input: {
- readonly handlers: readonly PluginHookCommandHandler[];
- readonly sessionId: string;
- readonly turnId: string;
- readonly cwd: string;
- readonly transcriptPath?: string | null;
- readonly codexTranscriptPath?: string | null;
- readonly model?: string;
- readonly permissionMode?: string;
- readonly effort?: PluginHookEventInput['effort'];
- readonly promptId?: string;
- readonly stopHookActive?: boolean;
- readonly lastAssistantMessage?: string | null;
- readonly signal?: AbortSignal;
- }): Promise<PluginHookRunResult> {
- const inherited = this.subagents.get(input.sessionId);
- const hookInput = inherited
- ? {
- event: 'SubagentStop' as const,
- sessionId: input.sessionId,
- turnId: input.turnId,
- transcriptPath: inherited.agentTranscriptPath,
- codexTranscriptPath: inherited.agentCodexTranscriptPath,
- promptId: inherited.parentPromptId ?? inherited.parentTurnId,
- subagentContext: registrationSubagentContext(inherited),
- cwd: input.cwd,
- model: input.model ?? inherited.model,
- permissionMode: input.permissionMode ?? inherited.permissionMode,
- effort: input.effort ?? inherited.effort,
- matcherValue: inherited.agentType,
- payload: {
- stop_hook_active: input.stopHookActive ?? false,
- agent_id: inherited.agentId,
- agent_type: inherited.agentType,
- agent_transcript_path: inherited.agentTranscriptPath ?? null,
- last_assistant_message: input.lastAssistantMessage ?? null,
- },
- }
- : eventInput('Stop', input, {
- stop_hook_active: input.stopHookActive ?? false,
- last_assistant_message: input.lastAssistantMessage ?? null,
- });
- const result = await this.runner.run(
- inherited?.handlers ?? input.handlers,
- hookInput,
- input.signal,
- );
- return result;
- }
- completeTurn(sessionId: string): void {
- if (this.subagents.has(sessionId)) {
- this.releaseSubagent(sessionId);
- return;
- }
- const state = this.sessions.get(sessionId);
- if (state) {
- state.turnActive = false;
- state.suspendedIdleSinceMs = undefined;
- state.idleSinceMs = Date.now();
- const pending = state.pendingSessionEnd;
- this.scheduleSessionEnd(
- sessionId,
- state,
- pending ? SESSION_END_FENCE_RETRY_MS : SESSION_IDLE_MS,
- pending?.reason ?? 'idle_timeout',
- pending?.enabledPluginNames,
- );
- }
- }
- /** Suspends idle SessionEnd while a process-local compaction is active. */
- beginSessionActivity(sessionId: string, _turnId: string): void {
- const state = this.sessions.get(sessionId);
- if (!state) return;
- state.idleEpoch += 1;
- state.suspendedIdleSinceMs = state.idleSinceMs;
- state.idleSinceMs = undefined;
- state.turnActive = true;
- if (state.idleTimer) clearTimeout(state.idleTimer);
- state.idleTimer = undefined;
- }
- completeSessionActivity(sessionId: string): void {
- this.completeTurn(sessionId);
- }
- /** Restores the previous idle anchor after a denied, aborted, or failed turn. */
- resumeIdleCountdown(sessionId: string): void {
- if (this.subagents.has(sessionId)) return;
- const state = this.sessions.get(sessionId);
- if (!state) return;
- state.turnActive = false;
- if (state.idleSinceMs === undefined) {
- state.idleSinceMs = state.suspendedIdleSinceMs ?? Date.now();
- }
- state.suspendedIdleSinceMs = undefined;
- if (this.hasActiveDescendantExecutions(sessionId)) return;
- const pending = state.pendingSessionEnd;
- const remainingMs = pending
- ? SESSION_END_FENCE_RETRY_MS
- : Math.max(0, state.idleSinceMs + SESSION_IDLE_MS - Date.now());
- this.scheduleSessionEnd(
- sessionId,
- state,
- remainingMs,
- pending?.reason ?? 'idle_timeout',
- pending?.enabledPluginNames,
- );
- }
- markCompacted(sessionId: string, handlers: readonly PluginHookCommandHandler[]): void {
- this.session(sessionId).pendingCompactPluginIdentities = new Set(handlers.map(pluginIdentity));
- }
- admitAutomaticCompaction(sessionId: string, requestedDefer: boolean): boolean {
- const forced = this.forcedAutomaticCompactions.delete(sessionId);
- if (!requestedDefer) return true;
- if (forced) return true;
- this.forcedAutomaticCompactions.add(sessionId);
- return false;
- }
- completeAutomaticCompaction(sessionId: string): void {
- this.forcedAutomaticCompactions.delete(sessionId);
- }
- clearAutomaticCompactionDeferral(sessionId: string): void {
- this.forcedAutomaticCompactions.delete(sessionId);
- }
- handlersForTurn(
- sessionId: string,
- fallback: readonly PluginHookCommandHandler[],
- ): readonly PluginHookCommandHandler[] {
- return this.subagents.get(sessionId)?.handlers ?? fallback;
- }
- async startSubagent(
- registration: PluginSubagentRegistration,
- signal?: AbortSignal,
- ): Promise<PluginHookDecision> {
- this.subagents.set(registration.childSessionId, registration);
- const result = await this.runner.run(
- registration.handlers,
- {
- event: 'SubagentStart',
- sessionId: registration.childSessionId,
- turnId: registration.childTurnId,
- promptId: registration.parentPromptId ?? registration.parentTurnId,
- transcriptPath: registration.agentTranscriptPath,
- codexTranscriptPath: registration.agentCodexTranscriptPath,
- subagentContext: registrationSubagentContext(registration),
- cwd: registration.cwd,
- model: registration.model,
- permissionMode: registration.permissionMode,
- effort: registration.effort,
- matcherValue: registration.agentType,
- payload: {
- agent_id: registration.agentId,
- agent_type: registration.agentType,
- },
- },
- signal,
- );
- return result.decision;
- }
- cancelSubagent(childSessionId: string): void {
- this.releaseSubagent(childSessionId);
- }
- async endSession(
- sessionId: string,
- reason: PluginHookSessionEndReason,
- signal?: AbortSignal,
- enabledPluginNames?: ReadonlySet<string>,
- ): Promise<PluginHookRunResult | undefined> {
- if (reason !== 'idle_timeout') {
- const current = this.explicitEndTasks.get(sessionId);
- if (current) return current;
- const controller = new AbortController();
- this.explicitEndControllers.set(sessionId, controller);
- const task = this.performSessionEnd(
- sessionId,
- reason,
- combineAbortSignals(signal, controller.signal),
- enabledPluginNames,
- ).finally(() => {
- if (this.explicitEndTasks.get(sessionId) === task) {
- this.explicitEndTasks.delete(sessionId);
- }
- if (this.explicitEndControllers.get(sessionId) === controller) {
- this.explicitEndControllers.delete(sessionId);
- }
- });
- this.explicitEndTasks.set(sessionId, task);
- return task;
- }
- return this.performSessionEnd(sessionId, reason, signal, enabledPluginNames);
- }
- private async performSessionEnd(
- sessionId: string,
- reason: PluginHookSessionEndReason,
- signal?: AbortSignal,
- enabledPluginNames?: ReadonlySet<string>,
- ): Promise<PluginHookRunResult | undefined> {
- const state = this.sessions.get(sessionId);
- if (!state) return;
- if (reason !== 'idle_timeout' && !state.pendingSessionEnd) {
- state.pendingSessionEnd = {
- reason,
- ...(enabledPluginNames ? { enabledPluginNames: new Set(enabledPluginNames) } : {}),
- };
- }
- const pending = state.pendingSessionEnd;
- const effectiveReason = pending?.reason ?? reason;
- const effectiveEnabledPluginNames = pending?.enabledPluginNames ?? enabledPluginNames;
- // An explicit lifecycle action is allowed to return before an active Turn
- // drains, but SessionEnd itself must not run concurrently with that Turn.
- // Keep retrying the request; completeTurn/resumeIdleCountdown will also
- // promptly re-arm it when the local Turn actually releases.
- if (
- effectiveReason !== 'idle_timeout' &&
- (state.turnActive || this.hasActiveDescendantExecutions(sessionId))
- ) {
- this.scheduleSessionEnd(
- sessionId,
- state,
- SESSION_END_FENCE_RETRY_MS,
- effectiveReason,
- effectiveEnabledPluginNames,
- );
- return undefined;
- }
- const operation = (fenceSignal?: AbortSignal) =>
- this.finishSessionEnd(
- sessionId,
- state,
- effectiveReason,
- combineAbortSignals(signal, fenceSignal),
- effectiveEnabledPluginNames,
- );
- if (!this.sessionEndFence) return operation();
- let outcome: Awaited<ReturnType<PluginHookSessionEndFence['run']>>;
- try {
- outcome = await this.sessionEndFence.run({
- sessionId,
- reason: effectiveReason,
- ...(state.idleSinceMs !== undefined ? { idleSinceMs: state.idleSinceMs } : {}),
- ...(state.sessionOwnershipClaim
- ? { sessionOwnershipClaim: state.sessionOwnershipClaim }
- : {}),
- operation,
- });
- } catch {
- outcome = { status: 'deferred' };
- }
- if (outcome.status === 'executed') return outcome.result;
- if (outcome.status === 'superseded') {
- await this.abandonSupersededSession(sessionId, state);
- return undefined;
- }
- if (this.sessions.get(sessionId) === state) {
- if (outcome.latestActivityAtMs !== undefined) {
- state.idleSinceMs = Math.max(state.idleSinceMs ?? 0, outcome.latestActivityAtMs);
- }
- const delayMs =
- effectiveReason === 'idle_timeout' && state.idleSinceMs !== undefined
- ? Math.max(SESSION_END_FENCE_RETRY_MS, state.idleSinceMs + SESSION_IDLE_MS - Date.now())
- : SESSION_END_FENCE_RETRY_MS;
- if (effectiveReason !== 'idle_timeout' || !state.turnActive) {
- this.scheduleSessionEnd(
- sessionId,
- state,
- delayMs,
- effectiveReason,
- effectiveEnabledPluginNames,
- );
- }
- }
- return undefined;
- }
- private async finishSessionEnd(
- sessionId: string,
- state: SessionState,
- reason: PluginHookSessionEndReason,
- signal: AbortSignal | undefined,
- enabledPluginNames: ReadonlySet<string> | undefined,
- ): Promise<PluginHookRunResult | undefined> {
- if (this.sessions.get(sessionId) !== state) return undefined;
- this.sessions.delete(sessionId);
- this.forcedAutomaticCompactions.delete(sessionId);
- this.markEnded(sessionId);
- if (state.idleTimer) clearTimeout(state.idleTimer);
- if (reason !== 'idle_timeout' && state.idleEndController) {
- state.idleEndController.abort('Session ended explicitly');
- }
- state.idleSinceMs = undefined;
- const enabled = enabledPluginNames ?? this.resolveEnabledPluginNames?.();
- const plugins = [...state.plugins.values()].filter(
- (plugin) => !enabled || enabled.has(plugin.pluginName),
- );
- const firstPlugin = plugins[0];
- let result: PluginHookRunResult | undefined;
- try {
- if (firstPlugin) {
- result = await this.runner.run(
- plugins.flatMap((plugin) => plugin.handlers),
- {
- event: 'SessionEnd',
- sessionId,
- transcriptPath: firstPlugin.transcriptPath,
- codexTranscriptPath: firstPlugin.codexTranscriptPath,
- cwd: firstPlugin.cwd,
- model: firstPlugin.model,
- permissionMode: firstPlugin.permissionMode,
- effort: firstPlugin.effort,
- promptId: firstPlugin.promptId,
- matcherValue: reason,
- payload: { reason },
- },
- signal,
- );
- }
- } finally {
- await this.reportSessionEndResult(sessionId, result);
- await cleanupSessionResources(state, sessionId);
- const idleController = this.idleEndControllers.get(sessionId);
- if (idleController?.signal === signal) this.idleEndControllers.delete(sessionId);
- }
- return result;
- }
- private async abandonSupersededSession(sessionId: string, state: SessionState): Promise<void> {
- if (this.sessions.get(sessionId) !== state) return;
- this.sessions.delete(sessionId);
- this.forcedAutomaticCompactions.delete(sessionId);
- this.markEnded(sessionId);
- if (state.idleTimer) clearTimeout(state.idleTimer);
- // The newer owner owns the logical SessionEnd command, but it cannot clear
- // Session-scoped permissions held in this stale process.
- await this.reportSessionEndResult(sessionId, undefined);
- await cleanupSessionResources(state, sessionId);
- }
- /**
- * Runs an idle SessionEnd as a cancellable admission fence. A new turn for
- * the same session aborts and drains this task before SessionStart, avoiding
- * overlapping old-session cleanup with a resumed turn.
- */
- async endIdleSession(
- sessionId: string,
- signal?: AbortSignal,
- enabledPluginNames?: ReadonlySet<string>,
- ): Promise<void> {
- const current = this.idleEndTasks.get(sessionId);
- if (current) return current;
- const state = this.sessions.get(sessionId);
- if (!state || state.turnActive || this.hasActiveDescendantExecutions(sessionId)) return;
- const controller = new AbortController();
- const abort = () => controller.abort(signal?.reason);
- if (signal?.aborted) abort();
- else signal?.addEventListener('abort', abort, { once: true });
- state.idleEndController = controller;
- this.idleEndControllers.set(sessionId, controller);
- const task = this.endSession(
- sessionId,
- 'idle_timeout',
- controller.signal,
- enabledPluginNames,
- ).then(() => undefined);
- this.idleEndTasks.set(sessionId, task);
- try {
- await task;
- } finally {
- signal?.removeEventListener('abort', abort);
- if (this.idleEndTasks.get(sessionId) === task) this.idleEndTasks.delete(sessionId);
- if (this.idleEndControllers.get(sessionId) === controller) {
- this.idleEndControllers.delete(sessionId);
- }
- }
- }
- /**
- * Ends the idle sessions owned by one sandbox release fence without
- * creating an unbounded burst of Hook processes. The shared deadline is a
- * release budget, not a per-session extension: remaining sessions are left
- * for the sandbox teardown once the budget expires.
- */
- async endIdleSessions(
- sessionIds: readonly string[],
- signal?: AbortSignal,
- enabledPluginNames?: ReadonlySet<string>,
- ): Promise<void> {
- const pendingSessions = [...new Set(sessionIds)].flatMap((sessionId) => {
- const state = this.sessions.get(sessionId);
- return state ? [{ sessionId, state, idleEpoch: state.idleEpoch }] : [];
- });
- const controller = new AbortController();
- const abort = () => controller.abort(signal?.reason);
- if (signal?.aborted) abort();
- else signal?.addEventListener('abort', abort, { once: true });
- const timer = setTimeout(
- () => controller.abort('Plugin SessionEnd release deadline exceeded'),
- SESSION_END_BUDGET_MS,
- );
- let cursor = 0;
- try {
- await Promise.all(
- Array.from(
- { length: Math.min(SESSION_END_CONCURRENCY, pendingSessions.length) },
- async () => {
- while (cursor < pendingSessions.length && !controller.signal.aborted) {
- const candidate = pendingSessions[cursor];
- cursor += 1;
- if (!candidate) continue;
- const current = this.sessions.get(candidate.sessionId);
- if (
- current !== candidate.state ||
- current.idleEpoch !== candidate.idleEpoch ||
- current.turnActive ||
- this.hasActiveDescendantExecutions(candidate.sessionId)
- ) {
- continue;
- }
- await this.endIdleSession(candidate.sessionId, controller.signal, enabledPluginNames);
- }
- },
- ),
- );
- } finally {
- clearTimeout(timer);
- signal?.removeEventListener('abort', abort);
- }
- }
- async endAllSessions(
- reason: 'logout',
- signal?: AbortSignal,
- enabledPluginNames?: ReadonlySet<string>,
- ): Promise<void> {
- const sessionIds = [...this.sessions.keys()];
- const controller = new AbortController();
- const abort = () => controller.abort(signal?.reason);
- signal?.addEventListener('abort', abort, { once: true });
- if (signal?.aborted) abort();
- const timer = setTimeout(
- () => controller.abort('Plugin SessionEnd total deadline exceeded'),
- SESSION_END_BUDGET_MS,
- );
- let cursor = 0;
- try {
- await Promise.all(
- Array.from({ length: Math.min(SESSION_END_CONCURRENCY, sessionIds.length) }, async () => {
- while (cursor < sessionIds.length && !controller.signal.aborted) {
- const sessionId = sessionIds[cursor];
- cursor += 1;
- if (sessionId) {
- await this.endSession(sessionId, reason, controller.signal, enabledPluginNames);
- }
- }
- }),
- );
- } finally {
- clearTimeout(timer);
- signal?.removeEventListener('abort', abort);
- this.pruneSubagentsWithoutActiveRoot();
- // The foreground logout sweep has a fixed UX budget. Every session still
- // present (active, deferred, or not reached by a worker) must retry in
- // the background with its original explicit reason; deleting it would
- // skip both SessionEnd and session-permission cleanup.
- for (const sessionId of sessionIds) {
- const state = this.sessions.get(sessionId);
- if (!state) continue;
- if (!state.pendingSessionEnd) {
- state.pendingSessionEnd = {
- reason,
- ...(enabledPluginNames ? { enabledPluginNames: new Set(enabledPluginNames) } : {}),
- };
- }
- this.scheduleSessionEnd(
- sessionId,
- state,
- SESSION_END_FENCE_RETRY_MS,
- state.pendingSessionEnd.reason,
- state.pendingSessionEnd.enabledPluginNames,
- );
- }
- }
- }
- async dispose(): Promise<void> {
- const sessionStates = [...this.sessions].map(([sessionId, state]) => ({ sessionId, state }));
- for (const state of this.sessions.values()) if (state.idleTimer) clearTimeout(state.idleTimer);
- for (const state of this.sessions.values()) state.idleEndController?.abort('disposed');
- for (const controller of this.idleEndControllers.values()) controller.abort('disposed');
- for (const controller of this.explicitEndControllers.values()) controller.abort('disposed');
- await Promise.allSettled([...this.idleEndTasks.values(), ...this.explicitEndTasks.values()]);
- this.sessions.clear();
- this.endedSessions.clear();
- this.subagents.clear();
- this.idleEndControllers.clear();
- this.idleEndTasks.clear();
- this.explicitEndControllers.clear();
- this.explicitEndTasks.clear();
- this.forcedAutomaticCompactions.clear();
- this.pendingSessionStartSources.clear();
- this.resolveEnabledPluginNames = undefined;
- this.sessionEndFence = undefined;
- await Promise.allSettled(
- sessionStates.map((state) => cleanupSessionResources(state.state, state.sessionId)),
- );
- await this.runner.dispose();
- }
- private session(sessionId: string): SessionState {
- const current = this.sessions.get(sessionId);
- if (current) return current;
- const created: SessionState = { plugins: new Map(), idleEpoch: 0, turnActive: false };
- this.sessions.set(sessionId, created);
- return created;
- }
- private async drainExplicitSessionEndBeforeTurn(sessionId: string): Promise<void> {
- const current = this.explicitEndTasks.get(sessionId);
- if (current) {
- this.explicitEndControllers
- .get(sessionId)
- ?.abort('A new turn is waiting for explicit SessionEnd cleanup');
- const drained = await settlesWithin(current, SESSION_END_RESUME_DRAIN_BUDGET_MS);
- if (!drained) {
- throw new Error(`Previous Plugin SessionEnd is still finalizing: ${sessionId}`);
- }
- }
- if (this.sessions.get(sessionId)?.pendingSessionEnd) {
- // A prior explicit request was deferred while its Turn drained. Its retry
- // owns SessionEnd; admitting a replacement Turn here could starve that
- // retry and overlap old Session cleanup with the new session generation.
- throw new Error(`Previous Plugin SessionEnd is pending: ${sessionId}`);
- }
- }
- private async rollbackTurnAdmission(input: {
- readonly sessionId: string;
- readonly state: SessionState;
- readonly previousSnapshot?: SessionState;
- readonly previousPendingStartSource?: Exclude<
- PluginHookSessionStartSource,
- 'compact' | 'plugin_activation'
- >;
- readonly wasEnded: boolean;
- }): Promise<void> {
- if (this.sessions.get(input.sessionId) !== input.state) return;
- if (input.state.idleTimer) clearTimeout(input.state.idleTimer);
- input.state.idleEndController?.abort('Plugin Hook admission rolled back');
- this.idleEndControllers.get(input.sessionId)?.abort('Plugin Hook admission rolled back');
- this.idleEndControllers.delete(input.sessionId);
- const currentHandlers = [...input.state.plugins.entries()]
- .filter(([identity]) => !input.previousSnapshot?.plugins.has(identity))
- .flatMap(([, plugin]) => plugin.handlers);
- const currentTranscriptCleanup = input.state.transcriptCleanup;
- const previousTranscriptCleanup = input.previousSnapshot?.transcriptCleanup;
- await Promise.allSettled([
- ...(currentTranscriptCleanup && currentTranscriptCleanup !== previousTranscriptCleanup
- ? [Promise.resolve().then(currentTranscriptCleanup)]
- : []),
- ...(currentHandlers.length > 0
- ? [cleanupPluginHookSessionArtifacts(currentHandlers, input.sessionId)]
- : []),
- ]);
- if (input.previousSnapshot) {
- const pendingSessionEnd =
- input.state.pendingSessionEnd ?? input.previousSnapshot.pendingSessionEnd;
- const restored = snapshotSessionState(input.previousSnapshot);
- restored.idleTimer = undefined;
- restored.idleEndController = undefined;
- if (pendingSessionEnd) restored.pendingSessionEnd = pendingSessionEnd;
- this.sessions.set(input.sessionId, restored);
- } else {
- this.sessions.delete(input.sessionId);
- }
- if (!this.pendingSessionStartSources.has(input.sessionId) && input.previousPendingStartSource) {
- this.pendingSessionStartSources.set(input.sessionId, input.previousPendingStartSource);
- }
- if (input.wasEnded) this.markEnded(input.sessionId);
- else this.endedSessions.delete(input.sessionId);
- }
- private pruneSubagentsWithoutActiveRoot(): void {
- for (const childSessionId of this.subagents.keys()) {
- let registration = this.subagents.get(childSessionId);
- const visited = new Set<string>([childSessionId]);
- let retained = false;
- while (registration) {
- if (this.sessions.has(registration.parentSessionId)) {
- retained = true;
- break;
- }
- if (visited.has(registration.parentSessionId)) break;
- visited.add(registration.parentSessionId);
- registration = this.subagents.get(registration.parentSessionId);
- }
- if (!retained) this.subagents.delete(childSessionId);
- }
- }
- private markEnded(sessionId: string): void {
- this.endedSessions.delete(sessionId);
- this.endedSessions.add(sessionId);
- if (this.endedSessions.size <= MAX_RESUMABLE_SESSION_MARKERS) return;
- const oldest = this.endedSessions.values().next().value as string | undefined;
- if (oldest) this.endedSessions.delete(oldest);
- }
- private releaseSubagent(childSessionId: string): void {
- const registration = this.subagents.get(childSessionId);
- if (!registration) return;
- this.subagents.delete(childSessionId);
- for (const [descendantId, descendant] of this.subagents) {
- if (descendant.parentSessionId !== childSessionId) continue;
- this.subagents.set(descendantId, {
- ...descendant,
- parentSessionId: registration.parentSessionId,
- parentTurnId: registration.parentTurnId,
- });
- }
- const parent = this.sessions.get(registration.parentSessionId);
- if (!parent || parent.turnActive || parent.idleSinceMs === undefined) return;
- const pending = parent.pendingSessionEnd;
- const remainingMs = pending
- ? SESSION_END_FENCE_RETRY_MS
- : Math.max(0, parent.idleSinceMs + SESSION_IDLE_MS - Date.now());
- this.scheduleSessionEnd(
- registration.parentSessionId,
- parent,
- remainingMs,
- pending?.reason ?? 'idle_timeout',
- pending?.enabledPluginNames,
- );
- }
- private scheduleSessionEnd(
- sessionId: string,
- state: SessionState,
- delayMs: number,
- reason: PluginHookSessionEndReason,
- enabledPluginNames?: ReadonlySet<string>,
- ): void {
- if (state.idleTimer) clearTimeout(state.idleTimer);
- if (!this.automaticIdleSessionEnd && reason === 'idle_timeout') {
- state.idleTimer = undefined;
- return;
- }
- if ([...this.subagents.values()].some((child) => child.parentSessionId === sessionId)) {
- state.idleTimer = undefined;
- return;
- }
- const epoch = ++state.idleEpoch;
- state.idleTimer = setTimeout(() => {
- if (this.sessions.get(sessionId) !== state || state.idleEpoch !== epoch) return;
- void (reason === 'idle_timeout'
- ? this.endIdleSession(
- sessionId,
- undefined,
- enabledPluginNames ?? this.resolveEnabledPluginNames?.(),
- )
- : this.endSession(
- sessionId,
- reason,
- undefined,
- enabledPluginNames ?? this.resolveEnabledPluginNames?.(),
- ));
- }, delayMs);
- state.idleTimer.unref?.();
- }
- private async reportSessionEndResult(
- sessionId: string,
- result: PluginHookRunResult | undefined,
- ): Promise<void> {
- if (!this.onSessionEndResult) return;
- try {
- await settleWithin(
- Promise.resolve(this.onSessionEndResult({ sessionId, ...(result ? { result } : {}) })),
- SESSION_END_REPORT_BUDGET_MS,
- );
- } catch {
- // User-visible diagnostics are best-effort and must never fail teardown.
- }
- }
- }
- function combineAbortSignals(
- caller: AbortSignal | undefined,
- fence: AbortSignal | undefined,
- ): AbortSignal | undefined {
- if (!caller) return fence;
- if (!fence) return caller;
- return AbortSignal.any([caller, fence]);
- }
- async function settleWithin(value: Promise<unknown>, timeoutMs: number): Promise<void> {
- let timer: ReturnType<typeof setTimeout> | undefined;
- await Promise.race([
- value.then(
- () => undefined,
- () => undefined,
- ),
- new Promise<void>((resolve) => {
- timer = setTimeout(resolve, timeoutMs);
- timer.unref?.();
- }),
- ]);
- if (timer) clearTimeout(timer);
- }
- async function settlesWithin(value: Promise<unknown>, timeoutMs: number): Promise<boolean> {
- let timer: ReturnType<typeof setTimeout> | undefined;
- try {
- return await Promise.race([
- value.then(
- () => true,
- () => true,
- ),
- new Promise<boolean>((resolve) => {
- timer = setTimeout(() => resolve(false), timeoutMs);
- timer.unref?.();
- }),
- ]);
- } finally {
- if (timer) clearTimeout(timer);
- }
- }
- async function cleanupSessionResources(state: SessionState, sessionId: string): Promise<void> {
- const cleanup = state.transcriptCleanup;
- state.transcriptCleanup = undefined;
- const sessionEndCleanup = state.sessionEndCleanup;
- state.sessionEndCleanup = undefined;
- const handlers = [...state.plugins.values()].flatMap((plugin) => plugin.handlers);
- await Promise.allSettled([
- ...(cleanup ? [Promise.resolve().then(cleanup)] : []),
- ...(sessionEndCleanup ? [Promise.resolve().then(sessionEndCleanup)] : []),
- cleanupPluginHookSessionArtifacts(handlers, sessionId),
- ]);
- }
- function snapshotSessionState(state: SessionState): SessionState {
- return {
- plugins: new Map(state.plugins),
- ...(state.sessionOwnershipClaim ? { sessionOwnershipClaim: state.sessionOwnershipClaim } : {}),
- ...(state.pendingSessionEnd
- ? {
- pendingSessionEnd: {
- reason: state.pendingSessionEnd.reason,
- ...(state.pendingSessionEnd.enabledPluginNames
- ? { enabledPluginNames: new Set(state.pendingSessionEnd.enabledPluginNames) }
- : {}),
- },
- }
- : {}),
- ...(state.transcriptCleanup ? { transcriptCleanup: state.transcriptCleanup } : {}),
- ...(state.sessionEndCleanup ? { sessionEndCleanup: state.sessionEndCleanup } : {}),
- ...(state.pendingCompactPluginIdentities
- ? { pendingCompactPluginIdentities: new Set(state.pendingCompactPluginIdentities) }
- : {}),
- ...(state.idleSinceMs !== undefined ? { idleSinceMs: state.idleSinceMs } : {}),
- ...(state.suspendedIdleSinceMs !== undefined
- ? { suspendedIdleSinceMs: state.suspendedIdleSinceMs }
- : {}),
- turnActive: state.turnActive,
- ...(state.idleTimer ? { idleTimer: state.idleTimer } : {}),
- idleEpoch: state.idleEpoch,
- ...(state.idleEndController ? { idleEndController: state.idleEndController } : {}),
- };
- }
- function registrationSubagentContext(
- registration: PluginSubagentRegistration,
- ): NonNullable<PluginHookEventInput['subagentContext']> {
- return {
- parentSessionId: registration.parentSessionId,
- parentTurnId: registration.parentTurnId,
- parentTranscriptPath: registration.parentTranscriptPath,
- parentCodexTranscriptPath: registration.parentCodexTranscriptPath,
- childSessionId: registration.childSessionId,
- childTranscriptPath: registration.agentTranscriptPath,
- childCodexTranscriptPath: registration.agentCodexTranscriptPath,
- };
- }
- function stopsTurnLifecycle(result: PluginHookRunResult): boolean {
- return result.decision.continue === false || result.decision.decision === 'deny';
- }
- function resolveSessionStartSource(
- wasKnownSession: boolean,
- isResume: boolean,
- explicitSource?: Exclude<PluginHookSessionStartSource, 'compact' | 'plugin_activation'>,
- ): PluginHookSessionStartSource {
- if (explicitSource) return explicitSource;
- if (wasKnownSession) return 'plugin_activation';
- return isResume ? 'resume' : 'startup';
- }
- function groupByPlugin(
- handlers: readonly PluginHookCommandHandler[],
- runtime: {
- readonly cwd: string;
- readonly transcriptPath?: string | null;
- readonly codexTranscriptPath?: string | null;
- readonly model?: string;
- readonly permissionMode?: string;
- readonly effort?: PluginHookEventInput['effort'];
- readonly promptId?: string;
- },
- ): Map<string, ActivePlugin> {
- const grouped = new Map<string, PluginHookCommandHandler[]>();
- for (const handler of handlers) {
- const identity = pluginIdentity(handler);
- const values = grouped.get(identity) ?? [];
- values.push(handler);
- grouped.set(identity, values);
- }
- const result = new Map<string, ActivePlugin>();
- for (const [identity, values] of grouped) {
- const first = values[0];
- if (first) {
- result.set(identity, {
- pluginName: first.pluginName,
- handlers: values,
- cwd: runtime.cwd,
- transcriptPath: runtime.transcriptPath,
- codexTranscriptPath: runtime.codexTranscriptPath,
- model: runtime.model,
- permissionMode: runtime.permissionMode,
- effort: runtime.effort,
- promptId: runtime.promptId,
- });
- }
- }
- return result;
- }
- function pluginIdentity(handler: PluginHookCommandHandler): string {
- return JSON.stringify([handler.pluginName, handler.pluginRoot, handler.activationKey ?? '']);
- }
- function mergeRunResults(results: readonly PluginHookRunResult[]): PluginHookRunResult {
- const decision = results.reduce(
- (current, result) => mergePluginHookDecisions(current, result.decision),
- { decision: 'allow' } as PluginHookDecision,
- );
- return {
- decision,
- diagnostics: results.flatMap((result) => result.diagnostics),
- };
- }
- function eventInput(
- event: PluginHookEventInput['event'],
- input: {
- readonly sessionId: string;
- readonly turnId: string;
- readonly cwd: string;
- readonly transcriptPath?: string | null;
- readonly codexTranscriptPath?: string | null;
- readonly model?: string;
- readonly permissionMode?: string;
- readonly effort?: PluginHookEventInput['effort'];
- readonly promptId?: string;
- },
- payload: Readonly<Record<string, unknown>>,
- matcherValue?: string,
- ): PluginHookEventInput {
- return {
- event,
- sessionId: input.sessionId,
- turnId: input.turnId,
- transcriptPath: input.transcriptPath,
- codexTranscriptPath: input.codexTranscriptPath,
- cwd: input.cwd,
- model: input.model,
- permissionMode: input.permissionMode,
- effort: input.effort,
- promptId: input.promptId,
- ...(matcherValue ? { matcherValue } : {}),
- payload,
- };
- }
|