fsanyoto commited on
Commit
aadfd74
·
verified ·
1 Parent(s): 92127d3

Deploy AIOS web (React glide grid + FastAPI slice)

Browse files
RELEASES.json CHANGED
@@ -1,497 +1,11 @@
1
  {
2
- "current": "bb3a8714cd79044c8a7f99ddd29afbf5a891cdd3 (bb3a8714cd79)",
3
  "releases": [
4
  {
5
- "version": "v83",
6
- "sha": "9b7471968e79",
7
- "date": "2026-10-21",
8
- "subject": "release v83"
9
- },
10
- {
11
- "version": "v82",
12
- "sha": "9c61b1ee94d0",
13
- "date": "2026-10-17",
14
- "subject": "release v82"
15
- },
16
- {
17
- "version": "v81",
18
- "sha": "e141ccc85ef0",
19
- "date": "2026-10-15",
20
- "subject": "release v81"
21
- },
22
- {
23
- "version": "v80",
24
- "sha": "869b8fcc62ac",
25
- "date": "2026-10-13",
26
- "subject": "release v80"
27
- },
28
- {
29
- "version": "v79",
30
- "sha": "406417b8ded0",
31
- "date": "2026-10-13",
32
- "subject": "release v79"
33
- },
34
- {
35
- "version": "v78",
36
- "sha": "aad6561d63e6",
37
- "date": "2026-10-09",
38
- "subject": "release v78: SDLC 71 with the D-7536 and D-7537 repairs"
39
- },
40
- {
41
- "version": "v77",
42
- "sha": "e741e5ca604d",
43
- "date": "2026-10-05",
44
- "subject": "release v77"
45
- },
46
- {
47
- "version": "v76",
48
- "sha": "7990f03e0254",
49
- "date": "2026-10-01",
50
- "subject": "release v76"
51
- },
52
- {
53
- "version": "v75",
54
- "sha": "2c5146981ccf",
55
- "date": "2026-09-19",
56
- "subject": "release v75"
57
- },
58
- {
59
- "version": "v74",
60
- "sha": "40dceba9262e",
61
- "date": "2026-09-14",
62
- "subject": "release v74: waves 58+59 incl. the Amazon credential door (W59-T21/22/23) and sync producer (W59-T42); hotfix promote so"
63
- },
64
- {
65
- "version": "v73",
66
- "sha": "bb94de5665e6",
67
- "date": "2026-09-13",
68
- "subject": "release v73"
69
- },
70
- {
71
- "version": "v71",
72
- "sha": "96143dc6fb65",
73
- "date": "2026-09-04",
74
- "subject": "release v71 - wave 51, QA'd 24 PASS / 6 FAIL / 5 UNPROVEN"
75
- },
76
- {
77
- "version": "v70",
78
- "sha": "eedfb6a00163",
79
- "date": "2026-09-03",
80
- "subject": "release v70 - wave 50 (grid analytics, the Sheet as a spreadsheet, view trash and audit)"
81
- },
82
- {
83
- "version": "v69",
84
- "sha": "8598f56293a0",
85
- "date": "2026-09-02",
86
- "subject": "release v69: wave48-continuation validated + hotfixes v67/v68/d4e3e31 + go-live QA repairs (Supplier option limit, optio"
87
- },
88
- {
89
- "version": "v68",
90
- "sha": "62b065a2610a",
91
- "date": "2026-09-01",
92
- "subject": "release v68: unified holiday date offsets"
93
- },
94
- {
95
- "version": "v67",
96
- "sha": "3b2241982cf7",
97
- "date": "2026-09-01",
98
- "subject": "release v67"
99
- },
100
- {
101
- "version": "v66",
102
- "sha": "73c94f076740",
103
- "date": "2026-08-31",
104
- "subject": "release v66"
105
- },
106
- {
107
- "version": "v65",
108
- "sha": "9f6cfedb8a43",
109
- "date": "2026-08-31",
110
- "subject": "release v65"
111
- },
112
- {
113
- "version": "v64",
114
- "sha": "bb8985c6e73e",
115
- "date": "2026-08-31",
116
- "subject": "release v64"
117
- },
118
- {
119
- "version": "v63",
120
- "sha": "ec75cb8f3412",
121
- "date": "2026-08-31",
122
- "subject": "release v63"
123
- },
124
- {
125
- "version": "v62",
126
- "sha": "2c8f702c964f",
127
- "date": "2026-08-31",
128
- "subject": "release v62"
129
- },
130
- {
131
- "version": "v61",
132
- "sha": "bbc4c0760ab5",
133
- "date": "2026-08-30",
134
- "subject": "v61 - wave 46 hotfix: D-590 schema overlay reaches the grid read (all three assemblies), D-599 route to zone association"
135
- },
136
- {
137
- "version": "v60",
138
- "sha": "dd1b0620210e",
139
- "date": "2026-08-30",
140
- "subject": "v60 - wave 46"
141
- },
142
- {
143
- "version": "v59",
144
- "sha": "3cb73ce8021e",
145
- "date": "2026-08-29",
146
- "subject": "release v59"
147
- },
148
- {
149
- "version": "v58",
150
- "sha": "c526cede82f9",
151
- "date": "2026-08-29",
152
- "subject": "release v58"
153
- },
154
- {
155
- "version": "v57",
156
- "sha": "4859c035e6a7",
157
- "date": "2026-08-29",
158
- "subject": "release v57"
159
- },
160
- {
161
- "version": "v56",
162
- "sha": "2970b729e8f0",
163
- "date": "2026-08-28",
164
- "subject": "release v56"
165
- },
166
- {
167
- "version": "v55",
168
- "sha": "b766b904b8fa",
169
- "date": "2026-08-28",
170
- "subject": "release v55"
171
- },
172
- {
173
- "version": "v54",
174
- "sha": "6a8e99a064d9",
175
- "date": "2026-08-27",
176
- "subject": "release v54"
177
- },
178
- {
179
- "version": "v53",
180
- "sha": "89ed2cb867da",
181
- "date": "2026-08-24",
182
- "subject": "release v53"
183
- },
184
- {
185
- "version": "v52",
186
- "sha": "826349376241",
187
- "date": "2026-08-23",
188
- "subject": "release v52"
189
- },
190
- {
191
- "version": "v51",
192
- "sha": "5740d20fa585",
193
- "date": "2026-08-23",
194
- "subject": "release v51"
195
- },
196
- {
197
- "version": "v50",
198
- "sha": "8027c83d1da6",
199
- "date": "2026-08-23",
200
- "subject": "release v50"
201
- },
202
- {
203
- "version": "v49",
204
- "sha": "9a14c01a48a4",
205
- "date": "2026-08-23",
206
- "subject": "release v49"
207
- },
208
- {
209
- "version": "v48",
210
- "sha": "3c69fd93da7c",
211
- "date": "2026-08-23",
212
- "subject": "release v48"
213
- },
214
- {
215
- "version": "v47",
216
- "sha": "917f9a0eb509",
217
- "date": "2026-08-23",
218
- "subject": "release v47"
219
- },
220
- {
221
- "version": "v46",
222
- "sha": "a8dc531b5078",
223
- "date": "2026-08-23",
224
- "subject": "release v46"
225
- },
226
- {
227
- "version": "v45",
228
- "sha": "0b2b5010d86f",
229
- "date": "2026-08-23",
230
- "subject": "release v45"
231
- },
232
- {
233
- "version": "v44",
234
- "sha": "a7877aaf20ff",
235
- "date": "2026-08-23",
236
- "subject": "Hotfix: allow renaming the active Map view"
237
- },
238
- {
239
- "version": "v43",
240
- "sha": "e2ba4616fa0f",
241
- "date": "2026-08-23",
242
- "subject": "release v43"
243
- },
244
- {
245
- "version": "v42",
246
- "sha": "25b885e6373a",
247
- "date": "2026-08-23",
248
- "subject": "release v42"
249
- },
250
- {
251
- "version": "v41",
252
- "sha": "1d6adcc3c3ac",
253
- "date": "2026-08-22",
254
- "subject": "v41 vertical route navigation"
255
- },
256
- {
257
- "version": "v40",
258
- "sha": "1451837168f3",
259
- "date": "2026-08-22",
260
- "subject": "release v40 route UI correction"
261
- },
262
- {
263
- "version": "v39",
264
- "sha": "a78bdf00e38e",
265
- "date": "2026-08-21",
266
- "subject": "W39 clean stale migration targets"
267
- },
268
- {
269
- "version": "v38",
270
- "sha": "a556631cf979",
271
- "date": "2026-08-21",
272
- "subject": "W39 reversible Live store cutover"
273
- },
274
- {
275
- "version": "v37",
276
- "sha": "1ddd8cf82379",
277
- "date": "2026-08-21",
278
- "subject": "W39: coalesce derived workspace reads"
279
- },
280
- {
281
- "version": "v36",
282
- "sha": "51db3b7fee2c",
283
- "date": "2026-08-21",
284
- "subject": "W39 share-field oid repair"
285
- },
286
- {
287
- "version": "v35",
288
- "sha": "0f642129c8f7",
289
- "date": "2026-08-21",
290
- "subject": "W39 visual QA fixture"
291
- },
292
- {
293
- "version": "v34",
294
- "sha": "cb4caa188e05",
295
- "date": "2026-08-21",
296
- "subject": "W39 Staging PostgreSQL egress controls"
297
- },
298
- {
299
- "version": "v33",
300
- "sha": "2b115e573d6a",
301
- "date": "2026-08-21",
302
- "subject": "W39: stop nonproduction Postgres background egress"
303
- },
304
- {
305
- "version": "v32",
306
- "sha": "b47b27ca7b2a",
307
- "date": "2026-08-21",
308
- "subject": "W39: repair Staging Postgres projection"
309
- },
310
- {
311
- "version": "v31",
312
- "sha": "8880a49a244e",
313
- "date": "2026-08-21",
314
- "subject": "v31: Wave 39 staging candidate"
315
- },
316
- {
317
- "version": "v30",
318
- "sha": "71776ca6c8df",
319
- "date": "2026-08-20",
320
- "subject": "v30: W39-T29 Member-owned View first-share hotfix"
321
- },
322
- {
323
- "version": "v29",
324
- "sha": "55ced5018ff2",
325
- "date": "2026-08-18",
326
- "subject": "v29: the build staging has been running since 2026-08-18 (55ced50)."
327
- },
328
- {
329
- "version": "v28",
330
- "sha": "2793e4ba53dc",
331
- "date": "2026-08-18",
332
- "subject": "release v28"
333
- },
334
- {
335
- "version": "v27",
336
- "sha": "c05a794837fc",
337
- "date": "2026-08-18",
338
- "subject": "release v27"
339
- },
340
- {
341
- "version": "v25",
342
- "sha": "bc248295ed66",
343
- "date": "2026-08-14",
344
- "subject": "v25 - wave 32's LIVE build (deployed 2026-08-13, bc24829)"
345
- },
346
- {
347
- "version": "v26",
348
- "sha": "6a642e139cfa",
349
- "date": "2026-08-14",
350
- "subject": "v26 - wave 33's build (deployed 2026-08-14, 6a642e1)"
351
- },
352
- {
353
- "version": "v24",
354
- "sha": "514d8292ce73",
355
- "date": "2026-08-09",
356
- "subject": "release v24"
357
- },
358
- {
359
- "version": "v23",
360
- "sha": "44cc28380fca",
361
- "date": "2026-08-09",
362
- "subject": "release v23"
363
- },
364
- {
365
- "version": "v22",
366
- "sha": "f14e199ff32f",
367
- "date": "2026-08-09",
368
- "subject": "release v22"
369
- },
370
- {
371
- "version": "v21",
372
- "sha": "5fdffce8b558",
373
- "date": "2026-08-09",
374
- "subject": "release v21"
375
- },
376
- {
377
- "version": "v20",
378
- "sha": "442e4a1430a7",
379
- "date": "2026-08-09",
380
- "subject": "release v20"
381
- },
382
- {
383
- "version": "v19",
384
- "sha": "94cb4a4dfdfe",
385
- "date": "2026-08-09",
386
- "subject": "release v19"
387
- },
388
- {
389
- "version": "v18",
390
- "sha": "0106ef949805",
391
- "date": "2026-08-09",
392
- "subject": "release v18"
393
- },
394
- {
395
- "version": "v17",
396
- "sha": "308240cc75e8",
397
- "date": "2026-08-09",
398
- "subject": "release v17"
399
- },
400
- {
401
- "version": "v16",
402
- "sha": "a804df54430f",
403
- "date": "2026-08-09",
404
- "subject": "release v16"
405
- },
406
- {
407
- "version": "v15",
408
- "sha": "6b84d7264c81",
409
- "date": "2026-08-09",
410
- "subject": "release v15"
411
- },
412
- {
413
- "version": "v14",
414
- "sha": "bee5eaf5b5cb",
415
- "date": "2026-08-07",
416
- "subject": "release v14"
417
- },
418
- {
419
- "version": "v13",
420
- "sha": "314c0e368b1d",
421
- "date": "2026-08-06",
422
- "subject": "release v13"
423
- },
424
- {
425
- "version": "v12",
426
- "sha": "4908f399785d",
427
- "date": "2026-08-06",
428
- "subject": "release v12"
429
- },
430
- {
431
- "version": "v11",
432
- "sha": "312661712b98",
433
- "date": "2026-08-05",
434
- "subject": "release v11"
435
- },
436
- {
437
- "version": "v10",
438
- "sha": "43f804287496",
439
- "date": "2026-08-05",
440
- "subject": "release v10"
441
- },
442
- {
443
- "version": "v9",
444
- "sha": "2e26f57396b1",
445
- "date": "2026-08-05",
446
- "subject": "release v9"
447
- },
448
- {
449
- "version": "v8",
450
- "sha": "8073b3ecbac3",
451
- "date": "2026-08-05",
452
- "subject": "release v8"
453
- },
454
- {
455
- "version": "v7",
456
- "sha": "b1c5a7e9b848",
457
- "date": "2026-08-05",
458
- "subject": "release v7"
459
- },
460
- {
461
- "version": "v6",
462
- "sha": "a344020f114a",
463
- "date": "2026-08-05",
464
- "subject": "release v6"
465
- },
466
- {
467
- "version": "v5",
468
- "sha": "c0b0fbb40a7b",
469
- "date": "2026-08-05",
470
- "subject": "release v5"
471
- },
472
- {
473
- "version": "v4",
474
- "sha": "de5037ff1c8f",
475
- "date": "2026-08-05",
476
- "subject": "release v4"
477
- },
478
- {
479
- "version": "v3",
480
- "sha": "6b9fa62d93e8",
481
- "date": "2026-08-05",
482
- "subject": "release v3"
483
- },
484
- {
485
- "version": "v2",
486
- "sha": "fd0586199858",
487
- "date": "2026-08-04",
488
- "subject": "release v2"
489
- },
490
- {
491
- "version": "v1",
492
- "sha": "5b4e2c4bc3f3",
493
- "date": "2026-08-04",
494
- "subject": "release v1"
495
  }
496
  ]
497
  }
 
1
  {
2
+ "current": "v84 (7bb3a2e09e)",
3
  "releases": [
4
  {
5
+ "version": "v84",
6
+ "sha": "7bb3a2e09e",
7
+ "date": "2026-10-24",
8
+ "subject": "release v84"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  }
10
  ]
11
  }
VERSION CHANGED
@@ -1 +1 @@
1
- bb3a8714cd79044c8a7f99ddd29afbf5a891cdd3 (bb3a8714cd79)
 
1
+ v84 (7bb3a2e09e)
api/connector_onboarding.py CHANGED
@@ -87,6 +87,12 @@ def ensure_salespeople_selection(rt, *, namespace=None):
87
  rt.update(rel.CONFIG_KEY, admit, flush="sync")
88
 
89
 
 
 
 
 
 
 
90
  def selected_mirror_entities(rt):
91
  """None preserves old sync policy; an explicit setup limits source pulls to selected dependencies."""
92
  import odoo_relational as rel
@@ -106,8 +112,12 @@ def selected_mirror_entities(rt):
106
  for key, enabled in grids.items():
107
  if not enabled:
108
  continue
109
- if key in {"customer_data", "product_data"}:
110
- entities.add({"customer_data": "res_partner", "product_data": "product_product"}[key])
 
 
 
 
111
  continue
112
  if key not in sources:
113
  continue
 
87
  rt.update(rel.CONFIG_KEY, admit, flush="sync")
88
 
89
 
90
+ #: Every mirror table the product pool reads. A selected tenant with product data syncs all of them.
91
+ PRODUCT_DATA_ENTITIES = ("product_product", "product_pricelist", "product_pricelist_item",
92
+ "product_tag", "stock_quant", "stock_location", "sale_order",
93
+ "sale_order_line", "res_partner")
94
+
95
+
96
  def selected_mirror_entities(rt):
97
  """None preserves old sync policy; an explicit setup limits source pulls to selected dependencies."""
98
  import odoo_relational as rel
 
112
  for key, enabled in grids.items():
113
  if not enabled:
114
  continue
115
+ if key == "customer_data":
116
+ entities.add("res_partner")
117
+ continue
118
+ if key == "product_data":
119
+ # The product pool reads only the mirror, so its data pulls every table the pool reads.
120
+ entities.update(PRODUCT_DATA_ENTITIES)
121
  continue
122
  if key not in sources:
123
  continue
api/registry_source.py CHANGED
@@ -558,8 +558,26 @@ def _canonical_batches(session, cur, table_key, *, ids=None, needed=None, metric
558
  else:
559
  raise err(503, 'connector_paused', 'This source is paused and its scoped snapshot is unavailable.')
560
  elif table_key == 'product_data':
561
- batches = product_data.source_batches(cur, st=runtime, team_id=team, ids=ids, needed=required,
562
- raw_fields=[field['rawName'] for field in raw])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
563
  else:
564
  batches = customer_data.source_batches(cur, st=runtime, agent_name=agent, team_id=team,
565
  ids=ids, needed=required, ledger_batches=items(session.runtime, customer_data.LEDGER_KEY,
 
558
  else:
559
  raise err(503, 'connector_paused', 'This source is paused and its scoped snapshot is unavailable.')
560
  elif table_key == 'product_data':
561
+ from harness import canonical_source
562
+ def mirrored():
563
+ # Stock, sales and product reads come from the Odoo mirror, never live Odoo. A lent
564
+ # cursor that cannot see it (Sheet's in-memory one) gets a child cursor on the mirror.
565
+ source_cur, owned = cur, None
566
+ if canonical_source.mirror_missing(cur):
567
+ import routes_odoo_tables as rot
568
+ base, mismatch = rot._mirror_cur(session)
569
+ if mismatch:
570
+ raise err(409, 'cross_tenant_store', mismatch)
571
+ source_cur = owned = base.cursor()
572
+ try:
573
+ source = product_data.source_batches(source_cur, st=runtime, team_id=team, ids=ids,
574
+ needed=required, raw_fields=[field['rawName'] for field in raw])
575
+ with canonical_source.mirrored(source_cur), closing(source):
576
+ yield from source
577
+ finally:
578
+ if owned is not None:
579
+ owned.close()
580
+ batches = mirrored()
581
  else:
582
  batches = customer_data.source_batches(cur, st=runtime, agent_name=agent, team_id=team,
583
  ids=ids, needed=required, ledger_batches=items(session.runtime, customer_data.LEDGER_KEY,
api/routes_products.py CHANGED
@@ -104,9 +104,12 @@ def _pool_for(rt, team_id):
104
  # Preserve the long-standing pool call shape when no raw field is enabled. Besides
105
  # keeping existing adapters valid, this avoids paying for an unnecessary `extra_fields`
106
  # branch on the overwhelmingly common/default-off path.
107
- rows = (pd.pool(team_id=team_id,
108
- raw_fields=[field.get("rawName") for field in enabled_raw])
109
- if enabled_raw else pd.pool(team_id=team_id))
 
 
 
110
  return _project_enabled_raw_rows(rows, enabled_raw, drop_source=True)
111
 
112
  return scope_cache.get(rt.pool_cache, key, _CACHE_TTL, build)
 
104
  # Preserve the long-standing pool call shape when no raw field is enabled. Besides
105
  # keeping existing adapters valid, this avoids paying for an unnecessary `extra_fields`
106
  # branch on the overwhelmingly common/default-off path.
107
+ from harness import canonical_source, runtime as _runtime
108
+ # Stock, sales and product reads come from the Odoo mirror; an unsynced mirror raises.
109
+ with canonical_source.mirrored(lambda: _runtime.mirror_cursor(rt).cursor()):
110
+ rows = (pd.pool(team_id=team_id,
111
+ raw_fields=[field.get("rawName") for field in enabled_raw])
112
+ if enabled_raw else pd.pool(team_id=team_id))
113
  return _project_enabled_raw_rows(rows, enabled_raw, drop_source=True)
114
 
115
  return scope_cache.get(rt.pool_cache, key, _CACHE_TTL, build)
platform/harness/canonical_source.py CHANGED
@@ -2,6 +2,8 @@
2
  import contextvars
3
  import itertools
4
  import json
 
 
5
  import uuid
6
  from contextlib import contextmanager
7
 
@@ -9,6 +11,7 @@ from core import odoo as _odoo
9
 
10
  BATCH = 1024
11
  _ACTIVE = contextvars.ContextVar('canonical_source', default=None)
 
12
 
13
 
14
  def code_sql(alias):
@@ -164,9 +167,10 @@ class Source:
164
  return {pid: {'top_category': cat, 'top_category_pct': pct, 'sku_count': n, 'top_sku': name}
165
  for pid, cat, pct, n, name in self.cur.execute(query).fetchall()}
166
 
167
- def categories(self):
168
  rows = self.products()
169
  cats = self.search_read('product.category', [], ['id', 'complete_name'])
 
170
  return {code: category for code, category in self.cur.execute(f"""SELECT
171
  {code_sql('p')} code,
172
  first(coalesce(nullif(trim(list_extract(string_split(json_extract_string(c.body, '/complete_name'), '/'), 2)), ''),
@@ -174,7 +178,7 @@ class Source:
174
  FROM "{rows.table}" p LEFT JOIN "{cats.table}" c
175
  ON json_extract_string(p.body, '/categ_id/0')=json_extract_string(c.body, '/id')
176
  WHERE json_extract_string(p.body, '/active') = 'true'
177
- AND json_type(p.body, '/default_code') = 'VARCHAR' AND json_extract_string(p.body, '/default_code') != ''
178
  GROUP BY code""").fetchall()}
179
 
180
  def backlog(self, groups):
@@ -237,21 +241,32 @@ class Source:
237
  canonical_unit=str(_odoo.m2o_name(r.get('packaging_id')) or '').strip()) for r in rules)
238
  prods = products.active_products() if prods is None else prods
239
  declared = sorted({name for _p, _u, name, _n in products.PRICELIST_TIER_COLUMNS})
240
- query = f"""WITH eligible AS (
241
- SELECT {code_sql('p')} code, p.ordinal product_order, r.ordinal rule_order,
242
- trim(json_extract_string(l.body, '/name')) pricelist,
 
 
 
 
 
 
 
 
243
  try_cast(json_extract_string(r.body, '/canonical_price') AS DOUBLE) price,
244
  json_extract_string(r.body, '/canonical_unit') unit
245
- FROM "{prods.table}" p JOIN "{normalized.table}" r ON (
246
- (json_extract_string(r.body, '/applied_on')='0_product_variant'
247
- AND json_extract_string(r.body, '/product_id/0')=json_extract_string(p.body, '/id')) OR
248
- (json_extract_string(r.body, '/applied_on')='1_product'
249
- AND json_extract_string(r.body, '/product_tmpl_id/0')=json_extract_string(p.body, '/product_tmpl_id/0')
250
- AND NOT EXISTS (SELECT 1 FROM "{normalized.table}" v
251
- WHERE json_extract_string(v.body, '/applied_on')='0_product_variant'
252
- AND json_extract_string(v.body, '/pricelist_id/0')=json_extract_string(r.body, '/pricelist_id/0')
253
- AND json_extract_string(v.body, '/product_id/0')=json_extract_string(p.body, '/id'))))
254
- JOIN "{lists.table}" l ON json_extract_string(l.body, '/id')=json_extract_string(r.body, '/pricelist_id/0')),
 
 
 
255
  identified AS (SELECT *, min(product_order) OVER (PARTITION BY code) first_product,
256
  row_number() OVER (PARTITION BY code, pricelist, price, unit ORDER BY product_order, rule_order) ident FROM eligible),
257
  merged AS (SELECT * FROM identified WHERE product_order=first_product OR ident=1),
@@ -353,13 +368,551 @@ class Source:
353
  self.cache.clear()
354
 
355
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
356
  def active():
357
  return _ACTIVE.get()
358
 
359
 
360
  @contextmanager
361
- def bounded(cur, kind, identities, store=None):
362
- source = Source(cur, kind, identities, store)
363
  token = _ACTIVE.set(source)
364
  try:
365
  yield source
@@ -370,6 +923,33 @@ def bounded(cur, kind, identities, store=None):
370
  source.close()
371
 
372
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373
  class _Odoo:
374
  def __getattr__(self, name):
375
  source = active()
@@ -377,6 +957,8 @@ class _Odoo:
377
  return getattr(source, name)
378
  if source is not None and name == 'get_odoo':
379
  return lambda: self
 
 
380
  return getattr(_odoo, name)
381
 
382
 
 
2
  import contextvars
3
  import itertools
4
  import json
5
+ import logging
6
+ import re
7
  import uuid
8
  from contextlib import contextmanager
9
 
 
11
 
12
  BATCH = 1024
13
  _ACTIVE = contextvars.ContextVar('canonical_source', default=None)
14
+ _LOG = logging.getLogger('canonical_source')
15
 
16
 
17
  def code_sql(alias):
 
167
  return {pid: {'top_category': cat, 'top_category_pct': pct, 'sku_count': n, 'top_sku': name}
168
  for pid, cat, pct, n, name in self.cur.execute(query).fetchall()}
169
 
170
+ def categories(self, keep_blank_code=False):
171
  rows = self.products()
172
  cats = self.search_read('product.category', [], ['id', 'complete_name'])
173
+ blank = '' if keep_blank_code else "AND json_extract_string(p.body, '/default_code') != ''"
174
  return {code: category for code, category in self.cur.execute(f"""SELECT
175
  {code_sql('p')} code,
176
  first(coalesce(nullif(trim(list_extract(string_split(json_extract_string(c.body, '/complete_name'), '/'), 2)), ''),
 
178
  FROM "{rows.table}" p LEFT JOIN "{cats.table}" c
179
  ON json_extract_string(p.body, '/categ_id/0')=json_extract_string(c.body, '/id')
180
  WHERE json_extract_string(p.body, '/active') = 'true'
181
+ AND json_type(p.body, '/default_code') = 'VARCHAR' {blank}
182
  GROUP BY code""").fetchall()}
183
 
184
  def backlog(self, groups):
 
241
  canonical_unit=str(_odoo.m2o_name(r.get('packaging_id')) or '').strip()) for r in rules)
242
  prods = products.active_products() if prods is None else prods
243
  declared = sorted({name for _p, _u, name, _n in products.PRICELIST_TIER_COLUMNS})
244
+ # Two equality joins and an anti-join: an OR join with a correlated NOT EXISTS is a nested loop
245
+ # (234 s for the whole catalogue on the staging mirror).
246
+ query = f"""WITH pp AS (
247
+ SELECT {code_sql('p')} code, p.ordinal product_order, json_extract_string(p.body, '/id') pid,
248
+ json_extract_string(p.body, '/product_tmpl_id/0') tmpl
249
+ FROM "{prods.table}" p),
250
+ rr AS (
251
+ SELECT r.ordinal rule_order, json_extract_string(r.body, '/applied_on') applied_on,
252
+ json_extract_string(r.body, '/product_id/0') rpid,
253
+ json_extract_string(r.body, '/product_tmpl_id/0') rtmpl,
254
+ json_extract_string(r.body, '/pricelist_id/0') plid,
255
  try_cast(json_extract_string(r.body, '/canonical_price') AS DOUBLE) price,
256
  json_extract_string(r.body, '/canonical_unit') unit
257
+ FROM "{normalized.table}" r),
258
+ ll AS (SELECT json_extract_string(body, '/id') plid, trim(json_extract_string(body, '/name')) pricelist
259
+ FROM "{lists.table}"),
260
+ shadow AS (SELECT DISTINCT plid, rpid FROM rr WHERE applied_on='0_product_variant'),
261
+ eligible AS (
262
+ SELECT pp.code, pp.product_order, rr.rule_order, ll.pricelist, rr.price, rr.unit
263
+ FROM pp JOIN rr ON rr.applied_on='0_product_variant' AND rr.rpid=pp.pid
264
+ JOIN ll ON ll.plid=rr.plid
265
+ UNION ALL
266
+ SELECT pp.code, pp.product_order, rr.rule_order, ll.pricelist, rr.price, rr.unit
267
+ FROM pp JOIN rr ON rr.applied_on='1_product' AND rr.rtmpl=pp.tmpl
268
+ JOIN ll ON ll.plid=rr.plid
269
+ ANTI JOIN shadow ON shadow.plid=rr.plid AND shadow.rpid=pp.pid),
270
  identified AS (SELECT *, min(product_order) OVER (PARTITION BY code) first_product,
271
  row_number() OVER (PARTITION BY code, pricelist, price, unit ORDER BY product_order, rule_order) ident FROM eligible),
272
  merged AS (SELECT * FROM identified WHERE product_order=first_product OR ident=1),
 
368
  self.cache.clear()
369
 
370
 
371
+ #: Tables a product pool needs before it may read the mirror instead of Odoo.
372
+ MIRROR_TABLES = ('product_product', 'stock_quant', 'stock_location', 'sale_order',
373
+ 'sale_order_line', 'res_partner')
374
+ _MIRROR_MODELS = {
375
+ 'product.product': 'product_product',
376
+ 'sale.order.line': 'sale_order_line',
377
+ 'sale.order': 'sale_order',
378
+ 'stock.quant': 'stock_quant',
379
+ 'stock.location': 'stock_location',
380
+ 'res.partner': 'res_partner',
381
+ 'product.pricelist': 'product_pricelist',
382
+ 'product.pricelist.item': 'product_pricelist_item',
383
+ 'product.tag': 'product_tag',
384
+ # product.category is not synced: its id and complete_name ride on product_product.
385
+ 'product.category': ('(SELECT DISTINCT categ_id AS id, categ_name AS complete_name '
386
+ 'FROM product_product WHERE categ_id IS NOT NULL)'),
387
+ }
388
+ # The only many2one hops a mirrored domain may cross, one level deep.
389
+ _MIRROR_RELATIONS = {
390
+ ('sale.order.line', 'order_id'): 'sale.order',
391
+ ('sale.order.line', 'product_id'): 'product.product',
392
+ ('sale.order.line', 'order_partner_id'): 'res.partner',
393
+ ('stock.quant', 'product_id'): 'product.product',
394
+ ('stock.quant', 'location_id'): 'stock.location',
395
+ ('sale.order', 'partner_id'): 'res.partner',
396
+ }
397
+ # Odoo's line state is the order's state, and the mirror stores it only on sale_order.
398
+ _MIRROR_ALIASES = {('sale.order.line', 'state'): 'order_id.state'}
399
+ _NUMERIC = ('DOUBLE', 'FLOAT', 'REAL', 'INTEGER', 'BIGINT', 'SMALLINT', 'TINYINT', 'HUGEINT',
400
+ 'UBIGINT', 'UINTEGER', 'USMALLINT', 'UTINYINT', 'DECIMAL')
401
+ _CODE_FIELD = '__code__'
402
+ #: The product pool's mirror contract, read as declared even before `datastore.ENTITIES` names it.
403
+ _DECLARED = {
404
+ 'product_product': {
405
+ 'create_date': 'VARCHAR', 'product_tmpl_id': 'BIGINT', 'product_tmpl_name': 'VARCHAR',
406
+ 'volume': 'DOUBLE', 'display_name': 'VARCHAR', 'product_tag_ids': 'VARCHAR',
407
+ 'incoming_qty': 'DOUBLE', 'free_qty': 'DOUBLE', 'virtual_available': 'DOUBLE'},
408
+ 'product_pricelist': {'id': 'BIGINT', 'name': 'VARCHAR'},
409
+ 'product_pricelist_item': {
410
+ 'id': 'BIGINT', 'pricelist_id': 'BIGINT', 'pricelist_name': 'VARCHAR',
411
+ 'product_id': 'BIGINT', 'product_name': 'VARCHAR', 'product_tmpl_id': 'BIGINT',
412
+ 'product_tmpl_name': 'VARCHAR', 'applied_on': 'VARCHAR', 'compute_price': 'VARCHAR',
413
+ 'fixed_price': 'DOUBLE', 'min_quantity': 'DOUBLE', 'date_start': 'VARCHAR',
414
+ 'date_end': 'VARCHAR', 'packaging_id': 'BIGINT', 'packaging_name': 'VARCHAR',
415
+ 'packaging_price': 'DOUBLE'},
416
+ 'product_tag': {'id': 'BIGINT', 'name': 'VARCHAR'},
417
+ }
418
+ _DATE_ONLY = re.compile(r'\d{4}-\d{2}-\d{2}')
419
+
420
+
421
+ def declared_columns(table):
422
+ """`{column: SQL type}` the mirror sync declares for `table`, as `datastore._cols` names them."""
423
+ from harness import datastore
424
+ out = {}
425
+ spec = datastore.ENTITIES.get(table)
426
+ for col in datastore._cols(spec) if spec else ():
427
+ name, typ = col.split()[:2]
428
+ out[name] = typ
429
+ out.update(_DECLARED.get(table) or {})
430
+ return out
431
+
432
+
433
+ def _kind(name, typ):
434
+ typ = str(typ).upper()
435
+ if typ == 'BOOLEAN':
436
+ return 'bool'
437
+ if typ.startswith(_NUMERIC):
438
+ return 'num'
439
+ # A many2many lands as a VARCHAR JSON list of ids.
440
+ return 'm2m' if name.endswith('_ids') else 'str'
441
+
442
+
443
+ class _Unmirrored(Exception):
444
+ """A model, field, path or operator the mirror cannot answer."""
445
+
446
+
447
+ def mirror_missing(cur):
448
+ """The `MIRROR_TABLES` this cursor cannot see, in declaration order."""
449
+ found = {r[0] for r in cur.execute(
450
+ 'SELECT DISTINCT table_name FROM information_schema.tables WHERE table_name IN '
451
+ '(SELECT unnest($1::VARCHAR[]))', [list(MIRROR_TABLES)]).fetchall()}
452
+ return [t for t in MIRROR_TABLES if t not in found]
453
+
454
+
455
+ def _unready(missing):
456
+ return ('canonical_source_mirror_unavailable: the Odoo mirror has not synced '
457
+ + ', '.join(missing) + ' yet, and the product pool does not read Odoo live')
458
+
459
+
460
+ def _sql_list_type(values):
461
+ if all(isinstance(v, bool) for v in values):
462
+ return 'BOOLEAN'
463
+ if all(isinstance(v, int) and not isinstance(v, bool) for v in values):
464
+ return 'BIGINT'
465
+ if all(isinstance(v, (int, float)) and not isinstance(v, bool) for v in values):
466
+ return 'DOUBLE'
467
+ if all(isinstance(v, str) for v in values):
468
+ return 'VARCHAR'
469
+ raise _Unmirrored('mixed value types ' + repr(values[:3]))
470
+
471
+
472
+ def _plain(value):
473
+ # Dates compare as Odoo's fixed-width text, the shape the mirror stores them in.
474
+ return str(value) if hasattr(value, 'isoformat') else value
475
+
476
+
477
+ class _Query:
478
+ """Translate one Odoo model's domain and fields into SQL over the mirror tables."""
479
+
480
+ def __init__(self, source, model, params=None):
481
+ if model not in _MIRROR_MODELS:
482
+ raise _Unmirrored('model ' + model)
483
+ self.source, self.model = source, model
484
+ self.params = params if params is not None else []
485
+ self.joins = {}
486
+
487
+ def param(self, value):
488
+ self.params.append(value)
489
+ return f'${len(self.params)}'
490
+
491
+ def _column(self, model, name, alias):
492
+ if name == _CODE_FIELD and model == 'product.product':
493
+ return (f"CASE WHEN {alias}.default_code IS NULL OR {alias}.default_code = '' "
494
+ f"THEN 'pid:' || CAST({alias}.id AS VARCHAR) ELSE trim({alias}.default_code) END"), 'str'
495
+ cols = self.source.columns(model)
496
+ kind = cols.get(name)
497
+ if kind is None:
498
+ # A declared column the sync has not filled yet reads blank; an undeclared one refuses.
499
+ typ = self.source.pending_column(model, name)
500
+ if typ is None:
501
+ raise _Unmirrored(f'{model}.{name}')
502
+ kind, expr = _kind(name, typ), f'CAST(NULL AS {typ})'
503
+ if name.endswith('_id') and kind == 'num':
504
+ return (expr, 'CAST(NULL AS VARCHAR)'), 'm2o'
505
+ return expr, kind
506
+ if name == 'id':
507
+ return f'{alias}.id', 'int'
508
+ if name.endswith('_id') and kind == 'num':
509
+ label = name[:-3] + '_name'
510
+ if label not in cols:
511
+ self.source.pending_column(model, label)
512
+ return (f'{alias}.{name}', f'{alias}.{label}' if label in cols else 'NULL'), 'm2o'
513
+ return f'{alias}.{name}', kind
514
+
515
+ def field(self, path):
516
+ """`(expr, kind, guard)` for a field path; a dotted path joins its many2one once."""
517
+ if not isinstance(path, str) or not path.replace('.', '').replace('_', '').isalnum():
518
+ raise _Unmirrored('path ' + repr(path))
519
+ path = _MIRROR_ALIASES.get((self.model, path), path)
520
+ head, _, rest = path.partition('.')
521
+ if not rest:
522
+ expr, kind = self._column(self.model, head, 'b')
523
+ return expr, kind, None
524
+ comodel = _MIRROR_RELATIONS.get((self.model, head))
525
+ if comodel is None or '.' in rest:
526
+ raise _Unmirrored(f'{self.model}.{path}')
527
+ self._column(self.model, head, 'b')
528
+ alias = 'j_' + head
529
+ self.joins[head] = (comodel, alias)
530
+ expr, kind = self._column(comodel, rest, alias)
531
+ return expr, kind, f'{alias}.id IS NOT NULL'
532
+
533
+ def leaf(self, term):
534
+ if not isinstance(term, (list, tuple)) or len(term) != 3:
535
+ raise _Unmirrored('term ' + repr(term))
536
+ path, op, value = term
537
+ op = {'==': '=', '<>': '!='}.get(str(op).lower(), str(op).lower())
538
+ expr, kind, guard = self.field(path)
539
+ if kind == 'm2m':
540
+ raise _Unmirrored(f'many2many filter on {self.model}.{path}')
541
+ if kind == 'str' and op in {'<=', '>'} and isinstance(value, str) and _DATE_ONLY.fullmatch(value):
542
+ # Odoo reads a bare date as its midnight, so a value of exactly midnight is <= that day.
543
+ value += ' 00:00:00'
544
+ if kind == 'm2o':
545
+ expr = expr[0]
546
+ if isinstance(value, str) or op in {'like', 'ilike', '=like', '=ilike'}:
547
+ raise _Unmirrored(f'name search on {path}')
548
+ if kind == 'bool':
549
+ expr = f'coalesce({expr}, false)'
550
+ value = [_plain(v) for v in value] if isinstance(value, (list, tuple, set)) else _plain(value)
551
+ empty = value is False or value is None
552
+ if op in {'=', '!='} and empty and kind != 'bool':
553
+ cond = f'{expr} IS NULL' if op == '=' else f'{expr} IS NOT NULL'
554
+ elif op in {'=', '!='}:
555
+ value = bool(value) if kind == 'bool' else value
556
+ cond = f'{expr} = {self.param(value)}' if op == '=' else \
557
+ f'({expr} <> {self.param(value)} OR {expr} IS NULL)'
558
+ elif op in {'in', 'not in'}:
559
+ values = list(value) if isinstance(value, list) else [value]
560
+ if kind == 'bool':
561
+ values, nulls = [bool(v) for v in values], False
562
+ else:
563
+ nulls = any(v is False or v is None for v in values)
564
+ values = [v for v in values if v is not False and v is not None]
565
+ member = (f'{expr} IN (SELECT unnest({self.param(values)}::{_sql_list_type(values)}[]))'
566
+ if values else 'false')
567
+ if nulls:
568
+ member = f'({member} OR {expr} IS NULL)'
569
+ cond = member if op == 'in' else f'NOT coalesce({member}, false)'
570
+ elif op in {'<', '<=', '>', '>='}:
571
+ cond = f'{expr} {op} {self.param(value)}'
572
+ elif op in {'=like', 'like', '=ilike', 'ilike'}:
573
+ pattern = value if op.startswith('=') else f'%{value}%'
574
+ cond = f"{expr} {'ILIKE' if 'ilike' in op else 'LIKE'} {self.param(pattern)}"
575
+ else:
576
+ raise _Unmirrored('operator ' + op)
577
+ return f'coalesce(({guard} AND {cond}), false)' if guard else f'coalesce(({cond}), false)'
578
+
579
+ def where(self, domain):
580
+ """Odoo prefix notation to SQL, with the model's default active filter."""
581
+ domain = list(domain or [])
582
+ stack = []
583
+ for term in reversed(domain):
584
+ if isinstance(term, str) and term in {'&', '|'}:
585
+ if len(stack) < 2:
586
+ raise _Unmirrored('malformed domain')
587
+ a, b = stack.pop(), stack.pop()
588
+ stack.append(f"({a} {'AND' if term == '&' else 'OR'} {b})")
589
+ elif isinstance(term, str) and term == '!':
590
+ if not stack:
591
+ raise _Unmirrored('malformed domain')
592
+ stack.append(f'(NOT {stack.pop()})')
593
+ else:
594
+ stack.append(self.leaf(term))
595
+ named = any(isinstance(t, (list, tuple)) and len(t) == 3 and t[0] == 'active' for t in domain)
596
+ if not named and 'active' in self.source.columns(self.model):
597
+ stack.append('coalesce(b.active, false)')
598
+ return ' AND '.join(reversed(stack)) or 'true'
599
+
600
+ def from_sql(self):
601
+ joins = ''.join(f' LEFT JOIN {self.source.relation(comodel)} {alias} ON {alias}.id = b.{head}'
602
+ for head, (comodel, alias) in self.joins.items())
603
+ return f'{self.source.relation(self.model)} b{joins}'
604
+
605
+
606
+ def _json_value(expr, kind, grouped=False):
607
+ if isinstance(expr, str) and expr.startswith('CAST(NULL AS'):
608
+ return "'[]'::JSON" if kind == 'm2m' else "'false'::JSON"
609
+ if kind == 'm2m':
610
+ return f"coalesce(TRY_CAST({expr} AS JSON), '[]'::JSON)"
611
+ if kind == 'm2o':
612
+ ident, label = expr
613
+ label = f'min({label})' if grouped else label
614
+ return f"CASE WHEN {ident} IS NULL THEN 'false'::JSON ELSE json_array({ident}, {label}) END"
615
+ if kind == 'bool':
616
+ return f'coalesce({expr}, false)'
617
+ if kind == 'num':
618
+ return f'coalesce({expr}, 0)'
619
+ if kind == 'int':
620
+ return expr
621
+ return f"CASE WHEN {expr} IS NULL THEN 'false'::JSON ELSE to_json({expr}) END"
622
+
623
+
624
+ def _refuse(method, model, cause):
625
+ return RuntimeError(f'canonical_source_unmirrored: {model}.{method} needs {cause}, '
626
+ 'which the Odoo mirror does not carry')
627
+
628
+
629
+ class MirrorSource(Source):
630
+ """A `Source` that answers every product pool read from the tenant's Odoo mirror, never live.
631
+
632
+ `live_reads` stays `[]`. `pending` names each declared `table.column` or `table` the sync has
633
+ not filled yet: those read blank or empty, logged once per root build.
634
+ """
635
+
636
+ def __init__(self, cur, kind, identities, store=None):
637
+ super().__init__(cur, kind, identities, store)
638
+ self.live_reads = []
639
+ self.pending = []
640
+ self.dropped = []
641
+ self.parent = None
642
+ self._columns = {}
643
+ self._relations = {}
644
+
645
+ def _note(self, bucket, name):
646
+ if name not in bucket:
647
+ bucket.append(name)
648
+
649
+ def relation(self, model):
650
+ """The FROM operand for `model`: its mirror table, or an empty typed one while unsynced."""
651
+ if model not in self._relations:
652
+ target = _MIRROR_MODELS[model]
653
+ if not target.startswith('(') and not self.cur.execute(
654
+ 'SELECT 1 FROM information_schema.tables WHERE table_name = $1 LIMIT 1',
655
+ [target]).fetchone():
656
+ declared = declared_columns(target)
657
+ if target in MIRROR_TABLES:
658
+ raise RuntimeError(_unready([target]))
659
+ if not declared:
660
+ raise _Unmirrored('table ' + target)
661
+ self._note(self.pending, target)
662
+ target = '(SELECT ' + ', '.join(f'CAST(NULL AS {t}) AS {c}' for c, t in declared.items()) \
663
+ + ' WHERE false)'
664
+ self._relations[model] = target
665
+ return self._relations[model]
666
+
667
+ def pending_column(self, model, name):
668
+ """The SQL type of a declared column the mirror table lacks, recorded as pending, or None."""
669
+ target = _MIRROR_MODELS[model]
670
+ if target.startswith('('):
671
+ return None
672
+ typ = declared_columns(target).get(name)
673
+ if typ is not None:
674
+ self._note(self.pending, f'{target}.{name}')
675
+ return typ
676
+
677
+ def categories(self, keep_blank_code=True):
678
+ # Live `_code_category` keys every `default_code != False` product, a blank '' code included.
679
+ return Source.categories(self, keep_blank_code)
680
+
681
+ def servable(self, model, names):
682
+ """The `names` the mirror declares for `model`; the rest are logged once and dropped."""
683
+ kept, dropped = [], []
684
+ for name in names:
685
+ try:
686
+ _Query(self, model).field(name)
687
+ kept.append(name)
688
+ except _Unmirrored:
689
+ dropped.append(name)
690
+ fresh = [n for n in dropped if f'{model}.{n}' not in self.dropped]
691
+ for name in fresh:
692
+ self._note(self.dropped, f'{model}.{name}')
693
+ if fresh:
694
+ _LOG.warning('canonical_source_dropped: the Odoo mirror does not declare %s, so the product '
695
+ 'pool leaves those fields blank. Declare them in harness.datastore.ENTITIES to '
696
+ 'sync them.', ', '.join(f'{model}.{n}' for n in fresh))
697
+ return kept
698
+
699
+ @property
700
+ def cur(self):
701
+ # A factory opens the mirror on the first read, so a build that reads nothing never opens it.
702
+ if self._cur is None:
703
+ opened = self._factory()
704
+ try:
705
+ missing = mirror_missing(opened)
706
+ except BaseException:
707
+ opened.close()
708
+ raise
709
+ if missing:
710
+ opened.close()
711
+ raise RuntimeError(_unready(missing))
712
+ self._cur = opened
713
+ return self._cur
714
+
715
+ @cur.setter
716
+ def cur(self, value):
717
+ lazy = callable(value) and not hasattr(value, 'execute')
718
+ self._factory, self._cur = (value, None) if lazy else (None, value)
719
+
720
+ def columns(self, model):
721
+ if model not in self._columns:
722
+ rows = self.cur.execute(f'DESCRIBE SELECT * FROM {self.relation(model)} AS t').fetchall()
723
+ self._columns[model] = {name: _kind(name, typ) for name, typ, *_ in rows}
724
+ return self._columns[model]
725
+
726
+ def _scoped(self, model, domain):
727
+ domain = list(domain or [])
728
+ if self.kind == 'product' and model == 'product.product':
729
+ domain.append((_CODE_FIELD, 'in', list(self.identities)))
730
+ elif self.kind == 'product' and model in {'sale.order.line', 'stock.quant'}:
731
+ domain.append(('product_id.' + _CODE_FIELD, 'in', list(self.identities)))
732
+ elif self.kind == 'customer':
733
+ domain = Source.domain(self, model, domain)
734
+ return domain
735
+
736
+ def _materialize(self, query, select, where, order, group=None):
737
+ name = 'canonical_' + uuid.uuid4().hex
738
+ self.cur.execute(f'CREATE TEMP TABLE "{name}" (ordinal BIGINT, body JSON)')
739
+ self.tables.append(name)
740
+ body = ', '.join(f"'{key}', {value}" for key, value in select)
741
+ grouping = f' GROUP BY {group}' if group else ''
742
+ try:
743
+ self.cur.execute(f'INSERT INTO "{name}" SELECT row_number() OVER (ORDER BY {order}) - 1, '
744
+ f'json_object({body}) FROM {query.from_sql()} WHERE {where}{grouping}',
745
+ query.params)
746
+ except BaseException as exc:
747
+ self.failure = exc
748
+ raise
749
+ return Rows(self, name)
750
+
751
+ def search_read(self, model, domain=None, fields=None, **kw):
752
+ wanted = list(dict.fromkeys(['id', *(['default_code'] if self.kind == 'product' and model == 'product.product' else []),
753
+ *(fields or [])]))
754
+ try:
755
+ query = _Query(self, model)
756
+ if not fields:
757
+ wanted = list(self.columns(model))
758
+ select = [(f, _json_value(*query.field(f)[:2])) for f in wanted]
759
+ where = query.where(self._scoped(model, domain))
760
+ except _Unmirrored as exc:
761
+ raise _refuse('search_read', model, exc) from None
762
+ return self._materialize(query, select, where, 'b.id')
763
+
764
+ def read_group(self, model, domain, fields, groupby, **kw):
765
+ groupby = [groupby] if isinstance(groupby, str) else list(groupby or [])
766
+ try:
767
+ query = _Query(self, model)
768
+ select, keys = [], []
769
+ for g in groupby:
770
+ if ':' in g:
771
+ raise _Unmirrored('date grouping ' + g)
772
+ expr, kind, _ = query.field(g)
773
+ if kind == 'm2m':
774
+ raise _Unmirrored('many2many grouping ' + g)
775
+ keys.append(expr[0] if kind == 'm2o' else expr)
776
+ select.append((g, _json_value(expr, kind, grouped=True)))
777
+ for spec in fields or []:
778
+ name, _, agg = str(spec).partition(':')
779
+ if name in groupby or name in {'id', '__count'}:
780
+ continue
781
+ expr, kind, _ = query.field(name)
782
+ if kind == 'm2m':
783
+ raise _Unmirrored('many2many aggregate ' + spec)
784
+ ident = expr[0] if kind == 'm2o' else expr
785
+ agg = agg or ('sum' if kind == 'num' else '')
786
+ if agg in {'sum', 'min', 'max', 'avg'}:
787
+ select.append((name, f'coalesce({agg}({ident}), 0)'))
788
+ elif agg == 'count':
789
+ select.append((name, f'count({ident})'))
790
+ elif agg == 'count_distinct':
791
+ select.append((name, f'count(DISTINCT {ident})'))
792
+ elif agg:
793
+ raise _Unmirrored('aggregate ' + spec)
794
+ select.append(('__count', 'count(*)'))
795
+ where = query.where(self._scoped(model, domain))
796
+ except _Unmirrored as exc:
797
+ raise _refuse('read_group', model, exc) from None
798
+ order = ', '.join(f'{k} NULLS LAST' for k in keys) or '1'
799
+ return self._materialize(query, select, where, order, group=', '.join(keys) or None)
800
+
801
+ def search_count(self, model, domain=None, **kw):
802
+ try:
803
+ query = _Query(self, model)
804
+ where = query.where(self._scoped(model, domain))
805
+ except _Unmirrored as exc:
806
+ raise _refuse('search_count', model, exc) from None
807
+ return self.cur.execute(f'SELECT count(*) FROM {query.from_sql()} WHERE {where}',
808
+ query.params).fetchone()[0]
809
+
810
+ def excluded_partner_ids(self):
811
+ """`core.odoo.excluded_partner_ids` resolved from res_partner, shared through its cache."""
812
+ ctx = _odoo._ctx()
813
+ other = ctx is not None and ctx['slug'] != _odoo._DEFAULT_SLUG
814
+ slug = ctx['slug'] if other else _odoo._DEFAULT_SLUG
815
+ names = (ctx['exclude_partner_names'] or set()) if other else _odoo.EXCLUDE_PARTNER_NAMES
816
+ with _odoo._EXCL_LOCK:
817
+ if slug in _odoo._EXCL_IDS:
818
+ return _odoo._EXCL_IDS[slug]
819
+ found = tuple(r[0] for r in self.cur.execute(
820
+ 'SELECT id FROM res_partner WHERE name IN (SELECT unnest($1::VARCHAR[])) '
821
+ 'AND coalesce(active, false) ORDER BY id', [sorted(names)]).fetchall()) if names else ()
822
+ with _odoo._EXCL_LOCK:
823
+ return _odoo._EXCL_IDS.setdefault(slug, found)
824
+
825
+ def sale_line_domain(self, *args, **kw):
826
+ # core.sale_line_domain calls its own excluded_partner_ids, so the cache is filled first.
827
+ self.excluded_partner_ids()
828
+ return _odoo.sale_line_domain(*args, **kw)
829
+
830
+ def inventory_frame(self, t):
831
+ from core import periods
832
+ import pandas as pd
833
+ lf, lt = periods.ltm(t)
834
+ params = []
835
+ prods = _Query(self, 'product.product', params)
836
+ quants = _Query(self, 'stock.quant', params)
837
+ sales = _Query(self, 'sale.order.line', params)
838
+ pw = prods.where(self._scoped('product.product', [('default_code', '!=', False)]))
839
+ qw = quants.where([('location_id.usage', '=', 'internal')])
840
+ sw = sales.where(_odoo.sale_line_domain(lf, lt, all_channels=True))
841
+ result = self.cur.execute(f"""WITH prods AS (SELECT b.id, trim(b.default_code) AS sku, b.name,
842
+ b.type = 'product' AS storable, coalesce(b.standard_price, 0) AS cost,
843
+ coalesce(nullif(trim(list_extract(string_split(b.categ_name, '/'), 2)), ''), '(uncategorized)') AS category
844
+ FROM {prods.from_sql()} WHERE {pw}),
845
+ quant AS (SELECT b.product_id id, sum(coalesce(b.quantity, 0)) qty
846
+ FROM {quants.from_sql()} WHERE {qw} GROUP BY 1),
847
+ sold AS (SELECT b.product_id id, sum(coalesce(b.product_uom_qty, 0)) qty,
848
+ sum(coalesce(b.price_subtotal, 0)) rev FROM {sales.from_sql()} WHERE {sw} GROUP BY 1)
849
+ SELECT p.sku, first(p.name ORDER BY p.id) AS name, first(p.category ORDER BY p.id) category,
850
+ bool_or(p.storable) storable, sum(coalesce(q.qty, 0)) on_hand,
851
+ sum(coalesce(q.qty, 0) * p.cost) inv_value, sum(coalesce(s.qty, 0) * p.cost) cogs_ltm,
852
+ sum(coalesce(s.qty, 0)) qty_ltm, sum(coalesce(s.rev, 0)) rev_ltm
853
+ FROM prods p LEFT JOIN quant q ON q.id = p.id LEFT JOIN sold s ON s.id = p.id
854
+ GROUP BY p.sku ORDER BY p.sku""", params)
855
+ names = [d[0] for d in result.description]
856
+ rows = []
857
+ while True:
858
+ batch = result.fetchmany(BATCH)
859
+ if not batch:
860
+ break
861
+ rows.extend(batch)
862
+ return pd.DataFrame(rows, columns=names), (lf, lt)
863
+
864
+ def fba_qty(self, product_ids=None):
865
+ """`warehouse.fba_qty_by_product_id`'s `(by_pid | None, report)` from the mirror's internal
866
+ quants under the FBA view location's parent_path."""
867
+ from modules import warehouse
868
+ view = self.cur.execute(
869
+ "SELECT id, parent_path, warehouse_id, warehouse_name FROM stock_location "
870
+ "WHERE usage = 'view' AND warehouse_id IS NOT NULL AND complete_name = $1 "
871
+ "AND coalesce(active, false) ORDER BY id LIMIT 1", [warehouse.FBA_WAREHOUSE_CODE]).fetchone()
872
+ if view is None:
873
+ return None, warehouse.no_fba_warehouse_report()
874
+ if product_ids is None:
875
+ ids = [r[0] for r in self.cur.execute(
876
+ 'SELECT id FROM product_product WHERE coalesce(active, false) ORDER BY id').fetchall()]
877
+ else:
878
+ ids = [int(i) for i in product_ids if i is not None]
879
+ out = {}
880
+ # Odoo's search_read on product.product skips archived ids, so they stay absent here too.
881
+ result = self.cur.execute(
882
+ "SELECT p.id, coalesce(f.qty, 0) FROM product_product p LEFT JOIN ("
883
+ "SELECT q.product_id, sum(coalesce(q.quantity, 0)) qty FROM stock_quant q "
884
+ "JOIN stock_location l ON l.id = q.location_id "
885
+ "WHERE l.usage = 'internal' AND starts_with(l.parent_path, $1) "
886
+ "GROUP BY 1) f ON f.product_id = p.id "
887
+ "WHERE p.id IN (SELECT unnest($2::BIGINT[])) AND coalesce(p.active, false)",
888
+ [view[1], ids])
889
+ while True:
890
+ batch = result.fetchmany(BATCH)
891
+ if not batch:
892
+ break
893
+ out.update({pid: float(qty) for pid, qty in batch})
894
+ wh = {'id': view[2], 'name': view[3], 'code': warehouse.FBA_WAREHOUSE_CODE}
895
+ return out, warehouse.fba_report(wh, len(ids), len(out), 1 if ids else 0,
896
+ 'the FBA view location in the Odoo mirror, never a pinned id')
897
+
898
+ def close(self):
899
+ super().close()
900
+ self._relations.clear()
901
+ if self.parent is None and self.pending:
902
+ _LOG.warning('canonical_source_pending: the Odoo mirror has not synced %s yet, so the '
903
+ 'product pool reads those cells blank until the next sync pass',
904
+ ', '.join(self.pending))
905
+ if self._factory is not None and self._cur is not None:
906
+ self._cur.close()
907
+ self._cur = None
908
+
909
+
910
  def active():
911
  return _ACTIVE.get()
912
 
913
 
914
  @contextmanager
915
+ def _activate(source):
 
916
  token = _ACTIVE.set(source)
917
  try:
918
  yield source
 
923
  source.close()
924
 
925
 
926
+ @contextmanager
927
+ def bounded(cur, kind, identities, store=None):
928
+ parent = active()
929
+ if isinstance(parent, MirrorSource):
930
+ source = MirrorSource(cur, kind, identities, store)
931
+ source.live_reads, source.pending, source.dropped = parent.live_reads, parent.pending, parent.dropped
932
+ source.parent = parent
933
+ else:
934
+ source = Source(cur, kind, identities, store)
935
+ with _activate(source) as entered:
936
+ yield entered
937
+
938
+
939
+ @contextmanager
940
+ def mirrored(cur, store=None):
941
+ """Serve every proxied read in this block from the Odoo mirror; never reads Odoo live.
942
+
943
+ `cur` is a mirror cursor, or a zero-argument factory opened on the first read and closed on exit.
944
+ """
945
+ if not callable(cur) or hasattr(cur, 'execute'):
946
+ missing = mirror_missing(cur)
947
+ if missing:
948
+ raise RuntimeError(_unready(missing))
949
+ with _activate(MirrorSource(cur, 'all', [], store)) as entered:
950
+ yield entered
951
+
952
+
953
  class _Odoo:
954
  def __getattr__(self, name):
955
  source = active()
 
957
  return getattr(source, name)
958
  if source is not None and name == 'get_odoo':
959
  return lambda: self
960
+ if isinstance(source, MirrorSource) and name in {'excluded_partner_ids', 'sale_line_domain'}:
961
+ return getattr(source, name)
962
  return getattr(_odoo, name)
963
 
964
 
platform/harness/datastore.py CHANGED
@@ -311,7 +311,12 @@ ENTITIES = {
311
  # carry a real barcode and 1,997 carry none. It is a `store=True` char column on
312
  # `product.product`, so the mirror can hold it.
313
  "fields": ["default_code", "name", "categ_id", "type", "standard_price", "active",
314
- "barcode", "uom_id", "write_date"],
 
 
 
 
 
315
  # ⛔ WITHOUT THIS THE COLUMN ALTERS IN AND STAYS NULL FOREVER — the same trap
316
  # `account_move.invoice_origin` and the `res_partner` address block are both commented
317
  # for. WE moved the schema; no product's `write_date` moved, so an incremental
@@ -327,7 +332,7 @@ ENTITIES = {
327
  # DOES NOT COME FROM HERE: it comes live from `modules/products.py::catalogue()`. This
328
  # column exists so the mirror is not permanently blind to a field the source has, which is
329
  # D-165's shape; widening `read_products` is a separate ticket with its own fence.
330
- "backfill_fields": ["barcode", "uom_id"],
331
  },
332
  "account_move": {
333
  "model": "account.move", "archivable": False,
@@ -620,8 +625,10 @@ def check_odoo_snapshot(rt, candidate, published, row_counts):
620
  return {row[0] for row in con.execute(
621
  "SELECT column_name FROM information_schema.columns WHERE table_name=?", [table]).fetchall()}
622
 
 
623
  previous_counts = {name: published.execute(f'SELECT count(*) FROM "{name}"').fetchone()[0]
624
- for name in ENTITIES if columns(published, name)}
 
625
  record("row_count_drop", [
626
  {"table": name, "previous": count, "candidate": row_counts.get(name, 0),
627
  "removed": count - row_counts.get(name, 0),
@@ -1930,6 +1937,8 @@ def _flatten(rec, fields, m2m=(), *, named_m2m=None, analytic_names=None, named_
1930
  for account_id in key.split(",")]}
1931
  for key, share in v.items()
1932
  ], ensure_ascii=False)
 
 
1933
  elif isinstance(v, (list, tuple)) and len(v) == 2 and isinstance(v[0], int):
1934
  out[f"{f.removesuffix('_id')}_id"] = v[0]
1935
  out[f"{f.removesuffix('_id')}_name"] = str(v[1] or "")
@@ -1994,6 +2003,8 @@ NUMERIC_FIELDS = ("amount_untaxed", "amount_untaxed_signed", "amount_residual_si
1994
 
1995
  # Odoo also names this many2one without the usual `_id` suffix.
1996
  MANY2ONE_FIELDS = frozenset({"product_uom", "create_uid"})
 
 
1997
 
1998
  # S72-T43 / R107: the audited Odoo fields the Data grids show. Each one is declared AND
1999
  # backfilled, because existing rows never re-sync when the schema grows. The list must equal
@@ -2117,6 +2128,38 @@ ENTITIES.update({
2117
  },
2118
  })
2119
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2120
  _connector_spec.register("odoo", _connector_spec.tables_for("odoo"),
2121
  mirror=_connector_spec.ConnectorMirrorSpec(tuple(ENTITIES)))
2122
 
@@ -2347,7 +2390,7 @@ def sync_entity(key, max_batches=200, batch=BATCH, log=print):
2347
  return _sync_entity(key, max_batches=max_batches, batch=batch, log=log)
2348
  candidate = getattr(_SYNC_TLS, "candidate", None)
2349
  if candidate is not None:
2350
- phase, pulled = _sync_entity(key, max_batches=max_batches, batch=batch, log=log)
2351
  # A recurring refresh may report changed pages to a bespoke cursor, but a
2352
  # tenant pass cannot claim completion until those private pages finish too.
2353
  pending = candidate["con"].execute(
@@ -2358,7 +2401,7 @@ def sync_entity(key, max_batches=200, batch=BATCH, log=print):
2358
  return phase, pulled
2359
  rt = _resident_odoo_runtime()
2360
  if rt is None:
2361
- return _sync_entity(key, max_batches=max_batches, batch=batch, log=log)
2362
  from harness import mirror_store
2363
  report = {}
2364
  with mirror_store.sync_mirror("odoo", rt.key, rt, report=report, log=log, expected_tables=[key]):
@@ -2502,6 +2545,98 @@ def _sync_entity(key, max_batches=200, batch=BATCH, log=print):
2502
  con.close()
2503
 
2504
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2505
  def _already(con, key, rec):
2506
  return bool(con.execute(f"SELECT 1 FROM {key} WHERE id=?", [rec["id"]]).fetchone())
2507
 
 
311
  # carry a real barcode and 1,997 carry none. It is a `store=True` char column on
312
  # `product.product`, so the mirror can hold it.
313
  "fields": ["default_code", "name", "categ_id", "type", "standard_price", "active",
314
+ "barcode", "uom_id", "create_date", "product_tmpl_id", "volume",
315
+ "display_name", "product_tag_ids", "incoming_qty", "free_qty",
316
+ "virtual_available", "write_date"],
317
+ # Computed or template-related in Odoo 16: they change with no product.product write_date.
318
+ "refresh_fields": ["display_name", "product_tag_ids", "incoming_qty", "free_qty",
319
+ "virtual_available"],
320
  # ⛔ WITHOUT THIS THE COLUMN ALTERS IN AND STAYS NULL FOREVER — the same trap
321
  # `account_move.invoice_origin` and the `res_partner` address block are both commented
322
  # for. WE moved the schema; no product's `write_date` moved, so an incremental
 
332
  # DOES NOT COME FROM HERE: it comes live from `modules/products.py::catalogue()`. This
333
  # column exists so the mirror is not permanently blind to a field the source has, which is
334
  # D-165's shape; widening `read_products` is a separate ticket with its own fence.
335
+ "backfill_fields": ["barcode", "uom_id", "create_date", "product_tmpl_id", "volume"],
336
  },
337
  "account_move": {
338
  "model": "account.move", "archivable": False,
 
625
  return {row[0] for row in con.execute(
626
  "SELECT column_name FROM information_schema.columns WHERE table_name=?", [table]).fetchall()}
627
 
628
+ # A whole-table replace is a complete count-checked read of Odoo, so its drop is real, not a lost copy.
629
  previous_counts = {name: published.execute(f'SELECT count(*) FROM "{name}"').fetchone()[0]
630
+ for name in ENTITIES if columns(published, name)
631
+ and (name not in REPLACE_EACH_PASS or REPLACE_EACH_PASS[name].get("scope") == "internal")}
632
  record("row_count_drop", [
633
  {"table": name, "previous": count, "candidate": row_counts.get(name, 0),
634
  "removed": count - row_counts.get(name, 0),
 
1937
  for account_id in key.split(",")]}
1938
  for key, share in v.items()
1939
  ], ensure_ascii=False)
1940
+ elif f in ID_LIST_FIELDS: # a 2-id list would otherwise read as an m2o pair
1941
+ out[f] = json.dumps(list(v or []))
1942
  elif isinstance(v, (list, tuple)) and len(v) == 2 and isinstance(v[0], int):
1943
  out[f"{f.removesuffix('_id')}_id"] = v[0]
1944
  out[f"{f.removesuffix('_id')}_name"] = str(v[1] or "")
 
2003
 
2004
  # Odoo also names this many2one without the usual `_id` suffix.
2005
  MANY2ONE_FIELDS = frozenset({"product_uom", "create_uid"})
2006
+ # Plain x2many fields stored whole as a JSON id list, `[]` for none.
2007
+ ID_LIST_FIELDS = frozenset({"product_tag_ids"})
2008
 
2009
  # S72-T43 / R107: the audited Odoo fields the Data grids show. Each one is declared AND
2010
  # backfilled, because existing rows never re-sync when the schema grows. The list must equal
 
2128
  },
2129
  })
2130
 
2131
+ # The product pool's pricing and tags. Each table holds exactly Odoo's default (active_test) read,
2132
+ # so it carries no `active` column and a reader needs no active filter.
2133
+ ENTITIES.update({
2134
+ "product_pricelist": {
2135
+ "model": "product.pricelist", "archivable": False, "optional": True,
2136
+ "fields": ["name"],
2137
+ },
2138
+ "product_pricelist_item": {
2139
+ "model": "product.pricelist.item", "archivable": False, "optional": True,
2140
+ "fields": ["pricelist_id", "product_id", "product_tmpl_id", "applied_on", "compute_price",
2141
+ "fixed_price", "min_quantity", "date_start", "date_end", "packaging_id",
2142
+ "packaging_price"],
2143
+ },
2144
+ "product_tag": {
2145
+ "model": "product.tag", "archivable": False, "optional": True,
2146
+ "fields": ["name"],
2147
+ },
2148
+ })
2149
+ NUMERIC_FIELDS += ("volume", "incoming_qty", "free_qty", "virtual_available", "fixed_price",
2150
+ "min_quantity")
2151
+
2152
+ #: Entities read in full every pass and swapped into the mirror in one transaction. A cursor sync
2153
+ #: cannot see an Odoo delete, or a quant quantity that moved without a write_date change.
2154
+ #: Scope `internal` replaces the internal-location quants only. Any other scope is the whole table.
2155
+ REPLACE_EACH_PASS = {
2156
+ "stock_quant": {"domain": [("location_id.usage", "=", "internal")], "scope": "internal"},
2157
+ "product_pricelist": {"domain": []},
2158
+ "product_pricelist_item": {"domain": []},
2159
+ "product_tag": {"domain": []},
2160
+ }
2161
+ REPLACE_PAGE = 5000
2162
+
2163
  _connector_spec.register("odoo", _connector_spec.tables_for("odoo"),
2164
  mirror=_connector_spec.ConnectorMirrorSpec(tuple(ENTITIES)))
2165
 
 
2390
  return _sync_entity(key, max_batches=max_batches, batch=batch, log=log)
2391
  candidate = getattr(_SYNC_TLS, "candidate", None)
2392
  if candidate is not None:
2393
+ phase, pulled = _sync_one(key, max_batches=max_batches, batch=batch, log=log)
2394
  # A recurring refresh may report changed pages to a bespoke cursor, but a
2395
  # tenant pass cannot claim completion until those private pages finish too.
2396
  pending = candidate["con"].execute(
 
2401
  return phase, pulled
2402
  rt = _resident_odoo_runtime()
2403
  if rt is None:
2404
+ return _sync_one(key, max_batches=max_batches, batch=batch, log=log)
2405
  from harness import mirror_store
2406
  report = {}
2407
  with mirror_store.sync_mirror("odoo", rt.key, rt, report=report, log=log, expected_tables=[key]):
 
2545
  con.close()
2546
 
2547
 
2548
+ def _sync_one(key, max_batches=200, batch=BATCH, log=print):
2549
+ """One entity's pass: the cursor sync, the full replace, or the cursor sync then the replace."""
2550
+ plan = REPLACE_EACH_PASS.get(key)
2551
+ if plan is None or source_paused():
2552
+ return _sync_entity(key, max_batches=max_batches, batch=batch, log=log)
2553
+ if plan.get("scope") != "internal":
2554
+ return _replace_entity(key, log=log)
2555
+ phase, pulled = _sync_entity(key, max_batches=max_batches, batch=batch, log=log)
2556
+ return phase, pulled + _replace_entity(key, log=log)[1]
2557
+
2558
+
2559
+ def _replace_scope(con, key, plan):
2560
+ """SQL predicate for the mirror rows a replace owns. `_replace_stage` holds the fresh read."""
2561
+ if plan.get("scope") != "internal":
2562
+ return "TRUE"
2563
+ terms = ["id IN (SELECT id FROM _replace_stage)",
2564
+ "location_id IN (SELECT location_id FROM _replace_stage WHERE location_id IS NOT NULL)"]
2565
+ if {"id", "usage"} <= set(columns_of("stock_location", cur=con) or ()):
2566
+ terms.append("location_id IN (SELECT id FROM stock_location WHERE usage = 'internal')")
2567
+ return "(" + " OR ".join(terms) + ")"
2568
+
2569
+
2570
+ def _replace_entity(key, log=print):
2571
+ """Read every row the plan's domain matches, then swap the mirror's scope for it in one transaction.
2572
+
2573
+ Returns (phase, changed_rows). A failed or short read raises before any delete.
2574
+ """
2575
+ if source_paused():
2576
+ _paused_notice(f"full read of {key}", log)
2577
+ return PAUSED_PHASE, 0
2578
+ spec, plan = ENTITIES[key], REPLACE_EACH_PASS[key]
2579
+ domain = list(plan["domain"])
2580
+ fields = spec["fields"] + list(spec.get("m2m") or [])
2581
+ expected = int(O.get_odoo().search_count(spec["model"], domain))
2582
+ rows, last = [], 0
2583
+ while True:
2584
+ recs = O.search_read(spec["model"], domain + [("id", ">", last)], ["id"] + fields,
2585
+ limit=REPLACE_PAGE, order="id asc")
2586
+ if any(field not in rec for rec in recs for field in fields):
2587
+ raise RuntimeError(f"{key}: full read omitted a requested field. The mirror keeps its previous rows.")
2588
+ rows += _flatten_records(recs, spec["fields"], spec.get("m2m") or (),
2589
+ named_m2m=spec.get("named_m2m"))
2590
+ # Stop on an EMPTY page: a server-side page cap returns short pages before the end.
2591
+ if not recs:
2592
+ break
2593
+ last = recs[-1]["id"]
2594
+ # Rows moved during the read when the read matches a second count. Refuse only a read
2595
+ # that disagrees with both counts, before any delete.
2596
+ if len(rows) != expected:
2597
+ after = int(O.get_odoo().search_count(spec["model"], domain))
2598
+ if len(rows) != after:
2599
+ raise RuntimeError(f"{key}: full read returned {len(rows)} rows, counts were "
2600
+ f"{expected} and {after}. The mirror keeps its previous rows.")
2601
+ con = connect()
2602
+ try:
2603
+ _ensure_table(con, key, spec)
2604
+ _ensure_columns(con, key, spec)
2605
+ colnames = [c.split()[0] for c in _cols(spec)]
2606
+ cols = ", ".join(colnames)
2607
+ con.execute(f"CREATE OR REPLACE TEMP TABLE _replace_stage AS SELECT {cols} FROM {key} LIMIT 0")
2608
+ if rows:
2609
+ con.executemany(f"INSERT INTO _replace_stage ({cols}) VALUES ({', '.join('?' for _ in colnames)})",
2610
+ [[r.get(c) for c in colnames] for r in rows])
2611
+ scope = _replace_scope(con, key, plan)
2612
+ changed = con.execute(
2613
+ f"SELECT count(DISTINCT id) FROM ((SELECT {cols} FROM _replace_stage "
2614
+ f"EXCEPT SELECT {cols} FROM {key} WHERE {scope}) UNION ALL "
2615
+ f"(SELECT {cols} FROM {key} WHERE {scope} EXCEPT SELECT {cols} FROM _replace_stage))"
2616
+ ).fetchone()[0]
2617
+ if changed:
2618
+ con.execute("BEGIN TRANSACTION")
2619
+ try:
2620
+ con.execute(f"DELETE FROM {key} WHERE {scope}")
2621
+ con.execute(f"INSERT INTO {key} ({cols}) SELECT {cols} FROM _replace_stage")
2622
+ except BaseException:
2623
+ con.execute("ROLLBACK")
2624
+ raise
2625
+ con.execute("COMMIT")
2626
+ if plan.get("scope") == "internal":
2627
+ n = con.execute(f"SELECT count(*) FROM {key}").fetchone()[0]
2628
+ con.execute("UPDATE _sync_state SET rows=? WHERE entity=?", [n, key])
2629
+ else:
2630
+ _save_state(con, key, "live", max((r["id"] for r in rows), default=0), None)
2631
+ log(f" {key}: full read {len(rows):,} rows, {changed:,} changed")
2632
+ return "live", changed
2633
+ finally:
2634
+ try:
2635
+ con.execute("DROP TABLE IF EXISTS _replace_stage")
2636
+ finally:
2637
+ con.close()
2638
+
2639
+
2640
  def _already(con, key, rec):
2641
  return bool(con.execute(f"SELECT 1 FROM {key} WHERE id=?", [rec["id"]]).fetchone())
2642
 
platform/modules/inventory.py CHANGED
@@ -308,6 +308,47 @@ def sku_inventory(target_days=180, carry_pct=_CARRY_PCT, t=None):
308
  return out
309
 
310
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
311
  def finance_workflow_contract():
312
  """Describe inventory work requirements without creating a second fact plane."""
313
  return [
 
308
  return out
309
 
310
 
311
+ def validate_mirror(cur, t=None, codes=None, sample=40):
312
+ """Reconcile the mirror-built `sku_inventory` to a live bounded build; this oracle reads live Odoo.
313
+
314
+ Returns `{'checked', 'mismatches': [{sku, column, live, mirror}], 'columns'}` at tolerance 0.01.
315
+ The mirror side reads on_hand from the mirror's internal quants, so a mismatch also names quant drift.
316
+ """
317
+ from harness.canonical_source import MirrorSource, active, bounded, mirrored
318
+ if isinstance(active(), MirrorSource):
319
+ raise RuntimeError('inventory.validate_mirror needs a live oracle; call it outside mirrored()')
320
+ t = t or P.today()
321
+ if codes is None:
322
+ with mirrored(cur):
323
+ whole = sku_inventory(t=t)
324
+ half = max(1, sample // 2)
325
+ by_value = sorted(whole, key=lambda c: (-whole[c]['inv_value'], c))[:half]
326
+ by_units = [c for c in sorted(whole, key=lambda c: (-whole[c]['qty_ltm'], c))
327
+ if c not in by_value][:sample - len(by_value)]
328
+ codes = by_value + by_units
329
+ codes = [str(c) for c in codes]
330
+ with bounded(cur, 'product', codes):
331
+ live = sku_inventory(t=t)
332
+ with mirrored(cur):
333
+ with bounded(cur, 'product', codes):
334
+ mirror = sku_inventory(t=t)
335
+ columns = sorted({k for side in (live, mirror) for row in side.values() for k in row})
336
+ mismatches = []
337
+ for sku in codes:
338
+ a, b = live.get(sku), mirror.get(sku)
339
+ if a is None or b is None:
340
+ if a is not b:
341
+ mismatches.append({'sku': sku, 'column': '(row)', 'live': a is not None,
342
+ 'mirror': b is not None})
343
+ continue
344
+ for col in columns:
345
+ x, y = a.get(col), b.get(col)
346
+ numeric = all(isinstance(v, (int, float)) and not isinstance(v, bool) for v in (x, y))
347
+ if (abs(float(x) - float(y)) > 0.01) if numeric else (x != y):
348
+ mismatches.append({'sku': sku, 'column': col, 'live': x, 'mirror': y})
349
+ return {'checked': len(codes), 'mismatches': mismatches, 'columns': columns}
350
+
351
+
352
  def finance_workflow_contract():
353
  """Describe inventory work requirements without creating a second fact plane."""
354
  return [
platform/modules/products.py CHANGED
@@ -333,6 +333,12 @@ def inventory_value_per_quantity(inv_value, quantity):
333
  return inventory_value_per_quantity_result(inv_value, quantity)[0]
334
 
335
 
 
 
 
 
 
 
336
  def catalogue():
337
  """`{sku_code: {'product', 'category', 'id', 'created_at', 'upc', 'discontinued',
338
  'incoming', 'available', 'forecasted', 'product_tmpl_id', 'volume'}}` — EVERY ACTIVE product, sold or not.
@@ -405,7 +411,9 @@ def catalogue():
405
  f"a search_count of {n}. A short catalogue renders as a plausible smaller grid with "
406
  f"nothing reporting it; raise the limit before shipping this.")
407
  code_cat = _code_category()
408
- disc_tag = discontinued_tag_id()
 
 
409
  out = {}
410
  for p in prods:
411
  code = (str(p['default_code']).strip() if p.get('default_code') else f"pid:{p['id']}")
@@ -437,19 +445,19 @@ def catalogue():
437
  'volume': (float(p['volume']) if isinstance(p.get('volume'),
438
  (int, float)) and not isinstance(p['volume'], bool)
439
  else None),
440
- 'discontinued': 'No', 'incoming': 0.0,
441
- 'available': 0.0, 'forecasted': 0.0})
442
  # ⛔ ACROSS EVERY RECORD SHARING THE CODE, not just the first one that won above. A SKU
443
  # whose variants are separate records carries its inbound on whichever record the PO named,
444
  # and 'first record wins' would drop the rest — a buy list that under-counts what is
445
  # already on the water tells you to re-order stock you have already bought.
446
- row['incoming'] += float(p.get('incoming_qty') or 0.0)
447
  # S72-T40 (R104): Available is Odoo's `free_qty` (on hand minus reserved) and Forecasted is
448
  # `virtual_available` (on hand plus incoming minus outgoing). Both are unstored computed
449
  # fields Odoo refuses to aggregate, so they ride this read and accumulate per code like
450
  # `incoming`. `validate_available_forecasted` ties the totals to stock.quant and stock.move.
451
- row['available'] += float(p.get('free_qty') or 0.0)
452
- row['forecasted'] += float(p.get('virtual_available') or 0.0)
453
  # ⭐ W40-T08 — the EARLIEST create_date across the records sharing this code, not the
454
  # first record's. A row here is a SKU, and the honest answer to "when was this record
455
  # created" for a merged SKU is when the earliest of its records appeared. Only `2112-12`
@@ -503,7 +511,9 @@ def discontinued_tag_id():
503
  RED on the difference instead of shipping a column that quietly says nobody is discontinued.
504
  """
505
  rows = O.search_read('product.tag', [('name', '=ilike', DISCONTINUED_TAG)], ['id'], limit=2)
506
- return rows[0]['id'] if rows else None
 
 
507
 
508
 
509
  def incoming_from_po(prods=None):
@@ -657,7 +667,12 @@ def active_products(limit=50000, extra_fields=()):
657
  on this connection, on a path that is somebody's first page load.
658
  """
659
  dom = [('active', '=', True)]
660
- fields = ['id', 'default_code', 'product_tmpl_id'] + [str(f) for f in extra_fields if str(f) not in {'id', 'default_code', 'product_tmpl_id'}]
 
 
 
 
 
661
  prods = O.search_read('product.product', dom, fields,
662
  limit=limit)
663
  n = O.get_odoo().search_count('product.product', dom)
 
333
  return inventory_value_per_quantity_result(inv_value, quantity)[0]
334
 
335
 
336
+ def _add_qty(row, key, value):
337
+ """Add a record's quantity to its SKU row. None (a mirror column not yet synced) stays blank, never 0."""
338
+ if value is not None and value is not False:
339
+ row[key] = (row[key] or 0.0) + float(value)
340
+
341
+
342
  def catalogue():
343
  """`{sku_code: {'product', 'category', 'id', 'created_at', 'upc', 'discontinued',
344
  'incoming', 'available', 'forecasted', 'product_tmpl_id', 'volume'}}` — EVERY ACTIVE product, sold or not.
 
411
  f"a search_count of {n}. A short catalogue renders as a plausible smaller grid with "
412
  f"nothing reporting it; raise the limit before shipping this.")
413
  code_cat = _code_category()
414
+ from harness.canonical_source import active
415
+ # A source-bound read must not fill the process cache: an unsynced tag table would pin "No".
416
+ disc_tag = discontinued_tag_id.__wrapped__() if active() is not None else discontinued_tag_id()
417
  out = {}
418
  for p in prods:
419
  code = (str(p['default_code']).strip() if p.get('default_code') else f"pid:{p['id']}")
 
445
  'volume': (float(p['volume']) if isinstance(p.get('volume'),
446
  (int, float)) and not isinstance(p['volume'], bool)
447
  else None),
448
+ 'discontinued': 'No', 'incoming': None,
449
+ 'available': None, 'forecasted': None})
450
  # ⛔ ACROSS EVERY RECORD SHARING THE CODE, not just the first one that won above. A SKU
451
  # whose variants are separate records carries its inbound on whichever record the PO named,
452
  # and 'first record wins' would drop the rest — a buy list that under-counts what is
453
  # already on the water tells you to re-order stock you have already bought.
454
+ _add_qty(row, 'incoming', p.get('incoming_qty'))
455
  # S72-T40 (R104): Available is Odoo's `free_qty` (on hand minus reserved) and Forecasted is
456
  # `virtual_available` (on hand plus incoming minus outgoing). Both are unstored computed
457
  # fields Odoo refuses to aggregate, so they ride this read and accumulate per code like
458
  # `incoming`. `validate_available_forecasted` ties the totals to stock.quant and stock.move.
459
+ _add_qty(row, 'available', p.get('free_qty'))
460
+ _add_qty(row, 'forecasted', p.get('virtual_available'))
461
  # ⭐ W40-T08 — the EARLIEST create_date across the records sharing this code, not the
462
  # first record's. A row here is a SKU, and the honest answer to "when was this record
463
  # created" for a merged SKU is when the earliest of its records appeared. Only `2112-12`
 
511
  RED on the difference instead of shipping a column that quietly says nobody is discontinued.
512
  """
513
  rows = O.search_read('product.tag', [('name', '=ilike', DISCONTINUED_TAG)], ['id'], limit=2)
514
+ # A source-bound read returns iterable `Rows`, not a list.
515
+ first = next(iter(rows), None)
516
+ return first['id'] if first else None
517
 
518
 
519
  def incoming_from_po(prods=None):
 
667
  on this connection, on a path that is somebody's first page load.
668
  """
669
  dom = [('active', '=', True)]
670
+ extra = [str(f) for f in extra_fields if str(f) not in {'id', 'default_code', 'product_tmpl_id'}]
671
+ from harness.canonical_source import active, MirrorSource
672
+ if extra and isinstance(active(), MirrorSource):
673
+ # Standing rule 1: a field the mirror cannot serve is dropped and logged, never read live.
674
+ extra = active().servable('product.product', extra)
675
+ fields = ['id', 'default_code', 'product_tmpl_id'] + extra
676
  prods = O.search_read('product.product', dom, fields,
677
  limit=limit)
678
  n = O.get_odoo().search_count('product.product', dom)
platform/modules/warehouse.py CHANGED
@@ -774,13 +774,14 @@ def fba_qty_by_product_id(product_ids=None, wh=None):
774
  ⚠ `(None, report)` — not `({}, report)` — when the tenant has no FBA warehouse. An empty map
775
  would read as "every SKU holds zero", and the caller must be able to tell the two apart.
776
  """
 
 
 
 
 
777
  wh = wh if wh is not None else fba_warehouse()
778
  if not wh:
779
- return None, {'available': False,
780
- 'cause': "no stock.warehouse carries the code " + FBA_WAREHOUSE_CODE,
781
- 'recommended_fix': "give the Amazon warehouse the short code "
782
- + FBA_WAREHOUSE_CODE + " in Odoo, or leave the column "
783
- "blank if this tenant sells nothing through FBA"}
784
  o = O.get_odoo()
785
  ctx = {'warehouse': wh['id']}
786
  if product_ids is None:
@@ -794,12 +795,27 @@ def fba_qty_by_product_id(product_ids=None, wh=None):
794
  for r in o.execute_kw('product.product', 'search_read', [[('id', 'in', chunk)]],
795
  {'fields': ['id', 'qty_available'], 'context': ctx}):
796
  out[r['id']] = r.get('qty_available') or 0.0
797
- return out, {'available': True, 'warehouse_id': wh['id'],
798
- 'warehouse_code': wh.get('code'), 'warehouse_name': wh.get('name'),
799
- 'resolved_by': 'stock.warehouse.code at runtime, never a pinned id',
800
- 'products_requested': len(ids), 'products_read': len(out),
801
- 'batches': batches, 'batch_size': _FBA_BATCH,
802
- 'cap': 'none: every requested product id is read, 5000 per round trip'}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
803
 
804
 
805
  def program_health(adj):
 
774
  ⚠ `(None, report)` — not `({}, report)` — when the tenant has no FBA warehouse. An empty map
775
  would read as "every SKU holds zero", and the caller must be able to tell the two apart.
776
  """
777
+ from harness.canonical_source import active
778
+ source = active()
779
+ # Inside `canonical_source.mirrored` the FBA quants come from the mirror, never live Odoo.
780
+ if wh is None and hasattr(source, 'fba_qty'):
781
+ return source.fba_qty(product_ids)
782
  wh = wh if wh is not None else fba_warehouse()
783
  if not wh:
784
+ return None, no_fba_warehouse_report()
 
 
 
 
785
  o = O.get_odoo()
786
  ctx = {'warehouse': wh['id']}
787
  if product_ids is None:
 
795
  for r in o.execute_kw('product.product', 'search_read', [[('id', 'in', chunk)]],
796
  {'fields': ['id', 'qty_available'], 'context': ctx}):
797
  out[r['id']] = r.get('qty_available') or 0.0
798
+ return out, fba_report(wh, len(ids), len(out), batches,
799
+ 'stock.warehouse.code at runtime, never a pinned id')
800
+
801
+
802
+ def no_fba_warehouse_report():
803
+ """The `(None, report)` half `fba_qty_by_product_id` returns when no warehouse has the code."""
804
+ return {'available': False,
805
+ 'cause': "no stock.warehouse carries the code " + FBA_WAREHOUSE_CODE,
806
+ 'recommended_fix': "give the Amazon warehouse the short code "
807
+ + FBA_WAREHOUSE_CODE + " in Odoo, or leave the column "
808
+ "blank if this tenant sells nothing through FBA"}
809
+
810
+
811
+ def fba_report(wh, requested, read, batches, resolved_by):
812
+ """The report `fba_qty_by_product_id` returns beside a read FBA map."""
813
+ return {'available': True, 'warehouse_id': wh['id'],
814
+ 'warehouse_code': wh.get('code'), 'warehouse_name': wh.get('name'),
815
+ 'resolved_by': resolved_by,
816
+ 'products_requested': requested, 'products_read': read,
817
+ 'batches': batches, 'batch_size': _FBA_BATCH,
818
+ 'cap': 'none: every requested product id is read, 5000 per round trip'}
819
 
820
 
821
  def program_health(adj):
web/public/AIOS_DEPLOY_RECEIPT CHANGED
@@ -1 +1 @@
1
- a7a3bbeebf18bb7116b669ba2c9dddd8b93014e7ecae45679ca8eb23bbc10f8d
 
1
+ 8b414243790f40291a06720082e07f23adf9fb657d1b3bdbee733c65fa3bc155
web/src/customer-grid/CustomerGrid.tsx CHANGED
@@ -288,6 +288,10 @@ import { windowLabel } from "./windows";
288
  */
289
  export const GRID_CHAT_OPEN_EVENT = "aios:grid-chat-open";
290
 
 
 
 
 
291
  const ROW_PX: Record<RowHeightMode, number> = { short: 28, medium: 34, tall: 48 };
292
 
293
  /**
@@ -12097,7 +12101,7 @@ function CustomerGridSurface({
12097
  scroll to a row nobody can read. `SUMMARY_BAR_PX` is the component's own
12098
  constant and `--cg-sum-h` in `styles/wave50/grid.css` is its twin. */
12099
  height={Math.max(1, gridSize.height - (summaryShown ? SUMMARY_BAR_PX : 0))}
12100
- customRenderers={[ratingCellRenderer, userCellRenderer, imageCellRenderer]}
12101
  headerIcons={HEADER_ICONS}
12102
  rightElement={!canAddField ? undefined : (
12103
  // Wave-6 item 4 — the "+" of the header row: the create-field form,
 
288
  */
289
  export const GRID_CHAT_OPEN_EVENT = "aios:grid-chat-open";
290
 
291
+ // One identity for the life of the module: a new array remounts glide's open cell editor, which
292
+ // selects the text typed so far, and the next keystroke replaces it.
293
+ const CUSTOM_RENDERERS = [ratingCellRenderer, userCellRenderer, imageCellRenderer];
294
+
295
  const ROW_PX: Record<RowHeightMode, number> = { short: 28, medium: 34, tall: 48 };
296
 
297
  /**
 
12101
  scroll to a row nobody can read. `SUMMARY_BAR_PX` is the component's own
12102
  constant and `--cg-sum-h` in `styles/wave50/grid.css` is its twin. */
12103
  height={Math.max(1, gridSize.height - (summaryShown ? SUMMARY_BAR_PX : 0))}
12104
+ customRenderers={CUSTOM_RENDERERS}
12105
  headerIcons={HEADER_ICONS}
12106
  rightElement={!canAddField ? undefined : (
12107
  // Wave-6 item 4 — the "+" of the header row: the create-field form,
web/src/customer-grid/apiBridge.ts CHANGED
@@ -222,6 +222,8 @@ const rowsCache = new Map<string, { at: number; payload: CustomersPayload }>();
222
  const rowWrites = new Map<string, Promise<boolean>>();
223
  const rowVersions = new Map<string, number>();
224
  let rowsSession = 0;
 
 
225
 
226
  /**
227
  * ⭐⭐ WAVE 31 · T21 (owner item 7: *"It still takes a very long time to load from one Database
@@ -261,9 +263,15 @@ export function clearWorkspaceCache(): void {
261
  }
262
 
263
  export function clearCustomersCache(): void {
 
 
 
 
 
 
 
264
  rowsCache.clear();
265
  rowVersions.clear();
266
- rowWrites.clear();
267
  rowsSession += 1;
268
  // ⛔ THE ENVELOPE IS SESSION-SCOPED FOR THE SAME REASON THE ROWS ARE, and it carries MORE that
269
  // is per-account than they do: `viewer`, `userOptions`, the user's own views and folders. One
@@ -1810,7 +1818,7 @@ export async function patchTopicRow(
1810
  onAccepted?: (accepted: Partial<Row>, cleared: string[]) => void,
1811
  onFailure?: (failure: CellWriteFailure) => void,
1812
  ): Promise<boolean> {
1813
- const session = rowsSession;
1814
  const rowKey = `${topic.rowsPath}/${pid}`;
1815
  const task = (rowWrites.get(rowKey) ?? Promise.resolve(true)).then(async () => {
1816
  const fail = (body: unknown, status: number, fallback: string): false => {
@@ -1824,10 +1832,10 @@ export async function patchTopicRow(
1824
  else signal(TOAST_EVENT, failure.message);
1825
  return false;
1826
  };
1827
- if (session !== rowsSession) return false;
1828
  try {
1829
  const tables = topic.scope.startsWith("ut_") ? await connectedTables() : new Set<string>();
1830
- if (session !== rowsSession) return false;
1831
  if (!tables) return fail(null, 0, "This database's write capabilities could not be loaded. Try again.");
1832
  const connected = tables.has(topic.key);
1833
  const id = eventId("cell");
@@ -1836,9 +1844,9 @@ export async function patchTopicRow(
1836
  body: JSON.stringify(connected
1837
  ? { scopeKey: topic.scope, events: [{ id, type: "overlay_patch", pid, updates }] } : updates),
1838
  });
1839
- if (handledSession(res) || session !== rowsSession) return false;
1840
  const envelope = await readJson(res);
1841
- if (session !== rowsSession) return false;
1842
  if (!res.ok) return fail(envelope, res.status, "This edit could not be saved.");
1843
  type Answer = { id?: string; ok?: boolean; value?: unknown; updates?: unknown;
1844
  refused?: unknown; cleared?: unknown; routeSwaps?: unknown; reason?: string };
@@ -1875,7 +1883,7 @@ export async function patchTopicRow(
1875
  if (!valid) return fail(body, res.status, "The server did not confirm this edit. Reload before trying again.");
1876
  return true;
1877
  } catch {
1878
- return session === rowsSession ? fail(null, 0, "This edit did not reach the server. Try again.") : false;
1879
  }
1880
  });
1881
  rowWrites.set(rowKey, task);
 
222
  const rowWrites = new Map<string, Promise<boolean>>();
223
  const rowVersions = new Map<string, number>();
224
  let rowsSession = 0;
225
+ // Bumped only at an account boundary: a cache refresh must never cancel a cell write in flight.
226
+ let writeSession = 0;
227
 
228
  /**
229
  * ⭐⭐ WAVE 31 · T21 (owner item 7: *"It still takes a very long time to load from one Database
 
263
  }
264
 
265
  export function clearCustomersCache(): void {
266
+ rowWrites.clear();
267
+ writeSession += 1;
268
+ refreshCustomersCache();
269
+ }
270
+
271
+ /** Drop every cached read and keep the cell writes in flight: a stale workspace is not a sign-out. */
272
+ export function refreshCustomersCache(): void {
273
  rowsCache.clear();
274
  rowVersions.clear();
 
275
  rowsSession += 1;
276
  // ⛔ THE ENVELOPE IS SESSION-SCOPED FOR THE SAME REASON THE ROWS ARE, and it carries MORE that
277
  // is per-account than they do: `viewer`, `userOptions`, the user's own views and folders. One
 
1818
  onAccepted?: (accepted: Partial<Row>, cleared: string[]) => void,
1819
  onFailure?: (failure: CellWriteFailure) => void,
1820
  ): Promise<boolean> {
1821
+ const session = writeSession;
1822
  const rowKey = `${topic.rowsPath}/${pid}`;
1823
  const task = (rowWrites.get(rowKey) ?? Promise.resolve(true)).then(async () => {
1824
  const fail = (body: unknown, status: number, fallback: string): false => {
 
1832
  else signal(TOAST_EVENT, failure.message);
1833
  return false;
1834
  };
1835
+ if (session !== writeSession) return false;
1836
  try {
1837
  const tables = topic.scope.startsWith("ut_") ? await connectedTables() : new Set<string>();
1838
+ if (session !== writeSession) return false;
1839
  if (!tables) return fail(null, 0, "This database's write capabilities could not be loaded. Try again.");
1840
  const connected = tables.has(topic.key);
1841
  const id = eventId("cell");
 
1844
  body: JSON.stringify(connected
1845
  ? { scopeKey: topic.scope, events: [{ id, type: "overlay_patch", pid, updates }] } : updates),
1846
  });
1847
+ if (handledSession(res) || session !== writeSession) return false;
1848
  const envelope = await readJson(res);
1849
+ if (session !== writeSession) return false;
1850
  if (!res.ok) return fail(envelope, res.status, "This edit could not be saved.");
1851
  type Answer = { id?: string; ok?: boolean; value?: unknown; updates?: unknown;
1852
  refused?: unknown; cleared?: unknown; routeSwaps?: unknown; reason?: string };
 
1883
  if (!valid) return fail(body, res.status, "The server did not confirm this edit. Reload before trying again.");
1884
  return true;
1885
  } catch {
1886
+ return session === writeSession ? fail(null, 0, "This edit did not reach the server. Try again.") : false;
1887
  }
1888
  });
1889
  rowWrites.set(rowKey, task);
web/src/shell/Shell.tsx CHANGED
@@ -87,7 +87,7 @@ const AutomationSurface = lazy(() => import("../automation/AutomationSurface"));
87
  // ⚠ IT JOINS `Lazily`, NOT A BARE `Suspense`: the shared wrapper carries the ErrorBoundary, so a
88
  // chunk that fails to load names the surface it was for instead of blanking the frame.
89
  const CustomerGrid = lazy(() => import("../customer-grid/CustomerGrid"));
90
- import { clearCustomersCache, currentSurfaceScope } from "../customer-grid/apiBridge";
91
  import { OverlayProvider } from "../customer-grid/OverlaySurface";
92
  import { appPathHash, dataPathHash, readDataPath } from "../customer-grid/dataPath";
93
  import type { DataDatabase } from "../customer-grid/dataPath";
@@ -2334,7 +2334,8 @@ function ShellFrame() {
2334
  // W63-T29: connector creation/enablement/removal changes both membership and the rows a
2335
  // newly addressable database will read. Drop the client cache before asking `/nav` again;
2336
  // otherwise a re-enabled key can paint the old absence even after its rail row arrives.
2337
- clearCustomersCache();
 
2338
  setDataError("");
2339
  // Invalidate immediately: an older response can resolve before the next effect cleans up.
2340
  navRequestEpoch.current += 1;
 
87
  // ⚠ IT JOINS `Lazily`, NOT A BARE `Suspense`: the shared wrapper carries the ErrorBoundary, so a
88
  // chunk that fails to load names the surface it was for instead of blanking the frame.
89
  const CustomerGrid = lazy(() => import("../customer-grid/CustomerGrid"));
90
+ import { clearCustomersCache, currentSurfaceScope, refreshCustomersCache } from "../customer-grid/apiBridge";
91
  import { OverlayProvider } from "../customer-grid/OverlaySurface";
92
  import { appPathHash, dataPathHash, readDataPath } from "../customer-grid/dataPath";
93
  import type { DataDatabase } from "../customer-grid/dataPath";
 
2334
  // W63-T29: connector creation/enablement/removal changes both membership and the rows a
2335
  // newly addressable database will read. Drop the client cache before asking `/nav` again;
2336
  // otherwise a re-enabled key can paint the old absence even after its rail row arrives.
2337
+ // A refresh, not a sign-out: clearing the write session here rolled back saves in flight.
2338
+ refreshCustomersCache();
2339
  setDataError("");
2340
  // Invalidate immediately: an older response can resolve before the next effect cleans up.
2341
  navRequestEpoch.current += 1;