fsanyoto commited on
Commit
665e5ea
Β·
verified Β·
1 Parent(s): dcdb685

Deploy AIOS web (React glide grid + FastAPI slice)

Browse files
RELEASES.json CHANGED
@@ -1,5 +1,5 @@
1
  {
2
- "current": "bac40c2",
3
  "releases": [
4
  {
5
  "version": "v24",
 
1
  {
2
+ "current": "6fffe9d",
3
  "releases": [
4
  {
5
  "version": "v24",
VERSION CHANGED
@@ -1 +1 @@
1
- bac40c2
 
1
+ 6fffe9d
api/routes_tables.py CHANGED
@@ -722,6 +722,18 @@ def import_rows(table_key: str, body: dict = Body(default=None),
722
  label = by_key[computed[0]].get("label") or computed[0]
723
  raise err(400, "computed_field",
724
  f"{label!r} is worked out from other columns, so it cannot be imported into")
 
 
 
 
 
 
 
 
 
 
 
 
725
  try:
726
  made = ut.add_rows(table_key, rows_in, session.uname, st=session.runtime)
727
  except Exception:
 
722
  label = by_key[computed[0]].get("label") or computed[0]
723
  raise err(400, "computed_field",
724
  f"{label!r} is worked out from other columns, so it cannot be imported into")
725
+ # β›” W29-T81 β€” THE TYPE WALL, AND IT LIVES HERE BECAUSE THE ONLY OTHER ONE IS IN THE BROWSER.
726
+ # `coerceClipboardValue` refuses "seventeen-ish" at an `int` column in the dialog; curl, a
727
+ # second client, or a future importer met no wall at all and the string landed verbatim in a
728
+ # typed column, where the grid then painted it as a fabricated `0`. Refused whole and BEFORE
729
+ # `add_rows`, matching this door's own atomicity: a half-imported file is the outcome every
730
+ # rule on this route exists to prevent. The sentence names the row and the column, because
731
+ # "invalid value" sends somebody hunting through 2,000 lines of spreadsheet.
732
+ for index, row in enumerate(rows_in):
733
+ for key, value in row.items():
734
+ why = ut.cell_type_refusal(by_key[key], value)
735
+ if why:
736
+ raise err(400, "bad_value", f"row {index + 1}: {why} β€” nothing was imported")
737
  try:
738
  made = ut.add_rows(table_key, rows_in, session.uname, st=session.runtime)
739
  except Exception:
platform/aios_grid.py CHANGED
The diff for this file is too large to render. See raw diff
 
platform/core/grid_events.py CHANGED
The diff for this file is too large to render. See raw diff
 
platform/core/table_store.py CHANGED
@@ -1,595 +1,618 @@
1
- """The generic per-user TABLE WORKSPACE store β€” the persistence half of the table-page factory.
2
-
3
- One durable store key holds one table OBJECT's per-user Airtable-style state:
4
-
5
- {username: {'views': {view_id: SavedView},
6
- 'fields': {field_key: Field}, # notes + custom_ + measure_ strata
7
- 'overlays': {str(pid): {field_key: value}}}}
8
-
9
- `make(table_key)` returns the six operations a table page's host loop needs, closed over that
10
- key. The Customer table's ops (`modules/customer_data.py`, key 'customer_table_workspace') are
11
- these exact functions β€” the logic MOVED here 2026-07-27 so that duplicating the Customer table
12
- pattern to a new object is a registry row + a config, not a copy of the store plumbing
13
- (owner directive: the table-page factory).
14
-
15
- A LIST (membership/formula semantics) is deliberately a different store from a VIEW
16
- (presentation/query state) β€” see modules/customer_data.py's customer_lists key.
17
- """
18
- import core.store as store
19
-
20
- #: Wave-9 I17 β€” the SHARED bucket. Views whose permissions make them visible to anyone but
21
- #: their creator live here instead of in a personal workspace, under a key that cannot collide
22
- #: with a username (usernames come from core/users.py and are never dunder-wrapped; `is_shared`
23
- #: guards it anyway). ONE HOME PER VIEW, never both: a view moved to personal is REMOVED from
24
- #: here, and a view shared is removed from its creator's workspace. Two homes would mean two
25
- #: divergent copies the moment either was edited.
26
- SHARED_KEY = '__shared__'
27
-
28
-
29
- def _may_see(view, viewer, is_admin=False):
30
- """Visibility for ONE shared view, fail-closed.
31
-
32
- 'collaborative' = everyone who can already open the module (the caller has gated that).
33
- 'users' = the named users, plus the creator, plus admins β€” an admin who could not
34
- see a view could not administer it either.
35
- Anything unrecognised returns False rather than defaulting open: an unreadable permission
36
- must never widen access ([[aios-permissioning]] β€” no fail-open defaults).
37
- """
38
- if not isinstance(view, dict):
39
- return False
40
- if view.get('createdBy') == viewer or is_admin:
41
- return True
42
- perms = view.get('permissions') or {}
43
- edit = perms.get('edit')
44
- if edit == 'collaborative':
45
- return True
46
- if edit == 'users':
47
- return viewer in set(perms.get('users') or ())
48
- return False # 'personal', absent, or junk
49
-
50
-
51
- def _may_edit(view, viewer, is_admin=False):
52
- """Who may WRITE a shared view. Same set as visibility today β€” the owner's item asks 'who
53
- can edit' and lists who 'can have access', i.e. seeing and editing are one grant. Kept as a
54
- separate function so they can diverge (a future read-only share) without hunting callers."""
55
- return _may_see(view, viewer, is_admin)
56
-
57
-
58
- def _may_administer(view, viewer, is_admin=False):
59
- """Who may change a view's PERMISSIONS, or delete it: the creator or an admin ONLY.
60
-
61
- Deliberately narrower than _may_edit. If a collaborator could rewrite `permissions` they
62
- could grant themselves sole ownership of somebody else's view, or quietly widen a
63
- users-scoped view to everyone β€” the classic privilege-escalation-by-edit hole.
64
- """
65
- if not isinstance(view, dict):
66
- return False
67
- return bool(is_admin) or view.get('createdBy') == viewer
68
-
69
-
70
- def is_shared(view):
71
- """A view belongs in the shared bucket when its permissions reach beyond its creator."""
72
- return ((view or {}).get('permissions') or {}).get('edit') in ('collaborative', 'users')
73
-
74
-
75
- def _unique_name(wanted, existing, *, fallback='Untitled', max_len=120):
76
- """Allocate one human-facing name inside a store.update transaction.
77
-
78
- Keys/ids remain structural identity. Names compare case-insensitively after collapsing
79
- whitespace, because those variants are indistinguishable in the UI. This helper belongs
80
- in the store layer: allocating from a pre-write snapshot lets two concurrent requests both
81
- choose the same free name before either write lands.
82
- """
83
- limit = max(1, int(max_len))
84
-
85
- def _clean(value):
86
- return ' '.join(str(value or '').split())
87
-
88
- base = (_clean(wanted) or _clean(fallback) or 'Untitled')[:limit].rstrip()
89
- taken = {_clean(value).casefold() for value in existing if _clean(value)}
90
- if base.casefold() not in taken:
91
- return base
92
- index = 2
93
- while True:
94
- suffix = f' {index}'
95
- stem = base[:max(0, limit - len(suffix))].rstrip()
96
- candidate = f'{stem}{suffix}' if stem else str(index)[-limit:]
97
- if candidate.casefold() not in taken:
98
- return candidate
99
- index += 1
100
-
101
-
102
- class TableStore:
103
- """The six store operations for one table object's workspace, closed over its store key.
104
-
105
- `st` (wave 18, C3-UT) is the STORE HANDLE β€” anything exposing `get(name)` /
106
- `update(name, fn, flush=)`. Default = `core.store` (tenant #0, every existing caller,
107
- zero behaviour change). The API passes the session's `TenantRuntime`, whose accessors
108
- apply the tenant prefix / repo binding β€” which is what makes a user table created by a
109
- Nurilab admin land in Nurilab's store instead of Royal's.
110
- """
111
-
112
- def __init__(self, table_key, st=None):
113
- self.table_key = table_key
114
- self._st = st if st is not None else store
115
-
116
- @property
117
- def st(self):
118
- """The bound store handle β€” for SIBLING registries (core/shares) that must read the
119
- same tenant's buckets this workspace lives in (wave 21, C1)."""
120
- return self._st
121
-
122
- def find_view(self, view_id):
123
- """`(owner_username, view)` for a view living in ANY personal stratum, else None.
124
-
125
- ⭐ Wave 21 (item 9, C1): the R10 grant registry names bare ids, so projecting a granted
126
- view means locating the OWNER's record inside this topic's bucket. Personal strata
127
- only β€” the `__shared__` bucket has its own read path (`shared_views`), and serving one
128
- view from two finders is how two copies drift."""
129
- vid = str(view_id or '').strip()
130
- if not vid:
131
- return None
132
- try:
133
- data = self._st.get(self.table_key) or {}
134
- except Exception:
135
- return None
136
- for username, ws in data.items():
137
- if username == SHARED_KEY or not isinstance(ws, dict):
138
- continue
139
- v = (ws.get('views') or {}).get(vid)
140
- if isinstance(v, dict):
141
- return str(username), dict(v)
142
- return None
143
-
144
- def find_folder(self, folder_id):
145
- """`(owner_username, folder_row, {view_id: view})` for a VIEWS folder living in any
146
- personal stratum, else None. `find_view`'s sibling, and here for the same reason.
147
-
148
- ⭐ D-37 (wave 20's R10 remainder, closed 2026-08-05): the grant registry accepts kind
149
- `folder` and has since wave 20, but only the VIEW kind was ever projected β€” so "share
150
- this folder with Karen" recorded a row, listed under Shared with me, and put nothing on
151
- Karen's screen. Projecting a folder means two lookups the view path does not need: WHO
152
- owns it, and WHICH views are filed in it. Folder membership lives in the owner's
153
- `itemFolders` map (item id -> folder id), never on the view record, so the views are
154
- found by asking that map rather than by reading a list off the folder.
155
-
156
- Views only (`folders['views']`): the cohort surface has its own store and its own
157
- sharing question, and answering both here would make one function mean two things.
158
- """
159
- fid = str(folder_id or '').strip()
160
- if not fid:
161
- return None
162
- try:
163
- data = self._st.get(self.table_key) or {}
164
- except Exception:
165
- return None
166
- for username, ws in data.items():
167
- if username == SHARED_KEY or not isinstance(ws, dict):
168
- continue
169
- rows = (ws.get('folders') or {}).get('views') or []
170
- hit = next((f for f in rows
171
- if isinstance(f, dict) and str(f.get('id') or '') == fid), None)
172
- if not hit:
173
- continue
174
- # ⚠ `itemFolders` IS KEYED BY SURFACE FIRST (`{'views': {itemId: folderId}, …}`) β€”
175
- # reading item ids off the top level finds the surface names instead and matches
176
- # nothing, so the projection silently returns an EMPTY folder and the feature looks
177
- # exactly as broken as it was before the fix. Caught by this change's own gate,
178
- # which is the entire argument for writing one.
179
- placed = (ws.get('itemFolders') or {}).get('views') or {}
180
- views = ws.get('views') or {}
181
- inside = {str(vid): dict(v) for vid, v in views.items()
182
- if isinstance(v, dict) and str(placed.get(str(vid)) or '') == fid}
183
- return str(username), dict(hit), inside
184
- return None
185
-
186
- # ---------------------------------------------------------------- read
187
- def workspace(self, username, consume_corrections=True):
188
- """One user's durable workspace: always the full three-strata shape."""
189
- try:
190
- data = self._st.get(self.table_key) or {}
191
- ws = data.get(username, {}) or {}
192
- # A collision acknowledgement is protocol state, not part of a field definition.
193
- # Consume it with the first fresh workspace payload after the correcting write,
194
- # then splice a bounded copy into that payload only. Keeping it out of `fields`
195
- # prevents an old request id surviving forever and overriding a later rename.
196
- corrections = {}
197
- if consume_corrections and ws.get('fieldCorrections'):
198
- def _take(current):
199
- current_ws = current.get(username) or {}
200
- pending = current_ws.get('fieldCorrections') or {}
201
- corrections.update({
202
- str(key)[:80]: dict(value)
203
- for key, value in pending.items()
204
- if isinstance(value, dict)
205
- })
206
- current_ws.pop('fieldCorrections', None)
207
- return current
208
-
209
- data = self._st.update(self.table_key, _take, flush='async')
210
- ws = (data or {}).get(username, {}) or {}
211
- except Exception:
212
- ws = {}
213
- corrections = {}
214
- fields = {
215
- key: dict(value) if isinstance(value, dict) else value
216
- for key, value in (ws.get('fields') or {}).items()
217
- }
218
- for key, ack in corrections.items():
219
- field = fields.get(key)
220
- accepted_label = str(ack.get('label') or '')[:120]
221
- requested_label = str(ack.get('labelCorrectedFrom') or '')[:120]
222
- correction_id = str(ack.get('labelCorrectionId') or '')[:180]
223
- # A newer field write clears/replaces the pending ack in the SAME transaction.
224
- # The label check is an extra belt against ever attaching a stale ack to a newer
225
- # definition if a future store implementation weakens that ordering.
226
- if (isinstance(field, dict) and accepted_label
227
- and str(field.get('label') or '') == accepted_label
228
- and requested_label and correction_id):
229
- field['labelCorrectedFrom'] = requested_label
230
- field['labelCorrectionId'] = correction_id
231
- out = {
232
- 'views': dict(ws.get('views') or {}),
233
- 'fields': fields,
234
- 'overlays': dict(ws.get('overlays') or {}),
235
- # wave-8 I11 (C4): folders over the saved views / cohorts sidebars. A FOURTH
236
- # stratum rather than a key on each item β€” see aios_grid.clean_folders for why
237
- # (a cohort lives in another store, and filing is an organising act, not part of
238
- # what a view is). Absent for every workspace saved before this wave, which is
239
- # exactly "no folders yet".
240
- 'folders': dict(ws.get('folders') or {}),
241
- 'itemFolders': dict(ws.get('itemFolders') or {}),
242
- }
243
- # 2026-07-31 (owner item 3): WHERE THE USER LEFT OFF survives a new browser. The
244
- # client's localStorage copy wins when present; this is the server's answer for a
245
- # fresh profile, which used to fall all the way to the system default view.
246
- if ws.get('activeViewId'):
247
- out['activeViewId'] = str(ws['activeViewId'])
248
- # Wave 2026-08-02 (C-LAYOUT): the per-user record-detail field order. A fifth
249
- # stratum, absent until the user first reorders β€” exactly "default order".
250
- if isinstance(ws.get('recordLayout'), dict):
251
- out['recordLayout'] = dict(ws['recordLayout'])
252
- return out
253
-
254
- # ---------------------------------------------------------------- write
255
- def _update(self, username, change):
256
- def _up(data):
257
- ws = data.setdefault(username, {})
258
- ws.setdefault('views', {})
259
- ws.setdefault('fields', {})
260
- ws.setdefault('overlays', {})
261
- ws.setdefault('folders', {})
262
- ws.setdefault('itemFolders', {})
263
- change(ws)
264
- return data
265
- # flush='async' (wave-7 W3): this is THE hot path β€” every autosaved filter tweak,
266
- # column note and typed overlay cell lands here inside the component round-trip, and
267
- # the historical synchronous hub commit cost seconds per edit. The mutation applies to
268
- # the in-process cache (read-your-writes for every subsequent render); the hub write
269
- # coalesces in the background. Registry/auth writes elsewhere stay flush='sync'.
270
- return self._st.update(self.table_key, _up, flush='async')
271
-
272
- def rename_choice_values(self, username, change):
273
- """Apply an arbitrary workspace rewrite (wave 20, item 15 / C-RENAME).
274
-
275
- ⚠ NAMED FOR ITS ONE CALLER RATHER THAN EXPOSED AS A GENERIC `mutate`, deliberately. A
276
- public "do anything to the workspace" method is an invitation to put write logic in
277
- callers instead of here, and every OTHER method on this class exists precisely because
278
- that logic belongs in one place. Renaming a choice is the one operation that must touch
279
- three strata AT ONCE β€” the field's `choices`, the cells in `overlays`, and the views that
280
- filter or colour by the old value β€” inside a SINGLE transaction, because a rename that
281
- updated the cells and not the filters would leave a saved view matching nothing.
282
-
283
- `change(ws)` receives the whole workspace with every stratum pre-created (see `_update`).
284
- """
285
- return self._update(username, change)
286
-
287
- def save_active_view(self, username, view_id):
288
- """Remember which view this user last opened (owner item 3, 2026-07-31).
289
-
290
- Presentation state, not authorisation: the READ side re-validates the id against what
291
- the caller may actually see, so a stale or foreign id degrades to the default view
292
- rather than granting anything. Stored per user like every other stratum.
293
- """
294
- vid = str(view_id or '').strip()[:120]
295
- if not vid or username == SHARED_KEY:
296
- return
297
-
298
- def _set(ws):
299
- ws['activeViewId'] = vid
300
- self._update(username, _set)
301
-
302
- def save_record_layout(self, username, order):
303
- """The per-user RECORD-DETAIL field order (wave 2026-08-02, C-LAYOUT).
304
-
305
- Presentation state for ONE surface β€” the record modal. Deliberately not view config:
306
- the owner's ask is per-user, not per-view, and it must never reorder grid columns.
307
- The event handler validated keys against the live field set; the wire re-validates at
308
- serve time (aios_grid.workspace_wire), so a deleted field cannot outlive itself here.
309
- An empty order clears the stratum back to "default order".
310
- """
311
- if username == SHARED_KEY:
312
- return
313
- clean, seen = [], set()
314
- for key in (order or [])[:200]:
315
- key = str(key or '').strip()[:80]
316
- if key and key not in seen:
317
- seen.add(key)
318
- clean.append(key)
319
-
320
- def _set(ws):
321
- if clean:
322
- ws['recordLayout'] = {'order': clean}
323
- else:
324
- ws.pop('recordLayout', None)
325
-
326
- self._update(username, _set)
327
-
328
- def save_folders(self, username, folders, item_folders):
329
- """Replace the folder stratum wholesale (wave-8 I11).
330
-
331
- Wholesale rather than per-folder because the caller has ALREADY validated the complete
332
- picture through aios_grid.clean_folders / clean_item_folders, and those two are
333
- interdependent: a placement is only legal while its folder exists, so committing them
334
- separately would leave a window where a reader sees an item filed into a folder that is
335
- not there yet. One write, one consistent state.
336
- """
337
- def _set(ws):
338
- ws['folders'] = dict(folders or {})
339
- ws['itemFolders'] = dict(item_folders or {})
340
- self._update(username, _set)
341
-
342
- def save_view_order(self, username, order):
343
- """⭐ WAVE-27 item 5 (contract C7) β€” this user's own ORDER for the views rail.
344
-
345
- Wholesale, like `save_folders` above and for the same reason: the client sends the full
346
- list it is looking at, not a delta, because a partial order cannot say where an UNNAMED
347
- view went.
348
-
349
- β›” PER USER, and it belongs in this stratum rather than on the view records themselves.
350
- `aios_grid`'s own folder note argues it out for placements and every word applies: an
351
- arrangement is a per-user ORGANISING act, not part of what a view IS β€” so keeping it out
352
- of the view config means duplicating, sharing or exporting a view does not drag one
353
- person's rail position along with it. It also means a SHARED view can sit in a different
354
- place for each person who can see it, which is the only coherent answer once two people
355
- share one view.
356
-
357
- An empty list CLEARS the arrangement (back to server order) rather than storing `[]`.
358
- """
359
- def _set(ws):
360
- clean = []
361
- seen = set()
362
- for vid in (order or []):
363
- vid = str(vid).strip()[:120]
364
- if vid and vid not in seen:
365
- seen.add(vid)
366
- clean.append(vid)
367
- if clean:
368
- ws['viewOrder'] = clean
369
- else:
370
- ws.pop('viewOrder', None)
371
- self._update(username, _set)
372
-
373
- def shared_views(self, viewer, is_admin=False):
374
- """Every SHARED view this viewer may see, by id (wave-9 I17).
375
-
376
- Read-only and independent of the viewer's own workspace: the caller merges. Returns
377
- only what `_may_see` allows, so a caller cannot accidentally render somebody else's
378
- personal view by forgetting to filter.
379
- """
380
- try:
381
- bucket = ((self._st.get(self.table_key) or {}).get(SHARED_KEY) or {}).get('views') or {}
382
- except Exception:
383
- return {}
384
- return {vid: dict(v) for vid, v in bucket.items()
385
- if _may_see(v, viewer, is_admin)}
386
-
387
- def shared_view(self, view_id):
388
- """One shared view RAW β€” no visibility filter. For authorisation decisions only: a
389
- caller must know a view exists and who owns it before it can decide whether the actor
390
- may touch it. Never hand the result to a renderer without checking `_may_see`."""
391
- try:
392
- return ((self._st.get(self.table_key) or {}).get(SHARED_KEY) or {}
393
- ).get('views', {}).get(str(view_id))
394
- except Exception:
395
- return None
396
-
397
- def save_view(self, username, view, shared=None, reserved_names=(), is_admin=False):
398
- """Upsert a SavedView into its ONE home β€” personal workspace or the shared bucket.
399
-
400
- `shared` defaults to reading the view's own permissions (`is_shared`). Whichever home
401
- it lands in, the view is REMOVED from the other, so a view can never exist as two
402
- copies that diverge on the next edit.
403
-
404
- ⚠ AUTHORISATION IS THE CALLER'S JOB and must happen BEFORE this is called β€” this layer
405
- moves data and does not know who is asking. `_cl_handle_one` is the wall.
406
- """
407
- view_id = str((view or {}).get('id') or '').strip()
408
- if not view_id:
409
- raise ValueError('view id is required')
410
- if username == SHARED_KEY:
411
- raise ValueError('reserved username')
412
- to_shared = is_shared(view) if shared is None else bool(shared)
413
- requested = dict(view)
414
- accepted = {}
415
-
416
- def _up(data):
417
- # View names are tenant-global: every personal workspace plus the shared bucket.
418
- # This deliberately includes views the actor cannot see. The only disclosed fact
419
- # is that a display name is already taken, while the categorical "no duplicate
420
- # view names" contract remains true when a personal view is later shared.
421
- names = list(reserved_names or ())
422
- for workspace in data.values():
423
- if not isinstance(workspace, dict):
424
- continue
425
- names.extend(
426
- value.get('name')
427
- for candidate_id, value in (workspace.get('views') or {}).items()
428
- if candidate_id != view_id and isinstance(value, dict)
429
- )
430
- payload = dict(requested)
431
- payload['name'] = _unique_name(payload.get('name'), names)
432
- accepted.clear()
433
- accepted.update(payload)
434
- if to_shared:
435
- bucket = data.setdefault(SHARED_KEY, {})
436
- bucket.setdefault('views', {})[view_id] = payload
437
- # it may have lived in the creator's workspace before being shared
438
- owner = data.get(payload.get('createdBy') or username) or {}
439
- (owner.get('views') or {}).pop(view_id, None)
440
- else:
441
- ws = data.setdefault(username, {})
442
- ws.setdefault('views', {})[view_id] = payload
443
- (data.get(SHARED_KEY, {}).get('views') or {}).pop(view_id, None)
444
- return data
445
-
446
- self._st.update(self.table_key, _up, flush='async')
447
- return dict(accepted)
448
-
449
- def delete_view(self, username, view_id):
450
- """Delete a custom/list view override. The system all-rows view is guarded by caller.
451
-
452
- Removes from BOTH homes: the caller has already authorised the delete, and leaving a
453
- stale copy in the other bucket would resurrect the view on the next read.
454
- """
455
- vid = str(view_id)
456
-
457
- def _up(data):
458
- (data.get(username, {}).get('views') or {}).pop(vid, None)
459
- (data.get(SHARED_KEY, {}).get('views') or {}).pop(vid, None)
460
- return data
461
-
462
- self._st.update(self.table_key, _up, flush='async')
463
-
464
- def save_field(self, username, field, reserved_names=(), correction_id=None):
465
- """Persist a column note or a user-created (custom_/measure_) field definition."""
466
- key = str((field or {}).get('key') or '').strip()
467
- if not key:
468
- raise ValueError('field key is required')
469
- requested = dict(field)
470
- accepted = {}
471
-
472
- def _save(ws):
473
- names = list(reserved_names or ())
474
- names.extend(
475
- value.get('label')
476
- for candidate_key, value in (ws.get('fields') or {}).items()
477
- if candidate_key != key and isinstance(value, dict)
478
- )
479
- payload = dict(requested)
480
- payload.pop('labelCorrectedFrom', None)
481
- payload.pop('labelCorrectionId', None)
482
- requested_label = ' '.join(
483
- str(payload.get('label') or 'Untitled').split())[:120].rstrip()
484
- payload['label'] = _unique_name(requested_label, names)
485
- corrections = ws.setdefault('fieldCorrections', {})
486
- corrections.pop(key, None)
487
- if payload['label'] != requested_label and correction_id:
488
- corrections[key] = {
489
- 'label': payload['label'],
490
- 'labelCorrectedFrom': requested_label,
491
- 'labelCorrectionId': str(correction_id)[:180],
492
- }
493
- if not corrections:
494
- ws.pop('fieldCorrections', None)
495
- accepted.clear()
496
- accepted.update(payload)
497
- # A cleared note on an immutable source field returns to the canonical schema
498
- # instead of leaving a meaningless override row. Custom fields remain even with
499
- # an empty note β€” and so does a PRESET field carrying a measure-window override
500
- # (wave-2 item 8) or a DISPLAY-format override (wave-5 item 10): the note may be
501
- # empty but the period/format is real user state.
502
- if (not payload.get('custom') and not str(payload.get('note') or '').strip()
503
- and not isinstance(payload.get('measure'), dict)
504
- and not isinstance(payload.get('format'), dict)):
505
- ws['fields'].pop(key, None)
506
- else:
507
- ws['fields'][key] = payload
508
-
509
- self._update(username, _save)
510
- return dict(accepted)
511
-
512
- def duplicate_field(self, username, source_key, new_key, field,
513
- reserved_names=(), correction_id=None):
514
- """Clone a user-created field in ONE store transaction (wave-5 item 1): the new
515
- definition plus β€” for `custom_` overlay sources only β€” every stored cell value under
516
- the source key. One transaction, because a def without its values (or values without a
517
- def) is exactly the orphan state delete_field exists to prevent, in reverse.
518
- The caller validated both keys (same created stratum) and stamped the clone's
519
- createdBy; this layer only moves data."""
520
- source_key = str(source_key or '').strip()
521
- new_key = str(new_key or '').strip()
522
- if not source_key or not new_key or source_key == new_key:
523
- raise ValueError('duplicate_field needs two distinct keys')
524
- requested = dict(field)
525
- accepted = {}
526
-
527
- def _dup(ws):
528
- names = list(reserved_names or ())
529
- names.extend(
530
- value.get('label')
531
- for candidate_key, value in (ws.get('fields') or {}).items()
532
- if candidate_key != new_key and isinstance(value, dict)
533
- )
534
- payload = dict(requested)
535
- payload.pop('labelCorrectedFrom', None)
536
- payload.pop('labelCorrectionId', None)
537
- requested_label = ' '.join(
538
- str(payload.get('label') or 'Untitled').split())[:120].rstrip()
539
- payload['label'] = _unique_name(requested_label, names)
540
- corrections = ws.setdefault('fieldCorrections', {})
541
- corrections.pop(new_key, None)
542
- if payload['label'] != requested_label and correction_id:
543
- corrections[new_key] = {
544
- 'label': payload['label'],
545
- 'labelCorrectedFrom': requested_label,
546
- 'labelCorrectionId': str(correction_id)[:180],
547
- }
548
- if not corrections:
549
- ws.pop('fieldCorrections', None)
550
- accepted.clear()
551
- accepted.update(payload)
552
- ws['fields'][new_key] = payload
553
- if source_key.startswith('custom_'):
554
- for row in ws['overlays'].values():
555
- if isinstance(row, dict) and source_key in row:
556
- row[new_key] = row[source_key]
557
-
558
- self._update(username, _dup)
559
- return dict(accepted)
560
-
561
- def delete_field(self, username, key):
562
- """Delete a USER-CREATED field definition outright (owner gap closed 2026-07-27).
563
-
564
- Only the created strata ever reach here (`custom_` overlay fields, `measure_` formula
565
- columns β€” the caller enforces the prefix). The stored overlay VALUES for the key are
566
- scrubbed with it: a deleted column's cells must not linger as orphan data that would
567
- silently resurface if the key were ever reused. Views referencing the key self-heal on
568
- their next autosave (an unknown colId is dropped) β€” the rule every stale key rides.
569
- """
570
- key = str(key or '').strip()
571
- if not key:
572
- return
573
-
574
- def _drop(ws):
575
- ws['fields'].pop(key, None)
576
- for row in ws['overlays'].values():
577
- if isinstance(row, dict):
578
- row.pop(key, None)
579
-
580
- self._update(username, _drop)
581
-
582
- def patch_overlay(self, username, pid, updates):
583
- """Patch only the external editable stratum; never writes to the source system."""
584
- clean = dict(updates or {})
585
- if not clean:
586
- return
587
-
588
- def _patch(ws):
589
- ws['overlays'].setdefault(str(int(pid)), {}).update(clean)
590
-
591
- self._update(username, _patch)
592
-
593
-
594
- def make(table_key, st=None):
595
- return TableStore(table_key, st=st)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """The generic per-user TABLE WORKSPACE store β€” the persistence half of the table-page factory.
2
+
3
+ One durable store key holds one table OBJECT's per-user Airtable-style state:
4
+
5
+ {username: {'views': {view_id: SavedView},
6
+ 'fields': {field_key: Field}, # notes + custom_ + measure_ strata
7
+ 'overlays': {str(pid): {field_key: value}}}}
8
+
9
+ `make(table_key)` returns the six operations a table page's host loop needs, closed over that
10
+ key. The Customer table's ops (`modules/customer_data.py`, key 'customer_table_workspace') are
11
+ these exact functions β€” the logic MOVED here 2026-07-27 so that duplicating the Customer table
12
+ pattern to a new object is a registry row + a config, not a copy of the store plumbing
13
+ (owner directive: the table-page factory).
14
+
15
+ A LIST (membership/formula semantics) is deliberately a different store from a VIEW
16
+ (presentation/query state) β€” see modules/customer_data.py's customer_lists key.
17
+ """
18
+ import core.store as store
19
+
20
+ #: Wave-9 I17 β€” the SHARED bucket. Views whose permissions make them visible to anyone but
21
+ #: their creator live here instead of in a personal workspace, under a key that cannot collide
22
+ #: with a username (usernames come from core/users.py and are never dunder-wrapped; `is_shared`
23
+ #: guards it anyway). ONE HOME PER VIEW, never both: a view moved to personal is REMOVED from
24
+ #: here, and a view shared is removed from its creator's workspace. Two homes would mean two
25
+ #: divergent copies the moment either was edited.
26
+ SHARED_KEY = '__shared__'
27
+
28
+
29
+ def _may_see(view, viewer, is_admin=False):
30
+ """Visibility for ONE shared view, fail-closed.
31
+
32
+ 'collaborative' = everyone who can already open the module (the caller has gated that).
33
+ 'users' = the named users, plus the creator, plus admins β€” an admin who could not
34
+ see a view could not administer it either.
35
+ Anything unrecognised returns False rather than defaulting open: an unreadable permission
36
+ must never widen access ([[aios-permissioning]] β€” no fail-open defaults).
37
+ """
38
+ if not isinstance(view, dict):
39
+ return False
40
+ if view.get('createdBy') == viewer or is_admin:
41
+ return True
42
+ perms = view.get('permissions') or {}
43
+ edit = perms.get('edit')
44
+ if edit == 'collaborative':
45
+ return True
46
+ if edit == 'users':
47
+ return viewer in set(perms.get('users') or ())
48
+ return False # 'personal', absent, or junk
49
+
50
+
51
+ def _may_edit(view, viewer, is_admin=False):
52
+ """Who may WRITE a shared view. Same set as visibility today β€” the owner's item asks 'who
53
+ can edit' and lists who 'can have access', i.e. seeing and editing are one grant. Kept as a
54
+ separate function so they can diverge (a future read-only share) without hunting callers."""
55
+ return _may_see(view, viewer, is_admin)
56
+
57
+
58
+ def _may_administer(view, viewer, is_admin=False):
59
+ """Who may change a view's PERMISSIONS, or delete it: the creator or an admin ONLY.
60
+
61
+ Deliberately narrower than _may_edit. If a collaborator could rewrite `permissions` they
62
+ could grant themselves sole ownership of somebody else's view, or quietly widen a
63
+ users-scoped view to everyone β€” the classic privilege-escalation-by-edit hole.
64
+ """
65
+ if not isinstance(view, dict):
66
+ return False
67
+ return bool(is_admin) or view.get('createdBy') == viewer
68
+
69
+
70
+ def is_shared(view):
71
+ """A view belongs in the shared bucket when its permissions reach beyond its creator."""
72
+ return ((view or {}).get('permissions') or {}).get('edit') in ('collaborative', 'users')
73
+
74
+
75
+ def source_override_is_empty(payload):
76
+ """Does this stored definition of a SOURCE (non-custom) column carry any user state?
77
+
78
+ A cleared note on an immutable source field returns to the canonical schema instead of
79
+ leaving a meaningless override row. Custom fields remain even with an empty note β€” and so
80
+ does a PRESET field carrying a measure-window override (wave-2 item 8), a DISPLAY-format
81
+ override (wave-5 item 10), and, since W29-T83, a COLUMN SUMMARY.
82
+
83
+ β›” EVERY CLAUSE IS A SETTING A USER MADE, and each one omitted is a setting that silently
84
+ stops surviving a session. `agg` was missing: choosing Average on Customer's `Overdue days`
85
+ built an override whose only content was that summary, so this rule threw the whole row away
86
+ on write while the menu went on reading "Summary: Average" from the client's own optimistic
87
+ copy until the next login β€” a discarded WRITE wearing the face of a failed read
88
+ ([[lost-write-looks-like-failed-read]]). Measured on `bac40c2`; a `ut_*` table, which stores
89
+ its definitions through another door entirely, kept it.
90
+
91
+ ⚠ ONE RULE, TWO CALLERS β€” here and `grid_events`' store-less fallback. Two copies of a
92
+ discard rule is how one of them keeps a write the other bins ([[one-evaluator-per-question]]).
93
+ ⚠ A CLEARED summary still drops the row, which is the intent: with nothing else set, the
94
+ column goes back to whatever the contract declares for it.
95
+ """
96
+ payload = payload or {}
97
+ if payload.get('custom'):
98
+ return False
99
+ return (not str(payload.get('note') or '').strip()
100
+ and not isinstance(payload.get('measure'), dict)
101
+ and not isinstance(payload.get('format'), dict)
102
+ and not str(payload.get('agg') or '').strip())
103
+
104
+
105
+ def _unique_name(wanted, existing, *, fallback='Untitled', max_len=120):
106
+ """Allocate one human-facing name inside a store.update transaction.
107
+
108
+ Keys/ids remain structural identity. Names compare case-insensitively after collapsing
109
+ whitespace, because those variants are indistinguishable in the UI. This helper belongs
110
+ in the store layer: allocating from a pre-write snapshot lets two concurrent requests both
111
+ choose the same free name before either write lands.
112
+ """
113
+ limit = max(1, int(max_len))
114
+
115
+ def _clean(value):
116
+ return ' '.join(str(value or '').split())
117
+
118
+ base = (_clean(wanted) or _clean(fallback) or 'Untitled')[:limit].rstrip()
119
+ taken = {_clean(value).casefold() for value in existing if _clean(value)}
120
+ if base.casefold() not in taken:
121
+ return base
122
+ index = 2
123
+ while True:
124
+ suffix = f' {index}'
125
+ stem = base[:max(0, limit - len(suffix))].rstrip()
126
+ candidate = f'{stem}{suffix}' if stem else str(index)[-limit:]
127
+ if candidate.casefold() not in taken:
128
+ return candidate
129
+ index += 1
130
+
131
+
132
+ class TableStore:
133
+ """The six store operations for one table object's workspace, closed over its store key.
134
+
135
+ `st` (wave 18, C3-UT) is the STORE HANDLE β€” anything exposing `get(name)` /
136
+ `update(name, fn, flush=)`. Default = `core.store` (tenant #0, every existing caller,
137
+ zero behaviour change). The API passes the session's `TenantRuntime`, whose accessors
138
+ apply the tenant prefix / repo binding β€” which is what makes a user table created by a
139
+ Nurilab admin land in Nurilab's store instead of Royal's.
140
+ """
141
+
142
+ def __init__(self, table_key, st=None):
143
+ self.table_key = table_key
144
+ self._st = st if st is not None else store
145
+
146
+ @property
147
+ def st(self):
148
+ """The bound store handle β€” for SIBLING registries (core/shares) that must read the
149
+ same tenant's buckets this workspace lives in (wave 21, C1)."""
150
+ return self._st
151
+
152
+ def find_view(self, view_id):
153
+ """`(owner_username, view)` for a view living in ANY personal stratum, else None.
154
+
155
+ ⭐ Wave 21 (item 9, C1): the R10 grant registry names bare ids, so projecting a granted
156
+ view means locating the OWNER's record inside this topic's bucket. Personal strata
157
+ only β€” the `__shared__` bucket has its own read path (`shared_views`), and serving one
158
+ view from two finders is how two copies drift."""
159
+ vid = str(view_id or '').strip()
160
+ if not vid:
161
+ return None
162
+ try:
163
+ data = self._st.get(self.table_key) or {}
164
+ except Exception:
165
+ return None
166
+ for username, ws in data.items():
167
+ if username == SHARED_KEY or not isinstance(ws, dict):
168
+ continue
169
+ v = (ws.get('views') or {}).get(vid)
170
+ if isinstance(v, dict):
171
+ return str(username), dict(v)
172
+ return None
173
+
174
+ def find_folder(self, folder_id):
175
+ """`(owner_username, folder_row, {view_id: view})` for a VIEWS folder living in any
176
+ personal stratum, else None. `find_view`'s sibling, and here for the same reason.
177
+
178
+ ⭐ D-37 (wave 20's R10 remainder, closed 2026-08-05): the grant registry accepts kind
179
+ `folder` and has since wave 20, but only the VIEW kind was ever projected β€” so "share
180
+ this folder with Karen" recorded a row, listed under Shared with me, and put nothing on
181
+ Karen's screen. Projecting a folder means two lookups the view path does not need: WHO
182
+ owns it, and WHICH views are filed in it. Folder membership lives in the owner's
183
+ `itemFolders` map (item id -> folder id), never on the view record, so the views are
184
+ found by asking that map rather than by reading a list off the folder.
185
+
186
+ Views only (`folders['views']`): the cohort surface has its own store and its own
187
+ sharing question, and answering both here would make one function mean two things.
188
+ """
189
+ fid = str(folder_id or '').strip()
190
+ if not fid:
191
+ return None
192
+ try:
193
+ data = self._st.get(self.table_key) or {}
194
+ except Exception:
195
+ return None
196
+ for username, ws in data.items():
197
+ if username == SHARED_KEY or not isinstance(ws, dict):
198
+ continue
199
+ rows = (ws.get('folders') or {}).get('views') or []
200
+ hit = next((f for f in rows
201
+ if isinstance(f, dict) and str(f.get('id') or '') == fid), None)
202
+ if not hit:
203
+ continue
204
+ # ⚠ `itemFolders` IS KEYED BY SURFACE FIRST (`{'views': {itemId: folderId}, …}`) β€”
205
+ # reading item ids off the top level finds the surface names instead and matches
206
+ # nothing, so the projection silently returns an EMPTY folder and the feature looks
207
+ # exactly as broken as it was before the fix. Caught by this change's own gate,
208
+ # which is the entire argument for writing one.
209
+ placed = (ws.get('itemFolders') or {}).get('views') or {}
210
+ views = ws.get('views') or {}
211
+ inside = {str(vid): dict(v) for vid, v in views.items()
212
+ if isinstance(v, dict) and str(placed.get(str(vid)) or '') == fid}
213
+ return str(username), dict(hit), inside
214
+ return None
215
+
216
+ # ---------------------------------------------------------------- read
217
+ def workspace(self, username, consume_corrections=True):
218
+ """One user's durable workspace: always the full three-strata shape."""
219
+ try:
220
+ data = self._st.get(self.table_key) or {}
221
+ ws = data.get(username, {}) or {}
222
+ # A collision acknowledgement is protocol state, not part of a field definition.
223
+ # Consume it with the first fresh workspace payload after the correcting write,
224
+ # then splice a bounded copy into that payload only. Keeping it out of `fields`
225
+ # prevents an old request id surviving forever and overriding a later rename.
226
+ corrections = {}
227
+ if consume_corrections and ws.get('fieldCorrections'):
228
+ def _take(current):
229
+ current_ws = current.get(username) or {}
230
+ pending = current_ws.get('fieldCorrections') or {}
231
+ corrections.update({
232
+ str(key)[:80]: dict(value)
233
+ for key, value in pending.items()
234
+ if isinstance(value, dict)
235
+ })
236
+ current_ws.pop('fieldCorrections', None)
237
+ return current
238
+
239
+ data = self._st.update(self.table_key, _take, flush='async')
240
+ ws = (data or {}).get(username, {}) or {}
241
+ except Exception:
242
+ ws = {}
243
+ corrections = {}
244
+ fields = {
245
+ key: dict(value) if isinstance(value, dict) else value
246
+ for key, value in (ws.get('fields') or {}).items()
247
+ }
248
+ for key, ack in corrections.items():
249
+ field = fields.get(key)
250
+ accepted_label = str(ack.get('label') or '')[:120]
251
+ requested_label = str(ack.get('labelCorrectedFrom') or '')[:120]
252
+ correction_id = str(ack.get('labelCorrectionId') or '')[:180]
253
+ # A newer field write clears/replaces the pending ack in the SAME transaction.
254
+ # The label check is an extra belt against ever attaching a stale ack to a newer
255
+ # definition if a future store implementation weakens that ordering.
256
+ if (isinstance(field, dict) and accepted_label
257
+ and str(field.get('label') or '') == accepted_label
258
+ and requested_label and correction_id):
259
+ field['labelCorrectedFrom'] = requested_label
260
+ field['labelCorrectionId'] = correction_id
261
+ out = {
262
+ 'views': dict(ws.get('views') or {}),
263
+ 'fields': fields,
264
+ 'overlays': dict(ws.get('overlays') or {}),
265
+ # wave-8 I11 (C4): folders over the saved views / cohorts sidebars. A FOURTH
266
+ # stratum rather than a key on each item β€” see aios_grid.clean_folders for why
267
+ # (a cohort lives in another store, and filing is an organising act, not part of
268
+ # what a view is). Absent for every workspace saved before this wave, which is
269
+ # exactly "no folders yet".
270
+ 'folders': dict(ws.get('folders') or {}),
271
+ 'itemFolders': dict(ws.get('itemFolders') or {}),
272
+ }
273
+ # 2026-07-31 (owner item 3): WHERE THE USER LEFT OFF survives a new browser. The
274
+ # client's localStorage copy wins when present; this is the server's answer for a
275
+ # fresh profile, which used to fall all the way to the system default view.
276
+ if ws.get('activeViewId'):
277
+ out['activeViewId'] = str(ws['activeViewId'])
278
+ # Wave 2026-08-02 (C-LAYOUT): the per-user record-detail field order. A fifth
279
+ # stratum, absent until the user first reorders β€” exactly "default order".
280
+ if isinstance(ws.get('recordLayout'), dict):
281
+ out['recordLayout'] = dict(ws['recordLayout'])
282
+ return out
283
+
284
+ # ---------------------------------------------------------------- write
285
+ def _update(self, username, change):
286
+ def _up(data):
287
+ ws = data.setdefault(username, {})
288
+ ws.setdefault('views', {})
289
+ ws.setdefault('fields', {})
290
+ ws.setdefault('overlays', {})
291
+ ws.setdefault('folders', {})
292
+ ws.setdefault('itemFolders', {})
293
+ change(ws)
294
+ return data
295
+ # flush='async' (wave-7 W3): this is THE hot path β€” every autosaved filter tweak,
296
+ # column note and typed overlay cell lands here inside the component round-trip, and
297
+ # the historical synchronous hub commit cost seconds per edit. The mutation applies to
298
+ # the in-process cache (read-your-writes for every subsequent render); the hub write
299
+ # coalesces in the background. Registry/auth writes elsewhere stay flush='sync'.
300
+ return self._st.update(self.table_key, _up, flush='async')
301
+
302
+ def rename_choice_values(self, username, change):
303
+ """Apply an arbitrary workspace rewrite (wave 20, item 15 / C-RENAME).
304
+
305
+ ⚠ NAMED FOR ITS ONE CALLER RATHER THAN EXPOSED AS A GENERIC `mutate`, deliberately. A
306
+ public "do anything to the workspace" method is an invitation to put write logic in
307
+ callers instead of here, and every OTHER method on this class exists precisely because
308
+ that logic belongs in one place. Renaming a choice is the one operation that must touch
309
+ three strata AT ONCE β€” the field's `choices`, the cells in `overlays`, and the views that
310
+ filter or colour by the old value β€” inside a SINGLE transaction, because a rename that
311
+ updated the cells and not the filters would leave a saved view matching nothing.
312
+
313
+ `change(ws)` receives the whole workspace with every stratum pre-created (see `_update`).
314
+ """
315
+ return self._update(username, change)
316
+
317
+ def save_active_view(self, username, view_id):
318
+ """Remember which view this user last opened (owner item 3, 2026-07-31).
319
+
320
+ Presentation state, not authorisation: the READ side re-validates the id against what
321
+ the caller may actually see, so a stale or foreign id degrades to the default view
322
+ rather than granting anything. Stored per user like every other stratum.
323
+ """
324
+ vid = str(view_id or '').strip()[:120]
325
+ if not vid or username == SHARED_KEY:
326
+ return
327
+
328
+ def _set(ws):
329
+ ws['activeViewId'] = vid
330
+ self._update(username, _set)
331
+
332
+ def save_record_layout(self, username, order):
333
+ """The per-user RECORD-DETAIL field order (wave 2026-08-02, C-LAYOUT).
334
+
335
+ Presentation state for ONE surface β€” the record modal. Deliberately not view config:
336
+ the owner's ask is per-user, not per-view, and it must never reorder grid columns.
337
+ The event handler validated keys against the live field set; the wire re-validates at
338
+ serve time (aios_grid.workspace_wire), so a deleted field cannot outlive itself here.
339
+ An empty order clears the stratum back to "default order".
340
+ """
341
+ if username == SHARED_KEY:
342
+ return
343
+ clean, seen = [], set()
344
+ for key in (order or [])[:200]:
345
+ key = str(key or '').strip()[:80]
346
+ if key and key not in seen:
347
+ seen.add(key)
348
+ clean.append(key)
349
+
350
+ def _set(ws):
351
+ if clean:
352
+ ws['recordLayout'] = {'order': clean}
353
+ else:
354
+ ws.pop('recordLayout', None)
355
+
356
+ self._update(username, _set)
357
+
358
+ def save_folders(self, username, folders, item_folders):
359
+ """Replace the folder stratum wholesale (wave-8 I11).
360
+
361
+ Wholesale rather than per-folder because the caller has ALREADY validated the complete
362
+ picture through aios_grid.clean_folders / clean_item_folders, and those two are
363
+ interdependent: a placement is only legal while its folder exists, so committing them
364
+ separately would leave a window where a reader sees an item filed into a folder that is
365
+ not there yet. One write, one consistent state.
366
+ """
367
+ def _set(ws):
368
+ ws['folders'] = dict(folders or {})
369
+ ws['itemFolders'] = dict(item_folders or {})
370
+ self._update(username, _set)
371
+
372
+ def save_view_order(self, username, order):
373
+ """⭐ WAVE-27 item 5 (contract C7) β€” this user's own ORDER for the views rail.
374
+
375
+ Wholesale, like `save_folders` above and for the same reason: the client sends the full
376
+ list it is looking at, not a delta, because a partial order cannot say where an UNNAMED
377
+ view went.
378
+
379
+ β›” PER USER, and it belongs in this stratum rather than on the view records themselves.
380
+ `aios_grid`'s own folder note argues it out for placements and every word applies: an
381
+ arrangement is a per-user ORGANISING act, not part of what a view IS β€” so keeping it out
382
+ of the view config means duplicating, sharing or exporting a view does not drag one
383
+ person's rail position along with it. It also means a SHARED view can sit in a different
384
+ place for each person who can see it, which is the only coherent answer once two people
385
+ share one view.
386
+
387
+ An empty list CLEARS the arrangement (back to server order) rather than storing `[]`.
388
+ """
389
+ def _set(ws):
390
+ clean = []
391
+ seen = set()
392
+ for vid in (order or []):
393
+ vid = str(vid).strip()[:120]
394
+ if vid and vid not in seen:
395
+ seen.add(vid)
396
+ clean.append(vid)
397
+ if clean:
398
+ ws['viewOrder'] = clean
399
+ else:
400
+ ws.pop('viewOrder', None)
401
+ self._update(username, _set)
402
+
403
+ def shared_views(self, viewer, is_admin=False):
404
+ """Every SHARED view this viewer may see, by id (wave-9 I17).
405
+
406
+ Read-only and independent of the viewer's own workspace: the caller merges. Returns
407
+ only what `_may_see` allows, so a caller cannot accidentally render somebody else's
408
+ personal view by forgetting to filter.
409
+ """
410
+ try:
411
+ bucket = ((self._st.get(self.table_key) or {}).get(SHARED_KEY) or {}).get('views') or {}
412
+ except Exception:
413
+ return {}
414
+ return {vid: dict(v) for vid, v in bucket.items()
415
+ if _may_see(v, viewer, is_admin)}
416
+
417
+ def shared_view(self, view_id):
418
+ """One shared view RAW β€” no visibility filter. For authorisation decisions only: a
419
+ caller must know a view exists and who owns it before it can decide whether the actor
420
+ may touch it. Never hand the result to a renderer without checking `_may_see`."""
421
+ try:
422
+ return ((self._st.get(self.table_key) or {}).get(SHARED_KEY) or {}
423
+ ).get('views', {}).get(str(view_id))
424
+ except Exception:
425
+ return None
426
+
427
+ def save_view(self, username, view, shared=None, reserved_names=(), is_admin=False):
428
+ """Upsert a SavedView into its ONE home β€” personal workspace or the shared bucket.
429
+
430
+ `shared` defaults to reading the view's own permissions (`is_shared`). Whichever home
431
+ it lands in, the view is REMOVED from the other, so a view can never exist as two
432
+ copies that diverge on the next edit.
433
+
434
+ ⚠ AUTHORISATION IS THE CALLER'S JOB and must happen BEFORE this is called β€” this layer
435
+ moves data and does not know who is asking. `_cl_handle_one` is the wall.
436
+ """
437
+ view_id = str((view or {}).get('id') or '').strip()
438
+ if not view_id:
439
+ raise ValueError('view id is required')
440
+ if username == SHARED_KEY:
441
+ raise ValueError('reserved username')
442
+ to_shared = is_shared(view) if shared is None else bool(shared)
443
+ requested = dict(view)
444
+ accepted = {}
445
+
446
+ def _up(data):
447
+ # View names are tenant-global: every personal workspace plus the shared bucket.
448
+ # This deliberately includes views the actor cannot see. The only disclosed fact
449
+ # is that a display name is already taken, while the categorical "no duplicate
450
+ # view names" contract remains true when a personal view is later shared.
451
+ names = list(reserved_names or ())
452
+ for workspace in data.values():
453
+ if not isinstance(workspace, dict):
454
+ continue
455
+ names.extend(
456
+ value.get('name')
457
+ for candidate_id, value in (workspace.get('views') or {}).items()
458
+ if candidate_id != view_id and isinstance(value, dict)
459
+ )
460
+ payload = dict(requested)
461
+ payload['name'] = _unique_name(payload.get('name'), names)
462
+ accepted.clear()
463
+ accepted.update(payload)
464
+ if to_shared:
465
+ bucket = data.setdefault(SHARED_KEY, {})
466
+ bucket.setdefault('views', {})[view_id] = payload
467
+ # it may have lived in the creator's workspace before being shared
468
+ owner = data.get(payload.get('createdBy') or username) or {}
469
+ (owner.get('views') or {}).pop(view_id, None)
470
+ else:
471
+ ws = data.setdefault(username, {})
472
+ ws.setdefault('views', {})[view_id] = payload
473
+ (data.get(SHARED_KEY, {}).get('views') or {}).pop(view_id, None)
474
+ return data
475
+
476
+ self._st.update(self.table_key, _up, flush='async')
477
+ return dict(accepted)
478
+
479
+ def delete_view(self, username, view_id):
480
+ """Delete a custom/list view override. The system all-rows view is guarded by caller.
481
+
482
+ Removes from BOTH homes: the caller has already authorised the delete, and leaving a
483
+ stale copy in the other bucket would resurrect the view on the next read.
484
+ """
485
+ vid = str(view_id)
486
+
487
+ def _up(data):
488
+ (data.get(username, {}).get('views') or {}).pop(vid, None)
489
+ (data.get(SHARED_KEY, {}).get('views') or {}).pop(vid, None)
490
+ return data
491
+
492
+ self._st.update(self.table_key, _up, flush='async')
493
+
494
+ def save_field(self, username, field, reserved_names=(), correction_id=None):
495
+ """Persist a column note or a user-created (custom_/measure_) field definition."""
496
+ key = str((field or {}).get('key') or '').strip()
497
+ if not key:
498
+ raise ValueError('field key is required')
499
+ requested = dict(field)
500
+ accepted = {}
501
+
502
+ def _save(ws):
503
+ names = list(reserved_names or ())
504
+ names.extend(
505
+ value.get('label')
506
+ for candidate_key, value in (ws.get('fields') or {}).items()
507
+ if candidate_key != key and isinstance(value, dict)
508
+ )
509
+ payload = dict(requested)
510
+ payload.pop('labelCorrectedFrom', None)
511
+ payload.pop('labelCorrectionId', None)
512
+ requested_label = ' '.join(
513
+ str(payload.get('label') or 'Untitled').split())[:120].rstrip()
514
+ payload['label'] = _unique_name(requested_label, names)
515
+ corrections = ws.setdefault('fieldCorrections', {})
516
+ corrections.pop(key, None)
517
+ if payload['label'] != requested_label and correction_id:
518
+ corrections[key] = {
519
+ 'label': payload['label'],
520
+ 'labelCorrectedFrom': requested_label,
521
+ 'labelCorrectionId': str(correction_id)[:180],
522
+ }
523
+ if not corrections:
524
+ ws.pop('fieldCorrections', None)
525
+ accepted.clear()
526
+ accepted.update(payload)
527
+ if source_override_is_empty(payload):
528
+ ws['fields'].pop(key, None)
529
+ else:
530
+ ws['fields'][key] = payload
531
+
532
+ self._update(username, _save)
533
+ return dict(accepted)
534
+
535
+ def duplicate_field(self, username, source_key, new_key, field,
536
+ reserved_names=(), correction_id=None):
537
+ """Clone a user-created field in ONE store transaction (wave-5 item 1): the new
538
+ definition plus β€” for `custom_` overlay sources only β€” every stored cell value under
539
+ the source key. One transaction, because a def without its values (or values without a
540
+ def) is exactly the orphan state delete_field exists to prevent, in reverse.
541
+ The caller validated both keys (same created stratum) and stamped the clone's
542
+ createdBy; this layer only moves data."""
543
+ source_key = str(source_key or '').strip()
544
+ new_key = str(new_key or '').strip()
545
+ if not source_key or not new_key or source_key == new_key:
546
+ raise ValueError('duplicate_field needs two distinct keys')
547
+ requested = dict(field)
548
+ accepted = {}
549
+
550
+ def _dup(ws):
551
+ names = list(reserved_names or ())
552
+ names.extend(
553
+ value.get('label')
554
+ for candidate_key, value in (ws.get('fields') or {}).items()
555
+ if candidate_key != new_key and isinstance(value, dict)
556
+ )
557
+ payload = dict(requested)
558
+ payload.pop('labelCorrectedFrom', None)
559
+ payload.pop('labelCorrectionId', None)
560
+ requested_label = ' '.join(
561
+ str(payload.get('label') or 'Untitled').split())[:120].rstrip()
562
+ payload['label'] = _unique_name(requested_label, names)
563
+ corrections = ws.setdefault('fieldCorrections', {})
564
+ corrections.pop(new_key, None)
565
+ if payload['label'] != requested_label and correction_id:
566
+ corrections[new_key] = {
567
+ 'label': payload['label'],
568
+ 'labelCorrectedFrom': requested_label,
569
+ 'labelCorrectionId': str(correction_id)[:180],
570
+ }
571
+ if not corrections:
572
+ ws.pop('fieldCorrections', None)
573
+ accepted.clear()
574
+ accepted.update(payload)
575
+ ws['fields'][new_key] = payload
576
+ if source_key.startswith('custom_'):
577
+ for row in ws['overlays'].values():
578
+ if isinstance(row, dict) and source_key in row:
579
+ row[new_key] = row[source_key]
580
+
581
+ self._update(username, _dup)
582
+ return dict(accepted)
583
+
584
+ def delete_field(self, username, key):
585
+ """Delete a USER-CREATED field definition outright (owner gap closed 2026-07-27).
586
+
587
+ Only the created strata ever reach here (`custom_` overlay fields, `measure_` formula
588
+ columns β€” the caller enforces the prefix). The stored overlay VALUES for the key are
589
+ scrubbed with it: a deleted column's cells must not linger as orphan data that would
590
+ silently resurface if the key were ever reused. Views referencing the key self-heal on
591
+ their next autosave (an unknown colId is dropped) β€” the rule every stale key rides.
592
+ """
593
+ key = str(key or '').strip()
594
+ if not key:
595
+ return
596
+
597
+ def _drop(ws):
598
+ ws['fields'].pop(key, None)
599
+ for row in ws['overlays'].values():
600
+ if isinstance(row, dict):
601
+ row.pop(key, None)
602
+
603
+ self._update(username, _drop)
604
+
605
+ def patch_overlay(self, username, pid, updates):
606
+ """Patch only the external editable stratum; never writes to the source system."""
607
+ clean = dict(updates or {})
608
+ if not clean:
609
+ return
610
+
611
+ def _patch(ws):
612
+ ws['overlays'].setdefault(str(int(pid)), {}).update(clean)
613
+
614
+ self._update(username, _patch)
615
+
616
+
617
+ def make(table_key, st=None):
618
+ return TableStore(table_key, st=st)
platform/core/user_tables.py CHANGED
@@ -1574,6 +1574,64 @@ def is_computed_cell(field):
1574
  or is_derived_link(f))
1575
 
1576
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1577
  #: ⭐⭐ 2026-08-09 (owner) β€” THE STAMP THAT SURVIVES A RECONCILER.
1578
  #:
1579
  #: Owner: *"No rollup field should be uneditable, everything is custom and changeable always."*
 
1574
  or is_derived_link(f))
1575
 
1576
 
1577
+ #: The numeric family and the date family, named once. Membership decides which cells the
1578
+ #: refusal below can speak about at all β€” a `text` column takes any string by definition.
1579
+ _NUMERIC_CELL_TYPES = ('int', 'currency', 'pct')
1580
+ _DATE_CELL_TYPES = ('date',)
1581
+
1582
+
1583
+ def cell_type_refusal(field, value):
1584
+ """Why this value cannot go in this column, or None if it can. β›” REFUSES; never rewrites.
1585
+
1586
+ ⭐⭐ W29-T81 β€” THE SERVER-SIDE HALF of "can this text be this field's value". Until now the
1587
+ only evaluator was `coerceClipboardValue`, which lives in the BROWSER β€” so the import door
1588
+ took `{"qa_count": "seventeen-ish"}` at an `int` column, answered `201 {"imported": 1}`, and
1589
+ stored it verbatim. Measured against production on `bac40c2`. A rule enforced only by the
1590
+ client is a rule that holds for exactly one client ([[one-evaluator-per-question]]).
1591
+
1592
+ ⚠ DELIBERATELY NARROWER THAN THE CLIENT'S COERCER, and it must stay that way: this asks only
1593
+ whether a NUMBER is a number and a DATE is a date, the two families where an unusable string
1594
+ is later rendered as a fabricated figure. It accepts everything the client's coercer emits
1595
+ (canonical ISO dates, plain decimals) plus the human spellings that coercer also takes
1596
+ ("$1,234.50"), so no UI path can trip a wall the UI cannot see. Choice vocabularies stay
1597
+ client-side for now: their options change under a stored row, and refusing an import against
1598
+ a vocabulary edited yesterday would reject data that is merely out of date.
1599
+
1600
+ ⚠ BLANK IS ALWAYS ACCEPTABLE. An empty cell is an empty cell in every column; the import
1601
+ plan already omits them, and refusing one would make a ragged spreadsheet unimportable.
1602
+ """
1603
+ f = field or {}
1604
+ ftype = f.get('type')
1605
+ text = '' if value is None else str(value).strip()
1606
+ if not text:
1607
+ return None
1608
+ label = f.get('label') or f.get('key') or 'that column'
1609
+ if ftype in _NUMERIC_CELL_TYPES:
1610
+ try:
1611
+ float(text.replace('$', '').replace(',', '').replace('%', '').replace(' ', ''))
1612
+ except (TypeError, ValueError):
1613
+ return f'{text[:40]!r} is not a number, and {label!r} holds numbers'
1614
+ return None
1615
+ if ftype == 'rating':
1616
+ try:
1617
+ n = int(text)
1618
+ except (TypeError, ValueError):
1619
+ return f'{text[:40]!r} is not a whole number, and {label!r} holds a rating'
1620
+ top = f.get('max') if isinstance(f.get('max'), int) and f.get('max') else 5
1621
+ if not 1 <= n <= top:
1622
+ return f'{label!r} takes a rating from 1 to {top}, and {text[:40]!r} is outside it'
1623
+ return None
1624
+ if ftype in _DATE_CELL_TYPES:
1625
+ # ISO-prefixed, which is exactly what the client's coercer produces and what every
1626
+ # reader of a stored date assumes. A month name is not refused as invalid English β€” it
1627
+ # is refused because nothing downstream would parse it either.
1628
+ if not re.match(r'^\d{4}-\d{2}-\d{2}', text):
1629
+ return (f'{text[:40]!r} is not a date, and {label!r} holds dates '
1630
+ f'(write it as YYYY-MM-DD)')
1631
+ return None
1632
+ return None
1633
+
1634
+
1635
  #: ⭐⭐ 2026-08-09 (owner) β€” THE STAMP THAT SURVIVES A RECONCILER.
1636
  #:
1637
  #: Owner: *"No rollup field should be uneditable, everything is custom and changeable always."*
web/src/customer-grid/ImportDialog.tsx CHANGED
@@ -28,9 +28,12 @@ import { useMemo, useRef, useState } from "react";
28
  // ⚠ From `apiContract`, which is where these two are DECLARED β€” not re-exported from
29
  // `apiBridge`, which does not export them and is another session's fence this wave.
30
  import { API_V1, CREDENTIALS } from "../apiContract";
31
- import { coerceClipboardValue } from "./clipboard";
32
- import { choiceOptions, isImportTarget } from "./types";
33
  import type { Field } from "./types";
 
 
 
 
34
  import "./ImportDialog.css";
35
 
36
  /** The one header a JSON POST needs. `apiBridge` keeps its own module-local copy; this is a
@@ -38,13 +41,10 @@ import "./ImportDialog.css";
38
  * this session's to change. */
39
  const JSON_HEADERS = { "Content-Type": "application/json" };
40
 
41
- /** The upload door's answer (`routes_uploads.tabular`). */
42
- interface Parsed {
43
- columns: string[];
44
- rows: number;
45
- values: string[][];
46
- truncated?: boolean;
47
- }
48
 
49
  export interface ImportDialogProps {
50
  /** The target database's fields β€” the mapping targets, before this file filters them. */
@@ -68,7 +68,7 @@ export default function ImportDialog({
68
  onClose,
69
  }: ImportDialogProps): React.ReactElement {
70
  const [parsed, setParsed] = useState<Parsed | null>(null);
71
- const [mapping, setMapping] = useState<Record<number, string>>({});
72
  const [busy, setBusy] = useState(false);
73
  const [error, setError] = useState("");
74
  const fileRef = useRef<HTMLInputElement | null>(null);
@@ -108,7 +108,7 @@ export default function ImportDialog({
108
  const byName = new Map(
109
  targets.map((f) => [f.label.trim().toLowerCase(), f.key] as const)
110
  );
111
- const guess: Record<number, string> = {};
112
  (body.columns ?? []).forEach((name, i) => {
113
  const hit = byName.get(String(name).trim().toLowerCase());
114
  if (hit) guess[i] = hit;
@@ -122,40 +122,14 @@ export default function ImportDialog({
122
  }
123
 
124
  /**
125
- * Coerce every mapped cell and collect the refusals. Runs over the WHOLE parsed file, not the
126
- * preview: the point of this step is that a bad value on row 900 is reported BEFORE anything is
127
- * written, rather than discovered as a half-imported file.
128
  */
129
- const plan = useMemo(() => {
130
- const problems: { row: number; column: string; value: string; field: string }[] = [];
131
- const rows: Record<string, string>[] = [];
132
- if (!parsed) return { rows, problems };
133
- const mapped = Object.entries(mapping).filter(([, key]) => key !== NONE);
134
- for (let r = 0; r < (parsed.values?.length ?? 0); r += 1) {
135
- const out: Record<string, string> = {};
136
- let blank = true;
137
- for (const [idxText, key] of mapped) {
138
- const field = targets.find((f) => f.key === key);
139
- if (!field) continue;
140
- const raw = String(parsed.values[r]?.[Number(idxText)] ?? "");
141
- if (raw.trim() === "") continue;
142
- blank = false;
143
- const value = coerceClipboardValue(field, raw, choiceOptions(field));
144
- if (value === null) {
145
- problems.push({
146
- row: r + 2, // +1 for the header row, +1 because humans count from 1
147
- column: parsed.columns[Number(idxText)] ?? `Column ${Number(idxText) + 1}`,
148
- value: raw.slice(0, 40),
149
- field: field.label,
150
- });
151
- continue;
152
- }
153
- out[key] = String(value);
154
- }
155
- if (!blank) rows.push(out);
156
- }
157
- return { rows, problems };
158
- }, [parsed, mapping, targets]);
159
 
160
  async function run(): Promise<void> {
161
  setBusy(true);
 
28
  // ⚠ From `apiContract`, which is where these two are DECLARED β€” not re-exported from
29
  // `apiBridge`, which does not export them and is another session's fence this wave.
30
  import { API_V1, CREDENTIALS } from "../apiContract";
31
+ import { isImportTarget } from "./types";
 
32
  import type { Field } from "./types";
33
+ // ⭐ W29-T76 β€” the plan lives in a pure module so a test can EXECUTE it against the server's real
34
+ // payload. This file holds the WINDOW; `importPlan.ts` holds what would be written.
35
+ import { buildImportPlan } from "./importPlan";
36
+ import type { ImportMapping, ParsedUpload } from "./importPlan";
37
  import "./ImportDialog.css";
38
 
39
  /** The one header a JSON POST needs. `apiBridge` keeps its own module-local copy; this is a
 
41
  * this session's to change. */
42
  const JSON_HEADERS = { "Content-Type": "application/json" };
43
 
44
+ /** The upload door's answer (`routes_uploads.tabular`) β€” declared in `importPlan.ts`, beside the
45
+ * only code that reads its shape. β›” It is COLUMN-MAJOR; the copy that used to live here said
46
+ * `string[][]` and that one wrong word is why nothing ever imported. */
47
+ type Parsed = ParsedUpload;
 
 
 
48
 
49
  export interface ImportDialogProps {
50
  /** The target database's fields β€” the mapping targets, before this file filters them. */
 
68
  onClose,
69
  }: ImportDialogProps): React.ReactElement {
70
  const [parsed, setParsed] = useState<Parsed | null>(null);
71
+ const [mapping, setMapping] = useState<ImportMapping>({});
72
  const [busy, setBusy] = useState(false);
73
  const [error, setError] = useState("");
74
  const fileRef = useRef<HTMLInputElement | null>(null);
 
108
  const byName = new Map(
109
  targets.map((f) => [f.label.trim().toLowerCase(), f.key] as const)
110
  );
111
+ const guess: ImportMapping = {};
112
  (body.columns ?? []).forEach((name, i) => {
113
  const hit = byName.get(String(name).trim().toLowerCase());
114
  if (hit) guess[i] = hit;
 
122
  }
123
 
124
  /**
125
+ * What would be written, and what cannot be. The work is `importPlan.buildImportPlan` β€” a pure
126
+ * module, so the node suite executes it against the server's real payload rather than grepping
127
+ * this file for the shape of a loop.
128
  */
129
+ const plan = useMemo(
130
+ () => buildImportPlan(parsed, mapping, targets),
131
+ [parsed, mapping, targets]
132
+ );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
 
134
  async function run(): Promise<void> {
135
  setBusy(true);
web/src/customer-grid/cells.ts CHANGED
@@ -51,7 +51,8 @@ import {
51
  // distinguishable at the call site rather than one silently shadowing the other.
52
  import { optionTint, pickTint as choicePickTint } from "./choiceColors";
53
  import { automationState, avatarInitials, avatarSize, checkboxOn, dateTimeText, formulaIsBlank,
54
- codePreview, formulaIsText, jsonPreview, num, numberText, userCellPayload } from "./display";
 
55
  import type { AutomationState, CellValue, UserCellData } from "./display";
56
 
57
  // Wave-7 (item W2): the pure display-string half moved to display.ts so the
@@ -430,21 +431,26 @@ function baseCell(
430
  ): GridCell {
431
  const ro = { allowOverlay: editable, readonly: !editable };
432
  const blank = v == null || v === "";
 
 
 
 
 
433
 
434
  switch (field.type) {
435
  case "currency":
436
  return {
437
  kind: GridCellKind.Number,
438
- data: blank ? undefined : num(v),
439
- displayData: blank ? "" : "$" + numberText(num(v), field.format),
440
  contentAlign: "right",
441
  ...ro,
442
  };
443
  case "int":
444
  return {
445
  kind: GridCellKind.Number,
446
- data: blank ? undefined : num(v),
447
- displayData: blank ? "" : numberText(num(v), field.format),
448
  contentAlign: "right",
449
  ...ro,
450
  };
@@ -487,8 +493,8 @@ function baseCell(
487
  case "pct":
488
  return {
489
  kind: GridCellKind.Number,
490
- data: blank ? undefined : num(v),
491
- displayData: blank ? "" : num(v).toFixed(1) + "%",
492
  contentAlign: "right",
493
  ...ro,
494
  };
 
51
  // distinguishable at the call site rather than one silently shadowing the other.
52
  import { optionTint, pickTint as choicePickTint } from "./choiceColors";
53
  import { automationState, avatarInitials, avatarSize, checkboxOn, dateTimeText, formulaIsBlank,
54
+ codePreview, formulaIsText, jsonPreview, num, numberText, numericIsBlank,
55
+ userCellPayload } from "./display";
56
  import type { AutomationState, CellValue, UserCellData } from "./display";
57
 
58
  // Wave-7 (item W2): the pure display-string half moved to display.ts so the
 
431
  ): GridCell {
432
  const ro = { allowOverlay: editable, readonly: !editable };
433
  const blank = v == null || v === "";
434
+ // β›” W29-T81 β€” A NUMERIC COLUMN HOLDING A NON-NUMBER PAINTS NOTHING, not `0`. `blank` above is
435
+ // emptiness; this is emptiness OR a value no reader would call a number. The import door can
436
+ // now put a spreadsheet's "seventeen-ish" in an `int` column, and `num()` answers 0 for it β€”
437
+ // a fabricated figure on the canvas beside an honest em-dash in the record panel.
438
+ const noNumber = numericIsBlank(v);
439
 
440
  switch (field.type) {
441
  case "currency":
442
  return {
443
  kind: GridCellKind.Number,
444
+ data: noNumber ? undefined : num(v),
445
+ displayData: noNumber ? "" : "$" + numberText(num(v), field.format),
446
  contentAlign: "right",
447
  ...ro,
448
  };
449
  case "int":
450
  return {
451
  kind: GridCellKind.Number,
452
+ data: noNumber ? undefined : num(v),
453
+ displayData: noNumber ? "" : numberText(num(v), field.format),
454
  contentAlign: "right",
455
  ...ro,
456
  };
 
493
  case "pct":
494
  return {
495
  kind: GridCellKind.Number,
496
+ data: noNumber ? undefined : num(v),
497
+ displayData: noNumber ? "" : num(v).toFixed(1) + "%",
498
  contentAlign: "right",
499
  ...ro,
500
  };
web/src/customer-grid/display.ts CHANGED
@@ -80,6 +80,30 @@ export function num(v: CellValue): number {
80
  return Number.isFinite(n) ? n : 0;
81
  }
82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  /**
84
  * β›” IS THIS FORMULA RESULT TEXT? β€” and it must be asked of the RAW value.
85
  *
 
80
  return Number.isFinite(n) ? n : 0;
81
  }
82
 
83
+ /**
84
+ * β›” W29-T81 β€” HAS THIS NUMERIC CELL NOTHING TO SHOW? Blank, or a stored value that is not a
85
+ * number at all.
86
+ *
87
+ * `num()` above answers **0** for anything unparseable, which is a measurement where there was
88
+ * none. That is survivable for a column only this app writes; it stopped being survivable when
89
+ * the import door began accepting rows from a spreadsheet β€” a `qty` column holding
90
+ * "seventeen-ish" painted `0` on the canvas while the record panel honestly showed an em-dash.
91
+ * Two surfaces disagreeing about the same cell, and the one that looks authoritative is the one
92
+ * making the number up.
93
+ *
94
+ * ⚠ The FOLD was already right (`aggregations.numOf` skips what will not parse), so the totals
95
+ * row never counted these β€” it is the painted cell alone. Same family as `formulaIsText` below,
96
+ * asked of the RAW value for the same reason.
97
+ * ⚠ `" "` is blank, not a zero: `Number(" ")` is 0, which is how a whitespace cell becomes a
98
+ * number nobody typed.
99
+ */
100
+ export function numericIsBlank(v: CellValue): boolean {
101
+ if (v === null || v === undefined) return true;
102
+ const text = typeof v === "number" ? String(v) : String(v).trim();
103
+ if (text === "") return true;
104
+ return !Number.isFinite(Number(text));
105
+ }
106
+
107
  /**
108
  * β›” IS THIS FORMULA RESULT TEXT? β€” and it must be asked of the RAW value.
109
  *
web/src/customer-grid/importPlan.ts ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // ---------------------------------------------------------------------------
2
+ // customer-grid / importPlan.ts
3
+ //
4
+ // ⭐⭐ WAVE-29 T76 β€” THE IMPORT PLAN: what a parsed spreadsheet would write, and what it cannot.
5
+ //
6
+ // Lifted OUT of `ImportDialog.tsx` and given no React and no stylesheet, for one reason: the
7
+ // dialog's headline feature had NEVER imported a row, and nothing could see it. The gate greps
8
+ // the .tsx for strings, and the node suite could only reach the pure `isImportTarget` predicate β€”
9
+ // so the one thing neither could do was RUN the plan builder against the payload the server
10
+ // actually sends. It runs here ([[gate-answers-the-wrong-question]]).
11
+ //
12
+ // β›” THE PAYLOAD IS COLUMN-MAJOR, AND THIS FILE IS THE ONLY PLACE THAT SHAPE IS READ.
13
+ // `routes_uploads.tabular` answers `{"values": {columnName: [cell, cell, …]}}` β€” its docstring
14
+ // says so at :4 and `:86` builds it β€” while the old reader declared `string[][]` and indexed
15
+ // `values[row][col]`. Every read was `undefined`, `values.length` was `undefined ?? 0`, the plan
16
+ // loop ran zero times, and the footer honestly reported "0 records" about a file full of them.
17
+ // The correct reader was already written one file away (`SelectFromFile.tsx:69`, by column NAME).
18
+ //
19
+ // ⚠ NAME-INDEXING IS SAFE BECAUSE THE SERVER DEDUPES ITS HEADERS. `routes_uploads._headers`
20
+ // renames a second "SKU" to "SKU (2)", so `columns[i]` is a unique key into `values` β€” a sheet
21
+ // with two identically-named columns maps two distinct pickers, never one merged list.
22
+ //
23
+ // ⚠ THE ROW COUNT IS THE SERVER'S OWN `rows`, never a column array's length: a mapping that
24
+ // names no column must still not invent a row count, and a short column is padded by `?? ""`
25
+ // rather than silently truncating the file.
26
+ // ---------------------------------------------------------------------------
27
+
28
+ import { coerceClipboardValue } from "./clipboard";
29
+ import { choiceOptions } from "./types";
30
+ import type { Field } from "./types";
31
+
32
+ /** The upload door's answer (`routes_uploads.tabular`). */
33
+ export interface ParsedUpload {
34
+ columns: string[];
35
+ /** Data rows READ (blank rows skipped, cap disclosed by `truncated`) β€” the loop's bound. */
36
+ rows: number;
37
+ /** β›” COLUMN-MAJOR: keyed by the deduped column NAME, one array of cells per column. */
38
+ values: Record<string, string[]>;
39
+ truncated?: boolean;
40
+ }
41
+
42
+ /** One refused cell, named the way the person holding the spreadsheet can find it. */
43
+ export interface ImportProblem {
44
+ /** 1-based, and +1 again for the header row β€” what the file's own row numbers say. */
45
+ row: number;
46
+ column: string;
47
+ value: string;
48
+ field: string;
49
+ }
50
+
51
+ export interface ImportPlan {
52
+ /** Field-keyed rows, ready for `POST /tables/{key}/rows/import`. */
53
+ rows: Record<string, string>[];
54
+ problems: ImportProblem[];
55
+ }
56
+
57
+ /** Column index (the picker's key) β†’ field key, or `""` for "don't import". */
58
+ export type ImportMapping = Record<number, string>;
59
+
60
+ const NONE = "";
61
+
62
+ /**
63
+ * THE ONE PLACE the wire shape is read. A column's cells, by the index the picker holds.
64
+ *
65
+ * ⚠ Keep this a named function rather than an inline expression: it is the single line a
66
+ * negative control can mutate to restore the row-major misreading, which is how the gate proves
67
+ * it can still see this defect ([[gate-negative-control]]).
68
+ */
69
+ export function columnCells(parsed: ParsedUpload, index: number): readonly string[] {
70
+ const name = parsed.columns?.[index] ?? "";
71
+ return parsed.values?.[name] ?? [];
72
+ }
73
+
74
+ /**
75
+ * Coerce every mapped cell and collect the refusals. Runs over the WHOLE file, not the preview:
76
+ * the point of this step is that a bad value on row 900 is reported BEFORE anything is written,
77
+ * rather than discovered as a half-imported file.
78
+ *
79
+ * β›” THE REFUSAL SENTINEL IS `undefined`. `coerceClipboardValue` is typed `string | undefined`
80
+ * and every refusal path returns `undefined`; the old check tested `=== null`, which no path can
81
+ * produce β€” so a refused value was neither reported NOR skipped, it was written as the four-letter
82
+ * string `String(undefined)`. `""` is NOT a refusal: it is an explicit clear (a checkbox's
83
+ * "FALSE"), and it keeps its row.
84
+ */
85
+ export function buildImportPlan(
86
+ parsed: ParsedUpload | null | undefined,
87
+ mapping: ImportMapping,
88
+ targets: Field[]
89
+ ): ImportPlan {
90
+ const problems: ImportProblem[] = [];
91
+ const rows: Record<string, string>[] = [];
92
+ if (!parsed) return { rows, problems };
93
+
94
+ // Resolved ONCE per column rather than per cell: a 20,000-row file would otherwise re-scan the
95
+ // field list 20,000 times per mapped column.
96
+ const mapped = Object.entries(mapping)
97
+ .filter(([, key]) => key !== NONE)
98
+ .map(([indexText, key]) => {
99
+ const index = Number(indexText);
100
+ return {
101
+ key,
102
+ field: targets.find((f) => f.key === key),
103
+ column: parsed.columns?.[index] ?? `Column ${index + 1}`,
104
+ cells: columnCells(parsed, index),
105
+ choices: undefined as string[] | undefined,
106
+ };
107
+ })
108
+ .filter((m) => m.field !== undefined);
109
+ for (const m of mapped) m.choices = choiceOptions(m.field as Field);
110
+
111
+ const total = Number.isFinite(parsed.rows) ? Math.max(0, Math.trunc(parsed.rows)) : 0;
112
+ for (let r = 0; r < total; r += 1) {
113
+ const out: Record<string, string> = {};
114
+ // A row of only unmapped or blank cells is padding, not a record β€” importing it would add an
115
+ // empty record per trailing line of the sheet.
116
+ let blank = true;
117
+ for (const m of mapped) {
118
+ const raw = String(m.cells[r] ?? "");
119
+ if (raw.trim() === "") continue;
120
+ blank = false;
121
+ const value = coerceClipboardValue(m.field as Field, raw, m.choices);
122
+ if (value === undefined) {
123
+ problems.push({
124
+ row: r + 2, // +1 for the header row, +1 because humans count from 1
125
+ column: m.column,
126
+ value: raw.slice(0, 40),
127
+ field: (m.field as Field).label,
128
+ });
129
+ continue;
130
+ }
131
+ out[m.key] = value;
132
+ }
133
+ if (!blank) rows.push(out);
134
+ }
135
+ return { rows, problems };
136
+ }
web/src/customer-grid/useVisibleRows.ts CHANGED
@@ -636,6 +636,20 @@ function groupRows(
636
  * counts the thing the reader is counting.
637
  */
638
  export function sliceForDisplay(rows: VisibleRow[], cap: number): VisibleRow[] {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
639
  const out: VisibleRow[] = [];
640
  let taken = 0;
641
  /** The group being accumulated. Held back until its footer arrives, because a group without
@@ -686,6 +700,40 @@ export function sliceForDisplay(rows: VisibleRow[], cap: number): VisibleRow[] {
686
  return out;
687
  }
688
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
689
  /** Everything `useVisibleRows` does, minus the memo β€” so it is callable from a plain test.
690
  * The hook is a one-line wrapper; this is where the pipeline actually lives. */
691
  export interface PipelineInput {
 
636
  * counts the thing the reader is counting.
637
  */
638
  export function sliceForDisplay(rows: VisibleRow[], cap: number): VisibleRow[] {
639
+ const whole = admitWholeGroups(rows, cap);
640
+ // β›” W29-T83 β€” AN EMPTY GRID IS NEVER AN HONEST ANSWER. Grouping the 3,627-record Customer
641
+ // database by State painted "Showing first 0 of 3,627": the first bucket (Texas, 75 rows) is
642
+ // larger than the 50-row cap, so it was dropped WHOLE β€” correct by the rule above β€” and the
643
+ // loop then hit its `break` and abandoned every LATER group as well. The rule is right; the
644
+ // blast radius was the whole view. When admitting whole groups yields nothing at all, the
645
+ // reader gets the first group TRUNCATED and SAID SO, rather than a blank screen.
646
+ if (whole.length || cap <= 0) return whole;
647
+ return firstGroupTruncated(rows, cap);
648
+ }
649
+
650
+ /** The whole-groups-only pass β€” the original rule, unchanged: a data row costs one, structural
651
+ * rows cost nothing, and a group is admitted with its own footer or not at all. */
652
+ function admitWholeGroups(rows: VisibleRow[], cap: number): VisibleRow[] {
653
  const out: VisibleRow[] = [];
654
  let taken = 0;
655
  /** The group being accumulated. Held back until its footer arrives, because a group without
 
700
  return out;
701
  }
702
 
703
+ /**
704
+ * β›” W29-T83 β€” THE RESCUE, reached only when the whole-group pass admitted NOTHING.
705
+ *
706
+ * The FIRST group, truncated to the cap, under a header that says it was truncated β€” and
707
+ * deliberately WITHOUT its footer: that subtotal folds all M rows of the bucket, and printing it
708
+ * under N of them is exactly the "number describing rows the reader cannot see" the whole-group
709
+ * rule exists to prevent ([[no-unverifiable-aggregates]]). The header's own `(M)` count stays
710
+ * true, so the label reads `β–Ό Texas (US) β€” first 50 (75)`.
711
+ *
712
+ * ⚠ Only the first group: any later one would need budget this one has already spent, and
713
+ * skipping to a smaller bucket further down would silently answer a different question than the
714
+ * one the reader asked ("group by State" starting at group seven).
715
+ */
716
+ function firstGroupTruncated(rows: VisibleRow[], cap: number): VisibleRow[] {
717
+ const out: VisibleRow[] = [];
718
+ let taken = 0;
719
+ for (const vr of rows) {
720
+ if (vr.kind === "group-header") {
721
+ if (out.length) break; // past the first group β€” nothing more is affordable
722
+ out.push({ ...vr, label: `${vr.label} β€” first ${cap.toLocaleString()}` });
723
+ continue;
724
+ }
725
+ // Its footer describes the whole bucket, so it stops the walk rather than joining it.
726
+ if (vr.kind === "group-footer") break;
727
+ if (vr.kind !== "data" || !out.length) continue;
728
+ if (taken >= cap) break;
729
+ out.push(vr);
730
+ taken += 1;
731
+ }
732
+ // A header with no rows under it paints as a collapsed group β€” worse than the blank grid this
733
+ // function exists to remove, because it looks deliberate.
734
+ return taken ? out : [];
735
+ }
736
+
737
  /** Everything `useVisibleRows` does, minus the memo β€” so it is callable from a plain test.
738
  * The hook is a one-line wrapper; this is where the pipeline actually lives. */
739
  export interface PipelineInput {