cyborg-hunter 0.7.0 → 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/cli/ingest.js CHANGED
@@ -14,8 +14,9 @@ import { readFileSync, readdirSync } from 'fs';
14
14
  import { join, extname } from 'path';
15
15
  import { gunzipSync } from 'zlib';
16
16
  import Papa from 'papaparse';
17
- import { TRIAL_REPORT_FIELDS } from '../shared/schema.js';
18
17
  import { sanitizeId } from '../shared/constants.js';
18
+ import { getByPath } from '../shared/paths.js';
19
+ import { extractIntegrityData, ruleChronologicalCompare } from './extract-core.js';
19
20
 
20
21
  // Replay artifacts saved by the replay extension:
21
22
  // <sanitizedPid>-replay-<sessionStartEpochMs>.json[.gz]
@@ -297,541 +298,15 @@ function attachReplayArtifacts(participants, config, warnings) {
297
298
  }
298
299
  }
299
300
 
300
- // Resolves a possibly-dotted field path against an object (0.6.1).
301
- // A flat key wins over a dotted walk, so data that literally contains a
302
- // "metadata.sessionId" column stays addressable; otherwise the path is
303
- // walked one segment at a time. Returns undefined when any segment is
304
- // missing or a non-object is hit mid-path.
305
- export function getByPath(obj, path) {
306
- if (obj == null || typeof path !== 'string' || path.length === 0) return undefined;
307
- if (Object.prototype.hasOwnProperty.call(obj, path)) return obj[path];
308
- if (!path.includes('.')) return undefined;
309
- let cur = obj;
310
- for (const seg of path.split('.')) {
311
- if (cur == null || typeof cur !== 'object') return undefined;
312
- cur = cur[seg];
313
- }
314
- return cur;
315
- }
316
-
317
- // Extracts integrity trial data from a single participant's raw JSON.
318
- // Returns { participantId, trials, warnings, metadata }.
319
- export function extractIntegrityData(raw, config) {
320
- const warnings = [];
321
- const pidField = config.participantIdField || 'participantId';
322
- const intField = config.integrityField || 'integrity';
323
-
324
- // Determine participant ID — check top level, then metadata sub-object.
325
- // Since 0.6.1 the field supports dot-paths ("metadata.sessionId"); plain
326
- // names keep the historical top-level → metadata fallback. `||` (not `??`)
327
- // preserves the pre-0.6.1 treatment of empty-string IDs as missing.
328
- const participantId = getByPath(raw, pidField) || getByPath(raw.metadata, pidField) || 'unknown';
329
- // An id field that resolves to nothing yields 'unknown'. Silently, that both
330
- // loses the real id AND collides every such file under one 'unknown' bucket
331
- // downstream (see the duplicate-id check in ingest()). Warn so a mistyped
332
- // participantIdField is visible instead of producing an all-'unknown' cohort.
333
- if (participantId === 'unknown') {
334
- warnings.push(
335
- `participantId unresolved (field "${pidField}" not found at top level or in ` +
336
- `metadata) — defaulted to "unknown". Check participantIdField.`
337
- );
338
- }
339
-
340
- let trials = [];
341
-
342
- // Shape 1: { trials: [{ integrity: {...} }, ...] } — jsPsych extension data.
343
- // Each trial has an `integrity` sub-object added by CyborgHunter.endTrial().
344
- // We merge the trial's own fields (ruleId, timestamp, rt, rulePosition, etc.)
345
- // with the integrity sub-object's fields (tabAwayEvents, copyEvents, etc.).
346
- // On a key collision the integrity sub-object wins, since it's the more
347
- // authoritative source for those fields. This preserves both the renderer-
348
- // needed experiment metadata AND the cyborg-hunter signal data on the same
349
- // trial object.
350
- if (Array.isArray(raw.trials)) {
351
- trials = raw.trials
352
- .filter(t => t && t[intField])
353
- .map(t => ({ ...t, ...t[intField] }));
354
- // jsPsych extension data carries trialStart_perfNow per trial (set by the
355
- // wrapper's on_load), so normalization takes the exact-subtraction fast
356
- // path. Without it, renderers see only session-absolute `start` values
357
- // and plot tab-away markers far off the per-trial axis.
358
- normalizeTabAwayTimestamps(trials, raw);
359
- }
360
-
361
- // Phase-trial extension: per-phase integrity reports for
362
- // gallery / post-gallery-query / end-requery phases live on raw.phaseTrials,
363
- // mirroring the per-classification-trial shape so the renderer can place
364
- // gallery mouse trajectories and tab-away events on the same axes.
365
- //
366
- // After merging, sort by (rulePosition, phase-rank, trialNumber) so the
367
- // trajectories grid shows trials in the chronological order each rule was
368
- // actually experienced: gallery → post-gallery query → classification t1..t6.
369
- // End-requery trials carry rulePosition=null and sort to the very end of
370
- // the array, matching their session-end timing.
371
- if (Array.isArray(raw.phaseTrials) && raw.phaseTrials.length > 0) {
372
- const phaseTrials = raw.phaseTrials
373
- .filter(t => t && t[intField])
374
- .map(t => ({ ...t, ...t[intField] }));
375
- normalizeTabAwayTimestamps(phaseTrials, raw);
376
- trials = trials.concat(phaseTrials);
377
- trials.sort(ruleChronologicalCompare);
378
- }
379
-
380
- // Shapes 2 and 3 are ALTERNATIVE top-level layouts, tried only when the
381
- // Shape-1 trials/phaseTrials path produced nothing. Previously the Shape-2
382
- // branch was chained as `else if` off the phaseTrials `if`, so a payload
383
- // carrying both `trials` (real integrity) and `responses` — or even a stray
384
- // empty `responses: []` — had its already-extracted integrity trials clobbered
385
- // (or the participant silently dropped). Gate on trials.length so Shape-1 wins.
386
- if (trials.length === 0) {
387
- // Shape 2: { responses: [{ mouseTrack, tabAwayEvents, ... }] } (legacy).
388
- // Signal data lives directly on the response — no integrity wrapper. We apply
389
- // field name mapping (mouseTrack → mouseEvents).
390
- if (Array.isArray(raw.responses)) {
391
- trials = raw.responses.map((r, i) => mapLegacyFields({
392
- ...r,
393
- _sourceIndex: i
394
- }));
395
- // Normalize tab-away timestamps from session-relative performance.now()
396
- // to trial-relative milliseconds so renderers can plot them on the
397
- // same x-axis as mouseEvents[].t (which is already trial-relative).
398
- normalizeTabAwayTimestamps(trials, raw);
399
- }
400
- // Shape 3: Top-level array of trials. Same merge contract as Shape 1:
401
- // outer trial fields survive, integrity wins on collision, and tab-away
402
- // timestamps get normalized. The spread builds OUR copy, so the
403
- // array-field coercion below mutates that copy — never a caller-owned or
404
- // frozen object (a frozen one would throw instead of coercing). The
405
- // outer-merge also keeps replay pointers (integrityReplayMeta /
406
- // replayFinalizeError ride the outer trial rows) visible to
407
- // attachReplayArtifacts.
408
- else if (Array.isArray(raw)) {
409
- trials = raw.filter(t => t && t[intField]).map(t => ({ ...t, ...t[intField] }));
410
- normalizeTabAwayTimestamps(trials, raw);
411
- }
412
- }
413
-
414
- if (trials.length === 0) {
415
- warnings.push(`No integrity data found (looked for "${intField}" field)`);
416
- }
417
-
418
- // Validate each trial has the minimum required fields from the schema.
419
- // Missing fields get a warning but don't prevent analysis. Array-typed signal
420
- // fields that arrive as a non-array (e.g. a hand-edited `pasteEvents: {}`) are
421
- // coerced to [] — otherwise a downstream `for (const e of trial.pasteEvents)`
422
- // throws "not iterable" and, because report.js has no per-renderer boundary,
423
- // one malformed file aborts the ENTIRE report. Coercing here keeps the run
424
- // alive and localizes the damage to a warning on that trial.
425
- for (const trial of trials) {
426
- const missing = [];
427
- for (const [field, spec] of Object.entries(TRIAL_REPORT_FIELDS)) {
428
- if (spec.required && trial[field] === undefined) {
429
- missing.push(field);
430
- }
431
- if (spec.type === 'array' && trial[field] != null && !Array.isArray(trial[field])) {
432
- warnings.push(
433
- `Trial ${trial.trialId ?? '?'}: field "${field}" is not an array ` +
434
- `(got ${typeof trial[field]}) — coerced to [] to keep analysis running.`
435
- );
436
- trial[field] = [];
437
- }
438
- }
439
- if (missing.length > 0) {
440
- warnings.push(`Trial ${trial.trialId || '?'}: missing fields: ${missing.join(', ')}`);
441
- }
442
- }
443
-
444
- const { session, score } = findSessionData(raw, config);
445
- if (session === null && trials.length > 0) {
446
- warnings.push('No session-level integrity data — some signals unavailable (did the experiment call getSessionReport()?)');
447
- }
448
-
449
- // The jsPsych adapter drops a `cyborgHunterFinalizeError` marker (via
450
- // addProperties) when finalize() throws, so analysts can tell a missing-session
451
- // run apart from a genuine finalize() failure. Surface it as a warning instead
452
- // of leaving it dead in the data behind a generic "no session data" message.
453
- const finalizeError = raw.cyborgHunterFinalizeError
454
- ?? raw.metadata?.cyborgHunterFinalizeError
455
- ?? (Array.isArray(raw.trials)
456
- ? raw.trials.find(t => t?.cyborgHunterFinalizeError)?.cyborgHunterFinalizeError
457
- : undefined);
458
- if (finalizeError) {
459
- warnings.push(`finalize() failed for this participant (cyborgHunterFinalizeError): ${finalizeError} — session data may be incomplete`);
460
- }
461
-
462
- return {
463
- participantId,
464
- trials,
465
- warnings,
466
- metadata: raw.metadata || {},
467
- session,
468
- score,
469
- // Surface a few top-level payload fields that some renderers need but
470
- // that aren't part of the session-level integrity object. Keeping the
471
- // list explicit (rather than exposing `raw` wholesale) avoids future
472
- // renderers silently coupling to payload internals.
473
- galleryStudyMs: Array.isArray(raw.galleryStudyMs) ? raw.galleryStudyMs : null,
474
- postGalleryGuesses: Array.isArray(raw.postGalleryGuesses) ? raw.postGalleryGuesses : null,
475
- // App-written top-level guardFriction wins; otherwise synthesize the guard
476
- // lane from the honeypot's session violation log (which the shipped guard
477
- // extensions actually emit) so the timeline renders for library-only data.
478
- guardFriction: raw.guardFriction ?? (() => {
479
- const v = findGuardViolations(raw);
480
- return v ? { violations: v } : null;
481
- })(),
482
- // Guard-honeypot self-disclosure (visible bait). null when the honeypot
483
- // extension was not used.
484
- honeypot: findHoneypotDisclosure(raw),
485
- };
486
- }
487
-
488
- // True when obj plausibly IS a getSessionReport() output — i.e. it carries at
489
- // least one of the well-known top-level session-report keys (see
490
- // src/core/monitor.js sessionData / getSessionReport()). Guards the
491
- // analyst-supplied sessionIntegrityPath below: `typeof === 'object'` alone
492
- // accepts any object the path happens to resolve to, including a near-miss
493
- // wrapper one level up the tree (e.g. `metadata` instead of
494
- // `metadata.integritySession`), which would otherwise silently zero out every
495
- // downstream signal instead of falling through.
496
- function looksLikeSessionData(obj) {
497
- if (!obj || typeof obj !== 'object') return false;
498
- return ['tabAwaySums', 'hardScore', 'softScore', 'anyHardTriggered', 'trialsCompleted']
499
- .some(key => Object.prototype.hasOwnProperty.call(obj, key));
500
- }
501
-
502
- // Locates session-level integrity data. An analyst-supplied dotted path
503
- // (config.sessionIntegrityPath, 0.6.1) is checked first; then the built-in
504
- // conventions, in priority order:
505
- // 1. raw.metadata.integritySession / integrityScore — the metadata convention.
506
- // 2. Last trial's integritySession / integrityScore — jsPsych addDataToLastTrial (Option A).
507
- // 3. Any trial's integritySession — fallback (Option B).
508
- // 4. raw.cyborgHunter — native top-level location used by raw-DOM
509
- // adopters before they adopt the
510
- // metadata.integritySession mirror. Added 2026-05-26.
511
- // Returns { session, score }, both null if not found.
512
- function findSessionData(raw, config) {
513
- let session = null;
514
- let score = null;
515
-
516
- // 0. Analyst-specified location, e.g. "payload.cyborgHunter" for pipelines
517
- // that nest the getSessionReport() output somewhere non-standard. Falls
518
- // through to the built-in conventions when the path resolves to nothing
519
- // OR to something that doesn't look like a session report (looksLikeSessionData,
520
- // 0.6.1 — a malformed/near-miss path used to be accepted on typeof alone),
521
- // so a partially-migrated cohort still ingests. The score is synthesized
522
- // from the session object below (getSessionReport() embeds it).
523
- const customSession = config?.sessionIntegrityPath
524
- ? getByPath(raw, config.sessionIntegrityPath) : null;
525
- if (looksLikeSessionData(customSession)) {
526
- session = customSession;
527
- }
528
- // 1. metadata convention
529
- else if (raw.metadata?.integritySession) {
530
- session = raw.metadata.integritySession;
531
- score = raw.metadata.integrityScore || null;
532
- }
533
- // 2. jsPsych addDataToLastTrial convention (Option A)
534
- else if (Array.isArray(raw.trials) && raw.trials.length > 0 &&
535
- raw.trials[raw.trials.length - 1]?.integritySession) {
536
- const last = raw.trials[raw.trials.length - 1];
537
- session = last.integritySession;
538
- score = last.integrityScore || null;
539
- }
540
- // 3. Any-trial fallback
541
- else if (Array.isArray(raw.trials) && raw.trials.find(x => x?.integritySession)) {
542
- const t = raw.trials.find(x => x?.integritySession);
543
- session = t.integritySession;
544
- score = t.integrityScore || null;
545
- }
546
- // 4. Native top-level location. An early raw-DOM adopter app
547
- // writes the getSessionReport() output directly to `raw.cyborgHunter`.
548
- // Sessions saved before the metadata.integritySession mirror landed
549
- // (2026-05-24) have ONLY this top-level field. Without
550
- // this fallback, the renderer can't find windowPositions and falls back
551
- // to stale top-level metadata.windowWidth — visible as a misaligned
552
- // "browser window" dashed rectangle in the trajectory PNGs.
553
- else if (raw.cyborgHunter && typeof raw.cyborgHunter === 'object') {
554
- session = raw.cyborgHunter;
555
- score = null;
556
- }
557
-
558
- // When no separate integrityScore blob was saved, synthesize the authoritative
559
- // score from the session object itself. getSessionReport() embeds the scoring
560
- // summary (hardScore/softScore/anyHardTriggered/softScoreThreshold/
561
- // trialsCompleted) directly in the session report, so integritySession-only
562
- // and raw.cyborgHunter payloads already carry it. Without this, summary.js
563
- // falls back to a per-trial `trialHits > 0` rule that OVERSTATES hard flags
564
- // (any single hit flags hard, ignoring the count threshold).
565
- if (!score && session) score = scoreFromSession(session);
566
-
567
- return { session, score };
568
- }
569
-
570
- // Builds a { hardScore, softScore, anyHardTriggered, softScoreThreshold,
571
- // trialsCompleted } score object from a session report that embeds those
572
- // fields. Returns null if the session carries no scoring fields at all.
573
- function scoreFromSession(session) {
574
- if (!session || typeof session !== 'object') return null;
575
- const hasScore = session.softScore !== undefined
576
- || session.anyHardTriggered !== undefined
577
- || session.hardScore !== undefined;
578
- if (!hasScore) return null;
579
- // Derive anyHardTriggered from hardScore when the boolean is absent (some
580
- // session payloads carry the per-signal hardScore map but not the rolled-up
581
- // flag). Without this, summary.js would fall back to its per-trial trialHits>0
582
- // rule, which overstates hard flags.
583
- let anyHardTriggered = session.anyHardTriggered;
584
- if (anyHardTriggered === undefined && session.hardScore && typeof session.hardScore === 'object') {
585
- anyHardTriggered = Object.values(session.hardScore).some(s => s && s.triggered);
586
- }
587
- return {
588
- hardScore: session.hardScore,
589
- softScore: session.softScore,
590
- anyHardTriggered,
591
- softScoreThreshold: session.softScoreThreshold,
592
- trialsCompleted: session.trialsCompleted,
593
- };
594
- }
595
-
596
- // Normalizes guard-honeypot violation evidence into the { violations: [...] }
597
- // shape the session-timeline renderer expects under `guardFriction`. The
598
- // honeypot extension writes the friction violation log (it subscribes to
599
- // guard-friction.onViolation) as a STRINGIFIED `guard_assistance_violations_session`
600
- // field via addProperties, so on a saved payload it lands on the trial rows
601
- // (or metadata). Each entry is { reason, start, end, duration, in_progress };
602
- // the renderer keys off `t` (perfNow ms), so map start → t. Apps that hand-write
603
- // a top-level `raw.guardFriction` object still take precedence over this.
604
- function findGuardViolations(raw) {
605
- const parseArr = (v) => {
606
- if (Array.isArray(v)) return v;
607
- if (typeof v === 'string') { try { return JSON.parse(v); } catch { return null; } }
608
- return null;
609
- };
610
- // True when a candidate value parses to at least one REAL violation (an entry
611
- // with a numeric start). Used to scan trials for real data rather than the
612
- // first trial that merely has a truthy field.
613
- const hasReal = (v) => {
614
- const a = parseArr(v);
615
- return Array.isArray(a) && a.some(x => x && typeof x.start === 'number');
616
- };
617
- const candidates = [
618
- raw.metadata?.guard_assistance_violations_session,
619
- Array.isArray(raw.trials) && raw.trials.length > 0
620
- ? raw.trials[raw.trials.length - 1]?.guard_assistance_violations_session : null,
621
- // Scan ALL trials for one carrying REAL violations — not just the first trial
622
- // with a truthy field. A truthy-but-empty "[]" on an earlier trial used to
623
- // make .find() lock on, then the outer loop fell through to candidate 4 and
624
- // a later trial's real violations were never reached. Not live for the
625
- // shipped honeypot (it stamps this field identically on every trial), but a
626
- // latent bug for any non-uniform / merged producer. (Sol R2 candidate-3.)
627
- Array.isArray(raw.trials)
628
- ? raw.trials.map(t => t?.guard_assistance_violations_session).find(hasReal)
629
- : null,
630
- raw.guard_assistance_violations_session,
631
- ];
632
- for (const c of candidates) {
633
- const arr = parseArr(c);
634
- if (Array.isArray(arr)) {
635
- const violations = arr
636
- .filter(v => v && typeof v.start === 'number')
637
- .map(v => ({ t: v.start, reason: v.reason || 'unknown', phase: 'unknown', duration_ms: v.duration }));
638
- // Fall through to the next source on an empty/violation-less candidate
639
- // instead of locking onto it — otherwise an empty placeholder (e.g. a
640
- // metadata mirror set to []) would shadow real violations on the trial rows.
641
- if (violations.length > 0) return violations;
642
- }
643
- }
644
- return null;
645
- }
646
-
647
- // Surfaces the guard-honeypot self-disclosure fields (the visible bait: an "I used
648
- // AI" checkbox and a free-text box). The honeypot writes ai_use_session /
649
- // ai_report_session via addProperties and per-trial ai_use / ai_report via
650
- // on_finish. The runtime emits them but no analyzer consumed them — this lifts
651
- // them onto the participant so summary.csv / triage can surface them. Returns
652
- // null when the honeypot was not used (fields absent everywhere).
653
- function findHoneypotDisclosure(raw) {
654
- // Aggregate across every source rather than returning the first that carries a
655
- // honeypot key. addProperties stamps ai_use_session=false / ai_report_session=''
656
- // onto ALL trials, so the final trial is usually blank-but-present; returning it
657
- // first would mask a positive disclosure recorded on an earlier trial. A
658
- // disclosure anywhere (ticked checkbox OR non-empty report) makes the whole
659
- // participant positive.
660
- const sources = [];
661
- if (raw.metadata && typeof raw.metadata === 'object') sources.push(raw.metadata);
662
- if (Array.isArray(raw.trials)) sources.push(...raw.trials);
663
- sources.push(raw);
664
-
665
- // Accept the checkbox as a real boolean OR its CSV string form ("true"/"false"
666
- // survive Papa's dynamicTyping in some pipelines). Crucially, presence requires
667
- // an actual checkbox value or non-empty report text — NOT mere key existence:
668
- // shared-header CSVs give honeypot-less participants blank ('' / null) cells,
669
- // and counting those as "present" would manufacture a `no` instead of the
670
- // documented empty (honeypot-absent) state.
671
- const isBool = (v) => v === true || v === false || v === 'true' || v === 'false';
672
- const isTrue = (v) => v === true || v === 'true';
673
- const reportText = (v) => (typeof v === 'string' ? v : '');
674
-
675
- let present = false;
676
- let ticked = false;
677
- let sessionReport = ''; // authoritative *_session report text (prefer longest)
678
- let trialReport = ''; // longest per-trial report snapshot
679
- for (const obj of sources) {
680
- if (!obj || typeof obj !== 'object') continue;
681
- const repS = reportText(obj.ai_report_session);
682
- const repT = reportText(obj.ai_report);
683
- const hasDisclosureField = isBool(obj.ai_use_session) || isBool(obj.ai_use)
684
- || repS.length > 0 || repT.length > 0;
685
- if (!hasDisclosureField) continue;
686
- present = true;
687
- if (isTrue(obj.ai_use_session) || isTrue(obj.ai_use)) ticked = true;
688
- if (repS.length > sessionReport.length) sessionReport = repS;
689
- if (repT.length > trialReport.length) trialReport = repT;
690
- }
691
- if (!present) return null;
692
- const aiReport = sessionReport || trialReport;
693
- // aiUse reflects the explicit "I used AI" checkbox ONLY. The free-text box is
694
- // surfaced separately (aiReport / honeypot_ai_report) for the reviewer to read
695
- // and judge — auto-classifying any non-empty text as a positive would
696
- // false-flag entries like "none" / "didn't use AI" and contradicts the
697
- // documented "YES = ticked" semantics. The tool produces evidence, not verdicts.
698
- return { aiUse: ticked, aiReport };
699
- }
700
-
701
- // Chronological-by-rule trial ordering: (rulePosition, phase-rank,
702
- // trialNumber). Shows trials in the order each rule was actually
703
- // experienced — gallery → post-gallery query → classification t1..t6 — with
704
- // end-requery trials (rulePosition=null → Infinity) sorting to the very end,
705
- // matching their session-end timing. Used by the phaseTrials merge above and
706
- // by the trajectories renderer's default displayOrder. Stable-sort friendly:
707
- // trials without any of these fields compare equal and keep their input order.
708
- export function ruleChronologicalCompare(a, b) {
709
- const phaseRank = (ph) => {
710
- if (ph === 'gallery') return 0;
711
- if (ph === 'post_gallery_query') return 1;
712
- if (ph === 'end_requery') return 99;
713
- return 2; // classification or unspecified
714
- };
715
- const aPos = a.rulePosition ?? Infinity;
716
- const bPos = b.rulePosition ?? Infinity;
717
- if (aPos !== bPos) return aPos - bPos;
718
- const aP = phaseRank(a.phase);
719
- const bP = phaseRank(b.phase);
720
- if (aP !== bP) return aP - bP;
721
- return (a.trialNumber ?? 0) - (b.trialNumber ?? 0);
722
- }
723
-
724
- // Maps Shape-2 legacy field names to CyborgHunter schema names.
725
- // The CLI can then use a single set of field names downstream.
726
- const LEGACY_FIELD_MAP = {
727
- mouseTrack: 'mouseEvents',
728
- };
729
-
730
- // Normalizes tabAwayEvents[*].start from session-relative performance.now()
731
- // (ms since browser navigation) to trial-relative ms (ms since this trial
732
- // began), stored as a new `startRel_ms` field. Renderers plot per-trial
733
- // against trial-relative time; without normalization, tab-away markers
734
- // land tens of thousands of ms off the right edge — invisible.
735
- //
736
- // Two paths:
737
- // FAST PATH — every trial has a per-trial performance.now() anchor (either
738
- // trialStart_perfNow set by the jsPsych wrapper, or startTime set by the
739
- // standalone monitor). Subtract directly; result is exact.
740
- // ESTIMATOR PATH — Shape-2 / pre-monitor data has neither anchor.
741
- // We infer the session-start performance.now() value from the
742
- // relationship between trial-end wall-clocks (`timestamp`),
743
- // `responseTime_ms`, and the first tab-away's session-relative `start`,
744
- // then subtract the inferred offset to get a trial-relative value.
745
- // Less precise than the fast path; used only when nothing better exists.
746
- function normalizeTabAwayTimestamps(trials, raw) {
747
- // Fast path — every trial has a usable anchor.
748
- const everyTrialHasAnchor = trials.length > 0 && trials.every(t =>
749
- typeof (t.trialStart_perfNow ?? t.startTime) === 'number'
750
- );
751
- if (everyTrialHasAnchor) {
752
- for (const trial of trials) {
753
- const tabs = trial.tabAwayEvents;
754
- if (!Array.isArray(tabs) || tabs.length === 0) continue;
755
- const trialStart = trial.trialStart_perfNow ?? trial.startTime;
756
- trial.tabAwayEvents = tabs.map(ta => ({
757
- ...ta,
758
- startRel_ms: typeof ta.start === 'number' ? ta.start - trialStart : null
759
- }));
760
- }
761
- return;
762
- }
763
-
764
- // Estimator path. Need a session-start wall-clock anchor; if there isn't
765
- // one, we have no way to relate trial-end timestamps to a 0 reference.
766
- const sessStartIso = raw.metadata?.startTime;
767
- if (!sessStartIso) return;
768
- const sessStartMs = Date.parse(sessStartIso);
769
- if (!Number.isFinite(sessStartMs)) return;
770
-
771
- // For each trial, compute trial-start in ms-since-session-start (wall-clock).
772
- // trial-end is response.timestamp; trial-start = trial-end − duration.
773
- const trialStartRels = trials.map(t => {
774
- if (!t.timestamp || t.responseTime_ms == null) return null;
775
- const endMs = Date.parse(t.timestamp);
776
- if (!Number.isFinite(endMs)) return null;
777
- return (endMs - sessStartMs) - t.responseTime_ms;
778
- });
779
-
780
- // For each trial that has ≥1 tab-away, estimate the session offset:
781
- // sessionOffset = (performance.now value at session start, in ms)
782
- // The first tab-away of the trial must fall inside the trial's response
783
- // window, so:
784
- // firstTabStart ∈ [sessionOffset + trialStartRel,
785
- // sessionOffset + trialStartRel + trialDuration]
786
- // Solving for sessionOffset gives an interval; we use the midpoint as
787
- // each trial's candidate, then take the median across trials as the
788
- // robust estimate. This is approximate but converges quickly with even
789
- // a few tab-away-bearing trials.
790
- const candidates = [];
791
- for (let i = 0; i < trials.length; i++) {
792
- const tabs = trials[i].tabAwayEvents;
793
- if (!Array.isArray(tabs) || tabs.length === 0) continue;
794
- const trialStartRel = trialStartRels[i];
795
- const trialDurationMs = trials[i].responseTime_ms;
796
- if (trialStartRel == null || trialDurationMs == null) continue;
797
- const firstTabStart = tabs[0].start;
798
- if (typeof firstTabStart !== 'number') continue;
799
- candidates.push(firstTabStart - trialStartRel - trialDurationMs / 2);
800
- }
801
-
802
- if (candidates.length === 0) return;
803
-
804
- candidates.sort((a, b) => a - b);
805
- const sessionOffset = candidates[Math.floor(candidates.length / 2)];
806
-
807
- // Apply normalization. Original `start` is preserved alongside `startRel_ms`
808
- // so consumers can still see the absolute time if they want it.
809
- for (let i = 0; i < trials.length; i++) {
810
- const tabs = trials[i].tabAwayEvents;
811
- if (!Array.isArray(tabs) || tabs.length === 0) continue;
812
- const trialStartRel = trialStartRels[i];
813
- if (trialStartRel == null) continue;
814
- trials[i].tabAwayEvents = tabs.map(ta => ({
815
- ...ta,
816
- startRel_ms: typeof ta.start === 'number'
817
- ? ta.start - sessionOffset - trialStartRel
818
- : null
819
- }));
820
- }
821
- }
822
-
823
- function mapLegacyFields(trial) {
824
- const mapped = { ...trial };
825
- for (const [oldName, newName] of Object.entries(LEGACY_FIELD_MAP)) {
826
- if (mapped[oldName] !== undefined && mapped[newName] === undefined) {
827
- mapped[newName] = mapped[oldName];
828
- delete mapped[oldName];
829
- }
830
- }
831
- // Flag distinguishes "no tracking hardware" from "tracked, zero events"
832
- mapped.mouseDataAvailable = Array.isArray(mapped.mouseEvents) && mapped.mouseEvents.length > 0;
833
- return mapped;
834
- }
301
+ // Load-bearing re-exports:
302
+ // - getByPath: html-index.js (and external adopters) import it from ingest.js.
303
+ // - extractIntegrityData, ruleChronologicalCompare: moved to extract-core.js
304
+ // (0.7.2 extraction — pure/no Node APIs so a browser demo can bundle it);
305
+ // this file re-exports both for existing callers — trajectories.js imports
306
+ // ruleChronologicalCompare from here, and tests/cli/*.test.js import
307
+ // extractIntegrityData from here.
308
+ export { getByPath };
309
+ export { extractIntegrityData, ruleChronologicalCompare };
835
310
 
836
311
  // Finds files matching a glob pattern in the given directory.
837
312
  // Supports:
@@ -0,0 +1,36 @@
1
+ // src/cli/preview-entry.js
2
+ // Browser-preview surface (0.7.2): the pure subset of the CLI pipeline the
3
+ // in-browser results screen (demo/results.js) needs to run the same
4
+ // ingest → analyze → render steps the real CLI runs, without any Node APIs.
5
+ // Bundled by tools/build-preview-core.mjs into demo/preview-core.js (esbuild,
6
+ // platform: browser) — every re-export below must resolve to a module with
7
+ // no fs/path/zlib/papaparse imports, or the bundle step fails with an
8
+ // unresolved-import error (the empirical gate the 0.7.2 scope relies on).
9
+ //
10
+ // extractIntegrityData is NOT re-exported from src/cli/ingest.js here even
11
+ // though that's its public home — ingest.js imports Node's `fs`/`zlib` at
12
+ // module scope for the file-discovery/CSV/replay-artifact machinery that
13
+ // surrounds it, so bundling ingest.js itself pulls those in. extract-core.js
14
+ // is the pure extraction the ingest.js module re-exports from (0.7.2), and
15
+ // is what this entry point bundles instead.
16
+
17
+ export { renderIndexHtml } from './renderers/html-index-core.js';
18
+ export { computeSummary } from './analyzers/summary.js';
19
+ export { detectEdgeExits } from './analyzers/edge-exit.js';
20
+ export { rankTriage } from './analyzers/triage.js';
21
+ export { extractIntegrityData } from './extract-core.js';
22
+ // buildViewerModel: pure wire->viewer conversion (src/replay/viewer-model.js,
23
+ // see that file's docblock) — the demo's results build needs it to construct
24
+ // the visitor's replay viewer-model in-browser (demo/results.js, C2) without
25
+ // re-implementing the time-conversion logic. No Node APIs, so it bundles
26
+ // cleanly here.
27
+ export { buildViewerModel } from '../replay/viewer-model.js';
28
+
29
+ // The three pure plot cores (0.7.2-style extraction — see each file's own
30
+ // docblock): drawSessionTimeline, drawTrajectoryGrid, drawTypingProfile.
31
+ // Each takes an injected createCanvas factory instead of importing the
32
+ // `canvas` package directly, so they bundle cleanly here too. Consumed by
33
+ // demo/plot-adapter.js to render the visitor's own plots in-browser.
34
+ export { drawSessionTimeline } from './renderers/session-timeline-core.js';
35
+ export { drawTrajectoryGrid } from './renderers/trajectories-core.js';
36
+ export { drawTypingProfile } from './renderers/typing-profile-core.js';