feat(ai): harden local agent file edit guards

This commit is contained in:
lix-2026
2026-06-07 01:11:54 +08:00
parent 1f25364374
commit b65501bf86
8 changed files with 1445 additions and 60 deletions
@@ -1859,6 +1859,17 @@ export function createSidebarPageAiRuntime(context) {
});
}
function assertPageAiLocalWritePermission(prompt, targetPackage) {
if (currentSourceKind() !== 'local_folder') return;
if (!pageAiLooksLikeBlockEdit(prompt)) return;
var target = pageAiNormalizeArray(targetPackage && targetPackage.targets)[0] || null;
var permission = String(target && target.policy && target.policy.permission || '').trim();
if (permission === 'read_write') return;
var error = new Error('当前本地工作区只读授权,不能让 AI 写入目标文件。请切换到有写权限的工作区或调整授权后重试。');
error.code = 'page_ai_target_readonly';
throw error;
}
async function pageAiTryBlockEditWorkflow(prompt, scopedContext, runTargetSnapshot) {
if (currentSourceKind() === 'local_folder') return false;
if (!pageAiLooksLikeBlockEdit(prompt)) return false;
@@ -1955,14 +1966,15 @@ export function createSidebarPageAiRuntime(context) {
var scopedContext = pageAiScopedPageContext(contextSnapshot);
scopedContext.editorTarget = currentPageAiScopedEditorTarget();
assertPageAiTargetInCurrentWorkspace(scopedContext.editorTarget);
await assertPageAiTargetWritable(scopedContext.editorTarget);
var runTargetSnapshot = pageAiBuildRunTargetSnapshot(scopedContext, prompt);
if (currentSourceKind() === 'local_folder' && !pageAiNormalizeArray(pageUiState.pageAiAllowedRoots).length) {
await pageAiLoadAllowedRoots();
}
await assertPageAiTargetWritable(scopedContext.editorTarget);
var runTargetSnapshot = pageAiBuildRunTargetSnapshot(scopedContext, prompt);
var contextRefs = pageAiBuildContextRefs(scopedContext, runTargetSnapshot);
var allowedRoots = pageAiBuildAllowedRoots();
var agentTargetPackage = pageAiBuildAgentTargetPackage(scopedContext, runTargetSnapshot);
assertPageAiLocalWritePermission(prompt, agentTargetPackage);
if (typeof pageAiEnrichOcrContextRefs === 'function') {
var ocrContext = await pageAiEnrichOcrContextRefs(contextRefs, agentTargetPackage, scopedContext.editorTarget);
contextRefs = ocrContext.contextRefs || contextRefs;