bunnyquery 1.15.1 → 1.15.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bunnyquery",
3
- "version": "1.15.1",
3
+ "version": "1.15.2",
4
4
  "description": "Embeddable BunnyQuery AI chat widget + its framework-agnostic chat engine",
5
5
  "main": "bunnyquery.js",
6
6
  "exports": {
@@ -611,6 +611,17 @@ export class ChatSession {
611
611
  /** Files this client has an index dispatch in flight for, by scoped path ->
612
612
  * wall clock. See claimIndexRun. */
613
613
  private _indexClaims: { [scopedPath: string]: number };
614
+ /** bgTaskQueue by the SAME scoped path the claims use -> how many entries
615
+ * name that file. Derived from the array, never written to directly: see
616
+ * _bgQueueIndex for when it is rebuilt and why that is exact. */
617
+ private _bgQueueByFile: { [scopedPath: string]: number } | null;
618
+ /** What _bgQueueByFile was built from: the array itself, its length and its
619
+ * first and last entries. Equal stamp = equal contents (see _bgQueueIndex). */
620
+ private _bgQueueStamp: { arr: BgTaskEntry[] | null; length: number; first: any; last: any };
621
+ /** How many times the index was rebuilt. Diagnostics and tests only: a
622
+ * 3,000-file batch should build it a few thousand times at most (once per
623
+ * push or settle), never once per claim. */
624
+ _bgQueueIndexRebuilds: number;
614
625
 
615
626
  constructor(host: ChatHost) {
616
627
  this.host = host;
@@ -651,6 +662,9 @@ export class ChatSession {
651
662
  this._liveIndexKey = '';
652
663
  this._liveIndexAt = 0;
653
664
  this._indexClaims = {};
665
+ this._bgQueueByFile = null;
666
+ this._bgQueueStamp = { arr: null, length: 0, first: undefined, last: undefined };
667
+ this._bgQueueIndexRebuilds = 0;
654
668
  }
655
669
 
656
670
  /** What the display layer needs to decide whether a run is finished. `keys` holds
@@ -699,13 +713,14 @@ export class ChatSession {
699
713
  // of truth says "not indexing", and a second dispatch in that window is
700
714
  // exactly the duplicate this guard exists to stop. Observed live: two first
701
715
  // passes four seconds apart, two collapsed rows.
702
- var claimed = this._indexClaims[this._indexClaimKey(storagePath)];
716
+ var scoped = this._indexClaimKey(storagePath);
717
+ var claimed = this._indexClaims[scoped];
703
718
  if (claimed && nowMs() - claimed < INDEX_DISPATCH_CLAIM_MS) return true;
704
- var id = this.host.getIdentity();
705
- for (var i = 0; i < this.bgTaskQueue.length; i++) {
706
- var e = this.bgTaskQueue[i];
707
- if (e && e.storagePath === storagePath && e.projectId === id.projectId && e.platform === id.platform) return true;
708
- }
719
+ // The queue, looked up by the same scoped path instead of scanned. A batch
720
+ // claims once per file, and the queue holds one entry per file already
721
+ // dispatched, so the scan was O(n) per claim and O(n^2) over a 3,000-file
722
+ // batch. The index is rebuilt only when the array changes (_bgQueueIndex).
723
+ if (this._bgQueueIndex()[scoped] > 0) return true;
709
724
  return this.state.messages.some(function (m) {
710
725
  if (!m.isBackgroundTask || m.role !== 'user' || m.isCancelled) return false;
711
726
  if (!(m.isPendingQueued || m.isPendingInProcess || m.isSendingToServer)) return false;
@@ -720,6 +735,48 @@ export class ChatSession {
720
735
  return indexScopeKey(id.projectId, id.platform) + '|' + storagePath;
721
736
  }
722
737
 
738
+ /**
739
+ * bgTaskQueue as a map: scoped path (indexScopeKey(projectId, platform) +
740
+ * '|' + storagePath, the key the claims and the stops use) -> entries.
741
+ *
742
+ * The array stays the source of truth because the engine does not own every
743
+ * write to it: both hosts hand in their own array (the dashboard a Vue
744
+ * reactive one it pushes to from the Files page and splices from its
745
+ * fallback poller), and tests push to it directly. So the map is DERIVED, and
746
+ * rebuilt when a cheap stamp says the array changed: its identity, its length,
747
+ * its first and its last entry.
748
+ *
749
+ * Why that stamp is exact for this array: every write to bgTaskQueue is either
750
+ * a push of a NEW entry object or a splice that removes one (nothing inserts
751
+ * in the middle, replaces in place or re-pushes a removed entry). Under those
752
+ * two operations, same length plus same last entry means the same contents:
753
+ * a push changes the last entry unless that same push is removed again, and a
754
+ * removal changes the length unless a push restores it, which changes the
755
+ * last entry. The first entry is in the stamp as margin. A rebuild is one
756
+ * pass over the array, paid once per change instead of once per claim.
757
+ */
758
+ private _bgQueueIndex(): { [scopedPath: string]: number } {
759
+ var q = this.bgTaskQueue;
760
+ var n = q ? q.length : 0;
761
+ var first = n ? q[0] : undefined;
762
+ var last = n ? q[n - 1] : undefined;
763
+ var st = this._bgQueueStamp;
764
+ if (this._bgQueueByFile && st.arr === q && st.length === n && st.first === first && st.last === last) {
765
+ return this._bgQueueByFile;
766
+ }
767
+ var idx: { [scopedPath: string]: number } = {};
768
+ for (var i = 0; i < n; i++) {
769
+ var e = q[i];
770
+ if (!e || !e.storagePath) continue;
771
+ var k = indexScopeKey(e.projectId, e.platform) + '|' + e.storagePath;
772
+ idx[k] = (idx[k] || 0) + 1;
773
+ }
774
+ this._bgQueueByFile = idx;
775
+ this._bgQueueStamp = { arr: q, length: n, first: first, last: last };
776
+ this._bgQueueIndexRebuilds++;
777
+ return idx;
778
+ }
779
+
723
780
  /**
724
781
  * Take this file's indexing slot, or report that someone already has it.
725
782
  *