Skip to content

Commit 76856c3

Browse files
committed
Name a down capability in the system prompt, and keep its URL clean
A Bot could promise work for one of its own attached MCP servers or skills before learning that it cannot be reached. The host already knew: the capability status carries `error` or `needs-approval`. A second prompt section (order 700) now names each of those in one line, built from capabilityStatusesView(), and renders to nothing while every capability is ready, so the identity section and its cached prefix stay byte-stable. From the security read at 6b401eb: a URL such as https://alice:s3cr3t-pa55@host/mcp was accepted, the transport error quoted it verbatim, and that sentence is now printed every turn. The save refuses userinfo, and a stored error of a capability saved before the refusal has its userinfo masked before the 300-character cut.
1 parent 4a3ad73 commit 76856c3

14 files changed

Lines changed: 543 additions & 21 deletions

‎changelog.d/3273.fixed.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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)).

‎core/client.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,6 +1912,9 @@ var HOST_ZH = {
19121912
'Use a capability name with lowercase letters, digits, and single hyphens; "github" and "composio" are reserved.': "能力名只能用小写字母、数字和单个连字符;「github」和「composio」是保留名。",
19131913
"Enter a command for the MCP server.": "请填写 MCP 服务器的命令。",
19141914
"Enter a valid HTTP or HTTPS URL.": "请填写有效的 HTTP 或 HTTPS 地址。",
1915+
// #3273: a URL carrying userinfo is refused instead of stored, so the credential never reaches
1916+
// a status line and never reaches the system prompt.
1917+
"Enter the URL without a username or password. Save the credential as a secret and reference it in a header.": "地址里不要写用户名或密码。请把凭据保存为密钥,并在请求头里引用它。",
19151918
"A capability cannot change its kind. Delete it and save a new one.": "能力类型不能改。请删掉它,再新建一个。",
19161919
"Environment variables must contain text values.": "环境变量里只能填文本值。",
19171920
"Headers must contain text values.": "请求头里只能填文本值。"

‎core/src/client/i18n.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,6 +1872,9 @@ const HOST_ZH = {
18721872
'Use a capability name with lowercase letters, digits, and single hyphens; "github" and "composio" are reserved.': '能力名只能用小写字母、数字和单个连字符;「github」和「composio」是保留名。',
18731873
'Enter a command for the MCP server.': '请填写 MCP 服务器的命令。',
18741874
'Enter a valid HTTP or HTTPS URL.': '请填写有效的 HTTP 或 HTTPS 地址。',
1875+
// #3273: a URL carrying userinfo is refused instead of stored, so the credential never reaches
1876+
// a status line and never reaches the system prompt.
1877+
'Enter the URL without a username or password. Save the credential as a secret and reference it in a header.': '地址里不要写用户名或密码。请把凭据保存为密钥,并在请求头里引用它。',
18751878
'A capability cannot change its kind. Delete it and save a new one.': '能力类型不能改。请删掉它,再新建一个。',
18761879
'Environment variables must contain text values.': '环境变量里只能填文本值。',
18771880
'Headers must contain text values.': '请求头里只能填文本值。',

‎core/src/host/capabilities.js‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ export function expandCapabilityNames(capabilities, library) {
3636
return expanded
3737
}
3838

39+
// #3273: `redactSecrets` replaces saved key values, so a password written into a URL is a shape it
40+
// cannot know. A transport error quotes that URL back verbatim, and the status line reaches the
41+
// system prompt every turn, so userinfo is dropped before the sentence is stored.
42+
const withoutUserinfo = text => text.replace(/([a-z][a-z0-9+.-]*:\/\/)[^/\s@]+@/gi, '$1')
43+
3944
const same = (a, b) => JSON.stringify(a) === JSON.stringify(b)
4045
const strings = value => {
4146
if (value === undefined) return []
@@ -112,10 +117,10 @@ export function install(rt) {
112117
Object.assign(item, { transport, command, args, env, approved: ui ? true : changed ? false : previous.approved === true })
113118
} else {
114119
const url = String(body.url ?? '').trim()
115-
try {
116-
const parsed = new URL(url)
117-
if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:') throw new Error()
118-
} catch { throw new Error('Enter a valid HTTP or HTTPS URL.') }
120+
let parsed
121+
try { parsed = new URL(url) } catch { throw new Error('Enter a valid HTTP or HTTPS URL.') }
122+
if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:') throw new Error('Enter a valid HTTP or HTTPS URL.')
123+
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.')
119124
Object.assign(item, { transport, url, headers: stringMap(body.headers, 'Headers'), approved: true })
120125
}
121126
} else if (kind === 'skill') {
@@ -312,7 +317,8 @@ export function install(rt) {
312317
statusSet(bot.id, name, sessionId, { state: 'ready' })
313318
} catch (error) {
314319
await drop()
315-
const message = rt.redactSecrets([error, error?.cause].filter(Boolean).map(part => String(part?.message ?? part)).join(': ') || 'Connection failed').slice(0, 300)
320+
const joined = [error, error?.cause].filter(Boolean).map(part => String(part?.message ?? part)).join(': ') || 'Connection failed'
321+
const message = withoutUserinfo(rt.redactSecrets(joined)).slice(0, 300)
316322
statusSet(bot.id, name, sessionId, { state: 'error', error: message })
317323
}
318324
}

‎core/src/host/prompt.js‎

Lines changed: 34 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎core/test/capabilities.test.js‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,54 @@ test('a capability description is cut at 2000 without splitting an emoji', async
466466
assert.ok(description.startsWith(kept), 'a code-point prefix of the description the peer sent')
467467
assert.ok(kept.length <= 2000, 'the budget of 2000 UTF-16 units is unchanged')
468468
})
469+
470+
// #3273, from the security read at 6b401ebb: an inline credential in a URL is a shape
471+
// `redactSecrets` cannot know (it replaces saved key values), so the host refuses it on the way in
472+
// and masks userinfo on the way out — the string never reaches a status line, and therefore never
473+
// reaches the system prompt section that prints one.
474+
const USERINFO_REFUSAL = 'Enter the URL without a username or password. Save the credential as a secret and reference it in a header.'
475+
476+
test('a capability URL that carries a username or password is refused, and nothing is stored', async t => {
477+
const env = await setup()
478+
t.after(() => env.dispose())
479+
const { rt } = env
480+
for (const url of ['https://alice:s3cr3t-pa55@host/mcp', 'https://alice@host/mcp']) {
481+
await assert.rejects(
482+
rt.capabilitySave({ ...mcp('inline'), transport: 'streamable-http', url }, { ui: true }),
483+
error => error.message === USERINFO_REFUSAL,
484+
)
485+
}
486+
assert.equal(rt.state.capabilities.items.inline, undefined, 'a refused save leaves no item')
487+
assert.equal(JSON.stringify(rt.state).includes('s3cr3t-pa55'), false)
488+
})
489+
490+
test('an error that quotes a URL saved before this fix stores it with the userinfo masked', async t => {
491+
const state = team()
492+
state.capabilities = {
493+
items: {
494+
inline: {
495+
name: 'inline', kind: 'mcp', description: 'Saved before the refusal', transport: 'streamable-http',
496+
url: 'https://alice:s3cr3t-pa55@host/mcp', headers: {}, approved: true, createdBy: 'worker', createdAt: 1, updatedAt: 1,
497+
},
498+
},
499+
}
500+
const env = await setup({ state })
501+
t.after(() => env.dispose())
502+
const { ctx, rt, mounts, skills } = env
503+
await rt.changeCapabilityAttachment('attach', { name: 'inline' }, 'worker')
504+
const quoted = 'Request cannot be constructed from a URL that includes credentials: https://alice:s3cr3t-pa55@host/mcp'
505+
ctx.emit('agent/created', { agent: testAgent('worker', mounts, skills, { awaitMount: async () => { throw new TypeError(quoted) } }) })
506+
await rt.scheduleCapabilitiesReconcile()
507+
const status = rt.capabilityStatusesView().worker.inline
508+
assert.equal(status.state, 'error')
509+
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')
510+
assert.equal(JSON.stringify(rt.capabilityStatusesView()).includes('s3cr3t-pa55'), false)
511+
})
512+
513+
test('a plain HTTPS URL still saves', async t => {
514+
const env = await setup()
515+
t.after(() => env.dispose())
516+
const saved = await env.rt.capabilitySave({ ...mcp('plain'), transport: 'streamable-http', url: 'https://host/mcp' }, { ui: true })
517+
assert.equal(saved.url, 'https://host/mcp')
518+
assert.equal(env.rt.state.capabilities.items.plain.url, 'https://host/mcp')
519+
})

‎core/test/install.test.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ test('apply registers listeners, tools, and effects in a fixed order', (t) => {
5151
'on agent/created',
5252
'effect ds-bot: identity section',
5353
'prompt bot-identity 640',
54+
'effect ds-bot: capability section',
55+
'prompt bot-capabilities 700',
5456
'on agent/pre-step (prepend)',
5557
'tool list_bots',
5658
'tool message_bot',

‎core/test/prompt.test.js‎

Lines changed: 126 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,31 @@ import { test } from 'node:test'
33
import { install } from '../src/host/prompt.js'
44
import { fakeContext } from './fake-ctx.js'
55

6-
function start() {
6+
function start({ statuses = {}, library = [] } = {}) {
77
const ctx = fakeContext()
88
const bots = new Map([
9-
['main', { name: 'Chief' }],
10-
['worker', { name: 'Dev', instructions: 'Check files.' }],
9+
['main', { id: 'main', name: 'Chief' }],
10+
['worker', { id: 'worker', name: 'Dev', instructions: 'Check files.' }],
1111
])
1212
install({
1313
ctx,
1414
selfOf: id => id === 'group-worker' ? 'worker' : id,
1515
botOf: id => bots.get(id),
1616
roomOf: id => id === 'room' ? {} : undefined,
1717
isMain: id => id === 'main',
18+
capabilityStatusesView: () => statuses,
19+
capabilityLibraryView: () => library,
1820
})
1921
const prompt = id => ctx.sections.get('bot-identity').text({ agent: { id } })
20-
return { ctx, prompt }
22+
// The host's own join: sections in order, empty ones dropped, one blank line between
23+
// (dsh-system-prompt renderSections). Rendering here keeps the byte-level assertion real.
24+
const render = id => [...ctx.sections.values()]
25+
.sort((a, b) => a.order - b.order)
26+
.map(section => section.text({ agent: { id } }))
27+
.filter(text => text.length > 0)
28+
.join('\n\n')
29+
const capabilities = id => ctx.sections.get('bot-capabilities')?.text({ agent: { id } })
30+
return { ctx, prompt, render, capabilities }
2131
}
2232

2333
test('Bots get a scoped progress rule without changing the private/group prompt prefix', (t) => {
@@ -80,3 +90,115 @@ test('a Bot stays idle until it is addressed or given a task (part B of #645)',
8090
const replyRule = '- When a "[Reply from …]" arrives, tell the user the outcome if it matters to them.'
8191
for (const id of ['main', 'worker']) assert.ok(prompt(id).split('\n').includes(replyRule), `${id} still answers a reply`)
8292
})
93+
94+
// #3273: a Bot must not promise work for one of its own capabilities that cannot be reached.
95+
// The identity section stays byte-stable (the prompt prefix is cached), so the state lives in a
96+
// second section that renders to nothing whenever every attached capability is usable.
97+
test('#3273: a capability whose connection failed is named before the Bot acts', (t) => {
98+
const { ctx, render, prompt, capabilities } = start({
99+
statuses: { worker: { github: { state: 'error', error: '401 bad credentials' } } },
100+
library: [{ name: 'github', kind: 'mcp' }],
101+
})
102+
t.after(() => ctx.dispose())
103+
const text = render('worker')
104+
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')
105+
assert.equal(capabilities('worker').split('# Capabilities that need attention').length - 1, 1, 'one title, once')
106+
assert.ok(
107+
text.split('\n').includes('- github (mcp): 401 bad credentials — tell the user before acting.'),
108+
`the failed capability and its reason are on one line:\n${text}`,
109+
)
110+
assert.equal(text.slice(0, prompt('worker').length), prompt('worker'), 'the identity section is untouched')
111+
assert.doesNotMatch(prompt('worker'), /401 bad credentials/, 'the identity section stays byte-stable')
112+
})
113+
114+
test('#3273: a capability waiting for the user is named the same way', (t) => {
115+
const { ctx, render } = start({
116+
statuses: { worker: { github: { state: 'needs-approval' } } },
117+
library: [{ name: 'github', kind: 'mcp' }],
118+
})
119+
t.after(() => ctx.dispose())
120+
const text = render('worker')
121+
assert.ok(
122+
text.split('\n').includes('- github (mcp): needs user approval — tell the user before acting.'),
123+
`the unapproved capability asks the Bot to say so first:\n${text}`,
124+
)
125+
})
126+
127+
test('#3273: only the capabilities that are not usable are listed, ready ones are silent', (t) => {
128+
const { ctx, capabilities } = start({
129+
statuses: {
130+
worker: {
131+
github: { state: 'error', error: '401 bad credentials' },
132+
linear: { state: 'ready' },
133+
slack: { state: 'needs-approval' },
134+
},
135+
},
136+
library: [{ name: 'github', kind: 'mcp' }, { name: 'linear', kind: 'mcp' }, { name: 'slack', kind: 'pack' }],
137+
})
138+
t.after(() => ctx.dispose())
139+
const listed = capabilities('worker').split('\n').filter(line => line.startsWith('- '))
140+
assert.deepEqual(listed, [
141+
'- github (mcp): 401 bad credentials — tell the user before acting.',
142+
'- slack (pack): needs user approval — tell the user before acting.',
143+
])
144+
})
145+
146+
// Test found by mutation that a healthy render compared only against another render of the same
147+
// code proves nothing: a trailing space added to the identity text passes both sides. The claim is
148+
// that the healthy prompt *is* the identity text, so its exact head and its exact last line are
149+
// pinned here as literals, and the capability section has to contribute the empty string.
150+
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.'
151+
const IDENTITY_HEADS = {
152+
main: "# Your identity\nYou are Chief, a Bot on the user's team in FlyBot.",
153+
worker: "# Your identity\nYou are Dev, a Bot on the user's team in FlyBot.",
154+
}
155+
156+
test('#3273: a healthy Bot renders exactly the prompt it renders today', (t) => {
157+
const healthy = start({ statuses: { main: {}, worker: { github: { state: 'ready' } } }, library: [{ name: 'github', kind: 'mcp' }] })
158+
const none = start()
159+
t.after(() => healthy.ctx.dispose())
160+
t.after(() => none.ctx.dispose())
161+
assert.deepEqual([...healthy.ctx.sections.keys()].sort(), ['bot-capabilities', 'bot-identity'])
162+
assert.equal(healthy.ctx.sections.get('bot-identity').order, 640)
163+
assert.equal(healthy.ctx.sections.get('bot-capabilities').order, 700)
164+
for (const id of ['main', 'worker']) {
165+
assert.equal(healthy.capabilities(id), '', 'nothing unhealthy renders no section text at all')
166+
const text = healthy.render(id)
167+
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`)
168+
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`)
169+
assert.equal(text.trimEnd(), text, `${id}: no trailing whitespace`)
170+
assert.equal(healthy.render(id), none.render(id), 'byte for byte the prompt a Bot with no capabilities gets')
171+
assert.equal(none.render(id), none.prompt(id), 'the rendered prompt is the identity section alone — no extra section, no blank line')
172+
}
173+
// A relay session is not a Bot: it never carries capability state.
174+
assert.equal(healthy.render('room'), healthy.prompt('room'))
175+
})
176+
177+
test('#3273: one Bot never sees another Bot\'s capability state, and a failure is whole-Bot', (t) => {
178+
const { ctx, capabilities, render } = start({
179+
statuses: {
180+
main: { github: { state: 'ready' } },
181+
worker: { github: { state: 'error', error: '401 bad credentials' } },
182+
},
183+
library: [{ name: 'github', kind: 'mcp' }],
184+
})
185+
t.after(() => ctx.dispose())
186+
assert.equal(capabilities('main'), '', 'the Chief sees no line for a Worker capability that is down')
187+
assert.equal(render('main').includes('401 bad credentials'), false)
188+
assert.ok(capabilities('worker').includes('- github (mcp): 401 bad credentials'), 'the Bot whose capability is down sees it')
189+
// The status view aggregates per Bot across its Sessions, the same way list_capabilities does:
190+
// a failure reached in one Session is the whole Bot's failure, so the group Session reads it too.
191+
assert.equal(render('group-worker'), render('worker'))
192+
})
193+
194+
test('#3273: an error with no reason still reads as a sentence', (t) => {
195+
const { ctx, capabilities } = start({
196+
statuses: { worker: { github: { state: 'error', error: ' ' }, linear: { state: 'error', error: '' } } },
197+
library: [{ name: 'github', kind: 'mcp' }, { name: 'linear', kind: 'mcp' }],
198+
})
199+
t.after(() => ctx.dispose())
200+
assert.deepEqual(capabilities('worker').split('\n').filter(line => line.startsWith('- ')), [
201+
'- github (mcp): not connected — tell the user before acting.',
202+
'- linear (mcp): not connected — tell the user before acting.',
203+
], 'an empty or whitespace-only reason becomes "not connected", never a dangling dash')
204+
})

0 commit comments

Comments
 (0)