Skip to content

Your team of AI agents, on your Mac, Windows or Linux. Agents that talk in channels, write code, review each other and use your tools, on the Claude Code, Codex, OpenCode or Pi you already have. Anything that touches the real world waits for your yes.

Handing the browser back resumes a pending ask_user (#18)

Gian Moraes58000e3120ed
diff --git a/src/core/browser.ts b/src/core/browser.ts
index 3acba25..f7d5595 100644
--- a/src/core/browser.ts
+++ b/src/core/browser.ts
@@ -217,6 +217,7 @@ export interface BrowserDeps {
emit: (e: CoreEvent) => void
me: () => string
jev?: { key(): Promise<string | undefined>; on(): boolean; fetch?: typeof fetch } // finds what to click, when chosen
+ onHandback?: (agentId: string) => void // the user handed the browser back; the runner resumes a pending ask_user
}
type Ctx = { conversationId: string; messageId: string }
@@ -288,7 +289,10 @@ export class Browser {
if (this.views.get(agentId)?.shown === shown) return
this.live(agentId)
this.update(agentId, { shown }, { label: shown ? L(`${this.deps.me()} assumiu`, `${this.deps.me()} took over`) : L(`${this.deps.me()} devolveu`, `${this.deps.me()} handed back`), detail: '' })
- if (!shown) void this.deps.host(agentId, 'frame').then((r) => this.seen(agentId, r), () => {})
+ if (!shown) {
+ void this.deps.host(agentId, 'frame').then((r) => this.seen(agentId, r), () => {})
+ this.deps.onHandback?.(agentId)
+ }
}
async action(agent: Agent, action: 'show' | 'hide' | 'clear'): Promise<void> {
diff --git a/src/core/index.ts b/src/core/index.ts
index 983d935..9e4b198 100644
--- a/src/core/index.ts
+++ b/src/core/index.ts
@@ -237,7 +237,8 @@ const browser = new Browser({
dataDir,
emit,
me: () => settings().workspaceName,
- jev: { key: () => secrets.get(JEV_SECRET), on: () => !!settings().browserJev && settings().jevKey === true }
+ jev: { key: () => secrets.get(JEV_SECRET), on: () => !!settings().browserJev && settings().jevKey === true },
+ onHandback: (agentId) => void runner.handedBack(agentId)
})
const connectors = new ConnectorManager({
db,
diff --git a/src/core/runs.ts b/src/core/runs.ts
index d77abc8..49ab033 100644
--- a/src/core/runs.ts
+++ b/src/core/runs.ts
@@ -1242,6 +1242,35 @@ export class Runner {
for (const ap of this.deps.db.expiredApprovals()) await this.decide(ap.id, 'expired')
}
+ // The user took over the agent's browser ("Assumir") and handed it back
+ // ("Devolver" or closing the window) — exactly what the agent asked for with
+ // ask_user, so it goes on by itself instead of waiting for a message.
+ async handedBack(agentId: string): Promise<void> {
+ // Paused, nobody starts working: the question stays pending, as a message would leave it.
+ if (this.paused) return
+ const agent = this.deps.db.agent(agentId)
+ if (!agent || agent.archived) return
+ let hit: { conv: Conversation; asked: Message } | undefined // the latest pending question of this agent, any conversation
+ for (const conv of this.deps.db.conversations()) {
+ const asked = this.deps.db
+ .recentMessages(conv.id, 50)
+ .reverse()
+ .find((m) => m.author.kind === 'agent' && m.author.id === agentId && m.run?.status === 'waiting_input')
+ if (asked && (!hit || asked.at > hit.asked.at)) hit = { conv, asked }
+ }
+ if (!hit) return
+ const { conv, asked } = hit
+ this.update(asked, { run: { ...asked.run!, status: 'done' } })
+ const { workspaceName } = await this.deps.settings()
+ this.enqueue(() =>
+ this.execute(agent, conv, {
+ depth: 1,
+ work: asked.run?.work,
+ task: L(`${workspaceName} devolveu o controle do seu navegador. Continue de onde parou.`, `${workspaceName} handed your browser back. Pick up where you left off.`)
+ })
+ )
+ }
+
// An agent that mentions another agent of the conversation hands it the turn,
// up to MAX_DEPTH replies in a row without a human message in between.
// Whether the conversation goes on with another agent.
diff --git a/test/runner.test.ts b/test/runner.test.ts
index 9f16b11..46e8688 100644
--- a/test/runner.test.ts
+++ b/test/runner.test.ts
@@ -294,3 +294,66 @@ describe('ask_user through the gateway', () => {
expect(db.messages().some((m) => m.text.startsWith('Chamei'))).toBe(false)
})
})
+
+describe('browser handback', () => {
+ let gateway: Gateway
+
+ beforeEach(async () => {
+ gateway = new Gateway()
+ await gateway.start()
+ return () => gateway.stop()
+ })
+
+ function runnerWithGateway(): Runner {
+ claude = counted(fakeEngine('claude', { delayMs: 1 }))
+ return new Runner({
+ db,
+ engines: { claude, codex: fakeEngine('codex', { delayMs: 1 }), opencode: fakeEngine('opencode'), pi: fakeEngine('pi') },
+ settings: async () => settings,
+ emit: (e) => events.push(e),
+ workDir: mkdtempSync(join(tmpdir(), 'ad-work-')),
+ gateway
+ })
+ }
+
+ it('resumes the pending ask_user on its own when the browser is handed back', async () => {
+ const r = runnerWithGateway()
+ await r.send('c1', '@lia me pergunta o que faltar')
+ await r.idle()
+ const [asked] = agentMessages()
+ expect(asked?.run?.status).toBe('waiting_input')
+
+ // No new human message: the handback itself is the answer.
+ await r.handedBack('a1')
+ await r.idle()
+
+ expect(db.message(asked!.id)?.run?.status).toBe('done')
+ const replies = agentMessages()
+ expect(replies).toHaveLength(2)
+ expect((replies[1]!.author as { id: string }).id).toBe('a1')
+ expect(claude.turns[1]?.prompt).toContain('devolveu o controle do seu navegador')
+ expect(events.some((e) => e.type === 'message' && (e as { message?: Message }).message?.author.kind === 'agent')).toBe(true)
+ })
+
+ it('leaves the question pending while the agents are paused', async () => {
+ const r = runnerWithGateway()
+ await r.send('c1', '@lia me pergunta o que faltar')
+ await r.idle()
+ const [asked] = agentMessages()
+ r.setPaused(true)
+ await r.handedBack('a1')
+ await r.idle()
+ expect(db.message(asked!.id)?.run?.status).toBe('waiting_input')
+ expect(agentMessages()).toHaveLength(1)
+ })
+
+ it('does nothing when nothing is pending or the agent is gone', async () => {
+ const r = runnerWithGateway()
+ await r.handedBack('a1')
+ await r.idle()
+ expect(claude.turns).toHaveLength(0)
+ await r.handedBack('ghost')
+ await r.idle()
+ expect(claude.turns).toHaveLength(0)
+ })
+})