mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-20 07:04:53 +00:00
Contributor GOUKI9999 session_reconnect/session_recover. Maintainer gate fix: do not report ready while still offline/connecting (pending instead); connected keeps advisory shellReady; regenerate capability tool specs. Unit tests 12/12. Electron smoke PASS (SSH shell path). Residual P2 hung-worker readiness left timeout-bounded. No release.
533 lines
19 KiB
JavaScript
533 lines
19 KiB
JavaScript
"use strict";
|
|
|
|
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
|
|
const { createCapabilityRpcDispatcher, UNROUTED } = require("./capabilityRpcDispatch.cjs");
|
|
const { CAPABILITY_SURFACES, PERMISSION_MODES } = require("../../capabilities/constants.cjs");
|
|
const { evaluatePermissionWithGrants } = require("../../capabilities/policy.cjs");
|
|
|
|
function createTestDispatcher(overrides = {}) {
|
|
const invokeVaultAgent = overrides.invokeVaultAgent || (async (op, params) => ({
|
|
ok: true,
|
|
op,
|
|
params,
|
|
}));
|
|
const requestApprovalFromRenderer = overrides.requestApprovalFromRenderer
|
|
|| (async () => true);
|
|
|
|
return createCapabilityRpcDispatcher({
|
|
invokeVaultAgent,
|
|
evaluatePermissionWithGrants: overrides.evaluatePermissionWithGrants || ((input, grants) => ({
|
|
allowed: true,
|
|
requiresApproval: false,
|
|
grants,
|
|
...input,
|
|
})),
|
|
permissionMode: overrides.permissionMode || PERMISSION_MODES.CONFIRM,
|
|
permissionGrantsSnapshot: [],
|
|
isChatSessionCancelled: () => false,
|
|
requestApprovalFromRenderer,
|
|
USER_DENIED_MESSAGE: "User denied the operation.",
|
|
...overrides,
|
|
});
|
|
}
|
|
|
|
test('reconnect respects observer mode and denial before touching transport', async () => {
|
|
let calls = 0;
|
|
for (const mode of [PERMISSION_MODES.OBSERVER, PERMISSION_MODES.CONFIRM]) {
|
|
const dispatch = createTestDispatcher({ permissionMode: mode, evaluatePermissionWithGrants,
|
|
validateSessionReconnect: () => ({ ok: true }),
|
|
requestApprovalFromRenderer: async () => false,
|
|
invokeVaultAgent: async () => { calls++; return { ok: true }; } });
|
|
assert.equal((await dispatch('session/reconnect', { sessionId: 's', chatSessionId: 'chat' })).ok, false);
|
|
assert.equal((await dispatch('session/recover', { sessionId: 's', chatSessionId: 'chat' })).ok, false);
|
|
}
|
|
assert.equal(calls, 0);
|
|
});
|
|
|
|
test('reconnect revalidates scope after approval and supports public RPC', async () => {
|
|
let allowed = true;
|
|
let calls = 0;
|
|
const dispatch = createTestDispatcher({ evaluatePermissionWithGrants,
|
|
validateSessionReconnect: () => allowed ? { ok: true } : { ok: false, error: 'scope revoked' },
|
|
requestApprovalFromRenderer: async () => { allowed = false; return true; },
|
|
invokeVaultAgent: async () => { calls++; return { ok: true }; } });
|
|
const result = await dispatch('public/session/reconnect', { sessionId: 's', chatSessionId: 'chat' });
|
|
assert.equal(result.error, 'scope revoked');
|
|
assert.equal(calls, 0);
|
|
});
|
|
|
|
test("host open does not report success after its scope binding was revoked", async () => {
|
|
const dispatch = createTestDispatcher({
|
|
invokeVaultAgent: async () => ({ ok: true, sessionId: "opened-1" }),
|
|
onHostOpened: async () => false,
|
|
});
|
|
const result = await dispatch("vault/hosts/open", { hostId: "host-1", chatSessionId: "chat-1" });
|
|
assert.equal(result.ok, false);
|
|
assert.equal(result.code, "SESSION_SCOPE_BIND_FAILED");
|
|
});
|
|
|
|
test("host open confirms binding only after the ownership callback completes", async () => {
|
|
const dispatch = createTestDispatcher({
|
|
invokeVaultAgent: async () => ({ ok: true, sessionId: "opened-1" }),
|
|
onHostOpened: async () => true,
|
|
});
|
|
const result = await dispatch("vault/hosts/open", { hostId: "host-1", chatSessionId: "chat-1" });
|
|
assert.equal(result.scopeBound, true);
|
|
});
|
|
|
|
test("dispatchCapabilityRpc returns UNROUTED for netcatty builtin methods", async () => {
|
|
const dispatch = createTestDispatcher();
|
|
const result = await dispatch("netcatty/exec", { chatSessionId: "chat-1" });
|
|
assert.equal(result, UNROUTED);
|
|
});
|
|
|
|
test("dispatchCapabilityRpc routes vault host notes get to vault service", async () => {
|
|
let invokedOp = null;
|
|
const dispatch = createTestDispatcher({
|
|
invokeVaultAgent: async (op, params) => {
|
|
invokedOp = op;
|
|
return { ok: true, hostId: params.hostId, notes: "notes" };
|
|
},
|
|
});
|
|
|
|
const result = await dispatch("vault/host/notes/get", { hostId: "host-1" });
|
|
assert.equal(invokedOp, "host.notes.get");
|
|
assert.equal(result.ok, true);
|
|
assert.equal(result.notes, "notes");
|
|
});
|
|
|
|
test("dispatchCapabilityRpc routes public vault host notes set through approval", async () => {
|
|
const approvalCalls = [];
|
|
const dispatch = createTestDispatcher({
|
|
evaluatePermissionWithGrants: () => ({
|
|
allowed: true,
|
|
requiresApproval: true,
|
|
}),
|
|
requestApprovalFromRenderer: async (toolName, args, chatSessionId) => {
|
|
approvalCalls.push({ toolName, args, chatSessionId });
|
|
return true;
|
|
},
|
|
invokeVaultAgent: async (op, params) => ({
|
|
ok: true,
|
|
op,
|
|
hostId: params.hostId,
|
|
notes: params.notes,
|
|
}),
|
|
});
|
|
|
|
const result = await dispatch("public/vault/hostNotes/set", {
|
|
chatSessionId: "chat-1",
|
|
hostId: "host-1",
|
|
notes: "updated",
|
|
});
|
|
|
|
assert.equal(approvalCalls.length, 1);
|
|
assert.equal(approvalCalls[0].toolName, "host_notes_set");
|
|
assert.equal(result.ok, true);
|
|
assert.equal(result.notes, "updated");
|
|
});
|
|
|
|
test("vault host deletion cannot borrow a different session's host grant", async () => {
|
|
let approvalTarget;
|
|
let deleted = false;
|
|
const dispatch = createTestDispatcher({
|
|
evaluatePermissionWithGrants,
|
|
permissionGrantsSnapshot: [{
|
|
id: "trusted-host", capabilityId: "*", sessionPattern: "host:trusted", createdAt: 1,
|
|
}],
|
|
getApprovalTarget: () => ({ sessionId: "trusted-session", hostId: "trusted", label: "Trusted" }),
|
|
requestApprovalFromRenderer: async (_name, _args, _chatSessionId, context) => {
|
|
approvalTarget = context.target;
|
|
return false;
|
|
},
|
|
invokeVaultAgent: async () => {
|
|
deleted = true;
|
|
return { ok: true };
|
|
},
|
|
});
|
|
|
|
const result = await dispatch("public/vault/hosts/delete", {
|
|
chatSessionId: "__external_mcp__",
|
|
sessionId: "trusted-session",
|
|
hostId: "other-host",
|
|
});
|
|
assert.equal(result.ok, false);
|
|
assert.equal(deleted, false);
|
|
assert.equal(approvalTarget, null);
|
|
});
|
|
|
|
test("host_open names its vault host and cannot borrow another host's grant", async () => {
|
|
const approvals = [];
|
|
const opened = [];
|
|
const hosts = {
|
|
test: { id: "test", label: "Test server", hostname: "test.example" },
|
|
prod: { id: "prod", label: "Production", hostname: "prod.example" },
|
|
temporary: { id: "temporary", label: "Temporary", hostname: "temp.example", ephemeral: true },
|
|
serial: { id: "serial", label: "Serial device", hostname: "ttyUSB0", protocol: "serial" },
|
|
};
|
|
const dispatch = createTestDispatcher({
|
|
evaluatePermissionWithGrants,
|
|
permissionGrantsSnapshot: [{
|
|
id: "test-only", capabilityId: "vault.host.open", sessionPattern: "host:test", createdAt: 1,
|
|
}],
|
|
requestApprovalFromRenderer: async (_name, _args, _chat, context) => {
|
|
approvals.push(context.target);
|
|
return false;
|
|
},
|
|
invokeVaultAgent: async (op, params) => {
|
|
if (op === "host.get") return { ok: true, host: hosts[params.hostId] };
|
|
if (op === "host.open") {
|
|
opened.push(params.hostId);
|
|
return { ok: true, sessionId: `session-${params.hostId}` };
|
|
}
|
|
return { ok: false };
|
|
},
|
|
});
|
|
|
|
assert.equal((await dispatch("public/vault/hosts/open", { hostId: "test" })).ok, true);
|
|
assert.equal((await dispatch("public/vault/hosts/open", { hostId: "prod" })).ok, false);
|
|
assert.equal((await dispatch("public/vault/hosts/open", { hostId: "temporary" })).ok, false);
|
|
assert.equal((await dispatch("public/vault/hosts/open", { hostId: "serial" })).ok, false);
|
|
assert.deepEqual(opened, ["test"]);
|
|
assert.deepEqual(approvals, [
|
|
{ sessionId: "", hostId: "prod", label: "Production", hostname: "prod.example" },
|
|
{ sessionId: "", hostId: "", label: "Temporary", hostname: "temp.example" },
|
|
{ sessionId: "", hostId: "", label: "Serial device", hostname: "ttyUSB0" },
|
|
]);
|
|
});
|
|
|
|
test("dispatchCapabilityRpc asks for approval before importing vault notes from the CLI rpc", async () => {
|
|
const approvalCalls = [];
|
|
let invokedOp = null;
|
|
const dispatch = createTestDispatcher({
|
|
evaluatePermissionWithGrants: () => ({
|
|
allowed: true,
|
|
requiresApproval: true,
|
|
}),
|
|
requestApprovalFromRenderer: async (toolName, args, chatSessionId) => {
|
|
approvalCalls.push({ toolName, args, chatSessionId });
|
|
return true;
|
|
},
|
|
invokeVaultAgent: async (op) => {
|
|
invokedOp = op;
|
|
return { ok: true, importedCount: 1, notes: [] };
|
|
},
|
|
});
|
|
|
|
const result = await dispatch("vault/notes/import", {
|
|
chatSessionId: "chat-1",
|
|
content: "# Steps",
|
|
fileName: "runbook.md",
|
|
});
|
|
|
|
assert.equal(approvalCalls.length, 1);
|
|
assert.equal(approvalCalls[0].toolName, "vault_notes_import");
|
|
assert.equal(approvalCalls[0].chatSessionId, "chat-1");
|
|
assert.equal(approvalCalls[0].args.content, "# Steps");
|
|
assert.equal(invokedOp, "note.import");
|
|
assert.equal(result.importedCount, 1);
|
|
});
|
|
|
|
test("dispatchCapabilityRpc denies vault note import when the user rejects it", async () => {
|
|
let invoked = false;
|
|
const dispatch = createTestDispatcher({
|
|
evaluatePermissionWithGrants: () => ({
|
|
allowed: true,
|
|
requiresApproval: true,
|
|
}),
|
|
requestApprovalFromRenderer: async () => false,
|
|
invokeVaultAgent: async () => {
|
|
invoked = true;
|
|
return { ok: true };
|
|
},
|
|
});
|
|
|
|
const result = await dispatch("vault/notes/import", {
|
|
chatSessionId: "chat-1",
|
|
content: "# Steps",
|
|
});
|
|
|
|
assert.equal(invoked, false);
|
|
assert.equal(result.ok, false);
|
|
assert.match(result.error, /denied/i);
|
|
});
|
|
|
|
test("dispatchCapabilityRpc denies public vault host notes set when approval rejected", async () => {
|
|
const dispatch = createTestDispatcher({
|
|
evaluatePermissionWithGrants: () => ({
|
|
allowed: true,
|
|
requiresApproval: true,
|
|
}),
|
|
requestApprovalFromRenderer: async () => false,
|
|
});
|
|
|
|
const result = await dispatch("public/vault/hostNotes/set", {
|
|
chatSessionId: "chat-1",
|
|
hostId: "host-1",
|
|
notes: "updated",
|
|
});
|
|
|
|
assert.equal(result.ok, false);
|
|
assert.match(result.error, /denied/i);
|
|
});
|
|
|
|
test("dispatchCapabilityRpc closes an owned session through the renderer bridge", async () => {
|
|
const calls = [];
|
|
const closed = [];
|
|
const lifecycle = [];
|
|
const dispatch = createTestDispatcher({
|
|
validateSessionClose: (params) => {
|
|
assert.equal(params.chatSessionId, "chat-1");
|
|
assert.equal(params.sessionId, "session-1");
|
|
return { ok: true };
|
|
},
|
|
beforeSessionClose: async () => {
|
|
await Promise.resolve();
|
|
lifecycle.push("sftp-clean");
|
|
},
|
|
afterSessionClose: async () => {
|
|
lifecycle.push("close-finished");
|
|
},
|
|
onSessionClosed: async (sessionId) => {
|
|
await Promise.resolve();
|
|
lifecycle.push("session-jobs-settled");
|
|
closed.push(sessionId);
|
|
},
|
|
invokeVaultAgent: async (op, params) => {
|
|
lifecycle.push("session-close");
|
|
calls.push({ op, params });
|
|
return { ok: true, sessionId: params.sessionId, status: "closed" };
|
|
},
|
|
});
|
|
|
|
const result = await dispatch("public/session/close", {
|
|
chatSessionId: "chat-1",
|
|
sessionId: "session-1",
|
|
});
|
|
|
|
assert.equal(result.ok, true);
|
|
assert.deepEqual(calls, [{ op: "session.close", params: { sessionId: "session-1" } }]);
|
|
assert.deepEqual(closed, ["session-1"]);
|
|
assert.deepEqual(lifecycle, ["sftp-clean", "session-close", "session-jobs-settled", "close-finished"]);
|
|
});
|
|
|
|
test("dispatchCapabilityRpc refuses to close a session outside ownership", async () => {
|
|
let invoked = false;
|
|
const dispatch = createTestDispatcher({
|
|
validateSessionClose: () => ({ ok: false, error: "not owned" }),
|
|
invokeVaultAgent: async () => {
|
|
invoked = true;
|
|
return { ok: true };
|
|
},
|
|
});
|
|
|
|
const result = await dispatch("public/session/close", {
|
|
chatSessionId: "chat-1",
|
|
sessionId: "session-2",
|
|
});
|
|
|
|
assert.equal(result.ok, false);
|
|
assert.equal(invoked, false);
|
|
});
|
|
|
|
test("dispatchCapabilityRpc preserves the host-open scope generation across the async bridge", async () => {
|
|
const registrations = [];
|
|
const dispatch = createTestDispatcher({
|
|
captureHostOpenScope: (chatSessionId) => {
|
|
assert.equal(chatSessionId, "chat-1");
|
|
return 7;
|
|
},
|
|
onHostOpened: (chatSessionId, sessionId, generation, result) => {
|
|
registrations.push({ chatSessionId, sessionId, generation, result });
|
|
},
|
|
invokeVaultAgent: async () => ({ ok: true, sessionId: "session-1" }),
|
|
});
|
|
|
|
await dispatch("public/vault/hosts/open", { chatSessionId: "chat-1", hostId: "host-1" });
|
|
|
|
assert.deepEqual(registrations, [{
|
|
chatSessionId: "chat-1",
|
|
sessionId: "session-1",
|
|
generation: 7,
|
|
result: { ok: true, sessionId: "session-1" },
|
|
}]);
|
|
});
|
|
|
|
test("dispatchCapabilityRpc routes vault hosts create to vault service", async () => {
|
|
let invokedOp = null;
|
|
const dispatch = createTestDispatcher({
|
|
invokeVaultAgent: async (op, params) => {
|
|
invokedOp = op;
|
|
return { ok: true, addedCount: 1, previewHosts: [] , params };
|
|
},
|
|
});
|
|
|
|
const result = await dispatch("vault/hosts/create", {
|
|
hosts: JSON.stringify([{ hostname: "10.2.0.209", username: "root" }]),
|
|
dryRun: "true",
|
|
});
|
|
assert.equal(invokedOp, "hosts.create");
|
|
assert.equal(result.ok, true);
|
|
});
|
|
|
|
test("dispatchCapabilityRpc routes vault host update to vault service", async () => {
|
|
let invokedOp = null;
|
|
let invokedParams = null;
|
|
const dispatch = createTestDispatcher({
|
|
invokeVaultAgent: async (op, params) => {
|
|
invokedOp = op;
|
|
invokedParams = params;
|
|
return { ok: true, hostId: params.hostId };
|
|
},
|
|
});
|
|
|
|
const result = await dispatch("vault/hosts/update", {
|
|
hostId: "host-1",
|
|
label: "updated",
|
|
});
|
|
assert.equal(invokedOp, "host.update");
|
|
assert.equal(invokedParams.label, "updated");
|
|
assert.equal(result.ok, true);
|
|
});
|
|
|
|
test("dispatchCapabilityRpc routes vault host delete to vault service", async () => {
|
|
let invokedOp = null;
|
|
const dispatch = createTestDispatcher({
|
|
invokeVaultAgent: async (op, params) => {
|
|
invokedOp = op;
|
|
return { ok: true, hostId: params.hostId };
|
|
},
|
|
});
|
|
|
|
const result = await dispatch("vault/hosts/delete", { hostId: "host-1" });
|
|
assert.equal(invokedOp, "host.delete");
|
|
assert.equal(result.ok, true);
|
|
});
|
|
|
|
test("dispatchCapabilityRpc routes vault hosts import to vault service", async () => {
|
|
let invokedOp = null;
|
|
const dispatch = createTestDispatcher({
|
|
invokeVaultAgent: async (op) => {
|
|
invokedOp = op;
|
|
return { ok: true, addedCount: 0 };
|
|
},
|
|
});
|
|
|
|
const result = await dispatch("vault/hosts/import", {
|
|
format: "csv",
|
|
text: "hostname,username\n10.0.0.1,root\n",
|
|
dryRun: "true",
|
|
});
|
|
assert.equal(invokedOp, "host.import");
|
|
assert.equal(result.ok, true);
|
|
});
|
|
|
|
test("dispatchCapabilityRpc routes portforward start to portforward service", async () => {
|
|
let invokedOp = null;
|
|
const dispatch = createTestDispatcher({
|
|
invokeVaultAgent: async (op, params) => {
|
|
invokedOp = op;
|
|
return { ok: true, ruleId: params.ruleId, status: "active" };
|
|
},
|
|
});
|
|
|
|
const result = await dispatch("portforward/start", {
|
|
chatSessionId: "chat-1",
|
|
ruleId: "rule-1",
|
|
});
|
|
assert.equal(invokedOp, "portforward.start");
|
|
assert.equal(result.ok, true);
|
|
assert.equal(result.ruleId, "rule-1");
|
|
});
|
|
|
|
test("dispatchCapabilityRpc lists port forwards from the configured runtime", async () => {
|
|
const calls = [];
|
|
const dispatch = createTestDispatcher({
|
|
listPortForwards: async () => {
|
|
calls.push("list");
|
|
return [{ tunnelId: "worker-pf", status: "active" }];
|
|
},
|
|
});
|
|
|
|
const result = await dispatch("portforward/tunnels/list", {});
|
|
assert.deepEqual(result, {
|
|
ok: true,
|
|
tunnels: [{ tunnelId: "worker-pf", status: "active" }],
|
|
});
|
|
assert.deepEqual(calls, ["list"]);
|
|
});
|
|
|
|
test("dispatchCapabilityRpc reads permissionMode from deps on each call", async () => {
|
|
const seenModes = [];
|
|
const mutableDeps = { permissionMode: PERMISSION_MODES.CONFIRM };
|
|
const liveDispatch = createCapabilityRpcDispatcher({
|
|
invokeVaultAgent: async () => ({ ok: true }),
|
|
evaluatePermissionWithGrants: (input) => {
|
|
seenModes.push(input.permissionMode);
|
|
return { allowed: true, requiresApproval: false };
|
|
},
|
|
get permissionMode() {
|
|
return mutableDeps.permissionMode;
|
|
},
|
|
permissionGrantsSnapshot: [],
|
|
isChatSessionCancelled: () => false,
|
|
requestApprovalFromRenderer: async () => true,
|
|
USER_DENIED_MESSAGE: "User denied the operation.",
|
|
});
|
|
|
|
await liveDispatch("vault/host/get", { hostId: "host-1" });
|
|
mutableDeps.permissionMode = PERMISSION_MODES.AUTO;
|
|
await liveDispatch("vault/host/get", { hostId: "host-2" });
|
|
|
|
assert.deepEqual(seenModes, [PERMISSION_MODES.CONFIRM, PERMISSION_MODES.AUTO]);
|
|
});
|
|
|
|
test("implemented vault capabilities do not return CAPABILITY_NOT_IMPLEMENTED", async () => {
|
|
const dispatch = createTestDispatcher();
|
|
const result = await dispatch("vault/host/get", { hostId: "host-1" });
|
|
assert.notEqual(result.code, "CAPABILITY_NOT_IMPLEMENTED");
|
|
});
|
|
|
|
test("managed privacy is revalidated after capability approval and before writes", async () => {
|
|
const { createAiPrivacyService } = require("../../shared/aiPrivacyService.cjs");
|
|
const { createManagedPrivacyBoundary } = require("../../shared/managedPrivacyBoundary.cjs");
|
|
const privacy = createAiPrivacyService();
|
|
privacy.configure({ enabled: true });
|
|
const token = privacy.redact("approval-chat", "192.0.2.14");
|
|
let invoked = false;
|
|
const dispatch = createManagedPrivacyBoundary(createTestDispatcher({
|
|
evaluatePermissionWithGrants: () => ({ allowed: true, requiresApproval: true }),
|
|
requestApprovalFromRenderer: async (_tool, args) => {
|
|
assert.equal(args.notes, "192.0.2.14");
|
|
privacy.configure({ enabled: false });
|
|
return true;
|
|
},
|
|
invokeVaultAgent: async () => { invoked = true; return { ok: true }; },
|
|
}), privacy);
|
|
await assert.rejects(dispatch("public/vault/hostNotes/set", {
|
|
chatSessionId: "approval-chat", hostId: "host-1", notes: token,
|
|
}), /settings changed|unavailable/);
|
|
assert.equal(invoked, false);
|
|
});
|
|
|
|
test("managed session close revalidates privacy after asynchronous close preparation", async () => {
|
|
const { createAiPrivacyService } = require("../../shared/aiPrivacyService.cjs");
|
|
const { createManagedPrivacyBoundary } = require("../../shared/managedPrivacyBoundary.cjs");
|
|
const privacy = createAiPrivacyService();
|
|
privacy.configure({ enabled: true });
|
|
let invoked = false;
|
|
let outcome;
|
|
const dispatch = createManagedPrivacyBoundary(createTestDispatcher({
|
|
beforeSessionClose: async () => { privacy.configure({ enabled: false }); },
|
|
afterSessionClose: async (_params, result) => { outcome = result; },
|
|
invokeVaultAgent: async () => { invoked = true; return { ok: true }; },
|
|
}), privacy);
|
|
await assert.rejects(dispatch("public/session/close", {
|
|
chatSessionId: "closing-chat", sessionId: "session-1",
|
|
}), /settings changed|unavailable/);
|
|
assert.equal(invoked, false);
|
|
assert.equal(outcome.closed, false);
|
|
});
|