@vue-skuilder/db 0.2.23 → 0.2.24

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/index.mjs CHANGED
@@ -11402,6 +11402,8 @@ var EloService = class {
11402
11402
  * @param userCourseRegDoc User's course registration document (will be mutated)
11403
11403
  * @param currentCard Current card session record
11404
11404
  * @param k Optional K-factor for ELO calculation
11405
+ * @returns The captured before→after exchange for session logging, or null
11406
+ * if no update was applied (missing registration / unreadable ELO).
11405
11407
  */
11406
11408
  async updateUserAndCardElo(userScore, course_id, card_id, userCourseRegDoc, currentCard, k) {
11407
11409
  if (k) {
@@ -11413,13 +11415,36 @@ var EloService = class {
11413
11415
  logger.error(
11414
11416
  `[EloService] No registration for course ${course_id} on user's registration doc \u2014 skipping ELO update for card ${card_id}. (Is the user registered for this course?)`
11415
11417
  );
11416
- return;
11418
+ return null;
11417
11419
  }
11418
11420
  const userElo = toCourseElo7(courseReg.elo);
11419
11421
  const cardElo = (await courseDB.getCardEloData([currentCard.card.card_id]))[0];
11420
11422
  if (cardElo && userElo) {
11423
+ const beforeUserGlobal = userElo.global.score;
11424
+ const beforeCardGlobal = cardElo.global.score;
11425
+ const cardTagKeys = Object.keys(cardElo.tags);
11426
+ const beforeUserTag = {};
11427
+ for (const tag of cardTagKeys) {
11428
+ beforeUserTag[tag] = userElo.tags[tag]?.score ?? userElo.global.score;
11429
+ }
11421
11430
  const eloUpdate = adjustCourseScores(userElo, cardElo, userScore);
11422
11431
  courseReg.elo = eloUpdate.userElo;
11432
+ const tags = {};
11433
+ for (const tag of cardTagKeys) {
11434
+ tags[tag] = {
11435
+ before: beforeUserTag[tag],
11436
+ after: eloUpdate.userElo.tags[tag].score,
11437
+ score: userScore
11438
+ };
11439
+ }
11440
+ const event = {
11441
+ cardId: card_id,
11442
+ at: (/* @__PURE__ */ new Date()).toISOString(),
11443
+ userScore,
11444
+ global: { before: beforeUserGlobal, after: eloUpdate.userElo.global.score },
11445
+ card: { before: beforeCardGlobal, after: eloUpdate.cardElo.global.score },
11446
+ ...cardTagKeys.length ? { tags } : {}
11447
+ };
11423
11448
  const results = await Promise.allSettled([
11424
11449
  this.user.updateUserElo(course_id, eloUpdate.userElo),
11425
11450
  courseDB.updateCardElo(card_id, eloUpdate.cardElo)
@@ -11450,7 +11475,9 @@ var EloService = class {
11450
11475
  logger.error("[EloService] Card ELO update error:", results[1].reason);
11451
11476
  }
11452
11477
  }
11478
+ return event;
11453
11479
  }
11480
+ return null;
11454
11481
  }
11455
11482
  /**
11456
11483
  * Updates both user and card ELO ratings with per-tag granularity.
@@ -11469,7 +11496,7 @@ var EloService = class {
11469
11496
  logger.error(
11470
11497
  `[EloService] No registration for course ${course_id} on user's registration doc \u2014 skipping per-tag ELO update for card ${card_id}. (Is the user registered for this course?)`
11471
11498
  );
11472
- return;
11499
+ return null;
11473
11500
  }
11474
11501
  const userElo = toCourseElo7(courseReg.elo);
11475
11502
  const [cardEloResults, cardTagsMap] = await Promise.all([
@@ -11486,8 +11513,29 @@ var EloService = class {
11486
11513
  }
11487
11514
  }
11488
11515
  if (cardElo && userElo) {
11516
+ const beforeUserGlobal = userElo.global.score;
11517
+ const beforeCardGlobal = cardElo.global.score;
11518
+ const touchedTags = Object.keys(enriched).filter((key) => key !== "_global");
11519
+ const beforeUserTag = {};
11520
+ for (const tag of touchedTags) {
11521
+ beforeUserTag[tag] = userElo.tags[tag]?.score ?? userElo.global.score;
11522
+ }
11489
11523
  const eloUpdate = adjustCourseScoresPerTag(userElo, cardElo, enriched);
11490
11524
  courseReg.elo = eloUpdate.userElo;
11525
+ const tags = {};
11526
+ for (const tag of touchedTags) {
11527
+ const score = enriched[tag];
11528
+ const after = score === null ? beforeUserTag[tag] : eloUpdate.userElo.tags[tag]?.score ?? beforeUserTag[tag];
11529
+ tags[tag] = { before: beforeUserTag[tag], after, score };
11530
+ }
11531
+ const event = {
11532
+ cardId: card_id,
11533
+ at: (/* @__PURE__ */ new Date()).toISOString(),
11534
+ userScore: globalScore,
11535
+ global: { before: beforeUserGlobal, after: eloUpdate.userElo.global.score },
11536
+ card: { before: beforeCardGlobal, after: eloUpdate.cardElo.global.score },
11537
+ ...touchedTags.length ? { tags } : {}
11538
+ };
11491
11539
  const results = await Promise.allSettled([
11492
11540
  this.user.updateUserElo(course_id, eloUpdate.userElo),
11493
11541
  courseDB.updateCardElo(card_id, eloUpdate.cardElo)
@@ -11519,7 +11567,9 @@ var EloService = class {
11519
11567
  logger.error("[EloService] Card ELO update error:", results[1].reason);
11520
11568
  }
11521
11569
  }
11570
+ return event;
11522
11571
  }
11572
+ return null;
11523
11573
  }
11524
11574
  };
11525
11575
 
@@ -11530,9 +11580,12 @@ import { isTaggedPerformance as isTaggedPerformance2 } from "@vue-skuilder/commo
11530
11580
  var ResponseProcessor = class {
11531
11581
  srsService;
11532
11582
  eloService;
11533
- constructor(srsService, eloService) {
11583
+ /** Sink for resolved ELO exchanges, wired to the session's in-memory log. */
11584
+ onEloEvent;
11585
+ constructor(srsService, eloService, onEloEvent) {
11534
11586
  this.srsService = srsService;
11535
11587
  this.eloService = eloService;
11588
+ this.onEloEvent = onEloEvent;
11536
11589
  }
11537
11590
  /**
11538
11591
  * ELO updates are fired without awaiting so response handling isn't blocked
@@ -11542,6 +11595,17 @@ var ResponseProcessor = class {
11542
11595
  logEloFailure(context, cardId) {
11543
11596
  return (e) => logger.error(`[ResponseProcessor] ELO update failed (${context}) for ${cardId}:`, e);
11544
11597
  }
11598
+ /**
11599
+ * Fire an ELO update without blocking response handling, forwarding the
11600
+ * resolved exchange to the session log (if any) and surfacing failures.
11601
+ * Mirrors the previous `void update(...).catch(logEloFailure)` shape, with
11602
+ * the added `.then` capturing the before→after event for diagnostics.
11603
+ */
11604
+ trackElo(update, context, cardId) {
11605
+ void update.then((event) => {
11606
+ if (event) this.onEloEvent?.(event);
11607
+ }).catch(this.logEloFailure(context, cardId));
11608
+ }
11545
11609
  /**
11546
11610
  * Parses performance data into global score and optional per-tag scores.
11547
11611
  *
@@ -11655,27 +11719,45 @@ var ResponseProcessor = class {
11655
11719
  logger.info(
11656
11720
  `[FirstContactElo] correct first-attempt per-tag ELO update for ${cardId} (historyLen=${history.records.length}, priorAttemps=${cardRecord.priorAttemps}): scored=[${scoredTags.join(", ")}] count-only=[${nullTags.join(", ")}]`
11657
11721
  );
11658
- void this.eloService.updateUserAndCardEloPerTag(
11659
- taggedPerformance,
11660
- courseId,
11661
- cardId,
11662
- courseRegistrationDoc,
11663
- currentCard
11664
- ).catch(this.logEloFailure("correct per-tag", cardId));
11722
+ this.trackElo(
11723
+ this.eloService.updateUserAndCardEloPerTag(
11724
+ taggedPerformance,
11725
+ courseId,
11726
+ cardId,
11727
+ courseRegistrationDoc,
11728
+ currentCard
11729
+ ),
11730
+ "correct per-tag",
11731
+ cardId
11732
+ );
11665
11733
  } else {
11666
11734
  const userScore = 0.5 + globalScore / 2;
11667
11735
  if (history.records.length === 1) {
11668
- void this.eloService.updateUserAndCardElo(userScore, courseId, cardId, courseRegistrationDoc, currentCard).catch(this.logEloFailure("correct", cardId));
11736
+ this.trackElo(
11737
+ this.eloService.updateUserAndCardElo(
11738
+ userScore,
11739
+ courseId,
11740
+ cardId,
11741
+ courseRegistrationDoc,
11742
+ currentCard
11743
+ ),
11744
+ "correct",
11745
+ cardId
11746
+ );
11669
11747
  } else {
11670
11748
  const k = Math.ceil(32 / history.records.length);
11671
- void this.eloService.updateUserAndCardElo(
11672
- userScore,
11673
- courseId,
11674
- cardId,
11675
- courseRegistrationDoc,
11676
- currentCard,
11677
- k
11678
- ).catch(this.logEloFailure("correct repeat-view", cardId));
11749
+ this.trackElo(
11750
+ this.eloService.updateUserAndCardElo(
11751
+ userScore,
11752
+ courseId,
11753
+ cardId,
11754
+ courseRegistrationDoc,
11755
+ currentCard,
11756
+ k
11757
+ ),
11758
+ "correct repeat-view",
11759
+ cardId
11760
+ );
11679
11761
  }
11680
11762
  logger.info(
11681
11763
  `[FirstContactElo] correct first-attempt ELO update (score=${userScore.toFixed(3)}) for ${cardId} (historyLen=${history.records.length}, priorAttemps=${cardRecord.priorAttemps})`
@@ -11714,25 +11796,33 @@ var ResponseProcessor = class {
11714
11796
  let eloUpdated = false;
11715
11797
  if (cardRecord.priorAttemps === 0) {
11716
11798
  if (taggedPerformance) {
11717
- void this.eloService.updateUserAndCardEloPerTag(
11718
- taggedPerformance,
11719
- courseId,
11720
- cardId,
11721
- courseRegistrationDoc,
11722
- currentCard
11723
- ).catch(this.logEloFailure("incorrect per-tag", cardId));
11799
+ this.trackElo(
11800
+ this.eloService.updateUserAndCardEloPerTag(
11801
+ taggedPerformance,
11802
+ courseId,
11803
+ cardId,
11804
+ courseRegistrationDoc,
11805
+ currentCard
11806
+ ),
11807
+ "incorrect per-tag",
11808
+ cardId
11809
+ );
11724
11810
  logger.info(
11725
11811
  `[FirstContactElo] incorrect first-attempt per-tag ELO update for ${cardId} (historyLen=${history.records.length}, priorAttemps=${cardRecord.priorAttemps}, tags=${Object.keys(taggedPerformance).length - 1})`
11726
11812
  );
11727
11813
  } else {
11728
- void this.eloService.updateUserAndCardElo(
11729
- 0,
11730
- // Failed response = 0 score
11731
- courseId,
11732
- cardId,
11733
- courseRegistrationDoc,
11734
- currentCard
11735
- ).catch(this.logEloFailure("incorrect", cardId));
11814
+ this.trackElo(
11815
+ this.eloService.updateUserAndCardElo(
11816
+ 0,
11817
+ // Failed response = 0 score
11818
+ courseId,
11819
+ cardId,
11820
+ courseRegistrationDoc,
11821
+ currentCard
11822
+ ),
11823
+ "incorrect",
11824
+ cardId
11825
+ );
11736
11826
  logger.info(
11737
11827
  `[FirstContactElo] incorrect first-attempt ELO update (score=0) for ${cardId} (historyLen=${history.records.length}, priorAttemps=${cardRecord.priorAttemps})`
11738
11828
  );
@@ -11747,15 +11837,29 @@ var ResponseProcessor = class {
11747
11837
  if (sessionViews >= maxSessionViews) {
11748
11838
  if (!eloUpdated) {
11749
11839
  if (taggedPerformance) {
11750
- void this.eloService.updateUserAndCardEloPerTag(
11751
- taggedPerformance,
11752
- courseId,
11753
- cardId,
11754
- courseRegistrationDoc,
11755
- currentCard
11756
- ).catch(this.logEloFailure("dismiss-failed per-tag", cardId));
11840
+ this.trackElo(
11841
+ this.eloService.updateUserAndCardEloPerTag(
11842
+ taggedPerformance,
11843
+ courseId,
11844
+ cardId,
11845
+ courseRegistrationDoc,
11846
+ currentCard
11847
+ ),
11848
+ "dismiss-failed per-tag",
11849
+ cardId
11850
+ );
11757
11851
  } else {
11758
- void this.eloService.updateUserAndCardElo(0, courseId, cardId, courseRegistrationDoc, currentCard).catch(this.logEloFailure("dismiss-failed", cardId));
11852
+ this.trackElo(
11853
+ this.eloService.updateUserAndCardElo(
11854
+ 0,
11855
+ courseId,
11856
+ cardId,
11857
+ courseRegistrationDoc,
11858
+ currentCard
11859
+ ),
11860
+ "dismiss-failed",
11861
+ cardId
11862
+ );
11759
11863
  }
11760
11864
  logger.info(
11761
11865
  `[FirstContactElo] dismiss-failed final ELO penalty for ${cardId} (historyLen=${history.records.length}, sessionViews=${sessionViews})`
@@ -14679,6 +14783,13 @@ var SessionController = class _SessionController extends Loggable {
14679
14783
  _openDocPromise = null;
14680
14784
  _runLog = [];
14681
14785
  static MAX_LOGGED_RUNS = 100;
14786
+ /**
14787
+ * Per-response ELO exchanges as they resolve (see `SessionEloEvent`). Fed by
14788
+ * the ResponseProcessor's `onEloEvent` sink; flushed to the session doc at
14789
+ * close. Bounded so a runaway session can't grow it without limit.
14790
+ */
14791
+ _eloLog = [];
14792
+ static MAX_LOGGED_ELO_EVENTS = 2e3;
14682
14793
  /** Guards `endSession()` against the several termination paths racing it. */
14683
14794
  _sessionClosed = false;
14684
14795
  startTime;
@@ -14726,7 +14837,11 @@ var SessionController = class _SessionController extends Loggable {
14726
14837
  () => this._getItemsToHydrate()
14727
14838
  );
14728
14839
  this.services = {
14729
- response: new ResponseProcessor(this.srsService, this.eloService)
14840
+ response: new ResponseProcessor(
14841
+ this.srsService,
14842
+ this.eloService,
14843
+ (event) => this._recordEloEvent(event)
14844
+ )
14730
14845
  };
14731
14846
  this.sources = sources;
14732
14847
  this.startTime = /* @__PURE__ */ new Date();
@@ -15115,6 +15230,16 @@ var SessionController = class _SessionController extends Loggable {
15115
15230
  * skipped, never wedging the session. Keep observers cheap and `void` any
15116
15231
  * long work (e.g. a triggered replan) to avoid stalling the draw.
15117
15232
  */
15233
+ /**
15234
+ * Append a resolved ELO exchange to the in-memory log. Fires async (the ELO
15235
+ * update is non-blocking), so an event can land after the response that
15236
+ * produced it has returned — but well before close, which is when the log is
15237
+ * persisted. Bounded to avoid unbounded growth.
15238
+ */
15239
+ _recordEloEvent(event) {
15240
+ if (this._eloLog.length >= _SessionController.MAX_LOGGED_ELO_EVENTS) return;
15241
+ this._eloLog.push(event);
15242
+ }
15118
15243
  async _notifyOutcomeObservers(record, currentCard, result) {
15119
15244
  if (this._outcomeObservers.length === 0) return;
15120
15245
  if (!isQuestionRecord(record)) return;
@@ -15767,6 +15892,7 @@ var SessionController = class _SessionController extends Loggable {
15767
15892
  secondsRemaining: Math.max(0, this._secondsRemaining)
15768
15893
  },
15769
15894
  runs: [...this._runLog],
15895
+ eloEvents: [...this._eloLog],
15770
15896
  finalHints: this._sessionHints,
15771
15897
  stateAtEnd: await this._captureState("end")
15772
15898
  };