Files
Netcatty/electron/bridges/aiBridge/agentProcessHandlers.cjs
T
9a3d96be76 fix(ai): improve approval command readability and cancel idle timeout on review (#2734)
* fix(#2697): automated Cursor CLI fix

* fix: address Codex review on PR #2734

* fix(ai): keep MCP absolute approval deadline and Escape reject

Canceling idle review no longer leaves MCP approvals unbounded past the
external agent tool-call window, and Enter-only key stopPropagation lets
Escape still reject from Copy/Expand controls.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(ai): keep wrapped CLI target context on approval cards

Parse --session from netcatty-tool-cli wrappers into execution context and
still show invocation args when the pending card displays an unwrapped command.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(ai): surface Codex reason on pending approval cards

Keep agent justification visible in the execution-context strip and still
show extra invocation args such as commandActions beside the command block.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(ai): bound Catty approval cancel with absolute deadline

Keep Catty confirm-mode approvals from going unbounded after review:
cancelApprovalTimeout re-arms the creation absolute deadline (MCP-style)
so a late approve cannot outlive the original timeout/stream tool budget.

Also harden pending-card Enter: extract isNestedInteractiveApprovalTarget
so Copy/Expand (and other nested controls) never Approve Once via bubble.

* fix(ai): bound Codex approval cancel and unwrap CLI wrappers

cancelInteractionTimeout now re-arms the absolute creation deadline
(Catty/MCP style) instead of leaving review unbounded. Also treat
netcatty-tool-cli.cjs/.cmd as CLI wrappers so approval Command/Copy
shows the remote command after --.

* fix: address Codex review on PR #2734

* fix(ai): separate Catty approval idle timer from hard deadline

Review activity re-arms a full idle window from now instead of the original
absolute remainder, capped by a hard deadline (3x idle, max 30m). Stream
tool budgets use the hard deadline so mid-review approvals are not cut off.

* fix(ai): re-arm Catty approval idle and sync-deny hard deadline

Review interactions kept a one-shot timeoutCancelledRef, so later
cancelApprovalTimeout calls no longer re-armed idle. Remove the one-shot
and deny synchronously when the hard deadline has already elapsed so a
late approve cannot race setTimeout(0).

* fix(ai): re-arm approval idle on args-only scroll review

Pending approvals without a display command (Codex file-change/permissions,
write tools with JSON args) only show the overflow args block. Wire the same
onScroll/onWheel markReviewing path used by the command pre so continuous
wheel/trackpad review re-arms Catty and MCP/Codex idle timers.

---------

Co-authored-by: netcatty-bot <308658023+netcatty-bot@users.noreply.github.com>
Co-authored-by: bincxz <16399091+binaricat@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-06 08:53:36 +08:00

145 lines
6.4 KiB
JavaScript

/* eslint-disable no-undef */
function registerAgentProcessHandlers(ctx) {
with (ctx) {
const maxCommandTimeoutSeconds = 24 * 60 * 60;
// ── MCP Server session metadata ──
ipcMain.handle("netcatty:ai:mcp:update-sessions", async (event, { sessions: sessionList, chatSessionId }) => {
if (!validateSenderOrSettings(event)) return { ok: false, error: "Unauthorized IPC sender" };
const list = Array.isArray(sessionList) ? sessionList : [];
const externalId = mcpServerBridge.EXTERNAL_MCP_CHAT_SESSION_ID;
if (chatSessionId === externalId) {
// App-wide External MCP scope is owned by the main-window full-session sync.
// Reject writes while disabled so in-flight renderer pushes cannot resurrect
// metadata after stopActiveRuntime cleared the scope.
try {
const external = typeof getExternalMcpController === "function"
? getExternalMcpController()
: null;
if (!external?.isEnabled?.()) {
return { ok: false, error: "External MCP is disabled" };
}
} catch {
return { ok: false, error: "External MCP is unavailable" };
}
}
mcpServerBridge.updateSessionMetadata(list, chatSessionId);
return { ok: true, count: list.length };
});
// Merge (do not replace) session metadata into a chat scope. Used when agents
// open a host mid-turn so terminal tools can target the new sessionId
// without waiting for the next full scope push.
ipcMain.handle("netcatty:ai:mcp:merge-sessions", async (event, { sessions: sessionList, chatSessionId }) => {
if (!validateSenderOrSettings(event)) return { ok: false, error: "Unauthorized IPC sender" };
if (!chatSessionId || typeof chatSessionId !== "string") {
return { ok: false, error: "chatSessionId is required" };
}
const list = Array.isArray(sessionList) ? sessionList : [];
const externalId = mcpServerBridge.EXTERNAL_MCP_CHAT_SESSION_ID;
if (chatSessionId === externalId) {
try {
const external = typeof getExternalMcpController === "function"
? getExternalMcpController()
: null;
if (!external?.isEnabled?.()) {
return { ok: false, error: "External MCP is disabled" };
}
} catch {
return { ok: false, error: "External MCP is unavailable" };
}
}
return mcpServerBridge.mergeSessionMetadata(list, chatSessionId);
});
ipcMain.handle("netcatty:ai:mcp:update-attachments", async (event, { attachments, chatSessionId }) => {
if (!validateSenderOrSettings(event)) return { ok: false, error: "Unauthorized IPC sender" };
mcpServerBridge.updateAttachmentMetadata(attachments || [], chatSessionId);
return { ok: true };
});
ipcMain.handle("netcatty:ai:mcp:set-command-blocklist", async (event, { blocklist }) => {
if (!validateSenderOrSettings(event)) return { ok: false, error: "Unauthorized IPC sender" };
// Validate: must be an array of strings, each a valid regex pattern
if (!Array.isArray(blocklist)) {
return { ok: false, error: "blocklist must be an array" };
}
const validPatterns = [];
for (const pattern of blocklist) {
if (typeof pattern !== "string") continue;
try {
new RegExp(pattern, "i"); // Validate regex
validPatterns.push(pattern);
} catch {
// Skip invalid regex patterns silently
}
}
mcpServerBridge.setCommandBlocklist(validPatterns);
return { ok: true };
});
ipcMain.handle("netcatty:ai:mcp:set-command-timeout", async (event, { timeout }) => {
if (!validateSenderOrSettings(event)) return { ok: false, error: "Unauthorized IPC sender" };
const value = Number(timeout);
if (!Number.isFinite(value) || value < 1 || value > maxCommandTimeoutSeconds) {
return { ok: false, error: `timeout must be a number between 1 and ${maxCommandTimeoutSeconds}` };
}
mcpServerBridge.setCommandTimeout(value);
return { ok: true };
});
ipcMain.handle("netcatty:ai:mcp:set-max-iterations", async (event, { maxIterations }) => {
if (!validateSenderOrSettings(event)) return { ok: false, error: "Unauthorized IPC sender" };
const value = Number(maxIterations);
if (!Number.isFinite(value) || value < 1 || value > 100) {
return { ok: false, error: "maxIterations must be a number between 1 and 100" };
}
mcpServerBridge.setMaxIterations(value);
return { ok: true };
});
ipcMain.handle("netcatty:ai:mcp:set-permission-mode", async (event, { mode }) => {
if (!validateSenderOrSettings(event)) return { ok: false, error: "Unauthorized IPC sender" };
const validModes = ["observer", "confirm", "auto"];
if (!validModes.includes(mode)) {
return { ok: false, error: `mode must be one of: ${validModes.join(", ")}` };
}
mcpServerBridge.setPermissionMode(mode);
return { ok: true };
});
ipcMain.handle("netcatty:ai:mcp:set-tool-integration-mode", async (event, { mode }) => {
if (!validateSenderOrSettings(event)) return { ok: false, error: "Unauthorized IPC sender" };
const validModes = ["mcp", "skills"];
if (!validModes.includes(mode)) {
return { ok: false, error: `mode must be one of: ${validModes.join(", ")}` };
}
setToolIntegrationMode(mode);
return { ok: true };
});
ipcMain.handle("netcatty:ai:mcp:sync-permission-grants", async (event, { grants }) => {
if (!validateSenderOrSettings(event)) return { ok: false, error: "Unauthorized IPC sender" };
mcpServerBridge.setPermissionGrants(grants);
return { ok: true, count: mcpServerBridge.getPermissionGrants().length };
});
// ── MCP Approval response (renderer → main) ──
ipcMain.handle("netcatty:ai:mcp:approval-response", async (event, { approvalId, approved }) => {
// Settings window also hosts External MCP approval cards.
if (!validateSenderOrSettings(event)) return { ok: false, error: "Unauthorized IPC sender" };
mcpServerBridge.resolveApprovalFromRenderer(approvalId, approved);
return { ok: true };
});
// Cancel MCP approval auto-deny after the user starts reviewing the card.
ipcMain.handle("netcatty:ai:mcp:approval-cancel-timeout", async (event, { approvalId }) => {
if (!validateSenderOrSettings(event)) return { ok: false, error: "Unauthorized IPC sender" };
const cancelled = mcpServerBridge.cancelApprovalTimeoutFromRenderer?.(approvalId) === true;
return { ok: true, cancelled };
});
}
}
module.exports = { registerAgentProcessHandlers };