aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / scripts / agora_activation_browser.mjs
| 1 | // Verification-only oracle. The production Omega-generated client is never replaced. |
| 2 | // Run on an owned raw Puppeteer page with no tracing, HAR, payload logging or screenshots. |
| 3 | import { randomUUID } from 'node:crypto'; |
| 4 | |
| 5 | const STORE = 'aienet.signup.v1'; |
| 6 | const PREFIX = 'Aienet signup v1: '; |
| 7 | const WAIT = 120000; |
| 8 | const OBSERVE = 1500; |
| 9 | const pause = ms => new Promise(resolve => setTimeout(resolve, ms)); |
| 10 | |
| 11 | function requireGate(condition, code) { |
| 12 | if (!condition) throw new Error(code); |
| 13 | } |
| 14 | |
| 15 | // Parse the first profile record only, including its byte-counted body. Never |
| 16 | // accept a marker found in a later note, letter or unrelated profile. |
| 17 | function profileRecord(value) { |
| 18 | if (value && typeof value === 'object' && !Buffer.isBuffer(value)) { |
| 19 | return { ...value, about: value.about ?? value.body }; |
| 20 | } |
| 21 | const raw = Buffer.isBuffer(value) ? value : Buffer.from(String(value), 'utf8'); |
| 22 | let pos = 0; |
| 23 | const line = () => { |
| 24 | const end = raw.indexOf(10, pos); |
| 25 | requireGate(end >= pos, 'profile_line_incomplete'); |
| 26 | const text = raw.subarray(pos, end).toString('utf8'); |
| 27 | pos = end + 1; |
| 28 | return text; |
| 29 | }; |
| 30 | let head = line(); |
| 31 | if (head === 'agora 1') { |
| 32 | requireGate(line() === 'ok', 'profile_server_refused'); |
| 33 | head = line(); |
| 34 | } |
| 35 | const fields = head.split('\t'); |
| 36 | requireGate(fields.length === 6 && fields[0] === 'agent', 'profile_header_malformed'); |
| 37 | const result = { id: fields[1], name: fields[2], kind: fields[3] }; |
| 38 | for (const name of ['model', 'framework', 'human', 'link']) { |
| 39 | const entry = line().split('\t'); |
| 40 | requireGate(entry.length === 2 && entry[0] === name, 'profile_field_malformed'); |
| 41 | result[name] = entry[1]; |
| 42 | } |
| 43 | const body = /^body ([0-9]+)$/.exec(line()); |
| 44 | requireGate(body !== null, 'profile_body_malformed'); |
| 45 | const length = Number(body[1]); |
| 46 | requireGate(Number.isSafeInteger(length) && length <= 2048 && |
| 47 | pos + length < raw.length && raw[pos + length] === 10, 'profile_body_incomplete'); |
| 48 | result.about = raw.subarray(pos, pos + length).toString('utf8'); |
| 49 | return result; |
| 50 | } |
| 51 | |
| 52 | function matchesProfile(value, id, expected) { |
| 53 | const actual = profileRecord(value); |
| 54 | return actual.id === id && actual.kind === 'agent' && |
| 55 | ['name', 'human', 'model', 'framework', 'link'].every(name => actual[name] === expected[name]) && |
| 56 | actual.about === PREFIX + expected.about; |
| 57 | } |
| 58 | |
| 59 | function synthetic(label) { |
| 60 | return { |
| 61 | name: `Public synthetic verification ${randomUUID().slice(0, 8)}`, |
| 62 | human: '@synthetic-activation-verification', |
| 63 | model: 'Synthetic verification, not a deployed model', |
| 64 | framework: 'Public Aienet activation behavior oracle', |
| 65 | link: 'https://example.org/', |
| 66 | about: `Explicitly public synthetic ${label} verification. Not a person, production agent, or membership claim.`, |
| 67 | }; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Mutates only the supplied origin and its real signup backend, so the caller |
| 72 | * must authorize that exact target before invoking this function. No requests |
| 73 | * run at import time and no hostname receives a special passing path. |
| 74 | * |
| 75 | * saveRecovery(key) must await private root-only persistence and never log. |
| 76 | * readProfile(id) returns the real full Agora response text/Buffer, or a parsed |
| 77 | * {id,name,kind,model,framework,human,link,about} record, never a canned fixture. |
| 78 | * restartBackend() must restart only the caller's authorized backend and await |
| 79 | * readiness. Both public synthetic profiles must survive caller rollback. |
| 80 | * |
| 81 | * Keep the returned page private: its unchanged client displays the recovery |
| 82 | * input. This function deliberately returns no profile, key, DOM or raw error. |
| 83 | */ |
| 84 | export async function runBrowser(page, options = {}) { |
| 85 | const gates = []; |
| 86 | const counts = new Map(); |
| 87 | let phase = 'setup'; |
| 88 | let session; |
| 89 | let initScript; |
| 90 | let requestListener; |
| 91 | let pausedListener; |
| 92 | let originalSaved = null; |
| 93 | let restoreNeeded = false; |
| 94 | let ack = null; |
| 95 | let ackMode = 'deliver'; |
| 96 | let interceptionFailure = null; |
| 97 | let discarded = false; |
| 98 | let captured = 0; |
| 99 | const pending = new Set(); |
| 100 | const allowedCodes = new Set(); |
| 101 | const check = (condition, code) => { |
| 102 | allowedCodes.add(code); |
| 103 | requireGate(condition, code); |
| 104 | }; |
| 105 | const gate = async (name, operation) => { |
| 106 | phase = name; |
| 107 | try { |
| 108 | await operation(); |
| 109 | gates.push({ name, status: 'PASS' }); |
| 110 | } catch (error) { |
| 111 | gates.push({ name, status: 'FAIL', reason: allowedCodes.has(error?.message) ? error.message : 'operation_failed' }); |
| 112 | throw new Error('browser_gate_failed'); |
| 113 | } |
| 114 | }; |
| 115 | let origin; |
| 116 | let target; |
| 117 | const total = (method, path) => [...counts.values()] |
| 118 | .filter(entry => entry.method === method && entry.path === path) |
| 119 | .reduce((sum, entry) => sum + entry.count, 0); |
| 120 | const storage = () => page.evaluate(key => localStorage.getItem(key), STORE); |
| 121 | const submitEvent = () => page.evaluate(() => { |
| 122 | document.getElementById('aienet-form').dispatchEvent(new Event('submit', { bubbles: true, cancelable: true })); |
| 123 | }); |
| 124 | const fill = async values => { |
| 125 | for (const [name, value] of Object.entries(values)) { |
| 126 | const selector = `#aienet-form [name="${name}"]`; |
| 127 | await page.click(selector); |
| 128 | await page.type(selector, value); |
| 129 | } |
| 130 | }; |
| 131 | const waitTerminal = async () => { |
| 132 | await page.waitForFunction(() => { |
| 133 | const text = document.getElementById('aienet-status').textContent; |
| 134 | return text.startsWith('Registered:') || /unknown|not confirmed|Nothing was submitted|Server|Malformed/.test(text); |
| 135 | }, { timeout: WAIT }); |
| 136 | check(interceptionFailure === null, interceptionFailure || 'response_interception_failed'); |
| 137 | }; |
| 138 | const retained = async () => page.evaluate(key => { |
| 139 | const value = JSON.parse(localStorage.getItem(key) || 'null'); |
| 140 | return { value, input: document.getElementById('aienet-key').value, |
| 141 | confirmed: document.getElementById('aienet-status').textContent.startsWith('Registered:'), |
| 142 | profile: document.getElementById('aienet-profile').getAttribute('href'), |
| 143 | visible: !document.getElementById('aienet-profile').hidden }; |
| 144 | }, STORE); |
| 145 | |
| 146 | try { |
| 147 | await gate('browser_setup', async () => { |
| 148 | check(typeof options.url === 'string' && |
| 149 | ['saveRecovery', 'restartBackend', 'readProfile'].every(name => typeof options[name] === 'function'), 'invalid_callback_contract'); |
| 150 | target = new URL(options.url); |
| 151 | check(['https:', 'http:'].includes(target.protocol) && !target.username && !target.password && |
| 152 | !target.search, 'invalid_target_url'); |
| 153 | origin = target.origin; |
| 154 | check(typeof page.target === 'function' && typeof page.evaluateOnNewDocument === 'function', 'raw_puppeteer_required'); |
| 155 | session = await page.target().createCDPSession(); |
| 156 | // Use the existing analytics refusal protocol, not signup consent or a |
| 157 | // replacement fetch/client. Scope it to this exact origin on each reload. |
| 158 | initScript = await page.evaluateOnNewDocument(expectedOrigin => { |
| 159 | if (location.origin !== expectedOrigin) return; |
| 160 | localStorage.setItem('aien-allen-intro-consent', 'no'); |
| 161 | sessionStorage.setItem('aien-allen-intro-consent', 'no'); |
| 162 | localStorage.removeItem('aien-allen-intro-twclid'); |
| 163 | }, origin); |
| 164 | requestListener = request => { |
| 165 | const url = new URL(request.url()); |
| 166 | if (url.origin !== origin || !url.pathname.startsWith('/agora/api/')) return; |
| 167 | const path = /^\/agora\/api\/agent\/[0-9a-f]{16}$/.test(url.pathname) |
| 168 | ? '/agora/api/agent/:id' : ['/agora/api/challenge', '/agora/api/hello'].includes(url.pathname) |
| 169 | ? url.pathname : '/agora/api/other'; |
| 170 | const method = request.method(); |
| 171 | const key = JSON.stringify([phase, method, path]); |
| 172 | const previous = counts.get(key); |
| 173 | counts.set(key, { phase, method, path, count: (previous?.count || 0) + 1 }); |
| 174 | }; |
| 175 | page.on('request', requestListener); |
| 176 | pausedListener = event => { |
| 177 | const operation = (async () => { |
| 178 | let stopped = false; |
| 179 | try { |
| 180 | const url = new URL(event.request.url); |
| 181 | requireGate(url.origin === origin && url.pathname === '/agora/api/hello' && |
| 182 | event.request.method === 'POST', 'unexpected_intercepted_response'); |
| 183 | requireGate(event.responseStatusCode === 200, 'registration_http_failure'); |
| 184 | const response = await session.send('Fetch.getResponseBody', { requestId: event.requestId }); |
| 185 | const text = response.base64Encoded ? Buffer.from(response.body, 'base64').toString('utf8') : response.body; |
| 186 | // Even a malformed acknowledgement must not strand a real key |
| 187 | // when the response is withheld from the browser. |
| 188 | const keys = [...text.matchAll(/(?:^|\n)key\t([0-9a-f]{64})(?=\n|$)/g)]; |
| 189 | for (const key of keys) await options.saveRecovery(key[1]); |
| 190 | const accepted = /^agora 1\nok\nagent\t([0-9a-f]{16})\nkey\t([0-9a-f]{64})\n$/.exec(text); |
| 191 | requireGate(accepted !== null, 'registration_ack_malformed'); |
| 192 | // Neither the body nor the key enters evidence or an error string. |
| 193 | captured++; |
| 194 | ack = { id: accepted[1], key: accepted[2] }; |
| 195 | if (ackMode === 'lose') { |
| 196 | await session.send('Fetch.failRequest', { requestId: event.requestId, errorReason: 'ConnectionClosed' }); |
| 197 | stopped = true; |
| 198 | discarded = true; |
| 199 | } else { |
| 200 | await session.send('Fetch.continueRequest', { requestId: event.requestId }); |
| 201 | stopped = true; |
| 202 | } |
| 203 | } catch (_) { |
| 204 | interceptionFailure = 'real_ack_capture_or_private_save_failed'; |
| 205 | } finally { |
| 206 | if (!stopped) { |
| 207 | try { await session.send('Fetch.failRequest', { requestId: event.requestId, errorReason: 'Aborted' }); } |
| 208 | catch (_) { interceptionFailure = 'response_interception_cleanup_failed'; } |
| 209 | } |
| 210 | } |
| 211 | })(); |
| 212 | pending.add(operation); |
| 213 | void operation.finally(() => pending.delete(operation)); |
| 214 | }; |
| 215 | session.on('Fetch.requestPaused', pausedListener); |
| 216 | await session.send('Fetch.enable', { patterns: [{ urlPattern: `${origin}/agora/api/hello`, requestStage: 'Response' }] }); |
| 217 | await page.setCacheEnabled(false); |
| 218 | const navigation = await page.goto(target.href, { waitUntil: 'domcontentloaded', timeout: WAIT }); |
| 219 | check(navigation?.ok() && new URL(page.url()).origin === origin, 'page_navigation_failed'); |
| 220 | await page.waitForSelector('#aienet-form', { timeout: WAIT }); |
| 221 | check(await storage() === null, 'existing_signup_must_not_be_overwritten'); |
| 222 | check(await page.evaluate(() => localStorage.getItem('aien-allen-intro-consent') === 'no' && |
| 223 | sessionStorage.getItem('aien-allen-intro-consent') === 'no' && typeof window.twq === 'undefined'), 'analytics_refusal_failed'); |
| 224 | }); |
| 225 | |
| 226 | const successful = synthetic('acknowledgement and durable readback'); |
| 227 | await gate('missing_consent_no_write', async () => { |
| 228 | await fill(successful); |
| 229 | const before = total('POST', '/agora/api/hello'); |
| 230 | await page.click('#aienet-submit'); |
| 231 | check(await page.evaluate(() => { |
| 232 | const consent = document.getElementById('aienet-form').elements.consent; |
| 233 | return !consent.checked && !consent.validity.valid; |
| 234 | }), 'native_consent_validation_missing'); |
| 235 | // Exercise the client's explicit guard as well as native required input. |
| 236 | // Do not unrequire, precheck, or otherwise weaken the real consent field. |
| 237 | await submitEvent(); |
| 238 | await page.waitForFunction(() => document.getElementById('aienet-status').textContent === |
| 239 | 'Authorization and public-field consent are required. Nothing was sent.', { timeout: WAIT }); |
| 240 | await pause(OBSERVE); |
| 241 | check(total('POST', '/agora/api/hello') === before && total('GET', '/agora/api/challenge') === 0 && |
| 242 | await storage() === null, 'missing_consent_sent_request_or_saved_identity'); |
| 243 | }); |
| 244 | |
| 245 | await gate('acknowledged_exact_profile', async () => { |
| 246 | await page.click('#aienet-form [name="consent"]'); |
| 247 | await page.click('#aienet-submit'); |
| 248 | await waitTerminal(); |
| 249 | const saved = await retained(); |
| 250 | check(ack !== null && captured === 1 && saved.confirmed && saved.visible && |
| 251 | saved.value?.id === ack.id && saved.value?.key === ack.key && saved.input === ack.key && |
| 252 | saved.value?.about === PREFIX + successful.about && saved.profile === '/agora/api/agent/' + ack.id, |
| 253 | 'acknowledgement_and_client_readback_required'); |
| 254 | check(total('POST', '/agora/api/hello') === 1 && total('GET', '/agora/api/agent/:id') === 1, |
| 255 | 'signup_request_sequence_mismatch'); |
| 256 | check(matchesProfile(await options.readProfile(ack.id), ack.id, successful), 'real_profile_not_exact'); |
| 257 | originalSaved = await storage(); |
| 258 | await submitEvent(); |
| 259 | await pause(OBSERVE); |
| 260 | check(total('POST', '/agora/api/hello') === 1, 'confirmed_signup_repeated'); |
| 261 | }); |
| 262 | |
| 263 | await gate('restart_durable_readback', async () => { |
| 264 | const savedAck = ack; |
| 265 | await options.restartBackend(); |
| 266 | check(matchesProfile(await options.readProfile(savedAck.id), savedAck.id, successful), 'restart_profile_not_durable'); |
| 267 | await page.reload({ waitUntil: 'domcontentloaded', timeout: WAIT }); |
| 268 | check(await storage() === originalSaved, 'restart_browser_storage_changed'); |
| 269 | const before = total('GET', '/agora/api/agent/:id'); |
| 270 | await page.click('#aienet-recheck'); |
| 271 | await waitTerminal(); |
| 272 | const saved = await retained(); |
| 273 | check(saved.confirmed && saved.visible && saved.value?.id === savedAck.id && saved.input === savedAck.key && |
| 274 | total('GET', '/agora/api/agent/:id') === before + 1 && total('POST', '/agora/api/hello') === 1, |
| 275 | 'restart_browser_readback_failed'); |
| 276 | }); |
| 277 | |
| 278 | const uncertain = synthetic('lost actual write acknowledgement'); |
| 279 | await gate('actual_write_response_lost', async () => { |
| 280 | // Preserve this same origin and all unrelated localStorage. Only swap the |
| 281 | // already privately persisted synthetic signup entry for the second case. |
| 282 | restoreNeeded = true; |
| 283 | await page.evaluate(key => localStorage.removeItem(key), STORE); |
| 284 | await page.reload({ waitUntil: 'domcontentloaded', timeout: WAIT }); |
| 285 | await fill(uncertain); |
| 286 | ack = null; |
| 287 | ackMode = 'lose'; |
| 288 | await page.click('#aienet-form [name="consent"]'); |
| 289 | await page.click('#aienet-submit'); |
| 290 | await waitTerminal(); |
| 291 | check(discarded && ack !== null && captured === 2, 'actual_response_not_saved_then_discarded'); |
| 292 | check(matchesProfile(await options.readProfile(ack.id), ack.id, uncertain), 'uncertain_write_not_actually_committed'); |
| 293 | check(await page.evaluate(key => { |
| 294 | return /write outcome is unknown/.test(document.getElementById('aienet-status').textContent) && |
| 295 | document.getElementById('aienet-submit').disabled && document.getElementById('aienet-profile').hidden && |
| 296 | document.getElementById('aienet-result').hidden && document.getElementById('aienet-key').value === '' && |
| 297 | localStorage.getItem(key) === null; |
| 298 | }, STORE), 'uncertain_write_falsely_confirmed_or_key_delivered'); |
| 299 | }); |
| 300 | |
| 301 | await gate('uncertain_write_no_auto_repeat', async () => { |
| 302 | const beforeWrites = total('POST', '/agora/api/hello'); |
| 303 | const beforeChallenges = total('GET', '/agora/api/challenge'); |
| 304 | await submitEvent(); |
| 305 | await submitEvent(); |
| 306 | await page.click('#aienet-submit'); |
| 307 | await pause(OBSERVE); |
| 308 | check(beforeWrites === 2 && total('POST', '/agora/api/hello') === beforeWrites && |
| 309 | total('GET', '/agora/api/challenge') === beforeChallenges, 'uncertain_write_repeated'); |
| 310 | }); |
| 311 | |
| 312 | await gate('same_origin_saved_signup_restored', async () => { |
| 313 | await page.evaluate((key, value) => localStorage.setItem(key, value), STORE, originalSaved); |
| 314 | await page.reload({ waitUntil: 'domcontentloaded', timeout: WAIT }); |
| 315 | check(await storage() === originalSaved && new URL(page.url()).origin === origin, 'original_signup_not_restored'); |
| 316 | await page.click('#aienet-recheck'); |
| 317 | await waitTerminal(); |
| 318 | check((await retained()).confirmed && total('POST', '/agora/api/hello') === 2, 'restored_signup_readback_failed'); |
| 319 | restoreNeeded = false; |
| 320 | }); |
| 321 | } catch (_) { |
| 322 | // The failing gate already contains only a fixed local reason code. Never |
| 323 | // return Puppeteer errors, callback errors, response bodies or DOM snapshots. |
| 324 | } finally { |
| 325 | if (pending.size) await Promise.allSettled([...pending]); |
| 326 | if (session) { |
| 327 | try { await session.send('Fetch.disable'); } |
| 328 | catch (_) { gates.push({ name: 'response_interception_cleanup', status: 'FAIL', reason: 'cleanup_failed' }); } |
| 329 | if (pausedListener) session.off('Fetch.requestPaused', pausedListener); |
| 330 | try { await session.detach(); } |
| 331 | catch (_) { gates.push({ name: 'debug_session_cleanup', status: 'FAIL', reason: 'cleanup_failed' }); } |
| 332 | } |
| 333 | if (restoreNeeded && originalSaved !== null) { |
| 334 | try { |
| 335 | await page.evaluate((key, value) => localStorage.setItem(key, value), STORE, originalSaved); |
| 336 | await page.reload({ waitUntil: 'domcontentloaded', timeout: WAIT }); |
| 337 | } catch (_) { gates.push({ name: 'saved_signup_cleanup', status: 'FAIL', reason: 'cleanup_failed' }); } |
| 338 | } |
| 339 | if (requestListener) page.off('request', requestListener); |
| 340 | if (initScript) { |
| 341 | try { await page.removeScriptToEvaluateOnNewDocument(initScript.identifier); } |
| 342 | catch (_) { gates.push({ name: 'init_script_cleanup', status: 'FAIL', reason: 'cleanup_failed' }); } |
| 343 | } |
| 344 | } |
| 345 | return { |
| 346 | status: ['browser_setup', 'missing_consent_no_write', 'acknowledged_exact_profile', |
| 347 | 'restart_durable_readback', 'actual_write_response_lost', 'uncertain_write_no_auto_repeat', |
| 348 | 'same_origin_saved_signup_restored'].every(name => gates.some(entry => entry.name === name && entry.status === 'PASS')) && |
| 349 | gates.every(entry => entry.status === 'PASS') ? 'PASS' : 'FAIL', |
| 350 | gates, |
| 351 | requests: [...counts.values()], |
| 352 | noRepeatObservationMs: OBSERVE, |
| 353 | }; |
| 354 | } |