mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
fix(ocm): scope the repo proxy to the requested worktree
This commit is contained in:
1 parent
f4d2221d46
commit
f952a99b37
3 files changed
+154
-11
No files matched your search
@@ -341,7 +341,7 @@ app.route('/api/health', createHealthRoutes(db, openCodeSupervisor))
|
||||
|
||||
app.route('/api/mcp-oauth-proxy', createMcpOauthProxyRoutes(openCodeClient, requireAuth))
|
||||
app.route('/api/internal', createInternalRoutes(db, scheduleService, notificationService, settingsService, openCodeClient, sessionPermissionModeService, repoWorkspaces, gitAuthService))
|
||||
app.route('/api/opencode-proxy', createOpenCodeProxyRoutes(db, settingsService))
|
||||
app.route('/api/opencode-proxy', createOpenCodeProxyRoutes(db, settingsService, gitAuthService, openCodeClient))
|
||||
|
||||
const protectedApi = new Hono()
|
||||
protectedApi.use('/*', requireAuth)
|
||||
|
||||
@@ -11,6 +11,11 @@ import {
|
||||
withDefaultOpenCodeDirectory,
|
||||
} from '../services/opencode/upstream'
|
||||
import { getRepoById } from '../db/queries'
|
||||
import { getSiblingRepos, resolveRepoWorkingDirectory } from '../services/repo'
|
||||
import type { GitAuthService } from '../services/git-auth'
|
||||
import type { OpenCodeClient } from '../services/opencode/client'
|
||||
import type { Repo } from '@opencode-manager/shared/types'
|
||||
import type { RepoSibling } from '@opencode-manager/shared/utils'
|
||||
import { buildProxyResponseHeaders, filterProxyHeaders } from '../utils/proxy-headers'
|
||||
|
||||
interface ProxyRequestParts {
|
||||
@@ -22,12 +27,25 @@ interface ProxyRequestParts {
|
||||
|
||||
type ProxyRewrite = (parts: ProxyRequestParts) => void
|
||||
|
||||
type ProxyBodyRewrite = (bodyText: string) => string | undefined
|
||||
type ProxyBodyRewrite = (bodyText: string) => Promise<string | undefined>
|
||||
|
||||
type RepoDirectoryResolver = (directory: string | undefined) => Promise<string>
|
||||
|
||||
const REPO_SIBLINGS_CACHE_TTL_MS = 5_000
|
||||
|
||||
function isJsonContentType(contentType: string | undefined): boolean {
|
||||
return (contentType ?? '').toLowerCase().includes('application/json')
|
||||
}
|
||||
|
||||
function decodeDirectoryHeader(value: string | undefined): string | undefined {
|
||||
if (!value) return undefined
|
||||
try {
|
||||
return decodeURIComponent(value)
|
||||
} catch {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
function rewriteRepoLocation(parts: ProxyRequestParts, directory: string): void {
|
||||
parts.headers[OPENCODE_DIRECTORY_HEADER] = encodeURIComponent(directory)
|
||||
|
||||
@@ -35,12 +53,12 @@ function rewriteRepoLocation(parts: ProxyRequestParts, directory: string): void
|
||||
parts.searchParams.set('location[directory]', directory)
|
||||
}
|
||||
|
||||
if (parts.method === 'GET' && parts.path === '/api/session') {
|
||||
if (parts.method === 'GET' && parts.path === '/api/session' && !parts.searchParams.has('project')) {
|
||||
parts.searchParams.set('directory', directory)
|
||||
}
|
||||
}
|
||||
|
||||
function rewriteRepoLocationBody(bodyText: string, directory: string): string | undefined {
|
||||
async function rewriteRepoLocationBody(bodyText: string, resolveDirectory: RepoDirectoryResolver): Promise<string | undefined> {
|
||||
let parsed: unknown
|
||||
try {
|
||||
parsed = JSON.parse(bodyText)
|
||||
@@ -53,15 +71,41 @@ function rewriteRepoLocationBody(bodyText: string, directory: string): string |
|
||||
const location = (parsed as { location?: unknown }).location
|
||||
if (!location || typeof location !== 'object' || Array.isArray(location)) return undefined
|
||||
|
||||
const requested = (location as { directory?: unknown }).directory
|
||||
const directory = await resolveDirectory(typeof requested === 'string' ? requested : undefined)
|
||||
;(parsed as { location: Record<string, unknown> }).location = { ...location, directory }
|
||||
return JSON.stringify(parsed)
|
||||
}
|
||||
|
||||
export function createOpenCodeProxyRoutes(db: Database, settingsService: SettingsService) {
|
||||
export function createOpenCodeProxyRoutes(
|
||||
db: Database,
|
||||
settingsService: SettingsService,
|
||||
gitAuthService: GitAuthService,
|
||||
openCodeClient: OpenCodeClient,
|
||||
) {
|
||||
const app = new Hono()
|
||||
const repoSiblingsCache = new Map<number, { expiresAt: number; siblings: Promise<RepoSibling[]> }>()
|
||||
|
||||
app.use('/*', createInternalTokenMiddleware(db))
|
||||
|
||||
function loadRepoSiblings(repoId: number): Promise<RepoSibling[]> {
|
||||
const cached = repoSiblingsCache.get(repoId)
|
||||
if (cached && cached.expiresAt > Date.now()) return cached.siblings
|
||||
|
||||
const siblings = getSiblingRepos(db, repoId, gitAuthService.getGitEnvironment(), openCodeClient, { includeBranch: false })
|
||||
.catch(() => {
|
||||
repoSiblingsCache.delete(repoId)
|
||||
return []
|
||||
})
|
||||
repoSiblingsCache.set(repoId, { expiresAt: Date.now() + REPO_SIBLINGS_CACHE_TTL_MS, siblings })
|
||||
return siblings
|
||||
}
|
||||
|
||||
function createRepoDirectoryResolver(repo: Repo): RepoDirectoryResolver {
|
||||
return async (directory) =>
|
||||
(await resolveRepoWorkingDirectory(repo, directory, () => loadRepoSiblings(repo.id))) ?? repo.fullPath
|
||||
}
|
||||
|
||||
async function forwardToOpenCode(
|
||||
c: Context,
|
||||
pathSuffix: string,
|
||||
@@ -105,7 +149,7 @@ export function createOpenCodeProxyRoutes(db: Database, settingsService: Setting
|
||||
|
||||
if (rewriteBody && rawBody !== undefined) {
|
||||
const bodyText = rawBody.byteLength > 0 ? new TextDecoder().decode(rawBody) : undefined
|
||||
const rewrittenBody = bodyText === undefined ? undefined : rewriteBody(bodyText)
|
||||
const rewrittenBody = bodyText === undefined ? undefined : await rewriteBody(bodyText)
|
||||
requestBody = rewrittenBody === undefined ? rawBody : rewrittenBody
|
||||
}
|
||||
|
||||
@@ -141,12 +185,16 @@ export function createOpenCodeProxyRoutes(db: Database, settingsService: Setting
|
||||
|
||||
const url = new URL(c.req.url)
|
||||
const pathSuffix = url.pathname.replace(/^\/api\/opencode-proxy\/repos\/[^/]+/, '') || '/'
|
||||
const resolveDirectory = createRepoDirectoryResolver(repo)
|
||||
const directory = await resolveDirectory(
|
||||
url.searchParams.get('location[directory]') || decodeDirectoryHeader(c.req.header(OPENCODE_DIRECTORY_HEADER)),
|
||||
)
|
||||
|
||||
return forwardToOpenCode(
|
||||
c,
|
||||
pathSuffix,
|
||||
(parts) => rewriteRepoLocation(parts, repo.fullPath),
|
||||
(bodyText) => rewriteRepoLocationBody(bodyText, repo.fullPath),
|
||||
(parts) => rewriteRepoLocation(parts, directory),
|
||||
(bodyText) => rewriteRepoLocationBody(bodyText, resolveDirectory),
|
||||
)
|
||||
})
|
||||
|
||||
|
||||
@@ -12,6 +12,8 @@ import { buildOpenCodeBasicAuth } from '@opencode-manager/shared/opencode'
|
||||
import { getWorkspacePath } from '@opencode-manager/shared/config/env'
|
||||
import { createOpenCodeProxyRoutes } from '../../src/routes/opencode-proxy'
|
||||
import type { SettingsService } from '../../src/services/settings'
|
||||
import type { GitAuthService } from '../../src/services/git-auth'
|
||||
import type { OpenCodeClient } from '../../src/services/opencode/client'
|
||||
import { OpenCodeSupervisor } from '../../src/services/opencode-supervisor'
|
||||
import { resolveOpenCode2Binary, startOpenCodeServe } from '../helpers/opencode-binary'
|
||||
|
||||
@@ -35,6 +37,13 @@ vi.mock('../../src/db/queries', () => ({
|
||||
getRepoById: getRepoByIdMock,
|
||||
}))
|
||||
|
||||
const getSiblingReposMock = vi.hoisted(() => vi.fn().mockResolvedValue([]))
|
||||
|
||||
vi.mock('../../src/services/repo', async (importOriginal) => ({
|
||||
...await importOriginal<typeof import('../../src/services/repo')>(),
|
||||
getSiblingRepos: getSiblingReposMock,
|
||||
}))
|
||||
|
||||
const upstreamBaseUrl = vi.hoisted(() => ({ value: 'http://127.0.0.1:5551' }))
|
||||
|
||||
vi.mock('../../src/services/opencode/upstream', async (importOriginal) => ({
|
||||
@@ -52,6 +61,10 @@ const mockSettingsService = {
|
||||
|
||||
const mockDb = {} as Database
|
||||
|
||||
const mockGitAuthService = { getGitEnvironment: () => ({}) } as unknown as GitAuthService
|
||||
|
||||
const mockOpenCodeClient = {} as OpenCodeClient
|
||||
|
||||
function upstreamOk() {
|
||||
const upstreamFetch = vi.fn().mockResolvedValue(
|
||||
new Response('ok', { status: 200, headers: { 'content-type': 'text/plain' } })
|
||||
@@ -69,7 +82,7 @@ describe('opencode-proxy routes', () => {
|
||||
isLifecycleInitializedMock.mockReturnValue(true)
|
||||
originalFetch = globalThis.fetch
|
||||
app = new Hono()
|
||||
app.route('/api/opencode-proxy', createOpenCodeProxyRoutes(mockDb, mockSettingsService))
|
||||
app.route('/api/opencode-proxy', createOpenCodeProxyRoutes(mockDb, mockSettingsService, mockGitAuthService, mockOpenCodeClient))
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
@@ -771,7 +784,7 @@ describe('opencode-proxy repo-scoped mount', () => {
|
||||
isLifecycleInitializedMock.mockReturnValue(true)
|
||||
originalFetch = globalThis.fetch
|
||||
app = new Hono()
|
||||
app.route('/api/opencode-proxy', createOpenCodeProxyRoutes(mockDb, mockSettingsService))
|
||||
app.route('/api/opencode-proxy', createOpenCodeProxyRoutes(mockDb, mockSettingsService, mockGitAuthService, mockOpenCodeClient))
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
@@ -924,6 +937,88 @@ describe('opencode-proxy repo-scoped mount', () => {
|
||||
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
||||
expect(new TextDecoder().decode(fetchCall[1].body as ArrayBuffer)).toBe('{not json')
|
||||
})
|
||||
|
||||
describe('repo worktrees', () => {
|
||||
const worktreePath = '/srv/worktrees/my-repo-feature'
|
||||
|
||||
beforeEach(() => {
|
||||
getRepoByIdMock.mockReturnValue(readyRepo)
|
||||
getSiblingReposMock.mockResolvedValue([readyRepo, { ...readyRepo, id: -1, fullPath: worktreePath, worktreeSource: 'opencode' }])
|
||||
})
|
||||
|
||||
it('keeps a worktree x-opencode-directory and scopes the session list to it', async () => {
|
||||
const upstreamFetch = upstreamOk()
|
||||
|
||||
const res = await app.request('/api/opencode-proxy/repos/7/api/session?limit=1', {
|
||||
headers: {
|
||||
Authorization: 'Bearer test-internal-token',
|
||||
'x-opencode-directory': encodeURIComponent(worktreePath),
|
||||
},
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
||||
expect((fetchCall[1].headers as Record<string, string>)['x-opencode-directory']).toBe(encodeURIComponent(worktreePath))
|
||||
expect(new URL(fetchCall[0]).searchParams.get('directory')).toBe(worktreePath)
|
||||
})
|
||||
|
||||
it('keeps a worktree location[directory] query value', async () => {
|
||||
const upstreamFetch = upstreamOk()
|
||||
|
||||
const res = await app.request(
|
||||
`/api/opencode-proxy/repos/7/api/agent?location%5Bdirectory%5D=${encodeURIComponent(worktreePath)}`,
|
||||
{ headers: { Authorization: 'Bearer test-internal-token' } }
|
||||
)
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
||||
expect(new URL(fetchCall[0]).searchParams.get('location[directory]')).toBe(worktreePath)
|
||||
expect((fetchCall[1].headers as Record<string, string>)['x-opencode-directory']).toBe(encodeURIComponent(worktreePath))
|
||||
})
|
||||
|
||||
it('keeps a worktree body location.directory', async () => {
|
||||
const upstreamFetch = upstreamOk()
|
||||
|
||||
const res = await app.request('/api/opencode-proxy/repos/7/api/session', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: 'Bearer test-internal-token',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ title: 'x', location: { directory: worktreePath } }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
||||
expect(JSON.parse(fetchCall[1].body as string)).toEqual({ title: 'x', location: { directory: worktreePath } })
|
||||
})
|
||||
|
||||
it('does not add a directory filter to a project-scoped session list', async () => {
|
||||
const upstreamFetch = upstreamOk()
|
||||
|
||||
const res = await app.request('/api/opencode-proxy/repos/7/api/session?project=prj_1&subpath=..%2Fx', {
|
||||
headers: { Authorization: 'Bearer test-internal-token' },
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
||||
expect(new URL(fetchCall[0]).searchParams.has('directory')).toBe(false)
|
||||
})
|
||||
|
||||
it('loads siblings once across requests within the cache window', async () => {
|
||||
upstreamOk()
|
||||
const request = () => app.request('/api/opencode-proxy/repos/7/api/agent', {
|
||||
headers: {
|
||||
Authorization: 'Bearer test-internal-token',
|
||||
'x-opencode-directory': encodeURIComponent(worktreePath),
|
||||
},
|
||||
})
|
||||
|
||||
await Promise.all([request(), request(), request()])
|
||||
|
||||
expect(getSiblingReposMock).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
const SHIPPED_OPENCODE_BIN = resolveOpenCode2Binary()
|
||||
@@ -1036,7 +1131,7 @@ describe.skipIf(SHIPPED_OPENCODE_BIN === null)('opencode-proxy repo-scoped mount
|
||||
getRepoByIdMock.mockReturnValue({ id: 1, fullPath: repoPath, cloneStatus: 'ready' })
|
||||
|
||||
const proxyApp = new Hono()
|
||||
proxyApp.route('/api/opencode-proxy', createOpenCodeProxyRoutes(mockDb, mockSettingsService))
|
||||
proxyApp.route('/api/opencode-proxy', createOpenCodeProxyRoutes(mockDb, mockSettingsService, mockGitAuthService, mockOpenCodeClient))
|
||||
const proxyServer = await new Promise<ReturnType<typeof serve>>((resolve) => {
|
||||
const server = serve({ fetch: proxyApp.fetch, port: 0, hostname: '127.0.0.1' }, () => resolve(server))
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user