From e6fde254468fdaf3b55572008e7105d8a884f881 Mon Sep 17 00:00:00 2001 From: yiling Date: Tue, 4 Aug 2026 17:01:47 +0800 Subject: [PATCH] fix(agent): activate bash tool after skill load and fix chat stream Two bugs found while testing skill loading as a non-owner: 1. The injected bash tool was never actually usable: AgentHarness.setTools only updates the tool registry unless activeToolNames is passed, so bash was missing from the active tool set sent to the model on the next turn. Pass the full post-update tool list as activeToolNames. 2. The active_tools_change session entry (written by setTools) has no chat representation, so mapEntryToMessage fell back to fabricating an empty user message that appeared as a stray empty bubble in the chat stream. Render active_tools_change as a typed custom message and skip entry types that have no chat representation instead of emitting empty text. --- packages/agent/src/index.test.ts | 4 ++++ packages/agent/src/index.ts | 8 ++++++- packages/core/src/chat/chat.test.ts | 33 +++++++++++++++++++++++++++++ packages/core/src/chat/chat.ts | 18 ++++++++++------ 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/packages/agent/src/index.test.ts b/packages/agent/src/index.test.ts index 365b3a6e..eb6634fd 100644 --- a/packages/agent/src/index.test.ts +++ b/packages/agent/src/index.test.ts @@ -766,10 +766,14 @@ describe('createAgentSession bash restriction for non-owner users', () => { const readSkillTool = harness.getTools().find((t) => t.name === 'read_skill') expect(readSkillTool).toBeDefined() expect(harness.getTools().some((t) => t.name === 'bash')).toBe(false) + expect(harness.getActiveTools().some((t) => t.name === 'bash')).toBe(false) await readSkillTool!.execute('1', { skillId: skill.id }, undefined, undefined, undefined) expect(harness.getTools().some((t) => t.name === 'bash')).toBe(true) + // bash must be part of the ACTIVE tool set, not just the registry, or the model + // would never receive it on the next turn + expect(harness.getActiveTools().some((t) => t.name === 'bash')).toBe(true) }) it('should not inject the bash tool for restricted users when bash is in deniedTools', async () => { diff --git a/packages/agent/src/index.ts b/packages/agent/src/index.ts index b5e40f4a..36c28db3 100644 --- a/packages/agent/src/index.ts +++ b/packages/agent/src/index.ts @@ -304,7 +304,13 @@ export async function createAgentSession(params: CreateAgentSessionParams) { if (!harness || bashInjected || deniedTools.includes('bash')) return if (harness.getTools().some((t) => t.name === 'bash')) return bashInjected = true - await harness.setTools([...harness.getTools(), sandboxedBash]) + // Pass the full post-update tool list as activeToolNames: setTools only updates the + // registry otherwise, and the model would never see the bash tool on the next turn. + const next = [...harness.getTools(), sandboxedBash] + await harness.setTools( + next, + next.map((t) => t.name), + ) } const readSkill = createReadSkillTool(userId, onEnvsAdded, restricted ? onSkillLoaded : undefined) diff --git a/packages/core/src/chat/chat.test.ts b/packages/core/src/chat/chat.test.ts index 7e56035d..1ca32072 100644 --- a/packages/core/src/chat/chat.test.ts +++ b/packages/core/src/chat/chat.test.ts @@ -525,6 +525,39 @@ describe('ChatService', () => { expect((msg3.details as Record).fromId).toBe('entry-old') }) + it('should render active_tools_change entries as custom messages', () => { + const pathEntries = [ + { + id: 'entry-tools', + type: 'active_tools_change', + timestamp: '2026-07-07T00:05:00.000Z', + activeToolNames: ['read_skill', 'bash'], + }, + ] + + const messages = buildSessionMessages(pathEntries as unknown as PathEntry[]) + expect(messages).toHaveLength(1) + // Casting to any to access properties of union in test assertions + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const msg = messages[0] as any + expect(msg.role).toBe('custom') + expect(msg.customType).toBe('active-tools-change') + expect(msg.content).toBe('Active tools: read_skill, bash') + expect(msg.details).toEqual({ activeToolNames: ['read_skill', 'bash'] }) + }) + + it('should return null for entry types without a chat representation instead of an empty message', () => { + const leafRecord = { + id: 'entry-leaf', + type: 'leaf', + parentId: null, + createdAt: new Date('2026-07-07T00:05:00.000Z'), + data: { targetId: 'some-entry' }, + } as unknown as Parameters[0] + + expect(mapEntryToMessage(leafRecord)).toBeNull() + }) + describe('getNewSessionMessages', () => { it('returns new messages and lastEntryId', async () => { const { user, team, project } = await setupBasicData() diff --git a/packages/core/src/chat/chat.ts b/packages/core/src/chat/chat.ts index 896673e7..2e13f7a1 100644 --- a/packages/core/src/chat/chat.ts +++ b/packages/core/src/chat/chat.ts @@ -64,6 +64,15 @@ export function buildSessionMessages(pathEntries: PathEntry[]): ChatMessage[] { content: `Thinking level changed to ${entry.thinkingLevel}`, timestamp: timestampMs, } as unknown as ChatMessage) + } else if (entry.type === 'active_tools_change') { + messages.push({ + id: entry.id, + role: 'custom', + customType: 'active-tools-change', + content: `Active tools: ${entry.activeToolNames.join(', ')}`, + details: { activeToolNames: entry.activeToolNames }, + timestamp: timestampMs, + } as unknown as ChatMessage) } } @@ -121,12 +130,9 @@ export function mapEntryToMessage( if (messages.length > 0) { return messages[0] } - return { - id: entryRecord.id, - role: 'user', - content: '', - timestamp: entryRecord.createdAt.getTime(), - } as unknown as ChatMessage + // Entry types that have no chat representation (e.g. leaf, label, model_change, + // or any unknown/unsupported type) are skipped rather than rendered as an empty message. + return null } export class ChatService {