// Background-list attention is driven by content, not activity timestamps. import assert from 'node:assert/strict'; import { mkdir, mkdtemp, writeFile } from 'node:fs/promises'; import { once } from 'node:events'; import { tmpdir } from 'node:os'; import { join, resolve } from 'node:path'; import { startFixture } from './runs_probe_server.mjs'; import { launchChrome, waitForPort, waitForPage, connect } from './pipelines_probe_cdp_lib.mjs'; import { axNodes, enableSemantics, waitFor, capture, clickAt } from './pipelines_probe_cdp_input.mjs'; const output=resolve(process.argv[3]??'build/chat-attention-browser'); await mkdir(output,{recursive:true}); const fixture=await startFixture({root:resolve(process.argv[2]),chatMessages:[]}); const profile=await mkdtemp(join(tmpdir(),'chat-attention-')); const chrome=launchChrome('/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',profile,'about:blank'); const log={checks:[],captures:[]};let c; const names=async()=>(await axNodes(c)).map(n=>n.name?.value??''); const isNew=async()=>(await names()).some(n=>n.includes('Probe conversation')&&/\bNew\b/.test(n)); const working=async()=>(await names()).some(n=>n.includes('Chat is working')); const emit=async body=>{assert.ok((await fetch(fixture.url+'/fixture/chat-event',{method:'POST',body:JSON.stringify(body)})).ok);}; try { c=await connect((await waitForPage(await waitForPort(join(profile,'DevToolsActivePort')))).webSocketDebuggerUrl); await c.send('Page.enable'); await c.send('Emulation.setDeviceMetricsOverride',{width:1280,height:900,deviceScaleFactor:1,mobile:false}); await c.send('Page.navigate',{url:fixture.url+'/chat'}); assert.ok(await waitFor(()=>c.evaluate("Boolean(document.querySelector('flt-semantics-placeholder'))"),30000)); await enableSemantics(c); assert.ok(await waitFor(async()=>(await names()).some(n=>n.includes('Probe conversation')),15000)); assert.ok(!await isNew()); await emit({kind:'step',step:{status:'running',turn_id:'attention'}}); assert.ok(await waitFor(working,10000)); await emit({kind:'reasoning',message_id:'attention-answer',part:{type:'reasoning',id:'r'},delta:'Background thinking is not unread.'}); await emit({kind:'shell',shell:{id:'background',status:'exited'}}); assert.ok(!await isNew()); assert.ok(await working(),'background shell exit cannot clear active turn'); await capture(c,output,log,'01-working-not-new'); await emit({kind:'step',step:{status:'ended',turn_id:'attention'}}); assert.ok(await waitFor(async()=>!await working(),10000)); assert.ok(!await isNew(),'completion without content is not unread'); log.checks.push('working indicator for activity-only events, no New on reasoning/shell/turn completion'); await emit({kind:'step',step:{status:'running',turn_id:'attention-two'}}); await emit({kind:'text',message_id:'attention-answer',part:{type:'text',id:'answer'},delta:'A new assistant message to read.'}); assert.ok(await waitFor(isNew,10000)); assert.ok(!await working(),'unread content takes precedence over loading'); await capture(c,output,log,'02-new-message'); const row=(await axNodes(c)).find(n=>n.name?.value?.startsWith('Claude harness')&&n.name.value.includes('Probe conversation')&&n.backendDOMNodeId); assert.ok(row); const {model}=await c.send('DOM.getBoxModel',{backendNodeId:row.backendDOMNodeId}); await clickAt(c,model.content[0]+30,model.content[1]+10); assert.ok(await waitFor(async()=>!await isNew(),10000)); await capture(c,output,log,'03-opened-clears-new'); log.checks.push('actual unread message shows New even while running; opening acknowledges it'); console.log(JSON.stringify(log,null,2)); } catch(error){if(c){await capture(c,output,log,'failure');await writeFile(join(output,'failure-ax.json'),JSON.stringify(await axNodes(c),null,2));}throw error;} finally{await writeFile(join(output,'result.json'),JSON.stringify(log,null,2));c?.close();const exited=once(chrome,'exit');chrome.kill();await exited;await fixture.close();}