Files
Netcatty/electron/bridges/mcpServerBridge.approvalTimeout.test.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

105 lines
3.2 KiB
JavaScript

"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
const { EventEmitter } = require("node:events");
const { setTimeout: delay } = require("node:timers/promises");
const mcpServerBridge = require("./mcpServerBridge.cjs");
function createFakeWindow() {
const contents = new EventEmitter();
contents.id = Math.floor(Math.random() * 1e6);
contents.send = (channel, payload) => {
contents.emit("send", channel, payload);
};
contents.isDestroyed = () => false;
return {
isDestroyed: () => false,
webContents: contents,
};
}
function stubNow(t, startMs) {
const realNow = Date.now;
let now = startMs;
Date.now = () => now;
t.after(() => {
Date.now = realNow;
});
return {
advance: (deltaMs) => {
now += deltaMs;
},
};
}
test("cancelApprovalTimeoutFromRenderer keeps the absolute MCP deadline armed", async (t) => {
const win = createFakeWindow();
mcpServerBridge.setMainWindowGetter(() => win);
t.after(() => {
mcpServerBridge.clearPendingApprovals();
mcpServerBridge.setMainWindowGetter(() => null);
});
const requests = [];
const cleared = [];
win.webContents.on("send", (channel, payload) => {
if (channel === "netcatty:ai:mcp:approval-request") requests.push(payload);
if (channel === "netcatty:ai:mcp:approval-cleared") {
cleared.push(...(payload?.approvalIds || []));
}
});
const clock = stubNow(t, 1_000_000);
const approvalPromise = mcpServerBridge.requestApprovalFromRenderer(
"terminal_execute",
{ command: "echo hi" },
"chat-mcp-absolute",
);
assert.equal(requests.length, 1);
const approvalId = requests[0].approvalId;
// Jump close to the 110s absolute ceiling, then cancel idle. Remaining absolute ~40ms.
clock.advance(109_960);
assert.equal(mcpServerBridge.cancelApprovalTimeoutFromRenderer(approvalId), true);
assert.equal(mcpServerBridge.cancelApprovalTimeoutFromRenderer(approvalId), false);
await delay(20);
assert.equal(cleared.includes(approvalId), false, "must stay pending before absolute expiry");
const outcome = await Promise.race([
approvalPromise.then((approved) => ({ approved })),
delay(120).then(() => ({ approved: "timeout-wait" })),
]);
assert.deepEqual(outcome, { approved: false });
assert.ok(cleared.includes(approvalId));
});
test("cancelApprovalTimeoutFromRenderer still allows explicit approve before absolute expiry", async (t) => {
const win = createFakeWindow();
mcpServerBridge.setMainWindowGetter(() => win);
t.after(() => {
mcpServerBridge.clearPendingApprovals();
mcpServerBridge.setMainWindowGetter(() => null);
});
const requests = [];
win.webContents.on("send", (channel, payload) => {
if (channel === "netcatty:ai:mcp:approval-request") requests.push(payload);
});
stubNow(t, 2_000_000);
const approvalPromise = mcpServerBridge.requestApprovalFromRenderer(
"sftp_write",
{ path: "/tmp/x" },
"chat-mcp-approve",
);
const approvalId = requests[0].approvalId;
assert.equal(mcpServerBridge.cancelApprovalTimeoutFromRenderer(approvalId), true);
mcpServerBridge.resolveApprovalFromRenderer(approvalId, true);
assert.equal(await approvalPromise, true);
});