Merge pull request #2029 from coleleavitt/fix/plug-resource-leaks
fix: plug resource leaks and add hook command timeout
This commit is contained in:
@@ -1,53 +1,58 @@
|
|||||||
import type { OhMyOpenCodeConfig } from "../config"
|
import type { OhMyOpenCodeConfig } from "../config";
|
||||||
import type { PluginContext } from "./types"
|
import type { PluginContext } from "./types";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
clearSessionAgent,
|
clearSessionAgent,
|
||||||
getMainSessionID,
|
getMainSessionID,
|
||||||
getSessionAgent,
|
getSessionAgent,
|
||||||
|
setMainSession,
|
||||||
subagentSessions,
|
subagentSessions,
|
||||||
syncSubagentSessions,
|
syncSubagentSessions,
|
||||||
setMainSession,
|
|
||||||
updateSessionAgent,
|
updateSessionAgent,
|
||||||
} from "../features/claude-code-session-state"
|
} from "../features/claude-code-session-state";
|
||||||
import { resetMessageCursor } from "../shared"
|
import {
|
||||||
import { lspManager } from "../tools"
|
clearPendingModelFallback,
|
||||||
import { shouldRetryError } from "../shared/model-error-classifier"
|
clearSessionFallbackChain,
|
||||||
import { clearPendingModelFallback, clearSessionFallbackChain, setPendingModelFallback } from "../hooks/model-fallback/hook"
|
setPendingModelFallback,
|
||||||
import { log } from "../shared/logger"
|
} from "../hooks/model-fallback/hook";
|
||||||
import { clearSessionModel, setSessionModel } from "../shared/session-model-state"
|
import { resetMessageCursor } from "../shared";
|
||||||
|
import { log } from "../shared/logger";
|
||||||
|
import { shouldRetryError } from "../shared/model-error-classifier";
|
||||||
|
import { clearSessionModel, setSessionModel } from "../shared/session-model-state";
|
||||||
|
import { deleteSessionTools } from "../shared/session-tools-store";
|
||||||
|
import { lspManager } from "../tools";
|
||||||
|
|
||||||
import type { CreatedHooks } from "../create-hooks"
|
import type { CreatedHooks } from "../create-hooks";
|
||||||
import type { Managers } from "../create-managers"
|
import type { Managers } from "../create-managers";
|
||||||
import { normalizeSessionStatusToIdle } from "./session-status-normalizer"
|
import { pruneRecentSyntheticIdles } from "./recent-synthetic-idles";
|
||||||
import { pruneRecentSyntheticIdles } from "./recent-synthetic-idles"
|
import { normalizeSessionStatusToIdle } from "./session-status-normalizer";
|
||||||
|
|
||||||
type FirstMessageVariantGate = {
|
type FirstMessageVariantGate = {
|
||||||
markSessionCreated: (sessionInfo: { id?: string; title?: string; parentID?: string } | undefined) => void
|
markSessionCreated: (sessionInfo: { id?: string; title?: string; parentID?: string } | undefined) => void;
|
||||||
clear: (sessionID: string) => void
|
clear: (sessionID: string) => void;
|
||||||
}
|
};
|
||||||
|
|
||||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||||
return typeof value === "object" && value !== null
|
return typeof value === "object" && value !== null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function normalizeFallbackModelID(modelID: string): string {
|
function normalizeFallbackModelID(modelID: string): string {
|
||||||
return modelID
|
return modelID
|
||||||
.replace(/-thinking$/i, "")
|
.replace(/-thinking$/i, "")
|
||||||
.replace(/-max$/i, "")
|
.replace(/-max$/i, "")
|
||||||
.replace(/-high$/i, "")
|
.replace(/-high$/i, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractErrorName(error: unknown): string | undefined {
|
function extractErrorName(error: unknown): string | undefined {
|
||||||
if (isRecord(error) && typeof error.name === "string") return error.name
|
if (isRecord(error) && typeof error.name === "string") return error.name;
|
||||||
if (error instanceof Error) return error.name
|
if (error instanceof Error) return error.name;
|
||||||
return undefined
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractErrorMessage(error: unknown): string {
|
function extractErrorMessage(error: unknown): string {
|
||||||
if (!error) return ""
|
if (!error) return "";
|
||||||
if (typeof error === "string") return error
|
if (typeof error === "string") return error;
|
||||||
if (error instanceof Error) return error.message
|
if (error instanceof Error) return error.message;
|
||||||
|
|
||||||
if (isRecord(error)) {
|
if (isRecord(error)) {
|
||||||
const candidates: unknown[] = [
|
const candidates: unknown[] = [
|
||||||
@@ -56,116 +61,112 @@ function extractErrorMessage(error: unknown): string {
|
|||||||
error.error,
|
error.error,
|
||||||
isRecord(error.data) ? error.data.error : undefined,
|
isRecord(error.data) ? error.data.error : undefined,
|
||||||
error.cause,
|
error.cause,
|
||||||
]
|
];
|
||||||
|
|
||||||
for (const candidate of candidates) {
|
for (const candidate of candidates) {
|
||||||
if (isRecord(candidate) && typeof candidate.message === "string" && candidate.message.length > 0) {
|
if (isRecord(candidate) && typeof candidate.message === "string" && candidate.message.length > 0) {
|
||||||
return candidate.message
|
return candidate.message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return JSON.stringify(error)
|
return JSON.stringify(error);
|
||||||
} catch {
|
} catch {
|
||||||
return String(error)
|
return String(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractProviderModelFromErrorMessage(
|
function extractProviderModelFromErrorMessage(message: string): { providerID?: string; modelID?: string } {
|
||||||
message: string,
|
const lower = message.toLowerCase();
|
||||||
): { providerID?: string; modelID?: string } {
|
|
||||||
const lower = message.toLowerCase()
|
|
||||||
|
|
||||||
const providerModel = lower.match(/model\s+not\s+found:\s*([a-z0-9_-]+)\s*\/\s*([a-z0-9._-]+)/i)
|
const providerModel = lower.match(/model\s+not\s+found:\s*([a-z0-9_-]+)\s*\/\s*([a-z0-9._-]+)/i);
|
||||||
if (providerModel) {
|
if (providerModel) {
|
||||||
return {
|
return {
|
||||||
providerID: providerModel[1],
|
providerID: providerModel[1],
|
||||||
modelID: providerModel[2],
|
modelID: providerModel[2],
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const modelOnly = lower.match(/unknown\s+provider\s+for\s+model\s+([a-z0-9._-]+)/i)
|
const modelOnly = lower.match(/unknown\s+provider\s+for\s+model\s+([a-z0-9._-]+)/i);
|
||||||
if (modelOnly) {
|
if (modelOnly) {
|
||||||
return {
|
return {
|
||||||
modelID: modelOnly[1],
|
modelID: modelOnly[1],
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return {}
|
return {};
|
||||||
}
|
}
|
||||||
type EventInput = Parameters<
|
type EventInput = Parameters<NonNullable<NonNullable<CreatedHooks["writeExistingFileGuard"]>["event"]>>[0];
|
||||||
NonNullable<NonNullable<CreatedHooks["writeExistingFileGuard"]>["event"]>
|
|
||||||
>[0]
|
|
||||||
export function createEventHandler(args: {
|
export function createEventHandler(args: {
|
||||||
ctx: PluginContext
|
ctx: PluginContext;
|
||||||
pluginConfig: OhMyOpenCodeConfig
|
pluginConfig: OhMyOpenCodeConfig;
|
||||||
firstMessageVariantGate: FirstMessageVariantGate
|
firstMessageVariantGate: FirstMessageVariantGate;
|
||||||
managers: Managers
|
managers: Managers;
|
||||||
hooks: CreatedHooks
|
hooks: CreatedHooks;
|
||||||
}): (input: EventInput) => Promise<void> {
|
}): (input: EventInput) => Promise<void> {
|
||||||
const { ctx, firstMessageVariantGate, managers, hooks } = args
|
const { ctx, firstMessageVariantGate, managers, hooks } = args;
|
||||||
const pluginContext = ctx as {
|
const pluginContext = ctx as {
|
||||||
directory: string
|
directory: string;
|
||||||
client: {
|
client: {
|
||||||
session: {
|
session: {
|
||||||
abort: (input: { path: { id: string } }) => Promise<unknown>
|
abort: (input: { path: { id: string } }) => Promise<unknown>;
|
||||||
prompt: (input: {
|
prompt: (input: {
|
||||||
path: { id: string }
|
path: { id: string };
|
||||||
body: { parts: Array<{ type: "text"; text: string }> }
|
body: { parts: Array<{ type: "text"; text: string }> };
|
||||||
query: { directory: string }
|
query: { directory: string };
|
||||||
}) => Promise<unknown>
|
}) => Promise<unknown>;
|
||||||
}
|
};
|
||||||
}
|
};
|
||||||
}
|
};
|
||||||
const isRuntimeFallbackEnabled =
|
const isRuntimeFallbackEnabled =
|
||||||
hooks.runtimeFallback !== null &&
|
hooks.runtimeFallback !== null &&
|
||||||
hooks.runtimeFallback !== undefined &&
|
hooks.runtimeFallback !== undefined &&
|
||||||
(typeof args.pluginConfig.runtime_fallback === "boolean"
|
(typeof args.pluginConfig.runtime_fallback === "boolean"
|
||||||
? args.pluginConfig.runtime_fallback
|
? args.pluginConfig.runtime_fallback
|
||||||
: (args.pluginConfig.runtime_fallback?.enabled ?? false))
|
: (args.pluginConfig.runtime_fallback?.enabled ?? false));
|
||||||
|
|
||||||
// Avoid triggering multiple abort+continue cycles for the same failing assistant message.
|
// Avoid triggering multiple abort+continue cycles for the same failing assistant message.
|
||||||
const lastHandledModelErrorMessageID = new Map<string, string>()
|
const lastHandledModelErrorMessageID = new Map<string, string>();
|
||||||
const lastHandledRetryStatusKey = new Map<string, string>()
|
const lastHandledRetryStatusKey = new Map<string, string>();
|
||||||
const lastKnownModelBySession = new Map<string, { providerID: string; modelID: string }>()
|
const lastKnownModelBySession = new Map<string, { providerID: string; modelID: string }>();
|
||||||
|
|
||||||
const dispatchToHooks = async (input: EventInput): Promise<void> => {
|
const dispatchToHooks = async (input: EventInput): Promise<void> => {
|
||||||
await Promise.resolve(hooks.autoUpdateChecker?.event?.(input))
|
await Promise.resolve(hooks.autoUpdateChecker?.event?.(input));
|
||||||
await Promise.resolve(hooks.claudeCodeHooks?.event?.(input))
|
await Promise.resolve(hooks.claudeCodeHooks?.event?.(input));
|
||||||
await Promise.resolve(hooks.backgroundNotificationHook?.event?.(input))
|
await Promise.resolve(hooks.backgroundNotificationHook?.event?.(input));
|
||||||
await Promise.resolve(hooks.sessionNotification?.(input))
|
await Promise.resolve(hooks.sessionNotification?.(input));
|
||||||
await Promise.resolve(hooks.todoContinuationEnforcer?.handler?.(input))
|
await Promise.resolve(hooks.todoContinuationEnforcer?.handler?.(input));
|
||||||
await Promise.resolve(hooks.unstableAgentBabysitter?.event?.(input))
|
await Promise.resolve(hooks.unstableAgentBabysitter?.event?.(input));
|
||||||
await Promise.resolve(hooks.contextWindowMonitor?.event?.(input))
|
await Promise.resolve(hooks.contextWindowMonitor?.event?.(input));
|
||||||
await Promise.resolve(hooks.directoryAgentsInjector?.event?.(input))
|
await Promise.resolve(hooks.directoryAgentsInjector?.event?.(input));
|
||||||
await Promise.resolve(hooks.directoryReadmeInjector?.event?.(input))
|
await Promise.resolve(hooks.directoryReadmeInjector?.event?.(input));
|
||||||
await Promise.resolve(hooks.rulesInjector?.event?.(input))
|
await Promise.resolve(hooks.rulesInjector?.event?.(input));
|
||||||
await Promise.resolve(hooks.thinkMode?.event?.(input))
|
await Promise.resolve(hooks.thinkMode?.event?.(input));
|
||||||
await Promise.resolve(hooks.anthropicContextWindowLimitRecovery?.event?.(input))
|
await Promise.resolve(hooks.anthropicContextWindowLimitRecovery?.event?.(input));
|
||||||
await Promise.resolve(hooks.runtimeFallback?.event?.(input))
|
await Promise.resolve(hooks.runtimeFallback?.event?.(input));
|
||||||
await Promise.resolve(hooks.agentUsageReminder?.event?.(input))
|
await Promise.resolve(hooks.agentUsageReminder?.event?.(input));
|
||||||
await Promise.resolve(hooks.categorySkillReminder?.event?.(input))
|
await Promise.resolve(hooks.categorySkillReminder?.event?.(input));
|
||||||
await Promise.resolve(hooks.interactiveBashSession?.event?.(input as EventInput))
|
await Promise.resolve(hooks.interactiveBashSession?.event?.(input as EventInput));
|
||||||
await Promise.resolve(hooks.ralphLoop?.event?.(input))
|
await Promise.resolve(hooks.ralphLoop?.event?.(input));
|
||||||
await Promise.resolve(hooks.stopContinuationGuard?.event?.(input))
|
await Promise.resolve(hooks.stopContinuationGuard?.event?.(input));
|
||||||
await Promise.resolve(hooks.compactionTodoPreserver?.event?.(input))
|
await Promise.resolve(hooks.compactionTodoPreserver?.event?.(input));
|
||||||
await Promise.resolve(hooks.writeExistingFileGuard?.event?.(input))
|
await Promise.resolve(hooks.writeExistingFileGuard?.event?.(input));
|
||||||
await Promise.resolve(hooks.atlasHook?.handler?.(input))
|
await Promise.resolve(hooks.atlasHook?.handler?.(input));
|
||||||
}
|
};
|
||||||
|
|
||||||
const recentSyntheticIdles = new Map<string, number>()
|
const recentSyntheticIdles = new Map<string, number>();
|
||||||
const recentRealIdles = new Map<string, number>()
|
const recentRealIdles = new Map<string, number>();
|
||||||
const DEDUP_WINDOW_MS = 500
|
const DEDUP_WINDOW_MS = 500;
|
||||||
|
|
||||||
const shouldAutoRetrySession = (sessionID: string): boolean => {
|
const shouldAutoRetrySession = (sessionID: string): boolean => {
|
||||||
if (syncSubagentSessions.has(sessionID)) return true
|
if (syncSubagentSessions.has(sessionID)) return true;
|
||||||
const mainSessionID = getMainSessionID()
|
const mainSessionID = getMainSessionID();
|
||||||
if (mainSessionID) return sessionID === mainSessionID
|
if (mainSessionID) return sessionID === mainSessionID;
|
||||||
// Headless runs (or resumed sessions) may not emit session.created, so mainSessionID can be unset.
|
// Headless runs (or resumed sessions) may not emit session.created, so mainSessionID can be unset.
|
||||||
// In that case, treat any non-subagent session as the "main" interactive session.
|
// In that case, treat any non-subagent session as the "main" interactive session.
|
||||||
return !subagentSessions.has(sessionID)
|
return !subagentSessions.has(sessionID);
|
||||||
}
|
};
|
||||||
|
|
||||||
return async (input): Promise<void> => {
|
return async (input): Promise<void> => {
|
||||||
pruneRecentSyntheticIdles({
|
pruneRecentSyntheticIdles({
|
||||||
@@ -173,97 +174,98 @@ export function createEventHandler(args: {
|
|||||||
recentRealIdles,
|
recentRealIdles,
|
||||||
now: Date.now(),
|
now: Date.now(),
|
||||||
dedupWindowMs: DEDUP_WINDOW_MS,
|
dedupWindowMs: DEDUP_WINDOW_MS,
|
||||||
})
|
});
|
||||||
|
|
||||||
if (input.event.type === "session.idle") {
|
if (input.event.type === "session.idle") {
|
||||||
const sessionID = (input.event.properties as Record<string, unknown> | undefined)?.sessionID as string | undefined
|
const sessionID = (input.event.properties as Record<string, unknown> | undefined)?.sessionID as
|
||||||
|
| string
|
||||||
|
| undefined;
|
||||||
if (sessionID) {
|
if (sessionID) {
|
||||||
const emittedAt = recentSyntheticIdles.get(sessionID)
|
const emittedAt = recentSyntheticIdles.get(sessionID);
|
||||||
if (emittedAt && Date.now() - emittedAt < DEDUP_WINDOW_MS) {
|
if (emittedAt && Date.now() - emittedAt < DEDUP_WINDOW_MS) {
|
||||||
recentSyntheticIdles.delete(sessionID)
|
recentSyntheticIdles.delete(sessionID);
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
recentRealIdles.set(sessionID, Date.now())
|
recentRealIdles.set(sessionID, Date.now());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await dispatchToHooks(input)
|
await dispatchToHooks(input);
|
||||||
|
|
||||||
const syntheticIdle = normalizeSessionStatusToIdle(input)
|
const syntheticIdle = normalizeSessionStatusToIdle(input);
|
||||||
if (syntheticIdle) {
|
if (syntheticIdle) {
|
||||||
const sessionID = (syntheticIdle.event.properties as Record<string, unknown>)?.sessionID as string
|
const sessionID = (syntheticIdle.event.properties as Record<string, unknown>)?.sessionID as string;
|
||||||
const emittedAt = recentRealIdles.get(sessionID)
|
const emittedAt = recentRealIdles.get(sessionID);
|
||||||
if (emittedAt && Date.now() - emittedAt < DEDUP_WINDOW_MS) {
|
if (emittedAt && Date.now() - emittedAt < DEDUP_WINDOW_MS) {
|
||||||
recentRealIdles.delete(sessionID)
|
recentRealIdles.delete(sessionID);
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
recentSyntheticIdles.set(sessionID, Date.now())
|
recentSyntheticIdles.set(sessionID, Date.now());
|
||||||
await dispatchToHooks(syntheticIdle as EventInput)
|
await dispatchToHooks(syntheticIdle as EventInput);
|
||||||
}
|
}
|
||||||
|
|
||||||
const { event } = input
|
const { event } = input;
|
||||||
const props = event.properties as Record<string, unknown> | undefined
|
const props = event.properties as Record<string, unknown> | undefined;
|
||||||
|
|
||||||
if (event.type === "session.created") {
|
if (event.type === "session.created") {
|
||||||
const sessionInfo = props?.info as
|
const sessionInfo = props?.info as { id?: string; title?: string; parentID?: string } | undefined;
|
||||||
| { id?: string; title?: string; parentID?: string }
|
|
||||||
| undefined
|
|
||||||
|
|
||||||
if (!sessionInfo?.parentID) {
|
if (!sessionInfo?.parentID) {
|
||||||
setMainSession(sessionInfo?.id)
|
setMainSession(sessionInfo?.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
firstMessageVariantGate.markSessionCreated(sessionInfo)
|
firstMessageVariantGate.markSessionCreated(sessionInfo);
|
||||||
|
|
||||||
await managers.tmuxSessionManager.onSessionCreated(
|
await managers.tmuxSessionManager.onSessionCreated(
|
||||||
event as {
|
event as {
|
||||||
type: string
|
type: string;
|
||||||
properties?: {
|
properties?: {
|
||||||
info?: { id?: string; parentID?: string; title?: string }
|
info?: { id?: string; parentID?: string; title?: string };
|
||||||
}
|
};
|
||||||
},
|
},
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.type === "session.deleted") {
|
if (event.type === "session.deleted") {
|
||||||
const sessionInfo = props?.info as { id?: string } | undefined
|
const sessionInfo = props?.info as { id?: string } | undefined;
|
||||||
if (sessionInfo?.id === getMainSessionID()) {
|
if (sessionInfo?.id === getMainSessionID()) {
|
||||||
setMainSession(undefined)
|
setMainSession(undefined);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sessionInfo?.id) {
|
if (sessionInfo?.id) {
|
||||||
clearSessionAgent(sessionInfo.id)
|
clearSessionAgent(sessionInfo.id);
|
||||||
lastHandledModelErrorMessageID.delete(sessionInfo.id)
|
lastHandledModelErrorMessageID.delete(sessionInfo.id);
|
||||||
lastHandledRetryStatusKey.delete(sessionInfo.id)
|
lastHandledRetryStatusKey.delete(sessionInfo.id);
|
||||||
lastKnownModelBySession.delete(sessionInfo.id)
|
lastKnownModelBySession.delete(sessionInfo.id);
|
||||||
clearPendingModelFallback(sessionInfo.id)
|
clearPendingModelFallback(sessionInfo.id);
|
||||||
clearSessionFallbackChain(sessionInfo.id)
|
clearSessionFallbackChain(sessionInfo.id);
|
||||||
resetMessageCursor(sessionInfo.id)
|
resetMessageCursor(sessionInfo.id);
|
||||||
firstMessageVariantGate.clear(sessionInfo.id)
|
firstMessageVariantGate.clear(sessionInfo.id);
|
||||||
clearSessionModel(sessionInfo.id)
|
clearSessionModel(sessionInfo.id);
|
||||||
syncSubagentSessions.delete(sessionInfo.id)
|
syncSubagentSessions.delete(sessionInfo.id);
|
||||||
await managers.skillMcpManager.disconnectSession(sessionInfo.id)
|
deleteSessionTools(sessionInfo.id);
|
||||||
await lspManager.cleanupTempDirectoryClients()
|
await managers.skillMcpManager.disconnectSession(sessionInfo.id);
|
||||||
|
await lspManager.cleanupTempDirectoryClients();
|
||||||
await managers.tmuxSessionManager.onSessionDeleted({
|
await managers.tmuxSessionManager.onSessionDeleted({
|
||||||
sessionID: sessionInfo.id,
|
sessionID: sessionInfo.id,
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.type === "message.updated") {
|
if (event.type === "message.updated") {
|
||||||
const info = props?.info as Record<string, unknown> | undefined
|
const info = props?.info as Record<string, unknown> | undefined;
|
||||||
const sessionID = info?.sessionID as string | undefined
|
const sessionID = info?.sessionID as string | undefined;
|
||||||
const agent = info?.agent as string | undefined
|
const agent = info?.agent as string | undefined;
|
||||||
const role = info?.role as string | undefined
|
const role = info?.role as string | undefined;
|
||||||
if (sessionID && role === "user") {
|
if (sessionID && role === "user") {
|
||||||
if (agent) {
|
if (agent) {
|
||||||
updateSessionAgent(sessionID, agent)
|
updateSessionAgent(sessionID, agent);
|
||||||
}
|
}
|
||||||
const providerID = info?.providerID as string | undefined
|
const providerID = info?.providerID as string | undefined;
|
||||||
const modelID = info?.modelID as string | undefined
|
const modelID = info?.modelID as string | undefined;
|
||||||
if (providerID && modelID) {
|
if (providerID && modelID) {
|
||||||
lastKnownModelBySession.set(sessionID, { providerID, modelID })
|
lastKnownModelBySession.set(sessionID, { providerID, modelID });
|
||||||
setSessionModel(sessionID, { providerID, modelID })
|
setSessionModel(sessionID, { providerID, modelID });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -271,132 +273,128 @@ export function createEventHandler(args: {
|
|||||||
// session.error events are not guaranteed for all providers, so we also observe message.updated.
|
// session.error events are not guaranteed for all providers, so we also observe message.updated.
|
||||||
if (sessionID && role === "assistant" && !isRuntimeFallbackEnabled) {
|
if (sessionID && role === "assistant" && !isRuntimeFallbackEnabled) {
|
||||||
try {
|
try {
|
||||||
const assistantMessageID = info?.id as string | undefined
|
const assistantMessageID = info?.id as string | undefined;
|
||||||
const assistantError = info?.error
|
const assistantError = info?.error;
|
||||||
if (assistantMessageID && assistantError) {
|
if (assistantMessageID && assistantError) {
|
||||||
const lastHandled = lastHandledModelErrorMessageID.get(sessionID)
|
const lastHandled = lastHandledModelErrorMessageID.get(sessionID);
|
||||||
if (lastHandled === assistantMessageID) {
|
if (lastHandled === assistantMessageID) {
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const errorName = extractErrorName(assistantError)
|
const errorName = extractErrorName(assistantError);
|
||||||
const errorMessage = extractErrorMessage(assistantError)
|
const errorMessage = extractErrorMessage(assistantError);
|
||||||
const errorInfo = { name: errorName, message: errorMessage }
|
const errorInfo = { name: errorName, message: errorMessage };
|
||||||
|
|
||||||
if (shouldRetryError(errorInfo)) {
|
if (shouldRetryError(errorInfo)) {
|
||||||
// Prefer the agent/model/provider from the assistant message payload.
|
// Prefer the agent/model/provider from the assistant message payload.
|
||||||
let agentName = agent ?? getSessionAgent(sessionID)
|
let agentName = agent ?? getSessionAgent(sessionID);
|
||||||
if (!agentName && sessionID === getMainSessionID()) {
|
if (!agentName && sessionID === getMainSessionID()) {
|
||||||
if (errorMessage.includes("claude-opus") || errorMessage.includes("opus")) {
|
if (errorMessage.includes("claude-opus") || errorMessage.includes("opus")) {
|
||||||
agentName = "sisyphus"
|
agentName = "sisyphus";
|
||||||
} else if (errorMessage.includes("gpt-5")) {
|
} else if (errorMessage.includes("gpt-5")) {
|
||||||
agentName = "hephaestus"
|
agentName = "hephaestus";
|
||||||
} else {
|
} else {
|
||||||
agentName = "sisyphus"
|
agentName = "sisyphus";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (agentName) {
|
if (agentName) {
|
||||||
const currentProvider = (info?.providerID as string | undefined) ?? "opencode"
|
const currentProvider = (info?.providerID as string | undefined) ?? "opencode";
|
||||||
const rawModel = (info?.modelID as string | undefined) ?? "claude-opus-4-6"
|
const rawModel = (info?.modelID as string | undefined) ?? "claude-opus-4-6";
|
||||||
const currentModel = normalizeFallbackModelID(rawModel)
|
const currentModel = normalizeFallbackModelID(rawModel);
|
||||||
|
|
||||||
const setFallback = setPendingModelFallback(
|
const setFallback = setPendingModelFallback(sessionID, agentName, currentProvider, currentModel);
|
||||||
sessionID,
|
|
||||||
agentName,
|
|
||||||
currentProvider,
|
|
||||||
currentModel,
|
|
||||||
)
|
|
||||||
|
|
||||||
if (setFallback && shouldAutoRetrySession(sessionID) && !hooks.stopContinuationGuard?.isStopped(sessionID)) {
|
if (
|
||||||
lastHandledModelErrorMessageID.set(sessionID, assistantMessageID)
|
setFallback &&
|
||||||
|
shouldAutoRetrySession(sessionID) &&
|
||||||
|
!hooks.stopContinuationGuard?.isStopped(sessionID)
|
||||||
|
) {
|
||||||
|
lastHandledModelErrorMessageID.set(sessionID, assistantMessageID);
|
||||||
|
|
||||||
await pluginContext.client.session.abort({ path: { id: sessionID } }).catch(() => {})
|
await pluginContext.client.session.abort({ path: { id: sessionID } }).catch(() => {});
|
||||||
await pluginContext.client.session
|
await pluginContext.client.session
|
||||||
.prompt({
|
.prompt({
|
||||||
path: { id: sessionID },
|
path: { id: sessionID },
|
||||||
body: { parts: [{ type: "text", text: "continue" }] },
|
body: { parts: [{ type: "text", text: "continue" }] },
|
||||||
query: { directory: pluginContext.directory },
|
query: { directory: pluginContext.directory },
|
||||||
})
|
})
|
||||||
.catch(() => {})
|
.catch(() => {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log("[event] model-fallback error in message.updated:", { sessionID, error: err })
|
log("[event] model-fallback error in message.updated:", { sessionID, error: err });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.type === "session.status") {
|
if (event.type === "session.status") {
|
||||||
const sessionID = props?.sessionID as string | undefined
|
const sessionID = props?.sessionID as string | undefined;
|
||||||
const status = props?.status as
|
const status = props?.status as { type?: string; attempt?: number; message?: string; next?: number } | undefined;
|
||||||
| { type?: string; attempt?: number; message?: string; next?: number }
|
|
||||||
| undefined
|
|
||||||
|
|
||||||
if (sessionID && status?.type === "retry") {
|
if (sessionID && status?.type === "retry") {
|
||||||
try {
|
try {
|
||||||
const retryMessage = typeof status.message === "string" ? status.message : ""
|
const retryMessage = typeof status.message === "string" ? status.message : "";
|
||||||
const retryKey = `${status.attempt ?? "?"}:${status.next ?? "?"}:${retryMessage}`
|
const retryKey = `${status.attempt ?? "?"}:${status.next ?? "?"}:${retryMessage}`;
|
||||||
if (lastHandledRetryStatusKey.get(sessionID) === retryKey) {
|
if (lastHandledRetryStatusKey.get(sessionID) === retryKey) {
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
lastHandledRetryStatusKey.set(sessionID, retryKey)
|
lastHandledRetryStatusKey.set(sessionID, retryKey);
|
||||||
|
|
||||||
const errorInfo = { name: undefined as string | undefined, message: retryMessage }
|
const errorInfo = { name: undefined as string | undefined, message: retryMessage };
|
||||||
if (shouldRetryError(errorInfo)) {
|
if (shouldRetryError(errorInfo)) {
|
||||||
let agentName = getSessionAgent(sessionID)
|
let agentName = getSessionAgent(sessionID);
|
||||||
if (!agentName && sessionID === getMainSessionID()) {
|
if (!agentName && sessionID === getMainSessionID()) {
|
||||||
if (retryMessage.includes("claude-opus") || retryMessage.includes("opus")) {
|
if (retryMessage.includes("claude-opus") || retryMessage.includes("opus")) {
|
||||||
agentName = "sisyphus"
|
agentName = "sisyphus";
|
||||||
} else if (retryMessage.includes("gpt-5")) {
|
} else if (retryMessage.includes("gpt-5")) {
|
||||||
agentName = "hephaestus"
|
agentName = "hephaestus";
|
||||||
} else {
|
} else {
|
||||||
agentName = "sisyphus"
|
agentName = "sisyphus";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (agentName) {
|
if (agentName) {
|
||||||
const parsed = extractProviderModelFromErrorMessage(retryMessage)
|
const parsed = extractProviderModelFromErrorMessage(retryMessage);
|
||||||
const lastKnown = lastKnownModelBySession.get(sessionID)
|
const lastKnown = lastKnownModelBySession.get(sessionID);
|
||||||
const currentProvider = parsed.providerID ?? lastKnown?.providerID ?? "opencode"
|
const currentProvider = parsed.providerID ?? lastKnown?.providerID ?? "opencode";
|
||||||
let currentModel = parsed.modelID ?? lastKnown?.modelID ?? "claude-opus-4-6"
|
let currentModel = parsed.modelID ?? lastKnown?.modelID ?? "claude-opus-4-6";
|
||||||
currentModel = normalizeFallbackModelID(currentModel)
|
currentModel = normalizeFallbackModelID(currentModel);
|
||||||
|
|
||||||
const setFallback = setPendingModelFallback(
|
const setFallback = setPendingModelFallback(sessionID, agentName, currentProvider, currentModel);
|
||||||
sessionID,
|
|
||||||
agentName,
|
|
||||||
currentProvider,
|
|
||||||
currentModel,
|
|
||||||
)
|
|
||||||
|
|
||||||
if (setFallback && shouldAutoRetrySession(sessionID) && !hooks.stopContinuationGuard?.isStopped(sessionID)) {
|
if (
|
||||||
await pluginContext.client.session.abort({ path: { id: sessionID } }).catch(() => {})
|
setFallback &&
|
||||||
|
shouldAutoRetrySession(sessionID) &&
|
||||||
|
!hooks.stopContinuationGuard?.isStopped(sessionID)
|
||||||
|
) {
|
||||||
|
await pluginContext.client.session.abort({ path: { id: sessionID } }).catch(() => {});
|
||||||
await pluginContext.client.session
|
await pluginContext.client.session
|
||||||
.prompt({
|
.prompt({
|
||||||
path: { id: sessionID },
|
path: { id: sessionID },
|
||||||
body: { parts: [{ type: "text", text: "continue" }] },
|
body: { parts: [{ type: "text", text: "continue" }] },
|
||||||
query: { directory: pluginContext.directory },
|
query: { directory: pluginContext.directory },
|
||||||
})
|
})
|
||||||
.catch(() => {})
|
.catch(() => {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log("[event] model-fallback error in session.status:", { sessionID, error: err })
|
log("[event] model-fallback error in session.status:", { sessionID, error: err });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.type === "session.error") {
|
if (event.type === "session.error") {
|
||||||
try {
|
try {
|
||||||
const sessionID = props?.sessionID as string | undefined
|
const sessionID = props?.sessionID as string | undefined;
|
||||||
const error = props?.error
|
const error = props?.error;
|
||||||
|
|
||||||
const errorName = extractErrorName(error)
|
const errorName = extractErrorName(error);
|
||||||
const errorMessage = extractErrorMessage(error)
|
const errorMessage = extractErrorMessage(error);
|
||||||
const errorInfo = { name: errorName, message: errorMessage }
|
const errorInfo = { name: errorName, message: errorMessage };
|
||||||
|
|
||||||
// First, try session recovery for internal errors (thinking blocks, tool results, etc.)
|
// First, try session recovery for internal errors (thinking blocks, tool results, etc.)
|
||||||
if (hooks.sessionRecovery?.isRecoverableError(error)) {
|
if (hooks.sessionRecovery?.isRecoverableError(error)) {
|
||||||
@@ -405,8 +403,8 @@ export function createEventHandler(args: {
|
|||||||
role: "assistant" as const,
|
role: "assistant" as const,
|
||||||
sessionID,
|
sessionID,
|
||||||
error,
|
error,
|
||||||
}
|
};
|
||||||
const recovered = await hooks.sessionRecovery.handleSessionRecovery(messageInfo)
|
const recovered = await hooks.sessionRecovery.handleSessionRecovery(messageInfo);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
recovered &&
|
recovered &&
|
||||||
@@ -420,53 +418,52 @@ export function createEventHandler(args: {
|
|||||||
body: { parts: [{ type: "text", text: "continue" }] },
|
body: { parts: [{ type: "text", text: "continue" }] },
|
||||||
query: { directory: pluginContext.directory },
|
query: { directory: pluginContext.directory },
|
||||||
})
|
})
|
||||||
.catch(() => {})
|
.catch(() => {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Second, try model fallback for model errors (rate limit, quota, provider issues, etc.)
|
// Second, try model fallback for model errors (rate limit, quota, provider issues, etc.)
|
||||||
else if (sessionID && shouldRetryError(errorInfo) && !isRuntimeFallbackEnabled) {
|
else if (sessionID && shouldRetryError(errorInfo) && !isRuntimeFallbackEnabled) {
|
||||||
let agentName = getSessionAgent(sessionID)
|
let agentName = getSessionAgent(sessionID);
|
||||||
|
|
||||||
if (!agentName && sessionID === getMainSessionID()) {
|
if (!agentName && sessionID === getMainSessionID()) {
|
||||||
if (errorMessage.includes("claude-opus") || errorMessage.includes("opus")) {
|
if (errorMessage.includes("claude-opus") || errorMessage.includes("opus")) {
|
||||||
agentName = "sisyphus"
|
agentName = "sisyphus";
|
||||||
} else if (errorMessage.includes("gpt-5")) {
|
} else if (errorMessage.includes("gpt-5")) {
|
||||||
agentName = "hephaestus"
|
agentName = "hephaestus";
|
||||||
} else {
|
} else {
|
||||||
agentName = "sisyphus"
|
agentName = "sisyphus";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (agentName) {
|
|
||||||
const parsed = extractProviderModelFromErrorMessage(errorMessage)
|
|
||||||
const currentProvider = props?.providerID as string || parsed.providerID || "opencode"
|
|
||||||
let currentModel = props?.modelID as string || parsed.modelID || "claude-opus-4-6"
|
|
||||||
currentModel = normalizeFallbackModelID(currentModel)
|
|
||||||
|
|
||||||
const setFallback = setPendingModelFallback(
|
if (agentName) {
|
||||||
sessionID,
|
const parsed = extractProviderModelFromErrorMessage(errorMessage);
|
||||||
agentName,
|
const currentProvider = (props?.providerID as string) || parsed.providerID || "opencode";
|
||||||
currentProvider,
|
let currentModel = (props?.modelID as string) || parsed.modelID || "claude-opus-4-6";
|
||||||
currentModel,
|
currentModel = normalizeFallbackModelID(currentModel);
|
||||||
)
|
|
||||||
|
const setFallback = setPendingModelFallback(sessionID, agentName, currentProvider, currentModel);
|
||||||
if (setFallback && shouldAutoRetrySession(sessionID) && !hooks.stopContinuationGuard?.isStopped(sessionID)) {
|
|
||||||
await pluginContext.client.session.abort({ path: { id: sessionID } }).catch(() => {})
|
if (
|
||||||
|
setFallback &&
|
||||||
await pluginContext.client.session
|
shouldAutoRetrySession(sessionID) &&
|
||||||
.prompt({
|
!hooks.stopContinuationGuard?.isStopped(sessionID)
|
||||||
path: { id: sessionID },
|
) {
|
||||||
body: { parts: [{ type: "text", text: "continue" }] },
|
await pluginContext.client.session.abort({ path: { id: sessionID } }).catch(() => {});
|
||||||
query: { directory: pluginContext.directory },
|
|
||||||
})
|
await pluginContext.client.session
|
||||||
.catch(() => {})
|
.prompt({
|
||||||
|
path: { id: sessionID },
|
||||||
|
body: { parts: [{ type: "text", text: "continue" }] },
|
||||||
|
query: { directory: pluginContext.directory },
|
||||||
|
})
|
||||||
|
.catch(() => {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const sessionID = props?.sessionID as string | undefined
|
const sessionID = props?.sessionID as string | undefined;
|
||||||
log("[event] model-fallback error in session.error:", { sessionID, error: err })
|
log("[event] model-fallback error in session.error:", { sessionID, error: err });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,78 +1,129 @@
|
|||||||
import { spawn } from "node:child_process"
|
import { spawn } from "node:child_process";
|
||||||
import { getHomeDirectory } from "./home-directory"
|
import { getHomeDirectory } from "./home-directory";
|
||||||
import { findBashPath, findZshPath } from "./shell-path"
|
import { findBashPath, findZshPath } from "./shell-path";
|
||||||
|
|
||||||
export interface CommandResult {
|
export interface CommandResult {
|
||||||
exitCode: number
|
exitCode: number;
|
||||||
stdout?: string
|
stdout?: string;
|
||||||
stderr?: string
|
stderr?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const DEFAULT_HOOK_TIMEOUT_MS = 30_000;
|
||||||
|
const SIGKILL_GRACE_MS = 5_000;
|
||||||
|
|
||||||
export interface ExecuteHookOptions {
|
export interface ExecuteHookOptions {
|
||||||
forceZsh?: boolean
|
forceZsh?: boolean;
|
||||||
zshPath?: string
|
zshPath?: string;
|
||||||
|
/** Timeout in milliseconds. Process is killed after this. Default: 30000 */
|
||||||
|
timeoutMs?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function executeHookCommand(
|
export async function executeHookCommand(
|
||||||
command: string,
|
command: string,
|
||||||
stdin: string,
|
stdin: string,
|
||||||
cwd: string,
|
cwd: string,
|
||||||
options?: ExecuteHookOptions,
|
options?: ExecuteHookOptions,
|
||||||
): Promise<CommandResult> {
|
): Promise<CommandResult> {
|
||||||
const home = getHomeDirectory()
|
const home = getHomeDirectory();
|
||||||
|
const timeoutMs = options?.timeoutMs ?? DEFAULT_HOOK_TIMEOUT_MS;
|
||||||
|
|
||||||
const expandedCommand = command
|
const expandedCommand = command
|
||||||
.replace(/^~(?=\/|$)/g, home)
|
.replace(/^~(?=\/|$)/g, home)
|
||||||
.replace(/\s~(?=\/)/g, ` ${home}`)
|
.replace(/\s~(?=\/)/g, ` ${home}`)
|
||||||
.replace(/\$CLAUDE_PROJECT_DIR/g, cwd)
|
.replace(/\$CLAUDE_PROJECT_DIR/g, cwd)
|
||||||
.replace(/\$\{CLAUDE_PROJECT_DIR\}/g, cwd)
|
.replace(/\$\{CLAUDE_PROJECT_DIR\}/g, cwd);
|
||||||
|
|
||||||
let finalCommand = expandedCommand
|
let finalCommand = expandedCommand;
|
||||||
|
|
||||||
if (options?.forceZsh) {
|
if (options?.forceZsh) {
|
||||||
const zshPath = findZshPath(options.zshPath)
|
const zshPath = findZshPath(options.zshPath);
|
||||||
const escapedCommand = expandedCommand.replace(/'/g, "'\\''")
|
const escapedCommand = expandedCommand.replace(/'/g, "'\\''");
|
||||||
if (zshPath) {
|
if (zshPath) {
|
||||||
finalCommand = `${zshPath} -lc '${escapedCommand}'`
|
finalCommand = `${zshPath} -lc '${escapedCommand}'`;
|
||||||
} else {
|
} else {
|
||||||
const bashPath = findBashPath()
|
const bashPath = findBashPath();
|
||||||
if (bashPath) {
|
if (bashPath) {
|
||||||
finalCommand = `${bashPath} -lc '${escapedCommand}'`
|
finalCommand = `${bashPath} -lc '${escapedCommand}'`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Promise((resolve) => {
|
return new Promise(resolve => {
|
||||||
const proc = spawn(finalCommand, {
|
let settled = false;
|
||||||
cwd,
|
let killTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
shell: true,
|
|
||||||
env: { ...process.env, HOME: home, CLAUDE_PROJECT_DIR: cwd },
|
|
||||||
})
|
|
||||||
|
|
||||||
let stdout = ""
|
const isWin32 = process.platform === "win32";
|
||||||
let stderr = ""
|
const proc = spawn(finalCommand, {
|
||||||
|
cwd,
|
||||||
|
shell: true,
|
||||||
|
detached: !isWin32,
|
||||||
|
env: { ...process.env, HOME: home, CLAUDE_PROJECT_DIR: cwd },
|
||||||
|
});
|
||||||
|
|
||||||
proc.stdout?.on("data", (data) => {
|
let stdout = "";
|
||||||
stdout += data.toString()
|
let stderr = "";
|
||||||
})
|
|
||||||
|
|
||||||
proc.stderr?.on("data", (data) => {
|
proc.stdout?.on("data", (data: Buffer) => {
|
||||||
stderr += data.toString()
|
stdout += data.toString();
|
||||||
})
|
});
|
||||||
|
|
||||||
proc.stdin?.write(stdin)
|
proc.stderr?.on("data", (data: Buffer) => {
|
||||||
proc.stdin?.end()
|
stderr += data.toString();
|
||||||
|
});
|
||||||
|
|
||||||
proc.on("close", (code) => {
|
proc.stdin?.on("error", () => {});
|
||||||
resolve({
|
proc.stdin?.write(stdin);
|
||||||
exitCode: code ?? 0,
|
proc.stdin?.end();
|
||||||
stdout: stdout.trim(),
|
|
||||||
stderr: stderr.trim(),
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
proc.on("error", (err) => {
|
const settle = (result: CommandResult) => {
|
||||||
resolve({ exitCode: 1, stderr: err.message })
|
if (settled) return;
|
||||||
})
|
settled = true;
|
||||||
})
|
if (killTimer) clearTimeout(killTimer);
|
||||||
|
if (timeoutTimer) clearTimeout(timeoutTimer);
|
||||||
|
resolve(result);
|
||||||
|
};
|
||||||
|
|
||||||
|
proc.on("close", code => {
|
||||||
|
settle({
|
||||||
|
exitCode: code ?? 1,
|
||||||
|
stdout: stdout.trim(),
|
||||||
|
stderr: stderr.trim(),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
proc.on("error", err => {
|
||||||
|
settle({ exitCode: 1, stderr: err.message });
|
||||||
|
});
|
||||||
|
|
||||||
|
const killProcessGroup = (signal: NodeJS.Signals) => {
|
||||||
|
try {
|
||||||
|
if (!isWin32 && proc.pid) {
|
||||||
|
try {
|
||||||
|
process.kill(-proc.pid, signal);
|
||||||
|
} catch {
|
||||||
|
proc.kill(signal);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
proc.kill(signal);
|
||||||
|
}
|
||||||
|
} catch {}
|
||||||
|
};
|
||||||
|
|
||||||
|
const timeoutTimer = setTimeout(() => {
|
||||||
|
if (settled) return;
|
||||||
|
// Kill entire process group to avoid orphaned children
|
||||||
|
killProcessGroup("SIGTERM");
|
||||||
|
killTimer = setTimeout(() => {
|
||||||
|
if (settled) return;
|
||||||
|
killProcessGroup("SIGKILL");
|
||||||
|
}, SIGKILL_GRACE_MS);
|
||||||
|
// Append timeout notice to stderr
|
||||||
|
stderr += `\nHook command timed out after ${timeoutMs}ms`;
|
||||||
|
}, timeoutMs);
|
||||||
|
|
||||||
|
// Don't let the timeout timer keep the process alive
|
||||||
|
if (timeoutTimer && typeof timeoutTimer === "object" && "unref" in timeoutTimer) {
|
||||||
|
timeoutTimer.unref();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,18 @@
|
|||||||
const store = new Map<string, Record<string, boolean>>()
|
const store = new Map<string, Record<string, boolean>>();
|
||||||
|
|
||||||
export function setSessionTools(sessionID: string, tools: Record<string, boolean>): void {
|
export function setSessionTools(sessionID: string, tools: Record<string, boolean>): void {
|
||||||
store.set(sessionID, { ...tools })
|
store.set(sessionID, { ...tools });
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getSessionTools(sessionID: string): Record<string, boolean> | undefined {
|
export function getSessionTools(sessionID: string): Record<string, boolean> | undefined {
|
||||||
const tools = store.get(sessionID)
|
const tools = store.get(sessionID);
|
||||||
return tools ? { ...tools } : undefined
|
return tools ? { ...tools } : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function deleteSessionTools(sessionID: string): void {
|
||||||
|
store.delete(sessionID);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function clearSessionTools(): void {
|
export function clearSessionTools(): void {
|
||||||
store.clear()
|
store.clear();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,45 +1,71 @@
|
|||||||
type ManagedClientForCleanup = {
|
type ManagedClientForCleanup = {
|
||||||
client: {
|
client: {
|
||||||
stop: () => Promise<void>
|
stop: () => Promise<void>;
|
||||||
}
|
};
|
||||||
}
|
};
|
||||||
|
|
||||||
type ProcessCleanupOptions = {
|
type ProcessCleanupOptions = {
|
||||||
getClients: () => IterableIterator<[string, ManagedClientForCleanup]>
|
getClients: () => IterableIterator<[string, ManagedClientForCleanup]>;
|
||||||
clearClients: () => void
|
clearClients: () => void;
|
||||||
clearCleanupInterval: () => void
|
clearCleanupInterval: () => void;
|
||||||
}
|
};
|
||||||
|
|
||||||
|
type RegisteredHandler = {
|
||||||
|
event: string;
|
||||||
|
listener: (...args: unknown[]) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type LspProcessCleanupHandle = {
|
||||||
|
unregister: () => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function registerLspManagerProcessCleanup(options: ProcessCleanupOptions): LspProcessCleanupHandle {
|
||||||
|
const handlers: RegisteredHandler[] = [];
|
||||||
|
|
||||||
export function registerLspManagerProcessCleanup(options: ProcessCleanupOptions): void {
|
|
||||||
// Synchronous cleanup for 'exit' event (cannot await)
|
// Synchronous cleanup for 'exit' event (cannot await)
|
||||||
const syncCleanup = () => {
|
const syncCleanup = () => {
|
||||||
for (const [, managed] of options.getClients()) {
|
for (const [, managed] of options.getClients()) {
|
||||||
try {
|
try {
|
||||||
// Fire-and-forget during sync exit - process is terminating
|
// Fire-and-forget during sync exit - process is terminating
|
||||||
void managed.client.stop().catch(() => {})
|
void managed.client.stop().catch(() => {});
|
||||||
} catch {}
|
} catch {}
|
||||||
}
|
}
|
||||||
options.clearClients()
|
options.clearClients();
|
||||||
options.clearCleanupInterval()
|
options.clearCleanupInterval();
|
||||||
}
|
};
|
||||||
|
|
||||||
// Async cleanup for signal handlers - properly await all stops
|
// Async cleanup for signal handlers - properly await all stops
|
||||||
const asyncCleanup = async () => {
|
const asyncCleanup = async () => {
|
||||||
const stopPromises: Promise<void>[] = []
|
const stopPromises: Promise<void>[] = [];
|
||||||
for (const [, managed] of options.getClients()) {
|
for (const [, managed] of options.getClients()) {
|
||||||
stopPromises.push(managed.client.stop().catch(() => {}))
|
stopPromises.push(managed.client.stop().catch(() => {}));
|
||||||
}
|
}
|
||||||
await Promise.allSettled(stopPromises)
|
await Promise.allSettled(stopPromises);
|
||||||
options.clearClients()
|
options.clearClients();
|
||||||
options.clearCleanupInterval()
|
options.clearCleanupInterval();
|
||||||
}
|
};
|
||||||
|
|
||||||
process.on("exit", syncCleanup)
|
const registerHandler = (event: string, listener: (...args: unknown[]) => void) => {
|
||||||
|
handlers.push({ event, listener });
|
||||||
|
process.on(event, listener);
|
||||||
|
};
|
||||||
|
|
||||||
|
registerHandler("exit", syncCleanup);
|
||||||
|
|
||||||
// Don't call process.exit() here; other handlers (background-agent manager) handle final exit.
|
// Don't call process.exit() here; other handlers (background-agent manager) handle final exit.
|
||||||
process.on("SIGINT", () => void asyncCleanup().catch(() => {}))
|
const signalCleanup = () => void asyncCleanup().catch(() => {});
|
||||||
process.on("SIGTERM", () => void asyncCleanup().catch(() => {}))
|
registerHandler("SIGINT", signalCleanup);
|
||||||
|
registerHandler("SIGTERM", signalCleanup);
|
||||||
if (process.platform === "win32") {
|
if (process.platform === "win32") {
|
||||||
process.on("SIGBREAK", () => void asyncCleanup().catch(() => {}))
|
registerHandler("SIGBREAK", signalCleanup);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
unregister: () => {
|
||||||
|
for (const { event, listener } of handlers) {
|
||||||
|
process.off(event, listener);
|
||||||
|
}
|
||||||
|
handlers.length = 0;
|
||||||
|
},
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,73 +1,74 @@
|
|||||||
import type { ResolvedServer } from "./types"
|
import { LSPClient } from "./lsp-client";
|
||||||
import { registerLspManagerProcessCleanup } from "./lsp-manager-process-cleanup"
|
import { registerLspManagerProcessCleanup, type LspProcessCleanupHandle } from "./lsp-manager-process-cleanup";
|
||||||
import { cleanupTempDirectoryLspClients } from "./lsp-manager-temp-directory-cleanup"
|
import { cleanupTempDirectoryLspClients } from "./lsp-manager-temp-directory-cleanup";
|
||||||
import { LSPClient } from "./lsp-client"
|
import type { ResolvedServer } from "./types";
|
||||||
interface ManagedClient {
|
interface ManagedClient {
|
||||||
client: LSPClient
|
client: LSPClient;
|
||||||
lastUsedAt: number
|
lastUsedAt: number;
|
||||||
refCount: number
|
refCount: number;
|
||||||
initPromise?: Promise<void>
|
initPromise?: Promise<void>;
|
||||||
isInitializing: boolean
|
isInitializing: boolean;
|
||||||
initializingSince?: number
|
initializingSince?: number;
|
||||||
}
|
}
|
||||||
class LSPServerManager {
|
class LSPServerManager {
|
||||||
private static instance: LSPServerManager
|
private static instance: LSPServerManager;
|
||||||
private clients = new Map<string, ManagedClient>()
|
private clients = new Map<string, ManagedClient>();
|
||||||
private cleanupInterval: ReturnType<typeof setInterval> | null = null
|
private cleanupInterval: ReturnType<typeof setInterval> | null = null;
|
||||||
private readonly IDLE_TIMEOUT = 5 * 60 * 1000
|
private readonly IDLE_TIMEOUT = 5 * 60 * 1000;
|
||||||
private readonly INIT_TIMEOUT = 60 * 1000
|
private readonly INIT_TIMEOUT = 60 * 1000;
|
||||||
|
private cleanupHandle: LspProcessCleanupHandle | null = null;
|
||||||
private constructor() {
|
private constructor() {
|
||||||
this.startCleanupTimer()
|
this.startCleanupTimer();
|
||||||
this.registerProcessCleanup()
|
this.registerProcessCleanup();
|
||||||
}
|
}
|
||||||
private registerProcessCleanup(): void {
|
private registerProcessCleanup(): void {
|
||||||
registerLspManagerProcessCleanup({
|
this.cleanupHandle = registerLspManagerProcessCleanup({
|
||||||
getClients: () => this.clients.entries(),
|
getClients: () => this.clients.entries(),
|
||||||
clearClients: () => {
|
clearClients: () => {
|
||||||
this.clients.clear()
|
this.clients.clear();
|
||||||
},
|
},
|
||||||
clearCleanupInterval: () => {
|
clearCleanupInterval: () => {
|
||||||
if (this.cleanupInterval) {
|
if (this.cleanupInterval) {
|
||||||
clearInterval(this.cleanupInterval)
|
clearInterval(this.cleanupInterval);
|
||||||
this.cleanupInterval = null
|
this.cleanupInterval = null;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static getInstance(): LSPServerManager {
|
static getInstance(): LSPServerManager {
|
||||||
if (!LSPServerManager.instance) {
|
if (!LSPServerManager.instance) {
|
||||||
LSPServerManager.instance = new LSPServerManager()
|
LSPServerManager.instance = new LSPServerManager();
|
||||||
}
|
}
|
||||||
return LSPServerManager.instance
|
return LSPServerManager.instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
private getKey(root: string, serverId: string): string {
|
private getKey(root: string, serverId: string): string {
|
||||||
return `${root}::${serverId}`
|
return `${root}::${serverId}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
private startCleanupTimer(): void {
|
private startCleanupTimer(): void {
|
||||||
if (this.cleanupInterval) return
|
if (this.cleanupInterval) return;
|
||||||
this.cleanupInterval = setInterval(() => {
|
this.cleanupInterval = setInterval(() => {
|
||||||
this.cleanupIdleClients()
|
this.cleanupIdleClients();
|
||||||
}, 60000)
|
}, 60000);
|
||||||
}
|
}
|
||||||
|
|
||||||
private cleanupIdleClients(): void {
|
private cleanupIdleClients(): void {
|
||||||
const now = Date.now()
|
const now = Date.now();
|
||||||
for (const [key, managed] of this.clients) {
|
for (const [key, managed] of this.clients) {
|
||||||
if (managed.refCount === 0 && now - managed.lastUsedAt > this.IDLE_TIMEOUT) {
|
if (managed.refCount === 0 && now - managed.lastUsedAt > this.IDLE_TIMEOUT) {
|
||||||
managed.client.stop()
|
managed.client.stop();
|
||||||
this.clients.delete(key)
|
this.clients.delete(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getClient(root: string, server: ResolvedServer): Promise<LSPClient> {
|
async getClient(root: string, server: ResolvedServer): Promise<LSPClient> {
|
||||||
const key = this.getKey(root, server.id)
|
const key = this.getKey(root, server.id);
|
||||||
let managed = this.clients.get(key)
|
let managed = this.clients.get(key);
|
||||||
if (managed) {
|
if (managed) {
|
||||||
const now = Date.now()
|
const now = Date.now();
|
||||||
if (
|
if (
|
||||||
managed.isInitializing &&
|
managed.isInitializing &&
|
||||||
managed.initializingSince !== undefined &&
|
managed.initializingSince !== undefined &&
|
||||||
@@ -75,45 +76,45 @@ class LSPServerManager {
|
|||||||
) {
|
) {
|
||||||
// Stale init can permanently block subsequent calls (e.g., LSP process hang)
|
// Stale init can permanently block subsequent calls (e.g., LSP process hang)
|
||||||
try {
|
try {
|
||||||
await managed.client.stop()
|
await managed.client.stop();
|
||||||
} catch {}
|
} catch {}
|
||||||
this.clients.delete(key)
|
this.clients.delete(key);
|
||||||
managed = undefined
|
managed = undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (managed) {
|
if (managed) {
|
||||||
if (managed.initPromise) {
|
if (managed.initPromise) {
|
||||||
try {
|
try {
|
||||||
await managed.initPromise
|
await managed.initPromise;
|
||||||
} catch {
|
} catch {
|
||||||
// Failed init should not keep the key blocked forever.
|
// Failed init should not keep the key blocked forever.
|
||||||
try {
|
try {
|
||||||
await managed.client.stop()
|
await managed.client.stop();
|
||||||
} catch {}
|
} catch {}
|
||||||
this.clients.delete(key)
|
this.clients.delete(key);
|
||||||
managed = undefined
|
managed = undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (managed) {
|
if (managed) {
|
||||||
if (managed.client.isAlive()) {
|
if (managed.client.isAlive()) {
|
||||||
managed.refCount++
|
managed.refCount++;
|
||||||
managed.lastUsedAt = Date.now()
|
managed.lastUsedAt = Date.now();
|
||||||
return managed.client
|
return managed.client;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
await managed.client.stop()
|
await managed.client.stop();
|
||||||
} catch {}
|
} catch {}
|
||||||
this.clients.delete(key)
|
this.clients.delete(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = new LSPClient(root, server)
|
const client = new LSPClient(root, server);
|
||||||
const initPromise = (async () => {
|
const initPromise = (async () => {
|
||||||
await client.start()
|
await client.start();
|
||||||
await client.initialize()
|
await client.initialize();
|
||||||
})()
|
})();
|
||||||
const initStartedAt = Date.now()
|
const initStartedAt = Date.now();
|
||||||
this.clients.set(key, {
|
this.clients.set(key, {
|
||||||
client,
|
client,
|
||||||
lastUsedAt: initStartedAt,
|
lastUsedAt: initStartedAt,
|
||||||
@@ -121,37 +122,37 @@ class LSPServerManager {
|
|||||||
initPromise,
|
initPromise,
|
||||||
isInitializing: true,
|
isInitializing: true,
|
||||||
initializingSince: initStartedAt,
|
initializingSince: initStartedAt,
|
||||||
})
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await initPromise
|
await initPromise;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.clients.delete(key)
|
this.clients.delete(key);
|
||||||
try {
|
try {
|
||||||
await client.stop()
|
await client.stop();
|
||||||
} catch {}
|
} catch {}
|
||||||
throw error
|
throw error;
|
||||||
}
|
}
|
||||||
const m = this.clients.get(key)
|
const m = this.clients.get(key);
|
||||||
if (m) {
|
if (m) {
|
||||||
m.initPromise = undefined
|
m.initPromise = undefined;
|
||||||
m.isInitializing = false
|
m.isInitializing = false;
|
||||||
m.initializingSince = undefined
|
m.initializingSince = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
return client
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
warmupClient(root: string, server: ResolvedServer): void {
|
warmupClient(root: string, server: ResolvedServer): void {
|
||||||
const key = this.getKey(root, server.id)
|
const key = this.getKey(root, server.id);
|
||||||
if (this.clients.has(key)) return
|
if (this.clients.has(key)) return;
|
||||||
const client = new LSPClient(root, server)
|
const client = new LSPClient(root, server);
|
||||||
const initPromise = (async () => {
|
const initPromise = (async () => {
|
||||||
await client.start()
|
await client.start();
|
||||||
await client.initialize()
|
await client.initialize();
|
||||||
})()
|
})();
|
||||||
|
|
||||||
const initStartedAt = Date.now()
|
const initStartedAt = Date.now();
|
||||||
this.clients.set(key, {
|
this.clients.set(key, {
|
||||||
client,
|
client,
|
||||||
lastUsedAt: initStartedAt,
|
lastUsedAt: initStartedAt,
|
||||||
@@ -159,53 +160,55 @@ class LSPServerManager {
|
|||||||
initPromise,
|
initPromise,
|
||||||
isInitializing: true,
|
isInitializing: true,
|
||||||
initializingSince: initStartedAt,
|
initializingSince: initStartedAt,
|
||||||
})
|
});
|
||||||
|
|
||||||
initPromise
|
initPromise
|
||||||
.then(() => {
|
.then(() => {
|
||||||
const m = this.clients.get(key)
|
const m = this.clients.get(key);
|
||||||
if (m) {
|
if (m) {
|
||||||
m.initPromise = undefined
|
m.initPromise = undefined;
|
||||||
m.isInitializing = false
|
m.isInitializing = false;
|
||||||
m.initializingSince = undefined
|
m.initializingSince = undefined;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
// Warmup failures must not permanently block future initialization.
|
// Warmup failures must not permanently block future initialization.
|
||||||
this.clients.delete(key)
|
this.clients.delete(key);
|
||||||
void client.stop().catch(() => {})
|
void client.stop().catch(() => {});
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
releaseClient(root: string, serverId: string): void {
|
releaseClient(root: string, serverId: string): void {
|
||||||
const key = this.getKey(root, serverId)
|
const key = this.getKey(root, serverId);
|
||||||
const managed = this.clients.get(key)
|
const managed = this.clients.get(key);
|
||||||
if (managed && managed.refCount > 0) {
|
if (managed && managed.refCount > 0) {
|
||||||
managed.refCount--
|
managed.refCount--;
|
||||||
managed.lastUsedAt = Date.now()
|
managed.lastUsedAt = Date.now();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
isServerInitializing(root: string, serverId: string): boolean {
|
isServerInitializing(root: string, serverId: string): boolean {
|
||||||
const key = this.getKey(root, serverId)
|
const key = this.getKey(root, serverId);
|
||||||
const managed = this.clients.get(key)
|
const managed = this.clients.get(key);
|
||||||
return managed?.isInitializing ?? false
|
return managed?.isInitializing ?? false;
|
||||||
}
|
}
|
||||||
|
|
||||||
async stopAll(): Promise<void> {
|
async stopAll(): Promise<void> {
|
||||||
|
this.cleanupHandle?.unregister();
|
||||||
|
this.cleanupHandle = null;
|
||||||
for (const [, managed] of this.clients) {
|
for (const [, managed] of this.clients) {
|
||||||
await managed.client.stop()
|
await managed.client.stop();
|
||||||
}
|
}
|
||||||
this.clients.clear()
|
this.clients.clear();
|
||||||
if (this.cleanupInterval) {
|
if (this.cleanupInterval) {
|
||||||
clearInterval(this.cleanupInterval)
|
clearInterval(this.cleanupInterval);
|
||||||
this.cleanupInterval = null
|
this.cleanupInterval = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async cleanupTempDirectoryClients(): Promise<void> {
|
async cleanupTempDirectoryClients(): Promise<void> {
|
||||||
await cleanupTempDirectoryLspClients(this.clients)
|
await cleanupTempDirectoryLspClients(this.clients);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const lspManager = LSPServerManager.getInstance()
|
export const lspManager = LSPServerManager.getInstance();
|
||||||
|
|||||||
Reference in New Issue
Block a user