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.
This commit is contained in:
yiling
2026-08-04 17:01:47 +08:00
parent dd2f8e68d9
commit e6fde25446
4 changed files with 56 additions and 7 deletions
+4
View File
@@ -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 () => {
+7 -1
View File
@@ -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)
+33
View File
@@ -525,6 +525,39 @@ describe('ChatService', () => {
expect((msg3.details as Record<string, unknown>).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<typeof mapEntryToMessage>[0]
expect(mapEntryToMessage(leafRecord)).toBeNull()
})
describe('getNewSessionMessages', () => {
it('returns new messages and lastEntryId', async () => {
const { user, team, project } = await setupBasicData()
+12 -6
View File
@@ -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 {