File size: 32,581 Bytes
f9edbec
 
 
 
 
 
 
8b39f58
 
f9edbec
 
8b39f58
 
f9edbec
 
8b39f58
 
f9edbec
 
 
 
 
 
 
 
 
8b39f58
 
 
 
 
 
 
f9edbec
 
 
 
 
 
 
 
 
 
 
 
 
 
c7ce083
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9edbec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b39f58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7ce083
8b39f58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9edbec
8b39f58
 
 
 
 
 
 
 
 
f9edbec
8b39f58
 
c7ce083
f9edbec
 
 
 
8b39f58
f9edbec
 
 
 
 
 
 
 
 
8b39f58
 
 
 
 
 
 
 
 
 
 
 
 
f9edbec
 
 
8b39f58
 
 
 
 
f9edbec
c7ce083
f9edbec
 
c7ce083
f9edbec
c7ce083
f9edbec
 
8b39f58
f9edbec
 
8b39f58
 
 
 
 
f9edbec
 
 
 
8b39f58
 
 
 
 
f9edbec
 
 
 
 
8b39f58
f9edbec
8b39f58
 
f9edbec
8b39f58
 
 
 
 
 
 
f9edbec
 
 
 
8b39f58
 
f9edbec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b39f58
f9edbec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7ce083
f9edbec
 
 
8b39f58
f9edbec
 
 
 
c7ce083
 
 
 
 
 
f9edbec
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/* THESEUS manual KG navigation challenge. Browser-only. Never sends user routes to a server. */
(() => {
'use strict';
const $=id=>document.getElementById(id);
const DATA={
  kinship:{title:'Kinship',dataset:'HalcyonSolutions/Kinship',config:'nhop',graph:'kg/orig/triplets.txt',key:'kinship'},
  sa:{title:'MQuAKE-ST · Single Answer',dataset:'HalcyonSolutions/MQuAKE-ST',config:'SA-nhop',graph:'kg/triplets.txt',key:'mquake'},
  ma:{title:'MQuAKE-ST · Multi Answer',dataset:'HalcyonSolutions/MQuAKE-ST',config:'MA-nhop',graph:'kg/triplets.txt',key:'mquake'},
  metaqa:{title:'MetaQA · Encoded',dataset:null,config:null,graph:null,key:'metaqa'}
};
const API='https://datasets-server.huggingface.co';
// Public GCS JSON API supports browser CORS without a bucket-specific CORS rule.
const METAQA_ARCHIVE='https://storage.googleapis.com/download/storage/v1/b/halcyon_data/o/'+encodeURIComponent('multihop_ds/datasets/MetaQA/metaqa_dataset.zip')+'?alt=media';
const MAX_STEPS=12;
const PAGE_SIZE=40;
const state={dataset:'kinship',question:null,source:'',current:'',route:[],finished:false,activeRel:null,neighborOffset:0,relationItems:[],neighborCount:0,task:0,split:'test',wording:'canonical',questionWording:'',workerReq:0,pending:new Map(),graphReady:new Set(),loadingGraph:null,nodeLabels:new Map(),relationLabels:new Map(),metaReady:false,mquakeNodeLabels:new Map(),mquakeRelationLabels:new Map(),metaqaNodeLabels:new Map(),metaqaRelationLabels:new Map()};
const worker=new Worker('graph-worker.js?v=4');
const text=(el,val)=>{el.textContent=val==null?'':String(val);return el;};
const el=(tag,cls,content)=>{let n=document.createElement(tag);if(cls)n.className=cls;if(content!==undefined)text(n,content);return n;};
const append=(parent,...children)=>{children.forEach(c=>parent.append(c));return parent;};
function status(msg,error=false){text($('setup-status'),msg);$('setup-status').classList.toggle('error',error);}
function navStatus(msg,error=false){text($('navigation-status'),msg);$('navigation-status').classList.toggle('error',error);}
function dataset(){return DATA[state.dataset];}
function escaped(v){return String(v||'').replace(/[^a-zA-Z0-9_-]/g,'');}
function makeUrl(d,path){return `https://huggingface.co/datasets/${d.dataset}/resolve/main/${path}`;}
function labelEntity(id){id=String(id??'');return state.dataset==='kinship'?id:(state.nodeLabels.get(id)||id);}
function labelRelation(id){
 id=String(id??'');
 if(state.dataset==='kinship')return id;
 if(state.dataset==='metaqa'&&id.startsWith('_'))return '← '+(state.relationLabels.get(id.slice(1))||id.slice(1))+' (reverse)';
 return state.relationLabels.get(id)||id;
}
function wikiLink(kind,id){if(state.dataset==='kinship'||state.dataset==='metaqa')return null;return kind==='entity'&&/^Q\d+$/.test(id)?`https://www.wikidata.org/wiki/${id}`:kind==='relation'&&/^P\d+$/.test(id)?`https://www.wikidata.org/wiki/Property:${id}`:null;}
function named(parent,kind,id,withLink=true){const name=kind==='entity'?labelEntity(id):labelRelation(id);const href=wikiLink(kind,id);const holder=el('span');append(holder,el('span','',name));if(name!==id)append(holder,el('span','nodeid',` (${id})`));if(href&&withLink){let a=el('a','link','↗');a.href=href;a.title=`Open ${id} on Wikidata`;a.target='_blank';a.rel='noopener noreferrer';a.style.marginLeft='5px';append(holder,a);}parent.append(holder);return holder;}
function paramUrl(route,params){return `${API}/${route}?${new URLSearchParams(params)}`;}
async function fetchJson(url,timeout=19000){const controller=new AbortController();const timer=setTimeout(()=>controller.abort(),timeout);try{const res=await fetch(url,{signal:controller.signal,headers:{Accept:'application/json'}});if(!res.ok)throw Error(`HTTP ${res.status} at dataset viewer`);return await res.json();}finally{clearTimeout(timer);}}
function asList(input){
 if(Array.isArray(input))return input;
 if(typeof input!=='string')return [];
 let s=input.trim();if(!s.startsWith('[')||s.length>100000)return [];
 try{const x=JSON.parse(s);if(Array.isArray(x))return x;}catch(_){/* Python literal */}
 let i=0;const ws=()=>{while(/\s/.test(s[i]||'')&&i<s.length)i++;};
 function parse(level){if(level>9)throw Error('Too deeply nested');ws();if(s[i]==='['){i++;let out=[];ws();while(s[i]!==']'){if(i>=s.length||out.length>10000)throw Error('Invalid list');out.push(parse(level+1));ws();if(s[i]===','){i++;ws();}else if(s[i]!==']')throw Error('Invalid list separator');}i++;return out;}
 if(s[i]==="'"||s[i]==='"'){let quote=s[i++],out='';while(i<s.length&&s[i]!==quote){if(s[i]==='\\'){i++;const ch=s[i++]||'';out+=({n:'\n',t:'\t',r:'\r'}[ch]??ch);}else out+=s[i++];}if(s[i]!==quote)throw Error('Bad literal');i++;return out;}
 const token=/^(None|True|False|-?\d+(?:\.\d+)?)/.exec(s.slice(i));if(!token)throw Error('Unrecognized literal');i+=token[0].length;return token[0]==='None'?null:token[0]==='True'?true:token[0]==='False'?false:Number(token[0]);}
 try{const out=parse(0);ws();return i===s.length&&Array.isArray(out)?out:[];}catch(_){return [];}
}
/** Released Question-Paraphrased values are serialized Python-list literals (or arrays). */
function normalizedWording(value){
 return String(value??'').normalize('NFKC').toLocaleLowerCase()
   .replace(/[\p{P}\p{S}]+/gu,' ').replace(/\s+/g,' ').trim();
}
function alternateWordings(row){
 const canonical=String(row?.Question??'').trim();
 const raw=row?.['Question-Paraphrased'];
 const variants=Array.isArray(raw)?raw:asList(raw);
 // Favor genuinely different phrasing. Some released lists include the canonical string.
 const seen=new Set([normalizedWording(canonical)]);
 return variants.filter(v=>typeof v==='string').map(v=>v.trim()).filter(v=>{
  const key=normalizedWording(v);if(!key||seen.has(key))return false;seen.add(key);return true;
 });
}
function questionWording(row,mode){
 if(mode!=='paraphrase')return String(row.Question).trim();
 const variants=alternateWordings(row);
 if(!variants.length)throw Error('No alternative paraphrased wording is available for this question.');
 return variants[Math.floor(Math.random()*variants.length)];
}
/** Update the currently displayed question when the wording selector changes.
    Keep the same QA record, start entity, answers, graph, and navigation route. */
function applyWording(mode,notify=false){
 if(!state.question)return false;
 let shown;
 try{shown=questionWording(state.question,mode);}
 catch(err){
   if(notify)status('This question has no distinct paraphrase. Try Get random question to sample another.',true);
   return false;
 }
 state.wording=mode;
 state.questionWording=shown;
 text($('question-text'),shown);
 text($('wording-chip'),mode==='paraphrase'?'Random paraphrase':'Canonical');
 if(mode==='paraphrase'){
   const candidates=alternateWordings(state.question);
   text($('wording-note'),`Paraphrase ${candidates.indexOf(shown)+1} of ${candidates.length} distinct alternative(s) · from Question-Paraphrased. The original wording appears after submission.`);
 }else{
   text($('wording-note'),'Original wording from the released Question column.');
 }
 if(notify)status(mode==='paraphrase'?'Showing a distinct paraphrased version of this question.':'Showing the original canonical question.');
 return true;
}
function referencePaths(row){const p=asList(row.Paths);if(!p.length)return[];if(Array.isArray(p[0])&&p[0].length===3&&!Array.isArray(p[0][0]))return [p];if(Array.isArray(p[0])&&Array.isArray(p[0][0]))return p.filter(path=>Array.isArray(path)&&path.every(step=>Array.isArray(step)&&step.length===3));return[];}
function answers(row){const item=row['Answer-Entity'];return Array.isArray(item)?item.map(String):typeof item==='string'&&item.trim().startsWith('[')?asList(item).map(String):item!=null?[String(item).trim()]:[];}
function chainCandidates(row){
 let value=row['Path-Key'];if(value==null||value==='')return [];
 // Released relation paths can be strings of the form P50->P27 or lists.
 if(Array.isArray(value)){
  if(value.every(v=>typeof v==='string'&&!v.includes('->')))return [value];
  return value.flatMap(v=>typeof v==='string'?[v.split(/\s*->\s*/).filter(Boolean)]:Array.isArray(v)?[v]:[]);
 }
 const s=String(value).trim();
 if(s.startsWith('[')){
  const parsed=asList(s);
  if(parsed.length&&parsed.every(x=>typeof x==='string'&&!x.includes('->')))return[parsed.map(String)];
  return parsed.flatMap(x=>Array.isArray(x)?[x.map(String)]:typeof x==='string'?[x.split(/\s*->\s*/).filter(Boolean)]:[]);
 }
 return [s.split(/\s*->\s*/).map(v=>v.trim()).filter(Boolean)];
}
function sampleSplit(row,desired){return desired==='all'||String(row.SplitLabel||'').trim().toLowerCase()===desired;}
// Cache Dataset Viewer responses per page session. A bounded reservoir of
// eligible rows lets many questions be sampled without another API request.
// Kinship, SA and MA have independent pools, and each benchmark partition is
// kept separate. Canonical/paraphrased selection shares the same source rows.
const HF_QA_BATCH_SIZE=64;
const HF_QA_CACHE_LIMIT=256;
const hfConfigCache=new Map();
const hfQaPools=new Map();
async function configsFor(d){
 const key=`${d.dataset}/${d.config}`;
 if(!hfConfigCache.has(key)){
  const pending=(async()=>{
   const data=await fetchJson(paramUrl('splits',{dataset:d.dataset}));
   if(!data.splits?.length)throw Error('Dataset Viewer returned no QA configurations');
   const named=data.splits.find(x=>x.config===d.config)||data.splits.find(x=>x.config?.toLowerCase()===d.config.toLowerCase());
   if(!named)throw Error(`The ${d.config} configuration is not available from the Dataset Viewer.`);
   return {config:named.config,split:named.split};
  })();
  hfConfigCache.set(key,pending);
  pending.catch(()=>{if(hfConfigCache.get(key)===pending)hfConfigCache.delete(key);});
 }
 return hfConfigCache.get(key);
}
function qaPool(d,split){
 const key=`${d.dataset}/${d.config}/${split}`;
 if(!hfQaPools.has(key))hfQaPools.set(key,{rows:[],known:new Set(),filterCount:null,filterDisabled:false,rowCount:null});
 return hfQaPools.get(key);
}
function addQaRows(pool,items,split){
 for(const item of items||[]){
  const row=item?.row;
  if(!row?.Question||!row['Source-Entity']||!answers(row).length||!sampleSplit(row,split))continue;
  // row_idx is the stable index returned by the Dataset Viewer. Fall back to
  // a content key for providers that omit it.
  const key=item.row_idx!=null?`row:${item.row_idx}`:`qa:${row['Question-Number']??''}:${row['Source-Entity']}:${row.Question}`;
  if(pool.known.has(key))continue;
  pool.rows.push({key,row,used:false});
  pool.known.add(key);
 }
 // Prevent unbounded growth on long-running tabs. Unused questions have
 // priority over questions already served when trimming the buffer.
 if(pool.rows.length>HF_QA_CACHE_LIMIT){
  const excess=pool.rows.length-HF_QA_CACHE_LIMIT;
  const ordered=[...pool.rows.filter(x=>x.used),...pool.rows.filter(x=>!x.used)];
  const discard=new Set(ordered.slice(0,excess));
  pool.rows=pool.rows.filter(x=>!discard.has(x));
  for(const item of discard)pool.known.delete(item.key);
 }
}
function takeCachedQa(pool,wording,allowRepeat=false){
 let eligible=pool.rows.filter(item=>!item.used&&(wording!=='paraphrase'||alternateWordings(item.row).length>0));
 if(!eligible.length&&allowRepeat){
  // A very small QA split can run out of previously unseen questions.
  // Allow repeats after attempting new pages rather than failing permanently.
  eligible=pool.rows.filter(item=>wording!=='paraphrase'||alternateWordings(item.row).length>0);
  for(const item of eligible)item.used=false;
 }
 if(!eligible.length)return null;
 const chosen=eligible[Math.floor(Math.random()*eligible.length)];
 chosen.used=true;
 return chosen.row;
}
function randomBatchOffset(total,size){
 return Math.floor(Math.random()*(Math.max(0,total-size)+1));
}
async function fillQaPool(pool,base,split){
 if(split!=='all'&&!pool.filterDisabled){
  const where=`"SplitLabel"='${split}'`;
  try{
   if(pool.filterCount===null){
    const first=await fetchJson(paramUrl('filter',{...base,where,offset:0,length:1}));
    pool.filterCount=Number(first.num_rows_total||0);
    addQaRows(pool,first.rows,split);
   }
   if(pool.filterCount>0){
    const size=Math.min(HF_QA_BATCH_SIZE,pool.filterCount);
    const offset=randomBatchOffset(pool.filterCount,size);
    const batch=await fetchJson(paramUrl('filter',{...base,where,offset,length:size}));
    addQaRows(pool,batch.rows,split);
    return;
   }
  }catch(e){
   // Some Dataset Viewer configurations do not support /filter. Cache that
   // capability result and use /rows instead of retrying /filter each time.
   pool.filterDisabled=true;
   console.info('Dataset Viewer filter unavailable, sampling rows instead',e);
  }
 }
 if(pool.rowCount===null){
  const first=await fetchJson(paramUrl('rows',{...base,offset:0,length:1}));
  pool.rowCount=Number(first.num_rows_total||0);
  addQaRows(pool,first.rows,split);
 }
 if(pool.rowCount<1)throw Error('The selected QA configuration contains no rows.');
 // Fallback samples larger blocks because the benchmark SplitLabel may be
 // sparsely distributed in the API's underlying CSV row order.
 const size=Math.min(100,pool.rowCount);
 const offset=randomBatchOffset(pool.rowCount,size);
 const batch=await fetchJson(paramUrl('rows',{...base,offset,length:size}));
 addQaRows(pool,batch.rows,split);
}
async function randomQuestion(d,split,wording='canonical'){
 if(d.key==='metaqa'){
  const reply=await askWorker({type:'sample-metaqa',key:d.key,url:METAQA_ARCHIVE,split},progress=>{
   if(progress.message)status(progress.message);
  });
  return reply.row;
 }
 const pool=qaPool(d,split);
 const cached=takeCachedQa(pool,wording);
 if(cached)return cached;
 // Once all rows in a small partition have been served, allow reuse without
 // unnecessary requests to the Dataset Viewer.
 if(pool.filterCount!==null&&pool.filterCount>0&&pool.known.size>=pool.filterCount){
  const repeated=takeCachedQa(pool,wording,true);
  if(repeated)return repeated;
 }
 if(split==='all'&&pool.rowCount!==null&&pool.known.size>=pool.rowCount){
  const repeated=takeCachedQa(pool,wording,true);
  if(repeated)return repeated;
 }
 const config=await configsFor(d);
 const base={dataset:d.dataset,config:config.config,split:config.split};
 // Fetch one response page, then draw questions locally until the cached
 // candidates have been used. Refill when needed without repeating an API
 // call for every new question. Keep prior records for alternate wordings.
 for(let attempt=0;attempt<8;attempt++){
  await fillQaPool(pool,base,split);
  const row=takeCachedQa(pool,wording);
  if(row)return row;
 }
 const repeated=takeCachedQa(pool,wording,true);
 if(repeated)return repeated;
 throw Error(`No valid ${split} ${wording==='paraphrase'?'paraphrased ':''}question found after sampling. Try “Any split” or another partition.`);
}
function parseCsv(str){let all=[],line=[],field='',q=false;for(let i=0;i<str.length;i++){const c=str[i];if(c==='"'){if(q&&str[i+1]==='"'){field+='"';i++;}else q=!q;}else if(c===','&&!q){line.push(field);field='';}else if((c==='\n'||c==='\r')&&!q){if(c==='\r'&&str[i+1]==='\n')i++;line.push(field);all.push(line);field='';line=[];}else field+=c;}if(field||line.length){line.push(field);all.push(line);}return all;}
async function fetchMetadata(){
 if(state.metaReady)return;const d=DATA.sa;
 const sources=[['metadata/relation_data.csv','Property','Title',state.mquakeRelationLabels],['metadata/node_data.csv','QID','Title',state.mquakeNodeLabels]];
 await Promise.all(sources.map(async([path,key,title,map])=>{try{const res=await fetch(makeUrl(d,path));if(!res.ok)throw Error(`HTTP ${res.status}`);const content=await res.text();const records=parseCsv(content);const names=records.shift().map(x=>x.trim());const idx=names.indexOf(key),label=names.indexOf(title);if(idx===-1||label===-1)throw Error(`Missing ${key} / ${title} columns`);for(const row of records){const k=row[idx]?.trim(),v=row[label]?.trim();if(k&&v)map.set(k,v);}}catch(err){console.info('Optional name metadata not loaded',path,err);}}));state.metaReady=true;
}
worker.onmessage=(event)=>{
 const msg=event.data;const job=state.pending.get(msg.id);if(!job)return;
 if(msg.type==='progress'){if(job.progress)job.progress(msg);return;}
 state.pending.delete(msg.id);
 if(msg.type==='error')job.reject(Error(msg.message));else job.resolve(msg);
};
function askWorker(message,progress){return new Promise((resolve,reject)=>{const id=++state.workerReq;state.pending.set(id,{resolve,reject,progress});worker.postMessage({...message,id});});}
function toggleBusy(busy){
 $('new-question').disabled=busy;
 document.querySelectorAll('input[name=dataset],#split').forEach(e=>e.disabled=busy);
 $('wording').disabled=busy||document.querySelector('input[name=dataset]:checked')?.value==='metaqa';
}
function updateWordingChoice(){
 const isMetaqa=document.querySelector('input[name=dataset]:checked')?.value==='metaqa';
 if(isMetaqa)$('wording').value='canonical';
 $('wording').disabled=isMetaqa;
 $('wording').title=isMetaqa?'The released encoded MetaQA collection does not contain alternative paraphrases.':'';
}
document.querySelectorAll('input[name=dataset]').forEach(input=>input.addEventListener('change',updateWordingChoice));
updateWordingChoice();
async function launch(){
 const myTask=++state.task;
 const selected=document.querySelector('input[name=dataset]:checked').value;
 state.dataset=selected;
 if(state.dataset==='sa'||state.dataset==='ma'){state.nodeLabels=state.mquakeNodeLabels;state.relationLabels=state.mquakeRelationLabels;}
 else if(state.dataset==='metaqa'){state.nodeLabels=state.metaqaNodeLabels;state.relationLabels=state.metaqaRelationLabels;}
 state.split=$('split').value;state.wording=$('wording').value;state.question=null;state.questionWording='';state.finished=false;state.route=[];state.activeRel=null;
 $('play').classList.add('hidden');toggleBusy(true);status(state.dataset==='metaqa'?'Sampling an encoded MetaQA question from its public GCS archive…':'Finding a random question from the published dataset…');
 try{
  const row=await randomQuestion(dataset(),state.split,state.wording);
  if(myTask!==state.task)return;
  state.question=row;state.source=String(row['Source-Entity']).trim();state.current=state.source;
  if(!applyWording(state.wording))throw Error('Selected question has no alternative paraphrase.');
  text($('dataset-chip'),dataset().title);text($('split-chip'),state.split==='all'?'Any split':`${state.split[0].toUpperCase()}${state.split.slice(1)} partition`);
  renderNamedOnly($('question-source'),'entity',state.source);
  $('play').classList.remove('hidden');$('result').classList.add('hidden');$('game-content').classList.add('hidden');
  $('graph-prompt').classList.remove('hidden');
  text($('graph-help'),state.dataset==='kinship'?'Kinship has a small directed graph. It loads quickly from the published dataset.':state.dataset==='metaqa'?'The encoded MetaQA graph shares the GCS archive already downloaded for this question. Reverse traversal is available as in the original MetaQA navigation setting.':'MQuAKE-ST uses a large directed graph. The first download can take time and browser memory, especially on mobile. The two MQuAKE modes share this graph.');
  status('Question loaded. Prepare the graph to start navigating.');
  $('play').scrollIntoView({block:'start',behavior:'smooth'});
  if(state.graphReady.has(dataset().key)){
    if(dataset().key==='mquake'){state.nodeLabels=state.mquakeNodeLabels;state.relationLabels=state.mquakeRelationLabels;}
    if(dataset().key==='metaqa'){state.nodeLabels=state.metaqaNodeLabels;state.relationLabels=state.metaqaRelationLabels;}
    await enterGame();
  }
 }catch(err){status(`Could not load a question: ${err.message}`,true);}finally{if(myTask===state.task)toggleBusy(false);}
}
function renderNamedOnly(parent,kind,id){parent.replaceChildren();named(parent,kind,id);}
async function loadGraph(){
 const d=dataset();if(state.graphReady.has(d.key)){
   if(d.key==='mquake'){state.nodeLabels=state.mquakeNodeLabels;state.relationLabels=state.mquakeRelationLabels;}
   if(d.key==='metaqa'){state.nodeLabels=state.metaqaNodeLabels;state.relationLabels=state.metaqaRelationLabels;}
   await enterGame();return;
 }
 const button=$('load-graph');button.disabled=true;
 text(button,'Loading…');
 text($('graph-help'),'Downloading the public graph and building a directed adjacency index locally. Please keep this tab open.');
 const thisTask=state.task;
 try{
  const loaded=await askWorker(d.key==='metaqa'?{type:'load-metaqa',key:d.key,url:METAQA_ARCHIVE}:{type:'load',key:d.key,url:makeUrl(d,d.graph)},p=>{
    if(thisTask!==state.task)return;
    if(p.message)text($('graph-help'),p.message);
    else{const count=p.edges.toLocaleString();text($('graph-help'),`Indexed ${count} edges… ${p.expected ? Math.min(100,Math.round(p.bytes/p.expected*100))+'% downloaded' : 'Working in your browser'}.`);}
  });
  if(d.key==='metaqa'){
    state.metaqaNodeLabels=new Map(loaded.nodes||[]);
    state.metaqaRelationLabels=new Map(loaded.relations||[]);
    state.nodeLabels=state.metaqaNodeLabels;
    state.relationLabels=state.metaqaRelationLabels;
    renderNamedOnly($('question-source'),'entity',state.source);
  }
  state.graphReady.add(d.key);
  if(d.key==='mquake'){
    text($('graph-help'),'Graph loaded. Loading optional readable Wikidata labels…');
    await fetchMetadata();
    state.nodeLabels=state.mquakeNodeLabels;
    state.relationLabels=state.mquakeRelationLabels;
  }
  if(thisTask===state.task)await enterGame();
 }catch(err){text($('graph-help'),`Graph could not be loaded: ${err.message}. Confirm the dataset is public and that cross-origin browser requests are allowed.`);}
 finally{button.disabled=false;text(button,'Load graph');}
}
async function enterGame(){if(!state.question)return;$('graph-prompt').classList.add('hidden');$('game-content').classList.remove('hidden');$('result').classList.add('hidden');state.finished=false;state.route=[];state.current=state.source;state.activeRel=null;renderState();await showRelations();}
function renderState(){
 renderNamedOnly($('current-entity'),'entity',state.current);text($('steps-chip'),`${state.route.length} step${state.route.length===1?'':'s'} taken`);
 const route=$('route-list');route.replaceChildren();
 let li=el('li','route-step');append(li,el('span','route-rel','START ENTITY'));named(li,'entity',state.source);route.append(li);
 for(const step of state.route){li=el('li','route-step');append(li,el('span','route-rel',`↓ ${labelRelation(step.relation)}${labelRelation(step.relation)===step.relation?'':` (${step.relation})`}`));named(li,'entity',step.to);route.append(li);}
 $('undo').disabled=state.route.length===0||state.finished;
 $('restart').disabled=state.route.length===0||state.finished;
 $('submit').disabled=state.finished;
 $('give-up').disabled=state.finished;
}
async function showRelations(){
 if(state.finished)return;
 $('neighbor-box').classList.add('hidden');state.activeRel=null;
 const task=state.task,node=state.current;
 text($('action-total'),'Loading edges…');$('relation-list').replaceChildren();
 try{
  const data=await askWorker({type:'relations',key:dataset().key,node});
  if(task!==state.task||node!==state.current||state.finished)return;
  state.relationItems=data.items;filterRelations();
  navStatus(state.route.length>=MAX_STEPS?'Safety limit reached. Undo a step or submit your answer.':data.total?`Choose from ${data.total.toLocaleString()} outgoing edge(s).`:'This entity has no outgoing edges. Undo or submit your answer.');
 }catch(err){navStatus(`Cannot read graph actions: ${err.message}`,true);}
}
function filterRelations(){
 const list=$('relation-list');list.replaceChildren();
 const needle=$('relation-search').value.trim().toLowerCase();
 const values=state.relationItems.filter(i=>`${i.relation} ${labelRelation(i.relation)}`.toLowerCase().includes(needle));
 text($('action-total'),`${values.length} relation${values.length===1?'':'s'}`);
 if(state.route.length>=MAX_STEPS){list.append(el('div','empty',`Reached the ${MAX_STEPS}-step safety limit. Undo a step or submit.`));return;}
 if(!values.length){list.append(el('div','empty','No matching outgoing relations.'));return;}
 for(const item of values){
  const button=el('button','rel-item'+(state.activeRel===item.relation?' active':''));button.type='button';
  const left=el('span','name');append(left,el('span','',labelRelation(item.relation)));
  if(labelRelation(item.relation)!==item.relation)append(left,el('span','nodeid',` (${item.relation})`));
  append(button,left,el('small','',`${item.count} neighbor${item.count===1?'':'s'} ›`));
  button.addEventListener('click',()=>openRelation(item.relation));list.append(button);
 }
}
async function openRelation(relation){
 if(state.finished)return;state.activeRel=relation;state.neighborOffset=0;filterRelations();
 $('neighbor-box').classList.remove('hidden');text($('neighbor-heading'),`Via ${labelRelation(relation)}`);
 $('neighbor-search').value='';$('neighbor-search').placeholder=state.dataset==='kinship'?'Filter neighboring people…':state.dataset==='metaqa'?'Filter neighboring names or IDs…':'Filter neighboring QIDs…';
 await showNeighbors();
}
async function showNeighbors(){
 const relation=state.activeRel;if(!relation||state.finished)return;
 const node=state.current,task=state.task,offset=state.neighborOffset,q=$('neighbor-search').value.trim();
 const list=$('neighbor-list');list.replaceChildren(el('div','empty','Loading neighbors…'));
 try{
  const data=await askWorker({type:'neighbors',key:dataset().key,node,relation,offset,limit:PAGE_SIZE,query:q});
  if(task!==state.task||state.activeRel!==relation||node!==state.current||state.finished||q!==$('neighbor-search').value.trim())return;
  list.replaceChildren();state.neighborCount=data.total;
  text($('neighbor-count'),`${data.total?offset+1:0}–${Math.min(offset+PAGE_SIZE,data.total)} / ${data.total}`);
  $('neighbor-prev').disabled=offset===0;$('neighbor-next').disabled=offset+PAGE_SIZE>=data.total;
  if(!data.items.length){list.append(el('div','empty','No neighbors match your search.'));return;}
  for(const id of data.items){
   const item=el('div','neighbor-row');
   const button=el('button','neighbor');button.type='button';
   named(button,'entity',id,false);
   button.addEventListener('click',()=>takeStep(relation,id));
   item.append(button);
   const url=wikiLink('entity',id);
   if(url){const link=el('a','neighbor-external','↗');link.href=url;link.target='_blank';link.rel='noopener noreferrer';link.title=`Open ${id} on Wikidata`;item.append(link);}
   list.append(item);
  }
 }catch(err){list.replaceChildren(el('div','empty',`Could not load neighbors: ${err.message}`));}
}
async function takeStep(relation,to){
 if(state.finished||state.route.length>=MAX_STEPS)return;
 state.route.push({from:state.current,relation,to});state.current=to;state.activeRel=null;
 $('relation-search').value='';renderState();await showRelations();
}
async function undo(){if(!state.route.length||state.finished)return;let last=state.route.pop();state.current=last.from;renderState();await showRelations();}
async function restart(){if(state.finished)return;state.route=[];state.current=state.source;state.activeRel=null;renderState();await showRelations();}
function isReferenceMatch(row,route){
 const paths=referencePaths(row);const chains=chainCandidates(row);
 const triples=route.map(step=>[step.from,step.relation,step.to]);
 if(paths.length){return {kind:'entity path',matched:paths.some(path=>path.length===triples.length&&path.every((triple,i)=>triple.every((field,j)=>String(field)===String(triples[i][j])))),possible:true};}
 if(chains.length){const rel=route.map(s=>s.relation);return{kind:'relation chain',matched:chains.some(chain=>chain.length===rel.length&&chain.every((r,i)=>String(r)===rel[i])),possible:true};}
 return{kind:'reference path',matched:false,possible:false};
}
function showResult(giveUp){
 if(!state.question||state.finished)return;
 state.finished=true;
 const row=state.question;
 const gold=answers(row),ok=!giveUp&&gold.includes(state.current);
 const fidelity=isReferenceMatch(row,state.route);
 $('neighbor-box').classList.add('hidden');renderState();filterRelations();
 $('result').classList.remove('hidden');
 text($('result-heading'),giveUp?'Reference revealed':ok?'You found a valid answer!':'Not a released answer');
 text($('answer-badge'),giveUp?'Given up':ok?'Answer correct':'Answer incorrect');
 $('answer-badge').className='result-badge '+(!giveUp&&ok?'success':giveUp?'':'fail');
 text($('result-description'),giveUp?'Here are the released answers and reference evidence for this question.':ok?'Your terminal entity belongs to the released answer set. Your navigation path is evaluated independently below.':'Your terminal entity does not match the released answer set. You can try the same question again.');
 renderNamedOnly($('result-terminal'),'entity',state.current);
 const target=$('result-answers');target.replaceChildren();gold.forEach((v,i)=>{if(i)target.append(el('span','',', '));named(target,'entity',v);});
 const assess=$('path-assessment');text(assess,(!giveUp&&fidelity.possible)?fidelity.matched?`✓ Your route matches an annotated ${fidelity.kind}.`:`Your route does not exactly match the released ${fidelity.kind}. That does not prove your path is invalid.`:fidelity.possible?`Released ${fidelity.kind} available below. No attempt was scored.`:'No reference path or relation chain is available for this question.');
 const detail=$('reference-content');detail.replaceChildren();
 const p=el('p','',`Annotated hop count: ${row.Hops??'not provided'}. You took ${state.route.length} step${state.route.length===1?'':'s'}.`);detail.append(p);if(state.wording==='paraphrase')detail.append(el('p','',`Canonical question: ${row.Question}`));
 const paths=referencePaths(row),chains=chainCandidates(row);
 if(paths.length){for(const [i,path] of paths.slice(0,5).entries()){const item=el('p','',`Reference ${i+1}: `);for(let j=0;j<path.length;j++){if(j===0)named(item,'entity',String(path[j][0]));append(item,el('span','',` —[${labelRelation(String(path[j][1]))}]→ `));named(item,'entity',String(path[j][2]));}detail.append(item);}}
 else if(chains.length){for(const [i,chain]of chains.slice(0,5).entries()){let item=el('p','',`Annotated relation chain ${i+1}: `);chain.forEach((r,j)=>{if(j)item.append(el('span','',` → `));named(item,'relation',String(r));});detail.append(item);}if(state.dataset==='ma')detail.append(el('p','',`Multi Answer does not provide a single entity-level reference path. These are released relation-chain annotations.`));}
 else detail.append(el('p','',state.dataset==='metaqa'?'MetaQA provides answer supervision but no released reference navigation paths. Endpoint correctness is evaluated, but exact path fidelity is unavailable.':'No entity-level path or relation chain was provided in this record.')); 
 const ref=$('result').querySelector('details');ref.open=false;
 $('result').scrollIntoView({block:'start',behavior:'smooth'});
}
async function retry(){state.finished=false;state.route=[];state.current=state.source;state.activeRel=null;$('result').classList.add('hidden');$('game-content').classList.remove('hidden');renderState();await showRelations();$('play').scrollIntoView({block:'start',behavior:'smooth'});}
$('wording').addEventListener('change',()=>{
 // A selection made before loading a challenge is applied by launch().
 if(!state.question)return;
 const prior=state.wording;
 if(!applyWording($('wording').value,true))$('wording').value=prior;
});
$('new-question').addEventListener('click',launch);
$('load-graph').addEventListener('click',loadGraph);
$('undo').addEventListener('click',undo);$('restart').addEventListener('click',restart);
$('relation-search').addEventListener('input',filterRelations);
$('neighbor-search').addEventListener('input',()=>{state.neighborOffset=0;showNeighbors();});
$('neighbor-prev').addEventListener('click',()=>{state.neighborOffset=Math.max(0,state.neighborOffset-PAGE_SIZE);showNeighbors();});
$('neighbor-next').addEventListener('click',()=>{state.neighborOffset+=PAGE_SIZE;showNeighbors();});
$('close-neighbors').addEventListener('click',()=>{state.activeRel=null;$('neighbor-box').classList.add('hidden');filterRelations();});
$('submit').addEventListener('click',()=>showResult(false));$('give-up').addEventListener('click',()=>showResult(true));$('retry').addEventListener('click',retry);$('another').addEventListener('click',launch);
})();