Skip to content

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

Fix saved tab and load error edge cases

Fei241128edbb801310
diff --git a/app/src/bot-views.mjs b/app/src/bot-views.mjs
index 7eeb4516..75d85c0c 100644
--- a/app/src/bot-views.mjs
+++ b/app/src/bot-views.mjs
@@ -46,7 +46,28 @@ export class BotViews {
if (statePath !== undefined) {
try {
const stored = JSON.parse(readFileSync(statePath, 'utf8'))
- if (stored && typeof stored === 'object' && stored.bots && typeof stored.bots === 'object') this.#saved = stored.bots
+ if (stored && typeof stored === 'object' && stored.bots && typeof stored.bots === 'object') {
+ const bots = {}
+ let changed = false
+ for (const [botId, saved] of Object.entries(stored.bots)) {
+ if (!saved || !Array.isArray(saved.tabs)) { changed = true; continue }
+ const tabs = saved.tabs.flatMap(tab => {
+ if (!tab || typeof tab.tabId !== 'string' || tab.tabId.length === 0) { changed = true; return [] }
+ const url = typeof tab.url === 'string' ? tab.url : 'about:blank'
+ const safeUrl = allowedNavigation(url, this.#origin()) ? url : 'about:blank'
+ const title = safeUrl === url && typeof tab.title === 'string' ? tab.title : ''
+ if (safeUrl !== url || title !== tab.title) changed = true
+ return [{ tabId: tab.tabId, url: safeUrl, title }]
+ })
+ const active = tabs.some(tab => tab.tabId === saved.active) ? saved.active : tabs[0]?.tabId ?? null
+ if (tabs.length !== saved.tabs.length || active !== saved.active) changed = true
+ if (tabs.length === 0) { changed = true; continue }
+ bots[botId] = { tabs, active }
+ }
+ if (Object.keys(bots).length !== Object.keys(stored.bots).length) changed = true
+ this.#saved = bots
+ if (changed) this.#persist()
+ }
} catch {}
}
this.#server = createServer((_request, response) => { response.writeHead(404).end() })
@@ -102,8 +123,8 @@ export class BotViews {
})
contents.on('did-start-loading', () => this.#publish(entry))
contents.on('did-stop-loading', () => this.#publish(entry))
- contents.on('did-start-navigation', (_event, _url, isInPlace, isMainFrame) => {
- if (isMainFrame && !isInPlace) {
+ contents.on('did-start-navigation', event => {
+ if (event.isMainFrame && !event.isSameDocument) {
entry.error = null
this.#publish(entry)
}
@@ -231,7 +252,8 @@ export class BotViews {
if (this.sync(botId).length === 0) {
const saved = this.#saved[botId]
const restored = saved?.tabs?.find(tab => tab.tabId === saved.active) ?? saved?.tabs?.[0]
- this.create(botId, { url: restored?.url ?? 'about:blank', ...(restored?.tabId ? { tabId: restored.tabId } : {}), createdByBot: true })
+ const url = restored && allowedNavigation(restored.url, this.#origin()) ? restored.url : 'about:blank'
+ this.create(botId, { url, ...(restored?.tabId ? { tabId: restored.tabId } : {}), createdByBot: true })
}
for (const [token, id] of this.#tokens) if (id === botId) this.#tokens.delete(token)
const token = randomBytes(32).toString('hex')
@@ -307,9 +329,15 @@ export class BotViews {
const previous = this.#saved[botId]
if (!previous) return
const tabs = previous.tabs.filter(tab => tab.tabId !== tabId)
- const active = this.#active.get(botId) === tabId ? tabs.at(-1)?.tabId ?? null : previous.active
- if (tabs.length === 0) delete this.#saved[botId]
- else this.#saved[botId] = { tabs, active }
+ const wasActive = this.#active.get(botId) === tabId
+ const active = wasActive ? tabs.at(-1)?.tabId ?? null : previous.active
+ if (tabs.length === 0) {
+ delete this.#saved[botId]
+ this.#active.delete(botId)
+ } else {
+ if (wasActive) this.#active.set(botId, active)
+ this.#saved[botId] = { tabs, active }
+ }
this.#persist()
}
diff --git a/app/test/bot-views.test.mjs b/app/test/bot-views.test.mjs
index 79a49e8d..9f7d051b 100644
--- a/app/test/bot-views.test.mjs
+++ b/app/test/bot-views.test.mjs
@@ -1,6 +1,6 @@
import assert from 'node:assert/strict'
import { EventEmitter } from 'node:events'
-import { existsSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
+import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { test } from 'node:test'
@@ -268,6 +268,49 @@ test('saved active tabs restore their tab id and URL', async t => {
t.after(() => socket.close())
})
+test('closing the active saved tab preserves the replacement after another page updates', async t => {
+ const directory = mkdtempSync(join(tmpdir(), 'bot-views-active-state-'))
+ const statePath = join(directory, 'bot-views.json')
+ t.after(() => rmSync(directory, { recursive: true, force: true }))
+ const first = fixture({ statePath })
+ first.manager.create('a', { tabId: 'A', url: 'http://127.0.0.1:43118/a' })
+ first.manager.create('a', { tabId: 'B', url: 'http://127.0.0.1:43118/b' })
+ first.manager.create('a', { tabId: 'C', url: 'http://127.0.0.1:43118/c' })
+ first.manager.place('B', { left: 0, top: 0, width: 400, height: 300 }, true)
+ await first.manager.close('a', 'B')
+ first.views[0].webContents.url = 'http://127.0.0.1:43118/a-updated'
+ first.views[0].webContents.emit('did-navigate')
+ first.manager.dispose()
+
+ const second = fixture({ statePath })
+ t.after(() => second.manager.dispose())
+ await second.manager.endpoint('a')
+ assert.deepEqual(second.manager.sync('a').map(({ tabId, url }) => ({ tabId, url })), [
+ { tabId: 'C', url: 'http://127.0.0.1:43118/c' },
+ ])
+})
+
+test('saved file and app-origin URLs restore as blank with their tab ids', async t => {
+ const directory = mkdtempSync(join(tmpdir(), 'bot-views-unsafe-state-'))
+ const statePath = join(directory, 'bot-views.json')
+ t.after(() => rmSync(directory, { recursive: true, force: true }))
+ writeFileSync(statePath, JSON.stringify({ version: 1, bots: {
+ file: { tabs: [{ tabId: 'file-tab', url: 'file:///etc/passwd', title: 'passwd' }], active: 'file-tab' },
+ app: { tabs: [{ tabId: 'app-tab', url: 'http://127.0.0.1:43117/settings', title: 'Settings' }], active: 'app-tab' },
+ } }))
+ const { manager } = fixture({ statePath })
+ t.after(() => manager.dispose())
+ const stored = JSON.parse(readFileSync(statePath, 'utf8'))
+ assert.deepEqual(stored.bots.file.tabs, [{ tabId: 'file-tab', url: 'about:blank', title: '' }])
+ assert.deepEqual(stored.bots.app.tabs, [{ tabId: 'app-tab', url: 'about:blank', title: '' }])
+ for (const [botId, tabId] of [['file', 'file-tab'], ['app', 'app-tab']]) {
+ await manager.endpoint(botId)
+ assert.deepEqual(manager.sync(botId).map(({ tabId: actualId, url }) => ({ tabId: actualId, url })), [
+ { tabId, url: 'about:blank' },
+ ])
+ }
+})
+
test('load errors remain published until a real main-frame navigation starts', async t => {
const { manager, views, changes } = fixture()
t.after(() => manager.dispose())
@@ -276,7 +319,7 @@ test('load errors remain published until a real main-frame navigation starts', a
contents.emit('did-fail-load', {}, -105, 'offline', contents.getURL(), true)
contents.emit('did-stop-loading')
assert.equal(changes.at(-1).error, 'offline')
- contents.emit('did-start-navigation', {}, 'http://127.0.0.1:43118/form', false, true)
+ contents.emit('did-start-navigation', { isMainFrame: true, isSameDocument: false })
assert.equal(changes.at(-1).error, null)
assert.ok(entry)
})