Skip to content

A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.

Persist refreshed OAuth clients and contain sign-in listener errors

Fei24119d0ce031cb7d
diff --git a/ds-bot/src/host/connectors/index.js b/ds-bot/src/host/connectors/index.js
index 068909ce..df8ea36d 100644
--- a/ds-bot/src/host/connectors/index.js
+++ b/ds-bot/src/host/connectors/index.js
@@ -270,11 +270,17 @@ export function install(rt) {
// The loopback server for the browser flow: fixed port first, else a random one.
async function loopback(ready) {
const listener = createServer((req, res) => {
- const url = new URL(req.url ?? '/', 'http://127.0.0.1')
+ req.on('error', () => res.destroy())
+ res.on('error', () => req.destroy())
+ let url
+ try { url = new URL(req.url ?? '/', 'http://127.0.0.1') } catch { res.writeHead(400); res.end(); return }
if (url.pathname !== '/callback') { res.writeHead(404); res.end(); return }
res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' })
res.end('<!doctype html><title>Signed in</title><p style="font-family:sans-serif;margin:3em;text-align:center">Signed in — you can close this tab and go back to DS Bot.</p>')
- void ready({ code: url.searchParams.get('code'), state: url.searchParams.get('state'), iss: url.searchParams.get('iss'), error: url.searchParams.get('error'), errorDescription: url.searchParams.get('error_description') })
+ void ready({ code: url.searchParams.get('code'), state: url.searchParams.get('state'), iss: url.searchParams.get('iss'), error: url.searchParams.get('error'), errorDescription: url.searchParams.get('error_description') }).catch(error => rt.warn('ds-bot: connector sign-in callback failed: %s', reason(error)))
+ })
+ listener.on('error', error => {
+ if (listener.listening) rt.warn('ds-bot: connector sign-in listener failed: %s', error?.code ?? 'error')
})
const listen = port => new Promise((resolve, reject) => {
const onError = error => { listener.off('listening', onOk); reject(error) }
@@ -410,7 +416,12 @@ export function install(rt) {
return false
}
const refresh = rt.secretValue(refreshSecretOf(id))
- if (refresh === undefined || saved?.oauth === undefined) { statuses.set(id, { state: 'needs-sign-in' }); return false }
+ let path = saved?.client?.via ?? 'inferred'
+ if (refresh === undefined || saved?.oauth === undefined) {
+ statuses.set(id, { state: 'needs-sign-in' })
+ rt.warn('ds-bot: connector %s token refresh failed (client: %s; missing refresh token or OAuth metadata)', id, path)
+ return false
+ }
try {
const discovery = await oauth.rediscover(saved.oauth)
// Refresh with the client that signed in: catalog → the current catalog id,
@@ -421,7 +432,13 @@ export function install(rt) {
const clientId = via === 'catalog' ? entryFor(id)?.auth?.client?.id
: via === 'cimd' ? oauth.CIMD_CLIENT_ID
: saved.client?.clientId ?? (discovery.metadata?.client_id_metadata_document_supported === true ? oauth.CIMD_CLIENT_ID : undefined)
- if (clientId === undefined) { statuses.set(id, { state: 'needs-sign-in' }); return false }
+ const clientVia = via ?? (saved.client?.clientId ? 'manual' : 'cimd')
+ if (via === undefined && clientId !== undefined) path = `inferred/${clientVia}`
+ if (clientId === undefined) {
+ statuses.set(id, { state: 'needs-sign-in' })
+ rt.warn('ds-bot: connector %s token refresh failed (client: %s; no OAuth client)', id, path)
+ return false
+ }
const secret = rt.secretValue(clientSecretOf(id))
const client = { client_id: clientId, ...(secret !== undefined ? { client_secret: secret } : {}) }
const tokens = await oauth.refreshTokens(discovery, client, refresh, { scope: saved.oauth.scope })
@@ -431,12 +448,17 @@ export function install(rt) {
// Rotation: the old refresh token stays if this write fails.
await writeSecret(refreshSecretOf(id), tokens.refresh_token, purpose).catch(() => {})
}
- await saveState(id, { tokenExpiresAt: Number.isFinite(tokens.expires_in) ? Date.now() + tokens.expires_in * 1000 : undefined })
+ await saveState(id, {
+ client: { ...saved.client, via: clientVia, clientId },
+ tokenExpiresAt: Number.isFinite(tokens.expires_in) ? Date.now() + tokens.expires_in * 1000 : undefined,
+ })
statuses.delete(id)
+ rt.warn('ds-bot: connector %s token refresh succeeded (client: %s)', id, path)
return true
} catch (error) {
if (oauth.invalidGrant(error)) statuses.set(id, { state: 'needs-sign-in', error: 'Sign in again' })
else statuses.set(id, { state: 'error', error: reason(error) })
+ rt.warn('ds-bot: connector %s token refresh failed (client: %s; %s)', id, path, oauth.invalidGrant(error) ? 'invalid_grant' : 'refresh error')
return false
}
})().finally(() => refreshes.delete(id))
diff --git a/ds-bot/src/host/connectors/mail/index.js b/ds-bot/src/host/connectors/mail/index.js
index 62ff41fd..cb193808 100644
--- a/ds-bot/src/host/connectors/mail/index.js
+++ b/ds-bot/src/host/connectors/mail/index.js
@@ -90,9 +90,11 @@ export function install(rt, host) {
if (typeof tokens.refresh_token === 'string' && tokens.refresh_token !== '') {
await host.secrets.writeSecret(mailSecretName(account.address, 'REFRESH'), tokens.refresh_token, `Mailbox ${account.address}`).catch(() => {})
}
+ rt.warn('ds-bot: connector mail token refresh succeeded (client: manual/Google)')
return true
} catch (error) {
if (invalidGrant(error)) account.error = 'Sign in again'
+ rt.warn('ds-bot: connector mail token refresh failed (client: manual/Google; %s)', invalidGrant(error) ? 'invalid_grant' : 'refresh error')
return false
}
}
@@ -408,10 +410,16 @@ export function install(rt, host) {
googlePendings.set(address, pending)
const { createServer } = await import('node:http')
const listener = createServer((req, res) => {
- const url = new URL(req.url ?? '/', 'http://127.0.0.1')
+ req.on('error', () => res.destroy())
+ res.on('error', () => req.destroy())
+ let url
+ try { url = new URL(req.url ?? '/', 'http://127.0.0.1') } catch { res.writeHead(400); res.end(); return }
res.writeHead(200, { 'content-type': 'text/html' })
res.end('<p style="font-family:sans-serif">Signed in — you can close this tab.</p>')
- void googleCode({ address, input: url.toString() })
+ void googleCode({ address, input: url.toString() }).catch(error => rt.warn('ds-bot: mail Google sign-in failed: %s', host.reason(error)))
+ })
+ listener.on('error', error => {
+ if (listener.listening) rt.warn('ds-bot: mail Google sign-in listener failed: %s', error?.code ?? 'error')
})
await new Promise((resolve, reject) => { listener.once('error', reject); listener.listen(0, '127.0.0.1', resolve) })
pending.listener = listener
diff --git a/ds-bot/test/connectors-engine.test.js b/ds-bot/test/connectors-engine.test.js
index 463065b0..f5863de4 100644
--- a/ds-bot/test/connectors-engine.test.js
+++ b/ds-bot/test/connectors-engine.test.js
@@ -296,11 +296,16 @@ test('refresh is single-flight, rotates the refresh token, and invalid_grant mea
assert.equal(b, true)
assert.equal(grants, 1)
assert.equal(rt.secretValue('CONNECTOR_NOTION_REFRESH'), 'RT-2')
+ let logs = await rt.diagnostics.entries()
+ assert.equal(logs.filter(row => /connector notion token refresh succeeded.*dcr/.test(row.text)).length, 1)
// invalid_grant on the next refresh: needs-sign-in, old secrets kept.
globalThis.fetch = async (url, init) => String(url?.url ?? url) === 'https://as.test/token' ? json({ error: 'invalid_grant' }, 400) : request(url, init)
assert.equal(await rt.connectorsInternals.refreshConnector('notion'), false)
assert.equal(rt.connectorsInternals.statuses.get('notion').state, 'needs-sign-in', JSON.stringify(rt.connectorsInternals.statuses.get('notion')))
assert.equal(rt.secretValue('CONNECTOR_NOTION_REFRESH'), 'RT-2')
+ logs = await rt.diagnostics.entries()
+ assert.ok(logs.some(row => /connector notion token refresh failed.*dcr/.test(row.text)))
+ assert.equal(JSON.stringify(logs).includes('RT-2'), false)
})
test('refresh uses the client that signed in: cimd, catalog, and legacy inference', async (t) => {
@@ -334,6 +339,7 @@ test('refresh uses the client that signed in: cimd, catalog, and legacy inferenc
t.after(done)
assert.equal(await rt.connectorsInternals.refreshConnector('notion'), true, JSON.stringify(rt.connectorsInternals.statuses.get('notion')))
assert.equal(seen.at(-1), 'https://dsbot.app/oauth/client.json')
+ assert.equal(rt.state.connectors.notion.client.via, 'cimd')
}
// A catalog sign-in refreshes with the current catalog client id.
{
@@ -344,6 +350,16 @@ test('refresh uses the client that signed in: cimd, catalog, and legacy inferenc
assert.equal(await rt.connectorsInternals.refreshConnector('github'), true, JSON.stringify(rt.connectorsInternals.statuses.get('github')))
assert.equal(seen.at(-1), 'test-gh-client')
}
+ {
+ const { rt, done } = await make(
+ { notion: { oauth: { authorizationServerUrl: 'https://as.test' }, client: { via: 'manual', clientId: 'manual-client' } } },
+ { name: 'CONNECTOR_NOTION_REFRESH', value: 'RT-manual', scope: [] })
+ t.after(done)
+ assert.equal(await rt.connectorsInternals.refreshConnector('notion'), true)
+ assert.equal(seen.at(-1), 'manual-client')
+ assert.equal(rt.state.connectors.notion.client.via, 'manual')
+ assert.ok((await rt.diagnostics.entries()).some(row => /refresh succeeded.*manual/.test(row.text)))
+ }
// Legacy rows have no `client`: CIMD when the AS supports it, else needs-sign-in.
{
asMeta = { ...AS, client_id_metadata_document_supported: true }
@@ -353,6 +369,8 @@ test('refresh uses the client that signed in: cimd, catalog, and legacy inferenc
t.after(done)
assert.equal(await rt.connectorsInternals.refreshConnector('notion'), true, JSON.stringify(rt.connectorsInternals.statuses.get('notion')))
assert.equal(seen.at(-1), 'https://dsbot.app/oauth/client.json')
+ assert.deepEqual(rt.state.connectors.notion.client, { via: 'cimd', clientId: oauth.CIMD_CLIENT_ID })
+ assert.ok((await rt.diagnostics.entries()).some(row => /refresh succeeded.*inferred.*cimd/.test(row.text)))
}
{
asMeta = AS
@@ -753,6 +771,83 @@ test('connector-sign-in-code refuses a wrong state and accepts the pasted URL or
assert.equal(rt.secretValue('CONNECTOR_NOTION_TOKEN'), 'AT-9')
})
+test('OAuth loopback keeps an error listener after binding, including repeated errors', async (t) => {
+ const real = globalThis.fetch
+ globalThis.fetch = async (url, init) => {
+ url = String(url?.url ?? url)
+ if (url.includes('oauth-authorization-server')) return json({ ...AS, client_id_metadata_document_supported: true })
+ if (url === 'https://as.test/register') return json({ client_id: 'dcr-local', redirect_uris: JSON.parse(init.body).redirect_uris })
+ if (url.includes('oauth-protected-resource')) return json({ resource: 'https://mcp.notion.com/mcp', authorization_servers: ['https://as.test'] })
+ return new Response('', { status: 401 })
+ }
+ t.after(() => { globalThis.fetch = real })
+ const home = teamHome()
+ const ctx = fakeContext()
+ const rt = createRuntime(ctx, { home, github: false })
+ t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true }) })
+ await rt.load()
+ await rt.connectorEndpoint('connector-connect', { id: 'notion' })
+ const listener = rt.connectorsInternals.pendings.get('notion').listener
+ assert.doesNotThrow(() => {
+ listener.emit('error', new Error('listener failed'))
+ listener.emit('error', new Error('listener failed again'))
+ })
+})
+
+test('Google loopback catches a rejected sign-in callback instead of an unhandled rejection', async (t) => {
+ const oldId = process.env.DS_BOT_GOOGLE_OAUTH_CLIENT_ID
+ const oldSecret = process.env.DS_BOT_GOOGLE_OAUTH_CLIENT_SECRET
+ process.env.DS_BOT_GOOGLE_OAUTH_CLIENT_ID = 'google-test'
+ process.env.DS_BOT_GOOGLE_OAUTH_CLIENT_SECRET = 'google-test-secret'
+ t.after(() => {
+ if (oldId === undefined) delete process.env.DS_BOT_GOOGLE_OAUTH_CLIENT_ID
+ else process.env.DS_BOT_GOOGLE_OAUTH_CLIENT_ID = oldId
+ if (oldSecret === undefined) delete process.env.DS_BOT_GOOGLE_OAUTH_CLIENT_SECRET
+ else process.env.DS_BOT_GOOGLE_OAUTH_CLIENT_SECRET = oldSecret
+ })
+ const home = teamHome()
+ const ctx = fakeContext()
+ const rt = createRuntime(ctx, { home, github: false, mail: { fetch: async url => {
+ if (String(url).includes('oauth-authorization-server')) return json(AS)
+ return notFound()
+ } } })
+ t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true }) })
+ await rt.load()
+ const view = await rt.connectorEndpoint('mail-account-add', { address: addressAt('gmail.com'), google: true })
+ const callback = new URL(view.pending[0].url).searchParams.get('redirect_uri')
+ await fetch(callback) // No code: googleCode rejects.
+ await new Promise(resolve => setImmediate(resolve))
+ assert.ok((await rt.diagnostics.entries()).some(row => /mail Google sign-in failed/.test(row.text)))
+})
+
+test('Google mailbox refresh reports success and failure without credentials or addresses', async () => {
+ const { install } = await import('../src/host/connectors/mail/index.js')
+ const logs = []
+ const secrets = new Map()
+ const address = addressAt('gmail.com')
+ secrets.set(mailSecretName(address, 'REFRESH'), 'google-refresh-private')
+ let fail = false
+ const rt = {
+ state: {}, readOnly: () => undefined, secretValue: key => secrets.get(key),
+ warn: (...args) => logs.push(args),
+ config: { mail: { fetch: async url => {
+ if (String(url).includes('well-known')) return json(AS)
+ if (String(url).includes('/token')) return fail
+ ? json({ error: 'invalid_grant' }, 400)
+ : json({ access_token: 'google-access-private', token_type: 'Bearer' })
+ return notFound()
+ } } },
+ }
+ const mail = install(rt, { secrets: { writeSecret: async (key, value) => secrets.set(key, value) } })
+ assert.equal(await mail._internals.refreshGoogle({ address }), true)
+ fail = true
+ assert.equal(await mail._internals.refreshGoogle({ address }), false)
+ assert.match(JSON.stringify(logs), /succeeded.*manual\/Google/)
+ assert.match(JSON.stringify(logs), /failed.*manual\/Google/)
+ assert.equal(JSON.stringify(logs).includes('private'), false)
+ assert.equal(JSON.stringify(logs).includes(address), false)
+})
+
test('gmail transport: search, read, draft, send, modify, trash, and one refresh on 401', async () => {
const calls = []
let refreshes = 0