Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/3273.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A Bot now learns from its system prompt that one of its own capabilities cannot be used right now, naming the capability and the reason, so it says "GitHub isn't connected (401 bad credentials)" before acting instead of agreeing to the work and failing half a turn later ([#3273](https://github.com/ThinkFlowLab/FlyBot/issues/3273)). Saving a capability URL that carries a username or password is refused, and an older error that quotes such a URL is stored with the userinfo masked, so a credential can never be printed into the Bot's system prompt ([#3273](https://github.com/ThinkFlowLab/FlyBot/issues/3273)).
3 changes: 3 additions & 0 deletions core/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1912,6 +1912,9 @@ var HOST_ZH = {
'Use a capability name with lowercase letters, digits, and single hyphens; "github" and "composio" are reserved.': "能力名只能用小写字母、数字和单个连字符;「github」和「composio」是保留名。",
"Enter a command for the MCP server.": "请填写 MCP 服务器的命令。",
"Enter a valid HTTP or HTTPS URL.": "请填写有效的 HTTP 或 HTTPS 地址。",
// #3273: a URL carrying userinfo is refused instead of stored, so the credential never reaches
// a status line and never reaches the system prompt.
"Enter the URL without a username or password. Save the credential as a secret and reference it in a header.": "地址里不要写用户名或密码。请把凭据保存为密钥,并在请求头里引用它。",
"A capability cannot change its kind. Delete it and save a new one.": "能力类型不能改。请删掉它,再新建一个。",
"Environment variables must contain text values.": "环境变量里只能填文本值。",
"Headers must contain text values.": "请求头里只能填文本值。"
Expand Down
3 changes: 3 additions & 0 deletions core/src/client/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -1872,6 +1872,9 @@ const HOST_ZH = {
'Use a capability name with lowercase letters, digits, and single hyphens; "github" and "composio" are reserved.': '能力名只能用小写字母、数字和单个连字符;「github」和「composio」是保留名。',
'Enter a command for the MCP server.': '请填写 MCP 服务器的命令。',
'Enter a valid HTTP or HTTPS URL.': '请填写有效的 HTTP 或 HTTPS 地址。',
// #3273: a URL carrying userinfo is refused instead of stored, so the credential never reaches
// a status line and never reaches the system prompt.
'Enter the URL without a username or password. Save the credential as a secret and reference it in a header.': '地址里不要写用户名或密码。请把凭据保存为密钥,并在请求头里引用它。',
'A capability cannot change its kind. Delete it and save a new one.': '能力类型不能改。请删掉它,再新建一个。',
'Environment variables must contain text values.': '环境变量里只能填文本值。',
'Headers must contain text values.': '请求头里只能填文本值。',
Expand Down
16 changes: 11 additions & 5 deletions core/src/host/capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export function expandCapabilityNames(capabilities, library) {
return expanded
}

// #3273: `redactSecrets` replaces saved key values, so a password written into a URL is a shape it
// cannot know. A transport error quotes that URL back verbatim, and the status line reaches the
// system prompt every turn, so userinfo is dropped before the sentence is stored.
const withoutUserinfo = text => text.replace(/([a-z][a-z0-9+.-]*:\/\/)[^/\s@]+@/gi, '$1')

const same = (a, b) => JSON.stringify(a) === JSON.stringify(b)
const strings = value => {
if (value === undefined) return []
Expand Down Expand Up @@ -112,10 +117,10 @@ export function install(rt) {
Object.assign(item, { transport, command, args, env, approved: ui ? true : changed ? false : previous.approved === true })
} else {
const url = String(body.url ?? '').trim()
try {
const parsed = new URL(url)
if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:') throw new Error()
} catch { throw new Error('Enter a valid HTTP or HTTPS URL.') }
let parsed
try { parsed = new URL(url) } catch { throw new Error('Enter a valid HTTP or HTTPS URL.') }
if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:') throw new Error('Enter a valid HTTP or HTTPS URL.')
if (parsed.username !== '' || parsed.password !== '') throw new Error('Enter the URL without a username or password. Save the credential as a secret and reference it in a header.')
Object.assign(item, { transport, url, headers: stringMap(body.headers, 'Headers'), approved: true })
}
} else if (kind === 'skill') {
Expand Down Expand Up @@ -312,7 +317,8 @@ export function install(rt) {
statusSet(bot.id, name, sessionId, { state: 'ready' })
} catch (error) {
await drop()
const message = rt.redactSecrets([error, error?.cause].filter(Boolean).map(part => String(part?.message ?? part)).join(': ') || 'Connection failed').slice(0, 300)
const joined = [error, error?.cause].filter(Boolean).map(part => String(part?.message ?? part)).join(': ') || 'Connection failed'
const message = withoutUserinfo(rt.redactSecrets(joined)).slice(0, 300)
statusSet(bot.id, name, sessionId, { state: 'error', error: message })
}
}
Expand Down
36 changes: 34 additions & 2 deletions core/src/host/prompt.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions core/test/capabilities.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,54 @@ test('a capability description is cut at 2000 without splitting an emoji', async
assert.ok(description.startsWith(kept), 'a code-point prefix of the description the peer sent')
assert.ok(kept.length <= 2000, 'the budget of 2000 UTF-16 units is unchanged')
})

// #3273, from the security read at 6b401ebb: an inline credential in a URL is a shape
// `redactSecrets` cannot know (it replaces saved key values), so the host refuses it on the way in
// and masks userinfo on the way out — the string never reaches a status line, and therefore never
// reaches the system prompt section that prints one.
const USERINFO_REFUSAL = 'Enter the URL without a username or password. Save the credential as a secret and reference it in a header.'

test('a capability URL that carries a username or password is refused, and nothing is stored', async t => {
const env = await setup()
t.after(() => env.dispose())
const { rt } = env
for (const url of ['https://alice:s3cr3t-pa55@host/mcp', 'https://alice@host/mcp']) {
await assert.rejects(
rt.capabilitySave({ ...mcp('inline'), transport: 'streamable-http', url }, { ui: true }),
error => error.message === USERINFO_REFUSAL,
)
}
assert.equal(rt.state.capabilities.items.inline, undefined, 'a refused save leaves no item')
assert.equal(JSON.stringify(rt.state).includes('s3cr3t-pa55'), false)
})

test('an error that quotes a URL saved before this fix stores it with the userinfo masked', async t => {
const state = team()
state.capabilities = {
items: {
inline: {
name: 'inline', kind: 'mcp', description: 'Saved before the refusal', transport: 'streamable-http',
url: 'https://alice:s3cr3t-pa55@host/mcp', headers: {}, approved: true, createdBy: 'worker', createdAt: 1, updatedAt: 1,
},
},
}
const env = await setup({ state })
t.after(() => env.dispose())
const { ctx, rt, mounts, skills } = env
await rt.changeCapabilityAttachment('attach', { name: 'inline' }, 'worker')
const quoted = 'Request cannot be constructed from a URL that includes credentials: https://alice:s3cr3t-pa55@host/mcp'
ctx.emit('agent/created', { agent: testAgent('worker', mounts, skills, { awaitMount: async () => { throw new TypeError(quoted) } }) })
await rt.scheduleCapabilitiesReconcile()
const status = rt.capabilityStatusesView().worker.inline
assert.equal(status.state, 'error')
assert.equal(status.error, 'Request cannot be constructed from a URL that includes credentials: https://host/mcp', 'the exact stored sentence: the scheme and host stay, the userinfo is gone')
assert.equal(JSON.stringify(rt.capabilityStatusesView()).includes('s3cr3t-pa55'), false)
})

test('a plain HTTPS URL still saves', async t => {
const env = await setup()
t.after(() => env.dispose())
const saved = await env.rt.capabilitySave({ ...mcp('plain'), transport: 'streamable-http', url: 'https://host/mcp' }, { ui: true })
assert.equal(saved.url, 'https://host/mcp')
assert.equal(env.rt.state.capabilities.items.plain.url, 'https://host/mcp')
})
2 changes: 2 additions & 0 deletions core/test/install.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ test('apply registers listeners, tools, and effects in a fixed order', (t) => {
'on agent/created',
'effect ds-bot: identity section',
'prompt bot-identity 640',
'effect ds-bot: capability section',
'prompt bot-capabilities 700',
'on agent/pre-step (prepend)',
'tool list_bots',
'tool message_bot',
Expand Down
130 changes: 126 additions & 4 deletions core/test/prompt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,31 @@ import { test } from 'node:test'
import { install } from '../src/host/prompt.js'
import { fakeContext } from './fake-ctx.js'

function start() {
function start({ statuses = {}, library = [] } = {}) {
const ctx = fakeContext()
const bots = new Map([
['main', { name: 'Chief' }],
['worker', { name: 'Dev', instructions: 'Check files.' }],
['main', { id: 'main', name: 'Chief' }],
['worker', { id: 'worker', name: 'Dev', instructions: 'Check files.' }],
])
install({
ctx,
selfOf: id => id === 'group-worker' ? 'worker' : id,
botOf: id => bots.get(id),
roomOf: id => id === 'room' ? {} : undefined,
isMain: id => id === 'main',
capabilityStatusesView: () => statuses,
capabilityLibraryView: () => library,
})
const prompt = id => ctx.sections.get('bot-identity').text({ agent: { id } })
return { ctx, prompt }
// The host's own join: sections in order, empty ones dropped, one blank line between
// (dsh-system-prompt renderSections). Rendering here keeps the byte-level assertion real.
const render = id => [...ctx.sections.values()]
.sort((a, b) => a.order - b.order)
.map(section => section.text({ agent: { id } }))
.filter(text => text.length > 0)
.join('\n\n')
const capabilities = id => ctx.sections.get('bot-capabilities')?.text({ agent: { id } })
return { ctx, prompt, render, capabilities }
}

test('Bots get a scoped progress rule without changing the private/group prompt prefix', (t) => {
Expand Down Expand Up @@ -80,3 +90,115 @@ test('a Bot stays idle until it is addressed or given a task (part B of #645)',
const replyRule = '- When a "[Reply from …]" arrives, tell the user the outcome if it matters to them.'
for (const id of ['main', 'worker']) assert.ok(prompt(id).split('\n').includes(replyRule), `${id} still answers a reply`)
})

// #3273: a Bot must not promise work for one of its own capabilities that cannot be reached.
// The identity section stays byte-stable (the prompt prefix is cached), so the state lives in a
// second section that renders to nothing whenever every attached capability is usable.
test('#3273: a capability whose connection failed is named before the Bot acts', (t) => {
const { ctx, render, prompt, capabilities } = start({
statuses: { worker: { github: { state: 'error', error: '401 bad credentials' } } },
library: [{ name: 'github', kind: 'mcp' }],
})
t.after(() => ctx.dispose())
const text = render('worker')
assert.equal(capabilities('worker').split('\n')[0], '# Capabilities that need attention', 'the section carries its own title, so the list is not read as a continuation of the identity block')
assert.equal(capabilities('worker').split('# Capabilities that need attention').length - 1, 1, 'one title, once')
assert.ok(
text.split('\n').includes('- github (mcp): 401 bad credentials — tell the user before acting.'),
`the failed capability and its reason are on one line:\n${text}`,
)
assert.equal(text.slice(0, prompt('worker').length), prompt('worker'), 'the identity section is untouched')
assert.doesNotMatch(prompt('worker'), /401 bad credentials/, 'the identity section stays byte-stable')
})

test('#3273: a capability waiting for the user is named the same way', (t) => {
const { ctx, render } = start({
statuses: { worker: { github: { state: 'needs-approval' } } },
library: [{ name: 'github', kind: 'mcp' }],
})
t.after(() => ctx.dispose())
const text = render('worker')
assert.ok(
text.split('\n').includes('- github (mcp): needs user approval — tell the user before acting.'),
`the unapproved capability asks the Bot to say so first:\n${text}`,
)
})

test('#3273: only the capabilities that are not usable are listed, ready ones are silent', (t) => {
const { ctx, capabilities } = start({
statuses: {
worker: {
github: { state: 'error', error: '401 bad credentials' },
linear: { state: 'ready' },
slack: { state: 'needs-approval' },
},
},
library: [{ name: 'github', kind: 'mcp' }, { name: 'linear', kind: 'mcp' }, { name: 'slack', kind: 'pack' }],
})
t.after(() => ctx.dispose())
const listed = capabilities('worker').split('\n').filter(line => line.startsWith('- '))
assert.deepEqual(listed, [
'- github (mcp): 401 bad credentials — tell the user before acting.',
'- slack (pack): needs user approval — tell the user before acting.',
])
})

// Test found by mutation that a healthy render compared only against another render of the same
// code proves nothing: a trailing space added to the identity text passes both sides. The claim is
// that the healthy prompt *is* the identity text, so its exact head and its exact last line are
// pinned here as literals, and the capability section has to contribute the empty string.
const IDENTITY_LAST_RULE = '- When you cannot continue without an answer, pass ask with the one question on that task. On a group card, worked in your own chat, call ask_user for the user, and post one short question in the group only when a person there has to decide. On your own chat\'s other tasks, ask_user. When the answer arrives, pass answer with what they said and update the task. Do not leave the task waiting without that question.'
const IDENTITY_HEADS = {
main: "# Your identity\nYou are Chief, a Bot on the user's team in FlyBot.",
worker: "# Your identity\nYou are Dev, a Bot on the user's team in FlyBot.",
}

test('#3273: a healthy Bot renders exactly the prompt it renders today', (t) => {
const healthy = start({ statuses: { main: {}, worker: { github: { state: 'ready' } } }, library: [{ name: 'github', kind: 'mcp' }] })
const none = start()
t.after(() => healthy.ctx.dispose())
t.after(() => none.ctx.dispose())
assert.deepEqual([...healthy.ctx.sections.keys()].sort(), ['bot-capabilities', 'bot-identity'])
assert.equal(healthy.ctx.sections.get('bot-identity').order, 640)
assert.equal(healthy.ctx.sections.get('bot-capabilities').order, 700)
for (const id of ['main', 'worker']) {
assert.equal(healthy.capabilities(id), '', 'nothing unhealthy renders no section text at all')
const text = healthy.render(id)
assert.ok(text.startsWith(IDENTITY_HEADS[id]), `${id}: the rendered prompt starts on the identity title and the Bot's own line, byte for byte`)
assert.ok(text.endsWith(IDENTITY_LAST_RULE), `${id}: the rendered prompt ends on the last identity rule — no separator, no blank line, no trailing space`)
assert.equal(text.trimEnd(), text, `${id}: no trailing whitespace`)
assert.equal(healthy.render(id), none.render(id), 'byte for byte the prompt a Bot with no capabilities gets')
assert.equal(none.render(id), none.prompt(id), 'the rendered prompt is the identity section alone — no extra section, no blank line')
}
// A relay session is not a Bot: it never carries capability state.
assert.equal(healthy.render('room'), healthy.prompt('room'))
})

test('#3273: one Bot never sees another Bot\'s capability state, and a failure is whole-Bot', (t) => {
const { ctx, capabilities, render } = start({
statuses: {
main: { github: { state: 'ready' } },
worker: { github: { state: 'error', error: '401 bad credentials' } },
},
library: [{ name: 'github', kind: 'mcp' }],
})
t.after(() => ctx.dispose())
assert.equal(capabilities('main'), '', 'the Chief sees no line for a Worker capability that is down')
assert.equal(render('main').includes('401 bad credentials'), false)
assert.ok(capabilities('worker').includes('- github (mcp): 401 bad credentials'), 'the Bot whose capability is down sees it')
// The status view aggregates per Bot across its Sessions, the same way list_capabilities does:
// a failure reached in one Session is the whole Bot's failure, so the group Session reads it too.
assert.equal(render('group-worker'), render('worker'))
})

test('#3273: an error with no reason still reads as a sentence', (t) => {
const { ctx, capabilities } = start({
statuses: { worker: { github: { state: 'error', error: ' ' }, linear: { state: 'error', error: '' } } },
library: [{ name: 'github', kind: 'mcp' }, { name: 'linear', kind: 'mcp' }],
})
t.after(() => ctx.dispose())
assert.deepEqual(capabilities('worker').split('\n').filter(line => line.startsWith('- ')), [
'- github (mcp): not connected — tell the user before acting.',
'- linear (mcp): not connected — tell the user before acting.',
], 'an empty or whitespace-only reason becomes "not connected", never a dangling dash')
})
Loading
Loading