mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
loop: terminal-dev-loop completed after 30 iterations
This commit is contained in:
1 parent
a383e57bc5
commit
16b91ccf02
8 files changed
+336
-13
No files matched your search
@@ -2,6 +2,7 @@ import { Hono } from 'hono'
|
||||
import type { MiddlewareHandler } from 'hono'
|
||||
import type { Database } from 'bun:sqlite'
|
||||
import type { UpgradeWebSocket } from 'hono/ws'
|
||||
import { ENV } from '@opencode-manager/shared/config/env'
|
||||
import type { GitAuthService } from '../services/git-auth'
|
||||
import type { OpenCodeClient } from '../services/opencode/client'
|
||||
import type { TerminalService } from '../services/terminal'
|
||||
@@ -16,6 +17,19 @@ export interface RepoTerminalSocketVariables {
|
||||
|
||||
const MAX_CLOSE_REASON_BYTES = 120
|
||||
|
||||
const DEFAULT_TRUSTED_ORIGINS = ENV.AUTH.TRUSTED_ORIGINS.split(',')
|
||||
.map((origin) => origin.trim())
|
||||
.filter(Boolean)
|
||||
|
||||
function isAllowedUpgradeOrigin(
|
||||
origin: string | undefined,
|
||||
trustedOrigins: readonly string[],
|
||||
): boolean {
|
||||
if (origin === undefined) return true
|
||||
if (origin === 'null') return false
|
||||
return trustedOrigins.includes(origin)
|
||||
}
|
||||
|
||||
function parseCursor(raw: string | undefined): number | undefined | null {
|
||||
if (raw === undefined) return undefined
|
||||
if (!/^-?\d+$/.test(raw)) return null
|
||||
@@ -36,11 +50,16 @@ export function createRepoTerminalSocketRoutes(
|
||||
openCodeClient: OpenCodeClient,
|
||||
terminalService: TerminalService,
|
||||
upgradeWebSocket: UpgradeWebSocket,
|
||||
trustedOrigins: readonly string[] = DEFAULT_TRUSTED_ORIGINS,
|
||||
) {
|
||||
const app = new Hono<{ Variables: RepoTerminalSocketVariables }>()
|
||||
const deps = { database, gitAuthService, openCodeClient }
|
||||
|
||||
const validate: MiddlewareHandler<{ Variables: RepoTerminalSocketVariables }> = async (c, next) => {
|
||||
if (!isAllowedUpgradeOrigin(c.req.header('origin'), trustedOrigins)) {
|
||||
return c.json({ error: 'Origin not allowed' }, 403)
|
||||
}
|
||||
|
||||
const cursor = parseCursor(c.req.query('cursor'))
|
||||
if (cursor === null) {
|
||||
return c.json({ error: 'Invalid cursor' }, 400)
|
||||
|
||||
@@ -94,13 +94,10 @@ export function createRepoRoutes(
|
||||
}
|
||||
|
||||
if (repo.isWorktree) {
|
||||
const projectRepo = await projectConfigService.resolveProjectRepo(repo)
|
||||
if (projectRepo.id !== repo.id) {
|
||||
return c.json({
|
||||
...repo,
|
||||
worktreeSetup: await projectConfigService.runWorktreeSetup(projectRepo, repo.fullPath, terminalService),
|
||||
})
|
||||
}
|
||||
return c.json({
|
||||
...repo,
|
||||
worktreeSetup: await projectConfigService.runWorktreeSetupForRepo(repo, repo.fullPath, terminalService),
|
||||
})
|
||||
}
|
||||
|
||||
return c.json(repo)
|
||||
@@ -332,7 +329,7 @@ app.get('/', async (c) => {
|
||||
const worktree = await openCodeClient.api.worktree.create({ projectID })
|
||||
return c.json({
|
||||
...worktree,
|
||||
worktreeSetup: await projectConfigService.runWorktreeSetup(repo, worktree.directory, terminalService),
|
||||
worktreeSetup: await projectConfigService.runWorktreeSetupForRepo(repo, worktree.directory, terminalService),
|
||||
})
|
||||
} catch (error: unknown) {
|
||||
if (isWorktreeError(error)) {
|
||||
|
||||
@@ -265,6 +265,20 @@ export class ProjectConfigService {
|
||||
}
|
||||
}
|
||||
|
||||
async runWorktreeSetupForRepo(
|
||||
repo: Repo,
|
||||
worktreeDirectory: string,
|
||||
terminalService: TerminalService,
|
||||
): Promise<WorktreeSetupResult> {
|
||||
try {
|
||||
const projectRepo = await this.resolveProjectRepo(repo)
|
||||
return await this.runWorktreeSetup(projectRepo, worktreeDirectory, terminalService)
|
||||
} catch (error: unknown) {
|
||||
logger.warn(`Failed to prepare worktree setup for ${worktreeDirectory}:`, error)
|
||||
return { status: 'failed', error: getErrorMessage(error) }
|
||||
}
|
||||
}
|
||||
|
||||
private isRepoFileTrusted(projectRepo: Repo, repoFile: RepoFileState): boolean {
|
||||
return (
|
||||
repoFile.hash !== null &&
|
||||
@@ -340,7 +354,7 @@ export class ProjectConfigService {
|
||||
return {
|
||||
repoFile: nextFile,
|
||||
personalActions: personal.filter((item) => item.id !== action.id),
|
||||
trustHash: this.hashRepoFile(nextFile),
|
||||
trustHash: this.resolveTrustAfterMove(projectRepo, state, this.hashRepoFile(nextFile)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -356,7 +370,11 @@ export class ProjectConfigService {
|
||||
const nextPersonal = personal.filter((_, itemIndex) => itemIndex !== index)
|
||||
this.assertRepoFileValid(nextFile)
|
||||
this.assertPersonalSetupValid(nextPersonal)
|
||||
return { repoFile: nextFile, personalSetup: nextPersonal, trustHash: this.hashRepoFile(nextFile) }
|
||||
return {
|
||||
repoFile: nextFile,
|
||||
personalSetup: nextPersonal,
|
||||
trustHash: this.resolveTrustAfterMove(projectRepo, state, this.hashRepoFile(nextFile)),
|
||||
}
|
||||
}
|
||||
|
||||
private planMoveToPersonal(projectRepo: Repo, state: RepoFileState, request: MoveProjectItemRequest): MoveCommit {
|
||||
@@ -377,7 +395,11 @@ export class ProjectConfigService {
|
||||
})
|
||||
const personalActions = [...this.getPersonalActions(projectRepo), action]
|
||||
return nextFile
|
||||
? { repoFile: nextFile, personalActions, trustHash: this.hashRepoFile(nextFile) }
|
||||
? {
|
||||
repoFile: nextFile,
|
||||
personalActions,
|
||||
trustHash: this.resolveTrustAfterMove(projectRepo, state, this.hashRepoFile(nextFile)),
|
||||
}
|
||||
: { repoFile: null, personalActions, trustHash: null }
|
||||
}
|
||||
|
||||
@@ -393,10 +415,33 @@ export class ProjectConfigService {
|
||||
const personalSetup = [...this.getPersonalSetup(projectRepo), request.command]
|
||||
this.assertPersonalSetupValid(personalSetup)
|
||||
return nextFile
|
||||
? { repoFile: nextFile, personalSetup, trustHash: this.hashRepoFile(nextFile) }
|
||||
? {
|
||||
repoFile: nextFile,
|
||||
personalSetup,
|
||||
trustHash: this.resolveTrustAfterMove(projectRepo, state, this.hashRepoFile(nextFile)),
|
||||
}
|
||||
: { repoFile: null, personalSetup, trustHash: null }
|
||||
}
|
||||
|
||||
private resolveTrustAfterMove(projectRepo: Repo, state: RepoFileState, nextHash: string | null): string | null {
|
||||
if (nextHash === null) {
|
||||
return null
|
||||
}
|
||||
if (this.hasUntrustedExecutableContent(projectRepo, state)) {
|
||||
return getRepoSetting(this.database, projectRepo.id, REPO_CONFIG_TRUST_KEY)
|
||||
}
|
||||
return nextHash
|
||||
}
|
||||
|
||||
private hasUntrustedExecutableContent(projectRepo: Repo, state: RepoFileState): boolean {
|
||||
if (!state.exists || !state.file) {
|
||||
return false
|
||||
}
|
||||
const hasExecutable =
|
||||
(state.file.projectActions?.length ?? 0) > 0 || (state.file.setupWorktree?.length ?? 0) > 0
|
||||
return hasExecutable && !this.isRepoFileTrusted(projectRepo, state)
|
||||
}
|
||||
|
||||
private pruneRepoFile(file: RepoProjectFile): RepoProjectFile | null {
|
||||
const next: RepoProjectFile = { ...file }
|
||||
if (next.projectActions && next.projectActions.length === 0) {
|
||||
|
||||
Reference in new issue
Block a user