@datagrok/hit-triage 1.10.8 → 1.11.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 (129) hide show
  1. package/.grokignore +2 -0
  2. package/.turbo/turbo-build.log +24 -6
  3. package/.vscode/settings.json +16 -0
  4. package/CHANGELOG.md +80 -1
  5. package/CLAUDE.md +87 -10
  6. package/README_HD.md +14 -14
  7. package/README_HT.md +6 -6
  8. package/README_PH.md +110 -0
  9. package/bdd/bdd.config.json +3 -0
  10. package/bdd/bindings/campaign.ts +626 -0
  11. package/bdd/bindings/hit-design.ts +93 -0
  12. package/bdd/bindings/landing.ts +839 -0
  13. package/bdd/bindings/pepti-hit.ts +35 -0
  14. package/bdd/features/campaign/crumbs.feature +22 -0
  15. package/bdd/features/campaign/grid.feature +56 -0
  16. package/bdd/features/campaign/merge.feature +105 -0
  17. package/bdd/features/campaign/molecule-vid.feature +40 -0
  18. package/bdd/features/campaign/permissions.feature +46 -0
  19. package/bdd/features/campaign/progress-tracker.feature +89 -0
  20. package/bdd/features/campaign/refresh-hint.feature +33 -0
  21. package/bdd/features/campaign/ribbon.feature +49 -0
  22. package/bdd/features/campaign/stage-choices.feature +39 -0
  23. package/bdd/features/campaign/submit.feature +135 -0
  24. package/bdd/features/campaign/tracker-cards.feature +46 -0
  25. package/bdd/features/campaign/view-only.feature +37 -0
  26. package/bdd/features/hit-design/campaign-column-metadata.feature +61 -0
  27. package/bdd/features/landing/campaign-deep-links.feature +61 -0
  28. package/bdd/features/landing/hit-design-campaigns-fixes.feature +116 -0
  29. package/bdd/features/landing/hit-design-campaigns.feature +153 -0
  30. package/bdd/features/landing/hit-triage-campaigns.feature +99 -0
  31. package/bdd/features/landing/new-campaign.feature +69 -0
  32. package/bdd/features/landing/pep-triage-campaigns.feature +41 -0
  33. package/bdd/features/landing/pepti-hit-landing.feature +132 -0
  34. package/bdd/features/pepti-hit/helm-edit.feature +47 -0
  35. package/bdd/features/templates/template-editor.feature +130 -0
  36. package/bdd/features/templates/template-picker.feature +112 -0
  37. package/bdd/features/templates/triage-forms.feature +59 -0
  38. package/bdd/fixtures/bar-chart.layout +1 -0
  39. package/bdd/fixtures/merge-local.csv +2 -0
  40. package/bdd/generated/campaign/crumbs.test.ts +33 -0
  41. package/bdd/generated/campaign/grid.test.ts +79 -0
  42. package/bdd/generated/campaign/merge.test.ts +134 -0
  43. package/bdd/generated/campaign/molecule-vid.test.ts +52 -0
  44. package/bdd/generated/campaign/permissions.test.ts +58 -0
  45. package/bdd/generated/campaign/progress-tracker.test.ts +129 -0
  46. package/bdd/generated/campaign/refresh-hint.test.ts +44 -0
  47. package/bdd/generated/campaign/ribbon.test.ts +65 -0
  48. package/bdd/generated/campaign/stage-choices.test.ts +55 -0
  49. package/bdd/generated/campaign/submit.test.ts +167 -0
  50. package/bdd/generated/campaign/tracker-cards.test.ts +49 -0
  51. package/bdd/generated/campaign/view-only.test.ts +51 -0
  52. package/bdd/generated/hit-design/campaign-column-metadata.test.ts +76 -0
  53. package/bdd/generated/landing/campaign-deep-links.test.ts +68 -0
  54. package/bdd/generated/landing/hit-design-campaigns-fixes.test.ts +99 -0
  55. package/bdd/generated/landing/hit-design-campaigns.test.ts +160 -0
  56. package/bdd/generated/landing/hit-triage-campaigns.test.ts +107 -0
  57. package/bdd/generated/landing/new-campaign.test.ts +78 -0
  58. package/bdd/generated/landing/pep-triage-campaigns.test.ts +52 -0
  59. package/bdd/generated/landing/pepti-hit-landing.test.ts +130 -0
  60. package/bdd/generated/pepti-hit/helm-edit.test.ts +64 -0
  61. package/bdd/generated/templates/template-editor.test.ts +137 -0
  62. package/bdd/generated/templates/template-picker.test.ts +119 -0
  63. package/bdd/generated/templates/triage-forms.test.ts +68 -0
  64. package/bdd/package.json +1 -0
  65. package/bdd/tsconfig.json +13 -0
  66. package/css/hit-triage.css +27 -23
  67. package/dist/package-test.js +690 -4
  68. package/dist/package-test.js.map +1 -1
  69. package/dist/package.js +318 -73
  70. package/dist/package.js.map +1 -1
  71. package/package.json +15 -8
  72. package/queries/locks.sql +37 -0
  73. package/src/app/accordeons/layout-input.ts +52 -44
  74. package/src/app/accordeons/new-campaign-accordeon.ts +91 -54
  75. package/src/app/accordeons/new-hit-design-campaign-accordeon.ts +3 -67
  76. package/src/app/accordeons/new-pep-triage-template-accordeon.ts +39 -41
  77. package/src/app/accordeons/new-template-accordeon.ts +41 -24
  78. package/src/app/ai/view-functions.ts +22 -19
  79. package/src/app/base-view.ts +99 -8
  80. package/src/app/consts.ts +8 -5
  81. package/src/app/dialogs/functions-dialog.ts +14 -9
  82. package/src/app/dialogs/merge-table-dialog.ts +181 -325
  83. package/src/app/hit-app-base.ts +61 -90
  84. package/src/app/hit-design-app.ts +492 -882
  85. package/src/app/hit-design-views/info-view.ts +57 -407
  86. package/src/app/hit-design-views/submit-view.ts +216 -111
  87. package/src/app/hit-design-views/tiles-view.ts +168 -123
  88. package/src/app/hit-design-views/utils.css +0 -8
  89. package/src/app/hit-triage-app.ts +69 -38
  90. package/src/app/hit-triage-views/info-view.ts +70 -371
  91. package/src/app/pep-triage-app.ts +2 -2
  92. package/src/app/pep-triage-views/info-view.ts +42 -83
  93. package/src/app/pepti-hit-app.ts +32 -183
  94. package/src/app/pepti-hits-views/info-view.ts +15 -37
  95. package/src/app/shared/campaign-catalog.ts +408 -0
  96. package/src/app/shared/campaign-session.ts +557 -0
  97. package/src/app/shared/compute-editor.ts +274 -0
  98. package/src/app/shared/hit-home.ts +45 -0
  99. package/src/app/shared/new-campaign-form.ts +90 -0
  100. package/src/app/shared/permissions-editor.ts +93 -0
  101. package/src/app/shared/template-editor.ts +331 -0
  102. package/src/app/shared/template-picker.ts +154 -0
  103. package/src/app/types.ts +2 -0
  104. package/src/app/utils/merge-table.ts +183 -78
  105. package/src/app/utils.ts +34 -325
  106. package/src/package-api.ts +29 -8
  107. package/src/package-test.ts +17 -2
  108. package/src/package.g.ts +12 -8
  109. package/src/package.ts +19 -7
  110. package/src/tests/ai.ts +244 -0
  111. package/src/tests/campaign-layout.ts +82 -0
  112. package/src/tests/campaign-session.ts +168 -0
  113. package/src/tests/campaigns.ts +23 -0
  114. package/src/tests/catalog.ts +156 -0
  115. package/src/tests/compat.ts +482 -0
  116. package/src/tests/compute.ts +165 -0
  117. package/src/tests/fixtures.ts +359 -0
  118. package/src/tests/merge.ts +149 -0
  119. package/src/tests/owners.ts +11 -0
  120. package/src/tests/save-lock.ts +932 -0
  121. package/src/tests/storage.ts +106 -0
  122. package/src/tests/template-editor.ts +538 -0
  123. package/src/tests/templates.ts +143 -0
  124. package/src/tests/triage-start.ts +81 -0
  125. package/src/tests/u2-guards.ts +384 -0
  126. package/src/tests/vid.ts +77 -0
  127. package/tsconfig.json +3 -0
  128. package/src/app/accordeons/new-hit-design-template-accordeon.ts +0 -238
  129. package/src/app/dialogs/permissions-dialog.ts +0 -66
@@ -1,9 +1,10 @@
1
1
  /* eslint-disable max-len */
2
2
  import * as DG from 'datagrok-api/dg';
3
3
  import * as grok from 'datagrok-api/grok';
4
- import {HitDesignApp} from '../hit-design-app';
4
+ import {Platform} from '@datagrok-libraries/u2/dg';
5
+ import type {HitDesignApp} from '../hit-design-app';
5
6
  import {HitAppBase} from '../hit-app-base';
6
- import {HitDesignMergeConfig, HitDesignMergeMode, HitDesignTemplate} from '../types';
7
+ import {HitDesignMergeConfig, HitDesignMergeMode, HitDesignTemplate, TemplateCompute} from '../types';
7
8
  import {HitDesignMolColName, TileCategoriesColName, ViDColName, ViDSemType} from '../consts';
8
9
  import {calculateCellValues} from './calculate-single-cell';
9
10
  import {registerMolsBatch} from './molreg';
@@ -12,10 +13,8 @@ import {_package} from '../../package';
12
13
  const MOL_NAME_HINTS = ['molecule', 'smiles', 'mol', 'structure', 'canonical_smiles', 'canonicalsmiles'];
13
14
  const VID_NAME_HINTS = ['v-id', 'vid', 'v_id'];
14
15
 
15
- // Mirrors the drag-n-drop path in HitDesignApp.handleUploadingDataframe: csv goes through
16
- // DG.DataFrame.fromCsv, anything else is delegated to a registered file-handler function.
17
- // HitAppBase.molFileExtReaders is statically filtered to only those extensions for which
18
- // the platform actually exposes a reader, so this list reflects what we can really parse.
16
+ // What a merge or a dropped file can be: csv, and every extension the platform has a reader for
17
+ // (HitAppBase.molFileExtReaders keeps only those).
19
18
  export const MERGE_ALLOWED_EXTENSIONS: string[] = ['csv', ...HitAppBase.molFileExtReaders.map((r) => r.ext)];
20
19
 
21
20
  export function getFileInfoExtension(fi: DG.FileInfo): string {
@@ -28,21 +27,25 @@ export function getFileInfoExtension(fi: DG.FileInfo): string {
28
27
 
29
28
  export async function readFileInfoToDataFrame(fi: DG.FileInfo): Promise<DG.DataFrame | null> {
30
29
  const ext = getFileInfoExtension(fi);
31
- if (ext === 'csv') {
32
- const text = await fi.readAsString();
33
- return DG.DataFrame.fromCsv(text);
34
- }
30
+ return ext === 'csv' ? DG.DataFrame.fromCsv(await fi.readAsString()) : readMoleculeFile(ext, await fi.readAsBytes());
31
+ }
32
+
33
+ /** A file dropped from the user's computer. */
34
+ export async function readFileToDataFrame(file: File): Promise<DG.DataFrame | null> {
35
+ const ext = file.name.split('.').pop()?.toLowerCase() ?? '';
36
+ return ext === 'csv' ? DG.DataFrame.fromCsv(await file.text()) :
37
+ readMoleculeFile(ext, new Uint8Array(await file.arrayBuffer()));
38
+ }
39
+
40
+ async function readMoleculeFile(ext: string, bytes: Uint8Array | null): Promise<DG.DataFrame | null> {
35
41
  const reader = HitAppBase.molFileExtReaders.find((r) => r.ext === ext);
36
- if (!reader) return null;
37
- const bytes = await fi.readAsBytes();
38
- if (!bytes) return null;
42
+ if (!reader || !bytes) return null;
39
43
  const bytesArg = reader.handlerFunc.inputs[0].name;
40
44
  const result: any = await reader.handlerFunc.apply({[bytesArg]: bytes});
41
45
  return Array.isArray(result) ? result[0] : result;
42
46
  }
43
47
 
44
- export function guessMoleculeColumn(df: DG.DataFrame): DG.Column | null {
45
- const cols = df.columns.toList();
48
+ export function guessMoleculeColumn(cols: DG.Column[]): DG.Column | null {
46
49
  const semCols = cols.filter((c) => c.semType === DG.SEMTYPE.MOLECULE);
47
50
  if (semCols.length > 0) {
48
51
  const named = semCols.find((c) => MOL_NAME_HINTS.includes(c.name.toLowerCase()));
@@ -53,8 +56,7 @@ export function guessMoleculeColumn(df: DG.DataFrame): DG.Column | null {
53
56
  return cols.find((c) => c.type === DG.COLUMN_TYPE.STRING) ?? null;
54
57
  }
55
58
 
56
- export function guessVidColumn(df: DG.DataFrame): DG.Column | null {
57
- const cols = df.columns.toList();
59
+ export function guessVidColumn(cols: DG.Column[]): DG.Column | null {
58
60
  const named = cols.find((c) => VID_NAME_HINTS.includes(c.name.toLowerCase()));
59
61
  if (named) return named;
60
62
  const semCol = cols.find((c) => c.semType === ViDSemType);
@@ -62,6 +64,29 @@ export function guessVidColumn(df: DG.DataFrame): DG.Column | null {
62
64
  return null;
63
65
  }
64
66
 
67
+ /** Marks a column the campaign view added empty for a layout's viewer bound to it: saved only once it holds values. */
68
+ export const PlaceholderTag = '.hit-design-placeholder';
69
+
70
+ /** A layout's column nothing has filled yet: a merge gives it the incoming column's type, a save leaves it out. */
71
+ export function isEmptyPlaceholder(col: DG.Column): boolean {
72
+ return col.getTag(PlaceholderTag) === 'true' && col.stats.missingValueCount === col.length;
73
+ }
74
+
75
+ /** The campaign a merge goes into: the one the app has open, or the one an open is reading. */
76
+ export interface MergeTarget {
77
+ df: DG.DataFrame;
78
+ molColName: string;
79
+ campaignId?: string;
80
+ stages: string[];
81
+ compute?: TemplateCompute;
82
+ }
83
+
84
+ function openTarget<T extends HitDesignTemplate>(app: HitDesignApp<T>): MergeTarget {
85
+ if (!app.dataFrame) throw new Error('Campaign has no data');
86
+ return {df: app.dataFrame, molColName: app.molColName, campaignId: app.campaignId, stages: app.stages,
87
+ compute: app.campaign?.template?.compute ?? app.template?.compute};
88
+ }
89
+
65
90
  export function isLocalUploadFileInfo(fi: DG.FileInfo): boolean {
66
91
  const fullPath = (fi.fullPath ?? '').toLowerCase();
67
92
  const name = (fi.name ?? '').toLowerCase();
@@ -77,7 +102,7 @@ function getSourceMappings(
77
102
  cfg: HitDesignMergeConfig,
78
103
  ): SourceMapping[] {
79
104
  const sources: SourceMapping[] = [];
80
- for (const col of incomingDf.columns.toList()) {
105
+ for (const col of Platform.listedColumns(incomingDf)) {
81
106
  // In SMILES mode, never carry over a V-iD-like column from the incoming table:
82
107
  // campaign V-iDs are authoritative.
83
108
  if (cfg.mode === 'smiles' && col.name === ViDColName) continue;
@@ -89,7 +114,7 @@ function getSourceMappings(
89
114
  sources.push({col, target, isComputed: false});
90
115
  }
91
116
  if (computedDf) {
92
- for (const col of computedDf.columns.toList()) {
117
+ for (const col of Platform.listedColumns(computedDf)) {
93
118
  if (col.name === HitDesignMolColName || col.name === campMolColName) continue;
94
119
  if (sources.some((s) => s.target === col.name)) continue;
95
120
  sources.push({col, target: col.name, isComputed: true});
@@ -193,7 +218,7 @@ function buildIncomingKeys(incomingDf: DG.DataFrame, cfg: HitDesignMergeConfig):
193
218
  }
194
219
 
195
220
  /**
196
- * Merges `incomingDf` into the current campaign of `app` based on the supplied config.
221
+ * Merges `incomingDf` into the current campaign of `app` (or `target`) based on the supplied config.
197
222
  * Supports both SMILES-based and V-iD-based joins, optional addition of new rows,
198
223
  * and optional execution of the campaign compute pipeline on the incoming molecules.
199
224
  */
@@ -201,10 +226,10 @@ export async function mergeIntoCampaign<T extends HitDesignTemplate>(
201
226
  app: HitDesignApp<T>,
202
227
  incomingDf: DG.DataFrame,
203
228
  cfg: HitDesignMergeConfig,
229
+ target: MergeTarget = openTarget(app),
204
230
  ): Promise<void> {
205
- if (!app.dataFrame) throw new Error('Campaign has no data');
206
- const campDf = app.dataFrame;
207
- const campMolColName = app.molColName;
231
+ const campDf = target.df;
232
+ const campMolColName = target.molColName;
208
233
 
209
234
  // Effective flags: silently strip combinations that the dialog should already have prevented.
210
235
  const canAddNewRows = cfg.addNewRows && !!cfg.molColName;
@@ -230,7 +255,7 @@ export async function mergeIntoCampaign<T extends HitDesignTemplate>(
230
255
  // original incoming rows.
231
256
  let computedDf: DG.DataFrame | null = null;
232
257
  if (willRunCompute && cfg.molColName && unmatched.length > 0) {
233
- const compute = app.campaign?.template?.compute ?? app.template?.compute;
258
+ const compute = target.compute;
234
259
  const incMolCol = incomingDf.col(cfg.molColName);
235
260
  if (compute && incMolCol) {
236
261
  const newMols = unmatched.map((i) => incMolCol.get(i));
@@ -246,17 +271,22 @@ export async function mergeIntoCampaign<T extends HitDesignTemplate>(
246
271
 
247
272
  const sources = getSourceMappings(incomingDf, computedDf, campMolColName, cfg);
248
273
 
249
- // Add columns missing in the campaign
274
+ // Add columns missing in the campaign; a layout's empty placeholder takes the incoming column's type
250
275
  const newlyAdded = new Set<string>();
251
276
  for (const s of sources) {
252
- if (!campDf.columns.contains(s.target)) {
253
- const nc = campDf.columns.addNew(s.target, s.col.type);
254
- if (s.col.semType) nc.semType = s.col.semType;
255
- newlyAdded.add(s.target);
256
- }
277
+ const existing = campDf.col(s.target);
278
+ if (existing !== null && !isEmptyPlaceholder(existing))
279
+ continue;
280
+ if (existing === null)
281
+ campDf.columns.addNew(s.target, s.col.type);
282
+ else if (existing.type !== s.col.type)
283
+ campDf.changeColumnType(s.target, s.col.type as DG.COLUMN_TYPE);
284
+ if (s.col.semType) campDf.col(s.target)!.semType = s.col.semType;
285
+ newlyAdded.add(s.target);
257
286
  }
258
287
 
259
- app.isJoining = true;
288
+ const newRowStartIdx = campDf.rowCount;
289
+ app.joining++;
260
290
  try {
261
291
  // Fill matched rows. Computed columns are skipped here — they were only computed for
262
292
  // the newly added subset and do not have values aligned with matched incoming rows.
@@ -276,70 +306,145 @@ export async function mergeIntoCampaign<T extends HitDesignTemplate>(
276
306
  }
277
307
 
278
308
  // Add unmatched rows.
279
- const newRowStartIdx = campDf.rowCount;
280
- if (unmatched.length > 0) {
281
- const hasStages = (app.stages?.length ?? 0) > 0 && campDf.columns.contains(TileCategoriesColName);
282
- for (let u = 0; u < unmatched.length; u++) {
283
- const incRow = unmatched[u];
284
- campDf.rows.addNew(null, true);
285
- const newCampRow = campDf.rowCount - 1;
286
- for (const s of sources) {
287
- // Computed columns are aligned with the unmatched array (index `u`); incoming
288
- // columns stay aligned with the original incoming row index.
289
- const fromIdx = s.isComputed ? u : incRow;
290
- setCorrectedValue(campDf.col(s.target)!, s.col, newCampRow, fromIdx);
291
- }
292
- if (hasStages) {
293
- const stageCol = campDf.col(TileCategoriesColName)!;
294
- if (isCellEmpty(stageCol, newCampRow))
295
- stageCol.set(newCampRow, app.stages[0], false);
296
- }
309
+ const hasStages = target.stages.length > 0 && campDf.columns.contains(TileCategoriesColName);
310
+ for (let u = 0; u < unmatched.length; u++) {
311
+ const incRow = unmatched[u];
312
+ campDf.rows.addNew(null, true);
313
+ const newCampRow = campDf.rowCount - 1;
314
+ for (const s of sources) {
315
+ // Computed columns are aligned with the unmatched array (index `u`); incoming
316
+ // columns stay aligned with the original incoming row index.
317
+ const fromIdx = s.isComputed ? u : incRow;
318
+ setCorrectedValue(campDf.col(s.target)!, s.col, newCampRow, fromIdx);
297
319
  }
298
-
299
- // Re-register new molecules so they get fresh, dictionary-backed V-iDs.
300
- // This deliberately overwrites any V-iD value carried over from the incoming table
301
- // (incoming V-iDs are not authoritative for the campaign).
302
- if (cfg.molColName) {
303
- const incMolCol = incomingDf.col(cfg.molColName);
304
- const campVidCol = campDf.col(ViDColName);
305
- if (incMolCol && campVidCol && app.campaignId) {
306
- const newMols = unmatched.map((i) => incMolCol.get(i)).filter((m) => !!m && m !== '');
307
- if (newMols.length > 0) {
308
- try {
309
- const vidMap = await registerMolsBatch(newMols, app.campaignId, app.appName);
310
- for (let j = 0; j < unmatched.length; j++) {
311
- const mol = incMolCol.get(unmatched[j]);
312
- const vid = mol ? vidMap.get(mol) : undefined;
313
- if (vid) campVidCol.set(newRowStartIdx + j, vid, false);
314
- }
315
- } catch (e) {
316
- _package.logger.error(e);
317
- }
318
- }
319
- }
320
+ if (hasStages) {
321
+ const stageCol = campDf.col(TileCategoriesColName)!;
322
+ if (isCellEmpty(stageCol, newCampRow))
323
+ stageCol.set(newCampRow, target.stages[0], false);
320
324
  }
321
325
  }
322
326
  } finally {
323
- setTimeout(() => {app.isJoining = false;}, 500);
327
+ app.joining--;
328
+ }
329
+
330
+ // Re-register new molecules so they get fresh, dictionary-backed V-iDs.
331
+ // This deliberately overwrites any V-iD value carried over from the incoming table
332
+ // (incoming V-iDs are not authoritative for the campaign).
333
+ const incMolCol = cfg.molColName ? incomingDf.col(cfg.molColName) : null;
334
+ const campVidCol = campDf.col(ViDColName);
335
+ const newMols = incMolCol ? unmatched.map((i) => incMolCol.get(i)).filter((m) => !!m && m !== '') : [];
336
+ if (incMolCol && campVidCol && target.campaignId && newMols.length > 0) {
337
+ try {
338
+ const vidMap = await registerMolsBatch(newMols, target.campaignId, app.appName);
339
+ for (let j = 0; j < unmatched.length; j++) {
340
+ const mol = incMolCol.get(unmatched[j]);
341
+ const vid = mol ? vidMap.get(mol) : undefined;
342
+ if (vid) campVidCol.set(newRowStartIdx + j, vid, false);
343
+ }
344
+ }
345
+ catch (e) {
346
+ _package.logger.error(e);
347
+ }
324
348
  }
325
349
 
326
350
  campDf.fireValuesChanged();
327
351
  }
328
352
 
353
+ /**
354
+ * A save over a campaign another session saved since this one read it: the server's table is merged into `ours` by
355
+ * canonical SMILES. Its rows we lack are added and its new columns copied into the rows we share, as before; in a
356
+ * shared row a cell we left as we read it ({@link base}) takes the server's value, so the other session's edits are
357
+ * kept, and a cell we edited keeps ours; a column we did not hold when we read the campaign we read as empty. Rows
358
+ * without a valid molecule are never matched. Of a molecule more than one row holds on either side, which row is which
359
+ * is a guess: its cells stay ours, and a new column takes the server's later row's value.
360
+ */
361
+ export function mergeServerTable<T extends HitDesignTemplate>(app: HitDesignApp<T>, ours: DG.DataFrame,
362
+ molColName: string, server: DG.DataFrame, base: DG.DataFrame | undefined): void {
363
+ if (!server.col(molColName) || !ours.col(molColName))
364
+ throw new Error('Molecule column not found');
365
+ const key = (v: string | null): string | null => {
366
+ const smiles = v ? _package.convertToSmiles(v) : null;
367
+ return smiles && smiles !== 'MALFORMED_INPUT_VALUE' ? smiles : null;
368
+ };
369
+ /** Each row's key, the (last) row of each key, and the keys more than one row holds. */
370
+ const rowsOf = (df: DG.DataFrame | undefined) => {
371
+ const mols = df?.col(molColName);
372
+ const keys = mols ? Array.from({length: df!.rowCount}, (_, i) => key(mols.get(i))) : [];
373
+ const rows = new Map<string, number>();
374
+ const twice = new Set<string>();
375
+ for (const [i, k] of keys.entries()) {
376
+ if (k === null)
377
+ continue;
378
+ if (rows.has(k))
379
+ twice.add(k);
380
+ rows.set(k, i);
381
+ }
382
+ return {keys, rows, twice};
383
+ };
384
+ const mine = rowsOf(ours);
385
+ const theirs = rowsOf(server);
386
+ const read = rowsOf(base);
387
+ app.joining++;
388
+ try {
389
+ // a column we lack, or hold only as a layout's empty placeholder, comes as the server has it
390
+ const added = new Set<string>();
391
+ for (const col of server.columns) {
392
+ const own = ours.col(col.name);
393
+ if (own !== null && !isEmptyPlaceholder(own))
394
+ continue;
395
+ if (own === null)
396
+ ours.columns.addNew(col.name, col.type);
397
+ else if (own.type !== col.type)
398
+ ours.changeColumnType(col.name, col.type as DG.COLUMN_TYPE);
399
+ if (col.semType)
400
+ ours.col(col.name)!.semType = col.semType;
401
+ added.add(col.name);
402
+ }
403
+ for (let i = 0; i < server.rowCount; i++) {
404
+ const k = theirs.keys[i];
405
+ if (k === null)
406
+ continue;
407
+ const row = mine.rows.get(k);
408
+ if (row === undefined) {
409
+ ours.rows.addNew(null, true);
410
+ for (const col of server.columns)
411
+ setCorrectedValue(ours.col(col.name)!, col, ours.rowCount - 1, i);
412
+ continue;
413
+ }
414
+ const baseRow = read.rows.get(k);
415
+ const threeWay = baseRow !== undefined && !mine.twice.has(k) && !theirs.twice.has(k) && !read.twice.has(k);
416
+ for (const col of server.columns) {
417
+ if (col.name === molColName)
418
+ continue;
419
+ const target = ours.col(col.name)!;
420
+ if (added.has(col.name)) {
421
+ setCorrectedValue(target, col, row, i);
422
+ continue;
423
+ }
424
+ const baseCol = base?.col(col.name);
425
+ const before = baseCol ? baseCol.getString(baseRow!) : '';
426
+ if (threeWay && target.getString(row) === before && target.getString(row) !== col.getString(i))
427
+ setCorrectedValue(target, col, row, i);
428
+ }
429
+ }
430
+ } finally {
431
+ app.joining--;
432
+ }
433
+ ours.fireValuesChanged();
434
+ }
435
+
329
436
  /**
330
437
  * Runs the implicit "auto-merge on campaign open" pass: if the campaign has a saved
331
- * mergeConfig with `autoMergeOnOpen` set and a `filePath` from the file share, re-read the
332
- * file and merge it into the campaign in-place. Compute is force-disabled for this pass
438
+ * mergeConfig (`cfg`) with `autoMergeOnOpen` set and a `filePath` from the file share, re-read the
439
+ * file and merge it into the campaign the open reads (`target`). Compute is force-disabled for this pass
333
440
  * and the campaign is NOT saved — the merged state lives in memory until the user saves
334
441
  * it themselves. Failures are logged and surfaced as a warning balloon; they never bubble
335
442
  * up to the open flow so the campaign still opens.
336
443
  */
337
444
  export async function autoMergeOnCampaignOpen<T extends HitDesignTemplate>(
338
- app: HitDesignApp<T>,
445
+ app: HitDesignApp<T>, cfg: HitDesignMergeConfig | undefined, target: MergeTarget,
339
446
  ): Promise<void> {
340
- const cfg = app.campaign?.mergeConfig;
341
447
  if (!cfg?.autoMergeOnOpen || !cfg.filePath) return;
342
- if (!app.dataFrame) return;
343
448
  try {
344
449
  const exists = await grok.dapi.files.exists(cfg.filePath);
345
450
  if (!exists) {
@@ -356,7 +461,7 @@ export async function autoMergeOnCampaignOpen<T extends HitDesignTemplate>(
356
461
  await grok.data.detectSemanticTypes(df);
357
462
  // Compute pipeline never runs on the implicit pass — only structural data flows in.
358
463
  const passCfg: HitDesignMergeConfig = {...cfg, runComputeOnNewRows: false};
359
- await mergeIntoCampaign(app, df, passCfg);
464
+ await mergeIntoCampaign(app, df, passCfg, target);
360
465
  } catch (e) {
361
466
  _package.logger.error(e);
362
467
  grok.shell.warning('Auto-merge failed; opening the campaign without merging.');