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.

feat(core): mode-aware tool exposure — leave Spotify schemas out of unrelated turns

Salman Qureshi17bf649cc941
diff --git a/src/core/agent.ts b/src/core/agent.ts
index 58632666..35ac674b 100644
--- a/src/core/agent.ts
+++ b/src/core/agent.ts
@@ -90,6 +90,7 @@ import { buildStatusVerbPrompt, parseStatusVerbs, shouldRefreshStatusVerbs } fro
import { verbPoolFor } from '../ui/status-word.js';
import { StallWatchdog } from './stall-watchdog.js';
import { setProgressPulse } from './progress-pulse.js';
+import { selectActiveTools } from './tool-exposure.js';
import { buildFileChangePreview } from '../utils/file-preview.js';
import { whatsNewText } from '../utils/whats-new.js';
@@ -3190,6 +3191,14 @@ export class Agent {
const volatileSystem = contextBlocks.length > 0
? `## Context for this turn\n\n${contextBlocks.join('\n\n')}`
: '';
+ // Mode-aware tool exposure: niche tool groups (Spotify) are left out of
+ // the request unless the turn is about them. Schemas are re-sent on
+ // every step, so this saves tokens on every step of every turn.
+ const activeTools = selectActiveTools(
+ Object.keys(this.capabilities.getTools()),
+ msg.content,
+ recentMemory.map((m) => m.content),
+ );
// ── Skill Intent Routing & Batch Execution ──
//
@@ -3485,6 +3494,7 @@ export class Agent {
messages: this.withCachedSystem(systemPrompt, providerMessages, volatileSystem),
tools: this.programmingMode.isPlan() ? this.capabilities.getPlanTools() : this.capabilities.getTools(),
maxOutputTokens: providerMaxOutputTokens,
+ ...(activeTools ? { activeTools } : {}),
stopWhen: stepCountIs(effectiveMaxSteps),
abortSignal: loopAbortController.signal,
// Memory: the SDK retains a structuredClone of the whole
@@ -3939,6 +3949,7 @@ export class Agent {
messages: this.withCachedSystem(systemPrompt, providerMessages, volatileSystem),
tools: this.programmingMode.isPlan() ? this.capabilities.getPlanTools() : this.capabilities.getTools(),
maxOutputTokens: providerMaxOutputTokens,
+ ...(activeTools ? { activeTools } : {}),
stopWhen: stepCountIs(effectiveMaxSteps),
abortSignal: loopAbortController.signal,
// Same O(N²) step retention as streamText (see comment above).
@@ -4991,6 +5002,7 @@ export class Agent {
steps: this.completedStepCount,
toolCalls: turnToolTrace.length,
historyEntries: recentMemory.length,
+ activeTools: activeTools?.length ?? 'all',
elapsedMs: Date.now() - startTime,
}, 'Turn usage');
this.syncTokenInfoToCli();
diff --git a/src/core/tool-exposure.test.ts b/src/core/tool-exposure.test.ts
new file mode 100644
index 00000000..e96e639c
--- /dev/null
+++ b/src/core/tool-exposure.test.ts
@@ -0,0 +1,28 @@
+import { describe, expect, it } from 'vitest';
+import { selectActiveTools } from './tool-exposure.js';
+
+const ALL = ['read_file', 'write_file', 'run_command', 'spotify_play', 'spotify_pause', 'spotify_search'];
+
+describe('selectActiveTools (ROADMAP P1.4)', () => {
+ it('drops Spotify tools from an unrelated turn', () => {
+ expect(selectActiveTools(ALL, 'fix the failing test in src/app.ts')).toEqual(['read_file', 'write_file', 'run_command']);
+ });
+
+ it('keeps everything when the message is about music', () => {
+ expect(selectActiveTools(ALL, 'play some lo-fi on spotify')).toBeUndefined();
+ expect(selectActiveTools(ALL, 'skip this track')).toBeUndefined();
+ });
+
+ it('keeps everything when recent history is about music', () => {
+ expect(selectActiveTools(ALL, 'next one please', ['I put on your focus playlist.'])).toBeUndefined();
+ });
+
+ it('only looks at the last few history entries', () => {
+ const old = ['playlist', 'a', 'b', 'c', 'd', 'e'];
+ expect(selectActiveTools(ALL, 'summarise the README', old)).toEqual(['read_file', 'write_file', 'run_command']);
+ });
+
+ it('returns undefined when there is nothing to trim', () => {
+ expect(selectActiveTools(['read_file', 'run_command'], 'anything')).toBeUndefined();
+ });
+});
diff --git a/src/core/tool-exposure.ts b/src/core/tool-exposure.ts
new file mode 100644
index 00000000..dbea4c63
--- /dev/null
+++ b/src/core/tool-exposure.ts
@@ -0,0 +1,28 @@
+/**
+ * Mode-aware tool exposure.
+ *
+ * Every tool schema is re-sent on every step of a turn (up to 75 steps), and
+ * niche groups cost real tokens: the 14 Spotify tools alone are roughly 1.7K
+ * tokens of schema. They stay registered — the model can still be routed to
+ * them — but they are only included in a request when the turn looks like it
+ * is about them. Returns undefined when nothing should be trimmed so callers
+ * can pass the AI SDK `activeTools` option through unchanged.
+ */
+
+const SPOTIFY_PREFIX = 'spotify_';
+const SPOTIFY_HINT = /\b(spotify|music|song|songs|playlist|playlists|track|tracks|album|albums|artist|volume|play|pause|resume|skip|shuffle|now playing|listening|queue)\b/i;
+
+/** How many recent history entries count as context for the hint check. */
+const RECENT_CONTEXT_ENTRIES = 4;
+
+export function selectActiveTools(
+ allToolNames: readonly string[],
+ message: string,
+ recentHistory: readonly string[] = [],
+): string[] | undefined {
+ const spotify = allToolNames.filter((name) => name.startsWith(SPOTIFY_PREFIX));
+ if (spotify.length === 0) return undefined;
+ const corpus = [message, ...recentHistory.slice(-RECENT_CONTEXT_ENTRIES)].join('\n');
+ if (SPOTIFY_HINT.test(corpus)) return undefined;
+ return allToolNames.filter((name) => !name.startsWith(SPOTIFY_PREFIX));
+}