Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

PRD-9486: Remove browser folder when discarding an empty agent chat (#6546)

Andrew Mikofalvy4e742d450f90
diff --git a/.changeset/discard-empty-chat-browser-folder.md b/.changeset/discard-empty-chat-browser-folder.md
new file mode 100644
index 000000000..0c3bcc4c2
--- /dev/null
+++ b/.changeset/discard-empty-chat-browser-folder.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Closing an agent chat before sending it a message now also removes that chat's browser files, as deleting a chat already did.
diff --git a/docs/content/features/editor.mdx b/docs/content/features/editor.mdx
index 734d884d3..d67baaa80 100644
--- a/docs/content/features/editor.mdx
+++ b/docs/content/features/editor.mdx
@@ -345,7 +345,7 @@ In-app agent chats in the Agents panel follow the same rule and the same setting
Shell commands in an in-app chat still prompt one at a time. When the command an agent asks to run is read-only (listing, reading, and searching files, `git status`, `log`, `diff` and other read-only git subcommands, and plain `curl` GET probes), the prompt adds **Allow read-only commands while this chat is open**. Choosing it approves that command and every later read-only command in the same chat without a prompt. A pipeline or compound command counts as read-only only when every stage does, and output can go only to `/dev/null` or into another stream (`2>&1`). Globs and variables are fine in the arguments of commands that cannot change anything, such as `cat`, `ls`, `grep` and `echo`, but `git`, `curl`, `find`, `sort` and the other commands whose options the classifier checks prompt when handed one. Command substitution, and anything else the classifier cannot prove read-only, keeps prompting. The grant shows as **Read-only allowed** on the agent settings trigger, lasts while the chat stays open (a restarted server prompts again), and **Ask again before read-only commands** in that menu revokes it.
-In-app Claude Code and Codex chats can also get a browser they can drive. Turn on **Settings → Preferences → “Let agent chats use a browser”** ([`agents.browserTools`](../reference/configuration.mdx), off by default) and each Claude Code or Codex chat you start afterwards gets the [Playwright MCP](https://github.com/microsoft/playwright-mcp) server, so the agent can open pages, click, type, and take screenshots, for example to check a site it just built. Other agents' chats don't get it, because OpenKnowledge can't tell their browser actions apart from their other tools. It needs Google Chrome and Node.js installed. OpenKnowledge starts the server with the `npx` on the agent's own PATH, from a folder of its own outside the project, so npm never uses the project's `package.json`, `node_modules`, or `.npmrc`. It installs the server before the chat starts, so closing the chat during that install can't leave a broken copy in npm's cache. If that install takes longer than two minutes (45 seconds when you resume or retry a chat, change its context window, or finish signing in to its agent), the chat starts without the browser while the install carries on, and chats you start once it finishes get the browser. If it can't find a usable `npx`, or the browser can't be set up, the chat starts without the browser and says so in the conversation, and the server log has the details. Without Chrome, the agent's browser actions fail with a message that Chrome is missing. The browser opens in its own window (headless on Linux with no display) with a fresh profile kept in memory, so it starts signed out of everything, and pages cannot add tools of their own. Every browser action asks for your approval, with no option to always allow it, unless the agent skips approvals itself: through its mode (the **Agent mode** item in the chat's agent settings menu) or its own permission settings, such as a Claude Code rule that allows the browser's tools. Everything the browser saves, whether or not the agent names the file, goes to a folder OpenKnowledge keeps for the chat under `~/.ok/agent-browser/` (Beta: `~/.ok-beta/agent-browser/`), outside the project, and is removed when you delete the chat. Pages can also carry instructions aimed at the agent, and OpenKnowledge's own read and write tools still run without asking; turn off **Let agents use OpenKnowledge without asking** ([`agents.autoApproveOkTools`](../reference/configuration.mdx)) to approve those too.
+In-app Claude Code and Codex chats can also get a browser they can drive. Turn on **Settings → Preferences → “Let agent chats use a browser”** ([`agents.browserTools`](../reference/configuration.mdx), off by default) and each Claude Code or Codex chat you start afterwards gets the [Playwright MCP](https://github.com/microsoft/playwright-mcp) server, so the agent can open pages, click, type, and take screenshots, for example to check a site it just built. Other agents' chats don't get it, because OpenKnowledge can't tell their browser actions apart from their other tools. It needs Google Chrome and Node.js installed. OpenKnowledge starts the server with the `npx` on the agent's own PATH, from a folder of its own outside the project, so npm never uses the project's `package.json`, `node_modules`, or `.npmrc`. It installs the server before the chat starts, so closing the chat during that install can't leave a broken copy in npm's cache. If that install takes longer than two minutes (45 seconds when you resume or retry a chat, change its context window, or finish signing in to its agent), the chat starts without the browser while the install carries on, and chats you start once it finishes get the browser. If it can't find a usable `npx`, or the browser can't be set up, the chat starts without the browser and says so in the conversation, and the server log has the details. Without Chrome, the agent's browser actions fail with a message that Chrome is missing. The browser opens in its own window (headless on Linux with no display) with a fresh profile kept in memory, so it starts signed out of everything, and pages cannot add tools of their own. Every browser action asks for your approval, with no option to always allow it, unless the agent skips approvals itself: through its mode (the **Agent mode** item in the chat's agent settings menu) or its own permission settings, such as a Claude Code rule that allows the browser's tools. Everything the browser saves, whether or not the agent names the file, goes to a folder OpenKnowledge keeps for the chat under `~/.ok/agent-browser/` (Beta: `~/.ok-beta/agent-browser/`), outside the project, and is removed when you delete the chat, or when you close a chat you never sent a message to. Pages can also carry instructions aimed at the agent, and OpenKnowledge's own read and write tools still run without asking; turn off **Let agents use OpenKnowledge without asking** ([`agents.autoApproveOkTools`](../reference/configuration.mdx)) to approve those too.
## Tabs and split panes
diff --git a/docs/content/reference/what-open-knowledge-writes.mdx b/docs/content/reference/what-open-knowledge-writes.mdx
index 50516a720..2a9069fc9 100644
--- a/docs/content/reference/what-open-knowledge-writes.mdx
+++ b/docs/content/reference/what-open-knowledge-writes.mdx
@@ -200,7 +200,7 @@ Beta writes the `~/.ok/` entries below under `~/.ok-beta/` instead, except the s
| `~/.agents/skills/<name>/`, `~/.claude/skills/<name>/`, … | Your global-scope skills, versioned in place in your home editor dirs (kept by `ok uninstall` unless `--purge-content`) | No |
| `~/.ok/local/installed-skills.json`, `~/.ok/local/skill-placements.json`, `~/.ok/local/skill-move-retained.json`, `~/.ok/skills-lock.json` | The same skill-install markers, placement records, unfinished-scope-move bookkeeping, and import provenance that a project keeps under its own `.ok/`, written under your home directory for Global-scope skills instead. Shared by every product: Beta writes these to `~/.ok/` too | No |
| `~/.ok/stats.jsonl` | Local "open with AI" handoff stats — local-only, no phone-home | No |
-| `~/.ok/agent-browser/<chat>/` | Only while **Let agent chats use a browser** is on: `npm/`, the folder that chat's browser server starts from, rebuilt each time it launches, and `files/`, everything the browser saves. Removed when you delete the chat in OpenKnowledge; turning the setting off keeps it, and nothing else clears it, so delete the folder yourself to remove every chat's saved files at once | No |
+| `~/.ok/agent-browser/<chat>/` | Only while **Let agent chats use a browser** is on: `npm/`, the folder that chat's browser server starts from, rebuilt each time it launches, and `files/`, everything the browser saves. Removed when you delete the chat in OpenKnowledge, or when you close a chat you never sent a message to; turning the setting off keeps it, and a closed chat you did message keeps it until you delete that chat, so delete the folder yourself to remove every chat's saved files at once | No |
| `_npx/<hash>/` in the npm cache that launch uses: your own npm cache (`~/.npm` unless your `npm_config_cache` says otherwise) when the agent runs through your system npx, or OpenKnowledge's managed Node.js runtime cache at `~/.ok/runtimes/cache/node` when no usable system Node.js was found and the agent runs on that runtime | Written when OpenKnowledge installs or launches an in-app agent through npx, a custom command such as `npx -y <package>` included, and when the browser server a Claude Code or Codex chat starts while **Let agent chats use a browser** is on installs `@playwright/mcp`. OpenKnowledge repairs the entry only for catalog-managed agents and the browser server, never for custom agent commands or uv-managed adapters. When npm reports that the installed copy is missing as a catalog-managed agent starts, OpenKnowledge installs it again once. When npm reports that the entry is missing its `package.json`, OpenKnowledge deletes that one directory and relaunches the agent once, which installs it again. The browser server's entry is cleared the same way and reinstalled once | No |
| `~/.ok/themes/` | Your saved color themes from the [Themes plugin](../plugins/themes.mdx), one Tinted Theming base16 YAML file per theme, written when you create or edit a theme | No |
| `~/.ok/edit-backups/discarded/projects/<project>/<document>/`, `~/.ok/edit-backups/discarded/<skill>/` | A copy of a version OpenKnowledge set aside but could not save to the project's history, for example in a project with no shadow repo: the outside version of a Mermaid, Excalidraw or plain-text file, a skill's outside version that could not be merged, or a settings version that was replaced. Files of a project go in a folder for that project (`<project>` is a short hash of its content folder), so two projects with the same file keep separate copies; global and external skills keep one folder each. Each copy is named for the time it was kept, and the five newest per folder stay. The notice on the document, or in Settings, names the file | No |
@@ -343,7 +343,7 @@ Beta writes the `~/.ok/` entries below under `~/.ok-beta/` instead, except the s
| `~/.agents/skills/<name>/`, `~/.claude/skills/<name>/`, … | Your global-scope skills, versioned in place in your home editor dirs (kept by `ok uninstall` unless `--purge-content`) | No |
| `~/.ok/local/installed-skills.json`, `~/.ok/local/skill-placements.json`, `~/.ok/local/skill-move-retained.json`, `~/.ok/skills-lock.json` | The same skill-install markers, placement records, unfinished-scope-move bookkeeping, and import provenance that a project keeps under its own `.ok/`, written under your home directory for Global-scope skills instead. Shared by every product: Beta writes these to `~/.ok/` too | No |
| `~/.ok/stats.jsonl` | Local "open with AI" handoff stats — local-only, no phone-home | No |
-| `~/.ok/agent-browser/<chat>/` | Only while **Let agent chats use a browser** is on: `npm/`, the folder that chat's browser server starts from, rebuilt each time it launches, and `files/`, everything the browser saves. Removed when you delete the chat in OpenKnowledge; turning the setting off keeps it, and nothing else clears it, so delete the folder yourself to remove every chat's saved files at once | No |
+| `~/.ok/agent-browser/<chat>/` | Only while **Let agent chats use a browser** is on: `npm/`, the folder that chat's browser server starts from, rebuilt each time it launches, and `files/`, everything the browser saves. Removed when you delete the chat in OpenKnowledge, or when you close a chat you never sent a message to; turning the setting off keeps it, and a closed chat you did message keeps it until you delete that chat, so delete the folder yourself to remove every chat's saved files at once | No |
| `_npx/<hash>/` in the npm cache that launch uses: your own npm cache (`~/.npm` unless your `npm_config_cache` says otherwise) when the agent runs through your system npx, or OpenKnowledge's managed Node.js runtime cache at `~/.ok/runtimes/cache/node` when no usable system Node.js was found and the agent runs on that runtime | Written when OpenKnowledge installs or launches an in-app agent through npx, a custom command such as `npx -y <package>` included, and when the browser server a Claude Code or Codex chat starts while **Let agent chats use a browser** is on installs `@playwright/mcp`. OpenKnowledge repairs the entry only for catalog-managed agents and the browser server, never for custom agent commands or uv-managed adapters. When npm reports that the installed copy is missing as a catalog-managed agent starts, OpenKnowledge installs it again once. When npm reports that the entry is missing its `package.json`, OpenKnowledge deletes that one directory and relaunches the agent once, which installs it again. The browser server's entry is cleared the same way and reinstalled once | No |
| `~/.ok/themes/` | Your saved color themes from the [Themes plugin](../plugins/themes.mdx), one Tinted Theming base16 YAML file per theme, written when you create or edit a theme | No |
| `~/.ok/edit-backups/discarded/projects/<project>/<document>/`, `~/.ok/edit-backups/discarded/<skill>/` | A copy of a version OpenKnowledge set aside but could not save to the project's history, for example in a project with no shadow repo: the outside version of a Mermaid, Excalidraw or plain-text file, a skill's outside version that could not be merged, or a settings version that was replaced. Files of a project go in a folder for that project (`<project>` is a short hash of its content folder), so two projects with the same file keep separate copies; global and external skills keep one folder each. Each copy is named for the time it was kept, and the five newest per folder stay. The notice on the document, or in Settings, names the file | No |
diff --git a/packages/server/src/acp/thread-manager.integration.test.ts b/packages/server/src/acp/thread-manager.integration.test.ts
index a3eac2ab5..be465166c 100644
--- a/packages/server/src/acp/thread-manager.integration.test.ts
+++ b/packages/server/src/acp/thread-manager.integration.test.ts
@@ -1349,11 +1349,12 @@ describe('AcpThreadManager persistence + resume', () => {
expect(agentChunks(replayed)).toContain(notedEcho(localDir, 'hello there'));
}, 45_000);
- test('closing a never-prompted thread discards it instead of archiving', async () => {
+ test('closing a never-prompted thread discards it and its browser folders instead of archiving', async () => {
const contentDir = tmp();
const localDir = tmp();
+ const globalDir = tmp();
writeExampleAgentEntry(localDir);
- const manager = makeManager(contentDir, localDir);
+ const manager = makeManager(contentDir, localDir, { globalDir });
await manager.init();
const info = await manager.createThread({ agent: { source: 'custom', id: 'example' } });
@@ -1362,9 +1363,16 @@ describe('AcpThreadManager persistence + resume', () => {
15_000,
'agent ready',
);
+ const own = join(globalDir, 'agent-browser', info.threadId);
+ const sibling = join(globalDir, 'agent-browser', crypto.randomUUID());
+ mkdirSync(own, { recursive: true });
+ writeFileSync(join(own, 'files-page.png'), 'x');
+ mkdirSync(sibling, { recursive: true });
await manager.closeThread(info.threadId);
expect(manager.listThreads().find((t) => t.threadId === info.threadId)).toBeUndefined();
+ expect(existsSync(own)).toBe(false);
+ expect(existsSync(sibling)).toBe(true);
const manager2 = makeManager(contentDir, localDir);
await manager2.init();
expect(manager2.listThreads().find((t) => t.threadId === info.threadId)).toBeUndefined();
diff --git a/packages/server/src/acp/thread-manager.ts b/packages/server/src/acp/thread-manager.ts
index a43c76aba..322600ccb 100644
--- a/packages/server/src/acp/thread-manager.ts
+++ b/packages/server/src/acp/thread-manager.ts
@@ -3581,15 +3581,7 @@ export class AcpThreadManager {
});
const failedStart = t.info.status === 'error' || t.info.status === 'auth_required';
if (!t.hadUserMessage && !failedStart) {
- this.threads.delete(threadId);
- t.subscribers.clear();
- if (t.flushTimer !== null) {
- clearTimeout(t.flushTimer);
- t.flushTimer = null;
- }
- this.clearStall(t);
- await this.persistence.whenIdle(threadId);
- await this.persistence.delete(threadId);
+ await this.forgetThread(t);
this.opts.log.info({ threadId }, '[acp-threads] empty thread discarded on close');
return;
}
@@ -3617,6 +3609,12 @@ export class AcpThreadManager {
if (t.resumeInFlight) {
throw new ThreadOpError('not-ready', 'a resume is in progress');
}
+ await this.forgetThread(t);
+ this.opts.log.info({ threadId }, '[acp-threads] thread deleted');
+ }
+
+ private async forgetThread(t: ThreadRecord): Promise<void> {
+ const threadId = t.info.threadId;
this.threads.delete(threadId);
t.subscribers.clear();
if (t.flushTimer !== null) {
@@ -3631,7 +3629,6 @@ export class AcpThreadManager {
this.opts.log.warn({ err, threadId }, '[acp-threads] removing browser folders failed');
});
}
- this.opts.log.info({ threadId }, '[acp-threads] thread deleted');
}
async destroy(): Promise<void> {