Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(web-app): run the Cowork runner in the thread sandbox
Akarshan Biswasc6db55633472
diff --git a/web-app/src/lib/__tests__/agentTools.test.ts b/web-app/src/lib/__tests__/agentTools.test.tsindex c983bb871..3dfe3dd2b 100644--- a/web-app/src/lib/__tests__/agentTools.test.ts+++ b/web-app/src/lib/__tests__/agentTools.test.ts@@ -10,6 +10,8 @@ const memoryCatalog = vi.fn()const memoryRead = vi.fn()const subagentResultReserve = vi.fn()const subagentResultFill = vi.fn()+const attachmentImport = vi.fn()+const startMonitor = vi.fn()const getJanDataFolder = vi.fn()vi.mock('@janhq/tauri-plugin-agent-tools-api', () => ({@@ -24,7 +26,8 @@ vi.mock('@janhq/tauri-plugin-agent-tools-api', () => ({memoryRead: (...args: unknown[]) => memoryRead(...args),subagentResultReserve: (...args: unknown[]) => subagentResultReserve(...args),subagentResultFill: (...args: unknown[]) => subagentResultFill(...args),- attachmentImport: vi.fn(),+ attachmentImport: (...args: unknown[]) => attachmentImport(...args),+ startMonitor: (...args: unknown[]) => startMonitor(...args),}))vi.mock('@/hooks/useServiceHub', () => ({@@ -53,6 +56,8 @@ describe('agentTools', () => {memoryRead.mockReset().mockResolvedValue('')subagentResultReserve.mockReset()subagentResultFill.mockReset()+ attachmentImport.mockReset()+ startMonitor.mockReset()getJanDataFolder.mockReset().mockResolvedValue('/data')})@@ -500,6 +505,30 @@ describe('agentTools', () => {expect(threadWorkspaceSweep).toHaveBeenCalledWith('/data', ['a', 'b'])})+ // A Cowork conversation is a thread now, so an attachment must land in the+ // sandbox its tools run in, not the legacy sessions/ tree.+ it('imports an attachment into the thread sandbox', async () => {+ attachmentImport.mockResolvedValue({ path: '/p/a.txt', textPath: null })+ const { importAttachment } = await import('../agentTools')+ await importAttachment('c1', '/src/a.txt', 'text')+ expect(attachmentImport).toHaveBeenCalledWith(+ '/data',+ 'c1',+ '/src/a.txt',+ 'text',+ 'thread'+ )+ })++ it('starts a monitor in the thread sandbox', async () => {+ startMonitor.mockResolvedValue('m1')+ const { startAgentMonitor } = await import('../agentTools')+ await startAgentMonitor('c1', { path: 'a' }, () => {})+ expect(startMonitor.mock.calls.at(-1)?.[4]).toMatchObject({+ scope: 'thread',+ })+ })+it('copies a session sandbox into the thread workspace of the same id', async () => {const { copySessionWorkspaceToThread } = await import('../agentTools')await expect(copySessionWorkspaceToThread('s1')).resolves.toMatchObject({diff --git a/web-app/src/lib/__tests__/coworkDispatch.test.ts b/web-app/src/lib/__tests__/coworkDispatch.test.tsindex 98de0add4..1a904706c 100644--- a/web-app/src/lib/__tests__/coworkDispatch.test.ts+++ b/web-app/src/lib/__tests__/coworkDispatch.test.ts@@ -40,9 +40,10 @@ describe('dispatchCoworkTool', () => {executeAgentTool.mockResolvedValue({ content: 'ok' })})- // The trailing 'session' is load-bearing: under the default 'thread' scope a- // session's files land where the thread sweep's keep-list can never mention- // them, and the sweep deletes the only copy of the agent's work.+ // The scope is load-bearing: a Cowork conversation is a thread, and the+ // migration copied its sandbox to threads/<id>. Running it under 'session'+ // would read the stale pre-migration copy and keep the thread sweep from+ // ever seeing the files the agent writes.it('routes built-ins to the Rust plugin with the session as workspace key', async () => {const c = ctx()await dispatchCoworkTool(call('read', { path: 'a' }), c)@@ -51,7 +52,7 @@ describe('dispatchCoworkTool', () => {{ path: 'a' },'s1',null,- 'session',+ 'thread',true,true,null@@ -67,7 +68,7 @@ describe('dispatchCoworkTool', () => {{ command: 'curl x' },'s1',null,- 'session',+ 'thread',true,true,null@@ -80,7 +81,7 @@ describe('dispatchCoworkTool', () => {{ command: 'curl x' },'s1',null,- 'session',+ 'thread',false,true,null@@ -154,7 +155,7 @@ describe('dispatchCoworkTool', () => {{},'s1','/repo',- 'session',+ 'thread',true,true,'/repo'diff --git a/web-app/src/lib/agentTools.ts b/web-app/src/lib/agentTools.tsindex a400c7805..8d9885fc1 100644--- a/web-app/src/lib/agentTools.ts+++ b/web-app/src/lib/agentTools.ts@@ -323,7 +323,7 @@ function isMemoryChange(args: Record<string, unknown>): boolean {export type { MonitorUpdate }/**- * Start a file monitor for a Cowork session. `onUpdate` receives every match+ * Start a file monitor for a Cowork conversation (a thread). `onUpdate` receives every match* (and the terminal all-met/timeout update) as it happens; the returned string* is the model-facing `monitor` tool result. An error comes back as an* `ERROR:`-prefixed output rather than a throw, matching the tool contract.@@ -345,7 +345,7 @@ export async function startAgentMonitor(allowNetwork,readOnlyProject: readOnlyProject ?? undefined,projectWritable,- scope: 'session',+ scope: 'thread',})return { output }} catch (e) {@@ -485,7 +485,8 @@ export async function importAttachment(try {const dataFolder = await getServiceHub().app().getJanDataFolder()if (!dataFolder) return null- return await attachmentImport(dataFolder, sessionId, source, text)+ // Thread scope: the sandbox the conversation's tools run in.+ return await attachmentImport(dataFolder, sessionId, source, text, 'thread')} catch (e) {console.warn('[agentTools] Failed to import attachment:', messageOf(e))return nulldiff --git a/web-app/src/lib/coworkDispatch.ts b/web-app/src/lib/coworkDispatch.tsindex 6552de06d..757006b10 100644--- a/web-app/src/lib/coworkDispatch.ts+++ b/web-app/src/lib/coworkDispatch.ts@@ -227,9 +227,10 @@ export async function dispatchCoworkTool(}}- // `'session'`, not the default `'thread'`: a Cowork session id lives in its- // own namespace, and the thread sweep would otherwise delete this sandbox- // because no chat thread claims it. The network flag follows Cowork's own+ // `'thread'`: a Cowork conversation is a thread, and the migration copied+ // its sandbox to threads/<id>, so that is where its files live and what the+ // thread sweep keeps. (Spelled out because `scope` precedes the arguments+ // after it.) The network flag follows Cowork's own// setting (on by default), read per call so a toggle in Settings applies to// the next command; chat's shell stays network-closed regardless.// The attached folder is writable on this surface: Cowork's shared folder@@ -241,7 +242,7 @@ export async function dispatchCoworkTool(call.input,ctx.sessionId,ctx.readOnlyFolder,- 'session',+ 'thread',useCoworkConfig.getState().networkEnabled,true,ctx.readOnlyFolderdiff --git a/web-app/src/routes/__tests__/coworkSkills.test.tsx b/web-app/src/routes/__tests__/coworkSkills.test.tsxindex effcf2d6c..7d3551e0c 100644--- a/web-app/src/routes/__tests__/coworkSkills.test.tsx+++ b/web-app/src/routes/__tests__/coworkSkills.test.tsx@@ -112,7 +112,7 @@ vi.mock('@/lib/coworkSubagentRegistry', () => ({}))vi.mock('@/lib/model-factory', () => ({ ModelFactory: {} }))vi.mock('@janhq/tauri-plugin-agent-tools-api', () => ({- sessionWorkspacePath: async () => '/sandbox',+ threadWorkspacePath: async () => '/sandbox',}))vi.mock('@/containers/ChatInput', () => ({default: ({diff --git a/web-app/src/routes/artifacts.tsx b/web-app/src/routes/artifacts.tsxindex c61152171..822912036 100644--- a/web-app/src/routes/artifacts.tsx+++ b/web-app/src/routes/artifacts.tsx@@ -18,7 +18,7 @@ import { useTranslation } from '@/i18n/react-i18next-compat'import { useCoworkSessions } from '@/hooks/useCoworkSessions'import { useCoworkRun } from '@/hooks/useCoworkRun'import { getServiceHub } from '@/hooks/useServiceHub'-import { sessionWorkspacePath } from '@janhq/tauri-plugin-agent-tools-api'+import { threadWorkspacePath } from '@janhq/tauri-plugin-agent-tools-api'import {ARTIFACT_GROUP_NAMES,artifactsFromTurns,@@ -59,7 +59,7 @@ function useSessionWorkspaces(sessionIds: string[]): Record<string, string> {.filter(Boolean).map(async (id) => {try {- return [id, await sessionWorkspacePath(dataFolder, id)] as const+ return [id, await threadWorkspacePath(dataFolder, id)] as const} catch {return [id, ''] as const}diff --git a/web-app/src/routes/cowork.tsx b/web-app/src/routes/cowork.tsxindex b471c0601..41b11b50b 100644--- a/web-app/src/routes/cowork.tsx+++ b/web-app/src/routes/cowork.tsx@@ -20,7 +20,7 @@ import { Bot, Command, Sparkles } from 'lucide-react'import { AnimatePresence, motion, useReducedMotion } from 'motion/react'import { cn } from '@/lib/utils'import { getLoadedModels } from '@janhq/tauri-plugin-llamacpp-api'-import { sessionWorkspacePath } from '@janhq/tauri-plugin-agent-tools-api'+import { threadWorkspacePath } from '@janhq/tauri-plugin-agent-tools-api'import { getModelDisplayName, getProviderTitle } from '@/lib/utils'import { predefinedProviders } from '@/constants/providers'import { providerHasRemoteApiKeys } from '@/lib/provider-api-keys'@@ -281,7 +281,7 @@ function CoworkPage() {try {const dataFolder = await serviceHub.app().getJanDataFolder()if (!dataFolder) return- const path = await sessionWorkspacePath(dataFolder, session.id)+ const path = await threadWorkspacePath(dataFolder, session.id)if (alive) setWorkspacePath(path)} catch {// A missing path only costs the pill a subtitle.