A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Let Google mail sign-in run without an address and consume stale attempts
Fei2411bcff98cb8470
diff --git a/ds-bot/src/host/connectors/mail/index.js b/ds-bot/src/host/connectors/mail/index.jsindex 530d667b..ce079c3f 100644--- a/ds-bot/src/host/connectors/mail/index.js+++ b/ds-bot/src/host/connectors/mail/index.js@@ -428,6 +428,7 @@ export function install(rt, host) {const session = sessions.get(account.id)sessions.delete(account.id)await session?.close?.().catch(() => {})+ dropGooglePending(account.address)syncTools()return mailView()},@@ -496,10 +497,17 @@ export function install(rt, host) {// --- Google sign-in ------------------------------------------------------const googlePendings = new Map()+ const dropGooglePending = key => {+ const pending = googlePendings.get(key)+ if (pending === undefined) return+ googlePendings.delete(key)+ pending.listener?.close()+ }async function beginGoogle(address) {const client = googleClient()const discovery = await discover('https://accounts.google.com', { originFallback: true, fetch: rt.config.mail?.fetch ?? fetch })- const pending = { discovery, address: address || undefined }+ dropGooglePending(address)+ const pending = { discovery, address: address || undefined, key: address }googlePendings.set(address, pending)const { createServer } = await import('node:http')const listener = createServer((req, res) => {@@ -507,7 +515,13 @@ export function install(rt, host) {res.on('error', () => req.destroy())let urltry { url = new URL(req.url ?? '/', 'http://127.0.0.1') } catch { res.writeHead(400); res.end(); return }- res.writeHead(200, { 'content-type': 'text/html' })+ if (url.pathname !== '/callback') { res.writeHead(404); res.end(); return }+ res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' })+ if (!url.searchParams.get('code')) {+ res.end('<p style="font-family:sans-serif">Google sign-in did not finish — go back to DS Bot and try again.</p>')+ rt.warn('ds-bot: mail Google sign-in failed: %s', url.searchParams.get('error') ?? 'no code')+ return+ }res.end('<p style="font-family:sans-serif">Signed in — you can close this tab.</p>')void googleCode({ address, input: url.toString() }).catch(error => rt.warn('ds-bot: mail Google sign-in failed: %s', host.reason(error)))})@@ -517,8 +531,10 @@ export function install(rt, host) {await new Promise((resolve, reject) => { listener.once('error', reject); listener.listen(0, '127.0.0.1', resolve) })pending.listener = listenerpending.redirectUri = `http://127.0.0.1:${listener.address().port}/callback`- const { url, codeVerifier } = await beginBrowser(discovery, { client_id: client.id, client_secret: client.secret }, pending.redirectUri, { state: randomUUID(), scope: GOOGLE_SCOPES })+ const state = randomUUID()+ const { url, codeVerifier } = await beginBrowser(discovery, { client_id: client.id, client_secret: client.secret }, pending.redirectUri, { state, scope: GOOGLE_SCOPES })pending.codeVerifier = codeVerifier+ pending.state = statepending.url = urlreturn mailView()}@@ -530,6 +546,10 @@ export function install(rt, host) {const url = /^https?:/i.test(input) ? new URL(input) : undefinedconst code = url ? url.searchParams.get('code') : inputif (!code) throw new Error('The address has no code')+ const given = url?.searchParams.get('state')+ if (given !== null && pending.state !== undefined && given !== pending.state) throw new Error('The sign-in answer is not for this attempt')+ googlePendings.delete(pending.key ?? pending.address)+ pending.listener?.close()const client = googleClient()const tokens = await exchange(pending.discovery, { client_id: client.id, client_secret: client.secret }, { code, codeVerifier: pending.codeVerifier, redirectUri: pending.redirectUri, iss: url?.searchParams.get('iss') ?? undefined }, { fetch: rt.config.mail?.fetch })const email = pending.address || await googleEmail(tokens.access_token)@@ -539,8 +559,6 @@ export function install(rt, host) {await host.secrets.writeSecret(mailSecretName(email, 'TOKEN'), tokens.access_token, `Mailbox ${email}`)if (typeof tokens.refresh_token === 'string') await host.secrets.writeSecret(mailSecretName(email, 'REFRESH'), tokens.refresh_token, `Mailbox ${email}`)await rt.save()- pending.listener?.close()- googlePendings.delete(pending.address)syncTools()return mailView()}diff --git a/ds-bot/test/connectors-engine.test.js b/ds-bot/test/connectors-engine.test.jsindex dd98be6b..9ca7d75b 100644--- a/ds-bot/test/connectors-engine.test.js+++ b/ds-bot/test/connectors-engine.test.js@@ -895,6 +895,52 @@ test('Google loopback catches a rejected sign-in callback instead of an unhandleassert.ok((await rt.diagnostics.entries()).some(row => /mail Google sign-in failed/.test(row.text)))})+test('Google sign-in retries get a fresh URL and a success clears the pending row', 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 => {+ const u = String(url)+ if (u.includes('oauth-authorization-server')) return json(AS)+ if (u === 'https://as.test/token') return json({ access_token: 'tok-aaaa', refresh_token: 'ref-bbbb', expires_in: 3600, token_type: 'Bearer' })+ if (u.includes('userinfo')) return json({ email: '[email protected]' })+ return notFound()+ } } })+ t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true }) })+ await rt.load()+ const first = await rt.connectorEndpoint('mail-account-add', { google: true })+ const second = await rt.connectorEndpoint('mail-account-add', { google: true })+ assert.notEqual(first.pending[0].url, second.pending[0].url, 'retry should start a new attempt')+ const url = new URL(second.pending[0].url)+ const callback = new URL(url.searchParams.get('redirect_uri'))+ callback.search = `?code=C&state=WRONG`+ const bad = await fetch(callback)+ assert.equal(bad.status, 200)+ await new Promise(resolve => setImmediate(resolve))+ assert.ok((await rt.diagnostics.entries()).some(row => /not for this attempt/.test(row.text)))+ callback.search = `?code=C&state=${url.searchParams.get('state')}`+ const ok = await fetch(callback)+ assert.match(await ok.text(), /Signed in/)+ for (let i = 0; i < 20; i++) {+ await new Promise(resolve => setImmediate(resolve))+ const view = await rt.connectorEndpoint('mail-rule-set', {})+ if (view.accounts.some(a => a.address === '[email protected]')) {+ assert.ok(!view.pending?.length, 'pending row must be gone after a successful sign-in: ' + JSON.stringify({ second: second.pending[0].url, leftover: view.pending }))+ return+ }+ }+ assert.fail('account never landed')+})+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 = []