diff --git a/electron/capabilities/codegen/toolSurfaces.test.cjs b/electron/capabilities/codegen/toolSurfaces.test.cjs index 5a7c411ba..4aabdfe94 100644 --- a/electron/capabilities/codegen/toolSurfaces.test.cjs +++ b/electron/capabilities/codegen/toolSurfaces.test.cjs @@ -106,6 +106,14 @@ test("core MCP descriptions strongly route live terminal work through Netcatty", assert.match(vaultHosts?.description || "", /host_open/i); }); +test("vault_hosts_list exposes optional query and group filters", () => { + const vaultHosts = listMcpTools().find((tool) => tool.mcpTool === "vault_hosts_list"); + assert.ok(vaultHosts); + assert.deepEqual(Object.keys(vaultHosts.inputShape).sort(), ["group", "query"]); + assert.equal(vaultHosts.inputShape.query.optional, true); + assert.equal(vaultHosts.inputShape.group.optional, true); +}); + test("session_close is exposed to agents and external MCP clients", () => { const mcpTool = listMcpTools().find((tool) => tool.mcpTool === "session_close"); assert.ok(mcpTool); diff --git a/electron/capabilities/schemas/toolInputs.cjs b/electron/capabilities/schemas/toolInputs.cjs index 0603de4d3..1350cb679 100644 --- a/electron/capabilities/schemas/toolInputs.cjs +++ b/electron/capabilities/schemas/toolInputs.cjs @@ -86,7 +86,10 @@ const TOOL_INPUT_FIELDS = Object.freeze({ "vault.host.get": { hostId: { type: "string", description: "Vault host ID." }, }, - "vault.host.list": {}, + "vault.host.list": { + query: { type: "string", optional: true, description: "Optional case-insensitive search text. Every space-separated word must match the label, hostname, username, or tags. Does not search group." }, + group: { type: "string", optional: true, description: "Optional group path (e.g. prod). Includes subgroups: prod matches prod/web but not production. Case-sensitive." }, + }, "vault.host.open": { hostId: { type: "string", description: "Vault host ID to open. Use vault_hosts_list / host_get to resolve id from label or hostname." }, workspaceId: { type: "string", optional: true, description: "Target workspace ID. Omit to open into the invoking chat's bound workspace (workspace chat → that workspace; terminal chat → that terminal's workspace); unbound chats get a standalone tab. 'current' means the chat's bound workspace, or the active workspace when unbound. Applies to newly opened sessions; reused sessions retain their placement." }, @@ -430,7 +433,7 @@ const MODEL_DESCRIPTION_HINTS = Object.freeze({ "vault.host.open": "Reuse-first: if this hostId already has a connected or connecting session with matching connection settings (including one auto-reconnecting after a drop, which keeps the same sessionId), that sessionId is returned and no new tab is opened — retry terminal_execute against it rather than reopening the host. Open a new tab only when get_environment shows no session for the host, when the task truly needs a parallel connection, or with newTab true. Call session_close when finished only for a newly opened session (reused is not true). A reused session keeps its original owner; do not close it as task cleanup. Disconnected or restored tabs needing user action are not reused. Connection may still be establishing when the tool returns — use get_environment or wait briefly before terminal_execute if needed. Auth prompts (passphrase / keyboard-interactive) still require the user in the Netcatty UI.", "vault.host.list": - "Use when the user names a saved Netcatty host but get_environment has no matching live session. Resolve the hostId here, call host_open, then call get_environment to obtain the new sessionId.", + "Use when the user names a saved Netcatty host but get_environment has no matching live session. Resolve the hostId here, call host_open, then call get_environment to obtain the new sessionId. Use query with the host name the user gave, or group, to narrow the list instead of returning every host.", "vault.host.import": "Only for text in known export formats (PuTTY reg, MobaXterm ini, CSV template, SecureCRT, FinalShell JSON, ssh_config). If attached host text is unknown or auto-detection fails, use read_attachment content, extract fields yourself, and call vault_hosts_create.", "vault.hosts.create": diff --git a/electron/capabilities/services/vaultService.cjs b/electron/capabilities/services/vaultService.cjs index d7c55bdfa..f8449f4e0 100644 --- a/electron/capabilities/services/vaultService.cjs +++ b/electron/capabilities/services/vaultService.cjs @@ -36,10 +36,13 @@ function createVaultService(ctx = {}) { if (bridgeErr) return bridgeErr; return invokeVaultAgent("host.get", { hostId: params.hostId }); }, - listHosts: async () => { + listHosts: async (params = {}) => { const bridgeErr = requireBridge(); if (bridgeErr) return bridgeErr; - return invokeVaultAgent("host.list", {}); + return invokeVaultAgent("host.list", { + query: params.query, + group: params.group, + }); }, openHost: async (params = {}) => { const bridgeErr = requireBridge(); diff --git a/infrastructure/ai/harness/generated/cattyToolSpecs.json b/infrastructure/ai/harness/generated/cattyToolSpecs.json index 9b986a46d..b2620ac1b 100644 --- a/infrastructure/ai/harness/generated/cattyToolSpecs.json +++ b/infrastructure/ai/harness/generated/cattyToolSpecs.json @@ -593,8 +593,19 @@ "toolName": "vault_hosts_list", "rpcMethod": "vault/hosts/list", "localExecution": false, - "description": "List saved hosts in the vault (metadata only — no passwords or keys). Use when the user names a saved Netcatty host but get_environment has no matching live session. Resolve the hostId here, call host_open, then call get_environment to obtain the new sessionId.", - "inputShape": {}, + "description": "List saved hosts in the vault (metadata only — no passwords or keys). Use when the user names a saved Netcatty host but get_environment has no matching live session. Resolve the hostId here, call host_open, then call get_environment to obtain the new sessionId. Use query with the host name the user gave, or group, to narrow the list instead of returning every host.", + "inputShape": { + "query": { + "type": "string", + "optional": true, + "description": "Optional case-insensitive search text. Every space-separated word must match the label, hostname, username, or tags. Does not search group." + }, + "group": { + "type": "string", + "optional": true, + "description": "Optional group path (e.g. prod). Includes subgroups: prod matches prod/web but not production. Case-sensitive." + } + }, "policy": { "write": false, "sensitiveRead": true, diff --git a/infrastructure/ai/harness/generated/globalAgentToolSpecs.json b/infrastructure/ai/harness/generated/globalAgentToolSpecs.json index f73746471..f6a27a98e 100644 --- a/infrastructure/ai/harness/generated/globalAgentToolSpecs.json +++ b/infrastructure/ai/harness/generated/globalAgentToolSpecs.json @@ -615,8 +615,19 @@ "toolName": "vault_hosts_list", "rpcMethod": "vault/hosts/list", "localExecution": false, - "description": "List saved hosts in the vault (metadata only — no passwords or keys). Use when the user names a saved Netcatty host but get_environment has no matching live session. Resolve the hostId here, call host_open, then call get_environment to obtain the new sessionId.", - "inputShape": {}, + "description": "List saved hosts in the vault (metadata only — no passwords or keys). Use when the user names a saved Netcatty host but get_environment has no matching live session. Resolve the hostId here, call host_open, then call get_environment to obtain the new sessionId. Use query with the host name the user gave, or group, to narrow the list instead of returning every host.", + "inputShape": { + "query": { + "type": "string", + "optional": true, + "description": "Optional case-insensitive search text. Every space-separated word must match the label, hostname, username, or tags. Does not search group." + }, + "group": { + "type": "string", + "optional": true, + "description": "Optional group path (e.g. prod). Includes subgroups: prod matches prod/web but not production. Case-sensitive." + } + }, "policy": { "write": false, "sensitiveRead": true, diff --git a/infrastructure/ai/vaultAgentBridgeClient.test.ts b/infrastructure/ai/vaultAgentBridgeClient.test.ts index a7379ee73..8848b3000 100644 --- a/infrastructure/ai/vaultAgentBridgeClient.test.ts +++ b/infrastructure/ai/vaultAgentBridgeClient.test.ts @@ -442,6 +442,100 @@ describe('handleVaultAgentOp vault hosts', () => { assert.equal('password' in (hosts?.[0] ?? {}), false); }); + describe('host.list filters', () => { + const hosts: Host[] = [ + { id: 'h1', label: 'web-1', hostname: '10.0.0.1', username: 'deploy', port: 22, group: 'prod/web', tags: ['nginx'], os: 'linux' }, + { id: 'h2', label: 'db-1', hostname: '10.0.0.2', username: 'root', port: 22, group: 'prod/db', tags: ['postgres'], os: 'linux' }, + { id: 'h3', label: 'old-box', hostname: '10.0.0.3', username: 'root', port: 22, group: 'production-old', tags: [], os: 'linux' }, + { id: 'h4', label: 'prod', hostname: '10.0.0.4', username: 'root', port: 22, tags: [], os: 'linux' }, + { id: 'h5', label: 'root-host', hostname: '10.0.0.5', username: 'ops', port: 22, group: 'prod', tags: [], os: 'linux' }, + ]; + + async function listIds(params: Record): Promise { + const result = await handleVaultAgentOp('host.list', params, createDeps({ hosts })); + assert.equal(result.ok, true); + return ((result as { hosts: Array<{ id: string }> }).hosts).map((host) => host.id); + } + + it('returns every host when no filter is given', async () => { + assert.deepEqual(await listIds({}), ['h1', 'h2', 'h3', 'h4', 'h5']); + assert.deepEqual(await listIds({ query: ' ', group: ' ' }), ['h1', 'h2', 'h3', 'h4', 'h5']); + }); + + it('query matches label, hostname, username, and tags case-insensitively', async () => { + assert.deepEqual(await listIds({ query: 'WEB-1' }), ['h1']); + assert.deepEqual(await listIds({ query: '10.0.0.2' }), ['h2']); + assert.deepEqual(await listIds({ query: 'deploy' }), ['h1']); + assert.deepEqual(await listIds({ query: 'Postgres' }), ['h2']); + }); + + it('query requires every whitespace-separated word to match', async () => { + assert.deepEqual(await listIds({ query: 'root db' }), ['h2']); + assert.deepEqual(await listIds({ query: 'root nomatch' }), []); + }); + + it('query does not match group', async () => { + assert.deepEqual(await listIds({ query: 'production' }), []); + assert.deepEqual(await listIds({ query: 'prod/web' }), []); + }); + + it('group matches the group and its subgroups on a path boundary', async () => { + assert.deepEqual(await listIds({ group: 'prod' }), ['h1', 'h2', 'h5']); + assert.deepEqual(await listIds({ group: 'prod/db' }), ['h2']); + assert.deepEqual(await listIds({ group: 'production-old' }), ['h3']); + }); + + it('group tolerates surrounding whitespace and trailing slashes', async () => { + assert.deepEqual(await listIds({ group: ' prod/ ' }), ['h1', 'h2', 'h5']); + assert.deepEqual(await listIds({ group: 'prod/db//' }), ['h2']); + }); + + it('group is case-sensitive and ignores ungrouped hosts', async () => { + assert.deepEqual(await listIds({ group: 'PROD' }), []); + assert.equal((await listIds({ group: 'prod' })).includes('h4'), false); + }); + + it('treats missing, null and non-string filters as no filter', async () => { + const all = ['h1', 'h2', 'h3', 'h4', 'h5']; + assert.deepEqual(await listIds(undefined as unknown as Record), all); + assert.deepEqual(await listIds(null as unknown as Record), all); + assert.deepEqual(await listIds({ query: null, group: null }), all); + assert.deepEqual(await listIds({ query: undefined, group: undefined }), all); + assert.deepEqual(await listIds({ query: 123, group: { a: 1 } }), all); + assert.deepEqual(await listIds({ query: ['web'], group: ['prod'] }), all); + assert.deepEqual(await listIds({ group: '/' }), all); + }); + + it('treats query characters literally, not as a pattern', async () => { + assert.deepEqual(await listIds({ query: '10.0.0.1' }), ['h1']); + assert.deepEqual(await listIds({ query: '.*' }), []); + assert.deepEqual(await listIds({ query: '(' }), []); + }); + + it('does not crash on hosts with missing or malformed fields', async () => { + const messy = [ + { id: 'm1', label: 'solo', hostname: 'solo.example.com', port: 22, tags: 'oops', os: 'linux' }, + { id: 'm2', hostname: 'bare.example.com', port: 22, tags: null, os: 'linux' }, + { id: 'm3', label: 'tagged', hostname: 'tagged.example.com', port: 22, tags: 5, os: 'linux' }, + ] as unknown as Host[]; + const result = await handleVaultAgentOp('host.list', { query: 'example' }, createDeps({ hosts: messy })); + assert.deepEqual((result as { hosts: Array<{ id: string }> }).hosts.map((h) => h.id), ['m1', 'm2', 'm3']); + const grouped = await handleVaultAgentOp('host.list', { group: 'prod' }, createDeps({ hosts: messy })); + assert.deepEqual((grouped as { hosts: unknown[] }).hosts, []); + }); + + it('returns an empty list, not an error, when nothing matches', async () => { + const result = await handleVaultAgentOp('host.list', { query: 'zzz' }, createDeps({ hosts })); + assert.deepEqual(result, { ok: true, hosts: [] }); + }); + + it('combines query and group', async () => { + // h2 matches via username, h5 via its label; h3/h4 match the query but sit outside the group. + assert.deepEqual(await listIds({ group: 'prod', query: 'root' }), ['h2', 'h5']); + assert.deepEqual(await listIds({ group: 'prod/db', query: 'root' }), ['h2']); + }); + }); + it('sanitizeHostForAgent omits TOTP secrets', () => { const sanitized = sanitizeHostForAgent({ id: 'host-1', diff --git a/infrastructure/ai/vaultAgentBridgeClient.ts b/infrastructure/ai/vaultAgentBridgeClient.ts index bd57d410e..689c4e695 100644 --- a/infrastructure/ai/vaultAgentBridgeClient.ts +++ b/infrastructure/ai/vaultAgentBridgeClient.ts @@ -156,6 +156,26 @@ function summarizeHostForList(host: Host) { }; } +type HostListSummary = ReturnType; + +/** Empty filter matches everything. Matches the group itself or any subgroup, on a path boundary. */ +function hostMatchesGroupFilter(host: HostListSummary, group: string): boolean { + if (!group) return true; + const hostGroup = host.group ?? ''; + return hostGroup === group || hostGroup.startsWith(`${group}/`); +} + +/** Every term must appear (case-insensitive) in label, hostname, username, or tags. Group is deliberately excluded. */ +function hostMatchesQueryTerms(host: HostListSummary, terms: string[]): boolean { + if (terms.length === 0) return true; + const tags = Array.isArray(host.tags) ? host.tags : []; + const haystack = [host.label, host.hostname, host.username, ...tags] + .filter((value): value is string => typeof value === 'string') + .join('\n') + .toLowerCase(); + return terms.every((term) => haystack.includes(term)); +} + function parseOptionalBoolean(value: unknown): boolean | undefined { if (value === undefined || value === null) return undefined; if (typeof value === 'boolean') return value; @@ -690,10 +710,18 @@ export async function handleVaultAgentOp( return { ok: true, host: sanitizeHostForAgent(deps.resolveEffectiveHost(host)) }; } case 'host.list': { - return { - ok: true, - hosts: deps.getHosts().map((host) => summarizeHostForList(deps.resolveEffectiveHost(host))), - }; + // Filters are optional: anything missing or not a string means "no filter". + const rawQuery = params?.query; + const rawGroup = params?.group; + const queryTerms = typeof rawQuery === 'string' + ? rawQuery.toLowerCase().split(/\s+/).filter(Boolean) + : []; + const group = typeof rawGroup === 'string' ? rawGroup.trim().replace(/\/+$/, '') : ''; + const hosts = deps + .getHosts() + .map((host) => summarizeHostForList(deps.resolveEffectiveHost(host))) + .filter((host) => hostMatchesGroupFilter(host, group) && hostMatchesQueryTerms(host, queryTerms)); + return { ok: true, hosts }; } case 'host.open': { if (params.workspaceId !== undefined && (typeof params.workspaceId !== 'string' || !params.workspaceId.trim())) {