fix(extension): restructure DOM observer to prevent false positive triggers (v0.5.10)

This commit is contained in:
Variet Worker
2026-03-24 18:15:05 +09:00
parent 86e5a24a75
commit 101ec20b21
6 changed files with 64 additions and 10 deletions

View File

@@ -27,9 +27,21 @@ export function generateApprovalObserverScript(_port: number): string {
// Searches: main document → iframes (contentDocument) → webview elements → shadow DOMs
function deepFindButtons(patterns){
var results=[];
// 1. Main document buttons
// 1. Prioritize Agent panel
var panel=findPanel();
if(panel){
collectButtons(panel,results,patterns,'panel');
if(results.length>0) return results;
}
// 2. Prioritize VS Code Toasts & Dialogs
var toasts=document.querySelectorAll('.notifications-toasts, .monaco-dialog-box');
for(var t=0;t<toasts.length;t++){
collectButtons(toasts[t],results,patterns,'toast');
}
if(results.length>0) return results;
// 3. Main document fallback
collectButtons(document,results,patterns,'main');
// 2. Iframe traversal (try contentDocument — works if same-origin or webSecurity off)
// 4. Iframe traversal (try contentDocument — works if same-origin or webSecurity off)
var iframes=document.querySelectorAll('iframe');
for(var i=0;i<iframes.length;i++){
try{
@@ -262,11 +274,11 @@ export function generateApprovalObserverScript(_port: number): string {
// Negative/secondary buttons (Deny, Reject, Dismiss) will be collected as siblings.
var PATS=[
{re:/^Run/i, type:'terminal_command'},
{re:/^Accept all$/i, type:'diff_review'},
{re:/^Accept$/i, type:'agent_step'},
{re:/^(?:Always )?Allow/i, type:'permission'},
{re:/^Accept all/i, type:'diff_review'},
{re:/^Accept/i, type:'agent_step'},
{re:/^(?:Always )?Allow/i, type:'permission'},
{re:/^Approve/i, type:'agent_step'},
{re:/^Retry$/i, type:'error_recovery'},
{re:/^Retry/i, type:'error_recovery'},
];
// ALL actionable button patterns (for grouping siblings in same container)
@@ -400,10 +412,25 @@ export function generateApprovalObserverScript(_port: number): string {
txt=txt.replace(/(Alt|Ctrl|Shift|Meta)\+.*/,'').trim();
if(!txt)continue;
var isBodyRoot = (searchRoots[r] === document.body);
var isVSCodeMainWindow = !!document.querySelector('.monaco-workbench');
// Match against patterns
var matchedType=null;
for(var p=0;p<PATS.length;p++){
if(PATS[p].re.test(txt)){matchedType=PATS[p].type;break;}
if(PATS[p].re.test(txt)){
// STRUCTURAL CONSTRAINT: If we are scanning the main VS Code Editor body, reject Agent/Terminal buttons
// to prevent freezing on CodeLens 'Run' or 'Accept' false positives.
if (isVSCodeMainWindow && isBodyRoot && PATS[p].type !== 'diff_review' && PATS[p].type !== 'permission') {
continue;
}
// Prevent duplicates if already scanned via panel root
if (isBodyRoot && panel && panel.contains(b)) {
continue;
}
matchedType=PATS[p].type;
break;
}
}
if(!matchedType)continue;
@@ -618,7 +645,7 @@ export function generateApprovalObserverScript(_port: number): string {
if(!d.action)return;
log('🔔 TRIGGER-CLICK received: action='+d.action);
var approveRe=[/^Run$/i,/^Run /i,/^Accept/i,/^(?:Always )?Allow/i,/^Approve/i,/^Continue$/i,/^Proceed$/i,/^Retry$/i];
var approveRe=[/^Run/i,/^Accept/i,/^Accept all/i,/^(?:Always )?Allow/i,/^Approve/i,/^Continue$/i,/^Proceed$/i,/^Retry$/i];
var rejectRe=[/^Reject/i,/^Cancel$/i,/^Deny$/i,/^Stop$/i,/^Decline$/i,/^Dismiss$/i];
var patterns=(d.action==='approve')?approveRe:rejectRe;
var emoji=(d.action==='approve')?'✅':'❌';