Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

v2.5.0 B15: multi-repo agent context (Sprint C #8)

purpleebc966fad21a
diff --git a/src/api/__tests__/agent-context-workspace.test.ts b/src/api/__tests__/agent-context-workspace.test.ts
new file mode 100644
index 00000000..883270e3
--- /dev/null
+++ b/src/api/__tests__/agent-context-workspace.test.ts
@@ -0,0 +1,112 @@
+import { describe, it, expect, beforeEach } from 'vitest'
+import {
+ setActiveChatId,
+ setActiveWorkspace,
+ getActiveWorkspace,
+ clearActiveChatId,
+ renderWorkspaceSection,
+} from '../agent-context'
+
+describe('agent-context — active workspace pointer', () => {
+ beforeEach(() => {
+ clearActiveChatId()
+ })
+
+ it('starts null', () => {
+ expect(getActiveWorkspace()).toBeNull()
+ })
+
+ it('stores a folder workspace', () => {
+ setActiveWorkspace({ kind: 'folder', path: '/Users/me/repo' })
+ expect(getActiveWorkspace()).toEqual({
+ kind: 'folder',
+ path: '/Users/me/repo',
+ })
+ })
+
+ it('sandbox workspaces leave the pointer null', () => {
+ // Tools fall back to the bridge's per-chat sandbox path; we don't want
+ // chatCtx() to send workingDirectory in that case.
+ setActiveWorkspace({ kind: 'sandbox' })
+ expect(getActiveWorkspace()).toBeNull()
+ })
+
+ it('folder workspace without a path is treated like sandbox', () => {
+ setActiveWorkspace({ kind: 'folder' })
+ expect(getActiveWorkspace()).toBeNull()
+ })
+
+ it('null / undefined explicitly clear the pointer', () => {
+ setActiveWorkspace({ kind: 'folder', path: '/a' })
+ setActiveWorkspace(null)
+ expect(getActiveWorkspace()).toBeNull()
+ setActiveWorkspace({ kind: 'folder', path: '/b' })
+ setActiveWorkspace(undefined)
+ expect(getActiveWorkspace()).toBeNull()
+ })
+
+ it('clearActiveChatId drops the workspace too', () => {
+ setActiveChatId('chat-1')
+ setActiveWorkspace({ kind: 'folder', path: '/x' })
+ clearActiveChatId()
+ expect(getActiveWorkspace()).toBeNull()
+ })
+
+ // ── multi-repo extras (Sprint C #8) ──────────────────────────────
+
+ it('keeps extraPaths when they are non-empty distinct strings', () => {
+ setActiveWorkspace({
+ kind: 'folder',
+ path: '/Users/me/repo-a',
+ extraPaths: ['/Users/me/repo-b', '/Users/me/repo-c'],
+ })
+ expect(getActiveWorkspace()).toEqual({
+ kind: 'folder',
+ path: '/Users/me/repo-a',
+ extraPaths: ['/Users/me/repo-b', '/Users/me/repo-c'],
+ })
+ })
+
+ it('dedupes extras and removes blanks / primary collisions', () => {
+ setActiveWorkspace({
+ kind: 'folder',
+ path: '/a',
+ extraPaths: ['/b', '/b', '', '/a', '/c'],
+ })
+ expect(getActiveWorkspace()).toEqual({
+ kind: 'folder',
+ path: '/a',
+ extraPaths: ['/b', '/c'],
+ })
+ })
+
+ it('drops the extras field entirely when nothing survives the filter', () => {
+ setActiveWorkspace({ kind: 'folder', path: '/a', extraPaths: ['', '/a'] })
+ expect(getActiveWorkspace()).toEqual({ kind: 'folder', path: '/a' })
+ })
+})
+
+describe('renderWorkspaceSection', () => {
+ it('returns empty for sandbox / missing path', () => {
+ expect(renderWorkspaceSection(null)).toBe('')
+ expect(renderWorkspaceSection({ kind: 'sandbox' })).toBe('')
+ expect(renderWorkspaceSection({ kind: 'folder' })).toBe('')
+ })
+
+ it('returns a single-line primary when there are no extras', () => {
+ const out = renderWorkspaceSection({ kind: 'folder', path: '/a' })
+ expect(out).toBe('\n\nPrimary workspace: /a')
+ })
+
+ it('lists primary + extras when extras are present', () => {
+ const out = renderWorkspaceSection({
+ kind: 'folder',
+ path: '/a',
+ extraPaths: ['/b', '/c'],
+ })
+ expect(out).toMatch(/Workspaces/)
+ expect(out).toMatch(/Primary:\s+\/a/)
+ expect(out).toMatch(/Extra:\s+\/b/)
+ expect(out).toMatch(/Extra:\s+\/c/)
+ })
+})
diff --git a/src/api/agent-context.ts b/src/api/agent-context.ts
index 2eddbd27..826e9536 100644
--- a/src/api/agent-context.ts
+++ b/src/api/agent-context.ts
@@ -19,7 +19,10 @@
* legacy shared folder.
*/
+import type { AgentWorkspace } from '../types/agent-workspace'
+
let activeChatId: string | null = null
+let activeWorkspace: AgentWorkspace | null = null
export function setActiveChatId(id: string | null | undefined): void {
activeChatId = id ? String(id) : null
@@ -31,6 +34,73 @@ export function getActiveChatId(): string | null {
export function clearActiveChatId(): void {
activeChatId = null
+ activeWorkspace = null
+}
+
+// ── Multi-Repo Agent (Sprint C #8 from uselu) ──────────────────
+//
+// When the agent loop runs in a 'folder' workspace (vs the per-chat
+// sandbox), the bridge resolves relative paths against `ws.path` and
+// the system prompt advertises any additional repo paths in `extraPaths`
+// so the model can address them by absolute path. Use case: "sync the
+// API in repo-A with the client in repo-B" — primary = repo-A,
+// extras = [repo-B].
+
+/**
+ * Pin the active workspace for the current agent loop. Called by
+ * useAgentChat / useCodex right after setActiveChatId. Sandbox mode
+ * passes null so the bridge falls back to ~/agent-workspace/<slug>/.
+ */
+export function setActiveWorkspace(ws: AgentWorkspace | null | undefined): void {
+ if (ws && ws.kind === 'folder' && ws.path) {
+ // Defensive: filter out blanks + dedupe extras + drop the primary if
+ // a caller accidentally listed it as both. Keeps the public shape
+ // stable for downstream readers (system prompt + chatCtx).
+ const cleanedExtras = Array.isArray(ws.extraPaths)
+ ? Array.from(
+ new Set(
+ ws.extraPaths
+ .filter((p): p is string => typeof p === 'string' && p.length > 0)
+ .filter((p) => p !== ws.path),
+ ),
+ )
+ : []
+ activeWorkspace = {
+ kind: 'folder',
+ path: ws.path,
+ extraPaths: cleanedExtras.length > 0 ? cleanedExtras : undefined,
+ }
+ } else {
+ // Sandbox (or unset) → leave pointer null so the bridge falls back
+ // to its own per-chat sandbox path. Setting it to { kind: 'sandbox' }
+ // would just duplicate state the bridge already owns.
+ activeWorkspace = null
+ }
+}
+
+export function getActiveWorkspace(): AgentWorkspace | null {
+ return activeWorkspace
+}
+
+/**
+ * Render the workspace section appended to the agent / Codex system
+ * prompt. Empty string when the loop is in sandbox mode (the bridge
+ * already knows its own sandbox path — no need to tell the model).
+ */
+export function renderWorkspaceSection(ws: AgentWorkspace | null): string {
+ if (!ws || ws.kind !== 'folder' || !ws.path) return ''
+ const extras = ws.extraPaths ?? []
+ if (extras.length === 0) {
+ return `\n\nPrimary workspace: ${ws.path}`
+ }
+ const lines = [
+ '',
+ '',
+ 'Workspaces (relative paths resolve against the primary; address extras by absolute path):',
+ `- Primary: ${ws.path}`,
+ ...extras.map((p) => `- Extra: ${p}`),
+ ]
+ return lines.join('\n')
}
/**