@vue-skuilder/db 0.2.36 → 0.3.0

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.
Files changed (59) hide show
  1. package/dist/{contentSource-D_E7JaxR.d.ts → contentSource-2zVmcjKA.d.ts} +6 -1
  2. package/dist/core/index.cjs +203 -148
  3. package/dist/core/index.cjs.map +1 -1
  4. package/dist/core/index.d.ts +5 -5
  5. package/dist/core/navigators/Pipeline.mjs +8 -0
  6. package/dist/core/navigators/Pipeline.mjs.map +1 -1
  7. package/dist/core/navigators/generators/prescribed.mjs +29 -11
  8. package/dist/core/navigators/generators/prescribed.mjs.map +1 -1
  9. package/dist/core/versionStamps.mjs +1 -1
  10. package/dist/{dataLayerProvider-BDshhSp_.d.ts → dataLayerProvider-rGyLQ93v.d.ts} +1 -1
  11. package/dist/diagnostics/index.d.ts +3 -3
  12. package/dist/impl/common/BaseUserDB.mjs +32 -15
  13. package/dist/impl/common/BaseUserDB.mjs.map +1 -1
  14. package/dist/impl/couch/CourseSyncService.mjs +111 -11
  15. package/dist/impl/couch/CourseSyncService.mjs.map +1 -1
  16. package/dist/impl/couch/PouchDataLayerProvider.mjs +16 -1
  17. package/dist/impl/couch/PouchDataLayerProvider.mjs.map +1 -1
  18. package/dist/impl/couch/courseAPI.mjs +24 -10
  19. package/dist/impl/couch/courseAPI.mjs.map +1 -1
  20. package/dist/impl/couch/courseDB.mjs +19 -9
  21. package/dist/impl/couch/courseDB.mjs.map +1 -1
  22. package/dist/impl/couch/index.cjs +297 -141
  23. package/dist/impl/couch/index.cjs.map +1 -1
  24. package/dist/impl/couch/index.d.ts +27 -4
  25. package/dist/impl/couch/index.mjs +20 -3
  26. package/dist/impl/couch/index.mjs.map +1 -1
  27. package/dist/impl/couch/updateQueue.mjs +69 -46
  28. package/dist/impl/couch/updateQueue.mjs.map +1 -1
  29. package/dist/impl/static/index.cjs +147 -93
  30. package/dist/impl/static/index.cjs.map +1 -1
  31. package/dist/impl/static/index.d.ts +4 -4
  32. package/dist/{index-BLDpFYG3.d.ts → index-C745YB_6.d.ts} +1 -1
  33. package/dist/index.cjs +345 -171
  34. package/dist/index.cjs.map +1 -1
  35. package/dist/index.d.ts +23 -10
  36. package/dist/index.mjs +2 -1
  37. package/dist/study/SessionController.mjs +4 -0
  38. package/dist/study/SessionController.mjs.map +1 -1
  39. package/dist/{types-DChCz22D.d.ts → types-DF0JuPbs.d.ts} +1 -1
  40. package/dist/{types-legacy-fT5Y1Wuy.d.ts → types-legacy-DdVjg81H.d.ts} +1 -1
  41. package/dist/util/packer/index.d.ts +3 -3
  42. package/dist/util/perfMarks.mjs +30 -0
  43. package/dist/util/perfMarks.mjs.map +1 -0
  44. package/package.json +3 -3
  45. package/src/core/interfaces/courseDB.ts +6 -0
  46. package/src/core/navigators/Pipeline.ts +8 -0
  47. package/src/core/navigators/generators/prescribed.ts +42 -16
  48. package/src/impl/common/BaseUserDB.ts +42 -23
  49. package/src/impl/couch/CourseSyncService.ts +149 -22
  50. package/src/impl/couch/PouchDataLayerProvider.ts +14 -1
  51. package/src/impl/couch/courseAPI.ts +26 -12
  52. package/src/impl/couch/courseDB.ts +25 -12
  53. package/src/impl/couch/index.ts +16 -5
  54. package/src/impl/couch/updateQueue.ts +86 -82
  55. package/src/study/SessionController.ts +4 -0
  56. package/src/util/index.ts +1 -0
  57. package/src/util/perfMarks.ts +35 -0
  58. package/tests/impl/courseSync.test.ts +139 -0
  59. package/tests/impl/updateQueue.test.ts +57 -0
package/package.json CHANGED
@@ -13,7 +13,7 @@
13
13
  "publishConfig": {
14
14
  "access": "public"
15
15
  },
16
- "version": "0.2.36",
16
+ "version": "0.3.0",
17
17
  "description": "Database layer for vue-skuilder",
18
18
  "main": "dist/index.cjs",
19
19
  "module": "dist/index.mjs",
@@ -69,7 +69,7 @@
69
69
  },
70
70
  "dependencies": {
71
71
  "@nilock2/pouchdb-authentication": "^1.0.2",
72
- "@vue-skuilder/common": "0.2.36",
72
+ "@vue-skuilder/common": "0.3.0",
73
73
  "cross-fetch": "^4.1.0",
74
74
  "moment": "^2.29.4",
75
75
  "pouchdb": "^9.0.0",
@@ -87,5 +87,5 @@
87
87
  "vite": "^8.0.0",
88
88
  "vitest": "^4.1.0"
89
89
  },
90
- "stableVersion": "0.2.36"
90
+ "stableVersion": "0.3.0"
91
91
  }
@@ -27,6 +27,12 @@ export interface CourseInfo {
27
27
  }
28
28
 
29
29
  export interface CourseDBInterface extends NavigationStrategyManager, StudyContentSource {
30
+ /**
31
+ * Whether reads go to a local replica rather than the server. Optional: only
32
+ * implementations with both (couch) report it; perf marks read it.
33
+ */
34
+ readonly readsLocal?: boolean;
35
+
30
36
  /**
31
37
  * Get course config
32
38
  */
@@ -7,6 +7,7 @@ import type { CardFilter, FilterContext } from './filters/types';
7
7
  import type { CardGenerator, GeneratorContext } from './generators/types';
8
8
  import type { GeneratorResult } from './generators/types';
9
9
  import { logger } from '../../util/logger';
10
+ import { perfMeasure } from '../../util/perfMarks';
10
11
  import { createOrchestrationContext, OrchestrationContext } from '../orchestration';
11
12
  import { captureRun, buildRunReport, registerPipelineForDebug, type GeneratorSummary, type FilterImpact } from './PipelineDebugger';
12
13
  import { diversityRerank } from './diversityRerank';
@@ -572,6 +573,13 @@ export class Pipeline extends ContentNavigator implements PipelineForecaster {
572
573
  `(context=${(tContext - t0).toFixed(0)} generate=${(tGenerate - tContext).toFixed(0)} ` +
573
574
  `hydrate=${(tHydrate - tGenerate).toFixed(0)} filter=${(tFilter - tHydrate).toFixed(0)})`
574
575
  );
576
+ perfMeasure('sk:pipeline-run', t0, tFilter, {
577
+ context: Math.round(tContext - t0),
578
+ generate: Math.round(tGenerate - tContext),
579
+ hydrate: Math.round(tHydrate - tGenerate),
580
+ filter: Math.round(tFilter - tHydrate),
581
+ binding: this.course?.readsLocal === undefined ? 'unknown' : this.course.readsLocal ? 'local' : 'remote',
582
+ });
575
583
 
576
584
  // Toggle execution summary logging:
577
585
  const topScores = result.slice(0, 3).map((c) => c.score);
@@ -269,6 +269,12 @@ function pickTopByScore(cards: WeightedCard[], limit: number): WeightedCard[] {
269
269
  export default class PrescribedCardsGenerator extends ContentNavigator implements CardGenerator {
270
270
  name: string;
271
271
  private config: PrescribedConfig;
272
+ /**
273
+ * Course-wide reads that don't change within a navigator's lifetime (the
274
+ * navigator is cached per CourseDB, minutes), loaded once, not per run.
275
+ */
276
+ private _cardsByTag: Promise<Map<string, string[]>> | null = null;
277
+ private _hierarchyConfigs: Promise<HierarchyConfig[]> | null = null;
272
278
 
273
279
  constructor(
274
280
  user: UserDBInterface,
@@ -326,21 +332,7 @@ export default class PrescribedCardsGenerator extends ContentNavigator implement
326
332
  ? await this.course.getAppliedTagsBatch(allRelevantIds)
327
333
  : new Map<string, string[]>();
328
334
 
329
- const courseTagDocs = await this.course.getCourseTagStubs().catch(
330
- () =>
331
- ({
332
- rows: [],
333
- offset: 0,
334
- total_rows: 0,
335
- }) as unknown as Awaited<ReturnType<CourseDBInterface['getCourseTagStubs']>>
336
- );
337
- const cardsByTag = new Map<string, string[]>();
338
- for (const row of courseTagDocs.rows ?? []) {
339
- const tagDoc = row.doc as { name?: string; taggedCards?: string[] } | undefined;
340
- if (tagDoc?.name && Array.isArray(tagDoc.taggedCards)) {
341
- cardsByTag.set(tagDoc.name, [...tagDoc.taggedCards]);
342
- }
343
- }
335
+ const cardsByTag = await this.loadCardsByTag();
344
336
 
345
337
  const nextState: PrescribedProgressState = {
346
338
  updatedAt: isoNow(),
@@ -677,7 +669,39 @@ export default class PrescribedCardsGenerator extends ContentNavigator implement
677
669
  }
678
670
  }
679
671
 
680
- private async loadHierarchyConfigs(): Promise<HierarchyConfig[]> {
672
+ /** Tag name → tagged card IDs, from the course's tag docs (once per instance). */
673
+ private loadCardsByTag(): Promise<Map<string, string[]>> {
674
+ if (!this._cardsByTag) {
675
+ this._cardsByTag = this.course
676
+ .getCourseTagStubs()
677
+ .then((docs) => {
678
+ const cardsByTag = new Map<string, string[]>();
679
+ for (const row of docs.rows ?? []) {
680
+ const tagDoc = row.doc as { name?: string; taggedCards?: string[] } | undefined;
681
+ if (tagDoc?.name && Array.isArray(tagDoc.taggedCards)) {
682
+ cardsByTag.set(tagDoc.name, [...tagDoc.taggedCards]);
683
+ }
684
+ }
685
+ return cardsByTag;
686
+ })
687
+ .catch((e) => {
688
+ // Not cached: the next run retries.
689
+ this._cardsByTag = null;
690
+ logger.debug(`[Prescribed] Failed to load tag docs: ${e}`);
691
+ return new Map<string, string[]>();
692
+ });
693
+ }
694
+ return this._cardsByTag;
695
+ }
696
+
697
+ private loadHierarchyConfigs(): Promise<HierarchyConfig[]> {
698
+ if (!this._hierarchyConfigs) {
699
+ this._hierarchyConfigs = this.fetchHierarchyConfigs();
700
+ }
701
+ return this._hierarchyConfigs;
702
+ }
703
+
704
+ private async fetchHierarchyConfigs(): Promise<HierarchyConfig[]> {
681
705
  try {
682
706
  const strategies = await this.course.getAllNavigationStrategies();
683
707
  return strategies
@@ -693,6 +717,8 @@ export default class PrescribedCardsGenerator extends ContentNavigator implement
693
717
  }
694
718
  });
695
719
  } catch (e) {
720
+ // Not cached: the next run retries.
721
+ this._hierarchyConfigs = null;
696
722
  logger.debug(`[Prescribed] Failed to load hierarchy configs: ${e}`);
697
723
  return [];
698
724
  }
@@ -376,6 +376,19 @@ Currently logged-in as ${this._username}.`
376
376
  return this.updateQueue.update(id, update);
377
377
  }
378
378
 
379
+ /**
380
+ * Create a default doc after a 404. Concurrent first reads (e.g. two
381
+ * components on a brand-new user) both see the 404 and both create; the
382
+ * loser's conflict just means the doc now exists, which is what it wanted.
383
+ */
384
+ private async putDefaultUnlessCreated<T extends object>(doc: PouchDB.Core.PutDocument<T>) {
385
+ try {
386
+ await this.localDB.put<T>(doc);
387
+ } catch (e) {
388
+ if ((e as PouchError).status !== 409) throw e;
389
+ }
390
+ }
391
+
379
392
  public async getCourseRegistrationsDoc(): Promise<
380
393
  CourseRegistrationDoc & PouchDB.Core.IdMeta & PouchDB.Core.GetMeta
381
394
  > {
@@ -399,7 +412,7 @@ Currently logged-in as ${this._username}.`
399
412
  `then lose to their real document once sync resumes.`
400
413
  );
401
414
  }
402
- await this.localDB.put<CourseRegistrationDoc>({
415
+ await this.putDefaultUnlessCreated<CourseRegistrationDoc>({
403
416
  _id: BaseUser.DOC_IDS.COURSE_REGISTRATIONS,
404
417
  courses: [],
405
418
  studyWeight: {},
@@ -705,7 +718,7 @@ Currently logged-in as ${this._username}.`
705
718
  `over what may be an existing one.`
706
719
  );
707
720
  }
708
- await this.localDB.put<UserConfig>(defaultConfig);
721
+ await this.putDefaultUnlessCreated<UserConfig>(defaultConfig);
709
722
  return this.getConfig();
710
723
  } else {
711
724
  logger.error(`Error setting user default config:`, e);
@@ -1050,27 +1063,24 @@ Currently logged-in as ${this._username}.`
1050
1063
  // stringify the current record to make it writable to couchdb
1051
1064
  record.timeStamp = moment.utc(record.timeStamp).toString() as unknown as Moment;
1052
1065
 
1053
- try {
1054
- const cardHistory = await this.update<CardHistory<T>>(
1055
- cardHistoryID,
1056
- function (h: CardHistory<T>) {
1057
- h.records.push(record);
1058
- h.bestInterval = h.bestInterval || 0;
1059
- // Scheduling (SpacedRepetition.newQuestionInterval) re-derives
1060
- // lapses/streak from `records` on every call, so these persisted
1061
- // fields are informational — kept current so the stored doc reads
1062
- // truthfully when inspected outside a session.
1063
- if (areQuestionRecords(h)) {
1064
- h.lapses = getLapses(h.records);
1065
- h.streak = getStreak(h.records);
1066
- } else {
1067
- h.lapses = h.lapses || 0;
1068
- h.streak = h.streak || 0;
1069
- }
1070
- return h;
1071
- }
1072
- );
1073
-
1066
+ const append = (h: CardHistory<T>): CardHistory<T> => {
1067
+ h.records.push(record);
1068
+ h.bestInterval = h.bestInterval || 0;
1069
+ // Scheduling (SpacedRepetition.newQuestionInterval) re-derives
1070
+ // lapses/streak from `records` on every call, so these persisted
1071
+ // fields are informational — kept current so the stored doc reads
1072
+ // truthfully when inspected outside a session.
1073
+ if (areQuestionRecords(h)) {
1074
+ h.lapses = getLapses(h.records);
1075
+ h.streak = getStreak(h.records);
1076
+ } else {
1077
+ h.lapses = h.lapses || 0;
1078
+ h.streak = h.streak || 0;
1079
+ }
1080
+ return h;
1081
+ };
1082
+ const appendToExisting = async () => {
1083
+ const cardHistory = await this.update<CardHistory<T>>(cardHistoryID, append);
1074
1084
  // Convert timestamps to moment objects
1075
1085
  cardHistory.records = cardHistory.records.map<T>((record) => {
1076
1086
  const ret: T = {
@@ -1080,6 +1090,10 @@ Currently logged-in as ${this._username}.`
1080
1090
  return ret;
1081
1091
  });
1082
1092
  return cardHistory;
1093
+ };
1094
+
1095
+ try {
1096
+ return await appendToExisting();
1083
1097
  } catch (e) {
1084
1098
  const reason = e as PouchError;
1085
1099
  if (reason.status === 404) {
@@ -1096,6 +1110,11 @@ Currently logged-in as ${this._username}.`
1096
1110
  const putResult = await this.writeDB.put<CardHistory<T>>(initCardHistory);
1097
1111
  return { ...initCardHistory, _rev: putResult.rev };
1098
1112
  } catch (creationError) {
1113
+ // Another record for this card created the doc first (two first
1114
+ // records in flight at once): it exists now, so append to it.
1115
+ if ((creationError as PouchError).name === 'conflict') {
1116
+ return appendToExisting();
1117
+ }
1099
1118
  throw new Error(
1100
1119
  `Failed to create CardHistory for ${cardHistoryID}. Reason: ${creationError}`
1101
1120
  );
@@ -1,6 +1,7 @@
1
1
  import pouch from './pouchdb-setup';
2
2
  import { getCourseDB } from '.';
3
3
  import { logger } from '../../util/logger';
4
+ import { perfMark } from '../../util/perfMarks';
4
5
  import type { CourseConfig } from '@vue-skuilder/common';
5
6
 
6
7
  // ============================================================================
@@ -23,6 +24,15 @@ import type { CourseConfig } from '@vue-skuilder/common';
23
24
  // This avoids propagating per-interaction ELO write noise to every syncing
24
25
  // client. Each client's local snapshot refreshes on the next page load.
25
26
  //
27
+ // A replica that completed a sync on an earlier page load carries a
28
+ // `_local/` marker, and is handed out as ready immediately; its refresh
29
+ // (epoch check, incremental pull, view warming) runs in the background, into
30
+ // the same DB. Sessions may therefore start on the previous visit's data and
31
+ // catch up seconds later. If that refresh finds the remote DB recreated (an
32
+ // epoch change), the replica is NOT destroyed under running readers: the
33
+ // marker is dropped, this page keeps the outdated copy, and the next page
34
+ // load rebuilds it from scratch (the path a device without a replica takes).
35
+ //
26
36
  // Live replication is intentionally NOT supported. The remote course DB
27
37
  // receives high-frequency ELO updates from all concurrent users — live
28
38
  // sync would cause constant re-indexing of local PouchDB views.
@@ -41,6 +51,12 @@ export type CourseSyncState =
41
51
  | 'disabled'
42
52
  | 'error';
43
53
 
54
+ /**
55
+ * Written into the local replica when a sync completes. `_local/` docs don't
56
+ * replicate, so it describes this device's copy.
57
+ */
58
+ const SYNC_MARKER_ID = '_local/sk-course-sync-complete';
59
+
44
60
  /**
45
61
  * Detailed sync status for observability.
46
62
  */
@@ -178,26 +194,11 @@ export class CourseSyncService {
178
194
  async ensureSynced(courseId: string, forceEnabled?: boolean): Promise<void> {
179
195
  const existing = this.entries.get(courseId);
180
196
 
181
- // Already synced — but check if the remote DB has been recreated
182
- // (e.g., after a dev reseed). The seed script writes a `db-epoch`
183
- // doc; if the remote epoch differs from our local copy, the local
184
- // replica is stale and must be destroyed before re-syncing.
197
+ // Ready on this page load. Its epoch was checked on the way (by
198
+ // performSync, or by the background refresh, which keeps an outdated
199
+ // replica rather than destroy it under running sessions).
185
200
  if (existing?.status.state === 'ready' && existing.localDB) {
186
- const stale = await this.isLocalEpochStale(courseId, existing.localDB);
187
- if (!stale) {
188
- return;
189
- }
190
- logger.info(
191
- `[CourseSyncService] Remote DB epoch changed for course ${courseId} — destroying stale local replica`
192
- );
193
- try {
194
- await existing.localDB.destroy();
195
- } catch {
196
- // Ignore cleanup errors
197
- }
198
- existing.localDB = null;
199
- existing.readyPromise = null;
200
- // Fall through to start a fresh sync
201
+ return;
201
202
  }
202
203
 
203
204
  // Already disabled
@@ -218,10 +219,125 @@ export class CourseSyncService {
218
219
  };
219
220
  this.entries.set(courseId, entry);
220
221
 
221
- entry.readyPromise = this.performSync(courseId, entry, forceEnabled);
222
+ entry.readyPromise = this.startSync(courseId, entry, forceEnabled);
222
223
  return entry.readyPromise;
223
224
  }
224
225
 
226
+ /**
227
+ * A replica completed on an earlier page load is ready now, refreshed in the
228
+ * background; anything else goes through a full performSync.
229
+ */
230
+ private async startSync(
231
+ courseId: string,
232
+ entry: SyncEntry,
233
+ forceEnabled?: boolean
234
+ ): Promise<void> {
235
+ const localDB = new pouch(this.localDBName(courseId));
236
+ if (await this.hasSyncMarker(localDB)) {
237
+ entry.localDB = localDB;
238
+ entry.status = { state: 'ready' };
239
+ entry.readyPromise = null;
240
+ perfMark('sk:course-sync-ready', { courseId, from: 'marker' });
241
+ logger.info(
242
+ `[CourseSyncService] Using this device's replica of course ${courseId}; refreshing in the background`
243
+ );
244
+ void this.refreshInBackground(courseId, entry, forceEnabled);
245
+ return;
246
+ }
247
+ return this.performSync(courseId, entry, forceEnabled);
248
+ }
249
+
250
+ /**
251
+ * Bring a ready replica up to date without taking it away from readers.
252
+ * Failures leave it ready (and outdated).
253
+ */
254
+ private async refreshInBackground(
255
+ courseId: string,
256
+ entry: SyncEntry,
257
+ forceEnabled?: boolean
258
+ ): Promise<void> {
259
+ const localDB = entry.localDB!;
260
+ try {
261
+ if (!forceEnabled && !(await this.checkLocalSyncEnabled(courseId))) {
262
+ await this.clearSyncMarker(localDB);
263
+ logger.info(
264
+ `[CourseSyncService] Local sync no longer enabled for course ${courseId}; replica kept for this page only`
265
+ );
266
+ return;
267
+ }
268
+
269
+ if (await this.isLocalEpochStale(courseId, localDB)) {
270
+ await this.clearSyncMarker(localDB);
271
+ logger.info(
272
+ `[CourseSyncService] Remote DB recreated for course ${courseId}: keeping the outdated replica ` +
273
+ `for this page; it is rebuilt on the next page load`
274
+ );
275
+ return;
276
+ }
277
+
278
+ const start = Date.now();
279
+ const result = await this.replicate(this.getRemoteDB(courseId), localDB);
280
+ const syncTimeMs = Date.now() - start;
281
+ const warmStart = Date.now();
282
+ await this.warmViewIndices(localDB);
283
+ const viewWarmTimeMs = Date.now() - warmStart;
284
+ await this.writeSyncMarker(localDB);
285
+ entry.status = {
286
+ state: 'ready',
287
+ docsReplicated: result.docs_written,
288
+ syncTimeMs,
289
+ viewWarmTimeMs,
290
+ };
291
+ perfMark('sk:course-sync-refreshed', {
292
+ courseId,
293
+ docs: result.docs_written,
294
+ syncTimeMs,
295
+ viewWarmTimeMs,
296
+ });
297
+ logger.info(
298
+ `[CourseSyncService] Background refresh of course ${courseId}: ` +
299
+ `${result.docs_written} docs in ${syncTimeMs}ms, views ${viewWarmTimeMs}ms`
300
+ );
301
+ } catch (e) {
302
+ logger.warn(
303
+ `[CourseSyncService] Background refresh of course ${courseId} failed; replica kept as is: ${e}`
304
+ );
305
+ }
306
+ }
307
+
308
+ private async hasSyncMarker(localDB: PouchDB.Database): Promise<boolean> {
309
+ try {
310
+ await localDB.get(SYNC_MARKER_ID);
311
+ return true;
312
+ } catch {
313
+ return false;
314
+ }
315
+ }
316
+
317
+ private async writeSyncMarker(localDB: PouchDB.Database): Promise<void> {
318
+ try {
319
+ let _rev: string | undefined;
320
+ try {
321
+ _rev = (await localDB.get(SYNC_MARKER_ID))._rev;
322
+ } catch {
323
+ // First completion on this device.
324
+ }
325
+ await localDB.put({ _id: SYNC_MARKER_ID, _rev, completedAt: new Date().toISOString() });
326
+ } catch (e) {
327
+ // Costs the next page load the full-sync path, not correctness.
328
+ logger.warn(`[CourseSyncService] Could not write sync marker: ${e}`);
329
+ }
330
+ }
331
+
332
+ private async clearSyncMarker(localDB: PouchDB.Database): Promise<void> {
333
+ try {
334
+ const marker = await localDB.get(SYNC_MARKER_ID);
335
+ await localDB.remove(marker);
336
+ } catch {
337
+ // Absent already.
338
+ }
339
+ }
340
+
225
341
  /**
226
342
  * Get the local PouchDB for a course, or null if not available.
227
343
  *
@@ -312,6 +428,11 @@ export class CourseSyncService {
312
428
 
313
429
  const result = await this.replicate(remoteDB, localDB);
314
430
  const syncTimeMs = Date.now() - syncStart;
431
+ perfMark('sk:course-sync-replicated', {
432
+ courseId,
433
+ docs: result.docs_written,
434
+ ms: syncTimeMs,
435
+ });
315
436
 
316
437
  logger.info(
317
438
  `[CourseSyncService] Replication complete for course ${courseId}: ` +
@@ -328,6 +449,8 @@ export class CourseSyncService {
328
449
  `[CourseSyncService] View indices warmed for course ${courseId} in ${viewWarmTimeMs}ms`
329
450
  );
330
451
 
452
+ await this.writeSyncMarker(localDB);
453
+
331
454
  // Done
332
455
  entry.status = {
333
456
  state: 'ready',
@@ -335,6 +458,7 @@ export class CourseSyncService {
335
458
  syncTimeMs,
336
459
  viewWarmTimeMs,
337
460
  };
461
+ perfMark('sk:course-sync-ready', { courseId, from: 'sync', viewWarmTimeMs });
338
462
  } catch (e) {
339
463
  const errorMsg = e instanceof Error ? e.message : String(e);
340
464
  logger.error(
@@ -442,8 +566,11 @@ export class CourseSyncService {
442
566
  try {
443
567
  localEpoch = await localDB.get<{ epoch: number }>('db-epoch');
444
568
  } catch {
445
- // Local doesn't have the epoch doc — stale
446
- return true;
569
+ // No epoch doc locally: a DB never written to (this device's first
570
+ // sync) has nothing to be stale; anything else (e.g. an interrupted
571
+ // earlier sync) is.
572
+ const info = await localDB.info();
573
+ return info.doc_count > 0 || !!info.update_seq;
447
574
  }
448
575
 
449
576
  return remoteEpoch.epoch !== localEpoch.epoch;
@@ -28,6 +28,14 @@ export class CouchDataLayerProvider implements DataLayerProvider {
28
28
  // or group of courses
29
29
  private _courseIDs: string[] = [];
30
30
 
31
+ /**
32
+ * One CourseDB per course, so its caches (navigator, ELO pool, tags) last
33
+ * across sessions and card hydration. Rebuilt when the local replica it reads
34
+ * from changes (becomes ready, or is replaced); a session keeps the instance
35
+ * it started with.
36
+ */
37
+ private _courseDBs = new Map<string, { db: CourseDB; localDB: PouchDB.Database | null }>();
38
+
31
39
  constructor(coursIDs?: string[]) {
32
40
  if (coursIDs) {
33
41
  this._courseIDs = coursIDs;
@@ -75,7 +83,12 @@ export class CouchDataLayerProvider implements DataLayerProvider {
75
83
  // pass it to CourseDB so reads (pipeline, card hydration) run locally.
76
84
  // Writes always go to the remote DB (handled inside CourseDB).
77
85
  const localDB = CourseSyncService.getInstance().getLocalDB(courseId);
78
- return new CourseDB(courseId, async () => this.getUserDB(), localDB ?? undefined);
86
+ const cached = this._courseDBs.get(courseId);
87
+ if (cached && cached.localDB === localDB) return cached.db;
88
+
89
+ const db = new CourseDB(courseId, async () => this.getUserDB(), localDB ?? undefined);
90
+ this._courseDBs.set(courseId, { db, localDB });
91
+ return db;
79
92
  }
80
93
 
81
94
  /**
@@ -1,6 +1,4 @@
1
- import pouch from './pouchdb-setup';
2
- import { createPouchDBConfig } from '.';
3
- import { ENV } from '@db/factory';
1
+ import { getCourseDB } from '.';
4
2
  // import { DataShape } from '../..base-course/Interfaces/DataShape';
5
3
  import { NameSpacer, ShapeDescriptor } from '@vue-skuilder/common';
6
4
  import { CourseConfig, DataShape } from '@vue-skuilder/common';
@@ -171,14 +169,37 @@ async function addCard(
171
169
  return card;
172
170
  }
173
171
 
172
+ /**
173
+ * CourseConfig reads, cached per course for a few minutes: a study session reads
174
+ * it several times per pipeline run and per card. An edit made elsewhere shows
175
+ * up after the TTL (or a reload); writes through this client invalidate.
176
+ */
177
+ const COURSE_CONFIG_TTL_MS = 5 * 60 * 1000;
178
+ const courseConfigCache = new Map<string, { at: number; config: Promise<CourseConfig> }>();
179
+
180
+ export function invalidateCourseConfigCache(courseID: string): void {
181
+ courseConfigCache.delete(courseID);
182
+ }
183
+
174
184
  export async function getCredentialledCourseConfig(courseID: string): Promise<CourseConfig> {
175
- try {
185
+ const hit = courseConfigCache.get(courseID);
186
+ if (hit && Date.now() - hit.at < COURSE_CONFIG_TTL_MS) {
187
+ // A copy: callers mutate configs (e.g. before writing them back).
188
+ return structuredClone(await hit.config);
189
+ }
190
+
191
+ const config = (async () => {
176
192
  const db = getCourseDB(courseID);
177
193
  const ret = await db.get<CourseConfig>('CourseConfig');
178
194
  ret.courseID = courseID;
179
- logger.info(`Returning course config: ${JSON.stringify(ret)}`);
195
+ logger.debug(`Fetched course config for ${courseID}`);
180
196
  return ret;
197
+ })();
198
+ courseConfigCache.set(courseID, { at: Date.now(), config });
199
+ try {
200
+ return structuredClone(await config);
181
201
  } catch (e) {
202
+ courseConfigCache.delete(courseID);
182
203
  logger.error(`Error fetching config for ${courseID}:`, e);
183
204
  throw e;
184
205
  }
@@ -275,10 +296,3 @@ export function getTagID(tagName: string): string {
275
296
  }
276
297
  }
277
298
 
278
- export function getCourseDB(courseID: string): PouchDB.Database {
279
- const dbName = `coursedb-${courseID}`;
280
- return new pouch(
281
- ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + dbName,
282
- createPouchDBConfig()
283
- );
284
- }
@@ -25,7 +25,13 @@ import {
25
25
  } from '../../core/types/types-legacy';
26
26
  import { logger } from '../../util/logger';
27
27
  import { GET_CACHED } from './clientCache';
28
- import { addNote55, addTagToCard, getCredentialledCourseConfig, getTagID } from './courseAPI';
28
+ import {
29
+ addNote55,
30
+ addTagToCard,
31
+ getCredentialledCourseConfig,
32
+ getTagID,
33
+ invalidateCourseConfigCache,
34
+ } from './courseAPI';
29
35
  import { DataLayerResult } from '@db/core/types/db';
30
36
  import { PouchError } from './types';
31
37
  import CourseLookup from './courseLookupDB';
@@ -128,6 +134,11 @@ export class CourseDB implements CourseDBInterface {
128
134
  * values may be stale, so read-modify-write cycles must go through
129
135
  * the remote DB to avoid conflicts).
130
136
  */
137
+ /** Reads go to this device's replica (perf marks report it as the session's binding). */
138
+ public get readsLocal(): boolean {
139
+ return this.db !== this.remoteDB;
140
+ }
141
+
131
142
  constructor(id: string, userLookup: () => Promise<UserDBInterface>, localDB?: PouchDB.Database) {
132
143
  this.id = id;
133
144
  const remote = getCourseDB(this.id);
@@ -401,12 +412,8 @@ above:\n${above.rows.map((r) => `\t${r.id}-${r.key}\n`)}`;
401
412
  }
402
413
 
403
414
  async getAppliedTags(cardId: string): Promise<PouchDB.Query.Response<TagStub>> {
404
- const ret = await getAppliedTags(this.id, cardId);
405
- if (ret) {
406
- return ret;
407
- } else {
408
- throw new Error(`Failed to find tags for card ${this.id}-${cardId}`);
409
- }
415
+ // this.db: the local replica when one is ready (the module function is remote).
416
+ return this.db.query<TagStub>('getTags', { startkey: cardId, endkey: cardId });
410
417
  }
411
418
 
412
419
  async getAppliedTagsBatch(cardIds: string[]): Promise<Map<string, string[]>> {
@@ -491,7 +498,8 @@ above:\n${above.rows.map((r) => `\t${r.id}-${r.key}\n`)}`;
491
498
  }
492
499
 
493
500
  async getCourseTagStubs(): Promise<PouchDB.Core.AllDocsResponse<Tag>> {
494
- return getCourseTagStubs(this.id);
501
+ // this.db: the local replica when one is ready (the module function is remote).
502
+ return filterAllDocsByPrefix<Tag>(this.db, DocType.TAG.valueOf() + '-');
495
503
  }
496
504
 
497
505
  async addNote(
@@ -1161,12 +1169,17 @@ ${JSON.stringify(config)}
1161
1169
  `);
1162
1170
 
1163
1171
  const db = getCourseDB(courseID);
1172
+ invalidateCourseConfigCache(courseID);
1164
1173
  const old = await getCredentialledCourseConfig(courseID);
1165
1174
 
1166
- return await db.put<CourseConfig>({
1167
- ...config,
1168
- _rev: (old as any)._rev,
1169
- });
1175
+ try {
1176
+ return await db.put<CourseConfig>({
1177
+ ...config,
1178
+ _rev: (old as any)._rev,
1179
+ });
1180
+ } finally {
1181
+ invalidateCourseConfigCache(courseID);
1182
+ }
1170
1183
  }
1171
1184
 
1172
1185
  function isSuccessRow<T>(
@@ -69,12 +69,23 @@ function getCouchDB(dbName: string): PouchDB.Database {
69
69
  );
70
70
  }
71
71
 
72
+ const courseDBHandles = new Map<string, PouchDB.Database>();
73
+
74
+ /**
75
+ * The remote course DB, one handle per URL for the page's lifetime.
76
+ *
77
+ * `skip_setup`: a fresh handle otherwise checks the DB exists (and would try to
78
+ * create it) before its first request: one extra round trip per handle. Course
79
+ * DBs are only created server-side (express), never through this handle.
80
+ */
72
81
  export function getCourseDB(courseID: string): PouchDB.Database {
73
- // todo: keep a cache of opened courseDBs? need to benchmark this somehow
74
- return new pouch(
75
- ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + 'coursedb-' + courseID,
76
- createPouchDBConfig()
77
- );
82
+ const url = ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + 'coursedb-' + courseID;
83
+ let db = courseDBHandles.get(url);
84
+ if (!db) {
85
+ db = new pouch(url, { ...createPouchDBConfig(), skip_setup: true });
86
+ courseDBHandles.set(url, db);
87
+ }
88
+ return db;
78
89
  }
79
90
 
80
91
  export async function getLatestVersion() {