feat(runtime): default to passthrough and persist execution permissions
This commit is contained in:
+27
-22
@@ -1,3 +1,4 @@
|
||||
import { unrestricted, ceiling } from '../shared/contracts/execution-policy.mjs';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { assertChangeSetsResolved } from '../shared/contracts/change-sets.mjs';
|
||||
@@ -42,8 +43,8 @@ function protectedIndex(stageText, flagText, paths) {
|
||||
flags: flagText.split('\0').filter(line => line && !chosen.has(line.slice(2))).sort(),
|
||||
}));
|
||||
}
|
||||
function validMessage(value) {
|
||||
if (typeof value !== 'string' || !value.trim() || value.length > 1600 || value.includes('\0')
|
||||
function validMessage(value, config) {
|
||||
if (typeof value !== 'string' || !value.trim() || value.length > ceiling(config, 1600) || value.includes('\0')
|
||||
|| /^Workbench-(?:Delivery|Task|Session):/im.test(value)) throw new Error('提交说明需要1到1600个字符,不能包含工作台保留的trailer。');
|
||||
return value.replaceAll('\r\n', '\n').trim();
|
||||
}
|
||||
@@ -118,13 +119,13 @@ export class GitDelivery {
|
||||
if (!oid(headId) || !ref.ok || !/^refs\/heads\/[^\s\0]+$/.test(reference)) throw conflict('当前交付切片需要已有提交的命名分支;请先用Git建立基线分支。');
|
||||
const gitDir = fs.realpathSync.native(gitDirectory.text.trim()), indexPath = path.resolve(index.text.trim());
|
||||
if (normalize(path.dirname(indexPath)) !== normalize(gitDir) || !fs.existsSync(indexPath) || fs.lstatSync(indexPath).isSymbolicLink()
|
||||
|| fs.statSync(indexPath).size > 16 * 1024 * 1024) throw conflict('当前Git索引位置、类型或大小未被交付器支持。');
|
||||
|| fs.statSync(indexPath).size > ceiling(this.store, 16 * 1024 * 1024)) throw conflict('当前Git索引位置、类型或大小未被交付器支持。');
|
||||
for (const name of ['MERGE_HEAD', 'CHERRY_PICK_HEAD', 'REVERT_HEAD', 'REBASE_HEAD', 'rebase-apply', 'rebase-merge', 'sequencer', 'BISECT_START']) {
|
||||
if (fs.existsSync(path.join(gitDir, name))) throw conflict('Git正在合并、变基或定位问题;先完成该操作再交付。');
|
||||
}
|
||||
if (!allowOwnLock && fs.existsSync(indexPath + '.lock')) throw conflict('Git索引正在被其他操作锁定,请先核对该操作。');
|
||||
const paths = [...new Set(inventory.text.split('\0').filter(Boolean))];
|
||||
if (paths.length > 1000) throw conflict('工程超过当前1000文件快照上限,请缩小工程范围后交付。');
|
||||
if (!unrestricted(this.store) && paths.length > 1000) throw conflict('工程超过当前1000文件快照上限,请缩小工程范围后交付。');
|
||||
const attributes = await this.needGit(project, ['check-attr', '-z', 'filter', 'working-tree-encoding', '--stdin'], { input: paths.join('\0') + (paths.length ? '\0' : '') });
|
||||
const attributeParts = attributes.text.split('\0').filter(Boolean);
|
||||
for (let i = 0; i < attributeParts.length; i += 3) if (!['unspecified', 'unset'].includes(attributeParts[i + 2])) {
|
||||
@@ -165,7 +166,7 @@ export class GitDelivery {
|
||||
if (!selected.has(relative)) selected.set(relative, []);
|
||||
selected.get(relative).push(change);
|
||||
}
|
||||
if (!selected.size || selected.size > 32) throw conflict('当前任务需要1到32个已应用且归属明确的文件;旧版未记录Task归属的提案需要重新审查。');
|
||||
if (!selected.size || selected.size > ceiling(this.store, 32)) throw conflict('当前任务需要已应用且归属明确的文件。');
|
||||
const stageEntries = entries(snapshot.stage), result = [];
|
||||
for (const [relative, chain] of selected) {
|
||||
const latest = chain.at(-1), bytes = fs.readFileSync(safePath(project, relative));
|
||||
@@ -199,21 +200,21 @@ export class GitDelivery {
|
||||
deletions: parts.filter(part => part.removed).reduce((sum, part) => sum + part.count, 0), diff: this.redact(diff) });
|
||||
}
|
||||
if (!result.length) throw conflict('当前任务的文件内容已在Git HEAD中,没有新的交付改动。');
|
||||
if (Buffer.byteLength(result.map(file => file.diff).join('\n'), 'utf8') > 1024 * 1024) throw conflict('提交差异超过1MB预览上限,请缩小本次任务。');
|
||||
if (!unrestricted(this.store) && Buffer.byteLength(result.map(file => file.diff).join('\n'), 'utf8') > 1024 * 1024) throw conflict('提交差异超过1MB预览上限,请缩小本次任务。');
|
||||
return result.sort((a, b) => a.path.localeCompare(b.path));
|
||||
}
|
||||
|
||||
async plan(session, options = {}, { source = 'user' } = {}) {
|
||||
if (session.fileDrafts?.some(row => row.taskId === session.taskId && row.status === 'staging')) throw conflict('分段文件草稿尚未完成,请完成提案或显式放弃后再交付。');
|
||||
assertWorkersResolved(this.store, session.projectId);
|
||||
if (workerForProject(this.store, session.projectId)) throw conflict('受限子任务不能独立Git提交;请在父任务审查合并后交付。');
|
||||
if (!unrestricted(this.store) && workerForProject(this.store, session.projectId)) throw conflict('受限子任务不能独立Git提交;请在父任务审查合并后交付。');
|
||||
const idleOptions = { exceptSessionId: source === 'agent' ? session.id : null };
|
||||
this.assertIdle(session, idleOptions); const { task, run } = this.assertTask(session);
|
||||
const identity = { taskId: task.id, runId: run.id }, project = this.store.project(session.projectId);
|
||||
const snapshot = await this.snapshot(project), inspection = await inspectProject(project);
|
||||
const check = verifiedCheck(this.store, session, inspection);
|
||||
if (!check) {
|
||||
const previous = this.store.core.check_job.get(task.verification.checkJobId);
|
||||
if (!check && !unrestricted(this.store)) {
|
||||
const previous = this.store.core.check_job.get(task.verification?.checkJobId);
|
||||
if (previous?.status === 'passed' && task.verification.status === 'passed') {
|
||||
previous.stale = true; previous.staleReason = 'Git交付核对发现源码或检查日志证据失效。';
|
||||
task.verification = { ...task.verification, status: 'stale', reason: previous.staleReason };
|
||||
@@ -221,30 +222,33 @@ export class GitDelivery {
|
||||
}
|
||||
throw conflict('提交需要与当前源码一致、日志完整且测试数量明确的通过记录;请先执行工程检查。');
|
||||
}
|
||||
if (!check.sourceFingerprint) {
|
||||
if (check && !check.sourceFingerprint) {
|
||||
check.sourceFingerprint = inspection.sourceFingerprint;
|
||||
task.verification = { ...task.verification, sourceFingerprint: inspection.sourceFingerprint };
|
||||
}
|
||||
assertBrowserResolved(this.store, session.projectId);
|
||||
const browser = await currentBrowserVerification(this.store, session, inspection);
|
||||
if (browser.required && browser.status !== 'passed') throw conflict('本任务已配置页面验收,请先取得当前源码与构建的实际浏览器断言通过记录。');
|
||||
const taskPlan = await new TaskPlanManager(this.store).assertReady(session, { inspection });
|
||||
const requirements = await new RequirementReview(this.store).assertReady(session, { inspection });
|
||||
if (!unrestricted(this.store) && browser.required && browser.status !== 'passed') throw conflict('本任务已配置页面验收,请先取得当前源码与构建的实际浏览器断言通过记录。');
|
||||
const taskPlan = unrestricted(this.store) ? {} : await new TaskPlanManager(this.store).assertReady(session, { inspection });
|
||||
const requirements = unrestricted(this.store) ? {} : await new RequirementReview(this.store).assertReady(session, { inspection });
|
||||
const files = await this.selectedFiles(session, project, snapshot);
|
||||
const name = options.authorName ?? snapshot.config['user.name'] ?? 'Agent Workbench';
|
||||
const email = options.authorEmail ?? snapshot.config['user.email'] ?? 'local@agent-workbench.invalid';
|
||||
const author = { ...validAuthor(name, email), source: options.authorName || options.authorEmail ? 'explicit' : snapshot.config['user.name'] && snapshot.config['user.email'] ? 'git-config' : 'workbench-local' };
|
||||
const message = validMessage(options.message ?? '完成:' + task.goal.replace(/\s+/g, ' ').slice(0, 90));
|
||||
const message = validMessage(options.message ?? '完成:' + task.goal.replace(/\s+/g, ' ').slice(0, 90), this.store.config);
|
||||
const second = await this.snapshot(project);
|
||||
if (snapshot.digest !== second.digest || identity.taskId !== session.taskId || identity.runId !== session.runId) throw conflict('准备提交期间工程或任务已变化,请重新预览。');
|
||||
this.assertIdle(session, idleOptions); this.assertTask(session);
|
||||
await new TaskPlanManager(this.store).assertReady(session, { expectedBinding: taskPlan.binding_sha256 });
|
||||
await new RequirementReview(this.store).assertReady(session, { expectedBinding: requirements.binding_sha256 });
|
||||
if (!unrestricted(this.store)) {
|
||||
await new TaskPlanManager(this.store).assertReady(session, { expectedBinding: taskPlan.binding_sha256 });
|
||||
await new RequirementReview(this.store).assertReady(session, { expectedBinding: requirements.binding_sha256 });
|
||||
}
|
||||
const diffArtifact = this.store.artifactStore.write(session, run, files.map(file => file.diff).join('\n'), { kind: 'delivery-diff', mediaType: 'text/plain' });
|
||||
this.store.core.artifact.set(diffArtifact.id, diffArtifact);
|
||||
const plan = { id: randomUUID(), sessionId: session.id, projectId: session.projectId, taskId: task.id, runId: run.id, status: 'planned', createdAt: at(),
|
||||
message, author, files, paths: files.map(file => file.path), snapshot, preparedBy: source, fingerprint: inspection.fingerprint, sourceFingerprint: inspection.sourceFingerprint,
|
||||
checkJobId: check.id, checkSummary: check.results.map(value => ({ name: value.name, command: value.command, exitCode: value.exitCode, tests: value.tests, logArtifactId: value.logArtifactId })),
|
||||
executionMode: this.store.config.executionMode ?? 'restricted',
|
||||
checkJobId: check?.id ?? null, checkSummary: (check?.results ?? []).map(value => ({ name: value.name, command: value.command, exitCode: value.exitCode, tests: value.tests, logArtifactId: value.logArtifactId })),
|
||||
...(taskPlan.configured ? { taskPlanId: taskPlan.plan_id, taskPlanRevision: taskPlan.revision, taskPlanBindingSha256: taskPlan.binding_sha256 } : {}),
|
||||
...(requirements.configured ? { requirementBindingSha256: requirements.binding_sha256 } : {}),
|
||||
...(browser.required ? { browserJobId: browser.jobId, browserPlanId: browser.planId, browserBuildSha256: browser.buildSha256 } : {}),
|
||||
@@ -274,8 +278,8 @@ export class GitDelivery {
|
||||
return plan;
|
||||
}
|
||||
|
||||
async start(session, planId) {
|
||||
this.assertIdle(session); this.assertTask(session);
|
||||
async start(session, planId, { source = 'user' } = {}) {
|
||||
this.assertIdle(session, { exceptSessionId: source === 'agent' ? session.id : null }); this.assertTask(session);
|
||||
const plan = this.store.owned('delivery_plan', planId, session.id);
|
||||
if (plan.taskId !== session.taskId || plan.runId !== session.runId || plan.status !== 'planned') throw conflict('提交计划已使用或不属于当前Task/Run,请重新预览。');
|
||||
const controller = new AbortController();
|
||||
@@ -299,6 +303,7 @@ export class GitDelivery {
|
||||
}
|
||||
|
||||
async verifyBrowserDelivery(session, plan, inspection) {
|
||||
if (unrestricted(plan)) return;
|
||||
await new RequirementReview(this.store).assertReady(session, { inspection, expectedBinding: plan.requirementBindingSha256 ?? null });
|
||||
await new TaskPlanManager(this.store).assertReady(session, { inspection, expectedBinding: plan.taskPlanBindingSha256 ?? null });
|
||||
assertBrowserResolved(this.store, session.projectId);
|
||||
@@ -315,7 +320,7 @@ export class GitDelivery {
|
||||
const snapshot = await this.snapshot(project), inspection = await inspectProject(project);
|
||||
signal.throwIfAborted();
|
||||
if (session.taskId !== plan.taskId || session.runId !== plan.runId || snapshot.digest !== plan.snapshot.digest || inspection.fingerprint !== plan.fingerprint) throw conflict('提交预览后工程、Git索引或Task/Run已变化,没有执行提交。');
|
||||
if (!verifiedCheck(this.store, session, inspection)) throw conflict('提交前检查证据已失效,请重新运行工程检查。');
|
||||
if (!unrestricted(plan) && !verifiedCheck(this.store, session, inspection)) throw conflict('提交前检查证据已失效,请重新运行工程检查。');
|
||||
await this.verifyBrowserDelivery(session, plan, inspection);
|
||||
const selected = await this.selectedFiles(session, project, snapshot);
|
||||
if (JSON.stringify(selected) !== JSON.stringify(plan.files)) throw conflict('当前文件与已审核提交清单不一致。');
|
||||
@@ -390,7 +395,7 @@ export class GitDelivery {
|
||||
child.stderr.on('data', bytes => { errors = (errors + bytes.toString('utf8')).slice(-8000); });
|
||||
const done = new Promise((resolve, reject) => { child.once('error', reject); child.once('close', code => resolve(code)); });
|
||||
done.catch(() => {});
|
||||
const timer = setTimeout(() => child.kill(), 15000);
|
||||
const timer = unrestricted(plan) ? null : setTimeout(() => child.kill(), 15000);
|
||||
const stop = () => { if (!commitSent && !child.stdin.writableEnded) child.stdin.end('abort\n'); };
|
||||
signal.addEventListener('abort', stop, { once: true });
|
||||
try {
|
||||
@@ -401,7 +406,7 @@ export class GitDelivery {
|
||||
const actualRef = (await this.needGit(project, ['symbolic-ref', '--quiet', 'HEAD'])).text.trim();
|
||||
if (actualRef !== plan.snapshot.reference || digestFile(job.indexPath) !== job.indexBeforeSha256) throw conflict('Git锁定后当前分支或索引与提交计划不同,未发布提交。');
|
||||
const inspection = await inspectProject(project);
|
||||
if (inspection.sourceFingerprint !== plan.sourceFingerprint || !verifiedCheck(this.store, session, inspection)) throw conflict('Git锁定后源码或实际检查已经变化,未发布提交。');
|
||||
if (inspection.sourceFingerprint !== plan.sourceFingerprint || !unrestricted(plan) && !verifiedCheck(this.store, session, inspection)) throw conflict('Git锁定后源码或实际检查已经变化,未发布提交。');
|
||||
await this.verifyBrowserDelivery(session, plan, inspection);
|
||||
await this.checkpoint(session, job, 'ref_prepared');
|
||||
signal.throwIfAborted();
|
||||
|
||||
Reference in New Issue
Block a user