fix: bound Windows Git metadata paths before workspace creation

This commit is contained in:
Agent Workbench Dev
2026-10-30 00:13:52 +08:00
parent bb48f69f25
commit 4c8321a2ee
29 changed files with 224 additions and 55 deletions
+10 -2
View File
@@ -133,6 +133,13 @@ export class WorkspaceManager {
if (fs.readdirSync(this.hooksDirectory).length || !config.isFile() || config.isSymbolicLink() || config.nlink !== 1 || config.size !== 0) throw conflict('工作区执行配置已被外部修改。');
if (!inside(this.outputRoot, record.root) || !same(path.dirname(record.root), this.outputRoot)) throw conflict('工作区目标超出当前执行宿主拥有的目录。');
if (same(record.root, record.source.root) || inside(record.source.root, record.root) || inside(record.root, record.source.root)) throw conflict('隔离工作区与来源目录不能互相嵌套。');
// Git setup_explicit_git_dir reserves 40 bytes below PATH_MAX, even with
// core.longpaths. Keep room for Windows path conversion and Git suffixes.
// Apply this only before creation; existing/unknown workspaces stay readable.
if (absent && process.platform === 'win32'
&& Buffer.byteLength(path.join(record.source.commonDir, 'worktrees', path.basename(record.root)), 'utf8') > 200) {
throw conflict('来源Git管理目录过长,Windows Git无法可靠创建独立工作区;请将来源工程放在较短目录后重新准备。');
}
if (fs.existsSync(record.root)) {
if (absent) throw conflict('目标工作区目录已存在,不能覆盖或复用外部目录。'); if (checkExisting) ordinaryDirectory(record.root);
}
@@ -211,7 +218,7 @@ export class WorkspaceManager {
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, slug + '-' + id), branch: 'workbench/' + slug + '-' + id.slice(0, 12),
root: path.join(this.outputRoot, 'w-' + id), branch: 'workbench/' + slug + '-' + id.slice(0, 12),
childProjectId: randomUUID(), 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工作区。' };
@@ -262,7 +269,8 @@ export class WorkspaceManager {
record.gitResult = { code: result.code, ok: result.ok, timedOut: result.timedOut, aborted: result.aborted,
message: this.redact(result.error).slice(0, 2000) }; record.gitPid = null;
this.save(session, record, 'git_finished');
if (!result.ok) throw conflict('Git创建没有完整确认结果,请核对实际分支和工作区。');
if (!result.ok) throw conflict('Git创建没有完整确认结果(退出码 ' + String(result.code) + '),请核对实际分支和工作区。'
+ (record.gitResult.message ? '\n' + record.gitResult.message : ''));
record.status = 'verifying'; this.save(session, record, 'verifying');
const observation = await this.observe(record);
record.observation = observation;