# Herdr orchestrator skill and Codex harness runtime

> Source: <https://gist.github.com/changeroa/f6dbe944c635eacac5d862b29017639e>
> Published: 2026-11-19 00:15:34+00:00

**Key takeaway:** Harness Runtime hooks into Codex's session, prompt, tool-use and stop events to record real command results and reuse verification evidence while inputs stay valid, leaving continuation to Codex itself.

|  | diff --git a/.codex-plugin/plugin.json b/.codex-plugin/plugin.json | 
|  | new file mode 100644 | 
|  | index 0000000..2ed091c | 
|  | --- /dev/null | 
|  | +++ b/.codex-plugin/plugin.json | 
|  | @@ -0,0 +1,17 @@ | 
|  | +{ | 
|  | +  "name": "harness-runtime", | 
|  | +  "version": "0.1.0+codex.20261011-task-loop-enrollment-v2", | 
|  | +  "description": "Support Codex's native goal loop with observed outcomes, reusable verification, execution context, and owned process monitoring.", | 
|  | +  "author": { | 
|  | +    "name": "Local developer" | 
|  | +  }, | 
|  | +  "interface": { | 
|  | +    "displayName": "Harness Runtime", | 
|  | +    "shortDescription": "Keep goals and verify actual outcomes across turns.", | 
|  | +    "longDescription": "Record real command results, validate declared outcomes, and reuse evidence while its inputs remain valid. Codex owns continuation; workflow guidance is discovered through the configured gisool MCP server.", | 
|  | +    "developerName": "Local developer", | 
|  | +    "category": "Productivity", | 
|  | +    "capabilities": [], | 
|  | +    "defaultPrompt": "Help me use Harness Runtime." | 
|  | +  } | 
|  | +} | 
|  | diff --git a/hooks/hooks.json b/hooks/hooks.json | 
|  | new file mode 100644 | 
|  | index 0000000..9a6d023 | 
|  | --- /dev/null | 
|  | +++ b/hooks/hooks.json | 
|  | @@ -0,0 +1,73 @@ | 
|  | +{ | 
|  | +  "hooks": { | 
|  | +    "SessionStart": [ | 
|  | +      { | 
|  | +        "hooks": [ | 
|  | +          { | 
|  | +            "type": "command", | 
|  | +            "command": "node \"${CODEX_HOME:-$HOME/.codex}/harness-runtime/current/scripts/harness.mjs\" hook", | 
|  | +            "timeout": 5, | 
|  | +            "additionalContextLimit": 600 | 
|  | +          } | 
|  | +        ] | 
|  | +      } | 
|  | +    ], | 
|  | +    "UserPromptSubmit": [ | 
|  | +      { | 
|  | +        "hooks": [ | 
|  | +          { | 
|  | +            "type": "command", | 
|  | +            "command": "node \"${CODEX_HOME:-$HOME/.codex}/harness-runtime/current/scripts/harness.mjs\" hook", | 
|  | +            "timeout": 5, | 
|  | +            "additionalContextLimit": 600 | 
|  | +          } | 
|  | +        ] | 
|  | +      } | 
|  | +    ], | 
|  | +    "PreToolUse": [ | 
|  | +      { | 
|  | +        "hooks": [ | 
|  | +          { | 
|  | +            "type": "command", | 
|  | +            "command": "node \"${CODEX_HOME:-$HOME/.codex}/harness-runtime/current/scripts/harness.mjs\" hook", | 
|  | +            "timeout": 5, | 
|  | +            "additionalContextLimit": 600 | 
|  | +          } | 
|  | +        ] | 
|  | +      } | 
|  | +    ], | 
|  | +    "Stop": [ | 
|  | +      { | 
|  | +        "hooks": [ | 
|  | +          { | 
|  | +            "type": "command", | 
|  | +            "command": "node \"${CODEX_HOME:-$HOME/.codex}/harness-runtime/current/scripts/harness.mjs\" hook", | 
|  | +            "timeout": 5 | 
|  | +          } | 
|  | +        ] | 
|  | +      } | 
|  | +    ], | 
|  | +    "SessionEnd": [ | 
|  | +      { | 
|  | +        "hooks": [ | 
|  | +          { | 
|  | +            "type": "command", | 
|  | +            "command": "node \"${CODEX_HOME:-$HOME/.codex}/harness-runtime/current/scripts/harness.mjs\" hook", | 
|  | +            "timeout": 3 | 
|  | +          } | 
|  | +        ] | 
|  | +      } | 
|  | +    ], | 
|  | +    "Interrupt": [ | 
|  | +      { | 
|  | +        "hooks": [ | 
|  | +          { | 
|  | +            "type": "command", | 
|  | +            "command": "node \"${CODEX_HOME:-$HOME/.codex}/harness-runtime/current/scripts/harness.mjs\" hook", | 
|  | +            "timeout": 3 | 
|  | +          } | 
|  | +        ] | 
|  | +      } | 
|  | +    ] | 
|  | +  } | 
|  | +} | 
|  | diff --git a/package.json b/package.json | 
|  | new file mode 100644 | 
|  | index 0000000..1d78de6 | 
|  | --- /dev/null | 
|  | +++ b/package.json | 
|  | @@ -0,0 +1 @@ | 
|  | +{"name":"harness-runtime","version":"0.1.0","private":true,"type":"module","scripts":{"test":"node --test test/*.test.mjs","verify":"node scripts/check-runtime.mjs","verify:installed":"node scripts/check-installed.mjs"}} | 
|  | diff --git a/scripts/check-installed.mjs b/scripts/check-installed.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..4f322bf | 
|  | --- /dev/null | 
|  | +++ b/scripts/check-installed.mjs | 
|  | @@ -0,0 +1,29 @@ | 
|  | +#!/usr/bin/env node | 
|  | +import fs from 'node:fs'; | 
|  | +import os from 'node:os'; | 
|  | +import path from 'node:path'; | 
|  | +import crypto from 'node:crypto'; | 
|  | +import {spawnSync} from 'node:child_process'; | 
|  | +import {fileURLToPath} from 'node:url'; | 
|  | + | 
|  | +const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); | 
|  | +const base = path.join(process.env.CODEX_HOME \|\| path.join(os.homedir(), '.codex'), 'harness-runtime'); | 
|  | +const hash = value => crypto.createHash('sha256').update(value).digest('hex'); | 
|  | +const git = (...args) => {const result = spawnSync('git', ['-C', root, ...args], {encoding: 'utf8', timeout: 2000}); return result.status === 0 ? result.stdout.trim() : null;}; | 
|  | +try { | 
|  | +  const release = fs.realpathSync(path.join(base, 'current')); | 
|  | +  const receipt = JSON.parse(fs.readFileSync(path.join(base, 'installation.json'), 'utf8')); | 
|  | +  const files = ['scripts/harness.mjs','scripts/herdr-identity.mjs','scripts/herdr-binding.mjs', 'scripts/verification.mjs', 'scripts/execution.mjs','scripts/task-loop.mjs','scripts/queue-transport.mjs','scripts/continuation-watch-trigger.mjs','hooks/hooks.json', '.codex-plugin/plugin.json'].map(file => ({file, | 
|  | +    equal: hash(fs.readFileSync(path.join(root, file))) === hash(fs.readFileSync(path.join(release, file)))})); | 
|  | +  const help = spawnSync(process.execPath, [path.join(release, 'scripts/harness.mjs'), 'verify', 'help'], {encoding: 'utf8', timeout: 3000}); | 
|  | +  const features = spawnSync('codex', ['features', 'list'], {encoding: 'utf8', timeout: 10000}); | 
|  | +  const goals = features.status === 0 && /^goals\s+\S+\s+true\s*$/m.test(features.stdout); | 
|  | +  const result = {source: root, source_commit: git('rev-parse', 'HEAD'), source_clean: git('status', '--porcelain') === '', | 
|  | +    release, installed_commit: receipt.source_commit, installation_source: receipt.source, | 
|  | +    receipt_matches_release: receipt.release === release, files_match: files.every(file => file.equal), files, | 
|  | +    verifier_available: help.status === 0 && JSON.parse(help.stdout).schema?.schema_version === 2, | 
|  | +    native_goals_enabled: goals, current_unchanged: fs.realpathSync(path.join(base, 'current')) === release}; | 
|  | +  console.log(JSON.stringify(result)); | 
|  | +  if (!result.source_clean \|\| result.source_commit !== result.installed_commit \|\| result.installation_source !== root \|\| | 
|  | +      !result.receipt_matches_release \|\| !result.files_match \|\| !result.verifier_available \|\| !goals \|\| !result.current_unchanged) process.exitCode = 1; | 
|  | +} catch (error) {console.log(JSON.stringify({verified: false, error: error.message})); process.exitCode = 1;} | 
|  | diff --git a/scripts/check-runtime.mjs b/scripts/check-runtime.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..5d5e5d0 | 
|  | --- /dev/null | 
|  | +++ b/scripts/check-runtime.mjs | 
|  | @@ -0,0 +1,20 @@ | 
|  | +#!/usr/bin/env node | 
|  | +import fs from 'node:fs'; | 
|  | +import path from 'node:path'; | 
|  | +import {fileURLToPath} from 'node:url'; | 
|  | +import {run} from 'node:test'; | 
|  | + | 
|  | +const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); | 
|  | +const files = fs.readdirSync(path.join(root, 'test')).filter(name => name.endsWith('.test.mjs')).sort().map(name => path.join(root, 'test', name)); | 
|  | +let summary = null; | 
|  | +const failures = []; | 
|  | +for await (const event of run({files})) { | 
|  | +  if (event.type === 'test:summary' && !event.data.file) summary = event.data; | 
|  | +  if (event.type === 'test:fail') failures.push({name: event.data.name, message: event.data.details?.error?.message}); | 
|  | +} | 
|  | +const result = {success: Boolean(summary?.success), executed: summary?.counts?.tests ?? 0, | 
|  | +  passed: summary?.counts?.passed ?? 0, failed: summary?.counts?.failed ?? null, | 
|  | +  cancelled: summary?.counts?.cancelled ?? null, skipped: summary?.counts?.skipped ?? null, | 
|  | +  todo: summary?.counts?.todo ?? null, duration_ms: summary?.duration_ms ?? null, failures}; | 
|  | +console.log(JSON.stringify(result)); | 
|  | +if (!result.success \|\| !result.executed \|\| result.failed \|\| result.cancelled \|\| result.skipped \|\| result.todo) process.exitCode = 1; | 
|  | diff --git a/scripts/continuation-watch-trigger.mjs b/scripts/continuation-watch-trigger.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..a98b5da | 
|  | --- /dev/null | 
|  | +++ b/scripts/continuation-watch-trigger.mjs | 
|  | @@ -0,0 +1,90 @@ | 
|  | +import fs from 'node:fs'; | 
|  | +import path from 'node:path'; | 
|  | +import crypto from 'node:crypto'; | 
|  | +import {execFileSync} from 'node:child_process'; | 
|  | +import {pathToFileURL} from 'node:url'; | 
|  | + | 
|  | +export const enrollmentHash=value=>crypto.createHash('sha256').update(String(value)).digest('hex'); | 
|  | +const id=value=>typeof value==='string'&&/^[a-zA-Z0-9][a-zA-Z0-9._:-]{0,199}$/.test(value); | 
|  | +const absolute=value=>typeof value==='string'&&value.length<=4096&&path.isAbsolute(value); | 
|  | +function mapping(config){ | 
|  | +  const c=config,p=c?.parent; | 
|  | +  if(c?.schema_version!==1\|\|!['armed','cancelled','enrolled'].includes(c.state)\|\|!id(c.task_id)\|\|!id(c.workspace_id)\|\|!id(c.goal_tab)\|\|!p\|\|!id(p.session_id)\|\|!id(p.pane_id)\|\|!absolute(p.cwd)\|\|!['project_root','state_root','claim_file','registration_script','canary_result'].every(k=>absolute(c[k])))return null; | 
|  | +  const max=c.max_emissions??3,retry=c.retry_after_ms??60000; | 
|  | +  if(!Number.isSafeInteger(max)\|\|max<1\|\|max>3\|\|!Number.isSafeInteger(retry)\|\|retry<60000\|\|retry>300000)return null; | 
|  | +  return {schema_version:1,task_id:c.task_id,workspace_id:c.workspace_id,goal_tab:c.goal_tab,project_root:c.project_root,parent:{session_id:p.session_id,pane_id:p.pane_id,cwd:p.cwd},expires_at:c.expires_at,state_root:c.state_root,claim_file:c.claim_file,registration_script:c.registration_script,canary_result:c.canary_result,max_emissions:max,retry_after_ms:retry}; | 
|  | +} | 
|  | +export function evaluateEnrollment({config,snapshot,previous=null,now=Date.now(),observed_at,guards}={}){ | 
|  | +  const reject=reason=>({event:null,state:previous,reason}),m=mapping(config); | 
|  | +  if(!m\|\|!Number.isFinite(now))return reject('invalid_mapping'); | 
|  | +  if(config.state!=='armed')return reject(config.state); | 
|  | +  const expiry=Date.parse(m.expires_at),observed=Date.parse(observed_at); | 
|  | +  if(!Number.isFinite(expiry)\|\|expiry<=now\|\|expiry-now>3600000)return reject('expired_or_unbounded'); | 
|  | +  if(!Number.isFinite(observed)\|\|observed>now\|\|now-observed>30000)return reject('snapshot_not_fresh'); | 
|  | +  if(!guards\|\|guards.bound!==false\|\|guards.claimed!==false)return reject('bound_claimed_or_guard_unavailable'); | 
|  | +  if(guards.canary_passed!==true\|\|guards.paths_present!==true)return reject('activation_prerequisite'); | 
|  | +  if(!Array.isArray(snapshot?.tabs)\|\|!Array.isArray(snapshot?.agents))return reject('snapshot_invalid'); | 
|  | +  const tabs=snapshot.tabs.filter(t=>t?.tab_id===m.goal_tab),agents=snapshot.agents; | 
|  | +  if(tabs?.length!==1\|\|tabs[0].workspace_id!==m.workspace_id\|\|!Array.isArray(agents))return reject('goal_identity'); | 
|  | +  const parents=agents.filter(a=>a?.pane_id===m.parent.pane_id),sessionMatches=agents.filter(a=>a?.agent_session?.value===m.parent.session_id); | 
|  | +  if(parents.length!==1\|\|sessionMatches.length!==1)return reject('parent_identity'); | 
|  | +  const a=parents[0]; | 
|  | +  if(a.agent!=='codex'\|\|a.agent_session?.kind!=='id'\|\|a.agent_session.value!==m.parent.session_id\|\|a.workspace_id!==m.workspace_id\|\|a.tab_id!==m.goal_tab\|\|a.cwd!==m.parent.cwd\|\|a.foreground_cwd&&a.foreground_cwd!==m.parent.cwd)return reject('parent_identity'); | 
|  | +  if(!['idle','done'].includes(a.agent_status))return reject('parent_not_ready'); | 
|  | +  const configHash=enrollmentHash(JSON.stringify(m)); | 
|  | +  if(previous&&(previous.schema_version!==1\|\|previous.config_hash!==configHash\|\|!Number.isSafeInteger(previous.emissions)\|\|previous.emissions<1\|\|previous.emissions>m.max_emissions\|\|!Number.isFinite(previous.next_emission_at)))return reject('receipt_invalid_or_mapping_changed'); | 
|  | +  if(previous?.emissions>=m.max_emissions)return reject('emission_budget'); | 
|  | +  if(previous?.next_emission_at>now)return reject('unchanged_backoff'); | 
|  | +  const emissions=(previous?.emissions\|\|0)+1,eventId=enrollmentHash('enrollment:'+configHash); | 
|  | +  const event={type:'harness-enrollment-ready',event_id:eventId,emission:emissions,task_id:m.task_id,workspace_id:m.workspace_id,goal_tab:m.goal_tab,project_root:m.project_root,parent:m.parent,expires_at:m.expires_at,claim_file:m.claim_file,registration_script:m.registration_script,observed_at,source:'herdr_live'}; | 
|  | +  return {event,state:{schema_version:1,config_hash:configHash,event_id:eventId,emissions,last_emitted_at:new Date(now).toISOString(),next_emission_at:now+m.retry_after_ms},reason:'ready'}; | 
|  | +} | 
|  | +export function composeWatchResult(original,event){ | 
|  | +  if(!event)return original; | 
|  | +  return {...original,fire:true,message:JSON.stringify({type:'herdr-watch-and-enrollment',original_message:original.message??null,enrollment:event})}; | 
|  | +} | 
|  | +function readJson(file){const stat=fs.statSync(file);if(!stat.isFile()\|\|stat.size>262144)throw Error('invalid_resource');return JSON.parse(fs.readFileSync(file,'utf8'));} | 
|  | +function exists(file){try{fs.lstatSync(file);return true;}catch(e){if(e.code==='ENOENT')return false;throw Error('resource_unavailable');}} | 
|  | +function guardsFor(c,now){ | 
|  | +  const canary=readJson(c.canary_result),at=Date.parse(canary.verified_at); | 
|  | +  return {bound:exists(path.join(c.state_root,'sessions',enrollmentHash(c.parent.session_id),'task-membership.json'))\|\|exists(path.join(c.state_root,'tasks',enrollmentHash(c.task_id)+'.json')),claimed:exists(c.claim_file), | 
|  | +    canary_passed:canary.status==='pass'&&canary.synthetic===true&&canary.actual_continuations===1&&canary.actual_turn_trigger==='queue'&&canary.business_mutations===0&&canary.queue_empty===true&&canary.terminal_bindings_inactive===true&&Number.isFinite(at)&&at<=now&&now-at<=3600000, | 
|  | +    paths_present:[c.project_root,c.parent.cwd,c.state_root].every(p=>fs.statSync(p).isDirectory())&&fs.statSync(c.registration_script).isFile()}; | 
|  | +} | 
|  | +export async function runWatchTrigger({previous={},config_file,original_file,state_file,timeout_ms=10000}){ | 
|  | +  const deadline=Date.now()+Math.max(1,Math.min(10000,Number.isFinite(timeout_ms)?timeout_ms:10000)); | 
|  | +  const run=args=>{if(Date.now()>=deadline)throw Object.assign(Error('deadline'),{code:'ETIMEDOUT'});return execFileSync('herdr',args,{encoding:'utf8',timeout:Math.max(1,Math.min(3000,deadline-Date.now())),maxBuffer:2*1024*1024,stdio:['ignore','pipe','pipe']});}; | 
|  | +  let original,snapshot,observed_at; | 
|  | +  try{ | 
|  | +    const source=await import(pathToFileURL(original_file).href);snapshot=JSON.parse(run(['api','snapshot'])).result.snapshot;observed_at=new Date().toISOString(); | 
|  | +    original=source.evaluate(snapshot,previous,pane=>run(['agent','read',pane,'--source','recent-unwrapped','--lines','100'])); | 
|  | +    if(previous.failure){original.fire=true;original.message=JSON.stringify({recovery:'Herdr connection recovered',update:original.message});} | 
|  | +  }catch(error){ | 
|  | +    const failure=String(error.code\|\|error.status\|\|'read-failed'); | 
|  | +    return {fire:previous.failure!==failure,message:JSON.stringify({type:'monitor-error',reason:'Herdr 상태나 작업 내용을 읽지 못했습니다.',code:failure}),state:{...previous,failure}}; | 
|  | +  } | 
|  | +  let lock,owned=false; | 
|  | +  try{ | 
|  | +    if(Date.now()>=deadline\|\|![config_file,state_file].every(absolute))return original; | 
|  | +    const config=readJson(config_file);if(!mapping(config))return original; | 
|  | +    fs.mkdirSync(path.dirname(state_file),{recursive:true,mode:0o700});lock=state_file+'.lock'; | 
|  | +    // Fail closed on an existing/abandoned lock; never unlink another owner. | 
|  | +    fs.mkdirSync(lock,{mode:0o700});owned=true; | 
|  | +    const previousEmission=exists(state_file)?readJson(state_file):null; | 
|  | +    const result=evaluateEnrollment({config,snapshot,previous:previousEmission,now:Date.now(),observed_at,guards:guardsFor(config,Date.now())}); | 
|  | +    if(!result.event\|\|Date.now()>=deadline)return original; | 
|  | +    // Detection never claims delivery. Recheck revocation/binding/claim before | 
|  | +    // receipt publication; watcher must repeat these checks before its prompt. | 
|  | +    const latest=readJson(config_file); | 
|  | +    if(latest.state!=='armed'\|\|JSON.stringify(mapping(latest))!==JSON.stringify(mapping(config))\|\|guardsFor(latest,Date.now()).bound\|\|exists(latest.claim_file)\|\|Date.parse(latest.expires_at)<=Date.now())return original; | 
|  | +    const temporary=path.join(lock,'receipt.json');fs.writeFileSync(temporary,JSON.stringify(result.state)+'\n',{mode:0o600});fs.renameSync(temporary,state_file); | 
|  | +    return composeWatchResult(original,result.event); | 
|  | +  }catch{return original;}finally{if(owned){try{fs.rmdirSync(lock);}catch{}}} | 
|  | +} | 
|  | +async function main(){ | 
|  | +  const args=process.argv.slice(2);let previous={};if(args[0]&&!args[0].startsWith('--'))previous=JSON.parse(args.shift()); | 
|  | +  const values={};while(args.length){const flag=args.shift(),value=args.shift();if(!['--config','--original','--state'].includes(flag)\|\|!value\|\|values[flag])throw Error('invalid_arguments');values[flag]=value;} | 
|  | +  if(!['--config','--original','--state'].every(k=>absolute(values[k])))throw Error('explicit_absolute_paths_required'); | 
|  | +  console.log(JSON.stringify(await runWatchTrigger({previous,config_file:values['--config'],original_file:values['--original'],state_file:values['--state']}))); | 
|  | +} | 
|  | +if(process.argv[1]&&fs.existsSync(process.argv[1])&&import.meta.url===pathToFileURL(fs.realpathSync(process.argv[1])).href)main().catch(()=>{console.error('continuation_watch_arguments_invalid');process.exitCode=1;}); | 
|  | diff --git a/scripts/execution.mjs b/scripts/execution.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..43ce0a7 | 
|  | --- /dev/null | 
|  | +++ b/scripts/execution.mjs | 
|  | @@ -0,0 +1,168 @@ | 
|  | +import fs from 'node:fs'; | 
|  | +import path from 'node:path'; | 
|  | +import crypto from 'node:crypto'; | 
|  | +import {spawnSync} from 'node:child_process'; | 
|  | + | 
|  | +const hash = value => crypto.createHash('sha256').update(value).digest('hex'); | 
|  | +const object = value => value && typeof value === 'object' && !Array.isArray(value); | 
|  | +const read = file => {try {return JSON.parse(fs.readFileSync(file,'utf8'));} catch {return null;}}; | 
|  | + | 
|  | +// Deliberately recognize only simple shell commands. Never execute a shell to | 
|  | +// discover its cwd, expand arguments, or treat quoted source text as a command. | 
|  | +export function commandArgs(command) { | 
|  | +  if(Array.isArray(command))return command.every(x=>typeof x==='string')?command:null; | 
|  | +  if(typeof command!=='string'\|\|!/^\s*[\w./=-]+(?:[ \t]+[\w./=-]+)*\s*$/.test(command))return null; | 
|  | +  return command.trim().split(/[ \t]+/); | 
|  | +} | 
|  | +export function gitPreflight(command,cwd) { | 
|  | +  const args=commandArgs(command); | 
|  | +  if(!args?.length\|\|path.basename(args[0])!=='git')return {status:'not_applicable'}; | 
|  | +  if(!cwd\|\|!path.isAbsolute(cwd))return {status:'unverified',reason:'effective_cwd_unavailable'}; | 
|  | +  let effective=cwd,i=1; | 
|  | +  while(args[i]==='-C'){ | 
|  | +    if(!args[i+1])return {status:'unverified',reason:'git_arguments_unrecognized'}; | 
|  | +    effective=path.resolve(effective,args[i+1]);i+=2; | 
|  | +  } | 
|  | +  const subcommand=args[i]; | 
|  | +  // These operations really require a repository. Other commands (init, clone, | 
|  | +  // rev-parse probes, --version, diff --no-index, aliases) keep Git's semantics. | 
|  | +  if(!['status','worktree','branch','log','show','commit','add','reset','checkout','switch','restore','merge','rebase','fetch','pull','push','tag','diff'].includes(subcommand)\|\| | 
|  | +    (subcommand==='diff'&&args.includes('--no-index')))return {status:'not_applicable'}; | 
|  | +  const result=spawnSync('git',['-C',effective,'rev-parse','--git-dir'],{encoding:'utf8',timeout:1500}); | 
|  | +  return result.status===0?{status:'pass',cwd:effective}:{status:'failed',cwd:effective,reason:'repository_precondition_unverified'}; | 
|  | +} | 
|  | + | 
|  | +// A deliberately small shell lexer: unknown shell syntax stays reviewable. | 
|  | +function shellStages(text) { | 
|  | +  const stages=[[]];let word='',started=false,quote=null; | 
|  | +  const flush=()=>{if(started){stages.at(-1).push(word);word='';started=false;}}; | 
|  | +  for(let i=0;i<text.length;i++){ | 
|  | +    const c=text[i]; | 
|  | +    if(quote){ | 
|  | +      if(c===quote){quote=null;continue;} | 
|  | +      if(quote==='"'&&/[$`\\]/.test(c))return null; | 
|  | +      word+=c;continue; | 
|  | +    } | 
|  | +    if(c==="'"\|\|c==='"'){quote=c;started=true;continue;} | 
|  | +    if(c==='\\'){if(i+1===text.length\|\|text[i+1]==='\n')return null;word+=text[++i];started=true;continue;} | 
|  | +    if(/[;&<>$`(){}\n\r#]/.test(c))return null; | 
|  | +    if(c==='\|'){if(text[i+1]==='\|')return null;flush();if(!stages.at(-1).length)return null;stages.push([]);continue;} | 
|  | +    if(/[ \t]/.test(c)){flush();continue;} | 
|  | +    word+=c;started=true; | 
|  | +  } | 
|  | +  if(quote)return null;flush(); | 
|  | +  return stages.every(s=>s.length)?stages:null; | 
|  | +} | 
|  | +function effectiveStages(command,depth=0) { | 
|  | +  if(depth>4)return null; | 
|  | +  const stages=Array.isArray(command)&&command.every(x=>typeof x==='string')?[command]: | 
|  | +    typeof command==='string'?shellStages(command):null; | 
|  | +  if(!stages\|\|stages.some(s=>!s.length))return null; | 
|  | +  if(stages.length===1&&['bash','zsh','sh'].includes(path.basename(stages[0][0]))){ | 
|  | +    const args=stages[0]; | 
|  | +    if(args.length!==3\|\|!['-lc','-c'].includes(args[1]))return null; | 
|  | +    return effectiveStages(args[2],depth+1); | 
|  | +  } | 
|  | +  // Do not mistake shell keywords, assignments or negation for simple stages. | 
|  | +  if(stages.some(s=>!/^[/\w.-]+$/.test(s[0])&&!['[','[['].includes(s[0])\|\| | 
|  | +    ['if','then','for','while','until','case','do','done','else','fi','!'].includes(s[0])))return null; | 
|  | +  return stages; | 
|  | +} | 
|  | +function commandProgram(command) { | 
|  | +  const args=effectiveStages(command)?.at(-1); | 
|  | +  if(!args)return null; | 
|  | +  const name=path.basename(args[0]); | 
|  | +  return name==='git'&&args[1]?`git ${args[1]}`:name; | 
|  | +} | 
|  | +function probeProgram(command) { | 
|  | +  const args=effectiveStages(command)?.at(-1); | 
|  | +  if(!args)return null; | 
|  | +  const name=path.basename(args[0]); | 
|  | +  if(['rg','grep','egrep','fgrep','test','diff','cmp'].includes(name))return name; | 
|  | +  if(name==='['&&args.at(-1)===']')return name; | 
|  | +  if(name==='[['&&args.at(-1)===']]')return name; | 
|  | +  if(name==='git'&&args[1]==='grep')return 'git grep'; | 
|  | +  if(name==='git'&&args[1]==='diff'&&args.slice(2,args.indexOf('--')<0?undefined:args.indexOf('--')).some(a=>['--exit-code','--quiet'].includes(a)))return 'git diff'; | 
|  | +  return null; | 
|  | +} | 
|  | + | 
|  | +// Bounded local evidence inspection. Only transport fields vote; printed | 
|  | +// stdout, exceptions and JSON-looking application data are never status facts. | 
|  | +export function scanExecutions(file,turnId,{maxBytes=8*1024*1024,deadline=Date.now()+500}={}) { | 
|  | +  const result={turn_id:turnId\|\|null,coverage:'unavailable',commands_observed:0,failures:[],excluded:[]}; | 
|  | +  if(!file\|\|!turnId)return result; | 
|  | +  let data,offset=0,fd; | 
|  | +  try { | 
|  | +    fd=fs.openSync(file,'r');const stat=fs.fstatSync(fd); | 
|  | +    if(!stat.isFile())return result; | 
|  | +    offset=Math.max(0,stat.size-maxBytes);const buffer=Buffer.alloc(Math.min(stat.size,maxBytes)); | 
|  | +    const bytes=fs.readSync(fd,buffer,0,buffer.length,offset);data=buffer.subarray(0,bytes).toString('utf8'); | 
|  | +    if(offset)data=data.slice(data.indexOf('\n')+1); | 
|  | +  }catch{return result;}finally{if(fd!==undefined)fs.closeSync(fd);} | 
|  | +  let current=null,sawStart=false,partial=false; | 
|  | +  const commands=new Map(); | 
|  | +  for(const raw of data.split('\n')){ | 
|  | +    if(Date.now()>deadline){partial=true;break;} | 
|  | +    if(!raw.trim())continue; | 
|  | +    let line;try{line=JSON.parse(raw);}catch{partial=true;continue;} | 
|  | +    if(line.type!=='event_msg')continue; | 
|  | +    const p=line.payload; | 
|  | +    if(!object(p))continue; | 
|  | +    if(p.type==='task_started'){current=p.turn_id;if(current===turnId)sawStart=true;} | 
|  | +    if((p.turn_id??current)!==turnId)continue; | 
|  | +    const item=p.type==='item_completed'&&p.item?.type==='CommandExecution'?p.item: | 
|  | +      p.type==='exec_command_end'?p:null; | 
|  | +    if(!item)continue; | 
|  | +    const id=item.id??item.call_id; | 
|  | +    if(typeof id!=='string'){partial=true;continue;} | 
|  | +    const code=item.exit_code; | 
|  | +    const program=probeProgram(item.command); | 
|  | +    const excluded=code===1&&program&&!item.signal&&!item.timed_out&& | 
|  | +      !['error','cancelled','canceled','timed_out'].includes(item.status); | 
|  | +    const failed=Boolean(item.signal\|\|item.timed_out)\|\|Number.isSafeInteger(code)&&code!==0\|\|['failed','error','cancelled','canceled','timed_out'].includes(item.status); | 
|  | +    const previous=commands.get(id)\|\|{id,exit_codes:[],failed:false,excluded:false,program:commandProgram(item.command)\|\|'unknown'}; | 
|  | +    if(Number.isSafeInteger(code)&&!previous.exit_codes.includes(code))previous.exit_codes.push(code); | 
|  | +    previous.failed \|\|= failed&&!excluded; | 
|  | +    previous.excluded \|\|= Boolean(excluded); | 
|  | +    if(excluded)previous.probe=program; | 
|  | +    commands.set(id,previous); | 
|  | +  } | 
|  | +  result.coverage=partial\|\|!sawStart?'partial':'current_turn'; | 
|  | +  result.commands_observed=commands.size; | 
|  | +  result.failures=[...commands.values()].filter(x=>x.failed).map(x=>({ | 
|  | +    id:x.id,exit_codes:x.exit_codes.sort((a,b)=>a-b), | 
|  | +    program:x.program, | 
|  | +    fingerprint:hash(JSON.stringify([turnId,x.id,x.exit_codes.sort((a,b)=>a-b)])), | 
|  | +  })); | 
|  | +  result.excluded=[...commands.values()].filter(x=>x.excluded&&!x.failed).map(x=>({ | 
|  | +    id:x.id,exit_codes:x.exit_codes.sort((a,b)=>a-b),program:x.probe,reason:'no_match_probe', | 
|  | +  })); | 
|  | +  return result; | 
|  | +} | 
|  | + | 
|  | +export function assessExecutionReview(execution,review) { | 
|  | +  const entries=review?.turn_id===execution.turn_id&&Array.isArray(review.failures)?review.failures:[]; | 
|  | +  const unresolved=execution.failures.filter(f=>!entries.some(r=>validReview(r)&&r.fingerprint===f.fingerprint)); | 
|  | +  const dispositions={recovered:0,unrelated:0,limited:0}; | 
|  | +  const excluded_by_program={},failing_by_program={}; | 
|  | +  for(const x of execution.excluded\|\|[])excluded_by_program[x.program]=(excluded_by_program[x.program]\|\|0)+1; | 
|  | +  for(const x of execution.failures)failing_by_program[x.program\|\|'unknown']=(failing_by_program[x.program\|\|'unknown']\|\|0)+1; | 
|  | +  for(const f of execution.failures){const r=entries.find(r=>validReview(r)&&r.fingerprint===f.fingerprint);if(r)dispositions[r.disposition]++;} | 
|  | +  return {status:unresolved.length?'required':execution.failures.length?'reviewed':execution.coverage==='unavailable'?'not_observed':'no_failures_observed', | 
|  | +    coverage:execution.coverage,commands_observed:execution.commands_observed,failure_count:execution.failures.length, | 
|  | +    unresolved_count:unresolved.length,dispositions,excluded_by_program,failing_by_program,assessment_source:'observed_exit_fields_with_agent_reported_review'}; | 
|  | +} | 
|  | +function validReview(r){return object(r)&&['recovered','unrelated','limited'].includes(r.disposition)&&typeof r.reason==='string'&&r.reason.trim()&& | 
|  | +  Array.isArray(r.evidence)&&r.evidence.length>0&&r.evidence.every(x=>typeof x==='string'&&x.trim());} | 
|  | +export function validateExecutionReview(execution,review){ | 
|  | +  if(!execution?.turn_id\|\|review?.turn_id!==execution.turn_id\|\|!Array.isArray(review.failures))throw Error('Review requires the current execution turn_id and failures array.'); | 
|  | +  const seen=new Set(); | 
|  | +  for(const entry of review.failures){ | 
|  | +    if(!validReview(entry)\|\|seen.has(entry.fingerprint)\|\|!execution.failures.some(f=>f.fingerprint===entry.fingerprint)) | 
|  | +      throw Error('Each failure review needs its exact fingerprint, disposition recovered\|unrelated\|limited, reason, and evidence references.'); | 
|  | +    seen.add(entry.fingerprint); | 
|  | +  } | 
|  | +  return review; | 
|  | +} | 
|  | +export const executionFiles=(dir,turn)=>({execution:path.join(dir,'executions',`${hash(turn)}.json`),review:path.join(dir,'execution-reviews',`${hash(turn)}.json`)}); | 
|  | +export function executionReview(dir,execution){return assessExecutionReview(execution,read(executionFiles(dir,execution.turn_id).review));} | 
|  | diff --git a/scripts/harness.mjs b/scripts/harness.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..471eec2 | 
|  | --- /dev/null | 
|  | +++ b/scripts/harness.mjs | 
|  | @@ -0,0 +1,851 @@ | 
|  | +#!/usr/bin/env node | 
|  | +import fs from 'node:fs'; | 
|  | +import path from 'node:path'; | 
|  | +import os from 'node:os'; | 
|  | +import crypto from 'node:crypto'; | 
|  | +import net from 'node:net'; | 
|  | +import {spawn, spawnSync} from 'node:child_process'; | 
|  | +import {fileURLToPath} from 'node:url'; | 
|  | +import {createTaskRuntime as taskRuntime} from './task-loop.mjs'; | 
|  | +export {createCodexQueue} from './task-loop.mjs'; | 
|  | +import {validateVerificationContract, validateRevision, assessVerification, runVerification} from './verification.mjs'; | 
|  | +import {gitPreflight, scanExecutions, executionFiles, executionReview, validateExecutionReview} from './execution.mjs'; | 
|  | +import {herdrRequest, selectHerdrIdentity} from './herdr-identity.mjs'; | 
|  | +import {bindHerdrSession, restoreHerdrBinding, processStart} from './herdr-binding.mjs'; | 
|  | + | 
|  | +export const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); | 
|  | +export const HOME = process.env.CODEX_HOME \|\| path.join(os.homedir(), '.codex'); | 
|  | +export const STATE = process.env.HARNESS_STATE_DIR \|\| path.join(HOME, 'harness-runtime'); | 
|  | +export const digest = value => crypto.createHash('sha256').update(value).digest('hex'); | 
|  | +export const sessionDir = id => path.join(STATE, 'sessions', digest(String(id))); | 
|  | +export const createTaskRuntime = options => taskRuntime({state:STATE,...options}); | 
|  | +const read = (file, fallback=null) => {try {return JSON.parse(fs.readFileSync(file,'utf8'));} catch {return fallback;}}; | 
|  | +export function write(file, data) { | 
|  | +  fs.mkdirSync(path.dirname(file),{recursive:true,mode:0o700}); | 
|  | +  const tmp=`${file}.${process.pid}.${crypto.randomUUID()}.tmp`; | 
|  | +  fs.writeFileSync(tmp,JSON.stringify(data,null,2)+'\n',{mode:0o600});fs.renameSync(tmp,file); | 
|  | +} | 
|  | +export function collectReport(state=STATE,{since=7,now=Date.now(),maxFiles=50000,maxBytes=64*1024*1024,deadline=Date.now()+3000}={}) { | 
|  | +  if(!Number.isFinite(since)\|\|since<0)throw Error('--since requires nonnegative days.'); | 
|  | +  const report={since_days:since,from:new Date(now-since*86400000).toISOString(),to:new Date(now).toISOString(), | 
|  | +    turns_total:0,turns_warned:0,turns_blocked:0,turns_without_stop_action:0, | 
|  | +    unresolved_failure_count:0,excluded_probe_count:0,excluded_probes_by_program:{}, | 
|  | +    top_failing_programs:[],sample_turns:[],skipped_files:0,files_examined:0,truncated:false}; | 
|  | +  const failing={},samples=[];let bytes=0; | 
|  | +  const count=n=>Number.isSafeInteger(n)&&n>=0?n:0; | 
|  | +  const add=(target,values)=>{for(const [key,n] of Object.entries(values\|\|{}))target[key]=(target[key]\|\|0)+count(n);}; | 
|  | +  const sessions=path.join(state,'sessions');let dirs; | 
|  | +  try{dirs=fs.opendirSync(sessions);}catch(error){if(error.code!=='ENOENT')report.skipped_files++;return report;} | 
|  | +  try{ | 
|  | +    outer: for(let dir;(dir=dirs.readSync());){ | 
|  | +      if(Date.now()>deadline){report.truncated=true;break;} | 
|  | +      if(!dir.isDirectory())continue; | 
|  | +      let outcomes;try{outcomes=fs.opendirSync(path.join(sessions,dir.name,'outcomes'));}catch(error){if(error.code!=='ENOENT')report.skipped_files++;continue;} | 
|  | +      try{ | 
|  | +        for(let entry;(entry=outcomes.readSync());){ | 
|  | +          if(!entry.name.endsWith('.json'))continue; | 
|  | +          if(report.files_examined>=maxFiles\|\|bytes>=maxBytes\|\|Date.now()>deadline){report.truncated=true;break outer;} | 
|  | +          report.files_examined++; | 
|  | +          const file=path.join(sessions,dir.name,'outcomes',entry.name); | 
|  | +          let outcome; | 
|  | +          try{ | 
|  | +            const stat=fs.lstatSync(file); | 
|  | +            if(!stat.isFile()\|\|stat.size>128*1024)throw Error('Record size or type'); | 
|  | +            if(bytes+stat.size>maxBytes){report.truncated=true;break outer;} | 
|  | +            bytes+=stat.size;outcome=JSON.parse(fs.readFileSync(file,'utf8')); | 
|  | +            if(!outcome\|\|!Number.isFinite(Date.parse(outcome.at)))throw Error('Record timestamp'); | 
|  | +          }catch{report.skipped_files++;continue;} | 
|  | +          const at=Date.parse(outcome.at); | 
|  | +          if(at<now-since*86400000\|\|at>now)continue; | 
|  | +          report.turns_total++; | 
|  | +          if(outcome.stop_action==='warned')report.turns_warned++; | 
|  | +          else if(outcome.stop_action==='blocked')report.turns_blocked++; | 
|  | +          else if(!outcome.stop_action)report.turns_without_stop_action++; | 
|  | +          const execution=outcome.execution_review\|\|{}; | 
|  | +          report.unresolved_failure_count+=count(execution.unresolved_count); | 
|  | +          report.excluded_probe_count+=count(outcome.excluded_probe_count); | 
|  | +          add(report.excluded_probes_by_program,execution.excluded_by_program); | 
|  | +          add(failing,execution.failing_by_program); | 
|  | +          const known=Object.values(execution.failing_by_program\|\|{}).reduce((sum,n)=>sum+count(n),0); | 
|  | +          if(count(execution.failure_count)>known)failing.unknown=(failing.unknown\|\|0)+count(execution.failure_count)-known; | 
|  | +          samples.push({turn_id:outcome.turn_id\|\|null,transcript_path:outcome.transcript_path\|\|null, | 
|  | +            outcome_path:file,at:outcome.at,stop_action:outcome.stop_action\|\|null,unresolved_count:count(execution.unresolved_count)}); | 
|  | +          samples.sort((a,b)=>Number(Boolean(b.unresolved_count))-Number(Boolean(a.unresolved_count))\|\|Date.parse(b.at)-Date.parse(a.at)); | 
|  | +          samples.length=Math.min(samples.length,10); | 
|  | +        } | 
|  | +      }finally{outcomes.closeSync();} | 
|  | +    } | 
|  | +  }finally{dirs.closeSync();} | 
|  | +  report.top_failing_programs=Object.entries(failing).map(([program,count])=>({program,count})).sort((a,b)=>b.count-a.count\|\|a.program.localeCompare(b.program)).slice(0,10); | 
|  | +  report.sample_turns=samples; | 
|  | +  return report; | 
|  | +} | 
|  | +export function formatReport(report) { | 
|  | +  const programs=values=>Object.entries(values).map(([program,count])=>`${program}: ${count}`).join(', ')\|\|'none recorded'; | 
|  | +  return [`# Harness evidence report`,`${report.from} through ${report.to}`, | 
|  | +    `Turns: ${report.turns_total}; warned: ${report.turns_warned}; blocked: ${report.turns_blocked}; missing historical stop action: ${report.turns_without_stop_action}`, | 
|  | +    `Unresolved failures: ${report.unresolved_failure_count}; excluded probes: ${report.excluded_probe_count}`, | 
|  | +    `Excluded probes by program: ${programs(report.excluded_probes_by_program)}`, | 
|  | +    `Top failing programs: ${programs(Object.fromEntries(report.top_failing_programs.map(x=>[x.program,x.count])))}`, | 
|  | +    `Files examined: ${report.files_examined}; skipped unreadable/invalid files or directories: ${report.skipped_files}; truncated: ${report.truncated}`, | 
|  | +    `## Sample turns (unresolved first, then newest; missing historical fields remain unknown)`, | 
|  | +    ...report.sample_turns.map(x=>`- turn_id: ${x.turn_id}; unresolved: ${x.unresolved_count}; stop_action: ${x.stop_action??'unknown'}\n  transcript: ${x.transcript_path??'not recorded'}\n  outcome: ${x.outcome_path}`)].join('\n')+'\n'; | 
|  | +} | 
|  | +function git(cwd,...args) { | 
|  | +  const r=spawnSync('git',['-C',cwd,...args],{encoding:'utf8',timeout:2000}); | 
|  | +  return r.status===0?r.stdout.trim():null; | 
|  | +} | 
|  | +export function context(cwd) { | 
|  | +  cwd=fs.realpathSync(cwd); | 
|  | +  const root=git(cwd,'rev-parse','--show-toplevel'); | 
|  | +  const executable={}; | 
|  | +  for(const tool of ['node','python3','pnpm','herdr','codex']) { | 
|  | +    const dirs=(process.env.PATH\|\|'').split(path.delimiter); | 
|  | +    executable[tool]=dirs.map(d=>path.join(d,tool)).find(p=>{try{fs.accessSync(p,fs.constants.X_OK);return true;}catch{return false;}})\|\|null; | 
|  | +  } | 
|  | +  const lf=read(path.join(HOME,'langfuse-masked.json')); | 
|  | +  return {cwd,repository_root:root,git_dir:root?git(root,'rev-parse','--absolute-git-dir'):null,git_common_dir:root?git(root,'rev-parse','--path-format=absolute','--git-common-dir'):null, | 
|  | +    branch:root?git(root,'branch','--show-current'):null,commit:root?git(root,'rev-parse','HEAD'):null, | 
|  | +    executables:executable,connections:{langfuse:{configured:Boolean(lf?.public_key&&lf?.secret_key),base_url:lf?.base_url\|\|null}},captured_at:new Date().toISOString()}; | 
|  | +} | 
|  | +function fileHash(file) {try {return {path:file,sha256:digest(fs.readFileSync(file))};}catch{return null;}} | 
|  | +function ancestors(cwd) { | 
|  | +  const dirs=[];let dir=path.resolve(cwd); | 
|  | +  while(true){dirs.push(dir);const parent=path.dirname(dir);if(parent===dir)break;dir=parent;} | 
|  | +  return dirs; | 
|  | +} | 
|  | +function instructionFiles(cwd) { | 
|  | +  return [...new Set([path.join(HOME,'AGENTS.md'),...ancestors(cwd).map(dir=>path.join(dir,'AGENTS.md'))])]; | 
|  | +} | 
|  | +function stamp(file) { | 
|  | +  try {const s=fs.statSync(file);return [file,s.ino,s.size,s.mtimeMs,s.ctimeMs];}catch{return [file,null];} | 
|  | +} | 
|  | +function environmentStamp(cwd,ctx) { | 
|  | +  const markers=ancestors(cwd).map(dir=>{ | 
|  | +    const file=path.join(dir,'.git'); | 
|  | +    try{return fs.statSync(file).isDirectory()?[file,'directory']:stamp(file);}catch{return [file,null];} | 
|  | +  }); | 
|  | +  const gitFiles=[]; | 
|  | +  if(ctx?.git_dir){ | 
|  | +    const head=path.join(ctx.git_dir,'HEAD');gitFiles.push(head,path.join(ctx.git_dir,'commondir')); | 
|  | +    try {const ref=fs.readFileSync(head,'utf8').trim().match(/^ref: (refs\/.+)$/)?.[1]; | 
|  | +      if(ref)gitFiles.push(path.join(ctx.git_common_dir\|\|ctx.git_dir,ref)); | 
|  | +    }catch{} | 
|  | +    gitFiles.push(path.join(ctx.git_common_dir\|\|ctx.git_dir,'packed-refs')); | 
|  | +  } | 
|  | +  // Directory metadata catches new/replaced executables without rescanning PATH. | 
|  | +  const dirs=(process.env.PATH\|\|'').split(path.delimiter); | 
|  | +  return digest(JSON.stringify([cwd,markers,gitFiles.map(stamp),dirs.map(stamp),stamp(path.join(HOME,'langfuse-masked.json'))])); | 
|  | +} | 
|  | +export function cachedContext(dir,cwd,{force=false}={}) { | 
|  | +  cwd=fs.realpathSync(cwd); | 
|  | +  const file=path.join(dir,'context.json'),old=read(file),cache=read(path.join(dir,'context-cache.json')); | 
|  | +  if(!force&&old?.cwd===cwd&&cache?.version===1&&cache.stamp===environmentStamp(cwd,old))return {value:old,refreshed:false}; | 
|  | +  const value=context(cwd);write(file,value); | 
|  | +  write(path.join(dir,'context-cache.json'),{version:1,stamp:environmentStamp(cwd,value)}); | 
|  | +  return {value,refreshed:true}; | 
|  | +} | 
|  | +export function snapshot(cwd) { | 
|  | +  return {captured_at:new Date().toISOString(),scope:'configuration_files_at_turn_start_not_full_prompt', | 
|  | +    instruction_files:instructionFiles(cwd).map(fileHash).filter(Boolean),available_skill_files:[],remote_skill_references:[],remote_skill_reference_scope:'routing_only_not_load_evidence', | 
|  | +    runtime_version:read(path.join(ROOT,'.codex-plugin','plugin.json'))?.version}; | 
|  | +} | 
|  | +export function cachedSnapshot(dir,cwd,turn) { | 
|  | +  const file=path.join(dir,'instruction-cache.json'); | 
|  | +  const fingerprint=digest(JSON.stringify([cwd,...instructionFiles(cwd).map(stamp),stamp(path.join(ROOT,'.codex-plugin','plugin.json'))])); | 
|  | +  let cache=read(file); | 
|  | +  const reused=cache?.stamp===fingerprint; | 
|  | +  if(!reused){cache={stamp:fingerprint,value:snapshot(cwd)};write(file,cache);} | 
|  | +  const target=path.join(dir,'snapshots',`${digest(String(turn\|\|'session'))}.json`); | 
|  | +  if(read(target)?.cache_stamp!==fingerprint)write(target,{...cache.value,cache_stamp:fingerprint,cache_reused:reused,cache_validated_at:new Date().toISOString()}); | 
|  | +  return {value:cache.value,reused}; | 
|  | +} | 
|  | +export function validateContract(c) { | 
|  | +  if(c?.schema_version===2)return validateVerificationContract(c); | 
|  | +  if(!c\|\|c.schema_version!==1\|\|typeof c.objective!=='string'\|\|!c.objective.trim())throw Error('Contract requires schema_version:1 and objective.'); | 
|  | +  if(!['in_progress','complete','partial','blocked','interrupted'].includes(c.status))throw Error('Invalid contract status.'); | 
|  | +  if(!Array.isArray(c.requirements)\|\|!Array.isArray(c.remaining))throw Error('requirements and remaining must be arrays.'); | 
|  | +  const ids=new Set(); | 
|  | +  for(const r of c.requirements){ | 
|  | +    if(!r.id\|\|ids.has(r.id)\|\|typeof r.text!=='string'\|\|!r.text.trim())throw Error('Requirements need unique IDs and text.');ids.add(r.id); | 
|  | +    if(!['pending','done','blocked','superseded'].includes(r.status))throw Error('Invalid requirement status.'); | 
|  | +    if(!Array.isArray(r.evidence)\|\|r.evidence.some(x=>typeof x!=='string'\|\|!x.trim()))throw Error('Evidence must be nonempty references.'); | 
|  | +    if(r.count){if(!Number.isFinite(r.count.target)\|\|!Number.isFinite(r.count.actual)\|\|r.count.target<0\|\|r.count.actual<0\|\|!r.count.unit)throw Error('Counts need nonnegative target, actual, and unit.');} | 
|  | +  } | 
|  | +  return c; | 
|  | +} | 
|  | +export function checkContract(c,dir,{deadline=Infinity}={}) { | 
|  | +  validateContract(c);const issues=[]; | 
|  | +  // Partial or blocked work must remain reportable; this is not a permission gate. | 
|  | +  if(c.status!=='complete')return issues; | 
|  | +  if(c.schema_version===2)return dir?assessVerification(c,dir,{deadline}).issues:['Observed verification requires a session evidence directory.']; | 
|  | +  if(!c.requirements.length)issues.push('No completion requirements recorded.'); | 
|  | +  if(c.remaining.length)issues.push('Remaining work exists despite complete status.'); | 
|  | +  for(const r of c.requirements){ | 
|  | +    if(r.status==='superseded'){if(!r.decision_basis)issues.push(`${r.id}: superseded without decision basis`);continue;} | 
|  | +    if(r.status!=='done')issues.push(`${r.id}: requirement is ${r.status}`); | 
|  | +    if(!r.evidence.length)issues.push(`${r.id}: missing evidence`); | 
|  | +    if(r.count&&r.count.actual<r.count.target)issues.push(`${r.id}: ${r.count.actual}/${r.count.target} ${r.count.unit}`); | 
|  | +    for(const v of r.verification\|\|[]){ | 
|  | +      if(v.required!==false&&(v.result!=='pass'\|\|!v.evidence))issues.push(`${r.id}: required verification ${v.id\|\|''} is not evidenced pass`); | 
|  | +    } | 
|  | +  } | 
|  | +  if(c.deployment?.requested&&(!c.deployment.commit\|\|!c.deployment.evidence\|\|c.deployment.status!=='deployed'))issues.push('Requested deployment has no deployed commit/evidence.'); | 
|  | +  return issues; | 
|  | +} | 
|  | +export function setContract(id,c) { | 
|  | +  validateContract(c);const dir=sessionDir(id);const old=read(path.join(dir,'contract.json')); | 
|  | +  if(c.schema_version===2){ | 
|  | +    validateRevision(old,c); | 
|  | +    const issues=checkContract(c,dir); | 
|  | +    if(issues.length)throw Error(`Completion is unverified: ${issues.join('; ')}`); | 
|  | +  } | 
|  | +  if(old)write(path.join(dir,'history',`${Date.now()}-${digest(JSON.stringify(old)).slice(0,12)}.json`),old); | 
|  | +  const turn=read(path.join(dir,'session.json'))?.turn_id;const saved={...c,assessment_turn_id:turn\|\|null,updated_at:new Date().toISOString(),assessment_source:c.schema_version===2?'observed_commands_and_declared_assertions':'agent_reported_not_independent_verification'}; | 
|  | +  write(path.join(dir,'contract.json'),saved);if(turn)write(path.join(dir,'contracts',`${digest(String(turn))}.json`),{...saved,turn_id:turn});return saved; | 
|  | +} | 
|  | +const promptContext = (event,text) => ({hookSpecificOutput:{hookEventName:event,additionalContext:text}}); | 
|  | +function signature(pid) { | 
|  | +  const r=spawnSync('ps',['-p',String(pid),'-o','lstart=','-o','command='],{encoding:'utf8',timeout:1000}); | 
|  | +  return r.status===0?digest(r.stdout.trim()):null; | 
|  | +} | 
|  | +export function stopOwned(record, reason='normal_cleanup') { | 
|  | +  if(!record?.pid\|\|!record.signature\|\|signature(record.pid)!==record.signature)return {stopped:false,reason:'not_owned_or_exited'}; | 
|  | +  // Only processes started by this runtime are registered. PID reuse is checked. | 
|  | +  try {process.kill(-record.pid,'SIGTERM');return {stopped:true,reason};}catch(e){return {stopped:false,reason:e.code\|\|'stop_failed'};} | 
|  | +} | 
|  | +function socketPath(){return process.env.HERDR_ENV === '1' && process.env.HERDR_PANE_ID ? process.env.HERDR_SOCKET_PATH : undefined;} | 
|  | +function startMonitor(id,cwd) { | 
|  | +  if(!socketPath() \|\| !fs.existsSync(socketPath()))return {state:'unavailable'}; | 
|  | +  const dir=sessionDir(id), file=path.join(dir,'monitor.json'),old=read(file); | 
|  | +  if(old?.pid&&signature(old.pid)===old.signature){ | 
|  | +    if(old.runtime===ROOT&&old.cwd===cwd&&old.socket===socketPath()&&old.lifecycle?.state!=='stopped')return {...old,state:old.lifecycle?.state\|\|'starting'}; | 
|  | +    stopOwned(old,'runtime_or_cwd_changed'); | 
|  | +  } | 
|  | +  fs.mkdirSync(dir,{recursive:true,mode:0o700}); | 
|  | +  // Exclusive lock prevents parallel SessionStart/UserPromptSubmit from duplicating a subscription. | 
|  | +  let lock;try{lock=fs.openSync(path.join(dir,'monitor.lock'),'wx',0o600);}catch{return {state:'starting'};} | 
|  | +  try{ | 
|  | +    const child=spawn(process.execPath,[fileURLToPath(import.meta.url),'monitor',id,cwd,socketPath()],{detached:true,stdio:'ignore'}); | 
|  | +    const record={pid:child.pid,signature:signature(child.pid),created_at:new Date().toISOString(),owner:id,cwd,runtime:ROOT,socket:socketPath()}; | 
|  | +    write(file,record);child.unref();return {...record,state:'starting'}; | 
|  | +  }finally{fs.closeSync(lock);fs.unlinkSync(path.join(dir,'monitor.lock'));} | 
|  | +} | 
|  | +const monitorTypes=['workspace_closed','worktree_removed','pane_exited','tab_created','tab_closed','tab_renamed','tab_moved','pane_created','pane_closed','pane_moved','pane_agent_detected','pane_updated','pane_agent_status_changed']; | 
|  | +const monitorStates=new Set(['idle','working','blocked','done','unknown']); | 
|  | +const monitorId=value=>typeof value==='string'&&value.length>0&&value.length<=200; | 
|  | +// Topology is evidence of tab membership, never authority to prompt an agent. | 
|  | +function monitorMembers(snapshot) { | 
|  | +  const members=new Map(),duplicates=new Set(),seenAgents=new Set(); | 
|  | +  const panes=Array.isArray(snapshot?.panes)?snapshot.panes:Array.isArray(snapshot?.agents)?snapshot.agents:[]; | 
|  | +  for(const p of panes){ | 
|  | +    if(!monitorId(p?.pane_id)\|\|!monitorId(p.tab_id)\|\|!monitorId(p.terminal_id)\|\|!monitorId(p.workspace_id))continue; | 
|  | +    if(members.has(p.pane_id)){duplicates.add(p.pane_id);continue;} | 
|  | +    members.set(p.pane_id,{pane_id:p.pane_id,tab_id:p.tab_id,terminal_id:p.terminal_id,workspace_id:p.workspace_id}); | 
|  | +  } | 
|  | +  for(const p of Array.isArray(snapshot?.agents)?snapshot.agents:[]){ | 
|  | +    if(!p\|\|typeof p!=='object')continue; | 
|  | +    if(seenAgents.has(p.pane_id))duplicates.add(p.pane_id); | 
|  | +    seenAgents.add(p.pane_id); | 
|  | +    const m=members.get(p.pane_id); | 
|  | +    if(m&&p.tab_id===m.tab_id&&p.terminal_id===m.terminal_id&&p.agent_session?.kind==='id')m.session_id=p.agent_session.value; | 
|  | +  } | 
|  | +  for(const p of duplicates)members.delete(p); | 
|  | +  return members; | 
|  | +} | 
|  | +const sameMonitorMember=(a,b)=>Boolean(a&&b&&['pane_id','tab_id','workspace_id','terminal_id','session_id'].every(k=>a[k]===b[k])); | 
|  | +function monitorScope(id,binding,identity,verified,members) { | 
|  | +  const exact=x=>{ | 
|  | +    const m=members.get(x?.pane_id); | 
|  | +    return m&&m.session_id===x.session_id&&m.tab_id===x.tab_id&&(!x.terminal_id\|\|m.terminal_id===x.terminal_id); | 
|  | +  }; | 
|  | +  const goal=binding?.goal?.tab_id; | 
|  | +  if(goal && binding.mode==='observation_only'){ | 
|  | +    // An external observer may live elsewhere. Its active exact assignment, | 
|  | +    // freshly verified in the goal tab, establishes observation-only scope. | 
|  | +    const anchor=verified.find(x=>exact(x)&&binding.identities.some(e=>e.session_id===x.session_id&&e.pane_id===x.pane_id&&e.cwd===x.cwd)); | 
|  | +    return anchor?{task_id:binding.task_id,mode:binding.mode,tab_id:anchor.tab_id,workspace_id:members.get(anchor.pane_id).workspace_id}:null; | 
|  | +  } | 
|  | +  const owner=identity.status==='verified'&&identity.identity; | 
|  | +  if(!owner\|\|owner.session_id!==id\|\|!exact(owner))return null; | 
|  | +  return {task_id:binding?.task_id\|\|null,mode:binding?.mode\|\|null,tab_id:owner.tab_id,workspace_id:members.get(owner.pane_id).workspace_id}; | 
|  | +} | 
|  | +const monitorRevalidationMs=1000; | 
|  | +export async function resolveMonitorIdentity({sessionId,snapshot,socket,request=herdrRequest,stamp=processStart,paneHint,repair=true}) { | 
|  | +  const file=path.join(sessionDir(sessionId),'herdr-binding.json'),binding=read(file); | 
|  | +  const persist=proof=>write(file,proof); | 
|  | +  let initial=true; | 
|  | +  const reuse=(target,method,params)=>{ | 
|  | +    if(initial&&method==='session.snapshot'){initial=false;return Promise.resolve({result:{snapshot}});} | 
|  | +    return request(target,method,params); | 
|  | +  }; | 
|  | +  if(binding)return restoreHerdrBinding({sessionId,binding,socket,request:reuse,stamp,repair,persist}); | 
|  | +  const selected=selectHerdrIdentity(snapshot,{sessionId,paneHint}); | 
|  | +  if(selected.status!=='verified')return selected; | 
|  | +  const agent=snapshot.agents.find(a=>a.pane_id===selected.identity.pane_id); | 
|  | +  if(agent.agent_session.source!=='herdr:codex-client')return selected; | 
|  | +  try{ | 
|  | +    const result=await bindHerdrSession({sessionId,paneId:agent.pane_id,socket,request:reuse,stamp,persist,requireClient:true}); | 
|  | +    return {...selected,identity:result.identity}; | 
|  | +  }catch{return {status:'identity_unresolved',reason:'client_binding_unverified'};} | 
|  | +} | 
|  | +async function monitorEvidence(id,tasks,socket) { | 
|  | +  // One budget for the whole hook read, independent of the number of workers. | 
|  | +  const deadline=Date.now()+monitorRevalidationMs; | 
|  | +  const request=(target,method,params={})=>{ | 
|  | +    const remaining=deadline-Date.now(); | 
|  | +    if(remaining<=0)throw Error('monitor_revalidation_deadline'); | 
|  | +    return herdrRequest(target,method,params,{timeoutMs:remaining}); | 
|  | +  }; | 
|  | +  const response=await request(socket,'session.snapshot'),snapshot=response.result?.snapshot; | 
|  | +  const binding=tasks.monitorBinding(id),goal=binding?.goal?.tab_id; | 
|  | +  const options={snapshot,socket,request,repair:false,stamp:pid=>{ | 
|  | +    if(deadline-Date.now()<500)throw Error('monitor_revalidation_deadline'); | 
|  | +    return processStart(pid); | 
|  | +  }}; | 
|  | +  let identity=selectHerdrIdentity(snapshot,{sessionId:id}),anchor=null; | 
|  | +  if(goal && binding.mode==='observation_only'){ | 
|  | +    // The snapshot verifies all membership in memory. Only the scope anchor | 
|  | +    // needs persisted process-proof checks; unrelated workers cost no RPCs. | 
|  | +    for(const expected of binding.identities){ | 
|  | +      const live=selectHerdrIdentity(snapshot,{sessionId:expected.session_id}); | 
|  | +      if(live.status==='verified'&&live.identity.pane_id===expected.pane_id&&live.identity.cwd===expected.cwd&&(!expected.tab_id\|\|live.identity.tab_id===expected.tab_id)){anchor=live;break;} | 
|  | +    } | 
|  | +    if(anchor)anchor=await resolveMonitorIdentity({sessionId:anchor.identity.session_id,...options}); | 
|  | +  }else{ | 
|  | +    identity=await resolveMonitorIdentity({sessionId:id,...options}); | 
|  | +    if(identity.status==='verified')anchor=identity; | 
|  | +  } | 
|  | +  if(Date.now()>=deadline)throw Error('monitor_revalidation_deadline'); | 
|  | +  return {snapshot,binding,identity,verified:anchor?.status==='verified'?[anchor.identity]:[]}; | 
|  | +} | 
|  | +export function createMonitorEvents({id,tasks,send,onEvent,onCoverage=()=>{}}) { | 
|  | +  let paneKey=null,bindingKey=null,requestId=null,acknowledged=false,ackConsumed=false,serial=0,closed=false,pending=false,running=null,requestedPanes=[],acknowledgedPanes=[]; | 
|  | +  let identity={status:'identity_unresolved',reason:'not_checked'},members=new Map(),verified=[]; | 
|  | +  let eligiblePanes=null; | 
|  | +  const previous=new Map(),lifecycle=new Map(),invalidated=new Map();let invalidSerial=0; | 
|  | +  const ownerRequired=b=>!b\|\|b.mode!=='observation_only'\|\|b.identities.some(x=>x.session_id===id); | 
|  | +  const coverage=()=>{ | 
|  | +    const b=tasks.monitorBinding(id),unknown=(b?.coverage\|\|[]).filter(x=>x.identity_status!=='verified'\|\|invalidated.has(x.pane_id)); | 
|  | +    const registered=b?.identities\|\|[],unbound=b?.unbound_panes\|\|[]; | 
|  | +    const covered=registered.filter(x=>acknowledged&&acknowledgedPanes.includes(x.pane_id)&&!invalidated.has(x.pane_id)&&(!eligiblePanes\|\|eligiblePanes.has(x.pane_id))); | 
|  | +    const selfVerified=identity.status==='verified'&&acknowledged&&acknowledgedPanes.includes(identity.identity?.pane_id); | 
|  | +    const status=!b?(selfVerified?'unbound':'partial'):(ownerRequired(b)&&!selfVerified)\|\|covered.length!==registered.length\|\|unknown.length\|\|unbound.length\|\|!registered.length?'partial':'complete'; | 
|  | +    const scope=monitorScope(id,b,identity,verified,members); | 
|  | +    const result={task_id:b?.task_id\|\|null,status,identity,notification_scope:scope,owner_required:ownerRequired(b),subscription_acknowledged:acknowledged,subscription_request_id:requestId,acknowledged_panes:acknowledged?acknowledgedPanes:[],registered_identities:registered,covered_identities:covered,unknown_identities:unknown,unbound_panes:unbound,captured_at:new Date().toISOString()}; | 
|  | +    onCoverage(result);return result; | 
|  | +  }; | 
|  | +  const reconcile=()=>{ | 
|  | +    const binding=tasks.monitorBinding(id); | 
|  | +    if(closed\|\|!acknowledged\|\|!binding\|\|(ownerRequired(binding)&&identity.status!=='verified'))return; | 
|  | +    pending=true;if(running)return; | 
|  | +    running=(async()=>{while(pending&&!closed){pending=false;const invalidBefore=new Map(invalidated);try{await tasks.reconcileSession(id);for(const [p,g] of invalidBefore)if(invalidated.get(p)===g)invalidated.delete(p);}catch{}coverage();}})().finally(()=>{running=null;}); | 
|  | +  }; | 
|  | +  function refresh(){ | 
|  | +    if(closed)return; | 
|  | +    const binding=tasks.monitorBinding(id),identities=binding?.identities\|\|[]; | 
|  | +    const nextBinding=JSON.stringify([binding?.task_id,binding?.mode,binding?.goal,identities]); | 
|  | +    const scope=monitorScope(id,binding,identity,verified,members); | 
|  | +    const panes=[...new Set([identity.identity?.pane_id,...identities.map(x=>x.pane_id)])] | 
|  | +      .filter(p=>monitorId(p)&&(!eligiblePanes\|\|eligiblePanes.has(p))).sort(); | 
|  | +    const nextPanes=JSON.stringify(panes); | 
|  | +    const changedBinding=nextBinding!==bindingKey;bindingKey=nextBinding; | 
|  | +    if(changedBinding){lifecycle.clear();previous.clear();} | 
|  | +    if(nextPanes===paneKey){coverage();if(changedBinding)reconcile();return;} | 
|  | +    paneKey=nextPanes;acknowledged=false;ackConsumed=false;acknowledgedPanes=[];requestedPanes=panes;requestId='harness-monitor-'+(++serial); | 
|  | +    const subscriptions=monitorTypes.filter(t=>t!=='pane_agent_status_changed').map(t=>({type:t.replace('_','.')})); | 
|  | +    subscriptions.push(...panes.map(pane_id=>({type:'pane.agent_status_changed',pane_id}))); | 
|  | +    send({id:requestId,method:'events.subscribe',params:{subscriptions}}); | 
|  | +    coverage(); | 
|  | +  } | 
|  | +  function receive(msg){ | 
|  | +    if(closed\|\|!msg\|\|typeof msg!=='object'\|\|Array.isArray(msg))return; | 
|  | +    if(msg.id!==undefined){if(msg.id===requestId&&!ackConsumed){ackConsumed=true;acknowledged=!msg.error&&msg.result?.type==='subscription_started';acknowledgedPanes=acknowledged?[...requestedPanes]:[];coverage();if(acknowledged)reconcile();}return;} | 
|  | +    if(!acknowledged)return; | 
|  | +    const d=msg.data;if(!d\|\|typeof d!=='object'\|\|Array.isArray(d))return; | 
|  | +    const type=msg.event==='pane.agent_status_changed'?'pane_agent_status_changed':msg.event; | 
|  | +    if(!monitorTypes.includes(type)\|\|d.type!==undefined&&d.type!==type)return; | 
|  | +    if(msg.event!=='pane.agent_status_changed'&&d.type!==type)return; | 
|  | +    const p=d.pane;let paneId=p?.pane_id\|\|d.pane_id; | 
|  | +    if(type.startsWith('pane_')&&!monitorId(paneId))return; | 
|  | +    if(type==='pane_agent_status_changed'&&(!monitorStates.has(d.agent_status)\|\|!monitorId(d.workspace_id)))return; | 
|  | +    if(type.startsWith('tab_')&&!monitorId(d.tab?.tab_id\|\|d.tab_id))return; | 
|  | +    if(['pane_updated','pane_created','pane_moved'].includes(type)&&(!p\|\|typeof p!=='object'\|\|!monitorStates.has(p.agent_status)))return; | 
|  | +    const binding=tasks.monitorBinding(id),expected=binding?.identities.find(x=>x.pane_id===paneId),linked=Boolean(expected); | 
|  | +    const scope=monitorScope(id,binding,identity,verified,members),oldMember=members.get(d.previous_pane_id\|\|paneId); | 
|  | +    if(!p&&oldMember&&d.workspace_id&&d.workspace_id!==oldMember.workspace_id)return; | 
|  | +    if(type==='pane_moved'&&oldMember&&(d.previous_tab_id!==oldMember.tab_id\|\|d.previous_workspace_id!==oldMember.workspace_id))return; | 
|  | +    let member=oldMember; | 
|  | +    if(p?.tab_id){ | 
|  | +      const next=monitorMembers({panes:[p],agents:[p]}).get(paneId); | 
|  | +      // Preserve the known agent identity only while the same terminal survives. | 
|  | +      if(next&&!Object.hasOwn(p,'agent_session')&&oldMember?.terminal_id===next.terminal_id)next.session_id=oldMember.session_id; | 
|  | +      member=next; | 
|  | +      if(next)members.set(paneId,next);else members.delete(paneId); | 
|  | +    } | 
|  | +    if(type==='pane_moved'){ | 
|  | +      if(d.previous_pane_id!==paneId)members.delete(d.previous_pane_id); | 
|  | +      if(oldMember?.tab_id===scope?.tab_id)member=oldMember; | 
|  | +    } | 
|  | +    const tabId=p?.tab_id\|\|d.tab?.tab_id\|\|d.tab_id; | 
|  | +    const workspace=d.tab?.workspace_id\|\|d.workspace_id; | 
|  | +    const owned = ref => ref && [binding?.mode==='observation_only'?null:identity.identity,...(binding?.identities\|\|[])].some(x => | 
|  | +      x?.pane_id === ref.pane_id && x.session_id === ref.session_id && (!x.terminal_id \|\| x.terminal_id === ref.terminal_id)); | 
|  | +    const scoped = scope && (type.startsWith('tab_') | 
|  | +      ? [...members.values()].some(m=>m.tab_id===tabId && (!workspace\|\|m.workspace_id===workspace) && owned(m)) | 
|  | +      : owned(member) && (!workspace\|\|member.workspace_id===workspace)); | 
|  | +    const tabWorkspaceMatches=!workspace\|\|[...members.values()].some(m=>m.tab_id===tabId&&m.workspace_id===workspace); | 
|  | +    if(type==='tab_closed'&&tabWorkspaceMatches){ | 
|  | +      const affected=(binding?.identities\|\|[]).filter(x=>x.tab_id===tabId); | 
|  | +      if(affected.length){for(const x of affected)invalidated.set(x.pane_id,++invalidSerial);coverage();reconcile();} | 
|  | +    } | 
|  | +    if(type==='pane_closed'&&linked){invalidated.set(paneId,++invalidSerial);coverage();} | 
|  | +    if(type==='pane_closed')members.delete(paneId); | 
|  | +    if(type==='tab_closed'&&tabWorkspaceMatches)for(const [key,m] of members)if(m.tab_id===tabId)members.delete(key); | 
|  | +    const compact={type,pane:paneId,tab:member?.tab_id\|\|tabId,agent:p?.agent\|\|d.agent,status:p?.agent_status\|\|d.agent_status}; | 
|  | +    const hintedSession=p&&Object.hasOwn(p,'agent_session')?[p.agent_session?.kind\|\|null,p.agent_session?.value\|\|null]:['id',expected?.session_id]; | 
|  | +    const hint=linked?JSON.stringify([compact.status,compact.agent\|\|'codex',hintedSession,p?.cwd\|\|expected.cwd,p?.foreground_cwd\|\|expected.cwd,p?.tab_id\|\|expected.tab_id]):null; | 
|  | +    if(!scoped && !linked)return; | 
|  | +    const key=compact.pane\|\|compact.tab\|\|type,value=JSON.stringify([compact,hint]); | 
|  | +    if(previous.get(key)===value)return;previous.set(key,value);if(previous.size>256)previous.delete(previous.keys().next().value); | 
|  | +    if(scoped)onEvent({...compact,scope,member:type.startsWith('pane_')?member:undefined,...(type==='pane_moved'?{destination:members.get(paneId)}:{})}); | 
|  | +    if(linked){ | 
|  | +      // UI churn and the two different event envelopes must share one state | 
|  | +      // dedupe key. Event identity/state is only a hint; pump probes live Herdr. | 
|  | +      if(lifecycle.get(paneId)===hint)return;lifecycle.set(paneId,hint); | 
|  | +      reconcile(); | 
|  | +    } | 
|  | +  } | 
|  | +  return {refresh,receive,coverage,setIdentity:(next,panes,snapshot,identities=[])=>{ | 
|  | +    const before=monitorScope(id,tasks.monitorBinding(id),identity,verified,members); | 
|  | +    identity=next;eligiblePanes=new Set(panes);members=monitorMembers(snapshot);verified=identities; | 
|  | +    if(JSON.stringify(before)!==JSON.stringify(monitorScope(id,tasks.monitorBinding(id),identity,verified,members)))previous.clear(); | 
|  | +  },invalidate:()=>{identity={status:'identity_unresolved',reason:'subscription_target_changed'};members=new Map();verified=[];eligiblePanes=new Set();paneKey=null;acknowledged=false;coverage();},disconnected:()=>{acknowledged=false;ackConsumed=false;coverage();},tick:()=>{refresh();reconcile();},settled:async()=>{while(running)await running;},close:()=>{closed=true;pending=false;acknowledged=false;coverage();}}; | 
|  | +} | 
|  | +// Herdr reads exactly one request on a subscription connection. Subsequent | 
|  | +// bytes are treated as a disconnected peer, so pane-set updates replace the | 
|  | +// stream inside this monitor, never write a second request or start a new owner. | 
|  | +export function createMonitorSubscription({socket,onMessage,onDisconnect,onDisposition,onEnd,onRejected=()=>{},connect=net.createConnection,backoff_ms=250,max_reconnects=3,ack_timeout_ms=3000}) { | 
|  | +  let desired=null,current=null,retryTimer=null,closed=false,generation=0,reconnects=0,lastDisconnect=null,lastError=null; | 
|  | +  const disposition=(state,reason)=>onDisposition({state,reason,last_disconnect_reason:lastDisconnect,last_error:lastError,connection_generation:generation,reconnects,at:new Date().toISOString()}); | 
|  | +  function close(reason='shutdown'){ | 
|  | +    if(closed)return;closed=true;clearTimeout(retryTimer);retryTimer=null; | 
|  | +    if(current){clearTimeout(current.timer);current.connection.destroy();current=null;} | 
|  | +    disposition('stopped',reason); | 
|  | +  } | 
|  | +  function recover(reason){ | 
|  | +    if(closed)return; | 
|  | +    onDisconnect();lastDisconnect=reason;disposition('disconnected',reason); | 
|  | +    if(reconnects>=max_reconnects){close('reconnect_budget');onEnd('reconnect_budget');return;} | 
|  | +    reconnects++;disposition('reconnecting',reason); | 
|  | +    retryTimer=setTimeout(()=>{retryTimer=null;open();},Math.min(4000,backoff_ms*2**(reconnects-1))); | 
|  | +  } | 
|  | +  function open(){ | 
|  | +    if(closed\|\|current\|\|!desired)return; | 
|  | +    const slot={request:desired,buffer:'',acknowledged:false,rotating:false};generation++; | 
|  | +    try{slot.connection=connect(socket);}catch{recover('connection_error');return;} | 
|  | +    current=slot;disposition('connecting','subscription_opened'); | 
|  | +    const active=()=>!closed&¤t===slot&&!slot.rotating; | 
|  | +    slot.timer=setTimeout(()=>{if(active()){slot.reason='ack_timeout';slot.connection.destroy();}},ack_timeout_ms); | 
|  | +    slot.connection.on('connect',()=>{if(active())slot.connection.write(JSON.stringify(slot.request)+'\n');}); | 
|  | +    slot.connection.on('data',chunk=>{ | 
|  | +      if(!active())return;slot.buffer+=chunk.toString(); | 
|  | +      if(slot.buffer.length>1048576){slot.reason='response_overflow';slot.connection.destroy();return;} | 
|  | +      let pos;while(active()&&(pos=slot.buffer.indexOf('\n'))>=0){ | 
|  | +        const raw=slot.buffer.slice(0,pos);slot.buffer=slot.buffer.slice(pos+1);let msg;try{msg=JSON.parse(raw);}catch{continue;} | 
|  | +        // This connection has exactly one pending request. Herdr may return a | 
|  | +        // subscription-specific error ID (request:sub:0:probe), not our ID. | 
|  | +        if(slot.acknowledged && msg?.error?.code === 'events_lost'){ | 
|  | +          slot.reason = 'events_lost'; | 
|  | +          slot.connection.destroy(); | 
|  | +          return; | 
|  | +        } | 
|  | +        if(!slot.acknowledged&&msg?.error){ | 
|  | +          lastError={code:String(msg.error.code\|\|'subscription_rejected').slice(0,100),message:String(msg.error.message\|\|'Subscription rejected').slice(0,500)}; | 
|  | +          slot.reason=lastError.code;slot.rejected=true;slot.connection.destroy();return; | 
|  | +        } | 
|  | +        if(msg?.id===slot.request.id&&!slot.acknowledged){ | 
|  | +          if(msg.error\|\|msg.result?.type!=='subscription_started'){slot.reason='subscription_rejected';slot.connection.destroy();return;} | 
|  | +          slot.acknowledged=true;clearTimeout(slot.timer);disposition('subscribed','subscription_acknowledged'); | 
|  | +        } | 
|  | +        try{onMessage(msg);}catch{slot.reason='event_processing_failed';slot.connection.destroy();return;} | 
|  | +      } | 
|  | +    }); | 
|  | +    slot.connection.on('error',()=>{if(active())slot.reason='connection_error';slot.connection.destroy();}); | 
|  | +    slot.connection.on('close',()=>{ | 
|  | +      clearTimeout(slot.timer);if(closed\|\|current!==slot)return;current=null; | 
|  | +      if(slot.rotating){disposition('reconnecting','pane_set_changed');open();} | 
|  | +      else if(slot.rejected){onDisconnect();lastDisconnect=slot.reason;desired=null;disposition('degraded',slot.reason);onRejected(lastError,slot.request);} | 
|  | +      else recover(slot.reason\|\|'socket_closed'); | 
|  | +    }); | 
|  | +  } | 
|  | +  return {subscribe:request=>{ | 
|  | +    if(closed)return;desired=request; | 
|  | +    if(retryTimer)return; // A binding changed during backoff: open only latest. | 
|  | +    if(current){current.rotating=true;clearTimeout(current.timer);onDisconnect();current.connection.destroy();} | 
|  | +    else open(); | 
|  | +  },close}; | 
|  | +} | 
|  | +async function verifyMonitorCaller(id,socket) { | 
|  | +  const caller=process.env.HERDR_PANE_ID; | 
|  | +  const ping=await herdrRequest(socket,'ping'); | 
|  | +  if(ping.result?.type!=='pong') throw Error('Could not verify Herdr server.'); | 
|  | +  const proof=read(path.join(sessionDir(id),'herdr-binding.json')); | 
|  | +  let pane; | 
|  | +  try { pane=(await herdrRequest(socket,'pane.current',{caller_pane_id:caller})).result?.pane; } catch {} | 
|  | +  if(pane?.pane_id===caller && pane.terminal_id && pane.tab_id && pane.workspace_id && | 
|  | +      !(pane.agent_session && pane.agent_session.value!==id)) return pane.terminal_id; | 
|  | +  if(proof?.session_id===id && proof.terminal_id && proof.client_verified){ | 
|  | +    const restored=await restoreHerdrBinding({sessionId:id,binding:proof,socket,repair:false}); | 
|  | +    if(restored.status==='verified' && restored.identity.terminal_id===proof.terminal_id) return proof.terminal_id; | 
|  | +  } | 
|  | +  throw Error('Cannot verify caller pane or recover its recorded terminal and session.'); | 
|  | +} | 
|  | +function monitor(id,cwd,socket,callerTerminal) { | 
|  | +  const dir=sessionDir(id),ownerSignature=signature(process.pid); | 
|  | +  let timer,taskWatch,sessionWatch,refreshTimer,ended=false,refreshing=null,refreshPending=false; | 
|  | +  let currentIdentity={status:'identity_unresolved',reason:'not_checked'}; | 
|  | +  const tasks=createTaskRuntime(); | 
|  | +  const controller=createMonitorEvents({id,cwd,tasks,send:msg=>subscription.subscribe(msg),onCoverage:coverage=>{ | 
|  | +    const file=path.join(dir,'monitor.json'),record=read(file); | 
|  | +    if(record?.pid===process.pid&&record.signature===ownerSignature&&record.owner===id&&record.runtime===ROOT)write(file,{...record,coverage}); | 
|  | +  },onEvent:compact=>{ | 
|  | +    const events=read(path.join(dir,'events.json'),[]);events.push({...compact,event_id:crypto.randomUUID(),at:new Date().toISOString()});write(path.join(dir,'events.json'),events.slice(-40)); | 
|  | +  }}); | 
|  | +  const scheduleRefresh=()=>{if(!ended&&!refreshTimer)refreshTimer=setTimeout(()=>{refreshTimer=null;refresh();},100);}; | 
|  | +  const subscription=createMonitorSubscription({socket,onMessage:msg=>{ | 
|  | +    controller.receive(msg); | 
|  | +    if (msg.result?.type === 'subscription_started') refresh(); | 
|  | +    const event=msg.data?.type\|\|msg.event, p=msg.data?.pane; | 
|  | +    if(['pane_created','pane_closed','pane_moved','pane_agent_detected','pane_exited','workspace_closed','worktree_removed','tab_closed','tab_moved'].includes(event) \|\| | 
|  | +       event==='pane_updated' && p?.terminal_id && p.terminal_id===currentIdentity.identity?.terminal_id && | 
|  | +       (p.pane_id!==currentIdentity.identity?.pane_id \|\| p.agent_session?.value!==id))scheduleRefresh(); | 
|  | +  },onDisconnect:()=>{controller.disconnected();controller.setIdentity({status:'identity_unresolved',reason:'awaiting_fresh_snapshot'},[],null);},onEnd:reason=>end(reason),onRejected:(_error,request)=>{ | 
|  | +    // Keep general topology events alive while exact targets are rechecked. | 
|  | +    // Do not resend the rejected pane set, even when the snapshot lags deletion. | 
|  | +    controller.invalidate(); | 
|  | +    if(request.params.subscriptions.some(s=>s.pane_id))controller.refresh(); | 
|  | +  },onDisposition:lifecycle=>{ | 
|  | +    const file=path.join(dir,'monitor.json'),record=read(file); | 
|  | +    if(record?.pid===process.pid&&record.signature===ownerSignature&&record.owner===id&&record.runtime===ROOT)write(file,{...record,lifecycle}); | 
|  | +  }}); | 
|  | +  const refresh=()=>{ | 
|  | +    if(ended)return; | 
|  | +    refreshPending=true;if(refreshing)return refreshing; | 
|  | +    refreshing=(async()=>{while(refreshPending&&!ended){ | 
|  | +      refreshPending=false;let snapshot=null,next; | 
|  | +      try{ | 
|  | +        const response=await herdrRequest(socket,'session.snapshot');snapshot=response.result?.snapshot; | 
|  | +        next=await resolveMonitorIdentity({sessionId:id,snapshot,socket,paneHint:process.env.HERDR_PANE_ID}); | 
|  | +        if(next.status==='verified' && next.identity.terminal_id!==callerTerminal) next={status:'identity_unresolved',reason:'caller_terminal_changed'}; | 
|  | +        if(next.status==='verified'&&read(path.join(dir,'herdr-binding.json')))snapshot=(await herdrRequest(socket,'session.snapshot')).result?.snapshot; | 
|  | +      }catch(error){next={status:'identity_unresolved',reason:error.code\|\|'snapshot_unavailable'};} | 
|  | +      if(ended)break; | 
|  | +      currentIdentity=next; | 
|  | +      const panes=[],verified=[]; | 
|  | +      if(next.status==='verified')panes.push(next.identity.pane_id); | 
|  | +      for(const expected of tasks.monitorBinding(id)?.identities\|\|[]){ | 
|  | +        const live=expected.session_id===id?next: | 
|  | +          await resolveMonitorIdentity({sessionId:expected.session_id,snapshot,socket,repair:false,paneHint:expected.pane_id}); | 
|  | +        if(ended)break; | 
|  | +        if(live.status==='verified'&&live.identity.pane_id===expected.pane_id&&live.identity.cwd===expected.cwd&& | 
|  | +           (!expected.tab_id\|\|live.identity.tab_id===expected.tab_id)){panes.push(expected.pane_id);verified.push(live.identity);} | 
|  | +      } | 
|  | +      controller.setIdentity(next,panes,snapshot,verified);controller.refresh(); | 
|  | +      if(!taskWatch&&tasks.monitorBinding(id))try{ | 
|  | +        taskWatch=fs.watch(path.join(STATE,'tasks'),(_event,name)=>{const binding=tasks.monitorBinding(id);if(binding&&name===digest(binding.task_id)+'.json')scheduleRefresh();}); | 
|  | +        refreshPending=true; | 
|  | +      }catch{} | 
|  | +    }})().finally(()=>{refreshing=null;}); | 
|  | +    return refreshing; | 
|  | +  }; | 
|  | +  const end=reason=>{if(ended)return;ended=true;controller.close();taskWatch?.close();sessionWatch?.close();clearInterval(timer);clearTimeout(refreshTimer);subscription.close(reason);process.exit(0);}; | 
|  | +  process.on('SIGTERM',()=>end('signal'));process.on('SIGINT',()=>end('signal')); | 
|  | +  try{sessionWatch=fs.watch(dir,(_event,name)=>{if(['task-membership.json','herdr-binding.json','session.json'].includes(name))scheduleRefresh();});}catch{}controller.setIdentity({status:'identity_unresolved',reason:'awaiting_fresh_snapshot'},[],null);controller.refresh(); | 
|  | +  timer=setInterval(()=>{const state=read(path.join(dir,'session.json'));if(!state\|\|Date.now()-Date.parse(state.updated_at)>30*60*1000)end('session_expired');else refresh().then(()=>controller.tick());},15000);timer.unref(); | 
|  | +} | 
|  | +export async function consumeEvents(dir,{id=read(path.join(dir,'session.json'))?.session_id,tasks=createTaskRuntime(),socket=socketPath()}={}){ | 
|  | +  const all=read(path.join(dir,'events.json'),[]),cursor=read(path.join(dir,'event-cursor.json')); | 
|  | +  const index=cursor?all.findIndex(e=>e.event_id===cursor.event_id):-1,events=all.slice(index+1); | 
|  | +  if(!events.length)return ''; | 
|  | +  // Advance over rejected legacy entries too: a later tab/occupant must never | 
|  | +  // make an old notification eligible. No snapshot means no task injection. | 
|  | +  write(path.join(dir,'event-cursor.json'),{event_id:all.at(-1).event_id}); | 
|  | +  if(!events.some(e=>e?.scope))return ''; | 
|  | +  let evidence;try{evidence=await monitorEvidence(id,tasks,socket);}catch{return '';} | 
|  | +  const {snapshot,binding,identity,verified}=evidence,members=monitorMembers(snapshot); | 
|  | +  const scope=monitorScope(id,binding,identity,verified,members); | 
|  | +  if(!scope\|\|JSON.stringify(tasks.monitorBinding(id))!==JSON.stringify(binding))return ''; | 
|  | +  const latest=new Map(); | 
|  | +  for(const e of events){ | 
|  | +    if(!monitorTypes.includes(e.type)\|\|!e.scope\|\|JSON.stringify(e.scope)!==JSON.stringify(scope))continue; | 
|  | +    if(e.type.startsWith('pane_')){ | 
|  | +      if(!e.member \|\| ![binding?.mode==='observation_only'?null:identity.identity,...(binding?.identities\|\|[])].some(x => | 
|  | +        x?.pane_id === e.member.pane_id && x.session_id === e.member.session_id && (!x.terminal_id\|\|x.terminal_id===e.member.terminal_id)))continue; | 
|  | +      const current=members.get(e.member.pane_id); | 
|  | +      if(e.type==='pane_closed'){if(current)continue;} | 
|  | +      else if(e.type==='pane_moved'){ | 
|  | +        if(!e.destination\|\|!sameMonitorMember(e.destination,members.get(e.destination.pane_id))\|\|current&¤t.terminal_id!==e.member.terminal_id)continue; | 
|  | +      }else if(!sameMonitorMember(e.member,current))continue; | 
|  | +    } | 
|  | +    const key=JSON.stringify([e.type,e.pane,e.tab]);latest.delete(key); | 
|  | +    latest.set(key,{type:e.type,pane:e.pane,tab:e.tab,agent:e.agent,status:e.status}); | 
|  | +  } | 
|  | +  return latest.size?`Herdr changes: ${JSON.stringify([...latest.values()].slice(-4))}`:''; | 
|  | +} | 
|  | +export function monitorHealth(record,{alive=Boolean(record?.pid&&signature(record.pid)===record.signature)}={}) { | 
|  | +  const coverage=record?.coverage,lifecycle=record?.lifecycle; | 
|  | +  if(!record)return {state:'unavailable',reason:'not_started'}; | 
|  | +  if(!alive\|\|lifecycle?.state==='stopped')return {state:'stopped',reason:lifecycle?.reason\|\|'process_exited',error:lifecycle?.last_error\|\|null}; | 
|  | +  if(lifecycle?.state!=='subscribed'\|\|!coverage?.subscription_acknowledged) | 
|  | +    return {state:lifecycle?.state==='connecting'?'starting':'partial',reason:lifecycle?.reason\|\|'awaiting_subscription',error:lifecycle?.last_error\|\|null}; | 
|  | +  if(coverage.owner_required!==false&&coverage.identity?.status!=='verified')return {state:'partial',reason:coverage.identity?.reason\|\|'identity_unresolved'}; | 
|  | +  if(!coverage.notification_scope)return {state:'partial',reason:'goal_tab_unverified'}; | 
|  | +  return coverage.status==='partial'?{state:'partial',reason:'incomplete_pane_coverage'}:{state:'healthy',reason:'identity_and_subscription_verified'}; | 
|  | +} | 
|  | +export const relevantTool=name=>/^(Bash\|exec_command\|functions\.exec_command\|apply_patch\|functions\.apply_patch\|Edit\|Write)$/.test(name\|\|''); | 
|  | +function environmentSummary(ctx){return `Harness: cwd=${ctx.cwd}; repo=${ctx.repository_root\|\|'none'}; branch=${ctx.branch\|\|'none'}; Langfuse API configured=${ctx.connections.langfuse.configured}.`;} | 
|  | +export async function handleHook(p,{monitors=true,tasks=createTaskRuntime()}={}) { | 
|  | +  const id=p.session_id;if(!id)return {}; | 
|  | +  const event=p.hook_event_name;const dir=sessionDir(id);const cwd=p.cwd\|\|process.cwd(); | 
|  | +  const continuationFile=path.join(dir,'task-continuation.json'); | 
|  | +  if(event==='PreToolUse'){const guard=read(continuationFile);if(guard&&(guard.authorized!==true\|\|!tasks.guard(id,guard.prompt)\|\|guard.turn_id!==p.turn_id))return {hookSpecificOutput:{hookEventName:event,permissionDecision:'deny',permissionDecisionReason:'Task continuation revoked, expired or identity changed; perform no task action.'}};} | 
|  | +  if(event==='PreToolUse'&&!relevantTool(p.tool_name))return {}; | 
|  | +  let session=read(path.join(dir,'session.json'),{session_id:id,cwd});session={...session,cwd,turn_id:p.turn_id\|\|session.turn_id,updated_at:new Date().toISOString()};write(path.join(dir,'session.json'),session); | 
|  | +  const contractFile=path.join(dir,'contract.json'); | 
|  | +  const cli=`node "${path.join(HOME,'harness-runtime/current/scripts/harness.mjs')}"`; | 
|  | +  if(event==='UserPromptSubmit'){ | 
|  | +    const recognized=typeof p.prompt==='string'&&p.prompt.startsWith('[HARNESS_CONTINUATION '); | 
|  | +    // Deny before any await: hook timeout/crash can never leave a token unguarded. | 
|  | +    if(recognized)write(continuationFile,{turn_id:p.turn_id\|\|null,prompt:p.prompt,authorized:false}); | 
|  | +    let gate;try{gate=await tasks.continuation(id,p.prompt,p.turn_id);}catch{if(recognized)return promptContext(event,'Continuation authorization unavailable; perform no task action.');throw Error('Task continuation unavailable.');} | 
|  | +    if(gate){write(continuationFile,{turn_id:p.turn_id\|\|null,prompt:p.prompt,authorized:gate.authorized===true});if(!gate.authorized)return promptContext(event,gate.reason\|\|'Continuation denied; perform no task action.');} | 
|  | +    else if(recognized)return promptContext(event,'Malformed task continuation; perform no task action.'); | 
|  | +    else if(fs.existsSync(continuationFile))fs.unlinkSync(continuationFile); | 
|  | +  } | 
|  | +  if(['SessionStart','UserPromptSubmit'].includes(event)){ | 
|  | +    const ctx=cachedContext(dir,cwd,{force:event==='SessionStart'}).value; | 
|  | +    cachedSnapshot(dir,cwd,p.turn_id); | 
|  | +    if(monitors)startMonitor(id,cwd); | 
|  | +    await tasks.reconcileSession(id,{dispatch:false,deadline:Date.now()+1000}); | 
|  | +    const summary=environmentSummary(ctx),previous=read(path.join(dir,'announced-context.json')); | 
|  | +    const bootstrap=event==='SessionStart'\|\|!previous; | 
|  | +    const lines=[]; | 
|  | +    if(monitors){ | 
|  | +      const health=monitorHealth(read(path.join(dir,'monitor.json'))); | 
|  | +      const previousHealth=read(path.join(dir,'announced-monitor.json')); | 
|  | +      if(JSON.stringify(previousHealth)!==JSON.stringify(health)){ | 
|  | +        write(path.join(dir,'announced-monitor.json'),health); | 
|  | +        if(health.state==='partial'\|\|health.state==='stopped')lines.push(`Herdr monitoring ${health.state}: ${health.reason}. Inspect monitor-status; do not treat the process as complete monitoring coverage.`); | 
|  | +      } | 
|  | +    } | 
|  | +    if(bootstrap\|\|previous.summary!==summary){lines.push(summary);write(path.join(dir,'announced-context.json'),{summary});} | 
|  | +    if(bootstrap){ | 
|  | +      lines.push(`Runtime CLI: ${cli}; session=${id}. Use explicit workdir or run --session ${id} -- <command>.`); | 
|  | +      const carried=read(contractFile); | 
|  | +      if(carried&&carried.status!=='complete')lines.push(`Carried task (${carried.status}): ${String(carried.objective).slice(0,180)}. Record: ${contractFile}. Reassess only if relevant to this request.`); | 
|  | +      if(carried?.schema_version===2&&carried.status!=='complete')lines.push(`Inspect remaining checks with verify status --session ${id}; run only checks needed for this task. Codex owns continuation.`); | 
|  | +    } | 
|  | +    lines.push(await consumeEvents(dir,{id,tasks})); | 
|  | +    return lines.some(Boolean)?promptContext(event,lines.filter(Boolean).join('\n')):{}; | 
|  | +  } | 
|  | +  if(event==='PreToolUse'){ | 
|  | +    const ctx=read(path.join(dir,'context.json')); | 
|  | +    let input=p.tool_input; | 
|  | +    if(typeof input==='string'){try{input=JSON.parse(input);}catch{input=null;}} | 
|  | +    const lines=[]; | 
|  | +    // Codex's Bash compatibility payload only has command, dropping workdir. | 
|  | +    // Absence in that lossy payload is not evidence of a missing working directory. | 
|  | +    const canInspectCwd=['exec_command','functions.exec_command'].includes(p.tool_name)&&input&&typeof input==='object'&&typeof input.cmd==='string'; | 
|  | +    const preflight=gitPreflight(input?.cmd??input?.command,input?.workdir??input?.cwd); | 
|  | +    if(preflight.status==='failed')return {hookSpecificOutput:{hookEventName:event,permissionDecision:'deny',permissionDecisionReason:`Git repository preflight failed in ${preflight.cwd}. Select and verify the intended repository before retrying. Skip repository-only housekeeping when the task has no repository.`}}; | 
|  | +    if(p.tool_name==='Bash'&&input?.command&&gitPreflight(input.command,ctx?.cwd).status==='failed') | 
|  | +      lines.push('Git repository preflight could not establish a repository in the recorded cwd. This Bash payload omits the actual workdir; verify the effective cwd or use git -C with the intended repository. Skip repository-only housekeeping for a non-repository task.'); | 
|  | +    const marker=path.join(dir,'cwd-reminder.json'); | 
|  | +    const turn=p.turn_id\|\|'session'; | 
|  | +    if(canInspectCwd&&!input.cwd&&!input.workdir&&ctx?.cwd&&read(marker)?.turn_id!==turn){ | 
|  | +      write(marker,{turn_id:turn}); | 
|  | +      lines.push(`Recorded working directory: ${ctx.cwd}. Supply it explicitly for repository commands or use the harness run command. Do not infer paths after a missing-file error.`); | 
|  | +    } | 
|  | +    lines.push(await consumeEvents(dir,{id,tasks})); | 
|  | +    return lines.some(Boolean)?promptContext(event,lines.filter(Boolean).join('\n')):{}; | 
|  | +  } | 
|  | +  if(event==='Stop'){ | 
|  | +    const stopDeadline=Date.now()+3500; | 
|  | +    const carried=read(contractFile),c=carried?.assessment_turn_id===p.turn_id&&p.turn_id?carried:null; | 
|  | +    let issues=[],malformed=false;try{if(c){issues=checkContract(c,dir,{deadline:Date.now()+1500});if(p.turn_id)write(path.join(dir,'contracts',`${digest(String(p.turn_id))}.json`),{...c,turn_id:p.turn_id});}}catch{malformed=true;issues=['Task contract or evidence is unavailable; repair it or report it as unverified.'];} | 
|  | +    const loop=await tasks.stop(id,p.turn_id,{deadline:stopDeadline});issues.push(...loop.issues); | 
|  | +    const execution=scanExecutions(p.transcript_path,p.turn_id); | 
|  | +    const executionAssessment=p.turn_id?executionReview(dir,execution):{status:'not_observed',coverage:'unavailable'}; | 
|  | +    if(p.turn_id)write(executionFiles(dir,p.turn_id).execution,execution); | 
|  | +    const observed=c?.schema_version===2&&c.status==='complete'; | 
|  | +    const result={turn_id:p.turn_id\|\|null,at:new Date().toISOString(),task_outcome:c?.status\|\|'untracked',assessment_source:c?.assessment_source\|\|null, | 
|  | +      structural_check:!c?'not_applicable':malformed\|\|(!observed&&issues.length)?'issues':'pass',verification_status:observed?(issues.length?'unverified':'pass'):'not_checked',issues}; | 
|  | +    result.execution_review=executionAssessment; | 
|  | +    const marker=path.join(dir,'continued',`${digest(String(p.turn_id\|\|'session'))}.json`); | 
|  | +    const block=issues.length>0&&!p.stop_hook_active&&!fs.existsSync(marker); | 
|  | +    result.stop_action=block?'blocked':issues.length?'warned':'none'; | 
|  | +    result.excluded_probe_count=execution.excluded.length; | 
|  | +    result.transcript_path=p.transcript_path\|\|null; | 
|  | +    write(path.join(dir,'outcomes',`${digest(String(p.turn_id\|\|'session'))}.json`),result); | 
|  | +    if(block){ | 
|  | +      write(marker,{at:result.at});return {decision:'block',reason:`Completion is unverified: ${issues.join('; ')}. Reconcile the recorded request with actual evidence; continue authorized remaining work or accurately mark partial/blocked. Do not invent evidence or ask for renewed permission. Record: ${contractFile}`}; | 
|  | +    } | 
|  | +    return issues.length?{systemMessage:`Completion record remains unverified: ${issues.join('; ')}`} : {}; | 
|  | +  } | 
|  | +  if(['SessionEnd','Interrupt'].includes(event)){ | 
|  | +    await tasks.interrupt(id); | 
|  | +    const r=read(path.join(dir,'monitor.json'));stopOwned(r,event==='Interrupt'?'user_interrupt':'session_end'); | 
|  | +    if(event==='SessionEnd')cleanup(id,'session_end'); | 
|  | +    return {}; | 
|  | +  } | 
|  | +  return {}; | 
|  | +} | 
|  | +function cleanup(id,reason){ | 
|  | +  const dir=path.join(sessionDir(id),'processes');if(!fs.existsSync(dir))return; | 
|  | +  for(const f of fs.readdirSync(dir).filter(x=>x.endsWith('.json'))){const file=path.join(dir,f),r=read(file);if(!r\|\|r.ended_at\|\|r.retain)continue; | 
|  | +    write(file,{...r,termination_request:{reason,at:new Date().toISOString()},stop_result:stopOwned(r,reason)}); | 
|  | +  } | 
|  | +} | 
|  | +async function processWorker(id,name,args,cwd,instance){ | 
|  | +  const file=path.join(sessionDir(id),'processes',`${digest(name)}.json`);for(let i=0;i<50&&!fs.existsSync(file);i++)await new Promise(r=>setTimeout(r,20));let r=read(file)\|\|{};if(r.pid!==process.pid\|\|r.instance!==instance)throw Error('Process ownership registration mismatch');const log=fs.openSync(file+'.log','a',0o600); | 
|  | +  const child=spawn(args[0],args.slice(1),{cwd,stdio:['ignore',log,log]});let ended=false; | 
|  | +  const finish=(code,signal,error)=>{if(ended)return;ended=true;const latest=read(file);if(latest?.instance!==instance\|\|latest?.pid!==process.pid)process.exit(0);r=latest; | 
|  | +    const reason=r.termination_request?.reason; | 
|  | +    write(file,{...r,ended_at:new Date().toISOString(),exit_code:code,signal,error:error?.code\|\|null, | 
|  | +      execution_outcome:reason&&(signal\|\|code===128+15\|\|code===128+2)?(reason==='normal_cleanup'\|\|reason==='session_end'?'expected_shutdown':'cancelled'):code===0?'success':'failure', | 
|  | +      duration_semantics:'process_lifetime_not_blocking_wait'});fs.closeSync(log);process.exit(0);}; | 
|  | +  child.on('error',e=>finish(null,null,e));child.on('exit',(c,s)=>finish(c,s)); | 
|  | +  // Group signal already reaches child; keep the wrapper alive to persist its result. | 
|  | +  process.on('SIGTERM',()=>{});process.on('SIGINT',()=>{}); | 
|  | +} | 
|  | +export async function main(argv=process.argv.slice(2)){ | 
|  | +  const [cmd,...rest]=argv;const separator=rest.indexOf('--');const flags=separator<0?rest:rest.slice(0,separator);const option=k=>{const i=flags.indexOf(`--${k}`);return i<0?undefined:flags[i+1];}; | 
|  | +  const id=option('session')\|\|process.env.CODEX_THREAD_ID; | 
|  | +  if(cmd==='hook'){let s='';for await(const chunk of process.stdin){s+=chunk;if(s.length>2097152)throw Error('Hook payload too large');}console.log(JSON.stringify(await handleHook(JSON.parse(s\|\|'{}'))));return;} | 
|  | +  if(cmd==='monitor'){ | 
|  | +    if (!socketPath() \|\| rest[2] !== socketPath()) throw Error('Monitor requires the inherited Herdr pane and socket.'); | 
|  | +    const callerTerminal=await verifyMonitorCaller(rest[0],rest[2]); | 
|  | +    monitor(...rest.slice(0,3),callerTerminal);return; | 
|  | +  } | 
|  | +  if(cmd==='process-worker'){await processWorker(rest[0],rest[1],JSON.parse(rest[2]),rest[3],rest[4]);return;} | 
|  | +  if(cmd==='context'){console.log(JSON.stringify(context(option('cwd')\|\|process.cwd()),null,2));return;} | 
|  | +  if(cmd==='report'){ | 
|  | +    for(const flag of ['since','format'])if(flags.includes(`--${flag}`)&&(!option(flag)\|\|option(flag).startsWith('--')))throw Error(`--${flag} requires a value.`); | 
|  | +    const format=option('format')\|\|'md'; | 
|  | +    if(!['md','json'].includes(format))throw Error('--format requires md or json.'); | 
|  | +    const report=collectReport(STATE,{since:Number(option('since')??7)}); | 
|  | +    console.log(format==='json'?JSON.stringify(report,null,2):formatReport(report));return; | 
|  | +  } | 
|  | +  if(cmd==='verify'&&rest[0]==='help'){ | 
|  | +    console.log(JSON.stringify({commands:['contract set --session <id> --file <schema-v2.json>','verify status --session <id>','verify run --session <id> --check <id> [--force]','contract complete --session <id>'], | 
|  | +      schema:{schema_version:2,task_id:'approved-task',objective:'Deliver the approved outcome',status:'in_progress',requirements:[{id:'outcome',text:'Required observable outcome',source:'issue URL or current user request',checks:['outcome']}],checks:[{id:'outcome',cwd:'/absolute/project',command:['node','scripts/check-outcome.mjs'],format:'json',assertions:[{pointer:'/failed',op:'eq',value:0},{pointer:'/executed',op:'gte',value:1}]}]}, | 
|  | +      semantics:'Runs only explicitly selected commands, records their actual outputs, and reuses valid observations. Codex owns the goal and continuation. Credentials belong in the environment, never in a contract.'},null,2));return; | 
|  | +  } | 
|  | +  if(cmd==='task'&&rest[0]==='help'){console.log(JSON.stringify({commands:['task register --session <parent> --file <task.json>','task assign --session <parent> --task <id> --file <assignment.json>','task report --session <member> --task <id> --file <report.json>','task status --task <id> \| task list','task observe-register --coordinator <external-id> --file <goal.json>','task observe-assign\|bind-observer\|unbind-observer\|retain-pane\|resolve-pane\|observe-end\|preserve\|retirement\|retire\|handoff --coordinator <external-id> --task <id> --file <input.json>','task bind-observer\|unbind-observer\|retain-pane\|resolve-pane\|preserve\|retirement\|retire\|handoff --session <parent> --task <id> --file <input.json>','task reconcile\|pause\|cancel\|complete --session <member> --task <id>'],semantics:'Explicit opt-in. Observation-only registration never adopts workers, dispatches continuation, or closes panes. Retirement records a tombstone only. Runtime probes exact identities; result preservation requires actual verification receipts. Continuation ownership and fences are unchanged.'}));return;} | 
|  | +  if(cmd==='task'&&['status','list'].includes(rest[0])){const tasks=createTaskRuntime();console.log(JSON.stringify(rest[0]==='list'?tasks.list():tasks.status(option('task')),null,2));return;} | 
|  | +  if(cmd==='task'&&option('coordinator')){ | 
|  | +    if(option('session'))throw Error('External coordinator is not a Codex session.'); | 
|  | +    const tasks=createTaskRuntime(),actor=option('coordinator'),task=option('task'),action=rest[0],input=JSON.parse(fs.readFileSync(option('file'),'utf8')); | 
|  | +    if(action!=='observe-register'&&tasks.get(task)?.mode!=='observation_only')throw Error('External coordinator may only mutate its observation-only records.'); | 
|  | +    const methods={'observe-register':'registerObserver','observe-assign':'observeAssign','bind-observer':'bindObserver','unbind-observer':'unbindObserver','resolve-pane':'resolvePane','observe-end':'endObservation','retain-pane':'retainPane',preserve:'preserve',retirement:'retirement',retire:'retire',handoff:'handoff'}; | 
|  | +    if(!methods[action])throw Error('Unsupported external observation command.'); | 
|  | +    const result=action==='observe-register'?await tasks.registerObserver(actor,input):await tasks[methods[action]](actor,task,input); | 
|  | +    console.log(JSON.stringify(result,null,2));return; | 
|  | +  } | 
|  | +  if(!id)throw Error('Provide --session or CODEX_THREAD_ID.'); | 
|  | +  const dir=sessionDir(id); | 
|  | +  if(cmd==='monitor-bind'){ | 
|  | +    if(process.env.CODEX_THREAD_ID&&process.env.CODEX_THREAD_ID!==id)throw Error('Binding session differs from current Codex identity.'); | 
|  | +    const paneId=option('pane');if(!paneId)throw Error('Provide the explicitly verified --pane; cwd/focus are not identity evidence.'); | 
|  | +    const result=await bindHerdrSession({sessionId:id,paneId,socket:socketPath(),persist:binding=>write(path.join(dir,'herdr-binding.json'),binding)}); | 
|  | +    const session=read(path.join(dir,'session.json')); | 
|  | +    if(session?.cwd)startMonitor(id,session.cwd); | 
|  | +    console.log(JSON.stringify(result));return; | 
|  | +  } | 
|  | +  if(cmd==='monitor-start'){ | 
|  | +    const session=read(path.join(dir,'session.json'));if(!session?.cwd)throw Error('No recorded session cwd.'); | 
|  | +    console.log(JSON.stringify(startMonitor(id,session.cwd)));return; | 
|  | +  } | 
|  | +  if(cmd==='task'){ | 
|  | +    if(process.env.CODEX_THREAD_ID&&process.env.CODEX_THREAD_ID!==id)throw Error('Task command session differs from current Codex identity.'); | 
|  | +    const tasks=createTaskRuntime(),task=option('task'),action=rest[0];let result; | 
|  | +    const input=()=>JSON.parse(fs.readFileSync(option('file'),'utf8')); | 
|  | +    if(action==='register')result=await tasks.register(id,input()); | 
|  | +    else if(action==='assign')result=await tasks.assign(id,task,input()); | 
|  | +    else if(action==='report'){const turn=read(path.join(dir,'session.json'))?.turn_id;if(!turn)throw Error('No exact current turn recorded.');result=await tasks.report(id,task,turn,input());} | 
|  | +    else if(['bind-observer','unbind-observer','resolve-pane','retain-pane','preserve','retirement','retire','handoff'].includes(action))result=await tasks[{'bind-observer':'bindObserver','unbind-observer':'unbindObserver','resolve-pane':'resolvePane','observe-end':'endObservation','retain-pane':'retainPane',preserve:'preserve',retirement:'retirement',retire:'retire',handoff:'handoff'}[action]](id,task,input()); | 
|  | +    else if(action==='reconcile'){if(!tasks.monitorBinding(id)\|\|tasks.monitorBinding(id).task_id!==task)throw Error('Task member mismatch.');result=await tasks.reconcileSession(id);} | 
|  | +    else if(['pause','cancel','complete'].includes(action))result=await tasks.control(id,task,{pause:'paused',cancel:'cancelled',complete:'completed'}[action]); | 
|  | +    else throw Error('task help\|register\|assign\|report\|status\|reconcile\|pause\|cancel\|complete'); | 
|  | +    console.log(JSON.stringify(result,null,2));return; | 
|  | +  } | 
|  | +  if(cmd==='execution'){ | 
|  | +    const turn=read(path.join(dir,'session.json'))?.turn_id; | 
|  | +    if(!turn)throw Error('No current turn is recorded.'); | 
|  | +    const files=executionFiles(dir,turn),execution=read(files.execution); | 
|  | +    if(!execution)throw Error('No observed execution snapshot for this turn.'); | 
|  | +    if(rest[0]==='review'){ | 
|  | +      const review=validateExecutionReview(execution,JSON.parse(fs.readFileSync(option('file'),'utf8'))); | 
|  | +      write(files.review,{...review,assessment_source:'agent_reported_scope_review',updated_at:new Date().toISOString()}); | 
|  | +    }else if(rest[0]!=='status')throw Error('execution status\|review'); | 
|  | +    console.log(JSON.stringify({execution,review:executionReview(dir,execution)},null,2));return; | 
|  | +  } | 
|  | +  if(cmd==='contract'){ | 
|  | +    if(rest[0]==='set'){const c=JSON.parse(fs.readFileSync(option('file'),'utf8'));console.log(JSON.stringify({path:path.join(dir,'contract.json'),issues:checkContract(setContract(id,c),dir)}));} | 
|  | +    else if(rest[0]==='complete'){ | 
|  | +      const c=read(path.join(dir,'contract.json'));if(c?.schema_version!==2)throw Error('Observed completion requires a schema-v2 contract.'); | 
|  | +      const result=assessVerification(c,dir); | 
|  | +      if(result.status!=='pass'){ | 
|  | +        setContract(id,{...c,status:'in_progress'});console.log(JSON.stringify(result,null,2));process.exitCode=1;return; | 
|  | +      } | 
|  | +      const saved=setContract(id,{...c,status:'complete'});console.log(JSON.stringify({...result,status:saved.status,verification_status:result.status},null,2)); | 
|  | +    } | 
|  | +    else if(rest[0]==='show')console.log(JSON.stringify(read(path.join(dir,'contract.json')),null,2)); | 
|  | +    else if(rest[0]==='handoff'){const c=read(path.join(dir,'contract.json'));console.log(JSON.stringify({contract:c,context:read(path.join(dir,'context.json')),source_session:id},null,2));} | 
|  | +    else throw Error('contract set\|show\|handoff\|complete');return; | 
|  | +  } | 
|  | +  if(cmd==='verify'){ | 
|  | +    const c=read(path.join(dir,'contract.json'));if(c?.schema_version!==2)throw Error('verify requires a schema-v2 contract; use verify help.'); | 
|  | +    if(rest[0]==='run'){ | 
|  | +      const active=setContract(id,{...c,status:'in_progress'}); | 
|  | +      const result=await runVerification(active,dir,option('check'),{force:flags.includes('--force')}); | 
|  | +      console.log(JSON.stringify(result,null,2));if(result.status!=='pass')process.exitCode=1; | 
|  | +    }else if(rest[0]==='status'){ | 
|  | +      const result=assessVerification(c,dir); | 
|  | +      setContract(id,{...c,status:c.status==='complete'&&result.status!=='pass'?'in_progress':c.status}); | 
|  | +      console.log(JSON.stringify(result,null,2)); | 
|  | +    }else throw Error('verify help\|status\|run');return; | 
|  | +  } | 
|  | +  if(cmd==='run'\|\|cmd==='process'){ | 
|  | +    const ctx=read(path.join(dir,'context.json'));const cwd=option('cwd')\|\|ctx?.cwd; | 
|  | +    if(!cwd\|\|!fs.statSync(cwd).isDirectory())throw Error('No valid working directory; pass --cwd.'); | 
|  | +    const args=rest.slice(rest.indexOf('--')+1); | 
|  | +    if(cmd==='process'&&rest[0]==='stop'){ | 
|  | +      const file=path.join(dir,'processes',`${digest(option('name'))}.json`),r=read(file);if(!r)throw Error('Unknown owned process'); | 
|  | +      write(file,{...r,termination_request:{reason:option('reason')\|\|'normal_cleanup',at:new Date().toISOString()}});console.log(JSON.stringify(stopOwned(r,option('reason')\|\|'normal_cleanup')));return; | 
|  | +    } | 
|  | +    if(!rest.includes('--')\|\|!args.length)throw Error('Expected -- <command> [args]'); | 
|  | +    if(cmd==='run'){ | 
|  | +      const preflight=gitPreflight(args,cwd); | 
|  | +      if(preflight.status==='failed')throw Error(`Git repository preflight failed in ${preflight.cwd}; select the intended repository or skip unrelated repository housekeeping.`); | 
|  | +      const child=spawn(args[0],args.slice(1),{cwd,stdio:'inherit'}); | 
|  | +      child.on('error',e=>{console.error(e.message);process.exitCode=1;});child.on('exit',(c,s)=>{process.exitCode=c??(s?128:1);});return; | 
|  | +    } | 
|  | +    const name=option('name');if(!name)throw Error('process start needs --name'); | 
|  | +    const file=path.join(dir,'processes',`${digest(name)}.json`); | 
|  | +    fs.mkdirSync(path.dirname(file),{recursive:true,mode:0o700}); | 
|  | +    let lock;try{lock=fs.openSync(file+'.lock','wx',0o600);}catch{throw Error('Process name is being registered; retry after the active operation finishes');} | 
|  | +    try{ | 
|  | +      const old=read(file); | 
|  | +      if(old&&!old.ended_at&&signature(old.pid)===old.signature)throw Error('Owned process name already running'); | 
|  | +      const instance=crypto.randomUUID(); | 
|  | +      const child=spawn(process.execPath,[fileURLToPath(import.meta.url),'process-worker',id,name,JSON.stringify(args),cwd,instance],{detached:true,stdio:'ignore'}); | 
|  | +      const record={owner:id,name,instance,pid:child.pid,signature:signature(child.pid),cwd,started_at:new Date().toISOString(),purpose:option('purpose')\|\|'background',duration_semantics:'process_lifetime_not_blocking_wait'}; | 
|  | +      write(file,record);child.unref();console.log(JSON.stringify({name,pid:record.pid,record:file,log:file+'.log'}));return; | 
|  | +    }finally{fs.closeSync(lock);fs.unlinkSync(file+'.lock');} | 
|  | +  } | 
|  | +  if(cmd==='monitor-status'){const tasks=createTaskRuntime(),record=read(path.join(dir,'monitor.json'));console.log(JSON.stringify({health:monitorHealth(record),monitor:record,identity_binding:read(path.join(dir,'herdr-binding.json')),binding:tasks.monitorBinding(id),coverage:tasks.monitorBinding(id)?tasks.status(tasks.monitorBinding(id).task_id)?.monitor_coverage:record?.coverage\|\|{status:'unbound'},events:read(path.join(dir,'events.json'),[])}));return;} | 
|  | +  throw Error('Commands: context, contract, verify, execution, report, run, process, monitor-status, monitor-start, monitor-bind, task'); | 
|  | +} | 
|  | +if(process.argv[1]&&fs.existsSync(process.argv[1])&&fs.realpathSync(process.argv[1])===fileURLToPath(import.meta.url))main().catch(e=>{if(process.argv[2]==='hook'){console.log(JSON.stringify({systemMessage:`Harness hook unavailable: ${e.message}`}));}else{console.error(e.message);process.exitCode=1;}}); | 
|  | diff --git a/scripts/herdr-binding.mjs b/scripts/herdr-binding.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..2c7125d | 
|  | --- /dev/null | 
|  | +++ b/scripts/herdr-binding.mjs | 
|  | @@ -0,0 +1,117 @@ | 
|  | +import path from 'node:path'; | 
|  | +import {spawnSync} from 'node:child_process'; | 
|  | +import {authoritativeSessionCandidates, herdrRequest, selectHerdrIdentity} from './herdr-identity.mjs'; | 
|  | + | 
|  | +// A terminal survives pane moves. Legacy proof pins a process generation; | 
|  | +// verified clients may renew it, moving terminals only after the old generation | 
|  | +// exits. Never use cwd/focus. | 
|  | +export function processStart(pid) { | 
|  | +  if (!Number.isSafeInteger(pid) \|\| pid <= 0) return null; | 
|  | +  const r=spawnSync('ps',['-p',String(pid),'-o','lstart='],{encoding:'utf8',timeout:500}); | 
|  | +  return r.status===0 && r.stdout.trim() \|\| null; | 
|  | +} | 
|  | +export function processAbsent(pid,kill=(...args)=>process.kill(...args)) { | 
|  | +  if(!Number.isSafeInteger(pid)\|\|pid<=0)return false; | 
|  | +  try{kill(pid,0);return false;} | 
|  | +  catch(error){return error.code==='ESRCH';} | 
|  | +} | 
|  | +const unresolved=reason=>({status:'identity_unresolved',reason}); | 
|  | +const sessionOf=a=>a?.agent_session?.kind==='id'?a.agent_session.value:null; | 
|  | +const clientOf=a=>a?.agent_session?.source==='herdr:codex-client'; | 
|  | +async function proofFor(a,{socket,request,stamp}) { | 
|  | +  if (!a?.terminal_id \|\| a.agent!=='codex') return null; | 
|  | +  const response=await request(socket,'pane.process_info',{pane_id:a.pane_id}); | 
|  | +  const info=response.result?.process_info; | 
|  | +  if (info?.pane_id!==a.pane_id) return null; | 
|  | +  const processes=(info.foreground_processes\|\|[]).filter(p=> | 
|  | +    p.name==='codex' \|\| path.basename(p.argv0\|\|'')==='codex'); | 
|  | +  if (processes.length!==1) return null; | 
|  | +  const pid=processes[0].pid, startedAt=stamp(pid); | 
|  | +  return startedAt?{terminal_id:a.terminal_id,pid,started_at:startedAt}:null; | 
|  | +} | 
|  | +export async function bindHerdrSession({sessionId,paneId,socket,request=herdrRequest,stamp=processStart,persist=()=>{},requireClient=false}) { | 
|  | +  const response=await request(socket,'session.snapshot'); | 
|  | +  const agents=response.result?.snapshot?.agents; | 
|  | +  if (!sessionId \|\| !paneId \|\| !Array.isArray(agents)) throw Error('Cannot verify explicit Herdr session binding.'); | 
|  | +  const candidates=agents.filter(a=>a?.pane_id===paneId); | 
|  | +  const a=candidates.length===1 && candidates[0].agent==='codex'?candidates[0]:null; | 
|  | +  if (!a \|\| (a.agent_session && sessionOf(a)!==sessionId) \|\| | 
|  | +      authoritativeSessionCandidates(agents,sessionId).some(other=>other.pane_id!==paneId)) | 
|  | +    throw Error('Pane is missing, ambiguous, or belongs to another session.'); | 
|  | +  if(requireClient && (!clientOf(a)\|\|sessionOf(a)!==sessionId))throw Error('Client session is no longer registered.'); | 
|  | +  const proof=await proofFor(a,{socket,request,stamp}); | 
|  | +  if (!proof) throw Error('Cannot verify the pane terminal and foreground Codex process.'); | 
|  | +  let binding={session_id:sessionId,pane_id:paneId,...proof,...(clientOf(a)?{client_verified:true}:{})}; | 
|  | +  const save=async next=>{await persist(next);binding=next;}; | 
|  | +  // Persist proof before the first write. A failed/aborted registration must | 
|  | +  // never turn into an unguarded exact-session match on the next refresh. | 
|  | +  await save(binding); | 
|  | +  const result=await restoreHerdrBinding({sessionId,binding,socket,request,stamp,persist:save}); | 
|  | +  if (result.status!=='verified') throw Error('Herdr session registration was not verified: '+result.reason); | 
|  | +  return {binding,identity:result.identity}; | 
|  | +} | 
|  | +export async function restoreHerdrBinding({sessionId,binding,socket,request=herdrRequest,stamp=processStart,absent=processAbsent,repair=true,persist=()=>{}}) { | 
|  | +  if (!binding \|\| binding.session_id!==sessionId \|\| !binding.terminal_id \|\| !binding.started_at) | 
|  | +    return unresolved('no_verified_binding'); | 
|  | +  try { | 
|  | +    const response=await request(socket,'session.snapshot'); | 
|  | +    const snapshot=response.result?.snapshot; | 
|  | +    if (!Array.isArray(snapshot?.agents)) return unresolved('invalid_snapshot'); | 
|  | +    let terminalId=binding.terminal_id; | 
|  | +    if(binding.client_verified){ | 
|  | +      const selected=selectHerdrIdentity(snapshot,{sessionId}); | 
|  | +      if(selected.status==='verified'&&selected.identity.terminal_id&&selected.identity.terminal_id!==terminalId){ | 
|  | +        const next=snapshot.agents.find(a=>a.pane_id===selected.identity.pane_id); | 
|  | +        if(clientOf(next)){ | 
|  | +          const oldStart=stamp(binding.pid); | 
|  | +          if(oldStart===binding.started_at)return unresolved('session_conflict'); | 
|  | +          // A missing ps stamp can also mean timeout/error. Only a changed | 
|  | +          // nonempty stamp or independently confirmed ESRCH proves exit/reuse. | 
|  | +          if(!(typeof oldStart==='string'&&oldStart.length>0)&&!absent(binding.pid))return unresolved('previous_process_unverified'); | 
|  | +          terminalId=selected.identity.terminal_id; | 
|  | +        } | 
|  | +      } | 
|  | +    } | 
|  | +    const candidates=snapshot.agents.filter(a=>a.terminal_id===terminalId && a.agent==='codex'); | 
|  | +    if (candidates.length!==1) return unresolved('terminal_missing_or_ambiguous'); | 
|  | +    const a=candidates[0]; | 
|  | +    if(binding.client_verified && (!clientOf(a)\|\|sessionOf(a)!==sessionId))return unresolved('client_session_not_registered'); | 
|  | +    if (a.agent_session && sessionOf(a)!==sessionId) return unresolved('session_conflict'); | 
|  | +    if (authoritativeSessionCandidates(snapshot.agents,sessionId).some(other=>other.pane_id!==a.pane_id)) | 
|  | +      return unresolved('session_conflict'); | 
|  | +    if(clientOf(a)){ | 
|  | +      const selected=selectHerdrIdentity(snapshot,{sessionId}); | 
|  | +      if(selected.status!=='verified')return selected; | 
|  | +    } | 
|  | +    const proof=await proofFor(a,{socket,request,stamp}); | 
|  | +    if(!proof)return unresolved('terminal_process_changed'); | 
|  | +    if(proof.terminal_id!==binding.terminal_id\|\|proof.pid!==binding.pid\|\|proof.started_at!==binding.started_at){ | 
|  | +      // Only a unique server-validated client can renew proof; another terminal | 
|  | +      // was admitted above only after the pinned process generation exited. | 
|  | +      if(!binding.client_verified\|\|!clientOf(a))return unresolved('terminal_process_changed'); | 
|  | +      binding={...binding,pane_id:a.pane_id,...proof};await persist(binding); | 
|  | +    } | 
|  | +    if(clientOf(a)&&!binding.client_verified){binding={...binding,client_verified:true};await persist(binding);} | 
|  | +    if (!a.agent_session) { | 
|  | +      if(!repair)return unresolved('session_not_registered'); | 
|  | +      await request(socket,'pane.report_agent_session',{ | 
|  | +        pane_id:a.pane_id,source:'herdr:codex',agent:'codex', | 
|  | +        agent_session_id:sessionId,seq:Date.now()*1000000,session_start_source:'resume', | 
|  | +      }); | 
|  | +    } | 
|  | +    const confirmed=await request(socket,'session.snapshot'); | 
|  | +    if(binding.client_verified){ | 
|  | +      const live=confirmed.result?.snapshot?.agents?.filter(x=>x.terminal_id===binding.terminal_id); | 
|  | +      if(live?.length!==1\|\|!clientOf(live[0])\|\|sessionOf(live[0])!==sessionId)return unresolved('client_session_not_registered'); | 
|  | +    } | 
|  | +    const result=selectHerdrIdentity(confirmed.result?.snapshot,{sessionId,paneHint:a.pane_id}); | 
|  | +    if(result.status!=='verified')return result; | 
|  | +    if(result.identity.terminal_id!==binding.terminal_id)return unresolved('terminal_changed_during_registration'); | 
|  | +    const verifiedAgent=confirmed.result.snapshot.agents.find(x=>x.pane_id===result.identity.pane_id); | 
|  | +    const verifiedProof=await proofFor(verifiedAgent,{socket,request,stamp}); | 
|  | +    if(!verifiedProof\|\|verifiedProof.pid!==binding.pid\|\|verifiedProof.started_at!==binding.started_at) | 
|  | +      return unresolved('terminal_process_changed'); | 
|  | +    if(clientOf(verifiedAgent)&&!binding.client_verified)await persist({...binding,client_verified:true}); | 
|  | +    return result; | 
|  | +  } catch (error) { return unresolved(error.code\|\|'registration_unavailable'); } | 
|  | +} | 
|  | diff --git a/scripts/herdr-identity.mjs b/scripts/herdr-identity.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..d3b7e57 | 
|  | --- /dev/null | 
|  | +++ b/scripts/herdr-identity.mjs | 
|  | @@ -0,0 +1,107 @@ | 
|  | +import net from 'node:net'; | 
|  | +import {randomUUID} from 'node:crypto'; | 
|  | +import path from 'node:path'; | 
|  | + | 
|  | +const TIMEOUT_MS = 1000; | 
|  | +const MAX_RESPONSE_BYTES = 2 * 1024 * 1024; | 
|  | +const record = value => value !== null && typeof value === 'object' && !Array.isArray(value); | 
|  | +const id = value => typeof value === 'string' && /^[A-Za-z0-9][A-Za-z0-9._:-]{0,199}$/.test(value); | 
|  | +const errorCodes = new Set(['invalid_request', 'request_timeout', 'socket_error', 'socket_closed', 'malformed_response', 'response_id_mismatch', 'response_error', 'response_too_large']); | 
|  | +const failure = code => Object.assign(new Error(code), {code}); | 
|  | + | 
|  | +/** | 
|  | + * One newline-delimited Herdr socket request. Returns the FULL {id, result} | 
|  | + * envelope. Only the generated request ID is accepted; errors expose fixed codes, | 
|  | + * never server messages or local socket paths. The deadline includes connection | 
|  | + * time, and both the timer and socket are disposed on every completion path. | 
|  | + */ | 
|  | +export async function herdrRequest(socket, method, params = {}, options = {}) { | 
|  | +  if (typeof socket !== 'string' \|\| !socket \|\| typeof method !== 'string' \|\| !method \|\| !record(params)) throw failure('invalid_request'); | 
|  | +  const timeoutMs = Math.max(1, Math.min(TIMEOUT_MS, Number.isFinite(options?.timeoutMs) ? options.timeoutMs : TIMEOUT_MS)); | 
|  | +  const requestId = randomUUID(); | 
|  | +  let packet; | 
|  | +  try { packet = JSON.stringify({id: requestId, method, params}) + '\n'; } | 
|  | +  catch { throw failure('invalid_request'); } | 
|  | +  return new Promise((resolve, reject) => { | 
|  | +    let peer, timer, settled = false, buffer = '', bytes = 0; | 
|  | +    const finish = (error, response) => { | 
|  | +      if (settled) return; | 
|  | +      settled = true; | 
|  | +      clearTimeout(timer); | 
|  | +      peer?.destroy(); | 
|  | +      if (error) reject(failure(error)); | 
|  | +      else resolve(response); | 
|  | +    }; | 
|  | +    timer = setTimeout(() => finish('request_timeout'), timeoutMs); | 
|  | +    try { peer = net.createConnection({path: socket}); } | 
|  | +    catch { finish('socket_error'); return; } | 
|  | +    peer.setEncoding('utf8'); | 
|  | +    peer.on('connect', () => { if (!settled) peer.write(packet); }); | 
|  | +    peer.on('error', () => finish('socket_error')); | 
|  | +    peer.on('end', () => finish(buffer ? 'malformed_response' : 'socket_closed')); | 
|  | +    peer.on('close', () => finish('socket_closed')); | 
|  | +    peer.on('data', chunk => { | 
|  | +      if (settled) return; | 
|  | +      bytes += Buffer.byteLength(chunk); | 
|  | +      if (bytes > MAX_RESPONSE_BYTES) { finish('response_too_large'); return; } | 
|  | +      buffer += chunk; | 
|  | +      const newline = buffer.indexOf('\n'); | 
|  | +      if (newline < 0) return; | 
|  | +      let response; | 
|  | +      try { response = JSON.parse(buffer.slice(0, newline)); } | 
|  | +      catch { finish('malformed_response'); return; } | 
|  | +      if (!record(response) \|\| typeof response.id !== 'string') { finish('malformed_response'); return; } | 
|  | +      if (response.id !== requestId) { finish('response_id_mismatch'); return; } | 
|  | +      if (Object.hasOwn(response, 'error')) { finish('response_error'); return; } | 
|  | +      if (!record(response.result)) { finish('malformed_response'); return; } | 
|  | +      finish(null, response); | 
|  | +    }); | 
|  | +  }); | 
|  | +} | 
|  | + | 
|  | +/** Prefer server-validated client reports, including malformed identities so | 
|  | + * validation fails closed instead of falling back to a stale legacy report. */ | 
|  | +export function authoritativeSessionCandidates(agents, sessionId) { | 
|  | +  const matches = agents.filter(agent => agent?.agent_session?.kind === 'id' && agent.agent_session.value === sessionId); | 
|  | +  const clients = matches.filter(agent => agent.agent_session.source === 'herdr:codex-client'); | 
|  | +  return clients.length ? clients : matches; | 
|  | +} | 
|  | + | 
|  | +/** | 
|  | + * Accepts the inner response.result.snapshot. Its agents array is Herdr's live | 
|  | + * agent list (including idle/done agents); no focus, cwd or hint establishes | 
|  | + * session identity. Only exact agent_session.kind=id/value matches are eligible. | 
|  | + */ | 
|  | +export function selectHerdrIdentity(snapshot, {sessionId, paneHint} = {}) { | 
|  | +  const unresolved = reason => ({status: 'identity_unresolved', reason, hint: paneHint}); | 
|  | +  if (!id(sessionId)) return unresolved('invalid_session_id'); | 
|  | +  if (!record(snapshot) \|\| !Array.isArray(snapshot.agents) \|\| !snapshot.agents.every(record)) return unresolved('malformed_snapshot'); | 
|  | +  const matches = authoritativeSessionCandidates(snapshot.agents, sessionId); | 
|  | +  if (!matches.length) return unresolved('session_not_found'); | 
|  | +  if (matches.length !== 1) return unresolved('duplicate_session'); | 
|  | +  const agent = matches[0]; | 
|  | +  if (agent.agent !== 'codex' \|\| (agent.agent_session.agent != null && agent.agent_session.agent !== 'codex') \|\| !id(agent.pane_id) \|\| typeof agent.cwd !== 'string' \|\| !path.isAbsolute(agent.cwd) \|\| agent.cwd.includes('\0') \|\| agent.cwd.length > 4096) return unresolved('invalid_identity'); | 
|  | +  if (snapshot.agents.filter(other => other.pane_id === agent.pane_id).length !== 1) return unresolved('duplicate_pane'); | 
|  | +  if (['terminal_id', 'tab_id'].some(key => agent[key] != null && !id(agent[key]))) return unresolved('invalid_identity'); | 
|  | +  const identity = {session_id: sessionId, pane_id: agent.pane_id, cwd: agent.cwd}; | 
|  | +  for (const key of ['terminal_id', 'tab_id']) if (agent[key] != null) identity[key] = agent[key]; | 
|  | +  return {status: 'verified', reason: paneHint && paneHint !== agent.pane_id ? 'pane_hint_repaired' : 'session_match', identity, hint: paneHint}; | 
|  | +} | 
|  | + | 
|  | +/** Injectable request has the same full-envelope contract as herdrRequest. */ | 
|  | +export async function resolveHerdrIdentity({sessionId, paneHint, socket, request = herdrRequest} = {}) { | 
|  | +  const unresolved = reason => ({status: 'identity_unresolved', reason, hint: paneHint}); | 
|  | +  if (!id(sessionId)) return unresolved('invalid_session_id'); | 
|  | +  let timer; | 
|  | +  try { | 
|  | +    // Also bound injected transports that fail to honor their timeout option. | 
|  | +    const response = await Promise.race([ | 
|  | +      Promise.resolve().then(() => request(socket, 'session.snapshot', {}, {timeoutMs: TIMEOUT_MS})), | 
|  | +      new Promise((_, reject) => { timer = setTimeout(() => reject(failure('request_timeout')), TIMEOUT_MS); }) | 
|  | +    ]); | 
|  | +    if (!record(response) \|\| Object.hasOwn(response, 'error') \|\| !record(response.result)) return unresolved('malformed_response'); | 
|  | +    return selectHerdrIdentity(response.result.snapshot, {sessionId, paneHint}); | 
|  | +  } catch (error) { | 
|  | +    return unresolved(errorCodes.has(error?.code) ? error.code : 'request_failed'); | 
|  | +  } finally { clearTimeout(timer); } | 
|  | +} | 
|  | diff --git a/scripts/install-runtime.mjs b/scripts/install-runtime.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..df44177 | 
|  | --- /dev/null | 
|  | +++ b/scripts/install-runtime.mjs | 
|  | @@ -0,0 +1,40 @@ | 
|  | +#!/usr/bin/env node | 
|  | +import fs from 'node:fs'; | 
|  | +import os from 'node:os'; | 
|  | +import path from 'node:path'; | 
|  | +import crypto from 'node:crypto'; | 
|  | +import {fileURLToPath} from 'node:url'; | 
|  | +import {spawnSync} from 'node:child_process'; | 
|  | + | 
|  | +// Publish an immutable release outside Codex's disposable plugin cache. | 
|  | +// Swapping current leaves already-running hooks and monitors untouched. | 
|  | +export function installRuntime(source, home=process.env.CODEX_HOME\|\|path.join(os.homedir(),'.codex')) { | 
|  | +  const files=['scripts/harness.mjs','scripts/herdr-identity.mjs','scripts/herdr-binding.mjs','scripts/verification.mjs','scripts/execution.mjs','scripts/task-loop.mjs','scripts/queue-transport.mjs','scripts/continuation-watch-trigger.mjs','hooks/hooks.json','.codex-plugin/plugin.json']; | 
|  | +  const contents=files.map(file=>fs.readFileSync(path.join(source,file))); | 
|  | +  const hash=crypto.createHash('sha256'); | 
|  | +  for(let i=0;i<files.length;i++)hash.update(files[i]).update('\0').update(contents[i]); | 
|  | +  const base=path.join(home,'harness-runtime'); | 
|  | +  const release=path.join(base,'releases',hash.digest('hex')); | 
|  | +  fs.mkdirSync(path.dirname(release),{recursive:true,mode:0o700}); | 
|  | +  if(!fs.existsSync(release)) { | 
|  | +    const staging=fs.mkdtempSync(path.join(base,'releases','.install-')); | 
|  | +    try { | 
|  | +      files.forEach((file,i)=>{const target=path.join(staging,file);fs.mkdirSync(path.dirname(target),{recursive:true,mode:0o700});fs.writeFileSync(target,contents[i],{mode:0o600});}); | 
|  | +      try{fs.renameSync(staging,release);}catch(error){if(!fs.existsSync(release))throw error;} | 
|  | +    }finally{fs.rmSync(staging,{recursive:true,force:true});} | 
|  | +  } | 
|  | +  const current=path.join(base,'current'); | 
|  | +  const next=path.join(base,`.current-${crypto.randomUUID()}`); | 
|  | +  fs.symlinkSync(path.relative(base,release),next,'dir'); | 
|  | +  try{fs.renameSync(next,current);}finally{if(fs.existsSync(next))fs.unlinkSync(next);} | 
|  | +  const git=(...args)=>{const r=spawnSync('git',['-C',source,...args],{encoding:'utf8',timeout:2000});return r.status===0?r.stdout.trim():null;}; | 
|  | +  const status=git('status','--porcelain'); | 
|  | +  const receipt={source:fs.realpathSync(source),source_commit:git('rev-parse','HEAD'),source_clean:status===null?null:status==='',release,installed_at:new Date().toISOString()}; | 
|  | +  const receiptFile=path.join(base,`.installation-${crypto.randomUUID()}.json`); | 
|  | +  fs.writeFileSync(receiptFile,JSON.stringify(receipt,null,2)+'\n',{mode:0o600}); | 
|  | +  fs.renameSync(receiptFile,path.join(base,'installation.json')); | 
|  | +  return path.join(current,'scripts/harness.mjs'); | 
|  | +} | 
|  | +if(process.argv[1]&&path.resolve(process.argv[1])===fileURLToPath(import.meta.url)) { | 
|  | +  console.log(installRuntime(path.resolve(path.dirname(fileURLToPath(import.meta.url)),'..'))); | 
|  | +} | 
|  | diff --git a/scripts/queue-transport.mjs b/scripts/queue-transport.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..137d91b | 
|  | --- /dev/null | 
|  | +++ b/scripts/queue-transport.mjs | 
|  | @@ -0,0 +1,88 @@ | 
|  | +import crypto from 'node:crypto'; | 
|  | +import {spawn} from 'node:child_process'; | 
|  | + | 
|  | +const MAX_MESSAGE=262144,MAX_HEADER=8192,MAX_WIRE=1048576,MAX_FRAMES=1024; | 
|  | +const decoder=new TextDecoder('utf-8',{fatal:true}); | 
|  | +function clientFrame(opcode,payload){ | 
|  | +  const size=payload.length,header=Buffer.alloc(size<126?2:size<=65535?4:10),mask=crypto.randomBytes(4); | 
|  | +  header[0]=128\|opcode;header[1]=128\|(size<126?size:size<=65535?126:127); | 
|  | +  if(size>=126&&size<=65535)header.writeUInt16BE(size,2);else if(size>65535)header.writeBigUInt64BE(BigInt(size),2); | 
|  | +  const masked=Buffer.from(payload);for(let i=0;i<size;i++)masked[i]^=mask[i%4]; | 
|  | +  return Buffer.concat([header,mask,masked]); | 
|  | +} | 
|  | + | 
|  | +// The CLI proxy forwards bytes to the existing Unix control socket. It does not | 
|  | +// convert WebSocket into JSONL. One short-lived connection carries one RPC; no | 
|  | +// reconnect or retry is safe after a potentially accepted queue mutation. | 
|  | +export function queueRpc({binary='codex',socket,timeout_ms=3000},method,params){ | 
|  | +  return new Promise((resolve,reject)=>{ | 
|  | +    let request;try{request=Buffer.from(JSON.stringify({id:2,method,params}));}catch{return reject(Error('queue_request_invalid'));} | 
|  | +    if(request.length>MAX_MESSAGE)return reject(Error('queue_request_overflow')); | 
|  | +    const key=crypto.randomBytes(16).toString('base64'),accept=crypto.createHash('sha1').update(key+'258EAFA5-E914-47DA-95CA-C5AB0DC85B11').digest('base64'); | 
|  | +    let child;try{child=spawn(binary,['app-server','proxy',...(socket?['--sock',socket]:[])],{stdio:['pipe','pipe','pipe']});}catch{return reject(Error('queue_backend_unavailable'));} | 
|  | +    let buffer=Buffer.alloc(0),upgraded=false,stage='initialize',done=false,total=0,frames=0,fragment=null,fragmentSize=0; | 
|  | +    const finish=(code,result)=>{if(done)return;done=true;clearTimeout(timer);child.stdin.destroy();child.kill();code?reject(Error(code)):resolve(result);}; | 
|  | +    const timer=setTimeout(()=>finish('queue_transport_uncertain'),Math.max(1,Math.min(5000,Number.isFinite(timeout_ms)?timeout_ms:3000))); | 
|  | +    const send=(opcode,payload)=>{if(!done)child.stdin.write(clientFrame(opcode,payload));}; | 
|  | +    const json=value=>send(1,Buffer.from(JSON.stringify(value))); | 
|  | +    child.on('error',()=>finish('queue_backend_unavailable')); | 
|  | +    child.on('exit',()=>finish('queue_transport_uncertain')); | 
|  | +    child.stdin.on('error',()=>finish('queue_transport_uncertain')); | 
|  | +    child.stdout.on('error',()=>finish('queue_transport_uncertain')); | 
|  | +    child.stderr.on('error',()=>finish('queue_transport_uncertain'));child.stderr.resume(); | 
|  | +    function message(payload){ | 
|  | +      let msg;try{msg=JSON.parse(decoder.decode(payload));}catch{return finish('queue_response_invalid');} | 
|  | +      if(!msg\|\|typeof msg!=='object'\|\|Array.isArray(msg))return finish('queue_response_invalid'); | 
|  | +      if(msg.id!==1&&msg.id!==2)return; | 
|  | +      const result=Object.hasOwn(msg,'result'),error=Object.hasOwn(msg,'error'); | 
|  | +      if(result===error)return finish('queue_response_invalid'); | 
|  | +      if(msg.id===1){ | 
|  | +        if(stage!=='initialize')return finish('queue_response_order_invalid'); | 
|  | +        if(error)return finish('queue_initialize_failed'); | 
|  | +        // Stage changes before writing: a duplicate initialize acknowledgement | 
|  | +        // can never issue a second mutation, even inside one stdout chunk. | 
|  | +        stage='request';json({method:'initialized'});send(1,request); | 
|  | +      }else{ | 
|  | +        if(stage!=='request')return finish('queue_response_order_invalid'); | 
|  | +        finish(error?'queue_request_rejected':null,msg.result); | 
|  | +      } | 
|  | +    } | 
|  | +    child.stdout.on('data',chunk=>{ | 
|  | +      if(done)return; | 
|  | +      try{ | 
|  | +        total+=chunk.length;if(total>MAX_WIRE)return finish('queue_response_overflow'); | 
|  | +        buffer=Buffer.concat([buffer,chunk]); | 
|  | +        if(!upgraded){ | 
|  | +          const end=buffer.indexOf('\r\n\r\n'); | 
|  | +          if(end<0){if(buffer.length>MAX_HEADER)finish('queue_handshake_overflow');return;} | 
|  | +          if(end+4>MAX_HEADER)return finish('queue_handshake_overflow'); | 
|  | +          const lines=buffer.subarray(0,end).toString('latin1').split('\r\n'),status=lines.shift(),headers=new Map(); | 
|  | +          for(const line of lines){const colon=line.indexOf(':');if(colon<=0)return finish('queue_handshake_invalid');const name=line.slice(0,colon).toLowerCase(),value=line.slice(colon+1).trim();if(headers.has(name))return finish('queue_handshake_invalid');headers.set(name,value);} | 
|  | +          const tokens=name=>(headers.get(name)\|\|'').toLowerCase().split(',').map(s=>s.trim()); | 
|  | +          if(!/^HTTP\/1\.1 101(?: \|$)/.test(status)\|\|!tokens('upgrade').includes('websocket')\|\|!tokens('connection').includes('upgrade')\|\|headers.get('sec-websocket-accept')!==accept\|\|headers.has('sec-websocket-extensions')\|\|headers.has('sec-websocket-protocol'))return finish('queue_handshake_invalid'); | 
|  | +          buffer=buffer.subarray(end+4);upgraded=true; | 
|  | +          json({id:1,method:'initialize',params:{clientInfo:{name:'harness-task-loop',version:'1'},capabilities:{experimentalApi:true}}}); | 
|  | +        } | 
|  | +        while(!done&&buffer.length>=2){ | 
|  | +          const fin=!!(buffer[0]&128),opcode=buffer[0]&15,control=opcode>=8;let size=buffer[1]&127,offset=2; | 
|  | +          if(buffer[0]&112\|\|buffer[1]&128\|\|![0,1,8,9,10].includes(opcode)\|\|control&&(!fin\|\|size>125))return finish('queue_frame_invalid'); | 
|  | +          if(size===126){if(buffer.length<4)return;size=buffer.readUInt16BE(2);offset=4;if(size<126)return finish('queue_frame_invalid');} | 
|  | +          else if(size===127){if(buffer.length<10)return;const big=buffer.readBigUInt64BE(2);if(big>BigInt(MAX_MESSAGE))return finish('queue_response_overflow');size=Number(big);offset=10;if(size<=65535)return finish('queue_frame_invalid');} | 
|  | +          if(size>MAX_MESSAGE\|\|!control&&size+fragmentSize>MAX_MESSAGE)return finish('queue_response_overflow'); | 
|  | +          if(buffer.length<offset+size)return; | 
|  | +          if(++frames>MAX_FRAMES)return finish('queue_response_overflow'); | 
|  | +          const payload=buffer.subarray(offset,offset+size);buffer=buffer.subarray(offset+size); | 
|  | +          if(opcode===8){ | 
|  | +            if(size===1)return finish('queue_frame_invalid'); | 
|  | +            if(size>=2){const code=payload.readUInt16BE();if(!([1000,1001,1002,1003,1007,1008,1009,1010,1011,1012,1013,1014].includes(code)\|\|code>=3000&&code<=4999))return finish('queue_frame_invalid');decoder.decode(payload.subarray(2));} | 
|  | +            send(8,payload);return finish('queue_transport_closed'); | 
|  | +          } | 
|  | +          if(opcode===9){send(10,payload);continue;}if(opcode===10)continue; | 
|  | +          if(opcode===0){if(!fragment)return finish('queue_frame_invalid');fragment.push(payload);fragmentSize+=size;if(fin){const complete=Buffer.concat(fragment,fragmentSize);fragment=null;fragmentSize=0;message(complete);}} | 
|  | +          else{if(fragment)return finish('queue_frame_invalid');if(fin)message(payload);else{fragment=[payload];fragmentSize=size;}} | 
|  | +        } | 
|  | +      }catch{finish('queue_response_invalid');} | 
|  | +    }); | 
|  | +    child.stdin.write(`GET / HTTP/1.1\r\nHost: localhost\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: ${key}\r\nSec-WebSocket-Version: 13\r\n\r\n`); | 
|  | +  }); | 
|  | +} | 
|  | diff --git a/scripts/task-loop.mjs b/scripts/task-loop.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..7138763 | 
|  | --- /dev/null | 
|  | +++ b/scripts/task-loop.mjs | 
|  | @@ -0,0 +1,453 @@ | 
|  | +import fs from 'node:fs'; | 
|  | +import path from 'node:path'; | 
|  | +import crypto from 'node:crypto'; | 
|  | +import {spawnSync} from 'node:child_process'; | 
|  | +import {assessVerification} from './verification.mjs'; | 
|  | +import {herdrRequest} from './herdr-identity.mjs'; | 
|  | +import {queueRpc} from './queue-transport.mjs'; | 
|  | +const hash=value=>crypto.createHash('sha256').update(Buffer.isBuffer(value)?value:String(value)).digest('hex'); | 
|  | +const read=file=>{try{return JSON.parse(fs.readFileSync(file,'utf8'));}catch{return null;}}; | 
|  | +const text=value=>typeof value==='string'&&value.trim().length>0&&value.length<=4096; | 
|  | +const id=value=>typeof value==='string'&&/^[a-zA-Z0-9][a-zA-Z0-9._:-]{0,199}$/.test(value); | 
|  | +const states=new Set(['working','idle','done','unknown','blocked','missing']); | 
|  | +function write(file,data){fs.mkdirSync(path.dirname(file),{recursive:true,mode:0o700});const tmp=file+'.'+crypto.randomUUID();fs.writeFileSync(tmp,JSON.stringify(data,null,2)+'\n',{mode:0o600});fs.renameSync(tmp,file);} | 
|  | +const fresh=(at,now)=>Number.isFinite(Date.parse(at))&&Date.parse(at)<=now&&now-Date.parse(at)<=30000; | 
|  | +const identityTuple=value=>value&&id(value.session_id)&&id(value.pane_id)&&path.isAbsolute(value.cwd\|\|'')&&(!value.tab_id\|\|id(value.tab_id)); | 
|  | +const identity=value=>identityTuple(value)&&fs.existsSync(value.cwd); | 
|  | +const same=(a,b)=>a?.session_id===b?.session_id&&a?.pane_id===b?.pane_id&&a?.cwd===b?.cwd&&(!a?.tab_id\|\|a.tab_id===b?.tab_id); | 
|  | +const liveAssignments=task=>Object.values(task.assignments).filter(a=>!['retired','superseded'].includes(a.lifecycle?.status)); | 
|  | +const exact=value=>({session_id:value.session_id,pane_id:value.pane_id,cwd:value.cwd,...(value.tab_id?{tab_id:value.tab_id}:{})}); | 
|  | +function processSignature(pid){const r=spawnSync('ps',['-p',String(pid),'-o','lstart=','-o','command='],{encoding:'utf8',timeout:500});return r.status===0?hash(r.stdout.trim()):null;} | 
|  | + | 
|  | +// The one shared state lock serializes authority and dispatch across hook/monitor | 
|  | +// processes. Crash recovery never retries a dispatch whose fence reached disk. | 
|  | +async function locked(state,fn,{deadline=Date.now()+20000}={}){ | 
|  | +  const directory=path.join(state,'tasks','mutation.lock');fs.mkdirSync(path.dirname(directory),{recursive:true,mode:0o700}); | 
|  | +  const nonce=crypto.randomUUID(),token=path.join(directory,'owner.'+nonce+'.json');let acquired=false; | 
|  | +  for(let n=0;n<1000&&Date.now()<deadline;n++){ | 
|  | +    try{ | 
|  | +      fs.mkdirSync(directory,{mode:0o700});const inode=fs.statSync(directory); | 
|  | +      fs.writeFileSync(token,JSON.stringify({pid:process.pid,signature:processSignature(process.pid),nonce}),{flag:'wx',mode:0o600}); | 
|  | +      const current=fs.statSync(directory); | 
|  | +      if(inode.ino!==current.ino\|\|inode.dev!==current.dev){try{fs.unlinkSync(token);}catch{}throw Error('Authority directory changed during registration.');} | 
|  | +      acquired=true;break; | 
|  | +    }catch(error){ | 
|  | +      if(error.code!=='EEXIST')throw error; | 
|  | +      try{ | 
|  | +        const names=fs.readdirSync(directory); | 
|  | +        // Delete only a dead owner's immutable token. rmdir cannot remove a | 
|  | +        // replacement live owner's nonempty directory (no check/unlink TOCTOU). | 
|  | +        if(names.length===1&&/^owner\.[a-f0-9-]+\.json$/.test(names[0])){ | 
|  | +          const oldToken=path.join(directory,names[0]),old=read(oldToken); | 
|  | +          if(old?.signature&&processSignature(old.pid)!==old.signature){try{fs.unlinkSync(oldToken);}catch{}try{fs.rmdirSync(directory);}catch{}} | 
|  | +        }else if(!names.length&&Date.now()-fs.statSync(directory).mtimeMs>5000){try{fs.rmdirSync(directory);}catch{}} | 
|  | +      }catch{} | 
|  | +      await new Promise(r=>setTimeout(r,20)); | 
|  | +    } | 
|  | +  } | 
|  | +  if(!acquired)throw Error('Task authority lock busy; no dispatch attempted.'); | 
|  | +  try{return await fn();}finally{try{fs.unlinkSync(token);}catch{}try{fs.rmdirSync(directory);}catch{}} | 
|  | +} | 
|  | +export async function probeHerdr(expected,{timeout_ms=2000}={}){ | 
|  | +  const socket = process.env.HERDR_SOCKET_PATH; | 
|  | +  const unavailable = {...expected, state:'missing', observed_at:new Date().toISOString(), source:'herdr_live', identity_verified:false}; | 
|  | +  if (process.env.HERDR_ENV !== '1' \|\| !socket \|\| !process.env.HERDR_PANE_ID \|\| (expected.socket && expected.socket !== socket)) return unavailable; | 
|  | +  const deadline = Date.now() + timeout_ms; | 
|  | +  const request = (method, params) => herdrRequest(socket, method, params, {timeoutMs:deadline-Date.now()}); | 
|  | +  const pinned = expected.terminal_id && expected.agent_session; | 
|  | +  function matches(agent) { | 
|  | +    const session = agent?.agent_session; | 
|  | +    if (agent?.agent !== 'codex' \|\| session?.kind !== 'id' \|\| session.value !== expected.session_id) return false; | 
|  | +    if (agent.cwd !== expected.cwd \|\| (agent.foreground_cwd && agent.foreground_cwd !== expected.cwd)) return false; | 
|  | +    if (!pinned) return agent.pane_id === expected.pane_id && (!expected.tab_id \|\| agent.tab_id === expected.tab_id); | 
|  | +    return agent.terminal_id === expected.terminal_id && ['source','agent','kind','value'].every(k => session[k] === expected.agent_session[k]); | 
|  | +  } | 
|  | +  async function lookup(pane_id) { | 
|  | +    const result = spawnSync('herdr', ['agent','get',pane_id], {encoding:'utf8', timeout:Math.max(1,deadline-Date.now()), maxBuffer:65536}); | 
|  | +    try { return result.status === 0 ? JSON.parse(result.stdout).result.agent : null; } | 
|  | +    catch { return null; } | 
|  | +  } | 
|  | +  let agent = await lookup(expected.pane_id); | 
|  | +  if (!matches(agent) && pinned && Date.now() < deadline) { | 
|  | +    const snapshot = (await request('session.snapshot', {})).result.snapshot; | 
|  | +    const candidates = snapshot.agents.filter(matches); | 
|  | +    if (candidates.length === 1 && Date.now() < deadline) agent = await lookup(candidates[0].pane_id); | 
|  | +  } | 
|  | +  if (!matches(agent)) return unavailable; | 
|  | +  const reference = agent.agent_session; | 
|  | +  const proof = agent.terminal_id && ['source','agent','kind','value'].every(k => typeof reference[k] === 'string' && reference[k]) | 
|  | +    ? {socket, terminal_id:agent.terminal_id, agent_session:{...reference}} : {}; | 
|  | +  return {...expected, ...proof, pane_id:agent.pane_id, tab_id:agent.tab_id, workspace_id:agent.workspace_id, | 
|  | +    state:states.has(agent.agent_status)?agent.agent_status:'unknown', observed_at:new Date().toISOString(), source:'herdr_live', identity_verified:true}; | 
|  | +} | 
|  | + | 
|  | +// Supported control-socket proxy, not terminal prompt injection or SQLite edits. | 
|  | +// Bounded JSON-RPC; only exact acknowledged submission IDs may be started/deleted. | 
|  | +export function createCodexQueue({binary='codex',socket,timeout_ms=3000}={}){ | 
|  | +  const rpc=(method,params)=>queueRpc({binary,socket,timeout_ms},method,params); | 
|  | +  return { | 
|  | +    available:async({threadId}={})=>{if(!id(threadId))return false;try{await rpc('thread/queue/list',{threadId,limit:1});return true;}catch{return false;}}, | 
|  | +    add:async item=>{const result=await rpc('thread/queue/add',item);const submission=result?.queuedSubmission;if(!id(submission?.id)\|\|submission.clientUserMessageId!==item.clientUserMessageId)throw Error('queue_acknowledgement_invalid');return {queuedSubmissionId:submission.id};}, | 
|  | +    start:async item=>{const result=await rpc('thread/queue/start',item);if(!id(result?.turn?.id))throw Error('queue_start_acknowledgement_invalid');return result;},delete:item=>rpc('thread/queue/delete',item),list:item=>rpc('thread/queue/list',item) | 
|  | +  }; | 
|  | +} | 
|  | +export function createTaskRuntime({state,probe=probeHerdr,queue=createCodexQueue(),now=()=>Date.now(),owner,verifier}={}){ | 
|  | +  if(!state)throw Error('Task runtime needs explicit state directory.'); | 
|  | +  const file=task=>path.join(state,'tasks',hash(task)+'.json'); | 
|  | +  const memberFile=session=>path.join(state,'sessions',hash(session),'task-membership.json'); | 
|  | +  const revocationFile=task=>path.join(state,'tasks',hash(task)+'.revocation.json'); | 
|  | +  const revoked=task=>read(revocationFile(task.task_id)); | 
|  | +  const applyRevocation=task=>{const fence=task&&revoked(task);if(fence){task.status=fence.status;task.generation=Math.max(task.generation,fence.generation);}return task;}; | 
|  | +  const get=task=>applyRevocation(read(file(task))); | 
|  | +  const revoke=(task,status)=>{const previous=revoked(task);write(revocationFile(task.task_id),{task_id:task.task_id,parent_session_id:task.parent.session_id,status:previous?.status==='cancelled'?'cancelled':status,generation:task.generation+1,requested_at:new Date(now()).toISOString()});}; | 
|  | +  const save=task=>{applyRevocation(task);task.updated_at=new Date(now()).toISOString();write(file(task.task_id),task);if(['completed','cancelled','closed'].includes(task.status))endBindings(task);}; | 
|  | +  const current=session=>{const m=read(memberFile(session)),t=m&&m.active!==false?get(m.task_id):null;return t&&!['completed','cancelled','closed'].includes(t.status)&&(m.role==='observer'?t.lifecycle?.observers?.[session]:member(t,session))?t:null;}; | 
|  | +  const forTurn=(session,turn)=>{const m=read(memberFile(session));const t=m&&(m.active!==false\|\|m.terminal_turn_id===turn)?get(m.task_id):null;return t?.mode==='observation_only'\|\|m?.role==='observer'?null:t;}; | 
|  | +  const endBindings=task=>{for(const session of [task.parent.session_id,...liveAssignments(task).map(a=>a.worker.session_id),...Object.keys(task.lifecycle?.observers\|\|{})].filter(id)){const m=read(memberFile(session));if(m?.task_id===task.task_id&&m.active!==false)write(memberFile(session),{...m,active:false,terminal_turn_id:read(path.join(state,'sessions',hash(session),'session.json'))?.turn_id\|\|task.reports[session]?.turn_id\|\|null});}}; | 
|  | +  const owned=task=>{if(task.mode==='observation_only')return false;const o=owner??read(path.join(state,'continuation-owner.json'));return o?.exclusive===true&&o.owner==='harness_monitor'&&o.backend==='codex_app_server'&&(!o.conflicting_owners\|\|o.conflicting_owners.length===0)&&task.continuation.owner===o.owner&&task.continuation.backend===o.backend;}; | 
|  | +  const member=(task,session)=>task.mode==='observation_only'?null:task.parent.session_id===session?{identity:task.parent,assignment_id:null}:liveAssignments(task).find(a=>a.worker.session_id===session)?(()=>{const a=liveAssignments(task).find(a=>a.worker.session_id===session);return {identity:a.worker,assignment_id:a.assignment_id};})():null; | 
|  | +  const requireMember=async(task,session,parentOnly=false)=>{if(!task)throw Error('Task unavailable.');const m=member(task,session);if(!m\|\|(parentOnly&&m.assignment_id!==null))throw Error('Task member identity/authority mismatch.');const o=await observe(task,m.identity,m.assignment_id);if(!o.identity_verified\|\|!fresh(o.observed_at,now()))throw Error('Task member identity is not freshly verified.');return m;}; | 
|  | +  async function observe(task,expected,assignment_id,{deadline=Infinity}={}){ | 
|  | +    let measured;try{if(Date.now()<deadline)measured=await probe(expected,{timeout_ms:Math.max(1,Math.min(2000,deadline-Date.now()))});}catch{} | 
|  | +    if (measured?.identity_verified && measured.source === 'herdr_live' && measured.terminal_id && measured.agent_session && | 
|  | +        measured.session_id === expected.session_id && measured.cwd === expected.cwd) { | 
|  | +      Object.assign(expected, {socket:measured.socket, terminal_id:measured.terminal_id, agent_session:measured.agent_session, | 
|  | +        pane_id:measured.pane_id, tab_id:measured.tab_id, workspace_id:measured.workspace_id}); | 
|  | +    } | 
|  | +    const o={event_id:crypto.randomUUID(),assignment_id,session_id:expected.session_id,pane_id:expected.pane_id,cwd:expected.cwd,...(expected.tab_id?{tab_id:expected.tab_id}:{}),state:states.has(measured?.state)?measured.state:'missing',observed_at:measured?.observed_at\|\|new Date(now()).toISOString(),source:'herdr_live',identity_verified:measured?.source==='herdr_live'&&measured.identity_verified===true&&same(measured,expected)}; | 
|  | +    task.observations.push(o);task.observations=task.observations.slice(-256); | 
|  | +    if(assignment_id&&task.assignments[assignment_id]&&o.identity_verified&&fresh(o.observed_at,now())&&o.state==='working')task.assignments[assignment_id].was_working=true; | 
|  | +    return o; | 
|  | +  } | 
|  | +  async function acceptance(task,{deadline=Date.now()+1500}={}){ | 
|  | +    const at=new Date(now()).toISOString();let result={status:'unverified',evidence_ids:[]}; | 
|  | +    try{ | 
|  | +      if(verifier)result=await verifier(task); | 
|  | +      else {const dir=path.join(state,'sessions',hash(task.parent.session_id)),c=read(path.join(dir,'contract.json')); | 
|  | +        if(c?.schema_version===2&&c.task_id===task.task_id){const assessed=assessVerification(c,dir,{deadline});result={status:assessed.status==='failed'?'fail':assessed.status,evidence_ids:assessed.status==='pass'&&assessed.checks.length?assessed.checks.filter(x=>x.status==='pass').map(x=>hash(fs.readFileSync(x.receipt))):[]};}} | 
|  | +    }catch{} | 
|  | +    return {status:['pass','fail','unverified'].includes(result.status)&&!(result.status==='pass'&&!result.evidence_ids?.length)?result.status:'unverified',source:'harness_verifier',observed_at:at,evidence_ids:(result.evidence_ids\|\|[]).filter(id)}; | 
|  | +  } | 
|  | +  function remaining(task){return task.remaining.length;} | 
|  | +  function token(task,handoff){return `[HARNESS_CONTINUATION ${task.task_id} ${task.generation} ${handoff.event_id}]`;} | 
|  | +  async function deletePending(task){for(const h of task.handoffs){if(h.queued_submission_id&&!h.delete_attempted){h.delete_attempted=true;save(task);try{if(typeof queue?.delete!=='function')throw Error('backend_unavailable');await queue.delete({threadId:task.parent.session_id,queuedSubmissionId:h.queued_submission_id});h.reason='cancelled_queue_deleted';}catch{h.reason='cancel_delete_uncertain';}}}} | 
|  | +  async function pump(task,{dispatch=true,deadline=Infinity}={}){ | 
|  | +    if(task.status!=='active'\|\|revoked(task)){await deletePending(task);save(task);return;} | 
|  | +    if(task.mode==='observation_only'){for(const a of liveAssignments(task))await observe(task,a.worker,a.assignment_id,{deadline});save(task);return;} | 
|  | +    if(!task.continuation.enabled){ | 
|  | +      const pinned=liveAssignments(task).filter(a=>a.worker.terminal_id && a.worker.agent_session); | 
|  | +      if(pinned.length){ | 
|  | +        for(const a of pinned) await observe(task,a.worker,a.assignment_id,{deadline}); | 
|  | +        save(task); | 
|  | +      } | 
|  | +      return; | 
|  | +    } | 
|  | +    if(Date.parse(task.continuation.expires_at)<=now()){task.continuation.fenced_reason='expired';await deletePending(task);save(task);return;} | 
|  | +    if(task.probes>=task.continuation.max_probes){task.continuation.fenced_reason='probe_budget';save(task);return;} | 
|  | +    task.probes++;const parentObservation=await observe(task,task.parent,null,{deadline});task.parent_observation=parentObservation; | 
|  | +    for(const a of liveAssignments(task)){ | 
|  | +      const o=await observe(task,a.worker,a.assignment_id,{deadline}); | 
|  | +      if(!fresh(o.observed_at,now())\|\|!o.identity_verified)continue; | 
|  | +      if(o.state==='working')a.was_working=true; | 
|  | +      if(a.was_working&&!a.finished_event_id&&['idle','done'].includes(o.state)){ | 
|  | +        a.finished_event_id=o.event_id; | 
|  | +        task.handoffs.push({event_id:hash(task.task_id+':'+a.assignment_id+':'+a.assigned_at),assignment_id:a.assignment_id,worker_finished_at:o.observed_at,parent_continued_at:null,dispatch_status:'pending',attempts:0,next_probe_at:0}); | 
|  | +      } | 
|  | +    } | 
|  | +    save(task); | 
|  | +    if(!dispatch)return; | 
|  | +    for(const h of task.handoffs){ | 
|  | +      if(revoked(task)){await deletePending(task);save(task);return;} | 
|  | +      // Persisted fence survives crashes at every point of external dispatch. | 
|  | +      if(!liveAssignments(task).some(a=>a.assignment_id===h.assignment_id)\|\|h.dispatch_status!=='pending'\|\|h.next_probe_at>now()\|\|!remaining(task)\|\|!text(task.next_action))continue; | 
|  | +      const defer=reason=>{h.reason=reason;h.next_probe_at=now()+task.continuation.backoff_ms;save(task);}; | 
|  | +      if(!owned(task)){defer('continuation_owner_conflict');continue;} | 
|  | +      if(task.wakes>=task.continuation.max_wakes){defer('wake_budget');continue;} | 
|  | +      let p=await observe(task,task.parent,null);task.parent_observation=p; | 
|  | +      if(!p.identity_verified\|\|!fresh(p.observed_at,now())){defer('parent_identity_unverified');continue;} | 
|  | +      if(!['idle','done'].includes(p.state)){defer(p.state==='working'?'parent_busy':'parent_not_idle');continue;} | 
|  | +      let available=false;try{available=Boolean(queue&&await queue.available({threadId:task.parent.session_id}));}catch{} | 
|  | +      if(!available){defer('backend_unavailable');continue;} | 
|  | +      // Availability probe may take time. Recheck both authority and live identity. | 
|  | +      p=await observe(task,task.parent,null);task.parent_observation=p; | 
|  | +      if(revoked(task)\|\|!owned(task)\|\|Date.parse(task.continuation.expires_at)<=now()\|\|!liveAssignments(task).some(a=>a.assignment_id===h.assignment_id)){defer('authorization_expired_or_owner_conflict');continue;} | 
|  | +      if(!p.identity_verified\|\|!fresh(p.observed_at,now())\|\|!['idle','done'].includes(p.state)){defer('parent_changed');continue;} | 
|  | +      const dispatchFile=path.join(state,'tasks','dispatch',h.event_id+'.json'); | 
|  | +      fs.mkdirSync(path.dirname(dispatchFile),{recursive:true,mode:0o700}); | 
|  | +      try{fs.writeFileSync(dispatchFile,JSON.stringify({task_id:task.task_id,event_id:h.event_id,at:new Date(now()).toISOString()}),{flag:'wx',mode:0o600});} | 
|  | +      catch(error){if(error.code!=='EEXIST')throw error;h.dispatch_status='uncertain';h.reason='existing_dispatch_fence';save(task);continue;} | 
|  | +      h.dispatch_status='uncertain';h.reason='dispatch_fence';h.attempts++;task.wakes++;save(task); | 
|  | +      const input=[{type:'text',text:`${token(task,h)}\nBefore any action, check task status and generation with harness task status --task ${task.task_id}. Continue only if active, unexpired and generation matches. Pause/cancel/Interrupt revokes this message. Remaining authorized action: ${task.next_action}`,text_elements:[]}]; | 
|  | +      try{ | 
|  | +        const ack=await queue.add({threadId:task.parent.session_id,clientUserMessageId:h.event_id,input}); | 
|  | +        if(!id(ack?.queuedSubmissionId))throw Error('queue_acknowledgement_invalid'); | 
|  | +        h.queued_submission_id=ack.queuedSubmissionId;h.reason='queued';save(task); | 
|  | +        p=await observe(task,task.parent,null);task.parent_observation=p; | 
|  | +        if(revoked(task)\|\|!owned(task)\|\|Date.parse(task.continuation.expires_at)<=now()\|\|!p.identity_verified\|\|!fresh(p.observed_at,now())\|\|!['idle','done'].includes(p.state)){ | 
|  | +          h.reason='start_fenced';save(task);await deletePending(task);continue; | 
|  | +        } | 
|  | +        // Never retry start, even when its acknowledgement is lost. | 
|  | +        h.reason='start_fence';save(task); | 
|  | +        if(revoked(task)){await deletePending(task);save(task);continue;} | 
|  | +        await queue.start({threadId:task.parent.session_id,queuedSubmissionId:h.queued_submission_id});h.dispatch_status='sent';h.reason='start_acknowledged';save(task); | 
|  | +      }catch{h.dispatch_status='uncertain';h.reason='dispatch_uncertain';save(task);} | 
|  | +    } | 
|  | +  } | 
|  | +  function lifecycle(goal,taskId,coordinator){ | 
|  | +    if(goal!==undefined&&(!id(goal.goal_id)\|\|goal.tab_id!=null&&!id(goal.tab_id)))throw Error('Goal needs a valid goal_id and optional tab_id.'); | 
|  | +    return {version:1,goal:goal?{goal_id:goal.goal_id,tab_id:goal.tab_id\|\|null}:{goal_id:taskId,tab_id:null},coordinator,created_at:new Date(now()).toISOString(),observers:{},unbound_panes:{},events:[]}; | 
|  | +  } | 
|  | +  function assignmentLifecycle(actor,worker){return {status:'active',owner:{kind:'codex',session_id:worker.session_id},assigned_by:actor,created_at:new Date(now()).toISOString(),retention:{mode:'unspecified',reason:null,next_work:null},preserved_result:null};} | 
|  | +  async function authority(task,actor){ | 
|  | +    if(task?.mode==='observation_only'){ | 
|  | +      if(task.lifecycle?.coordinator.coordinator_id!==actor)throw Error('External coordinator authority mismatch.'); | 
|  | +    }else await requireMember(task,actor,true); | 
|  | +    if(task.status!=='active')throw Error('Lifecycle mutation requires an active task.'); | 
|  | +    task.lifecycle??=lifecycle(undefined,task.task_id,{kind:'codex',session_id:task.parent.session_id}); | 
|  | +  } | 
|  | +  function assignment(task,input,{includeSuperseded=false}={}){ | 
|  | +    const a=task.assignments[input?.assignment_id]; | 
|  | +    if(!a\|\|(a.lifecycle?.status==='retired'\|\|!includeSuperseded&&a.lifecycle?.status==='superseded')\|\|!identityTuple(input.expected)\|\|!same(a.worker,input.expected))throw Error('Assignment exact identity is stale or retired.'); | 
|  | +    return a; | 
|  | +  } | 
|  | +  function recordEvent(task,type,actor,assignment_id,details={}){ | 
|  | +    task.lifecycle.events.push({event_id:crypto.randomUUID(),type,actor,assignment_id,at:new Date(now()).toISOString(),...details}); | 
|  | +  } | 
|  | +  function artifacts(input){ | 
|  | +    if(!Array.isArray(input)\|\|!input.length\|\|input.length>32)throw Error('Preserved result needs hashed artifacts.'); | 
|  | +    return input.map(a=>{ | 
|  | +      if(!path.isAbsolute(a?.path\|\|'')\|\|!/^([a-f0-9]{64})$/.test(a.sha256\|\|'')\|\|hash(fs.readFileSync(a.path))!==a.sha256)throw Error('Preserved artifact missing or changed.'); | 
|  | +      return {path:a.path,sha256:a.sha256}; | 
|  | +    }); | 
|  | +  } | 
|  | +  function verifyResult(task,a,session){ | 
|  | +    // Reuse actual harness command receipts. A delivered/idle claim cannot be | 
|  | +    // substituted for a verifier pass, including for an external coordinator. | 
|  | +    if(!id(session)\|\|![a.worker.session_id,task.parent.session_id,...Object.keys(task.lifecycle.observers)].includes(session))throw Error('Verification session is not a registered result reviewer.'); | 
|  | +    const dir=path.join(state,'sessions',hash(session)),contract=read(path.join(dir,'contract.json')); | 
|  | +    if(contract?.schema_version!==2\|\|contract.task_id!==task.task_id)throw Error('Result requires a matching verification contract.'); | 
|  | +    const checked=assessVerification(contract,dir,{deadline:Date.now()+2000}); | 
|  | +    if(checked.status!=='pass'\|\|!checked.checks.length)throw Error('Result verification is not pass.'); | 
|  | +    return checked.checks.map(c=>({path:c.receipt,sha256:hash(fs.readFileSync(c.receipt))})); | 
|  | +  } | 
|  | +  async function invalidateAssignment(task,a,actor,status){ | 
|  | +    a.lifecycle??=assignmentLifecycle(actor,a.worker);a.lifecycle.status=status;a.lifecycle.ended_at=new Date(now()).toISOString();save(task); | 
|  | +    const m=read(memberFile(a.worker.session_id)); | 
|  | +    if(m?.task_id===task.task_id&&m.assignment_id===a.assignment_id)write(memberFile(a.worker.session_id),{...m,active:false,terminal_turn_id:null}); | 
|  | +    // Persist tombstone before touching the backend. Never retry an uncertain | 
|  | +    // dispatch or delete anything except this assignment's acknowledged item. | 
|  | +    for(const h of task.handoffs.filter(h=>h.assignment_id===a.assignment_id)){ | 
|  | +      h.dispatch_status='retired';h.reason='assignment_'+status; | 
|  | +      if(h.queued_submission_id&&!h.delete_attempted){h.delete_attempted=true;save(task);try{await queue.delete({threadId:task.parent.session_id,queuedSubmissionId:h.queued_submission_id});}catch{h.reason='retired_delete_uncertain';}} | 
|  | +    } | 
|  | +    delete task.reports[a.worker.session_id];save(task); | 
|  | +  } | 
|  | +  async function addAssignment(task,actor,input,{bind=true,persist=true,predecessor=null}={}){ | 
|  | +    // Central fence protects every entry point, including future callers. | 
|  | +    if(task.mode==='observation_only')bind=false; | 
|  | +    if(!id(input?.assignment_id)\|\|!identity(input.worker)\|\|!text(input.objective)\|\|!text(input.model_claim)\|\|liveAssignments(task).filter(a=>a!==predecessor).length>=16\|\|task.assignments[input.assignment_id])throw Error('Invalid or duplicate active assignment.'); | 
|  | +    if(liveAssignments(task).filter(a=>a!==predecessor).some(a=>a.worker.session_id===input.worker.session_id\|\|a.worker.pane_id===input.worker.pane_id)\|\|task.parent.session_id===input.worker.session_id)throw Error('Duplicate active worker identity.'); | 
|  | +    if(bind&¤t(input.worker.session_id)&&!(predecessor&&read(memberFile(input.worker.session_id))?.task_id===task.task_id&&read(memberFile(input.worker.session_id))?.assignment_id===predecessor.assignment_id))throw Error('Worker already assigned to a task.'); | 
|  | +    const expected={session_id:input.worker.session_id,pane_id:input.worker.pane_id,cwd:input.worker.cwd,tab_id:input.worker.tab_id}; | 
|  | +    const o=await observe(task,expected,input.assignment_id); | 
|  | +    if(!o.identity_verified\|\|!fresh(o.observed_at,now()))throw Error('Worker identity is not freshly verified.'); | 
|  | +    const a={assignment_id:input.assignment_id,worker:expected,objective:input.objective,model_claim:input.model_claim,assigned_at:new Date(now()).toISOString(),was_working:o.state==='working',finished_event_id:null,result:null,lifecycle:assignmentLifecycle(actor,input.worker)}; | 
|  | +    task.assignments[a.assignment_id]=a; | 
|  | +    recordEvent(task,'assignment_created',actor,a.assignment_id);if(persist)save(task); | 
|  | +    if(bind&&persist)write(memberFile(a.worker.session_id),{task_id:task.task_id,assignment_id:a.assignment_id,active:true}); | 
|  | +    return a; | 
|  | +  } | 
|  | +  async function retirement(actor,taskId,input,commit=false){return locked(state,async()=>{ | 
|  | +    const task=get(taskId);await authority(task,actor);const a=assignment(task,input,{includeSuperseded:true}),reasons=[]; | 
|  | +    const r=a.lifecycle?.retention,p=a.lifecycle?.preserved_result; | 
|  | +    if(r?.mode!=='release')reasons.push('retention_not_released'); | 
|  | +    if(r?.next_work!==''\|\|!text(r?.reason))reasons.push('next_work_or_unspecified_retention'); | 
|  | +    if(!p)reasons.push('preserved_verified_result_missing'); | 
|  | +    else try{artifacts(p.artifacts);const receipts=verifyResult(task,a,p.verification_session_id);if(JSON.stringify(receipts)!==JSON.stringify(p.receipts))throw Error('Verifier evidence changed.');}catch{reasons.push('preserved_result_unverified_or_changed');} | 
|  | +    const o=await observe(task,a.worker,a.assignment_id); | 
|  | +    if(!o.identity_verified\|\|!fresh(o.observed_at,now()))reasons.push('identity_unverified_or_stale'); | 
|  | +    if(!['idle','done'].includes(o.state))reasons.push('worker_busy_or_unknown'); | 
|  | +    const result={task_id:taskId,assignment_id:a.assignment_id,identity:a.worker,eligible:reasons.length===0,reasons,observed_at:o.observed_at,action:'record_only_no_pane_close'}; | 
|  | +    save(task); | 
|  | +    if(commit){if(reasons.length)throw Error('Retirement refused: '+reasons.join(', '));await invalidateAssignment(task,a,actor,'retired');recordEvent(task,'assignment_retired',actor,a.assignment_id);save(task);} | 
|  | +    return result; | 
|  | +  });} | 
|  | +  function taskBinding(task){ | 
|  | +    if(!task)return null; | 
|  | +    const identities=[...(identityTuple(task.parent)?[task.parent]:[]),...liveAssignments(task).map(a=>a.worker)]; | 
|  | +    const coverage=identities.map(expected=>{ | 
|  | +      const o=task.observations.filter(o=>same(expected,o)).at(-1); | 
|  | +      return {...expected,identity_status:o?.identity_verified&&fresh(o.observed_at,now())?'verified':'unknown',state:o?.state\|\|'unknown',observed_at:o?.observed_at\|\|null}; | 
|  | +    }); | 
|  | +    return {task_id:task.task_id,mode:task.mode\|\|'continuation',goal:task.lifecycle?.goal\|\|null,identities,coverage,unbound_panes:Object.values(task.lifecycle?.unbound_panes\|\|{})}; | 
|  | +  } | 
|  | +  const binding=session=>taskBinding(current(session)); | 
|  | +  function status(taskId){ | 
|  | +    const task=get(taskId);if(!task)return null;const b=taskBinding(task); | 
|  | +    const monitors=[...new Set([task.parent.session_id,...Object.keys(task.lifecycle?.observers\|\|{}),...(task.mode==='observation_only'?[]:liveAssignments(task).map(a=>a.worker.session_id))].filter(id))].map(session=>{ | 
|  | +      const r=read(path.join(state,'sessions',hash(session),'monitor.json')); | 
|  | +      const expectedMonitor=task.lifecycle?.observers?.[session]\|\|member(task,session)?.identity; | 
|  | +      const active=expectedMonitor&&r?.cwd===expectedMonitor.cwd&&!['completed','cancelled','closed'].includes(task.status)&&r?.owner===session&&r.signature&&r.signature===processSignature(r.pid)&&r.lifecycle?.state==='subscribed'&&r.coverage?.task_id===taskId&&Date.parse(r.coverage.captured_at)<=now()&&now()-Date.parse(r.coverage.captured_at)<=90000; | 
|  | +      return {session_id:session,active:Boolean(active),coverage:active?r.coverage:null}; | 
|  | +    }); | 
|  | +    const identities=b.coverage.map(x=>({...x,monitoring:x.identity_status!=='verified'?'unknown':monitors.some(m=>m.active&&m.coverage.subscription_acknowledged&&(m.coverage.covered_identities\|\|[]).some(e=>same(x,e)))?'monitored':'registered'})); | 
|  | +    const coverage={status:!identities.length?'unbound':b.unbound_panes.length\|\|identities.some(x=>x.monitoring!=='monitored')?'partial':'complete',identities,unbound_panes:b.unbound_panes,monitors}; | 
|  | +    return {...task,monitor_coverage:coverage}; | 
|  | +  } | 
|  | +  return { | 
|  | +    get, | 
|  | +    status, | 
|  | +    list:()=>{const dir=path.join(state,'tasks');return fs.existsSync(dir)?fs.readdirSync(dir).filter(n=>/^[a-f0-9]{64}\.json$/.test(n)).map(n=>read(path.join(dir,n))).filter(Boolean).map(t=>status(t.task_id)):[];}, | 
|  | +    registerObserver:(coordinator,input)=>locked(state,async()=>{ | 
|  | +      const c=input?.coordinator; | 
|  | +      if(input?.schema_version!==1\|\|!input.goal\|\|!id(input.task_id)\|\|!id(coordinator)\|\|c?.kind!=='external'\|\|c.provider!=='openclaw'\|\|c.coordinator_id!==coordinator\|\|!path.isAbsolute(c.cwd\|\|'')\|\|!fs.existsSync(c.cwd)\|\|c.session_id!==undefined\|\|c.pane_id!==undefined\|\|input.parent!==undefined\|\|input.continuation!==undefined)throw Error('Observation registration needs truthful external OpenClaw coordinator, no Codex parent or continuation.'); | 
|  | +      if(get(input.task_id))throw Error('Task already registered.'); | 
|  | +      const task={schema_version:1,mode:'observation_only',task_id:input.task_id,parent:{...c},status:'active',generation:1,remaining:[],next_action:null,continuation:{enabled:false},assignments:{},reports:{},observations:[],handoffs:[],probes:0,wakes:0,lifecycle:lifecycle(input.goal,input.task_id,{...c})}; | 
|  | +      recordEvent(task,'goal_created',coordinator,null);save(task);return task; | 
|  | +    }), | 
|  | +    observeAssign:(coordinator,taskId,input)=>locked(state,async()=>{const t=get(taskId);await authority(t,coordinator);if(t.mode!=='observation_only')throw Error('Observation assignment requires observation-only task.');return addAssignment(t,coordinator,input,{bind:false});}), | 
|  | +    bindObserver:(actor,taskId,input)=>locked(state,async()=>{ | 
|  | +      const task=get(taskId);await authority(task,actor); | 
|  | +      if(input?.kind!=='hook_monitor'&&!identity(input))throw Error('Observer needs exact real Codex monitor identity.'); | 
|  | +      let observer; | 
|  | +      if(input.kind==='hook_monitor'){ | 
|  | +        // The hook monitor can belong to a coordinator outside Herdr. Bind the | 
|  | +        // recorded runtime owner, not a fabricated pane or Codex worker tuple. | 
|  | +        if(!id(input.session_id)\|\|!path.isAbsolute(input.cwd\|\|'')\|\|input.pane_id!==undefined\|\|input.tab_id!==undefined)throw Error('Hook observer needs real session/cwd and no pane identity.'); | 
|  | +        const dir=path.join(state,'sessions',hash(input.session_id)),s=read(path.join(dir,'session.json')),m=read(path.join(dir,'monitor.json')); | 
|  | +        if(s?.session_id!==input.session_id\|\|s.cwd!==input.cwd\|\|m?.owner!==input.session_id\|\|m.cwd!==input.cwd\|\|!m.signature\|\|processSignature(m.pid)!==m.signature\|\|!path.isAbsolute(m.runtime\|\|'')\|\|m.lifecycle?.state!=='subscribed')throw Error('Hook observer has no live recorded session/monitor ownership evidence.'); | 
|  | +        observer={kind:'hook_monitor',session_id:input.session_id,cwd:input.cwd,bound_at:new Date(now()).toISOString(),binding_evidence:{monitor_pid:m.pid,monitor_signature:m.signature,runtime:m.runtime}}; | 
|  | +      }else { | 
|  | +        const o=await observe(task,input,null);if(!o.identity_verified\|\|!fresh(o.observed_at,now()))throw Error('Observer identity is not freshly verified.'); | 
|  | +        observer=exact(input); | 
|  | +      } | 
|  | +      const old=current(input.session_id);if(old&&old.task_id!==taskId\|\|old&&read(memberFile(input.session_id))?.role!=='observer')throw Error('Observer binding would replace task ownership.'); | 
|  | +      task.lifecycle.observers[input.session_id]=observer;recordEvent(task,'observer_bound',actor,null,{session_id:input.session_id});save(task);write(memberFile(input.session_id),{task_id:taskId,role:'observer',active:true});return observer; | 
|  | +    }), | 
|  | +    unbindObserver:(actor,taskId,input)=>locked(state,async()=>{ | 
|  | +      const task=get(taskId);await authority(task,actor); | 
|  | +      if(!task.lifecycle.observers[input?.session_id]\|\|!text(input.reason))throw Error('Unbinding needs registered observer session and reason.'); | 
|  | +      delete task.lifecycle.observers[input.session_id];recordEvent(task,'observer_unbound',actor,null,{session_id:input.session_id,reason:input.reason});save(task); | 
|  | +      const m=read(memberFile(input.session_id));if(m?.task_id===taskId&&m.role==='observer')write(memberFile(input.session_id),{...m,active:false,terminal_turn_id:null});return task; | 
|  | +    }), | 
|  | +    retainPane:(actor,taskId,input)=>locked(state,async()=>{ | 
|  | +      const task=get(taskId);await authority(task,actor); | 
|  | +      if(!id(input?.pane_id)\|\|!text(input.reason)\|\|input.session_id!==undefined\|\|input.cwd!==undefined&&!path.isAbsolute(input.cwd))throw Error('Unbound pane retention needs pane_id and reason, never an invented session.'); | 
|  | +      task.lifecycle.unbound_panes[input.pane_id]={pane_id:input.pane_id,cwd:input.cwd\|\|null,identity_status:'unbound',retention:{mode:'retain',reason:input.reason,next_work:input.next_work\|\|null},owner:actor,at:new Date(now()).toISOString()};save(task);return task.lifecycle.unbound_panes[input.pane_id]; | 
|  | +    }), | 
|  | +    resolvePane:(actor,taskId,input)=>locked(state,async()=>{ | 
|  | +      const task=get(taskId);await authority(task,actor);const retained=task.lifecycle.unbound_panes[input?.pane_id]; | 
|  | +      if(!retained\|\|!text(input.reason)\|\|!['registered','released'].includes(input.resolution))throw Error('Resolution needs retained pane, registered\|released and explicit reason.'); | 
|  | +      if(input.resolution==='registered'){ | 
|  | +        const a=task.assignments[input.assignment_id];if(!a\|\|!liveAssignments(task).includes(a)\|\|a.worker.pane_id!==input.pane_id)throw Error('Resolution needs an active exact assignment for this pane.'); | 
|  | +        const o=await observe(task,a.worker,a.assignment_id);if(!o.identity_verified\|\|!fresh(o.observed_at,now()))throw Error('Resolved pane identity is not freshly verified.'); | 
|  | +      } | 
|  | +      recordEvent(task,'retained_pane_resolved',actor,input.assignment_id\|\|null,{retained_pane:retained,resolution:input.resolution,reason:input.reason});delete task.lifecycle.unbound_panes[input.pane_id];save(task);return task; | 
|  | +    }), | 
|  | +    endObservation:(actor,taskId,input)=>locked(state,async()=>{ | 
|  | +      const task=get(taskId);await authority(task,actor); | 
|  | +      if(task.mode!=='observation_only'\|\|!text(input?.reason)\|\|liveAssignments(task).length\|\|Object.keys(task.lifecycle.unbound_panes).length)throw Error('Observation closure needs reason, no active assignments and no unresolved retained panes.'); | 
|  | +      task.status='closed';task.generation++;task.lifecycle.closed_at=new Date(now()).toISOString();recordEvent(task,'goal_closed',actor,null,{reason:input.reason});save(task);return task; | 
|  | +    }), | 
|  | +    preserve:(actor,taskId,input)=>locked(state,async()=>{ | 
|  | +      const task=get(taskId);await authority(task,actor);const a=assignment(task,input,{includeSuperseded:true}),r=input.retention; | 
|  | +      if(!r\|\|!['retain','release'].includes(r.mode)\|\|!text(r.reason)\|\|typeof r.next_work!=='string'\|\|r.next_work.length>4096)throw Error('Explicit retention needs mode, reason and concrete next_work (empty to release).'); | 
|  | +      const preserved=input.artifacts?{artifacts:artifacts(input.artifacts),verification_session_id:input.verification_session_id,receipts:verifyResult(task,a,input.verification_session_id),verified_at:new Date(now()).toISOString()}:a.lifecycle?.preserved_result\|\|null; | 
|  | +      a.lifecycle??=assignmentLifecycle(actor,a.worker);a.lifecycle.retention={...r};a.lifecycle.preserved_result=preserved;recordEvent(task,'result_or_retention_updated',actor,a.assignment_id);save(task);return a; | 
|  | +    }), | 
|  | +    retirement:(actor,taskId,input)=>retirement(actor,taskId,input), | 
|  | +    retire:(actor,taskId,input)=>retirement(actor,taskId,input,true), | 
|  | +    handoff:(actor,taskId,input)=>locked(state,async()=>{ | 
|  | +      const task=get(taskId);await authority(task,actor);const a=assignment(task,input); | 
|  | +      if(!text(input.reason)\|\|!input.successor\|\|same(a.worker,input.successor.worker))throw Error('Handoff needs reason and a distinct verified successor.'); | 
|  | +      // Verify successor before retiring routing. Old identity may be closed; | 
|  | +      // persisted expected identity still prevents a stale handoff replay. | 
|  | +      const successor=await addAssignment(task,actor,input.successor,{bind:task.mode!=='observation_only',persist:false,predecessor:a}); | 
|  | +      await invalidateAssignment(task,a,actor,'superseded');a.lifecycle.successor_assignment_id=successor.assignment_id;recordEvent(task,'assignment_handed_off',actor,a.assignment_id,{successor_assignment_id:successor.assignment_id,reason:input.reason});save(task);if(task.mode!=='observation_only')write(memberFile(successor.worker.session_id),{task_id:taskId,assignment_id:successor.assignment_id,active:true});return successor; | 
|  | +    }), | 
|  | +    monitorBinding:binding, | 
|  | +    guard:(session,prompt)=>{const match=typeof prompt==='string'&&prompt.match(/^\[HARNESS_CONTINUATION ([a-zA-Z0-9._:-]+) (\d+) ([a-f0-9]{64})\]/);if(!match)return false;const t=get(match[1]);return Boolean(t&&t.parent.session_id===session&&t.status==='active'&&t.generation===Number(match[2])&&Date.parse(t.continuation.expires_at)>now()&&owned(t)&&t.handoffs.some(h=>h.event_id===match[3]&&h.dispatch_status!=='retired'&&liveAssignments(t).some(a=>a.assignment_id===h.assignment_id)));}, | 
|  | +    register:(session,input)=>locked(state,async()=>{ | 
|  | +      if(input?.schema_version!==1\|\|!id(input.task_id)\|\|!identity(input.parent)\|\|input.parent.session_id!==session)throw Error('Task needs schema_version, task_id and exact parent identity.'); | 
|  | +      if(get(input.task_id))throw Error('Task already registered; identity and continuation owner are immutable.'); | 
|  | +      const old=current(session);if(old&&['active','paused'].includes(old.status))throw Error('Session already owns a task.'); | 
|  | +      const c=input.continuation; | 
|  | +      if(!c\|\|c.owner!=='harness_monitor'\|\|c.backend!=='codex_app_server'\|\|typeof c.enabled!=='boolean'\|\|!Number.isSafeInteger(c.max_wakes)\|\|c.max_wakes<0\|\|c.max_wakes>10\|\|!Number.isSafeInteger(c.max_probes)\|\|c.max_probes<1\|\|c.max_probes>1000\|\|!Number.isSafeInteger(c.backoff_ms)\|\|c.backoff_ms<1000\|\|c.backoff_ms>60000\|\|!Number.isFinite(Date.parse(c.expires_at))\|\|Date.parse(c.expires_at)<=now()\|\|Date.parse(c.expires_at)>now()+3600000)throw Error('Invalid bounded continuation owner/backend, budgets, backoff or expiry.'); | 
|  | +      if(!Array.isArray(input.remaining)\|\|input.remaining.some(x=>!text(x))\|\|!text(input.next_action))throw Error('Task needs remaining work and explicit next_action.'); | 
|  | +      const task={schema_version:1,task_id:input.task_id,parent:exact(input.parent),synthetic:input.synthetic===true,status:'active',generation:1,remaining:[...input.remaining],next_action:input.next_action,continuation:{...c},assignments:{},reports:{},observations:[],handoffs:[],probes:0,wakes:0}; | 
|  | +      task.lifecycle=lifecycle(input.goal,task.task_id,{kind:'codex',session_id:session});recordEvent(task,'goal_created',session,null); | 
|  | +      if(c.enabled&&!owned(task))throw Error('Exclusive continuation owner unavailable or conflicting external owner.'); | 
|  | +      await requireMember(task,session,true);save(task);write(memberFile(session),{task_id:task.task_id,assignment_id:null,active:true});return task; | 
|  | +    }), | 
|  | +    assign:(session,taskId,input)=>locked(state,async()=>{ | 
|  | +      const task=get(taskId);if(task?.mode==='observation_only')throw Error('Observation-only tasks require observe-assign and never adopt workers.');await authority(task,session); | 
|  | +      return addAssignment(task,session,input); | 
|  | +    }), | 
|  | +    report:(session,taskId,turn,input)=>locked(state,async()=>{ | 
|  | +      const task=get(taskId),m=await requireMember(task,session);if(!id(turn))throw Error('Report needs exact turn identity.'); | 
|  | +      const allowed=new Set(['claims','result','remaining','next_action']);if(!input\|\|Object.keys(input).some(k=>!allowed.has(k)))throw Error('Unsupported report field; observations are runtime owned.'); | 
|  | +      if(!Array.isArray(input.claims)\|\|input.claims.length>32)throw Error('Structured claims array required.'); | 
|  | +      // Probe claimed workers before stamping claims; measurements are never input. | 
|  | +      for(const claim of input.claims){if(!['running','completed','unverified'].includes(claim.kind)\|\|Object.keys(claim).some(k=>!['kind','assignment_id'].includes(k)))throw Error('Invalid claim.');if(claim.kind==='running'){const a=task.assignments[claim.assignment_id];if(!a\|\|['retired','superseded'].includes(a.lifecycle?.status)\|\|m.assignment_id&&m.assignment_id!==a.assignment_id)throw Error('Claim assignment identity mismatch.');await observe(task,a.worker,a.assignment_id);}} | 
|  | +      if(input.result!==undefined){if(m.assignment_id===null\|\|!['delivered','blocked','partial'].includes(input.result?.status)\|\|Object.keys(input.result).some(k=>k!=='status'))throw Error('Worker result supports status delivered\|blocked\|partial only.');task.assignments[m.assignment_id].result={status:input.result.status,source:'worker_report',at:new Date(now()).toISOString(),turn_id:turn};} | 
|  | +      if(input.remaining!==undefined\|\|input.next_action!==undefined){if(m.assignment_id!==null\|\|!Array.isArray(input.remaining)\|\|input.remaining.some(x=>!text(x))\|\|!text(input.next_action))throw Error('Only parent may set remaining and next_action together.');task.remaining=[...input.remaining];task.next_action=input.next_action;} | 
|  | +      const a=await acceptance(task);task.acceptance=a; | 
|  | +      const claims=input.claims.map(c=>({claim_id:crypto.randomUUID(),kind:c.kind,assignment_id:c.assignment_id\|\|null,at:new Date(now()).toISOString()}));task.reports[session]={turn_id:turn,claims,acceptance:a};save(task);return task.reports[session]; | 
|  | +    }), | 
|  | +    reconcile:taskId=>locked(state,async()=>{const task=get(taskId);if(task)await pump(task);return task;}), | 
|  | +    reconcileSession:(session,options)=>{if(!current(session))return Promise.resolve(null);return locked(state,async()=>{const task=current(session);if(task){if(read(memberFile(session))?.role==='observer'){for(const a of liveAssignments(task))await observe(task,a.worker,a.assignment_id,options);save(task);}else await pump(task,options);}return task;},{deadline:options?.deadline});}, | 
|  | +    interested:(session,pane)=>{const task=current(session);return Boolean(task&&(task.parent.pane_id===pane\|\|liveAssignments(task).some(a=>a.worker.pane_id===pane)));}, | 
|  | +    control:async(session,taskId,status)=>{ | 
|  | +      const registered=get(taskId);if(!registered\|\|registered.parent.session_id!==session)throw Error('Task parent authority mismatch.'); | 
|  | +      if(!['paused','cancelled','completed'].includes(status))throw Error('Control supports pause, cancel or verified completion; resume needs a new task identity.'); | 
|  | +      // Publish revocation without waiting on an add/start holding the state lock. | 
|  | +      if(status!=='completed'){revoke(registered,status);if(status==='cancelled')endBindings(registered);} | 
|  | +      return locked(state,async()=>{ | 
|  | +        const task=get(taskId); | 
|  | +        if(status==='completed'){ | 
|  | +          await requireMember(task,session,true);task.acceptance=await acceptance(task); | 
|  | +          if(task.status!=='active'\|\|remaining(task)\|\|task.acceptance.status!=='pass'\|\|!task.acceptance.evidence_ids.length)throw Error('Task completion requires actual verifier acceptance and no remaining work.'); | 
|  | +          task.status='completed';task.generation++; | 
|  | +        } | 
|  | +        save(task);await deletePending(task);save(task);return task; | 
|  | +      }); | 
|  | +    }, | 
|  | +    interrupt:async session=>{ | 
|  | +      const task=current(session); | 
|  | +      if(task&&task.parent.session_id===session&&task.status==='active')revoke(task,'paused'); | 
|  | +      // Dispatcher or next existing monitor tick deletes acknowledged queued work. | 
|  | +      return current(session); | 
|  | +    }, | 
|  | +    continuation:async(session,prompt,turn)=>{ | 
|  | +      const match=typeof prompt==='string'&&prompt.match(/^\[HARNESS_CONTINUATION ([a-zA-Z0-9._:-]+) (\d+) ([a-f0-9]{64})\]/);if(!match)return null; | 
|  | +      const deadline=Date.now()+1500; | 
|  | +      return locked(state,async()=>{ | 
|  | +        const task=get(match[1]);const h=task?.handoffs.find(x=>x.event_id===match[3]);const valid=id(turn)&&task?.parent.session_id===session&&task.status==='active'&&task.generation===Number(match[2])&&Date.parse(task.continuation.expires_at)>now()&&owned(task)&&h&&liveAssignments(task).some(a=>a.assignment_id===h.assignment_id)&&['sent','uncertain'].includes(h.dispatch_status); | 
|  | +        if(!valid)return {authorized:false,reason:'Continuation revoked, replayed, expired, replaced or unowned; perform no task action.'}; | 
|  | +        const o=await observe(task,task.parent,null,{deadline});if(!o.identity_verified\|\|!fresh(o.observed_at,now()))return {authorized:false,reason:'Continuation parent identity unverified; perform no task action.'}; | 
|  | +        h.parent_continued_at=o.observed_at;h.dispatch_status='acknowledged';h.continued_turn_id=turn;save(task);return {authorized:task.status==='active'&&!revoked(task)}; | 
|  | +      },{deadline}); | 
|  | +    }, | 
|  | +    stop:(session,turn,{deadline=Date.now()+3000}={})=>{if(!forTurn(session,turn))return Promise.resolve({issues:[],snapshot:null});return locked(state,async()=>{ | 
|  | +      const task=forTurn(session,turn);if(!task\|\|!id(turn))return {issues:[],snapshot:null}; | 
|  | +      const m=member(task,session);if(!m)return {issues:[],snapshot:null}; | 
|  | +      // Stop probes and validates, but never queues: parent is still finishing a turn. | 
|  | +      const terminal=['completed','cancelled'].includes(task.status); | 
|  | +      if(!terminal){task.parent_observation=await observe(task,task.parent,null,{deadline});for(const a of liveAssignments(task))await observe(task,a.worker,a.assignment_id,{deadline});} | 
|  | +      const report=task.reports[session]?.turn_id===turn?task.reports[session]:null;const claims=report?.claims\|\|[];const issues=[]; | 
|  | +      for(const c of claims){ | 
|  | +        if(c.kind==='running'){ | 
|  | +          const latest=task.observations.filter(o=>o.assignment_id===c.assignment_id&&Date.parse(o.observed_at)<=Date.parse(c.at)).at(-1); | 
|  | +          const current=task.observations.filter(o=>o.assignment_id===c.assignment_id).at(-1); | 
|  | +          if(task.status!=='active'\|\|!latest?.identity_verified\|\|latest.state!=='working'\|\|!fresh(latest.observed_at,Date.parse(c.at))\|\|!fresh(c.at,now())\|\|!current?.identity_verified\|\|current.state!=='working'\|\|!fresh(current.observed_at,now()))issues.push(`running claim ${c.claim_id} lacks fresh owned working evidence`); | 
|  | +        } | 
|  | +        if(c.kind==='completed'&&(task.status!=='completed'\|\|m.assignment_id!==null\|\|remaining(task)\|\|report.acceptance.status!=='pass'\|\|!report.acceptance.evidence_ids.length\|\|!fresh(report.acceptance.observed_at,Date.parse(c.at))))issues.push(`completed claim ${c.claim_id} lacks actual verifier acceptance`); | 
|  | +      } | 
|  | +      const a=await acceptance(task,{deadline});task.acceptance=a; | 
|  | +      if(claims.some(c=>c.kind==='completed')&&(a.status!=='pass'\|\|!a.evidence_ids.length))issues.push('completed claim acceptance is no longer valid'); | 
|  | +      if(!report&&!terminal)issues.push('Structured task report missing for exact session and turn; claims are unverified.'); | 
|  | +      const snapshot={schema_version:1,task_id:task.task_id,session_id:session,turn_id:turn,parent_session_id:task.parent.session_id,run_id:session,assignment_id:m.assignment_id,synthetic:task.synthetic===true,captured_at:new Date(now()).toISOString(),task_status:task.status,remaining_count:remaining(task),next_action_present:text(task.next_action),claims,observations:task.observations.map(({cwd,tab_id,...o})=>o),claim_acceptance:report?.acceptance\|\|null,acceptance:a,handoffs:task.handoffs.map(h=>({event_id:h.event_id,worker_finished_at:h.worker_finished_at,parent_continued_at:h.parent_continued_at,dispatch_status:h.dispatch_status,attempts:h.attempts})),parent_observation:task.parent_observation?{state:task.parent_observation.state,observed_at:task.parent_observation.observed_at,source:'herdr_live',identity_verified:task.parent_observation.identity_verified}:null}; | 
|  | +      save(task);write(path.join(state,'sessions',hash(session),'loops',hash(turn)+'.json'),snapshot);return {issues,snapshot}; | 
|  | +    },{deadline});} | 
|  | +  }; | 
|  | +} | 
|  | diff --git a/scripts/verification.mjs b/scripts/verification.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..b574809 | 
|  | --- /dev/null | 
|  | +++ b/scripts/verification.mjs | 
|  | @@ -0,0 +1,301 @@ | 
|  | +import fs from 'node:fs'; | 
|  | +import path from 'node:path'; | 
|  | +import crypto from 'node:crypto'; | 
|  | +import {spawn, spawnSync} from 'node:child_process'; | 
|  | +import {isDeepStrictEqual} from 'node:util'; | 
|  | + | 
|  | +const hash = value => crypto.createHash('sha256').update(value).digest('hex'); | 
|  | +const read = file => {try {return JSON.parse(fs.readFileSync(file, 'utf8'));} catch {return null;}}; | 
|  | +const canonical = value => JSON.stringify(value, function (_key, item) { | 
|  | +  return item && typeof item === 'object' && !Array.isArray(item) | 
|  | +    ? Object.fromEntries(Object.keys(item).sort().map(key => [key, item[key]])) : item; | 
|  | +}); | 
|  | +const nonempty = value => typeof value === 'string' && value.trim().length > 0; | 
|  | +const identifier = value => typeof value === 'string' && /^[a-zA-Z0-9][a-zA-Z0-9._-]{0,99}$/.test(value); | 
|  | +const statuses = ['in_progress', 'complete', 'partial', 'blocked', 'interrupted']; | 
|  | +const MAX_OUTPUT = 8 * 1024 * 1024; | 
|  | + | 
|  | +function write(file, value) { | 
|  | +  fs.mkdirSync(path.dirname(file), {recursive: true, mode: 0o700}); | 
|  | +  const tmp = `${file}.${crypto.randomUUID()}.tmp`; | 
|  | +  fs.writeFileSync(tmp, JSON.stringify(value, null, 2) + '\n', {mode: 0o600}); | 
|  | +  fs.renameSync(tmp, file); | 
|  | +} | 
|  | +function relativeFile(value) { | 
|  | +  return nonempty(value) && !path.isAbsolute(value) && !value.split(/[\\/]/).includes('..'); | 
|  | +} | 
|  | + | 
|  | +export function validateVerificationContract(c) { | 
|  | +  if (c?.schema_version !== 2 \|\| !identifier(c.task_id) \|\| !nonempty(c.objective) \|\| !statuses.includes(c.status)) | 
|  | +    throw Error('Verification contract needs schema_version:2, task_id, objective and a valid status.'); | 
|  | +  if (!Array.isArray(c.requirements) \|\| !c.requirements.length \|\| !Array.isArray(c.checks)) | 
|  | +    throw Error('Verification contract needs requirements and checks.'); | 
|  | +  if (c.remaining !== undefined && (!Array.isArray(c.remaining) \|\| c.remaining.some(x => !nonempty(x)))) | 
|  | +    throw Error('remaining must contain descriptions of unfinished work.'); | 
|  | +  const ids = new Set(); | 
|  | +  for (const check of c.checks) { | 
|  | +    if (!identifier(check.id) \|\| ids.has(check.id)) throw Error('Checks need unique IDs.'); | 
|  | +    ids.add(check.id); | 
|  | +    if (!Array.isArray(check.command) \|\| !check.command.length \|\| check.command.some(x => typeof x !== 'string' \|\| x.includes('\0')) \|\| !nonempty(check.command[0])) | 
|  | +      throw Error(`${check.id}: command must be an argument array.`); | 
|  | +    if (!path.isAbsolute(check.cwd \|\| '') \|\| !fs.statSync(check.cwd).isDirectory()) | 
|  | +      throw Error(`${check.id}: cwd must be an existing absolute directory.`); | 
|  | +    if (!['exit', 'json'].includes(check.format)) throw Error(`${check.id}: format must be exit or json.`); | 
|  | +    for (const key of ['inputs', 'artifacts']) { | 
|  | +      if (check[key] !== undefined && (!Array.isArray(check[key]) \|\| check[key].some(x => !relativeFile(x)))) | 
|  | +        throw Error(`${check.id}: ${key} must be relative paths inside cwd.`); | 
|  | +    } | 
|  | +    if (check.inputs && !check.inputs.length) throw Error(`${check.id}: inputs cannot be empty.`); | 
|  | +    for (const key of ['timeout_ms', 'max_age_seconds']) { | 
|  | +      if (check[key] !== undefined && (!Number.isSafeInteger(check[key]) \|\| check[key] <= 0)) | 
|  | +        throw Error(`${check.id}: ${key} must be a positive integer.`); | 
|  | +    } | 
|  | +    if (check.format === 'json') { | 
|  | +      if (!Array.isArray(check.assertions) \|\| !check.assertions.length) throw Error(`${check.id}: JSON observations need assertions.`); | 
|  | +      for (const assertion of check.assertions) { | 
|  | +        if (typeof assertion.pointer !== 'string' \|\| (assertion.pointer && !assertion.pointer.startsWith('/')) \|\| /~(?![01])/.test(assertion.pointer)) | 
|  | +          throw Error(`${check.id}: assertions use JSON pointers.`); | 
|  | +        if (!['eq', 'gte', 'lte'].includes(assertion.op) \|\| !Object.hasOwn(assertion, 'value')) | 
|  | +          throw Error(`${check.id}: assertion needs op eq/gte/lte and value.`); | 
|  | +        if (assertion.op !== 'eq' && !Number.isFinite(assertion.value)) throw Error(`${check.id}: numeric assertions need a finite value.`); | 
|  | +      } | 
|  | +    } else if (check.assertions !== undefined) throw Error(`${check.id}: assertions require format:json.`); | 
|  | +  } | 
|  | +  const requirementIds = new Set(); | 
|  | +  for (const requirement of c.requirements) { | 
|  | +    if (!identifier(requirement.id) \|\| requirementIds.has(requirement.id) \|\| !nonempty(requirement.text) \|\| !nonempty(requirement.source)) | 
|  | +      throw Error('Requirements need unique IDs, text and a source reference.'); | 
|  | +    requirementIds.add(requirement.id); | 
|  | +    if (requirement.status === 'superseded') { | 
|  | +      if (!nonempty(requirement.decision_basis)) throw Error(`${requirement.id}: superseded requires decision_basis.`); | 
|  | +      continue; | 
|  | +    } | 
|  | +    if (requirement.status !== undefined) throw Error(`${requirement.id}: progress is computed; only superseded is a declared requirement status.`); | 
|  | +    if (!Array.isArray(requirement.checks) \|\| !requirement.checks.length \|\| new Set(requirement.checks).size !== requirement.checks.length \|\| requirement.checks.some(id => !ids.has(id))) | 
|  | +      throw Error(`${requirement.id}: link at least one declared check.`); | 
|  | +  } | 
|  | +  return c; | 
|  | +} | 
|  | + | 
|  | +export function validateRevision(previous, next) { | 
|  | +  if (previous?.schema_version !== 2 \|\| previous.task_id !== next.task_id) return; | 
|  | +  const retained = new Map(next.requirements.map(r => [r.id, r])); | 
|  | +  for (const requirement of previous.requirements) { | 
|  | +    if (!retained.has(requirement.id)) throw Error(`Keep requirement ${requirement.id}; mark it superseded with its decision_basis instead of deleting it.`); | 
|  | +  } | 
|  | +  const definition = c => ({objective: c.objective, requirements: c.requirements, checks: c.checks}); | 
|  | +  if (canonical(definition(previous)) !== canonical(definition(next)) && !nonempty(next.change_basis)) | 
|  | +    throw Error('Changed scope or checks require change_basis referencing the decision or correction.'); | 
|  | +} | 
|  | + | 
|  | +function definitionHash(c, check) { | 
|  | +  return hash(canonical({task_id: c.task_id, objective: c.objective, check, | 
|  | +    requirements: c.requirements.filter(r => r.status !== 'superseded' && r.checks.includes(check.id))})); | 
|  | +} | 
|  | +function checkDirectory(dir, c, check) {return path.join(dir, 'verification', hash(c.task_id), hash(check.id));} | 
|  | +function pointerValue(value, pointer) { | 
|  | +  for (const part of pointer ? pointer.slice(1).split('/').map(x => x.replace(/~1/g, '/').replace(/~0/g, '~')) : []) { | 
|  | +    if (value === null \|\| typeof value !== 'object' \|\| !Object.hasOwn(value, part)) return {found: false}; | 
|  | +    value = value[part]; | 
|  | +  } | 
|  | +  return {found: true, value}; | 
|  | +} | 
|  | +export function inspectOutput(check, output) { | 
|  | +  if (check.format === 'exit') return []; | 
|  | +  let data; | 
|  | +  try {data = JSON.parse(output);} catch {return ['Command stdout is not a complete JSON observation.'];} | 
|  | +  return check.assertions.flatMap(assertion => { | 
|  | +    const observed = pointerValue(data, assertion.pointer); | 
|  | +    const pass = observed.found && (assertion.op === 'eq' ? isDeepStrictEqual(observed.value, assertion.value) | 
|  | +      : Number.isFinite(observed.value) && (assertion.op === 'gte' ? observed.value >= assertion.value : observed.value <= assertion.value)); | 
|  | +    return pass ? [] : [`Observation ${assertion.pointer \|\| '/'} is ${observed.found ? JSON.stringify(observed.value).slice(0,160) : 'missing'}; expected ${assertion.op} ${JSON.stringify(assertion.value)}.`]; | 
|  | +  }); | 
|  | +} | 
|  | + | 
|  | +// Hash actual inputs, not just HEAD: committing identical bytes keeps evidence valid, | 
|  | +// while dirty/untracked changes invalidate it. No file contents are returned to hooks. | 
|  | +export function sourceFingerprint(check, {deadline = Infinity} = {}) { | 
|  | +  const cwd = fs.realpathSync(check.cwd); | 
|  | +  const selected = (check.inputs \|\| ['.']).map(x => path.normalize(x)); | 
|  | +  const artifacts = new Set((check.artifacts \|\| []).map(x => path.normalize(x))); | 
|  | +  const git = spawnSync('git', ['-C', cwd, 'ls-files', '--cached', '--others', '--exclude-standard', '-z'], | 
|  | +    {encoding: 'utf8', timeout: Math.max(1, Math.min(2000, deadline - Date.now())), maxBuffer: MAX_OUTPUT}); | 
|  | +  let files; | 
|  | +  const walk = (relative, into) => { | 
|  | +    if (Date.now() > deadline \|\| into.length > 20000) throw Error('Input snapshot limit exceeded; select the relevant input scope.'); | 
|  | +    const absolute = path.join(cwd, relative), stat = fs.lstatSync(absolute); | 
|  | +    if (stat.isDirectory()) for (const name of fs.readdirSync(absolute).sort()) walk(path.join(relative, name), into); | 
|  | +    else into.push(relative); | 
|  | +  }; | 
|  | +  if (git.status === 0) { | 
|  | +    files = git.stdout.split('\0').filter(Boolean).filter(file => selected.some(input => input === '.' \|\| file === input \|\| file.startsWith(input + path.sep))); | 
|  | +    // Explicit ignored files/directories still belong to the declared input scope. | 
|  | +    for (const input of selected) if (input !== '.' && fs.existsSync(path.join(cwd, input))) walk(input, files); | 
|  | +    for (const input of selected) if (input !== '.' && !fs.existsSync(path.join(cwd, input)) && !files.some(file => file === input \|\| file.startsWith(input + path.sep))) | 
|  | +      throw Error(`Declared input does not exist: ${input}`); | 
|  | +  } else { | 
|  | +    if (!check.inputs \|\| selected.includes('.')) throw Error('Outside Git, declare explicit input files or directories.'); | 
|  | +    files = []; | 
|  | +    for (const input of selected) walk(input, files); | 
|  | +  } | 
|  | +  files = [...new Set(files)].filter(file => !artifacts.has(path.normalize(file))).sort(); | 
|  | +  if (!files.length \|\| files.length > 20000) throw Error('No input files, or input snapshot limit exceeded; declare the relevant input scope.'); | 
|  | +  const digest = crypto.createHash('sha256'); | 
|  | +  digest.update(canonical({cwd, inputs: selected})); | 
|  | +  let bytes = 0, observedFiles = 0; | 
|  | +  for (const file of files) { | 
|  | +    if (Date.now() > deadline) throw Error('Input snapshot timed out; evidence remains unverified.'); | 
|  | +    const absolute = path.join(cwd, file); | 
|  | +    let stat; | 
|  | +    try {stat = fs.lstatSync(absolute);} catch (error) {if (error.code === 'ENOENT') continue; throw error;} | 
|  | +    digest.update(file).update('\0'); observedFiles++; | 
|  | +    if (stat.isSymbolicLink()) { | 
|  | +      // Include link target and target bytes so external target changes are observable. | 
|  | +      digest.update(fs.readlinkSync(absolute)).update('\0'); | 
|  | +      stat = fs.statSync(absolute); | 
|  | +    } | 
|  | +    if (!stat.isFile()) throw Error(`Unsupported input file: ${file}`); | 
|  | +    bytes += stat.size; | 
|  | +    if (bytes > 128 * 1024 * 1024) throw Error('Input snapshot exceeds 128 MiB; select the relevant input scope.'); | 
|  | +    digest.update(String(stat.mode & 0o111)).update('\0').update(fs.readFileSync(absolute)).update('\0'); | 
|  | +  } | 
|  | +  return {sha256: digest.digest('hex'), files: observedFiles, bytes, cwd}; | 
|  | +} | 
|  | + | 
|  | +function artifactStamp(file) { | 
|  | +  try {const s=fs.statSync(file);return {ino:s.ino,size:s.size,mtimeMs:s.mtimeMs,ctimeMs:s.ctimeMs};} | 
|  | +  catch(error){if(error.code==='ENOENT')return null;throw error;} | 
|  | +} | 
|  | +function artifactRecords(check, {previous=[], deadline=Infinity}={}) { | 
|  | +  return (check.artifacts \|\| []).map(file => { | 
|  | +    const absolute=path.resolve(check.cwd,file),stamp=artifactStamp(absolute); | 
|  | +    if(!stamp)throw Error(`Output artifact does not exist: ${file}`); | 
|  | +    const old=previous.find(item=>item.path===absolute); | 
|  | +    if(old&&isDeepStrictEqual(old.stamp,stamp))return old; | 
|  | +    const fd=fs.openSync(absolute,'r'),digest=crypto.createHash('sha256'),buffer=Buffer.alloc(1024*1024); | 
|  | +    try {let count;while((count=fs.readSync(fd,buffer,0,buffer.length,null))>0){ | 
|  | +      if(Date.now()>deadline)throw Error('Artifact inspection timed out; evidence remains unverified.'); | 
|  | +      digest.update(buffer.subarray(0,count)); | 
|  | +    }}finally{fs.closeSync(fd);} | 
|  | +    if(!isDeepStrictEqual(stamp,artifactStamp(absolute)))throw Error(`Artifact changed while reading: ${file}`); | 
|  | +    return {path:absolute,sha256:digest.digest('hex'),stamp}; | 
|  | +  }); | 
|  | +} | 
|  | +function inspectReceipt(c, check, dir, {deadline = Infinity, snapshots = new Map()} = {}) { | 
|  | +  const directory = checkDirectory(dir, c, check), receipt = read(path.join(directory, 'latest.json')); | 
|  | +  const result = {id: check.id, status: 'unverified', issues: [], receipt: null}; | 
|  | +  if (!receipt) {result.issues.push('No observed execution.'); return result;} | 
|  | +  result.receipt = path.join(directory, 'latest.json'); | 
|  | +  if (receipt.definition !== definitionHash(c, check)) {result.issues.push('Check or requirement definition changed.'); return result;} | 
|  | +  if (receipt.status === 'running') {result.issues.push('Latest execution is running or was interrupted.'); return result;} | 
|  | +  if (!receipt.finished_at \|\| !Number.isFinite(Date.parse(receipt.finished_at)) \|\| Date.parse(receipt.finished_at) > Date.now() + 1000) {result.issues.push('Invalid observation timestamp.'); return result;} | 
|  | +  if (check.max_age_seconds && Date.now() - Date.parse(receipt.finished_at) > check.max_age_seconds * 1000) {result.issues.push('Observation expired.'); return result;} | 
|  | +  try { | 
|  | +    const key = canonical({cwd: check.cwd, inputs: check.inputs, artifacts: check.artifacts}); | 
|  | +    if (!snapshots.has(key)) snapshots.set(key, sourceFingerprint(check, {deadline})); | 
|  | +    if (receipt.source_before?.sha256 !== receipt.source_after?.sha256 \|\| receipt.source_after?.sha256 !== snapshots.get(key).sha256) | 
|  | +      result.issues.push('Inputs changed during or after the check.'); | 
|  | +    for (const stream of ['stdout', 'stderr']) { | 
|  | +      const file = path.join(directory, receipt.run_id, stream + '.log'); | 
|  | +      if (hash(fs.readFileSync(file)) !== receipt[stream + '_sha256']) result.issues.push(`${stream} evidence changed.`); | 
|  | +    } | 
|  | +    const identities=items=>items.map(({path,sha256})=>({path,sha256})); | 
|  | +    if (!isDeepStrictEqual(identities(artifactRecords(check,{previous:receipt.artifacts,deadline})), identities(receipt.artifacts))) result.issues.push('Output artifact changed.'); | 
|  | +    if (result.issues.length) return result; | 
|  | +    if (receipt.exit_code !== 0 \|\| receipt.signal \|\| receipt.error \|\| receipt.timed_out \|\| receipt.output_overflow) { | 
|  | +      result.status = 'failed'; | 
|  | +      result.issues.push(receipt.error \|\| (receipt.timed_out ? 'Latest command timed out.' : receipt.output_overflow ? 'Latest command exceeded its output limit.' : `Latest command exited ${receipt.exit_code} (${receipt.signal \|\| 'no signal'}).`)); | 
|  | +      return result; | 
|  | +    } | 
|  | +    result.issues.push(...inspectOutput(check, fs.readFileSync(path.join(directory, receipt.run_id, 'stdout.log'), 'utf8'))); | 
|  | +    result.status = result.issues.length ? 'failed' : 'pass'; | 
|  | +  } catch (error) {result.issues.push(`Evidence unavailable: ${error.message}`);} | 
|  | +  return result; | 
|  | +} | 
|  | + | 
|  | +export function assessVerification(c, dir, {deadline = Infinity} = {}) { | 
|  | +  validateVerificationContract(c); | 
|  | +  const active = c.requirements.filter(r => r.status !== 'superseded'); | 
|  | +  const required = new Set(active.flatMap(r => r.checks)); | 
|  | +  const options = {deadline, snapshots: new Map()}; | 
|  | +  const checks = c.checks.filter(check => required.has(check.id)).map(check => inspectReceipt(c, check, dir, options)); | 
|  | +  const results = new Map(checks.map(check => [check.id, check])); | 
|  | +  const requirements = c.requirements.map(r => ({id: r.id, status: r.status === 'superseded' ? 'superseded' | 
|  | +    : r.checks.every(id => results.get(id).status === 'pass') ? 'pass' : r.checks.some(id => results.get(id).status === 'failed') ? 'failed' : 'unverified'})); | 
|  | +  const issues = [...(c.remaining \|\| []).map(x => `Remaining: ${x}`), ...checks.flatMap(check => check.issues.map(issue => `${check.id}: ${issue}`))]; | 
|  | +  return {task_id: c.task_id, status: checks.some(check => check.status === 'failed') ? 'failed' : issues.length ? 'unverified' : 'pass', assessment_source: 'observed_commands_and_declared_assertions', | 
|  | +    requirements, checks, issues, next_checks: checks.filter(check => check.status !== 'pass').map(check => check.id)}; | 
|  | +} | 
|  | + | 
|  | +function ownerSignature(pid) { | 
|  | +  const result = spawnSync('ps', ['-p', String(pid), '-o', 'lstart=', '-o', 'command='], {encoding: 'utf8', timeout: 1000}); | 
|  | +  return result.status === 0 && result.stdout.trim() ? hash(result.stdout.trim()) : null; | 
|  | +} | 
|  | + | 
|  | +export async function runVerification(c, dir, checkId, {force = false} = {}) { | 
|  | +  validateVerificationContract(c); | 
|  | +  const check = c.checks.find(item => item.id === checkId); | 
|  | +  if (!check \|\| !c.requirements.some(r => r.status !== 'superseded' && r.checks.includes(checkId))) throw Error('Select a check linked to an active requirement.'); | 
|  | +  const directory = checkDirectory(dir, c, check), lock = path.join(directory, 'lock.json'); | 
|  | +  fs.mkdirSync(directory, {recursive: true, mode: 0o700}); | 
|  | +  const owner = read(lock); | 
|  | +  if (fs.existsSync(lock)) { | 
|  | +    if (owner?.signature && ownerSignature(owner.pid) === owner.signature) throw Error('This check is already running.'); | 
|  | +    if (!owner && Date.now() - fs.statSync(lock).mtimeMs < 5000) throw Error('This check is being registered.'); | 
|  | +    fs.unlinkSync(lock); | 
|  | +  } | 
|  | +  const nonce = crypto.randomUUID(); | 
|  | +  fs.writeFileSync(lock, JSON.stringify({pid: process.pid, signature: ownerSignature(process.pid), nonce}), {flag: 'wx', mode: 0o600}); | 
|  | +  try { | 
|  | +    const existing = inspectReceipt(c, check, dir); | 
|  | +    if (!force && existing.status === 'pass') return {...existing, reused: true}; | 
|  | +    const run_id = crypto.randomUUID(), runDir = path.join(directory, run_id); | 
|  | +    fs.mkdirSync(runDir, {mode: 0o700}); | 
|  | +    const receipt = {schema_version: 1, task_id: c.task_id, check_id: check.id, run_id, definition: definitionHash(c, check), | 
|  | +      status: 'running', started_at: new Date().toISOString(), command: check.command, cwd: check.cwd}; | 
|  | +    // Retire the old pass before attempting a new observation, including snapshot failures. | 
|  | +    write(path.join(directory, 'latest.json'), receipt); | 
|  | +    try {receipt.source_before = sourceFingerprint(check);} catch (error) { | 
|  | +      Object.assign(receipt, {status: 'unverified', error: error.message, finished_at: new Date().toISOString()}); | 
|  | +      write(path.join(directory, 'latest.json'), receipt); | 
|  | +      return {id: check.id, status: 'unverified', reused: false, issues: [error.message], receipt: path.join(directory, 'latest.json')}; | 
|  | +    } | 
|  | +    const artifactBefore=(check.artifacts\|\|[]).map(file=>artifactStamp(path.resolve(check.cwd,file))); | 
|  | +    const files = Object.fromEntries(['stdout', 'stderr'].map(name => [name, path.join(runDir, name + '.log')])); | 
|  | +    const fds = Object.fromEntries(Object.entries(files).map(([name, file]) => [name, fs.openSync(file, 'w', 0o600)])); | 
|  | +    const sizes = {stdout: 0, stderr: 0}; | 
|  | +    try { | 
|  | +      Object.assign(receipt, await new Promise(resolve => { | 
|  | +        const child = spawn(check.command[0], check.command.slice(1), {cwd: check.cwd, stdio: ['ignore', 'pipe', 'pipe'], detached: process.platform !== 'win32'}); | 
|  | +        let error = null, timed_out = false, output_overflow = false, escalation; | 
|  | +        const signal = name => {try {if (process.platform !== 'win32' && child.pid) process.kill(-child.pid, name); else child.kill(name);} catch {}}; | 
|  | +        const stop = () => {signal('SIGTERM'); escalation \|\|= setTimeout(() => signal('SIGKILL'), 1000);}; | 
|  | +        const interrupt = () => {error = 'Verification interrupted'; stop();}; | 
|  | +        process.on('SIGINT', interrupt); process.on('SIGTERM', interrupt); | 
|  | +        const timer = setTimeout(() => {timed_out = true; stop();}, check.timeout_ms \|\| 300000); | 
|  | +        child.on('error', value => {error = value.message;}); | 
|  | +        for (const stream of ['stdout', 'stderr']) child[stream].on('data', chunk => { | 
|  | +          sizes[stream] += chunk.length; | 
|  | +          if (sizes[stream] <= MAX_OUTPUT) fs.writeSync(fds[stream], chunk); | 
|  | +          else {output_overflow = true; stop();} | 
|  | +        }); | 
|  | +        child.on('close', (exit_code, exitSignal) => { | 
|  | +          clearTimeout(timer); clearTimeout(escalation); process.off('SIGINT', interrupt); process.off('SIGTERM', interrupt); | 
|  | +          resolve({exit_code, signal: exitSignal, error, timed_out, output_overflow}); | 
|  | +        }); | 
|  | +      })); | 
|  | +    } finally {for (const fd of Object.values(fds)) fs.closeSync(fd);} | 
|  | +    receipt.finished_at = new Date().toISOString(); | 
|  | +    for (const [stream, file] of Object.entries(files)) receipt[stream + '_sha256'] = hash(fs.readFileSync(file)); | 
|  | +    try { | 
|  | +      receipt.source_after = sourceFingerprint(check); receipt.artifacts = artifactRecords(check); | 
|  | +      if(receipt.artifacts.some((artifact,index)=>isDeepStrictEqual(artifact.stamp,artifactBefore[index]))) | 
|  | +        receipt.error=[receipt.error,'Declared output artifact was not refreshed by this execution.'].filter(Boolean).join('; '); | 
|  | +    } | 
|  | +    catch (error) {receipt.error = [receipt.error, error.message].filter(Boolean).join('; ');} | 
|  | +    receipt.status = 'observed'; | 
|  | +    write(path.join(runDir, 'receipt.json'), receipt); | 
|  | +    write(path.join(directory, 'latest.json'), receipt); | 
|  | +    return {...inspectReceipt(c, check, dir), reused: false}; | 
|  | +  } finally {if (read(lock)?.nonce === nonce) fs.unlinkSync(lock);} | 
|  | +} | 
|  | diff --git a/test/continuation-watch-trigger.test.mjs b/test/continuation-watch-trigger.test.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..26710d4 | 
|  | --- /dev/null | 
|  | +++ b/test/continuation-watch-trigger.test.mjs | 
|  | @@ -0,0 +1,63 @@ | 
|  | +import test from 'node:test'; | 
|  | +import assert from 'node:assert/strict'; | 
|  | +import fs from 'node:fs'; | 
|  | +import os from 'node:os'; | 
|  | +import path from 'node:path'; | 
|  | +import {spawnSync} from 'node:child_process'; | 
|  | +const module=await import('../scripts/continuation-watch-trigger.mjs').catch(()=>({})); | 
|  | +const now=Date.parse('2026-10-11T15:00:00Z'); | 
|  | +const config={schema_version:1,state:'armed',task_id:'studio-task',workspace_id:'w1',goal_tab:'w1:t1H',project_root:'/projects/studio',parent:{session_id:'parent',pane_id:'w1:p60',cwd:'/worktrees/editor'},expires_at:new Date(now+600000).toISOString(),state_root:'/runtime',claim_file:'/private/claim.json',registration_script:'/private/register.mjs',canary_result:'/private/canary.json'}; | 
|  | +const snapshot={tabs:[{tab_id:'w1:t1H',workspace_id:'w1',label:'Studio'}],agents:[{workspace_id:'w1',tab_id:'w1:t1H',pane_id:'w1:p60',agent:'codex',name:'issues-editor',agent_session:{kind:'id',value:'parent'},cwd:'/worktrees/editor',foreground_cwd:'/worktrees/editor',agent_status:'idle'},{workspace_id:'w1',tab_id:'w1:t1H',pane_id:'w1:p71',agent:'codex',name:'issues-signup',agent_status:'working'}]}; | 
|  | +const evaluate=(options={})=>module.evaluateEnrollment({config,snapshot,now,observed_at:new Date(now).toISOString(),guards:{bound:false,claimed:false,canary_passed:true,paths_present:true},...options}); | 
|  | +test('named coordinator idle with worker working emits only exact enrollment despite original no-fire',()=>{ | 
|  | +  const result=evaluate();assert.equal(result.reason,'ready');assert.equal(result.event.parent.session_id,'parent');assert.equal(result.event.goal_tab,'w1:t1H');assert.equal(result.event.type,'harness-enrollment-ready'); | 
|  | +  assert.equal(JSON.stringify(result.event).includes('issues-signup'),false);assert.equal(JSON.stringify(result.event).includes('agent_status'),false); | 
|  | +  const original={fire:false,state:{tabs:{'w1:t1H':{active:true,since:null}},failure:null}};const composed=module.composeWatchResult(original,result.event);assert.equal(composed.fire,true);assert.deepEqual(composed.state,original.state);assert.deepEqual(JSON.parse(composed.message).enrollment,result.event); | 
|  | +}); | 
|  | +for(const status of ['working','blocked','unknown'])test(`parent ${status} fails closed independently of worker`,()=>{const s=structuredClone(snapshot);s.agents[0].agent_status=status;assert.equal(evaluate({snapshot:s}).event,null);}); | 
|  | +test('replaced, wrong cwd/tab/workspace, ambiguous and stale/future identities fail closed',()=>{ | 
|  | +  for(const change of [a=>a.agent_session.value='replacement',a=>a.cwd='/other',a=>a.foreground_cwd='/other',a=>a.tab_id='other',a=>a.workspace_id='other',a=>a.agent='shell']){const s=structuredClone(snapshot);change(s.agents[0]);assert.equal(evaluate({snapshot:s}).event,null);} | 
|  | +  const s=structuredClone(snapshot);s.agents.push({...s.agents[0]});assert.equal(evaluate({snapshot:s}).event,null); | 
|  | +  for(const bad of [null,{tabs:{},agents:[]},{tabs:[],agents:{}},{tabs:[null],agents:[null]}])assert.equal(evaluate({snapshot:bad}).event,null); | 
|  | +  for(const delta of [-30001,1])assert.equal(evaluate({observed_at:new Date(now+delta).toISOString()}).event,null); | 
|  | +}); | 
|  | +test('explicit expiry, cancelled/enrolled state, canary/paths and malformed mapping fail closed',()=>{ | 
|  | +  for(const c of [{...config,state:'cancelled'},{...config,state:'enrolled'},{...config,expires_at:new Date(now).toISOString()},{...config,expires_at:new Date(now+7200000).toISOString()},{...config,parent:{...config.parent,pane_id:''}}])assert.equal(evaluate({config:c}).event,null); | 
|  | +  for(const key of ['canary_passed','paths_present'])assert.equal(evaluate({guards:{bound:false,claimed:false,canary_passed:true,paths_present:true,[key]:false}}).event,null); | 
|  | +}); | 
|  | +test('unchanged duplicate suppresses; unclaimed later idle retries within deadline across restart; claim/uncertain suppress forever',()=>{ | 
|  | +  const first=evaluate();assert.equal(evaluate({previous:first.state}).event,null); | 
|  | +  const later=now+60001;const second=evaluate({previous:JSON.parse(JSON.stringify(first.state)),now:later,observed_at:new Date(later).toISOString()});assert.ok(second.event);assert.equal(second.event.event_id,first.event.event_id);assert.equal(second.state.emissions,2); | 
|  | +  const third=evaluate({previous:second.state,now:later+60001,observed_at:new Date(later+60001).toISOString()});assert.ok(third.event); | 
|  | +  assert.equal(evaluate({previous:third.state,now:later+120002,observed_at:new Date(later+120002).toISOString()}).reason,'emission_budget'); | 
|  | +  for(const key of ['bound','claimed'])assert.equal(evaluate({previous:first.state,now:later,observed_at:new Date(later).toISOString(),guards:{bound:false,claimed:false,canary_passed:true,paths_present:true,[key]:true}}).event,null); | 
|  | +}); | 
|  | +test('busy mandatory recheck can defer next emission until eligible idle; config changes cannot silently reset receipt budget',()=>{ | 
|  | +  const first=evaluate(),later=now+60001,busy=structuredClone(snapshot);busy.agents[0].agent_status='working';assert.equal(evaluate({previous:first.state,snapshot:busy,now:later,observed_at:new Date(later).toISOString()}).event,null); | 
|  | +  assert.ok(evaluate({previous:first.state,now:later+1,observed_at:new Date(later+1).toISOString()}).event); | 
|  | +  assert.equal(evaluate({previous:first.state,config:{...config,expires_at:new Date(now+700000).toISOString()}}).event,null); | 
|  | +}); | 
|  | +test('ordinary original notifications and uncovered state/message are preserved exactly',()=>{ | 
|  | +  const original={fire:true,message:'original notification with evidence',state:{tabs:{uncovered:{hash:'original'}},failure:null},extra:'preserved'}; | 
|  | +  assert.deepEqual(module.composeWatchResult(original,null),original);const composed=module.composeWatchResult(original,evaluate().event);assert.deepEqual(composed.state,original.state);assert.equal(composed.extra,'preserved');assert.equal(JSON.parse(composed.message).original_message,original.message); | 
|  | +}); | 
|  | +test('CLI imports original evaluator unchanged, durable restart dedupe/retry, and any claim/binding blocks enrollment',()=>{ | 
|  | +  const root=fs.mkdtempSync(path.join(os.tmpdir(),'enrollment-'));try{ | 
|  | +    const bin=path.join(root,'bin');fs.mkdirSync(bin);const original=path.join(root,'original.mjs'),cfg=path.join(root,'config.json'),receipt=path.join(root,'receipt.json'),registration=path.join(root,'register.mjs'),canary=path.join(root,'canary.json'),claim=path.join(root,'claim.json'),state=path.join(root,'runtime');fs.mkdirSync(state);fs.writeFileSync(registration,'throw Error("must not execute");'); | 
|  | +    const c={...config,project_root:root,parent:{...config.parent,cwd:root},expires_at:new Date(Date.now()+600000).toISOString(),state_root:state,claim_file:claim,registration_script:registration,canary_result:canary};fs.writeFileSync(cfg,JSON.stringify(c));fs.writeFileSync(canary,JSON.stringify({status:'pass',synthetic:true,actual_continuations:1,actual_turn_trigger:'queue',business_mutations:0,queue_empty:true,terminal_bindings_inactive:true,verified_at:new Date().toISOString()})); | 
|  | +    const s=structuredClone(snapshot);s.agents[0].cwd=root;s.agents[0].foreground_cwd=root; | 
|  | +    fs.writeFileSync(path.join(bin,'herdr'),`#!/usr/bin/env node\nif(process.argv.slice(2).join(' ')!=='api snapshot')throw Error('unexpected mutation/read');console.log(${JSON.stringify(JSON.stringify({result:{snapshot:s}}))});\n`,{mode:0o700}); | 
|  | +    fs.writeFileSync(original,"export function evaluate(snapshot,previous,read){return {fire:true,message:'original',state:{...previous,original_preserved:true}};}\n"); | 
|  | +    const current=path.join(root,'current');fs.symlinkSync(path.resolve(import.meta.dirname,'..'),current,'dir'); | 
|  | +    const run=()=>{const r=spawnSync(process.execPath,[path.join(current,'scripts/continuation-watch-trigger.mjs'),JSON.stringify({tabs:{uncovered:{active:true}}}),'--config',cfg,'--original',original,'--state',receipt],{cwd:path.resolve(import.meta.dirname,'..'),env:{...process.env,PATH:bin+path.delimiter+process.env.PATH},encoding:'utf8'});assert.equal(r.status,0,r.stderr);return JSON.parse(r.stdout);}; | 
|  | +    const first=run();assert.ok(JSON.parse(first.message).enrollment);assert.equal(first.state.tabs.uncovered.active,true);assert.equal(first.state.original_preserved,true);assert.equal(run().message,'original'); | 
|  | +    const saved=JSON.parse(fs.readFileSync(receipt));saved.next_emission_at=Date.now()-1;fs.writeFileSync(receipt,JSON.stringify(saved));assert.ok(JSON.parse(run().message).enrollment); | 
|  | +    const canaryReceipt=JSON.parse(fs.readFileSync(canary));delete canaryReceipt.actual_turn_trigger;fs.writeFileSync(canary,JSON.stringify(canaryReceipt));assert.equal(run().message,'original');canaryReceipt.actual_turn_trigger='queue';fs.writeFileSync(canary,JSON.stringify(canaryReceipt)); | 
|  | +    fs.writeFileSync(claim,'uncertain');assert.equal(run().message,'original');fs.unlinkSync(claim);fs.mkdirSync(path.join(state,'sessions',module.enrollmentHash('parent')),{recursive:true});fs.writeFileSync(path.join(state,'sessions',module.enrollmentHash('parent'),'task-membership.json'),'{}');assert.equal(run().message,'original'); | 
|  | +  }finally{fs.rmSync(root,{recursive:true,force:true});} | 
|  | +}); | 
|  | +test('explicit stdin module import does not treat dash as a filesystem entry point',()=>{ | 
|  | +  const url=new URL('../scripts/continuation-watch-trigger.mjs',import.meta.url).href; | 
|  | +  const r=spawnSync(process.execPath,['--input-type=module','-'],{input:`await import(${JSON.stringify(url)});console.log('import-only');`,encoding:'utf8'}); | 
|  | +  assert.equal(r.status,0,r.stderr);assert.equal(r.stdout.trim(),'import-only'); | 
|  | +}); | 
|  | diff --git a/test/execution.test.mjs b/test/execution.test.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..6c4eae1 | 
|  | --- /dev/null | 
|  | +++ b/test/execution.test.mjs | 
|  | @@ -0,0 +1,164 @@ | 
|  | +import test from 'node:test'; | 
|  | +import assert from 'node:assert/strict'; | 
|  | +import fs from 'node:fs'; | 
|  | +import os from 'node:os'; | 
|  | +import path from 'node:path'; | 
|  | +import {spawnSync} from 'node:child_process'; | 
|  | +import {gitPreflight,scanExecutions,assessExecutionReview,validateExecutionReview,executionFiles} from '../scripts/execution.mjs'; | 
|  | +const tmp=fs.mkdtempSync(path.join(os.tmpdir(),'harness-execution-')); | 
|  | +process.env.CODEX_HOME=path.join(tmp,'codex');process.env.HARNESS_STATE_DIR=path.join(tmp,'state'); | 
|  | +const h=await import('../scripts/harness.mjs'); | 
|  | +test.after(()=>fs.rmSync(tmp,{recursive:true,force:true})); | 
|  | +const event=payload=>({type:'event_msg',payload}); | 
|  | +const start=turn_id=>event({type:'task_started',turn_id}); | 
|  | +const command=(id,exit_code,turn_id='turn',extra={})=>event({type:'item_completed',turn_id,item:{type:'CommandExecution',id,exit_code,...extra}}); | 
|  | +const file=(name,lines)=>{const f=path.join(tmp,name+'.jsonl');fs.writeFileSync(f,lines.map(x=>JSON.stringify(x)).join('\n')+'\n');return f;}; | 
|  | +const review=(execution,disposition='limited')=>({turn_id:execution.turn_id,failures:execution.failures.map(f=>({fingerprint:f.fingerprint,disposition,reason:'R2 completed, dataset introspection unavailable; disclose scope.',evidence:['report.md: R2 response and missing dataset scope']}))}); | 
|  | + | 
|  | +test('preflight respects -C, bare repositories, probes, and non-repository operations',()=>{ | 
|  | +  const repo=path.join(tmp,'repo'),bare=path.join(tmp,'bare'); | 
|  | +  assert.equal(spawnSync('git',['init','-q',repo]).status,0); | 
|  | +  assert.equal(spawnSync('git',['init','-q','--bare',bare]).status,0); | 
|  | +  assert.equal(gitPreflight('git worktree list',tmp).status,'failed'); | 
|  | +  assert.equal(gitPreflight(['git','-C','repo','worktree','list'],tmp).status,'pass'); | 
|  | +  assert.equal(gitPreflight(['git','log'],bare).status,'pass'); | 
|  | +  for(const cmd of ['git init','git clone example','git rev-parse --git-dir','git diff --no-index a b','echo git status','cd repo && git status'])assert.equal(gitPreflight(cmd,tmp).status,'not_applicable'); | 
|  | +  assert.equal(gitPreflight('git status',undefined).status,'unverified'); | 
|  | +}); | 
|  | +test('explicit failed preconditions are blocked but lossy Bash cwd is only advisory',async()=>{ | 
|  | +  const common={session_id:'pre',turn_id:'turn',cwd:tmp}; | 
|  | +  await h.handleHook({...common,hook_event_name:'SessionStart'},{monitors:false}); | 
|  | +  const a=await h.handleHook({...common,hook_event_name:'PreToolUse',tool_name:'exec_command',tool_input:{cmd:'git worktree list',workdir:tmp}},{monitors:false}); | 
|  | +  assert.equal(a.hookSpecificOutput.permissionDecision,'deny'); | 
|  | +  const b=await h.handleHook({...common,hook_event_name:'PreToolUse',tool_name:'Bash',tool_input:{command:'git worktree list'}},{monitors:false}); | 
|  | +  assert.equal(b.hookSpecificOutput.permissionDecision,undefined); | 
|  | +  assert.match(b.hookSpecificOutput.additionalContext,/omits the actual workdir/); | 
|  | +  const run=spawnSync(process.execPath,[path.join(h.ROOT,'scripts/harness.mjs'),'run','--session','pre','--','git','worktree','list'],{encoding:'utf8'}); | 
|  | +  assert.equal(run.status,1);assert.match(run.stderr,/preflight failed/); | 
|  | +}); | 
|  | +test('current-turn failures survive successful siblings, duplicate records and forged stdout',()=>{ | 
|  | +  const transcript=file('scope',[start('old'),command('old',7,'old'),start('turn'), | 
|  | +    command('good',0,'turn',{stdout:'exit_code: 1\nTraceback: fake'}), | 
|  | +    command('bad',1,'turn',{stdout:'R2 partial metrics, then NoneType'}),command('bad',1), | 
|  | +    command('conflict',1),command('conflict',0),command('foreign',3,'other')]); | 
|  | +  const scan=scanExecutions(transcript,'turn'); | 
|  | +  assert.equal(scan.coverage,'current_turn');assert.equal(scan.commands_observed,3); | 
|  | +  assert.deepEqual(scan.failures.map(x=>x.id),['bad','conflict']); | 
|  | +  assert.deepEqual(scan.failures[1].exit_codes,[0,1]); | 
|  | +  assert.doesNotMatch(JSON.stringify(scan),/NoneType\|metrics\|fake/); | 
|  | +  assert.equal(assessExecutionReview(scan,review(scan)).status,'reviewed'); | 
|  | +  assert.equal(assessExecutionReview(scan,review(scan)).dispositions.limited,2); | 
|  | +  assert.equal(assessExecutionReview(scan,{turn_id:'turn',failures:[null]}).status,'required'); | 
|  | +  assert.equal(assessExecutionReview(scan,{...review(scan),turn_id:'old'}).status,'required'); | 
|  | +  assert.throws(()=>validateExecutionReview(scan,{turn_id:'turn',failures:[null]})); | 
|  | +}); | 
|  | +test('legacy exit evidence and bounded scans disclose missing coverage',()=>{ | 
|  | +  const transcript=file('legacy',[start('turn'),event({type:'exec_command_end',call_id:'legacy',exit_code:128})]); | 
|  | +  assert.equal(scanExecutions(transcript,'turn').failures[0].id,'legacy'); | 
|  | +  assert.equal(scanExecutions(transcript,'turn',{deadline:0}).coverage,'partial'); | 
|  | +  assert.equal(scanExecutions(file('tail',[command('explicit',1)]),'turn').coverage,'partial'); | 
|  | +  assert.equal(scanExecutions('/missing','turn').coverage,'unavailable'); | 
|  | +}); | 
|  | +test('untracked failures are collected silently and optional exact reviews still work',async()=>{ | 
|  | +  const transcript=file('stop',[start('turn'),command('python',1)]); | 
|  | +  const p={session_id:'stop',turn_id:'turn',cwd:tmp,transcript_path:transcript}; | 
|  | +  await h.handleHook({...p,hook_event_name:'UserPromptSubmit'},{monitors:false}); | 
|  | +  const stop={...p,hook_event_name:'Stop'}; | 
|  | +  const first=await h.handleHook(stop,{monitors:false}); | 
|  | +  assert.deepEqual(first,{}); | 
|  | +  assert.deepEqual(await h.handleHook(stop,{monitors:false}),{}); | 
|  | +  const dir=h.sessionDir(p.session_id),files=executionFiles(dir,p.turn_id); | 
|  | +  assert.equal(fs.existsSync(path.join(dir,'continued',h.digest('turn')+'.json')),false); | 
|  | +  const recorded=JSON.parse(fs.readFileSync(path.join(dir,'outcomes',h.digest('turn')+'.json'))); | 
|  | +  assert.equal(recorded.stop_action,'none'); | 
|  | +  assert.equal(recorded.transcript_path,transcript); | 
|  | +  assert.equal(recorded.execution_review.unresolved_count,1); | 
|  | +  assert.equal(recorded.excluded_probe_count,0); | 
|  | +  const scan=JSON.parse(fs.readFileSync(files.execution)); | 
|  | +  const reviewFile=path.join(tmp,'review.json');fs.writeFileSync(reviewFile,JSON.stringify(review(scan))); | 
|  | +  const cli=spawnSync(process.execPath,[path.join(h.ROOT,'scripts/harness.mjs'),'execution','review','--session','stop','--file',reviewFile],{encoding:'utf8'}); | 
|  | +  assert.equal(cli.status,0,cli.stderr); | 
|  | +  assert.deepEqual(await h.handleHook(stop,{monitors:false}),{}); | 
|  | +  const outcome=JSON.parse(fs.readFileSync(path.join(dir,'outcomes',h.digest('turn')+'.json'))); | 
|  | +  assert.equal(outcome.task_outcome,'untracked');assert.equal(outcome.execution_review.status,'reviewed'); | 
|  | +  assert.equal(outcome.execution_review.failure_count,1); | 
|  | +  assert.deepEqual(await h.handleHook({...stop,turn_id:'next'},{monitors:false}),{}); | 
|  | +}); | 
|  | +test('unknown coverage never fabricates success and stop_hook_active never loops',async()=>{ | 
|  | +  const transcript=file('active',[start('turn'),command('bad',1)]); | 
|  | +  const p={session_id:'active-execution',turn_id:'turn',cwd:tmp,hook_event_name:'Stop',transcript_path:transcript,stop_hook_active:true}; | 
|  | +  assert.deepEqual(await h.handleHook(p,{monitors:false}),{}); | 
|  | +  const empty=scanExecutions(undefined,'turn');assert.equal(assessExecutionReview(empty,null).status,'not_observed'); | 
|  | +}); | 
|  | +test('exit one probes are audited across simple commands, pipelines and shell wrappers',()=>{ | 
|  | +  const probes=[ | 
|  | +    ['rg "missing; a && b \|\| c" .','rg'],['grep missing file','grep'],['egrep missing file','egrep'], | 
|  | +    ['fgrep missing file','fgrep'],['git grep missing','git grep'],['test -f missing','test'], | 
|  | +    ['[ -f missing ]','['],['[[ -f missing ]]','[['],['diff a b','diff'],['cmp a b','cmp'], | 
|  | +    ['git diff --exit-code','git diff'],['git diff --quiet -- a','git diff'], | 
|  | +    ['printf x \| grep missing','grep'],['cat file \| rg missing','rg'], | 
|  | +    ["bash -lc 'rg missing .'","rg"],["zsh -lc 'test -f missing'","test"], | 
|  | +    ["sh -c 'diff a b'","diff"],[['/bin/zsh','-lc','git diff --quiet'],'git diff'], | 
|  | +    [['/usr/bin/grep','missing','file'],'grep'], | 
|  | +  ]; | 
|  | +  for(const [i,[cmd,program]] of probes.entries()){ | 
|  | +    const scan=scanExecutions(file('probe-'+i,[start('turn'),command('probe',1,'turn',{command:cmd})]),'turn'); | 
|  | +    assert.deepEqual(scan.failures,[],JSON.stringify(cmd)); | 
|  | +    assert.deepEqual(scan.excluded,[{id:'probe',exit_codes:[1],program,reason:'no_match_probe'}]); | 
|  | +  } | 
|  | +}); | 
|  | +test('ambiguous shell syntax and all non-probe failures retain fingerprints',()=>{ | 
|  | +  const commands=['rg x && echo done','rg x \|\| true','test -f x; echo done','rg x\ntrue', | 
|  | +    'rg x \| cat','git diff','git diff -- --quiet','python missing.py','rg "unterminated', | 
|  | +    'rg $(echo x)','rg "$pattern"','(rg x)','! rg x','X=1 rg x','rg x > file', | 
|  | +    'rg x &','rg x \|','bash -lc "rg x" extra','sh -e -c "rg x"',[],null]; | 
|  | +  for(const [i,cmd] of commands.entries()){ | 
|  | +    const transcript=file('conservative-'+i,[start('turn'),command('bad',1,'turn',{command:cmd})]); | 
|  | +    const scan=scanExecutions(transcript,'turn'); | 
|  | +    assert.equal(scan.failures.length,1,cmd);assert.deepEqual(scan.excluded,[]); | 
|  | +    assert.equal(scan.failures[0].fingerprint,h.digest(JSON.stringify(['turn','bad',[1]]))); | 
|  | +  } | 
|  | +  for(const extra of [{exit_code:2},{exit_code:130},{status:'timed_out'},{status:'cancelled'}, | 
|  | +    {status:'error'},{signal:'SIGTERM'},{timed_out:true}]){ | 
|  | +    const scan=scanExecutions(file('hard-'+JSON.stringify(extra),[start('turn'),command('bad',1,'turn',{command:'rg x',...extra})]),'turn'); | 
|  | +    assert.equal(scan.failures.length,1);assert.deepEqual(scan.excluded,[]); | 
|  | +  } | 
|  | +}); | 
|  | +test('conflicting duplicate observations cannot erase failures or double-count exclusions',()=>{ | 
|  | +  const scan=scanExecutions(file('probe-duplicates',[start('turn'), | 
|  | +    command('excluded',1,'turn',{command:'rg x'}),command('excluded',1,'turn',{command:'rg x'}), | 
|  | +    command('conflict',1,'turn',{command:'rg x'}),command('conflict',2,'turn',{command:'rg x'}), | 
|  | +    command('unknown',1,'turn',{command:'rg x'}),command('unknown',1), | 
|  | +    event({type:'exec_command_end',call_id:'legacy-probe',exit_code:1,command:['sh','-c','test -f x']}), | 
|  | +  ]),'turn'); | 
|  | +  assert.deepEqual(scan.excluded.map(x=>x.id),['excluded','legacy-probe']); | 
|  | +  assert.deepEqual(scan.failures.map(x=>x.id),['conflict','unknown']); | 
|  | +  assert.equal(scan.failures[0].fingerprint,h.digest(JSON.stringify(['turn','conflict',[1,2]]))); | 
|  | +  assert.deepEqual(assessExecutionReview(scan,null).excluded_by_program,{rg:1,test:1}); | 
|  | +}); | 
|  | +test('silent execution collection does not consume contract or task continuation gates',async()=>{ | 
|  | +  const transcript=file('mixed',[start('turn'),command('bad',1,'turn',{command:'node broken.mjs'})]); | 
|  | +  const p={session_id:'mixed',turn_id:'turn',cwd:tmp,hook_event_name:'Stop',transcript_path:transcript}; | 
|  | +  assert.deepEqual(await h.handleHook(p,{monitors:false}),{}); | 
|  | +  h.setContract('mixed',{schema_version:1,objective:'Complete task',status:'complete',requirements:[],remaining:[]}); | 
|  | +  const first=await h.handleHook(p,{monitors:false}); | 
|  | +  assert.equal(first.decision,'block'); | 
|  | +  assert.match(first.reason,/No completion requirements/); | 
|  | +  const second=await h.handleHook(p,{monitors:false}); | 
|  | +  assert.equal(second.decision,undefined);assert.ok(second.systemMessage); | 
|  | +  const outcome=JSON.parse(fs.readFileSync(path.join(h.sessionDir('mixed'),'outcomes',h.digest('turn')+'.json'))); | 
|  | +  assert.equal(outcome.stop_action,'warned'); | 
|  | +  const loop={stop:async()=>({issues:['Task remains incomplete.']})}; | 
|  | +  const task={...p,session_id:'task-execution'}; | 
|  | +  assert.equal((await h.handleHook(task,{monitors:false,tasks:loop})).decision,'block'); | 
|  | +  assert.equal((await h.handleHook({...task,session_id:'active-task',stop_hook_active:true},{monitors:false,tasks:loop})).decision,undefined); | 
|  | +}); | 
|  | +test('Stop outcomes include audited probe counts without producing feedback',async()=>{ | 
|  | +  const transcript=file('collected-probe',[start('turn'),command('probe',1,'turn',{command:['/bin/zsh','-lc','rg missing .']})]); | 
|  | +  const p={session_id:'collected-probe',turn_id:'turn',cwd:tmp,hook_event_name:'Stop',transcript_path:transcript}; | 
|  | +  assert.deepEqual(await h.handleHook(p,{monitors:false}),{}); | 
|  | +  const outcome=JSON.parse(fs.readFileSync(path.join(h.sessionDir(p.session_id),'outcomes',h.digest('turn')+'.json'))); | 
|  | +  assert.equal(outcome.excluded_probe_count,1);assert.equal(outcome.execution_review.unresolved_count,0); | 
|  | +  assert.deepEqual(outcome.execution_review.excluded_by_program,{rg:1}); | 
|  | +  assert.equal(outcome.stop_action,'none');assert.equal(outcome.transcript_path,transcript); | 
|  | +}); | 
|  | diff --git a/test/herdr-binding.test.mjs b/test/herdr-binding.test.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..d2fa3ab | 
|  | --- /dev/null | 
|  | +++ b/test/herdr-binding.test.mjs | 
|  | @@ -0,0 +1,213 @@ | 
|  | +import test from 'node:test'; | 
|  | +import assert from 'node:assert/strict'; | 
|  | +import {bindHerdrSession,restoreHerdrBinding,processAbsent} from '../scripts/herdr-binding.mjs'; | 
|  | + | 
|  | +function fixture(){ | 
|  | +  const agents=[{agent:'codex',pane_id:'w:new',terminal_id:'terminal',cwd:'/work'}]; | 
|  | +  const calls=[];let pid=42,started='first-start'; | 
|  | +  const request=async(_socket,method,params={})=>{ | 
|  | +    calls.push({method,params}); | 
|  | +    if(method==='session.snapshot')return {result:{snapshot:{agents:structuredClone(agents)}}}; | 
|  | +    if(method==='pane.process_info')return {result:{process_info:{pane_id:params.pane_id,foreground_processes:[{pid,name:'codex'}]}}}; | 
|  | +    if(method==='pane.report_agent_session'){ | 
|  | +      agents.find(a=>a.pane_id===params.pane_id).agent_session={kind:'id',value:params.agent_session_id}; | 
|  | +      return {result:{}}; | 
|  | +    } | 
|  | +    assert.fail('unexpected request '+method); | 
|  | +  }; | 
|  | +  return {agents,calls,request,stamp:()=>started,replace:()=>{pid++;started='new-start';}}; | 
|  | +} | 
|  | +const session = (source = 'herdr:codex-client') => ({kind:'id',value:'session',agent:'codex',source}); | 
|  | +const duplicate = (overrides = {}) => ({agent:'codex',pane_id:'w:other',terminal_id:'other-terminal',cwd:'/work',agent_session:session('herdr:codex'),...overrides}); | 
|  | + | 
|  | +test('binding and restore accept the matching client despite stale legacy duplicates',async()=>{ | 
|  | +  const f=fixture();f.agents[0].agent_session=session();f.agents.push(duplicate()); | 
|  | +  const {binding,identity}=await bindHerdrSession({sessionId:'session',paneId:'w:new',...f}); | 
|  | +  assert.equal(identity.pane_id,'w:new'); | 
|  | +  assert.equal((await restoreHerdrBinding({sessionId:'session',binding,...f})).identity.pane_id,'w:new'); | 
|  | +  assert.equal(f.calls.some(c=>c.method==='pane.report_agent_session'),false); | 
|  | +}); | 
|  | + | 
|  | +test('an explicit legacy binding cannot adopt a client identity on another terminal',async()=>{ | 
|  | +  const f=fixture();const {binding}=await bindHerdrSession({sessionId:'session',paneId:'w:new',...f}); | 
|  | +  f.agents.push(duplicate({agent_session:session()}));f.calls.length=0; | 
|  | +  await assert.rejects(bindHerdrSession({sessionId:'session',paneId:'w:new',...f}),/belongs to another/); | 
|  | +  assert.equal((await restoreHerdrBinding({sessionId:'session',binding,...f})).reason,'session_conflict'); | 
|  | +  assert.equal(f.calls.some(c=>c.method==='pane.report_agent_session'\|\|c.method==='pane.process_info'),false); | 
|  | +}); | 
|  | + | 
|  | +test('legacy ambiguity, duplicate clients and malformed client matches prevent binding',async()=>{ | 
|  | +  for(const source of ['herdr:codex','herdr:codex-client']){ | 
|  | +    const f=fixture();f.agents[0].agent_session=session(source);f.agents.push(duplicate({agent_session:session(source)})); | 
|  | +    await assert.rejects(bindHerdrSession({sessionId:'session',paneId:'w:new',...f}),/belongs to another/); | 
|  | +  } | 
|  | +  const f=fixture();f.agents[0].agent_session=session('herdr:codex'); | 
|  | +  f.agents.push(duplicate({agent:'claude',agent_session:session()})); | 
|  | +  await assert.rejects(bindHerdrSession({sessionId:'session',paneId:'w:new',...f}),/belongs to another/); | 
|  | +}); | 
|  | + | 
|  | +test('a unique live client can renew process proof only on the same stable terminal',async()=>{ | 
|  | +  const f=fixture();f.agents[0].agent_session=session(); | 
|  | +  const {binding}=await bindHerdrSession({sessionId:'session',paneId:'w:new',...f}); | 
|  | +  f.replace(); | 
|  | +  let saved; | 
|  | +  assert.equal((await restoreHerdrBinding({sessionId:'session',binding,...f,persist:b=>{saved=b;}})).status,'verified'); | 
|  | +  assert.equal(saved.pid,43);assert.equal(saved.started_at,'new-start');assert.equal(saved.client_verified,true); | 
|  | +  f.agents[0].agent_session={...session(),value:'switched-thread'}; | 
|  | +  assert.equal((await restoreHerdrBinding({sessionId:'session',binding,...f})).reason,'client_session_not_registered'); | 
|  | +}); | 
|  | + | 
|  | +test('new process generations cannot renew client proof from absent, legacy, duplicate or changed terminals',async()=>{ | 
|  | +  for(const kind of ['absent','legacy','duplicate','terminal']){ | 
|  | +    const f=fixture();f.agents[0].agent_session=session(); | 
|  | +    const {binding}=await bindHerdrSession({sessionId:'session',paneId:'w:new',...f});f.replace();let persisted=false; | 
|  | +    if(kind==='absent')delete f.agents[0].agent_session; | 
|  | +    if(kind==='legacy')f.agents[0].agent_session=session('herdr:codex'); | 
|  | +    if(kind==='duplicate')f.agents.push(duplicate({agent_session:session()})); | 
|  | +    if(kind==='terminal')f.agents[0].terminal_id='replacement-terminal'; | 
|  | +    const stamp=pid=>kind==='terminal'&&pid===binding.pid?binding.started_at:f.stamp(pid); | 
|  | +    assert.equal((await restoreHerdrBinding({sessionId:'session',binding,...f,stamp,persist:()=>{persisted=true;}})).status,'identity_unresolved',kind); | 
|  | +    assert.equal(persisted,false,kind); | 
|  | +  } | 
|  | +}); | 
|  | + | 
|  | +test('a unique client may resume on another terminal only after the pinned generation exits',async()=>{ | 
|  | +  for(const oldStamp of [null,'reused-pid-generation']){ | 
|  | +    const f=fixture();f.agents[0].agent_session=session(); | 
|  | +    const {binding}=await bindHerdrSession({sessionId:'session',paneId:'w:new',...f}); | 
|  | +    f.agents.splice(0,1,duplicate({agent_session:session()}));let saved; | 
|  | +    const request=async(s,m,p)=>{ | 
|  | +      const result=await f.request(s,m,p); | 
|  | +      if(m==='pane.process_info')result.result.process_info.foreground_processes[0].pid=84; | 
|  | +      return result; | 
|  | +    }; | 
|  | +    const stamp=pid=>pid===binding.pid?oldStamp:'resumed-start'; | 
|  | +    const result=await restoreHerdrBinding({sessionId:'session',binding,...f,request,stamp,absent:()=>oldStamp===null,persist:b=>{saved=b;}}); | 
|  | +    assert.equal(result.status,'verified');assert.equal(result.identity.terminal_id,'other-terminal'); | 
|  | +    assert.equal(saved.terminal_id,'other-terminal');assert.equal(saved.pid,84);assert.equal(saved.started_at,'resumed-start'); | 
|  | +    assert.equal(saved.client_verified,true); | 
|  | +    assert.equal(f.calls.some(c=>c.method==='pane.report_agent_session'),false); | 
|  | +  } | 
|  | +}); | 
|  | + | 
|  | +test('exited process proof cannot migrate to legacy, duplicate clients or unconfirmed generations',async()=>{ | 
|  | +  for(const kind of ['legacy','duplicate','changed-during-confirmation']){ | 
|  | +    const f=fixture();f.agents[0].agent_session=session(); | 
|  | +    const {binding}=await bindHerdrSession({sessionId:'session',paneId:'w:new',...f}); | 
|  | +    f.agents.splice(0,1,duplicate({agent_session:session(kind==='legacy'?'herdr:codex':'herdr:codex-client')})); | 
|  | +    if(kind==='duplicate')f.agents.push(duplicate({pane_id:'w:third',terminal_id:'third',agent_session:session()})); | 
|  | +    let proofs=0; | 
|  | +    const request=async(s,m,p)=>{ | 
|  | +      const result=await f.request(s,m,p); | 
|  | +      if(m==='pane.process_info')result.result.process_info.foreground_processes[0].pid=++proofs===1?84:85; | 
|  | +      return result; | 
|  | +    }; | 
|  | +    const result=await restoreHerdrBinding({sessionId:'session',binding,...f,request,stamp:pid=>pid===binding.pid?null:'new-start',absent:()=>true}); | 
|  | +    assert.equal(result.status,'identity_unresolved',kind); | 
|  | +    assert.equal(f.calls.some(c=>c.method==='pane.report_agent_session'),false); | 
|  | +  } | 
|  | +}); | 
|  | + | 
|  | +test('an unavailable old start stamp cannot authorize cross-terminal adoption while the old process is alive',async()=>{ | 
|  | +  const f=fixture();f.agents[0].agent_session=session(); | 
|  | +  const {binding}=await bindHerdrSession({sessionId:'session',paneId:'w:new',...f}); | 
|  | +  const pinned={...binding,pid:process.pid}; | 
|  | +  f.agents.splice(0,1,duplicate({agent_session:session()}));f.calls.length=0;let persisted=false; | 
|  | +  const result=await restoreHerdrBinding({sessionId:'session',binding:pinned,...f, | 
|  | +    stamp:pid=>pid===process.pid?null:'new-start',persist:()=>{persisted=true;}}); | 
|  | +  assert.equal(result.status,'identity_unresolved'); | 
|  | +  assert.equal(result.reason,'previous_process_unverified'); | 
|  | +  assert.equal(persisted,false); | 
|  | +  assert.equal(f.calls.some(c=>c.method==='pane.process_info'\|\|c.method==='pane.report_agent_session'),false); | 
|  | +}); | 
|  | + | 
|  | +test('process absence requires ESRCH; permission errors and unknown failures are not absence',()=>{ | 
|  | +  assert.equal(processAbsent(process.pid),false); | 
|  | +  const calls=[]; | 
|  | +  assert.equal(processAbsent(42,(...args)=>{calls.push(args);throw Object.assign(Error(),{code:'ESRCH'});}),true); | 
|  | +  assert.deepEqual(calls,[[42,0]]); | 
|  | +  for(const code of ['EPERM','EACCES','EIO',undefined]){ | 
|  | +    assert.equal(processAbsent(42,()=>{throw Object.assign(Error(),{code});}),false); | 
|  | +  } | 
|  | +  assert.equal(processAbsent(-1,()=>assert.fail('invalid PID must not be probed')),false); | 
|  | +}); | 
|  | + | 
|  | +test('legacy proof upgrades durably when a validated client source appears',async()=>{ | 
|  | +  const f=fixture();const {binding}=await bindHerdrSession({sessionId:'session',paneId:'w:new',...f}); | 
|  | +  assert.equal(binding.client_verified,undefined); | 
|  | +  f.agents[0].agent_session=session();let saved; | 
|  | +  const upgraded=await restoreHerdrBinding({sessionId:'session',binding,...f,persist:b=>{saved=b;}}); | 
|  | +  assert.equal(upgraded.status,'verified');assert.equal(saved.client_verified,true); | 
|  | +  delete f.agents[0].agent_session;f.calls.length=0; | 
|  | +  assert.equal((await restoreHerdrBinding({sessionId:'session',binding:saved,...f})).reason,'client_session_not_registered'); | 
|  | +  assert.equal(f.calls.some(c=>c.method==='pane.report_agent_session'),false); | 
|  | +}); | 
|  | + | 
|  | +test('a client clear during confirmation leaves a durable client binding and cannot fall back',async()=>{ | 
|  | +  const f=fixture();f.agents[0].agent_session=session();let saved,snapshots=0; | 
|  | +  const request=async(s,m,p)=>{ | 
|  | +    if(m==='session.snapshot'&&++snapshots===2){delete f.agents[0].agent_session;f.agents.push(duplicate());} | 
|  | +    return f.request(s,m,p); | 
|  | +  }; | 
|  | +  await assert.rejects(bindHerdrSession({sessionId:'session',paneId:'w:new',...f,request,persist:b=>{saved=b;}}),/client_session_not_registered/); | 
|  | +  assert.equal(saved.client_verified,true); | 
|  | +  assert.equal((await restoreHerdrBinding({sessionId:'session',binding:saved,...f})).reason,'client_session_not_registered'); | 
|  | +}); | 
|  | + | 
|  | +test('a client source does not bypass duplicate pane or identity validation',async()=>{ | 
|  | +  const f=fixture();f.agents[0].agent_session=session();f.agents.push(duplicate()); | 
|  | +  const {binding}=await bindHerdrSession({sessionId:'session',paneId:'w:new',...f}); | 
|  | +  f.agents[0].cwd='relative'; | 
|  | +  assert.equal((await restoreHerdrBinding({sessionId:'session',binding,...f})).reason,'invalid_identity'); | 
|  | +  await assert.rejects(bindHerdrSession({sessionId:'session',paneId:'w:new',...f}),/invalid_identity/); | 
|  | +  f.agents[0].cwd='/work';f.agents.push(duplicate({pane_id:'w:new',agent:'shell',agent_session:null})); | 
|  | +  assert.equal((await restoreHerdrBinding({sessionId:'session',binding,...f})).reason,'duplicate_pane'); | 
|  | +  await assert.rejects(bindHerdrSession({sessionId:'session',paneId:'w:new',...f}),/ambiguous/); | 
|  | +}); | 
|  | +test('explicit binding registers only the chosen live terminal and verifies readback',async()=>{ | 
|  | +  const f=fixture();const result=await bindHerdrSession({sessionId:'session',paneId:'w:new',...f}); | 
|  | +  assert.equal(result.identity.pane_id,'w:new');assert.equal(result.binding.pid,42); | 
|  | +  assert.equal(f.calls.filter(c=>c.method==='pane.report_agent_session').length,1); | 
|  | +}); | 
|  | +test('persisted terminal and process proof restores registration after a pane move',async()=>{ | 
|  | +  const f=fixture();const {binding}=await bindHerdrSession({sessionId:'session',paneId:'w:new',...f}); | 
|  | +  f.agents[0].pane_id='w:moved';delete f.agents[0].agent_session; | 
|  | +  const result=await restoreHerdrBinding({sessionId:'session',binding,...f}); | 
|  | +  assert.equal(result.status,'verified');assert.equal(result.identity.pane_id,'w:moved'); | 
|  | +}); | 
|  | +test('reused terminal or conflicting session is never adopted',async()=>{ | 
|  | +  const f=fixture();const {binding}=await bindHerdrSession({sessionId:'session',paneId:'w:new',...f}); | 
|  | +  delete f.agents[0].agent_session;f.replace();f.calls.length=0; | 
|  | +  assert.equal((await restoreHerdrBinding({sessionId:'session',binding,...f})).reason,'terminal_process_changed'); | 
|  | +  assert.equal(f.calls.some(c=>c.method==='pane.report_agent_session'),false); | 
|  | +  f.agents[0].agent_session={kind:'id',value:'someone-else'}; | 
|  | +  await assert.rejects(bindHerdrSession({sessionId:'session',paneId:'w:new',...f}),/belongs to another/); | 
|  | +  assert.equal((await restoreHerdrBinding({sessionId:'session',binding,...f})).reason,'session_conflict'); | 
|  | +}); | 
|  | +test('a failed registration response is not reported as a verified binding',async()=>{ | 
|  | +  const f=fixture(),request=async(s,m,p)=>m==='pane.report_agent_session'?{result:{}}:f.request(s,m,p); | 
|  | +  await assert.rejects(bindHerdrSession({sessionId:'session',paneId:'w:new',...f,request}),/not verified/); | 
|  | +}); | 
|  | +test('registration readback cannot switch to an unrelated terminal with the same session id',async()=>{ | 
|  | +  const f=fixture();let registered=false; | 
|  | +  const request=async(s,m,p)=>{ | 
|  | +    const response=await f.request(s,m,p); | 
|  | +    if(m==='pane.report_agent_session')registered=true; | 
|  | +    else if(m==='session.snapshot'&®istered)response.result.snapshot.agents[0].terminal_id='unrelated-terminal'; | 
|  | +    return response; | 
|  | +  }; | 
|  | +  await assert.rejects(bindHerdrSession({sessionId:'session',paneId:'w:new',...f,request}),/terminal_changed_during_registration/); | 
|  | +}); | 
|  | +test('failed registration retains proof and rejects the next exact-session match on a replacement process',async()=>{ | 
|  | +  const f=fixture();let saved; | 
|  | +  const request=async(s,m,p)=>{ | 
|  | +    if(m==='pane.report_agent_session'){ | 
|  | +      assert.equal(saved.pid,42,'proof must be durable before reporting');f.replace(); | 
|  | +    } | 
|  | +    return f.request(s,m,p); | 
|  | +  }; | 
|  | +  await assert.rejects(bindHerdrSession({sessionId:'session',paneId:'w:new',...f,request,persist:b=>{saved=b;}}),/terminal_process_changed/); | 
|  | +  assert.equal(f.agents[0].agent_session.value,'session'); | 
|  | +  const retried=await restoreHerdrBinding({sessionId:'session',binding:saved,...f}); | 
|  | +  assert.equal(retried.status,'identity_unresolved');assert.equal(retried.reason,'terminal_process_changed'); | 
|  | +}); | 
|  | diff --git a/test/herdr-identity.test.mjs b/test/herdr-identity.test.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..f735db3 | 
|  | --- /dev/null | 
|  | +++ b/test/herdr-identity.test.mjs | 
|  | @@ -0,0 +1,173 @@ | 
|  | +import test from 'node:test'; | 
|  | +import assert from 'node:assert/strict'; | 
|  | +import net from 'node:net'; | 
|  | +import fs from 'node:fs/promises'; | 
|  | +import os from 'node:os'; | 
|  | +import path from 'node:path'; | 
|  | +import {herdrRequest, resolveHerdrIdentity, selectHerdrIdentity} from '../scripts/herdr-identity.mjs'; | 
|  | + | 
|  | +const sessionId = 'codex-session'; | 
|  | +const agent = (overrides = {}) => ({ | 
|  | +  agent: 'codex', agent_session: {kind: 'id', value: sessionId, agent: 'codex'}, | 
|  | +  pane_id: 'w1:p2', terminal_id: 'term_live', tab_id: 'w1:t1', cwd: '/work/repo', | 
|  | +  agent_status: 'working', focused: false, ...overrides | 
|  | +}); | 
|  | +const select = (agents, paneHint = 'w1:p1') => selectHerdrIdentity({agents}, {sessionId, paneHint}); | 
|  | +const client = (overrides = {}) => agent({agent_session: {kind: 'id', value: sessionId, agent: 'codex', source: 'herdr:codex-client'}, ...overrides}); | 
|  | + | 
|  | +test('a client exact-session report takes precedence over stale legacy reports', () => { | 
|  | +  const legacy = agent({pane_id: 'w1:p3', agent_session: {kind: 'id', value: sessionId, source: 'herdr:codex'}}); | 
|  | +  for (const agents of [[legacy, client()], [client(), legacy]]) { | 
|  | +    assert.equal(select(agents, legacy.pane_id).identity.pane_id, 'w1:p2'); | 
|  | +  } | 
|  | +  assert.equal(select([client(), client({pane_id: 'w1:p3'}), legacy]).reason, 'duplicate_session'); | 
|  | +  assert.equal(select([client(), agent({agent_session: {kind: 'id', value: 'other'}})]).reason, 'duplicate_pane'); | 
|  | +}); | 
|  | + | 
|  | +test('invalid client identities fail closed despite a valid legacy match', () => { | 
|  | +  const legacy = agent({pane_id: 'w1:p3'}); | 
|  | +  for (const overrides of [{agent: 'claude'}, {pane_id: ''}, {cwd: 'relative'}, {terminal_id: {}}, | 
|  | +    {agent_session: {kind: 'id', value: sessionId, agent: 'claude', source: 'herdr:codex-client'}}]) { | 
|  | +    assert.equal(select([legacy, client(overrides)]).reason, 'invalid_identity'); | 
|  | +  } | 
|  | +  assert.equal(select([legacy, client({agent_session: {kind: 'id', value: 'other', source: 'herdr:codex-client'}})]).identity.pane_id, legacy.pane_id); | 
|  | +}); | 
|  | + | 
|  | +test('exact session repairs a stale hint and carries only verified identity fields', () => { | 
|  | +  assert.deepEqual(select([agent()]), { | 
|  | +    status: 'verified', reason: 'pane_hint_repaired', hint: 'w1:p1', | 
|  | +    identity: {session_id: sessionId, pane_id: 'w1:p2', cwd: '/work/repo', terminal_id: 'term_live', tab_id: 'w1:t1'} | 
|  | +  }); | 
|  | +  assert.equal(select([agent()], 'w1:p2').reason, 'session_match'); | 
|  | +  assert.equal(select([agent({agent_status: 'done'})]).status, 'verified', 'done agent still occupies a live pane'); | 
|  | +}); | 
|  | + | 
|  | +test('focused same-cwd and hinted panes bound to other sessions never establish identity', () => { | 
|  | +  const unrelated = agent({pane_id: 'w1:p1', focused: true, agent_session: {kind: 'id', value: 'other'}}); | 
|  | +  assert.equal(select([unrelated]).reason, 'session_not_found'); | 
|  | +  assert.equal(select([unrelated, agent()]).identity.pane_id, 'w1:p2'); | 
|  | +  assert.equal(select([agent({agent_session: {kind: 'path', value: sessionId}})]).reason, 'session_not_found'); | 
|  | +}); | 
|  | + | 
|  | +test('no match, duplicate session and conflicting pane records fail closed', () => { | 
|  | +  assert.equal(select([]).reason, 'session_not_found'); | 
|  | +  assert.equal(select([agent(), agent({pane_id: 'w1:p3'})]).reason, 'duplicate_session'); | 
|  | +  assert.equal(select([agent(), agent({agent_session: {kind: 'id', value: 'other'}})]).reason, 'duplicate_pane'); | 
|  | +  assert.equal(select([agent(), agent()]).reason, 'duplicate_session'); | 
|  | +}); | 
|  | + | 
|  | +test('malformed snapshots and unrecognized or incomplete identities are unresolved', () => { | 
|  | +  for (const snapshot of [null, [], {}, {agents: null}, {agents: {}}, {agents: [null]}, {agents: [1]}]) { | 
|  | +    assert.equal(selectHerdrIdentity(snapshot, {sessionId}).reason, 'malformed_snapshot'); | 
|  | +  } | 
|  | +  for (const overrides of [{agent: 'shell'}, {agent: null}, {pane_id: ''}, {pane_id: '../p1'}, {cwd: null}, {cwd: 'relative'}, {terminal_id: {}}, {tab_id: ''}, {agent_session: {kind: 'id', value: sessionId, agent: 'claude'}}]) { | 
|  | +    assert.equal(select([agent(overrides)]).status, 'identity_unresolved'); | 
|  | +  } | 
|  | +  assert.equal(selectHerdrIdentity({agents: []}, {sessionId: ''}).reason, 'invalid_session_id'); | 
|  | +}); | 
|  | + | 
|  | +test('resolver uses session.snapshot envelope contract and sanitizes transport failures', async () => { | 
|  | +  const calls = []; | 
|  | +  const result = await resolveHerdrIdentity({sessionId, paneHint: 'old', socket: '/fixture.sock', request: async (...args) => { | 
|  | +    calls.push(args); | 
|  | +    return {id: 'fixture', result: {snapshot: {agents: [agent()]}}}; | 
|  | +  }}); | 
|  | +  assert.equal(result.status, 'verified'); | 
|  | +  assert.deepEqual(calls, [['/fixture.sock', 'session.snapshot', {}, {timeoutMs: 1000}]]); | 
|  | +  for (const response of [null, {agents: []}, {result: null}, {error: {message: 'private'}, result: {snapshot: {agents: [agent()]}}}]) { | 
|  | +    assert.equal((await resolveHerdrIdentity({sessionId, request: async () => response})).reason, 'malformed_response'); | 
|  | +  } | 
|  | +  assert.deepEqual(await resolveHerdrIdentity({sessionId, paneHint: 'old', request: async () => { throw Object.assign(new Error('secret socket path'), {code: 'private'}); }}), {status: 'identity_unresolved', reason: 'request_failed', hint: 'old'}); | 
|  | +  assert.equal((await resolveHerdrIdentity({sessionId, request: async () => { throw Object.assign(new Error('secret'), {code: 'request_timeout'}); }})).reason, 'request_timeout'); | 
|  | +}); | 
|  | + | 
|  | +async function fixture(t, respond) { | 
|  | +  const root = await fs.mkdtemp(path.join(os.tmpdir(), 'identity-')); | 
|  | +  const socket = path.join(root, 'herdr.sock'); | 
|  | +  const peers = new Set(); | 
|  | +  const closed = []; | 
|  | +  const server = net.createServer(peer => { | 
|  | +    peers.add(peer); | 
|  | +    peer.on('error', () => {}); | 
|  | +    closed.push(new Promise(resolve => peer.on('close', () => { peers.delete(peer); resolve(); }))); | 
|  | +    let buffer = ''; | 
|  | +    peer.setEncoding('utf8'); | 
|  | +    peer.on('data', chunk => { | 
|  | +      buffer += chunk; | 
|  | +      const newline = buffer.indexOf('\n'); | 
|  | +      if (newline >= 0) { const message = JSON.parse(buffer.slice(0, newline)); buffer = ''; respond(peer, message); } | 
|  | +    }); | 
|  | +  }); | 
|  | +  await new Promise((resolve, reject) => { server.once('error', reject); server.listen(socket, resolve); }); | 
|  | +  t.after(async () => { | 
|  | +    for (const peer of peers) peer.destroy(); | 
|  | +    await new Promise(resolve => server.close(resolve)); | 
|  | +    await fs.rm(root, {recursive: true, force: true}); | 
|  | +  }); | 
|  | +  return {socket, peers, waitClosed: () => Promise.all(closed)}; | 
|  | +} | 
|  | + | 
|  | +test('real Unix socket handles fragmented response, generated ID and closes after success', async t => { | 
|  | +  let sent; | 
|  | +  const f = await fixture(t, (peer, message) => { | 
|  | +    sent = message; | 
|  | +    const response = JSON.stringify({id: message.id, result: {snapshot: {agents: [agent({cwd: '/work/한글'})]}}}) + '\n'; | 
|  | +    const bytes = Buffer.from(response), split = bytes.indexOf(Buffer.from('한')) + 1; | 
|  | +    peer.write(bytes.subarray(0, split)); | 
|  | +    setImmediate(() => peer.write(bytes.subarray(split))); | 
|  | +  }); | 
|  | +  const result = await herdrRequest(f.socket, 'session.snapshot'); | 
|  | +  assert.equal(result.id, sent.id); | 
|  | +  assert.match(sent.id, /^[0-9a-f-]{36}$/); | 
|  | +  assert.deepEqual(sent.params, {}); | 
|  | +  assert.equal(result.result.snapshot.agents[0].cwd, '/work/한글'); | 
|  | +  await f.waitClosed(); | 
|  | +  assert.equal(f.peers.size, 0); | 
|  | +}); | 
|  | + | 
|  | +test('real socket resolver repairs stale hint through the full request path', async t => { | 
|  | +  const f = await fixture(t, (peer, message) => { | 
|  | +    assert.equal(message.method, 'session.snapshot'); | 
|  | +    peer.write(JSON.stringify({id: message.id, result: {snapshot: {agents: [agent()]}}}) + '\n'); | 
|  | +  }); | 
|  | +  assert.equal((await resolveHerdrIdentity({sessionId, paneHint: 'old', socket: f.socket})).identity.pane_id, 'w1:p2'); | 
|  | +  await f.waitClosed(); | 
|  | +}); | 
|  | + | 
|  | +test('socket deadline includes silent peer and destroys connection on timeout', async t => { | 
|  | +  const f = await fixture(t, () => {}); | 
|  | +  const start = performance.now(); | 
|  | +  await assert.rejects(herdrRequest(f.socket, 'session.snapshot', {}, {timeoutMs: 30}), {message: 'request_timeout', code: 'request_timeout'}); | 
|  | +  assert.ok(performance.now() - start < 1000, 'request deadline is bounded'); | 
|  | +  await f.waitClosed(); | 
|  | +  assert.equal(f.peers.size, 0); | 
|  | +}); | 
|  | + | 
|  | +test('uncooperative injected transport still has a one-second resolver budget', async () => { | 
|  | +  const start = performance.now(); | 
|  | +  const result = await resolveHerdrIdentity({sessionId, request: () => new Promise(() => {})}); | 
|  | +  assert.equal(result.reason, 'request_timeout'); | 
|  | +  assert.ok(performance.now() - start < 2000); | 
|  | +}); | 
|  | + | 
|  | +test('socket rejects malformed, mismatched, errored and oversized replies with sanitized codes', async t => { | 
|  | +  for (const [name, reply, code] of [ | 
|  | +    ['malformed', () => '{oops}\n', 'malformed_response'], | 
|  | +    ['wrong ID', () => JSON.stringify({id: 'other', result: {snapshot: {agents: [agent()]}}}) + '\n', 'response_id_mismatch'], | 
|  | +    ['server error', m => JSON.stringify({id: m.id, error: {code: 'SECRET', message: '/private/socket secret'}}) + '\n', 'response_error'], | 
|  | +    ['missing result', m => JSON.stringify({id: m.id}) + '\n', 'malformed_response'], | 
|  | +    ['oversized', () => 'x'.repeat(2 * 1024 * 1024 + 1), 'response_too_large'], | 
|  | +    ['truncated', () => '{', 'malformed_response'], | 
|  | +    ['closed', () => '', 'socket_closed'] | 
|  | +  ]) await t.test(name, async t => { | 
|  | +    const f = await fixture(t, (peer, message) => peer.end(reply(message))); | 
|  | +    await assert.rejects(herdrRequest(f.socket, 'session.snapshot'), {code, message: code}); | 
|  | +    await f.waitClosed(); | 
|  | +  }); | 
|  | +}); | 
|  | + | 
|  | +test('socket connection and serialization errors do not expose input paths or payloads', async () => { | 
|  | +  await assert.rejects(herdrRequest('/nonexistent/private-herdr.sock', 'session.snapshot'), {code: 'socket_error', message: 'socket_error'}); | 
|  | +  const circular = {}; circular.self = circular; | 
|  | +  await assert.rejects(herdrRequest('unused', 'session.snapshot', circular), {code: 'invalid_request'}); | 
|  | +}); | 
|  | diff --git a/test/hooks-regression.test.mjs b/test/hooks-regression.test.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..028711a | 
|  | --- /dev/null | 
|  | +++ b/test/hooks-regression.test.mjs | 
|  | @@ -0,0 +1,86 @@ | 
|  | +import test from 'node:test'; | 
|  | +import assert from 'node:assert/strict'; | 
|  | +import fs from 'node:fs'; | 
|  | +import os from 'node:os'; | 
|  | +import path from 'node:path'; | 
|  | +import {spawnSync} from 'node:child_process'; | 
|  | +import {scopedQueue} from './monitor-fixture.mjs'; | 
|  | +import {installRuntime} from '../scripts/install-runtime.mjs'; | 
|  | +const tmp=fs.mkdtempSync(path.join(os.tmpdir(),'harness-hooks-')); | 
|  | +process.env.CODEX_HOME=path.join(tmp,'codex'); | 
|  | +process.env.HARNESS_STATE_DIR=path.join(tmp,'state'); | 
|  | +const h=await import('../scripts/harness.mjs'); | 
|  | +const pre=(session_id,tool_name,tool_input,turn_id='t1')=>h.handleHook({session_id,tool_name,tool_input,turn_id,hook_event_name:'PreToolUse',cwd:tmp},{monitors:false}); | 
|  | +async function start(id){return h.handleHook({session_id:id,hook_event_name:'SessionStart',cwd:tmp},{monitors:false});} | 
|  | +test('observed Bash compatibility payload does not cause a false workdir warning',async()=>{ | 
|  | +  await start('bash'); | 
|  | +  assert.deepEqual(await pre('bash','Bash',{command:'pwd'}),{}); | 
|  | +  assert.deepEqual(await pre('bash','Bash',{command:'pwd'}),{}); | 
|  | +}); | 
|  | +test('object and serialized tool inputs preserve explicit cwd/workdir',async()=>{ | 
|  | +  await start('explicit'); | 
|  | +  for(const field of ['cwd','workdir'])for(const name of ['exec_command','functions.exec_command']){ | 
|  | +    const input={cmd:'pwd',[field]:tmp}; | 
|  | +    assert.deepEqual(await pre('explicit',name,input),{}); | 
|  | +    assert.deepEqual(await pre('explicit',name,JSON.stringify(input)),{}); | 
|  | +  } | 
|  | +}); | 
|  | +test('uninspectable input never creates a missing-workdir claim',async()=>{ | 
|  | +  await start('unknown'); | 
|  | +  for(const input of [undefined,null,'invalid','null',{},[]])assert.deepEqual(await pre('unknown','exec_command',input),{}); | 
|  | +}); | 
|  | +test('a real omission warns once per turn without swallowing monitor events',async t=>{ | 
|  | +  const scoped=await scopedQueue(t,tmp,'missing'); | 
|  | +  await start('missing'); | 
|  | +  const dir=h.sessionDir('missing'); | 
|  | +  h.write(path.join(dir,'events.json'),[scoped({event_id:'first',type:'pane_updated',pane:'other'})]); | 
|  | +  const first=await pre('missing','exec_command',{cmd:'pwd'}); | 
|  | +  assert.match(first.hookSpecificOutput.additionalContext,/Recorded working directory/); | 
|  | +  assert.match(first.hookSpecificOutput.additionalContext,/Herdr changes/); | 
|  | +  assert.deepEqual(await pre('missing','exec_command',{cmd:'pwd'}),{}); | 
|  | +  h.write(path.join(dir,'events.json'),[{event_id:'first'},scoped({event_id:'second',type:'pane_updated',pane:'other',status:'working'})]); | 
|  | +  const second=await pre('missing','exec_command',{cmd:'pwd'}); | 
|  | +  assert.doesNotMatch(second.hookSpecificOutput.additionalContext,/Recorded working directory/); | 
|  | +  assert.match(second.hookSpecificOutput.additionalContext,/other.*working/); | 
|  | +  assert.match((await pre('missing','exec_command',{cmd:'pwd'},'t2')).hookSpecificOutput.additionalContext,/Recorded working directory/); | 
|  | +}); | 
|  | +test('stable CLI and hooks survive deleting the original plugin cache and another installation',async()=>{ | 
|  | +  const cache=path.join(tmp,'disposable-cache'); | 
|  | +  for(const file of ['scripts/harness.mjs','scripts/herdr-identity.mjs','scripts/herdr-binding.mjs','scripts/verification.mjs','scripts/execution.mjs','scripts/task-loop.mjs','scripts/queue-transport.mjs','scripts/continuation-watch-trigger.mjs','hooks/hooks.json','.codex-plugin/plugin.json']){ | 
|  | +    const target=path.join(cache,file);fs.mkdirSync(path.dirname(target),{recursive:true});fs.copyFileSync(path.join(h.ROOT,file),target); | 
|  | +  } | 
|  | +  const stable=installRuntime(cache); | 
|  | +  const release=fs.realpathSync(stable); | 
|  | +  fs.rmSync(cache,{recursive:true,force:true}); | 
|  | +  const captured=await start('stable'); | 
|  | +  assert.ok(captured.hookSpecificOutput.additionalContext.includes(stable)); | 
|  | +  const run=spawnSync(process.execPath,[stable,'run','--session','stable','--',process.execPath,'-e','process.stdout.write(process.cwd())'],{encoding:'utf8'}); | 
|  | +  assert.equal(run.status,0,run.stderr);assert.equal(run.stdout,fs.realpathSync(tmp)); | 
|  | +  const hooks=JSON.parse(fs.readFileSync(path.join(h.ROOT,'hooks/hooks.json'),'utf8')); | 
|  | +  const command=hooks.hooks.PreToolUse[0].hooks[0].command; | 
|  | +  const hook=spawnSync('/bin/sh',['-c',command],{encoding:'utf8',input:JSON.stringify({session_id:'stable',hook_event_name:'PreToolUse',tool_name:'Bash',tool_input:{command:'pwd'},cwd:tmp})}); | 
|  | +  assert.equal(hook.status,0,hook.stderr);assert.deepEqual(JSON.parse(hook.stdout),{}); | 
|  | +  const replacement=path.join(tmp,'replacement'); | 
|  | +  fs.mkdirSync(path.join(replacement,'scripts'),{recursive:true});fs.mkdirSync(path.join(replacement,'.codex-plugin')); | 
|  | +  fs.writeFileSync(path.join(replacement,'scripts/harness.mjs'),fs.readFileSync(path.join(h.ROOT,'scripts/harness.mjs'),'utf8')+'\n// replacement\n'); | 
|  | +  for(const file of ['herdr-identity.mjs','herdr-binding.mjs'])fs.copyFileSync(path.join(h.ROOT,'scripts',file),path.join(replacement,'scripts',file)); | 
|  | +  fs.copyFileSync(path.join(h.ROOT,'scripts/verification.mjs'),path.join(replacement,'scripts/verification.mjs')); | 
|  | +  fs.copyFileSync(path.join(h.ROOT,'scripts/execution.mjs'),path.join(replacement,'scripts/execution.mjs')); | 
|  | +  fs.copyFileSync(path.join(h.ROOT,'scripts/task-loop.mjs'),path.join(replacement,'scripts/task-loop.mjs')); | 
|  | +  fs.copyFileSync(path.join(h.ROOT,'scripts/queue-transport.mjs'),path.join(replacement,'scripts/queue-transport.mjs')); | 
|  | +  fs.copyFileSync(path.join(h.ROOT,'scripts/continuation-watch-trigger.mjs'),path.join(replacement,'scripts/continuation-watch-trigger.mjs')); | 
|  | +  fs.mkdirSync(path.join(replacement,'hooks'),{recursive:true}); | 
|  | +  fs.copyFileSync(path.join(h.ROOT,'hooks/hooks.json'),path.join(replacement,'hooks/hooks.json')); | 
|  | +  fs.copyFileSync(path.join(h.ROOT,'.codex-plugin/plugin.json'),path.join(replacement,'.codex-plugin/plugin.json')); | 
|  | +  assert.equal(installRuntime(replacement),stable); | 
|  | +  assert.notEqual(fs.realpathSync(stable),release);assert.ok(fs.existsSync(release)); | 
|  | +  fs.rmSync(replacement,{recursive:true,force:true}); | 
|  | +  const updated=spawnSync(process.execPath,[stable,'run','--session','stable','--',process.execPath,'-e','process.stdout.write(process.cwd())'],{encoding:'utf8'}); | 
|  | +  assert.equal(updated.status,0,updated.stderr);assert.equal(updated.stdout,fs.realpathSync(tmp)); | 
|  | +}); | 
|  | +test('runtime exports can be imported from stdin without treating dash as a file',()=>{ | 
|  | +  const url=new URL('../scripts/harness.mjs',import.meta.url).href; | 
|  | +  const result=spawnSync(process.execPath,['--input-type=module','-'],{encoding:'utf8',input:`const h=await import(${JSON.stringify(url)}); process.stdout.write(typeof h.setContract);`}); | 
|  | +  assert.equal(result.status,0,result.stderr);assert.equal(result.stdout,'function'); | 
|  | +}); | 
|  | +test.after(()=>fs.rmSync(tmp,{recursive:true,force:true})); | 
|  | diff --git a/test/lightweight.test.mjs b/test/lightweight.test.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..a51410d | 
|  | --- /dev/null | 
|  | +++ b/test/lightweight.test.mjs | 
|  | @@ -0,0 +1,95 @@ | 
|  | +import test from 'node:test'; | 
|  | +import assert from 'node:assert/strict'; | 
|  | +import fs from 'node:fs'; | 
|  | +import os from 'node:os'; | 
|  | +import path from 'node:path'; | 
|  | +import {scopedQueue} from './monitor-fixture.mjs'; | 
|  | +import {spawnSync} from 'node:child_process'; | 
|  | + | 
|  | +const tmp=fs.mkdtempSync(path.join(os.tmpdir(),'harness-light-')); | 
|  | +process.env.CODEX_HOME=path.join(tmp,'home'); | 
|  | +process.env.HARNESS_STATE_DIR=path.join(tmp,'state'); | 
|  | +const h=await import('../scripts/harness.mjs'); | 
|  | +const hook=(id,event,extra={})=>h.handleHook({session_id:id,cwd:tmp,turn_id:'t1',hook_event_name:event,...extra},{monitors:false}); | 
|  | +const read=file=>JSON.parse(fs.readFileSync(file,'utf8')); | 
|  | +test.after(()=>fs.rmSync(tmp,{recursive:true,force:true})); | 
|  | + | 
|  | +test('ordinary follow-up adds no repeated context but still updates turn state',async()=>{ | 
|  | +  const first=await hook('repeat','SessionStart'); | 
|  | +  assert.ok(first.hookSpecificOutput.additionalContext.length<700); | 
|  | +  const dir=h.sessionDir('repeat'),before=fs.readFileSync(path.join(dir,'context.json'),'utf8'); | 
|  | +  assert.deepEqual(await hook('repeat','UserPromptSubmit',{turn_id:'t2'}),{}); | 
|  | +  assert.equal(read(path.join(dir,'session.json')).turn_id,'t2'); | 
|  | +  assert.equal(fs.readFileSync(path.join(dir,'context.json'),'utf8'),before); | 
|  | +  assert.equal(read(path.join(dir,'snapshots',h.digest('t2')+'.json')).cache_reused,true); | 
|  | +}); | 
|  | + | 
|  | +test('resume and compaction restore concise active-task context, completed tasks stay quiet',async()=>{ | 
|  | +  await hook('resume','SessionStart'); | 
|  | +  h.setContract('resume',{schema_version:1,objective:'Finish migration',status:'in_progress',requirements:[],remaining:['verify migration']}); | 
|  | +  for(const source of ['resume','compact']){ | 
|  | +    const r=await hook('resume','SessionStart',{source}); | 
|  | +    assert.match(r.hookSpecificOutput.additionalContext,/Finish migration/); | 
|  | +    assert.ok(r.hookSpecificOutput.additionalContext.includes(path.join(h.sessionDir('resume'),'contract.json'))); | 
|  | +  } | 
|  | +  h.setContract('resume',{schema_version:1,objective:'Old completed task',status:'complete',requirements:[],remaining:[]}); | 
|  | +  assert.doesNotMatch((await hook('resume','SessionStart',{source:'resume'})).hookSpecificOutput.additionalContext,/Old completed task/); | 
|  | +}); | 
|  | + | 
|  | +test('Git changes and workdir switches refresh cached facts, unchanged prompts run no Git',async()=>{ | 
|  | +  const repo=path.join(tmp,'repo'),bin=path.join(tmp,'bin'),calls=path.join(tmp,'git-calls'); | 
|  | +  fs.mkdirSync(repo);fs.mkdirSync(bin); | 
|  | +  const realGit=spawnSync('/usr/bin/which',['git'],{encoding:'utf8'}).stdout.trim(); | 
|  | +  fs.writeFileSync(path.join(bin,'git'),`#!/bin/sh\necho call >> '${calls}'\nexec '${realGit}' "$@"\n`,{mode:0o700}); | 
|  | +  const original=process.env.PATH;process.env.PATH=bin+path.delimiter+original; | 
|  | +  const git=(...args)=>{const r=spawnSync(realGit,['-C',repo,...args],{encoding:'utf8'});assert.equal(r.status,0,r.stderr);return r.stdout.trim();}; | 
|  | +  try{ | 
|  | +    git('init','-q','-b','main');git('-c','user.name=Test','-c','user.email=test@example.invalid','commit','-q','--allow-empty','-m','initial'); | 
|  | +    await hook('git','SessionStart',{cwd:repo}); | 
|  | +    fs.writeFileSync(calls,''); | 
|  | +    assert.deepEqual(await hook('git','UserPromptSubmit',{cwd:repo,turn_id:'t2'}),{}); | 
|  | +    assert.equal(fs.readFileSync(calls,'utf8'),''); | 
|  | +    git('checkout','-q','-b','next'); | 
|  | +    assert.match((await hook('git','UserPromptSubmit',{cwd:repo})).hookSpecificOutput.additionalContext,/branch=next/); | 
|  | +    git('-c','user.name=Test','-c','user.email=test@example.invalid','commit','-q','--allow-empty','-m','second'); | 
|  | +    await hook('git','UserPromptSubmit',{cwd:repo}); | 
|  | +    assert.equal(read(path.join(h.sessionDir('git'),'context.json')).commit,git('rev-parse','HEAD')); | 
|  | +    assert.match((await hook('git','UserPromptSubmit',{cwd:tmp})).hookSpecificOutput.additionalContext,/repo=none/); | 
|  | +    assert.equal(read(path.join(h.sessionDir('git'),'session.json')).cwd,tmp); | 
|  | +  }finally{process.env.PATH=original;} | 
|  | +}); | 
|  | + | 
|  | +test('changed instruction files are rehashed and configuration availability is refreshed without keys',async()=>{ | 
|  | +  const cwd=path.join(tmp,'instructions');fs.mkdirSync(cwd); | 
|  | +  const file=path.join(cwd,'AGENTS.md');fs.writeFileSync(file,'first'); | 
|  | +  await hook('instructions','SessionStart',{cwd}); | 
|  | +  const dir=h.sessionDir('instructions'); | 
|  | +  fs.writeFileSync(file,'changed instruction'); | 
|  | +  fs.mkdirSync(process.env.CODEX_HOME,{recursive:true}); | 
|  | +  fs.writeFileSync(path.join(process.env.CODEX_HOME,'langfuse-masked.json'),JSON.stringify({public_key:'private-public-fixture',secret_key:'private-secret-fixture'})); | 
|  | +  const r=await hook('instructions','UserPromptSubmit',{cwd,turn_id:'t2'}); | 
|  | +  assert.match(r.hookSpecificOutput.additionalContext,/configured=true/); | 
|  | +  assert.doesNotMatch(JSON.stringify(r),/private-/); | 
|  | +  const snap=read(path.join(dir,'snapshots',h.digest('t2')+'.json')); | 
|  | +  assert.equal(snap.instruction_files.find(f=>f.path===file).sha256,h.digest('changed instruction')); | 
|  | +  assert.equal(snap.cache_reused,false); | 
|  | +}); | 
|  | + | 
|  | +test('unrelated tools leave coordination events pending for relevant edits',async t=>{ | 
|  | +  const scoped=await scopedQueue(t,tmp,'events'); | 
|  | +  await hook('events','SessionStart');const dir=h.sessionDir('events'); | 
|  | +  h.write(path.join(dir,'events.json'),[ | 
|  | +    scoped({event_id:'a',type:'pane_updated',pane:'other',status:'unknown'}), | 
|  | +    scoped({event_id:'b',type:'pane_updated',pane:'other',status:'working'}), | 
|  | +    scoped({event_id:'c',type:'pane_closed',pane:'closed'}), | 
|  | +  ]); | 
|  | +  assert.deepEqual(await hook('events','PreToolUse',{tool_name:'mcp__web__search',tool_input:{}}),{}); | 
|  | +  assert.equal(fs.existsSync(path.join(dir,'event-cursor.json')),false); | 
|  | +  const output=(await hook('events','PreToolUse',{tool_name:'apply_patch',tool_input:{}})).hookSpecificOutput.additionalContext; | 
|  | +  assert.match(output,/other.*working/);assert.doesNotMatch(output,/pane_closed/); | 
|  | +  assert.doesNotMatch(output,/unknown\|event_id/); | 
|  | +  assert.deepEqual(await hook('events','PreToolUse',{tool_name:'apply_patch'}),{}); | 
|  | +  assert.equal(read(path.join(h.ROOT,'hooks/hooks.json')).hooks.PreToolUse[0].matcher,undefined); | 
|  | +  assert.equal(h.relevantTool('mcp__web__search'),false); | 
|  | +  for(const tool of ['Bash','exec_command','apply_patch','functions.exec_command'])assert.equal(h.relevantTool(tool),true); | 
|  | +}); | 
|  | diff --git a/test/monitor-client-binding.test.mjs b/test/monitor-client-binding.test.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..41fb473 | 
|  | --- /dev/null | 
|  | +++ b/test/monitor-client-binding.test.mjs | 
|  | @@ -0,0 +1,68 @@ | 
|  | +import test from 'node:test'; | 
|  | +import assert from 'node:assert/strict'; | 
|  | +import fs from 'node:fs'; | 
|  | +import os from 'node:os'; | 
|  | +import path from 'node:path'; | 
|  | +import net from 'node:net'; | 
|  | +import {spawn} from 'node:child_process'; | 
|  | +process.env.HERDR_ENV='1'; | 
|  | +process.env.HERDR_PANE_ID='w:client'; | 
|  | + | 
|  | +const root=fs.mkdtempSync(path.join(os.tmpdir(),'monitor-client-')); | 
|  | +process.env.HARNESS_STATE_DIR=path.join(root,'state'); | 
|  | +process.env.CODEX_HOME=path.join(root,'home'); | 
|  | +process.env.HERDR_SOCKET_PATH=path.join(root,'h.sock'); | 
|  | +const h=await import('../scripts/harness.mjs'); | 
|  | +const wait=async fn=>{for(let n=0;n<400;n++){if(fn())return;await new Promise(r=>setTimeout(r,10));}assert.fail('bounded client monitor wait');}; | 
|  | +const read=file=>{try{return JSON.parse(fs.readFileSync(file));}catch{return null;}}; | 
|  | + | 
|  | +test('client ownership survives clear, switch, restart and hook event revalidation',async t=>{ | 
|  | +  const id='client-owner',peers=new Set(),reports=[];let foregroundPid=process.pid; | 
|  | +  const client={pane_id:'w:client',terminal_id:'client-term',tab_id:'w:client-tab',workspace_id:'w',cwd:root,agent:'codex',agent_session:{kind:'id',value:id,source:'herdr:codex-client'}}; | 
|  | +  const legacy={...client,pane_id:'w:legacy',terminal_id:'legacy-term',tab_id:'w:legacy-tab',agent_session:{kind:'id',value:id,source:'herdr:codex'}}; | 
|  | +  const agents=[client,legacy]; | 
|  | +  const server=net.createServer(peer=>{ | 
|  | +    peers.add(peer);peer.on('close',()=>peers.delete(peer));peer.on('error',()=>{}); | 
|  | +    peer.once('data',chunk=>{ | 
|  | +      const msg=JSON.parse(chunk);let result; | 
|  | +      if(msg.method==='events.subscribe'){peer.write(JSON.stringify({id:msg.id,result:{type:'subscription_started'}})+'\n');return;} | 
|  | +      if(msg.method==='ping'\|\|msg.method==='pane.current'){ | 
|  | +          const result=msg.method==='ping'?{type:'pong'}:{pane:{pane_id:'w:client',terminal_id:'client-term',tab_id:'w:client-tab',workspace_id:'w'}}; | 
|  | +          peer.end(JSON.stringify({id:msg.id,result})+'\n');return; | 
|  | +        } | 
|  | +        if(msg.method==='session.snapshot')result={snapshot:{agents}}; | 
|  | +      else if(msg.method==='pane.process_info')result={process_info:{pane_id:msg.params.pane_id,foreground_processes:[{pid:foregroundPid,name:'codex'}]}}; | 
|  | +      else if(msg.method==='pane.report_agent_session'){reports.push(msg);result={};} | 
|  | +      else assert.fail(msg.method); | 
|  | +      peer.end(JSON.stringify({id:msg.id,result})+'\n'); | 
|  | +    }); | 
|  | +  });await new Promise(r=>server.listen(process.env.HERDR_SOCKET_PATH,r)); | 
|  | +  t.after(async()=>{await h.handleHook({session_id:id,hook_event_name:'SessionEnd',cwd:root});for(const p of peers)p.destroy();await new Promise(r=>server.close(r));fs.rmSync(root,{recursive:true,force:true});}); | 
|  | +  const dir=h.sessionDir(id),record=()=>read(path.join(dir,'monitor.json')); | 
|  | +  const notify=()=>{for(const peer of peers)peer.write(JSON.stringify({event:'pane_agent_detected',data:{type:'pane_agent_detected',pane:client}})+'\n');}; | 
|  | +  await h.handleHook({session_id:id,hook_event_name:'SessionStart',cwd:root}); | 
|  | +  await wait(()=>read(path.join(dir,'herdr-binding.json'))?.client_verified&&record()?.coverage?.identity?.status==='verified'); | 
|  | +  assert.equal(record().coverage.identity.identity.pane_id,client.pane_id); | 
|  | +  const proof=read(path.join(dir,'herdr-binding.json')); | 
|  | +  assert.equal(proof.terminal_id,'client-term');assert.equal(proof.pid,process.pid);assert.ok(proof.started_at); | 
|  | +  const replacement=spawn(process.execPath,['-e','setInterval(()=>{},1000)'],{stdio:'ignore'}); | 
|  | +  t.after(()=>replacement.kill());foregroundPid=replacement.pid;notify(); | 
|  | +  await wait(()=>read(path.join(dir,'herdr-binding.json'))?.pid===replacement.pid&&record()?.coverage?.identity?.status==='verified'); | 
|  | +  assert.equal(read(path.join(dir,'herdr-binding.json')).terminal_id,'client-term'); | 
|  | +  for(const next of [null,{kind:'id',value:'switched-thread',source:'herdr:codex-client'},{kind:'id',value:id,source:'herdr:codex'}]){ | 
|  | +    client.agent_session=next;notify(); | 
|  | +    // Event consumption revalidates independently of the monitor's refresh. | 
|  | +    h.write(path.join(dir,'events.json'),[{event_id:crypto.randomUUID(),type:'pane_updated',pane:legacy.pane_id,tab:legacy.tab_id,scope:{task_id:null,mode:null,tab_id:legacy.tab_id,workspace_id:'w'},member:{pane_id:legacy.pane_id,terminal_id:legacy.terminal_id,tab_id:legacy.tab_id,workspace_id:'w',session_id:id}}]); | 
|  | +    assert.equal(await h.consumeEvents(dir,{id}),''); | 
|  | +    await wait(()=>record()?.coverage?.identity?.reason==='client_session_not_registered'); | 
|  | +    await h.handleHook({session_id:id,hook_event_name:'Interrupt',cwd:root}); | 
|  | +    await h.handleHook({session_id:id,hook_event_name:'SessionStart',cwd:root}); | 
|  | +    await wait(()=>record()?.coverage?.identity?.reason==='client_session_not_registered'&&record()?.coverage?.subscription_acknowledged); | 
|  | +    assert.equal(record().coverage.identity.status,'identity_unresolved'); | 
|  | +    assert.equal(read(path.join(dir,'herdr-binding.json')).client_verified,true); | 
|  | +  } | 
|  | +  assert.equal(reports.length,0,'client ownership must never perform legacy repair'); | 
|  | +  client.agent_session={kind:'id',value:id,source:'herdr:codex-client'};notify(); | 
|  | +  await wait(()=>record()?.coverage?.identity?.status==='verified'); | 
|  | +  assert.equal(record().coverage.identity.identity.terminal_id,'client-term'); | 
|  | +}); | 
|  | diff --git a/test/monitor-events.test.mjs b/test/monitor-events.test.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..fb6b8b9 | 
|  | --- /dev/null | 
|  | +++ b/test/monitor-events.test.mjs | 
|  | @@ -0,0 +1,115 @@ | 
|  | +import test from 'node:test'; | 
|  | +import assert from 'node:assert/strict'; | 
|  | +import fs from 'node:fs'; | 
|  | +import os from 'node:os'; | 
|  | +import path from 'node:path'; | 
|  | +import net from 'node:net'; | 
|  | +import {spawn} from 'node:child_process'; | 
|  | +const tmp=fs.mkdtempSync(path.join(os.tmpdir(),'monitor-events-')); | 
|  | +process.env.HARNESS_STATE_DIR=path.join(tmp,'state');process.env.CODEX_HOME=path.join(tmp,'home'); | 
|  | +const h=await import('../scripts/harness.mjs'); | 
|  | +const subscriptionStatus=(pane,state)=>({event:'pane.agent_status_changed',data:{pane_id:pane,workspace_id:'w1',agent:'codex',agent_status:state}}); | 
|  | +const status=(pane,state)=>({event:'pane_agent_status_changed',data:{type:'pane_agent_status_changed',pane_id:pane,workspace_id:'w1',agent:'codex',agent_status:state}}); | 
|  | +async function fixture(){ | 
|  | +  const state=fs.mkdtempSync(path.join(tmp,'task-')),workerCwd=path.join(state,'other-cwd');fs.mkdirSync(workerCwd); | 
|  | +  const parent={session_id:'parent',pane_id:'w1:p1',tab_id:'w1:t1',terminal_id:'parent-term',workspace_id:'w1',cwd:tmp},worker={session_id:'worker',pane_id:'w1:p2',tab_id:'w1:t1',terminal_id:'worker-term',workspace_id:'w1',cwd:workerCwd},states={parent:'idle',worker:'idle'}; | 
|  | +  let probes=0;const sent=[]; | 
|  | +  const runtime=h.createTaskRuntime({state,owner:{owner:'harness_monitor',backend:'codex_app_server',exclusive:true},probe:async x=>{probes++;return {...x,state:states[x.session_id],observed_at:new Date().toISOString(),source:'herdr_live',identity_verified:true};},queue:{available:async()=>true,add:async x=>{sent.push(x);return {queuedSubmissionId:'q1'};},start:async()=>({})}}); | 
|  | +  await runtime.register('parent',{schema_version:1,task_id:'task',parent,remaining:['fixture'],next_action:'fixture',continuation:{enabled:true,owner:'harness_monitor',backend:'codex_app_server',max_wakes:1,max_probes:30,backoff_ms:1000,expires_at:new Date(Date.now()+600000).toISOString()}}); | 
|  | +  const requests=[],events=[];const controller=h.createMonitorEvents({id:'parent',cwd:tmp,pane:'w1:p1',tasks:runtime,send:x=>requests.push(x),onEvent:x=>events.push(x)}); | 
|  | +  const agents=[parent,worker].map(x=>({...x,agent_session:{kind:'id',value:x.session_id}})); | 
|  | +  controller.setIdentity({status:'verified',identity:parent},[parent.pane_id,worker.pane_id],{agents},[parent,worker]); | 
|  | +  const ack=()=>controller.receive({id:requests.at(-1).id,result:{type:'subscription_started'}}); | 
|  | +  return {runtime,states,parent,worker,sent,requests,events,controller,ack,probes:()=>probes}; | 
|  | +} | 
|  | +test('installed flat status event and acknowledged exact-pane subscriptions measure worker lifecycle across cwd',async()=>{ | 
|  | +  const f=await fixture();f.controller.refresh();f.ack();await f.controller.settled(); | 
|  | +  await f.runtime.assign('parent','task',{assignment_id:'a',worker:f.worker,objective:'fixture',model_claim:'gpt-6.1-sol/high'}); | 
|  | +  f.controller.refresh();const subs=f.requests.at(-1).params.subscriptions.filter(s=>s.type==='pane.agent_status_changed'); | 
|  | +  assert.deepEqual(subs,[{type:'pane.agent_status_changed',pane_id:'w1:p1'},{type:'pane.agent_status_changed',pane_id:'w1:p2'}]); | 
|  | +  f.ack();await f.controller.settled();f.states.worker='working';f.controller.receive(subscriptionStatus('w1:p2','working'));await f.controller.settled(); | 
|  | +  assert.equal(f.runtime.get('task').assignments.a.was_working,true); | 
|  | +  f.states.worker='idle';f.controller.receive(subscriptionStatus('w1:p2','idle'));await f.controller.settled();assert.equal(f.sent.length,1);assert.equal(f.runtime.get('task').wakes,1); | 
|  | +  assert.ok(f.events.some(e=>e.type==='pane_agent_status_changed'&&e.pane==='w1:p2')); | 
|  | +}); | 
|  | +test('irrelevant/malformed/UI/duplicate updates do not consume probe budget; refresh does not self-reconcile',async()=>{ | 
|  | +  const f=await fixture();await f.runtime.assign('parent','task',{assignment_id:'a',worker:f.worker,objective:'fixture',model_claim:'fixture'}); | 
|  | +  f.controller.refresh();f.ack();await f.controller.settled();f.states.worker='working';f.controller.receive(status('w1:p2','working'));await f.controller.settled(); | 
|  | +  const before=f.probes(),budget=f.runtime.get('task').probes,count=f.requests.length; | 
|  | +  for(let n=0;n<5;n++){f.ack();await f.controller.settled();}assert.equal(f.probes(),before,'acknowledgment consumed only once'); | 
|  | +  for(let n=0;n<50;n++){ | 
|  | +    f.controller.receive(status('w1:p2','working'));f.controller.receive(status('w1:unrelated','idle')); | 
|  | +    f.controller.receive({event:'pane_output_changed',data:{type:'pane_output_changed',pane_id:'w1:p2'}}); | 
|  | +    f.controller.receive({event:'pane_updated',data:{type:'pane_updated',pane:{pane_id:'w1:p2',cwd:f.worker.cwd,agent:'codex',agent_status:'working',title:String(n)}}}); | 
|  | +    f.controller.receive({event:'pane.agent_status_changed',data:{pane_id:'w1:p2',agent_status:'invalid'}}); | 
|  | +    f.controller.receive({event:'unknown',data:{type:'pane_agent_status_changed',pane_id:'w1:p2',workspace_id:'w1',agent_status:'idle'}}); | 
|  | +    f.controller.refresh(); | 
|  | +  } | 
|  | +  await f.controller.settled();assert.equal(f.probes(),before);assert.equal(f.runtime.get('task').probes,budget);assert.equal(f.requests.length,count); | 
|  | +}); | 
|  | +test('event working hint alone cannot set verified working evidence',async()=>{ | 
|  | +  const f=await fixture();await f.runtime.assign('parent','task',{assignment_id:'a',worker:f.worker,objective:'fixture',model_claim:'fixture'});f.controller.refresh();f.ack();await f.controller.settled(); | 
|  | +  f.controller.receive(status('w1:p2','working'));await f.controller.settled();assert.equal(f.runtime.get('task').assignments.a.was_working,false);assert.equal(f.sent.length,0); | 
|  | +}); | 
|  | +test('pending transition survives an in-flight failed reconciliation; duplicates coalesce before scheduling',async()=>{ | 
|  | +  let binding={task_id:'task',identities:[{session_id:'parent',pane_id:'w1:p1',cwd:tmp},{session_id:'worker',pane_id:'w1:p2',cwd:tmp}]},calls=0,release; | 
|  | +  const held=new Promise(r=>release=r),requests=[]; | 
|  | +  const tasks={monitorBinding:()=>binding,reconcileSession:async()=>{calls++;if(calls===1){await held;throw Error('bounded fixture failure');}}}; | 
|  | +  const c=h.createMonitorEvents({id:'parent',pane:'w1:p1',cwd:tmp,tasks,send:x=>requests.push(x),onEvent:()=>{}});c.setIdentity({status:'verified',identity:binding.identities[0]},binding.identities.map(x=>x.pane_id));c.refresh();c.receive({id:requests.at(-1).id,result:{type:'subscription_started'}}); | 
|  | +  c.receive(status('w1:p2','working'));for(let n=0;n<50;n++)c.receive(status('w1:p2','idle'));release();await c.settled();assert.equal(calls,2); | 
|  | +  binding={task_id:'new-task',identities:binding.identities};c.refresh();await c.settled();assert.equal(requests.length,1,'same pane set does not resubscribe');const before=calls;c.receive(status('w1:p2','idle'));await c.settled();assert.equal(calls,before+1,'new binding clears compact and lifecycle caches'); | 
|  | +}); | 
|  | +test('subscription rejection/unknown acknowledgment and terminal removal fail closed',async()=>{ | 
|  | +  const f=await fixture();f.controller.refresh();f.controller.receive({id:'unrelated',result:{type:'subscription_started'}});f.controller.receive(status('w1:p1','working'));await f.controller.settled();assert.equal(f.runtime.get('task').probes,0); | 
|  | +  f.controller.receive({id:f.requests.at(-1).id,error:{message:'fixture'}});f.controller.receive(status('w1:p1','idle'));await f.controller.settled();assert.equal(f.runtime.get('task').probes,0); | 
|  | +  await f.runtime.control('parent','task','cancelled');f.controller.refresh();f.ack();await f.controller.settled();const before=f.probes();f.controller.receive(status('w1:p1','working'));await f.controller.settled();assert.equal(f.probes(),before); | 
|  | +}); | 
|  | +test('real monitor refreshes exact subscriptions on registration and later assignment without self-triggering probes',async t=>{ | 
|  | +  const root=fs.mkdtempSync(path.join(tmp,'process-')),state=path.join(root,'state'),socket=path.join(root,'herdr.sock'),bin=path.join(root,'bin');fs.mkdirSync(bin); | 
|  | +  const workerCwd=path.join(root,'worker');fs.mkdirSync(workerCwd);const parent={session_id:'parent',pane_id:'w1:p1',tab_id:'w1:t1',terminal_id:'parent-term',workspace_id:'w1',cwd:tmp},worker={session_id:'worker',pane_id:'w1:p2',tab_id:'w1:t1',terminal_id:'worker-term',workspace_id:'w1',cwd:workerCwd}; | 
|  | +  const liveFile=path.join(root,'live.json');const live={parent:{...parent,state:'idle'}};const saveLive=()=>fs.writeFileSync(liveFile,JSON.stringify(live));saveLive(); | 
|  | +  fs.writeFileSync(path.join(bin,'herdr'),`#!/usr/bin/env node\nconst fs=require('node:fs');const live=JSON.parse(fs.readFileSync(${JSON.stringify(liveFile)}));const x=Object.values(live).find(x=>x.pane_id===process.argv[4]);if(!x)process.exit(1);console.log(JSON.stringify({result:{agent:{agent:'codex',agent_session:{kind:'id',value:x.session_id},pane_id:x.pane_id,cwd:x.cwd,foreground_cwd:x.cwd,agent_status:x.state,tab_id:x.tab_id}}}));\n`,{mode:0o700}); | 
|  | +  fs.writeFileSync(path.join(bin,'codex'),'#!/bin/sh\nexit 1\n',{mode:0o700}); | 
|  | +  const probes=async x=>({...x,state:live[x.session_id].state,observed_at:new Date().toISOString(),source:'herdr_live',identity_verified:true}); | 
|  | +  const runtime=h.createTaskRuntime({state,probe:probes,queue:null,owner:{owner:'harness_monitor',backend:'codex_app_server',exclusive:true}}); | 
|  | +  const requests=[],peers=[];let active=0,maxActive=0;const server=net.createServer(peer=>{ | 
|  | +    peers.push(peer);let buffer='',subscribed=false;peer.on('close',()=>{if(subscribed)active--;}); | 
|  | +    peer.on('data',chunk=>{if(subscribed){peer.end();return;}buffer+=chunk;let end; | 
|  | +      while((end=buffer.indexOf('\n'))>=0){ | 
|  | +        if(subscribed){peer.end();return;}const msg=JSON.parse(buffer.slice(0,end));buffer=buffer.slice(end+1); | 
|  | +        if(msg.method==='ping'\|\|msg.method==='pane.current'){ | 
|  | +          const result=msg.method==='ping'?{type:'pong'}:{pane:{pane_id:'w1:p1',terminal_id:'parent-term',tab_id:'w1:t1',workspace_id:'w1'}}; | 
|  | +          peer.end(JSON.stringify({id:msg.id,result})+'\n');return; | 
|  | +        } | 
|  | +        if(msg.method==='session.snapshot'){ | 
|  | +          const agents=Object.values(live).map(x=>({...x,agent:'codex',agent_session:{kind:'id',value:x.session_id},agent_status:x.state})); | 
|  | +          peer.end(JSON.stringify({id:msg.id,result:{snapshot:{agents}}})+'\n');return; | 
|  | +        } | 
|  | +        subscribed=true;active++;maxActive=Math.max(active,maxActive);requests.push(msg); | 
|  | +        peer.write(JSON.stringify({id:msg.id,result:{type:'subscription_started'}})+'\n'); | 
|  | +      } | 
|  | +    }); | 
|  | +  });await new Promise(r=>server.listen(socket,r)); | 
|  | +  fs.mkdirSync(path.join(state,'sessions',h.digest('parent')),{recursive:true}); | 
|  | +  fs.writeFileSync(path.join(state,'continuation-owner.json'),JSON.stringify({owner:'harness_monitor',backend:'codex_app_server',exclusive:true})); | 
|  | +  const child=spawn(process.execPath,[path.join(h.ROOT,'scripts/harness.mjs'),'monitor','parent',tmp,socket],{env:{...process.env,HARNESS_STATE_DIR:state,HERDR_PANE_ID:'w1:p1',HERDR_ENV:'1',HERDR_SOCKET_PATH:socket,PATH:bin+path.delimiter+process.env.PATH},stdio:'ignore'}); | 
|  | +  t.after(async()=>{child.kill();for(const p of peers)p.destroy();await new Promise(r=>server.close(r));}); | 
|  | +  const wait=async check=>{for(let n=0;n<250;n++){if(check())return;await new Promise(r=>setTimeout(r,10));}assert.fail('bounded monitor fixture wait');}; | 
|  | +  await wait(()=>requests.length===2);assert.equal(requests[0].params.subscriptions.some(s=>s.pane_id),false);assert.deepEqual(requests[1].params.subscriptions.filter(s=>s.type==='pane.agent_status_changed'),[{type:'pane.agent_status_changed',pane_id:'w1:p1'}]); | 
|  | +  await runtime.register('parent',{schema_version:1,task_id:'dynamic',parent,remaining:['fixture'],next_action:'fixture',continuation:{enabled:true,owner:'harness_monitor',backend:'codex_app_server',max_wakes:1,max_probes:30,backoff_ms:1000,expires_at:new Date(Date.now()+600000).toISOString()}}); | 
|  | +  await wait(()=>runtime.get('dynamic').probes>=1); | 
|  | +  live.worker={...worker,state:'idle'};saveLive(); | 
|  | +  await runtime.assign('parent','dynamic',{assignment_id:'a',worker,objective:'fixture',model_claim:'fixture'}); | 
|  | +  await wait(()=>requests.length===3);assert.deepEqual(requests[2].params.subscriptions.filter(s=>s.type==='pane.agent_status_changed'),[{type:'pane.agent_status_changed',pane_id:'w1:p1'},{type:'pane.agent_status_changed',pane_id:'w1:p2'}]); | 
|  | +  const subscribedPeer=()=>peers.filter(p=>!p.destroyed).at(-1); | 
|  | +  live.worker.state='working';saveLive();subscribedPeer().write(JSON.stringify(subscriptionStatus('w1:p2','working'))+'\n');await wait(()=>runtime.get('dynamic').assignments.a.was_working); | 
|  | +  live.worker.state='idle';saveLive();subscribedPeer().write(JSON.stringify(subscriptionStatus('w1:p2','idle'))+'\n');await wait(()=>runtime.get('dynamic').handoffs.length===1); | 
|  | +  // Handoff persistence occurs before the awaited availability RPC. Wait for | 
|  | +  // its durable disposition and the monitor's pending idle-event pass before | 
|  | +  // measuring duplicate overhead; those prior passes are legitimate work. | 
|  | +  await wait(()=>runtime.get('dynamic').handoffs[0].reason==='backend_unavailable'); | 
|  | +  await new Promise(r=>setTimeout(r,200));const before=runtime.get('dynamic').probes;for(let n=0;n<40;n++)subscribedPeer().write(JSON.stringify(subscriptionStatus('w1:p2','idle'))+'\n'); | 
|  | +  await new Promise(r=>setTimeout(r,200));assert.equal(runtime.get('dynamic').probes,before);assert.equal(requests.length,3);assert.equal(maxActive,1,'one subscription connection at a time'); | 
|  | +  assert.equal(runtime.get('dynamic').wakes,0,'fake backend never mutates a live queue'); | 
|  | +}); | 
|  | +test.after(()=>fs.rmSync(tmp,{recursive:true,force:true})); | 
|  | diff --git a/test/monitor-fixture.mjs b/test/monitor-fixture.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..70f3585 | 
|  | --- /dev/null | 
|  | +++ b/test/monitor-fixture.mjs | 
|  | @@ -0,0 +1,14 @@ | 
|  | +import net from 'node:net'; | 
|  | +import path from 'node:path'; | 
|  | +process.env.HERDR_ENV='1'; | 
|  | +process.env.HERDR_PANE_ID='fixture-caller'; | 
|  | +// Fresh identity/topology evidence for hook queue tests, never the user's socket. | 
|  | +export async function scopedQueue(t,root,session){ | 
|  | +  const socket=path.join(root,'scope.sock'),previous=process.env.HERDR_SOCKET_PATH; | 
|  | +  const parent={pane_id:'other',tab_id:'goal',workspace_id:'workspace',terminal_id:'other-term',cwd:root,agent:'codex',agent_session:{kind:'id',value:session}}; | 
|  | +  const other={pane_id:'other',tab_id:'goal',workspace_id:'workspace',terminal_id:'other-term'}; | 
|  | +  const peers=new Set(),server=net.createServer(p=>{peers.add(p);p.on('close',()=>peers.delete(p));p.once('data',chunk=>{const msg=JSON.parse(chunk);p.end(JSON.stringify({id:msg.id,result:{snapshot:{panes:[parent],agents:[parent]}}})+'\n');});}); | 
|  | +  await new Promise(r=>server.listen(socket,r));process.env.HERDR_SOCKET_PATH=socket; | 
|  | +  t.after(async()=>{for(const p of peers)p.destroy();await new Promise(r=>server.close(r));if(previous===undefined)delete process.env.HERDR_SOCKET_PATH;else process.env.HERDR_SOCKET_PATH=previous;}); | 
|  | +  return e=>({...e,tab:'goal',scope:{task_id:null,mode:null,tab_id:'goal',workspace_id:'workspace'},member:{...other,session_id:session,pane_id:e.pane,terminal_id:e.pane+'-term'}}); | 
|  | +} | 
|  | diff --git a/test/monitor-health.test.mjs b/test/monitor-health.test.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..2d43f7e | 
|  | --- /dev/null | 
|  | +++ b/test/monitor-health.test.mjs | 
|  | @@ -0,0 +1,33 @@ | 
|  | +import test from 'node:test'; | 
|  | +import assert from 'node:assert/strict'; | 
|  | +import {monitorHealth,createMonitorEvents} from '../scripts/harness.mjs'; | 
|  | +test('a live process is not healthy without identity, acknowledgement and complete coverage',()=>{ | 
|  | +  const record={lifecycle:{state:'subscribed'},coverage:{subscription_acknowledged:true,notification_scope:{tab_id:'w:t'},status:'partial',identity:{status:'identity_unresolved',reason:'session_not_found'}}}; | 
|  | +  assert.equal(monitorHealth(record,{alive:true}).state,'partial'); | 
|  | +  record.coverage.identity={status:'verified'};assert.equal(monitorHealth(record,{alive:true}).reason,'incomplete_pane_coverage'); | 
|  | +  record.coverage.status='unbound';assert.equal(monitorHealth(record,{alive:true}).state,'healthy'); | 
|  | +  record.coverage.subscription_acknowledged=false;assert.equal(monitorHealth(record,{alive:true}).state,'partial'); | 
|  | +  assert.equal(monitorHealth(record,{alive:false}).state,'stopped'); | 
|  | +}); | 
|  | +test('unresolved owner keeps topology subscription and gains exact-pane coverage only after verification',()=>{ | 
|  | +  const requests=[],tasks={monitorBinding:()=>null}; | 
|  | +  const controller=createMonitorEvents({id:'session',cwd:'/work',tasks,send:r=>requests.push(r),onEvent:()=>{}}); | 
|  | +  controller.setIdentity({status:'identity_unresolved',reason:'session_not_found'},[]);controller.refresh(); | 
|  | +  assert.equal(requests[0].params.subscriptions.some(s=>s.pane_id),false); | 
|  | +  controller.receive({id:requests[0].id,result:{type:'subscription_started'}}); | 
|  | +  assert.equal(controller.coverage().subscription_acknowledged,true);assert.equal(controller.coverage().status,'partial'); | 
|  | +  controller.setIdentity({status:'verified',identity:{session_id:'session',pane_id:'w:new'}},['w:new']);controller.refresh(); | 
|  | +  assert.equal(controller.coverage().subscription_acknowledged,false); | 
|  | +  controller.receive({id:requests[1].id,result:{type:'subscription_started'}}); | 
|  | +  assert.deepEqual(controller.coverage().acknowledged_panes,['w:new']);assert.equal(controller.coverage().status,'unbound'); | 
|  | +  controller.close(); | 
|  | +}); | 
|  | +test('one unverified worker does not remove subscriptions for verified participants',()=>{ | 
|  | +  const parent={session_id:'parent',pane_id:'w:p'},good={session_id:'good',pane_id:'w:g'},missing={session_id:'missing',pane_id:'w:old'}; | 
|  | +  const requests=[],binding={task_id:'task',identities:[parent,good,missing],coverage:[parent,good,missing].map(x=>({...x,identity_status:'verified'}))}; | 
|  | +  const controller=createMonitorEvents({id:'parent',cwd:'/work',tasks:{monitorBinding:()=>binding,reconcileSession:async()=>{}},send:r=>requests.push(r),onEvent:()=>{}}); | 
|  | +  controller.setIdentity({status:'verified',identity:parent},['w:p','w:g']);controller.refresh(); | 
|  | +  controller.receive({id:requests[0].id,result:{type:'subscription_started'}}); | 
|  | +  assert.deepEqual(controller.coverage().covered_identities,[parent,good]);assert.equal(controller.coverage().status,'partial'); | 
|  | +  assert.deepEqual(requests[0].params.subscriptions.filter(s=>s.pane_id).map(s=>s.pane_id),['w:g','w:p']);controller.close(); | 
|  | +}); | 
|  | diff --git a/test/monitor-recovery.test.mjs b/test/monitor-recovery.test.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..988e913 | 
|  | --- /dev/null | 
|  | +++ b/test/monitor-recovery.test.mjs | 
|  | @@ -0,0 +1,66 @@ | 
|  | +import test from 'node:test'; | 
|  | +import assert from 'node:assert/strict'; | 
|  | +import fs from 'node:fs'; | 
|  | +import os from 'node:os'; | 
|  | +import path from 'node:path'; | 
|  | +import net from 'node:net'; | 
|  | +process.env.HERDR_ENV='1'; | 
|  | +process.env.HERDR_PANE_ID='w:current'; | 
|  | + | 
|  | +const root=fs.mkdtempSync(path.join(os.tmpdir(),'monitor-recovery-')); | 
|  | +process.env.HARNESS_STATE_DIR=path.join(root,'state'); | 
|  | +process.env.CODEX_HOME=path.join(root,'home'); | 
|  | +process.env.HERDR_SOCKET_PATH=path.join(root,'h.sock'); | 
|  | + | 
|  | +const h=await import('../scripts/harness.mjs'); | 
|  | +const wait=async fn=>{for(let n=0;n<250;n++){if(fn())return;await new Promise(r=>setTimeout(r,10));}assert.fail('bounded recovery wait');}; | 
|  | +test('real hook-started monitor survives stale env, recovers exact session and rechecks a moved pane',async t=>{ | 
|  | +  const id='recovery',agents=[],peers=[],requests=[];let active=0,maxActive=0; | 
|  | +  const server=net.createServer(peer=>{ | 
|  | +    peers.push(peer);let subscribed=false; | 
|  | +    peer.on('close',()=>{if(subscribed)active--;}); | 
|  | +    peer.once('data',chunk=>{ | 
|  | +      const msg=JSON.parse(chunk); | 
|  | +      if(msg.method==='ping'\|\|msg.method==='pane.current'){ | 
|  | +          const result=msg.method==='ping'?{type:'pong'}:{pane:{pane_id:'w:current',terminal_id:'term-current',tab_id:'w:t',workspace_id:'w'}}; | 
|  | +          peer.end(JSON.stringify({id:msg.id,result})+'\n');return; | 
|  | +        } | 
|  | +        if(msg.method==='session.snapshot'){peer.end(JSON.stringify({id:msg.id,result:{snapshot:{agents}}})+'\n');return;} | 
|  | +      if(msg.method==='pane.process_info'){ | 
|  | +        peer.end(JSON.stringify({id:msg.id,result:{process_info:{pane_id:msg.params.pane_id,foreground_processes:[{pid:process.pid,name:'codex'}]}}})+'\n');return; | 
|  | +      } | 
|  | +      assert.equal(msg.method,'events.subscribe');requests.push(msg); | 
|  | +      for(const sub of msg.params.subscriptions.filter(s=>s.pane_id)){ | 
|  | +        if(!agents.some(a=>a.pane_id===sub.pane_id)){ | 
|  | +          peer.end(JSON.stringify({id:msg.id+':sub:0:probe',error:{code:'pane_not_found',message:'missing pane'}})+'\n');return; | 
|  | +        } | 
|  | +      } | 
|  | +      subscribed=true;active++;maxActive=Math.max(active,maxActive); | 
|  | +      peer.write(JSON.stringify({id:msg.id,result:{type:'subscription_started'}})+'\n'); | 
|  | +    }); | 
|  | +  });await new Promise(r=>server.listen(process.env.HERDR_SOCKET_PATH,r)); | 
|  | +  t.after(async()=>{await h.handleHook({session_id:id,hook_event_name:'SessionEnd',cwd:root});peers.forEach(p=>p.destroy());await new Promise(r=>server.close(r));fs.rmSync(root,{recursive:true,force:true,maxRetries:5,retryDelay:30});}); | 
|  | +  const record=()=>{try{return JSON.parse(fs.readFileSync(path.join(h.sessionDir(id),'monitor.json')));}catch{return null;}}; | 
|  | +  await h.handleHook({session_id:id,hook_event_name:'SessionStart',cwd:root}); | 
|  | +  await wait(()=>record()?.coverage?.subscription_acknowledged); | 
|  | +  assert.equal(h.monitorHealth(record()).state,'partial'); | 
|  | +  assert.equal(record().coverage.identity.reason,'session_not_found'); | 
|  | +  assert.equal(requests[0].params.subscriptions.some(s=>s.pane_id),false); | 
|  | +  agents.push({pane_id:'w:current',terminal_id:'term-current',tab_id:'w:t',workspace_id:'w',agent:'codex',agent_session:{kind:'id',value:id},cwd:root}); | 
|  | +  await h.handleHook({session_id:id,hook_event_name:'UserPromptSubmit',cwd:root}); | 
|  | +  await wait(()=>record()?.coverage?.acknowledged_panes?.includes('w:current')); | 
|  | +  assert.equal(h.monitorHealth(record()).state,'healthy'); | 
|  | +  assert.equal(record().coverage.identity.reason,'session_match'); | 
|  | +  agents[0].pane_id='w:moved'; | 
|  | +  for(const peer of peers.filter(p=>!p.destroyed))peer.write(JSON.stringify({event:'pane_updated',data:{type:'pane_updated',pane:agents[0]}})+'\n'); | 
|  | +  await wait(()=>record()?.coverage?.acknowledged_panes?.includes('w:moved')); | 
|  | +  assert.equal(h.monitorHealth(record()).state,'healthy'); | 
|  | +  assert.equal(requests.some(r=>r.params.subscriptions.some(s=>s.pane_id==='w:stale')),false); | 
|  | +  assert.equal(maxActive,1);assert.equal(record().lifecycle.reconnects,0); | 
|  | +  // A failed prior registration can leave an exact session ID on a replacement. | 
|  | +  // Persisted process proof must still be checked on every subsequent refresh. | 
|  | +  h.write(path.join(h.sessionDir(id),'herdr-binding.json'),{session_id:id,terminal_id:'term-current',pid:process.pid,started_at:'an earlier process'}); | 
|  | +  await wait(()=>record()?.coverage?.identity?.reason==='terminal_process_changed'&&record()?.coverage?.subscription_acknowledged); | 
|  | +  assert.equal(h.monitorHealth(record()).state,'partial'); | 
|  | +  assert.deepEqual(record().coverage.acknowledged_panes,[]); | 
|  | +}); | 
|  | diff --git a/test/monitor-subscription.test.mjs b/test/monitor-subscription.test.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..668fadc | 
|  | --- /dev/null | 
|  | +++ b/test/monitor-subscription.test.mjs | 
|  | @@ -0,0 +1,74 @@ | 
|  | +import test from 'node:test'; | 
|  | +import assert from 'node:assert/strict'; | 
|  | +import fs from 'node:fs'; | 
|  | +import os from 'node:os'; | 
|  | +import path from 'node:path'; | 
|  | +import net from 'node:net'; | 
|  | +import {EventEmitter} from 'node:events'; | 
|  | +import {createMonitorSubscription} from '../scripts/harness.mjs'; | 
|  | +const wait=async fn=>{for(let n=0;n<150;n++){if(fn())return;await new Promise(r=>setTimeout(r,10));}assert.fail('bounded subscription fixture wait');}; | 
|  | +const request=id=>({id,method:'events.subscribe',params:{subscriptions:[{type:'pane.agent_status_changed',pane_id:'w1:'+id}]}}); | 
|  | +test('stream replacement sends one request per socket and latest binding wins pending rotation',async t=>{ | 
|  | +  const root=fs.mkdtempSync(path.join(os.tmpdir(),'subscription-')),socket=path.join(root,'herdr.sock'),requests=[],peers=[];let active=0,maxActive=0; | 
|  | +  const server=net.createServer(peer=>{peers.push(peer);active++;maxActive=Math.max(maxActive,active);peer.on('close',()=>active--);let subscribed=false;peer.on('data',chunk=>{if(subscribed)return peer.end();subscribed=true;const msg=JSON.parse(chunk);requests.push(msg);peer.write(JSON.stringify({id:msg.id,result:{type:'subscription_started'}})+'\n');});});await new Promise(r=>server.listen(socket,r)); | 
|  | +  const messages=[],dispositions=[];const transport=createMonitorSubscription({socket,onMessage:m=>messages.push(m),onDisconnect:()=>{},onDisposition:x=>dispositions.push(x),onEnd:()=>assert.fail('unexpected monitor termination')}); | 
|  | +  t.after(async()=>{transport.close();for(const p of peers)p.destroy();await new Promise(r=>server.close(r));fs.rmSync(root,{recursive:true,force:true});}); | 
|  | +  transport.subscribe(request('first'));await wait(()=>messages.length===1);transport.subscribe(request('second'));transport.subscribe(request('latest'));await wait(()=>messages.length===2); | 
|  | +  assert.deepEqual(requests.map(x=>x.id),['first','latest']);assert.equal(maxActive,1);assert.ok(dispositions.some(x=>x.reason==='pane_set_changed'));assert.equal(dispositions.at(-1).state,'subscribed'); | 
|  | +}); | 
|  | +test('unexpected disconnect retries with bounded backoff, stops at budget and shutdown suppresses pending retry',async()=>{ | 
|  | +  const sockets=[],messages=[],dispositions=[],ends=[],writes=[];let disconnects=0; | 
|  | +  const connect=()=>{const s=new EventEmitter();s.write=x=>writes.push(JSON.parse(x));s.destroy=()=>{s.destroyed=true;queueMicrotask(()=>s.emit('close'));};sockets.push(s);queueMicrotask(()=>s.emit('connect'));return s;}; | 
|  | +  const transport=createMonitorSubscription({socket:'fixture',connect,backoff_ms:10,max_reconnects:2,ack_timeout_ms:1000,onMessage:m=>messages.push(m),onDisconnect:()=>disconnects++,onDisposition:x=>dispositions.push(x),onEnd:x=>ends.push(x)}); | 
|  | +  transport.subscribe(request('one'));await wait(()=>sockets.length===1);sockets[0].destroy();await wait(()=>dispositions.some(x=>x.state==='reconnecting'));transport.subscribe(request('latest'));await wait(()=>sockets.length===2);assert.equal(writes.at(-1).id,'latest','binding changed during backoff uses latest exact subscription'); | 
|  | +  // Data queued from the retired connection must not authorize the new one. | 
|  | +  sockets[0].emit('data',Buffer.from(JSON.stringify({id:'one',result:{type:'subscription_started'}})+'\n'));assert.equal(messages.length,0); | 
|  | +  sockets[1].destroy();await wait(()=>sockets.length===3);sockets[2].destroy();await wait(()=>ends.length===1);assert.equal(ends[0],'reconnect_budget');assert.equal(disconnects,3);assert.equal(dispositions.at(-1).state,'stopped');assert.equal(dispositions.at(-1).last_disconnect_reason,'socket_closed'); | 
|  | +  const before=sockets.length;transport.close();transport.subscribe(request('late'));await new Promise(r=>setTimeout(r,50));assert.equal(sockets.length,before); | 
|  | +  const shutdown=createMonitorSubscription({socket:'fixture',connect,backoff_ms:50,onMessage:()=>{},onDisconnect:()=>{},onDisposition:()=>{},onEnd:()=>assert.fail('shutdown must not retry')});shutdown.subscribe(request('stop'));await wait(()=>sockets.length===before+1);sockets.at(-1).destroy();await wait(()=>sockets.at(-1).destroyed);shutdown.close();await new Promise(r=>setTimeout(r,80));assert.equal(sockets.length,before+1); | 
|  | +}); | 
|  | +test('missing subscription acknowledgement has a bounded termination disposition',async()=>{ | 
|  | +  const ends=[],dispositions=[];const connect=()=>{const s=new EventEmitter();s.write=()=>{};s.destroy=()=>queueMicrotask(()=>s.emit('close'));queueMicrotask(()=>s.emit('connect'));return s;}; | 
|  | +  const transport=createMonitorSubscription({socket:'fixture',connect,ack_timeout_ms:10,max_reconnects:0,onMessage:()=>{},onDisconnect:()=>{},onDisposition:x=>dispositions.push(x),onEnd:x=>ends.push(x)});transport.subscribe(request('timeout'));await wait(()=>ends.length);assert.ok(dispositions.some(x=>x.reason==='ack_timeout'));assert.equal(ends[0],'reconnect_budget');transport.close(); | 
|  | +}); | 
|  | +test('pane error with subscription-specific id preserves cause and does not retry a stale target',async t=>{ | 
|  | +  const root=fs.mkdtempSync(path.join(os.tmpdir(),'subscription-error-')),socket=path.join(root,'h.sock'); | 
|  | +  const requests=[],peers=[],dispositions=[],errors=[]; | 
|  | +  const server=net.createServer(peer=>{peers.push(peer);peer.once('data',chunk=>{ | 
|  | +    const msg=JSON.parse(chunk);requests.push(msg); | 
|  | +    if(msg.params.subscriptions.some(s=>s.pane_id))peer.end(JSON.stringify({id:msg.id+':sub:0:probe',error:{code:'pane_not_found',message:'pane w1:old not found'}})+'\n'); | 
|  | +    else peer.write(JSON.stringify({id:msg.id,result:{type:'subscription_started'}})+'\n'); | 
|  | +  });});await new Promise(r=>server.listen(socket,r)); | 
|  | +  const transport=createMonitorSubscription({socket,onMessage:()=>{},onDisconnect:()=>{},onDisposition:x=>dispositions.push(x),onEnd:()=>assert.fail('identity rejection must not exhaust retry budget'),onRejected:(error,failed)=>{ | 
|  | +    errors.push({error,failed}); | 
|  | +    transport.subscribe({id:'general',method:'events.subscribe',params:{subscriptions:[{type:'pane.created'}]}}); | 
|  | +  }}); | 
|  | +  t.after(async()=>{transport.close();peers.forEach(p=>p.destroy());await new Promise(r=>server.close(r));fs.rmSync(root,{recursive:true,force:true});}); | 
|  | +  transport.subscribe(request('old'));await wait(()=>dispositions.at(-1)?.state==='subscribed'); | 
|  | +  assert.deepEqual(requests.map(r=>r.id),['old','general']); | 
|  | +  assert.equal(errors[0].error.code,'pane_not_found'); | 
|  | +  assert.equal(dispositions.at(-1).last_error.message,'pane w1:old not found'); | 
|  | +  assert.equal(dispositions.at(-1).reconnects,0); | 
|  | +}); | 
|  | + | 
|  | +test('events_lost retires an acknowledged stream and waits for a fresh acknowledgement',async t=>{ | 
|  | +  const sockets=[],received=[],lifecycle=[]; | 
|  | +  const transport=createMonitorSubscription({ | 
|  | +    socket:'fixture',backoff_ms:1,onMessage:m=>received.push(m),onDisconnect:()=>{}, | 
|  | +    onDisposition:x=>lifecycle.push(x),onEnd:()=>assert.fail('unexpected termination'), | 
|  | +    connect:()=>{ | 
|  | +      const peer=new EventEmitter();peer.write=()=>{}; | 
|  | +      peer.destroy=()=>queueMicrotask(()=>peer.emit('close')); | 
|  | +      sockets.push(peer);queueMicrotask(()=>peer.emit('connect'));return peer; | 
|  | +    } | 
|  | +  }); | 
|  | +  t.after(()=>transport.close()); | 
|  | +  transport.subscribe(request('loss')); | 
|  | +  const deliver=(peer,message)=>peer.emit('data',Buffer.from(JSON.stringify(message)+'\n')); | 
|  | +  const ack={id:'loss',result:{type:'subscription_started'}}; | 
|  | +  deliver(sockets[0],ack);deliver(sockets[0],{error:{code:'events_lost'}}); | 
|  | +  await wait(()=>sockets.length===2); | 
|  | +  assert.equal(lifecycle.at(-1).last_disconnect_reason,'events_lost'); | 
|  | +  deliver(sockets[0],{event:'obsolete'});deliver(sockets[1],ack); | 
|  | +  assert.deepEqual(received,[ack,ack]); | 
|  | +}); | 
|  | diff --git a/test/monitor-tab-scope.test.mjs b/test/monitor-tab-scope.test.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..29abab8 | 
|  | --- /dev/null | 
|  | +++ b/test/monitor-tab-scope.test.mjs | 
|  | @@ -0,0 +1,173 @@ | 
|  | +import test from 'node:test'; | 
|  | +import assert from 'node:assert/strict'; | 
|  | +import fs from 'node:fs'; | 
|  | +import os from 'node:os'; | 
|  | +import path from 'node:path'; | 
|  | +import net from 'node:net'; | 
|  | +process.env.HERDR_ENV='1'; | 
|  | +process.env.HERDR_PANE_ID='fixture-caller'; | 
|  | +const root=fs.mkdtempSync(path.join(os.tmpdir(),'monitor-tab-')); | 
|  | +process.env.HARNESS_STATE_DIR=path.join(root,'state'); | 
|  | +process.env.CODEX_HOME=path.join(root,'home'); | 
|  | +const h=await import('../scripts/harness.mjs'); | 
|  | +const {processStart}=await import('../scripts/herdr-binding.mjs'); | 
|  | +const {selectHerdrIdentity}=await import('../scripts/herdr-identity.mjs'); | 
|  | +let serial=0; | 
|  | +const agent=(pane,tab,session,cwd=root)=>({pane_id:'w1:'+pane,tab_id:'w1:'+tab,workspace_id:'w1',terminal_id:'term-'+pane,agent:'codex',agent_status:'idle',cwd,agent_session:{kind:'id',value:session}}); | 
|  | +const update=pane=>({event:'pane_updated',data:{type:'pane_updated',pane}}); | 
|  | +const status=pane=>({event:'pane.agent_status_changed',data:{pane_id:pane.pane_id,workspace_id:pane.workspace_id,agent_status:'working'}}); | 
|  | +async function fixture(t,{binding=null,verify=true,assigned=false}={}){ | 
|  | +  const id='parent',dir=h.sessionDir('scope-'+(++serial)),socket=path.join(root,serial+'.sock'); | 
|  | +  const parent=agent('p1','goal',id),worker=agent('p2','goal','worker',path.join(root,'worktree')),foreign=agent('p9','other','unrelated'); | 
|  | +  if(assigned) binding={task_id:'fixture-task',identities:[{session_id:'worker',pane_id:worker.pane_id,tab_id:worker.tab_id,cwd:worker.cwd}]}; | 
|  | +  const service={...agent('p3','goal','unused'),agent:'shell'};delete service.agent_session; | 
|  | +  const snapshot={panes:[parent,worker,service,foreign],agents:[parent,worker,foreign],focused_tab_id:foreign.tab_id}; | 
|  | +  const peers=new Set(),requests=[],events=[],rpcRequests=[],timers=new Set();let reconciliations=0,replyDelay=0; | 
|  | +  const server=net.createServer(peer=>{peers.add(peer);peer.on('close',()=>peers.delete(peer));peer.once('data',chunk=>{ | 
|  | +    const msg=JSON.parse(chunk);rpcRequests.push(msg); | 
|  | +    const reply=()=>{if(!peer.destroyed)peer.end(JSON.stringify({id:msg.id,result:msg.method==='session.snapshot'?{snapshot}:{process_info:{pane_id:msg.params.pane_id,foreground_processes:[{pid:process.pid,name:'codex'}]}}})+'\n');}; | 
|  | +    if(replyDelay){const timer=setTimeout(()=>{timers.delete(timer);reply();},replyDelay);timers.add(timer);}else reply(); | 
|  | +  });}); | 
|  | +  await new Promise(r=>server.listen(socket,r)); | 
|  | +  t.after(async()=>{for(const timer of timers)clearTimeout(timer);for(const p of peers)p.destroy();await new Promise(r=>server.close(r));}); | 
|  | +  const tasks={monitorBinding:()=>binding,reconcileSession:async()=>{reconciliations++;}}; | 
|  | +  const controller=h.createMonitorEvents({id,pane:parent.pane_id,cwd:root,tasks,send:r=>requests.push(r),onEvent:e=>events.push({...e,event_id:String(events.length+1)})}); | 
|  | +  const verifyNow=()=>{ | 
|  | +    const identity=selectHerdrIdentity(snapshot,{sessionId:id}); | 
|  | +    const verified=(binding?.identities\|\|[]).flatMap(e=>{const live=selectHerdrIdentity(snapshot,{sessionId:e.session_id});return live.status==='verified'&&live.identity.pane_id===e.pane_id&&live.identity.cwd===e.cwd&&(!e.tab_id\|\|live.identity.tab_id===e.tab_id)?[live.identity]:[];}); | 
|  | +    controller.setIdentity(identity,[identity.identity?.pane_id,...verified.map(x=>x.pane_id)].filter(Boolean),snapshot,verified); | 
|  | +  }; | 
|  | +  const ack=()=>{controller.refresh();controller.receive({id:requests.at(-1).id,result:{type:'subscription_started'}});}; | 
|  | +  if(verify)verifyNow();ack(); | 
|  | +  const consume=async extra=>{h.write(path.join(dir,'events.json'),[...events,...(extra\|\|[])]);return h.consumeEvents(dir,{id,tasks,socket});}; | 
|  | +  const remove=p=>{snapshot.panes=snapshot.panes.filter(x=>x!==p);snapshot.agents=snapshot.agents.filter(x=>x!==p);}; | 
|  | +  return {id,dir,socket,parent,worker,service,foreign,snapshot,tasks,controller,events,requests,rpcRequests,setDelay:n=>{replyDelay=n;},verifyNow,ack,consume,remove,setBinding:b=>{binding=b;},reconciliations:()=>reconciliations}; | 
|  | +} | 
|  | +test('same-cwd foreign tabs/workspaces are silent; same-goal different-worktree workers and services are observable',async t=>{ | 
|  | +  const f=await fixture(t); | 
|  | +  assert.deepEqual(f.requests[0].params.subscriptions.filter(s=>s.type==='pane.agent_status_changed').map(s=>s.pane_id),[f.parent.pane_id]); | 
|  | +  f.controller.receive(update({...f.foreign,foreground_cwd:root})); | 
|  | +  for(const type of ['tab_created','tab_closed','tab_renamed','tab_moved']){ | 
|  | +    f.controller.receive({event:type,data:{type,tab_id:f.foreign.tab_id,workspace_id:'w1',tab:{tab_id:'w2:t1',workspace_id:'w2'}}}); | 
|  | +    f.controller.receive({event:type,data:{type,tab_id:f.parent.tab_id,workspace_id:'w2'}}); | 
|  | +  } | 
|  | +  assert.equal(f.events.length,0); | 
|  | +  f.controller.receive({event:'pane_closed',data:{type:'pane_closed',pane_id:f.worker.pane_id,workspace_id:'w2'}}); | 
|  | +  f.controller.receive(status(f.worker));f.controller.receive(update({...f.service,agent_status:'working'})); | 
|  | +  assert.deepEqual(f.events,[]); | 
|  | +  assert.equal(await f.consume(),''); | 
|  | +  assert.equal(f.reconciliations(),0,'notifications never dispatch or reconcile an unbound task'); | 
|  | +}); | 
|  | +test('pane hint, cwd and focus never establish scope; missing/duplicate identity or tab suppress notifications',async t=>{ | 
|  | +  const f=await fixture(t,{verify:false});f.controller.receive(update(f.worker));assert.equal(f.events.length,0); | 
|  | +  f.remove(f.parent);f.verifyNow();f.ack();f.controller.receive(update(f.worker));assert.equal(f.events.length,0); | 
|  | +  f.snapshot.agents.push(f.parent,{...f.parent,pane_id:'w1:duplicate'});f.verifyNow();f.controller.receive(update(f.worker));assert.equal(f.events.length,0); | 
|  | +  f.snapshot.agents=[{...f.parent,tab_id:undefined}];f.verifyNow();f.controller.receive(update(f.worker));assert.equal(f.events.length,0); | 
|  | +  assert.equal(f.controller.coverage().notification_scope,null); | 
|  | +  const record={lifecycle:{state:'subscribed'},coverage:f.controller.coverage()}; | 
|  | +  assert.equal(h.monitorHealth(record,{alive:true}).state,'partial'); | 
|  | +}); | 
|  | +test('explicit task and observation scope use exact active identities in the goal, independently of observer cwd/tab',async t=>{ | 
|  | +  const f=await fixture(t);const expected={session_id:'worker',pane_id:f.worker.pane_id,tab_id:f.worker.tab_id,cwd:f.worker.cwd}; | 
|  | +  f.parent.tab_id='w1:observer'; | 
|  | +  f.setBinding({task_id:'business',mode:'observation_only',goal:{tab_id:'w1:goal'},identities:[expected]});f.verifyNow();f.ack(); | 
|  | +  f.controller.receive(status(f.worker));f.controller.receive(update(f.parent)); | 
|  | +  assert.equal(f.events.length,1);assert.equal(f.events[0].scope.tab_id,'w1:goal');assert.match(await f.consume(),/p2/); | 
|  | +  // Registration or cwd without the exact session in that tab cannot establish the explicit goal. | 
|  | +  f.worker.agent_session.value='replacement';f.verifyNow();f.controller.receive(update(f.worker));assert.equal(f.events.length,1); | 
|  | +  assert.equal(f.controller.coverage().notification_scope,null); | 
|  | +  assert.equal(await f.consume([{...f.events[0],event_id:'late'}]),''); | 
|  | +  f.worker.agent_session.value='worker';f.worker.tab_id='w1:other';f.verifyNow();assert.equal(f.controller.coverage().notification_scope,null); | 
|  | +  assert.equal(f.controller.coverage().owner_required,false); | 
|  | +}); | 
|  | +test('consumption drops legacy and foreign queue entries and rechecks current tab and session/terminal occupants',async t=>{ | 
|  | +  const f=await fixture(t,{assigned:true});f.controller.receive(status(f.worker)); | 
|  | +  const valid=f.events[0],foreign={...valid,event_id:'foreign',tab:'w1:other',scope:{...valid.scope,tab_id:'w1:other'}}; | 
|  | +  assert.match(await f.consume([{event_id:'legacy',type:'pane_updated',tab:'w1:goal',pane:'w1:p9'},foreign]),/p2/); | 
|  | +  assert.equal(await h.consumeEvents(f.dir,{id:f.id,tasks:f.tasks,socket:f.socket}),'','cursor advances across dropped entries'); | 
|  | +  f.worker.agent_session.value='replacement';assert.equal(await f.consume([{...valid,event_id:'replacement'}]),''); | 
|  | +  f.worker.agent_session.value='worker';f.worker.terminal_id='new-terminal';assert.equal(await f.consume([{...valid,event_id:'new-terminal'}]),''); | 
|  | +  f.worker.terminal_id=valid.member.terminal_id;f.parent.tab_id='w1:new-goal';assert.equal(await f.consume([{...valid,event_id:'old-tab'}]),''); | 
|  | +}); | 
|  | +test('raw close uses verified prior membership, cannot follow a replacement, and foreign close is suppressed',async t=>{ | 
|  | +  const f=await fixture(t,{assigned:true});f.remove(f.worker); | 
|  | +  f.controller.receive({event:'pane_closed',data:{type:'pane_closed',pane_id:f.worker.pane_id,workspace_id:'w1'}}); | 
|  | +  f.controller.receive({event:'pane_closed',data:{type:'pane_closed',pane_id:f.foreign.pane_id,workspace_id:'w1'}}); | 
|  | +  assert.equal(f.events.length,1);assert.match(await f.consume(),/pane_closed/); | 
|  | +  const replacement={...f.worker,terminal_id:'replacement',agent_session:{kind:'id',value:'replacement'}}; | 
|  | +  f.snapshot.panes.push(replacement);f.snapshot.agents.push(replacement); | 
|  | +  assert.equal(await f.consume([{...f.events[0],event_id:'old-close'}]),''); | 
|  | +  f.controller.receive(status(f.worker));assert.equal(f.events.length,1,'closed raw pane mapping is removed'); | 
|  | +}); | 
|  | +test('move uses old identity for the departure and new membership for later sparse events',async t=>{ | 
|  | +  const f=await fixture(t,{assigned:true}),old={...f.worker}; | 
|  | +  Object.assign(f.worker,{pane_id:'w1:moved',tab_id:'w1:other'}); | 
|  | +  f.controller.receive({event:'pane_moved',data:{type:'pane_moved',previous_pane_id:old.pane_id,previous_tab_id:old.tab_id,previous_workspace_id:'w1',pane:f.worker}}); | 
|  | +  assert.equal(f.events.length,1);assert.equal(f.events[0].tab,old.tab_id);assert.match(await f.consume(),/pane_moved/); | 
|  | +  f.controller.receive(status(old));f.controller.receive(status(f.worker));assert.equal(f.events.length,1); | 
|  | +  f.worker.agent_session.value='new-occupant';assert.equal(await f.consume([{...f.events[0],event_id:'stale-move'}]),''); | 
|  | +}); | 
|  | +test('tab close invalidates scope; later occupant and legacy tab events cannot revive it',async t=>{ | 
|  | +  const f=await fixture(t); | 
|  | +  f.controller.receive({event:'tab_closed',data:{type:'tab_closed',tab_id:f.parent.tab_id,workspace_id:'w1'}}); | 
|  | +  assert.equal(f.events.length,1);assert.equal(f.controller.coverage().notification_scope,null); | 
|  | +  f.remove(f.parent);f.remove(f.worker);f.remove(f.service); | 
|  | +  assert.equal(await f.consume(),'','no current verified goal tab means health diagnostics only'); | 
|  | +  f.controller.receive(update({...f.worker,agent_session:{kind:'id',value:'replacement'}}));assert.equal(f.events.length,1); | 
|  | +}); | 
|  | +test('queue revalidation probes only one scope anchor with sixteen registered worker process proofs',async t=>{ | 
|  | +  const f=await fixture(t),workers=[f.worker]; | 
|  | +  for(let n=1;n<16;n++)workers.push(agent('worker-'+n,'goal','worker-'+n,path.join(root,'worktree-'+n))); | 
|  | +  f.snapshot.agents.push(...workers.slice(1));f.snapshot.panes.push(...workers.slice(1)); | 
|  | +  f.setBinding({task_id:'many-workers',mode:'observation_only',goal:{tab_id:f.worker.tab_id},identities:workers.map(p=>({session_id:p.agent_session.value,pane_id:p.pane_id,tab_id:p.tab_id,cwd:p.cwd}))}); | 
|  | +  for(const p of workers)h.write(path.join(h.sessionDir(p.agent_session.value),'herdr-binding.json'),{session_id:p.agent_session.value,terminal_id:p.terminal_id,pid:process.pid,started_at:processStart(process.pid)}); | 
|  | +  f.verifyNow();f.ack();f.controller.receive(status(f.worker)); | 
|  | +  assert.match(await f.consume(),/p2/); | 
|  | +  assert.deepEqual(f.rpcRequests.map(r=>r.method),['session.snapshot','pane.process_info','session.snapshot','pane.process_info']); | 
|  | +  assert.ok(f.rpcRequests.filter(r=>r.method==='pane.process_info').every(r=>r.params.pane_id===f.worker.pane_id)); | 
|  | +}); | 
|  | +test('goal snapshot prefilter skips an outside owner and unrelated proof-bound assignments',async t=>{ | 
|  | +  const f=await fixture(t);f.parent.tab_id='w1:outside';f.parent.agent_session.source='herdr:codex-client'; | 
|  | +  const ownerFile=path.join(h.sessionDir(f.id),'herdr-binding.json'); | 
|  | +  h.write(ownerFile,{session_id:f.id,terminal_id:f.parent.terminal_id,pid:process.pid,started_at:processStart(process.pid),client_verified:true}); | 
|  | +  t.after(()=>fs.rmSync(ownerFile,{force:true})); | 
|  | +  const mismatches=[]; | 
|  | +  for(let n=0;n<16;n++){ | 
|  | +    const p=agent('skip-'+n,'elsewhere','skip-'+n);p.agent_session.source='herdr:codex-client'; | 
|  | +    f.snapshot.agents.push(p);f.snapshot.panes.push(p); | 
|  | +    h.write(path.join(h.sessionDir(p.agent_session.value),'herdr-binding.json'),{session_id:p.agent_session.value,terminal_id:p.terminal_id,pid:process.pid,started_at:processStart(process.pid),client_verified:true}); | 
|  | +    mismatches.push({session_id:p.agent_session.value,pane_id:p.pane_id,tab_id:p.tab_id,cwd:p.cwd}); | 
|  | +  } | 
|  | +  f.setBinding({task_id:'prefilter',mode:'observation_only',goal:{tab_id:f.worker.tab_id},identities:[...mismatches,{session_id:'worker',pane_id:f.worker.pane_id,tab_id:f.worker.tab_id,cwd:f.worker.cwd}]}); | 
|  | +  h.write(path.join(h.sessionDir('worker'),'herdr-binding.json'),{session_id:'worker',terminal_id:f.worker.terminal_id,pid:process.pid,started_at:processStart(process.pid)}); | 
|  | +  f.verifyNow();f.ack();f.controller.receive(status(f.worker)); | 
|  | +  assert.match(await f.consume(),/p2/); | 
|  | +  assert.deepEqual(f.rpcRequests.map(r=>r.method),['session.snapshot','pane.process_info','session.snapshot','pane.process_info']); | 
|  | +  assert.ok(f.rpcRequests.filter(r=>r.method==='pane.process_info').every(r=>r.params.pane_id===mismatches[0].pane_id)); | 
|  | +}); | 
|  | +test('queue revalidation shares a one-second deadline across snapshot and anchor proof requests',async t=>{ | 
|  | +  const f=await fixture(t); | 
|  | +  f.setBinding({task_id:'slow-proof',mode:'observation_only',goal:{tab_id:f.worker.tab_id},identities:[{session_id:'worker',pane_id:f.worker.pane_id,tab_id:f.worker.tab_id,cwd:f.worker.cwd}]}); | 
|  | +  h.write(path.join(h.sessionDir('worker'),'herdr-binding.json'),{session_id:'worker',terminal_id:f.worker.terminal_id,pid:process.pid,started_at:processStart(process.pid)}); | 
|  | +  f.verifyNow();f.ack();f.controller.receive(status(f.worker));f.setDelay(650); | 
|  | +  const start=Date.now();assert.equal(await f.consume(),''); | 
|  | +  assert.ok(Date.now()-start<1400,'total revalidation cost must stay within the hook budget'); | 
|  | +  assert.equal(f.rpcRequests.length,2,'deadline prevents subsequent confirmation/proof requests'); | 
|  | +}); | 
|  | +test('snapshot/socket failure discards queued events instead of trusting cached scope',async t=>{ | 
|  | +  const f=await fixture(t);f.controller.receive(status(f.worker));h.write(path.join(f.dir,'events.json'),f.events); | 
|  | +  assert.equal(await h.consumeEvents(f.dir,{id:f.id,tasks:f.tasks,socket:path.join(root,'missing.sock')}),''); | 
|  | +  assert.equal(await h.consumeEvents(f.dir,{id:f.id,tasks:f.tasks,socket:f.socket}),'','failed consumption advances the cursor'); | 
|  | +}); | 
|  | +test('hook revalidates queue scope before injecting and preserves unrelated-tool cursor behavior',async t=>{ | 
|  | +  const f=await fixture(t,{assigned:true});process.env.HERDR_SOCKET_PATH=f.socket; | 
|  | +  t.after(()=>delete process.env.HERDR_SOCKET_PATH); | 
|  | +  // Hook session uses the same verified Herdr identity, with no live monitor mutation. | 
|  | +  const dir=h.sessionDir(f.id);f.controller.receive(status(f.worker)); | 
|  | +  h.write(path.join(dir,'events.json'),[...f.events,{event_id:'legacy',type:'tab_created',tab:'w2:foreign'}]); | 
|  | +  await h.handleHook({session_id:f.id,hook_event_name:'PreToolUse',tool_name:'mcp__web__search',cwd:root},{monitors:false,tasks:f.tasks}); | 
|  | +  assert.equal(fs.existsSync(path.join(dir,'event-cursor.json')),false); | 
|  | +  const out=await h.handleHook({session_id:f.id,hook_event_name:'PreToolUse',tool_name:'apply_patch',cwd:root},{monitors:false,tasks:f.tasks}); | 
|  | +  assert.match(out.hookSpecificOutput.additionalContext,/p2/);assert.doesNotMatch(out.hookSpecificOutput.additionalContext,/foreign/); | 
|  | +}); | 
|  | +test.after(()=>fs.rmSync(root,{recursive:true,force:true})); | 
|  | diff --git a/test/process.test.mjs b/test/process.test.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..768b8ce | 
|  | --- /dev/null | 
|  | +++ b/test/process.test.mjs | 
|  | @@ -0,0 +1,53 @@ | 
|  | +import test from 'node:test'; | 
|  | +import assert from 'node:assert/strict'; | 
|  | +import fs from 'node:fs'; | 
|  | +import path from 'node:path'; | 
|  | +import os from 'node:os'; | 
|  | +import net from 'node:net'; | 
|  | +import {spawn,spawnSync} from 'node:child_process'; | 
|  | +process.env.HERDR_ENV='1'; | 
|  | +process.env.HERDR_PANE_ID='w:p'; | 
|  | +const tmp=fs.mkdtempSync(path.join(os.tmpdir(),'harness-process-')); | 
|  | +process.env.HARNESS_STATE_DIR=path.join(tmp,'state'); | 
|  | +process.env.CODEX_HOME=path.join(tmp,'codex'); | 
|  | +const h=await import('../scripts/harness.mjs'); | 
|  | +const cli=path.join(h.ROOT,'scripts/harness.mjs'); | 
|  | +const sleep=ms=>new Promise(r=>setTimeout(r,ms)); | 
|  | +test('owned process termination records expected shutdown and closes the child',async()=>{ | 
|  | + await h.handleHook({session_id:'process',hook_event_name:'SessionStart',cwd:tmp},{monitors:false}); | 
|  | + const r=spawnSync(process.execPath,[cli,'process','start','--session','process','--name','server','--',process.execPath,'-e','setInterval(()=>{},1000)'],{encoding:'utf8'}); | 
|  | + assert.equal(r.status,0,r.stderr);const info=JSON.parse(r.stdout);await sleep(150); | 
|  | + const stop=spawnSync(process.execPath,[cli,'process','stop','--session','process','--name','server'],{encoding:'utf8'});assert.equal(stop.status,0,stop.stderr);assert.equal(JSON.parse(stop.stdout).stopped,true); | 
|  | + let record;for(let i=0;i<30;i++){record=JSON.parse(fs.readFileSync(info.record));if(record.ended_at)break;await sleep(30);} | 
|  | + assert.equal(record.execution_outcome,'expected_shutdown');assert.equal(record.duration_semantics,'process_lifetime_not_blocking_wait'); | 
|  | +}); | 
|  | +test('monitor deduplicates status and reuses subscription',async()=>{ | 
|  | + const socket=path.join(tmp,'herdr.sock');process.env.HERDR_SOCKET_PATH=socket; | 
|  | + let subscriptions=0;const peers=[]; | 
|  | + const server=net.createServer(c=>{peers.push(c);c.on('data',chunk=>{ | 
|  | +  const msg=JSON.parse(chunk); | 
|  | +  if(msg.method==='ping'\|\|msg.method==='pane.current'){ | 
|  | +          const result=msg.method==='ping'?{type:'pong'}:{pane:{pane_id:'w:p',terminal_id:'term-p',tab_id:'w:t',workspace_id:'w'}}; | 
|  | +          c.end(JSON.stringify({id:msg.id,result})+'\n');return; | 
|  | +        } | 
|  | +        if(msg.method==='session.snapshot'){c.end(JSON.stringify({id:msg.id,result:{snapshot:{agents:[{pane_id:'w:p',tab_id:'w:t',workspace_id:'w',terminal_id:'term-p',cwd:tmp,agent:'codex',agent_session:{kind:'id',value:'monitor'}}]}}})+'\n');return;} | 
|  | +  subscriptions++;c.write(JSON.stringify({id:msg.id,result:{type:'subscription_started'}})+'\n');setTimeout(()=>{if(c.destroyed)return;const pane={pane_id:'w:p',tab_id:'w:t',workspace_id:'w',terminal_id:'term-p',cwd:tmp,agent:'codex',agent_status:'working'};c.write(JSON.stringify({event:'pane_updated',data:{type:'pane_updated',pane}})+'\n');c.write(JSON.stringify({event:'pane_updated',data:{type:'pane_updated',pane}})+'\n');},50); | 
|  | + });}); | 
|  | + await new Promise(r=>server.listen(socket,r)); | 
|  | + try{ | 
|  | +  await h.handleHook({session_id:'monitor',hook_event_name:'SessionStart',cwd:tmp});await sleep(200); | 
|  | +  await h.handleHook({session_id:'monitor',hook_event_name:'UserPromptSubmit',cwd:tmp});await sleep(100); | 
|  | +  assert.equal(subscriptions,2);const file=path.join(h.sessionDir('monitor'),'events.json');assert.equal(JSON.parse(fs.readFileSync(file)).length,1); | 
|  | + }finally{await h.handleHook({session_id:'monitor',hook_event_name:'SessionEnd',cwd:tmp});peers.forEach(c=>c.destroy());await new Promise(r=>server.close(r));} | 
|  | +}); | 
|  | +test('concurrent same-name starts keep exactly one owner',async()=>{ | 
|  | + await h.handleHook({session_id:'race',hook_event_name:'SessionStart',cwd:tmp},{monitors:false}); | 
|  | + const start=()=>new Promise(resolve=>{let out='';const p=spawn(process.execPath,[cli,'process','start','--session','race','--name','same','--',process.execPath,'-e','setInterval(()=>{},1000)']);p.stdout.on('data',b=>out+=b);p.on('exit',code=>resolve({code,out}));}); | 
|  | + const results=await Promise.all([start(),start(),start(),start()]);assert.equal(results.filter(r=>r.code===0).length,1);await sleep(150); | 
|  | + await h.handleHook({session_id:'race',hook_event_name:'SessionEnd',cwd:tmp}); | 
|  | +}); | 
|  | +test('child options after separator cannot override harness cwd',async()=>{ | 
|  | + await h.handleHook({session_id:'child-options',hook_event_name:'SessionStart',cwd:tmp},{monitors:false}); | 
|  | + const r=spawnSync(process.execPath,[cli,'run','--session','child-options','--',process.execPath,'-e','process.stdout.write(process.cwd())','--','--cwd',os.tmpdir()],{encoding:'utf8'});assert.equal(r.status,0);assert.equal(r.stdout,fs.realpathSync(tmp)); | 
|  | +}); | 
|  | +test.after(async()=>{await sleep(100);fs.rmSync(tmp,{recursive:true,force:true});}); | 
|  | diff --git a/test/queue-transport.test.mjs b/test/queue-transport.test.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..a3e1f04 | 
|  | --- /dev/null | 
|  | +++ b/test/queue-transport.test.mjs | 
|  | @@ -0,0 +1,83 @@ | 
|  | +import test from 'node:test'; | 
|  | +import assert from 'node:assert/strict'; | 
|  | +import fs from 'node:fs'; | 
|  | +import os from 'node:os'; | 
|  | +import path from 'node:path'; | 
|  | +import net from 'node:net'; | 
|  | +import crypto from 'node:crypto'; | 
|  | +import {createCodexQueue} from '../scripts/task-loop.mjs'; | 
|  | + | 
|  | +const frame=(opcode,payload,fin=true)=>{ | 
|  | +  payload=Buffer.from(payload);const size=payload.length;const header=Buffer.alloc(size<126?2:size<=65535?4:10); | 
|  | +  header[0]=(fin?128:0)\|opcode;header[1]=size<126?size:size<=65535?126:127; | 
|  | +  if(size>=126&&size<=65535)header.writeUInt16BE(size,2);else if(size>65535)header.writeBigUInt64BE(BigInt(size),2); | 
|  | +  return Buffer.concat([header,payload]); | 
|  | +}; | 
|  | +async function fixture(t,mode='normal'){ | 
|  | +  const root=fs.mkdtempSync(path.join(os.tmpdir(),'queue-ws-')),socket=path.join(root,'control.sock'),binary=path.join(root,'proxy'); | 
|  | +  // A byte-forwarding proxy, just like the installed CLI. The fixture server is | 
|  | +  // independently responsible for HTTP Upgrade and RFC6455 wire framing. | 
|  | +  fs.writeFileSync(binary,`#!/usr/bin/env node\nconst net=require('node:net');const s=net.connect(process.argv[process.argv.indexOf('--sock')+1]);process.stdin.pipe(s);s.pipe(process.stdout);s.on('error',()=>process.exit(1));s.on('close',()=>process.exit());\n`,{mode:0o700}); | 
|  | +  const messages=[],pongs=[],connections=new Set();let wireError; | 
|  | +  const server=net.createServer(s=>{ | 
|  | +    connections.add(s);s.on('close',()=>connections.delete(s));s.on('error',()=>{}); | 
|  | +    let buffer=Buffer.alloc(0),upgraded=false; | 
|  | +    s.on('data',chunk=>{try{ | 
|  | +      buffer=Buffer.concat([buffer,chunk]); | 
|  | +      if(!upgraded){const end=buffer.indexOf('\r\n\r\n');if(end<0)return;const request=buffer.subarray(0,end).toString();buffer=buffer.subarray(end+4); | 
|  | +        assert.match(request,/^GET \/ HTTP\/1\.1\r\n/);assert.match(request,/Upgrade: websocket/i); | 
|  | +        const key=request.match(/Sec-WebSocket-Key: ([^\r]+)/i)?.[1];assert.ok(key); | 
|  | +        if(mode==='timeout')return; | 
|  | +        if(mode==='bad-handshake'){s.write('HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: secret-invalid\r\n\r\n');return;} | 
|  | +        if(mode==='header-overflow'){s.write('HTTP/1.1 101 '+ 'x'.repeat(9000));return;} | 
|  | +        const accept=crypto.createHash('sha1').update(key+'258EAFA5-E914-47DA-95CA-C5AB0DC85B11').digest('base64'); | 
|  | +        const response=Buffer.from(`HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: keep-alive, Upgrade\r\nSec-WebSocket-Accept: ${accept}\r\n\r\n`); | 
|  | +        s.write(response.subarray(0,17));s.write(response.subarray(17));upgraded=true; | 
|  | +        if(mode==='premature'){s.write(frame(1,JSON.stringify({id:2,result:{}})));return;} | 
|  | +      } | 
|  | +      while(buffer.length>=2){assert.ok(buffer[1]&128,'client frames must be masked');const opcode=buffer[0]&15;let length=buffer[1]&127,offset=2; | 
|  | +        if(length===126){if(buffer.length<4)return;length=buffer.readUInt16BE(2);offset=4;}else if(length===127){if(buffer.length<10)return;length=Number(buffer.readBigUInt64BE(2));offset=10;} | 
|  | +        if(buffer.length<offset+4+length)return;const mask=buffer.subarray(offset,offset+4),payload=Buffer.from(buffer.subarray(offset+4,offset+4+length));buffer=buffer.subarray(offset+4+length); | 
|  | +        for(let i=0;i<payload.length;i++)payload[i]^=mask[i%4]; | 
|  | +        if(opcode===10){pongs.push(payload.toString());continue;}if(opcode===8){s.end(frame(8,payload));continue;} | 
|  | +        assert.equal(opcode,1);const m=JSON.parse(payload.toString());messages.push(m); | 
|  | +        if(m.method==='initialize'){ | 
|  | +          if(mode==='close'){s.write(frame(8,Buffer.from([3,232])));continue;} | 
|  | +          if(mode==='oversize'){s.write(Buffer.from([129,127,0,0,0,0,0,4,0,1]));continue;} | 
|  | +          if(mode==='bad-fragment'){s.write(frame(0,'unexpected'));continue;} | 
|  | +          if(mode==='bad-utf8'){s.write(frame(1,Buffer.from([255])));continue;} | 
|  | +          const ack=JSON.stringify({id:1,result:{platformFamily:'unix'}});s.write(frame(1,ack.slice(0,12),false));s.write(frame(9,'ping'));s.write(frame(0,ack.slice(12))); | 
|  | +          if(mode==='duplicate-init')s.write(frame(1,ack)); | 
|  | +        }else if(m.id===2){ | 
|  | +          assert.ok(messages.some(x=>x.method==='initialized'),'initialization precedes queue request'); | 
|  | +          if(mode==='reject'){s.write(frame(1,JSON.stringify({id:2,error:{message:'secret backend detail'}})));continue;} | 
|  | +          if(mode==='lost-ack'){s.destroy();continue;} | 
|  | +          let result={data:[],nextCursor:null}; | 
|  | +          if(m.method==='thread/queue/add')result={queuedSubmission:{id:'exact-q',clientUserMessageId:mode==='wrong-id'?'other-event':m.params.clientUserMessageId}}; | 
|  | +          if(m.method==='thread/queue/start')result={turn:{id:'exact-turn'}}; | 
|  | +          s.write(frame(1,JSON.stringify({id:99,result:{wrong:true}})));s.write(frame(1,JSON.stringify({id:2,result}))); | 
|  | +        } | 
|  | +      } | 
|  | +    }catch(error){wireError=error;s.destroy();}}); | 
|  | +  }); | 
|  | +  await new Promise(resolve=>server.listen(socket,resolve)); | 
|  | +  t.after(async()=>{for(const s of connections)s.destroy();await new Promise(resolve=>server.close(resolve));fs.rmSync(root,{recursive:true,force:true});if(wireError)throw wireError;}); | 
|  | +  return {queue:createCodexQueue({binary,socket,timeout_ms:1500}),messages,pongs}; | 
|  | +} | 
|  | +test('raw proxy performs Unix WebSocket upgrade, masked RPC, fragmented replies and ping',async t=>{ | 
|  | +  const f=await fixture(t);assert.equal(await f.queue.available({threadId:'parent'}),true); | 
|  | +  const input=[{type:'text',text:'x'.repeat(70000),text_elements:[]}]; | 
|  | +  const ack=await f.queue.add({threadId:'parent',clientUserMessageId:'stable-event',input});assert.equal(ack.queuedSubmissionId,'exact-q'); | 
|  | +  assert.equal((await f.queue.start({threadId:'parent',queuedSubmissionId:ack.queuedSubmissionId})).turn.id,'exact-turn'); | 
|  | +  await f.queue.delete({threadId:'parent',queuedSubmissionId:ack.queuedSubmissionId}); | 
|  | +  assert.deepEqual(f.messages.filter(m=>m.id===2).map(m=>m.method),['thread/queue/list','thread/queue/add','thread/queue/start','thread/queue/delete']); | 
|  | +  assert.deepEqual(f.messages.find(m=>m.method==='thread/queue/add').params,{threadId:'parent',clientUserMessageId:'stable-event',input}); | 
|  | +  assert.ok(f.messages.filter(m=>['thread/queue/start','thread/queue/delete'].includes(m.method)).every(m=>m.params.queuedSubmissionId==='exact-q')); | 
|  | +  assert.ok(f.pongs.length>=4);assert.ok(f.pongs.every(x=>x==='ping')); | 
|  | +}); | 
|  | +const codes={'bad-handshake':'queue_handshake_invalid','header-overflow':'queue_handshake_overflow',timeout:'queue_transport_uncertain',close:'queue_transport_closed',oversize:'queue_response_overflow','bad-fragment':'queue_frame_invalid','bad-utf8':'queue_response_invalid',reject:'queue_request_rejected','lost-ack':'queue_transport_uncertain','duplicate-init':'queue_response_order_invalid',premature:'queue_response_order_invalid'}; | 
|  | +for(const mode of Object.keys(codes))test(`transport fails bounded and sanitized: ${mode}`,async t=>{ | 
|  | +  const f=await fixture(t,mode),at=Date.now();await assert.rejects(f.queue.list({threadId:'parent'}),error=>error.message===codes[mode]);assert.ok(Date.now()-at<3000); | 
|  | +  assert.ok(f.messages.filter(m=>m.id===2).length<=1,'uncertain request is never retried'); | 
|  | +}); | 
|  | +test('acknowledgement must match the exact client event identity',async t=>{const f=await fixture(t,'wrong-id');await assert.rejects(f.queue.add({threadId:'parent',clientUserMessageId:'event',input:[]}),/queue_acknowledgement_invalid/);}); | 
|  | diff --git a/test/report.test.mjs b/test/report.test.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..4cfd7b8 | 
|  | --- /dev/null | 
|  | +++ b/test/report.test.mjs | 
|  | @@ -0,0 +1,63 @@ | 
|  | +import test from 'node:test'; | 
|  | +import assert from 'node:assert/strict'; | 
|  | +import fs from 'node:fs'; | 
|  | +import os from 'node:os'; | 
|  | +import path from 'node:path'; | 
|  | +import {spawnSync} from 'node:child_process'; | 
|  | +const tmp=fs.mkdtempSync(path.join(os.tmpdir(),'harness-report-')); | 
|  | +process.env.CODEX_HOME=path.join(tmp,'codex');process.env.HARNESS_STATE_DIR=path.join(tmp,'state'); | 
|  | +const h=await import('../scripts/harness.mjs'); | 
|  | +test.after(()=>fs.rmSync(tmp,{recursive:true,force:true})); | 
|  | +const now=Date.parse('2026-10-14T09:00:00Z'); | 
|  | +const record=(state,id,extra={})=>h.write(path.join(state,'sessions','session','outcomes',id+'.json'),{ | 
|  | +  at:new Date(now).toISOString(),turn_id:id,stop_action:'none',transcript_path:`/transcripts/${id}.jsonl`,...extra, | 
|  | +}); | 
|  | +test('report counts windowed facts, historical unknowns, and evidence samples',()=>{ | 
|  | +  const state=path.join(tmp,'facts'); | 
|  | +  record(state,'a',{stop_action:'warned',excluded_probe_count:2,execution_review:{ | 
|  | +    unresolved_count:1,failure_count:2,excluded_by_program:{rg:1,test:1},failing_by_program:{node:2}}}); | 
|  | +  record(state,'b',{stop_action:'blocked',excluded_probe_count:1,execution_review:{ | 
|  | +    unresolved_count:2,failure_count:2,excluded_by_program:{rg:1},failing_by_program:{python:2}}}); | 
|  | +  record(state,'old-format',{stop_action:undefined,transcript_path:undefined,execution_review:{failure_count:1,unresolved_count:1}}); | 
|  | +  record(state,'boundary',{at:new Date(now-7*86400000).toISOString()}); | 
|  | +  record(state,'too-old',{at:new Date(now-7*86400000-1).toISOString()}); | 
|  | +  record(state,'future',{at:new Date(now+1).toISOString()}); | 
|  | +  const report=h.collectReport(state,{now}); | 
|  | +  assert.equal(report.turns_total,4); | 
|  | +  assert.equal(report.turns_warned,1);assert.equal(report.turns_blocked,1); | 
|  | +  assert.equal(report.turns_without_stop_action,1); | 
|  | +  assert.equal(report.unresolved_failure_count,4);assert.equal(report.excluded_probe_count,3); | 
|  | +  assert.deepEqual(report.excluded_probes_by_program,{rg:2,test:1}); | 
|  | +  assert.deepEqual(report.top_failing_programs,[{program:'node',count:2},{program:'python',count:2},{program:'unknown',count:1}]); | 
|  | +  assert.equal(report.sample_turns.length,4);assert.ok(report.sample_turns[0].unresolved_count>0); | 
|  | +  const md=h.formatReport(report); | 
|  | +  assert.match(md,/\/transcripts\/a.jsonl/);assert.match(md,/turn_id: a/); | 
|  | +  assert.match(md,/transcript: not recorded/);assert.equal(report.truncated,false); | 
|  | +}); | 
|  | +test('report skips malformed and oversized files and exposes finite limits',()=>{ | 
|  | +  const state=path.join(tmp,'bounded'); | 
|  | +  for(let i=0;i<15;i++)record(state,String(i)); | 
|  | +  const dir=path.join(state,'sessions','session','outcomes'); | 
|  | +  fs.writeFileSync(path.join(dir,'bad.json'),'{'); | 
|  | +  fs.writeFileSync(path.join(dir,'large.json'),'x'.repeat(128*1024+1)); | 
|  | +  fs.writeFileSync(path.join(dir,'date.json'),JSON.stringify({at:'not a date'})); | 
|  | +  const report=h.collectReport(state,{now}); | 
|  | +  assert.equal(report.turns_total,15);assert.equal(report.skipped_files,3); | 
|  | +  assert.equal(report.sample_turns.length,10); | 
|  | +  assert.equal(h.collectReport(state,{now,maxFiles:1}).truncated,true); | 
|  | +  assert.equal(h.collectReport(state,{now,maxBytes:1}).truncated,true); | 
|  | +  assert.equal(h.collectReport(state,{now,deadline:0}).truncated,true); | 
|  | +  assert.equal(h.collectReport(path.join(tmp,'missing'),{now}).turns_total,0); | 
|  | +  assert.throws(()=>h.collectReport(state,{since:NaN})); | 
|  | +}); | 
|  | +test('report CLI is projectless, respects isolated state, and validates flags',()=>{ | 
|  | +  record(h.STATE,'cli',{at:new Date().toISOString()}); | 
|  | +  const cli=path.join(h.ROOT,'scripts/harness.mjs'); | 
|  | +  const json=spawnSync(process.execPath,[cli,'report','--since','7','--format','json'],{encoding:'utf8'}); | 
|  | +  assert.equal(json.status,0,json.stderr);assert.equal(JSON.parse(json.stdout).turns_total,1); | 
|  | +  const md=spawnSync(process.execPath,[cli,'report'],{encoding:'utf8'}); | 
|  | +  assert.equal(md.status,0,md.stderr);assert.match(md.stdout,/turn_id: cli/); | 
|  | +  for(const args of [['--since','invalid'],['--since','-1'],['--format','csv'],['--since'],['--format']]){ | 
|  | +    assert.equal(spawnSync(process.execPath,[cli,'report',...args],{encoding:'utf8'}).status,1); | 
|  | +  } | 
|  | +}); | 
|  | diff --git a/test/runtime.test.mjs b/test/runtime.test.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..301a5de | 
|  | --- /dev/null | 
|  | +++ b/test/runtime.test.mjs | 
|  | @@ -0,0 +1,27 @@ | 
|  | +import test from 'node:test'; | 
|  | +import assert from 'node:assert/strict'; | 
|  | +import fs from 'node:fs'; | 
|  | +import os from 'node:os'; | 
|  | +import path from 'node:path'; | 
|  | +import {spawnSync} from 'node:child_process'; | 
|  | +const tmp=fs.mkdtempSync(path.join(os.tmpdir(),'harness-test-')); | 
|  | +process.env.HARNESS_STATE_DIR=path.join(tmp,'state'); | 
|  | +process.env.CODEX_HOME=path.join(tmp,'codex');fs.mkdirSync(process.env.CODEX_HOME); | 
|  | +const h=await import('../scripts/harness.mjs'); | 
|  | +const make=()=>({schema_version:1,objective:'Preserve navigation',status:'complete',requirements:[{id:'nav',text:'Unified navigation',status:'done',evidence:['test: verified navigation']}],remaining:[]}); | 
|  | +test('full collection cannot be completed using fewer examples',()=>{const c=make();c.requirements[0].count={target:100,actual:52,unit:'examples'};assert.match(h.checkContract(c)[0],/52\/100/);}); | 
|  | +test('unfinished scope and missing agreement evidence cannot pass',()=>{const c=make();c.remaining=['source contents'];c.requirements[0].status='pending';c.requirements[0].evidence=[];assert.equal(h.checkContract(c).length,3);}); | 
|  | +test('explicitly superseded decision is allowed with its basis',()=>{const c=make();c.requirements[0].status='superseded';c.requirements[0].evidence=[];c.requirements[0].decision_basis='User explicitly changed navigation';assert.deepEqual(h.checkContract(c),[]);}); | 
|  | +test('missing return-visit evidence prevents complete but not honest partial reporting',()=>{const c=make();c.requirements[0].verification=[{id:'existing-application-return',result:'unverified',required:true}];assert.equal(h.checkContract(c).length,1);c.status='partial';assert.deepEqual(h.checkContract(c),[]);}); | 
|  | +test('requested deployment requires actual deployed commit and evidence',()=>{const c=make();c.deployment={requested:true,status:'built'};assert.equal(h.checkContract(c).length,1);}); | 
|  | +test('small question without contract is not blocked',async()=>{assert.deepEqual(await h.handleHook({session_id:'question',hook_event_name:'Stop',turn_id:'q',cwd:tmp},{monitors:false}),{});}); | 
|  | +test('contradictory complete status continues only once per turn',async()=>{const c=make();c.remaining=['unfinished'];await h.handleHook({session_id:'bounded',hook_event_name:'UserPromptSubmit',turn_id:'turn',cwd:tmp},{monitors:false});h.setContract('bounded',c);const p={session_id:'bounded',hook_event_name:'Stop',turn_id:'turn',cwd:tmp};assert.equal((await h.handleHook(p,{monitors:false})).decision,'block');assert.equal((await h.handleHook(p,{monitors:false})).decision,undefined);}); | 
|  | +test('host stop_hook_active prevents a continuation loop',async()=>{const c=make();c.remaining=['unfinished'];await h.handleHook({session_id:'active',hook_event_name:'UserPromptSubmit',turn_id:'turn',cwd:tmp},{monitors:false});h.setContract('active',c);assert.equal((await h.handleHook({session_id:'active',hook_event_name:'Stop',turn_id:'turn',cwd:tmp,stop_hook_active:true},{monitors:false})).decision,undefined);}); | 
|  | +test('context advertises API availability without exposing keys',()=>{fs.writeFileSync(path.join(process.env.CODEX_HOME,'langfuse-masked.json'),JSON.stringify({public_key:'TEST_PUBLIC',secret_key:'TEST_SECRET',base_url:'https://example.invalid'}));const c=h.context(tmp);assert.equal(c.connections.langfuse.configured,true);assert.ok(!JSON.stringify(c).includes('TEST_SECRET'));assert.ok(!JSON.stringify(c).includes('TEST_PUBLIC'));}); | 
|  | +test('run uses captured cwd and preserves explicit override',async()=>{await h.handleHook({session_id:'cwd',hook_event_name:'SessionStart',cwd:tmp},{monitors:false});const file=path.join(h.ROOT,'scripts/harness.mjs');const a=spawnSync(process.execPath,[file,'run','--session','cwd','--',process.execPath,'-e','process.stdout.write(process.cwd())'],{encoding:'utf8'});assert.equal(a.status,0);assert.equal(a.stdout,fs.realpathSync(tmp));const b=spawnSync(process.execPath,[file,'run','--session','cwd','--cwd',h.ROOT,'--',process.execPath,'-e','process.stdout.write(process.cwd())'],{encoding:'utf8'});assert.equal(b.stdout,h.ROOT);}); | 
|  | +test('PID identity mismatch never signals a foreign process',()=>{assert.deepEqual(h.stopOwned({pid:process.pid,signature:'different'}),{stopped:false,reason:'not_owned_or_exited'});}); | 
|  | +test('context snapshot is explicitly not a full prompt or active-skill assertion',()=>{assert.equal(h.snapshot(tmp).scope,'configuration_files_at_turn_start_not_full_prompt');}); | 
|  | +test('a stale contradictory contract never blocks an unrelated later turn',async()=>{await h.handleHook({session_id:'stale',hook_event_name:'UserPromptSubmit',turn_id:'old',cwd:tmp},{monitors:false});const c=make();c.remaining=['unfinished'];h.setContract('stale',c);await h.handleHook({session_id:'stale',hook_event_name:'UserPromptSubmit',turn_id:'new',cwd:tmp},{monitors:false});assert.deepEqual(await h.handleHook({session_id:'stale',hook_event_name:'Stop',turn_id:'new',cwd:tmp},{monitors:false}),{});assert.equal(fs.existsSync(path.join(h.sessionDir('stale'),'contracts',h.digest('new')+'.json')),false);}); | 
|  | +test.after(()=>fs.rmSync(tmp,{recursive:true,force:true})); | 
|  | + | 
|  | +test('snapshots omit fixed remote skill routes and skill bodies',async()=>{const r=await h.handleHook({session_id:'remote',hook_event_name:'UserPromptSubmit',turn_id:'remote-turn',cwd:tmp},{monitors:false});const text=r.hookSpecificOutput.additionalContext;assert.ok(!text.includes(path.join(h.ROOT,'skills')));assert.ok(!text.includes('load_skill'));const snap=h.snapshot(tmp);assert.deepEqual(snap.available_skill_files,[]);assert.deepEqual(snap.remote_skill_references,[]);assert.equal(snap.remote_skill_reference_scope,'routing_only_not_load_evidence');}); | 
|  | diff --git a/test/task-lifecycle.test.mjs b/test/task-lifecycle.test.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..df378e5 | 
|  | --- /dev/null | 
|  | +++ b/test/task-lifecycle.test.mjs | 
|  | @@ -0,0 +1,249 @@ | 
|  | +import test from 'node:test'; | 
|  | +import assert from 'node:assert/strict'; | 
|  | +import fs from 'node:fs'; | 
|  | +import os from 'node:os'; | 
|  | +import path from 'node:path'; | 
|  | +import net from 'node:net'; | 
|  | +import {spawn,spawnSync} from 'node:child_process'; | 
|  | +import {createTaskRuntime,probeHerdr} from '../scripts/task-loop.mjs'; | 
|  | +import {createMonitorEvents,digest,ROOT} from '../scripts/harness.mjs'; | 
|  | +import {runVerification} from '../scripts/verification.mjs'; | 
|  | +process.env.HERDR_ENV='1'; | 
|  | +process.env.HERDR_PANE_ID='fixture-caller'; | 
|  | +const root=fs.mkdtempSync(path.join(os.tmpdir(),'harness-lifecycle-')); | 
|  | +let serial=0; | 
|  | +async function fixture({observation=true}={}){ | 
|  | +  const dir=path.join(root,String(++serial));fs.mkdirSync(dir); | 
|  | +  const workerCwd=path.join(dir,'worker');fs.mkdirSync(workerCwd); | 
|  | +  const parent={session_id:'parent',pane_id:'w4:p0',tab_id:'w4:t4',cwd:dir}; | 
|  | +  const worker={session_id:'worker',pane_id:'w4:p1',tab_id:'w4:t4',cwd:workerCwd}; | 
|  | +  const observer={session_id:'observer',pane_id:'w4:p2',tab_id:'w4:t4',cwd:dir}; | 
|  | +  let clock=Date.now();const live=new Map([parent,worker,observer].map(x=>[x.pane_id,{...x,state:'idle'}])),calls=[]; | 
|  | +  const probe=async x=>{const actual=live.get(x.pane_id);return {...actual,state:actual?.state\|\|'missing',identity_verified:Boolean(actual&&x.session_id===actual.session_id&&x.cwd===actual.cwd&&x.tab_id===actual.tab_id),source:'herdr_live',observed_at:new Date(clock).toISOString()};}; | 
|  | +  const deps={state:path.join(dir,'state'),now:()=>clock,probe,owner:{owner:'harness_monitor',backend:'codex_app_server',exclusive:true},queue:{available:async()=>{calls.push('available');return true;},add:async()=>{calls.push('add');return {queuedSubmissionId:'q'};},start:async()=>calls.push('start'),delete:async()=>calls.push('delete')}}; | 
|  | +  const runtime=createTaskRuntime(deps),actor=observation?'OpenClaw-external':'parent',goal={goal_id:'HERDR-LIFECYCLE',tab_id:'w4:t4'}; | 
|  | +  if(observation){await runtime.registerObserver(actor,{schema_version:1,task_id:'task',goal,coordinator:{kind:'external',provider:'openclaw',coordinator_id:actor,cwd:dir}});await runtime.observeAssign(actor,'task',{assignment_id:'a',worker,objective:'bounded review',model_claim:'gpt-6.1-sol/high'});} | 
|  | +  else {await runtime.register(actor,{schema_version:1,task_id:'task',goal,parent,remaining:['integrate'],next_action:'review result',continuation:{enabled:true,owner:'harness_monitor',backend:'codex_app_server',max_wakes:2,max_probes:100,backoff_ms:1000,expires_at:new Date(clock+600000).toISOString()}});await runtime.assign(actor,'task',{assignment_id:'a',worker,objective:'bounded review',model_claim:'gpt-6.1-sol/high'});} | 
|  | +  const expected={assignment_id:'a',expected:worker}; | 
|  | +  async function preserve(retention={mode:'release',reason:'Verified output preserved; no next work',next_work:''}){ | 
|  | +    const artifact=path.join(workerCwd,'result.txt');fs.writeFileSync(artifact,'verified result\n'); | 
|  | +    const contract={schema_version:2,task_id:'task',objective:'verify preserved worker result',status:'in_progress',requirements:[{id:'result',text:'result is preserved',source:'assignment a',checks:['result']}],checks:[{id:'result',cwd:workerCwd,command:[process.execPath,'-e',"const fs=require('fs');console.log(JSON.stringify({valid:fs.readFileSync('result.txt','utf8')==='verified result\\n'}))"],format:'json',inputs:['result.txt'],assertions:[{pointer:'/valid',op:'eq',value:true}]}]}; | 
|  | +    const sessionDir=path.join(deps.state,'sessions',digest('worker'));fs.mkdirSync(sessionDir,{recursive:true});fs.writeFileSync(path.join(sessionDir,'contract.json'),JSON.stringify(contract)); | 
|  | +    assert.equal((await runVerification(contract,sessionDir,'result')).status,'pass'); | 
|  | +    await runtime.preserve(actor,'task',{...expected,retention,artifacts:[{path:artifact,sha256:digest(fs.readFileSync(artifact))}],verification_session_id:'worker'}); | 
|  | +    return artifact; | 
|  | +  } | 
|  | +  return {dir,runtime,deps,actor,parent,worker,observer,live,calls,expected,preserve,advance:n=>{clock+=n;}}; | 
|  | +} | 
|  | +const monitorProof=f=>{const agents=[...f.live.values()].map(x=>({...x,workspace_id:'w4',terminal_id:x.pane_id+'-term',agent_session:{kind:'id',value:x.session_id}}));return [{status:'verified',identity:agents.find(x=>x.session_id==='observer')},agents.map(x=>x.pane_id),{agents},agents];}; | 
|  | +const status=(pane,state)=>({event:'pane.agent_status_changed',data:{pane_id:pane,workspace_id:'w4',agent_status:state}}); | 
|  | +test('external observation is durable, truthful and never adopts business workers or dispatches',async()=>{ | 
|  | +  const f=await fixture();assert.equal(fs.existsSync(path.join(f.deps.state,'sessions',digest('worker'),'task-membership.json')),false); | 
|  | +  await f.runtime.bindObserver(f.actor,'task',f.observer); | 
|  | +  f.live.get(f.worker.pane_id).state='working';await f.runtime.reconcileSession('observer'); | 
|  | +  f.live.get(f.worker.pane_id).state='idle';await f.runtime.reconcile('task'); | 
|  | +  assert.deepEqual(f.calls,[]);assert.equal(f.runtime.get('task').probes,0);assert.deepEqual(f.runtime.get('task').handoffs,[]); | 
|  | +  assert.deepEqual(await f.runtime.stop('worker','turn'),{issues:[],snapshot:null});assert.deepEqual(await f.runtime.stop('observer','turn'),{issues:[],snapshot:null}); | 
|  | +  await f.runtime.interrupt('worker');await f.runtime.interrupt('observer');assert.equal(f.runtime.get('task').status,'active'); | 
|  | +  const restarted=createTaskRuntime(f.deps);assert.deepEqual(restarted.monitorBinding('observer').identities,[f.worker]);assert.equal(restarted.get('task').lifecycle.coordinator.coordinator_id,f.actor);assert.equal(restarted.get('task').parent.session_id,undefined); | 
|  | +  assert.equal(restarted.guard(f.actor,'[HARNESS_CONTINUATION task 1 '+ 'a'.repeat(64)+']'),false); | 
|  | +  assert.equal(restarted.list()[0].monitor_coverage.status,'partial','registration alone is not monitored coverage'); | 
|  | +}); | 
|  | +test('cross-cwd monitor subscribes verified goal participants and exposes retained unknown panes',async()=>{ | 
|  | +  const f=await fixture(),requests=[],events=[];await f.runtime.bindObserver(f.actor,'task',f.observer); | 
|  | +  await f.runtime.retainPane(f.actor,'task',{pane_id:'w6:p1',reason:'Business session UUID unavailable; explicitly retained'}); | 
|  | +  const c=createMonitorEvents({id:'observer',cwd:f.dir,pane:f.observer.pane_id,tasks:f.runtime,send:x=>requests.push(x),onEvent:x=>events.push(x)}); | 
|  | +  c.setIdentity(...monitorProof(f));c.refresh();assert.equal(c.coverage().status,'partial');c.receive({id:requests[0].id,result:{type:'subscription_started'}});await c.settled(); | 
|  | +  const subscribed=requests[0].params.subscriptions.filter(x=>x.type==='pane.agent_status_changed').map(x=>x.pane_id); | 
|  | +  assert.deepEqual(subscribed,[f.worker.pane_id,f.observer.pane_id]);assert.equal(c.coverage().unbound_panes[0].pane_id,'w6:p1');assert.equal(c.coverage().status,'partial'); | 
|  | +  f.live.get(f.worker.pane_id).state='working';c.receive(status(f.worker.pane_id,'working'));await c.settled();assert.equal(f.runtime.get('task').observations.at(-1).state,'working');assert.equal(events.length,1); | 
|  | +  c.receive(status('w6:p1','idle'));await c.settled();assert.equal(events.length,1,'retained unbound pane is not falsely linked'); | 
|  | +  f.live.get(f.worker.pane_id).session_id='replacement';c.receive(status(f.worker.pane_id,'idle'));await c.settled();assert.equal(c.coverage().unknown_identities[0].session_id,'worker'); | 
|  | +  c.disconnected();assert.equal(c.coverage().subscription_acknowledged,false);c.close();assert.deepEqual(f.calls,[]); | 
|  | +}); | 
|  | +test('idle alone, retained work, absent verification, changed evidence and busy states refuse retirement',async()=>{ | 
|  | +  const f=await fixture();assert.equal((await f.runtime.retirement(f.actor,'task',f.expected)).eligible,false); | 
|  | +  await assert.rejects(f.runtime.retire(f.actor,'task',f.expected),/Retirement refused/); | 
|  | +  const artifact=await f.preserve({mode:'retain',reason:'Waiting for integration',next_work:'Review merged result'}); | 
|  | +  let eligibility=await f.runtime.retirement(f.actor,'task',f.expected);assert.ok(eligibility.reasons.includes('retention_not_released')); | 
|  | +  await f.runtime.preserve(f.actor,'task',{...f.expected,retention:{mode:'release',reason:'Released',next_work:'Follow-up still needed'}});assert.equal((await f.runtime.retirement(f.actor,'task',f.expected)).eligible,false); | 
|  | +  await f.runtime.preserve(f.actor,'task',{...f.expected,retention:{mode:'release',reason:'Released',next_work:''}}); | 
|  | +  for(const state of ['working','blocked','unknown','missing']){f.live.get(f.worker.pane_id).state=state;assert.equal((await f.runtime.retirement(f.actor,'task',f.expected)).eligible,false);} | 
|  | +  f.live.get(f.worker.pane_id).state='idle';assert.equal((await f.runtime.retirement(f.actor,'task',f.expected)).eligible,true); | 
|  | +  fs.writeFileSync(artifact,'changed');eligibility=await f.runtime.retirement(f.actor,'task',f.expected);assert.ok(eligibility.reasons.includes('preserved_result_unverified_or_changed')); | 
|  | +  assert.equal(f.runtime.get('task').assignments.a.lifecycle.status,'active');assert.deepEqual(f.calls,[]); | 
|  | +}); | 
|  | +test('exact fresh session/cwd/tab/pane identity is required; close/replacement/move never retires',async()=>{ | 
|  | +  for(const change of ['closed','session','cwd','tab','pane','stale']){ | 
|  | +    const f=await fixture();await f.preserve();const live=f.live.get(f.worker.pane_id); | 
|  | +    if(change==='closed')f.live.delete(f.worker.pane_id); | 
|  | +    if(change==='session')live.session_id='replacement'; | 
|  | +    if(change==='cwd')live.cwd=f.dir; | 
|  | +    if(change==='tab')live.tab_id='w4:t-other'; | 
|  | +    if(change==='pane')live.pane_id='w4:other'; | 
|  | +    if(change==='stale')f.deps.probe=async x=>({...x,state:'idle',identity_verified:true,source:'herdr_live',observed_at:new Date(Date.now()-60000).toISOString()}); | 
|  | +    const runtime=createTaskRuntime(f.deps);const e=await runtime.retirement(f.actor,'task',f.expected);assert.equal(e.eligible,false,change);assert.ok(e.reasons.includes('identity_unverified_or_stale'),change); | 
|  | +  } | 
|  | +}); | 
|  | +test('verified result release retires record and routing, preserves artifacts and survives restart',async()=>{ | 
|  | +  const f=await fixture();const artifact=await f.preserve();await f.runtime.bindObserver(f.actor,'task',f.observer); | 
|  | +  const result=await f.runtime.retire(f.actor,'task',f.expected);assert.equal(result.eligible,true);assert.equal(result.action,'record_only_no_pane_close'); | 
|  | +  const restarted=createTaskRuntime(f.deps);assert.equal(restarted.get('task').assignments.a.lifecycle.status,'retired');assert.deepEqual(restarted.monitorBinding('observer').identities,[]); | 
|  | +  assert.equal(fs.readFileSync(artifact,'utf8'),'verified result\n');assert.equal(f.live.has(f.worker.pane_id),true,'runtime never closes panes'); | 
|  | +  await assert.rejects(restarted.handoff(f.actor,'task',{...f.expected,reason:'stale replay',successor:{}}),/stale or retired/);await assert.rejects(restarted.observeAssign(f.actor,'task',{assignment_id:'a',worker:f.worker,objective:'reuse',model_claim:'fixture'}),/duplicate/); | 
|  | +}); | 
|  | +test('handoff replaces stale owner routing, allows same replaced pane, and invalidates pending continuation',async()=>{ | 
|  | +  const f=await fixture({observation:false});f.live.get(f.worker.pane_id).state='working';await f.runtime.reconcile('task'); | 
|  | +  f.live.get(f.parent.pane_id).state='working';f.live.get(f.worker.pane_id).state='idle';await f.runtime.reconcile('task');assert.equal(f.runtime.get('task').handoffs[0].dispatch_status,'pending'); | 
|  | +  const successor={...f.worker,session_id:'successor'};f.live.set(successor.pane_id,{...successor,state:'idle'}); | 
|  | +  const input={...f.expected,reason:'Original owner replaced; follow-up belongs to successor',successor:{assignment_id:'b',worker:successor,objective:'remaining review',model_claim:'gpt-6.1-sol/high'}}; | 
|  | +  await f.runtime.handoff(f.actor,'task',input);const task=f.runtime.get('task');assert.equal(task.assignments.a.lifecycle.status,'superseded');assert.equal(task.handoffs[0].dispatch_status,'retired'); | 
|  | +  assert.equal(f.runtime.monitorBinding('worker'),null);assert.deepEqual(f.runtime.monitorBinding('successor').identities,[f.parent,successor]); | 
|  | +  f.live.get(f.parent.pane_id).state='idle';f.advance(1001);await createTaskRuntime(f.deps).reconcile('task');assert.deepEqual(f.calls,[]); | 
|  | +  await assert.rejects(f.runtime.handoff(f.actor,'task',input),/stale or retired/);await assert.rejects(f.runtime.report('worker','task','turn',{claims:[]}),/identity\/authority mismatch/); | 
|  | +}); | 
|  | +test('handoff revokes an acknowledged continuation token and only deletes its exact queue item',async()=>{ | 
|  | +  const f=await fixture({observation:false});f.live.get(f.worker.pane_id).state='working';await f.runtime.reconcile('task');f.live.get(f.worker.pane_id).state='idle';await f.runtime.reconcile('task'); | 
|  | +  const h=f.runtime.get('task').handoffs[0],token=`[HARNESS_CONTINUATION task 1 ${h.event_id}]`; | 
|  | +  assert.equal((await f.runtime.continuation('parent',token,'turn')).authorized,true);assert.equal(f.runtime.guard('parent',token),true); | 
|  | +  const successor={...f.worker,session_id:'next'};f.live.set(successor.pane_id,{...successor,state:'idle'}); | 
|  | +  await f.runtime.handoff('parent','task',{...f.expected,reason:'Owner handoff',successor:{assignment_id:'b',worker:successor,objective:'follow-up',model_claim:'fixture'}}); | 
|  | +  assert.equal(f.runtime.guard('parent',token),false);assert.equal(f.calls.filter(x=>x==='delete').length,1); | 
|  | +  assert.equal((await f.runtime.continuation('parent',token,'late')).authorized,false); | 
|  | +}); | 
|  | +test('external observation preserves existing business task membership and refuses monitor takeover',async()=>{ | 
|  | +  const f=await fixture({observation:false}),before=fs.readFileSync(path.join(f.deps.state,'sessions',digest('worker'),'task-membership.json'),'utf8'); | 
|  | +  const actor='OpenClaw';await f.runtime.registerObserver(actor,{schema_version:1,task_id:'observe-business',goal:{goal_id:'business',tab_id:'w4:t4'},coordinator:{kind:'external',provider:'openclaw',coordinator_id:actor,cwd:f.dir}}); | 
|  | +  await f.runtime.observeAssign(actor,'observe-business',{assignment_id:'observed',worker:f.worker,objective:'observe only',model_claim:'unknown'}); | 
|  | +  await assert.rejects(f.runtime.assign(actor,'observe-business',{assignment_id:'ordinary',worker:{...f.worker,session_id:'unrelated'},objective:'must not adopt',model_claim:'unknown'}),/require observe-assign/); | 
|  | +  assert.equal(fs.readFileSync(path.join(f.deps.state,'sessions',digest('worker'),'task-membership.json'),'utf8'),before); | 
|  | +  await assert.rejects(f.runtime.bindObserver(actor,'observe-business',f.parent),/replace task ownership/); | 
|  | +  await f.runtime.interrupt('worker');assert.equal(f.runtime.get('task').status,'active');assert.equal(f.runtime.get('observe-business').status,'active'); | 
|  | +  assert.equal(f.runtime.monitorBinding('worker').task_id,'task'); | 
|  | +}); | 
|  | +test('observer attached to a continuation task does not pump dispatch or require reports',async()=>{ | 
|  | +  const f=await fixture({observation:false});await f.runtime.bindObserver('parent','task',f.observer); | 
|  | +  f.live.get(f.worker.pane_id).state='working';await f.runtime.reconcileSession('observer');f.live.get(f.worker.pane_id).state='idle';await f.runtime.reconcileSession('observer'); | 
|  | +  assert.equal(f.runtime.get('task').probes,0);assert.deepEqual(f.calls,[]);assert.deepEqual(await f.runtime.stop('observer','turn'),{issues:[],snapshot:null});await f.runtime.interrupt('observer');assert.equal(f.runtime.get('task').status,'active'); | 
|  | +}); | 
|  | +test('observation CLI has independent coordinator namespace; list/status expose unbound coverage JSON',async()=>{ | 
|  | +  const f=await fixture(),file=path.join(f.dir,'unbound.json');fs.writeFileSync(file,JSON.stringify({pane_id:'w4:business',reason:'No session UUID; retain'})); | 
|  | +  const env={...process.env,HARNESS_STATE_DIR:f.deps.state,CODEX_THREAD_ID:'unrelated-real-codex'}; | 
|  | +  const cli=(...args)=>spawnSync(process.execPath,[path.join(ROOT,'scripts/harness.mjs'),'task',...args],{env,encoding:'utf8'}); | 
|  | +  const retained=cli('retain-pane','--coordinator',f.actor,'--task','task','--file',file);assert.equal(retained.status,0,retained.stderr);assert.equal(JSON.parse(retained.stdout).identity_status,'unbound'); | 
|  | +  const listed=cli('list');assert.equal(listed.status,0);assert.equal(JSON.parse(listed.stdout)[0].monitor_coverage.unbound_panes[0].pane_id,'w4:business'); | 
|  | +  assert.equal(JSON.parse(cli('status','--task','task').stdout).continuation.enabled,false); | 
|  | +  assert.notEqual(cli('retain-pane','--coordinator','wrong','--task','task','--file',file).status,0); | 
|  | +  assert.notEqual(cli('retain-pane','--coordinator',f.actor,'--session','spoof','--task','task','--file',file).status,0); | 
|  | +}); | 
|  | +test('live Herdr probe rejects missing session UUID and closed/replaced/moved pane tuples',async()=>{ | 
|  | +  const dir=path.join(root,'probe'),bin=path.join(dir,'bin');fs.mkdirSync(bin,{recursive:true});const file=path.join(dir,'agent.json'); | 
|  | +  fs.writeFileSync(path.join(bin,'herdr'),`#!/usr/bin/env node\nconst fs=require('fs');console.log(fs.readFileSync(${JSON.stringify(file)},'utf8'));\n`,{mode:0o700}); | 
|  | +  const oldSocket=process.env.HERDR_SOCKET_PATH;process.env.HERDR_SOCKET_PATH=path.join(dir,'fixture.sock'); | 
|  | +  const previous=process.env.PATH;process.env.PATH=bin+path.delimiter+previous; | 
|  | +  try{ | 
|  | +    const expected={session_id:'uuid',pane_id:'w4:p1',tab_id:'w4:t4',cwd:dir}; | 
|  | +    const agent={agent:'codex',agent_session:{kind:'id',value:'uuid'},pane_id:'w4:p1',tab_id:'w4:t4',cwd:dir,foreground_cwd:dir,agent_status:'idle'}; | 
|  | +    for(const change of [{},{agent_session:null},{agent_session:{kind:'id',value:'different'}},{pane_id:'w4:p2'},{tab_id:'w4:other'},{cwd:root},{foreground_cwd:root}]){ | 
|  | +      fs.writeFileSync(file,JSON.stringify({result:{agent:{...agent,...change}}}));assert.equal((await probeHerdr(expected)).identity_verified,Object.keys(change).length===0); | 
|  | +    } | 
|  | +    fs.writeFileSync(file,JSON.stringify({result:{agent:null}}));assert.equal((await probeHerdr(expected)).identity_verified,false); | 
|  | +  }finally{process.env.PATH=previous;if(oldSocket===undefined)delete process.env.HERDR_SOCKET_PATH;else process.env.HERDR_SOCKET_PATH=oldSocket;} | 
|  | +}); | 
|  | +test('a moved worker must retain its terminal, complete agent session and socket',async t=>{ | 
|  | +  const dir=fs.mkdtempSync(path.join(root,'move-')),socket=path.join(dir,'herdr.sock'); | 
|  | +  const file=path.join(dir,'live.json'),cli=path.join(dir,'herdr'); | 
|  | +  const session={source:'herdr:codex-client',agent:'codex',kind:'id',value:'worker'}; | 
|  | +  const live={agent:'codex',terminal_id:'terminal',agent_session:session,workspace_id:'new',tab_id:'new:tab',pane_id:'new:p',cwd:dir,foreground_cwd:dir,agent_status:'working'}; | 
|  | +  fs.writeFileSync(file,JSON.stringify(live)); | 
|  | +  fs.writeFileSync(cli,`#!/usr/bin/env node | 
|  | +const a=JSON.parse(require('fs').readFileSync(${JSON.stringify(file)}));console.log(JSON.stringify({result:{agent:process.argv[4]===a.pane_id?a:null}})); | 
|  | +`,{mode:0o700}); | 
|  | +  const server=net.createServer(peer=>peer.once('data',data=>{ | 
|  | +    const q=JSON.parse(data);peer.end(JSON.stringify({id:q.id,result:{snapshot:{agents:[JSON.parse(fs.readFileSync(file))]}}})+'\n'); | 
|  | +  })); | 
|  | +  await new Promise(r=>server.listen(socket,r)); | 
|  | +  const vars={HERDR_SOCKET_PATH:socket,HERDR_ENV:'1',HERDR_PANE_ID:'caller',PATH:dir+path.delimiter+process.env.PATH}; | 
|  | +  const original=Object.fromEntries(Object.keys(vars).map(k=>[k,process.env[k]])); | 
|  | +  Object.assign(process.env,vars); | 
|  | +  t.after(async()=>{for(const [k,v] of Object.entries(original)){if(v===undefined)delete process.env[k];else process.env[k]=v;}await new Promise(r=>server.close(r));}); | 
|  | +  const old={session_id:'worker',terminal_id:'terminal',agent_session:session,socket,pane_id:'old:p',tab_id:'old:t',cwd:dir}; | 
|  | +  const recovered=await probeHerdr(old); | 
|  | +  assert.equal(recovered.identity_verified,true);assert.equal(recovered.pane_id,live.pane_id); | 
|  | +  for(const changed of [{terminal_id:'other'},{agent_session:{...session,source:'other'}},{agent_session:{...session,agent:'other'}}]){ | 
|  | +    fs.writeFileSync(file,JSON.stringify({...live,...changed}));assert.equal((await probeHerdr(old)).identity_verified,false); | 
|  | +  } | 
|  | +  assert.equal((await probeHerdr({...old,socket:'other'})).identity_verified,false); | 
|  | +}); | 
|  | +test('external hook cannot launch a monitor against an explicit socket',()=>{ | 
|  | +  const result=spawnSync(process.execPath,[path.join(ROOT,'scripts/harness.mjs'),'monitor','external',root,path.join(root,'foreign.sock')],{ | 
|  | +    env:{...process.env,HERDR_ENV:'',HERDR_PANE_ID:''},encoding:'utf8',timeout:2000 | 
|  | +  }); | 
|  | +  assert.equal(result.status,1); | 
|  | +  assert.match(result.stderr,/inherited Herdr pane and socket/); | 
|  | +}); | 
|  | + | 
|  | +test('hook monitor binding rejects fabricated, dead, wrong-owner and pane-bearing identities',async()=>{ | 
|  | +  const f=await fixture(),session='hook',dir=path.join(f.deps.state,'sessions',digest(session));fs.mkdirSync(dir,{recursive:true}); | 
|  | +  const signature=digest(spawnSync('ps',['-p',String(process.pid),'-o','lstart=','-o','command='],{encoding:'utf8'}).stdout.trim()); | 
|  | +  fs.writeFileSync(path.join(dir,'session.json'),JSON.stringify({session_id:session,cwd:f.dir})); | 
|  | +  const monitor={pid:process.pid,signature,owner:session,cwd:f.dir,runtime:ROOT,lifecycle:{state:'subscribed'}}; | 
|  | +  for(const change of [{owner:'wrong'},{signature:'wrong'},{pid:99999999},{cwd:root},{lifecycle:{state:'disconnected'}}]){ | 
|  | +    fs.writeFileSync(path.join(dir,'monitor.json'),JSON.stringify({...monitor,...change}));await assert.rejects(f.runtime.bindObserver(f.actor,'task',{kind:'hook_monitor',session_id:session,cwd:f.dir}),/ownership evidence/); | 
|  | +  } | 
|  | +  await assert.rejects(f.runtime.bindObserver(f.actor,'task',{kind:'hook_monitor',session_id:session,cwd:f.dir,pane_id:'invented'}),/no pane identity/); | 
|  | +}); | 
|  | +test('retained unbound panes resolve to freshly verified assignments or explicit record release; closure refuses unresolved work',async()=>{ | 
|  | +  const f=await fixture();await f.runtime.retainPane(f.actor,'task',{pane_id:'w6:p1',reason:'No UUID, retain business work'}); | 
|  | +  await assert.rejects(f.runtime.endObservation(f.actor,'task',{reason:'Premature'}),/no active assignments/); | 
|  | +  await assert.rejects(f.runtime.resolvePane(f.actor,'task',{pane_id:'w6:p1',resolution:'registered',assignment_id:'a',reason:'Wrong pane'}),/active exact assignment/); | 
|  | +  await f.runtime.resolvePane(f.actor,'task',{pane_id:'w6:p1',resolution:'released',reason:'Owner confirms record may be removed; pane untouched'});assert.deepEqual(f.runtime.get('task').lifecycle.unbound_panes,{}); | 
|  | +  await f.runtime.retainPane(f.actor,'task',{pane_id:f.worker.pane_id,reason:'Earlier unknown session'}); | 
|  | +  f.live.get(f.worker.pane_id).session_id='wrong';await assert.rejects(f.runtime.resolvePane(f.actor,'task',{pane_id:f.worker.pane_id,resolution:'registered',assignment_id:'a',reason:'Now known'}),/not freshly verified/); | 
|  | +  f.live.get(f.worker.pane_id).session_id=f.worker.session_id;await f.runtime.resolvePane(f.actor,'task',{pane_id:f.worker.pane_id,resolution:'registered',assignment_id:'a',reason:'Exact worker now verified'}); | 
|  | +  assert.deepEqual(f.runtime.get('task').lifecycle.unbound_panes,{});assert.equal(f.runtime.get('task').lifecycle.events.filter(e=>e.type==='retained_pane_resolved').length,2); | 
|  | +  await f.preserve();await f.runtime.retire(f.actor,'task',f.expected);await f.runtime.endObservation(f.actor,'task',{reason:'No current assignments or unresolved panes'}); | 
|  | +  assert.equal(f.runtime.get('task').status,'closed');assert.equal(f.live.has(f.worker.pane_id),true); | 
|  | +}); | 
|  | +test('coverage and status/CLI only mark current acknowledged subscription identities monitored, including in-flight registration and tab closure',async()=>{ | 
|  | +  const f=await fixture();await f.runtime.bindObserver(f.actor,'task',f.observer);const requests=[],persisted=[]; | 
|  | +  const dir=path.join(f.deps.state,'sessions',digest('observer')),monitorFile=path.join(dir,'monitor.json'); | 
|  | +  const signature=digest(spawnSync('ps',['-p',String(process.pid),'-o','lstart=','-o','command='],{encoding:'utf8'}).stdout.trim()); | 
|  | +  const c=createMonitorEvents({id:'observer',cwd:f.dir,pane:f.observer.pane_id,tasks:f.runtime,send:x=>requests.push(x),onEvent:()=>{},onCoverage:coverage=>{persisted.push(coverage);fs.writeFileSync(monitorFile,JSON.stringify({pid:process.pid,signature,owner:'observer',cwd:f.dir,runtime:ROOT,lifecycle:{state:'subscribed'},coverage}));}}); | 
|  | +  c.setIdentity(...monitorProof(f));c.refresh();c.receive({id:requests[0].id,result:{type:'subscription_started'}});await c.settled();assert.equal(c.coverage().status,'complete'); | 
|  | +  const next={...f.worker,session_id:'next',pane_id:'w4:p3'};f.live.set(next.pane_id,{...next,state:'idle'}); | 
|  | +  // Hold a reconciliation across registration to exercise persistence before | 
|  | +  // the file watch refreshes/rotates the acknowledged old stream. | 
|  | +  let release;const original=f.runtime.reconcileSession;f.runtime.reconcileSession=async(...args)=>{await new Promise(r=>release=r);return original(...args);}; | 
|  | +  c.receive(status(f.worker.pane_id,'working')); | 
|  | +  await f.runtime.observeAssign(f.actor,'task',{assignment_id:'b',worker:next,objective:'new worker',model_claim:'fixture'});release();await c.settled();f.runtime.reconcileSession=original; | 
|  | +  assert.equal(c.coverage().status,'partial');assert.deepEqual(c.coverage().covered_identities.map(x=>x.pane_id),[f.worker.pane_id]); | 
|  | +  assert.equal(f.runtime.status('task').monitor_coverage.identities.find(x=>x.session_id==='next').monitoring,'registered'); | 
|  | +  const cli=()=>spawnSync(process.execPath,[path.join(ROOT,'scripts/harness.mjs'),'task','status','--task','task'],{env:{...process.env,HARNESS_STATE_DIR:f.deps.state},encoding:'utf8'}); | 
|  | +  assert.equal(JSON.parse(cli().stdout).monitor_coverage.status,'partial'); | 
|  | +  c.setIdentity(...monitorProof(f));c.refresh();c.receive({id:requests[0].id,result:{type:'subscription_started'}});assert.equal(c.coverage().status,'partial','stale generation ack is ignored'); | 
|  | +  c.receive({id:requests.at(-1).id,result:{type:'subscription_started'}});await c.settled();assert.equal(c.coverage().status,'complete'); | 
|  | +  f.live.delete(f.worker.pane_id);f.live.delete(next.pane_id);c.receive({event:'tab_closed',data:{type:'tab_closed',tab_id:'w4:t4'}}); | 
|  | +  assert.equal(c.coverage().status,'partial','tab close invalidates immediately');assert.deepEqual(c.coverage().covered_identities,[]);assert.equal(JSON.parse(cli().stdout).monitor_coverage.status,'partial'); | 
|  | +  await c.settled();assert.equal(c.coverage().status,'partial');assert.equal(f.runtime.status('task').monitor_coverage.identities[0].monitoring,'unknown');assert.deepEqual(f.calls,[]); | 
|  | +  const record=JSON.parse(fs.readFileSync(monitorFile));delete record.coverage.covered_identities;fs.writeFileSync(monitorFile,JSON.stringify(record));assert.equal(JSON.parse(cli().stdout).monitor_coverage.status,'partial','older records without acknowledged coverage fail closed');c.close(); | 
|  | +}); | 
|  | +test('handoff matches historical identity after old worktree removal and requires a fresh existing successor',async()=>{ | 
|  | +  const f=await fixture();fs.rmdirSync(f.worker.cwd);f.live.delete(f.worker.pane_id); | 
|  | +  const successor={...f.worker,session_id:'next',pane_id:'w4:p3',cwd:f.dir};f.live.set(successor.pane_id,{...successor,state:'idle'}); | 
|  | +  await f.runtime.handoff(f.actor,'task',{...f.expected,reason:'Old worktree removed; ownership moves to verified successor',successor:{assignment_id:'b',worker:successor,objective:'finish handoff',model_claim:'fixture'}}); | 
|  | +  assert.equal(f.runtime.get('task').assignments.a.lifecycle.status,'superseded');assert.equal(f.runtime.get('task').assignments.b.worker.cwd,f.dir); | 
|  | +  await assert.rejects(f.runtime.handoff(f.actor,'task',{...f.expected,reason:'stale retry',successor:{}}),/stale or retired/);assert.deepEqual(f.calls,[]); | 
|  | +}); | 
|  | +test('retired assignment history is preserved without exhausting active worker slots',async()=>{ | 
|  | +  const f=await fixture();await f.preserve();await f.runtime.retire(f.actor,'task',f.expected); | 
|  | +  // Persist a long-lived goal fixture with sixteen retired assignments. The | 
|  | +  // first is retired through the public lifecycle; the rest model old history. | 
|  | +  const file=path.join(f.deps.state,'tasks',digest('task')+'.json'),task=f.runtime.get('task'); | 
|  | +  for(let n=1;n<16;n++)task.assignments['history-'+n]={...task.assignments.a,assignment_id:'history-'+n,worker:{...f.worker,session_id:'old-'+n,pane_id:'w4:old-'+n}}; | 
|  | +  fs.writeFileSync(file,JSON.stringify(task)); | 
|  | +  const next={...f.worker,session_id:'new-session',pane_id:'w4:new'};f.live.set(next.pane_id,{...next,state:'working'}); | 
|  | +  await createTaskRuntime(f.deps).observeAssign(f.actor,'task',{assignment_id:'new-work',worker:next,objective:'Independent work in a new session',model_claim:'fixture'}); | 
|  | +  const result=f.runtime.get('task');assert.equal(Object.keys(result.assignments).length,17);assert.equal(Object.values(result.assignments).filter(a=>a.lifecycle.status==='retired').length,16);assert.equal(result.assignments['new-work'].worker.session_id,'new-session'); | 
|  | +}); | 
|  | +test.after(()=>fs.rmSync(root,{recursive:true,force:true})); | 
|  | diff --git a/test/task-loop.test.mjs b/test/task-loop.test.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..17f8817 | 
|  | --- /dev/null | 
|  | +++ b/test/task-loop.test.mjs | 
|  | @@ -0,0 +1,230 @@ | 
|  | +import test from 'node:test'; | 
|  | +import assert from 'node:assert/strict'; | 
|  | +import fs from 'node:fs'; | 
|  | +import os from 'node:os'; | 
|  | +import path from 'node:path'; | 
|  | +import net from 'node:net'; | 
|  | +import {spawn,spawnSync} from 'node:child_process'; | 
|  | +import {runVerification} from '../scripts/verification.mjs'; | 
|  | +process.env.HERDR_ENV='1'; | 
|  | +process.env.HERDR_PANE_ID='p1'; | 
|  | +const tmp=fs.mkdtempSync(path.join(os.tmpdir(),'harness-loop-')); | 
|  | +process.env.HARNESS_STATE_DIR=path.join(tmp,'state'); | 
|  | +process.env.CODEX_HOME=path.join(tmp,'codex'); | 
|  | +const h=await import('../scripts/harness.mjs'); | 
|  | +const parent={session_id:'parent',pane_id:'p1',cwd:tmp}; | 
|  | +const worker={session_id:'worker',pane_id:'p2',cwd:tmp}; | 
|  | +let serial=0; | 
|  | +async function fixture(options={}){ | 
|  | +  const state=path.join(tmp,String(++serial));let clock=Date.now();const states={parent:'idle',worker:options.initial_worker_state??'working'};const sent=[],started=[],deleted=[]; | 
|  | +  const probe=async identity=>({...identity,state:states[identity.session_id]\|\|'missing',identity_verified:true,observed_at:new Date(clock).toISOString(),source:'herdr_live'}); | 
|  | +  const queue={available:async()=>true,add:async item=>{sent.push(item);return {queuedSubmissionId:'q1'};},start:async item=>{started.push(item);return {};},delete:async item=>{deleted.push(item);return {};},...options.queue}; | 
|  | +  const deps={state,probe,queue:options.missing?null:queue,now:()=>clock,owner:{owner:'harness_monitor',backend:'codex_app_server',exclusive:true},...options,queue:options.missing?null:queue}; | 
|  | +  const runtime=h.createTaskRuntime(deps); | 
|  | +  await runtime.register('parent',{schema_version:1,task_id:'task',parent,remaining:['integrate'],next_action:'review worker result',continuation:{enabled:options.enabled??true,owner:'harness_monitor',backend:'codex_app_server',max_wakes:options.max_wakes??2,max_probes:30,expires_at:new Date(clock+600000).toISOString(),backoff_ms:1000}}); | 
|  | +  await runtime.assign('parent','task',{assignment_id:'a1',worker:options.worker??worker,objective:'bounded check',model_claim:'gpt-6.1-sol/high'}); | 
|  | +  return {runtime,deps,states,sent,started,deleted,advance:n=>{clock+=n;},clock:()=>clock}; | 
|  | +} | 
|  | +const finish=async f=>{f.states.worker='idle';await f.runtime.reconcile('task');}; | 
|  | +test('staleidleclaim: idle and stale observations cannot verify running claims',async()=>{ | 
|  | +  const f=await fixture();f.states.worker='idle'; | 
|  | +  await f.runtime.report('parent','task','turn',{claims:[{kind:'running',assignment_id:'a1'}]}); | 
|  | +  const s=await f.runtime.stop('parent','turn');assert.ok(s.issues.some(x=>x.includes('running')));assert.equal(s.snapshot.claims[0].kind,'running'); | 
|  | +  const g=await fixture();g.deps.probe=async identity=>({...identity,state:'working',identity_verified:true,observed_at:new Date(g.clock()-(identity.session_id==='worker'?60000:0)).toISOString(),source:'herdr_live'});g.runtime=h.createTaskRuntime(g.deps); | 
|  | +  await g.runtime.report('parent','task','turn',{claims:[{kind:'running',assignment_id:'a1'}]});assert.ok((await g.runtime.stop('parent','turn')).issues.length); | 
|  | +}); | 
|  | +test('valid running requires fresh owned working evidence separate from claims',async()=>{ | 
|  | +  const f=await fixture();await f.runtime.report('parent','task','turn',{claims:[{kind:'running',assignment_id:'a1'}]}); | 
|  | +  const s=await f.runtime.stop('parent','turn');assert.deepEqual(s.issues,[]);assert.equal(s.snapshot.observations.at(-1).source,'herdr_live');assert.equal(s.snapshot.task_status,'active'); | 
|  | +}); | 
|  | +test('idle-parent completion queues exact event once and does not complete task',async()=>{ | 
|  | +  const f=await fixture();await finish(f);assert.equal(f.sent.length,1);assert.equal(f.started.length,1);assert.equal(f.started[0].queuedSubmissionId,'q1'); | 
|  | +  assert.equal(f.runtime.get('task').status,'active');assert.equal(f.runtime.get('task').handoffs[0].attempts,1); | 
|  | +}); | 
|  | +test('duplicates and concurrent consumers enqueue at most once',async()=>{ | 
|  | +  const f=await fixture();f.states.worker='idle';const second=h.createTaskRuntime(f.deps); | 
|  | +  await Promise.all([f.runtime.reconcile('task'),second.reconcile('task'),f.runtime.reconcile('task')]);await f.runtime.reconcile('task');assert.equal(f.sent.length,1); | 
|  | +}); | 
|  | +test('restart preserves stable event and dispatch fence',async()=>{ | 
|  | +  const f=await fixture();await finish(f);const event=f.runtime.get('task').handoffs[0].event_id; | 
|  | +  await h.createTaskRuntime(f.deps).reconcile('task');assert.equal(f.sent.length,1);assert.equal(f.runtime.get('task').handoffs[0].event_id,event); | 
|  | +}); | 
|  | +test('cancel/pause/Interrupt fences pending and future continuation',async()=>{ | 
|  | +  for(const status of ['cancelled','paused']){const f=await fixture();await f.runtime.control('parent','task',status);await finish(f);assert.equal(f.sent.length,0);} | 
|  | +  const f=await fixture();await f.runtime.interrupt('parent');await finish(f);assert.equal(f.sent.length,0);assert.equal(f.runtime.get('task').status,'paused'); | 
|  | +}); | 
|  | +test('busy parent defers and backoff then queues when freshly idle',async()=>{ | 
|  | +  const f=await fixture();f.states.parent='working';await finish(f);assert.equal(f.sent.length,0);assert.equal(f.runtime.get('task').handoffs[0].reason,'parent_busy'); | 
|  | +  f.states.parent='idle';f.advance(1001);await f.runtime.reconcile('task');assert.equal(f.sent.length,1); | 
|  | +}); | 
|  | +test('missing backend fails closed with durable reason',async()=>{ | 
|  | +  const f=await fixture({missing:true});await finish(f);assert.equal(f.sent.length,0);assert.equal(f.runtime.get('task').handoffs[0].reason,'backend_unavailable'); | 
|  | +}); | 
|  | +test('ambiguous dispatch remains uncertain across restart without blind retry',async()=>{ | 
|  | +  let calls=0;const f=await fixture({queue:{add:async()=>{calls++;throw Error('transport lost after write');}}});await finish(f); | 
|  | +  assert.equal(f.runtime.get('task').handoffs[0].dispatch_status,'uncertain');f.advance(2000);await h.createTaskRuntime(f.deps).reconcile('task');assert.equal(calls,1); | 
|  | +}); | 
|  | +test('falsecompletion: idle and worker result never substitute verifier acceptance',async()=>{ | 
|  | +  const f=await fixture();await f.runtime.report('worker','task','wt',{result:{status:'delivered'},claims:[]}); | 
|  | +  await f.runtime.report('parent','task','turn',{claims:[{kind:'completed'}]});const s=await f.runtime.stop('parent','turn');assert.ok(s.issues.length);assert.equal(s.snapshot.acceptance.status,'unverified');assert.equal(f.runtime.get('task').status,'active'); | 
|  | +}); | 
|  | +test('task isolation rejects outsiders, forged observations and cross-turn claims',async()=>{ | 
|  | +  const f=await fixture();await assert.rejects(()=>f.runtime.report('outsider','task','turn',{claims:[]}),/member\|identity/); | 
|  | +  await assert.rejects(()=>f.runtime.report('parent','task','turn',{claims:[],observations:[{state:'working'}]}),/observation\|field/); | 
|  | +  await f.runtime.report('parent','task','turn',{claims:[{kind:'running',assignment_id:'a1'}]});assert.equal((await f.runtime.stop('parent','other')).snapshot.claims.length,0); | 
|  | +  assert.equal((await f.runtime.stop('outsider','turn')).snapshot,null); | 
|  | +}); | 
|  | +test('replaced identity, unknown state, expiry and budget fence sends',async()=>{ | 
|  | +  const f=await fixture();f.deps.probe=async identity=>({...identity,session_id:'replacement',state:'idle',identity_verified:false,observed_at:new Date(f.clock()).toISOString(),source:'herdr_live'}); | 
|  | +  const replaced=h.createTaskRuntime(f.deps);await replaced.reconcile('task');assert.equal(f.sent.length,0); | 
|  | +  const g=await fixture();g.states.parent='unknown';await finish(g);assert.equal(g.sent.length,0); | 
|  | +  const e=await fixture();e.advance(600001);await finish(e);assert.equal(e.sent.length,0); | 
|  | +}); | 
|  | +test('timestamped allowlisted per-turn export joins exact identities across cwd',async()=>{ | 
|  | +  const f=await fixture();await f.runtime.report('parent','task','turn',{claims:[]});const s=await f.runtime.stop('parent','turn'); | 
|  | +  const exported=JSON.parse(fs.readFileSync(path.join(f.deps.state,'sessions',h.digest('parent'),'loops',h.digest('turn')+'.json'))); | 
|  | +  assert.equal(exported.session_id,'parent');assert.equal(exported.turn_id,'turn');assert.equal(exported.parent_session_id,'parent');assert.equal(exported.assignment_id,null); | 
|  | +  assert.equal(exported.task_id,'task');assert.equal(exported.captured_at,new Date(f.clock()).toISOString());assert.ok(!JSON.stringify(exported).includes('review worker result'));assert.ok(!JSON.stringify(exported).includes(tmp));assert.deepEqual(exported,s.snapshot); | 
|  | +}); | 
|  | + | 
|  | + | 
|  | +test('conflicting continuation owner refuses registration',async()=>{await assert.rejects(()=>fixture({owner:{owner:'openclaw_watch',backend:'other',exclusive:false}}),/owner/);}); | 
|  | + | 
|  | + | 
|  | + | 
|  | +test('deferred add plus concurrent Interrupt publishes revocation before lock and never starts',async()=>{ | 
|  | +  let entered,release;const entering=new Promise(r=>entered=r),held=new Promise(r=>release=r); | 
|  | +  const f=await fixture({queue:{add:async item=>{entered();await held;return {queuedSubmissionId:'q-held'};}}}); | 
|  | +  f.states.worker='idle';const dispatch=f.runtime.reconcile('task');await entering; | 
|  | +  const paused=await f.runtime.interrupt('parent');assert.equal(paused.status,'paused'); | 
|  | +  assert.equal(h.createTaskRuntime(f.deps).get('task').status,'paused');release();await dispatch; | 
|  | +  assert.equal(f.started.length,0);assert.equal(f.deleted.length,1);assert.equal(f.deleted[0].queuedSubmissionId,'q-held'); | 
|  | +}); | 
|  | +test('lost start acknowledgement, crash fence and revoked owner are never retried',async()=>{ | 
|  | +  let calls=0;const f=await fixture({queue:{start:async()=>{calls++;throw Error('lost ack');}}});await finish(f); | 
|  | +  await h.createTaskRuntime(f.deps).reconcile('task');assert.equal(calls,1);assert.equal(f.runtime.get('task').handoffs[0].dispatch_status,'uncertain'); | 
|  | +  const g=await fixture();g.states.parent='working';await finish(g);g.states.parent='idle';g.advance(1001); | 
|  | +  g.deps.owner={owner:'other',backend:'other',exclusive:false};await h.createTaskRuntime(g.deps).reconcile('task');assert.equal(g.sent.length,0); | 
|  | +}); | 
|  | +test('late cancel/dequeue is fenced again at UserPromptSubmit and every PreToolUse',async()=>{ | 
|  | +  const f=await fixture();await finish(f);const prompt=f.sent[0].input[0].text; | 
|  | +  await f.runtime.control('parent','task','cancelled'); | 
|  | +  const gate=await h.handleHook({session_id:'parent',turn_id:'queued-turn',prompt,cwd:tmp,hook_event_name:'UserPromptSubmit'},{monitors:false,tasks:f.runtime}); | 
|  | +  assert.match(gate.hookSpecificOutput.additionalContext,/revoked/); | 
|  | +  const tool=await h.handleHook({session_id:'parent',turn_id:'queued-turn',tool_name:'Bash',cwd:tmp,hook_event_name:'PreToolUse'},{monitors:false,tasks:f.runtime});assert.equal(tool.hookSpecificOutput.permissionDecision,'deny'); | 
|  | +  await h.handleHook({session_id:'parent',turn_id:'ordinary',prompt:'status question',cwd:tmp,hook_event_name:'UserPromptSubmit'},{monitors:false,tasks:f.runtime}); | 
|  | +}); | 
|  | +test('Stop validates exact current structured report with one bounded block',async()=>{ | 
|  | +  const f=await fixture();f.states.worker='idle';await f.runtime.report('parent','task','stop-turn',{claims:[{kind:'running',assignment_id:'a1'}]}); | 
|  | +  const payload={session_id:'parent',turn_id:'stop-turn',cwd:tmp,hook_event_name:'Stop'}; | 
|  | +  const first=await h.handleHook(payload,{monitors:false,tasks:f.runtime});assert.equal(first.decision,'block');assert.match(first.reason,/running claim/); | 
|  | +  const next=await h.handleHook(payload,{monitors:false,tasks:f.runtime});assert.equal(next.decision,undefined);assert.match(next.systemMessage,/running claim/); | 
|  | +}); | 
|  | +test('actual verifier execution permits completed only with no remaining work and explicit completion',async()=>{ | 
|  | +  const f=await fixture();const dir=path.join(f.deps.state,'sessions',h.digest('parent')); | 
|  | +  const source=path.join(f.deps.state,'source');fs.mkdirSync(source,{recursive:true});fs.writeFileSync(path.join(source,'input.json'),'{}'); | 
|  | +  const c={schema_version:2,task_id:'task',objective:'fixture acceptance',status:'in_progress',requirements:[{id:'outcome',text:'Observed positive count',source:'fixture',checks:['count']}],checks:[{id:'count',cwd:source,inputs:['input.json'],command:[process.execPath,'-e','console.log(JSON.stringify({executed:1,failed:0}))'],format:'json',assertions:[{pointer:'/executed',op:'gte',value:1},{pointer:'/failed',op:'eq',value:0}]}]}; | 
|  | +  h.write(path.join(dir,'contract.json'),c);await assert.rejects(()=>f.runtime.control('parent','task','completed'),/acceptance/); | 
|  | +  assert.equal((await runVerification(c,dir,'count')).status,'pass'); | 
|  | +  await f.runtime.report('parent','task','complete-turn',{claims:[],remaining:[],next_action:'final report'});await f.runtime.control('parent','task','completed'); | 
|  | +  await f.runtime.report('parent','task','complete-turn',{claims:[{kind:'completed'}]});const result=await f.runtime.stop('parent','complete-turn');assert.deepEqual(result.issues,[]);assert.equal(result.snapshot.task_status,'completed');assert.equal(result.snapshot.acceptance.status,'pass');assert.ok(result.snapshot.acceptance.evidence_ids.length); | 
|  | +  fs.writeFileSync(path.join(source,'input.json'),'changed');assert.ok((await f.runtime.stop('parent','complete-turn')).issues.length); | 
|  | +}); | 
|  | +test('future observations and wake/probe limits cannot verify or dispatch',async()=>{ | 
|  | +  const f=await fixture();f.deps.probe=async expected=>({...expected,identity_verified:true,state:'working',source:'herdr_live',observed_at:new Date(f.clock()+(expected.session_id==='worker'?1000:0)).toISOString()});f.runtime=h.createTaskRuntime(f.deps); | 
|  | +  await f.runtime.report('parent','task','future',{claims:[{kind:'running',assignment_id:'a1'}]});assert.ok((await f.runtime.stop('parent','future')).issues.length); | 
|  | +  const g=await fixture();g.states.parent='working';for(let n=0;n<31;n++){g.advance(1001);await g.runtime.reconcile('task');}g.states.worker='idle';g.states.parent='idle';await g.runtime.reconcile('task');assert.equal(g.sent.length,0);assert.equal(g.runtime.get('task').continuation.fenced_reason,'probe_budget'); | 
|  | +}); | 
|  | + | 
|  | + | 
|  | +test('revoked continuation guard covers functions.exec, MCP and browser without broad preflight',async()=>{ | 
|  | +  const f=await fixture();await finish(f);const prompt=f.sent[0].input[0].text; | 
|  | +  await h.handleHook({session_id:'parent',turn_id:'all-tools',prompt,cwd:tmp,hook_event_name:'UserPromptSubmit'},{monitors:false,tasks:f.runtime}); | 
|  | +  await f.runtime.interrupt('parent'); | 
|  | +  for(const tool_name of ['functions.exec','functions.write_stdin','mcp__anything__mutation','mcp__cua_repl__js','browser','irrelevant_read']){ | 
|  | +    const response=await h.handleHook({session_id:'parent',turn_id:'all-tools',tool_name,cwd:tmp,hook_event_name:'PreToolUse'},{monitors:false,tasks:f.runtime});assert.equal(response.hookSpecificOutput.permissionDecision,'deny',tool_name); | 
|  | +  } | 
|  | +  let probes=0;const untracked=h.createTaskRuntime({state:path.join(tmp,'untracked'),probe:async()=>{probes++;throw Error('unexpected probe');}}); | 
|  | +  for(const tool_name of ['functions.exec','mcp__anything__mutation','browser'])assert.deepEqual(await h.handleHook({session_id:'unrelated',turn_id:'u',tool_name,cwd:tmp,hook_event_name:'PreToolUse'},{monitors:false,tasks:untracked}),{}); | 
|  | +  assert.equal(probes,0);assert.equal(fs.existsSync(path.join(tmp,'untracked','tasks')),false); | 
|  | +  const hooks=JSON.parse(fs.readFileSync(path.join(h.ROOT,'hooks/hooks.json')));assert.equal(hooks.hooks.PreToolUse[0].matcher,undefined); | 
|  | +  await h.handleHook({session_id:'parent',turn_id:'clear',prompt:'ordinary',cwd:tmp,hook_event_name:'UserPromptSubmit'},{monitors:false,tasks:f.runtime}); | 
|  | +}); | 
|  | +test('actual process crash after durable dispatch fence recovers without resending',async()=>{ | 
|  | +  const f=await fixture();const marker=path.join(f.deps.state,'dispatch-entered');const module=new URL('../scripts/task-loop.mjs',import.meta.url).href; | 
|  | +  const child=spawn(process.execPath,['--input-type=module','-e',`import fs from 'node:fs';import {createTaskRuntime} from ${JSON.stringify(module)};setInterval(()=>{},1000);const r=createTaskRuntime({state:${JSON.stringify(f.deps.state)},owner:{owner:'harness_monitor',backend:'codex_app_server',exclusive:true},probe:async x=>({...x,state:'idle',observed_at:new Date().toISOString(),source:'herdr_live',identity_verified:true}),queue:{available:async()=>true,add:async()=>{fs.writeFileSync(${JSON.stringify(marker)},'entered');await new Promise(()=>{});}}});await r.reconcile('task');`],{stdio:'ignore'}); | 
|  | +  const exit=new Promise(resolve=>child.on('exit',resolve)); | 
|  | +  try{for(let i=0;i<100&&!fs.existsSync(marker);i++)await new Promise(r=>setTimeout(r,20));assert.equal(fs.existsSync(marker),true);}finally{child.kill('SIGKILL');await exit;} | 
|  | +  f.states.worker='idle';await h.createTaskRuntime(f.deps).reconcile('task');assert.equal(f.sent.length,0);assert.equal(f.runtime.get('task').handoffs[0].dispatch_status,'uncertain'); | 
|  | +  const g=await fixture({max_wakes:0});await finish(g);assert.equal(g.sent.length,0);assert.equal(g.runtime.get('task').handoffs[0].reason,'wake_budget'); | 
|  | +}); | 
|  | + | 
|  | + | 
|  | +test('replayed acknowledged continuation persists denied authority for its exact turn',async()=>{ | 
|  | +  const f=await fixture();await finish(f);const prompt=f.sent[0].input[0].text; | 
|  | +  await h.handleHook({session_id:'parent',turn_id:'first-token',prompt,cwd:tmp,hook_event_name:'UserPromptSubmit'},{monitors:false,tasks:f.runtime}); | 
|  | +  assert.equal(f.runtime.get('task').handoffs[0].dispatch_status,'acknowledged'); | 
|  | +  const replay=await h.handleHook({session_id:'parent',turn_id:'replay-token',prompt,cwd:tmp,hook_event_name:'UserPromptSubmit'},{monitors:false,tasks:f.runtime});assert.match(replay.hookSpecificOutput.additionalContext,/replayed/); | 
|  | +  for(const tool_name of ['Bash','functions.exec','mcp__cua_repl__js'])assert.equal((await h.handleHook({session_id:'parent',turn_id:'replay-token',tool_name,cwd:tmp,hook_event_name:'PreToolUse'},{monitors:false,tasks:f.runtime})).hookSpecificOutput.permissionDecision,'deny'); | 
|  | +  await h.handleHook({session_id:'parent',turn_id:'clear-replay',prompt:'ordinary',cwd:tmp,hook_event_name:'UserPromptSubmit'},{monitors:false,tasks:f.runtime}); | 
|  | +}); | 
|  | +test('a hook killed while the queue lock is held has already persisted a pending deny guard',async()=>{ | 
|  | +  let entered,release,input;const entering=new Promise(r=>entered=r),held=new Promise(r=>release=r); | 
|  | +  const f=await fixture({queue:{add:async item=>{input=item;entered();await held;return {queuedSubmissionId:'held-q'};}}});f.states.worker='idle';const dispatch=f.runtime.reconcile('task');await entering; | 
|  | +  const env={...process.env,HARNESS_STATE_DIR:f.deps.state,CODEX_THREAD_ID:''};const cli=path.join(h.ROOT,'scripts/harness.mjs'); | 
|  | +  const child=spawn(process.execPath,[cli,'hook'],{env,stdio:['pipe','ignore','ignore']});const exit=new Promise(r=>child.on('exit',r));child.stdin.end(JSON.stringify({session_id:'parent',turn_id:'killed-hook',cwd:tmp,prompt:input.input[0].text,hook_event_name:'UserPromptSubmit'})); | 
|  | +  const guardFile=path.join(f.deps.state,'sessions',h.digest('parent'),'task-continuation.json'); | 
|  | +  try{for(let n=0;n<100&&!fs.existsSync(guardFile);n++)await new Promise(r=>setTimeout(r,10));assert.equal(JSON.parse(fs.readFileSync(guardFile)).authorized,false);child.kill('SIGKILL');await exit; | 
|  | +    const next=spawnSync(process.execPath,[cli,'hook'],{env,encoding:'utf8',input:JSON.stringify({session_id:'parent',turn_id:'killed-hook',cwd:tmp,tool_name:'functions.exec',hook_event_name:'PreToolUse'}),timeout:3000});assert.equal(next.status,0,next.stderr);assert.equal(JSON.parse(next.stdout).hookSpecificOutput.permissionDecision,'deny');assert.equal(f.runtime.get('task').handoffs[0].parent_continued_at,null); | 
|  | +  }finally{child.kill('SIGKILL');await f.runtime.interrupt('parent');release();await dispatch;} | 
|  | +}); | 
|  | +test('terminal memberships do not require reports or probes on ordinary later turns',async()=>{ | 
|  | +  const f=await fixture();await f.runtime.control('parent','task','cancelled');const before=f.runtime.get('task').observations.length; | 
|  | +  assert.deepEqual(await f.runtime.stop('parent','later'),{issues:[],snapshot:null});await f.runtime.reconcileSession('parent');assert.equal(f.runtime.get('task').observations.length,before); | 
|  | +  const binding=JSON.parse(fs.readFileSync(path.join(f.deps.state,'sessions',h.digest('parent'),'task-membership.json')));assert.equal(binding.active,false); | 
|  | +  const g=await fixture({verifier:async()=>({status:'pass',evidence_ids:['fixture-evidence']})});await g.runtime.report('parent','task','final-turn',{claims:[],remaining:[],next_action:'final'});await g.runtime.control('parent','task','completed');await g.runtime.report('parent','task','final-turn',{claims:[{kind:'completed'}]});assert.ok((await g.runtime.stop('parent','final-turn')).snapshot);assert.deepEqual(await g.runtime.stop('parent','later'),{issues:[],snapshot:null}); | 
|  | +}); | 
|  | +test('claim-time acceptance remains unverified even when a later current recheck passes',async()=>{ | 
|  | +  let accepted=false;const f=await fixture({verifier:async()=>({status:accepted?'pass':'unverified',evidence_ids:accepted?['fixture-evidence']:[]})}); | 
|  | +  await f.runtime.report('parent','task','early-claim',{claims:[{kind:'completed'}],remaining:[],next_action:'final'});accepted=true;await f.runtime.control('parent','task','completed');const s=await f.runtime.stop('parent','early-claim');assert.ok(s.issues.length);assert.equal(s.snapshot.claim_acceptance.status,'unverified');assert.equal(s.snapshot.acceptance.status,'pass'); | 
|  | +}); | 
|  | +test('working observed by report/Stop persists before restart and later idle handoff',async()=>{ | 
|  | +  const f=await fixture({initial_worker_state:'idle'});f.states.worker='working';await f.runtime.report('parent','task','work-turn',{claims:[{kind:'running',assignment_id:'a1'}]});await f.runtime.stop('parent','work-turn'); | 
|  | +  assert.equal(f.runtime.get('task').assignments.a1.was_working,true);f.states.worker='idle';await h.createTaskRuntime(f.deps).reconcile('task');assert.equal(f.sent.length,1); | 
|  | +}); | 
|  | +test('concurrent processes recover dead ownership-directory tokens without double registration',async()=>{ | 
|  | +  const state=path.join(tmp,'registration-race'),lock=path.join(state,'tasks','mutation.lock');fs.mkdirSync(lock,{recursive:true});fs.writeFileSync(path.join(lock,'owner.dead.json'),JSON.stringify({pid:99999999,signature:'dead',nonce:'dead'})); | 
|  | +  const module=new URL('../scripts/task-loop.mjs',import.meta.url).href;const spec={schema_version:1,task_id:'race-task',parent,remaining:['check'],next_action:'check',continuation:{enabled:false,owner:'harness_monitor',backend:'codex_app_server',max_wakes:0,max_probes:5,backoff_ms:1000,expires_at:new Date(Date.now()+600000).toISOString()}}; | 
|  | +  const attempt=()=>new Promise(resolve=>{let output='';const child=spawn(process.execPath,['--input-type=module','-e',`import {createTaskRuntime} from ${JSON.stringify(module)};const r=createTaskRuntime({state:${JSON.stringify(state)},probe:async x=>({...x,state:'idle',source:'herdr_live',identity_verified:true,observed_at:new Date().toISOString()})});try{await r.register('parent',${JSON.stringify(spec)});console.log('registered');}catch(e){console.log(e.message);process.exitCode=1;}`]);child.stdout.on('data',b=>output+=b);child.on('exit',code=>resolve({code,output}));}); | 
|  | +  const results=await Promise.all([attempt(),attempt(),attempt(),attempt()]);assert.equal(results.filter(r=>r.code===0).length,1,JSON.stringify(results));assert.equal(fs.existsSync(lock),false); | 
|  | +}); | 
|  | + | 
|  | + | 
|  | +test('existing monitor captures explicitly linked worker across cwd and ignores unrelated pane',async()=>{ | 
|  | +  const workerCwd=path.join(tmp,'worker-worktree');fs.mkdirSync(workerCwd);const socket=path.join(tmp,'loop-monitor.sock');const previous=process.env.HERDR_SOCKET_PATH;process.env.HERDR_SOCKET_PATH=socket; | 
|  | +  const f=await fixture({state:h.STATE,enabled:false,worker:{...worker,cwd:workerCwd}}); | 
|  | +  // Sparse events use the freshly verified same-goal pane mapping; worker | 
|  | +  // worktree differences do not affect membership. | 
|  | +  let subscriptions=0;const peers=[];const server=net.createServer(peer=>{peers.push(peer);peer.on('data',chunk=>{ | 
|  | +    const msg=JSON.parse(chunk); | 
|  | +    if(msg.method==='ping'\|\|msg.method==='pane.current'){ | 
|  | +          const result=msg.method==='ping'?{type:'pong'}:{pane:{pane_id:'p1',terminal_id:'p1-term',tab_id:'w:t',workspace_id:'w'}}; | 
|  | +          peer.end(JSON.stringify({id:msg.id,result})+'\n');return; | 
|  | +        } | 
|  | +        if(msg.method==='session.snapshot'){ | 
|  | +      const agents=[parent,{...worker,cwd:workerCwd}].map(x=>({...x,tab_id:'w:t',workspace_id:'w',terminal_id:x.pane_id+'-term',agent:'codex',agent_session:{kind:'id',value:x.session_id}})); | 
|  | +      peer.end(JSON.stringify({id:msg.id,result:{snapshot:{agents}}})+'\n');return; | 
|  | +    } | 
|  | +    subscriptions++;peer.write(JSON.stringify({id:msg.id,result:{type:'subscription_started'}})+'\n');setTimeout(()=>{if(peer.destroyed)return;for(const pane of [{pane_id:'p2',cwd:workerCwd,agent:'codex',agent_status:'idle'},{pane_id:'p-unrelated',cwd:workerCwd,agent:'codex',agent_status:'working'}])peer.write(JSON.stringify({event:'pane_updated',data:{type:'pane_updated',pane}})+'\n');},50); | 
|  | +  });}); | 
|  | +  await new Promise(r=>server.listen(socket,r)); | 
|  | +  try{ | 
|  | +    await h.handleHook({session_id:'parent',hook_event_name:'SessionStart',cwd:tmp,turn_id:'monitor-turn'},{tasks:f.runtime}); | 
|  | +    const eventsFile=path.join(h.sessionDir('parent'),'events.json');let events=[]; | 
|  | +    for(let n=0;n<100;n++){try{events=JSON.parse(fs.readFileSync(eventsFile));}catch{}if(events.some(e=>e.pane==='p2'))break;await new Promise(r=>setTimeout(r,10));} | 
|  | +    assert.equal(events.some(e=>e.pane==='p2'),true);assert.equal(events.some(e=>e.pane==='p-unrelated'),false);assert.equal(subscriptions,2); | 
|  | +    await h.handleHook({session_id:'parent',hook_event_name:'UserPromptSubmit',cwd:tmp,turn_id:'monitor-next',prompt:'fixture followup'},{tasks:f.runtime});assert.equal(subscriptions,2); | 
|  | +  }finally{await h.handleHook({session_id:'parent',hook_event_name:'SessionEnd',cwd:tmp},{tasks:f.runtime});peers.forEach(p=>p.destroy());await new Promise(r=>server.close(r));if(previous===undefined)delete process.env.HERDR_SOCKET_PATH;else process.env.HERDR_SOCKET_PATH=previous;} | 
|  | +}); | 
|  | +test('missing delete backend never reports acknowledged queue deletion',async()=>{const f=await fixture();await finish(f);const disconnected=h.createTaskRuntime({...f.deps,queue:null});await disconnected.control('parent','task','cancelled');assert.equal(disconnected.get('task').handoffs[0].reason,'cancel_delete_uncertain');}); | 
|  | +test.after(()=>fs.rmSync(tmp,{recursive:true,force:true})); | 
|  | diff --git a/test/verification.test.mjs b/test/verification.test.mjs | 
|  | new file mode 100644 | 
|  | index 0000000..5ea39b5 | 
|  | --- /dev/null | 
|  | +++ b/test/verification.test.mjs | 
|  | @@ -0,0 +1,215 @@ | 
|  | +import test from 'node:test'; | 
|  | +import assert from 'node:assert/strict'; | 
|  | +import fs from 'node:fs'; | 
|  | +import os from 'node:os'; | 
|  | +import path from 'node:path'; | 
|  | +import {spawnSync} from 'node:child_process'; | 
|  | +import {validateVerificationContract, validateRevision, sourceFingerprint, assessVerification, runVerification} from '../scripts/verification.mjs'; | 
|  | + | 
|  | +const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'harness-outcome-tests-')); | 
|  | +process.env.HARNESS_STATE_DIR = path.join(tmp, 'state'); | 
|  | +const h = await import('../scripts/harness.mjs'); | 
|  | +const cli = path.join(h.ROOT, 'scripts/harness.mjs'); | 
|  | +test.after(() => fs.rmSync(tmp, {recursive: true, force: true})); | 
|  | +function fixture() { | 
|  | +  const cwd = fs.mkdtempSync(path.join(tmp, 'project-')); | 
|  | +  fs.writeFileSync(path.join(cwd, 'data.json'), JSON.stringify({items: ['a', 'b'], release: 'new', ui: 'complete', server: 'complete'})); | 
|  | +  fs.writeFileSync(path.join(cwd, 'observe.mjs'), `import fs from 'node:fs'; | 
|  | +const d=JSON.parse(fs.readFileSync('data.json')); | 
|  | +const n=fs.existsSync('count.txt')?Number(fs.readFileSync('count.txt')):0; | 
|  | +fs.writeFileSync('count.txt',String(n+1)); | 
|  | +if(fs.existsSync('fail.flag'))process.exit(7); | 
|  | +process.stdout.write(JSON.stringify({count:d.items.length,release:d.release,ui:d.ui,server:d.server})); | 
|  | +`); | 
|  | +  const c = {schema_version: 2, task_id: 'delivery', objective: 'Deliver the approved usable items', status: 'in_progress', | 
|  | +    requirements: [{id: 'items', text: 'Two items are usable', source: 'fixture user request', checks: ['observe']}], | 
|  | +    checks: [{id: 'observe', cwd, inputs: ['data.json', 'observe.mjs'], command: [process.execPath, 'observe.mjs'], format: 'json', | 
|  | +      assertions: [{pointer: '/count', op: 'gte', value: 2}, {pointer: '/release', op: 'eq', value: 'new'}, | 
|  | +        {pointer: '/ui', op: 'eq', value: 'complete'}, {pointer: '/server', op: 'eq', value: 'complete'}]}]}; | 
|  | +  return {c, cwd, dir: path.join(cwd, 'state')}; | 
|  | +} | 
|  | +const get = file => JSON.parse(fs.readFileSync(file)); | 
|  | + | 
|  | +test('missing observations cannot complete; actual results pass and can be reused without rerunning', async () => { | 
|  | +  const {c, cwd, dir} = fixture(); | 
|  | +  assert.deepEqual(assessVerification(c, dir).next_checks, ['observe']); | 
|  | +  const first = await runVerification(c, dir, 'observe'); | 
|  | +  assert.equal(first.status, 'pass'); assert.equal(first.reused, false); | 
|  | +  assert.equal(assessVerification(c, dir).status, 'pass'); | 
|  | +  const second = await runVerification(c, dir, 'observe'); | 
|  | +  assert.equal(second.reused, true); | 
|  | +  assert.equal(fs.readFileSync(path.join(cwd, 'count.txt'), 'utf8'), '1'); | 
|  | +}); | 
|  | + | 
|  | +test('wrong count, wrong deployment and UI/server disagreement fail despite command exit zero', async () => { | 
|  | +  for (const change of [{items: ['a']}, {release: 'old'}, {ui: 'processing'}]) { | 
|  | +    const {c, cwd, dir} = fixture(); | 
|  | +    const file = path.join(cwd, 'data.json'); fs.writeFileSync(file, JSON.stringify({...get(file), ...change})); | 
|  | +    const result = await runVerification(c, dir, 'observe'); | 
|  | +    assert.equal(result.status, 'failed'); assert.equal(get(result.receipt).exit_code, 0); | 
|  | +    assert.notEqual(assessVerification(c, dir).status, 'pass'); | 
|  | +  } | 
|  | +}); | 
|  | + | 
|  | +test('missing JSON fields, invalid JSON and zero executed tests do not count as passes', async () => { | 
|  | +  for (const output of ['{}', 'not-json', '{"executed":0,"failed":0}']) { | 
|  | +    const {c, dir} = fixture(); | 
|  | +    c.checks[0].command = [process.execPath, '-e', 'process.stdout.write(process.argv[1])', output]; | 
|  | +    c.checks[0].assertions = [{pointer: '/executed', op: 'gte', value: 1}, {pointer: '/failed', op: 'eq', value: 0}]; | 
|  | +    assert.equal((await runVerification(c, dir, 'observe')).status, 'failed'); | 
|  | +  } | 
|  | +}); | 
|  | + | 
|  | +test('dirty input changes invalidate a previous pass; a new observation is required', async () => { | 
|  | +  const {c, cwd, dir} = fixture(); await runVerification(c, dir, 'observe'); | 
|  | +  fs.appendFileSync(path.join(cwd, 'observe.mjs'), '\n// relevant source changed\n'); | 
|  | +  assert.match(assessVerification(c, dir).issues.join(' '), /Inputs changed/); | 
|  | +  assert.equal((await runVerification(c, dir, 'observe')).reused, false); | 
|  | +  assert.equal(assessVerification(c, dir).status, 'pass'); | 
|  | +}); | 
|  | + | 
|  | +test('modified, missing, and failing evidence cannot be rescued by an earlier pass', async () => { | 
|  | +  const {c, cwd, dir} = fixture(); | 
|  | +  const result = await runVerification(c, dir, 'observe'); | 
|  | +  const receipt = get(result.receipt); | 
|  | +  const output = path.join(path.dirname(result.receipt), receipt.run_id, 'stdout.log'); | 
|  | +  fs.writeFileSync(output, '{}'); | 
|  | +  assert.match(assessVerification(c, dir).issues.join(' '), /evidence changed/); | 
|  | +  await runVerification(c, dir, 'observe'); | 
|  | +  fs.writeFileSync(path.join(cwd, 'fail.flag'), 'fail'); | 
|  | +  const failed = await runVerification(c, dir, 'observe', {force: true}); | 
|  | +  assert.equal(failed.status, 'failed'); assert.equal(get(failed.receipt).exit_code, 7); | 
|  | +  assert.equal(assessVerification(c, dir).checks[0].status, 'failed'); | 
|  | +  fs.unlinkSync(failed.receipt); | 
|  | +  assert.match(assessVerification(c, dir).issues.join(' '), /No observed execution/); | 
|  | +}); | 
|  | + | 
|  | +test('artifacts must exist and keep their observed bytes', async () => { | 
|  | +  const {c, cwd, dir} = fixture(); | 
|  | +  c.checks[0].artifacts = ['screen.txt']; | 
|  | +  fs.appendFileSync(path.join(cwd, 'observe.mjs'), "\nfs.writeFileSync('screen.txt','observed screen');\n"); | 
|  | +  await runVerification(c, dir, 'observe'); | 
|  | +  assert.equal(assessVerification(c, dir).status, 'pass'); | 
|  | +  fs.writeFileSync(path.join(cwd, 'screen.txt'), 'different screen'); | 
|  | +  assert.match(assessVerification(c, dir).issues.join(' '), /artifact changed/); | 
|  | +  fs.unlinkSync(path.join(cwd, 'screen.txt')); | 
|  | +  assert.notEqual(assessVerification(c, dir).status, 'pass'); | 
|  | +}); | 
|  | + | 
|  | +test('preexisting artifacts cannot masquerade as a new observation', async () => { | 
|  | +  const {c,cwd,dir}=fixture(); | 
|  | +  c.checks[0].artifacts=['screen.txt']; | 
|  | +  fs.writeFileSync(path.join(cwd,'screen.txt'),'an older screen'); | 
|  | +  const result=await runVerification(c,dir,'observe'); | 
|  | +  assert.equal(result.status,'failed'); | 
|  | +  assert.match(get(result.receipt).error,/not refreshed/); | 
|  | +}); | 
|  | + | 
|  | +test('concurrent executions cannot overwrite each other and orphaned attempts can resume', async () => { | 
|  | +  const {c,cwd,dir}=fixture(); | 
|  | +  fs.appendFileSync(path.join(cwd,'observe.mjs'), '\nawait new Promise(resolve=>setTimeout(resolve,150));\n'); | 
|  | +  const first=runVerification(c,dir,'observe'); | 
|  | +  await assert.rejects(()=>runVerification(c,dir,'observe'),/already running/); | 
|  | +  const result=await first; | 
|  | +  assert.equal(result.status,'pass'); | 
|  | +  const directory=path.dirname(result.receipt); | 
|  | +  const receipt=get(result.receipt);receipt.status='running'; | 
|  | +  fs.writeFileSync(result.receipt,JSON.stringify(receipt)); | 
|  | +  fs.writeFileSync(path.join(directory,'lock.json'),JSON.stringify({pid:process.pid,signature:'not-this-process',nonce:'orphan'})); | 
|  | +  const resumed=await runVerification(c,dir,'observe'); | 
|  | +  assert.equal(resumed.status,'pass');assert.equal(resumed.reused,false); | 
|  | +}); | 
|  | + | 
|  | +test('changed criteria and task IDs cannot reuse unrelated evidence', async () => { | 
|  | +  const {c, dir} = fixture(); await runVerification(c, dir, 'observe'); | 
|  | +  const revised = structuredClone(c); revised.checks[0].assertions[0].value = 3; | 
|  | +  assert.match(assessVerification(revised, dir).issues.join(' '), /definition changed/); | 
|  | +  revised.task_id = 'different-task'; | 
|  | +  assert.match(assessVerification(revised, dir).issues.join(' '), /No observed execution/); | 
|  | +}); | 
|  | + | 
|  | +test('scope omissions are rejected; explicit supersession stays possible', () => { | 
|  | +  const {c} = fixture(); | 
|  | +  const revised = structuredClone(c); | 
|  | +  revised.requirements = [{id: 'other', text: 'Other result', source: 'request', checks: ['observe']}]; | 
|  | +  revised.change_basis = 'Updated task'; | 
|  | +  assert.throws(() => validateRevision(c, revised), /Keep requirement items/); | 
|  | +  const changed = structuredClone(c); changed.requirements[0].text = 'New decision'; | 
|  | +  assert.throws(() => validateRevision(c, changed), /change_basis/); | 
|  | +  const canceled = structuredClone(c); | 
|  | +  canceled.requirements[0] = {...canceled.requirements[0], status: 'superseded', decision_basis: 'User explicitly withdrew this requirement'}; | 
|  | +  canceled.change_basis = 'User explicitly withdrew this requirement'; | 
|  | +  assert.doesNotThrow(() => validateVerificationContract(canceled)); | 
|  | +  assert.doesNotThrow(() => validateRevision(c, canceled)); | 
|  | +}); | 
|  | + | 
|  | +test('uncovered requirements and empty assertions are invalid', () => { | 
|  | +  const {c} = fixture(); | 
|  | +  const uncovered = structuredClone(c); uncovered.requirements[0].checks = []; | 
|  | +  assert.throws(() => validateVerificationContract(uncovered), /link at least one/); | 
|  | +  c.checks[0].assertions = []; | 
|  | +  assert.throws(() => validateVerificationContract(c), /need assertions/); | 
|  | +}); | 
|  | + | 
|  | +test('changes during execution and interrupted receipts remain unverified', async () => { | 
|  | +  const {c, dir} = fixture(); | 
|  | +  c.checks[0].command = [process.execPath, '-e', "require('fs').appendFileSync('observe.mjs',String.fromCharCode(10)+'// changed');process.stdout.write('{}')"]; | 
|  | +  const result = await runVerification(c, dir, 'observe'); | 
|  | +  assert.equal(result.status, 'unverified'); assert.match(result.issues.join(' '), /Inputs changed/); | 
|  | +  const receipt = get(result.receipt); receipt.status = 'running'; fs.writeFileSync(result.receipt, JSON.stringify(receipt)); | 
|  | +  assert.match(assessVerification(c, dir).issues.join(' '), /running or was interrupted/); | 
|  | +}); | 
|  | + | 
|  | +test('timeouts and expired external observations do not pass', async () => { | 
|  | +  const {c, dir} = fixture(); | 
|  | +  c.checks[0].command = [process.execPath, '-e', 'setInterval(()=>{},1000)']; c.checks[0].timeout_ms = 60; | 
|  | +  const timed = await runVerification(c, dir, 'observe'); | 
|  | +  assert.equal(timed.status, 'failed'); assert.equal(get(timed.receipt).timed_out, true); | 
|  | +  const next = fixture(); next.c.checks[0].max_age_seconds = 1; | 
|  | +  await runVerification(next.c, next.dir, 'observe'); | 
|  | +  await new Promise(resolve => setTimeout(resolve, 1050)); | 
|  | +  assert.match(assessVerification(next.c, next.dir).issues.join(' '), /expired/); | 
|  | +}); | 
|  | + | 
|  | +test('Git inputs include dirty and untracked files; committing identical inputs preserves the fingerprint', () => { | 
|  | +  const {cwd} = fixture(); | 
|  | +  const git = (...args) => {const result = spawnSync('git', ['-C', cwd, ...args], {encoding: 'utf8'}); assert.equal(result.status, 0, result.stderr);}; | 
|  | +  git('init', '-q'); git('config', 'user.name', 'Fixture'); git('config', 'user.email', 'fixture@example.invalid'); | 
|  | +  const check = {cwd}; const before = sourceFingerprint(check).sha256; | 
|  | +  git('add', '.'); git('commit', '-qm', 'Fixture'); | 
|  | +  assert.equal(sourceFingerprint(check).sha256, before); | 
|  | +  fs.writeFileSync(path.join(cwd, 'new-source.txt'), 'new behavior'); | 
|  | +  assert.notEqual(sourceFingerprint(check).sha256, before); | 
|  | +  const added = sourceFingerprint(check).sha256; | 
|  | +  git('add', '.'); git('commit', '-qm', 'New input'); | 
|  | +  assert.equal(sourceFingerprint(check).sha256, added); | 
|  | +}); | 
|  | + | 
|  | +test('CLI completion requires evidence and reports remaining checks without claiming success', () => { | 
|  | +  const {c, cwd} = fixture(), session = 'cli-completion'; | 
|  | +  const file = path.join(cwd, 'contract.json'); fs.writeFileSync(file, JSON.stringify(c)); | 
|  | +  const run = args => spawnSync(process.execPath, [cli, ...args, '--session', session], {encoding: 'utf8'}); | 
|  | +  assert.equal(run(['contract', 'set', '--file', file]).status, 0); | 
|  | +  const pending = run(['contract', 'complete']); assert.equal(pending.status, 1); | 
|  | +  assert.deepEqual(JSON.parse(pending.stdout).next_checks, ['observe']); | 
|  | +  assert.equal(run(['verify', 'run', '--check', 'observe']).status, 0); | 
|  | +  assert.equal(run(['contract', 'complete']).status, 0); | 
|  | +  assert.equal(get(path.join(h.sessionDir(session), 'contract.json')).status, 'complete'); | 
|  | +}); | 
|  | + | 
|  | +test('Stop catches stale completion once but does not impose verification on unrelated later questions', async () => { | 
|  | +  const {c, cwd} = fixture(), session = 'verified-stop'; | 
|  | +  await h.handleHook({session_id: session, hook_event_name: 'UserPromptSubmit', turn_id: 'work', cwd}, {monitors: false}); | 
|  | +  h.setContract(session, c); | 
|  | +  assert.throws(() => h.setContract(session, {...c, status: 'complete'}), /unverified/); | 
|  | +  await runVerification(c, h.sessionDir(session), 'observe'); | 
|  | +  h.setContract(session, {...c, status: 'complete'}); | 
|  | +  fs.appendFileSync(path.join(cwd, 'observe.mjs'), '\n// changed after completion\n'); | 
|  | +  const stop = {session_id: session, hook_event_name: 'Stop', turn_id: 'work', cwd}; | 
|  | +  assert.equal((await h.handleHook(stop, {monitors: false})).decision, 'block'); | 
|  | +  assert.equal((await h.handleHook(stop, {monitors: false})).decision, undefined); | 
|  | +  await h.handleHook({session_id: session, hook_event_name: 'UserPromptSubmit', turn_id: 'question', cwd}, {monitors: false}); | 
|  | +  assert.deepEqual(await h.handleHook({...stop, turn_id: 'question'}, {monitors: false}), {}); | 
|  | +  const result = get(path.join(h.sessionDir(session), 'outcomes', h.digest('question') + '.json')); | 
|  | +  assert.equal(result.structural_check, 'not_applicable'); assert.equal(result.verification_status, 'not_checked'); | 
|  | +}); |
