feat: execute two owned workers in isolated Git workspaces with frozen scopes
This commit is contained in:
@@ -7,6 +7,7 @@ import { assertChangeSetsResolved, CHANGE_SET_OPEN } from '../../shared/contract
|
||||
import { assertWorkspacesResolved } from '../../shared/contracts/workspaces.mjs';
|
||||
import { verifyWorkspaceProject } from './git-workspaces.mjs';
|
||||
import { assertMcpResolved } from '../tools/mcp-contract.mjs';
|
||||
import { assertWorkersResolved, assertWorkerWriteScope } from '../../shared/contracts/workers.mjs';
|
||||
|
||||
const at = () => new Date().toISOString();
|
||||
const conflict = message => Object.assign(new Error(message), { status: 409 });
|
||||
@@ -81,6 +82,7 @@ export class ChangeSetManager {
|
||||
}
|
||||
save(session, set, phase) { this.store.save(session, { type: 'change_set.' + phase, detail: { changeSetId: set.id } }); }
|
||||
requireIdle(session, { source = 'user', exceptId = null, includePrepared = false } = {}) {
|
||||
assertWorkersResolved(this.store, session.projectId);
|
||||
assertMcpResolved(this.store, session.projectId);
|
||||
if (this.projectActive(session.projectId) || this.projectBusy(session.projectId, source === 'agent' ? session.id : null)) {
|
||||
throw conflict('同一项目正在执行任务、检查、提交或文件批次,请等待结束后再操作。');
|
||||
@@ -99,6 +101,7 @@ export class ChangeSetManager {
|
||||
if (set.root !== project.root || hash(Buffer.from(JSON.stringify(sealed(set)))) !== set.fingerprint) throw conflict('变更集封存信息校验失败,请核对原批次。');
|
||||
validatePaths(project, set.files);
|
||||
return set.files.map(file => {
|
||||
assertWorkerWriteScope(this.store, session, file.path, { readOnly: true });
|
||||
const change = session.changes.find(value => value.id === file.changeId);
|
||||
if (!change || change.taskId !== set.taskId || change.runId !== file.sourceRunId || targetFor(project, change.path).path !== file.path
|
||||
|| change.baseExists !== file.baseExists || change.baseSha256 !== file.baseSha256 || change.sha256 !== file.sha256
|
||||
@@ -141,6 +144,7 @@ export class ChangeSetManager {
|
||||
if (changes.some(value => !value || value.taskId !== task.id || this.store.core.run.get(value.runId)?.taskId !== task.id || value.status !== 'pending'
|
||||
|| value.changeSetId && CHANGE_SET_OPEN.has(this.store.core.change_set.get(value.changeSetId)?.status))) throw conflict('变更集只能封存当前Task的待审查提案。');
|
||||
const project = this.store.project(session.projectId);
|
||||
for (const change of changes) assertWorkerWriteScope(this.store, session, change.path);
|
||||
validatePaths(project, changes);
|
||||
const temporaryId = randomUUID(); this.projects.set(project.id, temporaryId);
|
||||
try {
|
||||
|
||||
@@ -7,6 +7,8 @@ import { safePath, excludedFromInventory } from '../../server/files.mjs';
|
||||
import { assertChangeSetsResolved } from '../../shared/contracts/change-sets.mjs';
|
||||
import { assertWorkspacesResolved, assertWorkspacePath, WORKSPACE_ACTIVE } from '../../shared/contracts/workspaces.mjs';
|
||||
import { assertMcpResolved } from '../tools/mcp-contract.mjs';
|
||||
import { isId } from '../../shared/contracts/execution.mjs';
|
||||
import { assertWorkersResolved, workerForProject } from '../../shared/contracts/workers.mjs';
|
||||
|
||||
const at = () => new Date().toISOString();
|
||||
const hash = value => createHash('sha256').update(typeof value === 'string' || Buffer.isBuffer(value) ? value : JSON.stringify(value)).digest('hex');
|
||||
@@ -209,19 +211,25 @@ export class WorkspaceManager {
|
||||
return { ...snapshot, fingerprint: hash(snapshot) };
|
||||
}
|
||||
|
||||
prepare(session, label) { return this.track(session, () => this.prepareOwned(session, label)); }
|
||||
async prepareOwned(session, label) {
|
||||
prepare(session, label, identities = null) { return this.track(session, () => this.prepareOwned(session, label, identities)); }
|
||||
async prepareOwned(session, label, identities = null) {
|
||||
assertWorkersResolved(this.store, session.projectId);
|
||||
if (workerForProject(this.store, session.projectId)) throw conflict('受限子任务不能创建嵌套工作区。');
|
||||
if (identities && (Object.keys(identities).some(key => !['id', 'childProjectId', 'childSessionId'].includes(key))
|
||||
|| ['id', 'childProjectId', 'childSessionId'].some(key => !isId(identities[key]))
|
||||
|| this.store.core.workspace.has(identities.id) || this.store.projects.some(project => project.id === identities.childProjectId)
|
||||
|| this.store.sessions.has(identities.childSessionId))) throw conflict('预分配的隔离工作区身份无效或已使用。');
|
||||
if (typeof label !== 'string' || !label.trim() || label.length > 64 || /[\x00-\x1f]/.test(label)) throw new Error('工作区名称需要1到64个普通字符。');
|
||||
this.assertIdle(session);
|
||||
if ([...this.store.core.workspace.values()].some(value => value.projectId === session.projectId && value.status === 'prepared')) throw conflict('这个项目已有待确认的工作区计划,请先处理该记录。');
|
||||
this.projects.set(session.projectId, 'preparing');
|
||||
try {
|
||||
const source = await this.snapshot(this.store.project(session.projectId)), id = randomUUID();
|
||||
const source = await this.snapshot(this.store.project(session.projectId)), id = identities?.id ?? randomUUID();
|
||||
const slug = label.trim().toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 32) || 'task';
|
||||
const record = { schemaVersion: 1, id, projectId: session.projectId, sessionId: session.id,
|
||||
taskId: session.taskId ?? null, runId: session.runId ?? null, label: label.trim(), source,
|
||||
root: path.join(this.outputRoot, 'w-' + hash(id).slice(0, 20)), branch: 'workbench/' + slug + '-' + id.slice(0, 12),
|
||||
childProjectId: randomUUID(), childSessionId: randomUUID(), createdAt: at(), updatedAt: at(), finishedAt: null,
|
||||
childProjectId: identities?.childProjectId ?? randomUUID(), childSessionId: identities?.childSessionId ?? randomUUID(), createdAt: at(), updatedAt: at(), finishedAt: null,
|
||||
status: 'prepared', phase: 'prepared', creationAttempted: false, requiresVerification: false, ownerPid: null, gitPid: null,
|
||||
sourcePreserved: null, observation: null, summary: '已封存来源提交和原工作区状态,确认后创建独立Git工作区。' };
|
||||
this.target(record, { absent: true });
|
||||
|
||||
Reference in New Issue
Block a user