← back to Adsense Fleet Viewer

verification/TK-11352-health/status-candidate.patch

50 lines

diff --git a/tmp/TK-11352-status-before.mjs b/tmp/TK-11352-status-candidate.mjs
index 4ea97e7..553d7ab 100644
--- a/tmp/TK-11352-status-before.mjs
+++ b/tmp/TK-11352-status-candidate.mjs
@@ -20,7 +20,7 @@ const ours = new Set(fs.readdirSync(FLEET).filter(f => f.endsWith('.json'))
 
 function oc(args) { return execSync(`openclaw ${args}`, { encoding: 'utf8', timeout: 30000, stdio: ['ignore', 'pipe', 'ignore'] }); }
 
-let rows = [], ok = false, complete = false, expectedConsoleCount = null;
+let rows = [], ok = false, complete = false, expectedConsoleCount = null, observedCount = 0;
 try {
   if (!/^[A-Za-z0-9_-]+$/.test(TARGET)) throw new Error('ADSENSE_BROWSER_TARGET is required for isolated console reads');
   const st = JSON.parse(oc('browser status --json 2>/dev/null') || '{}');
@@ -28,9 +28,12 @@ try {
     oc(`browser navigate ${JSON.stringify(LIST)} --target-id ${TARGET} >/dev/null 2>&1`);
     execSync('sleep 3');
     // Parse the sites table innerText into {domain, status, adstxt}
-    const fn = "() => { const t=document.body.innerText; const lines=t.split('\\n').map(x=>x.trim()).filter(Boolean); const out=[]; for(let i=0;i<lines.length;i++){ if(/^[a-z0-9.-]+\\.(com|net|org|ai)$/i.test(lines[i])){ const dom=lines[i]; const win=lines.slice(i+1,i+5).join(' | '); out.push({dom, win}); } } const m=t.match(/\\bof\\s+(\\d+)\\b/i); return {rows:out,total:m?Number(m[1]):null}; }";
+    const fn = "() => { const t=document.body.innerText; const lines=t.split('\\n').map(x=>x.trim()).filter(Boolean); const out=[]; for(let i=0;i<lines.length;i++){ if(/^[a-z0-9.-]+\\.(com|net|org|ai)$/i.test(lines[i])){ const dom=lines[i]; const win=lines.slice(i+1,i+5).join(' | '); out.push({dom, win}); } } const m=t.match(/\\b\\d+\\s*[-–]\\s*\\d+\\s+of\\s+(\\d+)\\b/i); return {rows:out,total:m?Number(m[1]):null}; }";
     const raw = oc(`browser evaluate --target-id ${TARGET} --fn ${JSON.stringify(fn)} 2>/dev/null`);
-    const parsedObj = JSON.parse(raw.slice(raw.indexOf('{')));
+    const envelope = JSON.parse(raw.slice(raw.indexOf('{')));
+    if (envelope.ok === false || (envelope.targetId && envelope.targetId !== TARGET) || (envelope.url && envelope.url !== LIST)) throw new Error('Console response ownership or URL mismatch');
+    const parsedObj = envelope.result || envelope;
+    if (parsedObj.url && parsedObj.url !== LIST) throw new Error('Console navigated away');
     const parsed = parsedObj.rows || [];
     const consoleTotal = Number.isFinite(parsedObj.total) ? parsedObj.total : null;
     const statusRe = /\b(Ready|Getting ready|Requires review|Needs attention)\b/i;
@@ -43,7 +46,7 @@ try {
       seen.add(p.dom);
       rows.push({ domain: p.dom, approval: (p.win.match(statusRe) || [,'UNKNOWN'])[1], ads_txt: (p.win.match(adsRe) || [,'UNKNOWN'])[1] });
     }
-    complete = rows.length >= (consoleTotal || ours.size) && !rows.some(r => r.approval === 'UNKNOWN' || r.ads_txt === 'UNKNOWN');
+    observedCount = rows.length;
     if (consoleTotal) expectedConsoleCount = consoleTotal;
     ok = rows.length > 0;
   }
@@ -52,9 +55,10 @@ try {
 // fill any of ours we didn't see as UNKNOWN
 for (const d of ours) if (!rows.find(r => r.domain === d)) rows.push({ domain: d, approval: 'UNKNOWN', ads_txt: 'UNKNOWN' });
 rows.sort((a, b) => a.domain.localeCompare(b.domain));
+complete = ok && Number.isInteger(expectedConsoleCount) && expectedConsoleCount > 0 && observedCount === expectedConsoleCount && !rows.some(r => r.approval === 'UNKNOWN' || r.ads_txt === 'UNKNOWN');
 
 const tally = rows.reduce((m, r) => (m[r.approval] = (m[r.approval] || 0) + 1, m), {});
-const out = { ts: new Date().toISOString(), source: ok ? 'openclaw-console' : 'unavailable', complete, expected_count: expectedConsoleCount || ours.size, count: rows.length, tally, rows };
+const out = { ts: new Date().toISOString(), source: ok ? 'openclaw-console' : 'unavailable', complete, observed_count: observedCount, expected_count: expectedConsoleCount, count: rows.length, tally, rows };
 fs.mkdirSync(path.join(SKILL, 'data'), { recursive: true });
 fs.writeFileSync(path.join(SKILL, 'data', 'status-latest.json'), JSON.stringify(out, null, 2));
 console.log(`AdSense status (${out.source}) — ${JSON.stringify(tally)}`);