feat: add durable isolated Git workspaces and source-bound delivery
This commit is contained in:
+33
-1
@@ -4,6 +4,8 @@ import { listFiles, readText, proposeFile, proposePatch, applyChange } from './f
|
||||
import { APP_VERSION, executionEnvironment, sessionSnapshot } from './diagnostics.mjs';
|
||||
import { inspectProject } from '../core/project-inspector.mjs';
|
||||
import { assertProposalUnsealed } from '../shared/contracts/change-sets.mjs';
|
||||
import { assertWorkspacesResolved } from '../shared/contracts/workspaces.mjs';
|
||||
import { verifyWorkspaceProject } from '../core/workspaces/git-workspaces.mjs';
|
||||
|
||||
export function createSessionPublisher(clients) {
|
||||
return session => {
|
||||
@@ -121,6 +123,7 @@ return async (request, response) => {
|
||||
const projectMatch = route.match(/^\/api\/projects\/([a-f0-9-]+)\/(files|file|inspection|code-index|code-query)$/);
|
||||
if (method === 'GET' && projectMatch) {
|
||||
const project = store.project(projectMatch[1]);
|
||||
await verifyWorkspaceProject(store, project);
|
||||
if (projectMatch[2] === 'inspection') return json(response, await inspectProject(project));
|
||||
if (['code-index', 'code-query'].includes(projectMatch[2])) {
|
||||
const indexer = runtime.projectIndexer(), index = await indexer.refresh(project);
|
||||
@@ -134,6 +137,7 @@ return async (request, response) => {
|
||||
if (method === 'GET' && route === '/api/sessions') return json(response, store.listSessions());
|
||||
if (method === 'POST' && route === '/api/sessions') {
|
||||
const input = await body(request);
|
||||
await verifyWorkspaceProject(store, store.project(input.projectId));
|
||||
return json(response, store.publicSession(store.createSession(input.projectId)), 201);
|
||||
}
|
||||
const sessionMatch = route.match(/^\/api\/sessions\/([a-f0-9-]+)(?:\/(.*))?$/);
|
||||
@@ -142,6 +146,30 @@ return async (request, response) => {
|
||||
const session = store.session(id);
|
||||
if (method === 'GET' && !action) return json(response, store.publicSession(session));
|
||||
if (method === 'GET' && action === 'execution') return json(response, store.executionSnapshot(session));
|
||||
if (method === 'GET' && action === 'workspaces') return json(response, store.executionSnapshot(session).workspaces);
|
||||
if (method === 'POST' && action === 'workspaces') {
|
||||
const input = await body(request);
|
||||
return json(response, await runtime.workspaceManager().prepare(session, input.label), 201);
|
||||
}
|
||||
const workspace = action.match(/^workspaces\/([a-f0-9-]+)(?:\/(create|reconcile|acknowledge|discard|open|stop))?$/);
|
||||
if (workspace && method === 'GET' && !workspace[2]) return json(response, store.owned('workspace', workspace[1], id));
|
||||
if (workspace && method === 'POST' && workspace[2]) {
|
||||
const input = await body(request), record = store.owned('workspace', workspace[1], id);
|
||||
if (workspace[2] === 'open') {
|
||||
if (record.status !== 'ready') throw Object.assign(new Error('工作区尚未完整确认,请先核对创建结果。'), { status: 409 });
|
||||
const project = store.project(record.childProjectId), child = store.session(record.childSessionId);
|
||||
await verifyWorkspaceProject(store, project);
|
||||
if (child.projectId !== project.id || child.archived) throw Object.assign(new Error('原工作区会话已归档或归属变化,请从项目栏打开该项目的当前会话。'), { status: 409 });
|
||||
return json(response, { workspace: record, project, session: store.publicSession(child) });
|
||||
}
|
||||
const manager = runtime.workspaceManager();
|
||||
if (workspace[2] === 'create') await manager.start(session, record.id, input.fingerprint);
|
||||
else if (workspace[2] === 'reconcile') await manager.reconcile(session, record.id, input.fingerprint);
|
||||
else if (workspace[2] === 'acknowledge') await manager.acknowledge(session, record.id, input.fingerprint, input.observationFingerprint);
|
||||
else if (workspace[2] === 'discard') manager.discard(session, record.id, input.fingerprint);
|
||||
else await manager.stop(id);
|
||||
return json(response, record, workspace[2] === 'create' ? 202 : 200);
|
||||
}
|
||||
if (method === 'GET' && action === 'context-plans') {
|
||||
const runId = url.searchParams.get('run'); if (runId) store.owned('run', runId, id);
|
||||
return json(response, [...store.core.context_plan.values()].filter(plan => plan.sessionId === id && (!runId || plan.runId === runId)).slice(-100));
|
||||
@@ -261,6 +289,8 @@ return async (request, response) => {
|
||||
if (method === 'POST' && change) {
|
||||
await body(request);
|
||||
if (runtime.projectHasActivity(session.projectId)) throw Object.assign(new Error('项目正在执行任务或检查,请等待结束或停止后再审查文件改动。'), { status: 409 });
|
||||
assertWorkspacesResolved(store, session.projectId, { includePrepared: true });
|
||||
await verifyWorkspaceProject(store, store.project(session.projectId));
|
||||
if (change[2] === 'apply') applyChange(store, session, change[1]);
|
||||
else {
|
||||
const item = session.changes.find(candidate => candidate.id === change[1]);
|
||||
@@ -274,7 +304,9 @@ return async (request, response) => {
|
||||
}
|
||||
if (method === 'POST' && ['proposals', 'patch-proposals'].includes(action)) {
|
||||
const input = await body(request);
|
||||
if (runtime.checks?.projectActive(session.projectId) || runtime.deliveries?.projectActive(session.projectId) || runtime.changeSets?.projectActive(session.projectId)) throw Object.assign(new Error('工程检查、Git交付或文件批次正在执行,请等待结束后再编辑文件。'), { status: 409 });
|
||||
if (runtime.checks?.projectActive(session.projectId) || runtime.deliveries?.projectActive(session.projectId) || runtime.changeSets?.projectActive(session.projectId) || runtime.workspaces?.projectActive(session.projectId)) throw Object.assign(new Error('工程检查、Git交付、文件批次或工作区正在执行,请等待结束后再编辑文件。'), { status: 409 });
|
||||
assertWorkspacesResolved(store, session.projectId, { includePrepared: true });
|
||||
await verifyWorkspaceProject(store, store.project(session.projectId));
|
||||
if (action === 'patch-proposals') proposePatch(store, session, input);
|
||||
else proposeFile(store, session, input);
|
||||
return json(response, store.publicSession(session), 201);
|
||||
|
||||
Reference in New Issue
Block a user