@nac3/runtime 2.5.0-alpha.3 → 2.5.0-alpha.5

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/dist/nac.cjs CHANGED
@@ -3576,7 +3576,22 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
3576
3576
  throw NacError('not_found', 'option ' + option + ' not present in ' + nac_id);
3577
3577
  }
3578
3578
 
3579
- async function tab(plugin, tab_key) {
3579
+ async function tab(plugin, tab_key, opts) {
3580
+ /* v2.5.0-alpha.4: opts third arg accepts V24-04 expected_*
3581
+ parameters (expected_plugin_version / expected_tree_version /
3582
+ expected_element_state_hash). When opts has any expected_* OR
3583
+ STRICT_VERSIONING is on, the OCC gate runs before the tab
3584
+ click. Pre-alpha.4 the signature was (plugin, tab_key); the
3585
+ single-arg ergonomic form keeps working because opts on the
3586
+ single-arg path is the SECOND argument (tab_key); we detect
3587
+ that case and re-bind. */
3588
+ /* Re-binding: single-arg ergonomic form may have been called as
3589
+ tab(bareId) OR tab(bareId, opts). Differentiate by typeof. */
3590
+ if (tab_key !== undefined && typeof tab_key === 'object' && tab_key !== null
3591
+ && typeof plugin === 'string' && plugin.indexOf('.') >= 0) {
3592
+ opts = tab_key;
3593
+ tab_key = undefined;
3594
+ }
3580
3595
  /* H1.v5 (2026-05-20): single-arg ergonomic form. When the caller
3581
3596
  passes only a full nac_id (e.g. "conduit.tab.feed__your_feed"),
3582
3597
  resolve it directly against the DOM without requiring the
@@ -3647,6 +3662,16 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
3647
3662
  '" not in DOM (no exact match, no "' + bareId + '__" qualified instance)',
3648
3663
  { candidates: available });
3649
3664
  }
3665
+ /* v2.5 V24-04 OCC gate, same conditional pattern as click. */
3666
+ {
3667
+ const _o = opts || {};
3668
+ const _hasExp = ('expected_plugin_version' in _o)
3669
+ || ('expected_tree_version' in _o)
3670
+ || ('expected_element_state_hash' in _o);
3671
+ if (_hasExp || _strictVersioningOn()) {
3672
+ await _resolveVersionExpectations(opts, tabEl);
3673
+ }
3674
+ }
3650
3675
  _focusElement(tabEl);
3651
3676
  tabEl.click();
3652
3677
  try { await wait_for('nac:tab:changed', 1500); } catch (e) { /* tolerated */ }
@@ -3658,6 +3683,16 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
3658
3683
  '[data-nac-role="tab"][data-nac-id="' + tab_key + '"]'
3659
3684
  );
3660
3685
  if (!tabEl) throw NacError('not_found', 'tab ' + tab_key + ' missing');
3686
+ /* v2.5 V24-04 OCC gate (two-arg path). */
3687
+ {
3688
+ const _o = opts || {};
3689
+ const _hasExp = ('expected_plugin_version' in _o)
3690
+ || ('expected_tree_version' in _o)
3691
+ || ('expected_element_state_hash' in _o);
3692
+ if (_hasExp || _strictVersioningOn()) {
3693
+ await _resolveVersionExpectations(opts, tabEl);
3694
+ }
3695
+ }
3661
3696
  _focusElement(tabEl);
3662
3697
  tabEl.click();
3663
3698
  try {
@@ -4185,7 +4220,8 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
4185
4220
  + targetPlugin + '"',
4186
4221
  { candidates: available });
4187
4222
  }
4188
- return await tab(targetPlugin, matched.nac_id);
4223
+ /* v2.5 alpha.4: propagate opts (V24-04 expected_*) down to tab(). */
4224
+ return await tab(targetPlugin, matched.nac_id, opts);
4189
4225
  }
4190
4226
 
4191
4227
  /* ---------- Visualization mode ---------------------------------- */
@@ -7555,11 +7591,251 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
7555
7591
  }
7556
7592
  }
7557
7593
 
7594
+ /* ================================================================
7595
+ v2.5 Block C -- RFC V25-03: asistive labels (narrate)
7596
+ ================================================================
7597
+
7598
+ NAC.narrate(plugin, nac_id) -> string
7599
+ Canonical human-readable label for any nac_id in the plugin's
7600
+ DOM. Priority order:
7601
+ 1. aria-label (a11y-first)
7602
+ 2. visible text (the human reading the chrome)
7603
+ 3. data-nac-label (author override)
7604
+ 4. nac_id (worst case)
7605
+
7606
+ For elements where the visible text leaks child-interactive
7607
+ content (eg a button containing an icon and a label), narrate
7608
+ prefers the FIRST text node directly under the element so the
7609
+ spoken output stays focused.
7610
+
7611
+ Returns null only if the element cannot be found (unknown
7612
+ nac_id or unknown plugin). Empty/whitespace labels collapse to
7613
+ the next priority tier. */
7614
+
7615
+ function _v25c_visibleText(el) {
7616
+ if (!el) return '';
7617
+ /* Collect text directly under el, excluding nested elements that
7618
+ are themselves nac-addressable (those have their own narrate
7619
+ resolution). */
7620
+ let out = '';
7621
+ for (let n = el.firstChild; n; n = n.nextSibling) {
7622
+ if (n.nodeType === 3 /* TEXT_NODE */) {
7623
+ out += n.nodeValue || '';
7624
+ } else if (n.nodeType === 1 /* ELEMENT_NODE */) {
7625
+ /* Skip nested nac-addressable subtrees -- they have their
7626
+ own label. */
7627
+ if (n.getAttribute && n.getAttribute('data-nac-id')) continue;
7628
+ /* Recurse into non-addressable children (typical: icon
7629
+ <span> + text). */
7630
+ out += _v25c_visibleText(n);
7631
+ }
7632
+ }
7633
+ return out.replace(/\s+/g, ' ').trim();
7634
+ }
7635
+
7636
+ function narrate(plugin, nac_id) {
7637
+ if (typeof nac_id !== 'string' || !nac_id) return null;
7638
+ /* Scope by plugin if provided -- look only inside [data-nac-plugin=plugin]
7639
+ subtree. If plugin is omitted, fall back to global search. */
7640
+ let root = document;
7641
+ if (plugin) {
7642
+ const pluginRoot = document.querySelector('[data-nac-plugin="' + plugin + '"]');
7643
+ if (!pluginRoot) return null;
7644
+ root = pluginRoot;
7645
+ }
7646
+ /* Use attribute selector with CSS-safe escape via JSON.stringify
7647
+ to avoid injection when nac_id contains weird chars. */
7648
+ const safeId = '"' + String(nac_id).replace(/["\\]/g, '\\$&') + '"';
7649
+ const el = root.querySelector('[data-nac-id=' + safeId + ']');
7650
+ if (!el) return null;
7651
+ /* 1. aria-label */
7652
+ const aria = el.getAttribute('aria-label');
7653
+ if (aria && aria.trim()) return aria.trim();
7654
+ /* 2. visible text (direct + nested non-addressable) */
7655
+ const vis = _v25c_visibleText(el);
7656
+ if (vis) return vis;
7657
+ /* 3. data-nac-label */
7658
+ const explicit = el.getAttribute('data-nac-label');
7659
+ if (explicit && explicit.trim()) return explicit.trim();
7660
+ /* 4. nac_id fallback */
7661
+ return nac_id;
7662
+ }
7663
+
7664
+ /* ================================================================
7665
+ v2.5 Block B -- RFC V25-02: tree navigation
7666
+ ================================================================
7667
+
7668
+ NAC.context(plugin) -> { breadcrumb: [{nac_id, label}, ...] }
7669
+ Walks up from document.activeElement through ancestor nac-id
7670
+ elements within the plugin subtree. Empty array when nothing
7671
+ focused inside the plugin.
7672
+
7673
+ NAC.tree(plugin, rootId?) -> [{nac_id, label, opens?, leaf?}, ...]
7674
+ Enumerates the IMMEDIATE addressable children of rootId (or the
7675
+ plugin root when rootId is omitted). "Immediate" means: there
7676
+ are no other nac-id elements between them and the start. Each
7677
+ entry reports the data-nac-opens target slug (if any) and a
7678
+ boolean leaf flag (data-nac-leaf attribute present).
7679
+
7680
+ Both functions delegate label resolution to narrate(). */
7681
+
7682
+ function _v25b_pluginRoot(plugin) {
7683
+ if (!plugin) return null;
7684
+ return document.querySelector('[data-nac-plugin="' + plugin + '"]');
7685
+ }
7686
+
7687
+ function context(plugin) {
7688
+ const out = { breadcrumb: [] };
7689
+ const root = _v25b_pluginRoot(plugin);
7690
+ if (!root) return out;
7691
+ const active = document.activeElement;
7692
+ if (!active || !root.contains(active)) return out;
7693
+ /* Walk up; collect each ancestor that owns a data-nac-id. Stop
7694
+ at the plugin root inclusive (root itself may not carry a
7695
+ nac-id -- in that case we just stop there). */
7696
+ let cur = active;
7697
+ while (cur && cur !== root.parentElement) {
7698
+ const id = cur.getAttribute && cur.getAttribute('data-nac-id');
7699
+ if (id) {
7700
+ out.breadcrumb.unshift({ nac_id: id, label: narrate(plugin, id) });
7701
+ }
7702
+ if (cur === root) break;
7703
+ cur = cur.parentElement;
7704
+ }
7705
+ return out;
7706
+ }
7707
+
7708
+ function _v25b_isImmediate(start, node) {
7709
+ /* True if `node` has no OTHER nac-id ancestor between itself and
7710
+ `start`. Excludes node === start and ancestors above start. */
7711
+ let p = node.parentElement;
7712
+ while (p && p !== start) {
7713
+ if (p.getAttribute && p.getAttribute('data-nac-id')) return false;
7714
+ p = p.parentElement;
7715
+ }
7716
+ return true;
7717
+ }
7718
+
7719
+ /* ================================================================
7720
+ v2.5 Block D -- RFC V25-04: effect detection (expect + diff)
7721
+ ================================================================
7722
+
7723
+ NAC.expect(plugin, statePath, value, opts?) -> Promise
7724
+ Polls NAC.read(plugin, statePath) until the returned value
7725
+ satisfies the comparator (default ===) or the timeout fires.
7726
+ Resolves with the resolved value on match.
7727
+ Rejects with NacError('timeout', ...) on deadline.
7728
+ Read failures are retried until the deadline (treated as
7729
+ "the state is not yet observable").
7730
+
7731
+ opts:
7732
+ timeout_ms default 5000
7733
+ poll_ms default 100
7734
+ comparator default (cur, want) => cur === want
7735
+ receives (current, expected) and returns boolean
7736
+
7737
+ NAC.diff(plugin, statePath, actionFn) -> Promise<{before, after, changed}>
7738
+ Reads state, awaits actionFn(), reads state again, returns
7739
+ both snapshots + a `changed` boolean (JSON-stringify compare).
7740
+ Read failures collapse to undefined and changed is computed
7741
+ on the stringified pair (so undefined vs value still flags).
7742
+
7743
+ Both delegate to NAC.read() so they inherit V25-01's full
7744
+ fallback-chain semantics for free. */
7745
+
7746
+ function expect(plugin, statePath, value, opts) {
7747
+ opts = opts || {};
7748
+ const timeout = (typeof opts.timeout_ms === 'number') ? opts.timeout_ms : 5000;
7749
+ const pollMs = (typeof opts.poll_ms === 'number') ? opts.poll_ms : 100;
7750
+ const cmp = (typeof opts.comparator === 'function')
7751
+ ? opts.comparator
7752
+ : function (cur, want) { return cur === want; };
7753
+ const deadline = Date.now() + timeout;
7754
+ return new Promise(function (resolve, reject) {
7755
+ function tick() {
7756
+ read(plugin, statePath).then(function (cur) {
7757
+ let matches = false;
7758
+ try { matches = !!cmp(cur, value); }
7759
+ catch (_) { matches = false; }
7760
+ if (matches) { resolve(cur); return; }
7761
+ if (Date.now() >= deadline) {
7762
+ reject(_v25_buildNacError('timeout',
7763
+ 'NAC.expect: state "' + statePath + '" did not match within ' +
7764
+ timeout + 'ms (last value: ' + JSON.stringify(cur) + ')'));
7765
+ return;
7766
+ }
7767
+ setTimeout(tick, pollMs);
7768
+ }, function (_err) {
7769
+ if (Date.now() >= deadline) {
7770
+ reject(_v25_buildNacError('timeout',
7771
+ 'NAC.expect: state "' + statePath + '" unreadable + timeout'));
7772
+ return;
7773
+ }
7774
+ setTimeout(tick, pollMs);
7775
+ });
7776
+ }
7777
+ tick();
7778
+ });
7779
+ }
7780
+
7781
+ function diff(plugin, statePath, actionFn) {
7782
+ function safeRead() {
7783
+ return read(plugin, statePath).then(
7784
+ function (v) { return v; },
7785
+ function () { return undefined; }
7786
+ );
7787
+ }
7788
+ return safeRead().then(function (before) {
7789
+ const act = (typeof actionFn === 'function')
7790
+ ? Promise.resolve(actionFn())
7791
+ : Promise.resolve();
7792
+ return act.then(function () {
7793
+ return safeRead().then(function (after) {
7794
+ let changed;
7795
+ try {
7796
+ changed = JSON.stringify(before) !== JSON.stringify(after);
7797
+ } catch (_) {
7798
+ changed = before !== after;
7799
+ }
7800
+ return { before: before, after: after, changed: changed };
7801
+ });
7802
+ });
7803
+ });
7804
+ }
7805
+
7806
+ function tree(plugin, rootId) {
7807
+ const pluginRoot = _v25b_pluginRoot(plugin);
7808
+ if (!pluginRoot) return [];
7809
+ let start = pluginRoot;
7810
+ if (rootId) {
7811
+ const safeId = '"' + String(rootId).replace(/["\\]/g, '\\$&') + '"';
7812
+ const found = pluginRoot.querySelector('[data-nac-id=' + safeId + ']');
7813
+ if (!found) return [];
7814
+ start = found;
7815
+ }
7816
+ const candidates = start.querySelectorAll('[data-nac-id]');
7817
+ const out = [];
7818
+ for (let i = 0; i < candidates.length; i++) {
7819
+ const k = candidates[i];
7820
+ if (!_v25b_isImmediate(start, k)) continue;
7821
+ const id = k.getAttribute('data-nac-id');
7822
+ const entry = {
7823
+ nac_id: id,
7824
+ label: narrate(plugin, id),
7825
+ };
7826
+ const opens = k.getAttribute('data-nac-opens');
7827
+ if (opens) entry.opens = opens;
7828
+ if (k.hasAttribute('data-nac-leaf')) entry.leaf = true;
7829
+ out.push(entry);
7830
+ }
7831
+ return out;
7832
+ }
7833
+
7558
7834
  /* ---------- Install -------------------------------------------- */
7559
7835
 
7560
7836
  global.NAC = {
7561
7837
  __nac_v1_installed: true,
7562
- version: '2.5.0-alpha.3',
7838
+ version: '2.5.0-alpha.5',
7563
7839
  spec_version: '2.5',
7564
7840
  /* v2.2.0 (V22-01) -- strict validation toggle. When true,
7565
7841
  NAC.register throws on findings (manifest_role_unknown,
@@ -7599,10 +7875,12 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
7599
7875
  describe() output (RFC sec 5.2/5.3). expected_plugin_version
7600
7876
  / expected_tree_version / expected_element_state_hash on every
7601
7877
  mutating API reject with NacError('snapshot_stale', ...) when
7602
- the snapshot is stale. Default STRICT_VERSIONING=false in
7603
- 2.5.0-alpha.2 so adopters opt in; flips to true in alpha.3
7604
- once the dispatch hooks finish their soak window. */
7605
- STRICT_VERSIONING: false,
7878
+ the snapshot is stale. As of v2.5.0-alpha.4 every mutating
7879
+ verb (click/fill/select/invoke/tab/tab_by_label) honours the
7880
+ gate, so STRICT_VERSIONING default flips to true: callers
7881
+ MUST pass at least one expected_* or set STRICT_VERSIONING
7882
+ =false explicitly. Removed in v2.6 (becomes unconditional). */
7883
+ STRICT_VERSIONING: true,
7606
7884
  eager_hashing: false,
7607
7885
  conformance_tier: 'NAC-3-versioned',
7608
7886
  markHydrationComplete: markHydrationComplete,
@@ -7808,6 +8086,25 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
7808
8086
  read: read,
7809
8087
  subscribe: subscribe,
7810
8088
  unsubscribe: unsubscribe,
8089
+ /* v2.5 Block C -- asistive labels (SPEC.md sec 17 / RFC V25-03).
8090
+ Canonical human-readable label resolution for screen readers,
8091
+ TTS, voice flows, audit logs. Priority order: aria-label >
8092
+ visible text > data-nac-label > nac_id fallback. */
8093
+ narrate: narrate,
8094
+ /* v2.5 Block B -- tree navigation (SPEC.md sec 16 / RFC V25-02).
8095
+ context(plugin) returns the focused node's breadcrumb;
8096
+ tree(plugin, rootId?) enumerates navigable children.
8097
+ data-nac-opens declares parent->child relationships; data-nac-leaf
8098
+ marks terminals. Companion to narrate for chrome-driven walks. */
8099
+ context: context,
8100
+ tree: tree,
8101
+ /* v2.5 Block D -- effect detection (SPEC.md sec 18 / RFC V25-04).
8102
+ Closes the "silent damage" gap for assistive callers: the agent
8103
+ confirms the EFFECT of an action, not just that the command was
8104
+ emitted. expect() polls until a state matches; diff() snapshots
8105
+ around an action. Both compose with NAC.read() / V25-01 surface. */
8106
+ expect: expect,
8107
+ diff: diff,
7811
8108
  };
7812
8109
 
7813
8110
  /* v2.2.1: guard browser-only init so this module is importable in