Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

fix(subagents): enforce allowedTools as a runtime restriction

SeashoreShi0f5fe787160c
diff --git a/src/core/sub-agent.ts b/src/core/sub-agent.ts
index ce65d849..45f5b434 100644
--- a/src/core/sub-agent.ts
+++ b/src/core/sub-agent.ts
@@ -14,6 +14,7 @@ import type { SaverMode } from './saver-mode.js';
import { getHeapStatistics } from 'node:v8';
import { memoryGovernorThresholds, memoryGovernorVerdict, CONVERSATION_TOOL_BUDGET_CHARS, TOOL_RESULT_KEEP_RECENT, summarizeToolResult } from './memory-governor.js';
import { classifyStreamCompletion } from './stream-completion.js';
+import { filterToolsByAllowlist } from '../utils/tool-filter.js';
import { logger } from '../utils/logger.js';
export type ProgressCallback = (agentId: string, progress: string) => void;
@@ -206,7 +207,7 @@ export class SubAgent {
model: provider.getModelInstance(),
system: systemPrompt,
messages,
- tools: this.capabilities.getTools(),
+ tools: this.resolveTools(),
stopWhen: stepCountIs(stepsRemaining),
abortSignal: this.abortController.signal,
// Stop the SDK retaining raw HTTP bodies in every step's result
@@ -486,6 +487,15 @@ export class SubAgent {
}
}
+ /**
+ * Tools handed to the model. `allowedTools` is a real runtime restriction:
+ * when set, the child never sees tools outside the list even though the
+ * shared registry would otherwise expose them (e.g. list_agents/stop_agent).
+ */
+ private resolveTools() {
+ return filterToolsByAllowlist(this.capabilities.getTools(), this.config.allowedTools);
+ }
+
private buildSystemPrompt(): string {
let prompt = this.identity.getSystemPrompt(this.agentConfig.identity);
diff --git a/src/utils/tool-filter.test.ts b/src/utils/tool-filter.test.ts
new file mode 100644
index 00000000..04e7d923
--- /dev/null
+++ b/src/utils/tool-filter.test.ts
@@ -0,0 +1,31 @@
+import { describe, expect, it } from 'vitest';
+import { filterToolsByAllowlist } from './tool-filter.js';
+
+const tools = {
+ read_file: 1,
+ write_file: 2,
+ list_agents: 3,
+ stop_agent: 4,
+};
+
+describe('filterToolsByAllowlist', () => {
+ it('returns every tool when no allowlist is given', () => {
+ expect(filterToolsByAllowlist(tools)).toEqual(tools);
+ expect(filterToolsByAllowlist(tools, [])).toEqual(tools);
+ });
+
+ it('keeps only the allowed tools', () => {
+ expect(filterToolsByAllowlist(tools, ['read_file'])).toEqual({ read_file: 1 });
+ expect(filterToolsByAllowlist(tools, ['read_file', 'write_file'])).toEqual({ read_file: 1, write_file: 2 });
+ });
+
+ it('drops orchestration tools that are not allowlisted', () => {
+ const filtered = filterToolsByAllowlist(tools, ['read_file']);
+ expect(filtered).not.toHaveProperty('list_agents');
+ expect(filtered).not.toHaveProperty('stop_agent');
+ });
+
+ it('ignores allowlist entries that match no tool', () => {
+ expect(filterToolsByAllowlist(tools, ['read_file', 'does_not_exist'])).toEqual({ read_file: 1 });
+ });
+});
diff --git a/src/utils/tool-filter.ts b/src/utils/tool-filter.ts
new file mode 100644
index 00000000..8a685743
--- /dev/null
+++ b/src/utils/tool-filter.ts
@@ -0,0 +1,9 @@
+/**
+ * Filter a tool map down to an allowlist. A missing or empty allowlist means
+ * "no restriction" and returns the full map unchanged.
+ */
+export function filterToolsByAllowlist<T>(all: Record<string, T>, allowed?: readonly string[]): Record<string, T> {
+ if (!allowed || allowed.length === 0) return all;
+ const allowedSet = new Set(allowed);
+ return Object.fromEntries(Object.entries(all).filter(([name]) => allowedSet.has(name)));
+}